vserver 1.9.5.x5
[linux-2.6.git] / fs / nfs / dir.c
index f061e70..cc3c278 100644 (file)
@@ -31,6 +31,9 @@
 #include <linux/pagemap.h>
 #include <linux/smp_lock.h>
 #include <linux/namei.h>
+#include <linux/vserver/xid.h>
+
+#include "delegation.h"
 
 #define NFS_PARANOIA 1
 /* #define NFS_DEBUG_VERBOSE 1 */
@@ -38,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 *);
@@ -49,12 +50,14 @@ 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);
 
 struct file_operations nfs_dir_operations = {
        .read           = generic_read_dir,
        .readdir        = nfs_readdir,
        .open           = nfs_opendir,
        .release        = nfs_release,
+       .fsync          = nfs_fsync_dir,
 };
 
 struct inode_operations nfs_dir_inode_operations = {
@@ -290,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...
  */
@@ -317,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;
@@ -329,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);
@@ -348,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;
 }
@@ -490,6 +491,15 @@ static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
        return 0;
 }
 
+/*
+ * 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)
+{
+       return 0;
+}
+
 /*
  * A check for whether or not the parent directory has changed.
  * In the case it has, we assume that the dentries are untrustworthy
@@ -602,33 +612,18 @@ 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 (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:
@@ -710,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;
@@ -718,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();
@@ -733,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
@@ -785,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;
@@ -815,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:
@@ -828,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);
 }
@@ -850,22 +847,22 @@ 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);
 
@@ -887,87 +884,57 @@ 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_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;
 }
 
 /*
@@ -982,12 +949,18 @@ 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;
        }
+       if (!(fattr->valid & NFS_ATTR_FATTR)) {
+               struct nfs_server *server = NFS_SB(dentry->d_sb);
+               error = server->rpc_ops->getattr(server, fhandle, fattr);
+               if (error < 0)
+                       goto out_err;
+       }
        inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
        if (inode) {
                d_instantiate(dentry, inode);
@@ -1024,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);
@@ -1299,19 +1266,6 @@ nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
        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;
-       }
-
 #ifdef NFS_PARANOIA
 if (dentry->d_inode)
 printk("nfs_proc_symlink: %s/%s not negative!\n",
@@ -1341,8 +1295,6 @@ dentry->d_parent->d_name.name, dentry->d_name.name);
                d_drop(dentry);
        }
        unlock_kernel();
-
-out:
        return error;
 }
 
@@ -1432,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,
@@ -1447,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,
@@ -1498,34 +1448,63 @@ out:
        return error;
 }
 
-int
-nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
+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;
+
+       if (cache->cred != cred
+                       || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
+                       || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
+               return -ENOENT;
+       memcpy(res, cache, sizeof(*res));
+       return 0;
+}
+
+void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
+{
+       struct nfs_access_entry *cache = &NFS_I(inode)->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;
+       }
+       cache->jiffies = set->jiffies;
+       cache->mask = set->mask;
+}
+
+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;
 
        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() */
@@ -1542,39 +1521,13 @@ nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
                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:
+       res = nfs_do_access(inode, cred, mask);
        put_rpccred(cred);
        unlock_kernel();
        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 = generic_permission(inode, mask, NULL);
        unlock_kernel();
        return res;
 }