Merge branch 'stable/gntdev' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / lockd / clntlock.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/lockd/clntlock.c
3 *
4 * Lock handling for the client side NLM implementation
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/module.h>
10#include <linux/types.h>
5a0e3ad6 11#include <linux/slab.h>
1da177e4
LT
12#include <linux/time.h>
13#include <linux/nfs_fs.h>
14#include <linux/sunrpc/clnt.h>
15#include <linux/sunrpc/svc.h>
16#include <linux/lockd/lockd.h>
df94f000 17#include <linux/kthread.h>
1da177e4
LT
18
19#define NLMDBG_FACILITY NLMDBG_CLIENT
20
21/*
22 * Local function prototypes
23 */
24static int reclaimer(void *ptr);
25
26/*
27 * The following functions handle blocking and granting from the
28 * client perspective.
29 */
30
31/*
32 * This is the representation of a blocked client lock.
33 */
34struct nlm_wait {
4f15e2b1 35 struct list_head b_list; /* linked list */
1da177e4
LT
36 wait_queue_head_t b_wait; /* where to wait on */
37 struct nlm_host * b_host;
38 struct file_lock * b_lock; /* local file lock */
39 unsigned short b_reclaim; /* got to reclaim lock */
e8c5c045 40 __be32 b_status; /* grant callback status */
1da177e4
LT
41};
42
4f15e2b1 43static LIST_HEAD(nlm_blocked);
63185942 44static DEFINE_SPINLOCK(nlm_blocked_lock);
1da177e4 45
52c4044d
CL
46/**
47 * nlmclnt_init - Set up per-NFS mount point lockd data structures
883bb163 48 * @nlm_init: pointer to arguments structure
52c4044d
CL
49 *
50 * Returns pointer to an appropriate nlm_host struct,
51 * or an ERR_PTR value.
52 */
883bb163 53struct nlm_host *nlmclnt_init(const struct nlmclnt_initdata *nlm_init)
52c4044d
CL
54{
55 struct nlm_host *host;
883bb163 56 u32 nlm_version = (nlm_init->nfs_version == 2) ? 1 : 4;
52c4044d
CL
57 int status;
58
26a41409 59 status = lockd_up();
52c4044d
CL
60 if (status < 0)
61 return ERR_PTR(status);
62
d7d20440 63 host = nlmclnt_lookup_host(nlm_init->address, nlm_init->addrlen,
883bb163 64 nlm_init->protocol, nlm_version,
0cb2659b 65 nlm_init->hostname, nlm_init->noresvport);
52c4044d
CL
66 if (host == NULL) {
67 lockd_down();
68 return ERR_PTR(-ENOLCK);
69 }
70
71 return host;
72}
73EXPORT_SYMBOL_GPL(nlmclnt_init);
74
75/**
76 * nlmclnt_done - Release resources allocated by nlmclnt_init()
77 * @host: nlm_host structure reserved by nlmclnt_init()
78 *
79 */
80void nlmclnt_done(struct nlm_host *host)
81{
8ea6ecc8 82 nlmclnt_release_host(host);
52c4044d
CL
83 lockd_down();
84}
85EXPORT_SYMBOL_GPL(nlmclnt_done);
86
1da177e4 87/*
ecdbf769 88 * Queue up a lock for blocking so that the GRANTED request can see it
1da177e4 89 */
3a649b88 90struct nlm_wait *nlmclnt_prepare_block(struct nlm_host *host, struct file_lock *fl)
ecdbf769
TM
91{
92 struct nlm_wait *block;
93
ecdbf769 94 block = kmalloc(sizeof(*block), GFP_KERNEL);
3a649b88
TM
95 if (block != NULL) {
96 block->b_host = host;
97 block->b_lock = fl;
98 init_waitqueue_head(&block->b_wait);
e8c5c045 99 block->b_status = nlm_lck_blocked;
63185942
BS
100
101 spin_lock(&nlm_blocked_lock);
3a649b88 102 list_add(&block->b_list, &nlm_blocked);
63185942 103 spin_unlock(&nlm_blocked_lock);
3a649b88
TM
104 }
105 return block;
ecdbf769
TM
106}
107
3a649b88 108void nlmclnt_finish_block(struct nlm_wait *block)
1da177e4 109{
ecdbf769
TM
110 if (block == NULL)
111 return;
63185942 112 spin_lock(&nlm_blocked_lock);
ecdbf769 113 list_del(&block->b_list);
63185942 114 spin_unlock(&nlm_blocked_lock);
ecdbf769
TM
115 kfree(block);
116}
1da177e4 117
ecdbf769
TM
118/*
119 * Block on a lock
120 */
3a649b88 121int nlmclnt_block(struct nlm_wait *block, struct nlm_rqst *req, long timeout)
ecdbf769 122{
ecdbf769 123 long ret;
1da177e4 124
ecdbf769
TM
125 /* A borken server might ask us to block even if we didn't
126 * request it. Just say no!
127 */
3a649b88 128 if (block == NULL)
ecdbf769 129 return -EAGAIN;
1da177e4
LT
130
131 /* Go to sleep waiting for GRANT callback. Some servers seem
132 * to lose callbacks, however, so we're going to poll from
133 * time to time just to make sure.
134 *
135 * For now, the retry frequency is pretty high; normally
136 * a 1 minute timeout would do. See the comment before
137 * nlmclnt_lock for an explanation.
138 */
ecdbf769 139 ret = wait_event_interruptible_timeout(block->b_wait,
e8c5c045 140 block->b_status != nlm_lck_blocked,
ecdbf769 141 timeout);
3a649b88
TM
142 if (ret < 0)
143 return -ERESTARTSYS;
144 req->a_res.status = block->b_status;
145 return 0;
1da177e4
LT
146}
147
148/*
149 * The server lockd has called us back to tell us the lock was granted
150 */
dcff09f1 151__be32 nlmclnt_grant(const struct sockaddr *addr, const struct nlm_lock *lock)
1da177e4 152{
5ac5f9d1
TM
153 const struct file_lock *fl = &lock->fl;
154 const struct nfs_fh *fh = &lock->fh;
1da177e4 155 struct nlm_wait *block;
52921e02 156 __be32 res = nlm_lck_denied;
1da177e4
LT
157
158 /*
159 * Look up blocked request based on arguments.
160 * Warning: must not use cookie to match it!
161 */
63185942 162 spin_lock(&nlm_blocked_lock);
4f15e2b1 163 list_for_each_entry(block, &nlm_blocked, b_list) {
5ac5f9d1
TM
164 struct file_lock *fl_blocked = block->b_lock;
165
7bab377f
TM
166 if (fl_blocked->fl_start != fl->fl_start)
167 continue;
168 if (fl_blocked->fl_end != fl->fl_end)
169 continue;
170 /*
171 * Careful! The NLM server will return the 32-bit "pid" that
172 * we put on the wire: in this case the lockowner "pid".
173 */
174 if (fl_blocked->fl_u.nfs_fl.owner->pid != lock->svid)
5ac5f9d1 175 continue;
4516fc04 176 if (!rpc_cmp_addr(nlm_addr(block->b_host), addr))
5ac5f9d1 177 continue;
225a719f 178 if (nfs_compare_fh(NFS_FH(fl_blocked->fl_file->f_path.dentry->d_inode) ,fh) != 0)
5ac5f9d1
TM
179 continue;
180 /* Alright, we found a lock. Set the return status
181 * and wake up the caller
182 */
e8c5c045 183 block->b_status = nlm_granted;
5ac5f9d1
TM
184 wake_up(&block->b_wait);
185 res = nlm_granted;
1da177e4 186 }
63185942 187 spin_unlock(&nlm_blocked_lock);
ecdbf769 188 return res;
1da177e4
LT
189}
190
191/*
192 * The following procedures deal with the recovery of locks after a
193 * server crash.
194 */
195
1da177e4
LT
196/*
197 * Reclaim all locks on server host. We do this by spawning a separate
198 * reclaimer thread.
199 */
200void
5c8dd29c 201nlmclnt_recovery(struct nlm_host *host)
1da177e4 202{
df94f000
JL
203 struct task_struct *task;
204
28df955a 205 if (!host->h_reclaiming++) {
1da177e4 206 nlm_get_host(host);
df94f000
JL
207 task = kthread_run(reclaimer, host, "%s-reclaim", host->h_name);
208 if (IS_ERR(task))
209 printk(KERN_ERR "lockd: unable to spawn reclaimer "
210 "thread. Locks for %s won't be reclaimed! "
211 "(%ld)\n", host->h_name, PTR_ERR(task));
1da177e4
LT
212 }
213}
214
215static int
216reclaimer(void *ptr)
217{
218 struct nlm_host *host = (struct nlm_host *) ptr;
219 struct nlm_wait *block;
26bcbf96 220 struct file_lock *fl, *next;
28df955a 221 u32 nsmstate;
1da177e4 222
1da177e4
LT
223 allow_signal(SIGKILL);
224
5c8dd29c 225 down_write(&host->h_rwsem);
26a41409 226 lockd_up(); /* note: this cannot fail as lockd is already running */
1da177e4 227
d019bcf0 228 dprintk("lockd: reclaiming locks for host %s\n", host->h_name);
5c8dd29c 229
1da177e4 230restart:
28df955a 231 nsmstate = host->h_nsmstate;
5c8dd29c
OK
232
233 /* Force a portmap getport - the peer's lockd will
234 * most likely end up on a different port.
235 */
0ade060e 236 host->h_nextrebind = jiffies;
5c8dd29c
OK
237 nlm_rebind_host(host);
238
239 /* First, reclaim all locks that have been granted. */
240 list_splice_init(&host->h_granted, &host->h_reclaim);
26bcbf96 241 list_for_each_entry_safe(fl, next, &host->h_reclaim, fl_u.nfs_fl.list) {
4c060b53 242 list_del_init(&fl->fl_u.nfs_fl.list);
1da177e4 243
df94f000
JL
244 /*
245 * sending this thread a SIGKILL will result in any unreclaimed
246 * locks being removed from the h_granted list. This means that
247 * the kernel will not attempt to reclaim them again if a new
248 * reclaimer thread is spawned for this host.
249 */
1da177e4 250 if (signalled())
4c060b53 251 continue;
28df955a
TM
252 if (nlmclnt_reclaim(host, fl) != 0)
253 continue;
254 list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
255 if (host->h_nsmstate != nsmstate) {
256 /* Argh! The server rebooted again! */
28df955a
TM
257 goto restart;
258 }
1da177e4 259 }
5c8dd29c
OK
260
261 host->h_reclaiming = 0;
262 up_write(&host->h_rwsem);
d019bcf0 263 dprintk("NLM: done reclaiming locks for host %s\n", host->h_name);
1da177e4
LT
264
265 /* Now, wake up all processes that sleep on a blocked lock */
63185942 266 spin_lock(&nlm_blocked_lock);
4f15e2b1 267 list_for_each_entry(block, &nlm_blocked, b_list) {
1da177e4 268 if (block->b_host == host) {
e8c5c045 269 block->b_status = nlm_lck_denied_grace_period;
1da177e4
LT
270 wake_up(&block->b_wait);
271 }
272 }
63185942 273 spin_unlock(&nlm_blocked_lock);
1da177e4
LT
274
275 /* Release host handle after use */
8ea6ecc8 276 nlmclnt_release_host(host);
1da177e4 277 lockd_down();
df94f000 278 return 0;
1da177e4 279}