xref: /xnu-8019.80.24/bsd/vfs/vfs_lookup.c (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
1 /*
2  * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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  */
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  */
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/syslimits.h>
78 #include <sys/time.h>
79 #include <sys/namei.h>
80 #include <sys/vm.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>
93 #if CONFIG_MACF
94 #include <security/mac_framework.h>
95 #endif
96 #include <os/atomic_private.h>
97 
98 #include <sys/paths.h>
99 
100 #if NAMEDRSRCFORK
101 #include <sys/xattr.h>
102 #endif
103 /*
104  * The minimum volfs-style pathname is 9.
105  * Example:  "/.vol/1/2"
106  */
107 #define VOLFS_MIN_PATH_LEN  9
108 
109 
110 #if CONFIG_VOLFS
111 static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
112 #define MAX_VOLFS_RESTARTS 5
113 #endif
114 
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);
123 
124 #if NAMEDRSRCFORK
125 static int              lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx);
126 #endif
127 
128 extern lck_rw_t rootvnode_rw_lock;
129 
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  *	}
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
160  *		lookup:ERECYCLE	 vnode was recycled from underneath us in lookup.
161  *						 This means we should re-drive lookup from this point.
162  *		lookup: ???
163  *		VNOP_READLINK:???
164  */
165 int
namei(struct nameidata * ndp)166 namei(struct nameidata *ndp)
167 {
168 	struct vnode *dp;       /* the directory we are searching */
169 	struct vnode *usedvp = ndp->ni_dvp;  /* store pointer to vp in case we must loop due to
170 	                                      *                                          heavy vnode pressure */
171 	uint32_t cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
172 	int error;
173 	struct componentname *cnp = &ndp->ni_cnd;
174 	vfs_context_t ctx = cnp->cn_context;
175 	proc_t p = vfs_context_proc(ctx);
176 #if CONFIG_AUDIT
177 /* XXX ut should be from context */
178 	uthread_t ut = current_uthread();
179 #endif
180 
181 #if CONFIG_VOLFS
182 	int volfs_restarts = 0;
183 #endif
184 	size_t bytes_copied = 0;
185 	vnode_t rootdir_with_usecount = NULLVP;
186 	vnode_t startdir_with_usecount = NULLVP;
187 	vnode_t usedvp_dp = NULLVP;
188 	int32_t old_count = 0;
189 	bool dp_has_iocount = false;
190 
191 #if DIAGNOSTIC
192 	if (!vfs_context_ucred(ctx) || !p) {
193 		panic("namei: bad cred/proc");
194 	}
195 	if (cnp->cn_nameiop & (~OPMASK)) {
196 		panic("namei: nameiop contaminated with flags");
197 	}
198 	if (cnp->cn_flags & OPMASK) {
199 		panic("namei: flags contaminated with nameiops");
200 	}
201 #endif
202 
203 	/*
204 	 * A compound VNOP found something that needs further processing:
205 	 * either a trigger vnode, a covered directory, or a symlink.
206 	 */
207 	if (ndp->ni_flag & NAMEI_CONTLOOKUP) {
208 		int rdonly, vbusyflags, keep_going, wantparent;
209 
210 		rdonly = cnp->cn_flags & RDONLY;
211 		vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0;
212 		keep_going = 0;
213 		wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
214 
215 		ndp->ni_flag &= ~(NAMEI_CONTLOOKUP);
216 
217 		error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
218 		    &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
219 		if (error) {
220 			goto out_drop;
221 		}
222 		if (keep_going) {
223 			if ((cnp->cn_flags & ISSYMLINK) == 0) {
224 				panic("We need to keep going on a continued lookup, but for vp type %d (tag %d)", ndp->ni_vp->v_type, ndp->ni_vp->v_tag);
225 			}
226 			goto continue_symlink;
227 		}
228 
229 		return 0;
230 	}
231 
232 vnode_recycled:
233 
234 	/*
235 	 * Get a buffer for the name to be translated, and copy the
236 	 * name into the buffer.
237 	 */
238 	if ((cnp->cn_flags & HASBUF) == 0) {
239 		cnp->cn_pnbuf = ndp->ni_pathbuf;
240 		cnp->cn_pnlen = PATHBUFLEN;
241 	}
242 #if LP64_DEBUG
243 	if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
244 	    && (ndp->ni_segflg != UIO_SYSSPACE)
245 	    && (ndp->ni_segflg != UIO_SYSSPACE32)) {
246 		panic("invalid ni_segflg");
247 	}
248 #endif /* LP64_DEBUG */
249 
250 retry_copy:
251 	if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
252 		error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
253 		    cnp->cn_pnlen, &bytes_copied);
254 	} else {
255 		error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
256 		    cnp->cn_pnlen, &bytes_copied);
257 	}
258 	if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
259 		cnp->cn_pnbuf = zalloc(ZV_NAMEI);
260 		cnp->cn_flags |= HASBUF;
261 		cnp->cn_pnlen = MAXPATHLEN;
262 		bytes_copied = 0;
263 
264 		goto retry_copy;
265 	}
266 	if (error) {
267 		goto error_out;
268 	}
269 	assert(bytes_copied <= MAXPATHLEN);
270 	ndp->ni_pathlen = (u_int)bytes_copied;
271 	bytes_copied = 0;
272 
273 	/*
274 	 * Since the name cache may contain positive entries of
275 	 * the incorrect case, force lookup() to bypass the cache
276 	 * and call directly into the filesystem for each path
277 	 * component. Note: the FS may still consult the cache,
278 	 * but can apply rules to validate the results.
279 	 */
280 	if (proc_is_forcing_hfs_case_sensitivity(p)) {
281 		cnp->cn_flags |= CN_SKIPNAMECACHE;
282 	}
283 
284 #if CONFIG_VOLFS
285 	/*
286 	 * Check for legacy volfs style pathnames.
287 	 *
288 	 * For compatibility reasons we currently allow these paths,
289 	 * but future versions of the OS may not support them.
290 	 */
291 	if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN &&
292 	    cnp->cn_pnbuf[0] == '/' &&
293 	    cnp->cn_pnbuf[1] == '.' &&
294 	    cnp->cn_pnbuf[2] == 'v' &&
295 	    cnp->cn_pnbuf[3] == 'o' &&
296 	    cnp->cn_pnbuf[4] == 'l' &&
297 	    cnp->cn_pnbuf[5] == '/') {
298 		char * realpath;
299 		int realpath_err;
300 		/* Attempt to resolve a legacy volfs style pathname. */
301 		realpath = zalloc(ZV_NAMEI);
302 		/*
303 		 * We only error out on the ENAMETOOLONG cases where we know that
304 		 * vfs_getrealpath translation succeeded but the path could not fit into
305 		 * MAXPATHLEN characters.  In other failure cases, we may be dealing with a path
306 		 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
307 		 */
308 		if ((realpath_err = vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) {
309 			zfree(ZV_NAMEI, realpath);
310 			if (realpath_err == ENOSPC || realpath_err == ENAMETOOLONG) {
311 				error = ENAMETOOLONG;
312 				goto error_out;
313 			}
314 		} else {
315 			size_t tmp_len;
316 			if (cnp->cn_flags & HASBUF) {
317 				zfree(ZV_NAMEI, cnp->cn_pnbuf);
318 			}
319 			cnp->cn_pnbuf = realpath;
320 			cnp->cn_pnlen = MAXPATHLEN;
321 			tmp_len = strlen(realpath) + 1;
322 			assert(tmp_len <= UINT_MAX);
323 			ndp->ni_pathlen = (u_int)tmp_len;
324 			cnp->cn_flags |= HASBUF | CN_VOLFSPATH;
325 		}
326 	}
327 #endif /* CONFIG_VOLFS */
328 
329 #if CONFIG_AUDIT
330 	/* If we are auditing the kernel pathname, save the user pathname */
331 	if (cnp->cn_flags & AUDITVNPATH1) {
332 		AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
333 	}
334 	if (cnp->cn_flags & AUDITVNPATH2) {
335 		AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
336 	}
337 #endif /* CONFIG_AUDIT */
338 
339 	/*
340 	 * Do not allow empty pathnames
341 	 */
342 	if (*cnp->cn_pnbuf == '\0') {
343 		error = ENOENT;
344 		goto error_out;
345 	}
346 	if (ndp->ni_flag & NAMEI_NOFOLLOW_ANY) {
347 		ndp->ni_loopcnt = MAXSYMLINKS;
348 	} else {
349 		ndp->ni_loopcnt = 0;
350 	}
351 
352 	/*
353 	 * determine the starting point for the translation.
354 	 */
355 	proc_dirs_lock_shared(p);
356 	lck_rw_lock_shared(&rootvnode_rw_lock);
357 
358 	if (fdt_flag_test(&p->p_fd, FD_CHROOT)) {
359 		ndp->ni_rootdir = p->p_fd.fd_rdir;
360 	} else {
361 		ndp->ni_rootdir = rootvnode;
362 	}
363 
364 	if (!ndp->ni_rootdir) {
365 		if (fdt_flag_test(&p->p_fd, FD_CHROOT)) {
366 			/* This should be a panic */
367 			printf("p->p_fd.fd_rdir is not set\n");
368 		} else {
369 			printf("rootvnode is not set\n");
370 		}
371 		lck_rw_unlock_shared(&rootvnode_rw_lock);
372 		proc_dirs_unlock_shared(p);
373 		error = ENOENT;
374 		goto error_out;
375 	}
376 
377 	cnp->cn_nameptr = cnp->cn_pnbuf;
378 
379 	ndp->ni_usedvp = NULLVP;
380 
381 	if (*(cnp->cn_nameptr) == '/') {
382 		while (*(cnp->cn_nameptr) == '/') {
383 			cnp->cn_nameptr++;
384 			ndp->ni_pathlen--;
385 		}
386 		dp = ndp->ni_rootdir;
387 	} else if (cnp->cn_flags & USEDVP) {
388 		dp = ndp->ni_dvp;
389 		ndp->ni_usedvp = dp;
390 		usedvp_dp = dp;
391 	} else {
392 		dp = vfs_context_cwd(ctx);
393 	}
394 
395 	if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
396 		dp = NULLVP;
397 		lck_rw_unlock_shared(&rootvnode_rw_lock);
398 		proc_dirs_unlock_shared(p);
399 		error = ENOENT;
400 		goto error_out;
401 	}
402 
403 	/*
404 	 * We need our own usecount on the root vnode and the starting dir across
405 	 * the lookup. There's two things that be done here. We can hold the locks
406 	 * (which protect the existing usecounts on the directories) across the
407 	 * lookup or take our own usecount. Holding the locks across the lookup can
408 	 * cause deadlock issues if we re-enter namei on the same thread so the
409 	 * correct thing to do is to acquire our own usecount.
410 	 *
411 	 * Ideally, the usecount should be obtained by vnode_get->vnode_ref->vnode_put.
412 	 * However when this vnode is the rootvnode, that sequence will produce a
413 	 * lot of vnode mutex locks and  unlocks on a single vnode (the rootvnode)
414 	 * and will be highly contended and degrade performance. Since we have
415 	 * an existing usecount protected by the locks we hold, we'll just use
416 	 * an atomic op to increment the usecount on a vnode which already has one
417 	 * and can't be released because we have the locks which protect against that
418 	 * happening.
419 	 */
420 	rootdir_with_usecount = ndp->ni_rootdir;
421 	old_count = os_atomic_inc_orig(&rootdir_with_usecount->v_usecount, relaxed);
422 	if (old_count < 1) {
423 		panic("(1) invalid pre-increment usecount (%d) for rootdir vnode %p",
424 		    old_count, rootdir_with_usecount);
425 	} else if (old_count == INT32_MAX) {
426 		panic("(1) usecount overflow for vnode %p", rootdir_with_usecount);
427 	}
428 
429 	if ((dp != rootdir_with_usecount) && (dp != usedvp_dp)) {
430 		old_count = os_atomic_inc_orig(&dp->v_usecount, relaxed);
431 		if (old_count < 1) {
432 			panic("(2) invalid pre-increment usecount (%d) for vnode %p", old_count, dp);
433 		} else if (old_count == INT32_MAX) {
434 			panic("(2) usecount overflow for vnode %p", dp);
435 		}
436 		startdir_with_usecount = dp;
437 	}
438 
439 	/* Now that we have our usecount, release the locks */
440 	lck_rw_unlock_shared(&rootvnode_rw_lock);
441 	proc_dirs_unlock_shared(p);
442 
443 	ndp->ni_dvp = NULLVP;
444 	ndp->ni_vp  = NULLVP;
445 
446 	for (;;) {
447 #if CONFIG_MACF
448 		/*
449 		 * Give MACF policies a chance to reject the lookup
450 		 * before performing any filesystem operations.
451 		 * This hook is called before resolving the path and
452 		 * again each time a symlink is encountered.
453 		 * NB: policies receive path information as supplied
454 		 *     by the caller and thus cannot be trusted.
455 		 */
456 		error = mac_vnode_check_lookup_preflight(ctx, dp, cnp->cn_nameptr, cnp->cn_namelen);
457 		if (error) {
458 			goto error_out;
459 		}
460 #endif
461 		ndp->ni_startdir = dp;
462 		dp = NULLVP;
463 
464 		if ((error = lookup(ndp))) {
465 			goto error_out;
466 		}
467 
468 		/*
469 		 * Check for symbolic link
470 		 */
471 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
472 			if (startdir_with_usecount) {
473 				vnode_rele(startdir_with_usecount);
474 				startdir_with_usecount = NULLVP;
475 			}
476 			if (rootdir_with_usecount) {
477 				lck_rw_lock_shared(&rootvnode_rw_lock);
478 				if (rootdir_with_usecount == rootvnode) {
479 					old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
480 					if (old_count < 2) {
481 						/*
482 						 * There needs to have been at least 1 usecount left on the rootvnode
483 						 */
484 						panic("(3) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
485 						    old_count, rootdir_with_usecount);
486 					}
487 					rootdir_with_usecount = NULLVP;
488 				}
489 				lck_rw_unlock_shared(&rootvnode_rw_lock);
490 				if (rootdir_with_usecount) {
491 					vnode_rele(rootdir_with_usecount);
492 					rootdir_with_usecount = NULLVP;
493 				}
494 			}
495 
496 			return 0;
497 		}
498 
499 continue_symlink:
500 		/* Gives us a new path to process, and a starting dir */
501 		error = lookup_handle_symlink(ndp, &dp, &dp_has_iocount, ctx);
502 		if (error != 0) {
503 			break;
504 		}
505 		if (dp_has_iocount) {
506 			if ((dp != rootdir_with_usecount) && (dp != startdir_with_usecount) &&
507 			    (dp != usedvp_dp)) {
508 				if (startdir_with_usecount) {
509 					vnode_rele(startdir_with_usecount);
510 				}
511 				vnode_ref_ext(dp, 0, VNODE_REF_FORCE);
512 				startdir_with_usecount = dp;
513 			}
514 			vnode_put(dp);
515 			dp_has_iocount = false;
516 		}
517 	}
518 	/*
519 	 * only come here if we fail to handle a SYMLINK...
520 	 * if either ni_dvp or ni_vp is non-NULL, then
521 	 * we need to drop the iocount that was picked
522 	 * up in the lookup routine
523 	 */
524 out_drop:
525 	if (ndp->ni_dvp) {
526 		vnode_put(ndp->ni_dvp);
527 	}
528 	if (ndp->ni_vp) {
529 		vnode_put(ndp->ni_vp);
530 	}
531 error_out:
532 	if (startdir_with_usecount) {
533 		vnode_rele(startdir_with_usecount);
534 		startdir_with_usecount = NULLVP;
535 	}
536 	if (rootdir_with_usecount) {
537 		lck_rw_lock_shared(&rootvnode_rw_lock);
538 		if (rootdir_with_usecount == rootvnode) {
539 			old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
540 			if (old_count < 2) {
541 				/*
542 				 * There needs to have been at least 1 usecount left on the rootvnode
543 				 */
544 				panic("(4) Unexpected pre-decrement value (%d) of usecount for rootvnode %p",
545 				    old_count, rootdir_with_usecount);
546 			}
547 			lck_rw_unlock_shared(&rootvnode_rw_lock);
548 		} else {
549 			lck_rw_unlock_shared(&rootvnode_rw_lock);
550 			vnode_rele(rootdir_with_usecount);
551 		}
552 		rootdir_with_usecount = NULLVP;
553 	}
554 
555 	if ((cnp->cn_flags & HASBUF)) {
556 		cnp->cn_flags &= ~HASBUF;
557 		zfree(ZV_NAMEI, cnp->cn_pnbuf);
558 	}
559 	cnp->cn_pnbuf = NULL;
560 	ndp->ni_vp = NULLVP;
561 	ndp->ni_dvp = NULLVP;
562 
563 #if CONFIG_VOLFS
564 	/*
565 	 * Deal with volfs fallout.
566 	 *
567 	 * At this point, if we were originally given a volfs path that
568 	 * looks like /.vol/123/456, then we would have had to convert it into
569 	 * a full path.  Assuming that part worked properly, we will now attempt
570 	 * to conduct a lookup of the item in the namespace.  Under normal
571 	 * circumstances, if a user looked up /tmp/foo and it was not there, it
572 	 * would be permissible to return ENOENT.
573 	 *
574 	 * However, we may not want to do that here.  Specifically, the volfs path
575 	 * uniquely identifies a certain item in the namespace regardless of where it
576 	 * lives.  If the item has moved in between the time we constructed the
577 	 * path and now, when we're trying to do a lookup/authorization on the full
578 	 * path, we may have gotten an ENOENT.
579 	 *
580 	 * At this point we can no longer tell if the path no longer exists
581 	 * or if the item in question no longer exists. It could have been renamed
582 	 * away, in which case the /.vol identifier is still valid.
583 	 *
584 	 * Do this dance a maximum of MAX_VOLFS_RESTARTS times.
585 	 */
586 	if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) {
587 		if (volfs_restarts < MAX_VOLFS_RESTARTS) {
588 			volfs_restarts++;
589 			goto vnode_recycled;
590 		}
591 	}
592 #endif
593 
594 	if (error == ERECYCLE) {
595 		/* vnode was recycled underneath us. re-drive lookup to start at
596 		 *  the beginning again, since recycling invalidated last lookup*/
597 		ndp->ni_cnd.cn_flags = cnpflags;
598 		ndp->ni_dvp = usedvp;
599 		goto vnode_recycled;
600 	}
601 
602 
603 	return error;
604 }
605 
606 int
namei_compound_available(vnode_t dp,struct nameidata * ndp)607 namei_compound_available(vnode_t dp, struct nameidata *ndp)
608 {
609 	if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) {
610 		return vnode_compound_open_available(dp);
611 	}
612 
613 	return 0;
614 }
615 
616 static int
lookup_authorize_search(vnode_t dp,struct componentname * cnp,int dp_authorized_in_cache,vfs_context_t ctx)617 lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
618 {
619 #if !CONFIG_MACF
620 #pragma unused(cnp)
621 #endif
622 
623 	int error;
624 
625 	if (!dp_authorized_in_cache) {
626 		error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
627 		if (error) {
628 			return error;
629 		}
630 	}
631 #if CONFIG_MACF
632 	error = mac_vnode_check_lookup(ctx, dp, cnp);
633 	if (error) {
634 		return error;
635 	}
636 #endif /* CONFIG_MACF */
637 
638 	return 0;
639 }
640 
641 static void
lookup_consider_update_cache(vnode_t dvp,vnode_t vp,struct componentname * cnp,int nc_generation)642 lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
643 {
644 	int isdot_or_dotdot;
645 	isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
646 
647 	if (vp->v_name == NULL || vp->v_parent == NULLVP) {
648 		int  update_flags = 0;
649 
650 		if (isdot_or_dotdot == 0) {
651 			if (vp->v_name == NULL) {
652 				update_flags |= VNODE_UPDATE_NAME;
653 			}
654 			if (dvp != NULLVP && vp->v_parent == NULLVP) {
655 				update_flags |= VNODE_UPDATE_PARENT;
656 			}
657 
658 			if (update_flags) {
659 				vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
660 			}
661 		}
662 	}
663 	if ((cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
664 		/*
665 		 * missing from name cache, but should
666 		 * be in it... this can happen if volfs
667 		 * causes the vnode to be created or the
668 		 * name cache entry got recycled but the
669 		 * vnode didn't...
670 		 * check to make sure that ni_dvp is valid
671 		 * cache_lookup_path may return a NULL
672 		 * do a quick check to see if the generation of the
673 		 * directory matches our snapshot... this will get
674 		 * rechecked behind the name cache lock, but if it
675 		 * already fails to match, no need to go any further
676 		 */
677 		if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot)) {
678 			cache_enter_with_gen(dvp, vp, cnp, nc_generation);
679 		}
680 	}
681 }
682 
683 #if NAMEDRSRCFORK
684 /*
685  * Can change ni_dvp and ni_vp.  On success, returns with iocounts on stream vnode (always) and
686  * data fork if requested.  On failure, returns with iocount data fork (always) and its parent directory
687  * (if one was provided).
688  */
689 static int
lookup_handle_rsrc_fork(vnode_t dp,struct nameidata * ndp,struct componentname * cnp,int wantparent,vfs_context_t ctx)690 lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx)
691 {
692 	vnode_t svp = NULLVP;
693 	enum nsoperation nsop;
694 	int nsflags;
695 	int error;
696 
697 	if (dp->v_type != VREG) {
698 		error = ENOENT;
699 		goto out;
700 	}
701 	switch (cnp->cn_nameiop) {
702 	case DELETE:
703 		if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
704 			nsop = NS_DELETE;
705 		} else {
706 			error = EPERM;
707 			goto out;
708 		}
709 		break;
710 	case CREATE:
711 		if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
712 			nsop = NS_CREATE;
713 		} else {
714 			error = EPERM;
715 			goto out;
716 		}
717 		break;
718 	case LOOKUP:
719 		/* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
720 		if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
721 			nsop = NS_OPEN;
722 		} else {
723 			error = EPERM;
724 			goto out;
725 		}
726 		break;
727 	default:
728 		error = EPERM;
729 		goto out;
730 	}
731 
732 	nsflags = 0;
733 	if (cnp->cn_flags & CN_RAW_ENCRYPTED) {
734 		nsflags |= NS_GETRAWENCRYPTED;
735 	}
736 
737 	/* Ask the file system for the resource fork. */
738 	error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, nsflags, ctx);
739 
740 	/* During a create, it OK for stream vnode to be missing. */
741 	if (error == ENOATTR || error == ENOENT) {
742 		error = (nsop == NS_CREATE) ? 0 : ENOENT;
743 	}
744 	if (error) {
745 		goto out;
746 	}
747 	/* The "parent" of the stream is the file. */
748 	if (wantparent) {
749 		if (ndp->ni_dvp) {
750 			vnode_put(ndp->ni_dvp);
751 		}
752 		ndp->ni_dvp = dp;
753 	} else {
754 		vnode_put(dp);
755 	}
756 	ndp->ni_vp = svp;  /* on create this may be null */
757 
758 	/* Restore the truncated pathname buffer (for audits). */
759 	if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
760 		/*
761 		 * While we replaced only '/' with '\0' and would ordinarily
762 		 * need to just switch that back, the buffer in which we did
763 		 * this may not be what the pathname buffer is now when symlinks
764 		 * are involved. If we just restore the "/" we will make the
765 		 * string not terminated anymore, so be safe and restore the
766 		 * entire suffix.
767 		 */
768 		strncpy(ndp->ni_next, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC));
769 		cnp->cn_nameptr = ndp->ni_next + 1;
770 		cnp->cn_namelen = sizeof(_PATH_RSRCFORKSPEC) - 1;
771 		ndp->ni_next += cnp->cn_namelen;
772 		if (ndp->ni_next[0] != '\0') {
773 			panic("Incorrect termination of path in %s", __FUNCTION__);
774 		}
775 	}
776 	cnp->cn_flags  &= ~MAKEENTRY;
777 
778 	return 0;
779 out:
780 	return error;
781 }
782 #endif /* NAMEDRSRCFORK */
783 
784 /*
785  * iocounts in:
786  *      --One on ni_vp.  One on ni_dvp if there is more path, or we didn't come through the
787  *      cache, or we came through the cache and the caller doesn't want the parent.
788  *
789  * iocounts out:
790  *	--Leaves us in the correct state for the next step, whatever that might be.
791  *	--If we find a symlink, returns with iocounts on both ni_vp and ni_dvp.
792  *	--If we are to look up another component, then we have an iocount on ni_vp and
793  *	nothing else.
794  *	--If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags.
795  *	--In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount
796  *	was dropped).
797  */
798 static int
lookup_handle_found_vnode(struct nameidata * ndp,struct componentname * cnp,int rdonly,int vbusyflags,int * keep_going,int nc_generation,int wantparent,int atroot,vfs_context_t ctx)799 lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
800     int vbusyflags, int *keep_going, int nc_generation,
801     int wantparent, int atroot, vfs_context_t ctx)
802 {
803 	vnode_t dp;
804 	int error;
805 	char *cp;
806 
807 	dp = ndp->ni_vp;
808 	*keep_going = 0;
809 
810 	if (ndp->ni_vp == NULLVP) {
811 		panic("NULL ni_vp in %s", __FUNCTION__);
812 	}
813 
814 	if (atroot) {
815 		goto nextname;
816 	}
817 
818 	/*
819 	 * Take into account any additional components consumed by
820 	 * the underlying filesystem.
821 	 */
822 	if (cnp->cn_consume > 0) {
823 		cnp->cn_nameptr += cnp->cn_consume;
824 		ndp->ni_next += cnp->cn_consume;
825 		ndp->ni_pathlen -= cnp->cn_consume;
826 		cnp->cn_consume = 0;
827 	} else {
828 		lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation);
829 	}
830 
831 	/*
832 	 * Check to see if the vnode has been mounted on...
833 	 * if so find the root of the mounted file system.
834 	 * Updates ndp->ni_vp.
835 	 */
836 	error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx);
837 	dp = ndp->ni_vp;
838 	if (error) {
839 		goto out;
840 	}
841 
842 #if CONFIG_MACF
843 	if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
844 		error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx);
845 		if (error) {
846 			goto out;
847 		}
848 	}
849 #endif
850 
851 	/*
852 	 * Check for symbolic link
853 	 */
854 	if ((dp->v_type == VLNK) &&
855 	    ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
856 		cnp->cn_flags |= ISSYMLINK;
857 		*keep_going = 1;
858 		return 0;
859 	}
860 
861 	/*
862 	 * Check for bogus trailing slashes.
863 	 */
864 	if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
865 		if (dp->v_type != VDIR) {
866 			error = ENOTDIR;
867 			goto out;
868 		}
869 		ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
870 	}
871 
872 #if NAMEDSTREAMS
873 	/*
874 	 * Deny namei/lookup requests to resolve paths that point to shadow files.
875 	 * Access to shadow files must be conducted by explicit calls to VNOP_LOOKUP
876 	 * directly, and not use lookup/namei
877 	 */
878 	if (vnode_isshadow(dp)) {
879 		error = ENOENT;
880 		goto out;
881 	}
882 #endif
883 
884 nextname:
885 	/*
886 	 * Not a symbolic link.  If more pathname,
887 	 * continue at next component, else return.
888 	 *
889 	 * Definitely have a dvp if there's another slash
890 	 */
891 	if (*ndp->ni_next == '/') {
892 		cnp->cn_nameptr = ndp->ni_next + 1;
893 		ndp->ni_pathlen--;
894 		while (*cnp->cn_nameptr == '/') {
895 			cnp->cn_nameptr++;
896 			ndp->ni_pathlen--;
897 		}
898 
899 		cp = cnp->cn_nameptr;
900 		vnode_put(ndp->ni_dvp);
901 		ndp->ni_dvp = NULLVP;
902 
903 		if (*cp == '\0') {
904 			goto emptyname;
905 		}
906 
907 		*keep_going = 1;
908 		return 0;
909 	}
910 
911 	/*
912 	 * Disallow directory write attempts on read-only file systems.
913 	 */
914 	if (rdonly &&
915 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
916 		error = EROFS;
917 		goto out;
918 	}
919 
920 	/* If SAVESTART is set, we should have a dvp */
921 	if (cnp->cn_flags & SAVESTART) {
922 		/*
923 		 * note that we already hold a reference
924 		 * on both dp and ni_dvp, but for some reason
925 		 * can't get another one... in this case we
926 		 * need to do vnode_put on dp in 'bad2'
927 		 */
928 		if ((vnode_get(ndp->ni_dvp))) {
929 			error = ENOENT;
930 			goto out;
931 		}
932 		ndp->ni_startdir = ndp->ni_dvp;
933 	}
934 	if (!wantparent && ndp->ni_dvp) {
935 		vnode_put(ndp->ni_dvp);
936 		ndp->ni_dvp = NULLVP;
937 	}
938 
939 	if (cnp->cn_flags & AUDITVNPATH1) {
940 		AUDIT_ARG(vnpath, dp, ARG_VNODE1);
941 	} else if (cnp->cn_flags & AUDITVNPATH2) {
942 		AUDIT_ARG(vnpath, dp, ARG_VNODE2);
943 	}
944 
945 #if NAMEDRSRCFORK
946 	/*
947 	 * Caller wants the resource fork.
948 	 */
949 	if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
950 		error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx);
951 		if (error != 0) {
952 			goto out;
953 		}
954 
955 		dp = ndp->ni_vp;
956 	}
957 #endif
958 	if (kdebug_enable) {
959 		kdebug_lookup(ndp->ni_vp, cnp);
960 	}
961 
962 	return 0;
963 
964 emptyname:
965 	error = lookup_handle_emptyname(ndp, cnp, wantparent);
966 	if (error != 0) {
967 		goto out;
968 	}
969 
970 	return 0;
971 out:
972 	return error;
973 }
974 
975 /*
976  * Comes in iocount on ni_vp.  May overwrite ni_dvp, but doesn't interpret incoming value.
977  */
978 static int
lookup_handle_emptyname(struct nameidata * ndp,struct componentname * cnp,int wantparent)979 lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent)
980 {
981 	vnode_t dp;
982 	int error = 0;
983 
984 	dp = ndp->ni_vp;
985 	cnp->cn_namelen = 0;
986 	/*
987 	 * A degenerate name (e.g. / or "") which is a way of
988 	 * talking about a directory, e.g. like "/." or ".".
989 	 */
990 	if (dp->v_type != VDIR) {
991 		error = ENOTDIR;
992 		goto out;
993 	}
994 	if (cnp->cn_nameiop != LOOKUP) {
995 		error = EISDIR;
996 		goto out;
997 	}
998 	if (wantparent) {
999 		/*
1000 		 * note that we already hold a reference
1001 		 * on dp, but for some reason can't
1002 		 * get another one... in this case we
1003 		 * need to do vnode_put on dp in 'bad'
1004 		 */
1005 		if ((vnode_get(dp))) {
1006 			error = ENOENT;
1007 			goto out;
1008 		}
1009 		ndp->ni_dvp = dp;
1010 	}
1011 	cnp->cn_flags &= ~ISDOTDOT;
1012 	cnp->cn_flags |= ISLASTCN;
1013 	ndp->ni_next = cnp->cn_nameptr;
1014 	ndp->ni_vp = dp;
1015 
1016 	if (cnp->cn_flags & AUDITVNPATH1) {
1017 		AUDIT_ARG(vnpath, dp, ARG_VNODE1);
1018 	} else if (cnp->cn_flags & AUDITVNPATH2) {
1019 		AUDIT_ARG(vnpath, dp, ARG_VNODE2);
1020 	}
1021 	if (cnp->cn_flags & SAVESTART) {
1022 		panic("lookup: SAVESTART");
1023 	}
1024 
1025 	return 0;
1026 out:
1027 	return error;
1028 }
1029 /*
1030  * Search a pathname.
1031  * This is a very central and rather complicated routine.
1032  *
1033  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
1034  * The starting directory is taken from ni_startdir. The pathname is
1035  * descended until done, or a symbolic link is encountered. The variable
1036  * ni_more is clear if the path is completed; it is set to one if a
1037  * symbolic link needing interpretation is encountered.
1038  *
1039  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
1040  * whether the name is to be looked up, created, renamed, or deleted.
1041  * When CREATE, RENAME, or DELETE is specified, information usable in
1042  * creating, renaming, or deleting a directory entry may be calculated.
1043  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
1044  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
1045  * returned unlocked. Otherwise the parent directory is not returned. If
1046  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
1047  * the target is returned locked, otherwise it is returned unlocked.
1048  * When creating or renaming and LOCKPARENT is specified, the target may not
1049  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
1050  *
1051  * Overall outline of lookup:
1052  *
1053  * dirloop:
1054  *	identify next component of name at ndp->ni_ptr
1055  *	handle degenerate case where name is null string
1056  *	if .. and crossing mount points and on mounted filesys, find parent
1057  *	call VNOP_LOOKUP routine for next component name
1058  *	    directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
1059  *	    component vnode returned in ni_vp (if it exists), locked.
1060  *	if result vnode is mounted on and crossing mount points,
1061  *	    find mounted on vnode
1062  *	if more components of name, do next level at dirloop
1063  *	return the answer in ni_vp, locked if LOCKLEAF set
1064  *	    if LOCKPARENT set, return locked parent in ni_dvp
1065  *	    if WANTPARENT set, return unlocked parent in ni_dvp
1066  *
1067  * Returns:	0			Success
1068  *		ENOENT			No such file or directory
1069  *		EBADF			Bad file descriptor
1070  *		ENOTDIR			Not a directory
1071  *		EROFS			Read-only file system [CREATE]
1072  *		EISDIR			Is a directory [CREATE]
1073  *		cache_lookup_path:ERECYCLE  (vnode was recycled from underneath us, redrive lookup again)
1074  *		vnode_authorize:EROFS
1075  *		vnode_authorize:EACCES
1076  *		vnode_authorize:EPERM
1077  *		vnode_authorize:???
1078  *		VNOP_LOOKUP:ENOENT	No such file or directory
1079  *		VNOP_LOOKUP:EJUSTRETURN	Restart system call (INTERNAL)
1080  *		VNOP_LOOKUP:???
1081  *		VFS_ROOT:ENOTSUP
1082  *		VFS_ROOT:ENOENT
1083  *		VFS_ROOT:???
1084  */
1085 int
lookup(struct nameidata * ndp)1086 lookup(struct nameidata *ndp)
1087 {
1088 	char    *cp;            /* pointer into pathname argument */
1089 	vnode_t         tdp;            /* saved dp */
1090 	vnode_t         dp;             /* the directory we are searching */
1091 	int docache = 1;                /* == 0 do not cache last component */
1092 	int wantparent;                 /* 1 => wantparent or lockparent flag */
1093 	int rdonly;                     /* lookup read-only flag bit */
1094 	int dp_authorized = 0;
1095 	int error = 0;
1096 	struct componentname *cnp = &ndp->ni_cnd;
1097 	vfs_context_t ctx = cnp->cn_context;
1098 	int vbusyflags = 0;
1099 	int nc_generation = 0;
1100 	vnode_t last_dp = NULLVP;
1101 	int keep_going;
1102 	int atroot;
1103 
1104 	/*
1105 	 * Setup: break out flag bits into variables.
1106 	 */
1107 	if (cnp->cn_flags & NOCACHE) {
1108 		docache = 0;
1109 	}
1110 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1111 	rdonly = cnp->cn_flags & RDONLY;
1112 	cnp->cn_flags &= ~ISSYMLINK;
1113 	cnp->cn_consume = 0;
1114 
1115 	dp = ndp->ni_startdir;
1116 	ndp->ni_startdir = NULLVP;
1117 
1118 	if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) {
1119 		vbusyflags = LK_NOWAIT;
1120 	}
1121 	cp = cnp->cn_nameptr;
1122 
1123 	if (*cp == '\0') {
1124 		if ((vnode_getwithref(dp))) {
1125 			dp = NULLVP;
1126 			error = ENOENT;
1127 			goto bad;
1128 		}
1129 		ndp->ni_vp = dp;
1130 		error = lookup_handle_emptyname(ndp, cnp, wantparent);
1131 		if (error) {
1132 			goto bad;
1133 		}
1134 
1135 		return 0;
1136 	}
1137 dirloop:
1138 	atroot = 0;
1139 	ndp->ni_vp = NULLVP;
1140 
1141 	if ((error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp))) {
1142 		dp = NULLVP;
1143 		goto bad;
1144 	}
1145 	if ((cnp->cn_flags & ISLASTCN)) {
1146 		if (docache) {
1147 			cnp->cn_flags |= MAKEENTRY;
1148 		}
1149 	} else {
1150 		cnp->cn_flags |= MAKEENTRY;
1151 	}
1152 
1153 	dp = ndp->ni_dvp;
1154 
1155 	if (ndp->ni_vp != NULLVP) {
1156 		/*
1157 		 * cache_lookup_path returned a non-NULL ni_vp then,
1158 		 * we're guaranteed that the dp is a VDIR, it's
1159 		 * been authorized, and vp is not ".."
1160 		 *
1161 		 * make sure we don't try to enter the name back into
1162 		 * the cache if this vp is purged before we get to that
1163 		 * check since we won't have serialized behind whatever
1164 		 * activity is occurring in the FS that caused the purge
1165 		 */
1166 		if (dp != NULLVP) {
1167 			nc_generation = dp->v_nc_generation - 1;
1168 		}
1169 
1170 		goto returned_from_lookup_path;
1171 	}
1172 
1173 	/*
1174 	 * Handle "..": two special cases.
1175 	 * 1. If at root directory (e.g. after chroot)
1176 	 *    or at absolute root directory
1177 	 *    then ignore it so can't get out.
1178 	 * 2. If this vnode is the root of a mounted
1179 	 *    filesystem, then replace it with the
1180 	 *    vnode which was mounted on so we take the
1181 	 *    .. in the other file system.
1182 	 */
1183 	if ((cnp->cn_flags & ISDOTDOT)) {
1184 		/*
1185 		 * if this is a chroot'ed process, check if the current
1186 		 * directory is still a subdirectory of the process's
1187 		 * root directory.
1188 		 */
1189 		if (ndp->ni_rootdir && (ndp->ni_rootdir != rootvnode) &&
1190 		    dp != ndp->ni_rootdir) {
1191 			int sdir_error;
1192 			int is_subdir = FALSE;
1193 
1194 			sdir_error = vnode_issubdir(dp, ndp->ni_rootdir,
1195 			    &is_subdir, vfs_context_kernel());
1196 
1197 			/*
1198 			 * If we couldn't determine if dp is a subdirectory of
1199 			 * ndp->ni_rootdir (sdir_error != 0), we let the request
1200 			 * proceed.
1201 			 */
1202 			if (!sdir_error && !is_subdir) {
1203 				vnode_put(dp);
1204 				dp = ndp->ni_rootdir;
1205 				/*
1206 				 * There's a ref on the process's root directory
1207 				 * but we can't use vnode_getwithref here as
1208 				 * there is nothing preventing that ref being
1209 				 * released by another thread.
1210 				 */
1211 				if (vnode_get(dp)) {
1212 					error = ENOENT;
1213 					goto bad;
1214 				}
1215 			}
1216 		}
1217 
1218 		for (;;) {
1219 			if (dp == ndp->ni_rootdir || dp == rootvnode) {
1220 				ndp->ni_dvp = dp;
1221 				ndp->ni_vp = dp;
1222 				/*
1223 				 * we're pinned at the root
1224 				 * we've already got one reference on 'dp'
1225 				 * courtesy of cache_lookup_path... take
1226 				 * another one for the ".."
1227 				 * if we fail to get the new reference, we'll
1228 				 * drop our original down in 'bad'
1229 				 */
1230 				if ((vnode_get(dp))) {
1231 					error = ENOENT;
1232 					goto bad;
1233 				}
1234 				atroot = 1;
1235 				goto returned_from_lookup_path;
1236 			}
1237 			if ((dp->v_flag & VROOT) == 0 ||
1238 			    (cnp->cn_flags & NOCROSSMOUNT)) {
1239 				break;
1240 			}
1241 			if (dp->v_mount == NULL) {      /* forced umount */
1242 				error = EBADF;
1243 				goto bad;
1244 			}
1245 			tdp = dp;
1246 			dp = tdp->v_mount->mnt_vnodecovered;
1247 
1248 			if ((vnode_getwithref(dp))) {
1249 				vnode_put(tdp);
1250 				dp = NULLVP;
1251 				error = ENOENT;
1252 				goto bad;
1253 			}
1254 
1255 			vnode_put(tdp);
1256 
1257 			ndp->ni_dvp = dp;
1258 			dp_authorized = 0;
1259 		}
1260 	}
1261 
1262 	/*
1263 	 * We now have a segment name to search for, and a directory to search.
1264 	 */
1265 #if CONFIG_UNION_MOUNTS
1266 unionlookup:
1267 #endif /* CONFIG_UNION_MOUNTS */
1268 	ndp->ni_vp = NULLVP;
1269 
1270 	if (dp->v_type != VDIR) {
1271 		error = ENOTDIR;
1272 		goto lookup_error;
1273 	}
1274 	if ((cnp->cn_flags & DONOTAUTH) != DONOTAUTH) {
1275 		error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
1276 		if (error) {
1277 			goto lookup_error;
1278 		}
1279 	}
1280 
1281 	/*
1282 	 * Now that we've authorized a lookup, can bail out if the filesystem
1283 	 * will be doing a batched operation.  Return an iocount on dvp.
1284 	 */
1285 #if NAMEDRSRCFORK
1286 	if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
1287 #else
1288 	if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp)) {
1289 #endif /* NAMEDRSRCFORK */
1290 		ndp->ni_flag |= NAMEI_UNFINISHED;
1291 		ndp->ni_ncgeneration = dp->v_nc_generation;
1292 		return 0;
1293 	}
1294 
1295 	nc_generation = dp->v_nc_generation;
1296 
1297 	/*
1298 	 * Note:
1299 	 * Filesystems that support hardlinks may want to call vnode_update_identity
1300 	 * if the lookup operation below will modify the in-core vnode to belong to a new point
1301 	 * in the namespace.  VFS cannot infer whether or not the look up operation makes the vnode
1302 	 * name change or change parents.  Without this, the lookup may make update
1303 	 * filesystem-specific in-core metadata but fail to update the v_parent or v_name
1304 	 * fields in the vnode.  If VFS were to do this, it would be necessary to call
1305 	 * vnode_update_identity on every lookup operation -- expensive!
1306 	 *
1307 	 * However, even with this in place, multiple lookups may occur in between this lookup
1308 	 * and the subsequent vnop, so, at best, we could only guarantee that you would get a
1309 	 * valid path back, and not necessarily the one that you wanted.
1310 	 *
1311 	 * Example:
1312 	 * /tmp/a == /foo/b
1313 	 *
1314 	 * If you are now looking up /foo/b and the vnode for this link represents /tmp/a,
1315 	 * vnode_update_identity will fix the parentage so that you can get /foo/b back
1316 	 * through the v_parent chain (preventing you from getting /tmp/b back). It would
1317 	 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1318 	 */
1319 
1320 	error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
1321 
1322 	if (error) {
1323 lookup_error:
1324 #if CONFIG_UNION_MOUNTS
1325 		if ((error == ENOENT) &&
1326 		    (dp->v_mount != NULL) &&
1327 		    (dp->v_mount->mnt_flag & MNT_UNION)) {
1328 			tdp = dp;
1329 			error = lookup_traverse_union(tdp, &dp, ctx);
1330 			vnode_put(tdp);
1331 			if (error) {
1332 				dp = NULLVP;
1333 				goto bad;
1334 			}
1335 
1336 			ndp->ni_dvp = dp;
1337 			dp_authorized = 0;
1338 			goto unionlookup;
1339 		}
1340 #endif /* CONFIG_UNION_MOUNTS */
1341 
1342 		if (error != EJUSTRETURN) {
1343 			goto bad;
1344 		}
1345 
1346 		if (ndp->ni_vp != NULLVP) {
1347 			panic("leaf should be empty");
1348 		}
1349 
1350 #if NAMEDRSRCFORK
1351 		/*
1352 		 * At this point, error should be EJUSTRETURN.
1353 		 *
1354 		 * If CN_WANTSRSRCFORK is set, that implies that the
1355 		 * underlying filesystem could not find the "parent" of the
1356 		 * resource fork (the data fork), and we are doing a lookup
1357 		 * for a CREATE event.
1358 		 *
1359 		 * However, this should be converted to an error, as the
1360 		 * failure to find this parent should disallow further
1361 		 * progress to try and acquire a resource fork vnode.
1362 		 */
1363 		if (cnp->cn_flags & CN_WANTSRSRCFORK) {
1364 			error = ENOENT;
1365 			goto bad;
1366 		}
1367 #endif
1368 
1369 		error = lookup_validate_creation_path(ndp);
1370 		if (error) {
1371 			goto bad;
1372 		}
1373 		/*
1374 		 * We return with ni_vp NULL to indicate that the entry
1375 		 * doesn't currently exist, leaving a pointer to the
1376 		 * referenced directory vnode in ndp->ni_dvp.
1377 		 */
1378 		if (cnp->cn_flags & SAVESTART) {
1379 			if ((vnode_get(ndp->ni_dvp))) {
1380 				error = ENOENT;
1381 				goto bad;
1382 			}
1383 			ndp->ni_startdir = ndp->ni_dvp;
1384 		}
1385 		if (!wantparent) {
1386 			vnode_put(ndp->ni_dvp);
1387 		}
1388 
1389 		if (kdebug_enable) {
1390 			kdebug_lookup(ndp->ni_dvp, cnp);
1391 		}
1392 		return 0;
1393 	}
1394 returned_from_lookup_path:
1395 	/* We'll always have an iocount on ni_vp when this finishes. */
1396 	error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx);
1397 	if (error != 0) {
1398 		goto bad2;
1399 	}
1400 
1401 	if (keep_going) {
1402 		dp = ndp->ni_vp;
1403 
1404 		/* namei() will handle symlinks */
1405 		if ((dp->v_type == VLNK) &&
1406 		    ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
1407 			return 0;
1408 		}
1409 
1410 		/*
1411 		 * Otherwise, there's more path to process.
1412 		 * cache_lookup_path is now responsible for dropping io ref on dp
1413 		 * when it is called again in the dirloop.  This ensures we hold
1414 		 * a ref on dp until we complete the next round of lookup.
1415 		 */
1416 		last_dp = dp;
1417 
1418 		goto dirloop;
1419 	}
1420 
1421 	return 0;
1422 bad2:
1423 	if (ndp->ni_dvp) {
1424 		vnode_put(ndp->ni_dvp);
1425 	}
1426 
1427 	vnode_put(ndp->ni_vp);
1428 	ndp->ni_vp = NULLVP;
1429 
1430 	if (kdebug_enable) {
1431 		kdebug_lookup(dp, cnp);
1432 	}
1433 	return error;
1434 
1435 bad:
1436 	if (dp) {
1437 		vnode_put(dp);
1438 	}
1439 	ndp->ni_vp = NULLVP;
1440 
1441 	if (kdebug_enable) {
1442 		kdebug_lookup(dp, cnp);
1443 	}
1444 	return error;
1445 }
1446 
1447 #if CONFIG_UNION_MOUNTS
1448 /*
1449  * Given a vnode in a union mount, traverse to the equivalent
1450  * vnode in the underlying mount.
1451  */
1452 int
1453 lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx)
1454 {
1455 	char *path = NULL, *pp;
1456 	const char *name, *np;
1457 	size_t len;
1458 	int error = 0;
1459 	struct nameidata nd;
1460 	vnode_t vp = dvp;
1461 
1462 	*new_dvp = NULL;
1463 
1464 	if (vp && vp->v_flag & VROOT) {
1465 		*new_dvp = vp->v_mount->mnt_vnodecovered;
1466 		if (vnode_getwithref(*new_dvp)) {
1467 			return ENOENT;
1468 		}
1469 		return 0;
1470 	}
1471 
1472 	path = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_NOFAIL);
1473 
1474 	/*
1475 	 * Walk back up to the mountpoint following the
1476 	 * v_parent chain and build a slash-separated path.
1477 	 * Then lookup that path starting with the covered vnode.
1478 	 */
1479 	pp = path + (MAXPATHLEN - 1);
1480 	*pp = '\0';
1481 
1482 	while (1) {
1483 		name = vnode_getname(vp);
1484 		if (name == NULL) {
1485 			printf("lookup_traverse_union: null parent name: .%s\n", pp);
1486 			error = ENOENT;
1487 			goto done;
1488 		}
1489 		len = strlen(name);
1490 		if ((len + 1) > (size_t)(pp - path)) {          // Enough space for this name ?
1491 			error = ENAMETOOLONG;
1492 			vnode_putname(name);
1493 			goto done;
1494 		}
1495 		for (np = name + len; len > 0; len--) { // Copy name backwards
1496 			*--pp = *--np;
1497 		}
1498 		vnode_putname(name);
1499 		vp = vp->v_parent;
1500 		if (vp == NULLVP || vp->v_flag & VROOT) {
1501 			break;
1502 		}
1503 		*--pp = '/';
1504 	}
1505 
1506 	/* Evaluate the path in the underlying mount */
1507 	NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(pp), ctx);
1508 	nd.ni_dvp = dvp->v_mount->mnt_vnodecovered;
1509 	error = namei(&nd);
1510 	if (error == 0) {
1511 		*new_dvp = nd.ni_vp;
1512 	}
1513 	nameidone(&nd);
1514 done:
1515 	if (path) {
1516 		zfree(ZV_NAMEI, path);
1517 	}
1518 	return error;
1519 }
1520 #endif /* CONFIG_UNION_MOUNTS */
1521 
1522 int
1523 lookup_validate_creation_path(struct nameidata *ndp)
1524 {
1525 	struct componentname *cnp = &ndp->ni_cnd;
1526 
1527 	/*
1528 	 * If creating and at end of pathname, then can consider
1529 	 * allowing file to be created.
1530 	 */
1531 	if (cnp->cn_flags & RDONLY) {
1532 		return EROFS;
1533 	}
1534 	if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) {
1535 		return ENOENT;
1536 	}
1537 
1538 	return 0;
1539 }
1540 
1541 /*
1542  * Modifies only ni_vp.  Always returns with ni_vp still valid (iocount held).
1543  */
1544 static int
1545 lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1546     int vbusyflags, vfs_context_t ctx)
1547 {
1548 	mount_t mp;
1549 	vnode_t tdp;
1550 	int error = 0;
1551 	uint32_t depth = 0;
1552 	vnode_t mounted_on_dp;
1553 	int current_mount_generation = 0;
1554 #if CONFIG_TRIGGERS
1555 	vnode_t triggered_dp = NULLVP;
1556 	int retry_cnt = 0;
1557 #define MAX_TRIGGER_RETRIES 1
1558 #endif
1559 
1560 	if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT) {
1561 		return 0;
1562 	}
1563 
1564 	mounted_on_dp = dp;
1565 #if CONFIG_TRIGGERS
1566 restart:
1567 #endif
1568 	current_mount_generation = mount_generation;
1569 
1570 	while (dp->v_mountedhere) {
1571 		vnode_lock_spin(dp);
1572 		if ((mp = dp->v_mountedhere)) {
1573 			mp->mnt_crossref++;
1574 			vnode_unlock(dp);
1575 		} else {
1576 			vnode_unlock(dp);
1577 			break;
1578 		}
1579 
1580 		if (ISSET(mp->mnt_lflag, MNT_LFORCE)) {
1581 			mount_dropcrossref(mp, dp, 0);
1582 			break;  // don't traverse into a forced unmount
1583 		}
1584 
1585 
1586 		if (vfs_busy(mp, vbusyflags)) {
1587 			mount_dropcrossref(mp, dp, 0);
1588 			if (vbusyflags == LK_NOWAIT) {
1589 				error = ENOENT;
1590 				goto out;
1591 			}
1592 
1593 			continue;
1594 		}
1595 
1596 		error = VFS_ROOT(mp, &tdp, ctx);
1597 
1598 		mount_dropcrossref(mp, dp, 0);
1599 		vfs_unbusy(mp);
1600 
1601 		if (error) {
1602 			goto out;
1603 		}
1604 
1605 		vnode_put(dp);
1606 		ndp->ni_vp = dp = tdp;
1607 		if (dp->v_type != VDIR) {
1608 #if DEVELOPMENT || DEBUG
1609 			panic("%s : Root of filesystem not a directory",
1610 			    __FUNCTION__);
1611 #else
1612 			break;
1613 #endif
1614 		}
1615 		depth++;
1616 	}
1617 
1618 #if CONFIG_TRIGGERS
1619 	/*
1620 	 * The triggered_dp check here is required but is susceptible to a
1621 	 * (unlikely) race in which trigger mount is done from here and is
1622 	 * unmounted before we get past vfs_busy above. We retry to deal with
1623 	 * that case but it has the side effect of unwanted retries for
1624 	 * "special" processes which don't want to trigger mounts.
1625 	 */
1626 	if (dp->v_resolve && retry_cnt < MAX_TRIGGER_RETRIES) {
1627 		error = vnode_trigger_resolve(dp, ndp, ctx);
1628 		if (error) {
1629 			goto out;
1630 		}
1631 		if (dp == triggered_dp) {
1632 			retry_cnt += 1;
1633 		} else {
1634 			retry_cnt = 0;
1635 		}
1636 		triggered_dp = dp;
1637 		goto restart;
1638 	}
1639 #endif /* CONFIG_TRIGGERS */
1640 
1641 	if (depth) {
1642 		mp = mounted_on_dp->v_mountedhere;
1643 
1644 		if (mp) {
1645 			mount_lock_spin(mp);
1646 			mp->mnt_realrootvp_vid = dp->v_id;
1647 			mp->mnt_realrootvp = dp;
1648 			mp->mnt_generation = current_mount_generation;
1649 			mount_unlock(mp);
1650 		}
1651 	}
1652 
1653 	return 0;
1654 
1655 out:
1656 	return error;
1657 }
1658 
1659 /*
1660  * Takes ni_vp and ni_dvp non-NULL.  Returns with *new_dp set to the location
1661  * at which to start a lookup with a resolved path, and all other iocounts dropped.
1662  */
1663 static int
1664 lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, bool *new_dp_has_iocount, vfs_context_t ctx)
1665 {
1666 	int error;
1667 	char *cp;               /* pointer into pathname argument */
1668 	uio_t auio;
1669 	uio_stackbuf_t uio_buf[UIO_SIZEOF(1)];
1670 	int need_newpathbuf;
1671 	u_int linklen;
1672 	struct componentname *cnp = &ndp->ni_cnd;
1673 	vnode_t dp;
1674 	char *tmppn;
1675 	u_int rsrclen = (cnp->cn_flags & CN_WANTSRSRCFORK) ? sizeof(_PATH_RSRCFORKSPEC) : 0;
1676 	bool dp_has_iocount = false;
1677 
1678 	if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1679 		return ELOOP;
1680 	}
1681 #if CONFIG_MACF
1682 	if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0) {
1683 		return error;
1684 	}
1685 #endif /* MAC */
1686 	if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) {
1687 		need_newpathbuf = 1;
1688 	} else {
1689 		need_newpathbuf = 0;
1690 	}
1691 
1692 	if (need_newpathbuf) {
1693 		cp = zalloc(ZV_NAMEI);
1694 	} else {
1695 		cp = cnp->cn_pnbuf;
1696 	}
1697 	auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf));
1698 
1699 	uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1700 
1701 	error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
1702 
1703 	if (!error) {
1704 		user_ssize_t resid = uio_resid(auio);
1705 
1706 		assert(resid <= MAXPATHLEN);
1707 
1708 		if (resid == MAXPATHLEN) {
1709 			linklen = 0;
1710 		} else {
1711 			/*
1712 			 * Safe to set unsigned with a [larger] signed type here
1713 			 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1714 			 * is only 1024.
1715 			 */
1716 			linklen = (u_int)strnlen(cp, MAXPATHLEN - (u_int)resid);
1717 		}
1718 
1719 		if (linklen == 0) {
1720 			error = ENOENT;
1721 		} else if (linklen + ndp->ni_pathlen + rsrclen > MAXPATHLEN) {
1722 			error = ENAMETOOLONG;
1723 		}
1724 	}
1725 
1726 	if (error) {
1727 		if (need_newpathbuf) {
1728 			zfree(ZV_NAMEI, cp);
1729 		}
1730 		return error;
1731 	}
1732 
1733 	if (need_newpathbuf) {
1734 		tmppn = cnp->cn_pnbuf;
1735 		bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1736 		cnp->cn_pnbuf = cp;
1737 		cnp->cn_pnlen = MAXPATHLEN;
1738 
1739 		if ((cnp->cn_flags & HASBUF)) {
1740 			zfree(ZV_NAMEI, tmppn);
1741 		} else {
1742 			cnp->cn_flags |= HASBUF;
1743 		}
1744 	} else {
1745 		cnp->cn_pnbuf[linklen] = '\0';
1746 	}
1747 
1748 	ndp->ni_pathlen += linklen;
1749 	cnp->cn_nameptr = cnp->cn_pnbuf;
1750 
1751 	/*
1752 	 * starting point for 'relative'
1753 	 * symbolic link path
1754 	 */
1755 	dp = ndp->ni_dvp;
1756 
1757 	/*
1758 	 * get rid of reference returned via 'lookup'
1759 	 * ni_dvp is released only if we restart at /.
1760 	 */
1761 	vnode_put(ndp->ni_vp);
1762 	ndp->ni_vp = NULLVP;
1763 	ndp->ni_dvp = NULLVP;
1764 
1765 	dp_has_iocount = true;
1766 
1767 	/*
1768 	 * Check if symbolic link restarts us at the root
1769 	 */
1770 	if (*(cnp->cn_nameptr) == '/') {
1771 		while (*(cnp->cn_nameptr) == '/') {
1772 			cnp->cn_nameptr++;
1773 			ndp->ni_pathlen--;
1774 		}
1775 		if (linklen != 0) {
1776 			vnode_put(dp); /* ALWAYS have a dvp for a symlink */
1777 			dp_has_iocount = false;
1778 			if ((dp = ndp->ni_rootdir) == NULLVP) {
1779 				return ENOENT;
1780 			}
1781 		}
1782 	}
1783 
1784 	*new_dp = dp;
1785 	*new_dp_has_iocount = dp_has_iocount;
1786 
1787 	return 0;
1788 }
1789 
1790 /*
1791  * relookup - lookup a path name component
1792  *    Used by lookup to re-aquire things.
1793  */
1794 int
1795 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1796 {
1797 	struct vnode *dp = NULL;                /* the directory we are searching */
1798 	int wantparent;                 /* 1 => wantparent or lockparent flag */
1799 	int rdonly;                     /* lookup read-only flag bit */
1800 	int error = 0;
1801 #ifdef NAMEI_DIAGNOSTIC
1802 	int i, newhash;                 /* DEBUG: check name hash */
1803 	char *cp;                       /* DEBUG: check name ptr/len */
1804 #endif
1805 	vfs_context_t ctx = cnp->cn_context;
1806 
1807 	/*
1808 	 * Setup: break out flag bits into variables.
1809 	 */
1810 	wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1811 	rdonly = cnp->cn_flags & RDONLY;
1812 	cnp->cn_flags &= ~ISSYMLINK;
1813 
1814 	if (cnp->cn_flags & NOCACHE) {
1815 		cnp->cn_flags &= ~MAKEENTRY;
1816 	} else {
1817 		cnp->cn_flags |= MAKEENTRY;
1818 	}
1819 
1820 	dp = dvp;
1821 
1822 	/*
1823 	 * Check for degenerate name (e.g. / or "")
1824 	 * which is a way of talking about a directory,
1825 	 * e.g. like "/." or ".".
1826 	 */
1827 	if (cnp->cn_nameptr[0] == '\0') {
1828 		if (cnp->cn_nameiop != LOOKUP || wantparent) {
1829 			error = EISDIR;
1830 			goto bad;
1831 		}
1832 		if (dp->v_type != VDIR) {
1833 			error = ENOTDIR;
1834 			goto bad;
1835 		}
1836 		if ((vnode_get(dp))) {
1837 			error = ENOENT;
1838 			goto bad;
1839 		}
1840 		*vpp = dp;
1841 
1842 		if (cnp->cn_flags & SAVESTART) {
1843 			panic("lookup: SAVESTART");
1844 		}
1845 		return 0;
1846 	}
1847 	/*
1848 	 * We now have a segment name to search for, and a directory to search.
1849 	 */
1850 	if ((error = VNOP_LOOKUP(dp, vpp, cnp, ctx))) {
1851 		if (error != EJUSTRETURN) {
1852 			goto bad;
1853 		}
1854 #if DIAGNOSTIC
1855 		if (*vpp != NULL) {
1856 			panic("leaf should be empty");
1857 		}
1858 #endif
1859 		/*
1860 		 * If creating and at end of pathname, then can consider
1861 		 * allowing file to be created.
1862 		 */
1863 		if (rdonly) {
1864 			error = EROFS;
1865 			goto bad;
1866 		}
1867 		/*
1868 		 * We return with ni_vp NULL to indicate that the entry
1869 		 * doesn't currently exist, leaving a pointer to the
1870 		 * (possibly locked) directory inode in ndp->ni_dvp.
1871 		 */
1872 		return 0;
1873 	}
1874 	dp = *vpp;
1875 
1876 #if DIAGNOSTIC
1877 	/*
1878 	 * Check for symbolic link
1879 	 */
1880 	if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) {
1881 		panic("relookup: symlink found.");
1882 	}
1883 #endif
1884 
1885 	/*
1886 	 * Disallow directory write attempts on read-only file systems.
1887 	 */
1888 	if (rdonly &&
1889 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1890 		error = EROFS;
1891 		goto bad2;
1892 	}
1893 	/* ASSERT(dvp == ndp->ni_startdir) */
1894 
1895 	return 0;
1896 
1897 bad2:
1898 	vnode_put(dp);
1899 bad:
1900 	*vpp = NULL;
1901 
1902 	return error;
1903 }
1904 
1905 /*
1906  * Free pathname buffer
1907  */
1908 void
1909 nameidone(struct nameidata *ndp)
1910 {
1911 	if (ndp->ni_cnd.cn_flags & HASBUF) {
1912 		char *tmp = ndp->ni_cnd.cn_pnbuf;
1913 
1914 		ndp->ni_cnd.cn_pnbuf = NULL;
1915 		ndp->ni_cnd.cn_flags &= ~HASBUF;
1916 		zfree(ZV_NAMEI, tmp);
1917 	}
1918 }
1919 
1920 
1921 /*
1922  * Log (part of) a pathname using kdebug, as used by fs_usage.  The path up to
1923  * and including the current component name are logged.  Up to NUMPARMS * 4
1924  * bytes of pathname will be logged.  If the path to be logged is longer than
1925  * that, then the last NUMPARMS * 4 bytes are logged. That is, the truncation
1926  * removes the leading portion of the path.
1927  *
1928  * The logging is done via multiple KDBG_RELEASE calls.  The first one is marked
1929  * with DBG_FUNC_START.  The last one is marked with DBG_FUNC_END (in addition
1930  * to DBG_FUNC_START if it is also the first).  There may be intermediate ones
1931  * with neither DBG_FUNC_START nor DBG_FUNC_END.
1932  *
1933  * The first event passes the vnode pointer and 24 or 32 (on K32, 12 or 24)
1934  * bytes of pathname.  The remaining events add 32 (on K32, 16) bytes of
1935  * pathname each.  The minimum number of events required to pass the path are
1936  * used.  Any excess padding in the final event (because not all of the 24 or 32
1937  * (on K32, 12 or 16) bytes are needed for the remainder of the path) is set to
1938  * zero bytes, or '>' if there is more path beyond the current component name
1939  * (usually because an intermediate component was not found).
1940  *
1941  * NOTE: If the path length is greater than NUMPARMS * 4, or is not of the form
1942  * 24 + N * 32 (or on K32, 12 + N * 16), there will be no padding.
1943  */
1944 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
1945 
1946 void
1947 kdebug_vfs_lookup(unsigned long *path_words, int path_len, void *vnp,
1948     uint32_t flags)
1949 {
1950 	bool noprocfilt = flags & KDBG_VFS_LOOKUP_FLAG_NOPROCFILT;
1951 
1952 	assert(path_len >= 0);
1953 
1954 	int code = ((flags & KDBG_VFS_LOOKUP_FLAG_LOOKUP) ? VFS_LOOKUP :
1955 	    VFS_LOOKUP_DONE) | DBG_FUNC_START;
1956 
1957 	if (path_len <= (3 * (int)sizeof(long))) {
1958 		code |= DBG_FUNC_END;
1959 	}
1960 
1961 	if (noprocfilt) {
1962 		KDBG_RELEASE_NOPROCFILT(code, kdebug_vnode(vnp), path_words[0],
1963 		    path_words[1], path_words[2]);
1964 	} else {
1965 		KDBG_RELEASE(code, kdebug_vnode(vnp), path_words[0], path_words[1],
1966 		    path_words[2]);
1967 	}
1968 
1969 	code &= ~DBG_FUNC_START;
1970 
1971 	for (int i = 3; i * (int)sizeof(long) < path_len; i += 4) {
1972 		if ((i + 4) * (int)sizeof(long) >= path_len) {
1973 			code |= DBG_FUNC_END;
1974 		}
1975 
1976 		if (noprocfilt) {
1977 			KDBG_RELEASE_NOPROCFILT(code, path_words[i], path_words[i + 1],
1978 			    path_words[i + 2], path_words[i + 3]);
1979 		} else {
1980 			KDBG_RELEASE(code, path_words[i], path_words[i + 1],
1981 			    path_words[i + 2], path_words[i + 3]);
1982 		}
1983 	}
1984 }
1985 
1986 void
1987 kdebug_lookup_gen_events(long *path_words, int path_len, void *vnp, bool lookup)
1988 {
1989 	assert(path_len >= 0);
1990 	kdebug_vfs_lookup((unsigned long *)path_words, path_len, vnp,
1991 	    lookup ? KDBG_VFS_LOOKUP_FLAG_LOOKUP : 0);
1992 }
1993 
1994 void
1995 kdebug_lookup(vnode_t vnp, struct componentname *cnp)
1996 {
1997 	unsigned long path_words[NUMPARMS];
1998 
1999 	/*
2000 	 * Truncate the leading portion of the path to fit in path_words.
2001 	 */
2002 	char *path_end = cnp->cn_nameptr + cnp->cn_namelen;
2003 	size_t path_len = MIN(path_end - cnp->cn_pnbuf,
2004 	    (ssize_t)sizeof(path_words));
2005 	assert(path_len >= 0);
2006 	char *path_trunc = path_end - path_len;
2007 
2008 	memcpy(path_words, path_trunc, path_len);
2009 
2010 	/*
2011 	 * Pad with '\0' or '>'.
2012 	 */
2013 	if (path_len < (ssize_t)sizeof(path_words)) {
2014 		bool complete_str = *(cnp->cn_nameptr + cnp->cn_namelen) == '\0';
2015 		memset((char *)path_words + path_len, complete_str ? '\0' : '>',
2016 		    sizeof(path_words) - path_len);
2017 	}
2018 	kdebug_vfs_lookup(path_words, (int)path_len, vnp, KDBG_VFS_LOOKUP_FLAG_LOOKUP);
2019 }
2020 
2021 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
2022 
2023 void
2024 kdebug_vfs_lookup(long *dbg_parms __unused, int dbg_namelen __unused,
2025     void *dp __unused, __unused uint32_t flags)
2026 {
2027 }
2028 
2029 static void
2030 kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
2031 {
2032 }
2033 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
2034 
2035 int
2036 vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx)
2037 {
2038 	mount_t mp;
2039 	int error;
2040 
2041 	mp = mount_lookupby_volfsid(fsid->val[0], 1);
2042 	if (mp == NULL) {
2043 		return EINVAL;
2044 	}
2045 
2046 	/* Get the target vnode. */
2047 	if (ino == 2) {
2048 		error = VFS_ROOT(mp, vpp, ctx);
2049 	} else {
2050 		error = VFS_VGET(mp, ino, vpp, ctx);
2051 	}
2052 
2053 	vfs_unbusy(mp);
2054 	return error;
2055 }
2056 /*
2057  * Obtain the real path from a legacy volfs style path.
2058  *
2059  * Valid formats of input path:
2060  *
2061  *	"555/@"
2062  *	"555/2"
2063  *	"555/123456"
2064  *	"555/123456/foobar"
2065  *
2066  * Where:
2067  *	555 represents the volfs file system id
2068  *	'@' and '2' are aliases to the root of a file system
2069  *	123456 represents a file id
2070  *	"foobar" represents a file name
2071  */
2072 #if CONFIG_VOLFS
2073 static int
2074 vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
2075 {
2076 	vnode_t vp;
2077 	struct mount *mp = NULL;
2078 	char  *str;
2079 	char ch;
2080 	unsigned long id;
2081 	ino64_t ino;
2082 	int error;
2083 	int length;
2084 
2085 	/* Get file system id and move str to next component. */
2086 	id = strtoul(path, &str, 10);
2087 	if (id == 0 || str[0] != '/') {
2088 		return EINVAL;
2089 	}
2090 	while (*str == '/') {
2091 		str++;
2092 	}
2093 	ch = *str;
2094 
2095 	if (id > INT_MAX) {
2096 		return ENOENT;
2097 	}
2098 	mp = mount_lookupby_volfsid((int)id, 1);
2099 	if (mp == NULL) {
2100 		return EINVAL;  /* unexpected failure */
2101 	}
2102 	/* Check for an alias to a file system root. */
2103 	if (ch == '@' && str[1] == '\0') {
2104 		ino = 2;
2105 		str++;
2106 	} else {
2107 		/* Get file id and move str to next component. */
2108 		ino = strtouq(str, &str, 10);
2109 	}
2110 
2111 	/* Get the target vnode. */
2112 	if (ino == 2) {
2113 		struct vfs_attr vfsattr;
2114 		int use_vfs_root = TRUE;
2115 
2116 		VFSATTR_INIT(&vfsattr);
2117 		VFSATTR_WANTED(&vfsattr, f_capabilities);
2118 		if (vfs_getattr(mp, &vfsattr, vfs_context_kernel()) == 0 &&
2119 		    VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) {
2120 			if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS) &&
2121 			    (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS)) {
2122 				use_vfs_root = FALSE;
2123 			}
2124 		}
2125 
2126 		if (use_vfs_root) {
2127 			error = VFS_ROOT(mp, &vp, ctx);
2128 		} else {
2129 			error = VFS_VGET(mp, ino, &vp, ctx);
2130 		}
2131 	} else {
2132 		error = VFS_VGET(mp, ino, &vp, ctx);
2133 	}
2134 	vfs_unbusy(mp);
2135 	if (error) {
2136 		goto out;
2137 	}
2138 	realpath[0] = '\0';
2139 
2140 	/* Get the absolute path to this vnode. */
2141 	error = build_path(vp, realpath, (int)bufsize, &length, 0, ctx);
2142 	vnode_put(vp);
2143 
2144 	if (error == 0 && *str != '\0') {
2145 		size_t attempt = strlcat(realpath, str, MAXPATHLEN);
2146 		if (attempt > MAXPATHLEN) {
2147 			error = ENAMETOOLONG;
2148 		}
2149 	}
2150 out:
2151 	return error;
2152 }
2153 #endif
2154 
2155 void
2156 lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create)
2157 {
2158 	if (error == 0 && vp == NULLVP) {
2159 		panic("NULL vp with error == 0.");
2160 	}
2161 
2162 	/*
2163 	 * We don't want to do any of this if we didn't use the compound vnop
2164 	 * to perform the lookup... i.e. if we're allowing and using the legacy pattern,
2165 	 * where we did a full lookup.
2166 	 */
2167 	if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) {
2168 		return;
2169 	}
2170 
2171 	/*
2172 	 * If we're going to continue the lookup, we'll handle
2173 	 * all lookup-related updates at that time.
2174 	 */
2175 	if (error == EKEEPLOOKING) {
2176 		return;
2177 	}
2178 
2179 	/*
2180 	 * Only audit or update cache for *found* vnodes.  For creation
2181 	 * neither would happen in the non-compound-vnop case.
2182 	 */
2183 	if ((vp != NULLVP) && !did_create) {
2184 		/*
2185 		 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
2186 		 * then we certainly don't want to update cache or identity.
2187 		 */
2188 		if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) {
2189 			lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration);
2190 		}
2191 		if (ndp->ni_cnd.cn_flags & AUDITVNPATH1) {
2192 			AUDIT_ARG(vnpath, vp, ARG_VNODE1);
2193 		} else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2) {
2194 			AUDIT_ARG(vnpath, vp, ARG_VNODE2);
2195 		}
2196 	}
2197 
2198 	/*
2199 	 * If you created (whether you opened or not), cut a lookup tracepoint
2200 	 * for the parent dir (as would happen without a compound vnop).  Note: we may need
2201 	 * a vnode despite failure in this case!
2202 	 *
2203 	 * If you did not create:
2204 	 *      Found child (succeeded or not): cut a tracepoint for the child.
2205 	 *      Did not find child: cut a tracepoint with the parent.
2206 	 */
2207 	if (kdebug_enable) {
2208 		kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
2209 	}
2210 }
2211