]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/nfs/nfs_vnops.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_vnops.c
index 47c4610768b1a9676f0754b981415ffa752e7fd2..e771822d813d8af1e9c3489867195fbdf563d656 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
 #include <net/if.h>
 #include <netinet/in.h>
 #include <netinet/in_var.h>
+
 #include <vm/vm_kern.h>
+#include <vm/vm_pageout.h>
 
 #include <kern/task.h>
 #include <kern/sched_prim.h>
-#include <libkern/OSAtomic.h>
+
+#define NFS_VNOP_DBG(...) NFS_DBG(NFS_FAC_VNOP, 7, ## __VA_ARGS__)
 
 /*
  * NFS vnode ops
  */
-static int     nfs_vnop_lookup(struct vnop_lookup_args *);
-static int     nfsspec_vnop_read(struct vnop_read_args *);
-static int     nfsspec_vnop_write(struct vnop_write_args *);
-static int     nfsspec_vnop_close(struct vnop_close_args *);
+int    nfs_vnop_lookup(struct vnop_lookup_args *);
+int    nfsspec_vnop_read(struct vnop_read_args *);
+int    nfsspec_vnop_write(struct vnop_write_args *);
+int    nfsspec_vnop_close(struct vnop_close_args *);
 #if FIFO
-static int     nfsfifo_vnop_read(struct vnop_read_args *);
-static int     nfsfifo_vnop_write(struct vnop_write_args *);
-static int     nfsfifo_vnop_close(struct vnop_close_args *);
+int    nfsfifo_vnop_read(struct vnop_read_args *);
+int    nfsfifo_vnop_write(struct vnop_write_args *);
+int    nfsfifo_vnop_close(struct vnop_close_args *);
 #endif
-static int     nfs_vnop_ioctl(struct vnop_ioctl_args *);
-static int     nfs_vnop_select(struct vnop_select_args *);
-static int     nfs_vnop_setattr(struct vnop_setattr_args *);
-static int     nfs_vnop_read(struct vnop_read_args *);
-static int     nfs_vnop_mmap(struct vnop_mmap_args *);
-static int     nfs_vnop_fsync(struct vnop_fsync_args *);
-static int     nfs_vnop_remove(struct vnop_remove_args *);
-static int     nfs_vnop_rename(struct vnop_rename_args *);
-static int     nfs_vnop_readdir(struct vnop_readdir_args *);
-static int     nfs_vnop_readlink(struct vnop_readlink_args *);
-static int     nfs_vnop_pathconf(struct vnop_pathconf_args *);
-static int     nfs_vnop_pagein(struct vnop_pagein_args *);
-static int     nfs_vnop_pageout(struct vnop_pageout_args *);
-static int     nfs_vnop_blktooff(struct vnop_blktooff_args *);
-static int     nfs_vnop_offtoblk(struct vnop_offtoblk_args *);
-static int     nfs_vnop_blockmap(struct vnop_blockmap_args *);
-
-static int     nfs3_vnop_create(struct vnop_create_args *);
-static int     nfs3_vnop_mknod(struct vnop_mknod_args *);
-static int     nfs3_vnop_getattr(struct vnop_getattr_args *);
-static int     nfs3_vnop_link(struct vnop_link_args *);
-static int     nfs3_vnop_mkdir(struct vnop_mkdir_args *);
-static int     nfs3_vnop_rmdir(struct vnop_rmdir_args *);
-static int     nfs3_vnop_symlink(struct vnop_symlink_args *);
+int    nfs_vnop_ioctl(struct vnop_ioctl_args *);
+int    nfs_vnop_select(struct vnop_select_args *);
+int    nfs_vnop_setattr(struct vnop_setattr_args *);
+int    nfs_vnop_fsync(struct vnop_fsync_args *);
+int    nfs_vnop_rename(struct vnop_rename_args *);
+int    nfs_vnop_readdir(struct vnop_readdir_args *);
+int    nfs_vnop_readlink(struct vnop_readlink_args *);
+int    nfs_vnop_pathconf(struct vnop_pathconf_args *);
+int    nfs_vnop_pagein(struct vnop_pagein_args *);
+int    nfs_vnop_pageout(struct vnop_pageout_args *);
+int    nfs_vnop_blktooff(struct vnop_blktooff_args *);
+int    nfs_vnop_offtoblk(struct vnop_offtoblk_args *);
+int    nfs_vnop_blockmap(struct vnop_blockmap_args *);
+int    nfs_vnop_monitor(struct vnop_monitor_args *);
+
+int    nfs3_vnop_create(struct vnop_create_args *);
+int    nfs3_vnop_mknod(struct vnop_mknod_args *);
+int    nfs3_vnop_getattr(struct vnop_getattr_args *);
+int    nfs3_vnop_link(struct vnop_link_args *);
+int    nfs3_vnop_mkdir(struct vnop_mkdir_args *);
+int    nfs3_vnop_rmdir(struct vnop_rmdir_args *);
+int    nfs3_vnop_symlink(struct vnop_symlink_args *);
 
 vnop_t **nfsv2_vnodeop_p;
 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
@@ -161,8 +162,8 @@ static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
        { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup },       /* lookup */
        { &vnop_create_desc, (vnop_t *)nfs3_vnop_create },      /* create */
        { &vnop_mknod_desc, (vnop_t *)nfs3_vnop_mknod },        /* mknod */
-       { &vnop_open_desc, (vnop_t *)nfs3_vnop_open },          /* open */
-       { &vnop_close_desc, (vnop_t *)nfs3_vnop_close },        /* close */
+       { &vnop_open_desc, (vnop_t *)nfs_vnop_open },           /* open */
+       { &vnop_close_desc, (vnop_t *)nfs_vnop_close },         /* close */
        { &vnop_access_desc, (vnop_t *)nfs_vnop_access },       /* access */
        { &vnop_getattr_desc, (vnop_t *)nfs3_vnop_getattr },    /* getattr */
        { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr },     /* setattr */
@@ -172,6 +173,7 @@ static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
        { &vnop_select_desc, (vnop_t *)nfs_vnop_select },       /* select */
        { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke },       /* revoke */
        { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap },           /* mmap */
+       { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap },       /* mnomap */
        { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync },         /* fsync */
        { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove },       /* remove */
        { &vnop_link_desc, (vnop_t *)nfs3_vnop_link },          /* link */
@@ -185,7 +187,7 @@ static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
        { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim },     /* reclaim */
        { &vnop_strategy_desc, (vnop_t *)err_strategy },        /* strategy */
        { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf },   /* pathconf */
-       { &vnop_advlock_desc, (vnop_t *)nfs3_vnop_advlock },    /* advlock */
+       { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock },     /* advlock */
        { &vnop_bwrite_desc, (vnop_t *)err_bwrite },            /* bwrite */
        { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein },       /* Pagein */
        { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout },     /* Pageout */
