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