]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/hfs/hfs_lookup.c
xnu-344.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
index 821bd1b66eac3d028ccc5602ff07237738ea1448..824f615dc70275c11158657f7040fc5d854bcd5b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  *     
  *
  *     hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
- *
- *     MODIFICATION HISTORY:
- *     21-May-1999 Don Brady           Add support for HFS rooting.
- *      25-Feb-1999 Clark Warner       Fixed the error case of VFS_VGGET when
- *                                      processing DotDot (..) to relock parent
- *     23-Feb-1999 Pat Dirks           Finish cleanup around Don's last fix in "." and ".." handling.
- *     11-Nov-1998 Don Brady           Take out VFS_VGET that got added as part of previous fix.
- *     14-Oct-1998 Don Brady           Fix locking policy volation in hfs_lookup for ".." case
- *                                                             (radar #2279902).
- *      4-Jun-1998 Pat Dirks           Split off from hfs_vnodeops.c
  */
+#define LEGACY_FORK_NAMES      0
 
 #include <sys/param.h>
-#include <sys/namei.h>
 #include <sys/buf.h>
 #include <sys/file.h>
 #include <sys/mount.h>
 #include <sys/vnode.h>
+#include <sys/namei.h>
 #include <sys/malloc.h>
 #include <sys/paths.h>
 
-#include       "hfs.h"
-#include       "hfs_dbg.h"
-#include       "hfscommon/headers/FileMgrInternal.h"
+#include "hfs.h"
+#include "hfs_catalog.h"
+#include "hfs_cnode.h"
 
-u_int16_t      GetForkFromName(struct componentname  *cnp);
-int                    hfs_vget_sibling(struct vnode *vdp, u_int16_t forkType, struct vnode **vpp);
-int            hfs_vget_catinfo(struct vnode *parent_vp, struct hfsCatalogInfo *catInfo, u_int32_t forkType, struct vnode **target_vpp);
 
-/*
- *     XXX SER fork strings.
- * Put these someplace better
- */
-#define gHFSForkIdentStr       "/"
-#define gDataForkNameStr       "data"
-#define gRsrcForkNameStr       "rsrc"
+static int forkcomponent(struct componentname *cnp, int *rsrcfork);
 
+#define _PATH_DATAFORKSPEC     "/..namedfork/data"
 
-#if DBG_VOP_TEST_LOCKS
-extern void DbgVopTest(int maxSlots, int retval, VopDbgStoreRec *VopDbgStore, char *funcname);
+#ifdef LEGACY_FORK_NAMES
+#define LEGACY_RSRCFORKSPEC    "/rsrc"
 #endif
 
-/*****************************************************************************
-*
-*      Operations on vnodes
-*
-*****************************************************************************/
-
-
 /*     
  * FROM FREEBSD 3.1
- * Convert a component of a pathname into a pointer to a locked hfsnode.
+ * Convert a component of a pathname into a pointer to a locked cnode.
  * This is a very central and rather complicated routine.
  * If the file system is not maintained in a strict tree hierarchy,
  * this can result in a deadlock situation (see comments in code below).
@@ -139,7 +115,7 @@ extern void DbgVopTest(int maxSlots, int retval, VopDbgStoreRec *VopDbgStore, ch
  * routine wants to access the parent of the target, locked or unlocked.
  *
  * Keeping the parent locked as long as possible protects from other processes
- * looking up the same item, so it has to be locked until the hfsnode is totally finished
+ * looking up the same item, so it has to be locked until the cnode is totally finished
  *
  * This routine is actually used as VOP_CACHEDLOOKUP method, and the
  * filesystem employs the generic hfs_cache_lookup() as VOP_LOOKUP
@@ -166,7 +142,7 @@ extern void DbgVopTest(int maxSlots, int retval, VopDbgStoreRec *VopDbgStore, ch
  * found:
  *     if at end of path and deleting, return information to allow delete
  *     if at end of path and rewriting (RENAME and LOCKPARENT), lock target
- *       inode and return info to allow rewrite
+ *       cnode and return info to allow rewrite
  *     if not at end, add name to cache; if at end and neither creating
  *       nor deleting, add name to cache
  */
@@ -174,18 +150,20 @@ extern void DbgVopTest(int maxSlots, int retval, VopDbgStoreRec *VopDbgStore, ch
 /*     
  *     Lookup *nm in directory *pvp, return it in *a_vpp.
  *     **a_vpp is held on exit.
- *     We create a hfsnode for the file, but we do NOT open the file here.
+ *     We create a cnode for the file, but we do NOT open the file here.
 
 #% lookup      dvp L ? ?
 #% lookup      vpp - L -
 
        IN struct vnode *dvp - Parent node of file;
-       INOUT struct vnode **vpp - node of target file, its a new node if the target vnode did not exist;
+       INOUT struct vnode **vpp - node of target file, its a new node if
+               the target vnode did not exist;
        IN struct componentname *cnp - Name of file;
 
  *     When should we lock parent_hp in here ??
  */
 
+__private_extern__
 int
 hfs_lookup(ap)
        struct vop_cachedlookup_args /* {
@@ -194,397 +172,288 @@ hfs_lookup(ap)
                struct componentname *a_cnp;
        } */ *ap;
 {
-       struct vnode                                    *parent_vp;
-       struct vnode                                    *target_vp;
-       struct vnode                                    *tparent_vp;
-       struct hfsnode                                  *parent_hp;                             /* parent */
-       struct componentname                    *cnp;
-       struct ucred                                    *cred;
-       struct proc                                             *p;
-       struct hfsCatalogInfo                   catInfo;
-       u_int32_t                                               parent_id;
-       u_int32_t                                               nodeID;
-       u_int16_t                                               targetLen;
-       u_int16_t                                               forkType;
-       int                                                     flags;
-       int                                                             lockparent;                                             /* !0 => lockparent flag is set */
-       int                                                             wantparent;                                             /* !0 => wantparent or lockparent flag */
-       int                                                             nameiop;
-       int                                                             retval;
-       u_char                                                  isDot, isDotDot, found;
-       DBG_FUNC_NAME("lookup");
-       DBG_VOP_LOCKS_DECL(2);
-       DBG_VOP_LOCKS_INIT(0,ap->a_dvp, VOPDBG_LOCKED, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS);
-       DBG_VOP_LOCKS_INIT(1,*ap->a_vpp, VOPDBG_IGNORE, VOPDBG_LOCKED, VOPDBG_IGNORE, VOPDBG_POS);
-       DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n"));
-       DBG_HFS_NODE_CHECK(ap->a_dvp);
-
-
-       /*
-        * Do initial setup
-        */
-        INIT_CATALOGDATA(&catInfo.nodeData, 0);
-       parent_vp               = ap->a_dvp;
-       cnp                             = ap->a_cnp;
-       parent_hp               = VTOH(parent_vp);                              /* parent */
-       target_vp               = NULL;
-       targetLen               = cnp->cn_namelen;
-       nameiop                 = cnp->cn_nameiop;
-       cred                    = cnp->cn_cred;
-       p                               = cnp->cn_proc;
-       lockparent              = cnp->cn_flags & LOCKPARENT;
-       wantparent              = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
-       flags                   = cnp->cn_flags;
-       parent_id               = H_FILEID(parent_hp);
-       nodeID                  = kUnknownID;
-       found                   = FALSE;
-       isDot                   = FALSE;
-       isDotDot                = FALSE;
-       retval                  = E_NONE;
-       forkType                = kUndefinedFork;
-
-
-       /*
-        * We now have a segment name to search for, and a directory to search.
-        *
-        */
+       struct vnode *dvp;      /* vnode for directory being searched */
+       struct cnode *dcp;      /* cnode for directory being searched */
+       struct vnode *tvp;      /* target vnode */
+       struct hfsmount *hfsmp;
+       struct componentname *cnp;      
+       struct ucred *cred;
+       struct proc *p;
+       int wantrsrc = 0;
+       int forknamelen = 0;
+       int flags;
+       int wantparent;
+       int nameiop;
+       int retval = 0;
+       int isDot;
+       struct cat_desc desc = {0};
+       struct cat_desc cndesc;
+       struct cat_attr attr;
+       struct cat_fork fork;
+       struct vnode **vpp;
+
+       vpp = ap->a_vpp;
+       cnp = ap->a_cnp;
+       dvp = ap->a_dvp;
+       dcp = VTOC(dvp);
+       hfsmp = VTOHFS(dvp);
+       *vpp = NULL;
+       isDot = FALSE;
+       tvp = NULL;
+       nameiop = cnp->cn_nameiop;
+       cred = cnp->cn_cred;
+       p = cnp->cn_proc;
+       flags = cnp->cn_flags;
+       wantparent = flags & (LOCKPARENT|WANTPARENT);
 
        /*
         * First check to see if it is a . or .., else look it up.
         */
-
-       if (flags & ISDOTDOT) {                                                                 /* Wanting the parent */
-               isDotDot = TRUE;
-               found = TRUE;                                                                           /* .. is always defined */
-               nodeID = H_DIRID(parent_hp);
-       }                                                                                                               /* Wanting ourselves */
-       else if ((cnp->cn_nameptr[0] == '.') && (targetLen == 1)) {
+       if (flags & ISDOTDOT) {         /* Wanting the parent */
+               goto found;     /* .. is always defined */
+       } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
                isDot = TRUE;
-               found = TRUE;                                                                           /* We always know who we are */
-       } 
-       else {                                                                                                  /* Wanting something else */
-               catInfo.hint = kNoHint;
-
-               /* lock catalog b-tree */
-               retval = hfs_metafilelocking(VTOHFS(parent_vp), kHFSCatalogFileID, LK_SHARED, p);
-               if (retval)
-                          goto Err_Exit;
-
-               retval = hfs_getcatalog (VTOVCB(parent_vp), parent_id, cnp->cn_nameptr, targetLen, &catInfo);
-       
-       /* unlock catalog b-tree */
-               (void) hfs_metafilelocking(VTOHFS(parent_vp), kHFSCatalogFileID, LK_RELEASE, p);
-       
-               if (retval == E_NONE)
-                       found = TRUE;
-       };
-
+               goto found;     /* We always know who we are */
+       } else {
+               /* Check fork suffix to see if we want the resource fork */
+               forknamelen = forkcomponent(cnp, &wantrsrc);
 
-       /*
-        * At this point we know IF we have a valid dir/name.
-        */
+               /* No need to go to catalog if there are no children */
+               if (dcp->c_entries == 0)
+                       goto notfound;
 
+               bzero(&cndesc, sizeof(cndesc));
+               cndesc.cd_nameptr = cnp->cn_nameptr;
+               cndesc.cd_namelen = cnp->cn_namelen;
+               cndesc.cd_parentcnid = dcp->c_cnid;
+               cndesc.cd_hint = dcp->c_childhint;
 
-       retval = E_NONE;
-       if (! found) {
-       /*
-        * This is a non-existing entry
-        *
-        * If creating, and at end of pathname and current
-        * directory has not been removed, then can consider
-        * allowing file to be created.
-        */
-       if ((nameiop == CREATE || nameiop == RENAME ||
-                       (nameiop == DELETE &&
-                       (ap->a_cnp->cn_flags & DOWHITEOUT) &&
-                       (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
-                       (flags & ISLASTCN)) {
-               /*
-                * Access for write is interpreted as allowing
-                * creation of files in the directory.
-                */
-               retval = VOP_ACCESS(parent_vp, VWRITE, cred, cnp->cn_proc);
+               /* Lock catalog b-tree */
+               retval = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, p);
                if (retval)
-                       return (retval);
+                          goto exit;
+               retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork);
+               
+               if (retval == 0 && S_ISREG(attr.ca_mode) && attr.ca_blocks < fork.cf_blocks)
+                       panic("hfs_lookup: bad ca_blocks (too small)");
        
-               cnp->cn_flags |= SAVENAME;
-               if (!lockparent)
-                       VOP_UNLOCK(parent_vp, 0, p);
-               retval = EJUSTRETURN;
-               goto Err_Exit;
+               /* Unlock catalog b-tree */
+               (void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, p);
+               if (retval == 0) {
+                       dcp->c_childhint = desc.cd_hint;
+                       goto found;
+               }
+notfound:
+               /*
+                * This is a non-existing entry
+                *
+                * If creating, and at end of pathname and current
+                * directory has not been removed, then can consider
+                * allowing file to be created.
+                */
+               if ((nameiop == CREATE || nameiop == RENAME ||
+                   (nameiop == DELETE &&
+                   (ap->a_cnp->cn_flags & DOWHITEOUT) &&
+                   (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
+                   (flags & ISLASTCN)) {
+                       /*
+                        * Access for write is interpreted as allowing
+                        * creation of files in the directory.
+                        */
+                       retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc);
+                       if (retval)
+                               goto exit;
+               
+                       cnp->cn_flags |= SAVENAME;
+                       if (!(flags & LOCKPARENT))
+                               VOP_UNLOCK(dvp, 0, p);
+                       retval = EJUSTRETURN;
+                       goto exit;
                }
        
                /*
                 * Insert name into cache (as non-existent) if appropriate.
+                *
+                * Disable negative caching since HFS is case-insensitive.
                 */
-
-               /*
-               * XXX SER - Here we would store the name in cache as non-existant if not trying to create it, but,
-       * the name cache IS case-sensitive, thus maybe showing a negative hit, when the name
-       * is only different by case. So hfs does not support negative caching. Something to look at.
-       * (See radar 2293594 for a failed example)
+#if 0
                if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
-                       cache_enter(parent_vp, *vpp, cnp);
-               */
-       
+                       cache_enter(dvp, *vpp, cnp);
+#endif
                retval = ENOENT;
+               goto exit;
        }
-       else {
-               /*
-                * We have found an entry
-                *
-                * Here we have to decide what type of vnode to create.
-                * There are 3 type of objects that are given:
-                * 1. '.': return the same dp
-                * 2. '..' return the parent of dp, always a VDIR
-                * 3. catinfo rec: return depending on type:
-                *  A. VDIR, nodeType is kCatalogFolderNode
-                *  B. VLINK nodeType is kCatalogFileNode, the mode is IFLNK (esp. if it is a link to a directory e.g. bar/link/foo)
-                *  C. VREG, nodeType is kCatalogFileNode, forkType at this point is unknown
-                * To determine the forkType, we can use this algorithm (\0 in the strings mean the NULL character):
-                * a. forkType is kDataType iff ISLASTCN is set (as in the case of the default fork e.g. data/foo).
-                * b. forkType is kDataType iff ISLASTCN is not set and the namePtr is followed by "/?AppleHFSFork/data\0"
-                * c. forkType is kRsrcType iff ISLASTCN is not set and the namePtr is followed by "/?AppleHFSFork/rsrc\0"
-                * If the latter two are correct, then we 'consume' the remaining of the name buffer
-                * and set the cnp as appropriate.
-                * Anything else returns an retval
-                */
 
-               
+found:
+       /*
+        * Process any fork specifiers
+        */
+       if (forknamelen && S_ISREG(attr.ca_mode)) {
+               /* fork names are only for lookups */
+               if ((nameiop != LOOKUP) && (nameiop != CREATE)) {
+                       retval = EPERM;  
+                       goto exit;
+               }
+               cnp->cn_consume = forknamelen;
+               flags |= ISLASTCN;
+       } else {
+               wantrsrc = 0;
+               forknamelen = 0;
+       }
+
+       /*
+        * If deleting, and at end of pathname, return
+        * parameters which can be used to remove file.
+        */
+       if (nameiop == DELETE && (flags & ISLASTCN)) {
                /*
-                * If deleting, and at end of pathname, return
-                * parameters which can be used to remove file.
-                * If the wantparent flag isn't set, we return only
-                * the directory (in ndp->ndvp), otherwise we go
-                * on and lock the hfsnode, being careful with ".".
-                *
-                * Forks cannot be deleted so scan-ahead is illegal, so just return the default fork
-                */
-               if (nameiop == DELETE && (flags & ISLASTCN)) {
-                       /*
-                       * Write access to directory required to delete files.
-                       */
-                       retval = VOP_ACCESS(parent_vp, VWRITE, cred, cnp->cn_proc);
+               * Write access to directory required to delete files.
+               */
+               if ((retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc)))
+                       goto exit;
+               
+               if (isDot) {    /* Want to return ourselves */
+                       VREF(dvp);
+                       *vpp = dvp;
+                       goto exit;
+               } else if (flags & ISDOTDOT) {
+                       retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
+                               NULL, 0, NULL, NULL, &tvp);
                        if (retval)
-                               goto Err_Exit;
-       
-                       if (isDot) {                                    /* Want to return ourselves */
-                               VREF(parent_vp);
-                               target_vp = parent_vp;
-                               goto Err_Exit;
-                       }
-                       else if (isDotDot) {
-                               retval = VFS_VGET(parent_vp->v_mount, &nodeID, &target_vp);
-                               if (retval)
-                                       goto Err_Exit;
-                       }
-                       else {
-                               retval = hfs_vget_catinfo(parent_vp, &catInfo, kDefault, &target_vp);
-                               if (retval)
-                                       goto Err_Exit;
-                               CLEAN_CATALOGDATA(&catInfo.nodeData);
-                       };
-
+                               goto exit;
+               } else {
+                       retval = hfs_getcnode(hfsmp, attr.ca_fileid,
+                               &desc, wantrsrc, &attr, &fork, &tvp);
+                       if (retval)
+                               goto exit;
+               }
 
-                       /*
-                        * If directory is "sticky", then user must own
-                        * the directory, or the file in it, else she
-                        * may not delete it (unless she's root). This
-                        * implements append-only directories.
-                        */
-                       if ((parent_hp->h_meta->h_mode & ISVTX) &&
-                               (cred->cr_uid != 0) &&
-                               (cred->cr_uid != parent_hp->h_meta->h_uid) &&
-                               (target_vp->v_type != VLNK) &&
-                               (hfs_owner_rights(target_vp, cred, p, false))) {
-                               vput(target_vp);
-                               retval = EPERM;
-                               goto Err_Exit;
-                       }
-#if HFS_HARDLINKS
-                       /*
-                        * If this is a link node then we need to save the name
-                        * (of the link) so we can delete it from the catalog b-tree.
-                        * In this case, hfs_remove will then free the component name.
-                        */
-                       if (target_vp && (VTOH(target_vp)->h_meta->h_metaflags & IN_DATANODE))
-                               cnp->cn_flags |= SAVENAME;
-#endif
-         
-                       if (!lockparent)
-                               VOP_UNLOCK(parent_vp, 0, p);
-                       goto Err_Exit;
-                };
-       
                /*
-                * If rewriting 'RENAME', return the hfsnode and the
-                * information required to rewrite the present directory
+                * If directory is "sticky", then user must own
+                * the directory, or the file in it, else she
+                * may not delete it (unless she's root). This
+                * implements append-only directories.
                 */
-               if (nameiop == RENAME && wantparent && (cnp->cn_flags & ISLASTCN)) {
-       
-                       if ((retval = VOP_ACCESS(parent_vp, VWRITE, cred, cnp->cn_proc)) != 0)
-                               goto Err_Exit;
+               if ((dcp->c_mode & S_ISTXT) &&
+                       (cred->cr_uid != 0) &&
+                       (cred->cr_uid != dcp->c_uid) &&
+                       (tvp->v_type != VLNK) &&
+                       (hfs_owner_rights(hfsmp, VTOC(tvp)->c_uid, cred, p, false))) {
+                       vput(tvp);
+                       retval = EPERM;
+                       goto exit;
+               }
 
-                       /*
-                        * Careful about locking second inode.
-                        * This can only occur if the target is ".". like 'mv foo/bar foo/.'
-                        */
-                       if (isDot) {
-                               retval = EISDIR;
-                               goto Err_Exit;
-                       }
-                       else if (isDotDot) {
-                               retval = VFS_VGET(parent_vp->v_mount, &nodeID, &target_vp);
-                               if (retval)
-                                       goto Err_Exit;
-                       }
-            else {
-
-                retval = hfs_vget_catinfo(parent_vp, &catInfo, kDefault, &target_vp);
-                if (retval)
-                    goto Err_Exit;
-
-                CLEAN_CATALOGDATA(&catInfo.nodeData);  /* Should do nothing */
-            };
-                       
+               /*
+                * If this is a link node then we need to save the name
+                * (of the link) so we can delete it from the catalog b-tree.
+                * In this case, hfs_remove will then free the component name.
+                *
+                * DJB - IS THIS STILL NEEDED????
+                */
+               if (tvp && (VTOC(tvp)->c_flag & C_HARDLINK))
                        cnp->cn_flags |= SAVENAME;
-                       if (!lockparent)
-                               VOP_UNLOCK(parent_vp, 0, p);
-
-                       goto Err_Exit;
-               /* Finished...all is well, goto the end */
-                };
+  
+               if (!(flags & LOCKPARENT))
+                       VOP_UNLOCK(dvp, 0, p);
+               *vpp = tvp;
+               goto exit;
+        }
 
+       /*
+        * If renaming, return the cnode and save the current name.
+        */
+       if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
+               if ((retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc)) != 0)
+                       goto exit;
                /*
-                * Step through the translation in the name.  We do not `vput' the
-                * directory because we may need it again if a symbolic link
-                * is relative to the current directory.  Instead we save it
-                * unlocked as "tparent_vp".  We must get the target hfsnode before unlocking
-                * the directory to insure that the hfsnode will not be removed
-                * before we get it.  We prevent deadlock by always fetching
-                * inodes from the root, moving down the directory tree. Thus
-                * when following backward pointers ".." we must unlock the
-                * parent directory before getting the requested directory.
-                * There is a potential race condition here if both the current
-                * and parent directories are removed before the VFS_VGET for the
-                * hfsnode associated with ".." returns.  We hope that this occurs
-                * infrequently since we cannot avoid this race condition without
-                * implementing a sophisticated deadlock detection algorithm.
-                * Note also that this simple deadlock detection scheme will not
-                * work if the file system has any hard links other than ".."
-                * that point backwards in the directory structure.
+                * Careful about locking second cnode.
                 */
-       
-               tparent_vp = parent_vp;
-               if (isDotDot) {
-                       VOP_UNLOCK(tparent_vp, 0, p);   /* race to get the inode */
-                       if ((retval = VFS_VGET(parent_vp->v_mount, &nodeID, &target_vp))) {
-                       vn_lock(tparent_vp, LK_EXCLUSIVE | LK_RETRY, p);
-                       goto Err_Exit;
+               if (isDot) {
+                       retval = EISDIR;
+                       goto exit;
+               } else if (flags & ISDOTDOT) {
+                       retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
+                               NULL, 0, NULL, NULL, &tvp);
+                       if (retval)
+                               goto exit;
+               } else {
+                       retval = hfs_getcnode(hfsmp, attr.ca_fileid,
+                               &desc, wantrsrc, &attr, &fork, &tvp);
+                       if (retval)
+                               goto exit;
                }
-                       if (lockparent && (flags & ISLASTCN) && (tparent_vp != target_vp) && 
-                           (retval = vn_lock(tparent_vp, LK_EXCLUSIVE, p))) {
-                               vput(target_vp);
-                               goto Err_Exit;
-                       }
+               cnp->cn_flags |= SAVENAME;
+               if (!(flags & LOCKPARENT))
+                       VOP_UNLOCK(dvp, 0, p);
+               *vpp = tvp;
+               goto exit;
+        }
+
+       /*
+        * We must get the target cnode before unlocking
+        * the directory to insure that the cnode will not be removed
+        * before we get it.  We prevent deadlock by always fetching
+        * cnodes from the root, moving down the directory tree. Thus
+        * when following backward pointers ".." we must unlock the
+        * parent directory before getting the requested directory.
+        * There is a potential race condition here if both the current
+        * and parent directories are removed before the VFS_VGET for the
+        * cnode associated with ".." returns.  We hope that this occurs
+        * infrequently since we cannot avoid this race condition without
+        * implementing a sophisticated deadlock detection algorithm.
+        */
+       if (flags & ISDOTDOT) {
+               VOP_UNLOCK(dvp, 0, p);  /* race to get the cnode */
+               retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
+                       NULL, 0, NULL, NULL, &tvp);
+               if (retval) {
+                       vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
+                       goto exit;
                }
-               else if (isDot) {
-                       VREF(parent_vp);        /* we want ourself, ie "." */
-                       target_vp = parent_vp;
+               if ((flags & LOCKPARENT) && (flags & ISLASTCN) && (dvp != tvp) && 
+                   (retval = vn_lock(dvp, LK_EXCLUSIVE, p))) {
+                       vput(tvp);
+                       goto exit;
                }
-               else {
-                       mode_t mode;
-                       /* 
-                        * Determine what fork to get, currenty 3 scenarios are supported:
-                        * 1. ./foo: if it is a dir, return a VDIR else return data fork
-                        * 2. ./foo/.__Fork/data: return data fork
-                        * 3. ./foo/.__Fork/rsrc: return resource fork
-                        * So the algorithm is:
-                        * If the object is a directory
-                        *      then return a VDIR vnode
-                        * else if ISLASTCN is true
-                        *      then get the vnode with forkType=kDataFork
-                        * else
-                        *      compare with the remaining cnp buffer with "/.__Fork/"
-                        *      if a match
-                        *              then compare string after that with either 'data' or 'rsrc'
-                        *              if match
-                        *                      then 
-                        *                      'consume' rest of cnp, setting appropriate values and flags
-                        *                      return vnode depending on match
-                        *              else
-                        *                      bad fork name
-                        *      else
-                        *              illegal path after a file object
-                        */
+               *vpp = tvp;
+       } else if (isDot) {
+               VREF(dvp);      /* we want ourself, ie "." */
+               *vpp = dvp;
+       } else {
+               int type = (attr.ca_mode & S_IFMT);
 
-                       mode = (mode_t)(catInfo.nodeData.cnd_mode);
-                       
-                       if (catInfo.nodeData.cnd_type == kCatalogFolderNode) {
-                               forkType = kDirectory;                          /* Really ignored */
-                       } 
-                       else if ((mode & IFMT) == IFLNK) {
-                               forkType = kDataFork;
-                       }                                                                       /* After this point, nodeType should be a file */
-                       else if (flags & ISLASTCN) {                    /* Create a default fork */
-                               forkType = kDataFork;
+               if (!(flags & ISLASTCN) && type != S_IFDIR && type != S_IFLNK) {
+                       retval = ENOTDIR;
+                       goto exit;
                }
-                       else {                                                                  /* determine what fork was specified */
-                               forkType = GetForkFromName(cnp);
-                               flags |= ISLASTCN;                                      /* To know to unlock the parent if needed */
-                       };      /* else */
-
-                       
-                       /* If couldn't determine what type of fork, leave */
-                       if (forkType == kUndefinedFork) {                               
-                               retval = ENOTDIR;
-                               goto Err_Exit;
-                       };
-                               
-                       /* Get the vnode now that what type of fork is known */
-                       DBG_ASSERT((forkType==kDirectory) || (forkType==kDataFork) || (forkType==kRsrcFork));
-                       retval = hfs_vget_catinfo(tparent_vp, &catInfo, forkType, &target_vp);
-                       if (retval != E_NONE)
-                               goto Err_Exit;
-
-                       if (!lockparent || !(flags & ISLASTCN))
-                               VOP_UNLOCK(tparent_vp, 0, p);
-
-                       CLEAN_CATALOGDATA(&catInfo.nodeData);
 
-               };      /* else found */
-
-
-               /*
-               * Insert name in cache if wanted.
-                * Names with composed chars are not put into the name cache.
-                * Resource forks are not entered in the name cache. This
-                * avoids deadlocks.
-                */
-               if ((cnp->cn_flags & MAKEENTRY)
-                   && (cnp->cn_namelen == catInfo.nodeData.cnm_length)
-                       && ((H_FORKTYPE(VTOH(target_vp))) != kRsrcFork))        {
-                       /*
-                        * XXX SER - Might be good idea to bcopy(catInfo.nodeData.fsspec.name, cnp->cn_nameptr)
-                        * to "normalize" the name cache. This will avoid polluting the name cache with
-                        * names that are different in case, and allow negative caching
-                        */
-                       cache_enter(parent_vp, target_vp, cnp);
-                       }
-       
-
-
-       };      /* else found == TRUE */
-       
-Err_Exit:
+               retval = hfs_getcnode(hfsmp, attr.ca_fileid,
+                       &desc, wantrsrc, &attr, &fork, &tvp);
+               if (retval)
+                       goto exit;
 
-       CLEAN_CATALOGDATA(&catInfo.nodeData);           /* Just to make sure */
-       *ap->a_vpp = target_vp;
+               if (!(flags & LOCKPARENT) || !(flags & ISLASTCN))
+                       VOP_UNLOCK(dvp, 0, p);
+               *vpp = tvp;
+       }
 
-       DBG_VOP_UPDATE_VP(1, *ap->a_vpp);
-       //DBG_VOP_LOOKUP_TEST (funcname, cnp, parent_vp, target_vp);
-       //DBG_VOP_LOCKS_TEST(E_NONE);
+       /*
+        * Insert name in cache if appropriate.
+        *  - "." and ".." are not cached.
+        *  - Resource fork names are not cached.
+        *  - Names with composed chars are not cached.
+        */
+       if ((cnp->cn_flags & MAKEENTRY)
+           && !isDot
+           && !(flags & ISDOTDOT)
+           && !wantrsrc
+           && (cnp->cn_namelen == VTOC(*vpp)->c_desc.cd_namelen)) {
+               cache_enter(dvp, *vpp, cnp);
+       }
 
+exit:
+       cat_releasedesc(&desc);
        return (retval);
 }
 
@@ -608,14 +477,9 @@ Err_Exit:
  * is for DELETE, or NOCACHE is set (rewrite), and the
  * name is located in the cache, it will be dropped.
  *
- * In hfs, since a name can represent multiple forks, it cannot
- * be known what fork the name matches, so further checks have to be done.
- * Currently a policy of first requested, is the one stored, is followed.
- *
- * SER XXX If this proves inadequate maybe we can munge the name to contain a fork reference
- * like foo -> foo.d for the data fork.
  */
 
+__private_extern__
 int
 hfs_cache_lookup(ap)
        struct vop_lookup_args /* {
@@ -624,39 +488,31 @@ hfs_cache_lookup(ap)
                struct componentname *a_cnp;
        } */ *ap;
 {
-       struct vnode *vdp;
-       struct vnode *pdp;
+       struct vnode *dvp;
+       struct vnode *vp;
+       struct cnode *cp;
        int lockparent; 
        int error;
        struct vnode **vpp = ap->a_vpp;
-       struct componentname    *cnp = ap->a_cnp;
-       struct ucred            *cred = cnp->cn_cred;
+       struct componentname *cnp = ap->a_cnp;
+       struct ucred *cred = cnp->cn_cred;
        int flags = cnp->cn_flags;
-       struct proc             *p = cnp->cn_proc;
-       struct hfsnode          *hp;
-       u_int32_t vpid; /* capability number of vnode */
-       DBG_FUNC_NAME("cache_lookup");
-       DBG_VOP_LOCKS_DECL(2);
-       DBG_VOP_LOCKS_INIT(0,ap->a_dvp, VOPDBG_LOCKED, VOPDBG_IGNORE, VOPDBG_IGNORE, VOPDBG_POS);
-       DBG_VOP_LOCKS_INIT(1,*ap->a_vpp, VOPDBG_IGNORE, VOPDBG_LOCKED, VOPDBG_IGNORE, VOPDBG_POS);
-       DBG_VOP_PRINT_FUNCNAME();DBG_VOP_CONT(("\n"));
-    DBG_VOP_CONT(("\tTarget: "));DBG_VOP_PRINT_CPN_INFO(ap->a_cnp);DBG_VOP_CONT(("\n"));
-       DBG_HFS_NODE_CHECK(ap->a_dvp);
+       struct proc *p = cnp->cn_proc;
+       u_long vpid;    /* capability number of vnode */
 
        *vpp = NULL;
-       vdp = ap->a_dvp;
+       dvp = ap->a_dvp;
        lockparent = flags & LOCKPARENT;
 
-       if (vdp->v_type != VDIR)
+       /*
+        * Check accessiblity of directory.
+        */
+       if (dvp->v_type != VDIR)
                return (ENOTDIR);
-
-       if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
-               (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
+       if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
+           (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
                return (EROFS);
-
-       error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc);
-
-       if (error)
+       if ((error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_proc)))
                return (error);
 
        /*
@@ -666,54 +522,46 @@ hfs_cache_lookup(ap)
         * (negative cacheing), a status of ENOENT is returned. If the lookup
         * fails, a status of zero is returned.
         */
-       error = cache_lookup(vdp, vpp, cnp);
-
+       error = cache_lookup(dvp, vpp, cnp);
        if (error == 0)  {              /* Unsuccessfull */
-               DBG_VOP(("\tWas not in name cache\n"));
                error = hfs_lookup(ap);
-#if HFS_HARDLINKS
-               if (error)
-                       return (error);
-               /*
-                * If this is a hard-link vnode then we need to update
-                * the name (of the link) and update the parent ID. This
-                * enables getattrlist calls to return correct link info.
-                */
-               hp = VTOH(*ap->a_vpp);
-               if ((flags & ISLASTCN) && (hp->h_meta->h_metaflags & IN_DATANODE)) {
-                       H_DIRID(hp) = H_FILEID(VTOH(ap->a_dvp));
-                       hfs_set_metaname(cnp->cn_nameptr, hp->h_meta, HTOHFS(hp));
-               }
-#endif
                return (error);
-       };
+       }
        
-       DBG_VOP(("\tName was found in the name cache"));
-       if (error == ENOENT) {
-               DBG_VOP_CONT((" though it was a NEGATIVE HIT\n"));
+       if (error == ENOENT)
                return (error);
-       };
-       DBG_VOP_CONT(("\n"));
        
-#if HFS_HARDLINKS
+       /* We have a name that matched */
+       vp = *vpp;
+       vpid = vp->v_id;
+
        /*
         * If this is a hard-link vnode then we need to update
-        * the name (of the link) and update the parent ID. This
-        * enables getattrlist calls to return correct link info.
+        * the name (of the link), the parent ID, the cnid, the
+        * text encoding and the catalog hint.  This enables
+        * getattrlist calls to return the correct link info.
         */
-       hp = VTOH(*vpp);
-       if ((flags & ISLASTCN) && (hp->h_meta->h_metaflags & IN_DATANODE)) {
-               H_DIRID(hp) = H_FILEID(VTOH(vdp));
-               hfs_set_metaname(cnp->cn_nameptr, hp->h_meta, HTOHFS(hp));
+       cp = VTOC(vp);
+       if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK) &&
+            ((cp->c_parentcnid != VTOC(ap->a_dvp)->c_cnid) ||
+             (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0))) {
+             
+               struct cat_desc desc;
+
+               /*
+                * Get an updated descriptor
+                */
+               bzero(&desc, sizeof(desc));
+               desc.cd_nameptr = cnp->cn_nameptr;
+               desc.cd_namelen = cnp->cn_namelen;
+               desc.cd_parentcnid = VTOC(ap->a_dvp)->c_cnid;
+               desc.cd_hint = VTOC(ap->a_dvp)->c_childhint;
+               if (cat_lookup(VTOHFS(vp), &desc, 0, &desc, NULL, NULL) == 0)
+                       replace_desc(cp, &desc);
        }
-#endif
 
-       /* We have a name that matched */
-       pdp = vdp;
-       vdp = *vpp;
-       vpid = vdp->v_id;
-       if (pdp == vdp) {       /* lookup on "." */
-               VREF(vdp);
+       if (dvp == vp) {        /* lookup on "." */
+               VREF(vp);
                error = 0;
        } else if (flags & ISDOTDOT) {
                /* 
@@ -722,128 +570,85 @@ hfs_cache_lookup(ap)
                 * to release lock on child before trying to lock parent
                 * then regain lock if needed
                 */
-               VOP_UNLOCK(pdp, 0, p);
-               error = vget(vdp, LK_EXCLUSIVE, p);
+               VOP_UNLOCK(dvp, 0, p);
+               error = vget(vp, LK_EXCLUSIVE, p);
                if (!error && lockparent && (flags & ISLASTCN))
-                       error = vn_lock(pdp, LK_EXCLUSIVE, p);
-       } else if ((! (flags & ISLASTCN)) && (vdp->v_type == VREG) && 
-                               (GetForkFromName(cnp) != kDataFork)) {
-               /* 
-                * We only store data forks in the name cache.
-                */                              
-               goto finished;
+                       error = vn_lock(dvp, LK_EXCLUSIVE, p);
        } else {
-               error = vget(vdp, LK_EXCLUSIVE, p);
+               if ((flags & ISLASTCN) == 0 && vp->v_type == VREG) {
+                       int wantrsrc = 0;
+
+                       cnp->cn_consume = forkcomponent(cnp, &wantrsrc);
+                       
+                       /* Fork names are only for lookups */
+                       if (cnp->cn_consume &&
+                           (cnp->cn_nameiop != LOOKUP && cnp->cn_nameiop != CREATE))
+                               return (EPERM);
+                       /* 
+                        * We only store data forks in the name cache.
+                        */                              
+                       if (wantrsrc)
+                               return (hfs_lookup(ap));
+               }
+               error = vget(vp, LK_EXCLUSIVE, p);
                if (!lockparent || error || !(flags & ISLASTCN))
-                       VOP_UNLOCK(pdp, 0, p);
+                       VOP_UNLOCK(dvp, 0, p);
        }
        /*
         * Check that the capability number did not change
         * while we were waiting for the lock.
         */
        if (!error) {
-               if (vpid == vdp->v_id)
-                       return (0);             /* HERE IS THE NORMAL EXIT FOR CACHE LOOKUP!!!! */
+               if (vpid == vp->v_id)
+                       return (0);
                /*
                 * The above is the NORMAL exit, after this point is an error
                 * condition.
                 */
-               vput(vdp);
-               if (lockparent && pdp != vdp && (flags & ISLASTCN))
-                       VOP_UNLOCK(pdp, 0, p);
+               vput(vp);
+               if (lockparent && (dvp != vp) && (flags & ISLASTCN))
+                       VOP_UNLOCK(dvp, 0, p);
        }
-       error = vn_lock(pdp, LK_EXCLUSIVE, p);
-       if (error)
-               return (error);
 
-finished:
+       if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)))
+               return (error);
 
        return (hfs_lookup(ap));
 }
 