@@ -193,6 +195,7 @@ static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
@@ -204,8 +207,8 @@ static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = {
        { &vnop_lookup_desc, (vnop_t *)nfs_vnop_lookup },       /* lookup */
        { &vnop_create_desc, (vnop_t *)nfs4_vnop_create },      /* create */
        { &vnop_mknod_desc, (vnop_t *)nfs4_vnop_mknod },        /* mknod */
-       { &vnop_open_desc, (vnop_t *)nfs4_vnop_open },          /* open */
-       { &vnop_close_desc, (vnop_t *)nfs4_vnop_close },        /* close */
+       { &vnop_open_desc, (vnop_t *)nfs_vnop_open },           /* open */
+       { &vnop_close_desc, (vnop_t *)nfs_vnop_close },         /* close */
        { &vnop_access_desc, (vnop_t *)nfs_vnop_access },       /* access */
        { &vnop_getattr_desc, (vnop_t *)nfs4_vnop_getattr },    /* getattr */
        { &vnop_setattr_desc, (vnop_t *)nfs_vnop_setattr },     /* setattr */
@@ -215,6 +218,7 @@ static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = {
        { &vnop_select_desc, (vnop_t *)nfs_vnop_select },       /* select */
        { &vnop_revoke_desc, (vnop_t *)nfs_vnop_revoke },       /* revoke */
        { &vnop_mmap_desc, (vnop_t *)nfs_vnop_mmap },           /* mmap */
+       { &vnop_mnomap_desc, (vnop_t *)nfs_vnop_mnomap },       /* mnomap */
        { &vnop_fsync_desc, (vnop_t *)nfs_vnop_fsync },         /* fsync */
        { &vnop_remove_desc, (vnop_t *)nfs_vnop_remove },       /* remove */
        { &vnop_link_desc, (vnop_t *)nfs4_vnop_link },          /* link */
@@ -228,7 +232,7 @@ static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = {
        { &vnop_reclaim_desc, (vnop_t *)nfs_vnop_reclaim },     /* reclaim */
        { &vnop_strategy_desc, (vnop_t *)err_strategy },        /* strategy */
        { &vnop_pathconf_desc, (vnop_t *)nfs_vnop_pathconf },   /* pathconf */
-       { &vnop_advlock_desc, (vnop_t *)nfs4_vnop_advlock },    /* advlock */
+       { &vnop_advlock_desc, (vnop_t *)nfs_vnop_advlock },     /* advlock */
        { &vnop_bwrite_desc, (vnop_t *)err_bwrite },            /* bwrite */
        { &vnop_pagein_desc, (vnop_t *)nfs_vnop_pagein },       /* Pagein */
        { &vnop_pageout_desc, (vnop_t *)nfs_vnop_pageout },     /* Pageout */
@@ -236,6 +240,16 @@ static struct vnodeopv_entry_desc nfsv4_vnodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr },  /* getxattr */
+       { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr },  /* setxattr */
+       { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
+       { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
+#if NAMEDSTREAMS
+       { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream },      /* getnamedstream */
+       { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream },    /* makenamedstream */
+       { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
+#endif
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc nfsv4_vnodeop_opv_desc =
@@ -280,6 +294,7 @@ static struct vnodeopv_entry_desc spec_nfsv2nodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
@@ -320,6 +335,16 @@ static struct vnodeopv_entry_desc spec_nfsv4nodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr },  /* getxattr */
+       { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr },  /* setxattr */
+       { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
+       { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
+#if NAMEDSTREAMS
+       { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream },      /* getnamedstream */
+       { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream },    /* makenamedstream */
+       { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
+#endif
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc spec_nfsv4nodeop_opv_desc =
@@ -362,6 +387,7 @@ static struct vnodeopv_entry_desc fifo_nfsv2nodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
@@ -403,6 +429,16 @@ static struct vnodeopv_entry_desc fifo_nfsv4nodeop_entries[] = {
        { &vnop_blktooff_desc, (vnop_t *)nfs_vnop_blktooff },   /* blktooff */
        { &vnop_offtoblk_desc, (vnop_t *)nfs_vnop_offtoblk },   /* offtoblk */
        { &vnop_blockmap_desc, (vnop_t *)nfs_vnop_blockmap },   /* blockmap */
+       { &vnop_getxattr_desc, (vnop_t *)nfs4_vnop_getxattr },  /* getxattr */
+       { &vnop_setxattr_desc, (vnop_t *)nfs4_vnop_setxattr },  /* setxattr */
+       { &vnop_removexattr_desc, (vnop_t *)nfs4_vnop_removexattr },/* removexattr */
+       { &vnop_listxattr_desc, (vnop_t *)nfs4_vnop_listxattr },/* listxattr */
+#if NAMEDSTREAMS
+       { &vnop_getnamedstream_desc, (vnop_t *)nfs4_vnop_getnamedstream },      /* getnamedstream */
+       { &vnop_makenamedstream_desc, (vnop_t *)nfs4_vnop_makenamedstream },    /* makenamedstream */
+       { &vnop_removenamedstream_desc, (vnop_t *)nfs4_vnop_removenamedstream },/* removenamedstream */
+#endif
+       { &vnop_monitor_desc, (vnop_t *)nfs_vnop_monitor },     /* monitor */
        { NULL, NULL }
 };
 struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc =
@@ -410,35 +446,35 @@ struct vnodeopv_desc fifo_nfsv4nodeop_opv_desc =
 #endif /* FIFO */
 
 
-static int     nfs_sillyrename(nfsnode_t,nfsnode_t,struct componentname *,vfs_context_t);
+int    nfs_sillyrename(nfsnode_t,nfsnode_t,struct componentname *,vfs_context_t);
 
 /*
  * Find the slot in the access cache for this UID.
  * If adding and no existing slot is found, reuse slots in FIFO order.
- * The index of the next slot to use is kept in the last entry of the n_mode array.
+ * The index of the next slot to use is kept in the last entry of the n_access array.
  */
 int
-nfs_node_mode_slot(nfsnode_t np, uid_t uid, int add)
+nfs_node_access_slot(nfsnode_t np, uid_t uid, int add)
 {
        int slot;
 
        for (slot=0; slot < NFS_ACCESS_CACHE_SIZE; slot++)
-               if (np->n_modeuid[slot] == uid)
+               if (np->n_accessuid[slot] == uid)
                        break;
        if (slot == NFS_ACCESS_CACHE_SIZE) {
                if (!add)
                        return (-1);
-               slot = np->n_mode[NFS_ACCESS_CACHE_SIZE];
-               np->n_mode[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE;
+               slot = np->n_access[NFS_ACCESS_CACHE_SIZE];
+               np->n_access[NFS_ACCESS_CACHE_SIZE] = (slot + 1) % NFS_ACCESS_CACHE_SIZE;
        }
        return (slot);
 }
 
 int
-nfs3_access_rpc(nfsnode_t np, u_long *mode, vfs_context_t ctx)
+nfs3_access_rpc(nfsnode_t np, u_int32_t *access, vfs_context_t ctx)
 {
-       int error = 0, status, slot;
-       uint32_t access = 0;
+       int error = 0, lockerror = ENOENT, status, slot;
+       uint32_t access_result = 0;
        u_int64_t xid;
        struct nfsm_chain nmreq, nmrep;
        struct timeval now;
@@ -449,23 +485,24 @@ nfs3_access_rpc(nfsnode_t np, u_long *mode, vfs_context_t ctx)
 
        nfsm_chain_build_alloc_init(error, &nmreq, NFSX_FH(NFS_VER3) + NFSX_UNSIGNED);
        nfsm_chain_add_fh(error, &nmreq, NFS_VER3, np->n_fhp, np->n_fhsize);
-       nfsm_chain_add_32(error, &nmreq, *mode);
+       nfsm_chain_add_32(error, &nmreq, *access);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, NULL, &nmreq, NFSPROC_ACCESS, ctx,
-                       &nmrep, &xid, &status);
+       error = nfs_request(np, NULL, &nmreq, NFSPROC_ACCESS, ctx, NULL, &nmrep, &xid, &status);
+       if ((lockerror = nfs_node_lock(np)))
+               error = lockerror;
        nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
        if (!error)
                error = status;
-       nfsm_chain_get_32(error, &nmrep, access);
+       nfsm_chain_get_32(error, &nmrep, access_result);
        nfsmout_if(error);
 
        uid = kauth_cred_getuid(vfs_context_ucred(ctx));
-       slot = nfs_node_mode_slot(np, uid, 1);
-       np->n_modeuid[slot] = uid;
+       slot = nfs_node_access_slot(np, uid, 1);
+       np->n_accessuid[slot] = uid;
        microuptime(&now);
-       np->n_modestamp[slot] = now.tv_sec;
-       np->n_mode[slot] = access;
+       np->n_accessstamp[slot] = now.tv_sec;
+       np->n_access[slot] = access_result;
 
        /*
         * If we asked for DELETE but didn't get it, the server
@@ -474,12 +511,17 @@ nfs3_access_rpc(nfsnode_t np, u_long *mode, vfs_context_t ctx)
         * and just let any subsequent delete action fail if it
         * really isn't deletable.
         */
-       if ((*mode & NFS_ACCESS_DELETE) &&
-           !(np->n_mode[slot] & NFS_ACCESS_DELETE))
-               np->n_mode[slot] |= NFS_ACCESS_DELETE;
-       /* pass back the mode returned with this request */
-       *mode = np->n_mode[slot];
+       if ((*access & NFS_ACCESS_DELETE) &&
+           !(np->n_access[slot] & NFS_ACCESS_DELETE))
+               np->n_access[slot] |= NFS_ACCESS_DELETE;
+       /* ".zfs" subdirectories may erroneously give a denied answer for add/remove */
+       if (nfs_access_dotzfs && (np->n_flag & NISDOTZFSCHILD))
+               np->n_access[slot] |= (NFS_ACCESS_MODIFY|NFS_ACCESS_EXTEND|NFS_ACCESS_DELETE);
+       /* pass back the access returned with this request */
+       *access = np->n_access[slot];
 nfsmout:
+       if (!lockerror)
+               nfs_node_unlock(np);
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
        return (error);
@@ -488,8 +530,8 @@ nfsmout:
 /*
  * NFS access vnode op.
  * For NFS version 2, just return ok. File accesses may fail later.
- * For NFS version 3+, use the access RPC to check accessibility. If file modes
- * are changed on the server, accesses might still fail later.
+ * For NFS version 3+, use the access RPC to check accessibility. If file
+ * permissions are changed on the server, accesses might still fail later.
  */
 int
 nfs_vnop_access(
@@ -503,7 +545,7 @@ nfs_vnop_access(
        vfs_context_t ctx = ap->a_context;
        vnode_t vp = ap->a_vp;
        int error = 0, slot, dorpc;
-       u_long mode, wmode;
+       u_int32_t access, waccess;
        nfsnode_t np = VTONFS(vp);
        struct nfsmount *nmp;
        int nfsvers;
@@ -531,59 +573,74 @@ nfs_vnop_access(
         * in the cache.
         */
 
+       /*
+        * In addition if the kernel is checking for access, KAUTH_VNODE_ACCESS
+        * not set, just return. At this moment do not know what the state of
+        * the server is and what ever we get back be it either yea or nay is
+        * going to be stale.  Finder (Desktop services/FileURL) might hang when
+        * going over the wire when just asking getattrlist for the roots FSID
+        * since we are going to be called to see if we're authorized for
+        * search. Since we are returning without checking the cache and/or
+        * going over the wire, it makes no sense to update the cache.
+        *
+        * N.B. This is also the strategy that SMB is using.
+        */
+       if (!(ap->a_action & KAUTH_VNODE_ACCESS))
+               return (0);
+       
        /*
         * Convert KAUTH primitives to NFS access rights.
         */
-       mode = 0;
+       access = 0;
        if (vnode_isdir(vp)) {
                /* directory */
                if (ap->a_action &
                    (KAUTH_VNODE_LIST_DIRECTORY |
                    KAUTH_VNODE_READ_EXTATTRIBUTES))
-                       mode |= NFS_ACCESS_READ;
+                       access |= NFS_ACCESS_READ;
                if (ap->a_action & KAUTH_VNODE_SEARCH)
-                       mode |= NFS_ACCESS_LOOKUP;
+                       access |= NFS_ACCESS_LOOKUP;
                if (ap->a_action &
                    (KAUTH_VNODE_ADD_FILE |
                    KAUTH_VNODE_ADD_SUBDIRECTORY))
-                       mode |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
+                       access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
                if (ap->a_action & KAUTH_VNODE_DELETE_CHILD)
-                       mode |= NFS_ACCESS_MODIFY;
+                       access |= NFS_ACCESS_MODIFY;
        } else {
                /* file */
                if (ap->a_action &
                    (KAUTH_VNODE_READ_DATA |
                    KAUTH_VNODE_READ_EXTATTRIBUTES))
-                       mode |= NFS_ACCESS_READ;
+                       access |= NFS_ACCESS_READ;
                if (ap->a_action & KAUTH_VNODE_WRITE_DATA)
-                       mode |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
+                       access |= NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND;
                if (ap->a_action & KAUTH_VNODE_APPEND_DATA)
-                       mode |= NFS_ACCESS_EXTEND;
+                       access |= NFS_ACCESS_EXTEND;
                if (ap->a_action & KAUTH_VNODE_EXECUTE)
-                       mode |= NFS_ACCESS_EXECUTE;
+                       access |= NFS_ACCESS_EXECUTE;
        }
        /* common */
        if (ap->a_action & KAUTH_VNODE_DELETE)
-               mode |= NFS_ACCESS_DELETE;
+               access |= NFS_ACCESS_DELETE;
        if (ap->a_action &
            (KAUTH_VNODE_WRITE_ATTRIBUTES |
            KAUTH_VNODE_WRITE_EXTATTRIBUTES |
            KAUTH_VNODE_WRITE_SECURITY))
-               mode |= NFS_ACCESS_MODIFY;
+               access |= NFS_ACCESS_MODIFY;
        /* XXX this is pretty dubious */
        if (ap->a_action & KAUTH_VNODE_CHANGE_OWNER)
-               mode |= NFS_ACCESS_MODIFY;
+               access |= NFS_ACCESS_MODIFY;
 
        /* if caching, always ask for every right */
        if (nfs_access_cache_timeout > 0) {
-               wmode = NFS_ACCESS_READ | NFS_ACCESS_MODIFY |
+               waccess = NFS_ACCESS_READ | NFS_ACCESS_MODIFY |
                        NFS_ACCESS_EXTEND | NFS_ACCESS_EXECUTE |
                        NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP;
        } else {
-               wmode = mode;
+               waccess = access;
        }
 
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
 
        /*
@@ -591,34 +648,44 @@ nfs_vnop_access(
         * this request?
         */
        uid = kauth_cred_getuid(vfs_context_ucred(ctx));
-       slot = nfs_node_mode_slot(np, uid, 0);
+       slot = nfs_node_access_slot(np, uid, 0);
        dorpc = 1;
-       if (NMODEVALID(np, slot)) {
+       if (access == 0) {
+               /* not asking for any rights understood by NFS, so don't bother doing an RPC */
+               /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
+               dorpc = 0;
+               waccess = 0;
+       } else if (NACCESSVALID(np, slot)) {
                microuptime(&now);
-               if ((now.tv_sec < (np->n_modestamp[slot] + nfs_access_cache_timeout)) &&
-                   ((np->n_mode[slot] & mode) == mode)) {
-                       /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_hits); */
+               if ((now.tv_sec < (np->n_accessstamp[slot] + nfs_access_cache_timeout)) &&
+                   ((np->n_access[slot] & access) == access)) {
+                       /* OSAddAtomic(1, &nfsstats.accesscache_hits); */
                        dorpc = 0;
-                       wmode = np->n_mode[slot];
+                       waccess = np->n_access[slot];
                }
        }
+       nfs_node_unlock(np);
        if (dorpc) {
                /* Either a no, or a don't know.  Go to the wire. */
-               /* OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
-               error = nmp->nm_funcs->nf_access_rpc(np, &wmode, ctx);
+               /* OSAddAtomic(1, &nfsstats.accesscache_misses); */
+               error = nmp->nm_funcs->nf_access_rpc(np, &waccess, ctx);
        }
-       if (!error && ((wmode & mode) != mode))
+       if (!error && ((waccess & access) != access))
                error = EACCES;
-       nfs_unlock(np);
 
        return (error);
 }
 
+
 /*
  * NFS open vnode op
+ *
+ * Perform various update/invalidation checks and then add the
+ * open to the node.  Regular files will have an open file structure
+ * on the node and, for NFSv4, perform an OPEN request on the server.
  */
 int
-nfs3_vnop_open(
+nfs_vnop_open(
        struct vnop_open_args /* {
                struct vnodeop_desc *a_desc;
                vnode_t a_vp;
@@ -630,85 +697,213 @@ nfs3_vnop_open(
        vnode_t vp = ap->a_vp;
        nfsnode_t np = VTONFS(vp);
        struct nfsmount *nmp;
-       struct nfs_vattr nvattr;
+       int error, accessMode, denyMode, opened = 0;
+       struct nfs_open_owner *noop = NULL;
+       struct nfs_open_file *nofp = NULL;
        enum vtype vtype;
-       int error, nfsvers;
+
+       if (!(ap->a_mode & (FREAD|FWRITE)))
+               return (EINVAL);
 
        nmp = VTONMP(vp);
        if (!nmp)
                return (ENXIO);
-       nfsvers = nmp->nm_vers;
+       if (np->n_flag & NREVOKE)
+               return (EIO);
 
        vtype = vnode_vtype(vp);
        if ((vtype != VREG) && (vtype != VDIR) && (vtype != VLNK))
                return (EACCES);
+
+       /* First, check if we need to update/invalidate */
        if (ISSET(np->n_flag, NUPDATESIZE))
                nfs_data_update_size(np, 0);
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        if (np->n_flag & NNEEDINVALIDATE) {
                np->n_flag &= ~NNEEDINVALIDATE;
-               nfs_unlock(np);
+               if (vtype == VDIR)
+                       nfs_invaldir(np);
+               nfs_node_unlock(np);
                nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
-               if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+               if ((error = nfs_node_lock(np)))
                        return (error);
        }
+       if (vtype == VREG)
+               np->n_lastrahead = -1;
        if (np->n_flag & NMODIFIED) {
-               nfs_unlock(np);
-               if ((error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1)) == EINTR)
-                       return (error);
-               if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
-                       return (error);
                if (vtype == VDIR)
-                       np->n_direofoffset = 0;
-               NATTRINVALIDATE(np); /* For Open/Close consistency */
-               error = nfs_getattr(np, &nvattr, ctx, 1);
-               if (error) {
-                       nfs_unlock(np);
+                       nfs_invaldir(np);
+               nfs_node_unlock(np);
+               if ((error = nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1)))
                        return (error);
-               }
-               if (vtype == VDIR) {
-                       /* if directory changed, purge any name cache entries */
-                       if (NFS_CHANGED_NC(nfsvers, np, &nvattr)) {
-                               np->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(vp);
-                       }
-                       NFS_CHANGED_UPDATE_NC(nfsvers, np, &nvattr);
-               }
-               NFS_CHANGED_UPDATE(nfsvers, np, &nvattr);
        } else {
-               NATTRINVALIDATE(np); /* For Open/Close consistency */
-               error = nfs_getattr(np, &nvattr, ctx, 1);
-               if (error) {
-                       nfs_unlock(np);
-                       return (error);
+               nfs_node_unlock(np);
+       }
+
+       /* nfs_getattr() will check changed and purge caches */
+       if ((error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED)))
+               return (error);
+
+       if (vtype != VREG) {
+               /* Just mark that it was opened */
+               lck_mtx_lock(&np->n_openlock);
+               np->n_openrefcnt++;
+               lck_mtx_unlock(&np->n_openlock);
+               return (0);
+       }
+
+       /* mode contains some combination of: FREAD, FWRITE, O_SHLOCK, O_EXLOCK */
+       accessMode = 0;
+       if (ap->a_mode & FREAD)
+               accessMode |= NFS_OPEN_SHARE_ACCESS_READ;
+       if (ap->a_mode & FWRITE)
+               accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE;
+       if (ap->a_mode & O_EXLOCK)
+               denyMode = NFS_OPEN_SHARE_DENY_BOTH;
+       else if (ap->a_mode & O_SHLOCK)
+               denyMode = NFS_OPEN_SHARE_DENY_WRITE;
+       else
+               denyMode = NFS_OPEN_SHARE_DENY_NONE;
+       // XXX don't do deny modes just yet (and never do it for !v4)
+       denyMode = NFS_OPEN_SHARE_DENY_NONE;
+
+       noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
+       if (!noop)
+               return (ENOMEM);
+
+restart:
+       error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
+       if (error) {
+               nfs_open_owner_rele(noop);
+               return (error);
+       }
+       if (np->n_flag & NREVOKE) {
+               error = EIO;
+               nfs_mount_state_in_use_end(nmp, 0);
+               nfs_open_owner_rele(noop);
+               return (error);
+       }
+
+       error = nfs_open_file_find(np, noop, &nofp, accessMode, denyMode, 1);
+       if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
+               NP(np, "nfs_vnop_open: LOST %d", kauth_cred_getuid(nofp->nof_owner->noo_cred));
+               error = EIO;
+       }
+       if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
+               nfs_mount_state_in_use_end(nmp, 0);
+               error = nfs4_reopen(nofp, vfs_context_thread(ctx));
+               nofp = NULL;
+               if (!error)
+                       goto restart;
+       }
+       if (!error)
+               error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
+       if (error) {
+               nofp = NULL;
+               goto out;
+       }
+
+       if (nmp->nm_vers < NFS_VER4) {
+               /*
+                * NFS v2/v3 opens are always allowed - so just add it.
+                */
+               nfs_open_file_add_open(nofp, accessMode, denyMode, 0);
+               goto out;
+       }
+
+       /*
+        * If we just created the file and the modes match, then we simply use
+        * the open performed in the create.  Otherwise, send the request.
+        */
+       if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
+           (nofp->nof_creator == current_thread()) &&
+           (accessMode == NFS_OPEN_SHARE_ACCESS_BOTH) &&
+           (denyMode == NFS_OPEN_SHARE_DENY_NONE)) {
+               nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
+               nofp->nof_creator = NULL;
+       } else {
+               if (!opened)
+                       error = nfs4_open(np, nofp, accessMode, denyMode, ctx);
+               if ((error == EACCES) && (nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
+                   (nofp->nof_creator == current_thread())) {
+                       /*
+                        * Ugh.  This can happen if we just created the file with read-only
+                        * perms and we're trying to open it for real with different modes
+                        * (e.g. write-only or with a deny mode) and the server decides to
+                        * not allow the second open because of the read-only perms.
+                        * The best we can do is to just use the create's open.
+                        * We may have access we don't need or we may not have a requested
+                        * deny mode.  We may log complaints later, but we'll try to avoid it.
+                        */
+                       if (denyMode != NFS_OPEN_SHARE_DENY_NONE)
+                               NP(np, "nfs_vnop_open: deny mode foregone on create, %d", kauth_cred_getuid(nofp->nof_owner->noo_cred));
+                       nofp->nof_creator = NULL;
+                       error = 0;
                }
-               if (NFS_CHANGED(nfsvers, np, &nvattr)) {
-                       if (vtype == VDIR) {
-                               np->n_direofoffset = 0;
-                               nfs_invaldir(np);
-                               /* purge name cache entries */
-                               if (NFS_CHANGED_NC(nfsvers, np, &nvattr)) {
-                                       np->n_flag &= ~NNEGNCENTRIES;
-                                       cache_purge(vp);
-                               }
+               if (error)
+                       goto out;
+               opened = 1;
+               /*
+                * If we had just created the file, we already had it open.
+                * If the actual open mode is less than what we grabbed at
+                * create time, then we'll downgrade the open here.
+                */
+               if ((nofp->nof_flags & NFS_OPEN_FILE_CREATE) &&
+                   (nofp->nof_creator == current_thread())) {
+                       error = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_BOTH, NFS_OPEN_SHARE_DENY_NONE, ctx);
+                       if (error)
+                               NP(np, "nfs_vnop_open: create close error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred));
+                       if (!nfs_mount_state_error_should_restart(error)) {
+                               error = 0;
+                               nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
                        }
-                       nfs_unlock(np);
-                       if ((error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1)) == EINTR)
-                               return (error);
-                       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
-                               return (error);
-                       if (vtype == VDIR)
-                               NFS_CHANGED_UPDATE_NC(nfsvers, np, &nvattr);
-                       NFS_CHANGED_UPDATE(nfsvers, np, &nvattr);
                }
        }
-       nfs_unlock(np);
-       return (0);
+
+out:
+       if (nofp)
+               nfs_open_file_clear_busy(nofp);
+       if (nfs_mount_state_in_use_end(nmp, error)) {
+               nofp = NULL;
+               goto restart;
+       }
+       if (error)
+               NP(np, "nfs_vnop_open: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
+       if (noop)
+               nfs_open_owner_rele(noop);
+       if (!error && vtype == VREG && (ap->a_mode & FWRITE)) {
+               lck_mtx_lock(&nmp->nm_lock);
+               nmp->nm_state &= ~NFSSTA_SQUISHY;
+               nmp->nm_curdeadtimeout = nmp->nm_deadtimeout;
+               if (nmp->nm_curdeadtimeout <= 0)
+                       nmp->nm_deadto_start = 0;
+               nmp->nm_writers++;
+               lck_mtx_unlock(&nmp->nm_lock);
+       }
+               
+       return (error);
+}
+
+static uint32_t
+nfs_no_of_open_file_writers(nfsnode_t np)
+{
+       uint32_t writers = 0;
+       struct nfs_open_file *nofp;
+
+       TAILQ_FOREACH(nofp,  &np->n_opens, nof_link) {
+               writers += nofp->nof_w + nofp->nof_rw + nofp->nof_w_dw + nofp->nof_rw_dw +
+                       nofp->nof_w_drw + nofp->nof_rw_drw + nofp->nof_d_w_dw +
+                       nofp->nof_d_rw_dw + nofp->nof_d_w_drw + nofp->nof_d_rw_drw +
+                       nofp->nof_d_w + nofp->nof_d_rw;
+       }
+       
+       return (writers);
 }
 
 /*
  * NFS close vnode op
+ *
  * What an NFS client should do upon close after writing is a debatable issue.
  * Most NFS clients push delayed writes to the server upon close, basically for
  * two reasons:
@@ -726,11 +921,11 @@ nfs3_vnop_open(
  *
  * The current code does the following:
  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
- * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
- *                     them.
+ * for NFS Version 3 - flush dirty buffers to the server but don't invalidate them.
+ * for NFS Version 4 - basically the same as NFSv3
  */
 int
-nfs3_vnop_close(
+nfs_vnop_close(
        struct vnop_close_args /* {
                struct vnodeop_desc *a_desc;
                vnode_t a_vp;
@@ -742,65 +937,356 @@ nfs3_vnop_close(
        vnode_t vp = ap->a_vp;
        nfsnode_t np = VTONFS(vp);
        struct nfsmount *nmp;
-       int nfsvers;
-       int error = 0;
+       int error = 0, error1, nfsvers;
+       int fflag = ap->a_fflag;
+       enum vtype vtype;
+       int accessMode, denyMode;
+       struct nfs_open_owner *noop = NULL;
+       struct nfs_open_file *nofp = NULL;
 
-       if (vnode_vtype(vp) != VREG)
-               return (0);
        nmp = VTONMP(vp);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
+       vtype = vnode_vtype(vp);
 
+       /* First, check if we need to update/flush/invalidate */
        if (ISSET(np->n_flag, NUPDATESIZE))
                nfs_data_update_size(np, 0);
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
-               return (error);
+       nfs_node_lock_force(np);
        if (np->n_flag & NNEEDINVALIDATE) {
                np->n_flag &= ~NNEEDINVALIDATE;
-               nfs_unlock(np);
+               nfs_node_unlock(np);
                nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
-               if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
-                       return (error);
+               nfs_node_lock_force(np);
        }
-       if (np->n_flag & NMODIFIED) {
-               nfs_unlock(np);
+       if ((vtype == VREG) && (np->n_flag & NMODIFIED) && (fflag & FWRITE)) {
+               /* we're closing an open for write and the file is modified, so flush it */
+               nfs_node_unlock(np);
                if (nfsvers != NFS_VER2)
                        error = nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), 0);
                else
                        error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
-               if (error)
-                       return (error);
-               nfs_lock(np, NFS_NODE_LOCK_FORCE);
+               nfs_node_lock_force(np);
                NATTRINVALIDATE(np);
        }
        if (np->n_flag & NWRITEERR) {
                np->n_flag &= ~NWRITEERR;
                error = np->n_error;
        }
-       nfs_unlock(np);
+       nfs_node_unlock(np);
+
+       if (vtype != VREG) {
+               /* Just mark that it was closed */
+               lck_mtx_lock(&np->n_openlock);
+               if (np->n_openrefcnt == 0) {
+                       if (fflag & (FREAD|FWRITE)) {
+                               NP(np, "nfs_vnop_close: open reference underrun");
+                               error = EINVAL;
+                       }
+               } else if (fflag & (FREAD|FWRITE)) {
+                       np->n_openrefcnt--;
+               } else {
+                       /* No FREAD/FWRITE set - probably the final close */
+                       np->n_openrefcnt = 0;
+               }
+               lck_mtx_unlock(&np->n_openlock);
+               return (error);
+       }
+       error1 = error;
+
+       /* fflag should contain some combination of: FREAD, FWRITE, FHASLOCK */
+       accessMode = 0;
+       if (fflag & FREAD)
+               accessMode |= NFS_OPEN_SHARE_ACCESS_READ;
+       if (fflag & FWRITE)
+               accessMode |= NFS_OPEN_SHARE_ACCESS_WRITE;
+// XXX It would be nice if we still had the O_EXLOCK/O_SHLOCK flags that were on the open
+//     if (fflag & O_EXLOCK)
+//             denyMode = NFS_OPEN_SHARE_DENY_BOTH;
+//     else if (fflag & O_SHLOCK)
+//             denyMode = NFS_OPEN_SHARE_DENY_WRITE;
+//     else
+//             denyMode = NFS_OPEN_SHARE_DENY_NONE;
+#if 0  // Not yet
+       if (fflag & FHASLOCK) {
+               /* XXX assume FHASLOCK is for the deny mode and not flock */
+               /* FHASLOCK flock will be unlocked in the close path, but the flag is not cleared. */
+               if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_READ)
+                       denyMode = NFS_OPEN_SHARE_DENY_BOTH;
+               else if (nofp->nof_deny & NFS_OPEN_SHARE_DENY_WRITE)
+                       denyMode = NFS_OPEN_SHARE_DENY_WRITE;
+               else
+                       denyMode = NFS_OPEN_SHARE_DENY_NONE;
+       } else {
+                       denyMode = NFS_OPEN_SHARE_DENY_NONE;
+       }
+#else
+       // XXX don't do deny modes just yet (and never do it for !v4)
+       denyMode = NFS_OPEN_SHARE_DENY_NONE;
+#endif
+
+       if (!accessMode) {
+               /*
+                * No mode given to close?
+                * Guess this is the final close.
+                * We should unlock all locks and close all opens.
+                */
+               uint32_t writers;
+               mount_t mp = vnode_mount(vp);
+               int force = (!mp || (mp->mnt_kern_flag & MNTK_FRCUNMOUNT));
+
+               writers = nfs_no_of_open_file_writers(np);
+               nfs_release_open_state_for_node(np, force);
+               if (writers) {
+                       lck_mtx_lock(&nmp->nm_lock);
+                       if (writers > nmp->nm_writers) {
+                               NP(np, "nfs_vnop_close: number of write opens for mount underrun. Node has %d"
+                                  " opens for write. Mount has total of %d opens for write\n", 
+                                  writers, nmp->nm_writers);
+                               nmp->nm_writers = 0;
+                       } else {
+                               nmp->nm_writers -= writers;
+                       }
+                       lck_mtx_unlock(&nmp->nm_lock);
+               }
+               
+               return (error);
+       } else if (fflag & FWRITE) {
+               lck_mtx_lock(&nmp->nm_lock);
+               if (nmp->nm_writers == 0) {
+                       NP(np, "nfs_vnop_close: removing open writer from mount, but mount has no files open for writing");
+               } else {
+                       nmp->nm_writers--;
+               }
+               lck_mtx_unlock(&nmp->nm_lock);
+       }
+       
+
+       noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 0);
+       if (!noop) {
+               // printf("nfs_vnop_close: can't get open owner!\n");
+               return (EIO);
+       }
+
+restart:
+       error = nfs_mount_state_in_use_start(nmp, NULL);
+       if (error) {
+               nfs_open_owner_rele(noop);
+               return (error);
+       }
+
+       error = nfs_open_file_find(np, noop, &nofp, 0, 0, 0);
+       if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
+               nfs_mount_state_in_use_end(nmp, 0);
+               error = nfs4_reopen(nofp, NULL);
+               nofp = NULL;
+               if (!error)
+                       goto restart;
+       }
+       if (error) {
+               NP(np, "nfs_vnop_close: no open file for owner, error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
+               error = EBADF;
+               goto out;
+       }
+       error = nfs_open_file_set_busy(nofp, NULL);
+       if (error) {
+               nofp = NULL;
+               goto out;
+       }
+
+       error = nfs_close(np, nofp, accessMode, denyMode, ctx);
+       if (error)
+               NP(np, "nfs_vnop_close: close error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
+
+out:
+       if (nofp)
+               nfs_open_file_clear_busy(nofp);
+       if (nfs_mount_state_in_use_end(nmp, error)) {
+               nofp = NULL;
+               goto restart;
+       }
+       if (!error)
+               error = error1;
+       if (error)
+               NP(np, "nfs_vnop_close: error %d, %d", error, kauth_cred_getuid(noop->noo_cred));
+       if (noop)
+               nfs_open_owner_rele(noop);
+       return (error);
+}
+
+/*
+ * nfs_close(): common function that does all the heavy lifting of file closure
+ *
+ * Takes an open file structure and a set of access/deny modes and figures out how
+ * to update the open file structure (and the state on the server) appropriately.
+ */
+int
+nfs_close(
+       nfsnode_t np,
+       struct nfs_open_file *nofp,
+       uint32_t accessMode,
+       uint32_t denyMode,
+       vfs_context_t ctx)
+{
+       struct nfs_lock_owner *nlop;
+       int error = 0, changed = 0, delegated = 0, closed = 0, downgrade = 0;
+       uint32_t newAccessMode, newDenyMode;
+       
+       /* warn if modes don't match current state */
+       if (((accessMode & nofp->nof_access) != accessMode) || ((denyMode & nofp->nof_deny) != denyMode))
+               NP(np, "nfs_close: mode mismatch %d %d, current %d %d, %d",
+                       accessMode, denyMode, nofp->nof_access, nofp->nof_deny,
+                       kauth_cred_getuid(nofp->nof_owner->noo_cred));
+
+       /*
+        * If we're closing a write-only open, we may not have a write-only count
+        * if we also grabbed read access.  So, check the read-write count.
+        */
+       if (denyMode == NFS_OPEN_SHARE_DENY_NONE) {
+               if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
+                   (nofp->nof_w == 0) && (nofp->nof_d_w == 0) &&
+                   (nofp->nof_rw || nofp->nof_d_rw))
+                       accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
+       } else if (denyMode == NFS_OPEN_SHARE_DENY_WRITE) {
+               if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
+                   (nofp->nof_w_dw == 0) && (nofp->nof_d_w_dw == 0) &&
+                   (nofp->nof_rw_dw || nofp->nof_d_rw_dw))
+                       accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
+       } else { /* NFS_OPEN_SHARE_DENY_BOTH */
+               if ((accessMode == NFS_OPEN_SHARE_ACCESS_WRITE) &&
+                   (nofp->nof_w_drw == 0) && (nofp->nof_d_w_drw == 0) &&
+                   (nofp->nof_rw_drw || nofp->nof_d_rw_drw))
+                       accessMode = NFS_OPEN_SHARE_ACCESS_BOTH;
+       }
+
+       nfs_open_file_remove_open_find(nofp, accessMode, denyMode, &newAccessMode, &newDenyMode, &delegated);
+       if ((newAccessMode != nofp->nof_access) || (newDenyMode != nofp->nof_deny))
+               changed = 1;
+       else
+               changed = 0;
+
+       if (NFSTONMP(np)->nm_vers < NFS_VER4) /* NFS v2/v3 closes simply need to remove the open. */
+               goto v3close;
+
+       if ((newAccessMode == 0) || (nofp->nof_opencnt == 1)) {
+               /*
+                * No more access after this close, so clean up and close it.
+                * Don't send a close RPC if we're closing a delegated open.
+                */
+               nfs_wait_bufs(np);
+               closed = 1;
+               if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST))
+                       error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0);
+               if (error == NFSERR_LOCKS_HELD) {
+                       /*
+                        * Hmm... the server says we have locks we need to release first
+                        * Find the lock owner and try to unlock everything.
+                        */
+                       nlop = nfs_lock_owner_find(np, vfs_context_proc(ctx), 0);
+                       if (nlop) {
+                               nfs4_unlock_rpc(np, nlop, F_WRLCK, 0, UINT64_MAX,
+                                       0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
+                               nfs_lock_owner_rele(nlop);
+                       }
+                       error = nfs4_close_rpc(np, nofp, vfs_context_thread(ctx), vfs_context_ucred(ctx), 0);
+               }
+       } else if (changed) {
+               /*
+                * File is still open but with less access, so downgrade the open.
+                * Don't send a downgrade RPC if we're closing a delegated open.
+                */
+               if (!delegated && !(nofp->nof_flags & NFS_OPEN_FILE_LOST)) {
+                       downgrade = 1;
+                       /*
+                        * If we have delegated opens, we should probably claim them before sending
+                        * the downgrade because the server may not know the open we are downgrading to.
+                        */
+                       if (nofp->nof_d_rw_drw || nofp->nof_d_w_drw || nofp->nof_d_r_drw ||
+                           nofp->nof_d_rw_dw || nofp->nof_d_w_dw || nofp->nof_d_r_dw ||
+                           nofp->nof_d_rw || nofp->nof_d_w || nofp->nof_d_r)
+                               nfs4_claim_delegated_state_for_open_file(nofp, 0);
+                       /* need to remove the open before sending the downgrade */
+                       nfs_open_file_remove_open(nofp, accessMode, denyMode);
+                       error = nfs4_open_downgrade_rpc(np, nofp, ctx);
+                       if (error) /* Hmm.. that didn't work. Add the open back in. */
+                               nfs_open_file_add_open(nofp, accessMode, denyMode, delegated);
+               }
+       }
+
+       if (error) {
+               NP(np, "nfs_close: error %d, %d", error, kauth_cred_getuid(nofp->nof_owner->noo_cred));
+               return (error);
+       }
+
+v3close:
+       if (!downgrade)
+               nfs_open_file_remove_open(nofp, accessMode, denyMode);
+
+       if (closed) {
+               lck_mtx_lock(&nofp->nof_lock);
+               if (nofp->nof_r || nofp->nof_d_r || nofp->nof_w || nofp->nof_d_w || nofp->nof_d_rw ||
+                   (nofp->nof_rw && !((nofp->nof_flags & NFS_OPEN_FILE_CREATE) && !nofp->nof_creator && (nofp->nof_rw == 1))) ||
+                   nofp->nof_r_dw || nofp->nof_d_r_dw || nofp->nof_w_dw || nofp->nof_d_w_dw ||
+                   nofp->nof_rw_dw || nofp->nof_d_rw_dw || nofp->nof_r_drw || nofp->nof_d_r_drw ||
+                   nofp->nof_w_drw || nofp->nof_d_w_drw || nofp->nof_rw_drw || nofp->nof_d_rw_drw)
+                       NP(np, "nfs_close: unexpected count: %u.%u %u.%u %u.%u dw %u.%u %u.%u %u.%u drw %u.%u %u.%u %u.%u flags 0x%x, %d",
+                               nofp->nof_r, nofp->nof_d_r, nofp->nof_w, nofp->nof_d_w,
+                               nofp->nof_rw, nofp->nof_d_rw, nofp->nof_r_dw, nofp->nof_d_r_dw,
+                               nofp->nof_w_dw, nofp->nof_d_w_dw, nofp->nof_rw_dw, nofp->nof_d_rw_dw,
+                               nofp->nof_r_drw, nofp->nof_d_r_drw, nofp->nof_w_drw, nofp->nof_d_w_drw,
+                               nofp->nof_rw_drw, nofp->nof_d_rw_drw, nofp->nof_flags,
+                               kauth_cred_getuid(nofp->nof_owner->noo_cred));
+               /* clear out all open info, just to be safe */
+               nofp->nof_access = nofp->nof_deny = 0;
+               nofp->nof_mmap_access = nofp->nof_mmap_deny = 0;
+               nofp->nof_r = nofp->nof_d_r = 0;
+               nofp->nof_w = nofp->nof_d_w = 0;
+               nofp->nof_rw = nofp->nof_d_rw = 0;
+               nofp->nof_r_dw = nofp->nof_d_r_dw = 0;
+               nofp->nof_w_dw = nofp->nof_d_w_dw = 0;
+               nofp->nof_rw_dw = nofp->nof_d_rw_dw = 0;
+               nofp->nof_r_drw = nofp->nof_d_r_drw = 0;
+               nofp->nof_w_drw = nofp->nof_d_w_drw = 0;
+               nofp->nof_rw_drw = nofp->nof_d_rw_drw = 0;
+               nofp->nof_flags &= ~NFS_OPEN_FILE_CREATE;
+               lck_mtx_unlock(&nofp->nof_lock);
+               /* XXX we may potentially want to clean up idle/unused open file structures */
+       }
+       if (nofp->nof_flags & NFS_OPEN_FILE_LOST) {
+               error = EIO;
+               NP(np, "nfs_close: LOST%s, %d", !nofp->nof_opencnt ? " (last)" : "",
+                       kauth_cred_getuid(nofp->nof_owner->noo_cred));
+       }
+               
        return (error);
 }
 
 
+
+
 int
 nfs3_getattr_rpc(
        nfsnode_t np,
        mount_t mp,
        u_char *fhp,
        size_t fhsize,
+       int flags,
        vfs_context_t ctx,
        struct nfs_vattr *nvap,
        u_int64_t *xidp)
 {
        struct nfsmount *nmp = mp ? VFSTONFS(mp) : NFSTONMP(np);
-       int error = 0, status, nfsvers;
+       int error = 0, status, nfsvers, rpcflags = 0;
        struct nfsm_chain nmreq, nmrep;
 
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
 
+       if (flags & NGA_MONITOR) /* vnode monitor requests should be soft */
+               rpcflags = R_RECOVER;
+
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
 
@@ -810,8 +1296,9 @@ nfs3_getattr_rpc(
        nfsm_chain_add_opaque(error, &nmreq, fhp, fhsize);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, mp, &nmreq, NFSPROC_GETATTR, ctx,
-                       &nmrep, xidp, &status);
+       error = nfs_request2(np, mp, &nmreq, NFSPROC_GETATTR, 
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx),
+                       NULL, rpcflags, &nmrep, xidp, &status);
        if (!error)
                error = status;
        nfsmout_if(error);
@@ -824,111 +1311,177 @@ nfsmout:
 
 
 int
-nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int alreadylocked)
+nfs_getattr(nfsnode_t np, struct nfs_vattr *nvap, vfs_context_t ctx, int flags)
 {
        struct nfsmount *nmp;
-       int error = 0, lockerror = ENOENT, nfsvers, avoidfloods;
+       int error = 0, nfsvers, inprogset = 0, wanted = 0, avoidfloods;
+       struct nfs_vattr nvattr;
+       struct timespec ts = { 2, 0 };
        u_int64_t xid;
 
        FSDBG_TOP(513, np->n_size, np, np->n_vattr.nva_size, np->n_flag);
 
+       if (!(nmp = NFSTONMP(np)))
+               return (ENXIO);
+       nfsvers = nmp->nm_vers;
+
+       if (!nvap)
+               nvap = &nvattr;
+       NVATTR_INIT(nvap);
+
        /* Update local times for special files. */
        if (np->n_flag & (NACC | NUPD)) {
-               if (!alreadylocked)
-                       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+               nfs_node_lock_force(np);
                np->n_flag |= NCHG;
-               if (!alreadylocked)
-                       nfs_unlock(np);
+               nfs_node_unlock(np);
        }
        /* Update size, if necessary */
-       if (!alreadylocked && ISSET(np->n_flag, NUPDATESIZE))
+       if (ISSET(np->n_flag, NUPDATESIZE))
                nfs_data_update_size(np, 0);
 
-       /*
-        * First look in the cache.
-        */
-       if ((error = nfs_getattrcache(np, nvap, alreadylocked)) == 0)
-               goto nfsmout;
-       if (error != ENOENT)
-               goto nfsmout;
+       error = nfs_node_lock(np);
+       nfsmout_if(error);
+       if (!(flags & (NGA_UNCACHED|NGA_MONITOR)) || ((nfsvers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))) {
+               /*
+                * Use the cache or wait for any getattr in progress if:
+                * - it's a cached request, or
+                * - we have a delegation
+                */
+               while (1) {
+                       error = nfs_getattrcache(np, nvap, flags);
+                       if (!error || (error != ENOENT)) {
+                               nfs_node_unlock(np);
+                               goto nfsmout;
+                       }
+                       error = 0;
+                       if (!ISSET(np->n_flag, NGETATTRINPROG))
+                               break;
+                       if (flags & NGA_MONITOR) {
+                               /* no need to wait if a request is pending */
+                               error = EINPROGRESS;
+                               nfs_node_unlock(np);
+                               goto nfsmout;
+                       }
+                       SET(np->n_flag, NGETATTRWANT);
+                       msleep(np, &np->n_lock, PZERO-1, "nfsgetattrwant", &ts);
+                       if ((error = nfs_sigintr(NFSTONMP(np), NULL, vfs_context_thread(ctx), 0))) {
+                               nfs_node_unlock(np);
+                               goto nfsmout;
+                       }
+               }
+               SET(np->n_flag, NGETATTRINPROG);
+               inprogset = 1;
+       } else if (!ISSET(np->n_flag, NGETATTRINPROG)) {
+               SET(np->n_flag, NGETATTRINPROG);
+               inprogset = 1;
+       } else if (flags & NGA_MONITOR) {
+               /* no need to make a request if one is pending */
+               error = EINPROGRESS;
+       }
+       nfs_node_unlock(np);
 
        nmp = NFSTONMP(np);
-       if (!nmp) {
+       if (!nmp)
                error = ENXIO;
+       if (error)
                goto nfsmout;
-       }
-       nfsvers = nmp->nm_vers;
 
        /*
-        * Try to get both the attributes and access info by making an
-        * ACCESS call and seeing if it returns updated attributes.
+        * We might want to try to get both the attributes and access info by
+        * making an ACCESS call and seeing if it returns updated attributes.
         * But don't bother if we aren't caching access info or if the
         * attributes returned wouldn't be cached.
         */
-       if ((nfsvers != NFS_VER2) && (nfs_access_cache_timeout > 0)) {
-               if (!alreadylocked && ((error = lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE))))
-                       goto nfsmout;
+       if (!(flags & NGA_ACL) && (nfsvers != NFS_VER2) && nfs_access_for_getattr && (nfs_access_cache_timeout > 0)) {
                if (nfs_attrcachetimeout(np) > 0) {
-                       /*  OSAddAtomic(1, (SInt32*)&nfsstats.accesscache_misses); */
-                       u_long mode = NFS_ACCESS_ALL;
-                       error = nmp->nm_funcs->nf_access_rpc(np, &mode, ctx);
+                       /*  OSAddAtomic(1, &nfsstats.accesscache_misses); */
+                       u_int32_t access = NFS_ACCESS_ALL;
+                       error = nmp->nm_funcs->nf_access_rpc(np, &access, ctx);
                        if (error)
                                goto nfsmout;
-                       if ((error = nfs_getattrcache(np, nvap, 1)) == 0)
-                               goto nfsmout;
-                       if (error != ENOENT)
+                       nfs_node_lock_force(np);
+                       error = nfs_getattrcache(np, nvap, flags);
+                       nfs_node_unlock(np);
+                       if (!error || (error != ENOENT))
                                goto nfsmout;
+                       /* Well, that didn't work... just do a getattr... */
                        error = 0;
                }
-       } else if (!alreadylocked) {
-               error = lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
-               nfsmout_if(error);
        }
+
        avoidfloods = 0;
 tryagain:
-       error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, ctx, nvap, &xid);
-       nfsmout_if(error);
-       error = nfs_loadattrcache(np, nvap, &xid, 0);
+       error = nmp->nm_funcs->nf_getattr_rpc(np, NULL, np->n_fhp, np->n_fhsize, flags, ctx, nvap, &xid);
+       if (!error) {
+               nfs_node_lock_force(np);
+               error = nfs_loadattrcache(np, nvap, &xid, 0);
+               nfs_node_unlock(np);
+       }
        nfsmout_if(error);
        if (!xid) { /* out-of-order rpc - attributes were dropped */
                FSDBG(513, -1, np, np->n_xid >> 32, np->n_xid);
-               if (avoidfloods++ < 100)
+               if (avoidfloods++ < 20)
                        goto tryagain;
-               /* avoidfloods>1 is bizarre.  at 100 pull the plug */
-               panic("nfs_getattr: getattr flood\n");
+               /* avoidfloods>1 is bizarre.  at 20 pull the plug */
+               /* just return the last attributes we got */
+       }
+nfsmout:
+       nfs_node_lock_force(np);
+       if (inprogset) {
+               wanted = ISSET(np->n_flag, NGETATTRWANT);
+               CLR(np->n_flag, (NGETATTRINPROG | NGETATTRWANT));
        }
-       if (NFS_CHANGED(nfsvers, np, nvap)) {
+       if (!error) {
+               /* check if the node changed on us */
                vnode_t vp = NFSTOV(np);
                enum vtype vtype = vnode_vtype(vp);
-               FSDBG(513, -1, np, -1, np);
-               if (vtype == VDIR) {
-                       nfs_invaldir(np);
-                       /* purge name cache entries */
-                       if (NFS_CHANGED_NC(nfsvers, np, nvap)) {
-                               np->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(vp);
-                       }
+               if ((vtype == VDIR) && NFS_CHANGED_NC(nfsvers, np, nvap)) {
+                       FSDBG(513, -1, np, 0, np);
+                       np->n_flag &= ~NNEGNCENTRIES;
+                       cache_purge(vp);
+                       np->n_ncgen++;
+                       NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap);
+                       NFS_VNOP_DBG("Purge directory 0x%llx\n", 
+                             (uint64_t)VM_KERNEL_ADDRPERM(vp));
                }
-               if (!alreadylocked) {
-                       nfs_unlock(np);
-                       lockerror = ENOENT;
+               if (NFS_CHANGED(nfsvers, np, nvap)) {
+                       FSDBG(513, -1, np, -1, np);
+                       if (vtype == VDIR) {
+                               NFS_VNOP_DBG("Invalidate directory 0x%llx\n", 
+                                      (uint64_t)VM_KERNEL_ADDRPERM(vp));
+                               nfs_invaldir(np);
+                       }
+                       nfs_node_unlock(np);
+                       if (wanted)
+                               wakeup(np);
                        error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
                        FSDBG(513, -1, np, -2, error);
-                       if (!error)
-                               error = lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
                        if (!error) {
-                               if (vtype == VDIR)
-                                       NFS_CHANGED_UPDATE_NC(nfsvers, np, nvap);
+                               nfs_node_lock_force(np);
                                NFS_CHANGED_UPDATE(nfsvers, np, nvap);
+                               nfs_node_unlock(np);
                        }
                } else {
-                       /* invalidate later */
-                       np->n_flag |= NNEEDINVALIDATE;
+                       nfs_node_unlock(np);
+                       if (wanted)
+                               wakeup(np);
+               }
+       } else {
+               nfs_node_unlock(np);
+               if (wanted)
+                       wakeup(np);
+       }
+
+       if (nvap == &nvattr) {
+               NVATTR_CLEANUP(nvap);
+       } else if (!(flags & NGA_ACL)) {
+               /* make sure we don't return an ACL if it wasn't asked for */
+               NFS_BITMAP_CLR(nvap->nva_bitmap, NFS_FATTR_ACL);
+               if (nvap->nva_acl) {
+                       kauth_acl_free(nvap->nva_acl);
+                       nvap->nva_acl = NULL;
                }
        }
-nfsmout:
-       if (!lockerror)
-               nfs_unlock(np);
        FSDBG_BOT(513, np->n_size, error, np->n_vattr.nva_size, np->n_flag);
        return (error);
 }
@@ -936,7 +1489,29 @@ nfsmout:
 /*
  * NFS getattr call from vfs.
  */
-static int
+
+/*
+ * The attributes we support over the wire.
+ * We also get fsid but the vfs layer gets it out of the mount 
+ * structure after this calling us so there's no need to return it,
+ * and Finder expects to call getattrlist just looking for the FSID
+ * with out hanging on a non responsive server.
+ */
+#define NFS3_SUPPORTED_VATTRS \
+       (VNODE_ATTR_va_rdev |           \
+        VNODE_ATTR_va_nlink |          \
+        VNODE_ATTR_va_data_size |      \
+        VNODE_ATTR_va_data_alloc |     \
+        VNODE_ATTR_va_uid |            \
+        VNODE_ATTR_va_gid |            \
+        VNODE_ATTR_va_mode |           \
+        VNODE_ATTR_va_modify_time |    \
+        VNODE_ATTR_va_change_time |    \
+        VNODE_ATTR_va_access_time |    \
+        VNODE_ATTR_va_fileid |         \
+        VNODE_ATTR_va_type)
+
+int
 nfs3_vnop_getattr(
        struct vnop_getattr_args /* {
                struct vnodeop_desc *a_desc;
@@ -950,7 +1525,20 @@ nfs3_vnop_getattr(
        struct vnode_attr *vap = ap->a_vap;
        dev_t rdev;
 
-       error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, 0);
+       /*
+        * Lets don't go over the wire if we don't support any of the attributes.
+        * Just fall through at the VFS layer and let it cons up what it needs.
+        */
+       /* Return the io size no matter what, since we don't go over the wire for this */
+       VATTR_RETURN(vap, va_iosize, nfs_iosize);
+       if ((vap->va_active & NFS3_SUPPORTED_VATTRS) == 0)
+               return (0);
+
+       if (VATTR_IS_ACTIVE(ap->a_vap, va_name))
+           NFS_VNOP_DBG("Getting attrs for 0x%llx, vname is %s\n", 
+                 (uint64_t)VM_KERNEL_ADDRPERM(ap->a_vp),
+                 ap->a_vp->v_name ? ap->a_vp->v_name : "empty");
+       error = nfs_getattr(VTONFS(ap->a_vp), &nva, ap->a_context, NGA_CACHED);
        if (error)
                return (error);
 
@@ -965,7 +1553,6 @@ nfs3_vnop_getattr(
        VATTR_RETURN(vap, va_fileid, nva.nva_fileid);
        VATTR_RETURN(vap, va_data_size, nva.nva_size);
        VATTR_RETURN(vap, va_data_alloc, nva.nva_bytes);
-       VATTR_RETURN(vap, va_iosize, nfs_iosize);
        vap->va_access_time.tv_sec = nva.nva_timesec[NFSTIME_ACCESS];
        vap->va_access_time.tv_nsec = nva.nva_timensec[NFSTIME_ACCESS];
        VATTR_SET_SUPPORTED(vap, va_access_time);
@@ -983,7 +1570,7 @@ nfs3_vnop_getattr(
 /*
  * NFS setattr call.
  */
-static int
+int
 nfs_vnop_setattr(
        struct vnop_setattr_args /* {
                struct vnodeop_desc *a_desc;
@@ -998,17 +1585,20 @@ nfs_vnop_setattr(
        struct nfsmount *nmp;
        struct vnode_attr *vap = ap->a_vap;
        int error = 0;
-       int biosize, nfsvers;
-       u_quad_t origsize;
+       int biosize, nfsvers, namedattrs;
+       u_quad_t origsize, vapsize;
        struct nfs_dulookup dul;
        nfsnode_t dnp = NULL;
        vnode_t dvp = NULL;
        const char *vname = NULL;
+       struct nfs_open_owner *noop = NULL;
+       struct nfs_open_file *nofp = NULL;
 
        nmp = VTONMP(vp);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
+       namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
        biosize = nmp->nm_biosize;
 
        /* Disallow write attempts if the filesystem is mounted read-only. */
@@ -1043,19 +1633,63 @@ nfs_vnop_setattr(
                        FSDBG_TOP(512, np->n_size, vap->va_data_size,
                                  np->n_vattr.nva_size, np->n_flag);
                        /* clear NNEEDINVALIDATE, if set */
-                       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+                       if ((error = nfs_node_lock(np)))
                                return (error);
                        if (np->n_flag & NNEEDINVALIDATE)
                                np->n_flag &= ~NNEEDINVALIDATE;
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        /* flush everything */
                        error = nfs_vinvalbuf(vp, (vap->va_data_size ? V_SAVE : 0) , ctx, 1);
                        if (error) {
-                               printf("nfs_setattr: nfs_vinvalbuf %d\n", error);
+                               NP(np, "nfs_setattr: nfs_vinvalbuf %d", error);
                                FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, -1);
                                return (error);
                        }
-                       nfs_data_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
+                       if (nfsvers >= NFS_VER4) {
+                               /* setting file size requires having the file open for write access */
+                               if (np->n_flag & NREVOKE)
+                                       return (EIO);
+                               noop = nfs_open_owner_find(nmp, vfs_context_ucred(ctx), 1);
+                               if (!noop)
+                                       return (ENOMEM);
+restart:
+                               error = nfs_mount_state_in_use_start(nmp, vfs_context_thread(ctx));
+                               if (error)
+                                       return (error);
+                               if (np->n_flag & NREVOKE) {
+                                       nfs_mount_state_in_use_end(nmp, 0);
+                                       return (EIO);
+                               }
+                               error = nfs_open_file_find(np, noop, &nofp, 0, 0, 1);
+                               if (!error && (nofp->nof_flags & NFS_OPEN_FILE_LOST))
+                                       error = EIO;
+                               if (!error && (nofp->nof_flags & NFS_OPEN_FILE_REOPEN)) {
+                                       nfs_mount_state_in_use_end(nmp, 0);
+                                       error = nfs4_reopen(nofp, vfs_context_thread(ctx));
+                                       nofp = NULL;
+                                       if (!error)
+                                               goto restart;
+                               }
+                               if (!error)
+                                       error = nfs_open_file_set_busy(nofp, vfs_context_thread(ctx));
+                               if (error) {
+                                       nfs_open_owner_rele(noop);
+                                       return (error);
+                               }
+                               if (!(nofp->nof_access & NFS_OPEN_SHARE_ACCESS_WRITE)) {
+                                       /* we don't have the file open for write access, so open it */
+                                       error = nfs4_open(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx);
+                                       if (!error)
+                                               nofp->nof_flags |= NFS_OPEN_FILE_SETATTR;
+                                       if (nfs_mount_state_error_should_restart(error)) {
+                                               nfs_open_file_clear_busy(nofp);
+                                               nofp = NULL;
+                                               if (nfs_mount_state_in_use_end(nmp, error))
+                                                       goto restart;
+                                       }
+                               }
+                       }
+                       nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
                        if (np->n_size > vap->va_data_size) { /* shrinking? */
                                daddr64_t obn, bn;
                                int neweofoff, mustwrite;
@@ -1111,7 +1745,7 @@ nfs_vnop_setattr(
                                        // Note: bp has been released
                                        if (error) {
                                                FSDBG(512, bp, 0xd00dee, 0xbad, error);
-                                               nfs_lock(np, NFS_NODE_LOCK_FORCE);
+                                               nfs_node_lock_force(np);
                                                np->n_error = error;
                                                np->n_flag |= NWRITEERR;
                                                /*
@@ -1122,10 +1756,10 @@ nfs_vnop_setattr(
                                                 * we may no longer know the correct size)
                                                 */
                                                NATTRINVALIDATE(np);
-                                               nfs_unlock(np);
+                                               nfs_node_unlock(np);
                                                nfs_data_unlock(np);
                                                nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
-                                               nfs_data_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
+                                               nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
                                                error = 0;
                                        }
                                }
@@ -1134,79 +1768,118 @@ nfs_vnop_setattr(
                                ubc_setsize(vp, (off_t)vap->va_data_size); /* XXX error? */
                        origsize = np->n_size;
                        np->n_size = np->n_vattr.nva_size = vap->va_data_size;
+                       nfs_node_lock_force(np);
                        CLR(np->n_flag, NUPDATESIZE);
+                       nfs_node_unlock(np);
                        FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
                }
        } else if (VATTR_IS_ACTIVE(vap, va_modify_time) ||
                    VATTR_IS_ACTIVE(vap, va_access_time) ||
                    (vap->va_vaflags & VA_UTIMES_NULL)) {
-               if ((error = nfs_lock(np, NFS_NODE_LOCK_SHARED)))
+               if ((error = nfs_node_lock(np)))
                        return (error);
                if ((np->n_flag & NMODIFIED) && (vnode_vtype(vp) == VREG)) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
                        if (error == EINTR)
                                return (error);
                } else {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                }
        }
-       if (VATTR_IS_ACTIVE(vap, va_mode) ||
-           VATTR_IS_ACTIVE(vap, va_uid) ||
-           VATTR_IS_ACTIVE(vap, va_gid)) {
-               if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE))) {
-                       if (VATTR_IS_ACTIVE(vap, va_data_size))
-                               nfs_data_unlock(np);
-                       return (error);
-               }
-               NMODEINVALIDATE(np);
-               nfs_unlock(np);
-               dvp = vnode_getparent(vp);
-               vname = vnode_getname(vp);
-               dnp = (dvp && vname) ? VTONFS(dvp) : NULL;
-               if (dnp) {
-                       error = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE);
-                       if (error) {
-                               dnp = NULL;
-                               error = 0;
+       if ((VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) ||
+            VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) &&
+           !(error = nfs_node_lock(np))) {
+               NACCESSINVALIDATE(np);
+               nfs_node_unlock(np);
+               if (!namedattrs) {
+                       dvp = vnode_getparent(vp);
+                       vname = vnode_getname(vp);
+                       dnp = (dvp && vname) ? VTONFS(dvp) : NULL;
+                       if (dnp) {
+                               error = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
+                               if (error) {
+                                       dnp = NULL;
+                                       error = 0;
+                               }
+                       }
+                       if (dnp) {
+                               nfs_dulookup_init(&dul, dnp, vname, strlen(vname), ctx);
+                               nfs_dulookup_start(&dul, dnp, ctx);
                        }
-               }
-               if (dnp) {
-                       nfs_dulookup_init(&dul, dnp, vname, strlen(vname));
-                       nfs_dulookup_start(&dul, dnp, ctx);
                }
        }
 
-       error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx, 0);
-
-       if (VATTR_IS_ACTIVE(vap, va_mode) ||
-           VATTR_IS_ACTIVE(vap, va_uid) ||
-           VATTR_IS_ACTIVE(vap, va_gid)) {
-               if (dnp) {
-                       nfs_dulookup_finish(&dul, dnp, ctx);
-                       nfs_unlock(dnp);
+       if (!error)
+               error = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
+
+       if (VATTR_IS_ACTIVE(vap, va_mode) || VATTR_IS_ACTIVE(vap, va_uid) || VATTR_IS_ACTIVE(vap, va_gid) ||
+           VATTR_IS_ACTIVE(vap, va_acl) || VATTR_IS_ACTIVE(vap, va_uuuid) || VATTR_IS_ACTIVE(vap, va_guuid)) {
+               if (!namedattrs) {
+                       if (dnp) {
+                               nfs_dulookup_finish(&dul, dnp, ctx);
+                               nfs_node_clear_busy(dnp);
+                       }
+                       if (dvp != NULLVP)
+                               vnode_put(dvp);
+                       if (vname != NULL)
+                               vnode_putname(vname);
                }
-               if (dvp != NULLVP)
-                       vnode_put(dvp);
-               if (vname != NULL)
-                       vnode_putname(vname);
        }
 
        FSDBG_BOT(512, np->n_size, vap->va_data_size, np->n_vattr.nva_size, error);
        if (VATTR_IS_ACTIVE(vap, va_data_size)) {
-               if (error && (origsize != np->n_size)) {
+               if (error && (origsize != np->n_size) &&
+                   ((nfsvers < NFS_VER4) || !nfs_mount_state_error_should_restart(error))) {
                        /* make every effort to resync file size w/ server... */
+                       /* (don't bother if we'll be restarting the operation) */
                        int err; /* preserve "error" for return */
                        np->n_size = np->n_vattr.nva_size = origsize;
+                       nfs_node_lock_force(np);
                        CLR(np->n_flag, NUPDATESIZE);
+                       nfs_node_unlock(np);
                        FSDBG(512, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002);
                        ubc_setsize(vp, (off_t)np->n_size); /* XXX check error */
+                       vapsize = vap->va_data_size;
                        vap->va_data_size = origsize;
-                       err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx, 0);
+                       err = nmp->nm_funcs->nf_setattr_rpc(np, vap, ctx);
                        if (err)
-                               printf("nfs_vnop_setattr: nfs%d_setattr_rpc %d %d\n", nfsvers, error, err);
+                               NP(np, "nfs_vnop_setattr: nfs%d_setattr_rpc %d %d", nfsvers, error, err);
+                       vap->va_data_size = vapsize;
+               }
+               nfs_node_lock_force(np);
+               /*
+                * The size was just set.  If the size is already marked for update, don't
+                * trust the newsize (it may have been set while the setattr was in progress).
+                * Clear the update flag and make sure we fetch new attributes so we are sure
+                * we have the latest size.
+                */
+               if (ISSET(np->n_flag, NUPDATESIZE)) {
+                       CLR(np->n_flag, NUPDATESIZE);
+                       NATTRINVALIDATE(np);
+                       nfs_node_unlock(np);
+                       nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
+               } else {
+                       nfs_node_unlock(np);
                }
                nfs_data_unlock(np);
+               if (nfsvers >= NFS_VER4) {
+                       if (nofp) {
+                               /* don't close our setattr open if we'll be restarting... */
+                               if (!nfs_mount_state_error_should_restart(error) &&
+                                   (nofp->nof_flags & NFS_OPEN_FILE_SETATTR)) {
+                                       int err = nfs_close(np, nofp, NFS_OPEN_SHARE_ACCESS_WRITE, NFS_OPEN_SHARE_DENY_NONE, ctx);
+                                       if (err)
+                                               NP(np, "nfs_vnop_setattr: close error: %d", err);
+                                       nofp->nof_flags &= ~NFS_OPEN_FILE_SETATTR;
+                               }
+                               nfs_open_file_clear_busy(nofp);
+                               nofp = NULL;
+                       }
+                       if (nfs_mount_state_in_use_end(nmp, error))
+                               goto restart;
+                       nfs_open_owner_rele(noop);
+               }
        }
        return (error);
 }
@@ -1218,12 +1891,11 @@ int
 nfs3_setattr_rpc(
        nfsnode_t np,
        struct vnode_attr *vap,
-       vfs_context_t ctx,
-       int alreadylocked)
+       vfs_context_t ctx)
 {
        struct nfsmount *nmp = NFSTONMP(np);
        int error = 0, lockerror = ENOENT, status, wccpostattr = 0, nfsvers;
-       u_int64_t xid;
+       u_int64_t xid, nextxid;
        struct nfsm_chain nmreq, nmrep;
 
        if (!nmp)
@@ -1328,9 +2000,8 @@ nfs3_setattr_rpc(
        }
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx,
-                       &nmrep, &xid, &status);
-       if (!alreadylocked && ((lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE))))
+       error = nfs_request(np, NULL, &nmreq, NFSPROC_SETATTR, ctx, NULL, &nmrep, &xid, &status);
+       if ((lockerror = nfs_node_lock(np)))
                error = lockerror;
        if (nfsvers == NFS_VER3) {
                struct timespec premtime = { 0, 0 };
@@ -1349,11 +2020,26 @@ nfs3_setattr_rpc(
        } else {
                if (!error)
                        error = status;
-               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, NULL, &xid);
+               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
+       }
+       /*
+        * We just changed the attributes and we want to make sure that we
+        * see the latest attributes.  Get the next XID.  If it's not the
+        * next XID after the SETATTR XID, then it's possible that another
+        * RPC was in flight at the same time and it might put stale attributes
+        * in the cache.  In that case, we invalidate the attributes and set
+        * the attribute cache XID to guarantee that newer attributes will
+        * get loaded next.
+        */
+       nextxid = 0;
+       nfs_get_xid(&nextxid);
+       if (nextxid != (xid + 1)) {
+               np->n_xid = nextxid;
+               NATTRINVALIDATE(np);
        }
 nfsmout:
-       if (!alreadylocked && !lockerror)
-               nfs_unlock(np);
+       if (!lockerror)
+               nfs_node_unlock(np);
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
        return (error);
@@ -1364,7 +2050,7 @@ nfsmout:
  * First look in cache
  * If not found, unlock the directory nfsnode and do the RPC
  */
-static int
+int
 nfs_vnop_lookup(
        struct vnop_lookup_args /* {
                struct vnodeop_desc *a_desc;
@@ -1383,7 +2069,7 @@ nfs_vnop_lookup(
        nfsnode_t dnp, np;
        struct nfsmount *nmp;
        mount_t mp;
-       int nfsvers, error, lockerror = ENOENT, isdot, isdotdot, negnamecache;
+       int nfsvers, error, busyerror = ENOENT, isdot, isdotdot, negnamecache;
        u_int64_t xid;
        struct nfs_vattr nvattr;
        int ngflags;
@@ -1394,6 +2080,7 @@ nfs_vnop_lookup(
        *vpp = NULLVP;
 
        dnp = VTONFS(dvp);
+       NVATTR_INIT(&nvattr);
 
        mp = vnode_mount(dvp);
        nmp = VFSTONFS(mp);
@@ -1402,22 +2089,13 @@ nfs_vnop_lookup(
                goto error_return;
        }
        nfsvers = nmp->nm_vers;
-       negnamecache = !(nmp->nm_flag & NFSMNT_NONEGNAMECACHE);
+       negnamecache = !NMFLAG(nmp, NONEGNAMECACHE);
 
-       error = lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE);
-       if (!error)
-               error = nfs_getattr(dnp, &nvattr, ctx, 1);
-       if (error)
+       if ((error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx))))
+               goto error_return;
+       /* nfs_getattr() will check changed and purge caches */
+       if ((error = nfs_getattr(dnp, NULL, ctx, NGA_CACHED)))
                goto error_return;
-       if (NFS_CHANGED_NC(nfsvers, dnp, &nvattr)) {
-               /*
-                * This directory has changed on us.
-                * Purge any name cache entries.
-                */
-               dnp->n_flag &= ~NNEGNCENTRIES;
-               cache_purge(dvp);
-               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &nvattr);
-       }
 
        error = cache_lookup(dvp, vpp, cnp);
        switch (error) {
@@ -1426,15 +2104,27 @@ nfs_vnop_lookup(
                goto error_return;
        case 0:
                /* cache miss */
-               break;
+               if ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
+                       /* if rdirplus, try dir buf cache lookup */
+                       error = nfs_dir_buf_cache_lookup(dnp, &np, cnp, ctx, 0);
+                       if (!error && np) {
+                               /* dir buf cache hit */
+                               *vpp = NFSTOV(np);
+                               error = -1;
+                       }
+               }
+               if (error != -1) /* cache miss */
+                       break;
+               /* FALLTHROUGH */
        case -1:
                /* cache hit, not really an error */
-               OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_hits);
+               OSAddAtomic64(1, &nfsstats.lookupcache_hits);
 
-               nfs_unlock(dnp);
-               lockerror = ENOENT;
+               nfs_node_clear_busy(dnp);
+               busyerror = ENOENT;
 
                /* check for directory access */
+               naa.a_desc = &vnop_access_desc;
                naa.a_vp = dvp;
                naa.a_action = KAUTH_VNODE_SEARCH;
                naa.a_context = ctx;
@@ -1459,6 +2149,11 @@ nfs_vnop_lookup(
                fh.fh_len = 0;
                goto found;
        }
+       if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) {
+               /* we should never be looking things up in a trigger directory, return nothing */
+               error = ENOENT;
+               goto error_return;
+       }
 
        /* do we know this name is too long? */
        nmp = VTONMP(dvp);
@@ -1467,7 +2162,7 @@ nfs_vnop_lookup(
                goto error_return;
        }
        if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
-            (cnp->cn_namelen > (long)nmp->nm_fsattr.nfsa_maxname)) {
+            (cnp->cn_namelen > (int)nmp->nm_fsattr.nfsa_maxname)) {
                error = ENAMETOOLONG;
                goto error_return;
        }
@@ -1475,18 +2170,17 @@ nfs_vnop_lookup(
        error = 0;
        newvp = NULLVP;
 
-       OSAddAtomic(1, (SInt32*)&nfsstats.lookupcache_misses);
+       OSAddAtomic64(1, &nfsstats.lookupcache_misses);
 
        error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &req);
        nfsmout_if(error);
-       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, ctx, req, &xid, &fh, &nvattr);
+       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, req, &xid, &fh, &nvattr);
        nfsmout_if(error);
 
        /* is the file handle the same as this directory's file handle? */
        isdot = NFS_CMPFH(dnp, fh.fh_data, fh.fh_len);
 
 found:
