Import 2.1.118
[davej-history.git] / fs / lockd / clntproc.c
blobbbfdefc7138c8b021d08a5a55eac707f8dea904b
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;
34 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
36 static inline void
37 nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
39 struct nlm_args *argp = &req->a_args;
40 struct nlm_lock *lock = &argp->lock;
42 memset(argp, 0, sizeof(*argp));
43 argp->cookie = nlm_cookie++;
44 argp->state = nsm_local_state;
45 lock->fh = *NFS_FH(fl->fl_file->f_dentry);
46 lock->caller = system_utsname.nodename;
47 lock->oh.data = req->a_owner;
48 lock->oh.len = sprintf(req->a_owner, "%d@%s",
49 current->pid, system_utsname.nodename);
50 lock->fl = *fl;
54 * Initialize arguments for GRANTED call. The nlm_rqst structure
55 * has been cleared already.
57 int
58 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
60 call->a_args.cookie = nlm_cookie++;
61 call->a_args.lock = *lock;
62 call->a_args.lock.caller = system_utsname.nodename;
64 /* set default data area */
65 call->a_args.lock.oh.data = call->a_owner;
67 if (lock->oh.len > NLMCLNT_OHSIZE) {
68 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
69 if (!data)
70 return 0;
71 call->a_args.lock.oh.data = (u8 *) data;
74 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
75 return 1;
78 void
79 nlmclnt_freegrantargs(struct nlm_rqst *call)
82 * Check whether we allocated memory for the owner.
84 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
85 kfree(call->a_args.lock.oh.data);
90 * This is the main entry point for the NLM client.
92 int
93 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
95 struct nfs_server *nfssrv = NFS_SERVER(inode);
96 struct nlm_host *host;
97 struct nlm_rqst reqst, *call = &reqst;
98 sigset_t oldset;
99 unsigned long flags;
100 int status;
102 /* Always use NLM version 1 over UDP for now... */
103 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), IPPROTO_UDP, 1)))
104 return -ENOLCK;
106 /* Create RPC client handle if not there, and copy soft
107 * and intr flags from NFS client. */
108 if (host->h_rpcclnt == NULL) {
109 struct rpc_clnt *clnt;
111 /* Bind an rpc client to this host handle (does not
112 * perform a portmapper lookup) */
113 if (!(clnt = nlm_bind_host(host))) {
114 status = -ENOLCK;
115 goto done;
117 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
118 clnt->cl_intr = nfssrv->client->cl_intr;
119 clnt->cl_chatty = nfssrv->client->cl_chatty;
122 /* Keep the old signal mask */
123 spin_lock_irqsave(&current->sigmask_lock, flags);
124 oldset = current->blocked;
126 /* If we're cleaning up locks because the process is exiting,
127 * perform the RPC call asynchronously. */
128 if (cmd == F_SETLK && fl->fl_type == F_UNLCK
129 && (current->flags & PF_EXITING)) {
130 sigfillset(&current->blocked); /* Mask all signals */
131 recalc_sigpending(current);
132 spin_unlock_irqrestore(&current->sigmask_lock, flags);
134 call = nlmclnt_alloc_call();
135 call->a_flags = RPC_TASK_ASYNC;
136 } else {
137 spin_unlock_irqrestore(&current->sigmask_lock, flags);
138 call->a_flags = 0;
140 call->a_host = host;
142 /* Set up the argument struct */
143 nlmclnt_setlockargs(call, fl);
145 if (cmd == F_GETLK) {
146 status = nlmclnt_test(call, fl);
147 } else if (cmd == F_SETLK && fl->fl_type == F_UNLCK) {
148 status = nlmclnt_unlock(call, fl);
149 } else if (cmd == F_SETLK || cmd == F_SETLKW) {
150 call->a_args.block = (cmd == F_SETLKW)? 1 : 0;
151 status = nlmclnt_lock(call, fl);
152 } else {
153 status = -EINVAL;
156 if (status < 0 && (call->a_flags & RPC_TASK_ASYNC))
157 rpc_free(call);
159 spin_lock_irqsave(&current->sigmask_lock, flags);
160 current->blocked = oldset;
161 recalc_sigpending(current);
162 spin_unlock_irqrestore(&current->sigmask_lock, flags);
164 done:
165 dprintk("lockd: clnt proc returns %d\n", status);
166 nlm_release_host(host);
167 return status;
171 * Wait while server is in grace period
173 static inline int
174 nlmclnt_grace_wait(struct nlm_host *host)
176 if (!host->h_reclaiming)
177 current->timeout = jiffies + 10 * HZ;
178 interruptible_sleep_on(&host->h_gracewait);
179 current->timeout = 0;
180 return signalled()? -ERESTARTSYS : 0;
184 * Allocate an NLM RPC call struct
186 struct nlm_rqst *
187 nlmclnt_alloc_call(void)
189 struct nlm_rqst *call;
191 while (!signalled()) {
192 call = (struct nlm_rqst *) rpc_allocate(RPC_TASK_ASYNC,
193 sizeof(struct nlm_rqst));
194 if (call)
195 return call;
196 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
197 current->timeout = jiffies + 5 * HZ;
198 current->state = TASK_INTERRUPTIBLE;
199 schedule();
200 current->timeout = 0;
202 return NULL;
206 * Generic NLM call
209 nlmclnt_call(struct nlm_rqst *req, u32 proc)
211 struct nlm_host *host = req->a_host;
212 struct rpc_clnt *clnt;
213 struct nlm_args *argp = &req->a_args;
214 struct nlm_res *resp = &req->a_res;
215 int status;
217 dprintk("lockd: call procedure %s on %s\n",
218 nlm_procname(proc), host->h_name);
220 do {
221 if (host->h_reclaiming && !argp->reclaim) {
222 interruptible_sleep_on(&host->h_gracewait);
223 continue;
226 /* If we have no RPC client yet, create one. */
227 if ((clnt = nlm_bind_host(host)) == NULL)
228 return -ENOLCK;
230 /* Perform the RPC call. If an error occurs, try again */
231 if ((status = rpc_call(clnt, proc, argp, resp, 0)) < 0) {
232 dprintk("lockd: rpc_call returned error %d\n", -status);
233 if (status == -ERESTARTSYS)
234 return status;
235 nlm_rebind_host(host);
236 } else
237 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
238 dprintk("lockd: server in grace period\n");
239 if (argp->reclaim) {
240 printk(KERN_WARNING
241 "lockd: spurious grace period reject?!\n");
242 return -ENOLCK;
244 } else {
245 dprintk("lockd: server returns status %d\n", resp->status);
246 return 0; /* Okay, call complete */
249 /* Back off a little and try again */
250 current->timeout = jiffies + 15 * HZ;
251 interruptible_sleep_on(&host->h_gracewait);
252 current->timeout = 0;
253 } while (!signalled());
255 return -ERESTARTSYS;
259 * Generic NLM call, async version.
262 nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
264 struct nlm_host *host = req->a_host;
265 struct rpc_clnt *clnt;
266 struct nlm_args *argp = &req->a_args;
267 struct nlm_res *resp = &req->a_res;
268 int status;
270 dprintk("lockd: call procedure %s on %s (async)\n",
271 nlm_procname(proc), host->h_name);
273 /* If we have no RPC client yet, create one. */
274 if ((clnt = nlm_bind_host(host)) == NULL)
275 return -ENOLCK;
277 /* bootstrap and kick off the async RPC call */
278 status = rpc_do_call(clnt, proc, argp, resp, RPC_TASK_ASYNC,
279 callback, req);
281 /* If the async call is proceeding, increment host refcount */
282 if (status >= 0 && (req->a_flags & RPC_TASK_ASYNC))
283 host->h_count++;
284 return status;
288 * TEST for the presence of a conflicting lock
290 static int
291 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
293 int status;
295 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
296 return status;
298 status = req->a_res.status;
299 if (status == NLM_LCK_GRANTED) {
300 fl->fl_type = F_UNLCK;
301 } if (status == NLM_LCK_DENIED) {
303 * Report the conflicting lock back to the application.
304 * FIXME: Is it OK to report the pid back as well?
306 memcpy(fl, &req->a_res.lock.fl, sizeof(*fl));
307 /* fl->fl_pid = 0; */
308 } else {
309 return nlm_stat_to_errno(req->a_res.status);
312 return 0;
316 * LOCK: Try to create a lock
318 * Programmer Harassment Alert
320 * When given a blocking lock request in a sync RPC call, the HPUX lockd
321 * will faithfully return LCK_BLOCKED but never cares to notify us when
322 * the lock could be granted. This way, our local process could hang
323 * around forever waiting for the callback.
325 * Solution A: Implement busy-waiting
326 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
328 * For now I am implementing solution A, because I hate the idea of
329 * re-implementing lockd for a third time in two months. The async
330 * calls shouldn't be too hard to do, however.
332 * This is one of the lovely things about standards in the NFS area:
333 * they're so soft and squishy you can't really blame HP for doing this.
335 static int
336 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
338 struct nlm_host *host = req->a_host;
339 struct nlm_res *resp = &req->a_res;
340 int status;
342 if (!host->h_monitored && nsm_monitor(host) < 0) {
343 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
344 host->h_name);
345 return -ENOLCK;
348 while (1) {
349 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
350 if (resp->status != NLM_LCK_BLOCKED)
351 break;
352 status = nlmclnt_block(host, fl, &resp->status);
354 if (status < 0)
355 return status;
358 if (resp->status == NLM_LCK_GRANTED) {
359 fl->fl_u.nfs_fl.state = host->h_state;
360 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
363 return nlm_stat_to_errno(resp->status);
367 * RECLAIM: Try to reclaim a lock
370 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
372 struct nlm_rqst reqst, *req;
373 int status;
375 req = &reqst;
376 req->a_host = host;
377 req->a_flags = 0;
379 /* Set up the argument struct */
380 nlmclnt_setlockargs(req, fl);
381 req->a_args.reclaim = 1;
383 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
384 && req->a_res.status == NLM_LCK_GRANTED)
385 return 0;
387 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
388 "(errno %d, status %d)\n", fl->fl_pid,
389 status, req->a_res.status);
392 * FIXME: This is a serious failure. We can
394 * a. Ignore the problem
395 * b. Send the owning process some signal (Linux doesn't have
396 * SIGLOST, though...)
397 * c. Retry the operation
399 * Until someone comes up with a simple implementation
400 * for b or c, I'll choose option a.
403 return -ENOLCK;
407 * UNLOCK: remove an existing lock
409 static int
410 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
412 struct nlm_host *host = req->a_host;
413 struct nlm_res *resp = &req->a_res;
414 int status;
416 /* No monitor, no lock: see nlmclnt_lock().
417 * Since this is an UNLOCK, don't try to setup monitoring here. */
418 if (!host->h_monitored)
419 return -ENOLCK;
421 /* Clean the GRANTED flag now so the lock doesn't get
422 * reclaimed while we're stuck in the unlock call. */
423 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
425 if (req->a_flags & RPC_TASK_ASYNC) {
426 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
427 nlmclnt_unlock_callback);
430 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
431 return status;
433 if (resp->status == NLM_LCK_GRANTED)
434 return 0;
436 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
437 printk("lockd: unexpected unlock status: %d\n", resp->status);
439 /* What to do now? I'm out of my depth... */
441 return -ENOLCK;
444 static void
445 nlmclnt_unlock_callback(struct rpc_task *task)
447 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
448 int status = req->a_res.status;
450 if (RPC_ASSASSINATED(task))
451 goto die;
453 if (task->tk_status < 0) {
454 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
455 nlm_rebind_host(req->a_host);
456 rpc_restart_call(task);
457 return;
459 if (status != NLM_LCK_GRANTED
460 && status != NLM_LCK_DENIED_GRACE_PERIOD) {
461 printk("lockd: unexpected unlock status: %d\n", status);
464 die:
465 rpc_release_task(task);
469 * Cancel a blocked lock request.
470 * We always use an async RPC call for this in order not to hang a
471 * process that has been Ctrl-C'ed.
474 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
476 struct nlm_rqst *req;
477 unsigned long flags;
478 sigset_t oldset;
479 int status;
481 /* Block all signals while setting up call */
482 spin_lock_irqsave(&current->sigmask_lock, flags);
483 oldset = current->blocked;
484 sigfillset(&current->blocked);
485 recalc_sigpending(current);
486 spin_unlock_irqrestore(&current->sigmask_lock, flags);
488 do {
489 req = (struct nlm_rqst *) rpc_allocate(RPC_TASK_ASYNC,
490 sizeof(*req));
491 } while (req == NULL);
492 req->a_host = host;
493 req->a_flags = RPC_TASK_ASYNC;
495 nlmclnt_setlockargs(req, fl);
497 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
498 nlmclnt_cancel_callback);
499 if (status < 0)
500 rpc_free(req);
502 spin_lock_irqsave(&current->sigmask_lock, flags);
503 current->blocked = oldset;
504 recalc_sigpending(current);
505 spin_unlock_irqrestore(&current->sigmask_lock, flags);
507 return status;
510 static void
511 nlmclnt_cancel_callback(struct rpc_task *task)
513 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
515 if (RPC_ASSASSINATED(task))
516 goto die;
518 if (task->tk_status < 0) {
519 dprintk("lockd: CANCEL call error %d, retrying.\n",
520 task->tk_status);
521 goto retry_cancel;
524 dprintk("lockd: cancel status %d (task %d)\n",
525 req->a_res.status, task->tk_pid);
527 switch (req->a_res.status) {
528 case NLM_LCK_GRANTED:
529 case NLM_LCK_DENIED_GRACE_PERIOD:
530 /* Everything's good */
531 break;
532 case NLM_LCK_DENIED_NOLOCKS:
533 dprintk("lockd: CANCEL failed (server has no locks)\n");
534 goto retry_cancel;
535 default:
536 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
537 req->a_res.status);
540 die:
541 rpc_release_task(task);
542 nlm_release_host(req->a_host);
543 kfree(req);
544 return;
546 retry_cancel:
547 nlm_rebind_host(req->a_host);
548 rpc_restart_call(task);
549 rpc_delay(task, 30 * HZ);
550 return;
554 * Convert an NLM status code to a generic kernel errno
556 static int
557 nlm_stat_to_errno(u32 status)
559 switch(status) {
560 case NLM_LCK_GRANTED:
561 return 0;
562 case NLM_LCK_DENIED:
563 return -EAGAIN;
564 case NLM_LCK_DENIED_NOLOCKS:
565 case NLM_LCK_DENIED_GRACE_PERIOD:
566 return -ENOLCK;
567 case NLM_LCK_BLOCKED:
568 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
569 return -ENOLCK;
571 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
572 return -ENOLCK;