]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_lookup.c
537bdaa97858a813f8d80432e932f652681d1264
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
69 */
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/syslimits.h>
74 #include <sys/time.h>
75 #include <sys/namei.h>
76 #include <sys/vm.h>
77 #include <sys/vnode_internal.h>
78 #include <sys/mount_internal.h>
79 #include <sys/errno.h>
80 #include <sys/malloc.h>
81 #include <sys/filedesc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kdebug.h>
84 #include <sys/unistd.h> /* For _PC_NAME_MAX */
85 #include <sys/uio_internal.h>
86 #include <sys/kauth.h>
87
88 #include <bsm/audit_kernel.h>
89
90 #if KTRACE
91 #include <sys/ktrace.h>
92 #endif
93
94
95 static void kdebug_lookup(struct vnode *dp, struct componentname *cnp);
96
97 /*
98 * Convert a pathname into a pointer to a locked inode.
99 *
100 * The FOLLOW flag is set when symbolic links are to be followed
101 * when they occur at the end of the name translation process.
102 * Symbolic links are always followed for all other pathname
103 * components other than the last.
104 *
105 * The segflg defines whether the name is to be copied from user
106 * space or kernel space.
107 *
108 * Overall outline of namei:
109 *
110 * copy in name
111 * get starting directory
112 * while (!done && !error) {
113 * call lookup to search path.
114 * if symbolic link, massage name in buffer and continue
115 * }
116 */
117
118 int
119 namei(ndp)
120 register struct nameidata *ndp;
121 {
122 register struct filedesc *fdp; /* pointer to file descriptor state */
123 register char *cp; /* pointer into pathname argument */
124 register struct vnode *dp; /* the directory we are searching */
125 uio_t auio;
126 int error;
127 struct componentname *cnp = &ndp->ni_cnd;
128 vfs_context_t ctx = cnp->cn_context;
129 struct proc *p = vfs_context_proc(ctx);
130 char *tmppn;
131 char uio_buf[ UIO_SIZEOF(1) ];
132
133 #if DIAGNOSTIC
134 if (!vfs_context_ucred(ctx) || !p)
135 panic ("namei: bad cred/proc");
136 if (cnp->cn_nameiop & (~OPMASK))
137 panic ("namei: nameiop contaminated with flags");
138 if (cnp->cn_flags & OPMASK)
139 panic ("namei: flags contaminated with nameiops");
140 #endif
141 fdp = p->p_fd;
142
143 /*
144 * Get a buffer for the name to be translated, and copy the
145 * name into the buffer.
146 */
147 if ((cnp->cn_flags & HASBUF) == 0) {
148 cnp->cn_pnbuf = &ndp->ni_pathbuf;
149 cnp->cn_pnlen = PATHBUFLEN;
150 }
151 #if LP64_DEBUG
152 if (IS_VALID_UIO_SEGFLG(ndp->ni_segflg) == 0) {
153 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
154 }
155 #endif /* LP64_DEBUG */
156
157 retry_copy:
158 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg))
159 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
160 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
161 else
162 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
163 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
164
165 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
166 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t,
167 MAXPATHLEN, M_NAMEI, M_WAITOK);
168
169 cnp->cn_flags |= HASBUF;
170 cnp->cn_pnlen = MAXPATHLEN;
171
172 goto retry_copy;
173 }
174 if (error)
175 goto error_out;
176
177 /* If we are auditing the kernel pathname, save the user pathname */
178 if (cnp->cn_flags & AUDITVNPATH1)
179 AUDIT_ARG(upath, p, cnp->cn_pnbuf, ARG_UPATH1);
180 if (cnp->cn_flags & AUDITVNPATH2)
181 AUDIT_ARG(upath, p, cnp->cn_pnbuf, ARG_UPATH2);
182
183 /*
184 * Do not allow empty pathnames
185 */
186 if (*cnp->cn_pnbuf == '\0') {
187 error = ENOENT;
188 goto error_out;
189 }
190 ndp->ni_loopcnt = 0;
191 #if KTRACE
192 if (KTRPOINT(p, KTR_NAMEI))
193 ktrnamei(p->p_tracep, cnp->cn_pnbuf);
194 #endif
195
196 /*
197 * determine the starting point for the translation.
198 */
199 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) {
200 if ( !(fdp->fd_flags & FD_CHROOT))
201 ndp->ni_rootdir = rootvnode;
202 }
203 cnp->cn_nameptr = cnp->cn_pnbuf;
204
205 ndp->ni_usedvp = NULLVP;
206
207 if (*(cnp->cn_nameptr) == '/') {
208 while (*(cnp->cn_nameptr) == '/') {
209 cnp->cn_nameptr++;
210 ndp->ni_pathlen--;
211 }
212 dp = ndp->ni_rootdir;
213 } else if (cnp->cn_flags & USEDVP) {
214 dp = ndp->ni_dvp;
215 ndp->ni_usedvp = dp;
216 } else
217 dp = fdp->fd_cdir;
218
219 if (dp == NULLVP) {
220 error = ENOENT;
221 goto error_out;
222 }
223 ndp->ni_dvp = NULLVP;
224 ndp->ni_vp = NULLVP;
225
226 for (;;) {
227 int need_newpathbuf;
228 int linklen;
229
230 ndp->ni_startdir = dp;
231
232 if ( (error = lookup(ndp)) ) {
233 goto error_out;
234 }
235 /*
236 * Check for symbolic link
237 */
238 if ((cnp->cn_flags & ISSYMLINK) == 0) {
239 return (0);
240 }
241 if ((cnp->cn_flags & FSNODELOCKHELD)) {
242 cnp->cn_flags &= ~FSNODELOCKHELD;
243 unlock_fsnode(ndp->ni_dvp, NULL);
244 }
245 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
246 error = ELOOP;
247 break;
248 }
249 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF))
250 need_newpathbuf = 1;
251 else
252 need_newpathbuf = 0;
253
254 if (need_newpathbuf) {
255 MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
256 } else {
257 cp = cnp->cn_pnbuf;
258 }
259 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf));
260
261 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
262
263 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
264 if (error) {
265 if (need_newpathbuf)
266 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
267 break;
268 }
269 // LP64todo - fix this
270 linklen = MAXPATHLEN - uio_resid(auio);
271 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
272 if (need_newpathbuf)
273 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
274
275 error = ENAMETOOLONG;
276 break;
277 }
278 if (need_newpathbuf) {
279 long len = cnp->cn_pnlen;
280
281 tmppn = cnp->cn_pnbuf;
282 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
283 cnp->cn_pnbuf = cp;
284 cnp->cn_pnlen = MAXPATHLEN;
285
286 if ( (cnp->cn_flags & HASBUF) )
287 FREE_ZONE(tmppn, len, M_NAMEI);
288 else
289 cnp->cn_flags |= HASBUF;
290 } else
291 cnp->cn_pnbuf[linklen] = '\0';
292
293 ndp->ni_pathlen += linklen;
294 cnp->cn_nameptr = cnp->cn_pnbuf;
295
296 /*
297 * starting point for 'relative'
298 * symbolic link path
299 */
300 dp = ndp->ni_dvp;
301 /*
302 * get rid of references returned via 'lookup'
303 */
304 vnode_put(ndp->ni_vp);
305 vnode_put(ndp->ni_dvp);
306
307 ndp->ni_vp = NULLVP;
308 ndp->ni_dvp = NULLVP;
309
310 /*
311 * Check if symbolic link restarts us at the root
312 */
313 if (*(cnp->cn_nameptr) == '/') {
314 while (*(cnp->cn_nameptr) == '/') {
315 cnp->cn_nameptr++;
316 ndp->ni_pathlen--;
317 }
318 if ((dp = ndp->ni_rootdir) == NULLVP) {
319 error = ENOENT;
320 goto error_out;
321 }
322 }
323 }
324 /*
325 * only come here if we fail to handle a SYMLINK...
326 * if either ni_dvp or ni_vp is non-NULL, then
327 * we need to drop the iocount that was picked
328 * up in the lookup routine
329 */
330 if (ndp->ni_dvp)
331 vnode_put(ndp->ni_dvp);
332 if (ndp->ni_vp)
333 vnode_put(ndp->ni_vp);
334 error_out:
335 if ( (cnp->cn_flags & HASBUF) ) {
336 cnp->cn_flags &= ~HASBUF;
337 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
338 }
339 cnp->cn_pnbuf = NULL;
340 ndp->ni_vp = NULLVP;
341
342 return (error);
343 }
344
345
346 /*
347 * Search a pathname.
348 * This is a very central and rather complicated routine.
349 *
350 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
351 * The starting directory is taken from ni_startdir. The pathname is
352 * descended until done, or a symbolic link is encountered. The variable
353 * ni_more is clear if the path is completed; it is set to one if a
354 * symbolic link needing interpretation is encountered.
355 *
356 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
357 * whether the name is to be looked up, created, renamed, or deleted.
358 * When CREATE, RENAME, or DELETE is specified, information usable in
359 * creating, renaming, or deleting a directory entry may be calculated.
360 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
361 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
362 * returned unlocked. Otherwise the parent directory is not returned. If
363 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
364 * the target is returned locked, otherwise it is returned unlocked.
365 * When creating or renaming and LOCKPARENT is specified, the target may not
366 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
367 *
368 * Overall outline of lookup:
369 *
370 * dirloop:
371 * identify next component of name at ndp->ni_ptr
372 * handle degenerate case where name is null string
373 * if .. and crossing mount points and on mounted filesys, find parent
374 * call VNOP_LOOKUP routine for next component name
375 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
376 * component vnode returned in ni_vp (if it exists), locked.
377 * if result vnode is mounted on and crossing mount points,
378 * find mounted on vnode
379 * if more components of name, do next level at dirloop
380 * return the answer in ni_vp, locked if LOCKLEAF set
381 * if LOCKPARENT set, return locked parent in ni_dvp
382 * if WANTPARENT set, return unlocked parent in ni_dvp
383 */
384 int
385 lookup(ndp)
386 register struct nameidata *ndp;
387 {
388 register char *cp; /* pointer into pathname argument */
389 vnode_t tdp; /* saved dp */
390 vnode_t dp; /* the directory we are searching */
391 mount_t mp; /* mount table entry */
392 int docache = 1; /* == 0 do not cache last component */
393 int wantparent; /* 1 => wantparent or lockparent flag */
394 int rdonly; /* lookup read-only flag bit */
395 int trailing_slash = 0;
396 int dp_authorized = 0;
397 int error = 0;
398 struct componentname *cnp = &ndp->ni_cnd;
399 vfs_context_t ctx = cnp->cn_context;
400
401 /*
402 * Setup: break out flag bits into variables.
403 */
404 if (cnp->cn_flags & (NOCACHE | DOWHITEOUT)) {
405 if ((cnp->cn_flags & NOCACHE) || (cnp->cn_nameiop == DELETE))
406 docache = 0;
407 }
408 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
409 rdonly = cnp->cn_flags & RDONLY;
410 cnp->cn_flags &= ~ISSYMLINK;
411 cnp->cn_consume = 0;
412
413 dp = ndp->ni_startdir;
414 ndp->ni_startdir = NULLVP;
415
416 cp = cnp->cn_nameptr;
417
418 if (*cp == '\0') {
419 if ( (vnode_getwithref(dp)) ) {
420 dp = NULLVP;
421 error = ENOENT;
422 goto bad;
423 }
424 goto emptyname;
425 }
426 dirloop:
427 ndp->ni_vp = NULLVP;
428
429 if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &trailing_slash, &dp_authorized)) ) {
430 dp = NULLVP;
431 goto bad;
432 }
433 if ((cnp->cn_flags & ISLASTCN)) {
434 if (docache)
435 cnp->cn_flags |= MAKEENTRY;
436 } else
437 cnp->cn_flags |= MAKEENTRY;
438
439 dp = ndp->ni_dvp;
440
441 if (ndp->ni_vp != NULLVP) {
442 /*
443 * cache_lookup_path returned a non-NULL ni_vp then,
444 * we're guaranteed that the dp is a VDIR, it's
445 * been authorized, and vp is not ".."
446 */
447 goto returned_from_lookup_path;
448 }
449
450 /*
451 * Handle "..": two special cases.
452 * 1. If at root directory (e.g. after chroot)
453 * or at absolute root directory
454 * then ignore it so can't get out.
455 * 2. If this vnode is the root of a mounted
456 * filesystem, then replace it with the
457 * vnode which was mounted on so we take the
458 * .. in the other file system.
459 */
460 if ( (cnp->cn_flags & ISDOTDOT) ) {
461 for (;;) {
462 if (dp == ndp->ni_rootdir || dp == rootvnode) {
463 ndp->ni_dvp = dp;
464 ndp->ni_vp = dp;
465 /*
466 * we're pinned at the root
467 * we've already got one reference on 'dp'
468 * courtesy of cache_lookup_path... take
469 * another one for the ".."
470 * if we fail to get the new reference, we'll
471 * drop our original down in 'bad'
472 */
473 if ( (vnode_get(dp)) ) {
474 error = ENOENT;
475 goto bad;
476 }
477 goto nextname;
478 }
479 if ((dp->v_flag & VROOT) == 0 ||
480 (cnp->cn_flags & NOCROSSMOUNT))
481 break;
482 if (dp->v_mount == NULL) { /* forced umount */
483 error = EBADF;
484 goto bad;
485 }
486 tdp = dp;
487 dp = tdp->v_mount->mnt_vnodecovered;
488
489 vnode_put(tdp);
490
491 if ( (vnode_getwithref(dp)) ) {
492 dp = NULLVP;
493 error = ENOENT;
494 goto bad;
495 }
496 ndp->ni_dvp = dp;
497 dp_authorized = 0;
498 }
499 }
500
501 /*
502 * We now have a segment name to search for, and a directory to search.
503 */
504 unionlookup:
505 ndp->ni_vp = NULLVP;
506
507 if (dp->v_type != VDIR) {
508 error = ENOTDIR;
509 goto lookup_error;
510 }
511 if ( !(dp_authorized || (cnp->cn_flags & DONOTAUTH)) ) {
512 if ( (error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx)) )
513 goto lookup_error;
514 }
515 if ( (error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx)) ) {
516 lookup_error:
517 if ((error == ENOENT) &&
518 (dp->v_flag & VROOT) && (dp->v_mount != NULL) &&
519 (dp->v_mount->mnt_flag & MNT_UNION)) {
520 if ((cnp->cn_flags & FSNODELOCKHELD)) {
521 cnp->cn_flags &= ~FSNODELOCKHELD;
522 unlock_fsnode(dp, NULL);
523 }
524 tdp = dp;
525 dp = tdp->v_mount->mnt_vnodecovered;
526
527 vnode_put(tdp);
528
529 if ( (vnode_getwithref(dp)) ) {
530 dp = NULLVP;
531 error = ENOENT;
532 goto bad;
533 }
534 ndp->ni_dvp = dp;
535 dp_authorized = 0;
536 goto unionlookup;
537 }
538
539 if (error != EJUSTRETURN)
540 goto bad;
541
542 if (ndp->ni_vp != NULLVP)
543 panic("leaf should be empty");
544
545 /*
546 * If creating and at end of pathname, then can consider
547 * allowing file to be created.
548 */
549 if (rdonly) {
550 error = EROFS;
551 goto bad;
552 }
553 if ((cnp->cn_flags & ISLASTCN) && trailing_slash && !(cnp->cn_flags & WILLBEDIR)) {
554 error = ENOENT;
555 goto bad;
556 }
557 /*
558 * We return with ni_vp NULL to indicate that the entry
559 * doesn't currently exist, leaving a pointer to the
560 * referenced directory vnode in ndp->ni_dvp.
561 */
562 if (cnp->cn_flags & SAVESTART) {
563 if ( (vnode_get(ndp->ni_dvp)) ) {
564 error = ENOENT;
565 goto bad;
566 }
567 ndp->ni_startdir = ndp->ni_dvp;
568 }
569 if (!wantparent)
570 vnode_put(ndp->ni_dvp);
571
572 if (kdebug_enable)
573 kdebug_lookup(ndp->ni_dvp, cnp);
574 return (0);
575 }
576 returned_from_lookup_path:
577 dp = ndp->ni_vp;
578
579 /*
580 * Take into account any additional components consumed by
581 * the underlying filesystem.
582 */
583 if (cnp->cn_consume > 0) {
584 cnp->cn_nameptr += cnp->cn_consume;
585 ndp->ni_next += cnp->cn_consume;
586 ndp->ni_pathlen -= cnp->cn_consume;
587 cnp->cn_consume = 0;
588 } else {
589 if (dp->v_name == NULL || dp->v_parent == NULLVP) {
590 int isdot_or_dotdot;
591 int update_flags = 0;
592
593 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
594
595 if (isdot_or_dotdot == 0) {
596 if (dp->v_name == NULL)
597 update_flags |= VNODE_UPDATE_NAME;
598 if (ndp->ni_dvp != NULLVP && dp->v_parent == NULLVP)
599 update_flags |= VNODE_UPDATE_PARENT;
600
601 if (update_flags)
602 vnode_update_identity(dp, ndp->ni_dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
603 }
604 }
605
606 if ( (cnp->cn_flags & MAKEENTRY) && (dp->v_flag & VNCACHEABLE) && LIST_FIRST(&dp->v_nclinks) == NULL) {
607 /*
608 * missing from name cache, but should
609 * be in it... this can happen if volfs
610 * causes the vnode to be created or the
611 * name cache entry got recycled but the
612 * vnode didn't...
613 * check to make sure that ni_dvp is valid
614 * cache_lookup_path may return a NULL
615 */
616 if (ndp->ni_dvp != NULL)
617 cache_enter(ndp->ni_dvp, dp, cnp);
618 }
619 }
620
621 /*
622 * Check to see if the vnode has been mounted on...
623 * if so find the root of the mounted file system.
624 */
625 check_mounted_on:
626 if ((dp->v_type == VDIR) && dp->v_mountedhere &&
627 ((cnp->cn_flags & NOCROSSMOUNT) == 0)) {
628
629 vnode_lock(dp);
630
631 if ((dp->v_type == VDIR) && (mp = dp->v_mountedhere)) {
632
633 mp->mnt_crossref++;
634 vnode_unlock(dp);
635
636 if (vfs_busy(mp, 0)) {
637 mount_dropcrossref(mp, dp, 0);
638 goto check_mounted_on;
639 }
640 error = VFS_ROOT(mp, &tdp, ctx);
641 /*
642 * mount_dropcrossref does a vnode_put
643 * on dp if the 3rd arg is non-zero
644 */
645 mount_dropcrossref(mp, dp, 1);
646 dp = NULL;
647 vfs_unbusy(mp);
648
649 if (error) {
650 goto bad2;
651 }
652 ndp->ni_vp = dp = tdp;
653
654 goto check_mounted_on;
655 }
656 vnode_unlock(dp);
657 }
658
659 /*
660 * Check for symbolic link
661 */
662 if ((dp->v_type == VLNK) &&
663 ((cnp->cn_flags & FOLLOW) || trailing_slash || *ndp->ni_next == '/')) {
664 cnp->cn_flags |= ISSYMLINK;
665 return (0);
666 }
667
668 /*
669 * Check for bogus trailing slashes.
670 */
671 if (trailing_slash) {
672 if (dp->v_type != VDIR) {
673 error = ENOTDIR;
674 goto bad2;
675 }
676 trailing_slash = 0;
677 }
678
679 nextname:
680 /*
681 * Not a symbolic link. If more pathname,
682 * continue at next component, else return.
683 */
684 if (*ndp->ni_next == '/') {
685 cnp->cn_nameptr = ndp->ni_next + 1;
686 ndp->ni_pathlen--;
687 while (*cnp->cn_nameptr == '/') {
688 cnp->cn_nameptr++;
689 ndp->ni_pathlen--;
690 }
691 vnode_put(ndp->ni_dvp);
692
693 cp = cnp->cn_nameptr;
694
695 if (*cp == '\0')
696 goto emptyname;
697
698 vnode_put(dp);
699 goto dirloop;
700 }
701
702 /*
703 * Disallow directory write attempts on read-only file systems.
704 */
705 if (rdonly &&
706 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
707 error = EROFS;
708 goto bad2;
709 }
710 if (cnp->cn_flags & SAVESTART) {
711 /*
712 * note that we already hold a reference
713 * on both dp and ni_dvp, but for some reason
714 * can't get another one... in this case we
715 * need to do vnode_put on dp in 'bad2'
716 */
717 if ( (vnode_get(ndp->ni_dvp)) ) {
718 error = ENOENT;
719 goto bad2;
720 }
721 ndp->ni_startdir = ndp->ni_dvp;
722 }
723 if (!wantparent && ndp->ni_dvp)
724 vnode_put(ndp->ni_dvp);
725
726 if (cnp->cn_flags & AUDITVNPATH1)
727 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
728 else if (cnp->cn_flags & AUDITVNPATH2)
729 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
730
731 if (kdebug_enable)
732 kdebug_lookup(dp, cnp);
733 return (0);
734
735 emptyname:
736 cnp->cn_namelen = 0;
737 /*
738 * A degenerate name (e.g. / or "") which is a way of
739 * talking about a directory, e.g. like "/." or ".".
740 */
741 if (dp->v_type != VDIR) {
742 error = ENOTDIR;
743 goto bad;
744 }
745 if (cnp->cn_nameiop != LOOKUP) {
746 error = EISDIR;
747 goto bad;
748 }
749 if (wantparent) {
750 /*
751 * note that we already hold a reference
752 * on dp, but for some reason can't
753 * get another one... in this case we
754 * need to do vnode_put on dp in 'bad'
755 */
756 if ( (vnode_get(dp)) ) {
757 error = ENOENT;
758 goto bad;
759 }
760 ndp->ni_dvp = dp;
761 }
762 cnp->cn_flags &= ~ISDOTDOT;
763 cnp->cn_flags |= ISLASTCN;
764 ndp->ni_next = cp;
765 ndp->ni_vp = dp;
766
767 if (cnp->cn_flags & AUDITVNPATH1)
768 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
769 else if (cnp->cn_flags & AUDITVNPATH2)
770 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
771 if (cnp->cn_flags & SAVESTART)
772 panic("lookup: SAVESTART");
773 return (0);
774
775 bad2:
776 if ((cnp->cn_flags & FSNODELOCKHELD)) {
777 cnp->cn_flags &= ~FSNODELOCKHELD;
778 unlock_fsnode(ndp->ni_dvp, NULL);
779 }
780 if (ndp->ni_dvp)
781 vnode_put(ndp->ni_dvp);
782 if (dp)
783 vnode_put(dp);
784 ndp->ni_vp = NULLVP;
785
786 if (kdebug_enable)
787 kdebug_lookup(dp, cnp);
788 return (error);
789
790 bad:
791 if ((cnp->cn_flags & FSNODELOCKHELD)) {
792 cnp->cn_flags &= ~FSNODELOCKHELD;
793 unlock_fsnode(ndp->ni_dvp, NULL);
794 }
795 if (dp)
796 vnode_put(dp);
797 ndp->ni_vp = NULLVP;
798
799 if (kdebug_enable)
800 kdebug_lookup(dp, cnp);
801 return (error);
802 }
803
804 /*
805 * relookup - lookup a path name component
806 * Used by lookup to re-aquire things.
807 */
808 int
809 relookup(dvp, vpp, cnp)
810 struct vnode *dvp, **vpp;
811 struct componentname *cnp;
812 {
813 struct vnode *dp = 0; /* the directory we are searching */
814 int wantparent; /* 1 => wantparent or lockparent flag */
815 int rdonly; /* lookup read-only flag bit */
816 int error = 0;
817 #ifdef NAMEI_DIAGNOSTIC
818 int i, newhash; /* DEBUG: check name hash */
819 char *cp; /* DEBUG: check name ptr/len */
820 #endif
821 vfs_context_t ctx = cnp->cn_context;;
822
823 /*
824 * Setup: break out flag bits into variables.
825 */
826 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
827 rdonly = cnp->cn_flags & RDONLY;
828 cnp->cn_flags &= ~ISSYMLINK;
829
830 if (cnp->cn_flags & NOCACHE)
831 cnp->cn_flags &= ~MAKEENTRY;
832 else
833 cnp->cn_flags |= MAKEENTRY;
834
835 dp = dvp;
836
837 /*
838 * Check for degenerate name (e.g. / or "")
839 * which is a way of talking about a directory,
840 * e.g. like "/." or ".".
841 */
842 if (cnp->cn_nameptr[0] == '\0') {
843 if (cnp->cn_nameiop != LOOKUP || wantparent) {
844 error = EISDIR;
845 goto bad;
846 }
847 if (dp->v_type != VDIR) {
848 error = ENOTDIR;
849 goto bad;
850 }
851 if ( (vnode_get(dp)) ) {
852 error = ENOENT;
853 goto bad;
854 }
855 *vpp = dp;
856
857 if (cnp->cn_flags & SAVESTART)
858 panic("lookup: SAVESTART");
859 return (0);
860 }
861 /*
862 * We now have a segment name to search for, and a directory to search.
863 */
864 if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) {
865 if (error != EJUSTRETURN)
866 goto bad;
867 #if DIAGNOSTIC
868 if (*vpp != NULL)
869 panic("leaf should be empty");
870 #endif
871 /*
872 * If creating and at end of pathname, then can consider
873 * allowing file to be created.
874 */
875 if (rdonly) {
876 error = EROFS;
877 goto bad;
878 }
879 /*
880 * We return with ni_vp NULL to indicate that the entry
881 * doesn't currently exist, leaving a pointer to the
882 * (possibly locked) directory inode in ndp->ni_dvp.
883 */
884 return (0);
885 }
886 dp = *vpp;
887
888 #if DIAGNOSTIC
889 /*
890 * Check for symbolic link
891 */
892 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
893 panic ("relookup: symlink found.\n");
894 #endif
895
896 /*
897 * Disallow directory write attempts on read-only file systems.
898 */
899 if (rdonly &&
900 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
901 error = EROFS;
902 goto bad2;
903 }
904 /* ASSERT(dvp == ndp->ni_startdir) */
905
906 return (0);
907
908 bad2:
909 vnode_put(dp);
910 bad:
911 *vpp = NULL;
912
913 return (error);
914 }
915
916 /*
917 * Free pathname buffer
918 */
919 void
920 nameidone(struct nameidata *ndp)
921 {
922 if ((ndp->ni_cnd.cn_flags & FSNODELOCKHELD)) {
923 ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD;
924 unlock_fsnode(ndp->ni_dvp, NULL);
925 }
926 if (ndp->ni_cnd.cn_flags & HASBUF) {
927 char *tmp = ndp->ni_cnd.cn_pnbuf;
928
929 ndp->ni_cnd.cn_pnbuf = NULL;
930 ndp->ni_cnd.cn_flags &= ~HASBUF;
931 FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI);
932 }
933 }
934
935
936 #define NUMPARMS 23
937
938 static void
939 kdebug_lookup(dp, cnp)
940 struct vnode *dp;
941 struct componentname *cnp;
942 {
943 register unsigned int i, n, code;
944 register int dbg_namelen;
945 register int save_dbg_namelen;
946 register char *dbg_nameptr;
947 long dbg_parms[NUMPARMS];
948 char dbg_buf[4];
949 static char *dbg_filler = ">>>>";
950
951 /* Collect the pathname for tracing */
952 dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen;
953 dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen;
954
955 if (dbg_namelen > sizeof(dbg_parms))
956 dbg_namelen = sizeof(dbg_parms);
957 dbg_nameptr -= dbg_namelen;
958 save_dbg_namelen = dbg_namelen;
959
960 i = 0;
961
962 while (dbg_namelen > 0) {
963 if (dbg_namelen >= 4) {
964 dbg_parms[i++] = *(long *)dbg_nameptr;
965 dbg_nameptr += sizeof(long);
966 dbg_namelen -= sizeof(long);
967 } else {
968 for (n = 0; n < dbg_namelen; n++)
969 dbg_buf[n] = *dbg_nameptr++;
970 while (n <= 3) {
971 if (*dbg_nameptr)
972 dbg_buf[n++] = '>';
973 else
974 dbg_buf[n++] = 0;
975 }
976 dbg_parms[i++] = *(long *)&dbg_buf[0];
977
978 break;
979 }
980 }
981 while (i < NUMPARMS) {
982 if (*dbg_nameptr)
983 dbg_parms[i++] = *(long *)dbg_filler;
984 else
985 dbg_parms[i++] = 0;
986 }
987 dbg_namelen = save_dbg_namelen - 12;
988
989 /*
990 * In the event that we collect multiple, consecutive pathname
991 * entries, we must mark the start of the path's string and the end
992 */
993 code = (FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_START;
994
995 if (dbg_namelen <= 0)
996 code |= DBG_FUNC_END;
997
998 KERNEL_DEBUG_CONSTANT(code, (unsigned int)dp, dbg_parms[0], dbg_parms[1], dbg_parms[2], 0);
999
1000 code &= ~DBG_FUNC_START;
1001
1002 for (i = 3; dbg_namelen > 0; i += 4) {
1003
1004 dbg_namelen -= (4 * sizeof(long));
1005 if (dbg_namelen <= 0)
1006 code |= DBG_FUNC_END;
1007
1008 KERNEL_DEBUG_CONSTANT(code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0);
1009 }
1010 }