1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/lockd/svcproc.c
5 * Lockd server procedures. We don't implement the NLM_*_RES
6 * procedures because we don't use the async procedures.
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
19 #ifdef CONFIG_LOCKD_V4
21 cast_to_nlm(__be32 status, u32 vers)
23 /* Note: status is assumed to be in network byte order !!! */
28 case nlm_lck_denied_nolocks:
30 case nlm_lck_denied_grace_period:
34 status = nlm_lck_denied;
37 status = nlm_lck_denied_nolocks;
43 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
45 #define cast_status(status) (status)
49 * Obtain client and file from arguments
52 nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
53 struct nlm_host **hostp, struct nlm_file **filp)
55 struct nlm_host *host = NULL;
56 struct nlm_file *file = NULL;
57 struct nlm_lock *lock = &argp->lock;
61 /* nfsd callbacks must have been installed for this procedure */
63 return nlm_lck_denied_nolocks;
65 /* Obtain host handle */
66 if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
67 || (argp->monitor && nsm_monitor(host) < 0))
71 /* Obtain file pointer. Not used by FREE_ALL call. */
73 error = cast_status(nlm_lookup_file(rqstp, &file, lock));
78 /* Set up the missing parts of the file_lock structure */
79 mode = lock_to_openmode(&lock->fl);
80 lock->fl.fl_file = file->f_file[mode];
81 lock->fl.fl_pid = current->tgid;
82 lock->fl.fl_lmops = &nlmsvc_lock_operations;
83 nlmsvc_locks_init_private(&lock->fl, host, (pid_t)lock->svid);
84 if (!lock->fl.fl_owner) {
85 /* lockowner allocation has failed */
86 nlmsvc_release_host(host);
87 return nlm_lck_denied_nolocks;
94 nlmsvc_release_host(host);
97 return nlm_lck_denied_nolocks;
101 * NULL: Test for presence of service
104 nlmsvc_proc_null(struct svc_rqst *rqstp)
106 dprintk("lockd: NULL called\n");
111 * TEST: Check for conflicting lock
114 __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
116 struct nlm_args *argp = rqstp->rq_argp;
117 struct nlm_host *host;
118 struct nlm_file *file;
119 struct nlm_lockowner *test_owner;
120 __be32 rc = rpc_success;
122 dprintk("lockd: TEST called\n");
123 resp->cookie = argp->cookie;
125 /* Obtain client and file */
126 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
127 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
129 test_owner = argp->lock.fl.fl_owner;
131 /* Now check for conflicting locks */
132 resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie));
133 if (resp->status == nlm_drop_reply)
136 dprintk("lockd: TEST status %d vers %d\n",
137 ntohl(resp->status), rqstp->rq_vers);
139 nlmsvc_put_lockowner(test_owner);
140 nlmsvc_release_host(host);
141 nlm_release_file(file);
146 nlmsvc_proc_test(struct svc_rqst *rqstp)
148 return __nlmsvc_proc_test(rqstp, rqstp->rq_resp);
152 __nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_res *resp)
154 struct nlm_args *argp = rqstp->rq_argp;
155 struct nlm_host *host;
156 struct nlm_file *file;
157 __be32 rc = rpc_success;
159 dprintk("lockd: LOCK called\n");
161 resp->cookie = argp->cookie;
163 /* Obtain client and file */
164 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
165 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
168 /* If supplied state doesn't match current state, we assume it's
169 * an old request that time-warped somehow. Any error return would
170 * do in this case because it's irrelevant anyway.
172 * NB: We don't retrieve the remote host's state yet.
174 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
175 resp->status = nlm_lck_denied_nolocks;
179 /* Now try to lock the file */
180 resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock,
181 argp->block, &argp->cookie,
183 if (resp->status == nlm_drop_reply)
186 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
188 nlmsvc_release_lockowner(&argp->lock);
189 nlmsvc_release_host(host);
190 nlm_release_file(file);
195 nlmsvc_proc_lock(struct svc_rqst *rqstp)
197 return __nlmsvc_proc_lock(rqstp, rqstp->rq_resp);
201 __nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_res *resp)
203 struct nlm_args *argp = rqstp->rq_argp;
204 struct nlm_host *host;
205 struct nlm_file *file;
206 struct net *net = SVC_NET(rqstp);
208 dprintk("lockd: CANCEL called\n");
210 resp->cookie = argp->cookie;
212 /* Don't accept requests during grace period */
213 if (locks_in_grace(net)) {
214 resp->status = nlm_lck_denied_grace_period;
218 /* Obtain client and file */
219 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
220 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
222 /* Try to cancel request. */
223 resp->status = cast_status(nlmsvc_cancel_blocked(net, file, &argp->lock));
225 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
226 nlmsvc_release_lockowner(&argp->lock);
227 nlmsvc_release_host(host);
228 nlm_release_file(file);
233 nlmsvc_proc_cancel(struct svc_rqst *rqstp)
235 return __nlmsvc_proc_cancel(rqstp, rqstp->rq_resp);
239 * UNLOCK: release a lock
242 __nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_res *resp)
244 struct nlm_args *argp = rqstp->rq_argp;
245 struct nlm_host *host;
246 struct nlm_file *file;
247 struct net *net = SVC_NET(rqstp);
249 dprintk("lockd: UNLOCK called\n");
251 resp->cookie = argp->cookie;
253 /* Don't accept new lock requests during grace period */
254 if (locks_in_grace(net)) {
255 resp->status = nlm_lck_denied_grace_period;
259 /* Obtain client and file */
260 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
261 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
263 /* Now try to remove the lock */
264 resp->status = cast_status(nlmsvc_unlock(net, file, &argp->lock));
266 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
267 nlmsvc_release_lockowner(&argp->lock);
268 nlmsvc_release_host(host);
269 nlm_release_file(file);
274 nlmsvc_proc_unlock(struct svc_rqst *rqstp)
276 return __nlmsvc_proc_unlock(rqstp, rqstp->rq_resp);
280 * GRANTED: A server calls us to tell that a process' lock request
284 __nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_res *resp)
286 struct nlm_args *argp = rqstp->rq_argp;
288 resp->cookie = argp->cookie;
290 dprintk("lockd: GRANTED called\n");
291 resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
292 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
297 nlmsvc_proc_granted(struct svc_rqst *rqstp)
299 return __nlmsvc_proc_granted(rqstp, rqstp->rq_resp);
303 * This is the generic lockd callback for async RPC calls
305 static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
309 void nlmsvc_release_call(struct nlm_rqst *call)
311 if (!refcount_dec_and_test(&call->a_count))
313 nlmsvc_release_host(call->a_host);
317 static void nlmsvc_callback_release(void *data)
319 nlmsvc_release_call(data);
322 static const struct rpc_call_ops nlmsvc_callback_ops = {
323 .rpc_call_done = nlmsvc_callback_exit,
324 .rpc_release = nlmsvc_callback_release,
328 * `Async' versions of the above service routines. They aren't really,
329 * because we send the callback before the reply proper. I hope this
330 * doesn't break any clients.
332 static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc,
333 __be32 (*func)(struct svc_rqst *, struct nlm_res *))
335 struct nlm_args *argp = rqstp->rq_argp;
336 struct nlm_host *host;
337 struct nlm_rqst *call;
340 host = nlmsvc_lookup_host(rqstp,
344 return rpc_system_err;
346 call = nlm_alloc_call(host);
347 nlmsvc_release_host(host);
349 return rpc_system_err;
351 stat = func(rqstp, &call->a_res);
353 nlmsvc_release_call(call);
357 call->a_flags = RPC_TASK_ASYNC;
358 if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
359 return rpc_system_err;
363 static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp)
365 dprintk("lockd: TEST_MSG called\n");
366 return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, __nlmsvc_proc_test);
369 static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp)
371 dprintk("lockd: LOCK_MSG called\n");
372 return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, __nlmsvc_proc_lock);
375 static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp)
377 dprintk("lockd: CANCEL_MSG called\n");
378 return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, __nlmsvc_proc_cancel);
382 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp)
384 dprintk("lockd: UNLOCK_MSG called\n");
385 return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, __nlmsvc_proc_unlock);
389 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp)
391 dprintk("lockd: GRANTED_MSG called\n");
392 return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, __nlmsvc_proc_granted);
396 * SHARE: create a DOS share or alter existing share.
399 nlmsvc_proc_share(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: SHARE called\n");
408 resp->cookie = argp->cookie;
410 /* Don't accept new lock requests during grace period */
411 if (locks_in_grace(SVC_NET(rqstp)) && !argp->reclaim) {
412 resp->status = nlm_lck_denied_grace_period;
416 /* Obtain client and file */
417 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
418 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
420 /* Now try to create the share */
421 resp->status = cast_status(nlmsvc_share_file(host, file, argp));
423 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
424 nlmsvc_release_lockowner(&argp->lock);
425 nlmsvc_release_host(host);
426 nlm_release_file(file);
431 * UNSHARE: Release a DOS share.
434 nlmsvc_proc_unshare(struct svc_rqst *rqstp)
436 struct nlm_args *argp = rqstp->rq_argp;
437 struct nlm_res *resp = rqstp->rq_resp;
438 struct nlm_host *host;
439 struct nlm_file *file;
441 dprintk("lockd: UNSHARE called\n");
443 resp->cookie = argp->cookie;
445 /* Don't accept requests during grace period */
446 if (locks_in_grace(SVC_NET(rqstp))) {
447 resp->status = nlm_lck_denied_grace_period;
451 /* Obtain client and file */
452 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
453 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
455 /* Now try to unshare the file */
456 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
458 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
459 nlmsvc_release_lockowner(&argp->lock);
460 nlmsvc_release_host(host);
461 nlm_release_file(file);
466 * NM_LOCK: Create an unmonitored lock
469 nlmsvc_proc_nm_lock(struct svc_rqst *rqstp)
471 struct nlm_args *argp = rqstp->rq_argp;
473 dprintk("lockd: NM_LOCK called\n");
475 argp->monitor = 0; /* just clean the monitor flag */
476 return nlmsvc_proc_lock(rqstp);
480 * FREE_ALL: Release all locks and shares held by client
483 nlmsvc_proc_free_all(struct svc_rqst *rqstp)
485 struct nlm_args *argp = rqstp->rq_argp;
486 struct nlm_host *host;
489 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
492 nlmsvc_free_host_resources(host);
493 nlmsvc_release_host(host);
498 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
501 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp)
503 struct nlm_reboot *argp = rqstp->rq_argp;
505 dprintk("lockd: SM_NOTIFY called\n");
507 if (!nlm_privileged_requester(rqstp)) {
508 char buf[RPC_MAX_ADDRBUFLEN];
509 printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
510 svc_print_addr(rqstp, buf, sizeof(buf)));
511 return rpc_system_err;
514 nlm_host_rebooted(SVC_NET(rqstp), argp);
519 * client sent a GRANTED_RES, let's remove the associated block
522 nlmsvc_proc_granted_res(struct svc_rqst *rqstp)
524 struct nlm_res *argp = rqstp->rq_argp;
529 dprintk("lockd: GRANTED_RES called\n");
531 nlmsvc_grant_reply(&argp->cookie, argp->status);
536 nlmsvc_proc_unused(struct svc_rqst *rqstp)
538 return rpc_proc_unavail;
542 * NLM Server procedures.
545 struct nlm_void { int dummy; };
547 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
548 #define St 1 /* status */
549 #define No (1+1024/4) /* Net Obj */
550 #define Rg 2 /* range - offset + size */
552 const struct svc_procedure nlmsvc_procedures[24] = {
554 .pc_func = nlmsvc_proc_null,
555 .pc_decode = nlmsvc_decode_void,
556 .pc_encode = nlmsvc_encode_void,
557 .pc_argsize = sizeof(struct nlm_void),
558 .pc_ressize = sizeof(struct nlm_void),
563 .pc_func = nlmsvc_proc_test,
564 .pc_decode = nlmsvc_decode_testargs,
565 .pc_encode = nlmsvc_encode_testres,
566 .pc_argsize = sizeof(struct nlm_args),
567 .pc_ressize = sizeof(struct nlm_res),
568 .pc_xdrressize = Ck+St+2+No+Rg,
572 .pc_func = nlmsvc_proc_lock,
573 .pc_decode = nlmsvc_decode_lockargs,
574 .pc_encode = nlmsvc_encode_res,
575 .pc_argsize = sizeof(struct nlm_args),
576 .pc_ressize = sizeof(struct nlm_res),
577 .pc_xdrressize = Ck+St,
581 .pc_func = nlmsvc_proc_cancel,
582 .pc_decode = nlmsvc_decode_cancargs,
583 .pc_encode = nlmsvc_encode_res,
584 .pc_argsize = sizeof(struct nlm_args),
585 .pc_ressize = sizeof(struct nlm_res),
586 .pc_xdrressize = Ck+St,
590 .pc_func = nlmsvc_proc_unlock,
591 .pc_decode = nlmsvc_decode_unlockargs,
592 .pc_encode = nlmsvc_encode_res,
593 .pc_argsize = sizeof(struct nlm_args),
594 .pc_ressize = sizeof(struct nlm_res),
595 .pc_xdrressize = Ck+St,
598 [NLMPROC_GRANTED] = {
599 .pc_func = nlmsvc_proc_granted,
600 .pc_decode = nlmsvc_decode_testargs,
601 .pc_encode = nlmsvc_encode_res,
602 .pc_argsize = sizeof(struct nlm_args),
603 .pc_ressize = sizeof(struct nlm_res),
604 .pc_xdrressize = Ck+St,
605 .pc_name = "GRANTED",
607 [NLMPROC_TEST_MSG] = {
608 .pc_func = nlmsvc_proc_test_msg,
609 .pc_decode = nlmsvc_decode_testargs,
610 .pc_encode = nlmsvc_encode_void,
611 .pc_argsize = sizeof(struct nlm_args),
612 .pc_ressize = sizeof(struct nlm_void),
614 .pc_name = "TEST_MSG",
616 [NLMPROC_LOCK_MSG] = {
617 .pc_func = nlmsvc_proc_lock_msg,
618 .pc_decode = nlmsvc_decode_lockargs,
619 .pc_encode = nlmsvc_encode_void,
620 .pc_argsize = sizeof(struct nlm_args),
621 .pc_ressize = sizeof(struct nlm_void),
623 .pc_name = "LOCK_MSG",
625 [NLMPROC_CANCEL_MSG] = {
626 .pc_func = nlmsvc_proc_cancel_msg,
627 .pc_decode = nlmsvc_decode_cancargs,
628 .pc_encode = nlmsvc_encode_void,
629 .pc_argsize = sizeof(struct nlm_args),
630 .pc_ressize = sizeof(struct nlm_void),
632 .pc_name = "CANCEL_MSG",
634 [NLMPROC_UNLOCK_MSG] = {
635 .pc_func = nlmsvc_proc_unlock_msg,
636 .pc_decode = nlmsvc_decode_unlockargs,
637 .pc_encode = nlmsvc_encode_void,
638 .pc_argsize = sizeof(struct nlm_args),
639 .pc_ressize = sizeof(struct nlm_void),
641 .pc_name = "UNLOCK_MSG",
643 [NLMPROC_GRANTED_MSG] = {
644 .pc_func = nlmsvc_proc_granted_msg,
645 .pc_decode = nlmsvc_decode_testargs,
646 .pc_encode = nlmsvc_encode_void,
647 .pc_argsize = sizeof(struct nlm_args),
648 .pc_ressize = sizeof(struct nlm_void),
650 .pc_name = "GRANTED_MSG",
652 [NLMPROC_TEST_RES] = {
653 .pc_func = nlmsvc_proc_null,
654 .pc_decode = nlmsvc_decode_void,
655 .pc_encode = nlmsvc_encode_void,
656 .pc_argsize = sizeof(struct nlm_res),
657 .pc_ressize = sizeof(struct nlm_void),
659 .pc_name = "TEST_RES",
661 [NLMPROC_LOCK_RES] = {
662 .pc_func = nlmsvc_proc_null,
663 .pc_decode = nlmsvc_decode_void,
664 .pc_encode = nlmsvc_encode_void,
665 .pc_argsize = sizeof(struct nlm_res),
666 .pc_ressize = sizeof(struct nlm_void),
668 .pc_name = "LOCK_RES",
670 [NLMPROC_CANCEL_RES] = {
671 .pc_func = nlmsvc_proc_null,
672 .pc_decode = nlmsvc_decode_void,
673 .pc_encode = nlmsvc_encode_void,
674 .pc_argsize = sizeof(struct nlm_res),
675 .pc_ressize = sizeof(struct nlm_void),
677 .pc_name = "CANCEL_RES",
679 [NLMPROC_UNLOCK_RES] = {
680 .pc_func = nlmsvc_proc_null,
681 .pc_decode = nlmsvc_decode_void,
682 .pc_encode = nlmsvc_encode_void,
683 .pc_argsize = sizeof(struct nlm_res),
684 .pc_ressize = sizeof(struct nlm_void),
686 .pc_name = "UNLOCK_RES",
688 [NLMPROC_GRANTED_RES] = {
689 .pc_func = nlmsvc_proc_granted_res,
690 .pc_decode = nlmsvc_decode_res,
691 .pc_encode = nlmsvc_encode_void,
692 .pc_argsize = sizeof(struct nlm_res),
693 .pc_ressize = sizeof(struct nlm_void),
695 .pc_name = "GRANTED_RES",
697 [NLMPROC_NSM_NOTIFY] = {
698 .pc_func = nlmsvc_proc_sm_notify,
699 .pc_decode = nlmsvc_decode_reboot,
700 .pc_encode = nlmsvc_encode_void,
701 .pc_argsize = sizeof(struct nlm_reboot),
702 .pc_ressize = sizeof(struct nlm_void),
704 .pc_name = "SM_NOTIFY",
707 .pc_func = nlmsvc_proc_unused,
708 .pc_decode = nlmsvc_decode_void,
709 .pc_encode = nlmsvc_encode_void,
710 .pc_argsize = sizeof(struct nlm_void),
711 .pc_ressize = sizeof(struct nlm_void),
716 .pc_func = nlmsvc_proc_unused,
717 .pc_decode = nlmsvc_decode_void,
718 .pc_encode = nlmsvc_encode_void,
719 .pc_argsize = sizeof(struct nlm_void),
720 .pc_ressize = sizeof(struct nlm_void),
725 .pc_func = nlmsvc_proc_unused,
726 .pc_decode = nlmsvc_decode_void,
727 .pc_encode = nlmsvc_encode_void,
728 .pc_argsize = sizeof(struct nlm_void),
729 .pc_ressize = sizeof(struct nlm_void),
734 .pc_func = nlmsvc_proc_share,
735 .pc_decode = nlmsvc_decode_shareargs,
736 .pc_encode = nlmsvc_encode_shareres,
737 .pc_argsize = sizeof(struct nlm_args),
738 .pc_ressize = sizeof(struct nlm_res),
739 .pc_xdrressize = Ck+St+1,
742 [NLMPROC_UNSHARE] = {
743 .pc_func = nlmsvc_proc_unshare,
744 .pc_decode = nlmsvc_decode_shareargs,
745 .pc_encode = nlmsvc_encode_shareres,
746 .pc_argsize = sizeof(struct nlm_args),
747 .pc_ressize = sizeof(struct nlm_res),
748 .pc_xdrressize = Ck+St+1,
749 .pc_name = "UNSHARE",
751 [NLMPROC_NM_LOCK] = {
752 .pc_func = nlmsvc_proc_nm_lock,
753 .pc_decode = nlmsvc_decode_lockargs,
754 .pc_encode = nlmsvc_encode_res,
755 .pc_argsize = sizeof(struct nlm_args),
756 .pc_ressize = sizeof(struct nlm_res),
757 .pc_xdrressize = Ck+St,
758 .pc_name = "NM_LOCK",
760 [NLMPROC_FREE_ALL] = {
761 .pc_func = nlmsvc_proc_free_all,
762 .pc_decode = nlmsvc_decode_notify,
763 .pc_encode = nlmsvc_encode_void,
764 .pc_argsize = sizeof(struct nlm_args),
765 .pc_ressize = sizeof(struct nlm_void),
767 .pc_name = "FREE_ALL",