xref: /openbmc/linux/fs/lockd/clntlock.c (revision 1dfd89af)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/fs/lockd/clntlock.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Lock handling 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>
115a0e3ad6STejun Heo #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/time.h>
131da177e4SLinus Torvalds #include <linux/nfs_fs.h>
145976687aSJeff Layton #include <linux/sunrpc/addr.h>
151da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
161da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
17df94f000SJeff Layton #include <linux/kthread.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds /*
221da177e4SLinus Torvalds  * Local function prototypes
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds static int			reclaimer(void *ptr);
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * The following functions handle blocking and granting from the
281da177e4SLinus Torvalds  * client perspective.
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds /*
321da177e4SLinus Torvalds  * This is the representation of a blocked client lock.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds struct nlm_wait {
354f15e2b1STrond Myklebust 	struct list_head	b_list;		/* linked list */
361da177e4SLinus Torvalds 	wait_queue_head_t	b_wait;		/* where to wait on */
371da177e4SLinus Torvalds 	struct nlm_host *	b_host;
381da177e4SLinus Torvalds 	struct file_lock *	b_lock;		/* local file lock */
391da177e4SLinus Torvalds 	unsigned short		b_reclaim;	/* got to reclaim lock */
40e8c5c045SAl Viro 	__be32			b_status;	/* grant callback status */
411da177e4SLinus Torvalds };
421da177e4SLinus Torvalds 
434f15e2b1STrond Myklebust static LIST_HEAD(nlm_blocked);
4463185942SBryan Schumaker static DEFINE_SPINLOCK(nlm_blocked_lock);
451da177e4SLinus Torvalds 
4652c4044dSChuck Lever /**
4752c4044dSChuck Lever  * nlmclnt_init - Set up per-NFS mount point lockd data structures
48883bb163SChuck Lever  * @nlm_init: pointer to arguments structure
4952c4044dSChuck Lever  *
5052c4044dSChuck Lever  * Returns pointer to an appropriate nlm_host struct,
5152c4044dSChuck Lever  * or an ERR_PTR value.
5252c4044dSChuck Lever  */
53883bb163SChuck Lever struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
5452c4044dSChuck Lever {
5552c4044dSChuck Lever 	struct nlm_host *host;
56883bb163SChuck Lever 	u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
5752c4044dSChuck Lever 	int status;
5852c4044dSChuck Lever 
59e3f70eadSStanislav Kinsbursky 	status = lockd_up(nlm_init->net);
6052c4044dSChuck Lever 	if (status < 0)
6152c4044dSChuck Lever 		return ERR_PTR(status);
6252c4044dSChuck Lever 
63d7d20440SChuck Lever 	host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
64883bb163SChuck Lever 				   nlm_init->protocol, nlm_version,
6566697bfdSStanislav Kinsbursky 				   nlm_init->hostname, nlm_init->noresvport,
6666697bfdSStanislav Kinsbursky 				   nlm_init->net);
6752c4044dSChuck Lever 	if (host == NULL) {
68e3f70eadSStanislav Kinsbursky 		lockd_down(nlm_init->net);
6952c4044dSChuck Lever 		return ERR_PTR(-ENOLCK);
7052c4044dSChuck Lever 	}
7152c4044dSChuck Lever 
7252c4044dSChuck Lever 	return host;
7352c4044dSChuck Lever }
7452c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_init);
7552c4044dSChuck Lever 
7652c4044dSChuck Lever /**
7752c4044dSChuck Lever  * nlmclnt_done - Release resources allocated by nlmclnt_init()
7852c4044dSChuck Lever  * @host: nlm_host structure reserved by nlmclnt_init()
7952c4044dSChuck Lever  *
8052c4044dSChuck Lever  */
8152c4044dSChuck Lever void nlmclnt_done(struct nlm_host *host)
8252c4044dSChuck Lever {
83e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
84e3f70eadSStanislav Kinsbursky 
858ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
86e3f70eadSStanislav Kinsbursky 	lockd_down(net);
8752c4044dSChuck Lever }
8852c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_done);
8952c4044dSChuck Lever 
901da177e4SLinus Torvalds /*
91ecdbf769STrond Myklebust  * Queue up a lock for blocking so that the GRANTED request can see it
92ecdbf769STrond Myklebust  */
933a649b88STrond Myklebust struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
94ecdbf769STrond Myklebust {
95ecdbf769STrond Myklebust 	struct nlm_wait *block;
96ecdbf769STrond Myklebust 
97ecdbf769STrond Myklebust 	block = kmalloc(sizeof(*block), GFP_KERNEL);
983a649b88STrond Myklebust 	if (block != NULL) {
99ecdbf769STrond Myklebust 		block->b_host = host;
100ecdbf769STrond Myklebust 		block->b_lock = fl;
101ecdbf769STrond Myklebust 		init_waitqueue_head(&block->b_wait);
102e8c5c045SAl Viro 		block->b_status = nlm_lck_blocked;
10363185942SBryan Schumaker 
10463185942SBryan Schumaker 		spin_lock(&nlm_blocked_lock);
105ecdbf769STrond Myklebust 		list_add(&block->b_list, &nlm_blocked);
10663185942SBryan Schumaker 		spin_unlock(&nlm_blocked_lock);
1073a649b88STrond Myklebust 	}
1083a649b88STrond Myklebust 	return block;
109ecdbf769STrond Myklebust }
110ecdbf769STrond Myklebust 
1113a649b88STrond Myklebust void nlmclnt_finish_block(struct nlm_wait *block)
112ecdbf769STrond Myklebust {
113ecdbf769STrond Myklebust 	if (block == NULL)
114ecdbf769STrond Myklebust 		return;
11563185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
116ecdbf769STrond Myklebust 	list_del(&block->b_list);
11763185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
118ecdbf769STrond Myklebust 	kfree(block);
119ecdbf769STrond Myklebust }
120ecdbf769STrond Myklebust 
121ecdbf769STrond Myklebust /*
1221da177e4SLinus Torvalds  * Block on a lock
1231da177e4SLinus Torvalds  */
1243a649b88STrond Myklebust int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1251da177e4SLinus Torvalds {
126ecdbf769STrond Myklebust 	long ret;
1271da177e4SLinus Torvalds 
128ecdbf769STrond Myklebust 	/* A borken server might ask us to block even if we didn't
129ecdbf769STrond Myklebust 	 * request it. Just say no!
130ecdbf769STrond Myklebust 	 */
1313a649b88STrond Myklebust 	if (block == NULL)
132ecdbf769STrond Myklebust 		return -EAGAIN;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	/* Go to sleep waiting for GRANT callback. Some servers seem
1351da177e4SLinus Torvalds 	 * to lose callbacks, however, so we're going to poll from
1361da177e4SLinus Torvalds 	 * time to time just to make sure.
1371da177e4SLinus Torvalds 	 *
1381da177e4SLinus Torvalds 	 * For now, the retry frequency is pretty high; normally
1391da177e4SLinus Torvalds 	 * a 1 minute timeout would do. See the comment before
1401da177e4SLinus Torvalds 	 * nlmclnt_lock for an explanation.
1411da177e4SLinus Torvalds 	 */
142ecdbf769STrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
143e8c5c045SAl Viro 			block->b_status != nlm_lck_blocked,
144ecdbf769STrond Myklebust 			timeout);
1453a649b88STrond Myklebust 	if (ret < 0)
1463a649b88STrond Myklebust 		return -ERESTARTSYS;
1471dfd89afSTrond Myklebust 	/* Reset the lock status after a server reboot so we resend */
1481dfd89afSTrond Myklebust 	if (block->b_status == nlm_lck_denied_grace_period)
1491dfd89afSTrond Myklebust 		block->b_status = nlm_lck_blocked;
150ecdbf769STrond Myklebust 	req->a_res.status = block->b_status;
1513a649b88STrond Myklebust 	return 0;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
1561da177e4SLinus Torvalds  */
157dcff09f1SChuck Lever __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1581da177e4SLinus Torvalds {
1595ac5f9d1STrond Myklebust 	const struct file_lock *fl = &lock->fl;
1605ac5f9d1STrond Myklebust 	const struct nfs_fh *fh = &lock->fh;
1611da177e4SLinus Torvalds 	struct nlm_wait	*block;
16252921e02SAl Viro 	__be32 res = nlm_lck_denied;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/*
1651da177e4SLinus Torvalds 	 * Look up blocked request based on arguments.
1661da177e4SLinus Torvalds 	 * Warning: must not use cookie to match it!
1671da177e4SLinus Torvalds 	 */
16863185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
1694f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1705ac5f9d1STrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1715ac5f9d1STrond Myklebust 
1727bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1737bab377fSTrond Myklebust 			continue;
1747bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1757bab377fSTrond Myklebust 			continue;
1767bab377fSTrond Myklebust 		/*
1777bab377fSTrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
1787bab377fSTrond Myklebust 		 * we put on the wire: in this case the lockowner "pid".
1797bab377fSTrond Myklebust 		 */
1807bab377fSTrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
1815ac5f9d1STrond Myklebust 			continue;
1824516fc04SJeff Layton 		if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
1835ac5f9d1STrond Myklebust 			continue;
184496ad9aaSAl Viro 		if (nfs_compare_fh(NFS_FH(file_inode(fl_blocked->fl_file)) ,fh) != 0)
1855ac5f9d1STrond Myklebust 			continue;
186ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
187ecdbf769STrond Myklebust 		 * and wake up the caller
1881da177e4SLinus Torvalds 		 */
189e8c5c045SAl Viro 		block->b_status = nlm_granted;
1901da177e4SLinus Torvalds 		wake_up(&block->b_wait);
191ecdbf769STrond Myklebust 		res = nlm_granted;
192ecdbf769STrond Myklebust 	}
19363185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
194ecdbf769STrond Myklebust 	return res;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /*
1981da177e4SLinus Torvalds  * The following procedures deal with the recovery of locks after a
1991da177e4SLinus Torvalds  * server crash.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds  * Reclaim all locks on server host. We do this by spawning a separate
2041da177e4SLinus Torvalds  * reclaimer thread.
2051da177e4SLinus Torvalds  */
2061da177e4SLinus Torvalds void
2075c8dd29cSOlaf Kirch nlmclnt_recovery(struct nlm_host *host)
2081da177e4SLinus Torvalds {
209df94f000SJeff Layton 	struct task_struct *task;
210df94f000SJeff Layton 
21128df955aSTrond Myklebust 	if (!host->h_reclaiming++) {
2121da177e4SLinus Torvalds 		nlm_get_host(host);
213df94f000SJeff Layton 		task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
214df94f000SJeff Layton 		if (IS_ERR(task))
215df94f000SJeff Layton 			printk(KERN_ERR "lockd: unable to spawn reclaimer "
216df94f000SJeff Layton 				"thread. Locks for %s won't be reclaimed! "
217df94f000SJeff Layton 				"(%ld)\n", host->h_name, PTR_ERR(task));
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds static int
2221da177e4SLinus Torvalds reclaimer(void *ptr)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
2251da177e4SLinus Torvalds 	struct nlm_wait	  *block;
226f25cc71eSTim Gardner 	struct nlm_rqst   *req;
22726bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
22828df955aSTrond Myklebust 	u32 nsmstate;
229e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
2301da177e4SLinus Torvalds 
231f25cc71eSTim Gardner 	req = kmalloc(sizeof(*req), GFP_KERNEL);
232f25cc71eSTim Gardner 	if (!req) {
233f25cc71eSTim Gardner 		printk(KERN_ERR "lockd: reclaimer unable to alloc memory."
234f25cc71eSTim Gardner 				" Locks for %s won't be reclaimed!\n",
235f25cc71eSTim Gardner 				host->h_name);
236f25cc71eSTim Gardner 		return 0;
237f25cc71eSTim Gardner 	}
238f25cc71eSTim Gardner 
2391da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2401da177e4SLinus Torvalds 
2415c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
242e3f70eadSStanislav Kinsbursky 	lockd_up(net);	/* note: this cannot fail as lockd is already running */
2431da177e4SLinus Torvalds 
244d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2455c8dd29cSOlaf Kirch 
2461da177e4SLinus Torvalds restart:
24728df955aSTrond Myklebust 	nsmstate = host->h_nsmstate;
2485c8dd29cSOlaf Kirch 
2495c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2505c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
2515c8dd29cSOlaf Kirch 	 */
2520ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2535c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2545c8dd29cSOlaf Kirch 
2555c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2565c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
25726bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2584c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2591da177e4SLinus Torvalds 
260df94f000SJeff Layton 		/*
261df94f000SJeff Layton 		 * sending this thread a SIGKILL will result in any unreclaimed
262df94f000SJeff Layton 		 * locks being removed from the h_granted list. This means that
263df94f000SJeff Layton 		 * the kernel will not attempt to reclaim them again if a new
264df94f000SJeff Layton 		 * reclaimer thread is spawned for this host.
265df94f000SJeff Layton 		 */
2661da177e4SLinus Torvalds 		if (signalled())
2674c060b53STrond Myklebust 			continue;
268f25cc71eSTim Gardner 		if (nlmclnt_reclaim(host, fl, req) != 0)
26928df955aSTrond Myklebust 			continue;
2704c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
27128df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
27228df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2731da177e4SLinus Torvalds 			goto restart;
2741da177e4SLinus Torvalds 		}
27528df955aSTrond Myklebust 	}
2765c8dd29cSOlaf Kirch 
2775c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2785c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
279d019bcf0SAdrian Bunk 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
28263185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
2834f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2841da177e4SLinus Torvalds 		if (block->b_host == host) {
285e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2861da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 	}
28963185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* Release host handle after use */
2928ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
293e3f70eadSStanislav Kinsbursky 	lockd_down(net);
294f25cc71eSTim Gardner 	kfree(req);
295df94f000SJeff Layton 	return 0;
2961da177e4SLinus Torvalds }
297