xref: /dragonfly/sys/kern/vfs_nlookup.c (revision 655933d6)
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 		 * POSIX junk
896 		 */
897 		if (nd->nl_flags & NLC_CREATE)
898 			error = EEXIST;
899 		else if (nd->nl_flags & NLC_DELETE)
900 			error = (wasdotordotdot == 1) ? EINVAL : ENOTEMPTY;
901 		else
902 			error = EINVAL;
903 	    }
904 	}
905 
906 	/*
907 	 * Early completion on error.
908 	 */
909 	if (error) {
910 	    if (last_element)
911 		    cache_unlock(&nch);
912 	    cache_drop_and_cache(&nch, nd->nl_elmno);
913 	    break;
914 	}
915 
916 	/*
917 	 * If the element is a symlink and it is either not the last
918 	 * element or it is the last element and we are allowed to
919 	 * follow symlinks, resolve the symlink.
920 	 */
921 	if ((nch.ncp->nc_flag & NCF_ISSYMLINK) &&
922 	    (*ptr || (nd->nl_flags & NLC_FOLLOW))
923 	) {
924 	    if (nd->nl_loopcnt++ >= MAXSYMLINKS) {
925 		error = ELOOP;
926 		if (last_element)
927 			cache_unlock(&nch);
928 		cache_drop_and_cache(&nch, nd->nl_elmno);
929 		break;
930 	    }
931 	    if (last_element == 0)
932 		cache_lock_maybe_shared(&nch, 1);
933 
934 	    error = nreadsymlink(nd, &nch, &nlc);
935 	    cache_put(&nch);
936 	    if (error)
937 		break;
938 
939 	    /*
940 	     * Concatenate trailing path elements onto the returned symlink.
941 	     * Note that if the path component (ptr) is not exhausted, it
942 	     * will being with a '/', so we do not have to add another one.
943 	     *
944 	     * The symlink may not be empty.
945 	     */
946 	    len = strlen(ptr);
947 	    if (nlc.nlc_namelen == 0 || nlc.nlc_namelen + len >= MAXPATHLEN) {
948 		error = nlc.nlc_namelen ? ENAMETOOLONG : ENOENT;
949 		objcache_put(namei_oc, nlc.nlc_nameptr);
950 		break;
951 	    }
952 	    bcopy(ptr, nlc.nlc_nameptr + nlc.nlc_namelen, len + 1);
953 
954 	    if (path_reset) {
955 		if (nd->nl_flags & NLC_HASBUF)
956 		    objcache_put(namei_oc, nd->nl_path);
957 	    } else {
958 		path_reset = nd->nl_path;
959 	    }
960 	    nd->nl_path = nlc.nlc_nameptr;
961 	    nd->nl_flags |= NLC_HASBUF;
962 	    ptr = nd->nl_path;
963 
964 	    /*
965 	     * Go back up to the top to resolve any initial '/'s in the
966 	     * symlink.
967 	     */
968 	    continue;
969 	}
970 
971 	/*
972 	 * If the element is a directory and we are crossing a mount point,
973 	 * Locate the mount.
974 	 */
975 	while ((nch.ncp->nc_flag & NCF_ISMOUNTPT) &&
976 	    (nd->nl_flags & NLC_NOCROSSMOUNT) == 0 &&
977 	    (mp = cache_findmount(&nch)) != NULL
978 	) {
979 	    struct vnode *tdp;
980 	    int vfs_do_busy = 0;
981 
982 	    /*
983 	     * VFS must be busied before the namecache entry is locked,
984 	     * but we don't want to waste time calling vfs_busy() if the
985 	     * mount point is already resolved.
986 	     */
987 again:
988 	    if (last_element)
989 		    cache_unlock(&nch);
990 	    cache_drop_and_cache(&nch, nd->nl_elmno);
991 
992 	    if (vfs_do_busy) {
993 		while (vfs_busy(mp, 0)) {
994 		    if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
995 			kprintf("nlookup: warning umount race avoided\n");
996 			cache_dropmount(mp);
997 			error = EBUSY;
998 			vfs_do_busy = 0;
999 			goto double_break;
1000 		    }
1001 		}
1002 	    }
1003 
1004 	    /*
1005 	     * We don't need to lock the nch unless the entry is unresolved
1006 	     * or this is the last element.
1007 	     */
1008 	    if (last_element)
1009 		cache_get_maybe_shared(&mp->mnt_ncmountpt, &nch,
1010 				       wantsexcllock(nd, 1));
1011 	    else
1012 		cache_copy(&mp->mnt_ncmountpt, &nch);
1013 
1014 	    if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1015 		if (last_element == 0)
1016 		    cache_lock(&nch);
1017 		if (nch.ncp->nc_flag & NCF_UNRESOLVED) {
1018 		    if (vfs_do_busy == 0) {
1019 			vfs_do_busy = 1;
1020 			if (last_element == 0)
1021 			    cache_unlock(&nch);
1022 			goto again;
1023 		    }
1024 		    error = VFS_ROOT(mp, &tdp);
1025 		    vfs_unbusy(mp);
1026 		    vfs_do_busy = 0;
1027 		    if (keeperror(nd, error)) {
1028 			cache_dropmount(mp);
1029 			if (last_element == 0)
1030 			    cache_unlock(&nch);
1031 			break;
1032 		    }
1033 		    if (error == 0) {
1034 			cache_setvp(&nch, tdp);
1035 			vput(tdp);
1036 		    }
1037 		}
1038 		if (last_element == 0)
1039 		    cache_unlock(&nch);
1040 	    }
1041 	    if (vfs_do_busy)
1042 		vfs_unbusy(mp);
1043 	    cache_dropmount(mp);
1044 	}
1045 
1046 	/*
1047 	 * Break out on error
1048 	 */
1049 	if (keeperror(nd, error)) {
1050 	    if (last_element)
1051 		    cache_unlock(&nch);
1052 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1053 double_break:
1054 	    break;
1055 	}
1056 
1057 	/*
1058 	 * Skip any slashes to get to the next element.  If there
1059 	 * are any slashes at all the current element must be a
1060 	 * directory or, in the create case, intended to become a directory.
1061 	 * If it isn't we break without incrementing ptr and fall through
1062 	 * to the failure case below.
1063 	 */
1064 	while (*ptr == '/') {
1065 	    if ((nch.ncp->nc_flag & NCF_ISDIR) == 0 &&
1066 		!(nd->nl_flags & NLC_WILLBEDIR)
1067 	    ) {
1068 		break;
1069 	    }
1070 	    ++ptr;
1071 	}
1072 
1073 	/*
1074 	 * Continuation case: additional elements and the current
1075 	 * element is a directory.
1076 	 */
1077 	if (*ptr && (nch.ncp->nc_flag & NCF_ISDIR)) {
1078 	    cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1079 	    if (last_element)
1080 		    cache_unlock(&nch);
1081 	    /*nchislocked = 0; not needed */
1082 	    KKASSERT((nd->nl_flags & NLC_NCPISLOCKED) == 0);
1083 	    nd->nl_nch = nch;
1084 	    continue;
1085 	}
1086 
1087 	/*
1088 	 * Failure case: additional elements and the current element
1089 	 * is not a directory
1090 	 */
1091 	if (*ptr) {
1092 	    if (last_element)
1093 		    cache_unlock(&nch);
1094 	    cache_drop_and_cache(&nch, nd->nl_elmno);
1095 	    error = ENOTDIR;
1096 	    break;
1097 	}
1098 
1099 	/*
1100 	 * Successful lookup of last element.
1101 	 *
1102 	 * Check permissions if the target exists.  If the target does not
1103 	 * exist directory permissions were already tested in the early
1104 	 * completion code above.
1105 	 *
1106 	 * nd->nl_flags will be adjusted on return with NLC_APPENDONLY
1107 	 * if the file is marked append-only, and NLC_STICKY if the directory
1108 	 * containing the file is sticky.
1109 	 */
1110 	KKASSERT(last_element);
1111 
1112 	if (nch.ncp->nc_vp && (nd->nl_flags & NLC_ALLCHKS)) {
1113 	    error = naccess(&nch, nd->nl_flags | dflags,
1114 			    nd->nl_cred, NULL, 1);
1115 	    if (keeperror(nd, error)) {
1116 		cache_put(&nch);
1117 		break;
1118 	    }
1119 	}
1120 
1121 	/*
1122 	 * Termination: no more elements.
1123 	 *
1124 	 * Check to see if the immediate parent has been destroyed.  This race
1125 	 * can occur because the element lookup must temporarily unlock
1126 	 * the parent.  If so, do a retry.
1127 	 */
1128 	if (nch.ncp->nc_parent &&
1129 	    (nch.ncp->nc_parent->nc_flag & NCF_DESTROYED)) {
1130 		doretry = TRUE;
1131 	}
1132 
1133 	/*
1134 	 * Termination: no more elements.
1135 	 *
1136 	 * If NLC_REFDVP is set acquire a referenced parent dvp.  Typically
1137 	 * used for mkdir/mknod/ncreate/nremove/unlink/rename.
1138 	 *
1139 	 * If a mount-point transition occurs due to ncp being a mount point,
1140 	 * or a null-mount, nl_dvp will be set to NULL and an error code of
1141 	 * 0 will be returned.  A NULL nc_parent is not necessarily the only
1142 	 * indication of a mount-point as null-mounts will also tend to have
1143 	 * a non-null nc_parent.
1144 	 *
1145 	 * nch is locked, standard lock order for the namecache is
1146 	 * child-to-parent so we can safely lock its parent.  We can
1147 	 * just use cache_dvpref().
1148 	 */
1149 	if ((nd->nl_flags & NLC_REFDVP) &&
1150 	    (doretry == FALSE || inretry == TRUE)) {
1151 		if (nch.ncp->nc_parent) {
1152 			error = cache_resolve_dvp(&nch, nd->nl_cred,
1153 						  &nd->nl_dvp);
1154 			if (error) {
1155 				kprintf("Parent directory lost during "
1156 					"nlookup: %s/%s (%08x/%08x)\n",
1157 					nch.ncp->nc_parent->nc_name,
1158 					nch.ncp->nc_name,
1159 					nch.ncp->nc_parent->nc_flag,
1160 					nch.ncp->nc_flag);
1161 				cache_put(&nch);
1162 				error = EINVAL;
1163 				break;
1164 			}
1165 
1166 			/*
1167 			 * Mount-point, nl_dvp should remain NULL, error 0,
1168 			 * caller won't be able to use the results so leave
1169 			 * the ncp referenced but unlocked.
1170 			 */
1171 			if (nd->nl_dvp == NULL) {
1172 				cache_put(&nch);
1173 				break;
1174 			}
1175 
1176 			/*
1177 			 * Good directory, fall through to drop-and-cache
1178 			 * below
1179 			 */
1180 			/* */
1181 		} else {
1182 			/*
1183 			 * Mount-point, nl_dvp should remain NULL, error 0,
1184 			 * caller won't be able to use the results so leave
1185 			 * the ncp referenced but unlocked.
1186 			 */
1187 			error = 0;
1188 			cache_put(&nch);
1189 			break;
1190 		}
1191 	}
1192 
1193 	/*
1194 	 * ncp left with lock+ref on break, set NLC_NCPISLOCKED flag
1195 	 */
1196 	cache_drop_and_cache(&nd->nl_nch, nd->nl_elmno);
1197 	nd->nl_nch = nch;
1198 	nd->nl_flags |= NLC_NCPISLOCKED;
1199 	error = 0;
1200 	break;
1201     }
1202 
1203     /*
1204      * We are done / or possibly retry
1205      */
1206     if (hit)
1207 	++gd->gd_nchstats->ncs_longhits;
1208     else
1209 	++gd->gd_nchstats->ncs_longmiss;
1210 
1211     if (nd->nl_flags & NLC_NCPISLOCKED)
1212 	KKASSERT(cache_lockstatus(&nd->nl_nch) > 0);
1213 
1214     /*
1215      * Reset nd->nl_path if necessary (due to softlinks).  We want to return
1216      * nl_path to its original state before retrying or returning.
1217      */
1218     if (path_reset) {
1219 	if (nd->nl_flags & NLC_HASBUF) {
1220 	    objcache_put(namei_oc, nd->nl_path);
1221 	    nd->nl_flags &= ~NLC_HASBUF;
1222 	}
1223 	nd->nl_path = path_reset;
1224 	nd->nl_flags |= saveflag & NLC_HASBUF;
1225 	path_reset = NULL;
1226     }
1227 
1228     /*
1229      * Retry the whole thing if doretry flag is set, but only once.
1230      *
1231      * autofs(5) may mount another filesystem under its root directory
1232      * while resolving a path.
1233      *
1234      * NFS might return ESTALE
1235      */
1236     if (doretry && !inretry) {
1237 	kprintf("nlookup: errno %d retry %s\n", error, nd->nl_path);
1238 	inretry = TRUE;
1239 
1240 	/*
1241 	 * Clean up nd->nl_nch and reset to base directory
1242 	 */
1243 	if (nd->nl_flags & NLC_NCPISLOCKED) {
1244 		cache_unlock(&nd->nl_nch);
1245 		nd->nl_flags &= ~NLC_NCPISLOCKED;
1246 	}
1247 	cache_drop(&nd->nl_nch);
1248 	cache_copy(nd->nl_basench, &nd->nl_nch);
1249 
1250 	nd->nl_elmno = 0;
1251 	nd->nl_flags |= saveflag;
1252 
1253 	goto nlookup_start;
1254     }
1255 
1256     /*
1257      * NOTE: If NLC_CREATE was set the ncp may represent a negative hit
1258      * (ncp->nc_error will be ENOENT), but we will still return an error
1259      * code of 0.
1260      */
1261     return(error);
1262 }
1263 
1264 /*
1265  * Resolve a mount point's glue ncp.  This ncp connects creates the illusion
1266  * of continuity in the namecache tree by connecting the ncp related to the
1267  * vnode under the mount to the ncp related to the mount's root vnode.
1268  *
1269  * If no error occured a locked, ref'd ncp is stored in *ncpp.
1270  */
1271 int
1272 nlookup_mp(struct mount *mp, struct nchandle *nch)
1273 {
1274     struct vnode *vp;
1275     int error;
1276 
1277     error = 0;
1278     cache_get(&mp->mnt_ncmountpt, nch);
1279     if (nch->ncp->nc_flag & NCF_UNRESOLVED) {
1280 	while (vfs_busy(mp, 0))
1281 	    ;
1282 	error = VFS_ROOT(mp, &vp);
1283 	vfs_unbusy(mp);
1284 	if (error) {
1285 	    cache_put(nch);
1286 	} else {
1287 	    cache_setvp(nch, vp);
1288 	    vput(vp);
1289 	}
1290     }
1291     return(error);
1292 }
1293 
1294 /*
1295  * Read the contents of a symlink, allocate a path buffer out of the
1296  * namei_oc and initialize the supplied nlcomponent with the result.
1297  *
1298  * If an error occurs no buffer will be allocated or returned in the nlc.
1299  */
1300 int
1301 nreadsymlink(struct nlookupdata *nd, struct nchandle *nch,
1302 		struct nlcomponent *nlc)
1303 {
1304     struct vnode *vp;
1305     struct iovec aiov;
1306     struct uio auio;
1307     int linklen;
1308     int error;
1309     char *cp;
1310 
1311     nlc->nlc_nameptr = NULL;
1312     nlc->nlc_namelen = 0;
1313     if (nch->ncp->nc_vp == NULL)
1314 	return(ENOENT);
1315     if ((error = cache_vget(nch, nd->nl_cred, LK_SHARED, &vp)) != 0)
1316 	return(error);
1317     cp = objcache_get(namei_oc, M_WAITOK);
1318     aiov.iov_base = cp;
1319     aiov.iov_len = MAXPATHLEN;
1320     auio.uio_iov = &aiov;
1321     auio.uio_iovcnt = 1;
1322     auio.uio_offset = 0;
1323     auio.uio_rw = UIO_READ;
1324     auio.uio_segflg = UIO_SYSSPACE;
1325     auio.uio_td = nd->nl_td;
1326     auio.uio_resid = MAXPATHLEN - 1;
1327     error = VOP_READLINK(vp, &auio, nd->nl_cred);
1328     if (error)
1329 	goto fail;
1330     linklen = MAXPATHLEN - 1 - auio.uio_resid;
1331     if (varsym_enable) {
1332 	linklen = varsymreplace(cp, linklen, MAXPATHLEN - 1);
1333 	if (linklen < 0) {
1334 	    error = ENAMETOOLONG;
1335 	    goto fail;
1336 	}
1337     }
1338     cp[linklen] = 0;
1339     nlc->nlc_nameptr = cp;
1340     nlc->nlc_namelen = linklen;
1341     vput(vp);
1342     return(0);
1343 fail:
1344     objcache_put(namei_oc, cp);
1345     vput(vp);
1346     return(error);
1347 }
1348 
1349 /*
1350  * Check access [XXX cache vattr!] [XXX quota]
1351  *
1352  * Generally check the NLC_* access bits.   All specified bits must pass
1353  * for this function to return 0.
1354  *
1355  * The file does not have to exist when checking NLC_CREATE or NLC_RENAME_DST
1356  * access, otherwise it must exist.  No error is returned in this case.
1357  *
1358  * The file must not exist if NLC_EXCL is specified.
1359  *
1360  * Directory permissions in general are tested for NLC_CREATE if the file
1361  * does not exist, NLC_DELETE if the file does exist, and NLC_RENAME_DST
1362  * whether the file exists or not.
1363  *
1364  * The directory sticky bit is tested for NLC_DELETE and NLC_RENAME_DST,
1365  * the latter is only tested if the target exists.
1366  *
1367  * The passed ncp must be referenced and locked.  If it is already resolved
1368  * it may be locked shared but otherwise should be locked exclusively.
1369  */
1370 
1371 #define S_WXOK_MASK	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1372 #define S_XOK_MASK	(S_IXUSR|S_IXGRP|S_IXOTH)
1373 
1374 static int
1375 naccess(struct nchandle *nch, int nflags, struct ucred *cred, int *nflagsp,
1376 	int nchislocked)
1377 {
1378     struct vnode *vp;
1379     struct vattr_lite lva;
1380     struct namecache *ncp;
1381     int error;
1382     int cflags;
1383 
1384     KKASSERT(nchislocked == 0 || cache_lockstatus(nch) > 0);
1385 
1386     ncp = nch->ncp;
1387 again:
1388     if (ncp->nc_flag & NCF_UNRESOLVED) {
1389 	if (nchislocked == 0) {
1390 		cache_lock(nch);
1391 		nchislocked = 2;
1392 	}
1393 	cache_resolve(nch, cred);
1394 	ncp = nch->ncp;
1395     }
1396     error = ncp->nc_error;
1397 
1398     /*
1399      * Directory permissions checks.  Silently ignore ENOENT if these
1400      * tests pass.  It isn't an error.
1401      *
1402      * We can safely resolve ncp->nc_parent because ncp is currently
1403      * locked.
1404      */
1405     if (nflags & (NLC_CREATE | NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST)) {
1406 	if (((nflags & NLC_CREATE) && ncp->nc_vp == NULL) ||
1407 	    ((nflags & NLC_DELETE) && ncp->nc_vp != NULL) ||
1408 	    ((nflags & NLC_RENAME_SRC) && ncp->nc_vp != NULL) ||
1409 	    (nflags & NLC_RENAME_DST)
1410 	) {
1411 	    struct nchandle par;
1412 
1413 	    if (nchislocked == 0) {
1414 		cache_lock_maybe_shared(nch, 0);
1415 		nchislocked = 2;
1416 		if (ncp->nc_flag & NCF_UNRESOLVED)
1417 			goto again;
1418 	    }
1419 	    if ((par.ncp = ncp->nc_parent) == NULL) {
1420 		if (error != EAGAIN)
1421 			error = EINVAL;
1422 	    } else if (error == 0 || error == ENOENT) {
1423 		par.mount = nch->mount;
1424 		cache_hold(&par);
1425 		cache_lock_maybe_shared(&par, 0);
1426 		error = naccess(&par, NLC_WRITE, cred, NULL, 1);
1427 		cache_put(&par);
1428 	    }
1429 	}
1430     }
1431 
1432     /*
1433      * NLC_EXCL check.  Target file must not exist.
1434      */
1435     if (error == 0 && (nflags & NLC_EXCL) && ncp->nc_vp != NULL)
1436 	error = EEXIST;
1437 
1438     /*
1439      * Try to short-cut the vnode operation for intermediate directory
1440      * components.  This is a major SMP win because it avoids having
1441      * to execute a lot of code for intermediate directory components,
1442      * including shared refs and locks on intermediate directory vnodes.
1443      *
1444      * We can only do this if the caller does not need nflagsp.
1445      */
1446     if (error == 0 && nflagsp == NULL &&
1447 	nflags == NLC_EXEC && (ncp->nc_flag & NCF_WXOK)) {
1448 	if (nchislocked == 2)
1449 		cache_unlock(nch);
1450 	return 0;
1451     }
1452 
1453     /*
1454      * Get the vnode attributes so we can do the rest of our checks.
1455      *
1456      * NOTE: We only call naccess_lva() if the target exists.
1457      */
1458     if (error == 0) {
1459 	if (nchislocked == 0) {
1460 	    cache_lock_maybe_shared(nch, 0);
1461 	    nchislocked = 2;
1462 	}
1463 #if 0
1464 	error = cache_vget(nch, cred, LK_SHARED, &vp);
1465 #else
1466 	error = cache_vref(nch, cred, &vp);
1467 #endif
1468 	if (error == ENOENT) {
1469 	    /*
1470 	     * Silently zero-out ENOENT if creating or renaming
1471 	     * (rename target).  It isn't an error.
1472 	     */
1473 	    if (nflags & (NLC_CREATE | NLC_RENAME_DST))
1474 		error = 0;
1475 	} else if (error == 0) {
1476 	    /*
1477 	     * Get the vnode attributes and check for illegal O_TRUNC
1478 	     * requests and read-only mounts.
1479 	     *
1480 	     * NOTE: You can still open devices on read-only mounts for
1481 	     * 	     writing.
1482 	     *
1483 	     * NOTE: creates/deletes/renames are handled by the NLC_WRITE
1484 	     *	     check on the parent directory above.
1485 	     *
1486 	     * XXX cache the va in the namecache or in the vnode
1487 	     */
1488 	    error = VOP_GETATTR_LITE(vp, &lva);
1489 	    if (error == 0 && (nflags & NLC_TRUNCATE)) {
1490 		switch(lva.va_type) {
1491 		case VREG:
1492 		case VDATABASE:
1493 		case VCHR:
1494 		case VBLK:
1495 		case VFIFO:
1496 		    break;
1497 		case VDIR:
1498 		    error = EISDIR;
1499 		    break;
1500 		default:
1501 		    error = EINVAL;
1502 		    break;
1503 		}
1504 	    }
1505 	    if (error == 0 && (nflags & NLC_WRITE) && vp->v_mount &&
1506 		(vp->v_mount->mnt_flag & MNT_RDONLY)
1507 	    ) {
1508 		switch(lva.va_type) {
1509 		case VDIR:
1510 		case VLNK:
1511 		case VREG:
1512 		case VDATABASE:
1513 		    error = EROFS;
1514 		    break;
1515 		default:
1516 		    break;
1517 		}
1518 	    }
1519 #if 0
1520 	    vput(vp);
1521 #else
1522 	    vrele(vp);
1523 #endif
1524 
1525 	    /*
1526 	     * Check permissions based on file attributes.  The passed
1527 	     * flags (*nflagsp) are modified with feedback based on
1528 	     * special attributes and requirements.
1529 	     */
1530 	    if (error == 0) {
1531 		/*
1532 		 * Adjust the returned (*nflagsp) if non-NULL.
1533 		 */
1534 		if (nflagsp) {
1535 		    if ((lva.va_mode & VSVTX) && lva.va_uid != cred->cr_uid)
1536 			*nflagsp |= NLC_STICKY;
1537 		    if (lva.va_flags & APPEND)
1538 			*nflagsp |= NLC_APPENDONLY;
1539 		    if (lva.va_flags & IMMUTABLE)
1540 			*nflagsp |= NLC_IMMUTABLE;
1541 		}
1542 
1543 		/*
1544 		 * NCF_WXOK can be set for world-searchable directories.
1545 		 *
1546 		 * XXX When we implement capabilities this code would also
1547 		 * need a cap check, or only set the flag if there are no
1548 		 * capabilities.
1549 		 */
1550 		cflags = 0;
1551 		if (lva.va_type == VDIR &&
1552 		    (lva.va_mode & S_WXOK_MASK) == S_WXOK_MASK) {
1553 			cflags |= NCF_WXOK;
1554 		}
1555 		if ((lva.va_mode & S_XOK_MASK) == 0)
1556 			cflags |= NCF_NOTX;
1557 
1558 		/*
1559 		 * Track swapcache management flags in the namecache.
1560 		 *
1561 		 * Calculate the flags based on the current vattr_lite info
1562 		 * and recalculate the inherited flags from the parent
1563 		 * (the original cache linkage may have occurred without
1564 		 * getattrs and thus have stale flags).
1565 		 */
1566 		if (lva.va_flags & SF_NOCACHE)
1567 			cflags |= NCF_SF_NOCACHE;
1568 		if (lva.va_flags & UF_CACHE)
1569 			cflags |= NCF_UF_CACHE;
1570 		if (ncp->nc_parent) {
1571 			if (ncp->nc_parent->nc_flag &
1572 			    (NCF_SF_NOCACHE | NCF_SF_PNOCACHE)) {
1573 				cflags |= NCF_SF_PNOCACHE;
1574 			}
1575 			if (ncp->nc_parent->nc_flag &
1576 			    (NCF_UF_CACHE | NCF_UF_PCACHE)) {
1577 				cflags |= NCF_UF_PCACHE;
1578 			}
1579 		}
1580 
1581 		/*
1582 		 * We're not supposed to update nc_flag when holding a shared
1583 		 * lock, but we allow the case for certain flags.  Note that
1584 		 * holding an exclusive lock allows updating nc_flag without
1585 		 * atomics.  nc_flag is not allowe to be updated at all unless
1586 		 * a shared or exclusive lock is held.
1587 		 */
1588 		atomic_clear_short(&ncp->nc_flag,
1589 				   (NCF_SF_NOCACHE | NCF_UF_CACHE |
1590 				   NCF_SF_PNOCACHE | NCF_UF_PCACHE |
1591 				   NCF_WXOK | NCF_NOTX) & ~cflags);
1592 		atomic_set_short(&ncp->nc_flag, cflags);
1593 
1594 		/*
1595 		 * Process general access.
1596 		 */
1597 		error = naccess_lva(&lva, nflags, cred);
1598 	    }
1599 	}
1600     }
1601     if (nchislocked == 2)
1602 	cache_unlock(nch);
1603     return(error);
1604 }
1605 
1606 /*
1607  * Check the requested access against the given vattr using cred.
1608  */
1609 int
1610 naccess_lva(struct vattr_lite *lvap, int nflags, struct ucred *cred)
1611 {
1612     int i;
1613     int vmode;
1614 
1615     /*
1616      * Test the immutable bit.  Creations, deletions, renames (source
1617      * or destination) are not allowed.  chown/chmod/other is also not
1618      * allowed but is handled by SETATTR.  Hardlinks to the immutable
1619      * file are allowed.
1620      *
1621      * If the directory is set to immutable then creations, deletions,
1622      * renames (source or dest) and hardlinks to files within the directory
1623      * are not allowed, and regular files opened through the directory may
1624      * not be written to or truncated (unless a special device).
1625      *
1626      * NOTE!  New hardlinks to immutable files work but new hardlinks to
1627      * files, immutable or not, sitting inside an immutable directory are
1628      * not allowed.  As always if the file is hardlinked via some other
1629      * path additional hardlinks may be possible even if the file is marked
1630      * immutable.  The sysop needs to create a closure by checking the hard
1631      * link count.  Once closure is achieved you are good, and security
1632      * scripts should check link counts anyway.
1633      *
1634      * Writes and truncations are only allowed on special devices.
1635      */
1636     if ((lvap->va_flags & IMMUTABLE) || (nflags & NLC_IMMUTABLE)) {
1637 	if ((nflags & NLC_IMMUTABLE) && (nflags & NLC_HLINK))
1638 	    return (EPERM);
1639 	if (nflags & (NLC_CREATE | NLC_DELETE |
1640 		      NLC_RENAME_SRC | NLC_RENAME_DST)) {
1641 	    return (EPERM);
1642 	}
1643 	if (nflags & (NLC_WRITE | NLC_TRUNCATE)) {
1644 	    switch(lvap->va_type) {
1645 	    case VDIR:
1646 		return (EISDIR);
1647 	    case VLNK:
1648 	    case VREG:
1649 	    case VDATABASE:
1650 		return (EPERM);
1651 	    default:
1652 		break;
1653 	    }
1654 	}
1655     }
1656 
1657     /*
1658      * Test the no-unlink and append-only bits for opens, rename targets,
1659      * and deletions.  These bits are not tested for creations or
1660      * rename sources.
1661      *
1662      * Unlike FreeBSD we allow a file with APPEND set to be renamed.
1663      * If you do not wish this you must also set NOUNLINK.
1664      *
1665      * If the governing directory is marked APPEND-only it implies
1666      * NOUNLINK for all entries in the directory.
1667      */
1668     if (((lvap->va_flags & NOUNLINK) || (nflags & NLC_APPENDONLY)) &&
1669 	(nflags & (NLC_DELETE | NLC_RENAME_SRC | NLC_RENAME_DST))
1670     ) {
1671 	return (EPERM);
1672     }
1673 
1674     /*
1675      * A file marked append-only may not be deleted but can be renamed.
1676      */
1677     if ((lvap->va_flags & APPEND) &&
1678 	(nflags & (NLC_DELETE | NLC_RENAME_DST))
1679     ) {
1680 	return (EPERM);
1681     }
1682 
1683     /*
1684      * A file marked append-only which is opened for writing must also
1685      * be opened O_APPEND.
1686      */
1687     if ((lvap->va_flags & APPEND) && (nflags & (NLC_OPEN | NLC_TRUNCATE))) {
1688 	if (nflags & NLC_TRUNCATE)
1689 	    return (EPERM);
1690 	if ((nflags & (NLC_OPEN | NLC_WRITE)) == (NLC_OPEN | NLC_WRITE)) {
1691 	    if ((nflags & NLC_APPEND) == 0)
1692 		return (EPERM);
1693 	}
1694     }
1695 
1696     /*
1697      * root gets universal access
1698      */
1699     if (cred->cr_uid == 0)
1700 	return(0);
1701 
1702     /*
1703      * Check owner perms.
1704      *
1705      * If NLC_OWN is set the owner of the file is allowed no matter when
1706      * the owner-mode bits say (utimes).
1707      */
1708     vmode = 0;
1709     if (nflags & NLC_READ)
1710 	vmode |= S_IRUSR;
1711     if (nflags & NLC_WRITE)
1712 	vmode |= S_IWUSR;
1713     if (nflags & NLC_EXEC)
1714 	vmode |= S_IXUSR;
1715 
1716     if (cred->cr_uid == lvap->va_uid) {
1717 	if ((nflags & NLC_OWN) == 0) {
1718 	    if ((vmode & lvap->va_mode) != vmode)
1719 		return(EACCES);
1720 	}
1721 	return(0);
1722     }
1723 
1724     /*
1725      * If NLC_STICKY is set only the owner may delete or rename a file.
1726      * This bit is typically set on /tmp.
1727      *
1728      * Note that the NLC_READ/WRITE/EXEC bits are not typically set in
1729      * the specific delete or rename case.  For deletions and renames we
1730      * usually just care about directory permissions, not file permissions.
1731      */
1732     if ((nflags & NLC_STICKY) &&
1733 	(nflags & (NLC_RENAME_SRC | NLC_RENAME_DST | NLC_DELETE))) {
1734 	return(EACCES);
1735     }
1736 
1737     /*
1738      * Check group perms
1739      */
1740     vmode >>= 3;
1741     for (i = 0; i < cred->cr_ngroups; ++i) {
1742 	if (lvap->va_gid == cred->cr_groups[i]) {
1743 	    if ((vmode & lvap->va_mode) != vmode)
1744 		return(EACCES);
1745 	    return(0);
1746 	}
1747     }
1748 
1749     /*
1750      * Check world perms
1751      */
1752     vmode >>= 3;
1753     if ((vmode & lvap->va_mode) != vmode)
1754 	return(EACCES);
1755     return(0);
1756 }
1757 
1758 /*
1759  * Long-term (10-second interval) statistics collection
1760  */
1761 static
1762 uint64_t
1763 collect_nlookup_callback(int n)
1764 {
1765 	static uint64_t last_total;
1766 	uint64_t save;
1767 	uint64_t total;
1768 
1769 	total = 0;
1770 	for (n = 0; n < ncpus; ++n) {
1771 		globaldata_t gd = globaldata_find(n);
1772 		struct nchstats *sp;
1773 
1774 		if ((sp = gd->gd_nchstats) != NULL)
1775 			total += sp->ncs_longhits + sp->ncs_longmiss;
1776 	}
1777 	save = total;
1778 	total = total - last_total;
1779 	last_total = save;
1780 
1781 	return total;
1782 }
1783 
1784 static
1785 void
1786 nlookup_collect_init(void *dummy __unused)
1787 {
1788 	kcollect_register(KCOLLECT_NLOOKUP, "nlookup", collect_nlookup_callback,
1789 			  KCOLLECT_SCALE(KCOLLECT_NLOOKUP_FORMAT, 0));
1790 }
1791 SYSINIT(collect_nlookup, SI_SUB_PROP, SI_ORDER_ANY, nlookup_collect_init, 0);
1792