Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6] / fs / lockd / svclock.c
1 /*
2  * linux/fs/lockd/svclock.c
3  *
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.
10  *
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.
19  *
20  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
21  */
22
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/sunrpc/svc.h>
30 #include <linux/lockd/nlm.h>
31 #include <linux/lockd/lockd.h>
32 #include <linux/kthread.h>
33
34 #define NLMDBG_FACILITY         NLMDBG_SVCLOCK
35
36 #ifdef CONFIG_LOCKD_V4
37 #define nlm_deadlock    nlm4_deadlock
38 #else
39 #define nlm_deadlock    nlm_lck_denied
40 #endif
41
42 static void nlmsvc_release_block(struct nlm_block *block);
43 static void     nlmsvc_insert_block(struct nlm_block *block, unsigned long);
44 static void     nlmsvc_remove_block(struct nlm_block *block);
45
46 static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock);
47 static void nlmsvc_freegrantargs(struct nlm_rqst *call);
48 static const struct rpc_call_ops nlmsvc_grant_ops;
49
50 /*
51  * The list of blocked locks to retry
52  */
53 static LIST_HEAD(nlm_blocked);
54
55 /*
56  * Insert a blocked lock into the global list
57  */
58 static void
59 nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
60 {
61         struct nlm_block *b;
62         struct list_head *pos;
63
64         dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
65         if (list_empty(&block->b_list)) {
66                 kref_get(&block->b_count);
67         } else {
68                 list_del_init(&block->b_list);
69         }
70
71         pos = &nlm_blocked;
72         if (when != NLM_NEVER) {
73                 if ((when += jiffies) == NLM_NEVER)
74                         when ++;
75                 list_for_each(pos, &nlm_blocked) {
76                         b = list_entry(pos, struct nlm_block, b_list);
77                         if (time_after(b->b_when,when) || b->b_when == NLM_NEVER)
78                                 break;
79                 }
80                 /* On normal exit from the loop, pos == &nlm_blocked,
81                  * so we will be adding to the end of the list - good
82                  */
83         }
84
85         list_add_tail(&block->b_list, pos);
86         block->b_when = when;
87 }
88
89 /*
90  * Remove a block from the global list
91  */
92 static inline void
93 nlmsvc_remove_block(struct nlm_block *block)
94 {
95         if (!list_empty(&block->b_list)) {
96                 list_del_init(&block->b_list);
97                 nlmsvc_release_block(block);
98         }
99 }
100
101 /*
102  * Find a block for a given lock
103  */
104 static struct nlm_block *
105 nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock)
106 {
107         struct nlm_block        *block;
108         struct file_lock        *fl;
109
110         dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
111                                 file, lock->fl.fl_pid,
112                                 (long long)lock->fl.fl_start,
113                                 (long long)lock->fl.fl_end, lock->fl.fl_type);
114         list_for_each_entry(block, &nlm_blocked, b_list) {
115                 fl = &block->b_call->a_args.lock.fl;
116                 dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n",
117                                 block->b_file, fl->fl_pid,
118                                 (long long)fl->fl_start,
119                                 (long long)fl->fl_end, fl->fl_type,
120                                 nlmdbg_cookie2a(&block->b_call->a_args.cookie));
121                 if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
122                         kref_get(&block->b_count);
123                         return block;
124                 }
125         }
126
127         return NULL;
128 }
129
130 static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b)
131 {
132         if (a->len != b->len)
133                 return 0;
134         if (memcmp(a->data, b->data, a->len))
135                 return 0;
136         return 1;
137 }
138
139 /*
140  * Find a block with a given NLM cookie.
141  */
142 static inline struct nlm_block *
143 nlmsvc_find_block(struct nlm_cookie *cookie)
144 {
145         struct nlm_block *block;
146
147         list_for_each_entry(block, &nlm_blocked, b_list) {
148                 if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie))
149                         goto found;
150         }
151
152         return NULL;
153
154 found:
155         dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block);
156         kref_get(&block->b_count);
157         return block;
158 }
159
160 /*
161  * Create a block and initialize it.
162  *
163  * Note: we explicitly set the cookie of the grant reply to that of
164  * the blocked lock request. The spec explicitly mentions that the client
165  * should _not_ rely on the callback containing the same cookie as the
166  * request, but (as I found out later) that's because some implementations
167  * do just this. Never mind the standards comittees, they support our
168  * logging industries.
169  *
170  * 10 years later: I hope we can safely ignore these old and broken
171  * clients by now. Let's fix this so we can uniquely identify an incoming
172  * GRANTED_RES message by cookie, without having to rely on the client's IP
173  * address. --okir
174  */
175 static struct nlm_block *
176 nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
177                     struct nlm_file *file, struct nlm_lock *lock,
178                     struct nlm_cookie *cookie)
179 {
180         struct nlm_block        *block;
181         struct nlm_rqst         *call = NULL;
182
183         nlm_get_host(host);
184         call = nlm_alloc_call(host);
185         if (call == NULL)
186                 return NULL;
187
188         /* Allocate memory for block, and initialize arguments */
189         block = kzalloc(sizeof(*block), GFP_KERNEL);
190         if (block == NULL)
191                 goto failed;
192         kref_init(&block->b_count);
193         INIT_LIST_HEAD(&block->b_list);
194         INIT_LIST_HEAD(&block->b_flist);
195
196         if (!nlmsvc_setgrantargs(call, lock))
197                 goto failed_free;
198
199         /* Set notifier function for VFS, and init args */
200         call->a_args.lock.fl.fl_flags |= FL_SLEEP;
201         call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
202         nlmclnt_next_cookie(&call->a_args.cookie);
203
204         dprintk("lockd: created block %p...\n", block);
205
206         /* Create and initialize the block */
207         block->b_daemon = rqstp->rq_server;
208         block->b_host   = host;
209         block->b_file   = file;
210         block->b_fl = NULL;
211         file->f_count++;
212
213         /* Add to file's list of blocks */
214         list_add(&block->b_flist, &file->f_blocks);
215
216         /* Set up RPC arguments for callback */
217         block->b_call = call;
218         call->a_flags   = RPC_TASK_ASYNC;
219         call->a_block = block;
220
221         return block;
222
223 failed_free:
224         kfree(block);
225 failed:
226         nlm_release_call(call);
227         return NULL;
228 }
229
230 /*
231  * Delete a block.
232  * It is the caller's responsibility to check whether the file
233  * can be closed hereafter.
234  */
235 static int nlmsvc_unlink_block(struct nlm_block *block)
236 {
237         int status;
238         dprintk("lockd: unlinking block %p...\n", block);
239
240         /* Remove block from list */
241         status = posix_unblock_lock(block->b_file->f_file, &block->b_call->a_args.lock.fl);
242         nlmsvc_remove_block(block);
243         return status;
244 }
245
246 static void nlmsvc_free_block(struct kref *kref)
247 {
248         struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
249         struct nlm_file         *file = block->b_file;
250
251         dprintk("lockd: freeing block %p...\n", block);
252
253         /* Remove block from file's list of blocks */
254         mutex_lock(&file->f_mutex);
255         list_del_init(&block->b_flist);
256         mutex_unlock(&file->f_mutex);
257
258         nlmsvc_freegrantargs(block->b_call);
259         nlm_release_call(block->b_call);
260         nlm_release_file(block->b_file);
261         kfree(block->b_fl);
262         kfree(block);
263 }
264
265 static void nlmsvc_release_block(struct nlm_block *block)
266 {
267         if (block != NULL)
268                 kref_put(&block->b_count, nlmsvc_free_block);
269 }
270
271 /*
272  * Loop over all blocks and delete blocks held by
273  * a matching host.
274  */
275 void nlmsvc_traverse_blocks(struct nlm_host *host,
276                         struct nlm_file *file,
277                         nlm_host_match_fn_t match)
278 {
279         struct nlm_block *block, *next;
280
281 restart:
282         mutex_lock(&file->f_mutex);
283         list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
284                 if (!match(block->b_host, host))
285                         continue;
286                 /* Do not destroy blocks that are not on
287                  * the global retry list - why? */
288                 if (list_empty(&block->b_list))
289                         continue;
290                 kref_get(&block->b_count);
291                 mutex_unlock(&file->f_mutex);
292                 nlmsvc_unlink_block(block);
293                 nlmsvc_release_block(block);
294                 goto restart;
295         }
296         mutex_unlock(&file->f_mutex);
297 }
298
299 /*
300  * Initialize arguments for GRANTED call. The nlm_rqst structure
301  * has been cleared already.
302  */
303 static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
304 {
305         locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
306         memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
307         call->a_args.lock.caller = utsname()->nodename;
308         call->a_args.lock.oh.len = lock->oh.len;
309
310         /* set default data area */
311         call->a_args.lock.oh.data = call->a_owner;
312         call->a_args.lock.svid = lock->fl.fl_pid;
313
314         if (lock->oh.len > NLMCLNT_OHSIZE) {
315                 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
316                 if (!data)
317                         return 0;
318                 call->a_args.lock.oh.data = (u8 *) data;
319         }
320
321         memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
322         return 1;
323 }
324
325 static void nlmsvc_freegrantargs(struct nlm_rqst *call)
326 {
327         if (call->a_args.lock.oh.data != call->a_owner)
328                 kfree(call->a_args.lock.oh.data);
329 }
330
331 /*
332  * Deferred lock request handling for non-blocking lock
333  */
334 static __be32
335 nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
336 {
337         __be32 status = nlm_lck_denied_nolocks;
338
339         block->b_flags |= B_QUEUED;
340
341         nlmsvc_insert_block(block, NLM_TIMEOUT);
342
343         block->b_cache_req = &rqstp->rq_chandle;
344         if (rqstp->rq_chandle.defer) {
345                 block->b_deferred_req =
346                         rqstp->rq_chandle.defer(block->b_cache_req);
347                 if (block->b_deferred_req != NULL)
348                         status = nlm_drop_reply;
349         }
350         dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
351                 block, block->b_flags, ntohl(status));
352
353         return status;
354 }
355
356 /*
357  * Attempt to establish a lock, and if it can't be granted, block it
358  * if required.
359  */
360 __be32
361 nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
362             struct nlm_host *host, struct nlm_lock *lock, int wait,
363             struct nlm_cookie *cookie, int reclaim)
364 {
365         struct nlm_block        *block = NULL;
366         int                     error;
367         __be32                  ret;
368
369         dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
370                                 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
371                                 file->f_file->f_path.dentry->d_inode->i_ino,
372                                 lock->fl.fl_type, lock->fl.fl_pid,
373                                 (long long)lock->fl.fl_start,
374                                 (long long)lock->fl.fl_end,
375                                 wait);
376
377         /* Lock file against concurrent access */
378         mutex_lock(&file->f_mutex);
379         /* Get existing block (in case client is busy-waiting)
380          * or create new block
381          */
382         block = nlmsvc_lookup_block(file, lock);
383         if (block == NULL) {
384                 block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
385                 ret = nlm_lck_denied_nolocks;
386                 if (block == NULL)
387                         goto out;
388                 lock = &block->b_call->a_args.lock;
389         } else
390                 lock->fl.fl_flags &= ~FL_SLEEP;
391
392         if (block->b_flags & B_QUEUED) {
393                 dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
394                                                         block, block->b_flags);
395                 if (block->b_granted) {
396                         nlmsvc_unlink_block(block);
397                         ret = nlm_granted;
398                         goto out;
399                 }
400                 if (block->b_flags & B_TIMED_OUT) {
401                         nlmsvc_unlink_block(block);
402                         ret = nlm_lck_denied;
403                         goto out;
404                 }
405                 ret = nlm_drop_reply;
406                 goto out;
407         }
408
409         if (locks_in_grace() && !reclaim) {
410                 ret = nlm_lck_denied_grace_period;
411                 goto out;
412         }
413         if (reclaim && !locks_in_grace()) {
414                 ret = nlm_lck_denied_grace_period;
415                 goto out;
416         }
417
418         if (!wait)
419                 lock->fl.fl_flags &= ~FL_SLEEP;
420         error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
421         lock->fl.fl_flags &= ~FL_SLEEP;
422
423         dprintk("lockd: vfs_lock_file returned %d\n", error);
424         switch (error) {
425                 case 0:
426                         ret = nlm_granted;
427                         goto out;
428                 case -EAGAIN:
429                         ret = nlm_lck_denied;
430                         break;
431                 case FILE_LOCK_DEFERRED:
432                         if (wait)
433                                 break;
434                         /* Filesystem lock operation is in progress
435                            Add it to the queue waiting for callback */
436                         ret = nlmsvc_defer_lock_rqst(rqstp, block);
437                         goto out;
438                 case -EDEADLK:
439                         ret = nlm_deadlock;
440                         goto out;
441                 default:                        /* includes ENOLCK */
442                         ret = nlm_lck_denied_nolocks;
443                         goto out;
444         }
445
446         ret = nlm_lck_denied;
447         if (!wait)
448                 goto out;
449
450         ret = nlm_lck_blocked;
451
452         /* Append to list of blocked */
453         nlmsvc_insert_block(block, NLM_NEVER);
454 out:
455         mutex_unlock(&file->f_mutex);
456         nlmsvc_release_block(block);
457         dprintk("lockd: nlmsvc_lock returned %u\n", ret);
458         return ret;
459 }
460
461 /*
462  * Test for presence of a conflicting lock.
463  */
464 __be32
465 nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
466                 struct nlm_host *host, struct nlm_lock *lock,
467                 struct nlm_lock *conflock, struct nlm_cookie *cookie)
468 {
469         struct nlm_block        *block = NULL;
470         int                     error;
471         __be32                  ret;
472
473         dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
474                                 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
475                                 file->f_file->f_path.dentry->d_inode->i_ino,
476                                 lock->fl.fl_type,
477                                 (long long)lock->fl.fl_start,
478                                 (long long)lock->fl.fl_end);
479
480         /* Get existing block (in case client is busy-waiting) */
481         block = nlmsvc_lookup_block(file, lock);
482
483         if (block == NULL) {
484                 struct file_lock *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
485
486                 if (conf == NULL)
487                         return nlm_granted;
488                 block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
489                 if (block == NULL) {
490                         kfree(conf);
491                         return nlm_granted;
492                 }
493                 block->b_fl = conf;
494         }
495         if (block->b_flags & B_QUEUED) {
496                 dprintk("lockd: nlmsvc_testlock deferred block %p flags %d fl %p\n",
497                         block, block->b_flags, block->b_fl);
498                 if (block->b_flags & B_TIMED_OUT) {
499                         nlmsvc_unlink_block(block);
500                         ret = nlm_lck_denied;
501                         goto out;
502                 }
503                 if (block->b_flags & B_GOT_CALLBACK) {
504                         nlmsvc_unlink_block(block);
505                         if (block->b_fl != NULL
506                                         && block->b_fl->fl_type != F_UNLCK) {
507                                 lock->fl = *block->b_fl;
508                                 goto conf_lock;
509                         } else {
510                                 ret = nlm_granted;
511                                 goto out;
512                         }
513                 }
514                 ret = nlm_drop_reply;
515                 goto out;
516         }
517
518         if (locks_in_grace()) {
519                 ret = nlm_lck_denied_grace_period;
520                 goto out;
521         }
522         error = vfs_test_lock(file->f_file, &lock->fl);
523         if (error == FILE_LOCK_DEFERRED) {
524                 ret = nlmsvc_defer_lock_rqst(rqstp, block);
525                 goto out;
526         }
527         if (error) {
528                 ret = nlm_lck_denied_nolocks;
529                 goto out;
530         }
531         if (lock->fl.fl_type == F_UNLCK) {
532                 ret = nlm_granted;
533                 goto out;
534         }
535
536 conf_lock:
537         dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
538                 lock->fl.fl_type, (long long)lock->fl.fl_start,
539                 (long long)lock->fl.fl_end);
540         conflock->caller = "somehost";  /* FIXME */
541         conflock->len = strlen(conflock->caller);
542         conflock->oh.len = 0;           /* don't return OH info */
543         conflock->svid = lock->fl.fl_pid;
544         conflock->fl.fl_type = lock->fl.fl_type;
545         conflock->fl.fl_start = lock->fl.fl_start;
546         conflock->fl.fl_end = lock->fl.fl_end;
547         ret = nlm_lck_denied;
548 out:
549         if (block)
550                 nlmsvc_release_block(block);
551         return ret;
552 }
553
554 /*
555  * Remove a lock.
556  * This implies a CANCEL call: We send a GRANT_MSG, the client replies
557  * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
558  * afterwards. In this case the block will still be there, and hence
559  * must be removed.
560  */
561 __be32
562 nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock)
563 {
564         int     error;
565
566         dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
567                                 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
568                                 file->f_file->f_path.dentry->d_inode->i_ino,
569                                 lock->fl.fl_pid,
570                                 (long long)lock->fl.fl_start,
571                                 (long long)lock->fl.fl_end);
572
573         /* First, cancel any lock that might be there */
574         nlmsvc_cancel_blocked(file, lock);
575
576         lock->fl.fl_type = F_UNLCK;
577         error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
578
579         return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
580 }
581
582 /*
583  * Cancel a previously blocked request.
584  *
585  * A cancel request always overrides any grant that may currently
586  * be in progress.
587  * The calling procedure must check whether the file can be closed.
588  */
589 __be32
590 nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock)
591 {
592         struct nlm_block        *block;
593         int status = 0;
594
595         dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
596                                 file->f_file->f_path.dentry->d_inode->i_sb->s_id,
597                                 file->f_file->f_path.dentry->d_inode->i_ino,
598                                 lock->fl.fl_pid,
599                                 (long long)lock->fl.fl_start,
600                                 (long long)lock->fl.fl_end);
601
602         if (locks_in_grace())
603                 return nlm_lck_denied_grace_period;
604
605         mutex_lock(&file->f_mutex);
606         block = nlmsvc_lookup_block(file, lock);
607         mutex_unlock(&file->f_mutex);
608         if (block != NULL) {
609                 vfs_cancel_lock(block->b_file->f_file,
610                                 &block->b_call->a_args.lock.fl);
611                 status = nlmsvc_unlink_block(block);
612                 nlmsvc_release_block(block);
613         }
614         return status ? nlm_lck_denied : nlm_granted;
615 }
616
617 /*
618  * This is a callback from the filesystem for VFS file lock requests.
619  * It will be used if fl_grant is defined and the filesystem can not
620  * respond to the request immediately.
621  * For GETLK request it will copy the reply to the nlm_block.
622  * For SETLK or SETLKW request it will get the local posix lock.
623  * In all cases it will move the block to the head of nlm_blocked q where
624  * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
625  * deferred rpc for GETLK and SETLK.
626  */
627 static void
628 nlmsvc_update_deferred_block(struct nlm_block *block, struct file_lock *conf,
629                              int result)
630 {
631         block->b_flags |= B_GOT_CALLBACK;
632         if (result == 0)
633                 block->b_granted = 1;
634         else
635                 block->b_flags |= B_TIMED_OUT;
636         if (conf) {
637                 if (block->b_fl)
638                         __locks_copy_lock(block->b_fl, conf);
639         }
640 }
641
642 static int nlmsvc_grant_deferred(struct file_lock *fl, struct file_lock *conf,
643                                         int result)
644 {
645         struct nlm_block *block;
646         int rc = -ENOENT;
647
648         lock_kernel();
649         list_for_each_entry(block, &nlm_blocked, b_list) {
650                 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
651                         dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
652                                                         block, block->b_flags);
653                         if (block->b_flags & B_QUEUED) {
654                                 if (block->b_flags & B_TIMED_OUT) {
655                                         rc = -ENOLCK;
656                                         break;
657                                 }
658                                 nlmsvc_update_deferred_block(block, conf, result);
659                         } else if (result == 0)
660                                 block->b_granted = 1;
661
662                         nlmsvc_insert_block(block, 0);
663                         svc_wake_up(block->b_daemon);
664                         rc = 0;
665                         break;
666                 }
667         }
668         unlock_kernel();
669         if (rc == -ENOENT)
670                 printk(KERN_WARNING "lockd: grant for unknown block\n");
671         return rc;
672 }
673
674 /*
675  * Unblock a blocked lock request. This is a callback invoked from the
676  * VFS layer when a lock on which we blocked is removed.
677  *
678  * This function doesn't grant the blocked lock instantly, but rather moves
679  * the block to the head of nlm_blocked where it can be picked up by lockd.
680  */
681 static void
682 nlmsvc_notify_blocked(struct file_lock *fl)
683 {
684         struct nlm_block        *block;
685
686         dprintk("lockd: VFS unblock notification for block %p\n", fl);
687         list_for_each_entry(block, &nlm_blocked, b_list) {
688                 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
689                         nlmsvc_insert_block(block, 0);
690                         svc_wake_up(block->b_daemon);
691                         return;
692                 }
693         }
694
695         printk(KERN_WARNING "lockd: notification for unknown block!\n");
696 }
697
698 static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
699 {
700         return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
701 }
702
703 struct lock_manager_operations nlmsvc_lock_operations = {
704         .fl_compare_owner = nlmsvc_same_owner,
705         .fl_notify = nlmsvc_notify_blocked,
706         .fl_grant = nlmsvc_grant_deferred,
707 };
708
709 /*
710  * Try to claim a lock that was previously blocked.
711  *
712  * Note that we use both the RPC_GRANTED_MSG call _and_ an async
713  * RPC thread when notifying the client. This seems like overkill...
714  * Here's why:
715  *  -   we don't want to use a synchronous RPC thread, otherwise
716  *      we might find ourselves hanging on a dead portmapper.
717  *  -   Some lockd implementations (e.g. HP) don't react to
718  *      RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
719  */
720 static void
721 nlmsvc_grant_blocked(struct nlm_block *block)
722 {
723         struct nlm_file         *file = block->b_file;
724         struct nlm_lock         *lock = &block->b_call->a_args.lock;
725         int                     error;
726
727         dprintk("lockd: grant blocked lock %p\n", block);
728
729         kref_get(&block->b_count);
730
731         /* Unlink block request from list */
732         nlmsvc_unlink_block(block);
733
734         /* If b_granted is true this means we've been here before.
735          * Just retry the grant callback, possibly refreshing the RPC
736          * binding */
737         if (block->b_granted) {
738                 nlm_rebind_host(block->b_host);
739                 goto callback;
740         }
741
742         /* Try the lock operation again */
743         lock->fl.fl_flags |= FL_SLEEP;
744         error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
745         lock->fl.fl_flags &= ~FL_SLEEP;
746
747         switch (error) {
748         case 0:
749                 break;
750         case FILE_LOCK_DEFERRED:
751                 dprintk("lockd: lock still blocked error %d\n", error);
752                 nlmsvc_insert_block(block, NLM_NEVER);
753                 nlmsvc_release_block(block);
754                 return;
755         default:
756                 printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
757                                 -error, __func__);
758                 nlmsvc_insert_block(block, 10 * HZ);
759                 nlmsvc_release_block(block);
760                 return;
761         }
762
763 callback:
764         /* Lock was granted by VFS. */
765         dprintk("lockd: GRANTing blocked lock.\n");
766         block->b_granted = 1;
767
768         /* keep block on the list, but don't reattempt until the RPC
769          * completes or the submission fails
770          */
771         nlmsvc_insert_block(block, NLM_NEVER);
772
773         /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked
774          * will queue up a new one if this one times out
775          */
776         error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
777                                 &nlmsvc_grant_ops);
778
779         /* RPC submission failed, wait a bit and retry */
780         if (error < 0)
781                 nlmsvc_insert_block(block, 10 * HZ);
782 }
783
784 /*
785  * This is the callback from the RPC layer when the NLM_GRANTED_MSG
786  * RPC call has succeeded or timed out.
787  * Like all RPC callbacks, it is invoked by the rpciod process, so it
788  * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
789  * chain once more in order to have it removed by lockd itself (which can
790  * then sleep on the file semaphore without disrupting e.g. the nfs client).
791  */
792 static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
793 {
794         struct nlm_rqst         *call = data;
795         struct nlm_block        *block = call->a_block;
796         unsigned long           timeout;
797
798         dprintk("lockd: GRANT_MSG RPC callback\n");
799
800         lock_kernel();
801         /* if the block is not on a list at this point then it has
802          * been invalidated. Don't try to requeue it.
803          *
804          * FIXME: it's possible that the block is removed from the list
805          * after this check but before the nlmsvc_insert_block. In that
806          * case it will be added back. Perhaps we need better locking
807          * for nlm_blocked?
808          */
809         if (list_empty(&block->b_list))
810                 goto out;
811
812         /* Technically, we should down the file semaphore here. Since we
813          * move the block towards the head of the queue only, no harm
814          * can be done, though. */
815         if (task->tk_status < 0) {
816                 /* RPC error: Re-insert for retransmission */
817                 timeout = 10 * HZ;
818         } else {
819                 /* Call was successful, now wait for client callback */
820                 timeout = 60 * HZ;
821         }
822         nlmsvc_insert_block(block, timeout);
823         svc_wake_up(block->b_daemon);
824 out:
825         unlock_kernel();
826 }
827
828 static void nlmsvc_grant_release(void *data)
829 {
830         struct nlm_rqst         *call = data;
831
832         lock_kernel();
833         nlmsvc_release_block(call->a_block);
834         unlock_kernel();
835 }
836
837 static const struct rpc_call_ops nlmsvc_grant_ops = {
838         .rpc_call_done = nlmsvc_grant_callback,
839         .rpc_release = nlmsvc_grant_release,
840 };
841
842 /*
843  * We received a GRANT_RES callback. Try to find the corresponding
844  * block.
845  */
846 void
847 nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status)
848 {
849         struct nlm_block        *block;
850
851         dprintk("grant_reply: looking for cookie %x, s=%d \n",
852                 *(unsigned int *)(cookie->data), status);
853         if (!(block = nlmsvc_find_block(cookie)))
854                 return;
855
856         if (block) {
857                 if (status == nlm_lck_denied_grace_period) {
858                         /* Try again in a couple of seconds */
859                         nlmsvc_insert_block(block, 10 * HZ);
860                 } else {
861                         /* Lock is now held by client, or has been rejected.
862                          * In both cases, the block should be removed. */
863                         nlmsvc_unlink_block(block);
864                 }
865         }
866         nlmsvc_release_block(block);
867 }
868
869 /* Helper function to handle retry of a deferred block.
870  * If it is a blocking lock, call grant_blocked.
871  * For a non-blocking lock or test lock, revisit the request.
872  */
873 static void
874 retry_deferred_block(struct nlm_block *block)
875 {
876         if (!(block->b_flags & B_GOT_CALLBACK))
877                 block->b_flags |= B_TIMED_OUT;
878         nlmsvc_insert_block(block, NLM_TIMEOUT);
879         dprintk("revisit block %p flags %d\n",  block, block->b_flags);
880         if (block->b_deferred_req) {
881                 block->b_deferred_req->revisit(block->b_deferred_req, 0);
882                 block->b_deferred_req = NULL;
883         }
884 }
885
886 /*
887  * Retry all blocked locks that have been notified. This is where lockd
888  * picks up locks that can be granted, or grant notifications that must
889  * be retransmitted.
890  */
891 unsigned long
892 nlmsvc_retry_blocked(void)
893 {
894         unsigned long   timeout = MAX_SCHEDULE_TIMEOUT;
895         struct nlm_block *block;
896
897         while (!list_empty(&nlm_blocked) && !kthread_should_stop()) {
898                 block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
899
900                 if (block->b_when == NLM_NEVER)
901                         break;
902                 if (time_after(block->b_when, jiffies)) {
903                         timeout = block->b_when - jiffies;
904                         break;
905                 }
906
907                 dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
908                         block, block->b_when);
909                 if (block->b_flags & B_QUEUED) {
910                         dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
911                                 block, block->b_granted, block->b_flags);
912                         retry_deferred_block(block);
913                 } else
914                         nlmsvc_grant_blocked(block);
915         }
916
917         return timeout;
918 }