xref: /openbmc/linux/fs/lockd/svc4proc.c (revision 7111c66e4e70588c9602035a4996c9cdc2087d2d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/lockd/svc4proc.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 /*
251da177e4SLinus Torvalds  * Obtain client and file from arguments
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds static u32
281da177e4SLinus Torvalds nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
291da177e4SLinus Torvalds 			struct nlm_host **hostp, struct nlm_file **filp)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	struct nlm_host		*host = NULL;
321da177e4SLinus Torvalds 	struct nlm_file		*file = NULL;
331da177e4SLinus Torvalds 	struct nlm_lock		*lock = &argp->lock;
341da177e4SLinus Torvalds 	u32			error = 0;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds 	/* nfsd callbacks must have been installed for this procedure */
371da177e4SLinus Torvalds 	if (!nlmsvc_ops)
381da177e4SLinus Torvalds 		return nlm_lck_denied_nolocks;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	/* Obtain host handle */
41db4e4c9aSOlaf Kirch 	if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
42977faf39SOlaf Kirch 	 || (argp->monitor && nsm_monitor(host) < 0))
431da177e4SLinus Torvalds 		goto no_locks;
441da177e4SLinus Torvalds 	*hostp = host;
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds 	/* Obtain file pointer. Not used by FREE_ALL call. */
471da177e4SLinus Torvalds 	if (filp != NULL) {
481da177e4SLinus Torvalds 		if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
491da177e4SLinus Torvalds 			goto no_locks;
501da177e4SLinus Torvalds 		*filp = file;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 		/* Set up the missing parts of the file_lock structure */
531da177e4SLinus Torvalds 		lock->fl.fl_file  = file->f_file;
541da177e4SLinus Torvalds 		lock->fl.fl_owner = (fl_owner_t) host;
551da177e4SLinus Torvalds 		lock->fl.fl_lmops = &nlmsvc_lock_operations;
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	return 0;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds no_locks:
611da177e4SLinus Torvalds 	if (host)
621da177e4SLinus Torvalds 		nlm_release_host(host);
631da177e4SLinus Torvalds  	if (error)
641da177e4SLinus Torvalds 		return error;
651da177e4SLinus Torvalds 	return nlm_lck_denied_nolocks;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  * NULL: Test for presence of service
701da177e4SLinus Torvalds  */
71*7111c66eSAl Viro static __be32
721da177e4SLinus Torvalds nlm4svc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	dprintk("lockd: NULL          called\n");
751da177e4SLinus Torvalds 	return rpc_success;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds  * TEST: Check for conflicting lock
801da177e4SLinus Torvalds  */
81*7111c66eSAl Viro static __be32
821da177e4SLinus Torvalds nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
831da177e4SLinus Torvalds 				         struct nlm_res  *resp)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	struct nlm_host	*host;
861da177e4SLinus Torvalds 	struct nlm_file	*file;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	dprintk("lockd: TEST4        called\n");
891da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	/* Don't accept test requests during grace period */
921da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
931da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
941da177e4SLinus Torvalds 		return rpc_success;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/* Obtain client and file */
981da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
99d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* Now check for conflicting locks */
1021da177e4SLinus Torvalds 	resp->status = nlmsvc_testlock(file, &argp->lock, &resp->lock);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	dprintk("lockd: TEST4          status %d\n", ntohl(resp->status));
1051da177e4SLinus Torvalds 	nlm_release_host(host);
1061da177e4SLinus Torvalds 	nlm_release_file(file);
1071da177e4SLinus Torvalds 	return rpc_success;
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
110*7111c66eSAl Viro static __be32
1111da177e4SLinus Torvalds nlm4svc_proc_lock(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;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	dprintk("lockd: LOCK          called\n");
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
1221da177e4SLinus Torvalds 	if (nlmsvc_grace_period && !argp->reclaim) {
1231da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
1241da177e4SLinus Torvalds 		return rpc_success;
1251da177e4SLinus Torvalds 	}
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* Obtain client and file */
1281da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
129d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #if 0
1321da177e4SLinus Torvalds 	/* If supplied state doesn't match current state, we assume it's
1331da177e4SLinus Torvalds 	 * an old request that time-warped somehow. Any error return would
1341da177e4SLinus Torvalds 	 * do in this case because it's irrelevant anyway.
1351da177e4SLinus Torvalds 	 *
1361da177e4SLinus Torvalds 	 * NB: We don't retrieve the remote host's state yet.
1371da177e4SLinus Torvalds 	 */
1381da177e4SLinus Torvalds 	if (host->h_nsmstate && host->h_nsmstate != argp->state) {
1391da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_nolocks;
1401da177e4SLinus Torvalds 	} else
1411da177e4SLinus Torvalds #endif
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	/* Now try to lock the file */
1441da177e4SLinus Torvalds 	resp->status = nlmsvc_lock(rqstp, file, &argp->lock,
1451da177e4SLinus Torvalds 					argp->block, &argp->cookie);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	dprintk("lockd: LOCK          status %d\n", ntohl(resp->status));
1481da177e4SLinus Torvalds 	nlm_release_host(host);
1491da177e4SLinus Torvalds 	nlm_release_file(file);
1501da177e4SLinus Torvalds 	return rpc_success;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153*7111c66eSAl Viro static __be32
1541da177e4SLinus Torvalds nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
1551da177e4SLinus Torvalds 				           struct nlm_res  *resp)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	struct nlm_host	*host;
1581da177e4SLinus Torvalds 	struct nlm_file	*file;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        called\n");
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
1651da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
1661da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
1671da177e4SLinus Torvalds 		return rpc_success;
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	/* Obtain client and file */
1711da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
172d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/* Try to cancel request. */
1751da177e4SLinus Torvalds 	resp->status = nlmsvc_cancel_blocked(file, &argp->lock);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	dprintk("lockd: CANCEL        status %d\n", ntohl(resp->status));
1781da177e4SLinus Torvalds 	nlm_release_host(host);
1791da177e4SLinus Torvalds 	nlm_release_file(file);
1801da177e4SLinus Torvalds 	return rpc_success;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds  * UNLOCK: release a lock
1851da177e4SLinus Torvalds  */
186*7111c66eSAl Viro static __be32
1871da177e4SLinus Torvalds nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
1881da177e4SLinus Torvalds 				           struct nlm_res  *resp)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct nlm_host	*host;
1911da177e4SLinus Torvalds 	struct nlm_file	*file;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        called\n");
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
1981da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
1991da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
2001da177e4SLinus Torvalds 		return rpc_success;
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	/* Obtain client and file */
2041da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
205d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* Now try to remove the lock */
2081da177e4SLinus Torvalds 	resp->status = nlmsvc_unlock(file, &argp->lock);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK        status %d\n", ntohl(resp->status));
2111da177e4SLinus Torvalds 	nlm_release_host(host);
2121da177e4SLinus Torvalds 	nlm_release_file(file);
2131da177e4SLinus Torvalds 	return rpc_success;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /*
2171da177e4SLinus Torvalds  * GRANTED: A server calls us to tell that a process' lock request
2181da177e4SLinus Torvalds  * was granted
2191da177e4SLinus Torvalds  */
220*7111c66eSAl Viro static __be32
2211da177e4SLinus Torvalds nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
2221da177e4SLinus Torvalds 				            struct nlm_res  *resp)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       called\n");
2275ac5f9d1STrond Myklebust 	resp->status = nlmclnt_grant(&rqstp->rq_addr, &argp->lock);
2281da177e4SLinus Torvalds 	dprintk("lockd: GRANTED       status %d\n", ntohl(resp->status));
2291da177e4SLinus Torvalds 	return rpc_success;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /*
233d4716624STrond Myklebust  * This is the generic lockd callback for async RPC calls
234d4716624STrond Myklebust  */
235d4716624STrond Myklebust static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
236d4716624STrond Myklebust {
237d4716624STrond Myklebust 	dprintk("lockd: %4d callback returned %d\n", task->tk_pid,
238d4716624STrond Myklebust 			-task->tk_status);
239d4716624STrond Myklebust }
240d4716624STrond Myklebust 
241d4716624STrond Myklebust static void nlm4svc_callback_release(void *data)
242d4716624STrond Myklebust {
243d4716624STrond Myklebust 	nlm_release_call(data);
244d4716624STrond Myklebust }
245d4716624STrond Myklebust 
246d4716624STrond Myklebust static const struct rpc_call_ops nlm4svc_callback_ops = {
247d4716624STrond Myklebust 	.rpc_call_done = nlm4svc_callback_exit,
248d4716624STrond Myklebust 	.rpc_release = nlm4svc_callback_release,
249d4716624STrond Myklebust };
250d4716624STrond Myklebust 
251d4716624STrond Myklebust /*
2521da177e4SLinus Torvalds  * `Async' versions of the above service routines. They aren't really,
2531da177e4SLinus Torvalds  * because we send the callback before the reply proper. I hope this
2541da177e4SLinus Torvalds  * doesn't break any clients.
2551da177e4SLinus Torvalds  */
256*7111c66eSAl Viro static __be32 nlm4svc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
257*7111c66eSAl Viro 		__be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res  *))
258d4716624STrond Myklebust {
259d4716624STrond Myklebust 	struct nlm_host	*host;
260d4716624STrond Myklebust 	struct nlm_rqst	*call;
261*7111c66eSAl Viro 	__be32 stat;
262d4716624STrond Myklebust 
263db4e4c9aSOlaf Kirch 	host = nlmsvc_lookup_host(rqstp,
264db4e4c9aSOlaf Kirch 				  argp->lock.caller,
265db4e4c9aSOlaf Kirch 				  argp->lock.len);
266d4716624STrond Myklebust 	if (host == NULL)
267d4716624STrond Myklebust 		return rpc_system_err;
268d4716624STrond Myklebust 
269d4716624STrond Myklebust 	call = nlm_alloc_call(host);
270d4716624STrond Myklebust 	if (call == NULL)
271d4716624STrond Myklebust 		return rpc_system_err;
272d4716624STrond Myklebust 
273d4716624STrond Myklebust 	stat = func(rqstp, argp, &call->a_res);
274d4716624STrond Myklebust 	if (stat != 0) {
275d4716624STrond Myklebust 		nlm_release_call(call);
276d4716624STrond Myklebust 		return stat;
277d4716624STrond Myklebust 	}
278d4716624STrond Myklebust 
279d4716624STrond Myklebust 	call->a_flags = RPC_TASK_ASYNC;
280d4716624STrond Myklebust 	if (nlm_async_reply(call, proc, &nlm4svc_callback_ops) < 0)
281d4716624STrond Myklebust 		return rpc_system_err;
282d4716624STrond Myklebust 	return rpc_success;
283d4716624STrond Myklebust }
284d4716624STrond Myklebust 
285*7111c66eSAl Viro static __be32 nlm4svc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
2861da177e4SLinus Torvalds 					     void	     *resp)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	dprintk("lockd: TEST_MSG      called\n");
289d4716624STrond Myklebust 	return nlm4svc_callback(rqstp, NLMPROC_TEST_RES, argp, nlm4svc_proc_test);
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
292*7111c66eSAl Viro static __be32 nlm4svc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
2931da177e4SLinus Torvalds 					     void	     *resp)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	dprintk("lockd: LOCK_MSG      called\n");
296d4716624STrond Myklebust 	return nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlm4svc_proc_lock);
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
299*7111c66eSAl Viro static __be32 nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3001da177e4SLinus Torvalds 					       void	       *resp)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	dprintk("lockd: CANCEL_MSG    called\n");
303d4716624STrond Myklebust 	return nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlm4svc_proc_cancel);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306*7111c66eSAl Viro static __be32 nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3071da177e4SLinus Torvalds                                                void            *resp)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	dprintk("lockd: UNLOCK_MSG    called\n");
310d4716624STrond Myklebust 	return nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlm4svc_proc_unlock);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
313*7111c66eSAl Viro static __be32 nlm4svc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
3141da177e4SLinus Torvalds                                                 void            *resp)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	dprintk("lockd: GRANTED_MSG   called\n");
317d4716624STrond Myklebust 	return nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlm4svc_proc_granted);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /*
3211da177e4SLinus Torvalds  * SHARE: create a DOS share or alter existing share.
3221da177e4SLinus Torvalds  */
323*7111c66eSAl Viro static __be32
3241da177e4SLinus Torvalds nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
3251da177e4SLinus Torvalds 				          struct nlm_res  *resp)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct nlm_host	*host;
3281da177e4SLinus Torvalds 	struct nlm_file	*file;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	dprintk("lockd: SHARE         called\n");
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/* Don't accept new lock requests during grace period */
3351da177e4SLinus Torvalds 	if (nlmsvc_grace_period && !argp->reclaim) {
3361da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
3371da177e4SLinus Torvalds 		return rpc_success;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	/* Obtain client and file */
3411da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
342d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	/* Now try to create the share */
3451da177e4SLinus Torvalds 	resp->status = nlmsvc_share_file(host, file, argp);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	dprintk("lockd: SHARE         status %d\n", ntohl(resp->status));
3481da177e4SLinus Torvalds 	nlm_release_host(host);
3491da177e4SLinus Torvalds 	nlm_release_file(file);
3501da177e4SLinus Torvalds 	return rpc_success;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds /*
3541da177e4SLinus Torvalds  * UNSHARE: Release a DOS share.
3551da177e4SLinus Torvalds  */
356*7111c66eSAl Viro static __be32
3571da177e4SLinus Torvalds nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
3581da177e4SLinus Torvalds 				            struct nlm_res  *resp)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	struct nlm_host	*host;
3611da177e4SLinus Torvalds 	struct nlm_file	*file;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       called\n");
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	resp->cookie = argp->cookie;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	/* Don't accept requests during grace period */
3681da177e4SLinus Torvalds 	if (nlmsvc_grace_period) {
3691da177e4SLinus Torvalds 		resp->status = nlm_lck_denied_grace_period;
3701da177e4SLinus Torvalds 		return rpc_success;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* Obtain client and file */
3741da177e4SLinus Torvalds 	if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
375d343fce1SNeilBrown 		return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/* Now try to lock the file */
3781da177e4SLinus Torvalds 	resp->status = nlmsvc_unshare_file(host, file, argp);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	dprintk("lockd: UNSHARE       status %d\n", ntohl(resp->status));
3811da177e4SLinus Torvalds 	nlm_release_host(host);
3821da177e4SLinus Torvalds 	nlm_release_file(file);
3831da177e4SLinus Torvalds 	return rpc_success;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /*
3871da177e4SLinus Torvalds  * NM_LOCK: Create an unmonitored lock
3881da177e4SLinus Torvalds  */
389*7111c66eSAl Viro static __be32
3901da177e4SLinus Torvalds nlm4svc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
3911da177e4SLinus Torvalds 				            struct nlm_res  *resp)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	dprintk("lockd: NM_LOCK       called\n");
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	argp->monitor = 0;		/* just clean the monitor flag */
3961da177e4SLinus Torvalds 	return nlm4svc_proc_lock(rqstp, argp, resp);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds /*
4001da177e4SLinus Torvalds  * FREE_ALL: Release all locks and shares held by client
4011da177e4SLinus Torvalds  */
402*7111c66eSAl Viro static __be32
4031da177e4SLinus Torvalds nlm4svc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
4041da177e4SLinus Torvalds 					     void            *resp)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct nlm_host	*host;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/* Obtain client */
4091da177e4SLinus Torvalds 	if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
4101da177e4SLinus Torvalds 		return rpc_success;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	nlmsvc_free_host_resources(host);
4131da177e4SLinus Torvalds 	nlm_release_host(host);
4141da177e4SLinus Torvalds 	return rpc_success;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds /*
4181da177e4SLinus Torvalds  * SM_NOTIFY: private callback from statd (not part of official NLM proto)
4191da177e4SLinus Torvalds  */
420*7111c66eSAl Viro static __be32
4211da177e4SLinus Torvalds nlm4svc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
4221da177e4SLinus Torvalds 					      void	        *resp)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	struct sockaddr_in	saddr = rqstp->rq_addr;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	dprintk("lockd: SM_NOTIFY     called\n");
4271da177e4SLinus Torvalds 	if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
4281da177e4SLinus Torvalds 	 || ntohs(saddr.sin_port) >= 1024) {
4291da177e4SLinus Torvalds 		printk(KERN_WARNING
4301da177e4SLinus Torvalds 			"lockd: rejected NSM callback from %08x:%d\n",
4311da177e4SLinus Torvalds 			ntohl(rqstp->rq_addr.sin_addr.s_addr),
4321da177e4SLinus Torvalds 			ntohs(rqstp->rq_addr.sin_port));
4331da177e4SLinus Torvalds 		return rpc_system_err;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* Obtain the host pointer for this NFS server and try to
4371da177e4SLinus Torvalds 	 * reclaim all locks we hold on this server.
4381da177e4SLinus Torvalds 	 */
439cf712c24SOlaf Kirch 	memset(&saddr, 0, sizeof(saddr));
4401da177e4SLinus Torvalds 	saddr.sin_addr.s_addr = argp->addr;
4415c8dd29cSOlaf Kirch 	nlm_host_rebooted(&saddr, argp->mon, argp->len, argp->state);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	return rpc_success;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds  * client sent a GRANTED_RES, let's remove the associated block
4481da177e4SLinus Torvalds  */
449*7111c66eSAl Viro static __be32
4501da177e4SLinus Torvalds nlm4svc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res  *argp,
4511da177e4SLinus Torvalds                                                 void            *resp)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds         if (!nlmsvc_ops)
4541da177e4SLinus Torvalds                 return rpc_success;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds         dprintk("lockd: GRANTED_RES   called\n");
4571da177e4SLinus Torvalds 
45839be4502SOlaf Kirch         nlmsvc_grant_reply(&argp->cookie, argp->status);
4591da177e4SLinus Torvalds         return rpc_success;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4641da177e4SLinus Torvalds  * NLM Server procedures.
4651da177e4SLinus Torvalds  */
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds #define nlm4svc_encode_norep	nlm4svc_encode_void
4681da177e4SLinus Torvalds #define nlm4svc_decode_norep	nlm4svc_decode_void
4691da177e4SLinus Torvalds #define nlm4svc_decode_testres	nlm4svc_decode_void
4701da177e4SLinus Torvalds #define nlm4svc_decode_lockres	nlm4svc_decode_void
4711da177e4SLinus Torvalds #define nlm4svc_decode_unlockres	nlm4svc_decode_void
4721da177e4SLinus Torvalds #define nlm4svc_decode_cancelres	nlm4svc_decode_void
4731da177e4SLinus Torvalds #define nlm4svc_decode_grantedres	nlm4svc_decode_void
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds #define nlm4svc_proc_none	nlm4svc_proc_null
4761da177e4SLinus Torvalds #define nlm4svc_proc_test_res	nlm4svc_proc_null
4771da177e4SLinus Torvalds #define nlm4svc_proc_lock_res	nlm4svc_proc_null
4781da177e4SLinus Torvalds #define nlm4svc_proc_cancel_res	nlm4svc_proc_null
4791da177e4SLinus Torvalds #define nlm4svc_proc_unlock_res	nlm4svc_proc_null
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds struct nlm_void			{ int dummy; };
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds #define PROC(name, xargt, xrest, argt, rest, respsize)	\
4841da177e4SLinus Torvalds  { .pc_func	= (svc_procfunc) nlm4svc_proc_##name,	\
4851da177e4SLinus Torvalds    .pc_decode	= (kxdrproc_t) nlm4svc_decode_##xargt,	\
4861da177e4SLinus Torvalds    .pc_encode	= (kxdrproc_t) nlm4svc_encode_##xrest,	\
4871da177e4SLinus Torvalds    .pc_release	= NULL,					\
4881da177e4SLinus Torvalds    .pc_argsize	= sizeof(struct nlm_##argt),		\
4891da177e4SLinus Torvalds    .pc_ressize	= sizeof(struct nlm_##rest),		\
4901da177e4SLinus Torvalds    .pc_xdrressize = respsize,				\
4911da177e4SLinus Torvalds  }
4921da177e4SLinus Torvalds #define	Ck	(1+XDR_QUADLEN(NLM_MAXCOOKIELEN))	/* cookie */
4931da177e4SLinus Torvalds #define	No	(1+1024/4)				/* netobj */
4941da177e4SLinus Torvalds #define	St	1					/* status */
4951da177e4SLinus Torvalds #define	Rg	4					/* range (offset + length) */
4961da177e4SLinus Torvalds struct svc_procedure		nlmsvc_procedures4[] = {
4971da177e4SLinus Torvalds   PROC(null,		void,		void,		void,	void, 1),
4981da177e4SLinus Torvalds   PROC(test,		testargs,	testres,	args,	res, Ck+St+2+No+Rg),
4991da177e4SLinus Torvalds   PROC(lock,		lockargs,	res,		args,	res, Ck+St),
5001da177e4SLinus Torvalds   PROC(cancel,		cancargs,	res,		args,	res, Ck+St),
5011da177e4SLinus Torvalds   PROC(unlock,		unlockargs,	res,		args,	res, Ck+St),
5021da177e4SLinus Torvalds   PROC(granted,		testargs,	res,		args,	res, Ck+St),
5031da177e4SLinus Torvalds   PROC(test_msg,	testargs,	norep,		args,	void, 1),
5041da177e4SLinus Torvalds   PROC(lock_msg,	lockargs,	norep,		args,	void, 1),
5051da177e4SLinus Torvalds   PROC(cancel_msg,	cancargs,	norep,		args,	void, 1),
5061da177e4SLinus Torvalds   PROC(unlock_msg,	unlockargs,	norep,		args,	void, 1),
5071da177e4SLinus Torvalds   PROC(granted_msg,	testargs,	norep,		args,	void, 1),
5081da177e4SLinus Torvalds   PROC(test_res,	testres,	norep,		res,	void, 1),
5091da177e4SLinus Torvalds   PROC(lock_res,	lockres,	norep,		res,	void, 1),
5101da177e4SLinus Torvalds   PROC(cancel_res,	cancelres,	norep,		res,	void, 1),
5111da177e4SLinus Torvalds   PROC(unlock_res,	unlockres,	norep,		res,	void, 1),
5121da177e4SLinus Torvalds   PROC(granted_res,	res,		norep,		res,	void, 1),
5131da177e4SLinus Torvalds   /* statd callback */
5141da177e4SLinus Torvalds   PROC(sm_notify,	reboot,		void,		reboot,	void, 1),
5151da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 0),
5161da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 0),
5171da177e4SLinus Torvalds   PROC(none,		void,		void,		void,	void, 0),
5181da177e4SLinus Torvalds   PROC(share,		shareargs,	shareres,	args,	res, Ck+St+1),
5191da177e4SLinus Torvalds   PROC(unshare,		shareargs,	shareres,	args,	res, Ck+St+1),
5201da177e4SLinus Torvalds   PROC(nm_lock,		lockargs,	res,		args,	res, Ck+St),
5211da177e4SLinus Torvalds   PROC(free_all,	notify,		void,		args,	void, 1),
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds };
524