]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - fs/nfsd/vfs.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial
[linux-2.6.git] / fs / nfsd / vfs.c
index f83ab4cf426503d4b6714035e8b5601817e960c7..c9e3b5a8fe07d6ce8a790f668da75898cb0474f7 100644 (file)
@@ -16,7 +16,6 @@
  * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  */
 
-#include <linux/config.h>
 #include <linux/string.h>
 #include <linux/time.h>
 #include <linux/errno.h>
@@ -48,8 +47,8 @@
 #include <linux/fsnotify.h>
 #include <linux/posix_acl.h>
 #include <linux/posix_acl_xattr.h>
-#ifdef CONFIG_NFSD_V4
 #include <linux/xattr.h>
+#ifdef CONFIG_NFSD_V4
 #include <linux/nfs4.h>
 #include <linux/nfs4_acl.h>
 #include <linux/nfsd_idmap.h>
@@ -365,8 +364,26 @@ out_nfserr:
        goto out;
 }
 
-#if defined(CONFIG_NFSD_V4)
+#if defined(CONFIG_NFSD_V2_ACL) || \
+    defined(CONFIG_NFSD_V3_ACL) || \
+    defined(CONFIG_NFSD_V4)
+static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
+{
+       ssize_t buflen;
+
+       buflen = vfs_getxattr(dentry, key, NULL, 0);
+       if (buflen <= 0)
+               return buflen;
+
+       *buf = kmalloc(buflen, GFP_KERNEL);
+       if (!*buf)
+               return -ENOMEM;
+
+       return vfs_getxattr(dentry, key, *buf, buflen);
+}
+#endif
 
+#if defined(CONFIG_NFSD_V4)
 static int
 set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
 {
@@ -374,7 +391,6 @@ set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
        size_t buflen;
        char *buf = NULL;
        int error = 0;
-       struct inode *inode = dentry->d_inode;
 
        buflen = posix_acl_xattr_size(pacl->a_count);
        buf = kmalloc(buflen, GFP_KERNEL);
@@ -388,15 +404,7 @@ set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
                goto out;
        }
 
-       error = -EOPNOTSUPP;
-       if (inode->i_op && inode->i_op->setxattr) {
-               down(&inode->i_sem);
-               security_inode_setxattr(dentry, key, buf, len, 0);
-               error = inode->i_op->setxattr(dentry, key, buf, len, 0);
-               if (!error)
-                       security_inode_post_setxattr(dentry, key, buf, len, 0);
-               up(&inode->i_sem);
-       }
+       error = vfs_setxattr(dentry, key, buf, len, 0);
 out:
        kfree(buf);
        return error;
@@ -455,44 +463,19 @@ out_nfserr:
 static struct posix_acl *
 _get_posix_acl(struct dentry *dentry, char *key)
 {
-       struct inode *inode = dentry->d_inode;
-       char *buf = NULL;
-       int buflen, error = 0;
+       void *buf = NULL;
        struct posix_acl *pacl = NULL;
+       int buflen;
 
-       error = -EOPNOTSUPP;
-       if (inode->i_op == NULL)
-               goto out_err;
-       if (inode->i_op->getxattr == NULL)
-               goto out_err;
-
-       error = security_inode_getxattr(dentry, key);
-       if (error)
-               goto out_err;
-
-       buflen = inode->i_op->getxattr(dentry, key, NULL, 0);
-       if (buflen <= 0) {
-               error = buflen < 0 ? buflen : -ENODATA;
-               goto out_err;
-       }
-
-       buf = kmalloc(buflen, GFP_KERNEL);
-       if (buf == NULL) {
-               error = -ENOMEM;
-               goto out_err;
-       }
-
-       error = inode->i_op->getxattr(dentry, key, buf, buflen);
-       if (error < 0)
-               goto out_err;
+       buflen = nfsd_getxattr(dentry, key, &buf);
+       if (!buflen)
+               buflen = -ENODATA;
+       if (buflen <= 0)
+               return ERR_PTR(buflen);
 
        pacl = posix_acl_from_xattr(buf, buflen);
- out:
        kfree(buf);
        return pacl;
- out_err:
-       pacl = ERR_PTR(error);
-       goto out;
 }
 
 int
