xref: /openbmc/linux/fs/lockd/clntlock.c (revision d97c0589)
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>
172f90e18fSJeff Layton #include <linux/sunrpc/svc_xprt.h>
181da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
19df94f000SJeff Layton #include <linux/kthread.h>
201da177e4SLinus Torvalds 
212f90e18fSJeff Layton #include "trace.h"
222f90e18fSJeff 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)96*d97c0589SBenjamin Coddington struct rpc_clnt *nlmclnt_rpc_clnt(struct nlm_host *host)
97*d97c0589SBenjamin Coddington {
98*d97c0589SBenjamin Coddington 	return host->h_rpcclnt;
99*d97c0589SBenjamin Coddington }
100*d97c0589SBenjamin Coddington EXPORT_SYMBOL_GPL(nlmclnt_rpc_clnt);
101*d97c0589SBenjamin Coddington 
1022005f5b9SJeff Layton /*
1032005f5b9SJeff Layton  * Queue up a lock for blocking so that the GRANTED request can see it
1042005f5b9SJeff Layton  */
nlmclnt_queue_block(struct nlm_wait * block)1052005f5b9SJeff Layton void nlmclnt_queue_block(struct nlm_wait *block)
1062005f5b9SJeff Layton {
10763185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
108ecdbf769STrond Myklebust 	list_add(&block->b_list, &nlm_blocked);
10963185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
1103a649b88STrond Myklebust }
111ecdbf769STrond Myklebust 
1122005f5b9SJeff Layton /*
1132005f5b9SJeff Layton  * Dequeue the block and return its final status
1142005f5b9SJeff Layton  */
nlmclnt_dequeue_block(struct nlm_wait * block)1152005f5b9SJeff Layton __be32 nlmclnt_dequeue_block(struct nlm_wait *block)
116ecdbf769STrond Myklebust {
1172005f5b9SJeff Layton 	__be32 status;
1182005f5b9SJeff Layton 
11963185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
120ecdbf769STrond Myklebust 	list_del(&block->b_list);
1212005f5b9SJeff Layton 	status = block->b_status;
12263185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
1232005f5b9SJeff Layton 	return status;
124ecdbf769STrond Myklebust }
125ecdbf769STrond Myklebust 
126ecdbf769STrond Myklebust /*
1271da177e4SLinus Torvalds  * Block on a lock
1281da177e4SLinus Torvalds  */
nlmclnt_wait(struct nlm_wait * block,struct nlm_rqst * req,long timeout)1292005f5b9SJeff Layton int nlmclnt_wait(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1301da177e4SLinus Torvalds {
131ecdbf769STrond Myklebust 	long ret;
1321da177e4SLinus Torvalds 
133ecdbf769STrond Myklebust 	/* A borken server might ask us to block even if we didn't
134ecdbf769STrond Myklebust 	 * request it. Just say no!
135ecdbf769STrond Myklebust 	 */
1363a649b88STrond Myklebust 	if (block == NULL)
137ecdbf769STrond Myklebust 		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
1411da177e4SLinus Torvalds 	 * time to time just to make sure.
1421da177e4SLinus Torvalds 	 *
1431da177e4SLinus Torvalds 	 * For now, the retry frequency is pretty high; normally
1441da177e4SLinus Torvalds 	 * a 1 minute timeout would do. See the comment before
1451da177e4SLinus Torvalds 	 * nlmclnt_lock for an explanation.
1461da177e4SLinus Torvalds 	 */
147ecdbf769STrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
148e8c5c045SAl Viro 			block->b_status != nlm_lck_blocked,
149ecdbf769STrond Myklebust 			timeout);
1503a649b88STrond Myklebust 	if (ret < 0)
1513a649b88STrond Myklebust 		return -ERESTARTSYS;
1521dfd89afSTrond Myklebust 	/* Reset the lock status after a server reboot so we resend */
1531dfd89afSTrond Myklebust 	if (block->b_status == nlm_lck_denied_grace_period)
1541dfd89afSTrond Myklebust 		block->b_status = nlm_lck_blocked;
1553a649b88STrond Myklebust 	return 0;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /*
1591da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
1601da177e4SLinus Torvalds  */
nlmclnt_grant(const struct sockaddr * addr,const struct nlm_lock * lock)161dcff09f1SChuck Lever __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1621da177e4SLinus Torvalds {
1635ac5f9d1STrond Myklebust 	const struct file_lock *fl = &lock->fl;
1645ac5f9d1STrond Myklebust 	const struct nfs_fh *fh = &lock->fh;
1651da177e4SLinus Torvalds 	struct nlm_wait	*block;
16652921e02SAl Viro 	__be32 res = nlm_lck_denied;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	/*
1691da177e4SLinus Torvalds 	 * Look up blocked request based on arguments.
1701da177e4SLinus Torvalds 	 * Warning: must not use cookie to match it!
1711da177e4SLinus Torvalds 	 */
17263185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
1734f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1745ac5f9d1STrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1755ac5f9d1STrond Myklebust 
1767bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1777bab377fSTrond Myklebust 			continue;
1787bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1797bab377fSTrond Myklebust 			continue;
1807bab377fSTrond Myklebust 		/*
1817bab377fSTrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
1827bab377fSTrond Myklebust 		 * we put on the wire: in this case the lockowner "pid".
1837bab377fSTrond Myklebust 		 */
1847bab377fSTrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
1855ac5f9d1STrond Myklebust 			continue;
1864516fc04SJeff Layton 		if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
1875ac5f9d1STrond Myklebust 			continue;
188c65454a9SJeff Layton 		if (nfs_compare_fh(NFS_FH(file_inode(fl_blocked->fl_file)), fh) != 0)
1895ac5f9d1STrond Myklebust 			continue;
190ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
191ecdbf769STrond Myklebust 		 * and wake up the caller
1921da177e4SLinus Torvalds 		 */
193e8c5c045SAl Viro 		block->b_status = nlm_granted;
1941da177e4SLinus Torvalds 		wake_up(&block->b_wait);
195ecdbf769STrond Myklebust 		res = nlm_granted;
196ecdbf769STrond Myklebust 	}
19763185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
1982f90e18fSJeff Layton 	trace_nlmclnt_grant(lock, addr, svc_addr_len(addr), res);
199ecdbf769STrond Myklebust 	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  */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  * Reclaim all locks on server host. We do this by spawning a separate
2091da177e4SLinus Torvalds  * reclaimer thread.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds void
nlmclnt_recovery(struct nlm_host * host)2125c8dd29cSOlaf Kirch nlmclnt_recovery(struct nlm_host *host)
2131da177e4SLinus Torvalds {
214df94f000SJeff Layton 	struct task_struct *task;
215df94f000SJeff Layton 
21628df955aSTrond Myklebust 	if (!host->h_reclaiming++) {
2171da177e4SLinus Torvalds 		nlm_get_host(host);
218df94f000SJeff Layton 		task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
219df94f000SJeff Layton 		if (IS_ERR(task))
220df94f000SJeff Layton 			printk(KERN_ERR "lockd: unable to spawn reclaimer "
221df94f000SJeff Layton 				"thread. Locks for %s won't be reclaimed! "
222df94f000SJeff Layton 				"(%ld)\n", host->h_name, PTR_ERR(task));
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds static int
reclaimer(void * ptr)2271da177e4SLinus Torvalds reclaimer(void *ptr)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
2301da177e4SLinus Torvalds 	struct nlm_wait	  *block;
231f25cc71eSTim Gardner 	struct nlm_rqst   *req;
23226bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
23328df955aSTrond Myklebust 	u32 nsmstate;
234e3f70eadSStanislav Kinsbursky 	struct net *net = host->net;
2351da177e4SLinus Torvalds 
236f25cc71eSTim Gardner 	req = kmalloc(sizeof(*req), GFP_KERNEL);
23758a69893SMarkus Elfring 	if (!req)
238f25cc71eSTim Gardner 		return 0;
239f25cc71eSTim Gardner 
2401da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2411da177e4SLinus Torvalds 
2425c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
24340373b12STrond Myklebust 	lockd_up(net, NULL);	/* note: this cannot fail as lockd is already running */
2441da177e4SLinus Torvalds 
245d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2465c8dd29cSOlaf Kirch 
2471da177e4SLinus Torvalds restart:
24828df955aSTrond Myklebust 	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.
2525c8dd29cSOlaf Kirch 	 */
2530ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2545c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2555c8dd29cSOlaf Kirch 
2565c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2575c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
25826bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2594c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2601da177e4SLinus Torvalds 
261df94f000SJeff Layton 		/*
262df94f000SJeff Layton 		 * sending this thread a SIGKILL will result in any unreclaimed
263df94f000SJeff Layton 		 * locks being removed from the h_granted list. This means that
264df94f000SJeff Layton 		 * the kernel will not attempt to reclaim them again if a new
265df94f000SJeff Layton 		 * reclaimer thread is spawned for this host.
266df94f000SJeff Layton 		 */
2671da177e4SLinus Torvalds 		if (signalled())
2684c060b53STrond Myklebust 			continue;
269f25cc71eSTim Gardner 		if (nlmclnt_reclaim(host, fl, req) != 0)
27028df955aSTrond Myklebust 			continue;
2714c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
27228df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
27328df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2741da177e4SLinus Torvalds 			goto restart;
2751da177e4SLinus Torvalds 		}
27628df955aSTrond Myklebust 	}
2775c8dd29cSOlaf Kirch 
2785c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2795c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
280d019bcf0SAdrian Bunk 	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 */
28363185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
2844f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2851da177e4SLinus Torvalds 		if (block->b_host == host) {
286e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2871da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 	}
29063185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	/* Release host handle after use */
2938ea6ecc8SChuck Lever 	nlmclnt_release_host(host);
294e3f70eadSStanislav Kinsbursky 	lockd_down(net);
295f25cc71eSTim Gardner 	kfree(req);
296df94f000SJeff Layton 	return 0;
2971da177e4SLinus Torvalds }
298