Commit af90f707 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by J. Bruce Fields

nfsd: take struct file setup fully into nfs4_preprocess_stateid_op

This patch changes nfs4_preprocess_stateid_op so it always returns
a valid struct file if it has been asked for that.  For that we
now allocate a temporary struct file for special stateids, and check
permissions if we got the file structure from the stateid.  This
ensures that all callers will get their handling of special stateids
right, and avoids code duplication.

There is a little wart in here because the read code needs to know
if we allocated a file structure so that it can copy around the
read-ahead parameters.  In the long run we should probably aim to
cache full file structures used with special stateids instead.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJ. Bruce Fields <bfields@redhat.com>
parent a0649b2d
...@@ -760,8 +760,6 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -760,8 +760,6 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
{ {
__be32 status; __be32 status;
/* no need to check permission - this will be done in nfsd_read() */
read->rd_filp = NULL; read->rd_filp = NULL;
if (read->rd_offset >= OFFSET_MAX) if (read->rd_offset >= OFFSET_MAX)
return nfserr_inval; return nfserr_inval;
...@@ -778,9 +776,9 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -778,9 +776,9 @@ nfsd4_read(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags); clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
/* check stateid */ /* check stateid */
if ((status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), status = nfs4_preprocess_stateid_op(rqstp, cstate, &read->rd_stateid,
cstate, &read->rd_stateid, RD_STATE, &read->rd_filp, &read->rd_tmp_file);
RD_STATE, &read->rd_filp))) { if (status) {
dprintk("NFSD: nfsd4_read: couldn't process stateid!\n"); dprintk("NFSD: nfsd4_read: couldn't process stateid!\n");
goto out; goto out;
} }
...@@ -924,8 +922,8 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -924,8 +922,8 @@ nfsd4_setattr(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
int err; int err;
if (setattr->sa_iattr.ia_valid & ATTR_SIZE) { if (setattr->sa_iattr.ia_valid & ATTR_SIZE) {
status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate, status = nfs4_preprocess_stateid_op(rqstp, cstate,
&setattr->sa_stateid, WR_STATE, NULL); &setattr->sa_stateid, WR_STATE, NULL, NULL);
if (status) { if (status) {
dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n"); dprintk("NFSD: nfsd4_setattr: couldn't process stateid!\n");
return status; return status;
...@@ -986,13 +984,11 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -986,13 +984,11 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
unsigned long cnt; unsigned long cnt;
int nvecs; int nvecs;
/* no need to check permission - this will be done in nfsd_write() */
if (write->wr_offset >= OFFSET_MAX) if (write->wr_offset >= OFFSET_MAX)
return nfserr_inval; return nfserr_inval;
status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), status = nfs4_preprocess_stateid_op(rqstp, cstate, stateid, WR_STATE,
cstate, stateid, WR_STATE, &filp); &filp, NULL);
if (status) { if (status) {
dprintk("NFSD: nfsd4_write: couldn't process stateid!\n"); dprintk("NFSD: nfsd4_write: couldn't process stateid!\n");
return status; return status;
...@@ -1005,10 +1001,9 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -1005,10 +1001,9 @@ nfsd4_write(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
nvecs = fill_in_write_vector(rqstp->rq_vec, write); nvecs = fill_in_write_vector(rqstp->rq_vec, write);
WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec)); WARN_ON_ONCE(nvecs > ARRAY_SIZE(rqstp->rq_vec));
status = nfsd_write(rqstp, &cstate->current_fh, filp, status = nfsd_vfs_write(rqstp, &cstate->current_fh, filp,
write->wr_offset, rqstp->rq_vec, nvecs, write->wr_offset, rqstp->rq_vec, nvecs, &cnt,
&cnt, &write->wr_how_written); &write->wr_how_written);
if (filp)
fput(filp); fput(filp);
write->wr_bytes_written = cnt; write->wr_bytes_written = cnt;
...@@ -1023,15 +1018,13 @@ nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -1023,15 +1018,13 @@ nfsd4_fallocate(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
__be32 status = nfserr_notsupp; __be32 status = nfserr_notsupp;
struct file *file; struct file *file;
status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate, status = nfs4_preprocess_stateid_op(rqstp, cstate,
&fallocate->falloc_stateid, &fallocate->falloc_stateid,
WR_STATE, &file); WR_STATE, &file, NULL);
if (status != nfs_ok) { if (status != nfs_ok) {
dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n"); dprintk("NFSD: nfsd4_fallocate: couldn't process stateid!\n");
return status; return status;
} }
if (!file)
return nfserr_bad_stateid;
status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file, status = nfsd4_vfs_fallocate(rqstp, &cstate->current_fh, file,
fallocate->falloc_offset, fallocate->falloc_offset,
...@@ -1064,15 +1057,13 @@ nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, ...@@ -1064,15 +1057,13 @@ nfsd4_seek(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
__be32 status; __be32 status;
struct file *file; struct file *file;
status = nfs4_preprocess_stateid_op(SVC_NET(rqstp), cstate, status = nfs4_preprocess_stateid_op(rqstp, cstate,
&seek->seek_stateid, &seek->seek_stateid,
RD_STATE, &file); RD_STATE, &file, NULL);
if (status) { if (status) {
dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n"); dprintk("NFSD: nfsd4_seek: couldn't process stateid!\n");
return status; return status;
} }
if (!file)
return nfserr_bad_stateid;
switch (seek->seek_whence) { switch (seek->seek_whence) {
case NFS4_CONTENT_DATA: case NFS4_CONTENT_DATA:
......
...@@ -4576,6 +4576,9 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, ...@@ -4576,6 +4576,9 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
static struct file * static struct file *
nfs4_find_file(struct nfs4_stid *s, int flags) nfs4_find_file(struct nfs4_stid *s, int flags)
{ {
if (!s)
return NULL;
switch (s->sc_type) { switch (s->sc_type) {
case NFS4_DELEG_STID: case NFS4_DELEG_STID:
if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
...@@ -4607,27 +4610,63 @@ nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags) ...@@ -4607,27 +4610,63 @@ nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
return nfs4_check_openmode(ols, flags); return nfs4_check_openmode(ols, flags);
} }
static __be32
nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
struct file **filpp, bool *tmp_file, int flags)
{
int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
struct file *file;
__be32 status;
file = nfs4_find_file(s, flags);
if (file) {
status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
acc | NFSD_MAY_OWNER_OVERRIDE);
if (status) {
fput(file);
return status;
}
*filpp = file;
} else {
status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
if (status)
return status;
if (tmp_file)
*tmp_file = true;
}
return 0;
}
/* /*
* Checks for stateid operations * Checks for stateid operations
*/ */
__be32 __be32
nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
stateid_t *stateid, int flags, struct file **filpp) struct nfsd4_compound_state *cstate, stateid_t *stateid,
int flags, struct file **filpp, bool *tmp_file)
{ {
struct svc_fh *fhp = &cstate->current_fh; struct svc_fh *fhp = &cstate->current_fh;
struct inode *ino = d_inode(fhp->fh_dentry); struct inode *ino = d_inode(fhp->fh_dentry);
struct net *net = SVC_NET(rqstp);
struct nfsd_net *nn = net_generic(net, nfsd_net_id); struct nfsd_net *nn = net_generic(net, nfsd_net_id);
struct nfs4_stid *s; struct nfs4_stid *s = NULL;
__be32 status; __be32 status;
if (filpp) if (filpp)
*filpp = NULL; *filpp = NULL;
if (tmp_file)
*tmp_file = false;
if (grace_disallows_io(net, ino)) if (grace_disallows_io(net, ino))
return nfserr_grace; return nfserr_grace;
if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
return check_special_stateids(net, fhp, stateid, flags); status = check_special_stateids(net, fhp, stateid, flags);
goto done;
}
status = nfsd4_lookup_stateid(cstate, stateid, status = nfsd4_lookup_stateid(cstate, stateid,
NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
...@@ -4652,12 +4691,11 @@ nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate, ...@@ -4652,12 +4691,11 @@ nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
break; break;
} }
if (!status && filpp) { done:
*filpp = nfs4_find_file(s, flags); if (!status && filpp)
if (!*filpp) status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
status = nfserr_serverfault;
}
out: out:
if (s)
nfs4_put_stid(s); nfs4_put_stid(s);
return status; return status;
} }
......
...@@ -33,7 +33,6 @@ ...@@ -33,7 +33,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <linux/file.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/statfs.h> #include <linux/statfs.h>
...@@ -3418,7 +3417,6 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr, ...@@ -3418,7 +3417,6 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
unsigned long maxcount; unsigned long maxcount;
struct xdr_stream *xdr = &resp->xdr; struct xdr_stream *xdr = &resp->xdr;
struct file *file = read->rd_filp; struct file *file = read->rd_filp;
struct svc_fh *fhp = read->rd_fhp;
int starting_len = xdr->buf->len; int starting_len = xdr->buf->len;
struct raparms *ra = NULL; struct raparms *ra = NULL;
__be32 *p; __be32 *p;
...@@ -3442,20 +3440,8 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr, ...@@ -3442,20 +3440,8 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
maxcount = min_t(unsigned long, maxcount, (xdr->buf->buflen - xdr->buf->len)); maxcount = min_t(unsigned long, maxcount, (xdr->buf->buflen - xdr->buf->len));
maxcount = min_t(unsigned long, maxcount, read->rd_length); maxcount = min_t(unsigned long, maxcount, read->rd_length);
if (read->rd_filp) { if (read->rd_tmp_file)
err = nfsd_permission(resp->rqstp, fhp->fh_export,
fhp->fh_dentry,
NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
if (err)
goto err_truncate;
} else {
err = nfsd_open(resp->rqstp, fhp, S_IFREG, NFSD_MAY_READ,
&file);
if (err)
goto err_truncate;
ra = nfsd_init_raparms(file); ra = nfsd_init_raparms(file);
}
if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
err = nfsd4_encode_splice_read(resp, read, file, maxcount); err = nfsd4_encode_splice_read(resp, read, file, maxcount);
...@@ -3464,10 +3450,7 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr, ...@@ -3464,10 +3450,7 @@ nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
if (ra) if (ra)
nfsd_put_raparams(file, ra); nfsd_put_raparams(file, ra);
if (!read->rd_filp)
fput(file);
err_truncate:
if (err) if (err)
xdr_truncate_encode(xdr, starting_len); xdr_truncate_encode(xdr, starting_len);
return err; return err;
......
...@@ -583,9 +583,9 @@ enum nfsd4_cb_op { ...@@ -583,9 +583,9 @@ enum nfsd4_cb_op {
struct nfsd4_compound_state; struct nfsd4_compound_state;
struct nfsd_net; struct nfsd_net;
extern __be32 nfs4_preprocess_stateid_op(struct net *net, extern __be32 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
struct nfsd4_compound_state *cstate, struct nfsd4_compound_state *cstate, stateid_t *stateid,
stateid_t *stateid, int flags, struct file **filp); int flags, struct file **filp, bool *tmp_file);
__be32 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, __be32 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
stateid_t *stateid, unsigned char typemask, stateid_t *stateid, unsigned char typemask,
struct nfs4_stid **s, struct nfsd_net *nn); struct nfs4_stid **s, struct nfsd_net *nn);
......
...@@ -502,16 +502,11 @@ __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp, ...@@ -502,16 +502,11 @@ __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
struct file *file, loff_t offset, loff_t len, struct file *file, loff_t offset, loff_t len,
int flags) int flags)
{ {
__be32 err;
int error; int error;
if (!S_ISREG(file_inode(file)->i_mode)) if (!S_ISREG(file_inode(file)->i_mode))
return nfserr_inval; return nfserr_inval;
err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, NFSD_MAY_WRITE);
if (err)
return err;
error = vfs_fallocate(file, flags, offset, len); error = vfs_fallocate(file, flags, offset, len);
if (!error) if (!error)
error = commit_metadata(fhp); error = commit_metadata(fhp);
...@@ -912,7 +907,7 @@ static int wait_for_concurrent_writes(struct file *file) ...@@ -912,7 +907,7 @@ static int wait_for_concurrent_writes(struct file *file)
return err; return err;
} }
static __be32 __be32
nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file, nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
loff_t offset, struct kvec *vec, int vlen, loff_t offset, struct kvec *vec, int vlen,
unsigned long *cnt, int *stablep) unsigned long *cnt, int *stablep)
......
...@@ -80,6 +80,10 @@ __be32 nfsd_read(struct svc_rqst *, struct svc_fh *, ...@@ -80,6 +80,10 @@ __be32 nfsd_read(struct svc_rqst *, struct svc_fh *,
loff_t, struct kvec *, int, unsigned long *); loff_t, struct kvec *, int, unsigned long *);
__be32 nfsd_write(struct svc_rqst *, struct svc_fh *,struct file *, __be32 nfsd_write(struct svc_rqst *, struct svc_fh *,struct file *,
loff_t, struct kvec *,int, unsigned long *, int *); loff_t, struct kvec *,int, unsigned long *, int *);
__be32 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);
__be32 nfsd_readlink(struct svc_rqst *, struct svc_fh *, __be32 nfsd_readlink(struct svc_rqst *, struct svc_fh *,
char *, int *); char *, int *);
__be32 nfsd_symlink(struct svc_rqst *, struct svc_fh *, __be32 nfsd_symlink(struct svc_rqst *, struct svc_fh *,
......
...@@ -273,6 +273,7 @@ struct nfsd4_read { ...@@ -273,6 +273,7 @@ struct nfsd4_read {
u32 rd_length; /* request */ u32 rd_length; /* request */
int rd_vlen; int rd_vlen;
struct file *rd_filp; struct file *rd_filp;
bool rd_tmp_file;
struct svc_rqst *rd_rqstp; /* response */ struct svc_rqst *rd_rqstp; /* response */
struct svc_fh * rd_fhp; /* response */ struct svc_fh * rd_fhp; /* response */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment