]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/lockd/clntproc.c
VFS: Fix __posix_lock_file() copy of private lock area
[net-next-2.6.git] / fs / lockd / clntproc.c
CommitLineData
1da177e4
LT
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)
ecdbf769 24#define NLMCLNT_POLL_TIMEOUT (30*HZ)
aaaa9942 25#define NLMCLNT_MAX_RETRIES 3
1da177e4
LT
26
27static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
28static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
29static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
1da177e4
LT
30static int nlm_stat_to_errno(u32 stat);
31static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
16fb2425 32static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
1da177e4 33
963d8fe5
TM
34static const struct rpc_call_ops nlmclnt_unlock_ops;
35static const struct rpc_call_ops nlmclnt_cancel_ops;
36
1da177e4
LT
37/*
38 * Cookie counter for NLM requests
39 */
40static u32 nlm_cookie = 0x1234;
41
42static 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
50static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
51{
52 atomic_inc(&lockowner->count);
53 return lockowner;
54}
55
56static 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
66static 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
76static 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
85static 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
96static 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);
f99d49ad 118 kfree(new);
1da177e4
LT
119 return res;
120}
121
122/*
123 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
124 */
125static 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
140static 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 */
152int
153nlmclnt_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
176void
177nlmclnt_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 */
193int
194nlmclnt_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;
f518e35a 227 clnt->cl_intr = nfssrv->client->cl_intr;
1da177e4
LT
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
279done:
280 dprintk("lockd: clnt proc returns %d\n", status);
281 nlm_release_host(host);
282 return status;
283}
284EXPORT_SYMBOL(nlmclnt_proc);
285
286/*
287 * Allocate an NLM RPC call struct
288 */
289struct nlm_rqst *
290nlmclnt_alloc_call(void)
291{
292 struct nlm_rqst *call;
293
294 while (!signalled()) {
295 call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
296 if (call) {
297 memset(call, 0, sizeof(*call));
298 locks_init_lock(&call->a_args.lock.fl);
299 locks_init_lock(&call->a_res.lock.fl);
300 return call;
301 }
302 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
041e0e3b 303 schedule_timeout_interruptible(5*HZ);
1da177e4
LT
304 }
305 return NULL;
306}
307
308static int nlm_wait_on_grace(wait_queue_head_t *queue)
309{
310 DEFINE_WAIT(wait);
311 int status = -EINTR;
312
313 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
314 if (!signalled ()) {
315 schedule_timeout(NLMCLNT_GRACE_WAIT);
3e1d1d28 316 try_to_freeze();
1da177e4
LT
317 if (!signalled ())
318 status = 0;
319 }
320 finish_wait(queue, &wait);
321 return status;
322}
323
324/*
325 * Generic NLM call
326 */
327static int
328nlmclnt_call(struct nlm_rqst *req, u32 proc)
329{
330 struct nlm_host *host = req->a_host;
331 struct rpc_clnt *clnt;
332 struct nlm_args *argp = &req->a_args;
333 struct nlm_res *resp = &req->a_res;
334 struct rpc_message msg = {
335 .rpc_argp = argp,
336 .rpc_resp = resp,
337 };
338 int status;
339
340 dprintk("lockd: call procedure %d on %s\n",
341 (int)proc, host->h_name);
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
388in_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 */
963d8fe5 404int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
1da177e4
LT
405{
406 struct nlm_host *host = req->a_host;
407 struct rpc_clnt *clnt;
408 struct rpc_message msg = {
409 .rpc_argp = &req->a_args,
410 .rpc_resp = &req->a_res,
411 };
412 int status;
413
414 dprintk("lockd: call procedure %d on %s (async)\n",
415 (int)proc, host->h_name);
416
417 /* If we have no RPC client yet, create one. */
418 if ((clnt = nlm_bind_host(host)) == NULL)
419 return -ENOLCK;
420 msg.rpc_proc = &clnt->cl_procinfo[proc];
421
422 /* bootstrap and kick off the async RPC call */
963d8fe5 423 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
1da177e4
LT
424
425 return status;
426}
427
963d8fe5 428static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
1da177e4
LT
429{
430 struct nlm_host *host = req->a_host;
431 struct rpc_clnt *clnt;
432 struct nlm_args *argp = &req->a_args;
433 struct nlm_res *resp = &req->a_res;
434 struct rpc_message msg = {
435 .rpc_argp = argp,
436 .rpc_resp = resp,
437 };
438 int status;
439
440 dprintk("lockd: call procedure %d on %s (async)\n",
441 (int)proc, host->h_name);
442
443 /* If we have no RPC client yet, create one. */
444 if ((clnt = nlm_bind_host(host)) == NULL)
445 return -ENOLCK;
446 msg.rpc_proc = &clnt->cl_procinfo[proc];
447
448 /* Increment host refcount */
449 nlm_get_host(host);
450 /* bootstrap and kick off the async RPC call */
963d8fe5 451 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
1da177e4
LT
452 if (status < 0)
453 nlm_release_host(host);
454 return status;
455}
456
457/*
458 * TEST for the presence of a conflicting lock
459 */
460static int
461nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
462{
463 int status;
464
465 status = nlmclnt_call(req, NLMPROC_TEST);
466 nlmclnt_release_lockargs(req);
467 if (status < 0)
468 return status;
469
470 status = req->a_res.status;
471 if (status == NLM_LCK_GRANTED) {
472 fl->fl_type = F_UNLCK;
473 } if (status == NLM_LCK_DENIED) {
474 /*
475 * Report the conflicting lock back to the application.
476 */
477 locks_copy_lock(fl, &req->a_res.lock.fl);
478 fl->fl_pid = 0;
479 } else {
480 return nlm_stat_to_errno(req->a_res.status);
481 }
482
483 return 0;
484}
485
486static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
487{
488 memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
489 nlm_get_lockowner(new->fl_u.nfs_fl.owner);
490}
491
492static void nlmclnt_locks_release_private(struct file_lock *fl)
493{
494 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
495 fl->fl_ops = NULL;
496}
497
498static struct file_lock_operations nlmclnt_lock_ops = {
499 .fl_copy_lock = nlmclnt_locks_copy_lock,
500 .fl_release_private = nlmclnt_locks_release_private,
501};
502
503static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
504{
505 BUG_ON(fl->fl_ops != NULL);
506 fl->fl_u.nfs_fl.state = 0;
507 fl->fl_u.nfs_fl.flags = 0;
508 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
509 fl->fl_ops = &nlmclnt_lock_ops;
510}
511
512static void do_vfs_lock(struct file_lock *fl)
513{
514 int res = 0;
515 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
516 case FL_POSIX:
517 res = posix_lock_file_wait(fl->fl_file, fl);
518 break;
519 case FL_FLOCK:
520 res = flock_lock_file_wait(fl->fl_file, fl);
521 break;
522 default:
523 BUG();
524 }
525 if (res < 0)
526 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
527 __FUNCTION__);
528}
529
530/*
531 * LOCK: Try to create a lock
532 *
533 * Programmer Harassment Alert
534 *
535 * When given a blocking lock request in a sync RPC call, the HPUX lockd
536 * will faithfully return LCK_BLOCKED but never cares to notify us when
537 * the lock could be granted. This way, our local process could hang
538 * around forever waiting for the callback.
539 *
540 * Solution A: Implement busy-waiting
541 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
542 *
543 * For now I am implementing solution A, because I hate the idea of
544 * re-implementing lockd for a third time in two months. The async
545 * calls shouldn't be too hard to do, however.
546 *
547 * This is one of the lovely things about standards in the NFS area:
548 * they're so soft and squishy you can't really blame HP for doing this.
549 */
550static int
551nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
552{
553 struct nlm_host *host = req->a_host;
554 struct nlm_res *resp = &req->a_res;
ecdbf769
TM
555 long timeout;
556 int status;
1da177e4
LT
557
558 if (!host->h_monitored && nsm_monitor(host) < 0) {
559 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
560 host->h_name);
561 status = -ENOLCK;
562 goto out;
563 }
564
ecdbf769
TM
565 if (req->a_args.block) {
566 status = nlmclnt_prepare_block(req, host, fl);
1da177e4
LT
567 if (status < 0)
568 goto out;
ecdbf769
TM
569 }
570 for(;;) {
571 status = nlmclnt_call(req, NLMPROC_LOCK);
572 if (status < 0)
573 goto out_unblock;
574 if (resp->status != NLM_LCK_BLOCKED)
575 break;
576 /* Wait on an NLM blocking lock */
577 timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
578 /* Did a reclaimer thread notify us of a server reboot? */
579 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
580 continue;
581 if (resp->status != NLM_LCK_BLOCKED)
582 break;
583 if (timeout >= 0)
584 continue;
585 /* We were interrupted. Send a CANCEL request to the server
586 * and exit
587 */
588 status = (int)timeout;
589 goto out_unblock;
590 }
1da177e4
LT
591
592 if (resp->status == NLM_LCK_GRANTED) {
593 fl->fl_u.nfs_fl.state = host->h_state;
594 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
595 fl->fl_flags |= FL_SLEEP;
596 do_vfs_lock(fl);
597 }
598 status = nlm_stat_to_errno(resp->status);
ecdbf769
TM
599out_unblock:
600 nlmclnt_finish_block(req);
601 /* Cancel the blocked request if it is still pending */
602 if (resp->status == NLM_LCK_BLOCKED)
16fb2425 603 nlmclnt_cancel(host, req->a_args.block, fl);
1da177e4
LT
604out:
605 nlmclnt_release_lockargs(req);
606 return status;
607}
608
609/*
610 * RECLAIM: Try to reclaim a lock
611 */
612int
613nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
614{
615 struct nlm_rqst reqst, *req;
616 int status;
617
618 req = &reqst;
619 memset(req, 0, sizeof(*req));
620 locks_init_lock(&req->a_args.lock.fl);
621 locks_init_lock(&req->a_res.lock.fl);
622 req->a_host = host;
623 req->a_flags = 0;
624
625 /* Set up the argument struct */
626 nlmclnt_setlockargs(req, fl);
627 req->a_args.reclaim = 1;
628
629 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
630 && req->a_res.status == NLM_LCK_GRANTED)
631 return 0;
632
633 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
634 "(errno %d, status %d)\n", fl->fl_pid,
635 status, req->a_res.status);
636
637 /*
638 * FIXME: This is a serious failure. We can
639 *
640 * a. Ignore the problem
641 * b. Send the owning process some signal (Linux doesn't have
642 * SIGLOST, though...)
643 * c. Retry the operation
644 *
645 * Until someone comes up with a simple implementation
646 * for b or c, I'll choose option a.
647 */
648
649 return -ENOLCK;
650}
651
652/*
653 * UNLOCK: remove an existing lock
654 */
655static int
656nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
657{
658 struct nlm_res *resp = &req->a_res;
659 int status;
660
661 /* Clean the GRANTED flag now so the lock doesn't get
662 * reclaimed while we're stuck in the unlock call. */
663 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
664
30f4e20a
TM
665 /*
666 * Note: the server is supposed to either grant us the unlock
667 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
668 * case, we want to unlock.
669 */
670 do_vfs_lock(fl);
671
1da177e4
LT
672 if (req->a_flags & RPC_TASK_ASYNC) {
673 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
963d8fe5 674 &nlmclnt_unlock_ops);
1da177e4
LT
675 /* Hrmf... Do the unlock early since locks_remove_posix()
676 * really expects us to free the lock synchronously */
1da177e4
LT
677 if (status < 0) {
678 nlmclnt_release_lockargs(req);
679 kfree(req);
680 }
681 return status;
682 }
683
684 status = nlmclnt_call(req, NLMPROC_UNLOCK);
685 nlmclnt_release_lockargs(req);
686 if (status < 0)
687 return status;
688
1da177e4
LT
689 if (resp->status == NLM_LCK_GRANTED)
690 return 0;
691
692 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
693 printk("lockd: unexpected unlock status: %d\n", resp->status);
694
695 /* What to do now? I'm out of my depth... */
696
697 return -ENOLCK;
698}
699
963d8fe5 700static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
1da177e4 701{
963d8fe5 702 struct nlm_rqst *req = data;
1da177e4
LT
703 int status = req->a_res.status;
704
705 if (RPC_ASSASSINATED(task))
706 goto die;
707
708 if (task->tk_status < 0) {
709 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
710 goto retry_rebind;
711 }
712 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
713 rpc_delay(task, NLMCLNT_GRACE_WAIT);
714 goto retry_unlock;
715 }
716 if (status != NLM_LCK_GRANTED)
717 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
718die:
719 nlm_release_host(req->a_host);
720 nlmclnt_release_lockargs(req);
721 kfree(req);
722 return;
723 retry_rebind:
724 nlm_rebind_host(req->a_host);
725 retry_unlock:
726 rpc_restart_call(task);
727}
728
963d8fe5
TM
729static const struct rpc_call_ops nlmclnt_unlock_ops = {
730 .rpc_call_done = nlmclnt_unlock_callback,
731};
732
1da177e4
LT
733/*
734 * Cancel a blocked lock request.
735 * We always use an async RPC call for this in order not to hang a
736 * process that has been Ctrl-C'ed.
737 */
16fb2425 738static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
1da177e4
LT
739{
740 struct nlm_rqst *req;
741 unsigned long flags;
742 sigset_t oldset;
743 int status;
744
745 /* Block all signals while setting up call */
746 spin_lock_irqsave(&current->sighand->siglock, flags);
747 oldset = current->blocked;
748 sigfillset(&current->blocked);
749 recalc_sigpending();
750 spin_unlock_irqrestore(&current->sighand->siglock, flags);
751
752 req = nlmclnt_alloc_call();
753 if (!req)
754 return -ENOMEM;
755 req->a_host = host;
756 req->a_flags = RPC_TASK_ASYNC;
757
758 nlmclnt_setlockargs(req, fl);
16fb2425 759 req->a_args.block = block;
1da177e4 760
963d8fe5 761 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
1da177e4
LT
762 if (status < 0) {
763 nlmclnt_release_lockargs(req);
764 kfree(req);
765 }
766
767 spin_lock_irqsave(&current->sighand->siglock, flags);
768 current->blocked = oldset;
769 recalc_sigpending();
770 spin_unlock_irqrestore(&current->sighand->siglock, flags);
771
772 return status;
773}
774
963d8fe5 775static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
1da177e4 776{
963d8fe5 777 struct nlm_rqst *req = data;
1da177e4
LT
778
779 if (RPC_ASSASSINATED(task))
780 goto die;
781
782 if (task->tk_status < 0) {
783 dprintk("lockd: CANCEL call error %d, retrying.\n",
784 task->tk_status);
785 goto retry_cancel;
786 }
787
788 dprintk("lockd: cancel status %d (task %d)\n",
789 req->a_res.status, task->tk_pid);
790
791 switch (req->a_res.status) {
792 case NLM_LCK_GRANTED:
793 case NLM_LCK_DENIED_GRACE_PERIOD:
794 /* Everything's good */
795 break;
796 case NLM_LCK_DENIED_NOLOCKS:
797 dprintk("lockd: CANCEL failed (server has no locks)\n");
798 goto retry_cancel;
799 default:
800 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
801 req->a_res.status);
802 }
803
804die:
805 nlm_release_host(req->a_host);
806 nlmclnt_release_lockargs(req);
807 kfree(req);
808 return;
809
810retry_cancel:
aaaa9942
TM
811 /* Don't ever retry more than 3 times */
812 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
813 goto die;
1da177e4
LT
814 nlm_rebind_host(req->a_host);
815 rpc_restart_call(task);
816 rpc_delay(task, 30 * HZ);
817}
818
963d8fe5
TM
819static const struct rpc_call_ops nlmclnt_cancel_ops = {
820 .rpc_call_done = nlmclnt_cancel_callback,
821};
822
1da177e4
LT
823/*
824 * Convert an NLM status code to a generic kernel errno
825 */
826static int
827nlm_stat_to_errno(u32 status)
828{
829 switch(status) {
830 case NLM_LCK_GRANTED:
831 return 0;
832 case NLM_LCK_DENIED:
833 return -EAGAIN;
834 case NLM_LCK_DENIED_NOLOCKS:
835 case NLM_LCK_DENIED_GRACE_PERIOD:
836 return -ENOLCK;
837 case NLM_LCK_BLOCKED:
838 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
839 return -ENOLCK;
840#ifdef CONFIG_LOCKD_V4
841 case NLM_DEADLCK:
842 return -EDEADLK;
843 case NLM_ROFS:
844 return -EROFS;
845 case NLM_STALE_FH:
846 return -ESTALE;
847 case NLM_FBIG:
848 return -EOVERFLOW;
849 case NLM_FAILED:
850 return -ENOLCK;
851#endif
852 }
853 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
854 return -ENOLCK;
855}