@@ -689,7 +672,10 @@ nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
                goto out_nfserr;
 
        if (access & MAY_WRITE) {
-               flags = O_WRONLY|O_LARGEFILE;
+               if (access & MAY_READ)
+                       flags = O_RDWR|O_LARGEFILE;
+               else
+                       flags = O_WRONLY|O_LARGEFILE;
 
                DQUOT_INIT(inode);
        }
@@ -718,18 +704,19 @@ nfsd_close(struct file *filp)
  * after it.
  */
 static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
-                             struct file_operations *fop)
+                             const struct file_operations *fop)
 {
        struct inode *inode = dp->d_inode;
        int (*fsync) (struct file *, struct dentry *, int);
-       int err = nfs_ok;
+       int err;
 
-       filemap_fdatawrite(inode->i_mapping);
-       if (fop && (fsync = fop->fsync))
-               err=fsync(filp, dp, 0);
-       filemap_fdatawait(inode->i_mapping);
+       err = filemap_fdatawrite(inode->i_mapping);
+       if (err == 0 && fop && (fsync = fop->fsync))
+               err = fsync(filp, dp, 0);
+       if (err == 0)
+               err = filemap_fdatawait(inode->i_mapping);
 
-       return nfserrno(err);
+       return err;
 }
        
 
@@ -739,17 +726,17 @@ nfsd_sync(struct file *filp)
         int err;
        struct inode *inode = filp->f_dentry->d_inode;
        dprintk("nfsd: sync file %s\n", filp->f_dentry->d_name.name);
-       down(&inode->i_sem);
+       mutex_lock(&inode->i_mutex);
        err=nfsd_dosync(filp, filp->f_dentry, filp->f_op);
-       up(&inode->i_sem);
+       mutex_unlock(&inode->i_mutex);
 
        return err;
 }
 
-void
+int
 nfsd_sync_dir(struct dentry *dp)
 {
-       nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
+       return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
 }
 
 /*
@@ -826,7 +813,7 @@ nfsd_read_actor(read_descriptor_t *desc, struct page *page, unsigned long offset
        return size;
 }
 
-static inline int
+static int
 nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
               loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
 {
@@ -849,7 +836,7 @@ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
        if (ra && ra->p_set)
                file->f_ra = ra->p_ra;
 
-       if (file->f_op->sendfile) {
+       if (file->f_op->sendfile && rqstp->rq_sendfile_ok) {
                svc_pushback_unused_pages(rqstp);
                err = file->f_op->sendfile(file, &offset, *count,
                                                 nfsd_read_actor, rqstp);
@@ -880,7 +867,17 @@ out:
        return err;
 }
 
-static inline int
+static void kill_suid(struct dentry *dentry)
+{
+       struct iattr    ia;
+       ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
+
+       mutex_lock(&dentry->d_inode->i_mutex);
+       notify_change(dentry, &ia);
+       mutex_unlock(&dentry->d_inode->i_mutex);
+}
+
+static int
 nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
                                loff_t offset, struct kvec *vec, int vlen,
                                unsigned long cnt, int *stablep)
@@ -892,9 +889,9 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
        int                     err = 0;
        int                     stable = *stablep;
 
+#ifdef MSNFS
        err = nfserr_perm;
 
-#ifdef MSNFS
        if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
                (!lock_may_write(file->f_dentry->d_inode, offset, cnt)))
                goto out;
@@ -933,14 +930,8 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
        }
 
        /* clear setuid/setgid flag after write */
-       if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID))) {
-               struct iattr    ia;
-               ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
-
-               down(&inode->i_sem);
-               notify_change(dentry, &ia);
-               up(&inode->i_sem);
-       }
+       if (err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
+               kill_suid(dentry);
 
        if (err >= 0 && stable) {
                static ino_t    last_ino;
@@ -1072,7 +1063,7 @@ nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
                return err;
        if (EX_ISSYNC(fhp->fh_export)) {
                if (file->f_op && file->f_op->fsync) {
-                       err = nfsd_sync(file);
+                       err = nfserrno(nfsd_sync(file));
                } else {
                        err = nfserr_notsupp;
                }
@@ -1140,7 +1131,7 @@ nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
                                "nfsd_create: parent %s/%s not locked!\n",
                                dentry->d_parent->d_name.name,
                                dentry->d_name.name);
-                       err = -EIO;
+                       err = nfserr_io;
                        goto out;
                }
        }
