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