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 * Initialize arguments for GRANTED call. The nlm_rqst structure
152 * has been cleared already.
155 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
157 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
158 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
159 call->a_args.lock.caller = system_utsname.nodename;
160 call->a_args.lock.oh.len = lock->oh.len;
162 /* set default data area */
163 call->a_args.lock.oh.data = call->a_owner;
164 call->a_args.lock.svid = lock->fl.fl_pid;
166 if (lock->oh.len > NLMCLNT_OHSIZE) {
167 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
169 nlmclnt_freegrantargs(call);
172 call->a_args.lock.oh.data = (u8 *) data;
175 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
180 nlmclnt_freegrantargs(struct nlm_rqst *call)
182 struct file_lock *fl = &call->a_args.lock.fl;
184 * Check whether we allocated memory for the owner.
186 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
187 kfree(call->a_args.lock.oh.data);
189 if (fl->fl_ops && fl->fl_ops->fl_release_private)
190 fl->fl_ops->fl_release_private(fl);
194 * This is the main entry point for the NLM client.
197 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
199 struct nfs_server *nfssrv = NFS_SERVER(inode);
200 struct nlm_host *host;
201 struct nlm_rqst reqst, *call = &reqst;
204 int status, proto, vers;
206 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
207 if (NFS_PROTO(inode)->version > 3) {
208 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
212 /* Retrieve transport protocol from NFS client */
213 proto = NFS_CLIENT(inode)->cl_xprt->prot;
215 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
218 /* Create RPC client handle if not there, and copy soft
219 * and intr flags from NFS client. */
220 if (host->h_rpcclnt == NULL) {
221 struct rpc_clnt *clnt;
223 /* Bind an rpc client to this host handle (does not
224 * perform a portmapper lookup) */
225 if (!(clnt = nlm_bind_host(host))) {
229 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
230 clnt->cl_intr = nfssrv->client->cl_intr;
233 /* Keep the old signal mask */
234 spin_lock_irqsave(¤t->sighand->siglock, flags);
235 oldset = current->blocked;
237 /* If we're cleaning up locks because the process is exiting,
238 * perform the RPC call asynchronously. */
239 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
240 && fl->fl_type == F_UNLCK
241 && (current->flags & PF_EXITING)) {
242 sigfillset(¤t->blocked); /* Mask all signals */
244 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
246 call = nlmclnt_alloc_call();
251 call->a_flags = RPC_TASK_ASYNC;
253 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
254 memset(call, 0, sizeof(*call));
255 locks_init_lock(&call->a_args.lock.fl);
256 locks_init_lock(&call->a_res.lock.fl);
260 nlmclnt_locks_init_private(fl, host);
262 /* Set up the argument struct */
263 nlmclnt_setlockargs(call, fl);
265 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
266 if (fl->fl_type != F_UNLCK) {
267 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
268 status = nlmclnt_lock(call, fl);
270 status = nlmclnt_unlock(call, fl);
271 } else if (IS_GETLK(cmd))
272 status = nlmclnt_test(call, fl);
277 spin_lock_irqsave(¤t->sighand->siglock, flags);
278 current->blocked = oldset;
280 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
283 dprintk("lockd: clnt proc returns %d\n", status);
284 nlm_release_host(host);
287 EXPORT_SYMBOL(nlmclnt_proc);
290 * Allocate an NLM RPC call struct
293 nlmclnt_alloc_call(void)
295 struct nlm_rqst *call;
298 call = kzalloc(sizeof(*call), GFP_KERNEL);
300 locks_init_lock(&call->a_args.lock.fl);
301 locks_init_lock(&call->a_res.lock.fl);
306 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
307 schedule_timeout_interruptible(5*HZ);
312 static int nlm_wait_on_grace(wait_queue_head_t *queue)
317 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
319 schedule_timeout(NLMCLNT_GRACE_WAIT);
324 finish_wait(queue, &wait);
332 nlmclnt_call(struct nlm_rqst *req, u32 proc)
334 struct nlm_host *host = req->a_host;
335 struct rpc_clnt *clnt;
336 struct nlm_args *argp = &req->a_args;
337 struct nlm_res *resp = &req->a_res;
338 struct rpc_message msg = {
344 dprintk("lockd: call procedure %d on %s\n",
345 (int)proc, host->h_name);
348 if (host->h_reclaiming && !argp->reclaim)
349 goto in_grace_period;
351 /* If we have no RPC client yet, create one. */
352 if ((clnt = nlm_bind_host(host)) == NULL)
354 msg.rpc_proc = &clnt->cl_procinfo[proc];
356 /* Perform the RPC call. If an error occurs, try again */
357 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
358 dprintk("lockd: rpc_call returned error %d\n", -status);
360 case -EPROTONOSUPPORT:
366 nlm_rebind_host(host);
370 return signalled () ? -EINTR : status;
376 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
377 dprintk("lockd: server in grace period\n");
380 "lockd: spurious grace period reject?!\n");
384 if (!argp->reclaim) {
385 /* We appear to be out of the grace period */
386 wake_up_all(&host->h_gracewait);
388 dprintk("lockd: server returns status %d\n", resp->status);
389 return 0; /* Okay, call complete */
394 * The server has rebooted and appears to be in the grace
395 * period during which locks are only allowed to be
397 * We can only back off and try again later.
399 status = nlm_wait_on_grace(&host->h_gracewait);
400 } while (status == 0);
406 * Generic NLM call, async version.
408 int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
410 struct nlm_host *host = req->a_host;
411 struct rpc_clnt *clnt;
412 struct rpc_message msg = {
413 .rpc_argp = &req->a_args,
414 .rpc_resp = &req->a_res,
418 dprintk("lockd: call procedure %d on %s (async)\n",
419 (int)proc, host->h_name);
421 /* If we have no RPC client yet, create one. */
422 if ((clnt = nlm_bind_host(host)) == NULL)
424 msg.rpc_proc = &clnt->cl_procinfo[proc];
426 /* bootstrap and kick off the async RPC call */
427 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
432 static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
434 struct nlm_host *host = req->a_host;
435 struct rpc_clnt *clnt;
436 struct nlm_args *argp = &req->a_args;
437 struct nlm_res *resp = &req->a_res;
438 struct rpc_message msg = {
444 dprintk("lockd: call procedure %d on %s (async)\n",
445 (int)proc, host->h_name);
447 /* If we have no RPC client yet, create one. */
448 if ((clnt = nlm_bind_host(host)) == NULL)
450 msg.rpc_proc = &clnt->cl_procinfo[proc];
452 /* Increment host refcount */
454 /* bootstrap and kick off the async RPC call */
455 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
457 nlm_release_host(host);
462 * TEST for the presence of a conflicting lock
465 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
469 status = nlmclnt_call(req, NLMPROC_TEST);
470 nlmclnt_release_lockargs(req);
474 status = req->a_res.status;
475 if (status == NLM_LCK_GRANTED) {
476 fl->fl_type = F_UNLCK;
477 } if (status == NLM_LCK_DENIED) {
479 * Report the conflicting lock back to the application.
481 locks_copy_lock(fl, &req->a_res.lock.fl);
484 return nlm_stat_to_errno(req->a_res.status);
490 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
492 memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
493 nlm_get_lockowner(new->fl_u.nfs_fl.owner);
496 static void nlmclnt_locks_release_private(struct file_lock *fl)
498 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
502 static struct file_lock_operations nlmclnt_lock_ops = {
503 .fl_copy_lock = nlmclnt_locks_copy_lock,
504 .fl_release_private = nlmclnt_locks_release_private,
507 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
509 BUG_ON(fl->fl_ops != NULL);
510 fl->fl_u.nfs_fl.state = 0;
511 fl->fl_u.nfs_fl.flags = 0;
512 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
513 fl->fl_ops = &nlmclnt_lock_ops;
516 static void do_vfs_lock(struct file_lock *fl)
519 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
521 res = posix_lock_file_wait(fl->fl_file, fl);
524 res = flock_lock_file_wait(fl->fl_file, fl);
530 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
535 * LOCK: Try to create a lock
537 * Programmer Harassment Alert
539 * When given a blocking lock request in a sync RPC call, the HPUX lockd
540 * will faithfully return LCK_BLOCKED but never cares to notify us when
541 * the lock could be granted. This way, our local process could hang
542 * around forever waiting for the callback.
544 * Solution A: Implement busy-waiting
545 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
547 * For now I am implementing solution A, because I hate the idea of
548 * re-implementing lockd for a third time in two months. The async
549 * calls shouldn't be too hard to do, however.
551 * This is one of the lovely things about standards in the NFS area:
552 * they're so soft and squishy you can't really blame HP for doing this.
555 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
557 struct nlm_host *host = req->a_host;
558 struct nlm_res *resp = &req->a_res;
562 if (!host->h_monitored && nsm_monitor(host) < 0) {
563 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
569 if (req->a_args.block) {
570 status = nlmclnt_prepare_block(req, host, fl);
575 status = nlmclnt_call(req, NLMPROC_LOCK);
578 if (resp->status != NLM_LCK_BLOCKED)
580 /* Wait on an NLM blocking lock */
581 timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
582 /* Did a reclaimer thread notify us of a server reboot? */
583 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
585 if (resp->status != NLM_LCK_BLOCKED)
589 /* We were interrupted. Send a CANCEL request to the server
592 status = (int)timeout;
596 if (resp->status == NLM_LCK_GRANTED) {
597 fl->fl_u.nfs_fl.state = host->h_state;
598 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
599 fl->fl_flags |= FL_SLEEP;
602 status = nlm_stat_to_errno(resp->status);
604 nlmclnt_finish_block(req);
605 /* Cancel the blocked request if it is still pending */
606 if (resp->status == NLM_LCK_BLOCKED)
607 nlmclnt_cancel(host, req->a_args.block, fl);
609 nlmclnt_release_lockargs(req);
614 * RECLAIM: Try to reclaim a lock
617 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
619 struct nlm_rqst reqst, *req;
623 memset(req, 0, sizeof(*req));
624 locks_init_lock(&req->a_args.lock.fl);
625 locks_init_lock(&req->a_res.lock.fl);
629 /* Set up the argument struct */
630 nlmclnt_setlockargs(req, fl);
631 req->a_args.reclaim = 1;
633 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
634 && req->a_res.status == NLM_LCK_GRANTED)
637 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
638 "(errno %d, status %d)\n", fl->fl_pid,
639 status, req->a_res.status);
642 * FIXME: This is a serious failure. We can
644 * a. Ignore the problem
645 * b. Send the owning process some signal (Linux doesn't have
646 * SIGLOST, though...)
647 * c. Retry the operation
649 * Until someone comes up with a simple implementation
650 * for b or c, I'll choose option a.
657 * UNLOCK: remove an existing lock
660 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
662 struct nlm_res *resp = &req->a_res;
665 /* Clean the GRANTED flag now so the lock doesn't get
666 * reclaimed while we're stuck in the unlock call. */
667 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
670 * Note: the server is supposed to either grant us the unlock
671 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
672 * case, we want to unlock.
676 if (req->a_flags & RPC_TASK_ASYNC) {
677 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
678 &nlmclnt_unlock_ops);
679 /* Hrmf... Do the unlock early since locks_remove_posix()
680 * really expects us to free the lock synchronously */
682 nlmclnt_release_lockargs(req);
688 status = nlmclnt_call(req, NLMPROC_UNLOCK);
689 nlmclnt_release_lockargs(req);
693 if (resp->status == NLM_LCK_GRANTED)
696 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
697 printk("lockd: unexpected unlock status: %d\n", resp->status);
699 /* What to do now? I'm out of my depth... */
704 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
706 struct nlm_rqst *req = data;
707 int status = req->a_res.status;
709 if (RPC_ASSASSINATED(task))
712 if (task->tk_status < 0) {
713 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
716 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
717 rpc_delay(task, NLMCLNT_GRACE_WAIT);
720 if (status != NLM_LCK_GRANTED)
721 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
723 nlm_release_host(req->a_host);
724 nlmclnt_release_lockargs(req);
728 nlm_rebind_host(req->a_host);
730 rpc_restart_call(task);
733 static const struct rpc_call_ops nlmclnt_unlock_ops = {
734 .rpc_call_done = nlmclnt_unlock_callback,
738 * Cancel a blocked lock request.
739 * We always use an async RPC call for this in order not to hang a
740 * process that has been Ctrl-C'ed.
742 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
744 struct nlm_rqst *req;
749 /* Block all signals while setting up call */
750 spin_lock_irqsave(¤t->sighand->siglock, flags);
751 oldset = current->blocked;
752 sigfillset(¤t->blocked);
754 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
756 req = nlmclnt_alloc_call();
760 req->a_flags = RPC_TASK_ASYNC;
762 nlmclnt_setlockargs(req, fl);
763 req->a_args.block = block;
765 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
767 nlmclnt_release_lockargs(req);
771 spin_lock_irqsave(¤t->sighand->siglock, flags);
772 current->blocked = oldset;
774 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
779 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
781 struct nlm_rqst *req = data;
783 if (RPC_ASSASSINATED(task))
786 if (task->tk_status < 0) {
787 dprintk("lockd: CANCEL call error %d, retrying.\n",
792 dprintk("lockd: cancel status %d (task %d)\n",
793 req->a_res.status, task->tk_pid);
795 switch (req->a_res.status) {
796 case NLM_LCK_GRANTED:
797 case NLM_LCK_DENIED_GRACE_PERIOD:
798 /* Everything's good */
800 case NLM_LCK_DENIED_NOLOCKS:
801 dprintk("lockd: CANCEL failed (server has no locks)\n");
804 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
809 nlm_release_host(req->a_host);
810 nlmclnt_release_lockargs(req);
815 /* Don't ever retry more than 3 times */
816 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
818 nlm_rebind_host(req->a_host);
819 rpc_restart_call(task);
820 rpc_delay(task, 30 * HZ);
823 static const struct rpc_call_ops nlmclnt_cancel_ops = {
824 .rpc_call_done = nlmclnt_cancel_callback,
828 * Convert an NLM status code to a generic kernel errno
831 nlm_stat_to_errno(u32 status)
834 case NLM_LCK_GRANTED:
838 case NLM_LCK_DENIED_NOLOCKS:
839 case NLM_LCK_DENIED_GRACE_PERIOD:
841 case NLM_LCK_BLOCKED:
842 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
844 #ifdef CONFIG_LOCKD_V4
857 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);