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);
32 * Cookie counter for NLM requests
34 static u32 nlm_cookie = 0x1234;
36 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
38 memcpy(c->data, &nlm_cookie, 4);
39 memset(c->data+4, 0, 4);
45 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
48 nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
50 struct nlm_args *argp = &req->a_args;
51 struct nlm_lock *lock = &argp->lock;
53 nlmclnt_next_cookie(&argp->cookie);
54 argp->state = nsm_local_state;
55 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
56 lock->caller = system_utsname.nodename;
57 lock->oh.data = req->a_owner;
58 lock->oh.len = sprintf(req->a_owner, "%d@%s",
59 current->pid, system_utsname.nodename);
60 locks_copy_lock(&lock->fl, fl);
64 * Initialize arguments for GRANTED call. The nlm_rqst structure
65 * has been cleared already.
68 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
70 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
71 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
72 call->a_args.lock.caller = system_utsname.nodename;
73 call->a_args.lock.oh.len = lock->oh.len;
75 /* set default data area */
76 call->a_args.lock.oh.data = call->a_owner;
78 if (lock->oh.len > NLMCLNT_OHSIZE) {
79 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
82 call->a_args.lock.oh.data = (u8 *) data;
85 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
90 nlmclnt_freegrantargs(struct nlm_rqst *call)
93 * Check whether we allocated memory for the owner.
95 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
96 kfree(call->a_args.lock.oh.data);
101 * This is the main entry point for the NLM client.
104 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
106 struct nfs_server *nfssrv = NFS_SERVER(inode);
107 struct nlm_host *host;
108 struct nlm_rqst reqst, *call = &reqst;
111 int status, proto, vers;
113 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
114 if (NFS_PROTO(inode)->version > 3) {
115 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
119 /* Retrieve transport protocol from NFS client */
120 proto = NFS_CLIENT(inode)->cl_xprt->prot;
122 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
125 /* Create RPC client handle if not there, and copy soft
126 * and intr flags from NFS client. */
127 if (host->h_rpcclnt == NULL) {
128 struct rpc_clnt *clnt;
130 /* Bind an rpc client to this host handle (does not
131 * perform a portmapper lookup) */
132 if (!(clnt = nlm_bind_host(host))) {
136 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
137 clnt->cl_intr = nfssrv->client->cl_intr;
138 clnt->cl_chatty = nfssrv->client->cl_chatty;
141 /* Keep the old signal mask */
142 spin_lock_irqsave(¤t->sighand->siglock, flags);
143 oldset = current->blocked;
145 /* If we're cleaning up locks because the process is exiting,
146 * perform the RPC call asynchronously. */
147 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
148 && fl->fl_type == F_UNLCK
149 && (current->flags & PF_EXITING)) {
150 sigfillset(¤t->blocked); /* Mask all signals */
152 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
154 call = nlmclnt_alloc_call();
159 call->a_flags = RPC_TASK_ASYNC;
161 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
162 memset(call, 0, sizeof(*call));
163 locks_init_lock(&call->a_args.lock.fl);
164 locks_init_lock(&call->a_res.lock.fl);
168 /* Set up the argument struct */
169 nlmclnt_setlockargs(call, fl);
171 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
172 if (fl->fl_type != F_UNLCK) {
173 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
174 status = nlmclnt_lock(call, fl);
176 status = nlmclnt_unlock(call, fl);
177 } else if (IS_GETLK(cmd))
178 status = nlmclnt_test(call, fl);
182 if (status < 0 && (call->a_flags & RPC_TASK_ASYNC))
186 spin_lock_irqsave(¤t->sighand->siglock, flags);
187 current->blocked = oldset;
189 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
192 dprintk("lockd: clnt proc returns %d\n", status);
193 nlm_release_host(host);
198 * Allocate an NLM RPC call struct
201 nlmclnt_alloc_call(void)
203 struct nlm_rqst *call;
205 while (!signalled()) {
206 call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
208 memset(call, 0, sizeof(*call));
209 locks_init_lock(&call->a_args.lock.fl);
210 locks_init_lock(&call->a_res.lock.fl);
213 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
214 current->state = TASK_INTERRUPTIBLE;
215 schedule_timeout(5*HZ);
220 static int nlm_wait_on_grace(wait_queue_head_t *queue)
225 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
227 schedule_timeout(NLMCLNT_GRACE_WAIT);
231 finish_wait(queue, &wait);
239 nlmclnt_call(struct nlm_rqst *req, u32 proc)
241 struct nlm_host *host = req->a_host;
242 struct rpc_clnt *clnt;
243 struct nlm_args *argp = &req->a_args;
244 struct nlm_res *resp = &req->a_res;
245 struct file *filp = argp->lock.fl.fl_file;
246 struct rpc_message msg = {
252 dprintk("lockd: call procedure %d on %s\n",
253 (int)proc, host->h_name);
256 msg.rpc_cred = nfs_file_cred(filp);
259 if (host->h_reclaiming && !argp->reclaim)
260 goto in_grace_period;
262 /* If we have no RPC client yet, create one. */
263 if ((clnt = nlm_bind_host(host)) == NULL)
265 msg.rpc_proc = &clnt->cl_procinfo[proc];
267 /* Perform the RPC call. If an error occurs, try again */
268 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
269 dprintk("lockd: rpc_call returned error %d\n", -status);
271 case -EPROTONOSUPPORT:
277 nlm_rebind_host(host);
281 return signalled () ? -EINTR : status;
287 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
288 dprintk("lockd: server in grace period\n");
291 "lockd: spurious grace period reject?!\n");
295 if (!argp->reclaim) {
296 /* We appear to be out of the grace period */
297 wake_up_all(&host->h_gracewait);
299 dprintk("lockd: server returns status %d\n", resp->status);
300 return 0; /* Okay, call complete */
305 * The server has rebooted and appears to be in the grace
306 * period during which locks are only allowed to be
308 * We can only back off and try again later.
310 status = nlm_wait_on_grace(&host->h_gracewait);
311 } while (status == 0);
317 * Generic NLM call, async version.
320 nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
322 struct nlm_host *host = req->a_host;
323 struct rpc_clnt *clnt;
324 struct rpc_message msg = {
325 .rpc_argp = &req->a_args,
326 .rpc_resp = &req->a_res,
330 dprintk("lockd: call procedure %d on %s (async)\n",
331 (int)proc, host->h_name);
333 /* If we have no RPC client yet, create one. */
334 if ((clnt = nlm_bind_host(host)) == NULL)
336 msg.rpc_proc = &clnt->cl_procinfo[proc];
338 /* bootstrap and kick off the async RPC call */
339 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
345 nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
347 struct nlm_host *host = req->a_host;
348 struct rpc_clnt *clnt;
349 struct nlm_args *argp = &req->a_args;
350 struct nlm_res *resp = &req->a_res;
351 struct file *file = argp->lock.fl.fl_file;
352 struct rpc_message msg = {
358 dprintk("lockd: call procedure %d on %s (async)\n",
359 (int)proc, host->h_name);
361 /* If we have no RPC client yet, create one. */
362 if ((clnt = nlm_bind_host(host)) == NULL)
364 msg.rpc_proc = &clnt->cl_procinfo[proc];
366 /* bootstrap and kick off the async RPC call */
368 msg.rpc_cred = nfs_file_cred(file);
369 /* Increment host refcount */
371 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
373 nlm_release_host(host);
378 * TEST for the presence of a conflicting lock
381 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
385 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
388 status = req->a_res.status;
389 if (status == NLM_LCK_GRANTED) {
390 fl->fl_type = F_UNLCK;
391 } if (status == NLM_LCK_DENIED) {
393 * Report the conflicting lock back to the application.
394 * FIXME: Is it OK to report the pid back as well?
396 locks_copy_lock(fl, &req->a_res.lock.fl);
397 /* fl->fl_pid = 0; */
399 return nlm_stat_to_errno(req->a_res.status);
406 void nlmclnt_insert_lock_callback(struct file_lock *fl)
408 nlm_get_host(fl->fl_u.nfs_fl.host);
411 void nlmclnt_remove_lock_callback(struct file_lock *fl)
413 if (fl->fl_u.nfs_fl.host) {
414 nlm_release_host(fl->fl_u.nfs_fl.host);
415 fl->fl_u.nfs_fl.host = NULL;
420 * LOCK: Try to create a lock
422 * Programmer Harassment Alert
424 * When given a blocking lock request in a sync RPC call, the HPUX lockd
425 * will faithfully return LCK_BLOCKED but never cares to notify us when
426 * the lock could be granted. This way, our local process could hang
427 * around forever waiting for the callback.
429 * Solution A: Implement busy-waiting
430 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
432 * For now I am implementing solution A, because I hate the idea of
433 * re-implementing lockd for a third time in two months. The async
434 * calls shouldn't be too hard to do, however.
436 * This is one of the lovely things about standards in the NFS area:
437 * they're so soft and squishy you can't really blame HP for doing this.
440 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
442 struct nlm_host *host = req->a_host;
443 struct nlm_res *resp = &req->a_res;
446 if (!host->h_monitored && nsm_monitor(host) < 0) {
447 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
453 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
454 if (resp->status != NLM_LCK_BLOCKED)
456 status = nlmclnt_block(host, fl, &resp->status);
460 } while (resp->status == NLM_LCK_BLOCKED && req->a_args.block);
462 if (resp->status == NLM_LCK_GRANTED) {
463 fl->fl_u.nfs_fl.state = host->h_state;
464 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
465 fl->fl_u.nfs_fl.host = host;
466 fl->fl_insert = nlmclnt_insert_lock_callback;
467 fl->fl_remove = nlmclnt_remove_lock_callback;
470 return nlm_stat_to_errno(resp->status);
474 * RECLAIM: Try to reclaim a lock
477 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
479 struct nlm_rqst reqst, *req;
483 memset(req, 0, sizeof(*req));
484 locks_init_lock(&req->a_args.lock.fl);
485 locks_init_lock(&req->a_res.lock.fl);
489 /* Set up the argument struct */
490 nlmclnt_setlockargs(req, fl);
491 req->a_args.reclaim = 1;
493 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
494 && req->a_res.status == NLM_LCK_GRANTED)
497 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
498 "(errno %d, status %d)\n", fl->fl_pid,
499 status, req->a_res.status);
502 * FIXME: This is a serious failure. We can
504 * a. Ignore the problem
505 * b. Send the owning process some signal (Linux doesn't have
506 * SIGLOST, though...)
507 * c. Retry the operation
509 * Until someone comes up with a simple implementation
510 * for b or c, I'll choose option a.
517 * UNLOCK: remove an existing lock
520 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
522 struct nlm_res *resp = &req->a_res;
525 /* Clean the GRANTED flag now so the lock doesn't get
526 * reclaimed while we're stuck in the unlock call. */
527 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
529 if (req->a_flags & RPC_TASK_ASYNC) {
530 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
531 nlmclnt_unlock_callback);
534 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
537 if (resp->status == NLM_LCK_GRANTED)
540 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
541 printk("lockd: unexpected unlock status: %d\n", resp->status);
543 /* What to do now? I'm out of my depth... */
549 nlmclnt_unlock_callback(struct rpc_task *task)
551 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
552 int status = req->a_res.status;
554 if (RPC_ASSASSINATED(task))
557 if (task->tk_status < 0) {
558 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
561 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
562 rpc_delay(task, NLMCLNT_GRACE_WAIT);
565 if (status != NLM_LCK_GRANTED)
566 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
569 nlm_release_host(req->a_host);
573 nlm_rebind_host(req->a_host);
575 rpc_restart_call(task);
579 * Cancel a blocked lock request.
580 * We always use an async RPC call for this in order not to hang a
581 * process that has been Ctrl-C'ed.
584 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
586 struct nlm_rqst *req;
591 /* Block all signals while setting up call */
592 spin_lock_irqsave(¤t->sighand->siglock, flags);
593 oldset = current->blocked;
594 sigfillset(¤t->blocked);
596 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
598 req = nlmclnt_alloc_call();
602 req->a_flags = RPC_TASK_ASYNC;
604 nlmclnt_setlockargs(req, fl);
606 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
607 nlmclnt_cancel_callback);
611 spin_lock_irqsave(¤t->sighand->siglock, flags);
612 current->blocked = oldset;
614 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
620 nlmclnt_cancel_callback(struct rpc_task *task)
622 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
624 if (RPC_ASSASSINATED(task))
627 if (task->tk_status < 0) {
628 dprintk("lockd: CANCEL call error %d, retrying.\n",
633 dprintk("lockd: cancel status %d (task %d)\n",
634 req->a_res.status, task->tk_pid);
636 switch (req->a_res.status) {
637 case NLM_LCK_GRANTED:
638 case NLM_LCK_DENIED_GRACE_PERIOD:
639 /* Everything's good */
641 case NLM_LCK_DENIED_NOLOCKS:
642 dprintk("lockd: CANCEL failed (server has no locks)\n");
645 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
650 nlm_release_host(req->a_host);
655 nlm_rebind_host(req->a_host);
656 rpc_restart_call(task);
657 rpc_delay(task, 30 * HZ);
661 * Convert an NLM status code to a generic kernel errno
664 nlm_stat_to_errno(u32 status)
667 case NLM_LCK_GRANTED:
671 case NLM_LCK_DENIED_NOLOCKS:
672 case NLM_LCK_DENIED_GRACE_PERIOD:
674 case NLM_LCK_BLOCKED:
675 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
677 #ifdef CONFIG_LOCKD_V4
690 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);