xref: /openbmc/linux/fs/lockd/clntproc.c (revision 28df955a2ad484d602314b30183ea8496a9aa34a)
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/config.h>
101da177e4SLinus Torvalds #include <linux/module.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>
161da177e4SLinus Torvalds #include <linux/smp_lock.h>
171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
181da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
191da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
201da177e4SLinus Torvalds #include <linux/lockd/sm_inter.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
231da177e4SLinus Torvalds #define NLMCLNT_GRACE_WAIT	(5*HZ)
24ecdbf769STrond Myklebust #define NLMCLNT_POLL_TIMEOUT	(30*HZ)
25aaaa9942STrond Myklebust #define NLMCLNT_MAX_RETRIES	3
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds static int	nlmclnt_test(struct nlm_rqst *, struct file_lock *);
281da177e4SLinus Torvalds static int	nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
291da177e4SLinus Torvalds static int	nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
301da177e4SLinus Torvalds static int	nlm_stat_to_errno(u32 stat);
311da177e4SLinus Torvalds static void	nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
3216fb2425STrond Myklebust static int	nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
331da177e4SLinus Torvalds 
34963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops;
35963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops;
36963d8fe5STrond Myklebust 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  * Cookie counter for NLM requests
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds static u32	nlm_cookie = 0x1234;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	memcpy(c->data, &nlm_cookie, 4);
451da177e4SLinus Torvalds 	memset(c->data+4, 0, 4);
461da177e4SLinus Torvalds 	c->len=4;
471da177e4SLinus Torvalds 	nlm_cookie++;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	atomic_inc(&lockowner->count);
531da177e4SLinus Torvalds 	return lockowner;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
591da177e4SLinus Torvalds 		return;
601da177e4SLinus Torvalds 	list_del(&lockowner->list);
611da177e4SLinus Torvalds 	spin_unlock(&lockowner->host->h_lock);
621da177e4SLinus Torvalds 	nlm_release_host(lockowner->host);
631da177e4SLinus Torvalds 	kfree(lockowner);
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
671da177e4SLinus Torvalds {
681da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
691da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
701da177e4SLinus Torvalds 		if (lockowner->pid == pid)
711da177e4SLinus Torvalds 			return -EBUSY;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 	return 0;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	uint32_t res;
791da177e4SLinus Torvalds 	do {
801da177e4SLinus Torvalds 		res = host->h_pidcount++;
811da177e4SLinus Torvalds 	} while (nlm_pidbusy(host, res) < 0);
821da177e4SLinus Torvalds 	return res;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct nlm_lockowner *lockowner;
881da177e4SLinus Torvalds 	list_for_each_entry(lockowner, &host->h_lockowners, list) {
891da177e4SLinus Torvalds 		if (lockowner->owner != owner)
901da177e4SLinus Torvalds 			continue;
911da177e4SLinus Torvalds 		return nlm_get_lockowner(lockowner);
921da177e4SLinus Torvalds 	}
931da177e4SLinus Torvalds 	return NULL;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	struct nlm_lockowner *res, *new = NULL;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	spin_lock(&host->h_lock);
1011da177e4SLinus Torvalds 	res = __nlm_find_lockowner(host, owner);
1021da177e4SLinus Torvalds 	if (res == NULL) {
1031da177e4SLinus Torvalds 		spin_unlock(&host->h_lock);
1041da177e4SLinus Torvalds 		new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL);
1051da177e4SLinus Torvalds 		spin_lock(&host->h_lock);
1061da177e4SLinus Torvalds 		res = __nlm_find_lockowner(host, owner);
1071da177e4SLinus Torvalds 		if (res == NULL && new != NULL) {
1081da177e4SLinus Torvalds 			res = new;
1091da177e4SLinus Torvalds 			atomic_set(&new->count, 1);
1101da177e4SLinus Torvalds 			new->owner = owner;
1111da177e4SLinus Torvalds 			new->pid = __nlm_alloc_pid(host);
1121da177e4SLinus Torvalds 			new->host = nlm_get_host(host);
1131da177e4SLinus Torvalds 			list_add(&new->list, &host->h_lockowners);
1141da177e4SLinus Torvalds 			new = NULL;
1151da177e4SLinus Torvalds 		}
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 	spin_unlock(&host->h_lock);
1181da177e4SLinus Torvalds 	kfree(new);
1191da177e4SLinus Torvalds 	return res;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
1281da177e4SLinus Torvalds 	struct nlm_lock	*lock = &argp->lock;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	nlmclnt_next_cookie(&argp->cookie);
1311da177e4SLinus Torvalds 	argp->state   = nsm_local_state;
1321da177e4SLinus Torvalds 	memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
1331da177e4SLinus Torvalds 	lock->caller  = system_utsname.nodename;
1341da177e4SLinus Torvalds 	lock->oh.data = req->a_owner;
1357bab377fSTrond Myklebust 	lock->oh.len  = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
1367bab377fSTrond Myklebust 				(unsigned int)fl->fl_u.nfs_fl.owner->pid,
1377bab377fSTrond Myklebust 				system_utsname.nodename);
1387bab377fSTrond Myklebust 	lock->svid = fl->fl_u.nfs_fl.owner->pid;
1393a649b88STrond Myklebust 	lock->fl.fl_start = fl->fl_start;
1403a649b88STrond Myklebust 	lock->fl.fl_end = fl->fl_end;
1413a649b88STrond Myklebust 	lock->fl.fl_type = fl->fl_type;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static void nlmclnt_release_lockargs(struct nlm_rqst *req)
1451da177e4SLinus Torvalds {
1463a649b88STrond Myklebust 	BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * This is the main entry point for the NLM client.
1511da177e4SLinus Torvalds  */
1521da177e4SLinus Torvalds int
1531da177e4SLinus Torvalds nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	struct nlm_host		*host;
15692737230STrond Myklebust 	struct nlm_rqst		*call;
1571da177e4SLinus Torvalds 	sigset_t		oldset;
1581da177e4SLinus Torvalds 	unsigned long		flags;
1591da177e4SLinus Torvalds 	int			status, proto, vers;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
1621da177e4SLinus Torvalds 	if (NFS_PROTO(inode)->version > 3) {
1631da177e4SLinus Torvalds 		printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
1641da177e4SLinus Torvalds 		return -ENOLCK;
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	/* Retrieve transport protocol from NFS client */
1681da177e4SLinus Torvalds 	proto = NFS_CLIENT(inode)->cl_xprt->prot;
1691da177e4SLinus Torvalds 
17092737230STrond Myklebust 	host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
17192737230STrond Myklebust 	if (host == NULL)
1721da177e4SLinus Torvalds 		return -ENOLCK;
1731da177e4SLinus Torvalds 
17492737230STrond Myklebust 	call = nlm_alloc_call(host);
17592737230STrond Myklebust 	if (call == NULL)
17692737230STrond Myklebust 		return -ENOMEM;
1771da177e4SLinus Torvalds 
17892737230STrond Myklebust 	nlmclnt_locks_init_private(fl, host);
17992737230STrond Myklebust 	/* Set up the argument struct */
18092737230STrond Myklebust 	nlmclnt_setlockargs(call, fl);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	/* Keep the old signal mask */
1831da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
1841da177e4SLinus Torvalds 	oldset = current->blocked;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	/* If we're cleaning up locks because the process is exiting,
1871da177e4SLinus Torvalds 	 * perform the RPC call asynchronously. */
1881da177e4SLinus Torvalds 	if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
1891da177e4SLinus Torvalds 	    && fl->fl_type == F_UNLCK
1901da177e4SLinus Torvalds 	    && (current->flags & PF_EXITING)) {
1911da177e4SLinus Torvalds 		sigfillset(&current->blocked);	/* Mask all signals */
1921da177e4SLinus Torvalds 		recalc_sigpending();
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 		call->a_flags = RPC_TASK_ASYNC;
1951da177e4SLinus Torvalds 	}
19692737230STrond Myklebust 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
1991da177e4SLinus Torvalds 		if (fl->fl_type != F_UNLCK) {
2001da177e4SLinus Torvalds 			call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
2011da177e4SLinus Torvalds 			status = nlmclnt_lock(call, fl);
2021da177e4SLinus Torvalds 		} else
2031da177e4SLinus Torvalds 			status = nlmclnt_unlock(call, fl);
2041da177e4SLinus Torvalds 	} else if (IS_GETLK(cmd))
2051da177e4SLinus Torvalds 		status = nlmclnt_test(call, fl);
2061da177e4SLinus Torvalds 	else
2071da177e4SLinus Torvalds 		status = -EINVAL;
2081da177e4SLinus Torvalds 
20992737230STrond Myklebust 	fl->fl_ops->fl_release_private(fl);
21092737230STrond Myklebust 	fl->fl_ops = NULL;
21192737230STrond Myklebust 
2121da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
2131da177e4SLinus Torvalds 	current->blocked = oldset;
2141da177e4SLinus Torvalds 	recalc_sigpending();
2151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	dprintk("lockd: clnt proc returns %d\n", status);
2181da177e4SLinus Torvalds 	return status;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds EXPORT_SYMBOL(nlmclnt_proc);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /*
2231da177e4SLinus Torvalds  * Allocate an NLM RPC call struct
22492737230STrond Myklebust  *
22592737230STrond Myklebust  * Note: the caller must hold a reference to host. In case of failure,
22692737230STrond Myklebust  * this reference will be released.
2271da177e4SLinus Torvalds  */
22892737230STrond Myklebust struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	struct nlm_rqst	*call;
2311da177e4SLinus Torvalds 
23236943fa4STrond Myklebust 	for(;;) {
23336943fa4STrond Myklebust 		call = kzalloc(sizeof(*call), GFP_KERNEL);
23436943fa4STrond Myklebust 		if (call != NULL) {
2351da177e4SLinus Torvalds 			locks_init_lock(&call->a_args.lock.fl);
2361da177e4SLinus Torvalds 			locks_init_lock(&call->a_res.lock.fl);
23792737230STrond Myklebust 			call->a_host = host;
2381da177e4SLinus Torvalds 			return call;
2391da177e4SLinus Torvalds 		}
24036943fa4STrond Myklebust 		if (signalled())
24136943fa4STrond Myklebust 			break;
24292737230STrond Myklebust 		printk("nlm_alloc_call: failed, waiting for memory\n");
243041e0e3bSNishanth Aravamudan 		schedule_timeout_interruptible(5*HZ);
2441da177e4SLinus Torvalds 	}
24592737230STrond Myklebust 	nlm_release_host(host);
2461da177e4SLinus Torvalds 	return NULL;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
24992737230STrond Myklebust void nlm_release_call(struct nlm_rqst *call)
25092737230STrond Myklebust {
25192737230STrond Myklebust 	nlm_release_host(call->a_host);
25292737230STrond Myklebust 	nlmclnt_release_lockargs(call);
25392737230STrond Myklebust 	kfree(call);
25492737230STrond Myklebust }
25592737230STrond Myklebust 
25692737230STrond Myklebust static void nlmclnt_rpc_release(void *data)
25792737230STrond Myklebust {
25892737230STrond Myklebust 	return nlm_release_call(data);
25992737230STrond Myklebust }
26092737230STrond Myklebust 
2611da177e4SLinus Torvalds static int nlm_wait_on_grace(wait_queue_head_t *queue)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
2641da177e4SLinus Torvalds 	int status = -EINTR;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
2671da177e4SLinus Torvalds 	if (!signalled ()) {
2681da177e4SLinus Torvalds 		schedule_timeout(NLMCLNT_GRACE_WAIT);
2693e1d1d28SChristoph Lameter 		try_to_freeze();
2701da177e4SLinus Torvalds 		if (!signalled ())
2711da177e4SLinus Torvalds 			status = 0;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 	finish_wait(queue, &wait);
2741da177e4SLinus Torvalds 	return status;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /*
2781da177e4SLinus Torvalds  * Generic NLM call
2791da177e4SLinus Torvalds  */
2801da177e4SLinus Torvalds static int
2811da177e4SLinus Torvalds nlmclnt_call(struct nlm_rqst *req, u32 proc)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
2841da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
2851da177e4SLinus Torvalds 	struct nlm_args	*argp = &req->a_args;
2861da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
2871da177e4SLinus Torvalds 	struct rpc_message msg = {
2881da177e4SLinus Torvalds 		.rpc_argp	= argp,
2891da177e4SLinus Torvalds 		.rpc_resp	= resp,
2901da177e4SLinus Torvalds 	};
2911da177e4SLinus Torvalds 	int		status;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s\n",
2941da177e4SLinus Torvalds 			(int)proc, host->h_name);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	do {
2971da177e4SLinus Torvalds 		if (host->h_reclaiming && !argp->reclaim)
2981da177e4SLinus Torvalds 			goto in_grace_period;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 		/* If we have no RPC client yet, create one. */
3011da177e4SLinus Torvalds 		if ((clnt = nlm_bind_host(host)) == NULL)
3021da177e4SLinus Torvalds 			return -ENOLCK;
3031da177e4SLinus Torvalds 		msg.rpc_proc = &clnt->cl_procinfo[proc];
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 		/* Perform the RPC call. If an error occurs, try again */
3061da177e4SLinus Torvalds 		if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
3071da177e4SLinus Torvalds 			dprintk("lockd: rpc_call returned error %d\n", -status);
3081da177e4SLinus Torvalds 			switch (status) {
3091da177e4SLinus Torvalds 			case -EPROTONOSUPPORT:
3101da177e4SLinus Torvalds 				status = -EINVAL;
3111da177e4SLinus Torvalds 				break;
3121da177e4SLinus Torvalds 			case -ECONNREFUSED:
3131da177e4SLinus Torvalds 			case -ETIMEDOUT:
3141da177e4SLinus Torvalds 			case -ENOTCONN:
3151da177e4SLinus Torvalds 				nlm_rebind_host(host);
3161da177e4SLinus Torvalds 				status = -EAGAIN;
3171da177e4SLinus Torvalds 				break;
3181da177e4SLinus Torvalds 			case -ERESTARTSYS:
3191da177e4SLinus Torvalds 				return signalled () ? -EINTR : status;
3201da177e4SLinus Torvalds 			default:
3211da177e4SLinus Torvalds 				break;
3221da177e4SLinus Torvalds 			}
3231da177e4SLinus Torvalds 			break;
3241da177e4SLinus Torvalds 		} else
3251da177e4SLinus Torvalds 		if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
3261da177e4SLinus Torvalds 			dprintk("lockd: server in grace period\n");
3271da177e4SLinus Torvalds 			if (argp->reclaim) {
3281da177e4SLinus Torvalds 				printk(KERN_WARNING
3291da177e4SLinus Torvalds 				     "lockd: spurious grace period reject?!\n");
3301da177e4SLinus Torvalds 				return -ENOLCK;
3311da177e4SLinus Torvalds 			}
3321da177e4SLinus Torvalds 		} else {
3331da177e4SLinus Torvalds 			if (!argp->reclaim) {
3341da177e4SLinus Torvalds 				/* We appear to be out of the grace period */
3351da177e4SLinus Torvalds 				wake_up_all(&host->h_gracewait);
3361da177e4SLinus Torvalds 			}
3371da177e4SLinus Torvalds 			dprintk("lockd: server returns status %d\n", resp->status);
3381da177e4SLinus Torvalds 			return 0;	/* Okay, call complete */
3391da177e4SLinus Torvalds 		}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds in_grace_period:
3421da177e4SLinus Torvalds 		/*
3431da177e4SLinus Torvalds 		 * The server has rebooted and appears to be in the grace
3441da177e4SLinus Torvalds 		 * period during which locks are only allowed to be
3451da177e4SLinus Torvalds 		 * reclaimed.
3461da177e4SLinus Torvalds 		 * We can only back off and try again later.
3471da177e4SLinus Torvalds 		 */
3481da177e4SLinus Torvalds 		status = nlm_wait_on_grace(&host->h_gracewait);
3491da177e4SLinus Torvalds 	} while (status == 0);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	return status;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds /*
3551da177e4SLinus Torvalds  * Generic NLM call, async version.
3561da177e4SLinus Torvalds  */
357d4716624STrond Myklebust static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
3601da177e4SLinus Torvalds 	struct rpc_clnt	*clnt;
36192737230STrond Myklebust 	int status = -ENOLCK;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	dprintk("lockd: call procedure %d on %s (async)\n",
3641da177e4SLinus Torvalds 			(int)proc, host->h_name);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	/* If we have no RPC client yet, create one. */
36792737230STrond Myklebust 	clnt = nlm_bind_host(host);
36892737230STrond Myklebust 	if (clnt == NULL)
36992737230STrond Myklebust 		goto out_err;
370d4716624STrond Myklebust 	msg->rpc_proc = &clnt->cl_procinfo[proc];
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds         /* bootstrap and kick off the async RPC call */
373d4716624STrond Myklebust         status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
37492737230STrond Myklebust 	if (status == 0)
37592737230STrond Myklebust 		return 0;
37692737230STrond Myklebust out_err:
37792737230STrond Myklebust 	nlm_release_call(req);
3781da177e4SLinus Torvalds 	return status;
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
381d4716624STrond Myklebust int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
382d4716624STrond Myklebust {
383d4716624STrond Myklebust 	struct rpc_message msg = {
384d4716624STrond Myklebust 		.rpc_argp	= &req->a_args,
385d4716624STrond Myklebust 		.rpc_resp	= &req->a_res,
386d4716624STrond Myklebust 	};
387d4716624STrond Myklebust 	return __nlm_async_call(req, proc, &msg, tk_ops);
388d4716624STrond Myklebust }
389d4716624STrond Myklebust 
390d4716624STrond Myklebust int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
391d4716624STrond Myklebust {
392d4716624STrond Myklebust 	struct rpc_message msg = {
393d4716624STrond Myklebust 		.rpc_argp	= &req->a_res,
394d4716624STrond Myklebust 	};
395d4716624STrond Myklebust 	return __nlm_async_call(req, proc, &msg, tk_ops);
396d4716624STrond Myklebust }
397d4716624STrond Myklebust 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * TEST for the presence of a conflicting lock
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds static int
4021da177e4SLinus Torvalds nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	int	status;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	status = nlmclnt_call(req, NLMPROC_TEST);
4071da177e4SLinus Torvalds 	if (status < 0)
40892737230STrond Myklebust 		goto out;
4091da177e4SLinus Torvalds 
41092737230STrond Myklebust 	switch (req->a_res.status) {
41192737230STrond Myklebust 		case NLM_LCK_GRANTED:
4121da177e4SLinus Torvalds 			fl->fl_type = F_UNLCK;
41392737230STrond Myklebust 			break;
41492737230STrond Myklebust 		case NLM_LCK_DENIED:
4151da177e4SLinus Torvalds 			/*
4161da177e4SLinus Torvalds 			 * Report the conflicting lock back to the application.
4171da177e4SLinus Torvalds 			 */
418e4cd038aSTrond Myklebust 			fl->fl_start = req->a_res.lock.fl.fl_start;
419e4cd038aSTrond Myklebust 			fl->fl_end = req->a_res.lock.fl.fl_start;
420e4cd038aSTrond Myklebust 			fl->fl_type = req->a_res.lock.fl.fl_type;
4211da177e4SLinus Torvalds 			fl->fl_pid = 0;
42292737230STrond Myklebust 			break;
42392737230STrond Myklebust 		default:
42492737230STrond Myklebust 			status = nlm_stat_to_errno(req->a_res.status);
4251da177e4SLinus Torvalds 	}
42692737230STrond Myklebust out:
42792737230STrond Myklebust 	nlm_release_call(req);
42892737230STrond Myklebust 	return status;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
4321da177e4SLinus Torvalds {
4334c060b53STrond Myklebust 	new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
4344c060b53STrond Myklebust 	new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
4354c060b53STrond Myklebust 	list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds static void nlmclnt_locks_release_private(struct file_lock *fl)
4391da177e4SLinus Torvalds {
4404c060b53STrond Myklebust 	list_del(&fl->fl_u.nfs_fl.list);
4411da177e4SLinus Torvalds 	nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds static struct file_lock_operations nlmclnt_lock_ops = {
4451da177e4SLinus Torvalds 	.fl_copy_lock = nlmclnt_locks_copy_lock,
4461da177e4SLinus Torvalds 	.fl_release_private = nlmclnt_locks_release_private,
4471da177e4SLinus Torvalds };
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	BUG_ON(fl->fl_ops != NULL);
4521da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.state = 0;
4531da177e4SLinus Torvalds 	fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
4544c060b53STrond Myklebust 	INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
4551da177e4SLinus Torvalds 	fl->fl_ops = &nlmclnt_lock_ops;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds static void do_vfs_lock(struct file_lock *fl)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	int res = 0;
4611da177e4SLinus Torvalds 	switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4621da177e4SLinus Torvalds 		case FL_POSIX:
4631da177e4SLinus Torvalds 			res = posix_lock_file_wait(fl->fl_file, fl);
4641da177e4SLinus Torvalds 			break;
4651da177e4SLinus Torvalds 		case FL_FLOCK:
4661da177e4SLinus Torvalds 			res = flock_lock_file_wait(fl->fl_file, fl);
4671da177e4SLinus Torvalds 			break;
4681da177e4SLinus Torvalds 		default:
4691da177e4SLinus Torvalds 			BUG();
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 	if (res < 0)
4721da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
4731da177e4SLinus Torvalds 				__FUNCTION__);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * LOCK: Try to create a lock
4781da177e4SLinus Torvalds  *
4791da177e4SLinus Torvalds  *			Programmer Harassment Alert
4801da177e4SLinus Torvalds  *
4811da177e4SLinus Torvalds  * When given a blocking lock request in a sync RPC call, the HPUX lockd
4821da177e4SLinus Torvalds  * will faithfully return LCK_BLOCKED but never cares to notify us when
4831da177e4SLinus Torvalds  * the lock could be granted. This way, our local process could hang
4841da177e4SLinus Torvalds  * around forever waiting for the callback.
4851da177e4SLinus Torvalds  *
4861da177e4SLinus Torvalds  *  Solution A:	Implement busy-waiting
4871da177e4SLinus Torvalds  *  Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
4881da177e4SLinus Torvalds  *
4891da177e4SLinus Torvalds  * For now I am implementing solution A, because I hate the idea of
4901da177e4SLinus Torvalds  * re-implementing lockd for a third time in two months. The async
4911da177e4SLinus Torvalds  * calls shouldn't be too hard to do, however.
4921da177e4SLinus Torvalds  *
4931da177e4SLinus Torvalds  * This is one of the lovely things about standards in the NFS area:
4941da177e4SLinus Torvalds  * they're so soft and squishy you can't really blame HP for doing this.
4951da177e4SLinus Torvalds  */
4961da177e4SLinus Torvalds static int
4971da177e4SLinus Torvalds nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	struct nlm_host	*host = req->a_host;
5001da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
5013a649b88STrond Myklebust 	struct nlm_wait *block = NULL;
5023a649b88STrond Myklebust 	int status = -ENOLCK;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (!host->h_monitored && nsm_monitor(host) < 0) {
5051da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: failed to monitor %s\n",
5061da177e4SLinus Torvalds 					host->h_name);
5071da177e4SLinus Torvalds 		goto out;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5103a649b88STrond Myklebust 	block = nlmclnt_prepare_block(host, fl);
511*28df955aSTrond Myklebust again:
512ecdbf769STrond Myklebust 	for(;;) {
513*28df955aSTrond Myklebust 		/* Reboot protection */
514*28df955aSTrond Myklebust 		fl->fl_u.nfs_fl.state = host->h_state;
515ecdbf769STrond Myklebust 		status = nlmclnt_call(req, NLMPROC_LOCK);
516ecdbf769STrond Myklebust 		if (status < 0)
517ecdbf769STrond Myklebust 			goto out_unblock;
5183a649b88STrond Myklebust 		if (!req->a_args.block)
519ecdbf769STrond Myklebust 			break;
520ecdbf769STrond Myklebust 		/* Did a reclaimer thread notify us of a server reboot? */
521ecdbf769STrond Myklebust 		if (resp->status ==  NLM_LCK_DENIED_GRACE_PERIOD)
522ecdbf769STrond Myklebust 			continue;
523ecdbf769STrond Myklebust 		if (resp->status != NLM_LCK_BLOCKED)
524ecdbf769STrond Myklebust 			break;
5253a649b88STrond Myklebust 		/* Wait on an NLM blocking lock */
5263a649b88STrond Myklebust 		status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
5273a649b88STrond Myklebust 		/* if we were interrupted. Send a CANCEL request to the server
528ecdbf769STrond Myklebust 		 * and exit
529ecdbf769STrond Myklebust 		 */
5303a649b88STrond Myklebust 		if (status < 0)
531ecdbf769STrond Myklebust 			goto out_unblock;
5323a649b88STrond Myklebust 		if (resp->status != NLM_LCK_BLOCKED)
5333a649b88STrond Myklebust 			break;
534ecdbf769STrond Myklebust 	}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (resp->status == NLM_LCK_GRANTED) {
537*28df955aSTrond Myklebust 		down_read(&host->h_rwsem);
538*28df955aSTrond Myklebust 		/* Check whether or not the server has rebooted */
539*28df955aSTrond Myklebust 		if (fl->fl_u.nfs_fl.state != host->h_state) {
540*28df955aSTrond Myklebust 			up_read(&host->h_rwsem);
541*28df955aSTrond Myklebust 			goto again;
542*28df955aSTrond Myklebust 		}
5431da177e4SLinus Torvalds 		fl->fl_flags |= FL_SLEEP;
5444c060b53STrond Myklebust 		/* Ensure the resulting lock will get added to granted list */
5451da177e4SLinus Torvalds 		do_vfs_lock(fl);
546*28df955aSTrond Myklebust 		up_read(&host->h_rwsem);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 	status = nlm_stat_to_errno(resp->status);
549ecdbf769STrond Myklebust out_unblock:
5503a649b88STrond Myklebust 	nlmclnt_finish_block(block);
551ecdbf769STrond Myklebust 	/* Cancel the blocked request if it is still pending */
552ecdbf769STrond Myklebust 	if (resp->status == NLM_LCK_BLOCKED)
55316fb2425STrond Myklebust 		nlmclnt_cancel(host, req->a_args.block, fl);
5541da177e4SLinus Torvalds out:
55592737230STrond Myklebust 	nlm_release_call(req);
5561da177e4SLinus Torvalds 	return status;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  * RECLAIM: Try to reclaim a lock
5611da177e4SLinus Torvalds  */
5621da177e4SLinus Torvalds int
5631da177e4SLinus Torvalds nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct nlm_rqst reqst, *req;
5661da177e4SLinus Torvalds 	int		status;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	req = &reqst;
5691da177e4SLinus Torvalds 	memset(req, 0, sizeof(*req));
5701da177e4SLinus Torvalds 	locks_init_lock(&req->a_args.lock.fl);
5711da177e4SLinus Torvalds 	locks_init_lock(&req->a_res.lock.fl);
5721da177e4SLinus Torvalds 	req->a_host  = host;
5731da177e4SLinus Torvalds 	req->a_flags = 0;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* Set up the argument struct */
5761da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
5771da177e4SLinus Torvalds 	req->a_args.reclaim = 1;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
5801da177e4SLinus Torvalds 	 && req->a_res.status == NLM_LCK_GRANTED)
5811da177e4SLinus Torvalds 		return 0;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
5841da177e4SLinus Torvalds 				"(errno %d, status %d)\n", fl->fl_pid,
5851da177e4SLinus Torvalds 				status, req->a_res.status);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/*
5881da177e4SLinus Torvalds 	 * FIXME: This is a serious failure. We can
5891da177e4SLinus Torvalds 	 *
5901da177e4SLinus Torvalds 	 *  a.	Ignore the problem
5911da177e4SLinus Torvalds 	 *  b.	Send the owning process some signal (Linux doesn't have
5921da177e4SLinus Torvalds 	 *	SIGLOST, though...)
5931da177e4SLinus Torvalds 	 *  c.	Retry the operation
5941da177e4SLinus Torvalds 	 *
5951da177e4SLinus Torvalds 	 * Until someone comes up with a simple implementation
5961da177e4SLinus Torvalds 	 * for b or c, I'll choose option a.
5971da177e4SLinus Torvalds 	 */
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	return -ENOLCK;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /*
6031da177e4SLinus Torvalds  * UNLOCK: remove an existing lock
6041da177e4SLinus Torvalds  */
6051da177e4SLinus Torvalds static int
6061da177e4SLinus Torvalds nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
6071da177e4SLinus Torvalds {
608*28df955aSTrond Myklebust 	struct nlm_host	*host = req->a_host;
6091da177e4SLinus Torvalds 	struct nlm_res	*resp = &req->a_res;
6101da177e4SLinus Torvalds 	int		status;
6111da177e4SLinus Torvalds 
61226bcbf96SChristoph Hellwig 	/*
61330f4e20aSTrond Myklebust 	 * Note: the server is supposed to either grant us the unlock
61430f4e20aSTrond Myklebust 	 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
61530f4e20aSTrond Myklebust 	 * case, we want to unlock.
61630f4e20aSTrond Myklebust 	 */
617*28df955aSTrond Myklebust 	down_read(&host->h_rwsem);
61830f4e20aSTrond Myklebust 	do_vfs_lock(fl);
619*28df955aSTrond Myklebust 	up_read(&host->h_rwsem);
62030f4e20aSTrond Myklebust 
62192737230STrond Myklebust 	if (req->a_flags & RPC_TASK_ASYNC)
62292737230STrond Myklebust 		return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	status = nlmclnt_call(req, NLMPROC_UNLOCK);
6251da177e4SLinus Torvalds 	if (status < 0)
62692737230STrond Myklebust 		goto out;
6271da177e4SLinus Torvalds 
62892737230STrond Myklebust 	status = 0;
6291da177e4SLinus Torvalds 	if (resp->status == NLM_LCK_GRANTED)
63092737230STrond Myklebust 		goto out;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (resp->status != NLM_LCK_DENIED_NOLOCKS)
6331da177e4SLinus Torvalds 		printk("lockd: unexpected unlock status: %d\n", resp->status);
6341da177e4SLinus Torvalds 	/* What to do now? I'm out of my depth... */
63592737230STrond Myklebust 	status = -ENOLCK;
63692737230STrond Myklebust out:
63792737230STrond Myklebust 	nlm_release_call(req);
63892737230STrond Myklebust 	return status;
6391da177e4SLinus Torvalds }
6401da177e4SLinus Torvalds 
641963d8fe5STrond Myklebust static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
6421da177e4SLinus Torvalds {
643963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
6441da177e4SLinus Torvalds 	int		status = req->a_res.status;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
6471da177e4SLinus Torvalds 		goto die;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	if (task->tk_status < 0) {
6501da177e4SLinus Torvalds 		dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
6511da177e4SLinus Torvalds 		goto retry_rebind;
6521da177e4SLinus Torvalds 	}
6531da177e4SLinus Torvalds 	if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
6541da177e4SLinus Torvalds 		rpc_delay(task, NLMCLNT_GRACE_WAIT);
6551da177e4SLinus Torvalds 		goto retry_unlock;
6561da177e4SLinus Torvalds 	}
6571da177e4SLinus Torvalds 	if (status != NLM_LCK_GRANTED)
6581da177e4SLinus Torvalds 		printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
6591da177e4SLinus Torvalds die:
6601da177e4SLinus Torvalds 	return;
6611da177e4SLinus Torvalds  retry_rebind:
6621da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
6631da177e4SLinus Torvalds  retry_unlock:
6641da177e4SLinus Torvalds 	rpc_restart_call(task);
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
667963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_unlock_ops = {
668963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_unlock_callback,
66992737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
670963d8fe5STrond Myklebust };
671963d8fe5STrond Myklebust 
6721da177e4SLinus Torvalds /*
6731da177e4SLinus Torvalds  * Cancel a blocked lock request.
6741da177e4SLinus Torvalds  * We always use an async RPC call for this in order not to hang a
6751da177e4SLinus Torvalds  * process that has been Ctrl-C'ed.
6761da177e4SLinus Torvalds  */
67716fb2425STrond Myklebust static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct nlm_rqst	*req;
6801da177e4SLinus Torvalds 	unsigned long	flags;
6811da177e4SLinus Torvalds 	sigset_t	oldset;
6821da177e4SLinus Torvalds 	int		status;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/* Block all signals while setting up call */
6851da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
6861da177e4SLinus Torvalds 	oldset = current->blocked;
6871da177e4SLinus Torvalds 	sigfillset(&current->blocked);
6881da177e4SLinus Torvalds 	recalc_sigpending();
6891da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
6901da177e4SLinus Torvalds 
69192737230STrond Myklebust 	req = nlm_alloc_call(nlm_get_host(host));
6921da177e4SLinus Torvalds 	if (!req)
6931da177e4SLinus Torvalds 		return -ENOMEM;
6941da177e4SLinus Torvalds 	req->a_flags = RPC_TASK_ASYNC;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	nlmclnt_setlockargs(req, fl);
69716fb2425STrond Myklebust 	req->a_args.block = block;
6981da177e4SLinus Torvalds 
69992737230STrond Myklebust 	status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	spin_lock_irqsave(&current->sighand->siglock, flags);
7021da177e4SLinus Torvalds 	current->blocked = oldset;
7031da177e4SLinus Torvalds 	recalc_sigpending();
7041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	return status;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
709963d8fe5STrond Myklebust static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
7101da177e4SLinus Torvalds {
711963d8fe5STrond Myklebust 	struct nlm_rqst	*req = data;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if (RPC_ASSASSINATED(task))
7141da177e4SLinus Torvalds 		goto die;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	if (task->tk_status < 0) {
7171da177e4SLinus Torvalds 		dprintk("lockd: CANCEL call error %d, retrying.\n",
7181da177e4SLinus Torvalds 					task->tk_status);
7191da177e4SLinus Torvalds 		goto retry_cancel;
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	dprintk("lockd: cancel status %d (task %d)\n",
7231da177e4SLinus Torvalds 			req->a_res.status, task->tk_pid);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	switch (req->a_res.status) {
7261da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
7271da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
72835576cbaSTrond Myklebust 	case NLM_LCK_DENIED:
7291da177e4SLinus Torvalds 		/* Everything's good */
7301da177e4SLinus Torvalds 		break;
7311da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
7321da177e4SLinus Torvalds 		dprintk("lockd: CANCEL failed (server has no locks)\n");
7331da177e4SLinus Torvalds 		goto retry_cancel;
7341da177e4SLinus Torvalds 	default:
7351da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
7361da177e4SLinus Torvalds 			req->a_res.status);
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds die:
7401da177e4SLinus Torvalds 	return;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds retry_cancel:
743aaaa9942STrond Myklebust 	/* Don't ever retry more than 3 times */
744aaaa9942STrond Myklebust 	if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
745aaaa9942STrond Myklebust 		goto die;
7461da177e4SLinus Torvalds 	nlm_rebind_host(req->a_host);
7471da177e4SLinus Torvalds 	rpc_restart_call(task);
7481da177e4SLinus Torvalds 	rpc_delay(task, 30 * HZ);
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
751963d8fe5STrond Myklebust static const struct rpc_call_ops nlmclnt_cancel_ops = {
752963d8fe5STrond Myklebust 	.rpc_call_done = nlmclnt_cancel_callback,
75392737230STrond Myklebust 	.rpc_release = nlmclnt_rpc_release,
754963d8fe5STrond Myklebust };
755963d8fe5STrond Myklebust 
7561da177e4SLinus Torvalds /*
7571da177e4SLinus Torvalds  * Convert an NLM status code to a generic kernel errno
7581da177e4SLinus Torvalds  */
7591da177e4SLinus Torvalds static int
7601da177e4SLinus Torvalds nlm_stat_to_errno(u32 status)
7611da177e4SLinus Torvalds {
7621da177e4SLinus Torvalds 	switch(status) {
7631da177e4SLinus Torvalds 	case NLM_LCK_GRANTED:
7641da177e4SLinus Torvalds 		return 0;
7651da177e4SLinus Torvalds 	case NLM_LCK_DENIED:
7661da177e4SLinus Torvalds 		return -EAGAIN;
7671da177e4SLinus Torvalds 	case NLM_LCK_DENIED_NOLOCKS:
7681da177e4SLinus Torvalds 	case NLM_LCK_DENIED_GRACE_PERIOD:
7691da177e4SLinus Torvalds 		return -ENOLCK;
7701da177e4SLinus Torvalds 	case NLM_LCK_BLOCKED:
7711da177e4SLinus Torvalds 		printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
7721da177e4SLinus Torvalds 		return -ENOLCK;
7731da177e4SLinus Torvalds #ifdef CONFIG_LOCKD_V4
7741da177e4SLinus Torvalds 	case NLM_DEADLCK:
7751da177e4SLinus Torvalds 		return -EDEADLK;
7761da177e4SLinus Torvalds 	case NLM_ROFS:
7771da177e4SLinus Torvalds 		return -EROFS;
7781da177e4SLinus Torvalds 	case NLM_STALE_FH:
7791da177e4SLinus Torvalds 		return -ESTALE;
7801da177e4SLinus Torvalds 	case NLM_FBIG:
7811da177e4SLinus Torvalds 		return -EOVERFLOW;
7821da177e4SLinus Torvalds 	case NLM_FAILED:
7831da177e4SLinus Torvalds 		return -ENOLCK;
7841da177e4SLinus Torvalds #endif
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
7871da177e4SLinus Torvalds 	return -ENOLCK;
7881da177e4SLinus Torvalds }
789