@@ -1183,7 +1174,7 @@ nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
                goto out_nfserr;
 
        if (EX_ISSYNC(fhp->fh_export)) {
-               nfsd_sync_dir(dentry);
+               err = nfserrno(nfsd_sync_dir(dentry));
                write_inode_now(dchild->d_inode, 1);
        }
 
@@ -1193,9 +1184,11 @@ nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
         * send along the gid when it tries to implement setgid
         * directories via NFS.
         */
-       err = 0;
-       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0)
-               err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
+       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
+               int err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
+               if (err2)
+                       err = err2;
+       }
        /*
         * Update the file handle to get the new inode info.
         */
@@ -1314,17 +1307,10 @@ nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
                goto out_nfserr;
 
        if (EX_ISSYNC(fhp->fh_export)) {
-               nfsd_sync_dir(dentry);
+               err = nfserrno(nfsd_sync_dir(dentry));
                /* setattr will sync the child (or not) */
        }
 
-       /*
-        * Update the filehandle to get the new inode info.
-        */
-       err = fh_update(resfhp);
-       if (err)
-               goto out;
-
        if (createmode == NFS3_CREATE_EXCLUSIVE) {
                /* Cram the verifier into atime/mtime/mode */
                iap->ia_valid = ATTR_MTIME|ATTR_ATIME
@@ -1345,8 +1331,17 @@ nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
         * implement setgid directories via NFS. Clear out all that cruft.
         */
  set_attr:
-       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0)
-               err = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
+       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID)) != 0) {
+               int err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
+               if (err2)
+                       err = err2;
+       }
+
+       /*
+        * Update the filehandle to get the new inode info.
+        */
+       if (!err)
+               err = fh_update(resfhp);
 
  out:
        fh_unlock(fhp);
@@ -1455,10 +1450,10 @@ nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
        } else
                err = vfs_symlink(dentry->d_inode, dnew, path, mode);
 
-       if (!err) {
+       if (!err)
                if (EX_ISSYNC(fhp->fh_export))
-                       nfsd_sync_dir(dentry);
-       } else
+                       err = nfsd_sync_dir(dentry);
+       if (err)
                err = nfserrno(err);
        fh_unlock(fhp);
 
@@ -1514,7 +1509,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
        err = vfs_link(dold, dirp, dnew);
        if (!err) {
                if (EX_ISSYNC(ffhp->fh_export)) {
-                       nfsd_sync_dir(ddir);
+                       err = nfserrno(nfsd_sync_dir(ddir));
                        write_inode_now(dest, 1);
                }
        } else {
@@ -1524,14 +1519,15 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
                        err = nfserrno(err);
        }
 
-       fh_unlock(ffhp);
        dput(dnew);
+out_unlock:
+       fh_unlock(ffhp);
 out:
        return err;
 
 out_nfserr:
        err = nfserrno(err);
