2 * Copyright (c) 2004-2022 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
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.
42 * This greatly improves filesystem parallelism and is a huge simplification
43 * of the API verses the old vnode locking / namei scheme.
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
52 #include "opt_ktrace.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
60 #include <sys/filedesc.h>
62 #include <sys/namei.h>
63 #include <sys/nlookup.h>
64 #include <sys/malloc.h>
66 #include <sys/objcache.h>
68 #include <sys/kcollect.h>
69 #include <sys/sysctl.h>
72 #include <sys/ktrace.h>
75 __read_mostly
static int nlookup_max_retries
= 4;
76 SYSCTL_INT(_debug
, OID_AUTO
, nlookup_max_retries
, CTLFLAG_RW
,
77 &nlookup_max_retries
, 0,
78 "retries on generation mismatch");
79 __read_mostly
static int nlookup_debug
;
80 SYSCTL_INT(_debug
, OID_AUTO
, nlookup_debug
, CTLFLAG_RW
,
84 static int naccess(struct nlookupdata
*nd
, struct nchandle
*nch
,
85 u_int
*genp
, int vmode
,
86 struct ucred
*cred
, int *stickyp
, int nchislocked
);
89 * unmount operations flag NLC_IGNBADDIR in order to allow the
90 * umount to successfully issue a nlookup() on the path in order
91 * to extract the mount point. Allow certain errors through.
95 keeperror(struct nlookupdata
*nd
, int error
)
98 if ((nd
->nl_flags
& NLC_IGNBADDIR
) == 0 ||
99 (error
!= EIO
&& error
!= EBADRPC
&& error
!= ESTALE
)) {
107 * Initialize a nlookup() structure, early error return for copyin faults
108 * or a degenerate empty string (which is not allowed).
110 * The first process proc0's credentials are used if the calling thread
111 * is not associated with a process context.
116 nlookup_init(struct nlookupdata
*nd
,
117 const char *path
, enum uio_seg seg
, int flags
)
128 * note: the pathlen set by copy*str() includes the terminating \0.
130 bzero(nd
, sizeof(struct nlookupdata
));
131 nd
->nl_path
= objcache_get(namei_oc
, M_WAITOK
);
132 nd
->nl_flags
|= NLC_HASBUF
;
133 if (seg
== UIO_SYSSPACE
)
134 error
= copystr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
136 error
= copyinstr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
139 * Don't allow empty pathnames.
140 * POSIX.1 requirement: "" is not a vaild file name.
142 if (error
== 0 && pathlen
<= 1)
147 if (nd
->nl_path
[0] == '/') {
148 if ((flags
& NLC_NLNCH_NOINIT
) == 0) {
149 nd
->nl_basench
= &p
->p_fd
->fd_nrdir
;
150 cache_copy(nd
->nl_basench
, &nd
->nl_nch
);
152 cache_copy(&p
->p_fd
->fd_nrdir
, &nd
->nl_rootnch
);
153 if (p
->p_fd
->fd_njdir
.ncp
)
154 cache_copy(&p
->p_fd
->fd_njdir
, &nd
->nl_jailnch
);
155 nd
->nl_cred
= td
->td_ucred
;
156 nd
->nl_flags
|= NLC_BORROWCRED
;
158 if ((flags
& NLC_NLNCH_NOINIT
) == 0) {
159 nd
->nl_basench
= &p
->p_fd
->fd_ncdir
;
160 cache_copy(nd
->nl_basench
, &nd
->nl_nch
);
162 cache_copy(&p
->p_fd
->fd_nrdir
, &nd
->nl_rootnch
);
163 if (p
->p_fd
->fd_njdir
.ncp
)
164 cache_copy(&p
->p_fd
->fd_njdir
, &nd
->nl_jailnch
);
165 nd
->nl_cred
= td
->td_ucred
;
166 nd
->nl_flags
|= NLC_BORROWCRED
;
169 if ((flags
& NLC_NLNCH_NOINIT
) == 0) {
170 nd
->nl_basench
= &rootnch
;
171 cache_copy(nd
->nl_basench
, &nd
->nl_nch
);
173 cache_copy(&rootnch
, &nd
->nl_rootnch
);
174 cache_copy(&rootnch
, &nd
->nl_jailnch
);
175 nd
->nl_cred
= proc0
.p_ucred
;
176 nd
->nl_flags
|= NLC_BORROWCRED
;
179 nd
->nl_flags
|= flags
& ~NLC_NLNCH_NOINIT
;
188 * nlookup_init() for "at" family of syscalls.
190 * Similar to nlookup_init() but if the path is relative and fd is not
191 * AT_FDCWD, the path will be interpreted relative to the directory pointed
192 * to by fd. In this case, the file entry pointed to by fd is ref'ed and
195 * If the call succeeds, nlookup_done_at() must be called to clean-up the nd
196 * and release the ref to the file entry.
199 nlookup_init_at(struct nlookupdata
*nd
, struct file
**fpp
, int fd
,
200 const char *path
, enum uio_seg seg
, int flags
)
202 struct thread
*td
= curthread
;
210 * Resolve the path, we might have to copy it in from userland,
211 * but don't initialize nl_basench, or nl_nch.
213 error
= nlookup_init(nd
, path
, seg
, flags
| NLC_NLNCH_NOINIT
);
214 if (__predict_false(error
))
218 * Setup nl_basench (a pointer only not refd), and copy+ref
219 * to initialize nl_nch. Only applicable to relative paths.
220 * For absolute paths, or if (fd) is degenerate, just use the
223 if (nd
->nl_path
[0] == '/') {
224 struct proc
*p
= curproc
;
225 nd
->nl_basench
= &p
->p_fd
->fd_nrdir
;
226 } else if (fd
== AT_FDCWD
) {
227 struct proc
*p
= curproc
;
228 nd
->nl_basench
= &p
->p_fd
->fd_ncdir
;
230 if ((error
= holdvnode(td
, fd
, &fp
)) != 0)
232 vp
= (struct vnode
*)fp
->f_data
;
233 if (vp
->v_type
!= VDIR
|| fp
->f_nchandle
.ncp
== NULL
) {
239 nd
->nl_basench
= &fp
->f_nchandle
;
242 cache_copy(nd
->nl_basench
, &nd
->nl_nch
);
250 * This works similarly to nlookup_init() but does not assume a process
251 * context. rootnch is always chosen for the root directory and the cred
252 * and starting directory are supplied in arguments.
255 nlookup_init_raw(struct nlookupdata
*nd
,
256 const char *path
, enum uio_seg seg
, int flags
,
257 struct ucred
*cred
, struct nchandle
*ncstart
)
265 bzero(nd
, sizeof(struct nlookupdata
));
266 nd
->nl_path
= objcache_get(namei_oc
, M_WAITOK
);
267 nd
->nl_flags
|= NLC_HASBUF
;
268 if (seg
== UIO_SYSSPACE
)
269 error
= copystr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
271 error
= copyinstr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
274 * Don't allow empty pathnames.
275 * POSIX.1 requirement: "" is not a vaild file name.
277 if (error
== 0 && pathlen
<= 1)
281 cache_copy(ncstart
, &nd
->nl_nch
);
282 cache_copy(&rootnch
, &nd
->nl_rootnch
);
283 cache_copy(&rootnch
, &nd
->nl_jailnch
);
284 nd
->nl_cred
= crhold(cred
);
286 nd
->nl_flags
|= flags
;
294 * This works similarly to nlookup_init_raw() but does not rely
295 * on rootnch being initialized yet.
298 nlookup_init_root(struct nlookupdata
*nd
,
299 const char *path
, enum uio_seg seg
, int flags
,
300 struct ucred
*cred
, struct nchandle
*ncstart
,
301 struct nchandle
*ncroot
)
309 bzero(nd
, sizeof(struct nlookupdata
));
310 nd
->nl_path
= objcache_get(namei_oc
, M_WAITOK
);
311 nd
->nl_flags
|= NLC_HASBUF
;
312 if (seg
== UIO_SYSSPACE
)
313 error
= copystr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
315 error
= copyinstr(path
, nd
->nl_path
, MAXPATHLEN
, &pathlen
);
318 * Don't allow empty pathnames.
319 * POSIX.1 requirement: "" is not a vaild file name.
321 if (error
== 0 && pathlen
<= 1)
325 cache_copy(ncstart
, &nd
->nl_nch
);
326 cache_copy(ncroot
, &nd
->nl_rootnch
);
327 cache_copy(ncroot
, &nd
->nl_jailnch
);
328 nd
->nl_cred
= crhold(cred
);
330 nd
->nl_flags
|= flags
;
339 * Set a different credential; this credential will be used by future
340 * operations performed on nd.nl_open_vp and nlookupdata structure.
343 nlookup_set_cred(struct nlookupdata
*nd
, struct ucred
*cred
)
345 KKASSERT(nd
->nl_cred
!= NULL
);
347 if (nd
->nl_cred
!= cred
) {
349 if ((nd
->nl_flags
& NLC_BORROWCRED
) == 0)
351 nd
->nl_flags
&= ~NLC_BORROWCRED
;
358 * Cleanup a nlookupdata structure after we are through with it. This may
359 * be called on any nlookupdata structure initialized with nlookup_init().
360 * Calling nlookup_done() is mandatory in all cases except where nlookup_init()
361 * returns an error, even if as a consumer you believe you have taken all
362 * dynamic elements out of the nlookupdata structure.
365 nlookup_done(struct nlookupdata
*nd
)
367 if (nd
->nl_nch
.ncp
) {
368 if (nd
->nl_flags
& NLC_NCPISLOCKED
)
369 cache_unlock(&nd
->nl_nch
);
370 cache_drop_and_cache(&nd
->nl_nch
, nd
->nl_elmno
);
372 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
373 if (nd
->nl_rootnch
.ncp
)
374 cache_drop_and_cache(&nd
->nl_rootnch
, 0);
375 if (nd
->nl_jailnch
.ncp
)
376 cache_drop_and_cache(&nd
->nl_jailnch
, 0);
377 if ((nd
->nl_flags
& NLC_HASBUF
) && nd
->nl_path
) {
378 objcache_put(namei_oc
, nd
->nl_path
);
382 if ((nd
->nl_flags
& NLC_BORROWCRED
) == 0)
385 nd
->nl_flags
&= ~NLC_BORROWCRED
;
387 if (nd
->nl_open_vp
) {
388 if (nd
->nl_flags
& NLC_LOCKVP
) {
389 vn_unlock(nd
->nl_open_vp
);
390 nd
->nl_flags
&= ~NLC_LOCKVP
;
392 vn_close(nd
->nl_open_vp
, nd
->nl_vp_fmode
, NULL
);
393 nd
->nl_open_vp
= NULL
;
399 nd
->nl_flags
= 0; /* clear remaining flags (just clear everything) */
400 nd
->nl_basench
= NULL
;
404 * Works similarly to nlookup_done() when nd initialized with
408 nlookup_done_at(struct nlookupdata
*nd
, struct file
*fp
)
416 nlookup_zero(struct nlookupdata
*nd
)
418 bzero(nd
, sizeof(struct nlookupdata
));
422 * Simple all-in-one nlookup. Returns a locked namecache structure or NULL
423 * if an error occured.
425 * Note that the returned ncp is not checked for permissions, though VEXEC
426 * is checked on the directory path leading up to the result. The caller
427 * must call naccess() to check the permissions of the returned leaf.
430 nlookup_simple(const char *str
, enum uio_seg seg
,
431 int niflags
, int *error
)
433 struct nlookupdata nd
;
436 *error
= nlookup_init(&nd
, str
, seg
, niflags
);
438 if ((*error
= nlookup(&nd
)) == 0) {
439 nch
= nd
.nl_nch
; /* keep hold ref from structure */
440 cache_zero(&nd
.nl_nch
); /* and NULL out */
452 * Returns non-zero if the path element is the last element
456 islastelement(const char *ptr
)
464 * Returns non-zero if we need to lock the namecache element
465 * exclusively. Unless otherwise requested by NLC_SHAREDLOCK,
466 * the last element of the namecache lookup will be locked
469 * O_CREAT or O_TRUNC need the last element to be locked exlcusively.
470 * Intermediate elements are always locked shared.
472 * NOTE: Even if we return on-zero, an unresolved namecache record
473 * will always be locked exclusively.
477 wantsexcllock(struct nlookupdata
*nd
, int last_element
)
479 if ((nd
->nl_flags
& NLC_SHAREDLOCK
) == 0)
480 return(last_element
);
486 * Do a generic nlookup. Note that the passed nd is not nlookup_done()'d
487 * on return, even if an error occurs. If no error occurs or NLC_CREATE
488 * is flagged and ENOENT is returned, then the returned nl_nch is always
489 * referenced and locked exclusively.
491 * WARNING: For any general error other than ENOENT w/NLC_CREATE, the
492 * the resulting nl_nch may or may not be locked and if locked
493 * might be locked either shared or exclusive.
495 * Intermediate directory elements, including the current directory, require
496 * execute (search) permission. nlookup does not examine the access
497 * permissions on the returned element.
499 * If NLC_CREATE is set the last directory must allow node creation,
500 * and an error code of 0 will be returned for a non-existant
501 * target (not ENOENT).
503 * If NLC_RENAME_DST is set the last directory mut allow node deletion,
504 * plus the sticky check is made, and an error code of 0 will be returned
505 * for a non-existant target (not ENOENT).
507 * If NLC_DELETE is set the last directory mut allow node deletion,
508 * plus the sticky check is made.
510 * If NLC_REFDVP is set nd->nl_dvp will be set to the directory vnode
511 * of the returned entry. The vnode will be referenced but not locked.
513 * IF THE PATH REPRESENTS A MOUNT POINT CROSSING THEN NLC_REFDVP WILL SET
514 * NL_DVP TO NULL AND RETURN NO ERROR (ERROR == 0), allowing the operation
515 * to return up the stack. The nch will only be referenced and not locked.
516 * High level code must check this case and do the right thing since,
517 * typically, it means things like 'mkdir' should fail with EEXIST. For
518 * example 'mkdir /var/cache' where /var/cache is a null-mount from
519 * /build/var.cache, needs to return EEXIST rather than a mount-crossing
522 * NOTE: As an optimization we attempt to obtain a shared namecache lock
523 * on any intermediate elements. On success, the returned element
524 * is ALWAYS locked exclusively.
526 * NOTE: If for any reason the nc_generation number of the ncp's being
527 * evaluated changes, the lookup is retried.
530 nlookup(struct nlookupdata
*nd
)
532 globaldata_t gd
= mycpu
;
533 struct nlcomponent nlc
;
535 struct nchandle nctmp
;
545 int saveflag
= nd
->nl_flags
;
546 int max_retries
= nlookup_max_retries
;
550 boolean_t doretry
= FALSE
;
551 boolean_t inretry
= FALSE
;
558 if (KTRPOINT(nd
->nl_td
, KTR_NAMEI
))
559 ktrnamei(nd
->nl_td
->td_lwp
, nd
->nl_path
);
561 bzero(&nlc
, sizeof(nlc
));
564 * Setup for the loop. The current working namecache element is
565 * always at least referenced. We lock it as required, but always
566 * return a locked, resolved namecache entry.
569 nd
->nl_dir_error
= 0;
576 nl_gen
= nd
->nl_nch
.ncp
? nd
->nl_nch
.ncp
->nc_generation
: 0;
581 * Loop on the path components. At the top of the loop nd->nl_nch
582 * is ref'd and unlocked and represents our current position.
588 KKASSERT((nd
->nl_flags
& NLC_NCPISLOCKED
) == 0);
591 * Check if the root directory should replace the current
592 * directory. This is done at the start of a translation
593 * or after a symbolic link has been found. In other cases
594 * ptr will never be pointing at a '/'.
599 } while (*ptr
== '/');
602 * We might already be at the root as a pre-optimization
604 if (nd
->nl_nch
.mount
!= nd
->nl_rootnch
.mount
||
605 nd
->nl_nch
.ncp
!= nd
->nl_rootnch
.ncp
) {
606 cache_drop_and_cache(&nd
->nl_nch
, 0);
607 cache_copy(&nd
->nl_rootnch
, &nd
->nl_nch
);
608 nl_gen
= nd
->nl_nch
.ncp
->nc_generation
& ~3;
612 * Fast-track termination. There is no parent directory of
613 * the root in the same mount from the point of view of
614 * the caller so return EACCES if NLC_REFDVP is specified,
615 * and EEXIST if NLC_CREATE is also specified.
616 * e.g. 'rmdir /' or 'mkdir /' are not allowed.
619 if (nd
->nl_flags
& NLC_REFDVP
)
620 error
= (nd
->nl_flags
& NLC_CREATE
) ? EEXIST
: EACCES
;
623 nd
->nl_flags
|= NLC_NCPISLOCKED
;
624 cache_lock_maybe_shared(&nd
->nl_nch
,
625 wantsexcllock(nd
, islastelement(ptr
)));
632 * Pre-calculate next path component so we can check whether the
633 * current component directory is the last directory in the path
636 for (nptr
= ptr
; *nptr
&& *nptr
!= '/'; ++nptr
)
640 * nd->nl_nch is referenced and not locked here.
642 * Check directory search permissions. This will load dflags to
643 * obtain directory-special permissions to be checked along with the
646 * We only need to pass-in &dflags for the second-to-last component.
647 * Optimize by passing-in NULL for any prior components, which may
648 * allow the code to bypass the naccess() call.
650 * naccess() is optimized to avoid having to lock the nch or get
651 * the related vnode if cached perms are sufficient.
654 if (*nptr
== '/' || (saveflag
& NLC_MODIFYING_MASK
) == 0) {
655 error
= naccess(nd
, &nd
->nl_nch
, &nl_gen
, NLC_EXEC
,
656 nd
->nl_cred
, NULL
, 0);
658 error
= naccess(nd
, &nd
->nl_nch
, &nl_gen
, NLC_EXEC
,
659 nd
->nl_cred
, &dflags
, 0);
662 if (keeperror(nd
, error
))
668 * Extract the next (or last) path component. Path components are
669 * limited to 255 characters.
671 nlc
.nlc_nameptr
= ptr
;
672 nlc
.nlc_namelen
= nptr
- ptr
;
674 if (nlc
.nlc_namelen
>= 256) {
675 error
= ENAMETOOLONG
;
678 last_element
= islastelement(nptr
);
681 * Lookup the path component in the cache, creating an unresolved
682 * entry if necessary. We have to handle "." and ".." as special
685 * When handling ".." we have to detect a traversal back through a
686 * mount point. If we are at the root, ".." just returns the root.
688 * When handling "." or ".." we also have to recalculate dflags
689 * since our dflags will be for some sub-directory instead of the
692 * This subsection returns a referenced and possibly locked 'nch'.
693 * The locking status is based on the last_element flag.
695 * The namecache topology is not allowed to be disconnected, so
696 * encountering a NULL parent will generate EINVAL. This typically
697 * occurs when a directory is removed out from under a process.
699 * WARNING! The unlocking of nd->nl_nch is sensitive code.
701 KKASSERT((nd
->nl_flags
& NLC_NCPISLOCKED
) == 0);
703 if (nlc
.nlc_namelen
== 1 && nlc
.nlc_nameptr
[0] == '.') {
705 cache_get_maybe_shared(&nd
->nl_nch
, &nch
,
706 wantsexcllock(nd
, 1));
708 cache_copy(&nd
->nl_nch
, &nch
);
710 nch_gen
= nch
.ncp
->nc_generation
& ~3;
712 } else if (nlc
.nlc_namelen
== 2 &&
713 nlc
.nlc_nameptr
[0] == '.' && nlc
.nlc_nameptr
[1] == '.') {
714 if (nd
->nl_nch
.mount
== nd
->nl_rootnch
.mount
&&
715 nd
->nl_nch
.ncp
== nd
->nl_rootnch
.ncp
718 * ".." at the root returns the root
721 cache_get_maybe_shared(&nd
->nl_nch
, &nch
,
722 wantsexcllock(nd
, 1));
724 cache_copy(&nd
->nl_nch
, &nch
);
728 * Locate the parent ncp. If we are at the root of a
729 * filesystem mount we have to skip to the mounted-on
730 * point in the underlying filesystem.
732 * Expect the parent to always be good since the
733 * mountpoint doesn't go away. XXX hack. cache_get()
734 * requires the ncp to already have a ref as a safety.
736 * However, a process which has been broken out of a chroot
737 * will wind up with a NULL parent if it tries to '..' above
738 * the real root, deal with the case. Note that this does
739 * not protect us from a jail breakout, it just stops a panic
740 * if the jail-broken process tries to '..' past the real
744 while (nctmp
.ncp
== nctmp
.mount
->mnt_ncmountpt
.ncp
) {
745 nctmp
= nctmp
.mount
->mnt_ncmounton
;
746 if (nctmp
.ncp
== NULL
)
749 if (nctmp
.ncp
== NULL
) {
750 if (curthread
->td_proc
) {
751 kprintf("vfs_nlookup: '..' traverse broke "
752 "jail: pid %d (%s)\n",
753 curthread
->td_proc
->p_pid
,
756 nctmp
= nd
->nl_rootnch
;
758 nctmp
.ncp
= nctmp
.ncp
->nc_parent
;
761 cache_get_maybe_shared(&nctmp
, &nch
,
762 wantsexcllock(nd
, 1));
764 cache_copy(&nctmp
, &nch
);
767 nch_gen
= nch
.ncp
->nc_generation
& ~3;
771 * Quickly lookup the component. If we can't find it, then
772 * slowly lookup and resolve the component.
775 error
= cache_nlookup_maybe_shared(&nd
->nl_nch
, &nlc
,
776 wantsexcllock(nd
, 1),
779 nch
= cache_nlookup_nonlocked(&nd
->nl_nch
, &nlc
);
785 * At this point the only possible error is EWOULDBLOCK.
787 * If no error nch is set and referenced, and then also locked
788 * according to last_element. For EWOULDBLOCK nch is not set.
789 * For any other error nch is set and referenced, but not locked.
791 * On EWOULDBLOCK the ncp may be unresolved (if not locked it can
792 * become unresolved at any time, but we don't care at this time).
794 if (error
== EWOULDBLOCK
) {
795 nch
= cache_nlookup(&nd
->nl_nch
, &nlc
);
796 if (nch
.ncp
->nc_flag
& NCF_UNRESOLVED
)
799 error
= cache_resolve(&nch
, &nch_gen
, nd
->nl_cred
);
800 if (error
!= EAGAIN
&&
801 (nch
.ncp
->nc_flag
& NCF_DESTROYED
) == 0) {
802 if (error
== ESTALE
) {
807 if (last_element
== 0)
811 kprintf("[diagnostic] nlookup: relookup %*.*s\n",
812 nch
.ncp
->nc_nlen
, nch
.ncp
->nc_nlen
,
815 nch
= cache_nlookup(&nd
->nl_nch
, &nlc
);
818 nch_gen
= nch
.ncp
->nc_generation
& ~3;
823 * If the component is "." or ".." our dflags no longer represents
824 * the parent directory and we have to explicitly look it up.
826 * Expect the parent to be good since nch is locked.
828 * nch will continue to be valid even if an error occurs after this
831 if (wasdotordotdot
&& error
== 0) {
835 if (last_element
== 0)
836 cache_lock_maybe_shared(&nch
, wantsexcllock(nd
, 0));
838 if ((par
.ncp
= nch
.ncp
->nc_parent
) != NULL
) {
841 par
.mount
= nch
.mount
;
843 error
= naccess(nd
, &par
, &dummy_gen
, 0, nd
->nl_cred
, &dflags
, 0);
844 cache_drop_and_cache(&par
, nd
->nl_elmno
- 1);
846 if (!keeperror(nd
, error
))
848 if (error
== EINVAL
) {
849 kprintf("nlookup (%s): trailing . or .. retry on %s\n",
850 curthread
->td_comm
, nd
->nl_path
);
856 if (last_element
== 0)
861 * [end of subsection]
863 * nch is referenced and locked according to (last_element).
864 * nd->nl_nch is unlocked and referenced.
865 * nl_gen and nch_gen are both set.
867 KKASSERT((nd
->nl_flags
& NLC_NCPISLOCKED
) == 0);
870 * Resolve the namespace if necessary. The ncp returned by
871 * cache_nlookup() is referenced, and also locked according
874 * XXX neither '.' nor '..' should return EAGAIN since they were
875 * previously resolved and thus cannot be newly created ncp's.
877 if (nch
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
878 if (last_element
== 0)
881 error
= cache_resolve(&nch
, &nch_gen
, nd
->nl_cred
);
882 if (error
== ESTALE
) {
887 if (last_element
== 0)
889 KKASSERT(error
!= EAGAIN
);
891 error
= nch
.ncp
->nc_error
;
895 * Early completion. ENOENT is not an error if this is the last
896 * component and NLC_CREATE or NLC_RENAME (rename target) was
897 * requested. Note that ncp->nc_error is left as ENOENT in that
898 * case, which we check later on.
900 * Also handle invalid '.' or '..' components terminating a path
901 * for a create/rename/delete. The standard requires this and pax
902 * pretty stupidly depends on it.
905 if (error
== ENOENT
&&
906 (nd
->nl_flags
& (NLC_CREATE
| NLC_RENAME_DST
)))
908 if (nd
->nl_flags
& NLC_NFS_RDONLY
) {
911 error
= naccess(nd
, &nch
, &nch_gen
,
912 nd
->nl_flags
| dflags
,
913 nd
->nl_cred
, NULL
, last_element
);
916 if (error
== 0 && wasdotordotdot
&&
917 (nd
->nl_flags
& (NLC_CREATE
| NLC_DELETE
|
918 NLC_RENAME_SRC
| NLC_RENAME_DST
)))
923 if (nd
->nl_flags
& NLC_CREATE
)
925 else if (nd
->nl_flags
& NLC_DELETE
)
926 error
= (wasdotordotdot
== 1) ? EINVAL
: ENOTEMPTY
;
933 * Early completion on error.
938 cache_drop_and_cache(&nch
, nd
->nl_elmno
);
943 * If the element is a symlink and it is either not the last
944 * element or it is the last element and we are allowed to
945 * follow symlinks, resolve the symlink.
947 if ((nch
.ncp
->nc_flag
& NCF_ISSYMLINK
) &&
948 (*ptr
|| (nd
->nl_flags
& NLC_FOLLOW
))
950 if (nd
->nl_loopcnt
++ >= MAXSYMLINKS
) {
954 cache_drop_and_cache(&nch
, nd
->nl_elmno
);
959 * Check for a generation change.
961 * NOTE: On generation changes we must at a minimum cycle
962 * the lock. Here we get or have the lock so we are
965 if (last_element
== 0)
966 cache_lock_maybe_shared(&nch
, 1);
968 if ((nch
.ncp
->nc_generation
- nch_gen
) & ~1) {
969 if (nlookup_debug
& 1) {
970 kprintf("nlookup: symlink: GEN CHANGE %d\n",
971 (nch
.ncp
->nc_generation
- nch_gen
));
976 error
= nreadsymlink(nd
, &nch
, &nlc
);
982 * Concatenate trailing path elements onto the returned symlink.
983 * Note that if the path component (ptr) is not exhausted, it
984 * will being with a '/', so we do not have to add another one.
986 * The symlink may not be empty.
989 if (nlc
.nlc_namelen
== 0 || nlc
.nlc_namelen
+ len
>= MAXPATHLEN
) {
990 error
= nlc
.nlc_namelen
? ENAMETOOLONG
: ENOENT
;
991 objcache_put(namei_oc
, nlc
.nlc_nameptr
);
994 bcopy(ptr
, nlc
.nlc_nameptr
+ nlc
.nlc_namelen
, len
+ 1);
997 if (nd
->nl_flags
& NLC_HASBUF
)
998 objcache_put(namei_oc
, nd
->nl_path
);
1000 path_reset
= nd
->nl_path
;
1002 nd
->nl_path
= nlc
.nlc_nameptr
;
1003 nd
->nl_flags
|= NLC_HASBUF
;
1005 /* nl_gen has not changed */
1008 * Go back up to the top to resolve any initial '/'s in the
1015 * If the element is a directory and we are crossing a mount point,
1018 while ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1019 (nd
->nl_flags
& NLC_NOCROSSMOUNT
) == 0 &&
1020 (mp
= cache_findmount(&nch
)) != NULL
1023 int vfs_do_busy
= 0;
1026 * VFS must be busied before the namecache entry is locked,
1027 * but we don't want to waste time calling vfs_busy() if the
1028 * mount point is already resolved.
1032 * Check for a generation change.
1034 * NOTE: On generation changes we must at a minimum cycle
1035 * the lock. So get and release the lock if we
1038 if ((nch
.ncp
->nc_generation
- nch_gen
) & ~1) {
1039 if (last_element
== 0) {
1040 cache_lock_maybe_shared(&nch
, 1);
1043 if (nlookup_debug
& 1) {
1044 kprintf("nlookup: mountpt: GEN CHANGE %d\n",
1045 (nch
.ncp
->nc_generation
- nch_gen
));
1051 cache_drop_and_cache(&nch
, nd
->nl_elmno
);
1054 while (vfs_busy(mp
, 0)) {
1055 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
) {
1056 kprintf("nlookup: warning umount race avoided\n");
1057 cache_dropmount(mp
);
1066 * We don't need to lock the nch unless the entry is unresolved
1067 * or this is the last element.
1070 cache_get_maybe_shared(&mp
->mnt_ncmountpt
, &nch
,
1071 wantsexcllock(nd
, 1));
1073 cache_copy(&mp
->mnt_ncmountpt
, &nch
);
1074 nch_gen
= nch
.ncp
->nc_generation
& ~3;
1076 if (nch
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
1077 if (last_element
== 0)
1079 if (nch
.ncp
->nc_flag
& NCF_UNRESOLVED
) {
1080 if (vfs_do_busy
== 0) {
1082 if (last_element
== 0)
1086 error
= VFS_ROOT(mp
, &tdp
);
1089 if (keeperror(nd
, error
)) {
1090 cache_dropmount(mp
);
1091 if (last_element
== 0)
1096 cache_setvp(&nch
, tdp
);
1097 nch_gen
= nch
.ncp
->nc_generation
& ~3;
1101 if (last_element
== 0)
1106 cache_dropmount(mp
);
1110 * Break out on error
1112 if (keeperror(nd
, error
)) {
1115 cache_drop_and_cache(&nch
, nd
->nl_elmno
);
1121 * Skip any slashes to get to the next element. If there
1122 * are any slashes at all the current element must be a
1123 * directory or, in the create case, intended to become a directory.
1124 * If it isn't we break without incrementing ptr and fall through
1125 * to the failure case below.
1127 while (*ptr
== '/') {
1128 if ((nch
.ncp
->nc_flag
& NCF_ISDIR
) == 0 &&
1129 !(nd
->nl_flags
& NLC_WILLBEDIR
)
1137 * Continuation case: additional elements and the current
1138 * element is a directory.
1140 if (*ptr
&& (nch
.ncp
->nc_flag
& NCF_ISDIR
)) {
1142 * Check for a generation change.
1144 * NOTE: On generation changes we must at a minimum cycle
1145 * the lock. So get and release the lock if we
1148 if ((nch
.ncp
->nc_generation
- nch_gen
) & ~1) {
1149 if (last_element
== 0) {
1150 cache_lock_maybe_shared(&nch
, 1);
1153 if (nlookup_debug
& 1) {
1154 kprintf("nlookup: next: GEN CHANGE %d\n",
1155 (nch
.ncp
->nc_generation
- nch_gen
));
1159 cache_drop_and_cache(&nd
->nl_nch
, nd
->nl_elmno
);
1162 /*nchislocked = 0; not needed */
1163 KKASSERT((nd
->nl_flags
& NLC_NCPISLOCKED
) == 0);
1170 * Check for a generation change.
1172 * NOTE: On generation changes we must at a minimum cycle
1173 * the lock. So get and release the lock if we
1176 if ((nch
.ncp
->nc_generation
- nch_gen
) & ~1) {
1177 if (nlookup_debug
& 1) {
1178 if (last_element
== 0) {
1179 cache_lock_maybe_shared(&nch
, 1);
1182 kprintf("nlookup: final: GEN CHANGE %d\n",
1183 (nch
.ncp
->nc_generation
- nch_gen
));
1189 * Failure case: additional elements and the current element
1190 * is not a directory
1195 cache_drop_and_cache(&nch
, nd
->nl_elmno
);
1201 * Successful lookup of last element.
1203 * Check permissions if the target exists. If the target does not
1204 * exist directory permissions were already tested in the early
1205 * completion code above.
1207 * nd->nl_flags will be adjusted on return with NLC_APPENDONLY
1208 * if the file is marked append-only, and NLC_STICKY if the directory
1209 * containing the file is sticky.
1211 KKASSERT(last_element
);
1213 if (nch
.ncp
->nc_vp
&& (nd
->nl_flags
& NLC_ALLCHKS
)) {
1214 error
= naccess(nd
, &nch
, &nch_gen
, nd
->nl_flags
| dflags
,
1215 nd
->nl_cred
, NULL
, 1);
1216 if (keeperror(nd
, error
)) {
1223 * Termination: no more elements.
1225 * Check to see if the immediate parent has been destroyed. This race
1226 * can occur because the element lookup must temporarily unlock
1227 * the parent. If so, do a retry.
1229 if (nch
.ncp
->nc_parent
&&
1230 (nch
.ncp
->nc_parent
->nc_flag
& NCF_DESTROYED
)) {
1235 * Termination: no more elements.
1237 * If NLC_REFDVP is set acquire a referenced parent dvp. Typically
1238 * used for mkdir/mknod/ncreate/nremove/unlink/rename.
1240 * If a mount-point transition occurs due to ncp being a mount point,
1241 * or a null-mount, nl_dvp will be set to NULL and an error code of
1242 * 0 will be returned. A NULL nc_parent is not necessarily the only
1243 * indication of a mount-point as null-mounts will also tend to have
1244 * a non-null nc_parent.
1246 * nch is locked, standard lock order for the namecache is
1247 * child-to-parent so we can safely lock its parent. We can
1248 * just use cache_dvpref().
1250 if ((nd
->nl_flags
& NLC_REFDVP
) &&
1251 (doretry
== FALSE
|| inretry
== TRUE
)) {
1252 if (nch
.ncp
->nc_parent
) {
1253 error
= cache_resolve_dvp(&nch
, nd
->nl_cred
,
1256 if (nlookup_debug
& 1) {
1257 kprintf("Parent directory lost during "
1258 "nlookup: %s/%s (%08x/%08x)\n",
1259 nch
.ncp
->nc_parent
->nc_name
,
1261 nch
.ncp
->nc_parent
->nc_flag
,
1270 * Mount-point, nl_dvp should remain NULL, error 0,
1271 * caller won't be able to use the results so leave
1272 * the ncp referenced but unlocked.
1274 if (nd
->nl_dvp
== NULL
) {
1280 * Good directory, fall through to drop-and-cache
1286 * Mount-point, nl_dvp should remain NULL, error 0,
1287 * caller won't be able to use the results so leave
1288 * the ncp referenced but unlocked.
1297 * ncp left with lock+ref on break, set NLC_NCPISLOCKED flag
1299 cache_drop_and_cache(&nd
->nl_nch
, nd
->nl_elmno
);
1301 nd
->nl_flags
|= NLC_NCPISLOCKED
;
1308 * Force a retry (up to max_retries) if nl_gen is incorrect
1310 * NOTE: On generation changes we must at a minimum cycle
1311 * the lock. In this case we have one so we are ok.
1313 if (nd
->nl_nch
.ncp
&& (nd
->nl_nch
.ncp
->nc_generation
- nl_gen
) & ~1) {
1314 if (nlookup_debug
& 1) {
1315 kprintf("nlookup: DONE error %d: GEN CHANGE ON \"%s\" "
1316 "%d (retries %d)\n",
1318 nd
->nl_nch
.ncp
->nc_name
,
1319 (nd
->nl_nch
.ncp
->nc_generation
- nl_gen
),
1335 * We are done / or possibly retry
1338 ++gd
->gd_nchstats
->ncs_longhits
;
1340 ++gd
->gd_nchstats
->ncs_longmiss
;
1342 if (nd
->nl_flags
& NLC_NCPISLOCKED
)
1343 KKASSERT(cache_lockstatus(&nd
->nl_nch
) > 0);
1346 * Reset nd->nl_path if necessary (due to softlinks). We want to return
1347 * nl_path to its original state before retrying or returning.
1350 if (nd
->nl_flags
& NLC_HASBUF
) {
1351 objcache_put(namei_oc
, nd
->nl_path
);
1352 nd
->nl_flags
&= ~NLC_HASBUF
;
1354 nd
->nl_path
= path_reset
;
1355 nd
->nl_flags
|= saveflag
& NLC_HASBUF
;
1360 * Retry the whole thing if doretry flag is set, but only once.
1362 * autofs(5) may mount another filesystem under its root directory
1363 * while resolving a path.
1365 * NFS might return ESTALE
1367 if (doretry
&& !inretry
) {
1368 if (nlookup_debug
& 2)
1369 kprintf("nlookup: errno %d retry %s\n", error
, nd
->nl_path
);
1373 * Clean up nd->nl_nch and reset to base directory
1375 if (nd
->nl_flags
& NLC_NCPISLOCKED
) {
1376 cache_unlock(&nd
->nl_nch
);
1377 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
1379 cache_drop(&nd
->nl_nch
);
1380 cache_copy(nd
->nl_basench
, &nd
->nl_nch
);
1383 nd
->nl_flags
|= saveflag
;
1389 * NOTE: If NLC_CREATE was set the ncp may represent a negative hit
1390 * (ncp->nc_error will be ENOENT), but we will still return an error
1397 * Resolve a mount point's glue ncp. This ncp connects creates the illusion
1398 * of continuity in the namecache tree by connecting the ncp related to the
1399 * vnode under the mount to the ncp related to the mount's root vnode.
1401 * If no error occured a locked, ref'd ncp is stored in *ncpp.
1404 nlookup_mp(struct mount
*mp
, struct nchandle
*nch
)
1410 cache_get(&mp
->mnt_ncmountpt
, nch
);
1411 if (nch
->ncp
->nc_flag
& NCF_UNRESOLVED
) {
1412 while (vfs_busy(mp
, 0))
1414 error
= VFS_ROOT(mp
, &vp
);
1419 cache_setvp(nch
, vp
);
1427 * Read the contents of a symlink, allocate a path buffer out of the
1428 * namei_oc and initialize the supplied nlcomponent with the result.
1430 * If an error occurs no buffer will be allocated or returned in the nlc.
1433 nreadsymlink(struct nlookupdata
*nd
, struct nchandle
*nch
,
1434 struct nlcomponent
*nlc
)
1443 nlc
->nlc_nameptr
= NULL
;
1444 nlc
->nlc_namelen
= 0;
1445 if (nch
->ncp
->nc_vp
== NULL
)
1447 if ((error
= cache_vget(nch
, nd
->nl_cred
, LK_SHARED
, &vp
)) != 0)
1449 cp
= objcache_get(namei_oc
, M_WAITOK
);
1451 aiov
.iov_len
= MAXPATHLEN
;
1452 auio
.uio_iov
= &aiov
;
1453 auio
.uio_iovcnt
= 1;
1454 auio
.uio_offset
= 0;
1455 auio
.uio_rw
= UIO_READ
;
1456 auio
.uio_segflg
= UIO_SYSSPACE
;
1457 auio
.uio_td
= nd
->nl_td
;
1458 auio
.uio_resid
= MAXPATHLEN
- 1;
1459 error
= VOP_READLINK(vp
, &auio
, nd
->nl_cred
);
1462 linklen
= MAXPATHLEN
- 1 - auio
.uio_resid
;
1463 if (varsym_enable
) {
1464 linklen
= varsymreplace(cp
, linklen
, MAXPATHLEN
- 1);
1466 error
= ENAMETOOLONG
;
1471 nlc
->nlc_nameptr
= cp
;
1472 nlc
->nlc_namelen
= linklen
;
1476 objcache_put(namei_oc
, cp
);
1482 * Check access [XXX cache vattr!] [XXX quota]
1484 * Generally check the NLC_* access bits. All specified bits must pass
1485 * for this function to return 0.
1487 * The file does not have to exist when checking NLC_CREATE or NLC_RENAME_DST
1488 * access, otherwise it must exist. No error is returned in this case.
1490 * The file must not exist if NLC_EXCL is specified.
1492 * Directory permissions in general are tested for NLC_CREATE if the file
1493 * does not exist, NLC_DELETE if the file does exist, and NLC_RENAME_DST
1494 * whether the file exists or not.
1496 * The directory sticky bit is tested for NLC_DELETE and NLC_RENAME_DST,
1497 * the latter is only tested if the target exists.
1499 * The passed ncp must be referenced and locked. If it is already resolved
1500 * it may be locked shared but otherwise should be locked exclusively.
1503 #define S_WXOK_MASK (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
1504 #define S_XOK_MASK (S_IXUSR|S_IXGRP|S_IXOTH)
1507 naccess(struct nlookupdata
*nd
, struct nchandle
*nch
, u_int
*genp
, int nflags
,
1508 struct ucred
*cred
, int *nflagsp
, int nchislocked
)
1511 struct vattr_lite lva
;
1512 struct namecache
*ncp
;
1520 * We need a resolved entry. If the entry is not resolved we need
1521 * to lock and resolve it. If it is already resolved, our ref should
1522 * prevent normal evictions (as long as we tested the lock race above).
1524 * If the ncp was locked by the caller and left unresolved, it must
1525 * have been locked exclusively.
1527 if (ncp
->nc_flag
& NCF_UNRESOLVED
) {
1528 if (nchislocked
== 0) {
1532 cache_resolve(nch
, genp
, cred
);
1535 error
= ncp
->nc_error
;
1538 * Only unresolved entries should return this error (though maybe
1539 * in-filesystem sockets can too). XXX check filetype for VSOCK.
1541 if (error
== ENOTCONN
) {
1542 if (nchislocked
== 0) {
1543 if (nlookup_debug
& 4) {
1544 kprintf("ncp %p %08x %d %s: Warning, unexpected state, "
1546 ncp
, ncp
->nc_flag
, ncp
->nc_error
, ncp
->nc_name
);
1547 print_backtrace(-1);
1553 if (nlookup_debug
& 4) {
1554 kprintf("ncp %p %08x %d %s: Warning, unexpected state\n",
1555 ncp
, ncp
->nc_flag
, ncp
->nc_error
, ncp
->nc_name
);
1556 print_backtrace(-1);
1561 * Directory permissions checks. Silently ignore ENOENT if these
1562 * tests pass. It isn't an error.
1564 * We can safely resolve ncp->nc_parent because ncp is currently
1567 * We set nl_dir_error if an error occurs checking directory perms
1568 * for an intermediate directory.
1570 if (nflags
& (NLC_CREATE
| NLC_DELETE
| NLC_RENAME_SRC
| NLC_RENAME_DST
)) {
1571 if (((nflags
& NLC_CREATE
) && ncp
->nc_vp
== NULL
) ||
1572 ((nflags
& NLC_DELETE
) && ncp
->nc_vp
!= NULL
) ||
1573 ((nflags
& NLC_RENAME_SRC
) && ncp
->nc_vp
!= NULL
) ||
1574 (nflags
& NLC_RENAME_DST
)
1576 struct nchandle par
;
1578 if (nchislocked
== 0) {
1579 cache_lock_maybe_shared(nch
, 0);
1583 if ((par
.ncp
= ncp
->nc_parent
) == NULL
) {
1584 if (error
!= EAGAIN
) {
1588 } else if (error
== 0 || error
== ENOENT
) {
1589 u_int dummy_gen
= 0;
1591 par
.mount
= nch
->mount
;
1593 cache_lock_maybe_shared(&par
, 0);
1594 error
= naccess(nd
, &par
, &dummy_gen
, NLC_WRITE
, cred
, NULL
, 1);
1603 * NLC_EXCL check. Target file must not exist.
1605 if (error
== 0 && (nflags
& NLC_EXCL
) && ncp
->nc_vp
!= NULL
)
1609 * Try to short-cut the vnode operation for intermediate directory
1610 * components. This is a major SMP win because it avoids having
1611 * to execute a lot of code for intermediate directory components,
1612 * including shared refs and locks on intermediate directory vnodes.
1614 * We can only do this if the caller does not need nflagsp.
1616 if (error
== 0 && nflagsp
== NULL
&&
1617 nflags
== NLC_EXEC
&& (ncp
->nc_flag
& NCF_WXOK
)) {
1618 if (nchislocked
== 2)
1624 * Get the vnode attributes so we can do the rest of our checks.
1626 * NOTE: We only call naccess_lva() if the target exists.
1629 if (nchislocked
== 0) {
1630 cache_lock_maybe_shared(nch
, 0);
1634 error
= cache_vget(nch
, cred
, LK_SHARED
, &vp
);
1636 error
= cache_vref(nch
, cred
, &vp
);
1638 if (error
== ENOENT
) {
1640 * Silently zero-out ENOENT if creating or renaming
1641 * (rename target). It isn't an error.
1643 if (nflags
& (NLC_CREATE
| NLC_RENAME_DST
))
1645 } else if (error
== 0) {
1647 * Get the vnode attributes and check for illegal O_TRUNC
1648 * requests and read-only mounts.
1650 * NOTE: You can still open devices on read-only mounts for
1653 * NOTE: creates/deletes/renames are handled by the NLC_WRITE
1654 * check on the parent directory above.
1656 * XXX cache the va in the namecache or in the vnode
1658 error
= VOP_GETATTR_LITE(vp
, &lva
);
1659 if (error
== 0 && (nflags
& NLC_TRUNCATE
)) {
1660 switch(lva
.va_type
) {
1675 if (error
== 0 && (nflags
& NLC_WRITE
) && vp
->v_mount
&&
1676 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1678 switch(lva
.va_type
) {
1696 * Check permissions based on file attributes. The passed
1697 * flags (*nflagsp) are modified with feedback based on
1698 * special attributes and requirements.
1702 * Adjust the returned (*nflagsp) if non-NULL.
1705 if ((lva
.va_mode
& VSVTX
) && lva
.va_uid
!= cred
->cr_uid
)
1706 *nflagsp
|= NLC_STICKY
;
1707 if (lva
.va_flags
& APPEND
)
1708 *nflagsp
|= NLC_APPENDONLY
;
1709 if (lva
.va_flags
& IMMUTABLE
)
1710 *nflagsp
|= NLC_IMMUTABLE
;
1714 * NCF_WXOK can be set for world-searchable directories.
1716 * XXX When we implement capabilities this code would also
1717 * need a cap check, or only set the flag if there are no
1721 if (lva
.va_type
== VDIR
&&
1722 (lva
.va_mode
& S_WXOK_MASK
) == S_WXOK_MASK
) {
1725 if ((lva
.va_mode
& S_XOK_MASK
) == 0)
1729 * Track swapcache management flags in the namecache.
1731 * Calculate the flags based on the current vattr_lite info
1732 * and recalculate the inherited flags from the parent
1733 * (the original cache linkage may have occurred without
1734 * getattrs and thus have stale flags).
1736 if (lva
.va_flags
& SF_NOCACHE
)
1737 cflags
|= NCF_SF_NOCACHE
;
1738 if (lva
.va_flags
& UF_CACHE
)
1739 cflags
|= NCF_UF_CACHE
;
1740 if (ncp
->nc_parent
) {
1741 if (ncp
->nc_parent
->nc_flag
&
1742 (NCF_SF_NOCACHE
| NCF_SF_PNOCACHE
)) {
1743 cflags
|= NCF_SF_PNOCACHE
;
1745 if (ncp
->nc_parent
->nc_flag
&
1746 (NCF_UF_CACHE
| NCF_UF_PCACHE
)) {
1747 cflags
|= NCF_UF_PCACHE
;
1752 * We're not supposed to update nc_flag when holding a shared
1753 * lock, but we allow the case for certain flags. Note that
1754 * holding an exclusive lock allows updating nc_flag without
1755 * atomics. nc_flag is not allowe to be updated at all unless
1756 * a shared or exclusive lock is held.
1758 atomic_clear_short(&ncp
->nc_flag
,
1759 (NCF_SF_NOCACHE
| NCF_UF_CACHE
|
1760 NCF_SF_PNOCACHE
| NCF_UF_PCACHE
|
1761 NCF_WXOK
| NCF_NOTX
) & ~cflags
);
1762 atomic_set_short(&ncp
->nc_flag
, cflags
);
1765 * Process general access.
1767 error
= naccess_lva(&lva
, nflags
, cred
);
1771 if (nchislocked
== 2)
1777 * Check the requested access against the given vattr using cred.
1780 naccess_lva(struct vattr_lite
*lvap
, int nflags
, struct ucred
*cred
)
1786 * Test the immutable bit. Creations, deletions, renames (source
1787 * or destination) are not allowed. chown/chmod/other is also not
1788 * allowed but is handled by SETATTR. Hardlinks to the immutable
1791 * If the directory is set to immutable then creations, deletions,
1792 * renames (source or dest) and hardlinks to files within the directory
1793 * are not allowed, and regular files opened through the directory may
1794 * not be written to or truncated (unless a special device).
1796 * NOTE! New hardlinks to immutable files work but new hardlinks to
1797 * files, immutable or not, sitting inside an immutable directory are
1798 * not allowed. As always if the file is hardlinked via some other
1799 * path additional hardlinks may be possible even if the file is marked
1800 * immutable. The sysop needs to create a closure by checking the hard
1801 * link count. Once closure is achieved you are good, and security
1802 * scripts should check link counts anyway.
1804 * Writes and truncations are only allowed on special devices.
1806 if ((lvap
->va_flags
& IMMUTABLE
) || (nflags
& NLC_IMMUTABLE
)) {
1807 if ((nflags
& NLC_IMMUTABLE
) && (nflags
& NLC_HLINK
))
1809 if (nflags
& (NLC_CREATE
| NLC_DELETE
|
1810 NLC_RENAME_SRC
| NLC_RENAME_DST
)) {
1813 if (nflags
& (NLC_WRITE
| NLC_TRUNCATE
)) {
1814 switch(lvap
->va_type
) {
1828 * Test the no-unlink and append-only bits for opens, rename targets,
1829 * and deletions. These bits are not tested for creations or
1832 * Unlike FreeBSD we allow a file with APPEND set to be renamed.
1833 * If you do not wish this you must also set NOUNLINK.
1835 * If the governing directory is marked APPEND-only it implies
1836 * NOUNLINK for all entries in the directory.
1838 if (((lvap
->va_flags
& NOUNLINK
) || (nflags
& NLC_APPENDONLY
)) &&
1839 (nflags
& (NLC_DELETE
| NLC_RENAME_SRC
| NLC_RENAME_DST
))
1845 * A file marked append-only may not be deleted but can be renamed.
1847 if ((lvap
->va_flags
& APPEND
) &&
1848 (nflags
& (NLC_DELETE
| NLC_RENAME_DST
))
1854 * A file marked append-only which is opened for writing must also
1855 * be opened O_APPEND.
1857 if ((lvap
->va_flags
& APPEND
) && (nflags
& (NLC_OPEN
| NLC_TRUNCATE
))) {
1858 if (nflags
& NLC_TRUNCATE
)
1860 if ((nflags
& (NLC_OPEN
| NLC_WRITE
)) == (NLC_OPEN
| NLC_WRITE
)) {
1861 if ((nflags
& NLC_APPEND
) == 0)
1867 * root gets universal access
1869 if (cred
->cr_uid
== 0)
1873 * Check owner perms.
1875 * If NLC_OWN is set the owner of the file is allowed no matter when
1876 * the owner-mode bits say (utimes).
1879 if (nflags
& NLC_READ
)
1881 if (nflags
& NLC_WRITE
)
1883 if (nflags
& NLC_EXEC
)
1886 if (cred
->cr_uid
== lvap
->va_uid
) {
1887 if ((nflags
& NLC_OWN
) == 0) {
1888 if ((vmode
& lvap
->va_mode
) != vmode
)
1895 * If NLC_STICKY is set only the owner may delete or rename a file.
1896 * This bit is typically set on /tmp.
1898 * Note that the NLC_READ/WRITE/EXEC bits are not typically set in
1899 * the specific delete or rename case. For deletions and renames we
1900 * usually just care about directory permissions, not file permissions.
1902 if ((nflags
& NLC_STICKY
) &&
1903 (nflags
& (NLC_RENAME_SRC
| NLC_RENAME_DST
| NLC_DELETE
))) {
1911 for (i
= 0; i
< cred
->cr_ngroups
; ++i
) {
1912 if (lvap
->va_gid
== cred
->cr_groups
[i
]) {
1913 if ((vmode
& lvap
->va_mode
) != vmode
)
1923 if ((vmode
& lvap
->va_mode
) != vmode
)
1929 * Long-term (10-second interval) statistics collection
1933 collect_nlookup_callback(int n
)
1935 static uint64_t last_total
;
1940 for (n
= 0; n
< ncpus
; ++n
) {
1941 globaldata_t gd
= globaldata_find(n
);
1942 struct nchstats
*sp
;
1944 if ((sp
= gd
->gd_nchstats
) != NULL
)
1945 total
+= sp
->ncs_longhits
+ sp
->ncs_longmiss
;
1948 total
= total
- last_total
;
1956 nlookup_collect_init(void *dummy __unused
)
1958 kcollect_register(KCOLLECT_NLOOKUP
, "nlookup", collect_nlookup_callback
,
1959 KCOLLECT_SCALE(KCOLLECT_NLOOKUP_FORMAT
, 0));
1961 SYSINIT(collect_nlookup
, SI_SUB_PROP
, SI_ORDER_ANY
, nlookup_collect_init
, 0);