Skip to content

Commit

Permalink
NFS41: make close wait for layoutreturn
Browse files Browse the repository at this point in the history
If we send a layoutreturn asynchronously before close, the close
might reach server first and layoutreturn would fail with BADSTATEID
because there is nothing keeping the layout stateid alive.

Also do not pretend sending layoutreturn if we are not.

Signed-off-by: Peng Tao <[email protected]>
Signed-off-by: Trond Myklebust <[email protected]>
  • Loading branch information
Peng Tao authored and trondmypd committed Sep 23, 2015
1 parent 834e465 commit 500d701
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
17 changes: 17 additions & 0 deletions fs/nfs/nfs4proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2658,6 +2658,15 @@ static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
return err;
}

static bool
nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
{
if (inode == NULL || !nfs_have_layout(inode))
return false;

return pnfs_wait_on_layoutreturn(inode, task);
}

struct nfs4_closedata {
struct inode *inode;
struct nfs4_state *state;
Expand Down Expand Up @@ -2776,6 +2785,11 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data)
goto out_no_action;
}

if (nfs4_wait_on_layoutreturn(inode, task)) {
nfs_release_seqid(calldata->arg.seqid);
goto out_wait;
}

if (calldata->arg.fmode == 0)
task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
if (calldata->roc)
Expand Down Expand Up @@ -5321,6 +5335,9 @@ static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)

d_data = (struct nfs4_delegreturndata *)data;

if (nfs4_wait_on_layoutreturn(d_data->inode, task))
return;

if (d_data->roc)
pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);

Expand Down
35 changes: 25 additions & 10 deletions fs/nfs/pnfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,20 +1104,15 @@ bool pnfs_roc(struct inode *ino)
mark_lseg_invalid(lseg, &tmp_list);
found = true;
}
/* pnfs_prepare_layoutreturn() grabs lo ref and it will be put
* in pnfs_roc_release(). We don't really send a layoutreturn but
* still want others to view us like we are sending one!
*
* If pnfs_prepare_layoutreturn() fails, it means someone else is doing
* LAYOUTRETURN, so we proceed like there are no layouts to return.
*
* ROC in three conditions:
/* ROC in two conditions:
* 1. there are ROC lsegs
* 2. we don't send layoutreturn
* 3. no others are sending layoutreturn
*/
if (found && !layoutreturn && pnfs_prepare_layoutreturn(lo))
if (found && !layoutreturn) {
/* lo ref dropped in pnfs_roc_release() */
pnfs_get_layout_hdr(lo);
roc = true;
}

out_noroc:
spin_unlock(&ino->i_lock);
Expand Down Expand Up @@ -1172,6 +1167,26 @@ void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
spin_unlock(&ino->i_lock);
}

bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
{
struct nfs_inode *nfsi = NFS_I(ino);
struct pnfs_layout_hdr *lo;
bool sleep = false;

/* we might not have grabbed lo reference. so need to check under
* i_lock */
spin_lock(&ino->i_lock);
lo = nfsi->layout;
if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
sleep = true;
spin_unlock(&ino->i_lock);

if (sleep)
rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);

return sleep;
}

/*
* Compare two layout segments for sorting into layout cache.
* We want to preferentially return RW over RO layouts, so ensure those
Expand Down
7 changes: 7 additions & 0 deletions fs/nfs/pnfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ bool pnfs_roc(struct inode *ino);
void pnfs_roc_release(struct inode *ino);
void pnfs_roc_set_barrier(struct inode *ino, u32 barrier);
void pnfs_roc_get_barrier(struct inode *ino, u32 *barrier);
bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task);
void pnfs_set_layoutcommit(struct inode *, struct pnfs_layout_segment *, loff_t);
void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data);
int pnfs_layoutcommit_inode(struct inode *inode, bool sync);
Expand Down Expand Up @@ -639,6 +640,12 @@ pnfs_roc_get_barrier(struct inode *ino, u32 *barrier)
{
}

static inline bool
pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
{
return false;
}

static inline void set_pnfs_layoutdriver(struct nfs_server *s,
const struct nfs_fh *mntfh, u32 id)
{
Expand Down

0 comments on commit 500d701

Please sign in to comment.