[PATCH] r/o bind mounts: elevate write count for rmdir and unlink.
[safe/jmp/linux-2.6] / fs / nfsd / vfs.c
index 8d6b5c4..efff58a 100644 (file)
 #define NFSDDBG_FACILITY               NFSDDBG_FILEOP
 
 
-/* We must ignore files (but only files) which might have mandatory
- * locks on them because there is no way to know if the accesser has
- * the lock.
- */
-#define IS_ISMNDLK(i)  (S_ISREG((i)->i_mode) && MANDATORY_LOCK(i))
-
 /*
  * This is a cache of readahead params that help us choose the proper
  * readahead strategy. Initially, we set all readahead parameters to 0
@@ -107,27 +101,28 @@ nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
 {
        struct svc_export *exp = *expp, *exp2 = NULL;
        struct dentry *dentry = *dpp;
-       struct vfsmount *mnt = mntget(exp->ex_mnt);
+       struct vfsmount *mnt = mntget(exp->ex_path.mnt);
        struct dentry *mounts = dget(dentry);
        int err = 0;
 
        while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
 
-       exp2 = exp_get_by_name(exp->ex_client, mnt, mounts, &rqstp->rq_chandle);
+       exp2 = rqst_exp_get_by_name(rqstp, mnt, mounts);
        if (IS_ERR(exp2)) {
-               err = PTR_ERR(exp2);
+               if (PTR_ERR(exp2) != -ENOENT)
+                       err = PTR_ERR(exp2);
                dput(mounts);
                mntput(mnt);
                goto out;
        }
-       if (exp2 && ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2))) {
+       if ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
                /* successfully crossed mount point */
                exp_put(exp);
                *expp = exp2;
                dput(dentry);
                *dpp = mounts;
        } else {
-               if (exp2) exp_put(exp2);
+               exp_put(exp2);
                dput(mounts);
        }
        mntput(mnt);
@@ -135,21 +130,10 @@ out:
        return err;
 }
 
-/*
- * Look up one component of a pathname.
- * N.B. After this call _both_ fhp and resfh need an fh_put
- *
- * If the lookup would cross a mountpoint, and the mounted filesystem
- * is exported to the client with NFSEXP_NOHIDE, then the lookup is
- * accepted as it stands and the mounted directory is
- * returned. Otherwise the covered directory is returned.
- * NOTE: this mountpoint crossing is not supported properly by all
- *   clients and is explicitly disallowed for NFSv3
- *      NeilBrown <neilb@cse.unsw.edu.au>
- */
 __be32
-nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
-                                       int len, struct svc_fh *resfh)
+nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
+                  const char *name, unsigned int len,
+                  struct svc_export **exp_ret, struct dentry **dentry_ret)
 {
        struct svc_export       *exp;
        struct dentry           *dparent;
@@ -168,21 +152,19 @@ nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
        exp  = fhp->fh_export;
        exp_get(exp);
 
-       err = nfserr_acces;
-
        /* Lookup the name, but don't follow links */
        if (isdotent(name, len)) {
                if (len==1)
                        dentry = dget(dparent);
-               else if (dparent != exp->ex_dentry) {
+               else if (dparent != exp->ex_path.dentry)
                        dentry = dget_parent(dparent);
-               } else  if (!EX_NOHIDE(exp))
+               else if (!EX_NOHIDE(exp))
                        dentry = dget(dparent); /* .. == . just like at / */
                else {
                        /* checking mountpoint crossing is very different when stepping up */
                        struct svc_export *exp2 = NULL;
                        struct dentry *dp;
-                       struct vfsmount *mnt = mntget(exp->ex_mnt);
+                       struct vfsmount *mnt = mntget(exp->ex_path.mnt);
                        dentry = dget(dparent);
                        while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
                                ;
@@ -190,17 +172,15 @@ nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
                        dput(dentry);
                        dentry = dp;
 
-                       exp2 = exp_parent(exp->ex_client, mnt, dentry,
-                                         &rqstp->rq_chandle);
-                       if (IS_ERR(exp2)) {
+                       exp2 = rqst_exp_parent(rqstp, mnt, dentry);
+                       if (PTR_ERR(exp2) == -ENOENT) {
+                               dput(dentry);
+                               dentry = dget(dparent);
+                       } else if (IS_ERR(exp2)) {
                                host_err = PTR_ERR(exp2);
                                dput(dentry);
                                mntput(mnt);
                                goto out_nfserr;
-                       }
-                       if (!exp2) {
-                               dput(dentry);
-                               dentry = dget(dparent);
                        } else {
                                exp_put(exp);
                                exp = exp2;
@@ -223,6 +203,41 @@ nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
                        }
                }
        }
+       *dentry_ret = dentry;
+       *exp_ret = exp;
+       return 0;
+
+out_nfserr:
+       exp_put(exp);
+       return nfserrno(host_err);
+}
+
+/*
+ * Look up one component of a pathname.
+ * N.B. After this call _both_ fhp and resfh need an fh_put
+ *
+ * If the lookup would cross a mountpoint, and the mounted filesystem
+ * is exported to the client with NFSEXP_NOHIDE, then the lookup is
+ * accepted as it stands and the mounted directory is
+ * returned. Otherwise the covered directory is returned.
+ * NOTE: this mountpoint crossing is not supported properly by all
+ *   clients and is explicitly disallowed for NFSv3
+ *      NeilBrown <neilb@cse.unsw.edu.au>
+ */
+__be32
+nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
+                               unsigned int len, struct svc_fh *resfh)
+{
+       struct svc_export       *exp;
+       struct dentry           *dentry;
+       __be32 err;
+
+       err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
+       if (err)
+               return err;
+       err = check_nfsd_access(exp, rqstp);
+       if (err)
+               goto out;
        /*
         * Note: we compose the file handle now, but as the
         * dentry may be negative, it may need to be updated.
@@ -230,16 +245,13 @@ nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
        err = fh_compose(resfh, exp, dentry, fhp);
        if (!err && !dentry->d_inode)
                err = nfserr_noent;
-       dput(dentry);
 out:
+       dput(dentry);
        exp_put(exp);
        return err;
-
-out_nfserr:
-       err = nfserrno(host_err);
-       goto out;
 }
 
+
 /*
  * Set various file attributes.
  * N.B. After this call fhp needs an fh_put
@@ -277,7 +289,8 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
        if (!iap->ia_valid)
                goto out;
 
-       /* NFSv2 does not differentiate between "set-[ac]time-to-now"
+       /*
+        * NFSv2 does not differentiate between "set-[ac]time-to-now"
         * which only requires access, and "set-[ac]time-to-X" which
         * requires ownership.
         * So if it looks like it might be "set both to the same time which
@@ -290,28 +303,36 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
         */
 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
 #define        MAX_TOUCH_TIME_ERROR (30*60)
-       if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET
-           && iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec
-           ) {
-           /* Looks probable.  Now just make sure time is in the right ballpark.
-            * Solaris, at least, doesn't seem to care what the time request is.
-            * We require it be within 30 minutes of now.
-            */
-           time_t delta = iap->ia_atime.tv_sec - get_seconds();
-           if (delta<0) delta = -delta;
-           if (delta < MAX_TOUCH_TIME_ERROR &&
-               inode_change_ok(inode, iap) != 0) {
-               /* turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME
-                * this will cause notify_change to set these times to "now"
+       if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
+           iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
+               /*
+                * Looks probable.
+                *
+                * Now just make sure time is in the right ballpark.
+                * Solaris, at least, doesn't seem to care what the time
+                * request is.  We require it be within 30 minutes of now.
                 */
-               iap->ia_valid &= ~BOTH_TIME_SET;
-           }
+               time_t delta = iap->ia_atime.tv_sec - get_seconds();
+               if (delta < 0)
+                       delta = -delta;
+               if (delta < MAX_TOUCH_TIME_ERROR &&
+                   inode_change_ok(inode, iap) != 0) {
+                       /*
+                        * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
+                        * This will cause notify_change to set these times
+                        * to "now"
+                        */
+                       iap->ia_valid &= ~BOTH_TIME_SET;
+               }
        }
            
