]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_lookup.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
1 /*
2 * Copyright (c) 2000-2007 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
92 #include <bsm/audit_kernel.h>
93
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 static void kdebug_lookup(struct vnode *dp, struct componentname *cnp);
109
110 #if CONFIG_VOLFS
111 static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
112 #endif
113
114 /*
115 * Convert a pathname into a pointer to a locked inode.
116 *
117 * The FOLLOW flag is set when symbolic links are to be followed
118 * when they occur at the end of the name translation process.
119 * Symbolic links are always followed for all other pathname
120 * components other than the last.
121 *
122 * The segflg defines whether the name is to be copied from user
123 * space or kernel space.
124 *
125 * Overall outline of namei:
126 *
127 * copy in name
128 * get starting directory
129 * while (!done && !error) {
130 * call lookup to search path.
131 * if symbolic link, massage name in buffer and continue
132 * }
133 *
134 * Returns: 0 Success
135 * ENOENT No such file or directory
136 * ELOOP Too many levels of symbolic links
137 * ENAMETOOLONG Filename too long
138 * copyinstr:EFAULT Bad address
139 * copyinstr:ENAMETOOLONG Filename too long
140 * lookup:EBADF Bad file descriptor
141 * lookup:EROFS
142 * lookup:EACCES
143 * lookup:EPERM
144 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
145 * This means we should re-drive lookup from this point.
146 * lookup: ???
147 * VNOP_READLINK:???
148 */
149 int
150 namei(struct nameidata *ndp)
151 {
152 struct filedesc *fdp; /* pointer to file descriptor state */
153 char *cp; /* pointer into pathname argument */
154 struct vnode *dp; /* the directory we are searching */
155 struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to
156 heavy vnode pressure */
157 u_long cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
158 uio_t auio;
159 int error;
160 struct componentname *cnp = &ndp->ni_cnd;
161 vfs_context_t ctx = cnp->cn_context;
162 proc_t p = vfs_context_proc(ctx);
163 /* XXX ut should be from context */
164 uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
165 char *tmppn;
166 char uio_buf[ UIO_SIZEOF(1) ];
167
168 #if DIAGNOSTIC
169 if (!vfs_context_ucred(ctx) || !p)
170 panic ("namei: bad cred/proc");
171 if (cnp->cn_nameiop & (~OPMASK))
172 panic ("namei: nameiop contaminated with flags");
173 if (cnp->cn_flags & OPMASK)
174 panic ("namei: flags contaminated with nameiops");
175 #endif
176 fdp = p->p_fd;
177
178 vnode_recycled:
179
180 /*
181 * Get a buffer for the name to be translated, and copy the
182 * name into the buffer.
183 */
184 if ((cnp->cn_flags & HASBUF) == 0) {
185 cnp->cn_pnbuf = ndp->ni_pathbuf;
186 cnp->cn_pnlen = PATHBUFLEN;
187 }
188 #if LP64_DEBUG
189 if (IS_VALID_UIO_SEGFLG(ndp->ni_segflg) == 0) {
190 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
191 }
192 #endif /* LP64_DEBUG */
193
194 retry_copy:
195 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
196 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
197 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
198 } else {
199 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
200 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
201 }
202 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
203 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
204 if (cnp->cn_pnbuf == NULL) {
205 error = ENOMEM;
206 goto error_out;
207 }
208
209 cnp->cn_flags |= HASBUF;
210 cnp->cn_pnlen = MAXPATHLEN;
211
212 goto retry_copy;
213 }
214 if (error)
215 goto error_out;
216
217 #if CONFIG_VOLFS
218 /*
219 * Check for legacy volfs style pathnames.
220 *
221 * For compatibility reasons we currently allow these paths,
222 * but future versions of the OS may not support them.
223 */
224 if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN &&
225 cnp->cn_pnbuf[0] == '/' &&
226 cnp->cn_pnbuf[1] == '.' &&
227 cnp->cn_pnbuf[2] == 'v' &&
228 cnp->cn_pnbuf[3] == 'o' &&
229 cnp->cn_pnbuf[4] == 'l' &&
230 cnp->cn_pnbuf[5] == '/' ) {
231 char * realpath;
232 int realpath_err;
233 /* Attempt to resolve a legacy volfs style pathname. */
234 MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
235 if (realpath) {
236 if ((realpath_err= vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) {
237 FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI);
238 if (realpath_err == ENOSPC){
239 error = ENAMETOOLONG;
240 goto error_out;
241 }
242 } else {
243 if (cnp->cn_flags & HASBUF) {
244 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
245 }
246 cnp->cn_pnbuf = realpath;
247 cnp->cn_pnlen = MAXPATHLEN;
248 ndp->ni_pathlen = strlen(realpath) + 1;
249 cnp->cn_flags |= HASBUF | CN_VOLFSPATH;
250 }
251 }
252 }
253 #endif /* CONFIG_VOLFS */
254
255 /* If we are auditing the kernel pathname, save the user pathname */
256 if (cnp->cn_flags & AUDITVNPATH1)
257 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
258 if (cnp->cn_flags & AUDITVNPATH2)
259 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
260
261 /*
262 * Do not allow empty pathnames
263 */
264 if (*cnp->cn_pnbuf == '\0') {
265 error = ENOENT;
266 goto error_out;
267 }
268 ndp->ni_loopcnt = 0;
269
270 /*
271 * determine the starting point for the translation.
272 */
273 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) {
274 if ( !(fdp->fd_flags & FD_CHROOT))
275 ndp->ni_rootdir = rootvnode;
276 }
277 cnp->cn_nameptr = cnp->cn_pnbuf;
278
279 ndp->ni_usedvp = NULLVP;
280
281 if (*(cnp->cn_nameptr) == '/') {
282 while (*(cnp->cn_nameptr) == '/') {
283 cnp->cn_nameptr++;
284 ndp->ni_pathlen--;
285 }
286 dp = ndp->ni_rootdir;
287 } else if (cnp->cn_flags & USEDVP) {
288 dp = ndp->ni_dvp;
289 ndp->ni_usedvp = dp;
290 } else
291 dp = vfs_context_cwd(ctx);
292
293 if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
294 error = ENOENT;
295 goto error_out;
296 }
297 ndp->ni_dvp = NULLVP;
298 ndp->ni_vp = NULLVP;
299
300 for (;;) {
301 int need_newpathbuf;
302 int linklen;
303
304 ndp->ni_startdir = dp;
305
306 if ( (error = lookup(ndp)) ) {
307 goto error_out;
308 }
309 /*
310 * Check for symbolic link
311 */
312 if ((cnp->cn_flags & ISSYMLINK) == 0) {
313 return (0);
314 }
315 if ((cnp->cn_flags & FSNODELOCKHELD)) {
316 cnp->cn_flags &= ~FSNODELOCKHELD;
317 unlock_fsnode(ndp->ni_dvp, NULL);
318 }
319 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
320 error = ELOOP;
321 break;
322 }
323 #if CONFIG_MACF
324 if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0)
325 break;
326 #endif /* MAC */
327 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF))
328 need_newpathbuf = 1;
329 else
330 need_newpathbuf = 0;
331
332 if (need_newpathbuf) {
333 MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
334 if (cp == NULL) {
335 error = ENOMEM;
336 break;
337 }
338 } else {
339 cp = cnp->cn_pnbuf;
340 }
341 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf));
342
343 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
344
345 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
346 if (error) {
347 if (need_newpathbuf)
348 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
349 break;
350 }
351 // LP64todo - fix this
352 linklen = MAXPATHLEN - uio_resid(auio);
353 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
354 if (need_newpathbuf)
355 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
356
357 error = ENAMETOOLONG;
358 break;
359 }
360 if (need_newpathbuf) {
361 long len = cnp->cn_pnlen;
362
363 tmppn = cnp->cn_pnbuf;
364 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
365 cnp->cn_pnbuf = cp;
366 cnp->cn_pnlen = MAXPATHLEN;
367
368 if ( (cnp->cn_flags & HASBUF) )
369 FREE_ZONE(tmppn, len, M_NAMEI);
370 else
371 cnp->cn_flags |= HASBUF;
372 } else
373 cnp->cn_pnbuf[linklen] = '\0';
374
375 ndp->ni_pathlen += linklen;
376 cnp->cn_nameptr = cnp->cn_pnbuf;
377
378 /*
379 * starting point for 'relative'
380 * symbolic link path
381 */
382 dp = ndp->ni_dvp;
383 /*
384 * get rid of references returned via 'lookup'
385 */
386 vnode_put(ndp->ni_vp);
387 vnode_put(ndp->ni_dvp);
388
389 ndp->ni_vp = NULLVP;
390 ndp->ni_dvp = NULLVP;
391
392 /*
393 * Check if symbolic link restarts us at the root
394 */
395 if (*(cnp->cn_nameptr) == '/') {
396 while (*(cnp->cn_nameptr) == '/') {
397 cnp->cn_nameptr++;
398 ndp->ni_pathlen--;
399 }
400 if ((dp = ndp->ni_rootdir) == NULLVP) {
401 error = ENOENT;
402 goto error_out;
403 }
404 }
405 }
406 /*
407 * only come here if we fail to handle a SYMLINK...
408 * if either ni_dvp or ni_vp is non-NULL, then
409 * we need to drop the iocount that was picked
410 * up in the lookup routine
411 */
412 if (ndp->ni_dvp)
413 vnode_put(ndp->ni_dvp);
414 if (ndp->ni_vp)
415 vnode_put(ndp->ni_vp);
416 error_out:
417 if ( (cnp->cn_flags & HASBUF) ) {
418 cnp->cn_flags &= ~HASBUF;
419 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
420 }
421 cnp->cn_pnbuf = NULL;
422 ndp->ni_vp = NULLVP;
423 if (error == ERECYCLE){
424 /* vnode was recycled underneath us. re-drive lookup to start at
425 the beginning again, since recycling invalidated last lookup*/
426 ndp->ni_cnd.cn_flags = cnpflags;
427 ndp->ni_dvp = usedvp;
428 goto vnode_recycled;
429 }
430
431
432 return (error);
433 }
434
435
436 /*
437 * Search a pathname.
438 * This is a very central and rather complicated routine.
439 *
440 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
441 * The starting directory is taken from ni_startdir. The pathname is
442 * descended until done, or a symbolic link is encountered. The variable
443 * ni_more is clear if the path is completed; it is set to one if a
444 * symbolic link needing interpretation is encountered.
445 *
446 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
447 * whether the name is to be looked up, created, renamed, or deleted.
448 * When CREATE, RENAME, or DELETE is specified, information usable in
449 * creating, renaming, or deleting a directory entry may be calculated.
450 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
451 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
452 * returned unlocked. Otherwise the parent directory is not returned. If
453 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
454 * the target is returned locked, otherwise it is returned unlocked.
455 * When creating or renaming and LOCKPARENT is specified, the target may not
456 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
457 *
458 * Overall outline of lookup:
459 *
460 * dirloop:
461 * identify next component of name at ndp->ni_ptr
462 * handle degenerate case where name is null string
463 * if .. and crossing mount points and on mounted filesys, find parent
464 * call VNOP_LOOKUP routine for next component name
465 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
466 * component vnode returned in ni_vp (if it exists), locked.
467 * if result vnode is mounted on and crossing mount points,
468 * find mounted on vnode
469 * if more components of name, do next level at dirloop
470 * return the answer in ni_vp, locked if LOCKLEAF set
471 * if LOCKPARENT set, return locked parent in ni_dvp
472 * if WANTPARENT set, return unlocked parent in ni_dvp
473 *
474 * Returns: 0 Success
475 * ENOENT No such file or directory
476 * EBADF Bad file descriptor
477 * ENOTDIR Not a directory
478 * EROFS Read-only file system [CREATE]
479 * EISDIR Is a directory [CREATE]
480 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
481 * vnode_authorize:EROFS
482 * vnode_authorize:EACCES
483 * vnode_authorize:EPERM
484 * vnode_authorize:???
485 * VNOP_LOOKUP:ENOENT No such file or directory
486 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
487 * VNOP_LOOKUP:???
488 * VFS_ROOT:ENOTSUP
489 * VFS_ROOT:ENOENT
490 * VFS_ROOT:???
491 */
492 int
493 lookup(struct nameidata *ndp)
494 {
495 char *cp; /* pointer into pathname argument */
496 vnode_t tdp; /* saved dp */
497 vnode_t dp; /* the directory we are searching */
498 mount_t mp; /* mount table entry */
499 int docache = 1; /* == 0 do not cache last component */
500 int wantparent; /* 1 => wantparent or lockparent flag */
501 int rdonly; /* lookup read-only flag bit */
502 int trailing_slash = 0;
503 int dp_authorized = 0;
504 int error = 0;
505 struct componentname *cnp = &ndp->ni_cnd;
506 vfs_context_t ctx = cnp->cn_context;
507 int mounted_on_depth = 0;
508 int dont_cache_mp = 0;
509 vnode_t mounted_on_dp = NULLVP;
510 int current_mount_generation = 0;
511 int vbusyflags = 0;
512 int nc_generation = 0;
513 vnode_t last_dp = NULLVP;
514
515 /*
516 * Setup: break out flag bits into variables.
517 */
518 if (cnp->cn_flags & (NOCACHE | DOWHITEOUT)) {
519 if ((cnp->cn_flags & NOCACHE) || (cnp->cn_nameiop == DELETE))
520 docache = 0;
521 }
522 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
523 rdonly = cnp->cn_flags & RDONLY;
524 cnp->cn_flags &= ~ISSYMLINK;
525 cnp->cn_consume = 0;
526
527 dp = ndp->ni_startdir;
528 ndp->ni_startdir = NULLVP;
529
530 if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0)
531 vbusyflags = LK_NOWAIT;
532 cp = cnp->cn_nameptr;
533
534 if (*cp == '\0') {
535 if ( (vnode_getwithref(dp)) ) {
536 dp = NULLVP;
537 error = ENOENT;
538 goto bad;
539 }
540 goto emptyname;
541 }
542 dirloop:
543 ndp->ni_vp = NULLVP;
544
545 if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &trailing_slash, &dp_authorized, last_dp)) ) {
546 dp = NULLVP;
547 goto bad;
548 }
549 if ((cnp->cn_flags & ISLASTCN)) {
550 if (docache)
551 cnp->cn_flags |= MAKEENTRY;
552 } else
553 cnp->cn_flags |= MAKEENTRY;
554
555 dp = ndp->ni_dvp;
556
557 if (ndp->ni_vp != NULLVP) {
558 /*
559 * cache_lookup_path returned a non-NULL ni_vp then,
560 * we're guaranteed that the dp is a VDIR, it's
561 * been authorized, and vp is not ".."
562 *
563 * make sure we don't try to enter the name back into
564 * the cache if this vp is purged before we get to that
565 * check since we won't have serialized behind whatever
566 * activity is occurring in the FS that caused the purge
567 */
568 if (dp != NULLVP)
569 nc_generation = dp->v_nc_generation - 1;
570
571 goto returned_from_lookup_path;
572 }
573
574 /*
575 * Handle "..": two special cases.
576 * 1. If at root directory (e.g. after chroot)
577 * or at absolute root directory
578 * then ignore it so can't get out.
579 * 2. If this vnode is the root of a mounted
580 * filesystem, then replace it with the
581 * vnode which was mounted on so we take the
582 * .. in the other file system.
583 */
584 if ( (cnp->cn_flags & ISDOTDOT) ) {
585 for (;;) {
586 if (dp == ndp->ni_rootdir || dp == rootvnode) {
587 ndp->ni_dvp = dp;
588 ndp->ni_vp = dp;
589 /*
590 * we're pinned at the root
591 * we've already got one reference on 'dp'
592 * courtesy of cache_lookup_path... take
593 * another one for the ".."
594 * if we fail to get the new reference, we'll
595 * drop our original down in 'bad'
596 */
597 if ( (vnode_get(dp)) ) {
598 error = ENOENT;
599 goto bad;
600 }
601 goto nextname;
602 }
603 if ((dp->v_flag & VROOT) == 0 ||
604 (cnp->cn_flags & NOCROSSMOUNT))
605 break;
606 if (dp->v_mount == NULL) { /* forced umount */
607 error = EBADF;
608 goto bad;
609 }
610 tdp = dp;
611 dp = tdp->v_mount->mnt_vnodecovered;
612
613 vnode_put(tdp);
614
615 if ( (vnode_getwithref(dp)) ) {
616 dp = NULLVP;
617 error = ENOENT;
618 goto bad;
619 }
620 ndp->ni_dvp = dp;
621 dp_authorized = 0;
622 }
623 }
624
625 /*
626 * We now have a segment name to search for, and a directory to search.
627 */
628 unionlookup:
629 ndp->ni_vp = NULLVP;
630
631 if (dp->v_type != VDIR) {
632 error = ENOTDIR;
633 goto lookup_error;
634 }
635 if ( (cnp->cn_flags & DONOTAUTH) != DONOTAUTH ) {
636 if (!dp_authorized) {
637 error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
638 if (error)
639 goto lookup_error;
640 }
641 #if CONFIG_MACF
642 error = mac_vnode_check_lookup(ctx, dp, cnp);
643 if (error)
644 goto lookup_error;
645 #endif /* CONFIG_MACF */
646 }
647
648 nc_generation = dp->v_nc_generation;
649
650 if ( (error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx)) ) {
651 lookup_error:
652 if ((error == ENOENT) &&
653 (dp->v_flag & VROOT) && (dp->v_mount != NULL) &&
654 (dp->v_mount->mnt_flag & MNT_UNION)) {
655 if ((cnp->cn_flags & FSNODELOCKHELD)) {
656 cnp->cn_flags &= ~FSNODELOCKHELD;
657 unlock_fsnode(dp, NULL);
658 }
659 tdp = dp;
660 dp = tdp->v_mount->mnt_vnodecovered;
661
662 vnode_put(tdp);
663
664 if ( (vnode_getwithref(dp)) ) {
665 dp = NULLVP;
666 error = ENOENT;
667 goto bad;
668 }
669 ndp->ni_dvp = dp;
670 dp_authorized = 0;
671 goto unionlookup;
672 }
673
674 if (error != EJUSTRETURN)
675 goto bad;
676
677 if (ndp->ni_vp != NULLVP)
678 panic("leaf should be empty");
679
680 /*
681 * If creating and at end of pathname, then can consider
682 * allowing file to be created.
683 */
684 if (rdonly) {
685 error = EROFS;
686 goto bad;
687 }
688 if ((cnp->cn_flags & ISLASTCN) && trailing_slash && !(cnp->cn_flags & WILLBEDIR)) {
689 error = ENOENT;
690 goto bad;
691 }
692 /*
693 * We return with ni_vp NULL to indicate that the entry
694 * doesn't currently exist, leaving a pointer to the
695 * referenced directory vnode in ndp->ni_dvp.
696 */
697 if (cnp->cn_flags & SAVESTART) {
698 if ( (vnode_get(ndp->ni_dvp)) ) {
699 error = ENOENT;
700 goto bad;
701 }
702 ndp->ni_startdir = ndp->ni_dvp;
703 }
704 if (!wantparent)
705 vnode_put(ndp->ni_dvp);
706
707 if (kdebug_enable)
708 kdebug_lookup(ndp->ni_dvp, cnp);
709 return (0);
710 }
711 returned_from_lookup_path:
712 dp = ndp->ni_vp;
713
714 /*
715 * Take into account any additional components consumed by
716 * the underlying filesystem.
717 */
718 if (cnp->cn_consume > 0) {
719 cnp->cn_nameptr += cnp->cn_consume;
720 ndp->ni_next += cnp->cn_consume;
721 ndp->ni_pathlen -= cnp->cn_consume;
722 cnp->cn_consume = 0;
723 } else {
724 if (dp->v_name == NULL || dp->v_parent == NULLVP) {
725 int isdot_or_dotdot;
726 int update_flags = 0;
727
728 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
729
730 if (isdot_or_dotdot == 0) {
731 if (dp->v_name == NULL)
732 update_flags |= VNODE_UPDATE_NAME;
733 if (ndp->ni_dvp != NULLVP && dp->v_parent == NULLVP)
734 update_flags |= VNODE_UPDATE_PARENT;
735
736 if (update_flags)
737 vnode_update_identity(dp, ndp->ni_dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
738 }
739 }
740 if ( (cnp->cn_flags & MAKEENTRY) && (dp->v_flag & VNCACHEABLE) && LIST_FIRST(&dp->v_nclinks) == NULL) {
741 /*
742 * missing from name cache, but should
743 * be in it... this can happen if volfs
744 * causes the vnode to be created or the
745 * name cache entry got recycled but the
746 * vnode didn't...
747 * check to make sure that ni_dvp is valid
748 * cache_lookup_path may return a NULL
749 * do a quick check to see if the generation of the
750 * directory matches our snapshot... this will get
751 * rechecked behind the name cache lock, but if it
752 * already fails to match, no need to go any further
753 */
754 if (ndp->ni_dvp != NULLVP && (nc_generation == ndp->ni_dvp->v_nc_generation))
755 cache_enter_with_gen(ndp->ni_dvp, dp, cnp, nc_generation);
756 }
757 }
758
759 mounted_on_dp = dp;
760 mounted_on_depth = 0;
761 dont_cache_mp = 0;
762 current_mount_generation = mount_generation;
763 /*
764 * Check to see if the vnode has been mounted on...
765 * if so find the root of the mounted file system.
766 */
767 check_mounted_on:
768 if ((dp->v_type == VDIR) && dp->v_mountedhere &&
769 ((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
770
771 vnode_lock(dp);
772
773 if ((dp->v_type == VDIR) && (mp = dp->v_mountedhere)) {
774 struct uthread *uth = (struct uthread *)get_bsdthread_info(current_thread());
775
776 mp->mnt_crossref++;
777 vnode_unlock(dp);
778
779
780 if (vfs_busy(mp, vbusyflags)) {
781 mount_dropcrossref(mp, dp, 0);
782 if (vbusyflags == LK_NOWAIT) {
783 error = ENOENT;
784 goto bad2;
785 }
786 goto check_mounted_on;
787 }
788
789 /*
790 * XXX - if this is the last component of the
791 * pathname, and it's either not a lookup operation
792 * or the NOTRIGGER flag is set for the operation,
793 * set a uthread flag to let VFS_ROOT() for autofs
794 * know it shouldn't trigger a mount.
795 */
796 if ((cnp->cn_flags & ISLASTCN) &&
797 (cnp->cn_nameiop != LOOKUP ||
798 (cnp->cn_flags & NOTRIGGER))) {
799 uth->uu_notrigger = 1;
800 dont_cache_mp = 1;
801 }
802 error = VFS_ROOT(mp, &tdp, ctx);
803 /* XXX - clear the uthread flag */
804 uth->uu_notrigger = 0;
805 /*
806 * mount_dropcrossref does a vnode_put
807 * on dp if the 3rd arg is non-zero
808 */
809 mount_dropcrossref(mp, dp, 1);
810 dp = NULL;
811 vfs_unbusy(mp);
812
813 if (error) {
814 goto bad2;
815 }
816 ndp->ni_vp = dp = tdp;
817 mounted_on_depth++;
818
819 goto check_mounted_on;
820 }
821 vnode_unlock(dp);
822 }
823
824 #if CONFIG_MACF
825 if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
826 error = vnode_label(vnode_mount(dp), NULL, dp, NULL,
827 VNODE_LABEL_NEEDREF, ctx);
828 if (error)
829 goto bad2;
830 }
831 #endif
832
833 if (mounted_on_depth && !dont_cache_mp) {
834 mp = mounted_on_dp->v_mountedhere;
835
836 if (mp) {
837 mount_lock(mp);
838 mp->mnt_realrootvp_vid = dp->v_id;
839 mp->mnt_realrootvp = dp;
840 mp->mnt_generation = current_mount_generation;
841 mount_unlock(mp);
842 }
843 }
844
845 /*
846 * Check for symbolic link
847 */
848 if ((dp->v_type == VLNK) &&
849 ((cnp->cn_flags & FOLLOW) || trailing_slash || *ndp->ni_next == '/')) {
850 cnp->cn_flags |= ISSYMLINK;
851 return (0);
852 }
853
854 /*
855 * Check for bogus trailing slashes.
856 */
857 if (trailing_slash) {
858 if (dp->v_type != VDIR) {
859 error = ENOTDIR;
860 goto bad2;
861 }
862 trailing_slash = 0;
863 }
864
865 nextname:
866 /*
867 * Not a symbolic link. If more pathname,
868 * continue at next component, else return.
869 */
870 if (*ndp->ni_next == '/') {
871 cnp->cn_nameptr = ndp->ni_next + 1;
872 ndp->ni_pathlen--;
873 while (*cnp->cn_nameptr == '/') {
874 cnp->cn_nameptr++;
875 ndp->ni_pathlen--;
876 }
877 vnode_put(ndp->ni_dvp);
878
879 cp = cnp->cn_nameptr;
880
881 if (*cp == '\0')
882 goto emptyname;
883
884 /*
885 * cache_lookup_path is now responsible for dropping io ref on dp
886 * when it is called again in the dirloop. This ensures we hold
887 * a ref on dp until we complete the next round of lookup.
888 */
889 last_dp = dp;
890 goto dirloop;
891 }
892
893 /*
894 * Disallow directory write attempts on read-only file systems.
895 */
896 if (rdonly &&
897 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
898 error = EROFS;
899 goto bad2;
900 }
901 if (cnp->cn_flags & SAVESTART) {
902 /*
903 * note that we already hold a reference
904 * on both dp and ni_dvp, but for some reason
905 * can't get another one... in this case we
906 * need to do vnode_put on dp in 'bad2'
907 */
908 if ( (vnode_get(ndp->ni_dvp)) ) {
909 error = ENOENT;
910 goto bad2;
911 }
912 ndp->ni_startdir = ndp->ni_dvp;
913 }
914 if (!wantparent && ndp->ni_dvp) {
915 vnode_put(ndp->ni_dvp);
916 ndp->ni_dvp = NULLVP;
917 }
918
919 if (cnp->cn_flags & AUDITVNPATH1)
920 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
921 else if (cnp->cn_flags & AUDITVNPATH2)
922 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
923
924 #if NAMEDRSRCFORK
925 /*
926 * Caller wants the resource fork.
927 */
928 if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
929 vnode_t svp = NULLVP;
930 enum nsoperation nsop;
931
932 if (dp->v_type != VREG) {
933 error = ENOENT;
934 goto bad2;
935 }
936 switch (cnp->cn_nameiop) {
937 case DELETE:
938 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
939 nsop = NS_DELETE;
940 }
941 else {
942 error = EPERM;
943 goto bad;
944 }
945 break;
946 case CREATE:
947 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
948 nsop = NS_CREATE;
949 }
950 else {
951 error = EPERM;
952 goto bad;
953 }
954 break;
955 case LOOKUP:
956 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
957 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
958 nsop = NS_OPEN;
959 } else {
960 error = EPERM;
961 goto bad2;
962 }
963 break;
964 default:
965 error = EPERM;
966 goto bad2;
967 }
968 /* Ask the file system for the resource fork. */
969 error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, 0, ctx);
970
971 /* During a create, it OK for stream vnode to be missing. */
972 if (error == ENOATTR || error == ENOENT) {
973 error = (nsop == NS_CREATE) ? 0 : ENOENT;
974 }
975 if (error) {
976 goto bad2;
977 }
978 /* The "parent" of the stream is the file. */
979 if (wantparent) {
980 if (ndp->ni_dvp) {
981 if (ndp->ni_cnd.cn_flags & FSNODELOCKHELD) {
982 ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD;
983 unlock_fsnode(ndp->ni_dvp, NULL);
984 }
985 vnode_put(ndp->ni_dvp);
986 }
987 ndp->ni_dvp = dp;
988 } else {
989 vnode_put(dp);
990 }
991 ndp->ni_vp = dp = svp; /* on create this may be null */
992
993 /* Restore the truncated pathname buffer (for audits). */
994 if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
995 ndp->ni_next[0] = '/';
996 }
997 cnp->cn_flags &= ~MAKEENTRY;
998 }
999 #endif
1000 if (kdebug_enable)
1001 kdebug_lookup(dp, cnp);
1002 return (0);
1003
1004 emptyname:
1005 cnp->cn_namelen = 0;
1006 /*
1007 * A degenerate name (e.g. / or "") which is a way of
1008 * talking about a directory, e.g. like "/." or ".".
1009 */
1010 if (dp->v_type != VDIR) {
1011 error = ENOTDIR;
1012 goto bad;
1013 }
1014 if (cnp->cn_nameiop != LOOKUP) {
1015 error = EISDIR;
1016 goto bad;
1017 }
1018 if (wantparent) {
1019 /*
1020 * note that we already hold a reference
1021 * on dp, but for some reason can't
1022 * get another one... in this case we
1023 * need to do vnode_put on dp in 'bad'
1024 */
1025 if ( (vnode_get(dp)) ) {
1026 error = ENOENT;
1027 goto bad;
1028 }
1029 ndp->ni_dvp = dp;
1030 }
1031 cnp->cn_flags &= ~ISDOTDOT;
1032 cnp->cn_flags |= ISLASTCN;
1033 ndp->ni_next = cp;
1034 ndp->ni_vp = dp;
1035
1036 if (cnp->cn_flags & AUDITVNPATH1)
1037 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
1038 else if (cnp->cn_flags & AUDITVNPATH2)
1039 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
1040 if (cnp->cn_flags & SAVESTART)
1041 panic("lookup: SAVESTART");
1042 return (0);
1043
1044 bad2:
1045 if ((cnp->cn_flags & FSNODELOCKHELD)) {
1046 cnp->cn_flags &= ~FSNODELOCKHELD;
1047 unlock_fsnode(ndp->ni_dvp, NULL);
1048 }
1049 if (ndp->ni_dvp)
1050 vnode_put(ndp->ni_dvp);
1051 if (dp)
1052 vnode_put(dp);
1053 ndp->ni_vp = NULLVP;
1054
1055 if (kdebug_enable)
1056 kdebug_lookup(dp, cnp);
1057 return (error);
1058
1059 bad:
1060 if ((cnp->cn_flags & FSNODELOCKHELD)) {
1061 cnp->cn_flags &= ~FSNODELOCKHELD;
1062 unlock_fsnode(ndp->ni_dvp, NULL);
1063 }
1064 if (dp)
1065 vnode_put(dp);
1066 ndp->ni_vp = NULLVP;
1067
1068 if (kdebug_enable)
1069 kdebug_lookup(dp, cnp);
1070 return (error);
1071 }
1072
1073 /*
1074 * relookup - lookup a path name component
1075 * Used by lookup to re-aquire things.
1076 */
1077 int
1078 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1079 {
1080 struct vnode *dp = NULL; /* the directory we are searching */
1081 int wantparent; /* 1 => wantparent or lockparent flag */
1082 int rdonly; /* lookup read-only flag bit */
1083 int error = 0;
1084 #ifdef NAMEI_DIAGNOSTIC
1085 int i, newhash; /* DEBUG: check name hash */
1086 char *cp; /* DEBUG: check name ptr/len */
1087 #endif
1088 vfs_context_t ctx = cnp->cn_context;;
1089
1090 /*
1091 * Setup: break out flag bits into variables.
1092 */
1093 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1094 rdonly = cnp->cn_flags & RDONLY;
1095 cnp->cn_flags &= ~ISSYMLINK;
1096
1097 if (cnp->cn_flags & NOCACHE)
1098 cnp->cn_flags &= ~MAKEENTRY;
1099 else
1100 cnp->cn_flags |= MAKEENTRY;
1101
1102 dp = dvp;
1103
1104 /*
1105 * Check for degenerate name (e.g. / or "")
1106 * which is a way of talking about a directory,
1107 * e.g. like "/." or ".".
1108 */
1109 if (cnp->cn_nameptr[0] == '\0') {
1110 if (cnp->cn_nameiop != LOOKUP || wantparent) {
1111 error = EISDIR;
1112 goto bad;
1113 }
1114 if (dp->v_type != VDIR) {
1115 error = ENOTDIR;
1116 goto bad;
1117 }
1118 if ( (vnode_get(dp)) ) {
1119 error = ENOENT;
1120 goto bad;
1121 }
1122 *vpp = dp;
1123
1124 if (cnp->cn_flags & SAVESTART)
1125 panic("lookup: SAVESTART");
1126 return (0);
1127 }
1128 /*
1129 * We now have a segment name to search for, and a directory to search.
1130 */
1131 if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) {
1132 if (error != EJUSTRETURN)
1133 goto bad;
1134 #if DIAGNOSTIC
1135 if (*vpp != NULL)
1136 panic("leaf should be empty");
1137 #endif
1138 /*
1139 * If creating and at end of pathname, then can consider
1140 * allowing file to be created.
1141 */
1142 if (rdonly) {
1143 error = EROFS;
1144 goto bad;
1145 }
1146 /*
1147 * We return with ni_vp NULL to indicate that the entry
1148 * doesn't currently exist, leaving a pointer to the
1149 * (possibly locked) directory inode in ndp->ni_dvp.
1150 */
1151 return (0);
1152 }
1153 dp = *vpp;
1154
1155 #if DIAGNOSTIC
1156 /*
1157 * Check for symbolic link
1158 */
1159 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1160 panic ("relookup: symlink found.\n");
1161 #endif
1162
1163 /*
1164 * Disallow directory write attempts on read-only file systems.
1165 */
1166 if (rdonly &&
1167 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1168 error = EROFS;
1169 goto bad2;
1170 }
1171 /* ASSERT(dvp == ndp->ni_startdir) */
1172
1173 return (0);
1174
1175 bad2:
1176 vnode_put(dp);
1177 bad:
1178 *vpp = NULL;
1179
1180 return (error);
1181 }
1182
1183 /*
1184 * Free pathname buffer
1185 */
1186 void
1187 nameidone(struct nameidata *ndp)
1188 {
1189 if ((ndp->ni_cnd.cn_flags & FSNODELOCKHELD)) {
1190 ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD;
1191 unlock_fsnode(ndp->ni_dvp, NULL);
1192 }
1193 if (ndp->ni_cnd.cn_flags & HASBUF) {
1194 char *tmp = ndp->ni_cnd.cn_pnbuf;
1195
1196 ndp->ni_cnd.cn_pnbuf = NULL;
1197 ndp->ni_cnd.cn_flags &= ~HASBUF;
1198 FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI);
1199 }
1200 }
1201
1202
1203 #define NUMPARMS 23
1204
1205 /*
1206 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1207 * by fs_usage. The path up to and including the current component name are
1208 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1209 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1210 * That is, the truncation removes the leading portion of the path.
1211 *
1212 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1213 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1214 * (in addition to DBG_FUNC_START if it is also the first). There may be
1215 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1216 *
1217 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1218 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1219 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1220 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1221 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1222 * path) is set to zero bytes, or '>' if there is more path beyond the
1223 * current component name (usually because an intermediate component was not
1224 * found).
1225 *
1226 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1227 * 12+N*16, there will be no padding.
1228 *
1229 * TODO: If there is more path beyond the current component name, should we
1230 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1231 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1232 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1233 */
1234 static void
1235 kdebug_lookup(struct vnode *dp, struct componentname *cnp)
1236 {
1237 unsigned int i;
1238 int code;
1239 int dbg_namelen;
1240 char *dbg_nameptr;
1241 long dbg_parms[NUMPARMS];
1242
1243 /* Collect the pathname for tracing */
1244 dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen;
1245 dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen;
1246
1247 if (dbg_namelen > (int)sizeof(dbg_parms))
1248 dbg_namelen = sizeof(dbg_parms);
1249 dbg_nameptr -= dbg_namelen;
1250
1251 /* Copy the (possibly truncated) path itself */
1252 memcpy(dbg_parms, dbg_nameptr, dbg_namelen);
1253
1254 /* Pad with '\0' or '>' */
1255 if (dbg_namelen < (int)sizeof(dbg_parms)) {
1256 memset((char *)dbg_parms + dbg_namelen,
1257 *(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0,
1258 sizeof(dbg_parms) - dbg_namelen);
1259 }
1260
1261 /*
1262 * In the event that we collect multiple, consecutive pathname
1263 * entries, we must mark the start of the path's string and the end.
1264 */
1265 code = (FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_START;
1266
1267 if (dbg_namelen <= 12)
1268 code |= DBG_FUNC_END;
1269
1270 KERNEL_DEBUG_CONSTANT(code, (unsigned int)dp, dbg_parms[0], dbg_parms[1], dbg_parms[2], 0);
1271
1272 code &= ~DBG_FUNC_START;
1273
1274 for (i=3, dbg_namelen -= 12; dbg_namelen > 0; i+=4, dbg_namelen -= 16) {
1275 if (dbg_namelen <= 16)
1276 code |= DBG_FUNC_END;
1277
1278 KERNEL_DEBUG_CONSTANT(code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0);
1279 }
1280 }
1281
1282 /*
1283 * Obtain the real path from a legacy volfs style path.
1284 *
1285 * Valid formats of input path:
1286 *
1287 * "555/@"
1288 * "555/2"
1289 * "555/123456"
1290 * "555/123456/foobar"
1291 *
1292 * Where:
1293 * 555 represents the volfs file system id
1294 * '@' and '2' are aliases to the root of a file system
1295 * 123456 represents a file id
1296 * "foobar" represents a file name
1297 */
1298 #if CONFIG_VOLFS
1299 static int
1300 vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
1301 {
1302 vnode_t vp;
1303 struct mount *mp = NULL;
1304 char *str;
1305 char ch;
1306 unsigned long id;
1307 ino64_t ino;
1308 int error;
1309 int length;
1310
1311 /* Get file system id and move str to next component. */
1312 id = strtoul(path, &str, 10);
1313 if (id == 0 || str[0] != '/') {
1314 return (EINVAL);
1315 }
1316 while (*str == '/') {
1317 str++;
1318 }
1319 ch = *str;
1320
1321 mp = mount_lookupby_volfsid(id, 1);
1322 if (mp == NULL) {
1323 return (EINVAL); /* unexpected failure */
1324 }
1325 /* Check for an alias to a file system root. */
1326 if (ch == '@' && str[1] == '\0') {
1327 ino = 2;
1328 str++;
1329 } else {
1330 /* Get file id and move str to next component. */
1331 ino = strtouq(str, &str, 10);
1332 }
1333
1334 /* Get the target vnode. */
1335 if (ino == 2) {
1336 error = VFS_ROOT(mp, &vp, ctx);
1337 } else {
1338 error = VFS_VGET(mp, ino, &vp, ctx);
1339 }
1340 vfs_unbusy(mp);
1341 if (error) {
1342 goto out;
1343 }
1344 realpath[0] = '\0';
1345
1346 /* Get the absolute path to this vnode. */
1347 error = build_path(vp, realpath, bufsize, &length, 0, ctx);
1348 vnode_put(vp);
1349
1350 if (error == 0 && *str != '\0') {
1351 int attempt = strlcat(realpath, str, MAXPATHLEN);
1352 if (attempt > MAXPATHLEN){
1353 error = ENAMETOOLONG;
1354 }
1355 }
1356 out:
1357 return (error);
1358 }
1359 #endif