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