-       /* The size case is special. It changes the file as well as the attributes.  */
+       /*
+        * The size case is special.
+        * It changes the file as well as the attributes.
+        */
        if (iap->ia_valid & ATTR_SIZE) {
                if (iap->ia_size < inode->i_size) {
-                       err = nfsd_permission(fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
+                       err = nfsd_permission(rqstp, fhp->fh_export, dentry, MAY_TRUNC|MAY_OWNER_OVERRIDE);
                        if (err)
                                goto out;
                }
@@ -343,14 +364,23 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
        if (iap->ia_valid & ATTR_MODE) {
                iap->ia_mode &= S_IALLUGO;
                imode = iap->ia_mode |= (imode & ~S_IALLUGO);
+               /* if changing uid/gid revoke setuid/setgid in mode */
+               if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) {
+                       iap->ia_valid |= ATTR_KILL_PRIV;
+                       iap->ia_mode &= ~S_ISUID;
+               }
+               if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
+                       iap->ia_mode &= ~S_ISGID;
+       } else {
+               /*
+                * Revoke setuid/setgid bit on chown/chgrp
+                */
+               if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
+                       iap->ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV;
+               if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
+                       iap->ia_valid |= ATTR_KILL_SGID;
        }
 
-       /* Revoke setuid/setgid bit on chown/chgrp */
-       if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
-               iap->ia_valid |= ATTR_KILL_SUID;
-       if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
-               iap->ia_valid |= ATTR_KILL_SGID;
-
        /* Change the attributes. */
 
        iap->ia_valid |= ATTR_CTIME;
@@ -599,7 +629,7 @@ nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *suppor
 
                        sresult |= map->access;
 
-                       err2 = nfsd_permission(export, dentry, map->how);
+                       err2 = nfsd_permission(rqstp, export, dentry, map->how);
                        switch (err2) {
                        case nfs_ok:
                                result |= map->access;
@@ -662,7 +692,12 @@ nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
        err = nfserr_perm;
        if (IS_APPEND(inode) && (access & MAY_WRITE))
                goto out;
-       if (IS_ISMNDLK(inode))
+       /*
+        * We must ignore files (but only files) which might have mandatory
+        * locks on them because there is no way to know if the accesser has
+        * the lock.
+        */
+       if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
                goto out;
 
        if (!inode->i_fop)
@@ -686,7 +721,8 @@ nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 
                DQUOT_INIT(inode);
        }
-       *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_mnt), flags);
+       *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
+                               flags);
        if (IS_ERR(*filp))
                host_err = PTR_ERR(*filp);
 out_nfserr:
@@ -839,6 +875,15 @@ static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
        return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
 }
 
+static inline int svc_msnfs(struct svc_fh *ffhp)
+{
+#ifdef MSNFS
+       return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
+#else
+       return 0;
+#endif
+}
+
 static __be32
 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)
@@ -851,11 +896,9 @@ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
 
        err = nfserr_perm;
        inode = file->f_path.dentry->d_inode;
-#ifdef MSNFS
-       if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
-               (!lock_may_read(inode, offset, *count)))
+
+       if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
                goto out;
-#endif
 
        /* Get readahead parameters */
        ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
@@ -904,7 +947,7 @@ out:
 static void kill_suid(struct dentry *dentry)
 {
        struct iattr    ia;
-       ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
+       ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
 
        mutex_lock(&dentry->d_inode->i_mutex);
        notify_change(dentry, &ia);
@@ -987,13 +1030,13 @@ nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
                if (EX_WGATHER(exp)) {
                        if (atomic_read(&inode->i_writecount) > 1
                            || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
-                               dprintk("nfsd: write defer %d\n", current->pid);
+                               dprintk("nfsd: write defer %d\n", task_pid_nr(current));
                                msleep(10);
-                               dprintk("nfsd: write resume %d\n", current->pid);
+                               dprintk("nfsd: write resume %d\n", task_pid_nr(current));
                        }
 
                        if (inode->i_state & I_DIRTY) {
-                               dprintk("nfsd: write sync %d\n", current->pid);
+                               dprintk("nfsd: write sync %d\n", task_pid_nr(current));
                                host_err=nfsd_sync(file);
                        }
 #if 0
@@ -1026,7 +1069,7 @@ nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
        __be32          err;
 
        if (file) {
-               err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
+               err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
                                MAY_READ|MAY_OWNER_OVERRIDE);
                if (err)
                        goto out;
@@ -1055,7 +1098,7 @@ nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
        __be32                  err = 0;
 
        if (file) {
-               err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
+               err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
                                MAY_WRITE|MAY_OWNER_OVERRIDE);
                if (err)
                        goto out;
@@ -1109,6 +1152,26 @@ nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
 }
 #endif /* CONFIG_NFSD_V3 */
 
+__be32
+nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
+                       struct iattr *iap)
+{
+       /*
+        * Mode has already been set earlier in create:
+        */
+       iap->ia_valid &= ~ATTR_MODE;
+       /*
+        * Setting uid/gid works only for root.  Irix appears to
+        * send along the gid on create when it tries to implement
+        * setgid directories via NFS:
+        */
+       if (current->fsuid != 0)
+               iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
+       if (iap->ia_valid)
+               return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
+       return 0;
+}
+
 /*
  * Create a file (regular, directory, device, fifo); UNIX sockets 
  * not yet implemented.
@@ -1125,6 +1188,7 @@ nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
        struct dentry   *dentry, *dchild = NULL;
        struct inode    *dirp;
        __be32          err;
+       __be32          err2;
        int             host_err;
 
        err = nfserr_perm;
@@ -1215,16 +1279,9 @@ nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
        }
 
 
-       /* Set file attributes. Mode has already been set and
-        * setting uid/gid works only for root. Irix appears to
-        * send along the gid when it tries to implement setgid
-        * directories via NFS.
-        */
-       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
-               __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
-               if (err2)
-                       err = err2;
-       }
+       err2 = nfsd_create_setattr(rqstp, resfhp, iap);
+       if (err2)
+               err = err2;
        /*
         * Update the file handle to get the new inode info.
         */
