]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/hfs/hfs_lookup.c
xnu-1228.5.20.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
index c4013f4c36418df98da9cfabeab40fa445133ae4..b59038b3b0b6dab0b17ad74359f62735cd2637a6 100644 (file)
@@ -1,16 +1,19 @@
 /*
- * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * This file contains Original Code and/or Modifications of Original Code
  * as defined in and that are subject to the Apple Public Source License
  * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
@@ -20,7 +23,7 @@
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * Copyright (c) 1989, 1993
  *
  *     hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
  */
-#define LEGACY_FORK_NAMES      0
 
 #include <sys/param.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 <sys/kdebug.h>
+#include <sys/kauth.h>
+#include <sys/namei.h>
 
 #include "hfs.h"
 #include "hfs_catalog.h"
 #include "hfs_cnode.h"
 
 
-static int forkcomponent(struct componentname *cnp, int *rsrcfork);
-
-#define _PATH_DATAFORKSPEC     "/..namedfork/data"
-
-#ifdef LEGACY_FORK_NAMES
-#define LEGACY_RSRCFORKSPEC    "/rsrc"
-#endif
-
 /*     
  * FROM FREEBSD 3.1
  * Convert a component of a pathname into a pointer to a locked cnode.
@@ -105,13 +99,6 @@ static int forkcomponent(struct componentname *cnp, int *rsrcfork);
  * creating, renaming, or deleting a directory entry may be calculated.
  * Notice that these are the only operations that can affect the directory of the target.
  *
- * If flag has LOCKPARENT or'ed into it and the target of the pathname
- * exists, lookup returns both the target and its parent directory locked.
- * When creating or renaming and LOCKPARENT is specified, the target may
- * not be ".". When deleting and LOCKPARENT is specified, the target may
- * be "."., but the caller must check to ensure it does an vrele and vput
- * instead of two vputs.
- *
  * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
  * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
  * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
@@ -120,10 +107,6 @@ static int forkcomponent(struct componentname *cnp, int *rsrcfork);
  * 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 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
- * method.
- *
  * hfs_cache_lookup() performs the following for us:
  *     check that it is a directory
  *     check accessibility of directory
@@ -133,7 +116,7 @@ static int forkcomponent(struct componentname *cnp, int *rsrcfork);
  *             drop it
  *              else
  *             return name.
- *     return VOP_CACHEDLOOKUP()
+ *     return hfs_lookup()
  *
  * Overall outline of hfs_lookup:
  *
@@ -150,9 +133,10 @@ static int forkcomponent(struct componentname *cnp, int *rsrcfork);
  *       nor deleting, add name to cache
  */
 
+
 /*     
- *     Lookup *nm in directory *pvp, return it in *a_vpp.
- *     **a_vpp is held on exit.
+ *     Lookup *cnp in directory *dvp, return it in *vpp.
+ *     **vpp is held on exit.
  *     We create a cnode for the file, but we do NOT open the file here.
 
 #% lookup      dvp L ? ?
@@ -165,88 +149,104 @@ static int forkcomponent(struct componentname *cnp, int *rsrcfork);
 
  *     When should we lock parent_hp in here ??
  */
