Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 19e03c57 authored by Trond Myklebust's avatar Trond Myklebust
Browse files

NFSv4: Ensure that file unlock requests don't conflict with state recovery



The unlock path is currently failing to take the nfs_client->cl_sem read
lock, and hence the recovery path may see locks disappear from underneath
it.
Also ensure that it takes the nfs_inode->rwsem read lock so that it there
is no conflict with delegation recalls.

Signed-off-by: default avatarTrond Myklebust <Trond.Myklebust@netapp.com>
parent 65de872e
Loading
Loading
Loading
Loading
+16 −10
Original line number Original line Diff line number Diff line
@@ -3273,6 +3273,8 @@ static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,


static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
{
{
	struct nfs_client *clp = state->owner->so_client;
	struct nfs_inode *nfsi = NFS_I(state->inode);
	struct nfs_seqid *seqid;
	struct nfs_seqid *seqid;
	struct nfs4_lock_state *lsp;
	struct nfs4_lock_state *lsp;
	struct rpc_task *task;
	struct rpc_task *task;
@@ -3282,8 +3284,15 @@ static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *
	status = nfs4_set_lock_state(state, request);
	status = nfs4_set_lock_state(state, request);
	/* Unlock _before_ we do the RPC call */
	/* Unlock _before_ we do the RPC call */
	request->fl_flags |= FL_EXISTS;
	request->fl_flags |= FL_EXISTS;
	if (do_vfs_lock(request->fl_file, request) == -ENOENT)
	down_read(&clp->cl_sem);
	down_read(&nfsi->rwsem);
	if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
		up_read(&nfsi->rwsem);
		up_read(&clp->cl_sem);
		goto out;
		goto out;
	}
	up_read(&nfsi->rwsem);
	up_read(&clp->cl_sem);
	if (status != 0)
	if (status != 0)
		goto out;
		goto out;
	/* Is this a delegated lock? */
	/* Is this a delegated lock? */
@@ -3510,6 +3519,7 @@ static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request
static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
{
{
	struct nfs_client *clp = state->owner->so_client;
	struct nfs_client *clp = state->owner->so_client;
	struct nfs_inode *nfsi = NFS_I(state->inode);
	unsigned char fl_flags = request->fl_flags;
	unsigned char fl_flags = request->fl_flags;
	int status;
	int status;


@@ -3522,19 +3532,14 @@ static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock
	if (status < 0)
	if (status < 0)
		goto out;
		goto out;
	down_read(&clp->cl_sem);
	down_read(&clp->cl_sem);
	down_read(&nfsi->rwsem);
	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
		struct nfs_inode *nfsi = NFS_I(state->inode);
		/* Yes: cache locks! */
		/* Yes: cache locks! */
		down_read(&nfsi->rwsem);
		/* ...but avoid races with delegation recall... */
		/* ...but avoid races with delegation recall... */
		if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
		request->fl_flags = fl_flags & ~FL_SLEEP;
		request->fl_flags = fl_flags & ~FL_SLEEP;
		status = do_vfs_lock(request->fl_file, request);
		status = do_vfs_lock(request->fl_file, request);
			up_read(&nfsi->rwsem);
		goto out_unlock;
		goto out_unlock;
	}
	}
		up_read(&nfsi->rwsem);
	}
	status = _nfs4_do_setlk(state, cmd, request, 0);
	status = _nfs4_do_setlk(state, cmd, request, 0);
	if (status != 0)
	if (status != 0)
		goto out_unlock;
		goto out_unlock;
@@ -3543,6 +3548,7 @@ static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock
	if (do_vfs_lock(request->fl_file, request) < 0)
	if (do_vfs_lock(request->fl_file, request) < 0)
		printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
		printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
out_unlock:
out_unlock:
	up_read(&nfsi->rwsem);
	up_read(&clp->cl_sem);
	up_read(&clp->cl_sem);
out:
out:
	request->fl_flags = fl_flags;
	request->fl_flags = fl_flags;
+4 −0
Original line number Original line Diff line number Diff line
@@ -849,9 +849,11 @@ static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_s
static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
{
{
	struct inode *inode = state->inode;
	struct inode *inode = state->inode;
	struct nfs_inode *nfsi = NFS_I(inode);
	struct file_lock *fl;
	struct file_lock *fl;
	int status = 0;
	int status = 0;


	down_write(&nfsi->rwsem);
	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
		if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
			continue;
			continue;
@@ -874,8 +876,10 @@ static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_
				goto out_err;
				goto out_err;
		}
		}
	}
	}
	up_write(&nfsi->rwsem);
	return 0;
	return 0;
out_err:
out_err:
	up_write(&nfsi->rwsem);
	return status;
	return status;
}
}