blob: 24c691f5480e215aa02fd965d166e68a35ef6f3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/clntproc.c
3 *
4 * RPC procedures for the client side NLM implementation
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/errno.h>
12#include <linux/fs.h>
13#include <linux/nfs_fs.h>
14#include <linux/utsname.h>
15#include <linux/smp_lock.h>
16#include <linux/sunrpc/clnt.h>
17#include <linux/sunrpc/svc.h>
18#include <linux/lockd/lockd.h>
19#include <linux/lockd/sm_inter.h>
20
21#define NLMDBG_FACILITY NLMDBG_CLIENT
22#define NLMCLNT_GRACE_WAIT (5*HZ)
Trond Myklebustecdbf762005-06-22 17:16:31 +000023#define NLMCLNT_POLL_TIMEOUT (30*HZ)
Trond Myklebustaaaa9942006-02-01 12:18:25 -050024#define NLMCLNT_MAX_RETRIES 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
27static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
28static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int nlm_stat_to_errno(u32 stat);
30static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
Trond Myklebust16fb2422006-02-01 12:18:22 -050031static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Trond Myklebust963d8fe2006-01-03 09:55:04 +010033static const struct rpc_call_ops nlmclnt_unlock_ops;
34static const struct rpc_call_ops nlmclnt_cancel_ops;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Cookie counter for NLM requests
38 */
39static u32 nlm_cookie = 0x1234;
40
41static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
42{
43 memcpy(c->data, &nlm_cookie, 4);
44 memset(c->data+4, 0, 4);
45 c->len=4;
46 nlm_cookie++;
47}
48
49static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
50{
51 atomic_inc(&lockowner->count);
52 return lockowner;
53}
54
55static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
56{
57 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
58 return;
59 list_del(&lockowner->list);
60 spin_unlock(&lockowner->host->h_lock);
61 nlm_release_host(lockowner->host);
62 kfree(lockowner);
63}
64
65static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
66{
67 struct nlm_lockowner *lockowner;
68 list_for_each_entry(lockowner, &host->h_lockowners, list) {
69 if (lockowner->pid == pid)
70 return -EBUSY;
71 }
72 return 0;
73}
74
75static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
76{
77 uint32_t res;
78 do {
79 res = host->h_pidcount++;
80 } while (nlm_pidbusy(host, res) < 0);
81 return res;
82}
83
84static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
85{
86 struct nlm_lockowner *lockowner;
87 list_for_each_entry(lockowner, &host->h_lockowners, list) {
88 if (lockowner->owner != owner)
89 continue;
90 return nlm_get_lockowner(lockowner);
91 }
92 return NULL;
93}
94
95static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
96{
97 struct nlm_lockowner *res, *new = NULL;
98
99 spin_lock(&host->h_lock);
100 res = __nlm_find_lockowner(host, owner);
101 if (res == NULL) {
102 spin_unlock(&host->h_lock);
103 new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL);
104 spin_lock(&host->h_lock);
105 res = __nlm_find_lockowner(host, owner);
106 if (res == NULL && new != NULL) {
107 res = new;
108 atomic_set(&new->count, 1);
109 new->owner = owner;
110 new->pid = __nlm_alloc_pid(host);
111 new->host = nlm_get_host(host);
112 list_add(&new->list, &host->h_lockowners);
113 new = NULL;
114 }
115 }
116 spin_unlock(&host->h_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800117 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return res;
119}
120
121/*
122 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
123 */
124static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
125{
126 struct nlm_args *argp = &req->a_args;
127 struct nlm_lock *lock = &argp->lock;
128
129 nlmclnt_next_cookie(&argp->cookie);
130 argp->state = nsm_local_state;
131 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
132 lock->caller = system_utsname.nodename;
133 lock->oh.data = req->a_owner;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500134 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
135 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
136 system_utsname.nodename);
137 lock->svid = fl->fl_u.nfs_fl.owner->pid;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500138 lock->fl.fl_start = fl->fl_start;
139 lock->fl.fl_end = fl->fl_end;
140 lock->fl.fl_type = fl->fl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static void nlmclnt_release_lockargs(struct nlm_rqst *req)
144{
Trond Myklebust3a649b82006-03-20 13:44:44 -0500145 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * This is the main entry point for the NLM client.
150 */
151int
152nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct nlm_host *host;
Trond Myklebust92737232006-03-20 13:44:45 -0500155 struct nlm_rqst *call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sigset_t oldset;
157 unsigned long flags;
158 int status, proto, vers;
159
160 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
161 if (NFS_PROTO(inode)->version > 3) {
162 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
163 return -ENOLCK;
164 }
165
166 /* Retrieve transport protocol from NFS client */
167 proto = NFS_CLIENT(inode)->cl_xprt->prot;
168
Trond Myklebust92737232006-03-20 13:44:45 -0500169 host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
170 if (host == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -ENOLCK;
172
Trond Myklebust92737232006-03-20 13:44:45 -0500173 call = nlm_alloc_call(host);
174 if (call == NULL)
175 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Trond Myklebust92737232006-03-20 13:44:45 -0500177 nlmclnt_locks_init_private(fl, host);
178 /* Set up the argument struct */
179 nlmclnt_setlockargs(call, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* Keep the old signal mask */
182 spin_lock_irqsave(&current->sighand->siglock, flags);
183 oldset = current->blocked;
184
185 /* If we're cleaning up locks because the process is exiting,
186 * perform the RPC call asynchronously. */
187 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
188 && fl->fl_type == F_UNLCK
189 && (current->flags & PF_EXITING)) {
190 sigfillset(&current->blocked); /* Mask all signals */
191 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 call->a_flags = RPC_TASK_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Trond Myklebust92737232006-03-20 13:44:45 -0500195 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
198 if (fl->fl_type != F_UNLCK) {
199 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
200 status = nlmclnt_lock(call, fl);
201 } else
202 status = nlmclnt_unlock(call, fl);
203 } else if (IS_GETLK(cmd))
204 status = nlmclnt_test(call, fl);
205 else
206 status = -EINVAL;
207
Trond Myklebust92737232006-03-20 13:44:45 -0500208 fl->fl_ops->fl_release_private(fl);
209 fl->fl_ops = NULL;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_lock_irqsave(&current->sighand->siglock, flags);
212 current->blocked = oldset;
213 recalc_sigpending();
214 spin_unlock_irqrestore(&current->sighand->siglock, flags);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 dprintk("lockd: clnt proc returns %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return status;
218}
219EXPORT_SYMBOL(nlmclnt_proc);
220
221/*
222 * Allocate an NLM RPC call struct
Trond Myklebust92737232006-03-20 13:44:45 -0500223 *
224 * Note: the caller must hold a reference to host. In case of failure,
225 * this reference will be released.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Trond Myklebust92737232006-03-20 13:44:45 -0500227struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct nlm_rqst *call;
230
Trond Myklebust36943fa2006-03-20 13:44:05 -0500231 for(;;) {
232 call = kzalloc(sizeof(*call), GFP_KERNEL);
233 if (call != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 locks_init_lock(&call->a_args.lock.fl);
235 locks_init_lock(&call->a_res.lock.fl);
Trond Myklebust92737232006-03-20 13:44:45 -0500236 call->a_host = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return call;
238 }
Trond Myklebust36943fa2006-03-20 13:44:05 -0500239 if (signalled())
240 break;
Trond Myklebust92737232006-03-20 13:44:45 -0500241 printk("nlm_alloc_call: failed, waiting for memory\n");
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700242 schedule_timeout_interruptible(5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Trond Myklebust92737232006-03-20 13:44:45 -0500244 nlm_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return NULL;
246}
247
Trond Myklebust92737232006-03-20 13:44:45 -0500248void nlm_release_call(struct nlm_rqst *call)
249{
250 nlm_release_host(call->a_host);
251 nlmclnt_release_lockargs(call);
252 kfree(call);
253}
254
255static void nlmclnt_rpc_release(void *data)
256{
257 return nlm_release_call(data);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int nlm_wait_on_grace(wait_queue_head_t *queue)
261{
262 DEFINE_WAIT(wait);
263 int status = -EINTR;
264
265 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
266 if (!signalled ()) {
267 schedule_timeout(NLMCLNT_GRACE_WAIT);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700268 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!signalled ())
270 status = 0;
271 }
272 finish_wait(queue, &wait);
273 return status;
274}
275
276/*
277 * Generic NLM call
278 */
279static int
280nlmclnt_call(struct nlm_rqst *req, u32 proc)
281{
282 struct nlm_host *host = req->a_host;
283 struct rpc_clnt *clnt;
284 struct nlm_args *argp = &req->a_args;
285 struct nlm_res *resp = &req->a_res;
286 struct rpc_message msg = {
287 .rpc_argp = argp,
288 .rpc_resp = resp,
289 };
290 int status;
291
292 dprintk("lockd: call procedure %d on %s\n",
293 (int)proc, host->h_name);
294
295 do {
296 if (host->h_reclaiming && !argp->reclaim)
297 goto in_grace_period;
298
299 /* If we have no RPC client yet, create one. */
300 if ((clnt = nlm_bind_host(host)) == NULL)
301 return -ENOLCK;
302 msg.rpc_proc = &clnt->cl_procinfo[proc];
303
304 /* Perform the RPC call. If an error occurs, try again */
305 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
306 dprintk("lockd: rpc_call returned error %d\n", -status);
307 switch (status) {
308 case -EPROTONOSUPPORT:
309 status = -EINVAL;
310 break;
311 case -ECONNREFUSED:
312 case -ETIMEDOUT:
313 case -ENOTCONN:
314 nlm_rebind_host(host);
315 status = -EAGAIN;
316 break;
317 case -ERESTARTSYS:
318 return signalled () ? -EINTR : status;
319 default:
320 break;
321 }
322 break;
323 } else
324 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
325 dprintk("lockd: server in grace period\n");
326 if (argp->reclaim) {
327 printk(KERN_WARNING
328 "lockd: spurious grace period reject?!\n");
329 return -ENOLCK;
330 }
331 } else {
332 if (!argp->reclaim) {
333 /* We appear to be out of the grace period */
334 wake_up_all(&host->h_gracewait);
335 }
336 dprintk("lockd: server returns status %d\n", resp->status);
337 return 0; /* Okay, call complete */
338 }
339
340in_grace_period:
341 /*
342 * The server has rebooted and appears to be in the grace
343 * period during which locks are only allowed to be
344 * reclaimed.
345 * We can only back off and try again later.
346 */
347 status = nlm_wait_on_grace(&host->h_gracewait);
348 } while (status == 0);
349
350 return status;
351}
352
353/*
354 * Generic NLM call, async version.
355 */
Trond Myklebustd4716622006-03-20 13:44:45 -0500356static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct nlm_host *host = req->a_host;
359 struct rpc_clnt *clnt;
Trond Myklebust92737232006-03-20 13:44:45 -0500360 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 dprintk("lockd: call procedure %d on %s (async)\n",
363 (int)proc, host->h_name);
364
365 /* If we have no RPC client yet, create one. */
Trond Myklebust92737232006-03-20 13:44:45 -0500366 clnt = nlm_bind_host(host);
367 if (clnt == NULL)
368 goto out_err;
Trond Myklebustd4716622006-03-20 13:44:45 -0500369 msg->rpc_proc = &clnt->cl_procinfo[proc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* bootstrap and kick off the async RPC call */
Trond Myklebustd4716622006-03-20 13:44:45 -0500372 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
Trond Myklebust92737232006-03-20 13:44:45 -0500373 if (status == 0)
374 return 0;
375out_err:
376 nlm_release_call(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return status;
378}
379
Trond Myklebustd4716622006-03-20 13:44:45 -0500380int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
381{
382 struct rpc_message msg = {
383 .rpc_argp = &req->a_args,
384 .rpc_resp = &req->a_res,
385 };
386 return __nlm_async_call(req, proc, &msg, tk_ops);
387}
388
389int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
390{
391 struct rpc_message msg = {
392 .rpc_argp = &req->a_res,
393 };
394 return __nlm_async_call(req, proc, &msg, tk_ops);
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * TEST for the presence of a conflicting lock
399 */
400static int
401nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
402{
403 int status;
404
405 status = nlmclnt_call(req, NLMPROC_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Trond Myklebust92737232006-03-20 13:44:45 -0500409 switch (req->a_res.status) {
410 case NLM_LCK_GRANTED:
411 fl->fl_type = F_UNLCK;
412 break;
413 case NLM_LCK_DENIED:
414 /*
415 * Report the conflicting lock back to the application.
416 */
417 fl->fl_start = req->a_res.lock.fl.fl_start;
418 fl->fl_end = req->a_res.lock.fl.fl_start;
419 fl->fl_type = req->a_res.lock.fl.fl_type;
420 fl->fl_pid = 0;
421 break;
422 default:
423 status = nlm_stat_to_errno(req->a_res.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Trond Myklebust92737232006-03-20 13:44:45 -0500425out:
426 nlm_release_call(req);
427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
431{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500432 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
433 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
434 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static void nlmclnt_locks_release_private(struct file_lock *fl)
438{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500439 list_del(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443static struct file_lock_operations nlmclnt_lock_ops = {
444 .fl_copy_lock = nlmclnt_locks_copy_lock,
445 .fl_release_private = nlmclnt_locks_release_private,
446};
447
448static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
449{
450 BUG_ON(fl->fl_ops != NULL);
451 fl->fl_u.nfs_fl.state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500453 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 fl->fl_ops = &nlmclnt_lock_ops;
455}
456
Trond Myklebust9b073572006-06-29 16:38:34 -0400457static int do_vfs_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 int res = 0;
460 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
461 case FL_POSIX:
462 res = posix_lock_file_wait(fl->fl_file, fl);
463 break;
464 case FL_FLOCK:
465 res = flock_lock_file_wait(fl->fl_file, fl);
466 break;
467 default:
468 BUG();
469 }
Trond Myklebust9b073572006-06-29 16:38:34 -0400470 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/*
474 * LOCK: Try to create a lock
475 *
476 * Programmer Harassment Alert
477 *
478 * When given a blocking lock request in a sync RPC call, the HPUX lockd
479 * will faithfully return LCK_BLOCKED but never cares to notify us when
480 * the lock could be granted. This way, our local process could hang
481 * around forever waiting for the callback.
482 *
483 * Solution A: Implement busy-waiting
484 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
485 *
486 * For now I am implementing solution A, because I hate the idea of
487 * re-implementing lockd for a third time in two months. The async
488 * calls shouldn't be too hard to do, however.
489 *
490 * This is one of the lovely things about standards in the NFS area:
491 * they're so soft and squishy you can't really blame HP for doing this.
492 */
493static int
494nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
495{
496 struct nlm_host *host = req->a_host;
497 struct nlm_res *resp = &req->a_res;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500498 struct nlm_wait *block = NULL;
499 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (!host->h_monitored && nsm_monitor(host) < 0) {
502 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
503 host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out;
505 }
506
Trond Myklebust3a649b82006-03-20 13:44:44 -0500507 block = nlmclnt_prepare_block(host, fl);
Trond Myklebust28df9552006-06-09 09:40:27 -0400508again:
Trond Myklebustecdbf762005-06-22 17:16:31 +0000509 for(;;) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400510 /* Reboot protection */
511 fl->fl_u.nfs_fl.state = host->h_state;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000512 status = nlmclnt_call(req, NLMPROC_LOCK);
513 if (status < 0)
514 goto out_unblock;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500515 if (!req->a_args.block)
Trond Myklebustecdbf762005-06-22 17:16:31 +0000516 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000517 /* Did a reclaimer thread notify us of a server reboot? */
518 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
519 continue;
520 if (resp->status != NLM_LCK_BLOCKED)
521 break;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500522 /* Wait on an NLM blocking lock */
523 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
524 /* if we were interrupted. Send a CANCEL request to the server
Trond Myklebustecdbf762005-06-22 17:16:31 +0000525 * and exit
526 */
Trond Myklebust3a649b82006-03-20 13:44:44 -0500527 if (status < 0)
528 goto out_unblock;
529 if (resp->status != NLM_LCK_BLOCKED)
530 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (resp->status == NLM_LCK_GRANTED) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400534 down_read(&host->h_rwsem);
535 /* Check whether or not the server has rebooted */
536 if (fl->fl_u.nfs_fl.state != host->h_state) {
537 up_read(&host->h_rwsem);
538 goto again;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 fl->fl_flags |= FL_SLEEP;
Trond Myklebust4c060b52006-03-20 13:44:41 -0500541 /* Ensure the resulting lock will get added to granted list */
Trond Myklebust9b073572006-06-29 16:38:34 -0400542 if (do_vfs_lock(fl) < 0)
543 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust28df9552006-06-09 09:40:27 -0400544 up_read(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 status = nlm_stat_to_errno(resp->status);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000547out_unblock:
Trond Myklebust3a649b82006-03-20 13:44:44 -0500548 nlmclnt_finish_block(block);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000549 /* Cancel the blocked request if it is still pending */
550 if (resp->status == NLM_LCK_BLOCKED)
Trond Myklebust16fb2422006-02-01 12:18:22 -0500551 nlmclnt_cancel(host, req->a_args.block, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552out:
Trond Myklebust92737232006-03-20 13:44:45 -0500553 nlm_release_call(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return status;
555}
556
557/*
558 * RECLAIM: Try to reclaim a lock
559 */
560int
561nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
562{
563 struct nlm_rqst reqst, *req;
564 int status;
565
566 req = &reqst;
567 memset(req, 0, sizeof(*req));
568 locks_init_lock(&req->a_args.lock.fl);
569 locks_init_lock(&req->a_res.lock.fl);
570 req->a_host = host;
571 req->a_flags = 0;
572
573 /* Set up the argument struct */
574 nlmclnt_setlockargs(req, fl);
575 req->a_args.reclaim = 1;
576
577 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
578 && req->a_res.status == NLM_LCK_GRANTED)
579 return 0;
580
581 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
582 "(errno %d, status %d)\n", fl->fl_pid,
583 status, req->a_res.status);
584
585 /*
586 * FIXME: This is a serious failure. We can
587 *
588 * a. Ignore the problem
589 * b. Send the owning process some signal (Linux doesn't have
590 * SIGLOST, though...)
591 * c. Retry the operation
592 *
593 * Until someone comes up with a simple implementation
594 * for b or c, I'll choose option a.
595 */
596
597 return -ENOLCK;
598}
599
600/*
601 * UNLOCK: remove an existing lock
602 */
603static int
604nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
605{
Trond Myklebust28df9552006-06-09 09:40:27 -0400606 struct nlm_host *host = req->a_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct nlm_res *resp = &req->a_res;
Trond Myklebust9b073572006-06-29 16:38:34 -0400608 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500610 /*
Trond Myklebust30f4e202006-03-13 21:20:49 -0800611 * Note: the server is supposed to either grant us the unlock
612 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
613 * case, we want to unlock.
614 */
Trond Myklebust9b073572006-06-29 16:38:34 -0400615 fl->fl_flags |= FL_EXISTS;
Trond Myklebust28df9552006-06-09 09:40:27 -0400616 down_read(&host->h_rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -0400617 if (do_vfs_lock(fl) == -ENOENT) {
618 up_read(&host->h_rwsem);
619 goto out;
620 }
Trond Myklebust28df9552006-06-09 09:40:27 -0400621 up_read(&host->h_rwsem);
Trond Myklebust30f4e202006-03-13 21:20:49 -0800622
Trond Myklebust92737232006-03-20 13:44:45 -0500623 if (req->a_flags & RPC_TASK_ASYNC)
624 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 status = nlmclnt_call(req, NLMPROC_UNLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (resp->status == NLM_LCK_GRANTED)
Trond Myklebust92737232006-03-20 13:44:45 -0500631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
634 printk("lockd: unexpected unlock status: %d\n", resp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* What to do now? I'm out of my depth... */
Trond Myklebust92737232006-03-20 13:44:45 -0500636 status = -ENOLCK;
637out:
638 nlm_release_call(req);
639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100642static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100644 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int status = req->a_res.status;
646
647 if (RPC_ASSASSINATED(task))
648 goto die;
649
650 if (task->tk_status < 0) {
651 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
652 goto retry_rebind;
653 }
654 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
655 rpc_delay(task, NLMCLNT_GRACE_WAIT);
656 goto retry_unlock;
657 }
658 if (status != NLM_LCK_GRANTED)
659 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
660die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662 retry_rebind:
663 nlm_rebind_host(req->a_host);
664 retry_unlock:
665 rpc_restart_call(task);
666}
667
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100668static const struct rpc_call_ops nlmclnt_unlock_ops = {
669 .rpc_call_done = nlmclnt_unlock_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500670 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100671};
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/*
674 * Cancel a blocked lock request.
675 * We always use an async RPC call for this in order not to hang a
676 * process that has been Ctrl-C'ed.
677 */
Trond Myklebust16fb2422006-02-01 12:18:22 -0500678static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct nlm_rqst *req;
681 unsigned long flags;
682 sigset_t oldset;
683 int status;
684
685 /* Block all signals while setting up call */
686 spin_lock_irqsave(&current->sighand->siglock, flags);
687 oldset = current->blocked;
688 sigfillset(&current->blocked);
689 recalc_sigpending();
690 spin_unlock_irqrestore(&current->sighand->siglock, flags);
691
Trond Myklebust92737232006-03-20 13:44:45 -0500692 req = nlm_alloc_call(nlm_get_host(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!req)
694 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 req->a_flags = RPC_TASK_ASYNC;
696
697 nlmclnt_setlockargs(req, fl);
Trond Myklebust16fb2422006-02-01 12:18:22 -0500698 req->a_args.block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Trond Myklebust92737232006-03-20 13:44:45 -0500700 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 spin_lock_irqsave(&current->sighand->siglock, flags);
703 current->blocked = oldset;
704 recalc_sigpending();
705 spin_unlock_irqrestore(&current->sighand->siglock, flags);
706
707 return status;
708}
709
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100710static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100712 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (RPC_ASSASSINATED(task))
715 goto die;
716
717 if (task->tk_status < 0) {
718 dprintk("lockd: CANCEL call error %d, retrying.\n",
719 task->tk_status);
720 goto retry_cancel;
721 }
722
723 dprintk("lockd: cancel status %d (task %d)\n",
724 req->a_res.status, task->tk_pid);
725
726 switch (req->a_res.status) {
727 case NLM_LCK_GRANTED:
728 case NLM_LCK_DENIED_GRACE_PERIOD:
Trond Myklebust35576cb2006-03-20 13:44:41 -0500729 case NLM_LCK_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* Everything's good */
731 break;
732 case NLM_LCK_DENIED_NOLOCKS:
733 dprintk("lockd: CANCEL failed (server has no locks)\n");
734 goto retry_cancel;
735 default:
736 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
737 req->a_res.status);
738 }
739
740die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return;
742
743retry_cancel:
Trond Myklebustaaaa9942006-02-01 12:18:25 -0500744 /* Don't ever retry more than 3 times */
745 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
746 goto die;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 nlm_rebind_host(req->a_host);
748 rpc_restart_call(task);
749 rpc_delay(task, 30 * HZ);
750}
751
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100752static const struct rpc_call_ops nlmclnt_cancel_ops = {
753 .rpc_call_done = nlmclnt_cancel_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500754 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100755};
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
758 * Convert an NLM status code to a generic kernel errno
759 */
760static int
761nlm_stat_to_errno(u32 status)
762{
763 switch(status) {
764 case NLM_LCK_GRANTED:
765 return 0;
766 case NLM_LCK_DENIED:
767 return -EAGAIN;
768 case NLM_LCK_DENIED_NOLOCKS:
769 case NLM_LCK_DENIED_GRACE_PERIOD:
770 return -ENOLCK;
771 case NLM_LCK_BLOCKED:
772 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
773 return -ENOLCK;
774#ifdef CONFIG_LOCKD_V4
775 case NLM_DEADLCK:
776 return -EDEADLK;
777 case NLM_ROFS:
778 return -EROFS;
779 case NLM_STALE_FH:
780 return -ESTALE;
781 case NLM_FBIG:
782 return -EOVERFLOW;
783 case NLM_FAILED:
784 return -ENOLCK;
785#endif
786 }
787 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
788 return -ENOLCK;
789}