xref: /openbmc/linux/fs/lockd/clntproc.c (revision 5e7f37a76fa5b604949020b7317962262812b2dd)
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>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/nfs_fs.h>
141da177e4SLinus Torvalds #include <linux/utsname.h>
157dfb7103SNigel Cunningham #include <linux/freezer.h>
161da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
171da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
181da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
191da177e4SLinus Torvalds #include <linux/lockd/sm_inter.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);
611da177e4SLinus Torvalds 	nlm_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);
1301da177e4SLinus Torvalds 	argp->state   = nsm_local_state;
131225a719fSJosef Sipek 	memcpy(&lock->fh, NFS_FH(fl->fl_file->f_path.dentry->d_inode), sizeof(struct nfs_fh));
132e9ff3990SSerge E. Hallyn 	lock->caller  = utsname()->nodename;
1331da177e4SLinus Torvalds 	lock->oh.data = req->a_owner;
1347bab377fSTrond Myklebust 	lock->oh.len  = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
1357bab377fSTrond Myklebust 				(unsigned int)fl->fl_u.nfs_fl.owner->pid,
136e9ff3990SSerge E. Hallyn 				utsname()->nodename);
1377bab377fSTrond Myklebust 	lock->svid = fl->fl_u.nfs_fl.owner->pid;
1383a649b88STrond Myklebust 	lock->fl.fl_start = fl->fl_start;
1393a649b88STrond Myklebust 	lock->fl.fl_end = fl->fl_end;
1403a649b88STrond Myklebust 	lock->fl.fl_type = fl->fl_type;
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static void nlmclnt_release_lockargs(struct nlm_rqst *req)
1441da177e4SLinus Torvalds {
1453a649b88STrond Myklebust 	BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481093a60eSChuck Lever /**
1491093a60eSChuck Lever  * nlmclnt_proc - Perform a single client-side lock request
1501093a60eSChuck Lever  * @host: address of a valid nlm_host context representing the NLM server
1511093a60eSChuck Lever  * @cmd: fcntl-style file lock operation to perform
1521093a60eSChuck Lever  * @fl: address of arguments for the lock operation
1531093a60eSChuck Lever  *
1541da177e4SLinus Torvalds  */
1551093a60eSChuck Lever int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl)
1561da177e4SLinus Torvalds {
15792737230STrond Myklebust 	struct nlm_rqst		*call;
1581da177e4SLinus Torvalds 	sigset_t		oldset;
1591da177e4SLinus Torvalds 	unsigned long		flags;
1601093a60eSChuck Lever 	int			status;
1611da177e4SLinus Torvalds 
1621093a60eSChuck Lever 	nlm_get_host(host);
16392737230STrond Myklebust 	call = nlm_alloc_call(host);
16492737230STrond Myklebust 	if (call == NULL)
16592737230STrond Myklebust 		return -ENOMEM;
1661da177e4SLinus Torvalds 
16792737230STrond Myklebust 	nlmclnt_locks_init_private(fl, host);
16892737230STrond Myklebust 	/* Set up the argument struct */
16992737230STrond Myklebust 	nlmclnt_setlockargs(call, fl);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* Keep the old signal mask */
1721da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
1731da177e4SLinus Torvalds 	oldset = current->blocked;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* If we're cleaning up locks because the process is exiting,
1761da177e4SLinus Torvalds 	 * perform the RPC call asynchronously. */
1771da177e4SLinus Torvalds 	if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
1781da177e4SLinus Torvalds 	    && fl->fl_type == F_UNLCK
1791da177e4SLinus Torvalds 	    && (current->flags & PF_EXITING)) {
1801da177e4SLinus Torvalds 		sigfillset(&current->blocked);	/* Mask all signals */
1811da177e4SLinus Torvalds 		recalc_sigpending();
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 		call->a_flags = RPC_TASK_ASYNC;
1841da177e4SLinus Torvalds 	}
18592737230STrond Myklebust 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
1881da177e4SLinus Torvalds 		if (fl->fl_type != F_UNLCK) {
1891da177e4SLinus Torvalds 			call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
1901da177e4SLinus Torvalds 			status = nlmclnt_lock(call, fl);
1911da177e4SLinus Torvalds 		} else
1921da177e4SLinus Torvalds 			status = nlmclnt_unlock(call, fl);
1931da177e4SLinus Torvalds 	} else if (IS_GETLK(cmd))
1941da177e4SLinus Torvalds 		status = nlmclnt_test(call, fl);
1951da177e4SLinus Torvalds 	else
1961da177e4SLinus Torvalds 		status = -EINVAL;
1971da177e4SLinus Torvalds 
19892737230STrond Myklebust 	fl->fl_ops->fl_release_private(fl);
19992737230STrond Myklebust 	fl->fl_ops = NULL;
20092737230STrond Myklebust 
2011da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
2021da177e4SLinus Torvalds 	current->blocked = oldset;
2031da177e4SLinus Torvalds 	recalc_sigpending();
2041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	dprintk("lockd: clnt proc returns %d\n", status);
2071da177e4SLinus Torvalds 	return status;
2081da177e4SLinus Torvalds }
2091093a60eSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_proc);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /*
2121da177e4SLinus Torvalds  * Allocate an NLM RPC call struct
21392737230STrond Myklebust  *
21492737230STrond Myklebust  * Note: the caller must hold a reference to host. In case of failure,
21592737230STrond Myklebust  * this reference will be released.
2161da177e4SLinus Torvalds  */
21792737230STrond Myklebust struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct nlm_rqst	*call;
2201da177e4SLinus Torvalds 
22136943fa4STrond Myklebust 	for(;;) {
22236943fa4STrond Myklebust 		call = kzalloc(sizeof(*call), GFP_KERNEL);
22336943fa4STrond Myklebust 		if (call != NULL) {
224*5e7f37a7STrond Myklebust 			atomic_set(&call->a_count, 1);
2251da177e4SLinus Torvalds 			locks_init_lock(&call->a_args.lock.fl);
2261da177e4SLinus Torvalds 			locks_init_lock(&call->a_res.lock.fl);
22792737230STrond Myklebust 			call->a_host = host;
2281da177e4SLinus Torvalds 			return call;
2291da177e4SLinus Torvalds 		}
23036943fa4STrond Myklebust 		if (signalled())
23136943fa4STrond Myklebust 			break;
23292737230STrond Myklebust 		printk("nlm_alloc_call: failed, waiting for memory\n");
233041e0e3bSNishanth Aravamudan 		schedule_timeout_interruptible(5*HZ);
2341da177e4SLinus Torvalds 	}
23592737230STrond Myklebust 	nlm_release_host(host);
2361da177e4SLinus Torvalds 	return NULL;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
23992737230STrond Myklebust void nlm_release_call(struct nlm_rqst *call)
24092737230STrond Myklebust {
241*5e7f37a7STrond Myklebust 	if (!atomic_dec_and_test(&call->a_count))
242*5e7f37a7STrond Myklebust 		return;
24392737230STrond Myklebust 	nlm_release_host(call->a_host);
24492737230STrond Myklebust 	nlmclnt_release_lockargs(call);
24592737230STrond Myklebust 	kfree(call);
24692737230STrond Myklebust }
24792737230STrond Myklebust 
24892737230STrond Myklebust static void nlmclnt_rpc_release(void *data)
24992737230STrond Myklebust {
25065fdf7d2STrond Myklebust 	nlm_release_call(data);
25192737230STrond Myklebust }
25292737230STrond Myklebust 
2531da177e4SLinus Torvalds static int nlm_wait_on_grace(wait_queue_head_t *queue)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
2561da177e4SLinus Torvalds 	int status = -EINTR;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
2591da177e4SLinus Torvalds 	if (!signalled ()) {
2601da177e4SLinus Torvalds 		schedule_timeout(NLMCLNT_GRACE_WAIT);
2613e1d1d28SChristoph Lameter 		try_to_freeze();
2621da177e4SLinus Torvalds 		if (!signalled ())
2631da177e4SLinus Torvalds 			status = 0;
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 	finish_wait(queue, &wait);
2661da177e4SLinus Torvalds 	return status;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /*
2701da177e4SLinus Torvalds  * Generic NLM call
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds static int
2731da177e4SLinus Torvalds nlmclnt_call(struct nlm_rqst *req, u32 proc)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
2761da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
2771da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
2781da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
2791da177e4SLinus Torvalds 	struct rpc_message msg = {
2801da177e4SLinus Torvalds 		.rpc_argp	= argp,
2811da177e4SLinus Torvalds 		.rpc_resp	= resp,
2821da177e4SLinus Torvalds 	};
2831da177e4SLinus Torvalds 	int		status;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s\n",
2861da177e4SLinus Torvalds 			(int)proc, host->h_name);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	do {
2891da177e4SLinus Torvalds 		if (host->h_reclaiming && !argp->reclaim)
2901da177e4SLinus Torvalds 			goto in_grace_period;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 		/* If we have no RPC client yet, create one. */
2931da177e4SLinus Torvalds 		if ((clnt = nlm_bind_host(host)) == NULL)
2941da177e4SLinus Torvalds 			return -ENOLCK;
2951da177e4SLinus Torvalds 		msg.rpc_proc = &clnt->cl_procinfo[proc];
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 		/* Perform the RPC call. If an error occurs, try again */
2981da177e4SLinus Torvalds 		if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
2991da177e4SLinus Torvalds 			dprintk("lockd: rpc_call returned error %d\n", -status);
3001da177e4SLinus Torvalds 			switch (status) {
3011da177e4SLinus Torvalds 			case -EPROTONOSUPPORT:
3021da177e4SLinus Torvalds 				status = -EINVAL;
3031da177e4SLinus Torvalds 				break;
3041da177e4SLinus Torvalds 			case -ECONNREFUSED:
3051da177e4SLinus Torvalds 			case -ETIMEDOUT:
3061da177e4SLinus Torvalds 			case -ENOTCONN:
3071da177e4SLinus Torvalds 				nlm_rebind_host(host);
3081da177e4SLinus Torvalds 				status = -EAGAIN;
3091da177e4SLinus Torvalds 				break;
3101da177e4SLinus Torvalds 			case -ERESTARTSYS:
3111da177e4SLinus Torvalds 				return signalled () ? -EINTR : status;
3121da177e4SLinus Torvalds 			default:
3131da177e4SLinus Torvalds 				break;
3141da177e4SLinus Torvalds 			}
3151da177e4SLinus Torvalds 			break;
3161da177e4SLinus Torvalds 		} else
317e8c5c045SAl Viro 		if (resp->status == nlm_lck_denied_grace_period) {
3181da177e4SLinus Torvalds 			dprintk("lockd: server in grace period\n");
3191da177e4SLinus Torvalds 			if (argp->reclaim) {
3201da177e4SLinus Torvalds 				printk(KERN_WARNING
3211da177e4SLinus Torvalds 				     "lockd: spurious grace period reject?!\n");
3221da177e4SLinus Torvalds 				return -ENOLCK;
3231da177e4SLinus Torvalds 			}
3241da177e4SLinus Torvalds 		} else {
3251da177e4SLinus Torvalds 			if (!argp->reclaim) {
3261da177e4SLinus Torvalds 				/* We appear to be out of the grace period */
3271da177e4SLinus Torvalds 				wake_up_all(&host->h_gracewait);
3281da177e4SLinus Torvalds 			}
3291da177e4SLinus Torvalds 			dprintk("lockd: server returns status %d\n", resp->status);
3301da177e4SLinus Torvalds 			return 0;	/* Okay, call complete */
3311da177e4SLinus Torvalds 		}
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds in_grace_period:
3341da177e4SLinus Torvalds 		/*
3351da177e4SLinus Torvalds 		 * The server has rebooted and appears to be in the grace
3361da177e4SLinus Torvalds 		 * period during which locks are only allowed to be
3371da177e4SLinus Torvalds 		 * reclaimed.
3381da177e4SLinus Torvalds 		 * We can only back off and try again later.
3391da177e4SLinus Torvalds 		 */
3401da177e4SLinus Torvalds 		status = nlm_wait_on_grace(&host->h_gracewait);
3411da177e4SLinus Torvalds 	} while (status == 0);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	return status;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*
3471da177e4SLinus Torvalds  * Generic NLM call, async version.
3481da177e4SLinus Torvalds  */
349d4716624STrond Myklebust static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
3521da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s (async)\n",
3551da177e4SLinus Torvalds 			(int)proc, host->h_name);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* If we have no RPC client yet, create one. */
35892737230STrond Myklebust 	clnt = nlm_bind_host(host);
35992737230STrond Myklebust 	if (clnt == NULL)
36092737230STrond Myklebust 		goto out_err;
361d4716624STrond Myklebust 	msg->rpc_proc = &clnt->cl_procinfo[proc];
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds         /* bootstrap and kick off the async RPC call */
364a995e9ebSTrond Myklebust         return rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
36592737230STrond Myklebust out_err:
366a995e9ebSTrond Myklebust 	tk_ops->rpc_release(req);
367a995e9ebSTrond Myklebust 	return -ENOLCK;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
370d4716624STrond Myklebust int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
371d4716624STrond Myklebust {
372d4716624STrond Myklebust 	struct rpc_message msg = {
373d4716624STrond Myklebust 		.rpc_argp	= &req->a_args,
374d4716624STrond Myklebust 		.rpc_resp	= &req->a_res,
375d4716624STrond Myklebust 	};
376d4716624STrond Myklebust 	return __nlm_async_call(req, proc, &msg, tk_ops);
377d4716624STrond Myklebust }
378d4716624STrond Myklebust 
379d4716624STrond Myklebust int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
380d4716624STrond Myklebust {
381d4716624STrond Myklebust 	struct rpc_message msg = {
382d4716624STrond Myklebust 		.rpc_argp	= &req->a_res,
383d4716624STrond Myklebust 	};
384d4716624STrond Myklebust 	return __nlm_async_call(req, proc, &msg, tk_ops);
385d4716624STrond Myklebust }
386d4716624STrond Myklebust 
3871da177e4SLinus Torvalds /*
3881da177e4SLinus Torvalds  * TEST for the presence of a conflicting lock
3891da177e4SLinus Torvalds  */
3901da177e4SLinus Torvalds static int
3911da177e4SLinus Torvalds nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	int	status;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	status = nlmclnt_call(req, NLMPROC_TEST);
3961da177e4SLinus Torvalds 	if (status < 0)
39792737230STrond Myklebust 		goto out;
3981da177e4SLinus Torvalds 
39992737230STrond Myklebust 	switch (req->a_res.status) {
400e8c5c045SAl Viro 		case nlm_granted:
4011da177e4SLinus Torvalds 			fl->fl_type = F_UNLCK;
40292737230STrond Myklebust 			break;
403e8c5c045SAl Viro 		case nlm_lck_denied:
4041da177e4SLinus Torvalds 			/*
4051da177e4SLinus Torvalds 			 * Report the conflicting lock back to the application.
4061da177e4SLinus Torvalds 			 */
407e4cd038aSTrond Myklebust 			fl->fl_start = req->a_res.lock.fl.fl_start;
408e4cd038aSTrond Myklebust 			fl->fl_end = req->a_res.lock.fl.fl_start;
409e4cd038aSTrond Myklebust 			fl->fl_type = req->a_res.lock.fl.fl_type;
4101da177e4SLinus Torvalds 			fl->fl_pid = 0;
41192737230STrond Myklebust 			break;
41292737230STrond Myklebust 		default:
41392737230STrond Myklebust 			status = nlm_stat_to_errno(req->a_res.status);
4141da177e4SLinus Torvalds 	}
41592737230STrond Myklebust out:
41692737230STrond Myklebust 	nlm_release_call(req);
41792737230STrond Myklebust 	return status;
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4211da177e4SLinus Torvalds {
4224c060b53STrond Myklebust 	new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
4234c060b53STrond Myklebust 	new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
4244c060b53STrond Myklebust 	list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds static void nlmclnt_locks_release_private(struct file_lock *fl)
4281da177e4SLinus Torvalds {
4294c060b53STrond Myklebust 	list_del(&fl->fl_u.nfs_fl.list);
4301da177e4SLinus Torvalds 	nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static struct file_lock_operations nlmclnt_lock_ops = {
4341da177e4SLinus Torvalds 	.fl_copy_lock = nlmclnt_locks_copy_lock,
4351da177e4SLinus Torvalds 	.fl_release_private = nlmclnt_locks_release_private,
4361da177e4SLinus Torvalds };
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	BUG_ON(fl->fl_ops != NULL);
4411da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.state = 0;
4421da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
4434c060b53STrond Myklebust 	INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
4441da177e4SLinus Torvalds 	fl->fl_ops = &nlmclnt_lock_ops;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4479b073574STrond Myklebust static int do_vfs_lock(struct file_lock *fl)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	int res = 0;
4501da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4511da177e4SLinus Torvalds 		case FL_POSIX:
4521da177e4SLinus Torvalds 			res = posix_lock_file_wait(fl->fl_file, fl);
4531da177e4SLinus Torvalds 			break;
4541da177e4SLinus Torvalds 		case FL_FLOCK:
4551da177e4SLinus Torvalds 			res = flock_lock_file_wait(fl->fl_file, fl);
4561da177e4SLinus Torvalds 			break;
4571da177e4SLinus Torvalds 		default:
4581da177e4SLinus Torvalds 			BUG();
4591da177e4SLinus Torvalds 	}
4609b073574STrond Myklebust 	return res;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4641da177e4SLinus Torvalds  * LOCK: Try to create a lock
4651da177e4SLinus Torvalds  *
4661da177e4SLinus Torvalds  *			Programmer Harassment Alert
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  * When given a blocking lock request in a sync RPC call, the HPUX lockd
4691da177e4SLinus Torvalds  * will faithfully return LCK_BLOCKED but never cares to notify us when
4701da177e4SLinus Torvalds  * the lock could be granted. This way, our local process could hang
4711da177e4SLinus Torvalds  * around forever waiting for the callback.
4721da177e4SLinus Torvalds  *
4731da177e4SLinus Torvalds  *  Solution A:	Implement busy-waiting
4741da177e4SLinus Torvalds  *  Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
4751da177e4SLinus Torvalds  *
4761da177e4SLinus Torvalds  * For now I am implementing solution A, because I hate the idea of
4771da177e4SLinus Torvalds  * re-implementing lockd for a third time in two months. The async
4781da177e4SLinus Torvalds  * calls shouldn't be too hard to do, however.
4791da177e4SLinus Torvalds  *
4801da177e4SLinus Torvalds  * This is one of the lovely things about standards in the NFS area:
4811da177e4SLinus Torvalds  * they're so soft and squishy you can't really blame HP for doing this.
4821da177e4SLinus Torvalds  */
4831da177e4SLinus Torvalds static int
4841da177e4SLinus Torvalds nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
4871da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
4883a649b88STrond Myklebust 	struct nlm_wait *block = NULL;
48901c3b861STrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
4903a649b88STrond Myklebust 	int status = -ENOLCK;
4911da177e4SLinus Torvalds 
492977faf39SOlaf Kirch 	if (nsm_monitor(host) < 0) {
4931da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: failed to monitor %s\n",
4941da177e4SLinus Torvalds 					host->h_name);
4951da177e4SLinus Torvalds 		goto out;
4961da177e4SLinus Torvalds 	}
49701c3b861STrond Myklebust 	fl->fl_flags |= FL_ACCESS;
49801c3b861STrond Myklebust 	status = do_vfs_lock(fl);
4994a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
50001c3b861STrond Myklebust 	if (status < 0)
50101c3b861STrond Myklebust 		goto out;
5021da177e4SLinus Torvalds 
5033a649b88STrond Myklebust 	block = nlmclnt_prepare_block(host, fl);
50428df955aSTrond Myklebust again:
505ecdbf769STrond Myklebust 	for(;;) {
50628df955aSTrond Myklebust 		/* Reboot protection */
50728df955aSTrond Myklebust 		fl->fl_u.nfs_fl.state = host->h_state;
508ecdbf769STrond Myklebust 		status = nlmclnt_call(req, NLMPROC_LOCK);
509ecdbf769STrond Myklebust 		if (status < 0)
510ecdbf769STrond Myklebust 			goto out_unblock;
5113a649b88STrond Myklebust 		if (!req->a_args.block)
512ecdbf769STrond Myklebust 			break;
513ecdbf769STrond Myklebust 		/* Did a reclaimer thread notify us of a server reboot? */
514e8c5c045SAl Viro 		if (resp->status ==  nlm_lck_denied_grace_period)
515ecdbf769STrond Myklebust 			continue;
516e8c5c045SAl Viro 		if (resp->status != nlm_lck_blocked)
517ecdbf769STrond Myklebust 			break;
5183a649b88STrond Myklebust 		/* Wait on an NLM blocking lock */
5193a649b88STrond Myklebust 		status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
5203a649b88STrond Myklebust 		/* if we were interrupted. Send a CANCEL request to the server
521ecdbf769STrond Myklebust 		 * and exit
522ecdbf769STrond Myklebust 		 */
5233a649b88STrond Myklebust 		if (status < 0)
524ecdbf769STrond Myklebust 			goto out_unblock;
525e8c5c045SAl Viro 		if (resp->status != nlm_lck_blocked)
5263a649b88STrond Myklebust 			break;
527ecdbf769STrond Myklebust 	}
5281da177e4SLinus Torvalds 
529e8c5c045SAl Viro 	if (resp->status == nlm_granted) {
53028df955aSTrond Myklebust 		down_read(&host->h_rwsem);
53128df955aSTrond Myklebust 		/* Check whether or not the server has rebooted */
53228df955aSTrond Myklebust 		if (fl->fl_u.nfs_fl.state != host->h_state) {
53328df955aSTrond Myklebust 			up_read(&host->h_rwsem);
53428df955aSTrond Myklebust 			goto again;
53528df955aSTrond Myklebust 		}
5364c060b53STrond Myklebust 		/* Ensure the resulting lock will get added to granted list */
5374a9af59fSTrond Myklebust 		fl->fl_flags |= FL_SLEEP;
5389b073574STrond Myklebust 		if (do_vfs_lock(fl) < 0)
5399b073574STrond Myklebust 			printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
54028df955aSTrond Myklebust 		up_read(&host->h_rwsem);
5414a9af59fSTrond Myklebust 		fl->fl_flags = fl_flags;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 	status = nlm_stat_to_errno(resp->status);
544ecdbf769STrond Myklebust out_unblock:
5453a649b88STrond Myklebust 	nlmclnt_finish_block(block);
546ecdbf769STrond Myklebust 	/* Cancel the blocked request if it is still pending */
547e8c5c045SAl Viro 	if (resp->status == nlm_lck_blocked)
54816fb2425STrond Myklebust 		nlmclnt_cancel(host, req->a_args.block, fl);
5491da177e4SLinus Torvalds out:
55092737230STrond Myklebust 	nlm_release_call(req);
5511da177e4SLinus Torvalds 	return status;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds  * RECLAIM: Try to reclaim a lock
5561da177e4SLinus Torvalds  */
5571da177e4SLinus Torvalds int
5581da177e4SLinus Torvalds nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	struct nlm_rqst reqst, *req;
5611da177e4SLinus Torvalds 	int		status;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	req = &reqst;
5641da177e4SLinus Torvalds 	memset(req, 0, sizeof(*req));
5651da177e4SLinus Torvalds 	locks_init_lock(&req->a_args.lock.fl);
5661da177e4SLinus Torvalds 	locks_init_lock(&req->a_res.lock.fl);
5671da177e4SLinus Torvalds 	req->a_host  = host;
5681da177e4SLinus Torvalds 	req->a_flags = 0;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/* Set up the argument struct */
5711da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
5721da177e4SLinus Torvalds 	req->a_args.reclaim = 1;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
575e8c5c045SAl Viro 	 && req->a_res.status == nlm_granted)
5761da177e4SLinus Torvalds 		return 0;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
5791da177e4SLinus Torvalds 				"(errno %d, status %d)\n", fl->fl_pid,
580e8c5c045SAl Viro 				status, ntohl(req->a_res.status));
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/*
5831da177e4SLinus Torvalds 	 * FIXME: This is a serious failure. We can
5841da177e4SLinus Torvalds 	 *
5851da177e4SLinus Torvalds 	 *  a.	Ignore the problem
5861da177e4SLinus Torvalds 	 *  b.	Send the owning process some signal (Linux doesn't have
5871da177e4SLinus Torvalds 	 *	SIGLOST, though...)
5881da177e4SLinus Torvalds 	 *  c.	Retry the operation
5891da177e4SLinus Torvalds 	 *
5901da177e4SLinus Torvalds 	 * Until someone comes up with a simple implementation
5911da177e4SLinus Torvalds 	 * for b or c, I'll choose option a.
5921da177e4SLinus Torvalds 	 */
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	return -ENOLCK;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /*
5981da177e4SLinus Torvalds  * UNLOCK: remove an existing lock
5991da177e4SLinus Torvalds  */
6001da177e4SLinus Torvalds static int
6011da177e4SLinus Torvalds nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
6021da177e4SLinus Torvalds {
60328df955aSTrond Myklebust 	struct nlm_host	*host = req->a_host;
6041da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
6054a9af59fSTrond Myklebust 	int status;
6064a9af59fSTrond Myklebust 	unsigned char fl_flags = fl->fl_flags;
6071da177e4SLinus Torvalds 
60826bcbf96SChristoph Hellwig 	/*
60930f4e20aSTrond Myklebust 	 * Note: the server is supposed to either grant us the unlock
61030f4e20aSTrond Myklebust 	 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
61130f4e20aSTrond Myklebust 	 * case, we want to unlock.
61230f4e20aSTrond Myklebust 	 */
6139b073574STrond Myklebust 	fl->fl_flags |= FL_EXISTS;
61428df955aSTrond Myklebust 	down_read(&host->h_rwsem);
6154a9af59fSTrond Myklebust 	status = do_vfs_lock(fl);
6169b073574STrond Myklebust 	up_read(&host->h_rwsem);
6174a9af59fSTrond Myklebust 	fl->fl_flags = fl_flags;
6184a9af59fSTrond Myklebust 	if (status == -ENOENT) {
6194a9af59fSTrond Myklebust 		status = 0;
6209b073574STrond Myklebust 		goto out;
6219b073574STrond Myklebust 	}
62230f4e20aSTrond Myklebust 
62392737230STrond Myklebust 	if (req->a_flags & RPC_TASK_ASYNC)
62492737230STrond Myklebust 		return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	status = nlmclnt_call(req, NLMPROC_UNLOCK);
6271da177e4SLinus Torvalds 	if (status < 0)
62892737230STrond Myklebust 		goto out;
6291da177e4SLinus Torvalds 
630e8c5c045SAl Viro 	if (resp->status == nlm_granted)
63192737230STrond Myklebust 		goto out;
6321da177e4SLinus Torvalds 
633e8c5c045SAl Viro 	if (resp->status != nlm_lck_denied_nolocks)
6341da177e4SLinus Torvalds 		printk("lockd: unexpected unlock status: %d\n", resp->status);
6351da177e4SLinus Torvalds 	/* What to do now? I'm out of my depth... */
63692737230STrond Myklebust 	status = -ENOLCK;
63792737230STrond Myklebust out:
63892737230STrond Myklebust 	nlm_release_call(req);
63992737230STrond Myklebust 	return status;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
642963d8fe5STrond Myklebust static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
6431da177e4SLinus Torvalds {
644963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
645e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
6481da177e4SLinus Torvalds 		goto die;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	if (task->tk_status < 0) {
6511da177e4SLinus Torvalds 		dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
6521da177e4SLinus Torvalds 		goto retry_rebind;
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
6551da177e4SLinus Torvalds 		rpc_delay(task, NLMCLNT_GRACE_WAIT);
6561da177e4SLinus Torvalds 		goto retry_unlock;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 	if (status != NLM_LCK_GRANTED)
6591da177e4SLinus Torvalds 		printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
6601da177e4SLinus Torvalds die:
6611da177e4SLinus Torvalds 	return;
6621da177e4SLinus Torvalds  retry_rebind:
6631da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
6641da177e4SLinus Torvalds  retry_unlock:
6651da177e4SLinus Torvalds 	rpc_restart_call(task);
6661da177e4SLinus Torvalds }
6671da177e4SLinus Torvalds 
668963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops = {
669963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_unlock_callback,
67092737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
671963d8fe5STrond Myklebust };
672963d8fe5STrond Myklebust 
6731da177e4SLinus Torvalds /*
6741da177e4SLinus Torvalds  * Cancel a blocked lock request.
6751da177e4SLinus Torvalds  * We always use an async RPC call for this in order not to hang a
6761da177e4SLinus Torvalds  * process that has been Ctrl-C'ed.
6771da177e4SLinus Torvalds  */
67816fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct nlm_rqst	*req;
6811da177e4SLinus Torvalds 	unsigned long	flags;
6821da177e4SLinus Torvalds 	sigset_t	oldset;
6831da177e4SLinus Torvalds 	int		status;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/* Block all signals while setting up call */
6861da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
6871da177e4SLinus Torvalds 	oldset = current->blocked;
6881da177e4SLinus Torvalds 	sigfillset(&current->blocked);
6891da177e4SLinus Torvalds 	recalc_sigpending();
6901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
6911da177e4SLinus Torvalds 
69292737230STrond Myklebust 	req = nlm_alloc_call(nlm_get_host(host));
6931da177e4SLinus Torvalds 	if (!req)
6941da177e4SLinus Torvalds 		return -ENOMEM;
6951da177e4SLinus Torvalds 	req->a_flags = RPC_TASK_ASYNC;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
69816fb2425STrond Myklebust 	req->a_args.block = block;
6991da177e4SLinus Torvalds 
70092737230STrond Myklebust 	status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
7031da177e4SLinus Torvalds 	current->blocked = oldset;
7041da177e4SLinus Torvalds 	recalc_sigpending();
7051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	return status;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
710963d8fe5STrond Myklebust static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
7111da177e4SLinus Torvalds {
712963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
713e8c5c045SAl Viro 	u32 status = ntohl(req->a_res.status);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
7161da177e4SLinus Torvalds 		goto die;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (task->tk_status < 0) {
7191da177e4SLinus Torvalds 		dprintk("lockd: CANCEL call error %d, retrying.\n",
7201da177e4SLinus Torvalds 					task->tk_status);
7211da177e4SLinus Torvalds 		goto retry_cancel;
7221da177e4SLinus Torvalds 	}
7231da177e4SLinus Torvalds 
724c041b5ffSChuck Lever 	dprintk("lockd: cancel status %u (task %u)\n",
725e8c5c045SAl Viro 			status, task->tk_pid);
7261da177e4SLinus Torvalds 
727e8c5c045SAl Viro 	switch (status) {
7281da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
7291da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
73035576cbaSTrond Myklebust 	case NLM_LCK_DENIED:
7311da177e4SLinus Torvalds 		/* Everything's good */
7321da177e4SLinus Torvalds 		break;
7331da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
7341da177e4SLinus Torvalds 		dprintk("lockd: CANCEL failed (server has no locks)\n");
7351da177e4SLinus Torvalds 		goto retry_cancel;
7361da177e4SLinus Torvalds 	default:
7371da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
738e8c5c045SAl Viro 			status);
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds die:
7421da177e4SLinus Torvalds 	return;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds retry_cancel:
745aaaa9942STrond Myklebust 	/* Don't ever retry more than 3 times */
746aaaa9942STrond Myklebust 	if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
747aaaa9942STrond Myklebust 		goto die;
7481da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
7491da177e4SLinus Torvalds 	rpc_restart_call(task);
7501da177e4SLinus Torvalds 	rpc_delay(task, 30 * HZ);
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
753963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops = {
754963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_cancel_callback,
75592737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
756963d8fe5STrond Myklebust };
757963d8fe5STrond Myklebust 
7581da177e4SLinus Torvalds /*
7591da177e4SLinus Torvalds  * Convert an NLM status code to a generic kernel errno
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds static int
762e8c5c045SAl Viro nlm_stat_to_errno(__be32 status)
7631da177e4SLinus Torvalds {
764e8c5c045SAl Viro 	switch(ntohl(status)) {
7651da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
7661da177e4SLinus Torvalds 		return 0;
7671da177e4SLinus Torvalds 	case NLM_LCK_DENIED:
7681da177e4SLinus Torvalds 		return -EAGAIN;
7691da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
7701da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
7711da177e4SLinus Torvalds 		return -ENOLCK;
7721da177e4SLinus Torvalds 	case NLM_LCK_BLOCKED:
7731da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
7741da177e4SLinus Torvalds 		return -ENOLCK;
7751da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
7761da177e4SLinus Torvalds 	case NLM_DEADLCK:
7771da177e4SLinus Torvalds 		return -EDEADLK;
7781da177e4SLinus Torvalds 	case NLM_ROFS:
7791da177e4SLinus Torvalds 		return -EROFS;
7801da177e4SLinus Torvalds 	case NLM_STALE_FH:
7811da177e4SLinus Torvalds 		return -ESTALE;
7821da177e4SLinus Torvalds 	case NLM_FBIG:
7831da177e4SLinus Torvalds 		return -EOVERFLOW;
7841da177e4SLinus Torvalds 	case NLM_FAILED:
7851da177e4SLinus Torvalds 		return -ENOLCK;
7861da177e4SLinus Torvalds #endif
7871da177e4SLinus Torvalds 	}
7881da177e4SLinus Torvalds 	printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
7891da177e4SLinus Torvalds 	return -ENOLCK;
7901da177e4SLinus Torvalds }
791