+
 /*
- *     Parses a componentname and sees if the remaining path
- *     contains a hfs named fork specifier. If it does set the
- *     componentname to consume the rest of the path, and
- *     return the forkType
+ * forkcomponent - look for a fork suffix in the component name
+ *
  */
-
-u_int16_t      GetForkFromName(struct componentname  *cnp)
+static int
+forkcomponent(struct componentname *cnp, int *rsrcfork)
 {
-       u_int16_t       forkType        = kUndefinedFork;
-       char            *tcp            = cnp->cn_nameptr + cnp->cn_namelen;
-
-       if (bcmp(tcp, _PATH_FORKSPECIFIER, sizeof(_PATH_FORKSPECIFIER) - 1) == 0) {             
-               /* Its a HFS fork, so far */
-               tcp += (sizeof(_PATH_FORKSPECIFIER) - 1);
-               if (bcmp(tcp, _PATH_DATANAME, sizeof(_PATH_DATANAME)) == 0) {
-                       forkType = kDataFork;
-                       cnp->cn_consume = sizeof(_PATH_FORKSPECIFIER) + sizeof(_PATH_DATANAME) - 2;
-               }
-               else if (bcmp(tcp, _PATH_RSRCNAME, sizeof(_PATH_RSRCNAME)) == 0) {
-                       forkType = kRsrcFork;
-                       cnp->cn_consume = sizeof(_PATH_FORKSPECIFIER) + sizeof(_PATH_RSRCNAME) - 2;
-               };      /* else if */
-       };      /* if bcmp */   
-
-
-       /* XXX SER For backwards compatability...keep it */
-       if (forkType == kUndefinedFork) {
-               tcp = cnp->cn_nameptr + cnp->cn_namelen;
-       if (bcmp(tcp, gHFSForkIdentStr, sizeof(gHFSForkIdentStr) - 1) == 0) {           
-               /* Its a HFS fork, so far */
-               tcp += (sizeof(gHFSForkIdentStr) - 1);
-               if (bcmp(tcp, gDataForkNameStr, sizeof(gDataForkNameStr)) == 0) {
-                       forkType = kDataFork;
-                       cnp->cn_consume = sizeof(gHFSForkIdentStr) + sizeof(gDataForkNameStr) - 2;
-               }
-               else if (bcmp(tcp, gRsrcForkNameStr, sizeof(gRsrcForkNameStr)) == 0) {
-                       forkType = kRsrcFork;
-                       cnp->cn_consume = sizeof(gHFSForkIdentStr) + sizeof(gRsrcForkNameStr) - 2;
-               };      /* else if */
-       };      /* if bcmp */                                                           
-       };                                                      
-
-       return forkType;        
-}
-
-#if DBG_VOP_TEST_LOCKS
+       char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
+       int consume = 0;
 
