Import 2.3.26pre2
[davej-history.git] / fs / lockd / clntproc.c
blob4160bf2e32b5537eec9f381fad435268c567f1ba
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(&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 int status;
301 dprintk("lockd: call procedure %s on %s (async)\n",
302 nlm_procname(proc), host->h_name);
304 /* If we have no RPC client yet, create one. */
305 if ((clnt = nlm_bind_host(host)) == NULL)
306 return -ENOLCK;
308 /* bootstrap and kick off the async RPC call */
309 status = rpc_do_call(clnt, proc, argp, resp, RPC_TASK_ASYNC,
310 callback, req);
312 /* If the async call is proceeding, increment host refcount */
313 if (status >= 0 && (req->a_flags & RPC_TASK_ASYNC))
314 host->h_count++;
315 return status;
319 * TEST for the presence of a conflicting lock
321 static int
322 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
324 int status;
326 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
327 return status;
329 status = req->a_res.status;
330 if (status == NLM_LCK_GRANTED) {
331 fl->fl_type = F_UNLCK;
332 } if (status == NLM_LCK_DENIED) {
334 * Report the conflicting lock back to the application.
335 * FIXME: Is it OK to report the pid back as well?
337 memcpy(fl, &req->a_res.lock.fl, sizeof(*fl));
338 /* fl->fl_pid = 0; */
339 } else {
340 return nlm_stat_to_errno(req->a_res.status);
343 return 0;
347 * LOCK: Try to create a lock
349 * Programmer Harassment Alert
351 * When given a blocking lock request in a sync RPC call, the HPUX lockd
352 * will faithfully return LCK_BLOCKED but never cares to notify us when
353 * the lock could be granted. This way, our local process could hang
354 * around forever waiting for the callback.
356 * Solution A: Implement busy-waiting
357 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
359 * For now I am implementing solution A, because I hate the idea of
360 * re-implementing lockd for a third time in two months. The async
361 * calls shouldn't be too hard to do, however.
363 * This is one of the lovely things about standards in the NFS area:
364 * they're so soft and squishy you can't really blame HP for doing this.
366 static int
367 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
369 struct nlm_host *host = req->a_host;
370 struct nlm_res *resp = &req->a_res;
371 int status;
373 if (!host->h_monitored && nsm_monitor(host) < 0) {
374 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
375 host->h_name);
376 return -ENOLCK;
379 while (1) {
380 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
381 if (resp->status != NLM_LCK_BLOCKED)
382 break;
383 status = nlmclnt_block(host, fl, &resp->status);
385 if (status < 0)
386 return status;
389 if (resp->status == NLM_LCK_GRANTED) {
390 fl->fl_u.nfs_fl.state = host->h_state;
391 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
394 return nlm_stat_to_errno(resp->status);
398 * RECLAIM: Try to reclaim a lock
401 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
403 struct nlm_rqst reqst, *req;
404 int status;
406 req = &reqst;
407 req->a_host = host;
408 req->a_flags = 0;
410 /* Set up the argument struct */
411 nlmclnt_setlockargs(req, fl);
412 req->a_args.reclaim = 1;
414 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
415 && req->a_res.status == NLM_LCK_GRANTED)
416 return 0;
418 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
419 "(errno %d, status %d)\n", fl->fl_pid,
420 status, req->a_res.status);
423 * FIXME: This is a serious failure. We can
425 * a. Ignore the problem
426 * b. Send the owning process some signal (Linux doesn't have
427 * SIGLOST, though...)
428 * c. Retry the operation
430 * Until someone comes up with a simple implementation
431 * for b or c, I'll choose option a.
434 return -ENOLCK;
438 * UNLOCK: remove an existing lock
440 static int
441 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
443 struct nlm_host *host = req->a_host;
444 struct nlm_res *resp = &req->a_res;
445 int status;
447 /* No monitor, no lock: see nlmclnt_lock().
448 * Since this is an UNLOCK, don't try to setup monitoring here. */
449 if (!host->h_monitored)
450 return -ENOLCK;
452 /* Clean the GRANTED flag now so the lock doesn't get
453 * reclaimed while we're stuck in the unlock call. */
454 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
456 if (req->a_flags & RPC_TASK_ASYNC) {
457 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
458 nlmclnt_unlock_callback);
461 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
462 return status;
464 if (resp->status == NLM_LCK_GRANTED)
465 return 0;
467 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
468 printk("lockd: unexpected unlock status: %d\n", resp->status);
470 /* What to do now? I'm out of my depth... */
472 return -ENOLCK;
475 static void
476 nlmclnt_unlock_callback(struct rpc_task *task)
478 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
479 int status = req->a_res.status;
481 if (RPC_ASSASSINATED(task))
482 goto die;
484 if (task->tk_status < 0) {
485 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
486 nlm_rebind_host(req->a_host);
487 rpc_restart_call(task);
488 return;
490 if (status != NLM_LCK_GRANTED
491 && status != NLM_LCK_DENIED_GRACE_PERIOD) {
492 printk("lockd: unexpected unlock status: %d\n", status);
495 die:
496 rpc_release_task(task);
500 * Cancel a blocked lock request.
501 * We always use an async RPC call for this in order not to hang a
502 * process that has been Ctrl-C'ed.
505 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
507 struct nlm_rqst *req;
508 unsigned long flags;
509 sigset_t oldset;
510 int status;
512 /* Block all signals while setting up call */
513 spin_lock_irqsave(&current->sigmask_lock, flags);
514 oldset = current->blocked;
515 sigfillset(&current->blocked);
516 recalc_sigpending(current);
517 spin_unlock_irqrestore(&current->sigmask_lock, flags);
519 do {
520 req = (struct nlm_rqst *) rpc_allocate(RPC_TASK_ASYNC,
521 sizeof(*req));
522 } while (req == NULL);
523 req->a_host = host;
524 req->a_flags = RPC_TASK_ASYNC;
526 nlmclnt_setlockargs(req, fl);
528 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
529 nlmclnt_cancel_callback);
530 if (status < 0)
531 rpc_free(req);
533 spin_lock_irqsave(&current->sigmask_lock, flags);
534 current->blocked = oldset;
535 recalc_sigpending(current);
536 spin_unlock_irqrestore(&current->sigmask_lock, flags);
538 return status;
541 static void
542 nlmclnt_cancel_callback(struct rpc_task *task)
544 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
546 if (RPC_ASSASSINATED(task))
547 goto die;
549 if (task->tk_status < 0) {
550 dprintk("lockd: CANCEL call error %d, retrying.\n",
551 task->tk_status);
552 goto retry_cancel;
555 dprintk("lockd: cancel status %d (task %d)\n",
556 req->a_res.status, task->tk_pid);
558 switch (req->a_res.status) {
559 case NLM_LCK_GRANTED:
560 case NLM_LCK_DENIED_GRACE_PERIOD:
561 /* Everything's good */
562 break;
563 case NLM_LCK_DENIED_NOLOCKS:
564 dprintk("lockd: CANCEL failed (server has no locks)\n");
565 goto retry_cancel;
566 default:
567 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
568 req->a_res.status);
571 die:
572 rpc_release_task(task);
573 nlm_release_host(req->a_host);
574 kfree(req);
575 return;
577 retry_cancel:
578 nlm_rebind_host(req->a_host);
579 rpc_restart_call(task);
580 rpc_delay(task, 30 * HZ);
581 return;
585 * Convert an NLM status code to a generic kernel errno
587 static int
588 nlm_stat_to_errno(u32 status)
590 switch(status) {
591 case NLM_LCK_GRANTED:
592 return 0;
593 case NLM_LCK_DENIED:
594 return -EAGAIN;
595 case NLM_LCK_DENIED_NOLOCKS:
596 case NLM_LCK_DENIED_GRACE_PERIOD:
597 return -ENOLCK;
598 case NLM_LCK_BLOCKED:
599 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
600 return -ENOLCK;
602 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
603 return -ENOLCK;