-
-__private_extern__
-int
-hfs_lookup(ap)
-       struct vop_cachedlookup_args /* {
-               struct vnode *a_dvp;
-               struct vnode **a_vpp;
-               struct componentname *a_cnp;
-       } */ *ap;
+static int
+hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int *cnode_locked)
 {
-       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 desc;
        struct cat_desc cndesc;
        struct cat_attr attr;
        struct cat_fork fork;
-       struct vnode **vpp;
+       int lockflags;
 
-       vpp = ap->a_vpp;
-       cnp = ap->a_cnp;
-       dvp = ap->a_dvp;
-       dcp = VTOC(dvp);
+  retry:
+       dcp = NULL;
        hfsmp = VTOHFS(dvp);
        *vpp = NULL;
+       *cnode_locked = 0;
        isDot = FALSE;
        tvp = NULL;
        nameiop = cnp->cn_nameiop;
-       cred = cnp->cn_cred;
-       p = cnp->cn_proc;
        flags = cnp->cn_flags;
-       wantparent = flags & (LOCKPARENT|WANTPARENT);
+       bzero(&desc, sizeof(desc));
 
        /*
         * First check to see if it is a . or .., else look it up.
         */
        if (flags & ISDOTDOT) {         /* Wanting the parent */
+               cnp->cn_flags &= ~MAKEENTRY;
                goto found;     /* .. is always defined */
        } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
                isDot = TRUE;
+               cnp->cn_flags &= ~MAKEENTRY;
                goto found;     /* We always know who we are */
        } else {
-               /* Check fork suffix to see if we want the resource fork */
-               forknamelen = forkcomponent(cnp, &wantrsrc);
+               if (hfs_lock(VTOC(dvp), HFS_EXCLUSIVE_LOCK) != 0) {
+                       retval = ENOENT;  /* The parent no longer exists ? */
+                       goto exit;
+               }
+               dcp = VTOC(dvp);
+
+               if (dcp->c_flag & C_DIR_MODIFICATION) {
+                   // XXXdbg - if we could msleep on a lck_rw_t then we would do that
+                   //          but since we can't we have to unlock, delay for a bit
+                   //          and then retry...
+                   // msleep((caddr_t)&dcp->c_flag, &dcp->c_rwlock, PINOD, "hfs_vnop_lookup", 0);
+                   hfs_unlock(dcp);
+                   tsleep((caddr_t)dvp, PRIBIO, "hfs_lookup", 1);
+
+                   goto retry;
+               }
 
                /* No need to go to catalog if there are no children */
-               if (dcp->c_entries == 0)
+               if (dcp->c_entries == 0) {
                        goto notfound;
+               }
 
                bzero(&cndesc, sizeof(cndesc));
-               cndesc.cd_nameptr = cnp->cn_nameptr;
+               cndesc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
                cndesc.cd_namelen = cnp->cn_namelen;
-               cndesc.cd_parentcnid = dcp->c_cnid;
+               cndesc.cd_parentcnid = dcp->c_fileid;
                cndesc.cd_hint = dcp->c_childhint;
 
-               /* Lock catalog b-tree */
-               retval = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, p);
-               if (retval)
-                          goto exit;
-               retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork);
+               lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
+
+               retval = cat_lookup(hfsmp, &cndesc, 0, &desc, &attr, &fork, NULL);
                
-               if (retval == 0 && S_ISREG(attr.ca_mode) && attr.ca_blocks < fork.cf_blocks)
-                       panic("hfs_lookup: bad ca_blocks (too small)");
-       
-               /* Unlock catalog b-tree */
-               (void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, p);
+               hfs_systemfile_unlock(hfsmp, lockflags);
+
                if (retval == 0) {
                        dcp->c_childhint = desc.cd_hint;
+                       /*
+                        * Note: We must drop the parent lock here before calling
+                        * hfs_getnewvnode (which takes the child lock).
+                        */
+                       hfs_unlock(dcp);
+                       dcp = NULL;
                        goto found;
                }
 notfound:
+               /*
+                * ENAMETOOLONG supersedes other errors
+                *
+                * For a CREATE or RENAME operation on the last component
+                * the ENAMETOOLONG will be handled in the next VNOP.
+                */
+               if ((retval != ENAMETOOLONG) && 
+                   (cnp->cn_namelen > kHFSPlusMaxFileNameChars) &&
+                   (((flags & ISLASTCN) == 0) || ((nameiop != CREATE) && (nameiop != RENAME)))) {
+                       retval = ENAMETOOLONG;
+               } else if (retval == 0) {
+                       retval = ENOENT;
+               }
+               if (retval != ENOENT)
+                       goto exit;
                /*
                 * This is a non-existing entry
                 *
@@ -256,207 +256,131 @@ notfound:
                 */
                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);
+                   (cnp->cn_flags & DOWHITEOUT) &&
+                   (cnp->cn_flags & ISWHITEOUT))) &&
+                   (flags & ISLASTCN) &&
+                   !(ISSET(dcp->c_flag, C_DELETED | C_NOEXISTS))) {
                        retval = EJUSTRETURN;
                        goto exit;
                }
-       
                /*
-                * Insert name into cache (as non-existent) if appropriate.
-                *
-                * Disable negative caching since HFS is case-insensitive.
+                * Insert name into the name cache (as non-existent).
                 */
-#if 0
-               if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
-                       cache_enter(dvp, *vpp, cnp);
-#endif
-               retval = ENOENT;
+               if ((hfsmp->hfs_flags & HFS_STANDARD) == 0 &&
+                   (cnp->cn_flags & MAKEENTRY) &&
+                   (nameiop != CREATE)) {
+                       cache_enter(dvp, NULL, cnp);
+                       dcp->c_flag |= C_NEG_ENTRIES;
+               }
                goto exit;
        }
 
 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;
