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)
25 static int nlmclnt_test(struct nlm_rqst
*, struct file_lock
*);
26 static int nlmclnt_lock(struct nlm_rqst
*, struct file_lock
*);
27 static int nlmclnt_unlock(struct nlm_rqst
*, struct file_lock
*);
28 static void nlmclnt_unlock_callback(struct rpc_task
*);
29 static void nlmclnt_cancel_callback(struct rpc_task
*);
30 static int nlm_stat_to_errno(u32 stat
);
31 static void nlmclnt_locks_init_private(struct file_lock
*fl
, struct nlm_host
*host
);
34 * Cookie counter for NLM requests
36 static u32 nlm_cookie
= 0x1234;
38 static inline void nlmclnt_next_cookie(struct nlm_cookie
*c
)
40 memcpy(c
->data
, &nlm_cookie
, 4);
41 memset(c
->data
+4, 0, 4);
46 static struct nlm_lockowner
*nlm_get_lockowner(struct nlm_lockowner
*lockowner
)
48 atomic_inc(&lockowner
->count
);
52 static void nlm_put_lockowner(struct nlm_lockowner
*lockowner
)
54 if (!atomic_dec_and_lock(&lockowner
->count
, &lockowner
->host
->h_lock
))
56 list_del(&lockowner
->list
);
57 spin_unlock(&lockowner
->host
->h_lock
);
58 nlm_release_host(lockowner
->host
);
62 static inline int nlm_pidbusy(struct nlm_host
*host
, uint32_t pid
)
64 struct nlm_lockowner
*lockowner
;
65 list_for_each_entry(lockowner
, &host
->h_lockowners
, list
) {
66 if (lockowner
->pid
== pid
)
72 static inline uint32_t __nlm_alloc_pid(struct nlm_host
*host
)
76 res
= host
->h_pidcount
++;
77 } while (nlm_pidbusy(host
, res
) < 0);
81 static struct nlm_lockowner
*__nlm_find_lockowner(struct nlm_host
*host
, fl_owner_t owner
)
83 struct nlm_lockowner
*lockowner
;
84 list_for_each_entry(lockowner
, &host
->h_lockowners
, list
) {
85 if (lockowner
->owner
!= owner
)
87 return nlm_get_lockowner(lockowner
);
92 static struct nlm_lockowner
*nlm_find_lockowner(struct nlm_host
*host
, fl_owner_t owner
)
94 struct nlm_lockowner
*res
, *new = NULL
;
96 spin_lock(&host
->h_lock
);
97 res
= __nlm_find_lockowner(host
, owner
);
99 spin_unlock(&host
->h_lock
);
100 new = (struct nlm_lockowner
*)kmalloc(sizeof(*new), GFP_KERNEL
);
101 spin_lock(&host
->h_lock
);
102 res
= __nlm_find_lockowner(host
, owner
);
103 if (res
== NULL
&& new != NULL
) {
105 atomic_set(&new->count
, 1);
107 new->pid
= __nlm_alloc_pid(host
);
108 new->host
= nlm_get_host(host
);
109 list_add(&new->list
, &host
->h_lockowners
);
113 spin_unlock(&host
->h_lock
);
120 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
122 static void nlmclnt_setlockargs(struct nlm_rqst
*req
, struct file_lock
*fl
)
124 struct nlm_args
*argp
= &req
->a_args
;
125 struct nlm_lock
*lock
= &argp
->lock
;
127 nlmclnt_next_cookie(&argp
->cookie
);
128 argp
->state
= nsm_local_state
;
129 memcpy(&lock
->fh
, NFS_FH(fl
->fl_file
->f_dentry
->d_inode
), sizeof(struct nfs_fh
));
130 lock
->caller
= system_utsname
.nodename
;
131 lock
->oh
.data
= req
->a_owner
;
132 lock
->oh
.len
= sprintf(req
->a_owner
, "%d@%s",
133 current
->pid
, system_utsname
.nodename
);
134 locks_copy_lock(&lock
->fl
, fl
);
137 static void nlmclnt_release_lockargs(struct nlm_rqst
*req
)
139 struct file_lock
*fl
= &req
->a_args
.lock
.fl
;
141 if (fl
->fl_ops
&& fl
->fl_ops
->fl_release_private
)
142 fl
->fl_ops
->fl_release_private(fl
);
146 * Initialize arguments for GRANTED call. The nlm_rqst structure
147 * has been cleared already.
150 nlmclnt_setgrantargs(struct nlm_rqst
*call
, struct nlm_lock
*lock
)
152 locks_copy_lock(&call
->a_args
.lock
.fl
, &lock
->fl
);
153 memcpy(&call
->a_args
.lock
.fh
, &lock
->fh
, sizeof(call
->a_args
.lock
.fh
));
154 call
->a_args
.lock
.caller
= system_utsname
.nodename
;
155 call
->a_args
.lock
.oh
.len
= lock
->oh
.len
;
157 /* set default data area */
158 call
->a_args
.lock
.oh
.data
= call
->a_owner
;
160 if (lock
->oh
.len
> NLMCLNT_OHSIZE
) {
161 void *data
= kmalloc(lock
->oh
.len
, GFP_KERNEL
);
163 nlmclnt_freegrantargs(call
);
166 call
->a_args
.lock
.oh
.data
= (u8
*) data
;
169 memcpy(call
->a_args
.lock
.oh
.data
, lock
->oh
.data
, lock
->oh
.len
);
174 nlmclnt_freegrantargs(struct nlm_rqst
*call
)
176 struct file_lock
*fl
= &call
->a_args
.lock
.fl
;
178 * Check whether we allocated memory for the owner.
180 if (call
->a_args
.lock
.oh
.data
!= (u8
*) call
->a_owner
) {
181 kfree(call
->a_args
.lock
.oh
.data
);
183 if (fl
->fl_ops
&& fl
->fl_ops
->fl_release_private
)
184 fl
->fl_ops
->fl_release_private(fl
);
188 * This is the main entry point for the NLM client.
191 nlmclnt_proc(struct inode
*inode
, int cmd
, struct file_lock
*fl
)
193 struct nfs_server
*nfssrv
= NFS_SERVER(inode
);
194 struct nlm_host
*host
;
195 struct nlm_rqst reqst
, *call
= &reqst
;
198 int status
, proto
, vers
;
200 vers
= (NFS_PROTO(inode
)->version
== 3) ? 4 : 1;
201 if (NFS_PROTO(inode
)->version
> 3) {
202 printk(KERN_NOTICE
"NFSv4 file locking not implemented!\n");
206 /* Retrieve transport protocol from NFS client */
207 proto
= NFS_CLIENT(inode
)->cl_xprt
->prot
;
209 if (!(host
= nlmclnt_lookup_host(NFS_ADDR(inode
), proto
, vers
)))
212 /* Create RPC client handle if not there, and copy soft
213 * and intr flags from NFS client. */
214 if (host
->h_rpcclnt
== NULL
) {
215 struct rpc_clnt
*clnt
;
217 /* Bind an rpc client to this host handle (does not
218 * perform a portmapper lookup) */
219 if (!(clnt
= nlm_bind_host(host
))) {
223 clnt
->cl_softrtry
= nfssrv
->client
->cl_softrtry
;
224 clnt
->cl_intr
= nfssrv
->client
->cl_intr
;
225 clnt
->cl_chatty
= nfssrv
->client
->cl_chatty
;
228 /* Keep the old signal mask */
229 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
230 oldset
= current
->blocked
;
232 /* If we're cleaning up locks because the process is exiting,
233 * perform the RPC call asynchronously. */
234 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
))
235 && fl
->fl_type
== F_UNLCK
236 && (current
->flags
& PF_EXITING
)) {
237 sigfillset(¤t
->blocked
); /* Mask all signals */
239 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
241 call
= nlmclnt_alloc_call();
246 call
->a_flags
= RPC_TASK_ASYNC
;
248 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
249 memset(call
, 0, sizeof(*call
));
250 locks_init_lock(&call
->a_args
.lock
.fl
);
251 locks_init_lock(&call
->a_res
.lock
.fl
);
255 nlmclnt_locks_init_private(fl
, host
);
257 /* Set up the argument struct */
258 nlmclnt_setlockargs(call
, fl
);
260 if (IS_SETLK(cmd
) || IS_SETLKW(cmd
)) {
261 if (fl
->fl_type
!= F_UNLCK
) {
262 call
->a_args
.block
= IS_SETLKW(cmd
) ? 1 : 0;
263 status
= nlmclnt_lock(call
, fl
);
265 status
= nlmclnt_unlock(call
, fl
);
266 } else if (IS_GETLK(cmd
))
267 status
= nlmclnt_test(call
, fl
);
272 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
273 current
->blocked
= oldset
;
275 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
278 dprintk("lockd: clnt proc returns %d\n", status
);
279 nlm_release_host(host
);
282 EXPORT_SYMBOL(nlmclnt_proc
);
285 * Allocate an NLM RPC call struct
288 nlmclnt_alloc_call(void)
290 struct nlm_rqst
*call
;
292 while (!signalled()) {
293 call
= (struct nlm_rqst
*) kmalloc(sizeof(struct nlm_rqst
), GFP_KERNEL
);
295 memset(call
, 0, sizeof(*call
));
296 locks_init_lock(&call
->a_args
.lock
.fl
);
297 locks_init_lock(&call
->a_res
.lock
.fl
);
300 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
301 current
->state
= TASK_INTERRUPTIBLE
;
302 schedule_timeout(5*HZ
);
307 static int nlm_wait_on_grace(wait_queue_head_t
*queue
)
312 prepare_to_wait(queue
, &wait
, TASK_INTERRUPTIBLE
);
314 schedule_timeout(NLMCLNT_GRACE_WAIT
);
315 try_to_freeze(PF_FREEZE
);
319 finish_wait(queue
, &wait
);
327 nlmclnt_call(struct nlm_rqst
*req
, u32 proc
)
329 struct nlm_host
*host
= req
->a_host
;
330 struct rpc_clnt
*clnt
;
331 struct nlm_args
*argp
= &req
->a_args
;
332 struct nlm_res
*resp
= &req
->a_res
;
333 struct rpc_message msg
= {
339 dprintk("lockd: call procedure %d on %s\n",
340 (int)proc
, host
->h_name
);
343 if (host
->h_reclaiming
&& !argp
->reclaim
)
344 goto in_grace_period
;
346 /* If we have no RPC client yet, create one. */
347 if ((clnt
= nlm_bind_host(host
)) == NULL
)
349 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
351 /* Perform the RPC call. If an error occurs, try again */
352 if ((status
= rpc_call_sync(clnt
, &msg
, 0)) < 0) {
353 dprintk("lockd: rpc_call returned error %d\n", -status
);
355 case -EPROTONOSUPPORT
:
361 nlm_rebind_host(host
);
365 return signalled () ? -EINTR
: status
;
371 if (resp
->status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
372 dprintk("lockd: server in grace period\n");
375 "lockd: spurious grace period reject?!\n");
379 if (!argp
->reclaim
) {
380 /* We appear to be out of the grace period */
381 wake_up_all(&host
->h_gracewait
);
383 dprintk("lockd: server returns status %d\n", resp
->status
);
384 return 0; /* Okay, call complete */
389 * The server has rebooted and appears to be in the grace
390 * period during which locks are only allowed to be
392 * We can only back off and try again later.
394 status
= nlm_wait_on_grace(&host
->h_gracewait
);
395 } while (status
== 0);
401 * Generic NLM call, async version.
404 nlmsvc_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
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
, callback
, req
);
429 nlmclnt_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
431 struct nlm_host
*host
= req
->a_host
;
432 struct rpc_clnt
*clnt
;
433 struct nlm_args
*argp
= &req
->a_args
;
434 struct nlm_res
*resp
= &req
->a_res
;
435 struct rpc_message msg
= {
441 dprintk("lockd: call procedure %d on %s (async)\n",
442 (int)proc
, host
->h_name
);
444 /* If we have no RPC client yet, create one. */
445 if ((clnt
= nlm_bind_host(host
)) == NULL
)
447 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
449 /* Increment host refcount */
451 /* bootstrap and kick off the async RPC call */
452 status
= rpc_call_async(clnt
, &msg
, RPC_TASK_ASYNC
, callback
, req
);
454 nlm_release_host(host
);
459 * TEST for the presence of a conflicting lock
462 nlmclnt_test(struct nlm_rqst
*req
, struct file_lock
*fl
)
466 status
= nlmclnt_call(req
, NLMPROC_TEST
);
467 nlmclnt_release_lockargs(req
);
471 status
= req
->a_res
.status
;
472 if (status
== NLM_LCK_GRANTED
) {
473 fl
->fl_type
= F_UNLCK
;
474 } if (status
== NLM_LCK_DENIED
) {
476 * Report the conflicting lock back to the application.
478 locks_copy_lock(fl
, &req
->a_res
.lock
.fl
);
481 return nlm_stat_to_errno(req
->a_res
.status
);
487 static void nlmclnt_locks_copy_lock(struct file_lock
*new, struct file_lock
*fl
)
489 memcpy(&new->fl_u
.nfs_fl
, &fl
->fl_u
.nfs_fl
, sizeof(new->fl_u
.nfs_fl
));
490 nlm_get_lockowner(new->fl_u
.nfs_fl
.owner
);
493 static void nlmclnt_locks_release_private(struct file_lock
*fl
)
495 nlm_put_lockowner(fl
->fl_u
.nfs_fl
.owner
);
499 static struct file_lock_operations nlmclnt_lock_ops
= {
500 .fl_copy_lock
= nlmclnt_locks_copy_lock
,
501 .fl_release_private
= nlmclnt_locks_release_private
,
504 static void nlmclnt_locks_init_private(struct file_lock
*fl
, struct nlm_host
*host
)
506 BUG_ON(fl
->fl_ops
!= NULL
);
507 fl
->fl_u
.nfs_fl
.state
= 0;
508 fl
->fl_u
.nfs_fl
.flags
= 0;
509 fl
->fl_u
.nfs_fl
.owner
= nlm_find_lockowner(host
, fl
->fl_owner
);
510 fl
->fl_ops
= &nlmclnt_lock_ops
;
513 static void do_vfs_lock(struct file_lock
*fl
)
516 switch (fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)) {
518 res
= posix_lock_file_wait(fl
->fl_file
, fl
);
521 res
= flock_lock_file_wait(fl
->fl_file
, fl
);
527 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
532 * LOCK: Try to create a lock
534 * Programmer Harassment Alert
536 * When given a blocking lock request in a sync RPC call, the HPUX lockd
537 * will faithfully return LCK_BLOCKED but never cares to notify us when
538 * the lock could be granted. This way, our local process could hang
539 * around forever waiting for the callback.
541 * Solution A: Implement busy-waiting
542 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
544 * For now I am implementing solution A, because I hate the idea of
545 * re-implementing lockd for a third time in two months. The async
546 * calls shouldn't be too hard to do, however.
548 * This is one of the lovely things about standards in the NFS area:
549 * they're so soft and squishy you can't really blame HP for doing this.
552 nlmclnt_lock(struct nlm_rqst
*req
, struct file_lock
*fl
)
554 struct nlm_host
*host
= req
->a_host
;
555 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",
566 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0) {
567 if (resp
->status
!= NLM_LCK_BLOCKED
)
569 status
= nlmclnt_block(host
, fl
, &resp
->status
);
573 } while (resp
->status
== NLM_LCK_BLOCKED
&& req
->a_args
.block
);
575 if (resp
->status
== NLM_LCK_GRANTED
) {
576 fl
->fl_u
.nfs_fl
.state
= host
->h_state
;
577 fl
->fl_u
.nfs_fl
.flags
|= NFS_LCK_GRANTED
;
578 fl
->fl_flags
|= FL_SLEEP
;
581 status
= nlm_stat_to_errno(resp
->status
);
583 nlmclnt_release_lockargs(req
);
588 * RECLAIM: Try to reclaim a lock
591 nlmclnt_reclaim(struct nlm_host
*host
, struct file_lock
*fl
)
593 struct nlm_rqst reqst
, *req
;
597 memset(req
, 0, sizeof(*req
));
598 locks_init_lock(&req
->a_args
.lock
.fl
);
599 locks_init_lock(&req
->a_res
.lock
.fl
);
603 /* Set up the argument struct */
604 nlmclnt_setlockargs(req
, fl
);
605 req
->a_args
.reclaim
= 1;
607 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0
608 && req
->a_res
.status
== NLM_LCK_GRANTED
)
611 printk(KERN_WARNING
"lockd: failed to reclaim lock for pid %d "
612 "(errno %d, status %d)\n", fl
->fl_pid
,
613 status
, req
->a_res
.status
);
616 * FIXME: This is a serious failure. We can
618 * a. Ignore the problem
619 * b. Send the owning process some signal (Linux doesn't have
620 * SIGLOST, though...)
621 * c. Retry the operation
623 * Until someone comes up with a simple implementation
624 * for b or c, I'll choose option a.
631 * UNLOCK: remove an existing lock
634 nlmclnt_unlock(struct nlm_rqst
*req
, struct file_lock
*fl
)
636 struct nlm_res
*resp
= &req
->a_res
;
639 /* Clean the GRANTED flag now so the lock doesn't get
640 * reclaimed while we're stuck in the unlock call. */
641 fl
->fl_u
.nfs_fl
.flags
&= ~NFS_LCK_GRANTED
;
643 if (req
->a_flags
& RPC_TASK_ASYNC
) {
644 status
= nlmclnt_async_call(req
, NLMPROC_UNLOCK
,
645 nlmclnt_unlock_callback
);
646 /* Hrmf... Do the unlock early since locks_remove_posix()
647 * really expects us to free the lock synchronously */
650 nlmclnt_release_lockargs(req
);
656 status
= nlmclnt_call(req
, NLMPROC_UNLOCK
);
657 nlmclnt_release_lockargs(req
);
662 if (resp
->status
== NLM_LCK_GRANTED
)
665 if (resp
->status
!= NLM_LCK_DENIED_NOLOCKS
)
666 printk("lockd: unexpected unlock status: %d\n", resp
->status
);
668 /* What to do now? I'm out of my depth... */
674 nlmclnt_unlock_callback(struct rpc_task
*task
)
676 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
677 int status
= req
->a_res
.status
;
679 if (RPC_ASSASSINATED(task
))
682 if (task
->tk_status
< 0) {
683 dprintk("lockd: unlock failed (err = %d)\n", -task
->tk_status
);
686 if (status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
687 rpc_delay(task
, NLMCLNT_GRACE_WAIT
);
690 if (status
!= NLM_LCK_GRANTED
)
691 printk(KERN_WARNING
"lockd: unexpected unlock status: %d\n", status
);
693 nlm_release_host(req
->a_host
);
694 nlmclnt_release_lockargs(req
);
698 nlm_rebind_host(req
->a_host
);
700 rpc_restart_call(task
);
704 * Cancel a blocked lock request.
705 * We always use an async RPC call for this in order not to hang a
706 * process that has been Ctrl-C'ed.
709 nlmclnt_cancel(struct nlm_host
*host
, struct file_lock
*fl
)
711 struct nlm_rqst
*req
;
716 /* Block all signals while setting up call */
717 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
718 oldset
= current
->blocked
;
719 sigfillset(¤t
->blocked
);
721 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
723 req
= nlmclnt_alloc_call();
727 req
->a_flags
= RPC_TASK_ASYNC
;
729 nlmclnt_setlockargs(req
, fl
);
731 status
= nlmclnt_async_call(req
, NLMPROC_CANCEL
,
732 nlmclnt_cancel_callback
);
734 nlmclnt_release_lockargs(req
);
738 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
739 current
->blocked
= oldset
;
741 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
747 nlmclnt_cancel_callback(struct rpc_task
*task
)
749 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
751 if (RPC_ASSASSINATED(task
))
754 if (task
->tk_status
< 0) {
755 dprintk("lockd: CANCEL call error %d, retrying.\n",
760 dprintk("lockd: cancel status %d (task %d)\n",
761 req
->a_res
.status
, task
->tk_pid
);
763 switch (req
->a_res
.status
) {
764 case NLM_LCK_GRANTED
:
765 case NLM_LCK_DENIED_GRACE_PERIOD
:
766 /* Everything's good */
768 case NLM_LCK_DENIED_NOLOCKS
:
769 dprintk("lockd: CANCEL failed (server has no locks)\n");
772 printk(KERN_NOTICE
"lockd: weird return %d for CANCEL call\n",
777 nlm_release_host(req
->a_host
);
778 nlmclnt_release_lockargs(req
);
783 nlm_rebind_host(req
->a_host
);
784 rpc_restart_call(task
);
785 rpc_delay(task
, 30 * HZ
);
789 * Convert an NLM status code to a generic kernel errno
792 nlm_stat_to_errno(u32 status
)
795 case NLM_LCK_GRANTED
:
799 case NLM_LCK_DENIED_NOLOCKS
:
800 case NLM_LCK_DENIED_GRACE_PERIOD
:
802 case NLM_LCK_BLOCKED
:
803 printk(KERN_NOTICE
"lockd: unexpected status NLM_BLOCKED\n");
805 #ifdef CONFIG_LOCKD_V4
818 printk(KERN_NOTICE
"lockd: unexpected server status %d\n", status
);