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