+       if (flags & ISLASTCN) {
+               switch(nameiop) {
+               case DELETE:
+                       cnp->cn_flags &= ~MAKEENTRY;
+                       break;
+
+               case RENAME:
+                       cnp->cn_flags &= ~MAKEENTRY;
+                       if (isDot) {
+                               retval = EISDIR;
+                               goto exit;
+                       }
+                       break;
                }
-               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)) {
-               /*
-               * Write access to directory required to delete files.
-               */
-               if ((retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc)))
+       if (isDot) {
+               if ((retval = vnode_get(dvp)))
                        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 exit;
-               } else {
-                       retval = hfs_getcnode(hfsmp, attr.ca_fileid,
-                               &desc, wantrsrc, &attr, &fork, &tvp);
-                       if (retval)
-                               goto exit;
-               }
-
+               *vpp = dvp;
+       } else if (flags & ISDOTDOT) {
                /*
-                * 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.
+                * Directory hard links can have multiple parents so
+                * find the appropriate parent for the current thread.
                 */
-               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;
+               if ((retval = hfs_vget(hfsmp, hfs_currentparent(VTOC(dvp)), &tvp, 0))) {
                        goto exit;
                }
-
-               /*
-                * 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 (!(flags & LOCKPARENT))
-                       VOP_UNLOCK(dvp, 0, p);
+               *cnode_locked = 1;
                *vpp = tvp;
-               goto exit;
-        }
+       } else {
+               int type = (attr.ca_mode & S_IFMT);
+#if NAMEDRSRCFORK
+               int rsrc_warn = 0;
 
-       /*
-        * 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;
                /*
-                * Careful about locking second cnode.
+                * Check if caller wants the resource fork but utilized
+                * the legacy "file/rsrc" access path.
+                *
+                * This is deprecated behavior and support for it will not
+                * be allowed beyond case insensitive HFS+ and even that
+                * support will be removed in the next major OS release.
                 */
-               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 ((type == S_IFREG) &&
+                   ((flags & ISLASTCN) == 0) &&
+                   (cnp->cn_nameptr[cnp->cn_namelen] == '/') &&
+                   (bcmp(&cnp->cn_nameptr[cnp->cn_namelen+1], "rsrc", 5) == 0) &&
+                   ((hfsmp->hfs_flags & (HFS_STANDARD | HFS_CASE_SENSITIVE)) == 0)) {
+               
+                       cnp->cn_consume = 5;
+                       cnp->cn_flags |= CN_WANTSRSRCFORK | ISLASTCN | NOCACHE;
+                       cnp->cn_flags &= ~MAKEENTRY;
+                       flags |= ISLASTCN;
+                       rsrc_warn = 1;
                }
-               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);
+#endif
+               if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) {
+                       retval = ENOTDIR;
                        goto exit;
                }
-               if ((flags & LOCKPARENT) && (flags & ISLASTCN) && (dvp != tvp) && 
-                   (retval = vn_lock(dvp, LK_EXCLUSIVE, p))) {
-                       vput(tvp);
-                       goto exit;
+               /* Don't cache directory hardlink names. */
+               if (attr.ca_recflags & kHFSHasLinkChainMask) {
+                       cnp->cn_flags &= ~MAKEENTRY;
                }
-               *vpp = tvp;
-       } else if (isDot) {
-               VREF(dvp);      /* we want ourself, ie "." */
-               *vpp = dvp;
-       } else {
-               int type = (attr.ca_mode & S_IFMT);
+               /* Names with composed chars are not cached. */
+               if (cnp->cn_namelen != desc.cd_namelen)
+                       cnp->cn_flags &= ~MAKEENTRY;
 
-               if (!(flags & ISLASTCN) && type != S_IFDIR && type != S_IFLNK) {
-                       retval = ENOTDIR;
-                       goto exit;
-               }
+               retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, 0, &attr, &fork, &tvp);
 
-               retval = hfs_getcnode(hfsmp, attr.ca_fileid,
-                       &desc, wantrsrc, &attr, &fork, &tvp);
-               if (retval)
+               if (retval) {
+                       /*
+                        * If this was a create operation lookup and another
+                        * process removed the object before we had a chance
+                        * to create the vnode, then just treat it as the not
+                        * found case above and return EJUSTRETURN.
+                        */
+                       if ((retval == ENOENT) &&
+                           (cnp->cn_nameiop == CREATE) &&
+                           (flags & ISLASTCN)) {
+                               retval = EJUSTRETURN;
+                       }
                        goto exit;
+               }
 