-
        if (flags & ISLASTCN) {
                switch (cnp->cn_nameiop) {
                case DELETE:
@@ -1503,8 +2197,6 @@ found:
        }
 
        if (isdotdot) {
-               nfs_unlock(dnp);
-               lockerror = ENOENT;
                newvp = vnode_getparent(dvp);
                if (!newvp) {
                        error = ENOENT;
@@ -1515,15 +2207,17 @@ found:
                if (error)
                        goto error_return;
                newvp = dvp;
+               nfs_node_lock_force(dnp);
                if (fh.fh_len && (dnp->n_xid <= xid))
                        nfs_loadattrcache(dnp, &nvattr, &xid, 0);
+               nfs_node_unlock(dnp);
        } else {
                ngflags = (cnp->cn_flags & MAKEENTRY) ? NG_MAKEENTRY : 0;
-               error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, ngflags, &np);
+               error = nfs_nget(mp, dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, ngflags, &np);
                if (error)
                        goto error_return;
                newvp = NFSTOV(np);
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        }
        *vpp = newvp;
 
@@ -1540,12 +2234,15 @@ nfsmout:
        if ((error == ENOENT) && (cnp->cn_flags & MAKEENTRY) &&
            (cnp->cn_nameiop != CREATE) && negnamecache) {
                /* add a negative entry in the name cache */
+               nfs_node_lock_force(dnp);
                cache_enter(dvp, NULL, cnp);
                dnp->n_flag |= NNEGNCENTRIES;
+               nfs_node_unlock(dnp);
        }
 error_return:
-       if (!lockerror)
-               nfs_unlock(dnp);
+       NVATTR_CLEANUP(&nvattr);
+       if (!busyerror)
+               nfs_node_clear_busy(dnp);
        if (error && *vpp) {
                vnode_put(*vpp);
                *vpp = NULLVP;
@@ -1553,30 +2250,10 @@ error_return:
        return (error);
 }
 
-/*
- * NFS read call.
- * Just call nfs_bioread() to do the work.
- */
-static int
-nfs_vnop_read(
-       struct vnop_read_args /* {
-               struct vnodeop_desc *a_desc;
-               vnode_t a_vp;
-               struct uio *a_uio;
-               int a_ioflag;
-               vfs_context_t a_context;
-       } */ *ap)
-{
-       if (vnode_vtype(ap->a_vp) != VREG)
-               return (EPERM);
-       return (nfs_bioread(VTONFS(ap->a_vp), ap->a_uio, ap->a_ioflag, NULL, ap->a_context));
-}
-
-
 /*
  * NFS readlink call
  */
-static int
+int
 nfs_vnop_readlink(
        struct vnop_readlink_args /* {
                struct vnodeop_desc *a_desc;
@@ -1588,18 +2265,17 @@ nfs_vnop_readlink(
        vfs_context_t ctx = ap->a_context;
        nfsnode_t np = VTONFS(ap->a_vp);
        struct nfsmount *nmp;
-       int error = 0, lockerror, nfsvers, changed = 0, n;
+       int error = 0, nfsvers;
        uint32_t buflen;
-       struct uio *uio = ap->a_uio;
-       struct nfs_vattr nvattr;
+       uio_t uio = ap->a_uio;
        struct nfsbuf *bp = NULL;
 
        if (vnode_vtype(ap->a_vp) != VLNK)
                return (EPERM);
 
-       if (uio_uio_resid(uio) == 0)
+       if (uio_resid(uio) == 0)
                return (0);
-       if (uio->uio_offset < 0)
+       if (uio_offset(uio) < 0)
                return (EINVAL);
 
        nmp = VTONMP(ap->a_vp);
@@ -1607,34 +2283,20 @@ nfs_vnop_readlink(
                return (ENXIO);
        nfsvers = nmp->nm_vers;
 
-       error = lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
-       if (!error)
-               error = nfs_getattr(np, &nvattr, ctx, 1);
-       if (error) {
-               if (!lockerror)
-                       nfs_unlock(np);
+       /* nfs_getattr() will check changed and purge caches */
+       if ((error = nfs_getattr(np, NULL, ctx, NGA_CACHED))) {
                FSDBG(531, np, 0xd1e0001, 0, error);
                return (error);
        }
-       if (NFS_CHANGED(nfsvers, np, &nvattr)) {
-               /* link changed, so just ignore NB_CACHE */
-               changed = 1;
-               NFS_CHANGED_UPDATE(nfsvers, np, &nvattr);
-       }
-       nfs_unlock(np);
 
-       OSAddAtomic(1, (SInt32*)&nfsstats.biocache_readlinks);
+       OSAddAtomic64(1, &nfsstats.biocache_readlinks);
        error = nfs_buf_get(np, 0, NFS_MAXPATHLEN, vfs_context_thread(ctx), NBLK_READ, &bp);
        if (error) {
                FSDBG(531, np, 0xd1e0002, 0, error);
                return (error);
        }
-       if (changed)
-               CLR(bp->nb_flags, NB_CACHE);
        if (!ISSET(bp->nb_flags, NB_CACHE)) {
-               SET(bp->nb_flags, NB_READ);
-               CLR(bp->nb_flags, NB_DONE);
-               OSAddAtomic(1, (SInt32*)&nfsstats.readlink_bios);
+               OSAddAtomic64(1, &nfsstats.readlink_bios);
                buflen = bp->nb_bufsize;
                error = nmp->nm_funcs->nf_readlink_rpc(np, bp->nb_data, &buflen, ctx);
                if (error) {
@@ -1644,14 +2306,9 @@ nfs_vnop_readlink(
                        bp->nb_validoff = 0;
                        bp->nb_validend = buflen;
                }
-               nfs_buf_iodone(bp);
-       }
-       if (!error) {
-               // LP64todo - fix this!
-               n = min(uio_uio_resid(uio), bp->nb_validend);
-               if (n > 0)
-                       error = uiomove(bp->nb_data, n, uio);
        }
+       if (!error && (bp->nb_validend > 0))
+               error = uiomove(bp->nb_data, bp->nb_validend, uio);
        FSDBG(531, np, bp->nb_validend, 0, error);
        nfs_buf_release(bp, 1);
        return (error);
@@ -1680,9 +2337,8 @@ nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx)
        nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx,
-                       &nmrep, &xid, &status);
-       if ((lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       error = nfs_request(np, NULL, &nmreq, NFSPROC_READLINK, ctx, NULL, &nmrep, &xid, &status);
+       if ((lockerror = nfs_node_lock(np)))
                error = lockerror;
        if (nfsvers == NFS_VER3)
                nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
@@ -1705,7 +2361,7 @@ nfs3_readlink_rpc(nfsnode_t np, char *buf, uint32_t *buflenp, vfs_context_t ctx)
                *buflenp = len;
 nfsmout:
        if (!lockerror)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
        return (error);
@@ -1716,37 +2372,60 @@ nfsmout:
  * Ditto above
  */
 int
-nfs_read_rpc(nfsnode_t np, struct uio *uiop, vfs_context_t ctx)
+nfs_read_rpc(nfsnode_t np, uio_t uio, vfs_context_t ctx)
 {
        struct nfsmount *nmp;
        int error = 0, nfsvers, eof = 0;
-       size_t nmrsize, len, retlen, tsiz;
+       size_t nmrsize, len, retlen;
+       user_ssize_t tsiz;
        off_t txoffset;
        struct nfsreq rq, *req = &rq;
+       uint32_t stategenid = 0, restart = 0;
 
-       FSDBG_TOP(536, np, uiop->uio_offset, uio_uio_resid(uiop), 0);
+       FSDBG_TOP(536, np, uio_offset(uio), uio_resid(uio), 0);
        nmp = NFSTONMP(np);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
        nmrsize = nmp->nm_rsize;
 
-       // LP64todo - fix this
-       tsiz = uio_uio_resid(uiop);
-       if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && (nfsvers == NFS_VER2)) {
-               FSDBG_BOT(536, np, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
+       txoffset = uio_offset(uio);
+       tsiz = uio_resid(uio);
+       if ((nfsvers == NFS_VER2) && ((uint64_t)(txoffset + tsiz) > 0xffffffffULL)) {
+               FSDBG_BOT(536, np, uio_offset(uio), uio_resid(uio), EFBIG);
                return (EFBIG);
        }
 
-       txoffset = uiop->uio_offset;
-
        while (tsiz > 0) {
-               len = retlen = (tsiz > nmrsize) ? nmrsize : tsiz;
+               len = retlen = (tsiz > (user_ssize_t)nmrsize) ? nmrsize : (size_t)tsiz;
                FSDBG(536, np, txoffset, len, 0);
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+                       break;
+               }
+               if (nmp->nm_vers >= NFS_VER4)
+                       stategenid = nmp->nm_stategenid;
                error = nmp->nm_funcs->nf_read_rpc_async(np, txoffset, len,
                                vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
                if (!error)
-                       error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uiop, &retlen, &eof);
+                       error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req, uio, &retlen, &eof);
+               if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) &&
+                   (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */
+                       lck_mtx_lock(&nmp->nm_lock);
+                       if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
+                               NP(np, "nfs_read_rpc: error %d, initiating recovery", error);
+                               nfs_need_recover(nmp, error);
+                       }
+                       lck_mtx_unlock(&nmp->nm_lock);
+                       if (np->n_flag & NREVOKE) {
+                               error = EIO;
+                       } else {
+                               if (error == NFSERR_GRACE)
+                                       tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
+                               if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
+                                       continue;
+                       }
+               }
                if (error)
                        break;
                txoffset += retlen;
@@ -1758,7 +2437,7 @@ nfs_read_rpc(nfsnode_t np, struct uio *uiop, vfs_context_t ctx)
                        tsiz = 0;
        }
 
-       FSDBG_BOT(536, np, eof, uio_uio_resid(uiop), error);
+       FSDBG_BOT(536, np, eof, uio_resid(uio), error);
        return (error);
 }
 
@@ -1794,7 +2473,7 @@ nfs3_read_rpc_async(
        }
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, cb, reqp);
+       error = nfs_request_async(np, NULL, &nmreq, NFSPROC_READ, thd, cred, NULL, 0, cb, reqp);
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
        return (error);
@@ -1804,7 +2483,7 @@ int
 nfs3_read_rpc_async_finish(
        nfsnode_t np,
        struct nfsreq *req,
-       struct uio *uiop,
+       uio_t uio,
        size_t *lenp,
        int *eofp)
 {
@@ -1827,7 +2506,7 @@ nfs3_read_rpc_async_finish(
        if (error == EINPROGRESS) /* async request restarted */
                return (error);
 
-       if ((lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((lockerror = nfs_node_lock(np)))
                error = lockerror;
        if (nfsvers == NFS_VER3)
                nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
@@ -1837,15 +2516,15 @@ nfs3_read_rpc_async_finish(
                nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
                nfsm_chain_get_32(error, &nmrep, eof);
        } else {
-               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, NULL, &xid);
+               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
        }
        if (!lockerror)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        nfsm_chain_get_32(error, &nmrep, retlen);
        if ((nfsvers == NFS_VER2) && (retlen > *lenp))
                error = EBADRPC;
        nfsmout_if(error);
-       error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uiop);
+       error = nfsm_chain_get_uio(&nmrep, MIN(retlen, *lenp), uio);
        if (eofp) {
                if (nfsvers == NFS_VER3) {
                        if (!eof && !retlen)
@@ -1875,37 +2554,36 @@ nfs_vnop_write(
        } */ *ap)
 {
        vfs_context_t ctx = ap->a_context;
-       struct uio *uio = ap->a_uio;
+       uio_t uio = ap->a_uio;
        vnode_t vp = ap->a_vp;
        nfsnode_t np = VTONFS(vp);
        int ioflag = ap->a_ioflag;
        struct nfsbuf *bp;
-       struct nfs_vattr nvattr;
        struct nfsmount *nmp = VTONMP(vp);
        daddr64_t lbn;
        int biosize;
        int n, on, error = 0;
        off_t boff, start, end;
-       struct iovec_32 iov;
-       struct uio auio;
+       uio_t auio;
+       char auio_buf [ UIO_SIZEOF(1) ];
        thread_t thd;
        kauth_cred_t cred;
 
-       FSDBG_TOP(515, np, uio->uio_offset, uio_uio_resid(uio), ioflag);
+       FSDBG_TOP(515, np, uio_offset(uio), uio_resid(uio), ioflag);
 
        if (vnode_vtype(vp) != VREG) {
-               FSDBG_BOT(515, np, uio->uio_offset, uio_uio_resid(uio), EIO);
+               FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), EIO);
                return (EIO);
        }
 
        thd = vfs_context_thread(ctx);
        cred = vfs_context_ucred(ctx);
 
-       nfs_data_lock(np, NFS_NODE_LOCK_SHARED);
+       nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
 
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE))) {
+       if ((error = nfs_node_lock(np))) {
                nfs_data_unlock(np);
-               FSDBG_BOT(515, np, uio->uio_offset, uio_uio_resid(uio), error);
+               FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
                return (error);
        }
        np->n_wrbusy++;
@@ -1916,71 +2594,67 @@ nfs_vnop_write(
        }
        if (np->n_flag & NNEEDINVALIDATE) {
                np->n_flag &= ~NNEEDINVALIDATE;
-               nfs_unlock(np);
+               nfs_node_unlock(np);
                nfs_data_unlock(np);
                nfs_vinvalbuf(vp, V_SAVE|V_IGNORE_WRITEERR, ctx, 1);
-               nfs_data_lock(np, NFS_NODE_LOCK_SHARED);
-               if (error || ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE))))
-                       goto out;
+               nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
+       } else {
+               nfs_node_unlock(np);
        }
-       if (error) {
-               nfs_unlock(np);
+       if (error)
                goto out;
-       }
 
        biosize = nmp->nm_biosize;
 
        if (ioflag & (IO_APPEND | IO_SYNC)) {
+               nfs_node_lock_force(np);
                if (np->n_flag & NMODIFIED) {
                        NATTRINVALIDATE(np);
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        nfs_data_unlock(np);
                        error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
-                       nfs_data_lock(np, NFS_NODE_LOCK_SHARED);
-                       if (error || ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))) {
-                               FSDBG(515, np, uio->uio_offset, 0x10bad01, error);
+                       nfs_data_lock(np, NFS_DATA_LOCK_SHARED);
+                       if (error) {
+                               FSDBG(515, np, uio_offset(uio), 0x10bad01, error);
                                goto out;
                        }
+               } else {
+                       nfs_node_unlock(np);
                }
                if (ioflag & IO_APPEND) {
-                       NATTRINVALIDATE(np);
-                       nfs_unlock(np);
                        nfs_data_unlock(np);
-                       error = nfs_getattr(np, &nvattr, ctx, 0);
+                       /* nfs_getattr() will check changed and purge caches */
+                       error = nfs_getattr(np, NULL, ctx, NGA_UNCACHED);
                        /* we'll be extending the file, so take the data lock exclusive */
-                       nfs_data_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
-                       if (error || ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))) {
-                               FSDBG(515, np, uio->uio_offset, 0x10bad02, error);
+                       nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
+                       if (error) {
+                               FSDBG(515, np, uio_offset(uio), 0x10bad02, error);
                                goto out;
                        }
-                       uio->uio_offset = np->n_size;
+                       uio_setoffset(uio, np->n_size);
                }
        }
