]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/vfs/vfs_lookup.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
... / ...
CommitLineData
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
111static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
112#define MAX_VOLFS_RESTARTS 5
113#endif
114
115static int lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, int vbusyflags, vfs_context_t ctx);
116static int lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, bool* dp_has_iocount, vfs_context_t ctx);
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);
123
124#if NAMEDRSRCFORK
125static int lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx);
126#endif
127
128extern 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 */
165int
166namei(struct nameidata *ndp)
167{
168 struct filedesc *fdp; /* pointer to file descriptor state */
169 struct vnode *dp; /* the directory we are searching */
170 struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to
171 * heavy vnode pressure */
172 uint32_t cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
173 int error;
174 struct componentname *cnp = &ndp->ni_cnd;
175 vfs_context_t ctx = cnp->cn_context;
176 proc_t p = vfs_context_proc(ctx);
177#if CONFIG_AUDIT
178/* XXX ut should be from context */
179 uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
180#endif
181
182#if CONFIG_VOLFS
183 int volfs_restarts = 0;
184#endif
185 size_t bytes_copied = 0;
186 vnode_t rootdir_with_usecount = NULLVP;
187 vnode_t startdir_with_usecount = NULLVP;
188 vnode_t usedvp_dp = NULLVP;
189 int32_t old_count = 0;
190 bool dp_has_iocount = false;
191
192 fdp = p->p_fd;
193
194#if DIAGNOSTIC
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 }
204#endif
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
220 error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
221 &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
222 if (error) {
223 goto out_drop;
224 }
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;
233 }
234
235vnode_recycled:
236
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) {
242 cnp->cn_pnbuf = ndp->ni_pathbuf;
243 cnp->cn_pnlen = PATHBUFLEN;
244 }
245#if LP64_DEBUG
246 if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
247 && (ndp->ni_segflg != UIO_SYSSPACE)
248 && (ndp->ni_segflg != UIO_SYSSPACE32)) {
249 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
250 }
251#endif /* LP64_DEBUG */
252
253retry_copy:
254 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
255 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
256 cnp->cn_pnlen, &bytes_copied);
257 } else {
258 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
259 cnp->cn_pnlen, &bytes_copied);
260 }
261 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
262 cnp->cn_pnbuf = zalloc(ZV_NAMEI);
263 cnp->cn_flags |= HASBUF;
264 cnp->cn_pnlen = MAXPATHLEN;
265 bytes_copied = 0;
266
267 goto retry_copy;
268 }
269 if (error) {
270 goto error_out;
271 }
272 assert(bytes_copied <= MAXPATHLEN);
273 ndp->ni_pathlen = (u_int)bytes_copied;
274 bytes_copied = 0;
275
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 */
283 if (proc_is_forcing_hfs_case_sensitivity(p)) {
284 cnp->cn_flags |= CN_SKIPNAMECACHE;
285 }
286
287#if CONFIG_VOLFS
288 /*
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' &&
300 cnp->cn_pnbuf[5] == '/') {
301 char * realpath;
302 int realpath_err;
303 /* Attempt to resolve a legacy volfs style pathname. */
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;
316 }
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;
328 }
329 }
330#endif /* CONFIG_VOLFS */
331
332#if CONFIG_AUDIT
333 /* If we are auditing the kernel pathname, save the user pathname */
334 if (cnp->cn_flags & AUDITVNPATH1) {
335 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
336 }
337 if (cnp->cn_flags & AUDITVNPATH2) {
338 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
339 }
340#endif /* CONFIG_AUDIT */
341
342 /*
343 * Do not allow empty pathnames
344 */
345 if (*cnp->cn_pnbuf == '\0') {
346 error = ENOENT;
347 goto error_out;
348 }
349 if (ndp->ni_flag & NAMEI_NOFOLLOW_ANY) {
350 ndp->ni_loopcnt = MAXSYMLINKS;
351 } else {
352 ndp->ni_loopcnt = 0;
353 }
354
355 /*
356 * determine the starting point for the translation.
357 */
358 proc_dirs_lock_shared(p);
359 lck_rw_lock_shared(&rootvnode_rw_lock);
360
361 if (!(fdp->fd_flags & FD_CHROOT)) {
362 ndp->ni_rootdir = rootvnode;
363 } else {
364 ndp->ni_rootdir = fdp->fd_rdir;
365 }
366
367 if (!ndp->ni_rootdir) {
368 if (!(fdp->fd_flags & FD_CHROOT)) {
369 printf("rootvnode is not set\n");
370 } else {
371 /* This should be a panic */
372 printf("fdp->fd_rdir is not set\n");
373 }
374 lck_rw_unlock_shared(&rootvnode_rw_lock);
375 proc_dirs_unlock_shared(p);
376 error = ENOENT;
377 goto error_out;
378 }
379
380 cnp->cn_nameptr = cnp->cn_pnbuf;
381
382 ndp->ni_usedvp = NULLVP;
383
384 if (*(cnp->cn_nameptr) == '/') {
385 while (*(cnp->cn_nameptr) == '/') {
386 cnp->cn_nameptr++;
387 ndp->ni_pathlen--;
388 }
389 dp = ndp->ni_rootdir;
390 } else if (cnp->cn_flags & USEDVP) {
391 dp = ndp->ni_dvp;
392 ndp->ni_usedvp = dp;
393 usedvp_dp = dp;
394 } else {
395 dp = vfs_context_cwd(ctx);
396 }
397
398 if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
399 dp = NULLVP;
400 lck_rw_unlock_shared(&rootvnode_rw_lock);
401 proc_dirs_unlock_shared(p);
402 error = ENOENT;
403 goto error_out;
404 }
405
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 */
443 lck_rw_unlock_shared(&rootvnode_rw_lock);
444 proc_dirs_unlock_shared(p);
445
446 ndp->ni_dvp = NULLVP;
447 ndp->ni_vp = NULLVP;
448
449 for (;;) {
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
464 ndp->ni_startdir = dp;
465 dp = NULLVP;
466
467 if ((error = lookup(ndp))) {
468 goto error_out;
469 }
470
471 /*
472 * Check for symbolic link
473 */
474 if ((cnp->cn_flags & ISSYMLINK) == 0) {
475 if (startdir_with_usecount) {
476 vnode_rele(startdir_with_usecount);
477 startdir_with_usecount = NULLVP;
478 }
479 if (rootdir_with_usecount) {
480 lck_rw_lock_shared(&rootvnode_rw_lock);
481 if (rootdir_with_usecount == rootvnode) {
482 old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
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 }
492 lck_rw_unlock_shared(&rootvnode_rw_lock);
493 if (rootdir_with_usecount) {
494 vnode_rele(rootdir_with_usecount);
495 rootdir_with_usecount = NULLVP;
496 }
497 }
498
499 return 0;
500 }
501
502continue_symlink:
503 /* Gives us a new path to process, and a starting dir */
504 error = lookup_handle_symlink(ndp, &dp, &dp_has_iocount, ctx);
505 if (error != 0) {
506 break;
507 }
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 }
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 */
527out_drop:
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:
535 if (startdir_with_usecount) {
536 vnode_rele(startdir_with_usecount);
537 startdir_with_usecount = NULLVP;
538 }
539 if (rootdir_with_usecount) {
540 lck_rw_lock_shared(&rootvnode_rw_lock);
541 if (rootdir_with_usecount == rootvnode) {
542 old_count = os_atomic_dec_orig(&rootdir_with_usecount->v_usecount, relaxed);
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 }
550 lck_rw_unlock_shared(&rootvnode_rw_lock);
551 } else {
552 lck_rw_unlock_shared(&rootvnode_rw_lock);
553 vnode_rele(rootdir_with_usecount);
554 }
555 rootdir_with_usecount = NULLVP;
556 }
557
558 if ((cnp->cn_flags & HASBUF)) {
559 cnp->cn_flags &= ~HASBUF;
560 zfree(ZV_NAMEI, cnp->cn_pnbuf);
561 }
562 cnp->cn_pnbuf = NULL;
563 ndp->ni_vp = NULLVP;
564 ndp->ni_dvp = NULLVP;
565
566#if CONFIG_VOLFS
567 /*
568 * Deal with volfs fallout.
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
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.
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
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.
586 *
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
597 if (error == ERECYCLE) {
598 /* vnode was recycled underneath us. re-drive lookup to start at
599 * the beginning again, since recycling invalidated last lookup*/
600 ndp->ni_cnd.cn_flags = cnpflags;
601 ndp->ni_dvp = usedvp;
602 goto vnode_recycled;
603 }
604
605
606 return error;
607}
608
609int
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 }
615
616 return 0;
617}
618
619static int
620lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
621{
622#if !CONFIG_MACF
623#pragma unused(cnp)
624#endif
625
626 int error;
627
628 if (!dp_authorized_in_cache) {
629 error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
630 if (error) {
631 return error;
632 }
633 }
634#if CONFIG_MACF
635 error = mac_vnode_check_lookup(ctx, dp, cnp);
636 if (error) {
637 return error;
638 }
639#endif /* CONFIG_MACF */
640
641 return 0;
642}
643
644static void
645lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
646{
647 int isdot_or_dotdot;
648 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
649
650 if (vp->v_name == NULL || vp->v_parent == NULLVP) {
651 int update_flags = 0;
652
653 if (isdot_or_dotdot == 0) {
654 if (vp->v_name == NULL) {
655 update_flags |= VNODE_UPDATE_NAME;
656 }
657 if (dvp != NULLVP && vp->v_parent == NULLVP) {
658 update_flags |= VNODE_UPDATE_PARENT;
659 }
660
661 if (update_flags) {
662 vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
663 }
664 }
665 }
666 if ((cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
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 */
680 if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot)) {
681 cache_enter_with_gen(dvp, vp, cnp, nc_generation);
682 }
683 }
684}
685
686#if NAMEDRSRCFORK
687/*
688 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
689 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
690 * (if one was provided).
691 */
692static int
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;
697 int nsflags;
698 int error;
699
700 if (dp->v_type != VREG) {
701 error = ENOENT;
702 goto out;
703 }
704 switch (cnp->cn_nameiop) {
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 {
726 error = EPERM;
727 goto out;
728 }
729 break;
730 default:
731 error = EPERM;
732 goto out;
733 }
734
735 nsflags = 0;
736 if (cnp->cn_flags & CN_RAW_ENCRYPTED) {
737 nsflags |= NS_GETRAWENCRYPTED;
738 }
739
740 /* Ask the file system for the resource fork. */
741 error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, nsflags, ctx);
742
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;
746 }
747 if (error) {
748 goto out;
749 }
750 /* The "parent" of the stream is the file. */
751 if (wantparent) {
752 if (ndp->ni_dvp) {
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 */
760
761 /* Restore the truncated pathname buffer (for audits). */
762 if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
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 }
778 }
779 cnp->cn_flags &= ~MAKEENTRY;
780
781 return 0;
782out:
783 return error;
784}
785#endif /* NAMEDRSRCFORK */
786
787/*
788 * iocounts in:
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.
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
796 * nothing else.
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 */
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)
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
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);
848 if (error) {
849 goto out;
850 }
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;
861 return 0;
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);
873 }
874
875#if NAMEDSTREAMS
876 /*
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 */
881 if (vnode_isshadow(dp)) {
882 error = ENOENT;
883 goto out;
884 }
885#endif
886
887nextname:
888 /*
889 * Not a symbolic link. If more pathname,
890 * continue at next component, else return.
891 *
892 * Definitely have a dvp if there's another slash
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 }
913
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 }
922
923 /* If SAVESTART is set, we should have a dvp */
924 if (cnp->cn_flags & SAVESTART) {
925 /*
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 */
931 if ((vnode_get(ndp->ni_dvp))) {
932 error = ENOENT;
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
942 if (cnp->cn_flags & AUDITVNPATH1) {
943 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
944 } else if (cnp->cn_flags & AUDITVNPATH2) {
945 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
946 }
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);
954 if (error != 0) {
955 goto out;
956 }
957
958 dp = ndp->ni_vp;
959 }
960#endif
961 if (kdebug_enable) {
962 kdebug_lookup(ndp->ni_vp, cnp);
963 }
964
965 return 0;
966
967emptyname:
968 error = lookup_handle_emptyname(ndp, cnp, wantparent);
969 if (error != 0) {
970 goto out;
971 }
972
973 return 0;
974out:
975 return error;
976}
977
978/*
979 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
980 */
981static int
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) {
1002 /*
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 */
1008 if ((vnode_get(dp))) {
1009 error = ENOENT;
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
1019 if (cnp->cn_flags & AUDITVNPATH1) {
1020 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
1021 } else if (cnp->cn_flags & AUDITVNPATH2) {
1022 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
1023 }
1024 if (cnp->cn_flags & SAVESTART) {
1025 panic("lookup: SAVESTART");
1026 }
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 ".".
1053 *
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{
1091 char *cp; /* pointer into pathname argument */
1092 vnode_t tdp; /* saved dp */
1093 vnode_t dp; /* the directory we are searching */
1094 int docache = 1; /* == 0 do not cache last component */
1095 int wantparent; /* 1 => wantparent or lockparent flag */
1096 int rdonly; /* lookup read-only flag bit */
1097 int dp_authorized = 0;
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 */
1110 if (cnp->cn_flags & NOCACHE) {
1111 docache = 0;
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
1121 if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) {
1122 vbusyflags = LK_NOWAIT;
1123 }
1124 cp = cnp->cn_nameptr;
1125
1126 if (*cp == '\0') {
1127 if ((vnode_getwithref(dp))) {
1128 dp = NULLVP;
1129 error = ENOENT;
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 }
1140dirloop:
1141 atroot = 0;
1142 ndp->ni_vp = NULLVP;
1143
1144 if ((error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp))) {
1145 dp = NULLVP;
1146 goto bad;
1147 }
1148 if ((cnp->cn_flags & ISLASTCN)) {
1149 if (docache) {
1150 cnp->cn_flags |= MAKEENTRY;
1151 }
1152 } else {
1153 cnp->cn_flags |= MAKEENTRY;
1154 }
1155
1156 dp = ndp->ni_dvp;
1157
1158 if (ndp->ni_vp != NULLVP) {
1159 /*
1160 * cache_lookup_path returned a non-NULL ni_vp then,
1161 * we're guaranteed that the dp is a VDIR, it's
1162 * been authorized, and vp is not ".."
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 */
1169 if (dp != NULLVP) {
1170 nc_generation = dp->v_nc_generation - 1;
1171 }
1172
1173 goto returned_from_lookup_path;
1174 }
1175
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 */
1186 if ((cnp->cn_flags & ISDOTDOT)) {
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) &&
1193 dp != ndp->ni_rootdir) {
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
1221 for (;;) {
1222 if (dp == ndp->ni_rootdir || dp == rootvnode) {
1223 ndp->ni_dvp = dp;
1224 ndp->ni_vp = dp;
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 */
1233 if ((vnode_get(dp))) {
1234 error = ENOENT;
1235 goto bad;
1236 }
1237 atroot = 1;
1238 goto returned_from_lookup_path;
1239 }
1240 if ((dp->v_flag & VROOT) == 0 ||
1241 (cnp->cn_flags & NOCROSSMOUNT)) {
1242 break;
1243 }
1244 if (dp->v_mount == NULL) { /* forced umount */
1245 error = EBADF;
1246 goto bad;
1247 }
1248 tdp = dp;
1249 dp = tdp->v_mount->mnt_vnodecovered;
1250
1251 if ((vnode_getwithref(dp))) {
1252 vnode_put(tdp);
1253 dp = NULLVP;
1254 error = ENOENT;
1255 goto bad;
1256 }
1257
1258 vnode_put(tdp);
1259
1260 ndp->ni_dvp = dp;
1261 dp_authorized = 0;
1262 }
1263 }
1264
1265 /*
1266 * We now have a segment name to search for, and a directory to search.
1267 */
1268unionlookup:
1269 ndp->ni_vp = NULLVP;
1270
1271 if (dp->v_type != VDIR) {
1272 error = ENOTDIR;
1273 goto lookup_error;
1274 }
1275 if ((cnp->cn_flags & DONOTAUTH) != DONOTAUTH) {
1276 error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
1277 if (error) {
1278 goto lookup_error;
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
1287 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
1288#else
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;
1294 }
1295
1296 nc_generation = dp->v_nc_generation;
1297
1298 /*
1299 * Note:
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
1310 * valid path back, and not necessarily the one that you wanted.
1311 *
1312 * Example:
1313 * /tmp/a == /foo/b
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
1318 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1319 */
1320
1321 error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
1322
1323 if (error) {
1324lookup_error:
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
1341 if (error != EJUSTRETURN) {
1342 goto bad;
1343 }
1344
1345 if (ndp->ni_vp != NULLVP) {
1346 panic("leaf should be empty");
1347 }
1348
1349#if NAMEDRSRCFORK
1350 /*
1351 * At this point, error should be EJUSTRETURN.
1352 *
1353 * If CN_WANTSRSRCFORK is set, that implies that the
1354 * underlying filesystem could not find the "parent" of the
1355 * resource fork (the data fork), and we are doing a lookup
1356 * for a CREATE event.
1357 *
1358 * However, this should be converted to an error, as the
1359 * failure to find this parent should disallow further
1360 * progress to try and acquire a resource fork vnode.
1361 */
1362 if (cnp->cn_flags & CN_WANTSRSRCFORK) {
1363 error = ENOENT;
1364 goto bad;
1365 }
1366#endif
1367
1368 error = lookup_validate_creation_path(ndp);
1369 if (error) {
1370 goto bad;
1371 }
1372 /*
1373 * We return with ni_vp NULL to indicate that the entry
1374 * doesn't currently exist, leaving a pointer to the
1375 * referenced directory vnode in ndp->ni_dvp.
1376 */
1377 if (cnp->cn_flags & SAVESTART) {
1378 if ((vnode_get(ndp->ni_dvp))) {
1379 error = ENOENT;
1380 goto bad;
1381 }
1382 ndp->ni_startdir = ndp->ni_dvp;
1383 }
1384 if (!wantparent) {
1385 vnode_put(ndp->ni_dvp);
1386 }
1387
1388 if (kdebug_enable) {
1389 kdebug_lookup(ndp->ni_dvp, cnp);
1390 }
1391 return 0;
1392 }
1393returned_from_lookup_path:
1394 /* We'll always have an iocount on ni_vp when this finishes. */
1395 error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx);
1396 if (error != 0) {
1397 goto bad2;
1398 }
1399
1400 if (keep_going) {
1401 dp = ndp->ni_vp;
1402
1403 /* namei() will handle symlinks */
1404 if ((dp->v_type == VLNK) &&
1405 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
1406 return 0;
1407 }
1408
1409 /*
1410 * Otherwise, there's more path to process.
1411 * cache_lookup_path is now responsible for dropping io ref on dp
1412 * when it is called again in the dirloop. This ensures we hold
1413 * a ref on dp until we complete the next round of lookup.
1414 */
1415 last_dp = dp;
1416
1417 goto dirloop;
1418 }
1419
1420 return 0;
1421bad2:
1422 if (ndp->ni_dvp) {
1423 vnode_put(ndp->ni_dvp);
1424 }
1425
1426 vnode_put(ndp->ni_vp);
1427 ndp->ni_vp = NULLVP;
1428
1429 if (kdebug_enable) {
1430 kdebug_lookup(dp, cnp);
1431 }
1432 return error;
1433
1434bad:
1435 if (dp) {
1436 vnode_put(dp);
1437 }
1438 ndp->ni_vp = NULLVP;
1439
1440 if (kdebug_enable) {
1441 kdebug_lookup(dp, cnp);
1442 }
1443 return error;
1444}
1445
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;
1455 size_t len;
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;
1464 if (vnode_getwithref(*new_dvp)) {
1465 return ENOENT;
1466 }
1467 return 0;
1468 }
1469
1470 path = (char *) zalloc(ZV_NAMEI);
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);
1492 if ((len + 1) > (size_t)(pp - path)) { // Enough space for this name ?
1493 error = ENAMETOOLONG;
1494 vnode_putname(name);
1495 goto done;
1496 }
1497 for (np = name + len; len > 0; len--) { // Copy name backwards
1498 *--pp = *--np;
1499 }
1500 vnode_putname(name);
1501 vp = vp->v_parent;
1502 if (vp == NULLVP || vp->v_flag & VROOT) {
1503 break;
1504 }
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);
1512 if (error == 0) {
1513 *new_dvp = nd.ni_vp;
1514 }
1515 nameidone(&nd);
1516done:
1517 if (path) {
1518 zfree(ZV_NAMEI, path);
1519 }
1520 return error;
1521}
1522
1523int
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 }
1538
1539 return 0;
1540}
1541
1542/*
1543 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1544 */
1545static int
1546lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1547 int vbusyflags, vfs_context_t ctx)
1548{
1549 mount_t mp;
1550 vnode_t tdp;
1551 int error = 0;
1552 uint32_t depth = 0;
1553 vnode_t mounted_on_dp;
1554 int current_mount_generation = 0;
1555#if CONFIG_TRIGGERS
1556 vnode_t triggered_dp = NULLVP;
1557 int retry_cnt = 0;
1558#define MAX_TRIGGER_RETRIES 1
1559#endif
1560
1561 if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT) {
1562 return 0;
1563 }
1564
1565 mounted_on_dp = dp;
1566#if CONFIG_TRIGGERS
1567restart:
1568#endif
1569 current_mount_generation = mount_generation;
1570
1571 while (dp->v_mountedhere) {
1572 vnode_lock_spin(dp);
1573 if ((mp = dp->v_mountedhere)) {
1574 mp->mnt_crossref++;
1575 vnode_unlock(dp);
1576 } else {
1577 vnode_unlock(dp);
1578 break;
1579 }
1580
1581 if (ISSET(mp->mnt_lflag, MNT_LFORCE)) {
1582 mount_dropcrossref(mp, dp, 0);
1583 break; // don't traverse into a forced unmount
1584 }
1585
1586
1587 if (vfs_busy(mp, vbusyflags)) {
1588 mount_dropcrossref(mp, dp, 0);
1589 if (vbusyflags == LK_NOWAIT) {
1590 error = ENOENT;
1591 goto out;
1592 }
1593
1594 continue;
1595 }
1596
1597 error = VFS_ROOT(mp, &tdp, ctx);
1598
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
1613 break;
1614#endif
1615 }
1616 depth++;
1617 }
1618
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);
1629 if (error) {
1630 goto out;
1631 }
1632 if (dp == triggered_dp) {
1633 retry_cnt += 1;
1634 } else {
1635 retry_cnt = 0;
1636 }
1637 triggered_dp = dp;
1638 goto restart;
1639 }
1640#endif /* CONFIG_TRIGGERS */
1641
1642 if (depth) {
1643 mp = mounted_on_dp->v_mountedhere;
1644
1645 if (mp) {
1646 mount_lock_spin(mp);
1647 mp->mnt_realrootvp_vid = dp->v_id;
1648 mp->mnt_realrootvp = dp;
1649 mp->mnt_generation = current_mount_generation;
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
1662 * at which to start a lookup with a resolved path, and all other iocounts dropped.
1663 */
1664static int
1665lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, bool *new_dp_has_iocount, vfs_context_t ctx)
1666{
1667 int error;
1668 char *cp; /* pointer into pathname argument */
1669 uio_t auio;
1670 union {
1671 union {
1672 struct user_iovec s_uiovec;
1673 struct kern_iovec s_kiovec;
1674 } u_iovec;
1675 struct uio s_uio;
1676 char uio_buf[UIO_SIZEOF(1)];
1677 } u_uio_buf; /* union only for aligning uio_buf correctly */
1678 int need_newpathbuf;
1679 u_int linklen;
1680 struct componentname *cnp = &ndp->ni_cnd;
1681 vnode_t dp;
1682 char *tmppn;
1683 u_int rsrclen = (cnp->cn_flags & CN_WANTSRSRCFORK) ? sizeof(_PATH_RSRCFORKSPEC) : 0;
1684 bool dp_has_iocount = false;
1685
1686 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1687 return ELOOP;
1688 }
1689#if CONFIG_MACF
1690 if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0) {
1691 return error;
1692 }
1693#endif /* MAC */
1694 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) {
1695 need_newpathbuf = 1;
1696 } else {
1697 need_newpathbuf = 0;
1698 }
1699
1700 if (need_newpathbuf) {
1701 cp = zalloc(ZV_NAMEI);
1702 } else {
1703 cp = cnp->cn_pnbuf;
1704 }
1705 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
1706 &u_uio_buf.uio_buf[0], sizeof(u_uio_buf.uio_buf));
1707
1708 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1709
1710 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
1711 if (error) {
1712 if (need_newpathbuf) {
1713 zfree(ZV_NAMEI, cp);
1714 }
1715 return error;
1716 }
1717
1718 /*
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);
1724 if (linklen + ndp->ni_pathlen + rsrclen > MAXPATHLEN) {
1725 if (need_newpathbuf) {
1726 zfree(ZV_NAMEI, cp);
1727 }
1728
1729 return ENAMETOOLONG;
1730 }
1731 if (need_newpathbuf) {
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
1737 if ((cnp->cn_flags & HASBUF)) {
1738 zfree(ZV_NAMEI, tmppn);
1739 } else {
1740 cnp->cn_flags |= HASBUF;
1741 }
1742 } else {
1743 cnp->cn_pnbuf[linklen] = '\0';
1744 }
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 /*
1756 * get rid of reference returned via 'lookup'
1757 * ni_dvp is released only if we restart at /.
1758 */
1759 vnode_put(ndp->ni_vp);
1760 ndp->ni_vp = NULLVP;
1761 ndp->ni_dvp = NULLVP;
1762
1763 /*
1764 * Check if symbolic link restarts us at the root
1765 */
1766 if (*(cnp->cn_nameptr) == '/') {
1767 vnode_put(dp); /* ALWAYS have a dvp for a symlink */
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 }
1775 } else {
1776 dp_has_iocount = true;
1777 }
1778
1779 *new_dp = dp;
1780 *new_dp_has_iocount = dp_has_iocount;
1781
1782 return 0;
1783}
1784
1785/*
1786 * relookup - lookup a path name component
1787 * Used by lookup to re-aquire things.
1788 */
1789int
1790relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1791{
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 */
1795 int error = 0;
1796#ifdef NAMEI_DIAGNOSTIC
1797 int i, newhash; /* DEBUG: check name hash */
1798 char *cp; /* DEBUG: check name ptr/len */
1799#endif
1800 vfs_context_t ctx = cnp->cn_context;;
1801
1802 /*
1803 * Setup: break out flag bits into variables.
1804 */
1805 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1806 rdonly = cnp->cn_flags & RDONLY;
1807 cnp->cn_flags &= ~ISSYMLINK;
1808
1809 if (cnp->cn_flags & NOCACHE) {
1810 cnp->cn_flags &= ~MAKEENTRY;
1811 } else {
1812 cnp->cn_flags |= MAKEENTRY;
1813 }
1814
1815 dp = dvp;
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 }
1831 if ((vnode_get(dp))) {
1832 error = ENOENT;
1833 goto bad;
1834 }
1835 *vpp = dp;
1836
1837 if (cnp->cn_flags & SAVESTART) {
1838 panic("lookup: SAVESTART");
1839 }
1840 return 0;
1841 }
1842 /*
1843 * We now have a segment name to search for, and a directory to search.
1844 */
1845 if ((error = VNOP_LOOKUP(dp, vpp, cnp, ctx))) {
1846 if (error != EJUSTRETURN) {
1847 goto bad;
1848 }
1849#if DIAGNOSTIC
1850 if (*vpp != NULL) {
1851 panic("leaf should be empty");
1852 }
1853#endif
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 }
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 */
1867 return 0;
1868 }
1869 dp = *vpp;
1870
1871#if DIAGNOSTIC
1872 /*
1873 * Check for symbolic link
1874 */
1875 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) {
1876 panic("relookup: symlink found.\n");
1877 }
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) */
1889
1890 return 0;
1891
1892bad2:
1893 vnode_put(dp);
1894bad:
1895 *vpp = NULL;
1896
1897 return error;
1898}
1899
1900/*
1901 * Free pathname buffer
1902 */
1903void
1904nameidone(struct nameidata *ndp)
1905{
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;
1911 zfree(ZV_NAMEI, tmp);
1912 }
1913}
1914
1915
1916/*
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.
1922 *
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.
1927 *
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).
1935 *
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.
1938 */
1939#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
1940
1941void
1942kdebug_vfs_lookup(unsigned long *path_words, int path_len, void *vnp,
1943 uint32_t flags)
1944{
1945 bool noprocfilt = flags & KDBG_VFS_LOOKUP_FLAG_NOPROCFILT;
1946
1947 assert(path_len >= 0);
1948
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))) {
1953 code |= DBG_FUNC_END;
1954 }
1955
1956 if (noprocfilt) {
1957 KDBG_RELEASE_NOPROCFILT(code, kdebug_vnode(vnp), path_words[0],
1958 path_words[1], path_words[2]);
1959 } else {
1960 KDBG_RELEASE(code, kdebug_vnode(vnp), path_words[0], path_words[1],
1961 path_words[2]);
1962 }
1963
1964 code &= ~DBG_FUNC_START;
1965
1966 for (int i = 3; i * (int)sizeof(long) < path_len; i += 4) {
1967 if ((i + 4) * (int)sizeof(long) >= path_len) {
1968 code |= DBG_FUNC_END;
1969 }
1970
1971 if (noprocfilt) {
1972 KDBG_RELEASE_NOPROCFILT(code, path_words[i], path_words[i + 1],
1973 path_words[i + 2], path_words[i + 3]);
1974 } else {
1975 KDBG_RELEASE(code, path_words[i], path_words[i + 1],
1976 path_words[i + 2], path_words[i + 3]);
1977 }
1978 }
1979}
1980
1981void
1982kdebug_lookup_gen_events(long *path_words, int path_len, void *vnp, bool lookup)
1983{
1984 assert(path_len >= 0);
1985 kdebug_vfs_lookup((unsigned long *)path_words, path_len, vnp,
1986 lookup ? KDBG_VFS_LOOKUP_FLAG_LOOKUP : 0);
1987}
1988
1989void
1990kdebug_lookup(vnode_t vnp, struct componentname *cnp)
1991{
1992 unsigned long path_words[NUMPARMS];
1993
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;
2002
2003 memcpy(path_words, path_trunc, path_len);
2004
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);
2012 }
2013 kdebug_vfs_lookup(path_words, (int)path_len, vnp, KDBG_VFS_LOOKUP_FLAG_LOOKUP);
2014}
2015
2016#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
2017
2018void
2019kdebug_vfs_lookup(long *dbg_parms __unused, int dbg_namelen __unused,
2020 void *dp __unused, __unused uint32_t flags)
2021{
2022}
2023
2024static void
2025kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
2026{
2027}
2028#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
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;
2035
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 }
2047
2048 vfs_unbusy(mp);
2049 return error;
2050}
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;
2075 unsigned long id;
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] != '/') {
2083 return EINVAL;
2084 }
2085 while (*str == '/') {
2086 str++;
2087 }
2088 ch = *str;
2089
2090 if (id > INT_MAX) {
2091 return ENOENT;
2092 }
2093 mp = mount_lookupby_volfsid((int)id, 1);
2094 if (mp == NULL) {
2095 return EINVAL; /* unexpected failure */
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. */
2103 ino = strtouq(str, &str, 10);
2104 }
2105
2106 /* Get the target vnode. */
2107 if (ino == 2) {
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 }
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. */
2136 error = build_path(vp, realpath, (int)bufsize, &length, 0, ctx);
2137 vnode_put(vp);
2138
2139 if (error == 0 && *str != '\0') {
2140 size_t attempt = strlcat(realpath, str, MAXPATHLEN);
2141 if (attempt > MAXPATHLEN) {
2142 error = ENAMETOOLONG;
2143 }
2144 }
2145out:
2146 return error;
2147}
2148#endif
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
2157 /*
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
2166 /*
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) {
2179 /*
2180 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
2181 * then we certainly don't want to update cache or identity.
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 }
2186 if (ndp->ni_cnd.cn_flags & AUDITVNPATH1) {
2187 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
2188 } else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2) {
2189 AUDIT_ARG(vnpath, vp, ARG_VNODE2);
2190 }
2191 }
2192
2193 /*
2194 * If you created (whether you opened or not), cut a lookup tracepoint
2195 * for the parent dir (as would happen without a compound vnop). Note: we may need
2196 * a vnode despite failure in this case!
2197 *
2198 * If you did not create:
2199 * Found child (succeeded or not): cut a tracepoint for the child.
2200 * Did not find child: cut a tracepoint with the parent.
2201 */
2202 if (kdebug_enable) {
2203 kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
2204 }
2205}