xref: /openbmc/linux/fs/lockd/clntlock.c (revision 2005f5b9)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/lockd/clntlock.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Lock handling for the client side NLM implementation
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/types.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/time.h>
141da177e4SLinus Torvalds #include <linux/nfs_fs.h>
155976687aSJeff Layton #include <linux/sunrpc/addr.h>
161da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
171da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
18df94f000SJeff Layton #include <linux/kthread.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * Local function prototypes
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds static int			reclaimer(void *ptr);
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /*
281da177e4SLinus Torvalds  * The following functions handle blocking and granting from the
291da177e4SLinus Torvalds  * client perspective.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
324f15e2b1STrond Myklebust static LIST_HEAD(nlm_blocked);
3363185942SBryan Schumaker static DEFINE_SPINLOCK(nlm_blocked_lock);
341da177e4SLinus Torvalds 
3552c4044dSChuck Lever /**
3652c4044dSChuck Lever  * nlmclnt_init - Set up per-NFS mount point lockd data structures
37883bb163SChuck Lever  * @nlm_init: pointer to arguments structure
3852c4044dSChuck Lever  *
3952c4044dSChuck Lever  * Returns pointer to an appropriate nlm_host struct,
4052c4044dSChuck Lever  * or an ERR_PTR value.
4152c4044dSChuck Lever  */
42883bb163SChuck Lever struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
4352c4044dSChuck Lever {
4452c4044dSChuck Lever 	struct nlm_host *host;
45883bb163SChuck Lever 	u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
4652c4044dSChuck Lever 	int status;
4752c4044dSChuck Lever 
4840373b12STrond Myklebust 	status = lockd_up(nlm_init->net, nlm_init->cred);
4952c4044dSChuck Lever 	if (status < 0)
5052c4044dSChuck Lever 		return ERR_PTR(status);
5152c4044dSChuck Lever 
52d7d20440SChuck Lever 	host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
53883bb163SChuck Lever 				   nlm_init->protocol, nlm_version,
5466697bfdSStanislav Kinsbursky 				   nlm_init->hostname, nlm_init->noresvport,
55b422df91STrond Myklebust 				   nlm_init->net, nlm_init->cred);
569a1b6bf8STrond Myklebust 	if (host == NULL)
579a1b6bf8STrond Myklebust 		goto out_nohost;
589a1b6bf8STrond Myklebust 	if (host->h_rpcclnt == NULL && nlm_bind_host(host) == NULL)
599a1b6bf8STrond Myklebust 		goto out_nobind;
6052c4044dSChuck Lever 
61b1ece737SBenjamin Coddington 	host->h_nlmclnt_ops = nlm_init->nlmclnt_ops;
6252c4044dSChuck Lever 	return host;
639a1b6bf8STrond Myklebust out_nobind:
649a1b6bf8STrond Myklebust 	nlmclnt_release_host(host);
659a1b6bf8STrond Myklebust out_nohost:
669a1b6bf8STrond Myklebust 	lockd_down(nlm_init->net);
679a1b6bf8STrond Myklebust 	return ERR_PTR(-ENOLCK);
6852c4044dSChuck Lever }
6952c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_init);
7052c4044dSChuck Lever 
7152c4044dSChuck Lever /**
7252c4044dSChuck Lever  * nlmclnt_done - Release resources allocated by nlmclnt_init()
7352c4044dSChuck Lever  * @host: nlm_host structure reserved by nlmclnt_init()
7452c4044dSChuck Lever  *
7552c4044dSChuck Lever  */
7652c4044dSChuck Lever void nlmclnt_done(struct nlm_host *host)
7752c4044dSChuck Lever {
78e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
79e3f70eadSStanislav Kinsbursky 
808ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
81e3f70eadSStanislav Kinsbursky 	lockd_down(net);
8252c4044dSChuck Lever }
8352c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_done);
8452c4044dSChuck Lever 
85*2005f5b9SJeff Layton void nlmclnt_prepare_block(struct nlm_wait *block, struct nlm_host *host, struct file_lock *fl)
86ecdbf769STrond Myklebust {
87ecdbf769STrond Myklebust 	block->b_host = host;
88ecdbf769STrond Myklebust 	block->b_lock = fl;
89ecdbf769STrond Myklebust 	init_waitqueue_head(&block->b_wait);
90e8c5c045SAl Viro 	block->b_status = nlm_lck_blocked;
91*2005f5b9SJeff Layton }
9263185942SBryan Schumaker 
93*2005f5b9SJeff Layton /*
94*2005f5b9SJeff Layton  * Queue up a lock for blocking so that the GRANTED request can see it
95*2005f5b9SJeff Layton  */
96*2005f5b9SJeff Layton void nlmclnt_queue_block(struct nlm_wait *block)
97*2005f5b9SJeff Layton {
9863185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
99ecdbf769STrond Myklebust 	list_add(&block->b_list, &nlm_blocked);
10063185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
1013a649b88STrond Myklebust }
102ecdbf769STrond Myklebust 
103*2005f5b9SJeff Layton /*
104*2005f5b9SJeff Layton  * Dequeue the block and return its final status
105*2005f5b9SJeff Layton  */
106*2005f5b9SJeff Layton __be32 nlmclnt_dequeue_block(struct nlm_wait *block)
107ecdbf769STrond Myklebust {
108*2005f5b9SJeff Layton 	__be32 status;
109*2005f5b9SJeff Layton 
11063185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
111ecdbf769STrond Myklebust 	list_del(&block->b_list);
112*2005f5b9SJeff Layton 	status = block->b_status;
11363185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
114*2005f5b9SJeff Layton 	return status;
115ecdbf769STrond Myklebust }
116ecdbf769STrond Myklebust 
117ecdbf769STrond Myklebust /*
1181da177e4SLinus Torvalds  * Block on a lock
1191da177e4SLinus Torvalds  */
120*2005f5b9SJeff Layton int nlmclnt_wait(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1211da177e4SLinus Torvalds {
122ecdbf769STrond Myklebust 	long ret;
1231da177e4SLinus Torvalds 
124ecdbf769STrond Myklebust 	/* A borken server might ask us to block even if we didn't
125ecdbf769STrond Myklebust 	 * request it. Just say no!
126ecdbf769STrond Myklebust 	 */
1273a649b88STrond Myklebust 	if (block == NULL)
128ecdbf769STrond Myklebust 		return -EAGAIN;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/* Go to sleep waiting for GRANT callback. Some servers seem
1311da177e4SLinus Torvalds 	 * to lose callbacks, however, so we're going to poll from
1321da177e4SLinus Torvalds 	 * time to time just to make sure.
1331da177e4SLinus Torvalds 	 *
1341da177e4SLinus Torvalds 	 * For now, the retry frequency is pretty high; normally
1351da177e4SLinus Torvalds 	 * a 1 minute timeout would do. See the comment before
1361da177e4SLinus Torvalds 	 * nlmclnt_lock for an explanation.
1371da177e4SLinus Torvalds 	 */
138ecdbf769STrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
139e8c5c045SAl Viro 			block->b_status != nlm_lck_blocked,
140ecdbf769STrond Myklebust 			timeout);
1413a649b88STrond Myklebust 	if (ret < 0)
1423a649b88STrond Myklebust 		return -ERESTARTSYS;
1431dfd89afSTrond Myklebust 	/* Reset the lock status after a server reboot so we resend */
1441dfd89afSTrond Myklebust 	if (block->b_status == nlm_lck_denied_grace_period)
1451dfd89afSTrond Myklebust 		block->b_status = nlm_lck_blocked;
1463a649b88STrond Myklebust 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
1501da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
1511da177e4SLinus Torvalds  */
152dcff09f1SChuck Lever __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1531da177e4SLinus Torvalds {
1545ac5f9d1STrond Myklebust 	const struct file_lock *fl = &lock->fl;
1555ac5f9d1STrond Myklebust 	const struct nfs_fh *fh = &lock->fh;
1561da177e4SLinus Torvalds 	struct nlm_wait	*block;
15752921e02SAl Viro 	__be32 res = nlm_lck_denied;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/*
1601da177e4SLinus Torvalds 	 * Look up blocked request based on arguments.
1611da177e4SLinus Torvalds 	 * Warning: must not use cookie to match it!
1621da177e4SLinus Torvalds 	 */
16363185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
1644f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1655ac5f9d1STrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1665ac5f9d1STrond Myklebust 
1677bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1687bab377fSTrond Myklebust 			continue;
1697bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1707bab377fSTrond Myklebust 			continue;
1717bab377fSTrond Myklebust 		/*
1727bab377fSTrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
1737bab377fSTrond Myklebust 		 * we put on the wire: in this case the lockowner "pid".
1747bab377fSTrond Myklebust 		 */
1757bab377fSTrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
1765ac5f9d1STrond Myklebust 			continue;
1774516fc04SJeff Layton 		if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
1785ac5f9d1STrond Myklebust 			continue;
179c65454a9SJeff Layton 		if (nfs_compare_fh(NFS_FH(file_inode(fl_blocked->fl_file)), fh) != 0)
1805ac5f9d1STrond Myklebust 			continue;
181ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
182ecdbf769STrond Myklebust 		 * and wake up the caller
1831da177e4SLinus Torvalds 		 */
184e8c5c045SAl Viro 		block->b_status = nlm_granted;
1851da177e4SLinus Torvalds 		wake_up(&block->b_wait);
186ecdbf769STrond Myklebust 		res = nlm_granted;
187ecdbf769STrond Myklebust 	}
18863185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
189ecdbf769STrond Myklebust 	return res;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * The following procedures deal with the recovery of locks after a
1941da177e4SLinus Torvalds  * server crash.
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /*
1981da177e4SLinus Torvalds  * Reclaim all locks on server host. We do this by spawning a separate
1991da177e4SLinus Torvalds  * reclaimer thread.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds void
2025c8dd29cSOlaf Kirch nlmclnt_recovery(struct nlm_host *host)
2031da177e4SLinus Torvalds {
204df94f000SJeff Layton 	struct task_struct *task;
205df94f000SJeff Layton 
20628df955aSTrond Myklebust 	if (!host->h_reclaiming++) {
2071da177e4SLinus Torvalds 		nlm_get_host(host);
208df94f000SJeff Layton 		task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
209df94f000SJeff Layton 		if (IS_ERR(task))
210df94f000SJeff Layton 			printk(KERN_ERR "lockd: unable to spawn reclaimer "
211df94f000SJeff Layton 				"thread. Locks for %s won't be reclaimed! "
212df94f000SJeff Layton 				"(%ld)\n", host->h_name, PTR_ERR(task));
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds static int
2171da177e4SLinus Torvalds reclaimer(void *ptr)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
2201da177e4SLinus Torvalds 	struct nlm_wait	  *block;
221f25cc71eSTim Gardner 	struct nlm_rqst   *req;
22226bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
22328df955aSTrond Myklebust 	u32 nsmstate;
224e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
2251da177e4SLinus Torvalds 
226f25cc71eSTim Gardner 	req = kmalloc(sizeof(*req), GFP_KERNEL);
22758a69893SMarkus Elfring 	if (!req)
228f25cc71eSTim Gardner 		return 0;
229f25cc71eSTim Gardner 
2301da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2311da177e4SLinus Torvalds 
2325c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
23340373b12STrond Myklebust 	lockd_up(net, NULL);	/* note: this cannot fail as lockd is already running */
2341da177e4SLinus Torvalds 
235d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2365c8dd29cSOlaf Kirch 
2371da177e4SLinus Torvalds restart:
23828df955aSTrond Myklebust 	nsmstate = host->h_nsmstate;
2395c8dd29cSOlaf Kirch 
2405c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2415c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
2425c8dd29cSOlaf Kirch 	 */
2430ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2445c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2455c8dd29cSOlaf Kirch 
2465c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2475c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
24826bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2494c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2501da177e4SLinus Torvalds 
251df94f000SJeff Layton 		/*
252df94f000SJeff Layton 		 * sending this thread a SIGKILL will result in any unreclaimed
253df94f000SJeff Layton 		 * locks being removed from the h_granted list. This means that
254df94f000SJeff Layton 		 * the kernel will not attempt to reclaim them again if a new
255df94f000SJeff Layton 		 * reclaimer thread is spawned for this host.
256df94f000SJeff Layton 		 */
2571da177e4SLinus Torvalds 		if (signalled())
2584c060b53STrond Myklebust 			continue;
259f25cc71eSTim Gardner 		if (nlmclnt_reclaim(host, fl, req) != 0)
26028df955aSTrond Myklebust 			continue;
2614c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
26228df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
26328df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2641da177e4SLinus Torvalds 			goto restart;
2651da177e4SLinus Torvalds 		}
26628df955aSTrond Myklebust 	}
2675c8dd29cSOlaf Kirch 
2685c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2695c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
270d019bcf0SAdrian Bunk 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
27363185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
2744f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2751da177e4SLinus Torvalds 		if (block->b_host == host) {
276e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2771da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 	}
28063185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Release host handle after use */
2838ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
284e3f70eadSStanislav Kinsbursky 	lockd_down(net);
285f25cc71eSTim Gardner 	kfree(req);
286df94f000SJeff Layton 	return 0;
2871da177e4SLinus Torvalds }
288