-       if (uio->uio_offset < 0) {
-               nfs_unlock(np);
+       if (uio_offset(uio) < 0) {
                error = EINVAL;
-               FSDBG_BOT(515, np, uio->uio_offset, 0xbad0ff, error);
+               FSDBG_BOT(515, np, uio_offset(uio), 0xbad0ff, error);
                goto out;
        }
-       if (uio_uio_resid(uio) == 0) {
-               nfs_unlock(np);
+       if (uio_resid(uio) == 0)
                goto out;
-       }
-
-       nfs_unlock(np);
 
-       if (((uio->uio_offset + uio_uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) {
+       if (((uio_offset(uio) + uio_resid(uio)) > (off_t)np->n_size) && !(ioflag & IO_APPEND)) {
                /* it looks like we'll be extending the file, so take the data lock exclusive */
                nfs_data_unlock(np);
-               nfs_data_lock(np, NFS_NODE_LOCK_EXCLUSIVE);
+               nfs_data_lock(np, NFS_DATA_LOCK_EXCLUSIVE);
        }
 
        do {
-               OSAddAtomic(1, (SInt32*)&nfsstats.biocache_writes);
-               lbn = uio->uio_offset / biosize;
-               on = uio->uio_offset % biosize;
-               // LP64todo - fix this
-               n = min((unsigned)(biosize - on), uio_uio_resid(uio));
+               OSAddAtomic64(1, &nfsstats.biocache_writes);
+               lbn = uio_offset(uio) / biosize;
+               on = uio_offset(uio) % biosize;
+               n = biosize - on;
+               if (uio_resid(uio) < n)
+                       n = uio_resid(uio);
 again:
                /*
                 * Get a cache block for writing.  The range to be written is
@@ -2017,7 +2691,7 @@ again:
                 */
                if (bp->nb_dirtyend > 0) {
                    if (on > bp->nb_dirtyend || (on + n) < bp->nb_dirtyoff || bp->nb_dirty) {
-                       FSDBG(515, np, uio->uio_offset, bp, 0xd15c001);
+                       FSDBG(515, np, uio_offset(uio), bp, 0xd15c001);
                        /* write/commit buffer "synchronously" */
                        /* (NB_STABLE indicates that data writes should be FILESYNC) */
                        CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL));
@@ -2036,7 +2710,7 @@ again:
                    pagemask = ((1 << (lastpg+1)) - 1) & ~((1 << firstpg) - 1);
                    /* check if there are dirty pages outside the write range */
                    if (bp->nb_dirty & ~pagemask) {
-                       FSDBG(515, np, uio->uio_offset, bp, 0xd15c002);
+                       FSDBG(515, np, uio_offset(uio), bp, 0xd15c002);
                        /* write/commit buffer "synchronously" */
                        /* (NB_STABLE indicates that data writes should be FILESYNC) */
                        CLR(bp->nb_flags, (NB_DONE | NB_ERROR | NB_INVAL));
@@ -2049,7 +2723,7 @@ again:
                    /* if the first or last pages are already dirty */
                    /* make sure that the dirty range encompasses those pages */
                    if (NBPGDIRTY(bp,firstpg) || NBPGDIRTY(bp,lastpg)) {
-                       FSDBG(515, np, uio->uio_offset, bp, 0xd15c003);
+                       FSDBG(515, np, uio_offset(uio), bp, 0xd15c003);
                        bp->nb_dirtyoff = min(on, firstpg * PAGE_SIZE);
                        if (NBPGDIRTY(bp,lastpg)) {
                            bp->nb_dirtyend = (lastpg+1) * PAGE_SIZE;
@@ -2070,13 +2744,13 @@ again:
                 * If there was a partial buf at the old eof, validate
                 * and zero the new bytes.
                 */
-               if ((uio->uio_offset + n) > (off_t)np->n_size) {
+               if ((uio_offset(uio) + n) > (off_t)np->n_size) {
                        struct nfsbuf *eofbp = NULL;
                        daddr64_t eofbn = np->n_size / biosize;
                        int eofoff = np->n_size % biosize;
-                       int neweofoff = (uio->uio_offset + n) % biosize;
+                       int neweofoff = (uio_offset(uio) + n) % biosize;
 
-                       FSDBG(515, 0xb1ffa000, uio->uio_offset + n, eofoff, neweofoff);
+                       FSDBG(515, 0xb1ffa000, uio_offset(uio) + n, eofoff, neweofoff);
 
                        if (eofoff && (eofbn < lbn) &&
                            ((error = nfs_buf_get(np, eofbn, biosize, thd, NBLK_WRITE|NBLK_ONLYVALID, &eofbp))))
@@ -2111,11 +2785,11 @@ again:
                                        }
                                }
                        }
-                       np->n_size = uio->uio_offset + n;
-                       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+                       np->n_size = uio_offset(uio) + n;
+                       nfs_node_lock_force(np);
                        CLR(np->n_flag, NUPDATESIZE);
                        np->n_flag |= NMODIFIED;
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        FSDBG(516, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
                        ubc_setsize(vp, (off_t)np->n_size); /* XXX errors */
                        if (eofbp) {
@@ -2176,12 +2850,14 @@ again:
                 *
                 * Notes:
                 * We don't want to read anything we're just going to write over.
+                * We don't want to read anything we're just going drop when the
+                *   I/O is complete (i.e. don't do reads for NOCACHE requests).
                 * We don't want to issue multiple I/Os if we don't have to
                 *   (because they're synchronous rpcs).
                 * We don't want to read anything we already have modified in the
                 *   page cache.
                 */
-               if (!ISSET(bp->nb_flags, NB_NOCACHE) && !ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) {
+               if (!ISSET(bp->nb_flags, NB_CACHE) && (n < biosize)) {
                        int firstpg, lastpg, dirtypg;
                        int firstpgoff, lastpgoff;
                        start = end = -1;
@@ -2200,6 +2876,22 @@ again:
                                        start = (lastpg * PAGE_SIZE) + lastpgoff;
                                end = (lastpg + 1) * PAGE_SIZE;
                        }
+                       if (ISSET(bp->nb_flags, NB_NOCACHE)) {
+                               /*
+                                * For nocache writes, if there is any partial page at the
+                                * start or end of the write range, then we do the write
+                                * synchronously to make sure that we can drop the data
+                                * from the cache as soon as the WRITE finishes.  Normally,
+                                * we would do an unstable write and not drop the data until
+                                * it was committed.  But doing that here would risk allowing
+                                * invalid data to be read from the cache between the WRITE
+                                * and the COMMIT.
+                                * (NB_STABLE indicates that data writes should be FILESYNC)
+                                */
+                               if (end > start)
+                                       SET(bp->nb_flags, NB_STABLE);
+                               goto skipread;
+                       }
                        if (end > start) {
                                /* need to read the data in range: start...end-1 */
 
@@ -2222,29 +2914,23 @@ again:
                                NFS_BUF_MAP(bp);
                                /* setup uio for read(s) */
                                boff = NBOFF(bp);
-                               auio.uio_iovs.iov32p = &iov;
-                               auio.uio_iovcnt = 1;
-#if 1   /* LP64todo - can't use new segment flags until the drivers are ready */
-                               auio.uio_segflg = UIO_SYSSPACE;
-#else
-                               auio.uio_segflg = UIO_SYSSPACE32;
-#endif
-                               auio.uio_rw = UIO_READ;
+                               auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
+                                       &auio_buf, sizeof(auio_buf));
 
                                if (dirtypg <= (end-1)/PAGE_SIZE) {
                                        /* there's a dirty page in the way, so just do two reads */
                                        /* we'll read the preceding data here */
-                                       auio.uio_offset = boff + start;
-                                       iov.iov_len = on - start;
-                                       uio_uio_resid_set(&auio, iov.iov_len);
-                                       iov.iov_base = (uintptr_t) bp->nb_data + start;
-                                       error = nfs_read_rpc(np, &auio, ctx);
-                                       if (error) /* couldn't read the data, so treat buffer as NOCACHE */
+                                       uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ);
+                                       uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), on - start);
+                                       error = nfs_read_rpc(np, auio, ctx);
+                                       if (error) {
+                                               /* couldn't read the data, so treat buffer as synchronous NOCACHE */
                                                SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
-                                       if (uio_uio_resid(&auio) > 0) {
-                                               FSDBG(516, bp, (caddr_t)iov.iov_base - bp->nb_data, uio_uio_resid(&auio), 0xd00dee01);
-                                               // LP64todo - fix this
-                                               bzero((caddr_t)iov.iov_base, uio_uio_resid(&auio));
+                                               goto skipread;
+                                       }
+                                       if (uio_resid(auio) > 0) {
+                                               FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee01);
+                                               bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio));
                                        }
                                        if (!error) {
                                                /* update validoff/validend if necessary */
@@ -2283,19 +2969,19 @@ again:
                                        FSDBG(516, bp, start, end - start, 0xd00dee00);
                                        bzero(bp->nb_data + start, end - start);
                                        error = 0;
-                               } else if (!ISSET(bp->nb_flags, NB_NOCACHE)) {
+                               } else {
                                        /* now we'll read the (rest of the) data */
-                                       auio.uio_offset = boff + start;
-                                       iov.iov_len = end - start;
-                                       uio_uio_resid_set(&auio, iov.iov_len);
-                                       iov.iov_base = (uintptr_t) (bp->nb_data + start);
-                                       error = nfs_read_rpc(np, &auio, ctx);
-                                       if (error) /* couldn't read the data, so treat buffer as NOCACHE */
+                                       uio_reset(auio, boff + start, UIO_SYSSPACE, UIO_READ);
+                                       uio_addiov(auio, CAST_USER_ADDR_T(bp->nb_data + start), end - start);
+                                       error = nfs_read_rpc(np, auio, ctx);
+                                       if (error) {
+                                               /* couldn't read the data, so treat buffer as synchronous NOCACHE */
                                                SET(bp->nb_flags, (NB_NOCACHE|NB_STABLE));
-                                       if (uio_uio_resid(&auio) > 0) {
-                                               FSDBG(516, bp, (caddr_t)iov.iov_base - bp->nb_data, uio_uio_resid(&auio), 0xd00dee02);
-                                               // LP64todo - fix this
-                                               bzero((caddr_t)iov.iov_base, uio_uio_resid(&auio));
+                                               goto skipread;
+                                       }
+                                       if (uio_resid(auio) > 0) {
+                                               FSDBG(516, bp, (caddr_t)uio_curriovbase(auio) - bp->nb_data, uio_resid(auio), 0xd00dee02);
+                                               bzero(CAST_DOWN(caddr_t, uio_curriovbase(auio)), uio_resid(auio));
                                        }
                                }
                                if (!error) {
@@ -2307,15 +2993,16 @@ again:
                                        if ((off_t)np->n_size > boff + bp->nb_validend)
                                                bp->nb_validend = min(np->n_size - (boff + start), biosize);
                                        /* validate any pages before the write offset's page */
-                                       for (; start < trunc_page_32(on); start+=PAGE_SIZE)
+                                       for (; start < (off_t)trunc_page_32(on); start+=PAGE_SIZE)
                                                NBPGVALID_SET(bp, start/PAGE_SIZE);
                                        /* validate any pages after the range of pages being written to */
-                                       for (; (end - 1) > round_page_32(on+n-1); end-=PAGE_SIZE)
+                                       for (; (end - 1) > (off_t)round_page_32(on+n-1); end-=PAGE_SIZE)
                                                NBPGVALID_SET(bp, (end-1)/PAGE_SIZE);
                                }
                                /* Note: pages being written to will be validated when written */
                        }
                }
+skipread:
 
                if (ISSET(bp->nb_flags, NB_ERROR)) {
                        error = bp->nb_error;
@@ -2323,9 +3010,9 @@ again:
                        goto out;
                }
 
-               nfs_lock(np, NFS_NODE_LOCK_FORCE);
+               nfs_node_lock_force(np);
                np->n_flag |= NMODIFIED;
-               nfs_unlock(np);
+               nfs_node_unlock(np);
 
                NFS_BUF_MAP(bp);
                error = uiomove((char *)bp->nb_data + on, n, uio);
@@ -2372,20 +3059,21 @@ again:
                 * again and not just committed.
                 */
                if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
-                       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+                       nfs_node_lock_force(np);
                        if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
                                np->n_needcommitcnt--;
                                CHECK_NEEDCOMMITCNT(np);
                        }
                        CLR(bp->nb_flags, NB_NEEDCOMMIT);
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                }
 
                if (ioflag & IO_SYNC) {
                        error = nfs_buf_write(bp);
                        if (error)
                                goto out;
-               } else if (((n + on) == biosize) || (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) {
+               } else if (((n + on) == biosize) || (ioflag & IO_APPEND) ||
+                          (ioflag & IO_NOCACHE) || ISSET(bp->nb_flags, NB_NOCACHE)) {
                        SET(bp->nb_flags, NB_ASYNC);
                        error = nfs_buf_write(bp);
                        if (error)
@@ -2395,21 +3083,21 @@ again:
                        if (!ISSET(bp->nb_flags, NB_DELWRI)) {
                                proc_t p = vfs_context_proc(ctx);
                                if (p && p->p_stats)
-                                       OSIncrementAtomic(&p->p_stats->p_ru.ru_oublock);
+                                       OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);
                        }
                        nfs_buf_write_delayed(bp);
                }
                if (np->n_needcommitcnt >= NFS_A_LOT_OF_NEEDCOMMITS)
                        nfs_flushcommits(np, 1);
 
-       } while (uio_uio_resid(uio) > 0 && n > 0);
+       } while (uio_resid(uio) > 0 && n > 0);
 
 out:
-       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+       nfs_node_lock_force(np);
        np->n_wrbusy--;
-       nfs_unlock(np);
+       nfs_node_unlock(np);
        nfs_data_unlock(np);
-       FSDBG_BOT(515, np, uio->uio_offset, uio_uio_resid(uio), error);
+       FSDBG_BOT(515, np, uio_offset(uio), uio_resid(uio), error);
        return (error);
 }
 
@@ -2420,61 +3108,90 @@ out:
 int
 nfs_write_rpc(
        nfsnode_t np,
-       struct uio *uiop,
+       uio_t uio,
        vfs_context_t ctx,
        int *iomodep,
        uint64_t *wverfp)
 {
-       return nfs_write_rpc2(np, uiop, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp);
+       return nfs_write_rpc2(np, uio, vfs_context_thread(ctx), vfs_context_ucred(ctx), iomodep, wverfp);
 }
 
 int
 nfs_write_rpc2(
        nfsnode_t np,
-       struct uio *uiop,
+       uio_t uio,
        thread_t thd,
        kauth_cred_t cred,
        int *iomodep,
        uint64_t *wverfp)
 {
        struct nfsmount *nmp;
-       int error = 0, nfsvers, restart;
-       int backup, wverfset, commit, committed;
+       int error = 0, nfsvers;
+       int wverfset, commit, committed;
        uint64_t wverf = 0, wverf2;
        size_t nmwsize, totalsize, tsiz, len, rlen;
        struct nfsreq rq, *req = &rq;
+       uint32_t stategenid = 0, vrestart = 0, restart = 0;
+       uio_t uio_save = NULL;
 
 #if DIAGNOSTIC
        /* XXX limitation based on need to back up uio on short write */
-       if (uiop->uio_iovcnt != 1)
+       if (uio_iovcnt(uio) != 1)
                panic("nfs3_write_rpc: iovcnt > 1");
 #endif
-       FSDBG_TOP(537, np, uiop->uio_offset, uio_uio_resid(uiop), *iomodep);
+       FSDBG_TOP(537, np, uio_offset(uio), uio_resid(uio), *iomodep);
        nmp = NFSTONMP(np);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
        nmwsize = nmp->nm_wsize;
 
-       restart = wverfset = 0;
+       wverfset = 0;
        committed = NFS_WRITE_FILESYNC;
 
-       // LP64todo - fix this
-       totalsize = tsiz = uio_uio_resid(uiop);
-       if (((u_int64_t)uiop->uio_offset + (unsigned int)tsiz > 0xffffffff) && (nfsvers == NFS_VER2)) {
-               FSDBG_BOT(537, np, uiop->uio_offset, uio_uio_resid(uiop), EFBIG);
+       totalsize = tsiz = uio_resid(uio);
+       if ((nfsvers == NFS_VER2) && ((uint64_t)(uio_offset(uio) + tsiz) > 0xffffffffULL)) {
+               FSDBG_BOT(537, np, uio_offset(uio), uio_resid(uio), EFBIG);
                return (EFBIG);
        }
 
+       uio_save = uio_duplicate(uio);
+       if (uio_save == NULL) {
+               return (EIO);
+       }
+
        while (tsiz > 0) {
                len = (tsiz > nmwsize) ? nmwsize : tsiz;
-               FSDBG(537, np, uiop->uio_offset, len, 0);
-               error = nmp->nm_funcs->nf_write_rpc_async(np, uiop, len, thd, cred, *iomodep, NULL, &req);
+               FSDBG(537, np, uio_offset(uio), len, 0);
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+                       break;
+               }
+               if (nmp->nm_vers >= NFS_VER4)
+                       stategenid = nmp->nm_stategenid;
+               error = nmp->nm_funcs->nf_write_rpc_async(np, uio, len, thd, cred, *iomodep, NULL, &req);
                if (!error)
                        error = nmp->nm_funcs->nf_write_rpc_async_finish(np, req, &commit, &rlen, &wverf2);
                nmp = NFSTONMP(np);
                if (!nmp)
                        error = ENXIO;
+               if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error) &&
+                   (++restart <= nfs_mount_state_max_restarts(nmp))) { /* guard against no progress */
+                       lck_mtx_lock(&nmp->nm_lock);
+                       if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
+                               NP(np, "nfs_write_rpc: error %d, initiating recovery", error);
+                               nfs_need_recover(nmp, error);
+                       }
+                       lck_mtx_unlock(&nmp->nm_lock);
+                       if (np->n_flag & NREVOKE) {
+                               error = EIO;
+                       } else {
+                               if (error == NFSERR_GRACE)
+                                       tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
+                               if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
+                                       continue;
+                       }
+               }
                if (error)
                        break;
                if (nfsvers == NFS_VER2) {
@@ -2484,11 +3201,9 @@ nfs_write_rpc2(
 
                /* check for a short write */
                if (rlen < len) {
-                       backup = len - rlen;
-                       uio_iov_base_add(uiop, -backup);
-                       uio_iov_len_add(uiop, backup);
-                       uiop->uio_offset -= backup;
-                       uio_uio_resid_add(uiop, backup);
+                       /* Reset the uio to reflect the actual transfer */
+                       *uio = *uio_save;
+                       uio_update(uio, totalsize - (tsiz - rlen));
                        len = rlen;
                }
 
@@ -2504,34 +3219,32 @@ nfs_write_rpc2(
                        wverfset = 1;
                } else if (wverf != wverf2) {
                        /* verifier changed, so we need to restart all the writes */
-                       if (++restart > 10) {
+                       if (++vrestart > 100) {
                                /* give up after too many restarts */
                                error = EIO;
                                break;
                        }
-                       backup = totalsize - tsiz;
-                       uio_iov_base_add(uiop, -backup);
-                       uio_iov_len_add(uiop, backup);
-                       uiop->uio_offset -= backup;
-                       uio_uio_resid_add(uiop, backup);
+                       *uio = *uio_save;       // Reset the uio back to the start
                        committed = NFS_WRITE_FILESYNC;
                        wverfset = 0;
                        tsiz = totalsize;
                }
        }
+       if (uio_save)
+               uio_free(uio_save);
        if (wverfset && wverfp)
                *wverfp = wverf;
        *iomodep = committed;
        if (error)
-               uio_uio_resid_set(uiop, tsiz);
-       FSDBG_BOT(537, np, committed, uio_uio_resid(uiop), error);
+               uio_setresid(uio, tsiz);
+       FSDBG_BOT(537, np, committed, uio_resid(uio), error);
        return (error);
 }
 
 int
 nfs3_write_rpc_async(
        nfsnode_t np,
-       struct uio *uiop,
+       uio_t uio,
        size_t len,
        thread_t thd,
        kauth_cred_t cred,
@@ -2540,8 +3253,8 @@ nfs3_write_rpc_async(
        struct nfsreq **reqp)
 {
        struct nfsmount *nmp;
+       mount_t mp;
        int error = 0, nfsvers;
-       off_t offset;
        struct nfsm_chain nmreq;
 
        nmp = NFSTONMP(np);
@@ -2549,27 +3262,30 @@ nfs3_write_rpc_async(
                return (ENXIO);
        nfsvers = nmp->nm_vers;
 
-       offset = uiop->uio_offset;
+       /* for async mounts, don't bother sending sync write requests */
+       if ((iomode != NFS_WRITE_UNSTABLE) && nfs_allow_async &&
+           ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC))
+               iomode = NFS_WRITE_UNSTABLE;
 
        nfsm_chain_null(&nmreq);
        nfsm_chain_build_alloc_init(error, &nmreq,
                NFSX_FH(nfsvers) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
        if (nfsvers == NFS_VER3) {
-               nfsm_chain_add_64(error, &nmreq, offset);
+               nfsm_chain_add_64(error, &nmreq, uio_offset(uio));
                nfsm_chain_add_32(error, &nmreq, len);
                nfsm_chain_add_32(error, &nmreq, iomode);
        } else {
                nfsm_chain_add_32(error, &nmreq, 0);
-               nfsm_chain_add_32(error, &nmreq, offset);
+               nfsm_chain_add_32(error, &nmreq, uio_offset(uio));
                nfsm_chain_add_32(error, &nmreq, 0);
        }
        nfsm_chain_add_32(error, &nmreq, len);
        nfsmout_if(error);
-       error = nfsm_chain_add_uio(&nmreq, uiop, len);
+       error = nfsm_chain_add_uio(&nmreq, uio, len);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, cb, reqp);
+       error = nfs_request_async(np, NULL, &nmreq, NFSPROC_WRITE, thd, cred, NULL, 0, cb, reqp);
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
        return (error);
@@ -2605,7 +3321,7 @@ nfs3_write_rpc_async_finish(
        nmp = NFSTONMP(np);
        if (!nmp)
                error = ENXIO;
-       if (!error && (lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if (!error && (lockerror = nfs_node_lock(np)))
                error = lockerror;
        if (nfsvers == NFS_VER3) {
                struct timespec premtime = { 0, 0 };
@@ -2635,14 +3351,14 @@ nfs3_write_rpc_async_finish(
        } else {
                if (!error)
                        error = status;
-               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, NULL, &xid);
+               nfsm_chain_loadattr(error, &nmrep, np, nfsvers, &xid);
                nfsmout_if(error);
        }
        if (updatemtime)
                NFS_CHANGED_UPDATE(nfsvers, np, &np->n_vattr);
 nfsmout:
        if (!lockerror)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        nfsm_chain_cleanup(&nmrep);
        if ((committed != NFS_WRITE_FILESYNC) && nfs_allow_async &&
            ((mp = NFSTOMP(np))) && (vfs_flags(mp) & MNT_ASYNC))
@@ -2657,7 +3373,7 @@ nfsmout:
  * For NFS v2 this is a kludge. Use a create RPC but with the IFMT bits of the
  * mode set to specify the file type and the size field for rdev.
  */
-static int
+int
 nfs3_vnop_mknod(
        struct vnop_mknod_args /* {
                struct vnodeop_desc *a_desc;
@@ -2677,14 +3393,15 @@ nfs3_vnop_mknod(
        nfsnode_t np = NULL;
        struct nfsmount *nmp;
        nfsnode_t dnp = VTONFS(dvp);
-       struct nfs_vattr nvattr, dnvattr;
+       struct nfs_vattr nvattr;
        fhandle_t fh;
-       int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
+       int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
        struct timespec premtime = { 0, 0 };
-       u_long rdev;
-       u_int64_t xid, dxid;
+       u_int32_t rdev;
+       u_int64_t xid = 0, dxid;
        int nfsvers, gotuid, gotgid;
        struct nfsm_chain nmreq, nmrep;
+       struct nfsreq rq, *req = &rq;
 
        nmp = VTONMP(dvp);
        if (!nmp)
@@ -2705,6 +3422,8 @@ nfs3_vnop_mknod(
        if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
                return (ENAMETOOLONG);
 
+       nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
+
        VATTR_SET_SUPPORTED(vap, va_mode);
        VATTR_SET_SUPPORTED(vap, va_uid);
        VATTR_SET_SUPPORTED(vap, va_gid);
@@ -2721,7 +3440,7 @@ nfs3_vnop_mknod(
                NFSX_FH(nfsvers) + 4 * NFSX_UNSIGNED +
                nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        if (nfsvers == NFS_VER3) {
                nfsm_chain_add_32(error, &nmreq, vtonfs_type(vap->va_type, nfsvers));
                nfsm_chain_add_v3sattr(error, &nmreq, vap);
@@ -2733,13 +3452,17 @@ nfs3_vnop_mknod(
                nfsm_chain_add_v2sattr(error, &nmreq, vap, rdev);
        }
        nfsm_chain_build_done(error, &nmreq);
-       nfsmout_if(error);
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
+       if (!error)
+               error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
        nfsmout_if(error);
 
-       error = nfs_request(dnp, NULL, &nmreq, NFSPROC_MKNOD, ctx, &nmrep, &xid, &status);
+       error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKNOD,
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
+       if (!error)
+               error = nfs_request_async_finish(req, &nmrep, &xid, &status);
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        /* XXX no EEXIST kludge here? */
        dxid = xid;
        if (!error && !status) {
@@ -2762,28 +3485,22 @@ nfsmout:
                /* if directory hadn't changed, update namecache mtime */
                if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
                        NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
-               if (!wccpostattr)
-                       NATTRINVALIDATE(dnp);
-               if (!nfs_getattr(dnp, &dnvattr, ctx, 1)) {
-                       if (NFS_CHANGED_NC(nfsvers, dnp, &dnvattr)) {
-                               dnp->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(dvp);
-                               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnvattr);
-                       }
-               }
+               nfs_node_unlock(dnp);
+               /* nfs_getattr() will check changed and purge caches */
+               nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
        }
 
        if (!error && fh.fh_len)
-               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, NG_MAKEENTRY, &np);
+               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
        if (!error && !np)
                error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
        if (!error && np)
                newvp = NFSTOV(np);
-       if (!lockerror)
-               nfs_unlock(dnp);
+       if (!busyerror)
+               nfs_node_clear_busy(dnp);
 
        if (!error && (gotuid || gotgid) &&
-           (!newvp || nfs_getattrcache(np, &nvattr, 1) ||
+           (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
             (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
             (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
                /* clear ID bits if server didn't use them (or we can't tell) */
@@ -2792,21 +3509,21 @@ nfsmout:
        }
        if (error) {
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                }
        } else {
                *vpp = newvp;
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        }
        return (error);
 }
 
-static u_long create_verf;
+static uint32_t create_verf;
 /*
  * NFS file create call
  */
-static int
+int
 nfs3_vnop_create(
        struct vnop_create_args /* {
                struct vnodeop_desc *a_desc;
@@ -2821,19 +3538,19 @@ nfs3_vnop_create(
        vnode_t dvp = ap->a_dvp;
        struct vnode_attr *vap = ap->a_vap;
        struct componentname *cnp = ap->a_cnp;
-       struct nfs_vattr nvattr, dnvattr;
+       struct nfs_vattr nvattr;
        fhandle_t fh;
        nfsnode_t np = NULL;
        struct nfsmount *nmp;
        nfsnode_t dnp = VTONFS(dvp);
        vnode_t newvp = NULL;
-       int error = 0, lockerror = ENOENT, status, wccpostattr = 0, fmode = 0;
+       int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0, fmode = 0;
        struct timespec premtime = { 0, 0 };
        int nfsvers, gotuid, gotgid;
        u_int64_t xid, dxid;
        uint32_t val;
        struct nfsm_chain nmreq, nmrep;
-       struct nfsreq *req;
+       struct nfsreq rq, *req = &rq;
        struct nfs_dulookup dul;
 
        nmp = VTONMP(dvp);
@@ -2844,6 +3561,8 @@ nfs3_vnop_create(
        if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
                return (ENAMETOOLONG);
 
+       nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
+
        VATTR_SET_SUPPORTED(vap, va_mode);
        VATTR_SET_SUPPORTED(vap, va_uid);
        VATTR_SET_SUPPORTED(vap, va_gid);
@@ -2853,12 +3572,15 @@ nfs3_vnop_create(
        gotuid = VATTR_IS_ACTIVE(vap, va_uid);
        gotgid = VATTR_IS_ACTIVE(vap, va_gid);
 
-       if (vap->va_vaflags & VA_EXCLUSIVE)
+       if (vap->va_vaflags & VA_EXCLUSIVE) {
                fmode |= O_EXCL;
+               if (!VATTR_IS_ACTIVE(vap, va_access_time) || !VATTR_IS_ACTIVE(vap, va_modify_time))
+                       vap->va_vaflags |= VA_UTIMES_NULL;
+       }
 
 again:
-       req = NULL;
-       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen);
+       error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
+       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
 
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
@@ -2867,14 +3589,16 @@ again:
                NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED +
                nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        if (nfsvers == NFS_VER3) {
                if (fmode & O_EXCL) {
                        nfsm_chain_add_32(error, &nmreq, NFS_CREATE_EXCLUSIVE);
+                       lck_rw_lock_shared(in_ifaddr_rwlock);
                        if (!TAILQ_EMPTY(&in_ifaddrhead))
                                val = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
                        else
                                val = create_verf;
+                       lck_rw_done(in_ifaddr_rwlock);
                        nfsm_chain_add_32(error, &nmreq, val);
                        ++create_verf;
                        nfsm_chain_add_32(error, &nmreq, create_verf);
@@ -2887,17 +3611,16 @@ again:
        }
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
-       nfsmout_if(error);
 
        error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_CREATE,
-                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
        if (!error) {
                nfs_dulookup_start(&dul, dnp, ctx);
                error = nfs_request_async_finish(req, &nmrep, &xid, &status);
        }
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        dxid = xid;
        if (!error && !status) {
                if (dnp->n_flag & NNEGNCENTRIES) {
@@ -2919,27 +3642,21 @@ nfsmout:
                /* if directory hadn't changed, update namecache mtime */
                if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
                        NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
-               if (!wccpostattr)
-                       NATTRINVALIDATE(dnp);
-               if (!nfs_getattr(dnp, &dnvattr, ctx, 1)) {
-                       if (NFS_CHANGED_NC(nfsvers, dnp, &dnvattr)) {
-                               dnp->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(dvp);
-                               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnvattr);
-                       }
-               }
+               nfs_node_unlock(dnp);
+               /* nfs_getattr() will check changed and purge caches */
+               nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
        }
 
        if (!error && fh.fh_len)
-               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, NG_MAKEENTRY, &np);
+               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
        if (!error && !np)
                error = nfs_lookitup(dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx, &np);
        if (!error && np)
                newvp = NFSTOV(np);
 
        nfs_dulookup_finish(&dul, dnp, ctx);
-       if (!lockerror)
-               nfs_unlock(dnp);
+       if (!busyerror)
+               nfs_node_clear_busy(dnp);
 
        if (error) {
                if ((nfsvers == NFS_VER3) && (fmode & O_EXCL) && (error == NFSERR_NOTSUPP)) {
@@ -2947,27 +3664,28 @@ nfsmout:
                        goto again;
                }
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                }
        } else if ((nfsvers == NFS_VER3) && (fmode & O_EXCL)) {
-               error = nfs3_setattr_rpc(np, vap, ctx, 1);
+               nfs_node_unlock(np);
+               error = nfs3_setattr_rpc(np, vap, ctx);
                if (error && (gotuid || gotgid)) {
                        /* it's possible the server didn't like our attempt to set IDs. */
                        /* so, let's try it again without those */
                        VATTR_CLEAR_ACTIVE(vap, va_uid);
                        VATTR_CLEAR_ACTIVE(vap, va_gid);
-                       error = nfs3_setattr_rpc(np, vap, ctx, 1);
+                       error = nfs3_setattr_rpc(np, vap, ctx);
                }
-               if (error) {
-                       nfs_unlock(np);
+               if (error)
                        vnode_put(newvp);
-               }
+               else
+                       nfs_node_lock_force(np);
        }
        if (!error)
                *ap->a_vpp = newvp;
        if (!error && (gotuid || gotgid) &&
-           (!newvp || nfs_getattrcache(np, &nvattr, 1) ||
+           (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
             (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
             (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
                /* clear ID bits if server didn't use them (or we can't tell) */
@@ -2975,7 +3693,7 @@ nfsmout:
                VATTR_CLEAR_SUPPORTED(vap, va_gid);
        }
        if (!error)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        return (error);
 }
 
@@ -2990,7 +3708,7 @@ nfsmout:
  *     else
  *       do the remove RPC
  */
-static int
+int
 nfs_vnop_remove(
        struct vnop_remove_args /* {
                struct vnodeop_desc *a_desc;
@@ -3007,7 +3725,7 @@ nfs_vnop_remove(
        struct componentname *cnp = ap->a_cnp;
        nfsnode_t dnp = VTONFS(dvp);
        nfsnode_t np = VTONFS(vp);
-       int error = 0, nfsvers, inuse, gotattr = 0, flushed = 0, setsize = 0;
+       int error = 0, nfsvers, namedattrs, inuse, gotattr = 0, flushed = 0, setsize = 0;
        struct nfs_vattr nvattr;
        struct nfsmount *nmp;
        struct nfs_dulookup dul;
@@ -3018,9 +3736,10 @@ nfs_vnop_remove(
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
+       namedattrs = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR);
 
 again_relock:
-       error = nfs_lock2(dnp, np, NFS_NODE_LOCK_EXCLUSIVE);
+       error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx));
        if (error)
                return (error);
 
@@ -3033,7 +3752,8 @@ again_relock:
        np->n_hflag |= NHLOCKED;
        lck_mtx_unlock(nfs_node_hash_mutex);
 
-       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen);
+       if (!namedattrs)
+               nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
 again:
        inuse = vnode_isinuse(vp, 0);
        if ((ap->a_flags & VNODE_REMOVE_NODELETEBUSY) && inuse) {
@@ -3042,7 +3762,7 @@ again:
                goto out;
        }
        if (inuse && !gotattr) {
-               if (nfs_getattr(np, &nvattr, ctx, 1))
+               if (nfs_getattr(np, &nvattr, ctx, NGA_CACHED))
                        nvattr.nva_nlink = 1;
                gotattr = 1;
                goto again;
@@ -3058,27 +3778,33 @@ again:
                                wakeup(np);
                        }
                        lck_mtx_unlock(nfs_node_hash_mutex);
-                       nfs_unlock2(dnp, np);
+                       nfs_node_clear_busy2(dnp, np);
                        error = nfs_vinvalbuf(vp, V_SAVE, ctx, 1);
                        FSDBG(260, np, np->n_size, np->n_vattr.nva_size, 0xf00d0011);
                        flushed = 1;
                        if (error == EINTR) {
-                               nfs_lock(np, NFS_NODE_LOCK_FORCE);
+                               nfs_node_lock_force(np);
                                NATTRINVALIDATE(np);
-                               nfs_unlock(np);
+                               nfs_node_unlock(np);
                                return (error);
                        }
+                       if (!namedattrs)
+                               nfs_dulookup_finish(&dul, dnp, ctx);
                        goto again_relock;
                }
 
+               if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK))
+                       nfs4_delegation_return(np, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
+
                /*
                 * Purge the name cache so that the chance of a lookup for
                 * the name succeeding while the remove is in progress is
                 * minimized.
                 */
-               cache_purge(vp);
+               nfs_name_cache_purge(dnp, np, cnp, ctx);
 
-               nfs_dulookup_start(&dul, dnp, ctx);
+               if (!namedattrs)
+                       nfs_dulookup_start(&dul, dnp, ctx);
 
                /* Do the rpc */
                error = nmp->nm_funcs->nf_remove_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
@@ -3109,30 +3835,36 @@ again:
                        lck_mtx_unlock(nfs_node_hash_mutex);
                        /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
                        /* clear all flags other than these */
+                       nfs_node_lock_force(np);
                        np->n_flag &= (NMODIFIED);
-                       vnode_recycle(vp);
                        NATTRINVALIDATE(np);
+                       nfs_node_unlock(np);
+                       vnode_recycle(vp);
                        setsize = 1;
                } else {
+                       nfs_node_lock_force(np);
                        NATTRINVALIDATE(np);
+                       nfs_node_unlock(np);
                }
        } else if (!np->n_sillyrename) {
-               nfs_dulookup_start(&dul, dnp, ctx);
+               if (!namedattrs)
+                       nfs_dulookup_start(&dul, dnp, ctx);
                error = nfs_sillyrename(dnp, np, cnp, ctx);
+               nfs_node_lock_force(np);
                NATTRINVALIDATE(np);
+               nfs_node_unlock(np);
        } else {
+               nfs_node_lock_force(np);
                NATTRINVALIDATE(np);
-               nfs_dulookup_start(&dul, dnp, ctx);
+               nfs_node_unlock(np);
+               if (!namedattrs)
+                       nfs_dulookup_start(&dul, dnp, ctx);
        }
 
-       if (!nfs_getattr(dnp, &nvattr, ctx, 1)) {
-               if (NFS_CHANGED_NC(nfsvers, dnp, &nvattr)) {
-                       dnp->n_flag &= ~NNEGNCENTRIES;
-                       cache_purge(dvp);
-                       NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &nvattr);
-               }
-       }
-       nfs_dulookup_finish(&dul, dnp, ctx);
+       /* nfs_getattr() will check changed and purge caches */
+       nfs_getattr(dnp, NULL, ctx, NGA_CACHED);
+       if (!namedattrs)
+               nfs_dulookup_finish(&dul, dnp, ctx);
 out:
        /* unlock the node */
        lck_mtx_lock(nfs_node_hash_mutex);
@@ -3142,7 +3874,7 @@ out:
                wakeup(np);
        }
        lck_mtx_unlock(nfs_node_hash_mutex);
-       nfs_unlock2(dnp, np);
+       nfs_node_clear_busy2(dnp, np);
        if (setsize)
                ubc_setsize(vp, 0);
        return (error);
@@ -3171,7 +3903,7 @@ nfs3_remove_rpc(
        thread_t thd,
        kauth_cred_t cred)
 {
-       int error = 0, status, wccpostattr = 0;
+       int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
        struct timespec premtime = { 0, 0 };
        struct nfsmount *nmp;
        int nfsvers;
@@ -3191,14 +3923,17 @@ nfs3_remove_rpc(
        nfsm_chain_build_alloc_init(error, &nmreq,
                NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, name, namelen);
+       nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
 
-       error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, 0, &nmrep, &xid, &status);
+       error = nfs_request2(dnp, NULL, &nmreq, NFSPROC_REMOVE, thd, cred, NULL, 0, &nmrep, &xid, &status);
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        if (nfsvers == NFS_VER3)
                nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
+       nfsmout_if(error);
        dnp->n_flag |= NMODIFIED;
        /* if directory hadn't changed, update namecache mtime */
        if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
@@ -3208,6 +3943,8 @@ nfs3_remove_rpc(
        if (!error)
                error = status;
 nfsmout:
+       if (!lockerror)
+               nfs_node_unlock(dnp);
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
        return (error);
@@ -3216,7 +3953,7 @@ nfsmout:
 /*
  * NFS file rename call
  */
-static int
+int
 nfs_vnop_rename(
        struct vnop_rename_args  /* {
                struct vnodeop_desc *a_desc;
@@ -3241,7 +3978,6 @@ nfs_vnop_rename(
        mount_t fmp, tdmp, tmp;
        struct nfs_vattr nvattr;
        struct nfsmount *nmp;
-       struct nfs_dulookup fdul, tdul;
 
        fdnp = VTONFS(fdvp);
        fnp = VTONFS(fvp);
@@ -3253,7 +3989,7 @@ nfs_vnop_rename(
                return (ENXIO);
        nfsvers = nmp->nm_vers;
 
-       error = nfs_lock4(fdnp, fnp, tdnp, tnp, NFS_NODE_LOCK_EXCLUSIVE);
+       error = nfs_node_set_busy4(fdnp, fnp, tdnp, tnp, vfs_context_thread(ctx));
        if (error)
                return (error);
 
@@ -3269,9 +4005,6 @@ nfs_vnop_rename(
                locked = 1;
        }
 
-       nfs_dulookup_init(&fdul, fdnp, fcnp->cn_nameptr, fcnp->cn_namelen);
-       nfs_dulookup_init(&tdul, tdnp, tcnp->cn_nameptr, tcnp->cn_namelen);
-
        /* Check for cross-device rename */
        fmp = vnode_mount(fvp);
        tmp = tvp ? vnode_mount(tvp) : NULL;
@@ -3301,11 +4034,10 @@ nfs_vnop_rename(
                        /* sillyrename succeeded.*/
                        tvp = NULL;
                }
+       } else if (tvp && (nmp->nm_vers >= NFS_VER4) && (tnp->n_openflags & N_DELEG_MASK)) {
+               nfs4_delegation_return(tnp, 0, vfs_context_thread(ctx), vfs_context_ucred(ctx));
        }
 
-       nfs_dulookup_start(&fdul, fdnp, ctx);
-       nfs_dulookup_start(&tdul, tdnp, ctx);
-
        error = nmp->nm_funcs->nf_rename_rpc(fdnp, fcnp->cn_nameptr, fcnp->cn_namelen,
                        tdnp, tcnp->cn_nameptr, tcnp->cn_namelen, ctx);
 
@@ -3316,8 +4048,10 @@ nfs_vnop_rename(
                error = 0;
 
        if (tvp && (tvp != fvp) && !tnp->n_sillyrename) {
+               nfs_node_lock_force(tnp);
                tvprecycle = (!error && !vnode_isinuse(tvp, 0) &&
-                   (nfs_getattrcache(tnp, &nvattr, 1) || (nvattr.nva_nlink == 1)));
+                   (nfs_getattrcache(tnp, &nvattr, 0) || (nvattr.nva_nlink == 1)));
+               nfs_node_unlock(tnp);
                lck_mtx_lock(nfs_node_hash_mutex);
                if (tvprecycle && (tnp->n_hflag & NHHASHED)) {
                        /*
@@ -3333,21 +4067,26 @@ nfs_vnop_rename(
        }
 
        /* purge the old name cache entries and enter the new one */
-       cache_purge(fvp);
+       nfs_name_cache_purge(fdnp, fnp, fcnp, ctx);
        if (tvp) {
-               cache_purge(tvp);
+               nfs_name_cache_purge(tdnp, tnp, tcnp, ctx);
                if (tvprecycle) {
                        /* clear flags now: won't get nfs_vnop_inactive for recycled vnode */
                        /* clear all flags other than these */
+                       nfs_node_lock_force(tnp);
                        tnp->n_flag &= (NMODIFIED);
+                       nfs_node_unlock(tnp);
                        vnode_recycle(tvp);
                }
        }
        if (!error) {
+               nfs_node_lock_force(tdnp);
                if (tdnp->n_flag & NNEGNCENTRIES) {
                        tdnp->n_flag &= ~NNEGNCENTRIES;
                        cache_purge_negatives(tdvp);
                }
+               nfs_node_unlock(tdnp);
+               nfs_node_lock_force(fnp);
                cache_enter(tdvp, fvp, tcnp);
                if (tdvp != fdvp) {     /* update parent pointer */
                        if (fnp->n_parent && !vnode_get(fnp->n_parent)) {
@@ -3364,24 +4103,12 @@ nfs_vnop_rename(
                                fnp->n_parent = NULL;
                        }
                }
+               nfs_node_unlock(fnp);
        }
 out:
-       if (!nfs_getattr(fdnp, &nvattr, ctx, 1)) {
-               if (NFS_CHANGED_NC(nfsvers, fdnp, &nvattr)) {
-                       fdnp->n_flag &= ~NNEGNCENTRIES;
-                       cache_purge(fdvp);
-                       NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &nvattr);
-               }
-       }
-       if (!nfs_getattr(tdnp, &nvattr, ctx, 1)) {
-               if (NFS_CHANGED_NC(nfsvers, tdnp, &nvattr)) {
-                       tdnp->n_flag &= ~NNEGNCENTRIES;
-                       cache_purge(tdvp);
-                       NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &nvattr);
-               }
-       }
-       nfs_dulookup_finish(&fdul, fdnp, ctx);
-       nfs_dulookup_finish(&tdul, tdnp, ctx);
+       /* nfs_getattr() will check changed and purge caches */
+       nfs_getattr(fdnp, NULL, ctx, NGA_CACHED);
+       nfs_getattr(tdnp, NULL, ctx, NGA_CACHED);
        if (locked) {
                /* unlock node */
                lck_mtx_lock(nfs_node_hash_mutex);
@@ -3392,7 +4119,7 @@ out:
                }
                lck_mtx_unlock(nfs_node_hash_mutex);
        }
-       nfs_unlock4(fdnp, fnp, tdnp, tnp);
+       nfs_node_clear_busy4(fdnp, fnp, tdnp, tnp);
        return (error);
 }
 
@@ -3409,7 +4136,7 @@ nfs3_rename_rpc(
        int tnamelen,
        vfs_context_t ctx)
 {
-       int error = 0, status, fwccpostattr = 0, twccpostattr = 0;
+       int error = 0, lockerror = ENOENT, status, fwccpostattr = 0, twccpostattr = 0;
        struct timespec fpremtime = { 0, 0 }, tpremtime = { 0, 0 };
        struct nfsmount *nmp;
        int nfsvers;
@@ -3431,14 +4158,16 @@ nfs3_rename_rpc(
                (NFSX_FH(nfsvers) + NFSX_UNSIGNED) * 2 +
                nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, fdnp->n_fhp, fdnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, fnameptr, fnamelen);
+       nfsm_chain_add_name(error, &nmreq, fnameptr, fnamelen, nmp);
        nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, tnameptr, tnamelen);
+       nfsm_chain_add_name(error, &nmreq, tnameptr, tnamelen, nmp);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
 
-       error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, &nmrep, &xid, &status);
+       error = nfs_request(fdnp, NULL, &nmreq, NFSPROC_RENAME, ctx, NULL, &nmrep, &xid, &status);
 
+       if ((lockerror = nfs_node_lock2(fdnp, tdnp)))
+               error = lockerror;
        if (nfsvers == NFS_VER3) {
                txid = xid;
                nfsm_chain_get_wcc_data(error, &nmrep, fdnp, &fpremtime, &fwccpostattr, &xid);
@@ -3449,25 +4178,28 @@ nfs3_rename_rpc(
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
-       fdnp->n_flag |= NMODIFIED;
-       /* if directory hadn't changed, update namecache mtime */
-       if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==))
-               NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr);
-       if (!fwccpostattr)
-               NATTRINVALIDATE(fdnp);
-       tdnp->n_flag |= NMODIFIED;
-       /* if directory hadn't changed, update namecache mtime */
-       if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==))
-               NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
-       if (!twccpostattr)
-               NATTRINVALIDATE(tdnp);
+       if (!lockerror) {
+               fdnp->n_flag |= NMODIFIED;
+               /* if directory hadn't changed, update namecache mtime */
+               if (nfstimespeccmp(&fdnp->n_ncmtime, &fpremtime, ==))
+                       NFS_CHANGED_UPDATE_NC(nfsvers, fdnp, &fdnp->n_vattr);
+               if (!fwccpostattr)
+                       NATTRINVALIDATE(fdnp);
+               tdnp->n_flag |= NMODIFIED;
+               /* if directory hadn't changed, update namecache mtime */
+               if (nfstimespeccmp(&tdnp->n_ncmtime, &tpremtime, ==))
+                       NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
+               if (!twccpostattr)
+                       NATTRINVALIDATE(tdnp);
+               nfs_node_unlock2(fdnp, tdnp);
+       }
        return (error);
 }
 
 /*
  * NFS hard link create call
  */
-static int
+int
 nfs3_vnop_link(
        struct vnop_link_args /* {
                struct vnodeop_desc *a_desc;
@@ -3481,7 +4213,7 @@ nfs3_vnop_link(
        vnode_t vp = ap->a_vp;
        vnode_t tdvp = ap->a_tdvp;
        struct componentname *cnp = ap->a_cnp;
-       int error = 0, status, wccpostattr = 0, attrflag = 0;
+       int error = 0, lockerror = ENOENT, status, wccpostattr = 0, attrflag = 0;
        struct timespec premtime = { 0, 0 };
        struct nfsmount *nmp;
        nfsnode_t np = VTONFS(vp);
@@ -3507,7 +4239,7 @@ nfs3_vnop_link(
         */
        nfs_flush(np, MNT_WAIT, vfs_context_thread(ctx), V_IGNORE_WRITEERR);
 
-       error = nfs_lock2(tdnp, np, NFS_NODE_LOCK_EXCLUSIVE);
+       error = nfs_node_set_busy2(tdnp, np, vfs_context_thread(ctx));
        if (error)
                return (error);
 
@@ -3518,11 +4250,15 @@ nfs3_vnop_link(
                NFSX_FH(nfsvers)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
        nfsm_chain_add_fh(error, &nmreq, nfsvers, tdnp->n_fhp, tdnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx,
-                       &nmrep, &xid, &status);
+       error = nfs_request(np, NULL, &nmreq, NFSPROC_LINK, ctx, NULL, &nmrep, &xid, &status);
+
+       if ((lockerror = nfs_node_lock2(tdnp, np))) {
+               error = lockerror;
+               goto nfsmout;
+       }
        if (nfsvers == NFS_VER3) {
                txid = xid;
                nfsm_chain_postop_attr_update_flag(error, &nmrep, np, attrflag, &xid);
@@ -3533,19 +4269,22 @@ nfs3_vnop_link(
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
-       tdnp->n_flag |= NMODIFIED;
-       if (!attrflag)
-               NATTRINVALIDATE(np);
-       /* if directory hadn't changed, update namecache mtime */
-       if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==))
-               NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
-       if (!wccpostattr)
-               NATTRINVALIDATE(tdnp);
-       if (!error && (tdnp->n_flag & NNEGNCENTRIES)) {
-               tdnp->n_flag &= ~NNEGNCENTRIES;
-               cache_purge_negatives(tdvp);
+       if (!lockerror) {
+               if (!attrflag)
+                       NATTRINVALIDATE(np);
+               tdnp->n_flag |= NMODIFIED;
+               /* if directory hadn't changed, update namecache mtime */
+               if (nfstimespeccmp(&tdnp->n_ncmtime, &premtime, ==))
+                       NFS_CHANGED_UPDATE_NC(nfsvers, tdnp, &tdnp->n_vattr);
+               if (!wccpostattr)
+                       NATTRINVALIDATE(tdnp);
+               if (!error && (tdnp->n_flag & NNEGNCENTRIES)) {
+                       tdnp->n_flag &= ~NNEGNCENTRIES;
+                       cache_purge_negatives(tdvp);
+               }
+               nfs_node_unlock2(tdnp, np);
        }
-       nfs_unlock2(tdnp, np);
+       nfs_node_clear_busy2(tdnp, np);
        /*
         * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
         */
@@ -3557,7 +4296,7 @@ nfsmout:
 /*
  * NFS symbolic link create call
  */
-static int
+int
 nfs3_vnop_symlink(
        struct vnop_symlink_args /* {
                struct vnodeop_desc *a_desc;
@@ -3573,18 +4312,18 @@ nfs3_vnop_symlink(
        vnode_t dvp = ap->a_dvp;
        struct vnode_attr *vap = ap->a_vap;
        struct componentname *cnp = ap->a_cnp;
-       struct nfs_vattr nvattr, dnvattr;
+       struct nfs_vattr nvattr;
        fhandle_t fh;
-       int slen, error = 0, lockerror = ENOENT, status, wccpostattr = 0;
+       int slen, error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
        struct timespec premtime = { 0, 0 };
        vnode_t newvp = NULL;
        int nfsvers, gotuid, gotgid;
-       u_int64_t xid, dxid;
+       u_int64_t xid = 0, dxid;
        nfsnode_t np = NULL;
        nfsnode_t dnp = VTONFS(dvp);
        struct nfsmount *nmp;
        struct nfsm_chain nmreq, nmrep;
-       struct nfsreq *req = NULL;
+       struct nfsreq rq, *req = &rq;
        struct nfs_dulookup dul;
 
        nmp = VTONMP(dvp);
@@ -3597,6 +4336,8 @@ nfs3_vnop_symlink(
            ((cnp->cn_namelen > NFS_MAXNAMLEN) || (slen > NFS_MAXPATHLEN)))
                return (ENAMETOOLONG);
 
+       nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
+
        VATTR_SET_SUPPORTED(vap, va_mode);
        VATTR_SET_SUPPORTED(vap, va_uid);
        VATTR_SET_SUPPORTED(vap, va_gid);
@@ -3606,7 +4347,8 @@ nfs3_vnop_symlink(
        gotuid = VATTR_IS_ACTIVE(vap, va_uid);
        gotgid = VATTR_IS_ACTIVE(vap, va_gid);
 
-       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen);
+       error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
+       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
 
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
@@ -3615,25 +4357,24 @@ nfs3_vnop_symlink(
                NFSX_FH(nfsvers) + 2 * NFSX_UNSIGNED +
                nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(nfsvers));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        if (nfsvers == NFS_VER3)
                nfsm_chain_add_v3sattr(error, &nmreq, vap);
-       nfsm_chain_add_string(error, &nmreq, ap->a_target, slen);
+       nfsm_chain_add_name(error, &nmreq, ap->a_target, slen, nmp);
        if (nfsvers == NFS_VER2)
                nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
-       nfsmout_if(error);
 
        error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_SYMLINK,
-                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
        if (!error) {
                nfs_dulookup_start(&dul, dnp, ctx);
                error = nfs_request_async_finish(req, &nmrep, &xid, &status);
        }
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        dxid = xid;
        if (!error && !status) {
                if (dnp->n_flag & NNEGNCENTRIES) {
@@ -3658,19 +4399,13 @@ nfsmout:
                /* if directory hadn't changed, update namecache mtime */
                if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
                        NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
-               if (!wccpostattr)
-                       NATTRINVALIDATE(dnp);
-               if (!nfs_getattr(dnp, &dnvattr, ctx, 1)) {
-                       if (NFS_CHANGED_NC(nfsvers, dnp, &dnvattr)) {
-                               dnp->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(dvp);
-                               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnvattr);
-                       }
-               }
+               nfs_node_unlock(dnp);
+               /* nfs_getattr() will check changed and purge caches */
+               nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
        }
 
        if (!error && fh.fh_len)
-               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, NG_MAKEENTRY, &np);
+               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
        if (!error && np)
                newvp = NFSTOV(np);
 
@@ -3682,7 +4417,7 @@ nfsmout:
         */
        if ((error == EEXIST) || (!error && !newvp)) {
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                        newvp = NULL;
                }
@@ -3693,10 +4428,10 @@ nfsmout:
                                error = EEXIST;
                }
        }
