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