Commit 02a913a7 authored by Trond Myklebust's avatar Trond Myklebust

NFSv4: Eliminate nfsv4 open race...

 Make NFSv4 return the fully initialized file pointer with the
 stateid that it created in the lookup w/intent.
Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent 834f2a4a
...@@ -914,7 +914,6 @@ static int is_atomic_open(struct inode *dir, struct nameidata *nd) ...@@ -914,7 +914,6 @@ 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) static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{ {
struct dentry *res = NULL; struct dentry *res = NULL;
struct inode *inode = NULL;
int error; int error;
/* Check that we are indeed trying to open this file */ /* Check that we are indeed trying to open this file */
...@@ -928,8 +927,10 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry ...@@ -928,8 +927,10 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
dentry->d_op = NFS_PROTO(dir)->dentry_ops; dentry->d_op = NFS_PROTO(dir)->dentry_ops;
/* Let vfs_create() deal with O_EXCL */ /* Let vfs_create() deal with O_EXCL */
if (nd->intent.open.flags & O_EXCL) if (nd->intent.open.flags & O_EXCL) {
goto no_entry; d_add(dentry, NULL);
goto out;
}
/* Open the file on the server */ /* Open the file on the server */
lock_kernel(); lock_kernel();
...@@ -943,18 +944,18 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry ...@@ -943,18 +944,18 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
if (nd->intent.open.flags & O_CREAT) { if (nd->intent.open.flags & O_CREAT) {
nfs_begin_data_update(dir); nfs_begin_data_update(dir);
inode = nfs4_atomic_open(dir, dentry, nd); res = nfs4_atomic_open(dir, dentry, nd);
nfs_end_data_update(dir); nfs_end_data_update(dir);
} else } else
inode = nfs4_atomic_open(dir, dentry, nd); res = nfs4_atomic_open(dir, dentry, nd);
unlock_kernel(); unlock_kernel();
if (IS_ERR(inode)) { if (IS_ERR(res)) {
error = PTR_ERR(inode); error = PTR_ERR(res);
switch (error) { switch (error) {
/* Make a negative dentry */ /* Make a negative dentry */
case -ENOENT: case -ENOENT:
inode = NULL; res = NULL;
break; goto out;
/* This turned out not to be a regular file */ /* This turned out not to be a regular file */
case -ELOOP: case -ELOOP:
if (!(nd->intent.open.flags & O_NOFOLLOW)) if (!(nd->intent.open.flags & O_NOFOLLOW))
...@@ -962,13 +963,9 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry ...@@ -962,13 +963,9 @@ static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry
/* case -EISDIR: */ /* case -EISDIR: */
/* case -EINVAL: */ /* case -EINVAL: */
default: default:
res = ERR_PTR(error);
goto out; goto out;
} }
} } else if (res != NULL)
no_entry:
res = d_add_unique(dentry, inode);
if (res != NULL)
dentry = res; dentry = res;
nfs_renew_times(dentry); nfs_renew_times(dentry);
nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
...@@ -1012,7 +1009,7 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd) ...@@ -1012,7 +1009,7 @@ static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
*/ */
lock_kernel(); lock_kernel();
verifier = nfs_save_change_attribute(dir); verifier = nfs_save_change_attribute(dir);
ret = nfs4_open_revalidate(dir, dentry, openflags); ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
if (!ret) if (!ret)
nfs_set_verifier(dentry, verifier); nfs_set_verifier(dentry, verifier);
unlock_kernel(); unlock_kernel();
...@@ -1135,7 +1132,7 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode, ...@@ -1135,7 +1132,7 @@ static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
lock_kernel(); lock_kernel();
nfs_begin_data_update(dir); nfs_begin_data_update(dir);
error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
nfs_end_data_update(dir); nfs_end_data_update(dir);
if (error != 0) if (error != 0)
goto out_err; goto out_err;
......
...@@ -299,7 +299,7 @@ static int nfs3_proc_commit(struct nfs_write_data *cdata) ...@@ -299,7 +299,7 @@ static int nfs3_proc_commit(struct nfs_write_data *cdata)
*/ */
static int static int
nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
int flags) int flags, struct nameidata *nd)
{ {
struct nfs_fh fhandle; struct nfs_fh fhandle;
struct nfs_fattr fattr; struct nfs_fattr fattr;
......
...@@ -215,8 +215,8 @@ extern int nfs4_proc_setclientid_confirm(struct nfs4_client *); ...@@ -215,8 +215,8 @@ extern int nfs4_proc_setclientid_confirm(struct nfs4_client *);
extern int nfs4_proc_async_renew(struct nfs4_client *); extern int nfs4_proc_async_renew(struct nfs4_client *);
extern int nfs4_proc_renew(struct nfs4_client *); extern int nfs4_proc_renew(struct nfs4_client *);
extern int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode); extern int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode);
extern struct inode *nfs4_atomic_open(struct inode *, struct dentry *, struct nameidata *); extern struct dentry *nfs4_atomic_open(struct inode *, struct dentry *, struct nameidata *);
extern int nfs4_open_revalidate(struct inode *, struct dentry *, int); extern int nfs4_open_revalidate(struct inode *, struct dentry *, int, struct nameidata *);
extern struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops; extern struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops;
extern struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops; extern struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops;
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include <linux/nfs_page.h> #include <linux/nfs_page.h>
#include <linux/smp_lock.h> #include <linux/smp_lock.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/mount.h>
#include "nfs4_fs.h" #include "nfs4_fs.h"
#include "delegation.h" #include "delegation.h"
...@@ -947,12 +948,26 @@ int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode) ...@@ -947,12 +948,26 @@ int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
return status; return status;
} }
struct inode * static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
{
struct file *filp;
filp = lookup_instantiate_filp(nd, dentry, NULL);
if (!IS_ERR(filp)) {
struct nfs_open_context *ctx;
ctx = (struct nfs_open_context *)filp->private_data;
ctx->state = state;
} else
nfs4_close_state(state, nd->intent.open.flags);
}
struct dentry *
nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{ {
struct iattr attr; struct iattr attr;
struct rpc_cred *cred; struct rpc_cred *cred;
struct nfs4_state *state; struct nfs4_state *state;
struct dentry *res;
if (nd->flags & LOOKUP_CREATE) { if (nd->flags & LOOKUP_CREATE) {
attr.ia_mode = nd->intent.open.create_mode; attr.ia_mode = nd->intent.open.create_mode;
...@@ -966,16 +981,23 @@ nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) ...@@ -966,16 +981,23 @@ nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0); cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
if (IS_ERR(cred)) if (IS_ERR(cred))
return (struct inode *)cred; return (struct dentry *)cred;
state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred); state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
put_rpccred(cred); put_rpccred(cred);
if (IS_ERR(state)) if (IS_ERR(state)) {
return (struct inode *)state; if (PTR_ERR(state) == -ENOENT)
return state->inode; d_add(dentry, NULL);
return (struct dentry *)state;
}
res = d_add_unique(dentry, state->inode);
if (res != NULL)
dentry = res;
nfs4_intent_set_file(nd, dentry, state);
return res;
} }
int int
nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags) nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
{ {
struct rpc_cred *cred; struct rpc_cred *cred;
struct nfs4_state *state; struct nfs4_state *state;
...@@ -988,18 +1010,30 @@ nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags) ...@@ -988,18 +1010,30 @@ nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
if (IS_ERR(state)) if (IS_ERR(state))
state = nfs4_do_open(dir, dentry, openflags, NULL, cred); state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
put_rpccred(cred); put_rpccred(cred);
if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0) if (IS_ERR(state)) {
switch (PTR_ERR(state)) {
case -EPERM:
case -EACCES:
case -EDQUOT:
case -ENOSPC:
case -EROFS:
lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
return 1; return 1;
if (IS_ERR(state)) case -ENOENT:
return 0; if (dentry->d_inode == NULL)
return 1;
}
goto out_drop;
}
inode = state->inode; inode = state->inode;
if (inode == dentry->d_inode) {
iput(inode); iput(inode);
if (inode == dentry->d_inode) {
nfs4_intent_set_file(nd, dentry, state);
return 1; return 1;
} }
d_drop(dentry);
nfs4_close_state(state, openflags); nfs4_close_state(state, openflags);
iput(inode); out_drop:
d_drop(dentry);
return 0; return 0;
} }
...@@ -1500,7 +1534,7 @@ static int nfs4_proc_commit(struct nfs_write_data *cdata) ...@@ -1500,7 +1534,7 @@ static int nfs4_proc_commit(struct nfs_write_data *cdata)
static int static int
nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
int flags) int flags, struct nameidata *nd)
{ {
struct nfs4_state *state; struct nfs4_state *state;
struct rpc_cred *cred; struct rpc_cred *cred;
...@@ -1522,12 +1556,12 @@ nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, ...@@ -1522,12 +1556,12 @@ nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
struct nfs_fattr fattr; struct nfs_fattr fattr;
status = nfs4_do_setattr(NFS_SERVER(dir), &fattr, status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
NFS_FH(state->inode), sattr, state); NFS_FH(state->inode), sattr, state);
if (status == 0) { if (status == 0)
nfs_setattr_update_inode(state->inode, sattr); nfs_setattr_update_inode(state->inode, sattr);
goto out;
} }
} else if (flags != 0) if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
goto out; nfs4_intent_set_file(nd, dentry, state);
else
nfs4_close_state(state, flags); nfs4_close_state(state, flags);
out: out:
return status; return status;
...@@ -2175,65 +2209,6 @@ nfs4_proc_renew(struct nfs4_client *clp) ...@@ -2175,65 +2209,6 @@ nfs4_proc_renew(struct nfs4_client *clp)
return 0; return 0;
} }
/*
* We will need to arrange for the VFS layer to provide an atomic open.
* Until then, this open method is prone to inefficiency and race conditions
* due to the lookup, potential create, and open VFS calls from sys_open()
* placed on the wire.
*/
static int
nfs4_proc_file_open(struct inode *inode, struct file *filp)
{
struct dentry *dentry = filp->f_dentry;
struct nfs_open_context *ctx;
struct nfs4_state *state = NULL;
struct rpc_cred *cred;
int status = -ENOMEM;
dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
(int)dentry->d_parent->d_name.len,
dentry->d_parent->d_name.name,
(int)dentry->d_name.len, dentry->d_name.name);
/* Find our open stateid */
cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
if (IS_ERR(cred))
return PTR_ERR(cred);
ctx = alloc_nfs_open_context(dentry, cred);
put_rpccred(cred);
if (unlikely(ctx == NULL))
return -ENOMEM;
status = -EIO; /* ERACE actually */
state = nfs4_find_state(inode, cred, filp->f_mode);
if (unlikely(state == NULL))
goto no_state;
ctx->state = state;
nfs4_close_state(state, filp->f_mode);
ctx->mode = filp->f_mode;
nfs_file_set_open_context(filp, ctx);
put_nfs_open_context(ctx);
if (filp->f_mode & FMODE_WRITE)
nfs_begin_data_update(inode);
return 0;
no_state:
printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
put_nfs_open_context(ctx);
return status;
}
/*
* Release our state
*/
static int
nfs4_proc_file_release(struct inode *inode, struct file *filp)
{
if (filp->f_mode & FMODE_WRITE)
nfs_end_data_update(inode);
nfs_file_clear_open_context(filp);
return 0;
}
static inline int nfs4_server_supports_acls(struct nfs_server *server) static inline int nfs4_server_supports_acls(struct nfs_server *server)
{ {
return (server->caps & NFS_CAP_ACLS) return (server->caps & NFS_CAP_ACLS)
...@@ -3145,8 +3120,8 @@ struct nfs_rpc_ops nfs_v4_clientops = { ...@@ -3145,8 +3120,8 @@ struct nfs_rpc_ops nfs_v4_clientops = {
.read_setup = nfs4_proc_read_setup, .read_setup = nfs4_proc_read_setup,
.write_setup = nfs4_proc_write_setup, .write_setup = nfs4_proc_write_setup,
.commit_setup = nfs4_proc_commit_setup, .commit_setup = nfs4_proc_commit_setup,
.file_open = nfs4_proc_file_open, .file_open = nfs_open,
.file_release = nfs4_proc_file_release, .file_release = nfs_release,
.lock = nfs4_proc_lock, .lock = nfs4_proc_lock,
.clear_acl_cache = nfs4_zap_acl_attr, .clear_acl_cache = nfs4_zap_acl_attr,
}; };
......
...@@ -216,7 +216,7 @@ static int nfs_proc_write(struct nfs_write_data *wdata) ...@@ -216,7 +216,7 @@ static int nfs_proc_write(struct nfs_write_data *wdata)
static int static int
nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
int flags) int flags, struct nameidata *nd)
{ {
struct nfs_fh fhandle; struct nfs_fh fhandle;
struct nfs_fattr fattr; struct nfs_fattr fattr;
......
...@@ -712,7 +712,7 @@ struct nfs_rpc_ops { ...@@ -712,7 +712,7 @@ struct nfs_rpc_ops {
int (*write) (struct nfs_write_data *); int (*write) (struct nfs_write_data *);
int (*commit) (struct nfs_write_data *); int (*commit) (struct nfs_write_data *);
int (*create) (struct inode *, struct dentry *, int (*create) (struct inode *, struct dentry *,
struct iattr *, int); struct iattr *, int, struct nameidata *);
int (*remove) (struct inode *, struct qstr *); int (*remove) (struct inode *, struct qstr *);
int (*unlink_setup) (struct rpc_message *, int (*unlink_setup) (struct rpc_message *,
struct dentry *, struct qstr *); struct dentry *, struct qstr *);
......
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