xref: /openbmc/linux/fs/lockd/clntlock.c (revision 63185942)
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>
171da177e4SLinus Torvalds #include <linux/smp_lock.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 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * This is the representation of a blocked client lock.
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds struct nlm_wait {
364f15e2b1STrond Myklebust 	struct list_head	b_list;		/* linked list */
371da177e4SLinus Torvalds 	wait_queue_head_t	b_wait;		/* where to wait on */
381da177e4SLinus Torvalds 	struct nlm_host *	b_host;
391da177e4SLinus Torvalds 	struct file_lock *	b_lock;		/* local file lock */
401da177e4SLinus Torvalds 	unsigned short		b_reclaim;	/* got to reclaim lock */
41e8c5c045SAl Viro 	__be32			b_status;	/* grant callback status */
421da177e4SLinus Torvalds };
431da177e4SLinus Torvalds 
444f15e2b1STrond Myklebust static LIST_HEAD(nlm_blocked);
4563185942SBryan Schumaker static DEFINE_SPINLOCK(nlm_blocked_lock);
461da177e4SLinus Torvalds 
4752c4044dSChuck Lever /**
4852c4044dSChuck Lever  * nlmclnt_init - Set up per-NFS mount point lockd data structures
49883bb163SChuck Lever  * @nlm_init: pointer to arguments structure
5052c4044dSChuck Lever  *
5152c4044dSChuck Lever  * Returns pointer to an appropriate nlm_host struct,
5252c4044dSChuck Lever  * or an ERR_PTR value.
5352c4044dSChuck Lever  */
54883bb163SChuck Lever struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
5552c4044dSChuck Lever {
5652c4044dSChuck Lever 	struct nlm_host *host;
57883bb163SChuck Lever 	u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
5852c4044dSChuck Lever 	int status;
5952c4044dSChuck Lever 
6026a41409SChuck Lever 	status = lockd_up();
6152c4044dSChuck Lever 	if (status < 0)
6252c4044dSChuck Lever 		return ERR_PTR(status);
6352c4044dSChuck Lever 
64d7d20440SChuck Lever 	host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
65883bb163SChuck Lever 				   nlm_init->protocol, nlm_version,
660cb2659bSChuck Lever 				   nlm_init->hostname, nlm_init->noresvport);
6752c4044dSChuck Lever 	if (host == NULL) {
6852c4044dSChuck Lever 		lockd_down();
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 {
8352c4044dSChuck Lever 	nlm_release_host(host);
8452c4044dSChuck Lever 	lockd_down();
8552c4044dSChuck Lever }
8652c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_done);
8752c4044dSChuck Lever 
881da177e4SLinus Torvalds /*
89ecdbf769STrond Myklebust  * Queue up a lock for blocking so that the GRANTED request can see it
90ecdbf769STrond Myklebust  */
913a649b88STrond Myklebust struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
92ecdbf769STrond Myklebust {
93ecdbf769STrond Myklebust 	struct nlm_wait *block;
94ecdbf769STrond Myklebust 
95ecdbf769STrond Myklebust 	block = kmalloc(sizeof(*block), GFP_KERNEL);
963a649b88STrond Myklebust 	if (block != NULL) {
97ecdbf769STrond Myklebust 		block->b_host = host;
98ecdbf769STrond Myklebust 		block->b_lock = fl;
99ecdbf769STrond Myklebust 		init_waitqueue_head(&block->b_wait);
100e8c5c045SAl Viro 		block->b_status = nlm_lck_blocked;
10163185942SBryan Schumaker 
10263185942SBryan Schumaker 		spin_lock(&nlm_blocked_lock);
103ecdbf769STrond Myklebust 		list_add(&block->b_list, &nlm_blocked);
10463185942SBryan Schumaker 		spin_unlock(&nlm_blocked_lock);
1053a649b88STrond Myklebust 	}
1063a649b88STrond Myklebust 	return block;
107ecdbf769STrond Myklebust }
108ecdbf769STrond Myklebust 
1093a649b88STrond Myklebust void nlmclnt_finish_block(struct nlm_wait *block)
110ecdbf769STrond Myklebust {
111ecdbf769STrond Myklebust 	if (block == NULL)
112ecdbf769STrond Myklebust 		return;
11363185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
114ecdbf769STrond Myklebust 	list_del(&block->b_list);
11563185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
116ecdbf769STrond Myklebust 	kfree(block);
117ecdbf769STrond Myklebust }
118ecdbf769STrond Myklebust 
119ecdbf769STrond Myklebust /*
1201da177e4SLinus Torvalds  * Block on a lock
1211da177e4SLinus Torvalds  */
1223a649b88STrond Myklebust int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1231da177e4SLinus Torvalds {
124ecdbf769STrond Myklebust 	long ret;
1251da177e4SLinus Torvalds 
126ecdbf769STrond Myklebust 	/* A borken server might ask us to block even if we didn't
127ecdbf769STrond Myklebust 	 * request it. Just say no!
128ecdbf769STrond Myklebust 	 */
1293a649b88STrond Myklebust 	if (block == NULL)
130ecdbf769STrond Myklebust 		return -EAGAIN;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* Go to sleep waiting for GRANT callback. Some servers seem
1331da177e4SLinus Torvalds 	 * to lose callbacks, however, so we're going to poll from
1341da177e4SLinus Torvalds 	 * time to time just to make sure.
1351da177e4SLinus Torvalds 	 *
1361da177e4SLinus Torvalds 	 * For now, the retry frequency is pretty high; normally
1371da177e4SLinus Torvalds 	 * a 1 minute timeout would do. See the comment before
1381da177e4SLinus Torvalds 	 * nlmclnt_lock for an explanation.
1391da177e4SLinus Torvalds 	 */
140ecdbf769STrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
141e8c5c045SAl Viro 			block->b_status != nlm_lck_blocked,
142ecdbf769STrond Myklebust 			timeout);
1433a649b88STrond Myklebust 	if (ret < 0)
1443a649b88STrond Myklebust 		return -ERESTARTSYS;
145ecdbf769STrond Myklebust 	req->a_res.status = block->b_status;
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;
179225a719fSJosef Sipek 		if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,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;
22126bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
22228df955aSTrond Myklebust 	u32 nsmstate;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2251da177e4SLinus Torvalds 
2265c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
22726a41409SChuck Lever 	lockd_up();	/* note: this cannot fail as lockd is already running */
2281da177e4SLinus Torvalds 
229d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2305c8dd29cSOlaf Kirch 
2311da177e4SLinus Torvalds restart:
23228df955aSTrond Myklebust 	nsmstate = host->h_nsmstate;
2335c8dd29cSOlaf Kirch 
2345c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2355c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
2365c8dd29cSOlaf Kirch 	 */
2370ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2385c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2395c8dd29cSOlaf Kirch 
2405c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2415c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
24226bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2434c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2441da177e4SLinus Torvalds 
245df94f000SJeff Layton 		/*
246df94f000SJeff Layton 		 * sending this thread a SIGKILL will result in any unreclaimed
247df94f000SJeff Layton 		 * locks being removed from the h_granted list. This means that
248df94f000SJeff Layton 		 * the kernel will not attempt to reclaim them again if a new
249df94f000SJeff Layton 		 * reclaimer thread is spawned for this host.
250df94f000SJeff Layton 		 */
2511da177e4SLinus Torvalds 		if (signalled())
2524c060b53STrond Myklebust 			continue;
25328df955aSTrond Myklebust 		if (nlmclnt_reclaim(host, fl) != 0)
25428df955aSTrond Myklebust 			continue;
2554c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
25628df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
25728df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2581da177e4SLinus Torvalds 			goto restart;
2591da177e4SLinus Torvalds 		}
26028df955aSTrond Myklebust 	}
2615c8dd29cSOlaf Kirch 
2625c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2635c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
264d019bcf0SAdrian Bunk 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
26763185942SBryan Schumaker 	spin_lock(&nlm_blocked_lock);
2684f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2691da177e4SLinus Torvalds 		if (block->b_host == host) {
270e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2711da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
27463185942SBryan Schumaker 	spin_unlock(&nlm_blocked_lock);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* Release host handle after use */
2771da177e4SLinus Torvalds 	nlm_release_host(host);
2781da177e4SLinus Torvalds 	lockd_down();
279df94f000SJeff Layton 	return 0;
2801da177e4SLinus Torvalds }
281