-       if (!lockerror)
-               nfs_unlock(dnp);
+       if (!busyerror)
+               nfs_node_clear_busy(dnp);
        if (!error && (gotuid || gotgid) &&
-           (!newvp || nfs_getattrcache(np, &nvattr, 1) ||
+           (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
             (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
             (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
                /* clear ID bits if server didn't use them (or we can't tell) */
@@ -3705,11 +4440,11 @@ nfsmout:
        }
        if (error) {
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                }
        } else {
-               nfs_unlock(np);
+               nfs_node_unlock(np);
                *ap->a_vpp = newvp;
        }
        return (error);
@@ -3718,7 +4453,7 @@ nfsmout:
 /*
  * NFS make dir call
  */
-static int
+int
 nfs3_vnop_mkdir(
        struct vnop_mkdir_args /* {
                struct vnodeop_desc *a_desc;
@@ -3733,18 +4468,18 @@ nfs3_vnop_mkdir(
        vnode_t dvp = ap->a_dvp;
        struct vnode_attr *vap = ap->a_vap;
        struct componentname *cnp = ap->a_cnp;
-       struct nfs_vattr nvattr, dnvattr;
+       struct nfs_vattr nvattr;
        nfsnode_t np = NULL;
        struct nfsmount *nmp;
        nfsnode_t dnp = VTONFS(dvp);
        vnode_t newvp = NULL;
-       int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
+       int error = 0, lockerror = ENOENT, busyerror = ENOENT, status, wccpostattr = 0;
        struct timespec premtime = { 0, 0 };
        int nfsvers, gotuid, gotgid;
-       u_int64_t xid, dxid;
+       u_int64_t xid= 0, dxid;
        fhandle_t fh;
        struct nfsm_chain nmreq, nmrep;
-       struct nfsreq *req = NULL;
+       struct nfsreq rq, *req = &rq;
        struct nfs_dulookup dul;
 
        nmp = VTONMP(dvp);
@@ -3754,6 +4489,8 @@ nfs3_vnop_mkdir(
        if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
                return (ENAMETOOLONG);
 
+       nfs_avoid_needless_id_setting_on_create(dnp, vap, ctx);
+
        VATTR_SET_SUPPORTED(vap, va_mode);
        VATTR_SET_SUPPORTED(vap, va_uid);
        VATTR_SET_SUPPORTED(vap, va_gid);
@@ -3763,7 +4500,8 @@ nfs3_vnop_mkdir(
        gotuid = VATTR_IS_ACTIVE(vap, va_uid);
        gotgid = VATTR_IS_ACTIVE(vap, va_gid);
 
-       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen);
+       error = busyerror = nfs_node_set_busy(dnp, vfs_context_thread(ctx));
+       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
 
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
@@ -3772,24 +4510,23 @@ nfs3_vnop_mkdir(
                NFSX_FH(nfsvers) + NFSX_UNSIGNED +
                nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(nfsvers));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        if (nfsvers == NFS_VER3)
                nfsm_chain_add_v3sattr(error, &nmreq, vap);
        else
                nfsm_chain_add_v2sattr(error, &nmreq, vap, -1);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
-       nfsmout_if(error);
 
        error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_MKDIR,
-                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
        if (!error) {
                nfs_dulookup_start(&dul, dnp, ctx);
                error = nfs_request_async_finish(req, &nmrep, &xid, &status);
        }
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        dxid = xid;
        if (!error && !status) {
                if (dnp->n_flag & NNEGNCENTRIES) {
@@ -3811,19 +4548,13 @@ nfsmout:
                /* if directory hadn't changed, update namecache mtime */
                if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
                        NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
-               if (!wccpostattr)
-                       NATTRINVALIDATE(dnp);
-               if (!nfs_getattr(dnp, &dnvattr, ctx, 1)) {
-                       if (NFS_CHANGED_NC(nfsvers, dnp, &dnvattr)) {
-                               dnp->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(dvp);
-                               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnvattr);
-                       }
-               }
+               nfs_node_unlock(dnp);
+               /* nfs_getattr() will check changed and purge caches */
+               nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
        }
 
        if (!error && fh.fh_len)
-               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, NG_MAKEENTRY, &np);
+               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len, &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
        if (!error && np)
                newvp = NFSTOV(np);
 
@@ -3833,9 +4564,9 @@ nfsmout:
         * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
         * if we can succeed in looking up the directory.
         */
-       if (error == EEXIST || (!error && !newvp)) {
+       if ((error == EEXIST) || (!error && !newvp)) {
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                        newvp = NULL;
                }
@@ -3846,10 +4577,10 @@ nfsmout:
                                error = EEXIST;
                }
        }
-       if (!lockerror)
-               nfs_unlock(dnp);
+       if (!busyerror)
+               nfs_node_clear_busy(dnp);
        if (!error && (gotuid || gotgid) &&
-           (!newvp || nfs_getattrcache(np, &nvattr, 1) ||
+           (!newvp || nfs_getattrcache(np, &nvattr, 0) ||
             (gotuid && (nvattr.nva_uid != vap->va_uid)) ||
             (gotgid && (nvattr.nva_gid != vap->va_gid)))) {
                /* clear ID bits if server didn't use them (or we can't tell) */
@@ -3858,11 +4589,11 @@ nfsmout:
        }
        if (error) {
                if (newvp) {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_put(newvp);
                }
        } else {
-               nfs_unlock(np);
+               nfs_node_unlock(np);
                *ap->a_vpp = newvp;
        }
        return (error);
@@ -3871,7 +4602,7 @@ nfsmout:
 /*
  * NFS remove directory call
  */
-static int
+int
 nfs3_vnop_rmdir(
        struct vnop_rmdir_args /* {
                struct vnodeop_desc *a_desc;
@@ -3885,16 +4616,15 @@ nfs3_vnop_rmdir(
        vnode_t vp = ap->a_vp;
        vnode_t dvp = ap->a_dvp;
        struct componentname *cnp = ap->a_cnp;
-       int error = 0, status, wccpostattr = 0;
+       int error = 0, lockerror = ENOENT, status, wccpostattr = 0;
        struct timespec premtime = { 0, 0 };
        struct nfsmount *nmp;
        nfsnode_t np = VTONFS(vp);
        nfsnode_t dnp = VTONFS(dvp);
-       struct nfs_vattr dnvattr;
        int nfsvers;
        u_int64_t xid;
        struct nfsm_chain nmreq, nmrep;
-       struct nfsreq *req = NULL;
+       struct nfsreq rq, *req = &rq;
        struct nfs_dulookup dul;
 
        nmp = VTONMP(vp);
@@ -3904,28 +4634,30 @@ nfs3_vnop_rmdir(
        if ((nfsvers == NFS_VER2) && (cnp->cn_namelen > NFS_MAXNAMLEN))
                return (ENAMETOOLONG);
 
-       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen);
-
-       if ((error = nfs_lock2(dnp, np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_set_busy2(dnp, np, vfs_context_thread(ctx))))
                return (error);
 
+       nfs_dulookup_init(&dul, dnp, cnp->cn_nameptr, cnp->cn_namelen, ctx);
+
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
 
        nfsm_chain_build_alloc_init(error, &nmreq,
                NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen);
+       nfsm_chain_add_name(error, &nmreq, cnp->cn_nameptr, cnp->cn_namelen, nmp);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
 
        error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_RMDIR,
-                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, &req);
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, &req);
        if (!error) {
                nfs_dulookup_start(&dul, dnp, ctx);
                error = nfs_request_async_finish(req, &nmrep, &xid, &status);
        }
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        if (nfsvers == NFS_VER3)
                nfsm_chain_get_wcc_data(error, &nmrep, dnp, &premtime, &wccpostattr, &xid);
        if (!error)
@@ -3934,22 +4666,18 @@ nfsmout:
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
 
-       dnp->n_flag |= NMODIFIED;
-       /* if directory hadn't changed, update namecache mtime */
-       if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
-               NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
-       if (!wccpostattr)
-               NATTRINVALIDATE(dnp);
-       cache_purge(vp);
-       if (!nfs_getattr(dnp, &dnvattr, ctx, 1)) {
-               if (NFS_CHANGED_NC(nfsvers, dnp, &dnvattr)) {
-                       dnp->n_flag &= ~NNEGNCENTRIES;
-                       cache_purge(dvp);
-                       NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnvattr);
-               }
+       if (!lockerror) {
+               dnp->n_flag |= NMODIFIED;
+               /* if directory hadn't changed, update namecache mtime */
+               if (nfstimespeccmp(&dnp->n_ncmtime, &premtime, ==))
+                       NFS_CHANGED_UPDATE_NC(nfsvers, dnp, &dnp->n_vattr);
+               nfs_node_unlock(dnp);
+               nfs_name_cache_purge(dnp, np, cnp, ctx);
+               /* nfs_getattr() will check changed and purge caches */
+               nfs_getattr(dnp, NULL, ctx, wccpostattr ? NGA_CACHED : NGA_UNCACHED);
        }
        nfs_dulookup_finish(&dul, dnp, ctx);
-       nfs_unlock2(dnp, np);
+       nfs_node_clear_busy2(dnp, np);
 
        /*
         * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
@@ -3975,609 +4703,979 @@ nfsmout:
 
 /*
  * NFS readdir call
+ *
+ * The incoming "offset" is a directory cookie indicating where in the
+ * directory entries should be read from.  A zero cookie means start at
+ * the beginning of the directory.  Any other cookie will be a cookie
+ * returned from the server.  
+ *
+ * Using that cookie, determine which buffer (and where in that buffer)
+ * to start returning entries from.  Buffer logical block numbers are
+ * the cookies they start at.  If a buffer is found that is not full,
+ * call into the bio/RPC code to fill it.  The RPC code will probably
+ * fill several buffers (dropping the first, requiring a re-get).
+ *
+ * When done copying entries to the buffer, set the offset to the current
+ * entry's cookie and enter that cookie in the cookie cache.
+ *
+ * Note: because the getdirentries(2) API returns a long-typed offset,
+ * the incoming offset is a potentially truncated cookie (ptc).
+ * The cookie matching code is aware of this and will fall back to
+ * matching only 32 bits of the cookie.
  */
-static int
+int
 nfs_vnop_readdir(
        struct vnop_readdir_args /* {
                struct vnodeop_desc *a_desc;
                vnode_t a_vp;
                struct uio *a_uio;
+               int a_flags;
                int *a_eofflag;
-               int *a_ncookies;
-               u_long **a_cookies;
+               int *a_numdirent;
                vfs_context_t a_context;
        } */ *ap)
 {
        vfs_context_t ctx = ap->a_context;
-       vnode_t vp = ap->a_vp;
-       nfsnode_t np = VTONFS(vp);
+       vnode_t dvp = ap->a_vp;
+       nfsnode_t dnp = VTONFS(dvp);
        struct nfsmount *nmp;
-       struct uio *uio = ap->a_uio;
-       int tresid, error, nfsvers;
-       struct nfs_vattr nvattr;
-
-       if (vnode_vtype(vp) != VDIR)
-               return (EPERM);
+       uio_t uio = ap->a_uio;
+       int error, nfsvers, extended, numdirent, bigcookies, ptc, done;
+       uint16_t i, iptc, rlen, nlen;
+       uint64_t cookie, nextcookie, lbn = 0;
+       struct nfsbuf *bp = NULL;
+       struct nfs_dir_buf_header *ndbhp;
+       struct direntry *dp, *dpptc;
+       struct dirent dent;
+       char *cp = NULL;
+       thread_t thd;
 
-       nmp = VTONMP(vp);
+       nmp = VTONMP(dvp);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
+       bigcookies = (nmp->nm_state & NFSSTA_BIGCOOKIES);
+       extended = (ap->a_flags & VNODE_READDIR_EXTENDED);
 
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
-               return (error);
+       if (vnode_vtype(dvp) != VDIR)
+               return (EPERM);
+
+       if (ap->a_eofflag)
+               *ap->a_eofflag = 0;
+
+       if (uio_resid(uio) == 0)
+               return (0);
+
+       if ((nfsvers >= NFS_VER4) && (dnp->n_vattr.nva_flags & NFS_FFLAG_TRIGGER)) {
+               /* trigger directories should never be read, return nothing */
+               return (0);
+       }
+
+       thd = vfs_context_thread(ctx);
+       numdirent = done = 0;
+       nextcookie = uio_offset(uio);
+       ptc = bigcookies && NFS_DIR_COOKIE_POTENTIALLY_TRUNCATED(nextcookie);
+
+       if ((error = nfs_node_lock(dnp)))
+               goto out;
+
+       if (dnp->n_flag & NNEEDINVALIDATE) {
+               dnp->n_flag &= ~NNEEDINVALIDATE;
+               nfs_invaldir(dnp);
+               nfs_node_unlock(dnp);
+               error = nfs_vinvalbuf(dvp, 0, ctx, 1);
+               if (!error)
+                       error = nfs_node_lock(dnp);
+               if (error)
+                       goto out;
+       }
 
        /*
-        * First, check for hit on the EOF offset cache
+        * check for need to invalidate when (re)starting at beginning
         */
-       if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
-           (np->n_flag & NMODIFIED) == 0) {
-               if (!nfs_getattr(np, &nvattr, ctx, 1)) {
-                       if (!NFS_CHANGED(nfsvers, np, &nvattr)) {
-                               nfs_unlock(np);
-                               OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_hits);
+       if (!nextcookie) {
+               if (dnp->n_flag & NMODIFIED) {
+                       nfs_invaldir(dnp);
+                       nfs_node_unlock(dnp);
+                       if ((error = nfs_vinvalbuf(dvp, 0, ctx, 1)))
+                               goto out;
+               } else {
+                       nfs_node_unlock(dnp);
+               }
+               /* nfs_getattr() will check changed and purge caches */
+               if ((error = nfs_getattr(dnp, NULL, ctx, NGA_UNCACHED)))
+                       goto out;
+       } else {
+               nfs_node_unlock(dnp);
+       }
+
+       error = nfs_dir_cookie_to_lbn(dnp, nextcookie, &ptc, &lbn);
+       if (error) {
+               if (error < 0) { /* just hit EOF cookie */
+                       done = 1;
+                       error = 0;
+               }
+               if (ap->a_eofflag)
+                       *ap->a_eofflag = 1;
+       }
+
+       while (!error && !done) {
+               OSAddAtomic64(1, &nfsstats.biocache_readdirs);
+               cookie = nextcookie;
+getbuffer:
+               error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ, &bp);
+               if (error)
+                       goto out;
+               ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+               if (!ISSET(bp->nb_flags, NB_CACHE) || !ISSET(ndbhp->ndbh_flags, NDB_FULL)) {
+                       if (!ISSET(bp->nb_flags, NB_CACHE)) { /* initialize the buffer */
+                               ndbhp->ndbh_flags = 0;
+                               ndbhp->ndbh_count = 0;
+                               ndbhp->ndbh_entry_end = sizeof(*ndbhp);
+                               ndbhp->ndbh_ncgen = dnp->n_ncgen;
+                       }
+                       error = nfs_buf_readdir(bp, ctx);
+                       if (error == NFSERR_DIRBUFDROPPED)
+                               goto getbuffer;
+                       if (error)
+                               nfs_buf_release(bp, 1);
+                       if (error && (error != ENXIO) && (error != ETIMEDOUT) && (error != EINTR) && (error != ERESTART)) {
+                               if (!nfs_node_lock(dnp)) {
+                                       nfs_invaldir(dnp);
+                                       nfs_node_unlock(dnp);
+                               }
+                               nfs_vinvalbuf(dvp, 0, ctx, 1);
+                               if (error == NFSERR_BAD_COOKIE)
+                                       error = ENOENT;
+                       }
+                       if (error)
+                               goto out;
+               }
+
+               /* find next entry to return */
+               dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
+               i = 0;
+               if ((lbn != cookie) && !(ptc && NFS_DIR_COOKIE_SAME32(lbn, cookie))) {
+                       dpptc = NULL;
+                       iptc = 0;
+                       for (; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) {
+                               if (ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) {
+                                       iptc = i;
+                                       dpptc = dp;
+                               }
+                               nextcookie = dp->d_seekoff;
+                               dp = NFS_DIRENTRY_NEXT(dp);
+                       }
+                       if ((i == ndbhp->ndbh_count) && dpptc) {
+                               i = iptc;
+                               dp = dpptc;
+                       }
+                       if (i < ndbhp->ndbh_count) {
+                               nextcookie = dp->d_seekoff;
+                               dp = NFS_DIRENTRY_NEXT(dp);
+                               i++;
+                       }
+               }
+               ptc = 0;  /* only have to deal with ptc on first cookie */
+
+               /* return as many entries as we can */
+               for (; i < ndbhp->ndbh_count; i++) {
+                       if (extended) {
+                               rlen = dp->d_reclen;
+                               cp = (char*)dp;
+                       } else {
+                               if (!cp) {
+                                       cp = (char*)&dent;
+                                       bzero(cp, sizeof(dent));
+                               }
+                               if (dp->d_namlen > (sizeof(dent.d_name) - 1))
+                                       nlen = sizeof(dent.d_name) - 1;
+                               else
+                                       nlen = dp->d_namlen;
+                               rlen = NFS_DIRENT_LEN(nlen);
+                               dent.d_reclen = rlen;
+                               dent.d_ino = dp->d_ino;
+                               dent.d_type = dp->d_type;
+                               dent.d_namlen = nlen;
+                               strlcpy(dent.d_name, dp->d_name, nlen + 1);
+                       }
+                       /* check that the record fits */
+                       if (rlen > uio_resid(uio)) {
+                               done = 1;
+                               break;
+                       }
+                       if ((error = uiomove(cp, rlen, uio)))
+                               break;
+                       numdirent++;
+                       nextcookie = dp->d_seekoff;
+                       dp = NFS_DIRENTRY_NEXT(dp);
+               }
+
+               if (i == ndbhp->ndbh_count) {
+                       /* hit end of buffer, move to next buffer */
+                       lbn = nextcookie;
+                       /* if we also hit EOF, we're done */
+                       if (ISSET(ndbhp->ndbh_flags, NDB_EOF)) {
+                               done = 1;
                                if (ap->a_eofflag)
                                        *ap->a_eofflag = 1;
-                               return (0);
-                       }
-                       if (NFS_CHANGED_NC(nfsvers, np, &nvattr)) {
-                               /* directory changed, purge any name cache entries */
-                               np->n_flag &= ~NNEGNCENTRIES;
-                               cache_purge(vp);
                        }
                }
+               if (!error)
+                       uio_setoffset(uio, nextcookie);
+               if (!error && !done && (nextcookie == cookie)) {
+                       printf("nfs readdir cookie didn't change 0x%llx, %d/%d\n", cookie, i, ndbhp->ndbh_count);
+                       error = EIO;
+               }
+               nfs_buf_release(bp, 1);
        }
-       nfs_unlock(np);
-       if (ap->a_eofflag)
-               *ap->a_eofflag = 0;
 
-       /*
-        * Call nfs_bioread() to do the real work.
-        */
-       // LP64todo - fix this
-       tresid = uio_uio_resid(uio);
-       error = nfs_bioread(np, uio, 0, ap->a_eofflag, ctx);
+       if (!error)
+               nfs_dir_cookie_cache(dnp, nextcookie, lbn);
 
-       if (!error && uio_uio_resid(uio) == tresid)
-               OSAddAtomic(1, (SInt32*)&nfsstats.direofcache_misses);
+       if (ap->a_numdirent)
+               *ap->a_numdirent = numdirent;
+out:
        return (error);
 }
 
+
 /*
- * Readdir RPC call.
- * Called from below the buffer cache by nfs_buf_readdir().
+ * Invalidate cached directory information, except for the actual directory
+ * blocks (which are invalidated separately).
  */
-#define        DIRHDSIZ        ((int)(sizeof(struct dirent) - (MAXNAMLEN + 1)))
-int
-nfs3_readdir_rpc(nfsnode_t dnp, struct uio *uiop, vfs_context_t ctx)
+void
+nfs_invaldir(nfsnode_t dnp)
 {
-       int len, skiplen, left;
-       struct dirent *dp = NULL;
-       nfsuint64 *cookiep;
-       nfsuint64 cookie;
-       struct nfsmount *nmp;
-       u_quad_t fileno;
-       int error = 0, lockerror, status, tlen, more_dirs = 1, blksiz = 0, bigenough = 1, eof;
-       int nfsvers, nmreaddirsize;
-       u_int64_t xid;
-       struct nfsm_chain nmreq, nmrep;
-       char *cp;
+       if (vnode_vtype(NFSTOV(dnp)) != VDIR)
+               return;
+       dnp->n_eofcookie = 0;
+       dnp->n_cookieverf = 0;
+       if (!dnp->n_cookiecache)
+               return;
+       dnp->n_cookiecache->free = 0;
+       dnp->n_cookiecache->mru = -1;
+       memset(dnp->n_cookiecache->next, -1, NFSNUMCOOKIES);
+}
 
-#if DIAGNOSTIC
-       /* XXX limitation based on need to adjust uio */
-       if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
-               (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
-               panic("nfs_readdirrpc: bad uio");
-#endif
-       nmp = NFSTONMP(dnp);
-       if (!nmp)
-               return (ENXIO);
-       nfsvers = nmp->nm_vers;
-       nmreaddirsize = nmp->nm_readdirsize;
+/*
+ * calculate how much space is available for additional directory entries.
+ */
+uint32_t
+nfs_dir_buf_freespace(struct nfsbuf *bp, int rdirplus)
+{
+       struct nfs_dir_buf_header *ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+       uint32_t space;
 
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_SHARED)))
-               return (lockerror);
+       if (!ndbhp)
+               return (0);
+       space = bp->nb_bufsize - ndbhp->ndbh_entry_end;
+       if (rdirplus)
+               space -= ndbhp->ndbh_count * sizeof(struct nfs_vattr);
+       return (space);
+}
 
