xref: /dragonfly/sys/kern/vfs_nlookup.c (revision faed5989)
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  *
507  * IF THE PATH REPRESENTS A MOUNT POINT CROSSING THEN NLC_REFDVP WILL SET
508  * NL_DVP TO NULL AND RETURN NO ERROR (ERROR == 0), allowing the operation
509  * to return up the stack.  The nch will only be referenced and not locked.
510  * High level code must check this case and do the right thing since,
511  * typically, it means things like 'mkdir' should fail with EEXIST.  For
512  * example 'mkdir /var/cache' where /var/cache is a null-mount from
513  * /build/var.cache, needs to return EEXIST rather than a mount-crossing
514  * failure.
515  *
516  * NOTE: As an optimization we attempt to obtain a shared namecache lock
517  *	 on any intermediate elements.  On success, the returned element
518  *	 is ALWAYS locked exclusively.
519  */
520 int
521 nlookup(struct nlookupdata *nd)
522 {
523     globaldata_t gd = mycpu;
524     struct nlcomponent nlc;
525     struct nchandle nch;
526     struct nchandle nctmp;
527     struct mount *mp;
528     int wasdotordotdot;
529     char *path_reset;
530     char *ptr;
531     char *nptr;
532     int error;
533     int len;
534     int dflags;
535     int hit = 1;
536     int saveflag = nd->nl_flags;
537     boolean_t doretry = FALSE;
538     boolean_t inretry = FALSE;
539 
540     if (nlookup_debug > 0) {
541 	--nlookup_debug;
542 	doretry = 1;
543     }
544     path_reset = NULL;
545 
546 nlookup_start:
547 
548 #ifdef KTRACE
549     if (KTRPOINT(nd->nl_td, KTR_NAMEI))
550 	ktrnamei(nd->nl_td->td_lwp, nd->nl_path);
551 #endif
552     bzero(&nlc, sizeof(nlc));
553 
554     /*
555      * Setup for the loop.  The current working namecache element is
556      * always at least referenced.  We lock it as required, but always
557      * return a locked, resolved namecache entry.
558      */
559     nd->nl_loopcnt = 0;
560     if (nd->nl_dvp) {
561 	vrele(nd->nl_dvp);
562 	nd->nl_dvp = NULL;
563     }
564     ptr = nd->nl_path;
565 
566     /*
567      * Loop on the path components.  At the top of the loop nd->nl_nch
568      * is ref'd and unlocked and represents our current position.
569      */
570     for (;;) {
571 	int last_element;
572 
573 	++nd->nl_elmno;
574 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
575 
576 	/*
577 	 * Check if the root directory should replace the current
578 	 * directory.  This is done at the start of a translation
579 	 * or after a symbolic link has been found.  In other cases
580 	 * ptr will never be pointing at a '/'.
581 	 */
582 	if (*ptr == '/') {
583 	    do {
584 		++ptr;
585 	    } while (*ptr == '/');
586 
587 	    /*
588 	     * We might already be at the root as a pre-optimization
589 	     */
590 	    if (nd->nl_nch.mount != nd->nl_rootnch.mount ||
591 		nd->nl_nch.ncp != nd->nl_rootnch.ncp) {
592 		cache_drop_and_cache(&nd->nl_nch, 0);
593 		cache_copy(&nd->nl_rootnch, &nd->nl_nch);
594 	    }
595 
596 	    /*
597 	     * Fast-track termination.  There is no parent directory of
598 	     * the root in the same mount from the point of view of
599 	     * the caller so return EACCES if NLC_REFDVP is specified,
600 	     * and EEXIST if NLC_CREATE is also specified.
601 	     * e.g. 'rmdir /' or 'mkdir /' are not allowed.
602 	     */
603 	    if (*ptr == 0) {
604 		if (nd->nl_flags & NLC_REFDVP)
605 			error = (nd->nl_flags & NLC_CREATE) ? EEXIST : EACCES;
606 		else
607 			error = 0;
608 		nd->nl_flags |= NLC_NCPISLOCKED;
609 		cache_lock_maybe_shared(&nd->nl_nch,
610 					wantsexcllock(nd, islastelement(ptr)));
611 		break;
612 	    }
613 	    continue;
614 	}
615 
616 	/*
617 	 * Pre-calculate next path component so we can check whether the
618 	 * current component directory is the last directory in the path
619 	 * or not.
620 	 */
621 	for (nptr = ptr; *nptr && *nptr != '/'; ++nptr)
622 		;
623 
624 	/*
625 	 * nd->nl_nch is referenced and not locked here.
626 	 *
627 	 * Check directory search permissions.  This will load dflags to
628 	 * obtain directory-special permissions to be checked along with the
629 	 * last component.
630 	 *
631 	 * We only need to pass-in &dflags for the second-to-last component.
632 	 * Optimize by passing-in NULL for any prior components, which may
633 	 * allow the code to bypass the naccess() call.
634 	 *
635 	 * naccess() is optimized to avoid having to lock the nch or get
636 	 * the related vnode if cached perms are sufficient.
637 	 */
638 	dflags = 0;
639 	if (*nptr == '/' || (saveflag & NLC_MODIFYING_MASK) == 0)
640 	    error = naccess(&nd->nl_nch, NLC_EXEC, nd->nl_cred, NULL, 0);
641 	else
642 	    error = naccess(&nd->nl_nch, NLC_EXEC, nd->nl_cred, &dflags, 0);
643 	if (error) {
644 	    if (keeperror(nd, error))
645 		    break;
646 	    error = 0;
647 	}
648 
649 	/*
650 	 * Extract the next (or last) path component.  Path components are
651 	 * limited to 255 characters.
652 	 */
653 	nlc.nlc_nameptr = ptr;
654 	nlc.nlc_namelen = nptr - ptr;
655 	ptr = nptr;
656 	if (nlc.nlc_namelen >= 256) {
657 	    error = ENAMETOOLONG;
658 	    break;
659 	}
660 	last_element = islastelement(nptr);
661 
662 	/*
663 	 * Lookup the path component in the cache, creating an unresolved
664 	 * entry if necessary.  We have to handle "." and ".." as special
665 	 * cases.
666 	 *
667 	 * When handling ".." we have to detect a traversal back through a
668 	 * mount point.   If we are at the root, ".." just returns the root.
669 	 *
670 	 * When handling "." or ".." we also have to recalculate dflags
671 	 * since our dflags will be for some sub-directory instead of the
672 	 * parent dir.
673 	 *
674 	 * This subsection returns a referenced and possibly locked 'nch'.
675 	 * The locking status is based on the last_element flag.
676 	 *
677 	 * The namecache topology is not allowed to be disconnected, so
678 	 * encountering a NULL parent will generate EINVAL.  This typically
679 	 * occurs when a directory is removed out from under a process.
680 	 *
681 	 * WARNING! The unlocking of nd->nl_nch is sensitive code.
682 	 */
683 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
684 
685 	if (nlc.nlc_namelen == 1 && nlc.nlc_nameptr[0] == '.') {
686 	    if (last_element) {
687 		cache_get_maybe_shared(&nd->nl_nch, &nch,
688 				       wantsexcllock(nd, 1));
689 	    } else {
690 		cache_copy(&nd->nl_nch, &nch);
691 	    }
692 	    wasdotordotdot = 1;
693 	} else if (nlc.nlc_namelen == 2 &&
694 		   nlc.nlc_nameptr[0] == '.' && nlc.nlc_nameptr[1] == '.') {
695 	    if (nd->nl_nch.mount == nd->nl_rootnch.mount &&
696 		nd->nl_nch.ncp == nd->nl_rootnch.ncp
697 	    ) {
698 		/*
699 		 * ".." at the root returns the root
700 		 */
701 		if (last_element) {
702 		    cache_get_maybe_shared(&nd->nl_nch, &nch,
703 					   wantsexcllock(nd, 1));
704 		} else {
705 		    cache_copy(&nd->nl_nch, &nch);
706 		}
707 	    } else {
708 		/*
709 		 * Locate the parent ncp.  If we are at the root of a
710 		 * filesystem mount we have to skip to the mounted-on
711 		 * point in the underlying filesystem.
712 		 *
713 		 * Expect the parent to always be good since the
714 		 * mountpoint doesn't go away.  XXX hack.  cache_get()
715 		 * requires the ncp to already have a ref as a safety.
716 		 *
717 		 * However, a process which has been broken out of a chroot
718 		 * will wind up with a NULL parent if it tries to '..' above
719 		 * the real root, deal with the case.  Note that this does
720 		 * not protect us from a jail breakout, it just stops a panic
721 		 * if the jail-broken process tries to '..' past the real
722 		 * root.
723 		 */
724 		nctmp = nd->nl_nch;
725 		while (nctmp.ncp == nctmp.mount->mnt_ncmountpt.ncp) {
726 			nctmp = nctmp.mount->mnt_ncmounton;
727 			if (nctmp.ncp == NULL)
728 				break;
729 		}
730 		if (nctmp.ncp == NULL) {
731 			if (curthread->td_proc) {
732 				kprintf("vfs_nlookup: '..' traverse broke "
733 					"jail: pid %d (%s)\n",
734 					curthread->td_proc->p_pid,
735 					curthread->td_comm);
736 			}
737 			nctmp = nd->nl_rootnch;
738 		} else {
739 			nctmp.ncp = nctmp.ncp->nc_parent;
740 		}
741 		if (last_element) {
742 		    cache_get_maybe_shared(&nctmp, &nch,
743 					   wantsexcllock(nd, 1));
744 		} else {
745 		    cache_copy(&nctmp, &nch);
746 		}
747 	    }
748 	    wasdotordotdot = 2;
749 	} else {
750 	    /*
751 	     * Quickly lookup the component.  If we can't find it, then
752 	     * slowly lookup and resolve the component.
753 	     */
754 	    if (last_element) {
755 		    error = cache_nlookup_maybe_shared(&nd->nl_nch, &nlc,
756 						       wantsexcllock(nd, 1),
757 						       &nch);
758 	    } else {
759 		    nch = cache_nlookup_nonlocked(&nd->nl_nch, &nlc);
760 		    if (nch.ncp == NULL)
761 			error = EWOULDBLOCK;
762 	    }
763 
764 	    /*
765 	     * At this point the only possible error is EWOULDBLOCK.
766 	     *
767 	     * If no error nch is set and referenced, and then also locked
768 	     * according to last_element.  For EWOULDBLOCK nch is not set.
769 	     * For any other error nch is set and referenced, but not locked.
770 	     *
771 	     * On EWOULDBLOCK the ncp may be unresolved (if not locked it can
772 	     * become unresolved at any time, but we don't care at this time).
773 	     */
774 	    if (error == EWOULDBLOCK) {
775 		nch = cache_nlookup(&nd->nl_nch, &nlc);
776 		if (nch.ncp->nc_flag & NCF_UNRESOLVED)
777 		    hit = 0;
778 		for (;;) {
779 		    error = cache_resolve(&nch, nd->nl_cred);
780 		    if (error != EAGAIN &&
781 			(nch.ncp->nc_flag & NCF_DESTROYED) == 0) {
782 			    if (error == ESTALE) {
783 				if (!inretry)
784 				    error = ENOENT;
785 				doretry = TRUE;
786 			    }
787 			    if (last_element == 0)
788 				    cache_unlock(&nch);
789 			    break;
790 		    }
791 		    kprintf("[diagnostic] nlookup: relookup %*.*s\n",
792 			    nch.ncp->nc_nlen, nch.ncp->nc_nlen,
793 			    nch.ncp->nc_name);
794 		    cache_put(&nch);
795 		    nch = cache_nlookup(&nd->nl_nch, &nlc);
796 		}
797 	    }
798 	    wasdotordotdot = 0;
799 	}
800 
801 	/*
802 	 * If the component is "." or ".." our dflags no longer represents
803 	 * the parent directory and we have to explicitly look it up.
804 	 *
805 	 * Expect the parent to be good since nch is locked.
806 	 *
807 	 * nch will continue to be valid even if an error occurs after this
808 	 * point.
809 	 */
810 	if (wasdotordotdot && error == 0) {
811 	    struct nchandle par;
812 
813 	    dflags = 0;
814 	    if (last_element == 0)
815 		cache_lock_maybe_shared(&nch, wantsexcllock(nd, 0));
816 
817 	    if ((par.ncp = nch.ncp->nc_parent) != NULL) {
818 		par.mount = nch.mount;
819 		cache_hold(&par);
820 		error = naccess(&par, 0, nd->nl_cred, &dflags, 0);
821 		cache_drop_and_cache(&par, nd->nl_elmno - 1);
822 		if (error) {
823 		    if (!keeperror(nd, error))
824 			error = 0;
825 		    if (error == EINVAL) {
826 			kprintf("nlookup (%s): trailing . or .. retry on %s\n",
827 				curthread->td_comm, nd->nl_path);
828 			doretry = TRUE;
829 		    }
830 		}
831 	    }
832 
833 	    if (last_element == 0)
834 		cache_unlock(&nch);
835 	}
836 
837 	/*
838 	 * [end of subsection]
839 	 *
840 	 * nch is referenced and locked according to (last_element).
841 	 * nd->nl_nch is unlocked and referenced.
842 	 */
843 	KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
844 
845 	/*
846 	 * Resolve the namespace if necessary.  The ncp returned by
847 	 * cache_nlookup() is referenced, and also locked according
848 	 * to last_element.
849 	 *
850 	 * XXX neither '.' nor '..' should return EAGAIN since they were
851 	 * previously resolved and thus cannot be newly created ncp's.
852 	 */
853 	if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
854 	    if (last_element == 0)
855 		cache_lock(&nch);
856 	    hit = 0;
857 	    error = cache_resolve(&nch, nd->nl_cred);
858 	    if (error == ESTALE) {
859 		if (!inretry)
860 		    error = ENOENT;
861 		doretry = TRUE;
862 	    }
863 	    if (last_element == 0)
864 		cache_unlock(&nch);
865 	    KKASSERT(error != EAGAIN);
866 	} else {
867 	    error = nch.ncp->nc_error;
868 	}
869 
870 	/*
871 	 * Early completion.  ENOENT is not an error if this is the last
872 	 * component and NLC_CREATE or NLC_RENAME (rename target) was
873 	 * requested.  Note that ncp->nc_error is left as ENOENT in that
874 	 * case, which we check later on.
875 	 *
876 	 * Also handle invalid '.' or '..' components terminating a path
877 	 * for a create/rename/delete.  The standard requires this and pax
878 	 * pretty stupidly depends on it.
879 	 */
880 	if (last_element) {
881 	    if (error == ENOENT &&
882 		(nd->nl_flags & (NLC_CREATE | NLC_RENAME_DST)))
883 	    {
884 		if (nd->nl_flags & NLC_NFS_RDONLY) {
885 			error = EROFS;
886 		} else {
887 			error = naccess(&nch, nd->nl_flags | dflags,
888 					nd->nl_cred, NULL, last_element);
889 		}
890 	    }
891 	    if (error == 0 && wasdotordotdot &&
892 		(nd->nl_flags & (NLC_CREATE | NLC_DELETE |
893 				 NLC_RENAME_SRC | NLC_RENAME_DST)))
894 	    {
895 		/*
896 		 * POSIX junk
897 		 */
898 		if (nd->nl_flags & NLC_CREATE)
899 			error = EEXIST;
900 		else if (nd->nl_flags & NLC_DELETE)
901 			error = (wasdotordotdot == 1) ? EINVAL : ENOTEMPTY;
902 		else
903 			error = EINVAL;
904 	    }
905 	}
906 
907 	/*
908 	 * Early completion on error.
909 	 */
910 	if (error) {
911 	    if (last_element)
912 		    cache_unlock(&nch);
913 	    cache_drop_and_cache(&nch, nd->nl_elmno);
914 	    break;
915 	}
916 
917 	/*
918 	 * If the element is a symlink and it is either not the last
919 	 * element or it is the last element and we are allowed to
920 	 * follow symlinks, resolve the symlink.
921 	 */
922 	if ((nch.ncp->nc_flag & NCF_ISSYMLINK) &&
923 	    (*ptr || (nd->nl_flags & NLC_FOLLOW))
924 	) {
925 	    if (nd->nl_loopcnt++ >= MAXSYMLINKS) {
926 		error = ELOOP;
927 		if (last_element)
928 			cache_unlock(&nch);
929 		cache_drop_and_cache(&nch, nd->nl_elmno);
930 		break;
931 	    }
932 	    if (last_element == 0)
933 		cache_lock_maybe_shared(&nch, 1);
934 
935 	    error = nreadsymlink(nd, &nch, &nlc);
936 	    cache_put(&nch);
937 	    if (error)
938 		break;
939 
940 	    /*
941 	     * Concatenate trailing path elements onto the returned symlink.
942 	     * Note that if the path component (ptr) is not exhausted, it
943 	     * will being with a '/', so we do not have to add another one.
944 	     *
945 	     * The symlink may not be empty.
946 	     */
947 	    len = strlen(ptr);
948 	    if (nlc.nlc_namelen == 0 || nlc.nlc_namelen + len >= MAXPATHLEN) {
949 		error = nlc.nlc_namelen ? ENAMETOOLONG : ENOENT;
950 		objcache_put(namei_oc, nlc.nlc_nameptr);
951 		break;
952 	    }
953 	    bcopy(ptr, nlc.nlc_nameptr + nlc.nlc_namelen, len + 1);
954 
955 	    if (path_reset) {
956 		if (nd->nl_flags & NLC_HASBUF)
957 		    objcache_put(namei_oc, nd->nl_path);
958 	    } else {
959 		path_reset = nd->nl_path;
960 	    }
961 	    nd->nl_path = nlc.nlc_nameptr;
962 	    nd->nl_flags |= NLC_HASBUF;
963 	    ptr = nd->nl_path;
964 
965 	    /*
966 	     * Go back up to the top to resolve any initial '/'s in the
967 	     * symlink.
968 	     */
969 	    continue;
970 	}
971 
972 	/*
973 	 * If the element is a directory and we are crossing a mount point,
974 	 * Locate the mount.
975 	 */
976 	while ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
977 	    (nd->nl_flags & NLC_NOCROSSMOUNT) == 0 &&
978 	    (mp = cache_findmount(&nch)) != NULL
979 	) {
980 	    struct vnode *tdp;
981 	    int vfs_do_busy = 0;
982 
983 	    /*
984 	     * VFS must be busied before the namecache entry is locked,
985 	     * but we don't want to waste time calling vfs_busy() if the
986 	     * mount point is already resolved.
987 	     */
988 again:
989 	    if (last_element)
990 		    cache_unlock(&nch);
991 	    cache_drop_and_cache(&nch, nd->nl_elmno);
992 
993 	    if (vfs_do_busy) {
994 		while (vfs_busy(mp, 0)) {
995 		    if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
996 			kprintf("nlookup: warning umount race avoided\n");
997 			cache_dropmount(mp);
998 			error = EBUSY;
999 			vfs_do_busy = 0;
1000 			goto double_break;
1001 		    }
1002 		}
1003 	    }
1004 
1005 	    /*
1006 	     * We don't need to lock the nch unless the entry is unresolved
1007 	     * or this is the last element.
1008 	     */
1009 	    if (last_element)
1010 		cache_get_maybe_shared(&mp->mnt_ncmountpt, &nch,
1011 				       wantsexcllock(nd, 1));
1012 	    else
1013 		cache_copy(&mp->mnt_ncmountpt, &nch);
1014 
1015 	    if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1016 		if (last_element == 0)
1017 		    cache_lock(&nch);
1018 		if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1019 		    if (vfs_do_busy == 0) {
1020 			vfs_do_busy = 1;
1021 			if (last_element == 0)
1022 			    cache_unlock(&nch);
1023 			goto again;
1024 		    }
1025 		    error = VFS_ROOT(mp, &tdp);
1026 		    vfs_unbusy(mp);
1027 		    vfs_do_busy = 0;
1028 		    if (keeperror(nd, error)) {
1029 			cache_dropmount(mp);
1030 			if (last_element == 0)
1031 			    cache_unlock(&nch);
1032 			break;
1033 		    }
1034 		    if (error == 0) {
1035 			cache_setvp(&nch, tdp);
1036 			vput(tdp);
1037 		    }
1038 		}
1039 		if (last_element == 0)
1040 		    cache_unlock(&nch);
1041 	    }
1042 	    if (vfs_do_busy)
1043 		vfs_unbusy(mp);
1044 	    cache_dropmount(mp);
1045 	}
1046 
1047 	/*
1048 	 * Break out on error
1049 	 */
1050 	if (keeperror(nd, error)) {
1051 	    if (last_element)
1052 		    cache_unlock(&nch);
1053 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1054 double_break:
1055 	    break;
1056 	}
1057 
1058 	/*
1059 	 * Skip any slashes to get to the next element.  If there
1060 	 * are any slashes at all the current element must be a
1061 	 * directory or, in the create case, intended to become a directory.
1062 	 * If it isn't we break without incrementing ptr and fall through
1063 	 * to the failure case below.
1064 	 */
1065 	while (*ptr == '/') {
1066 	    if ((nch.ncp->nc_flag & NCF_ISDIR) == 0 &&
1067 		!(nd->nl_flags & NLC_WILLBEDIR)
1068 	    ) {
1069 		break;
1070 	    }
1071 	    ++ptr;
1072 	}
1073 
1074 	/*
1075 	 * Continuation case: additional elements and the current
1076 	 * element is a directory.
1077 	 */
1078 	if (*ptr && (nch.ncp->nc_flag & NCF_ISDIR)) {
1079 	    cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1080 	    if (last_element)
1081 		    cache_unlock(&nch);
1082 	    /*nchislocked = 0; not needed */
1083 	    KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
1084 	    nd->nl_nch = nch;
1085 	    continue;
1086 	}
1087 
1088 	/*
1089 	 * Failure case: additional elements and the current element
1090 	 * is not a directory
1091 	 */
1092 	if (*ptr) {
1093 	    if (last_element)
1094 		    cache_unlock(&nch);
1095 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1096 	    error = ENOTDIR;
1097 	    break;
1098 	}
1099 
1100 	/*
1101 	 * Successful lookup of last element.
1102 	 *
1103 	 * Check permissions if the target exists.  If the target does not
1104 	 * exist directory permissions were already tested in the early
1105 	 * completion code above.
1106 	 *
1107 	 * nd->nl_flags will be adjusted on return with NLC_APPENDONLY
1108 	 * if the file is marked append-only, and NLC_STICKY if the directory
1109 	 * containing the file is sticky.
1110 	 */
1111 	KKASSERT(last_element);
1112 
1113 	if (nch.ncp->nc_vp && (nd->nl_flags & NLC_ALLCHKS)) {
1114 	    error = naccess(&nch, nd->nl_flags | dflags,
1115 			    nd->nl_cred, NULL, 1);
1116 	    if (keeperror(nd, error)) {
1117 		cache_put(&nch);
1118 		break;
1119 	    }
1120 	}
1121 
1122 	/*
1123 	 * Termination: no more elements.
1124 	 *
1125 	 * Check to see if the immediate parent has been destroyed.  This race
1126 	 * can occur because the element lookup must temporarily unlock
1127 	 * the parent.  If so, do a retry.
1128 	 */
1129 	if (nch.ncp->nc_parent &&
1130 	    (nch.ncp->nc_parent->nc_flag & NCF_DESTROYED)) {
1131 		doretry = TRUE;
1132 	}
1133 
1134 	/*
1135 	 * Termination: no more elements.
1136 	 *
1137 	 * If NLC_REFDVP is set acquire a referenced parent dvp.  Typically
1138 	 * used for mkdir/mknod/ncreate/nremove/unlink/rename.
1139 	 *
1140 	 * If a mount-point transition occurs due to ncp being a mount point,
1141 	 * or a null-mount, nl_dvp will be set to NULL and an error code of
1142 	 * 0 will be returned.  A NULL nc_parent is not necessarily the only
1143 	 * indication of a mount-point as null-mounts will also tend to have
1144 	 * a non-null nc_parent.
1145 	 *
1146 	 * nch is locked, standard lock order for the namecache is
1147 	 * child-to-parent so we can safely lock its parent.  We can
1148 	 * just use cache_dvpref().
1149 	 */
1150 	if ((nd->nl_flags & NLC_REFDVP) &&
1151 	    (doretry == FALSE || inretry == TRUE)) {
1152 		if (nch.ncp->nc_parent) {
1153 			error = cache_resolve_dvp(&nch, nd->nl_cred,
1154 						  &nd->nl_dvp);
1155 			if (error) {
1156 				kprintf("Parent directory lost during "
1157 					"nlookup: %s/%s (%08x/%08x)\n",
1158 					nch.ncp->nc_parent->nc_name,
1159 					nch.ncp->nc_name,
1160 					nch.ncp->nc_parent->nc_flag,
1161 					nch.ncp->nc_flag);
1162 				cache_put(&nch);
1163 				error = EINVAL;
1164 				break;
1165 			}
1166 
1167 			/*
1168 			 * Mount-point, nl_dvp should remain NULL, error 0,
1169 			 * caller won't be able to use the results so leave
1170 			 * the ncp referenced but unlocked.
1171 			 */
1172 			if (nd->nl_dvp == NULL) {
1173 				cache_put(&nch);
1174 				break;
1175 			}
1176 
1177 			/*
1178 			 * Good directory, fall through to drop-and-cache
1179 			 * below
1180 			 */
1181 			/* */
1182 		} else {
1183 			/*
1184 			 * Mount-point, nl_dvp should remain NULL, error 0,
1185 			 * caller won't be able to use the results so leave
1186 			 * the ncp referenced but unlocked.
1187 			 */
1188 			error = 0;
1189 			cache_put(&nch);
1190 			break;
1191 		}
1192 	}
1193 
1194 	/*
1195 	 * ncp left with lock+ref on break, set NLC_NCPISLOCKED flag
1196 	 */
1197 	cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1198 	nd->nl_nch = nch;
1199 	nd->nl_flags |= NLC_NCPISLOCKED;
1200 	error = 0;
1201 	break;
1202     }
1203 
1204     /*
1205      * We are done / or possibly retry
1206      */
1207     if (hit)
1208 	++gd->gd_nchstats->ncs_longhits;
1209     else
1210 	++gd->gd_nchstats->ncs_longmiss;
1211 
1212     if (nd->nl_flags & NLC_NCPISLOCKED)
1213 	KKASSERT(cache_lockstatus(&nd->nl_nch) > 0);
1214 
1215     /*
1216      * Reset nd->nl_path if necessary (due to softlinks).  We want to return
1217      * nl_path to its original state before retrying or returning.
1218      */
1219     if (path_reset) {
1220 	if (nd->nl_flags & NLC_HASBUF) {
1221 	    objcache_put(namei_oc, nd->nl_path);
1222 	    nd->nl_flags &= ~NLC_HASBUF;
1223 	}
1224 	nd->nl_path = path_reset;
1225 	nd->nl_flags |= saveflag & NLC_HASBUF;
1226 	path_reset = NULL;
1227     }
1228 
1229     /*
1230      * Retry the whole thing if doretry flag is set, but only once.
1231      *
1232      * autofs(5) may mount another filesystem under its root directory
1233      * while resolving a path.
1234      *
1235      * NFS might return ESTALE
1236      */
1237     if (doretry && !inretry) {
1238 	kprintf("nlookup: errno %d retry %s\n", error, nd->nl_path);
1239 	inretry = TRUE;
1240 
1241 	/*
1242 	 * Clean up nd->nl_nch and reset to base directory
1243 	 */
1244 	if (nd->nl_flags & NLC_NCPISLOCKED) {
1245 		cache_unlock(&nd->nl_nch);
1246 		nd->nl_flags &= ~NLC_NCPISLOCKED;
1247 	}
1248 	cache_drop(&nd->nl_nch);
1249 	cache_copy(nd->nl_basench, &nd->nl_nch);
1250 
1251 	nd->nl_elmno = 0;
1252 	nd->nl_flags |= saveflag;
1253 
1254 	goto nlookup_start;
1255     }
1256 
1257     /*
1258      * NOTE: If NLC_CREATE was set the ncp may represent a negative hit
1259      * (ncp->nc_error will be ENOENT), but we will still return an error
1260      * code of 0.
1261      */
1262     return(error);
1263 }
1264 
1265 /*
1266  * Resolve a mount point's glue ncp.  This ncp connects creates the illusion
1267  * of continuity in the namecache tree by connecting the ncp related to the
1268  * vnode under the mount to the ncp related to the mount's root vnode.
1269  *
1270  * If no error occured a locked, ref'd ncp is stored in *ncpp.
1271  */
1272 int
1273 nlookup_mp(struct mount *mp, struct nchandle *nch)
1274 {
1275     struct vnode *vp;
1276     int error;
1277 
1278     error = 0;
1279     cache_get(&mp->mnt_ncmountpt, nch);
1280     if (nch->ncp->nc_flag & NCF_UNRESOLVED) {
1281 	while (vfs_busy(mp, 0))
1282 	    ;
1283 	error = VFS_ROOT(mp, &vp);
1284 	vfs_unbusy(mp);
1285 	if (error) {
1286 	    cache_put(nch);
1287 	} else {
1288 	    cache_setvp(nch, vp);
1289 	    vput(vp);
1290 	}
1291     }
1292     return(error);
1293 }
1294 
1295 /*
1296  * Read the contents of a symlink, allocate a path buffer out of the
1297  * namei_oc and initialize the supplied nlcomponent with the result.
1298  *
1299  * If an error occurs no buffer will be allocated or returned in the nlc.
1300  */
1301 int
1302 nreadsymlink(struct nlookupdata *nd, struct nchandle *nch,
1303 		struct nlcomponent *nlc)
1304 {
1305     struct vnode *vp;
1306     struct iovec aiov;
1307     struct uio auio;
1308     int linklen;
1309     int error;
1310     char *cp;
1311 
1312     nlc->nlc_nameptr = NULL;
1313     nlc->nlc_namelen = 0;
1314     if (nch->ncp->nc_vp == NULL)
1315 	return(ENOENT);
1316     if ((error = cache_vget(nch, nd->nl_cred, LK_SHARED, &vp)) != 0)
1317 	return(error);
1318     cp = objcache_get(namei_oc, M_WAITOK);
1319     aiov.iov_base = cp;
1320     aiov.iov_len = MAXPATHLEN;
1321     auio.uio_iov = &aiov;
1322     auio.uio_iovcnt = 1;
1323     auio.uio_offset = 0;
1324     auio.uio_rw = UIO_READ;
1325     auio.uio_segflg = UIO_SYSSPACE;
1326     auio.uio_td = nd->nl_td;
1327     auio.uio_resid = MAXPATHLEN - 1;
1328     error = VOP_READLINK(vp, &auio, nd->nl_cred);
1329     if (error)
1330 	goto fail;
1331     linklen = MAXPATHLEN - 1 - auio.uio_resid;
1332     if (varsym_enable) {
1333 	linklen = varsymreplace(cp, linklen, MAXPATHLEN - 1);
1334 	if (linklen < 0) {
1335 	    error = ENAMETOOLONG;
1336 	    goto fail;
1337 	}
1338     }
1339     cp[linklen] = 0;
1340     nlc->nlc_nameptr = cp;
1341     nlc->nlc_namelen = linklen;
1342     vput(vp);
1343     return(0);
1344 fail:
1345     objcache_put(namei_oc, cp);
1346     vput(vp);
1347     return(error);
1348 }
1349 
1350 /*
1351  * Check access [XXX cache vattr!] [XXX quota]
1352  *
1353  * Generally check the NLC_* access bits.   All specified bits must pass
1354  * for this function to return 0.
1355  *
1356  * The file does not have to exist when checking NLC_CREATE or NLC_RENAME_DST
1357  * access, otherwise it must exist.  No error is returned in this case.
1358  *
1359  * The file must not exist if NLC_EXCL is specified.
1360  *
1361  * Directory permissions in general are tested for NLC_CREATE if the file
1362  * does not exist, NLC_DELETE if the file does exist, and NLC_RENAME_DST
1363  * whether the file exists or not.
1364  *
1365  * The directory sticky bit is tested for NLC_DELETE and NLC_RENAME_DST,
1366  * the latter is only tested if the target exists.
1367  *
1368  * The passed ncp must be referenced and locked.  If it is already resolved
1369  * it may be locked shared but otherwise should be locked exclusively.
1370  */
1371 
1372 #define S_WXOK_MASK	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1373 #define S_XOK_MASK	(S_IXUSR|S_IXGRP|S_IXOTH)
1374 
1375 static int
1376 naccess(struct nchandle *nch, int nflags, struct ucred *cred, int *nflagsp,
1377 	int nchislocked)
1378 {
1379     struct vnode *vp;
1380     struct vattr_lite lva;
1381     struct namecache *ncp;
1382     int error;
1383     int cflags;
1384     int ls = cache_lockstatus(nch);
1385 
1386     /*
1387      * nchislocked: 0 not locked by caller, might not be resolved
1388      *		    1 locked by caller shared or exclusive & resolved
1389      *		    2 locked by us shared or exclusive & resolved
1390      */
1391     KKASSERT(nchislocked == 0 || ls > 0);
1392 
1393     ncp = nch->ncp;
1394 
1395     /*
1396      * If the ncp is exclusively held by another process our ref may
1397      * have raced an eviction and we cannot safely test the fields
1398      * unlocked.
1399      */
1400     if (ls < 0 && nchislocked == 0) {
1401 	cache_lock(nch);
1402 	nchislocked = 2;
1403 	ls = LK_EXCLUSIVE;
1404     }
1405 
1406 again:
1407     /*
1408      * We need a resolved entry.  If the entry is not resolved we need
1409      * to lock and resolve it.  If it is already resolved, our ref should
1410      * prevent normal evictions (as long as we tested the lock race above).
1411      *
1412      * If the ncp was locked by the caller and left unresolved, it must
1413      * have been locked exclusively.
1414      */
1415     if (ncp->nc_flag & NCF_UNRESOLVED) {
1416 	if (nchislocked == 0) {
1417 	    cache_lock(nch);
1418 	    nchislocked = 2;
1419 	    ls = LK_EXCLUSIVE;
1420 	}
1421 	KKASSERT(ls == LK_EXCLUSIVE);
1422 	cache_resolve(nch, cred);
1423 	ncp = nch->ncp;
1424     }
1425     error = ncp->nc_error;
1426 
1427     /*
1428      * Only unresolved entries should return this error (though maybe
1429      * in-filesystem sockets can too).   XXX check filetype for VSOCK.
1430      */
1431     if (error == ENOTCONN) {
1432 	if (nchislocked == 0) {
1433 	    kprintf("ncp %p %08x %d %s: Warning, unexpected state, "
1434 		    "forcing lock\n",
1435 		    ncp, ncp->nc_flag, ncp->nc_error, ncp->nc_name);
1436 	    print_backtrace(-1);
1437 	    cache_lock(nch);
1438 	    nchislocked = 2;
1439 	    ls = LK_EXCLUSIVE;
1440 	    goto again;
1441 	}
1442 	kprintf("ncp %p %08x %d %s: Warning, unexpected state\n",
1443 		ncp, ncp->nc_flag, ncp->nc_error, ncp->nc_name);
1444 	print_backtrace(-1);
1445     }
1446 
1447     /*
1448      * Directory permissions checks.  Silently ignore ENOENT if these
1449      * tests pass.  It isn't an error.
1450      *
1451      * We can safely resolve ncp->nc_parent because ncp is currently
1452      * locked.
1453      */
1454     if (nflags & (NLC_CREATE | NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST)) {
1455 	if (((nflags & NLC_CREATE) && ncp->nc_vp == NULL) ||
1456 	    ((nflags & NLC_DELETE) && ncp->nc_vp != NULL) ||
1457 	    ((nflags & NLC_RENAME_SRC) && ncp->nc_vp != NULL) ||
1458 	    (nflags & NLC_RENAME_DST)
1459 	) {
1460 	    struct nchandle par;
1461 
1462 	    if (nchislocked == 0) {
1463 		cache_lock_maybe_shared(nch, 0);
1464 		nchislocked = 2;
1465 		ls = LK_EXCLUSIVE;
1466 		goto again;
1467 	    }
1468 	    if ((par.ncp = ncp->nc_parent) == NULL) {
1469 		if (error != EAGAIN)
1470 		    error = EINVAL;
1471 	    } else if (error == 0 || error == ENOENT) {
1472 		par.mount = nch->mount;
1473 		cache_hold(&par);
1474 		cache_lock_maybe_shared(&par, 0);
1475 		error = naccess(&par, NLC_WRITE, cred, NULL, 1);
1476 		cache_put(&par);
1477 	    }
1478 	}
1479     }
1480 
1481     /*
1482      * NLC_EXCL check.  Target file must not exist.
1483      */
1484     if (error == 0 && (nflags & NLC_EXCL) && ncp->nc_vp != NULL)
1485 	error = EEXIST;
1486 
1487     /*
1488      * Try to short-cut the vnode operation for intermediate directory
1489      * components.  This is a major SMP win because it avoids having
1490      * to execute a lot of code for intermediate directory components,
1491      * including shared refs and locks on intermediate directory vnodes.
1492      *
1493      * We can only do this if the caller does not need nflagsp.
1494      */
1495     if (error == 0 && nflagsp == NULL &&
1496 	nflags == NLC_EXEC && (ncp->nc_flag & NCF_WXOK)) {
1497 	if (nchislocked == 2)
1498 		cache_unlock(nch);
1499 	return 0;
1500     }
1501 
1502     /*
1503      * Get the vnode attributes so we can do the rest of our checks.
1504      *
1505      * NOTE: We only call naccess_lva() if the target exists.
1506      */
1507     if (error == 0) {
1508 	if (nchislocked == 0) {
1509 	    cache_lock_maybe_shared(nch, 0);
1510 	    nchislocked = 2;
1511 	}
1512 #if 0
1513 	error = cache_vget(nch, cred, LK_SHARED, &vp);
1514 #else
1515 	error = cache_vref(nch, cred, &vp);
1516 #endif
1517 	if (error == ENOENT) {
1518 	    /*
1519 	     * Silently zero-out ENOENT if creating or renaming
1520 	     * (rename target).  It isn't an error.
1521 	     */
1522 	    if (nflags & (NLC_CREATE | NLC_RENAME_DST))
1523 		error = 0;
1524 	} else if (error == 0) {
1525 	    /*
1526 	     * Get the vnode attributes and check for illegal O_TRUNC
1527 	     * requests and read-only mounts.
1528 	     *
1529 	     * NOTE: You can still open devices on read-only mounts for
1530 	     * 	     writing.
1531 	     *
1532 	     * NOTE: creates/deletes/renames are handled by the NLC_WRITE
1533 	     *	     check on the parent directory above.
1534 	     *
1535 	     * XXX cache the va in the namecache or in the vnode
1536 	     */
1537 	    error = VOP_GETATTR_LITE(vp, &lva);
1538 	    if (error == 0 && (nflags & NLC_TRUNCATE)) {
1539 		switch(lva.va_type) {
1540 		case VREG:
1541 		case VDATABASE:
1542 		case VCHR:
1543 		case VBLK:
1544 		case VFIFO:
1545 		    break;
1546 		case VDIR:
1547 		    error = EISDIR;
1548 		    break;
1549 		default:
1550 		    error = EINVAL;
1551 		    break;
1552 		}
1553 	    }
1554 	    if (error == 0 && (nflags & NLC_WRITE) && vp->v_mount &&
1555 		(vp->v_mount->mnt_flag & MNT_RDONLY)
1556 	    ) {
1557 		switch(lva.va_type) {
1558 		case VDIR:
1559 		case VLNK:
1560 		case VREG:
1561 		case VDATABASE:
1562 		    error = EROFS;
1563 		    break;
1564 		default:
1565 		    break;
1566 		}
1567 	    }
1568 #if 0
1569 	    vput(vp);
1570 #else
1571 	    vrele(vp);
1572 #endif
1573 
1574 	    /*
1575 	     * Check permissions based on file attributes.  The passed
1576 	     * flags (*nflagsp) are modified with feedback based on
1577 	     * special attributes and requirements.
1578 	     */
1579 	    if (error == 0) {
1580 		/*
1581 		 * Adjust the returned (*nflagsp) if non-NULL.
1582 		 */
1583 		if (nflagsp) {
1584 		    if ((lva.va_mode & VSVTX) && lva.va_uid != cred->cr_uid)
1585 			*nflagsp |= NLC_STICKY;
1586 		    if (lva.va_flags & APPEND)
1587 			*nflagsp |= NLC_APPENDONLY;
1588 		    if (lva.va_flags & IMMUTABLE)
1589 			*nflagsp |= NLC_IMMUTABLE;
1590 		}
1591 
1592 		/*
1593 		 * NCF_WXOK can be set for world-searchable directories.
1594 		 *
1595 		 * XXX When we implement capabilities this code would also
1596 		 * need a cap check, or only set the flag if there are no
1597 		 * capabilities.
1598 		 */
1599 		cflags = 0;
1600 		if (lva.va_type == VDIR &&
1601 		    (lva.va_mode & S_WXOK_MASK) == S_WXOK_MASK) {
1602 			cflags |= NCF_WXOK;
1603 		}
1604 		if ((lva.va_mode & S_XOK_MASK) == 0)
1605 			cflags |= NCF_NOTX;
1606 
1607 		/*
1608 		 * Track swapcache management flags in the namecache.
1609 		 *
1610 		 * Calculate the flags based on the current vattr_lite info
1611 		 * and recalculate the inherited flags from the parent
1612 		 * (the original cache linkage may have occurred without
1613 		 * getattrs and thus have stale flags).
1614 		 */
1615 		if (lva.va_flags & SF_NOCACHE)
1616 			cflags |= NCF_SF_NOCACHE;
1617 		if (lva.va_flags & UF_CACHE)
1618 			cflags |= NCF_UF_CACHE;
1619 		if (ncp->nc_parent) {
1620 			if (ncp->nc_parent->nc_flag &
1621 			    (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) {
1622 				cflags |= NCF_SF_PNOCACHE;
1623 			}
1624 			if (ncp->nc_parent->nc_flag &
1625 			    (NCF_UF_CACHE | NCF_UF_PCACHE)) {
1626 				cflags |= NCF_UF_PCACHE;
1627 			}
1628 		}
1629 
1630 		/*
1631 		 * We're not supposed to update nc_flag when holding a shared
1632 		 * lock, but we allow the case for certain flags.  Note that
1633 		 * holding an exclusive lock allows updating nc_flag without
1634 		 * atomics.  nc_flag is not allowe to be updated at all unless
1635 		 * a shared or exclusive lock is held.
1636 		 */
1637 		atomic_clear_short(&ncp->nc_flag,
1638 				   (NCF_SF_NOCACHE | NCF_UF_CACHE |
1639 				   NCF_SF_PNOCACHE | NCF_UF_PCACHE |
1640 				   NCF_WXOK | NCF_NOTX) & ~cflags);
1641 		atomic_set_short(&ncp->nc_flag, cflags);
1642 
1643 		/*
1644 		 * Process general access.
1645 		 */
1646 		error = naccess_lva(&lva, nflags, cred);
1647 	    }
1648 	}
1649     }
1650     if (nchislocked == 2)
1651 	cache_unlock(nch);
1652     return(error);
1653 }
1654 
1655 /*
1656  * Check the requested access against the given vattr using cred.
1657  */
1658 int
1659 naccess_lva(struct vattr_lite *lvap, int nflags, struct ucred *cred)
1660 {
1661     int i;
1662     int vmode;
1663 
1664     /*
1665      * Test the immutable bit.  Creations, deletions, renames (source
1666      * or destination) are not allowed.  chown/chmod/other is also not
1667      * allowed but is handled by SETATTR.  Hardlinks to the immutable
1668      * file are allowed.
1669      *
1670      * If the directory is set to immutable then creations, deletions,
1671      * renames (source or dest) and hardlinks to files within the directory
1672      * are not allowed, and regular files opened through the directory may
1673      * not be written to or truncated (unless a special device).
1674      *
1675      * NOTE!  New hardlinks to immutable files work but new hardlinks to
1676      * files, immutable or not, sitting inside an immutable directory are
1677      * not allowed.  As always if the file is hardlinked via some other
1678      * path additional hardlinks may be possible even if the file is marked
1679      * immutable.  The sysop needs to create a closure by checking the hard
1680      * link count.  Once closure is achieved you are good, and security
1681      * scripts should check link counts anyway.
1682      *
1683      * Writes and truncations are only allowed on special devices.
1684      */
1685     if ((lvap->va_flags & IMMUTABLE) || (nflags & NLC_IMMUTABLE)) {
1686 	if ((nflags & NLC_IMMUTABLE) && (nflags & NLC_HLINK))
1687 	    return (EPERM);
1688 	if (nflags & (NLC_CREATE | NLC_DELETE |
1689 		      NLC_RENAME_SRC | NLC_RENAME_DST)) {
1690 	    return (EPERM);
1691 	}
1692 	if (nflags & (NLC_WRITE | NLC_TRUNCATE)) {
1693 	    switch(lvap->va_type) {
1694 	    case VDIR:
1695 		return (EISDIR);
1696 	    case VLNK:
1697 	    case VREG:
1698 	    case VDATABASE:
1699 		return (EPERM);
1700 	    default:
1701 		break;
1702 	    }
1703 	}
1704     }
1705 
1706     /*
1707      * Test the no-unlink and append-only bits for opens, rename targets,
1708      * and deletions.  These bits are not tested for creations or
1709      * rename sources.
1710      *
1711      * Unlike FreeBSD we allow a file with APPEND set to be renamed.
1712      * If you do not wish this you must also set NOUNLINK.
1713      *
1714      * If the governing directory is marked APPEND-only it implies
1715      * NOUNLINK for all entries in the directory.
1716      */
1717     if (((lvap->va_flags & NOUNLINK) || (nflags & NLC_APPENDONLY)) &&
1718 	(nflags & (NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST))
1719     ) {
1720 	return (EPERM);
1721     }
1722 
1723     /*
1724      * A file marked append-only may not be deleted but can be renamed.
1725      */
1726     if ((lvap->va_flags & APPEND) &&
1727 	(nflags & (NLC_DELETE | NLC_RENAME_DST))
1728     ) {
1729 	return (EPERM);
1730     }
1731 
1732     /*
1733      * A file marked append-only which is opened for writing must also
1734      * be opened O_APPEND.
1735      */
1736     if ((lvap->va_flags & APPEND) && (nflags & (NLC_OPEN | NLC_TRUNCATE))) {
1737 	if (nflags & NLC_TRUNCATE)
1738 	    return (EPERM);
1739 	if ((nflags & (NLC_OPEN | NLC_WRITE)) == (NLC_OPEN | NLC_WRITE)) {
1740 	    if ((nflags & NLC_APPEND) == 0)
1741 		return (EPERM);
1742 	}
1743     }
1744 
1745     /*
1746      * root gets universal access
1747      */
1748     if (cred->cr_uid == 0)
1749 	return(0);
1750 
1751     /*
1752      * Check owner perms.
1753      *
1754      * If NLC_OWN is set the owner of the file is allowed no matter when
1755      * the owner-mode bits say (utimes).
1756      */
1757     vmode = 0;
1758     if (nflags & NLC_READ)
1759 	vmode |= S_IRUSR;
1760     if (nflags & NLC_WRITE)
1761 	vmode |= S_IWUSR;
1762     if (nflags & NLC_EXEC)
1763 	vmode |= S_IXUSR;
1764 
1765     if (cred->cr_uid == lvap->va_uid) {
1766 	if ((nflags & NLC_OWN) == 0) {
1767 	    if ((vmode & lvap->va_mode) != vmode)
1768 		return(EACCES);
1769 	}
1770 	return(0);
1771     }
1772 
1773     /*
1774      * If NLC_STICKY is set only the owner may delete or rename a file.
1775      * This bit is typically set on /tmp.
1776      *
1777      * Note that the NLC_READ/WRITE/EXEC bits are not typically set in
1778      * the specific delete or rename case.  For deletions and renames we
1779      * usually just care about directory permissions, not file permissions.
1780      */
1781     if ((nflags & NLC_STICKY) &&
1782 	(nflags & (NLC_RENAME_SRC | NLC_RENAME_DST | NLC_DELETE))) {
1783 	return(EACCES);
1784     }
1785 
1786     /*
1787      * Check group perms
1788      */
1789     vmode >>= 3;
1790     for (i = 0; i < cred->cr_ngroups; ++i) {
1791 	if (lvap->va_gid == cred->cr_groups[i]) {
1792 	    if ((vmode & lvap->va_mode) != vmode)
1793 		return(EACCES);
1794 	    return(0);
1795 	}
1796     }
1797 
1798     /*
1799      * Check world perms
1800      */
1801     vmode >>= 3;
1802     if ((vmode & lvap->va_mode) != vmode)
1803 	return(EACCES);
1804     return(0);
1805 }
1806 
1807 /*
1808  * Long-term (10-second interval) statistics collection
1809  */
1810 static
1811 uint64_t
1812 collect_nlookup_callback(int n)
1813 {
1814 	static uint64_t last_total;
1815 	uint64_t save;
1816 	uint64_t total;
1817 
1818 	total = 0;
1819 	for (n = 0; n < ncpus; ++n) {
1820 		globaldata_t gd = globaldata_find(n);
1821 		struct nchstats *sp;
1822 
1823 		if ((sp = gd->gd_nchstats) != NULL)
1824 			total += sp->ncs_longhits + sp->ncs_longmiss;
1825 	}
1826 	save = total;
1827 	total = total - last_total;
1828 	last_total = save;
1829 
1830 	return total;
1831 }
1832 
1833 static
1834 void
1835 nlookup_collect_init(void *dummy __unused)
1836 {
1837 	kcollect_register(KCOLLECT_NLOOKUP, "nlookup", collect_nlookup_callback,
1838 			  KCOLLECT_SCALE(KCOLLECT_NLOOKUP_FORMAT, 0));
1839 }
1840 SYSINIT(collect_nlookup, SI_SUB_PROP, SI_ORDER_ANY, nlookup_collect_init, 0);
1841