fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / nfs / dir.c
index f061e70..7422b73 100644 (file)
 #include <linux/nfs_mount.h>
 #include <linux/pagemap.h>
 #include <linux/smp_lock.h>
+#include <linux/pagevec.h>
 #include <linux/namei.h>
+#include <linux/mount.h>
+#include <linux/vs_tag.h>
+
+#include "nfs4_fs.h"
+#include "delegation.h"
+#include "iostat.h"
 
 #define NFS_PARANOIA 1
 /* #define NFS_DEBUG_VERBOSE 1 */
@@ -38,8 +45,6 @@
 static int nfs_opendir(struct inode *, struct file *);
 static int nfs_readdir(struct file *, void *, filldir_t);
 static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
-static int nfs_cached_lookup(struct inode *, struct dentry *,
-                               struct nfs_fh *, struct nfs_fattr *);
 static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
 static int nfs_mkdir(struct inode *, struct dentry *, int);
 static int nfs_rmdir(struct inode *, struct dentry *);
@@ -49,12 +54,16 @@ static int nfs_link(struct dentry *, struct inode *, struct dentry *);
 static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
 static int nfs_rename(struct inode *, struct dentry *,
                      struct inode *, struct dentry *);
+static int nfs_fsync_dir(struct file *, struct dentry *, int);
+static loff_t nfs_llseek_dir(struct file *, loff_t, int);
 
-struct file_operations nfs_dir_operations = {
+const struct file_operations nfs_dir_operations = {
+       .llseek         = nfs_llseek_dir,
        .read           = generic_read_dir,
        .readdir        = nfs_readdir,
        .open           = nfs_opendir,
        .release        = nfs_release,
+       .fsync          = nfs_fsync_dir,
 };
 
 struct inode_operations nfs_dir_inode_operations = {
@@ -72,6 +81,27 @@ struct inode_operations nfs_dir_inode_operations = {
        .setattr        = nfs_setattr,
 };
 
+#ifdef CONFIG_NFS_V3
+struct inode_operations nfs3_dir_inode_operations = {
+       .create         = nfs_create,
+       .lookup         = nfs_lookup,
+       .link           = nfs_link,
+       .unlink         = nfs_unlink,
+       .symlink        = nfs_symlink,
+       .mkdir          = nfs_mkdir,
+       .rmdir          = nfs_rmdir,
+       .mknod          = nfs_mknod,
+       .rename         = nfs_rename,
+       .permission     = nfs_permission,
+       .getattr        = nfs_getattr,
+       .setattr        = nfs_setattr,
+       .listxattr      = nfs3_listxattr,
+       .getxattr       = nfs3_getxattr,
+       .setxattr       = nfs3_setxattr,
+       .removexattr    = nfs3_removexattr,
+};
+#endif  /* CONFIG_NFS_V3 */
+
 #ifdef CONFIG_NFS_V4
 
 static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
@@ -88,6 +118,9 @@ struct inode_operations nfs4_dir_inode_operations = {
        .permission     = nfs_permission,
        .getattr        = nfs_getattr,
        .setattr        = nfs_setattr,
+       .getxattr       = nfs4_getxattr,
+       .setxattr       = nfs4_setxattr,
+       .listxattr      = nfs4_listxattr,
 };
 
 #endif /* CONFIG_NFS_V4 */
@@ -98,23 +131,26 @@ struct inode_operations nfs4_dir_inode_operations = {
 static int
 nfs_opendir(struct inode *inode, struct file *filp)
 {
-       int res = 0;
+       int res;
+
+       dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
+                       inode->i_sb->s_id, inode->i_ino);
 
        lock_kernel();
        /* Call generic open code in order to cache credentials */
-       if (!res)
-               res = nfs_open(inode, filp);
+       res = nfs_open(inode, filp);
        unlock_kernel();
        return res;
 }
 
-typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
+typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
 typedef struct {
        struct file     *file;
        struct page     *page;
        unsigned long   page_index;
-       u32             *ptr;
-       u64             target;
+       __be32          *ptr;
+       u64             *dir_cookie;
+       loff_t          current_index;
        struct nfs_entry *entry;
        decode_dirent_t decode;
        int             plus;
@@ -137,36 +173,40 @@ static
 int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
 {
        struct file     *file = desc->file;
-       struct inode    *inode = file->f_dentry->d_inode;
+       struct inode    *inode = file->f_path.dentry->d_inode;
        struct rpc_cred *cred = nfs_file_cred(file);
        unsigned long   timestamp;
        int             error;
 
-       dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
+       dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
+                       __FUNCTION__, (long long)desc->entry->cookie,
+                       page->index);
 
  again:
        timestamp = jiffies;
-       error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
+       error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
                                          NFS_SERVER(inode)->dtsize, desc->plus);
        if (error < 0) {
                /* We requested READDIRPLUS, but the server doesn't grok it */
                if (error == -ENOTSUPP && desc->plus) {
                        NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
-                       NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
+                       clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
                        desc->plus = 0;
                        goto again;
                }
                goto error;
        }
        SetPageUptodate(page);
-       NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
+       spin_lock(&inode->i_lock);
+       NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
+       spin_unlock(&inode->i_lock);
        /* Ensure consistent page alignment of the data.
         * Note: assumes we have exclusive access to this mapping either
-        *       throught inode->i_sem or some other mechanism.
+        *       through inode->i_mutex or some other mechanism.
         */
-       if (page->index == 0) {
-               invalidate_inode_pages(inode->i_mapping);
-               NFS_I(inode)->readdir_timestamp = timestamp;
+       if (page->index == 0 && invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1) < 0) {
+               /* Should never happen */
+               nfs_zap_mapping(inode, inode->i_mapping);
        }
        unlock_page(page);
        return 0;
@@ -181,7 +221,7 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
 static inline
 int dir_decode(nfs_readdir_descriptor_t *desc)
 {
-       u32     *p = desc->ptr;
+       __be32  *p = desc->ptr;
        p = desc->decode(p, desc->entry, desc->plus);
        if (IS_ERR(p))
                return PTR_ERR(p);
@@ -200,44 +240,82 @@ void dir_page_release(nfs_readdir_descriptor_t *desc)
 
 /*
  * Given a pointer to a buffer that has already been filled by a call
- * to readdir, find the next entry.
+ * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  *
  * If the end of the buffer has been reached, return -EAGAIN, if not,
  * return the offset within the buffer of the next entry to be
  * read.
  */
 static inline
-int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
+int find_dirent(nfs_readdir_descriptor_t *desc)
 {
        struct nfs_entry *entry = desc->entry;
        int             loop_count = 0,
                        status;
 
        while((status = dir_decode(desc)) == 0) {
-               dfprintk(VFS, "NFS: found cookie %Lu\n", (long long)entry->cookie);
-               if (entry->prev_cookie == desc->target)
+               dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
+                               __FUNCTION__, (unsigned long long)entry->cookie);
+               if (entry->prev_cookie == *desc->dir_cookie)
+                       break;
+               if (loop_count++ > 200) {
+                       loop_count = 0;
+                       schedule();
+               }
+       }
+       return status;
+}
+
+/*
+ * Given a pointer to a buffer that has already been filled by a call
+ * to readdir, find the entry at offset 'desc->file->f_pos'.
+ *
+ * If the end of the buffer has been reached, return -EAGAIN, if not,
+ * return the offset within the buffer of the next entry to be
+ * read.
+ */
+static inline
+int find_dirent_index(nfs_readdir_descriptor_t *desc)
+{
+       struct nfs_entry *entry = desc->entry;
+       int             loop_count = 0,
+                       status;
+
+       for(;;) {
+               status = dir_decode(desc);
+               if (status)
+                       break;
+
+               dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
+                               (unsigned long long)entry->cookie, desc->current_index);
+
+               if (desc->file->f_pos == desc->current_index) {
+                       *desc->dir_cookie = entry->cookie;
                        break;
+               }
+               desc->current_index++;
                if (loop_count++ > 200) {
                        loop_count = 0;
                        schedule();
                }
        }
-       dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
        return status;
 }
 
 /*
- * Find the given page, and call find_dirent() in order to try to
- * return the next entry.
+ * Find the given page, and call find_dirent() or find_dirent_index in
+ * order to try to return the next entry.
  */
 static inline
 int find_dirent_page(nfs_readdir_descriptor_t *desc)
 {
-       struct inode    *inode = desc->file->f_dentry->d_inode;
+       struct inode    *inode = desc->file->f_path.dentry->d_inode;
        struct page     *page;
        int             status;
 
-       dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
+       dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
+                       __FUNCTION__, desc->page_index,
+                       (long long) *desc->dir_cookie);
 
        page = read_cache_page(inode->i_mapping, desc->page_index,
                               (filler_t *)nfs_readdir_filler, desc);
@@ -251,11 +329,14 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc)
        /* NOTE: Someone else may have changed the READDIRPLUS flag */
        desc->page = page;
        desc->ptr = kmap(page);         /* matching kunmap in nfs_do_filldir */