-       /*
-        * If there is no cookie, assume directory was stale.
-        */
-       cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
-       if (cookiep)
-               cookie = *cookiep;
-       else {
-               nfs_unlock(dnp);
-               return (NFSERR_BAD_COOKIE);
+/*
+ * add/update a cookie->lbn entry in the directory cookie cache
+ */
+void
+nfs_dir_cookie_cache(nfsnode_t dnp, uint64_t cookie, uint64_t lbn)
+{
+       struct nfsdmap *ndcc;
+       int8_t i, prev;
+
+       if (!cookie)
+               return;
+
+       if (nfs_node_lock(dnp))
+               return;
+
+       if (cookie == dnp->n_eofcookie) { /* EOF cookie */
+               nfs_node_unlock(dnp);
+               return;
+       }
+
+       ndcc = dnp->n_cookiecache;
+       if (!ndcc) {
+               /* allocate the cookie cache structure */
+               MALLOC_ZONE(dnp->n_cookiecache, struct nfsdmap *,
+                       sizeof(struct nfsdmap), M_NFSDIROFF, M_WAITOK); 
+               if (!dnp->n_cookiecache) {
+                       nfs_node_unlock(dnp);
+                       return;
+               }
+               ndcc = dnp->n_cookiecache;
+               ndcc->free = 0;
+               ndcc->mru = -1;
+               memset(ndcc->next, -1, NFSNUMCOOKIES);
        }
 
        /*
-        * Loop around doing readdir rpc's of size nm_readdirsize
-        * truncated to a multiple of DIRBLKSIZ.
-        * The stopping criteria is EOF or buffer full.
+        * Search the list for this cookie.
+        * Keep track of previous and last entries.
         */
-       nfsm_chain_null(&nmreq);
-       nfsm_chain_null(&nmrep);
-       while (more_dirs && bigenough) {
-               nfsm_chain_build_alloc_init(error, &nmreq,
-                       NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers));
-               nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-               if (nfsvers == NFS_VER3) {
-                       /* opaque values don't need swapping, but as long */
-                       /* as we are consistent about it, it should be ok */
-                       nfsm_chain_add_32(error, &nmreq, cookie.nfsuquad[0]);
-                       nfsm_chain_add_32(error, &nmreq, cookie.nfsuquad[1]);
-                       nfsm_chain_add_32(error, &nmreq, dnp->n_cookieverf.nfsuquad[0]);
-                       nfsm_chain_add_32(error, &nmreq, dnp->n_cookieverf.nfsuquad[1]);
-               } else {
-                       nfsm_chain_add_32(error, &nmreq, cookie.nfsuquad[0]);
-               }
-               nfsm_chain_add_32(error, &nmreq, nmreaddirsize);
-               nfsm_chain_build_done(error, &nmreq);
-               nfs_unlock(dnp);
-               lockerror = ENOENT;
-               nfsmout_if(error);
+       prev = -1;
+       i = ndcc->mru;
+       while ((i != -1) && (cookie != ndcc->cookies[i].key)) {
+               if (ndcc->next[i] == -1) /* stop on last entry so we can reuse */
+                       break;
+               prev = i;
+               i = ndcc->next[i];
+       }
+       if ((i != -1) && (cookie == ndcc->cookies[i].key)) {
+               /* found it, remove from list */
+               if (prev != -1)
+                       ndcc->next[prev] = ndcc->next[i];
+               else
+                       ndcc->mru = ndcc->next[i];
+       } else {
+               /* not found, use next free entry or reuse last entry */
+               if (ndcc->free != NFSNUMCOOKIES)
+                       i = ndcc->free++;
+               else
+                       ndcc->next[prev] = -1;
+               ndcc->cookies[i].key = cookie;
+               ndcc->cookies[i].lbn = lbn;
+       }
+       /* insert cookie at head of MRU list */
+       ndcc->next[i] = ndcc->mru;
+       ndcc->mru = i;
+       nfs_node_unlock(dnp);
+}
 
-               error = nfs_request(dnp, NULL, &nmreq, NFSPROC_READDIR, ctx,
-                               &nmrep, &xid, &status);
+/*
+ * Try to map the given directory cookie to a directory buffer (return lbn).
+ * If we have a possibly truncated cookie (ptc), check for 32-bit matches too.
+ */
+int
+nfs_dir_cookie_to_lbn(nfsnode_t dnp, uint64_t cookie, int *ptc, uint64_t *lbnp)
+{
+       struct nfsdmap *ndcc = dnp->n_cookiecache;
+       int8_t eofptc, found;
+       int i, iptc;
+       struct nfsmount *nmp;
+       struct nfsbuf *bp, *lastbp;
+       struct nfsbuflists blist;
+       struct direntry *dp, *dpptc;
+       struct nfs_dir_buf_header *ndbhp;
+
+       if (!cookie) {  /* initial cookie */
+               *lbnp = 0;
+               *ptc = 0;
+               return (0);
+       }
 
-               if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-                       error = lockerror;
+       if (nfs_node_lock(dnp))
+               return (ENOENT);
 
-               if (nfsvers == NFS_VER3)
-                       nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
-               if (!error)
-                       error = status;
-               if (nfsvers == NFS_VER3) {
-                       nfsm_chain_get_32(error, &nmrep, dnp->n_cookieverf.nfsuquad[0]);
-                       nfsm_chain_get_32(error, &nmrep, dnp->n_cookieverf.nfsuquad[1]);
-               }
-               nfsm_chain_get_32(error, &nmrep, more_dirs);
+       if (cookie == dnp->n_eofcookie) { /* EOF cookie */
+               nfs_node_unlock(dnp);
+               OSAddAtomic64(1, &nfsstats.direofcache_hits);
+               *ptc = 0;
+               return (-1);
+       }
+       /* note if cookie is a 32-bit match with the EOF cookie */
+       eofptc = *ptc ? NFS_DIR_COOKIE_SAME32(cookie, dnp->n_eofcookie) : 0;
+       iptc = -1;
 
-               if (!lockerror) {
-                       nfs_unlock(dnp);
-                       lockerror = ENOENT;
+       /* search the list for the cookie */
+       for (i = ndcc ? ndcc->mru : -1; i >= 0; i = ndcc->next[i]) {
+               if (ndcc->cookies[i].key == cookie) {
+                       /* found a match for this cookie */
+                       *lbnp = ndcc->cookies[i].lbn;
+                       nfs_node_unlock(dnp);
+                       OSAddAtomic64(1, &nfsstats.direofcache_hits);
+                       *ptc = 0;
+                       return (0);
                }
-               nfsmout_if(error);
+               /* check for 32-bit match */
+               if (*ptc && (iptc == -1) && NFS_DIR_COOKIE_SAME32(ndcc->cookies[i].key, cookie))
+                       iptc = i;
+       }
+       /* exact match not found */
+       if (eofptc) {
+               /* but 32-bit match hit the EOF cookie */
+               nfs_node_unlock(dnp);
+               OSAddAtomic64(1, &nfsstats.direofcache_hits);
+               return (-1);
+       }
+       if (iptc >= 0) {
+               /* but 32-bit match got a hit */
+               *lbnp = ndcc->cookies[iptc].lbn;
+               nfs_node_unlock(dnp);
+               OSAddAtomic64(1, &nfsstats.direofcache_hits);
+               return (0);
+       }
+       nfs_node_unlock(dnp);
 
-               /* loop thru the dir entries, doctoring them to 4bsd form */
-               while (more_dirs && bigenough) {
-                       if (nfsvers == NFS_VER3)
-                               nfsm_chain_get_64(error, &nmrep, fileno);
+       /*
+        * No match found in the cookie cache... hmm...
+        * Let's search the directory's buffers for the cookie.
+        */
+       nmp = NFSTONMP(dnp);
+       if (!nmp)
+               return (ENXIO);
+       dpptc = NULL;
+       found = 0;
+
+       lck_mtx_lock(nfs_buf_mutex);
+       /*
+        * Scan the list of buffers, keeping them in order.
+        * Note that itercomplete inserts each of the remaining buffers
+        * into the head of list (thus reversing the elements).  So, we
+        * make sure to iterate through all buffers, inserting them after
+        * each other, to keep them in order.
+        * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
+        * we don't drop nfs_buf_mutex.
+        */
+       if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
+               lastbp = NULL;
+               while ((bp = LIST_FIRST(&blist))) {
+                       LIST_REMOVE(bp, nb_vnbufs);
+                       if (!lastbp)
+                               LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
                        else
-                               nfsm_chain_get_32(error, &nmrep, fileno);
-                       nfsm_chain_get_32(error, &nmrep, len);
-                       nfsmout_if(error);
-                       /* Note: v3 supports longer names, but struct dirent doesn't */
-                       /* so we just truncate the names to fit */
-                       if (len <= 0) {
-                               error = EBADRPC;
-                               goto nfsmout;
+                               LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
+                       lastbp = bp;
+                       if (found)
+                               continue;
+                       nfs_buf_refget(bp);
+                       if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
+                               /* just skip this buffer */
+                               nfs_buf_refrele(bp);
+                               continue;
                        }
-                       if (len > MAXNAMLEN) {
-                               skiplen = len - MAXNAMLEN;
-                               len = MAXNAMLEN;
-                       } else {
-                               skiplen = 0;
+                       nfs_buf_refrele(bp);
+
+                       /* scan the buffer for the cookie */
+                       ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+                       dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
+                       dpptc = NULL;
+                       for (i=0; (i < ndbhp->ndbh_count) && (cookie != dp->d_seekoff); i++) {
+                               if (*ptc && !dpptc && NFS_DIR_COOKIE_SAME32(cookie, dp->d_seekoff)) {
+                                       dpptc = dp;
+                                       iptc = i;
+                               }
+                               dp = NFS_DIRENTRY_NEXT(dp);
                        }
-                       tlen = nfsm_rndup(len);
-                       if (tlen == len)
-                               tlen += 4;      /* To ensure null termination */
-                       left = DIRBLKSIZ - blksiz;
-                       if ((tlen + DIRHDSIZ) > left) {
-                               dp->d_reclen += left;
-                               uio_iov_base_add(uiop, left);
-                               uio_iov_len_add(uiop, -left);
-                               uiop->uio_offset += left;
-                               uio_uio_resid_add(uiop, -left);
-                               blksiz = 0;
+                       if ((i == ndbhp->ndbh_count) && dpptc) {
+                               /* found only a PTC match */
+                               dp = dpptc;
+                               i = iptc;
+                       } else if (i < ndbhp->ndbh_count) {
+                               *ptc = 0;
                        }
-                       if ((tlen + DIRHDSIZ) > uio_uio_resid(uiop))
-                               bigenough = 0;
-                       if (bigenough) {
-                               // LP64todo - fix this!
-                               dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
-                               dp->d_fileno = (int)fileno;
-                               dp->d_namlen = len;
-                               dp->d_reclen = tlen + DIRHDSIZ;
-                               dp->d_type = DT_UNKNOWN;
-                               blksiz += dp->d_reclen;
-                               if (blksiz == DIRBLKSIZ)
-                                       blksiz = 0;
-                               uiop->uio_offset += DIRHDSIZ;
-#if LP64KERN
-                               uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
-                               uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
-#else
-                               uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
-                               uio_iov_len_add(uiop, -((int)DIRHDSIZ));
-#endif
-                               uio_iov_base_add(uiop, DIRHDSIZ);
-                               error = nfsm_chain_get_uio(&nmrep, len, uiop);
-                               nfsmout_if(error);
-                               // LP64todo - fix this!
-                               cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
-                               tlen -= len;
-                               *cp = '\0';     /* null terminate */
-                               uio_iov_base_add(uiop, tlen);
-                               uio_iov_len_add(uiop, -tlen);
-                               uiop->uio_offset += tlen;
-                               uio_uio_resid_add(uiop, -tlen);
-                               if (skiplen)
-                                       nfsm_chain_adv(error, &nmrep,
-                                               nfsm_rndup(len + skiplen) - nfsm_rndup(len));
-                       } else {
-                               nfsm_chain_adv(error, &nmrep, nfsm_rndup(len + skiplen));
+                       if (i < (ndbhp->ndbh_count-1)) {
+                               /* next entry is *in* this buffer: return this block */
+                               *lbnp = bp->nb_lblkno;
+                               found = 1;
+                       } else if (i == (ndbhp->ndbh_count-1)) {
+                               /* next entry refers to *next* buffer: return next block */
+                               *lbnp = dp->d_seekoff;
+                               found = 1;
                        }
-                       if (bigenough) {
-                               nfsm_chain_get_32(error, &nmrep, cookie.nfsuquad[0]);
-                               if (nfsvers == NFS_VER3)
-                                       nfsm_chain_get_32(error, &nmrep, cookie.nfsuquad[1]);
-                       } else if (nfsvers == NFS_VER3)
-                               nfsm_chain_adv(error, &nmrep, 2 * NFSX_UNSIGNED);
-                       else
-                               nfsm_chain_adv(error, &nmrep, NFSX_UNSIGNED);
-                       nfsm_chain_get_32(error, &nmrep, more_dirs);
-                       nfsmout_if(error);
-               }
-               /*
-                * If at end of rpc data, get the eof boolean
-                */
-               if (!more_dirs) {
-                       nfsm_chain_get_32(error, &nmrep, eof);
-                       if (!error)
-                               more_dirs = (eof == 0);
+                       nfs_buf_drop(bp);
                }
-               if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_SHARED)))
-                       error = lockerror;
-               nfsmout_if(error);
-               nfsm_chain_cleanup(&nmrep);
-               nfsm_chain_null(&nmreq);
+               nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
        }
-       if (!lockerror) {
-               nfs_unlock(dnp);
-               lockerror = ENOENT;
+       lck_mtx_unlock(nfs_buf_mutex);
+       if (found) {
+               OSAddAtomic64(1, &nfsstats.direofcache_hits);
+               return (0);
        }
-       /*
-        * Fill last record, iff any, out to a multiple of DIRBLKSIZ
-        * by increasing d_reclen for the last record.
-        */
-       if (blksiz > 0) {
-               left = DIRBLKSIZ - blksiz;
-               dp->d_reclen += left;
-               uio_iov_base_add(uiop, left);
-               uio_iov_len_add(uiop, -left);
-               uiop->uio_offset += left;
-               uio_uio_resid_add(uiop, -left);
+
+       /* still not found... oh well, just start a new block */
+       *lbnp = cookie;
+       OSAddAtomic64(1, &nfsstats.direofcache_misses);
+       return (0);
+}
+
+/*
+ * scan a directory buffer for the given name
+ * Returns: ESRCH if not found, ENOENT if found invalid, 0 if found
+ * Note: should only be called with RDIRPLUS directory buffers
+ */
+
+#define NDBS_PURGE     1
+#define NDBS_UPDATE    2
+
+int
+nfs_dir_buf_search(
+       struct nfsbuf *bp,
+       struct componentname *cnp,
+       fhandle_t *fhp,
+       struct nfs_vattr *nvap,
+       uint64_t *xidp,
+       time_t *attrstampp,
+       daddr64_t *nextlbnp,
+       int flags)
+{
+       struct direntry *dp;
+       struct nfs_dir_buf_header *ndbhp;
+       struct nfs_vattr *nvattrp;
+       daddr64_t nextlbn = 0;
+       int i, error = ESRCH, fhlen;
+
+       /* scan the buffer for the name */
+       ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+       dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
+       for (i=0; i < ndbhp->ndbh_count; i++) {
+               nextlbn = dp->d_seekoff;
+               if ((cnp->cn_namelen == dp->d_namlen) && !strcmp(cnp->cn_nameptr, dp->d_name)) {
+                       fhlen = dp->d_name[dp->d_namlen+1];
+                       nvattrp = NFS_DIR_BUF_NVATTR(bp, i);
+                       if ((ndbhp->ndbh_ncgen != bp->nb_np->n_ncgen) || (fhp->fh_len == 0) ||
+                           (nvattrp->nva_type == VNON) || (nvattrp->nva_fileid == 0)) {
+                               /* entry is not valid */
+                               error = ENOENT;
+                               break;
+                       }
+                       if (flags == NDBS_PURGE) {
+                               dp->d_fileno = 0;
+                               bzero(nvattrp, sizeof(*nvattrp));
+                               error = ENOENT;
+                               break;
+                       }
+                       if (flags == NDBS_UPDATE) {
+                               /* update direntry's attrs if fh matches */
+                               if ((fhp->fh_len == fhlen) && !bcmp(&dp->d_name[dp->d_namlen+2], fhp->fh_data, fhlen)) {
+                                       bcopy(nvap, nvattrp, sizeof(*nvap));
+                                       dp->d_fileno = nvattrp->nva_fileid;
+                                       nvattrp->nva_fileid = *xidp;
+                                       *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]) = *attrstampp;
+                               }
+                               error = 0;
+                               break;
+                       }
+                       /* copy out fh, attrs, attrstamp, and xid */
+                       fhp->fh_len = fhlen;
+                       bcopy(&dp->d_name[dp->d_namlen+2], fhp->fh_data, MAX(fhp->fh_len, (int)sizeof(fhp->fh_data)));
+                       *attrstampp = *(time_t*)(&dp->d_name[dp->d_namlen+2+fhp->fh_len]);
+                       bcopy(nvattrp, nvap, sizeof(*nvap));
+                       *xidp = nvap->nva_fileid;
+                       nvap->nva_fileid = dp->d_fileno;
+                       error = 0;
+                       break;
+               }
+               dp = NFS_DIRENTRY_NEXT(dp);
+       }
+       if (nextlbnp)
+               *nextlbnp = nextlbn;
+       return (error);
+}
+
+/*
+ * Look up a name in a directory's buffers.
+ * Note: should only be called with RDIRPLUS directory buffers
+ */
+int
+nfs_dir_buf_cache_lookup(nfsnode_t dnp, nfsnode_t *npp, struct componentname *cnp, vfs_context_t ctx, int purge)
+{
+       nfsnode_t newnp;
+       struct nfsmount *nmp;
+       int error = 0, i, found = 0, count = 0;
+       u_int64_t xid;
+       struct nfs_vattr nvattr;
+       fhandle_t fh;
+       time_t attrstamp = 0;
+       thread_t thd = vfs_context_thread(ctx);
+       struct nfsbuf *bp, *lastbp, *foundbp;
+       struct nfsbuflists blist;
+       daddr64_t lbn, nextlbn;
+       int dotunder = (cnp->cn_namelen > 2) && (cnp->cn_nameptr[0] == '.') && (cnp->cn_nameptr[1] == '_');
+
+       if (!(nmp = NFSTONMP(dnp)))
+               return (ENXIO);
+       if (!purge)
+               *npp = NULL;
+
+       /* first check most recent buffer (and next one too) */
+       lbn = dnp->n_lastdbl;
+       for (i=0; i < 2; i++) {
+               if ((error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp)))
+                       return (error);
+               if (!bp)
+                       break;
+               count++;
+               error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, &nextlbn, purge ? NDBS_PURGE : 0);
+               nfs_buf_release(bp, 0);
+               if (error == ESRCH) {
+                       error = 0;
+               } else {
+                       found = 1;
+                       break;
+               }
+               lbn = nextlbn;
        }
 
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
-       nfsmout_if(error);
+       lck_mtx_lock(nfs_buf_mutex);
+       if (found) {
+               dnp->n_lastdbl = lbn;
+               goto done;
+       }
 
        /*
-        * We are now either at the end of the directory or have filled the
-        * block.
+        * Scan the list of buffers, keeping them in order.
+        * Note that itercomplete inserts each of the remaining buffers
+        * into the head of list (thus reversing the elements).  So, we
+        * make sure to iterate through all buffers, inserting them after
+        * each other, to keep them in order.
+        * Also note: the LIST_INSERT_AFTER(lastbp) is only safe because
+        * we don't drop nfs_buf_mutex.
         */
-       if (bigenough)
-               dnp->n_direofoffset = uiop->uio_offset;
-       else {
-               if (uio_uio_resid(uiop) > 0)
-                       printf("EEK! readdirrpc resid > 0\n");
-               cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
-               if (cookiep)
-                       *cookiep = cookie;
+       if (!nfs_buf_iterprepare(dnp, &blist, NBI_CLEAN)) {
+               lastbp = foundbp = NULL;
+               while ((bp = LIST_FIRST(&blist))) {
+                       LIST_REMOVE(bp, nb_vnbufs);
+                       if (!lastbp)
+                               LIST_INSERT_HEAD(&dnp->n_cleanblkhd, bp, nb_vnbufs);
+                       else
+                               LIST_INSERT_AFTER(lastbp, bp, nb_vnbufs);
+                       lastbp = bp;
+                       if (error || found)
+                               continue;
+                       if (!purge && dotunder && (count > 100)) /* don't waste too much time looking for ._ files */
+                               continue;
+                       nfs_buf_refget(bp);
+                       lbn = bp->nb_lblkno;
+                       if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
+                               /* just skip this buffer */
+                               nfs_buf_refrele(bp);
+                               continue;
+                       }
+                       nfs_buf_refrele(bp);
+                       count++;
+                       error = nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, purge ? NDBS_PURGE : 0);
+                       if (error == ESRCH) {
+                               error = 0;
+                       } else {
+                               found = 1;
+                               foundbp = bp;
+                       }
+                       nfs_buf_drop(bp);
+               }
+               if (found) {
+                       LIST_REMOVE(foundbp, nb_vnbufs);
+                       LIST_INSERT_HEAD(&dnp->n_cleanblkhd, foundbp, nb_vnbufs);
+                       dnp->n_lastdbl = foundbp->nb_lblkno;
+               }
+               nfs_buf_itercomplete(dnp, &blist, NBI_CLEAN);
+       }
+done:
+       lck_mtx_unlock(nfs_buf_mutex);
+
+       if (!error && found && !purge) {
+               error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
+                               &nvattr, &xid, dnp->n_auth, NG_MAKEENTRY, &newnp);
+               if (error)
+                       return (error);
+               newnp->n_attrstamp = attrstamp;
+               *npp = newnp;
+               nfs_node_unlock(newnp);
+               /* check if the dir buffer's attrs are out of date */
+               if (!nfs_getattr(newnp, &nvattr, ctx, NGA_CACHED) &&
+                   (newnp->n_attrstamp != attrstamp)) {
+                       /* they are, so update them */
+                       error = nfs_buf_get(dnp, lbn, NFS_DIRBLKSIZ, thd, NBLK_READ|NBLK_ONLYVALID, &bp);
+                       if (!error && bp) {
+                               attrstamp = newnp->n_attrstamp;
+                               xid = newnp->n_xid;
+                               nfs_dir_buf_search(bp, cnp, &fh, &nvattr, &xid, &attrstamp, NULL, NDBS_UPDATE);
+                               nfs_buf_release(bp, 0);
+                       }
+                       error = 0;
+               }
        }
 
-nfsmout:
-       if (!lockerror)
-               nfs_unlock(dnp);
-       nfsm_chain_cleanup(&nmreq);
-       nfsm_chain_cleanup(&nmrep);
        return (error);
 }
 
 /*
- * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
+ * Purge name cache entries for the given node.
+ * For RDIRPLUS, also invalidate the entry in the directory's buffers.
+ */
+void
+nfs_name_cache_purge(nfsnode_t dnp, nfsnode_t np, struct componentname *cnp, vfs_context_t ctx)
+{
+       struct nfsmount *nmp = NFSTONMP(dnp);
+
+       cache_purge(NFSTOV(np));
+       if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS))
+               nfs_dir_buf_cache_lookup(dnp, NULL, cnp, ctx, 1);
+}
+
+/*
+ * NFS V3 readdir (plus) RPC.
  */
 int
-nfs3_readdirplus_rpc(nfsnode_t dnp, struct uio *uiop, vfs_context_t ctx)
+nfs3_readdir_rpc(nfsnode_t dnp, struct nfsbuf *bp, vfs_context_t ctx)
 {
-       size_t len, tlen, skiplen, left;
-       struct dirent *dp = NULL;
-       vnode_t newvp;
-       nfsuint64 *cookiep;
-       struct componentname cn, *cnp = &cn;
-       nfsuint64 cookie;
        struct nfsmount *nmp;
-       nfsnode_t np;
-       u_char *fhp;
-       u_quad_t fileno;
-       int error = 0, lockerror, status, more_dirs = 1, blksiz = 0, doit, bigenough = 1;
-       int nfsvers, nmreaddirsize, nmrsize, attrflag, eof;
-       size_t fhsize;
-       u_int64_t xid, savexid;
-       struct nfs_vattr nvattr;
-       struct nfsm_chain nmreq, nmrep;
-       char *cp;
+       int error = 0, lockerror, nfsvers, rdirplus, bigcookies;
+       int i, status, attrflag, fhflag, more_entries = 1, eof, bp_dropped = 0;
+       uint32_t nmreaddirsize, nmrsize;
+       uint32_t namlen, skiplen, fhlen, xlen, attrlen, reclen, space_free, space_needed;
+       uint64_t cookie, lastcookie, xid, savedxid, fileno;
+       struct nfsm_chain nmreq, nmrep, nmrepsave;
+       fhandle_t fh;
+       struct nfs_vattr *nvattrp;
+       struct nfs_dir_buf_header *ndbhp;
+       struct direntry *dp;
+       char *padstart, padlen;
+       struct timeval now;
 
-#if DIAGNOSTIC
-       /* XXX limitation based on need to adjust uio */
-       if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
-               (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)))
-               panic("nfs3_readdirplus_rpc: bad uio");
-#endif
        nmp = NFSTONMP(dnp);
        if (!nmp)
                return (ENXIO);
        nfsvers = nmp->nm_vers;
        nmreaddirsize = nmp->nm_readdirsize;
        nmrsize = nmp->nm_rsize;
+       bigcookies = nmp->nm_state & NFSSTA_BIGCOOKIES;
+noplus:
+       rdirplus = ((nfsvers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) ? 1 : 0;
 
-       bzero(cnp, sizeof(*cnp));
-       newvp = NULLVP;
-
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_SHARED)))
+       if ((lockerror = nfs_node_lock(dnp)))
                return (lockerror);
 
