xref: /openbmc/linux/fs/lockd/clntlock.c (revision 26a41409)
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>
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/nfs_fs.h>
131da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
141da177e4SLinus Torvalds #include <linux/sunrpc/svc.h>
151da177e4SLinus Torvalds #include <linux/lockd/lockd.h>
161da177e4SLinus Torvalds #include <linux/smp_lock.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #define NLMDBG_FACILITY		NLMDBG_CLIENT
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /*
211da177e4SLinus Torvalds  * Local function prototypes
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds static int			reclaimer(void *ptr);
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * The following functions handle blocking and granting from the
271da177e4SLinus Torvalds  * client perspective.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  * This is the representation of a blocked client lock.
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds struct nlm_wait {
344f15e2b1STrond Myklebust 	struct list_head	b_list;		/* linked list */
351da177e4SLinus Torvalds 	wait_queue_head_t	b_wait;		/* where to wait on */
361da177e4SLinus Torvalds 	struct nlm_host *	b_host;
371da177e4SLinus Torvalds 	struct file_lock *	b_lock;		/* local file lock */
381da177e4SLinus Torvalds 	unsigned short		b_reclaim;	/* got to reclaim lock */
39e8c5c045SAl Viro 	__be32			b_status;	/* grant callback status */
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
424f15e2b1STrond Myklebust static LIST_HEAD(nlm_blocked);
431da177e4SLinus Torvalds 
4452c4044dSChuck Lever /**
4552c4044dSChuck Lever  * nlmclnt_init - Set up per-NFS mount point lockd data structures
46883bb163SChuck Lever  * @nlm_init: pointer to arguments structure
4752c4044dSChuck Lever  *
4852c4044dSChuck Lever  * Returns pointer to an appropriate nlm_host struct,
4952c4044dSChuck Lever  * or an ERR_PTR value.
5052c4044dSChuck Lever  */
51883bb163SChuck Lever struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
5252c4044dSChuck Lever {
5352c4044dSChuck Lever 	struct nlm_host *host;
54883bb163SChuck Lever 	u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
5552c4044dSChuck Lever 	int status;
5652c4044dSChuck Lever 
5726a41409SChuck Lever 	status = lockd_up();
5852c4044dSChuck Lever 	if (status < 0)
5952c4044dSChuck Lever 		return ERR_PTR(status);
6052c4044dSChuck Lever 
61d7d20440SChuck Lever 	host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
62883bb163SChuck Lever 				   nlm_init->protocol, nlm_version,
63d7d20440SChuck Lever 				   nlm_init->hostname);
6452c4044dSChuck Lever 	if (host == NULL) {
6552c4044dSChuck Lever 		lockd_down();
6652c4044dSChuck Lever 		return ERR_PTR(-ENOLCK);
6752c4044dSChuck Lever 	}
6852c4044dSChuck Lever 
6952c4044dSChuck Lever 	return host;
7052c4044dSChuck Lever }
7152c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_init);
7252c4044dSChuck Lever 
7352c4044dSChuck Lever /**
7452c4044dSChuck Lever  * nlmclnt_done - Release resources allocated by nlmclnt_init()
7552c4044dSChuck Lever  * @host: nlm_host structure reserved by nlmclnt_init()
7652c4044dSChuck Lever  *
7752c4044dSChuck Lever  */
7852c4044dSChuck Lever void nlmclnt_done(struct nlm_host *host)
7952c4044dSChuck Lever {
8052c4044dSChuck Lever 	nlm_release_host(host);
8152c4044dSChuck Lever 	lockd_down();
8252c4044dSChuck Lever }
8352c4044dSChuck Lever EXPORT_SYMBOL_GPL(nlmclnt_done);
8452c4044dSChuck Lever 
851da177e4SLinus Torvalds /*
86ecdbf769STrond Myklebust  * Queue up a lock for blocking so that the GRANTED request can see it
87ecdbf769STrond Myklebust  */
883a649b88STrond Myklebust struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
89ecdbf769STrond Myklebust {
90ecdbf769STrond Myklebust 	struct nlm_wait *block;
91ecdbf769STrond Myklebust 
92ecdbf769STrond Myklebust 	block = kmalloc(sizeof(*block), GFP_KERNEL);
933a649b88STrond Myklebust 	if (block != NULL) {
94ecdbf769STrond Myklebust 		block->b_host = host;
95ecdbf769STrond Myklebust 		block->b_lock = fl;
96ecdbf769STrond Myklebust 		init_waitqueue_head(&block->b_wait);
97e8c5c045SAl Viro 		block->b_status = nlm_lck_blocked;
98ecdbf769STrond Myklebust 		list_add(&block->b_list, &nlm_blocked);
993a649b88STrond Myklebust 	}
1003a649b88STrond Myklebust 	return block;
101ecdbf769STrond Myklebust }
102ecdbf769STrond Myklebust 
1033a649b88STrond Myklebust void nlmclnt_finish_block(struct nlm_wait *block)
104ecdbf769STrond Myklebust {
105ecdbf769STrond Myklebust 	if (block == NULL)
106ecdbf769STrond Myklebust 		return;
107ecdbf769STrond Myklebust 	list_del(&block->b_list);
108ecdbf769STrond Myklebust 	kfree(block);
109ecdbf769STrond Myklebust }
110ecdbf769STrond Myklebust 
111ecdbf769STrond Myklebust /*
1121da177e4SLinus Torvalds  * Block on a lock
1131da177e4SLinus Torvalds  */
1143a649b88STrond Myklebust int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
1151da177e4SLinus Torvalds {
116ecdbf769STrond Myklebust 	long ret;
1171da177e4SLinus Torvalds 
118ecdbf769STrond Myklebust 	/* A borken server might ask us to block even if we didn't
119ecdbf769STrond Myklebust 	 * request it. Just say no!
120ecdbf769STrond Myklebust 	 */
1213a649b88STrond Myklebust 	if (block == NULL)
122ecdbf769STrond Myklebust 		return -EAGAIN;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	/* Go to sleep waiting for GRANT callback. Some servers seem
1251da177e4SLinus Torvalds 	 * to lose callbacks, however, so we're going to poll from
1261da177e4SLinus Torvalds 	 * time to time just to make sure.
1271da177e4SLinus Torvalds 	 *
1281da177e4SLinus Torvalds 	 * For now, the retry frequency is pretty high; normally
1291da177e4SLinus Torvalds 	 * a 1 minute timeout would do. See the comment before
1301da177e4SLinus Torvalds 	 * nlmclnt_lock for an explanation.
1311da177e4SLinus Torvalds 	 */
132ecdbf769STrond Myklebust 	ret = wait_event_interruptible_timeout(block->b_wait,
133e8c5c045SAl Viro 			block->b_status != nlm_lck_blocked,
134ecdbf769STrond Myklebust 			timeout);
1353a649b88STrond Myklebust 	if (ret < 0)
1363a649b88STrond Myklebust 		return -ERESTARTSYS;
137ecdbf769STrond Myklebust 	req->a_res.status = block->b_status;
1383a649b88STrond Myklebust 	return 0;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds  * The server lockd has called us back to tell us the lock was granted
1431da177e4SLinus Torvalds  */
144dcff09f1SChuck Lever __be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1451da177e4SLinus Torvalds {
1465ac5f9d1STrond Myklebust 	const struct file_lock *fl = &lock->fl;
1475ac5f9d1STrond Myklebust 	const struct nfs_fh *fh = &lock->fh;
1481da177e4SLinus Torvalds 	struct nlm_wait	*block;
14952921e02SAl Viro 	__be32 res = nlm_lck_denied;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	/*
1521da177e4SLinus Torvalds 	 * Look up blocked request based on arguments.
1531da177e4SLinus Torvalds 	 * Warning: must not use cookie to match it!
1541da177e4SLinus Torvalds 	 */
1554f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
1565ac5f9d1STrond Myklebust 		struct file_lock *fl_blocked = block->b_lock;
1575ac5f9d1STrond Myklebust 
1587bab377fSTrond Myklebust 		if (fl_blocked->fl_start != fl->fl_start)
1597bab377fSTrond Myklebust 			continue;
1607bab377fSTrond Myklebust 		if (fl_blocked->fl_end != fl->fl_end)
1617bab377fSTrond Myklebust 			continue;
1627bab377fSTrond Myklebust 		/*
1637bab377fSTrond Myklebust 		 * Careful! The NLM server will return the 32-bit "pid" that
1647bab377fSTrond Myklebust 		 * we put on the wire: in this case the lockowner "pid".
1657bab377fSTrond Myklebust 		 */
1667bab377fSTrond Myklebust 		if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
1675ac5f9d1STrond Myklebust 			continue;
168dcff09f1SChuck Lever 		if (!nlm_cmp_addr(nlm_addr(block->b_host), addr))
1695ac5f9d1STrond Myklebust 			continue;
170225a719fSJosef Sipek 		if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,fh) != 0)
1715ac5f9d1STrond Myklebust 			continue;
172ecdbf769STrond Myklebust 		/* Alright, we found a lock. Set the return status
173ecdbf769STrond Myklebust 		 * and wake up the caller
1741da177e4SLinus Torvalds 		 */
175e8c5c045SAl Viro 		block->b_status = nlm_granted;
1761da177e4SLinus Torvalds 		wake_up(&block->b_wait);
177ecdbf769STrond Myklebust 		res = nlm_granted;
178ecdbf769STrond Myklebust 	}
179ecdbf769STrond Myklebust 	return res;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
1831da177e4SLinus Torvalds  * The following procedures deal with the recovery of locks after a
1841da177e4SLinus Torvalds  * server crash.
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * Reclaim all locks on server host. We do this by spawning a separate
1891da177e4SLinus Torvalds  * reclaimer thread.
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds void
1925c8dd29cSOlaf Kirch nlmclnt_recovery(struct nlm_host *host)
1931da177e4SLinus Torvalds {
19428df955aSTrond Myklebust 	if (!host->h_reclaiming++) {
1951da177e4SLinus Torvalds 		nlm_get_host(host);
1961da177e4SLinus Torvalds 		__module_get(THIS_MODULE);
197550facd1SOleg Nesterov 		if (kernel_thread(reclaimer, host, CLONE_FS | CLONE_FILES) < 0)
1981da177e4SLinus Torvalds 			module_put(THIS_MODULE);
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds static int
2031da177e4SLinus Torvalds reclaimer(void *ptr)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct nlm_host	  *host = (struct nlm_host *) ptr;
2061da177e4SLinus Torvalds 	struct nlm_wait	  *block;
20726bcbf96SChristoph Hellwig 	struct file_lock *fl, *next;
20828df955aSTrond Myklebust 	u32 nsmstate;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	daemonize("%s-reclaim", host->h_name);
2111da177e4SLinus Torvalds 	allow_signal(SIGKILL);
2121da177e4SLinus Torvalds 
2135c8dd29cSOlaf Kirch 	down_write(&host->h_rwsem);
2145c8dd29cSOlaf Kirch 
2151da177e4SLinus Torvalds 	/* This one ensures that our parent doesn't terminate while the
2161da177e4SLinus Torvalds 	 * reclaim is in progress */
2171da177e4SLinus Torvalds 	lock_kernel();
21826a41409SChuck Lever 	lockd_up();	/* note: this cannot fail as lockd is already running */
2191da177e4SLinus Torvalds 
220d019bcf0SAdrian Bunk 	dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
2215c8dd29cSOlaf Kirch 
2221da177e4SLinus Torvalds restart:
22328df955aSTrond Myklebust 	nsmstate = host->h_nsmstate;
2245c8dd29cSOlaf Kirch 
2255c8dd29cSOlaf Kirch 	/* Force a portmap getport - the peer's lockd will
2265c8dd29cSOlaf Kirch 	 * most likely end up on a different port.
2275c8dd29cSOlaf Kirch 	 */
2280ade060eSOlaf Kirch 	host->h_nextrebind = jiffies;
2295c8dd29cSOlaf Kirch 	nlm_rebind_host(host);
2305c8dd29cSOlaf Kirch 
2315c8dd29cSOlaf Kirch 	/* First, reclaim all locks that have been granted. */
2325c8dd29cSOlaf Kirch 	list_splice_init(&host->h_granted, &host->h_reclaim);
23326bcbf96SChristoph Hellwig 	list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
2344c060b53STrond Myklebust 		list_del_init(&fl->fl_u.nfs_fl.list);
2351da177e4SLinus Torvalds 
2365c8dd29cSOlaf Kirch 		/* Why are we leaking memory here? --okir */
2371da177e4SLinus Torvalds 		if (signalled())
2384c060b53STrond Myklebust 			continue;
23928df955aSTrond Myklebust 		if (nlmclnt_reclaim(host, fl) != 0)
24028df955aSTrond Myklebust 			continue;
2414c060b53STrond Myklebust 		list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
24228df955aSTrond Myklebust 		if (host->h_nsmstate != nsmstate) {
24328df955aSTrond Myklebust 			/* Argh! The server rebooted again! */
2441da177e4SLinus Torvalds 			goto restart;
2451da177e4SLinus Torvalds 		}
24628df955aSTrond Myklebust 	}
2475c8dd29cSOlaf Kirch 
2485c8dd29cSOlaf Kirch 	host->h_reclaiming = 0;
2495c8dd29cSOlaf Kirch 	up_write(&host->h_rwsem);
250d019bcf0SAdrian Bunk 	dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* Now, wake up all processes that sleep on a blocked lock */
2534f15e2b1STrond Myklebust 	list_for_each_entry(block, &nlm_blocked, b_list) {
2541da177e4SLinus Torvalds 		if (block->b_host == host) {
255e8c5c045SAl Viro 			block->b_status = nlm_lck_denied_grace_period;
2561da177e4SLinus Torvalds 			wake_up(&block->b_wait);
2571da177e4SLinus Torvalds 		}
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	/* Release host handle after use */
2611da177e4SLinus Torvalds 	nlm_release_host(host);
2621da177e4SLinus Torvalds 	lockd_down();
2631da177e4SLinus Torvalds 	unlock_kernel();
2641da177e4SLinus Torvalds 	module_put_and_exit(0);
2651da177e4SLinus Torvalds }
266