FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_lookup.c
1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/6.4/sys/kern/vfs_lookup.c 166902 2007-02-23 12:23:41Z kib $");
39
40 #include "opt_ktrace.h"
41 #include "opt_mac.h"
42 #include "opt_vfs.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/mac.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
54 #include <sys/proc.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysctl.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <security/audit/audit.h>
62
63 #include <vm/uma.h>
64
65 #define NAMEI_DIAGNOSTIC 1
66 #undef NAMEI_DIAGNOSTIC
67
68 /*
69 * Allocation zone for namei
70 */
71 uma_zone_t namei_zone;
72 /*
73 * Placeholder vnode for mp traversal
74 */
75 static struct vnode *vp_crossmp;
76
77 static void
78 nameiinit(void *dummy __unused)
79 {
80 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
81 UMA_ALIGN_PTR, 0);
82 getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp);
83 vp_crossmp->v_vnlock->lk_flags &= ~LK_NOSHARE;
84 }
85 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL)
86
87 #ifdef LOOKUP_SHARED
88 static int lookup_shared = 1;
89 #else
90 static int lookup_shared = 0;
91 #endif
92 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0,
93 "Enables/Disables shared locks for path name translation");
94
95 /*
96 * Convert a pathname into a pointer to a locked vnode.
97 *
98 * The FOLLOW flag is set when symbolic links are to be followed
99 * when they occur at the end of the name translation process.
100 * Symbolic links are always followed for all other pathname
101 * components other than the last.
102 *
103 * The segflg defines whether the name is to be copied from user
104 * space or kernel space.
105 *
106 * Overall outline of namei:
107 *
108 * copy in name
109 * get starting directory
110 * while (!done && !error) {
111 * call lookup to search path.
112 * if symbolic link, massage name in buffer and continue
113 * }
114 */
115 int
116 namei(ndp)
117 register struct nameidata *ndp;
118 {
119 register struct filedesc *fdp; /* pointer to file descriptor state */
120 register char *cp; /* pointer into pathname argument */
121 register struct vnode *dp; /* the directory we are searching */
122 struct iovec aiov; /* uio for reading symbolic links */
123 struct uio auio;
124 int error, linklen;
125 struct componentname *cnp = &ndp->ni_cnd;
126 struct thread *td = cnp->cn_thread;
127 struct proc *p = td->td_proc;
128 int vfslocked;
129
130 KASSERT((cnp->cn_flags & MPSAFE) != 0 || mtx_owned(&Giant) != 0,
131 ("NOT MPSAFE and Giant not held"));
132 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
133 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
134 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
135 ("namei: nameiop contaminated with flags"));
136 KASSERT((cnp->cn_flags & OPMASK) == 0,
137 ("namei: flags contaminated with nameiops"));
138 if (!lookup_shared)
139 cnp->cn_flags &= ~LOCKSHARED;
140 fdp = p->p_fd;
141
142 /*
143 * Get a buffer for the name to be translated, and copy the
144 * name into the buffer.
145 */
146 if ((cnp->cn_flags & HASBUF) == 0)
147 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
148 if (ndp->ni_segflg == UIO_SYSSPACE)
149 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
150 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
151 else
152 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
153 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
154
155 /* If we are auditing the kernel pathname, save the user pathname. */
156 if (cnp->cn_flags & AUDITVNODE1)
157 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH1);
158 if (cnp->cn_flags & AUDITVNODE2)
159 AUDIT_ARG(upath, td, cnp->cn_pnbuf, ARG_UPATH2);
160
161 /*
162 * Don't allow empty pathnames.
163 */
164 if (!error && *cnp->cn_pnbuf == '\0')
165 error = ENOENT;
166
167 if (error) {
168 uma_zfree(namei_zone, cnp->cn_pnbuf);
169 #ifdef DIAGNOSTIC
170 cnp->cn_pnbuf = NULL;
171 cnp->cn_nameptr = NULL;
172 #endif
173 ndp->ni_vp = NULL;
174 return (error);
175 }
176 ndp->ni_loopcnt = 0;
177 #ifdef KTRACE
178 if (KTRPOINT(td, KTR_NAMEI)) {
179 KASSERT(cnp->cn_thread == curthread,
180 ("namei not using curthread"));
181 ktrnamei(cnp->cn_pnbuf);
182 }
183 #endif
184
185 /*
186 * Get starting point for the translation.
187 */
188 FILEDESC_LOCK(fdp);
189 ndp->ni_rootdir = fdp->fd_rdir;
190 ndp->ni_topdir = fdp->fd_jdir;
191
192 dp = fdp->fd_cdir;
193 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
194 VREF(dp);
195 FILEDESC_UNLOCK(fdp);
196 for (;;) {
197 /*
198 * Check if root directory should replace current directory.
199 * Done at start of translation and after symbolic link.
200 */
201 cnp->cn_nameptr = cnp->cn_pnbuf;
202 if (*(cnp->cn_nameptr) == '/') {
203 vrele(dp);
204 VFS_UNLOCK_GIANT(vfslocked);
205 while (*(cnp->cn_nameptr) == '/') {
206 cnp->cn_nameptr++;
207 ndp->ni_pathlen--;
208 }
209 dp = ndp->ni_rootdir;
210 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
211 VREF(dp);
212 }
213 if (vfslocked)
214 ndp->ni_cnd.cn_flags |= GIANTHELD;
215 ndp->ni_startdir = dp;
216 error = lookup(ndp);
217 if (error) {
218 uma_zfree(namei_zone, cnp->cn_pnbuf);
219 #ifdef DIAGNOSTIC
220 cnp->cn_pnbuf = NULL;
221 cnp->cn_nameptr = NULL;
222 #endif
223 return (error);
224 }
225 vfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
226 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
227 /*
228 * Check for symbolic link
229 */
230 if ((cnp->cn_flags & ISSYMLINK) == 0) {
231 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
232 uma_zfree(namei_zone, cnp->cn_pnbuf);
233 #ifdef DIAGNOSTIC
234 cnp->cn_pnbuf = NULL;
235 cnp->cn_nameptr = NULL;
236 #endif
237 } else
238 cnp->cn_flags |= HASBUF;
239
240 if ((cnp->cn_flags & MPSAFE) == 0) {
241 VFS_UNLOCK_GIANT(vfslocked);
242 } else if (vfslocked)
243 ndp->ni_cnd.cn_flags |= GIANTHELD;
244 return (0);
245 }
246 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
247 error = ELOOP;
248 break;
249 }
250 #ifdef MAC
251 if ((cnp->cn_flags & NOMACCHECK) == 0) {
252 error = mac_check_vnode_readlink(td->td_ucred,
253 ndp->ni_vp);
254 if (error)
255 break;
256 }
257 #endif
258 if (ndp->ni_pathlen > 1)
259 cp = uma_zalloc(namei_zone, M_WAITOK);
260 else
261 cp = cnp->cn_pnbuf;
262 aiov.iov_base = cp;
263 aiov.iov_len = MAXPATHLEN;
264 auio.uio_iov = &aiov;
265 auio.uio_iovcnt = 1;
266 auio.uio_offset = 0;
267 auio.uio_rw = UIO_READ;
268 auio.uio_segflg = UIO_SYSSPACE;
269 auio.uio_td = (struct thread *)0;
270 auio.uio_resid = MAXPATHLEN;
271 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
272 if (error) {
273 if (ndp->ni_pathlen > 1)
274 uma_zfree(namei_zone, cp);
275 break;
276 }
277 linklen = MAXPATHLEN - auio.uio_resid;
278 if (linklen == 0) {
279 if (ndp->ni_pathlen > 1)
280 uma_zfree(namei_zone, cp);
281 error = ENOENT;
282 break;
283 }
284 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
285 if (ndp->ni_pathlen > 1)
286 uma_zfree(namei_zone, cp);
287 error = ENAMETOOLONG;
288 break;
289 }
290 if (ndp->ni_pathlen > 1) {
291 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
292 uma_zfree(namei_zone, cnp->cn_pnbuf);
293 cnp->cn_pnbuf = cp;
294 } else
295 cnp->cn_pnbuf[linklen] = '\0';
296 ndp->ni_pathlen += linklen;
297 vput(ndp->ni_vp);
298 dp = ndp->ni_dvp;
299 }
300 uma_zfree(namei_zone, cnp->cn_pnbuf);
301 #ifdef DIAGNOSTIC
302 cnp->cn_pnbuf = NULL;
303 cnp->cn_nameptr = NULL;
304 #endif
305 vput(ndp->ni_vp);
306 ndp->ni_vp = NULL;
307 vrele(ndp->ni_dvp);
308 VFS_UNLOCK_GIANT(vfslocked);
309 return (error);
310 }
311
312 static int
313 compute_cn_lkflags(struct mount *mp, int lkflags)
314 {
315 if (mp == NULL ||
316 ((lkflags & LK_SHARED) && !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
317 lkflags &= ~LK_SHARED;
318 lkflags |= LK_EXCLUSIVE;
319 }
320 return lkflags;
321 }
322
323 /*
324 * Search a pathname.
325 * This is a very central and rather complicated routine.
326 *
327 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
328 * The starting directory is taken from ni_startdir. The pathname is
329 * descended until done, or a symbolic link is encountered. The variable
330 * ni_more is clear if the path is completed; it is set to one if a
331 * symbolic link needing interpretation is encountered.
332 *
333 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
334 * whether the name is to be looked up, created, renamed, or deleted.
335 * When CREATE, RENAME, or DELETE is specified, information usable in
336 * creating, renaming, or deleting a directory entry may be calculated.
337 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
338 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
339 * returned unlocked. Otherwise the parent directory is not returned. If
340 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
341 * the target is returned locked, otherwise it is returned unlocked.
342 * When creating or renaming and LOCKPARENT is specified, the target may not
343 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
344 *
345 * Overall outline of lookup:
346 *
347 * dirloop:
348 * identify next component of name at ndp->ni_ptr
349 * handle degenerate case where name is null string
350 * if .. and crossing mount points and on mounted filesys, find parent
351 * call VOP_LOOKUP routine for next component name
352 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
353 * component vnode returned in ni_vp (if it exists), locked.
354 * if result vnode is mounted on and crossing mount points,
355 * find mounted on vnode
356 * if more components of name, do next level at dirloop
357 * return the answer in ni_vp, locked if LOCKLEAF set
358 * if LOCKPARENT set, return locked parent in ni_dvp
359 * if WANTPARENT set, return unlocked parent in ni_dvp
360 */
361 int
362 lookup(ndp)
363 register struct nameidata *ndp;
364 {
365 register char *cp; /* pointer into pathname argument */
366 register struct vnode *dp = 0; /* the directory we are searching */
367 struct vnode *tdp; /* saved dp */
368 struct mount *mp; /* mount table entry */
369 int docache; /* == 0 do not cache last component */
370 int wantparent; /* 1 => wantparent or lockparent flag */
371 int rdonly; /* lookup read-only flag bit */
372 int trailing_slash;
373 int error = 0;
374 int dpunlocked = 0; /* dp has already been unlocked */
375 struct componentname *cnp = &ndp->ni_cnd;
376 struct thread *td = cnp->cn_thread;
377 int vfslocked; /* VFS Giant state for child */
378 int dvfslocked; /* VFS Giant state for parent */
379 int tvfslocked;
380 int lkflags_save;
381
382 /*
383 * Setup: break out flag bits into variables.
384 */
385 dvfslocked = (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
386 vfslocked = 0;
387 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
388 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
389 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
390 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
391 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
392 if (cnp->cn_nameiop == DELETE ||
393 (wantparent && cnp->cn_nameiop != CREATE &&
394 cnp->cn_nameiop != LOOKUP))
395 docache = 0;
396 rdonly = cnp->cn_flags & RDONLY;
397 cnp->cn_flags &= ~ISSYMLINK;
398 ndp->ni_dvp = NULL;
399 /*
400 * We use shared locks until we hit the parent of the last cn then
401 * we adjust based on the requesting flags.
402 */
403 if (lookup_shared)
404 cnp->cn_lkflags = LK_SHARED;
405 else
406 cnp->cn_lkflags = LK_EXCLUSIVE;
407 dp = ndp->ni_startdir;
408 ndp->ni_startdir = NULLVP;
409 vn_lock(dp, compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY), td);
410
411 dirloop:
412 /*
413 * Search a new directory.
414 *
415 * The last component of the filename is left accessible via
416 * cnp->cn_nameptr for callers that need the name. Callers needing
417 * the name set the SAVENAME flag. When done, they assume
418 * responsibility for freeing the pathname buffer.
419 */
420 cnp->cn_consume = 0;
421 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
422 continue;
423 cnp->cn_namelen = cp - cnp->cn_nameptr;
424 if (cnp->cn_namelen > NAME_MAX) {
425 error = ENAMETOOLONG;
426 goto bad;
427 }
428 #ifdef NAMEI_DIAGNOSTIC
429 { char c = *cp;
430 *cp = '\0';
431 printf("{%s}: ", cnp->cn_nameptr);
432 *cp = c; }
433 #endif
434 ndp->ni_pathlen -= cnp->cn_namelen;
435 ndp->ni_next = cp;
436
437 /*
438 * Replace multiple slashes by a single slash and trailing slashes
439 * by a null. This must be done before VOP_LOOKUP() because some
440 * fs's don't know about trailing slashes. Remember if there were
441 * trailing slashes to handle symlinks, existing non-directories
442 * and non-existing files that won't be directories specially later.
443 */
444 trailing_slash = 0;
445 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
446 cp++;
447 ndp->ni_pathlen--;
448 if (*cp == '\0') {
449 trailing_slash = 1;
450 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
451 }
452 }
453 ndp->ni_next = cp;
454
455 cnp->cn_flags |= MAKEENTRY;
456 if (*cp == '\0' && docache == 0)
457 cnp->cn_flags &= ~MAKEENTRY;
458 if (cnp->cn_namelen == 2 &&
459 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
460 cnp->cn_flags |= ISDOTDOT;
461 else
462 cnp->cn_flags &= ~ISDOTDOT;
463 if (*ndp->ni_next == 0)
464 cnp->cn_flags |= ISLASTCN;
465 else
466 cnp->cn_flags &= ~ISLASTCN;
467
468
469 /*
470 * Check for degenerate name (e.g. / or "")
471 * which is a way of talking about a directory,
472 * e.g. like "/." or ".".
473 */
474 if (cnp->cn_nameptr[0] == '\0') {
475 if (dp->v_type != VDIR) {
476 error = ENOTDIR;
477 goto bad;
478 }
479 if (cnp->cn_nameiop != LOOKUP) {
480 error = EISDIR;
481 goto bad;
482 }
483 if (wantparent) {
484 ndp->ni_dvp = dp;
485 VREF(dp);
486 }
487 ndp->ni_vp = dp;
488
489 if (cnp->cn_flags & AUDITVNODE1)
490 AUDIT_ARG(vnode, dp, ARG_VNODE1);
491 else if (cnp->cn_flags & AUDITVNODE2)
492 AUDIT_ARG(vnode, dp, ARG_VNODE2);
493
494 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
495 VOP_UNLOCK(dp, 0, td);
496 /* XXX This should probably move to the top of function. */
497 if (cnp->cn_flags & SAVESTART)
498 panic("lookup: SAVESTART");
499 goto success;
500 }
501
502 /*
503 * Handle "..": four special cases.
504 * 1. Return an error if this is the last component of
505 * the name and the operation is DELETE or RENAME.
506 * 2. If at root directory (e.g. after chroot)
507 * or at absolute root directory
508 * then ignore it so can't get out.
509 * 3. If this vnode is the root of a mounted
510 * filesystem, then replace it with the
511 * vnode which was mounted on so we take the
512 * .. in the other filesystem.
513 * 4. If the vnode is the top directory of
514 * the jail or chroot, don't let them out.
515 */
516 if (cnp->cn_flags & ISDOTDOT) {
517 if ((cnp->cn_flags & ISLASTCN) != 0 &&
518 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
519 error = EINVAL;
520 goto bad;
521 }
522 for (;;) {
523 if (dp == ndp->ni_rootdir ||
524 dp == ndp->ni_topdir ||
525 dp == rootvnode ||
526 ((dp->v_vflag & VV_ROOT) != 0 &&
527 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
528 ndp->ni_dvp = dp;
529 ndp->ni_vp = dp;
530 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
531 VREF(dp);
532 goto nextname;
533 }
534 if ((dp->v_vflag & VV_ROOT) == 0)
535 break;
536 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
537 error = EBADF;
538 goto bad;
539 }
540 tdp = dp;
541 dp = dp->v_mount->mnt_vnodecovered;
542 tvfslocked = dvfslocked;
543 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
544 VREF(dp);
545 vput(tdp);
546 VFS_UNLOCK_GIANT(tvfslocked);
547 vn_lock(dp, compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY), td);
548 }
549 }
550
551 /*
552 * We now have a segment name to search for, and a directory to search.
553 */
554 unionlookup:
555 #ifdef MAC
556 if ((cnp->cn_flags & NOMACCHECK) == 0) {
557 error = mac_check_vnode_lookup(td->td_ucred, dp, cnp);
558 if (error)
559 goto bad;
560 }
561 #endif
562 ndp->ni_dvp = dp;
563 ndp->ni_vp = NULL;
564 ASSERT_VOP_LOCKED(dp, "lookup");
565 VNASSERT(vfslocked == 0, dp, ("lookup: vfslocked %d", vfslocked));
566 /*
567 * If we have a shared lock we may need to upgrade the lock for the
568 * last operation.
569 */
570 if (dp != vp_crossmp &&
571 VOP_ISLOCKED(dp, td) == LK_SHARED &&
572 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT))
573 vn_lock(dp, LK_UPGRADE|LK_RETRY, td);
574 /*
575 * If we're looking up the last component and we need an exclusive
576 * lock, adjust our lkflags.
577 */
578 if ((cnp->cn_flags & (ISLASTCN|LOCKSHARED|LOCKLEAF)) ==
579 (ISLASTCN|LOCKLEAF))
580 cnp->cn_lkflags = LK_EXCLUSIVE;
581 #ifdef NAMEI_DIAGNOSTIC
582 vprint("lookup in", dp);
583 #endif
584 lkflags_save = cnp->cn_lkflags;
585 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags);
586 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
587 cnp->cn_lkflags = lkflags_save;
588 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
589 #ifdef NAMEI_DIAGNOSTIC
590 printf("not found\n");
591 #endif
592 if ((error == ENOENT) &&
593 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
594 (dp->v_mount->mnt_flag & MNT_UNION)) {
595 tdp = dp;
596 dp = dp->v_mount->mnt_vnodecovered;
597 tvfslocked = dvfslocked;
598 dvfslocked = VFS_LOCK_GIANT(dp->v_mount);
599 VREF(dp);
600 vput(tdp);
601 VFS_UNLOCK_GIANT(tvfslocked);
602 vn_lock(dp, compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY), td);
603 goto unionlookup;
604 }
605
606 if (error != EJUSTRETURN)
607 goto bad;
608 /*
609 * If creating and at end of pathname, then can consider
610 * allowing file to be created.
611 */
612 if (rdonly) {
613 error = EROFS;
614 goto bad;
615 }
616 if (*cp == '\0' && trailing_slash &&
617 !(cnp->cn_flags & WILLBEDIR)) {
618 error = ENOENT;
619 goto bad;
620 }
621 if ((cnp->cn_flags & LOCKPARENT) == 0)
622 VOP_UNLOCK(dp, 0, td);
623 /*
624 * This is a temporary assert to make sure I know what the
625 * behavior here was.
626 */
627 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
628 ("lookup: Unhandled case."));
629 /*
630 * We return with ni_vp NULL to indicate that the entry
631 * doesn't currently exist, leaving a pointer to the
632 * (possibly locked) directory vnode in ndp->ni_dvp.
633 */
634 if (cnp->cn_flags & SAVESTART) {
635 ndp->ni_startdir = ndp->ni_dvp;
636 VREF(ndp->ni_startdir);
637 }
638 goto success;
639 } else
640 cnp->cn_lkflags = lkflags_save;
641 #ifdef NAMEI_DIAGNOSTIC
642 printf("found\n");
643 #endif
644 /*
645 * Take into account any additional components consumed by
646 * the underlying filesystem.
647 */
648 if (cnp->cn_consume > 0) {
649 cnp->cn_nameptr += cnp->cn_consume;
650 ndp->ni_next += cnp->cn_consume;
651 ndp->ni_pathlen -= cnp->cn_consume;
652 cnp->cn_consume = 0;
653 }
654
655 dp = ndp->ni_vp;
656 vfslocked = VFS_LOCK_GIANT(dp->v_mount);
657
658 /*
659 * Check to see if the vnode has been mounted on;
660 * if so find the root of the mounted filesystem.
661 */
662 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
663 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
664 if (vfs_busy(mp, 0, 0, td))
665 continue;
666 vput(dp);
667 VFS_UNLOCK_GIANT(vfslocked);
668 vfslocked = VFS_LOCK_GIANT(mp);
669 if (dp != ndp->ni_dvp)
670 vput(ndp->ni_dvp);
671 else
672 vrele(ndp->ni_dvp);
673 VFS_UNLOCK_GIANT(dvfslocked);
674 dvfslocked = 0;
675 vref(vp_crossmp);
676 ndp->ni_dvp = vp_crossmp;
677 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags), &tdp, td);
678 vfs_unbusy(mp, td);
679 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT, td))
680 panic("vp_crossmp exclusively locked or reclaimed");
681 if (error) {
682 dpunlocked = 1;
683 goto bad2;
684 }
685 ndp->ni_vp = dp = tdp;
686 }
687
688 /*
689 * Check for symbolic link
690 */
691 if ((dp->v_type == VLNK) &&
692 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
693 *ndp->ni_next == '/')) {
694 cnp->cn_flags |= ISSYMLINK;
695 if (dp->v_iflag & VI_DOOMED) {
696 /* We can't know whether the directory was mounted with
697 * NOSYMFOLLOW, so we can't follow safely. */
698 error = EBADF;
699 goto bad2;
700 }
701 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
702 error = EACCES;
703 goto bad2;
704 }
705 /*
706 * Symlink code always expects an unlocked dvp.
707 */
708 if (ndp->ni_dvp != ndp->ni_vp)
709 VOP_UNLOCK(ndp->ni_dvp, 0, td);
710 goto success;
711 }
712
713 /*
714 * Check for bogus trailing slashes.
715 */
716 if (trailing_slash && dp->v_type != VDIR) {
717 error = ENOTDIR;
718 goto bad2;
719 }
720
721 nextname:
722 /*
723 * Not a symbolic link. If more pathname,
724 * continue at next component, else return.
725 */
726 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
727 ("lookup: invalid path state."));
728 if (*ndp->ni_next == '/') {
729 cnp->cn_nameptr = ndp->ni_next;
730 while (*cnp->cn_nameptr == '/') {
731 cnp->cn_nameptr++;
732 ndp->ni_pathlen--;
733 }
734 if (ndp->ni_dvp != dp)
735 vput(ndp->ni_dvp);
736 else
737 vrele(ndp->ni_dvp);
738 VFS_UNLOCK_GIANT(dvfslocked);
739 dvfslocked = vfslocked; /* dp becomes dvp in dirloop */
740 vfslocked = 0;
741 goto dirloop;
742 }
743 /*
744 * Disallow directory write attempts on read-only filesystems.
745 */
746 if (rdonly &&
747 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
748 error = EROFS;
749 goto bad2;
750 }
751 if (cnp->cn_flags & SAVESTART) {
752 ndp->ni_startdir = ndp->ni_dvp;
753 VREF(ndp->ni_startdir);
754 }
755 if (!wantparent) {
756 if (ndp->ni_dvp != dp)
757 vput(ndp->ni_dvp);
758 else
759 vrele(ndp->ni_dvp);
760 VFS_UNLOCK_GIANT(dvfslocked);
761 dvfslocked = 0;
762 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp)
763 VOP_UNLOCK(ndp->ni_dvp, 0, td);
764
765 if (cnp->cn_flags & AUDITVNODE1)
766 AUDIT_ARG(vnode, dp, ARG_VNODE1);
767 else if (cnp->cn_flags & AUDITVNODE2)
768 AUDIT_ARG(vnode, dp, ARG_VNODE2);
769
770 if ((cnp->cn_flags & LOCKLEAF) == 0)
771 VOP_UNLOCK(dp, 0, td);
772 success:
773 if (vfslocked && dvfslocked)
774 VFS_UNLOCK_GIANT(dvfslocked); /* Only need one */
775 if (vfslocked || dvfslocked)
776 ndp->ni_cnd.cn_flags |= GIANTHELD;
777 return (0);
778
779 bad2:
780 if (dp != ndp->ni_dvp)
781 vput(ndp->ni_dvp);
782 else
783 vrele(ndp->ni_dvp);
784 bad:
785 if (!dpunlocked)
786 vput(dp);
787 VFS_UNLOCK_GIANT(vfslocked);
788 VFS_UNLOCK_GIANT(dvfslocked);
789 ndp->ni_cnd.cn_flags &= ~GIANTHELD;
790 ndp->ni_vp = NULL;
791 return (error);
792 }
793
794 /*
795 * relookup - lookup a path name component
796 * Used by lookup to re-aquire things.
797 */
798 int
799 relookup(dvp, vpp, cnp)
800 struct vnode *dvp, **vpp;
801 struct componentname *cnp;
802 {
803 struct thread *td = cnp->cn_thread;
804 struct vnode *dp = 0; /* the directory we are searching */
805 int wantparent; /* 1 => wantparent or lockparent flag */
806 int rdonly; /* lookup read-only flag bit */
807 int error = 0;
808
809 KASSERT(cnp->cn_flags & ISLASTCN,
810 ("relookup: Not given last component."));
811 /*
812 * Setup: break out flag bits into variables.
813 */
814 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
815 KASSERT(wantparent, ("relookup: parent not wanted."));
816 rdonly = cnp->cn_flags & RDONLY;
817 cnp->cn_flags &= ~ISSYMLINK;
818 dp = dvp;
819 cnp->cn_lkflags = LK_EXCLUSIVE;
820 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
821
822 /*
823 * Search a new directory.
824 *
825 * The last component of the filename is left accessible via
826 * cnp->cn_nameptr for callers that need the name. Callers needing
827 * the name set the SAVENAME flag. When done, they assume
828 * responsibility for freeing the pathname buffer.
829 */
830 #ifdef NAMEI_DIAGNOSTIC
831 printf("{%s}: ", cnp->cn_nameptr);
832 #endif
833
834 /*
835 * Check for degenerate name (e.g. / or "")
836 * which is a way of talking about a directory,
837 * e.g. like "/." or ".".
838 */
839 if (cnp->cn_nameptr[0] == '\0') {
840 if (cnp->cn_nameiop != LOOKUP || wantparent) {
841 error = EISDIR;
842 goto bad;
843 }
844 if (dp->v_type != VDIR) {
845 error = ENOTDIR;
846 goto bad;
847 }
848 if (!(cnp->cn_flags & LOCKLEAF))
849 VOP_UNLOCK(dp, 0, td);
850 *vpp = dp;
851 /* XXX This should probably move to the top of function. */
852 if (cnp->cn_flags & SAVESTART)
853 panic("lookup: SAVESTART");
854 return (0);
855 }
856
857 if (cnp->cn_flags & ISDOTDOT)
858 panic ("relookup: lookup on dot-dot");
859
860 /*
861 * We now have a segment name to search for, and a directory to search.
862 */
863 #ifdef NAMEI_DIAGNOSTIC
864 vprint("search in:", dp);
865 #endif
866 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
867 KASSERT(*vpp == NULL, ("leaf should be empty"));
868 if (error != EJUSTRETURN)
869 goto bad;
870 /*
871 * If creating and at end of pathname, then can consider
872 * allowing file to be created.
873 */
874 if (rdonly) {
875 error = EROFS;
876 goto bad;
877 }
878 /* ASSERT(dvp == ndp->ni_startdir) */
879 if (cnp->cn_flags & SAVESTART)
880 VREF(dvp);
881 if ((cnp->cn_flags & LOCKPARENT) == 0)
882 VOP_UNLOCK(dp, 0, td);
883 /*
884 * This is a temporary assert to make sure I know what the
885 * behavior here was.
886 */
887 KASSERT((cnp->cn_flags & (WANTPARENT|LOCKPARENT)) != 0,
888 ("relookup: Unhandled case."));
889 /*
890 * We return with ni_vp NULL to indicate that the entry
891 * doesn't currently exist, leaving a pointer to the
892 * (possibly locked) directory vnode in ndp->ni_dvp.
893 */
894 return (0);
895 }
896
897 dp = *vpp;
898
899 /*
900 * Disallow directory write attempts on read-only filesystems.
901 */
902 if (rdonly &&
903 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
904 if (dvp == dp)
905 vrele(dvp);
906 else
907 vput(dvp);
908 error = EROFS;
909 goto bad;
910 }
911 /*
912 * Set the parent lock/ref state to the requested state.
913 */
914 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
915 if (wantparent)
916 VOP_UNLOCK(dvp, 0, td);
917 else
918 vput(dvp);
919 } else if (!wantparent)
920 vrele(dvp);
921 /*
922 * Check for symbolic link
923 */
924 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
925 ("relookup: symlink found.\n"));
926
927 /* ASSERT(dvp == ndp->ni_startdir) */
928 if (cnp->cn_flags & SAVESTART)
929 VREF(dvp);
930
931 if ((cnp->cn_flags & LOCKLEAF) == 0)
932 VOP_UNLOCK(dp, 0, td);
933 return (0);
934 bad:
935 vput(dp);
936 *vpp = NULL;
937 return (error);
938 }
939
940 /*
941 * Free data allocated by namei(); see namei(9) for details.
942 */
943 void
944 NDFREE(ndp, flags)
945 struct nameidata *ndp;
946 const u_int flags;
947 {
948 int unlock_dvp;
949 int unlock_vp;
950
951 unlock_dvp = 0;
952 unlock_vp = 0;
953
954 if (!(flags & NDF_NO_FREE_PNBUF) &&
955 (ndp->ni_cnd.cn_flags & HASBUF)) {
956 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
957 ndp->ni_cnd.cn_flags &= ~HASBUF;
958 }
959 if (!(flags & NDF_NO_VP_UNLOCK) &&
960 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
961 unlock_vp = 1;
962 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
963 if (unlock_vp) {
964 vput(ndp->ni_vp);
965 unlock_vp = 0;
966 } else
967 vrele(ndp->ni_vp);
968 ndp->ni_vp = NULL;
969 }
970 if (unlock_vp)
971 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_thread);
972 if (!(flags & NDF_NO_DVP_UNLOCK) &&
973 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
974 ndp->ni_dvp != ndp->ni_vp)
975 unlock_dvp = 1;
976 if (!(flags & NDF_NO_DVP_RELE) &&
977 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
978 if (unlock_dvp) {
979 vput(ndp->ni_dvp);
980 unlock_dvp = 0;
981 } else
982 vrele(ndp->ni_dvp);
983 ndp->ni_dvp = NULL;
984 }
985 if (unlock_dvp)
986 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_thread);
987 if (!(flags & NDF_NO_STARTDIR_RELE) &&
988 (ndp->ni_cnd.cn_flags & SAVESTART)) {
989 vrele(ndp->ni_startdir);
990 ndp->ni_startdir = NULL;
991 }
992 }
993
994 /*
995 * Determine if there is a suitable alternate filename under the specified
996 * prefix for the specified path. If the create flag is set, then the
997 * alternate prefix will be used so long as the parent directory exists.
998 * This is used by the various compatiblity ABIs so that Linux binaries prefer
999 * files under /compat/linux for example. The chosen path (whether under
1000 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1001 * to by pathbuf. The caller is responsible for free'ing the buffer from
1002 * the M_TEMP bucket if one is returned.
1003 */
1004 int
1005 kern_alternate_path(struct thread *td, const char *prefix, char *path,
1006 enum uio_seg pathseg, char **pathbuf, int create)
1007 {
1008 struct nameidata nd, ndroot;
1009 char *ptr, *buf, *cp;
1010 size_t len, sz;
1011 int error;
1012
1013 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1014 *pathbuf = buf;
1015
1016 /* Copy the prefix into the new pathname as a starting point. */
1017 len = strlcpy(buf, prefix, MAXPATHLEN);
1018 if (len >= MAXPATHLEN) {
1019 *pathbuf = NULL;
1020 free(buf, M_TEMP);
1021 return (EINVAL);
1022 }
1023 sz = MAXPATHLEN - len;
1024 ptr = buf + len;
1025
1026 /* Append the filename to the prefix. */
1027 if (pathseg == UIO_SYSSPACE)
1028 error = copystr(path, ptr, sz, &len);
1029 else
1030 error = copyinstr(path, ptr, sz, &len);
1031
1032 if (error) {
1033 *pathbuf = NULL;
1034 free(buf, M_TEMP);
1035 return (error);
1036 }
1037
1038 /* Only use a prefix with absolute pathnames. */
1039 if (*ptr != '/') {
1040 error = EINVAL;
1041 goto keeporig;
1042 }
1043
1044 /*
1045 * We know that there is a / somewhere in this pathname.
1046 * Search backwards for it, to find the file's parent dir
1047 * to see if it exists in the alternate tree. If it does,
1048 * and we want to create a file (cflag is set). We don't
1049 * need to worry about the root comparison in this case.
1050 */
1051
1052 if (create) {
1053 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1054 *cp = '\0';
1055
1056 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1057 error = namei(&nd);
1058 *cp = '/';
1059 if (error != 0)
1060 goto keeporig;
1061 } else {
1062 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, buf, td);
1063
1064 error = namei(&nd);
1065 if (error != 0)
1066 goto keeporig;
1067
1068 /*
1069 * We now compare the vnode of the prefix to the one
1070 * vnode asked. If they resolve to be the same, then we
1071 * ignore the match so that the real root gets used.
1072 * This avoids the problem of traversing "../.." to find the
1073 * root directory and never finding it, because "/" resolves
1074 * to the emulation root directory. This is expensive :-(
1075 */
1076 NDINIT(&ndroot, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE, prefix,
1077 td);
1078
1079 /* We shouldn't ever get an error from this namei(). */
1080 error = namei(&ndroot);
1081 if (error == 0) {
1082 if (nd.ni_vp == ndroot.ni_vp)
1083 error = ENOENT;
1084
1085 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1086 vrele(ndroot.ni_vp);
1087 VFS_UNLOCK_GIANT(NDHASGIANT(&ndroot));
1088 }
1089 }
1090
1091 NDFREE(&nd, NDF_ONLY_PNBUF);
1092 vrele(nd.ni_vp);
1093 VFS_UNLOCK_GIANT(NDHASGIANT(&nd));
1094
1095 keeporig:
1096 /* If there was an error, use the original path name. */
1097 if (error)
1098 bcopy(ptr, buf, len);
1099 return (error);
1100 }
Cache object: 8cfa4f3acfcbbf3ba913239d7bdebaf3
|