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 = sprintf(req->a_owner, "%d@%s",
136 current->pid, system_utsname.nodename);
137 locks_copy_lock(&lock->fl, fl);
140 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
142 struct file_lock *fl = &req->a_args.lock.fl;
144 if (fl->fl_ops && fl->fl_ops->fl_release_private)
145 fl->fl_ops->fl_release_private(fl);
149 * Initialize arguments for GRANTED call. The nlm_rqst structure
150 * has been cleared already.
153 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
155 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
156 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
157 call->a_args.lock.caller = system_utsname.nodename;
158 call->a_args.lock.oh.len = lock->oh.len;
160 /* set default data area */
161 call->a_args.lock.oh.data = call->a_owner;
163 if (lock->oh.len > NLMCLNT_OHSIZE) {
164 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
166 nlmclnt_freegrantargs(call);
169 call->a_args.lock.oh.data = (u8 *) data;
172 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
177 nlmclnt_freegrantargs(struct nlm_rqst *call)
179 struct file_lock *fl = &call->a_args.lock.fl;
181 * Check whether we allocated memory for the owner.
183 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
184 kfree(call->a_args.lock.oh.data);
186 if (fl->fl_ops && fl->fl_ops->fl_release_private)
187 fl->fl_ops->fl_release_private(fl);
191 * This is the main entry point for the NLM client.
194 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
196 struct nfs_server *nfssrv = NFS_SERVER(inode);
197 struct nlm_host *host;
198 struct nlm_rqst reqst, *call = &reqst;
201 int status, proto, vers;
203 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
204 if (NFS_PROTO(inode)->version > 3) {
205 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
209 /* Retrieve transport protocol from NFS client */
210 proto = NFS_CLIENT(inode)->cl_xprt->prot;
212 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
215 /* Create RPC client handle if not there, and copy soft
216 * and intr flags from NFS client. */
217 if (host->h_rpcclnt == NULL) {
218 struct rpc_clnt *clnt;
220 /* Bind an rpc client to this host handle (does not
221 * perform a portmapper lookup) */
222 if (!(clnt = nlm_bind_host(host))) {
226 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
227 clnt->cl_intr = nfssrv->client->cl_intr;
230 /* Keep the old signal mask */
231 spin_lock_irqsave(¤t->sighand->siglock, flags);
232 oldset = current->blocked;
234 /* If we're cleaning up locks because the process is exiting,
235 * perform the RPC call asynchronously. */
236 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
237 && fl->fl_type == F_UNLCK
238 && (current->flags & PF_EXITING)) {
239 sigfillset(¤t->blocked); /* Mask all signals */
241 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
243 call = nlmclnt_alloc_call();
248 call->a_flags = RPC_TASK_ASYNC;
250 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
251 memset(call, 0, sizeof(*call));
252 locks_init_lock(&call->a_args.lock.fl);
253 locks_init_lock(&call->a_res.lock.fl);
257 nlmclnt_locks_init_private(fl, host);
259 /* Set up the argument struct */
260 nlmclnt_setlockargs(call, fl);
262 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
263 if (fl->fl_type != F_UNLCK) {
264 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
265 status = nlmclnt_lock(call, fl);
267 status = nlmclnt_unlock(call, fl);
268 } else if (IS_GETLK(cmd))
269 status = nlmclnt_test(call, fl);
274 spin_lock_irqsave(¤t->sighand->siglock, flags);
275 current->blocked = oldset;
277 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
280 dprintk("lockd: clnt proc returns %d\n", status);
281 nlm_release_host(host);
284 EXPORT_SYMBOL(nlmclnt_proc);
287 * Allocate an NLM RPC call struct
290 nlmclnt_alloc_call(void)
292 struct nlm_rqst *call;
294 while (!signalled()) {
295 call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
297 memset(call, 0, sizeof(*call));
298 locks_init_lock(&call->a_args.lock.fl);
299 locks_init_lock(&call->a_res.lock.fl);
302 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
303 schedule_timeout_interruptible(5*HZ);
308 static int nlm_wait_on_grace(wait_queue_head_t *queue)
313 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
315 schedule_timeout(NLMCLNT_GRACE_WAIT);
320 finish_wait(queue, &wait);
328 nlmclnt_call(struct nlm_rqst *req, u32 proc)
330 struct nlm_host *host = req->a_host;
331 struct rpc_clnt *clnt;
332 struct nlm_args *argp = &req->a_args;
333 struct nlm_res *resp = &req->a_res;
334 struct rpc_message msg = {
340 dprintk("lockd: call procedure %d on %s\n",
341 (int)proc, host->h_name);
344 if (host->h_reclaiming && !argp->reclaim)
345 goto in_grace_period;
347 /* If we have no RPC client yet, create one. */
348 if ((clnt = nlm_bind_host(host)) == NULL)
350 msg.rpc_proc = &clnt->cl_procinfo[proc];
352 /* Perform the RPC call. If an error occurs, try again */
353 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
354 dprintk("lockd: rpc_call returned error %d\n", -status);
356 case -EPROTONOSUPPORT:
362 nlm_rebind_host(host);
366 return signalled () ? -EINTR : status;
372 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
373 dprintk("lockd: server in grace period\n");
376 "lockd: spurious grace period reject?!\n");
380 if (!argp->reclaim) {
381 /* We appear to be out of the grace period */
382 wake_up_all(&host->h_gracewait);
384 dprintk("lockd: server returns status %d\n", resp->status);
385 return 0; /* Okay, call complete */
390 * The server has rebooted and appears to be in the grace
391 * period during which locks are only allowed to be
393 * We can only back off and try again later.
395 status = nlm_wait_on_grace(&host->h_gracewait);
396 } while (status == 0);
402 * Generic NLM call, async version.
404 int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
406 struct nlm_host *host = req->a_host;
407 struct rpc_clnt *clnt;
408 struct rpc_message msg = {
409 .rpc_argp = &req->a_args,
410 .rpc_resp = &req->a_res,
414 dprintk("lockd: call procedure %d on %s (async)\n",
415 (int)proc, host->h_name);
417 /* If we have no RPC client yet, create one. */
418 if ((clnt = nlm_bind_host(host)) == NULL)
420 msg.rpc_proc = &clnt->cl_procinfo[proc];
422 /* bootstrap and kick off the async RPC call */
423 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
428 static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
430 struct nlm_host *host = req->a_host;
431 struct rpc_clnt *clnt;
432 struct nlm_args *argp = &req->a_args;
433 struct nlm_res *resp = &req->a_res;
434 struct rpc_message msg = {
440 dprintk("lockd: call procedure %d on %s (async)\n",
441 (int)proc, host->h_name);
443 /* If we have no RPC client yet, create one. */
444 if ((clnt = nlm_bind_host(host)) == NULL)
446 msg.rpc_proc = &clnt->cl_procinfo[proc];
448 /* Increment host refcount */
450 /* bootstrap and kick off the async RPC call */
451 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
453 nlm_release_host(host);
458 * TEST for the presence of a conflicting lock
461 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
465 status = nlmclnt_call(req, NLMPROC_TEST);
466 nlmclnt_release_lockargs(req);
470 status = req->a_res.status;
471 if (status == NLM_LCK_GRANTED) {
472 fl->fl_type = F_UNLCK;
473 } if (status == NLM_LCK_DENIED) {
475 * Report the conflicting lock back to the application.
477 locks_copy_lock(fl, &req->a_res.lock.fl);
480 return nlm_stat_to_errno(req->a_res.status);
486 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
488 memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
489 nlm_get_lockowner(new->fl_u.nfs_fl.owner);
492 static void nlmclnt_locks_release_private(struct file_lock *fl)
494 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
498 static struct file_lock_operations nlmclnt_lock_ops = {
499 .fl_copy_lock = nlmclnt_locks_copy_lock,
500 .fl_release_private = nlmclnt_locks_release_private,
503 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
505 BUG_ON(fl->fl_ops != NULL);
506 fl->fl_u.nfs_fl.state = 0;
507 fl->fl_u.nfs_fl.flags = 0;
508 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
509 fl->fl_ops = &nlmclnt_lock_ops;
512 static void do_vfs_lock(struct file_lock *fl)
515 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
517 res = posix_lock_file_wait(fl->fl_file, fl);
520 res = flock_lock_file_wait(fl->fl_file, fl);
526 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
531 * LOCK: Try to create a lock
533 * Programmer Harassment Alert
535 * When given a blocking lock request in a sync RPC call, the HPUX lockd
536 * will faithfully return LCK_BLOCKED but never cares to notify us when
537 * the lock could be granted. This way, our local process could hang
538 * around forever waiting for the callback.
540 * Solution A: Implement busy-waiting
541 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
543 * For now I am implementing solution A, because I hate the idea of
544 * re-implementing lockd for a third time in two months. The async
545 * calls shouldn't be too hard to do, however.
547 * This is one of the lovely things about standards in the NFS area:
548 * they're so soft and squishy you can't really blame HP for doing this.
551 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
553 struct nlm_host *host = req->a_host;
554 struct nlm_res *resp = &req->a_res;
558 if (!host->h_monitored && nsm_monitor(host) < 0) {
559 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
565 if (req->a_args.block) {
566 status = nlmclnt_prepare_block(req, host, fl);
571 status = nlmclnt_call(req, NLMPROC_LOCK);
574 if (resp->status != NLM_LCK_BLOCKED)
576 /* Wait on an NLM blocking lock */
577 timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
578 /* Did a reclaimer thread notify us of a server reboot? */
579 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
581 if (resp->status != NLM_LCK_BLOCKED)
585 /* We were interrupted. Send a CANCEL request to the server
588 status = (int)timeout;
592 if (resp->status == NLM_LCK_GRANTED) {
593 fl->fl_u.nfs_fl.state = host->h_state;
594 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
595 fl->fl_flags |= FL_SLEEP;
598 status = nlm_stat_to_errno(resp->status);
600 nlmclnt_finish_block(req);
601 /* Cancel the blocked request if it is still pending */
602 if (resp->status == NLM_LCK_BLOCKED)
603 nlmclnt_cancel(host, req->a_args.block, fl);
605 nlmclnt_release_lockargs(req);
610 * RECLAIM: Try to reclaim a lock
613 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
615 struct nlm_rqst reqst, *req;
619 memset(req, 0, sizeof(*req));
620 locks_init_lock(&req->a_args.lock.fl);
621 locks_init_lock(&req->a_res.lock.fl);
625 /* Set up the argument struct */
626 nlmclnt_setlockargs(req, fl);
627 req->a_args.reclaim = 1;
629 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
630 && req->a_res.status == NLM_LCK_GRANTED)
633 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
634 "(errno %d, status %d)\n", fl->fl_pid,
635 status, req->a_res.status);
638 * FIXME: This is a serious failure. We can
640 * a. Ignore the problem
641 * b. Send the owning process some signal (Linux doesn't have
642 * SIGLOST, though...)
643 * c. Retry the operation
645 * Until someone comes up with a simple implementation
646 * for b or c, I'll choose option a.
653 * UNLOCK: remove an existing lock
656 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
658 struct nlm_res *resp = &req->a_res;
661 /* Clean the GRANTED flag now so the lock doesn't get
662 * reclaimed while we're stuck in the unlock call. */
663 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
665 if (req->a_flags & RPC_TASK_ASYNC) {
666 status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
667 &nlmclnt_unlock_ops);
668 /* Hrmf... Do the unlock early since locks_remove_posix()
669 * really expects us to free the lock synchronously */
672 nlmclnt_release_lockargs(req);
678 status = nlmclnt_call(req, NLMPROC_UNLOCK);
679 nlmclnt_release_lockargs(req);
684 if (resp->status == NLM_LCK_GRANTED)
687 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
688 printk("lockd: unexpected unlock status: %d\n", resp->status);
690 /* What to do now? I'm out of my depth... */
695 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
697 struct nlm_rqst *req = data;
698 int status = req->a_res.status;
700 if (RPC_ASSASSINATED(task))
703 if (task->tk_status < 0) {
704 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
707 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
708 rpc_delay(task, NLMCLNT_GRACE_WAIT);
711 if (status != NLM_LCK_GRANTED)
712 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
714 nlm_release_host(req->a_host);
715 nlmclnt_release_lockargs(req);
719 nlm_rebind_host(req->a_host);
721 rpc_restart_call(task);
724 static const struct rpc_call_ops nlmclnt_unlock_ops = {
725 .rpc_call_done = nlmclnt_unlock_callback,
729 * Cancel a blocked lock request.
730 * We always use an async RPC call for this in order not to hang a
731 * process that has been Ctrl-C'ed.
733 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
735 struct nlm_rqst *req;
740 /* Block all signals while setting up call */
741 spin_lock_irqsave(¤t->sighand->siglock, flags);
742 oldset = current->blocked;
743 sigfillset(¤t->blocked);
745 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
747 req = nlmclnt_alloc_call();
751 req->a_flags = RPC_TASK_ASYNC;
753 nlmclnt_setlockargs(req, fl);
754 req->a_args.block = block;
756 status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
758 nlmclnt_release_lockargs(req);
762 spin_lock_irqsave(¤t->sighand->siglock, flags);
763 current->blocked = oldset;
765 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
770 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
772 struct nlm_rqst *req = data;
774 if (RPC_ASSASSINATED(task))
777 if (task->tk_status < 0) {
778 dprintk("lockd: CANCEL call error %d, retrying.\n",
783 dprintk("lockd: cancel status %d (task %d)\n",
784 req->a_res.status, task->tk_pid);
786 switch (req->a_res.status) {
787 case NLM_LCK_GRANTED:
788 case NLM_LCK_DENIED_GRACE_PERIOD:
789 /* Everything's good */
791 case NLM_LCK_DENIED_NOLOCKS:
792 dprintk("lockd: CANCEL failed (server has no locks)\n");
795 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
800 nlm_release_host(req->a_host);
801 nlmclnt_release_lockargs(req);
806 /* Don't ever retry more than 3 times */
807 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
809 nlm_rebind_host(req->a_host);
810 rpc_restart_call(task);
811 rpc_delay(task, 30 * HZ);
814 static const struct rpc_call_ops nlmclnt_cancel_ops = {
815 .rpc_call_done = nlmclnt_cancel_callback,
819 * Convert an NLM status code to a generic kernel errno
822 nlm_stat_to_errno(u32 status)
825 case NLM_LCK_GRANTED:
829 case NLM_LCK_DENIED_NOLOCKS:
830 case NLM_LCK_DENIED_GRACE_PERIOD:
832 case NLM_LCK_BLOCKED:
833 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
835 #ifdef CONFIG_LOCKD_V4
848 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);