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/module.h>
10 #include <linux/types.h>
11 #include <linux/errno.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)
23 #define NLMCLNT_POLL_TIMEOUT (30*HZ)
24 #define NLMCLNT_MAX_RETRIES 3
26 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
27 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
28 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
29 static int nlm_stat_to_errno(u32 stat);
30 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
31 static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
33 static const struct rpc_call_ops nlmclnt_unlock_ops;
34 static const struct rpc_call_ops nlmclnt_cancel_ops;
37 * Cookie counter for NLM requests
39 static atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
41 void nlmclnt_next_cookie(struct nlm_cookie *c)
43 u32 cookie = atomic_inc_return(&nlm_cookie);
45 memcpy(c->data, &cookie, 4);
49 static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
51 atomic_inc(&lockowner->count);
55 static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
57 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
59 list_del(&lockowner->list);
60 spin_unlock(&lockowner->host->h_lock);
61 nlm_release_host(lockowner->host);
65 static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
67 struct nlm_lockowner *lockowner;
68 list_for_each_entry(lockowner, &host->h_lockowners, list) {
69 if (lockowner->pid == pid)
75 static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
79 res = host->h_pidcount++;
80 } while (nlm_pidbusy(host, res) < 0);
84 static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
86 struct nlm_lockowner *lockowner;
87 list_for_each_entry(lockowner, &host->h_lockowners, list) {
88 if (lockowner->owner != owner)
90 return nlm_get_lockowner(lockowner);
95 static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
97 struct nlm_lockowner *res, *new = NULL;
99 spin_lock(&host->h_lock);
100 res = __nlm_find_lockowner(host, owner);
102 spin_unlock(&host->h_lock);
103 new = kmalloc(sizeof(*new), GFP_KERNEL);
104 spin_lock(&host->h_lock);
105 res = __nlm_find_lockowner(host, owner);
106 if (res == NULL && new != NULL) {
108 atomic_set(&new->count, 1);
110 new->pid = __nlm_alloc_pid(host);
111 new->host = nlm_get_host(host);
112 list_add(&new->list, &host->h_lockowners);
116 spin_unlock(&host->h_lock);
122 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
124 static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
126 struct nlm_args *argp = &req->a_args;
127 struct nlm_lock *lock = &argp->lock;
129 nlmclnt_next_cookie(&argp->cookie);
130 argp->state = nsm_local_state;
131 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
132 lock->caller = utsname()->nodename;
133 lock->oh.data = req->a_owner;
134 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
135 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
136 utsname()->nodename);
137 lock->svid = fl->fl_u.nfs_fl.owner->pid;
138 lock->fl.fl_start = fl->fl_start;
139 lock->fl.fl_end = fl->fl_end;
140 lock->fl.fl_type = fl->fl_type;
143 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
145 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
149 * This is the main entry point for the NLM client.
152 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
154 struct rpc_clnt *client = NFS_CLIENT(inode);
155 struct sockaddr_in addr;
156 struct nfs_server *nfssrv = NFS_SERVER(inode);
157 struct nlm_host *host;
158 struct nlm_rqst *call;
163 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
164 if (NFS_PROTO(inode)->version > 3) {
165 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
169 rpc_peeraddr(client, (struct sockaddr *) &addr, sizeof(addr));
170 host = nlmclnt_lookup_host(&addr, client->cl_xprt->prot, vers,
171 nfssrv->nfs_client->cl_hostname,
172 strlen(nfssrv->nfs_client->cl_hostname));
176 call = nlm_alloc_call(host);
180 nlmclnt_locks_init_private(fl, host);
181 /* Set up the argument struct */
182 nlmclnt_setlockargs(call, fl);
184 /* Keep the old signal mask */
185 spin_lock_irqsave(¤t->sighand->siglock, flags);
186 oldset = current->blocked;
188 /* If we're cleaning up locks because the process is exiting,
189 * perform the RPC call asynchronously. */
190 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
191 && fl->fl_type == F_UNLCK
192 && (current->flags & PF_EXITING)) {
193 sigfillset(¤t->blocked); /* Mask all signals */
196 call->a_flags = RPC_TASK_ASYNC;
198 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
200 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
201 if (fl->fl_type != F_UNLCK) {
202 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
203 status = nlmclnt_lock(call, fl);
205 status = nlmclnt_unlock(call, fl);
206 } else if (IS_GETLK(cmd))
207 status = nlmclnt_test(call, fl);
211 fl->fl_ops->fl_release_private(fl);
214 spin_lock_irqsave(¤t->sighand->siglock, flags);
215 current->blocked = oldset;
217 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
219 dprintk("lockd: clnt proc returns %d\n", status);
222 EXPORT_SYMBOL(nlmclnt_proc);
225 * Allocate an NLM RPC call struct
227 * Note: the caller must hold a reference to host. In case of failure,
228 * this reference will be released.
230 struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
232 struct nlm_rqst *call;
235 call = kzalloc(sizeof(*call), GFP_KERNEL);
237 locks_init_lock(&call->a_args.lock.fl);
238 locks_init_lock(&call->a_res.lock.fl);
244 printk("nlm_alloc_call: failed, waiting for memory\n");
245 schedule_timeout_interruptible(5*HZ);
247 nlm_release_host(host);
251 void nlm_release_call(struct nlm_rqst *call)
253 nlm_release_host(call->a_host);
254 nlmclnt_release_lockargs(call);
258 static void nlmclnt_rpc_release(void *data)
260 return nlm_release_call(data);
263 static int nlm_wait_on_grace(wait_queue_head_t *queue)
268 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
270 schedule_timeout(NLMCLNT_GRACE_WAIT);
275 finish_wait(queue, &wait);
283 nlmclnt_call(struct nlm_rqst *req, u32 proc)
285 struct nlm_host *host = req->a_host;
286 struct rpc_clnt *clnt;
287 struct nlm_args *argp = &req->a_args;
288 struct nlm_res *resp = &req->a_res;
289 struct rpc_message msg = {
295 dprintk("lockd: call procedure %d on %s\n",
296 (int)proc, host->h_name);
299 if (host->h_reclaiming && !argp->reclaim)
300 goto in_grace_period;
302 /* If we have no RPC client yet, create one. */
303 if ((clnt = nlm_bind_host(host)) == NULL)
305 msg.rpc_proc = &clnt->cl_procinfo[proc];
307 /* Perform the RPC call. If an error occurs, try again */
308 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
309 dprintk("lockd: rpc_call returned error %d\n", -status);
311 case -EPROTONOSUPPORT:
317 nlm_rebind_host(host);
321 return signalled () ? -EINTR : status;
327 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
328 dprintk("lockd: server in grace period\n");
331 "lockd: spurious grace period reject?!\n");
335 if (!argp->reclaim) {
336 /* We appear to be out of the grace period */
337 wake_up_all(&host->h_gracewait);
339 dprintk("lockd: server returns status %d\n", resp->status);
340 return 0; /* Okay, call complete */
345 * The server has rebooted and appears to be in the grace
346 * period during which locks are only allowed to be
348 * We can only back off and try again later.
350 status = nlm_wait_on_grace(&host->h_gracewait);
351 } while (status == 0);
357 * Generic NLM call, async version.
359 static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
361 struct nlm_host *host = req->a_host;
362 struct rpc_clnt *clnt;
363 int status = -ENOLCK;
365 dprintk("lockd: call procedure %d on %s (async)\n",
366 (int)proc, host->h_name);
368 /* If we have no RPC client yet, create one. */
369 clnt = nlm_bind_host(host);
372 msg->rpc_proc = &clnt->cl_procinfo[proc];
374 /* bootstrap and kick off the async RPC call */
375 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
379 nlm_release_call(req);
383 int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
385 struct rpc_message msg = {
386 .rpc_argp = &req->a_args,
387 .rpc_resp = &req->a_res,
389 return __nlm_async_call(req, proc, &msg, tk_ops);
392 int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
394 struct rpc_message msg = {
395 .rpc_argp = &req->a_res,
397 return __nlm_async_call(req, proc, &msg, tk_ops);
401 * TEST for the presence of a conflicting lock
404 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
408 status = nlmclnt_call(req, NLMPROC_TEST);
412 switch (req->a_res.status) {
413 case NLM_LCK_GRANTED:
414 fl->fl_type = F_UNLCK;
418 * Report the conflicting lock back to the application.
420 fl->fl_start = req->a_res.lock.fl.fl_start;
421 fl->fl_end = req->a_res.lock.fl.fl_start;
422 fl->fl_type = req->a_res.lock.fl.fl_type;
426 status = nlm_stat_to_errno(req->a_res.status);
429 nlm_release_call(req);
433 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
435 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
436 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
437 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
440 static void nlmclnt_locks_release_private(struct file_lock *fl)
442 list_del(&fl->fl_u.nfs_fl.list);
443 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
446 static struct file_lock_operations nlmclnt_lock_ops = {
447 .fl_copy_lock = nlmclnt_locks_copy_lock,
448 .fl_release_private = nlmclnt_locks_release_private,
451 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
453 BUG_ON(fl->fl_ops != NULL);
454 fl->fl_u.nfs_fl.state = 0;
455 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
456 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
457 fl->fl_ops = &nlmclnt_lock_ops;
460 static int do_vfs_lock(struct file_lock *fl)
463 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
465 res = posix_lock_file_wait(fl->fl_file, fl);
468 res = flock_lock_file_wait(fl->fl_file, fl);
477 * LOCK: Try to create a lock
479 * Programmer Harassment Alert
481 * When given a blocking lock request in a sync RPC call, the HPUX lockd
482 * will faithfully return LCK_BLOCKED but never cares to notify us when
483 * the lock could be granted. This way, our local process could hang
484 * around forever waiting for the callback.
486 * Solution A: Implement busy-waiting
487 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
489 * For now I am implementing solution A, because I hate the idea of
490 * re-implementing lockd for a third time in two months. The async
491 * calls shouldn't be too hard to do, however.
493 * This is one of the lovely things about standards in the NFS area:
494 * they're so soft and squishy you can't really blame HP for doing this.
497 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
499 struct nlm_host *host = req->a_host;
500 struct nlm_res *resp = &req->a_res;
501 struct nlm_wait *block = NULL;
502 unsigned char fl_flags = fl->fl_flags;
503 int status = -ENOLCK;
505 if (nsm_monitor(host) < 0) {
506 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
510 fl->fl_flags |= FL_ACCESS;
511 status = do_vfs_lock(fl);
515 block = nlmclnt_prepare_block(host, fl);
518 /* Reboot protection */
519 fl->fl_u.nfs_fl.state = host->h_state;
520 status = nlmclnt_call(req, NLMPROC_LOCK);
523 if (!req->a_args.block)
525 /* Did a reclaimer thread notify us of a server reboot? */
526 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
528 if (resp->status != NLM_LCK_BLOCKED)
530 /* Wait on an NLM blocking lock */
531 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
532 /* if we were interrupted. Send a CANCEL request to the server
537 if (resp->status != NLM_LCK_BLOCKED)
541 if (resp->status == NLM_LCK_GRANTED) {
542 down_read(&host->h_rwsem);
543 /* Check whether or not the server has rebooted */
544 if (fl->fl_u.nfs_fl.state != host->h_state) {
545 up_read(&host->h_rwsem);
548 /* Ensure the resulting lock will get added to granted list */
549 fl->fl_flags = fl_flags | FL_SLEEP;
550 if (do_vfs_lock(fl) < 0)
551 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
552 up_read(&host->h_rwsem);
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 nlm_release_call(req);
562 fl->fl_flags = fl_flags;
567 * RECLAIM: Try to reclaim a lock
570 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
572 struct nlm_rqst reqst, *req;
576 memset(req, 0, sizeof(*req));
577 locks_init_lock(&req->a_args.lock.fl);
578 locks_init_lock(&req->a_res.lock.fl);
582 /* Set up the argument struct */
583 nlmclnt_setlockargs(req, fl);
584 req->a_args.reclaim = 1;
586 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
587 && req->a_res.status == NLM_LCK_GRANTED)
590 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
591 "(errno %d, status %d)\n", fl->fl_pid,
592 status, req->a_res.status);
595 * FIXME: This is a serious failure. We can
597 * a. Ignore the problem
598 * b. Send the owning process some signal (Linux doesn't have
599 * SIGLOST, though...)
600 * c. Retry the operation
602 * Until someone comes up with a simple implementation
603 * for b or c, I'll choose option a.
610 * UNLOCK: remove an existing lock
613 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
615 struct nlm_host *host = req->a_host;
616 struct nlm_res *resp = &req->a_res;
620 * Note: the server is supposed to either grant us the unlock
621 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
622 * case, we want to unlock.
624 fl->fl_flags |= FL_EXISTS;
625 down_read(&host->h_rwsem);
626 if (do_vfs_lock(fl) == -ENOENT) {
627 up_read(&host->h_rwsem);
630 up_read(&host->h_rwsem);
632 if (req->a_flags & RPC_TASK_ASYNC)
633 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
635 status = nlmclnt_call(req, NLMPROC_UNLOCK);
639 if (resp->status == NLM_LCK_GRANTED)
642 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
643 printk("lockd: unexpected unlock status: %d\n", resp->status);
644 /* What to do now? I'm out of my depth... */
647 nlm_release_call(req);
651 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
653 struct nlm_rqst *req = data;
654 int status = req->a_res.status;
656 if (RPC_ASSASSINATED(task))
659 if (task->tk_status < 0) {
660 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
663 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
664 rpc_delay(task, NLMCLNT_GRACE_WAIT);
667 if (status != NLM_LCK_GRANTED)
668 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
672 nlm_rebind_host(req->a_host);
674 rpc_restart_call(task);
677 static const struct rpc_call_ops nlmclnt_unlock_ops = {
678 .rpc_call_done = nlmclnt_unlock_callback,
679 .rpc_release = nlmclnt_rpc_release,
683 * Cancel a blocked lock request.
684 * We always use an async RPC call for this in order not to hang a
685 * process that has been Ctrl-C'ed.
687 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
689 struct nlm_rqst *req;
694 /* Block all signals while setting up call */
695 spin_lock_irqsave(¤t->sighand->siglock, flags);
696 oldset = current->blocked;
697 sigfillset(¤t->blocked);
699 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
701 req = nlm_alloc_call(nlm_get_host(host));
704 req->a_flags = RPC_TASK_ASYNC;
706 nlmclnt_setlockargs(req, fl);
707 req->a_args.block = block;
709 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
711 spin_lock_irqsave(¤t->sighand->siglock, flags);
712 current->blocked = oldset;
714 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
719 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
721 struct nlm_rqst *req = data;
723 if (RPC_ASSASSINATED(task))
726 if (task->tk_status < 0) {
727 dprintk("lockd: CANCEL call error %d, retrying.\n",
732 dprintk("lockd: cancel status %u (task %u)\n",
733 req->a_res.status, task->tk_pid);
735 switch (req->a_res.status) {
736 case NLM_LCK_GRANTED:
737 case NLM_LCK_DENIED_GRACE_PERIOD:
739 /* Everything's good */
741 case NLM_LCK_DENIED_NOLOCKS:
742 dprintk("lockd: CANCEL failed (server has no locks)\n");
745 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
753 /* Don't ever retry more than 3 times */
754 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
756 nlm_rebind_host(req->a_host);
757 rpc_restart_call(task);
758 rpc_delay(task, 30 * HZ);
761 static const struct rpc_call_ops nlmclnt_cancel_ops = {
762 .rpc_call_done = nlmclnt_cancel_callback,
763 .rpc_release = nlmclnt_rpc_release,
767 * Convert an NLM status code to a generic kernel errno
770 nlm_stat_to_errno(u32 status)
773 case NLM_LCK_GRANTED:
777 case NLM_LCK_DENIED_NOLOCKS:
778 case NLM_LCK_DENIED_GRACE_PERIOD:
780 case NLM_LCK_BLOCKED:
781 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
783 #ifdef CONFIG_LOCKD_V4
796 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);