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