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>
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/errno.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>
22 #define NLMDBG_FACILITY NLMDBG_CLIENT
23 #define NLMCLNT_GRACE_WAIT (5*HZ)
24 #define NLMCLNT_POLL_TIMEOUT (30*HZ)
25 #define NLMCLNT_MAX_RETRIES 3
27 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
28 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
29 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
30 static int nlm_stat_to_errno(u32 stat);
31 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
32 static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
34 static const struct rpc_call_ops nlmclnt_unlock_ops;
35 static const struct rpc_call_ops nlmclnt_cancel_ops;
38 * Cookie counter for NLM requests
40 static u32 nlm_cookie = 0x1234;
42 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
44 memcpy(c->data, &nlm_cookie, 4);
45 memset(c->data+4, 0, 4);
50 static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
52 atomic_inc(&lockowner->count);
56 static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
58 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
60 list_del(&lockowner->list);
61 spin_unlock(&lockowner->host->h_lock);
62 nlm_release_host(lockowner->host);
66 static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
68 struct nlm_lockowner *lockowner;
69 list_for_each_entry(lockowner, &host->h_lockowners, list) {
70 if (lockowner->pid == pid)
76 static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
80 res = host->h_pidcount++;
81 } while (nlm_pidbusy(host, res) < 0);
85 static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
87 struct nlm_lockowner *lockowner;
88 list_for_each_entry(lockowner, &host->h_lockowners, list) {
89 if (lockowner->owner != owner)
91 return nlm_get_lockowner(lockowner);
96 static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
98 struct nlm_lockowner *res, *new = NULL;
100 spin_lock(&host->h_lock);
101 res = __nlm_find_lockowner(host, owner);
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) {
109 atomic_set(&new->count, 1);
111 new->pid = __nlm_alloc_pid(host);
112 new->host = nlm_get_host(host);
113 list_add(&new->list, &host->h_lockowners);
117 spin_unlock(&host->h_lock);
123 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
125 static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
127 struct nlm_args *argp = &req->a_args;
128 struct nlm_lock *lock = &argp->lock;
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;
135 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;
139 lock->fl.fl_start = fl->fl_start;
140 lock->fl.fl_end = fl->fl_end;
141 lock->fl.fl_type = fl->fl_type;
144 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
146 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
150 * This is the main entry point for the NLM client.
153 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
155 struct nfs_server *nfssrv = NFS_SERVER(inode);
156 struct nlm_host *host;
157 struct nlm_rqst reqst, *call = &reqst;
160 int status, proto, vers;
162 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
163 if (NFS_PROTO(inode)->version > 3) {
164 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
168 /* Retrieve transport protocol from NFS client */
169 proto = NFS_CLIENT(inode)->cl_xprt->prot;
171 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
174 /* Create RPC client handle if not there, and copy soft
175 * and intr flags from NFS client. */
176 if (host->h_rpcclnt == NULL) {
177 struct rpc_clnt *clnt;
179 /* Bind an rpc client to this host handle (does not
180 * perform a portmapper lookup) */
181 if (!(clnt = nlm_bind_host(host))) {
185 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
186 clnt->cl_intr = nfssrv->client->cl_intr;
189 /* Keep the old signal mask */
190 spin_lock_irqsave(¤t->sighand->siglock, flags);
191 oldset = current->blocked;
193 /* If we're cleaning up locks because the process is exiting,
194 * perform the RPC call asynchronously. */
195 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
196 && fl->fl_type == F_UNLCK
197 && (current->flags & PF_EXITING)) {
198 sigfillset(¤t->blocked); /* Mask all signals */
200 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
202 call = nlmclnt_alloc_call();
207 call->a_flags = RPC_TASK_ASYNC;
209 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
210 memset(call, 0, sizeof(*call));
211 locks_init_lock(&call->a_args.lock.fl);
212 locks_init_lock(&call->a_res.lock.fl);
216 nlmclnt_locks_init_private(fl, host);
218 /* Set up the argument struct */
219 nlmclnt_setlockargs(call, fl);
221 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
222 if (fl->fl_type != F_UNLCK) {
223 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
224 status = nlmclnt_lock(call, fl);
226 status = nlmclnt_unlock(call, fl);
227 } else if (IS_GETLK(cmd))
228 status = nlmclnt_test(call, fl);
233 spin_lock_irqsave(¤t->sighand->siglock, flags);
234 current->blocked = oldset;
236 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
239 dprintk("lockd: clnt proc returns %d\n", status);
240 nlm_release_host(host);
243 EXPORT_SYMBOL(nlmclnt_proc);
246 * Allocate an NLM RPC call struct
249 nlmclnt_alloc_call(void)
251 struct nlm_rqst *call;
254 call = kzalloc(sizeof(*call), GFP_KERNEL);
256 locks_init_lock(&call->a_args.lock.fl);
257 locks_init_lock(&call->a_res.lock.fl);
262 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
263 schedule_timeout_interruptible(5*HZ);
268 static int nlm_wait_on_grace(wait_queue_head_t *queue)
273 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
275 schedule_timeout(NLMCLNT_GRACE_WAIT);
280 finish_wait(queue, &wait);
288 nlmclnt_call(struct nlm_rqst *req, u32 proc)
290 struct nlm_host *host = req->a_host;
291 struct rpc_clnt *clnt;
292 struct nlm_args *argp = &req->a_args;
293 struct nlm_res *resp = &req->a_res;
294 struct rpc_message msg = {
300 dprintk("lockd: call procedure %d on %s\n",
301 (int)proc, host->h_name);
304 if (host->h_reclaiming && !argp->reclaim)
305 goto in_grace_period;
307 /* If we have no RPC client yet, create one. */
308 if ((clnt = nlm_bind_host(host)) == NULL)
310 msg.rpc_proc = &clnt->cl_procinfo[proc];
312 /* Perform the RPC call. If an error occurs, try again */
313 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
314 dprintk("lockd: rpc_call returned error %d\n", -status);
316 case -EPROTONOSUPPORT:
322 nlm_rebind_host(host);
326 return signalled () ? -EINTR : status;
332 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
333 dprintk("lockd: server in grace period\n");
336 "lockd: spurious grace period reject?!\n");
340 if (!argp->reclaim) {
341 /* We appear to be out of the grace period */
342 wake_up_all(&host->h_gracewait);
344 dprintk("lockd: server returns status %d\n", resp->status);
345 return 0; /* Okay, call complete */
350 * The server has rebooted and appears to be in the grace
351 * period during which locks are only allowed to be
353 * We can only back off and try again later.
355 status = nlm_wait_on_grace(&host->h_gracewait);
356 } while (status == 0);
362 * Generic NLM call, async version.
364 int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
366 struct nlm_host *host = req->a_host;
367 struct rpc_clnt *clnt;
368 struct rpc_message msg = {
369 .rpc_argp = &req->a_args,
370 .rpc_resp = &req->a_res,
374 dprintk("lockd: call procedure %d on %s (async)\n",
375 (int)proc, host->h_name);
377 /* If we have no RPC client yet, create one. */
378 if ((clnt = nlm_bind_host(host)) == NULL)
380 msg.rpc_proc = &clnt->cl_procinfo[proc];
382 /* bootstrap and kick off the async RPC call */
383 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
388 static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
390 struct nlm_host *host = req->a_host;
391 struct rpc_clnt *clnt;
392 struct nlm_args *argp = &req->a_args;
393 struct nlm_res *resp = &req->a_res;
394 struct rpc_message msg = {
400 dprintk("lockd: call procedure %d on %s (async)\n",
401 (int)proc, host->h_name);
403 /* If we have no RPC client yet, create one. */
404 if ((clnt = nlm_bind_host(host)) == NULL)
406 msg.rpc_proc = &clnt->cl_procinfo[proc];
408 /* Increment host refcount */
410 /* bootstrap and kick off the async RPC call */
411 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
413 nlm_release_host(host);
418 * TEST for the presence of a conflicting lock
421 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
425 status = nlmclnt_call(req, NLMPROC_TEST);
426 nlmclnt_release_lockargs(req);
430 status = req->a_res.status;
431 if (status == NLM_LCK_GRANTED) {
432 fl->fl_type = F_UNLCK;
433 } if (status == NLM_LCK_DENIED) {
435 * Report the conflicting lock back to the application.
437 locks_copy_lock(fl, &req->a_res.lock.fl);
440 return nlm_stat_to_errno(req->a_res.status);
446 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
448 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
449 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
450 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
453 static void nlmclnt_locks_release_private(struct file_lock *fl)
455 list_del(&fl->fl_u.nfs_fl.list);
456 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
459 static struct file_lock_operations nlmclnt_lock_ops = {
460 .fl_copy_lock = nlmclnt_locks_copy_lock,
461 .fl_release_private = nlmclnt_locks_release_private,
464 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
466 BUG_ON(fl->fl_ops != NULL);
467 fl->fl_u.nfs_fl.state = 0;
468 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
469 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
470 fl->fl_ops = &nlmclnt_lock_ops;
473 static void do_vfs_lock(struct file_lock *fl)
476 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
478 res = posix_lock_file_wait(fl->fl_file, fl);
481 res = flock_lock_file_wait(fl->fl_file, fl);
487 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
492 * LOCK: Try to create a lock
494 * Programmer Harassment Alert
496 * When given a blocking lock request in a sync RPC call, the HPUX lockd
497 * will faithfully return LCK_BLOCKED but never cares to notify us when
498 * the lock could be granted. This way, our local process could hang
499 * around forever waiting for the callback.
501 * Solution A: Implement busy-waiting
502 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
504 * For now I am implementing solution A, because I hate the idea of
505 * re-implementing lockd for a third time in two months. The async
506 * calls shouldn't be too hard to do, however.
508 * This is one of the lovely things about standards in the NFS area:
509 * they're so soft and squishy you can't really blame HP for doing this.
512 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
514 struct nlm_host *host = req->a_host;
515 struct nlm_res *resp = &req->a_res;
516 struct nlm_wait *block = NULL;
517 int status = -ENOLCK;
519 if (!host->h_monitored && nsm_monitor(host) < 0) {
520 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
525 block = nlmclnt_prepare_block(host, fl);
527 status = nlmclnt_call(req, NLMPROC_LOCK);
530 if (!req->a_args.block)
532 /* Did a reclaimer thread notify us of a server reboot? */
533 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
535 if (resp->status != NLM_LCK_BLOCKED)
537 /* Wait on an NLM blocking lock */
538 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
539 /* if we were interrupted. Send a CANCEL request to the server
544 if (resp->status != NLM_LCK_BLOCKED)
548 if (resp->status == NLM_LCK_GRANTED) {
549 fl->fl_u.nfs_fl.state = host->h_state;
550 fl->fl_flags |= FL_SLEEP;
551 /* Ensure the resulting lock will get added to granted list */
554 status = nlm_stat_to_errno(resp->status);
556 nlmclnt_finish_block(block);
557 /* Cancel the blocked request if it is still pending */
558 if (resp->status == NLM_LCK_BLOCKED)
559 nlmclnt_cancel(host, req->a_args.block, fl);
561 nlmclnt_release_lockargs(req);
566 * RECLAIM: Try to reclaim a lock
569 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
571 struct nlm_rqst reqst, *req;
575 memset(req, 0, sizeof(*req));
576 locks_init_lock(&req->a_args.lock.fl);
577 locks_init_lock(&req->a_res.lock.fl);
581 /* Set up the argument struct */
582 nlmclnt_setlockargs(req, fl);
583 req->a_args.reclaim = 1;
585 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
586 && req->a_res.status == NLM_LCK_GRANTED)
589 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
590 "(errno %d, status %d)\n", fl->fl_pid,
591 status, req->a_res.status);
594 * FIXME: This is a serious failure. We can
596 * a. Ignore the problem
597 * b. Send the owning process some signal (Linux doesn't have
598 * SIGLOST, though...)
599 * c. Retry the operation
601 * Until someone comes up with a simple implementation
602 * for b or c, I'll choose option a.
609 * UNLOCK: remove an existing lock
612 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
614 struct nlm_res *resp = &req->a_res;
618 * Note: the server is supposed to either grant us the unlock
619 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
620 * case, we want to unlock.
624 if (req->a_flags & RPC_TASK_ASYNC) {
625 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
626 &nlmclnt_unlock_ops);
627 /* Hrmf... Do the unlock early since locks_remove_posix()
628 * really expects us to free the lock synchronously */
630 nlmclnt_release_lockargs(req);
636 status = nlmclnt_call(req, NLMPROC_UNLOCK);
637 nlmclnt_release_lockargs(req);
641 if (resp->status == NLM_LCK_GRANTED)
644 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
645 printk("lockd: unexpected unlock status: %d\n", resp->status);
647 /* What to do now? I'm out of my depth... */
652 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
654 struct nlm_rqst *req = data;
655 int status = req->a_res.status;
657 if (RPC_ASSASSINATED(task))
660 if (task->tk_status < 0) {
661 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
664 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
665 rpc_delay(task, NLMCLNT_GRACE_WAIT);
668 if (status != NLM_LCK_GRANTED)
669 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
671 nlm_release_host(req->a_host);
672 nlmclnt_release_lockargs(req);
676 nlm_rebind_host(req->a_host);
678 rpc_restart_call(task);
681 static const struct rpc_call_ops nlmclnt_unlock_ops = {
682 .rpc_call_done = nlmclnt_unlock_callback,
686 * Cancel a blocked lock request.
687 * We always use an async RPC call for this in order not to hang a
688 * process that has been Ctrl-C'ed.
690 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
692 struct nlm_rqst *req;
697 /* Block all signals while setting up call */
698 spin_lock_irqsave(¤t->sighand->siglock, flags);
699 oldset = current->blocked;
700 sigfillset(¤t->blocked);
702 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
704 req = nlmclnt_alloc_call();
708 req->a_flags = RPC_TASK_ASYNC;
710 nlmclnt_setlockargs(req, fl);
711 req->a_args.block = block;
713 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
715 nlmclnt_release_lockargs(req);
719 spin_lock_irqsave(¤t->sighand->siglock, flags);
720 current->blocked = oldset;
722 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
727 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
729 struct nlm_rqst *req = data;
731 if (RPC_ASSASSINATED(task))
734 if (task->tk_status < 0) {
735 dprintk("lockd: CANCEL call error %d, retrying.\n",
740 dprintk("lockd: cancel status %d (task %d)\n",
741 req->a_res.status, task->tk_pid);
743 switch (req->a_res.status) {
744 case NLM_LCK_GRANTED:
745 case NLM_LCK_DENIED_GRACE_PERIOD:
747 /* Everything's good */
749 case NLM_LCK_DENIED_NOLOCKS:
750 dprintk("lockd: CANCEL failed (server has no locks)\n");
753 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
758 nlm_release_host(req->a_host);
759 nlmclnt_release_lockargs(req);
764 /* Don't ever retry more than 3 times */
765 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
767 nlm_rebind_host(req->a_host);
768 rpc_restart_call(task);
769 rpc_delay(task, 30 * HZ);
772 static const struct rpc_call_ops nlmclnt_cancel_ops = {
773 .rpc_call_done = nlmclnt_cancel_callback,
777 * Convert an NLM status code to a generic kernel errno
780 nlm_stat_to_errno(u32 status)
783 case NLM_LCK_GRANTED:
787 case NLM_LCK_DENIED_NOLOCKS:
788 case NLM_LCK_DENIED_GRACE_PERIOD:
790 case NLM_LCK_BLOCKED:
791 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
793 #ifdef CONFIG_LOCKD_V4
806 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);