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