-       status = find_dirent(desc, page);
+       if (*desc->dir_cookie != 0)
+               status = find_dirent(desc);
+       else
+               status = find_dirent_index(desc);
        if (status < 0)
                dir_page_release(desc);
  out:
-       dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
+       dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
        return status;
  read_error:
        page_cache_release(page);
@@ -266,7 +347,8 @@ int find_dirent_page(nfs_readdir_descriptor_t *desc)
  * Recurse through the page cache pages, and return a
  * filled nfs_entry structure of the next directory entry if possible.
  *
- * The target for the search is 'desc->target'.
+ * The target for the search is '*desc->dir_cookie' if non-0,
+ * 'desc->file->f_pos' otherwise
  */
 static inline
 int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
@@ -274,7 +356,18 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
        int             loop_count = 0;
        int             res;
 
-       dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (long long)desc->target);
+       /* Always search-by-index from the beginning of the cache */
+       if (*desc->dir_cookie == 0) {
+               dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
+                               (long long)desc->file->f_pos);
+               desc->page_index = 0;
+               desc->entry->cookie = desc->entry->prev_cookie = 0;
+               desc->entry->eof = 0;
+               desc->current_index = 0;
+       } else
+               dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
+                               (unsigned long long)*desc->dir_cookie);
+
        for (;;) {
                res = find_dirent_page(desc);
                if (res != -EAGAIN)
@@ -286,28 +379,18 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
                        schedule();
                }
        }
-       dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
+
+       dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
        return res;
 }
 
-static unsigned int nfs_type2dtype[] = {
-       DT_UNKNOWN,
-       DT_REG,
-       DT_DIR,
-       DT_BLK,
-       DT_CHR,
-       DT_LNK,
-       DT_SOCK,
-       DT_UNKNOWN,
-       DT_FIFO
-};
-
-static inline
-unsigned int nfs_type_to_d_type(enum nfs_ftype type)
+static inline unsigned int dt_type(struct inode *inode)
 {
-       return nfs_type2dtype[type];
+       return (inode->i_mode >> 12) & 15;
 }
 
+static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
+
 /*
  * Once we've found the start of the dirent within a page: fill 'er up...
  */
@@ -317,11 +400,13 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
 {
        struct file     *file = desc->file;
        struct nfs_entry *entry = desc->entry;
+       struct dentry   *dentry = NULL;
        unsigned long   fileid;
        int             loop_count = 0,
                        res;
 
-       dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
+       dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
+                       (unsigned long long)entry->cookie);
 
        for(;;) {
                unsigned d_type = DT_UNKNOWN;
@@ -329,15 +414,23 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
                 *       retrieving the current dirent on the server */
                fileid = nfs_fileid_to_ino_t(entry->ino);
 
+               /* Get a dentry if we have one */
+               if (dentry != NULL)
+                       dput(dentry);
+               dentry = nfs_readdir_lookup(desc);
+
                /* Use readdirplus info */
-               if (desc->plus && (entry->fattr->valid & NFS_ATTR_FATTR))
-                       d_type = nfs_type_to_d_type(entry->fattr->type);
+               if (dentry != NULL && dentry->d_inode != NULL) {
+                       d_type = dt_type(dentry->d_inode);
+                       fileid = dentry->d_inode->i_ino;
+               }
 
                res = filldir(dirent, entry->name, entry->len, 
-                             entry->prev_cookie, fileid, d_type);
+                             file->f_pos, fileid, d_type);
                if (res < 0)
                        break;
-               file->f_pos = desc->target = entry->cookie;
+               file->f_pos++;
+               *desc->dir_cookie = entry->cookie;
                if (dir_decode(desc) != 0) {
                        desc->page_index ++;
                        break;
@@ -348,8 +441,10 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
                }
        }
        dir_page_release(desc);
-
-       dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
+       if (dentry != NULL)
+               dput(dentry);
+       dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
+                       (unsigned long long)*desc->dir_cookie, res);
        return res;
 }
 
@@ -370,28 +465,31 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
                     filldir_t filldir)
 {
        struct file     *file = desc->file;
-       struct inode    *inode = file->f_dentry->d_inode;
+       struct inode    *inode = file->f_path.dentry->d_inode;
        struct rpc_cred *cred = nfs_file_cred(file);
        struct page     *page = NULL;
        int             status;
 
-       dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
+       dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
+                       (unsigned long long)*desc->dir_cookie);
 
        page = alloc_page(GFP_HIGHUSER);
        if (!page) {
                status = -ENOMEM;
                goto out;
        }
-       desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->target,
+       desc->error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, *desc->dir_cookie,
                                                page,
                                                NFS_SERVER(inode)->dtsize,
                                                desc->plus);
-       NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
+       spin_lock(&inode->i_lock);
+       NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
+       spin_unlock(&inode->i_lock);
        desc->page = page;
        desc->ptr = kmap(page);         /* matching kunmap in nfs_do_filldir */
        if (desc->error >= 0) {
                if ((status = dir_decode(desc)) == 0)
-                       desc->entry->prev_cookie = desc->target;
+                       desc->entry->prev_cookie = *desc->dir_cookie;
        } else
                status = -EIO;
        if (status < 0)
@@ -405,19 +503,21 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
        desc->entry->cookie = desc->entry->prev_cookie = 0;
        desc->entry->eof = 0;
  out:
-       dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
+       dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
+                       __FUNCTION__, status);
        return status;
  out_release:
        dir_page_release(desc);
        goto out;
 }
 
-/* The file offset position is now represented as a true offset into the
- * page cache as is the case in most of the other filesystems.
+/* The file offset position represents the dirent entry number.  A
+   last cookie cache takes care of the common case of reading the
+   whole directory.
  */
 static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
-       struct dentry   *dentry = filp->f_dentry;
+       struct dentry   *dentry = filp->f_path.dentry;
        struct inode    *inode = dentry->d_inode;
        nfs_readdir_descriptor_t my_desc,
                        *desc = &my_desc;
@@ -426,24 +526,29 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        struct nfs_fattr fattr;
        long            res;
 
+       dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
+                       dentry->d_parent->d_name.name, dentry->d_name.name,
+                       (long long)filp->f_pos);
+       nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
+
        lock_kernel();
 
-       res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
+       res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
        if (res < 0) {
                unlock_kernel();
                return res;
        }
 
        /*
-        * filp->f_pos points to the file offset in the page cache.
-        * but if the cache has meanwhile been zapped, we need to
-        * read from the last dirent to revalidate f_pos
-        * itself.
+        * filp->f_pos points to the dirent entry number.
+        * *desc->dir_cookie has the cookie for the next entry. We have
+        * to either find the entry with the appropriate number or
+        * revalidate the cookie.
         */
        memset(desc, 0, sizeof(*desc));
 
        desc->file = filp;
