]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_lookup.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
CommitLineData
1c79356b 1/*
3e170ce0 2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
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.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
53 *
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
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
67 */
2d21ac55
A
68/*
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,
72 * Version 2.0.
73 */
1c79356b
A
74
75#include <sys/param.h>
55e303ae 76#include <sys/systm.h>
1c79356b
A
77#include <sys/syslimits.h>
78#include <sys/time.h>
79#include <sys/namei.h>
80#include <sys/vm.h>
91447636
A
81#include <sys/vnode_internal.h>
82#include <sys/mount_internal.h>
1c79356b 83#include <sys/errno.h>
f427ee49 84#include <kern/kalloc.h>
1c79356b 85#include <sys/filedesc.h>
91447636 86#include <sys/proc_internal.h>
1c79356b 87#include <sys/kdebug.h>
0a7de745 88#include <sys/unistd.h> /* For _PC_NAME_MAX */
91447636
A
89#include <sys/uio_internal.h>
90#include <sys/kauth.h>
f427ee49 91#include <kern/zalloc.h>
b0d623f7 92#include <security/audit/audit.h>
2d21ac55
A
93#if CONFIG_MACF
94#include <security/mac_framework.h>
95#endif
f427ee49 96#include <os/atomic_private.h>
2d21ac55 97
cb323159
A
98#include <sys/paths.h>
99
2d21ac55
A
100#if NAMEDRSRCFORK
101#include <sys/xattr.h>
1c79356b 102#endif
2d21ac55
A
103/*
104 * The minimum volfs-style pathname is 9.
105 * Example: "/.vol/1/2"
106 */
107#define VOLFS_MIN_PATH_LEN 9
1c79356b 108
91447636 109
2d21ac55
A
110#if CONFIG_VOLFS
111static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
39236c6e 112#define MAX_VOLFS_RESTARTS 5
2d21ac55
A
113#endif
114
0a7de745 115static int lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, int vbusyflags, vfs_context_t ctx);
f427ee49 116static int lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, bool* dp_has_iocount, vfs_context_t ctx);
0a7de745
A
117static int lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx);
118static void lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation);
119static 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);
122static int lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent);
6d2010ae 123
fe8ab488 124#if NAMEDRSRCFORK
0a7de745 125static int lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx);
fe8ab488 126#endif
39236c6e 127
c3c9b80d 128extern lck_rw_t rootvnode_rw_lock;
39236c6e 129
1c79356b
A
130/*
131 * Convert a pathname into a pointer to a locked inode.
132 *
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.
137 *
138 * The segflg defines whether the name is to be copied from user
139 * space or kernel space.
140 *
141 * Overall outline of namei:
142 *
143 * copy in name
144 * get starting directory
145 * while (!done && !error) {
146 * call lookup to search path.
147 * if symbolic link, massage name in buffer and continue
148 * }
2d21ac55
A
149 *
150 * Returns: 0 Success
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
157 * lookup:EROFS
158 * lookup:EACCES
159 * lookup:EPERM
4a3eedf9
A
160 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
161 * This means we should re-drive lookup from this point.
162 * lookup: ???
2d21ac55 163 * VNOP_READLINK:???
1c79356b
A
164 */
165int
2d21ac55 166namei(struct nameidata *ndp)
1c79356b 167{
0a7de745
A
168 struct filedesc *fdp; /* pointer to file descriptor state */
169 struct vnode *dp; /* the directory we are searching */
4a3eedf9 170 struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to
0a7de745 171 * heavy vnode pressure */
f427ee49 172 uint32_t cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
91447636 173 int error;
1c79356b 174 struct componentname *cnp = &ndp->ni_cnd;
91447636 175 vfs_context_t ctx = cnp->cn_context;
2d21ac55 176 proc_t p = vfs_context_proc(ctx);
b0d623f7 177#if CONFIG_AUDIT
2d21ac55
A
178/* XXX ut should be from context */
179 uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
b0d623f7 180#endif
6d2010ae 181
39236c6e
A
182#if CONFIG_VOLFS
183 int volfs_restarts = 0;
184#endif
5ba3f43e 185 size_t bytes_copied = 0;
f427ee49
A
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;
39236c6e 191
6d2010ae 192 fdp = p->p_fd;
1c79356b 193
1c79356b 194#if DIAGNOSTIC
0a7de745
A
195 if (!vfs_context_ucred(ctx) || !p) {
196 panic("namei: bad cred/proc");
197 }
198 if (cnp->cn_nameiop & (~OPMASK)) {
199 panic("namei: nameiop contaminated with flags");
200 }
201 if (cnp->cn_flags & OPMASK) {
202 panic("namei: flags contaminated with nameiops");
203 }
1c79356b 204#endif
6d2010ae
A
205
206 /*
207 * A compound VNOP found something that needs further processing:
208 * either a trigger vnode, a covered directory, or a symlink.
209 */
210 if (ndp->ni_flag & NAMEI_CONTLOOKUP) {
211 int rdonly, vbusyflags, keep_going, wantparent;
212
213 rdonly = cnp->cn_flags & RDONLY;
214 vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0;
215 keep_going = 0;
216 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
217
218 ndp->ni_flag &= ~(NAMEI_CONTLOOKUP);
219
0a7de745
A
220 error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
221 &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
222 if (error) {
6d2010ae 223 goto out_drop;
0a7de745 224 }
6d2010ae
A
225 if (keep_going) {
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);
228 }
229 goto continue_symlink;
230 }
231
232 return 0;
6d2010ae 233 }
1c79356b 234
4a3eedf9
A
235vnode_recycled:
236
1c79356b
A
237 /*
238 * Get a buffer for the name to be translated, and copy the
239 * name into the buffer.
240 */
241 if ((cnp->cn_flags & HASBUF) == 0) {
2d21ac55 242 cnp->cn_pnbuf = ndp->ni_pathbuf;
91447636 243 cnp->cn_pnlen = PATHBUFLEN;
1c79356b 244 }
91447636 245#if LP64_DEBUG
b0d623f7 246 if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
0a7de745
A
247 && (ndp->ni_segflg != UIO_SYSSPACE)
248 && (ndp->ni_segflg != UIO_SYSSPACE32)) {
249 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
91447636
A
250 }
251#endif /* LP64_DEBUG */
252
253retry_copy:
2d21ac55 254 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
1c79356b 255 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
0a7de745 256 cnp->cn_pnlen, &bytes_copied);
2d21ac55 257 } else {
91447636 258 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
0a7de745 259 cnp->cn_pnlen, &bytes_copied);
2d21ac55 260 }
91447636 261 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
f427ee49 262 cnp->cn_pnbuf = zalloc(ZV_NAMEI);
91447636
A
263 cnp->cn_flags |= HASBUF;
264 cnp->cn_pnlen = MAXPATHLEN;
5ba3f43e 265 bytes_copied = 0;
0a7de745 266
91447636
A
267 goto retry_copy;
268 }
0a7de745
A
269 if (error) {
270 goto error_out;
271 }
f427ee49
A
272 assert(bytes_copied <= MAXPATHLEN);
273 ndp->ni_pathlen = (u_int)bytes_copied;
5ba3f43e 274 bytes_copied = 0;
55e303ae 275
39236c6e
A
276 /*
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.
282 */
0a7de745 283 if (proc_is_forcing_hfs_case_sensitivity(p)) {
39236c6e 284 cnp->cn_flags |= CN_SKIPNAMECACHE;
0a7de745 285 }
39236c6e 286
2d21ac55 287#if CONFIG_VOLFS
0a7de745 288 /*
2d21ac55
A
289 * Check for legacy volfs style pathnames.
290 *
291 * For compatibility reasons we currently allow these paths,
292 * but future versions of the OS may not support them.
293 */
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' &&
0a7de745 300 cnp->cn_pnbuf[5] == '/') {
2d21ac55
A
301 char * realpath;
302 int realpath_err;
303 /* Attempt to resolve a legacy volfs style pathname. */
f427ee49
A
304 realpath = zalloc(ZV_NAMEI);
305 /*
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
310 */
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;
315 goto error_out;
2d21ac55 316 }
f427ee49
A
317 } else {
318 size_t tmp_len;
319 if (cnp->cn_flags & HASBUF) {
320 zfree(ZV_NAMEI, cnp->cn_pnbuf);
321 }
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;
2d21ac55
A
328 }
329 }
b0d623f7 330#endif /* CONFIG_VOLFS */
2d21ac55 331
b0d623f7 332#if CONFIG_AUDIT
55e303ae 333 /* If we are auditing the kernel pathname, save the user pathname */
0a7de745
A
334 if (cnp->cn_flags & AUDITVNPATH1) {
335 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
336 }
337 if (cnp->cn_flags & AUDITVNPATH2) {
338 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
339 }
b0d623f7 340#endif /* CONFIG_AUDIT */
55e303ae 341
1c79356b
A
342 /*
343 * Do not allow empty pathnames
344 */
91447636 345 if (*cnp->cn_pnbuf == '\0') {
1c79356b 346 error = ENOENT;
2d21ac55 347 goto error_out;
1c79356b 348 }
f427ee49
A
349 if (ndp->ni_flag & NAMEI_NOFOLLOW_ANY) {
350 ndp->ni_loopcnt = MAXSYMLINKS;
351 } else {
352 ndp->ni_loopcnt = 0;
353 }
1c79356b
A
354
355 /*
91447636 356 * determine the starting point for the translation.
1c79356b 357 */
f427ee49 358 proc_dirs_lock_shared(p);
c3c9b80d 359 lck_rw_lock_shared(&rootvnode_rw_lock);
f427ee49 360
4ba76501
A
361 if (!(fdp->fd_flags & FD_CHROOT)) {
362 ndp->ni_rootdir = rootvnode;
363 } else {
364 ndp->ni_rootdir = fdp->fd_rdir;
365 }
94ff46dc 366
4ba76501 367 if (!ndp->ni_rootdir) {
0a7de745 368 if (!(fdp->fd_flags & FD_CHROOT)) {
4ba76501 369 printf("rootvnode is not set\n");
94ff46dc 370 } else {
94ff46dc 371 /* This should be a panic */
4ba76501 372 printf("fdp->fd_rdir is not set\n");
0a7de745 373 }
c3c9b80d 374 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49 375 proc_dirs_unlock_shared(p);
4ba76501
A
376 error = ENOENT;
377 goto error_out;
55e303ae 378 }
94ff46dc 379
91447636 380 cnp->cn_nameptr = cnp->cn_pnbuf;
55e303ae 381
91447636
A
382 ndp->ni_usedvp = NULLVP;
383
384 if (*(cnp->cn_nameptr) == '/') {
0a7de745
A
385 while (*(cnp->cn_nameptr) == '/') {
386 cnp->cn_nameptr++;
91447636 387 ndp->ni_pathlen--;
1c79356b 388 }
91447636
A
389 dp = ndp->ni_rootdir;
390 } else if (cnp->cn_flags & USEDVP) {
0a7de745 391 dp = ndp->ni_dvp;
91447636 392 ndp->ni_usedvp = dp;
f427ee49 393 usedvp_dp = dp;
0a7de745 394 } else {
bca245ac 395 dp = vfs_context_cwd(ctx);
0a7de745 396 }
91447636 397
2d21ac55 398 if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
94ff46dc 399 dp = NULLVP;
c3c9b80d 400 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49 401 proc_dirs_unlock_shared(p);
0a7de745 402 error = ENOENT;
91447636
A
403 goto error_out;
404 }
94ff46dc 405
f427ee49
A
406 /*
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.
413 *
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
421 * happening.
422 */
423 rootdir_with_usecount = ndp->ni_rootdir;
424 old_count = os_atomic_inc_orig(&rootdir_with_usecount->v_usecount, relaxed);
425 if (old_count < 1) {
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);
430 }
431
432 if ((dp != rootdir_with_usecount) && (dp != usedvp_dp)) {
433 old_count = os_atomic_inc_orig(&dp->v_usecount, relaxed);
434 if (old_count < 1) {
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);
438 }
439 startdir_with_usecount = dp;
440 }
441
442 /* Now that we have our usecount, release the locks */
c3c9b80d 443 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49
A
444 proc_dirs_unlock_shared(p);
445
91447636
A
446 ndp->ni_dvp = NULLVP;
447 ndp->ni_vp = NULLVP;
448
449 for (;;) {
5ba3f43e
A
450#if CONFIG_MACF
451 /*
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.
458 */
459 error = mac_vnode_check_lookup_preflight(ctx, dp, cnp->cn_nameptr, cnp->cn_namelen);
460 if (error) {
461 goto error_out;
462 }
463#endif
1c79356b 464 ndp->ni_startdir = dp;
94ff46dc 465 dp = NULLVP;
91447636 466
0a7de745 467 if ((error = lookup(ndp))) {
91447636 468 goto error_out;
1c79356b 469 }
3e170ce0 470
1c79356b
A
471 /*
472 * Check for symbolic link
473 */
474 if ((cnp->cn_flags & ISSYMLINK) == 0) {
f427ee49
A
475 if (startdir_with_usecount) {
476 vnode_rele(startdir_with_usecount);
477 startdir_with_usecount = NULLVP;
478 }
479 if (rootdir_with_usecount) {
c3c9b80d 480 lck_rw_lock_shared(&rootvnode_rw_lock);
f427ee49
A
481 if (rootdir_with_usecount == rootvnode) {
482 old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
483 if (old_count < 2) {
484 /*
485 * There needs to have been at least 1 usecount left on the rootvnode
486 */
487 panic("(3) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
488 old_count, rootdir_with_usecount);
489 }
490 rootdir_with_usecount = NULLVP;
491 }
c3c9b80d 492 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49
A
493 if (rootdir_with_usecount) {
494 vnode_rele(rootdir_with_usecount);
495 rootdir_with_usecount = NULLVP;
496 }
94ff46dc 497 }
f427ee49 498
0a7de745 499 return 0;
1c79356b 500 }
91447636 501
6d2010ae 502continue_symlink:
bca245ac 503 /* Gives us a new path to process, and a starting dir */
f427ee49 504 error = lookup_handle_symlink(ndp, &dp, &dp_has_iocount, ctx);
6d2010ae 505 if (error != 0) {
1c79356b
A
506 break;
507 }
f427ee49
A
508 if (dp_has_iocount) {
509 if ((dp != rootdir_with_usecount) && (dp != startdir_with_usecount) &&
510 (dp != usedvp_dp)) {
511 if (startdir_with_usecount) {
512 vnode_rele(startdir_with_usecount);
513 }
514 vnode_ref_ext(dp, 0, VNODE_REF_FORCE);
515 startdir_with_usecount = dp;
516 }
517 vnode_put(dp);
518 dp_has_iocount = false;
519 }
91447636
A
520 }
521 /*
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
526 */
6d2010ae 527out_drop:
0a7de745
A
528 if (ndp->ni_dvp) {
529 vnode_put(ndp->ni_dvp);
530 }
531 if (ndp->ni_vp) {
532 vnode_put(ndp->ni_vp);
533 }
534error_out:
f427ee49
A
535 if (startdir_with_usecount) {
536 vnode_rele(startdir_with_usecount);
537 startdir_with_usecount = NULLVP;
538 }
539 if (rootdir_with_usecount) {
c3c9b80d 540 lck_rw_lock_shared(&rootvnode_rw_lock);
f427ee49
A
541 if (rootdir_with_usecount == rootvnode) {
542 old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
543 if (old_count < 2) {
544 /*
545 * There needs to have been at least 1 usecount left on the rootvnode
546 */
547 panic("(4) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
548 old_count, rootdir_with_usecount);
549 }
c3c9b80d 550 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49 551 } else {
c3c9b80d 552 lck_rw_unlock_shared(&rootvnode_rw_lock);
f427ee49
A
553 vnode_rele(rootdir_with_usecount);
554 }
555 rootdir_with_usecount = NULLVP;
bca245ac 556 }
f427ee49 557
0a7de745 558 if ((cnp->cn_flags & HASBUF)) {
2d21ac55 559 cnp->cn_flags &= ~HASBUF;
f427ee49 560 zfree(ZV_NAMEI, cnp->cn_pnbuf);
91447636 561 }
55e303ae 562 cnp->cn_pnbuf = NULL;
91447636 563 ndp->ni_vp = NULLVP;
6d2010ae 564 ndp->ni_dvp = NULLVP;
39236c6e
A
565
566#if CONFIG_VOLFS
567 /*
0a7de745 568 * Deal with volfs fallout.
39236c6e
A
569 *
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
0a7de745
A
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.
39236c6e
A
576 *
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
0a7de745
A
581 * path, we may have gotten an ENOENT.
582 *
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.
39236c6e 586 *
39236c6e
A
587 * Do this dance a maximum of MAX_VOLFS_RESTARTS times.
588 */
589 if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) {
590 if (volfs_restarts < MAX_VOLFS_RESTARTS) {
591 volfs_restarts++;
592 goto vnode_recycled;
593 }
594 }
595#endif
596
0a7de745
A
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*/
4a3eedf9
A
600 ndp->ni_cnd.cn_flags = cnpflags;
601 ndp->ni_dvp = usedvp;
602 goto vnode_recycled;
603 }
604
55e303ae 605
0a7de745 606 return error;
1c79356b
A
607}
608
0a7de745 609int
6d2010ae
A
610namei_compound_available(vnode_t dp, struct nameidata *ndp)
611{
612 if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) {
613 return vnode_compound_open_available(dp);
614 }
91447636 615
6d2010ae
A
616 return 0;
617}
5ba3f43e 618
fe8ab488 619static int
6d2010ae 620lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
1c79356b 621{
39236c6e
A
622#if !CONFIG_MACF
623#pragma unused(cnp)
624#endif
625
6d2010ae 626 int error;
1c79356b 627
6d2010ae
A
628 if (!dp_authorized_in_cache) {
629 error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
0a7de745 630 if (error) {
6d2010ae 631 return error;
0a7de745 632 }
91447636 633 }
6d2010ae
A
634#if CONFIG_MACF
635 error = mac_vnode_check_lookup(ctx, dp, cnp);
0a7de745 636 if (error) {
6d2010ae 637 return error;
0a7de745 638 }
6d2010ae 639#endif /* CONFIG_MACF */
91447636 640
6d2010ae
A
641 return 0;
642}
1c79356b 643
0a7de745
A
644static void
645lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
6d2010ae
A
646{
647 int isdot_or_dotdot;
648 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
55e303ae 649
6d2010ae
A
650 if (vp->v_name == NULL || vp->v_parent == NULLVP) {
651 int update_flags = 0;
652
653 if (isdot_or_dotdot == 0) {
0a7de745 654 if (vp->v_name == NULL) {
6d2010ae 655 update_flags |= VNODE_UPDATE_NAME;
0a7de745
A
656 }
657 if (dvp != NULLVP && vp->v_parent == NULLVP) {
6d2010ae 658 update_flags |= VNODE_UPDATE_PARENT;
0a7de745 659 }
6d2010ae 660
0a7de745 661 if (update_flags) {
6d2010ae 662 vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
0a7de745 663 }
55e303ae 664 }
1c79356b 665 }
0a7de745 666 if ((cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
6d2010ae
A
667 /*
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
672 * vnode didn't...
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
679 */
0a7de745 680 if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot)) {
6d2010ae 681 cache_enter_with_gen(dvp, vp, cnp, nc_generation);
0a7de745 682 }
6d2010ae 683 }
6d2010ae
A
684}
685
686#if NAMEDRSRCFORK
687/*
688 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
0a7de745 689 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
6d2010ae
A
690 * (if one was provided).
691 */
fe8ab488 692static int
6d2010ae
A
693lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx)
694{
695 vnode_t svp = NULLVP;
696 enum nsoperation nsop;
5ba3f43e 697 int nsflags;
6d2010ae
A
698 int error;
699
700 if (dp->v_type != VREG) {
701 error = ENOENT;
702 goto out;
91447636 703 }
6d2010ae 704 switch (cnp->cn_nameiop) {
0a7de745
A
705 case DELETE:
706 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
707 nsop = NS_DELETE;
708 } else {
709 error = EPERM;
710 goto out;
711 }
712 break;
713 case CREATE:
714 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
715 nsop = NS_CREATE;
716 } else {
717 error = EPERM;
718 goto out;
719 }
720 break;
721 case LOOKUP:
722 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
723 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
724 nsop = NS_OPEN;
725 } else {
6d2010ae
A
726 error = EPERM;
727 goto out;
0a7de745
A
728 }
729 break;
730 default:
731 error = EPERM;
732 goto out;
6d2010ae 733 }
5ba3f43e
A
734
735 nsflags = 0;
0a7de745 736 if (cnp->cn_flags & CN_RAW_ENCRYPTED) {
5ba3f43e 737 nsflags |= NS_GETRAWENCRYPTED;
0a7de745 738 }
5ba3f43e 739
6d2010ae 740 /* Ask the file system for the resource fork. */
5ba3f43e 741 error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, nsflags, ctx);
91447636 742
6d2010ae
A
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;
0a7de745 746 }
6d2010ae
A
747 if (error) {
748 goto out;
749 }
750 /* The "parent" of the stream is the file. */
751 if (wantparent) {
752 if (ndp->ni_dvp) {
6d2010ae
A
753 vnode_put(ndp->ni_dvp);
754 }
755 ndp->ni_dvp = dp;
756 } else {
757 vnode_put(dp);
758 }
759 ndp->ni_vp = svp; /* on create this may be null */
91447636 760
6d2010ae
A
761 /* Restore the truncated pathname buffer (for audits). */
762 if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
cb323159
A
763 /*
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
769 * entire suffix.
770 */
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__);
777 }
6d2010ae
A
778 }
779 cnp->cn_flags &= ~MAKEENTRY;
2d21ac55 780
6d2010ae
A
781 return 0;
782out:
783 return error;
784}
785#endif /* NAMEDRSRCFORK */
786
787/*
788 * iocounts in:
0a7de745
A
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.
6d2010ae
A
791 *
792 * iocounts out:
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
0a7de745 796 * nothing else.
6d2010ae
A
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
799 * was dropped).
800 */
0a7de745
A
801static int
802lookup_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)
6d2010ae
A
805{
806 vnode_t dp;
807 int error;
808 char *cp;
809
810 dp = ndp->ni_vp;
811 *keep_going = 0;
812
813 if (ndp->ni_vp == NULLVP) {
814 panic("NULL ni_vp in %s\n", __FUNCTION__);
815 }
816
817 if (atroot) {
818 goto nextname;
819 }
820
6d2010ae
A
821 /*
822 * Take into account any additional components consumed by
823 * the underlying filesystem.
824 */
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;
829 cnp->cn_consume = 0;
830 } else {
831 lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation);
832 }
833
834 /*
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.
838 */
839 error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx);
840 dp = ndp->ni_vp;
841 if (error) {
842 goto out;
843 }
844
845#if CONFIG_MACF
846 if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
847 error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx);
0a7de745 848 if (error) {
6d2010ae 849 goto out;
0a7de745 850 }
6d2010ae
A
851 }
852#endif
853
854 /*
855 * Check for symbolic link
856 */
857 if ((dp->v_type == VLNK) &&
858 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
859 cnp->cn_flags |= ISSYMLINK;
860 *keep_going = 1;
0a7de745 861 return 0;
6d2010ae
A
862 }
863
864 /*
865 * Check for bogus trailing slashes.
866 */
867 if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
868 if (dp->v_type != VDIR) {
869 error = ENOTDIR;
870 goto out;
871 }
872 ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
0a7de745 873 }
4b17d6b6
A
874
875#if NAMEDSTREAMS
0a7de745 876 /*
4b17d6b6
A
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
880 */
0a7de745 881 if (vnode_isshadow(dp)) {
4b17d6b6
A
882 error = ENOENT;
883 goto out;
884 }
885#endif
886
6d2010ae
A
887nextname:
888 /*
889 * Not a symbolic link. If more pathname,
890 * continue at next component, else return.
891 *
0a7de745 892 * Definitely have a dvp if there's another slash
6d2010ae
A
893 */
894 if (*ndp->ni_next == '/') {
895 cnp->cn_nameptr = ndp->ni_next + 1;
896 ndp->ni_pathlen--;
897 while (*cnp->cn_nameptr == '/') {
898 cnp->cn_nameptr++;
899 ndp->ni_pathlen--;
900 }
901
902 cp = cnp->cn_nameptr;
903 vnode_put(ndp->ni_dvp);
904 ndp->ni_dvp = NULLVP;
905
906 if (*cp == '\0') {
907 goto emptyname;
908 }
909
910 *keep_going = 1;
911 return 0;
912 }
0a7de745 913
6d2010ae
A
914 /*
915 * Disallow directory write attempts on read-only file systems.
916 */
917 if (rdonly &&
918 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
919 error = EROFS;
920 goto out;
921 }
0a7de745 922
6d2010ae
A
923 /* If SAVESTART is set, we should have a dvp */
924 if (cnp->cn_flags & SAVESTART) {
0a7de745 925 /*
6d2010ae
A
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'
930 */
0a7de745
A
931 if ((vnode_get(ndp->ni_dvp))) {
932 error = ENOENT;
6d2010ae
A
933 goto out;
934 }
935 ndp->ni_startdir = ndp->ni_dvp;
936 }
937 if (!wantparent && ndp->ni_dvp) {
938 vnode_put(ndp->ni_dvp);
939 ndp->ni_dvp = NULLVP;
940 }
941
0a7de745 942 if (cnp->cn_flags & AUDITVNPATH1) {
6d2010ae 943 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
0a7de745 944 } else if (cnp->cn_flags & AUDITVNPATH2) {
6d2010ae 945 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
0a7de745 946 }
6d2010ae
A
947
948#if NAMEDRSRCFORK
949 /*
950 * Caller wants the resource fork.
951 */
952 if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
953 error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx);
0a7de745 954 if (error != 0) {
6d2010ae 955 goto out;
0a7de745 956 }
6d2010ae
A
957
958 dp = ndp->ni_vp;
959 }
960#endif
0a7de745
A
961 if (kdebug_enable) {
962 kdebug_lookup(ndp->ni_vp, cnp);
963 }
6d2010ae
A
964
965 return 0;
966
967emptyname:
968 error = lookup_handle_emptyname(ndp, cnp, wantparent);
0a7de745 969 if (error != 0) {
6d2010ae 970 goto out;
0a7de745 971 }
6d2010ae
A
972
973 return 0;
974out:
975 return error;
6d2010ae
A
976}
977
978/*
979 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
980 */
0a7de745 981static int
6d2010ae
A
982lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent)
983{
984 vnode_t dp;
985 int error = 0;
986
987 dp = ndp->ni_vp;
988 cnp->cn_namelen = 0;
989 /*
990 * A degenerate name (e.g. / or "") which is a way of
991 * talking about a directory, e.g. like "/." or ".".
992 */
993 if (dp->v_type != VDIR) {
994 error = ENOTDIR;
995 goto out;
996 }
997 if (cnp->cn_nameiop != LOOKUP) {
998 error = EISDIR;
999 goto out;
1000 }
1001 if (wantparent) {
0a7de745 1002 /*
6d2010ae
A
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'
1007 */
0a7de745
A
1008 if ((vnode_get(dp))) {
1009 error = ENOENT;
6d2010ae
A
1010 goto out;
1011 }
1012 ndp->ni_dvp = dp;
1013 }
1014 cnp->cn_flags &= ~ISDOTDOT;
1015 cnp->cn_flags |= ISLASTCN;
1016 ndp->ni_next = cnp->cn_nameptr;
1017 ndp->ni_vp = dp;
1018
0a7de745 1019 if (cnp->cn_flags & AUDITVNPATH1) {
6d2010ae 1020 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
0a7de745 1021 } else if (cnp->cn_flags & AUDITVNPATH2) {
6d2010ae 1022 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
0a7de745
A
1023 }
1024 if (cnp->cn_flags & SAVESTART) {
6d2010ae 1025 panic("lookup: SAVESTART");
0a7de745 1026 }
6d2010ae
A
1027
1028 return 0;
1029out:
1030 return error;
1031}
1032/*
1033 * Search a pathname.
1034 * This is a very central and rather complicated routine.
1035 *
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.
1041 *
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 ".".
0a7de745 1053 *
6d2010ae
A
1054 * Overall outline of lookup:
1055 *
1056 * dirloop:
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
1069 *
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)
1083 * VNOP_LOOKUP:???
1084 * VFS_ROOT:ENOTSUP
1085 * VFS_ROOT:ENOENT
1086 * VFS_ROOT:???
1087 */
1088int
1089lookup(struct nameidata *ndp)
1090{
0a7de745
A
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 */
6d2010ae
A
1097 int dp_authorized = 0;
1098 int error = 0;
1099 struct componentname *cnp = &ndp->ni_cnd;
1100 vfs_context_t ctx = cnp->cn_context;
1101 int vbusyflags = 0;
1102 int nc_generation = 0;
1103 vnode_t last_dp = NULLVP;
1104 int keep_going;
1105 int atroot;
1106
1107 /*
1108 * Setup: break out flag bits into variables.
1109 */
fe8ab488 1110 if (cnp->cn_flags & NOCACHE) {
0a7de745 1111 docache = 0;
6d2010ae
A
1112 }
1113 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1114 rdonly = cnp->cn_flags & RDONLY;
1115 cnp->cn_flags &= ~ISSYMLINK;
1116 cnp->cn_consume = 0;
1117
1118 dp = ndp->ni_startdir;
1119 ndp->ni_startdir = NULLVP;
1120
0a7de745
A
1121 if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) {
1122 vbusyflags = LK_NOWAIT;
1123 }
6d2010ae
A
1124 cp = cnp->cn_nameptr;
1125
1126 if (*cp == '\0') {
0a7de745 1127 if ((vnode_getwithref(dp))) {
6d2010ae 1128 dp = NULLVP;
0a7de745 1129 error = ENOENT;
6d2010ae
A
1130 goto bad;
1131 }
1132 ndp->ni_vp = dp;
1133 error = lookup_handle_emptyname(ndp, cnp, wantparent);
1134 if (error) {
1135 goto bad;
1136 }
1137
1138 return 0;
1139 }
0a7de745 1140dirloop:
6d2010ae
A
1141 atroot = 0;
1142 ndp->ni_vp = NULLVP;
1143
0a7de745 1144 if ((error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp))) {
6d2010ae
A
1145 dp = NULLVP;
1146 goto bad;
1147 }
1148 if ((cnp->cn_flags & ISLASTCN)) {
0a7de745
A
1149 if (docache) {
1150 cnp->cn_flags |= MAKEENTRY;
1151 }
1152 } else {
1153 cnp->cn_flags |= MAKEENTRY;
1154 }
6d2010ae
A
1155
1156 dp = ndp->ni_dvp;
1157
1158 if (ndp->ni_vp != NULLVP) {
0a7de745 1159 /*
6d2010ae 1160 * cache_lookup_path returned a non-NULL ni_vp then,
0a7de745 1161 * we're guaranteed that the dp is a VDIR, it's
6d2010ae
A
1162 * been authorized, and vp is not ".."
1163 *
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
1168 */
0a7de745
A
1169 if (dp != NULLVP) {
1170 nc_generation = dp->v_nc_generation - 1;
1171 }
6d2010ae 1172
0a7de745 1173 goto returned_from_lookup_path;
9bccf70c 1174 }
1c79356b 1175
1c79356b
A
1176 /*
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.
1185 */
0a7de745 1186 if ((cnp->cn_flags & ISDOTDOT)) {
39037602
A
1187 /*
1188 * if this is a chroot'ed process, check if the current
1189 * directory is still a subdirectory of the process's
1190 * root directory.
1191 */
1192 if (ndp->ni_rootdir && (ndp->ni_rootdir != rootvnode) &&
0a7de745 1193 dp != ndp->ni_rootdir) {
39037602
A
1194 int sdir_error;
1195 int is_subdir = FALSE;
1196
1197 sdir_error = vnode_issubdir(dp, ndp->ni_rootdir,
1198 &is_subdir, vfs_context_kernel());
1199
1200 /*
1201 * If we couldn't determine if dp is a subdirectory of
1202 * ndp->ni_rootdir (sdir_error != 0), we let the request
1203 * proceed.
1204 */
1205 if (!sdir_error && !is_subdir) {
1206 vnode_put(dp);
1207 dp = ndp->ni_rootdir;
1208 /*
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.
1213 */
1214 if (vnode_get(dp)) {
1215 error = ENOENT;
1216 goto bad;
1217 }
1218 }
1219 }
1220
1c79356b 1221 for (;;) {
0a7de745
A
1222 if (dp == ndp->ni_rootdir || dp == rootvnode) {
1223 ndp->ni_dvp = dp;
1c79356b 1224 ndp->ni_vp = dp;
91447636
A
1225 /*
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'
1232 */
0a7de745 1233 if ((vnode_get(dp))) {
91447636
A
1234 error = ENOENT;
1235 goto bad;
1236 }
6d2010ae
A
1237 atroot = 1;
1238 goto returned_from_lookup_path;
1c79356b
A
1239 }
1240 if ((dp->v_flag & VROOT) == 0 ||
0a7de745
A
1241 (cnp->cn_flags & NOCROSSMOUNT)) {
1242 break;
1243 }
1244 if (dp->v_mount == NULL) { /* forced umount */
1245 error = EBADF;
0b4e3aa0
A
1246 goto bad;
1247 }
1c79356b 1248 tdp = dp;
91447636
A
1249 dp = tdp->v_mount->mnt_vnodecovered;
1250
0a7de745 1251 if ((vnode_getwithref(dp))) {
ea3f0419 1252 vnode_put(tdp);
0a7de745 1253 dp = NULLVP;
91447636
A
1254 error = ENOENT;
1255 goto bad;
1256 }
ea3f0419
A
1257
1258 vnode_put(tdp);
1259
91447636
A
1260 ndp->ni_dvp = dp;
1261 dp_authorized = 0;
1c79356b
A
1262 }
1263 }
1264
1265 /*
1266 * We now have a segment name to search for, and a directory to search.
1267 */
1268unionlookup:
91447636
A
1269 ndp->ni_vp = NULLVP;
1270
1271 if (dp->v_type != VDIR) {
0a7de745
A
1272 error = ENOTDIR;
1273 goto lookup_error;
91447636 1274 }
0a7de745 1275 if ((cnp->cn_flags & DONOTAUTH) != DONOTAUTH) {
6d2010ae
A
1276 error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
1277 if (error) {
2d21ac55 1278 goto lookup_error;
6d2010ae
A
1279 }
1280 }
1281
1282 /*
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.
1285 */
1286#if NAMEDRSRCFORK
0a7de745
A
1287 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
1288#else
6d2010ae
A
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;
1293 return 0;
91447636 1294 }
2d21ac55 1295
0a7de745 1296 nc_generation = dp->v_nc_generation;
2d21ac55 1297
39236c6e 1298 /*
0a7de745 1299 * Note:
39236c6e
A
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!
1307 *
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
0a7de745 1310 * valid path back, and not necessarily the one that you wanted.
39236c6e 1311 *
0a7de745 1312 * Example:
39236c6e 1313 * /tmp/a == /foo/b
0a7de745
A
1314 *
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
39236c6e
A
1318 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1319 */
6d2010ae 1320
39236c6e 1321 error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
6d2010ae 1322
0a7de745 1323 if (error) {
91447636 1324lookup_error:
1c79356b 1325 if ((error == ENOENT) &&
39236c6e 1326 (dp->v_mount != NULL) &&
1c79356b
A
1327 (dp->v_mount->mnt_flag & MNT_UNION)) {
1328 tdp = dp;
39236c6e 1329 error = lookup_traverse_union(tdp, &dp, ctx);
91447636 1330 vnode_put(tdp);
39236c6e 1331 if (error) {
0a7de745 1332 dp = NULLVP;
91447636
A
1333 goto bad;
1334 }
39236c6e 1335
91447636
A
1336 ndp->ni_dvp = dp;
1337 dp_authorized = 0;
1c79356b
A
1338 goto unionlookup;
1339 }
1340
0a7de745 1341 if (error != EJUSTRETURN) {
1c79356b 1342 goto bad;
0a7de745 1343 }
91447636 1344
0a7de745 1345 if (ndp->ni_vp != NULLVP) {
91447636 1346 panic("leaf should be empty");
0a7de745 1347 }
91447636 1348
39236c6e 1349#if NAMEDRSRCFORK
0a7de745 1350 /*
39236c6e 1351 * At this point, error should be EJUSTRETURN.
0a7de745
A
1352 *
1353 * If CN_WANTSRSRCFORK is set, that implies that the
39236c6e 1354 * underlying filesystem could not find the "parent" of the
0a7de745 1355 * resource fork (the data fork), and we are doing a lookup
39236c6e
A
1356 * for a CREATE event.
1357 *
1358 * However, this should be converted to an error, as the
1359 * failure to find this parent should disallow further
0a7de745 1360 * progress to try and acquire a resource fork vnode.
39236c6e
A
1361 */
1362 if (cnp->cn_flags & CN_WANTSRSRCFORK) {
1363 error = ENOENT;
1364 goto bad;
1365 }
1366#endif
1367
6d2010ae 1368 error = lookup_validate_creation_path(ndp);
0a7de745 1369 if (error) {
9bccf70c 1370 goto bad;
0a7de745 1371 }
1c79356b
A
1372 /*
1373 * We return with ni_vp NULL to indicate that the entry
1374 * doesn't currently exist, leaving a pointer to the
91447636 1375 * referenced directory vnode in ndp->ni_dvp.
1c79356b
A
1376 */
1377 if (cnp->cn_flags & SAVESTART) {
0a7de745 1378 if ((vnode_get(ndp->ni_dvp))) {
91447636
A
1379 error = ENOENT;
1380 goto bad;
1381 }
1c79356b 1382 ndp->ni_startdir = ndp->ni_dvp;
1c79356b 1383 }
0a7de745
A
1384 if (!wantparent) {
1385 vnode_put(ndp->ni_dvp);
1386 }
91447636 1387
0a7de745
A
1388 if (kdebug_enable) {
1389 kdebug_lookup(ndp->ni_dvp, cnp);
1390 }
1391 return 0;
1c79356b 1392 }
91447636 1393returned_from_lookup_path:
6d2010ae
A
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);
1396 if (error != 0) {
0a7de745 1397 goto bad2;
1c79356b
A
1398 }
1399
6d2010ae
A
1400 if (keep_going) {
1401 dp = ndp->ni_vp;
2d21ac55 1402
6d2010ae
A
1403 /* namei() will handle symlinks */
1404 if ((dp->v_type == VLNK) &&
0a7de745
A
1405 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
1406 return 0;
2d21ac55 1407 }
1c79356b 1408
6d2010ae 1409 /*
0a7de745 1410 * Otherwise, there's more path to process.
6d2010ae
A
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.
91447636 1414 */
6d2010ae
A
1415 last_dp = dp;
1416
1417 goto dirloop;
55e303ae 1418 }
91447636 1419
0a7de745 1420 return 0;
1c79356b 1421bad2:
0a7de745 1422 if (ndp->ni_dvp) {
6d2010ae 1423 vnode_put(ndp->ni_dvp);
0a7de745 1424 }
6d2010ae
A
1425
1426 vnode_put(ndp->ni_vp);
91447636
A
1427 ndp->ni_vp = NULLVP;
1428
0a7de745
A
1429 if (kdebug_enable) {
1430 kdebug_lookup(dp, cnp);
1431 }
1432 return error;
91447636 1433
1c79356b 1434bad:
0a7de745
A
1435 if (dp) {
1436 vnode_put(dp);
1437 }
91447636
A
1438 ndp->ni_vp = NULLVP;
1439
0a7de745
A
1440 if (kdebug_enable) {
1441 kdebug_lookup(dp, cnp);
1442 }
1443 return error;
1c79356b
A
1444}
1445
39236c6e
A
1446/*
1447 * Given a vnode in a union mount, traverse to the equivalent
1448 * vnode in the underlying mount.
1449 */
1450int
1451lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx)
1452{
1453 char *path = NULL, *pp;
1454 const char *name, *np;
f427ee49 1455 size_t len;
39236c6e
A
1456 int error = 0;
1457 struct nameidata nd;
1458 vnode_t vp = dvp;
1459
1460 *new_dvp = NULL;
1461
1462 if (vp && vp->v_flag & VROOT) {
1463 *new_dvp = vp->v_mount->mnt_vnodecovered;
0a7de745 1464 if (vnode_getwithref(*new_dvp)) {
39236c6e 1465 return ENOENT;
0a7de745 1466 }
39236c6e
A
1467 return 0;
1468 }
1469
f427ee49 1470 path = (char *) zalloc(ZV_NAMEI);
39236c6e
A
1471 if (path == NULL) {
1472 error = ENOMEM;
1473 goto done;
1474 }
1475
1476 /*
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.
1480 */
1481 pp = path + (MAXPATHLEN - 1);
1482 *pp = '\0';
1483
1484 while (1) {
1485 name = vnode_getname(vp);
1486 if (name == NULL) {
1487 printf("lookup_traverse_union: null parent name: .%s\n", pp);
1488 error = ENOENT;
1489 goto done;
1490 }
1491 len = strlen(name);
f427ee49 1492 if ((len + 1) > (size_t)(pp - path)) { // Enough space for this name ?
39236c6e
A
1493 error = ENAMETOOLONG;
1494 vnode_putname(name);
1495 goto done;
1496 }
0a7de745 1497 for (np = name + len; len > 0; len--) { // Copy name backwards
39236c6e 1498 *--pp = *--np;
0a7de745 1499 }
39236c6e
A
1500 vnode_putname(name);
1501 vp = vp->v_parent;
0a7de745 1502 if (vp == NULLVP || vp->v_flag & VROOT) {
39236c6e 1503 break;
0a7de745 1504 }
39236c6e
A
1505 *--pp = '/';
1506 }
1507
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;
1511 error = namei(&nd);
0a7de745 1512 if (error == 0) {
39236c6e 1513 *new_dvp = nd.ni_vp;
0a7de745 1514 }
39236c6e
A
1515 nameidone(&nd);
1516done:
0a7de745 1517 if (path) {
f427ee49 1518 zfree(ZV_NAMEI, path);
0a7de745 1519 }
39236c6e
A
1520 return error;
1521}
1522
0a7de745 1523int
6d2010ae
A
1524lookup_validate_creation_path(struct nameidata *ndp)
1525{
1526 struct componentname *cnp = &ndp->ni_cnd;
1527
1528 /*
1529 * If creating and at end of pathname, then can consider
1530 * allowing file to be created.
1531 */
1532 if (cnp->cn_flags & RDONLY) {
1533 return EROFS;
1534 }
1535 if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) {
1536 return ENOENT;
1537 }
0a7de745 1538
6d2010ae
A
1539 return 0;
1540}
1541
1542/*
1543 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1544 */
fe8ab488 1545static int
0a7de745
A
1546lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1547 int vbusyflags, vfs_context_t ctx)
6d2010ae
A
1548{
1549 mount_t mp;
1550 vnode_t tdp;
1551 int error = 0;
6d2010ae 1552 uint32_t depth = 0;
0a7de745 1553 vnode_t mounted_on_dp;
6d2010ae 1554 int current_mount_generation = 0;
3e170ce0
A
1555#if CONFIG_TRIGGERS
1556 vnode_t triggered_dp = NULLVP;
1557 int retry_cnt = 0;
1558#define MAX_TRIGGER_RETRIES 1
1559#endif
0a7de745
A
1560
1561 if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT) {
3e170ce0 1562 return 0;
0a7de745 1563 }
39236c6e 1564
3e170ce0 1565 mounted_on_dp = dp;
6d2010ae 1566#if CONFIG_TRIGGERS
3e170ce0 1567restart:
6d2010ae 1568#endif
3e170ce0 1569 current_mount_generation = mount_generation;
6d2010ae 1570
3e170ce0
A
1571 while (dp->v_mountedhere) {
1572 vnode_lock_spin(dp);
1573 if ((mp = dp->v_mountedhere)) {
6d2010ae
A
1574 mp->mnt_crossref++;
1575 vnode_unlock(dp);
3e170ce0
A
1576 } else {
1577 vnode_unlock(dp);
1578 break;
1579 }
6d2010ae 1580
3e170ce0
A
1581 if (ISSET(mp->mnt_lflag, MNT_LFORCE)) {
1582 mount_dropcrossref(mp, dp, 0);
0a7de745 1583 break; // don't traverse into a forced unmount
3e170ce0 1584 }
6d2010ae 1585
6d2010ae 1586
3e170ce0 1587 if (vfs_busy(mp, vbusyflags)) {
6d2010ae 1588 mount_dropcrossref(mp, dp, 0);
3e170ce0
A
1589 if (vbusyflags == LK_NOWAIT) {
1590 error = ENOENT;
6d2010ae
A
1591 goto out;
1592 }
1593
3e170ce0
A
1594 continue;
1595 }
6d2010ae 1596
3e170ce0 1597 error = VFS_ROOT(mp, &tdp, ctx);
6d2010ae 1598
3e170ce0
A
1599 mount_dropcrossref(mp, dp, 0);
1600 vfs_unbusy(mp);
1601
1602 if (error) {
1603 goto out;
1604 }
1605
1606 vnode_put(dp);
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",
1611 __FUNCTION__);
1612#else
6d2010ae 1613 break;
3e170ce0 1614#endif
6d2010ae 1615 }
3e170ce0 1616 depth++;
6d2010ae
A
1617 }
1618
3e170ce0
A
1619#if CONFIG_TRIGGERS
1620 /*
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.
1626 */
1627 if (dp->v_resolve && retry_cnt < MAX_TRIGGER_RETRIES) {
1628 error = vnode_trigger_resolve(dp, ndp, ctx);
0a7de745 1629 if (error) {
3e170ce0 1630 goto out;
0a7de745
A
1631 }
1632 if (dp == triggered_dp) {
3e170ce0 1633 retry_cnt += 1;
0a7de745 1634 } else {
3e170ce0 1635 retry_cnt = 0;
0a7de745 1636 }
3e170ce0
A
1637 triggered_dp = dp;
1638 goto restart;
1639 }
1640#endif /* CONFIG_TRIGGERS */
1641
fe8ab488 1642 if (depth) {
0a7de745 1643 mp = mounted_on_dp->v_mountedhere;
6d2010ae
A
1644
1645 if (mp) {
0a7de745 1646 mount_lock_spin(mp);
6d2010ae
A
1647 mp->mnt_realrootvp_vid = dp->v_id;
1648 mp->mnt_realrootvp = dp;
1649 mp->mnt_generation = current_mount_generation;
1650 mount_unlock(mp);
1651 }
1652 }
1653
1654 return 0;
1655
1656out:
1657 return error;
1658}
1659
1660/*
1661 * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location
bca245ac 1662 * at which to start a lookup with a resolved path, and all other iocounts dropped.
6d2010ae 1663 */
0a7de745 1664static int
f427ee49 1665lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, bool *new_dp_has_iocount, vfs_context_t ctx)
6d2010ae
A
1666{
1667 int error;
0a7de745 1668 char *cp; /* pointer into pathname argument */
6d2010ae 1669 uio_t auio;
39037602
A
1670 union {
1671 union {
1672 struct user_iovec s_uiovec;
1673 struct kern_iovec s_kiovec;
1674 } u_iovec;
1675 struct uio s_uio;
0a7de745 1676 char uio_buf[UIO_SIZEOF(1)];
39037602 1677 } u_uio_buf; /* union only for aligning uio_buf correctly */
6d2010ae
A
1678 int need_newpathbuf;
1679 u_int linklen;
1680 struct componentname *cnp = &ndp->ni_cnd;
1681 vnode_t dp;
1682 char *tmppn;
cb323159 1683 u_int rsrclen = (cnp->cn_flags & CN_WANTSRSRCFORK) ? sizeof(_PATH_RSRCFORKSPEC) : 0;
f427ee49 1684 bool dp_has_iocount = false;
6d2010ae 1685
6d2010ae
A
1686 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1687 return ELOOP;
1688 }
1689#if CONFIG_MACF
0a7de745 1690 if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0) {
6d2010ae 1691 return error;
0a7de745 1692 }
6d2010ae 1693#endif /* MAC */
0a7de745 1694 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) {
6d2010ae 1695 need_newpathbuf = 1;
0a7de745 1696 } else {
6d2010ae 1697 need_newpathbuf = 0;
0a7de745 1698 }
6d2010ae
A
1699
1700 if (need_newpathbuf) {
f427ee49 1701 cp = zalloc(ZV_NAMEI);
6d2010ae
A
1702 } else {
1703 cp = cnp->cn_pnbuf;
1704 }
39037602
A
1705 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
1706 &u_uio_buf.uio_buf[0], sizeof(u_uio_buf.uio_buf));
6d2010ae
A
1707
1708 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1709
1710 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
1711 if (error) {
0a7de745 1712 if (need_newpathbuf) {
f427ee49 1713 zfree(ZV_NAMEI, cp);
0a7de745 1714 }
6d2010ae
A
1715 return error;
1716 }
1717
0a7de745 1718 /*
6d2010ae
A
1719 * Safe to set unsigned with a [larger] signed type here
1720 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1721 * is only 1024.
1722 */
1723 linklen = MAXPATHLEN - (u_int)uio_resid(auio);
cb323159 1724 if (linklen + ndp->ni_pathlen + rsrclen > MAXPATHLEN) {
0a7de745 1725 if (need_newpathbuf) {
f427ee49 1726 zfree(ZV_NAMEI, cp);
0a7de745 1727 }
6d2010ae
A
1728
1729 return ENAMETOOLONG;
1730 }
1731 if (need_newpathbuf) {
6d2010ae
A
1732 tmppn = cnp->cn_pnbuf;
1733 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1734 cnp->cn_pnbuf = cp;
1735 cnp->cn_pnlen = MAXPATHLEN;
1736
0a7de745 1737 if ((cnp->cn_flags & HASBUF)) {
f427ee49 1738 zfree(ZV_NAMEI, tmppn);
0a7de745 1739 } else {
6d2010ae 1740 cnp->cn_flags |= HASBUF;
0a7de745
A
1741 }
1742 } else {
6d2010ae 1743 cnp->cn_pnbuf[linklen] = '\0';
0a7de745 1744 }
6d2010ae
A
1745
1746 ndp->ni_pathlen += linklen;
1747 cnp->cn_nameptr = cnp->cn_pnbuf;
1748
1749 /*
1750 * starting point for 'relative'
1751 * symbolic link path
1752 */
1753 dp = ndp->ni_dvp;
1754
1755 /*
f427ee49
A
1756 * get rid of reference returned via 'lookup'
1757 * ni_dvp is released only if we restart at /.
6d2010ae
A
1758 */
1759 vnode_put(ndp->ni_vp);
6d2010ae 1760 ndp->ni_vp = NULLVP;
bca245ac 1761 ndp->ni_dvp = NULLVP;
6d2010ae
A
1762
1763 /*
1764 * Check if symbolic link restarts us at the root
1765 */
1766 if (*(cnp->cn_nameptr) == '/') {
f427ee49 1767 vnode_put(dp); /* ALWAYS have a dvp for a symlink */
6d2010ae
A
1768 while (*(cnp->cn_nameptr) == '/') {
1769 cnp->cn_nameptr++;
1770 ndp->ni_pathlen--;
1771 }
1772 if ((dp = ndp->ni_rootdir) == NULLVP) {
1773 return ENOENT;
1774 }
f427ee49
A
1775 } else {
1776 dp_has_iocount = true;
6d2010ae
A
1777 }
1778
1779 *new_dp = dp;
f427ee49 1780 *new_dp_has_iocount = dp_has_iocount;
6d2010ae
A
1781
1782 return 0;
1783}
1784
1c79356b
A
1785/*
1786 * relookup - lookup a path name component
1787 * Used by lookup to re-aquire things.
1788 */
1789int
2d21ac55 1790relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1c79356b 1791{
0a7de745
A
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 */
1c79356b
A
1795 int error = 0;
1796#ifdef NAMEI_DIAGNOSTIC
0a7de745
A
1797 int i, newhash; /* DEBUG: check name hash */
1798 char *cp; /* DEBUG: check name ptr/len */
1c79356b 1799#endif
91447636 1800 vfs_context_t ctx = cnp->cn_context;;
1c79356b
A
1801
1802 /*
1803 * Setup: break out flag bits into variables.
1804 */
0a7de745 1805 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1c79356b
A
1806 rdonly = cnp->cn_flags & RDONLY;
1807 cnp->cn_flags &= ~ISSYMLINK;
1c79356b 1808
0a7de745
A
1809 if (cnp->cn_flags & NOCACHE) {
1810 cnp->cn_flags &= ~MAKEENTRY;
1811 } else {
1812 cnp->cn_flags |= MAKEENTRY;
1813 }
91447636
A
1814
1815 dp = dvp;
1c79356b
A
1816
1817 /*
1818 * Check for degenerate name (e.g. / or "")
1819 * which is a way of talking about a directory,
1820 * e.g. like "/." or ".".
1821 */
1822 if (cnp->cn_nameptr[0] == '\0') {
1823 if (cnp->cn_nameiop != LOOKUP || wantparent) {
1824 error = EISDIR;
1825 goto bad;
1826 }
1827 if (dp->v_type != VDIR) {
1828 error = ENOTDIR;
1829 goto bad;
1830 }
0a7de745
A
1831 if ((vnode_get(dp))) {
1832 error = ENOENT;
91447636
A
1833 goto bad;
1834 }
1c79356b 1835 *vpp = dp;
91447636 1836
0a7de745 1837 if (cnp->cn_flags & SAVESTART) {
1c79356b 1838 panic("lookup: SAVESTART");
0a7de745
A
1839 }
1840 return 0;
1c79356b 1841 }
1c79356b
A
1842 /*
1843 * We now have a segment name to search for, and a directory to search.
1844 */
0a7de745
A
1845 if ((error = VNOP_LOOKUP(dp, vpp, cnp, ctx))) {
1846 if (error != EJUSTRETURN) {
91447636 1847 goto bad;
0a7de745 1848 }
1c79356b 1849#if DIAGNOSTIC
0a7de745 1850 if (*vpp != NULL) {
1c79356b 1851 panic("leaf should be empty");
0a7de745 1852 }
1c79356b 1853#endif
1c79356b
A
1854 /*
1855 * If creating and at end of pathname, then can consider
1856 * allowing file to be created.
1857 */
1858 if (rdonly) {
1859 error = EROFS;
1860 goto bad;
1861 }
1c79356b
A
1862 /*
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.
1866 */
0a7de745 1867 return 0;
1c79356b
A
1868 }
1869 dp = *vpp;
1870
1871#if DIAGNOSTIC
1872 /*
1873 * Check for symbolic link
1874 */
0a7de745
A
1875 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) {
1876 panic("relookup: symlink found.\n");
1877 }
1c79356b
A
1878#endif
1879
1880 /*
1881 * Disallow directory write attempts on read-only file systems.
1882 */
1883 if (rdonly &&
1884 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1885 error = EROFS;
1886 goto bad2;
1887 }
1888 /* ASSERT(dvp == ndp->ni_startdir) */
0a7de745
A
1889
1890 return 0;
1c79356b
A
1891
1892bad2:
91447636 1893 vnode_put(dp);
0a7de745 1894bad:
1c79356b 1895 *vpp = NULL;
91447636 1896
0a7de745 1897 return error;
1c79356b
A
1898}
1899
6d2010ae
A
1900/*
1901 * Free pathname buffer
1902 */
1903void
1904nameidone(struct nameidata *ndp)
1905{
91447636
A
1906 if (ndp->ni_cnd.cn_flags & HASBUF) {
1907 char *tmp = ndp->ni_cnd.cn_pnbuf;
1908
1909 ndp->ni_cnd.cn_pnbuf = NULL;
1910 ndp->ni_cnd.cn_flags &= ~HASBUF;
f427ee49 1911 zfree(ZV_NAMEI, tmp);
91447636
A
1912 }
1913}
1914
1c79356b 1915
2d21ac55 1916/*
f427ee49
A
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.
2d21ac55 1922 *
f427ee49
A
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.
2d21ac55 1927 *
f427ee49
A
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).
2d21ac55 1935 *
f427ee49
A
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.
2d21ac55 1938 */
316670eb 1939#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
39236c6e
A
1940
1941void
f427ee49
A
1942kdebug_vfs_lookup(unsigned long *path_words, int path_len, void *vnp,
1943 uint32_t flags)
1c79356b 1944{
d9a64523 1945 bool noprocfilt = flags & KDBG_VFS_LOOKUP_FLAG_NOPROCFILT;
39236c6e 1946
f427ee49 1947 assert(path_len >= 0);
39236c6e 1948
f427ee49
A
1949 int code = ((flags & KDBG_VFS_LOOKUP_FLAG_LOOKUP) ? VFS_LOOKUP :
1950 VFS_LOOKUP_DONE) | DBG_FUNC_START;
1951
1952 if (path_len <= (3 * (int)sizeof(long))) {
39236c6e 1953 code |= DBG_FUNC_END;
0a7de745 1954 }
39236c6e 1955
d9a64523 1956 if (noprocfilt) {
f427ee49
A
1957 KDBG_RELEASE_NOPROCFILT(code, kdebug_vnode(vnp), path_words[0],
1958 path_words[1], path_words[2]);
d9a64523 1959 } else {
f427ee49
A
1960 KDBG_RELEASE(code, kdebug_vnode(vnp), path_words[0], path_words[1],
1961 path_words[2]);
d9a64523 1962 }
39236c6e
A
1963
1964 code &= ~DBG_FUNC_START;
1965
f427ee49
A
1966 for (int i = 3; i * (int)sizeof(long) < path_len; i += 4) {
1967 if ((i + 4) * (int)sizeof(long) >= path_len) {
39236c6e 1968 code |= DBG_FUNC_END;
0a7de745 1969 }
39236c6e 1970
d9a64523 1971 if (noprocfilt) {
f427ee49
A
1972 KDBG_RELEASE_NOPROCFILT(code, path_words[i], path_words[i + 1],
1973 path_words[i + 2], path_words[i + 3]);
d9a64523 1974 } else {
f427ee49
A
1975 KDBG_RELEASE(code, path_words[i], path_words[i + 1],
1976 path_words[i + 2], path_words[i + 3]);
d9a64523 1977 }
39236c6e
A
1978 }
1979}
1980
d9a64523 1981void
f427ee49 1982kdebug_lookup_gen_events(long *path_words, int path_len, void *vnp, bool lookup)
d9a64523 1983{
f427ee49
A
1984 assert(path_len >= 0);
1985 kdebug_vfs_lookup((unsigned long *)path_words, path_len, vnp,
0a7de745 1986 lookup ? KDBG_VFS_LOOKUP_FLAG_LOOKUP : 0);
d9a64523
A
1987}
1988
1989void
f427ee49 1990kdebug_lookup(vnode_t vnp, struct componentname *cnp)
39236c6e 1991{
f427ee49 1992 unsigned long path_words[NUMPARMS];
1c79356b 1993
f427ee49
A
1994 /*
1995 * Truncate the leading portion of the path to fit in path_words.
1996 */
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;
0a7de745 2002
f427ee49 2003 memcpy(path_words, path_trunc, path_len);
0a7de745 2004
f427ee49
A
2005 /*
2006 * Pad with '\0' or '>'.
2007 */
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);
1c79356b 2012 }
f427ee49 2013 kdebug_vfs_lookup(path_words, (int)path_len, vnp, KDBG_VFS_LOOKUP_FLAG_LOOKUP);
d9a64523 2014}
0c530ab8 2015
39236c6e 2016#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
0c530ab8 2017
39236c6e 2018void
d9a64523 2019kdebug_vfs_lookup(long *dbg_parms __unused, int dbg_namelen __unused,
0a7de745 2020 void *dp __unused, __unused uint32_t flags)
39236c6e 2021{
2d21ac55 2022}
39236c6e 2023
6d2010ae
A
2024static void
2025kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
2026{
2027}
316670eb 2028#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
6d2010ae
A
2029
2030int
2031vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx)
2032{
2033 mount_t mp;
2034 int error;
0a7de745 2035
6d2010ae
A
2036 mp = mount_lookupby_volfsid(fsid->val[0], 1);
2037 if (mp == NULL) {
2038 return EINVAL;
2039 }
2040
2041 /* Get the target vnode. */
2042 if (ino == 2) {
2043 error = VFS_ROOT(mp, vpp, ctx);
2044 } else {
2045 error = VFS_VGET(mp, ino, vpp, ctx);
2046 }
0c530ab8 2047
6d2010ae
A
2048 vfs_unbusy(mp);
2049 return error;
2050}
2d21ac55
A
2051/*
2052 * Obtain the real path from a legacy volfs style path.
2053 *
2054 * Valid formats of input path:
2055 *
2056 * "555/@"
2057 * "555/2"
2058 * "555/123456"
2059 * "555/123456/foobar"
2060 *
2061 * Where:
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
2066 */
2067#if CONFIG_VOLFS
2068static int
2069vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
2070{
2071 vnode_t vp;
2072 struct mount *mp = NULL;
2073 char *str;
2074 char ch;
f427ee49 2075 unsigned long id;
2d21ac55
A
2076 ino64_t ino;
2077 int error;
2078 int length;
2079
2080 /* Get file system id and move str to next component. */
2081 id = strtoul(path, &str, 10);
2082 if (id == 0 || str[0] != '/') {
0a7de745 2083 return EINVAL;
2d21ac55
A
2084 }
2085 while (*str == '/') {
2086 str++;
0c530ab8 2087 }
2d21ac55
A
2088 ch = *str;
2089
f427ee49
A
2090 if (id > INT_MAX) {
2091 return ENOENT;
2092 }
2093 mp = mount_lookupby_volfsid((int)id, 1);
2d21ac55 2094 if (mp == NULL) {
0a7de745 2095 return EINVAL; /* unexpected failure */
2d21ac55
A
2096 }
2097 /* Check for an alias to a file system root. */
2098 if (ch == '@' && str[1] == '\0') {
2099 ino = 2;
2100 str++;
2101 } else {
2102 /* Get file id and move str to next component. */
0a7de745 2103 ino = strtouq(str, &str, 10);
2d21ac55
A
2104 }
2105
2106 /* Get the target vnode. */
2107 if (ino == 2) {
cb323159
A
2108 struct vfs_attr vfsattr;
2109 int use_vfs_root = TRUE;
2110
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;
2118 }
2119 }
2120
2121 if (use_vfs_root) {
2122 error = VFS_ROOT(mp, &vp, ctx);
2123 } else {
2124 error = VFS_VGET(mp, ino, &vp, ctx);
2125 }
2d21ac55
A
2126 } else {
2127 error = VFS_VGET(mp, ino, &vp, ctx);
2128 }
2129 vfs_unbusy(mp);
2130 if (error) {
2131 goto out;
2132 }
2133 realpath[0] = '\0';
2134
2135 /* Get the absolute path to this vnode. */
f427ee49 2136 error = build_path(vp, realpath, (int)bufsize, &length, 0, ctx);
2d21ac55
A
2137 vnode_put(vp);
2138
2139 if (error == 0 && *str != '\0') {
f427ee49 2140 size_t attempt = strlcat(realpath, str, MAXPATHLEN);
0a7de745 2141 if (attempt > MAXPATHLEN) {
2d21ac55
A
2142 error = ENAMETOOLONG;
2143 }
2144 }
2145out:
0a7de745 2146 return error;
1c79356b 2147}
2d21ac55 2148#endif
6d2010ae
A
2149
2150void
2151lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create)
2152{
2153 if (error == 0 && vp == NULLVP) {
2154 panic("NULL vp with error == 0.\n");
2155 }
2156
0a7de745 2157 /*
6d2010ae
A
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.
2161 */
2162 if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) {
2163 return;
2164 }
2165
0a7de745 2166 /*
6d2010ae
A
2167 * If we're going to continue the lookup, we'll handle
2168 * all lookup-related updates at that time.
2169 */
2170 if (error == EKEEPLOOKING) {
2171 return;
2172 }
2173
2174 /*
2175 * Only audit or update cache for *found* vnodes. For creation
2176 * neither would happen in the non-compound-vnop case.
2177 */
2178 if ((vp != NULLVP) && !did_create) {
0a7de745
A
2179 /*
2180 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
6d2010ae
A
2181 * then we certainly don't want to update cache or identity.
2182 */
2183 if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) {
2184 lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration);
2185 }
0a7de745 2186 if (ndp->ni_cnd.cn_flags & AUDITVNPATH1) {
6d2010ae 2187 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
0a7de745 2188 } else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2) {
6d2010ae 2189 AUDIT_ARG(vnpath, vp, ARG_VNODE2);
0a7de745 2190 }
6d2010ae
A
2191 }
2192
0a7de745
A
2193 /*
2194 * If you created (whether you opened or not), cut a lookup tracepoint
6d2010ae
A
2195 * for the parent dir (as would happen without a compound vnop). Note: we may need
2196 * a vnode despite failure in this case!
2197 *
2198 * If you did not create:
0a7de745
A
2199 * Found child (succeeded or not): cut a tracepoint for the child.
2200 * Did not find child: cut a tracepoint with the parent.
6d2010ae
A
2201 */
2202 if (kdebug_enable) {
0a7de745 2203 kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
6d2010ae
A
2204 }
2205}