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
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
);
163 /* XXX ut should be from context */
164 uthread_t ut
= (struct uthread
*)get_bsdthread_info(current_thread());
166 char uio_buf
[ UIO_SIZEOF(1) ];
169 if (!vfs_context_ucred(ctx
) || !p
)
170 panic ("namei: bad cred/proc");
171 if (cnp
->cn_nameiop
& (~OPMASK
))
172 panic ("namei: nameiop contaminated with flags");
173 if (cnp
->cn_flags
& OPMASK
)
174 panic ("namei: flags contaminated with nameiops");
181 * Get a buffer for the name to be translated, and copy the
182 * name into the buffer.
184 if ((cnp
->cn_flags
& HASBUF
) == 0) {
185 cnp
->cn_pnbuf
= ndp
->ni_pathbuf
;
186 cnp
->cn_pnlen
= PATHBUFLEN
;
189 if (IS_VALID_UIO_SEGFLG(ndp
->ni_segflg
) == 0) {
190 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
192 #endif /* LP64_DEBUG */
195 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
196 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
197 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
199 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
200 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
202 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
203 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
204 if (cnp
->cn_pnbuf
== NULL
) {
209 cnp
->cn_flags
|= HASBUF
;
210 cnp
->cn_pnlen
= MAXPATHLEN
;
219 * Check for legacy volfs style pathnames.
221 * For compatibility reasons we currently allow these paths,
222 * but future versions of the OS may not support them.
224 if (ndp
->ni_pathlen
>= VOLFS_MIN_PATH_LEN
&&
225 cnp
->cn_pnbuf
[0] == '/' &&
226 cnp
->cn_pnbuf
[1] == '.' &&
227 cnp
->cn_pnbuf
[2] == 'v' &&
228 cnp
->cn_pnbuf
[3] == 'o' &&
229 cnp
->cn_pnbuf
[4] == 'l' &&
230 cnp
->cn_pnbuf
[5] == '/' ) {
233 /* Attempt to resolve a legacy volfs style pathname. */
234 MALLOC_ZONE(realpath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
236 if ((realpath_err
= vfs_getrealpath(&cnp
->cn_pnbuf
[6], realpath
, MAXPATHLEN
, ctx
))) {
237 FREE_ZONE(realpath
, MAXPATHLEN
, M_NAMEI
);
238 if (realpath_err
== ENOSPC
){
239 error
= ENAMETOOLONG
;
243 if (cnp
->cn_flags
& HASBUF
) {
244 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
246 cnp
->cn_pnbuf
= realpath
;
247 cnp
->cn_pnlen
= MAXPATHLEN
;
248 ndp
->ni_pathlen
= strlen(realpath
) + 1;
249 cnp
->cn_flags
|= HASBUF
| CN_VOLFSPATH
;
253 #endif /* CONFIG_VOLFS */
255 /* If we are auditing the kernel pathname, save the user pathname */
256 if (cnp
->cn_flags
& AUDITVNPATH1
)
257 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH1
);
258 if (cnp
->cn_flags
& AUDITVNPATH2
)
259 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH2
);
262 * Do not allow empty pathnames
264 if (*cnp
->cn_pnbuf
== '\0') {
271 * determine the starting point for the translation.
273 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
274 if ( !(fdp
->fd_flags
& FD_CHROOT
))
275 ndp
->ni_rootdir
= rootvnode
;
277 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
279 ndp
->ni_usedvp
= NULLVP
;
281 if (*(cnp
->cn_nameptr
) == '/') {
282 while (*(cnp
->cn_nameptr
) == '/') {
286 dp
= ndp
->ni_rootdir
;
287 } else if (cnp
->cn_flags
& USEDVP
) {
291 dp
= vfs_context_cwd(ctx
);
293 if (dp
== NULLVP
|| (dp
->v_lflag
& VL_DEAD
)) {
297 ndp
->ni_dvp
= NULLVP
;
304 ndp
->ni_startdir
= dp
;
306 if ( (error
= lookup(ndp
)) ) {
310 * Check for symbolic link
312 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
315 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
316 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
317 unlock_fsnode(ndp
->ni_dvp
, NULL
);
319 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
324 if ((error
= mac_vnode_check_readlink(ctx
, ndp
->ni_vp
)) != 0)
327 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
332 if (need_newpathbuf
) {
333 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
341 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
343 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
345 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
348 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
351 // LP64todo - fix this
352 linklen
= MAXPATHLEN
- uio_resid(auio
);
353 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
355 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
357 error
= ENAMETOOLONG
;
360 if (need_newpathbuf
) {
361 long len
= cnp
->cn_pnlen
;
363 tmppn
= cnp
->cn_pnbuf
;
364 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
366 cnp
->cn_pnlen
= MAXPATHLEN
;
368 if ( (cnp
->cn_flags
& HASBUF
) )
369 FREE_ZONE(tmppn
, len
, M_NAMEI
);
371 cnp
->cn_flags
|= HASBUF
;
373 cnp
->cn_pnbuf
[linklen
] = '\0';
375 ndp
->ni_pathlen
+= linklen
;
376 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
379 * starting point for 'relative'
384 * get rid of references returned via 'lookup'
386 vnode_put(ndp
->ni_vp
);
387 vnode_put(ndp
->ni_dvp
);
390 ndp
->ni_dvp
= NULLVP
;
393 * Check if symbolic link restarts us at the root
395 if (*(cnp
->cn_nameptr
) == '/') {
396 while (*(cnp
->cn_nameptr
) == '/') {
400 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
407 * only come here if we fail to handle a SYMLINK...
408 * if either ni_dvp or ni_vp is non-NULL, then
409 * we need to drop the iocount that was picked
410 * up in the lookup routine
413 vnode_put(ndp
->ni_dvp
);
415 vnode_put(ndp
->ni_vp
);
417 if ( (cnp
->cn_flags
& HASBUF
) ) {
418 cnp
->cn_flags
&= ~HASBUF
;
419 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
421 cnp
->cn_pnbuf
= NULL
;
423 if (error
== ERECYCLE
){
424 /* vnode was recycled underneath us. re-drive lookup to start at
425 the beginning again, since recycling invalidated last lookup*/
426 ndp
->ni_cnd
.cn_flags
= cnpflags
;
427 ndp
->ni_dvp
= usedvp
;
438 * This is a very central and rather complicated routine.
440 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
441 * The starting directory is taken from ni_startdir. The pathname is
442 * descended until done, or a symbolic link is encountered. The variable
443 * ni_more is clear if the path is completed; it is set to one if a
444 * symbolic link needing interpretation is encountered.
446 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
447 * whether the name is to be looked up, created, renamed, or deleted.
448 * When CREATE, RENAME, or DELETE is specified, information usable in
449 * creating, renaming, or deleting a directory entry may be calculated.
450 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
451 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
452 * returned unlocked. Otherwise the parent directory is not returned. If
453 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
454 * the target is returned locked, otherwise it is returned unlocked.
455 * When creating or renaming and LOCKPARENT is specified, the target may not
456 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
458 * Overall outline of lookup:
461 * identify next component of name at ndp->ni_ptr
462 * handle degenerate case where name is null string
463 * if .. and crossing mount points and on mounted filesys, find parent
464 * call VNOP_LOOKUP routine for next component name
465 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
466 * component vnode returned in ni_vp (if it exists), locked.
467 * if result vnode is mounted on and crossing mount points,
468 * find mounted on vnode
469 * if more components of name, do next level at dirloop
470 * return the answer in ni_vp, locked if LOCKLEAF set
471 * if LOCKPARENT set, return locked parent in ni_dvp
472 * if WANTPARENT set, return unlocked parent in ni_dvp
475 * ENOENT No such file or directory
476 * EBADF Bad file descriptor
477 * ENOTDIR Not a directory
478 * EROFS Read-only file system [CREATE]
479 * EISDIR Is a directory [CREATE]
480 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
481 * vnode_authorize:EROFS
482 * vnode_authorize:EACCES
483 * vnode_authorize:EPERM
484 * vnode_authorize:???
485 * VNOP_LOOKUP:ENOENT No such file or directory
486 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
493 lookup(struct nameidata
*ndp
)
495 char *cp
; /* pointer into pathname argument */
496 vnode_t tdp
; /* saved dp */
497 vnode_t dp
; /* the directory we are searching */
498 mount_t mp
; /* mount table entry */
499 int docache
= 1; /* == 0 do not cache last component */
500 int wantparent
; /* 1 => wantparent or lockparent flag */
501 int rdonly
; /* lookup read-only flag bit */
502 int trailing_slash
= 0;
503 int dp_authorized
= 0;
505 struct componentname
*cnp
= &ndp
->ni_cnd
;
506 vfs_context_t ctx
= cnp
->cn_context
;
507 int mounted_on_depth
= 0;
508 int dont_cache_mp
= 0;
509 vnode_t mounted_on_dp
= NULLVP
;
510 int current_mount_generation
= 0;
512 int nc_generation
= 0;
513 vnode_t last_dp
= NULLVP
;
516 * Setup: break out flag bits into variables.
518 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
519 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
522 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
523 rdonly
= cnp
->cn_flags
& RDONLY
;
524 cnp
->cn_flags
&= ~ISSYMLINK
;
527 dp
= ndp
->ni_startdir
;
528 ndp
->ni_startdir
= NULLVP
;
530 if ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0)
531 vbusyflags
= LK_NOWAIT
;
532 cp
= cnp
->cn_nameptr
;
535 if ( (vnode_getwithref(dp
)) ) {
545 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
, last_dp
)) ) {
549 if ((cnp
->cn_flags
& ISLASTCN
)) {
551 cnp
->cn_flags
|= MAKEENTRY
;
553 cnp
->cn_flags
|= MAKEENTRY
;
557 if (ndp
->ni_vp
!= NULLVP
) {
559 * cache_lookup_path returned a non-NULL ni_vp then,
560 * we're guaranteed that the dp is a VDIR, it's
561 * been authorized, and vp is not ".."
563 * make sure we don't try to enter the name back into
564 * the cache if this vp is purged before we get to that
565 * check since we won't have serialized behind whatever
566 * activity is occurring in the FS that caused the purge
569 nc_generation
= dp
->v_nc_generation
- 1;
571 goto returned_from_lookup_path
;
575 * Handle "..": two special cases.
576 * 1. If at root directory (e.g. after chroot)
577 * or at absolute root directory
578 * then ignore it so can't get out.
579 * 2. If this vnode is the root of a mounted
580 * filesystem, then replace it with the
581 * vnode which was mounted on so we take the
582 * .. in the other file system.
584 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
586 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
590 * we're pinned at the root
591 * we've already got one reference on 'dp'
592 * courtesy of cache_lookup_path... take
593 * another one for the ".."
594 * if we fail to get the new reference, we'll
595 * drop our original down in 'bad'
597 if ( (vnode_get(dp
)) ) {
603 if ((dp
->v_flag
& VROOT
) == 0 ||
604 (cnp
->cn_flags
& NOCROSSMOUNT
))
606 if (dp
->v_mount
== NULL
) { /* forced umount */
611 dp
= tdp
->v_mount
->mnt_vnodecovered
;
615 if ( (vnode_getwithref(dp
)) ) {
626 * We now have a segment name to search for, and a directory to search.
631 if (dp
->v_type
!= VDIR
) {
635 if ( (cnp
->cn_flags
& DONOTAUTH
) != DONOTAUTH
) {
636 if (!dp_authorized
) {
637 error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
642 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
645 #endif /* CONFIG_MACF */
648 nc_generation
= dp
->v_nc_generation
;
650 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
652 if ((error
== ENOENT
) &&
653 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
654 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
655 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
656 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
657 unlock_fsnode(dp
, NULL
);
660 dp
= tdp
->v_mount
->mnt_vnodecovered
;
664 if ( (vnode_getwithref(dp
)) ) {
674 if (error
!= EJUSTRETURN
)
677 if (ndp
->ni_vp
!= NULLVP
)
678 panic("leaf should be empty");
681 * If creating and at end of pathname, then can consider
682 * allowing file to be created.
688 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
693 * We return with ni_vp NULL to indicate that the entry
694 * doesn't currently exist, leaving a pointer to the
695 * referenced directory vnode in ndp->ni_dvp.
697 if (cnp
->cn_flags
& SAVESTART
) {
698 if ( (vnode_get(ndp
->ni_dvp
)) ) {
702 ndp
->ni_startdir
= ndp
->ni_dvp
;
705 vnode_put(ndp
->ni_dvp
);
708 kdebug_lookup(ndp
->ni_dvp
, cnp
);
711 returned_from_lookup_path
:
715 * Take into account any additional components consumed by
716 * the underlying filesystem.
718 if (cnp
->cn_consume
> 0) {
719 cnp
->cn_nameptr
+= cnp
->cn_consume
;
720 ndp
->ni_next
+= cnp
->cn_consume
;
721 ndp
->ni_pathlen
-= cnp
->cn_consume
;
724 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
726 int update_flags
= 0;
728 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
730 if (isdot_or_dotdot
== 0) {
731 if (dp
->v_name
== NULL
)
732 update_flags
|= VNODE_UPDATE_NAME
;
733 if (ndp
->ni_dvp
!= NULLVP
&& dp
->v_parent
== NULLVP
)
734 update_flags
|= VNODE_UPDATE_PARENT
;
737 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
740 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
742 * missing from name cache, but should
743 * be in it... this can happen if volfs
744 * causes the vnode to be created or the
745 * name cache entry got recycled but the
747 * check to make sure that ni_dvp is valid
748 * cache_lookup_path may return a NULL
749 * do a quick check to see if the generation of the
750 * directory matches our snapshot... this will get
751 * rechecked behind the name cache lock, but if it
752 * already fails to match, no need to go any further
754 if (ndp
->ni_dvp
!= NULLVP
&& (nc_generation
== ndp
->ni_dvp
->v_nc_generation
))
755 cache_enter_with_gen(ndp
->ni_dvp
, dp
, cnp
, nc_generation
);
760 mounted_on_depth
= 0;
762 current_mount_generation
= mount_generation
;
764 * Check to see if the vnode has been mounted on...
765 * if so find the root of the mounted file system.
768 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
769 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
773 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
774 struct uthread
*uth
= (struct uthread
*)get_bsdthread_info(current_thread());
780 if (vfs_busy(mp
, vbusyflags
)) {
781 mount_dropcrossref(mp
, dp
, 0);
782 if (vbusyflags
== LK_NOWAIT
) {
786 goto check_mounted_on
;
790 * XXX - if this is the last component of the
791 * pathname, and it's either not a lookup operation
792 * or the NOTRIGGER flag is set for the operation,
793 * set a uthread flag to let VFS_ROOT() for autofs
794 * know it shouldn't trigger a mount.
796 if ((cnp
->cn_flags
& ISLASTCN
) &&
797 (cnp
->cn_nameiop
!= LOOKUP
||
798 (cnp
->cn_flags
& NOTRIGGER
))) {
799 uth
->uu_notrigger
= 1;
802 error
= VFS_ROOT(mp
, &tdp
, ctx
);
803 /* XXX - clear the uthread flag */
804 uth
->uu_notrigger
= 0;
806 * mount_dropcrossref does a vnode_put
807 * on dp if the 3rd arg is non-zero
809 mount_dropcrossref(mp
, dp
, 1);
816 ndp
->ni_vp
= dp
= tdp
;
819 goto check_mounted_on
;
825 if (vfs_flags(vnode_mount(dp
)) & MNT_MULTILABEL
) {
826 error
= vnode_label(vnode_mount(dp
), NULL
, dp
, NULL
,
827 VNODE_LABEL_NEEDREF
, ctx
);
833 if (mounted_on_depth
&& !dont_cache_mp
) {
834 mp
= mounted_on_dp
->v_mountedhere
;
838 mp
->mnt_realrootvp_vid
= dp
->v_id
;
839 mp
->mnt_realrootvp
= dp
;
840 mp
->mnt_generation
= current_mount_generation
;
846 * Check for symbolic link
848 if ((dp
->v_type
== VLNK
) &&
849 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
850 cnp
->cn_flags
|= ISSYMLINK
;
855 * Check for bogus trailing slashes.
857 if (trailing_slash
) {
858 if (dp
->v_type
!= VDIR
) {
867 * Not a symbolic link. If more pathname,
868 * continue at next component, else return.
870 if (*ndp
->ni_next
== '/') {
871 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
873 while (*cnp
->cn_nameptr
== '/') {
877 vnode_put(ndp
->ni_dvp
);
879 cp
= cnp
->cn_nameptr
;
885 * cache_lookup_path is now responsible for dropping io ref on dp
886 * when it is called again in the dirloop. This ensures we hold
887 * a ref on dp until we complete the next round of lookup.
894 * Disallow directory write attempts on read-only file systems.
897 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
901 if (cnp
->cn_flags
& SAVESTART
) {
903 * note that we already hold a reference
904 * on both dp and ni_dvp, but for some reason
905 * can't get another one... in this case we
906 * need to do vnode_put on dp in 'bad2'
908 if ( (vnode_get(ndp
->ni_dvp
)) ) {
912 ndp
->ni_startdir
= ndp
->ni_dvp
;
914 if (!wantparent
&& ndp
->ni_dvp
) {
915 vnode_put(ndp
->ni_dvp
);
916 ndp
->ni_dvp
= NULLVP
;
919 if (cnp
->cn_flags
& AUDITVNPATH1
)
920 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
921 else if (cnp
->cn_flags
& AUDITVNPATH2
)
922 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
926 * Caller wants the resource fork.
928 if ((cnp
->cn_flags
& CN_WANTSRSRCFORK
) && (dp
!= NULLVP
)) {
929 vnode_t svp
= NULLVP
;
930 enum nsoperation nsop
;
932 if (dp
->v_type
!= VREG
) {
936 switch (cnp
->cn_nameiop
) {
938 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
947 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
956 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
957 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
968 /* Ask the file system for the resource fork. */
969 error
= vnode_getnamedstream(dp
, &svp
, XATTR_RESOURCEFORK_NAME
, nsop
, 0, ctx
);
971 /* During a create, it OK for stream vnode to be missing. */
972 if (error
== ENOATTR
|| error
== ENOENT
) {
973 error
= (nsop
== NS_CREATE
) ? 0 : ENOENT
;
978 /* The "parent" of the stream is the file. */
981 if (ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
) {
982 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
983 unlock_fsnode(ndp
->ni_dvp
, NULL
);
985 vnode_put(ndp
->ni_dvp
);
991 ndp
->ni_vp
= dp
= svp
; /* on create this may be null */
993 /* Restore the truncated pathname buffer (for audits). */
994 if (ndp
->ni_pathlen
== 1 && ndp
->ni_next
[0] == '\0') {
995 ndp
->ni_next
[0] = '/';
997 cnp
->cn_flags
&= ~MAKEENTRY
;
1001 kdebug_lookup(dp
, cnp
);
1005 cnp
->cn_namelen
= 0;
1007 * A degenerate name (e.g. / or "") which is a way of
1008 * talking about a directory, e.g. like "/." or ".".
1010 if (dp
->v_type
!= VDIR
) {
1014 if (cnp
->cn_nameiop
!= LOOKUP
) {
1020 * note that we already hold a reference
1021 * on dp, but for some reason can't
1022 * get another one... in this case we
1023 * need to do vnode_put on dp in 'bad'
1025 if ( (vnode_get(dp
)) ) {
1031 cnp
->cn_flags
&= ~ISDOTDOT
;
1032 cnp
->cn_flags
|= ISLASTCN
;
1036 if (cnp
->cn_flags
& AUDITVNPATH1
)
1037 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
1038 else if (cnp
->cn_flags
& AUDITVNPATH2
)
1039 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
1040 if (cnp
->cn_flags
& SAVESTART
)
1041 panic("lookup: SAVESTART");
1045 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1046 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1047 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1050 vnode_put(ndp
->ni_dvp
);
1053 ndp
->ni_vp
= NULLVP
;
1056 kdebug_lookup(dp
, cnp
);
1060 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1061 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1062 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1066 ndp
->ni_vp
= NULLVP
;
1069 kdebug_lookup(dp
, cnp
);
1074 * relookup - lookup a path name component
1075 * Used by lookup to re-aquire things.
1078 relookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1080 struct vnode
*dp
= NULL
; /* the directory we are searching */
1081 int wantparent
; /* 1 => wantparent or lockparent flag */
1082 int rdonly
; /* lookup read-only flag bit */
1084 #ifdef NAMEI_DIAGNOSTIC
1085 int i
, newhash
; /* DEBUG: check name hash */
1086 char *cp
; /* DEBUG: check name ptr/len */
1088 vfs_context_t ctx
= cnp
->cn_context
;;
1091 * Setup: break out flag bits into variables.
1093 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
1094 rdonly
= cnp
->cn_flags
& RDONLY
;
1095 cnp
->cn_flags
&= ~ISSYMLINK
;
1097 if (cnp
->cn_flags
& NOCACHE
)
1098 cnp
->cn_flags
&= ~MAKEENTRY
;
1100 cnp
->cn_flags
|= MAKEENTRY
;
1105 * Check for degenerate name (e.g. / or "")
1106 * which is a way of talking about a directory,
1107 * e.g. like "/." or ".".
1109 if (cnp
->cn_nameptr
[0] == '\0') {
1110 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
1114 if (dp
->v_type
!= VDIR
) {
1118 if ( (vnode_get(dp
)) ) {
1124 if (cnp
->cn_flags
& SAVESTART
)
1125 panic("lookup: SAVESTART");
1129 * We now have a segment name to search for, and a directory to search.
1131 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
1132 if (error
!= EJUSTRETURN
)
1136 panic("leaf should be empty");
1139 * If creating and at end of pathname, then can consider
1140 * allowing file to be created.
1147 * We return with ni_vp NULL to indicate that the entry
1148 * doesn't currently exist, leaving a pointer to the
1149 * (possibly locked) directory inode in ndp->ni_dvp.
1157 * Check for symbolic link
1159 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
1160 panic ("relookup: symlink found.\n");
1164 * Disallow directory write attempts on read-only file systems.
1167 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
1171 /* ASSERT(dvp == ndp->ni_startdir) */
1184 * Free pathname buffer
1187 nameidone(struct nameidata
*ndp
)
1189 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
1190 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
1191 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1193 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
1194 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
1196 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
1197 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
1198 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
1206 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1207 * by fs_usage. The path up to and including the current component name are
1208 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1209 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1210 * That is, the truncation removes the leading portion of the path.
1212 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1213 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1214 * (in addition to DBG_FUNC_START if it is also the first). There may be
1215 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1217 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1218 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1219 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1220 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1221 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1222 * path) is set to zero bytes, or '>' if there is more path beyond the
1223 * current component name (usually because an intermediate component was not
1226 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1227 * 12+N*16, there will be no padding.
1229 * TODO: If there is more path beyond the current component name, should we
1230 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1231 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1232 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1235 kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
)
1241 long dbg_parms
[NUMPARMS
];
1243 /* Collect the pathname for tracing */
1244 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
1245 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
1247 if (dbg_namelen
> (int)sizeof(dbg_parms
))
1248 dbg_namelen
= sizeof(dbg_parms
);
1249 dbg_nameptr
-= dbg_namelen
;
1251 /* Copy the (possibly truncated) path itself */
1252 memcpy(dbg_parms
, dbg_nameptr
, dbg_namelen
);
1254 /* Pad with '\0' or '>' */
1255 if (dbg_namelen
< (int)sizeof(dbg_parms
)) {
1256 memset((char *)dbg_parms
+ dbg_namelen
,
1257 *(cnp
->cn_nameptr
+ cnp
->cn_namelen
) ? '>' : 0,
1258 sizeof(dbg_parms
) - dbg_namelen
);
1262 * In the event that we collect multiple, consecutive pathname
1263 * entries, we must mark the start of the path's string and the end.
1265 code
= (FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
;
1267 if (dbg_namelen
<= 12)
1268 code
|= DBG_FUNC_END
;
1270 KERNEL_DEBUG_CONSTANT(code
, (unsigned int)dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
1272 code
&= ~DBG_FUNC_START
;
1274 for (i
=3, dbg_namelen
-= 12; dbg_namelen
> 0; i
+=4, dbg_namelen
-= 16) {
1275 if (dbg_namelen
<= 16)
1276 code
|= DBG_FUNC_END
;
1278 KERNEL_DEBUG_CONSTANT(code
, dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);
1283 * Obtain the real path from a legacy volfs style path.
1285 * Valid formats of input path:
1290 * "555/123456/foobar"
1293 * 555 represents the volfs file system id
1294 * '@' and '2' are aliases to the root of a file system
1295 * 123456 represents a file id
1296 * "foobar" represents a file name
1300 vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
)
1303 struct mount
*mp
= NULL
;
1311 /* Get file system id and move str to next component. */
1312 id
= strtoul(path
, &str
, 10);
1313 if (id
== 0 || str
[0] != '/') {
1316 while (*str
== '/') {
1321 mp
= mount_lookupby_volfsid(id
, 1);
1323 return (EINVAL
); /* unexpected failure */
1325 /* Check for an alias to a file system root. */
1326 if (ch
== '@' && str
[1] == '\0') {
1330 /* Get file id and move str to next component. */
1331 ino
= strtouq(str
, &str
, 10);
1334 /* Get the target vnode. */
1336 error
= VFS_ROOT(mp
, &vp
, ctx
);
1338 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
1346 /* Get the absolute path to this vnode. */
1347 error
= build_path(vp
, realpath
, bufsize
, &length
, 0, ctx
);
1350 if (error
== 0 && *str
!= '\0') {
1351 int attempt
= strlcat(realpath
, str
, MAXPATHLEN
);
1352 if (attempt
> MAXPATHLEN
){
1353 error
= ENAMETOOLONG
;