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