-void DbgLookupTest( char *funcname, struct componentname  *cnp, struct vnode *dvp, struct vnode *vp)
-{
-       if (! (hfs_dbg_lookup || hfs_dbg_all))
-               return;
-               
-               
-       if (dvp) {
-               if (lockstatus(&VTOH(dvp)->h_lock)) {
-                       DBG_LOOKUP (("%s: Parent vnode exited LOCKED", funcname));
-               }
-               else {
-                       DBG_LOOKUP (("%s: Parent vnode exited UNLOCKED", funcname));
-               }
+       *rsrcfork = 0;
+       if (*suffix == '\0')
+               return (0);
+       /*
+        * There are only 3 valid fork suffixes:
+        *      "/..namedfork/rsrc"
+        *      "/..namedfork/data"
+        *      "/rsrc"  (legacy)
+        */
+       if (bcmp(suffix, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
+               consume = sizeof(_PATH_RSRCFORKSPEC) - 1;
+               *rsrcfork = 1;
+       } else if (bcmp(suffix, _PATH_DATAFORKSPEC, sizeof(_PATH_DATAFORKSPEC)) == 0) {
+               consume = sizeof(_PATH_DATAFORKSPEC) - 1;
        }
 
-       if (vp) {
-               if (vp==dvp)
-                 {
-                       DBG_LOOKUP (("%s: Target and Parent are the same", funcname));
-                 }
-               else {
-                       if (lockstatus(&VTOH(vp)->h_lock)) {
-                               DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname));
-                       }
-                       else {
-                               DBG_LOOKUP (("%s: Found vnode exited LOCKED", funcname));
-                       }
-               }
-               DBG_LOOKUP (("%s: Found vnode 0x%x has vtype of %d\n ", funcname, (u_int)vp, vp->v_type));
+#ifdef LEGACY_FORK_NAMES
+       else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
+               consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
+               *rsrcfork = 1;
        }
-       else
-               DBG_LOOKUP (("%s: Found vnode exited NULL\n",  funcname));
-
-
+#endif
+       return (consume);
 }
 
-#endif /* DBG_VOP_TEST_LOCKS */
-