xref: /openbmc/linux/fs/lockd/clntlock.c (revision 496ad9aa)
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>
141da177e4SLinus Torvalds #include <linux/sunrpc/clnt.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;
147ecdbf769STrond Myklebust 	req->a_res.status = block->b_status;
1483a649b88STrond Myklebust 	return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
1531da177e4SLinus Torvalds  */
154dcff09f1SChuck Lever __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1551da177e4SLinus Torvalds {
1565ac5f9d1STrond Myklebust 	const struct file_lock *fl = &lock->fl;
1575ac5f9d1STrond Myklebust 	const struct nfs_fh *fh = &lock->fh;
1581da177e4SLinus Torvalds 	struct nlm_wait	*block;
15952921e02SAl Viro 	__be32 res = nlm_lck_denied;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/*
1621da177e4SLinus Torvalds 	 * Look up blocked request based on arguments.
1631da177e4SLinus Torvalds 	 * Warning: must not use cookie to match it!
1641da177e4SLinus Torvalds 	 */
16563185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
1664f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1675ac5f9d1STrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1685ac5f9d1STrond Myklebust 
1697bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1707bab377fSTrond Myklebust 			continue;
1717bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1727bab377fSTrond Myklebust 			continue;
1737bab377fSTrond Myklebust 		/*
1747bab377fSTrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
1757bab377fSTrond Myklebust 		 * we put on the wire: in this case the lockowner "pid".
1767bab377fSTrond Myklebust 		 */
1777bab377fSTrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
1785ac5f9d1STrond Myklebust 			continue;
1794516fc04SJeff Layton 		if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
1805ac5f9d1STrond Myklebust 			continue;
181496ad9aaSAl Viro 		if (nfs_compare_fh(NFS_FH(file_inode(fl_blocked->fl_file)) ,fh) != 0)
1825ac5f9d1STrond Myklebust 			continue;
183ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
184ecdbf769STrond Myklebust 		 * and wake up the caller
1851da177e4SLinus Torvalds 		 */
186e8c5c045SAl Viro 		block->b_status = nlm_granted;
1871da177e4SLinus Torvalds 		wake_up(&block->b_wait);
188ecdbf769STrond Myklebust 		res = nlm_granted;
189ecdbf769STrond Myklebust 	}
19063185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
191ecdbf769STrond Myklebust 	return res;
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /*
1951da177e4SLinus Torvalds  * The following procedures deal with the recovery of locks after a
1961da177e4SLinus Torvalds  * server crash.
1971da177e4SLinus Torvalds  */
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * Reclaim all locks on server host. We do this by spawning a separate
2011da177e4SLinus Torvalds  * reclaimer thread.
2021da177e4SLinus Torvalds  */
2031da177e4SLinus Torvalds void
2045c8dd29cSOlaf Kirch nlmclnt_recovery(struct nlm_host *host)
2051da177e4SLinus Torvalds {
206df94f000SJeff Layton 	struct task_struct *task;
207df94f000SJeff Layton 
20828df955aSTrond Myklebust 	if (!host->h_reclaiming++) {
2091da177e4SLinus Torvalds 		nlm_get_host(host);
210df94f000SJeff Layton 		task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
211df94f000SJeff Layton 		if (IS_ERR(task))
212df94f000SJeff Layton 			printk(KERN_ERR "lockd: unable to spawn reclaimer "
213df94f000SJeff Layton 				"thread. Locks for %s won't be reclaimed! "
214df94f000SJeff Layton 				"(%ld)\n", host->h_name, PTR_ERR(task));
2151da177e4SLinus Torvalds 	}
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static int
2191da177e4SLinus Torvalds reclaimer(void *ptr)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
2221da177e4SLinus Torvalds 	struct nlm_wait	  *block;
22326bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
22428df955aSTrond Myklebust 	u32 nsmstate;
225e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2281da177e4SLinus Torvalds 
2295c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
230e3f70eadSStanislav Kinsbursky 	lockd_up(net);	/* note: this cannot fail as lockd is already running */
2311da177e4SLinus Torvalds 
232d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2335c8dd29cSOlaf Kirch 
2341da177e4SLinus Torvalds restart:
23528df955aSTrond Myklebust 	nsmstate = host->h_nsmstate;
2365c8dd29cSOlaf Kirch 
2375c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2385c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
2395c8dd29cSOlaf Kirch 	 */
2400ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2415c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2425c8dd29cSOlaf Kirch 
2435c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2445c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
24526bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2464c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2471da177e4SLinus Torvalds 
248df94f000SJeff Layton 		/*
249df94f000SJeff Layton 		 * sending this thread a SIGKILL will result in any unreclaimed
250df94f000SJeff Layton 		 * locks being removed from the h_granted list. This means that
251df94f000SJeff Layton 		 * the kernel will not attempt to reclaim them again if a new
252df94f000SJeff Layton 		 * reclaimer thread is spawned for this host.
253df94f000SJeff Layton 		 */
2541da177e4SLinus Torvalds 		if (signalled())
2554c060b53STrond Myklebust 			continue;
25628df955aSTrond Myklebust 		if (nlmclnt_reclaim(host, fl) != 0)
25728df955aSTrond Myklebust 			continue;
2584c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
25928df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
26028df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2611da177e4SLinus Torvalds 			goto restart;
2621da177e4SLinus Torvalds 		}
26328df955aSTrond Myklebust 	}
2645c8dd29cSOlaf Kirch 
2655c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2665c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
267d019bcf0SAdrian Bunk 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
27063185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
2714f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2721da177e4SLinus Torvalds 		if (block->b_host == host) {
273e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2741da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2751da177e4SLinus Torvalds 		}
2761da177e4SLinus Torvalds 	}
27763185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	/* Release host handle after use */
2808ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
281e3f70eadSStanislav Kinsbursky 	lockd_down(net);
282df94f000SJeff Layton 	return 0;
2831da177e4SLinus Torvalds }
284