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 <security/audit/audit.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
144 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
145 * This means we should re-drive lookup from this point.
150 namei(struct nameidata
*ndp
)
152 struct filedesc
*fdp
; /* pointer to file descriptor state */
153 char *cp
; /* pointer into pathname argument */
154 struct vnode
*dp
; /* the directory we are searching */
155 struct vnode
*usedvp
= ndp
->ni_dvp
; /* store pointer to vp in case we must loop due to
156 heavy vnode pressure */
157 u_long cnpflags
= ndp
->ni_cnd
.cn_flags
; /* store in case we have to restore after loop */
160 struct componentname
*cnp
= &ndp
->ni_cnd
;
161 vfs_context_t ctx
= cnp
->cn_context
;
162 proc_t p
= vfs_context_proc(ctx
);
164 /* XXX ut should be from context */
165 uthread_t ut
= (struct uthread
*)get_bsdthread_info(current_thread());
168 char uio_buf
[ UIO_SIZEOF(1) ];
171 if (!vfs_context_ucred(ctx
) || !p
)
172 panic ("namei: bad cred/proc");
173 if (cnp
->cn_nameiop
& (~OPMASK
))
174 panic ("namei: nameiop contaminated with flags");
175 if (cnp
->cn_flags
& OPMASK
)
176 panic ("namei: flags contaminated with nameiops");
183 * Get a buffer for the name to be translated, and copy the
184 * name into the buffer.
186 if ((cnp
->cn_flags
& HASBUF
) == 0) {
187 cnp
->cn_pnbuf
= ndp
->ni_pathbuf
;
188 cnp
->cn_pnlen
= PATHBUFLEN
;
191 if ((UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
) == 0)
192 && (ndp
->ni_segflg
!= UIO_SYSSPACE
)
193 && (ndp
->ni_segflg
!= UIO_SYSSPACE32
)) {
194 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
196 #endif /* LP64_DEBUG */
199 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
200 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
201 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
203 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
204 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
206 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
207 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
208 if (cnp
->cn_pnbuf
== NULL
) {
213 cnp
->cn_flags
|= HASBUF
;
214 cnp
->cn_pnlen
= MAXPATHLEN
;
223 * Check for legacy volfs style pathnames.
225 * For compatibility reasons we currently allow these paths,
226 * but future versions of the OS may not support them.
228 if (ndp
->ni_pathlen
>= VOLFS_MIN_PATH_LEN
&&
229 cnp
->cn_pnbuf
[0] == '/' &&
230 cnp
->cn_pnbuf
[1] == '.' &&
231 cnp
->cn_pnbuf
[2] == 'v' &&
232 cnp
->cn_pnbuf
[3] == 'o' &&
233 cnp
->cn_pnbuf
[4] == 'l' &&
234 cnp
->cn_pnbuf
[5] == '/' ) {
237 /* Attempt to resolve a legacy volfs style pathname. */
238 MALLOC_ZONE(realpath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
241 * We only error out on the ENAMETOOLONG cases where we know that
242 * vfs_getrealpath translation succeeded but the path could not fit into
243 * MAXPATHLEN characters. In other failure cases, we may be dealing with a path
244 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
246 if ((realpath_err
= vfs_getrealpath(&cnp
->cn_pnbuf
[6], realpath
, MAXPATHLEN
, ctx
))) {
247 FREE_ZONE(realpath
, MAXPATHLEN
, M_NAMEI
);
248 if (realpath_err
== ENOSPC
|| realpath_err
== ENAMETOOLONG
){
249 error
= ENAMETOOLONG
;
253 if (cnp
->cn_flags
& HASBUF
) {
254 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
256 cnp
->cn_pnbuf
= realpath
;
257 cnp
->cn_pnlen
= MAXPATHLEN
;
258 ndp
->ni_pathlen
= strlen(realpath
) + 1;
259 cnp
->cn_flags
|= HASBUF
| CN_VOLFSPATH
;
263 #endif /* CONFIG_VOLFS */
266 /* If we are auditing the kernel pathname, save the user pathname */
267 if (cnp
->cn_flags
& AUDITVNPATH1
)
268 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH1
);
269 if (cnp
->cn_flags
& AUDITVNPATH2
)
270 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH2
);
271 #endif /* CONFIG_AUDIT */
274 * Do not allow empty pathnames
276 if (*cnp
->cn_pnbuf
== '\0') {
283 * determine the starting point for the translation.
285 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
286 if ( !(fdp
->fd_flags
& FD_CHROOT
))
287 ndp
->ni_rootdir
= rootvnode
;
289 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
291 ndp
->ni_usedvp
= NULLVP
;
293 if (*(cnp
->cn_nameptr
) == '/') {
294 while (*(cnp
->cn_nameptr
) == '/') {
298 dp
= ndp
->ni_rootdir
;
299 } else if (cnp
->cn_flags
& USEDVP
) {
303 dp
= vfs_context_cwd(ctx
);
305 if (dp
== NULLVP
|| (dp
->v_lflag
& VL_DEAD
)) {
309 ndp
->ni_dvp
= NULLVP
;
316 ndp
->ni_startdir
= dp
;
318 if ( (error
= lookup(ndp
)) ) {
322 * Check for symbolic link
324 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
328 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
329 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
330 unlock_fsnode(ndp
->ni_dvp
, NULL
);
332 #endif /* __LP64__ */
334 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
339 if ((error
= mac_vnode_check_readlink(ctx
, ndp
->ni_vp
)) != 0)
342 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
347 if (need_newpathbuf
) {
348 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
356 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
358 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
360 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
363 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
368 * Safe to set unsigned with a [larger] signed type here
369 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
372 linklen
= MAXPATHLEN
- (u_int
)uio_resid(auio
);
373 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
375 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
377 error
= ENAMETOOLONG
;
380 if (need_newpathbuf
) {
381 long len
= cnp
->cn_pnlen
;
383 tmppn
= cnp
->cn_pnbuf
;
384 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
386 cnp
->cn_pnlen
= MAXPATHLEN
;
388 if ( (cnp
->cn_flags
& HASBUF
) )
389 FREE_ZONE(tmppn
, len
, M_NAMEI
);
391 cnp
->cn_flags
|= HASBUF
;
393 cnp
->cn_pnbuf
[linklen
] = '\0';
395 ndp
->ni_pathlen
+= linklen
;
396 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
399 * starting point for 'relative'
404 * get rid of references returned via 'lookup'
406 vnode_put(ndp
->ni_vp
);
407 vnode_put(ndp
->ni_dvp
);
410 ndp
->ni_dvp
= NULLVP
;
413 * Check if symbolic link restarts us at the root
415 if (*(cnp
->cn_nameptr
) == '/') {
416 while (*(cnp
->cn_nameptr
) == '/') {
420 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
427 * only come here if we fail to handle a SYMLINK...
428 * if either ni_dvp or ni_vp is non-NULL, then
429 * we need to drop the iocount that was picked
430 * up in the lookup routine
433 vnode_put(ndp
->ni_dvp
);
435 vnode_put(ndp
->ni_vp
);
437 if ( (cnp
->cn_flags
& HASBUF
) ) {
438 cnp
->cn_flags
&= ~HASBUF
;
439 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
441 cnp
->cn_pnbuf
= NULL
;
443 if (error
== ERECYCLE
){
444 /* vnode was recycled underneath us. re-drive lookup to start at
445 the beginning again, since recycling invalidated last lookup*/
446 ndp
->ni_cnd
.cn_flags
= cnpflags
;
447 ndp
->ni_dvp
= usedvp
;
458 * This is a very central and rather complicated routine.
460 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
461 * The starting directory is taken from ni_startdir. The pathname is
462 * descended until done, or a symbolic link is encountered. The variable
463 * ni_more is clear if the path is completed; it is set to one if a
464 * symbolic link needing interpretation is encountered.
466 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
467 * whether the name is to be looked up, created, renamed, or deleted.
468 * When CREATE, RENAME, or DELETE is specified, information usable in
469 * creating, renaming, or deleting a directory entry may be calculated.
470 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
471 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
472 * returned unlocked. Otherwise the parent directory is not returned. If
473 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
474 * the target is returned locked, otherwise it is returned unlocked.
475 * When creating or renaming and LOCKPARENT is specified, the target may not
476 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
478 * Overall outline of lookup:
481 * identify next component of name at ndp->ni_ptr
482 * handle degenerate case where name is null string
483 * if .. and crossing mount points and on mounted filesys, find parent
484 * call VNOP_LOOKUP routine for next component name
485 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
486 * component vnode returned in ni_vp (if it exists), locked.
487 * if result vnode is mounted on and crossing mount points,
488 * find mounted on vnode
489 * if more components of name, do next level at dirloop
490 * return the answer in ni_vp, locked if LOCKLEAF set
491 * if LOCKPARENT set, return locked parent in ni_dvp
492 * if WANTPARENT set, return unlocked parent in ni_dvp
495 * ENOENT No such file or directory
496 * EBADF Bad file descriptor
497 * ENOTDIR Not a directory
498 * EROFS Read-only file system [CREATE]
499 * EISDIR Is a directory [CREATE]
500 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
501 * vnode_authorize:EROFS
502 * vnode_authorize:EACCES
503 * vnode_authorize:EPERM
504 * vnode_authorize:???
505 * VNOP_LOOKUP:ENOENT No such file or directory
506 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
513 lookup(struct nameidata
*ndp
)
515 char *cp
; /* pointer into pathname argument */
516 vnode_t tdp
; /* saved dp */
517 vnode_t dp
; /* the directory we are searching */
518 mount_t mp
; /* mount table entry */
519 int docache
= 1; /* == 0 do not cache last component */
520 int wantparent
; /* 1 => wantparent or lockparent flag */
521 int rdonly
; /* lookup read-only flag bit */
522 int trailing_slash
= 0;
523 int dp_authorized
= 0;
525 struct componentname
*cnp
= &ndp
->ni_cnd
;
526 vfs_context_t ctx
= cnp
->cn_context
;
527 int mounted_on_depth
= 0;
528 int dont_cache_mp
= 0;
529 vnode_t mounted_on_dp
= NULLVP
;
530 int current_mount_generation
= 0;
532 int nc_generation
= 0;
533 vnode_t last_dp
= NULLVP
;
536 * Setup: break out flag bits into variables.
538 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
539 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
542 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
543 rdonly
= cnp
->cn_flags
& RDONLY
;
544 cnp
->cn_flags
&= ~ISSYMLINK
;
547 dp
= ndp
->ni_startdir
;
548 ndp
->ni_startdir
= NULLVP
;
550 if ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0)
551 vbusyflags
= LK_NOWAIT
;
552 cp
= cnp
->cn_nameptr
;
555 if ( (vnode_getwithref(dp
)) ) {
565 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
, last_dp
)) ) {
569 if ((cnp
->cn_flags
& ISLASTCN
)) {
571 cnp
->cn_flags
|= MAKEENTRY
;
573 cnp
->cn_flags
|= MAKEENTRY
;
577 if (ndp
->ni_vp
!= NULLVP
) {
579 * cache_lookup_path returned a non-NULL ni_vp then,
580 * we're guaranteed that the dp is a VDIR, it's
581 * been authorized, and vp is not ".."
583 * make sure we don't try to enter the name back into
584 * the cache if this vp is purged before we get to that
585 * check since we won't have serialized behind whatever
586 * activity is occurring in the FS that caused the purge
589 nc_generation
= dp
->v_nc_generation
- 1;
591 goto returned_from_lookup_path
;
595 * Handle "..": two special cases.
596 * 1. If at root directory (e.g. after chroot)
597 * or at absolute root directory
598 * then ignore it so can't get out.
599 * 2. If this vnode is the root of a mounted
600 * filesystem, then replace it with the
601 * vnode which was mounted on so we take the
602 * .. in the other file system.
604 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
606 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
610 * we're pinned at the root
611 * we've already got one reference on 'dp'
612 * courtesy of cache_lookup_path... take
613 * another one for the ".."
614 * if we fail to get the new reference, we'll
615 * drop our original down in 'bad'
617 if ( (vnode_get(dp
)) ) {
623 if ((dp
->v_flag
& VROOT
) == 0 ||
624 (cnp
->cn_flags
& NOCROSSMOUNT
))
626 if (dp
->v_mount
== NULL
) { /* forced umount */
631 dp
= tdp
->v_mount
->mnt_vnodecovered
;
635 if ( (vnode_getwithref(dp
)) ) {
646 * We now have a segment name to search for, and a directory to search.
651 if (dp
->v_type
!= VDIR
) {
655 if ( (cnp
->cn_flags
& DONOTAUTH
) != DONOTAUTH
) {
656 if (!dp_authorized
) {
657 error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
662 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
665 #endif /* CONFIG_MACF */
668 nc_generation
= dp
->v_nc_generation
;
670 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
672 if ((error
== ENOENT
) &&
673 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
674 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
676 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
677 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
678 unlock_fsnode(dp
, NULL
);
680 #endif /* __LP64__ */
682 dp
= tdp
->v_mount
->mnt_vnodecovered
;
686 if ( (vnode_getwithref(dp
)) ) {
696 if (error
!= EJUSTRETURN
)
699 if (ndp
->ni_vp
!= NULLVP
)
700 panic("leaf should be empty");
703 * If creating and at end of pathname, then can consider
704 * allowing file to be created.
710 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
715 * We return with ni_vp NULL to indicate that the entry
716 * doesn't currently exist, leaving a pointer to the
717 * referenced directory vnode in ndp->ni_dvp.
719 if (cnp
->cn_flags
& SAVESTART
) {
720 if ( (vnode_get(ndp
->ni_dvp
)) ) {
724 ndp
->ni_startdir
= ndp
->ni_dvp
;
727 vnode_put(ndp
->ni_dvp
);
730 kdebug_lookup(ndp
->ni_dvp
, cnp
);
733 returned_from_lookup_path
:
737 * Take into account any additional components consumed by
738 * the underlying filesystem.
740 if (cnp
->cn_consume
> 0) {
741 cnp
->cn_nameptr
+= cnp
->cn_consume
;
742 ndp
->ni_next
+= cnp
->cn_consume
;
743 ndp
->ni_pathlen
-= cnp
->cn_consume
;
747 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
749 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
750 int update_flags
= 0;
752 if (isdot_or_dotdot
== 0) {
753 if (dp
->v_name
== NULL
)
754 update_flags
|= VNODE_UPDATE_NAME
;
755 if (ndp
->ni_dvp
!= NULLVP
&& dp
->v_parent
== NULLVP
)
756 update_flags
|= VNODE_UPDATE_PARENT
;
759 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
762 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
764 * missing from name cache, but should
765 * be in it... this can happen if volfs
766 * causes the vnode to be created or the
767 * name cache entry got recycled but the
769 * check to make sure that ni_dvp is valid
770 * cache_lookup_path may return a NULL
771 * do a quick check to see if the generation of the
772 * directory matches our snapshot... this will get
773 * rechecked behind the name cache lock, but if it
774 * already fails to match, no need to go any further
776 if (ndp
->ni_dvp
!= NULLVP
&& (nc_generation
== ndp
->ni_dvp
->v_nc_generation
) && (!isdot_or_dotdot
))
777 cache_enter_with_gen(ndp
->ni_dvp
, dp
, cnp
, nc_generation
);
782 mounted_on_depth
= 0;
784 current_mount_generation
= mount_generation
;
786 * Check to see if the vnode has been mounted on...
787 * if so find the root of the mounted file system.
790 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
791 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
795 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
796 struct uthread
*uth
= (struct uthread
*)get_bsdthread_info(current_thread());
802 if (vfs_busy(mp
, vbusyflags
)) {
803 mount_dropcrossref(mp
, dp
, 0);
804 if (vbusyflags
== LK_NOWAIT
) {
808 goto check_mounted_on
;
812 * XXX - if this is the last component of the
813 * pathname, and it's either not a lookup operation
814 * or the NOTRIGGER flag is set for the operation,
815 * set a uthread flag to let VFS_ROOT() for autofs
816 * know it shouldn't trigger a mount.
818 if ((cnp
->cn_flags
& ISLASTCN
) &&
819 (cnp
->cn_nameiop
!= LOOKUP
||
820 (cnp
->cn_flags
& NOTRIGGER
))) {
821 uth
->uu_notrigger
= 1;
824 error
= VFS_ROOT(mp
, &tdp
, ctx
);
825 /* XXX - clear the uthread flag */
826 uth
->uu_notrigger
= 0;
828 * mount_dropcrossref does a vnode_put
829 * on dp if the 3rd arg is non-zero
831 mount_dropcrossref(mp
, dp
, 1);
838 ndp
->ni_vp
= dp
= tdp
;
841 goto check_mounted_on
;
847 if (vfs_flags(vnode_mount(dp
)) & MNT_MULTILABEL
) {
848 error
= vnode_label(vnode_mount(dp
), NULL
, dp
, NULL
, 0, ctx
);
854 if (mounted_on_depth
&& !dont_cache_mp
) {
855 mp
= mounted_on_dp
->v_mountedhere
;
859 mp
->mnt_realrootvp_vid
= dp
->v_id
;
860 mp
->mnt_realrootvp
= dp
;
861 mp
->mnt_generation
= current_mount_generation
;
867 * Check for symbolic link
869 if ((dp
->v_type
== VLNK
) &&
870 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
871 cnp
->cn_flags
|= ISSYMLINK
;
876 * Check for bogus trailing slashes.
878 if (trailing_slash
) {
879 if (dp
->v_type
!= VDIR
) {
888 * Not a symbolic link. If more pathname,
889 * continue at next component, else return.
891 if (*ndp
->ni_next
== '/') {
892 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
894 while (*cnp
->cn_nameptr
== '/') {
898 vnode_put(ndp
->ni_dvp
);
900 cp
= cnp
->cn_nameptr
;
906 * cache_lookup_path is now responsible for dropping io ref on dp
907 * when it is called again in the dirloop. This ensures we hold
908 * a ref on dp until we complete the next round of lookup.
915 * Disallow directory write attempts on read-only file systems.
918 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
922 if (cnp
->cn_flags
& SAVESTART
) {
924 * note that we already hold a reference
925 * on both dp and ni_dvp, but for some reason
926 * can't get another one... in this case we
927 * need to do vnode_put on dp in 'bad2'
929 if ( (vnode_get(ndp
->ni_dvp
)) ) {
933 ndp
->ni_startdir
= ndp
->ni_dvp
;
935 if (!wantparent
&& ndp
->ni_dvp
) {
936 vnode_put(ndp
->ni_dvp
);
937 ndp
->ni_dvp
= NULLVP
;
940 if (cnp
->cn_flags
& AUDITVNPATH1
)
941 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
942 else if (cnp
->cn_flags
& AUDITVNPATH2
)
943 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
947 * Caller wants the resource fork.
949 if ((cnp
->cn_flags
& CN_WANTSRSRCFORK
) && (dp
!= NULLVP
)) {
950 vnode_t svp
= NULLVP
;
951 enum nsoperation nsop
;
953 if (dp
->v_type
!= VREG
) {
957 switch (cnp
->cn_nameiop
) {
959 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
967 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
975 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
976 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
987 /* Ask the file system for the resource fork. */
988 error
= vnode_getnamedstream(dp
, &svp
, XATTR_RESOURCEFORK_NAME
, nsop
, 0, ctx
);
990 /* During a create, it OK for stream vnode to be missing. */
991 if (error
== ENOATTR
|| error
== ENOENT
) {
992 error
= (nsop
== NS_CREATE
) ? 0 : ENOENT
;
997 /* The "parent" of the stream is the file. */
1001 if (ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
) {
1002 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
1003 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1005 #endif /* __LP64__ */
1006 vnode_put(ndp
->ni_dvp
);
1012 ndp
->ni_vp
= dp
= svp
; /* on create this may be null */
1014 /* Restore the truncated pathname buffer (for audits). */
1015 if (ndp
->ni_pathlen
== 1 && ndp
->ni_next
[0] == '\0') {
1016 ndp
->ni_next
[0] = '/';
1018 cnp
->cn_flags
&= ~MAKEENTRY
;
1022 kdebug_lookup(dp
, cnp
);
1026 cnp
->cn_namelen
= 0;
1028 * A degenerate name (e.g. / or "") which is a way of
1029 * talking about a directory, e.g. like "/." or ".".
1031 if (dp
->v_type
!= VDIR
) {
1035 if (cnp
->cn_nameiop
!= LOOKUP
) {
1041 * note that we already hold a reference
1042 * on dp, but for some reason can't
1043 * get another one... in this case we
1044 * need to do vnode_put on dp in 'bad'
1046 if ( (vnode_get(dp
)) ) {
1052 cnp
->cn_flags
&= ~ISDOTDOT
;
1053 cnp
->cn_flags
|= ISLASTCN
;
1057 if (cnp
->cn_flags
& AUDITVNPATH1
)
1058 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
1059 else if (cnp
->cn_flags
& AUDITVNPATH2
)
1060 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
1061 if (cnp
->cn_flags
& SAVESTART
)
1062 panic("lookup: SAVESTART");
1067 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1068 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1069 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1071 #endif /* __LP64__ */
1073 vnode_put(ndp
->ni_dvp
);
1076 ndp
->ni_vp
= NULLVP
;
1079 kdebug_lookup(dp
, cnp
);
1084 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1085 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1086 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1088 #endif /* __LP64__ */
1091 ndp
->ni_vp
= NULLVP
;
1094 kdebug_lookup(dp
, cnp
);
1099 * relookup - lookup a path name component
1100 * Used by lookup to re-aquire things.
1103 relookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1105 struct vnode
*dp
= NULL
; /* the directory we are searching */
1106 int wantparent
; /* 1 => wantparent or lockparent flag */
1107 int rdonly
; /* lookup read-only flag bit */
1109 #ifdef NAMEI_DIAGNOSTIC
1110 int i
, newhash
; /* DEBUG: check name hash */
1111 char *cp
; /* DEBUG: check name ptr/len */
1113 vfs_context_t ctx
= cnp
->cn_context
;;
1116 * Setup: break out flag bits into variables.
1118 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
1119 rdonly
= cnp
->cn_flags
& RDONLY
;
1120 cnp
->cn_flags
&= ~ISSYMLINK
;
1122 if (cnp
->cn_flags
& NOCACHE
)
1123 cnp
->cn_flags
&= ~MAKEENTRY
;
1125 cnp
->cn_flags
|= MAKEENTRY
;
1130 * Check for degenerate name (e.g. / or "")
1131 * which is a way of talking about a directory,
1132 * e.g. like "/." or ".".
1134 if (cnp
->cn_nameptr
[0] == '\0') {
1135 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
1139 if (dp
->v_type
!= VDIR
) {
1143 if ( (vnode_get(dp
)) ) {
1149 if (cnp
->cn_flags
& SAVESTART
)
1150 panic("lookup: SAVESTART");
1154 * We now have a segment name to search for, and a directory to search.
1156 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
1157 if (error
!= EJUSTRETURN
)
1161 panic("leaf should be empty");
1164 * If creating and at end of pathname, then can consider
1165 * allowing file to be created.
1172 * We return with ni_vp NULL to indicate that the entry
1173 * doesn't currently exist, leaving a pointer to the
1174 * (possibly locked) directory inode in ndp->ni_dvp.
1182 * Check for symbolic link
1184 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
1185 panic ("relookup: symlink found.\n");
1189 * Disallow directory write attempts on read-only file systems.
1192 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
1196 /* ASSERT(dvp == ndp->ni_startdir) */
1209 * Free pathname buffer
1212 nameidone(struct nameidata
*ndp
)
1215 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
1216 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
1217 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1219 #endif /* __LP64__ */
1220 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
1221 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
1223 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
1224 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
1225 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
1233 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1234 * by fs_usage. The path up to and including the current component name are
1235 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1236 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1237 * That is, the truncation removes the leading portion of the path.
1239 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1240 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1241 * (in addition to DBG_FUNC_START if it is also the first). There may be
1242 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1244 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1245 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1246 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1247 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1248 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1249 * path) is set to zero bytes, or '>' if there is more path beyond the
1250 * current component name (usually because an intermediate component was not
1253 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1254 * 12+N*16, there will be no padding.
1256 * TODO: If there is more path beyond the current component name, should we
1257 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1258 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1259 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1262 kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
)
1268 long dbg_parms
[NUMPARMS
];
1270 /* Collect the pathname for tracing */
1271 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
1272 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
1274 if (dbg_namelen
> (int)sizeof(dbg_parms
))
1275 dbg_namelen
= sizeof(dbg_parms
);
1276 dbg_nameptr
-= dbg_namelen
;
1278 /* Copy the (possibly truncated) path itself */
1279 memcpy(dbg_parms
, dbg_nameptr
, dbg_namelen
);
1281 /* Pad with '\0' or '>' */
1282 if (dbg_namelen
< (int)sizeof(dbg_parms
)) {
1283 memset((char *)dbg_parms
+ dbg_namelen
,
1284 *(cnp
->cn_nameptr
+ cnp
->cn_namelen
) ? '>' : 0,
1285 sizeof(dbg_parms
) - dbg_namelen
);
1289 * In the event that we collect multiple, consecutive pathname
1290 * entries, we must mark the start of the path's string and the end.
1292 code
= (FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
;
1294 if (dbg_namelen
<= 12)
1295 code
|= DBG_FUNC_END
;
1297 KERNEL_DEBUG_CONSTANT(code
, dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
1299 code
&= ~DBG_FUNC_START
;
1301 for (i
=3, dbg_namelen
-= 12; dbg_namelen
> 0; i
+=4, dbg_namelen
-= 16) {
1302 if (dbg_namelen
<= 16)
1303 code
|= DBG_FUNC_END
;
1305 KERNEL_DEBUG_CONSTANT(code
, dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);
1310 * Obtain the real path from a legacy volfs style path.
1312 * Valid formats of input path:
1317 * "555/123456/foobar"
1320 * 555 represents the volfs file system id
1321 * '@' and '2' are aliases to the root of a file system
1322 * 123456 represents a file id
1323 * "foobar" represents a file name
1327 vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
)
1330 struct mount
*mp
= NULL
;
1338 /* Get file system id and move str to next component. */
1339 id
= strtoul(path
, &str
, 10);
1340 if (id
== 0 || str
[0] != '/') {
1343 while (*str
== '/') {
1348 mp
= mount_lookupby_volfsid(id
, 1);
1350 return (EINVAL
); /* unexpected failure */
1352 /* Check for an alias to a file system root. */
1353 if (ch
== '@' && str
[1] == '\0') {
1357 /* Get file id and move str to next component. */
1358 ino
= strtouq(str
, &str
, 10);
1361 /* Get the target vnode. */
1363 error
= VFS_ROOT(mp
, &vp
, ctx
);
1365 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
1373 /* Get the absolute path to this vnode. */
1374 error
= build_path(vp
, realpath
, bufsize
, &length
, 0, ctx
);
1377 if (error
== 0 && *str
!= '\0') {
1378 int attempt
= strlcat(realpath
, str
, MAXPATHLEN
);
1379 if (attempt
> MAXPATHLEN
){
1380 error
= ENAMETOOLONG
;