]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
2 * Copyright (c) 1999 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@
23 * Copyright (c) 1989, 1993
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)hfs_lookup.c 1.0
60 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
62 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
63 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
66 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
68 * MODIFICATION HISTORY:
69 * 21-May-1999 Don Brady Add support for HFS rooting.
70 * 25-Feb-1999 Clark Warner Fixed the error case of VFS_VGGET when
71 * processing DotDot (..) to relock parent
72 * 23-Feb-1999 Pat Dirks Finish cleanup around Don's last fix in "." and ".." handling.
73 * 11-Nov-1998 Don Brady Take out VFS_VGET that got added as part of previous fix.
74 * 14-Oct-1998 Don Brady Fix locking policy volation in hfs_lookup for ".." case
76 * 4-Jun-1998 Pat Dirks Split off from hfs_vnodeops.c
79 #include <sys/param.h>
80 #include <sys/namei.h>
83 #include <sys/mount.h>
84 #include <sys/vnode.h>
85 #include <sys/malloc.h>
86 #include <sys/paths.h>
90 #include "hfscommon/headers/FileMgrInternal.h"
92 u_int16_t
GetForkFromName(struct componentname
*cnp
);
93 int hfs_vget_sibling(struct vnode
*vdp
, u_int16_t forkType
, struct vnode
**vpp
);
94 int hfs_vget_catinfo(struct vnode
*parent_vp
, struct hfsCatalogInfo
*catInfo
, u_int32_t forkType
, struct vnode
**target_vpp
);
97 * XXX SER fork strings.
98 * Put these someplace better
100 #define gHFSForkIdentStr "/"
101 #define gDataForkNameStr "data"
102 #define gRsrcForkNameStr "rsrc"
105 #if DBG_VOP_TEST_LOCKS
106 extern void DbgVopTest(int maxSlots
, int retval
, VopDbgStoreRec
*VopDbgStore
, char *funcname
);
109 /*****************************************************************************
111 * Operations on vnodes
113 *****************************************************************************/
118 * Convert a component of a pathname into a pointer to a locked hfsnode.
119 * This is a very central and rather complicated routine.
120 * If the file system is not maintained in a strict tree hierarchy,
121 * this can result in a deadlock situation (see comments in code below).
123 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
124 * on whether the name is to be looked up, created, renamed, or deleted.
125 * When CREATE, RENAME, or DELETE is specified, information usable in
126 * creating, renaming, or deleting a directory entry may be calculated.
127 * Notice that these are the only operations that can affect the directory of the target.
129 * If flag has LOCKPARENT or'ed into it and the target of the pathname
130 * exists, lookup returns both the target and its parent directory locked.
131 * When creating or renaming and LOCKPARENT is specified, the target may
132 * not be ".". When deleting and LOCKPARENT is specified, the target may
133 * be "."., but the caller must check to ensure it does an vrele and vput
134 * instead of two vputs.
136 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
137 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
138 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
139 * routine wants to access the parent of the target, locked or unlocked.
141 * Keeping the parent locked as long as possible protects from other processes
142 * looking up the same item, so it has to be locked until the hfsnode is totally finished
144 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
145 * filesystem employs the generic hfs_cache_lookup() as VOP_LOOKUP
148 * hfs_cache_lookup() performs the following for us:
149 * check that it is a directory
150 * check accessibility of directory
151 * check for modification attempts on read-only mounts
152 * if name found in cache
153 * if at end of path and deleting or creating
157 * return VOP_CACHEDLOOKUP()
159 * Overall outline of hfs_lookup:
161 * handle simple cases of . and ..
162 * search for name in directory, to found or notfound
164 * if creating, return locked directory, leaving info on available slots
167 * if at end of path and deleting, return information to allow delete
168 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
169 * inode and return info to allow rewrite
170 * if not at end, add name to cache; if at end and neither creating
171 * nor deleting, add name to cache
175 * Lookup *nm in directory *pvp, return it in *a_vpp.
176 * **a_vpp is held on exit.
177 * We create a hfsnode for the file, but we do NOT open the file here.
182 IN struct vnode *dvp - Parent node of file;
183 INOUT struct vnode **vpp - node of target file, its a new node if the target vnode did not exist;
184 IN struct componentname *cnp - Name of file;
186 * When should we lock parent_hp in here ??
191 struct vop_cachedlookup_args
/* {
193 struct vnode **a_vpp;
194 struct componentname *a_cnp;
197 struct vnode
*parent_vp
;
198 struct vnode
*target_vp
;
199 struct vnode
*tparent_vp
;
200 struct hfsnode
*parent_hp
; /* parent */
201 struct componentname
*cnp
;
204 struct hfsCatalogInfo catInfo
;
210 int lockparent
; /* !0 => lockparent flag is set */
211 int wantparent
; /* !0 => wantparent or lockparent flag */
214 u_char isDot
, isDotDot
, found
;
215 DBG_FUNC_NAME("lookup");
216 DBG_VOP_LOCKS_DECL(2);
217 DBG_VOP_LOCKS_INIT(0,ap
->a_dvp
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_IGNORE
, VOPDBG_POS
);
218 DBG_VOP_LOCKS_INIT(1,*ap
->a_vpp
, VOPDBG_IGNORE
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_POS
);
219 DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n"));
220 DBG_HFS_NODE_CHECK(ap
->a_dvp
);
226 INIT_CATALOGDATA(&catInfo
.nodeData
, 0);
227 parent_vp
= ap
->a_dvp
;
229 parent_hp
= VTOH(parent_vp
); /* parent */
231 targetLen
= cnp
->cn_namelen
;
232 nameiop
= cnp
->cn_nameiop
;
235 lockparent
= cnp
->cn_flags
& LOCKPARENT
;
236 wantparent
= cnp
->cn_flags
& (LOCKPARENT
|WANTPARENT
);
237 flags
= cnp
->cn_flags
;
238 parent_id
= H_FILEID(parent_hp
);
244 forkType
= kUndefinedFork
;
248 * We now have a segment name to search for, and a directory to search.
253 * First check to see if it is a . or .., else look it up.
256 if (flags
& ISDOTDOT
) { /* Wanting the parent */
258 found
= TRUE
; /* .. is always defined */
259 nodeID
= H_DIRID(parent_hp
);
260 } /* Wanting ourselves */
261 else if ((cnp
->cn_nameptr
[0] == '.') && (targetLen
== 1)) {
263 found
= TRUE
; /* We always know who we are */
265 else { /* Wanting something else */
266 catInfo
.hint
= kNoHint
;
268 /* lock catalog b-tree */
269 retval
= hfs_metafilelocking(VTOHFS(parent_vp
), kHFSCatalogFileID
, LK_SHARED
, p
);
273 retval
= hfs_getcatalog (VTOVCB(parent_vp
), parent_id
, cnp
->cn_nameptr
, targetLen
, &catInfo
);
275 /* unlock catalog b-tree */
276 (void) hfs_metafilelocking(VTOHFS(parent_vp
), kHFSCatalogFileID
, LK_RELEASE
, p
);
278 if (retval
== E_NONE
)
284 * At this point we know IF we have a valid dir/name.
291 * This is a non-existing entry
293 * If creating, and at end of pathname and current
294 * directory has not been removed, then can consider
295 * allowing file to be created.
297 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
298 (nameiop
== DELETE
&&
299 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
300 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
301 (flags
& ISLASTCN
)) {
303 * Access for write is interpreted as allowing
304 * creation of files in the directory.
306 retval
= VOP_ACCESS(parent_vp
, VWRITE
, cred
, cnp
->cn_proc
);
310 cnp
->cn_flags
|= SAVENAME
;
312 VOP_UNLOCK(parent_vp
, 0, p
);
313 retval
= EJUSTRETURN
;
318 * Insert name into cache (as non-existent) if appropriate.
322 * XXX SER - Here we would store the name in cache as non-existant if not trying to create it, but,
323 * the name cache IS case-sensitive, thus maybe showing a negative hit, when the name
324 * is only different by case. So hfs does not support negative caching. Something to look at.
325 * (See radar 2293594 for a failed example)
326 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
327 cache_enter(parent_vp, *vpp, cnp);
334 * We have found an entry
336 * Here we have to decide what type of vnode to create.
337 * There are 3 type of objects that are given:
338 * 1. '.': return the same dp
339 * 2. '..' return the parent of dp, always a VDIR
340 * 3. catinfo rec: return depending on type:
341 * A. VDIR, nodeType is kCatalogFolderNode
342 * B. VLINK nodeType is kCatalogFileNode, the mode is IFLNK (esp. if it is a link to a directory e.g. bar/link/foo)
343 * C. VREG, nodeType is kCatalogFileNode, forkType at this point is unknown
344 * To determine the forkType, we can use this algorithm (\0 in the strings mean the NULL character):
345 * a. forkType is kDataType iff ISLASTCN is set (as in the case of the default fork e.g. data/foo).
346 * b. forkType is kDataType iff ISLASTCN is not set and the namePtr is followed by "/?AppleHFSFork/data\0"
347 * c. forkType is kRsrcType iff ISLASTCN is not set and the namePtr is followed by "/?AppleHFSFork/rsrc\0"
348 * If the latter two are correct, then we 'consume' the remaining of the name buffer
349 * and set the cnp as appropriate.
350 * Anything else returns an retval
355 * If deleting, and at end of pathname, return
356 * parameters which can be used to remove file.
357 * If the wantparent flag isn't set, we return only
358 * the directory (in ndp->ndvp), otherwise we go
359 * on and lock the hfsnode, being careful with ".".
361 * Forks cannot be deleted so scan-ahead is illegal, so just return the default fork
363 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
365 * Write access to directory required to delete files.
367 retval
= VOP_ACCESS(parent_vp
, VWRITE
, cred
, cnp
->cn_proc
);
371 if (isDot
) { /* Want to return ourselves */
373 target_vp
= parent_vp
;
377 retval
= VFS_VGET(parent_vp
->v_mount
, &nodeID
, &target_vp
);
382 retval
= hfs_vget_catinfo(parent_vp
, &catInfo
, kAnyFork
, &target_vp
);
385 CLEAN_CATALOGDATA(&catInfo
.nodeData
);
390 * If directory is "sticky", then user must own
391 * the directory, or the file in it, else she
392 * may not delete it (unless she's root). This
393 * implements append-only directories.
395 if ((parent_hp
->h_meta
->h_mode
& ISVTX
) &&
396 (cred
->cr_uid
!= 0) &&
397 (cred
->cr_uid
!= parent_hp
->h_meta
->h_uid
) &&
398 (target_vp
->v_type
!= VLNK
) &&
399 (hfs_owner_rights(target_vp
, cred
, p
, false))) {
406 * If this is a link node then we need to save the name
407 * (of the link) so we can delete it from the catalog b-tree.
408 * In this case, hfs_remove will then free the component name.
410 if (target_vp
&& (VTOH(target_vp
)->h_meta
->h_metaflags
& IN_DATANODE
))
411 cnp
->cn_flags
|= SAVENAME
;
415 VOP_UNLOCK(parent_vp
, 0, p
);
420 * If rewriting 'RENAME', return the hfsnode and the
421 * information required to rewrite the present directory
423 if (nameiop
== RENAME
&& wantparent
&& (cnp
->cn_flags
& ISLASTCN
)) {
425 if ((retval
= VOP_ACCESS(parent_vp
, VWRITE
, cred
, cnp
->cn_proc
)) != 0)
429 * Careful about locking second inode.
430 * This can only occur if the target is ".". like 'mv foo/bar foo/.'
437 retval
= VFS_VGET(parent_vp
->v_mount
, &nodeID
, &target_vp
);
442 /* If then name differs in case, then act like it does not exist
443 * This allows renaming foo->Foo
444 * Exclude length difference due to compose/decompe issues.
446 if ((cnp
->cn_namelen
== catInfo
.nodeData
.cnm_length
) &&
447 strncmp(cnp
->cn_nameptr
, catInfo
.nodeData
.cnm_nameptr
, targetLen
)) {
449 VOP_UNLOCK(parent_vp
, 0, p
);
450 retval
= EJUSTRETURN
;
454 retval
= hfs_vget_catinfo(parent_vp
, &catInfo
, kAnyFork
, &target_vp
);
458 CLEAN_CATALOGDATA(&catInfo
.nodeData
); /* Should do nothing */
461 cnp
->cn_flags
|= SAVENAME
;
463 VOP_UNLOCK(parent_vp
, 0, p
);
466 /* Finished...all is well, goto the end */
470 * Step through the translation in the name. We do not `vput' the
471 * directory because we may need it again if a symbolic link
472 * is relative to the current directory. Instead we save it
473 * unlocked as "tparent_vp". We must get the target hfsnode before unlocking
474 * the directory to insure that the hfsnode will not be removed
475 * before we get it. We prevent deadlock by always fetching
476 * inodes from the root, moving down the directory tree. Thus
477 * when following backward pointers ".." we must unlock the
478 * parent directory before getting the requested directory.
479 * There is a potential race condition here if both the current
480 * and parent directories are removed before the VFS_VGET for the
481 * hfsnode associated with ".." returns. We hope that this occurs
482 * infrequently since we cannot avoid this race condition without
483 * implementing a sophisticated deadlock detection algorithm.
484 * Note also that this simple deadlock detection scheme will not
485 * work if the file system has any hard links other than ".."
486 * that point backwards in the directory structure.
489 tparent_vp
= parent_vp
;
491 VOP_UNLOCK(tparent_vp
, 0, p
); /* race to get the inode */
492 if ((retval
= VFS_VGET(parent_vp
->v_mount
, &nodeID
, &target_vp
))) {
493 vn_lock(tparent_vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
496 if (lockparent
&& (flags
& ISLASTCN
) && (tparent_vp
!= target_vp
) &&
497 (retval
= vn_lock(tparent_vp
, LK_EXCLUSIVE
, p
))) {
503 VREF(parent_vp
); /* we want ourself, ie "." */
504 target_vp
= parent_vp
;
509 * Determine what fork to get, currenty 3 scenarios are supported:
510 * 1. ./foo: if it is a dir, return a VDIR else return data fork
511 * 2. ./foo/.__Fork/data: return data fork
512 * 3. ./foo/.__Fork/rsrc: return resource fork
513 * So the algorithm is:
514 * If the object is a directory
515 * then return a VDIR vnode
516 * else if ISLASTCN is true
517 * then get the vnode with forkType=kDataFork
519 * compare with the remaining cnp buffer with "/.__Fork/"
521 * then compare string after that with either 'data' or 'rsrc'
524 * 'consume' rest of cnp, setting appropriate values and flags
525 * return vnode depending on match
529 * illegal path after a file object
532 mode
= (mode_t
)(catInfo
.nodeData
.cnd_mode
);
534 if (catInfo
.nodeData
.cnd_type
== kCatalogFolderNode
) {
535 forkType
= kDirectory
; /* Really ignored */
537 else if ((mode
& IFMT
) == IFLNK
) {
538 forkType
= kDataFork
;
539 } /* After this point, nodeType should be a file */
540 else if (flags
& ISLASTCN
) { /* Create a default fork */
541 forkType
= kDataFork
;
543 else { /* determine what fork was specified */
544 forkType
= GetForkFromName(cnp
);
545 flags
|= ISLASTCN
; /* To know to unlock the parent if needed */
549 /* If couldn't determine what type of fork, leave */
550 if (forkType
== kUndefinedFork
) {
555 /* Get the vnode now that what type of fork is known */
556 DBG_ASSERT((forkType
==kDirectory
) || (forkType
==kDataFork
) || (forkType
==kRsrcFork
));
557 retval
= hfs_vget_catinfo(tparent_vp
, &catInfo
, forkType
, &target_vp
);
558 if (retval
!= E_NONE
)
561 if (!lockparent
|| !(flags
& ISLASTCN
))
562 VOP_UNLOCK(tparent_vp
, 0, p
);
564 CLEAN_CATALOGDATA(&catInfo
.nodeData
);
570 * Insert name in cache if wanted.
571 * Names with composed chars are not put into the name cache.
572 * Resource forks are not entered in the name cache. This
575 if ((cnp
->cn_flags
& MAKEENTRY
)
576 && (cnp
->cn_namelen
== catInfo
.nodeData
.cnm_length
)
577 && ((H_FORKTYPE(VTOH(target_vp
))) != kRsrcFork
)) {
579 * XXX SER - Might be good idea to bcopy(catInfo.nodeData.fsspec.name, cnp->cn_nameptr)
580 * to "normalize" the name cache. This will avoid polluting the name cache with
581 * names that are different in case, and allow negative caching
583 cache_enter(parent_vp
, target_vp
, cnp
);
588 }; /* else found == TRUE */
592 CLEAN_CATALOGDATA(&catInfo
.nodeData
); /* Just to make sure */
593 *ap
->a_vpp
= target_vp
;
595 DBG_VOP_UPDATE_VP(1, *ap
->a_vpp
);
596 //DBG_VOP_LOOKUP_TEST (funcname, cnp, parent_vp, target_vp);
597 //DBG_VOP_LOCKS_TEST(E_NONE);
605 * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
607 * Name caching works as follows:
609 * Names found by directory scans are retained in a cache
610 * for future reference. It is managed LRU, so frequently
611 * used names will hang around. Cache is indexed by hash value
612 * obtained from (vp, name) where vp refers to the directory
615 * If it is a "negative" entry, (i.e. for a name that is known NOT to
616 * exist) the vnode pointer will be NULL.
618 * Upon reaching the last segment of a path, if the reference
619 * is for DELETE, or NOCACHE is set (rewrite), and the
620 * name is located in the cache, it will be dropped.
622 * In hfs, since a name can represent multiple forks, it cannot
623 * be known what fork the name matches, so further checks have to be done.
624 * Currently a policy of first requested, is the one stored, is followed.
626 * SER XXX If this proves inadequate maybe we can munge the name to contain a fork reference
627 * like foo -> foo.d for the data fork.
632 struct vop_lookup_args
/* {
634 struct vnode **a_vpp;
635 struct componentname *a_cnp;
642 struct vnode
**vpp
= ap
->a_vpp
;
643 struct componentname
*cnp
= ap
->a_cnp
;
644 struct ucred
*cred
= cnp
->cn_cred
;
645 int flags
= cnp
->cn_flags
;
646 struct proc
*p
= cnp
->cn_proc
;
648 u_int32_t vpid
; /* capability number of vnode */
649 DBG_FUNC_NAME("cache_lookup");
650 DBG_VOP_LOCKS_DECL(2);
651 DBG_VOP_LOCKS_INIT(0,ap
->a_dvp
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_IGNORE
, VOPDBG_POS
);
652 DBG_VOP_LOCKS_INIT(1,*ap
->a_vpp
, VOPDBG_IGNORE
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_POS
);
653 DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n"));
654 DBG_VOP_CONT(("\tTarget: "));DBG_VOP_PRINT_CPN_INFO(ap
->a_cnp
);DBG_VOP_CONT(("\n"));
655 DBG_HFS_NODE_CHECK(ap
->a_dvp
);
659 lockparent
= flags
& LOCKPARENT
;
661 if (vdp
->v_type
!= VDIR
)
664 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
665 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
668 error
= VOP_ACCESS(vdp
, VEXEC
, cred
, cnp
->cn_proc
);
674 * Lookup an entry in the cache
675 * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
676 * returned. If the lookup determines that the name does not exist
677 * (negative cacheing), a status of ENOENT is returned. If the lookup
678 * fails, a status of zero is returned.
680 error
= cache_lookup(vdp
, vpp
, cnp
);
682 if (error
== 0) { /* Unsuccessfull */
683 DBG_VOP(("\tWas not in name cache\n"));
684 error
= hfs_lookup(ap
);
689 * If this is a hard-link vnode then we need to update
690 * the name (of the link) and update the parent ID. This
691 * enables getattrlist calls to return correct link info.
693 hp
= VTOH(*ap
->a_vpp
);
694 if ((flags
& ISLASTCN
) && (hp
->h_meta
->h_metaflags
& IN_DATANODE
)) {
695 H_DIRID(hp
) = H_FILEID(VTOH(ap
->a_dvp
));
696 hfs_set_metaname(cnp
->cn_nameptr
, hp
->h_meta
, HTOHFS(hp
));
702 DBG_VOP(("\tName was found in the name cache"));
703 if (error
== ENOENT
) {
704 DBG_VOP_CONT((" though it was a NEGATIVE HIT\n"));
707 DBG_VOP_CONT(("\n"));
711 * If this is a hard-link vnode then we need to update
712 * the name (of the link) and update the parent ID. This
713 * enables getattrlist calls to return correct link info.
716 if ((flags
& ISLASTCN
) && (hp
->h_meta
->h_metaflags
& IN_DATANODE
)) {
717 H_DIRID(hp
) = H_FILEID(VTOH(vdp
));
718 hfs_set_metaname(cnp
->cn_nameptr
, hp
->h_meta
, HTOHFS(hp
));
722 /* We have a name that matched */
726 if (pdp
== vdp
) { /* lookup on "." */
729 } else if (flags
& ISDOTDOT
) {
731 * Carefull on the locking policy,
732 * remember we always lock from parent to child, so have
733 * to release lock on child before trying to lock parent
734 * then regain lock if needed
736 VOP_UNLOCK(pdp
, 0, p
);
737 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
738 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
739 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
742 * Check to see if a specific fork is not being requested.
744 * If it is a file and not the last path item
745 * then check if its a proper fork
746 * If it is, check to see if the matched vnode is the same fork
747 * else see if the proper fork exists.
748 * If it does, return that one, else do VOP_CACHEDLOOKUP()
749 * Notice that nothing is done if an undefined fork is named. Just leave and let lookup()
750 * handle strange cases.
752 * XXX SER Notice that when the target is not what was in the name cache,
753 * it is locked, before trying to get its sibling. Could this be a problem since both
754 * siblings can be locked, but not in a determinalistic order????
758 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
759 if ((! error
) && (vdp
->v_type
== VREG
) && (vpid
== vdp
->v_id
)) {
760 if (!(flags
& ISLASTCN
)) {
761 forkType
= GetForkFromName(cnp
);
762 if (forkType
!= kUndefinedFork
) {
764 if (H_FORKTYPE(VTOH(vdp
)) != forkType
) {
765 error
= hfs_vget_sibling(vdp
, forkType
, vpp
);
775 /* Its the last item, so we want the data fork */
776 if (H_FORKTYPE(VTOH(vdp
)) != kDataFork
) {
777 error
= hfs_vget_sibling(vdp
, kDataFork
, vpp
);
786 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
787 VOP_UNLOCK(pdp
, 0, p
);
790 * Check that the capability number did not change
791 * while we were waiting for the lock.
794 if (vpid
== vdp
->v_id
)
795 return (0); /* HERE IS THE NORMAL EXIT FOR CACHE LOOKUP!!!! */
797 * The above is the NORMAL exit, after this point is an error
801 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
802 VOP_UNLOCK(pdp
, 0, p
);
804 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
807 return (hfs_lookup(ap
));
811 * Parses a componentname and sees if the remaining path
812 * contains a hfs named fork specifier. If it does set the
813 * componentname to consume the rest of the path, and
814 * return the forkType
817 u_int16_t
GetForkFromName(struct componentname
*cnp
)
819 u_int16_t forkType
= kUndefinedFork
;
820 char *tcp
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
822 if (bcmp(tcp
, _PATH_FORKSPECIFIER
, sizeof(_PATH_FORKSPECIFIER
) - 1) == 0) {
823 /* Its a HFS fork, so far */
824 tcp
+= (sizeof(_PATH_FORKSPECIFIER
) - 1);
825 if (bcmp(tcp
, _PATH_DATANAME
, sizeof(_PATH_DATANAME
)) == 0) {
826 forkType
= kDataFork
;
827 cnp
->cn_consume
= sizeof(_PATH_FORKSPECIFIER
) + sizeof(_PATH_DATANAME
) - 2;
829 else if (bcmp(tcp
, _PATH_RSRCNAME
, sizeof(_PATH_RSRCNAME
)) == 0) {
830 forkType
= kRsrcFork
;
831 cnp
->cn_consume
= sizeof(_PATH_FORKSPECIFIER
) + sizeof(_PATH_RSRCNAME
) - 2;
836 /* XXX SER For backwards compatability...keep it */
837 if (forkType
== kUndefinedFork
) {
838 tcp
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
839 if (bcmp(tcp
, gHFSForkIdentStr
, sizeof(gHFSForkIdentStr
) - 1) == 0) {
840 /* Its a HFS fork, so far */
841 tcp
+= (sizeof(gHFSForkIdentStr
) - 1);
842 if (bcmp(tcp
, gDataForkNameStr
, sizeof(gDataForkNameStr
)) == 0) {
843 forkType
= kDataFork
;
844 cnp
->cn_consume
= sizeof(gHFSForkIdentStr
) + sizeof(gDataForkNameStr
) - 2;
846 else if (bcmp(tcp
, gRsrcForkNameStr
, sizeof(gRsrcForkNameStr
)) == 0) {
847 forkType
= kRsrcFork
;
848 cnp
->cn_consume
= sizeof(gHFSForkIdentStr
) + sizeof(gRsrcForkNameStr
) - 2;
856 #if DBG_VOP_TEST_LOCKS
858 void DbgLookupTest( char *funcname
, struct componentname
*cnp
, struct vnode
*dvp
, struct vnode
*vp
)
860 if (! (hfs_dbg_lookup
|| hfs_dbg_all
))
865 if (lockstatus(&VTOH(dvp
)->h_lock
)) {
866 DBG_LOOKUP (("%s: Parent vnode exited LOCKED", funcname
));
869 DBG_LOOKUP (("%s: Parent vnode exited UNLOCKED", funcname
));
876 DBG_LOOKUP (("%s: Target and Parent are the same", funcname
));
879 if (lockstatus(&VTOH(vp
)->h_lock
)) {
880 DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname
));
883 DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname
));
886 DBG_LOOKUP (("%s: Found vnode 0x%x has vtype of %d\n ", funcname
, (u_int
)vp
, vp
->v_type
));
889 DBG_LOOKUP (("%s: Found vnode exited NULL\n", funcname
));
894 #endif /* DBG_VOP_TEST_LOCKS */