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/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>
21 #define NLMDBG_FACILITY NLMDBG_CLIENT
22 #define NLMCLNT_GRACE_WAIT (5*HZ)
24 static int nlmclnt_test(struct nlm_rqst
*, struct file_lock
*);
25 static int nlmclnt_lock(struct nlm_rqst
*, struct file_lock
*);
26 static int nlmclnt_unlock(struct nlm_rqst
*, struct file_lock
*);
27 static void nlmclnt_unlock_callback(struct rpc_task
*);
28 static void nlmclnt_cancel_callback(struct rpc_task
*);
29 static int nlm_stat_to_errno(u32 stat
);
30 static void nlmclnt_locks_init_private(struct file_lock
*fl
, struct nlm_host
*host
);
33 * Cookie counter for NLM requests
35 static u32 nlm_cookie
= 0x1234;
37 static inline void nlmclnt_next_cookie(struct nlm_cookie
*c
)
39 memcpy(c
->data
, &nlm_cookie
, 4);
40 memset(c
->data
+4, 0, 4);
45 static struct nlm_lockowner
*nlm_get_lockowner(struct nlm_lockowner
*lockowner
)
47 atomic_inc(&lockowner
->count
);
51 static void nlm_put_lockowner(struct nlm_lockowner
*lockowner
)
53 if (!atomic_dec_and_lock(&lockowner
->count
, &lockowner
->host
->h_lock
))
55 list_del(&lockowner
->list
);
56 spin_unlock(&lockowner
->host
->h_lock
);
57 nlm_release_host(lockowner
->host
);
61 static inline int nlm_pidbusy(struct nlm_host
*host
, uint32_t pid
)
63 struct nlm_lockowner
*lockowner
;
64 list_for_each_entry(lockowner
, &host
->h_lockowners
, list
) {
65 if (lockowner
->pid
== pid
)
71 static inline uint32_t __nlm_alloc_pid(struct nlm_host
*host
)
75 res
= host
->h_pidcount
++;
76 } while (nlm_pidbusy(host
, res
) < 0);
80 static struct nlm_lockowner
*__nlm_find_lockowner(struct nlm_host
*host
, fl_owner_t owner
)
82 struct nlm_lockowner
*lockowner
;
83 list_for_each_entry(lockowner
, &host
->h_lockowners
, list
) {
84 if (lockowner
->owner
!= owner
)
86 return nlm_get_lockowner(lockowner
);
91 static struct nlm_lockowner
*nlm_find_lockowner(struct nlm_host
*host
, fl_owner_t owner
)
93 struct nlm_lockowner
*res
, *new = NULL
;
95 spin_lock(&host
->h_lock
);
96 res
= __nlm_find_lockowner(host
, owner
);
98 spin_unlock(&host
->h_lock
);
99 new = (struct nlm_lockowner
*)kmalloc(sizeof(*new), GFP_KERNEL
);
100 spin_lock(&host
->h_lock
);
101 res
= __nlm_find_lockowner(host
, owner
);
102 if (res
== NULL
&& new != NULL
) {
104 atomic_set(&new->count
, 1);
106 new->pid
= __nlm_alloc_pid(host
);
107 new->host
= nlm_get_host(host
);
108 list_add(&new->list
, &host
->h_lockowners
);
112 spin_unlock(&host
->h_lock
);
119 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
121 static void nlmclnt_setlockargs(struct nlm_rqst
*req
, struct file_lock
*fl
)
123 struct nlm_args
*argp
= &req
->a_args
;
124 struct nlm_lock
*lock
= &argp
->lock
;
126 nlmclnt_next_cookie(&argp
->cookie
);
127 argp
->state
= nsm_local_state
;
128 memcpy(&lock
->fh
, NFS_FH(fl
->fl_file
->f_dentry
->d_inode
), sizeof(struct nfs_fh
));
129 lock
->caller
= system_utsname
.nodename
;
130 lock
->oh
.data
= req
->a_owner
;
131 lock
->oh
.len
= sprintf(req
->a_owner
, "%d@%s",
132 current
->pid
, system_utsname
.nodename
);
133 locks_copy_lock(&lock
->fl
, fl
);
136 static void nlmclnt_release_lockargs(struct nlm_rqst
*req
)
138 struct file_lock
*fl
= &req
->a_args
.lock
.fl
;
140 if (fl
->fl_ops
&& fl
->fl_ops
->fl_release_private
)
141 fl
->fl_ops
->fl_release_private(fl
);
145 * Initialize arguments for GRANTED call. The nlm_rqst structure
146 * has been cleared already.
149 nlmclnt_setgrantargs(struct nlm_rqst
*call
, struct nlm_lock
*lock
)
151 locks_copy_lock(&call
->a_args
.lock
.fl
, &lock
->fl
);
152 memcpy(&call
->a_args
.lock
.fh
, &lock
->fh
, sizeof(call
->a_args
.lock
.fh
));
153 call
->a_args
.lock
.caller
= system_utsname
.nodename
;
154 call
->a_args
.lock
.oh
.len
= lock
->oh
.len
;
156 /* set default data area */
157 call
->a_args
.lock
.oh
.data
= call
->a_owner
;
159 if (lock
->oh
.len
> NLMCLNT_OHSIZE
) {
160 void *data
= kmalloc(lock
->oh
.len
, GFP_KERNEL
);
162 nlmclnt_freegrantargs(call
);
165 call
->a_args
.lock
.oh
.data
= (u8
*) data
;
168 memcpy(call
->a_args
.lock
.oh
.data
, lock
->oh
.data
, lock
->oh
.len
);
173 nlmclnt_freegrantargs(struct nlm_rqst
*call
)
175 struct file_lock
*fl
= &call
->a_args
.lock
.fl
;
177 * Check whether we allocated memory for the owner.
179 if (call
->a_args
.lock
.oh
.data
!= (u8
*) call
->a_owner
) {
180 kfree(call
->a_args
.lock
.oh
.data
);
182 if (fl
->fl_ops
&& fl
->fl_ops
->fl_release_private
)
183 fl
->fl_ops
->fl_release_private(fl
);
187 * This is the main entry point for the NLM client.
190 nlmclnt_proc(struct inode
*inode
, int cmd
, struct file_lock
*fl
)
192 struct nfs_server
*nfssrv
= NFS_SERVER(inode
);
193 struct nlm_host
*host
;
194 struct nlm_rqst reqst
, *call
= &reqst
;
197 int status
, proto
, vers
;
199 vers
= (NFS_PROTO(inode
)->version
== 3) ? 4 : 1;
200 if (NFS_PROTO(inode
)->version
> 3) {
201 printk(KERN_NOTICE
"NFSv4 file locking not implemented!\n");
205 /* Retrieve transport protocol from NFS client */
206 proto
= NFS_CLIENT(inode
)->cl_xprt
->prot
;
208 if (!(host
= nlmclnt_lookup_host(NFS_ADDR(inode
), proto
, vers
)))
211 /* Create RPC client handle if not there, and copy soft
212 * and intr flags from NFS client. */
213 if (host
->h_rpcclnt
== NULL
) {
214 struct rpc_clnt
*clnt
;
216 /* Bind an rpc client to this host handle (does not
217 * perform a portmapper lookup) */
218 if (!(clnt
= nlm_bind_host(host
))) {
222 clnt
->cl_softrtry
= nfssrv
->client
->cl_softrtry
;
223 clnt
->cl_intr
= nfssrv
->client
->cl_intr
;
224 clnt
->cl_chatty
= nfssrv
->client
->cl_chatty
;
227 /* Keep the old signal mask */
228 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
229 oldset
= current
->blocked
;
231 /* If we're cleaning up locks because the process is exiting,
232 * perform the RPC call asynchronously. */
233 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
))
234 && fl
->fl_type
== F_UNLCK
235 && (current
->flags
& PF_EXITING
)) {
236 sigfillset(¤t
->blocked
); /* Mask all signals */
238 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
240 call
= nlmclnt_alloc_call();
245 call
->a_flags
= RPC_TASK_ASYNC
;
247 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
248 memset(call
, 0, sizeof(*call
));
249 locks_init_lock(&call
->a_args
.lock
.fl
);
250 locks_init_lock(&call
->a_res
.lock
.fl
);
254 nlmclnt_locks_init_private(fl
, host
);
256 /* Set up the argument struct */
257 nlmclnt_setlockargs(call
, fl
);
259 if (IS_SETLK(cmd
) || IS_SETLKW(cmd
)) {
260 if (fl
->fl_type
!= F_UNLCK
) {
261 call
->a_args
.block
= IS_SETLKW(cmd
) ? 1 : 0;
262 status
= nlmclnt_lock(call
, fl
);
264 status
= nlmclnt_unlock(call
, fl
);
265 } else if (IS_GETLK(cmd
))
266 status
= nlmclnt_test(call
, fl
);
271 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
272 current
->blocked
= oldset
;
274 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
277 dprintk("lockd: clnt proc returns %d\n", status
);
278 nlm_release_host(host
);
283 * Allocate an NLM RPC call struct
286 nlmclnt_alloc_call(void)
288 struct nlm_rqst
*call
;
290 while (!signalled()) {
291 call
= (struct nlm_rqst
*) kmalloc(sizeof(struct nlm_rqst
), GFP_KERNEL
);
293 memset(call
, 0, sizeof(*call
));
294 locks_init_lock(&call
->a_args
.lock
.fl
);
295 locks_init_lock(&call
->a_res
.lock
.fl
);
298 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
299 current
->state
= TASK_INTERRUPTIBLE
;
300 schedule_timeout(5*HZ
);
305 static int nlm_wait_on_grace(wait_queue_head_t
*queue
)
310 prepare_to_wait(queue
, &wait
, TASK_INTERRUPTIBLE
);
312 schedule_timeout(NLMCLNT_GRACE_WAIT
);
316 finish_wait(queue
, &wait
);
324 nlmclnt_call(struct nlm_rqst
*req
, u32 proc
)
326 struct nlm_host
*host
= req
->a_host
;
327 struct rpc_clnt
*clnt
;
328 struct nlm_args
*argp
= &req
->a_args
;
329 struct nlm_res
*resp
= &req
->a_res
;
330 struct file
*filp
= argp
->lock
.fl
.fl_file
;
331 struct rpc_message msg
= {
337 dprintk("lockd: call procedure %d on %s\n",
338 (int)proc
, host
->h_name
);
341 msg
.rpc_cred
= nfs_file_cred(filp
);
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.
405 nlmsvc_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
407 struct nlm_host
*host
= req
->a_host
;
408 struct rpc_clnt
*clnt
;
409 struct rpc_message msg
= {
410 .rpc_argp
= &req
->a_args
,
411 .rpc_resp
= &req
->a_res
,
415 dprintk("lockd: call procedure %d on %s (async)\n",
416 (int)proc
, host
->h_name
);
418 /* If we have no RPC client yet, create one. */
419 if ((clnt
= nlm_bind_host(host
)) == NULL
)
421 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
423 /* bootstrap and kick off the async RPC call */
424 status
= rpc_call_async(clnt
, &msg
, RPC_TASK_ASYNC
, callback
, req
);
430 nlmclnt_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
432 struct nlm_host
*host
= req
->a_host
;
433 struct rpc_clnt
*clnt
;
434 struct nlm_args
*argp
= &req
->a_args
;
435 struct nlm_res
*resp
= &req
->a_res
;
436 struct file
*file
= argp
->lock
.fl
.fl_file
;
437 struct rpc_message msg
= {
443 dprintk("lockd: call procedure %d on %s (async)\n",
444 (int)proc
, host
->h_name
);
446 /* If we have no RPC client yet, create one. */
447 if ((clnt
= nlm_bind_host(host
)) == NULL
)
449 msg
.rpc_proc
= &clnt
->cl_procinfo
[proc
];
451 /* bootstrap and kick off the async RPC call */
453 msg
.rpc_cred
= nfs_file_cred(file
);
454 /* Increment host refcount */
456 status
= rpc_call_async(clnt
, &msg
, RPC_TASK_ASYNC
, callback
, req
);
458 nlm_release_host(host
);
463 * TEST for the presence of a conflicting lock
466 nlmclnt_test(struct nlm_rqst
*req
, struct file_lock
*fl
)
470 status
= nlmclnt_call(req
, NLMPROC_TEST
);
471 nlmclnt_release_lockargs(req
);
475 status
= req
->a_res
.status
;
476 if (status
== NLM_LCK_GRANTED
) {
477 fl
->fl_type
= F_UNLCK
;
478 } if (status
== NLM_LCK_DENIED
) {
480 * Report the conflicting lock back to the application.
482 locks_copy_lock(fl
, &req
->a_res
.lock
.fl
);
485 return nlm_stat_to_errno(req
->a_res
.status
);
491 static void nlmclnt_locks_copy_lock(struct file_lock
*new, struct file_lock
*fl
)
493 memcpy(&new->fl_u
.nfs_fl
, &fl
->fl_u
.nfs_fl
, sizeof(new->fl_u
.nfs_fl
));
494 nlm_get_lockowner(new->fl_u
.nfs_fl
.owner
);
497 static void nlmclnt_locks_release_private(struct file_lock
*fl
)
499 nlm_put_lockowner(fl
->fl_u
.nfs_fl
.owner
);
503 static struct file_lock_operations nlmclnt_lock_ops
= {
504 .fl_copy_lock
= nlmclnt_locks_copy_lock
,
505 .fl_release_private
= nlmclnt_locks_release_private
,
508 static void nlmclnt_locks_init_private(struct file_lock
*fl
, struct nlm_host
*host
)
510 BUG_ON(fl
->fl_ops
!= NULL
);
511 fl
->fl_u
.nfs_fl
.state
= 0;
512 fl
->fl_u
.nfs_fl
.flags
= 0;
513 fl
->fl_u
.nfs_fl
.owner
= nlm_find_lockowner(host
, fl
->fl_owner
);
514 fl
->fl_ops
= &nlmclnt_lock_ops
;
518 * LOCK: Try to create a lock
520 * Programmer Harassment Alert
522 * When given a blocking lock request in a sync RPC call, the HPUX lockd
523 * will faithfully return LCK_BLOCKED but never cares to notify us when
524 * the lock could be granted. This way, our local process could hang
525 * around forever waiting for the callback.
527 * Solution A: Implement busy-waiting
528 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
530 * For now I am implementing solution A, because I hate the idea of
531 * re-implementing lockd for a third time in two months. The async
532 * calls shouldn't be too hard to do, however.
534 * This is one of the lovely things about standards in the NFS area:
535 * they're so soft and squishy you can't really blame HP for doing this.
538 nlmclnt_lock(struct nlm_rqst
*req
, struct file_lock
*fl
)
540 struct nlm_host
*host
= req
->a_host
;
541 struct nlm_res
*resp
= &req
->a_res
;
544 if (!host
->h_monitored
&& nsm_monitor(host
) < 0) {
545 printk(KERN_NOTICE
"lockd: failed to monitor %s\n",
552 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0) {
553 if (resp
->status
!= NLM_LCK_BLOCKED
)
555 status
= nlmclnt_block(host
, fl
, &resp
->status
);
559 } while (resp
->status
== NLM_LCK_BLOCKED
&& req
->a_args
.block
);
561 if (resp
->status
== NLM_LCK_GRANTED
) {
562 fl
->fl_u
.nfs_fl
.state
= host
->h_state
;
563 fl
->fl_u
.nfs_fl
.flags
|= NFS_LCK_GRANTED
;
564 fl
->fl_flags
|= FL_SLEEP
;
565 if (posix_lock_file_wait(fl
->fl_file
, fl
) < 0)
566 printk(KERN_WARNING
"%s: VFS is out of sync with lock manager!\n",
569 status
= nlm_stat_to_errno(resp
->status
);
571 nlmclnt_release_lockargs(req
);
576 * RECLAIM: Try to reclaim a lock
579 nlmclnt_reclaim(struct nlm_host
*host
, struct file_lock
*fl
)
581 struct nlm_rqst reqst
, *req
;
585 memset(req
, 0, sizeof(*req
));
586 locks_init_lock(&req
->a_args
.lock
.fl
);
587 locks_init_lock(&req
->a_res
.lock
.fl
);
591 /* Set up the argument struct */
592 nlmclnt_setlockargs(req
, fl
);
593 req
->a_args
.reclaim
= 1;
595 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0
596 && req
->a_res
.status
== NLM_LCK_GRANTED
)
599 printk(KERN_WARNING
"lockd: failed to reclaim lock for pid %d "
600 "(errno %d, status %d)\n", fl
->fl_pid
,
601 status
, req
->a_res
.status
);
604 * FIXME: This is a serious failure. We can
606 * a. Ignore the problem
607 * b. Send the owning process some signal (Linux doesn't have
608 * SIGLOST, though...)
609 * c. Retry the operation
611 * Until someone comes up with a simple implementation
612 * for b or c, I'll choose option a.
619 * UNLOCK: remove an existing lock
622 nlmclnt_unlock(struct nlm_rqst
*req
, struct file_lock
*fl
)
624 struct nlm_res
*resp
= &req
->a_res
;
627 /* Clean the GRANTED flag now so the lock doesn't get
628 * reclaimed while we're stuck in the unlock call. */
629 fl
->fl_u
.nfs_fl
.flags
&= ~NFS_LCK_GRANTED
;
631 if (req
->a_flags
& RPC_TASK_ASYNC
) {
632 status
= nlmclnt_async_call(req
, NLMPROC_UNLOCK
,
633 nlmclnt_unlock_callback
);
634 /* Hrmf... Do the unlock early since locks_remove_posix()
635 * really expects us to free the lock synchronously */
636 posix_lock_file(fl
->fl_file
, fl
);
638 nlmclnt_release_lockargs(req
);
644 status
= nlmclnt_call(req
, NLMPROC_UNLOCK
);
645 nlmclnt_release_lockargs(req
);
649 posix_lock_file(fl
->fl_file
, fl
);
650 if (resp
->status
== NLM_LCK_GRANTED
)
653 if (resp
->status
!= NLM_LCK_DENIED_NOLOCKS
)
654 printk("lockd: unexpected unlock status: %d\n", resp
->status
);
656 /* What to do now? I'm out of my depth... */
662 nlmclnt_unlock_callback(struct rpc_task
*task
)
664 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
665 int status
= req
->a_res
.status
;
667 if (RPC_ASSASSINATED(task
))
670 if (task
->tk_status
< 0) {
671 dprintk("lockd: unlock failed (err = %d)\n", -task
->tk_status
);
674 if (status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
675 rpc_delay(task
, NLMCLNT_GRACE_WAIT
);
678 if (status
!= NLM_LCK_GRANTED
)
679 printk(KERN_WARNING
"lockd: unexpected unlock status: %d\n", status
);
681 nlm_release_host(req
->a_host
);
682 nlmclnt_release_lockargs(req
);
686 nlm_rebind_host(req
->a_host
);
688 rpc_restart_call(task
);
692 * Cancel a blocked lock request.
693 * We always use an async RPC call for this in order not to hang a
694 * process that has been Ctrl-C'ed.
697 nlmclnt_cancel(struct nlm_host
*host
, struct file_lock
*fl
)
699 struct nlm_rqst
*req
;
704 /* Block all signals while setting up call */
705 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
706 oldset
= current
->blocked
;
707 sigfillset(¤t
->blocked
);
709 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
711 req
= nlmclnt_alloc_call();
715 req
->a_flags
= RPC_TASK_ASYNC
;
717 nlmclnt_setlockargs(req
, fl
);
719 status
= nlmclnt_async_call(req
, NLMPROC_CANCEL
,
720 nlmclnt_cancel_callback
);
722 nlmclnt_release_lockargs(req
);
726 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
727 current
->blocked
= oldset
;
729 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
735 nlmclnt_cancel_callback(struct rpc_task
*task
)
737 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
739 if (RPC_ASSASSINATED(task
))
742 if (task
->tk_status
< 0) {
743 dprintk("lockd: CANCEL call error %d, retrying.\n",
748 dprintk("lockd: cancel status %d (task %d)\n",
749 req
->a_res
.status
, task
->tk_pid
);
751 switch (req
->a_res
.status
) {
752 case NLM_LCK_GRANTED
:
753 case NLM_LCK_DENIED_GRACE_PERIOD
:
754 /* Everything's good */
756 case NLM_LCK_DENIED_NOLOCKS
:
757 dprintk("lockd: CANCEL failed (server has no locks)\n");
760 printk(KERN_NOTICE
"lockd: weird return %d for CANCEL call\n",
765 nlm_release_host(req
->a_host
);
766 nlmclnt_release_lockargs(req
);
771 nlm_rebind_host(req
->a_host
);
772 rpc_restart_call(task
);
773 rpc_delay(task
, 30 * HZ
);
777 * Convert an NLM status code to a generic kernel errno
780 nlm_stat_to_errno(u32 status
)
783 case NLM_LCK_GRANTED
:
787 case NLM_LCK_DENIED_NOLOCKS
:
788 case NLM_LCK_DENIED_GRACE_PERIOD
:
790 case NLM_LCK_BLOCKED
:
791 printk(KERN_NOTICE
"lockd: unexpected status NLM_BLOCKED\n");
793 #ifdef CONFIG_LOCKD_V4
806 printk(KERN_NOTICE
"lockd: unexpected server status %d\n", status
);