1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/lockd/svclock.c
4 *
5 * Handling of server-side locks, mostly of the blocked variety.
6 * This is the ugliest part of lockd because we tread on very thin ice.
7 * GRANT and CANCEL calls may get stuck, meet in mid-flight, etc.
8 * IMNSHO introducing the grant callback into the NLM protocol was one
9 * of the worst ideas Sun ever had. Except maybe for the idea of doing
10 * NFS file locking at all.
11 *
12 * I'm trying hard to avoid race conditions by protecting most accesses
13 * to a file's list of blocked locks through a semaphore. The global
14 * list of blocked locks is not protected in this fashion however.
15 * Therefore, some functions (such as the RPC callback for the async grant
16 * call) move blocked locks towards the head of the list *while some other
17 * process might be traversing it*. This should not be a problem in
18 * practice, because this will only cause functions traversing the list
19 * to visit some blocks twice.
20 *
21 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
22 */
23
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/sunrpc/clnt.h>
30 #include <linux/sunrpc/svc_xprt.h>
31
32 #include "lockd.h"
33
34 #define NLMDBG_FACILITY NLMDBG_SVCLOCK
35
36 static void nlmsvc_release_block(struct nlm_block *block);
37 static void nlmsvc_insert_block(struct nlm_block *block, unsigned long);
38 static void nlmsvc_remove_block(struct nlm_block *block);
39
40 static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock);
41 static void nlmsvc_freegrantargs(struct nlm_rqst *call);
42 static const struct rpc_call_ops nlmsvc_grant_ops;
43
44 /*
45 * The list of blocked locks to retry
46 */
47 static LIST_HEAD(nlm_blocked);
48 static DEFINE_SPINLOCK(nlm_blocked_lock);
49
50 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
nlmdbg_cookie2a(const struct nlm_cookie * cookie)51 static const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
52 {
53 /*
54 * We can get away with a static buffer because this is only called
55 * from lockd, which is single-threaded.
56 */
57 static char buf[2*NLM_MAXCOOKIELEN+1];
58 unsigned int i, len = sizeof(buf);
59 char *p = buf;
60
61 len--; /* allow for trailing \0 */
62 if (len < 3)
63 return "???";
64 for (i = 0 ; i < cookie->len ; i++) {
65 if (len < 2) {
66 strcpy(p-3, "...");
67 break;
68 }
69 sprintf(p, "%02x", cookie->data[i]);
70 p += 2;
71 len -= 2;
72 }
73 *p = '\0';
74
75 return buf;
76 }
77 #else
nlmdbg_cookie2a(const struct nlm_cookie * cookie)78 static inline const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
79 {
80 return "???";
81 }
82 #endif
83
84 /*
85 * Insert a blocked lock into the global list
86 */
87 static void
nlmsvc_insert_block_locked(struct nlm_block * block,unsigned long when)88 nlmsvc_insert_block_locked(struct nlm_block *block, unsigned long when)
89 {
90 struct nlm_block *b;
91 struct list_head *pos;
92
93 dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
94 if (list_empty(&block->b_list)) {
95 kref_get(&block->b_count);
96 } else {
97 list_del_init(&block->b_list);
98 }
99
100 pos = &nlm_blocked;
101 if (when != NLM_NEVER) {
102 if ((when += jiffies) == NLM_NEVER)
103 when ++;
104 list_for_each(pos, &nlm_blocked) {
105 b = list_entry(pos, struct nlm_block, b_list);
106 if (time_after(b->b_when,when) || b->b_when == NLM_NEVER)
107 break;
108 }
109 /* On normal exit from the loop, pos == &nlm_blocked,
110 * so we will be adding to the end of the list - good
111 */
112 }
113
114 list_add_tail(&block->b_list, pos);
115 block->b_when = when;
116 }
117
nlmsvc_insert_block(struct nlm_block * block,unsigned long when)118 static void nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
119 {
120 spin_lock(&nlm_blocked_lock);
121 nlmsvc_insert_block_locked(block, when);
122 spin_unlock(&nlm_blocked_lock);
123 }
124
125 /*
126 * Remove a block from the global list
127 */
128 static inline void
nlmsvc_remove_block(struct nlm_block * block)129 nlmsvc_remove_block(struct nlm_block *block)
130 {
131 spin_lock(&nlm_blocked_lock);
132 if (!list_empty(&block->b_list)) {
133 list_del_init(&block->b_list);
134 spin_unlock(&nlm_blocked_lock);
135 nlmsvc_release_block(block);
136 return;
137 }
138 spin_unlock(&nlm_blocked_lock);
139 }
140
141 /*
142 * Find a block for a given lock
143 */
144 static struct nlm_block *
nlmsvc_lookup_block(struct nlm_file * file,struct nlm_lock * lock)145 nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock)
146 {
147 struct nlm_block *block;
148 struct file_lock *fl;
149
150 dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
151 file, lock->fl.c.flc_pid,
152 (long long)lock->fl.fl_start,
153 (long long)lock->fl.fl_end,
154 lock->fl.c.flc_type);
155 spin_lock(&nlm_blocked_lock);
156 list_for_each_entry(block, &nlm_blocked, b_list) {
157 fl = &block->b_call->a_args.lock.fl;
158 dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n",
159 block->b_file, fl->c.flc_pid,
160 (long long)fl->fl_start,
161 (long long)fl->fl_end, fl->c.flc_type,
162 nlmdbg_cookie2a(&block->b_call->a_args.cookie));
163 if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
164 kref_get(&block->b_count);
165 spin_unlock(&nlm_blocked_lock);
166 return block;
167 }
168 }
169 spin_unlock(&nlm_blocked_lock);
170
171 return NULL;
172 }
173
nlm_cookie_match(struct nlm_cookie * a,struct nlm_cookie * b)174 static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b)
175 {
176 if (a->len != b->len)
177 return 0;
178 if (memcmp(a->data, b->data, a->len))
179 return 0;
180 return 1;
181 }
182
183 /*
184 * Find a block with a given NLM cookie.
185 */
186 static inline struct nlm_block *
nlmsvc_find_block(struct nlm_cookie * cookie)187 nlmsvc_find_block(struct nlm_cookie *cookie)
188 {
189 struct nlm_block *block;
190
191 spin_lock(&nlm_blocked_lock);
192 list_for_each_entry(block, &nlm_blocked, b_list) {
193 if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie))
194 goto found;
195 }
196 spin_unlock(&nlm_blocked_lock);
197
198 return NULL;
199
200 found:
201 dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block);
202 kref_get(&block->b_count);
203 spin_unlock(&nlm_blocked_lock);
204 return block;
205 }
206
207 /*
208 * Create a block and initialize it.
209 *
210 * Note: we explicitly set the cookie of the grant reply to that of
211 * the blocked lock request. The spec explicitly mentions that the client
212 * should _not_ rely on the callback containing the same cookie as the
213 * request, but (as I found out later) that's because some implementations
214 * do just this. Never mind the standards comittees, they support our
215 * logging industries.
216 *
217 * 10 years later: I hope we can safely ignore these old and broken
218 * clients by now. Let's fix this so we can uniquely identify an incoming
219 * GRANTED_RES message by cookie, without having to rely on the client's IP
220 * address. --okir
221 */
222 static struct nlm_block *
nlmsvc_create_block(struct svc_rqst * rqstp,struct nlm_host * host,struct nlm_file * file,struct nlm_lock * lock,struct nlm_cookie * cookie)223 nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
224 struct nlm_file *file, struct nlm_lock *lock,
225 struct nlm_cookie *cookie)
226 {
227 struct nlm_block *block;
228 struct nlm_rqst *call = NULL;
229
230 call = nlm_alloc_call(host);
231 if (call == NULL)
232 return NULL;
233
234 /* Allocate memory for block, and initialize arguments */
235 block = kzalloc_obj(*block);
236 if (block == NULL)
237 goto failed;
238 kref_init(&block->b_count);
239 INIT_LIST_HEAD(&block->b_list);
240 INIT_LIST_HEAD(&block->b_flist);
241
242 if (!nlmsvc_setgrantargs(call, lock))
243 goto failed_free;
244
245 /* Set notifier function for VFS, and init args */
246 call->a_args.lock.fl.c.flc_flags |= FL_SLEEP;
247 call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
248 nlmclnt_next_cookie(&call->a_args.cookie);
249
250 dprintk("lockd: created block %p...\n", block);
251
252 /* Create and initialize the block */
253 block->b_daemon = rqstp->rq_server;
254 block->b_host = host;
255 block->b_file = file;
256 file->f_count++;
257
258 /* Add to file's list of blocks */
259 list_add(&block->b_flist, &file->f_blocks);
260
261 /* Set up RPC arguments for callback */
262 block->b_call = call;
263 call->a_flags = RPC_TASK_ASYNC;
264 call->a_block = block;
265
266 return block;
267
268 failed_free:
269 kfree(block);
270 failed:
271 nlmsvc_release_call(call);
272 return NULL;
273 }
274
275 /*
276 * Delete a block.
277 * It is the caller's responsibility to check whether the file
278 * can be closed hereafter.
279 */
nlmsvc_unlink_block(struct nlm_block * block)280 static int nlmsvc_unlink_block(struct nlm_block *block)
281 {
282 int status;
283 dprintk("lockd: unlinking block %p...\n", block);
284
285 /* Remove block from list */
286 status = locks_delete_block(&block->b_call->a_args.lock.fl);
287 nlmsvc_remove_block(block);
288 return status;
289 }
290
nlmsvc_free_block(struct kref * kref)291 static void nlmsvc_free_block(struct kref *kref)
292 {
293 struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
294 struct nlm_file *file = block->b_file;
295
296 dprintk("lockd: freeing block %p...\n", block);
297
298 /* Remove block from file's list of blocks */
299 list_del_init(&block->b_flist);
300 mutex_unlock(&file->f_mutex);
301
302 nlmsvc_freegrantargs(block->b_call);
303 nlmsvc_release_call(block->b_call);
304 nlm_release_file(block->b_file);
305 kfree(block);
306 }
307
nlmsvc_release_block(struct nlm_block * block)308 static void nlmsvc_release_block(struct nlm_block *block)
309 {
310 if (block != NULL)
311 kref_put_mutex(&block->b_count, nlmsvc_free_block, &block->b_file->f_mutex);
312 }
313
314 /*
315 * Loop over all blocks and delete blocks held by
316 * a matching host.
317 */
nlmsvc_traverse_blocks(struct nlm_host * host,struct nlm_file * file,nlm_host_match_fn_t match)318 void nlmsvc_traverse_blocks(struct nlm_host *host,
319 struct nlm_file *file,
320 nlm_host_match_fn_t match)
321 {
322 struct nlm_block *block, *next;
323
324 restart:
325 mutex_lock(&file->f_mutex);
326 spin_lock(&nlm_blocked_lock);
327 list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
328 if (!match(block->b_host, host))
329 continue;
330 /* Do not destroy blocks that are not on
331 * the global retry list - why? */
332 if (list_empty(&block->b_list))
333 continue;
334 kref_get(&block->b_count);
335 spin_unlock(&nlm_blocked_lock);
336 mutex_unlock(&file->f_mutex);
337 nlmsvc_unlink_block(block);
338 nlmsvc_release_block(block);
339 goto restart;
340 }
341 spin_unlock(&nlm_blocked_lock);
342 mutex_unlock(&file->f_mutex);
343 }
344
345 static struct nlm_lockowner *
nlmsvc_get_lockowner(struct nlm_lockowner * lockowner)346 nlmsvc_get_lockowner(struct nlm_lockowner *lockowner)
347 {
348 refcount_inc(&lockowner->count);
349 return lockowner;
350 }
351
nlmsvc_put_lockowner(struct nlm_lockowner * lockowner)352 void nlmsvc_put_lockowner(struct nlm_lockowner *lockowner)
353 {
354 if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
355 return;
356 list_del(&lockowner->list);
357 spin_unlock(&lockowner->host->h_lock);
358 nlmsvc_release_host(lockowner->host);
359 kfree(lockowner);
360 }
361
__nlmsvc_find_lockowner(struct nlm_host * host,pid_t pid)362 static struct nlm_lockowner *__nlmsvc_find_lockowner(struct nlm_host *host, pid_t pid)
363 {
364 struct nlm_lockowner *lockowner;
365 list_for_each_entry(lockowner, &host->h_lockowners, list) {
366 if (lockowner->pid != pid)
367 continue;
368 return nlmsvc_get_lockowner(lockowner);
369 }
370 return NULL;
371 }
372
nlmsvc_find_lockowner(struct nlm_host * host,pid_t pid)373 static struct nlm_lockowner *nlmsvc_find_lockowner(struct nlm_host *host, pid_t pid)
374 {
375 struct nlm_lockowner *res, *new = NULL;
376
377 spin_lock(&host->h_lock);
378 res = __nlmsvc_find_lockowner(host, pid);
379
380 if (res == NULL) {
381 spin_unlock(&host->h_lock);
382 new = kmalloc_obj(*res);
383 spin_lock(&host->h_lock);
384 res = __nlmsvc_find_lockowner(host, pid);
385 if (res == NULL && new != NULL) {
386 res = new;
387 /* fs/locks.c will manage the refcount through lock_ops */
388 refcount_set(&new->count, 1);
389 new->pid = pid;
390 new->host = nlm_get_host(host);
391 list_add(&new->list, &host->h_lockowners);
392 new = NULL;
393 }
394 }
395
396 spin_unlock(&host->h_lock);
397 kfree(new);
398 return res;
399 }
400
401 void
nlmsvc_release_lockowner(struct nlm_lock * lock)402 nlmsvc_release_lockowner(struct nlm_lock *lock)
403 {
404 if (lock->fl.c.flc_owner)
405 nlmsvc_put_lockowner(lock->fl.c.flc_owner);
406 }
407
nlmsvc_locks_init_private(struct file_lock * fl,struct nlm_host * host,pid_t pid)408 void nlmsvc_locks_init_private(struct file_lock *fl, struct nlm_host *host,
409 pid_t pid)
410 {
411 fl->c.flc_owner = nlmsvc_find_lockowner(host, pid);
412 }
413
414 /*
415 * Initialize arguments for GRANTED call. The nlm_rqst structure
416 * has been cleared already.
417 */
nlmsvc_setgrantargs(struct nlm_rqst * call,struct nlm_lock * lock)418 static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
419 {
420 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
421 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
422 call->a_args.lock.caller = utsname()->nodename;
423 call->a_args.lock.oh.len = lock->oh.len;
424
425 /* set default data area */
426 call->a_args.lock.oh.data = call->a_owner;
427 call->a_args.lock.svid = ((struct nlm_lockowner *) lock->fl.c.flc_owner)->pid;
428
429 if (lock->oh.len > NLMCLNT_OHSIZE) {
430 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
431 if (!data)
432 return 0;
433 call->a_args.lock.oh.data = (u8 *) data;
434 }
435
436 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
437 return 1;
438 }
439
nlmsvc_freegrantargs(struct nlm_rqst * call)440 static void nlmsvc_freegrantargs(struct nlm_rqst *call)
441 {
442 if (call->a_args.lock.oh.data != call->a_owner)
443 kfree(call->a_args.lock.oh.data);
444
445 locks_release_private(&call->a_args.lock.fl);
446 }
447
448 /*
449 * Deferred lock request handling for non-blocking lock
450 */
451 static __be32
nlmsvc_defer_lock_rqst(struct svc_rqst * rqstp,struct nlm_block * block)452 nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
453 {
454 __be32 status = nlm_lck_denied_nolocks;
455
456 block->b_flags |= B_QUEUED;
457
458 nlmsvc_insert_block(block, NLM_TIMEOUT);
459
460 block->b_cache_req = &rqstp->rq_chandle;
461 if (rqstp->rq_chandle.defer) {
462 block->b_deferred_req =
463 rqstp->rq_chandle.defer(block->b_cache_req);
464 if (block->b_deferred_req != NULL)
465 status = nlm__int__drop_reply;
466 }
467 dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
468 block, block->b_flags, ntohl(status));
469
470 return status;
471 }
472
473 /*
474 * Attempt to establish a lock, and if it can't be granted, block it
475 * if required.
476 */
477 __be32
nlmsvc_lock(struct svc_rqst * rqstp,struct nlm_file * file,struct nlm_host * host,struct nlm_lock * lock,int wait,struct nlm_cookie * cookie,int reclaim)478 nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
479 struct nlm_host *host, struct nlm_lock *lock, int wait,
480 struct nlm_cookie *cookie, int reclaim)
481 {
482 struct inode *inode __maybe_unused = nlmsvc_file_inode(file);
483 struct nlm_block *block = NULL;
484 int error;
485 int mode;
486 int async_block = 0;
487 __be32 ret;
488
489 dprintk("lockd: nlmsvc_lock(%s/%llu, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
490 inode->i_sb->s_id, inode->i_ino,
491 lock->fl.c.flc_type,
492 lock->fl.c.flc_pid,
493 (long long)lock->fl.fl_start,
494 (long long)lock->fl.fl_end,
495 wait);
496
497 if (nlmsvc_file_cannot_lock(file))
498 return nlm_lck_denied_nolocks;
499
500 if (!locks_can_async_lock(nlmsvc_file_file(file)->f_op)) {
501 async_block = wait;
502 wait = 0;
503 }
504
505 /* Lock file against concurrent access */
506 mutex_lock(&file->f_mutex);
507 /* Get existing block (in case client is busy-waiting)
508 * or create new block
509 */
510 block = nlmsvc_lookup_block(file, lock);
511 if (block == NULL) {
512 block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
513 ret = nlm_lck_denied_nolocks;
514 if (block == NULL)
515 goto out;
516 lock = &block->b_call->a_args.lock;
517 } else
518 lock->fl.c.flc_flags &= ~FL_SLEEP;
519
520 if (block->b_flags & B_QUEUED) {
521 dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
522 block, block->b_flags);
523 if (block->b_granted) {
524 nlmsvc_unlink_block(block);
525 ret = nlm_granted;
526 goto out;
527 }
528 if (block->b_flags & B_TIMED_OUT) {
529 nlmsvc_unlink_block(block);
530 ret = nlm_lck_denied;
531 goto out;
532 }
533 ret = nlm__int__drop_reply;
534 goto out;
535 }
536
537 if (locks_in_grace(SVC_NET(rqstp)) && !reclaim) {
538 ret = nlm_lck_denied_grace_period;
539 goto out;
540 }
541 if (reclaim && !locks_in_grace(SVC_NET(rqstp))) {
542 ret = nlm_lck_denied_grace_period;
543 goto out;
544 }
545
546 spin_lock(&nlm_blocked_lock);
547 /*
548 * If this is a lock request for an already pending
549 * lock request we return nlm_lck_blocked without calling
550 * vfs_lock_file() again. Otherwise we have two pending
551 * requests on the underlaying ->lock() implementation but
552 * only one nlm_block to being granted by lm_grant().
553 */
554 if (locks_can_async_lock(nlmsvc_file_file(file)->f_op) &&
555 !list_empty(&block->b_list)) {
556 spin_unlock(&nlm_blocked_lock);
557 ret = nlm_lck_blocked;
558 goto out;
559 }
560
561 /* Append to list of blocked */
562 nlmsvc_insert_block_locked(block, NLM_NEVER);
563 spin_unlock(&nlm_blocked_lock);
564
565 if (!wait)
566 lock->fl.c.flc_flags &= ~FL_SLEEP;
567 mode = lock_to_openmode(&lock->fl);
568 error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL);
569 lock->fl.c.flc_flags &= ~FL_SLEEP;
570
571 dprintk("lockd: vfs_lock_file returned %d\n", error);
572 switch (error) {
573 case 0:
574 nlmsvc_remove_block(block);
575 ret = nlm_granted;
576 goto out;
577 case -EAGAIN:
578 if (!wait)
579 nlmsvc_remove_block(block);
580 ret = async_block ? nlm_lck_blocked : nlm_lck_denied;
581 goto out;
582 case FILE_LOCK_DEFERRED:
583 if (wait)
584 break;
585 /* Filesystem lock operation is in progress
586 Add it to the queue waiting for callback */
587 ret = nlmsvc_defer_lock_rqst(rqstp, block);
588 goto out;
589 case -EDEADLK:
590 nlmsvc_remove_block(block);
591 ret = nlm__int__deadlock;
592 goto out;
593 default: /* includes ENOLCK */
594 nlmsvc_remove_block(block);
595 ret = nlm_lck_denied_nolocks;
596 goto out;
597 }
598
599 ret = nlm_lck_blocked;
600 out:
601 mutex_unlock(&file->f_mutex);
602 nlmsvc_release_block(block);
603 dprintk("lockd: nlmsvc_lock returned %u\n", ret);
604 return ret;
605 }
606
607 /*
608 * Test for presence of a conflicting lock.
609 */
610 __be32
nlmsvc_testlock(struct svc_rqst * rqstp,struct nlm_file * file,struct nlm_host * host,struct nlm_lock * lock,struct nlm_lock * conflock)611 nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
612 struct nlm_host *host, struct nlm_lock *lock,
613 struct nlm_lock *conflock)
614 {
615 int error;
616 __be32 ret;
617
618 dprintk("lockd: nlmsvc_testlock(%s/%llu, ty=%d, %Ld-%Ld)\n",
619 nlmsvc_file_inode(file)->i_sb->s_id,
620 nlmsvc_file_inode(file)->i_ino,
621 lock->fl.c.flc_type,
622 (long long)lock->fl.fl_start,
623 (long long)lock->fl.fl_end);
624
625 if (nlmsvc_file_cannot_lock(file))
626 return nlm_lck_denied_nolocks;
627
628 if (locks_in_grace(SVC_NET(rqstp))) {
629 ret = nlm_lck_denied_grace_period;
630 goto out;
631 }
632
633 locks_init_lock(&conflock->fl);
634 /* vfs_test_lock only uses start, end, and owner, but tests flc_file */
635 conflock->fl.c.flc_file = lock->fl.c.flc_file;
636 conflock->fl.fl_start = lock->fl.fl_start;
637 conflock->fl.fl_end = lock->fl.fl_end;
638 conflock->fl.c.flc_owner = lock->fl.c.flc_owner;
639 error = vfs_test_lock(lock->fl.c.flc_file, &conflock->fl);
640 if (error) {
641 ret = nlm_lck_denied_nolocks;
642 goto out;
643 }
644
645 if (conflock->fl.c.flc_type == F_UNLCK) {
646 ret = nlm_granted;
647 goto out;
648 }
649
650 dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
651 conflock->fl.c.flc_type, (long long)conflock->fl.fl_start,
652 (long long)conflock->fl.fl_end);
653 conflock->caller = "somehost"; /* FIXME */
654 conflock->len = strlen(conflock->caller);
655 conflock->oh.len = 0; /* don't return OH info */
656 conflock->svid = conflock->fl.c.flc_pid;
657 locks_release_private(&conflock->fl);
658
659 ret = nlm_lck_denied;
660 out:
661 return ret;
662 }
663
664 /*
665 * Remove a lock.
666 * This implies a CANCEL call: We send a GRANT_MSG, the client replies
667 * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
668 * afterwards. In this case the block will still be there, and hence
669 * must be removed.
670 */
671 __be32
nlmsvc_unlock(struct net * net,struct nlm_file * file,struct nlm_lock * lock)672 nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
673 {
674 int error = 0;
675
676 dprintk("lockd: nlmsvc_unlock(%s/%llu, pi=%d, %Ld-%Ld)\n",
677 nlmsvc_file_inode(file)->i_sb->s_id,
678 nlmsvc_file_inode(file)->i_ino,
679 lock->fl.c.flc_pid,
680 (long long)lock->fl.fl_start,
681 (long long)lock->fl.fl_end);
682
683 if (nlmsvc_file_cannot_lock(file))
684 return nlm_lck_denied_nolocks;
685
686 /* First, cancel any lock that might be there */
687 nlmsvc_cancel_blocked(net, file, lock);
688
689 lock->fl.c.flc_type = F_UNLCK;
690 lock->fl.c.flc_file = file->f_file[O_RDONLY];
691 if (lock->fl.c.flc_file)
692 error = vfs_lock_file(lock->fl.c.flc_file, F_SETLK,
693 &lock->fl, NULL);
694 lock->fl.c.flc_file = file->f_file[O_WRONLY];
695 if (lock->fl.c.flc_file)
696 error |= vfs_lock_file(lock->fl.c.flc_file, F_SETLK,
697 &lock->fl, NULL);
698
699 return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
700 }
701
702 /*
703 * Cancel a previously blocked request.
704 *
705 * A cancel request always overrides any grant that may currently
706 * be in progress.
707 * The calling procedure must check whether the file can be closed.
708 */
709 __be32
nlmsvc_cancel_blocked(struct net * net,struct nlm_file * file,struct nlm_lock * lock)710 nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
711 {
712 struct nlm_block *block;
713 int status = 0;
714 int mode;
715
716 dprintk("lockd: nlmsvc_cancel(%s/%llu, pi=%d, %Ld-%Ld)\n",
717 nlmsvc_file_inode(file)->i_sb->s_id,
718 nlmsvc_file_inode(file)->i_ino,
719 lock->fl.c.flc_pid,
720 (long long)lock->fl.fl_start,
721 (long long)lock->fl.fl_end);
722
723 if (nlmsvc_file_cannot_lock(file))
724 return nlm_lck_denied_nolocks;
725
726 if (locks_in_grace(net))
727 return nlm_lck_denied_grace_period;
728
729 mutex_lock(&file->f_mutex);
730 block = nlmsvc_lookup_block(file, lock);
731 mutex_unlock(&file->f_mutex);
732 if (block != NULL) {
733 struct file_lock *fl = &block->b_call->a_args.lock.fl;
734
735 mode = lock_to_openmode(fl);
736 vfs_cancel_lock(block->b_file->f_file[mode], fl);
737 status = nlmsvc_unlink_block(block);
738 nlmsvc_release_block(block);
739 }
740 return status ? nlm_lck_denied : nlm_granted;
741 }
742
743 /*
744 * This is a callback from the filesystem for VFS file lock requests.
745 * It will be used if lm_grant is defined and the filesystem can not
746 * respond to the request immediately.
747 * For SETLK or SETLKW request it will get the local posix lock.
748 * In all cases it will move the block to the head of nlm_blocked q where
749 * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
750 * deferred rpc for GETLK and SETLK.
751 */
752 static void
nlmsvc_update_deferred_block(struct nlm_block * block,int result)753 nlmsvc_update_deferred_block(struct nlm_block *block, int result)
754 {
755 block->b_flags |= B_GOT_CALLBACK;
756 if (result == 0)
757 block->b_granted = 1;
758 else
759 block->b_flags |= B_TIMED_OUT;
760 }
761
nlmsvc_grant_deferred(struct file_lock * fl,int result)762 static int nlmsvc_grant_deferred(struct file_lock *fl, int result)
763 {
764 struct nlm_block *block;
765 int rc = -ENOENT;
766
767 spin_lock(&nlm_blocked_lock);
768 list_for_each_entry(block, &nlm_blocked, b_list) {
769 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
770 dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
771 block, block->b_flags);
772 if (block->b_flags & B_QUEUED) {
773 if (block->b_flags & B_TIMED_OUT) {
774 rc = -ENOLCK;
775 break;
776 }
777 nlmsvc_update_deferred_block(block, result);
778 } else if (result == 0)
779 block->b_granted = 1;
780
781 nlmsvc_insert_block_locked(block, 0);
782 svc_wake_up(block->b_daemon);
783 rc = 0;
784 break;
785 }
786 }
787 spin_unlock(&nlm_blocked_lock);
788 if (rc == -ENOENT)
789 printk(KERN_WARNING "lockd: grant for unknown block\n");
790 return rc;
791 }
792
793 /*
794 * Unblock a blocked lock request. This is a callback invoked from the
795 * VFS layer when a lock on which we blocked is removed.
796 *
797 * This function doesn't grant the blocked lock instantly, but rather moves
798 * the block to the head of nlm_blocked where it can be picked up by lockd.
799 */
800 static void
nlmsvc_notify_blocked(struct file_lock * fl)801 nlmsvc_notify_blocked(struct file_lock *fl)
802 {
803 struct nlm_block *block;
804
805 dprintk("lockd: VFS unblock notification for block %p\n", fl);
806 spin_lock(&nlm_blocked_lock);
807 list_for_each_entry(block, &nlm_blocked, b_list) {
808 if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
809 nlmsvc_insert_block_locked(block, 0);
810 spin_unlock(&nlm_blocked_lock);
811 svc_wake_up(block->b_daemon);
812 return;
813 }
814 }
815 spin_unlock(&nlm_blocked_lock);
816 printk(KERN_WARNING "lockd: notification for unknown block!\n");
817 }
818
nlmsvc_get_owner(fl_owner_t owner)819 static fl_owner_t nlmsvc_get_owner(fl_owner_t owner)
820 {
821 return nlmsvc_get_lockowner(owner);
822 }
823
nlmsvc_put_owner(fl_owner_t owner)824 static void nlmsvc_put_owner(fl_owner_t owner)
825 {
826 nlmsvc_put_lockowner(owner);
827 }
828
829 const struct lock_manager_operations nlmsvc_lock_operations = {
830 .lm_notify = nlmsvc_notify_blocked,
831 .lm_grant = nlmsvc_grant_deferred,
832 .lm_get_owner = nlmsvc_get_owner,
833 .lm_put_owner = nlmsvc_put_owner,
834 };
835
836 /*
837 * Try to claim a lock that was previously blocked.
838 *
839 * Note that we use both the RPC_GRANTED_MSG call _and_ an async
840 * RPC thread when notifying the client. This seems like overkill...
841 * Here's why:
842 * - we don't want to use a synchronous RPC thread, otherwise
843 * we might find ourselves hanging on a dead portmapper.
844 * - Some lockd implementations (e.g. HP) don't react to
845 * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
846 */
847 static void
nlmsvc_grant_blocked(struct nlm_block * block)848 nlmsvc_grant_blocked(struct nlm_block *block)
849 {
850 struct nlm_file *file = block->b_file;
851 struct nlm_lock *lock = &block->b_call->a_args.lock;
852 int mode;
853 int error;
854 loff_t fl_start, fl_end;
855
856 dprintk("lockd: grant blocked lock %p\n", block);
857
858 kref_get(&block->b_count);
859
860 /* Unlink block request from list */
861 nlmsvc_unlink_block(block);
862
863 /* If b_granted is true this means we've been here before.
864 * Just retry the grant callback, possibly refreshing the RPC
865 * binding */
866 if (block->b_granted) {
867 nlm_rebind_host(block->b_host);
868 goto callback;
869 }
870
871 /* Try the lock operation again */
872 /* vfs_lock_file() can mangle fl_start and fl_end, but we need
873 * them unchanged for the GRANT_MSG
874 */
875 lock->fl.c.flc_flags |= FL_SLEEP;
876 fl_start = lock->fl.fl_start;
877 fl_end = lock->fl.fl_end;
878 mode = lock_to_openmode(&lock->fl);
879 error = vfs_lock_file(file->f_file[mode], F_SETLK, &lock->fl, NULL);
880 lock->fl.c.flc_flags &= ~FL_SLEEP;
881 lock->fl.fl_start = fl_start;
882 lock->fl.fl_end = fl_end;
883
884 switch (error) {
885 case 0:
886 break;
887 case FILE_LOCK_DEFERRED:
888 dprintk("lockd: lock still blocked error %d\n", error);
889 nlmsvc_insert_block(block, NLM_NEVER);
890 nlmsvc_release_block(block);
891 return;
892 default:
893 printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
894 -error, __func__);
895 nlmsvc_insert_block(block, 10 * HZ);
896 nlmsvc_release_block(block);
897 return;
898 }
899
900 callback:
901 /* Lock was granted by VFS. */
902 dprintk("lockd: GRANTing blocked lock.\n");
903 block->b_granted = 1;
904
905 /* keep block on the list, but don't reattempt until the RPC
906 * completes or the submission fails
907 */
908 nlmsvc_insert_block(block, NLM_NEVER);
909
910 /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked
911 * will queue up a new one if this one times out
912 */
913 error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
914 &nlmsvc_grant_ops);
915
916 /* RPC submission failed, wait a bit and retry */
917 if (error < 0)
918 nlmsvc_insert_block(block, 10 * HZ);
919 }
920
921 /*
922 * This is the callback from the RPC layer when the NLM_GRANTED_MSG
923 * RPC call has succeeded or timed out.
924 * Like all RPC callbacks, it is invoked by the rpciod process, so it
925 * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
926 * chain once more in order to have it removed by lockd itself (which can
927 * then sleep on the file semaphore without disrupting e.g. the nfs client).
928 */
nlmsvc_grant_callback(struct rpc_task * task,void * data)929 static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
930 {
931 struct nlm_rqst *call = data;
932 struct nlm_block *block = call->a_block;
933 unsigned long timeout;
934
935 dprintk("lockd: GRANT_MSG RPC callback\n");
936
937 spin_lock(&nlm_blocked_lock);
938 /* if the block is not on a list at this point then it has
939 * been invalidated. Don't try to requeue it.
940 *
941 * FIXME: it's possible that the block is removed from the list
942 * after this check but before the nlmsvc_insert_block. In that
943 * case it will be added back. Perhaps we need better locking
944 * for nlm_blocked?
945 */
946 if (list_empty(&block->b_list))
947 goto out;
948
949 /* Technically, we should down the file semaphore here. Since we
950 * move the block towards the head of the queue only, no harm
951 * can be done, though. */
952 if (task->tk_status < 0) {
953 /* RPC error: Re-insert for retransmission */
954 timeout = 10 * HZ;
955 } else {
956 /* Call was successful, now wait for client callback */
957 timeout = 60 * HZ;
958 }
959 nlmsvc_insert_block_locked(block, timeout);
960 svc_wake_up(block->b_daemon);
961 out:
962 spin_unlock(&nlm_blocked_lock);
963 }
964
965 /*
966 * FIXME: nlmsvc_release_block() grabs a mutex. This is not allowed for an
967 * .rpc_release rpc_call_op
968 */
nlmsvc_grant_release(void * data)969 static void nlmsvc_grant_release(void *data)
970 {
971 struct nlm_rqst *call = data;
972 nlmsvc_release_block(call->a_block);
973 }
974
975 static const struct rpc_call_ops nlmsvc_grant_ops = {
976 .rpc_call_done = nlmsvc_grant_callback,
977 .rpc_release = nlmsvc_grant_release,
978 };
979
980 /*
981 * We received a GRANT_RES callback. Try to find the corresponding
982 * block.
983 */
984 void
nlmsvc_grant_reply(struct nlm_cookie * cookie,__be32 status)985 nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status)
986 {
987 struct nlm_block *block;
988 struct file_lock *fl;
989 int error;
990
991 dprintk("grant_reply: looking for cookie %x, s=%d\n",
992 *(unsigned int *)(cookie->data), status);
993 if (!(block = nlmsvc_find_block(cookie)))
994 return;
995
996 switch (status) {
997 case nlm_lck_denied_grace_period:
998 /* Try again in a couple of seconds */
999 nlmsvc_insert_block(block, 10 * HZ);
1000 break;
1001 case nlm_lck_denied:
1002 /* Client doesn't want it, just unlock it */
1003 nlmsvc_unlink_block(block);
1004 fl = &block->b_call->a_args.lock.fl;
1005 fl->c.flc_type = F_UNLCK;
1006 error = vfs_lock_file(fl->c.flc_file, F_SETLK, fl, NULL);
1007 if (error)
1008 pr_warn("lockd: unable to unlock lock rejected by client!\n");
1009 break;
1010 default:
1011 /*
1012 * Either it was accepted or the status makes no sense
1013 * just unlink it either way.
1014 */
1015 nlmsvc_unlink_block(block);
1016 }
1017 nlmsvc_release_block(block);
1018 }
1019
1020 /* Helper function to handle retry of a deferred block.
1021 * If it is a blocking lock, call grant_blocked.
1022 * For a non-blocking lock or test lock, revisit the request.
1023 */
1024 static void
retry_deferred_block(struct nlm_block * block)1025 retry_deferred_block(struct nlm_block *block)
1026 {
1027 if (!(block->b_flags & B_GOT_CALLBACK))
1028 block->b_flags |= B_TIMED_OUT;
1029 nlmsvc_insert_block(block, NLM_TIMEOUT);
1030 dprintk("revisit block %p flags %d\n", block, block->b_flags);
1031 if (block->b_deferred_req) {
1032 block->b_deferred_req->revisit(block->b_deferred_req, 0);
1033 block->b_deferred_req = NULL;
1034 }
1035 }
1036
1037 /*
1038 * Retry all blocked locks that have been notified. This is where lockd
1039 * picks up locks that can be granted, or grant notifications that must
1040 * be retransmitted.
1041 */
1042 void
nlmsvc_retry_blocked(struct svc_rqst * rqstp)1043 nlmsvc_retry_blocked(struct svc_rqst *rqstp)
1044 {
1045 unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
1046 struct nlm_block *block;
1047
1048 spin_lock(&nlm_blocked_lock);
1049 while (!list_empty(&nlm_blocked) && !svc_thread_should_stop(rqstp)) {
1050 block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
1051
1052 if (block->b_when == NLM_NEVER)
1053 break;
1054 if (time_after(block->b_when, jiffies)) {
1055 timeout = block->b_when - jiffies;
1056 break;
1057 }
1058 spin_unlock(&nlm_blocked_lock);
1059
1060 dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
1061 block, block->b_when);
1062 if (block->b_flags & B_QUEUED) {
1063 dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
1064 block, block->b_granted, block->b_flags);
1065 retry_deferred_block(block);
1066 } else
1067 nlmsvc_grant_blocked(block);
1068 spin_lock(&nlm_blocked_lock);
1069 }
1070 spin_unlock(&nlm_blocked_lock);
1071
1072 if (timeout < MAX_SCHEDULE_TIMEOUT)
1073 mod_timer(&nlmsvc_retry, jiffies + timeout);
1074 }
1075