Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
6d0f0ece KM |
2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
6d0f0ece | 6 | * |
659ba9ca | 7 | * @(#)vfs_lookup.c 7.29 (Berkeley) %G% |
da7c5cc6 | 8 | */ |
10873320 | 9 | |
94368568 | 10 | #include "param.h" |
6d0f0ece KM |
11 | #include "time.h" |
12 | #include "namei.h" | |
13 | #include "vnode.h" | |
94368568 | 14 | #include "mount.h" |
6d0f0ece | 15 | #include "errno.h" |
c3a74062 | 16 | #include "malloc.h" |
5e00df3b | 17 | #include "filedesc.h" |
658f5fdc | 18 | #include "proc.h" |
0caff0ad KM |
19 | |
20 | #ifdef KTRACE | |
658f5fdc MT |
21 | #include "ktrace.h" |
22 | #endif | |
10873320 BJ |
23 | |
24 | /* | |
7f69b0e6 | 25 | * Convert a pathname into a pointer to a locked inode. |
6bd0bb92 | 26 | * This is a very central and rather complicated routine. |
7f69b0e6 | 27 | * |
6d0f0ece KM |
28 | * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on |
29 | * whether the name is to be looked up, created, renamed, or deleted. | |
30 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
31 | * creating, renaming, or deleting a directory entry may be calculated. | |
32 | * If flag has LOCKPARENT or'ed into it and the target of the pathname | |
33 | * exists, namei returns both the target and its parent directory locked. | |
34 | * When creating or renaming and LOCKPARENT is specified, the target may not | |
35 | * be ".". When deleting and LOCKPARENT is specified, the target may be ".". | |
4f083fd7 | 36 | * |
d870be74 | 37 | * The FOLLOW flag is set when symbolic links are to be followed |
4f083fd7 | 38 | * when they occur at the end of the name translation process. |
7f69b0e6 KM |
39 | * Symbolic links are always followed for all other pathname |
40 | * components other than the last. | |
41 | * | |
42 | * The segflg defines whether the name is to be copied from user | |
43 | * space or kernel space. | |
10873320 | 44 | * |
f93197fc | 45 | * Overall outline of namei: |
6bd0bb92 BJ |
46 | * |
47 | * copy in name | |
48 | * get starting directory | |
49 | * dirloop: | |
d870be74 | 50 | * copy next component of name to ndp->ni_dent |
6bd0bb92 | 51 | * handle degenerate case where name is null string |
6d0f0ece KM |
52 | * if .. and on mounted filesys, find parent |
53 | * call lookup routine for next component name | |
54 | * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set | |
55 | * component vnode returned in ni_vp (if it exists), locked. | |
6bd0bb92 | 56 | * if symbolic link, massage name in buffer and continue at dirloop |
6d0f0ece | 57 | * if result inode is mounted on, find mounted on vnode |
6bd0bb92 | 58 | * if more components of name, do next level at dirloop |
6d0f0ece KM |
59 | * return the answer in ni_vp as locked vnode; |
60 | * if LOCKPARENT set, return locked parent in ni_dvp | |
4f083fd7 | 61 | * |
6d0f0ece | 62 | * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked. |
10873320 | 63 | */ |
8429d022 | 64 | namei(ndp, p) |
d870be74 | 65 | register struct nameidata *ndp; |
8429d022 | 66 | struct proc *p; |
10873320 | 67 | { |
5e00df3b | 68 | register struct filedesc *fdp; /* pointer to file descriptor state */ |
6bd0bb92 | 69 | register char *cp; /* pointer into pathname argument */ |
6d0f0ece KM |
70 | register struct vnode *dp = 0; /* the directory we are searching */ |
71 | register int i; /* Temp counter */ | |
72 | struct vnode *tdp; /* saved dp */ | |
73 | struct mount *mp; /* mount table entry */ | |
87c05e6e | 74 | int docache; /* == 0 do not cache last component */ |
6d0f0ece KM |
75 | int flag; /* LOOKUP, CREATE, RENAME or DELETE */ |
76 | int wantparent; /* 1 => wantparent or lockparent flag */ | |
206be3f9 | 77 | int lockparent; /* 1 => lockparent flag */ |
d5cea2aa KM |
78 | int getbuf; /* 1 => Malloc a pathname buffer */ |
79 | int rdonly; /* mounted read-only flag bit(s) */ | |
6d0f0ece | 80 | int error = 0; |
10873320 | 81 | |
d5cea2aa KM |
82 | /* |
83 | * Setup: break out flag bits into variables. | |
84 | */ | |
8429d022 | 85 | ndp->ni_cred = p->p_ucred; |
5e00df3b | 86 | fdp = p->p_fd; |
dd4eee4b | 87 | ndp->ni_dvp = NULL; |
5f9e231a | 88 | flag = ndp->ni_nameiop & OPMASK; |
6d0f0ece | 89 | wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); |
206be3f9 | 90 | lockparent = ndp->ni_nameiop & LOCKPARENT; |
d870be74 | 91 | docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; |
d5cea2aa | 92 | getbuf = (ndp->ni_nameiop & HASBUF) ^ HASBUF; |
e828c39a | 93 | if (flag == DELETE || (wantparent && flag != CREATE)) |
f93197fc | 94 | docache = 0; |
54fb9dc2 | 95 | rdonly = MNT_RDONLY; |
d5cea2aa | 96 | if (ndp->ni_nameiop & REMOTE) |
54fb9dc2 | 97 | rdonly |= MNT_EXRDONLY; |
f5039631 | 98 | /* |
6bd0bb92 BJ |
99 | * Get a buffer for the name to be translated, and copy the |
100 | * name into the buffer. | |
f5039631 | 101 | */ |
d5cea2aa KM |
102 | if (getbuf) { |
103 | MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
104 | if (ndp->ni_segflg == UIO_SYSSPACE) | |
105 | error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, | |
106 | MAXPATHLEN, &ndp->ni_pathlen); | |
107 | else | |
108 | error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, | |
109 | MAXPATHLEN, &ndp->ni_pathlen); | |
110 | if (error) { | |
111 | free(ndp->ni_pnbuf, M_NAMEI); | |
112 | ndp->ni_vp = NULL; | |
113 | return (error); | |
114 | } | |
d870be74 | 115 | } |
5f9e231a | 116 | ndp->ni_ptr = ndp->ni_pnbuf; |
6d0f0ece | 117 | ndp->ni_loopcnt = 0; |
658f5fdc | 118 | #ifdef KTRACE |
5f9e231a KM |
119 | if (KTRPOINT(p, KTR_NAMEI)) |
120 | ktrnamei(p->p_tracep, ndp->ni_pnbuf); | |
658f5fdc | 121 | #endif |
6bd0bb92 | 122 | |
5f9e231a KM |
123 | /* |
124 | * Get starting point for the translation. | |
125 | */ | |
126 | if (ndp->ni_nameiop & STARTDIR) | |
127 | dp = ndp->ni_startdir; | |
128 | else | |
129 | dp = fdp->fd_cdir; | |
130 | VREF(dp); | |
6d0f0ece | 131 | start: |
10873320 | 132 | /* |
6bd0bb92 | 133 | * Get starting directory. |
6d0f0ece | 134 | * Done at start of translation and after symbolic link. |
10873320 | 135 | */ |
6d0f0ece | 136 | if (*ndp->ni_ptr == '/') { |
5f9e231a KM |
137 | if (ndp->ni_nameiop & STARTDIR) |
138 | panic("namei: illegal path"); | |
6d0f0ece KM |
139 | vrele(dp); |
140 | while (*ndp->ni_ptr == '/') { | |
141 | ndp->ni_ptr++; | |
142 | ndp->ni_pathlen--; | |
143 | } | |
5e00df3b | 144 | if ((dp = fdp->fd_rdir) == NULL) |
10873320 | 145 | dp = rootdir; |
8fe1c702 | 146 | VREF(dp); |
6d0f0ece KM |
147 | } |
148 | VOP_LOCK(dp); | |
9e7c949b | 149 | ndp->ni_endoff = 0; |
6bd0bb92 | 150 | |
10873320 | 151 | /* |
6bd0bb92 | 152 | * We come to dirloop to search a new directory. |
10873320 | 153 | */ |
6bd0bb92 | 154 | dirloop: |
6bd0bb92 | 155 | /* |
d870be74 | 156 | * Copy next component of name to ndp->ni_dent. |
6d0f0ece KM |
157 | * XXX kern_exec looks at d_name |
158 | * ??? The ni_hash value may be useful for vfs_cache | |
159 | * XXX There must be the last component of the filename left | |
160 | * somewhere accessible via. ndp for NFS (and any other stateless file | |
161 | * systems) in case they are doing a CREATE. The "Towards a..." noted | |
162 | * that ni_ptr would be left pointing to the last component, but since | |
163 | * the ni_pnbuf gets free'd, that is not a good idea. | |
6bd0bb92 | 164 | */ |
d5cea2aa KM |
165 | if (getbuf) { |
166 | ndp->ni_hash = 0; | |
167 | for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) { | |
168 | if (i >= MAXNAMLEN) { | |
169 | error = ENAMETOOLONG; | |
067da729 KM |
170 | goto bad; |
171 | } | |
d5cea2aa KM |
172 | if (*cp & 0200) |
173 | if ((*cp&0377) == ('/'|0200) || | |
174 | flag != DELETE) { | |
175 | error = EINVAL; | |
176 | goto bad; | |
177 | } | |
178 | ndp->ni_dent.d_name[i++] = *cp; | |
179 | ndp->ni_hash += (unsigned char)*cp * i; | |
180 | } | |
181 | ndp->ni_namelen = i; | |
182 | ndp->ni_dent.d_namlen = i; | |
183 | ndp->ni_dent.d_name[i] = '\0'; | |
184 | ndp->ni_pathlen -= i; | |
185 | ndp->ni_next = cp; | |
6d0f0ece | 186 | #ifdef NAMEI_DIAGNOSTIC |
d5cea2aa | 187 | printf("{%s}: ", ndp->ni_dent.d_name); |
6d0f0ece | 188 | #endif |
d5cea2aa KM |
189 | } |
190 | cp = ndp->ni_next; | |
6d0f0ece | 191 | ndp->ni_makeentry = 1; |
87c05e6e | 192 | if (*cp == '\0' && docache == 0) |
6d0f0ece KM |
193 | ndp->ni_makeentry = 0; |
194 | ndp->ni_isdotdot = (ndp->ni_namelen == 2 && | |
195 | ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.'); | |
6bd0bb92 BJ |
196 | |
197 | /* | |
198 | * Check for degenerate name (e.g. / or "") | |
199 | * which is a way of talking about a directory, | |
200 | * e.g. like "/." or ".". | |
201 | */ | |
6d0f0ece KM |
202 | if (ndp->ni_ptr[0] == '\0') { |
203 | if (flag != LOOKUP || wantparent) { | |
204 | error = EISDIR; | |
6bd0bb92 | 205 | goto bad; |
f5039631 | 206 | } |
d5cea2aa KM |
207 | if (getbuf) |
208 | free(ndp->ni_pnbuf, M_NAMEI); | |
6d0f0ece KM |
209 | if (!(ndp->ni_nameiop & LOCKLEAF)) |
210 | VOP_UNLOCK(dp); | |
211 | ndp->ni_vp = dp; | |
212 | return (0); | |
f5039631 | 213 | } |
6bd0bb92 | 214 | |
e47da406 | 215 | /* |
6d0f0ece KM |
216 | * Handle "..": two special cases. |
217 | * 1. If at root directory (e.g. after chroot) | |
218 | * then ignore it so can't get out. | |
219 | * 2. If this vnode is the root of a mounted | |
220 | * file system, then replace it with the | |
221 | * vnode which was mounted on so we take the | |
222 | * .. in the other file system. | |
e47da406 | 223 | */ |
6d0f0ece | 224 | if (ndp->ni_isdotdot) { |
e47da406 | 225 | for (;;) { |
5e00df3b | 226 | if (dp == fdp->fd_rdir || dp == rootdir) { |
6d0f0ece | 227 | ndp->ni_dvp = dp; |
7655c64a | 228 | ndp->ni_vp = dp; |
8fe1c702 | 229 | VREF(dp); |
6d0f0ece | 230 | goto nextname; |
e47da406 | 231 | } |
d5cea2aa KM |
232 | if ((dp->v_flag & VROOT) == 0 || |
233 | (ndp->ni_nameiop & NOCROSSMOUNT)) | |
e47da406 | 234 | break; |
6d0f0ece | 235 | tdp = dp; |
54fb9dc2 | 236 | dp = dp->v_mount->mnt_vnodecovered; |
6d0f0ece | 237 | vput(tdp); |
8fe1c702 | 238 | VREF(dp); |
7655c64a | 239 | VOP_LOCK(dp); |
e47da406 KM |
240 | } |
241 | } | |
242 | ||
f93197fc KM |
243 | /* |
244 | * We now have a segment name to search for, and a directory to search. | |
6459ebe0 | 245 | */ |
659ba9ca | 246 | if (error = VOP_LOOKUP(dp, ndp, p)) { |
6d0f0ece KM |
247 | if (ndp->ni_vp != NULL) |
248 | panic("leaf should be empty"); | |
658f5fdc | 249 | #ifdef NAMEI_DIAGNOSTIC |
6d0f0ece | 250 | printf("not found\n"); |
658f5fdc | 251 | #endif |
1f6ef9f5 KM |
252 | if (flag == LOOKUP || flag == DELETE || |
253 | error != ENOENT || *cp != 0) | |
254 | goto bad; | |
f5039631 | 255 | /* |
6d0f0ece KM |
256 | * If creating and at end of pathname, then can consider |
257 | * allowing file to be created. | |
f5039631 | 258 | */ |
54fb9dc2 | 259 | if (ndp->ni_dvp->v_mount->mnt_flag & rdonly) { |
6d0f0ece | 260 | error = EROFS; |
6bd0bb92 | 261 | goto bad; |
1f6ef9f5 | 262 | } |
f5039631 | 263 | /* |
6d0f0ece KM |
264 | * We return with ni_vp NULL to indicate that the entry |
265 | * doesn't currently exist, leaving a pointer to the | |
266 | * (possibly locked) directory inode in ndp->ni_dvp. | |
f5039631 | 267 | */ |
d5cea2aa KM |
268 | if (getbuf) |
269 | FREE(ndp->ni_pnbuf, M_NAMEI); | |
6d0f0ece | 270 | return (0); /* should this be ENOENT? */ |
6bd0bb92 | 271 | } |
658f5fdc | 272 | #ifdef NAMEI_DIAGNOSTIC |
6d0f0ece | 273 | printf("found\n"); |
658f5fdc | 274 | #endif |
6bd0bb92 BJ |
275 | |
276 | /* | |
6d0f0ece | 277 | * Check for symbolic link |
4f083fd7 | 278 | */ |
6d0f0ece KM |
279 | dp = ndp->ni_vp; |
280 | if ((dp->v_type == VLNK) && | |
281 | ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) { | |
282 | struct iovec aiov; | |
283 | struct uio auio; | |
284 | int linklen; | |
285 | ||
d5cea2aa KM |
286 | if (!getbuf) |
287 | panic("namei: unexpected symlink"); | |
6d0f0ece KM |
288 | if (++ndp->ni_loopcnt > MAXSYMLINKS) { |
289 | error = ELOOP; | |
290 | goto bad2; | |
4f083fd7 | 291 | } |
0607f104 | 292 | if (ndp->ni_pathlen > 1) |
6d0f0ece KM |
293 | MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); |
294 | else | |
295 | cp = ndp->ni_pnbuf; | |
296 | aiov.iov_base = cp; | |
297 | aiov.iov_len = MAXPATHLEN; | |
298 | auio.uio_iov = &aiov; | |
299 | auio.uio_iovcnt = 1; | |
300 | auio.uio_offset = 0; | |
301 | auio.uio_rw = UIO_READ; | |
302 | auio.uio_segflg = UIO_SYSSPACE; | |
659ba9ca | 303 | auio.uio_procp = (struct proc *)0; |
6d0f0ece | 304 | auio.uio_resid = MAXPATHLEN; |
8429d022 | 305 | if (error = VOP_READLINK(dp, &auio, p->p_ucred)) { |
0607f104 | 306 | if (ndp->ni_pathlen > 1) |
6d0f0ece | 307 | free(cp, M_NAMEI); |
bde63aa5 | 308 | goto bad2; |
6d0f0ece KM |
309 | } |
310 | linklen = MAXPATHLEN - auio.uio_resid; | |
311 | if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { | |
0607f104 | 312 | if (ndp->ni_pathlen > 1) |
6d0f0ece KM |
313 | free(cp, M_NAMEI); |
314 | error = ENAMETOOLONG; | |
bde63aa5 | 315 | goto bad2; |
f93197fc | 316 | } |
0607f104 KM |
317 | if (ndp->ni_pathlen > 1) { |
318 | bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); | |
6d0f0ece KM |
319 | FREE(ndp->ni_pnbuf, M_NAMEI); |
320 | ndp->ni_pnbuf = cp; | |
206be3f9 | 321 | } else |
0607f104 | 322 | ndp->ni_pnbuf[linklen] = '\0'; |
6d0f0ece | 323 | ndp->ni_ptr = cp; |
6d0f0ece KM |
324 | vput(dp); |
325 | dp = ndp->ni_dvp; | |
ff4672c1 | 326 | if (lockparent && ndp->ni_pathlen == 1) |
206be3f9 | 327 | VOP_UNLOCK(dp); |
ff4672c1 | 328 | ndp->ni_pathlen += linklen; |
6d0f0ece | 329 | goto start; |
f93197fc KM |
330 | } |
331 | ||
6bd0bb92 | 332 | /* |
6d0f0ece KM |
333 | * Check to see if the vnode has been mounted on; |
334 | * if so find the root of the mounted file system. | |
6bd0bb92 | 335 | */ |
6d0f0ece | 336 | mntloop: |
d5cea2aa KM |
337 | while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && |
338 | (ndp->ni_nameiop & NOCROSSMOUNT) == 0) { | |
54fb9dc2 KM |
339 | while(mp->mnt_flag & MNT_MLOCK) { |
340 | mp->mnt_flag |= MNT_MWAIT; | |
6d0f0ece KM |
341 | sleep((caddr_t)mp, PVFS); |
342 | goto mntloop; | |
6bd0bb92 | 343 | } |
6d0f0ece KM |
344 | error = VFS_ROOT(dp->v_mountedhere, &tdp); |
345 | if (error) | |
6bd0bb92 | 346 | goto bad2; |
6d0f0ece KM |
347 | vput(dp); |
348 | ndp->ni_vp = dp = tdp; | |
10873320 | 349 | } |
6bd0bb92 | 350 | |
6d0f0ece | 351 | nextname: |
10873320 | 352 | /* |
6bd0bb92 BJ |
353 | * Not a symbolic link. If more pathname, |
354 | * continue at next component, else return. | |
10873320 | 355 | */ |
6d0f0ece KM |
356 | ndp->ni_ptr = ndp->ni_next; |
357 | if (*ndp->ni_ptr == '/') { | |
358 | while (*ndp->ni_ptr == '/') { | |
359 | ndp->ni_ptr++; | |
360 | ndp->ni_pathlen--; | |
6bd0bb92 | 361 | } |
6d0f0ece KM |
362 | vrele(ndp->ni_dvp); |
363 | goto dirloop; | |
6bd0bb92 BJ |
364 | } |
365 | /* | |
01633fea | 366 | * Check for read-only file systems. |
6bd0bb92 | 367 | */ |
01633fea | 368 | if (flag == DELETE || flag == RENAME) { |
dfc0e8dd | 369 | /* |
01633fea KM |
370 | * Disallow directory write attempts on read-only |
371 | * file systems. | |
dfc0e8dd | 372 | */ |
54fb9dc2 KM |
373 | if ((dp->v_mount->mnt_flag & rdonly) || |
374 | (wantparent && (ndp->ni_dvp->v_mount->mnt_flag & rdonly))) { | |
dfc0e8dd KM |
375 | error = EROFS; |
376 | goto bad2; | |
377 | } | |
378 | } | |
6d0f0ece KM |
379 | if (!wantparent) |
380 | vrele(ndp->ni_dvp); | |
381 | if ((ndp->ni_nameiop & LOCKLEAF) == 0) | |
382 | VOP_UNLOCK(dp); | |
d5cea2aa KM |
383 | if (getbuf) |
384 | FREE(ndp->ni_pnbuf, M_NAMEI); | |
6d0f0ece | 385 | return (0); |
4f083fd7 | 386 | |
6d0f0ece | 387 | bad2: |
206be3f9 KM |
388 | if (lockparent && *ndp->ni_next == '\0') |
389 | VOP_UNLOCK(ndp->ni_dvp); | |
6d0f0ece KM |
390 | vrele(ndp->ni_dvp); |
391 | bad: | |
392 | vput(dp); | |
393 | ndp->ni_vp = NULL; | |
d5cea2aa KM |
394 | if (getbuf) |
395 | FREE(ndp->ni_pnbuf, M_NAMEI); | |
b1aa93b9 KM |
396 | return (error); |
397 | } |