vfs_lookup.c revision 1.11 1 /*
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)vfs_lookup.c 7.32 (Berkeley) 5/21/91
39 * $Id: vfs_lookup.c,v 1.11 1994/05/18 05:12:44 cgd Exp $
40 */
41
42 #include <sys/param.h>
43 #include <sys/syslimits.h>
44 #include <sys/time.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/errno.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/proc.h>
52 #ifdef KTRACE
53 #include <sys/ktrace.h>
54 #endif
55
56 /*
57 * Convert a pathname into a pointer to a locked inode.
58 *
59 * The FOLLOW flag is set when symbolic links are to be followed
60 * when they occur at the end of the name translation process.
61 * Symbolic links are always followed for all other pathname
62 * components other than the last.
63 *
64 * The segflg defines whether the name is to be copied from user
65 * space or kernel space.
66 *
67 * Overall outline of namei:
68 *
69 * copy in name
70 * get starting directory
71 * while (!done && !error) {
72 * call lookup to search path.
73 * if symbolic link, massage name in buffer and continue
74 * }
75 */
76 namei(ndp, p)
77 register struct nameidata *ndp;
78 struct proc *p;
79 {
80 register struct filedesc *fdp; /* pointer to file descriptor state */
81 register char *cp; /* pointer into pathname argument */
82 register struct vnode *dp; /* the directory we are searching */
83 struct iovec aiov; /* uio for reading symbolic links */
84 struct uio auio;
85 int error, linklen;
86
87 ndp->ni_cred = p->p_ucred;
88 fdp = p->p_fd;
89
90 /*
91 * Get a buffer for the name to be translated, and copy the
92 * name into the buffer.
93 */
94 if ((ndp->ni_nameiop & HASBUF) == 0)
95 MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
96 if (ndp->ni_segflg == UIO_SYSSPACE)
97 error = copystr(ndp->ni_dirp, ndp->ni_pnbuf,
98 MAXPATHLEN, &ndp->ni_pathlen);
99 else
100 error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf,
101 MAXPATHLEN, &ndp->ni_pathlen);
102 if (error) {
103 free(ndp->ni_pnbuf, M_NAMEI);
104 ndp->ni_vp = NULL;
105 return (error);
106 }
107 ndp->ni_loopcnt = 0;
108 #ifdef KTRACE
109 if (KTRPOINT(p, KTR_NAMEI))
110 ktrnamei(p->p_tracep, ndp->ni_pnbuf);
111 #endif
112
113 /*
114 * Get starting point for the translation.
115 */
116 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
117 ndp->ni_rootdir = rootvnode;
118 dp = fdp->fd_cdir;
119 VREF(dp);
120 for (;;) {
121 /*
122 * Check if root directory should replace current directory.
123 * Done at start of translation and after symbolic link.
124 */
125 ndp->ni_ptr = ndp->ni_pnbuf;
126 if (*ndp->ni_ptr == '/') {
127 vrele(dp);
128 while (*ndp->ni_ptr == '/') {
129 ndp->ni_ptr++;
130 ndp->ni_pathlen--;
131 }
132 dp = ndp->ni_rootdir;
133 VREF(dp);
134 }
135 ndp->ni_startdir = dp;
136 if (error = lookup(ndp, p)) {
137 FREE(ndp->ni_pnbuf, M_NAMEI);
138 return (error);
139 }
140 /*
141 * Check for symbolic link
142 */
143 if (ndp->ni_more == 0) {
144 if ((ndp->ni_nameiop & (SAVENAME | SAVESTART)) == 0)
145 FREE(ndp->ni_pnbuf, M_NAMEI);
146 else
147 ndp->ni_nameiop |= HASBUF;
148 return (0);
149 }
150 if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1)
151 VOP_UNLOCK(ndp->ni_dvp);
152 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
153 error = ELOOP;
154 break;
155 }
156 if (ndp->ni_pathlen > 1)
157 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
158 else
159 cp = ndp->ni_pnbuf;
160 aiov.iov_base = cp;
161 aiov.iov_len = MAXPATHLEN;
162 auio.uio_iov = &aiov;
163 auio.uio_iovcnt = 1;
164 auio.uio_offset = 0;
165 auio.uio_rw = UIO_READ;
166 auio.uio_segflg = UIO_SYSSPACE;
167 auio.uio_procp = (struct proc *)0;
168 auio.uio_resid = MAXPATHLEN;
169 if (error = VOP_READLINK(ndp->ni_vp, &auio, p->p_ucred)) {
170 if (ndp->ni_pathlen > 1)
171 free(cp, M_NAMEI);
172 break;
173 }
174 linklen = MAXPATHLEN - auio.uio_resid;
175 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
176 if (ndp->ni_pathlen > 1)
177 free(cp, M_NAMEI);
178 error = ENAMETOOLONG;
179 break;
180 }
181 if (ndp->ni_pathlen > 1) {
182 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
183 FREE(ndp->ni_pnbuf, M_NAMEI);
184 ndp->ni_pnbuf = cp;
185 } else
186 ndp->ni_pnbuf[linklen] = '\0';
187 ndp->ni_pathlen += linklen;
188 vput(ndp->ni_vp);
189 dp = ndp->ni_dvp;
190 }
191 FREE(ndp->ni_pnbuf, M_NAMEI);
192 vrele(ndp->ni_dvp);
193 vput(ndp->ni_vp);
194 ndp->ni_vp = NULL;
195 return (error);
196 }
197
198 /*
199 * Search a pathname.
200 * This is a very central and rather complicated routine.
201 *
202 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
203 * The starting directory is taken from ni_startdir. The pathname is
204 * descended until done, or a symbolic link is encountered. The variable
205 * ni_more is clear if the path is completed; it is set to one if a
206 * symbolic link needing interpretation is encountered.
207 *
208 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
209 * whether the name is to be looked up, created, renamed, or deleted.
210 * When CREATE, RENAME, or DELETE is specified, information usable in
211 * creating, renaming, or deleting a directory entry may be calculated.
212 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
213 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
214 * returned unlocked. Otherwise the parent directory is not returned. If
215 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
216 * the target is returned locked, otherwise it is returned unlocked.
217 * When creating or renaming and LOCKPARENT is specified, the target may not
218 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
219 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked.
220 *
221 * Overall outline of lookup:
222 *
223 * dirloop:
224 * identify next component of name at ndp->ni_ptr
225 * handle degenerate case where name is null string
226 * if .. and crossing mount points and on mounted filesys, find parent
227 * call VOP_LOOKUP routine for next component name
228 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
229 * component vnode returned in ni_vp (if it exists), locked.
230 * if result vnode is mounted on and crossing mount points,
231 * find mounted on vnode
232 * if more components of name, do next level at dirloop
233 * return the answer in ni_vp, locked if LOCKLEAF set
234 * if LOCKPARENT set, return locked parent in ni_dvp
235 * if WANTPARENT set, return unlocked parent in ni_dvp
236 */
237 lookup(ndp, p)
238 register struct nameidata *ndp;
239 struct proc *p;
240 {
241 register char *cp; /* pointer into pathname argument */
242 register struct vnode *dp = 0; /* the directory we are searching */
243 struct vnode *tdp; /* saved dp */
244 struct mount *mp; /* mount table entry */
245 int docache; /* == 0 do not cache last component */
246 int flag; /* LOOKUP, CREATE, RENAME or DELETE */
247 int wantparent; /* 1 => wantparent or lockparent flag */
248 int rdonly; /* mounted read-only flag bit(s) */
249 int error = 0;
250
251 /*
252 * Setup: break out flag bits into variables.
253 */
254 flag = ndp->ni_nameiop & OPMASK;
255 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
256 docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
257 if (flag == DELETE || (wantparent && flag != CREATE))
258 docache = 0;
259 rdonly = MNT_RDONLY;
260 if (ndp->ni_nameiop & REMOTE)
261 rdonly |= MNT_EXRDONLY;
262 ndp->ni_dvp = NULL;
263 ndp->ni_more = 0;
264 dp = ndp->ni_startdir;
265 ndp->ni_startdir = NULLVP;
266 VOP_LOCK(dp);
267
268 dirloop:
269 /*
270 * Search a new directory.
271 *
272 * The ni_hash value is for use by vfs_cache.
273 * The last component of the filename is left accessible via
274 * ndp->ptr for callers that need the name. Callers needing
275 * the name set the SAVENAME flag. When done, they assume
276 * responsibility for freeing the pathname buffer.
277 */
278 ndp->ni_hash = 0;
279 for (cp = ndp->ni_ptr; *cp != 0 && *cp != '/'; cp++)
280 ndp->ni_hash += (unsigned char)*cp;
281 ndp->ni_hash ^= dp->v_id;
282 ndp->ni_namelen = cp - ndp->ni_ptr;
283 if (ndp->ni_namelen >= NAME_MAX) {
284 error = ENAMETOOLONG;
285 goto bad;
286 }
287 #ifdef NAMEI_DIAGNOSTIC
288 { char c = *cp;
289 *cp = '\0';
290 printf("{%s}: ", ndp->ni_ptr);
291 *cp = c; }
292 #endif
293 ndp->ni_pathlen -= ndp->ni_namelen;
294 ndp->ni_next = cp;
295 ndp->ni_makeentry = 1;
296 if (*cp == '\0' && docache == 0)
297 ndp->ni_makeentry = 0;
298 ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
299 ndp->ni_ptr[1] == '.' && ndp->ni_ptr[0] == '.');
300
301 /*
302 * Check for degenerate name (e.g. / or "")
303 * which is a way of talking about a directory,
304 * e.g. like "/." or ".".
305 */
306 if (ndp->ni_ptr[0] == '\0') {
307 if (flag != LOOKUP || wantparent) {
308 error = EISDIR;
309 goto bad;
310 }
311 if (dp->v_type != VDIR) {
312 error = ENOTDIR;
313 goto bad;
314 }
315 if (!(ndp->ni_nameiop & LOCKLEAF))
316 VOP_UNLOCK(dp);
317 ndp->ni_vp = dp;
318 if (ndp->ni_nameiop & SAVESTART)
319 panic("lookup: SAVESTART");
320 return (0);
321 }
322
323 /*
324 * Handle "..": two special cases.
325 * 1. If at root directory (e.g. after chroot)
326 * then ignore it so can't get out.
327 * 2. If this vnode is the root of a mounted
328 * filesystem, then replace it with the
329 * vnode which was mounted on so we take the
330 * .. in the other file system.
331 */
332 if (ndp->ni_isdotdot) {
333 for (;;) {
334 if ((dp == ndp->ni_rootdir) || (dp == rootvnode)) {
335 ndp->ni_dvp = dp;
336 ndp->ni_vp = dp;
337 VREF(dp);
338 goto nextname;
339 }
340 if ((dp->v_flag & VROOT) == 0 ||
341 (ndp->ni_nameiop & NOCROSSMOUNT))
342 break;
343 tdp = dp;
344 dp = dp->v_mount->mnt_vnodecovered;
345 vput(tdp);
346 VREF(dp);
347 VOP_LOCK(dp);
348 }
349 }
350
351 /*
352 * We now have a segment name to search for, and a directory to search.
353 * The filesystem will return EJUSTRETURN on the lookup if the file
354 * doesn't exist, but can be created.
355 */
356 relookup:
357 if (error = VOP_LOOKUP(dp, ndp, p)) {
358 #ifdef DIAGNOSTIC
359 if (ndp->ni_vp != NULL)
360 panic("leaf should be empty");
361 #endif
362 #ifdef NAMEI_DIAGNOSTIC
363 printf("not found\n");
364 #endif
365 if (error == ENOENT &&
366 (dp->v_flag & VROOT) &&
367 (dp->v_mount->mnt_flag & MNT_UNION)) {
368 tdp = dp;
369 dp = dp->v_mount->mnt_vnodecovered;
370 vput(tdp);
371 VREF(dp);
372 VOP_LOCK(dp);
373 goto relookup;
374 }
375 if (error != EJUSTRETURN)
376 goto bad;
377
378 /*
379 * If creating and at end of pathname, then can consider
380 * allowing file to be created.
381 */
382 if (ndp->ni_dvp->v_mount->mnt_flag & rdonly) {
383 error = EROFS;
384 goto bad;
385 }
386 /*
387 * We return with ni_vp NULL to indicate that the entry
388 * doesn't currently exist, leaving a pointer to the
389 * (possibly locked) directory inode in ndp->ni_dvp.
390 */
391 if (ndp->ni_nameiop & SAVESTART) {
392 ndp->ni_startdir = ndp->ni_dvp;
393 VREF(ndp->ni_startdir);
394 }
395 return (0);
396 }
397 #ifdef NAMEI_DIAGNOSTIC
398 printf("found\n");
399 #endif
400
401 dp = ndp->ni_vp;
402 /*
403 * Check for symbolic link
404 */
405 if ((dp->v_type == VLNK) &&
406 ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
407 ndp->ni_more = 1;
408 return (0);
409 }
410
411 /*
412 * Check to see if the vnode has been mounted on;
413 * if so find the root of the mounted file system.
414 */
415 mntloop:
416 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
417 (ndp->ni_nameiop & NOCROSSMOUNT) == 0) {
418 while(mp->mnt_flag & MNT_MLOCK) {
419 mp->mnt_flag |= MNT_MWAIT;
420 tsleep((caddr_t)mp, PVFS, "lookup", 0);
421 goto mntloop;
422 }
423 if (error = VFS_ROOT(dp->v_mountedhere, &tdp))
424 goto bad2;
425 vput(dp);
426 ndp->ni_vp = dp = tdp;
427 }
428
429 nextname:
430 /*
431 * Not a symbolic link. If more pathname,
432 * continue at next component, else return.
433 */
434 if (*ndp->ni_next == '/') {
435 ndp->ni_ptr = ndp->ni_next;
436 while (*ndp->ni_ptr == '/') {
437 ndp->ni_ptr++;
438 ndp->ni_pathlen--;
439 }
440 vrele(ndp->ni_dvp);
441 goto dirloop;
442 }
443 /*
444 * Check for read-only file systems.
445 */
446 if (flag == DELETE || flag == RENAME) {
447 /*
448 * Disallow directory write attempts on read-only
449 * file systems.
450 */
451 if ((dp->v_mount->mnt_flag & rdonly) ||
452 (wantparent && (ndp->ni_dvp->v_mount->mnt_flag & rdonly))) {
453 error = EROFS;
454 goto bad2;
455 }
456 }
457 if (ndp->ni_nameiop & SAVESTART) {
458 ndp->ni_startdir = ndp->ni_dvp;
459 VREF(ndp->ni_startdir);
460 }
461 if (!wantparent)
462 vrele(ndp->ni_dvp);
463 if ((ndp->ni_nameiop & LOCKLEAF) == 0)
464 VOP_UNLOCK(dp);
465 return (0);
466
467 bad2:
468 if ((ndp->ni_nameiop & LOCKPARENT) && *ndp->ni_next == '\0')
469 VOP_UNLOCK(ndp->ni_dvp);
470 vrele(ndp->ni_dvp);
471 bad:
472 vput(dp);
473 ndp->ni_vp = NULL;
474 return (error);
475 }
476