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