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 lock
->fl
.fl_start
= fl
->fl_start
;
140 lock
->fl
.fl_end
= fl
->fl_end
;
141 lock
->fl
.fl_type
= fl
->fl_type
;
144 static void nlmclnt_release_lockargs(struct nlm_rqst
*req
)
146 BUG_ON(req
->a_args
.lock
.fl
.fl_ops
!= NULL
);
150 * This is the main entry point for the NLM client.
153 nlmclnt_proc(struct inode
*inode
, int cmd
, struct file_lock
*fl
)
155 struct nlm_host
*host
;
156 struct nlm_rqst
*call
;
159 int status
, proto
, vers
;
161 vers
= (NFS_PROTO(inode
)->version
== 3) ? 4 : 1;
162 if (NFS_PROTO(inode
)->version
> 3) {
163 printk(KERN_NOTICE
"NFSv4 file locking not implemented!\n");
167 /* Retrieve transport protocol from NFS client */
168 proto
= NFS_CLIENT(inode
)->cl_xprt
->prot
;
170 host
= nlmclnt_lookup_host(NFS_ADDR(inode
), proto
, vers
);
174 call
= nlm_alloc_call(host
);
178 nlmclnt_locks_init_private(fl
, host
);
179 /* Set up the argument struct */
180 nlmclnt_setlockargs(call
, fl
);
182 /* Keep the old signal mask */
183 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
184 oldset
= current
->blocked
;
186 /* If we're cleaning up locks because the process is exiting,
187 * perform the RPC call asynchronously. */
188 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
))
189 && fl
->fl_type
== F_UNLCK
190 && (current
->flags
& PF_EXITING
)) {
191 sigfillset(¤t
->blocked
); /* Mask all signals */
194 call
->a_flags
= RPC_TASK_ASYNC
;
196 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
198 if (IS_SETLK(cmd
) || IS_SETLKW(cmd
)) {
199 if (fl
->fl_type
!= F_UNLCK
) {
200 call
->a_args
.block
= IS_SETLKW(cmd
) ? 1 : 0;
201 status
= nlmclnt_lock(call
, fl
);
203 status
= nlmclnt_unlock(call
, fl
);
204 } else if (IS_GETLK(cmd
))
205 status
= nlmclnt_test(call
, fl
);
209 fl
->fl_ops
->fl_release_private(fl
);
212 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
213 current
->blocked
= oldset
;
215 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
217 dprintk("lockd: clnt proc returns %d\n", status
);
220 EXPORT_SYMBOL(nlmclnt_proc
);
223 * Allocate an NLM RPC call struct
225 * Note: the caller must hold a reference to host. In case of failure,
226 * this reference will be released.
228 struct nlm_rqst
*nlm_alloc_call(struct nlm_host
*host
)
230 struct nlm_rqst
*call
;
233 call
= kzalloc(sizeof(*call
), GFP_KERNEL
);
235 locks_init_lock(&call
->a_args
.lock
.fl
);
236 locks_init_lock(&call
->a_res
.lock
.fl
);
242 printk("nlm_alloc_call: failed, waiting for memory\n");
243 schedule_timeout_interruptible(5*HZ
);
245 nlm_release_host(host
);
249 void nlm_release_call(struct nlm_rqst
*call
)
251 nlm_release_host(call
->a_host
);
252 nlmclnt_release_lockargs(call
);
256 static void nlmclnt_rpc_release(void *data
)
258 return nlm_release_call(data
);
261 static int nlm_wait_on_grace(wait_queue_head_t
*queue
)
266 prepare_to_wait(queue
, &wait
, TASK_INTERRUPTIBLE
);
268 schedule_timeout(NLMCLNT_GRACE_WAIT
);
273 finish_wait(queue
, &wait
);
281 nlmclnt_call(struct nlm_rqst
*req
, u32 proc
)
283 struct nlm_host
*host
= req
->a_host
;
284 struct rpc_clnt
*clnt
;
285 struct nlm_args
*argp
= &req
->a_args
;
286 struct nlm_res
*resp
= &req
->a_res
;
287 struct rpc_message msg
= {
293 dprintk("lockd: call procedure %d on %s\n",
294 (int)proc
, host
->h_name
);
297 if (host
->h_reclaiming
&& !argp
->reclaim
)
298 goto in_grace_period
;
300 /* If we have no RPC client yet, create one. */
301 if ((clnt
= nlm_bind_host(host
)) == NULL
)
303 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
305 /* Perform the RPC call. If an error occurs, try again */
306 if ((status
= rpc_call_sync(clnt
, &msg
, 0)) < 0) {
307 dprintk("lockd: rpc_call returned error %d\n", -status
);
309 case -EPROTONOSUPPORT
:
315 nlm_rebind_host(host
);
319 return signalled () ? -EINTR
: status
;
325 if (resp
->status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
326 dprintk("lockd: server in grace period\n");
329 "lockd: spurious grace period reject?!\n");
333 if (!argp
->reclaim
) {
334 /* We appear to be out of the grace period */
335 wake_up_all(&host
->h_gracewait
);
337 dprintk("lockd: server returns status %d\n", resp
->status
);
338 return 0; /* Okay, call complete */
343 * The server has rebooted and appears to be in the grace
344 * period during which locks are only allowed to be
346 * We can only back off and try again later.
348 status
= nlm_wait_on_grace(&host
->h_gracewait
);
349 } while (status
== 0);
355 * Generic NLM call, async version.
357 static int __nlm_async_call(struct nlm_rqst
*req
, u32 proc
, struct rpc_message
*msg
, const struct rpc_call_ops
*tk_ops
)
359 struct nlm_host
*host
= req
->a_host
;
360 struct rpc_clnt
*clnt
;
361 int status
= -ENOLCK
;
363 dprintk("lockd: call procedure %d on %s (async)\n",
364 (int)proc
, host
->h_name
);
366 /* If we have no RPC client yet, create one. */
367 clnt
= nlm_bind_host(host
);
370 msg
->rpc_proc
= &clnt
->cl_procinfo
[proc
];
372 /* bootstrap and kick off the async RPC call */
373 status
= rpc_call_async(clnt
, msg
, RPC_TASK_ASYNC
, tk_ops
, req
);
377 nlm_release_call(req
);
381 int nlm_async_call(struct nlm_rqst
*req
, u32 proc
, const struct rpc_call_ops
*tk_ops
)
383 struct rpc_message msg
= {
384 .rpc_argp
= &req
->a_args
,
385 .rpc_resp
= &req
->a_res
,
387 return __nlm_async_call(req
, proc
, &msg
, tk_ops
);
390 int nlm_async_reply(struct nlm_rqst
*req
, u32 proc
, const struct rpc_call_ops
*tk_ops
)
392 struct rpc_message msg
= {
393 .rpc_argp
= &req
->a_res
,
395 return __nlm_async_call(req
, proc
, &msg
, tk_ops
);
399 * TEST for the presence of a conflicting lock
402 nlmclnt_test(struct nlm_rqst
*req
, struct file_lock
*fl
)
406 status
= nlmclnt_call(req
, NLMPROC_TEST
);
410 switch (req
->a_res
.status
) {
411 case NLM_LCK_GRANTED
:
412 fl
->fl_type
= F_UNLCK
;
416 * Report the conflicting lock back to the application.
418 fl
->fl_start
= req
->a_res
.lock
.fl
.fl_start
;
419 fl
->fl_end
= req
->a_res
.lock
.fl
.fl_start
;
420 fl
->fl_type
= req
->a_res
.lock
.fl
.fl_type
;
424 status
= nlm_stat_to_errno(req
->a_res
.status
);
427 nlm_release_call(req
);
431 static void nlmclnt_locks_copy_lock(struct file_lock
*new, struct file_lock
*fl
)
433 new->fl_u
.nfs_fl
.state
= fl
->fl_u
.nfs_fl
.state
;
434 new->fl_u
.nfs_fl
.owner
= nlm_get_lockowner(fl
->fl_u
.nfs_fl
.owner
);
435 list_add_tail(&new->fl_u
.nfs_fl
.list
, &fl
->fl_u
.nfs_fl
.owner
->host
->h_granted
);
438 static void nlmclnt_locks_release_private(struct file_lock
*fl
)
440 list_del(&fl
->fl_u
.nfs_fl
.list
);
441 nlm_put_lockowner(fl
->fl_u
.nfs_fl
.owner
);
444 static struct file_lock_operations nlmclnt_lock_ops
= {
445 .fl_copy_lock
= nlmclnt_locks_copy_lock
,
446 .fl_release_private
= nlmclnt_locks_release_private
,
449 static void nlmclnt_locks_init_private(struct file_lock
*fl
, struct nlm_host
*host
)
451 BUG_ON(fl
->fl_ops
!= NULL
);
452 fl
->fl_u
.nfs_fl
.state
= 0;
453 fl
->fl_u
.nfs_fl
.owner
= nlm_find_lockowner(host
, fl
->fl_owner
);
454 INIT_LIST_HEAD(&fl
->fl_u
.nfs_fl
.list
);
455 fl
->fl_ops
= &nlmclnt_lock_ops
;
458 static void do_vfs_lock(struct file_lock
*fl
)
461 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
463 res
= posix_lock_file_wait(fl
->fl_file
, fl
);
466 res
= flock_lock_file_wait(fl
->fl_file
, fl
);
472 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
477 * LOCK: Try to create a lock
479 * Programmer Harassment Alert
481 * When given a blocking lock request in a sync RPC call, the HPUX lockd
482 * will faithfully return LCK_BLOCKED but never cares to notify us when
483 * the lock could be granted. This way, our local process could hang
484 * around forever waiting for the callback.
486 * Solution A: Implement busy-waiting
487 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
489 * For now I am implementing solution A, because I hate the idea of
490 * re-implementing lockd for a third time in two months. The async
491 * calls shouldn't be too hard to do, however.
493 * This is one of the lovely things about standards in the NFS area:
494 * they're so soft and squishy you can't really blame HP for doing this.
497 nlmclnt_lock(struct nlm_rqst
*req
, struct file_lock
*fl
)
499 struct nlm_host
*host
= req
->a_host
;
500 struct nlm_res
*resp
= &req
->a_res
;
501 struct nlm_wait
*block
= NULL
;
502 int status
= -ENOLCK
;
504 if (!host
->h_monitored
&& nsm_monitor(host
) < 0) {
505 printk(KERN_NOTICE
"lockd: failed to monitor %s\n",
510 block
= nlmclnt_prepare_block(host
, fl
);
513 /* Reboot protection */
514 fl
->fl_u
.nfs_fl
.state
= host
->h_state
;
515 status
= nlmclnt_call(req
, NLMPROC_LOCK
);
518 if (!req
->a_args
.block
)
520 /* Did a reclaimer thread notify us of a server reboot? */
521 if (resp
->status
== NLM_LCK_DENIED_GRACE_PERIOD
)
523 if (resp
->status
!= NLM_LCK_BLOCKED
)
525 /* Wait on an NLM blocking lock */
526 status
= nlmclnt_block(block
, req
, NLMCLNT_POLL_TIMEOUT
);
527 /* if we were interrupted. Send a CANCEL request to the server
532 if (resp
->status
!= NLM_LCK_BLOCKED
)
536 if (resp
->status
== NLM_LCK_GRANTED
) {
537 down_read(&host
->h_rwsem
);
538 /* Check whether or not the server has rebooted */
539 if (fl
->fl_u
.nfs_fl
.state
!= host
->h_state
) {
540 up_read(&host
->h_rwsem
);
543 fl
->fl_flags
|= FL_SLEEP
;
544 /* Ensure the resulting lock will get added to granted list */
546 up_read(&host
->h_rwsem
);
548 status
= nlm_stat_to_errno(resp
->status
);
550 nlmclnt_finish_block(block
);
551 /* Cancel the blocked request if it is still pending */
552 if (resp
->status
== NLM_LCK_BLOCKED
)
553 nlmclnt_cancel(host
, req
->a_args
.block
, fl
);
555 nlm_release_call(req
);
560 * RECLAIM: Try to reclaim a lock
563 nlmclnt_reclaim(struct nlm_host
*host
, struct file_lock
*fl
)
565 struct nlm_rqst reqst
, *req
;
569 memset(req
, 0, sizeof(*req
));
570 locks_init_lock(&req
->a_args
.lock
.fl
);
571 locks_init_lock(&req
->a_res
.lock
.fl
);
575 /* Set up the argument struct */
576 nlmclnt_setlockargs(req
, fl
);
577 req
->a_args
.reclaim
= 1;
579 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0
580 && req
->a_res
.status
== NLM_LCK_GRANTED
)
583 printk(KERN_WARNING
"lockd: failed to reclaim lock for pid %d "
584 "(errno %d, status %d)\n", fl
->fl_pid
,
585 status
, req
->a_res
.status
);
588 * FIXME: This is a serious failure. We can
590 * a. Ignore the problem
591 * b. Send the owning process some signal (Linux doesn't have
592 * SIGLOST, though...)
593 * c. Retry the operation
595 * Until someone comes up with a simple implementation
596 * for b or c, I'll choose option a.
603 * UNLOCK: remove an existing lock
606 nlmclnt_unlock(struct nlm_rqst
*req
, struct file_lock
*fl
)
608 struct nlm_host
*host
= req
->a_host
;
609 struct nlm_res
*resp
= &req
->a_res
;
613 * Note: the server is supposed to either grant us the unlock
614 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
615 * case, we want to unlock.
617 down_read(&host
->h_rwsem
);
619 up_read(&host
->h_rwsem
);
621 if (req
->a_flags
& RPC_TASK_ASYNC
)
622 return nlm_async_call(req
, NLMPROC_UNLOCK
, &nlmclnt_unlock_ops
);
624 status
= nlmclnt_call(req
, NLMPROC_UNLOCK
);
629 if (resp
->status
== NLM_LCK_GRANTED
)
632 if (resp
->status
!= NLM_LCK_DENIED_NOLOCKS
)
633 printk("lockd: unexpected unlock status: %d\n", resp
->status
);
634 /* What to do now? I'm out of my depth... */
637 nlm_release_call(req
);
641 static void nlmclnt_unlock_callback(struct rpc_task
*task
, void *data
)
643 struct nlm_rqst
*req
= data
;
644 int status
= req
->a_res
.status
;
646 if (RPC_ASSASSINATED(task
))
649 if (task
->tk_status
< 0) {
650 dprintk("lockd: unlock failed (err = %d)\n", -task
->tk_status
);
653 if (status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
654 rpc_delay(task
, NLMCLNT_GRACE_WAIT
);
657 if (status
!= NLM_LCK_GRANTED
)
658 printk(KERN_WARNING
"lockd: unexpected unlock status: %d\n", status
);
662 nlm_rebind_host(req
->a_host
);
664 rpc_restart_call(task
);
667 static const struct rpc_call_ops nlmclnt_unlock_ops
= {
668 .rpc_call_done
= nlmclnt_unlock_callback
,
669 .rpc_release
= nlmclnt_rpc_release
,
673 * Cancel a blocked lock request.
674 * We always use an async RPC call for this in order not to hang a
675 * process that has been Ctrl-C'ed.
677 static int nlmclnt_cancel(struct nlm_host
*host
, int block
, struct file_lock
*fl
)
679 struct nlm_rqst
*req
;
684 /* Block all signals while setting up call */
685 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
686 oldset
= current
->blocked
;
687 sigfillset(¤t
->blocked
);
689 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
691 req
= nlm_alloc_call(nlm_get_host(host
));
694 req
->a_flags
= RPC_TASK_ASYNC
;
696 nlmclnt_setlockargs(req
, fl
);
697 req
->a_args
.block
= block
;
699 status
= nlm_async_call(req
, NLMPROC_CANCEL
, &nlmclnt_cancel_ops
);
701 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
702 current
->blocked
= oldset
;
704 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
709 static void nlmclnt_cancel_callback(struct rpc_task
*task
, void *data
)
711 struct nlm_rqst
*req
= data
;
713 if (RPC_ASSASSINATED(task
))
716 if (task
->tk_status
< 0) {
717 dprintk("lockd: CANCEL call error %d, retrying.\n",
722 dprintk("lockd: cancel status %d (task %d)\n",
723 req
->a_res
.status
, task
->tk_pid
);
725 switch (req
->a_res
.status
) {
726 case NLM_LCK_GRANTED
:
727 case NLM_LCK_DENIED_GRACE_PERIOD
:
729 /* Everything's good */
731 case NLM_LCK_DENIED_NOLOCKS
:
732 dprintk("lockd: CANCEL failed (server has no locks)\n");
735 printk(KERN_NOTICE
"lockd: weird return %d for CANCEL call\n",
743 /* Don't ever retry more than 3 times */
744 if (req
->a_retries
++ >= NLMCLNT_MAX_RETRIES
)
746 nlm_rebind_host(req
->a_host
);
747 rpc_restart_call(task
);
748 rpc_delay(task
, 30 * HZ
);
751 static const struct rpc_call_ops nlmclnt_cancel_ops
= {
752 .rpc_call_done
= nlmclnt_cancel_callback
,
753 .rpc_release
= nlmclnt_rpc_release
,
757 * Convert an NLM status code to a generic kernel errno
760 nlm_stat_to_errno(u32 status
)
763 case NLM_LCK_GRANTED
:
767 case NLM_LCK_DENIED_NOLOCKS
:
768 case NLM_LCK_DENIED_GRACE_PERIOD
:
770 case NLM_LCK_BLOCKED
:
771 printk(KERN_NOTICE
"lockd: unexpected status NLM_BLOCKED\n");
773 #ifdef CONFIG_LOCKD_V4
786 printk(KERN_NOTICE
"lockd: unexpected server status %d\n", status
);