nfs: pnfs: handle early layoutreturn failures gracefully
Subsystems (2)
PATCH 1/1 nfs: pnfs: handle early layoutreturn failures gracefully
pnfs_layoutreturn_before_put_layout_hdr() bumps the layout header refcount and sets NFS_LAYOUT_RETURN before prepare or rpc_run_task dispatch. If the layout driver fails prepare or rpc_run_task() fails to queue the call, we currently leak refs and leave waiters stuck on pnfs_prepare_to_retry_layoutget().
Mirror the normal completion path for these early failures: warn and schedule pnfs_layoutreturn_retry_later(), free any reserved slot, drop refs/creds/inode, and clear the wait bit.
--- fs/nfs/nfs4proc.c | 37 +++++++++++++++++++++++++------------ fs/nfs/pnfs.c | 21 +++++++++++++++++++-- 2 files changed, 44 insertions(+), 14 deletions(-)
fs/nfs/nfs4proc.c +25 -12
@@ -10132,25 +10132,34 @@ static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)rpc_restart_call_prepare (task); } -static void nfs4_layoutreturn_release (void *calldata) +static void nfs4_layoutreturn_cleanup (struct nfs4_layoutreturn *lrp,int status) { -struct nfs4_layoutreturn *lrp = calldata;struct pnfs_layout_hdr *lo = lrp->args.layout; -if (lrp->rpc_status ==0 || !lrp->inode) -pnfs_layoutreturn_free_lsegs ( - lo, &lrp->args.stateid, &lrp->args.range, - lrp->res.lrs_present ? &lrp->res.stateid :NULL ); +if (status ==0 || !lrp->inode) +pnfs_layoutreturn_free_lsegs (lo, &lrp->args.stateid, + &lrp->args.range, + lrp->res.lrs_present ? + &lrp->res.stateid :NULL );else pnfs_layoutreturn_retry_later (lo, &lrp->args.stateid, &lrp->args.range); -nfs4_sequence_free_slot (&lrp->res.seq_res); +if (lrp->res.seq_res.sr_slot) +nfs4_sequence_free_slot (&lrp->res.seq_res);if (lrp->ld_private.ops && lrp->ld_private.ops->free) lrp->ld_private.ops->free (&lrp->ld_private); -pnfs_put_layout_hdr (lrp->args.layout); -nfs_iput_and_deactive (lrp->inode); +pnfs_put_layout_hdr (lo); +if (lrp->inode) +nfs_iput_and_deactive (lrp->inode);put_cred (lrp->cred); -kfree (calldata); +kfree (lrp); +} + +static void nfs4_layoutreturn_release (void *calldata) +{ +struct nfs4_layoutreturn *lrp = calldata; + +nfs4_layoutreturn_cleanup (lrp, lrp->rpc_status); }static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
@@ -10198,8 +10207,12 @@ int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)nfs4_init_sequence (&lrp->args.seq_args, &lrp->res.seq_res,1 ,0 ); task =rpc_run_task (&task_setup_data); -if (IS_ERR (task)) -return PTR_ERR (task); +if (IS_ERR (task)) { + status =PTR_ERR (task); +trace_nfs4_layoutreturn (lrp->args.inode, &lrp->args.stateid, status); +nfs4_layoutreturn_cleanup (lrp, status); +return status; + }if (!(flags &PNFS_FL_LAYOUTRETURN_ASYNC )) status = task->tk_status;trace_nfs4_layoutreturn (lrp->args.inode, &lrp->args.stateid, status);
fs/nfs/pnfs.c +19 -2
@@ -1370,13 +1370,30 @@ pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, lrp->args.ld_private = &lrp->ld_private; lrp->clp =NFS_SERVER (ino)->nfs_client; lrp->cred = cred; -if (ld->prepare_layoutreturn) - ld->prepare_layoutreturn (&lrp->args); +if (ld->prepare_layoutreturn) { + status = ld->prepare_layoutreturn (&lrp->args); +if (status) { +pr_warn_ratelimited ("NFS: pNFS layoutreturn prepare failed (%d) for layout driver %s\n" , + status, ld->name ? ld->name :"unknown" ); + goto out_prepare_fail; + } + } status =nfs4_proc_layoutreturn (lrp, flags); out:dprintk ("<-- %s status: %d\n" , __func__, status);return status; + +out_prepare_fail: +pnfs_layoutreturn_retry_later (lo, &lrp->args.stateid, &lrp->args.range); +if (lrp->ld_private.ops && lrp->ld_private.ops->free) + lrp->ld_private.ops->free (&lrp->ld_private); +if (lrp->inode) +nfs_iput_and_deactive (lrp->inode); +put_cred (cred); +kfree (lrp); +pnfs_put_layout_hdr (lo); +return status; }/* Return true if layoutreturn is needed */
base-commit: cb015814f8b6eebcbb8e46e111d108892c5e6821
Signature · 1 line
2.47.1
Trond Myklebust
maintainerQuoted 86 lines · Robert Milkowski
pnfs_layoutreturn_before_put_layout_hdr()bumps the layout header refcount
and setsNFS_LAYOUT_RETURNbefore prepare orrpc_run_taskdispatch. If the
layout driver fails prepare orrpc_run_task()fails to queue the call, we
currently leak refs and leave waiters stuck onpnfs_prepare_to_retry_layoutget().Mirror the normal completion path for these early failures: warn and schedule
pnfs_layoutreturn_retry_later(), free any reserved slot, drop
refs/creds/inode, and clear the wait bit.Signed-off-by Robert Milkowski <[email protected]>---
fs/nfs/nfs4proc.c | 37 +++++++++++++++++++++++++------------ fs/nfs/pnfs.c | 21 +++++++++++++++++++--2 files changed, 44 insertions(+), 14 deletions(-)fs/nfs/nfs4proc.c +0
-0@@ -10132,25 +10132,34 @@ static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) rpc_restart_call_prepare(task); }fs/nfs/nfs4proc.c +1
-1-static void nfs4_layoutreturn_release (void *calldata) +static void nfs4_layoutreturn_cleanup(struct nfs4_layoutreturn *lrp,int status)
{fs/nfs/nfs4proc.c +0
-1-struct nfs4_layoutreturn *lrp = calldata;struct
pnfs_layout_hdr*lo =lrp->args.layout;fs/nfs/nfs4proc.c +0
-4-if (lrp->rpc_status ==0 || !lrp->inode) - pnfs_layoutreturn_free_lsegs( - lo, &lrp->args.stateid, &lrp->args.range, - lrp->res.lrs_present ? &lrp->res.stateid :NULL);
fs/nfs/nfs4proc.c +5
-0+if (status ==0 || !lrp->inode) + pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, + &lrp->args.range, + lrp->res.lrs_present ? + &lrp->res.stateid :NULL);
else
pnfs_layoutreturn_retry_later(lo, &lrp-Quoted 1 line
args.stateid,
&lrp->args.range);fs/nfs/nfs4proc.c +2
-1-nfs4_sequence_free_slot (&lrp->res.seq_res); +if (lrp->res.seq_res.sr_slot) +nfs4_sequence_free_slot (&lrp->res.seq_res);if (
lrp->ld_private.ops&&lrp->ld_private.ops->free)lrp->ld_private.ops->free(&lrp->ld_private);fs/nfs/nfs4proc.c +3
-2-pnfs_put_layout_hdr (lrp->args.layout); -nfs_iput_and_deactive (lrp->inode); +pnfs_put_layout_hdr (lo); +if (lrp->inode) +nfs_iput_and_deactive (lrp->inode);
put_cred(lrp->cred);fs/nfs/nfs4proc.c +8
-1-kfree (calldata); +kfree (lrp); +} + +static void nfs4_layoutreturn_release (void *calldata) +{ +struct nfs4_layoutreturn *lrp = calldata; + +nfs4_layoutreturn_cleanup (lrp, lrp->rpc_status);}
static const structrpc_call_opsnfs4_layoutreturn_call_ops= {fs/nfs/nfs4proc.c +0
-0@@ -10198,8 +10207,12 @@ int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)nfs4_init_sequence(&lrp->args.seq_args, &lrp-
Quoted 1 line
res.seq_res, 1,
0);
task =rpc_run_task(&task_setup_data);fs/nfs/nfs4proc.c +3
-2-if (IS_ERR (task)) -return PTR_ERR (task); +if (IS_ERR (task)) { + status =PTR_ERR (task); + trace_nfs4_layoutreturn(lrp->args.inode, &lrp-Quoted 1 line
args.stateid, status);
fs/nfs/nfs4proc.c int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags) · line 10215
Quoted 3 lines
fs/nfs/nfs4proc.c +3
-0+nfs4_layoutreturn_cleanup (lrp, status); +return status; + }
NACK. The above introduces a use-after-free. There is no need to call the release routine after a call to rpc_run_task().
Quoted 15 lines
if (!(flags &
PNFS_FL_LAYOUTRETURN_ASYNC))
status =task->tk_status;trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);fs/nfs/pnfs.c +0
-2@@ -1370,13 +1370,30 @@ pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, lrp->args.ld_private = &lrp->ld_private; lrp->clp = NFS_SERVER(ino)->nfs_client; lrp->cred = cred; -if (ld->prepare_layoutreturn) - ld->prepare_layoutreturn (&lrp->args);
fs/nfs/pnfs.c pnfs_send_layoutreturn(struct pnfs_layout_hdr · line 1383
Quoted 10 lines
fs/nfs/pnfs.c +8
-0+if (ld->prepare_layoutreturn) { + status = ld->prepare_layoutreturn (&lrp->args); +if (status) { + pr_warn_ratelimited("NFS: pNFS layoutreturn prepare failed (%d) for layout driver %s\n", + status, ld->name ? ld->name :"unknown" ); + goto out_prepare_fail; + } + }
This is also unnecessary. The existing code will cope just fine with args->ld_private being unset.
Quoted 22 lines
status =nfs4_proc_layoutreturn(lrp, flags); out:
dprintk("<-- %s status: %d\n", __func__, status); return status;fs/nfs/pnfs.c +3
-0+ +out_prepare_fail: + pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid, &lrp-Quoted 1 line
args.range);
fs/nfs/pnfs.c +8
-0+if (lrp->ld_private.ops && lrp->ld_private.ops->free) + lrp->ld_private.ops->free (&lrp->ld_private); +if (lrp->inode) +nfs_iput_and_deactive (lrp->inode); +put_cred (cred); +kfree (lrp); +pnfs_put_layout_hdr (lo); +return status;}
/* Return true if layoutreturn is needed */base-commit:
cb015814f8b6eebcbb8e46e111d108892c5e6821
Signature · 3 lines
Trond Myklebust Linux NFS client maintainer, Hammerspace [email protected], [email protected]