blob: 8af0171058543bb5ada7038e63ef9e1ce558548e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
Trond Myklebustecdbf762005-06-22 17:16:31 +000024#define NLMCLNT_POLL_TIMEOUT (30*HZ)
Trond Myklebustaaaa9942006-02-01 12:18:25 -050025#define NLMCLNT_MAX_RETRIES 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int nlm_stat_to_errno(u32 stat);
31static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
Trond Myklebust16fb2422006-02-01 12:18:22 -050032static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Trond Myklebust963d8fe2006-01-03 09:55:04 +010034static const struct rpc_call_ops nlmclnt_unlock_ops;
35static const struct rpc_call_ops nlmclnt_cancel_ops;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
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);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800118 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500135 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;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500139 lock->fl.fl_start = fl->fl_start;
140 lock->fl.fl_end = fl->fl_end;
141 lock->fl.fl_type = fl->fl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static void nlmclnt_release_lockargs(struct nlm_rqst *req)
145{
Trond Myklebust3a649b82006-03-20 13:44:44 -0500146 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * This is the main entry point for the NLM client.
151 */
152int
153nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
154{
155 struct nfs_server *nfssrv = NFS_SERVER(inode);
156 struct nlm_host *host;
157 struct nlm_rqst reqst, *call = &reqst;
158 sigset_t oldset;
159 unsigned long flags;
160 int status, proto, vers;
161
162 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
163 if (NFS_PROTO(inode)->version > 3) {
164 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
165 return -ENOLCK;
166 }
167
168 /* Retrieve transport protocol from NFS client */
169 proto = NFS_CLIENT(inode)->cl_xprt->prot;
170
171 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
172 return -ENOLCK;
173
174 /* Create RPC client handle if not there, and copy soft
175 * and intr flags from NFS client. */
176 if (host->h_rpcclnt == NULL) {
177 struct rpc_clnt *clnt;
178
179 /* Bind an rpc client to this host handle (does not
180 * perform a portmapper lookup) */
181 if (!(clnt = nlm_bind_host(host))) {
182 status = -ENOLCK;
183 goto done;
184 }
185 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
Chuck Leverf518e352006-01-03 09:55:52 +0100186 clnt->cl_intr = nfssrv->client->cl_intr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 /* Keep the old signal mask */
190 spin_lock_irqsave(&current->sighand->siglock, flags);
191 oldset = current->blocked;
192
193 /* If we're cleaning up locks because the process is exiting,
194 * perform the RPC call asynchronously. */
195 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
196 && fl->fl_type == F_UNLCK
197 && (current->flags & PF_EXITING)) {
198 sigfillset(&current->blocked); /* Mask all signals */
199 recalc_sigpending();
200 spin_unlock_irqrestore(&current->sighand->siglock, flags);
201
202 call = nlmclnt_alloc_call();
203 if (!call) {
204 status = -ENOMEM;
205 goto out_restore;
206 }
207 call->a_flags = RPC_TASK_ASYNC;
208 } else {
209 spin_unlock_irqrestore(&current->sighand->siglock, flags);
210 memset(call, 0, sizeof(*call));
211 locks_init_lock(&call->a_args.lock.fl);
212 locks_init_lock(&call->a_res.lock.fl);
213 }
214 call->a_host = host;
215
216 nlmclnt_locks_init_private(fl, host);
217
218 /* Set up the argument struct */
219 nlmclnt_setlockargs(call, fl);
220
221 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
222 if (fl->fl_type != F_UNLCK) {
223 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
224 status = nlmclnt_lock(call, fl);
225 } else
226 status = nlmclnt_unlock(call, fl);
227 } else if (IS_GETLK(cmd))
228 status = nlmclnt_test(call, fl);
229 else
230 status = -EINVAL;
231
232 out_restore:
233 spin_lock_irqsave(&current->sighand->siglock, flags);
234 current->blocked = oldset;
235 recalc_sigpending();
236 spin_unlock_irqrestore(&current->sighand->siglock, flags);
237
238done:
239 dprintk("lockd: clnt proc returns %d\n", status);
240 nlm_release_host(host);
241 return status;
242}
243EXPORT_SYMBOL(nlmclnt_proc);
244
245/*
246 * Allocate an NLM RPC call struct
247 */
248struct nlm_rqst *
249nlmclnt_alloc_call(void)
250{
251 struct nlm_rqst *call;
252
Trond Myklebust36943fa2006-03-20 13:44:05 -0500253 for(;;) {
254 call = kzalloc(sizeof(*call), GFP_KERNEL);
255 if (call != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 locks_init_lock(&call->a_args.lock.fl);
257 locks_init_lock(&call->a_res.lock.fl);
258 return call;
259 }
Trond Myklebust36943fa2006-03-20 13:44:05 -0500260 if (signalled())
261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700263 schedule_timeout_interruptible(5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 return NULL;
266}
267
268static int nlm_wait_on_grace(wait_queue_head_t *queue)
269{
270 DEFINE_WAIT(wait);
271 int status = -EINTR;
272
273 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
274 if (!signalled ()) {
275 schedule_timeout(NLMCLNT_GRACE_WAIT);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700276 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!signalled ())
278 status = 0;
279 }
280 finish_wait(queue, &wait);
281 return status;
282}
283
284/*
285 * Generic NLM call
286 */
287static int
288nlmclnt_call(struct nlm_rqst *req, u32 proc)
289{
290 struct nlm_host *host = req->a_host;
291 struct rpc_clnt *clnt;
292 struct nlm_args *argp = &req->a_args;
293 struct nlm_res *resp = &req->a_res;
294 struct rpc_message msg = {
295 .rpc_argp = argp,
296 .rpc_resp = resp,
297 };
298 int status;
299
300 dprintk("lockd: call procedure %d on %s\n",
301 (int)proc, host->h_name);
302
303 do {
304 if (host->h_reclaiming && !argp->reclaim)
305 goto in_grace_period;
306
307 /* If we have no RPC client yet, create one. */
308 if ((clnt = nlm_bind_host(host)) == NULL)
309 return -ENOLCK;
310 msg.rpc_proc = &clnt->cl_procinfo[proc];
311
312 /* Perform the RPC call. If an error occurs, try again */
313 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
314 dprintk("lockd: rpc_call returned error %d\n", -status);
315 switch (status) {
316 case -EPROTONOSUPPORT:
317 status = -EINVAL;
318 break;
319 case -ECONNREFUSED:
320 case -ETIMEDOUT:
321 case -ENOTCONN:
322 nlm_rebind_host(host);
323 status = -EAGAIN;
324 break;
325 case -ERESTARTSYS:
326 return signalled () ? -EINTR : status;
327 default:
328 break;
329 }
330 break;
331 } else
332 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
333 dprintk("lockd: server in grace period\n");
334 if (argp->reclaim) {
335 printk(KERN_WARNING
336 "lockd: spurious grace period reject?!\n");
337 return -ENOLCK;
338 }
339 } else {
340 if (!argp->reclaim) {
341 /* We appear to be out of the grace period */
342 wake_up_all(&host->h_gracewait);
343 }
344 dprintk("lockd: server returns status %d\n", resp->status);
345 return 0; /* Okay, call complete */
346 }
347
348in_grace_period:
349 /*
350 * The server has rebooted and appears to be in the grace
351 * period during which locks are only allowed to be
352 * reclaimed.
353 * We can only back off and try again later.
354 */
355 status = nlm_wait_on_grace(&host->h_gracewait);
356 } while (status == 0);
357
358 return status;
359}
360
361/*
362 * Generic NLM call, async version.
363 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100364int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct nlm_host *host = req->a_host;
367 struct rpc_clnt *clnt;
368 struct rpc_message msg = {
369 .rpc_argp = &req->a_args,
370 .rpc_resp = &req->a_res,
371 };
372 int status;
373
374 dprintk("lockd: call procedure %d on %s (async)\n",
375 (int)proc, host->h_name);
376
377 /* If we have no RPC client yet, create one. */
378 if ((clnt = nlm_bind_host(host)) == NULL)
379 return -ENOLCK;
380 msg.rpc_proc = &clnt->cl_procinfo[proc];
381
382 /* bootstrap and kick off the async RPC call */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100383 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return status;
386}
387
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100388static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct nlm_host *host = req->a_host;
391 struct rpc_clnt *clnt;
392 struct nlm_args *argp = &req->a_args;
393 struct nlm_res *resp = &req->a_res;
394 struct rpc_message msg = {
395 .rpc_argp = argp,
396 .rpc_resp = resp,
397 };
398 int status;
399
400 dprintk("lockd: call procedure %d on %s (async)\n",
401 (int)proc, host->h_name);
402
403 /* If we have no RPC client yet, create one. */
404 if ((clnt = nlm_bind_host(host)) == NULL)
405 return -ENOLCK;
406 msg.rpc_proc = &clnt->cl_procinfo[proc];
407
408 /* Increment host refcount */
409 nlm_get_host(host);
410 /* bootstrap and kick off the async RPC call */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100411 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (status < 0)
413 nlm_release_host(host);
414 return status;
415}
416
417/*
418 * TEST for the presence of a conflicting lock
419 */
420static int
421nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
422{
423 int status;
424
425 status = nlmclnt_call(req, NLMPROC_TEST);
426 nlmclnt_release_lockargs(req);
427 if (status < 0)
428 return status;
429
430 status = req->a_res.status;
431 if (status == NLM_LCK_GRANTED) {
432 fl->fl_type = F_UNLCK;
433 } if (status == NLM_LCK_DENIED) {
434 /*
435 * Report the conflicting lock back to the application.
436 */
437 locks_copy_lock(fl, &req->a_res.lock.fl);
438 fl->fl_pid = 0;
439 } else {
440 return nlm_stat_to_errno(req->a_res.status);
441 }
442
443 return 0;
444}
445
446static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
447{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500448 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
449 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
450 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static void nlmclnt_locks_release_private(struct file_lock *fl)
454{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500455 list_del(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459static struct file_lock_operations nlmclnt_lock_ops = {
460 .fl_copy_lock = nlmclnt_locks_copy_lock,
461 .fl_release_private = nlmclnt_locks_release_private,
462};
463
464static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
465{
466 BUG_ON(fl->fl_ops != NULL);
467 fl->fl_u.nfs_fl.state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500469 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 fl->fl_ops = &nlmclnt_lock_ops;
471}
472
473static void do_vfs_lock(struct file_lock *fl)
474{
475 int res = 0;
476 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
477 case FL_POSIX:
478 res = posix_lock_file_wait(fl->fl_file, fl);
479 break;
480 case FL_FLOCK:
481 res = flock_lock_file_wait(fl->fl_file, fl);
482 break;
483 default:
484 BUG();
485 }
486 if (res < 0)
487 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
488 __FUNCTION__);
489}
490
491/*
492 * LOCK: Try to create a lock
493 *
494 * Programmer Harassment Alert
495 *
496 * When given a blocking lock request in a sync RPC call, the HPUX lockd
497 * will faithfully return LCK_BLOCKED but never cares to notify us when
498 * the lock could be granted. This way, our local process could hang
499 * around forever waiting for the callback.
500 *
501 * Solution A: Implement busy-waiting
502 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
503 *
504 * For now I am implementing solution A, because I hate the idea of
505 * re-implementing lockd for a third time in two months. The async
506 * calls shouldn't be too hard to do, however.
507 *
508 * This is one of the lovely things about standards in the NFS area:
509 * they're so soft and squishy you can't really blame HP for doing this.
510 */
511static int
512nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
513{
514 struct nlm_host *host = req->a_host;
515 struct nlm_res *resp = &req->a_res;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500516 struct nlm_wait *block = NULL;
517 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!host->h_monitored && nsm_monitor(host) < 0) {
520 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
521 host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto out;
523 }
524
Trond Myklebust3a649b82006-03-20 13:44:44 -0500525 block = nlmclnt_prepare_block(host, fl);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000526 for(;;) {
527 status = nlmclnt_call(req, NLMPROC_LOCK);
528 if (status < 0)
529 goto out_unblock;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500530 if (!req->a_args.block)
Trond Myklebustecdbf762005-06-22 17:16:31 +0000531 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000532 /* Did a reclaimer thread notify us of a server reboot? */
533 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
534 continue;
535 if (resp->status != NLM_LCK_BLOCKED)
536 break;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500537 /* Wait on an NLM blocking lock */
538 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
539 /* if we were interrupted. Send a CANCEL request to the server
Trond Myklebustecdbf762005-06-22 17:16:31 +0000540 * and exit
541 */
Trond Myklebust3a649b82006-03-20 13:44:44 -0500542 if (status < 0)
543 goto out_unblock;
544 if (resp->status != NLM_LCK_BLOCKED)
545 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (resp->status == NLM_LCK_GRANTED) {
549 fl->fl_u.nfs_fl.state = host->h_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 fl->fl_flags |= FL_SLEEP;
Trond Myklebust4c060b52006-03-20 13:44:41 -0500551 /* Ensure the resulting lock will get added to granted list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 do_vfs_lock(fl);
553 }
554 status = nlm_stat_to_errno(resp->status);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000555out_unblock:
Trond Myklebust3a649b82006-03-20 13:44:44 -0500556 nlmclnt_finish_block(block);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000557 /* Cancel the blocked request if it is still pending */
558 if (resp->status == NLM_LCK_BLOCKED)
Trond Myklebust16fb2422006-02-01 12:18:22 -0500559 nlmclnt_cancel(host, req->a_args.block, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560out:
561 nlmclnt_release_lockargs(req);
562 return status;
563}
564
565/*
566 * RECLAIM: Try to reclaim a lock
567 */
568int
569nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
570{
571 struct nlm_rqst reqst, *req;
572 int status;
573
574 req = &reqst;
575 memset(req, 0, sizeof(*req));
576 locks_init_lock(&req->a_args.lock.fl);
577 locks_init_lock(&req->a_res.lock.fl);
578 req->a_host = host;
579 req->a_flags = 0;
580
581 /* Set up the argument struct */
582 nlmclnt_setlockargs(req, fl);
583 req->a_args.reclaim = 1;
584
585 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
586 && req->a_res.status == NLM_LCK_GRANTED)
587 return 0;
588
589 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
590 "(errno %d, status %d)\n", fl->fl_pid,
591 status, req->a_res.status);
592
593 /*
594 * FIXME: This is a serious failure. We can
595 *
596 * a. Ignore the problem
597 * b. Send the owning process some signal (Linux doesn't have
598 * SIGLOST, though...)
599 * c. Retry the operation
600 *
601 * Until someone comes up with a simple implementation
602 * for b or c, I'll choose option a.
603 */
604
605 return -ENOLCK;
606}
607
608/*
609 * UNLOCK: remove an existing lock
610 */
611static int
612nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
613{
614 struct nlm_res *resp = &req->a_res;
615 int status;
616
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500617 /*
Trond Myklebust30f4e202006-03-13 21:20:49 -0800618 * Note: the server is supposed to either grant us the unlock
619 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
620 * case, we want to unlock.
621 */
622 do_vfs_lock(fl);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (req->a_flags & RPC_TASK_ASYNC) {
625 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100626 &nlmclnt_unlock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Hrmf... Do the unlock early since locks_remove_posix()
628 * really expects us to free the lock synchronously */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (status < 0) {
630 nlmclnt_release_lockargs(req);
631 kfree(req);
632 }
633 return status;
634 }
635
636 status = nlmclnt_call(req, NLMPROC_UNLOCK);
637 nlmclnt_release_lockargs(req);
638 if (status < 0)
639 return status;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (resp->status == NLM_LCK_GRANTED)
642 return 0;
643
644 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
645 printk("lockd: unexpected unlock status: %d\n", resp->status);
646
647 /* What to do now? I'm out of my depth... */
648
649 return -ENOLCK;
650}
651
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100652static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100654 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 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:
671 nlm_release_host(req->a_host);
672 nlmclnt_release_lockargs(req);
673 kfree(req);
674 return;
675 retry_rebind:
676 nlm_rebind_host(req->a_host);
677 retry_unlock:
678 rpc_restart_call(task);
679}
680
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100681static const struct rpc_call_ops nlmclnt_unlock_ops = {
682 .rpc_call_done = nlmclnt_unlock_callback,
683};
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
686 * Cancel a blocked lock request.
687 * We always use an async RPC call for this in order not to hang a
688 * process that has been Ctrl-C'ed.
689 */
Trond Myklebust16fb2422006-02-01 12:18:22 -0500690static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 struct nlm_rqst *req;
693 unsigned long flags;
694 sigset_t oldset;
695 int status;
696
697 /* Block all signals while setting up call */
698 spin_lock_irqsave(&current->sighand->siglock, flags);
699 oldset = current->blocked;
700 sigfillset(&current->blocked);
701 recalc_sigpending();
702 spin_unlock_irqrestore(&current->sighand->siglock, flags);
703
704 req = nlmclnt_alloc_call();
705 if (!req)
706 return -ENOMEM;
707 req->a_host = host;
708 req->a_flags = RPC_TASK_ASYNC;
709
710 nlmclnt_setlockargs(req, fl);
Trond Myklebust16fb2422006-02-01 12:18:22 -0500711 req->a_args.block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100713 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (status < 0) {
715 nlmclnt_release_lockargs(req);
716 kfree(req);
717 }
718
719 spin_lock_irqsave(&current->sighand->siglock, flags);
720 current->blocked = oldset;
721 recalc_sigpending();
722 spin_unlock_irqrestore(&current->sighand->siglock, flags);
723
724 return status;
725}
726
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100727static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100729 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (RPC_ASSASSINATED(task))
732 goto die;
733
734 if (task->tk_status < 0) {
735 dprintk("lockd: CANCEL call error %d, retrying.\n",
736 task->tk_status);
737 goto retry_cancel;
738 }
739
740 dprintk("lockd: cancel status %d (task %d)\n",
741 req->a_res.status, task->tk_pid);
742
743 switch (req->a_res.status) {
744 case NLM_LCK_GRANTED:
745 case NLM_LCK_DENIED_GRACE_PERIOD:
Trond Myklebust35576cb2006-03-20 13:44:41 -0500746 case NLM_LCK_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Everything's good */
748 break;
749 case NLM_LCK_DENIED_NOLOCKS:
750 dprintk("lockd: CANCEL failed (server has no locks)\n");
751 goto retry_cancel;
752 default:
753 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
754 req->a_res.status);
755 }
756
757die:
758 nlm_release_host(req->a_host);
759 nlmclnt_release_lockargs(req);
760 kfree(req);
761 return;
762
763retry_cancel:
Trond Myklebustaaaa9942006-02-01 12:18:25 -0500764 /* Don't ever retry more than 3 times */
765 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
766 goto die;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 nlm_rebind_host(req->a_host);
768 rpc_restart_call(task);
769 rpc_delay(task, 30 * HZ);
770}
771
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100772static const struct rpc_call_ops nlmclnt_cancel_ops = {
773 .rpc_call_done = nlmclnt_cancel_callback,
774};
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
777 * Convert an NLM status code to a generic kernel errno
778 */
779static int
780nlm_stat_to_errno(u32 status)
781{
782 switch(status) {
783 case NLM_LCK_GRANTED:
784 return 0;
785 case NLM_LCK_DENIED:
786 return -EAGAIN;
787 case NLM_LCK_DENIED_NOLOCKS:
788 case NLM_LCK_DENIED_GRACE_PERIOD:
789 return -ENOLCK;
790 case NLM_LCK_BLOCKED:
791 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
792 return -ENOLCK;
793#ifdef CONFIG_LOCKD_V4
794 case NLM_DEADLCK:
795 return -EDEADLK;
796 case NLM_ROFS:
797 return -EROFS;
798 case NLM_STALE_FH:
799 return -ESTALE;
800 case NLM_FBIG:
801 return -EOVERFLOW;
802 case NLM_FAILED:
803 return -ENOLCK;
804#endif
805 }
806 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
807 return -ENOLCK;
808}