-               if (!(flags & LOCKPARENT) || !(flags & ISLASTCN))
-                       VOP_UNLOCK(dvp, 0, p);
+               /* Save the origin info of a directory link for future ".." requests. */
+               if (S_ISDIR(attr.ca_mode) && (attr.ca_recflags & kHFSHasLinkChainMask)) {
+                       hfs_savelinkorigin(VTOC(tvp), VTOC(dvp)->c_fileid);
+               }
+               *cnode_locked = 1;
                *vpp = tvp;
+#if NAMEDRSRCFORK
+               if (rsrc_warn) {
+                       if ((VTOC(tvp)->c_flag & C_WARNED_RSRC) == 0) {
+                               VTOC(tvp)->c_flag |= C_WARNED_RSRC;
+                               printf("%.200s: file access by '/rsrc' was deprecated in 10.4\n",
+                                      cnp->cn_nameptr);
+                       }
+               }
+#endif
        }
-
-       /*
-        * 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:
+       if (dcp) {
+               hfs_unlock(dcp);
+       }
        cat_releasedesc(&desc);
        return (retval);
 }
@@ -464,8 +388,6 @@ exit:
 
 
 /*
- * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
- *
  * Name caching works as follows:
  *
  * Names found by directory scans are retained in a cache
@@ -483,61 +405,48 @@ exit:
  *
  */
 
+#define        S_IXALL 0000111
+
 __private_extern__
 int
-hfs_cache_lookup(ap)
-       struct vop_lookup_args /* {
-               struct vnode *a_dvp;
-               struct vnode **a_vpp;
-               struct componentname *a_cnp;
-       } */ *ap;
+hfs_vnop_lookup(struct vnop_lookup_args *ap)
 {
-       struct vnode *dvp;
+       struct vnode *dvp = ap->a_dvp;
        struct vnode *vp;
        struct cnode *cp;
-       int lockparent; 
+       struct cnode *dcp;
        int error;
        struct vnode **vpp = ap->a_vpp;
        struct componentname *cnp = ap->a_cnp;
-       struct ucred *cred = cnp->cn_cred;
        int flags = cnp->cn_flags;
-       struct proc *p = cnp->cn_proc;
-       u_long vpid;    /* capability number of vnode */
+       int cnode_locked;
 
        *vpp = NULL;
-       dvp = ap->a_dvp;
-       lockparent = flags & LOCKPARENT;
-
-       /*
-        * Check accessiblity of directory.
-        */
-       if (dvp->v_type != VDIR)
-               return (ENOTDIR);
-       if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
-           (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
-               return (EROFS);
-       if ((error = VOP_ACCESS(dvp, VEXEC, cred, cnp->cn_proc)))
-               return (error);
+       dcp = VTOC(dvp);
 
        /*
         * Lookup an entry in the cache
-        * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
-        * returned. If the lookup determines that the name does not exist
-        * (negative cacheing), a status of ENOENT is returned. If the lookup
-        * fails, a status of zero is returned.
+        *
+        * If the lookup succeeds, the vnode is returned in *vpp,
+        * and a status of -1 is returned.
+        *
+        * If the lookup determines that the name does not exist
+        * (negative cacheing), a status of ENOENT is returned.
+        *
+        * If the lookup fails, a status of zero is returned.
         */
        error = cache_lookup(dvp, vpp, cnp);
-       if (error == 0)  {              /* Unsuccessfull */
-               error = hfs_lookup(ap);
-               return (error);
+       if (error != -1) {
+               if ((error == ENOENT) && (cnp->cn_nameiop != CREATE))           
+                       goto exit;      /* found a negative cache entry */
+               goto lookup;            /* did not find it in the cache */
        }
-       
-       if (error == ENOENT)
-               return (error);
-       
-       /* We have a name that matched */
+       /*
+        * We have a name that matched
+        * cache_lookup returns the vp with an iocount reference already taken
+        */
+       error = 0;
        vp = *vpp;
-       vpid = vp->v_id;
 
        /*
         * If this is a hard-link vnode then we need to update
@@ -546,113 +455,75 @@ hfs_cache_lookup(ap)
         * getattrlist calls to return the correct link info.
         */
        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);
-       }
+       if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK)) {
+               hfs_lock(cp, HFS_FORCE_LOCK);
+               if ((cp->c_parentcnid != dcp->c_cnid) ||
+                   (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0)) {
+                       struct cat_desc desc;
+                       int lockflags;
 
-       if (dvp == vp) {        /* lookup on "." */
-               VREF(vp);
-               error = 0;
-       } else if (flags & ISDOTDOT) {
-               /* 
-                * Carefull on the locking policy,
-                * remember we always lock from parent to child, so have
-                * to release lock on child before trying to lock parent
-                * then regain lock if needed
-                */
-               VOP_UNLOCK(dvp, 0, p);
-               error = vget(vp, LK_EXCLUSIVE, p);
-               if (!error && lockparent && (flags & ISLASTCN))
-                       error = vn_lock(dvp, LK_EXCLUSIVE, p);
-       } else {
-               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));
+                       /*
+                        * Get an updated descriptor
+                        */
+                       desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
+                       desc.cd_namelen = cnp->cn_namelen;
+                       desc.cd_parentcnid = dcp->c_fileid;
+                       desc.cd_hint = dcp->c_childhint;
+                       desc.cd_encoding = 0;
+                       desc.cd_cnid = 0;
+                       desc.cd_flags = S_ISDIR(cp->c_mode) ? CD_ISDIR : 0;
+       
+                       lockflags = hfs_systemfile_lock(VTOHFS(dvp), SFL_CATALOG, HFS_SHARED_LOCK);             
+                       if (cat_lookup(VTOHFS(vp), &desc, 0, &desc, NULL, NULL, NULL) == 0)
+                               replace_desc(cp, &desc);
+                       hfs_systemfile_unlock(VTOHFS(dvp), lockflags);
                }
