1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/svc4proc.c
5 * Lockd server procedures. We don't implement the NLM_*_RES
6 * procedures because we don't use the async procedures.
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/lockd/lockd.h>
14 #include <linux/lockd/share.h>
15 #include <linux/sunrpc/svc_xprt.h>
17 #define NLMDBG_FACILITY NLMDBG_CLIENT
20 * Obtain client and file from arguments
23 nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
24 struct nlm_host **hostp, struct nlm_file **filp)
26 struct nlm_host *host = NULL;
27 struct nlm_file *file = NULL;
28 struct nlm_lock *lock = &argp->lock;
31 /* nfsd callbacks must have been installed for this procedure */
33 return nlm_lck_denied_nolocks;
35 if (lock->lock_start > OFFSET_MAX ||
36 (lock->lock_len && ((lock->lock_len - 1) > (OFFSET_MAX - lock->lock_start))))
39 /* Obtain host handle */
40 if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
41 || (argp->monitor && nsm_monitor(host) < 0))
45 /* Obtain file pointer. Not used by FREE_ALL call. */
47 int mode = lock_to_openmode(&lock->fl);
49 error = nlm_lookup_file(rqstp, &file, lock);
54 /* Set up the missing parts of the file_lock structure */
55 lock->fl.fl_file = file->f_file[mode];
56 lock->fl.fl_pid = current->tgid;
57 lock->fl.fl_start = (loff_t)lock->lock_start;
58 lock->fl.fl_end = lock->lock_len ?
59 (loff_t)(lock->lock_start + lock->lock_len - 1) :
61 lock->fl.fl_lmops = &nlmsvc_lock_operations;
62 nlmsvc_locks_init_private(&lock->fl, host, (pid_t)lock->svid);
63 if (!lock->fl.fl_owner) {
64 /* lockowner allocation has failed */
65 nlmsvc_release_host(host);
66 return nlm_lck_denied_nolocks;
73 nlmsvc_release_host(host);
76 return nlm_lck_denied_nolocks;
80 * NULL: Test for presence of service
83 nlm4svc_proc_null(struct svc_rqst *rqstp)
85 dprintk("lockd: NULL called\n");
90 * TEST: Check for conflicting lock
93 __nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
95 struct nlm_args *argp = rqstp->rq_argp;
96 struct nlm_host *host;
97 struct nlm_file *file;
98 __be32 rc = rpc_success;
100 dprintk("lockd: TEST4 called\n");
101 resp->cookie = argp->cookie;
103 /* Obtain client and file */
104 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
105 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
107 /* Now check for conflicting locks */
108 resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie);
109 if (resp->status == nlm_drop_reply)
112 dprintk("lockd: TEST4 status %d\n", ntohl(resp->status));
114 nlmsvc_release_lockowner(&argp->lock);
115 nlmsvc_release_host(host);
116 nlm_release_file(file);
121 nlm4svc_proc_test(struct svc_rqst *rqstp)
123 return __nlm4svc_proc_test(rqstp, rqstp->rq_resp);
127 __nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_res *resp)
129 struct nlm_args *argp = rqstp->rq_argp;
130 struct nlm_host *host;
131 struct nlm_file *file;
132 __be32 rc = rpc_success;
134 dprintk("lockd: LOCK called\n");
136 resp->cookie = argp->cookie;
138 /* Obtain client and file */
139 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
140 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
143 /* If supplied state doesn't match current state, we assume it's
144 * an old request that time-warped somehow. Any error return would
145 * do in this case because it's irrelevant anyway.
147 * NB: We don't retrieve the remote host's state yet.
149 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
150 resp->status = nlm_lck_denied_nolocks;
154 /* Now try to lock the file */
155 resp->status = nlmsvc_lock(rqstp, file, host, &argp->lock,
156 argp->block, &argp->cookie,
158 if (resp->status == nlm_drop_reply)
161 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
163 nlmsvc_release_lockowner(&argp->lock);
164 nlmsvc_release_host(host);
165 nlm_release_file(file);
170 nlm4svc_proc_lock(struct svc_rqst *rqstp)
172 return __nlm4svc_proc_lock(rqstp, rqstp->rq_resp);
176 __nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_res *resp)
178 struct nlm_args *argp = rqstp->rq_argp;
179 struct nlm_host *host;
180 struct nlm_file *file;
182 dprintk("lockd: CANCEL called\n");
184 resp->cookie = argp->cookie;
186 /* Don't accept requests during grace period */
187 if (locks_in_grace(SVC_NET(rqstp))) {
188 resp->status = nlm_lck_denied_grace_period;
192 /* Obtain client and file */
193 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
194 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
196 /* Try to cancel request. */
197 resp->status = nlmsvc_cancel_blocked(SVC_NET(rqstp), file, &argp->lock);
199 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
200 nlmsvc_release_lockowner(&argp->lock);
201 nlmsvc_release_host(host);
202 nlm_release_file(file);
207 nlm4svc_proc_cancel(struct svc_rqst *rqstp)
209 return __nlm4svc_proc_cancel(rqstp, rqstp->rq_resp);
213 * UNLOCK: release a lock
216 __nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_res *resp)
218 struct nlm_args *argp = rqstp->rq_argp;
219 struct nlm_host *host;
220 struct nlm_file *file;
222 dprintk("lockd: UNLOCK called\n");
224 resp->cookie = argp->cookie;
226 /* Don't accept new lock requests during grace period */
227 if (locks_in_grace(SVC_NET(rqstp))) {
228 resp->status = nlm_lck_denied_grace_period;
232 /* Obtain client and file */
233 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
234 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
236 /* Now try to remove the lock */
237 resp->status = nlmsvc_unlock(SVC_NET(rqstp), file, &argp->lock);
239 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
240 nlmsvc_release_lockowner(&argp->lock);
241 nlmsvc_release_host(host);
242 nlm_release_file(file);
247 nlm4svc_proc_unlock(struct svc_rqst *rqstp)
249 return __nlm4svc_proc_unlock(rqstp, rqstp->rq_resp);
253 * GRANTED: A server calls us to tell that a process' lock request
257 __nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_res *resp)
259 struct nlm_args *argp = rqstp->rq_argp;
261 resp->cookie = argp->cookie;
263 dprintk("lockd: GRANTED called\n");
264 resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
265 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
270 nlm4svc_proc_granted(struct svc_rqst *rqstp)
272 return __nlm4svc_proc_granted(rqstp, rqstp->rq_resp);
276 * This is the generic lockd callback for async RPC calls
278 static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
280 dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
284 static void nlm4svc_callback_release(void *data)
286 nlmsvc_release_call(data);
289 static const struct rpc_call_ops nlm4svc_callback_ops = {
290 .rpc_call_done = nlm4svc_callback_exit,
291 .rpc_release = nlm4svc_callback_release,
295 * `Async' versions of the above service routines. They aren't really,
296 * because we send the callback before the reply proper. I hope this
297 * doesn't break any clients.
299 static __be32 nlm4svc_callback(struct svc_rqst *rqstp, u32 proc,
300 __be32 (*func)(struct svc_rqst *, struct nlm_res *))
302 struct nlm_args *argp = rqstp->rq_argp;
303 struct nlm_host *host;
304 struct nlm_rqst *call;
307 host = nlmsvc_lookup_host(rqstp,
311 return rpc_system_err;
313 call = nlm_alloc_call(host);
314 nlmsvc_release_host(host);
316 return rpc_system_err;
318 stat = func(rqstp, &call->a_res);
320 nlmsvc_release_call(call);
324 call->a_flags = RPC_TASK_ASYNC;
325 if (nlm_async_reply(call, proc, &nlm4svc_callback_ops) < 0)
326 return rpc_system_err;
330 static __be32 nlm4svc_proc_test_msg(struct svc_rqst *rqstp)
332 dprintk("lockd: TEST_MSG called\n");
333 return nlm4svc_callback(rqstp, NLMPROC_TEST_RES, __nlm4svc_proc_test);
336 static __be32 nlm4svc_proc_lock_msg(struct svc_rqst *rqstp)
338 dprintk("lockd: LOCK_MSG called\n");
339 return nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, __nlm4svc_proc_lock);
342 static __be32 nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp)
344 dprintk("lockd: CANCEL_MSG called\n");
345 return nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, __nlm4svc_proc_cancel);
348 static __be32 nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp)
350 dprintk("lockd: UNLOCK_MSG called\n");
351 return nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, __nlm4svc_proc_unlock);
354 static __be32 nlm4svc_proc_granted_msg(struct svc_rqst *rqstp)
356 dprintk("lockd: GRANTED_MSG called\n");
357 return nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, __nlm4svc_proc_granted);
361 * SHARE: create a DOS share or alter existing share.
364 nlm4svc_proc_share(struct svc_rqst *rqstp)
366 struct nlm_args *argp = rqstp->rq_argp;
367 struct nlm_res *resp = rqstp->rq_resp;
368 struct nlm_host *host;
369 struct nlm_file *file;
371 dprintk("lockd: SHARE called\n");
373 resp->cookie = argp->cookie;
375 /* Don't accept new lock requests during grace period */
376 if (locks_in_grace(SVC_NET(rqstp)) && !argp->reclaim) {
377 resp->status = nlm_lck_denied_grace_period;
381 /* Obtain client and file */
382 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
383 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
385 /* Now try to create the share */
386 resp->status = nlmsvc_share_file(host, file, argp);
388 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
389 nlmsvc_release_lockowner(&argp->lock);
390 nlmsvc_release_host(host);
391 nlm_release_file(file);
396 * UNSHARE: Release a DOS share.
399 nlm4svc_proc_unshare(struct svc_rqst *rqstp)
401 struct nlm_args *argp = rqstp->rq_argp;
402 struct nlm_res *resp = rqstp->rq_resp;
403 struct nlm_host *host;
404 struct nlm_file *file;
406 dprintk("lockd: UNSHARE called\n");
408 resp->cookie = argp->cookie;
410 /* Don't accept requests during grace period */
411 if (locks_in_grace(SVC_NET(rqstp))) {
412 resp->status = nlm_lck_denied_grace_period;
416 /* Obtain client and file */
417 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
418 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
420 /* Now try to lock the file */
421 resp->status = nlmsvc_unshare_file(host, file, argp);
423 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
424 nlmsvc_release_lockowner(&argp->lock);
425 nlmsvc_release_host(host);
426 nlm_release_file(file);
431 * NM_LOCK: Create an unmonitored lock
434 nlm4svc_proc_nm_lock(struct svc_rqst *rqstp)
436 struct nlm_args *argp = rqstp->rq_argp;
438 dprintk("lockd: NM_LOCK called\n");
440 argp->monitor = 0; /* just clean the monitor flag */
441 return nlm4svc_proc_lock(rqstp);
445 * FREE_ALL: Release all locks and shares held by client
448 nlm4svc_proc_free_all(struct svc_rqst *rqstp)
450 struct nlm_args *argp = rqstp->rq_argp;
451 struct nlm_host *host;
454 if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
457 nlmsvc_free_host_resources(host);
458 nlmsvc_release_host(host);
463 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
466 nlm4svc_proc_sm_notify(struct svc_rqst *rqstp)
468 struct nlm_reboot *argp = rqstp->rq_argp;
470 dprintk("lockd: SM_NOTIFY called\n");
472 if (!nlm_privileged_requester(rqstp)) {
473 char buf[RPC_MAX_ADDRBUFLEN];
474 printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
475 svc_print_addr(rqstp, buf, sizeof(buf)));
476 return rpc_system_err;
479 nlm_host_rebooted(SVC_NET(rqstp), argp);
484 * client sent a GRANTED_RES, let's remove the associated block
487 nlm4svc_proc_granted_res(struct svc_rqst *rqstp)
489 struct nlm_res *argp = rqstp->rq_argp;
494 dprintk("lockd: GRANTED_RES called\n");
496 nlmsvc_grant_reply(&argp->cookie, argp->status);
501 nlm4svc_proc_unused(struct svc_rqst *rqstp)
503 return rpc_proc_unavail;
508 * NLM Server procedures.
511 struct nlm_void { int dummy; };
513 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
514 #define No (1+1024/4) /* netobj */
515 #define St 1 /* status */
516 #define Rg 4 /* range (offset + length) */
518 const struct svc_procedure nlmsvc_procedures4[24] = {
520 .pc_func = nlm4svc_proc_null,
521 .pc_decode = nlm4svc_decode_void,
522 .pc_encode = nlm4svc_encode_void,
523 .pc_argsize = sizeof(struct nlm_void),
524 .pc_ressize = sizeof(struct nlm_void),
529 .pc_func = nlm4svc_proc_test,
530 .pc_decode = nlm4svc_decode_testargs,
531 .pc_encode = nlm4svc_encode_testres,
532 .pc_argsize = sizeof(struct nlm_args),
533 .pc_ressize = sizeof(struct nlm_res),
534 .pc_xdrressize = Ck+St+2+No+Rg,
538 .pc_func = nlm4svc_proc_lock,
539 .pc_decode = nlm4svc_decode_lockargs,
540 .pc_encode = nlm4svc_encode_res,
541 .pc_argsize = sizeof(struct nlm_args),
542 .pc_ressize = sizeof(struct nlm_res),
543 .pc_xdrressize = Ck+St,
547 .pc_func = nlm4svc_proc_cancel,
548 .pc_decode = nlm4svc_decode_cancargs,
549 .pc_encode = nlm4svc_encode_res,
550 .pc_argsize = sizeof(struct nlm_args),
551 .pc_ressize = sizeof(struct nlm_res),
552 .pc_xdrressize = Ck+St,
556 .pc_func = nlm4svc_proc_unlock,
557 .pc_decode = nlm4svc_decode_unlockargs,
558 .pc_encode = nlm4svc_encode_res,
559 .pc_argsize = sizeof(struct nlm_args),
560 .pc_ressize = sizeof(struct nlm_res),
561 .pc_xdrressize = Ck+St,
564 [NLMPROC_GRANTED] = {
565 .pc_func = nlm4svc_proc_granted,
566 .pc_decode = nlm4svc_decode_testargs,
567 .pc_encode = nlm4svc_encode_res,
568 .pc_argsize = sizeof(struct nlm_args),
569 .pc_ressize = sizeof(struct nlm_res),
570 .pc_xdrressize = Ck+St,
571 .pc_name = "GRANTED",
573 [NLMPROC_TEST_MSG] = {
574 .pc_func = nlm4svc_proc_test_msg,
575 .pc_decode = nlm4svc_decode_testargs,
576 .pc_encode = nlm4svc_encode_void,
577 .pc_argsize = sizeof(struct nlm_args),
578 .pc_ressize = sizeof(struct nlm_void),
580 .pc_name = "TEST_MSG",
582 [NLMPROC_LOCK_MSG] = {
583 .pc_func = nlm4svc_proc_lock_msg,
584 .pc_decode = nlm4svc_decode_lockargs,
585 .pc_encode = nlm4svc_encode_void,
586 .pc_argsize = sizeof(struct nlm_args),
587 .pc_ressize = sizeof(struct nlm_void),
589 .pc_name = "LOCK_MSG",
591 [NLMPROC_CANCEL_MSG] = {
592 .pc_func = nlm4svc_proc_cancel_msg,
593 .pc_decode = nlm4svc_decode_cancargs,
594 .pc_encode = nlm4svc_encode_void,
595 .pc_argsize = sizeof(struct nlm_args),
596 .pc_ressize = sizeof(struct nlm_void),
598 .pc_name = "CANCEL_MSG",
600 [NLMPROC_UNLOCK_MSG] = {
601 .pc_func = nlm4svc_proc_unlock_msg,
602 .pc_decode = nlm4svc_decode_unlockargs,
603 .pc_encode = nlm4svc_encode_void,
604 .pc_argsize = sizeof(struct nlm_args),
605 .pc_ressize = sizeof(struct nlm_void),
607 .pc_name = "UNLOCK_MSG",
609 [NLMPROC_GRANTED_MSG] = {
610 .pc_func = nlm4svc_proc_granted_msg,
611 .pc_decode = nlm4svc_decode_testargs,
612 .pc_encode = nlm4svc_encode_void,
613 .pc_argsize = sizeof(struct nlm_args),
614 .pc_ressize = sizeof(struct nlm_void),
616 .pc_name = "GRANTED_MSG",
618 [NLMPROC_TEST_RES] = {
619 .pc_func = nlm4svc_proc_null,
620 .pc_decode = nlm4svc_decode_void,
621 .pc_encode = nlm4svc_encode_void,
622 .pc_argsize = sizeof(struct nlm_res),
623 .pc_ressize = sizeof(struct nlm_void),
625 .pc_name = "TEST_RES",
627 [NLMPROC_LOCK_RES] = {
628 .pc_func = nlm4svc_proc_null,
629 .pc_decode = nlm4svc_decode_void,
630 .pc_encode = nlm4svc_encode_void,
631 .pc_argsize = sizeof(struct nlm_res),
632 .pc_ressize = sizeof(struct nlm_void),
634 .pc_name = "LOCK_RES",
636 [NLMPROC_CANCEL_RES] = {
637 .pc_func = nlm4svc_proc_null,
638 .pc_decode = nlm4svc_decode_void,
639 .pc_encode = nlm4svc_encode_void,
640 .pc_argsize = sizeof(struct nlm_res),
641 .pc_ressize = sizeof(struct nlm_void),
643 .pc_name = "CANCEL_RES",
645 [NLMPROC_UNLOCK_RES] = {
646 .pc_func = nlm4svc_proc_null,
647 .pc_decode = nlm4svc_decode_void,
648 .pc_encode = nlm4svc_encode_void,
649 .pc_argsize = sizeof(struct nlm_res),
650 .pc_ressize = sizeof(struct nlm_void),
652 .pc_name = "UNLOCK_RES",
654 [NLMPROC_GRANTED_RES] = {
655 .pc_func = nlm4svc_proc_granted_res,
656 .pc_decode = nlm4svc_decode_res,
657 .pc_encode = nlm4svc_encode_void,
658 .pc_argsize = sizeof(struct nlm_res),
659 .pc_ressize = sizeof(struct nlm_void),
661 .pc_name = "GRANTED_RES",
663 [NLMPROC_NSM_NOTIFY] = {
664 .pc_func = nlm4svc_proc_sm_notify,
665 .pc_decode = nlm4svc_decode_reboot,
666 .pc_encode = nlm4svc_encode_void,
667 .pc_argsize = sizeof(struct nlm_reboot),
668 .pc_ressize = sizeof(struct nlm_void),
670 .pc_name = "SM_NOTIFY",
673 .pc_func = nlm4svc_proc_unused,
674 .pc_decode = nlm4svc_decode_void,
675 .pc_encode = nlm4svc_encode_void,
676 .pc_argsize = sizeof(struct nlm_void),
677 .pc_ressize = sizeof(struct nlm_void),
682 .pc_func = nlm4svc_proc_unused,
683 .pc_decode = nlm4svc_decode_void,
684 .pc_encode = nlm4svc_encode_void,
685 .pc_argsize = sizeof(struct nlm_void),
686 .pc_ressize = sizeof(struct nlm_void),
691 .pc_func = nlm4svc_proc_unused,
692 .pc_decode = nlm4svc_decode_void,
693 .pc_encode = nlm4svc_encode_void,
694 .pc_argsize = sizeof(struct nlm_void),
695 .pc_ressize = sizeof(struct nlm_void),
700 .pc_func = nlm4svc_proc_share,
701 .pc_decode = nlm4svc_decode_shareargs,
702 .pc_encode = nlm4svc_encode_shareres,
703 .pc_argsize = sizeof(struct nlm_args),
704 .pc_ressize = sizeof(struct nlm_res),
705 .pc_xdrressize = Ck+St+1,
708 [NLMPROC_UNSHARE] = {
709 .pc_func = nlm4svc_proc_unshare,
710 .pc_decode = nlm4svc_decode_shareargs,
711 .pc_encode = nlm4svc_encode_shareres,
712 .pc_argsize = sizeof(struct nlm_args),
713 .pc_ressize = sizeof(struct nlm_res),
714 .pc_xdrressize = Ck+St+1,
715 .pc_name = "UNSHARE",
717 [NLMPROC_NM_LOCK] = {
718 .pc_func = nlm4svc_proc_nm_lock,
719 .pc_decode = nlm4svc_decode_lockargs,
720 .pc_encode = nlm4svc_encode_res,
721 .pc_argsize = sizeof(struct nlm_args),
722 .pc_ressize = sizeof(struct nlm_res),
723 .pc_xdrressize = Ck+St,
724 .pc_name = "NM_LOCK",
726 [NLMPROC_FREE_ALL] = {
727 .pc_func = nlm4svc_proc_free_all,
728 .pc_decode = nlm4svc_decode_notify,
729 .pc_encode = nlm4svc_encode_void,
730 .pc_argsize = sizeof(struct nlm_args),
731 .pc_ressize = sizeof(struct nlm_void),
733 .pc_name = "FREE_ALL",