-       desc->target = filp->f_pos;
+       desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
        desc->decode = NFS_PROTO(inode)->decode_dirent;
        desc->plus = NFS_USE_READDIRPLUS(inode);
 
@@ -451,13 +556,15 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        my_entry.eof = 0;
        my_entry.fh = &fh;
        my_entry.fattr = &fattr;
+       nfs_fattr_init(&fattr);
        desc->entry = &my_entry;
 
        while(!desc->entry->eof) {
                res = readdir_search_pagecache(desc);
+
                if (res == -EBADCOOKIE) {
                        /* This means either end of directory */
-                       if (desc->entry->cookie != desc->target) {
+                       if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
                                /* Or that the server has 'lost' a cookie */
                                res = uncached_readdir(desc, dirent, filldir);
                                if (res >= 0)
@@ -467,7 +574,7 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                        break;
                }
                if (res == -ETOOSMALL && desc->plus) {
-                       NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
+                       clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
                        nfs_zap_caches(inode);
                        desc->plus = 0;
                        desc->entry->eof = 0;
@@ -483,10 +590,46 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                }
        }
        unlock_kernel();
-       if (desc->error < 0)
-               return desc->error;
-       if (res < 0)
-               return res;
+       if (res > 0)
+               res = 0;
+       dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
+                       dentry->d_parent->d_name.name, dentry->d_name.name,
+                       res);
+       return res;
+}
+
+loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
+{
+       mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
+       switch (origin) {
+               case 1:
+                       offset += filp->f_pos;
+               case 0:
+                       if (offset >= 0)
+                               break;
+               default:
+                       offset = -EINVAL;
+                       goto out;
+       }
+       if (offset != filp->f_pos) {
+               filp->f_pos = offset;
+               ((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
+       }
+out:
+       mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
+       return offset;
+}
+
+/*
+ * All directory operations under NFS are synchronous, so fsync()
+ * is a dummy operation.
+ */
+int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
+{
+       dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
+                       dentry->d_parent->d_name.name, dentry->d_name.name,
+                       datasync);
+
        return 0;
 }
 
@@ -499,7 +642,7 @@ static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
 {
        if (IS_ROOT(dentry))
                return 1;
-       if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0
+       if ((NFS_I(dir)->cache_validity & NFS_INO_INVALID_ATTR) != 0
                        || nfs_attribute_timeout(dir))
                return 0;
        return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
@@ -519,14 +662,47 @@ static inline void nfs_renew_times(struct dentry * dentry)
        dentry->d_time = jiffies;
 }
 
+/*
+ * Return the intent data that applies to this particular path component
+ *
+ * Note that the current set of intents only apply to the very last
+ * component of the path.
+ * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
+ */
+static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
+{
+       if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
+               return 0;
+       return nd->flags & mask;
+}
+
+/*
+ * Inode and filehandle revalidation for lookups.
+ *
+ * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
+ * or if the intent information indicates that we're about to open this
+ * particular file and the "nocto" mount flag is not set.
+ *
+ */
 static inline
-int nfs_lookup_verify_inode(struct inode *inode, int isopen)
+int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
 {
        struct nfs_server *server = NFS_SERVER(inode);
 
-       if (isopen && !(server->flags & NFS_MOUNT_NOCTO))
-               return __nfs_revalidate_inode(server, inode);
+       if (nd != NULL) {
+               /* VFS wants an on-the-wire revalidation */
+               if (nd->flags & LOOKUP_REVAL)
+                       goto out_force;
+               /* This is an open(2) */
+               if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
+                               !(server->flags & NFS_MOUNT_NOCTO) &&
+                               (S_ISREG(inode->i_mode) ||
+                                S_ISDIR(inode->i_mode)))
+                       goto out_force;
+       }
        return nfs_revalidate_inode(server, inode);
+out_force:
+       return __nfs_revalidate_inode(server, inode);
 }
 
 /*
@@ -540,12 +716,8 @@ static inline
 int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
                       struct nameidata *nd)
 {
-       int ndflags = 0;
-
-       if (nd)
-               ndflags = nd->flags;
        /* Don't revalidate a negative dentry if we're creating a new file */
-       if ((ndflags & LOOKUP_CREATE) && !(ndflags & LOOKUP_CONTINUE))
+       if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
                return 0;
        return !nfs_check_verifier(dir, dentry);
 }
@@ -570,16 +742,13 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
        struct nfs_fh fhandle;
        struct nfs_fattr fattr;
        unsigned long verifier;
-       int isopen = 0;
 
        parent = dget_parent(dentry);
        lock_kernel();
        dir = parent->d_inode;
+       nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
        inode = dentry->d_inode;
 
-       if (nd && !(nd->flags & LOOKUP_CONTINUE) && (nd->flags & LOOKUP_OPEN))
-               isopen = 1;
-
        if (!inode) {
                if (nfs_neg_need_reval(dir, dentry, nd))
                        goto out_bad;
@@ -587,53 +756,43 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
        }
 
        if (is_bad_inode(inode)) {
-               dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
-                       dentry->d_parent->d_name.name, dentry->d_name.name);
+               dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
+                               __FUNCTION__, dentry->d_parent->d_name.name,
+                               dentry->d_name.name);
                goto out_bad;
        }
 
        /* Revalidate parent directory attribute cache */
-       nfs_revalidate_inode(NFS_SERVER(dir), dir);
+       if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
+               goto out_zap_parent;
 
        /* Force a full look up iff the parent directory has changed */
        if (nfs_check_verifier(dir, dentry)) {
-               if (nfs_lookup_verify_inode(inode, isopen))
+               if (nfs_lookup_verify_inode(inode, nd))
                        goto out_zap_parent;
                goto out_valid;
        }
 
-       /*
-        * Note: we're not holding inode->i_sem and so may be racing with
-        * operations that change the directory. We therefore save the
-        * change attribute *before* we do the RPC call.
-        */
-       verifier = nfs_save_change_attribute(dir);
-       error = nfs_cached_lookup(dir, dentry, &fhandle, &fattr);
-       if (!error) {
-               if (memcmp(NFS_FH(inode), &fhandle, sizeof(struct nfs_fh))!= 0)
-                       goto out_bad;
-               if (nfs_lookup_verify_inode(inode, isopen))
-                       goto out_zap_parent;
-               goto out_valid_renew;
-       }
-
        if (NFS_STALE(inode))
                goto out_bad;
 
+       verifier = nfs_save_change_attribute(dir);
        error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
        if (error)
                goto out_bad;
-       if (memcmp(NFS_FH(inode), &fhandle, sizeof(struct nfs_fh))!= 0)
+       if (nfs_compare_fh(NFS_FH(inode), &fhandle))
                goto out_bad;
        if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
                goto out_bad;
 
- out_valid_renew:
        nfs_renew_times(dentry);
        nfs_set_verifier(dentry, verifier);
  out_valid:
        unlock_kernel();
        dput(parent);
+       dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
+                       __FUNCTION__, dentry->d_parent->d_name.name,
+                       dentry->d_name.name);
        return 1;
 out_zap_parent:
        nfs_zap_caches(dir);
@@ -650,6 +809,9 @@ out_zap_parent:
        d_drop(dentry);
        unlock_kernel();
        dput(parent);
+       dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
+                       __FUNCTION__, dentry->d_parent->d_name.name,
+                       dentry->d_name.name);
        return 0;
 }
 
@@ -681,9 +843,10 @@ static int nfs_dentry_delete(struct dentry *dentry)
  */
 static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
 {
+       nfs_inode_return_delegation(inode);
        if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
                lock_kernel();
-               inode->i_nlink--;
+               drop_nlink(inode);
                nfs_complete_unlink(dentry);
                unlock_kernel();
        }
