2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
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.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
68 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/syslimits.h>
75 #include <sys/namei.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>
88 #include <bsm/audit_kernel.h>
91 #include <sys/ktrace.h>
95 static void kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
);
98 * Convert a pathname into a pointer to a locked inode.
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.
105 * The segflg defines whether the name is to be copied from user
106 * space or kernel space.
108 * Overall outline of namei:
111 * get starting directory
112 * while (!done && !error) {
113 * call lookup to search path.
114 * if symbolic link, massage name in buffer and continue
120 register struct nameidata
*ndp
;
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 */
127 struct componentname
*cnp
= &ndp
->ni_cnd
;
128 vfs_context_t ctx
= cnp
->cn_context
;
129 struct proc
*p
= vfs_context_proc(ctx
);
131 char uio_buf
[ UIO_SIZEOF(1) ];
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");
144 * Get a buffer for the name to be translated, and copy the
145 * name into the buffer.
147 if ((cnp
->cn_flags
& HASBUF
) == 0) {
148 cnp
->cn_pnbuf
= &ndp
->ni_pathbuf
;
149 cnp
->cn_pnlen
= PATHBUFLEN
;
152 if (IS_VALID_UIO_SEGFLG(ndp
->ni_segflg
) == 0) {
153 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
155 #endif /* LP64_DEBUG */
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
);
162 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
163 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
165 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
166 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
,
167 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
169 cnp
->cn_flags
|= HASBUF
;
170 cnp
->cn_pnlen
= MAXPATHLEN
;
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
);
184 * Do not allow empty pathnames
186 if (*cnp
->cn_pnbuf
== '\0') {
192 if (KTRPOINT(p
, KTR_NAMEI
))
193 ktrnamei(p
->p_tracep
, cnp
->cn_pnbuf
);
197 * determine the starting point for the translation.
199 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
200 if ( !(fdp
->fd_flags
& FD_CHROOT
))
201 ndp
->ni_rootdir
= rootvnode
;
203 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
205 ndp
->ni_usedvp
= NULLVP
;
207 if (*(cnp
->cn_nameptr
) == '/') {
208 while (*(cnp
->cn_nameptr
) == '/') {
212 dp
= ndp
->ni_rootdir
;
213 } else if (cnp
->cn_flags
& USEDVP
) {
223 ndp
->ni_dvp
= NULLVP
;
230 ndp
->ni_startdir
= dp
;
232 if ( (error
= lookup(ndp
)) ) {
236 * Check for symbolic link
238 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
241 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
242 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
243 unlock_fsnode(ndp
->ni_dvp
, NULL
);
245 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
249 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
254 if (need_newpathbuf
) {
255 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
259 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
261 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
263 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
266 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
269 // LP64todo - fix this
270 linklen
= MAXPATHLEN
- uio_resid(auio
);
271 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
273 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
275 error
= ENAMETOOLONG
;
278 if (need_newpathbuf
) {
279 long len
= cnp
->cn_pnlen
;
281 tmppn
= cnp
->cn_pnbuf
;
282 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
284 cnp
->cn_pnlen
= MAXPATHLEN
;
286 if ( (cnp
->cn_flags
& HASBUF
) )
287 FREE_ZONE(tmppn
, len
, M_NAMEI
);
289 cnp
->cn_flags
|= HASBUF
;
291 cnp
->cn_pnbuf
[linklen
] = '\0';
293 ndp
->ni_pathlen
+= linklen
;
294 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
297 * starting point for 'relative'
302 * get rid of references returned via 'lookup'
304 vnode_put(ndp
->ni_vp
);
305 vnode_put(ndp
->ni_dvp
);
308 ndp
->ni_dvp
= NULLVP
;
311 * Check if symbolic link restarts us at the root
313 if (*(cnp
->cn_nameptr
) == '/') {
314 while (*(cnp
->cn_nameptr
) == '/') {
318 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
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
331 vnode_put(ndp
->ni_dvp
);
333 vnode_put(ndp
->ni_vp
);
335 if ( (cnp
->cn_flags
& HASBUF
) ) {
336 cnp
->cn_flags
&= ~HASBUF
;
337 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
339 cnp
->cn_pnbuf
= NULL
;
348 * This is a very central and rather complicated routine.
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.
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 ".".
368 * Overall outline of lookup:
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
386 register struct nameidata
*ndp
;
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;
398 struct componentname
*cnp
= &ndp
->ni_cnd
;
399 vfs_context_t ctx
= cnp
->cn_context
;
402 * Setup: break out flag bits into variables.
404 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
405 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
408 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
409 rdonly
= cnp
->cn_flags
& RDONLY
;
410 cnp
->cn_flags
&= ~ISSYMLINK
;
413 dp
= ndp
->ni_startdir
;
414 ndp
->ni_startdir
= NULLVP
;
416 cp
= cnp
->cn_nameptr
;
419 if ( (vnode_getwithref(dp
)) ) {
429 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
)) ) {
433 if ((cnp
->cn_flags
& ISLASTCN
)) {
435 cnp
->cn_flags
|= MAKEENTRY
;
437 cnp
->cn_flags
|= MAKEENTRY
;
441 if (ndp
->ni_vp
!= NULLVP
) {
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 ".."
447 goto returned_from_lookup_path
;
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.
460 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
462 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
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'
473 if ( (vnode_get(dp
)) ) {
479 if ((dp
->v_flag
& VROOT
) == 0 ||
480 (cnp
->cn_flags
& NOCROSSMOUNT
))
482 if (dp
->v_mount
== NULL
) { /* forced umount */
487 dp
= tdp
->v_mount
->mnt_vnodecovered
;
491 if ( (vnode_getwithref(dp
)) ) {
502 * We now have a segment name to search for, and a directory to search.
507 if (dp
->v_type
!= VDIR
) {
511 if ( !(dp_authorized
|| (cnp
->cn_flags
& DONOTAUTH
)) ) {
512 if ( (error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
)) )
515 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
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
);
525 dp
= tdp
->v_mount
->mnt_vnodecovered
;
529 if ( (vnode_getwithref(dp
)) ) {
539 if (error
!= EJUSTRETURN
)
542 if (ndp
->ni_vp
!= NULLVP
)
543 panic("leaf should be empty");
546 * If creating and at end of pathname, then can consider
547 * allowing file to be created.
553 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
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.
562 if (cnp
->cn_flags
& SAVESTART
) {
563 if ( (vnode_get(ndp
->ni_dvp
)) ) {
567 ndp
->ni_startdir
= ndp
->ni_dvp
;
570 vnode_put(ndp
->ni_dvp
);
573 kdebug_lookup(ndp
->ni_dvp
, cnp
);
576 returned_from_lookup_path
:
580 * Take into account any additional components consumed by
581 * the underlying filesystem.
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
;
589 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
591 int update_flags
= 0;
593 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
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
;
602 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
606 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
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
613 * check to make sure that ni_dvp is valid
614 * cache_lookup_path may return a NULL
616 if (ndp
->ni_dvp
!= NULL
)
617 cache_enter(ndp
->ni_dvp
, dp
, cnp
);
622 * Check to see if the vnode has been mounted on...
623 * if so find the root of the mounted file system.
626 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
627 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
631 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
636 if (vfs_busy(mp
, 0)) {
637 mount_dropcrossref(mp
, dp
, 0);
638 goto check_mounted_on
;
640 error
= VFS_ROOT(mp
, &tdp
, ctx
);
642 * mount_dropcrossref does a vnode_put
643 * on dp if the 3rd arg is non-zero
645 mount_dropcrossref(mp
, dp
, 1);
652 ndp
->ni_vp
= dp
= tdp
;
654 goto check_mounted_on
;
660 * Check for symbolic link
662 if ((dp
->v_type
== VLNK
) &&
663 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
664 cnp
->cn_flags
|= ISSYMLINK
;
669 * Check for bogus trailing slashes.
671 if (trailing_slash
) {
672 if (dp
->v_type
!= VDIR
) {
681 * Not a symbolic link. If more pathname,
682 * continue at next component, else return.
684 if (*ndp
->ni_next
== '/') {
685 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
687 while (*cnp
->cn_nameptr
== '/') {
691 vnode_put(ndp
->ni_dvp
);
693 cp
= cnp
->cn_nameptr
;
703 * Disallow directory write attempts on read-only file systems.
706 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
710 if (cnp
->cn_flags
& SAVESTART
) {
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'
717 if ( (vnode_get(ndp
->ni_dvp
)) ) {
721 ndp
->ni_startdir
= ndp
->ni_dvp
;
723 if (!wantparent
&& ndp
->ni_dvp
)
724 vnode_put(ndp
->ni_dvp
);
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
);
732 kdebug_lookup(dp
, cnp
);
738 * A degenerate name (e.g. / or "") which is a way of
739 * talking about a directory, e.g. like "/." or ".".
741 if (dp
->v_type
!= VDIR
) {
745 if (cnp
->cn_nameiop
!= LOOKUP
) {
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'
756 if ( (vnode_get(dp
)) ) {
762 cnp
->cn_flags
&= ~ISDOTDOT
;
763 cnp
->cn_flags
|= ISLASTCN
;
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");
776 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
777 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
778 unlock_fsnode(ndp
->ni_dvp
, NULL
);
781 vnode_put(ndp
->ni_dvp
);
787 kdebug_lookup(dp
, cnp
);
791 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
792 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
793 unlock_fsnode(ndp
->ni_dvp
, NULL
);
800 kdebug_lookup(dp
, cnp
);
805 * relookup - lookup a path name component
806 * Used by lookup to re-aquire things.
809 relookup(dvp
, vpp
, cnp
)
810 struct vnode
*dvp
, **vpp
;
811 struct componentname
*cnp
;
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 */
817 #ifdef NAMEI_DIAGNOSTIC
818 int i
, newhash
; /* DEBUG: check name hash */
819 char *cp
; /* DEBUG: check name ptr/len */
821 vfs_context_t ctx
= cnp
->cn_context
;;
824 * Setup: break out flag bits into variables.
826 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
827 rdonly
= cnp
->cn_flags
& RDONLY
;
828 cnp
->cn_flags
&= ~ISSYMLINK
;
830 if (cnp
->cn_flags
& NOCACHE
)
831 cnp
->cn_flags
&= ~MAKEENTRY
;
833 cnp
->cn_flags
|= MAKEENTRY
;
838 * Check for degenerate name (e.g. / or "")
839 * which is a way of talking about a directory,
840 * e.g. like "/." or ".".
842 if (cnp
->cn_nameptr
[0] == '\0') {
843 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
847 if (dp
->v_type
!= VDIR
) {
851 if ( (vnode_get(dp
)) ) {
857 if (cnp
->cn_flags
& SAVESTART
)
858 panic("lookup: SAVESTART");
862 * We now have a segment name to search for, and a directory to search.
864 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
865 if (error
!= EJUSTRETURN
)
869 panic("leaf should be empty");
872 * If creating and at end of pathname, then can consider
873 * allowing file to be created.
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.
890 * Check for symbolic link
892 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
893 panic ("relookup: symlink found.\n");
897 * Disallow directory write attempts on read-only file systems.
900 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
904 /* ASSERT(dvp == ndp->ni_startdir) */
917 * Free pathname buffer
920 nameidone(struct nameidata
*ndp
)
922 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
923 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
924 unlock_fsnode(ndp
->ni_dvp
, NULL
);
926 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
927 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
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
);
939 kdebug_lookup(dp
, cnp
)
941 struct componentname
*cnp
;
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
];
949 static char *dbg_filler
= ">>>>";
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
;
955 if (dbg_namelen
> sizeof(dbg_parms
))
956 dbg_namelen
= sizeof(dbg_parms
);
957 dbg_nameptr
-= dbg_namelen
;
958 save_dbg_namelen
= dbg_namelen
;
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);
968 for (n
= 0; n
< dbg_namelen
; n
++)
969 dbg_buf
[n
] = *dbg_nameptr
++;
976 dbg_parms
[i
++] = *(long *)&dbg_buf
[0];
981 while (i
< NUMPARMS
) {
983 dbg_parms
[i
++] = *(long *)dbg_filler
;
987 dbg_namelen
= save_dbg_namelen
- 12;
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
993 code
= (FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
;
995 if (dbg_namelen
<= 0)
996 code
|= DBG_FUNC_END
;
998 KERNEL_DEBUG_CONSTANT(code
, (unsigned int)dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
1000 code
&= ~DBG_FUNC_START
;
1002 for (i
= 3; dbg_namelen
> 0; i
+= 4) {
1004 dbg_namelen
-= (4 * sizeof(long));
1005 if (dbg_namelen
<= 0)
1006 code
|= DBG_FUNC_END
;
1008 KERNEL_DEBUG_CONSTANT(code
, dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);