]> git.saurik.com Git - apple/xnu.git/blame - bsd/vfs/vfs_lookup.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
CommitLineData
1c79356b 1/*
3e170ce0 2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
67 */
2d21ac55
A
68/*
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
72 * Version 2.0.
73 */
1c79356b
A
74
75#include <sys/param.h>
55e303ae 76#include <sys/systm.h>
1c79356b
A
77#include <sys/syslimits.h>
78#include <sys/time.h>
79#include <sys/namei.h>
80#include <sys/vm.h>
91447636
A
81#include <sys/vnode_internal.h>
82#include <sys/mount_internal.h>
1c79356b
A
83#include <sys/errno.h>
84#include <sys/malloc.h>
85#include <sys/filedesc.h>
91447636 86#include <sys/proc_internal.h>
1c79356b
A
87#include <sys/kdebug.h>
88#include <sys/unistd.h> /* For _PC_NAME_MAX */
91447636
A
89#include <sys/uio_internal.h>
90#include <sys/kauth.h>
39236c6e 91#include <kern/kalloc.h>
b0d623f7 92#include <security/audit/audit.h>
3e170ce0 93#include <sys/dtrace.h> /* to get the prototype for strstr() in sys/dtrace_glue.h */
2d21ac55
A
94#if CONFIG_MACF
95#include <security/mac_framework.h>
96#endif
97
98#if NAMEDRSRCFORK
99#include <sys/xattr.h>
1c79356b 100#endif
2d21ac55
A
101/*
102 * The minimum volfs-style pathname is 9.
103 * Example: "/.vol/1/2"
104 */
105#define VOLFS_MIN_PATH_LEN 9
1c79356b 106
91447636
A
107
108static void kdebug_lookup(struct vnode *dp, struct componentname *cnp);
55e303ae 109
2d21ac55
A
110#if CONFIG_VOLFS
111static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
39236c6e 112#define MAX_VOLFS_RESTARTS 5
2d21ac55
A
113#endif
114
fe8ab488
A
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, 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,
6d2010ae
A
120 int vbusyflags, int *keep_going, int nc_generation,
121 int wantparent, int atroot, vfs_context_t ctx);
fe8ab488 122static int lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent);
6d2010ae 123
fe8ab488
A
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
39236c6e
A
127
128
129
1c79356b
A
130/*
131 * Convert a pathname into a pointer to a locked inode.
132 *
133 * The FOLLOW flag is set when symbolic links are to be followed
134 * when they occur at the end of the name translation process.
135 * Symbolic links are always followed for all other pathname
136 * components other than the last.
137 *
138 * The segflg defines whether the name is to be copied from user
139 * space or kernel space.
140 *
141 * Overall outline of namei:
142 *
143 * copy in name
144 * get starting directory
145 * while (!done && !error) {
146 * call lookup to search path.
147 * if symbolic link, massage name in buffer and continue
148 * }
2d21ac55
A
149 *
150 * Returns: 0 Success
151 * ENOENT No such file or directory
152 * ELOOP Too many levels of symbolic links
153 * ENAMETOOLONG Filename too long
154 * copyinstr:EFAULT Bad address
155 * copyinstr:ENAMETOOLONG Filename too long
156 * lookup:EBADF Bad file descriptor
157 * lookup:EROFS
158 * lookup:EACCES
159 * lookup:EPERM
4a3eedf9
A
160 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
161 * This means we should re-drive lookup from this point.
162 * lookup: ???
2d21ac55 163 * VNOP_READLINK:???
1c79356b
A
164 */
165int
2d21ac55 166namei(struct nameidata *ndp)
1c79356b 167{
2d21ac55 168 struct filedesc *fdp; /* pointer to file descriptor state */
2d21ac55 169 struct vnode *dp; /* the directory we are searching */
4a3eedf9
A
170 struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to
171 heavy vnode pressure */
172 u_long cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
91447636 173 int error;
1c79356b 174 struct componentname *cnp = &ndp->ni_cnd;
91447636 175 vfs_context_t ctx = cnp->cn_context;
2d21ac55 176 proc_t p = vfs_context_proc(ctx);
b0d623f7 177#if CONFIG_AUDIT
2d21ac55
A
178/* XXX ut should be from context */
179 uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
b0d623f7 180#endif
6d2010ae 181
39236c6e
A
182#if CONFIG_VOLFS
183 int volfs_restarts = 0;
184#endif
5ba3f43e 185 size_t bytes_copied = 0;
39236c6e 186
6d2010ae 187 fdp = p->p_fd;
1c79356b 188
1c79356b 189#if DIAGNOSTIC
91447636 190 if (!vfs_context_ucred(ctx) || !p)
1c79356b
A
191 panic ("namei: bad cred/proc");
192 if (cnp->cn_nameiop & (~OPMASK))
193 panic ("namei: nameiop contaminated with flags");
194 if (cnp->cn_flags & OPMASK)
195 panic ("namei: flags contaminated with nameiops");
196#endif
6d2010ae
A
197
198 /*
199 * A compound VNOP found something that needs further processing:
200 * either a trigger vnode, a covered directory, or a symlink.
201 */
202 if (ndp->ni_flag & NAMEI_CONTLOOKUP) {
203 int rdonly, vbusyflags, keep_going, wantparent;
204
205 rdonly = cnp->cn_flags & RDONLY;
206 vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0;
207 keep_going = 0;
208 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
209
210 ndp->ni_flag &= ~(NAMEI_CONTLOOKUP);
211
212 error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
213 &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
214 if (error)
215 goto out_drop;
216 if (keep_going) {
217 if ((cnp->cn_flags & ISSYMLINK) == 0) {
218 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);
219 }
220 goto continue_symlink;
221 }
222
223 return 0;
224
225 }
1c79356b 226
4a3eedf9
A
227vnode_recycled:
228
1c79356b
A
229 /*
230 * Get a buffer for the name to be translated, and copy the
231 * name into the buffer.
232 */
233 if ((cnp->cn_flags & HASBUF) == 0) {
2d21ac55 234 cnp->cn_pnbuf = ndp->ni_pathbuf;
91447636 235 cnp->cn_pnlen = PATHBUFLEN;
1c79356b 236 }
91447636 237#if LP64_DEBUG
b0d623f7
A
238 if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
239 && (ndp->ni_segflg != UIO_SYSSPACE)
240 && (ndp->ni_segflg != UIO_SYSSPACE32)) {
91447636
A
241 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
242 }
243#endif /* LP64_DEBUG */
244
245retry_copy:
2d21ac55 246 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
1c79356b 247 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
5ba3f43e 248 cnp->cn_pnlen, &bytes_copied);
2d21ac55 249 } else {
91447636 250 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
5ba3f43e 251 cnp->cn_pnlen, &bytes_copied);
2d21ac55 252 }
91447636 253 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
2d21ac55
A
254 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
255 if (cnp->cn_pnbuf == NULL) {
256 error = ENOMEM;
257 goto error_out;
258 }
91447636
A
259
260 cnp->cn_flags |= HASBUF;
261 cnp->cn_pnlen = MAXPATHLEN;
5ba3f43e 262 bytes_copied = 0;
91447636
A
263
264 goto retry_copy;
265 }
266 if (error)
267 goto error_out;
5ba3f43e
A
268 ndp->ni_pathlen = bytes_copied;
269 bytes_copied = 0;
55e303ae 270
39236c6e
A
271 /*
272 * Since the name cache may contain positive entries of
273 * the incorrect case, force lookup() to bypass the cache
274 * and call directly into the filesystem for each path
275 * component. Note: the FS may still consult the cache,
276 * but can apply rules to validate the results.
277 */
278 if (proc_is_forcing_hfs_case_sensitivity(p))
279 cnp->cn_flags |= CN_SKIPNAMECACHE;
280
2d21ac55
A
281#if CONFIG_VOLFS
282 /*
283 * Check for legacy volfs style pathnames.
284 *
285 * For compatibility reasons we currently allow these paths,
286 * but future versions of the OS may not support them.
287 */
288 if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN &&
289 cnp->cn_pnbuf[0] == '/' &&
290 cnp->cn_pnbuf[1] == '.' &&
291 cnp->cn_pnbuf[2] == 'v' &&
292 cnp->cn_pnbuf[3] == 'o' &&
293 cnp->cn_pnbuf[4] == 'l' &&
294 cnp->cn_pnbuf[5] == '/' ) {
295 char * realpath;
296 int realpath_err;
297 /* Attempt to resolve a legacy volfs style pathname. */
298 MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
299 if (realpath) {
b0d623f7
A
300 /*
301 * We only error out on the ENAMETOOLONG cases where we know that
302 * vfs_getrealpath translation succeeded but the path could not fit into
303 * MAXPATHLEN characters. In other failure cases, we may be dealing with a path
304 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
305 */
2d21ac55
A
306 if ((realpath_err= vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) {
307 FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI);
b0d623f7 308 if (realpath_err == ENOSPC || realpath_err == ENAMETOOLONG){
2d21ac55
A
309 error = ENAMETOOLONG;
310 goto error_out;
311 }
312 } else {
313 if (cnp->cn_flags & HASBUF) {
314 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
315 }
316 cnp->cn_pnbuf = realpath;
317 cnp->cn_pnlen = MAXPATHLEN;
318 ndp->ni_pathlen = strlen(realpath) + 1;
319 cnp->cn_flags |= HASBUF | CN_VOLFSPATH;
320 }
321 }
322 }
b0d623f7 323#endif /* CONFIG_VOLFS */
2d21ac55 324
b0d623f7 325#if CONFIG_AUDIT
55e303ae
A
326 /* If we are auditing the kernel pathname, save the user pathname */
327 if (cnp->cn_flags & AUDITVNPATH1)
2d21ac55 328 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
55e303ae 329 if (cnp->cn_flags & AUDITVNPATH2)
2d21ac55 330 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
b0d623f7 331#endif /* CONFIG_AUDIT */
55e303ae 332
1c79356b
A
333 /*
334 * Do not allow empty pathnames
335 */
91447636 336 if (*cnp->cn_pnbuf == '\0') {
1c79356b 337 error = ENOENT;
2d21ac55 338 goto error_out;
1c79356b
A
339 }
340 ndp->ni_loopcnt = 0;
1c79356b
A
341
342 /*
91447636 343 * determine the starting point for the translation.
1c79356b 344 */
91447636
A
345 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) {
346 if ( !(fdp->fd_flags & FD_CHROOT))
347 ndp->ni_rootdir = rootvnode;
55e303ae 348 }
91447636 349 cnp->cn_nameptr = cnp->cn_pnbuf;
55e303ae 350
91447636
A
351 ndp->ni_usedvp = NULLVP;
352
353 if (*(cnp->cn_nameptr) == '/') {
354 while (*(cnp->cn_nameptr) == '/') {
355 cnp->cn_nameptr++;
356 ndp->ni_pathlen--;
1c79356b 357 }
91447636
A
358 dp = ndp->ni_rootdir;
359 } else if (cnp->cn_flags & USEDVP) {
360 dp = ndp->ni_dvp;
361 ndp->ni_usedvp = dp;
362 } else
2d21ac55 363 dp = vfs_context_cwd(ctx);
91447636 364
2d21ac55 365 if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
91447636
A
366 error = ENOENT;
367 goto error_out;
368 }
369 ndp->ni_dvp = NULLVP;
370 ndp->ni_vp = NULLVP;
371
372 for (;;) {
5ba3f43e
A
373#if CONFIG_MACF
374 /*
375 * Give MACF policies a chance to reject the lookup
376 * before performing any filesystem operations.
377 * This hook is called before resolving the path and
378 * again each time a symlink is encountered.
379 * NB: policies receive path information as supplied
380 * by the caller and thus cannot be trusted.
381 */
382 error = mac_vnode_check_lookup_preflight(ctx, dp, cnp->cn_nameptr, cnp->cn_namelen);
383 if (error) {
384 goto error_out;
385 }
386#endif
387
1c79356b 388 ndp->ni_startdir = dp;
91447636
A
389
390 if ( (error = lookup(ndp)) ) {
391 goto error_out;
1c79356b 392 }
3e170ce0 393
1c79356b
A
394 /*
395 * Check for symbolic link
396 */
397 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1c79356b
A
398 return (0);
399 }
91447636 400
6d2010ae
A
401continue_symlink:
402 /* Gives us a new path to process, and a starting dir */
403 error = lookup_handle_symlink(ndp, &dp, ctx);
404 if (error != 0) {
1c79356b
A
405 break;
406 }
91447636
A
407 }
408 /*
409 * only come here if we fail to handle a SYMLINK...
410 * if either ni_dvp or ni_vp is non-NULL, then
411 * we need to drop the iocount that was picked
412 * up in the lookup routine
413 */
6d2010ae 414out_drop:
91447636
A
415 if (ndp->ni_dvp)
416 vnode_put(ndp->ni_dvp);
417 if (ndp->ni_vp)
418 vnode_put(ndp->ni_vp);
419 error_out:
420 if ( (cnp->cn_flags & HASBUF) ) {
2d21ac55 421 cnp->cn_flags &= ~HASBUF;
91447636
A
422 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
423 }
55e303ae 424 cnp->cn_pnbuf = NULL;
91447636 425 ndp->ni_vp = NULLVP;
6d2010ae 426 ndp->ni_dvp = NULLVP;
39236c6e
A
427
428#if CONFIG_VOLFS
429 /*
430 * Deal with volfs fallout.
431 *
432 * At this point, if we were originally given a volfs path that
433 * looks like /.vol/123/456, then we would have had to convert it into
434 * a full path. Assuming that part worked properly, we will now attempt
435 * to conduct a lookup of the item in the namespace. Under normal
436 * circumstances, if a user looked up /tmp/foo and it was not there, it
437 * would be permissible to return ENOENT.
438 *
439 * However, we may not want to do that here. Specifically, the volfs path
440 * uniquely identifies a certain item in the namespace regardless of where it
441 * lives. If the item has moved in between the time we constructed the
442 * path and now, when we're trying to do a lookup/authorization on the full
443 * path, we may have gotten an ENOENT.
444 *
445 * At this point we can no longer tell if the path no longer exists
446 * or if the item in question no longer exists. It could have been renamed
447 * away, in which case the /.vol identifier is still valid.
448 *
449 * Do this dance a maximum of MAX_VOLFS_RESTARTS times.
450 */
451 if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) {
452 if (volfs_restarts < MAX_VOLFS_RESTARTS) {
453 volfs_restarts++;
454 goto vnode_recycled;
455 }
456 }
457#endif
458
4a3eedf9
A
459 if (error == ERECYCLE){
460 /* vnode was recycled underneath us. re-drive lookup to start at
461 the beginning again, since recycling invalidated last lookup*/
462 ndp->ni_cnd.cn_flags = cnpflags;
463 ndp->ni_dvp = usedvp;
464 goto vnode_recycled;
465 }
466
55e303ae 467
1c79356b
A
468 return (error);
469}
470
6d2010ae
A
471int
472namei_compound_available(vnode_t dp, struct nameidata *ndp)
473{
474 if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) {
475 return vnode_compound_open_available(dp);
476 }
91447636 477
6d2010ae
A
478 return 0;
479}
5ba3f43e 480
fe8ab488 481static int
6d2010ae 482lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
1c79356b 483{
39236c6e
A
484#if !CONFIG_MACF
485#pragma unused(cnp)
486#endif
487
6d2010ae 488 int error;
1c79356b 489
6d2010ae
A
490 if (!dp_authorized_in_cache) {
491 error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
492 if (error)
493 return error;
91447636 494 }
6d2010ae
A
495#if CONFIG_MACF
496 error = mac_vnode_check_lookup(ctx, dp, cnp);
497 if (error)
498 return error;
499#endif /* CONFIG_MACF */
91447636 500
6d2010ae
A
501 return 0;
502}
1c79356b 503
fe8ab488 504static void
6d2010ae
A
505lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
506{
507 int isdot_or_dotdot;
508 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
55e303ae 509
6d2010ae
A
510 if (vp->v_name == NULL || vp->v_parent == NULLVP) {
511 int update_flags = 0;
512
513 if (isdot_or_dotdot == 0) {
514 if (vp->v_name == NULL)
515 update_flags |= VNODE_UPDATE_NAME;
516 if (dvp != NULLVP && vp->v_parent == NULLVP)
517 update_flags |= VNODE_UPDATE_PARENT;
518
519 if (update_flags)
520 vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
55e303ae 521 }
1c79356b 522 }
6d2010ae
A
523 if ( (cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
524 /*
525 * missing from name cache, but should
526 * be in it... this can happen if volfs
527 * causes the vnode to be created or the
528 * name cache entry got recycled but the
529 * vnode didn't...
530 * check to make sure that ni_dvp is valid
531 * cache_lookup_path may return a NULL
532 * do a quick check to see if the generation of the
533 * directory matches our snapshot... this will get
534 * rechecked behind the name cache lock, but if it
535 * already fails to match, no need to go any further
536 */
537 if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot))
538 cache_enter_with_gen(dvp, vp, cnp, nc_generation);
539 }
9bccf70c 540
6d2010ae
A
541}
542
543#if NAMEDRSRCFORK
544/*
545 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
546 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
547 * (if one was provided).
548 */
fe8ab488 549static int
6d2010ae
A
550lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx)
551{
552 vnode_t svp = NULLVP;
553 enum nsoperation nsop;
5ba3f43e 554 int nsflags;
6d2010ae
A
555 int error;
556
557 if (dp->v_type != VREG) {
558 error = ENOENT;
559 goto out;
91447636 560 }
6d2010ae
A
561 switch (cnp->cn_nameiop) {
562 case DELETE:
563 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
564 nsop = NS_DELETE;
565 } else {
566 error = EPERM;
567 goto out;
568 }
569 break;
570 case CREATE:
571 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
572 nsop = NS_CREATE;
573 } else {
574 error = EPERM;
575 goto out;
576 }
577 break;
578 case LOOKUP:
579 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
580 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
581 nsop = NS_OPEN;
582 } else {
583 error = EPERM;
584 goto out;
585 }
586 break;
587 default:
588 error = EPERM;
589 goto out;
590 }
5ba3f43e
A
591
592 nsflags = 0;
593 if (cnp->cn_flags & CN_RAW_ENCRYPTED)
594 nsflags |= NS_GETRAWENCRYPTED;
595
6d2010ae 596 /* Ask the file system for the resource fork. */
5ba3f43e 597 error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, nsflags, ctx);
91447636 598
6d2010ae
A
599 /* During a create, it OK for stream vnode to be missing. */
600 if (error == ENOATTR || error == ENOENT) {
601 error = (nsop == NS_CREATE) ? 0 : ENOENT;
602 }
603 if (error) {
604 goto out;
605 }
606 /* The "parent" of the stream is the file. */
607 if (wantparent) {
608 if (ndp->ni_dvp) {
6d2010ae
A
609 vnode_put(ndp->ni_dvp);
610 }
611 ndp->ni_dvp = dp;
612 } else {
613 vnode_put(dp);
614 }
615 ndp->ni_vp = svp; /* on create this may be null */
91447636 616
6d2010ae
A
617 /* Restore the truncated pathname buffer (for audits). */
618 if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
619 ndp->ni_next[0] = '/';
620 }
621 cnp->cn_flags &= ~MAKEENTRY;
2d21ac55 622
6d2010ae
A
623 return 0;
624out:
625 return error;
626}
627#endif /* NAMEDRSRCFORK */
628
629/*
630 * iocounts in:
631 * --One on ni_vp. One on ni_dvp if there is more path, or we didn't come through the
632 * cache, or we came through the cache and the caller doesn't want the parent.
633 *
634 * iocounts out:
635 * --Leaves us in the correct state for the next step, whatever that might be.
636 * --If we find a symlink, returns with iocounts on both ni_vp and ni_dvp.
637 * --If we are to look up another component, then we have an iocount on ni_vp and
638 * nothing else.
639 * --If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags.
640 * --In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount
641 * was dropped).
642 */
fe8ab488 643static int
6d2010ae
A
644lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
645 int vbusyflags, int *keep_going, int nc_generation,
646 int wantparent, int atroot, vfs_context_t ctx)
647{
648 vnode_t dp;
649 int error;
650 char *cp;
651
652 dp = ndp->ni_vp;
653 *keep_going = 0;
654
655 if (ndp->ni_vp == NULLVP) {
656 panic("NULL ni_vp in %s\n", __FUNCTION__);
657 }
658
659 if (atroot) {
660 goto nextname;
661 }
662
6d2010ae
A
663 /*
664 * Take into account any additional components consumed by
665 * the underlying filesystem.
666 */
667 if (cnp->cn_consume > 0) {
668 cnp->cn_nameptr += cnp->cn_consume;
669 ndp->ni_next += cnp->cn_consume;
670 ndp->ni_pathlen -= cnp->cn_consume;
671 cnp->cn_consume = 0;
672 } else {
673 lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation);
674 }
675
676 /*
677 * Check to see if the vnode has been mounted on...
678 * if so find the root of the mounted file system.
679 * Updates ndp->ni_vp.
680 */
681 error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx);
682 dp = ndp->ni_vp;
683 if (error) {
684 goto out;
685 }
686
687#if CONFIG_MACF
688 if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
689 error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx);
690 if (error)
691 goto out;
692 }
693#endif
694
695 /*
696 * Check for symbolic link
697 */
698 if ((dp->v_type == VLNK) &&
699 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
700 cnp->cn_flags |= ISSYMLINK;
701 *keep_going = 1;
702 return (0);
703 }
704
705 /*
706 * Check for bogus trailing slashes.
707 */
708 if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
709 if (dp->v_type != VDIR) {
710 error = ENOTDIR;
711 goto out;
712 }
713 ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
714 }
4b17d6b6
A
715
716#if NAMEDSTREAMS
717 /*
718 * Deny namei/lookup requests to resolve paths that point to shadow files.
719 * Access to shadow files must be conducted by explicit calls to VNOP_LOOKUP
720 * directly, and not use lookup/namei
721 */
722 if (vnode_isshadow (dp)) {
723 error = ENOENT;
724 goto out;
725 }
726#endif
727
6d2010ae
A
728nextname:
729 /*
730 * Not a symbolic link. If more pathname,
731 * continue at next component, else return.
732 *
733 * Definitely have a dvp if there's another slash
734 */
735 if (*ndp->ni_next == '/') {
736 cnp->cn_nameptr = ndp->ni_next + 1;
737 ndp->ni_pathlen--;
738 while (*cnp->cn_nameptr == '/') {
739 cnp->cn_nameptr++;
740 ndp->ni_pathlen--;
741 }
742
743 cp = cnp->cn_nameptr;
744 vnode_put(ndp->ni_dvp);
745 ndp->ni_dvp = NULLVP;
746
747 if (*cp == '\0') {
748 goto emptyname;
749 }
750
751 *keep_going = 1;
752 return 0;
753 }
754
755 /*
756 * Disallow directory write attempts on read-only file systems.
757 */
758 if (rdonly &&
759 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
760 error = EROFS;
761 goto out;
762 }
763
764 /* If SAVESTART is set, we should have a dvp */
765 if (cnp->cn_flags & SAVESTART) {
766 /*
767 * note that we already hold a reference
768 * on both dp and ni_dvp, but for some reason
769 * can't get another one... in this case we
770 * need to do vnode_put on dp in 'bad2'
771 */
772 if ( (vnode_get(ndp->ni_dvp)) ) {
773 error = ENOENT;
774 goto out;
775 }
776 ndp->ni_startdir = ndp->ni_dvp;
777 }
778 if (!wantparent && ndp->ni_dvp) {
779 vnode_put(ndp->ni_dvp);
780 ndp->ni_dvp = NULLVP;
781 }
782
783 if (cnp->cn_flags & AUDITVNPATH1)
784 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
785 else if (cnp->cn_flags & AUDITVNPATH2)
786 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
787
788#if NAMEDRSRCFORK
789 /*
790 * Caller wants the resource fork.
791 */
792 if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
793 error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx);
794 if (error != 0)
795 goto out;
796
797 dp = ndp->ni_vp;
798 }
799#endif
800 if (kdebug_enable)
39236c6e 801 kdebug_lookup(ndp->ni_vp, cnp);
6d2010ae
A
802
803 return 0;
804
805emptyname:
806 error = lookup_handle_emptyname(ndp, cnp, wantparent);
807 if (error != 0)
808 goto out;
809
810 return 0;
811out:
812 return error;
813
814}
815
816/*
817 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
818 */
fe8ab488 819static int
6d2010ae
A
820lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent)
821{
822 vnode_t dp;
823 int error = 0;
824
825 dp = ndp->ni_vp;
826 cnp->cn_namelen = 0;
827 /*
828 * A degenerate name (e.g. / or "") which is a way of
829 * talking about a directory, e.g. like "/." or ".".
830 */
831 if (dp->v_type != VDIR) {
832 error = ENOTDIR;
833 goto out;
834 }
835 if (cnp->cn_nameiop != LOOKUP) {
836 error = EISDIR;
837 goto out;
838 }
839 if (wantparent) {
840 /*
841 * note that we already hold a reference
842 * on dp, but for some reason can't
843 * get another one... in this case we
844 * need to do vnode_put on dp in 'bad'
845 */
846 if ( (vnode_get(dp)) ) {
847 error = ENOENT;
848 goto out;
849 }
850 ndp->ni_dvp = dp;
851 }
852 cnp->cn_flags &= ~ISDOTDOT;
853 cnp->cn_flags |= ISLASTCN;
854 ndp->ni_next = cnp->cn_nameptr;
855 ndp->ni_vp = dp;
856
857 if (cnp->cn_flags & AUDITVNPATH1)
858 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
859 else if (cnp->cn_flags & AUDITVNPATH2)
860 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
861 if (cnp->cn_flags & SAVESTART)
862 panic("lookup: SAVESTART");
863
864 return 0;
865out:
866 return error;
867}
868/*
869 * Search a pathname.
870 * This is a very central and rather complicated routine.
871 *
872 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
873 * The starting directory is taken from ni_startdir. The pathname is
874 * descended until done, or a symbolic link is encountered. The variable
875 * ni_more is clear if the path is completed; it is set to one if a
876 * symbolic link needing interpretation is encountered.
877 *
878 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
879 * whether the name is to be looked up, created, renamed, or deleted.
880 * When CREATE, RENAME, or DELETE is specified, information usable in
881 * creating, renaming, or deleting a directory entry may be calculated.
882 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
883 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
884 * returned unlocked. Otherwise the parent directory is not returned. If
885 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
886 * the target is returned locked, otherwise it is returned unlocked.
887 * When creating or renaming and LOCKPARENT is specified, the target may not
888 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
889 *
890 * Overall outline of lookup:
891 *
892 * dirloop:
893 * identify next component of name at ndp->ni_ptr
894 * handle degenerate case where name is null string
895 * if .. and crossing mount points and on mounted filesys, find parent
896 * call VNOP_LOOKUP routine for next component name
897 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
898 * component vnode returned in ni_vp (if it exists), locked.
899 * if result vnode is mounted on and crossing mount points,
900 * find mounted on vnode
901 * if more components of name, do next level at dirloop
902 * return the answer in ni_vp, locked if LOCKLEAF set
903 * if LOCKPARENT set, return locked parent in ni_dvp
904 * if WANTPARENT set, return unlocked parent in ni_dvp
905 *
906 * Returns: 0 Success
907 * ENOENT No such file or directory
908 * EBADF Bad file descriptor
909 * ENOTDIR Not a directory
910 * EROFS Read-only file system [CREATE]
911 * EISDIR Is a directory [CREATE]
912 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
913 * vnode_authorize:EROFS
914 * vnode_authorize:EACCES
915 * vnode_authorize:EPERM
916 * vnode_authorize:???
917 * VNOP_LOOKUP:ENOENT No such file or directory
918 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
919 * VNOP_LOOKUP:???
920 * VFS_ROOT:ENOTSUP
921 * VFS_ROOT:ENOENT
922 * VFS_ROOT:???
923 */
924int
925lookup(struct nameidata *ndp)
926{
927 char *cp; /* pointer into pathname argument */
928 vnode_t tdp; /* saved dp */
929 vnode_t dp; /* the directory we are searching */
930 int docache = 1; /* == 0 do not cache last component */
931 int wantparent; /* 1 => wantparent or lockparent flag */
932 int rdonly; /* lookup read-only flag bit */
933 int dp_authorized = 0;
934 int error = 0;
935 struct componentname *cnp = &ndp->ni_cnd;
936 vfs_context_t ctx = cnp->cn_context;
937 int vbusyflags = 0;
938 int nc_generation = 0;
939 vnode_t last_dp = NULLVP;
940 int keep_going;
941 int atroot;
942
943 /*
944 * Setup: break out flag bits into variables.
945 */
fe8ab488
A
946 if (cnp->cn_flags & NOCACHE) {
947 docache = 0;
6d2010ae
A
948 }
949 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
950 rdonly = cnp->cn_flags & RDONLY;
951 cnp->cn_flags &= ~ISSYMLINK;
952 cnp->cn_consume = 0;
953
954 dp = ndp->ni_startdir;
955 ndp->ni_startdir = NULLVP;
956
957 if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0)
958 vbusyflags = LK_NOWAIT;
959 cp = cnp->cn_nameptr;
960
961 if (*cp == '\0') {
962 if ( (vnode_getwithref(dp)) ) {
963 dp = NULLVP;
964 error = ENOENT;
965 goto bad;
966 }
967 ndp->ni_vp = dp;
968 error = lookup_handle_emptyname(ndp, cnp, wantparent);
969 if (error) {
970 goto bad;
971 }
972
973 return 0;
974 }
975dirloop:
976 atroot = 0;
977 ndp->ni_vp = NULLVP;
978
979 if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp)) ) {
980 dp = NULLVP;
981 goto bad;
982 }
983 if ((cnp->cn_flags & ISLASTCN)) {
984 if (docache)
985 cnp->cn_flags |= MAKEENTRY;
986 } else
987 cnp->cn_flags |= MAKEENTRY;
988
989 dp = ndp->ni_dvp;
990
991 if (ndp->ni_vp != NULLVP) {
992 /*
993 * cache_lookup_path returned a non-NULL ni_vp then,
994 * we're guaranteed that the dp is a VDIR, it's
995 * been authorized, and vp is not ".."
996 *
997 * make sure we don't try to enter the name back into
998 * the cache if this vp is purged before we get to that
999 * check since we won't have serialized behind whatever
1000 * activity is occurring in the FS that caused the purge
1001 */
1002 if (dp != NULLVP)
1003 nc_generation = dp->v_nc_generation - 1;
1004
1005 goto returned_from_lookup_path;
9bccf70c 1006 }
1c79356b 1007
1c79356b
A
1008 /*
1009 * Handle "..": two special cases.
1010 * 1. If at root directory (e.g. after chroot)
1011 * or at absolute root directory
1012 * then ignore it so can't get out.
1013 * 2. If this vnode is the root of a mounted
1014 * filesystem, then replace it with the
1015 * vnode which was mounted on so we take the
1016 * .. in the other file system.
1017 */
91447636 1018 if ( (cnp->cn_flags & ISDOTDOT) ) {
39037602
A
1019 /*
1020 * if this is a chroot'ed process, check if the current
1021 * directory is still a subdirectory of the process's
1022 * root directory.
1023 */
1024 if (ndp->ni_rootdir && (ndp->ni_rootdir != rootvnode) &&
1025 dp != ndp->ni_rootdir) {
1026 int sdir_error;
1027 int is_subdir = FALSE;
1028
1029 sdir_error = vnode_issubdir(dp, ndp->ni_rootdir,
1030 &is_subdir, vfs_context_kernel());
1031
1032 /*
1033 * If we couldn't determine if dp is a subdirectory of
1034 * ndp->ni_rootdir (sdir_error != 0), we let the request
1035 * proceed.
1036 */
1037 if (!sdir_error && !is_subdir) {
1038 vnode_put(dp);
1039 dp = ndp->ni_rootdir;
1040 /*
1041 * There's a ref on the process's root directory
1042 * but we can't use vnode_getwithref here as
1043 * there is nothing preventing that ref being
1044 * released by another thread.
1045 */
1046 if (vnode_get(dp)) {
1047 error = ENOENT;
1048 goto bad;
1049 }
1050 }
1051 }
1052
1c79356b 1053 for (;;) {
91447636
A
1054 if (dp == ndp->ni_rootdir || dp == rootvnode) {
1055 ndp->ni_dvp = dp;
1c79356b 1056 ndp->ni_vp = dp;
91447636
A
1057 /*
1058 * we're pinned at the root
1059 * we've already got one reference on 'dp'
1060 * courtesy of cache_lookup_path... take
1061 * another one for the ".."
1062 * if we fail to get the new reference, we'll
1063 * drop our original down in 'bad'
1064 */
1065 if ( (vnode_get(dp)) ) {
1066 error = ENOENT;
1067 goto bad;
1068 }
6d2010ae
A
1069 atroot = 1;
1070 goto returned_from_lookup_path;
1c79356b
A
1071 }
1072 if ((dp->v_flag & VROOT) == 0 ||
1073 (cnp->cn_flags & NOCROSSMOUNT))
91447636 1074 break;
0b4e3aa0 1075 if (dp->v_mount == NULL) { /* forced umount */
91447636 1076 error = EBADF;
0b4e3aa0
A
1077 goto bad;
1078 }
1c79356b 1079 tdp = dp;
91447636
A
1080 dp = tdp->v_mount->mnt_vnodecovered;
1081
1082 vnode_put(tdp);
1083
1084 if ( (vnode_getwithref(dp)) ) {
1085 dp = NULLVP;
1086 error = ENOENT;
1087 goto bad;
1088 }
1089 ndp->ni_dvp = dp;
1090 dp_authorized = 0;
1c79356b
A
1091 }
1092 }
1093
1094 /*
1095 * We now have a segment name to search for, and a directory to search.
1096 */
1097unionlookup:
91447636
A
1098 ndp->ni_vp = NULLVP;
1099
1100 if (dp->v_type != VDIR) {
1101 error = ENOTDIR;
1102 goto lookup_error;
1103 }
2d21ac55 1104 if ( (cnp->cn_flags & DONOTAUTH) != DONOTAUTH ) {
6d2010ae
A
1105 error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
1106 if (error) {
2d21ac55 1107 goto lookup_error;
6d2010ae
A
1108 }
1109 }
1110
1111 /*
1112 * Now that we've authorized a lookup, can bail out if the filesystem
1113 * will be doing a batched operation. Return an iocount on dvp.
1114 */
1115#if NAMEDRSRCFORK
39236c6e 1116 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
6d2010ae
A
1117#else
1118 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp)) {
1119#endif /* NAMEDRSRCFORK */
1120 ndp->ni_flag |= NAMEI_UNFINISHED;
1121 ndp->ni_ncgeneration = dp->v_nc_generation;
1122 return 0;
91447636 1123 }
2d21ac55
A
1124
1125 nc_generation = dp->v_nc_generation;
1126
39236c6e
A
1127 /*
1128 * Note:
1129 * Filesystems that support hardlinks may want to call vnode_update_identity
1130 * if the lookup operation below will modify the in-core vnode to belong to a new point
1131 * in the namespace. VFS cannot infer whether or not the look up operation makes the vnode
1132 * name change or change parents. Without this, the lookup may make update
1133 * filesystem-specific in-core metadata but fail to update the v_parent or v_name
1134 * fields in the vnode. If VFS were to do this, it would be necessary to call
1135 * vnode_update_identity on every lookup operation -- expensive!
1136 *
1137 * However, even with this in place, multiple lookups may occur in between this lookup
1138 * and the subsequent vnop, so, at best, we could only guarantee that you would get a
1139 * valid path back, and not necessarily the one that you wanted.
1140 *
1141 * Example:
1142 * /tmp/a == /foo/b
1143 *
1144 * If you are now looking up /foo/b and the vnode for this link represents /tmp/a,
1145 * vnode_update_identity will fix the parentage so that you can get /foo/b back
1146 * through the v_parent chain (preventing you from getting /tmp/b back). It would
1147 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1148 */
6d2010ae 1149
39236c6e 1150 error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
6d2010ae
A
1151
1152 if ( error ) {
91447636 1153lookup_error:
1c79356b 1154 if ((error == ENOENT) &&
39236c6e 1155 (dp->v_mount != NULL) &&
1c79356b
A
1156 (dp->v_mount->mnt_flag & MNT_UNION)) {
1157 tdp = dp;
39236c6e 1158 error = lookup_traverse_union(tdp, &dp, ctx);
91447636 1159 vnode_put(tdp);
39236c6e 1160 if (error) {
91447636 1161 dp = NULLVP;
91447636
A
1162 goto bad;
1163 }
39236c6e 1164
91447636
A
1165 ndp->ni_dvp = dp;
1166 dp_authorized = 0;
1c79356b
A
1167 goto unionlookup;
1168 }
1169
1170 if (error != EJUSTRETURN)
1171 goto bad;
91447636
A
1172
1173 if (ndp->ni_vp != NULLVP)
1174 panic("leaf should be empty");
1175
39236c6e
A
1176#if NAMEDRSRCFORK
1177 /*
1178 * At this point, error should be EJUSTRETURN.
1179 *
1180 * If CN_WANTSRSRCFORK is set, that implies that the
1181 * underlying filesystem could not find the "parent" of the
1182 * resource fork (the data fork), and we are doing a lookup
1183 * for a CREATE event.
1184 *
1185 * However, this should be converted to an error, as the
1186 * failure to find this parent should disallow further
1187 * progress to try and acquire a resource fork vnode.
1188 */
1189 if (cnp->cn_flags & CN_WANTSRSRCFORK) {
1190 error = ENOENT;
1191 goto bad;
1192 }
1193#endif
1194
6d2010ae
A
1195 error = lookup_validate_creation_path(ndp);
1196 if (error)
9bccf70c 1197 goto bad;
1c79356b
A
1198 /*
1199 * We return with ni_vp NULL to indicate that the entry
1200 * doesn't currently exist, leaving a pointer to the
91447636 1201 * referenced directory vnode in ndp->ni_dvp.
1c79356b
A
1202 */
1203 if (cnp->cn_flags & SAVESTART) {
91447636
A
1204 if ( (vnode_get(ndp->ni_dvp)) ) {
1205 error = ENOENT;
1206 goto bad;
1207 }
1c79356b 1208 ndp->ni_startdir = ndp->ni_dvp;
1c79356b 1209 }
91447636
A
1210 if (!wantparent)
1211 vnode_put(ndp->ni_dvp);
1212
1c79356b
A
1213 if (kdebug_enable)
1214 kdebug_lookup(ndp->ni_dvp, cnp);
1215 return (0);
1216 }
91447636 1217returned_from_lookup_path:
6d2010ae
A
1218 /* We'll always have an iocount on ni_vp when this finishes. */
1219 error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx);
1220 if (error != 0) {
1221 goto bad2;
1c79356b
A
1222 }
1223
6d2010ae
A
1224 if (keep_going) {
1225 dp = ndp->ni_vp;
2d21ac55 1226
6d2010ae
A
1227 /* namei() will handle symlinks */
1228 if ((dp->v_type == VLNK) &&
1229 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
1230 return 0;
2d21ac55 1231 }
1c79356b 1232
6d2010ae
A
1233 /*
1234 * Otherwise, there's more path to process.
1235 * cache_lookup_path is now responsible for dropping io ref on dp
1236 * when it is called again in the dirloop. This ensures we hold
1237 * a ref on dp until we complete the next round of lookup.
91447636 1238 */
6d2010ae
A
1239 last_dp = dp;
1240
1241 goto dirloop;
55e303ae 1242 }
91447636 1243
55e303ae 1244 return (0);
1c79356b 1245bad2:
91447636 1246 if (ndp->ni_dvp)
6d2010ae
A
1247 vnode_put(ndp->ni_dvp);
1248
1249 vnode_put(ndp->ni_vp);
91447636
A
1250 ndp->ni_vp = NULLVP;
1251
1252 if (kdebug_enable)
1253 kdebug_lookup(dp, cnp);
1254 return (error);
1255
1c79356b 1256bad:
91447636
A
1257 if (dp)
1258 vnode_put(dp);
1259 ndp->ni_vp = NULLVP;
1260
1c79356b
A
1261 if (kdebug_enable)
1262 kdebug_lookup(dp, cnp);
1263 return (error);
1264}
1265
39236c6e
A
1266/*
1267 * Given a vnode in a union mount, traverse to the equivalent
1268 * vnode in the underlying mount.
1269 */
1270int
1271lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx)
1272{
1273 char *path = NULL, *pp;
1274 const char *name, *np;
1275 int len;
1276 int error = 0;
1277 struct nameidata nd;
1278 vnode_t vp = dvp;
1279
1280 *new_dvp = NULL;
1281
1282 if (vp && vp->v_flag & VROOT) {
1283 *new_dvp = vp->v_mount->mnt_vnodecovered;
1284 if (vnode_getwithref(*new_dvp))
1285 return ENOENT;
1286 return 0;
1287 }
1288
1289 path = (char *) kalloc(MAXPATHLEN);
1290 if (path == NULL) {
1291 error = ENOMEM;
1292 goto done;
1293 }
1294
1295 /*
1296 * Walk back up to the mountpoint following the
1297 * v_parent chain and build a slash-separated path.
1298 * Then lookup that path starting with the covered vnode.
1299 */
1300 pp = path + (MAXPATHLEN - 1);
1301 *pp = '\0';
1302
1303 while (1) {
1304 name = vnode_getname(vp);
1305 if (name == NULL) {
1306 printf("lookup_traverse_union: null parent name: .%s\n", pp);
1307 error = ENOENT;
1308 goto done;
1309 }
1310 len = strlen(name);
1311 if ((len + 1) > (pp - path)) { // Enough space for this name ?
1312 error = ENAMETOOLONG;
1313 vnode_putname(name);
1314 goto done;
1315 }
1316 for (np = name + len; len > 0; len--) // Copy name backwards
1317 *--pp = *--np;
1318 vnode_putname(name);
1319 vp = vp->v_parent;
1320 if (vp == NULLVP || vp->v_flag & VROOT)
1321 break;
1322 *--pp = '/';
1323 }
1324
1325 /* Evaluate the path in the underlying mount */
1326 NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(pp), ctx);
1327 nd.ni_dvp = dvp->v_mount->mnt_vnodecovered;
1328 error = namei(&nd);
1329 if (error == 0)
1330 *new_dvp = nd.ni_vp;
1331 nameidone(&nd);
1332done:
1333 if (path)
1334 kfree(path, MAXPATHLEN);
1335 return error;
1336}
1337
6d2010ae
A
1338int
1339lookup_validate_creation_path(struct nameidata *ndp)
1340{
1341 struct componentname *cnp = &ndp->ni_cnd;
1342
1343 /*
1344 * If creating and at end of pathname, then can consider
1345 * allowing file to be created.
1346 */
1347 if (cnp->cn_flags & RDONLY) {
1348 return EROFS;
1349 }
1350 if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) {
1351 return ENOENT;
1352 }
1353
1354 return 0;
1355}
1356
1357/*
1358 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1359 */
fe8ab488 1360static int
6d2010ae
A
1361lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1362 int vbusyflags, vfs_context_t ctx)
1363{
1364 mount_t mp;
1365 vnode_t tdp;
1366 int error = 0;
6d2010ae 1367 uint32_t depth = 0;
6d2010ae
A
1368 vnode_t mounted_on_dp;
1369 int current_mount_generation = 0;
3e170ce0
A
1370#if CONFIG_TRIGGERS
1371 vnode_t triggered_dp = NULLVP;
1372 int retry_cnt = 0;
1373#define MAX_TRIGGER_RETRIES 1
1374#endif
6d2010ae 1375
3e170ce0
A
1376 if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT)
1377 return 0;
39236c6e 1378
3e170ce0 1379 mounted_on_dp = dp;
6d2010ae 1380#if CONFIG_TRIGGERS
3e170ce0 1381restart:
6d2010ae 1382#endif
3e170ce0 1383 current_mount_generation = mount_generation;
6d2010ae 1384
3e170ce0
A
1385 while (dp->v_mountedhere) {
1386 vnode_lock_spin(dp);
1387 if ((mp = dp->v_mountedhere)) {
6d2010ae
A
1388 mp->mnt_crossref++;
1389 vnode_unlock(dp);
3e170ce0
A
1390 } else {
1391 vnode_unlock(dp);
1392 break;
1393 }
6d2010ae 1394
3e170ce0
A
1395 if (ISSET(mp->mnt_lflag, MNT_LFORCE)) {
1396 mount_dropcrossref(mp, dp, 0);
1397 break; // don't traverse into a forced unmount
1398 }
6d2010ae 1399
6d2010ae 1400
3e170ce0 1401 if (vfs_busy(mp, vbusyflags)) {
6d2010ae 1402 mount_dropcrossref(mp, dp, 0);
3e170ce0
A
1403 if (vbusyflags == LK_NOWAIT) {
1404 error = ENOENT;
6d2010ae
A
1405 goto out;
1406 }
1407
3e170ce0
A
1408 continue;
1409 }
6d2010ae 1410
3e170ce0 1411 error = VFS_ROOT(mp, &tdp, ctx);
6d2010ae 1412
3e170ce0
A
1413 mount_dropcrossref(mp, dp, 0);
1414 vfs_unbusy(mp);
1415
1416 if (error) {
1417 goto out;
1418 }
1419
1420 vnode_put(dp);
1421 ndp->ni_vp = dp = tdp;
1422 if (dp->v_type != VDIR) {
1423#if DEVELOPMENT || DEBUG
1424 panic("%s : Root of filesystem not a directory\n",
1425 __FUNCTION__);
1426#else
6d2010ae 1427 break;
3e170ce0 1428#endif
6d2010ae 1429 }
3e170ce0 1430 depth++;
6d2010ae
A
1431 }
1432
3e170ce0
A
1433#if CONFIG_TRIGGERS
1434 /*
1435 * The triggered_dp check here is required but is susceptible to a
1436 * (unlikely) race in which trigger mount is done from here and is
1437 * unmounted before we get past vfs_busy above. We retry to deal with
1438 * that case but it has the side effect of unwanted retries for
1439 * "special" processes which don't want to trigger mounts.
1440 */
1441 if (dp->v_resolve && retry_cnt < MAX_TRIGGER_RETRIES) {
1442 error = vnode_trigger_resolve(dp, ndp, ctx);
1443 if (error)
1444 goto out;
1445 if (dp == triggered_dp)
1446 retry_cnt += 1;
1447 else
1448 retry_cnt = 0;
1449 triggered_dp = dp;
1450 goto restart;
1451 }
1452#endif /* CONFIG_TRIGGERS */
1453
fe8ab488 1454 if (depth) {
6d2010ae
A
1455 mp = mounted_on_dp->v_mountedhere;
1456
1457 if (mp) {
1458 mount_lock_spin(mp);
1459 mp->mnt_realrootvp_vid = dp->v_id;
1460 mp->mnt_realrootvp = dp;
1461 mp->mnt_generation = current_mount_generation;
1462 mount_unlock(mp);
1463 }
1464 }
1465
1466 return 0;
1467
1468out:
1469 return error;
1470}
1471
1472/*
1473 * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location
1474 * at which to start a lookup with a resolved path, and all other iocounts dropped.
1475 */
fe8ab488 1476static int
6d2010ae
A
1477lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx)
1478{
1479 int error;
1480 char *cp; /* pointer into pathname argument */
1481 uio_t auio;
39037602
A
1482 union {
1483 union {
1484 struct user_iovec s_uiovec;
1485 struct kern_iovec s_kiovec;
1486 } u_iovec;
1487 struct uio s_uio;
1488 char uio_buf[ UIO_SIZEOF(1) ];
1489 } u_uio_buf; /* union only for aligning uio_buf correctly */
6d2010ae
A
1490 int need_newpathbuf;
1491 u_int linklen;
1492 struct componentname *cnp = &ndp->ni_cnd;
1493 vnode_t dp;
1494 char *tmppn;
1495
6d2010ae
A
1496 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1497 return ELOOP;
1498 }
1499#if CONFIG_MACF
1500 if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0)
1501 return error;
1502#endif /* MAC */
1503 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF))
1504 need_newpathbuf = 1;
1505 else
1506 need_newpathbuf = 0;
1507
1508 if (need_newpathbuf) {
1509 MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1510 if (cp == NULL) {
1511 return ENOMEM;
1512 }
1513 } else {
1514 cp = cnp->cn_pnbuf;
1515 }
39037602
A
1516 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
1517 &u_uio_buf.uio_buf[0], sizeof(u_uio_buf.uio_buf));
6d2010ae
A
1518
1519 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1520
1521 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
1522 if (error) {
1523 if (need_newpathbuf)
1524 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1525 return error;
1526 }
1527
1528 /*
1529 * Safe to set unsigned with a [larger] signed type here
1530 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1531 * is only 1024.
1532 */
1533 linklen = MAXPATHLEN - (u_int)uio_resid(auio);
1534 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
1535 if (need_newpathbuf)
1536 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1537
1538 return ENAMETOOLONG;
1539 }
1540 if (need_newpathbuf) {
1541 long len = cnp->cn_pnlen;
1542
1543 tmppn = cnp->cn_pnbuf;
1544 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1545 cnp->cn_pnbuf = cp;
1546 cnp->cn_pnlen = MAXPATHLEN;
1547
1548 if ( (cnp->cn_flags & HASBUF) )
1549 FREE_ZONE(tmppn, len, M_NAMEI);
1550 else
1551 cnp->cn_flags |= HASBUF;
1552 } else
1553 cnp->cn_pnbuf[linklen] = '\0';
1554
1555 ndp->ni_pathlen += linklen;
1556 cnp->cn_nameptr = cnp->cn_pnbuf;
1557
1558 /*
1559 * starting point for 'relative'
1560 * symbolic link path
1561 */
1562 dp = ndp->ni_dvp;
1563
1564 /*
1565 * get rid of references returned via 'lookup'
1566 */
1567 vnode_put(ndp->ni_vp);
1568 vnode_put(ndp->ni_dvp); /* ALWAYS have a dvp for a symlink */
1569
1570 ndp->ni_vp = NULLVP;
1571 ndp->ni_dvp = NULLVP;
1572
1573 /*
1574 * Check if symbolic link restarts us at the root
1575 */
1576 if (*(cnp->cn_nameptr) == '/') {
1577 while (*(cnp->cn_nameptr) == '/') {
1578 cnp->cn_nameptr++;
1579 ndp->ni_pathlen--;
1580 }
1581 if ((dp = ndp->ni_rootdir) == NULLVP) {
1582 return ENOENT;
1583 }
1584 }
1585
1586 *new_dp = dp;
1587
1588 return 0;
1589}
1590
1c79356b
A
1591/*
1592 * relookup - lookup a path name component
1593 * Used by lookup to re-aquire things.
1594 */
1595int
2d21ac55 1596relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1c79356b 1597{
2d21ac55 1598 struct vnode *dp = NULL; /* the directory we are searching */
1c79356b
A
1599 int wantparent; /* 1 => wantparent or lockparent flag */
1600 int rdonly; /* lookup read-only flag bit */
1601 int error = 0;
1602#ifdef NAMEI_DIAGNOSTIC
55e303ae 1603 int i, newhash; /* DEBUG: check name hash */
1c79356b
A
1604 char *cp; /* DEBUG: check name ptr/len */
1605#endif
91447636 1606 vfs_context_t ctx = cnp->cn_context;;
1c79356b
A
1607
1608 /*
1609 * Setup: break out flag bits into variables.
1610 */
1611 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1c79356b
A
1612 rdonly = cnp->cn_flags & RDONLY;
1613 cnp->cn_flags &= ~ISSYMLINK;
1c79356b 1614
91447636
A
1615 if (cnp->cn_flags & NOCACHE)
1616 cnp->cn_flags &= ~MAKEENTRY;
1617 else
1618 cnp->cn_flags |= MAKEENTRY;
1619
1620 dp = dvp;
1c79356b
A
1621
1622 /*
1623 * Check for degenerate name (e.g. / or "")
1624 * which is a way of talking about a directory,
1625 * e.g. like "/." or ".".
1626 */
1627 if (cnp->cn_nameptr[0] == '\0') {
1628 if (cnp->cn_nameiop != LOOKUP || wantparent) {
1629 error = EISDIR;
1630 goto bad;
1631 }
1632 if (dp->v_type != VDIR) {
1633 error = ENOTDIR;
1634 goto bad;
1635 }
91447636
A
1636 if ( (vnode_get(dp)) ) {
1637 error = ENOENT;
1638 goto bad;
1639 }
1c79356b 1640 *vpp = dp;
91447636 1641
1c79356b
A
1642 if (cnp->cn_flags & SAVESTART)
1643 panic("lookup: SAVESTART");
1644 return (0);
1645 }
1c79356b
A
1646 /*
1647 * We now have a segment name to search for, and a directory to search.
1648 */
91447636
A
1649 if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) {
1650 if (error != EJUSTRETURN)
1651 goto bad;
1c79356b
A
1652#if DIAGNOSTIC
1653 if (*vpp != NULL)
1654 panic("leaf should be empty");
1655#endif
1c79356b
A
1656 /*
1657 * If creating and at end of pathname, then can consider
1658 * allowing file to be created.
1659 */
1660 if (rdonly) {
1661 error = EROFS;
1662 goto bad;
1663 }
1c79356b
A
1664 /*
1665 * We return with ni_vp NULL to indicate that the entry
1666 * doesn't currently exist, leaving a pointer to the
1667 * (possibly locked) directory inode in ndp->ni_dvp.
1668 */
1669 return (0);
1670 }
1671 dp = *vpp;
1672
1673#if DIAGNOSTIC
1674 /*
1675 * Check for symbolic link
1676 */
1677 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1678 panic ("relookup: symlink found.\n");
1679#endif
1680
1681 /*
1682 * Disallow directory write attempts on read-only file systems.
1683 */
1684 if (rdonly &&
1685 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1686 error = EROFS;
1687 goto bad2;
1688 }
1689 /* ASSERT(dvp == ndp->ni_startdir) */
1c79356b 1690
1c79356b
A
1691 return (0);
1692
1693bad2:
91447636
A
1694 vnode_put(dp);
1695bad:
1c79356b 1696 *vpp = NULL;
91447636 1697
1c79356b
A
1698 return (error);
1699}
1700
6d2010ae
A
1701/*
1702 * Free pathname buffer
1703 */
1704void
1705nameidone(struct nameidata *ndp)
1706{
91447636
A
1707 if (ndp->ni_cnd.cn_flags & HASBUF) {
1708 char *tmp = ndp->ni_cnd.cn_pnbuf;
1709
1710 ndp->ni_cnd.cn_pnbuf = NULL;
1711 ndp->ni_cnd.cn_flags &= ~HASBUF;
1712 FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI);
1713 }
1714}
1715
1c79356b 1716
2d21ac55
A
1717/*
1718 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1719 * by fs_usage. The path up to and including the current component name are
1720 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1721 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1722 * That is, the truncation removes the leading portion of the path.
1723 *
1724 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1725 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1726 * (in addition to DBG_FUNC_START if it is also the first). There may be
1727 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1728 *
1729 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1730 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1731 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1732 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1733 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1734 * path) is set to zero bytes, or '>' if there is more path beyond the
1735 * current component name (usually because an intermediate component was not
1736 * found).
1737 *
1738 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1739 * 12+N*16, there will be no padding.
1740 *
1741 * TODO: If there is more path beyond the current component name, should we
1742 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1743 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1744 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1745 */
316670eb 1746#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
39236c6e
A
1747
1748void
1749kdebug_lookup_gen_events(long *dbg_parms, int dbg_namelen, void *dp, boolean_t lookup)
1c79356b 1750{
2d21ac55 1751 int code;
39236c6e
A
1752 unsigned int i;
1753
1754 /*
1755 * In the event that we collect multiple, consecutive pathname
1756 * entries, we must mark the start of the path's string and the end.
1757 */
1758 if (lookup == TRUE)
04b8595b 1759 code = VFS_LOOKUP | DBG_FUNC_START;
39236c6e 1760 else
04b8595b 1761 code = VFS_LOOKUP_DONE | DBG_FUNC_START;
39236c6e
A
1762
1763 if (dbg_namelen <= (int)(3 * sizeof(long)))
1764 code |= DBG_FUNC_END;
1765
39037602 1766 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, kdebug_vnode(dp), dbg_parms[0], dbg_parms[1], dbg_parms[2], 0);
39236c6e
A
1767
1768 code &= ~DBG_FUNC_START;
1769
1770 for (i=3, dbg_namelen -= (3 * sizeof(long)); dbg_namelen > 0; i+=4, dbg_namelen -= (4 * sizeof(long))) {
1771 if (dbg_namelen <= (int)(4 * sizeof(long)))
1772 code |= DBG_FUNC_END;
1773
1774 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0);
1775 }
1776}
1777
1778static void
1779kdebug_lookup(vnode_t dp, struct componentname *cnp)
1780{
2d21ac55
A
1781 int dbg_namelen;
1782 char *dbg_nameptr;
1c79356b 1783 long dbg_parms[NUMPARMS];
1c79356b
A
1784
1785 /* Collect the pathname for tracing */
1786 dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen;
1787 dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen;
1788
2d21ac55
A
1789 if (dbg_namelen > (int)sizeof(dbg_parms))
1790 dbg_namelen = sizeof(dbg_parms);
1c79356b 1791 dbg_nameptr -= dbg_namelen;
2d21ac55
A
1792
1793 /* Copy the (possibly truncated) path itself */
1794 memcpy(dbg_parms, dbg_nameptr, dbg_namelen);
1795
1796 /* Pad with '\0' or '>' */
1797 if (dbg_namelen < (int)sizeof(dbg_parms)) {
1798 memset((char *)dbg_parms + dbg_namelen,
1799 *(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0,
1800 sizeof(dbg_parms) - dbg_namelen);
1c79356b 1801 }
39236c6e
A
1802 kdebug_lookup_gen_events(dbg_parms, dbg_namelen, (void *)dp, TRUE);
1803}
0c530ab8 1804
39236c6e 1805#else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
0c530ab8 1806
39236c6e
A
1807void
1808kdebug_lookup_gen_events(long *dbg_parms __unused, int dbg_namelen __unused, void *dp __unused)
1809{
2d21ac55 1810}
39236c6e 1811
6d2010ae
A
1812static void
1813kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
1814{
1815}
316670eb 1816#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
6d2010ae
A
1817
1818int
1819vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx)
1820{
1821 mount_t mp;
1822 int error;
1823
1824 mp = mount_lookupby_volfsid(fsid->val[0], 1);
1825 if (mp == NULL) {
1826 return EINVAL;
1827 }
1828
1829 /* Get the target vnode. */
1830 if (ino == 2) {
1831 error = VFS_ROOT(mp, vpp, ctx);
1832 } else {
1833 error = VFS_VGET(mp, ino, vpp, ctx);
1834 }
0c530ab8 1835
6d2010ae
A
1836 vfs_unbusy(mp);
1837 return error;
1838}
2d21ac55
A
1839/*
1840 * Obtain the real path from a legacy volfs style path.
1841 *
1842 * Valid formats of input path:
1843 *
1844 * "555/@"
1845 * "555/2"
1846 * "555/123456"
1847 * "555/123456/foobar"
1848 *
1849 * Where:
1850 * 555 represents the volfs file system id
1851 * '@' and '2' are aliases to the root of a file system
1852 * 123456 represents a file id
1853 * "foobar" represents a file name
1854 */
1855#if CONFIG_VOLFS
1856static int
1857vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
1858{
1859 vnode_t vp;
1860 struct mount *mp = NULL;
1861 char *str;
1862 char ch;
b0d623f7 1863 uint32_t id;
2d21ac55
A
1864 ino64_t ino;
1865 int error;
1866 int length;
1867
1868 /* Get file system id and move str to next component. */
1869 id = strtoul(path, &str, 10);
1870 if (id == 0 || str[0] != '/') {
1871 return (EINVAL);
1872 }
1873 while (*str == '/') {
1874 str++;
0c530ab8 1875 }
2d21ac55
A
1876 ch = *str;
1877
1878 mp = mount_lookupby_volfsid(id, 1);
1879 if (mp == NULL) {
1880 return (EINVAL); /* unexpected failure */
1881 }
1882 /* Check for an alias to a file system root. */
1883 if (ch == '@' && str[1] == '\0') {
1884 ino = 2;
1885 str++;
1886 } else {
1887 /* Get file id and move str to next component. */
1888 ino = strtouq(str, &str, 10);
1889 }
1890
1891 /* Get the target vnode. */
1892 if (ino == 2) {
1893 error = VFS_ROOT(mp, &vp, ctx);
1894 } else {
1895 error = VFS_VGET(mp, ino, &vp, ctx);
1896 }
1897 vfs_unbusy(mp);
1898 if (error) {
1899 goto out;
1900 }
1901 realpath[0] = '\0';
1902
1903 /* Get the absolute path to this vnode. */
1904 error = build_path(vp, realpath, bufsize, &length, 0, ctx);
1905 vnode_put(vp);
1906
1907 if (error == 0 && *str != '\0') {
1908 int attempt = strlcat(realpath, str, MAXPATHLEN);
1909 if (attempt > MAXPATHLEN){
1910 error = ENAMETOOLONG;
1911 }
1912 }
1913out:
1914 return (error);
1c79356b 1915}
2d21ac55 1916#endif
6d2010ae
A
1917
1918void
1919lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create)
1920{
1921 if (error == 0 && vp == NULLVP) {
1922 panic("NULL vp with error == 0.\n");
1923 }
1924
1925 /*
1926 * We don't want to do any of this if we didn't use the compound vnop
1927 * to perform the lookup... i.e. if we're allowing and using the legacy pattern,
1928 * where we did a full lookup.
1929 */
1930 if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) {
1931 return;
1932 }
1933
1934 /*
1935 * If we're going to continue the lookup, we'll handle
1936 * all lookup-related updates at that time.
1937 */
1938 if (error == EKEEPLOOKING) {
1939 return;
1940 }
1941
1942 /*
1943 * Only audit or update cache for *found* vnodes. For creation
1944 * neither would happen in the non-compound-vnop case.
1945 */
1946 if ((vp != NULLVP) && !did_create) {
1947 /*
1948 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
1949 * then we certainly don't want to update cache or identity.
1950 */
1951 if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) {
1952 lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration);
1953 }
1954 if (ndp->ni_cnd.cn_flags & AUDITVNPATH1)
1955 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
1956 else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2)
1957 AUDIT_ARG(vnpath, vp, ARG_VNODE2);
1958 }
1959
1960 /*
1961 * If you created (whether you opened or not), cut a lookup tracepoint
1962 * for the parent dir (as would happen without a compound vnop). Note: we may need
1963 * a vnode despite failure in this case!
1964 *
1965 * If you did not create:
1966 * Found child (succeeded or not): cut a tracepoint for the child.
1967 * Did not find child: cut a tracepoint with the parent.
1968 */
1969 if (kdebug_enable) {
1970 kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
1971 }
1972}