@@ -698,18 +861,34 @@ struct dentry_operations nfs_dentry_operations = {
        .d_iput         = nfs_dentry_iput,
 };
 
+/*
+ * Use intent information to check whether or not we're going to do
+ * an O_EXCL create using this path component.
+ */
 static inline
 int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
 {
        if (NFS_PROTO(dir)->version == 2)
                return 0;
-       if (!nd || (nd->flags & LOOKUP_CONTINUE) || !(nd->flags & LOOKUP_CREATE))
+       if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
                return 0;
        return (nd->intent.open.flags & O_EXCL) != 0;
 }
 
+static inline int nfs_reval_fsid(struct vfsmount *mnt, struct inode *dir,
+                                struct nfs_fh *fh, struct nfs_fattr *fattr)
+{
+       struct nfs_server *server = NFS_SERVER(dir);
+
+       if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
+               /* Revalidate fsid on root dir */
+               return __nfs_revalidate_inode(server, mnt->mnt_root->d_inode);
+       return 0;
+}
+
 static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
 {
+       struct dentry *res;
        struct inode *inode = NULL;
        int error;
        struct nfs_fh fhandle;
@@ -717,45 +896,64 @@ static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, stru
 
        dfprintk(VFS, "NFS: lookup(%s/%s)\n",
                dentry->d_parent->d_name.name, dentry->d_name.name);
+       nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
 
-       error = -ENAMETOOLONG;
+       res = ERR_PTR(-ENAMETOOLONG);
        if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
                goto out;
 
-       error = -ENOMEM;
+       res = ERR_PTR(-ENOMEM);
        dentry->d_op = NFS_PROTO(dir)->dentry_ops;
 
        lock_kernel();
-       /* Revalidate parent directory attribute cache */
-       nfs_revalidate_inode(NFS_SERVER(dir), dir);
 
-       /* If we're doing an exclusive create, optimize away the lookup */
-       if (nfs_is_exclusive_create(dir, nd))
-               goto no_entry;
+       /*
+        * If we're doing an exclusive create, optimize away the lookup
+        * but don't hash the dentry.
+        */
+       if (nfs_is_exclusive_create(dir, nd)) {
+               d_instantiate(dentry, NULL);
+               res = NULL;
+               goto out_unlock;
+       }
 
-       error = nfs_cached_lookup(dir, dentry, &fhandle, &fattr);
-       if (error != 0) {
-               error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name,
-                               &fhandle, &fattr);
-               if (error == -ENOENT)
-                       goto no_entry;
-               if (error != 0)
-                       goto out_unlock;
+       error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
+       if (error == -ENOENT)
+               goto no_entry;
+       if (error < 0) {
+               res = ERR_PTR(error);
+               goto out_unlock;
+       }
+       error = nfs_reval_fsid(nd->mnt, dir, &fhandle, &fattr);
+       if (error < 0) {
+               res = ERR_PTR(error);
+               goto out_unlock;
        }
-       error = -EACCES;
        inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
-       if (!inode)
+       res = (struct dentry *)inode;
+       if (IS_ERR(res))
                goto out_unlock;
+
+       dx_propagate_tag(nd, inode);
 no_entry:
-       error = 0;
-       d_add(dentry, inode);
+       res = d_materialise_unique(dentry, inode);
+       if (res != NULL) {
+               struct dentry *parent;
+               if (IS_ERR(res))
+                       goto out_unlock;
+               /* Was a directory renamed! */
+               parent = dget_parent(res);
+               if (!IS_ROOT(parent))
+                       nfs_mark_for_revalidate(parent->d_inode);
+               dput(parent);
+               dentry = res;
+       }
        nfs_renew_times(dentry);
        nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
 out_unlock:
        unlock_kernel();
 out:
-       BUG_ON(error > 0);
-       return ERR_PTR(error);
+       return res;
 }
 
 #ifdef CONFIG_NFS_V4
@@ -767,77 +965,89 @@ struct dentry_operations nfs4_dentry_operations = {
        .d_iput         = nfs_dentry_iput,
 };
 
+/*
+ * Use intent information to determine whether we need to substitute
+ * the NFSv4-style stateful OPEN for the LOOKUP call
+ */
 static int is_atomic_open(struct inode *dir, struct nameidata *nd)
 {
-       if (!nd)
-               return 0;
-       /* Check that we are indeed trying to open this file */
-       if ((nd->flags & LOOKUP_CONTINUE) || !(nd->flags & LOOKUP_OPEN))
+       if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
                return 0;
        /* NFS does not (yet) have a stateful open for directories */
        if (nd->flags & LOOKUP_DIRECTORY)
                return 0;
        /* Are we trying to write to a read only partition? */
-       if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
+       if ((IS_RDONLY(dir) || MNT_IS_RDONLY(nd->mnt)) &&
+               (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
                return 0;
        return 1;
 }
 
 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
 {
-       struct inode *inode = NULL;
-       int error = 0;
+       struct dentry *res = NULL;
+       int error;
+
+       dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
+                       dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
 
        /* Check that we are indeed trying to open this file */
        if (!is_atomic_open(dir, nd))
                goto no_open;
 
        if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
-               error = -ENAMETOOLONG;
+               res = ERR_PTR(-ENAMETOOLONG);
                goto out;
        }
        dentry->d_op = NFS_PROTO(dir)->dentry_ops;
 
        /* Let vfs_create() deal with O_EXCL */
-       if (nd->intent.open.flags & O_EXCL)
-               goto no_entry;
+       if (nd->intent.open.flags & O_EXCL) {
+               d_add(dentry, NULL);
+               goto out;
+       }
 
        /* Open the file on the server */
        lock_kernel();
        /* Revalidate parent directory attribute cache */
-       nfs_revalidate_inode(NFS_SERVER(dir), dir);
+       error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
+       if (error < 0) {
+               res = ERR_PTR(error);
+               unlock_kernel();
+               goto out;
+       }
 
        if (nd->intent.open.flags & O_CREAT) {
                nfs_begin_data_update(dir);
-               inode = nfs4_atomic_open(dir, dentry, nd);
+               res = nfs4_atomic_open(dir, dentry, nd);
                nfs_end_data_update(dir);
        } else
-               inode = nfs4_atomic_open(dir, dentry, nd);
+               res = nfs4_atomic_open(dir, dentry, nd);
        unlock_kernel();
-       if (IS_ERR(inode)) {
-               error = PTR_ERR(inode);
+       if (IS_ERR(res)) {
+               error = PTR_ERR(res);
                switch (error) {
                        /* Make a negative dentry */
                        case -ENOENT:
-                               inode = NULL;
-                               break;
+                               res = NULL;
+                               goto out;
                        /* This turned out not to be a regular file */
+                       case -EISDIR:
+                       case -ENOTDIR:
+                               goto no_open;
                        case -ELOOP:
                                if (!(nd->intent.open.flags & O_NOFOLLOW))
                                        goto no_open;
-                       /* case -EISDIR: */
                        /* case -EINVAL: */
                        default:
                                goto out;
                }
-       }
-no_entry:
-       d_add(dentry, inode);
+       } else if (res != NULL)
+               dentry = res;
        nfs_renew_times(dentry);
        nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
 out:
-       BUG_ON(error > 0);
-       return ERR_PTR(error);
+       return res;
 no_open:
        return nfs_lookup(dir, dentry, nd);
 }
@@ -850,33 +1060,33 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
        unsigned long verifier;
        int openflags, ret = 0;
 
-       /* NFS only supports OPEN for regular files */
-       if (inode && !S_ISREG(inode->i_mode))
-               goto no_open;
        parent = dget_parent(dentry);
        dir = parent->d_inode;
        if (!is_atomic_open(dir, nd))
                goto no_open;
+       /* We can't create new files in nfs_open_revalidate(), so we
+        * optimize away revalidation of negative dentries.
+        */
+       if (inode == NULL)
+               goto out;
+       /* NFS only supports OPEN on regular files */
+       if (!S_ISREG(inode->i_mode))
+               goto no_open;
        openflags = nd->intent.open.flags;
-       if (openflags & O_CREAT) {
-               /* If this is a negative dentry, just drop it */
-               if (!inode)
-                       goto out;
-               /* If this is exclusive open, just revalidate */
-               if (openflags & O_EXCL)
-                       goto no_open;
-       }
+       /* We cannot do exclusive creation on a positive dentry */
+       if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
+               goto no_open;
        /* We can't create new files, or truncate existing ones here */
        openflags &= ~(O_CREAT|O_TRUNC);
 
        /*
-        * Note: we're not holding inode->i_sem and so may be racing with
+        * Note: we're not holding inode->i_mutex and so may be racing with
         * operations that change the directory. We therefore save the
         * change attribute *before* we do the RPC call.
         */
        lock_kernel();
        verifier = nfs_save_change_attribute(dir);
-       ret = nfs4_open_revalidate(dir, dentry, openflags);
+       ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
        if (!ret)
                nfs_set_verifier(dentry, verifier);
        unlock_kernel();
@@ -887,93 +1097,67 @@ out:
        return ret;
 no_open:
        dput(parent);
+       if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
+               return 1;
        return nfs_lookup_revalidate(dentry, nd);
 }
 #endif /* CONFIG_NFSV4 */
 
-static inline
-int find_dirent_name(nfs_readdir_descriptor_t *desc, struct page *page, struct dentry *dentry)
+static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
 {
+       struct dentry *parent = desc->file->f_path.dentry;
+       struct inode *dir = parent->d_inode;
        struct nfs_entry *entry = desc->entry;
-       int              status;
+       struct dentry *dentry, *alias;
+       struct qstr name = {
+               .name = entry->name,
+               .len = entry->len,
+       };
+       struct inode *inode;
 
-       while((status = dir_decode(desc)) == 0) {
-               if (entry->len != dentry->d_name.len)
-                       continue;
-               if (memcmp(entry->name, dentry->d_name.name, entry->len))
-                       continue;
-               if (!(entry->fattr->valid & NFS_ATTR_FATTR))
-                       continue;
-               break;
+       switch (name.len) {
+               case 2:
+                       if (name.name[0] == '.' && name.name[1] == '.')
+                               return dget_parent(parent);
+                       break;
+               case 1:
+                       if (name.name[0] == '.')
+                               return dget(parent);
+       }
+       name.hash = full_name_hash(name.name, name.len);
+       dentry = d_lookup(parent, &name);
+       if (dentry != NULL)
+               return dentry;
+       if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
+               return NULL;
+       /* Note: caller is already holding the dir->i_mutex! */
+       dentry = d_alloc(parent, &name);
+       if (dentry == NULL)
+               return NULL;
+       dentry->d_op = NFS_PROTO(dir)->dentry_ops;
+       inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
+       if (IS_ERR(inode)) {
+               dput(dentry);
+               return NULL;
        }
-       return status;
-}
-
-/*
- * Use the cached Readdirplus results in order to avoid a LOOKUP call
- * whenever we believe that the parent directory has not changed.
- *
- * We assume that any file creation/rename changes the directory mtime.
- * As this results in a page cache invalidation whenever it occurs,
- * we don't require any other tests for cache coherency.
- */
-static
-int nfs_cached_lookup(struct inode *dir, struct dentry *dentry,
-                       struct nfs_fh *fh, struct nfs_fattr *fattr)
-{
-       nfs_readdir_descriptor_t desc;
-       struct nfs_server *server;
-       struct nfs_entry entry;
-       struct page *page;
-       unsigned long timestamp;
-       int res;
-
-       if (!NFS_USE_READDIRPLUS(dir))
-               return -ENOENT;
-       server = NFS_SERVER(dir);
-       /* Don't use readdirplus unless the cache is stable */
-       if ((server->flags & NFS_MOUNT_NOAC) != 0
-                       || nfs_caches_unstable(dir)
-                       || nfs_attribute_timeout(dir))
-               return -ENOENT;
-       if ((NFS_FLAGS(dir) & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) != 0)
-               return -ENOENT;
-       timestamp = NFS_I(dir)->readdir_timestamp;
-
-       entry.fh = fh;
-       entry.fattr = fattr;
-
-       desc.decode = NFS_PROTO(dir)->decode_dirent;
-       desc.entry = &entry;
-       desc.page_index = 0;
-       desc.plus = 1;
-
-       for(;(page = find_get_page(dir->i_mapping, desc.page_index)); desc.page_index++) {
-
-               res = -EIO;
-               if (PageUptodate(page)) {
-                       void * kaddr = kmap_atomic(page, KM_USER0);
-                       desc.ptr = kaddr;
-                       res = find_dirent_name(&desc, page, dentry);
-                       kunmap_atomic(kaddr, KM_USER0);
-               }
-               page_cache_release(page);
 
-               if (res == 0)
-                       goto out_found;
-               if (res != -EAGAIN)
-                       break;
+       alias = d_materialise_unique(dentry, inode);
+       if (alias != NULL) {
+               dput(dentry);
+               if (IS_ERR(alias))
+                       return NULL;
+               dentry = alias;
        }
-       return -ENOENT;
- out_found:
-       fattr->timestamp = timestamp;
-       return 0;
+
+       nfs_renew_times(dentry);
+       nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
+       return dentry;
 }
 
 /*
  * Code common to create, mkdir, and mknod.
  */
-static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
+int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
                                struct nfs_fattr *fattr)
 {
        struct inode *inode;
@@ -982,23 +1166,26 @@ static int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
        /* We may have been initialized further down */
        if (dentry->d_inode)
                return 0;
-       if (fhandle->size == 0 || !(fattr->valid & NFS_ATTR_FATTR)) {
+       if (fhandle->size == 0) {
                struct inode *dir = dentry->d_parent->d_inode;
                error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
                if (error)
-                       goto out_err;
+                       return error;
        }
-       inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
-       if (inode) {
-               d_instantiate(dentry, inode);
-               nfs_renew_times(dentry);
-               nfs_set_verifier(dentry, nfs_save_change_attribute(dentry->d_parent->d_inode));
-               return 0;
+       if (!(fattr->valid & NFS_ATTR_FATTR)) {
+               struct nfs_server *server = NFS_SB(dentry->d_sb);
+               error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
+               if (error < 0)
+                       return error;
        }
-       error = -ENOMEM;
-out_err:
-       d_drop(dentry);
-       return error;
+       inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
+       error = PTR_ERR(inode);
+       if (IS_ERR(inode))
+               return error;
+       d_instantiate(dentry, inode);
+       if (d_unhashed(dentry))
+               d_rehash(dentry);
+       return 0;
 }
 
 /*
@@ -1011,12 +1198,11 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
                struct nameidata *nd)
 {
        struct iattr attr;
-       struct inode *inode;
        int error;
        int open_flags = 0;
 
-       dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id, 
-               dir->i_ino, dentry->d_name.name);
+       dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
+                       dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
 
        attr.ia_mode = mode;
        attr.ia_valid = ATTR_MODE;
@@ -1024,26 +1210,19 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
        if (nd && (nd->flags & LOOKUP_CREATE))
                open_flags = nd->intent.open.flags;
 
-       /*
-        * The 0 argument passed into the create function should one day
-        * contain the O_EXCL flag if requested. This allows NFSv3 to
-        * select the appropriate create strategy. Currently open_namei
-        * does not pass the create flags.
-        */
        lock_kernel();
        nfs_begin_data_update(dir);
-       inode = NFS_PROTO(dir)->create(dir, &dentry->d_name, &attr, open_flags);
+       error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
        nfs_end_data_update(dir);
-       if (!IS_ERR(inode)) {
-               d_instantiate(dentry, inode);
-               nfs_renew_times(dentry);
-               nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
-               error = 0;
-       } else {
-               error = PTR_ERR(inode);
-               d_drop(dentry);
-       }
+       if (error != 0)
+               goto out_err;
+       nfs_renew_times(dentry);
+       nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
+       unlock_kernel();
+       return 0;
+out_err:
        unlock_kernel();
+       d_drop(dentry);
        return error;
 }
 
@@ -1054,12 +1233,10 @@ static int
 nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
 {
        struct iattr attr;
-       struct nfs_fattr fattr;
-       struct nfs_fh fhandle;
-       int error;
+       int status;
 
-       dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id,
-               dir->i_ino, dentry->d_name.name);
+       dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
+                       dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
 
        if (!new_valid_dev(rdev))
                return -EINVAL;
@@ -1069,15 +1246,18 @@ nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
 
        lock_kernel();
        nfs_begin_data_update(dir);
-       error = NFS_PROTO(dir)->mknod(dir, &dentry->d_name, &attr, rdev,
-                                       &fhandle, &fattr);
+       status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
        nfs_end_data_update(dir);
-       if (!error)
-               error = nfs_instantiate(dentry, &fhandle, &fattr);
-       else
-               d_drop(dentry);
+       if (status != 0)
+               goto out_err;
+       nfs_renew_times(dentry);
+       nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
        unlock_kernel();
-       return error;
+       return 0;
+out_err:
+       unlock_kernel();
+       d_drop(dentry);
+       return status;
 }
 
 /*
@@ -1086,34 +1266,26 @@ nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
 static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 {
        struct iattr attr;
-       struct nfs_fattr fattr;
-       struct nfs_fh fhandle;
        int error;
 
-       dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id,
-               dir->i_ino, dentry->d_name.name);
+       dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
+                       dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
 
        attr.ia_valid = ATTR_MODE;
        attr.ia_mode = mode | S_IFDIR;
 
        lock_kernel();
-#if 0
-       /*
-        * Always drop the dentry, we can't always depend on
-        * the fattr returned by the server (AIX seems to be
-        * broken). We're better off doing another lookup than
-        * depending on potentially bogus information.
-        */
-       d_drop(dentry);
-#endif
        nfs_begin_data_update(dir);
-       error = NFS_PROTO(dir)->mkdir(dir, &dentry->d_name, &attr, &fhandle,
-                                       &fattr);
+       error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
        nfs_end_data_update(dir);
-       if (!error)
-               error = nfs_instantiate(dentry, &fhandle, &fattr);
-       else
-               d_drop(dentry);
+       if (error != 0)
+               goto out_err;
+       nfs_renew_times(dentry);
+       nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
+       unlock_kernel();
+       return 0;
+out_err:
+       d_drop(dentry);
        unlock_kernel();
        return error;
 }
