2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
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.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/syslimits.h>
73 #include <sys/namei.h>
75 #include <sys/vnode_internal.h>
76 #include <sys/mount_internal.h>
77 #include <sys/errno.h>
78 #include <sys/malloc.h>
79 #include <sys/filedesc.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kdebug.h>
82 #include <sys/unistd.h> /* For _PC_NAME_MAX */
83 #include <sys/uio_internal.h>
84 #include <sys/kauth.h>
86 #include <bsm/audit_kernel.h>
89 #include <sys/ktrace.h>
93 static void kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
);
96 * Convert a pathname into a pointer to a locked inode.
98 * The FOLLOW flag is set when symbolic links are to be followed
99 * when they occur at the end of the name translation process.
100 * Symbolic links are always followed for all other pathname
101 * components other than the last.
103 * The segflg defines whether the name is to be copied from user
104 * space or kernel space.
106 * Overall outline of namei:
109 * get starting directory
110 * while (!done && !error) {
111 * call lookup to search path.
112 * if symbolic link, massage name in buffer and continue
118 register struct nameidata
*ndp
;
120 register struct filedesc
*fdp
; /* pointer to file descriptor state */
121 register char *cp
; /* pointer into pathname argument */
122 register struct vnode
*dp
; /* the directory we are searching */
125 struct componentname
*cnp
= &ndp
->ni_cnd
;
126 vfs_context_t ctx
= cnp
->cn_context
;
127 struct proc
*p
= vfs_context_proc(ctx
);
129 char uio_buf
[ UIO_SIZEOF(1) ];
132 if (!vfs_context_ucred(ctx
) || !p
)
133 panic ("namei: bad cred/proc");
134 if (cnp
->cn_nameiop
& (~OPMASK
))
135 panic ("namei: nameiop contaminated with flags");
136 if (cnp
->cn_flags
& OPMASK
)
137 panic ("namei: flags contaminated with nameiops");
142 * Get a buffer for the name to be translated, and copy the
143 * name into the buffer.
145 if ((cnp
->cn_flags
& HASBUF
) == 0) {
146 cnp
->cn_pnbuf
= &ndp
->ni_pathbuf
;
147 cnp
->cn_pnlen
= PATHBUFLEN
;
150 if (IS_VALID_UIO_SEGFLG(ndp
->ni_segflg
) == 0) {
151 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
153 #endif /* LP64_DEBUG */
156 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
))
157 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
158 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
160 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
161 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
163 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
164 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
,
165 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
167 cnp
->cn_flags
|= HASBUF
;
168 cnp
->cn_pnlen
= MAXPATHLEN
;
175 /* If we are auditing the kernel pathname, save the user pathname */
176 if (cnp
->cn_flags
& AUDITVNPATH1
)
177 AUDIT_ARG(upath
, p
, cnp
->cn_pnbuf
, ARG_UPATH1
);
178 if (cnp
->cn_flags
& AUDITVNPATH2
)
179 AUDIT_ARG(upath
, p
, cnp
->cn_pnbuf
, ARG_UPATH2
);
182 * Do not allow empty pathnames
184 if (*cnp
->cn_pnbuf
== '\0') {
190 if (KTRPOINT(p
, KTR_NAMEI
))
191 ktrnamei(p
->p_tracep
, cnp
->cn_pnbuf
);
195 * determine the starting point for the translation.
197 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
198 if ( !(fdp
->fd_flags
& FD_CHROOT
))
199 ndp
->ni_rootdir
= rootvnode
;
201 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
203 ndp
->ni_usedvp
= NULLVP
;
205 if (*(cnp
->cn_nameptr
) == '/') {
206 while (*(cnp
->cn_nameptr
) == '/') {
210 dp
= ndp
->ni_rootdir
;
211 } else if (cnp
->cn_flags
& USEDVP
) {
221 ndp
->ni_dvp
= NULLVP
;
228 ndp
->ni_startdir
= dp
;
230 if ( (error
= lookup(ndp
)) ) {
234 * Check for symbolic link
236 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
239 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
240 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
241 unlock_fsnode(ndp
->ni_dvp
, NULL
);
243 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
247 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
252 if (need_newpathbuf
) {
253 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
257 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
259 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
261 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
264 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
267 // LP64todo - fix this
268 linklen
= MAXPATHLEN
- uio_resid(auio
);
269 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
271 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
273 error
= ENAMETOOLONG
;
276 if (need_newpathbuf
) {
277 long len
= cnp
->cn_pnlen
;
279 tmppn
= cnp
->cn_pnbuf
;
280 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
282 cnp
->cn_pnlen
= MAXPATHLEN
;
284 if ( (cnp
->cn_flags
& HASBUF
) )
285 FREE_ZONE(tmppn
, len
, M_NAMEI
);
287 cnp
->cn_flags
|= HASBUF
;
289 cnp
->cn_pnbuf
[linklen
] = '\0';
291 ndp
->ni_pathlen
+= linklen
;
292 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
295 * starting point for 'relative'
300 * get rid of references returned via 'lookup'
302 vnode_put(ndp
->ni_vp
);
303 vnode_put(ndp
->ni_dvp
);
306 ndp
->ni_dvp
= NULLVP
;
309 * Check if symbolic link restarts us at the root
311 if (*(cnp
->cn_nameptr
) == '/') {
312 while (*(cnp
->cn_nameptr
) == '/') {
316 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
323 * only come here if we fail to handle a SYMLINK...
324 * if either ni_dvp or ni_vp is non-NULL, then
325 * we need to drop the iocount that was picked
326 * up in the lookup routine
329 vnode_put(ndp
->ni_dvp
);
331 vnode_put(ndp
->ni_vp
);
333 if ( (cnp
->cn_flags
& HASBUF
) ) {
334 cnp
->cn_flags
&= ~HASBUF
;
335 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
337 cnp
->cn_pnbuf
= NULL
;
346 * This is a very central and rather complicated routine.
348 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
349 * The starting directory is taken from ni_startdir. The pathname is
350 * descended until done, or a symbolic link is encountered. The variable
351 * ni_more is clear if the path is completed; it is set to one if a
352 * symbolic link needing interpretation is encountered.
354 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
355 * whether the name is to be looked up, created, renamed, or deleted.
356 * When CREATE, RENAME, or DELETE is specified, information usable in
357 * creating, renaming, or deleting a directory entry may be calculated.
358 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
359 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
360 * returned unlocked. Otherwise the parent directory is not returned. If
361 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
362 * the target is returned locked, otherwise it is returned unlocked.
363 * When creating or renaming and LOCKPARENT is specified, the target may not
364 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
366 * Overall outline of lookup:
369 * identify next component of name at ndp->ni_ptr
370 * handle degenerate case where name is null string
371 * if .. and crossing mount points and on mounted filesys, find parent
372 * call VNOP_LOOKUP routine for next component name
373 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
374 * component vnode returned in ni_vp (if it exists), locked.
375 * if result vnode is mounted on and crossing mount points,
376 * find mounted on vnode
377 * if more components of name, do next level at dirloop
378 * return the answer in ni_vp, locked if LOCKLEAF set
379 * if LOCKPARENT set, return locked parent in ni_dvp
380 * if WANTPARENT set, return unlocked parent in ni_dvp
384 register struct nameidata
*ndp
;
386 register char *cp
; /* pointer into pathname argument */
387 vnode_t tdp
; /* saved dp */
388 vnode_t dp
; /* the directory we are searching */
389 mount_t mp
; /* mount table entry */
390 int docache
= 1; /* == 0 do not cache last component */
391 int wantparent
; /* 1 => wantparent or lockparent flag */
392 int rdonly
; /* lookup read-only flag bit */
393 int trailing_slash
= 0;
394 int dp_authorized
= 0;
396 struct componentname
*cnp
= &ndp
->ni_cnd
;
397 vfs_context_t ctx
= cnp
->cn_context
;
400 * Setup: break out flag bits into variables.
402 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
403 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
406 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
407 rdonly
= cnp
->cn_flags
& RDONLY
;
408 cnp
->cn_flags
&= ~ISSYMLINK
;
411 dp
= ndp
->ni_startdir
;
412 ndp
->ni_startdir
= NULLVP
;
414 cp
= cnp
->cn_nameptr
;
417 if ( (vnode_getwithref(dp
)) ) {
427 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
)) ) {
431 if ((cnp
->cn_flags
& ISLASTCN
)) {
433 cnp
->cn_flags
|= MAKEENTRY
;
435 cnp
->cn_flags
|= MAKEENTRY
;
439 if (ndp
->ni_vp
!= NULLVP
) {
441 * cache_lookup_path returned a non-NULL ni_vp then,
442 * we're guaranteed that the dp is a VDIR, it's
443 * been authorized, and vp is not ".."
445 goto returned_from_lookup_path
;
449 * Handle "..": two special cases.
450 * 1. If at root directory (e.g. after chroot)
451 * or at absolute root directory
452 * then ignore it so can't get out.
453 * 2. If this vnode is the root of a mounted
454 * filesystem, then replace it with the
455 * vnode which was mounted on so we take the
456 * .. in the other file system.
458 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
460 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
464 * we're pinned at the root
465 * we've already got one reference on 'dp'
466 * courtesy of cache_lookup_path... take
467 * another one for the ".."
468 * if we fail to get the new reference, we'll
469 * drop our original down in 'bad'
471 if ( (vnode_get(dp
)) ) {
477 if ((dp
->v_flag
& VROOT
) == 0 ||
478 (cnp
->cn_flags
& NOCROSSMOUNT
))
480 if (dp
->v_mount
== NULL
) { /* forced umount */
485 dp
= tdp
->v_mount
->mnt_vnodecovered
;
489 if ( (vnode_getwithref(dp
)) ) {
500 * We now have a segment name to search for, and a directory to search.
505 if (dp
->v_type
!= VDIR
) {
509 if ( !(dp_authorized
|| (cnp
->cn_flags
& DONOTAUTH
)) ) {
510 if ( (error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
)) )
513 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
515 if ((error
== ENOENT
) &&
516 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
517 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
518 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
519 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
520 unlock_fsnode(dp
, NULL
);
523 dp
= tdp
->v_mount
->mnt_vnodecovered
;
527 if ( (vnode_getwithref(dp
)) ) {
537 if (error
!= EJUSTRETURN
)
540 if (ndp
->ni_vp
!= NULLVP
)
541 panic("leaf should be empty");
544 * If creating and at end of pathname, then can consider
545 * allowing file to be created.
551 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
556 * We return with ni_vp NULL to indicate that the entry
557 * doesn't currently exist, leaving a pointer to the
558 * referenced directory vnode in ndp->ni_dvp.
560 if (cnp
->cn_flags
& SAVESTART
) {
561 if ( (vnode_get(ndp
->ni_dvp
)) ) {
565 ndp
->ni_startdir
= ndp
->ni_dvp
;
568 vnode_put(ndp
->ni_dvp
);
571 kdebug_lookup(ndp
->ni_dvp
, cnp
);
574 returned_from_lookup_path
:
578 * Take into account any additional components consumed by
579 * the underlying filesystem.
581 if (cnp
->cn_consume
> 0) {
582 cnp
->cn_nameptr
+= cnp
->cn_consume
;
583 ndp
->ni_next
+= cnp
->cn_consume
;
584 ndp
->ni_pathlen
-= cnp
->cn_consume
;
587 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
589 int update_flags
= 0;
591 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
593 if (isdot_or_dotdot
== 0) {
594 if (dp
->v_name
== NULL
)
595 update_flags
|= VNODE_UPDATE_NAME
;
596 if (ndp
->ni_dvp
!= NULLVP
&& dp
->v_parent
== NULLVP
)
597 update_flags
|= VNODE_UPDATE_PARENT
;
600 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
604 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
606 * missing from name cache, but should
607 * be in it... this can happen if volfs
608 * causes the vnode to be created or the
609 * name cache entry got recycled but the
611 * check to make sure that ni_dvp is valid
612 * cache_lookup_path may return a NULL
614 if (ndp
->ni_dvp
!= NULL
)
615 cache_enter(ndp
->ni_dvp
, dp
, cnp
);
620 * Check to see if the vnode has been mounted on...
621 * if so find the root of the mounted file system.
624 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
625 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
629 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
634 if (vfs_busy(mp
, 0)) {
635 mount_dropcrossref(mp
, dp
, 0);
636 goto check_mounted_on
;
638 error
= VFS_ROOT(mp
, &tdp
, ctx
);
640 * mount_dropcrossref does a vnode_put
641 * on dp if the 3rd arg is non-zero
643 mount_dropcrossref(mp
, dp
, 1);
650 ndp
->ni_vp
= dp
= tdp
;
652 goto check_mounted_on
;
658 * Check for symbolic link
660 if ((dp
->v_type
== VLNK
) &&
661 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
662 cnp
->cn_flags
|= ISSYMLINK
;
667 * Check for bogus trailing slashes.
669 if (trailing_slash
) {
670 if (dp
->v_type
!= VDIR
) {
679 * Not a symbolic link. If more pathname,
680 * continue at next component, else return.
682 if (*ndp
->ni_next
== '/') {
683 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
685 while (*cnp
->cn_nameptr
== '/') {
689 vnode_put(ndp
->ni_dvp
);
691 cp
= cnp
->cn_nameptr
;
701 * Disallow directory write attempts on read-only file systems.
704 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
708 if (cnp
->cn_flags
& SAVESTART
) {
710 * note that we already hold a reference
711 * on both dp and ni_dvp, but for some reason
712 * can't get another one... in this case we
713 * need to do vnode_put on dp in 'bad2'
715 if ( (vnode_get(ndp
->ni_dvp
)) ) {
719 ndp
->ni_startdir
= ndp
->ni_dvp
;
721 if (!wantparent
&& ndp
->ni_dvp
)
722 vnode_put(ndp
->ni_dvp
);
724 if (cnp
->cn_flags
& AUDITVNPATH1
)
725 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
726 else if (cnp
->cn_flags
& AUDITVNPATH2
)
727 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
730 kdebug_lookup(dp
, cnp
);
736 * A degenerate name (e.g. / or "") which is a way of
737 * talking about a directory, e.g. like "/." or ".".
739 if (dp
->v_type
!= VDIR
) {
743 if (cnp
->cn_nameiop
!= LOOKUP
) {
749 * note that we already hold a reference
750 * on dp, but for some reason can't
751 * get another one... in this case we
752 * need to do vnode_put on dp in 'bad'
754 if ( (vnode_get(dp
)) ) {
760 cnp
->cn_flags
&= ~ISDOTDOT
;
761 cnp
->cn_flags
|= ISLASTCN
;
765 if (cnp
->cn_flags
& AUDITVNPATH1
)
766 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
767 else if (cnp
->cn_flags
& AUDITVNPATH2
)
768 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
769 if (cnp
->cn_flags
& SAVESTART
)
770 panic("lookup: SAVESTART");
774 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
775 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
776 unlock_fsnode(ndp
->ni_dvp
, NULL
);
779 vnode_put(ndp
->ni_dvp
);
785 kdebug_lookup(dp
, cnp
);
789 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
790 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
791 unlock_fsnode(ndp
->ni_dvp
, NULL
);
798 kdebug_lookup(dp
, cnp
);
803 * relookup - lookup a path name component
804 * Used by lookup to re-aquire things.
807 relookup(dvp
, vpp
, cnp
)
808 struct vnode
*dvp
, **vpp
;
809 struct componentname
*cnp
;
811 struct vnode
*dp
= 0; /* the directory we are searching */
812 int wantparent
; /* 1 => wantparent or lockparent flag */
813 int rdonly
; /* lookup read-only flag bit */
815 #ifdef NAMEI_DIAGNOSTIC
816 int i
, newhash
; /* DEBUG: check name hash */
817 char *cp
; /* DEBUG: check name ptr/len */
819 vfs_context_t ctx
= cnp
->cn_context
;;
822 * Setup: break out flag bits into variables.
824 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
825 rdonly
= cnp
->cn_flags
& RDONLY
;
826 cnp
->cn_flags
&= ~ISSYMLINK
;
828 if (cnp
->cn_flags
& NOCACHE
)
829 cnp
->cn_flags
&= ~MAKEENTRY
;
831 cnp
->cn_flags
|= MAKEENTRY
;
836 * Check for degenerate name (e.g. / or "")
837 * which is a way of talking about a directory,
838 * e.g. like "/." or ".".
840 if (cnp
->cn_nameptr
[0] == '\0') {
841 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
845 if (dp
->v_type
!= VDIR
) {
849 if ( (vnode_get(dp
)) ) {
855 if (cnp
->cn_flags
& SAVESTART
)
856 panic("lookup: SAVESTART");
860 * We now have a segment name to search for, and a directory to search.
862 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
863 if (error
!= EJUSTRETURN
)
867 panic("leaf should be empty");
870 * If creating and at end of pathname, then can consider
871 * allowing file to be created.
878 * We return with ni_vp NULL to indicate that the entry
879 * doesn't currently exist, leaving a pointer to the
880 * (possibly locked) directory inode in ndp->ni_dvp.
888 * Check for symbolic link
890 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
891 panic ("relookup: symlink found.\n");
895 * Disallow directory write attempts on read-only file systems.
898 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
902 /* ASSERT(dvp == ndp->ni_startdir) */
915 * Free pathname buffer
918 nameidone(struct nameidata
*ndp
)
920 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
921 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
922 unlock_fsnode(ndp
->ni_dvp
, NULL
);
924 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
925 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
927 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
928 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
929 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
937 kdebug_lookup(dp
, cnp
)
939 struct componentname
*cnp
;
941 register unsigned int i
, n
;
942 register int dbg_namelen
;
943 register int save_dbg_namelen
;
944 register char *dbg_nameptr
;
945 long dbg_parms
[NUMPARMS
];
947 static char *dbg_filler
= ">>>>";
949 /* Collect the pathname for tracing */
950 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
951 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
953 if (dbg_namelen
> sizeof(dbg_parms
))
954 dbg_namelen
= sizeof(dbg_parms
);
955 dbg_nameptr
-= dbg_namelen
;
956 save_dbg_namelen
= dbg_namelen
;
960 while (dbg_namelen
> 0) {
961 if (dbg_namelen
>= 4) {
962 dbg_parms
[i
++] = *(long *)dbg_nameptr
;
963 dbg_nameptr
+= sizeof(long);
964 dbg_namelen
-= sizeof(long);
966 for (n
= 0; n
< dbg_namelen
; n
++)
967 dbg_buf
[n
] = *dbg_nameptr
++;
974 dbg_parms
[i
++] = *(long *)&dbg_buf
[0];
979 while (i
< NUMPARMS
) {
981 dbg_parms
[i
++] = *(long *)dbg_filler
;
987 In the event that we collect multiple, consecutive pathname
988 entries, we must mark the start of the path's string.
990 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
,
991 (unsigned int)dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
993 for (dbg_namelen
= save_dbg_namelen
-12, i
=3;
995 dbg_namelen
-=(4 * sizeof(long)), i
+= 4)
997 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_NONE
,
998 dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);