Commit cc8a5532 authored by Jeff Layton's avatar Jeff Layton Committed by J. Bruce Fields

nfsd: serialize layout stateid morphing operations

In order to allow the client to make a sane determination of what
happened with racing LAYOUTGET/LAYOUTRETURN/CB_LAYOUTRECALL calls, we
must ensure that the seqids return accurately represent the order of
operations. The simplest way to do that is to ensure that operations on
a single stateid are serialized.

This patch adds a mutex to the layout stateid, and locks it when
checking the layout stateid's seqid. The mutex is held over the entire
operation and released after the seqid is bumped.

Note that in the case of CB_LAYOUTRECALL we must move the increment of
the seqid and setting into a new cb "prepare" operation. The lease
infrastructure will call the lm_break callback with a spinlock held, so
and we can't take the mutex in that codepath.

Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJeff Layton <jeff.layton@primarydata.com>
Signed-off-by: default avatarJ. Bruce Fields <bfields@redhat.com>
parent 4eaea134
...@@ -201,6 +201,7 @@ nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate, ...@@ -201,6 +201,7 @@ nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
INIT_LIST_HEAD(&ls->ls_perfile); INIT_LIST_HEAD(&ls->ls_perfile);
spin_lock_init(&ls->ls_lock); spin_lock_init(&ls->ls_lock);
INIT_LIST_HEAD(&ls->ls_layouts); INIT_LIST_HEAD(&ls->ls_layouts);
mutex_init(&ls->ls_mutex);
ls->ls_layout_type = layout_type; ls->ls_layout_type = layout_type;
nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops, nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
NFSPROC4_CLNT_CB_LAYOUT); NFSPROC4_CLNT_CB_LAYOUT);
...@@ -262,19 +263,23 @@ nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp, ...@@ -262,19 +263,23 @@ nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
status = nfserr_jukebox; status = nfserr_jukebox;
if (!ls) if (!ls)
goto out; goto out;
mutex_lock(&ls->ls_mutex);
} else { } else {
ls = container_of(stid, struct nfs4_layout_stateid, ls_stid); ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
status = nfserr_bad_stateid; status = nfserr_bad_stateid;
mutex_lock(&ls->ls_mutex);
if (stateid->si_generation > stid->sc_stateid.si_generation) if (stateid->si_generation > stid->sc_stateid.si_generation)
goto out_put_stid; goto out_unlock_stid;
if (layout_type != ls->ls_layout_type) if (layout_type != ls->ls_layout_type)
goto out_put_stid; goto out_unlock_stid;
} }
*lsp = ls; *lsp = ls;
return 0; return 0;
out_unlock_stid:
mutex_unlock(&ls->ls_mutex);
out_put_stid: out_put_stid:
nfs4_put_stid(stid); nfs4_put_stid(stid);
out: out:
...@@ -296,8 +301,6 @@ nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls) ...@@ -296,8 +301,6 @@ nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
trace_layout_recall(&ls->ls_stid.sc_stateid); trace_layout_recall(&ls->ls_stid.sc_stateid);
atomic_inc(&ls->ls_stid.sc_count); atomic_inc(&ls->ls_stid.sc_count);
update_stateid(&ls->ls_stid.sc_stateid);
memcpy(&ls->ls_recall_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t));
nfsd4_run_cb(&ls->ls_recall); nfsd4_run_cb(&ls->ls_recall);
out_unlock: out_unlock:
...@@ -494,6 +497,7 @@ nfsd4_return_file_layouts(struct svc_rqst *rqstp, ...@@ -494,6 +497,7 @@ nfsd4_return_file_layouts(struct svc_rqst *rqstp,
} }
spin_unlock(&ls->ls_lock); spin_unlock(&ls->ls_lock);
mutex_unlock(&ls->ls_mutex);
nfs4_put_stid(&ls->ls_stid); nfs4_put_stid(&ls->ls_stid);
nfsd4_free_layouts(&reaplist); nfsd4_free_layouts(&reaplist);
return nfs_ok; return nfs_ok;
...@@ -608,6 +612,17 @@ nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls) ...@@ -608,6 +612,17 @@ nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
} }
} }
static void
nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
{
struct nfs4_layout_stateid *ls =
container_of(cb, struct nfs4_layout_stateid, ls_recall);
mutex_lock(&ls->ls_mutex);
update_stateid(&ls->ls_stid.sc_stateid);
memcpy(&ls->ls_recall_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t));
}
static int static int
nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task) nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
{ {
...@@ -649,12 +664,14 @@ nfsd4_cb_layout_release(struct nfsd4_callback *cb) ...@@ -649,12 +664,14 @@ nfsd4_cb_layout_release(struct nfsd4_callback *cb)
trace_layout_recall_release(&ls->ls_stid.sc_stateid); trace_layout_recall_release(&ls->ls_stid.sc_stateid);
mutex_unlock(&ls->ls_mutex);
nfsd4_return_all_layouts(ls, &reaplist); nfsd4_return_all_layouts(ls, &reaplist);
nfsd4_free_layouts(&reaplist); nfsd4_free_layouts(&reaplist);
nfs4_put_stid(&ls->ls_stid); nfs4_put_stid(&ls->ls_stid);
} }
static struct nfsd4_callback_ops nfsd4_cb_layout_ops = { static struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
.prepare = nfsd4_cb_layout_prepare,
.done = nfsd4_cb_layout_done, .done = nfsd4_cb_layout_done,
.release = nfsd4_cb_layout_release, .release = nfsd4_cb_layout_release,
}; };
......
...@@ -1309,6 +1309,7 @@ nfsd4_layoutget(struct svc_rqst *rqstp, ...@@ -1309,6 +1309,7 @@ nfsd4_layoutget(struct svc_rqst *rqstp,
nfserr = nfsd4_insert_layout(lgp, ls); nfserr = nfsd4_insert_layout(lgp, ls);
out_put_stid: out_put_stid:
mutex_unlock(&ls->ls_mutex);
nfs4_put_stid(&ls->ls_stid); nfs4_put_stid(&ls->ls_stid);
out: out:
return nfserr; return nfserr;
...@@ -1362,6 +1363,9 @@ nfsd4_layoutcommit(struct svc_rqst *rqstp, ...@@ -1362,6 +1363,9 @@ nfsd4_layoutcommit(struct svc_rqst *rqstp,
goto out; goto out;
} }
/* LAYOUTCOMMIT does not require any serialization */
mutex_unlock(&ls->ls_mutex);
if (new_size > i_size_read(inode)) { if (new_size > i_size_read(inode)) {
lcp->lc_size_chg = 1; lcp->lc_size_chg = 1;
lcp->lc_newsize = new_size; lcp->lc_newsize = new_size;
......
...@@ -562,6 +562,7 @@ struct nfs4_layout_stateid { ...@@ -562,6 +562,7 @@ struct nfs4_layout_stateid {
struct nfsd4_callback ls_recall; struct nfsd4_callback ls_recall;
stateid_t ls_recall_sid; stateid_t ls_recall_sid;
bool ls_recalled; bool ls_recalled;
struct mutex ls_mutex;
}; };
static inline struct nfs4_layout_stateid *layoutstateid(struct nfs4_stid *s) static inline struct nfs4_layout_stateid *layoutstateid(struct nfs4_stid *s)
......
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