@@ -1122,15 +1294,15 @@ static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
 {
        int error;
 
-       dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id,
-               dir->i_ino, dentry->d_name.name);
+       dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
+                       dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
 
        lock_kernel();
        nfs_begin_data_update(dir);
        error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
        /* Ensure the VFS deletes this inode */
        if (error == 0 && dentry->d_inode != NULL)
-               dentry->d_inode->i_nlink = 0;
+               clear_nlink(dentry->d_inode);
        nfs_end_data_update(dir);
        unlock_kernel();
 
@@ -1142,7 +1314,7 @@ static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
        static unsigned int sillycounter;
        const int      i_inosize  = sizeof(dir->i_ino)*2;
        const int      countersize = sizeof(sillycounter)*2;
-       const int      slen       = strlen(".nfs") + i_inosize + countersize;
+       const int      slen       = sizeof(".nfs") + i_inosize + countersize - 1;
        char           silly[slen+1];
        struct qstr    qsilly;
        struct dentry *sdentry;
@@ -1151,6 +1323,7 @@ static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
        dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
                dentry->d_parent->d_name.name, dentry->d_name.name, 
                atomic_read(&dentry->d_count));
+       nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
 
 #ifdef NFS_PARANOIA
 if (!dentry->d_inode)
@@ -1167,6 +1340,9 @@ dentry->d_parent->d_name.name, dentry->d_name.name);
        sprintf(silly, ".nfs%*.*lx",
                i_inosize, i_inosize, dentry->d_inode->i_ino);
 
+       /* Return delegation in anticipation of the rename */
+       nfs_inode_return_delegation(dentry->d_inode);
+
        sdentry = NULL;
        do {
                char *suffix = silly + slen - countersize;
@@ -1175,8 +1351,8 @@ dentry->d_parent->d_name.name, dentry->d_name.name);
                sillycounter++;
                sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
 
-               dfprintk(VFS, "trying to rename %s to %s\n",
-                        dentry->d_name.name, silly);
+               dfprintk(VFS, "NFS: trying to rename %s to %s\n",
+                               dentry->d_name.name, silly);
                
                sdentry = lookup_one_len(silly, dentry->d_parent, slen);
                /*
@@ -1194,6 +1370,7 @@ dentry->d_parent->d_name.name, dentry->d_name.name);
                nfs_begin_data_update(dentry->d_inode);
                error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
                                dir, &qsilly);
+               nfs_mark_for_revalidate(dentry->d_inode);
                nfs_end_data_update(dentry->d_inode);
        } else
                error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
@@ -1235,11 +1412,13 @@ static int nfs_safe_remove(struct dentry *dentry)
 
        nfs_begin_data_update(dir);
        if (inode != NULL) {
+               nfs_inode_return_delegation(inode);
                nfs_begin_data_update(inode);
                error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
                /* The VFS may want to delete this inode */
                if (error == 0)
-                       inode->i_nlink--;
+                       drop_nlink(inode);
+               nfs_mark_for_revalidate(inode);
                nfs_end_data_update(inode);
        } else
                error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
@@ -1287,63 +1466,82 @@ static int nfs_unlink(struct inode *dir, struct dentry *dentry)
        return error;
 }
 
-static int
-nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
+/*
+ * To create a symbolic link, most file systems instantiate a new inode,
+ * add a page to it containing the path, then write it out to the disk
+ * using prepare_write/commit_write.
+ *
+ * Unfortunately the NFS client can't create the in-core inode first
+ * because it needs a file handle to create an in-core inode (see
+ * fs/nfs/inode.c:nfs_fhget).  We only have a file handle *after* the
+ * symlink request has completed on the server.
+ *
+ * So instead we allocate a raw page, copy the symname into it, then do
+ * the SYMLINK request with the page as the buffer.  If it succeeds, we
+ * now have a new file handle and can instantiate an in-core NFS inode
+ * and move the raw page into its mapping.
+ */
+static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
 {
+       struct pagevec lru_pvec;
+       struct page *page;
+       char *kaddr;
        struct iattr attr;
-       struct nfs_fattr sym_attr;
-       struct nfs_fh sym_fh;
-       struct qstr qsymname;
+       unsigned int pathlen = strlen(symname);
        int error;
 
        dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
                dir->i_ino, dentry->d_name.name, symname);
 
-       error = -ENAMETOOLONG;
-       switch (NFS_PROTO(dir)->version) {
-               case 2:
-                       if (strlen(symname) > NFS2_MAXPATHLEN)
-                               goto out;
-                       break;
-               case 3:
-                       if (strlen(symname) > NFS3_MAXPATHLEN)
-                               goto out;
-               default:
-                       break;
-       }
+       if (pathlen > PAGE_SIZE)
+               return -ENAMETOOLONG;
 
-#ifdef NFS_PARANOIA
-if (dentry->d_inode)
-printk("nfs_proc_symlink: %s/%s not negative!\n",
-dentry->d_parent->d_name.name, dentry->d_name.name);
-#endif
-       /*
-        * Fill in the sattr for the call.
-        * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
-        */
-       attr.ia_valid = ATTR_MODE;
        attr.ia_mode = S_IFLNK | S_IRWXUGO;
