xref: /openbmc/linux/fs/lockd/svcproc.c (revision 89e0edfbea103d9b274efa10a8fc7a88bdac8f76)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/lockd/svcproc.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Lockd server procedures. We don't implement the NLM_*_RES
61da177e4SLinus Torvalds  * procedures because we don't use the async procedures.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/time.h>
131da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
141da177e4SLinus Torvalds #include <linux/lockd/share.h>
155ccb0066SStanislav Kinsbursky #include <linux/sunrpc/svc_xprt.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
2052921e02SAl Viro static __be32
2152921e02SAl Viro cast_to_nlm(__be32 status, u32 vers)
221da177e4SLinus Torvalds {
231da177e4SLinus Torvalds 	/* Note: status is assumed to be in network byte order !!! */
241da177e4SLinus Torvalds 	if (vers != 4){
251da177e4SLinus Torvalds 		switch (status) {
261da177e4SLinus Torvalds 		case nlm_granted:
271da177e4SLinus Torvalds 		case nlm_lck_denied:
281da177e4SLinus Torvalds 		case nlm_lck_denied_nolocks:
291da177e4SLinus Torvalds 		case nlm_lck_blocked:
301da177e4SLinus Torvalds 		case nlm_lck_denied_grace_period:
315ea0d750SMarc Eshel 		case nlm_drop_reply:
321da177e4SLinus Torvalds 			break;
331da177e4SLinus Torvalds 		case nlm4_deadlock:
341da177e4SLinus Torvalds 			status = nlm_lck_denied;
351da177e4SLinus Torvalds 			break;
361da177e4SLinus Torvalds 		default:
371da177e4SLinus Torvalds 			status = nlm_lck_denied_nolocks;
381da177e4SLinus Torvalds 		}
391da177e4SLinus Torvalds 	}
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 	return (status);
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds #define	cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
441da177e4SLinus Torvalds #else
451da177e4SLinus Torvalds #define cast_status(status) (status)
461da177e4SLinus Torvalds #endif
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Obtain client and file from arguments
501da177e4SLinus Torvalds  */
5152921e02SAl Viro static __be32
521da177e4SLinus Torvalds nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
531da177e4SLinus Torvalds 			struct nlm_host **hostp, struct nlm_file **filp)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	struct nlm_host		*host = NULL;
561da177e4SLinus Torvalds 	struct nlm_file		*file = NULL;
571da177e4SLinus Torvalds 	struct nlm_lock		*lock = &argp->lock;
5852921e02SAl Viro 	__be32			error = 0;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	/* nfsd callbacks must have been installed for this procedure */
611da177e4SLinus Torvalds 	if (!nlmsvc_ops)
621da177e4SLinus Torvalds 		return nlm_lck_denied_nolocks;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	/* Obtain host handle */
65db4e4c9aSOlaf Kirch 	if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
66977faf39SOlaf Kirch 	 || (argp->monitor && nsm_monitor(host) < 0))
671da177e4SLinus Torvalds 		goto no_locks;
681da177e4SLinus Torvalds 	*hostp = host;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	/* Obtain file pointer. Not used by FREE_ALL call. */
711da177e4SLinus Torvalds 	if (filp != NULL) {
72cd0b16c1STrond Myklebust 		error = cast_status(nlm_lookup_file(rqstp, &file, &lock->fh));
73cd0b16c1STrond Myklebust 		if (error != 0)
741da177e4SLinus Torvalds 			goto no_locks;
751da177e4SLinus Torvalds 		*filp = file;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 		/* Set up the missing parts of the file_lock structure */
781da177e4SLinus Torvalds 		lock->fl.fl_file  = file->f_file;
791da177e4SLinus Torvalds 		lock->fl.fl_lmops = &nlmsvc_lock_operations;
80*89e0edfbSBenjamin Coddington 		nlmsvc_locks_init_private(&lock->fl, host, (pid_t)lock->svid);
81*89e0edfbSBenjamin Coddington 		if (!lock->fl.fl_owner) {
82*89e0edfbSBenjamin Coddington 			/* lockowner allocation has failed */
83*89e0edfbSBenjamin Coddington 			nlmsvc_release_host(host);
84*89e0edfbSBenjamin Coddington 			return nlm_lck_denied_nolocks;
85*89e0edfbSBenjamin Coddington 		}
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	return 0;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds no_locks:
9167216b94SChuck Lever 	nlmsvc_release_host(host);
92d343fce1SNeilBrown 	if (error)
93d343fce1SNeilBrown 		return error;
941da177e4SLinus Torvalds 	return nlm_lck_denied_nolocks;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /*
981da177e4SLinus Torvalds  * NULL: Test for presence of service
991da177e4SLinus Torvalds  */
1007111c66eSAl Viro static __be32
101a6beb732SChristoph Hellwig nlmsvc_proc_null(struct svc_rqst *rqstp)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	dprintk("lockd: NULL          called\n");
1041da177e4SLinus Torvalds 	return rpc_success;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * TEST: Check for conflicting lock
1091da177e4SLinus Torvalds  */
1107111c66eSAl Viro static __be32
111a6beb732SChristoph Hellwig __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
1121da177e4SLinus Torvalds {
113a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
1141da177e4SLinus Torvalds 	struct nlm_host	*host;
1151da177e4SLinus Torvalds 	struct nlm_file	*file;
116317602f3SHarvey Harrison 	__be32 rc = rpc_success;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	dprintk("lockd: TEST          called\n");
1191da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/* Obtain client and file */
1221da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
123d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* Now check for conflicting locks */
1268f920d5eSJeff Layton 	resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie));
1275ea0d750SMarc Eshel 	if (resp->status == nlm_drop_reply)
128b7e6b869SOleg Drokin 		rc = rpc_drop_reply;
129b7e6b869SOleg Drokin 	else
1301da177e4SLinus Torvalds 		dprintk("lockd: TEST          status %d vers %d\n",
1311da177e4SLinus Torvalds 			ntohl(resp->status), rqstp->rq_vers);
132b7e6b869SOleg Drokin 
133*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
13467216b94SChuck Lever 	nlmsvc_release_host(host);
1351da177e4SLinus Torvalds 	nlm_release_file(file);
136b7e6b869SOleg Drokin 	return rc;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1397111c66eSAl Viro static __be32
140a6beb732SChristoph Hellwig nlmsvc_proc_test(struct svc_rqst *rqstp)
1411da177e4SLinus Torvalds {
142a6beb732SChristoph Hellwig 	return __nlmsvc_proc_test(rqstp, rqstp->rq_resp);
143a6beb732SChristoph Hellwig }
144a6beb732SChristoph Hellwig 
145a6beb732SChristoph Hellwig static __be32
146a6beb732SChristoph Hellwig __nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_res *resp)
147a6beb732SChristoph Hellwig {
148a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
1491da177e4SLinus Torvalds 	struct nlm_host	*host;
1501da177e4SLinus Torvalds 	struct nlm_file	*file;
151317602f3SHarvey Harrison 	__be32 rc = rpc_success;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	dprintk("lockd: LOCK          called\n");
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	/* Obtain client and file */
1581da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
159d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #if 0
1621da177e4SLinus Torvalds 	/* If supplied state doesn't match current state, we assume it's
1631da177e4SLinus Torvalds 	 * an old request that time-warped somehow. Any error return would
1641da177e4SLinus Torvalds 	 * do in this case because it's irrelevant anyway.
1651da177e4SLinus Torvalds 	 *
1661da177e4SLinus Torvalds 	 * NB: We don't retrieve the remote host's state yet.
1671da177e4SLinus Torvalds 	 */
1681da177e4SLinus Torvalds 	if (host->h_nsmstate && host->h_nsmstate != argp->state) {
1691da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_nolocks;
1701da177e4SLinus Torvalds 	} else
1711da177e4SLinus Torvalds #endif
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	/* Now try to lock the file */
1746cde4de8SJeff Layton 	resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock,
175b2b50289SJ. Bruce Fields 					       argp->block, &argp->cookie,
176b2b50289SJ. Bruce Fields 					       argp->reclaim));
1771a8322b2SMarc Eshel 	if (resp->status == nlm_drop_reply)
178b7e6b869SOleg Drokin 		rc = rpc_drop_reply;
179b7e6b869SOleg Drokin 	else
1801da177e4SLinus Torvalds 		dprintk("lockd: LOCK         status %d\n", ntohl(resp->status));
181b7e6b869SOleg Drokin 
182*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
18367216b94SChuck Lever 	nlmsvc_release_host(host);
1841da177e4SLinus Torvalds 	nlm_release_file(file);
185b7e6b869SOleg Drokin 	return rc;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1887111c66eSAl Viro static __be32
189a6beb732SChristoph Hellwig nlmsvc_proc_lock(struct svc_rqst *rqstp)
1901da177e4SLinus Torvalds {
191a6beb732SChristoph Hellwig 	return __nlmsvc_proc_lock(rqstp, rqstp->rq_resp);
192a6beb732SChristoph Hellwig }
193a6beb732SChristoph Hellwig 
194a6beb732SChristoph Hellwig static __be32
195a6beb732SChristoph Hellwig __nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_res *resp)
196a6beb732SChristoph Hellwig {
197a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
1981da177e4SLinus Torvalds 	struct nlm_host	*host;
1991da177e4SLinus Torvalds 	struct nlm_file	*file;
2005ccb0066SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        called\n");
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
2075ccb0066SStanislav Kinsbursky 	if (locks_in_grace(net)) {
2081da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
2091da177e4SLinus Torvalds 		return rpc_success;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* Obtain client and file */
2131da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
214d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* Try to cancel request. */
2175ccb0066SStanislav Kinsbursky 	resp->status = cast_status(nlmsvc_cancel_blocked(net, file, &argp->lock));
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        status %d\n", ntohl(resp->status));
220*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
22167216b94SChuck Lever 	nlmsvc_release_host(host);
2221da177e4SLinus Torvalds 	nlm_release_file(file);
2231da177e4SLinus Torvalds 	return rpc_success;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226a6beb732SChristoph Hellwig static __be32
227a6beb732SChristoph Hellwig nlmsvc_proc_cancel(struct svc_rqst *rqstp)
228a6beb732SChristoph Hellwig {
229a6beb732SChristoph Hellwig 	return __nlmsvc_proc_cancel(rqstp, rqstp->rq_resp);
230a6beb732SChristoph Hellwig }
231a6beb732SChristoph Hellwig 
2321da177e4SLinus Torvalds /*
2331da177e4SLinus Torvalds  * UNLOCK: release a lock
2341da177e4SLinus Torvalds  */
2357111c66eSAl Viro static __be32
236a6beb732SChristoph Hellwig __nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_res *resp)
2371da177e4SLinus Torvalds {
238a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
2391da177e4SLinus Torvalds 	struct nlm_host	*host;
2401da177e4SLinus Torvalds 	struct nlm_file	*file;
2415ccb0066SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        called\n");
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
2485ccb0066SStanislav Kinsbursky 	if (locks_in_grace(net)) {
2491da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
2501da177e4SLinus Torvalds 		return rpc_success;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	/* Obtain client and file */
2541da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
255d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* Now try to remove the lock */
2585ccb0066SStanislav Kinsbursky 	resp->status = cast_status(nlmsvc_unlock(net, file, &argp->lock));
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        status %d\n", ntohl(resp->status));
261*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
26267216b94SChuck Lever 	nlmsvc_release_host(host);
2631da177e4SLinus Torvalds 	nlm_release_file(file);
2641da177e4SLinus Torvalds 	return rpc_success;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
267a6beb732SChristoph Hellwig static __be32
268a6beb732SChristoph Hellwig nlmsvc_proc_unlock(struct svc_rqst *rqstp)
269a6beb732SChristoph Hellwig {
270a6beb732SChristoph Hellwig 	return __nlmsvc_proc_unlock(rqstp, rqstp->rq_resp);
271a6beb732SChristoph Hellwig }
272a6beb732SChristoph Hellwig 
2731da177e4SLinus Torvalds /*
2741da177e4SLinus Torvalds  * GRANTED: A server calls us to tell that a process' lock request
2751da177e4SLinus Torvalds  * was granted
2761da177e4SLinus Torvalds  */
2777111c66eSAl Viro static __be32
278a6beb732SChristoph Hellwig __nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_res *resp)
2791da177e4SLinus Torvalds {
280a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
281a6beb732SChristoph Hellwig 
2821da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       called\n");
285dcff09f1SChuck Lever 	resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
2861da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       status %d\n", ntohl(resp->status));
2871da177e4SLinus Torvalds 	return rpc_success;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290a6beb732SChristoph Hellwig static __be32
291a6beb732SChristoph Hellwig nlmsvc_proc_granted(struct svc_rqst *rqstp)
292a6beb732SChristoph Hellwig {
293a6beb732SChristoph Hellwig 	return __nlmsvc_proc_granted(rqstp, rqstp->rq_resp);
294a6beb732SChristoph Hellwig }
295a6beb732SChristoph Hellwig 
2961da177e4SLinus Torvalds /*
297d4716624STrond Myklebust  * This is the generic lockd callback for async RPC calls
298d4716624STrond Myklebust  */
299d4716624STrond Myklebust static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
300d4716624STrond Myklebust {
301c041b5ffSChuck Lever 	dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
302d4716624STrond Myklebust 			-task->tk_status);
303d4716624STrond Myklebust }
304d4716624STrond Myklebust 
3057db836d4SChuck Lever void nlmsvc_release_call(struct nlm_rqst *call)
3067db836d4SChuck Lever {
307fbca30c5SElena Reshetova 	if (!refcount_dec_and_test(&call->a_count))
3087db836d4SChuck Lever 		return;
30967216b94SChuck Lever 	nlmsvc_release_host(call->a_host);
3107db836d4SChuck Lever 	kfree(call);
3117db836d4SChuck Lever }
3127db836d4SChuck Lever 
313d4716624STrond Myklebust static void nlmsvc_callback_release(void *data)
314d4716624STrond Myklebust {
3157db836d4SChuck Lever 	nlmsvc_release_call(data);
316d4716624STrond Myklebust }
317d4716624STrond Myklebust 
318d4716624STrond Myklebust static const struct rpc_call_ops nlmsvc_callback_ops = {
319d4716624STrond Myklebust 	.rpc_call_done = nlmsvc_callback_exit,
320d4716624STrond Myklebust 	.rpc_release = nlmsvc_callback_release,
321d4716624STrond Myklebust };
322d4716624STrond Myklebust 
323d4716624STrond Myklebust /*
3241da177e4SLinus Torvalds  * `Async' versions of the above service routines. They aren't really,
3251da177e4SLinus Torvalds  * because we send the callback before the reply proper. I hope this
3261da177e4SLinus Torvalds  * doesn't break any clients.
3271da177e4SLinus Torvalds  */
328a6beb732SChristoph Hellwig static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc,
329a6beb732SChristoph Hellwig 		__be32 (*func)(struct svc_rqst *, struct nlm_res *))
330d4716624STrond Myklebust {
331a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
332d4716624STrond Myklebust 	struct nlm_host	*host;
333d4716624STrond Myklebust 	struct nlm_rqst	*call;
3347111c66eSAl Viro 	__be32 stat;
335d4716624STrond Myklebust 
336db4e4c9aSOlaf Kirch 	host = nlmsvc_lookup_host(rqstp,
337db4e4c9aSOlaf Kirch 				  argp->lock.caller,
338db4e4c9aSOlaf Kirch 				  argp->lock.len);
339d4716624STrond Myklebust 	if (host == NULL)
340d4716624STrond Myklebust 		return rpc_system_err;
341d4716624STrond Myklebust 
342d4716624STrond Myklebust 	call = nlm_alloc_call(host);
343446945abSAl Viro 	nlmsvc_release_host(host);
344d4716624STrond Myklebust 	if (call == NULL)
345d4716624STrond Myklebust 		return rpc_system_err;
346d4716624STrond Myklebust 
347a6beb732SChristoph Hellwig 	stat = func(rqstp, &call->a_res);
348d4716624STrond Myklebust 	if (stat != 0) {
3497db836d4SChuck Lever 		nlmsvc_release_call(call);
350d4716624STrond Myklebust 		return stat;
351d4716624STrond Myklebust 	}
352d4716624STrond Myklebust 
353d4716624STrond Myklebust 	call->a_flags = RPC_TASK_ASYNC;
354d4716624STrond Myklebust 	if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
355d4716624STrond Myklebust 		return rpc_system_err;
356d4716624STrond Myklebust 	return rpc_success;
357d4716624STrond Myklebust }
358d4716624STrond Myklebust 
359a6beb732SChristoph Hellwig static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	dprintk("lockd: TEST_MSG      called\n");
362a6beb732SChristoph Hellwig 	return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, __nlmsvc_proc_test);
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
365a6beb732SChristoph Hellwig static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	dprintk("lockd: LOCK_MSG      called\n");
368a6beb732SChristoph Hellwig 	return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, __nlmsvc_proc_lock);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
371a6beb732SChristoph Hellwig static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	dprintk("lockd: CANCEL_MSG    called\n");
374a6beb732SChristoph Hellwig 	return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, __nlmsvc_proc_cancel);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3777111c66eSAl Viro static __be32
378a6beb732SChristoph Hellwig nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK_MSG    called\n");
381a6beb732SChristoph Hellwig 	return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, __nlmsvc_proc_unlock);
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3847111c66eSAl Viro static __be32
385a6beb732SChristoph Hellwig nlmsvc_proc_granted_msg(struct svc_rqst *rqstp)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	dprintk("lockd: GRANTED_MSG   called\n");
388a6beb732SChristoph Hellwig 	return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, __nlmsvc_proc_granted);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds  * SHARE: create a DOS share or alter existing share.
3931da177e4SLinus Torvalds  */
3947111c66eSAl Viro static __be32
395a6beb732SChristoph Hellwig nlmsvc_proc_share(struct svc_rqst *rqstp)
3961da177e4SLinus Torvalds {
397a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
398a6beb732SChristoph Hellwig 	struct nlm_res *resp = rqstp->rq_resp;
3991da177e4SLinus Torvalds 	struct nlm_host	*host;
4001da177e4SLinus Torvalds 	struct nlm_file	*file;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	dprintk("lockd: SHARE         called\n");
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
4075ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp)) && !argp->reclaim) {
4081da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
4091da177e4SLinus Torvalds 		return rpc_success;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* Obtain client and file */
4131da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
414d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/* Now try to create the share */
4171da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_share_file(host, file, argp));
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	dprintk("lockd: SHARE         status %d\n", ntohl(resp->status));
420*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
42167216b94SChuck Lever 	nlmsvc_release_host(host);
4221da177e4SLinus Torvalds 	nlm_release_file(file);
4231da177e4SLinus Torvalds 	return rpc_success;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * UNSHARE: Release a DOS share.
4281da177e4SLinus Torvalds  */
4297111c66eSAl Viro static __be32
430a6beb732SChristoph Hellwig nlmsvc_proc_unshare(struct svc_rqst *rqstp)
4311da177e4SLinus Torvalds {
432a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
433a6beb732SChristoph Hellwig 	struct nlm_res *resp = rqstp->rq_resp;
4341da177e4SLinus Torvalds 	struct nlm_host	*host;
4351da177e4SLinus Torvalds 	struct nlm_file	*file;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       called\n");
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
4425ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp))) {
4431da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
4441da177e4SLinus Torvalds 		return rpc_success;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	/* Obtain client and file */
4481da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
449d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* Now try to unshare the file */
4521da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       status %d\n", ntohl(resp->status));
455*89e0edfbSBenjamin Coddington 	nlmsvc_release_lockowner(&argp->lock);
45667216b94SChuck Lever 	nlmsvc_release_host(host);
4571da177e4SLinus Torvalds 	nlm_release_file(file);
4581da177e4SLinus Torvalds 	return rpc_success;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /*
4621da177e4SLinus Torvalds  * NM_LOCK: Create an unmonitored lock
4631da177e4SLinus Torvalds  */
4647111c66eSAl Viro static __be32
465a6beb732SChristoph Hellwig nlmsvc_proc_nm_lock(struct svc_rqst *rqstp)
4661da177e4SLinus Torvalds {
467a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
468a6beb732SChristoph Hellwig 
4691da177e4SLinus Torvalds 	dprintk("lockd: NM_LOCK       called\n");
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	argp->monitor = 0;		/* just clean the monitor flag */
472a6beb732SChristoph Hellwig 	return nlmsvc_proc_lock(rqstp);
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds /*
4761da177e4SLinus Torvalds  * FREE_ALL: Release all locks and shares held by client
4771da177e4SLinus Torvalds  */
4787111c66eSAl Viro static __be32
479a6beb732SChristoph Hellwig nlmsvc_proc_free_all(struct svc_rqst *rqstp)
4801da177e4SLinus Torvalds {
481a6beb732SChristoph Hellwig 	struct nlm_args *argp = rqstp->rq_argp;
4821da177e4SLinus Torvalds 	struct nlm_host	*host;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	/* Obtain client */
4851da177e4SLinus Torvalds 	if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
4861da177e4SLinus Torvalds 		return rpc_success;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	nlmsvc_free_host_resources(host);
48967216b94SChuck Lever 	nlmsvc_release_host(host);
4901da177e4SLinus Torvalds 	return rpc_success;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * SM_NOTIFY: private callback from statd (not part of official NLM proto)
4951da177e4SLinus Torvalds  */
4967111c66eSAl Viro static __be32
497a6beb732SChristoph Hellwig nlmsvc_proc_sm_notify(struct svc_rqst *rqstp)
4981da177e4SLinus Torvalds {
499a6beb732SChristoph Hellwig 	struct nlm_reboot *argp = rqstp->rq_argp;
500a6beb732SChristoph Hellwig 
5011da177e4SLinus Torvalds 	dprintk("lockd: SM_NOTIFY     called\n");
502b85e4676SChuck Lever 
503b85e4676SChuck Lever 	if (!nlm_privileged_requester(rqstp)) {
504ad06e4bdSChuck Lever 		char buf[RPC_MAX_ADDRBUFLEN];
505ad06e4bdSChuck Lever 		printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
506ad06e4bdSChuck Lever 				svc_print_addr(rqstp, buf, sizeof(buf)));
5071da177e4SLinus Torvalds 		return rpc_system_err;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5100ad95472SAndrey Ryabinin 	nlm_host_rebooted(SVC_NET(rqstp), argp);
5111da177e4SLinus Torvalds 	return rpc_success;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /*
5151da177e4SLinus Torvalds  * client sent a GRANTED_RES, let's remove the associated block
5161da177e4SLinus Torvalds  */
5177111c66eSAl Viro static __be32
518a6beb732SChristoph Hellwig nlmsvc_proc_granted_res(struct svc_rqst *rqstp)
5191da177e4SLinus Torvalds {
520a6beb732SChristoph Hellwig 	struct nlm_res *argp = rqstp->rq_argp;
521a6beb732SChristoph Hellwig 
5221da177e4SLinus Torvalds 	if (!nlmsvc_ops)
5231da177e4SLinus Torvalds 		return rpc_success;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	dprintk("lockd: GRANTED_RES   called\n");
5261da177e4SLinus Torvalds 
52739be4502SOlaf Kirch 	nlmsvc_grant_reply(&argp->cookie, argp->status);
5281da177e4SLinus Torvalds 	return rpc_success;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds /*
5321da177e4SLinus Torvalds  * NLM Server procedures.
5331da177e4SLinus Torvalds  */
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds #define nlmsvc_encode_norep	nlmsvc_encode_void
5361da177e4SLinus Torvalds #define nlmsvc_decode_norep	nlmsvc_decode_void
5371da177e4SLinus Torvalds #define nlmsvc_decode_testres	nlmsvc_decode_void
5381da177e4SLinus Torvalds #define nlmsvc_decode_lockres	nlmsvc_decode_void
5391da177e4SLinus Torvalds #define nlmsvc_decode_unlockres	nlmsvc_decode_void
5401da177e4SLinus Torvalds #define nlmsvc_decode_cancelres	nlmsvc_decode_void
5411da177e4SLinus Torvalds #define nlmsvc_decode_grantedres	nlmsvc_decode_void
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds #define nlmsvc_proc_none	nlmsvc_proc_null
5441da177e4SLinus Torvalds #define nlmsvc_proc_test_res	nlmsvc_proc_null
5451da177e4SLinus Torvalds #define nlmsvc_proc_lock_res	nlmsvc_proc_null
5461da177e4SLinus Torvalds #define nlmsvc_proc_cancel_res	nlmsvc_proc_null
5471da177e4SLinus Torvalds #define nlmsvc_proc_unlock_res	nlmsvc_proc_null
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds struct nlm_void			{ int dummy; };
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds #define PROC(name, xargt, xrest, argt, rest, respsize)	\
552a6beb732SChristoph Hellwig  { .pc_func	= nlmsvc_proc_##name,			\
553026fec7eSChristoph Hellwig    .pc_decode	= nlmsvc_decode_##xargt,		\
55463f8de37SChristoph Hellwig    .pc_encode	= nlmsvc_encode_##xrest,		\
5551da177e4SLinus Torvalds    .pc_release	= NULL,					\
5561da177e4SLinus Torvalds    .pc_argsize	= sizeof(struct nlm_##argt),		\
5571da177e4SLinus Torvalds    .pc_ressize	= sizeof(struct nlm_##rest),		\
5581da177e4SLinus Torvalds    .pc_xdrressize = respsize,				\
5591da177e4SLinus Torvalds  }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds #define	Ck	(1+XDR_QUADLEN(NLM_MAXCOOKIELEN))	/* cookie */
5621da177e4SLinus Torvalds #define	St	1				/* status */
5631da177e4SLinus Torvalds #define	No	(1+1024/4)			/* Net Obj */
5641da177e4SLinus Torvalds #define	Rg	2				/* range - offset + size */
5651da177e4SLinus Torvalds 
566860bda29SChristoph Hellwig const struct svc_procedure nlmsvc_procedures[] = {
5671da177e4SLinus Torvalds   PROC(null,		void,		void,		void,	void, 1),
5681da177e4SLinus Torvalds   PROC(test,		testargs,	testres,	args,	res, Ck+St+2+No+Rg),
5691da177e4SLinus Torvalds   PROC(lock,		lockargs,	res,		args,	res, Ck+St),
5701da177e4SLinus Torvalds   PROC(cancel,		cancargs,	res,		args,	res, Ck+St),
5711da177e4SLinus Torvalds   PROC(unlock,		unlockargs,	res,		args,	res, Ck+St),
5721da177e4SLinus Torvalds   PROC(granted,		testargs,	res,		args,	res, Ck+St),
5731da177e4SLinus Torvalds   PROC(test_msg,	testargs,	norep,		args,	void, 1),
5741da177e4SLinus Torvalds   PROC(lock_msg,	lockargs,	norep,		args,	void, 1),
5751da177e4SLinus Torvalds   PROC(cancel_msg,	cancargs,	norep,		args,	void, 1),
5761da177e4SLinus Torvalds   PROC(unlock_msg,	unlockargs,	norep,		args,	void, 1),
5771da177e4SLinus Torvalds   PROC(granted_msg,	testargs,	norep,		args,	void, 1),
5781da177e4SLinus Torvalds   PROC(test_res,	testres,	norep,		res,	void, 1),
5791da177e4SLinus Torvalds   PROC(lock_res,	lockres,	norep,		res,	void, 1),
5801da177e4SLinus Torvalds   PROC(cancel_res,	cancelres,	norep,		res,	void, 1),
5811da177e4SLinus Torvalds   PROC(unlock_res,	unlockres,	norep,		res,	void, 1),
5821da177e4SLinus Torvalds   PROC(granted_res,	res,		norep,		res,	void, 1),
5831da177e4SLinus Torvalds   /* statd callback */
5841da177e4SLinus Torvalds   PROC(sm_notify,	reboot,		void,		reboot,	void, 1),
5851da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5861da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5871da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5881da177e4SLinus Torvalds   PROC(share,		shareargs,	shareres,	args,	res, Ck+St+1),
5891da177e4SLinus Torvalds   PROC(unshare,		shareargs,	shareres,	args,	res, Ck+St+1),
5901da177e4SLinus Torvalds   PROC(nm_lock,		lockargs,	res,		args,	res, Ck+St),
5911da177e4SLinus Torvalds   PROC(free_all,	notify,		void,		args,	void, 0),
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds };
594