xref: /openbmc/linux/fs/lockd/clntproc.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/lockd/clntproc.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * RPC procedures for the client side NLM implementation
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/types.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
141da177e4SLinus Torvalds #include <linux/fs.h>
155970e15dSJeff Layton #include <linux/filelock.h>
161da177e4SLinus Torvalds #include <linux/nfs_fs.h>
171da177e4SLinus Torvalds #include <linux/utsname.h>
187dfb7103SNigel Cunningham #include <linux/freezer.h>
191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
201da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
211da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
221da177e4SLinus Torvalds 
23*2f90e18fSJeff Layton #include "trace.h"
24*2f90e18fSJeff Layton 
251da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
261da177e4SLinus Torvalds #define NLMCLNT_GRACE_WAIT	(5*HZ)
27ecdbf769STrond Myklebust #define NLMCLNT_POLL_TIMEOUT	(30*HZ)
28aaaa9942STrond Myklebust #define NLMCLNT_MAX_RETRIES	3
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int	nlmclnt_test(struct nlm_rqst *, struct file_lock *);
311da177e4SLinus Torvalds static int	nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
321da177e4SLinus Torvalds static int	nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
33e8c5c045SAl Viro static int	nlm_stat_to_errno(__be32 stat);
341da177e4SLinus Torvalds static void	nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
3516fb2425STrond Myklebust static int	nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
361da177e4SLinus Torvalds 
37963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops;
38963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops;
39963d8fe5STrond Myklebust 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * Cookie counter for NLM requests
421da177e4SLinus Torvalds  */
43031d869dSOlaf Kirch static atomic_t	nlm_cookie = ATOMIC_INIT(0x1234);
441da177e4SLinus Torvalds 
nlmclnt_next_cookie(struct nlm_cookie * c)45031d869dSOlaf Kirch void nlmclnt_next_cookie(struct nlm_cookie *c)
461da177e4SLinus Torvalds {
47031d869dSOlaf Kirch 	u32	cookie = atomic_inc_return(&nlm_cookie);
48031d869dSOlaf Kirch 
49031d869dSOlaf Kirch 	memcpy(c->data, &cookie, 4);
501da177e4SLinus Torvalds 	c->len=4;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
539de3ec1dSBenjamin Coddington static struct nlm_lockowner *
nlmclnt_get_lockowner(struct nlm_lockowner * lockowner)549de3ec1dSBenjamin Coddington nlmclnt_get_lockowner(struct nlm_lockowner *lockowner)
551da177e4SLinus Torvalds {
56431f125bSElena Reshetova 	refcount_inc(&lockowner->count);
571da177e4SLinus Torvalds 	return lockowner;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
nlmclnt_put_lockowner(struct nlm_lockowner * lockowner)60291adeb2SYueHaibing static void nlmclnt_put_lockowner(struct nlm_lockowner *lockowner)
611da177e4SLinus Torvalds {
62431f125bSElena Reshetova 	if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
631da177e4SLinus Torvalds 		return;
641da177e4SLinus Torvalds 	list_del(&lockowner->list);
651da177e4SLinus Torvalds 	spin_unlock(&lockowner->host->h_lock);
668ea6ecc8SChuck Lever 	nlmclnt_release_host(lockowner->host);
671da177e4SLinus Torvalds 	kfree(lockowner);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
nlm_pidbusy(struct nlm_host * host,uint32_t pid)701da177e4SLinus Torvalds static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
731da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
741da177e4SLinus Torvalds 		if (lockowner->pid == pid)
751da177e4SLinus Torvalds 			return -EBUSY;
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 	return 0;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
__nlm_alloc_pid(struct nlm_host * host)801da177e4SLinus Torvalds static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	uint32_t res;
831da177e4SLinus Torvalds 	do {
841da177e4SLinus Torvalds 		res = host->h_pidcount++;
851da177e4SLinus Torvalds 	} while (nlm_pidbusy(host, res) < 0);
861da177e4SLinus Torvalds 	return res;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
__nlmclnt_find_lockowner(struct nlm_host * host,fl_owner_t owner)899de3ec1dSBenjamin Coddington static struct nlm_lockowner *__nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
921da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
931da177e4SLinus Torvalds 		if (lockowner->owner != owner)
941da177e4SLinus Torvalds 			continue;
959de3ec1dSBenjamin Coddington 		return nlmclnt_get_lockowner(lockowner);
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 	return NULL;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
nlmclnt_find_lockowner(struct nlm_host * host,fl_owner_t owner)1009de3ec1dSBenjamin Coddington static struct nlm_lockowner *nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct nlm_lockowner *res, *new = NULL;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	spin_lock(&host->h_lock);
1059de3ec1dSBenjamin Coddington 	res = __nlmclnt_find_lockowner(host, owner);
1061da177e4SLinus Torvalds 	if (res == NULL) {
1071da177e4SLinus Torvalds 		spin_unlock(&host->h_lock);
108f52720caSPanagiotis Issaris 		new = kmalloc(sizeof(*new), GFP_KERNEL);
1091da177e4SLinus Torvalds 		spin_lock(&host->h_lock);
1109de3ec1dSBenjamin Coddington 		res = __nlmclnt_find_lockowner(host, owner);
1111da177e4SLinus Torvalds 		if (res == NULL && new != NULL) {
1121da177e4SLinus Torvalds 			res = new;
113431f125bSElena Reshetova 			refcount_set(&new->count, 1);
1141da177e4SLinus Torvalds 			new->owner = owner;
1151da177e4SLinus Torvalds 			new->pid = __nlm_alloc_pid(host);
1161da177e4SLinus Torvalds 			new->host = nlm_get_host(host);
1171da177e4SLinus Torvalds 			list_add(&new->list, &host->h_lockowners);
1181da177e4SLinus Torvalds 			new = NULL;
1191da177e4SLinus Torvalds 		}
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 	spin_unlock(&host->h_lock);
1221da177e4SLinus Torvalds 	kfree(new);
1231da177e4SLinus Torvalds 	return res;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /*
1271da177e4SLinus Torvalds  * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
1281da177e4SLinus Torvalds  */
nlmclnt_setlockargs(struct nlm_rqst * req,struct file_lock * fl)1291da177e4SLinus Torvalds static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
1321da177e4SLinus Torvalds 	struct nlm_lock	*lock = &argp->lock;
1339a1b6bf8STrond Myklebust 	char *nodename = req->a_host->h_rpcclnt->cl_nodename;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	nlmclnt_next_cookie(&argp->cookie);
136c65454a9SJeff Layton 	memcpy(&lock->fh, NFS_FH(file_inode(fl->fl_file)), sizeof(struct nfs_fh));
1379a1b6bf8STrond Myklebust 	lock->caller  = nodename;
1381da177e4SLinus Torvalds 	lock->oh.data = req->a_owner;
1397bab377fSTrond Myklebust 	lock->oh.len  = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
1407bab377fSTrond Myklebust 				(unsigned int)fl->fl_u.nfs_fl.owner->pid,
1419a1b6bf8STrond Myklebust 				nodename);
1427bab377fSTrond Myklebust 	lock->svid = fl->fl_u.nfs_fl.owner->pid;
1433a649b88STrond Myklebust 	lock->fl.fl_start = fl->fl_start;
1443a649b88STrond Myklebust 	lock->fl.fl_end = fl->fl_end;
1453a649b88STrond Myklebust 	lock->fl.fl_type = fl->fl_type;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
nlmclnt_release_lockargs(struct nlm_rqst * req)1481da177e4SLinus Torvalds static void nlmclnt_release_lockargs(struct nlm_rqst *req)
1491da177e4SLinus Torvalds {
15026269348STrond Myklebust 	WARN_ON_ONCE(req->a_args.lock.fl.fl_ops != NULL);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531093a60eSChuck Lever /**
1541093a60eSChuck Lever  * nlmclnt_proc - Perform a single client-side lock request
1551093a60eSChuck Lever  * @host: address of a valid nlm_host context representing the NLM server
1561093a60eSChuck Lever  * @cmd: fcntl-style file lock operation to perform
1571093a60eSChuck Lever  * @fl: address of arguments for the lock operation
158b1ece737SBenjamin Coddington  * @data: address of data to be sent to callback operations
1591093a60eSChuck Lever  *
1601da177e4SLinus Torvalds  */
nlmclnt_proc(struct nlm_host * host,int cmd,struct file_lock * fl,void * data)161b1ece737SBenjamin Coddington int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl, void *data)
1621da177e4SLinus Torvalds {
16392737230STrond Myklebust 	struct nlm_rqst		*call;
1641093a60eSChuck Lever 	int			status;
165b1ece737SBenjamin Coddington 	const struct nlmclnt_operations *nlmclnt_ops = host->h_nlmclnt_ops;
1661da177e4SLinus Torvalds 
16792737230STrond Myklebust 	call = nlm_alloc_call(host);
16892737230STrond Myklebust 	if (call == NULL)
16992737230STrond Myklebust 		return -ENOMEM;
1701da177e4SLinus Torvalds 
171b1ece737SBenjamin Coddington 	if (nlmclnt_ops && nlmclnt_ops->nlmclnt_alloc_call)
172b1ece737SBenjamin Coddington 		nlmclnt_ops->nlmclnt_alloc_call(data);
173b1ece737SBenjamin Coddington 
17492737230STrond Myklebust 	nlmclnt_locks_init_private(fl, host);
175bf884891SAl Viro 	if (!fl->fl_u.nfs_fl.owner) {
176bf884891SAl Viro 		/* lockowner allocation has failed */
177bf884891SAl Viro 		nlmclnt_release_call(call);
178bf884891SAl Viro 		return -ENOMEM;
179bf884891SAl Viro 	}
18092737230STrond Myklebust 	/* Set up the argument struct */
18192737230STrond Myklebust 	nlmclnt_setlockargs(call, fl);
182b1ece737SBenjamin Coddington 	call->a_callback_data = data;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
1851da177e4SLinus Torvalds 		if (fl->fl_type != F_UNLCK) {
1861da177e4SLinus Torvalds 			call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
1871da177e4SLinus Torvalds 			status = nlmclnt_lock(call, fl);
1881da177e4SLinus Torvalds 		} else
1891da177e4SLinus Torvalds 			status = nlmclnt_unlock(call, fl);
1901da177e4SLinus Torvalds 	} else if (IS_GETLK(cmd))
1911da177e4SLinus Torvalds 		status = nlmclnt_test(call, fl);
1921da177e4SLinus Torvalds 	else
1931da177e4SLinus Torvalds 		status = -EINVAL;
19492737230STrond Myklebust 	fl->fl_ops->fl_release_private(fl);
19592737230STrond Myklebust 	fl->fl_ops = NULL;
19692737230STrond Myklebust 
1971da177e4SLinus Torvalds 	dprintk("lockd: clnt proc returns %d\n", status);
1981da177e4SLinus Torvalds 	return status;
1991da177e4SLinus Torvalds }
2001093a60eSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_proc);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds  * Allocate an NLM RPC call struct
2041da177e4SLinus Torvalds  */
nlm_alloc_call(struct nlm_host * host)20592737230STrond Myklebust struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	struct nlm_rqst	*call;
2081da177e4SLinus Torvalds 
20936943fa4STrond Myklebust 	for(;;) {
21036943fa4STrond Myklebust 		call = kzalloc(sizeof(*call), GFP_KERNEL);
21136943fa4STrond Myklebust 		if (call != NULL) {
212fbca30c5SElena Reshetova 			refcount_set(&call->a_count, 1);
2131da177e4SLinus Torvalds 			locks_init_lock(&call->a_args.lock.fl);
2141da177e4SLinus Torvalds 			locks_init_lock(&call->a_res.lock.fl);
215446945abSAl Viro 			call->a_host = nlm_get_host(host);
2161da177e4SLinus Torvalds 			return call;
2171da177e4SLinus Torvalds 		}
21836943fa4STrond Myklebust 		if (signalled())
21936943fa4STrond Myklebust 			break;
22092737230STrond Myklebust 		printk("nlm_alloc_call: failed, waiting for memory\n");
221041e0e3bSNishanth Aravamudan 		schedule_timeout_interruptible(5*HZ);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 	return NULL;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
nlmclnt_release_call(struct nlm_rqst * call)2267db836d4SChuck Lever void nlmclnt_release_call(struct nlm_rqst *call)
22792737230STrond Myklebust {
228b1ece737SBenjamin Coddington 	const struct nlmclnt_operations *nlmclnt_ops = call->a_host->h_nlmclnt_ops;
229b1ece737SBenjamin Coddington 
230fbca30c5SElena Reshetova 	if (!refcount_dec_and_test(&call->a_count))
2315e7f37a7STrond Myklebust 		return;
232b1ece737SBenjamin Coddington 	if (nlmclnt_ops && nlmclnt_ops->nlmclnt_release_call)
233b1ece737SBenjamin Coddington 		nlmclnt_ops->nlmclnt_release_call(call->a_callback_data);
2348ea6ecc8SChuck Lever 	nlmclnt_release_host(call->a_host);
23592737230STrond Myklebust 	nlmclnt_release_lockargs(call);
23692737230STrond Myklebust 	kfree(call);
23792737230STrond Myklebust }
23892737230STrond Myklebust 
nlmclnt_rpc_release(void * data)23992737230STrond Myklebust static void nlmclnt_rpc_release(void *data)
24092737230STrond Myklebust {
2417db836d4SChuck Lever 	nlmclnt_release_call(data);
24292737230STrond Myklebust }
24392737230STrond Myklebust 
nlm_wait_on_grace(wait_queue_head_t * queue)2441da177e4SLinus Torvalds static int nlm_wait_on_grace(wait_queue_head_t *queue)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
2471da177e4SLinus Torvalds 	int status = -EINTR;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
2501da177e4SLinus Torvalds 	if (!signalled ()) {
2511da177e4SLinus Torvalds 		schedule_timeout(NLMCLNT_GRACE_WAIT);
2523e1d1d28SChristoph Lameter 		try_to_freeze();
2531da177e4SLinus Torvalds 		if (!signalled ())
2541da177e4SLinus Torvalds 			status = 0;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 	finish_wait(queue, &wait);
2571da177e4SLinus Torvalds 	return status;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds /*
2611da177e4SLinus Torvalds  * Generic NLM call
2621da177e4SLinus Torvalds  */
2631da177e4SLinus Torvalds static int
nlmclnt_call(const struct cred * cred,struct nlm_rqst * req,u32 proc)264a52458b4SNeilBrown nlmclnt_call(const struct cred *cred, struct nlm_rqst *req, u32 proc)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
2671da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
2681da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
2691da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
2701da177e4SLinus Torvalds 	struct rpc_message msg = {
2711da177e4SLinus Torvalds 		.rpc_argp	= argp,
2721da177e4SLinus Torvalds 		.rpc_resp	= resp,
273d11d10ccSTrond Myklebust 		.rpc_cred	= cred,
2741da177e4SLinus Torvalds 	};
2751da177e4SLinus Torvalds 	int		status;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s\n",
2781da177e4SLinus Torvalds 			(int)proc, host->h_name);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	do {
2811da177e4SLinus Torvalds 		if (host->h_reclaiming && !argp->reclaim)
2821da177e4SLinus Torvalds 			goto in_grace_period;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		/* If we have no RPC client yet, create one. */
2851da177e4SLinus Torvalds 		if ((clnt = nlm_bind_host(host)) == NULL)
2861da177e4SLinus Torvalds 			return -ENOLCK;
2871da177e4SLinus Torvalds 		msg.rpc_proc = &clnt->cl_procinfo[proc];
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 		/* Perform the RPC call. If an error occurs, try again */
2901da177e4SLinus Torvalds 		if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
2911da177e4SLinus Torvalds 			dprintk("lockd: rpc_call returned error %d\n", -status);
2921da177e4SLinus Torvalds 			switch (status) {
2931da177e4SLinus Torvalds 			case -EPROTONOSUPPORT:
2941da177e4SLinus Torvalds 				status = -EINVAL;
2951da177e4SLinus Torvalds 				break;
2961da177e4SLinus Torvalds 			case -ECONNREFUSED:
2971da177e4SLinus Torvalds 			case -ETIMEDOUT:
2981da177e4SLinus Torvalds 			case -ENOTCONN:
2991da177e4SLinus Torvalds 				nlm_rebind_host(host);
3001da177e4SLinus Torvalds 				status = -EAGAIN;
3011da177e4SLinus Torvalds 				break;
3021da177e4SLinus Torvalds 			case -ERESTARTSYS:
3031da177e4SLinus Torvalds 				return signalled () ? -EINTR : status;
3041da177e4SLinus Torvalds 			default:
3051da177e4SLinus Torvalds 				break;
3061da177e4SLinus Torvalds 			}
3071da177e4SLinus Torvalds 			break;
3081da177e4SLinus Torvalds 		} else
309e8c5c045SAl Viro 		if (resp->status == nlm_lck_denied_grace_period) {
3101da177e4SLinus Torvalds 			dprintk("lockd: server in grace period\n");
3111da177e4SLinus Torvalds 			if (argp->reclaim) {
3121da177e4SLinus Torvalds 				printk(KERN_WARNING
3131da177e4SLinus Torvalds 				     "lockd: spurious grace period reject?!\n");
3141da177e4SLinus Torvalds 				return -ENOLCK;
3151da177e4SLinus Torvalds 			}
3161da177e4SLinus Torvalds 		} else {
3171da177e4SLinus Torvalds 			if (!argp->reclaim) {
3181da177e4SLinus Torvalds 				/* We appear to be out of the grace period */
3191da177e4SLinus Torvalds 				wake_up_all(&host->h_gracewait);
3201da177e4SLinus Torvalds 			}
32182c2c8b8SVasily Averin 			dprintk("lockd: server returns status %d\n",
32282c2c8b8SVasily Averin 				ntohl(resp->status));
3231da177e4SLinus Torvalds 			return 0;	/* Okay, call complete */
3241da177e4SLinus Torvalds 		}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds in_grace_period:
3271da177e4SLinus Torvalds 		/*
3281da177e4SLinus Torvalds 		 * The server has rebooted and appears to be in the grace
3291da177e4SLinus Torvalds 		 * period during which locks are only allowed to be
3301da177e4SLinus Torvalds 		 * reclaimed.
3311da177e4SLinus Torvalds 		 * We can only back off and try again later.
3321da177e4SLinus Torvalds 		 */
3331da177e4SLinus Torvalds 		status = nlm_wait_on_grace(&host->h_gracewait);
3341da177e4SLinus Torvalds 	} while (status == 0);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return status;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds /*
3401da177e4SLinus Torvalds  * Generic NLM call, async version.
3411da177e4SLinus Torvalds  */
__nlm_async_call(struct nlm_rqst * req,u32 proc,struct rpc_message * msg,const struct rpc_call_ops * tk_ops)342dc9d8d04STrond Myklebust static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
3451da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
346dc9d8d04STrond Myklebust 	struct rpc_task_setup task_setup_data = {
347dc9d8d04STrond Myklebust 		.rpc_message = msg,
348dc9d8d04STrond Myklebust 		.callback_ops = tk_ops,
349dc9d8d04STrond Myklebust 		.callback_data = req,
350dc9d8d04STrond Myklebust 		.flags = RPC_TASK_ASYNC,
351dc9d8d04STrond Myklebust 	};
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s (async)\n",
3541da177e4SLinus Torvalds 			(int)proc, host->h_name);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	/* If we have no RPC client yet, create one. */
35792737230STrond Myklebust 	clnt = nlm_bind_host(host);
35892737230STrond Myklebust 	if (clnt == NULL)
35992737230STrond Myklebust 		goto out_err;
360d4716624STrond Myklebust 	msg->rpc_proc = &clnt->cl_procinfo[proc];
361dc9d8d04STrond Myklebust 	task_setup_data.rpc_client = clnt;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds         /* bootstrap and kick off the async RPC call */
364dc9d8d04STrond Myklebust 	return rpc_run_task(&task_setup_data);
36592737230STrond Myklebust out_err:
366a995e9ebSTrond Myklebust 	tk_ops->rpc_release(req);
367dc9d8d04STrond Myklebust 	return ERR_PTR(-ENOLCK);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
nlm_do_async_call(struct nlm_rqst * req,u32 proc,struct rpc_message * msg,const struct rpc_call_ops * tk_ops)370dc9d8d04STrond Myklebust static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
371dc9d8d04STrond Myklebust {
372dc9d8d04STrond Myklebust 	struct rpc_task *task;
373dc9d8d04STrond Myklebust 
374dc9d8d04STrond Myklebust 	task = __nlm_async_call(req, proc, msg, tk_ops);
375dc9d8d04STrond Myklebust 	if (IS_ERR(task))
376dc9d8d04STrond Myklebust 		return PTR_ERR(task);
377dc9d8d04STrond Myklebust 	rpc_put_task(task);
378dc9d8d04STrond Myklebust 	return 0;
379dc9d8d04STrond Myklebust }
380dc9d8d04STrond Myklebust 
381dc9d8d04STrond Myklebust /*
382dc9d8d04STrond Myklebust  * NLM asynchronous call.
383dc9d8d04STrond Myklebust  */
nlm_async_call(struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)384d4716624STrond Myklebust int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
385d4716624STrond Myklebust {
386d4716624STrond Myklebust 	struct rpc_message msg = {
387d4716624STrond Myklebust 		.rpc_argp	= &req->a_args,
388d4716624STrond Myklebust 		.rpc_resp	= &req->a_res,
389d4716624STrond Myklebust 	};
390dc9d8d04STrond Myklebust 	return nlm_do_async_call(req, proc, &msg, tk_ops);
391d4716624STrond Myklebust }
392d4716624STrond Myklebust 
nlm_async_reply(struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)393d4716624STrond Myklebust int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
394d4716624STrond Myklebust {
395d4716624STrond Myklebust 	struct rpc_message msg = {
396d4716624STrond Myklebust 		.rpc_argp	= &req->a_res,
397d4716624STrond Myklebust 	};
398dc9d8d04STrond Myklebust 	return nlm_do_async_call(req, proc, &msg, tk_ops);
399dc9d8d04STrond Myklebust }
400dc9d8d04STrond Myklebust 
401dc9d8d04STrond Myklebust /*
402dc9d8d04STrond Myklebust  * NLM client asynchronous call.
403dc9d8d04STrond Myklebust  *
404dc9d8d04STrond Myklebust  * Note that although the calls are asynchronous, and are therefore
405dc9d8d04STrond Myklebust  *      guaranteed to complete, we still always attempt to wait for
406dc9d8d04STrond Myklebust  *      completion in order to be able to correctly track the lock
407dc9d8d04STrond Myklebust  *      state.
408dc9d8d04STrond Myklebust  */
nlmclnt_async_call(const struct cred * cred,struct nlm_rqst * req,u32 proc,const struct rpc_call_ops * tk_ops)409a52458b4SNeilBrown static int nlmclnt_async_call(const struct cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
410dc9d8d04STrond Myklebust {
411dc9d8d04STrond Myklebust 	struct rpc_message msg = {
412dc9d8d04STrond Myklebust 		.rpc_argp	= &req->a_args,
413dc9d8d04STrond Myklebust 		.rpc_resp	= &req->a_res,
414d11d10ccSTrond Myklebust 		.rpc_cred	= cred,
415dc9d8d04STrond Myklebust 	};
416dc9d8d04STrond Myklebust 	struct rpc_task *task;
417dc9d8d04STrond Myklebust 	int err;
418dc9d8d04STrond Myklebust 
419dc9d8d04STrond Myklebust 	task = __nlm_async_call(req, proc, &msg, tk_ops);
420dc9d8d04STrond Myklebust 	if (IS_ERR(task))
421dc9d8d04STrond Myklebust 		return PTR_ERR(task);
422dc9d8d04STrond Myklebust 	err = rpc_wait_for_completion_task(task);
423dc9d8d04STrond Myklebust 	rpc_put_task(task);
424dc9d8d04STrond Myklebust 	return err;
425d4716624STrond Myklebust }
426d4716624STrond Myklebust 
4271da177e4SLinus Torvalds /*
4281da177e4SLinus Torvalds  * TEST for the presence of a conflicting lock
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds static int
nlmclnt_test(struct nlm_rqst * req,struct file_lock * fl)4311da177e4SLinus Torvalds nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	int	status;
4341da177e4SLinus Torvalds 
435d11d10ccSTrond Myklebust 	status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_TEST);
4361da177e4SLinus Torvalds 	if (status < 0)
43792737230STrond Myklebust 		goto out;
4381da177e4SLinus Torvalds 
43992737230STrond Myklebust 	switch (req->a_res.status) {
440e8c5c045SAl Viro 		case nlm_granted:
4411da177e4SLinus Torvalds 			fl->fl_type = F_UNLCK;
44292737230STrond Myklebust 			break;
443e8c5c045SAl Viro 		case nlm_lck_denied:
4441da177e4SLinus Torvalds 			/*
4451da177e4SLinus Torvalds 			 * Report the conflicting lock back to the application.
4461da177e4SLinus Torvalds 			 */
447e4cd038aSTrond Myklebust 			fl->fl_start = req->a_res.lock.fl.fl_start;
448d67d1c7bSFelix Blyakher 			fl->fl_end = req->a_res.lock.fl.fl_end;
449e4cd038aSTrond Myklebust 			fl->fl_type = req->a_res.lock.fl.fl_type;
450b8eee0e9SBenjamin Coddington 			fl->fl_pid = -req->a_res.lock.fl.fl_pid;
45192737230STrond Myklebust 			break;
45292737230STrond Myklebust 		default:
45392737230STrond Myklebust 			status = nlm_stat_to_errno(req->a_res.status);
4541da177e4SLinus Torvalds 	}
45592737230STrond Myklebust out:
456*2f90e18fSJeff Layton 	trace_nlmclnt_test(&req->a_args.lock,
457*2f90e18fSJeff Layton 			   (const struct sockaddr *)&req->a_host->h_addr,
458*2f90e18fSJeff Layton 			   req->a_host->h_addrlen, req->a_res.status);
4597db836d4SChuck Lever 	nlmclnt_release_call(req);
46092737230STrond Myklebust 	return status;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
nlmclnt_locks_copy_lock(struct file_lock * new,struct file_lock * fl)4631da177e4SLinus Torvalds static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4641da177e4SLinus Torvalds {
46563185942SBryan Schumaker 	spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4664c060b53STrond Myklebust 	new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
4679de3ec1dSBenjamin Coddington 	new->fl_u.nfs_fl.owner = nlmclnt_get_lockowner(fl->fl_u.nfs_fl.owner);
4684c060b53STrond Myklebust 	list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
46963185942SBryan Schumaker 	spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
nlmclnt_locks_release_private(struct file_lock * fl)4721da177e4SLinus Torvalds static void nlmclnt_locks_release_private(struct file_lock *fl)
4731da177e4SLinus Torvalds {
47463185942SBryan Schumaker 	spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4754c060b53STrond Myklebust 	list_del(&fl->fl_u.nfs_fl.list);
47663185942SBryan Schumaker 	spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4779de3ec1dSBenjamin Coddington 	nlmclnt_put_lockowner(fl->fl_u.nfs_fl.owner);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4806aed6285SAlexey Dobriyan static const struct file_lock_operations nlmclnt_lock_ops = {
4811da177e4SLinus Torvalds 	.fl_copy_lock = nlmclnt_locks_copy_lock,
4821da177e4SLinus Torvalds 	.fl_release_private = nlmclnt_locks_release_private,
4831da177e4SLinus Torvalds };
4841da177e4SLinus Torvalds 
nlmclnt_locks_init_private(struct file_lock * fl,struct nlm_host * host)4851da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.state = 0;
4889de3ec1dSBenjamin Coddington 	fl->fl_u.nfs_fl.owner = nlmclnt_find_lockowner(host, fl->fl_owner);
4894c060b53STrond Myklebust 	INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
4901da177e4SLinus Torvalds 	fl->fl_ops = &nlmclnt_lock_ops;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
do_vfs_lock(struct file_lock * fl)4939b073574STrond Myklebust static int do_vfs_lock(struct file_lock *fl)
4941da177e4SLinus Torvalds {
4954f656367SBenjamin Coddington 	return locks_lock_file_wait(fl->fl_file, fl);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /*
4991da177e4SLinus Torvalds  * LOCK: Try to create a lock
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  *			Programmer Harassment Alert
5021da177e4SLinus Torvalds  *
5031da177e4SLinus Torvalds  * When given a blocking lock request in a sync RPC call, the HPUX lockd
5041da177e4SLinus Torvalds  * will faithfully return LCK_BLOCKED but never cares to notify us when
5051da177e4SLinus Torvalds  * the lock could be granted. This way, our local process could hang
5061da177e4SLinus Torvalds  * around forever waiting for the callback.
5071da177e4SLinus Torvalds  *
5081da177e4SLinus Torvalds  *  Solution A:	Implement busy-waiting
5091da177e4SLinus Torvalds  *  Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
5101da177e4SLinus Torvalds  *
5111da177e4SLinus Torvalds  * For now I am implementing solution A, because I hate the idea of
5121da177e4SLinus Torvalds  * re-implementing lockd for a third time in two months. The async
5131da177e4SLinus Torvalds  * calls shouldn't be too hard to do, however.
5141da177e4SLinus Torvalds  *
5151da177e4SLinus Torvalds  * This is one of the lovely things about standards in the NFS area:
5161da177e4SLinus Torvalds  * they're so soft and squishy you can't really blame HP for doing this.
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds static int
nlmclnt_lock(struct nlm_rqst * req,struct file_lock * fl)5191da177e4SLinus Torvalds nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
5201da177e4SLinus Torvalds {
521a52458b4SNeilBrown 	const struct cred *cred = nfs_file_cred(fl->fl_file);
5221da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
5231da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
5242005f5b9SJeff Layton 	struct nlm_wait block;
52501c3b861STrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
5265f50c0c6STrond Myklebust 	unsigned char fl_type;
5272005f5b9SJeff Layton 	__be32 b_status;
5283a649b88STrond Myklebust 	int status = -ENOLCK;
5291da177e4SLinus Torvalds 
530501c1ed3SChuck Lever 	if (nsm_monitor(host) < 0)
5311da177e4SLinus Torvalds 		goto out;
5326c9dc425SChuck Lever 	req->a_args.state = nsm_local_state;
533501c1ed3SChuck Lever 
53401c3b861STrond Myklebust 	fl->fl_flags |= FL_ACCESS;
53501c3b861STrond Myklebust 	status = do_vfs_lock(fl);
5364a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
53701c3b861STrond Myklebust 	if (status < 0)
53801c3b861STrond Myklebust 		goto out;
5391da177e4SLinus Torvalds 
5402005f5b9SJeff Layton 	nlmclnt_prepare_block(&block, host, fl);
54128df955aSTrond Myklebust again:
5425f50c0c6STrond Myklebust 	/*
5435f50c0c6STrond Myklebust 	 * Initialise resp->status to a valid non-zero value,
5445f50c0c6STrond Myklebust 	 * since 0 == nlm_lck_granted
5455f50c0c6STrond Myklebust 	 */
5465f50c0c6STrond Myklebust 	resp->status = nlm_lck_blocked;
5472005f5b9SJeff Layton 
5482005f5b9SJeff Layton 	/*
5492005f5b9SJeff Layton 	 * A GRANTED callback can come at any time -- even before the reply
5502005f5b9SJeff Layton 	 * to the LOCK request arrives, so we queue the wait before
5512005f5b9SJeff Layton 	 * requesting the lock.
5522005f5b9SJeff Layton 	 */
5532005f5b9SJeff Layton 	nlmclnt_queue_block(&block);
554ecdbf769STrond Myklebust 	for (;;) {
55528df955aSTrond Myklebust 		/* Reboot protection */
55628df955aSTrond Myklebust 		fl->fl_u.nfs_fl.state = host->h_state;
557d11d10ccSTrond Myklebust 		status = nlmclnt_call(cred, req, NLMPROC_LOCK);
558ecdbf769STrond Myklebust 		if (status < 0)
559ecdbf769STrond Myklebust 			break;
560ecdbf769STrond Myklebust 		/* Did a reclaimer thread notify us of a server reboot? */
561e8c5c045SAl Viro 		if (resp->status == nlm_lck_denied_grace_period)
562ecdbf769STrond Myklebust 			continue;
563e8c5c045SAl Viro 		if (resp->status != nlm_lck_blocked)
564ecdbf769STrond Myklebust 			break;
5653a649b88STrond Myklebust 		/* Wait on an NLM blocking lock */
5662005f5b9SJeff Layton 		status = nlmclnt_wait(&block, req, NLMCLNT_POLL_TIMEOUT);
5673a649b88STrond Myklebust 		if (status < 0)
5685f50c0c6STrond Myklebust 			break;
5692005f5b9SJeff Layton 		if (block.b_status != nlm_lck_blocked)
5703a649b88STrond Myklebust 			break;
571ecdbf769STrond Myklebust 	}
5722005f5b9SJeff Layton 	b_status = nlmclnt_dequeue_block(&block);
5732005f5b9SJeff Layton 	if (resp->status == nlm_lck_blocked)
5742005f5b9SJeff Layton 		resp->status = b_status;
5751da177e4SLinus Torvalds 
5765f50c0c6STrond Myklebust 	/* if we were interrupted while blocking, then cancel the lock request
5775f50c0c6STrond Myklebust 	 * and exit
5785f50c0c6STrond Myklebust 	 */
5795f50c0c6STrond Myklebust 	if (resp->status == nlm_lck_blocked) {
5805f50c0c6STrond Myklebust 		if (!req->a_args.block)
5815f50c0c6STrond Myklebust 			goto out_unlock;
5825f50c0c6STrond Myklebust 		if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
5832005f5b9SJeff Layton 			goto out;
5845f50c0c6STrond Myklebust 	}
5855f50c0c6STrond Myklebust 
586e8c5c045SAl Viro 	if (resp->status == nlm_granted) {
58728df955aSTrond Myklebust 		down_read(&host->h_rwsem);
58828df955aSTrond Myklebust 		/* Check whether or not the server has rebooted */
58928df955aSTrond Myklebust 		if (fl->fl_u.nfs_fl.state != host->h_state) {
59028df955aSTrond Myklebust 			up_read(&host->h_rwsem);
59128df955aSTrond Myklebust 			goto again;
59228df955aSTrond Myklebust 		}
5934c060b53STrond Myklebust 		/* Ensure the resulting lock will get added to granted list */
5944a9af59fSTrond Myklebust 		fl->fl_flags |= FL_SLEEP;
5959b073574STrond Myklebust 		if (do_vfs_lock(fl) < 0)
5968e24eea7SHarvey Harrison 			printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
59728df955aSTrond Myklebust 		up_read(&host->h_rwsem);
5984a9af59fSTrond Myklebust 		fl->fl_flags = fl_flags;
5995f50c0c6STrond Myklebust 		status = 0;
6001da177e4SLinus Torvalds 	}
6015f50c0c6STrond Myklebust 	if (status < 0)
6025f50c0c6STrond Myklebust 		goto out_unlock;
603cc77b152SMiklos Szeredi 	/*
604cc77b152SMiklos Szeredi 	 * EAGAIN doesn't make sense for sleeping locks, and in some
605cc77b152SMiklos Szeredi 	 * cases NLM_LCK_DENIED is returned for a permanent error.  So
606cc77b152SMiklos Szeredi 	 * turn it into an ENOLCK.
607cc77b152SMiklos Szeredi 	 */
608cc77b152SMiklos Szeredi 	if (resp->status == nlm_lck_denied && (fl_flags & FL_SLEEP))
609cc77b152SMiklos Szeredi 		status = -ENOLCK;
610cc77b152SMiklos Szeredi 	else
6111da177e4SLinus Torvalds 		status = nlm_stat_to_errno(resp->status);
6121da177e4SLinus Torvalds out:
613*2f90e18fSJeff Layton 	trace_nlmclnt_lock(&req->a_args.lock,
614*2f90e18fSJeff Layton 			   (const struct sockaddr *)&req->a_host->h_addr,
615*2f90e18fSJeff Layton 			   req->a_host->h_addrlen, req->a_res.status);
6167db836d4SChuck Lever 	nlmclnt_release_call(req);
6171da177e4SLinus Torvalds 	return status;
6185f50c0c6STrond Myklebust out_unlock:
6195f50c0c6STrond Myklebust 	/* Fatal error: ensure that we remove the lock altogether */
620*2f90e18fSJeff Layton 	trace_nlmclnt_lock(&req->a_args.lock,
621*2f90e18fSJeff Layton 			   (const struct sockaddr *)&req->a_host->h_addr,
622*2f90e18fSJeff Layton 			   req->a_host->h_addrlen, req->a_res.status);
6235f50c0c6STrond Myklebust 	dprintk("lockd: lock attempt ended in fatal error.\n"
6245f50c0c6STrond Myklebust 		"       Attempting to unlock.\n");
6255f50c0c6STrond Myklebust 	fl_type = fl->fl_type;
6265f50c0c6STrond Myklebust 	fl->fl_type = F_UNLCK;
6275f50c0c6STrond Myklebust 	down_read(&host->h_rwsem);
6285f50c0c6STrond Myklebust 	do_vfs_lock(fl);
6295f50c0c6STrond Myklebust 	up_read(&host->h_rwsem);
6305f50c0c6STrond Myklebust 	fl->fl_type = fl_type;
6315f50c0c6STrond Myklebust 	fl->fl_flags = fl_flags;
632d11d10ccSTrond Myklebust 	nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
6335f50c0c6STrond Myklebust 	return status;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /*
6371da177e4SLinus Torvalds  * RECLAIM: Try to reclaim a lock
6381da177e4SLinus Torvalds  */
6391da177e4SLinus Torvalds int
nlmclnt_reclaim(struct nlm_host * host,struct file_lock * fl,struct nlm_rqst * req)640f25cc71eSTim Gardner nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl,
641f25cc71eSTim Gardner 		struct nlm_rqst *req)
6421da177e4SLinus Torvalds {
6431da177e4SLinus Torvalds 	int		status;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	memset(req, 0, sizeof(*req));
6461da177e4SLinus Torvalds 	locks_init_lock(&req->a_args.lock.fl);
6471da177e4SLinus Torvalds 	locks_init_lock(&req->a_res.lock.fl);
6481da177e4SLinus Torvalds 	req->a_host  = host;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* Set up the argument struct */
6511da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
6521da177e4SLinus Torvalds 	req->a_args.reclaim = 1;
6531da177e4SLinus Torvalds 
654d11d10ccSTrond Myklebust 	status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_LOCK);
655d11d10ccSTrond Myklebust 	if (status >= 0 && req->a_res.status == nlm_granted)
6561da177e4SLinus Torvalds 		return 0;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
6591da177e4SLinus Torvalds 				"(errno %d, status %d)\n", fl->fl_pid,
660e8c5c045SAl Viro 				status, ntohl(req->a_res.status));
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	/*
6631da177e4SLinus Torvalds 	 * FIXME: This is a serious failure. We can
6641da177e4SLinus Torvalds 	 *
6651da177e4SLinus Torvalds 	 *  a.	Ignore the problem
6661da177e4SLinus Torvalds 	 *  b.	Send the owning process some signal (Linux doesn't have
6671da177e4SLinus Torvalds 	 *	SIGLOST, though...)
6681da177e4SLinus Torvalds 	 *  c.	Retry the operation
6691da177e4SLinus Torvalds 	 *
6701da177e4SLinus Torvalds 	 * Until someone comes up with a simple implementation
6711da177e4SLinus Torvalds 	 * for b or c, I'll choose option a.
6721da177e4SLinus Torvalds 	 */
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	return -ENOLCK;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /*
6781da177e4SLinus Torvalds  * UNLOCK: remove an existing lock
6791da177e4SLinus Torvalds  */
6801da177e4SLinus Torvalds static int
nlmclnt_unlock(struct nlm_rqst * req,struct file_lock * fl)6811da177e4SLinus Torvalds nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
6821da177e4SLinus Torvalds {
68328df955aSTrond Myklebust 	struct nlm_host	*host = req->a_host;
6841da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
6854a9af59fSTrond Myklebust 	int status;
6864a9af59fSTrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
6871da177e4SLinus Torvalds 
68826bcbf96SChristoph Hellwig 	/*
68930f4e20aSTrond Myklebust 	 * Note: the server is supposed to either grant us the unlock
69030f4e20aSTrond Myklebust 	 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
69130f4e20aSTrond Myklebust 	 * case, we want to unlock.
69230f4e20aSTrond Myklebust 	 */
6939b073574STrond Myklebust 	fl->fl_flags |= FL_EXISTS;
69428df955aSTrond Myklebust 	down_read(&host->h_rwsem);
6954a9af59fSTrond Myklebust 	status = do_vfs_lock(fl);
6969b073574STrond Myklebust 	up_read(&host->h_rwsem);
6974a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
6984a9af59fSTrond Myklebust 	if (status == -ENOENT) {
6994a9af59fSTrond Myklebust 		status = 0;
7009b073574STrond Myklebust 		goto out;
7019b073574STrond Myklebust 	}
70230f4e20aSTrond Myklebust 
703fbca30c5SElena Reshetova 	refcount_inc(&req->a_count);
704d11d10ccSTrond Myklebust 	status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
705d11d10ccSTrond Myklebust 			NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
7061da177e4SLinus Torvalds 	if (status < 0)
70792737230STrond Myklebust 		goto out;
7081da177e4SLinus Torvalds 
709e8c5c045SAl Viro 	if (resp->status == nlm_granted)
71092737230STrond Myklebust 		goto out;
7111da177e4SLinus Torvalds 
712e8c5c045SAl Viro 	if (resp->status != nlm_lck_denied_nolocks)
71382c2c8b8SVasily Averin 		printk("lockd: unexpected unlock status: %d\n",
71482c2c8b8SVasily Averin 			ntohl(resp->status));
7151da177e4SLinus Torvalds 	/* What to do now? I'm out of my depth... */
71692737230STrond Myklebust 	status = -ENOLCK;
71792737230STrond Myklebust out:
718*2f90e18fSJeff Layton 	trace_nlmclnt_unlock(&req->a_args.lock,
719*2f90e18fSJeff Layton 			     (const struct sockaddr *)&req->a_host->h_addr,
720*2f90e18fSJeff Layton 			     req->a_host->h_addrlen, req->a_res.status);
7217db836d4SChuck Lever 	nlmclnt_release_call(req);
72292737230STrond Myklebust 	return status;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
nlmclnt_unlock_prepare(struct rpc_task * task,void * data)725b1ece737SBenjamin Coddington static void nlmclnt_unlock_prepare(struct rpc_task *task, void *data)
726b1ece737SBenjamin Coddington {
727b1ece737SBenjamin Coddington 	struct nlm_rqst	*req = data;
728b1ece737SBenjamin Coddington 	const struct nlmclnt_operations *nlmclnt_ops = req->a_host->h_nlmclnt_ops;
729b1ece737SBenjamin Coddington 	bool defer_call = false;
730b1ece737SBenjamin Coddington 
731b1ece737SBenjamin Coddington 	if (nlmclnt_ops && nlmclnt_ops->nlmclnt_unlock_prepare)
732b1ece737SBenjamin Coddington 		defer_call = nlmclnt_ops->nlmclnt_unlock_prepare(task, req->a_callback_data);
733b1ece737SBenjamin Coddington 
734b1ece737SBenjamin Coddington 	if (!defer_call)
735b1ece737SBenjamin Coddington 		rpc_call_start(task);
736b1ece737SBenjamin Coddington }
737b1ece737SBenjamin Coddington 
nlmclnt_unlock_callback(struct rpc_task * task,void * data)738963d8fe5STrond Myklebust static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
7391da177e4SLinus Torvalds {
740963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
741e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
7421da177e4SLinus Torvalds 
743ae67bd38STrond Myklebust 	if (RPC_SIGNALLED(task))
7441da177e4SLinus Torvalds 		goto die;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (task->tk_status < 0) {
7471da177e4SLinus Torvalds 		dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
7480b760113STrond Myklebust 		switch (task->tk_status) {
7490b760113STrond Myklebust 		case -EACCES:
7500b760113STrond Myklebust 		case -EIO:
7510b760113STrond Myklebust 			goto die;
7520b760113STrond Myklebust 		default:
7531da177e4SLinus Torvalds 			goto retry_rebind;
7541da177e4SLinus Torvalds 		}
7550b760113STrond Myklebust 	}
7561da177e4SLinus Torvalds 	if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
7571da177e4SLinus Torvalds 		rpc_delay(task, NLMCLNT_GRACE_WAIT);
7581da177e4SLinus Torvalds 		goto retry_unlock;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 	if (status != NLM_LCK_GRANTED)
7611da177e4SLinus Torvalds 		printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
7621da177e4SLinus Torvalds die:
7631da177e4SLinus Torvalds 	return;
7641da177e4SLinus Torvalds  retry_rebind:
7651da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
7661da177e4SLinus Torvalds  retry_unlock:
7671da177e4SLinus Torvalds 	rpc_restart_call(task);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
770963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops = {
771b1ece737SBenjamin Coddington 	.rpc_call_prepare = nlmclnt_unlock_prepare,
772963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_unlock_callback,
77392737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
774963d8fe5STrond Myklebust };
775963d8fe5STrond Myklebust 
7761da177e4SLinus Torvalds /*
7771da177e4SLinus Torvalds  * Cancel a blocked lock request.
7781da177e4SLinus Torvalds  * We always use an async RPC call for this in order not to hang a
7791da177e4SLinus Torvalds  * process that has been Ctrl-C'ed.
7801da177e4SLinus Torvalds  */
nlmclnt_cancel(struct nlm_host * host,int block,struct file_lock * fl)78116fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	struct nlm_rqst	*req;
7846b4b3a75STrond Myklebust 	int status;
7856b4b3a75STrond Myklebust 
7866b4b3a75STrond Myklebust 	dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
7876b4b3a75STrond Myklebust 		"       Attempting to cancel lock.\n");
7881da177e4SLinus Torvalds 
789446945abSAl Viro 	req = nlm_alloc_call(host);
7901da177e4SLinus Torvalds 	if (!req)
7911da177e4SLinus Torvalds 		return -ENOMEM;
7921da177e4SLinus Torvalds 	req->a_flags = RPC_TASK_ASYNC;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
79516fb2425STrond Myklebust 	req->a_args.block = block;
7961da177e4SLinus Torvalds 
797fbca30c5SElena Reshetova 	refcount_inc(&req->a_count);
798d11d10ccSTrond Myklebust 	status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
799d11d10ccSTrond Myklebust 			NLMPROC_CANCEL, &nlmclnt_cancel_ops);
8006b4b3a75STrond Myklebust 	if (status == 0 && req->a_res.status == nlm_lck_denied)
8016b4b3a75STrond Myklebust 		status = -ENOLCK;
8027db836d4SChuck Lever 	nlmclnt_release_call(req);
8036b4b3a75STrond Myklebust 	return status;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
nlmclnt_cancel_callback(struct rpc_task * task,void * data)806963d8fe5STrond Myklebust static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
8071da177e4SLinus Torvalds {
808963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
809e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
8101da177e4SLinus Torvalds 
811ae67bd38STrond Myklebust 	if (RPC_SIGNALLED(task))
8121da177e4SLinus Torvalds 		goto die;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	if (task->tk_status < 0) {
8151da177e4SLinus Torvalds 		dprintk("lockd: CANCEL call error %d, retrying.\n",
8161da177e4SLinus Torvalds 					task->tk_status);
8171da177e4SLinus Torvalds 		goto retry_cancel;
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 
820e8c5c045SAl Viro 	switch (status) {
8211da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
8221da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
82335576cbaSTrond Myklebust 	case NLM_LCK_DENIED:
8241da177e4SLinus Torvalds 		/* Everything's good */
8251da177e4SLinus Torvalds 		break;
8261da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
8271da177e4SLinus Torvalds 		dprintk("lockd: CANCEL failed (server has no locks)\n");
8281da177e4SLinus Torvalds 		goto retry_cancel;
8291da177e4SLinus Torvalds 	default:
8301da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
831e8c5c045SAl Viro 			status);
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds die:
8351da177e4SLinus Torvalds 	return;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds retry_cancel:
838aaaa9942STrond Myklebust 	/* Don't ever retry more than 3 times */
839aaaa9942STrond Myklebust 	if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
840aaaa9942STrond Myklebust 		goto die;
8411da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
8421da177e4SLinus Torvalds 	rpc_restart_call(task);
8431da177e4SLinus Torvalds 	rpc_delay(task, 30 * HZ);
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops = {
847963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_cancel_callback,
84892737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
849963d8fe5STrond Myklebust };
850963d8fe5STrond Myklebust 
8511da177e4SLinus Torvalds /*
8521da177e4SLinus Torvalds  * Convert an NLM status code to a generic kernel errno
8531da177e4SLinus Torvalds  */
8541da177e4SLinus Torvalds static int
nlm_stat_to_errno(__be32 status)855e8c5c045SAl Viro nlm_stat_to_errno(__be32 status)
8561da177e4SLinus Torvalds {
857e8c5c045SAl Viro 	switch(ntohl(status)) {
8581da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
8591da177e4SLinus Torvalds 		return 0;
8601da177e4SLinus Torvalds 	case NLM_LCK_DENIED:
8611da177e4SLinus Torvalds 		return -EAGAIN;
8621da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
8631da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
8641da177e4SLinus Torvalds 		return -ENOLCK;
8651da177e4SLinus Torvalds 	case NLM_LCK_BLOCKED:
8661da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
8671da177e4SLinus Torvalds 		return -ENOLCK;
8681da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
8691da177e4SLinus Torvalds 	case NLM_DEADLCK:
8701da177e4SLinus Torvalds 		return -EDEADLK;
8711da177e4SLinus Torvalds 	case NLM_ROFS:
8721da177e4SLinus Torvalds 		return -EROFS;
8731da177e4SLinus Torvalds 	case NLM_STALE_FH:
8741da177e4SLinus Torvalds 		return -ESTALE;
8751da177e4SLinus Torvalds 	case NLM_FBIG:
8761da177e4SLinus Torvalds 		return -EOVERFLOW;
8771da177e4SLinus Torvalds 	case NLM_FAILED:
8781da177e4SLinus Torvalds 		return -ENOLCK;
8791da177e4SLinus Torvalds #endif
8801da177e4SLinus Torvalds 	}
88182c2c8b8SVasily Averin 	printk(KERN_NOTICE "lockd: unexpected server status %d\n",
88282c2c8b8SVasily Averin 		 ntohl(status));
8831da177e4SLinus Torvalds 	return -ENOLCK;
8841da177e4SLinus Torvalds }
885