]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
821bd1b66eac3d028ccc5602ff07237738ea1448
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
, kDefault
, &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
);
443 retval
= hfs_vget_catinfo(parent_vp
, &catInfo
, kDefault
, &target_vp
);
447 CLEAN_CATALOGDATA(&catInfo
.nodeData
); /* Should do nothing */
450 cnp
->cn_flags
|= SAVENAME
;
452 VOP_UNLOCK(parent_vp
, 0, p
);
455 /* Finished...all is well, goto the end */
459 * Step through the translation in the name. We do not `vput' the
460 * directory because we may need it again if a symbolic link
461 * is relative to the current directory. Instead we save it
462 * unlocked as "tparent_vp". We must get the target hfsnode before unlocking
463 * the directory to insure that the hfsnode will not be removed
464 * before we get it. We prevent deadlock by always fetching
465 * inodes from the root, moving down the directory tree. Thus
466 * when following backward pointers ".." we must unlock the
467 * parent directory before getting the requested directory.
468 * There is a potential race condition here if both the current
469 * and parent directories are removed before the VFS_VGET for the
470 * hfsnode associated with ".." returns. We hope that this occurs
471 * infrequently since we cannot avoid this race condition without
472 * implementing a sophisticated deadlock detection algorithm.
473 * Note also that this simple deadlock detection scheme will not
474 * work if the file system has any hard links other than ".."
475 * that point backwards in the directory structure.
478 tparent_vp
= parent_vp
;
480 VOP_UNLOCK(tparent_vp
, 0, p
); /* race to get the inode */
481 if ((retval
= VFS_VGET(parent_vp
->v_mount
, &nodeID
, &target_vp
))) {
482 vn_lock(tparent_vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
485 if (lockparent
&& (flags
& ISLASTCN
) && (tparent_vp
!= target_vp
) &&
486 (retval
= vn_lock(tparent_vp
, LK_EXCLUSIVE
, p
))) {
492 VREF(parent_vp
); /* we want ourself, ie "." */
493 target_vp
= parent_vp
;
498 * Determine what fork to get, currenty 3 scenarios are supported:
499 * 1. ./foo: if it is a dir, return a VDIR else return data fork
500 * 2. ./foo/.__Fork/data: return data fork
501 * 3. ./foo/.__Fork/rsrc: return resource fork
502 * So the algorithm is:
503 * If the object is a directory
504 * then return a VDIR vnode
505 * else if ISLASTCN is true
506 * then get the vnode with forkType=kDataFork
508 * compare with the remaining cnp buffer with "/.__Fork/"
510 * then compare string after that with either 'data' or 'rsrc'
513 * 'consume' rest of cnp, setting appropriate values and flags
514 * return vnode depending on match
518 * illegal path after a file object
521 mode
= (mode_t
)(catInfo
.nodeData
.cnd_mode
);
523 if (catInfo
.nodeData
.cnd_type
== kCatalogFolderNode
) {
524 forkType
= kDirectory
; /* Really ignored */
526 else if ((mode
& IFMT
) == IFLNK
) {
527 forkType
= kDataFork
;
528 } /* After this point, nodeType should be a file */
529 else if (flags
& ISLASTCN
) { /* Create a default fork */
530 forkType
= kDataFork
;
532 else { /* determine what fork was specified */
533 forkType
= GetForkFromName(cnp
);
534 flags
|= ISLASTCN
; /* To know to unlock the parent if needed */
538 /* If couldn't determine what type of fork, leave */
539 if (forkType
== kUndefinedFork
) {
544 /* Get the vnode now that what type of fork is known */
545 DBG_ASSERT((forkType
==kDirectory
) || (forkType
==kDataFork
) || (forkType
==kRsrcFork
));
546 retval
= hfs_vget_catinfo(tparent_vp
, &catInfo
, forkType
, &target_vp
);
547 if (retval
!= E_NONE
)
550 if (!lockparent
|| !(flags
& ISLASTCN
))
551 VOP_UNLOCK(tparent_vp
, 0, p
);
553 CLEAN_CATALOGDATA(&catInfo
.nodeData
);
559 * Insert name in cache if wanted.
560 * Names with composed chars are not put into the name cache.
561 * Resource forks are not entered in the name cache. This
564 if ((cnp
->cn_flags
& MAKEENTRY
)
565 && (cnp
->cn_namelen
== catInfo
.nodeData
.cnm_length
)
566 && ((H_FORKTYPE(VTOH(target_vp
))) != kRsrcFork
)) {
568 * XXX SER - Might be good idea to bcopy(catInfo.nodeData.fsspec.name, cnp->cn_nameptr)
569 * to "normalize" the name cache. This will avoid polluting the name cache with
570 * names that are different in case, and allow negative caching
572 cache_enter(parent_vp
, target_vp
, cnp
);
577 }; /* else found == TRUE */
581 CLEAN_CATALOGDATA(&catInfo
.nodeData
); /* Just to make sure */
582 *ap
->a_vpp
= target_vp
;
584 DBG_VOP_UPDATE_VP(1, *ap
->a_vpp
);
585 //DBG_VOP_LOOKUP_TEST (funcname, cnp, parent_vp, target_vp);
586 //DBG_VOP_LOCKS_TEST(E_NONE);
594 * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
596 * Name caching works as follows:
598 * Names found by directory scans are retained in a cache
599 * for future reference. It is managed LRU, so frequently
600 * used names will hang around. Cache is indexed by hash value
601 * obtained from (vp, name) where vp refers to the directory
604 * If it is a "negative" entry, (i.e. for a name that is known NOT to
605 * exist) the vnode pointer will be NULL.
607 * Upon reaching the last segment of a path, if the reference
608 * is for DELETE, or NOCACHE is set (rewrite), and the
609 * name is located in the cache, it will be dropped.
611 * In hfs, since a name can represent multiple forks, it cannot
612 * be known what fork the name matches, so further checks have to be done.
613 * Currently a policy of first requested, is the one stored, is followed.
615 * SER XXX If this proves inadequate maybe we can munge the name to contain a fork reference
616 * like foo -> foo.d for the data fork.
621 struct vop_lookup_args
/* {
623 struct vnode **a_vpp;
624 struct componentname *a_cnp;
631 struct vnode
**vpp
= ap
->a_vpp
;
632 struct componentname
*cnp
= ap
->a_cnp
;
633 struct ucred
*cred
= cnp
->cn_cred
;
634 int flags
= cnp
->cn_flags
;
635 struct proc
*p
= cnp
->cn_proc
;
637 u_int32_t vpid
; /* capability number of vnode */
638 DBG_FUNC_NAME("cache_lookup");
639 DBG_VOP_LOCKS_DECL(2);
640 DBG_VOP_LOCKS_INIT(0,ap
->a_dvp
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_IGNORE
, VOPDBG_POS
);
641 DBG_VOP_LOCKS_INIT(1,*ap
->a_vpp
, VOPDBG_IGNORE
, VOPDBG_LOCKED
, VOPDBG_IGNORE
, VOPDBG_POS
);
642 DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n"));
643 DBG_VOP_CONT(("\tTarget: "));DBG_VOP_PRINT_CPN_INFO(ap
->a_cnp
);DBG_VOP_CONT(("\n"));
644 DBG_HFS_NODE_CHECK(ap
->a_dvp
);
648 lockparent
= flags
& LOCKPARENT
;
650 if (vdp
->v_type
!= VDIR
)
653 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
654 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
657 error
= VOP_ACCESS(vdp
, VEXEC
, cred
, cnp
->cn_proc
);
663 * Lookup an entry in the cache
664 * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
665 * returned. If the lookup determines that the name does not exist
666 * (negative cacheing), a status of ENOENT is returned. If the lookup
667 * fails, a status of zero is returned.
669 error
= cache_lookup(vdp
, vpp
, cnp
);
671 if (error
== 0) { /* Unsuccessfull */
672 DBG_VOP(("\tWas not in name cache\n"));
673 error
= hfs_lookup(ap
);
678 * If this is a hard-link vnode then we need to update
679 * the name (of the link) and update the parent ID. This
680 * enables getattrlist calls to return correct link info.
682 hp
= VTOH(*ap
->a_vpp
);
683 if ((flags
& ISLASTCN
) && (hp
->h_meta
->h_metaflags
& IN_DATANODE
)) {
684 H_DIRID(hp
) = H_FILEID(VTOH(ap
->a_dvp
));
685 hfs_set_metaname(cnp
->cn_nameptr
, hp
->h_meta
, HTOHFS(hp
));
691 DBG_VOP(("\tName was found in the name cache"));
692 if (error
== ENOENT
) {
693 DBG_VOP_CONT((" though it was a NEGATIVE HIT\n"));
696 DBG_VOP_CONT(("\n"));
700 * If this is a hard-link vnode then we need to update
701 * the name (of the link) and update the parent ID. This
702 * enables getattrlist calls to return correct link info.
705 if ((flags
& ISLASTCN
) && (hp
->h_meta
->h_metaflags
& IN_DATANODE
)) {
706 H_DIRID(hp
) = H_FILEID(VTOH(vdp
));
707 hfs_set_metaname(cnp
->cn_nameptr
, hp
->h_meta
, HTOHFS(hp
));
711 /* We have a name that matched */
715 if (pdp
== vdp
) { /* lookup on "." */
718 } else if (flags
& ISDOTDOT
) {
720 * Carefull on the locking policy,
721 * remember we always lock from parent to child, so have
722 * to release lock on child before trying to lock parent
723 * then regain lock if needed
725 VOP_UNLOCK(pdp
, 0, p
);
726 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
727 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
728 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
729 } else if ((! (flags
& ISLASTCN
)) && (vdp
->v_type
== VREG
) &&
730 (GetForkFromName(cnp
) != kDataFork
)) {
732 * We only store data forks in the name cache.
736 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
737 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
738 VOP_UNLOCK(pdp
, 0, p
);
741 * Check that the capability number did not change
742 * while we were waiting for the lock.
745 if (vpid
== vdp
->v_id
)
746 return (0); /* HERE IS THE NORMAL EXIT FOR CACHE LOOKUP!!!! */
748 * The above is the NORMAL exit, after this point is an error
752 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
753 VOP_UNLOCK(pdp
, 0, p
);
755 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
761 return (hfs_lookup(ap
));
765 * Parses a componentname and sees if the remaining path
766 * contains a hfs named fork specifier. If it does set the
767 * componentname to consume the rest of the path, and
768 * return the forkType
771 u_int16_t
GetForkFromName(struct componentname
*cnp
)
773 u_int16_t forkType
= kUndefinedFork
;
774 char *tcp
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
776 if (bcmp(tcp
, _PATH_FORKSPECIFIER
, sizeof(_PATH_FORKSPECIFIER
) - 1) == 0) {
777 /* Its a HFS fork, so far */
778 tcp
+= (sizeof(_PATH_FORKSPECIFIER
) - 1);
779 if (bcmp(tcp
, _PATH_DATANAME
, sizeof(_PATH_DATANAME
)) == 0) {
780 forkType
= kDataFork
;
781 cnp
->cn_consume
= sizeof(_PATH_FORKSPECIFIER
) + sizeof(_PATH_DATANAME
) - 2;
783 else if (bcmp(tcp
, _PATH_RSRCNAME
, sizeof(_PATH_RSRCNAME
)) == 0) {
784 forkType
= kRsrcFork
;
785 cnp
->cn_consume
= sizeof(_PATH_FORKSPECIFIER
) + sizeof(_PATH_RSRCNAME
) - 2;
790 /* XXX SER For backwards compatability...keep it */
791 if (forkType
== kUndefinedFork
) {
792 tcp
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
793 if (bcmp(tcp
, gHFSForkIdentStr
, sizeof(gHFSForkIdentStr
) - 1) == 0) {
794 /* Its a HFS fork, so far */
795 tcp
+= (sizeof(gHFSForkIdentStr
) - 1);
796 if (bcmp(tcp
, gDataForkNameStr
, sizeof(gDataForkNameStr
)) == 0) {
797 forkType
= kDataFork
;
798 cnp
->cn_consume
= sizeof(gHFSForkIdentStr
) + sizeof(gDataForkNameStr
) - 2;
800 else if (bcmp(tcp
, gRsrcForkNameStr
, sizeof(gRsrcForkNameStr
)) == 0) {
801 forkType
= kRsrcFork
;
802 cnp
->cn_consume
= sizeof(gHFSForkIdentStr
) + sizeof(gRsrcForkNameStr
) - 2;
810 #if DBG_VOP_TEST_LOCKS
812 void DbgLookupTest( char *funcname
, struct componentname
*cnp
, struct vnode
*dvp
, struct vnode
*vp
)
814 if (! (hfs_dbg_lookup
|| hfs_dbg_all
))
819 if (lockstatus(&VTOH(dvp
)->h_lock
)) {
820 DBG_LOOKUP (("%s: Parent vnode exited LOCKED", funcname
));
823 DBG_LOOKUP (("%s: Parent vnode exited UNLOCKED", funcname
));
830 DBG_LOOKUP (("%s: Target and Parent are the same", funcname
));
833 if (lockstatus(&VTOH(vp
)->h_lock
)) {
834 DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname
));
837 DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname
));
840 DBG_LOOKUP (("%s: Found vnode 0x%x has vtype of %d\n ", funcname
, (u_int
)vp
, vp
->v_type
));
843 DBG_LOOKUP (("%s: Found vnode exited NULL\n", funcname
));
848 #endif /* DBG_VOP_TEST_LOCKS */