linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / fs / nfs / dir.c
index 8c827fe..cb038fc 100644 (file)
 #include <linux/sunrpc/clnt.h>
 #include <linux/nfs_fs.h>
 #include <linux/nfs_mount.h>
+#include <linux/mount.h>
 #include <linux/pagemap.h>
 #include <linux/smp_lock.h>
 #include <linux/namei.h>
 #include <linux/vserver/xid.h>
 
+#include "nfs4_fs.h"
 #include "delegation.h"
 
 #define NFS_PARANOIA 1
@@ -51,8 +53,10 @@ 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 = {
+       .llseek         = nfs_llseek_dir,
        .read           = generic_read_dir,
        .readdir        = nfs_readdir,
        .open           = nfs_opendir,
@@ -75,6 +79,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 *);
@@ -91,6 +116,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 */
@@ -117,7 +145,8 @@ typedef struct {
        struct page     *page;
        unsigned long   page_index;
        u32             *ptr;
-       u64             target;
+       u64             *dir_cookie;
+       loff_t          current_index;
        struct nfs_entry *entry;
        decode_dirent_t decode;
        int             plus;
@@ -155,22 +184,22 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
                /* 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);
        unlock_page(page);
        return 0;
  error:
@@ -203,22 +232,22 @@ 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(VFS, "NFS: found cookie %Lu\n", (unsigned long long)entry->cookie);
+               if (entry->prev_cookie == *desc->dir_cookie)
                        break;
                if (loop_count++ > 200) {
                        loop_count = 0;
@@ -230,8 +259,44 @@ int find_dirent(nfs_readdir_descriptor_t *desc, struct page *page)
 }
 
 /*
- * Find the given page, and call find_dirent() in order to try to
- * return the next entry.
+ * 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(VFS, "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_index() returns %d\n", status);
+       return status;
+}
+
+/*
+ * 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)
@@ -254,7 +319,10 @@ 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:
@@ -269,7 +337,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)
@@ -277,7 +346,16 @@ 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(VFS, "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(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (unsigned long long)*desc->dir_cookie);
+
        for (;;) {
                res = find_dirent_page(desc);
                if (res != -EAGAIN)
@@ -314,7 +392,7 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
        int             loop_count = 0,
                        res;
 
-       dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)desc->target);
+       dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)entry->cookie);
 
        for(;;) {
                unsigned d_type = DT_UNKNOWN;
@@ -334,10 +412,11 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
                }
 
                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;
@@ -350,7 +429,7 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
        dir_page_release(desc);
        if (dentry != NULL)
                dput(dentry);
-       dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (long long)desc->target, res);
+       dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (unsigned long long)*desc->dir_cookie, res);
        return res;
 }
 
@@ -376,23 +455,25 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
        struct page     *page = NULL;
        int             status;
 
-       dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (long long)desc->target);
+       dfprintk(VFS, "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_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)
@@ -413,8 +494,9 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
        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)
 {
@@ -436,15 +518,15 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        }
 
        /*
-        * 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);
 
@@ -452,13 +534,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)
@@ -468,7 +552,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;
@@ -484,13 +568,33 @@ 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;
        return 0;
 }
 
+loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
+{
+       mutex_lock(&filp->f_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_dentry->d_inode->i_mutex);
+       return offset;
+}
+
 /*
  * All directory operations under NFS are synchronous, so fsync()
  * is a dummy operation.
@@ -509,7 +613,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);
@@ -700,6 +804,7 @@ 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--;
@@ -750,17 +855,17 @@ static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, stru
        dentry->d_op = NFS_PROTO(dir)->dentry_ops;
 
        lock_kernel();
-       /* Revalidate parent directory attribute cache */
-       error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
-       if (error < 0) {
-               res = ERR_PTR(error);
+
+       /*
+        * 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;
        }
 
-       /* If we're doing an exclusive create, optimize away the lookup */
-       if (nfs_is_exclusive_create(dir, nd))
-               goto no_entry;
-
        error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
        if (error == -ENOENT)
                goto no_entry;
@@ -815,7 +920,6 @@ static int is_atomic_open(struct inode *dir, struct nameidata *nd)
 static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
 {
        struct dentry *res = NULL;
-       struct inode *inode = NULL;
        int error;
 
        /* Check that we are indeed trying to open this file */
@@ -829,8 +933,10 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
        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();
@@ -838,37 +944,36 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
        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:
-                               res = ERR_PTR(error);
                                goto out;
                }
-       }
-no_entry:
-       res = d_add_unique(dentry, inode);
-       if (res != NULL)
+       } else if (res != NULL)
                dentry = res;
        nfs_renew_times(dentry);
        nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
@@ -906,13 +1011,13 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
        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();
@@ -956,7 +1061,7 @@ static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
                return dentry;
        if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
                return NULL;
-       /* Note: caller is already holding the dir->i_sem! */
+       /* Note: caller is already holding the dir->i_mutex! */
        dentry = d_alloc(parent, &name);
        if (dentry == NULL)
                return NULL;
@@ -1005,6 +1110,8 @@ int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
        if (inode == NULL)
                goto out_err;
        d_instantiate(dentry, inode);
+       if (d_unhashed(dentry))
+               d_rehash(dentry);
        return 0;
 out_err:
        d_drop(dentry);
@@ -1035,7 +1142,7 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
 
        lock_kernel();
        nfs_begin_data_update(dir);
-       error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
+       error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
        nfs_end_data_update(dir);
        if (error != 0)
                goto out_err;
@@ -1162,6 +1269,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;
@@ -1189,6 +1299,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,
@@ -1230,11 +1341,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--;
+               nfs_mark_for_revalidate(inode);
                nfs_end_data_update(inode);
        } else
                error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
@@ -1336,17 +1449,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();
@@ -1410,9 +1520,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) > 2) {
+       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,
@@ -1437,7 +1549,8 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 #endif
                        goto out;
                }
-       }
+       } else
+               new_inode->i_nlink--;
 
 go_ahead:
        /*
@@ -1447,15 +1560,19 @@ go_ahead:
                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);
@@ -1478,11 +1595,12 @@ out:
 
 int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
 {
-       struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
+       struct nfs_inode *nfsi = NFS_I(inode);
+       struct nfs_access_entry *cache = &nfsi->cache_access;
 
        if (cache->cred != cred
                        || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
-                       || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
+                       || (nfsi->cache_validity & NFS_INO_INVALID_ACCESS))
                return -ENOENT;
        memcpy(res, cache, sizeof(*res));
        return 0;
@@ -1490,14 +1608,18 @@ int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs
 
 void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
 {
-       struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
+       struct nfs_inode *nfsi = NFS_I(inode);
+       struct nfs_access_entry *cache = &nfsi->cache_access;
 
        if (cache->cred != set->cred) {
                if (cache->cred)
                        put_rpccred(cache->cred);
                cache->cred = get_rpccred(set->cred);
        }
-       NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS;
+       /* FIXME: replace current access_cache BKL reliance with inode->i_lock */
+       spin_lock(&inode->i_lock);
+       nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
+       spin_unlock(&inode->i_lock);
        cache->jiffies = set->jiffies;
        cache->mask = set->mask;
 }