Import 2.3.99pre3-3
[davej-history.git] / fs / lockd / clntproc.c
blob20b9bb4902cb2221ca2485f4be1e2c033a5f9927
1 /*
2 * linux/fs/lockd/clntproc.c
4 * RPC procedures for the client side NLM implementation
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/utsname.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/svc.h>
16 #include <linux/lockd/lockd.h>
17 #include <linux/lockd/sm_inter.h>
19 #define NLMDBG_FACILITY NLMDBG_CLIENT
21 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
22 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
23 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
24 static void nlmclnt_unlock_callback(struct rpc_task *);
25 static void nlmclnt_cancel_callback(struct rpc_task *);
26 static int nlm_stat_to_errno(u32 stat);
29 * Cookie counter for NLM requests
31 static u32 nlm_cookie = 0x1234;
33 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
35 memcpy(c->data, &nlm_cookie, 4);
36 memset(c->data+4, 0, 4);
37 c->len=4;
38 nlm_cookie++;
42 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
44 static inline void
45 nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
47 struct nlm_args *argp = &req->a_args;
48 struct nlm_lock *lock = &argp->lock;
50 memset(argp, 0, sizeof(*argp));
51 nlmclnt_next_cookie(&argp->cookie);
52 argp->state = nsm_local_state;
53 lock->fh = *NFS_FH(fl->fl_file->f_dentry);
54 lock->caller = system_utsname.nodename;
55 lock->oh.data = req->a_owner;
56 lock->oh.len = sprintf(req->a_owner, "%d@%s",
57 current->pid, system_utsname.nodename);
58 lock->fl = *fl;
62 * Initialize arguments for GRANTED call. The nlm_rqst structure
63 * has been cleared already.
65 int
66 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
68 nlmclnt_next_cookie(&call->a_args.cookie);
69 call->a_args.lock = *lock;
70 call->a_args.lock.caller = system_utsname.nodename;
72 init_waitqueue_head(&call->a_args.lock.fl.fl_wait);
73 /* set default data area */
74 call->a_args.lock.oh.data = call->a_owner;
76 if (lock->oh.len > NLMCLNT_OHSIZE) {
77 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
78 if (!data)
79 return 0;
80 call->a_args.lock.oh.data = (u8 *) data;
83 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
84 return 1;
87 void
88 nlmclnt_freegrantargs(struct nlm_rqst *call)
91 * Check whether we allocated memory for the owner.
93 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
94 kfree(call->a_args.lock.oh.data);
99 * This is the main entry point for the NLM client.
102 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
104 struct nfs_server *nfssrv = NFS_SERVER(inode);
105 struct nlm_host *host;
106 struct nlm_rqst reqst, *call = &reqst;
107 sigset_t oldset;
108 unsigned long flags;
109 int status;
111 /* Always use NLM version 1 over UDP for now... */
112 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), IPPROTO_UDP, 1)))
113 return -ENOLCK;
115 /* Create RPC client handle if not there, and copy soft
116 * and intr flags from NFS client. */
117 if (host->h_rpcclnt == NULL) {
118 struct rpc_clnt *clnt;
120 /* Bind an rpc client to this host handle (does not
121 * perform a portmapper lookup) */
122 if (!(clnt = nlm_bind_host(host))) {
123 status = -ENOLCK;
124 goto done;
126 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
127 clnt->cl_intr = nfssrv->client->cl_intr;
128 clnt->cl_chatty = nfssrv->client->cl_chatty;
131 /* Keep the old signal mask */
132 spin_lock_irqsave(&current->sigmask_lock, flags);
133 oldset = current->blocked;
135 /* If we're cleaning up locks because the process is exiting,
136 * perform the RPC call asynchronously. */
137 if ((cmd == F_SETLK || cmd == F_SETLKW)
138 && fl->fl_type == F_UNLCK
139 && (current->flags & PF_EXITING)) {
140 sigfillset(&current->blocked); /* Mask all signals */
141 recalc_sigpending(current);
142 spin_unlock_irqrestore(&current->sigmask_lock, flags);
144 call = nlmclnt_alloc_call();
145 call->a_flags = RPC_TASK_ASYNC;
146 } else {
147 spin_unlock_irqrestore(&current->sigmask_lock, flags);
148 call->a_flags = 0;
150 call->a_host = host;
152 /* Set up the argument struct */
153 nlmclnt_setlockargs(call, fl);
155 if (cmd == F_GETLK) {
156 status = nlmclnt_test(call, fl);
157 } else if ((cmd == F_SETLK || cmd == F_SETLKW)
158 && fl->fl_type == F_UNLCK) {
159 status = nlmclnt_unlock(call, fl);
160 } else if (cmd == F_SETLK || cmd == F_SETLKW) {
161 call->a_args.block = (cmd == F_SETLKW)? 1 : 0;
162 status = nlmclnt_lock(call, fl);
163 } else {
164 status = -EINVAL;
167 if (status < 0 && (call->a_flags & RPC_TASK_ASYNC))
168 rpc_free(call);
170 spin_lock_irqsave(&current->sigmask_lock, flags);
171 current->blocked = oldset;
172 recalc_sigpending(current);
173 spin_unlock_irqrestore(&current->sigmask_lock, flags);
175 done:
176 dprintk("lockd: clnt proc returns %d\n", status);
177 nlm_release_host(host);
178 return status;
182 * Wait while server is in grace period
184 static inline int
185 nlmclnt_grace_wait(struct nlm_host *host)
187 if (!host->h_reclaiming)
188 interruptible_sleep_on_timeout(&host->h_gracewait, 10*HZ);
189 else
190 interruptible_sleep_on(&host->h_gracewait);
191 return signalled()? -ERESTARTSYS : 0;
195 * Allocate an NLM RPC call struct
197 struct nlm_rqst *
198 nlmclnt_alloc_call(void)
200 struct nlm_rqst *call;
202 while (!signalled()) {
203 call = (struct nlm_rqst *) rpc_allocate(RPC_TASK_ASYNC,
204 sizeof(struct nlm_rqst));
205 if (call)
206 return call;
207 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
208 current->state = TASK_INTERRUPTIBLE;
209 schedule_timeout(5*HZ);
211 return NULL;
215 * Generic NLM call
218 nlmclnt_call(struct nlm_rqst *req, u32 proc)
220 struct nlm_host *host = req->a_host;
221 struct rpc_clnt *clnt;
222 struct nlm_args *argp = &req->a_args;
223 struct nlm_res *resp = &req->a_res;
224 int status;
226 dprintk("lockd: call procedure %s on %s\n",
227 nlm_procname(proc), host->h_name);
229 do {
230 if (host->h_reclaiming && !argp->reclaim) {
231 interruptible_sleep_on(&host->h_gracewait);
232 continue;
235 /* If we have no RPC client yet, create one. */
236 if ((clnt = nlm_bind_host(host)) == NULL)
237 return -ENOLCK;
239 /* Perform the RPC call. If an error occurs, try again */
240 if ((status = rpc_call(clnt, proc, argp, resp, 0)) < 0) {
241 dprintk("lockd: rpc_call returned error %d\n", -status);
242 switch (status) {
243 case -EPROTONOSUPPORT:
244 status = -EINVAL;
245 break;
246 case -ECONNREFUSED:
247 case -ETIMEDOUT:
248 case -ENOTCONN:
249 status = -EAGAIN;
250 break;
251 case -ERESTARTSYS:
252 return signalled () ? -EINTR : status;
253 default:
254 break;
256 if (req->a_args.block)
257 nlm_rebind_host(host);
258 else
259 break;
260 } else
261 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
262 dprintk("lockd: server in grace period\n");
263 if (argp->reclaim) {
264 printk(KERN_WARNING
265 "lockd: spurious grace period reject?!\n");
266 return -ENOLCK;
268 } else {
269 dprintk("lockd: server returns status %d\n", resp->status);
270 return 0; /* Okay, call complete */
273 /* Back off a little and try again */
274 interruptible_sleep_on_timeout(&host->h_gracewait, 15*HZ);
276 /* When the lock requested by F_SETLKW isn't available,
277 we will wait until the request can be satisfied. If
278 a signal is received during wait, we should return
279 -EINTR. */
280 if (signalled ()) {
281 status = -EINTR;
282 break;
284 } while (1);
286 return status;
290 * Generic NLM call, async version.
293 nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
295 struct nlm_host *host = req->a_host;
296 struct rpc_clnt *clnt;
297 struct nlm_args *argp = &req->a_args;
298 struct nlm_res *resp = &req->a_res;
299 struct rpc_message msg;
300 int status;
302 dprintk("lockd: call procedure %s on %s (async)\n",
303 nlm_procname(proc), host->h_name);
305 /* If we have no RPC client yet, create one. */
306 if ((clnt = nlm_bind_host(host)) == NULL)
307 return -ENOLCK;
309 /* bootstrap and kick off the async RPC call */
310 msg.rpc_proc = proc;
311 msg.rpc_argp = argp;
312 msg.rpc_resp =resp;
313 msg.rpc_cred = NULL;
314 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
316 /* If the async call is proceeding, increment host refcount */
317 if (status >= 0 && (req->a_flags & RPC_TASK_ASYNC))
318 host->h_count++;
319 return status;
323 * TEST for the presence of a conflicting lock
325 static int
326 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
328 int status;
330 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
331 return status;
333 status = req->a_res.status;
334 if (status == NLM_LCK_GRANTED) {
335 fl->fl_type = F_UNLCK;
336 } if (status == NLM_LCK_DENIED) {
338 * Report the conflicting lock back to the application.
339 * FIXME: Is it OK to report the pid back as well?
341 memcpy(fl, &req->a_res.lock.fl, sizeof(*fl));
342 /* fl->fl_pid = 0; */
343 } else {
344 return nlm_stat_to_errno(req->a_res.status);
347 return 0;
351 * LOCK: Try to create a lock
353 * Programmer Harassment Alert
355 * When given a blocking lock request in a sync RPC call, the HPUX lockd
356 * will faithfully return LCK_BLOCKED but never cares to notify us when
357 * the lock could be granted. This way, our local process could hang
358 * around forever waiting for the callback.
360 * Solution A: Implement busy-waiting
361 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
363 * For now I am implementing solution A, because I hate the idea of
364 * re-implementing lockd for a third time in two months. The async
365 * calls shouldn't be too hard to do, however.
367 * This is one of the lovely things about standards in the NFS area:
368 * they're so soft and squishy you can't really blame HP for doing this.
370 static int
371 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
373 struct nlm_host *host = req->a_host;
374 struct nlm_res *resp = &req->a_res;
375 int status;
377 if (!host->h_monitored && nsm_monitor(host) < 0) {
378 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
379 host->h_name);
380 return -ENOLCK;
383 while (1) {
384 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
385 if (resp->status != NLM_LCK_BLOCKED)
386 break;
387 status = nlmclnt_block(host, fl, &resp->status);
389 if (status < 0)
390 return status;
393 if (resp->status == NLM_LCK_GRANTED) {
394 fl->fl_u.nfs_fl.state = host->h_state;
395 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
398 return nlm_stat_to_errno(resp->status);
402 * RECLAIM: Try to reclaim a lock
405 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
407 struct nlm_rqst reqst, *req;
408 int status;
410 req = &reqst;
411 req->a_host = host;
412 req->a_flags = 0;
414 /* Set up the argument struct */
415 nlmclnt_setlockargs(req, fl);
416 req->a_args.reclaim = 1;
418 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
419 && req->a_res.status == NLM_LCK_GRANTED)
420 return 0;
422 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
423 "(errno %d, status %d)\n", fl->fl_pid,
424 status, req->a_res.status);
427 * FIXME: This is a serious failure. We can
429 * a. Ignore the problem
430 * b. Send the owning process some signal (Linux doesn't have
431 * SIGLOST, though...)
432 * c. Retry the operation
434 * Until someone comes up with a simple implementation
435 * for b or c, I'll choose option a.
438 return -ENOLCK;
442 * UNLOCK: remove an existing lock
444 static int
445 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
447 struct nlm_host *host = req->a_host;
448 struct nlm_res *resp = &req->a_res;
449 int status;
451 /* No monitor, no lock: see nlmclnt_lock().
452 * Since this is an UNLOCK, don't try to setup monitoring here. */
453 if (!host->h_monitored)
454 return -ENOLCK;
456 /* Clean the GRANTED flag now so the lock doesn't get
457 * reclaimed while we're stuck in the unlock call. */
458 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
460 if (req->a_flags & RPC_TASK_ASYNC) {
461 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
462 nlmclnt_unlock_callback);
465 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
466 return status;
468 if (resp->status == NLM_LCK_GRANTED)
469 return 0;
471 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
472 printk("lockd: unexpected unlock status: %d\n", resp->status);
474 /* What to do now? I'm out of my depth... */
476 return -ENOLCK;
479 static void
480 nlmclnt_unlock_callback(struct rpc_task *task)
482 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
483 int status = req->a_res.status;
485 if (RPC_ASSASSINATED(task))
486 goto die;
488 if (task->tk_status < 0) {
489 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
490 nlm_rebind_host(req->a_host);
491 rpc_restart_call(task);
492 return;
494 if (status != NLM_LCK_GRANTED
495 && status != NLM_LCK_DENIED_GRACE_PERIOD) {
496 printk("lockd: unexpected unlock status: %d\n", status);
499 die:
500 rpc_release_task(task);
504 * Cancel a blocked lock request.
505 * We always use an async RPC call for this in order not to hang a
506 * process that has been Ctrl-C'ed.
509 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
511 struct nlm_rqst *req;
512 unsigned long flags;
513 sigset_t oldset;
514 int status;
516 /* Block all signals while setting up call */
517 spin_lock_irqsave(&current->sigmask_lock, flags);
518 oldset = current->blocked;
519 sigfillset(&current->blocked);
520 recalc_sigpending(current);
521 spin_unlock_irqrestore(&current->sigmask_lock, flags);
523 do {
524 req = (struct nlm_rqst *) rpc_allocate(RPC_TASK_ASYNC,
525 sizeof(*req));
526 } while (req == NULL);
527 req->a_host = host;
528 req->a_flags = RPC_TASK_ASYNC;
530 nlmclnt_setlockargs(req, fl);
532 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
533 nlmclnt_cancel_callback);
534 if (status < 0)
535 rpc_free(req);
537 spin_lock_irqsave(&current->sigmask_lock, flags);
538 current->blocked = oldset;
539 recalc_sigpending(current);
540 spin_unlock_irqrestore(&current->sigmask_lock, flags);
542 return status;
545 static void
546 nlmclnt_cancel_callback(struct rpc_task *task)
548 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
550 if (RPC_ASSASSINATED(task))
551 goto die;
553 if (task->tk_status < 0) {
554 dprintk("lockd: CANCEL call error %d, retrying.\n",
555 task->tk_status);
556 goto retry_cancel;
559 dprintk("lockd: cancel status %d (task %d)\n",
560 req->a_res.status, task->tk_pid);
562 switch (req->a_res.status) {
563 case NLM_LCK_GRANTED:
564 case NLM_LCK_DENIED_GRACE_PERIOD:
565 /* Everything's good */
566 break;
567 case NLM_LCK_DENIED_NOLOCKS:
568 dprintk("lockd: CANCEL failed (server has no locks)\n");
569 goto retry_cancel;
570 default:
571 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
572 req->a_res.status);
575 die:
576 rpc_release_task(task);
577 nlm_release_host(req->a_host);
578 kfree(req);
579 return;
581 retry_cancel:
582 nlm_rebind_host(req->a_host);
583 rpc_restart_call(task);
584 rpc_delay(task, 30 * HZ);
585 return;
589 * Convert an NLM status code to a generic kernel errno
591 static int
592 nlm_stat_to_errno(u32 status)
594 switch(status) {
595 case NLM_LCK_GRANTED:
596 return 0;
597 case NLM_LCK_DENIED:
598 return -EAGAIN;
599 case NLM_LCK_DENIED_NOLOCKS:
600 case NLM_LCK_DENIED_GRACE_PERIOD:
601 return -ENOLCK;
602 case NLM_LCK_BLOCKED:
603 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
604 return -ENOLCK;
606 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
607 return -ENOLCK;