xref: /openbmc/linux/fs/lockd/clntproc.c (revision 446945ab9a82515af4b099107eda27050e077c58)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/lockd/clntproc.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * RPC procedures for the client side NLM implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/nfs_fs.h>
151da177e4SLinus Torvalds #include <linux/utsname.h>
167dfb7103SNigel Cunningham #include <linux/freezer.h>
171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
181da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
191da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
221da177e4SLinus Torvalds #define NLMCLNT_GRACE_WAIT	(5*HZ)
23ecdbf769STrond Myklebust #define NLMCLNT_POLL_TIMEOUT	(30*HZ)
24aaaa9942STrond Myklebust #define NLMCLNT_MAX_RETRIES	3
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static int	nlmclnt_test(struct nlm_rqst *, struct file_lock *);
271da177e4SLinus Torvalds static int	nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
281da177e4SLinus Torvalds static int	nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
29e8c5c045SAl Viro static int	nlm_stat_to_errno(__be32 stat);
301da177e4SLinus Torvalds static void	nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
3116fb2425STrond Myklebust static int	nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
321da177e4SLinus Torvalds 
33963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops;
34963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops;
35963d8fe5STrond Myklebust 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * Cookie counter for NLM requests
381da177e4SLinus Torvalds  */
39031d869dSOlaf Kirch static atomic_t	nlm_cookie = ATOMIC_INIT(0x1234);
401da177e4SLinus Torvalds 
41031d869dSOlaf Kirch void nlmclnt_next_cookie(struct nlm_cookie *c)
421da177e4SLinus Torvalds {
43031d869dSOlaf Kirch 	u32	cookie = atomic_inc_return(&nlm_cookie);
44031d869dSOlaf Kirch 
45031d869dSOlaf Kirch 	memcpy(c->data, &cookie, 4);
461da177e4SLinus Torvalds 	c->len=4;
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	atomic_inc(&lockowner->count);
521da177e4SLinus Torvalds 	return lockowner;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
581da177e4SLinus Torvalds 		return;
591da177e4SLinus Torvalds 	list_del(&lockowner->list);
601da177e4SLinus Torvalds 	spin_unlock(&lockowner->host->h_lock);
618ea6ecc8SChuck Lever 	nlmclnt_release_host(lockowner->host);
621da177e4SLinus Torvalds 	kfree(lockowner);
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
681da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
691da177e4SLinus Torvalds 		if (lockowner->pid == pid)
701da177e4SLinus Torvalds 			return -EBUSY;
711da177e4SLinus Torvalds 	}
721da177e4SLinus Torvalds 	return 0;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	uint32_t res;
781da177e4SLinus Torvalds 	do {
791da177e4SLinus Torvalds 		res = host->h_pidcount++;
801da177e4SLinus Torvalds 	} while (nlm_pidbusy(host, res) < 0);
811da177e4SLinus Torvalds 	return res;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
871da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
881da177e4SLinus Torvalds 		if (lockowner->owner != owner)
891da177e4SLinus Torvalds 			continue;
901da177e4SLinus Torvalds 		return nlm_get_lockowner(lockowner);
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 	return NULL;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	struct nlm_lockowner *res, *new = NULL;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	spin_lock(&host->h_lock);
1001da177e4SLinus Torvalds 	res = __nlm_find_lockowner(host, owner);
1011da177e4SLinus Torvalds 	if (res == NULL) {
1021da177e4SLinus Torvalds 		spin_unlock(&host->h_lock);
103f52720caSPanagiotis Issaris 		new = kmalloc(sizeof(*new), GFP_KERNEL);
1041da177e4SLinus Torvalds 		spin_lock(&host->h_lock);
1051da177e4SLinus Torvalds 		res = __nlm_find_lockowner(host, owner);
1061da177e4SLinus Torvalds 		if (res == NULL && new != NULL) {
1071da177e4SLinus Torvalds 			res = new;
1081da177e4SLinus Torvalds 			atomic_set(&new->count, 1);
1091da177e4SLinus Torvalds 			new->owner = owner;
1101da177e4SLinus Torvalds 			new->pid = __nlm_alloc_pid(host);
1111da177e4SLinus Torvalds 			new->host = nlm_get_host(host);
1121da177e4SLinus Torvalds 			list_add(&new->list, &host->h_lockowners);
1131da177e4SLinus Torvalds 			new = NULL;
1141da177e4SLinus Torvalds 		}
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 	spin_unlock(&host->h_lock);
1171da177e4SLinus Torvalds 	kfree(new);
1181da177e4SLinus Torvalds 	return res;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
1271da177e4SLinus Torvalds 	struct nlm_lock	*lock = &argp->lock;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	nlmclnt_next_cookie(&argp->cookie);
130225a719fSJosef Sipek 	memcpy(&lock->fh, NFS_FH(fl->fl_file->f_path.dentry->d_inode), sizeof(struct nfs_fh));
131e9ff3990SSerge E. Hallyn 	lock->caller  = utsname()->nodename;
1321da177e4SLinus Torvalds 	lock->oh.data = req->a_owner;
1337bab377fSTrond Myklebust 	lock->oh.len  = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
1347bab377fSTrond Myklebust 				(unsigned int)fl->fl_u.nfs_fl.owner->pid,
135e9ff3990SSerge E. Hallyn 				utsname()->nodename);
1367bab377fSTrond Myklebust 	lock->svid = fl->fl_u.nfs_fl.owner->pid;
1373a649b88STrond Myklebust 	lock->fl.fl_start = fl->fl_start;
1383a649b88STrond Myklebust 	lock->fl.fl_end = fl->fl_end;
1393a649b88STrond Myklebust 	lock->fl.fl_type = fl->fl_type;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static void nlmclnt_release_lockargs(struct nlm_rqst *req)
1431da177e4SLinus Torvalds {
1443a649b88STrond Myklebust 	BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471093a60eSChuck Lever /**
1481093a60eSChuck Lever  * nlmclnt_proc - Perform a single client-side lock request
1491093a60eSChuck Lever  * @host: address of a valid nlm_host context representing the NLM server
1501093a60eSChuck Lever  * @cmd: fcntl-style file lock operation to perform
1511093a60eSChuck Lever  * @fl: address of arguments for the lock operation
1521093a60eSChuck Lever  *
1531da177e4SLinus Torvalds  */
1541093a60eSChuck Lever int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl)
1551da177e4SLinus Torvalds {
15692737230STrond Myklebust 	struct nlm_rqst		*call;
1571093a60eSChuck Lever 	int			status;
1581da177e4SLinus Torvalds 
15992737230STrond Myklebust 	call = nlm_alloc_call(host);
16092737230STrond Myklebust 	if (call == NULL)
16192737230STrond Myklebust 		return -ENOMEM;
1621da177e4SLinus Torvalds 
16392737230STrond Myklebust 	nlmclnt_locks_init_private(fl, host);
16492737230STrond Myklebust 	/* Set up the argument struct */
16592737230STrond Myklebust 	nlmclnt_setlockargs(call, fl);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
1681da177e4SLinus Torvalds 		if (fl->fl_type != F_UNLCK) {
1691da177e4SLinus Torvalds 			call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
1701da177e4SLinus Torvalds 			status = nlmclnt_lock(call, fl);
1711da177e4SLinus Torvalds 		} else
1721da177e4SLinus Torvalds 			status = nlmclnt_unlock(call, fl);
1731da177e4SLinus Torvalds 	} else if (IS_GETLK(cmd))
1741da177e4SLinus Torvalds 		status = nlmclnt_test(call, fl);
1751da177e4SLinus Torvalds 	else
1761da177e4SLinus Torvalds 		status = -EINVAL;
17792737230STrond Myklebust 	fl->fl_ops->fl_release_private(fl);
17892737230STrond Myklebust 	fl->fl_ops = NULL;
17992737230STrond Myklebust 
1801da177e4SLinus Torvalds 	dprintk("lockd: clnt proc returns %d\n", status);
1811da177e4SLinus Torvalds 	return status;
1821da177e4SLinus Torvalds }
1831093a60eSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_proc);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds /*
1861da177e4SLinus Torvalds  * Allocate an NLM RPC call struct
1871da177e4SLinus Torvalds  */
18892737230STrond Myklebust struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	struct nlm_rqst	*call;
1911da177e4SLinus Torvalds 
19236943fa4STrond Myklebust 	for(;;) {
19336943fa4STrond Myklebust 		call = kzalloc(sizeof(*call), GFP_KERNEL);
19436943fa4STrond Myklebust 		if (call != NULL) {
1955e7f37a7STrond Myklebust 			atomic_set(&call->a_count, 1);
1961da177e4SLinus Torvalds 			locks_init_lock(&call->a_args.lock.fl);
1971da177e4SLinus Torvalds 			locks_init_lock(&call->a_res.lock.fl);
198*446945abSAl Viro 			call->a_host = nlm_get_host(host);
1991da177e4SLinus Torvalds 			return call;
2001da177e4SLinus Torvalds 		}
20136943fa4STrond Myklebust 		if (signalled())
20236943fa4STrond Myklebust 			break;
20392737230STrond Myklebust 		printk("nlm_alloc_call: failed, waiting for memory\n");
204041e0e3bSNishanth Aravamudan 		schedule_timeout_interruptible(5*HZ);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	return NULL;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2097db836d4SChuck Lever void nlmclnt_release_call(struct nlm_rqst *call)
21092737230STrond Myklebust {
2115e7f37a7STrond Myklebust 	if (!atomic_dec_and_test(&call->a_count))
2125e7f37a7STrond Myklebust 		return;
2138ea6ecc8SChuck Lever 	nlmclnt_release_host(call->a_host);
21492737230STrond Myklebust 	nlmclnt_release_lockargs(call);
21592737230STrond Myklebust 	kfree(call);
21692737230STrond Myklebust }
21792737230STrond Myklebust 
21892737230STrond Myklebust static void nlmclnt_rpc_release(void *data)
21992737230STrond Myklebust {
2207db836d4SChuck Lever 	nlmclnt_release_call(data);
22192737230STrond Myklebust }
22292737230STrond Myklebust 
2231da177e4SLinus Torvalds static int nlm_wait_on_grace(wait_queue_head_t *queue)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
2261da177e4SLinus Torvalds 	int status = -EINTR;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
2291da177e4SLinus Torvalds 	if (!signalled ()) {
2301da177e4SLinus Torvalds 		schedule_timeout(NLMCLNT_GRACE_WAIT);
2313e1d1d28SChristoph Lameter 		try_to_freeze();
2321da177e4SLinus Torvalds 		if (!signalled ())
2331da177e4SLinus Torvalds 			status = 0;
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 	finish_wait(queue, &wait);
2361da177e4SLinus Torvalds 	return status;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /*
2401da177e4SLinus Torvalds  * Generic NLM call
2411da177e4SLinus Torvalds  */
2421da177e4SLinus Torvalds static int
243d11d10ccSTrond Myklebust nlmclnt_call(struct rpc_cred *cred, struct nlm_rqst *req, u32 proc)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
2461da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
2471da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
2481da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
2491da177e4SLinus Torvalds 	struct rpc_message msg = {
2501da177e4SLinus Torvalds 		.rpc_argp	= argp,
2511da177e4SLinus Torvalds 		.rpc_resp	= resp,
252d11d10ccSTrond Myklebust 		.rpc_cred	= cred,
2531da177e4SLinus Torvalds 	};
2541da177e4SLinus Torvalds 	int		status;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s\n",
2571da177e4SLinus Torvalds 			(int)proc, host->h_name);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	do {
2601da177e4SLinus Torvalds 		if (host->h_reclaiming && !argp->reclaim)
2611da177e4SLinus Torvalds 			goto in_grace_period;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		/* If we have no RPC client yet, create one. */
2641da177e4SLinus Torvalds 		if ((clnt = nlm_bind_host(host)) == NULL)
2651da177e4SLinus Torvalds 			return -ENOLCK;
2661da177e4SLinus Torvalds 		msg.rpc_proc = &clnt->cl_procinfo[proc];
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		/* Perform the RPC call. If an error occurs, try again */
2691da177e4SLinus Torvalds 		if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
2701da177e4SLinus Torvalds 			dprintk("lockd: rpc_call returned error %d\n", -status);
2711da177e4SLinus Torvalds 			switch (status) {
2721da177e4SLinus Torvalds 			case -EPROTONOSUPPORT:
2731da177e4SLinus Torvalds 				status = -EINVAL;
2741da177e4SLinus Torvalds 				break;
2751da177e4SLinus Torvalds 			case -ECONNREFUSED:
2761da177e4SLinus Torvalds 			case -ETIMEDOUT:
2771da177e4SLinus Torvalds 			case -ENOTCONN:
2781da177e4SLinus Torvalds 				nlm_rebind_host(host);
2791da177e4SLinus Torvalds 				status = -EAGAIN;
2801da177e4SLinus Torvalds 				break;
2811da177e4SLinus Torvalds 			case -ERESTARTSYS:
2821da177e4SLinus Torvalds 				return signalled () ? -EINTR : status;
2831da177e4SLinus Torvalds 			default:
2841da177e4SLinus Torvalds 				break;
2851da177e4SLinus Torvalds 			}
2861da177e4SLinus Torvalds 			break;
2871da177e4SLinus Torvalds 		} else
288e8c5c045SAl Viro 		if (resp->status == nlm_lck_denied_grace_period) {
2891da177e4SLinus Torvalds 			dprintk("lockd: server in grace period\n");
2901da177e4SLinus Torvalds 			if (argp->reclaim) {
2911da177e4SLinus Torvalds 				printk(KERN_WARNING
2921da177e4SLinus Torvalds 				     "lockd: spurious grace period reject?!\n");
2931da177e4SLinus Torvalds 				return -ENOLCK;
2941da177e4SLinus Torvalds 			}
2951da177e4SLinus Torvalds 		} else {
2961da177e4SLinus Torvalds 			if (!argp->reclaim) {
2971da177e4SLinus Torvalds 				/* We appear to be out of the grace period */
2981da177e4SLinus Torvalds 				wake_up_all(&host->h_gracewait);
2991da177e4SLinus Torvalds 			}
30082c2c8b8SVasily Averin 			dprintk("lockd: server returns status %d\n",
30182c2c8b8SVasily Averin 				ntohl(resp->status));
3021da177e4SLinus Torvalds 			return 0;	/* Okay, call complete */
3031da177e4SLinus Torvalds 		}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds in_grace_period:
3061da177e4SLinus Torvalds 		/*
3071da177e4SLinus Torvalds 		 * The server has rebooted and appears to be in the grace
3081da177e4SLinus Torvalds 		 * period during which locks are only allowed to be
3091da177e4SLinus Torvalds 		 * reclaimed.
3101da177e4SLinus Torvalds 		 * We can only back off and try again later.
3111da177e4SLinus Torvalds 		 */
3121da177e4SLinus Torvalds 		status = nlm_wait_on_grace(&host->h_gracewait);
3131da177e4SLinus Torvalds 	} while (status == 0);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	return status;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Generic NLM call, async version.
3201da177e4SLinus Torvalds  */
321dc9d8d04STrond 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)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
3241da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
325dc9d8d04STrond Myklebust 	struct rpc_task_setup task_setup_data = {
326dc9d8d04STrond Myklebust 		.rpc_message = msg,
327dc9d8d04STrond Myklebust 		.callback_ops = tk_ops,
328dc9d8d04STrond Myklebust 		.callback_data = req,
329dc9d8d04STrond Myklebust 		.flags = RPC_TASK_ASYNC,
330dc9d8d04STrond Myklebust 	};
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s (async)\n",
3331da177e4SLinus Torvalds 			(int)proc, host->h_name);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* If we have no RPC client yet, create one. */
33692737230STrond Myklebust 	clnt = nlm_bind_host(host);
33792737230STrond Myklebust 	if (clnt == NULL)
33892737230STrond Myklebust 		goto out_err;
339d4716624STrond Myklebust 	msg->rpc_proc = &clnt->cl_procinfo[proc];
340dc9d8d04STrond Myklebust 	task_setup_data.rpc_client = clnt;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds         /* bootstrap and kick off the async RPC call */
343dc9d8d04STrond Myklebust 	return rpc_run_task(&task_setup_data);
34492737230STrond Myklebust out_err:
345a995e9ebSTrond Myklebust 	tk_ops->rpc_release(req);
346dc9d8d04STrond Myklebust 	return ERR_PTR(-ENOLCK);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
349dc9d8d04STrond Myklebust static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
350dc9d8d04STrond Myklebust {
351dc9d8d04STrond Myklebust 	struct rpc_task *task;
352dc9d8d04STrond Myklebust 
353dc9d8d04STrond Myklebust 	task = __nlm_async_call(req, proc, msg, tk_ops);
354dc9d8d04STrond Myklebust 	if (IS_ERR(task))
355dc9d8d04STrond Myklebust 		return PTR_ERR(task);
356dc9d8d04STrond Myklebust 	rpc_put_task(task);
357dc9d8d04STrond Myklebust 	return 0;
358dc9d8d04STrond Myklebust }
359dc9d8d04STrond Myklebust 
360dc9d8d04STrond Myklebust /*
361dc9d8d04STrond Myklebust  * NLM asynchronous call.
362dc9d8d04STrond Myklebust  */
363d4716624STrond Myklebust int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
364d4716624STrond Myklebust {
365d4716624STrond Myklebust 	struct rpc_message msg = {
366d4716624STrond Myklebust 		.rpc_argp	= &req->a_args,
367d4716624STrond Myklebust 		.rpc_resp	= &req->a_res,
368d4716624STrond Myklebust 	};
369dc9d8d04STrond Myklebust 	return nlm_do_async_call(req, proc, &msg, tk_ops);
370d4716624STrond Myklebust }
371d4716624STrond Myklebust 
372d4716624STrond Myklebust int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
373d4716624STrond Myklebust {
374d4716624STrond Myklebust 	struct rpc_message msg = {
375d4716624STrond Myklebust 		.rpc_argp	= &req->a_res,
376d4716624STrond Myklebust 	};
377dc9d8d04STrond Myklebust 	return nlm_do_async_call(req, proc, &msg, tk_ops);
378dc9d8d04STrond Myklebust }
379dc9d8d04STrond Myklebust 
380dc9d8d04STrond Myklebust /*
381dc9d8d04STrond Myklebust  * NLM client asynchronous call.
382dc9d8d04STrond Myklebust  *
383dc9d8d04STrond Myklebust  * Note that although the calls are asynchronous, and are therefore
384dc9d8d04STrond Myklebust  *      guaranteed to complete, we still always attempt to wait for
385dc9d8d04STrond Myklebust  *      completion in order to be able to correctly track the lock
386dc9d8d04STrond Myklebust  *      state.
387dc9d8d04STrond Myklebust  */
388d11d10ccSTrond Myklebust static int nlmclnt_async_call(struct rpc_cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
389dc9d8d04STrond Myklebust {
390dc9d8d04STrond Myklebust 	struct rpc_message msg = {
391dc9d8d04STrond Myklebust 		.rpc_argp	= &req->a_args,
392dc9d8d04STrond Myklebust 		.rpc_resp	= &req->a_res,
393d11d10ccSTrond Myklebust 		.rpc_cred	= cred,
394dc9d8d04STrond Myklebust 	};
395dc9d8d04STrond Myklebust 	struct rpc_task *task;
396dc9d8d04STrond Myklebust 	int err;
397dc9d8d04STrond Myklebust 
398dc9d8d04STrond Myklebust 	task = __nlm_async_call(req, proc, &msg, tk_ops);
399dc9d8d04STrond Myklebust 	if (IS_ERR(task))
400dc9d8d04STrond Myklebust 		return PTR_ERR(task);
401dc9d8d04STrond Myklebust 	err = rpc_wait_for_completion_task(task);
402dc9d8d04STrond Myklebust 	rpc_put_task(task);
403dc9d8d04STrond Myklebust 	return err;
404d4716624STrond Myklebust }
405d4716624STrond Myklebust 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  * TEST for the presence of a conflicting lock
4081da177e4SLinus Torvalds  */
4091da177e4SLinus Torvalds static int
4101da177e4SLinus Torvalds nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	int	status;
4131da177e4SLinus Torvalds 
414d11d10ccSTrond Myklebust 	status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_TEST);
4151da177e4SLinus Torvalds 	if (status < 0)
41692737230STrond Myklebust 		goto out;
4171da177e4SLinus Torvalds 
41892737230STrond Myklebust 	switch (req->a_res.status) {
419e8c5c045SAl Viro 		case nlm_granted:
4201da177e4SLinus Torvalds 			fl->fl_type = F_UNLCK;
42192737230STrond Myklebust 			break;
422e8c5c045SAl Viro 		case nlm_lck_denied:
4231da177e4SLinus Torvalds 			/*
4241da177e4SLinus Torvalds 			 * Report the conflicting lock back to the application.
4251da177e4SLinus Torvalds 			 */
426e4cd038aSTrond Myklebust 			fl->fl_start = req->a_res.lock.fl.fl_start;
427d67d1c7bSFelix Blyakher 			fl->fl_end = req->a_res.lock.fl.fl_end;
428e4cd038aSTrond Myklebust 			fl->fl_type = req->a_res.lock.fl.fl_type;
4291da177e4SLinus Torvalds 			fl->fl_pid = 0;
43092737230STrond Myklebust 			break;
43192737230STrond Myklebust 		default:
43292737230STrond Myklebust 			status = nlm_stat_to_errno(req->a_res.status);
4331da177e4SLinus Torvalds 	}
43492737230STrond Myklebust out:
4357db836d4SChuck Lever 	nlmclnt_release_call(req);
43692737230STrond Myklebust 	return status;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4401da177e4SLinus Torvalds {
44163185942SBryan Schumaker 	spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4424c060b53STrond Myklebust 	new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
4434c060b53STrond Myklebust 	new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
4444c060b53STrond Myklebust 	list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
44563185942SBryan Schumaker 	spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds static void nlmclnt_locks_release_private(struct file_lock *fl)
4491da177e4SLinus Torvalds {
45063185942SBryan Schumaker 	spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4514c060b53STrond Myklebust 	list_del(&fl->fl_u.nfs_fl.list);
45263185942SBryan Schumaker 	spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
4531da177e4SLinus Torvalds 	nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4566aed6285SAlexey Dobriyan static const struct file_lock_operations nlmclnt_lock_ops = {
4571da177e4SLinus Torvalds 	.fl_copy_lock = nlmclnt_locks_copy_lock,
4581da177e4SLinus Torvalds 	.fl_release_private = nlmclnt_locks_release_private,
4591da177e4SLinus Torvalds };
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	BUG_ON(fl->fl_ops != NULL);
4641da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.state = 0;
4651da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
4664c060b53STrond Myklebust 	INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
4671da177e4SLinus Torvalds 	fl->fl_ops = &nlmclnt_lock_ops;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4709b073574STrond Myklebust static int do_vfs_lock(struct file_lock *fl)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	int res = 0;
4731da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4741da177e4SLinus Torvalds 		case FL_POSIX:
4751da177e4SLinus Torvalds 			res = posix_lock_file_wait(fl->fl_file, fl);
4761da177e4SLinus Torvalds 			break;
4771da177e4SLinus Torvalds 		case FL_FLOCK:
4781da177e4SLinus Torvalds 			res = flock_lock_file_wait(fl->fl_file, fl);
4791da177e4SLinus Torvalds 			break;
4801da177e4SLinus Torvalds 		default:
4811da177e4SLinus Torvalds 			BUG();
4821da177e4SLinus Torvalds 	}
4839b073574STrond Myklebust 	return res;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /*
4871da177e4SLinus Torvalds  * LOCK: Try to create a lock
4881da177e4SLinus Torvalds  *
4891da177e4SLinus Torvalds  *			Programmer Harassment Alert
4901da177e4SLinus Torvalds  *
4911da177e4SLinus Torvalds  * When given a blocking lock request in a sync RPC call, the HPUX lockd
4921da177e4SLinus Torvalds  * will faithfully return LCK_BLOCKED but never cares to notify us when
4931da177e4SLinus Torvalds  * the lock could be granted. This way, our local process could hang
4941da177e4SLinus Torvalds  * around forever waiting for the callback.
4951da177e4SLinus Torvalds  *
4961da177e4SLinus Torvalds  *  Solution A:	Implement busy-waiting
4971da177e4SLinus Torvalds  *  Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  * For now I am implementing solution A, because I hate the idea of
5001da177e4SLinus Torvalds  * re-implementing lockd for a third time in two months. The async
5011da177e4SLinus Torvalds  * calls shouldn't be too hard to do, however.
5021da177e4SLinus Torvalds  *
5031da177e4SLinus Torvalds  * This is one of the lovely things about standards in the NFS area:
5041da177e4SLinus Torvalds  * they're so soft and squishy you can't really blame HP for doing this.
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds static int
5071da177e4SLinus Torvalds nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
5081da177e4SLinus Torvalds {
509d11d10ccSTrond Myklebust 	struct rpc_cred *cred = nfs_file_cred(fl->fl_file);
5101da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
5111da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
5123a649b88STrond Myklebust 	struct nlm_wait *block = NULL;
51301c3b861STrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
5145f50c0c6STrond Myklebust 	unsigned char fl_type;
5153a649b88STrond Myklebust 	int status = -ENOLCK;
5161da177e4SLinus Torvalds 
517501c1ed3SChuck Lever 	if (nsm_monitor(host) < 0)
5181da177e4SLinus Torvalds 		goto out;
5196c9dc425SChuck Lever 	req->a_args.state = nsm_local_state;
520501c1ed3SChuck Lever 
52101c3b861STrond Myklebust 	fl->fl_flags |= FL_ACCESS;
52201c3b861STrond Myklebust 	status = do_vfs_lock(fl);
5234a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
52401c3b861STrond Myklebust 	if (status < 0)
52501c3b861STrond Myklebust 		goto out;
5261da177e4SLinus Torvalds 
5273a649b88STrond Myklebust 	block = nlmclnt_prepare_block(host, fl);
52828df955aSTrond Myklebust again:
5295f50c0c6STrond Myklebust 	/*
5305f50c0c6STrond Myklebust 	 * Initialise resp->status to a valid non-zero value,
5315f50c0c6STrond Myklebust 	 * since 0 == nlm_lck_granted
5325f50c0c6STrond Myklebust 	 */
5335f50c0c6STrond Myklebust 	resp->status = nlm_lck_blocked;
534ecdbf769STrond Myklebust 	for(;;) {
53528df955aSTrond Myklebust 		/* Reboot protection */
53628df955aSTrond Myklebust 		fl->fl_u.nfs_fl.state = host->h_state;
537d11d10ccSTrond Myklebust 		status = nlmclnt_call(cred, req, NLMPROC_LOCK);
538ecdbf769STrond Myklebust 		if (status < 0)
539ecdbf769STrond Myklebust 			break;
540ecdbf769STrond Myklebust 		/* Did a reclaimer thread notify us of a server reboot? */
541e8c5c045SAl Viro 		if (resp->status ==  nlm_lck_denied_grace_period)
542ecdbf769STrond Myklebust 			continue;
543e8c5c045SAl Viro 		if (resp->status != nlm_lck_blocked)
544ecdbf769STrond Myklebust 			break;
5453a649b88STrond Myklebust 		/* Wait on an NLM blocking lock */
5463a649b88STrond Myklebust 		status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
5473a649b88STrond Myklebust 		if (status < 0)
5485f50c0c6STrond Myklebust 			break;
549e8c5c045SAl Viro 		if (resp->status != nlm_lck_blocked)
5503a649b88STrond Myklebust 			break;
551ecdbf769STrond Myklebust 	}
5521da177e4SLinus Torvalds 
5535f50c0c6STrond Myklebust 	/* if we were interrupted while blocking, then cancel the lock request
5545f50c0c6STrond Myklebust 	 * and exit
5555f50c0c6STrond Myklebust 	 */
5565f50c0c6STrond Myklebust 	if (resp->status == nlm_lck_blocked) {
5575f50c0c6STrond Myklebust 		if (!req->a_args.block)
5585f50c0c6STrond Myklebust 			goto out_unlock;
5595f50c0c6STrond Myklebust 		if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
5605f50c0c6STrond Myklebust 			goto out_unblock;
5615f50c0c6STrond Myklebust 	}
5625f50c0c6STrond Myklebust 
563e8c5c045SAl Viro 	if (resp->status == nlm_granted) {
56428df955aSTrond Myklebust 		down_read(&host->h_rwsem);
56528df955aSTrond Myklebust 		/* Check whether or not the server has rebooted */
56628df955aSTrond Myklebust 		if (fl->fl_u.nfs_fl.state != host->h_state) {
56728df955aSTrond Myklebust 			up_read(&host->h_rwsem);
56828df955aSTrond Myklebust 			goto again;
56928df955aSTrond Myklebust 		}
5704c060b53STrond Myklebust 		/* Ensure the resulting lock will get added to granted list */
5714a9af59fSTrond Myklebust 		fl->fl_flags |= FL_SLEEP;
5729b073574STrond Myklebust 		if (do_vfs_lock(fl) < 0)
5738e24eea7SHarvey Harrison 			printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
57428df955aSTrond Myklebust 		up_read(&host->h_rwsem);
5754a9af59fSTrond Myklebust 		fl->fl_flags = fl_flags;
5765f50c0c6STrond Myklebust 		status = 0;
5771da177e4SLinus Torvalds 	}
5785f50c0c6STrond Myklebust 	if (status < 0)
5795f50c0c6STrond Myklebust 		goto out_unlock;
580cc77b152SMiklos Szeredi 	/*
581cc77b152SMiklos Szeredi 	 * EAGAIN doesn't make sense for sleeping locks, and in some
582cc77b152SMiklos Szeredi 	 * cases NLM_LCK_DENIED is returned for a permanent error.  So
583cc77b152SMiklos Szeredi 	 * turn it into an ENOLCK.
584cc77b152SMiklos Szeredi 	 */
585cc77b152SMiklos Szeredi 	if (resp->status == nlm_lck_denied && (fl_flags & FL_SLEEP))
586cc77b152SMiklos Szeredi 		status = -ENOLCK;
587cc77b152SMiklos Szeredi 	else
5881da177e4SLinus Torvalds 		status = nlm_stat_to_errno(resp->status);
589ecdbf769STrond Myklebust out_unblock:
5903a649b88STrond Myklebust 	nlmclnt_finish_block(block);
5911da177e4SLinus Torvalds out:
5927db836d4SChuck Lever 	nlmclnt_release_call(req);
5931da177e4SLinus Torvalds 	return status;
5945f50c0c6STrond Myklebust out_unlock:
5955f50c0c6STrond Myklebust 	/* Fatal error: ensure that we remove the lock altogether */
5965f50c0c6STrond Myklebust 	dprintk("lockd: lock attempt ended in fatal error.\n"
5975f50c0c6STrond Myklebust 		"       Attempting to unlock.\n");
5985f50c0c6STrond Myklebust 	nlmclnt_finish_block(block);
5995f50c0c6STrond Myklebust 	fl_type = fl->fl_type;
6005f50c0c6STrond Myklebust 	fl->fl_type = F_UNLCK;
6015f50c0c6STrond Myklebust 	down_read(&host->h_rwsem);
6025f50c0c6STrond Myklebust 	do_vfs_lock(fl);
6035f50c0c6STrond Myklebust 	up_read(&host->h_rwsem);
6045f50c0c6STrond Myklebust 	fl->fl_type = fl_type;
6055f50c0c6STrond Myklebust 	fl->fl_flags = fl_flags;
606d11d10ccSTrond Myklebust 	nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
6075f50c0c6STrond Myklebust 	return status;
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
6111da177e4SLinus Torvalds  * RECLAIM: Try to reclaim a lock
6121da177e4SLinus Torvalds  */
6131da177e4SLinus Torvalds int
6141da177e4SLinus Torvalds nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct nlm_rqst reqst, *req;
6171da177e4SLinus Torvalds 	int		status;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	req = &reqst;
6201da177e4SLinus Torvalds 	memset(req, 0, sizeof(*req));
6211da177e4SLinus Torvalds 	locks_init_lock(&req->a_args.lock.fl);
6221da177e4SLinus Torvalds 	locks_init_lock(&req->a_res.lock.fl);
6231da177e4SLinus Torvalds 	req->a_host  = host;
6241da177e4SLinus Torvalds 	req->a_flags = 0;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	/* Set up the argument struct */
6271da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
6281da177e4SLinus Torvalds 	req->a_args.reclaim = 1;
6291da177e4SLinus Torvalds 
630d11d10ccSTrond Myklebust 	status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_LOCK);
631d11d10ccSTrond Myklebust 	if (status >= 0 && req->a_res.status == nlm_granted)
6321da177e4SLinus Torvalds 		return 0;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
6351da177e4SLinus Torvalds 				"(errno %d, status %d)\n", fl->fl_pid,
636e8c5c045SAl Viro 				status, ntohl(req->a_res.status));
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	/*
6391da177e4SLinus Torvalds 	 * FIXME: This is a serious failure. We can
6401da177e4SLinus Torvalds 	 *
6411da177e4SLinus Torvalds 	 *  a.	Ignore the problem
6421da177e4SLinus Torvalds 	 *  b.	Send the owning process some signal (Linux doesn't have
6431da177e4SLinus Torvalds 	 *	SIGLOST, though...)
6441da177e4SLinus Torvalds 	 *  c.	Retry the operation
6451da177e4SLinus Torvalds 	 *
6461da177e4SLinus Torvalds 	 * Until someone comes up with a simple implementation
6471da177e4SLinus Torvalds 	 * for b or c, I'll choose option a.
6481da177e4SLinus Torvalds 	 */
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	return -ENOLCK;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /*
6541da177e4SLinus Torvalds  * UNLOCK: remove an existing lock
6551da177e4SLinus Torvalds  */
6561da177e4SLinus Torvalds static int
6571da177e4SLinus Torvalds nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
6581da177e4SLinus Torvalds {
65928df955aSTrond Myklebust 	struct nlm_host	*host = req->a_host;
6601da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
6614a9af59fSTrond Myklebust 	int status;
6624a9af59fSTrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
6631da177e4SLinus Torvalds 
66426bcbf96SChristoph Hellwig 	/*
66530f4e20aSTrond Myklebust 	 * Note: the server is supposed to either grant us the unlock
66630f4e20aSTrond Myklebust 	 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
66730f4e20aSTrond Myklebust 	 * case, we want to unlock.
66830f4e20aSTrond Myklebust 	 */
6699b073574STrond Myklebust 	fl->fl_flags |= FL_EXISTS;
67028df955aSTrond Myklebust 	down_read(&host->h_rwsem);
6714a9af59fSTrond Myklebust 	status = do_vfs_lock(fl);
6729b073574STrond Myklebust 	up_read(&host->h_rwsem);
6734a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
6744a9af59fSTrond Myklebust 	if (status == -ENOENT) {
6754a9af59fSTrond Myklebust 		status = 0;
6769b073574STrond Myklebust 		goto out;
6779b073574STrond Myklebust 	}
67830f4e20aSTrond Myklebust 
679dc9d8d04STrond Myklebust 	atomic_inc(&req->a_count);
680d11d10ccSTrond Myklebust 	status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
681d11d10ccSTrond Myklebust 			NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
6821da177e4SLinus Torvalds 	if (status < 0)
68392737230STrond Myklebust 		goto out;
6841da177e4SLinus Torvalds 
685e8c5c045SAl Viro 	if (resp->status == nlm_granted)
68692737230STrond Myklebust 		goto out;
6871da177e4SLinus Torvalds 
688e8c5c045SAl Viro 	if (resp->status != nlm_lck_denied_nolocks)
68982c2c8b8SVasily Averin 		printk("lockd: unexpected unlock status: %d\n",
69082c2c8b8SVasily Averin 			ntohl(resp->status));
6911da177e4SLinus Torvalds 	/* What to do now? I'm out of my depth... */
69292737230STrond Myklebust 	status = -ENOLCK;
69392737230STrond Myklebust out:
6947db836d4SChuck Lever 	nlmclnt_release_call(req);
69592737230STrond Myklebust 	return status;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
698963d8fe5STrond Myklebust static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
6991da177e4SLinus Torvalds {
700963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
701e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
7041da177e4SLinus Torvalds 		goto die;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	if (task->tk_status < 0) {
7071da177e4SLinus Torvalds 		dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
7080b760113STrond Myklebust 		switch (task->tk_status) {
7090b760113STrond Myklebust 		case -EACCES:
7100b760113STrond Myklebust 		case -EIO:
7110b760113STrond Myklebust 			goto die;
7120b760113STrond Myklebust 		default:
7131da177e4SLinus Torvalds 			goto retry_rebind;
7141da177e4SLinus Torvalds 		}
7150b760113STrond Myklebust 	}
7161da177e4SLinus Torvalds 	if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
7171da177e4SLinus Torvalds 		rpc_delay(task, NLMCLNT_GRACE_WAIT);
7181da177e4SLinus Torvalds 		goto retry_unlock;
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 	if (status != NLM_LCK_GRANTED)
7211da177e4SLinus Torvalds 		printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
7221da177e4SLinus Torvalds die:
7231da177e4SLinus Torvalds 	return;
7241da177e4SLinus Torvalds  retry_rebind:
7251da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
7261da177e4SLinus Torvalds  retry_unlock:
7271da177e4SLinus Torvalds 	rpc_restart_call(task);
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
730963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops = {
731963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_unlock_callback,
73292737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
733963d8fe5STrond Myklebust };
734963d8fe5STrond Myklebust 
7351da177e4SLinus Torvalds /*
7361da177e4SLinus Torvalds  * Cancel a blocked lock request.
7371da177e4SLinus Torvalds  * We always use an async RPC call for this in order not to hang a
7381da177e4SLinus Torvalds  * process that has been Ctrl-C'ed.
7391da177e4SLinus Torvalds  */
74016fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	struct nlm_rqst	*req;
7436b4b3a75STrond Myklebust 	int status;
7446b4b3a75STrond Myklebust 
7456b4b3a75STrond Myklebust 	dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
7466b4b3a75STrond Myklebust 		"       Attempting to cancel lock.\n");
7471da177e4SLinus Torvalds 
748*446945abSAl Viro 	req = nlm_alloc_call(host);
7491da177e4SLinus Torvalds 	if (!req)
7501da177e4SLinus Torvalds 		return -ENOMEM;
7511da177e4SLinus Torvalds 	req->a_flags = RPC_TASK_ASYNC;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
75416fb2425STrond Myklebust 	req->a_args.block = block;
7551da177e4SLinus Torvalds 
7566b4b3a75STrond Myklebust 	atomic_inc(&req->a_count);
757d11d10ccSTrond Myklebust 	status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
758d11d10ccSTrond Myklebust 			NLMPROC_CANCEL, &nlmclnt_cancel_ops);
7596b4b3a75STrond Myklebust 	if (status == 0 && req->a_res.status == nlm_lck_denied)
7606b4b3a75STrond Myklebust 		status = -ENOLCK;
7617db836d4SChuck Lever 	nlmclnt_release_call(req);
7626b4b3a75STrond Myklebust 	return status;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765963d8fe5STrond Myklebust static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
7661da177e4SLinus Torvalds {
767963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
768e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
7711da177e4SLinus Torvalds 		goto die;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	if (task->tk_status < 0) {
7741da177e4SLinus Torvalds 		dprintk("lockd: CANCEL call error %d, retrying.\n",
7751da177e4SLinus Torvalds 					task->tk_status);
7761da177e4SLinus Torvalds 		goto retry_cancel;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 
779c041b5ffSChuck Lever 	dprintk("lockd: cancel status %u (task %u)\n",
780e8c5c045SAl Viro 			status, task->tk_pid);
7811da177e4SLinus Torvalds 
782e8c5c045SAl Viro 	switch (status) {
7831da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
7841da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
78535576cbaSTrond Myklebust 	case NLM_LCK_DENIED:
7861da177e4SLinus Torvalds 		/* Everything's good */
7871da177e4SLinus Torvalds 		break;
7881da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
7891da177e4SLinus Torvalds 		dprintk("lockd: CANCEL failed (server has no locks)\n");
7901da177e4SLinus Torvalds 		goto retry_cancel;
7911da177e4SLinus Torvalds 	default:
7921da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
793e8c5c045SAl Viro 			status);
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds die:
7971da177e4SLinus Torvalds 	return;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds retry_cancel:
800aaaa9942STrond Myklebust 	/* Don't ever retry more than 3 times */
801aaaa9942STrond Myklebust 	if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
802aaaa9942STrond Myklebust 		goto die;
8031da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
8041da177e4SLinus Torvalds 	rpc_restart_call(task);
8051da177e4SLinus Torvalds 	rpc_delay(task, 30 * HZ);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
808963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops = {
809963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_cancel_callback,
81092737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
811963d8fe5STrond Myklebust };
812963d8fe5STrond Myklebust 
8131da177e4SLinus Torvalds /*
8141da177e4SLinus Torvalds  * Convert an NLM status code to a generic kernel errno
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds static int
817e8c5c045SAl Viro nlm_stat_to_errno(__be32 status)
8181da177e4SLinus Torvalds {
819e8c5c045SAl Viro 	switch(ntohl(status)) {
8201da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
8211da177e4SLinus Torvalds 		return 0;
8221da177e4SLinus Torvalds 	case NLM_LCK_DENIED:
8231da177e4SLinus Torvalds 		return -EAGAIN;
8241da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
8251da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
8261da177e4SLinus Torvalds 		return -ENOLCK;
8271da177e4SLinus Torvalds 	case NLM_LCK_BLOCKED:
8281da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
8291da177e4SLinus Torvalds 		return -ENOLCK;
8301da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
8311da177e4SLinus Torvalds 	case NLM_DEADLCK:
8321da177e4SLinus Torvalds 		return -EDEADLK;
8331da177e4SLinus Torvalds 	case NLM_ROFS:
8341da177e4SLinus Torvalds 		return -EROFS;
8351da177e4SLinus Torvalds 	case NLM_STALE_FH:
8361da177e4SLinus Torvalds 		return -ESTALE;
8371da177e4SLinus Torvalds 	case NLM_FBIG:
8381da177e4SLinus Torvalds 		return -EOVERFLOW;
8391da177e4SLinus Torvalds 	case NLM_FAILED:
8401da177e4SLinus Torvalds 		return -ENOLCK;
8411da177e4SLinus Torvalds #endif
8421da177e4SLinus Torvalds 	}
84382c2c8b8SVasily Averin 	printk(KERN_NOTICE "lockd: unexpected server status %d\n",
84482c2c8b8SVasily Averin 		 ntohl(status));
8451da177e4SLinus Torvalds 	return -ENOLCK;
8461da177e4SLinus Torvalds }
847