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