2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/syslimits.h>
79 #include <sys/namei.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/mount_internal.h>
83 #include <sys/errno.h>
84 #include <sys/malloc.h>
85 #include <sys/filedesc.h>
86 #include <sys/proc_internal.h>
87 #include <sys/kdebug.h>
88 #include <sys/unistd.h> /* For _PC_NAME_MAX */
89 #include <sys/uio_internal.h>
90 #include <sys/kauth.h>
92 #include <bsm/audit_kernel.h>
95 #include <security/mac_framework.h>
99 #include <sys/xattr.h>
102 * The minimum volfs-style pathname is 9.
103 * Example: "/.vol/1/2"
105 #define VOLFS_MIN_PATH_LEN 9
108 static void kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
);
111 static int vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
);
115 * Convert a pathname into a pointer to a locked inode.
117 * The FOLLOW flag is set when symbolic links are to be followed
118 * when they occur at the end of the name translation process.
119 * Symbolic links are always followed for all other pathname
120 * components other than the last.
122 * The segflg defines whether the name is to be copied from user
123 * space or kernel space.
125 * Overall outline of namei:
128 * get starting directory
129 * while (!done && !error) {
130 * call lookup to search path.
131 * if symbolic link, massage name in buffer and continue
135 * ENOENT No such file or directory
136 * ELOOP Too many levels of symbolic links
137 * ENAMETOOLONG Filename too long
138 * copyinstr:EFAULT Bad address
139 * copyinstr:ENAMETOOLONG Filename too long
140 * lookup:EBADF Bad file descriptor
148 namei(struct nameidata
*ndp
)
150 struct filedesc
*fdp
; /* pointer to file descriptor state */
151 char *cp
; /* pointer into pathname argument */
152 struct vnode
*dp
; /* the directory we are searching */
155 struct componentname
*cnp
= &ndp
->ni_cnd
;
156 vfs_context_t ctx
= cnp
->cn_context
;
157 proc_t p
= vfs_context_proc(ctx
);
158 /* XXX ut should be from context */
159 uthread_t ut
= (struct uthread
*)get_bsdthread_info(current_thread());
161 char uio_buf
[ UIO_SIZEOF(1) ];
164 if (!vfs_context_ucred(ctx
) || !p
)
165 panic ("namei: bad cred/proc");
166 if (cnp
->cn_nameiop
& (~OPMASK
))
167 panic ("namei: nameiop contaminated with flags");
168 if (cnp
->cn_flags
& OPMASK
)
169 panic ("namei: flags contaminated with nameiops");
174 * Get a buffer for the name to be translated, and copy the
175 * name into the buffer.
177 if ((cnp
->cn_flags
& HASBUF
) == 0) {
178 cnp
->cn_pnbuf
= ndp
->ni_pathbuf
;
179 cnp
->cn_pnlen
= PATHBUFLEN
;
182 if (IS_VALID_UIO_SEGFLG(ndp
->ni_segflg
) == 0) {
183 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
185 #endif /* LP64_DEBUG */
188 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
189 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
190 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
192 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
193 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
195 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
196 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
197 if (cnp
->cn_pnbuf
== NULL
) {
202 cnp
->cn_flags
|= HASBUF
;
203 cnp
->cn_pnlen
= MAXPATHLEN
;
212 * Check for legacy volfs style pathnames.
214 * For compatibility reasons we currently allow these paths,
215 * but future versions of the OS may not support them.
217 if (ndp
->ni_pathlen
>= VOLFS_MIN_PATH_LEN
&&
218 cnp
->cn_pnbuf
[0] == '/' &&
219 cnp
->cn_pnbuf
[1] == '.' &&
220 cnp
->cn_pnbuf
[2] == 'v' &&
221 cnp
->cn_pnbuf
[3] == 'o' &&
222 cnp
->cn_pnbuf
[4] == 'l' &&
223 cnp
->cn_pnbuf
[5] == '/' ) {
226 /* Attempt to resolve a legacy volfs style pathname. */
227 MALLOC_ZONE(realpath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
229 if ((realpath_err
= vfs_getrealpath(&cnp
->cn_pnbuf
[6], realpath
, MAXPATHLEN
, ctx
))) {
230 FREE_ZONE(realpath
, MAXPATHLEN
, M_NAMEI
);
231 if (realpath_err
== ENOSPC
){
232 error
= ENAMETOOLONG
;
236 if (cnp
->cn_flags
& HASBUF
) {
237 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
239 cnp
->cn_pnbuf
= realpath
;
240 cnp
->cn_pnlen
= MAXPATHLEN
;
241 ndp
->ni_pathlen
= strlen(realpath
) + 1;
242 cnp
->cn_flags
|= HASBUF
| CN_VOLFSPATH
;
246 #endif /* CONFIG_VOLFS */
248 /* If we are auditing the kernel pathname, save the user pathname */
249 if (cnp
->cn_flags
& AUDITVNPATH1
)
250 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH1
);
251 if (cnp
->cn_flags
& AUDITVNPATH2
)
252 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH2
);
255 * Do not allow empty pathnames
257 if (*cnp
->cn_pnbuf
== '\0') {
264 * determine the starting point for the translation.
266 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
267 if ( !(fdp
->fd_flags
& FD_CHROOT
))
268 ndp
->ni_rootdir
= rootvnode
;
270 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
272 ndp
->ni_usedvp
= NULLVP
;
274 if (*(cnp
->cn_nameptr
) == '/') {
275 while (*(cnp
->cn_nameptr
) == '/') {
279 dp
= ndp
->ni_rootdir
;
280 } else if (cnp
->cn_flags
& USEDVP
) {
284 dp
= vfs_context_cwd(ctx
);
286 if (dp
== NULLVP
|| (dp
->v_lflag
& VL_DEAD
)) {
290 ndp
->ni_dvp
= NULLVP
;
297 ndp
->ni_startdir
= dp
;
299 if ( (error
= lookup(ndp
)) ) {
303 * Check for symbolic link
305 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
308 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
309 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
310 unlock_fsnode(ndp
->ni_dvp
, NULL
);
312 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
317 if ((error
= mac_vnode_check_readlink(ctx
, ndp
->ni_vp
)) != 0)
320 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
325 if (need_newpathbuf
) {
326 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
334 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
336 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
338 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
341 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
344 // LP64todo - fix this
345 linklen
= MAXPATHLEN
- uio_resid(auio
);
346 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
348 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
350 error
= ENAMETOOLONG
;
353 if (need_newpathbuf
) {
354 long len
= cnp
->cn_pnlen
;
356 tmppn
= cnp
->cn_pnbuf
;
357 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
359 cnp
->cn_pnlen
= MAXPATHLEN
;
361 if ( (cnp
->cn_flags
& HASBUF
) )
362 FREE_ZONE(tmppn
, len
, M_NAMEI
);
364 cnp
->cn_flags
|= HASBUF
;
366 cnp
->cn_pnbuf
[linklen
] = '\0';
368 ndp
->ni_pathlen
+= linklen
;
369 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
372 * starting point for 'relative'
377 * get rid of references returned via 'lookup'
379 vnode_put(ndp
->ni_vp
);
380 vnode_put(ndp
->ni_dvp
);
383 ndp
->ni_dvp
= NULLVP
;
386 * Check if symbolic link restarts us at the root
388 if (*(cnp
->cn_nameptr
) == '/') {
389 while (*(cnp
->cn_nameptr
) == '/') {
393 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
400 * only come here if we fail to handle a SYMLINK...
401 * if either ni_dvp or ni_vp is non-NULL, then
402 * we need to drop the iocount that was picked
403 * up in the lookup routine
406 vnode_put(ndp
->ni_dvp
);
408 vnode_put(ndp
->ni_vp
);
410 if ( (cnp
->cn_flags
& HASBUF
) ) {
411 cnp
->cn_flags
&= ~HASBUF
;
412 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
414 cnp
->cn_pnbuf
= NULL
;
423 * This is a very central and rather complicated routine.
425 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
426 * The starting directory is taken from ni_startdir. The pathname is
427 * descended until done, or a symbolic link is encountered. The variable
428 * ni_more is clear if the path is completed; it is set to one if a
429 * symbolic link needing interpretation is encountered.
431 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
432 * whether the name is to be looked up, created, renamed, or deleted.
433 * When CREATE, RENAME, or DELETE is specified, information usable in
434 * creating, renaming, or deleting a directory entry may be calculated.
435 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
436 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
437 * returned unlocked. Otherwise the parent directory is not returned. If
438 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
439 * the target is returned locked, otherwise it is returned unlocked.
440 * When creating or renaming and LOCKPARENT is specified, the target may not
441 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
443 * Overall outline of lookup:
446 * identify next component of name at ndp->ni_ptr
447 * handle degenerate case where name is null string
448 * if .. and crossing mount points and on mounted filesys, find parent
449 * call VNOP_LOOKUP routine for next component name
450 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
451 * component vnode returned in ni_vp (if it exists), locked.
452 * if result vnode is mounted on and crossing mount points,
453 * find mounted on vnode
454 * if more components of name, do next level at dirloop
455 * return the answer in ni_vp, locked if LOCKLEAF set
456 * if LOCKPARENT set, return locked parent in ni_dvp
457 * if WANTPARENT set, return unlocked parent in ni_dvp
460 * ENOENT No such file or directory
461 * EBADF Bad file descriptor
462 * ENOTDIR Not a directory
463 * EROFS Read-only file system [CREATE]
464 * EISDIR Is a directory [CREATE]
465 * cache_lookup_path:ENOENT
466 * vnode_authorize:EROFS
467 * vnode_authorize:EACCES
468 * vnode_authorize:EPERM
469 * vnode_authorize:???
470 * VNOP_LOOKUP:ENOENT No such file or directory
471 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
478 lookup(struct nameidata
*ndp
)
480 char *cp
; /* pointer into pathname argument */
481 vnode_t tdp
; /* saved dp */
482 vnode_t dp
; /* the directory we are searching */
483 mount_t mp
; /* mount table entry */
484 int docache
= 1; /* == 0 do not cache last component */
485 int wantparent
; /* 1 => wantparent or lockparent flag */
486 int rdonly
; /* lookup read-only flag bit */
487 int trailing_slash
= 0;
488 int dp_authorized
= 0;
490 struct componentname
*cnp
= &ndp
->ni_cnd
;
491 vfs_context_t ctx
= cnp
->cn_context
;
492 int mounted_on_depth
= 0;
493 int dont_cache_mp
= 0;
494 vnode_t mounted_on_dp
= NULLVP
;
495 int current_mount_generation
= 0;
497 int nc_generation
= 0;
500 * Setup: break out flag bits into variables.
502 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
503 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
506 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
507 rdonly
= cnp
->cn_flags
& RDONLY
;
508 cnp
->cn_flags
&= ~ISSYMLINK
;
511 dp
= ndp
->ni_startdir
;
512 ndp
->ni_startdir
= NULLVP
;
514 if ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0)
515 vbusyflags
= LK_NOWAIT
;
516 cp
= cnp
->cn_nameptr
;
519 if ( (vnode_getwithref(dp
)) ) {
529 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
)) ) {
533 if ((cnp
->cn_flags
& ISLASTCN
)) {
535 cnp
->cn_flags
|= MAKEENTRY
;
537 cnp
->cn_flags
|= MAKEENTRY
;
541 if (ndp
->ni_vp
!= NULLVP
) {
543 * cache_lookup_path returned a non-NULL ni_vp then,
544 * we're guaranteed that the dp is a VDIR, it's
545 * been authorized, and vp is not ".."
547 * make sure we don't try to enter the name back into
548 * the cache if this vp is purged before we get to that
549 * check since we won't have serialized behind whatever
550 * activity is occurring in the FS that caused the purge
553 nc_generation
= dp
->v_nc_generation
- 1;
555 goto returned_from_lookup_path
;
559 * Handle "..": two special cases.
560 * 1. If at root directory (e.g. after chroot)
561 * or at absolute root directory
562 * then ignore it so can't get out.
563 * 2. If this vnode is the root of a mounted
564 * filesystem, then replace it with the
565 * vnode which was mounted on so we take the
566 * .. in the other file system.
568 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
570 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
574 * we're pinned at the root
575 * we've already got one reference on 'dp'
576 * courtesy of cache_lookup_path... take
577 * another one for the ".."
578 * if we fail to get the new reference, we'll
579 * drop our original down in 'bad'
581 if ( (vnode_get(dp
)) ) {
587 if ((dp
->v_flag
& VROOT
) == 0 ||
588 (cnp
->cn_flags
& NOCROSSMOUNT
))
590 if (dp
->v_mount
== NULL
) { /* forced umount */
595 dp
= tdp
->v_mount
->mnt_vnodecovered
;
599 if ( (vnode_getwithref(dp
)) ) {
610 * We now have a segment name to search for, and a directory to search.
615 if (dp
->v_type
!= VDIR
) {
619 if ( (cnp
->cn_flags
& DONOTAUTH
) != DONOTAUTH
) {
620 if (!dp_authorized
) {
621 error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
626 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
629 #endif /* CONFIG_MACF */
632 nc_generation
= dp
->v_nc_generation
;
634 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
636 if ((error
== ENOENT
) &&
637 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
638 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
639 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
640 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
641 unlock_fsnode(dp
, NULL
);
644 dp
= tdp
->v_mount
->mnt_vnodecovered
;
648 if ( (vnode_getwithref(dp
)) ) {
658 if (error
!= EJUSTRETURN
)
661 if (ndp
->ni_vp
!= NULLVP
)
662 panic("leaf should be empty");
665 * If creating and at end of pathname, then can consider
666 * allowing file to be created.
672 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
677 * We return with ni_vp NULL to indicate that the entry
678 * doesn't currently exist, leaving a pointer to the
679 * referenced directory vnode in ndp->ni_dvp.
681 if (cnp
->cn_flags
& SAVESTART
) {
682 if ( (vnode_get(ndp
->ni_dvp
)) ) {
686 ndp
->ni_startdir
= ndp
->ni_dvp
;
689 vnode_put(ndp
->ni_dvp
);
692 kdebug_lookup(ndp
->ni_dvp
, cnp
);
695 returned_from_lookup_path
:
699 * Take into account any additional components consumed by
700 * the underlying filesystem.
702 if (cnp
->cn_consume
> 0) {
703 cnp
->cn_nameptr
+= cnp
->cn_consume
;
704 ndp
->ni_next
+= cnp
->cn_consume
;
705 ndp
->ni_pathlen
-= cnp
->cn_consume
;
708 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
710 int update_flags
= 0;
712 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
714 if (isdot_or_dotdot
== 0) {
715 if (dp
->v_name
== NULL
)
716 update_flags
|= VNODE_UPDATE_NAME
;
717 if (ndp
->ni_dvp
!= NULLVP
&& dp
->v_parent
== NULLVP
)
718 update_flags
|= VNODE_UPDATE_PARENT
;
721 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
724 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
726 * missing from name cache, but should
727 * be in it... this can happen if volfs
728 * causes the vnode to be created or the
729 * name cache entry got recycled but the
731 * check to make sure that ni_dvp is valid
732 * cache_lookup_path may return a NULL
733 * do a quick check to see if the generation of the
734 * directory matches our snapshot... this will get
735 * rechecked behind the name cache lock, but if it
736 * already fails to match, no need to go any further
738 if (ndp
->ni_dvp
!= NULLVP
&& (nc_generation
== ndp
->ni_dvp
->v_nc_generation
))
739 cache_enter_with_gen(ndp
->ni_dvp
, dp
, cnp
, nc_generation
);
744 mounted_on_depth
= 0;
746 current_mount_generation
= mount_generation
;
748 * Check to see if the vnode has been mounted on...
749 * if so find the root of the mounted file system.
752 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
753 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
757 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
758 struct uthread
*uth
= (struct uthread
*)get_bsdthread_info(current_thread());
764 if (vfs_busy(mp
, vbusyflags
)) {
765 mount_dropcrossref(mp
, dp
, 0);
766 if (vbusyflags
== LK_NOWAIT
) {
770 goto check_mounted_on
;
774 * XXX - if this is the last component of the
775 * pathname, and it's either not a lookup operation
776 * or the NOTRIGGER flag is set for the operation,
777 * set a uthread flag to let VFS_ROOT() for autofs
778 * know it shouldn't trigger a mount.
780 if ((cnp
->cn_flags
& ISLASTCN
) &&
781 (cnp
->cn_nameiop
!= LOOKUP
||
782 (cnp
->cn_flags
& NOTRIGGER
))) {
783 uth
->uu_notrigger
= 1;
786 error
= VFS_ROOT(mp
, &tdp
, ctx
);
787 /* XXX - clear the uthread flag */
788 uth
->uu_notrigger
= 0;
790 * mount_dropcrossref does a vnode_put
791 * on dp if the 3rd arg is non-zero
793 mount_dropcrossref(mp
, dp
, 1);
800 ndp
->ni_vp
= dp
= tdp
;
803 goto check_mounted_on
;
809 if (vfs_flags(vnode_mount(dp
)) & MNT_MULTILABEL
) {
810 error
= vnode_label(vnode_mount(dp
), NULL
, dp
, NULL
,
811 VNODE_LABEL_NEEDREF
, ctx
);
817 if (mounted_on_depth
&& !dont_cache_mp
) {
818 mp
= mounted_on_dp
->v_mountedhere
;
822 mp
->mnt_realrootvp_vid
= dp
->v_id
;
823 mp
->mnt_realrootvp
= dp
;
824 mp
->mnt_generation
= current_mount_generation
;
830 * Check for symbolic link
832 if ((dp
->v_type
== VLNK
) &&
833 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
834 cnp
->cn_flags
|= ISSYMLINK
;
839 * Check for bogus trailing slashes.
841 if (trailing_slash
) {
842 if (dp
->v_type
!= VDIR
) {
851 * Not a symbolic link. If more pathname,
852 * continue at next component, else return.
854 if (*ndp
->ni_next
== '/') {
855 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
857 while (*cnp
->cn_nameptr
== '/') {
861 vnode_put(ndp
->ni_dvp
);
863 cp
= cnp
->cn_nameptr
;
873 * Disallow directory write attempts on read-only file systems.
876 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
880 if (cnp
->cn_flags
& SAVESTART
) {
882 * note that we already hold a reference
883 * on both dp and ni_dvp, but for some reason
884 * can't get another one... in this case we
885 * need to do vnode_put on dp in 'bad2'
887 if ( (vnode_get(ndp
->ni_dvp
)) ) {
891 ndp
->ni_startdir
= ndp
->ni_dvp
;
893 if (!wantparent
&& ndp
->ni_dvp
) {
894 vnode_put(ndp
->ni_dvp
);
895 ndp
->ni_dvp
= NULLVP
;
898 if (cnp
->cn_flags
& AUDITVNPATH1
)
899 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
900 else if (cnp
->cn_flags
& AUDITVNPATH2
)
901 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
905 * Caller wants the resource fork.
907 if ((cnp
->cn_flags
& CN_WANTSRSRCFORK
) && (dp
!= NULLVP
)) {
908 vnode_t svp
= NULLVP
;
909 enum nsoperation nsop
;
911 if (dp
->v_type
!= VREG
) {
915 switch (cnp
->cn_nameiop
) {
923 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
924 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
935 /* Ask the file system for the resource fork. */
936 error
= vnode_getnamedstream(dp
, &svp
, XATTR_RESOURCEFORK_NAME
, nsop
, 0, ctx
);
938 /* During a create, it OK for stream vnode to be missing. */
939 if (error
== ENOATTR
|| error
== ENOENT
) {
940 error
= (nsop
== NS_CREATE
) ? 0 : ENOENT
;
945 /* The "parent" of the stream is the file. */
948 if (ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
) {
949 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
950 unlock_fsnode(ndp
->ni_dvp
, NULL
);
952 vnode_put(ndp
->ni_dvp
);
958 ndp
->ni_vp
= dp
= svp
; /* on create this may be null */
960 /* Restore the truncated pathname buffer (for audits). */
961 if (ndp
->ni_pathlen
== 1 && ndp
->ni_next
[0] == '\0') {
962 ndp
->ni_next
[0] = '/';
964 cnp
->cn_flags
&= ~MAKEENTRY
;
968 kdebug_lookup(dp
, cnp
);
974 * A degenerate name (e.g. / or "") which is a way of
975 * talking about a directory, e.g. like "/." or ".".
977 if (dp
->v_type
!= VDIR
) {
981 if (cnp
->cn_nameiop
!= LOOKUP
) {
987 * note that we already hold a reference
988 * on dp, but for some reason can't
989 * get another one... in this case we
990 * need to do vnode_put on dp in 'bad'
992 if ( (vnode_get(dp
)) ) {
998 cnp
->cn_flags
&= ~ISDOTDOT
;
999 cnp
->cn_flags
|= ISLASTCN
;
1003 if (cnp
->cn_flags
& AUDITVNPATH1
)
1004 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
1005 else if (cnp
->cn_flags
& AUDITVNPATH2
)
1006 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
1007 if (cnp
->cn_flags
& SAVESTART
)
1008 panic("lookup: SAVESTART");
1012 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1013 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1014 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1017 vnode_put(ndp
->ni_dvp
);
1020 ndp
->ni_vp
= NULLVP
;
1023 kdebug_lookup(dp
, cnp
);
1027 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1028 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1029 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1033 ndp
->ni_vp
= NULLVP
;
1036 kdebug_lookup(dp
, cnp
);
1041 * relookup - lookup a path name component
1042 * Used by lookup to re-aquire things.
1045 relookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1047 struct vnode
*dp
= NULL
; /* the directory we are searching */
1048 int wantparent
; /* 1 => wantparent or lockparent flag */
1049 int rdonly
; /* lookup read-only flag bit */
1051 #ifdef NAMEI_DIAGNOSTIC
1052 int i
, newhash
; /* DEBUG: check name hash */
1053 char *cp
; /* DEBUG: check name ptr/len */
1055 vfs_context_t ctx
= cnp
->cn_context
;;
1058 * Setup: break out flag bits into variables.
1060 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
1061 rdonly
= cnp
->cn_flags
& RDONLY
;
1062 cnp
->cn_flags
&= ~ISSYMLINK
;
1064 if (cnp
->cn_flags
& NOCACHE
)
1065 cnp
->cn_flags
&= ~MAKEENTRY
;
1067 cnp
->cn_flags
|= MAKEENTRY
;
1072 * Check for degenerate name (e.g. / or "")
1073 * which is a way of talking about a directory,
1074 * e.g. like "/." or ".".
1076 if (cnp
->cn_nameptr
[0] == '\0') {
1077 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
1081 if (dp
->v_type
!= VDIR
) {
1085 if ( (vnode_get(dp
)) ) {
1091 if (cnp
->cn_flags
& SAVESTART
)
1092 panic("lookup: SAVESTART");
1096 * We now have a segment name to search for, and a directory to search.
1098 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
1099 if (error
!= EJUSTRETURN
)
1103 panic("leaf should be empty");
1106 * If creating and at end of pathname, then can consider
1107 * allowing file to be created.
1114 * We return with ni_vp NULL to indicate that the entry
1115 * doesn't currently exist, leaving a pointer to the
1116 * (possibly locked) directory inode in ndp->ni_dvp.
1124 * Check for symbolic link
1126 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
1127 panic ("relookup: symlink found.\n");
1131 * Disallow directory write attempts on read-only file systems.
1134 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
1138 /* ASSERT(dvp == ndp->ni_startdir) */
1151 * Free pathname buffer
1154 nameidone(struct nameidata
*ndp
)
1156 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
1157 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
1158 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1160 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
1161 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
1163 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
1164 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
1165 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
1173 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1174 * by fs_usage. The path up to and including the current component name are
1175 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1176 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1177 * That is, the truncation removes the leading portion of the path.
1179 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1180 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1181 * (in addition to DBG_FUNC_START if it is also the first). There may be
1182 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1184 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1185 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1186 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1187 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1188 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1189 * path) is set to zero bytes, or '>' if there is more path beyond the
1190 * current component name (usually because an intermediate component was not
1193 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1194 * 12+N*16, there will be no padding.
1196 * TODO: If there is more path beyond the current component name, should we
1197 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1198 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1199 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1202 kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
)
1208 long dbg_parms
[NUMPARMS
];
1210 /* Collect the pathname for tracing */
1211 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
1212 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
1214 if (dbg_namelen
> (int)sizeof(dbg_parms
))
1215 dbg_namelen
= sizeof(dbg_parms
);
1216 dbg_nameptr
-= dbg_namelen
;
1218 /* Copy the (possibly truncated) path itself */
1219 memcpy(dbg_parms
, dbg_nameptr
, dbg_namelen
);
1221 /* Pad with '\0' or '>' */
1222 if (dbg_namelen
< (int)sizeof(dbg_parms
)) {
1223 memset((char *)dbg_parms
+ dbg_namelen
,
1224 *(cnp
->cn_nameptr
+ cnp
->cn_namelen
) ? '>' : 0,
1225 sizeof(dbg_parms
) - dbg_namelen
);
1229 * In the event that we collect multiple, consecutive pathname
1230 * entries, we must mark the start of the path's string and the end.
1232 code
= (FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
;
1234 if (dbg_namelen
<= 12)
1235 code
|= DBG_FUNC_END
;
1237 KERNEL_DEBUG_CONSTANT(code
, (unsigned int)dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
1239 code
&= ~DBG_FUNC_START
;
1241 for (i
=3, dbg_namelen
-= 12; dbg_namelen
> 0; i
+=4, dbg_namelen
-= 16) {
1242 if (dbg_namelen
<= 16)
1243 code
|= DBG_FUNC_END
;
1245 KERNEL_DEBUG_CONSTANT(code
, dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);
1250 * Obtain the real path from a legacy volfs style path.
1252 * Valid formats of input path:
1257 * "555/123456/foobar"
1260 * 555 represents the volfs file system id
1261 * '@' and '2' are aliases to the root of a file system
1262 * 123456 represents a file id
1263 * "foobar" represents a file name
1267 vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
)
1270 struct mount
*mp
= NULL
;
1278 /* Get file system id and move str to next component. */
1279 id
= strtoul(path
, &str
, 10);
1280 if (id
== 0 || str
[0] != '/') {
1283 while (*str
== '/') {
1288 mp
= mount_lookupby_volfsid(id
, 1);
1290 return (EINVAL
); /* unexpected failure */
1292 /* Check for an alias to a file system root. */
1293 if (ch
== '@' && str
[1] == '\0') {
1297 /* Get file id and move str to next component. */
1298 ino
= strtouq(str
, &str
, 10);
1301 /* Get the target vnode. */
1303 error
= VFS_ROOT(mp
, &vp
, ctx
);
1305 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
1313 /* Get the absolute path to this vnode. */
1314 error
= build_path(vp
, realpath
, bufsize
, &length
, 0, ctx
);
1317 if (error
== 0 && *str
!= '\0') {
1318 int attempt
= strlcat(realpath
, str
, MAXPATHLEN
);
1319 if (attempt
> MAXPATHLEN
){
1320 error
= ENAMETOOLONG
;