Skip to content

Commit a1d617d

Browse files
jtlaytonamschuma-ntap
authored andcommitted
nfs: allow blocking locks to be awoken by lock callbacks
Add a waitqueue head to the client structure. Have clients set a wait on that queue prior to requesting a lock from the server. If the lock is blocked, then we can use that to wait for wakeups. Note that we do need to do this "manually" since we need to set the wait on the waitqueue prior to requesting the lock, but requesting a lock can involve activities that can block. However, only do that for NFSv4.1 locks, either by compiling out all of the waitqueue handling when CONFIG_NFS_V4_1 is disabled, or skipping all of it at runtime if we're dealing with v4.0, or v4.1 servers that don't send lock callbacks. Note too that even when we expect to get a lock callback, RFC5661 section 20.11.4 is pretty clear that we still need to poll for them, so we do still sleep on a timeout. We do however always poll at the longest interval in that case. Signed-off-by: Jeff Layton <[email protected]> [Anna: nfs4_retry_setlk() "status" should default to -ERESTARTSYS] Signed-off-by: Anna Schumaker <[email protected]>
1 parent d2f3a7f commit a1d617d

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

fs/nfs/callback_proc.c

+4
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ __be32 nfs4_callback_notify_lock(struct cb_notify_lock_args *args, void *dummy,
638638
dprintk_rcu("NFS: CB_NOTIFY_LOCK request from %s\n",
639639
rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
640640

641+
/* Don't wake anybody if the string looked bogus */
642+
if (args->cbnl_valid)
643+
__wake_up(&cps->clp->cl_lock_waitq, TASK_NORMAL, 0, args);
644+
641645
return htonl(NFS4_OK);
642646
}
643647
#endif /* CONFIG_NFS_V4_1 */

fs/nfs/nfs4client.c

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
199199
clp->cl_minorversion = cl_init->minorversion;
200200
clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
201201
clp->cl_mig_gen = 1;
202+
#if IS_ENABLED(CONFIG_NFS_V4_1)
203+
init_waitqueue_head(&clp->cl_lock_waitq);
204+
#endif
202205
return clp;
203206

204207
error:

fs/nfs/nfs4proc.c

+93-1
Original file line numberDiff line numberDiff line change
@@ -6166,7 +6166,8 @@ static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *
61666166
#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
61676167

61686168
static int
6169-
nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6169+
nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6170+
struct file_lock *request)
61706171
{
61716172
int status = -ERESTARTSYS;
61726173
unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
@@ -6183,6 +6184,97 @@ nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
61836184
return status;
61846185
}
61856186

6187+
#ifdef CONFIG_NFS_V4_1
6188+
struct nfs4_lock_waiter {
6189+
struct task_struct *task;
6190+
struct inode *inode;
6191+
struct nfs_lowner *owner;
6192+
bool notified;
6193+
};
6194+
6195+
static int
6196+
nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6197+
{
6198+
int ret;
6199+
struct cb_notify_lock_args *cbnl = key;
6200+
struct nfs4_lock_waiter *waiter = wait->private;
6201+
struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6202+
*wowner = waiter->owner;
6203+
6204+
/* Only wake if the callback was for the same owner */
6205+
if (lowner->clientid != wowner->clientid ||
6206+
lowner->id != wowner->id ||
6207+
lowner->s_dev != wowner->s_dev)
6208+
return 0;
6209+
6210+
/* Make sure it's for the right inode */
6211+
if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6212+
return 0;
6213+
6214+
waiter->notified = true;
6215+
6216+
/* override "private" so we can use default_wake_function */
6217+
wait->private = waiter->task;
6218+
ret = autoremove_wake_function(wait, mode, flags, key);
6219+
wait->private = waiter;
6220+
return ret;
6221+
}
6222+
6223+
static int
6224+
nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6225+
{
6226+
int status = -ERESTARTSYS;
6227+
unsigned long flags;
6228+
struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6229+
struct nfs_server *server = NFS_SERVER(state->inode);
6230+
struct nfs_client *clp = server->nfs_client;
6231+
wait_queue_head_t *q = &clp->cl_lock_waitq;
6232+
struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6233+
.id = lsp->ls_seqid.owner_id,
6234+
.s_dev = server->s_dev };
6235+
struct nfs4_lock_waiter waiter = { .task = current,
6236+
.inode = state->inode,
6237+
.owner = &owner,
6238+
.notified = false };
6239+
wait_queue_t wait;
6240+
6241+
/* Don't bother with waitqueue if we don't expect a callback */
6242+
if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6243+
return nfs4_retry_setlk_simple(state, cmd, request);
6244+
6245+
init_wait(&wait);
6246+
wait.private = &waiter;
6247+
wait.func = nfs4_wake_lock_waiter;
6248+
add_wait_queue(q, &wait);
6249+
6250+
while(!signalled()) {
6251+
status = nfs4_proc_setlk(state, cmd, request);
6252+
if ((status != -EAGAIN) || IS_SETLK(cmd))
6253+
break;
6254+
6255+
status = -ERESTARTSYS;
6256+
spin_lock_irqsave(&q->lock, flags);
6257+
if (waiter.notified) {
6258+
spin_unlock_irqrestore(&q->lock, flags);
6259+
continue;
6260+
}
6261+
set_current_state(TASK_INTERRUPTIBLE);
6262+
spin_unlock_irqrestore(&q->lock, flags);
6263+
6264+
freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6265+
}
6266+
6267+
finish_wait(q, &wait);
6268+
return status;
6269+
}
6270+
#else /* !CONFIG_NFS_V4_1 */
6271+
static inline int
6272+
nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6273+
{
6274+
return nfs4_retry_setlk_simple(state, cmd, request);
6275+
}
6276+
#endif
6277+
61866278
static int
61876279
nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
61886280
{

include/linux/nfs_fs_sb.h

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ struct nfs_client {
103103
#define NFS_SP4_MACH_CRED_WRITE 5 /* WRITE */
104104
#define NFS_SP4_MACH_CRED_COMMIT 6 /* COMMIT */
105105
#define NFS_SP4_MACH_CRED_PNFS_CLEANUP 7 /* LAYOUTRETURN */
106+
#if IS_ENABLED(CONFIG_NFS_V4_1)
107+
wait_queue_head_t cl_lock_waitq;
108+
#endif /* CONFIG_NFS_V4_1 */
106109
#endif /* CONFIG_NFS_V4 */
107110

108111
/* Our own IP address, as a null-terminated string.

0 commit comments

Comments
 (0)