-       /*
-        * If there is no cookie, assume directory was stale.
-        */
-       cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
-       if (cookiep)
-               cookie = *cookiep;
-       else {
-               nfs_unlock(dnp);
-               return (NFSERR_BAD_COOKIE);
+       /* determine cookie to use, and move dp to the right offset */
+       ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+       dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
+       if (ndbhp->ndbh_count) {
+               for (i=0; i < ndbhp->ndbh_count-1; i++)
+                       dp = NFS_DIRENTRY_NEXT(dp);
+               cookie = dp->d_seekoff;
+               dp = NFS_DIRENTRY_NEXT(dp);
+       } else {
+               cookie = bp->nb_lblkno;
+               /* increment with every buffer read */
+               OSAddAtomic64(1, &nfsstats.readdir_bios);
        }
+       lastcookie = cookie;
 
        /*
-        * Loop around doing readdir rpc's of size nm_readdirsize
-        * truncated to a multiple of DIRBLKSIZ.
-        * The stopping criteria is EOF or buffer full.
+        * Loop around doing readdir(plus) RPCs of size nm_readdirsize until
+        * the buffer is full (or we hit EOF).  Then put the remainder of the
+        * results in the next buffer(s).
         */
        nfsm_chain_null(&nmreq);
        nfsm_chain_null(&nmrep);
-       while (more_dirs && bigenough) {
+       while (nfs_dir_buf_freespace(bp, rdirplus) && !(ndbhp->ndbh_flags & NDB_FULL)) {
                nfsm_chain_build_alloc_init(error, &nmreq,
-                       NFSX_FH(NFS_VER3) + 6 * NFSX_UNSIGNED);
+                       NFSX_FH(nfsvers) + NFSX_READDIR(nfsvers) + NFSX_UNSIGNED);
                nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-               /* opaque values don't need swapping, but as long */
-               /* as we are consistent about it, it should be ok */
-               nfsm_chain_add_32(error, &nmreq, cookie.nfsuquad[0]);
-               nfsm_chain_add_32(error, &nmreq, cookie.nfsuquad[1]);
-               nfsm_chain_add_32(error, &nmreq, dnp->n_cookieverf.nfsuquad[0]);
-               nfsm_chain_add_32(error, &nmreq, dnp->n_cookieverf.nfsuquad[1]);
+               if (nfsvers == NFS_VER3) {
+                       /* opaque values don't need swapping, but as long */
+                       /* as we are consistent about it, it should be ok */
+                       nfsm_chain_add_64(error, &nmreq, cookie);
+                       nfsm_chain_add_64(error, &nmreq, dnp->n_cookieverf);
+               } else {
+                       nfsm_chain_add_32(error, &nmreq, cookie);
+               }
                nfsm_chain_add_32(error, &nmreq, nmreaddirsize);
-               nfsm_chain_add_32(error, &nmreq, nmrsize);
+               if (rdirplus)
+                       nfsm_chain_add_32(error, &nmreq, nmrsize);
                nfsm_chain_build_done(error, &nmreq);
-               nfs_unlock(dnp);
+               nfs_node_unlock(dnp);
                lockerror = ENOENT;
                nfsmout_if(error);
 
-               error = nfs_request(dnp, NULL, &nmreq, NFSPROC_READDIRPLUS, ctx,
-                               &nmrep, &xid, &status);
+               error = nfs_request(dnp, NULL, &nmreq,
+                               rdirplus ? NFSPROC_READDIRPLUS : NFSPROC_READDIR,
+                               ctx, NULL, &nmrep, &xid, &status);
 
-               if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
+               if ((lockerror = nfs_node_lock(dnp)))
                        error = lockerror;
 
-               savexid = xid;
-               nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
+               savedxid = xid;
+               if (nfsvers == NFS_VER3)
+                       nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
                if (!error)
                        error = status;
-               nfsm_chain_get_32(error, &nmrep, dnp->n_cookieverf.nfsuquad[0]);
-               nfsm_chain_get_32(error, &nmrep, dnp->n_cookieverf.nfsuquad[1]);
-               nfsm_chain_get_32(error, &nmrep, more_dirs);
+               if (nfsvers == NFS_VER3)
+                       nfsm_chain_get_64(error, &nmrep, dnp->n_cookieverf);
+               nfsm_chain_get_32(error, &nmrep, more_entries);
 
                if (!lockerror) {
-                       nfs_unlock(dnp);
+                       nfs_node_unlock(dnp);
                        lockerror = ENOENT;
                }
+               if (error == NFSERR_NOTSUPP) {
+                       /* oops... it doesn't look like readdirplus is supported */
+                       lck_mtx_lock(&nmp->nm_lock);
+                       NFS_BITMAP_CLR(nmp->nm_flags, NFS_MFLAG_RDIRPLUS);
+                       lck_mtx_unlock(&nmp->nm_lock);
+                       goto noplus;
+               }
                nfsmout_if(error);
-               nfsmout_if(error);
 
-               /* loop thru the dir entries, doctoring them to 4bsd form */
-               while (more_dirs && bigenough) {
-                       nfsm_chain_get_64(error, &nmrep, fileno);
-                       nfsm_chain_get_32(error, &nmrep, len);
+               if (rdirplus)
+                       microuptime(&now);
+
+               /* loop through the entries packing them into the buffer */
+               while (more_entries) {
+                       if (nfsvers == NFS_VER3)
+                               nfsm_chain_get_64(error, &nmrep, fileno);
+                       else
+                               nfsm_chain_get_32(error, &nmrep, fileno);
+                       nfsm_chain_get_32(error, &nmrep, namlen);
                        nfsmout_if(error);
-                       /* Note: v3 supports longer names, but struct dirent doesn't */
-                       /* so we just truncate the names to fit */
-                       if (len <= 0) {
+                       /* just truncate names that don't fit in direntry.d_name */
+                       if (namlen <= 0) {
                                error = EBADRPC;
                                goto nfsmout;
                        }
-                       if (len > MAXNAMLEN) {
-                               skiplen = len - MAXNAMLEN;
-                               len = MAXNAMLEN;
+                       if (namlen > (sizeof(dp->d_name)-1)) {
+                               skiplen = namlen - sizeof(dp->d_name) + 1;
+                               namlen = sizeof(dp->d_name) - 1;
                        } else {
                                skiplen = 0;
                        }
-                       tlen = nfsm_rndup(len);
-                       if (tlen == len)
-                               tlen += 4;      /* To ensure null termination */
-                       left = DIRBLKSIZ - blksiz;
-                       if ((tlen + DIRHDSIZ) > left) {
-                               dp->d_reclen += left;
-                               uio_iov_base_add(uiop, left);
-                               uio_iov_len_add(uiop, -left);
-                               uiop->uio_offset += left;
-                               uio_uio_resid_add(uiop, -left);
-                               blksiz = 0;
-                       }
-                       if ((tlen + DIRHDSIZ) > uio_uio_resid(uiop))
-                               bigenough = 0;
-                       if (bigenough) {
-                               // LP64todo - fix this!
-                               dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop));
-                               dp->d_fileno = (int)fileno;
-                               dp->d_namlen = len;
-                               dp->d_reclen = tlen + DIRHDSIZ;
-                               dp->d_type = DT_UNKNOWN;
-                               blksiz += dp->d_reclen;
-                               if (blksiz == DIRBLKSIZ)
-                                       blksiz = 0;
-                               uiop->uio_offset += DIRHDSIZ;
-#if LP64KERN
-                               uio_uio_resid_add(uiop, -((int64_t)DIRHDSIZ));
-                               uio_iov_len_add(uiop, -((int64_t)DIRHDSIZ));
-#else
-                               uio_uio_resid_add(uiop, -((int)DIRHDSIZ));
-                               uio_iov_len_add(uiop, -((int)DIRHDSIZ));
-#endif
-                               uio_iov_base_add(uiop, DIRHDSIZ);
-                               // LP64todo - fix this!
-                               cnp->cn_nameptr = CAST_DOWN(caddr_t, uio_iov_base(uiop));
-                               cnp->cn_namelen = len;
-                               error = nfsm_chain_get_uio(&nmrep, len, uiop);
+                       /* guess that fh size will be same as parent */
+                       fhlen = rdirplus ? (1 + dnp->n_fhsize) : 0;
+                       xlen = rdirplus ? (fhlen + sizeof(time_t)) : 0;
+                       attrlen = rdirplus ? sizeof(struct nfs_vattr) : 0;
+                       reclen = NFS_DIRENTRY_LEN(namlen + xlen);
+                       space_needed = reclen + attrlen;
+                       space_free = nfs_dir_buf_freespace(bp, rdirplus);
+                       if (space_needed > space_free) {
+                               /*
+                                * We still have entries to pack, but we've
+                                * run out of room in the current buffer.
+                                * So we need to move to the next buffer.
+                                * The block# for the next buffer is the
+                                * last cookie in the current buffer.
+                                */
+nextbuffer:
+                               ndbhp->ndbh_flags |= NDB_FULL;
+                               nfs_buf_release(bp, 0);
+                               bp_dropped = 1;
+                               bp = NULL;
+                               error = nfs_buf_get(dnp, lastcookie, NFS_DIRBLKSIZ, vfs_context_thread(ctx), NBLK_READ, &bp);
                                nfsmout_if(error);
-                               cp = CAST_DOWN(caddr_t, uio_iov_base(uiop));
-                               tlen -= len;
-                               *cp = '\0';
-                               uio_iov_base_add(uiop, tlen);
-                               uio_iov_len_add(uiop, -tlen);
-                               uiop->uio_offset += tlen;
-                               uio_uio_resid_add(uiop, -tlen);
-                               if (skiplen)
-                                       nfsm_chain_adv(error, &nmrep,
-                                               nfsm_rndup(len + skiplen) - nfsm_rndup(len));
-                       } else {
-                               nfsm_chain_adv(error, &nmrep, nfsm_rndup(len + skiplen));
+                               /* initialize buffer */
+                               ndbhp = (struct nfs_dir_buf_header*)bp->nb_data;
+                               ndbhp->ndbh_flags = 0;
+                               ndbhp->ndbh_count = 0;
+                               ndbhp->ndbh_entry_end = sizeof(*ndbhp);
+                               ndbhp->ndbh_ncgen = dnp->n_ncgen;
+                               space_free = nfs_dir_buf_freespace(bp, rdirplus);
+                               dp = NFS_DIR_BUF_FIRST_DIRENTRY(bp);
+                               /* increment with every buffer read */
+                               OSAddAtomic64(1, &nfsstats.readdir_bios);
                        }
-                       if (bigenough) {
-                               nfsm_chain_get_32(error, &nmrep, cookie.nfsuquad[0]);
-                               nfsm_chain_get_32(error, &nmrep, cookie.nfsuquad[1]);
-                       } else
-                               nfsm_chain_adv(error, &nmrep, 2 * NFSX_UNSIGNED);
-
-                       nfsm_chain_get_32(error, &nmrep, attrflag);
+                       nmrepsave = nmrep;
+                       dp->d_fileno = fileno;
+                       dp->d_namlen = namlen;
+                       dp->d_reclen = reclen;
+                       dp->d_type = DT_UNKNOWN;
+                       nfsm_chain_get_opaque(error, &nmrep, namlen, dp->d_name);
+                       nfsmout_if(error);
+                       dp->d_name[namlen] = '\0';
+                       if (skiplen)
+                               nfsm_chain_adv(error, &nmrep,
+                                       nfsm_rndup(namlen + skiplen) - nfsm_rndup(namlen));
+                       if (nfsvers == NFS_VER3)
+                               nfsm_chain_get_64(error, &nmrep, cookie);
+                       else
+                               nfsm_chain_get_32(error, &nmrep, cookie);
                        nfsmout_if(error);
-                       if (attrflag) {
-                           /* grab attributes */
-                           error = nfs_parsefattr(&nmrep, NFS_VER3, &nvattr);
-                           nfsmout_if(error);
-                           dp->d_type = IFTODT(VTTOIF(nvattr.nva_type));
-                           /* check for file handle */
-                           nfsm_chain_get_32(error, &nmrep, doit);
-                           nfsmout_if(error);
-                           if (doit) {
-                               nfsm_chain_get_fh_ptr(error, &nmrep, NFS_VER3, fhp, fhsize);
+                       dp->d_seekoff = cookie;
+                       if (!bigcookies && (cookie >> 32) && (nmp == NFSTONMP(dnp))) {
+                               /* we've got a big cookie, make sure flag is set */
+                               lck_mtx_lock(&nmp->nm_lock);
+                               nmp->nm_state |= NFSSTA_BIGCOOKIES;
+                               lck_mtx_unlock(&nmp->nm_lock);
+                               bigcookies = 1;
+                       }
+                       if (rdirplus) {
+                               nvattrp = NFS_DIR_BUF_NVATTR(bp, ndbhp->ndbh_count);
+                               /* check for attributes */
+                               nfsm_chain_get_32(error, &nmrep, attrflag);
                                nfsmout_if(error);
-                               if (NFS_CMPFH(dnp, fhp, fhsize)) {
-                                   error = vnode_ref(NFSTOV(dnp));
-                                   if (error) {
-                                       doit = 0;
-                                   } else {
-                                       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-                                           error = lockerror;
-                                       if (error) {
-                                           vnode_rele(NFSTOV(dnp));
-                                           goto nfsmout;
+                               if (attrflag) {
+                                       /* grab attributes */
+                                       error = nfs_parsefattr(&nmrep, NFS_VER3, nvattrp);
+                                       nfsmout_if(error);
+                                       dp->d_type = IFTODT(VTTOIF(nvattrp->nva_type));
+                                       /* fileid is already in d_fileno, so stash xid in attrs */
+                                       nvattrp->nva_fileid = savedxid;
+                               } else {
+                                       /* mark the attributes invalid */
+                                       bzero(nvattrp, sizeof(struct nfs_vattr));
+                               }
+                               /* check for file handle */
+                               nfsm_chain_get_32(error, &nmrep, fhflag);
+                               nfsmout_if(error);
+                               if (fhflag) {
+                                       nfsm_chain_get_fh(error, &nmrep, NFS_VER3, &fh);
+                                       nfsmout_if(error);
+                                       fhlen = fh.fh_len + 1;
+                                       xlen = fhlen + sizeof(time_t);
+                                       reclen = NFS_DIRENTRY_LEN(namlen + xlen);
+                                       space_needed = reclen + attrlen;
+                                       if (space_needed > space_free) {
+                                               /* didn't actually have the room... move on to next buffer */
+                                               nmrep = nmrepsave;
+                                               goto nextbuffer;
                                        }
-                                       newvp = NFSTOV(dnp);
-                                       np = dnp;
-                                   }
-                               } else if (!bigenough ||
-                                       (cnp->cn_namelen == 2 &&
-                                        cnp->cn_nameptr[1] == '.' &&
-                                        cnp->cn_nameptr[0] == '.')) {
-                                   /*
-                                    * XXXmacko I don't think this ".." thing is a problem anymore.
-                                    * don't doit if we can't guarantee
-                                    * that this entry is NOT ".." because
-                                    * we would have to drop the lock on
-                                    * the directory before getting the
-                                    * lock on the ".." vnode... and we
-                                    * don't want to drop the dvp lock in
-                                    * the middle of a readdirplus.
-                                    */
-                                   doit = 0;
+                                       /* pack the file handle into the record */
+                                       dp->d_name[dp->d_namlen+1] = fh.fh_len;
+                                       bcopy(fh.fh_data, &dp->d_name[dp->d_namlen+2], fh.fh_len);
                                } else {
-                                   cnp->cn_hash = 0;
-
-                                   error = nfs_nget(NFSTOMP(dnp), dnp, cnp,
-                                               fhp, fhsize, &nvattr, &xid, NG_MAKEENTRY, &np);
-                                   if (error)
-                                       doit = 0;
-                                   else
-                                       newvp = NFSTOV(np);
+                                       /* mark the file handle invalid */
+                                       fh.fh_len = 0;
+                                       fhlen = fh.fh_len + 1;
+                                       xlen = fhlen + sizeof(time_t);
+                                       reclen = NFS_DIRENTRY_LEN(namlen + xlen);
+                                       bzero(&dp->d_name[dp->d_namlen+1], fhlen);
                                }
-                           }
-                           /* update attributes if not already updated */
-                           if (doit && bigenough && (np->n_xid <= savexid)) {
-                               xid = savexid;
-                               nfs_loadattrcache(np, &nvattr, &xid, 0);
-                               /* any error can be ignored */
-                           }
-                       } else {
-                           /* Just skip over the file handle */
-                           nfsm_chain_get_32(error, &nmrep, fhsize);
-                           nfsm_chain_adv(error, &nmrep, nfsm_rndup(fhsize));
-                       }
-                       if (newvp != NULLVP) {
-                           nfs_unlock(np);
-                           if (newvp == NFSTOV(dnp))
-                               vnode_rele(newvp);
-                           else
-                               vnode_put(newvp);
-                           newvp = NULLVP;
+                               *(time_t*)(&dp->d_name[dp->d_namlen+1+fhlen]) = now.tv_sec;
+                               dp->d_reclen = reclen;
                        }
-                       nfsm_chain_get_32(error, &nmrep, more_dirs);
+                       padstart = dp->d_name + dp->d_namlen + 1 + xlen;
+                       ndbhp->ndbh_count++;
+                       lastcookie = cookie;
+                       /* advance to next direntry in buffer */
+                       dp = NFS_DIRENTRY_NEXT(dp);
+                       ndbhp->ndbh_entry_end = (char*)dp - bp->nb_data;
+                       /* zero out the pad bytes */
+                       padlen = (char*)dp - padstart;
+                       if (padlen > 0)
+                               bzero(padstart, padlen);
+                       /* check for more entries */
+                       nfsm_chain_get_32(error, &nmrep, more_entries);
                        nfsmout_if(error);
                }
-               /*
-                * If at end of rpc data, get the eof boolean
-                */
-               if (!more_dirs) {
-                       nfsm_chain_get_32(error, &nmrep, eof);
-                       if (!error)
-                               more_dirs = (eof == 0);
+               /* Finally, get the eof boolean */
+               nfsm_chain_get_32(error, &nmrep, eof);
+               nfsmout_if(error);
+               if (eof) {
+                       ndbhp->ndbh_flags |= (NDB_FULL|NDB_EOF);
+                       nfs_node_lock_force(dnp);
+                       dnp->n_eofcookie = lastcookie;
+                       nfs_node_unlock(dnp);
+               } else {
+                       more_entries = 1;
+               }
+               if (bp_dropped) {
+                       nfs_buf_release(bp, 0);
+                       bp = NULL;
+                       break;
                }
-               if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_SHARED)))
+               if ((lockerror = nfs_node_lock(dnp)))
                        error = lockerror;
                nfsmout_if(error);
                nfsm_chain_cleanup(&nmrep);
                nfsm_chain_null(&nmreq);
        }
-       if (!lockerror) {
-               nfs_unlock(dnp);
-               lockerror = ENOENT;
-       }
-       /*
-        * Fill last record, iff any, out to a multiple of DIRBLKSIZ
-        * by increasing d_reclen for the last record.
-        */
-       if (blksiz > 0) {
-               left = DIRBLKSIZ - blksiz;
-               dp->d_reclen += left;
-               uio_iov_base_add(uiop, left);
-               uio_iov_len_add(uiop, -left);
-               uiop->uio_offset += left;
-               uio_uio_resid_add(uiop, -left);
-       }
-
-       if ((lockerror = nfs_lock(dnp, NFS_NODE_LOCK_EXCLUSIVE)))
-               error = lockerror;
-       nfsmout_if(error);
-
-       /*
-        * We are now either at the end of the directory or have filled the
-        * block.
-        */
-       if (bigenough)
-               dnp->n_direofoffset = uiop->uio_offset;
-       else {
-               if (uio_uio_resid(uiop) > 0)
-                       printf("EEK! readdirplus_rpc resid > 0\n");
-               cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
-               if (cookiep)
-                       *cookiep = cookie;
-       }
-
 nfsmout:
+       if (bp_dropped && bp)
+               nfs_buf_release(bp, 0);
        if (!lockerror)
-               nfs_unlock(dnp);
+               nfs_node_unlock(dnp);
        nfsm_chain_cleanup(&nmreq);
        nfsm_chain_cleanup(&nmrep);
-       return (error);
+       return (bp_dropped ? NFSERR_DIRBUFDROPPED : error);
 }
 
 /*
@@ -4595,7 +5693,7 @@ nfsmout:
 /* starting from zero isn't silly enough */
 static uint32_t nfs_sillyrename_number = 0x20051025;
 
-static int
+int
 nfs_sillyrename(
        nfsnode_t dnp,
        nfsnode_t np,
@@ -4613,7 +5711,7 @@ nfs_sillyrename(
        if (!nmp)
                return (ENXIO);
 
-       cache_purge(NFSTOV(np));
+       nfs_name_cache_purge(dnp, np, cnp, ctx);
 
        MALLOC_ZONE(nsp, struct nfs_sillyrename *,
                        sizeof (struct nfs_sillyrename), M_NFSREQ, M_WAITOK);
@@ -4629,7 +5727,7 @@ nfs_sillyrename(
 
        /* Fudge together a funny name */
        pid = vfs_context_pid(ctx);
-       num = OSAddAtomic(1, (SInt32*)&nfs_sillyrename_number);
+       num = OSAddAtomic(1, &nfs_sillyrename_number);
        nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
                                NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
        if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
@@ -4637,7 +5735,7 @@ nfs_sillyrename(
 
        /* Try lookitups until we get one that isn't there */
        while (nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, NULL) == 0) {
-               num = OSAddAtomic(1, (SInt32*)&nfs_sillyrename_number);
+               num = OSAddAtomic(1, &nfs_sillyrename_number);
                nsp->nsr_namlen = snprintf(nsp->nsr_name, sizeof(nsp->nsr_name),
                                        NFS_SILLYNAME_FORMAT, num, (pid & 0xffff));
                if (nsp->nsr_namlen >= (int)sizeof(nsp->nsr_name))
@@ -4647,15 +5745,25 @@ nfs_sillyrename(
        /* now, do the rename */
        error = nmp->nm_funcs->nf_rename_rpc(dnp, cnp->cn_nameptr, cnp->cn_namelen,
                                        dnp, nsp->nsr_name, nsp->nsr_namlen, ctx);
-       if (!error && (dnp->n_flag & NNEGNCENTRIES)) {
-               dnp->n_flag &= ~NNEGNCENTRIES;
-               cache_purge_negatives(NFSTOV(dnp));
+
+       /* Kludge: Map ENOENT => 0 assuming that it is a reply to a retry. */
+       if (error == ENOENT)
+               error = 0;
+       if (!error) {
+               nfs_node_lock_force(dnp);
+               if (dnp->n_flag & NNEGNCENTRIES) {
+                       dnp->n_flag &= ~NNEGNCENTRIES;
+                       cache_purge_negatives(NFSTOV(dnp));
+               }
+               nfs_node_unlock(dnp);
        }
        FSDBG(267, dnp, np, num, error);
        if (error)
                goto bad;
        error = nfs_lookitup(dnp, nsp->nsr_name, nsp->nsr_namlen, ctx, &np);
+       nfs_node_lock_force(np);
        np->n_sillyrename = nsp;
+       nfs_node_unlock(np);
        return (0);
 bad:
        vnode_rele(NFSTOV(dnp));
@@ -4688,11 +5796,11 @@ nfs3_lookup_rpc_async(
        nfsm_chain_build_alloc_init(error, &nmreq,
                NFSX_FH(nfsvers) + NFSX_UNSIGNED + nfsm_rndup(namelen));
        nfsm_chain_add_fh(error, &nmreq, nfsvers, dnp->n_fhp, dnp->n_fhsize);
-       nfsm_chain_add_string(error, &nmreq, name, namelen);
+       nfsm_chain_add_name(error, &nmreq, name, namelen, nmp);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
        error = nfs_request_async(dnp, NULL, &nmreq, NFSPROC_LOOKUP,
-                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, reqp);
+                       vfs_context_thread(ctx), vfs_context_ucred(ctx), NULL, 0, NULL, reqp);
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
        return (error);
@@ -4701,13 +5809,15 @@ nfsmout:
 int
 nfs3_lookup_rpc_async_finish(
        nfsnode_t dnp,
+       __unused char *name,
+       __unused int namelen,
        vfs_context_t ctx,
        struct nfsreq *req,
        u_int64_t *xidp,
        fhandle_t *fhp,
        struct nfs_vattr *nvap)
 {
-       int error = 0, status, nfsvers, attrflag;
+       int error = 0, lockerror = ENOENT, status, nfsvers, attrflag;
        u_int64_t xid;
        struct nfsmount *nmp;
        struct nfsm_chain nmrep;
@@ -4719,6 +5829,8 @@ nfs3_lookup_rpc_async_finish(
 
        error = nfs_request_async_finish(req, &nmrep, xidp, &status);
 
+       if ((lockerror = nfs_node_lock(dnp)))
+               error = lockerror;
        xid = *xidp;
        if (error || status) {
                if (nfsvers == NFS_VER3)
@@ -4738,11 +5850,13 @@ nfs3_lookup_rpc_async_finish(
                nfsm_chain_postop_attr_get(error, &nmrep, attrflag, nvap);
                nfsm_chain_postop_attr_update(error, &nmrep, dnp, &xid);
                if (!error && !attrflag)
-                       error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, ctx, nvap, xidp);
+                       error = nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp);
        } else {
                error = nfs_parsefattr(&nmrep, nfsvers, nvap);
        }
 nfsmout:
+       if (!lockerror)
+               nfs_node_unlock(dnp);
        nfsm_chain_cleanup(&nmrep);
        return (error);
 }
@@ -4776,9 +5890,11 @@ nfs_lookitup(
                return (ENXIO);
 
        if (NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXNAME) &&
-           (namelen > (long)nmp->nm_fsattr.nfsa_maxname))
+           (namelen > (int)nmp->nm_fsattr.nfsa_maxname))
                return (ENAMETOOLONG);
 
+       NVATTR_INIT(&nvattr);
+
        /* check for lookup of "." */
        if ((name[0] == '.') && (namelen == 1)) {
                /* skip lookup, we know who we are */
@@ -4789,7 +5905,7 @@ nfs_lookitup(
 
        error = nmp->nm_funcs->nf_lookup_rpc_async(dnp, name, namelen, ctx, &req);
        nfsmout_if(error);
-       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, ctx, req, &xid, &fh, &nvattr);
+       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, name, namelen, ctx, req, &xid, &fh, &nvattr);
        nfsmout_if(!npp || error);
 
        if (*npp) {
@@ -4811,12 +5927,16 @@ nfs_lookitup(
                }
                bcopy(fh.fh_data, np->n_fhp, fh.fh_len);
                np->n_fhsize = fh.fh_len;
+               nfs_node_lock_force(np);
                error = nfs_loadattrcache(np, &nvattr, &xid, 0);
+               nfs_node_unlock(np);
                nfsmout_if(error);
                newnp = np;
        } else if (NFS_CMPFH(dnp, fh.fh_data, fh.fh_len)) {
+               nfs_node_lock_force(dnp);
                if (dnp->n_xid <= xid)
                        error = nfs_loadattrcache(dnp, &nvattr, &xid, 0);
+               nfs_node_unlock(dnp);
                nfsmout_if(error);
                newnp = dnp;
        } else {
@@ -4825,7 +5945,7 @@ nfs_lookitup(
                cnp->cn_nameptr = name;
                cnp->cn_namelen = namelen;
                error = nfs_nget(NFSTOMP(dnp), dnp, cnp, fh.fh_data, fh.fh_len,
-                           &nvattr, &xid, NG_MAKEENTRY, &np);
+                           &nvattr, &xid, rq.r_auth, NG_MAKEENTRY, &np);
                nfsmout_if(error);
                newnp = np;
        }
@@ -4833,6 +5953,7 @@ nfs_lookitup(
 nfsmout:
        if (npp && !*npp && !error)
                *npp = newnp;
+       NVATTR_CLEANUP(&nvattr);
        return (error);
 }
 
@@ -4841,15 +5962,18 @@ nfsmout:
  * performing async lookups.
  */
 void
-nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen)
+nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, int namelen, vfs_context_t ctx)
 {
        int error, du_namelen;
        vnode_t du_vp;
+       struct nfsmount *nmp = NFSTONMP(dnp);
 
        /* check for ._ file in name cache */
        dulp->du_flags = 0;
        bzero(&dulp->du_cn, sizeof(dulp->du_cn));
        du_namelen = namelen + 2;
+       if (!nmp || NMFLAG(nmp, NONEGNAMECACHE))
+               return;
        if ((namelen >= 2) && (name[0] == '.') && (name[1] == '_'))
                return;
        if (du_namelen >= (int)sizeof(dulp->du_smallname))
@@ -4861,14 +5985,27 @@ nfs_dulookup_init(struct nfs_dulookup *dulp, nfsnode_t dnp, const char *name, in
        dulp->du_cn.cn_namelen = du_namelen;
        snprintf(dulp->du_cn.cn_nameptr, du_namelen + 1, "._%s", name);
        dulp->du_cn.cn_nameptr[du_namelen] = '\0';
+       dulp->du_cn.cn_nameiop = LOOKUP;
+       dulp->du_cn.cn_flags = MAKEENTRY;
 
        error = cache_lookup(NFSTOV(dnp), &du_vp, &dulp->du_cn);
-       if (error == -1)
+       if (error == -1) {
                vnode_put(du_vp);
-       else if (!error)
-               dulp->du_flags |= NFS_DULOOKUP_DOIT;
-       else if (dulp->du_cn.cn_nameptr != dulp->du_smallname)
-               FREE(dulp->du_cn.cn_nameptr, M_TEMP);
+       } else if (!error) {
+               nmp = NFSTONMP(dnp);
+               if (nmp && (nmp->nm_vers > NFS_VER2) && NMFLAG(nmp, RDIRPLUS)) {
+                       /* if rdirplus, try dir buf cache lookup */
+                       nfsnode_t du_np = NULL;
+                       if (!nfs_dir_buf_cache_lookup(dnp, &du_np, &dulp->du_cn, ctx, 0) && du_np) {
+                               /* dir buf cache hit */
+                               du_vp = NFSTOV(du_np);
+                               vnode_put(du_vp);
+                               error = -1;
+                       }
+               }
+               if (!error)
+                       dulp->du_flags |= NFS_DULOOKUP_DOIT;
+       }
 }
 
 /*
@@ -4880,7 +6017,7 @@ nfs_dulookup_start(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
        struct nfsmount *nmp = NFSTONMP(dnp);
        struct nfsreq *req = &dulp->du_req;
 
-       if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT))
+       if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_DOIT) || (dulp->du_flags & NFS_DULOOKUP_INPROG))
                return;
        if (!nmp->nm_funcs->nf_lookup_rpc_async(dnp, dulp->du_cn.cn_nameptr,
                        dulp->du_cn.cn_namelen, ctx, &req))
@@ -4903,20 +6040,25 @@ nfs_dulookup_finish(struct nfs_dulookup *dulp, nfsnode_t dnp, vfs_context_t ctx)
        if (!nmp || !(dulp->du_flags & NFS_DULOOKUP_INPROG))
                goto out;
 
-       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, ctx, &dulp->du_req, &xid, &fh, &nvattr);
+       NVATTR_INIT(&nvattr);
+       error = nmp->nm_funcs->nf_lookup_rpc_async_finish(dnp, dulp->du_cn.cn_nameptr,
+                       dulp->du_cn.cn_namelen, ctx, &dulp->du_req, &xid, &fh, &nvattr);
        dulp->du_flags &= ~NFS_DULOOKUP_INPROG;
        if (error == ENOENT) {
                /* add a negative entry in the name cache */
+               nfs_node_lock_force(dnp);
                cache_enter(NFSTOV(dnp), NULL, &dulp->du_cn);
                dnp->n_flag |= NNEGNCENTRIES;
+               nfs_node_unlock(dnp);
        } else if (!error) {
                error = nfs_nget(NFSTOMP(dnp), dnp, &dulp->du_cn, fh.fh_data, fh.fh_len,
-                           &nvattr, &xid, NG_MAKEENTRY, &du_np);
+                           &nvattr, &xid, dulp->du_req.r_auth, NG_MAKEENTRY, &du_np);
                if (!error) {
-                       nfs_unlock(du_np);
+                       nfs_node_unlock(du_np);
                        vnode_put(NFSTOV(du_np));
                }
        }
+       NVATTR_CLEANUP(&nvattr);
 out:
        if (dulp->du_flags & NFS_DULOOKUP_INPROG)
                nfs_request_async_cancel(&dulp->du_req);
@@ -4931,14 +6073,15 @@ out:
 int
 nfs3_commit_rpc(
        nfsnode_t np,
-       u_int64_t offset,
-       u_int64_t count,
-       kauth_cred_t cred)
+       uint64_t offset,
+       uint64_t count,
+       kauth_cred_t cred,
+       uint64_t wverf)
 {
        struct nfsmount *nmp;
        int error = 0, lockerror, status, wccpostattr = 0, nfsvers;
        struct timespec premtime = { 0, 0 };
-       u_int64_t xid, wverf;
+       u_int64_t xid, newwverf;
        uint32_t count32;
        struct nfsm_chain nmreq, nmrep;
 
@@ -4965,22 +6108,22 @@ nfs3_commit_rpc(
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
        error = nfs_request2(np, NULL, &nmreq, NFSPROC_COMMIT,
-                       current_thread(), cred, 0, &nmrep, &xid, &status);
-       if ((lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+                       current_thread(), cred, NULL, 0, &nmrep, &xid, &status);
+       if ((lockerror = nfs_node_lock(np)))
                error = lockerror;
        /* can we do anything useful with the wcc info? */
        nfsm_chain_get_wcc_data(error, &nmrep, np, &premtime, &wccpostattr, &xid);
        if (!lockerror)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        if (!error)
                error = status;
-       nfsm_chain_get_64(error, &nmrep, wverf);
+       nfsm_chain_get_64(error, &nmrep, newwverf);
        nfsmout_if(error);
        lck_mtx_lock(&nmp->nm_lock);
-       if (nmp->nm_verf != wverf) {
-               nmp->nm_verf = wverf;
+       if (nmp->nm_verf != newwverf)
+               nmp->nm_verf = newwverf;
+       if (wverf != newwverf)
                error = NFSERR_STALEWRITEVERF;
-       }
        lck_mtx_unlock(&nmp->nm_lock);
 nfsmout:
        nfsm_chain_cleanup(&nmreq);
@@ -4989,7 +6132,7 @@ nfsmout:
 }
 
 
-static int
+int
 nfs_vnop_blockmap(
        __unused struct vnop_blockmap_args /* {
                struct vnodeop_desc *a_desc;
@@ -5005,29 +6148,12 @@ nfs_vnop_blockmap(
        return (ENOTSUP);
 }
 
-/*
- * Mmap a file
- *
- * NB Currently unsupported.
- */
-/*ARGSUSED*/
-static int
-nfs_vnop_mmap(
-       __unused struct vnop_mmap_args /* {
-               struct vnodeop_desc *a_desc;
-               vnode_t a_vp;
-               int a_fflags;
-               vfs_context_t a_context;
-       } */ *ap)
-{
-       return (EINVAL);
-}
 
 /*
  * fsync vnode op. Just call nfs_flush().
  */
 /* ARGSUSED */
-static int
+int
 nfs_vnop_fsync(
        struct vnop_fsync_args /* {
                struct vnodeop_desc *a_desc;
@@ -5067,13 +6193,12 @@ nfs3_pathconf_rpc(
        nfsm_chain_add_fh(error, &nmreq, nfsvers, np->n_fhp, np->n_fhsize);
        nfsm_chain_build_done(error, &nmreq);
        nfsmout_if(error);
-       error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx,
-                       &nmrep, &xid, &status);
-       if ((lockerror = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       error = nfs_request(np, NULL, &nmreq, NFSPROC_PATHCONF, ctx, NULL, &nmrep, &xid, &status);
+       if ((lockerror = nfs_node_lock(np)))
                error = lockerror;
        nfsm_chain_postop_attr_update(error, &nmrep, np, &xid);
        if (!lockerror)
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        if (!error)
                error = status;
        nfsm_chain_get_32(error, &nmrep, nfsap->nfsa_maxlink);
@@ -5128,13 +6253,13 @@ nfs3_pathconf_cache(struct nfsmount *nmp, struct nfs_fsattr *nfsap)
  * for V2.
  */
 /* ARGSUSED */
-static int
+int
 nfs_vnop_pathconf(
        struct vnop_pathconf_args /* {
                struct vnodeop_desc *a_desc;
                vnode_t a_vp;
                int a_name;
-               register_t *a_retval;
+               int32_t *a_retval;
                vfs_context_t a_context;
        } */ *ap)
 {
@@ -5164,6 +6289,12 @@ nfs_vnop_pathconf(
                        return (0);
                }
                break;
+       case _PC_XATTR_SIZE_BITS:
+               /* Do we support xattrs natively? */
+               if (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NAMED_ATTR)
+                       break;  /* Yes */
+               /* No... so just return an error */
+               /* FALLTHROUGH */
        default:
                /* don't bother contacting the server if we know the answer */
                return (EINVAL);
@@ -5227,35 +6358,36 @@ nfs_vnop_pathconf(
                break;
        case _PC_CHOWN_RESTRICTED:
                if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CHOWN_RESTRICTED))
-                       *ap->a_retval = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
+                       *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CHOWN_RESTRICTED) ? 200112 /* _POSIX_CHOWN_RESTRICTED */ : 0;
                else
                        error = EINVAL;
                break;
        case _PC_NO_TRUNC:
                if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_NO_TRUNC))
-                       *ap->a_retval = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
+                       *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_NO_TRUNC) ? 200112 /* _POSIX_NO_TRUNC */ : 0;
                else
                        error = EINVAL;
                break;
        case _PC_CASE_SENSITIVE:
                if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_INSENSITIVE))
-                       *ap->a_retval = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1;
+                       *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_INSENSITIVE) ? 0 : 1;
                else
                        error = EINVAL;
                break;
        case _PC_CASE_PRESERVING:
                if (NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_CASE_PRESERVING))
