xref: /openbmc/linux/fs/lockd/clntlock.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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>
17*2f90e18fSJeff Layton #include <linux/sunrpc/svc_xprt.h>
181da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
19df94f000SJeff Layton #include <linux/kthread.h>
201da177e4SLinus Torvalds 
21*2f90e18fSJeff Layton #include "trace.h"
22*2f90e18fSJeff Layton 
231da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * Local function prototypes
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds static int			reclaimer(void *ptr);
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * The following functions handle blocking and granting from the
321da177e4SLinus Torvalds  * client perspective.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
354f15e2b1STrond Myklebust static LIST_HEAD(nlm_blocked);
3663185942SBryan Schumaker static DEFINE_SPINLOCK(nlm_blocked_lock);
371da177e4SLinus Torvalds 
3852c4044dSChuck Lever /**
3952c4044dSChuck Lever  * nlmclnt_init - Set up per-NFS mount point lockd data structures
40883bb163SChuck Lever  * @nlm_init: pointer to arguments structure
4152c4044dSChuck Lever  *
4252c4044dSChuck Lever  * Returns pointer to an appropriate nlm_host struct,
4352c4044dSChuck Lever  * or an ERR_PTR value.
4452c4044dSChuck Lever  */
nlmclnt_init(const struct nlmclnt_initdata * nlm_init)45883bb163SChuck Lever struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
4652c4044dSChuck Lever {
4752c4044dSChuck Lever 	struct nlm_host *host;
48883bb163SChuck Lever 	u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
4952c4044dSChuck Lever 	int status;
5052c4044dSChuck Lever 
5140373b12STrond Myklebust 	status = lockd_up(nlm_init->net, nlm_init->cred);
5252c4044dSChuck Lever 	if (status < 0)
5352c4044dSChuck Lever 		return ERR_PTR(status);
5452c4044dSChuck Lever 
55d7d20440SChuck Lever 	host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
56883bb163SChuck Lever 				   nlm_init->protocol, nlm_version,
5766697bfdSStanislav Kinsbursky 				   nlm_init->hostname, nlm_init->noresvport,
58b422df91STrond Myklebust 				   nlm_init->net, nlm_init->cred);
599a1b6bf8STrond Myklebust 	if (host == NULL)
609a1b6bf8STrond Myklebust 		goto out_nohost;
619a1b6bf8STrond Myklebust 	if (host->h_rpcclnt == NULL && nlm_bind_host(host) == NULL)
629a1b6bf8STrond Myklebust 		goto out_nobind;
6352c4044dSChuck Lever 
64b1ece737SBenjamin Coddington 	host->h_nlmclnt_ops = nlm_init->nlmclnt_ops;
6552c4044dSChuck Lever 	return host;
669a1b6bf8STrond Myklebust out_nobind:
679a1b6bf8STrond Myklebust 	nlmclnt_release_host(host);
689a1b6bf8STrond Myklebust out_nohost:
699a1b6bf8STrond Myklebust 	lockd_down(nlm_init->net);
709a1b6bf8STrond Myklebust 	return ERR_PTR(-ENOLCK);
7152c4044dSChuck Lever }
7252c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_init);
7352c4044dSChuck Lever 
7452c4044dSChuck Lever /**
7552c4044dSChuck Lever  * nlmclnt_done - Release resources allocated by nlmclnt_init()
7652c4044dSChuck Lever  * @host: nlm_host structure reserved by nlmclnt_init()
7752c4044dSChuck Lever  *
7852c4044dSChuck Lever  */
nlmclnt_done(struct nlm_host * host)7952c4044dSChuck Lever void nlmclnt_done(struct nlm_host *host)
8052c4044dSChuck Lever {
81e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
82e3f70eadSStanislav Kinsbursky 
838ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
84e3f70eadSStanislav Kinsbursky 	lockd_down(net);
8552c4044dSChuck Lever }
8652c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_done);
8752c4044dSChuck Lever 
nlmclnt_prepare_block(struct nlm_wait * block,struct nlm_host * host,struct file_lock * fl)882005f5b9SJeff Layton void nlmclnt_prepare_block(struct nlm_wait *block, struct nlm_host *host, struct file_lock *fl)
89ecdbf769STrond Myklebust {
90ecdbf769STrond Myklebust 	block->b_host = host;
91ecdbf769STrond Myklebust 	block->b_lock = fl;
92ecdbf769STrond Myklebust 	init_waitqueue_head(&block->b_wait);
93e8c5c045SAl Viro 	block->b_status = nlm_lck_blocked;
942005f5b9SJeff Layton }
9563185942SBryan Schumaker 
nlmclnt_rpc_clnt(struct nlm_host * host)962005f5b9SJeff Layton struct rpc_clnt *nlmclnt_rpc_clnt(struct nlm_host *host)
972005f5b9SJeff Layton {
982005f5b9SJeff Layton 	return host->h_rpcclnt;
992005f5b9SJeff Layton }
1002005f5b9SJeff Layton EXPORT_SYMBOL_GPL(nlmclnt_rpc_clnt);
10163185942SBryan Schumaker 
102ecdbf769STrond Myklebust /*
10363185942SBryan Schumaker  * Queue up a lock for blocking so that the GRANTED request can see it
1043a649b88STrond Myklebust  */
nlmclnt_queue_block(struct nlm_wait * block)105ecdbf769STrond Myklebust void nlmclnt_queue_block(struct nlm_wait *block)
1062005f5b9SJeff Layton {
1072005f5b9SJeff Layton 	spin_lock(&nlm_blocked_lock);
1082005f5b9SJeff Layton 	list_add(&block->b_list, &nlm_blocked);
1092005f5b9SJeff Layton 	spin_unlock(&nlm_blocked_lock);
110ecdbf769STrond Myklebust }
1112005f5b9SJeff Layton 
1122005f5b9SJeff Layton /*
11363185942SBryan Schumaker  * Dequeue the block and return its final status
114ecdbf769STrond Myklebust  */
nlmclnt_dequeue_block(struct nlm_wait * block)1152005f5b9SJeff Layton __be32 nlmclnt_dequeue_block(struct nlm_wait *block)
11663185942SBryan Schumaker {
1172005f5b9SJeff Layton 	__be32 status;
118ecdbf769STrond Myklebust 
119ecdbf769STrond Myklebust 	spin_lock(&nlm_blocked_lock);
120ecdbf769STrond Myklebust 	list_del(&block->b_list);
1211da177e4SLinus Torvalds 	status = block->b_status;
1221da177e4SLinus Torvalds 	spin_unlock(&nlm_blocked_lock);
1232005f5b9SJeff Layton 	return status;
1241da177e4SLinus Torvalds }
125ecdbf769STrond Myklebust 
1261da177e4SLinus Torvalds /*
127ecdbf769STrond Myklebust  * Block on a lock
128ecdbf769STrond Myklebust  */
nlmclnt_wait(struct nlm_wait * block,struct nlm_rqst * req,long timeout)129ecdbf769STrond Myklebust int nlmclnt_wait(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1303a649b88STrond Myklebust {
131ecdbf769STrond Myklebust 	long ret;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	/* A borken server might ask us to block even if we didn't
1341da177e4SLinus Torvalds 	 * request it. Just say no!
1351da177e4SLinus Torvalds 	 */
1361da177e4SLinus Torvalds 	if (block == NULL)
1371da177e4SLinus Torvalds 		return -EAGAIN;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Go to sleep waiting for GRANT callback. Some servers seem
1401da177e4SLinus Torvalds 	 * to lose callbacks, however, so we're going to poll from
141ecdbf769STrond Myklebust 	 * time to time just to make sure.
142e8c5c045SAl Viro 	 *
143ecdbf769STrond Myklebust 	 * For now, the retry frequency is pretty high; normally
1443a649b88STrond Myklebust 	 * a 1 minute timeout would do. See the comment before
1453a649b88STrond Myklebust 	 * nlmclnt_lock for an explanation.
1461dfd89afSTrond Myklebust 	 */
1471dfd89afSTrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
1481dfd89afSTrond Myklebust 			block->b_status != nlm_lck_blocked,
1493a649b88STrond Myklebust 			timeout);
1501da177e4SLinus Torvalds 	if (ret < 0)
1511da177e4SLinus Torvalds 		return -ERESTARTSYS;
1521da177e4SLinus Torvalds 	/* Reset the lock status after a server reboot so we resend */
1531da177e4SLinus Torvalds 	if (block->b_status == nlm_lck_denied_grace_period)
1541da177e4SLinus Torvalds 		block->b_status = nlm_lck_blocked;
155dcff09f1SChuck Lever 	return 0;
1561da177e4SLinus Torvalds }
1575ac5f9d1STrond Myklebust 
1585ac5f9d1STrond Myklebust /*
1591da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
16052921e02SAl Viro  */
nlmclnt_grant(const struct sockaddr * addr,const struct nlm_lock * lock)1611da177e4SLinus Torvalds __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	const struct file_lock *fl = &lock->fl;
1641da177e4SLinus Torvalds 	const struct nfs_fh *fh = &lock->fh;
1651da177e4SLinus Torvalds 	struct nlm_wait	*block;
16663185942SBryan Schumaker 	__be32 res = nlm_lck_denied;
1674f15e2b1STrond Myklebust 
1685ac5f9d1STrond Myklebust 	/*
1695ac5f9d1STrond Myklebust 	 * Look up blocked request based on arguments.
1707bab377fSTrond Myklebust 	 * Warning: must not use cookie to match it!
1717bab377fSTrond Myklebust 	 */
1727bab377fSTrond Myklebust 	spin_lock(&nlm_blocked_lock);
1737bab377fSTrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1747bab377fSTrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1757bab377fSTrond Myklebust 
1767bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1777bab377fSTrond Myklebust 			continue;
1787bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1795ac5f9d1STrond Myklebust 			continue;
1804516fc04SJeff Layton 		/*
1815ac5f9d1STrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
182c65454a9SJeff Layton 		 * we put on the wire: in this case the lockowner "pid".
1835ac5f9d1STrond Myklebust 		 */
184ecdbf769STrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
185ecdbf769STrond Myklebust 			continue;
1861da177e4SLinus Torvalds 		if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
187e8c5c045SAl Viro 			continue;
1881da177e4SLinus Torvalds 		if (nfs_compare_fh(NFS_FH(file_inode(fl_blocked->fl_file)), fh) != 0)
189ecdbf769STrond Myklebust 			continue;
190ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
19163185942SBryan Schumaker 		 * and wake up the caller
192*2f90e18fSJeff Layton 		 */
193ecdbf769STrond Myklebust 		block->b_status = nlm_granted;
1941da177e4SLinus Torvalds 		wake_up(&block->b_wait);
1951da177e4SLinus Torvalds 		res = nlm_granted;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	spin_unlock(&nlm_blocked_lock);
1981da177e4SLinus Torvalds 	trace_nlmclnt_grant(lock, addr, svc_addr_len(addr), res);
1991da177e4SLinus Torvalds 	return res;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds  * The following procedures deal with the recovery of locks after a
2041da177e4SLinus Torvalds  * server crash.
2051da177e4SLinus Torvalds  */
2065c8dd29cSOlaf Kirch 
2071da177e4SLinus Torvalds /*
208df94f000SJeff Layton  * Reclaim all locks on server host. We do this by spawning a separate
209df94f000SJeff Layton  * reclaimer thread.
21028df955aSTrond Myklebust  */
2111da177e4SLinus Torvalds void
nlmclnt_recovery(struct nlm_host * host)212df94f000SJeff Layton nlmclnt_recovery(struct nlm_host *host)
213df94f000SJeff Layton {
214df94f000SJeff Layton 	struct task_struct *task;
215df94f000SJeff Layton 
216df94f000SJeff Layton 	if (!host->h_reclaiming++) {
2171da177e4SLinus Torvalds 		nlm_get_host(host);
2181da177e4SLinus Torvalds 		task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
2191da177e4SLinus Torvalds 		if (IS_ERR(task))
2201da177e4SLinus Torvalds 			printk(KERN_ERR "lockd: unable to spawn reclaimer "
2211da177e4SLinus Torvalds 				"thread. Locks for %s won't be reclaimed! "
2221da177e4SLinus Torvalds 				"(%ld)\n", host->h_name, PTR_ERR(task));
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
225f25cc71eSTim Gardner 
22626bcbf96SChristoph Hellwig static int
reclaimer(void * ptr)22728df955aSTrond Myklebust reclaimer(void *ptr)
228e3f70eadSStanislav Kinsbursky {
2291da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
230f25cc71eSTim Gardner 	struct nlm_wait	  *block;
23158a69893SMarkus Elfring 	struct nlm_rqst   *req;
232f25cc71eSTim Gardner 	struct file_lock *fl, *next;
233f25cc71eSTim Gardner 	u32 nsmstate;
2341da177e4SLinus Torvalds 	struct net *net = host->net;
2351da177e4SLinus Torvalds 
2365c8dd29cSOlaf Kirch 	req = kmalloc(sizeof(*req), GFP_KERNEL);
23740373b12STrond Myklebust 	if (!req)
2381da177e4SLinus Torvalds 		return 0;
239d019bcf0SAdrian Bunk 
2405c8dd29cSOlaf Kirch 	allow_signal(SIGKILL);
2411da177e4SLinus Torvalds 
24228df955aSTrond Myklebust 	down_write(&host->h_rwsem);
2435c8dd29cSOlaf Kirch 	lockd_up(net, NULL);	/* note: this cannot fail as lockd is already running */
2445c8dd29cSOlaf Kirch 
2455c8dd29cSOlaf Kirch 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2465c8dd29cSOlaf Kirch 
2470ade060eSOlaf Kirch restart:
2485c8dd29cSOlaf Kirch 	nsmstate = host->h_nsmstate;
2495c8dd29cSOlaf Kirch 
2505c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2515c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
25226bcbf96SChristoph Hellwig 	 */
2534c060b53STrond Myklebust 	host->h_nextrebind = jiffies;
2541da177e4SLinus Torvalds 	nlm_rebind_host(host);
255df94f000SJeff Layton 
256df94f000SJeff Layton 	/* First, reclaim all locks that have been granted. */
257df94f000SJeff Layton 	list_splice_init(&host->h_granted, &host->h_reclaim);
258df94f000SJeff Layton 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
259df94f000SJeff Layton 		list_del_init(&fl->fl_u.nfs_fl.list);
260df94f000SJeff Layton 
2611da177e4SLinus Torvalds 		/*
2624c060b53STrond Myklebust 		 * sending this thread a SIGKILL will result in any unreclaimed
263f25cc71eSTim Gardner 		 * locks being removed from the h_granted list. This means that
26428df955aSTrond Myklebust 		 * the kernel will not attempt to reclaim them again if a new
2654c060b53STrond Myklebust 		 * reclaimer thread is spawned for this host.
26628df955aSTrond Myklebust 		 */
26728df955aSTrond Myklebust 		if (signalled())
2681da177e4SLinus Torvalds 			continue;
2691da177e4SLinus Torvalds 		if (nlmclnt_reclaim(host, fl, req) != 0)
27028df955aSTrond Myklebust 			continue;
2715c8dd29cSOlaf Kirch 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
2725c8dd29cSOlaf Kirch 		if (host->h_nsmstate != nsmstate) {
2735c8dd29cSOlaf Kirch 			/* Argh! The server rebooted again! */
274d019bcf0SAdrian Bunk 			goto restart;
2751da177e4SLinus Torvalds 		}
2761da177e4SLinus Torvalds 	}
27763185942SBryan Schumaker 
2784f15e2b1STrond Myklebust 	host->h_reclaiming = 0;
2791da177e4SLinus Torvalds 	up_write(&host->h_rwsem);
280e8c5c045SAl Viro 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
2831da177e4SLinus Torvalds 	spin_lock(&nlm_blocked_lock);
28463185942SBryan Schumaker 	list_for_each_entry(block, &nlm_blocked, b_list) {
2851da177e4SLinus Torvalds 		if (block->b_host == host) {
2861da177e4SLinus Torvalds 			block->b_status = nlm_lck_denied_grace_period;
2878ea6ecc8SChuck Lever 			wake_up(&block->b_wait);
288e3f70eadSStanislav Kinsbursky 		}
289f25cc71eSTim Gardner 	}
290df94f000SJeff Layton 	spin_unlock(&nlm_blocked_lock);
2911da177e4SLinus Torvalds 
292 	/* Release host handle after use */
293 	nlmclnt_release_host(host);
294 	lockd_down(net);
295 	kfree(req);
296 	return 0;
297 }
298