vserver 1.9.5.x5
[linux-2.6.git] / fs / nfs / dir.c
index 626252a..cc3c278 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/pagemap.h>
 #include <linux/smp_lock.h>
 #include <linux/namei.h>
+#include <linux/vserver/xid.h>
 
 #include "delegation.h"
 
@@ -40,8 +41,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 *);
@@ -294,24 +293,13 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
        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...
  */
@@ -321,6 +309,7 @@ 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;
@@ -333,9 +322,16 @@ 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);
@@ -352,7 +348,8 @@ 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);
        return res;
 }
@@ -615,24 +612,10 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
                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 (nfs_compare_fh(NFS_FH(inode), &fhandle))
-                       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;
@@ -641,7 +624,6 @@ static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
        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:
@@ -723,6 +705,7 @@ int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
 
 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;
@@ -731,11 +714,11 @@ 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);
 
-       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();
@@ -746,29 +729,28 @@ static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, stru
        if (nfs_is_exclusive_create(dir, nd))
                goto no_entry;
 
-       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 = -EACCES;
+       res = ERR_PTR(-EACCES);
        inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
        if (!inode)
                goto out_unlock;
+       vx_propagate_xid(nd, inode);
 no_entry:
-       error = 0;
-       d_add(dentry, inode);
+       res = d_add_unique(dentry, inode);
+       if (res != NULL)
+               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
@@ -798,15 +780,15 @@ 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 = 0;
 
        /* 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;
@@ -828,7 +810,7 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
                inode = nfs4_atomic_open(dir, dentry, nd);
        unlock_kernel();
        if (IS_ERR(inode)) {
-               error = PTR_ERR(inode);
+               int error = PTR_ERR(inode);
                switch (error) {
                        /* Make a negative dentry */
                        case -ENOENT:
@@ -841,16 +823,18 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
                        /* case -EISDIR: */
                        /* case -EINVAL: */
                        default:
+                               res = ERR_PTR(error);
                                goto out;
                }
        }
 no_entry:
-       d_add(dentry, inode);
+       res = d_add_unique(dentry, inode);
+       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);
 }
@@ -906,83 +890,51 @@ no_open:
 }
 #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_dentry;
+       struct inode *dir = parent->d_inode;
        struct nfs_entry *entry = desc->entry;
-       int              status;
-
-       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;
-       }
-       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);
+       struct dentry *dentry, *alias;
+       struct qstr name = {
+               .name = entry->name,
+               .len = entry->len,
+       };
+       struct inode *inode;
 
-               if (res == 0)
-                       goto out_found;
-               if (res != -EAGAIN)
+       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);
        }
-       return -ENOENT;
- out_found:
-       fattr->timestamp = timestamp;
-       return 0;
+       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_sem! */
+       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 (!inode) {
+               dput(dentry);
+               return NULL;
+       }
+       alias = d_add_unique(dentry, inode);
+       if (alias != NULL) {
+               dput(dentry);
+               dentry = alias;
+       }
+       nfs_renew_times(dentry);
+       nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
+       return dentry;
 }
 
 /*
@@ -1045,15 +997,9 @@ 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);
+       inode = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
        nfs_end_data_update(dir);
        if (!IS_ERR(inode)) {
                d_instantiate(dentry, inode);
@@ -1438,7 +1384,7 @@ static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
                goto go_ahead;
        if (S_ISDIR(new_inode->i_mode))
                goto out;
-       else if (atomic_read(&new_dentry->d_count) > 1) {
+       else if (atomic_read(&new_dentry->d_count) > 2) {
                int err;
                /* copy the target dentry's name */
                dentry = d_alloc(new_dentry->d_parent,
@@ -1453,10 +1399,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,
@@ -1510,7 +1454,7 @@ int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs
 
        if (cache->cred != cred
                        || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
-                       || (NFS_FLAGS(inode) & NFS_INO_INVALID_ATTR))
+                       || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
                return -ENOENT;
        memcpy(res, cache, sizeof(*res));
        return 0;
@@ -1524,6 +1468,7 @@ void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
                if (cache->cred)
                        put_rpccred(cache->cred);
                cache->cred = get_rpccred(set->cred);
+               NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS;
        }
        cache->jiffies = set->jiffies;
        cache->mask = set->mask;
@@ -1555,29 +1500,11 @@ out:
 int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
 {
        struct rpc_cred *cred;
-       int mode = inode->i_mode;
        int res;
 
        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;
 
-               /*
-                *
-                * 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() */
@@ -1600,7 +1527,7 @@ int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
        return res;
 out_notsup:
        nfs_revalidate_inode(NFS_SERVER(inode), inode);
-       res = vfs_permission(inode, mask);
+       res = generic_permission(inode, mask, NULL);
        unlock_kernel();
        return res;
 }