1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/lockd/clntproc.c
4 *
5 * RPC procedures for the client side NLM implementation
6 *
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8 */
9
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/filelock.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/utsname.h>
18 #include <linux/freezer.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/sunrpc/svc.h>
21
22 #include "lockd.h"
23 #include "trace.h"
24
25 #define NLMDBG_FACILITY NLMDBG_CLIENT
26 #define NLMCLNT_GRACE_WAIT (5*HZ)
27 #define NLMCLNT_POLL_TIMEOUT (30*HZ)
28 #define NLMCLNT_MAX_RETRIES 3
29
30 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
31 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
32 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
33 static int nlm_stat_to_errno(__be32 stat);
34 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
35 static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
36
37 static const struct rpc_call_ops nlmclnt_unlock_ops;
38 static const struct rpc_call_ops nlmclnt_cancel_ops;
39
40 /*
41 * Cookie counter for NLM requests
42 */
43 static atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
44
nlmclnt_next_cookie(struct lockd_cookie * c)45 void nlmclnt_next_cookie(struct lockd_cookie *c)
46 {
47 u32 cookie = atomic_inc_return(&nlm_cookie);
48
49 memcpy(c->data, &cookie, 4);
50 c->len=4;
51 }
52
53 static struct nlm_lockowner *
nlmclnt_get_lockowner(struct nlm_lockowner * lockowner)54 nlmclnt_get_lockowner(struct nlm_lockowner *lockowner)
55 {
56 refcount_inc(&lockowner->count);
57 return lockowner;
58 }
59
nlmclnt_put_lockowner(struct nlm_lockowner * lockowner)60 static void nlmclnt_put_lockowner(struct nlm_lockowner *lockowner)
61 {
62 if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
63 return;
64 list_del(&lockowner->list);
65 spin_unlock(&lockowner->host->h_lock);
66 nlmclnt_release_host(lockowner->host);
67 kfree(lockowner);
68 }
69
nlm_pidbusy(struct nlm_host * host,uint32_t pid)70 static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
71 {
72 struct nlm_lockowner *lockowner;
73 list_for_each_entry(lockowner, &host->h_lockowners, list) {
74 if (lockowner->pid == pid)
75 return -EBUSY;
76 }
77 return 0;
78 }
79
__nlm_alloc_pid(struct nlm_host * host)80 static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
81 {
82 uint32_t res;
83 do {
84 res = host->h_pidcount++;
85 } while (nlm_pidbusy(host, res) < 0);
86 return res;
87 }
88
__nlmclnt_find_lockowner(struct nlm_host * host,fl_owner_t owner)89 static struct nlm_lockowner *__nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
90 {
91 struct nlm_lockowner *lockowner;
92 list_for_each_entry(lockowner, &host->h_lockowners, list) {
93 if (lockowner->owner != owner)
94 continue;
95 return nlmclnt_get_lockowner(lockowner);
96 }
97 return NULL;
98 }
99
nlmclnt_find_lockowner(struct nlm_host * host,fl_owner_t owner)100 static struct nlm_lockowner *nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
101 {
102 struct nlm_lockowner *res, *new = NULL;
103
104 spin_lock(&host->h_lock);
105 res = __nlmclnt_find_lockowner(host, owner);
106 if (res == NULL) {
107 spin_unlock(&host->h_lock);
108 new = kmalloc_obj(*new);
109 spin_lock(&host->h_lock);
110 res = __nlmclnt_find_lockowner(host, owner);
111 if (res == NULL && new != NULL) {
112 res = new;
113 refcount_set(&new->count, 1);
114 new->owner = owner;
115 new->pid = __nlm_alloc_pid(host);
116 new->host = nlm_get_host(host);
117 list_add(&new->list, &host->h_lockowners);
118 new = NULL;
119 }
120 }
121 spin_unlock(&host->h_lock);
122 kfree(new);
123 return res;
124 }
125
126 /*
127 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
128 */
nlmclnt_setlockargs(struct nlm_rqst * req,struct file_lock * fl)129 static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
130 {
131 struct lockd_args *argp = &req->a_args;
132 struct lockd_lock *lock = &argp->lock;
133 char *nodename = req->a_host->h_rpcclnt->cl_nodename;
134
135 nlmclnt_next_cookie(&argp->cookie);
136 memcpy(&lock->fh, NFS_FH(file_inode(fl->c.flc_file)),
137 sizeof(struct nfs_fh));
138 lock->caller = nodename;
139 lock->oh.data = req->a_owner;
140 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
141 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
142 nodename);
143 lock->svid = fl->fl_u.nfs_fl.owner->pid;
144 lock->fl.fl_start = fl->fl_start;
145 lock->fl.fl_end = fl->fl_end;
146 lock->fl.c.flc_type = fl->c.flc_type;
147 }
148
nlmclnt_release_lockargs(struct nlm_rqst * req)149 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
150 {
151 WARN_ON_ONCE(req->a_args.lock.fl.fl_ops != NULL);
152 }
153
154 /**
155 * nlmclnt_proc - Perform a single client-side lock request
156 * @host: address of a valid nlm_host context representing the NLM server
157 * @cmd: fcntl-style file lock operation to perform
158 * @fl: address of arguments for the lock operation
159 * @data: address of data to be sent to callback operations
160 *
161 */
nlmclnt_proc(struct nlm_host * host,int cmd,struct file_lock * fl,void * data)162 int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl, void *data)
163 {
164 struct nlm_rqst *call;
165 int status;
166 const struct nlmclnt_operations *nlmclnt_ops = host->h_nlmclnt_ops;
167
168 call = nlm_alloc_call(host);
169 if (call == NULL)
170 return -ENOMEM;
171
172 if (nlmclnt_ops && nlmclnt_ops->nlmclnt_alloc_call)
173 nlmclnt_ops->nlmclnt_alloc_call(data);
174
175 nlmclnt_locks_init_private(fl, host);
176 if (!fl->fl_u.nfs_fl.owner) {
177 /* lockowner allocation has failed */
178 nlmclnt_release_call(call);
179 return -ENOMEM;
180 }
181 /* Set up the argument struct */
182 nlmclnt_setlockargs(call, fl);
183 call->a_callback_data = data;
184
185 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
186 if (fl->c.flc_type != F_UNLCK) {
187 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
188 status = nlmclnt_lock(call, fl);
189 } else
190 status = nlmclnt_unlock(call, fl);
191 } else if (IS_GETLK(cmd))
192 status = nlmclnt_test(call, fl);
193 else
194 status = -EINVAL;
195 fl->fl_ops->fl_release_private(fl);
196 fl->fl_ops = NULL;
197
198 dprintk("lockd: clnt proc returns %d\n", status);
199 return status;
200 }
201 EXPORT_SYMBOL_GPL(nlmclnt_proc);
202
203 /*
204 * Allocate an NLM RPC call struct
205 */
nlm_alloc_call(struct nlm_host * host)206 struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
207 {
208 struct nlm_rqst *call;
209
210 for(;;) {
211 call = kzalloc_obj(*call);
212 if (call != NULL) {
213 refcount_set(&call->a_count, 1);
214 locks_init_lock(&call->a_args.lock.fl);
215 locks_init_lock(&call->a_res.lock.fl);
216 call->a_host = nlm_get_host(host);
217 return call;
218 }
219 if (signalled())
220 break;
221 printk("nlm_alloc_call: failed, waiting for memory\n");
222 schedule_timeout_interruptible(5*HZ);
223 }
224 return NULL;
225 }
226
nlmclnt_release_call(struct nlm_rqst * call)227 void nlmclnt_release_call(struct nlm_rqst *call)
228 {
229 const struct nlmclnt_operations *nlmclnt_ops = call->a_host->h_nlmclnt_ops;
230
231 if (!refcount_dec_and_test(&call->a_count))
232 return;
233 if (nlmclnt_ops && nlmclnt_ops->nlmclnt_release_call)
234 nlmclnt_ops->nlmclnt_release_call(call->a_callback_data);
235 nlmclnt_release_host(call->a_host);
236 nlmclnt_release_lockargs(call);
237 kfree(call);
238 }
239
nlmclnt_rpc_release(void * data)240 static void nlmclnt_rpc_release(void *data)
241 {
242 nlmclnt_release_call(data);
243 }
244
nlm_wait_on_grace(wait_queue_head_t * queue)245 static int nlm_wait_on_grace(wait_queue_head_t *queue)
246 {
247 DEFINE_WAIT(wait);
248 int status = -EINTR;
249
250 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
251 if (!signalled ()) {
252 schedule_timeout(NLMCLNT_GRACE_WAIT);
253 try_to_freeze();
254 if (!signalled ())
255 status = 0;
256 }
257 finish_wait(queue, &wait);
258 return status;
259 }
260
261 /*
262 * Generic NLM call
263 */
264 static int
nlmclnt_call(const struct cred * cred,struct nlm_rqst * req,u32 proc)265 nlmclnt_call(const struct cred *cred, struct nlm_rqst *req, u32 proc)
266 {
267 struct nlm_host *host = req->a_host;
268 struct rpc_clnt *clnt;
269 struct lockd_args *argp = &req->a_args;
270 struct lockd_res *resp = &req->a_res;
271 struct rpc_message msg = {
272 .rpc_argp = argp,
273 .rpc_resp = resp,
274 .rpc_cred = cred,
275 };
276 int status;
277
278 dprintk("lockd: call procedure %d on %s\n",
279 (int)proc, host->h_name);
280
281 do {
282 if (host->h_reclaiming && !argp->reclaim)
283 goto in_grace_period;
284
285 /* If we have no RPC client yet, create one. */
286 if ((clnt = nlm_bind_host(host)) == NULL)
287 return -ENOLCK;
288 msg.rpc_proc = &clnt->cl_procinfo[proc];
289
290 /* Perform the RPC call. If an error occurs, try again */
291 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
292 dprintk("lockd: rpc_call returned error %d\n", -status);
293 switch (status) {
294 case -EPROTONOSUPPORT:
295 status = -EINVAL;
296 break;
297 case -ECONNREFUSED:
298 case -ETIMEDOUT:
299 case -ENOTCONN:
300 nlm_rebind_host(host);
301 status = -EAGAIN;
302 break;
303 case -ERESTARTSYS:
304 return signalled () ? -EINTR : status;
305 default:
306 break;
307 }
308 break;
309 } else
310 if (resp->status == nlm_lck_denied_grace_period) {
311 dprintk("lockd: server in grace period\n");
312 if (argp->reclaim) {
313 printk(KERN_WARNING
314 "lockd: spurious grace period reject?!\n");
315 return -ENOLCK;
316 }
317 } else {
318 if (!argp->reclaim) {
319 /* We appear to be out of the grace period */
320 wake_up_all(&host->h_gracewait);
321 }
322 dprintk("lockd: server returns status %d\n",
323 ntohl(resp->status));
324 return 0; /* Okay, call complete */
325 }
326
327 in_grace_period:
328 /*
329 * The server has rebooted and appears to be in the grace
330 * period during which locks are only allowed to be
331 * reclaimed.
332 * We can only back off and try again later.
333 */
334 status = nlm_wait_on_grace(&host->h_gracewait);
335 } while (status == 0);
336
337 return status;
338 }
339
340 /*
341 * Generic NLM call, async version.
342 */
__nlm_async_call(struct nlm_rqst * req,u32 proc,struct rpc_message * msg,const struct rpc_call_ops * tk_ops)343 static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
344 {
345 struct nlm_host *host = req->a_host;
346 struct rpc_clnt *clnt;
347 struct rpc_task_setup task_setup_data = {
348 .rpc_message = msg,
349 .callback_ops = tk_ops,
350 .callback_data = req,
351 .flags = RPC_TASK_ASYNC,
352 };
353
354 dprintk("lockd: call procedure %d on %s (async)\n",
355 (int)proc, host->h_name);
356
357 /* If we have no RPC client yet, create one. */
358 clnt = nlm_bind_host(host);
359 if (clnt == NULL)
360 goto out_err;
361 msg->rpc_proc = &clnt->cl_procinfo[proc];
362 task_setup_data.rpc_client = clnt;
363
364 /* bootstrap and kick off the async RPC call */
365 return rpc_run_task(&task_setup_data);
366 out_err:
367 tk_ops->rpc_release(req);
368 return ERR_PTR(-ENOLCK);
369 }
370
nlm_do_async_call(struct nlm_rqst * req,u32 proc,struct rpc_message * msg,const struct rpc_call_ops * tk_ops)371 static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
372 {
373 struct rpc_task *task;
374
375 task = __nlm_async_call(req, proc, msg, tk_ops);
376 if (IS_ERR(task))
377 return PTR_ERR(task);
378 rpc_put_task(task);
379 return 0;
380 }
381
382 /*
383 * NLM asynchronous call.
384 */
nlm_async_call(struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)385 int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
386 {
387 struct rpc_message msg = {
388 .rpc_argp = &req->a_args,
389 .rpc_resp = &req->a_res,
390 };
391 return nlm_do_async_call(req, proc, &msg, tk_ops);
392 }
393
nlm_async_reply(struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)394 int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
395 {
396 struct rpc_message msg = {
397 .rpc_argp = &req->a_res,
398 };
399 return nlm_do_async_call(req, proc, &msg, tk_ops);
400 }
401
402 /*
403 * NLM client asynchronous call.
404 *
405 * Note that although the calls are asynchronous, and are therefore
406 * guaranteed to complete, we still always attempt to wait for
407 * completion in order to be able to correctly track the lock
408 * state.
409 */
nlmclnt_async_call(const struct cred * cred,struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)410 static int nlmclnt_async_call(const struct cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
411 {
412 struct rpc_message msg = {
413 .rpc_argp = &req->a_args,
414 .rpc_resp = &req->a_res,
415 .rpc_cred = cred,
416 };
417 struct rpc_task *task;
418 int err;
419
420 task = __nlm_async_call(req, proc, &msg, tk_ops);
421 if (IS_ERR(task))
422 return PTR_ERR(task);
423 err = rpc_wait_for_completion_task(task);
424 rpc_put_task(task);
425 return err;
426 }
427
428 /*
429 * TEST for the presence of a conflicting lock
430 */
431 static int
nlmclnt_test(struct nlm_rqst * req,struct file_lock * fl)432 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
433 {
434 int status;
435
436 status = nlmclnt_call(nfs_file_cred(fl->c.flc_file), req,
437 NLMPROC_TEST);
438 if (status < 0)
439 goto out;
440
441 switch (req->a_res.status) {
442 case nlm_granted:
443 fl->c.flc_type = F_UNLCK;
444 break;
445 case nlm_lck_denied:
446 /*
447 * Report the conflicting lock back to the application.
448 */
449 fl->fl_start = req->a_res.lock.fl.fl_start;
450 fl->fl_end = req->a_res.lock.fl.fl_end;
451 fl->c.flc_type = req->a_res.lock.fl.c.flc_type;
452 fl->c.flc_pid = -req->a_res.lock.fl.c.flc_pid;
453 break;
454 default:
455 status = nlm_stat_to_errno(req->a_res.status);
456 }
457 out:
458 trace_nlmclnt_test(&req->a_args.lock,
459 (const struct sockaddr *)&req->a_host->h_addr,
460 req->a_host->h_addrlen, req->a_res.status);
461 nlmclnt_release_call(req);
462 return status;
463 }
464
nlmclnt_locks_copy_lock(struct file_lock * new,struct file_lock * fl)465 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
466 {
467 spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
468 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
469 new->fl_u.nfs_fl.owner = nlmclnt_get_lockowner(fl->fl_u.nfs_fl.owner);
470 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
471 spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
472 }
473
nlmclnt_locks_release_private(struct file_lock * fl)474 static void nlmclnt_locks_release_private(struct file_lock *fl)
475 {
476 spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
477 list_del(&fl->fl_u.nfs_fl.list);
478 spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
479 nlmclnt_put_lockowner(fl->fl_u.nfs_fl.owner);
480 }
481
482 static const struct file_lock_operations nlmclnt_lock_ops = {
483 .fl_copy_lock = nlmclnt_locks_copy_lock,
484 .fl_release_private = nlmclnt_locks_release_private,
485 };
486
nlmclnt_locks_init_private(struct file_lock * fl,struct nlm_host * host)487 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
488 {
489 fl->fl_u.nfs_fl.state = 0;
490 fl->fl_ops = NULL;
491 fl->fl_u.nfs_fl.owner = nlmclnt_find_lockowner(host,
492 fl->c.flc_owner);
493 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
494 if (!fl->fl_u.nfs_fl.owner)
495 return;
496 fl->fl_ops = &nlmclnt_lock_ops;
497 }
498
do_vfs_lock(struct file_lock * fl)499 static int do_vfs_lock(struct file_lock *fl)
500 {
501 return locks_lock_file_wait(fl->c.flc_file, fl);
502 }
503
504 /*
505 * LOCK: Try to create a lock
506 *
507 * Programmer Harassment Alert
508 *
509 * When given a blocking lock request in a sync RPC call, the HPUX lockd
510 * will faithfully return LCK_BLOCKED but never cares to notify us when
511 * the lock could be granted. This way, our local process could hang
512 * around forever waiting for the callback.
513 *
514 * Solution A: Implement busy-waiting
515 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
516 *
517 * For now I am implementing solution A, because I hate the idea of
518 * re-implementing lockd for a third time in two months. The async
519 * calls shouldn't be too hard to do, however.
520 *
521 * This is one of the lovely things about standards in the NFS area:
522 * they're so soft and squishy you can't really blame HP for doing this.
523 */
524 static int
nlmclnt_lock(struct nlm_rqst * req,struct file_lock * fl)525 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
526 {
527 const struct cred *cred = nfs_file_cred(fl->c.flc_file);
528 struct nlm_host *host = req->a_host;
529 struct lockd_res *resp = &req->a_res;
530 struct nlm_wait block;
531 unsigned char flags = fl->c.flc_flags;
532 unsigned char type;
533 __be32 b_status;
534 int status = -ENOLCK;
535
536 if (nsm_monitor(host) < 0)
537 goto out;
538 req->a_args.state = nsm_local_state;
539
540 fl->c.flc_flags |= FL_ACCESS;
541 status = do_vfs_lock(fl);
542 fl->c.flc_flags = flags;
543 if (status < 0)
544 goto out;
545
546 nlmclnt_prepare_block(&block, host, fl);
547 again:
548 /*
549 * Initialise resp->status to a valid non-zero value,
550 * since 0 == nlm_lck_granted
551 */
552 resp->status = nlm_lck_blocked;
553
554 /*
555 * A GRANTED callback can come at any time -- even before the reply
556 * to the LOCK request arrives, so we queue the wait before
557 * requesting the lock.
558 */
559 nlmclnt_queue_block(&block);
560 for (;;) {
561 /* Reboot protection */
562 fl->fl_u.nfs_fl.state = host->h_state;
563 status = nlmclnt_call(cred, req, NLMPROC_LOCK);
564 if (status < 0)
565 break;
566 /* Did a reclaimer thread notify us of a server reboot? */
567 if (resp->status == nlm_lck_denied_grace_period)
568 continue;
569 if (resp->status != nlm_lck_blocked)
570 break;
571 /* Wait on an NLM blocking lock */
572 status = nlmclnt_wait(&block, req, NLMCLNT_POLL_TIMEOUT);
573 if (status < 0)
574 break;
575 if (block.b_status != nlm_lck_blocked)
576 break;
577 }
578 b_status = nlmclnt_dequeue_block(&block);
579 if (resp->status == nlm_lck_blocked)
580 resp->status = b_status;
581
582 /* if we were interrupted while blocking, then cancel the lock request
583 * and exit
584 */
585 if (resp->status == nlm_lck_blocked) {
586 if (!req->a_args.block)
587 goto out_unlock;
588 if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
589 goto out;
590 }
591
592 if (resp->status == nlm_granted) {
593 down_read(&host->h_rwsem);
594 /* Check whether or not the server has rebooted */
595 if (fl->fl_u.nfs_fl.state != host->h_state) {
596 up_read(&host->h_rwsem);
597 goto again;
598 }
599 /* Ensure the resulting lock will get added to granted list */
600 fl->c.flc_flags |= FL_SLEEP;
601 if (do_vfs_lock(fl) < 0)
602 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
603 up_read(&host->h_rwsem);
604 fl->c.flc_flags = flags;
605 status = 0;
606 }
607 if (status < 0)
608 goto out_unlock;
609 /*
610 * EAGAIN doesn't make sense for sleeping locks, and in some
611 * cases NLM_LCK_DENIED is returned for a permanent error. So
612 * turn it into an ENOLCK.
613 */
614 if (resp->status == nlm_lck_denied && (flags & FL_SLEEP))
615 status = -ENOLCK;
616 else
617 status = nlm_stat_to_errno(resp->status);
618 out:
619 trace_nlmclnt_lock(&req->a_args.lock,
620 (const struct sockaddr *)&req->a_host->h_addr,
621 req->a_host->h_addrlen, req->a_res.status);
622 nlmclnt_release_call(req);
623 return status;
624 out_unlock:
625 /* Fatal error: ensure that we remove the lock altogether */
626 trace_nlmclnt_lock(&req->a_args.lock,
627 (const struct sockaddr *)&req->a_host->h_addr,
628 req->a_host->h_addrlen, req->a_res.status);
629 dprintk("lockd: lock attempt ended in fatal error.\n"
630 " Attempting to unlock.\n");
631 type = fl->c.flc_type;
632 fl->c.flc_type = F_UNLCK;
633 down_read(&host->h_rwsem);
634 do_vfs_lock(fl);
635 up_read(&host->h_rwsem);
636 fl->c.flc_type = type;
637 fl->c.flc_flags = flags;
638 nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
639 return status;
640 }
641
642 /*
643 * RECLAIM: Try to reclaim a lock
644 */
645 int
nlmclnt_reclaim(struct nlm_host * host,struct file_lock * fl,struct nlm_rqst * req)646 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl,
647 struct nlm_rqst *req)
648 {
649 int status;
650
651 memset(req, 0, sizeof(*req));
652 locks_init_lock(&req->a_args.lock.fl);
653 locks_init_lock(&req->a_res.lock.fl);
654 req->a_host = host;
655
656 /* Set up the argument struct */
657 nlmclnt_setlockargs(req, fl);
658 req->a_args.reclaim = 1;
659
660 status = nlmclnt_call(nfs_file_cred(fl->c.flc_file), req,
661 NLMPROC_LOCK);
662 if (status >= 0 && req->a_res.status == nlm_granted)
663 return 0;
664
665 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
666 "(errno %d, status %d)\n",
667 fl->c.flc_pid,
668 status, ntohl(req->a_res.status));
669
670 /*
671 * FIXME: This is a serious failure. We can
672 *
673 * a. Ignore the problem
674 * b. Send the owning process some signal (Linux doesn't have
675 * SIGLOST, though...)
676 * c. Retry the operation
677 *
678 * Until someone comes up with a simple implementation
679 * for b or c, I'll choose option a.
680 */
681
682 return -ENOLCK;
683 }
684
685 /*
686 * UNLOCK: remove an existing lock
687 */
688 static int
nlmclnt_unlock(struct nlm_rqst * req,struct file_lock * fl)689 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
690 {
691 struct nlm_host *host = req->a_host;
692 struct lockd_res *resp = &req->a_res;
693 int status;
694 unsigned char flags = fl->c.flc_flags;
695
696 /*
697 * Note: the server is supposed to either grant us the unlock
698 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
699 * case, we want to unlock.
700 */
701 fl->c.flc_flags |= FL_EXISTS;
702 down_read(&host->h_rwsem);
703 status = do_vfs_lock(fl);
704 up_read(&host->h_rwsem);
705 fl->c.flc_flags = flags;
706 if (status == -ENOENT) {
707 status = 0;
708 goto out;
709 }
710
711 refcount_inc(&req->a_count);
712 status = nlmclnt_async_call(nfs_file_cred(fl->c.flc_file), req,
713 NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
714 if (status < 0)
715 goto out;
716
717 if (resp->status == nlm_granted)
718 goto out;
719
720 if (resp->status != nlm_lck_denied_nolocks)
721 printk("lockd: unexpected unlock status: %d\n",
722 ntohl(resp->status));
723 /* What to do now? I'm out of my depth... */
724 status = -ENOLCK;
725 out:
726 trace_nlmclnt_unlock(&req->a_args.lock,
727 (const struct sockaddr *)&req->a_host->h_addr,
728 req->a_host->h_addrlen, req->a_res.status);
729 nlmclnt_release_call(req);
730 return status;
731 }
732
nlmclnt_unlock_prepare(struct rpc_task * task,void * data)733 static void nlmclnt_unlock_prepare(struct rpc_task *task, void *data)
734 {
735 struct nlm_rqst *req = data;
736 const struct nlmclnt_operations *nlmclnt_ops = req->a_host->h_nlmclnt_ops;
737 bool defer_call = false;
738
739 if (nlmclnt_ops && nlmclnt_ops->nlmclnt_unlock_prepare)
740 defer_call = nlmclnt_ops->nlmclnt_unlock_prepare(task, req->a_callback_data);
741
742 if (!defer_call)
743 rpc_call_start(task);
744 }
745
nlmclnt_unlock_callback(struct rpc_task * task,void * data)746 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
747 {
748 struct nlm_rqst *req = data;
749 u32 status = ntohl(req->a_res.status);
750
751 if (RPC_SIGNALLED(task))
752 goto die;
753
754 if (task->tk_status < 0) {
755 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
756 switch (task->tk_status) {
757 case -EACCES:
758 case -EIO:
759 goto die;
760 default:
761 goto retry_rebind;
762 }
763 }
764 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
765 rpc_delay(task, NLMCLNT_GRACE_WAIT);
766 goto retry_unlock;
767 }
768 if (status != NLM_LCK_GRANTED)
769 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
770 die:
771 return;
772 retry_rebind:
773 nlm_rebind_host(req->a_host);
774 retry_unlock:
775 rpc_restart_call(task);
776 }
777
778 static const struct rpc_call_ops nlmclnt_unlock_ops = {
779 .rpc_call_prepare = nlmclnt_unlock_prepare,
780 .rpc_call_done = nlmclnt_unlock_callback,
781 .rpc_release = nlmclnt_rpc_release,
782 };
783
784 /*
785 * Cancel a blocked lock request.
786 * We always use an async RPC call for this in order not to hang a
787 * process that has been Ctrl-C'ed.
788 */
nlmclnt_cancel(struct nlm_host * host,int block,struct file_lock * fl)789 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
790 {
791 struct nlm_rqst *req;
792 int status;
793
794 dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
795 " Attempting to cancel lock.\n");
796
797 req = nlm_alloc_call(host);
798 if (!req)
799 return -ENOMEM;
800 req->a_flags = RPC_TASK_ASYNC;
801
802 nlmclnt_setlockargs(req, fl);
803 req->a_args.block = block;
804
805 refcount_inc(&req->a_count);
806 status = nlmclnt_async_call(nfs_file_cred(fl->c.flc_file), req,
807 NLMPROC_CANCEL, &nlmclnt_cancel_ops);
808 if (status == 0 && req->a_res.status == nlm_lck_denied)
809 status = -ENOLCK;
810 nlmclnt_release_call(req);
811 return status;
812 }
813
nlmclnt_cancel_callback(struct rpc_task * task,void * data)814 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
815 {
816 struct nlm_rqst *req = data;
817 u32 status = ntohl(req->a_res.status);
818
819 if (RPC_SIGNALLED(task))
820 goto die;
821
822 if (task->tk_status < 0) {
823 dprintk("lockd: CANCEL call error %d, retrying.\n",
824 task->tk_status);
825 goto retry_cancel;
826 }
827
828 switch (status) {
829 case NLM_LCK_GRANTED:
830 case NLM_LCK_DENIED_GRACE_PERIOD:
831 case NLM_LCK_DENIED:
832 /* Everything's good */
833 break;
834 case NLM_LCK_DENIED_NOLOCKS:
835 dprintk("lockd: CANCEL failed (server has no locks)\n");
836 goto retry_cancel;
837 default:
838 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
839 status);
840 }
841
842 die:
843 return;
844
845 retry_cancel:
846 /* Don't ever retry more than 3 times */
847 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
848 goto die;
849 nlm_rebind_host(req->a_host);
850 rpc_restart_call(task);
851 rpc_delay(task, 30 * HZ);
852 }
853
854 static const struct rpc_call_ops nlmclnt_cancel_ops = {
855 .rpc_call_done = nlmclnt_cancel_callback,
856 .rpc_release = nlmclnt_rpc_release,
857 };
858
859 /*
860 * Convert an NLM status code to a generic kernel errno
861 */
862 static int
nlm_stat_to_errno(__be32 status)863 nlm_stat_to_errno(__be32 status)
864 {
865 switch(ntohl(status)) {
866 case NLM_LCK_GRANTED:
867 return 0;
868 case NLM_LCK_DENIED:
869 return -EAGAIN;
870 case NLM_LCK_DENIED_NOLOCKS:
871 case NLM_LCK_DENIED_GRACE_PERIOD:
872 return -ENOLCK;
873 case NLM_LCK_BLOCKED:
874 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
875 return -ENOLCK;
876 #ifdef CONFIG_LOCKD_V4
877 case NLM_DEADLCK:
878 return -EDEADLK;
879 case NLM_ROFS:
880 return -EROFS;
881 case NLM_STALE_FH:
882 return -ESTALE;
883 case NLM_FBIG:
884 return -EOVERFLOW;
885 case NLM_FAILED:
886 return -ENOLCK;
887 #endif
888 }
889 printk(KERN_NOTICE "lockd: unexpected server status %d\n",
890 ntohl(status));
891 return -ENOLCK;
892 }
893