2 * linux/fs/lockd/svclock.c
4 * Handling of server-side locks, mostly of the blocked variety.
5 * This is the ugliest part of lockd because we tread on very thin ice.
6 * GRANT and CANCEL calls may get stuck, meet in mid-flight, etc.
7 * IMNSHO introducing the grant callback into the NLM protocol was one
8 * of the worst ideas Sun ever had. Except maybe for the idea of doing
9 * NFS file locking at all.
11 * I'm trying hard to avoid race conditions by protecting most accesses
12 * to a file's list of blocked locks through a semaphore. The global
13 * list of blocked locks is not protected in this fashion however.
14 * Therefore, some functions (such as the RPC callback for the async grant
15 * call) move blocked locks towards the head of the list *while some other
16 * process might be traversing it*. This should not be a problem in
17 * practice, because this will only cause functions traversing the list
18 * to visit some blocks twice.
20 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
23 #include <linux/config.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/sunrpc/svc.h>
29 #include <linux/lockd/nlm.h>
30 #include <linux/lockd/lockd.h>
33 #define NLMDBG_FACILITY NLMDBG_SVCLOCK
35 static void nlmsvc_insert_block(struct nlm_block
*block
, unsigned long);
36 static int nlmsvc_remove_block(struct nlm_block
*block
);
37 static void nlmsvc_grant_callback(struct rpc_task
*task
);
38 static void nlmsvc_notify_blocked(struct file_lock
*);
41 * The list of blocked locks to retry
43 static struct nlm_block
* nlm_blocked
= NULL
;
46 * Insert a blocked lock into the global list
49 nlmsvc_insert_block(struct nlm_block
*block
, unsigned long when
)
51 struct nlm_block
**bp
, *b
;
53 dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block
, when
);
55 nlmsvc_remove_block(block
);
56 for (bp
= &nlm_blocked
; (b
= *bp
); bp
= &b
->b_next
)
67 * Remove a block from the global list
70 nlmsvc_remove_block(struct nlm_block
*block
)
72 struct nlm_block
**bp
, *b
;
76 for (bp
= &nlm_blocked
; (b
= *bp
); bp
= &b
->b_next
) {
88 * Find a block for a given lock and optionally remove it from
91 static struct nlm_block
*
92 nlmsvc_lookup_block(struct nlm_file
*file
, struct nlm_lock
*lock
, int remove
)
94 struct nlm_block
**head
, *block
;
97 dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
98 file
, lock
->fl
.fl_pid
,
99 (long long)lock
->fl
.fl_start
,
100 (long long)lock
->fl
.fl_end
, lock
->fl
.fl_type
);
101 for (head
= &nlm_blocked
; (block
= *head
); head
= &block
->b_next
) {
102 fl
= &block
->b_call
.a_args
.lock
.fl
;
103 dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%x\n",
104 block
->b_file
, fl
->fl_pid
,
105 (long long)fl
->fl_start
,
106 (long long)fl
->fl_end
, fl
->fl_type
,
107 *(unsigned int*)(block
->b_call
.a_args
.cookie
.data
));
108 if (block
->b_file
== file
&& nlm_compare_locks(fl
, &lock
->fl
)) {
110 *head
= block
->b_next
;
118 static inline int nlm_cookie_match(struct nlm_cookie
*a
, struct nlm_cookie
*b
)
122 if(memcmp(a
->data
,b
->data
,a
->len
))
128 * Find a block with a given NLM cookie.
130 static inline struct nlm_block
*
131 nlmsvc_find_block(struct nlm_cookie
*cookie
)
133 struct nlm_block
*block
;
135 for (block
= nlm_blocked
; block
; block
= block
->b_next
) {
136 dprintk("cookie: head of blocked queue %p, block %p\n",
138 if (nlm_cookie_match(&block
->b_call
.a_args
.cookie
,cookie
))
146 * Create a block and initialize it.
148 * Note: we explicitly set the cookie of the grant reply to that of
149 * the blocked lock request. The spec explicitly mentions that the client
150 * should _not_ rely on the callback containing the same cookie as the
151 * request, but (as I found out later) that's because some implementations
152 * do just this. Never mind the standards comittees, they support our
153 * logging industries.
155 static inline struct nlm_block
*
156 nlmsvc_create_block(struct svc_rqst
*rqstp
, struct nlm_file
*file
,
157 struct nlm_lock
*lock
, struct nlm_cookie
*cookie
)
159 struct nlm_block
*block
;
160 struct nlm_host
*host
;
161 struct nlm_rqst
*call
;
163 /* Create host handle for callback */
164 host
= nlmclnt_lookup_host(&rqstp
->rq_addr
,
165 rqstp
->rq_prot
, rqstp
->rq_vers
);
169 /* Allocate memory for block, and initialize arguments */
170 if (!(block
= (struct nlm_block
*) kmalloc(sizeof(*block
), GFP_KERNEL
)))
172 memset(block
, 0, sizeof(*block
));
174 /* Set notifier function for VFS, and init args */
175 lock
->fl
.fl_notify
= nlmsvc_notify_blocked
;
176 if (!nlmclnt_setgrantargs(&block
->b_call
, lock
))
178 block
->b_call
.a_args
.cookie
= *cookie
; /* see above */
180 dprintk("lockd: created block %p...\n", block
);
182 /* Create and initialize the block */
183 block
->b_daemon
= rqstp
->rq_server
;
184 block
->b_host
= host
;
185 block
->b_file
= file
;
187 /* Add to file's list of blocks */
188 block
->b_fnext
= file
->f_blocks
;
189 file
->f_blocks
= block
;
191 /* Set up RPC arguments for callback */
192 call
= &block
->b_call
;
194 call
->a_flags
= RPC_TASK_ASYNC
;
201 nlm_release_host(host
);
206 * Delete a block. If the lock was cancelled or the grant callback
207 * failed, unlock is set to 1.
208 * It is the caller's responsibility to check whether the file
209 * can be closed hereafter.
212 nlmsvc_delete_block(struct nlm_block
*block
, int unlock
)
214 struct file_lock
*fl
= &block
->b_call
.a_args
.lock
.fl
;
215 struct nlm_file
*file
= block
->b_file
;
216 struct nlm_block
**bp
;
218 dprintk("lockd: deleting block %p...\n", block
);
220 /* Remove block from list */
221 nlmsvc_remove_block(block
);
223 /* If granted, unlock it, else remove from inode block list */
224 if (unlock
&& block
->b_granted
) {
225 dprintk("lockd: deleting granted lock\n");
226 fl
->fl_type
= F_UNLCK
;
227 posix_lock_file(&block
->b_file
->f_file
, fl
, 0);
228 block
->b_granted
= 0;
230 dprintk("lockd: unblocking blocked lock\n");
231 posix_unblock_lock(fl
);
234 /* If the block is in the middle of a GRANT callback,
235 * don't kill it yet. */
236 if (block
->b_incall
) {
237 nlmsvc_insert_block(block
, NLM_NEVER
);
242 /* Remove block from file's list of blocks */
243 for (bp
= &file
->f_blocks
; *bp
; bp
= &(*bp
)->b_fnext
) {
245 *bp
= block
->b_fnext
;
251 nlm_release_host(block
->b_host
);
252 nlmclnt_freegrantargs(&block
->b_call
);
257 * Loop over all blocks and perform the action specified.
258 * (NLM_ACT_CHECK handled by nlmsvc_inspect_file).
261 nlmsvc_traverse_blocks(struct nlm_host
*host
, struct nlm_file
*file
, int action
)
263 struct nlm_block
*block
, *next
;
266 for (block
= file
->f_blocks
; block
; block
= next
) {
267 next
= block
->b_fnext
;
268 if (action
== NLM_ACT_MARK
)
269 block
->b_host
->h_inuse
= 1;
270 else if (action
== NLM_ACT_UNLOCK
) {
271 if (host
== NULL
|| host
== block
->b_host
)
272 nlmsvc_delete_block(block
, 1);
280 * Attempt to establish a lock, and if it can't be granted, block it
284 nlmsvc_lock(struct svc_rqst
*rqstp
, struct nlm_file
*file
,
285 struct nlm_lock
*lock
, int wait
, struct nlm_cookie
*cookie
)
287 struct file_lock
*conflock
;
288 struct nlm_block
*block
;
291 dprintk("lockd: nlmsvc_lock(%04x/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
292 file
->f_file
.f_dentry
->d_inode
->i_dev
,
293 file
->f_file
.f_dentry
->d_inode
->i_ino
,
294 lock
->fl
.fl_type
, lock
->fl
.fl_pid
,
295 (long long)lock
->fl
.fl_start
,
296 (long long)lock
->fl
.fl_end
,
299 /* Lock file against concurrent access */
302 /* Get existing block (in case client is busy-waiting) */
303 block
= nlmsvc_lookup_block(file
, lock
, 0);
305 lock
->fl
.fl_flags
|= FL_LOCKD
;
308 if (!(conflock
= posix_test_lock(&file
->f_file
, &lock
->fl
))) {
309 error
= posix_lock_file(&file
->f_file
, &lock
->fl
, 0);
312 nlmsvc_delete_block(block
, 0);
315 dprintk("lockd: posix_lock_file returned %d\n", -error
);
320 #ifdef CONFIG_LOCKD_V4
321 return nlm4_deadlock
; /* will be downgraded to lck_deined if this
322 * is a NLMv1,3 request */
324 /* no applicable NLM status */
327 return nlm_lck_denied
;
328 default: /* includes ENOLCK */
329 return nlm_lck_denied_nolocks
;
335 return nlm_lck_denied
;
338 /* If we don't have a block, create and initialize it. Then
339 * retry because we may have slept in kmalloc. */
341 dprintk("lockd: blocking on this lock (allocating).\n");
342 if (!(block
= nlmsvc_create_block(rqstp
, file
, lock
, cookie
)))
343 return nlm_lck_denied_nolocks
;
347 /* Append to list of blocked */
348 nlmsvc_insert_block(block
, NLM_NEVER
);
350 if (!block
->b_call
.a_args
.lock
.fl
.fl_prevblock
) {
351 /* Now add block to block list of the conflicting lock
352 if we haven't done so. */
353 dprintk("lockd: blocking on this lock.\n");
354 posix_block_lock(conflock
, &block
->b_call
.a_args
.lock
.fl
);
358 return nlm_lck_blocked
;
362 * Test for presence of a conflicting lock.
365 nlmsvc_testlock(struct nlm_file
*file
, struct nlm_lock
*lock
,
366 struct nlm_lock
*conflock
)
368 struct file_lock
*fl
;
370 dprintk("lockd: nlmsvc_testlock(%04x/%ld, ty=%d, %Ld-%Ld)\n",
371 file
->f_file
.f_dentry
->d_inode
->i_dev
,
372 file
->f_file
.f_dentry
->d_inode
->i_ino
,
374 (long long)lock
->fl
.fl_start
,
375 (long long)lock
->fl
.fl_end
);
377 if ((fl
= posix_test_lock(&file
->f_file
, &lock
->fl
)) != NULL
) {
378 dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
379 fl
->fl_type
, (long long)fl
->fl_start
,
380 (long long)fl
->fl_end
);
381 conflock
->caller
= "somehost"; /* FIXME */
382 conflock
->oh
.len
= 0; /* don't return OH info */
384 return nlm_lck_denied
;
392 * This implies a CANCEL call: We send a GRANT_MSG, the client replies
393 * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
394 * afterwards. In this case the block will still be there, and hence
398 nlmsvc_unlock(struct nlm_file
*file
, struct nlm_lock
*lock
)
402 dprintk("lockd: nlmsvc_unlock(%04x/%ld, pi=%d, %Ld-%Ld)\n",
403 file
->f_file
.f_dentry
->d_inode
->i_dev
,
404 file
->f_file
.f_dentry
->d_inode
->i_ino
,
406 (long long)lock
->fl
.fl_start
,
407 (long long)lock
->fl
.fl_end
);
409 /* First, cancel any lock that might be there */
410 nlmsvc_cancel_blocked(file
, lock
);
412 lock
->fl
.fl_type
= F_UNLCK
;
413 error
= posix_lock_file(&file
->f_file
, &lock
->fl
, 0);
415 return (error
< 0)? nlm_lck_denied_nolocks
: nlm_granted
;
419 * Cancel a previously blocked request.
421 * A cancel request always overrides any grant that may currently
423 * The calling procedure must check whether the file can be closed.
426 nlmsvc_cancel_blocked(struct nlm_file
*file
, struct nlm_lock
*lock
)
428 struct nlm_block
*block
;
430 dprintk("lockd: nlmsvc_cancel(%04x/%ld, pi=%d, %Ld-%Ld)\n",
431 file
->f_file
.f_dentry
->d_inode
->i_dev
,
432 file
->f_file
.f_dentry
->d_inode
->i_ino
,
434 (long long)lock
->fl
.fl_start
,
435 (long long)lock
->fl
.fl_end
);
438 if ((block
= nlmsvc_lookup_block(file
, lock
, 1)) != NULL
)
439 nlmsvc_delete_block(block
, 1);
445 * Unblock a blocked lock request. This is a callback invoked from the
446 * VFS layer when a lock on which we blocked is removed.
448 * This function doesn't grant the blocked lock instantly, but rather moves
449 * the block to the head of nlm_blocked where it can be picked up by lockd.
452 nlmsvc_notify_blocked(struct file_lock
*fl
)
454 struct nlm_block
**bp
, *block
;
456 dprintk("lockd: VFS unblock notification for block %p\n", fl
);
457 posix_unblock_lock(fl
);
458 for (bp
= &nlm_blocked
; (block
= *bp
); bp
= &block
->b_next
) {
459 if (nlm_compare_locks(&block
->b_call
.a_args
.lock
.fl
, fl
)) {
460 svc_wake_up(block
->b_daemon
);
461 nlmsvc_insert_block(block
, 0);
466 printk(KERN_WARNING
"lockd: notification for unknown block!\n");
470 * Try to claim a lock that was previously blocked.
472 * Note that we use both the RPC_GRANTED_MSG call _and_ an async
473 * RPC thread when notifying the client. This seems like overkill...
475 * - we don't want to use a synchronous RPC thread, otherwise
476 * we might find ourselves hanging on a dead portmapper.
477 * - Some lockd implementations (e.g. HP) don't react to
478 * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
481 nlmsvc_grant_blocked(struct nlm_block
*block
)
483 struct nlm_file
*file
= block
->b_file
;
484 struct nlm_lock
*lock
= &block
->b_call
.a_args
.lock
;
485 struct file_lock
*conflock
;
488 dprintk("lockd: grant blocked lock %p\n", block
);
490 /* First thing is lock the file */
493 /* Unlink block request from list */
494 nlmsvc_remove_block(block
);
496 /* If b_granted is true this means we've been here before.
497 * Just retry the grant callback, possibly refreshing the RPC
499 if (block
->b_granted
) {
500 nlm_rebind_host(block
->b_host
);
504 /* Try the lock operation again */
505 if ((conflock
= posix_test_lock(&file
->f_file
, &lock
->fl
)) != NULL
) {
506 /* Bummer, we blocked again */
507 dprintk("lockd: lock still blocked\n");
508 nlmsvc_insert_block(block
, NLM_NEVER
);
509 posix_block_lock(conflock
, &lock
->fl
);
514 /* Alright, no conflicting lock. Now lock it for real. If the
515 * following yields an error, this is most probably due to low
516 * memory. Retry the lock in a few seconds.
518 if ((error
= posix_lock_file(&file
->f_file
, &lock
->fl
, 0)) < 0) {
519 printk(KERN_WARNING
"lockd: unexpected error %d in %s!\n",
520 -error
, __FUNCTION__
);
521 nlmsvc_insert_block(block
, jiffies
+ 10 * HZ
);
527 /* Lock was granted by VFS. */
528 dprintk("lockd: GRANTing blocked lock.\n");
529 block
->b_granted
= 1;
532 /* Schedule next grant callback in 30 seconds */
533 nlmsvc_insert_block(block
, jiffies
+ 30 * HZ
);
535 /* Call the client */
536 nlmclnt_async_call(&block
->b_call
, NLMPROC_GRANTED_MSG
,
537 nlmsvc_grant_callback
);
542 * This is the callback from the RPC layer when the NLM_GRANTED_MSG
543 * RPC call has succeeded or timed out.
544 * Like all RPC callbacks, it is invoked by the rpciod process, so it
545 * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
546 * chain once more in order to have it removed by lockd itself (which can
547 * then sleep on the file semaphore without disrupting e.g. the nfs client).
550 nlmsvc_grant_callback(struct rpc_task
*task
)
552 struct nlm_rqst
*call
= (struct nlm_rqst
*) task
->tk_calldata
;
553 struct nlm_block
*block
;
554 unsigned long timeout
;
556 dprintk("lockd: GRANT_MSG RPC callback\n");
557 dprintk("callback: looking for cookie %x \n",
558 *(unsigned int *)(call
->a_args
.cookie
.data
));
559 if (!(block
= nlmsvc_find_block(&call
->a_args
.cookie
))) {
560 dprintk("lockd: no block for cookie %x\n", *(u32
*)(call
->a_args
.cookie
.data
));
564 /* Technically, we should down the file semaphore here. Since we
565 * move the block towards the head of the queue only, no harm
566 * can be done, though. */
567 if (task
->tk_status
< 0) {
568 /* RPC error: Re-insert for retransmission */
569 timeout
= jiffies
+ 10 * HZ
;
570 } else if (block
->b_done
) {
571 /* Block already removed, kill it for real */
574 /* Call was successful, now wait for client callback */
575 timeout
= jiffies
+ 60 * HZ
;
577 nlmsvc_insert_block(block
, timeout
);
578 svc_wake_up(block
->b_daemon
);
581 nlm_release_host(call
->a_host
);
585 * We received a GRANT_RES callback. Try to find the corresponding
589 nlmsvc_grant_reply(struct nlm_cookie
*cookie
, u32 status
)
591 struct nlm_block
*block
;
592 struct nlm_file
*file
;
594 if (!(block
= nlmsvc_find_block(cookie
)))
596 file
= block
->b_file
;
600 if ((block
= nlmsvc_find_block(cookie
)) != NULL
) {
601 if (status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
602 /* Try again in a couple of seconds */
603 nlmsvc_insert_block(block
, jiffies
+ 10 * HZ
);
606 /* Lock is now held by client, or has been rejected.
607 * In both cases, the block should be removed. */
610 if (status
== NLM_LCK_GRANTED
)
611 nlmsvc_delete_block(block
, 0);
613 nlmsvc_delete_block(block
, 1);
618 nlm_release_file(file
);
622 * Retry all blocked locks that have been notified. This is where lockd
623 * picks up locks that can be granted, or grant notifications that must
627 nlmsvc_retry_blocked(void)
629 struct nlm_block
*block
;
631 dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
633 nlm_blocked
? nlm_blocked
->b_when
: 0);
634 while ((block
= nlm_blocked
) && block
->b_when
<= jiffies
) {
635 dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n",
636 block
, block
->b_when
, block
->b_done
);
638 nlmsvc_delete_block(block
, 0);
640 nlmsvc_grant_blocked(block
);
643 if ((block
= nlm_blocked
) && block
->b_when
!= NLM_NEVER
)
644 return (block
->b_when
- jiffies
);
646 return MAX_SCHEDULE_TIMEOUT
;