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/freezer.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(__be32 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 atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
42 void nlmclnt_next_cookie(struct nlm_cookie *c)
44 u32 cookie = atomic_inc_return(&nlm_cookie);
46 memcpy(c->data, &cookie, 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 = 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_path.dentry->d_inode), sizeof(struct nfs_fh));
133 lock->caller = 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 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 rpc_clnt *client = NFS_CLIENT(inode);
156 struct sockaddr_in addr;
157 struct nfs_server *nfssrv = NFS_SERVER(inode);
158 struct nlm_host *host;
159 struct nlm_rqst *call;
164 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
165 if (NFS_PROTO(inode)->version > 3) {
166 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
170 rpc_peeraddr(client, (struct sockaddr *) &addr, sizeof(addr));
171 host = nlmclnt_lookup_host(&addr, client->cl_xprt->prot, vers,
172 nfssrv->nfs_client->cl_hostname,
173 strlen(nfssrv->nfs_client->cl_hostname));
177 call = nlm_alloc_call(host);
181 nlmclnt_locks_init_private(fl, host);
182 /* Set up the argument struct */
183 nlmclnt_setlockargs(call, fl);
185 /* Keep the old signal mask */
186 spin_lock_irqsave(¤t->sighand->siglock, flags);
187 oldset = current->blocked;
189 /* If we're cleaning up locks because the process is exiting,
190 * perform the RPC call asynchronously. */
191 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
192 && fl->fl_type == F_UNLCK
193 && (current->flags & PF_EXITING)) {
194 sigfillset(¤t->blocked); /* Mask all signals */
197 call->a_flags = RPC_TASK_ASYNC;
199 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
201 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
202 if (fl->fl_type != F_UNLCK) {
203 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
204 status = nlmclnt_lock(call, fl);
206 status = nlmclnt_unlock(call, fl);
207 } else if (IS_GETLK(cmd))
208 status = nlmclnt_test(call, fl);
212 fl->fl_ops->fl_release_private(fl);
215 spin_lock_irqsave(¤t->sighand->siglock, flags);
216 current->blocked = oldset;
218 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
220 dprintk("lockd: clnt proc returns %d\n", status);
223 EXPORT_SYMBOL(nlmclnt_proc);
226 * Allocate an NLM RPC call struct
228 * Note: the caller must hold a reference to host. In case of failure,
229 * this reference will be released.
231 struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
233 struct nlm_rqst *call;
236 call = kzalloc(sizeof(*call), GFP_KERNEL);
238 locks_init_lock(&call->a_args.lock.fl);
239 locks_init_lock(&call->a_res.lock.fl);
245 printk("nlm_alloc_call: failed, waiting for memory\n");
246 schedule_timeout_interruptible(5*HZ);
248 nlm_release_host(host);
252 void nlm_release_call(struct nlm_rqst *call)
254 nlm_release_host(call->a_host);
255 nlmclnt_release_lockargs(call);
259 static void nlmclnt_rpc_release(void *data)
261 return nlm_release_call(data);
264 static int nlm_wait_on_grace(wait_queue_head_t *queue)
269 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
271 schedule_timeout(NLMCLNT_GRACE_WAIT);
276 finish_wait(queue, &wait);
284 nlmclnt_call(struct nlm_rqst *req, u32 proc)
286 struct nlm_host *host = req->a_host;
287 struct rpc_clnt *clnt;
288 struct nlm_args *argp = &req->a_args;
289 struct nlm_res *resp = &req->a_res;
290 struct rpc_message msg = {
296 dprintk("lockd: call procedure %d on %s\n",
297 (int)proc, host->h_name);
300 if (host->h_reclaiming && !argp->reclaim)
301 goto in_grace_period;
303 /* If we have no RPC client yet, create one. */
304 if ((clnt = nlm_bind_host(host)) == NULL)
306 msg.rpc_proc = &clnt->cl_procinfo[proc];
308 /* Perform the RPC call. If an error occurs, try again */
309 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
310 dprintk("lockd: rpc_call returned error %d\n", -status);
312 case -EPROTONOSUPPORT:
318 nlm_rebind_host(host);
322 return signalled () ? -EINTR : status;
328 if (resp->status == nlm_lck_denied_grace_period) {
329 dprintk("lockd: server in grace period\n");
332 "lockd: spurious grace period reject?!\n");
336 if (!argp->reclaim) {
337 /* We appear to be out of the grace period */
338 wake_up_all(&host->h_gracewait);
340 dprintk("lockd: server returns status %d\n", resp->status);
341 return 0; /* Okay, call complete */
346 * The server has rebooted and appears to be in the grace
347 * period during which locks are only allowed to be
349 * We can only back off and try again later.
351 status = nlm_wait_on_grace(&host->h_gracewait);
352 } while (status == 0);
358 * Generic NLM call, async version.
360 static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
362 struct nlm_host *host = req->a_host;
363 struct rpc_clnt *clnt;
364 int status = -ENOLCK;
366 dprintk("lockd: call procedure %d on %s (async)\n",
367 (int)proc, host->h_name);
369 /* If we have no RPC client yet, create one. */
370 clnt = nlm_bind_host(host);
373 msg->rpc_proc = &clnt->cl_procinfo[proc];
375 /* bootstrap and kick off the async RPC call */
376 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
380 nlm_release_call(req);
384 int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
386 struct rpc_message msg = {
387 .rpc_argp = &req->a_args,
388 .rpc_resp = &req->a_res,
390 return __nlm_async_call(req, proc, &msg, tk_ops);
393 int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
395 struct rpc_message msg = {
396 .rpc_argp = &req->a_res,
398 return __nlm_async_call(req, proc, &msg, tk_ops);
402 * TEST for the presence of a conflicting lock
405 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
409 status = nlmclnt_call(req, NLMPROC_TEST);
413 switch (req->a_res.status) {
415 fl->fl_type = F_UNLCK;
419 * Report the conflicting lock back to the application.
421 fl->fl_start = req->a_res.lock.fl.fl_start;
422 fl->fl_end = req->a_res.lock.fl.fl_start;
423 fl->fl_type = req->a_res.lock.fl.fl_type;
427 status = nlm_stat_to_errno(req->a_res.status);
430 nlm_release_call(req);
434 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
436 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
437 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
438 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
441 static void nlmclnt_locks_release_private(struct file_lock *fl)
443 list_del(&fl->fl_u.nfs_fl.list);
444 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
447 static struct file_lock_operations nlmclnt_lock_ops = {
448 .fl_copy_lock = nlmclnt_locks_copy_lock,
449 .fl_release_private = nlmclnt_locks_release_private,
452 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
454 BUG_ON(fl->fl_ops != NULL);
455 fl->fl_u.nfs_fl.state = 0;
456 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
457 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
458 fl->fl_ops = &nlmclnt_lock_ops;
461 static int do_vfs_lock(struct file_lock *fl)
464 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
466 res = posix_lock_file_wait(fl->fl_file, fl);
469 res = flock_lock_file_wait(fl->fl_file, fl);
478 * LOCK: Try to create a lock
480 * Programmer Harassment Alert
482 * When given a blocking lock request in a sync RPC call, the HPUX lockd
483 * will faithfully return LCK_BLOCKED but never cares to notify us when
484 * the lock could be granted. This way, our local process could hang
485 * around forever waiting for the callback.
487 * Solution A: Implement busy-waiting
488 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
490 * For now I am implementing solution A, because I hate the idea of
491 * re-implementing lockd for a third time in two months. The async
492 * calls shouldn't be too hard to do, however.
494 * This is one of the lovely things about standards in the NFS area:
495 * they're so soft and squishy you can't really blame HP for doing this.
498 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
500 struct nlm_host *host = req->a_host;
501 struct nlm_res *resp = &req->a_res;
502 struct nlm_wait *block = NULL;
503 unsigned char fl_flags = fl->fl_flags;
504 int status = -ENOLCK;
506 if (nsm_monitor(host) < 0) {
507 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
511 fl->fl_flags |= FL_ACCESS;
512 status = do_vfs_lock(fl);
516 block = nlmclnt_prepare_block(host, fl);
519 /* Reboot protection */
520 fl->fl_u.nfs_fl.state = host->h_state;
521 status = nlmclnt_call(req, NLMPROC_LOCK);
524 if (!req->a_args.block)
526 /* Did a reclaimer thread notify us of a server reboot? */
527 if (resp->status == nlm_lck_denied_grace_period)
529 if (resp->status != nlm_lck_blocked)
531 /* Wait on an NLM blocking lock */
532 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
533 /* if we were interrupted. Send a CANCEL request to the server
538 if (resp->status != nlm_lck_blocked)
542 if (resp->status == nlm_granted) {
543 down_read(&host->h_rwsem);
544 /* Check whether or not the server has rebooted */
545 if (fl->fl_u.nfs_fl.state != host->h_state) {
546 up_read(&host->h_rwsem);
549 /* Ensure the resulting lock will get added to granted list */
550 fl->fl_flags = fl_flags | FL_SLEEP;
551 if (do_vfs_lock(fl) < 0)
552 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
553 up_read(&host->h_rwsem);
555 status = nlm_stat_to_errno(resp->status);
557 nlmclnt_finish_block(block);
558 /* Cancel the blocked request if it is still pending */
559 if (resp->status == nlm_lck_blocked)
560 nlmclnt_cancel(host, req->a_args.block, fl);
562 nlm_release_call(req);
563 fl->fl_flags = fl_flags;
568 * RECLAIM: Try to reclaim a lock
571 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
573 struct nlm_rqst reqst, *req;
577 memset(req, 0, sizeof(*req));
578 locks_init_lock(&req->a_args.lock.fl);
579 locks_init_lock(&req->a_res.lock.fl);
583 /* Set up the argument struct */
584 nlmclnt_setlockargs(req, fl);
585 req->a_args.reclaim = 1;
587 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
588 && req->a_res.status == nlm_granted)
591 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
592 "(errno %d, status %d)\n", fl->fl_pid,
593 status, ntohl(req->a_res.status));
596 * FIXME: This is a serious failure. We can
598 * a. Ignore the problem
599 * b. Send the owning process some signal (Linux doesn't have
600 * SIGLOST, though...)
601 * c. Retry the operation
603 * Until someone comes up with a simple implementation
604 * for b or c, I'll choose option a.
611 * UNLOCK: remove an existing lock
614 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
616 struct nlm_host *host = req->a_host;
617 struct nlm_res *resp = &req->a_res;
621 * Note: the server is supposed to either grant us the unlock
622 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
623 * case, we want to unlock.
625 fl->fl_flags |= FL_EXISTS;
626 down_read(&host->h_rwsem);
627 if (do_vfs_lock(fl) == -ENOENT) {
628 up_read(&host->h_rwsem);
631 up_read(&host->h_rwsem);
633 if (req->a_flags & RPC_TASK_ASYNC)
634 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
636 status = nlmclnt_call(req, NLMPROC_UNLOCK);
640 if (resp->status == nlm_granted)
643 if (resp->status != nlm_lck_denied_nolocks)
644 printk("lockd: unexpected unlock status: %d\n", resp->status);
645 /* What to do now? I'm out of my depth... */
648 nlm_release_call(req);
652 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
654 struct nlm_rqst *req = data;
655 u32 status = ntohl(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);
673 nlm_rebind_host(req->a_host);
675 rpc_restart_call(task);
678 static const struct rpc_call_ops nlmclnt_unlock_ops = {
679 .rpc_call_done = nlmclnt_unlock_callback,
680 .rpc_release = nlmclnt_rpc_release,
684 * Cancel a blocked lock request.
685 * We always use an async RPC call for this in order not to hang a
686 * process that has been Ctrl-C'ed.
688 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
690 struct nlm_rqst *req;
695 /* Block all signals while setting up call */
696 spin_lock_irqsave(¤t->sighand->siglock, flags);
697 oldset = current->blocked;
698 sigfillset(¤t->blocked);
700 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
702 req = nlm_alloc_call(nlm_get_host(host));
705 req->a_flags = RPC_TASK_ASYNC;
707 nlmclnt_setlockargs(req, fl);
708 req->a_args.block = block;
710 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
712 spin_lock_irqsave(¤t->sighand->siglock, flags);
713 current->blocked = oldset;
715 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
720 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
722 struct nlm_rqst *req = data;
723 u32 status = ntohl(req->a_res.status);
725 if (RPC_ASSASSINATED(task))
728 if (task->tk_status < 0) {
729 dprintk("lockd: CANCEL call error %d, retrying.\n",
734 dprintk("lockd: cancel status %u (task %u)\n",
735 status, task->tk_pid);
738 case NLM_LCK_GRANTED:
739 case NLM_LCK_DENIED_GRACE_PERIOD:
741 /* Everything's good */
743 case NLM_LCK_DENIED_NOLOCKS:
744 dprintk("lockd: CANCEL failed (server has no locks)\n");
747 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
755 /* Don't ever retry more than 3 times */
756 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
758 nlm_rebind_host(req->a_host);
759 rpc_restart_call(task);
760 rpc_delay(task, 30 * HZ);
763 static const struct rpc_call_ops nlmclnt_cancel_ops = {
764 .rpc_call_done = nlmclnt_cancel_callback,
765 .rpc_release = nlmclnt_rpc_release,
769 * Convert an NLM status code to a generic kernel errno
772 nlm_stat_to_errno(__be32 status)
774 switch(ntohl(status)) {
775 case NLM_LCK_GRANTED:
779 case NLM_LCK_DENIED_NOLOCKS:
780 case NLM_LCK_DENIED_GRACE_PERIOD:
782 case NLM_LCK_BLOCKED:
783 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
785 #ifdef CONFIG_LOCKD_V4
798 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);