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