-       goto out;
+       goto out_unlock;
 }
 
 /*
@@ -1560,7 +1556,7 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
        tdir = tdentry->d_inode;
 
        err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
-       if (fdir->i_sb != tdir->i_sb)
+       if (ffhp->fh_export != tfhp->fh_export)
                goto out;
 
        err = nfserr_perm;
@@ -1598,13 +1594,14 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
        if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
                ((atomic_read(&odentry->d_count) > 1)
                 || (atomic_read(&ndentry->d_count) > 1))) {
-                       err = nfserr_perm;
+                       err = -EPERM;
        } else
 #endif
        err = vfs_rename(fdir, odentry, tdir, ndentry);
        if (!err && EX_ISSYNC(tfhp->fh_export)) {
-               nfsd_sync_dir(tdentry);
-               nfsd_sync_dir(fdentry);
+               err = nfsd_sync_dir(tdentry);
+               if (!err)
+                       err = nfsd_sync_dir(fdentry);
        }
 
  out_dput_new:
@@ -1669,7 +1666,7 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 #ifdef MSNFS
                if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
                        (atomic_read(&rdentry->d_count) > 1)) {
-                       err = nfserr_perm;
+                       err = -EPERM;
                } else
 #endif
                err = vfs_unlink(dirp, rdentry);
@@ -1679,17 +1676,14 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 
        dput(rdentry);
 
-       if (err)
-               goto out_nfserr;
-       if (EX_ISSYNC(fhp->fh_export)) 
-               nfsd_sync_dir(dentry);
-
-out:
-       return err;
+       if (err == 0 &&
+           EX_ISSYNC(fhp->fh_export))
+                       err = nfsd_sync_dir(dentry);
 
 out_nfserr:
        err = nfserrno(err);
-       goto out;
+out:
+       return err;
 }
 
 /*
@@ -1746,7 +1740,7 @@ int
 nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
 {
        int err = fh_verify(rqstp, fhp, 0, MAY_NOP);
-       if (!err && vfs_statfs(fhp->fh_dentry->d_inode->i_sb,stat))
+       if (!err && vfs_statfs(fhp->fh_dentry,stat))
                err = nfserr_io;
        return err;
 }
@@ -1880,39 +1874,25 @@ nfsd_get_posix_acl(struct svc_fh *fhp, int type)
        ssize_t size;
        struct posix_acl *acl;
 
-       if (!IS_POSIXACL(inode) || !inode->i_op || !inode->i_op->getxattr)
+       if (!IS_POSIXACL(inode))
+               return ERR_PTR(-EOPNOTSUPP);
+
+       switch (type) {
+       case ACL_TYPE_ACCESS:
+               name = POSIX_ACL_XATTR_ACCESS;
+               break;
+       case ACL_TYPE_DEFAULT:
+               name = POSIX_ACL_XATTR_DEFAULT;
+               break;
+       default:
                return ERR_PTR(-EOPNOTSUPP);
-       switch(type) {
-               case ACL_TYPE_ACCESS:
-                       name = POSIX_ACL_XATTR_ACCESS;
-                       break;
-               case ACL_TYPE_DEFAULT:
-                       name = POSIX_ACL_XATTR_DEFAULT;
-                       break;
-               default:
-                       return ERR_PTR(-EOPNOTSUPP);
        }
 
-       size = inode->i_op->getxattr(fhp->fh_dentry, name, NULL, 0);
+       size = nfsd_getxattr(fhp->fh_dentry, name, &value);
+       if (size < 0)
+               return ERR_PTR(size);
 
-       if (size < 0) {
-               acl = ERR_PTR(size);
-               goto getout;
-       } else if (size > 0) {
-               value = kmalloc(size, GFP_KERNEL);
-               if (!value) {
-                       acl = ERR_PTR(-ENOMEM);
-                       goto getout;
-               }
-               size = inode->i_op->getxattr(fhp->fh_dentry, name, value, size);
-               if (size < 0) {
-                       acl = ERR_PTR(size);
-                       goto getout;
-               }
-       }
        acl = posix_acl_from_xattr(value, size);
-
-getout:
        kfree(value);
        return acl;
 }
@@ -1945,24 +1925,20 @@ nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
                value = kmalloc(size, GFP_KERNEL);
                if (!value)
                        return -ENOMEM;
-               size = posix_acl_to_xattr(acl, value, size);
-               if (size < 0) {
-                       error = size;
+               error = posix_acl_to_xattr(acl, value, size);
+               if (error < 0)
                        goto getout;
-               }
+               size = error;
        } else
                size = 0;
 
-       if (!fhp->fh_locked)
-               fh_lock(fhp);  /* unlocking is done automatically */
        if (size)
-               error = inode->i_op->setxattr(fhp->fh_dentry, name,
-                                             value, size, 0);
+               error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
        else {
                if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
                        error = 0;
                else {
-                       error = inode->i_op->removexattr(fhp->fh_dentry, name);
+                       error = vfs_removexattr(fhp->fh_dentry, name);
                        if (error == -ENODATA)
                                error = 0;
                }