-
-       qsymname.name = symname;
-       qsymname.len  = strlen(symname);
+       attr.ia_valid = ATTR_MODE;
 
        lock_kernel();
+
+       page = alloc_page(GFP_KERNEL);
+       if (!page) {
+               unlock_kernel();
+               return -ENOMEM;
+       }
+
+       kaddr = kmap_atomic(page, KM_USER0);
+       memcpy(kaddr, symname, pathlen);
+       if (pathlen < PAGE_SIZE)
+               memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
+       kunmap_atomic(kaddr, KM_USER0);
+
        nfs_begin_data_update(dir);
-       error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
-                                         &attr, &sym_fh, &sym_attr);
+       error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
        nfs_end_data_update(dir);
-       if (!error) {
-               error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
-       } else {
-               if (error == -EEXIST)
-                       printk("nfs_proc_symlink: %s/%s already exists??\n",
-                              dentry->d_parent->d_name.name, dentry->d_name.name);
+       if (error != 0) {
+               dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
+                       dir->i_sb->s_id, dir->i_ino,
+                       dentry->d_name.name, symname, error);
                d_drop(dentry);
+               __free_page(page);
+               unlock_kernel();
+               return error;
        }
-       unlock_kernel();
 
-out:
-       return error;
+       /*
+        * No big deal if we can't add this page to the page cache here.
+        * READLINK will get the missing page from the server if needed.
+        */
+       pagevec_init(&lru_pvec, 0);
+       if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
+                                                       GFP_KERNEL)) {
+               pagevec_add(&lru_pvec, page);
+               pagevec_lru_add(&lru_pvec);
+               SetPageUptodate(page);
+               unlock_page(page);
+       } else
+               __free_page(page);
+
+       unlock_kernel();
+       return 0;
 }
 
 static int 
@@ -1356,17 +1554,14 @@ nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
                old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
                dentry->d_parent->d_name.name, dentry->d_name.name);
 
-       /*
-        * Drop the dentry in advance to force a new lookup.
-        * Since nfs_proc_link doesn't return a file handle,
-        * we can't use the existing dentry.
-        */
        lock_kernel();
-       d_drop(dentry);
-
        nfs_begin_data_update(dir);
        nfs_begin_data_update(inode);
        error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
+       if (error == 0) {
+               atomic_inc(&inode->i_count);
+               d_instantiate(dentry, inode);
+       }
        nfs_end_data_update(inode);
        nfs_end_data_update(dir);
        unlock_kernel();
@@ -1430,9 +1625,11 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
         */
        if (!new_inode)
                goto go_ahead;
-       if (S_ISDIR(new_inode->i_mode))
-               goto out;
-       else if (atomic_read(&new_dentry->d_count) > 1) {
+       if (S_ISDIR(new_inode->i_mode)) {
+               error = -EISDIR;
+               if (!S_ISDIR(old_inode->i_mode))
+                       goto out;
+       } else if (atomic_read(&new_dentry->d_count) > 2) {
                int err;
                /* copy the target dentry's name */
                dentry = d_alloc(new_dentry->d_parent,
@@ -1447,10 +1644,8 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
                        new_inode = NULL;
                        /* instantiate the replacement target */
                        d_instantiate(new_dentry, NULL);
-               }
-
+               } else if (atomic_read(&new_dentry->d_count) > 1) {
                /* dentry still busy? */
-               if (atomic_read(&new_dentry->d_count) > 1) {
 #ifdef NFS_PARANOIA
                        printk("nfs_rename: target %s/%s busy, d_count=%d\n",
                               new_dentry->d_parent->d_name.name,
@@ -1459,25 +1654,31 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 #endif
                        goto out;
                }
-       }
+       } else
+               drop_nlink(new_inode);
 
 go_ahead:
        /*
         * ... prune child dentries and writebacks if needed.
         */
        if (atomic_read(&old_dentry->d_count) > 1) {
-               nfs_wb_all(old_inode);
+               if (S_ISREG(old_inode->i_mode))
+                       nfs_wb_all(old_inode);
                shrink_dcache_parent(old_dentry);
        }
+       nfs_inode_return_delegation(old_inode);
 
-       if (new_inode)
+       if (new_inode != NULL) {
+               nfs_inode_return_delegation(new_inode);
                d_delete(new_dentry);
+       }
 
        nfs_begin_data_update(old_dir);
        nfs_begin_data_update(new_dir);
        nfs_begin_data_update(old_inode);
        error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
                                           new_dir, &new_dentry->d_name);
+       nfs_mark_for_revalidate(old_inode);
        nfs_end_data_update(old_inode);
        nfs_end_data_update(new_dir);
        nfs_end_data_update(old_dir);
@@ -1485,8 +1686,7 @@ out:
        if (rehash)
                d_rehash(rehash);
        if (!error) {
-               if (!S_ISDIR(old_inode->i_mode))
-                       d_move(old_dentry, new_dentry);
+               d_move(old_dentry, new_dentry);
                nfs_renew_times(new_dentry);
                nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
        }
@@ -1498,85 +1698,291 @@ out:
        return error;
 }
 
