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/types.h>
10 #include <linux/errno.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/utsname.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/svc.h>
16 #include <linux/lockd/lockd.h>
17 #include <linux/lockd/sm_inter.h>
19 #define NLMDBG_FACILITY NLMDBG_CLIENT
21 static int nlmclnt_test(struct nlm_rqst
*, struct file_lock
*);
22 static int nlmclnt_lock(struct nlm_rqst
*, struct file_lock
*);
23 static int nlmclnt_unlock(struct nlm_rqst
*, struct file_lock
*);
24 static void nlmclnt_unlock_callback(struct rpc_task
*);
25 static void nlmclnt_cancel_callback(struct rpc_task
*);
26 static int nlm_stat_to_errno(u32 stat
);
29 * Cookie counter for NLM requests
31 static u32 nlm_cookie
= 0x1234;
33 static inline void nlmclnt_next_cookie(struct nlm_cookie
*c
)
35 memcpy(c
->data
, &nlm_cookie
, 4);
36 memset(c
->data
+4, 0, 4);
42 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
45 nlmclnt_setlockargs(struct nlm_rqst
*req
, struct file_lock
*fl
)
47 struct nlm_args
*argp
= &req
->a_args
;
48 struct nlm_lock
*lock
= &argp
->lock
;
50 memset(argp
, 0, sizeof(*argp
));
51 nlmclnt_next_cookie(&argp
->cookie
);
52 argp
->state
= nsm_local_state
;
53 lock
->fh
= *NFS_FH(fl
->fl_file
->f_dentry
);
54 lock
->caller
= system_utsname
.nodename
;
55 lock
->oh
.data
= req
->a_owner
;
56 lock
->oh
.len
= sprintf(req
->a_owner
, "%d@%s",
57 current
->pid
, system_utsname
.nodename
);
62 * Initialize arguments for GRANTED call. The nlm_rqst structure
63 * has been cleared already.
66 nlmclnt_setgrantargs(struct nlm_rqst
*call
, struct nlm_lock
*lock
)
68 nlmclnt_next_cookie(&call
->a_args
.cookie
);
69 call
->a_args
.lock
= *lock
;
70 call
->a_args
.lock
.caller
= system_utsname
.nodename
;
72 init_waitqueue_head(&lock
->fl
.fl_wait
);
73 /* set default data area */
74 call
->a_args
.lock
.oh
.data
= call
->a_owner
;
76 if (lock
->oh
.len
> NLMCLNT_OHSIZE
) {
77 void *data
= kmalloc(lock
->oh
.len
, GFP_KERNEL
);
80 call
->a_args
.lock
.oh
.data
= (u8
*) data
;
83 memcpy(call
->a_args
.lock
.oh
.data
, lock
->oh
.data
, lock
->oh
.len
);
88 nlmclnt_freegrantargs(struct nlm_rqst
*call
)
91 * Check whether we allocated memory for the owner.
93 if (call
->a_args
.lock
.oh
.data
!= (u8
*) call
->a_owner
) {
94 kfree(call
->a_args
.lock
.oh
.data
);
99 * This is the main entry point for the NLM client.
102 nlmclnt_proc(struct inode
*inode
, int cmd
, struct file_lock
*fl
)
104 struct nfs_server
*nfssrv
= NFS_SERVER(inode
);
105 struct nlm_host
*host
;
106 struct nlm_rqst reqst
, *call
= &reqst
;
111 /* Always use NLM version 1 over UDP for now... */
112 if (!(host
= nlmclnt_lookup_host(NFS_ADDR(inode
), IPPROTO_UDP
, 1)))
115 /* Create RPC client handle if not there, and copy soft
116 * and intr flags from NFS client. */
117 if (host
->h_rpcclnt
== NULL
) {
118 struct rpc_clnt
*clnt
;
120 /* Bind an rpc client to this host handle (does not
121 * perform a portmapper lookup) */
122 if (!(clnt
= nlm_bind_host(host
))) {
126 clnt
->cl_softrtry
= nfssrv
->client
->cl_softrtry
;
127 clnt
->cl_intr
= nfssrv
->client
->cl_intr
;
128 clnt
->cl_chatty
= nfssrv
->client
->cl_chatty
;
131 /* Keep the old signal mask */
132 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
133 oldset
= current
->blocked
;
135 /* If we're cleaning up locks because the process is exiting,
136 * perform the RPC call asynchronously. */
137 if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
)
138 && fl
->fl_type
== F_UNLCK
139 && (current
->flags
& PF_EXITING
)) {
140 sigfillset(¤t
->blocked
); /* Mask all signals */
141 recalc_sigpending(current
);
142 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
144 call
= nlmclnt_alloc_call();
145 call
->a_flags
= RPC_TASK_ASYNC
;
147 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
152 /* Set up the argument struct */
153 nlmclnt_setlockargs(call
, fl
);
155 if (cmd
== F_GETLK
) {
156 status
= nlmclnt_test(call
, fl
);
157 } else if ((cmd
== F_SETLK
|| cmd
== F_SETLKW
)
158 && fl
->fl_type
== F_UNLCK
) {
159 status
= nlmclnt_unlock(call
, fl
);
160 } else if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
161 call
->a_args
.block
= (cmd
== F_SETLKW
)? 1 : 0;
162 status
= nlmclnt_lock(call
, fl
);
167 if (status
< 0 && (call
->a_flags
& RPC_TASK_ASYNC
))
170 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
171 current
->blocked
= oldset
;
172 recalc_sigpending(current
);
173 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
176 dprintk("lockd: clnt proc returns %d\n", status
);
177 nlm_release_host(host
);
182 * Wait while server is in grace period
185 nlmclnt_grace_wait(struct nlm_host
*host
)
187 if (!host
->h_reclaiming
)
188 interruptible_sleep_on_timeout(&host
->h_gracewait
, 10*HZ
);
190 interruptible_sleep_on(&host
->h_gracewait
);
191 return signalled()? -ERESTARTSYS
: 0;
195 * Allocate an NLM RPC call struct
198 nlmclnt_alloc_call(void)
200 struct nlm_rqst
*call
;
202 while (!signalled()) {
203 call
= (struct nlm_rqst
*) rpc_allocate(RPC_TASK_ASYNC
,
204 sizeof(struct nlm_rqst
));
207 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
208 current
->state
= TASK_INTERRUPTIBLE
;
209 schedule_timeout(5*HZ
);
218 nlmclnt_call(struct nlm_rqst
*req
, u32 proc
)
220 struct nlm_host
*host
= req
->a_host
;
221 struct rpc_clnt
*clnt
;
222 struct nlm_args
*argp
= &req
->a_args
;
223 struct nlm_res
*resp
= &req
->a_res
;
226 dprintk("lockd: call procedure %s on %s\n",
227 nlm_procname(proc
), host
->h_name
);
230 if (host
->h_reclaiming
&& !argp
->reclaim
) {
231 interruptible_sleep_on(&host
->h_gracewait
);
235 /* If we have no RPC client yet, create one. */
236 if ((clnt
= nlm_bind_host(host
)) == NULL
)
239 /* Perform the RPC call. If an error occurs, try again */
240 if ((status
= rpc_call(clnt
, proc
, argp
, resp
, 0)) < 0) {
241 dprintk("lockd: rpc_call returned error %d\n", -status
);
243 case -EPROTONOSUPPORT
:
252 return signalled () ? -EINTR
: status
;
256 if (req
->a_args
.block
)
257 nlm_rebind_host(host
);
261 if (resp
->status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
262 dprintk("lockd: server in grace period\n");
265 "lockd: spurious grace period reject?!\n");
269 dprintk("lockd: server returns status %d\n", resp
->status
);
270 return 0; /* Okay, call complete */
273 /* Back off a little and try again */
274 interruptible_sleep_on_timeout(&host
->h_gracewait
, 15*HZ
);
276 /* When the lock requested by F_SETLKW isn't available,
277 we will wait until the request can be satisfied. If
278 a signal is received during wait, we should return
290 * Generic NLM call, async version.
293 nlmclnt_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
295 struct nlm_host
*host
= req
->a_host
;
296 struct rpc_clnt
*clnt
;
297 struct nlm_args
*argp
= &req
->a_args
;
298 struct nlm_res
*resp
= &req
->a_res
;
301 dprintk("lockd: call procedure %s on %s (async)\n",
302 nlm_procname(proc
), host
->h_name
);
304 /* If we have no RPC client yet, create one. */
305 if ((clnt
= nlm_bind_host(host
)) == NULL
)
308 /* bootstrap and kick off the async RPC call */
309 status
= rpc_do_call(clnt
, proc
, argp
, resp
, RPC_TASK_ASYNC
,
312 /* If the async call is proceeding, increment host refcount */
313 if (status
>= 0 && (req
->a_flags
& RPC_TASK_ASYNC
))
319 * TEST for the presence of a conflicting lock
322 nlmclnt_test(struct nlm_rqst
*req
, struct file_lock
*fl
)
326 if ((status
= nlmclnt_call(req
, NLMPROC_TEST
)) < 0)
329 status
= req
->a_res
.status
;
330 if (status
== NLM_LCK_GRANTED
) {
331 fl
->fl_type
= F_UNLCK
;
332 } if (status
== NLM_LCK_DENIED
) {
334 * Report the conflicting lock back to the application.
335 * FIXME: Is it OK to report the pid back as well?
337 memcpy(fl
, &req
->a_res
.lock
.fl
, sizeof(*fl
));
338 /* fl->fl_pid = 0; */
340 return nlm_stat_to_errno(req
->a_res
.status
);
347 * LOCK: Try to create a lock
349 * Programmer Harassment Alert
351 * When given a blocking lock request in a sync RPC call, the HPUX lockd
352 * will faithfully return LCK_BLOCKED but never cares to notify us when
353 * the lock could be granted. This way, our local process could hang
354 * around forever waiting for the callback.
356 * Solution A: Implement busy-waiting
357 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
359 * For now I am implementing solution A, because I hate the idea of
360 * re-implementing lockd for a third time in two months. The async
361 * calls shouldn't be too hard to do, however.
363 * This is one of the lovely things about standards in the NFS area:
364 * they're so soft and squishy you can't really blame HP for doing this.
367 nlmclnt_lock(struct nlm_rqst
*req
, struct file_lock
*fl
)
369 struct nlm_host
*host
= req
->a_host
;
370 struct nlm_res
*resp
= &req
->a_res
;
373 if (!host
->h_monitored
&& nsm_monitor(host
) < 0) {
374 printk(KERN_NOTICE
"lockd: failed to monitor %s\n",
380 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0) {
381 if (resp
->status
!= NLM_LCK_BLOCKED
)
383 status
= nlmclnt_block(host
, fl
, &resp
->status
);
389 if (resp
->status
== NLM_LCK_GRANTED
) {
390 fl
->fl_u
.nfs_fl
.state
= host
->h_state
;
391 fl
->fl_u
.nfs_fl
.flags
|= NFS_LCK_GRANTED
;
394 return nlm_stat_to_errno(resp
->status
);
398 * RECLAIM: Try to reclaim a lock
401 nlmclnt_reclaim(struct nlm_host
*host
, struct file_lock
*fl
)
403 struct nlm_rqst reqst
, *req
;
410 /* Set up the argument struct */
411 nlmclnt_setlockargs(req
, fl
);
412 req
->a_args
.reclaim
= 1;
414 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0
415 && req
->a_res
.status
== NLM_LCK_GRANTED
)
418 printk(KERN_WARNING
"lockd: failed to reclaim lock for pid %d "
419 "(errno %d, status %d)\n", fl
->fl_pid
,
420 status
, req
->a_res
.status
);
423 * FIXME: This is a serious failure. We can
425 * a. Ignore the problem
426 * b. Send the owning process some signal (Linux doesn't have
427 * SIGLOST, though...)
428 * c. Retry the operation
430 * Until someone comes up with a simple implementation
431 * for b or c, I'll choose option a.
438 * UNLOCK: remove an existing lock
441 nlmclnt_unlock(struct nlm_rqst
*req
, struct file_lock
*fl
)
443 struct nlm_host
*host
= req
->a_host
;
444 struct nlm_res
*resp
= &req
->a_res
;
447 /* No monitor, no lock: see nlmclnt_lock().
448 * Since this is an UNLOCK, don't try to setup monitoring here. */
449 if (!host
->h_monitored
)
452 /* Clean the GRANTED flag now so the lock doesn't get
453 * reclaimed while we're stuck in the unlock call. */
454 fl
->fl_u
.nfs_fl
.flags
&= ~NFS_LCK_GRANTED
;
456 if (req
->a_flags
& RPC_TASK_ASYNC
) {
457 return nlmclnt_async_call(req
, NLMPROC_UNLOCK
,
458 nlmclnt_unlock_callback
);
461 if ((status
= nlmclnt_call(req
, NLMPROC_UNLOCK
)) < 0)
464 if (resp
->status
== NLM_LCK_GRANTED
)
467 if (resp
->status
!= NLM_LCK_DENIED_NOLOCKS
)
468 printk("lockd: unexpected unlock status: %d\n", resp
->status
);
470 /* What to do now? I'm out of my depth... */
476 nlmclnt_unlock_callback(struct rpc_task
*task
)
478 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
479 int status
= req
->a_res
.status
;
481 if (RPC_ASSASSINATED(task
))
484 if (task
->tk_status
< 0) {
485 dprintk("lockd: unlock failed (err = %d)\n", -task
->tk_status
);
486 nlm_rebind_host(req
->a_host
);
487 rpc_restart_call(task
);
490 if (status
!= NLM_LCK_GRANTED
491 && status
!= NLM_LCK_DENIED_GRACE_PERIOD
) {
492 printk("lockd: unexpected unlock status: %d\n", status
);
496 rpc_release_task(task
);
500 * Cancel a blocked lock request.
501 * We always use an async RPC call for this in order not to hang a
502 * process that has been Ctrl-C'ed.
505 nlmclnt_cancel(struct nlm_host
*host
, struct file_lock
*fl
)
507 struct nlm_rqst
*req
;
512 /* Block all signals while setting up call */
513 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
514 oldset
= current
->blocked
;
515 sigfillset(¤t
->blocked
);
516 recalc_sigpending(current
);
517 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
520 req
= (struct nlm_rqst
*) rpc_allocate(RPC_TASK_ASYNC
,
522 } while (req
== NULL
);
524 req
->a_flags
= RPC_TASK_ASYNC
;
526 nlmclnt_setlockargs(req
, fl
);
528 status
= nlmclnt_async_call(req
, NLMPROC_CANCEL
,
529 nlmclnt_cancel_callback
);
533 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
534 current
->blocked
= oldset
;
535 recalc_sigpending(current
);
536 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
542 nlmclnt_cancel_callback(struct rpc_task
*task
)
544 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
546 if (RPC_ASSASSINATED(task
))
549 if (task
->tk_status
< 0) {
550 dprintk("lockd: CANCEL call error %d, retrying.\n",
555 dprintk("lockd: cancel status %d (task %d)\n",
556 req
->a_res
.status
, task
->tk_pid
);
558 switch (req
->a_res
.status
) {
559 case NLM_LCK_GRANTED
:
560 case NLM_LCK_DENIED_GRACE_PERIOD
:
561 /* Everything's good */
563 case NLM_LCK_DENIED_NOLOCKS
:
564 dprintk("lockd: CANCEL failed (server has no locks)\n");
567 printk(KERN_NOTICE
"lockd: weird return %d for CANCEL call\n",
572 rpc_release_task(task
);
573 nlm_release_host(req
->a_host
);
578 nlm_rebind_host(req
->a_host
);
579 rpc_restart_call(task
);
580 rpc_delay(task
, 30 * HZ
);
585 * Convert an NLM status code to a generic kernel errno
588 nlm_stat_to_errno(u32 status
)
591 case NLM_LCK_GRANTED
:
595 case NLM_LCK_DENIED_NOLOCKS
:
596 case NLM_LCK_DENIED_GRACE_PERIOD
:
598 case NLM_LCK_BLOCKED
:
599 printk(KERN_NOTICE
"lockd: unexpected status NLM_BLOCKED\n");
602 printk(KERN_NOTICE
"lockd: unexpected server status %d\n", status
);