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;
34 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
37 nlmclnt_setlockargs(struct nlm_rqst
*req
, struct file_lock
*fl
)
39 struct nlm_args
*argp
= &req
->a_args
;
40 struct nlm_lock
*lock
= &argp
->lock
;
42 memset(argp
, 0, sizeof(*argp
));
43 argp
->cookie
= nlm_cookie
++;
44 argp
->state
= nsm_local_state
;
45 lock
->fh
= *NFS_FH(fl
->fl_file
->f_dentry
);
46 lock
->caller
= system_utsname
.nodename
;
47 lock
->oh
.data
= req
->a_owner
;
48 lock
->oh
.len
= sprintf(req
->a_owner
, "%d@%s",
49 current
->pid
, system_utsname
.nodename
);
54 * Initialize arguments for GRANTED call. The nlm_rqst structure
55 * has been cleared already.
58 nlmclnt_setgrantargs(struct nlm_rqst
*call
, struct nlm_lock
*lock
)
60 call
->a_args
.cookie
= nlm_cookie
++;
61 call
->a_args
.lock
= *lock
;
62 call
->a_args
.lock
.caller
= system_utsname
.nodename
;
64 /* set default data area */
65 call
->a_args
.lock
.oh
.data
= call
->a_owner
;
67 if (lock
->oh
.len
> NLMCLNT_OHSIZE
) {
68 void *data
= kmalloc(lock
->oh
.len
, GFP_KERNEL
);
71 call
->a_args
.lock
.oh
.data
= (u8
*) data
;
74 memcpy(call
->a_args
.lock
.oh
.data
, lock
->oh
.data
, lock
->oh
.len
);
79 nlmclnt_freegrantargs(struct nlm_rqst
*call
)
82 * Check whether we allocated memory for the owner.
84 if (call
->a_args
.lock
.oh
.data
!= (u8
*) call
->a_owner
) {
85 kfree(call
->a_args
.lock
.oh
.data
);
90 * This is the main entry point for the NLM client.
93 nlmclnt_proc(struct inode
*inode
, int cmd
, struct file_lock
*fl
)
95 struct nfs_server
*nfssrv
= NFS_SERVER(inode
);
96 struct nlm_host
*host
;
97 struct nlm_rqst reqst
, *call
= &reqst
;
102 /* Always use NLM version 1 over UDP for now... */
103 if (!(host
= nlmclnt_lookup_host(NFS_ADDR(inode
), IPPROTO_UDP
, 1)))
106 /* Create RPC client handle if not there, and copy soft
107 * and intr flags from NFS client. */
108 if (host
->h_rpcclnt
== NULL
) {
109 struct rpc_clnt
*clnt
;
111 /* Bind an rpc client to this host handle (does not
112 * perform a portmapper lookup) */
113 if (!(clnt
= nlm_bind_host(host
))) {
117 clnt
->cl_softrtry
= nfssrv
->client
->cl_softrtry
;
118 clnt
->cl_intr
= nfssrv
->client
->cl_intr
;
119 clnt
->cl_chatty
= nfssrv
->client
->cl_chatty
;
122 /* Keep the old signal mask */
123 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
124 oldset
= current
->blocked
;
126 /* If we're cleaning up locks because the process is exiting,
127 * perform the RPC call asynchronously. */
128 if (cmd
== F_SETLK
&& fl
->fl_type
== F_UNLCK
129 && (current
->flags
& PF_EXITING
)) {
130 sigfillset(¤t
->blocked
); /* Mask all signals */
131 recalc_sigpending(current
);
132 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
134 call
= nlmclnt_alloc_call();
135 call
->a_flags
= RPC_TASK_ASYNC
;
137 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
142 /* Set up the argument struct */
143 nlmclnt_setlockargs(call
, fl
);
145 if (cmd
== F_GETLK
) {
146 status
= nlmclnt_test(call
, fl
);
147 } else if (cmd
== F_SETLK
&& fl
->fl_type
== F_UNLCK
) {
148 status
= nlmclnt_unlock(call
, fl
);
149 } else if (cmd
== F_SETLK
|| cmd
== F_SETLKW
) {
150 call
->a_args
.block
= (cmd
== F_SETLKW
)? 1 : 0;
151 status
= nlmclnt_lock(call
, fl
);
156 if (status
< 0 && (call
->a_flags
& RPC_TASK_ASYNC
))
159 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
160 current
->blocked
= oldset
;
161 recalc_sigpending(current
);
162 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
165 dprintk("lockd: clnt proc returns %d\n", status
);
166 nlm_release_host(host
);
171 * Wait while server is in grace period
174 nlmclnt_grace_wait(struct nlm_host
*host
)
176 if (!host
->h_reclaiming
)
177 current
->timeout
= jiffies
+ 10 * HZ
;
178 interruptible_sleep_on(&host
->h_gracewait
);
179 current
->timeout
= 0;
180 return signalled()? -ERESTARTSYS
: 0;
184 * Allocate an NLM RPC call struct
187 nlmclnt_alloc_call(void)
189 struct nlm_rqst
*call
;
191 while (!signalled()) {
192 call
= (struct nlm_rqst
*) rpc_allocate(RPC_TASK_ASYNC
,
193 sizeof(struct nlm_rqst
));
196 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
197 current
->timeout
= jiffies
+ 5 * HZ
;
198 current
->state
= TASK_INTERRUPTIBLE
;
200 current
->timeout
= 0;
209 nlmclnt_call(struct nlm_rqst
*req
, u32 proc
)
211 struct nlm_host
*host
= req
->a_host
;
212 struct rpc_clnt
*clnt
;
213 struct nlm_args
*argp
= &req
->a_args
;
214 struct nlm_res
*resp
= &req
->a_res
;
217 dprintk("lockd: call procedure %s on %s\n",
218 nlm_procname(proc
), host
->h_name
);
221 if (host
->h_reclaiming
&& !argp
->reclaim
) {
222 interruptible_sleep_on(&host
->h_gracewait
);
226 /* If we have no RPC client yet, create one. */
227 if ((clnt
= nlm_bind_host(host
)) == NULL
)
230 /* Perform the RPC call. If an error occurs, try again */
231 if ((status
= rpc_call(clnt
, proc
, argp
, resp
, 0)) < 0) {
232 dprintk("lockd: rpc_call returned error %d\n", -status
);
233 if (status
== -ERESTARTSYS
)
235 nlm_rebind_host(host
);
237 if (resp
->status
== NLM_LCK_DENIED_GRACE_PERIOD
) {
238 dprintk("lockd: server in grace period\n");
241 "lockd: spurious grace period reject?!\n");
245 dprintk("lockd: server returns status %d\n", resp
->status
);
246 return 0; /* Okay, call complete */
249 /* Back off a little and try again */
250 current
->timeout
= jiffies
+ 15 * HZ
;
251 interruptible_sleep_on(&host
->h_gracewait
);
252 current
->timeout
= 0;
253 } while (!signalled());
259 * Generic NLM call, async version.
262 nlmclnt_async_call(struct nlm_rqst
*req
, u32 proc
, rpc_action callback
)
264 struct nlm_host
*host
= req
->a_host
;
265 struct rpc_clnt
*clnt
;
266 struct nlm_args
*argp
= &req
->a_args
;
267 struct nlm_res
*resp
= &req
->a_res
;
270 dprintk("lockd: call procedure %s on %s (async)\n",
271 nlm_procname(proc
), host
->h_name
);
273 /* If we have no RPC client yet, create one. */
274 if ((clnt
= nlm_bind_host(host
)) == NULL
)
277 /* bootstrap and kick off the async RPC call */
278 status
= rpc_do_call(clnt
, proc
, argp
, resp
, RPC_TASK_ASYNC
,
281 /* If the async call is proceeding, increment host refcount */
282 if (status
>= 0 && (req
->a_flags
& RPC_TASK_ASYNC
))
288 * TEST for the presence of a conflicting lock
291 nlmclnt_test(struct nlm_rqst
*req
, struct file_lock
*fl
)
295 if ((status
= nlmclnt_call(req
, NLMPROC_TEST
)) < 0)
298 status
= req
->a_res
.status
;
299 if (status
== NLM_LCK_GRANTED
) {
300 fl
->fl_type
= F_UNLCK
;
301 } if (status
== NLM_LCK_DENIED
) {
303 * Report the conflicting lock back to the application.
304 * FIXME: Is it OK to report the pid back as well?
306 memcpy(fl
, &req
->a_res
.lock
.fl
, sizeof(*fl
));
307 /* fl->fl_pid = 0; */
309 return nlm_stat_to_errno(req
->a_res
.status
);
316 * LOCK: Try to create a lock
318 * Programmer Harassment Alert
320 * When given a blocking lock request in a sync RPC call, the HPUX lockd
321 * will faithfully return LCK_BLOCKED but never cares to notify us when
322 * the lock could be granted. This way, our local process could hang
323 * around forever waiting for the callback.
325 * Solution A: Implement busy-waiting
326 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
328 * For now I am implementing solution A, because I hate the idea of
329 * re-implementing lockd for a third time in two months. The async
330 * calls shouldn't be too hard to do, however.
332 * This is one of the lovely things about standards in the NFS area:
333 * they're so soft and squishy you can't really blame HP for doing this.
336 nlmclnt_lock(struct nlm_rqst
*req
, struct file_lock
*fl
)
338 struct nlm_host
*host
= req
->a_host
;
339 struct nlm_res
*resp
= &req
->a_res
;
342 if (!host
->h_monitored
&& nsm_monitor(host
) < 0) {
343 printk(KERN_NOTICE
"lockd: failed to monitor %s\n",
349 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0) {
350 if (resp
->status
!= NLM_LCK_BLOCKED
)
352 status
= nlmclnt_block(host
, fl
, &resp
->status
);
358 if (resp
->status
== NLM_LCK_GRANTED
) {
359 fl
->fl_u
.nfs_fl
.state
= host
->h_state
;
360 fl
->fl_u
.nfs_fl
.flags
|= NFS_LCK_GRANTED
;
363 return nlm_stat_to_errno(resp
->status
);
367 * RECLAIM: Try to reclaim a lock
370 nlmclnt_reclaim(struct nlm_host
*host
, struct file_lock
*fl
)
372 struct nlm_rqst reqst
, *req
;
379 /* Set up the argument struct */
380 nlmclnt_setlockargs(req
, fl
);
381 req
->a_args
.reclaim
= 1;
383 if ((status
= nlmclnt_call(req
, NLMPROC_LOCK
)) >= 0
384 && req
->a_res
.status
== NLM_LCK_GRANTED
)
387 printk(KERN_WARNING
"lockd: failed to reclaim lock for pid %d "
388 "(errno %d, status %d)\n", fl
->fl_pid
,
389 status
, req
->a_res
.status
);
392 * FIXME: This is a serious failure. We can
394 * a. Ignore the problem
395 * b. Send the owning process some signal (Linux doesn't have
396 * SIGLOST, though...)
397 * c. Retry the operation
399 * Until someone comes up with a simple implementation
400 * for b or c, I'll choose option a.
407 * UNLOCK: remove an existing lock
410 nlmclnt_unlock(struct nlm_rqst
*req
, struct file_lock
*fl
)
412 struct nlm_host
*host
= req
->a_host
;
413 struct nlm_res
*resp
= &req
->a_res
;
416 /* No monitor, no lock: see nlmclnt_lock().
417 * Since this is an UNLOCK, don't try to setup monitoring here. */
418 if (!host
->h_monitored
)
421 /* Clean the GRANTED flag now so the lock doesn't get
422 * reclaimed while we're stuck in the unlock call. */
423 fl
->fl_u
.nfs_fl
.flags
&= ~NFS_LCK_GRANTED
;
425 if (req
->a_flags
& RPC_TASK_ASYNC
) {
426 return nlmclnt_async_call(req
, NLMPROC_UNLOCK
,
427 nlmclnt_unlock_callback
);
430 if ((status
= nlmclnt_call(req
, NLMPROC_UNLOCK
)) < 0)
433 if (resp
->status
== NLM_LCK_GRANTED
)
436 if (resp
->status
!= NLM_LCK_DENIED_NOLOCKS
)
437 printk("lockd: unexpected unlock status: %d\n", resp
->status
);
439 /* What to do now? I'm out of my depth... */
445 nlmclnt_unlock_callback(struct rpc_task
*task
)
447 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
448 int status
= req
->a_res
.status
;
450 if (RPC_ASSASSINATED(task
))
453 if (task
->tk_status
< 0) {
454 dprintk("lockd: unlock failed (err = %d)\n", -task
->tk_status
);
455 nlm_rebind_host(req
->a_host
);
456 rpc_restart_call(task
);
459 if (status
!= NLM_LCK_GRANTED
460 && status
!= NLM_LCK_DENIED_GRACE_PERIOD
) {
461 printk("lockd: unexpected unlock status: %d\n", status
);
465 rpc_release_task(task
);
469 * Cancel a blocked lock request.
470 * We always use an async RPC call for this in order not to hang a
471 * process that has been Ctrl-C'ed.
474 nlmclnt_cancel(struct nlm_host
*host
, struct file_lock
*fl
)
476 struct nlm_rqst
*req
;
481 /* Block all signals while setting up call */
482 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
483 oldset
= current
->blocked
;
484 sigfillset(¤t
->blocked
);
485 recalc_sigpending(current
);
486 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
489 req
= (struct nlm_rqst
*) rpc_allocate(RPC_TASK_ASYNC
,
491 } while (req
== NULL
);
493 req
->a_flags
= RPC_TASK_ASYNC
;
495 nlmclnt_setlockargs(req
, fl
);
497 status
= nlmclnt_async_call(req
, NLMPROC_CANCEL
,
498 nlmclnt_cancel_callback
);
502 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
503 current
->blocked
= oldset
;
504 recalc_sigpending(current
);
505 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
511 nlmclnt_cancel_callback(struct rpc_task
*task
)
513 struct nlm_rqst
*req
= (struct nlm_rqst
*) task
->tk_calldata
;
515 if (RPC_ASSASSINATED(task
))
518 if (task
->tk_status
< 0) {
519 dprintk("lockd: CANCEL call error %d, retrying.\n",
524 dprintk("lockd: cancel status %d (task %d)\n",
525 req
->a_res
.status
, task
->tk_pid
);
527 switch (req
->a_res
.status
) {
528 case NLM_LCK_GRANTED
:
529 case NLM_LCK_DENIED_GRACE_PERIOD
:
530 /* Everything's good */
532 case NLM_LCK_DENIED_NOLOCKS
:
533 dprintk("lockd: CANCEL failed (server has no locks)\n");
536 printk(KERN_NOTICE
"lockd: weird return %d for CANCEL call\n",
541 rpc_release_task(task
);
542 nlm_release_host(req
->a_host
);
547 nlm_rebind_host(req
->a_host
);
548 rpc_restart_call(task
);
549 rpc_delay(task
, 30 * HZ
);
554 * Convert an NLM status code to a generic kernel errno
557 nlm_stat_to_errno(u32 status
)
560 case NLM_LCK_GRANTED
:
564 case NLM_LCK_DENIED_NOLOCKS
:
565 case NLM_LCK_DENIED_GRACE_PERIOD
:
567 case NLM_LCK_BLOCKED
:
568 printk(KERN_NOTICE
"lockd: unexpected status NLM_BLOCKED\n");
571 printk(KERN_NOTICE
"lockd: unexpected server status %d\n", status
);