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>
20 #include <linux/vs_base.h>
21 #include <linux/vs_cvirt.h>
23 #define NLMDBG_FACILITY NLMDBG_CLIENT
24 #define NLMCLNT_GRACE_WAIT (5*HZ)
25 #define NLMCLNT_POLL_TIMEOUT (30*HZ)
26 #define NLMCLNT_MAX_RETRIES 3
28 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
29 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
30 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
31 static int nlm_stat_to_errno(u32 stat);
32 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
33 static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
35 static const struct rpc_call_ops nlmclnt_unlock_ops;
36 static const struct rpc_call_ops nlmclnt_cancel_ops;
39 * Cookie counter for NLM requests
41 static u32 nlm_cookie = 0x1234;
43 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
45 memcpy(c->data, &nlm_cookie, 4);
46 memset(c->data+4, 0, 4);
51 static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
53 atomic_inc(&lockowner->count);
57 static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
59 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
61 list_del(&lockowner->list);
62 spin_unlock(&lockowner->host->h_lock);
63 nlm_release_host(lockowner->host);
67 static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
69 struct nlm_lockowner *lockowner;
70 list_for_each_entry(lockowner, &host->h_lockowners, list) {
71 if (lockowner->pid == pid)
77 static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
81 res = host->h_pidcount++;
82 } while (nlm_pidbusy(host, res) < 0);
86 static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
88 struct nlm_lockowner *lockowner;
89 list_for_each_entry(lockowner, &host->h_lockowners, list) {
90 if (lockowner->owner != owner)
92 return nlm_get_lockowner(lockowner);
97 static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
99 struct nlm_lockowner *res, *new = NULL;
101 spin_lock(&host->h_lock);
102 res = __nlm_find_lockowner(host, owner);
104 spin_unlock(&host->h_lock);
105 new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL);
106 spin_lock(&host->h_lock);
107 res = __nlm_find_lockowner(host, owner);
108 if (res == NULL && new != NULL) {
110 atomic_set(&new->count, 1);
112 new->pid = __nlm_alloc_pid(host);
113 new->host = nlm_get_host(host);
114 list_add(&new->list, &host->h_lockowners);
118 spin_unlock(&host->h_lock);
124 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
126 static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
128 struct nlm_args *argp = &req->a_args;
129 struct nlm_lock *lock = &argp->lock;
131 nlmclnt_next_cookie(&argp->cookie);
132 argp->state = nsm_local_state;
133 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
134 lock->caller = vx_new_uts(nodename);
135 lock->oh.data = req->a_owner;
136 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
137 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
138 vx_new_uts(nodename));
139 lock->svid = fl->fl_u.nfs_fl.owner->pid;
140 lock->fl.fl_start = fl->fl_start;
141 lock->fl.fl_end = fl->fl_end;
142 lock->fl.fl_type = fl->fl_type;
145 static void nlmclnt_release_lockargs(struct nlm_rqst *req)
147 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
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 nlm_host *host;
157 struct nlm_rqst *call;
160 int status, proto, vers;
162 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
163 if (NFS_PROTO(inode)->version > 3) {
164 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
168 /* Retrieve transport protocol from NFS client */
169 proto = NFS_CLIENT(inode)->cl_xprt->prot;
171 host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
175 call = nlm_alloc_call(host);
179 nlmclnt_locks_init_private(fl, host);
180 /* Set up the argument struct */
181 nlmclnt_setlockargs(call, fl);
183 /* Keep the old signal mask */
184 spin_lock_irqsave(¤t->sighand->siglock, flags);
185 oldset = current->blocked;
187 /* If we're cleaning up locks because the process is exiting,
188 * perform the RPC call asynchronously. */
189 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
190 && fl->fl_type == F_UNLCK
191 && (current->flags & PF_EXITING)) {
192 sigfillset(¤t->blocked); /* Mask all signals */
195 call->a_flags = RPC_TASK_ASYNC;
197 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
199 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
200 if (fl->fl_type != F_UNLCK) {
201 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
202 status = nlmclnt_lock(call, fl);
204 status = nlmclnt_unlock(call, fl);
205 } else if (IS_GETLK(cmd))
206 status = nlmclnt_test(call, fl);
210 fl->fl_ops->fl_release_private(fl);
213 spin_lock_irqsave(¤t->sighand->siglock, flags);
214 current->blocked = oldset;
216 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
218 dprintk("lockd: clnt proc returns %d\n", status);
221 EXPORT_SYMBOL(nlmclnt_proc);
224 * Allocate an NLM RPC call struct
226 * Note: the caller must hold a reference to host. In case of failure,
227 * this reference will be released.
229 struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
231 struct nlm_rqst *call;
234 call = kzalloc(sizeof(*call), GFP_KERNEL);
236 locks_init_lock(&call->a_args.lock.fl);
237 locks_init_lock(&call->a_res.lock.fl);
243 printk("nlm_alloc_call: failed, waiting for memory\n");
244 schedule_timeout_interruptible(5*HZ);
246 nlm_release_host(host);
250 void nlm_release_call(struct nlm_rqst *call)
252 nlm_release_host(call->a_host);
253 nlmclnt_release_lockargs(call);
257 static void nlmclnt_rpc_release(void *data)
259 return nlm_release_call(data);
262 static int nlm_wait_on_grace(wait_queue_head_t *queue)
267 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
269 schedule_timeout(NLMCLNT_GRACE_WAIT);
274 finish_wait(queue, &wait);
282 nlmclnt_call(struct nlm_rqst *req, u32 proc)
284 struct nlm_host *host = req->a_host;
285 struct rpc_clnt *clnt;
286 struct nlm_args *argp = &req->a_args;
287 struct nlm_res *resp = &req->a_res;
288 struct rpc_message msg = {
294 dprintk("lockd: call procedure %d on %s\n",
295 (int)proc, host->h_name);
298 if (host->h_reclaiming && !argp->reclaim)
299 goto in_grace_period;
301 /* If we have no RPC client yet, create one. */
302 if ((clnt = nlm_bind_host(host)) == NULL)
304 msg.rpc_proc = &clnt->cl_procinfo[proc];
306 /* Perform the RPC call. If an error occurs, try again */
307 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
308 dprintk("lockd: rpc_call returned error %d\n", -status);
310 case -EPROTONOSUPPORT:
316 nlm_rebind_host(host);
320 return signalled () ? -EINTR : status;
326 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
327 dprintk("lockd: server in grace period\n");
330 "lockd: spurious grace period reject?!\n");
334 if (!argp->reclaim) {
335 /* We appear to be out of the grace period */
336 wake_up_all(&host->h_gracewait);
338 dprintk("lockd: server returns status %d\n", resp->status);
339 return 0; /* Okay, call complete */
344 * The server has rebooted and appears to be in the grace
345 * period during which locks are only allowed to be
347 * We can only back off and try again later.
349 status = nlm_wait_on_grace(&host->h_gracewait);
350 } while (status == 0);
356 * Generic NLM call, async version.
358 static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
360 struct nlm_host *host = req->a_host;
361 struct rpc_clnt *clnt;
362 int status = -ENOLCK;
364 dprintk("lockd: call procedure %d on %s (async)\n",
365 (int)proc, host->h_name);
367 /* If we have no RPC client yet, create one. */
368 clnt = nlm_bind_host(host);
371 msg->rpc_proc = &clnt->cl_procinfo[proc];
373 /* bootstrap and kick off the async RPC call */
374 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
378 nlm_release_call(req);
382 int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
384 struct rpc_message msg = {
385 .rpc_argp = &req->a_args,
386 .rpc_resp = &req->a_res,
388 return __nlm_async_call(req, proc, &msg, tk_ops);
391 int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
393 struct rpc_message msg = {
394 .rpc_argp = &req->a_res,
396 return __nlm_async_call(req, proc, &msg, tk_ops);
400 * TEST for the presence of a conflicting lock
403 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
407 status = nlmclnt_call(req, NLMPROC_TEST);
411 switch (req->a_res.status) {
412 case NLM_LCK_GRANTED:
413 fl->fl_type = F_UNLCK;
417 * Report the conflicting lock back to the application.
419 fl->fl_start = req->a_res.lock.fl.fl_start;
420 fl->fl_end = req->a_res.lock.fl.fl_start;
421 fl->fl_type = req->a_res.lock.fl.fl_type;
425 status = nlm_stat_to_errno(req->a_res.status);
428 nlm_release_call(req);
432 static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
434 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
435 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
436 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
439 static void nlmclnt_locks_release_private(struct file_lock *fl)
441 list_del(&fl->fl_u.nfs_fl.list);
442 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
445 static struct file_lock_operations nlmclnt_lock_ops = {
446 .fl_copy_lock = nlmclnt_locks_copy_lock,
447 .fl_release_private = nlmclnt_locks_release_private,
450 static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
452 BUG_ON(fl->fl_ops != NULL);
453 fl->fl_u.nfs_fl.state = 0;
454 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
455 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
456 fl->fl_ops = &nlmclnt_lock_ops;
459 static int do_vfs_lock(struct file_lock *fl)
462 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
464 res = posix_lock_file_wait(fl->fl_file, fl);
467 res = flock_lock_file_wait(fl->fl_file, fl);
476 * LOCK: Try to create a lock
478 * Programmer Harassment Alert
480 * When given a blocking lock request in a sync RPC call, the HPUX lockd
481 * will faithfully return LCK_BLOCKED but never cares to notify us when
482 * the lock could be granted. This way, our local process could hang
483 * around forever waiting for the callback.
485 * Solution A: Implement busy-waiting
486 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
488 * For now I am implementing solution A, because I hate the idea of
489 * re-implementing lockd for a third time in two months. The async
490 * calls shouldn't be too hard to do, however.
492 * This is one of the lovely things about standards in the NFS area:
493 * they're so soft and squishy you can't really blame HP for doing this.
496 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
498 struct nlm_host *host = req->a_host;
499 struct nlm_res *resp = &req->a_res;
500 struct nlm_wait *block = NULL;
501 unsigned char fl_flags = fl->fl_flags;
502 int status = -ENOLCK;
504 if (!host->h_monitored && nsm_monitor(host) < 0) {
505 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
509 fl->fl_flags |= FL_ACCESS;
510 status = do_vfs_lock(fl);
514 block = nlmclnt_prepare_block(host, fl);
517 /* Reboot protection */
518 fl->fl_u.nfs_fl.state = host->h_state;
519 status = nlmclnt_call(req, NLMPROC_LOCK);
522 if (!req->a_args.block)
524 /* Did a reclaimer thread notify us of a server reboot? */
525 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
527 if (resp->status != NLM_LCK_BLOCKED)
529 /* Wait on an NLM blocking lock */
530 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
531 /* if we were interrupted. Send a CANCEL request to the server
536 if (resp->status != NLM_LCK_BLOCKED)
540 if (resp->status == NLM_LCK_GRANTED) {
541 down_read(&host->h_rwsem);
542 /* Check whether or not the server has rebooted */
543 if (fl->fl_u.nfs_fl.state != host->h_state) {
544 up_read(&host->h_rwsem);
547 /* Ensure the resulting lock will get added to granted list */
548 fl->fl_flags = fl_flags | FL_SLEEP;
549 if (do_vfs_lock(fl) < 0)
550 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
551 up_read(&host->h_rwsem);
553 status = nlm_stat_to_errno(resp->status);
555 nlmclnt_finish_block(block);
556 /* Cancel the blocked request if it is still pending */
557 if (resp->status == NLM_LCK_BLOCKED)
558 nlmclnt_cancel(host, req->a_args.block, fl);
560 nlm_release_call(req);
561 fl->fl_flags = fl_flags;
566 * RECLAIM: Try to reclaim a lock
569 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
571 struct nlm_rqst reqst, *req;
575 memset(req, 0, sizeof(*req));
576 locks_init_lock(&req->a_args.lock.fl);
577 locks_init_lock(&req->a_res.lock.fl);
581 /* Set up the argument struct */
582 nlmclnt_setlockargs(req, fl);
583 req->a_args.reclaim = 1;
585 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
586 && req->a_res.status == NLM_LCK_GRANTED)
589 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
590 "(errno %d, status %d)\n", fl->fl_pid,
591 status, req->a_res.status);
594 * FIXME: This is a serious failure. We can
596 * a. Ignore the problem
597 * b. Send the owning process some signal (Linux doesn't have
598 * SIGLOST, though...)
599 * c. Retry the operation
601 * Until someone comes up with a simple implementation
602 * for b or c, I'll choose option a.
609 * UNLOCK: remove an existing lock
612 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
614 struct nlm_host *host = req->a_host;
615 struct nlm_res *resp = &req->a_res;
619 * Note: the server is supposed to either grant us the unlock
620 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
621 * case, we want to unlock.
623 fl->fl_flags |= FL_EXISTS;
624 down_read(&host->h_rwsem);
625 if (do_vfs_lock(fl) == -ENOENT) {
626 up_read(&host->h_rwsem);
629 up_read(&host->h_rwsem);
631 if (req->a_flags & RPC_TASK_ASYNC)
632 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
634 status = nlmclnt_call(req, NLMPROC_UNLOCK);
638 if (resp->status == NLM_LCK_GRANTED)
641 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
642 printk("lockd: unexpected unlock status: %d\n", resp->status);
643 /* What to do now? I'm out of my depth... */
646 nlm_release_call(req);
650 static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
652 struct nlm_rqst *req = data;
653 int status = req->a_res.status;
655 if (RPC_ASSASSINATED(task))
658 if (task->tk_status < 0) {
659 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
662 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
663 rpc_delay(task, NLMCLNT_GRACE_WAIT);
666 if (status != NLM_LCK_GRANTED)
667 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
671 nlm_rebind_host(req->a_host);
673 rpc_restart_call(task);
676 static const struct rpc_call_ops nlmclnt_unlock_ops = {
677 .rpc_call_done = nlmclnt_unlock_callback,
678 .rpc_release = nlmclnt_rpc_release,
682 * Cancel a blocked lock request.
683 * We always use an async RPC call for this in order not to hang a
684 * process that has been Ctrl-C'ed.
686 static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
688 struct nlm_rqst *req;
693 /* Block all signals while setting up call */
694 spin_lock_irqsave(¤t->sighand->siglock, flags);
695 oldset = current->blocked;
696 sigfillset(¤t->blocked);
698 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
700 req = nlm_alloc_call(nlm_get_host(host));
703 req->a_flags = RPC_TASK_ASYNC;
705 nlmclnt_setlockargs(req, fl);
706 req->a_args.block = block;
708 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
710 spin_lock_irqsave(¤t->sighand->siglock, flags);
711 current->blocked = oldset;
713 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
718 static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
720 struct nlm_rqst *req = data;
722 if (RPC_ASSASSINATED(task))
725 if (task->tk_status < 0) {
726 dprintk("lockd: CANCEL call error %d, retrying.\n",
731 dprintk("lockd: cancel status %d (task %d)\n",
732 req->a_res.status, task->tk_pid);
734 switch (req->a_res.status) {
735 case NLM_LCK_GRANTED:
736 case NLM_LCK_DENIED_GRACE_PERIOD:
738 /* Everything's good */
740 case NLM_LCK_DENIED_NOLOCKS:
741 dprintk("lockd: CANCEL failed (server has no locks)\n");
744 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
752 /* Don't ever retry more than 3 times */
753 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
755 nlm_rebind_host(req->a_host);
756 rpc_restart_call(task);
757 rpc_delay(task, 30 * HZ);
760 static const struct rpc_call_ops nlmclnt_cancel_ops = {
761 .rpc_call_done = nlmclnt_cancel_callback,
762 .rpc_release = nlmclnt_rpc_release,
766 * Convert an NLM status code to a generic kernel errno
769 nlm_stat_to_errno(u32 status)
772 case NLM_LCK_GRANTED:
776 case NLM_LCK_DENIED_NOLOCKS:
777 case NLM_LCK_DENIED_GRACE_PERIOD:
779 case NLM_LCK_BLOCKED:
780 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
782 #ifdef CONFIG_LOCKD_V4
795 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);