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