xref: /openbmc/linux/fs/lockd/svcproc.c (revision b7e6b86948df8d08d420558212e09eb449be9bfa)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/lockd/svcproc.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Lockd server procedures. We don't implement the NLM_*_RES
51da177e4SLinus Torvalds  * procedures because we don't use the async procedures.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/in.h>
141da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
151da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
161da177e4SLinus Torvalds #include <linux/nfsd/nfsd.h>
171da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
181da177e4SLinus Torvalds #include <linux/lockd/share.h>
191da177e4SLinus Torvalds #include <linux/lockd/sm_inter.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
2552921e02SAl Viro static __be32
2652921e02SAl Viro cast_to_nlm(__be32 status, u32 vers)
271da177e4SLinus Torvalds {
281da177e4SLinus Torvalds 	/* Note: status is assumed to be in network byte order !!! */
291da177e4SLinus Torvalds 	if (vers != 4){
301da177e4SLinus Torvalds 		switch (status) {
311da177e4SLinus Torvalds 		case nlm_granted:
321da177e4SLinus Torvalds 		case nlm_lck_denied:
331da177e4SLinus Torvalds 		case nlm_lck_denied_nolocks:
341da177e4SLinus Torvalds 		case nlm_lck_blocked:
351da177e4SLinus Torvalds 		case nlm_lck_denied_grace_period:
365ea0d750SMarc Eshel 		case nlm_drop_reply:
371da177e4SLinus Torvalds 			break;
381da177e4SLinus Torvalds 		case nlm4_deadlock:
391da177e4SLinus Torvalds 			status = nlm_lck_denied;
401da177e4SLinus Torvalds 			break;
411da177e4SLinus Torvalds 		default:
421da177e4SLinus Torvalds 			status = nlm_lck_denied_nolocks;
431da177e4SLinus Torvalds 		}
441da177e4SLinus Torvalds 	}
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	return (status);
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds #define	cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
491da177e4SLinus Torvalds #else
501da177e4SLinus Torvalds #define cast_status(status) (status)
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Obtain client and file from arguments
551da177e4SLinus Torvalds  */
5652921e02SAl Viro static __be32
571da177e4SLinus Torvalds nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
581da177e4SLinus Torvalds 			struct nlm_host **hostp, struct nlm_file **filp)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct nlm_host		*host = NULL;
611da177e4SLinus Torvalds 	struct nlm_file		*file = NULL;
621da177e4SLinus Torvalds 	struct nlm_lock		*lock = &argp->lock;
6352921e02SAl Viro 	__be32			error = 0;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	/* nfsd callbacks must have been installed for this procedure */
661da177e4SLinus Torvalds 	if (!nlmsvc_ops)
671da177e4SLinus Torvalds 		return nlm_lck_denied_nolocks;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	/* Obtain host handle */
70db4e4c9aSOlaf Kirch 	if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
71977faf39SOlaf Kirch 	 || (argp->monitor && nsm_monitor(host) < 0))
721da177e4SLinus Torvalds 		goto no_locks;
731da177e4SLinus Torvalds 	*hostp = host;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	/* Obtain file pointer. Not used by FREE_ALL call. */
761da177e4SLinus Torvalds 	if (filp != NULL) {
771da177e4SLinus Torvalds 		if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
781da177e4SLinus Torvalds 			goto no_locks;
791da177e4SLinus Torvalds 		*filp = file;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 		/* Set up the missing parts of the file_lock structure */
821da177e4SLinus Torvalds 		lock->fl.fl_file  = file->f_file;
831da177e4SLinus Torvalds 		lock->fl.fl_owner = (fl_owner_t) host;
841da177e4SLinus Torvalds 		lock->fl.fl_lmops = &nlmsvc_lock_operations;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	return 0;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds no_locks:
901da177e4SLinus Torvalds 	if (host)
911da177e4SLinus Torvalds 		nlm_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
1011da177e4SLinus Torvalds nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
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
1111da177e4SLinus Torvalds nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
1121da177e4SLinus Torvalds 				         struct nlm_res  *resp)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct nlm_host	*host;
1151da177e4SLinus Torvalds 	struct nlm_file	*file;
116*b7e6b869SOleg Drokin 	int rc = rpc_success;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	dprintk("lockd: TEST          called\n");
1191da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/* Don't accept test requests during grace period */
1221da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
1231da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
124*b7e6b869SOleg Drokin 		return rc;
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* Obtain client and file */
1281da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
129d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	/* Now check for conflicting locks */
13285f3f1b3SMarc Eshel 	resp->status = cast_status(nlmsvc_testlock(rqstp, file, &argp->lock, &resp->lock, &resp->cookie));
1335ea0d750SMarc Eshel 	if (resp->status == nlm_drop_reply)
134*b7e6b869SOleg Drokin 		rc = rpc_drop_reply;
135*b7e6b869SOleg Drokin 	else
1361da177e4SLinus Torvalds 		dprintk("lockd: TEST          status %d vers %d\n",
1371da177e4SLinus Torvalds 			ntohl(resp->status), rqstp->rq_vers);
138*b7e6b869SOleg Drokin 
1391da177e4SLinus Torvalds 	nlm_release_host(host);
1401da177e4SLinus Torvalds 	nlm_release_file(file);
141*b7e6b869SOleg Drokin 	return rc;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1447111c66eSAl Viro static __be32
1451da177e4SLinus Torvalds nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
1461da177e4SLinus Torvalds 				         struct nlm_res  *resp)
1471da177e4SLinus Torvalds {
1481da177e4SLinus Torvalds 	struct nlm_host	*host;
1491da177e4SLinus Torvalds 	struct nlm_file	*file;
150*b7e6b869SOleg Drokin 	int rc = rpc_success;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	dprintk("lockd: LOCK          called\n");
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
1571da177e4SLinus Torvalds 	if (nlmsvc_grace_period && !argp->reclaim) {
1581da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
159*b7e6b869SOleg Drokin 		return rc;
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/* Obtain client and file */
1631da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
164d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #if 0
1671da177e4SLinus Torvalds 	/* If supplied state doesn't match current state, we assume it's
1681da177e4SLinus Torvalds 	 * an old request that time-warped somehow. Any error return would
1691da177e4SLinus Torvalds 	 * do in this case because it's irrelevant anyway.
1701da177e4SLinus Torvalds 	 *
1711da177e4SLinus Torvalds 	 * NB: We don't retrieve the remote host's state yet.
1721da177e4SLinus Torvalds 	 */
1731da177e4SLinus Torvalds 	if (host->h_nsmstate && host->h_nsmstate != argp->state) {
1741da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_nolocks;
1751da177e4SLinus Torvalds 	} else
1761da177e4SLinus Torvalds #endif
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	/* Now try to lock the file */
1791da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock,
1801da177e4SLinus Torvalds 					       argp->block, &argp->cookie));
1811a8322b2SMarc Eshel 	if (resp->status == nlm_drop_reply)
182*b7e6b869SOleg Drokin 		rc = rpc_drop_reply;
183*b7e6b869SOleg Drokin 	else
1841da177e4SLinus Torvalds 		dprintk("lockd: LOCK         status %d\n", ntohl(resp->status));
185*b7e6b869SOleg Drokin 
1861da177e4SLinus Torvalds 	nlm_release_host(host);
1871da177e4SLinus Torvalds 	nlm_release_file(file);
188*b7e6b869SOleg Drokin 	return rc;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1917111c66eSAl Viro static __be32
1921da177e4SLinus Torvalds nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
1931da177e4SLinus Torvalds 				           struct nlm_res  *resp)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	struct nlm_host	*host;
1961da177e4SLinus Torvalds 	struct nlm_file	*file;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        called\n");
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
2031da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
2041da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
2051da177e4SLinus Torvalds 		return rpc_success;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* Obtain client and file */
2091da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
210d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* Try to cancel request. */
2131da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        status %d\n", ntohl(resp->status));
2161da177e4SLinus Torvalds 	nlm_release_host(host);
2171da177e4SLinus Torvalds 	nlm_release_file(file);
2181da177e4SLinus Torvalds 	return rpc_success;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /*
2221da177e4SLinus Torvalds  * UNLOCK: release a lock
2231da177e4SLinus Torvalds  */
2247111c66eSAl Viro static __be32
2251da177e4SLinus Torvalds nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
2261da177e4SLinus Torvalds 				           struct nlm_res  *resp)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct nlm_host	*host;
2291da177e4SLinus Torvalds 	struct nlm_file	*file;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        called\n");
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
2361da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
2371da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
2381da177e4SLinus Torvalds 		return rpc_success;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* Obtain client and file */
2421da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
243d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	/* Now try to remove the lock */
2461da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        status %d\n", ntohl(resp->status));
2491da177e4SLinus Torvalds 	nlm_release_host(host);
2501da177e4SLinus Torvalds 	nlm_release_file(file);
2511da177e4SLinus Torvalds 	return rpc_success;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * GRANTED: A server calls us to tell that a process' lock request
2561da177e4SLinus Torvalds  * was granted
2571da177e4SLinus Torvalds  */
2587111c66eSAl Viro static __be32
2591da177e4SLinus Torvalds nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
2601da177e4SLinus Torvalds 				            struct nlm_res  *resp)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       called\n");
26527459f09SChuck Lever 	resp->status = nlmclnt_grant(svc_addr_in(rqstp), &argp->lock);
2661da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       status %d\n", ntohl(resp->status));
2671da177e4SLinus Torvalds 	return rpc_success;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
271d4716624STrond Myklebust  * This is the generic lockd callback for async RPC calls
272d4716624STrond Myklebust  */
273d4716624STrond Myklebust static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
274d4716624STrond Myklebust {
275c041b5ffSChuck Lever 	dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
276d4716624STrond Myklebust 			-task->tk_status);
277d4716624STrond Myklebust }
278d4716624STrond Myklebust 
279d4716624STrond Myklebust static void nlmsvc_callback_release(void *data)
280d4716624STrond Myklebust {
281d4716624STrond Myklebust 	nlm_release_call(data);
282d4716624STrond Myklebust }
283d4716624STrond Myklebust 
284d4716624STrond Myklebust static const struct rpc_call_ops nlmsvc_callback_ops = {
285d4716624STrond Myklebust 	.rpc_call_done = nlmsvc_callback_exit,
286d4716624STrond Myklebust 	.rpc_release = nlmsvc_callback_release,
287d4716624STrond Myklebust };
288d4716624STrond Myklebust 
289d4716624STrond Myklebust /*
2901da177e4SLinus Torvalds  * `Async' versions of the above service routines. They aren't really,
2911da177e4SLinus Torvalds  * because we send the callback before the reply proper. I hope this
2921da177e4SLinus Torvalds  * doesn't break any clients.
2931da177e4SLinus Torvalds  */
2947111c66eSAl Viro static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
2957111c66eSAl Viro 		__be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res  *))
296d4716624STrond Myklebust {
297d4716624STrond Myklebust 	struct nlm_host	*host;
298d4716624STrond Myklebust 	struct nlm_rqst	*call;
2997111c66eSAl Viro 	__be32 stat;
300d4716624STrond Myklebust 
301db4e4c9aSOlaf Kirch 	host = nlmsvc_lookup_host(rqstp,
302db4e4c9aSOlaf Kirch 				  argp->lock.caller,
303db4e4c9aSOlaf Kirch 				  argp->lock.len);
304d4716624STrond Myklebust 	if (host == NULL)
305d4716624STrond Myklebust 		return rpc_system_err;
306d4716624STrond Myklebust 
307d4716624STrond Myklebust 	call = nlm_alloc_call(host);
308d4716624STrond Myklebust 	if (call == NULL)
309d4716624STrond Myklebust 		return rpc_system_err;
310d4716624STrond Myklebust 
311d4716624STrond Myklebust 	stat = func(rqstp, argp, &call->a_res);
312d4716624STrond Myklebust 	if (stat != 0) {
313d4716624STrond Myklebust 		nlm_release_call(call);
314d4716624STrond Myklebust 		return stat;
315d4716624STrond Myklebust 	}
316d4716624STrond Myklebust 
317d4716624STrond Myklebust 	call->a_flags = RPC_TASK_ASYNC;
318d4716624STrond Myklebust 	if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
319d4716624STrond Myklebust 		return rpc_system_err;
320d4716624STrond Myklebust 	return rpc_success;
321d4716624STrond Myklebust }
322d4716624STrond Myklebust 
3237111c66eSAl Viro static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3241da177e4SLinus Torvalds 					     void	     *resp)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	dprintk("lockd: TEST_MSG      called\n");
327d4716624STrond Myklebust 	return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, argp, nlmsvc_proc_test);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3307111c66eSAl Viro static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3311da177e4SLinus Torvalds 					     void	     *resp)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	dprintk("lockd: LOCK_MSG      called\n");
334d4716624STrond Myklebust 	return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlmsvc_proc_lock);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3377111c66eSAl Viro static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3381da177e4SLinus Torvalds 					       void	       *resp)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	dprintk("lockd: CANCEL_MSG    called\n");
341d4716624STrond Myklebust 	return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlmsvc_proc_cancel);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3447111c66eSAl Viro static __be32
3451da177e4SLinus Torvalds nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3461da177e4SLinus Torvalds                                                void            *resp)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK_MSG    called\n");
349d4716624STrond Myklebust 	return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlmsvc_proc_unlock);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3527111c66eSAl Viro static __be32
3531da177e4SLinus Torvalds nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3541da177e4SLinus Torvalds                                                 void            *resp)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	dprintk("lockd: GRANTED_MSG   called\n");
357d4716624STrond Myklebust 	return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlmsvc_proc_granted);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds  * SHARE: create a DOS share or alter existing share.
3621da177e4SLinus Torvalds  */
3637111c66eSAl Viro static __be32
3641da177e4SLinus Torvalds nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
3651da177e4SLinus Torvalds 				          struct nlm_res  *resp)
3661da177e4SLinus Torvalds {
3671da177e4SLinus Torvalds 	struct nlm_host	*host;
3681da177e4SLinus Torvalds 	struct nlm_file	*file;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	dprintk("lockd: SHARE         called\n");
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
3751da177e4SLinus Torvalds 	if (nlmsvc_grace_period && !argp->reclaim) {
3761da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
3771da177e4SLinus Torvalds 		return rpc_success;
3781da177e4SLinus Torvalds 	}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Obtain client and file */
3811da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
382d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Now try to create the share */
3851da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_share_file(host, file, argp));
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	dprintk("lockd: SHARE         status %d\n", ntohl(resp->status));
3881da177e4SLinus Torvalds 	nlm_release_host(host);
3891da177e4SLinus Torvalds 	nlm_release_file(file);
3901da177e4SLinus Torvalds 	return rpc_success;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  * UNSHARE: Release a DOS share.
3951da177e4SLinus Torvalds  */
3967111c66eSAl Viro static __be32
3971da177e4SLinus Torvalds nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
3981da177e4SLinus Torvalds 				            struct nlm_res  *resp)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	struct nlm_host	*host;
4011da177e4SLinus Torvalds 	struct nlm_file	*file;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       called\n");
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
4081da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
4091da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
4101da177e4SLinus Torvalds 		return rpc_success;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	/* Obtain client and file */
4141da177e4SLinus Torvalds 	if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
415d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/* Now try to unshare the file */
4181da177e4SLinus Torvalds 	resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       status %d\n", ntohl(resp->status));
4211da177e4SLinus Torvalds 	nlm_release_host(host);
4221da177e4SLinus Torvalds 	nlm_release_file(file);
4231da177e4SLinus Torvalds 	return rpc_success;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  * NM_LOCK: Create an unmonitored lock
4281da177e4SLinus Torvalds  */
4297111c66eSAl Viro static __be32
4301da177e4SLinus Torvalds nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
4311da177e4SLinus Torvalds 				            struct nlm_res  *resp)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	dprintk("lockd: NM_LOCK       called\n");
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	argp->monitor = 0;		/* just clean the monitor flag */
4361da177e4SLinus Torvalds 	return nlmsvc_proc_lock(rqstp, argp, resp);
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds /*
4401da177e4SLinus Torvalds  * FREE_ALL: Release all locks and shares held by client
4411da177e4SLinus Torvalds  */
4427111c66eSAl Viro static __be32
4431da177e4SLinus Torvalds nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
4441da177e4SLinus Torvalds 					     void            *resp)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct nlm_host	*host;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	/* Obtain client */
4491da177e4SLinus Torvalds 	if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
4501da177e4SLinus Torvalds 		return rpc_success;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	nlmsvc_free_host_resources(host);
4531da177e4SLinus Torvalds 	nlm_release_host(host);
4541da177e4SLinus Torvalds 	return rpc_success;
4551da177e4SLinus Torvalds }
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  * SM_NOTIFY: private callback from statd (not part of official NLM proto)
4591da177e4SLinus Torvalds  */
4607111c66eSAl Viro static __be32
4611da177e4SLinus Torvalds nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
4621da177e4SLinus Torvalds 					      void	        *resp)
4631da177e4SLinus Torvalds {
46427459f09SChuck Lever 	struct sockaddr_in	saddr;
46527459f09SChuck Lever 
46627459f09SChuck Lever 	memcpy(&saddr, svc_addr_in(rqstp), sizeof(saddr));
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	dprintk("lockd: SM_NOTIFY     called\n");
4691da177e4SLinus Torvalds 	if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
4701da177e4SLinus Torvalds 	 || ntohs(saddr.sin_port) >= 1024) {
471ad06e4bdSChuck Lever 		char buf[RPC_MAX_ADDRBUFLEN];
472ad06e4bdSChuck Lever 		printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
473ad06e4bdSChuck Lever 				svc_print_addr(rqstp, buf, sizeof(buf)));
4741da177e4SLinus Torvalds 		return rpc_system_err;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* Obtain the host pointer for this NFS server and try to
4781da177e4SLinus Torvalds 	 * reclaim all locks we hold on this server.
4791da177e4SLinus Torvalds 	 */
480cf712c24SOlaf Kirch 	memset(&saddr, 0, sizeof(saddr));
4811da177e4SLinus Torvalds 	saddr.sin_addr.s_addr = argp->addr;
4825c8dd29cSOlaf Kirch 	nlm_host_rebooted(&saddr, argp->mon, argp->len, argp->state);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	return rpc_success;
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /*
4881da177e4SLinus Torvalds  * client sent a GRANTED_RES, let's remove the associated block
4891da177e4SLinus Torvalds  */
4907111c66eSAl Viro static __be32
4911da177e4SLinus Torvalds nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res  *argp,
4921da177e4SLinus Torvalds                                                 void            *resp)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	if (!nlmsvc_ops)
4951da177e4SLinus Torvalds 		return rpc_success;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	dprintk("lockd: GRANTED_RES   called\n");
4981da177e4SLinus Torvalds 
49939be4502SOlaf Kirch 	nlmsvc_grant_reply(&argp->cookie, argp->status);
5001da177e4SLinus Torvalds 	return rpc_success;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds /*
5041da177e4SLinus Torvalds  * NLM Server procedures.
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds #define nlmsvc_encode_norep	nlmsvc_encode_void
5081da177e4SLinus Torvalds #define nlmsvc_decode_norep	nlmsvc_decode_void
5091da177e4SLinus Torvalds #define nlmsvc_decode_testres	nlmsvc_decode_void
5101da177e4SLinus Torvalds #define nlmsvc_decode_lockres	nlmsvc_decode_void
5111da177e4SLinus Torvalds #define nlmsvc_decode_unlockres	nlmsvc_decode_void
5121da177e4SLinus Torvalds #define nlmsvc_decode_cancelres	nlmsvc_decode_void
5131da177e4SLinus Torvalds #define nlmsvc_decode_grantedres	nlmsvc_decode_void
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds #define nlmsvc_proc_none	nlmsvc_proc_null
5161da177e4SLinus Torvalds #define nlmsvc_proc_test_res	nlmsvc_proc_null
5171da177e4SLinus Torvalds #define nlmsvc_proc_lock_res	nlmsvc_proc_null
5181da177e4SLinus Torvalds #define nlmsvc_proc_cancel_res	nlmsvc_proc_null
5191da177e4SLinus Torvalds #define nlmsvc_proc_unlock_res	nlmsvc_proc_null
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds struct nlm_void			{ int dummy; };
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds #define PROC(name, xargt, xrest, argt, rest, respsize)	\
5241da177e4SLinus Torvalds  { .pc_func	= (svc_procfunc) nlmsvc_proc_##name,	\
5251da177e4SLinus Torvalds    .pc_decode	= (kxdrproc_t) nlmsvc_decode_##xargt,	\
5261da177e4SLinus Torvalds    .pc_encode	= (kxdrproc_t) nlmsvc_encode_##xrest,	\
5271da177e4SLinus Torvalds    .pc_release	= NULL,					\
5281da177e4SLinus Torvalds    .pc_argsize	= sizeof(struct nlm_##argt),		\
5291da177e4SLinus Torvalds    .pc_ressize	= sizeof(struct nlm_##rest),		\
5301da177e4SLinus Torvalds    .pc_xdrressize = respsize,				\
5311da177e4SLinus Torvalds  }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds #define	Ck	(1+XDR_QUADLEN(NLM_MAXCOOKIELEN))	/* cookie */
5341da177e4SLinus Torvalds #define	St	1				/* status */
5351da177e4SLinus Torvalds #define	No	(1+1024/4)			/* Net Obj */
5361da177e4SLinus Torvalds #define	Rg	2				/* range - offset + size */
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds struct svc_procedure		nlmsvc_procedures[] = {
5391da177e4SLinus Torvalds   PROC(null,		void,		void,		void,	void, 1),
5401da177e4SLinus Torvalds   PROC(test,		testargs,	testres,	args,	res, Ck+St+2+No+Rg),
5411da177e4SLinus Torvalds   PROC(lock,		lockargs,	res,		args,	res, Ck+St),
5421da177e4SLinus Torvalds   PROC(cancel,		cancargs,	res,		args,	res, Ck+St),
5431da177e4SLinus Torvalds   PROC(unlock,		unlockargs,	res,		args,	res, Ck+St),
5441da177e4SLinus Torvalds   PROC(granted,		testargs,	res,		args,	res, Ck+St),
5451da177e4SLinus Torvalds   PROC(test_msg,	testargs,	norep,		args,	void, 1),
5461da177e4SLinus Torvalds   PROC(lock_msg,	lockargs,	norep,		args,	void, 1),
5471da177e4SLinus Torvalds   PROC(cancel_msg,	cancargs,	norep,		args,	void, 1),
5481da177e4SLinus Torvalds   PROC(unlock_msg,	unlockargs,	norep,		args,	void, 1),
5491da177e4SLinus Torvalds   PROC(granted_msg,	testargs,	norep,		args,	void, 1),
5501da177e4SLinus Torvalds   PROC(test_res,	testres,	norep,		res,	void, 1),
5511da177e4SLinus Torvalds   PROC(lock_res,	lockres,	norep,		res,	void, 1),
5521da177e4SLinus Torvalds   PROC(cancel_res,	cancelres,	norep,		res,	void, 1),
5531da177e4SLinus Torvalds   PROC(unlock_res,	unlockres,	norep,		res,	void, 1),
5541da177e4SLinus Torvalds   PROC(granted_res,	res,		norep,		res,	void, 1),
5551da177e4SLinus Torvalds   /* statd callback */
5561da177e4SLinus Torvalds   PROC(sm_notify,	reboot,		void,		reboot,	void, 1),
5571da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5581da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5591da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 1),
5601da177e4SLinus Torvalds   PROC(share,		shareargs,	shareres,	args,	res, Ck+St+1),
5611da177e4SLinus Torvalds   PROC(unshare,		shareargs,	shareres,	args,	res, Ck+St+1),
5621da177e4SLinus Torvalds   PROC(nm_lock,		lockargs,	res,		args,	res, Ck+St),
5631da177e4SLinus Torvalds   PROC(free_all,	notify,		void,		args,	void, 0),
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds };
566