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