2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/syslimits.h>
67 #include <sys/namei.h>
69 #include <sys/vnode_internal.h>
70 #include <sys/mount_internal.h>
71 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/filedesc.h>
74 #include <sys/proc_internal.h>
75 #include <sys/kdebug.h>
76 #include <sys/unistd.h> /* For _PC_NAME_MAX */
77 #include <sys/uio_internal.h>
78 #include <sys/kauth.h>
80 #include <bsm/audit_kernel.h>
83 #include <sys/ktrace.h>
87 static void kdebug_lookup(struct vnode
*dp
, struct componentname
*cnp
);
90 * Convert a pathname into a pointer to a locked inode.
92 * The FOLLOW flag is set when symbolic links are to be followed
93 * when they occur at the end of the name translation process.
94 * Symbolic links are always followed for all other pathname
95 * components other than the last.
97 * The segflg defines whether the name is to be copied from user
98 * space or kernel space.
100 * Overall outline of namei:
103 * get starting directory
104 * while (!done && !error) {
105 * call lookup to search path.
106 * if symbolic link, massage name in buffer and continue
112 register struct nameidata
*ndp
;
114 register struct filedesc
*fdp
; /* pointer to file descriptor state */
115 register char *cp
; /* pointer into pathname argument */
116 register struct vnode
*dp
; /* the directory we are searching */
119 struct componentname
*cnp
= &ndp
->ni_cnd
;
120 vfs_context_t ctx
= cnp
->cn_context
;
121 struct proc
*p
= vfs_context_proc(ctx
);
123 char uio_buf
[ UIO_SIZEOF(1) ];
126 if (!vfs_context_ucred(ctx
) || !p
)
127 panic ("namei: bad cred/proc");
128 if (cnp
->cn_nameiop
& (~OPMASK
))
129 panic ("namei: nameiop contaminated with flags");
130 if (cnp
->cn_flags
& OPMASK
)
131 panic ("namei: flags contaminated with nameiops");
136 * Get a buffer for the name to be translated, and copy the
137 * name into the buffer.
139 if ((cnp
->cn_flags
& HASBUF
) == 0) {
140 cnp
->cn_pnbuf
= &ndp
->ni_pathbuf
;
141 cnp
->cn_pnlen
= PATHBUFLEN
;
144 if (IS_VALID_UIO_SEGFLG(ndp
->ni_segflg
) == 0) {
145 panic("%s :%d - invalid ni_segflg\n", __FILE__
, __LINE__
);
147 #endif /* LP64_DEBUG */
150 if (UIO_SEG_IS_USER_SPACE(ndp
->ni_segflg
))
151 error
= copyinstr(ndp
->ni_dirp
, cnp
->cn_pnbuf
,
152 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
154 error
= copystr(CAST_DOWN(void *, ndp
->ni_dirp
), cnp
->cn_pnbuf
,
155 cnp
->cn_pnlen
, (size_t *)&ndp
->ni_pathlen
);
157 if (error
== ENAMETOOLONG
&& !(cnp
->cn_flags
& HASBUF
)) {
158 MALLOC_ZONE(cnp
->cn_pnbuf
, caddr_t
,
159 MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
161 cnp
->cn_flags
|= HASBUF
;
162 cnp
->cn_pnlen
= MAXPATHLEN
;
169 /* If we are auditing the kernel pathname, save the user pathname */
170 if (cnp
->cn_flags
& AUDITVNPATH1
)
171 AUDIT_ARG(upath
, p
, cnp
->cn_pnbuf
, ARG_UPATH1
);
172 if (cnp
->cn_flags
& AUDITVNPATH2
)
173 AUDIT_ARG(upath
, p
, cnp
->cn_pnbuf
, ARG_UPATH2
);
176 * Do not allow empty pathnames
178 if (*cnp
->cn_pnbuf
== '\0') {
184 if (KTRPOINT(p
, KTR_NAMEI
))
185 ktrnamei(p
->p_tracep
, cnp
->cn_pnbuf
);
189 * determine the starting point for the translation.
191 if ((ndp
->ni_rootdir
= fdp
->fd_rdir
) == NULLVP
) {
192 if ( !(fdp
->fd_flags
& FD_CHROOT
))
193 ndp
->ni_rootdir
= rootvnode
;
195 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
197 ndp
->ni_usedvp
= NULLVP
;
199 if (*(cnp
->cn_nameptr
) == '/') {
200 while (*(cnp
->cn_nameptr
) == '/') {
204 dp
= ndp
->ni_rootdir
;
205 } else if (cnp
->cn_flags
& USEDVP
) {
215 ndp
->ni_dvp
= NULLVP
;
222 ndp
->ni_startdir
= dp
;
224 if ( (error
= lookup(ndp
)) ) {
228 * Check for symbolic link
230 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
233 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
234 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
235 unlock_fsnode(ndp
->ni_dvp
, NULL
);
237 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
241 if (ndp
->ni_pathlen
> 1 || !(cnp
->cn_flags
& HASBUF
))
246 if (need_newpathbuf
) {
247 MALLOC_ZONE(cp
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
251 auio
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof(uio_buf
));
253 uio_addiov(auio
, CAST_USER_ADDR_T(cp
), MAXPATHLEN
);
255 error
= VNOP_READLINK(ndp
->ni_vp
, auio
, ctx
);
258 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
261 // LP64todo - fix this
262 linklen
= MAXPATHLEN
- uio_resid(auio
);
263 if (linklen
+ ndp
->ni_pathlen
> MAXPATHLEN
) {
265 FREE_ZONE(cp
, MAXPATHLEN
, M_NAMEI
);
267 error
= ENAMETOOLONG
;
270 if (need_newpathbuf
) {
271 long len
= cnp
->cn_pnlen
;
273 tmppn
= cnp
->cn_pnbuf
;
274 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
276 cnp
->cn_pnlen
= MAXPATHLEN
;
278 if ( (cnp
->cn_flags
& HASBUF
) )
279 FREE_ZONE(tmppn
, len
, M_NAMEI
);
281 cnp
->cn_flags
|= HASBUF
;
283 cnp
->cn_pnbuf
[linklen
] = '\0';
285 ndp
->ni_pathlen
+= linklen
;
286 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
289 * starting point for 'relative'
294 * get rid of references returned via 'lookup'
296 vnode_put(ndp
->ni_vp
);
297 vnode_put(ndp
->ni_dvp
);
300 ndp
->ni_dvp
= NULLVP
;
303 * Check if symbolic link restarts us at the root
305 if (*(cnp
->cn_nameptr
) == '/') {
306 while (*(cnp
->cn_nameptr
) == '/') {
310 if ((dp
= ndp
->ni_rootdir
) == NULLVP
) {
317 * only come here if we fail to handle a SYMLINK...
318 * if either ni_dvp or ni_vp is non-NULL, then
319 * we need to drop the iocount that was picked
320 * up in the lookup routine
323 vnode_put(ndp
->ni_dvp
);
325 vnode_put(ndp
->ni_vp
);
327 if ( (cnp
->cn_flags
& HASBUF
) ) {
328 cnp
->cn_flags
&= ~HASBUF
;
329 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
331 cnp
->cn_pnbuf
= NULL
;
340 * This is a very central and rather complicated routine.
342 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
343 * The starting directory is taken from ni_startdir. The pathname is
344 * descended until done, or a symbolic link is encountered. The variable
345 * ni_more is clear if the path is completed; it is set to one if a
346 * symbolic link needing interpretation is encountered.
348 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
349 * whether the name is to be looked up, created, renamed, or deleted.
350 * When CREATE, RENAME, or DELETE is specified, information usable in
351 * creating, renaming, or deleting a directory entry may be calculated.
352 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
353 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
354 * returned unlocked. Otherwise the parent directory is not returned. If
355 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
356 * the target is returned locked, otherwise it is returned unlocked.
357 * When creating or renaming and LOCKPARENT is specified, the target may not
358 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
360 * Overall outline of lookup:
363 * identify next component of name at ndp->ni_ptr
364 * handle degenerate case where name is null string
365 * if .. and crossing mount points and on mounted filesys, find parent
366 * call VNOP_LOOKUP routine for next component name
367 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
368 * component vnode returned in ni_vp (if it exists), locked.
369 * if result vnode is mounted on and crossing mount points,
370 * find mounted on vnode
371 * if more components of name, do next level at dirloop
372 * return the answer in ni_vp, locked if LOCKLEAF set
373 * if LOCKPARENT set, return locked parent in ni_dvp
374 * if WANTPARENT set, return unlocked parent in ni_dvp
378 register struct nameidata
*ndp
;
380 register char *cp
; /* pointer into pathname argument */
381 vnode_t tdp
; /* saved dp */
382 vnode_t dp
; /* the directory we are searching */
383 mount_t mp
; /* mount table entry */
384 int docache
= 1; /* == 0 do not cache last component */
385 int wantparent
; /* 1 => wantparent or lockparent flag */
386 int rdonly
; /* lookup read-only flag bit */
387 int trailing_slash
= 0;
388 int dp_authorized
= 0;
390 struct componentname
*cnp
= &ndp
->ni_cnd
;
391 vfs_context_t ctx
= cnp
->cn_context
;
394 * Setup: break out flag bits into variables.
396 if (cnp
->cn_flags
& (NOCACHE
| DOWHITEOUT
)) {
397 if ((cnp
->cn_flags
& NOCACHE
) || (cnp
->cn_nameiop
== DELETE
))
400 wantparent
= cnp
->cn_flags
& (LOCKPARENT
| WANTPARENT
);
401 rdonly
= cnp
->cn_flags
& RDONLY
;
402 cnp
->cn_flags
&= ~ISSYMLINK
;
405 dp
= ndp
->ni_startdir
;
406 ndp
->ni_startdir
= NULLVP
;
408 cp
= cnp
->cn_nameptr
;
411 if ( (vnode_getwithref(dp
)) ) {
421 if ( (error
= cache_lookup_path(ndp
, cnp
, dp
, ctx
, &trailing_slash
, &dp_authorized
)) ) {
425 if ((cnp
->cn_flags
& ISLASTCN
)) {
427 cnp
->cn_flags
|= MAKEENTRY
;
429 cnp
->cn_flags
|= MAKEENTRY
;
433 if (ndp
->ni_vp
!= NULLVP
) {
435 * cache_lookup_path returned a non-NULL ni_vp then,
436 * we're guaranteed that the dp is a VDIR, it's
437 * been authorized, and vp is not ".."
439 goto returned_from_lookup_path
;
443 * Handle "..": two special cases.
444 * 1. If at root directory (e.g. after chroot)
445 * or at absolute root directory
446 * then ignore it so can't get out.
447 * 2. If this vnode is the root of a mounted
448 * filesystem, then replace it with the
449 * vnode which was mounted on so we take the
450 * .. in the other file system.
452 if ( (cnp
->cn_flags
& ISDOTDOT
) ) {
454 if (dp
== ndp
->ni_rootdir
|| dp
== rootvnode
) {
458 * we're pinned at the root
459 * we've already got one reference on 'dp'
460 * courtesy of cache_lookup_path... take
461 * another one for the ".."
462 * if we fail to get the new reference, we'll
463 * drop our original down in 'bad'
465 if ( (vnode_get(dp
)) ) {
471 if ((dp
->v_flag
& VROOT
) == 0 ||
472 (cnp
->cn_flags
& NOCROSSMOUNT
))
474 if (dp
->v_mount
== NULL
) { /* forced umount */
479 dp
= tdp
->v_mount
->mnt_vnodecovered
;
483 if ( (vnode_getwithref(dp
)) ) {
494 * We now have a segment name to search for, and a directory to search.
499 if (dp
->v_type
!= VDIR
) {
503 if ( !(dp_authorized
|| (cnp
->cn_flags
& DONOTAUTH
)) ) {
504 if ( (error
= vnode_authorize(dp
, NULL
, KAUTH_VNODE_SEARCH
, ctx
)) )
507 if ( (error
= VNOP_LOOKUP(dp
, &ndp
->ni_vp
, cnp
, ctx
)) ) {
509 if ((error
== ENOENT
) &&
510 (dp
->v_flag
& VROOT
) && (dp
->v_mount
!= NULL
) &&
511 (dp
->v_mount
->mnt_flag
& MNT_UNION
)) {
512 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
513 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
514 unlock_fsnode(dp
, NULL
);
517 dp
= tdp
->v_mount
->mnt_vnodecovered
;
521 if ( (vnode_getwithref(dp
)) ) {
531 if (error
!= EJUSTRETURN
)
534 if (ndp
->ni_vp
!= NULLVP
)
535 panic("leaf should be empty");
538 * If creating and at end of pathname, then can consider
539 * allowing file to be created.
545 if ((cnp
->cn_flags
& ISLASTCN
) && trailing_slash
&& !(cnp
->cn_flags
& WILLBEDIR
)) {
550 * We return with ni_vp NULL to indicate that the entry
551 * doesn't currently exist, leaving a pointer to the
552 * referenced directory vnode in ndp->ni_dvp.
554 if (cnp
->cn_flags
& SAVESTART
) {
555 if ( (vnode_get(ndp
->ni_dvp
)) ) {
559 ndp
->ni_startdir
= ndp
->ni_dvp
;
562 vnode_put(ndp
->ni_dvp
);
565 kdebug_lookup(ndp
->ni_dvp
, cnp
);
568 returned_from_lookup_path
:
572 * Take into account any additional components consumed by
573 * the underlying filesystem.
575 if (cnp
->cn_consume
> 0) {
576 cnp
->cn_nameptr
+= cnp
->cn_consume
;
577 ndp
->ni_next
+= cnp
->cn_consume
;
578 ndp
->ni_pathlen
-= cnp
->cn_consume
;
581 if (dp
->v_name
== NULL
|| dp
->v_parent
== NULLVP
) {
583 int update_flags
= 0;
585 isdot_or_dotdot
= (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.') || (cnp
->cn_flags
& ISDOTDOT
);
587 if (isdot_or_dotdot
== 0) {
588 if (dp
->v_name
== NULL
)
589 update_flags
|= VNODE_UPDATE_NAME
;
590 if (ndp
->ni_dvp
!= NULLVP
&& dp
->v_parent
== NULLVP
)
591 update_flags
|= VNODE_UPDATE_PARENT
;
594 vnode_update_identity(dp
, ndp
->ni_dvp
, cnp
->cn_nameptr
, cnp
->cn_namelen
, cnp
->cn_hash
, update_flags
);
598 if ( (cnp
->cn_flags
& MAKEENTRY
) && (dp
->v_flag
& VNCACHEABLE
) && LIST_FIRST(&dp
->v_nclinks
) == NULL
) {
600 * missing from name cache, but should
601 * be in it... this can happen if volfs
602 * causes the vnode to be created or the
603 * name cache entry got recycled but the
605 * check to make sure that ni_dvp is valid
606 * cache_lookup_path may return a NULL
608 if (ndp
->ni_dvp
!= NULL
)
609 cache_enter(ndp
->ni_dvp
, dp
, cnp
);
614 * Check to see if the vnode has been mounted on...
615 * if so find the root of the mounted file system.
618 if ((dp
->v_type
== VDIR
) && dp
->v_mountedhere
&&
619 ((cnp
->cn_flags
& NOCROSSMOUNT
) == 0)) {
623 if ((dp
->v_type
== VDIR
) && (mp
= dp
->v_mountedhere
)) {
628 if (vfs_busy(mp
, 0)) {
629 mount_dropcrossref(mp
, dp
, 0);
630 goto check_mounted_on
;
632 error
= VFS_ROOT(mp
, &tdp
, ctx
);
634 * mount_dropcrossref does a vnode_put
635 * on dp if the 3rd arg is non-zero
637 mount_dropcrossref(mp
, dp
, 1);
644 ndp
->ni_vp
= dp
= tdp
;
646 goto check_mounted_on
;
652 * Check for symbolic link
654 if ((dp
->v_type
== VLNK
) &&
655 ((cnp
->cn_flags
& FOLLOW
) || trailing_slash
|| *ndp
->ni_next
== '/')) {
656 cnp
->cn_flags
|= ISSYMLINK
;
661 * Check for bogus trailing slashes.
663 if (trailing_slash
) {
664 if (dp
->v_type
!= VDIR
) {
673 * Not a symbolic link. If more pathname,
674 * continue at next component, else return.
676 if (*ndp
->ni_next
== '/') {
677 cnp
->cn_nameptr
= ndp
->ni_next
+ 1;
679 while (*cnp
->cn_nameptr
== '/') {
683 vnode_put(ndp
->ni_dvp
);
685 cp
= cnp
->cn_nameptr
;
695 * Disallow directory write attempts on read-only file systems.
698 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
702 if (cnp
->cn_flags
& SAVESTART
) {
704 * note that we already hold a reference
705 * on both dp and ni_dvp, but for some reason
706 * can't get another one... in this case we
707 * need to do vnode_put on dp in 'bad2'
709 if ( (vnode_get(ndp
->ni_dvp
)) ) {
713 ndp
->ni_startdir
= ndp
->ni_dvp
;
715 if (!wantparent
&& ndp
->ni_dvp
)
716 vnode_put(ndp
->ni_dvp
);
718 if (cnp
->cn_flags
& AUDITVNPATH1
)
719 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
720 else if (cnp
->cn_flags
& AUDITVNPATH2
)
721 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
724 kdebug_lookup(dp
, cnp
);
730 * A degenerate name (e.g. / or "") which is a way of
731 * talking about a directory, e.g. like "/." or ".".
733 if (dp
->v_type
!= VDIR
) {
737 if (cnp
->cn_nameiop
!= LOOKUP
) {
743 * note that we already hold a reference
744 * on dp, but for some reason can't
745 * get another one... in this case we
746 * need to do vnode_put on dp in 'bad'
748 if ( (vnode_get(dp
)) ) {
754 cnp
->cn_flags
&= ~ISDOTDOT
;
755 cnp
->cn_flags
|= ISLASTCN
;
759 if (cnp
->cn_flags
& AUDITVNPATH1
)
760 AUDIT_ARG(vnpath
, dp
, ARG_VNODE1
);
761 else if (cnp
->cn_flags
& AUDITVNPATH2
)
762 AUDIT_ARG(vnpath
, dp
, ARG_VNODE2
);
763 if (cnp
->cn_flags
& SAVESTART
)
764 panic("lookup: SAVESTART");
768 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
769 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
770 unlock_fsnode(ndp
->ni_dvp
, NULL
);
773 vnode_put(ndp
->ni_dvp
);
779 kdebug_lookup(dp
, cnp
);
783 if ((cnp
->cn_flags
& FSNODELOCKHELD
)) {
784 cnp
->cn_flags
&= ~FSNODELOCKHELD
;
785 unlock_fsnode(ndp
->ni_dvp
, NULL
);
792 kdebug_lookup(dp
, cnp
);
797 * relookup - lookup a path name component
798 * Used by lookup to re-aquire things.
801 relookup(dvp
, vpp
, cnp
)
802 struct vnode
*dvp
, **vpp
;
803 struct componentname
*cnp
;
805 struct vnode
*dp
= 0; /* the directory we are searching */
806 int wantparent
; /* 1 => wantparent or lockparent flag */
807 int rdonly
; /* lookup read-only flag bit */
809 #ifdef NAMEI_DIAGNOSTIC
810 int i
, newhash
; /* DEBUG: check name hash */
811 char *cp
; /* DEBUG: check name ptr/len */
813 vfs_context_t ctx
= cnp
->cn_context
;;
816 * Setup: break out flag bits into variables.
818 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
819 rdonly
= cnp
->cn_flags
& RDONLY
;
820 cnp
->cn_flags
&= ~ISSYMLINK
;
822 if (cnp
->cn_flags
& NOCACHE
)
823 cnp
->cn_flags
&= ~MAKEENTRY
;
825 cnp
->cn_flags
|= MAKEENTRY
;
830 * Check for degenerate name (e.g. / or "")
831 * which is a way of talking about a directory,
832 * e.g. like "/." or ".".
834 if (cnp
->cn_nameptr
[0] == '\0') {
835 if (cnp
->cn_nameiop
!= LOOKUP
|| wantparent
) {
839 if (dp
->v_type
!= VDIR
) {
843 if ( (vnode_get(dp
)) ) {
849 if (cnp
->cn_flags
& SAVESTART
)
850 panic("lookup: SAVESTART");
854 * We now have a segment name to search for, and a directory to search.
856 if ( (error
= VNOP_LOOKUP(dp
, vpp
, cnp
, ctx
)) ) {
857 if (error
!= EJUSTRETURN
)
861 panic("leaf should be empty");
864 * If creating and at end of pathname, then can consider
865 * allowing file to be created.
872 * We return with ni_vp NULL to indicate that the entry
873 * doesn't currently exist, leaving a pointer to the
874 * (possibly locked) directory inode in ndp->ni_dvp.
882 * Check for symbolic link
884 if (dp
->v_type
== VLNK
&& (cnp
->cn_flags
& FOLLOW
))
885 panic ("relookup: symlink found.\n");
889 * Disallow directory write attempts on read-only file systems.
892 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
896 /* ASSERT(dvp == ndp->ni_startdir) */
909 * Free pathname buffer
912 nameidone(struct nameidata
*ndp
)
914 if ((ndp
->ni_cnd
.cn_flags
& FSNODELOCKHELD
)) {
915 ndp
->ni_cnd
.cn_flags
&= ~FSNODELOCKHELD
;
916 unlock_fsnode(ndp
->ni_dvp
, NULL
);
918 if (ndp
->ni_cnd
.cn_flags
& HASBUF
) {
919 char *tmp
= ndp
->ni_cnd
.cn_pnbuf
;
921 ndp
->ni_cnd
.cn_pnbuf
= NULL
;
922 ndp
->ni_cnd
.cn_flags
&= ~HASBUF
;
923 FREE_ZONE(tmp
, ndp
->ni_cnd
.cn_pnlen
, M_NAMEI
);
931 kdebug_lookup(dp
, cnp
)
933 struct componentname
*cnp
;
935 register unsigned int i
, n
;
936 register int dbg_namelen
;
937 register int save_dbg_namelen
;
938 register char *dbg_nameptr
;
939 long dbg_parms
[NUMPARMS
];
941 static char *dbg_filler
= ">>>>";
943 /* Collect the pathname for tracing */
944 dbg_namelen
= (cnp
->cn_nameptr
- cnp
->cn_pnbuf
) + cnp
->cn_namelen
;
945 dbg_nameptr
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
947 if (dbg_namelen
> sizeof(dbg_parms
))
948 dbg_namelen
= sizeof(dbg_parms
);
949 dbg_nameptr
-= dbg_namelen
;
950 save_dbg_namelen
= dbg_namelen
;
954 while (dbg_namelen
> 0) {
955 if (dbg_namelen
>= 4) {
956 dbg_parms
[i
++] = *(long *)dbg_nameptr
;
957 dbg_nameptr
+= sizeof(long);
958 dbg_namelen
-= sizeof(long);
960 for (n
= 0; n
< dbg_namelen
; n
++)
961 dbg_buf
[n
] = *dbg_nameptr
++;
968 dbg_parms
[i
++] = *(long *)&dbg_buf
[0];
973 while (i
< NUMPARMS
) {
975 dbg_parms
[i
++] = *(long *)dbg_filler
;
981 In the event that we collect multiple, consecutive pathname
982 entries, we must mark the start of the path's string.
984 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_START
,
985 (unsigned int)dp
, dbg_parms
[0], dbg_parms
[1], dbg_parms
[2], 0);
987 for (dbg_namelen
= save_dbg_namelen
-12, i
=3;
989 dbg_namelen
-=(4 * sizeof(long)), i
+= 4)
991 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
,36)) | DBG_FUNC_NONE
,
992 dbg_parms
[i
], dbg_parms
[i
+1], dbg_parms
[i
+2], dbg_parms
[i
+3], 0);