-               error = vget(vp, LK_EXCLUSIVE, p);
-               if (!lockparent || error || !(flags & ISLASTCN))
-                       VOP_UNLOCK(dvp, 0, p);
+               hfs_unlock(cp);
        }
+#if NAMEDRSRCFORK
        /*
-        * Check that the capability number did not change
-        * while we were waiting for the lock.
+        * Check if caller wants the resource fork but utilized
+        * the legacy "file/rsrc" access path.
+        *
+        * This is deprecated behavior and support for it will not
+        * be allowed beyond case insensitive HFS+ and even that
+        * support will be removed in the next major OS release.
         */
-       if (!error) {
-               if (vpid == vp->v_id)
-                       return (0);
-               /*
-                * The above is the NORMAL exit, after this point is an error
-                * condition.
-                */
-               vput(vp);
-               if (lockparent && (dvp != vp) && (flags & ISLASTCN))
-                       VOP_UNLOCK(dvp, 0, p);
+       if ((dvp != vp) &&
+           ((flags & ISLASTCN) == 0) &&
+           vnode_isreg(vp) &&
+           (cnp->cn_nameptr[cnp->cn_namelen] == '/') &&
+           (bcmp(&cnp->cn_nameptr[cnp->cn_namelen+1], "rsrc", 5) == 0) &&
+           ((VTOHFS(vp)->hfs_flags & (HFS_STANDARD | HFS_CASE_SENSITIVE)) == 0)) {             
+               cnp->cn_consume = 5;
+               cnp->cn_flags |= CN_WANTSRSRCFORK | ISLASTCN | NOCACHE;
+               cnp->cn_flags &= ~MAKEENTRY;
+
+               hfs_lock(cp, HFS_FORCE_LOCK);
+               if ((cp->c_flag & C_WARNED_RSRC) == 0) {
+                       cp->c_flag |= C_WARNED_RSRC;
+                       printf("%.200s: file access by '/rsrc' was deprecated in 10.4\n", cnp->cn_nameptr);
+               }
+               hfs_unlock(cp);
        }
-
-       if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)))
-               return (error);
-
-       return (hfs_lookup(ap));
-}
-
-
-/*
- * forkcomponent - look for a fork suffix in the component name
- *
- */
-static int
-forkcomponent(struct componentname *cnp, int *rsrcfork)
-{
-       char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
-       int consume = 0;
-
-       *rsrcfork = 0;
-       if (*suffix == '\0')
-               return (0);
+#endif
+       return (error);
+       
+lookup:
        /*
-        * There are only 3 valid fork suffixes:
-        *      "/..namedfork/rsrc"
-        *      "/..namedfork/data"
-        *      "/rsrc"  (legacy)
+        * The vnode was not in the name cache or it was stale.
+        *
+        * So we need to do a real lookup.
         */
-       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;
-       }
+       cnode_locked = 0;
 
-#ifdef LEGACY_FORK_NAMES
-       else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
-               consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
-               *rsrcfork = 1;
-       }
-#endif
-       return (consume);
+       error = hfs_lookup(dvp, vpp, cnp, &cnode_locked);
+       
+       if (cnode_locked)
+               hfs_unlock(VTOC(*vpp));
+exit:
+       return (error);
 }
 
+