blob: cb469431bd1d739c71c2b274da7e4e225b53b413 [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
9#include <linux/config.h>
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/errno.h>
13#include <linux/fs.h>
14#include <linux/nfs_fs.h>
15#include <linux/utsname.h>
16#include <linux/smp_lock.h>
17#include <linux/sunrpc/clnt.h>
18#include <linux/sunrpc/svc.h>
19#include <linux/lockd/lockd.h>
20#include <linux/lockd/sm_inter.h>
21
22#define NLMDBG_FACILITY NLMDBG_CLIENT
23#define NLMCLNT_GRACE_WAIT (5*HZ)
Trond Myklebustecdbf762005-06-22 17:16:31 +000024#define NLMCLNT_POLL_TIMEOUT (30*HZ)
Trond Myklebustaaaa9942006-02-01 12:18:25 -050025#define NLMCLNT_MAX_RETRIES 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
28static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
29static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int nlm_stat_to_errno(u32 stat);
31static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
Trond Myklebust16fb2422006-02-01 12:18:22 -050032static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Trond Myklebust963d8fe2006-01-03 09:55:04 +010034static const struct rpc_call_ops nlmclnt_unlock_ops;
35static const struct rpc_call_ops nlmclnt_cancel_ops;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Cookie counter for NLM requests
39 */
40static u32 nlm_cookie = 0x1234;
41
42static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
43{
44 memcpy(c->data, &nlm_cookie, 4);
45 memset(c->data+4, 0, 4);
46 c->len=4;
47 nlm_cookie++;
48}
49
50static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
51{
52 atomic_inc(&lockowner->count);
53 return lockowner;
54}
55
56static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
57{
58 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
59 return;
60 list_del(&lockowner->list);
61 spin_unlock(&lockowner->host->h_lock);
62 nlm_release_host(lockowner->host);
63 kfree(lockowner);
64}
65
66static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
67{
68 struct nlm_lockowner *lockowner;
69 list_for_each_entry(lockowner, &host->h_lockowners, list) {
70 if (lockowner->pid == pid)
71 return -EBUSY;
72 }
73 return 0;
74}
75
76static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
77{
78 uint32_t res;
79 do {
80 res = host->h_pidcount++;
81 } while (nlm_pidbusy(host, res) < 0);
82 return res;
83}
84
85static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
86{
87 struct nlm_lockowner *lockowner;
88 list_for_each_entry(lockowner, &host->h_lockowners, list) {
89 if (lockowner->owner != owner)
90 continue;
91 return nlm_get_lockowner(lockowner);
92 }
93 return NULL;
94}
95
96static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
97{
98 struct nlm_lockowner *res, *new = NULL;
99
100 spin_lock(&host->h_lock);
101 res = __nlm_find_lockowner(host, owner);
102 if (res == NULL) {
103 spin_unlock(&host->h_lock);
104 new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL);
105 spin_lock(&host->h_lock);
106 res = __nlm_find_lockowner(host, owner);
107 if (res == NULL && new != NULL) {
108 res = new;
109 atomic_set(&new->count, 1);
110 new->owner = owner;
111 new->pid = __nlm_alloc_pid(host);
112 new->host = nlm_get_host(host);
113 list_add(&new->list, &host->h_lockowners);
114 new = NULL;
115 }
116 }
117 spin_unlock(&host->h_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800118 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return res;
120}
121
122/*
123 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
124 */
125static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
126{
127 struct nlm_args *argp = &req->a_args;
128 struct nlm_lock *lock = &argp->lock;
129
130 nlmclnt_next_cookie(&argp->cookie);
131 argp->state = nsm_local_state;
132 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
133 lock->caller = system_utsname.nodename;
134 lock->oh.data = req->a_owner;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500135 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
136 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
137 system_utsname.nodename);
138 lock->svid = fl->fl_u.nfs_fl.owner->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 locks_copy_lock(&lock->fl, fl);
140}
141
142static void nlmclnt_release_lockargs(struct nlm_rqst *req)
143{
144 struct file_lock *fl = &req->a_args.lock.fl;
145
146 if (fl->fl_ops && fl->fl_ops->fl_release_private)
147 fl->fl_ops->fl_release_private(fl);
148}
149
150/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * This is the main entry point for the NLM client.
152 */
153int
154nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
155{
156 struct nfs_server *nfssrv = NFS_SERVER(inode);
157 struct nlm_host *host;
158 struct nlm_rqst reqst, *call = &reqst;
159 sigset_t oldset;
160 unsigned long flags;
161 int status, proto, vers;
162
163 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
164 if (NFS_PROTO(inode)->version > 3) {
165 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
166 return -ENOLCK;
167 }
168
169 /* Retrieve transport protocol from NFS client */
170 proto = NFS_CLIENT(inode)->cl_xprt->prot;
171
172 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
173 return -ENOLCK;
174
175 /* Create RPC client handle if not there, and copy soft
176 * and intr flags from NFS client. */
177 if (host->h_rpcclnt == NULL) {
178 struct rpc_clnt *clnt;
179
180 /* Bind an rpc client to this host handle (does not
181 * perform a portmapper lookup) */
182 if (!(clnt = nlm_bind_host(host))) {
183 status = -ENOLCK;
184 goto done;
185 }
186 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
Chuck Leverf518e35a2006-01-03 09:55:52 +0100187 clnt->cl_intr = nfssrv->client->cl_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 /* Keep the old signal mask */
191 spin_lock_irqsave(&current->sighand->siglock, flags);
192 oldset = current->blocked;
193
194 /* If we're cleaning up locks because the process is exiting,
195 * perform the RPC call asynchronously. */
196 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
197 && fl->fl_type == F_UNLCK
198 && (current->flags & PF_EXITING)) {
199 sigfillset(&current->blocked); /* Mask all signals */
200 recalc_sigpending();
201 spin_unlock_irqrestore(&current->sighand->siglock, flags);
202
203 call = nlmclnt_alloc_call();
204 if (!call) {
205 status = -ENOMEM;
206 goto out_restore;
207 }
208 call->a_flags = RPC_TASK_ASYNC;
209 } else {
210 spin_unlock_irqrestore(&current->sighand->siglock, flags);
211 memset(call, 0, sizeof(*call));
212 locks_init_lock(&call->a_args.lock.fl);
213 locks_init_lock(&call->a_res.lock.fl);
214 }
215 call->a_host = host;
216
217 nlmclnt_locks_init_private(fl, host);
218
219 /* Set up the argument struct */
220 nlmclnt_setlockargs(call, fl);
221
222 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
223 if (fl->fl_type != F_UNLCK) {
224 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
225 status = nlmclnt_lock(call, fl);
226 } else
227 status = nlmclnt_unlock(call, fl);
228 } else if (IS_GETLK(cmd))
229 status = nlmclnt_test(call, fl);
230 else
231 status = -EINVAL;
232
233 out_restore:
234 spin_lock_irqsave(&current->sighand->siglock, flags);
235 current->blocked = oldset;
236 recalc_sigpending();
237 spin_unlock_irqrestore(&current->sighand->siglock, flags);
238
239done:
240 dprintk("lockd: clnt proc returns %d\n", status);
241 nlm_release_host(host);
242 return status;
243}
244EXPORT_SYMBOL(nlmclnt_proc);
245
246/*
247 * Allocate an NLM RPC call struct
248 */
249struct nlm_rqst *
250nlmclnt_alloc_call(void)
251{
252 struct nlm_rqst *call;
253
Trond Myklebust36943fa2006-03-20 13:44:05 -0500254 for(;;) {
255 call = kzalloc(sizeof(*call), GFP_KERNEL);
256 if (call != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 locks_init_lock(&call->a_args.lock.fl);
258 locks_init_lock(&call->a_res.lock.fl);
259 return call;
260 }
Trond Myklebust36943fa2006-03-20 13:44:05 -0500261 if (signalled())
262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700264 schedule_timeout_interruptible(5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 return NULL;
267}
268
269static int nlm_wait_on_grace(wait_queue_head_t *queue)
270{
271 DEFINE_WAIT(wait);
272 int status = -EINTR;
273
274 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
275 if (!signalled ()) {
276 schedule_timeout(NLMCLNT_GRACE_WAIT);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700277 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (!signalled ())
279 status = 0;
280 }
281 finish_wait(queue, &wait);
282 return status;
283}
284
285/*
286 * Generic NLM call
287 */
288static int
289nlmclnt_call(struct nlm_rqst *req, u32 proc)
290{
291 struct nlm_host *host = req->a_host;
292 struct rpc_clnt *clnt;
293 struct nlm_args *argp = &req->a_args;
294 struct nlm_res *resp = &req->a_res;
295 struct rpc_message msg = {
296 .rpc_argp = argp,
297 .rpc_resp = resp,
298 };
299 int status;
300
301 dprintk("lockd: call procedure %d on %s\n",
302 (int)proc, host->h_name);
303
304 do {
305 if (host->h_reclaiming && !argp->reclaim)
306 goto in_grace_period;
307
308 /* If we have no RPC client yet, create one. */
309 if ((clnt = nlm_bind_host(host)) == NULL)
310 return -ENOLCK;
311 msg.rpc_proc = &clnt->cl_procinfo[proc];
312
313 /* Perform the RPC call. If an error occurs, try again */
314 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
315 dprintk("lockd: rpc_call returned error %d\n", -status);
316 switch (status) {
317 case -EPROTONOSUPPORT:
318 status = -EINVAL;
319 break;
320 case -ECONNREFUSED:
321 case -ETIMEDOUT:
322 case -ENOTCONN:
323 nlm_rebind_host(host);
324 status = -EAGAIN;
325 break;
326 case -ERESTARTSYS:
327 return signalled () ? -EINTR : status;
328 default:
329 break;
330 }
331 break;
332 } else
333 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
334 dprintk("lockd: server in grace period\n");
335 if (argp->reclaim) {
336 printk(KERN_WARNING
337 "lockd: spurious grace period reject?!\n");
338 return -ENOLCK;
339 }
340 } else {
341 if (!argp->reclaim) {
342 /* We appear to be out of the grace period */
343 wake_up_all(&host->h_gracewait);
344 }
345 dprintk("lockd: server returns status %d\n", resp->status);
346 return 0; /* Okay, call complete */
347 }
348
349in_grace_period:
350 /*
351 * The server has rebooted and appears to be in the grace
352 * period during which locks are only allowed to be
353 * reclaimed.
354 * We can only back off and try again later.
355 */
356 status = nlm_wait_on_grace(&host->h_gracewait);
357 } while (status == 0);
358
359 return status;
360}
361
362/*
363 * Generic NLM call, async version.
364 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100365int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct nlm_host *host = req->a_host;
368 struct rpc_clnt *clnt;
369 struct rpc_message msg = {
370 .rpc_argp = &req->a_args,
371 .rpc_resp = &req->a_res,
372 };
373 int status;
374
375 dprintk("lockd: call procedure %d on %s (async)\n",
376 (int)proc, host->h_name);
377
378 /* If we have no RPC client yet, create one. */
379 if ((clnt = nlm_bind_host(host)) == NULL)
380 return -ENOLCK;
381 msg.rpc_proc = &clnt->cl_procinfo[proc];
382
383 /* bootstrap and kick off the async RPC call */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100384 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 return status;
387}
388
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100389static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct nlm_host *host = req->a_host;
392 struct rpc_clnt *clnt;
393 struct nlm_args *argp = &req->a_args;
394 struct nlm_res *resp = &req->a_res;
395 struct rpc_message msg = {
396 .rpc_argp = argp,
397 .rpc_resp = resp,
398 };
399 int status;
400
401 dprintk("lockd: call procedure %d on %s (async)\n",
402 (int)proc, host->h_name);
403
404 /* If we have no RPC client yet, create one. */
405 if ((clnt = nlm_bind_host(host)) == NULL)
406 return -ENOLCK;
407 msg.rpc_proc = &clnt->cl_procinfo[proc];
408
409 /* Increment host refcount */
410 nlm_get_host(host);
411 /* bootstrap and kick off the async RPC call */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100412 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (status < 0)
414 nlm_release_host(host);
415 return status;
416}
417
418/*
419 * TEST for the presence of a conflicting lock
420 */
421static int
422nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
423{
424 int status;
425
426 status = nlmclnt_call(req, NLMPROC_TEST);
427 nlmclnt_release_lockargs(req);
428 if (status < 0)
429 return status;
430
431 status = req->a_res.status;
432 if (status == NLM_LCK_GRANTED) {
433 fl->fl_type = F_UNLCK;
434 } if (status == NLM_LCK_DENIED) {
435 /*
436 * Report the conflicting lock back to the application.
437 */
438 locks_copy_lock(fl, &req->a_res.lock.fl);
439 fl->fl_pid = 0;
440 } else {
441 return nlm_stat_to_errno(req->a_res.status);
442 }
443
444 return 0;
445}
446
447static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
448{
449 memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
450 nlm_get_lockowner(new->fl_u.nfs_fl.owner);
451}
452
453static void nlmclnt_locks_release_private(struct file_lock *fl)
454{
455 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
456 fl->fl_ops = NULL;
457}
458
459static struct file_lock_operations nlmclnt_lock_ops = {
460 .fl_copy_lock = nlmclnt_locks_copy_lock,
461 .fl_release_private = nlmclnt_locks_release_private,
462};
463
464static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
465{
466 BUG_ON(fl->fl_ops != NULL);
467 fl->fl_u.nfs_fl.state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
469 fl->fl_ops = &nlmclnt_lock_ops;
470}
471
472static void do_vfs_lock(struct file_lock *fl)
473{
474 int res = 0;
475 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
476 case FL_POSIX:
477 res = posix_lock_file_wait(fl->fl_file, fl);
478 break;
479 case FL_FLOCK:
480 res = flock_lock_file_wait(fl->fl_file, fl);
481 break;
482 default:
483 BUG();
484 }
485 if (res < 0)
486 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
487 __FUNCTION__);
488}
489
490/*
491 * LOCK: Try to create a lock
492 *
493 * Programmer Harassment Alert
494 *
495 * When given a blocking lock request in a sync RPC call, the HPUX lockd
496 * will faithfully return LCK_BLOCKED but never cares to notify us when
497 * the lock could be granted. This way, our local process could hang
498 * around forever waiting for the callback.
499 *
500 * Solution A: Implement busy-waiting
501 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
502 *
503 * For now I am implementing solution A, because I hate the idea of
504 * re-implementing lockd for a third time in two months. The async
505 * calls shouldn't be too hard to do, however.
506 *
507 * This is one of the lovely things about standards in the NFS area:
508 * they're so soft and squishy you can't really blame HP for doing this.
509 */
510static int
511nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
512{
513 struct nlm_host *host = req->a_host;
514 struct nlm_res *resp = &req->a_res;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000515 long timeout;
516 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!host->h_monitored && nsm_monitor(host) < 0) {
519 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
520 host->h_name);
521 status = -ENOLCK;
522 goto out;
523 }
524
Trond Myklebustecdbf762005-06-22 17:16:31 +0000525 if (req->a_args.block) {
526 status = nlmclnt_prepare_block(req, host, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (status < 0)
528 goto out;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000529 }
530 for(;;) {
531 status = nlmclnt_call(req, NLMPROC_LOCK);
532 if (status < 0)
533 goto out_unblock;
534 if (resp->status != NLM_LCK_BLOCKED)
535 break;
536 /* Wait on an NLM blocking lock */
537 timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
538 /* Did a reclaimer thread notify us of a server reboot? */
539 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
540 continue;
541 if (resp->status != NLM_LCK_BLOCKED)
542 break;
543 if (timeout >= 0)
544 continue;
545 /* We were interrupted. Send a CANCEL request to the server
546 * and exit
547 */
548 status = (int)timeout;
549 goto out_unblock;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (resp->status == NLM_LCK_GRANTED) {
553 fl->fl_u.nfs_fl.state = host->h_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 fl->fl_flags |= FL_SLEEP;
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500555 list_add_tail(&fl->fl_u.nfs_fl.list, &host->h_granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 do_vfs_lock(fl);
557 }
558 status = nlm_stat_to_errno(resp->status);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000559out_unblock:
560 nlmclnt_finish_block(req);
561 /* Cancel the blocked request if it is still pending */
562 if (resp->status == NLM_LCK_BLOCKED)
Trond Myklebust16fb2422006-02-01 12:18:22 -0500563 nlmclnt_cancel(host, req->a_args.block, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564out:
565 nlmclnt_release_lockargs(req);
566 return status;
567}
568
569/*
570 * RECLAIM: Try to reclaim a lock
571 */
572int
573nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
574{
575 struct nlm_rqst reqst, *req;
576 int status;
577
578 req = &reqst;
579 memset(req, 0, sizeof(*req));
580 locks_init_lock(&req->a_args.lock.fl);
581 locks_init_lock(&req->a_res.lock.fl);
582 req->a_host = host;
583 req->a_flags = 0;
584
585 /* Set up the argument struct */
586 nlmclnt_setlockargs(req, fl);
587 req->a_args.reclaim = 1;
588
589 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
590 && req->a_res.status == NLM_LCK_GRANTED)
591 return 0;
592
593 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
594 "(errno %d, status %d)\n", fl->fl_pid,
595 status, req->a_res.status);
596
597 /*
598 * FIXME: This is a serious failure. We can
599 *
600 * a. Ignore the problem
601 * b. Send the owning process some signal (Linux doesn't have
602 * SIGLOST, though...)
603 * c. Retry the operation
604 *
605 * Until someone comes up with a simple implementation
606 * for b or c, I'll choose option a.
607 */
608
609 return -ENOLCK;
610}
611
612/*
613 * UNLOCK: remove an existing lock
614 */
615static int
616nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
617{
618 struct nlm_res *resp = &req->a_res;
619 int status;
620
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500621 /*
622 * Remove from the granted list now so the lock doesn't get
623 * reclaimed while we're stuck in the unlock call.
624 */
625 list_del(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Trond Myklebust30f4e202006-03-13 21:20:49 -0800627 /*
628 * Note: the server is supposed to either grant us the unlock
629 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
630 * case, we want to unlock.
631 */
632 do_vfs_lock(fl);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (req->a_flags & RPC_TASK_ASYNC) {
635 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100636 &nlmclnt_unlock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Hrmf... Do the unlock early since locks_remove_posix()
638 * really expects us to free the lock synchronously */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (status < 0) {
640 nlmclnt_release_lockargs(req);
641 kfree(req);
642 }
643 return status;
644 }
645
646 status = nlmclnt_call(req, NLMPROC_UNLOCK);
647 nlmclnt_release_lockargs(req);
648 if (status < 0)
649 return status;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (resp->status == NLM_LCK_GRANTED)
652 return 0;
653
654 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
655 printk("lockd: unexpected unlock status: %d\n", resp->status);
656
657 /* What to do now? I'm out of my depth... */
658
659 return -ENOLCK;
660}
661
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100662static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100664 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int status = req->a_res.status;
666
667 if (RPC_ASSASSINATED(task))
668 goto die;
669
670 if (task->tk_status < 0) {
671 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
672 goto retry_rebind;
673 }
674 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
675 rpc_delay(task, NLMCLNT_GRACE_WAIT);
676 goto retry_unlock;
677 }
678 if (status != NLM_LCK_GRANTED)
679 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
680die:
681 nlm_release_host(req->a_host);
682 nlmclnt_release_lockargs(req);
683 kfree(req);
684 return;
685 retry_rebind:
686 nlm_rebind_host(req->a_host);
687 retry_unlock:
688 rpc_restart_call(task);
689}
690
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100691static const struct rpc_call_ops nlmclnt_unlock_ops = {
692 .rpc_call_done = nlmclnt_unlock_callback,
693};
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Cancel a blocked lock request.
697 * We always use an async RPC call for this in order not to hang a
698 * process that has been Ctrl-C'ed.
699 */
Trond Myklebust16fb2422006-02-01 12:18:22 -0500700static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct nlm_rqst *req;
703 unsigned long flags;
704 sigset_t oldset;
705 int status;
706
707 /* Block all signals while setting up call */
708 spin_lock_irqsave(&current->sighand->siglock, flags);
709 oldset = current->blocked;
710 sigfillset(&current->blocked);
711 recalc_sigpending();
712 spin_unlock_irqrestore(&current->sighand->siglock, flags);
713
714 req = nlmclnt_alloc_call();
715 if (!req)
716 return -ENOMEM;
717 req->a_host = host;
718 req->a_flags = RPC_TASK_ASYNC;
719
720 nlmclnt_setlockargs(req, fl);
Trond Myklebust16fb2422006-02-01 12:18:22 -0500721 req->a_args.block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100723 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (status < 0) {
725 nlmclnt_release_lockargs(req);
726 kfree(req);
727 }
728
729 spin_lock_irqsave(&current->sighand->siglock, flags);
730 current->blocked = oldset;
731 recalc_sigpending();
732 spin_unlock_irqrestore(&current->sighand->siglock, flags);
733
734 return status;
735}
736
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100737static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100739 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (RPC_ASSASSINATED(task))
742 goto die;
743
744 if (task->tk_status < 0) {
745 dprintk("lockd: CANCEL call error %d, retrying.\n",
746 task->tk_status);
747 goto retry_cancel;
748 }
749
750 dprintk("lockd: cancel status %d (task %d)\n",
751 req->a_res.status, task->tk_pid);
752
753 switch (req->a_res.status) {
754 case NLM_LCK_GRANTED:
755 case NLM_LCK_DENIED_GRACE_PERIOD:
756 /* Everything's good */
757 break;
758 case NLM_LCK_DENIED_NOLOCKS:
759 dprintk("lockd: CANCEL failed (server has no locks)\n");
760 goto retry_cancel;
761 default:
762 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
763 req->a_res.status);
764 }
765
766die:
767 nlm_release_host(req->a_host);
768 nlmclnt_release_lockargs(req);
769 kfree(req);
770 return;
771
772retry_cancel:
Trond Myklebustaaaa9942006-02-01 12:18:25 -0500773 /* Don't ever retry more than 3 times */
774 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
775 goto die;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 nlm_rebind_host(req->a_host);
777 rpc_restart_call(task);
778 rpc_delay(task, 30 * HZ);
779}
780
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100781static const struct rpc_call_ops nlmclnt_cancel_ops = {
782 .rpc_call_done = nlmclnt_cancel_callback,
783};
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/*
786 * Convert an NLM status code to a generic kernel errno
787 */
788static int
789nlm_stat_to_errno(u32 status)
790{
791 switch(status) {
792 case NLM_LCK_GRANTED:
793 return 0;
794 case NLM_LCK_DENIED:
795 return -EAGAIN;
796 case NLM_LCK_DENIED_NOLOCKS:
797 case NLM_LCK_DENIED_GRACE_PERIOD:
798 return -ENOLCK;
799 case NLM_LCK_BLOCKED:
800 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
801 return -ENOLCK;
802#ifdef CONFIG_LOCKD_V4
803 case NLM_DEADLCK:
804 return -EDEADLK;
805 case NLM_ROFS:
806 return -EROFS;
807 case NLM_STALE_FH:
808 return -ESTALE;
809 case NLM_FBIG:
810 return -EOVERFLOW;
811 case NLM_FAILED:
812 return -ENOLCK;
813#endif
814 }
815 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
816 return -ENOLCK;
817}