2 * Copyright (c) 2000-2015 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 <kern/kalloc.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>
91 #include <kern/zalloc.h>
92 #include <security/audit/audit.h>
94 #include <security/mac_framework.h>
96 #include <os/atomic_private.h>
98 #include <sys/paths.h>
101 #include <sys/xattr.h>
104 * The minimum volfs-style pathname is 9.
105 * Example: "/.vol/1/2"
107 #define VOLFS_MIN_PATH_LEN 9
111 static int vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
);
112 #define MAX_VOLFS_RESTARTS 5
115 static int lookup_traverse_mountpoints(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
, int vbusyflags
, vfs_context_t ctx
);
116 static int lookup_handle_symlink(struct nameidata
*ndp
, vnode_t
*new_dp
, bool* dp_has_iocount
, vfs_context_t ctx
);
117 static int lookup_authorize_search(vnode_t dp
, struct componentname
*cnp
, int dp_authorized_in_cache
, vfs_context_t ctx
);
118 static void lookup_consider_update_cache(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, int nc_generation
);
119 static int lookup_handle_found_vnode(struct nameidata
*ndp
, struct componentname
*cnp
, int rdonly
,
120 int vbusyflags
, int *keep_going
, int nc_generation
,
121 int wantparent
, int atroot
, vfs_context_t ctx
);
122 static int lookup_handle_emptyname(struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
);
125 static int lookup_handle_rsrc_fork(vnode_t dp
, struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
, vfs_context_t ctx
);
128 extern lck_rw_t
* rootvnode_rw_lock
;
131 * Convert a pathname into a pointer to a locked inode.
133 * The FOLLOW flag is set when symbolic links are to be followed
134 * when they occur at the end of the name translation process.
135 * Symbolic links are always followed for all other pathname
136 * components other than the last.
138 * The segflg defines whether the name is to be copied from user
139 * space or kernel space.
141 * Overall outline of namei:
144 * get starting directory
145 * while (!done && !error) {
146 * call lookup to search path.
147 * if symbolic link, massage name in buffer and continue
151 * ENOENT No such file or directory
152 * ELOOP Too many levels of symbolic links
153 * ENAMETOOLONG Filename too long
154 * copyinstr:EFAULT Bad address
155 * copyinstr:ENAMETOOLONG Filename too long
156 * lookup:EBADF Bad file descriptor
160 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
161 * This means we should re-drive lookup from this point.
166 namei(struct nameidata
*ndp
)
168 struct filedesc
*fdp
; /* pointer to file descriptor state */
169 struct vnode
*dp
; /* the directory we are searching */
170 struct vnode
*usedvp
= ndp
->ni_dvp
; /* store pointer to vp in case we must loop due to
171 * heavy vnode pressure */
172 uint32_t cnpflags
= ndp
->ni_cnd
.cn_flags
; /* store in case we have to restore after loop */
174 struct componentname
*cnp
= &ndp
->ni_cnd
;
175 vfs_context_t ctx
= cnp
->cn_context
;
176 proc_t p
= vfs_context_proc(ctx
);
178 /* XXX ut should be from context */
179 uthread_t ut
= (struct uthread
*)get_bsdthread_info(current_thread());
183 int volfs_restarts
= 0;
185 size_t bytes_copied
= 0;
186 vnode_t rootdir_with_usecount
= NULLVP
;
187 vnode_t startdir_with_usecount
= NULLVP
;
188 vnode_t usedvp_dp
= NULLVP
;
189 int32_t old_count
= 0;
190 bool dp_has_iocount
= false;
195 if (!vfs_context_ucred(ctx
) || !p
) {
196 panic("namei: bad cred/proc");
198 if (cnp
->cn_nameiop
& (~OPMASK
)) {
199 panic("namei: nameiop contaminated with flags");
201 if (cnp
->cn_flags
& OPMASK
) {
202 panic("namei: flags contaminated with nameiops");
207 * A compound VNOP found something that needs further processing:
208 * either a trigger vnode, a covered directory, or a symlink.
210 if (ndp
->ni_flag
& NAMEI_CONTLOOKUP
) {
211 int rdonly
, vbusyflags
, keep_going
, wantparent
;
213 rdonly
= cnp
->cn_flags
& RDONLY
;
214 vbusyflags
= ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0) ? LK_NOWAIT
: 0;
216 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
218 ndp
->ni_flag
&= ~(NAMEI_CONTLOOKUP
);
220 error
= lookup_handle_found_vnode(ndp
, &ndp
->ni_cnd
, rdonly
, vbusyflags
,
221 &keep_going
, ndp
->ni_ncgeneration
, wantparent
, 0, ctx
);
226 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
227 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
);
229 goto continue_symlink
;
238 * Get a buffer for the name to be translated, and copy the
239 * name into the buffer.
241 if ((cnp
->cn_flags
& HASBUF
) == 0) {
242 cnp
->cn_pnbuf
= ndp
->ni_pathbuf
;
243 cnp
->cn_pnlen
= PATHBUFLEN
;
246 if ((UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
) == 0)
247 && (ndp
->ni_segflg
!= UIO_SYSSPACE
)
248 && (ndp
->ni_segflg
!= UIO_SYSSPACE32
)) {
249 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
251 #endif /* LP64_DEBUG */
254 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
)) {
255 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
256 cnp
->cn_pnlen
, &bytes_copied
);
258 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
259 cnp
->cn_pnlen
, &bytes_copied
);
261 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
262 cnp
->cn_pnbuf
= zalloc(ZV_NAMEI
);
263 cnp
->cn_flags
|= HASBUF
;
264 cnp
->cn_pnlen
= MAXPATHLEN
;
272 assert(bytes_copied
<= MAXPATHLEN
);
273 ndp
->ni_pathlen
= (u_int
)bytes_copied
;
277 * Since the name cache may contain positive entries of
278 * the incorrect case, force lookup() to bypass the cache
279 * and call directly into the filesystem for each path
280 * component. Note: the FS may still consult the cache,
281 * but can apply rules to validate the results.
283 if (proc_is_forcing_hfs_case_sensitivity(p
)) {
284 cnp
->cn_flags
|= CN_SKIPNAMECACHE
;
289 * Check for legacy volfs style pathnames.
291 * For compatibility reasons we currently allow these paths,
292 * but future versions of the OS may not support them.
294 if (ndp
->ni_pathlen
>= VOLFS_MIN_PATH_LEN
&&
295 cnp
->cn_pnbuf
[0] == '/' &&
296 cnp
->cn_pnbuf
[1] == '.' &&
297 cnp
->cn_pnbuf
[2] == 'v' &&
298 cnp
->cn_pnbuf
[3] == 'o' &&
299 cnp
->cn_pnbuf
[4] == 'l' &&
300 cnp
->cn_pnbuf
[5] == '/') {
303 /* Attempt to resolve a legacy volfs style pathname. */
304 realpath
= zalloc(ZV_NAMEI
);
306 * We only error out on the ENAMETOOLONG cases where we know that
307 * vfs_getrealpath translation succeeded but the path could not fit into
308 * MAXPATHLEN characters. In other failure cases, we may be dealing with a path
309 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
311 if ((realpath_err
= vfs_getrealpath(&cnp
->cn_pnbuf
[6], realpath
, MAXPATHLEN
, ctx
))) {
312 zfree(ZV_NAMEI
, realpath
);
313 if (realpath_err
== ENOSPC
|| realpath_err
== ENAMETOOLONG
) {
314 error
= ENAMETOOLONG
;
319 if (cnp
->cn_flags
& HASBUF
) {
320 zfree(ZV_NAMEI
, cnp
->cn_pnbuf
);
322 cnp
->cn_pnbuf
= realpath
;
323 cnp
->cn_pnlen
= MAXPATHLEN
;
324 tmp_len
= strlen(realpath
) + 1;
325 assert(tmp_len
<= UINT_MAX
);
326 ndp
->ni_pathlen
= (u_int
)tmp_len
;
327 cnp
->cn_flags
|= HASBUF
| CN_VOLFSPATH
;
330 #endif /* CONFIG_VOLFS */
333 /* If we are auditing the kernel pathname, save the user pathname */
334 if (cnp
->cn_flags
& AUDITVNPATH1
) {
335 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH1
);
337 if (cnp
->cn_flags
& AUDITVNPATH2
) {
338 AUDIT_ARG(upath
, ut
->uu_cdir
, cnp
->cn_pnbuf
, ARG_UPATH2
);
340 #endif /* CONFIG_AUDIT */
343 * Do not allow empty pathnames
345 if (*cnp
->cn_pnbuf
== '\0') {
349 if (ndp
->ni_flag
& NAMEI_NOFOLLOW_ANY
) {
350 ndp
->ni_loopcnt
= MAXSYMLINKS
;
356 * determine the starting point for the translation.
358 proc_dirs_lock_shared(p
);
359 lck_rw_lock_shared(rootvnode_rw_lock
);
361 if (!(fdp
->fd_flags
& FD_CHROOT
)) {
362 ndp
->ni_rootdir
= rootvnode
;
364 ndp
->ni_rootdir
= fdp
->fd_rdir
;
367 if (!ndp
->ni_rootdir
) {
368 if (!(fdp
->fd_flags
& FD_CHROOT
)) {
369 printf("rootvnode is not set\n");
371 /* This should be a panic */
372 printf("fdp->fd_rdir is not set\n");
374 lck_rw_unlock_shared(rootvnode_rw_lock
);
375 proc_dirs_unlock_shared(p
);
380 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
382 ndp
->ni_usedvp
= NULLVP
;
384 if (*(cnp
->cn_nameptr
) == '/') {
385 while (*(cnp
->cn_nameptr
) == '/') {
389 dp
= ndp
->ni_rootdir
;
390 } else if (cnp
->cn_flags
& USEDVP
) {
395 dp
= vfs_context_cwd(ctx
);
398 if (dp
== NULLVP
|| (dp
->v_lflag
& VL_DEAD
)) {
400 lck_rw_unlock_shared(rootvnode_rw_lock
);
401 proc_dirs_unlock_shared(p
);
407 * We need our own usecount on the root vnode and the starting dir across
408 * the lookup. There's two things that be done here. We can hold the locks
409 * (which protect the existing usecounts on the directories) across the
410 * lookup or take our own usecount. Holding the locks across the lookup can
411 * cause deadlock issues if we re-enter namei on the same thread so the
412 * correct thing to do is to acquire our own usecount.
414 * Ideally, the usecount should be obtained by vnode_get->vnode_ref->vnode_put.
415 * However when this vnode is the rootvnode, that sequence will produce a
416 * lot of vnode mutex locks and unlocks on a single vnode (the rootvnode)
417 * and will be highly contended and degrade performance. Since we have
418 * an existing usecount protected by the locks we hold, we'll just use
419 * an atomic op to increment the usecount on a vnode which already has one
420 * and can't be released becasue we have the locks which protect against that
423 rootdir_with_usecount
= ndp
->ni_rootdir
;
424 old_count
= os_atomic_inc_orig(&rootdir_with_usecount
->v_usecount
, relaxed
);
426 panic("(1) invalid pre-increment usecount (%d) for rootdir vnode %p",
427 old_count
, rootdir_with_usecount
);
428 } else if (old_count
== INT32_MAX
) {
429 panic("(1) usecount overflow for vnode %p", rootdir_with_usecount
);
432 if ((dp
!= rootdir_with_usecount
) && (dp
!= usedvp_dp
)) {
433 old_count
= os_atomic_inc_orig(&dp
->v_usecount
, relaxed
);
435 panic("(2) invalid pre-increment usecount (%d) for vnode %p", old_count
, dp
);
436 } else if (old_count
== INT32_MAX
) {
437 panic("(2) usecount overflow for vnode %p", dp
);
439 startdir_with_usecount
= dp
;
442 /* Now that we have our usecount, release the locks */
443 lck_rw_unlock_shared(rootvnode_rw_lock
);
444 proc_dirs_unlock_shared(p
);
446 ndp
->ni_dvp
= NULLVP
;
452 * Give MACF policies a chance to reject the lookup
453 * before performing any filesystem operations.
454 * This hook is called before resolving the path and
455 * again each time a symlink is encountered.
456 * NB: policies receive path information as supplied
457 * by the caller and thus cannot be trusted.
459 error
= mac_vnode_check_lookup_preflight(ctx
, dp
, cnp
->cn_nameptr
, cnp
->cn_namelen
);
464 ndp
->ni_startdir
= dp
;
467 if ((error
= lookup(ndp
))) {
472 * Check for symbolic link
474 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
475 if (startdir_with_usecount
) {
476 vnode_rele(startdir_with_usecount
);
477 startdir_with_usecount
= NULLVP
;
479 if (rootdir_with_usecount
) {
480 lck_rw_lock_shared(rootvnode_rw_lock
);
481 if (rootdir_with_usecount
== rootvnode
) {
482 old_count
= os_atomic_dec_orig(&rootdir_with_usecount
->v_usecount
, relaxed
);
485 * There needs to have been at least 1 usecount left on the rootvnode
487 panic("(3) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
488 old_count
, rootdir_with_usecount
);
490 rootdir_with_usecount
= NULLVP
;
492 lck_rw_unlock_shared(rootvnode_rw_lock
);
493 if (rootdir_with_usecount
) {
494 vnode_rele(rootdir_with_usecount
);
495 rootdir_with_usecount
= NULLVP
;
503 /* Gives us a new path to process, and a starting dir */
504 error
= lookup_handle_symlink(ndp
, &dp
, &dp_has_iocount
, ctx
);
508 if (dp_has_iocount
) {
509 if ((dp
!= rootdir_with_usecount
) && (dp
!= startdir_with_usecount
) &&
511 if (startdir_with_usecount
) {
512 vnode_rele(startdir_with_usecount
);
514 vnode_ref_ext(dp
, 0, VNODE_REF_FORCE
);
515 startdir_with_usecount
= dp
;
518 dp_has_iocount
= false;
522 * only come here if we fail to handle a SYMLINK...
523 * if either ni_dvp or ni_vp is non-NULL, then
524 * we need to drop the iocount that was picked
525 * up in the lookup routine
529 vnode_put(ndp
->ni_dvp
);
532 vnode_put(ndp
->ni_vp
);
535 if (startdir_with_usecount
) {
536 vnode_rele(startdir_with_usecount
);
537 startdir_with_usecount
= NULLVP
;
539 if (rootdir_with_usecount
) {
540 lck_rw_lock_shared(rootvnode_rw_lock
);
541 if (rootdir_with_usecount
== rootvnode
) {
542 old_count
= os_atomic_dec_orig(&rootdir_with_usecount
->v_usecount
, relaxed
);
545 * There needs to have been at least 1 usecount left on the rootvnode
547 panic("(4) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
548 old_count
, rootdir_with_usecount
);
550 lck_rw_unlock_shared(rootvnode_rw_lock
);
552 lck_rw_unlock_shared(rootvnode_rw_lock
);
553 vnode_rele(rootdir_with_usecount
);
555 rootdir_with_usecount
= NULLVP
;
558 if ((cnp
->cn_flags
& HASBUF
)) {
559 cnp
->cn_flags
&= ~HASBUF
;
560 zfree(ZV_NAMEI
, cnp
->cn_pnbuf
);
562 cnp
->cn_pnbuf
= NULL
;
564 ndp
->ni_dvp
= NULLVP
;
568 * Deal with volfs fallout.
570 * At this point, if we were originally given a volfs path that
571 * looks like /.vol/123/456, then we would have had to convert it into
572 * a full path. Assuming that part worked properly, we will now attempt
573 * to conduct a lookup of the item in the namespace. Under normal
574 * circumstances, if a user looked up /tmp/foo and it was not there, it
575 * would be permissible to return ENOENT.
577 * However, we may not want to do that here. Specifically, the volfs path
578 * uniquely identifies a certain item in the namespace regardless of where it
579 * lives. If the item has moved in between the time we constructed the
580 * path and now, when we're trying to do a lookup/authorization on the full
581 * path, we may have gotten an ENOENT.
583 * At this point we can no longer tell if the path no longer exists
584 * or if the item in question no longer exists. It could have been renamed
585 * away, in which case the /.vol identifier is still valid.
587 * Do this dance a maximum of MAX_VOLFS_RESTARTS times.
589 if ((error
== ENOENT
) && (ndp
->ni_cnd
.cn_flags
& CN_VOLFSPATH
)) {
590 if (volfs_restarts
< MAX_VOLFS_RESTARTS
) {
597 if (error
== ERECYCLE
) {
598 /* vnode was recycled underneath us. re-drive lookup to start at
599 * the beginning again, since recycling invalidated last lookup*/
600 ndp
->ni_cnd
.cn_flags
= cnpflags
;
601 ndp
->ni_dvp
= usedvp
;
610 namei_compound_available(vnode_t dp
, struct nameidata
*ndp
)
612 if ((ndp
->ni_flag
& NAMEI_COMPOUNDOPEN
) != 0) {
613 return vnode_compound_open_available(dp
);
620 lookup_authorize_search(vnode_t dp
, struct componentname
*cnp
, int dp_authorized_in_cache
, vfs_context_t ctx
)
628 if (!dp_authorized_in_cache
) {
629 error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
);
635 error
= mac_vnode_check_lookup(ctx
, dp
, cnp
);
639 #endif /* CONFIG_MACF */
645 lookup_consider_update_cache(vnode_t dvp
, vnode_t vp
, struct componentname
*cnp
, int nc_generation
)
648 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
650 if (vp
->v_name
== NULL
|| vp
->v_parent
== NULLVP
) {
651 int update_flags
= 0;
653 if (isdot_or_dotdot
== 0) {
654 if (vp
->v_name
== NULL
) {
655 update_flags
|= VNODE_UPDATE_NAME
;
657 if (dvp
!= NULLVP
&& vp
->v_parent
== NULLVP
) {
658 update_flags
|= VNODE_UPDATE_PARENT
;
662 vnode_update_identity(vp
, dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
666 if ((cnp
->cn_flags
& MAKEENTRY
) && (vp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&vp
->v_nclinks
) == NULL
) {
668 * missing from name cache, but should
669 * be in it... this can happen if volfs
670 * causes the vnode to be created or the
671 * name cache entry got recycled but the
673 * check to make sure that ni_dvp is valid
674 * cache_lookup_path may return a NULL
675 * do a quick check to see if the generation of the
676 * directory matches our snapshot... this will get
677 * rechecked behind the name cache lock, but if it
678 * already fails to match, no need to go any further
680 if (dvp
!= NULLVP
&& (nc_generation
== dvp
->v_nc_generation
) && (!isdot_or_dotdot
)) {
681 cache_enter_with_gen(dvp
, vp
, cnp
, nc_generation
);
688 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
689 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
690 * (if one was provided).
693 lookup_handle_rsrc_fork(vnode_t dp
, struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
, vfs_context_t ctx
)
695 vnode_t svp
= NULLVP
;
696 enum nsoperation nsop
;
700 if (dp
->v_type
!= VREG
) {
704 switch (cnp
->cn_nameiop
) {
706 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
714 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
722 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
723 if (cnp
->cn_flags
& CN_ALLOWRSRCFORK
) {
736 if (cnp
->cn_flags
& CN_RAW_ENCRYPTED
) {
737 nsflags
|= NS_GETRAWENCRYPTED
;
740 /* Ask the file system for the resource fork. */
741 error
= vnode_getnamedstream(dp
, &svp
, XATTR_RESOURCEFORK_NAME
, nsop
, nsflags
, ctx
);
743 /* During a create, it OK for stream vnode to be missing. */
744 if (error
== ENOATTR
|| error
== ENOENT
) {
745 error
= (nsop
== NS_CREATE
) ? 0 : ENOENT
;
750 /* The "parent" of the stream is the file. */
753 vnode_put(ndp
->ni_dvp
);
759 ndp
->ni_vp
= svp
; /* on create this may be null */
761 /* Restore the truncated pathname buffer (for audits). */
762 if (ndp
->ni_pathlen
== 1 && ndp
->ni_next
[0] == '\0') {
764 * While we replaced only '/' with '\0' and would ordinarily
765 * need to just switch that back, the buffer in which we did
766 * this may not be what the pathname buffer is now when symlinks
767 * are involved. If we just restore the "/" we will make the
768 * string not terminated anymore, so be safe and restore the
771 strncpy(ndp
->ni_next
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
));
772 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
773 cnp
->cn_namelen
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
774 ndp
->ni_next
+= cnp
->cn_namelen
;
775 if (ndp
->ni_next
[0] != '\0') {
776 panic("Incorrect termination of path in %s", __FUNCTION__
);
779 cnp
->cn_flags
&= ~MAKEENTRY
;
785 #endif /* NAMEDRSRCFORK */
789 * --One on ni_vp. One on ni_dvp if there is more path, or we didn't come through the
790 * cache, or we came through the cache and the caller doesn't want the parent.
793 * --Leaves us in the correct state for the next step, whatever that might be.
794 * --If we find a symlink, returns with iocounts on both ni_vp and ni_dvp.
795 * --If we are to look up another component, then we have an iocount on ni_vp and
797 * --If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags.
798 * --In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount
802 lookup_handle_found_vnode(struct nameidata
*ndp
, struct componentname
*cnp
, int rdonly
,
803 int vbusyflags
, int *keep_going
, int nc_generation
,
804 int wantparent
, int atroot
, vfs_context_t ctx
)
813 if (ndp
->ni_vp
== NULLVP
) {
814 panic("NULL ni_vp in %s\n", __FUNCTION__
);
822 * Take into account any additional components consumed by
823 * the underlying filesystem.
825 if (cnp
->cn_consume
> 0) {
826 cnp
->cn_nameptr
+= cnp
->cn_consume
;
827 ndp
->ni_next
+= cnp
->cn_consume
;
828 ndp
->ni_pathlen
-= cnp
->cn_consume
;
831 lookup_consider_update_cache(ndp
->ni_dvp
, dp
, cnp
, nc_generation
);
835 * Check to see if the vnode has been mounted on...
836 * if so find the root of the mounted file system.
837 * Updates ndp->ni_vp.
839 error
= lookup_traverse_mountpoints(ndp
, cnp
, dp
, vbusyflags
, ctx
);
846 if (vfs_flags(vnode_mount(dp
)) & MNT_MULTILABEL
) {
847 error
= vnode_label(vnode_mount(dp
), NULL
, dp
, NULL
, 0, ctx
);
855 * Check for symbolic link
857 if ((dp
->v_type
== VLNK
) &&
858 ((cnp
->cn_flags
& FOLLOW
) || (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) || *ndp
->ni_next
== '/')) {
859 cnp
->cn_flags
|= ISSYMLINK
;
865 * Check for bogus trailing slashes.
867 if ((ndp
->ni_flag
& NAMEI_TRAILINGSLASH
)) {
868 if (dp
->v_type
!= VDIR
) {
872 ndp
->ni_flag
&= ~(NAMEI_TRAILINGSLASH
);
877 * Deny namei/lookup requests to resolve paths that point to shadow files.
878 * Access to shadow files must be conducted by explicit calls to VNOP_LOOKUP
879 * directly, and not use lookup/namei
881 if (vnode_isshadow(dp
)) {
889 * Not a symbolic link. If more pathname,
890 * continue at next component, else return.
892 * Definitely have a dvp if there's another slash
894 if (*ndp
->ni_next
== '/') {
895 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
897 while (*cnp
->cn_nameptr
== '/') {
902 cp
= cnp
->cn_nameptr
;
903 vnode_put(ndp
->ni_dvp
);
904 ndp
->ni_dvp
= NULLVP
;
915 * Disallow directory write attempts on read-only file systems.
918 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
923 /* If SAVESTART is set, we should have a dvp */
924 if (cnp
->cn_flags
& SAVESTART
) {
926 * note that we already hold a reference
927 * on both dp and ni_dvp, but for some reason
928 * can't get another one... in this case we
929 * need to do vnode_put on dp in 'bad2'
931 if ((vnode_get(ndp
->ni_dvp
))) {
935 ndp
->ni_startdir
= ndp
->ni_dvp
;
937 if (!wantparent
&& ndp
->ni_dvp
) {
938 vnode_put(ndp
->ni_dvp
);
939 ndp
->ni_dvp
= NULLVP
;
942 if (cnp
->cn_flags
& AUDITVNPATH1
) {
943 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
944 } else if (cnp
->cn_flags
& AUDITVNPATH2
) {
945 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
950 * Caller wants the resource fork.
952 if ((cnp
->cn_flags
& CN_WANTSRSRCFORK
) && (dp
!= NULLVP
)) {
953 error
= lookup_handle_rsrc_fork(dp
, ndp
, cnp
, wantparent
, ctx
);
962 kdebug_lookup(ndp
->ni_vp
, cnp
);
968 error
= lookup_handle_emptyname(ndp
, cnp
, wantparent
);
979 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
982 lookup_handle_emptyname(struct nameidata
*ndp
, struct componentname
*cnp
, int wantparent
)
990 * A degenerate name (e.g. / or "") which is a way of
991 * talking about a directory, e.g. like "/." or ".".
993 if (dp
->v_type
!= VDIR
) {
997 if (cnp
->cn_nameiop
!= LOOKUP
) {
1003 * note that we already hold a reference
1004 * on dp, but for some reason can't
1005 * get another one... in this case we
1006 * need to do vnode_put on dp in 'bad'
1008 if ((vnode_get(dp
))) {
1014 cnp
->cn_flags
&= ~ISDOTDOT
;
1015 cnp
->cn_flags
|= ISLASTCN
;
1016 ndp
->ni_next
= cnp
->cn_nameptr
;
1019 if (cnp
->cn_flags
& AUDITVNPATH1
) {
1020 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
1021 } else if (cnp
->cn_flags
& AUDITVNPATH2
) {
1022 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
1024 if (cnp
->cn_flags
& SAVESTART
) {
1025 panic("lookup: SAVESTART");
1033 * Search a pathname.
1034 * This is a very central and rather complicated routine.
1036 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
1037 * The starting directory is taken from ni_startdir. The pathname is
1038 * descended until done, or a symbolic link is encountered. The variable
1039 * ni_more is clear if the path is completed; it is set to one if a
1040 * symbolic link needing interpretation is encountered.
1042 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
1043 * whether the name is to be looked up, created, renamed, or deleted.
1044 * When CREATE, RENAME, or DELETE is specified, information usable in
1045 * creating, renaming, or deleting a directory entry may be calculated.
1046 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
1047 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
1048 * returned unlocked. Otherwise the parent directory is not returned. If
1049 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
1050 * the target is returned locked, otherwise it is returned unlocked.
1051 * When creating or renaming and LOCKPARENT is specified, the target may not
1052 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
1054 * Overall outline of lookup:
1057 * identify next component of name at ndp->ni_ptr
1058 * handle degenerate case where name is null string
1059 * if .. and crossing mount points and on mounted filesys, find parent
1060 * call VNOP_LOOKUP routine for next component name
1061 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
1062 * component vnode returned in ni_vp (if it exists), locked.
1063 * if result vnode is mounted on and crossing mount points,
1064 * find mounted on vnode
1065 * if more components of name, do next level at dirloop
1066 * return the answer in ni_vp, locked if LOCKLEAF set
1067 * if LOCKPARENT set, return locked parent in ni_dvp
1068 * if WANTPARENT set, return unlocked parent in ni_dvp
1070 * Returns: 0 Success
1071 * ENOENT No such file or directory
1072 * EBADF Bad file descriptor
1073 * ENOTDIR Not a directory
1074 * EROFS Read-only file system [CREATE]
1075 * EISDIR Is a directory [CREATE]
1076 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
1077 * vnode_authorize:EROFS
1078 * vnode_authorize:EACCES
1079 * vnode_authorize:EPERM
1080 * vnode_authorize:???
1081 * VNOP_LOOKUP:ENOENT No such file or directory
1082 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
1089 lookup(struct nameidata
*ndp
)
1091 char *cp
; /* pointer into pathname argument */
1092 vnode_t tdp
; /* saved dp */
1093 vnode_t dp
; /* the directory we are searching */
1094 int docache
= 1; /* == 0 do not cache last component */
1095 int wantparent
; /* 1 => wantparent or lockparent flag */
1096 int rdonly
; /* lookup read-only flag bit */
1097 int dp_authorized
= 0;
1099 struct componentname
*cnp
= &ndp
->ni_cnd
;
1100 vfs_context_t ctx
= cnp
->cn_context
;
1102 int nc_generation
= 0;
1103 vnode_t last_dp
= NULLVP
;
1108 * Setup: break out flag bits into variables.
1110 if (cnp
->cn_flags
& NOCACHE
) {
1113 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
1114 rdonly
= cnp
->cn_flags
& RDONLY
;
1115 cnp
->cn_flags
&= ~ISSYMLINK
;
1116 cnp
->cn_consume
= 0;
1118 dp
= ndp
->ni_startdir
;
1119 ndp
->ni_startdir
= NULLVP
;
1121 if ((cnp
->cn_flags
& CN_NBMOUNTLOOK
) != 0) {
1122 vbusyflags
= LK_NOWAIT
;
1124 cp
= cnp
->cn_nameptr
;
1127 if ((vnode_getwithref(dp
))) {
1133 error
= lookup_handle_emptyname(ndp
, cnp
, wantparent
);
1142 ndp
->ni_vp
= NULLVP
;
1144 if ((error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &dp_authorized
, last_dp
))) {
1148 if ((cnp
->cn_flags
& ISLASTCN
)) {
1150 cnp
->cn_flags
|= MAKEENTRY
;
1153 cnp
->cn_flags
|= MAKEENTRY
;
1158 if (ndp
->ni_vp
!= NULLVP
) {
1160 * cache_lookup_path returned a non-NULL ni_vp then,
1161 * we're guaranteed that the dp is a VDIR, it's
1162 * been authorized, and vp is not ".."
1164 * make sure we don't try to enter the name back into
1165 * the cache if this vp is purged before we get to that
1166 * check since we won't have serialized behind whatever
1167 * activity is occurring in the FS that caused the purge
1170 nc_generation
= dp
->v_nc_generation
- 1;
1173 goto returned_from_lookup_path
;
1177 * Handle "..": two special cases.
1178 * 1. If at root directory (e.g. after chroot)
1179 * or at absolute root directory
1180 * then ignore it so can't get out.
1181 * 2. If this vnode is the root of a mounted
1182 * filesystem, then replace it with the
1183 * vnode which was mounted on so we take the
1184 * .. in the other file system.
1186 if ((cnp
->cn_flags
& ISDOTDOT
)) {
1188 * if this is a chroot'ed process, check if the current
1189 * directory is still a subdirectory of the process's
1192 if (ndp
->ni_rootdir
&& (ndp
->ni_rootdir
!= rootvnode
) &&
1193 dp
!= ndp
->ni_rootdir
) {
1195 int is_subdir
= FALSE
;
1197 sdir_error
= vnode_issubdir(dp
, ndp
->ni_rootdir
,
1198 &is_subdir
, vfs_context_kernel());
1201 * If we couldn't determine if dp is a subdirectory of
1202 * ndp->ni_rootdir (sdir_error != 0), we let the request
1205 if (!sdir_error
&& !is_subdir
) {
1207 dp
= ndp
->ni_rootdir
;
1209 * There's a ref on the process's root directory
1210 * but we can't use vnode_getwithref here as
1211 * there is nothing preventing that ref being
1212 * released by another thread.
1214 if (vnode_get(dp
)) {
1222 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
1226 * we're pinned at the root
1227 * we've already got one reference on 'dp'
1228 * courtesy of cache_lookup_path... take
1229 * another one for the ".."
1230 * if we fail to get the new reference, we'll
1231 * drop our original down in 'bad'
1233 if ((vnode_get(dp
))) {
1238 goto returned_from_lookup_path
;
1240 if ((dp
->v_flag
& VROOT
) == 0 ||
1241 (cnp
->cn_flags
& NOCROSSMOUNT
)) {
1244 if (dp
->v_mount
== NULL
) { /* forced umount */
1249 dp
= tdp
->v_mount
->mnt_vnodecovered
;
1251 if ((vnode_getwithref(dp
))) {
1266 * We now have a segment name to search for, and a directory to search.
1269 ndp
->ni_vp
= NULLVP
;
1271 if (dp
->v_type
!= VDIR
) {
1275 if ((cnp
->cn_flags
& DONOTAUTH
) != DONOTAUTH
) {
1276 error
= lookup_authorize_search(dp
, cnp
, dp_authorized
, ctx
);
1283 * Now that we've authorized a lookup, can bail out if the filesystem
1284 * will be doing a batched operation. Return an iocount on dvp.
1287 if ((cnp
->cn_flags
& ISLASTCN
) && namei_compound_available(dp
, ndp
) && !(cnp
->cn_flags
& CN_WANTSRSRCFORK
)) {
1289 if ((cnp
->cn_flags
& ISLASTCN
) && namei_compound_available(dp
, ndp
)) {
1290 #endif /* NAMEDRSRCFORK */
1291 ndp
->ni_flag
|= NAMEI_UNFINISHED
;
1292 ndp
->ni_ncgeneration
= dp
->v_nc_generation
;
1296 nc_generation
= dp
->v_nc_generation
;
1300 * Filesystems that support hardlinks may want to call vnode_update_identity
1301 * if the lookup operation below will modify the in-core vnode to belong to a new point
1302 * in the namespace. VFS cannot infer whether or not the look up operation makes the vnode
1303 * name change or change parents. Without this, the lookup may make update
1304 * filesystem-specific in-core metadata but fail to update the v_parent or v_name
1305 * fields in the vnode. If VFS were to do this, it would be necessary to call
1306 * vnode_update_identity on every lookup operation -- expensive!
1308 * However, even with this in place, multiple lookups may occur in between this lookup
1309 * and the subsequent vnop, so, at best, we could only guarantee that you would get a
1310 * valid path back, and not necessarily the one that you wanted.
1315 * If you are now looking up /foo/b and the vnode for this link represents /tmp/a,
1316 * vnode_update_identity will fix the parentage so that you can get /foo/b back
1317 * through the v_parent chain (preventing you from getting /tmp/b back). It would
1318 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1321 error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
);
1325 if ((error
== ENOENT
) &&
1326 (dp
->v_mount
!= NULL
) &&
1327 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
1329 error
= lookup_traverse_union(tdp
, &dp
, ctx
);
1341 if (error
!= EJUSTRETURN
) {
1345 if (ndp
->ni_vp
!= NULLVP
) {
1346 panic("leaf should be empty");
1351 * At this point, error should be EJUSTRETURN.
1353 * If CN_WANTSRSRCFORK is set, that implies that the
1354 * underlying filesystem could not find the "parent" of the
1355 * resource fork (the data fork), and we are doing a lookup
1356 * for a CREATE event.
1358 * However, this should be converted to an error, as the
1359 * failure to find this parent should disallow further
1360 * progress to try and acquire a resource fork vnode.
1362 if (cnp
->cn_flags
& CN_WANTSRSRCFORK
) {
1368 error
= lookup_validate_creation_path(ndp
);
1373 * We return with ni_vp NULL to indicate that the entry
1374 * doesn't currently exist, leaving a pointer to the
1375 * referenced directory vnode in ndp->ni_dvp.
1377 if (cnp
->cn_flags
& SAVESTART
) {
1378 if ((vnode_get(ndp
->ni_dvp
))) {
1382 ndp
->ni_startdir
= ndp
->ni_dvp
;
1385 vnode_put(ndp
->ni_dvp
);
1388 if (kdebug_enable
) {
1389 kdebug_lookup(ndp
->ni_dvp
, cnp
);
1393 returned_from_lookup_path
:
1394 /* We'll always have an iocount on ni_vp when this finishes. */
1395 error
= lookup_handle_found_vnode(ndp
, cnp
, rdonly
, vbusyflags
, &keep_going
, nc_generation
, wantparent
, atroot
, ctx
);
1403 /* namei() will handle symlinks */
1404 if ((dp
->v_type
== VLNK
) &&
1405 ((cnp
->cn_flags
& FOLLOW
) || (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) || *ndp
->ni_next
== '/')) {
1410 * Otherwise, there's more path to process.
1411 * cache_lookup_path is now responsible for dropping io ref on dp
1412 * when it is called again in the dirloop. This ensures we hold
1413 * a ref on dp until we complete the next round of lookup.
1423 vnode_put(ndp
->ni_dvp
);
1426 vnode_put(ndp
->ni_vp
);
1427 ndp
->ni_vp
= NULLVP
;
1429 if (kdebug_enable
) {
1430 kdebug_lookup(dp
, cnp
);
1438 ndp
->ni_vp
= NULLVP
;
1440 if (kdebug_enable
) {
1441 kdebug_lookup(dp
, cnp
);
1447 * Given a vnode in a union mount, traverse to the equivalent
1448 * vnode in the underlying mount.
1451 lookup_traverse_union(vnode_t dvp
, vnode_t
*new_dvp
, vfs_context_t ctx
)
1453 char *path
= NULL
, *pp
;
1454 const char *name
, *np
;
1457 struct nameidata nd
;
1462 if (vp
&& vp
->v_flag
& VROOT
) {
1463 *new_dvp
= vp
->v_mount
->mnt_vnodecovered
;
1464 if (vnode_getwithref(*new_dvp
)) {
1470 path
= (char *) zalloc(ZV_NAMEI
);
1477 * Walk back up to the mountpoint following the
1478 * v_parent chain and build a slash-separated path.
1479 * Then lookup that path starting with the covered vnode.
1481 pp
= path
+ (MAXPATHLEN
- 1);
1485 name
= vnode_getname(vp
);
1487 printf("lookup_traverse_union: null parent name: .%s\n", pp
);
1492 if ((len
+ 1) > (size_t)(pp
- path
)) { // Enough space for this name ?
1493 error
= ENAMETOOLONG
;
1494 vnode_putname(name
);
1497 for (np
= name
+ len
; len
> 0; len
--) { // Copy name backwards
1500 vnode_putname(name
);
1502 if (vp
== NULLVP
|| vp
->v_flag
& VROOT
) {
1508 /* Evaluate the path in the underlying mount */
1509 NDINIT(&nd
, LOOKUP
, OP_LOOKUP
, USEDVP
, UIO_SYSSPACE
, CAST_USER_ADDR_T(pp
), ctx
);
1510 nd
.ni_dvp
= dvp
->v_mount
->mnt_vnodecovered
;
1513 *new_dvp
= nd
.ni_vp
;
1518 zfree(ZV_NAMEI
, path
);
1524 lookup_validate_creation_path(struct nameidata
*ndp
)
1526 struct componentname
*cnp
= &ndp
->ni_cnd
;
1529 * If creating and at end of pathname, then can consider
1530 * allowing file to be created.
1532 if (cnp
->cn_flags
& RDONLY
) {
1535 if ((cnp
->cn_flags
& ISLASTCN
) && (ndp
->ni_flag
& NAMEI_TRAILINGSLASH
) && !(cnp
->cn_flags
& WILLBEDIR
)) {
1543 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1546 lookup_traverse_mountpoints(struct nameidata
*ndp
, struct componentname
*cnp
, vnode_t dp
,
1547 int vbusyflags
, vfs_context_t ctx
)
1553 vnode_t mounted_on_dp
;
1554 int current_mount_generation
= 0;
1556 vnode_t triggered_dp
= NULLVP
;
1558 #define MAX_TRIGGER_RETRIES 1
1561 if (dp
->v_type
!= VDIR
|| cnp
->cn_flags
& NOCROSSMOUNT
) {
1569 current_mount_generation
= mount_generation
;
1571 while (dp
->v_mountedhere
) {
1572 vnode_lock_spin(dp
);
1573 if ((mp
= dp
->v_mountedhere
)) {
1581 if (ISSET(mp
->mnt_lflag
, MNT_LFORCE
)) {
1582 mount_dropcrossref(mp
, dp
, 0);
1583 break; // don't traverse into a forced unmount
1587 if (vfs_busy(mp
, vbusyflags
)) {
1588 mount_dropcrossref(mp
, dp
, 0);
1589 if (vbusyflags
== LK_NOWAIT
) {
1597 error
= VFS_ROOT(mp
, &tdp
, ctx
);
1599 mount_dropcrossref(mp
, dp
, 0);
1607 ndp
->ni_vp
= dp
= tdp
;
1608 if (dp
->v_type
!= VDIR
) {
1609 #if DEVELOPMENT || DEBUG
1610 panic("%s : Root of filesystem not a directory\n",
1621 * The triggered_dp check here is required but is susceptible to a
1622 * (unlikely) race in which trigger mount is done from here and is
1623 * unmounted before we get past vfs_busy above. We retry to deal with
1624 * that case but it has the side effect of unwanted retries for
1625 * "special" processes which don't want to trigger mounts.
1627 if (dp
->v_resolve
&& retry_cnt
< MAX_TRIGGER_RETRIES
) {
1628 error
= vnode_trigger_resolve(dp
, ndp
, ctx
);
1632 if (dp
== triggered_dp
) {
1640 #endif /* CONFIG_TRIGGERS */
1643 mp
= mounted_on_dp
->v_mountedhere
;
1646 mount_lock_spin(mp
);
1647 mp
->mnt_realrootvp_vid
= dp
->v_id
;
1648 mp
->mnt_realrootvp
= dp
;
1649 mp
->mnt_generation
= current_mount_generation
;
1661 * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location
1662 * at which to start a lookup with a resolved path, and all other iocounts dropped.
1665 lookup_handle_symlink(struct nameidata
*ndp
, vnode_t
*new_dp
, bool *new_dp_has_iocount
, vfs_context_t ctx
)
1668 char *cp
; /* pointer into pathname argument */
1672 struct user_iovec s_uiovec
;
1673 struct kern_iovec s_kiovec
;
1676 char uio_buf
[UIO_SIZEOF(1)];
1677 } u_uio_buf
; /* union only for aligning uio_buf correctly */
1678 int need_newpathbuf
;
1680 struct componentname
*cnp
= &ndp
->ni_cnd
;
1683 u_int rsrclen
= (cnp
->cn_flags
& CN_WANTSRSRCFORK
) ? sizeof(_PATH_RSRCFORKSPEC
) : 0;
1684 bool dp_has_iocount
= false;
1686 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
1690 if ((error
= mac_vnode_check_readlink(ctx
, ndp
->ni_vp
)) != 0) {
1694 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
)) {
1695 need_newpathbuf
= 1;
1697 need_newpathbuf
= 0;
1700 if (need_newpathbuf
) {
1701 cp
= zalloc(ZV_NAMEI
);
1705 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
1706 &u_uio_buf
.uio_buf
[0], sizeof(u_uio_buf
.uio_buf
));
1708 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
1710 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
1712 if (need_newpathbuf
) {
1713 zfree(ZV_NAMEI
, cp
);
1719 * Safe to set unsigned with a [larger] signed type here
1720 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1723 linklen
= MAXPATHLEN
- (u_int
)uio_resid(auio
);
1724 if (linklen
+ ndp
->ni_pathlen
+ rsrclen
> MAXPATHLEN
) {
1725 if (need_newpathbuf
) {
1726 zfree(ZV_NAMEI
, cp
);
1729 return ENAMETOOLONG
;
1731 if (need_newpathbuf
) {
1732 tmppn
= cnp
->cn_pnbuf
;
1733 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
1735 cnp
->cn_pnlen
= MAXPATHLEN
;
1737 if ((cnp
->cn_flags
& HASBUF
)) {
1738 zfree(ZV_NAMEI
, tmppn
);
1740 cnp
->cn_flags
|= HASBUF
;
1743 cnp
->cn_pnbuf
[linklen
] = '\0';
1746 ndp
->ni_pathlen
+= linklen
;
1747 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
1750 * starting point for 'relative'
1751 * symbolic link path
1756 * get rid of reference returned via 'lookup'
1757 * ni_dvp is released only if we restart at /.
1759 vnode_put(ndp
->ni_vp
);
1760 ndp
->ni_vp
= NULLVP
;
1761 ndp
->ni_dvp
= NULLVP
;
1764 * Check if symbolic link restarts us at the root
1766 if (*(cnp
->cn_nameptr
) == '/') {
1767 vnode_put(dp
); /* ALWAYS have a dvp for a symlink */
1768 while (*(cnp
->cn_nameptr
) == '/') {
1772 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
1776 dp_has_iocount
= true;
1780 *new_dp_has_iocount
= dp_has_iocount
;
1786 * relookup - lookup a path name component
1787 * Used by lookup to re-aquire things.
1790 relookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
)
1792 struct vnode
*dp
= NULL
; /* the directory we are searching */
1793 int wantparent
; /* 1 => wantparent or lockparent flag */
1794 int rdonly
; /* lookup read-only flag bit */
1796 #ifdef NAMEI_DIAGNOSTIC
1797 int i
, newhash
; /* DEBUG: check name hash */
1798 char *cp
; /* DEBUG: check name ptr/len */
1800 vfs_context_t ctx
= cnp
->cn_context
;;
1803 * Setup: break out flag bits into variables.
1805 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
1806 rdonly
= cnp
->cn_flags
& RDONLY
;
1807 cnp
->cn_flags
&= ~ISSYMLINK
;
1809 if (cnp
->cn_flags
& NOCACHE
) {
1810 cnp
->cn_flags
&= ~MAKEENTRY
;
1812 cnp
->cn_flags
|= MAKEENTRY
;
1818 * Check for degenerate name (e.g. / or "")
1819 * which is a way of talking about a directory,
1820 * e.g. like "/." or ".".
1822 if (cnp
->cn_nameptr
[0] == '\0') {
1823 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
1827 if (dp
->v_type
!= VDIR
) {
1831 if ((vnode_get(dp
))) {
1837 if (cnp
->cn_flags
& SAVESTART
) {
1838 panic("lookup: SAVESTART");
1843 * We now have a segment name to search for, and a directory to search.
1845 if ((error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
))) {
1846 if (error
!= EJUSTRETURN
) {
1851 panic("leaf should be empty");
1855 * If creating and at end of pathname, then can consider
1856 * allowing file to be created.
1863 * We return with ni_vp NULL to indicate that the entry
1864 * doesn't currently exist, leaving a pointer to the
1865 * (possibly locked) directory inode in ndp->ni_dvp.
1873 * Check for symbolic link
1875 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
)) {
1876 panic("relookup: symlink found.\n");
1881 * Disallow directory write attempts on read-only file systems.
1884 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
1888 /* ASSERT(dvp == ndp->ni_startdir) */
1901 * Free pathname buffer
1904 nameidone(struct nameidata
*ndp
)
1906 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
1907 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
1909 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
1910 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
1911 zfree(ZV_NAMEI
, tmp
);
1917 * Log (part of) a pathname using kdebug, as used by fs_usage. The path up to
1918 * and including the current component name are logged. Up to NUMPARMS * 4
1919 * bytes of pathname will be logged. If the path to be logged is longer than
1920 * that, then the last NUMPARMS * 4 bytes are logged. That is, the truncation
1921 * removes the leading portion of the path.
1923 * The logging is done via multiple KDBG_RELEASE calls. The first one is marked
1924 * with DBG_FUNC_START. The last one is marked with DBG_FUNC_END (in addition
1925 * to DBG_FUNC_START if it is also the first). There may be intermediate ones
1926 * with neither DBG_FUNC_START nor DBG_FUNC_END.
1928 * The first event passes the vnode pointer and 24 or 32 (on K32, 12 or 24)
1929 * bytes of pathname. The remaining events add 32 (on K32, 16) bytes of
1930 * pathname each. The minimum number of events required to pass the path are
1931 * used. Any excess padding in the final event (because not all of the 24 or 32
1932 * (on K32, 12 or 16) bytes are needed for the remainder of the path) is set to
1933 * zero bytes, or '>' if there is more path beyond the current component name
1934 * (usually because an intermediate component was not found).
1936 * NOTE: If the path length is greater than NUMPARMS * 4, or is not of the form
1937 * 24 + N * 32 (or on K32, 12 + N * 16), there will be no padding.
1939 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
1942 kdebug_vfs_lookup(unsigned long *path_words
, int path_len
, void *vnp
,
1945 bool noprocfilt
= flags
& KDBG_VFS_LOOKUP_FLAG_NOPROCFILT
;
1947 assert(path_len
>= 0);
1949 int code
= ((flags
& KDBG_VFS_LOOKUP_FLAG_LOOKUP
) ? VFS_LOOKUP
:
1950 VFS_LOOKUP_DONE
) | DBG_FUNC_START
;
1952 if (path_len
<= (3 * (int)sizeof(long))) {
1953 code
|= DBG_FUNC_END
;
1957 KDBG_RELEASE_NOPROCFILT(code
, kdebug_vnode(vnp
), path_words
[0],
1958 path_words
[1], path_words
[2]);
1960 KDBG_RELEASE(code
, kdebug_vnode(vnp
), path_words
[0], path_words
[1],
1964 code
&= ~DBG_FUNC_START
;
1966 for (int i
= 3; i
* (int)sizeof(long) < path_len
; i
+= 4) {
1967 if ((i
+ 4) * (int)sizeof(long) >= path_len
) {
1968 code
|= DBG_FUNC_END
;
1972 KDBG_RELEASE_NOPROCFILT(code
, path_words
[i
], path_words
[i
+ 1],
1973 path_words
[i
+ 2], path_words
[i
+ 3]);
1975 KDBG_RELEASE(code
, path_words
[i
], path_words
[i
+ 1],
1976 path_words
[i
+ 2], path_words
[i
+ 3]);
1982 kdebug_lookup_gen_events(long *path_words
, int path_len
, void *vnp
, bool lookup
)
1984 assert(path_len
>= 0);
1985 kdebug_vfs_lookup((unsigned long *)path_words
, path_len
, vnp
,
1986 lookup
? KDBG_VFS_LOOKUP_FLAG_LOOKUP
: 0);
1990 kdebug_lookup(vnode_t vnp
, struct componentname
*cnp
)
1992 unsigned long path_words
[NUMPARMS
];
1995 * Truncate the leading portion of the path to fit in path_words.
1997 char *path_end
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
1998 size_t path_len
= MIN(path_end
- cnp
->cn_pnbuf
,
1999 (ssize_t
)sizeof(path_words
));
2000 assert(path_len
>= 0);
2001 char *path_trunc
= path_end
- path_len
;
2003 memcpy(path_words
, path_trunc
, path_len
);
2006 * Pad with '\0' or '>'.
2008 if (path_len
< (ssize_t
)sizeof(path_words
)) {
2009 bool complete_str
= *(cnp
->cn_nameptr
+ cnp
->cn_namelen
) == '\0';
2010 memset((char *)path_words
+ path_len
, complete_str
? '\0' : '>',
2011 sizeof(path_words
) - path_len
);
2013 kdebug_vfs_lookup(path_words
, (int)path_len
, vnp
, KDBG_VFS_LOOKUP_FLAG_LOOKUP
);
2016 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
2019 kdebug_vfs_lookup(long *dbg_parms __unused
, int dbg_namelen __unused
,
2020 void *dp __unused
, __unused
uint32_t flags
)
2025 kdebug_lookup(struct vnode
*dp __unused
, struct componentname
*cnp __unused
)
2028 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
2031 vfs_getbyid(fsid_t
*fsid
, ino64_t ino
, vnode_t
*vpp
, vfs_context_t ctx
)
2036 mp
= mount_lookupby_volfsid(fsid
->val
[0], 1);
2041 /* Get the target vnode. */
2043 error
= VFS_ROOT(mp
, vpp
, ctx
);
2045 error
= VFS_VGET(mp
, ino
, vpp
, ctx
);
2052 * Obtain the real path from a legacy volfs style path.
2054 * Valid formats of input path:
2059 * "555/123456/foobar"
2062 * 555 represents the volfs file system id
2063 * '@' and '2' are aliases to the root of a file system
2064 * 123456 represents a file id
2065 * "foobar" represents a file name
2069 vfs_getrealpath(const char * path
, char * realpath
, size_t bufsize
, vfs_context_t ctx
)
2072 struct mount
*mp
= NULL
;
2080 /* Get file system id and move str to next component. */
2081 id
= strtoul(path
, &str
, 10);
2082 if (id
== 0 || str
[0] != '/') {
2085 while (*str
== '/') {
2093 mp
= mount_lookupby_volfsid((int)id
, 1);
2095 return EINVAL
; /* unexpected failure */
2097 /* Check for an alias to a file system root. */
2098 if (ch
== '@' && str
[1] == '\0') {
2102 /* Get file id and move str to next component. */
2103 ino
= strtouq(str
, &str
, 10);
2106 /* Get the target vnode. */
2108 struct vfs_attr vfsattr
;
2109 int use_vfs_root
= TRUE
;
2111 VFSATTR_INIT(&vfsattr
);
2112 VFSATTR_WANTED(&vfsattr
, f_capabilities
);
2113 if (vfs_getattr(mp
, &vfsattr
, vfs_context_kernel()) == 0 &&
2114 VFSATTR_IS_SUPPORTED(&vfsattr
, f_capabilities
)) {
2115 if ((vfsattr
.f_capabilities
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
) &&
2116 (vfsattr
.f_capabilities
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_VOL_GROUPS
)) {
2117 use_vfs_root
= FALSE
;
2122 error
= VFS_ROOT(mp
, &vp
, ctx
);
2124 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
2127 error
= VFS_VGET(mp
, ino
, &vp
, ctx
);
2135 /* Get the absolute path to this vnode. */
2136 error
= build_path(vp
, realpath
, (int)bufsize
, &length
, 0, ctx
);
2139 if (error
== 0 && *str
!= '\0') {
2140 size_t attempt
= strlcat(realpath
, str
, MAXPATHLEN
);
2141 if (attempt
> MAXPATHLEN
) {
2142 error
= ENAMETOOLONG
;
2151 lookup_compound_vnop_post_hook(int error
, vnode_t dvp
, vnode_t vp
, struct nameidata
*ndp
, int did_create
)
2153 if (error
== 0 && vp
== NULLVP
) {
2154 panic("NULL vp with error == 0.\n");
2158 * We don't want to do any of this if we didn't use the compound vnop
2159 * to perform the lookup... i.e. if we're allowing and using the legacy pattern,
2160 * where we did a full lookup.
2162 if ((ndp
->ni_flag
& NAMEI_COMPOUND_OP_MASK
) == 0) {
2167 * If we're going to continue the lookup, we'll handle
2168 * all lookup-related updates at that time.
2170 if (error
== EKEEPLOOKING
) {
2175 * Only audit or update cache for *found* vnodes. For creation
2176 * neither would happen in the non-compound-vnop case.
2178 if ((vp
!= NULLVP
) && !did_create
) {
2180 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
2181 * then we certainly don't want to update cache or identity.
2183 if ((error
!= 0) || (ndp
->ni_cnd
.cn_flags
& MAKEENTRY
)) {
2184 lookup_consider_update_cache(dvp
, vp
, &ndp
->ni_cnd
, ndp
->ni_ncgeneration
);
2186 if (ndp
->ni_cnd
.cn_flags
& AUDITVNPATH1
) {
2187 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2188 } else if (ndp
->ni_cnd
.cn_flags
& AUDITVNPATH2
) {
2189 AUDIT_ARG(vnpath
, vp
, ARG_VNODE2
);
2194 * If you created (whether you opened or not), cut a lookup tracepoint
2195 * for the parent dir (as would happen without a compound vnop). Note: we may need
2196 * a vnode despite failure in this case!
2198 * If you did not create:
2199 * Found child (succeeded or not): cut a tracepoint for the child.
2200 * Did not find child: cut a tracepoint with the parent.
2202 if (kdebug_enable
) {
2203 kdebug_lookup(vp
? vp
: dvp
, &ndp
->ni_cnd
);