2 * Copyright (c) 2000-2010 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
);
114 boolean_t
lookup_continue_ok(struct nameidata
*ndp
);
115 int lookup_traverse_mountpoints(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
, int vbusyflags
, vfs_context_t ctx
);
116 int lookup_handle_symlink(struct nameidata
*ndp
, vnode_t
*new_dp
, vfs_context_t ctx
);
117 int lookup_authorize_search(vnode_t dp
, struct componentname
*cnp
, int dp_authorized_in_cache
, vfs_context_t ctx
);
118 void lookup_consider_update_cache(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, int nc_generation
);
119 int lookup_handle_rsrc_fork(vnode_t dp
, struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
, vfs_context_t ctx
);
120 int lookup_handle_found_vnode(struct nameidata
*ndp
, struct componentname
*cnp
, int rdonly
,
121 int vbusyflags
, int *keep_going
, int nc_generation
,
122 int wantparent
, int atroot
, vfs_context_t ctx
);
123 int lookup_handle_emptyname(struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
);
126 * Convert a pathname into a pointer to a locked inode.
128 * The FOLLOW flag is set when symbolic links are to be followed
129 * when they occur at the end of the name translation process.
130 * Symbolic links are always followed for all other pathname
131 * components other than the last.
133 * The segflg defines whether the name is to be copied from user
134 * space or kernel space.
136 * Overall outline of namei:
139 * get starting directory
140 * while (!done && !error) {
141 * call lookup to search path.
142 * if symbolic link, massage name in buffer and continue
146 * ENOENT No such file or directory
147 * ELOOP Too many levels of symbolic links
148 * ENAMETOOLONG Filename too long
149 * copyinstr:EFAULT Bad address
150 * copyinstr:ENAMETOOLONG Filename too long
151 * lookup:EBADF Bad file descriptor
155 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
156 * This means we should re-drive lookup from this point.
161 namei(struct nameidata
*ndp
)
163 struct filedesc
*fdp
; /* pointer to file descriptor state */
164 struct vnode
*dp
; /* the directory we are searching */
165 struct vnode
*usedvp
= ndp
->ni_dvp
; /* store pointer to vp in case we must loop due to
166 heavy vnode pressure */
167 u_long cnpflags
= ndp
->ni_cnd
.cn_flags
; /* store in case we have to restore after loop */
169 struct componentname
*cnp
= &ndp
->ni_cnd
;
170 vfs_context_t ctx
= cnp
->cn_context
;
171 proc_t p
= vfs_context_proc(ctx
);
173 /* XXX ut should be from context */
174 uthread_t ut
= (struct uthread
*)get_bsdthread_info(current_thread());
180 if (!vfs_context_ucred(ctx
) || !p
)
181 panic ("namei: bad cred/proc");
182 if (cnp
->cn_nameiop
& (~OPMASK
))
183 panic ("namei: nameiop contaminated with flags");
184 if (cnp
->cn_flags
& OPMASK
)
185 panic ("namei: flags contaminated with nameiops");
189 * A compound VNOP found something that needs further processing:
190 * either a trigger vnode, a covered directory, or a symlink.
192 if (ndp
->ni_flag
& NAMEI_CONTLOOKUP
) {
193 int rdonly
, vbusyflags
, keep_going
, wantparent
;
195 rdonly
= cnp
->cn_flags
& RDONLY
;
196 vbusyflags
= ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0) ? LK_NOWAIT
: 0;
198 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
200 ndp
->ni_flag
&= ~(NAMEI_CONTLOOKUP
);
202 error
= lookup_handle_found_vnode(ndp
, &ndp
->ni_cnd
, rdonly
, vbusyflags
,
203 &keep_going
, ndp
->ni_ncgeneration
, wantparent
, 0, ctx
);
207 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
208 panic("We need to keep going on a continued lookup, but for vp type %d (tag %d)\n", ndp
->ni_vp
->v_type
, ndp
->ni_vp
->v_tag
);
210 goto continue_symlink
;
220 * Get a buffer for the name to be translated, and copy the
221 * name into the buffer.
223 if ((cnp
->cn_flags
& HASBUF
) == 0) {
224 cnp
->cn_pnbuf
= ndp
->ni_pathbuf
;
225 cnp
->cn_pnlen
= PATHBUFLEN
;
228 if ((UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
) == 0)
229 && (ndp
->ni_segflg
!= UIO_SYSSPACE
)
230 && (ndp
->ni_segflg
!= UIO_SYSSPACE32
)) {
231 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
233 #endif /* LP64_DEBUG */
236 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
237 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
238 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
240 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
241 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
243 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
244 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
245 if (cnp
->cn_pnbuf
== NULL
) {
250 cnp
->cn_flags
|= HASBUF
;
251 cnp
->cn_pnlen
= MAXPATHLEN
;
260 * Check for legacy volfs style pathnames.
262 * For compatibility reasons we currently allow these paths,
263 * but future versions of the OS may not support them.
265 if (ndp
->ni_pathlen
>= VOLFS_MIN_PATH_LEN
&&
266 cnp
->cn_pnbuf
[0] == '/' &&
267 cnp
->cn_pnbuf
[1] == '.' &&
268 cnp
->cn_pnbuf
[2] == 'v' &&
269 cnp
->cn_pnbuf
[3] == 'o' &&
270 cnp
->cn_pnbuf
[4] == 'l' &&
271 cnp
->cn_pnbuf
[5] == '/' ) {
274 /* Attempt to resolve a legacy volfs style pathname. */
275 MALLOC_ZONE(realpath
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
278 * We only error out on the ENAMETOOLONG cases where we know that
279 * vfs_getrealpath translation succeeded but the path could not fit into
280 * MAXPATHLEN characters. In other failure cases, we may be dealing with a path
281 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
283 if ((realpath_err
= vfs_getrealpath(&cnp
->cn_pnbuf
[6], realpath
, MAXPATHLEN
, ctx
))) {
284 FREE_ZONE(realpath
, MAXPATHLEN
, M_NAMEI
);
285 if (realpath_err
== ENOSPC
|| realpath_err
== ENAMETOOLONG
){
286 error
= ENAMETOOLONG
;
290 if (cnp
->cn_flags
& HASBUF
) {
291 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
293 cnp
->cn_pnbuf
= realpath
;
294 cnp
->cn_pnlen
= MAXPATHLEN
;
295 ndp
->ni_pathlen
= strlen(realpath
) + 1;
296 cnp
->cn_flags
|= HASBUF
| CN_VOLFSPATH
;
300 #endif /* CONFIG_VOLFS */
303 /* If we are auditing the kernel pathname, save the user pathname */
304 if (cnp
->cn_flags
& AUDITVNPATH1
)
305 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH1
);
306 if (cnp
->cn_flags
& AUDITVNPATH2
)
307 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH2
);
308 #endif /* CONFIG_AUDIT */
311 * Do not allow empty pathnames
313 if (*cnp
->cn_pnbuf
== '\0') {
320 * determine the starting point for the translation.
322 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
323 if ( !(fdp
->fd_flags
& FD_CHROOT
))
324 ndp
->ni_rootdir
= rootvnode
;
326 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
328 ndp
->ni_usedvp
= NULLVP
;
330 if (*(cnp
->cn_nameptr
) == '/') {
331 while (*(cnp
->cn_nameptr
) == '/') {
335 dp
= ndp
->ni_rootdir
;
336 } else if (cnp
->cn_flags
& USEDVP
) {
340 dp
= vfs_context_cwd(ctx
);
342 if (dp
== NULLVP
|| (dp
->v_lflag
& VL_DEAD
)) {
346 ndp
->ni_dvp
= NULLVP
;
350 ndp
->ni_startdir
= dp
;
352 if ( (error
= lookup(ndp
)) ) {
356 * Check for symbolic link
358 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
363 /* Gives us a new path to process, and a starting dir */
364 error
= lookup_handle_symlink(ndp
, &dp
, ctx
);
370 * only come here if we fail to handle a SYMLINK...
371 * if either ni_dvp or ni_vp is non-NULL, then
372 * we need to drop the iocount that was picked
373 * up in the lookup routine
377 vnode_put(ndp
->ni_dvp
);
379 vnode_put(ndp
->ni_vp
);
381 if ( (cnp
->cn_flags
& HASBUF
) ) {
382 cnp
->cn_flags
&= ~HASBUF
;
383 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
385 cnp
->cn_pnbuf
= NULL
;
387 ndp
->ni_dvp
= NULLVP
;
388 if (error
== ERECYCLE
){
389 /* vnode was recycled underneath us. re-drive lookup to start at
390 the beginning again, since recycling invalidated last lookup*/
391 ndp
->ni_cnd
.cn_flags
= cnpflags
;
392 ndp
->ni_dvp
= usedvp
;
401 namei_compound_available(vnode_t dp
, struct nameidata
*ndp
)
403 if ((ndp
->ni_flag
& NAMEI_COMPOUNDOPEN
) != 0) {
404 return vnode_compound_open_available(dp
);
410 lookup_authorize_search(vnode_t dp
, struct componentname
*cnp
, int dp_authorized_in_cache
, vfs_context_t ctx
)
414 if (!dp_authorized_in_cache
) {
415 error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
420 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
423 #endif /* CONFIG_MACF */
429 lookup_consider_update_cache(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, int nc_generation
)
432 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
434 if (vp
->v_name
== NULL
|| vp
->v_parent
== NULLVP
) {
435 int update_flags
= 0;
437 if (isdot_or_dotdot
== 0) {
438 if (vp
->v_name
== NULL
)
439 update_flags
|= VNODE_UPDATE_NAME
;
440 if (dvp
!= NULLVP
&& vp
->v_parent
== NULLVP
)
441 update_flags
|= VNODE_UPDATE_PARENT
;
444 vnode_update_identity(vp
, dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
447 if ( (cnp
->cn_flags
& MAKEENTRY
) && (vp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&vp
->v_nclinks
) == NULL
) {
449 * missing from name cache, but should
450 * be in it... this can happen if volfs
451 * causes the vnode to be created or the
452 * name cache entry got recycled but the
454 * check to make sure that ni_dvp is valid
455 * cache_lookup_path may return a NULL
456 * do a quick check to see if the generation of the
457 * directory matches our snapshot... this will get
458 * rechecked behind the name cache lock, but if it
459 * already fails to match, no need to go any further
461 if (dvp
!= NULLVP
&& (nc_generation
== dvp
->v_nc_generation
) && (!isdot_or_dotdot
))
462 cache_enter_with_gen(dvp
, vp
, cnp
, nc_generation
);
469 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
470 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
471 * (if one was provided).
474 lookup_handle_rsrc_fork(vnode_t dp
, struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
, vfs_context_t ctx
)
476 vnode_t svp
= NULLVP
;
477 enum nsoperation nsop
;
480 if (dp
->v_type
!= VREG
) {
484 switch (cnp
->cn_nameiop
) {
486 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
494 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
502 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
503 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
514 /* Ask the file system for the resource fork. */
515 error
= vnode_getnamedstream(dp
, &svp
, XATTR_RESOURCEFORK_NAME
, nsop
, 0, ctx
);
517 /* During a create, it OK for stream vnode to be missing. */
518 if (error
== ENOATTR
|| error
== ENOENT
) {
519 error
= (nsop
== NS_CREATE
) ? 0 : ENOENT
;
524 /* The "parent" of the stream is the file. */
528 if (ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
) {
529 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
530 unlock_fsnode(ndp
->ni_dvp
, NULL
);
532 #endif /* __LP64__ */
533 vnode_put(ndp
->ni_dvp
);
539 ndp
->ni_vp
= svp
; /* on create this may be null */
541 /* Restore the truncated pathname buffer (for audits). */
542 if (ndp
->ni_pathlen
== 1 && ndp
->ni_next
[0] == '\0') {
543 ndp
->ni_next
[0] = '/';
545 cnp
->cn_flags
&= ~MAKEENTRY
;
551 #endif /* NAMEDRSRCFORK */
555 * --One on ni_vp. One on ni_dvp if there is more path, or we didn't come through the
556 * cache, or we came through the cache and the caller doesn't want the parent.
559 * --Leaves us in the correct state for the next step, whatever that might be.
560 * --If we find a symlink, returns with iocounts on both ni_vp and ni_dvp.
561 * --If we are to look up another component, then we have an iocount on ni_vp and
563 * --If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags.
564 * --In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount
568 lookup_handle_found_vnode(struct nameidata
*ndp
, struct componentname
*cnp
, int rdonly
,
569 int vbusyflags
, int *keep_going
, int nc_generation
,
570 int wantparent
, int atroot
, vfs_context_t ctx
)
579 if (ndp
->ni_vp
== NULLVP
) {
580 panic("NULL ni_vp in %s\n", __FUNCTION__
);
589 error
= vnode_trigger_resolve(dp
, ndp
, ctx
);
594 #endif /* CONFIG_TRIGGERS */
597 * Take into account any additional components consumed by
598 * the underlying filesystem.
600 if (cnp
->cn_consume
> 0) {
601 cnp
->cn_nameptr
+= cnp
->cn_consume
;
602 ndp
->ni_next
+= cnp
->cn_consume
;
603 ndp
->ni_pathlen
-= cnp
->cn_consume
;
606 lookup_consider_update_cache(ndp
->ni_dvp
, dp
, cnp
, nc_generation
);
610 * Check to see if the vnode has been mounted on...
611 * if so find the root of the mounted file system.
612 * Updates ndp->ni_vp.
614 error
= lookup_traverse_mountpoints(ndp
, cnp
, dp
, vbusyflags
, ctx
);
621 if (vfs_flags(vnode_mount(dp
)) & MNT_MULTILABEL
) {
622 error
= vnode_label(vnode_mount(dp
), NULL
, dp
, NULL
, 0, ctx
);
629 * Check for symbolic link
631 if ((dp
->v_type
== VLNK
) &&
632 ((cnp
->cn_flags
& FOLLOW
) || (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) || *ndp
->ni_next
== '/')) {
633 cnp
->cn_flags
|= ISSYMLINK
;
639 * Check for bogus trailing slashes.
641 if ((ndp
->ni_flag
& NAMEI_TRAILINGSLASH
)) {
642 if (dp
->v_type
!= VDIR
) {
646 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
651 * Not a symbolic link. If more pathname,
652 * continue at next component, else return.
654 * Definitely have a dvp if there's another slash
656 if (*ndp
->ni_next
== '/') {
657 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
659 while (*cnp
->cn_nameptr
== '/') {
664 cp
= cnp
->cn_nameptr
;
665 vnode_put(ndp
->ni_dvp
);
666 ndp
->ni_dvp
= NULLVP
;
677 * Disallow directory write attempts on read-only file systems.
680 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
685 /* If SAVESTART is set, we should have a dvp */
686 if (cnp
->cn_flags
& SAVESTART
) {
688 * note that we already hold a reference
689 * on both dp and ni_dvp, but for some reason
690 * can't get another one... in this case we
691 * need to do vnode_put on dp in 'bad2'
693 if ( (vnode_get(ndp
->ni_dvp
)) ) {
697 ndp
->ni_startdir
= ndp
->ni_dvp
;
699 if (!wantparent
&& ndp
->ni_dvp
) {
700 vnode_put(ndp
->ni_dvp
);
701 ndp
->ni_dvp
= NULLVP
;
704 if (cnp
->cn_flags
& AUDITVNPATH1
)
705 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
706 else if (cnp
->cn_flags
& AUDITVNPATH2
)
707 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
711 * Caller wants the resource fork.
713 if ((cnp
->cn_flags
& CN_WANTSRSRCFORK
) && (dp
!= NULLVP
)) {
714 error
= lookup_handle_rsrc_fork(dp
, ndp
, cnp
, wantparent
, ctx
);
722 kdebug_lookup(dp
, cnp
);
727 error
= lookup_handle_emptyname(ndp
, cnp
, wantparent
);
738 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
741 lookup_handle_emptyname(struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
)
749 * A degenerate name (e.g. / or "") which is a way of
750 * talking about a directory, e.g. like "/." or ".".
752 if (dp
->v_type
!= VDIR
) {
756 if (cnp
->cn_nameiop
!= LOOKUP
) {
762 * note that we already hold a reference
763 * on dp, but for some reason can't
764 * get another one... in this case we
765 * need to do vnode_put on dp in 'bad'
767 if ( (vnode_get(dp
)) ) {
773 cnp
->cn_flags
&= ~ISDOTDOT
;
774 cnp
->cn_flags
|= ISLASTCN
;
775 ndp
->ni_next
= cnp
->cn_nameptr
;
778 if (cnp
->cn_flags
& AUDITVNPATH1
)
779 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
780 else if (cnp
->cn_flags
& AUDITVNPATH2
)
781 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
782 if (cnp
->cn_flags
& SAVESTART
)
783 panic("lookup: SAVESTART");
791 * This is a very central and rather complicated routine.
793 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
794 * The starting directory is taken from ni_startdir. The pathname is
795 * descended until done, or a symbolic link is encountered. The variable
796 * ni_more is clear if the path is completed; it is set to one if a
797 * symbolic link needing interpretation is encountered.
799 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
800 * whether the name is to be looked up, created, renamed, or deleted.
801 * When CREATE, RENAME, or DELETE is specified, information usable in
802 * creating, renaming, or deleting a directory entry may be calculated.
803 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
804 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
805 * returned unlocked. Otherwise the parent directory is not returned. If
806 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
807 * the target is returned locked, otherwise it is returned unlocked.
808 * When creating or renaming and LOCKPARENT is specified, the target may not
809 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
811 * Overall outline of lookup:
814 * identify next component of name at ndp->ni_ptr
815 * handle degenerate case where name is null string
816 * if .. and crossing mount points and on mounted filesys, find parent
817 * call VNOP_LOOKUP routine for next component name
818 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
819 * component vnode returned in ni_vp (if it exists), locked.
820 * if result vnode is mounted on and crossing mount points,
821 * find mounted on vnode
822 * if more components of name, do next level at dirloop
823 * return the answer in ni_vp, locked if LOCKLEAF set
824 * if LOCKPARENT set, return locked parent in ni_dvp
825 * if WANTPARENT set, return unlocked parent in ni_dvp
828 * ENOENT No such file or directory
829 * EBADF Bad file descriptor
830 * ENOTDIR Not a directory
831 * EROFS Read-only file system [CREATE]
832 * EISDIR Is a directory [CREATE]
833 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
834 * vnode_authorize:EROFS
835 * vnode_authorize:EACCES
836 * vnode_authorize:EPERM
837 * vnode_authorize:???
838 * VNOP_LOOKUP:ENOENT No such file or directory
839 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
846 lookup(struct nameidata
*ndp
)
848 char *cp
; /* pointer into pathname argument */
849 vnode_t tdp
; /* saved dp */
850 vnode_t dp
; /* the directory we are searching */
851 int docache
= 1; /* == 0 do not cache last component */
852 int wantparent
; /* 1 => wantparent or lockparent flag */
853 int rdonly
; /* lookup read-only flag bit */
854 int dp_authorized
= 0;
856 struct componentname
*cnp
= &ndp
->ni_cnd
;
857 vfs_context_t ctx
= cnp
->cn_context
;
859 int nc_generation
= 0;
860 vnode_t last_dp
= NULLVP
;
865 * Setup: break out flag bits into variables.
867 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
868 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
871 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
872 rdonly
= cnp
->cn_flags
& RDONLY
;
873 cnp
->cn_flags
&= ~ISSYMLINK
;
876 dp
= ndp
->ni_startdir
;
877 ndp
->ni_startdir
= NULLVP
;
879 if ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0)
880 vbusyflags
= LK_NOWAIT
;
881 cp
= cnp
->cn_nameptr
;
884 if ( (vnode_getwithref(dp
)) ) {
890 error
= lookup_handle_emptyname(ndp
, cnp
, wantparent
);
901 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &dp_authorized
, last_dp
)) ) {
905 if ((cnp
->cn_flags
& ISLASTCN
)) {
907 cnp
->cn_flags
|= MAKEENTRY
;
909 cnp
->cn_flags
|= MAKEENTRY
;
913 if (ndp
->ni_vp
!= NULLVP
) {
915 * cache_lookup_path returned a non-NULL ni_vp then,
916 * we're guaranteed that the dp is a VDIR, it's
917 * been authorized, and vp is not ".."
919 * make sure we don't try to enter the name back into
920 * the cache if this vp is purged before we get to that
921 * check since we won't have serialized behind whatever
922 * activity is occurring in the FS that caused the purge
925 nc_generation
= dp
->v_nc_generation
- 1;
927 goto returned_from_lookup_path
;
931 * Handle "..": two special cases.
932 * 1. If at root directory (e.g. after chroot)
933 * or at absolute root directory
934 * then ignore it so can't get out.
935 * 2. If this vnode is the root of a mounted
936 * filesystem, then replace it with the
937 * vnode which was mounted on so we take the
938 * .. in the other file system.
940 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
942 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
946 * we're pinned at the root
947 * we've already got one reference on 'dp'
948 * courtesy of cache_lookup_path... take
949 * another one for the ".."
950 * if we fail to get the new reference, we'll
951 * drop our original down in 'bad'
953 if ( (vnode_get(dp
)) ) {
958 goto returned_from_lookup_path
;
960 if ((dp
->v_flag
& VROOT
) == 0 ||
961 (cnp
->cn_flags
& NOCROSSMOUNT
))
963 if (dp
->v_mount
== NULL
) { /* forced umount */
968 dp
= tdp
->v_mount
->mnt_vnodecovered
;
972 if ( (vnode_getwithref(dp
)) ) {
983 * We now have a segment name to search for, and a directory to search.
988 if (dp
->v_type
!= VDIR
) {
992 if ( (cnp
->cn_flags
& DONOTAUTH
) != DONOTAUTH
) {
993 error
= lookup_authorize_search(dp
, cnp
, dp_authorized
, ctx
);
1000 * Now that we've authorized a lookup, can bail out if the filesystem
1001 * will be doing a batched operation. Return an iocount on dvp.
1004 if ((cnp
->cn_flags
& ISLASTCN
) && namei_compound_available(dp
, ndp
) && !(cnp
->cn_flags
& CN_WANTSRSRCFORK
)) {
1006 if ((cnp
->cn_flags
& ISLASTCN
) && namei_compound_available(dp
, ndp
)) {
1007 #endif /* NAMEDRSRCFORK */
1008 ndp
->ni_flag
|= NAMEI_UNFINISHED
;
1009 ndp
->ni_ncgeneration
= dp
->v_nc_generation
;
1013 nc_generation
= dp
->v_nc_generation
;
1015 error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
);
1020 if ((error
== ENOENT
) &&
1021 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
1022 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
1024 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1025 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1026 unlock_fsnode(dp
, NULL
);
1028 #endif /* __LP64__ */
1030 dp
= tdp
->v_mount
->mnt_vnodecovered
;
1034 if ( (vnode_getwithref(dp
)) ) {
1044 if (error
!= EJUSTRETURN
)
1047 if (ndp
->ni_vp
!= NULLVP
)
1048 panic("leaf should be empty");
1050 error
= lookup_validate_creation_path(ndp
);
1054 * We return with ni_vp NULL to indicate that the entry
1055 * doesn't currently exist, leaving a pointer to the
1056 * referenced directory vnode in ndp->ni_dvp.
1058 if (cnp
->cn_flags
& SAVESTART
) {
1059 if ( (vnode_get(ndp
->ni_dvp
)) ) {
1063 ndp
->ni_startdir
= ndp
->ni_dvp
;
1066 vnode_put(ndp
->ni_dvp
);
1069 kdebug_lookup(ndp
->ni_dvp
, cnp
);
1072 returned_from_lookup_path
:
1073 /* We'll always have an iocount on ni_vp when this finishes. */
1074 error
= lookup_handle_found_vnode(ndp
, cnp
, rdonly
, vbusyflags
, &keep_going
, nc_generation
, wantparent
, atroot
, ctx
);
1082 /* namei() will handle symlinks */
1083 if ((dp
->v_type
== VLNK
) &&
1084 ((cnp
->cn_flags
& FOLLOW
) || (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) || *ndp
->ni_next
== '/')) {
1089 * Otherwise, there's more path to process.
1090 * cache_lookup_path is now responsible for dropping io ref on dp
1091 * when it is called again in the dirloop. This ensures we hold
1092 * a ref on dp until we complete the next round of lookup.
1102 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1103 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1104 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1106 #endif /* __LP64__ */
1108 vnode_put(ndp
->ni_dvp
);
1110 vnode_put(ndp
->ni_vp
);
1111 ndp
->ni_vp
= NULLVP
;
1114 kdebug_lookup(dp
, cnp
);
1119 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1120 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1121 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1123 #endif /* __LP64__ */
1126 ndp
->ni_vp
= NULLVP
;
1129 kdebug_lookup(dp
, cnp
);
1134 lookup_validate_creation_path(struct nameidata
*ndp
)
1136 struct componentname
*cnp
= &ndp
->ni_cnd
;
1139 * If creating and at end of pathname, then can consider
1140 * allowing file to be created.
1142 if (cnp
->cn_flags
& RDONLY
) {
1145 if ((cnp
->cn_flags
& ISLASTCN
) && (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) && !(cnp
->cn_flags
& WILLBEDIR
)) {
1153 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1156 lookup_traverse_mountpoints(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
1157 int vbusyflags
, vfs_context_t ctx
)
1164 int dont_cache_mp
= 0;
1165 vnode_t mounted_on_dp
;
1166 int current_mount_generation
= 0;
1169 current_mount_generation
= mount_generation
;
1171 while ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
1172 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
1175 * For a trigger vnode, call its resolver when crossing its mount (if requested)
1177 if (dp
->v_resolve
) {
1178 (void) vnode_trigger_resolve(dp
, ndp
, ctx
);
1183 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
1189 if (vfs_busy(mp
, vbusyflags
)) {
1190 mount_dropcrossref(mp
, dp
, 0);
1191 if (vbusyflags
== LK_NOWAIT
) {
1201 * XXX - if this is the last component of the
1202 * pathname, and it's either not a lookup operation
1203 * or the NOTRIGGER flag is set for the operation,
1204 * set a uthread flag to let VFS_ROOT() for autofs
1205 * know it shouldn't trigger a mount.
1207 uth
= (struct uthread
*)get_bsdthread_info(current_thread());
1208 if ((cnp
->cn_flags
& ISLASTCN
) &&
1209 (cnp
->cn_nameiop
!= LOOKUP
||
1210 (cnp
->cn_flags
& NOTRIGGER
))) {
1211 uth
->uu_notrigger
= 1;
1215 error
= VFS_ROOT(mp
, &tdp
, ctx
);
1216 /* XXX - clear the uthread flag */
1217 uth
->uu_notrigger
= 0;
1219 mount_dropcrossref(mp
, dp
, 0);
1227 ndp
->ni_vp
= dp
= tdp
;
1232 * Check if root dir is a trigger vnode
1234 if (dp
->v_resolve
) {
1235 error
= vnode_trigger_resolve(dp
, ndp
, ctx
);
1248 if (depth
&& !dont_cache_mp
) {
1249 mp
= mounted_on_dp
->v_mountedhere
;
1252 mount_lock_spin(mp
);
1253 mp
->mnt_realrootvp_vid
= dp
->v_id
;
1254 mp
->mnt_realrootvp
= dp
;
1255 mp
->mnt_generation
= current_mount_generation
;
1267 * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location
1268 * at which to start a lookup with a resolved path, and all other iocounts dropped.
1271 lookup_handle_symlink(struct nameidata
*ndp
, vnode_t
*new_dp
, vfs_context_t ctx
)
1274 char *cp
; /* pointer into pathname argument */
1276 char uio_buf
[ UIO_SIZEOF(1) ];
1277 int need_newpathbuf
;
1279 struct componentname
*cnp
= &ndp
->ni_cnd
;
1284 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
1285 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
1286 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1288 #endif /* __LP64__ */
1290 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
1294 if ((error
= mac_vnode_check_readlink(ctx
, ndp
->ni_vp
)) != 0)
1297 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
1298 need_newpathbuf
= 1;
1300 need_newpathbuf
= 0;
1302 if (need_newpathbuf
) {
1303 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1310 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
1312 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
1314 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
1316 if (need_newpathbuf
)
1317 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
1322 * Safe to set unsigned with a [larger] signed type here
1323 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1326 linklen
= MAXPATHLEN
- (u_int
)uio_resid(auio
);
1327 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
1328 if (need_newpathbuf
)
1329 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
1331 return ENAMETOOLONG
;
1333 if (need_newpathbuf
) {
1334 long len
= cnp
->cn_pnlen
;
1336 tmppn
= cnp
->cn_pnbuf
;
1337 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
1339 cnp
->cn_pnlen
= MAXPATHLEN
;
1341 if ( (cnp
->cn_flags
& HASBUF
) )
1342 FREE_ZONE(tmppn
, len
, M_NAMEI
);
1344 cnp
->cn_flags
|= HASBUF
;
1346 cnp
->cn_pnbuf
[linklen
] = '\0';
1348 ndp
->ni_pathlen
+= linklen
;
1349 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
1352 * starting point for 'relative'
1353 * symbolic link path
1358 * get rid of references returned via 'lookup'
1360 vnode_put(ndp
->ni_vp
);
1361 vnode_put(ndp
->ni_dvp
); /* ALWAYS have a dvp for a symlink */
1363 ndp
->ni_vp
= NULLVP
;
1364 ndp
->ni_dvp
= NULLVP
;
1367 * Check if symbolic link restarts us at the root
1369 if (*(cnp
->cn_nameptr
) == '/') {
1370 while (*(cnp
->cn_nameptr
) == '/') {
1374 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
1385 * relookup - lookup a path name component
1386 * Used by lookup to re-aquire things.
1389 relookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1391 struct vnode
*dp
= NULL
; /* the directory we are searching */
1392 int wantparent
; /* 1 => wantparent or lockparent flag */
1393 int rdonly
; /* lookup read-only flag bit */
1395 #ifdef NAMEI_DIAGNOSTIC
1396 int i
, newhash
; /* DEBUG: check name hash */
1397 char *cp
; /* DEBUG: check name ptr/len */
1399 vfs_context_t ctx
= cnp
->cn_context
;;
1402 * Setup: break out flag bits into variables.
1404 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
1405 rdonly
= cnp
->cn_flags
& RDONLY
;
1406 cnp
->cn_flags
&= ~ISSYMLINK
;
1408 if (cnp
->cn_flags
& NOCACHE
)
1409 cnp
->cn_flags
&= ~MAKEENTRY
;
1411 cnp
->cn_flags
|= MAKEENTRY
;
1416 * Check for degenerate name (e.g. / or "")
1417 * which is a way of talking about a directory,
1418 * e.g. like "/." or ".".
1420 if (cnp
->cn_nameptr
[0] == '\0') {
1421 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
1425 if (dp
->v_type
!= VDIR
) {
1429 if ( (vnode_get(dp
)) ) {
1435 if (cnp
->cn_flags
& SAVESTART
)
1436 panic("lookup: SAVESTART");
1440 * We now have a segment name to search for, and a directory to search.
1442 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
1443 if (error
!= EJUSTRETURN
)
1447 panic("leaf should be empty");
1450 * If creating and at end of pathname, then can consider
1451 * allowing file to be created.
1458 * We return with ni_vp NULL to indicate that the entry
1459 * doesn't currently exist, leaving a pointer to the
1460 * (possibly locked) directory inode in ndp->ni_dvp.
1468 * Check for symbolic link
1470 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
1471 panic ("relookup: symlink found.\n");
1475 * Disallow directory write attempts on read-only file systems.
1478 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
1482 /* ASSERT(dvp == ndp->ni_startdir) */
1495 namei_unlock_fsnode(struct nameidata
*ndp
)
1498 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
1499 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
1500 unlock_fsnode(ndp
->ni_dvp
, NULL
);
1504 #endif /* __LP64__ */
1508 * Free pathname buffer
1511 nameidone(struct nameidata
*ndp
)
1513 namei_unlock_fsnode(ndp
);
1515 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
1516 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
1518 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
1519 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
1520 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
1528 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1529 * by fs_usage. The path up to and including the current component name are
1530 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1531 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1532 * That is, the truncation removes the leading portion of the path.
1534 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1535 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1536 * (in addition to DBG_FUNC_START if it is also the first). There may be
1537 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1539 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1540 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1541 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1542 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1543 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1544 * path) is set to zero bytes, or '>' if there is more path beyond the
1545 * current component name (usually because an intermediate component was not
1548 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1549 * 12+N*16, there will be no padding.
1551 * TODO: If there is more path beyond the current component name, should we
1552 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1553 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1554 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1556 #if !defined(NO_KDEBUG)
1558 kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
)
1564 long dbg_parms
[NUMPARMS
];
1566 /* Collect the pathname for tracing */
1567 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
1568 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
1570 if (dbg_namelen
> (int)sizeof(dbg_parms
))
1571 dbg_namelen
= sizeof(dbg_parms
);
1572 dbg_nameptr
-= dbg_namelen
;
1574 /* Copy the (possibly truncated) path itself */
1575 memcpy(dbg_parms
, dbg_nameptr
, dbg_namelen
);
1577 /* Pad with '\0' or '>' */
1578 if (dbg_namelen
< (int)sizeof(dbg_parms
)) {
1579 memset((char *)dbg_parms
+ dbg_namelen
,
1580 *(cnp
->cn_nameptr
+ cnp
->cn_namelen
) ? '>' : 0,
1581 sizeof(dbg_parms
) - dbg_namelen
);
1585 * In the event that we collect multiple, consecutive pathname
1586 * entries, we must mark the start of the path's string and the end.
1588 code
= (FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
;
1590 if (dbg_namelen
<= 12)
1591 code
|= DBG_FUNC_END
;
1593 KERNEL_DEBUG_CONSTANT(code
, dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
1595 code
&= ~DBG_FUNC_START
;
1597 for (i
=3, dbg_namelen
-= 12; dbg_namelen
> 0; i
+=4, dbg_namelen
-= 16) {
1598 if (dbg_namelen
<= 16)
1599 code
|= DBG_FUNC_END
;
1601 KERNEL_DEBUG_CONSTANT(code
, dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);
1604 #else /* NO_KDEBUG */
1606 kdebug_lookup(struct vnode
*dp __unused
, struct componentname
*cnp __unused
)
1609 #endif /* NO_KDEBUG */
1612 vfs_getbyid(fsid_t
*fsid
, ino64_t ino
, vnode_t
*vpp
, vfs_context_t ctx
)
1617 mp
= mount_lookupby_volfsid(fsid
->val
[0], 1);
1622 /* Get the target vnode. */
1624 error
= VFS_ROOT(mp
, vpp
, ctx
);
1626 error
= VFS_VGET(mp
, ino
, vpp
, ctx
);
1633 * Obtain the real path from a legacy volfs style path.
1635 * Valid formats of input path:
1640 * "555/123456/foobar"
1643 * 555 represents the volfs file system id
1644 * '@' and '2' are aliases to the root of a file system
1645 * 123456 represents a file id
1646 * "foobar" represents a file name
1650 vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
)
1653 struct mount
*mp
= NULL
;
1661 /* Get file system id and move str to next component. */
1662 id
= strtoul(path
, &str
, 10);
1663 if (id
== 0 || str
[0] != '/') {
1666 while (*str
== '/') {
1671 mp
= mount_lookupby_volfsid(id
, 1);
1673 return (EINVAL
); /* unexpected failure */
1675 /* Check for an alias to a file system root. */
1676 if (ch
== '@' && str
[1] == '\0') {
1680 /* Get file id and move str to next component. */
1681 ino
= strtouq(str
, &str
, 10);
1684 /* Get the target vnode. */
1686 error
= VFS_ROOT(mp
, &vp
, ctx
);
1688 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
1696 /* Get the absolute path to this vnode. */
1697 error
= build_path(vp
, realpath
, bufsize
, &length
, 0, ctx
);
1700 if (error
== 0 && *str
!= '\0') {
1701 int attempt
= strlcat(realpath
, str
, MAXPATHLEN
);
1702 if (attempt
> MAXPATHLEN
){
1703 error
= ENAMETOOLONG
;
1712 lookup_compound_vnop_post_hook(int error
, vnode_t dvp
, vnode_t vp
, struct nameidata
*ndp
, int did_create
)
1714 if (error
== 0 && vp
== NULLVP
) {
1715 panic("NULL vp with error == 0.\n");
1719 * We don't want to do any of this if we didn't use the compound vnop
1720 * to perform the lookup... i.e. if we're allowing and using the legacy pattern,
1721 * where we did a full lookup.
1723 if ((ndp
->ni_flag
& NAMEI_COMPOUND_OP_MASK
) == 0) {
1728 * If we're going to continue the lookup, we'll handle
1729 * all lookup-related updates at that time.
1731 if (error
== EKEEPLOOKING
) {
1736 * Only audit or update cache for *found* vnodes. For creation
1737 * neither would happen in the non-compound-vnop case.
1739 if ((vp
!= NULLVP
) && !did_create
) {
1741 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
1742 * then we certainly don't want to update cache or identity.
1744 if ((error
!= 0) || (ndp
->ni_cnd
.cn_flags
& MAKEENTRY
)) {
1745 lookup_consider_update_cache(dvp
, vp
, &ndp
->ni_cnd
, ndp
->ni_ncgeneration
);
1747 if (ndp
->ni_cnd
.cn_flags
& AUDITVNPATH1
)
1748 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1749 else if (ndp
->ni_cnd
.cn_flags
& AUDITVNPATH2
)
1750 AUDIT_ARG(vnpath
, vp
, ARG_VNODE2
);
1754 * If you created (whether you opened or not), cut a lookup tracepoint
1755 * for the parent dir (as would happen without a compound vnop). Note: we may need
1756 * a vnode despite failure in this case!
1758 * If you did not create:
1759 * Found child (succeeded or not): cut a tracepoint for the child.
1760 * Did not find child: cut a tracepoint with the parent.
1762 if (kdebug_enable
) {
1763 kdebug_lookup(vp
? vp
: dvp
, &ndp
->ni_cnd
);