-int
-nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
+static DEFINE_SPINLOCK(nfs_access_lru_lock);
+static LIST_HEAD(nfs_access_lru_list);
+static atomic_long_t nfs_access_nr_entries;
+
+static void nfs_access_free_entry(struct nfs_access_entry *entry)
+{
+       put_rpccred(entry->cred);
+       kfree(entry);
+       smp_mb__before_atomic_dec();
+       atomic_long_dec(&nfs_access_nr_entries);
+       smp_mb__after_atomic_dec();
+}
+
+int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
+{
+       LIST_HEAD(head);
+       struct nfs_inode *nfsi;
+       struct nfs_access_entry *cache;
+
+       spin_lock(&nfs_access_lru_lock);
+restart:
+       list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
+               struct inode *inode;
+
+               if (nr_to_scan-- == 0)
+                       break;
+               inode = igrab(&nfsi->vfs_inode);
+               if (inode == NULL)
+                       continue;
+               spin_lock(&inode->i_lock);
+               if (list_empty(&nfsi->access_cache_entry_lru))
+                       goto remove_lru_entry;
+               cache = list_entry(nfsi->access_cache_entry_lru.next,
+                               struct nfs_access_entry, lru);
+               list_move(&cache->lru, &head);
+               rb_erase(&cache->rb_node, &nfsi->access_cache);
+               if (!list_empty(&nfsi->access_cache_entry_lru))
+                       list_move_tail(&nfsi->access_cache_inode_lru,
+                                       &nfs_access_lru_list);
+               else {
+remove_lru_entry:
+                       list_del_init(&nfsi->access_cache_inode_lru);
+                       clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
+               }
+               spin_unlock(&inode->i_lock);
+               iput(inode);
+               goto restart;
+       }
+       spin_unlock(&nfs_access_lru_lock);
+       while (!list_empty(&head)) {
+               cache = list_entry(head.next, struct nfs_access_entry, lru);
+               list_del(&cache->lru);
+               nfs_access_free_entry(cache);
+       }
+       return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
+}
+
+static void __nfs_access_zap_cache(struct inode *inode)
+{
+       struct nfs_inode *nfsi = NFS_I(inode);
+       struct rb_root *root_node = &nfsi->access_cache;
+       struct rb_node *n, *dispose = NULL;
+       struct nfs_access_entry *entry;
+
+       /* Unhook entries from the cache */
+       while ((n = rb_first(root_node)) != NULL) {
+               entry = rb_entry(n, struct nfs_access_entry, rb_node);
+               rb_erase(n, root_node);
+               list_del(&entry->lru);
+               n->rb_left = dispose;
+               dispose = n;
+       }
+       nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
+       spin_unlock(&inode->i_lock);
+
+       /* Now kill them all! */
+       while (dispose != NULL) {
+               n = dispose;
+               dispose = n->rb_left;
+               nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
+       }
+}
+
+void nfs_access_zap_cache(struct inode *inode)
+{
+       /* Remove from global LRU init */
+       if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
+               spin_lock(&nfs_access_lru_lock);
+               list_del_init(&NFS_I(inode)->access_cache_inode_lru);
+               spin_unlock(&nfs_access_lru_lock);
+       }
+
+       spin_lock(&inode->i_lock);
+       /* This will release the spinlock */
+       __nfs_access_zap_cache(inode);
+}
+
+static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
+{
+       struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
+       struct nfs_access_entry *entry;
+
+       while (n != NULL) {
+               entry = rb_entry(n, struct nfs_access_entry, rb_node);
+
+               if (cred < entry->cred)
+                       n = n->rb_left;
+               else if (cred > entry->cred)
+                       n = n->rb_right;
+               else
+                       return entry;
+       }
+       return NULL;
+}
+
+int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
+{
+       struct nfs_inode *nfsi = NFS_I(inode);
+       struct nfs_access_entry *cache;
+       int err = -ENOENT;
+
+       spin_lock(&inode->i_lock);
+       if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
+               goto out_zap;
+       cache = nfs_access_search_rbtree(inode, cred);
+       if (cache == NULL)
+               goto out;
+       if (time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
+               goto out_stale;
+       res->jiffies = cache->jiffies;
+       res->cred = cache->cred;
+       res->mask = cache->mask;
+       list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
+       err = 0;
+out:
+       spin_unlock(&inode->i_lock);
+       return err;
+out_stale:
+       rb_erase(&cache->rb_node, &nfsi->access_cache);
+       list_del(&cache->lru);
+       spin_unlock(&inode->i_lock);
+       nfs_access_free_entry(cache);
+       return -ENOENT;
+out_zap:
+       /* This will release the spinlock */
+       __nfs_access_zap_cache(inode);
+       return -ENOENT;
+}
+
+static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
+{
+       struct nfs_inode *nfsi = NFS_I(inode);
+       struct rb_root *root_node = &nfsi->access_cache;
+       struct rb_node **p = &root_node->rb_node;
+       struct rb_node *parent = NULL;
+       struct nfs_access_entry *entry;
+
+       spin_lock(&inode->i_lock);
+       while (*p != NULL) {
+               parent = *p;
+               entry = rb_entry(parent, struct nfs_access_entry, rb_node);
+
+               if (set->cred < entry->cred)
+                       p = &parent->rb_left;
+               else if (set->cred > entry->cred)
+                       p = &parent->rb_right;
+               else
+                       goto found;
+       }
+       rb_link_node(&set->rb_node, parent, p);
+       rb_insert_color(&set->rb_node, root_node);
+       list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
+       spin_unlock(&inode->i_lock);
+       return;
+found:
+       rb_replace_node(parent, &set->rb_node, root_node);
+       list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
+       list_del(&entry->lru);
+       spin_unlock(&inode->i_lock);
+       nfs_access_free_entry(entry);
+}
+
+void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
+{
+       struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
+       if (cache == NULL)
+               return;
+       RB_CLEAR_NODE(&cache->rb_node);
+       cache->jiffies = set->jiffies;
+       cache->cred = get_rpccred(set->cred);
+       cache->mask = set->mask;
+
+       nfs_access_add_rbtree(inode, cache);
+
+       /* Update accounting */
+       smp_mb__before_atomic_inc();
+       atomic_long_inc(&nfs_access_nr_entries);
+       smp_mb__after_atomic_inc();
+
+       /* Add inode to global LRU list */
+       if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
+               spin_lock(&nfs_access_lru_lock);
+               list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
+               spin_unlock(&nfs_access_lru_lock);
+       }
+}
+
+static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
+{
+       struct nfs_access_entry cache;
+       int status;
+
+       status = nfs_access_get_cached(inode, cred, &cache);
+       if (status == 0)
+               goto out;
+
+       /* Be clever: ask server to check for all possible rights */
+       cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
+       cache.cred = cred;
+       cache.jiffies = jiffies;
+       status = NFS_PROTO(inode)->access(inode, &cache);
+       if (status != 0)
+               return status;
+       nfs_access_add_cache(inode, &cache);
+out:
+       if ((cache.mask & mask) == mask)
+               return 0;
+       return -EACCES;
+}
+
+int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
 {
-       struct nfs_access_cache *cache = &NFS_I(inode)->cache_access;
        struct rpc_cred *cred;
-       int mode = inode->i_mode;
-       int res;
+       int res = 0;
+
+       nfs_inc_stats(inode, NFSIOS_VFSACCESS);
 
        if (mask == 0)
-               return 0;
-       if (mask & MAY_WRITE) {
-               /*
-                *
-                * Nobody gets write access to a read-only fs.
-                *
-                */
-               if (IS_RDONLY(inode) &&
-                   (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
-                       return -EROFS;
+               goto out;
+       /* Is this sys_access() ? */
+       if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
+               goto force_lookup;
 
-               /*
-                *
-                * Nobody gets write access to an immutable file.
-                *
-                */
-               if (IS_IMMUTABLE(inode))
-                       return -EACCES;
-       }
-       /* Are we checking permissions on anything other than lookup/execute? */
-       if ((mask & MAY_EXEC) == 0) {
-               /* We only need to check permissions on file open() and access() */
-               if (!nd || !(nd->flags & (LOOKUP_OPEN|LOOKUP_ACCESS)))
-                       return 0;
-               /* NFSv4 has atomic_open... */
-               if (NFS_PROTO(inode)->version > 3 && (nd->flags & LOOKUP_OPEN))
-                       return 0;
+       switch (inode->i_mode & S_IFMT) {
+               case S_IFLNK:
+                       goto out;
+               case S_IFREG:
+                       /* NFSv4 has atomic_open... */
+                       if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
+                                       && nd != NULL
+                                       && (nd->flags & LOOKUP_OPEN))
+                               goto out;
+                       break;
+               case S_IFDIR:
+                       /*
+                        * Optimize away all write operations, since the server
+                        * will check permissions when we perform the op.
+                        */
+                       if ((mask & MAY_WRITE) && !(mask & MAY_READ))
+                               goto out;
        }
 
+force_lookup:
        lock_kernel();
 
        if (!NFS_PROTO(inode)->access)
                goto out_notsup;
 
        cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
-       if (cache->cred == cred
-           && time_before(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
-           && !(NFS_FLAGS(inode) & NFS_INO_INVALID_ATTR)) {
-               if (!(res = cache->err)) {
-                       /* Is the mask a subset of an accepted mask? */
-                       if ((cache->mask & mask) == mask)
-                               goto out;
-               } else {
-                       /* ...or is it a superset of a rejected mask? */
-                       if ((cache->mask & mask) == cache->mask)
-                               goto out;
-               }
-       }
-
-       res = NFS_PROTO(inode)->access(inode, cred, mask);
-       if (!res || res == -EACCES)
-               goto add_cache;
-out:
-       put_rpccred(cred);
+       if (!IS_ERR(cred)) {
+               res = nfs_do_access(inode, cred, mask);
+               put_rpccred(cred);
+       } else
+               res = PTR_ERR(cred);
        unlock_kernel();
+out:
+       dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
+               inode->i_sb->s_id, inode->i_ino, mask, res);
        return res;
 out_notsup:
-       nfs_revalidate_inode(NFS_SERVER(inode), inode);
-       res = vfs_permission(inode, mask);
-       unlock_kernel();
-       return res;
-add_cache:
-       cache->jiffies = jiffies;
-       if (cache->cred)
-               put_rpccred(cache->cred);
-       cache->cred = cred;
-       cache->mask = mask;
-       cache->err = res;
+       res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
+       if (res == 0)
+               res = generic_permission(inode, mask, NULL);
        unlock_kernel();
-       return res;
+       goto out;
 }
 
 /*