Fix prototype of SMP version of synchronize_irq.
[linux-2.6/linux-mips.git] / fs / lockd / clntproc.c
blobc4c4e05951632bcf54779caebb9eb11323536197
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/config.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/fs.h>
13 #include <linux/nfs_fs.h>
14 #include <linux/utsname.h>
15 #include <linux/smp_lock.h>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/svc.h>
18 #include <linux/lockd/lockd.h>
19 #include <linux/lockd/sm_inter.h>
21 #define NLMDBG_FACILITY NLMDBG_CLIENT
22 #define NLMCLNT_GRACE_WAIT (5*HZ)
24 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
25 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
26 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
27 static void nlmclnt_unlock_callback(struct rpc_task *);
28 static void nlmclnt_cancel_callback(struct rpc_task *);
29 static int nlm_stat_to_errno(u32 stat);
32 * Cookie counter for NLM requests
34 static u32 nlm_cookie = 0x1234;
36 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
38 memcpy(c->data, &nlm_cookie, 4);
39 memset(c->data+4, 0, 4);
40 c->len=4;
41 nlm_cookie++;
45 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
47 static inline void
48 nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
50 struct nlm_args *argp = &req->a_args;
51 struct nlm_lock *lock = &argp->lock;
53 nlmclnt_next_cookie(&argp->cookie);
54 argp->state = nsm_local_state;
55 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
56 lock->caller = system_utsname.nodename;
57 lock->oh.data = req->a_owner;
58 lock->oh.len = sprintf(req->a_owner, "%d@%s",
59 current->pid, system_utsname.nodename);
60 locks_copy_lock(&lock->fl, fl);
64 * Initialize arguments for GRANTED call. The nlm_rqst structure
65 * has been cleared already.
67 int
68 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
70 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
71 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
72 call->a_args.lock.caller = system_utsname.nodename;
73 call->a_args.lock.oh.len = lock->oh.len;
75 /* set default data area */
76 call->a_args.lock.oh.data = call->a_owner;
78 if (lock->oh.len > NLMCLNT_OHSIZE) {
79 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
80 if (!data)
81 return 0;
82 call->a_args.lock.oh.data = (u8 *) data;
85 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
86 return 1;
89 void
90 nlmclnt_freegrantargs(struct nlm_rqst *call)
93 * Check whether we allocated memory for the owner.
95 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
96 kfree(call->a_args.lock.oh.data);
101 * This is the main entry point for the NLM client.
104 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
106 struct nfs_server *nfssrv = NFS_SERVER(inode);
107 struct nlm_host *host;
108 struct nlm_rqst reqst, *call = &reqst;
109 sigset_t oldset;
110 unsigned long flags;
111 int status, proto, vers;
113 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
114 if (NFS_PROTO(inode)->version > 3) {
115 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
116 return -ENOLCK;
119 /* Retrieve transport protocol from NFS client */
120 proto = NFS_CLIENT(inode)->cl_xprt->prot;
122 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
123 return -ENOLCK;
125 /* Create RPC client handle if not there, and copy soft
126 * and intr flags from NFS client. */
127 if (host->h_rpcclnt == NULL) {
128 struct rpc_clnt *clnt;
130 /* Bind an rpc client to this host handle (does not
131 * perform a portmapper lookup) */
132 if (!(clnt = nlm_bind_host(host))) {
133 status = -ENOLCK;
134 goto done;
136 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
137 clnt->cl_intr = nfssrv->client->cl_intr;
138 clnt->cl_chatty = nfssrv->client->cl_chatty;
141 /* Keep the old signal mask */
142 spin_lock_irqsave(&current->sighand->siglock, flags);
143 oldset = current->blocked;
145 /* If we're cleaning up locks because the process is exiting,
146 * perform the RPC call asynchronously. */
147 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
148 && fl->fl_type == F_UNLCK
149 && (current->flags & PF_EXITING)) {
150 sigfillset(&current->blocked); /* Mask all signals */
151 recalc_sigpending();
152 spin_unlock_irqrestore(&current->sighand->siglock, flags);
154 call = nlmclnt_alloc_call();
155 if (!call) {
156 status = -ENOMEM;
157 goto out_restore;
159 call->a_flags = RPC_TASK_ASYNC;
160 } else {
161 spin_unlock_irqrestore(&current->sighand->siglock, flags);
162 memset(call, 0, sizeof(*call));
163 locks_init_lock(&call->a_args.lock.fl);
164 locks_init_lock(&call->a_res.lock.fl);
166 call->a_host = host;
168 /* Set up the argument struct */
169 nlmclnt_setlockargs(call, fl);
171 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
172 if (fl->fl_type != F_UNLCK) {
173 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
174 status = nlmclnt_lock(call, fl);
175 } else
176 status = nlmclnt_unlock(call, fl);
177 } else if (IS_GETLK(cmd))
178 status = nlmclnt_test(call, fl);
179 else
180 status = -EINVAL;
182 if (status < 0 && (call->a_flags & RPC_TASK_ASYNC))
183 kfree(call);
185 out_restore:
186 spin_lock_irqsave(&current->sighand->siglock, flags);
187 current->blocked = oldset;
188 recalc_sigpending();
189 spin_unlock_irqrestore(&current->sighand->siglock, flags);
191 done:
192 dprintk("lockd: clnt proc returns %d\n", status);
193 nlm_release_host(host);
194 return status;
198 * Wait while server is in grace period
200 static inline int
201 nlmclnt_grace_wait(struct nlm_host *host)
203 if (!host->h_reclaiming)
204 interruptible_sleep_on_timeout(&host->h_gracewait, 10*HZ);
205 else
206 interruptible_sleep_on(&host->h_gracewait);
207 return signalled()? -ERESTARTSYS : 0;
211 * Allocate an NLM RPC call struct
213 struct nlm_rqst *
214 nlmclnt_alloc_call(void)
216 struct nlm_rqst *call;
218 while (!signalled()) {
219 call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
220 if (call) {
221 memset(call, 0, sizeof(*call));
222 locks_init_lock(&call->a_args.lock.fl);
223 locks_init_lock(&call->a_res.lock.fl);
224 return call;
226 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
227 current->state = TASK_INTERRUPTIBLE;
228 schedule_timeout(5*HZ);
230 return NULL;
234 * Generic NLM call
237 nlmclnt_call(struct nlm_rqst *req, u32 proc)
239 struct nlm_host *host = req->a_host;
240 struct rpc_clnt *clnt;
241 struct nlm_args *argp = &req->a_args;
242 struct nlm_res *resp = &req->a_res;
243 struct file *filp = argp->lock.fl.fl_file;
244 struct rpc_message msg = {
245 .rpc_argp = argp,
246 .rpc_resp = resp,
248 int status;
250 dprintk("lockd: call procedure %d on %s\n",
251 (int)proc, host->h_name);
253 if (filp)
254 msg.rpc_cred = nfs_file_cred(filp);
256 do {
257 if (host->h_reclaiming && !argp->reclaim) {
258 interruptible_sleep_on(&host->h_gracewait);
259 continue;
262 /* If we have no RPC client yet, create one. */
263 if ((clnt = nlm_bind_host(host)) == NULL)
264 return -ENOLCK;
265 msg.rpc_proc = &clnt->cl_procinfo[proc];
267 /* Perform the RPC call. If an error occurs, try again */
268 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
269 dprintk("lockd: rpc_call returned error %d\n", -status);
270 switch (status) {
271 case -EPROTONOSUPPORT:
272 status = -EINVAL;
273 break;
274 case -ECONNREFUSED:
275 case -ETIMEDOUT:
276 case -ENOTCONN:
277 nlm_rebind_host(host);
278 status = -EAGAIN;
279 break;
280 case -ERESTARTSYS:
281 return signalled () ? -EINTR : status;
282 default:
283 break;
285 break;
286 } else
287 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
288 dprintk("lockd: server in grace period\n");
289 if (argp->reclaim) {
290 printk(KERN_WARNING
291 "lockd: spurious grace period reject?!\n");
292 return -ENOLCK;
294 } else {
295 dprintk("lockd: server returns status %d\n", resp->status);
296 return 0; /* Okay, call complete */
299 /* Back off a little and try again */
300 interruptible_sleep_on_timeout(&host->h_gracewait, 15*HZ);
302 /* When the lock requested by F_SETLKW isn't available,
303 we will wait until the request can be satisfied. If
304 a signal is received during wait, we should return
305 -EINTR. */
306 if (signalled ()) {
307 status = -EINTR;
308 break;
310 } while (1);
312 return status;
316 * Generic NLM call, async version.
319 nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
321 struct nlm_host *host = req->a_host;
322 struct rpc_clnt *clnt;
323 struct rpc_message msg = {
324 .rpc_argp = &req->a_args,
325 .rpc_resp = &req->a_res,
327 int status;
329 dprintk("lockd: call procedure %d on %s (async)\n",
330 (int)proc, host->h_name);
332 /* If we have no RPC client yet, create one. */
333 if ((clnt = nlm_bind_host(host)) == NULL)
334 return -ENOLCK;
335 msg.rpc_proc = &clnt->cl_procinfo[proc];
337 /* bootstrap and kick off the async RPC call */
338 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
340 return status;
344 nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
346 struct nlm_host *host = req->a_host;
347 struct rpc_clnt *clnt;
348 struct nlm_args *argp = &req->a_args;
349 struct nlm_res *resp = &req->a_res;
350 struct file *file = argp->lock.fl.fl_file;
351 struct rpc_message msg = {
352 .rpc_argp = argp,
353 .rpc_resp = resp,
355 int status;
357 dprintk("lockd: call procedure %d on %s (async)\n",
358 (int)proc, host->h_name);
360 /* If we have no RPC client yet, create one. */
361 if ((clnt = nlm_bind_host(host)) == NULL)
362 return -ENOLCK;
363 msg.rpc_proc = &clnt->cl_procinfo[proc];
365 /* bootstrap and kick off the async RPC call */
366 if (file)
367 msg.rpc_cred = nfs_file_cred(file);
368 /* Increment host refcount */
369 nlm_get_host(host);
370 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
371 if (status < 0)
372 nlm_release_host(host);
373 return status;
377 * TEST for the presence of a conflicting lock
379 static int
380 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
382 int status;
384 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
385 return status;
387 status = req->a_res.status;
388 if (status == NLM_LCK_GRANTED) {
389 fl->fl_type = F_UNLCK;
390 } if (status == NLM_LCK_DENIED) {
392 * Report the conflicting lock back to the application.
393 * FIXME: Is it OK to report the pid back as well?
395 locks_copy_lock(fl, &req->a_res.lock.fl);
396 /* fl->fl_pid = 0; */
397 } else {
398 return nlm_stat_to_errno(req->a_res.status);
401 return 0;
404 static
405 void nlmclnt_insert_lock_callback(struct file_lock *fl)
407 nlm_get_host(fl->fl_u.nfs_fl.host);
409 static
410 void nlmclnt_remove_lock_callback(struct file_lock *fl)
412 if (fl->fl_u.nfs_fl.host) {
413 nlm_release_host(fl->fl_u.nfs_fl.host);
414 fl->fl_u.nfs_fl.host = NULL;
419 * LOCK: Try to create a lock
421 * Programmer Harassment Alert
423 * When given a blocking lock request in a sync RPC call, the HPUX lockd
424 * will faithfully return LCK_BLOCKED but never cares to notify us when
425 * the lock could be granted. This way, our local process could hang
426 * around forever waiting for the callback.
428 * Solution A: Implement busy-waiting
429 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
431 * For now I am implementing solution A, because I hate the idea of
432 * re-implementing lockd for a third time in two months. The async
433 * calls shouldn't be too hard to do, however.
435 * This is one of the lovely things about standards in the NFS area:
436 * they're so soft and squishy you can't really blame HP for doing this.
438 static int
439 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
441 struct nlm_host *host = req->a_host;
442 struct nlm_res *resp = &req->a_res;
443 int status;
445 if (!host->h_monitored && nsm_monitor(host) < 0) {
446 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
447 host->h_name);
448 return -ENOLCK;
451 do {
452 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
453 if (resp->status != NLM_LCK_BLOCKED)
454 break;
455 status = nlmclnt_block(host, fl, &resp->status);
457 if (status < 0)
458 return status;
459 } while (resp->status == NLM_LCK_BLOCKED);
461 if (resp->status == NLM_LCK_GRANTED) {
462 fl->fl_u.nfs_fl.state = host->h_state;
463 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
464 fl->fl_u.nfs_fl.host = host;
465 fl->fl_insert = nlmclnt_insert_lock_callback;
466 fl->fl_remove = nlmclnt_remove_lock_callback;
469 return nlm_stat_to_errno(resp->status);
473 * RECLAIM: Try to reclaim a lock
476 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
478 struct nlm_rqst reqst, *req;
479 int status;
481 req = &reqst;
482 memset(req, 0, sizeof(*req));
483 locks_init_lock(&req->a_args.lock.fl);
484 locks_init_lock(&req->a_res.lock.fl);
485 req->a_host = host;
486 req->a_flags = 0;
488 /* Set up the argument struct */
489 nlmclnt_setlockargs(req, fl);
490 req->a_args.reclaim = 1;
492 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
493 && req->a_res.status == NLM_LCK_GRANTED)
494 return 0;
496 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
497 "(errno %d, status %d)\n", fl->fl_pid,
498 status, req->a_res.status);
501 * FIXME: This is a serious failure. We can
503 * a. Ignore the problem
504 * b. Send the owning process some signal (Linux doesn't have
505 * SIGLOST, though...)
506 * c. Retry the operation
508 * Until someone comes up with a simple implementation
509 * for b or c, I'll choose option a.
512 return -ENOLCK;
516 * UNLOCK: remove an existing lock
518 static int
519 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
521 struct nlm_res *resp = &req->a_res;
522 int status;
524 /* Clean the GRANTED flag now so the lock doesn't get
525 * reclaimed while we're stuck in the unlock call. */
526 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
528 if (req->a_flags & RPC_TASK_ASYNC) {
529 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
530 nlmclnt_unlock_callback);
533 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
534 return status;
536 if (resp->status == NLM_LCK_GRANTED)
537 return 0;
539 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
540 printk("lockd: unexpected unlock status: %d\n", resp->status);
542 /* What to do now? I'm out of my depth... */
544 return -ENOLCK;
547 static void
548 nlmclnt_unlock_callback(struct rpc_task *task)
550 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
551 int status = req->a_res.status;
553 if (RPC_ASSASSINATED(task))
554 goto die;
556 if (task->tk_status < 0) {
557 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
558 goto retry_rebind;
560 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
561 rpc_delay(task, NLMCLNT_GRACE_WAIT);
562 goto retry_unlock;
564 if (status != NLM_LCK_GRANTED)
565 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
567 die:
568 nlm_release_host(req->a_host);
569 kfree(req);
570 return;
571 retry_rebind:
572 nlm_rebind_host(req->a_host);
573 retry_unlock:
574 rpc_restart_call(task);
578 * Cancel a blocked lock request.
579 * We always use an async RPC call for this in order not to hang a
580 * process that has been Ctrl-C'ed.
583 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
585 struct nlm_rqst *req;
586 unsigned long flags;
587 sigset_t oldset;
588 int status;
590 /* Block all signals while setting up call */
591 spin_lock_irqsave(&current->sighand->siglock, flags);
592 oldset = current->blocked;
593 sigfillset(&current->blocked);
594 recalc_sigpending();
595 spin_unlock_irqrestore(&current->sighand->siglock, flags);
597 req = nlmclnt_alloc_call();
598 if (!req)
599 return -ENOMEM;
600 req->a_host = host;
601 req->a_flags = RPC_TASK_ASYNC;
603 nlmclnt_setlockargs(req, fl);
605 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
606 nlmclnt_cancel_callback);
607 if (status < 0)
608 kfree(req);
610 spin_lock_irqsave(&current->sighand->siglock, flags);
611 current->blocked = oldset;
612 recalc_sigpending();
613 spin_unlock_irqrestore(&current->sighand->siglock, flags);
615 return status;
618 static void
619 nlmclnt_cancel_callback(struct rpc_task *task)
621 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
623 if (RPC_ASSASSINATED(task))
624 goto die;
626 if (task->tk_status < 0) {
627 dprintk("lockd: CANCEL call error %d, retrying.\n",
628 task->tk_status);
629 goto retry_cancel;
632 dprintk("lockd: cancel status %d (task %d)\n",
633 req->a_res.status, task->tk_pid);
635 switch (req->a_res.status) {
636 case NLM_LCK_GRANTED:
637 case NLM_LCK_DENIED_GRACE_PERIOD:
638 /* Everything's good */
639 break;
640 case NLM_LCK_DENIED_NOLOCKS:
641 dprintk("lockd: CANCEL failed (server has no locks)\n");
642 goto retry_cancel;
643 default:
644 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
645 req->a_res.status);
648 die:
649 nlm_release_host(req->a_host);
650 kfree(req);
651 return;
653 retry_cancel:
654 nlm_rebind_host(req->a_host);
655 rpc_restart_call(task);
656 rpc_delay(task, 30 * HZ);
660 * Convert an NLM status code to a generic kernel errno
662 static int
663 nlm_stat_to_errno(u32 status)
665 switch(status) {
666 case NLM_LCK_GRANTED:
667 return 0;
668 case NLM_LCK_DENIED:
669 return -EAGAIN;
670 case NLM_LCK_DENIED_NOLOCKS:
671 case NLM_LCK_DENIED_GRACE_PERIOD:
672 return -ENOLCK;
673 case NLM_LCK_BLOCKED:
674 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
675 return -ENOLCK;
676 #ifdef CONFIG_LOCKD_V4
677 case NLM_DEADLCK:
678 return -EDEADLK;
679 case NLM_ROFS:
680 return -EROFS;
681 case NLM_STALE_FH:
682 return -ESTALE;
683 case NLM_FBIG:
684 return -EOVERFLOW;
685 case NLM_FAILED:
686 return -ENOLCK;
687 #endif
689 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
690 return -ENOLCK;