blob: 4db62098d3f42da5093747b70077c97c556290c8 [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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nlm_host *host;
Trond Myklebust92737232006-03-20 13:44:45 -0500156 struct nlm_rqst *call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 sigset_t oldset;
158 unsigned long flags;
159 int status, proto, vers;
160
161 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
162 if (NFS_PROTO(inode)->version > 3) {
163 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
164 return -ENOLCK;
165 }
166
167 /* Retrieve transport protocol from NFS client */
168 proto = NFS_CLIENT(inode)->cl_xprt->prot;
169
Trond Myklebust92737232006-03-20 13:44:45 -0500170 host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
171 if (host == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -ENOLCK;
173
Trond Myklebust92737232006-03-20 13:44:45 -0500174 call = nlm_alloc_call(host);
175 if (call == NULL)
176 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Trond Myklebust92737232006-03-20 13:44:45 -0500178 nlmclnt_locks_init_private(fl, host);
179 /* Set up the argument struct */
180 nlmclnt_setlockargs(call, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* Keep the old signal mask */
183 spin_lock_irqsave(&current->sighand->siglock, flags);
184 oldset = current->blocked;
185
186 /* If we're cleaning up locks because the process is exiting,
187 * perform the RPC call asynchronously. */
188 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
189 && fl->fl_type == F_UNLCK
190 && (current->flags & PF_EXITING)) {
191 sigfillset(&current->blocked); /* Mask all signals */
192 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 call->a_flags = RPC_TASK_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Trond Myklebust92737232006-03-20 13:44:45 -0500196 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
199 if (fl->fl_type != F_UNLCK) {
200 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
201 status = nlmclnt_lock(call, fl);
202 } else
203 status = nlmclnt_unlock(call, fl);
204 } else if (IS_GETLK(cmd))
205 status = nlmclnt_test(call, fl);
206 else
207 status = -EINVAL;
208
Trond Myklebust92737232006-03-20 13:44:45 -0500209 fl->fl_ops->fl_release_private(fl);
210 fl->fl_ops = NULL;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_lock_irqsave(&current->sighand->siglock, flags);
213 current->blocked = oldset;
214 recalc_sigpending();
215 spin_unlock_irqrestore(&current->sighand->siglock, flags);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 dprintk("lockd: clnt proc returns %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return status;
219}
220EXPORT_SYMBOL(nlmclnt_proc);
221
222/*
223 * Allocate an NLM RPC call struct
Trond Myklebust92737232006-03-20 13:44:45 -0500224 *
225 * Note: the caller must hold a reference to host. In case of failure,
226 * this reference will be released.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 */
Trond Myklebust92737232006-03-20 13:44:45 -0500228struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct nlm_rqst *call;
231
Trond Myklebust36943fa2006-03-20 13:44:05 -0500232 for(;;) {
233 call = kzalloc(sizeof(*call), GFP_KERNEL);
234 if (call != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 locks_init_lock(&call->a_args.lock.fl);
236 locks_init_lock(&call->a_res.lock.fl);
Trond Myklebust92737232006-03-20 13:44:45 -0500237 call->a_host = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return call;
239 }
Trond Myklebust36943fa2006-03-20 13:44:05 -0500240 if (signalled())
241 break;
Trond Myklebust92737232006-03-20 13:44:45 -0500242 printk("nlm_alloc_call: failed, waiting for memory\n");
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700243 schedule_timeout_interruptible(5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Trond Myklebust92737232006-03-20 13:44:45 -0500245 nlm_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return NULL;
247}
248
Trond Myklebust92737232006-03-20 13:44:45 -0500249void nlm_release_call(struct nlm_rqst *call)
250{
251 nlm_release_host(call->a_host);
252 nlmclnt_release_lockargs(call);
253 kfree(call);
254}
255
256static void nlmclnt_rpc_release(void *data)
257{
258 return nlm_release_call(data);
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static int nlm_wait_on_grace(wait_queue_head_t *queue)
262{
263 DEFINE_WAIT(wait);
264 int status = -EINTR;
265
266 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
267 if (!signalled ()) {
268 schedule_timeout(NLMCLNT_GRACE_WAIT);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700269 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!signalled ())
271 status = 0;
272 }
273 finish_wait(queue, &wait);
274 return status;
275}
276
277/*
278 * Generic NLM call
279 */
280static int
281nlmclnt_call(struct nlm_rqst *req, u32 proc)
282{
283 struct nlm_host *host = req->a_host;
284 struct rpc_clnt *clnt;
285 struct nlm_args *argp = &req->a_args;
286 struct nlm_res *resp = &req->a_res;
287 struct rpc_message msg = {
288 .rpc_argp = argp,
289 .rpc_resp = resp,
290 };
291 int status;
292
293 dprintk("lockd: call procedure %d on %s\n",
294 (int)proc, host->h_name);
295
296 do {
297 if (host->h_reclaiming && !argp->reclaim)
298 goto in_grace_period;
299
300 /* If we have no RPC client yet, create one. */
301 if ((clnt = nlm_bind_host(host)) == NULL)
302 return -ENOLCK;
303 msg.rpc_proc = &clnt->cl_procinfo[proc];
304
305 /* Perform the RPC call. If an error occurs, try again */
306 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
307 dprintk("lockd: rpc_call returned error %d\n", -status);
308 switch (status) {
309 case -EPROTONOSUPPORT:
310 status = -EINVAL;
311 break;
312 case -ECONNREFUSED:
313 case -ETIMEDOUT:
314 case -ENOTCONN:
315 nlm_rebind_host(host);
316 status = -EAGAIN;
317 break;
318 case -ERESTARTSYS:
319 return signalled () ? -EINTR : status;
320 default:
321 break;
322 }
323 break;
324 } else
325 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
326 dprintk("lockd: server in grace period\n");
327 if (argp->reclaim) {
328 printk(KERN_WARNING
329 "lockd: spurious grace period reject?!\n");
330 return -ENOLCK;
331 }
332 } else {
333 if (!argp->reclaim) {
334 /* We appear to be out of the grace period */
335 wake_up_all(&host->h_gracewait);
336 }
337 dprintk("lockd: server returns status %d\n", resp->status);
338 return 0; /* Okay, call complete */
339 }
340
341in_grace_period:
342 /*
343 * The server has rebooted and appears to be in the grace
344 * period during which locks are only allowed to be
345 * reclaimed.
346 * We can only back off and try again later.
347 */
348 status = nlm_wait_on_grace(&host->h_gracewait);
349 } while (status == 0);
350
351 return status;
352}
353
354/*
355 * Generic NLM call, async version.
356 */
Trond Myklebustd4716622006-03-20 13:44:45 -0500357static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct nlm_host *host = req->a_host;
360 struct rpc_clnt *clnt;
Trond Myklebust92737232006-03-20 13:44:45 -0500361 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 dprintk("lockd: call procedure %d on %s (async)\n",
364 (int)proc, host->h_name);
365
366 /* If we have no RPC client yet, create one. */
Trond Myklebust92737232006-03-20 13:44:45 -0500367 clnt = nlm_bind_host(host);
368 if (clnt == NULL)
369 goto out_err;
Trond Myklebustd4716622006-03-20 13:44:45 -0500370 msg->rpc_proc = &clnt->cl_procinfo[proc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* bootstrap and kick off the async RPC call */
Trond Myklebustd4716622006-03-20 13:44:45 -0500373 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
Trond Myklebust92737232006-03-20 13:44:45 -0500374 if (status == 0)
375 return 0;
376out_err:
377 nlm_release_call(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return status;
379}
380
Trond Myklebustd4716622006-03-20 13:44:45 -0500381int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
382{
383 struct rpc_message msg = {
384 .rpc_argp = &req->a_args,
385 .rpc_resp = &req->a_res,
386 };
387 return __nlm_async_call(req, proc, &msg, tk_ops);
388}
389
390int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
391{
392 struct rpc_message msg = {
393 .rpc_argp = &req->a_res,
394 };
395 return __nlm_async_call(req, proc, &msg, tk_ops);
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
399 * TEST for the presence of a conflicting lock
400 */
401static int
402nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
403{
404 int status;
405
406 status = nlmclnt_call(req, NLMPROC_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Trond Myklebust92737232006-03-20 13:44:45 -0500410 switch (req->a_res.status) {
411 case NLM_LCK_GRANTED:
412 fl->fl_type = F_UNLCK;
413 break;
414 case NLM_LCK_DENIED:
415 /*
416 * Report the conflicting lock back to the application.
417 */
418 fl->fl_start = req->a_res.lock.fl.fl_start;
419 fl->fl_end = req->a_res.lock.fl.fl_start;
420 fl->fl_type = req->a_res.lock.fl.fl_type;
421 fl->fl_pid = 0;
422 break;
423 default:
424 status = nlm_stat_to_errno(req->a_res.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Trond Myklebust92737232006-03-20 13:44:45 -0500426out:
427 nlm_release_call(req);
428 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
432{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500433 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
434 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
435 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 -0700436}
437
438static void nlmclnt_locks_release_private(struct file_lock *fl)
439{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500440 list_del(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444static struct file_lock_operations nlmclnt_lock_ops = {
445 .fl_copy_lock = nlmclnt_locks_copy_lock,
446 .fl_release_private = nlmclnt_locks_release_private,
447};
448
449static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
450{
451 BUG_ON(fl->fl_ops != NULL);
452 fl->fl_u.nfs_fl.state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500454 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 fl->fl_ops = &nlmclnt_lock_ops;
456}
457
458static void do_vfs_lock(struct file_lock *fl)
459{
460 int res = 0;
461 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
462 case FL_POSIX:
463 res = posix_lock_file_wait(fl->fl_file, fl);
464 break;
465 case FL_FLOCK:
466 res = flock_lock_file_wait(fl->fl_file, fl);
467 break;
468 default:
469 BUG();
470 }
471 if (res < 0)
472 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
473 __FUNCTION__);
474}
475
476/*
477 * LOCK: Try to create a lock
478 *
479 * Programmer Harassment Alert
480 *
481 * When given a blocking lock request in a sync RPC call, the HPUX lockd
482 * will faithfully return LCK_BLOCKED but never cares to notify us when
483 * the lock could be granted. This way, our local process could hang
484 * around forever waiting for the callback.
485 *
486 * Solution A: Implement busy-waiting
487 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
488 *
489 * For now I am implementing solution A, because I hate the idea of
490 * re-implementing lockd for a third time in two months. The async
491 * calls shouldn't be too hard to do, however.
492 *
493 * This is one of the lovely things about standards in the NFS area:
494 * they're so soft and squishy you can't really blame HP for doing this.
495 */
496static int
497nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
498{
499 struct nlm_host *host = req->a_host;
500 struct nlm_res *resp = &req->a_res;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500501 struct nlm_wait *block = NULL;
502 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (!host->h_monitored && nsm_monitor(host) < 0) {
505 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
506 host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto out;
508 }
509
Trond Myklebust3a649b82006-03-20 13:44:44 -0500510 block = nlmclnt_prepare_block(host, fl);
Trond Myklebust28df9552006-06-09 09:40:27 -0400511again:
Trond Myklebustecdbf762005-06-22 17:16:31 +0000512 for(;;) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400513 /* Reboot protection */
514 fl->fl_u.nfs_fl.state = host->h_state;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000515 status = nlmclnt_call(req, NLMPROC_LOCK);
516 if (status < 0)
517 goto out_unblock;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500518 if (!req->a_args.block)
Trond Myklebustecdbf762005-06-22 17:16:31 +0000519 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000520 /* Did a reclaimer thread notify us of a server reboot? */
521 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
522 continue;
523 if (resp->status != NLM_LCK_BLOCKED)
524 break;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500525 /* Wait on an NLM blocking lock */
526 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
527 /* if we were interrupted. Send a CANCEL request to the server
Trond Myklebustecdbf762005-06-22 17:16:31 +0000528 * and exit
529 */
Trond Myklebust3a649b82006-03-20 13:44:44 -0500530 if (status < 0)
531 goto out_unblock;
532 if (resp->status != NLM_LCK_BLOCKED)
533 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (resp->status == NLM_LCK_GRANTED) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400537 down_read(&host->h_rwsem);
538 /* Check whether or not the server has rebooted */
539 if (fl->fl_u.nfs_fl.state != host->h_state) {
540 up_read(&host->h_rwsem);
541 goto again;
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 fl->fl_flags |= FL_SLEEP;
Trond Myklebust4c060b52006-03-20 13:44:41 -0500544 /* Ensure the resulting lock will get added to granted list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 do_vfs_lock(fl);
Trond Myklebust28df9552006-06-09 09:40:27 -0400546 up_read(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 status = nlm_stat_to_errno(resp->status);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000549out_unblock:
Trond Myklebust3a649b82006-03-20 13:44:44 -0500550 nlmclnt_finish_block(block);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000551 /* Cancel the blocked request if it is still pending */
552 if (resp->status == NLM_LCK_BLOCKED)
Trond Myklebust16fb2422006-02-01 12:18:22 -0500553 nlmclnt_cancel(host, req->a_args.block, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554out:
Trond Myklebust92737232006-03-20 13:44:45 -0500555 nlm_release_call(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return status;
557}
558
559/*
560 * RECLAIM: Try to reclaim a lock
561 */
562int
563nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
564{
565 struct nlm_rqst reqst, *req;
566 int status;
567
568 req = &reqst;
569 memset(req, 0, sizeof(*req));
570 locks_init_lock(&req->a_args.lock.fl);
571 locks_init_lock(&req->a_res.lock.fl);
572 req->a_host = host;
573 req->a_flags = 0;
574
575 /* Set up the argument struct */
576 nlmclnt_setlockargs(req, fl);
577 req->a_args.reclaim = 1;
578
579 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
580 && req->a_res.status == NLM_LCK_GRANTED)
581 return 0;
582
583 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
584 "(errno %d, status %d)\n", fl->fl_pid,
585 status, req->a_res.status);
586
587 /*
588 * FIXME: This is a serious failure. We can
589 *
590 * a. Ignore the problem
591 * b. Send the owning process some signal (Linux doesn't have
592 * SIGLOST, though...)
593 * c. Retry the operation
594 *
595 * Until someone comes up with a simple implementation
596 * for b or c, I'll choose option a.
597 */
598
599 return -ENOLCK;
600}
601
602/*
603 * UNLOCK: remove an existing lock
604 */
605static int
606nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
607{
Trond Myklebust28df9552006-06-09 09:40:27 -0400608 struct nlm_host *host = req->a_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct nlm_res *resp = &req->a_res;
610 int status;
611
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500612 /*
Trond Myklebust30f4e202006-03-13 21:20:49 -0800613 * Note: the server is supposed to either grant us the unlock
614 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
615 * case, we want to unlock.
616 */
Trond Myklebust28df9552006-06-09 09:40:27 -0400617 down_read(&host->h_rwsem);
Trond Myklebust30f4e202006-03-13 21:20:49 -0800618 do_vfs_lock(fl);
Trond Myklebust28df9552006-06-09 09:40:27 -0400619 up_read(&host->h_rwsem);
Trond Myklebust30f4e202006-03-13 21:20:49 -0800620
Trond Myklebust92737232006-03-20 13:44:45 -0500621 if (req->a_flags & RPC_TASK_ASYNC)
622 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 status = nlmclnt_call(req, NLMPROC_UNLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Trond Myklebust92737232006-03-20 13:44:45 -0500628 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (resp->status == NLM_LCK_GRANTED)
Trond Myklebust92737232006-03-20 13:44:45 -0500630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
633 printk("lockd: unexpected unlock status: %d\n", resp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* What to do now? I'm out of my depth... */
Trond Myklebust92737232006-03-20 13:44:45 -0500635 status = -ENOLCK;
636out:
637 nlm_release_call(req);
638 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100641static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100643 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int status = req->a_res.status;
645
646 if (RPC_ASSASSINATED(task))
647 goto die;
648
649 if (task->tk_status < 0) {
650 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
651 goto retry_rebind;
652 }
653 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
654 rpc_delay(task, NLMCLNT_GRACE_WAIT);
655 goto retry_unlock;
656 }
657 if (status != NLM_LCK_GRANTED)
658 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
659die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return;
661 retry_rebind:
662 nlm_rebind_host(req->a_host);
663 retry_unlock:
664 rpc_restart_call(task);
665}
666
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100667static const struct rpc_call_ops nlmclnt_unlock_ops = {
668 .rpc_call_done = nlmclnt_unlock_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500669 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100670};
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/*
673 * Cancel a blocked lock request.
674 * We always use an async RPC call for this in order not to hang a
675 * process that has been Ctrl-C'ed.
676 */
Trond Myklebust16fb2422006-02-01 12:18:22 -0500677static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct nlm_rqst *req;
680 unsigned long flags;
681 sigset_t oldset;
682 int status;
683
684 /* Block all signals while setting up call */
685 spin_lock_irqsave(&current->sighand->siglock, flags);
686 oldset = current->blocked;
687 sigfillset(&current->blocked);
688 recalc_sigpending();
689 spin_unlock_irqrestore(&current->sighand->siglock, flags);
690
Trond Myklebust92737232006-03-20 13:44:45 -0500691 req = nlm_alloc_call(nlm_get_host(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!req)
693 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 req->a_flags = RPC_TASK_ASYNC;
695
696 nlmclnt_setlockargs(req, fl);
Trond Myklebust16fb2422006-02-01 12:18:22 -0500697 req->a_args.block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Trond Myklebust92737232006-03-20 13:44:45 -0500699 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 spin_lock_irqsave(&current->sighand->siglock, flags);
702 current->blocked = oldset;
703 recalc_sigpending();
704 spin_unlock_irqrestore(&current->sighand->siglock, flags);
705
706 return status;
707}
708
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100709static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100711 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (RPC_ASSASSINATED(task))
714 goto die;
715
716 if (task->tk_status < 0) {
717 dprintk("lockd: CANCEL call error %d, retrying.\n",
718 task->tk_status);
719 goto retry_cancel;
720 }
721
722 dprintk("lockd: cancel status %d (task %d)\n",
723 req->a_res.status, task->tk_pid);
724
725 switch (req->a_res.status) {
726 case NLM_LCK_GRANTED:
727 case NLM_LCK_DENIED_GRACE_PERIOD:
Trond Myklebust35576cb2006-03-20 13:44:41 -0500728 case NLM_LCK_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Everything's good */
730 break;
731 case NLM_LCK_DENIED_NOLOCKS:
732 dprintk("lockd: CANCEL failed (server has no locks)\n");
733 goto retry_cancel;
734 default:
735 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
736 req->a_res.status);
737 }
738
739die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return;
741
742retry_cancel:
Trond Myklebustaaaa9942006-02-01 12:18:25 -0500743 /* Don't ever retry more than 3 times */
744 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
745 goto die;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 nlm_rebind_host(req->a_host);
747 rpc_restart_call(task);
748 rpc_delay(task, 30 * HZ);
749}
750
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100751static const struct rpc_call_ops nlmclnt_cancel_ops = {
752 .rpc_call_done = nlmclnt_cancel_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500753 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100754};
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
757 * Convert an NLM status code to a generic kernel errno
758 */
759static int
760nlm_stat_to_errno(u32 status)
761{
762 switch(status) {
763 case NLM_LCK_GRANTED:
764 return 0;
765 case NLM_LCK_DENIED:
766 return -EAGAIN;
767 case NLM_LCK_DENIED_NOLOCKS:
768 case NLM_LCK_DENIED_GRACE_PERIOD:
769 return -ENOLCK;
770 case NLM_LCK_BLOCKED:
771 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
772 return -ENOLCK;
773#ifdef CONFIG_LOCKD_V4
774 case NLM_DEADLCK:
775 return -EDEADLK;
776 case NLM_ROFS:
777 return -EROFS;
778 case NLM_STALE_FH:
779 return -ESTALE;
780 case NLM_FBIG:
781 return -EOVERFLOW;
782 case NLM_FAILED:
783 return -ENOLCK;
784#endif
785 }
786 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
787 return -ENOLCK;
788}