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 u32 nlm_cookie = 0x1234;
41 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
43 memcpy(c->data, &nlm_cookie, 4);
44 memset(c->data+4, 0, 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 = (struct nlm_lockowner *)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 = system_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 system_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 nlm_host *host;
155 struct nlm_rqst *call;
158 int status, proto, vers;
160 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
161 if (NFS_PROTO(inode)->version > 3) {
162 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
166 /* Retrieve transport protocol from NFS client */
167 proto = NFS_CLIENT(inode)->cl_xprt->prot;
169 host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
173 call = nlm_alloc_call(host);
177 nlmclnt_locks_init_private(fl, host);
178 /* Set up the argument struct */
179 nlmclnt_setlockargs(call, fl);
181 /* Keep the old signal mask */
182 spin_lock_irqsave(¤t->sighand->siglock, flags);
183 oldset = current->blocked;
185 /* If we're cleaning up locks because the process is exiting,
186 * perform the RPC call asynchronously. */
187 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
188 && fl->fl_type == F_UNLCK
189 && (current->flags & PF_EXITING)) {
190 sigfillset(¤t->blocked); /* Mask all signals */
193 call->a_flags = RPC_TASK_ASYNC;
195 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
197 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
198 if (fl->fl_type != F_UNLCK) {
199 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
200 status = nlmclnt_lock(call, fl);
202 status = nlmclnt_unlock(call, fl);
203 } else if (IS_GETLK(cmd))
204 status = nlmclnt_test(call, fl);
208 fl->fl_ops->fl_release_private(fl);
211 spin_lock_irqsave(¤t->sighand->siglock, flags);
212 current->blocked = oldset;
214 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
216 dprintk("lockd: clnt proc returns %d\n", status);
219 EXPORT_SYMBOL(nlmclnt_proc);
222 * Allocate an NLM RPC call struct
224 * Note: the caller must hold a reference to host. In case of failure,
225 * this reference will be released.
227 struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
229 struct nlm_rqst *call;
232 call = kzalloc(sizeof(*call), GFP_KERNEL);
234 locks_init_lock(&call->a_args.lock.fl);
235 locks_init_lock(&call->a_res.lock.fl);
241 printk("nlm_alloc_call: failed, waiting for memory\n");
242 schedule_timeout_interruptible(5*HZ);
244 nlm_release_host(host);
248 void nlm_release_call(struct nlm_rqst *call)
250 nlm_release_host(call->a_host);
251 nlmclnt_release_lockargs(call);
255 static void nlmclnt_rpc_release(void *data)
257 return nlm_release_call(data);
260 static int nlm_wait_on_grace(wait_queue_head_t *queue)
265 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
267 schedule_timeout(NLMCLNT_GRACE_WAIT);
272 finish_wait(queue, &wait);
280 nlmclnt_call(struct nlm_rqst *req, u32 proc)
282 struct nlm_host *host = req->a_host;
283 struct rpc_clnt *clnt;
284 struct nlm_args *argp = &req->a_args;
285 struct nlm_res *resp = &req->a_res;
286 struct rpc_message msg = {
292 dprintk("lockd: call procedure %d on %s\n",
293 (int)proc, host->h_name);
296 if (host->h_reclaiming && !argp->reclaim)
297 goto in_grace_period;
299 /* If we have no RPC client yet, create one. */
300 if ((clnt = nlm_bind_host(host)) == NULL)
302 msg.rpc_proc = &clnt->cl_procinfo[proc];
304 /* Perform the RPC call. If an error occurs, try again */
305 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
306 dprintk("lockd: rpc_call returned error %d\n", -status);
308 case -EPROTONOSUPPORT:
314 nlm_rebind_host(host);
318 return signalled () ? -EINTR : status;
324 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
325 dprintk("lockd: server in grace period\n");
328 "lockd: spurious grace period reject?!\n");
332 if (!argp->reclaim) {
333 /* We appear to be out of the grace period */
334 wake_up_all(&host->h_gracewait);
336 dprintk("lockd: server returns status %d\n", resp->status);
337 return 0; /* Okay, call complete */
342 * The server has rebooted and appears to be in the grace
343 * period during which locks are only allowed to be
345 * We can only back off and try again later.
347 status = nlm_wait_on_grace(&host->h_gracewait);
348 } while (status == 0);
354 * Generic NLM call, async version.
356 static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
358 struct nlm_host *host = req->a_host;
359 struct rpc_clnt *clnt;
360 int status = -ENOLCK;
362 dprintk("lockd: call procedure %d on %s (async)\n",
363 (int)proc, host->h_name);
365 /* If we have no RPC client yet, create one. */
366 clnt = nlm_bind_host(host);
369 msg->rpc_proc = &clnt->cl_procinfo[proc];
371 /* bootstrap and kick off the async RPC call */
372 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
376 nlm_release_call(req);
380 int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
382 struct rpc_message msg = {
383 .rpc_argp = &req->a_args,
384 .rpc_resp = &req->a_res,
386 return __nlm_async_call(req, proc, &msg, tk_ops);
389 int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
391 struct rpc_message msg = {
392 .rpc_argp = &req->a_res,
394 return __nlm_async_call(req, proc, &msg, tk_ops);
398 * TEST for the presence of a conflicting lock
401 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
405 status = nlmclnt_call(req, NLMPROC_TEST);
409 switch (req->a_res.status) {
410 case NLM_LCK_GRANTED:
411 fl->fl_type = F_UNLCK;
415 * Report the conflicting lock back to the application.
417 fl->fl_start = req->a_res.lock.fl.fl_start;
418 fl->fl_end = req->a_res.lock.fl.fl_start;
419 fl->fl_type = req->a_res.lock.fl.fl_type;
423 status = nlm_stat_to_errno(req->a_res.status);
426 nlm_release_call(req);
430 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
432 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
433 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
434 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
437 static void nlmclnt_locks_release_private(struct file_lock *fl)
439 list_del(&fl->fl_u.nfs_fl.list);
440 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
443 static struct file_lock_operations nlmclnt_lock_ops = {
444 .fl_copy_lock = nlmclnt_locks_copy_lock,
445 .fl_release_private = nlmclnt_locks_release_private,
448 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
450 BUG_ON(fl->fl_ops != NULL);
451 fl->fl_u.nfs_fl.state = 0;
452 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
453 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
454 fl->fl_ops = &nlmclnt_lock_ops;
457 static int do_vfs_lock(struct file_lock *fl)
460 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
462 res = posix_lock_file_wait(fl->fl_file, fl);
465 res = flock_lock_file_wait(fl->fl_file, fl);
474 * LOCK: Try to create a lock
476 * Programmer Harassment Alert
478 * When given a blocking lock request in a sync RPC call, the HPUX lockd
479 * will faithfully return LCK_BLOCKED but never cares to notify us when
480 * the lock could be granted. This way, our local process could hang
481 * around forever waiting for the callback.
483 * Solution A: Implement busy-waiting
484 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
486 * For now I am implementing solution A, because I hate the idea of
487 * re-implementing lockd for a third time in two months. The async
488 * calls shouldn't be too hard to do, however.
490 * This is one of the lovely things about standards in the NFS area:
491 * they're so soft and squishy you can't really blame HP for doing this.
494 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
496 struct nlm_host *host = req->a_host;
497 struct nlm_res *resp = &req->a_res;
498 struct nlm_wait *block = NULL;
499 int status = -ENOLCK;
501 if (!host->h_monitored && nsm_monitor(host) < 0) {
502 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
507 block = nlmclnt_prepare_block(host, fl);
510 /* Reboot protection */
511 fl->fl_u.nfs_fl.state = host->h_state;
512 status = nlmclnt_call(req, NLMPROC_LOCK);
515 if (!req->a_args.block)
517 /* Did a reclaimer thread notify us of a server reboot? */
518 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
520 if (resp->status != NLM_LCK_BLOCKED)
522 /* Wait on an NLM blocking lock */
523 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
524 /* if we were interrupted. Send a CANCEL request to the server
529 if (resp->status != NLM_LCK_BLOCKED)
533 if (resp->status == NLM_LCK_GRANTED) {
534 down_read(&host->h_rwsem);
535 /* Check whether or not the server has rebooted */
536 if (fl->fl_u.nfs_fl.state != host->h_state) {
537 up_read(&host->h_rwsem);
540 fl->fl_flags |= FL_SLEEP;
541 /* Ensure the resulting lock will get added to granted list */
542 if (do_vfs_lock(fl) < 0)
543 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
544 up_read(&host->h_rwsem);
546 status = nlm_stat_to_errno(resp->status);
548 nlmclnt_finish_block(block);
549 /* Cancel the blocked request if it is still pending */
550 if (resp->status == NLM_LCK_BLOCKED)
551 nlmclnt_cancel(host, req->a_args.block, fl);
553 nlm_release_call(req);
558 * RECLAIM: Try to reclaim a lock
561 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
563 struct nlm_rqst reqst, *req;
567 memset(req, 0, sizeof(*req));
568 locks_init_lock(&req->a_args.lock.fl);
569 locks_init_lock(&req->a_res.lock.fl);
573 /* Set up the argument struct */
574 nlmclnt_setlockargs(req, fl);
575 req->a_args.reclaim = 1;
577 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
578 && req->a_res.status == NLM_LCK_GRANTED)
581 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
582 "(errno %d, status %d)\n", fl->fl_pid,
583 status, req->a_res.status);
586 * FIXME: This is a serious failure. We can
588 * a. Ignore the problem
589 * b. Send the owning process some signal (Linux doesn't have
590 * SIGLOST, though...)
591 * c. Retry the operation
593 * Until someone comes up with a simple implementation
594 * for b or c, I'll choose option a.
601 * UNLOCK: remove an existing lock
604 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
606 struct nlm_host *host = req->a_host;
607 struct nlm_res *resp = &req->a_res;
611 * Note: the server is supposed to either grant us the unlock
612 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
613 * case, we want to unlock.
615 fl->fl_flags |= FL_EXISTS;
616 down_read(&host->h_rwsem);
617 if (do_vfs_lock(fl) == -ENOENT) {
618 up_read(&host->h_rwsem);
621 up_read(&host->h_rwsem);
623 if (req->a_flags & RPC_TASK_ASYNC)
624 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
626 status = nlmclnt_call(req, NLMPROC_UNLOCK);
630 if (resp->status == NLM_LCK_GRANTED)
633 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
634 printk("lockd: unexpected unlock status: %d\n", resp->status);
635 /* What to do now? I'm out of my depth... */
638 nlm_release_call(req);
642 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
644 struct nlm_rqst *req = data;
645 int status = req->a_res.status;
647 if (RPC_ASSASSINATED(task))
650 if (task->tk_status < 0) {
651 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
654 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
655 rpc_delay(task, NLMCLNT_GRACE_WAIT);
658 if (status != NLM_LCK_GRANTED)
659 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
663 nlm_rebind_host(req->a_host);
665 rpc_restart_call(task);
668 static const struct rpc_call_ops nlmclnt_unlock_ops = {
669 .rpc_call_done = nlmclnt_unlock_callback,
670 .rpc_release = nlmclnt_rpc_release,
674 * Cancel a blocked lock request.
675 * We always use an async RPC call for this in order not to hang a
676 * process that has been Ctrl-C'ed.
678 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
680 struct nlm_rqst *req;
685 /* Block all signals while setting up call */
686 spin_lock_irqsave(¤t->sighand->siglock, flags);
687 oldset = current->blocked;
688 sigfillset(¤t->blocked);
690 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
692 req = nlm_alloc_call(nlm_get_host(host));
695 req->a_flags = RPC_TASK_ASYNC;
697 nlmclnt_setlockargs(req, fl);
698 req->a_args.block = block;
700 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
702 spin_lock_irqsave(¤t->sighand->siglock, flags);
703 current->blocked = oldset;
705 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
710 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
712 struct nlm_rqst *req = data;
714 if (RPC_ASSASSINATED(task))
717 if (task->tk_status < 0) {
718 dprintk("lockd: CANCEL call error %d, retrying.\n",
723 dprintk("lockd: cancel status %d (task %d)\n",
724 req->a_res.status, task->tk_pid);
726 switch (req->a_res.status) {
727 case NLM_LCK_GRANTED:
728 case NLM_LCK_DENIED_GRACE_PERIOD:
730 /* Everything's good */
732 case NLM_LCK_DENIED_NOLOCKS:
733 dprintk("lockd: CANCEL failed (server has no locks)\n");
736 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
744 /* Don't ever retry more than 3 times */
745 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
747 nlm_rebind_host(req->a_host);
748 rpc_restart_call(task);
749 rpc_delay(task, 30 * HZ);
752 static const struct rpc_call_ops nlmclnt_cancel_ops = {
753 .rpc_call_done = nlmclnt_cancel_callback,
754 .rpc_release = nlmclnt_rpc_release,
758 * Convert an NLM status code to a generic kernel errno
761 nlm_stat_to_errno(u32 status)
764 case NLM_LCK_GRANTED:
768 case NLM_LCK_DENIED_NOLOCKS:
769 case NLM_LCK_DENIED_GRACE_PERIOD:
771 case NLM_LCK_BLOCKED:
772 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
774 #ifdef CONFIG_LOCKD_V4
787 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);