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 locks_copy_lock(&lock->fl, fl);
142 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
144 struct file_lock *fl = &req->a_args.lock.fl;
146 if (fl->fl_ops && fl->fl_ops->fl_release_private)
147 fl->fl_ops->fl_release_private(fl);
151 * This is the main entry point for the NLM client.
154 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
156 struct nfs_server *nfssrv = NFS_SERVER(inode);
157 struct nlm_host *host;
158 struct nlm_rqst reqst, *call = &reqst;
161 int status, proto, vers;
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 /* Retrieve transport protocol from NFS client */
170 proto = NFS_CLIENT(inode)->cl_xprt->prot;
172 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
175 /* Create RPC client handle if not there, and copy soft
176 * and intr flags from NFS client. */
177 if (host->h_rpcclnt == NULL) {
178 struct rpc_clnt *clnt;
180 /* Bind an rpc client to this host handle (does not
181 * perform a portmapper lookup) */
182 if (!(clnt = nlm_bind_host(host))) {
186 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
187 clnt->cl_intr = nfssrv->client->cl_intr;
190 /* Keep the old signal mask */
191 spin_lock_irqsave(¤t->sighand->siglock, flags);
192 oldset = current->blocked;
194 /* If we're cleaning up locks because the process is exiting,
195 * perform the RPC call asynchronously. */
196 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
197 && fl->fl_type == F_UNLCK
198 && (current->flags & PF_EXITING)) {
199 sigfillset(¤t->blocked); /* Mask all signals */
201 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
203 call = nlmclnt_alloc_call();
208 call->a_flags = RPC_TASK_ASYNC;
210 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
211 memset(call, 0, sizeof(*call));
212 locks_init_lock(&call->a_args.lock.fl);
213 locks_init_lock(&call->a_res.lock.fl);
217 nlmclnt_locks_init_private(fl, host);
219 /* Set up the argument struct */
220 nlmclnt_setlockargs(call, fl);
222 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
223 if (fl->fl_type != F_UNLCK) {
224 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
225 status = nlmclnt_lock(call, fl);
227 status = nlmclnt_unlock(call, fl);
228 } else if (IS_GETLK(cmd))
229 status = nlmclnt_test(call, fl);
234 spin_lock_irqsave(¤t->sighand->siglock, flags);
235 current->blocked = oldset;
237 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
240 dprintk("lockd: clnt proc returns %d\n", status);
241 nlm_release_host(host);
244 EXPORT_SYMBOL(nlmclnt_proc);
247 * Allocate an NLM RPC call struct
250 nlmclnt_alloc_call(void)
252 struct nlm_rqst *call;
255 call = kzalloc(sizeof(*call), GFP_KERNEL);
257 locks_init_lock(&call->a_args.lock.fl);
258 locks_init_lock(&call->a_res.lock.fl);
263 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
264 schedule_timeout_interruptible(5*HZ);
269 static int nlm_wait_on_grace(wait_queue_head_t *queue)
274 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
276 schedule_timeout(NLMCLNT_GRACE_WAIT);
281 finish_wait(queue, &wait);
289 nlmclnt_call(struct nlm_rqst *req, u32 proc)
291 struct nlm_host *host = req->a_host;
292 struct rpc_clnt *clnt;
293 struct nlm_args *argp = &req->a_args;
294 struct nlm_res *resp = &req->a_res;
295 struct rpc_message msg = {
301 dprintk("lockd: call procedure %d on %s\n",
302 (int)proc, host->h_name);
305 if (host->h_reclaiming && !argp->reclaim)
306 goto in_grace_period;
308 /* If we have no RPC client yet, create one. */
309 if ((clnt = nlm_bind_host(host)) == NULL)
311 msg.rpc_proc = &clnt->cl_procinfo[proc];
313 /* Perform the RPC call. If an error occurs, try again */
314 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
315 dprintk("lockd: rpc_call returned error %d\n", -status);
317 case -EPROTONOSUPPORT:
323 nlm_rebind_host(host);
327 return signalled () ? -EINTR : status;
333 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
334 dprintk("lockd: server in grace period\n");
337 "lockd: spurious grace period reject?!\n");
341 if (!argp->reclaim) {
342 /* We appear to be out of the grace period */
343 wake_up_all(&host->h_gracewait);
345 dprintk("lockd: server returns status %d\n", resp->status);
346 return 0; /* Okay, call complete */
351 * The server has rebooted and appears to be in the grace
352 * period during which locks are only allowed to be
354 * We can only back off and try again later.
356 status = nlm_wait_on_grace(&host->h_gracewait);
357 } while (status == 0);
363 * Generic NLM call, async version.
365 int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
367 struct nlm_host *host = req->a_host;
368 struct rpc_clnt *clnt;
369 struct rpc_message msg = {
370 .rpc_argp = &req->a_args,
371 .rpc_resp = &req->a_res,
375 dprintk("lockd: call procedure %d on %s (async)\n",
376 (int)proc, host->h_name);
378 /* If we have no RPC client yet, create one. */
379 if ((clnt = nlm_bind_host(host)) == NULL)
381 msg.rpc_proc = &clnt->cl_procinfo[proc];
383 /* bootstrap and kick off the async RPC call */
384 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
389 static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
391 struct nlm_host *host = req->a_host;
392 struct rpc_clnt *clnt;
393 struct nlm_args *argp = &req->a_args;
394 struct nlm_res *resp = &req->a_res;
395 struct rpc_message msg = {
401 dprintk("lockd: call procedure %d on %s (async)\n",
402 (int)proc, host->h_name);
404 /* If we have no RPC client yet, create one. */
405 if ((clnt = nlm_bind_host(host)) == NULL)
407 msg.rpc_proc = &clnt->cl_procinfo[proc];
409 /* Increment host refcount */
411 /* bootstrap and kick off the async RPC call */
412 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
414 nlm_release_host(host);
419 * TEST for the presence of a conflicting lock
422 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
426 status = nlmclnt_call(req, NLMPROC_TEST);
427 nlmclnt_release_lockargs(req);
431 status = req->a_res.status;
432 if (status == NLM_LCK_GRANTED) {
433 fl->fl_type = F_UNLCK;
434 } if (status == NLM_LCK_DENIED) {
436 * Report the conflicting lock back to the application.
438 locks_copy_lock(fl, &req->a_res.lock.fl);
441 return nlm_stat_to_errno(req->a_res.status);
447 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
449 memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
450 nlm_get_lockowner(new->fl_u.nfs_fl.owner);
453 static void nlmclnt_locks_release_private(struct file_lock *fl)
455 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.flags = 0;
469 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
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;
519 if (!host->h_monitored && nsm_monitor(host) < 0) {
520 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
526 if (req->a_args.block) {
527 status = nlmclnt_prepare_block(req, host, fl);
532 status = nlmclnt_call(req, NLMPROC_LOCK);
535 if (resp->status != NLM_LCK_BLOCKED)
537 /* Wait on an NLM blocking lock */
538 timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
539 /* Did a reclaimer thread notify us of a server reboot? */
540 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
542 if (resp->status != NLM_LCK_BLOCKED)
546 /* We were interrupted. Send a CANCEL request to the server
549 status = (int)timeout;
553 if (resp->status == NLM_LCK_GRANTED) {
554 fl->fl_u.nfs_fl.state = host->h_state;
555 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
556 fl->fl_flags |= FL_SLEEP;
559 status = nlm_stat_to_errno(resp->status);
561 nlmclnt_finish_block(req);
562 /* Cancel the blocked request if it is still pending */
563 if (resp->status == NLM_LCK_BLOCKED)
564 nlmclnt_cancel(host, req->a_args.block, fl);
566 nlmclnt_release_lockargs(req);
571 * RECLAIM: Try to reclaim a lock
574 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
576 struct nlm_rqst reqst, *req;
580 memset(req, 0, sizeof(*req));
581 locks_init_lock(&req->a_args.lock.fl);
582 locks_init_lock(&req->a_res.lock.fl);
586 /* Set up the argument struct */
587 nlmclnt_setlockargs(req, fl);
588 req->a_args.reclaim = 1;
590 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
591 && req->a_res.status == NLM_LCK_GRANTED)
594 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
595 "(errno %d, status %d)\n", fl->fl_pid,
596 status, req->a_res.status);
599 * FIXME: This is a serious failure. We can
601 * a. Ignore the problem
602 * b. Send the owning process some signal (Linux doesn't have
603 * SIGLOST, though...)
604 * c. Retry the operation
606 * Until someone comes up with a simple implementation
607 * for b or c, I'll choose option a.
614 * UNLOCK: remove an existing lock
617 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
619 struct nlm_res *resp = &req->a_res;
622 /* Clean the GRANTED flag now so the lock doesn't get
623 * reclaimed while we're stuck in the unlock call. */
624 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
627 * Note: the server is supposed to either grant us the unlock
628 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
629 * case, we want to unlock.
633 if (req->a_flags & RPC_TASK_ASYNC) {
634 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
635 &nlmclnt_unlock_ops);
636 /* Hrmf... Do the unlock early since locks_remove_posix()
637 * really expects us to free the lock synchronously */
639 nlmclnt_release_lockargs(req);
645 status = nlmclnt_call(req, NLMPROC_UNLOCK);
646 nlmclnt_release_lockargs(req);
650 if (resp->status == NLM_LCK_GRANTED)
653 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
654 printk("lockd: unexpected unlock status: %d\n", resp->status);
656 /* What to do now? I'm out of my depth... */
661 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
663 struct nlm_rqst *req = data;
664 int status = req->a_res.status;
666 if (RPC_ASSASSINATED(task))
669 if (task->tk_status < 0) {
670 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
673 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
674 rpc_delay(task, NLMCLNT_GRACE_WAIT);
677 if (status != NLM_LCK_GRANTED)
678 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
680 nlm_release_host(req->a_host);
681 nlmclnt_release_lockargs(req);
685 nlm_rebind_host(req->a_host);
687 rpc_restart_call(task);
690 static const struct rpc_call_ops nlmclnt_unlock_ops = {
691 .rpc_call_done = nlmclnt_unlock_callback,
695 * Cancel a blocked lock request.
696 * We always use an async RPC call for this in order not to hang a
697 * process that has been Ctrl-C'ed.
699 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
701 struct nlm_rqst *req;
706 /* Block all signals while setting up call */
707 spin_lock_irqsave(¤t->sighand->siglock, flags);
708 oldset = current->blocked;
709 sigfillset(¤t->blocked);
711 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
713 req = nlmclnt_alloc_call();
717 req->a_flags = RPC_TASK_ASYNC;
719 nlmclnt_setlockargs(req, fl);
720 req->a_args.block = block;
722 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
724 nlmclnt_release_lockargs(req);
728 spin_lock_irqsave(¤t->sighand->siglock, flags);
729 current->blocked = oldset;
731 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
736 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
738 struct nlm_rqst *req = data;
740 if (RPC_ASSASSINATED(task))
743 if (task->tk_status < 0) {
744 dprintk("lockd: CANCEL call error %d, retrying.\n",
749 dprintk("lockd: cancel status %d (task %d)\n",
750 req->a_res.status, task->tk_pid);
752 switch (req->a_res.status) {
753 case NLM_LCK_GRANTED:
754 case NLM_LCK_DENIED_GRACE_PERIOD:
755 /* Everything's good */
757 case NLM_LCK_DENIED_NOLOCKS:
758 dprintk("lockd: CANCEL failed (server has no locks)\n");
761 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
766 nlm_release_host(req->a_host);
767 nlmclnt_release_lockargs(req);
772 /* Don't ever retry more than 3 times */
773 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
775 nlm_rebind_host(req->a_host);
776 rpc_restart_call(task);
777 rpc_delay(task, 30 * HZ);
780 static const struct rpc_call_ops nlmclnt_cancel_ops = {
781 .rpc_call_done = nlmclnt_cancel_callback,
785 * Convert an NLM status code to a generic kernel errno
788 nlm_stat_to_errno(u32 status)
791 case NLM_LCK_GRANTED:
795 case NLM_LCK_DENIED_NOLOCKS:
796 case NLM_LCK_DENIED_GRACE_PERIOD:
798 case NLM_LCK_BLOCKED:
799 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
801 #ifdef CONFIG_LOCKD_V4
814 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);