@@ -1253,6 +1310,7 @@ nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
        struct dentry   *dentry, *dchild = NULL;
        struct inode    *dirp;
        __be32          err;
+       __be32          err2;
        int             host_err;
        __u32           v_mtime=0, v_atime=0;
 
@@ -1292,7 +1350,10 @@ nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
 
        if (createmode == NFS3_CREATE_EXCLUSIVE) {
                /* solaris7 gets confused (bugid 4218508) if these have
-                * the high bit set, so just clear the high bits.
+                * the high bit set, so just clear the high bits. If this is
+                * ever changed to use different attrs for storing the
+                * verifier, then do_open_lookup() will also need to be fixed
+                * accordingly.
                 */
                v_mtime = verifier[0]&0x7fffffff;
                v_atime = verifier[1]&0x7fffffff;
@@ -1354,16 +1415,10 @@ nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
                iap->ia_atime.tv_nsec = 0;
        }
 
-       /* Set file attributes.
-        * Irix appears to send along the gid when it tries to
-        * implement setgid directories via NFS. Clear out all that cruft.
-        */
  set_attr:
-       if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
-               __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
-               if (err2)
-                       err = err2;
-       }
+       err2 = nfsd_create_setattr(rqstp, resfhp, iap);
+       if (err2)
+               err = err2;
 
        /*
         * Update the filehandle to get the new inode info.
@@ -1408,7 +1463,7 @@ nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
        if (!inode->i_op || !inode->i_op->readlink)
                goto out;
 
-       touch_atime(fhp->fh_export->ex_mnt, dentry);
+       touch_atime(fhp->fh_export->ex_path.mnt, dentry);
        /* N.B. Why does this call need a get_fs()??
         * Remove the set_fs and watch the fireworks:-) --okir
         */
@@ -1695,6 +1750,10 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
        if (!type)
                type = rdentry->d_inode->i_mode & S_IFMT;
 
+       host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
+       if (host_err)
+               goto out_nfserr;
+
        if (type != S_IFDIR) { /* It's UNLINK */
 #ifdef MSNFS
                if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
@@ -1710,10 +1769,12 @@ nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
        dput(rdentry);
 
        if (host_err)
-               goto out_nfserr;
+               goto out_drop;
        if (EX_ISSYNC(fhp->fh_export))
                host_err = nfsd_sync_dir(dentry);
 
+out_drop:
+       mnt_drop_write(fhp->fh_export->ex_path.mnt);
 out_nfserr:
        err = nfserrno(host_err);
 out:
@@ -1780,11 +1841,17 @@ nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
        return err;
 }
 
+static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
+{
+       return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
+}
+
 /*
  * Check for a user's access permissions to this inode.
  */
 __be32
-nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
+nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
+                                       struct dentry *dentry, int acc)
 {
        struct inode    *inode = dentry->d_inode;
        int             err;
@@ -1815,7 +1882,7 @@ nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
         */
        if (!(acc & MAY_LOCAL_ACCESS))
                if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
-                       if (EX_RDONLY(exp) || IS_RDONLY(inode))
+                       if (exp_rdonly(rqstp, exp) || IS_RDONLY(inode))
                                return nfserr_rofs;
                        if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
                                return nfserr_perm;
@@ -1898,7 +1965,7 @@ nfsd_racache_init(int cache_size)
                raparm_hash[i].pb_head = NULL;
                spin_lock_init(&raparm_hash[i].pb_lock);
        }
-       nperbucket = cache_size >> RAPARM_HASH_BITS;
+       nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
        for (i = 0; i < cache_size - 1; i++) {
                if (i % nperbucket == 0)
                        raparm_hash[j++].pb_head = raparml + i;