From: Trond Myklebust Date: NFS: Ensure we always hold the BKL when dereferencing inode->i_flock Signed-off-by: Trond Myklebust --- fs/nfs/delegation.c | 26 ++++++++++---------------- fs/nfs/nfs4proc.c | 16 ++++++++++++++-- fs/nfs/nfs4state.c | 5 +++++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/fs/nfs/delegation.c b/fs/nfs/delegation.c index 968225a..cca405b 100644 --- a/fs/nfs/delegation.c +++ b/fs/nfs/delegation.c @@ -68,29 +68,20 @@ static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_ { struct inode *inode = state->inode; struct file_lock *fl; - int status; + int status = 0; + /* Protect inode->i_flock using the BKL */ + lock_kernel(); for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) continue; if (nfs_file_open_context(fl->fl_file) != ctx) continue; status = nfs4_lock_delegation_recall(state, fl); - if (status >= 0) - continue; - switch (status) { - default: - printk(KERN_ERR "%s: unhandled error %d.\n", - __func__, status); - case -NFS4ERR_EXPIRED: - /* kill_proc(fl->fl_pid, SIGLOST, 1); */ - case -NFS4ERR_STALE_CLIENTID: - nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client); - goto out_err; - } + if (status < 0) + break; } - return 0; -out_err: + unlock_kernel(); return status; } @@ -268,7 +259,10 @@ static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegat struct nfs_inode *nfsi = NFS_I(inode); nfs_msync_inode(inode); - /* Guard against new delegated open calls */ + /* + * Guard against new delegated open/lock/unlock calls and against + * state recovery + */ down_write(&nfsi->rwsem); nfs_delegation_claim_opens(inode, &delegation->stateid); up_write(&nfsi->rwsem); diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c index 476d59e..eb6ab15 100644 --- a/fs/nfs/nfs4proc.c +++ b/fs/nfs/nfs4proc.c @@ -3628,10 +3628,22 @@ int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl) do { err = _nfs4_do_setlk(state, F_SETLK, fl, 0); - if (err != -NFS4ERR_DELAY) - break; + switch (err) { + default: + printk(KERN_ERR "%s: unhandled error %d.\n", + __func__, err); + case 0: + goto out; + case -NFS4ERR_DELAY: + break; + case -NFS4ERR_EXPIRED: + case -NFS4ERR_STALE_CLIENTID: + nfs4_schedule_state_recovery(server->nfs_client); + goto out; + } err = nfs4_handle_exception(server, err, &exception); } while (exception.retry); +out: return err; } diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c index ee674b4..c8abe09 100644 --- a/fs/nfs/nfs4state.c +++ b/fs/nfs/nfs4state.c @@ -848,7 +848,10 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_ struct file_lock *fl; int status = 0; + /* Guard against delegation returns and new lock/unlock calls */ down_write(&nfsi->rwsem); + /* Protect inode->i_flock using the BKL */ + lock_kernel(); for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) continue; @@ -871,9 +874,11 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_ goto out_err; } } + unlock_kernel(); up_write(&nfsi->rwsem); return 0; out_err: + unlock_kernel(); up_write(&nfsi->rwsem); return status; }