-                       *ap->a_retval = (nmp->nm_fsattr.nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0;
+                       *ap->a_retval = (nfsap->nfsa_flags & NFS_FSFLAG_CASE_PRESERVING) ? 1 : 0;
                else
                        error = EINVAL;
                break;
+       case _PC_XATTR_SIZE_BITS: /* same as file size bits if named attrs supported */
        case _PC_FILESIZEBITS:
-               if (!NFS_BITMAP_ISSET(nmp->nm_fsattr.nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
+               if (!NFS_BITMAP_ISSET(nfsap->nfsa_bitmap, NFS_FATTR_MAXFILESIZE)) {
                        *ap->a_retval = 64;
                        error = 0;
                        break;
                }
-               maxFileSize = nmp->nm_fsattr.nfsa_maxfilesize;
+               maxFileSize = nfsap->nfsa_maxfilesize;
                nbits = 1;
                if (maxFileSize & 0xffffffff00000000ULL) {
                        nbits += 32;
@@ -5294,7 +6426,7 @@ nfs_vnop_pathconf(
 /*
  * Read wrapper for special devices.
  */
-static int
+int
 nfsspec_vnop_read(
        struct vnop_read_args /* {
                struct vnodeop_desc *a_desc;
@@ -5311,20 +6443,20 @@ nfsspec_vnop_read(
        /*
         * Set access flag.
         */
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        np->n_flag |= NACC;
        microtime(&now);
        np->n_atim.tv_sec = now.tv_sec;
        np->n_atim.tv_nsec = now.tv_usec * 1000;
-       nfs_unlock(np);
+       nfs_node_unlock(np);
        return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap));
 }
 
 /*
  * Write wrapper for special devices.
  */
-static int
+int
 nfsspec_vnop_write(
        struct vnop_write_args /* {
                struct vnodeop_desc *a_desc;
@@ -5341,13 +6473,13 @@ nfsspec_vnop_write(
        /*
         * Set update flag.
         */
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        np->n_flag |= NUPD;
        microtime(&now);
        np->n_mtim.tv_sec = now.tv_sec;
        np->n_mtim.tv_nsec = now.tv_usec * 1000;
-       nfs_unlock(np);
+       nfs_node_unlock(np);
        return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap));
 }
 
@@ -5356,7 +6488,7 @@ nfsspec_vnop_write(
  *
  * Update the times on the nfsnode then do device close.
  */
-static int
+int
 nfsspec_vnop_close(
        struct vnop_close_args /* {
                struct vnodeop_desc *a_desc;
@@ -5371,11 +6503,11 @@ nfsspec_vnop_close(
        mount_t mp;
        int error;
 
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        if (np->n_flag & (NACC | NUPD)) {
                np->n_flag |= NCHG;
-               if (!vnode_isinuse(vp, 1) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
+               if (!vnode_isinuse(vp, 0) && (mp = vnode_mount(vp)) && !vfs_isrdonly(mp)) {
                        VATTR_INIT(&vattr);
                        if (np->n_flag & NACC) {
                                vattr.va_access_time = np->n_atim;
@@ -5385,13 +6517,13 @@ nfsspec_vnop_close(
                                vattr.va_modify_time = np->n_mtim;
                                VATTR_SET_ACTIVE(&vattr, va_modify_time);
                        }
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_setattr(vp, &vattr, ap->a_context);
                } else {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                }
        } else {
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        }
        return (VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap));
 }
@@ -5402,7 +6534,7 @@ extern vnop_t **fifo_vnodeop_p;
 /*
  * Read wrapper for fifos.
  */
-static int
+int
 nfsfifo_vnop_read(
        struct vnop_read_args /* {
                struct vnodeop_desc *a_desc;
@@ -5419,20 +6551,20 @@ nfsfifo_vnop_read(
        /*
         * Set access flag.
         */
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        np->n_flag |= NACC;
        microtime(&now);
        np->n_atim.tv_sec = now.tv_sec;
        np->n_atim.tv_nsec = now.tv_usec * 1000;
-       nfs_unlock(np);
+       nfs_node_unlock(np);
        return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_read), ap));
 }
 
 /*
  * Write wrapper for fifos.
  */
-static int
+int
 nfsfifo_vnop_write(
        struct vnop_write_args /* {
                struct vnodeop_desc *a_desc;
@@ -5449,13 +6581,13 @@ nfsfifo_vnop_write(
        /*
         * Set update flag.
         */
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        np->n_flag |= NUPD;
        microtime(&now);
        np->n_mtim.tv_sec = now.tv_sec;
        np->n_mtim.tv_nsec = now.tv_usec * 1000;
-       nfs_unlock(np);
+       nfs_node_unlock(np);
        return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_write), ap));
 }
 
@@ -5464,7 +6596,7 @@ nfsfifo_vnop_write(
  *
  * Update the times on the nfsnode then do fifo close.
  */
-static int
+int
 nfsfifo_vnop_close(
        struct vnop_close_args /* {
                struct vnodeop_desc *a_desc;
@@ -5480,7 +6612,7 @@ nfsfifo_vnop_close(
        mount_t mp;
        int error;
 
-       if ((error = nfs_lock(np, NFS_NODE_LOCK_EXCLUSIVE)))
+       if ((error = nfs_node_lock(np)))
                return (error);
        if (np->n_flag & (NACC | NUPD)) {
                microtime(&now);
@@ -5503,40 +6635,55 @@ nfsfifo_vnop_close(
                                vattr.va_modify_time = np->n_mtim;
                                VATTR_SET_ACTIVE(&vattr, va_modify_time);
                        }
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        vnode_setattr(vp, &vattr, ap->a_context);
                } else {
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                }
        } else {
-               nfs_unlock(np);
+               nfs_node_unlock(np);
        }
        return (VOCALL(fifo_vnodeop_p, VOFFSET(vnop_close), ap));
 }
 #endif /* FIFO */
 
 /*ARGSUSED*/
-static int
+int
 nfs_vnop_ioctl(
-       __unused struct vnop_ioctl_args /* {
+       struct vnop_ioctl_args /* {
                struct vnodeop_desc *a_desc;
                vnode_t a_vp;
-               u_long a_command;
+               u_int32_t a_command;
                caddr_t a_data;
                int a_fflag;
                vfs_context_t a_context;
        } */ *ap)
 {
+       vfs_context_t ctx = ap->a_context;
+       vnode_t vp = ap->a_vp;
+       struct nfsmount *mp = VTONMP(vp);
+       int error = ENOTTY;
 
-       /*
-        * XXX we were once bogusly enoictl() which returned this (ENOTTY).
-        * Probably we should return ENODEV.
-        */
-       return (ENOTTY);
+       if (mp == NULL)
+               return (ENXIO);
+       
+       switch (ap->a_command) {
+
+       case F_FULLFSYNC:
+               if (vnode_vfsisrdonly(vp))
+                       return (EROFS);
+               error = nfs_flush(VTONFS(vp), MNT_WAIT, vfs_context_thread(ctx), 0);
+               break;
+       case NFS_FSCTL_DESTROY_CRED:
+               error = nfs_gss_clnt_ctx_destroy(mp, vfs_context_ucred(ctx));
+               break;
+       }
+
+       return (error);
 }
 
 /*ARGSUSED*/
-static int
+int
 nfs_vnop_select(
        __unused struct vnop_select_args /* {
                struct vnodeop_desc *a_desc;
@@ -5559,7 +6706,7 @@ nfs_vnop_select(
  *
  * No buffer I/O, just RPCs straight into the mapped pages.
  */
-static int
+int
 nfs_vnop_pagein(
        struct vnop_pagein_args /* {
                struct vnodeop_desc *a_desc;
@@ -5585,15 +6732,16 @@ nfs_vnop_pagein(
        off_t txoffset;
        struct nfsmount *nmp;
        int error = 0;
-       vm_offset_t ioaddr;
-       struct uio      auio;
-       struct iovec_32 aiov;
-       struct uio * uio = &auio;
+       vm_offset_t ioaddr, rxaddr;
+       uio_t uio;
+       char uio_buf [ UIO_SIZEOF(1) ];
        int nofreeupl = flags & UPL_NOCOMMIT;
        upl_page_info_t *plinfo;
 #define MAXPAGINGREQS  16      /* max outstanding RPCs for pagein/pageout */
        struct nfsreq *req[MAXPAGINGREQS];
        int nextsend, nextwait;
+       uint32_t stategenid = 0, restart = 0;
+       kern_return_t kret;
 
        FSDBG(322, np, f_offset, size, flags);
        if (pl == (upl_t)NULL)
@@ -5602,7 +6750,7 @@ nfs_vnop_pagein(
        if (size <= 0) {
                printf("nfs_pagein: invalid size %ld", size);
                if (!nofreeupl)
-                       (void) ubc_upl_abort(pl, 0);
+                       (void) ubc_upl_abort_range(pl, pl_offset, size, 0);
                return (EINVAL);
        }
        if (f_offset < 0 || f_offset >= (off_t)np->n_size || (f_offset & PAGE_MASK_64)) {
@@ -5617,14 +6765,8 @@ nfs_vnop_pagein(
        if (!IS_VALID_CRED(cred))
                cred = vfs_context_ucred(ap->a_context);
 
-       auio.uio_offset = f_offset;
-#if 1   /* LP64todo - can't use new segment flags until the drivers are ready */
-       auio.uio_segflg = UIO_SYSSPACE;
-#else
-       auio.uio_segflg = UIO_SYSSPACE32;
-#endif
-       auio.uio_rw = UIO_READ;
-       auio.uio_procp = vfs_context_proc(ap->a_context);
+       uio = uio_createwithbuffer(1, f_offset, UIO_SYSSPACE, UIO_READ,
+               &uio_buf, sizeof(uio_buf));
 
        nmp = VTONMP(vp);
        if (!nmp) {
@@ -5636,14 +6778,25 @@ nfs_vnop_pagein(
        nmrsize = nmp->nm_rsize;
 
        plinfo = ubc_upl_pageinfo(pl);
-       ubc_upl_map(pl, &ioaddr);
+       kret = ubc_upl_map(pl, &ioaddr);
+       if (kret != KERN_SUCCESS)
+               panic("nfs_vnop_pagein: ubc_upl_map() failed with (%d)", kret);
        ioaddr += pl_offset;
+
+tryagain:
+       if (nmp->nm_vers >= NFS_VER4)
+               stategenid = nmp->nm_stategenid;
        txsize = rxsize = size;
        txoffset = f_offset;
+       rxaddr = ioaddr;
 
        bzero(req, sizeof(req));
        nextsend = nextwait = 0;
        do {
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+                       break;
+               }
                /* send requests while we need to and have available slots */
                while ((txsize > 0) && (req[nextsend] == NULL)) {
                        iosize = MIN(nmrsize, txsize);
@@ -5658,49 +6811,70 @@ nfs_vnop_pagein(
                /* wait while we need to and break out if more requests to send */
                while ((rxsize > 0) && req[nextwait]) {
                        iosize = retsize = MIN(nmrsize, rxsize);
-                       aiov.iov_len  = iosize;
-                       aiov.iov_base = (uintptr_t)ioaddr;
-                       auio.uio_iovs.iov32p = &aiov;
-                       auio.uio_iovcnt = 1;
-                       uio_uio_resid_set(&auio, iosize);
-                       FSDBG(322, uio->uio_offset, uio_uio_resid(uio), ioaddr, rxsize);
-#ifdef UPL_DEBUG
-                       upl_ubc_alias_set(pl, current_thread(), 2);
+                       uio_reset(uio, uio_offset(uio), UIO_SYSSPACE, UIO_READ);
+                       uio_addiov(uio, CAST_USER_ADDR_T(rxaddr), iosize);
+                       FSDBG(322, uio_offset(uio), uio_resid(uio), rxaddr, rxsize);
+#if UPL_DEBUG
+                       upl_ubc_alias_set(pl, (uintptr_t) current_thread(), (uintptr_t) 2);
 #endif /* UPL_DEBUG */
-                       OSAddAtomic(1, (SInt32*)&nfsstats.pageins);
+                       OSAddAtomic64(1, &nfsstats.pageins);
                        error = nmp->nm_funcs->nf_read_rpc_async_finish(np, req[nextwait], uio, &retsize, NULL);
                        req[nextwait] = NULL;
                        nextwait = (nextwait + 1) % MAXPAGINGREQS;
+                       if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
+                               lck_mtx_lock(&nmp->nm_lock);
+                               if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
+                                       NP(np, "nfs_vnop_pagein: error %d, initiating recovery", error);
+                                       nfs_need_recover(nmp, error);
+                               }
+                               lck_mtx_unlock(&nmp->nm_lock);
+                               restart++;
+                               goto cancel;
+                       }
                        if (error) {
-                               FSDBG(322, uio->uio_offset, uio_uio_resid(uio), error, -1);
+                               FSDBG(322, uio_offset(uio), uio_resid(uio), error, -1);
                                break;
                        }
                        if (retsize < iosize) {
                                /* Just zero fill the rest of the valid area. */
-                               // LP64todo - fix this
                                int zcnt = iosize - retsize;
-                               bzero((char *)ioaddr + retsize, zcnt);
-                               FSDBG(324, uio->uio_offset, retsize, zcnt, ioaddr);
-                               uio->uio_offset += zcnt;
+                               bzero((char *)rxaddr + retsize, zcnt);
+                               FSDBG(324, uio_offset(uio), retsize, zcnt, rxaddr);
+                               uio_update(uio, zcnt);
                        }
-                       ioaddr += iosize;       
+                       rxaddr += iosize;       
                        rxsize -= iosize;
                        if (txsize)
                                break;
                }
        } while (!error && (txsize || rxsize));
 
-       ubc_upl_unmap(pl);
+       restart = 0;
 
        if (error) {
+cancel:
                /* cancel any outstanding requests */
                while (req[nextwait]) {
                        nfs_request_async_cancel(req[nextwait]);
                        req[nextwait] = NULL;
                        nextwait = (nextwait + 1) % MAXPAGINGREQS;
                }
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+               } else if (restart) {
+                       if (restart <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
+                               if (error == NFSERR_GRACE)
+                                       tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
+                               if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
+                                       goto tryagain;
+                       } else {
+                               NP(np, "nfs_pagein: too many restarts, aborting");
+                       }
+               }
        }
 
+       ubc_upl_unmap(pl);
+
        if (!nofreeupl) {
                if (error)
                        ubc_upl_abort_range(pl, pl_offset, size,
@@ -5722,7 +6896,8 @@ nfs_vnop_pagein(
  * are expected to match the same numbers here. If not, our actions maybe
  * erroneous.
  */
-enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, RETRYWITHSLEEP, SEVER};
+char nfs_pageouterrorhandler(int);
+enum actiontype {NOACTION, DUMP, DUMPANDLOG, RETRY, SEVER};
 #define NFS_ELAST 88
 static u_char errorcount[NFS_ELAST+1]; /* better be zeros when initialized */
 static const char errortooutcome[NFS_ELAST+1] = {
@@ -5826,7 +7001,7 @@ static const char errortooutcome[NFS_ELAST+1] = {
        DUMPANDLOG,             /* EBADMACHO            88      Malformed Macho file */
 };
 
-static char
+char
 nfs_pageouterrorhandler(int error)
 {
        if (error > NFS_ELAST)
@@ -5842,7 +7017,7 @@ nfs_pageouterrorhandler(int error)
  * No buffer I/O, just RPCs straight from the mapped pages.
  * File size changes are not permitted in pageout.
  */
-static int
+int
 nfs_vnop_pageout(
        struct vnop_pageout_args /* {
                struct vnodeop_desc *a_desc;
@@ -5870,13 +7045,15 @@ nfs_vnop_pageout(
        int error = 0, iomode;
        off_t off, txoffset, rxoffset;
        vm_offset_t ioaddr, txaddr, rxaddr;
-       struct uio      auio;
-       struct iovec_32 aiov;
+       uio_t auio;
+       char uio_buf [ UIO_SIZEOF(1) ];
        int nofreeupl = flags & UPL_NOCOMMIT;
        size_t nmwsize, biosize, iosize, pgsize, txsize, rxsize, xsize, remsize;
        struct nfsreq *req[MAXPAGINGREQS];
-       int nextsend, nextwait, wverfset, commit, restart = 0;
+       int nextsend, nextwait, wverfset, commit;
        uint64_t wverf, wverf2;
+       uint32_t stategenid = 0, vrestart = 0, restart = 0, vrestarts = 0, restarts = 0;
+       kern_return_t kret;
 
        FSDBG(323, f_offset, size, pl, pl_offset);
 
@@ -5886,7 +7063,7 @@ nfs_vnop_pageout(
        if (size <= 0) {
                printf("nfs_pageout: invalid size %ld", size);
                if (!nofreeupl)
-                       ubc_upl_abort(pl, 0);
+                       ubc_upl_abort_range(pl, pl_offset, size, 0);
                return (EINVAL);
        }
 
@@ -5898,7 +7075,7 @@ nfs_vnop_pageout(
        biosize = nmp->nm_biosize;
        nmwsize = nmp->nm_wsize;
 
-       nfs_data_lock2(np, NFS_NODE_LOCK_SHARED, 0);
+       nfs_data_lock_noupdate(np, NFS_DATA_LOCK_SHARED);
 
        /*
         * Check to see whether the buffer is incore.
@@ -5916,10 +7093,10 @@ nfs_vnop_pageout(
                        FSDBG(323, off, bp, bp->nb_lflags, bp->nb_flags);
                        if (nfs_buf_acquire(bp, NBAC_NOWAIT, 0, 0)) {
                                lck_mtx_unlock(nfs_buf_mutex);
-                               nfs_data_unlock2(np, 0);
+                               nfs_data_unlock_noupdate(np);
                                /* no panic. just tell vm we are busy */
                                if (!nofreeupl)
-                                       ubc_upl_abort(pl, 0);
+                                       ubc_upl_abort_range(pl, pl_offset, size, 0);
                                return (EBUSY);
                        }
                        if (bp->nb_dirtyend > 0) {
@@ -5949,13 +7126,24 @@ nfs_vnop_pageout(
                                end -= boff;
                                if ((bp->nb_dirtyoff < start) &&
                                    (bp->nb_dirtyend > end)) {
-                                   /* not gonna be able to clip the dirty region */
+                                   /*
+                                    * not gonna be able to clip the dirty region
+                                    *
+                                    * But before returning the bad news, move the
+                                    * buffer to the start of the delwri list and
+                                    * give the list a push to try to flush the
+                                    * buffer out.
+                                    */
                                    FSDBG(323, np, bp, 0xd00deebc, EBUSY);
+                                   nfs_buf_remfree(bp);
+                                   TAILQ_INSERT_HEAD(&nfsbufdelwri, bp, nb_free);
+                                   nfsbufdelwricnt++;
                                    nfs_buf_drop(bp);
+                                   nfs_buf_delwri_push(1);
                                    lck_mtx_unlock(nfs_buf_mutex);
-                                   nfs_data_unlock2(np, 0);
+                                   nfs_data_unlock_noupdate(np);
                                    if (!nofreeupl)
-                                       ubc_upl_abort(pl, 0);
+                                           ubc_upl_abort_range(pl, pl_offset, size, 0);
                                    return (EBUSY);
                                }
                                if ((bp->nb_dirtyoff < start) ||
@@ -5975,13 +7163,13 @@ nfs_vnop_pageout(
                        nfs_buf_remfree(bp);
                        lck_mtx_unlock(nfs_buf_mutex);
                        SET(bp->nb_flags, NB_INVAL);
-                       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+                       nfs_node_lock_force(np);
                        if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
                                CLR(bp->nb_flags, NB_NEEDCOMMIT);
                                np->n_needcommitcnt--;
                                CHECK_NEEDCOMMITCNT(np);
                        }
-                       nfs_unlock(np);
+                       nfs_node_unlock(np);
                        nfs_buf_release(bp, 1);
                } else {
                        lck_mtx_unlock(nfs_buf_mutex);
@@ -5993,28 +7181,30 @@ nfs_vnop_pageout(
        if (!IS_VALID_CRED(cred))
                cred = vfs_context_ucred(ap->a_context);
 
-       nfs_lock(np, NFS_NODE_LOCK_FORCE);
+       nfs_node_lock_force(np);
        if (np->n_flag & NWRITEERR) {
                error = np->n_error;
-               nfs_unlock(np);
-               nfs_data_unlock2(np, 0);
+               nfs_node_unlock(np);
+               nfs_data_unlock_noupdate(np);
                if (!nofreeupl)
                        ubc_upl_abort_range(pl, pl_offset, size,
                                            UPL_ABORT_FREE_ON_EMPTY);
                return (error);
        }
-       nfs_unlock(np);
+       nfs_node_unlock(np);
 
        if (f_offset < 0 || f_offset >= (off_t)np->n_size ||
            f_offset & PAGE_MASK_64 || size & PAGE_MASK_64) {
-               nfs_data_unlock2(np, 0);
+               nfs_data_unlock_noupdate(np);
                if (!nofreeupl)
                        ubc_upl_abort_range(pl, pl_offset, size,
                                            UPL_ABORT_FREE_ON_EMPTY);
                return (EINVAL);
        }
 
-       ubc_upl_map(pl, &ioaddr);
+       kret = ubc_upl_map(pl, &ioaddr);
+       if (kret != KERN_SUCCESS)
+               panic("nfs_vnop_pageout: ubc_upl_map() failed with (%d)", kret);
        ioaddr += pl_offset;
 
        if ((u_quad_t)f_offset + size > np->n_size)
@@ -6037,17 +7227,14 @@ nfs_vnop_pageout(
                bzero((caddr_t)(ioaddr + io), size - io);
                FSDBG(321, np->n_size, f_offset, f_offset + io, size - io);
        }
-       nfs_data_unlock2(np, 0);
+       nfs_data_unlock_noupdate(np);
 
-#if 1   /* LP64todo - can't use new segment flags until the drivers are ready */
-       auio.uio_segflg = UIO_SYSSPACE;
-#else
-       auio.uio_segflg = UIO_SYSSPACE32;
-#endif
-       auio.uio_rw = UIO_WRITE;
-       auio.uio_procp = vfs_context_proc(ap->a_context);
+       auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_WRITE,
+               &uio_buf, sizeof(uio_buf));
 
 tryagain:
+       if (nmp->nm_vers >= NFS_VER4)
+               stategenid = nmp->nm_stategenid;
        wverf = wverf2 = wverfset = 0;
        txsize = rxsize = xsize;
        txoffset = rxoffset = f_offset;
@@ -6057,22 +7244,28 @@ tryagain:
        bzero(req, sizeof(req));
        nextsend = nextwait = 0;
        do {
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+                       break;
+               }
                /* send requests while we need to and have available slots */
                while ((txsize > 0) && (req[nextsend] == NULL)) {
                        iosize = MIN(nmwsize, txsize);
-                       aiov.iov_len = iosize;
-                       aiov.iov_base = (uintptr_t)txaddr;
-                       auio.uio_iovs.iov32p = &aiov;
-                       auio.uio_iovcnt = 1;
-                       auio.uio_offset = txoffset;
-                       uio_uio_resid_set(&auio, iosize);
-                       FSDBG(323, auio.uio_offset, iosize, txaddr, txsize);
-                       OSAddAtomic(1, (SInt32*)&nfsstats.pageouts);
+                       uio_reset(auio, txoffset, UIO_SYSSPACE, UIO_WRITE);
+                       uio_addiov(auio, CAST_USER_ADDR_T(txaddr), iosize);
+                       FSDBG(323, uio_offset(auio), iosize, txaddr, txsize);
+                       OSAddAtomic64(1, &nfsstats.pageouts);
+                       nfs_node_lock_force(np);
+                       np->n_numoutput++;
+                       nfs_node_unlock(np);
                        vnode_startwrite(vp);
                        iomode = NFS_WRITE_UNSTABLE;
-                       if ((error = nmp->nm_funcs->nf_write_rpc_async(np, &auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) {
+                       if ((error = nmp->nm_funcs->nf_write_rpc_async(np, auio, iosize, thd, cred, iomode, NULL, &req[nextsend]))) {
                                req[nextsend] = NULL;
                                vnode_writedone(vp);
+                               nfs_node_lock_force(np);
+                               np->n_numoutput--;
+                               nfs_node_unlock(np);
                                break;
                        }
                        txaddr += iosize;
@@ -6087,6 +7280,19 @@ tryagain:
                        req[nextwait] = NULL;
                        nextwait = (nextwait + 1) % MAXPAGINGREQS;
                        vnode_writedone(vp);
+                       nfs_node_lock_force(np);
+                       np->n_numoutput--;
+                       nfs_node_unlock(np);
+                       if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
+                               lck_mtx_lock(&nmp->nm_lock);
+                               if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
+                                       NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
+                                       nfs_need_recover(nmp, error);
+                               }
+                               lck_mtx_unlock(&nmp->nm_lock);
+                               restart = 1;
+                               goto cancel;
+                       }
                        if (error) {
                                FSDBG(323, rxoffset, rxsize, error, -1);
                                break;
@@ -6096,7 +7302,7 @@ tryagain:
                                wverfset = 1;
                        } else if (wverf != wverf2) {
                                /* verifier changed, so we need to restart all the writes */
-                               restart++;
+                               vrestart = 1;
                                goto cancel;
                        }
                        /* Retain the lowest commitment level returned. */
@@ -6109,21 +7315,28 @@ tryagain:
                        if (remsize > 0) {
                                /* need to try sending the remainder */
                                iosize = remsize;
-                               aiov.iov_len = remsize;
-                               aiov.iov_base = (uintptr_t)rxaddr;
-                               auio.uio_iovs.iov32p = &aiov;
-                               auio.uio_iovcnt = 1;
-                               auio.uio_offset = rxoffset;
-                               uio_uio_resid_set(&auio, remsize);
+                               uio_reset(auio, rxoffset, UIO_SYSSPACE, UIO_WRITE);
+                               uio_addiov(auio, CAST_USER_ADDR_T(rxaddr), remsize);
                                iomode = NFS_WRITE_UNSTABLE;
-                               error = nfs_write_rpc2(np, &auio, thd, cred, &iomode, &wverf2);
+                               error = nfs_write_rpc2(np, auio, thd, cred, &iomode, &wverf2);
+                               if ((nmp->nm_vers >= NFS_VER4) && nfs_mount_state_error_should_restart(error)) {
+                                       NP(np, "nfs_vnop_pageout: restart: error %d", error);
+                                       lck_mtx_lock(&nmp->nm_lock);
+                                       if ((error != NFSERR_GRACE) && (stategenid == nmp->nm_stategenid)) {
+                                               NP(np, "nfs_vnop_pageout: error %d, initiating recovery", error);
+                                               nfs_need_recover(nmp, error);
+                                       }
+                                       lck_mtx_unlock(&nmp->nm_lock);
+                                       restart = 1;
+                                       goto cancel;
+                               }
                                if (error) {
                                        FSDBG(323, rxoffset, rxsize, error, -1);
                                        break;
                                }
                                if (wverf != wverf2) {
                                        /* verifier changed, so we need to restart all the writes */
-                                       restart++;
+                                       vrestart = 1;
                                        goto cancel;
                                }
                                if (iomode < commit)
@@ -6137,12 +7350,12 @@ tryagain:
                }
        } while (!error && (txsize || rxsize));
 
-       restart = 0;
+       vrestart = 0;
 
        if (!error && (commit != NFS_WRITE_FILESYNC)) {
-               error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred);
+               error = nmp->nm_funcs->nf_commit_rpc(np, f_offset, xsize, cred, wverf);
                if (error == NFSERR_STALEWRITEVERF) {
-                       restart++;
+                       vrestart = 1;
                        error = EIO;
                }
        }
@@ -6155,12 +7368,30 @@ cancel:
                        req[nextwait] = NULL;
                        nextwait = (nextwait + 1) % MAXPAGINGREQS;
                        vnode_writedone(vp);
+                       nfs_node_lock_force(np);
+                       np->n_numoutput--;
+                       nfs_node_unlock(np);
                }
-               if (restart) {
-                       if (restart <= 10)
-                               goto tryagain;
-                       printf("nfs_pageout: too many restarts, aborting.\n");
-                       FSDBG(323, f_offset, xsize, ERESTART, -1);
+               if (np->n_flag & NREVOKE) {
+                       error = EIO;
+               } else {
+                       if (vrestart) {
+                               if (++vrestarts <= 100) /* guard against no progress */
+                                       goto tryagain;
+                               NP(np, "nfs_pageout: too many restarts, aborting");
+                               FSDBG(323, f_offset, xsize, ERESTART, -1);
+                       }
+                       if (restart) {
+                               if (restarts <= nfs_mount_state_max_restarts(nmp)) { /* guard against no progress */
+                                       if (error == NFSERR_GRACE)
+                                               tsleep(&nmp->nm_state, (PZERO-1), "nfsgrace", 2*hz);
+                                       if (!(error = nfs_mount_state_wait_for_recovery(nmp)))
+                                               goto tryagain;
+                               } else {
+                                       NP(np, "nfs_pageout: too many restarts, aborting");
+                                       FSDBG(323, f_offset, xsize, ERESTART, -1);
+                               }
+                       }
                }
        }
 
@@ -6202,21 +7433,16 @@ cancel:
                                        abortflags = UPL_ABORT_DUMP_PAGES|UPL_ABORT_FREE_ON_EMPTY;
                                        if (error <= NFS_ELAST) {
                                                if ((errorcount[error] % 100) == 0)
-                                                       printf("nfs_pageout: unexpected error %d. dumping vm page\n", error);
+                                                       NP(np, "nfs_pageout: unexpected error %d. dumping vm page", error);
                                                errorcount[error]++;
                                        }
                                        break;
                                case RETRY:
                                        abortflags = UPL_ABORT_FREE_ON_EMPTY;
                                        break;
-                               case RETRYWITHSLEEP:
-                                       abortflags = UPL_ABORT_FREE_ON_EMPTY;
-                                       /* pri unused. PSOCK for placeholder. */
-                                       tsleep(&lbolt, PSOCK, "nfspageout", 0);
-                                       break;
                                case SEVER: /* not implemented */
                                default:
-                                       printf("nfs_pageout: action %d not expected\n", action);
+                                       NP(np, "nfs_pageout: action %d not expected", action);
                                        break;
                        }
 
@@ -6233,7 +7459,7 @@ cancel:
 }
 
 /* Blktooff derives file offset given a logical block number */
-static int
+int
 nfs_vnop_blktooff(
        struct vnop_blktooff_args /* {
                struct vnodeop_desc *a_desc;
@@ -6255,7 +7481,7 @@ nfs_vnop_blktooff(
        return (0);
 }
 
-static int
+int
 nfs_vnop_offtoblk(
        struct vnop_offtoblk_args /* {
                struct vnodeop_desc *a_desc;
@@ -6277,3 +7503,84 @@ nfs_vnop_offtoblk(
        return (0);
 }
 
+/*
+ * vnode change monitoring
+ */
+int
+nfs_vnop_monitor(
+       struct vnop_monitor_args /* {
+               struct vnodeop_desc *a_desc;
+               vnode_t a_vp;
+               uint32_t a_events;
+               uint32_t a_flags;
+               void *a_handle;
+               vfs_context_t a_context;
+       } */ *ap)
+{
+       nfsnode_t np = VTONFS(ap->a_vp);
+       struct nfsmount *nmp = VTONMP(ap->a_vp);
+       int error = 0;
+
+       if (!nmp)
+               return (ENXIO);
+
+       /* make sure that the vnode's monitoring status is up to date */
+       lck_mtx_lock(&nmp->nm_lock);
+       if (vnode_ismonitored(ap->a_vp)) {
+               /* This vnode is currently being monitored, make sure we're tracking it. */
+               if (np->n_monlink.le_next == NFSNOLIST) {
+                       LIST_INSERT_HEAD(&nmp->nm_monlist, np, n_monlink);
+                       nfs_mount_sock_thread_wake(nmp);
+               }
+       } else {
+               /* This vnode is no longer being monitored, make sure we're not tracking it. */
+               /* Wait for any in-progress getattr to complete first. */
+               while (np->n_mflag & NMMONSCANINPROG) {
+                       struct timespec ts = { 1, 0 };
+                       np->n_mflag |= NMMONSCANWANT;
+                       msleep(&np->n_mflag, &nmp->nm_lock, PZERO-1, "nfswaitmonscan", &ts);
+               }
+               if (np->n_monlink.le_next != NFSNOLIST) {
+                       LIST_REMOVE(np, n_monlink);
+                       np->n_monlink.le_next = NFSNOLIST;
+               }
+       }
+       lck_mtx_unlock(&nmp->nm_lock);
+
+       return (error);
+}
+
+/*
+ * Send a vnode notification for the given events.
+ */
+void
+nfs_vnode_notify(nfsnode_t np, uint32_t events)
+{
+       struct nfsmount *nmp = NFSTONMP(np);
+       struct nfs_vattr nvattr;
+       struct vnode_attr vattr, *vap = NULL;
+       struct timeval now;
+
+       microuptime(&now);
+       if ((np->n_evtstamp == now.tv_sec) || !nmp) {
+               /* delay sending this notify */
+               np->n_events |= events;
+               return;
+       }
+       events |= np->n_events;
+       np->n_events = 0;
+       np->n_evtstamp = now.tv_sec;
+
+       vfs_get_notify_attributes(&vattr);
+       if (!nfs_getattrcache(np, &nvattr, 0)) {
+               vap = &vattr;
+               VATTR_INIT(vap);
+               VATTR_RETURN(vap, va_fsid, vfs_statfs(nmp->nm_mountp)->f_fsid.val[0]);
+               VATTR_RETURN(vap, va_fileid, nvattr.nva_fileid);
+               VATTR_RETURN(vap, va_mode, nvattr.nva_mode);
+               VATTR_RETURN(vap, va_uid, nvattr.nva_uid);
+               VATTR_RETURN(vap, va_gid, nvattr.nva_gid);
+               VATTR_RETURN(vap, va_nlink, nvattr.nva_nlink);
+       }
+       vnode_notify(NFSTOV(np), events, vap);
+}