xref: /dragonfly/sys/kern/vfs_nlookup.c (revision da0d35cf)
1 /*
2  * Copyright (c) 2004-2020 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * nlookup() is the 'new' namei interface.  Rather then return directory and
36  * leaf vnodes (in various lock states) the new interface instead deals in
37  * namecache records.  Namecache records may represent both a positive or
38  * a negative hit.  The namespace is locked via the namecache record instead
39  * of via the vnode, and only the leaf namecache record (representing the
40  * filename) needs to be locked.
41  *
42  * This greatly improves filesystem parallelism and is a huge simplification
43  * of the API verses the old vnode locking / namei scheme.
44  *
45  * Filesystems must actively control the caching aspects of the namecache,
46  * and since namecache pointers are used as handles they are non-optional
47  * even for filesystems which do not generally wish to cache things.  It is
48  * intended that a separate cache coherency API will be constructed to handle
49  * these issues.
50  */
51 
52 #include "opt_ktrace.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/uio.h>
57 #include <sys/kernel.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/filedesc.h>
61 #include <sys/proc.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/malloc.h>
65 #include <sys/stat.h>
66 #include <sys/objcache.h>
67 #include <sys/file.h>
68 #include <sys/kcollect.h>
69 #include <sys/sysctl.h>
70 
71 #ifdef KTRACE
72 #include <sys/ktrace.h>
73 #endif
74 
75 __read_mostly static int nlookup_debug;
76 SYSCTL_INT(_debug, OID_AUTO, nlookup_debug, CTLFLAG_RW, &nlookup_debug, 0,
77 	"Force retry test");
78 
79 static int naccess(struct nchandle *nch, int vmode, struct ucred *cred,
80 			int *stickyp, int nchislocked);
81 
82 /*
83  * unmount operations flag NLC_IGNBADDIR in order to allow the
84  * umount to successfully issue a nlookup() on the path in order
85  * to extract the mount point.  Allow certain errors through.
86  */
87 static __inline
88 int
89 keeperror(struct nlookupdata *nd, int error)
90 {
91 	if (error) {
92 		if ((nd->nl_flags & NLC_IGNBADDIR) == 0 ||
93 		   (error != EIO && error != EBADRPC && error != ESTALE)) {
94 			return 1;
95 		}
96 	}
97 	return 0;
98 }
99 
100 /*
101  * Initialize a nlookup() structure, early error return for copyin faults
102  * or a degenerate empty string (which is not allowed).
103  *
104  * The first process proc0's credentials are used if the calling thread
105  * is not associated with a process context.
106  *
107  * MPSAFE
108  */
109 int
110 nlookup_init(struct nlookupdata *nd,
111 	     const char *path, enum uio_seg seg, int flags)
112 {
113     size_t pathlen;
114     struct proc *p;
115     thread_t td;
116     int error;
117 
118     td = curthread;
119     p = td->td_proc;
120 
121     /*
122      * note: the pathlen set by copy*str() includes the terminating \0.
123      */
124     bzero(nd, sizeof(struct nlookupdata));
125     nd->nl_path = objcache_get(namei_oc, M_WAITOK);
126     nd->nl_flags |= NLC_HASBUF;
127     if (seg == UIO_SYSSPACE)
128 	error = copystr(path, nd->nl_path, MAXPATHLEN, &pathlen);
129     else
130 	error = copyinstr(path, nd->nl_path, MAXPATHLEN, &pathlen);
131 
132     /*
133      * Don't allow empty pathnames.
134      * POSIX.1 requirement: "" is not a vaild file name.
135      */
136     if (error == 0 && pathlen <= 1)
137 	error = ENOENT;
138 
139     if (error == 0) {
140 	if (p && p->p_fd) {
141 	    if (nd->nl_path[0] == '/') {
142 		    if ((flags & NLC_NLNCH_NOINIT) == 0) {
143 			    nd->nl_basench = &p->p_fd->fd_nrdir;
144 			    cache_copy(nd->nl_basench, &nd->nl_nch);
145 		    }
146 		    cache_copy(&p->p_fd->fd_nrdir, &nd->nl_rootnch);
147 		    if (p->p_fd->fd_njdir.ncp)
148 			cache_copy(&p->p_fd->fd_njdir, &nd->nl_jailnch);
149 		    nd->nl_cred = td->td_ucred;
150 		    nd->nl_flags |= NLC_BORROWCRED;
151 	    } else {
152 		    if ((flags & NLC_NLNCH_NOINIT) == 0) {
153 			    nd->nl_basench = &p->p_fd->fd_ncdir;
154 			    cache_copy(nd->nl_basench, &nd->nl_nch);
155 		    }
156 		    cache_copy(&p->p_fd->fd_nrdir, &nd->nl_rootnch);
157 		    if (p->p_fd->fd_njdir.ncp)
158 			cache_copy(&p->p_fd->fd_njdir, &nd->nl_jailnch);
159 		    nd->nl_cred = td->td_ucred;
160 		    nd->nl_flags |= NLC_BORROWCRED;
161 	    }
162 	} else {
163 	    if ((flags & NLC_NLNCH_NOINIT) == 0) {
164 		    nd->nl_basench = &rootnch;
165 		    cache_copy(nd->nl_basench, &nd->nl_nch);
166 	    }
167 	    cache_copy(&rootnch, &nd->nl_rootnch);
168 	    cache_copy(&rootnch, &nd->nl_jailnch);
169 	    nd->nl_cred = proc0.p_ucred;
170 	    nd->nl_flags |= NLC_BORROWCRED;
171 	}
172 	nd->nl_td = td;
173 	nd->nl_flags |= flags & ~NLC_NLNCH_NOINIT;
174     } else {
175 	nlookup_done(nd);
176     }
177     return(error);
178 }
179 
180 
181 /*
182  * nlookup_init() for "at" family of syscalls.
183  *
184  * Similar to nlookup_init() but if the path is relative and fd is not
185  * AT_FDCWD, the path will be interpreted relative to the directory pointed
186 *  to by fd.  In this case, the file entry pointed to by fd is ref'ed and
187 *  returned in *fpp.
188  *
189  * If the call succeeds, nlookup_done_at() must be called to clean-up the nd
190  * and release the ref to the file entry.
191  */
192 int
193 nlookup_init_at(struct nlookupdata *nd, struct file **fpp, int fd,
194 		const char *path, enum uio_seg seg, int flags)
195 {
196 	struct thread *td = curthread;
197 	struct file* fp;
198 	struct vnode *vp;
199 	int error;
200 
201 	*fpp = NULL;
202 
203 	/*
204 	 * Resolve the path, we might have to copy it in from userland,
205 	 * but don't initialize nl_basench, or nl_nch.
206 	 */
207 	error = nlookup_init(nd, path, seg, flags | NLC_NLNCH_NOINIT);
208 	if (__predict_false(error))
209 		return (error);
210 
211 	/*
212 	 * Setup nl_basench (a pointer only not refd), and copy+ref
213 	 * to initialize nl_nch.  Only applicable to relative paths.
214 	 * For absolute paths, or if (fd) is degenerate, just use the
215 	 * normal path.
216 	 */
217 	if (nd->nl_path[0] == '/') {
218 		struct proc *p = curproc;
219 		nd->nl_basench = &p->p_fd->fd_nrdir;
220 	} else if (fd == AT_FDCWD) {
221 		struct proc *p = curproc;
222 		nd->nl_basench = &p->p_fd->fd_ncdir;
223 	} else {
224 		if ((error = holdvnode(td, fd, &fp)) != 0)
225 			goto done;
226 		vp = (struct vnode*)fp->f_data;
227 		if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL) {
228 			fdrop(fp);
229 			fp = NULL;
230 			error = ENOTDIR;
231 			goto done;
232 		}
233 		nd->nl_basench = &fp->f_nchandle;
234 		*fpp = fp;
235 	}
236 	cache_copy(nd->nl_basench, &nd->nl_nch);
237 done:
238 	if (error)
239 		nlookup_done(nd);
240 	return (error);
241 }
242 
243 /*
244  * This works similarly to nlookup_init() but does not assume a process
245  * context.  rootnch is always chosen for the root directory and the cred
246  * and starting directory are supplied in arguments.
247  */
248 int
249 nlookup_init_raw(struct nlookupdata *nd,
250 	     const char *path, enum uio_seg seg, int flags,
251 	     struct ucred *cred, struct nchandle *ncstart)
252 {
253     size_t pathlen;
254     thread_t td;
255     int error;
256 
257     td = curthread;
258 
259     bzero(nd, sizeof(struct nlookupdata));
260     nd->nl_path = objcache_get(namei_oc, M_WAITOK);
261     nd->nl_flags |= NLC_HASBUF;
262     if (seg == UIO_SYSSPACE)
263 	error = copystr(path, nd->nl_path, MAXPATHLEN, &pathlen);
264     else
265 	error = copyinstr(path, nd->nl_path, MAXPATHLEN, &pathlen);
266 
267     /*
268      * Don't allow empty pathnames.
269      * POSIX.1 requirement: "" is not a vaild file name.
270      */
271     if (error == 0 && pathlen <= 1)
272 	error = ENOENT;
273 
274     if (error == 0) {
275 	cache_copy(ncstart, &nd->nl_nch);
276 	cache_copy(&rootnch, &nd->nl_rootnch);
277 	cache_copy(&rootnch, &nd->nl_jailnch);
278 	nd->nl_cred = crhold(cred);
279 	nd->nl_td = td;
280 	nd->nl_flags |= flags;
281     } else {
282 	nlookup_done(nd);
283     }
284     return(error);
285 }
286 
287 /*
288  * This works similarly to nlookup_init_raw() but does not rely
289  * on rootnch being initialized yet.
290  */
291 int
292 nlookup_init_root(struct nlookupdata *nd,
293 	     const char *path, enum uio_seg seg, int flags,
294 	     struct ucred *cred, struct nchandle *ncstart,
295 	     struct nchandle *ncroot)
296 {
297     size_t pathlen;
298     thread_t td;
299     int error;
300 
301     td = curthread;
302 
303     bzero(nd, sizeof(struct nlookupdata));
304     nd->nl_path = objcache_get(namei_oc, M_WAITOK);
305     nd->nl_flags |= NLC_HASBUF;
306     if (seg == UIO_SYSSPACE)
307 	error = copystr(path, nd->nl_path, MAXPATHLEN, &pathlen);
308     else
309 	error = copyinstr(path, nd->nl_path, MAXPATHLEN, &pathlen);
310 
311     /*
312      * Don't allow empty pathnames.
313      * POSIX.1 requirement: "" is not a vaild file name.
314      */
315     if (error == 0 && pathlen <= 1)
316 	error = ENOENT;
317 
318     if (error == 0) {
319 	cache_copy(ncstart, &nd->nl_nch);
320 	cache_copy(ncroot, &nd->nl_rootnch);
321 	cache_copy(ncroot, &nd->nl_jailnch);
322 	nd->nl_cred = crhold(cred);
323 	nd->nl_td = td;
324 	nd->nl_flags |= flags;
325     } else {
326 	nlookup_done(nd);
327     }
328     return(error);
329 }
330 
331 #if 0
332 /*
333  * Set a different credential; this credential will be used by future
334  * operations performed on nd.nl_open_vp and nlookupdata structure.
335  */
336 void
337 nlookup_set_cred(struct nlookupdata *nd, struct ucred *cred)
338 {
339 	KKASSERT(nd->nl_cred != NULL);
340 
341 	if (nd->nl_cred != cred) {
342 		cred = crhold(cred);
343 		if ((nd->nl_flags & NLC_BORROWCRED) == 0)
344 			crfree(nd->nl_cred);
345 		nd->nl_flags &= ~NLC_BORROWCRED;
346 		nd->nl_cred = cred;
347 	}
348 }
349 #endif
350 
351 /*
352  * Cleanup a nlookupdata structure after we are through with it.  This may
353  * be called on any nlookupdata structure initialized with nlookup_init().
354  * Calling nlookup_done() is mandatory in all cases except where nlookup_init()
355  * returns an error, even if as a consumer you believe you have taken all
356  * dynamic elements out of the nlookupdata structure.
357  */
358 void
359 nlookup_done(struct nlookupdata *nd)
360 {
361     if (nd->nl_nch.ncp) {
362 	if (nd->nl_flags & NLC_NCPISLOCKED)
363 	    cache_unlock(&nd->nl_nch);
364 	cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
365     }
366     nd->nl_flags &= ~NLC_NCPISLOCKED;
367     if (nd->nl_rootnch.ncp)
368 	cache_drop_and_cache(&nd->nl_rootnch, 0);
369     if (nd->nl_jailnch.ncp)
370 	cache_drop_and_cache(&nd->nl_jailnch, 0);
371     if ((nd->nl_flags & NLC_HASBUF) && nd->nl_path) {
372 	objcache_put(namei_oc, nd->nl_path);
373 	nd->nl_path = NULL;
374     }
375     if (nd->nl_cred) {
376 	if ((nd->nl_flags & NLC_BORROWCRED) == 0)
377 	    crfree(nd->nl_cred);
378 	nd->nl_cred = NULL;
379 	nd->nl_flags &= ~NLC_BORROWCRED;
380     }
381     if (nd->nl_open_vp) {
382 	if (nd->nl_flags & NLC_LOCKVP) {
383 		vn_unlock(nd->nl_open_vp);
384 		nd->nl_flags &= ~NLC_LOCKVP;
385 	}
386 	vn_close(nd->nl_open_vp, nd->nl_vp_fmode, NULL);
387 	nd->nl_open_vp = NULL;
388     }
389     if (nd->nl_dvp) {
390 	vrele(nd->nl_dvp);
391 	nd->nl_dvp = NULL;
392     }
393     nd->nl_flags = 0;	/* clear remaining flags (just clear everything) */
394     nd->nl_basench = NULL;
395 }
396 
397 /*
398  * Works similarly to nlookup_done() when nd initialized with
399  * nlookup_init_at().
400  */
401 void
402 nlookup_done_at(struct nlookupdata *nd, struct file *fp)
403 {
404 	nlookup_done(nd);
405 	if (fp != NULL)
406 		fdrop(fp);
407 }
408 
409 void
410 nlookup_zero(struct nlookupdata *nd)
411 {
412 	bzero(nd, sizeof(struct nlookupdata));
413 }
414 
415 /*
416  * Simple all-in-one nlookup.  Returns a locked namecache structure or NULL
417  * if an error occured.
418  *
419  * Note that the returned ncp is not checked for permissions, though VEXEC
420  * is checked on the directory path leading up to the result.  The caller
421  * must call naccess() to check the permissions of the returned leaf.
422  */
423 struct nchandle
424 nlookup_simple(const char *str, enum uio_seg seg,
425 	       int niflags, int *error)
426 {
427     struct nlookupdata nd;
428     struct nchandle nch;
429 
430     *error = nlookup_init(&nd, str, seg, niflags);
431     if (*error == 0) {
432 	    if ((*error = nlookup(&nd)) == 0) {
433 		    nch = nd.nl_nch;	/* keep hold ref from structure */
434 		    cache_zero(&nd.nl_nch); /* and NULL out */
435 	    } else {
436 		    cache_zero(&nch);
437 	    }
438 	    nlookup_done(&nd);
439     } else {
440 	    cache_zero(&nch);
441     }
442     return(nch);
443 }
444 
445 /*
446  * Returns non-zero if the path element is the last element
447  */
448 static
449 int
450 islastelement(const char *ptr)
451 {
452 	while (*ptr == '/')
453 		++ptr;
454 	return (*ptr == 0);
455 }
456 
457 /*
458  * Returns non-zero if we need to lock the namecache element
459  * exclusively.  Unless otherwise requested by NLC_SHAREDLOCK,
460  * the last element of the namecache lookup will be locked
461  * exclusively.
462  *
463  * O_CREAT or O_TRUNC need the last element to be locked exlcusively.
464  * Intermediate elements are always locked shared.
465  *
466  * NOTE: Even if we return on-zero, an unresolved namecache record
467  *	 will always be locked exclusively.
468  */
469 static __inline
470 int
471 wantsexcllock(struct nlookupdata *nd, int last_element)
472 {
473 	if ((nd->nl_flags & NLC_SHAREDLOCK) == 0)
474 		return(last_element);
475 	return 0;
476 }
477 
478 
479 /*
480  * Do a generic nlookup.  Note that the passed nd is not nlookup_done()'d
481  * on return, even if an error occurs.  If no error occurs or NLC_CREATE
482  * is flagged and ENOENT is returned, then the returned nl_nch is always
483  * referenced and locked exclusively.
484  *
485  * WARNING: For any general error other than ENOENT w/NLC_CREATE, the
486  *	    the resulting nl_nch may or may not be locked and if locked
487  *	    might be locked either shared or exclusive.
488  *
489  * Intermediate directory elements, including the current directory, require
490  * execute (search) permission.  nlookup does not examine the access
491  * permissions on the returned element.
492  *
493  * If NLC_CREATE is set the last directory must allow node creation,
494  * and an error code of 0 will be returned for a non-existant
495  * target (not ENOENT).
496  *
497  * If NLC_RENAME_DST is set the last directory mut allow node deletion,
498  * plus the sticky check is made, and an error code of 0 will be returned
499  * for a non-existant target (not ENOENT).
500  *
501  * If NLC_DELETE is set the last directory mut allow node deletion,
502  * plus the sticky check is made.
503  *
504  * If NLC_REFDVP is set nd->nl_dvp will be set to the directory vnode
505  * of the returned entry.  The vnode will be referenced, but not locked,
506  * and will be released by nlookup_done() along with everything else.
507  *
508  * NOTE: As an optimization we attempt to obtain a shared namecache lock
509  *	 on any intermediate elements.  On success, the returned element
510  *	 is ALWAYS locked exclusively.
511  */
512 int
513 nlookup(struct nlookupdata *nd)
514 {
515     globaldata_t gd = mycpu;
516     struct nlcomponent nlc;
517     struct nchandle nch;
518     struct nchandle nctmp;
519     struct mount *mp;
520     int wasdotordotdot;
521     char *path_reset;
522     char *ptr;
523     char *nptr;
524     int error;
525     int len;
526     int dflags;
527     int hit = 1;
528     int saveflag = nd->nl_flags;
529     boolean_t doretry = FALSE;
530     boolean_t inretry = FALSE;
531 
532     if (nlookup_debug > 0) {
533 	--nlookup_debug;
534 	doretry = 1;
535     }
536     path_reset = NULL;
537 
538 nlookup_start:
539 
540 #ifdef KTRACE
541     if (KTRPOINT(nd->nl_td, KTR_NAMEI))
542 	ktrnamei(nd->nl_td->td_lwp, nd->nl_path);
543 #endif
544     bzero(&nlc, sizeof(nlc));
545 
546     /*
547      * Setup for the loop.  The current working namecache element is
548      * always at least referenced.  We lock it as required, but always
549      * return a locked, resolved namecache entry.
550      */
551     nd->nl_loopcnt = 0;
552     if (nd->nl_dvp) {
553 	vrele(nd->nl_dvp);
554 	nd->nl_dvp = NULL;
555     }
556     ptr = nd->nl_path;
557 
558     /*
559      * Loop on the path components.  At the top of the loop nd->nl_nch
560      * is ref'd and unlocked and represents our current position.
561      */
562     for (;;) {
563 	int last_element;
564 
565 	++nd->nl_elmno;
566 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
567 
568 	/*
569 	 * Check if the root directory should replace the current
570 	 * directory.  This is done at the start of a translation
571 	 * or after a symbolic link has been found.  In other cases
572 	 * ptr will never be pointing at a '/'.
573 	 */
574 	if (*ptr == '/') {
575 	    do {
576 		++ptr;
577 	    } while (*ptr == '/');
578 
579 	    /*
580 	     * We might already be at the root as a pre-optimization
581 	     */
582 	    if (nd->nl_nch.mount != nd->nl_rootnch.mount ||
583 		nd->nl_nch.ncp != nd->nl_rootnch.ncp) {
584 		cache_drop_and_cache(&nd->nl_nch, 0);
585 		cache_copy(&nd->nl_rootnch, &nd->nl_nch);
586 	    }
587 
588 	    /*
589 	     * Fast-track termination.  There is no parent directory of
590 	     * the root in the same mount from the point of view of
591 	     * the caller so return EACCES if NLC_REFDVP is specified,
592 	     * and EEXIST if NLC_CREATE is also specified.
593 	     * e.g. 'rmdir /' or 'mkdir /' are not allowed.
594 	     */
595 	    if (*ptr == 0) {
596 		if (nd->nl_flags & NLC_REFDVP)
597 			error = (nd->nl_flags & NLC_CREATE) ? EEXIST : EACCES;
598 		else
599 			error = 0;
600 		nd->nl_flags |= NLC_NCPISLOCKED;
601 		cache_lock_maybe_shared(&nd->nl_nch,
602 					wantsexcllock(nd, islastelement(ptr)));
603 		break;
604 	    }
605 	    continue;
606 	}
607 
608 	/*
609 	 * Pre-calculate next path component so we can check whether the
610 	 * current component directory is the last directory in the path
611 	 * or not.
612 	 */
613 	for (nptr = ptr; *nptr && *nptr != '/'; ++nptr)
614 		;
615 
616 	/*
617 	 * nd->nl_nch is referenced and not locked here.
618 	 *
619 	 * Check directory search permissions.  This will load dflags to
620 	 * obtain directory-special permissions to be checked along with the
621 	 * last component.
622 	 *
623 	 * We only need to pass-in &dflags for the second-to-last component.
624 	 * Optimize by passing-in NULL for any prior components, which may
625 	 * allow the code to bypass the naccess() call.
626 	 *
627 	 * naccess() is optimized to avoid having to lock the nch or get
628 	 * the related vnode if cached perms are sufficient.
629 	 */
630 	dflags = 0;
631 	if (*nptr == '/' || (saveflag & NLC_MODIFYING_MASK) == 0)
632 	    error = naccess(&nd->nl_nch, NLC_EXEC, nd->nl_cred, NULL, 0);
633 	else
634 	    error = naccess(&nd->nl_nch, NLC_EXEC, nd->nl_cred, &dflags, 0);
635 	if (error) {
636 	    if (keeperror(nd, error))
637 		    break;
638 	    error = 0;
639 	}
640 
641 	/*
642 	 * Extract the next (or last) path component.  Path components are
643 	 * limited to 255 characters.
644 	 */
645 	nlc.nlc_nameptr = ptr;
646 	nlc.nlc_namelen = nptr - ptr;
647 	ptr = nptr;
648 	if (nlc.nlc_namelen >= 256) {
649 	    error = ENAMETOOLONG;
650 	    break;
651 	}
652 	last_element = islastelement(nptr);
653 
654 	/*
655 	 * Lookup the path component in the cache, creating an unresolved
656 	 * entry if necessary.  We have to handle "." and ".." as special
657 	 * cases.
658 	 *
659 	 * When handling ".." we have to detect a traversal back through a
660 	 * mount point.   If we are at the root, ".." just returns the root.
661 	 *
662 	 * When handling "." or ".." we also have to recalculate dflags
663 	 * since our dflags will be for some sub-directory instead of the
664 	 * parent dir.
665 	 *
666 	 * This subsection returns a referenced and possibly locked 'nch'.
667 	 * The locking status is based on the last_element flag.
668 	 *
669 	 * The namecache topology is not allowed to be disconnected, so
670 	 * encountering a NULL parent will generate EINVAL.  This typically
671 	 * occurs when a directory is removed out from under a process.
672 	 *
673 	 * WARNING! The unlocking of nd->nl_nch is sensitive code.
674 	 */
675 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
676 
677 	if (nlc.nlc_namelen == 1 && nlc.nlc_nameptr[0] == '.') {
678 	    if (last_element) {
679 		cache_get_maybe_shared(&nd->nl_nch, &nch,
680 				       wantsexcllock(nd, 1));
681 	    } else {
682 		cache_copy(&nd->nl_nch, &nch);
683 	    }
684 	    wasdotordotdot = 1;
685 	} else if (nlc.nlc_namelen == 2 &&
686 		   nlc.nlc_nameptr[0] == '.' && nlc.nlc_nameptr[1] == '.') {
687 	    if (nd->nl_nch.mount == nd->nl_rootnch.mount &&
688 		nd->nl_nch.ncp == nd->nl_rootnch.ncp
689 	    ) {
690 		/*
691 		 * ".." at the root returns the root
692 		 */
693 		if (last_element) {
694 		    cache_get_maybe_shared(&nd->nl_nch, &nch,
695 					   wantsexcllock(nd, 1));
696 		} else {
697 		    cache_copy(&nd->nl_nch, &nch);
698 		}
699 	    } else {
700 		/*
701 		 * Locate the parent ncp.  If we are at the root of a
702 		 * filesystem mount we have to skip to the mounted-on
703 		 * point in the underlying filesystem.
704 		 *
705 		 * Expect the parent to always be good since the
706 		 * mountpoint doesn't go away.  XXX hack.  cache_get()
707 		 * requires the ncp to already have a ref as a safety.
708 		 *
709 		 * However, a process which has been broken out of a chroot
710 		 * will wind up with a NULL parent if it tries to '..' above
711 		 * the real root, deal with the case.  Note that this does
712 		 * not protect us from a jail breakout, it just stops a panic
713 		 * if the jail-broken process tries to '..' past the real
714 		 * root.
715 		 */
716 		nctmp = nd->nl_nch;
717 		while (nctmp.ncp == nctmp.mount->mnt_ncmountpt.ncp) {
718 			nctmp = nctmp.mount->mnt_ncmounton;
719 			if (nctmp.ncp == NULL)
720 				break;
721 		}
722 		if (nctmp.ncp == NULL) {
723 			if (curthread->td_proc) {
724 				kprintf("vfs_nlookup: '..' traverse broke "
725 					"jail: pid %d (%s)\n",
726 					curthread->td_proc->p_pid,
727 					curthread->td_comm);
728 			}
729 			nctmp = nd->nl_rootnch;
730 		} else {
731 			nctmp.ncp = nctmp.ncp->nc_parent;
732 		}
733 		if (last_element) {
734 		    cache_get_maybe_shared(&nctmp, &nch,
735 					   wantsexcllock(nd, 1));
736 		} else {
737 		    cache_copy(&nctmp, &nch);
738 		}
739 	    }
740 	    wasdotordotdot = 2;
741 	} else {
742 	    /*
743 	     * Quickly lookup the component.  If we can't find it, then
744 	     * slowly lookup and resolve the component.
745 	     */
746 	    if (last_element) {
747 		    error = cache_nlookup_maybe_shared(&nd->nl_nch, &nlc,
748 						       wantsexcllock(nd, 1),
749 						       &nch);
750 	    } else {
751 		    nch = cache_nlookup_nonlocked(&nd->nl_nch, &nlc);
752 		    if (nch.ncp == NULL)
753 			error = EWOULDBLOCK;
754 	    }
755 
756 	    /*
757 	     * At this point the only possible error is EWOULDBLOCK.
758 	     *
759 	     * If no error nch is set and referenced, and then also locked
760 	     * according to last_element.  For EWOULDBLOCK nch is not set.
761 	     * For any other error nch is set and referenced, but not locked.
762 	     *
763 	     * On EWOULDBLOCK the ncp may be unresolved (if not locked it can
764 	     * become unresolved at any time, but we don't care at this time).
765 	     */
766 	    if (error == EWOULDBLOCK) {
767 		nch = cache_nlookup(&nd->nl_nch, &nlc);
768 		if (nch.ncp->nc_flag & NCF_UNRESOLVED)
769 		    hit = 0;
770 		for (;;) {
771 		    error = cache_resolve(&nch, nd->nl_cred);
772 		    if (error != EAGAIN &&
773 			(nch.ncp->nc_flag & NCF_DESTROYED) == 0) {
774 			    if (error == ESTALE) {
775 				if (!inretry)
776 				    error = ENOENT;
777 				doretry = TRUE;
778 			    }
779 			    if (last_element == 0)
780 				    cache_unlock(&nch);
781 			    break;
782 		    }
783 		    kprintf("[diagnostic] nlookup: relookup %*.*s\n",
784 			    nch.ncp->nc_nlen, nch.ncp->nc_nlen,
785 			    nch.ncp->nc_name);
786 		    cache_put(&nch);
787 		    nch = cache_nlookup(&nd->nl_nch, &nlc);
788 		}
789 	    }
790 	    wasdotordotdot = 0;
791 	}
792 
793 	/*
794 	 * If the component is "." or ".." our dflags no longer represents
795 	 * the parent directory and we have to explicitly look it up.
796 	 *
797 	 * Expect the parent to be good since nch is locked.
798 	 *
799 	 * nch will continue to be valid even if an error occurs after this
800 	 * point.
801 	 */
802 	if (wasdotordotdot && error == 0) {
803 	    struct nchandle par;
804 
805 	    dflags = 0;
806 	    if (last_element == 0)
807 		cache_lock_maybe_shared(&nch, wantsexcllock(nd, 0));
808 
809 	    if ((par.ncp = nch.ncp->nc_parent) != NULL) {
810 		par.mount = nch.mount;
811 		cache_hold(&par);
812 		error = naccess(&par, 0, nd->nl_cred, &dflags, 0);
813 		cache_drop_and_cache(&par, nd->nl_elmno - 1);
814 		if (error) {
815 		    if (!keeperror(nd, error))
816 			error = 0;
817 		    if (error == EINVAL) {
818 			kprintf("nlookup (%s): trailing . or .. retry on %s\n",
819 				curthread->td_comm, nd->nl_path);
820 			doretry = TRUE;
821 		    }
822 		}
823 	    }
824 
825 	    if (last_element == 0)
826 		cache_unlock(&nch);
827 	}
828 
829 	/*
830 	 * [end of subsection]
831 	 *
832 	 * nch is referenced and locked according to (last_element).
833 	 * nd->nl_nch is unlocked and referenced.
834 	 */
835 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
836 
837 	/*
838 	 * Resolve the namespace if necessary.  The ncp returned by
839 	 * cache_nlookup() is referenced, and also locked according
840 	 * to last_element.
841 	 *
842 	 * XXX neither '.' nor '..' should return EAGAIN since they were
843 	 * previously resolved and thus cannot be newly created ncp's.
844 	 */
845 	if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
846 	    if (last_element == 0)
847 		cache_lock(&nch);
848 	    hit = 0;
849 	    error = cache_resolve(&nch, nd->nl_cred);
850 	    if (error == ESTALE) {
851 		if (!inretry)
852 		    error = ENOENT;
853 		doretry = TRUE;
854 	    }
855 	    if (last_element == 0)
856 		cache_unlock(&nch);
857 	    KKASSERT(error != EAGAIN);
858 	} else {
859 	    error = nch.ncp->nc_error;
860 	}
861 
862 	/*
863 	 * Early completion.  ENOENT is not an error if this is the last
864 	 * component and NLC_CREATE or NLC_RENAME (rename target) was
865 	 * requested.  Note that ncp->nc_error is left as ENOENT in that
866 	 * case, which we check later on.
867 	 *
868 	 * Also handle invalid '.' or '..' components terminating a path
869 	 * for a create/rename/delete.  The standard requires this and pax
870 	 * pretty stupidly depends on it.
871 	 */
872 	if (last_element) {
873 	    if (error == ENOENT &&
874 		(nd->nl_flags & (NLC_CREATE | NLC_RENAME_DST))
875 	    ) {
876 		if (nd->nl_flags & NLC_NFS_RDONLY) {
877 			error = EROFS;
878 		} else {
879 			error = naccess(&nch, nd->nl_flags | dflags,
880 					nd->nl_cred, NULL, last_element);
881 		}
882 	    }
883 	    if (error == 0 && wasdotordotdot &&
884 		(nd->nl_flags & (NLC_CREATE | NLC_DELETE |
885 				 NLC_RENAME_SRC | NLC_RENAME_DST))) {
886 		/*
887 		 * POSIX junk
888 		 */
889 		if (nd->nl_flags & NLC_CREATE)
890 			error = EEXIST;
891 		else if (nd->nl_flags & NLC_DELETE)
892 			error = (wasdotordotdot == 1) ? EINVAL : ENOTEMPTY;
893 		else
894 			error = EINVAL;
895 	    }
896 	}
897 
898 	/*
899 	 * Early completion on error.
900 	 */
901 	if (error) {
902 	    if (last_element)
903 		    cache_unlock(&nch);
904 	    cache_drop_and_cache(&nch, nd->nl_elmno);
905 	    break;
906 	}
907 
908 	/*
909 	 * If the element is a symlink and it is either not the last
910 	 * element or it is the last element and we are allowed to
911 	 * follow symlinks, resolve the symlink.
912 	 */
913 	if ((nch.ncp->nc_flag & NCF_ISSYMLINK) &&
914 	    (*ptr || (nd->nl_flags & NLC_FOLLOW))
915 	) {
916 	    if (nd->nl_loopcnt++ >= MAXSYMLINKS) {
917 		error = ELOOP;
918 		if (last_element)
919 			cache_unlock(&nch);
920 		cache_drop_and_cache(&nch, nd->nl_elmno);
921 		break;
922 	    }
923 	    if (last_element == 0)
924 		cache_lock_maybe_shared(&nch, 1);
925 
926 	    error = nreadsymlink(nd, &nch, &nlc);
927 	    cache_put(&nch);
928 	    if (error)
929 		break;
930 
931 	    /*
932 	     * Concatenate trailing path elements onto the returned symlink.
933 	     * Note that if the path component (ptr) is not exhausted, it
934 	     * will being with a '/', so we do not have to add another one.
935 	     *
936 	     * The symlink may not be empty.
937 	     */
938 	    len = strlen(ptr);
939 	    if (nlc.nlc_namelen == 0 || nlc.nlc_namelen + len >= MAXPATHLEN) {
940 		error = nlc.nlc_namelen ? ENAMETOOLONG : ENOENT;
941 		objcache_put(namei_oc, nlc.nlc_nameptr);
942 		break;
943 	    }
944 	    bcopy(ptr, nlc.nlc_nameptr + nlc.nlc_namelen, len + 1);
945 
946 	    if (path_reset) {
947 		if (nd->nl_flags & NLC_HASBUF)
948 		    objcache_put(namei_oc, nd->nl_path);
949 	    } else {
950 		path_reset = nd->nl_path;
951 	    }
952 	    nd->nl_path = nlc.nlc_nameptr;
953 	    nd->nl_flags |= NLC_HASBUF;
954 	    ptr = nd->nl_path;
955 
956 	    /*
957 	     * Go back up to the top to resolve any initial '/'s in the
958 	     * symlink.
959 	     */
960 	    continue;
961 	}
962 
963 	/*
964 	 * If the element is a directory and we are crossing a mount point,
965 	 * Locate the mount.
966 	 */
967 	while ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
968 	    (nd->nl_flags & NLC_NOCROSSMOUNT) == 0 &&
969 	    (mp = cache_findmount(&nch)) != NULL
970 	) {
971 	    struct vnode *tdp;
972 	    int vfs_do_busy = 0;
973 
974 	    /*
975 	     * VFS must be busied before the namecache entry is locked,
976 	     * but we don't want to waste time calling vfs_busy() if the
977 	     * mount point is already resolved.
978 	     */
979 again:
980 	    if (last_element)
981 		    cache_unlock(&nch);
982 	    cache_drop_and_cache(&nch, nd->nl_elmno);
983 
984 	    if (vfs_do_busy) {
985 		while (vfs_busy(mp, 0)) {
986 		    if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
987 			kprintf("nlookup: warning umount race avoided\n");
988 			cache_dropmount(mp);
989 			error = EBUSY;
990 			vfs_do_busy = 0;
991 			goto double_break;
992 		    }
993 		}
994 	    }
995 
996 	    /*
997 	     * We don't need to lock the nch unless the entry is unresolved
998 	     * or this is the last element.
999 	     */
1000 	    if (last_element)
1001 		cache_get_maybe_shared(&mp->mnt_ncmountpt, &nch,
1002 				       wantsexcllock(nd, 1));
1003 	    else
1004 		cache_copy(&mp->mnt_ncmountpt, &nch);
1005 
1006 	    if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1007 		if (last_element == 0)
1008 		    cache_lock(&nch);
1009 		if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1010 		    if (vfs_do_busy == 0) {
1011 			vfs_do_busy = 1;
1012 			if (last_element == 0)
1013 			    cache_unlock(&nch);
1014 			goto again;
1015 		    }
1016 		    error = VFS_ROOT(mp, &tdp);
1017 		    vfs_unbusy(mp);
1018 		    vfs_do_busy = 0;
1019 		    if (keeperror(nd, error)) {
1020 			cache_dropmount(mp);
1021 			if (last_element == 0)
1022 			    cache_unlock(&nch);
1023 			break;
1024 		    }
1025 		    if (error == 0) {
1026 			cache_setvp(&nch, tdp);
1027 			vput(tdp);
1028 		    }
1029 		}
1030 		if (last_element == 0)
1031 		    cache_unlock(&nch);
1032 	    }
1033 	    if (vfs_do_busy)
1034 		vfs_unbusy(mp);
1035 	    cache_dropmount(mp);
1036 	}
1037 
1038 	/*
1039 	 * Break out on error
1040 	 */
1041 	if (keeperror(nd, error)) {
1042 	    if (last_element)
1043 		    cache_unlock(&nch);
1044 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1045 double_break:
1046 	    break;
1047 	}
1048 
1049 	/*
1050 	 * Skip any slashes to get to the next element.  If there
1051 	 * are any slashes at all the current element must be a
1052 	 * directory or, in the create case, intended to become a directory.
1053 	 * If it isn't we break without incrementing ptr and fall through
1054 	 * to the failure case below.
1055 	 */
1056 	while (*ptr == '/') {
1057 	    if ((nch.ncp->nc_flag & NCF_ISDIR) == 0 &&
1058 		!(nd->nl_flags & NLC_WILLBEDIR)
1059 	    ) {
1060 		break;
1061 	    }
1062 	    ++ptr;
1063 	}
1064 
1065 	/*
1066 	 * Continuation case: additional elements and the current
1067 	 * element is a directory.
1068 	 */
1069 	if (*ptr && (nch.ncp->nc_flag & NCF_ISDIR)) {
1070 	    cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1071 	    if (last_element)
1072 		    cache_unlock(&nch);
1073 	    /*nchislocked = 0; not needed */
1074 	    KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
1075 	    nd->nl_nch = nch;
1076 	    continue;
1077 	}
1078 
1079 	/*
1080 	 * Failure case: additional elements and the current element
1081 	 * is not a directory
1082 	 */
1083 	if (*ptr) {
1084 	    if (last_element)
1085 		    cache_unlock(&nch);
1086 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1087 	    error = ENOTDIR;
1088 	    break;
1089 	}
1090 
1091 	/*
1092 	 * Successful lookup of last element.
1093 	 *
1094 	 * Check permissions if the target exists.  If the target does not
1095 	 * exist directory permissions were already tested in the early
1096 	 * completion code above.
1097 	 *
1098 	 * nd->nl_flags will be adjusted on return with NLC_APPENDONLY
1099 	 * if the file is marked append-only, and NLC_STICKY if the directory
1100 	 * containing the file is sticky.
1101 	 */
1102 	KKASSERT(last_element);
1103 
1104 	if (nch.ncp->nc_vp && (nd->nl_flags & NLC_ALLCHKS)) {
1105 	    error = naccess(&nch, nd->nl_flags | dflags,
1106 			    nd->nl_cred, NULL, 1);
1107 	    if (keeperror(nd, error)) {
1108 		cache_put(&nch);
1109 		break;
1110 	    }
1111 	}
1112 
1113 	/*
1114 	 * Termination: no more elements.
1115 	 *
1116 	 * Check to see if the immediate parent has been destroyed.  This race
1117 	 * can occur because the element lookup must temporarily unlock
1118 	 * the parent.  If so, do a retry.
1119 	 */
1120 	if (nch.ncp->nc_parent &&
1121 	    (nch.ncp->nc_parent->nc_flag & NCF_DESTROYED)) {
1122 		doretry = TRUE;
1123 	}
1124 
1125 	/*
1126 	 * Termination: no more elements.
1127 	 *
1128 	 * If NLC_REFDVP is set acquire a referenced parent dvp.  Typically
1129 	 * used for mkdir/mknod/ncreate/nremove/unlink/rename.
1130 	 *
1131 	 * NOTE: nd->nl_nch does not necessarily represent the parent
1132 	 *	 directory, e.g. due to a mount point transition.
1133 	 *
1134 	 * nch is locked, standard lock order for the namecache is
1135 	 * child-to-parent so we can safely lock its parent.  We can
1136 	 * just use cache_dvpref().
1137 	 *
1138 	 * If nc_parent is NULL this is probably a mount point or a deleted
1139 	 * file and there is no legal parent directory.  However, we do not
1140 	 * want to fail the nlookup() because a higher level may wish to
1141 	 * return a better error code, such as mkdir("/mntpt") would want to
1142 	 * return EEXIST
1143 	 */
1144 	if ((nd->nl_flags & NLC_REFDVP) &&
1145 	    (doretry == FALSE || inretry == TRUE)) {
1146 		if (nch.ncp->nc_parent) {
1147 			nd->nl_dvp = cache_dvpref(nch.ncp);
1148 			if (nd->nl_dvp == NULL) {
1149 				error = EINVAL;
1150 				if (keeperror(nd, error)) {
1151 					kprintf("Parent directory lost during "
1152 						"nlookup: %s/%s (%08x/%08x)\n",
1153 						nch.ncp->nc_parent->nc_name,
1154 						nch.ncp->nc_name,
1155 						nch.ncp->nc_parent->nc_flag,
1156 						nch.ncp->nc_flag);
1157 					cache_put(&nch);
1158 					break;
1159 				}
1160 			}
1161 		} else {
1162 			error = 0;
1163 			cache_put(&nch);
1164 			break;
1165 		}
1166 	}
1167 	cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1168 	nd->nl_nch = nch;
1169 	nd->nl_flags |= NLC_NCPISLOCKED;
1170 	error = 0;
1171 	break;
1172     }
1173 
1174     /*
1175      * We are done / or possibly retry
1176      */
1177 
1178     if (hit)
1179 	++gd->gd_nchstats->ncs_longhits;
1180     else
1181 	++gd->gd_nchstats->ncs_longmiss;
1182 
1183     if (nd->nl_flags & NLC_NCPISLOCKED)
1184 	KKASSERT(cache_lockstatus(&nd->nl_nch) > 0);
1185 
1186     /*
1187      * Reset nd->nl_path if necessary (due to softlinks).  We want to return
1188      * nl_path to its original state before retrying or returning.
1189      */
1190     if (path_reset) {
1191 	if (nd->nl_flags & NLC_HASBUF) {
1192 	    objcache_put(namei_oc, nd->nl_path);
1193 	    nd->nl_flags &= ~NLC_HASBUF;
1194 	}
1195 	nd->nl_path = path_reset;
1196 	nd->nl_flags |= saveflag & NLC_HASBUF;
1197 	path_reset = NULL;
1198     }
1199 
1200     /*
1201      * Retry the whole thing if doretry flag is set, but only once.
1202      *
1203      * autofs(5) may mount another filesystem under its root directory
1204      * while resolving a path.
1205      *
1206      * NFS might return ESTALE
1207      */
1208     if (doretry && !inretry) {
1209 	kprintf("nlookup: errno %d retry %s\n", error, nd->nl_path);
1210 	inretry = TRUE;
1211 
1212 	/*
1213 	 * Clean up nd->nl_nch and reset to base directory
1214 	 */
1215 	if (nd->nl_flags & NLC_NCPISLOCKED) {
1216 		cache_unlock(&nd->nl_nch);
1217 		nd->nl_flags &= ~NLC_NCPISLOCKED;
1218 	}
1219 	cache_drop(&nd->nl_nch);
1220 	cache_copy(nd->nl_basench, &nd->nl_nch);
1221 
1222 	nd->nl_elmno = 0;
1223 	nd->nl_flags |= saveflag;
1224 
1225 	goto nlookup_start;
1226     }
1227 
1228     /*
1229      * NOTE: If NLC_CREATE was set the ncp may represent a negative hit
1230      * (ncp->nc_error will be ENOENT), but we will still return an error
1231      * code of 0.
1232      */
1233     return(error);
1234 }
1235 
1236 /*
1237  * Resolve a mount point's glue ncp.  This ncp connects creates the illusion
1238  * of continuity in the namecache tree by connecting the ncp related to the
1239  * vnode under the mount to the ncp related to the mount's root vnode.
1240  *
1241  * If no error occured a locked, ref'd ncp is stored in *ncpp.
1242  */
1243 int
1244 nlookup_mp(struct mount *mp, struct nchandle *nch)
1245 {
1246     struct vnode *vp;
1247     int error;
1248 
1249     error = 0;
1250     cache_get(&mp->mnt_ncmountpt, nch);
1251     if (nch->ncp->nc_flag & NCF_UNRESOLVED) {
1252 	while (vfs_busy(mp, 0))
1253 	    ;
1254 	error = VFS_ROOT(mp, &vp);
1255 	vfs_unbusy(mp);
1256 	if (error) {
1257 	    cache_put(nch);
1258 	} else {
1259 	    cache_setvp(nch, vp);
1260 	    vput(vp);
1261 	}
1262     }
1263     return(error);
1264 }
1265 
1266 /*
1267  * Read the contents of a symlink, allocate a path buffer out of the
1268  * namei_oc and initialize the supplied nlcomponent with the result.
1269  *
1270  * If an error occurs no buffer will be allocated or returned in the nlc.
1271  */
1272 int
1273 nreadsymlink(struct nlookupdata *nd, struct nchandle *nch,
1274 		struct nlcomponent *nlc)
1275 {
1276     struct vnode *vp;
1277     struct iovec aiov;
1278     struct uio auio;
1279     int linklen;
1280     int error;
1281     char *cp;
1282 
1283     nlc->nlc_nameptr = NULL;
1284     nlc->nlc_namelen = 0;
1285     if (nch->ncp->nc_vp == NULL)
1286 	return(ENOENT);
1287     if ((error = cache_vget(nch, nd->nl_cred, LK_SHARED, &vp)) != 0)
1288 	return(error);
1289     cp = objcache_get(namei_oc, M_WAITOK);
1290     aiov.iov_base = cp;
1291     aiov.iov_len = MAXPATHLEN;
1292     auio.uio_iov = &aiov;
1293     auio.uio_iovcnt = 1;
1294     auio.uio_offset = 0;
1295     auio.uio_rw = UIO_READ;
1296     auio.uio_segflg = UIO_SYSSPACE;
1297     auio.uio_td = nd->nl_td;
1298     auio.uio_resid = MAXPATHLEN - 1;
1299     error = VOP_READLINK(vp, &auio, nd->nl_cred);
1300     if (error)
1301 	goto fail;
1302     linklen = MAXPATHLEN - 1 - auio.uio_resid;
1303     if (varsym_enable) {
1304 	linklen = varsymreplace(cp, linklen, MAXPATHLEN - 1);
1305 	if (linklen < 0) {
1306 	    error = ENAMETOOLONG;
1307 	    goto fail;
1308 	}
1309     }
1310     cp[linklen] = 0;
1311     nlc->nlc_nameptr = cp;
1312     nlc->nlc_namelen = linklen;
1313     vput(vp);
1314     return(0);
1315 fail:
1316     objcache_put(namei_oc, cp);
1317     vput(vp);
1318     return(error);
1319 }
1320 
1321 /*
1322  * Check access [XXX cache vattr!] [XXX quota]
1323  *
1324  * Generally check the NLC_* access bits.   All specified bits must pass
1325  * for this function to return 0.
1326  *
1327  * The file does not have to exist when checking NLC_CREATE or NLC_RENAME_DST
1328  * access, otherwise it must exist.  No error is returned in this case.
1329  *
1330  * The file must not exist if NLC_EXCL is specified.
1331  *
1332  * Directory permissions in general are tested for NLC_CREATE if the file
1333  * does not exist, NLC_DELETE if the file does exist, and NLC_RENAME_DST
1334  * whether the file exists or not.
1335  *
1336  * The directory sticky bit is tested for NLC_DELETE and NLC_RENAME_DST,
1337  * the latter is only tested if the target exists.
1338  *
1339  * The passed ncp must be referenced and locked.  If it is already resolved
1340  * it may be locked shared but otherwise should be locked exclusively.
1341  */
1342 
1343 #define S_WXOK_MASK	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1344 #define S_XOK_MASK	(S_IXUSR|S_IXGRP|S_IXOTH)
1345 
1346 static int
1347 naccess(struct nchandle *nch, int nflags, struct ucred *cred, int *nflagsp,
1348 	int nchislocked)
1349 {
1350     struct vnode *vp;
1351     struct vattr_lite lva;
1352     struct namecache *ncp;
1353     int error;
1354     int cflags;
1355 
1356     KKASSERT(nchislocked == 0 || cache_lockstatus(nch) > 0);
1357 
1358     ncp = nch->ncp;
1359 again:
1360     if (ncp->nc_flag & NCF_UNRESOLVED) {
1361 	if (nchislocked == 0) {
1362 		cache_lock(nch);
1363 		nchislocked = 2;
1364 	}
1365 	cache_resolve(nch, cred);
1366 	ncp = nch->ncp;
1367     }
1368     error = ncp->nc_error;
1369 
1370     /*
1371      * Directory permissions checks.  Silently ignore ENOENT if these
1372      * tests pass.  It isn't an error.
1373      *
1374      * We can safely resolve ncp->nc_parent because ncp is currently
1375      * locked.
1376      */
1377     if (nflags & (NLC_CREATE | NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST)) {
1378 	if (((nflags & NLC_CREATE) && ncp->nc_vp == NULL) ||
1379 	    ((nflags & NLC_DELETE) && ncp->nc_vp != NULL) ||
1380 	    ((nflags & NLC_RENAME_SRC) && ncp->nc_vp != NULL) ||
1381 	    (nflags & NLC_RENAME_DST)
1382 	) {
1383 	    struct nchandle par;
1384 
1385 	    if (nchislocked == 0) {
1386 		cache_lock_maybe_shared(nch, 0);
1387 		nchislocked = 2;
1388 		if (ncp->nc_flag & NCF_UNRESOLVED)
1389 			goto again;
1390 	    }
1391 	    if ((par.ncp = ncp->nc_parent) == NULL) {
1392 		if (error != EAGAIN)
1393 			error = EINVAL;
1394 	    } else if (error == 0 || error == ENOENT) {
1395 		par.mount = nch->mount;
1396 		cache_hold(&par);
1397 		cache_lock_maybe_shared(&par, 0);
1398 		error = naccess(&par, NLC_WRITE, cred, NULL, 1);
1399 		cache_put(&par);
1400 	    }
1401 	}
1402     }
1403 
1404     /*
1405      * NLC_EXCL check.  Target file must not exist.
1406      */
1407     if (error == 0 && (nflags & NLC_EXCL) && ncp->nc_vp != NULL)
1408 	error = EEXIST;
1409 
1410     /*
1411      * Try to short-cut the vnode operation for intermediate directory
1412      * components.  This is a major SMP win because it avoids having
1413      * to execute a lot of code for intermediate directory components,
1414      * including shared refs and locks on intermediate directory vnodes.
1415      *
1416      * We can only do this if the caller does not need nflagsp.
1417      */
1418     if (error == 0 && nflagsp == NULL &&
1419 	nflags == NLC_EXEC && (ncp->nc_flag & NCF_WXOK)) {
1420 	if (nchislocked == 2)
1421 		cache_unlock(nch);
1422 	return 0;
1423     }
1424 
1425     /*
1426      * Get the vnode attributes so we can do the rest of our checks.
1427      *
1428      * NOTE: We only call naccess_lva() if the target exists.
1429      */
1430     if (error == 0) {
1431 	if (nchislocked == 0) {
1432 	    cache_lock_maybe_shared(nch, 0);
1433 	    nchislocked = 2;
1434 	}
1435 #if 0
1436 	error = cache_vget(nch, cred, LK_SHARED, &vp);
1437 #else
1438 	error = cache_vref(nch, cred, &vp);
1439 #endif
1440 	if (error == ENOENT) {
1441 	    /*
1442 	     * Silently zero-out ENOENT if creating or renaming
1443 	     * (rename target).  It isn't an error.
1444 	     */
1445 	    if (nflags & (NLC_CREATE | NLC_RENAME_DST))
1446 		error = 0;
1447 	} else if (error == 0) {
1448 	    /*
1449 	     * Get the vnode attributes and check for illegal O_TRUNC
1450 	     * requests and read-only mounts.
1451 	     *
1452 	     * NOTE: You can still open devices on read-only mounts for
1453 	     * 	     writing.
1454 	     *
1455 	     * NOTE: creates/deletes/renames are handled by the NLC_WRITE
1456 	     *	     check on the parent directory above.
1457 	     *
1458 	     * XXX cache the va in the namecache or in the vnode
1459 	     */
1460 	    error = VOP_GETATTR_LITE(vp, &lva);
1461 	    if (error == 0 && (nflags & NLC_TRUNCATE)) {
1462 		switch(lva.va_type) {
1463 		case VREG:
1464 		case VDATABASE:
1465 		case VCHR:
1466 		case VBLK:
1467 		case VFIFO:
1468 		    break;
1469 		case VDIR:
1470 		    error = EISDIR;
1471 		    break;
1472 		default:
1473 		    error = EINVAL;
1474 		    break;
1475 		}
1476 	    }
1477 	    if (error == 0 && (nflags & NLC_WRITE) && vp->v_mount &&
1478 		(vp->v_mount->mnt_flag & MNT_RDONLY)
1479 	    ) {
1480 		switch(lva.va_type) {
1481 		case VDIR:
1482 		case VLNK:
1483 		case VREG:
1484 		case VDATABASE:
1485 		    error = EROFS;
1486 		    break;
1487 		default:
1488 		    break;
1489 		}
1490 	    }
1491 #if 0
1492 	    vput(vp);
1493 #else
1494 	    vrele(vp);
1495 #endif
1496 
1497 	    /*
1498 	     * Check permissions based on file attributes.  The passed
1499 	     * flags (*nflagsp) are modified with feedback based on
1500 	     * special attributes and requirements.
1501 	     */
1502 	    if (error == 0) {
1503 		/*
1504 		 * Adjust the returned (*nflagsp) if non-NULL.
1505 		 */
1506 		if (nflagsp) {
1507 		    if ((lva.va_mode & VSVTX) && lva.va_uid != cred->cr_uid)
1508 			*nflagsp |= NLC_STICKY;
1509 		    if (lva.va_flags & APPEND)
1510 			*nflagsp |= NLC_APPENDONLY;
1511 		    if (lva.va_flags & IMMUTABLE)
1512 			*nflagsp |= NLC_IMMUTABLE;
1513 		}
1514 
1515 		/*
1516 		 * NCF_WXOK can be set for world-searchable directories.
1517 		 *
1518 		 * XXX When we implement capabilities this code would also
1519 		 * need a cap check, or only set the flag if there are no
1520 		 * capabilities.
1521 		 */
1522 		cflags = 0;
1523 		if (lva.va_type == VDIR &&
1524 		    (lva.va_mode & S_WXOK_MASK) == S_WXOK_MASK) {
1525 			cflags |= NCF_WXOK;
1526 		}
1527 		if ((lva.va_mode & S_XOK_MASK) == 0)
1528 			cflags |= NCF_NOTX;
1529 
1530 		/*
1531 		 * Track swapcache management flags in the namecache.
1532 		 *
1533 		 * Calculate the flags based on the current vattr_lite info
1534 		 * and recalculate the inherited flags from the parent
1535 		 * (the original cache linkage may have occurred without
1536 		 * getattrs and thus have stale flags).
1537 		 */
1538 		if (lva.va_flags & SF_NOCACHE)
1539 			cflags |= NCF_SF_NOCACHE;
1540 		if (lva.va_flags & UF_CACHE)
1541 			cflags |= NCF_UF_CACHE;
1542 		if (ncp->nc_parent) {
1543 			if (ncp->nc_parent->nc_flag &
1544 			    (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) {
1545 				cflags |= NCF_SF_PNOCACHE;
1546 			}
1547 			if (ncp->nc_parent->nc_flag &
1548 			    (NCF_UF_CACHE | NCF_UF_PCACHE)) {
1549 				cflags |= NCF_UF_PCACHE;
1550 			}
1551 		}
1552 
1553 		/*
1554 		 * We're not supposed to update nc_flag when holding a shared
1555 		 * lock, but we allow the case for certain flags.  Note that
1556 		 * holding an exclusive lock allows updating nc_flag without
1557 		 * atomics.  nc_flag is not allowe to be updated at all unless
1558 		 * a shared or exclusive lock is held.
1559 		 */
1560 		atomic_clear_short(&ncp->nc_flag,
1561 				   (NCF_SF_NOCACHE | NCF_UF_CACHE |
1562 				   NCF_SF_PNOCACHE | NCF_UF_PCACHE |
1563 				   NCF_WXOK | NCF_NOTX) & ~cflags);
1564 		atomic_set_short(&ncp->nc_flag, cflags);
1565 
1566 		/*
1567 		 * Process general access.
1568 		 */
1569 		error = naccess_lva(&lva, nflags, cred);
1570 	    }
1571 	}
1572     }
1573     if (nchislocked == 2)
1574 	cache_unlock(nch);
1575     return(error);
1576 }
1577 
1578 /*
1579  * Check the requested access against the given vattr using cred.
1580  */
1581 int
1582 naccess_lva(struct vattr_lite *lvap, int nflags, struct ucred *cred)
1583 {
1584     int i;
1585     int vmode;
1586 
1587     /*
1588      * Test the immutable bit.  Creations, deletions, renames (source
1589      * or destination) are not allowed.  chown/chmod/other is also not
1590      * allowed but is handled by SETATTR.  Hardlinks to the immutable
1591      * file are allowed.
1592      *
1593      * If the directory is set to immutable then creations, deletions,
1594      * renames (source or dest) and hardlinks to files within the directory
1595      * are not allowed, and regular files opened through the directory may
1596      * not be written to or truncated (unless a special device).
1597      *
1598      * NOTE!  New hardlinks to immutable files work but new hardlinks to
1599      * files, immutable or not, sitting inside an immutable directory are
1600      * not allowed.  As always if the file is hardlinked via some other
1601      * path additional hardlinks may be possible even if the file is marked
1602      * immutable.  The sysop needs to create a closure by checking the hard
1603      * link count.  Once closure is achieved you are good, and security
1604      * scripts should check link counts anyway.
1605      *
1606      * Writes and truncations are only allowed on special devices.
1607      */
1608     if ((lvap->va_flags & IMMUTABLE) || (nflags & NLC_IMMUTABLE)) {
1609 	if ((nflags & NLC_IMMUTABLE) && (nflags & NLC_HLINK))
1610 	    return (EPERM);
1611 	if (nflags & (NLC_CREATE | NLC_DELETE |
1612 		      NLC_RENAME_SRC | NLC_RENAME_DST)) {
1613 	    return (EPERM);
1614 	}
1615 	if (nflags & (NLC_WRITE | NLC_TRUNCATE)) {
1616 	    switch(lvap->va_type) {
1617 	    case VDIR:
1618 		return (EISDIR);
1619 	    case VLNK:
1620 	    case VREG:
1621 	    case VDATABASE:
1622 		return (EPERM);
1623 	    default:
1624 		break;
1625 	    }
1626 	}
1627     }
1628 
1629     /*
1630      * Test the no-unlink and append-only bits for opens, rename targets,
1631      * and deletions.  These bits are not tested for creations or
1632      * rename sources.
1633      *
1634      * Unlike FreeBSD we allow a file with APPEND set to be renamed.
1635      * If you do not wish this you must also set NOUNLINK.
1636      *
1637      * If the governing directory is marked APPEND-only it implies
1638      * NOUNLINK for all entries in the directory.
1639      */
1640     if (((lvap->va_flags & NOUNLINK) || (nflags & NLC_APPENDONLY)) &&
1641 	(nflags & (NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST))
1642     ) {
1643 	return (EPERM);
1644     }
1645 
1646     /*
1647      * A file marked append-only may not be deleted but can be renamed.
1648      */
1649     if ((lvap->va_flags & APPEND) &&
1650 	(nflags & (NLC_DELETE | NLC_RENAME_DST))
1651     ) {
1652 	return (EPERM);
1653     }
1654 
1655     /*
1656      * A file marked append-only which is opened for writing must also
1657      * be opened O_APPEND.
1658      */
1659     if ((lvap->va_flags & APPEND) && (nflags & (NLC_OPEN | NLC_TRUNCATE))) {
1660 	if (nflags & NLC_TRUNCATE)
1661 	    return (EPERM);
1662 	if ((nflags & (NLC_OPEN | NLC_WRITE)) == (NLC_OPEN | NLC_WRITE)) {
1663 	    if ((nflags & NLC_APPEND) == 0)
1664 		return (EPERM);
1665 	}
1666     }
1667 
1668     /*
1669      * root gets universal access
1670      */
1671     if (cred->cr_uid == 0)
1672 	return(0);
1673 
1674     /*
1675      * Check owner perms.
1676      *
1677      * If NLC_OWN is set the owner of the file is allowed no matter when
1678      * the owner-mode bits say (utimes).
1679      */
1680     vmode = 0;
1681     if (nflags & NLC_READ)
1682 	vmode |= S_IRUSR;
1683     if (nflags & NLC_WRITE)
1684 	vmode |= S_IWUSR;
1685     if (nflags & NLC_EXEC)
1686 	vmode |= S_IXUSR;
1687 
1688     if (cred->cr_uid == lvap->va_uid) {
1689 	if ((nflags & NLC_OWN) == 0) {
1690 	    if ((vmode & lvap->va_mode) != vmode)
1691 		return(EACCES);
1692 	}
1693 	return(0);
1694     }
1695 
1696     /*
1697      * If NLC_STICKY is set only the owner may delete or rename a file.
1698      * This bit is typically set on /tmp.
1699      *
1700      * Note that the NLC_READ/WRITE/EXEC bits are not typically set in
1701      * the specific delete or rename case.  For deletions and renames we
1702      * usually just care about directory permissions, not file permissions.
1703      */
1704     if ((nflags & NLC_STICKY) &&
1705 	(nflags & (NLC_RENAME_SRC | NLC_RENAME_DST | NLC_DELETE))) {
1706 	return(EACCES);
1707     }
1708 
1709     /*
1710      * Check group perms
1711      */
1712     vmode >>= 3;
1713     for (i = 0; i < cred->cr_ngroups; ++i) {
1714 	if (lvap->va_gid == cred->cr_groups[i]) {
1715 	    if ((vmode & lvap->va_mode) != vmode)
1716 		return(EACCES);
1717 	    return(0);
1718 	}
1719     }
1720 
1721     /*
1722      * Check world perms
1723      */
1724     vmode >>= 3;
1725     if ((vmode & lvap->va_mode) != vmode)
1726 	return(EACCES);
1727     return(0);
1728 }
1729 
1730 /*
1731  * Long-term (10-second interval) statistics collection
1732  */
1733 static
1734 uint64_t
1735 collect_nlookup_callback(int n)
1736 {
1737 	static uint64_t last_total;
1738 	uint64_t save;
1739 	uint64_t total;
1740 
1741 	total = 0;
1742 	for (n = 0; n < ncpus; ++n) {
1743 		globaldata_t gd = globaldata_find(n);
1744 		struct nchstats *sp;
1745 
1746 		if ((sp = gd->gd_nchstats) != NULL)
1747 			total += sp->ncs_longhits + sp->ncs_longmiss;
1748 	}
1749 	save = total;
1750 	total = total - last_total;
1751 	last_total = save;
1752 
1753 	return total;
1754 }
1755 
1756 static
1757 void
1758 nlookup_collect_init(void *dummy __unused)
1759 {
1760 	kcollect_register(KCOLLECT_NLOOKUP, "nlookup", collect_nlookup_callback,
1761 			  KCOLLECT_SCALE(KCOLLECT_NLOOKUP_FORMAT, 0));
1762 }
1763 SYSINIT(collect_nlookup, SI_SUB_PROP, SI_ORDER_ANY, nlookup_collect_init, 0);
1764