File: /usr/src/linux/fs/lockd/clntproc.c
1 /*
2 * linux/fs/lockd/clntproc.c
3 *
4 * RPC procedures for the client side NLM implementation
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/nfs_fs.h>
13 #include <linux/utsname.h>
14 #include <linux/smp_lock.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/lockd/lockd.h>
18 #include <linux/lockd/sm_inter.h>
19
20 #define NLMDBG_FACILITY NLMDBG_CLIENT
21
22 static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
23 static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
24 static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
25 static void nlmclnt_unlock_callback(struct rpc_task *);
26 static void nlmclnt_cancel_callback(struct rpc_task *);
27 static int nlm_stat_to_errno(u32 stat);
28
29 /*
30 * Cookie counter for NLM requests
31 */
32 static u32 nlm_cookie = 0x1234;
33
34 static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
35 {
36 memcpy(c->data, &nlm_cookie, 4);
37 memset(c->data+4, 0, 4);
38 c->len=4;
39 nlm_cookie++;
40 }
41
42 /*
43 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
44 */
45 static inline void
46 nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
47 {
48 struct nlm_args *argp = &req->a_args;
49 struct nlm_lock *lock = &argp->lock;
50
51 nlmclnt_next_cookie(&argp->cookie);
52 argp->state = nsm_local_state;
53 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
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);
58 locks_copy_lock(&lock->fl, fl);
59 }
60
61 /*
62 * Initialize arguments for GRANTED call. The nlm_rqst structure
63 * has been cleared already.
64 */
65 int
66 nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
67 {
68 locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
69 memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
70 call->a_args.lock.caller = system_utsname.nodename;
71 call->a_args.lock.oh.len = lock->oh.len;
72
73 /* set default data area */
74 call->a_args.lock.oh.data = call->a_owner;
75
76 if (lock->oh.len > NLMCLNT_OHSIZE) {
77 void *data = kmalloc(lock->oh.len, GFP_KERNEL);
78 if (!data)
79 return 0;
80 call->a_args.lock.oh.data = (u8 *) data;
81 }
82
83 memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
84 return 1;
85 }
86
87 void
88 nlmclnt_freegrantargs(struct nlm_rqst *call)
89 {
90 /*
91 * Check whether we allocated memory for the owner.
92 */
93 if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
94 kfree(call->a_args.lock.oh.data);
95 }
96 }
97
98 /*
99 * This is the main entry point for the NLM client.
100 */
101 int
102 nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
103 {
104 struct nfs_server *nfssrv = NFS_SERVER(inode);
105 struct nlm_host *host;
106 struct nlm_rqst reqst, *call = &reqst;
107 sigset_t oldset;
108 unsigned long flags;
109 int status, proto, vers;
110
111 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
112 if (NFS_PROTO(inode)->version > 3) {
113 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
114 return -ENOLCK;
115 }
116
117 /* Retrieve transport protocol from NFS client */
118 proto = NFS_CLIENT(inode)->cl_xprt->prot;
119
120 if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
121 return -ENOLCK;
122
123 /* Create RPC client handle if not there, and copy soft
124 * and intr flags from NFS client. */
125 if (host->h_rpcclnt == NULL) {
126 struct rpc_clnt *clnt;
127
128 /* Bind an rpc client to this host handle (does not
129 * perform a portmapper lookup) */
130 if (!(clnt = nlm_bind_host(host))) {
131 status = -ENOLCK;
132 goto done;
133 }
134 clnt->cl_softrtry = nfssrv->client->cl_softrtry;
135 clnt->cl_intr = nfssrv->client->cl_intr;
136 clnt->cl_chatty = nfssrv->client->cl_chatty;
137 }
138
139 /* Keep the old signal mask */
140 spin_lock_irqsave(¤t->sigmask_lock, flags);
141 oldset = current->blocked;
142
143 /* If we're cleaning up locks because the process is exiting,
144 * perform the RPC call asynchronously. */
145 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
146 && fl->fl_type == F_UNLCK
147 && (current->flags & PF_EXITING)) {
148 sigfillset(¤t->blocked); /* Mask all signals */
149 recalc_sigpending(current);
150 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
151
152 call = nlmclnt_alloc_call();
153 if (!call) {
154 status = -ENOMEM;
155 goto out_restore;
156 }
157 call->a_flags = RPC_TASK_ASYNC;
158 } else {
159 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
160 memset(call, 0, sizeof(*call));
161 locks_init_lock(&call->a_args.lock.fl);
162 locks_init_lock(&call->a_res.lock.fl);
163 }
164 call->a_host = host;
165
166 /* Set up the argument struct */
167 nlmclnt_setlockargs(call, fl);
168
169 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
170 if (fl->fl_type != F_UNLCK) {
171 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
172 status = nlmclnt_lock(call, fl);
173 } else
174 status = nlmclnt_unlock(call, fl);
175 } else if (IS_GETLK(cmd))
176 status = nlmclnt_test(call, fl);
177 else
178 status = -EINVAL;
179
180 if (status < 0 && (call->a_flags & RPC_TASK_ASYNC))
181 kfree(call);
182
183 out_restore:
184 spin_lock_irqsave(¤t->sigmask_lock, flags);
185 current->blocked = oldset;
186 recalc_sigpending(current);
187 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
188
189 done:
190 dprintk("lockd: clnt proc returns %d\n", status);
191 nlm_release_host(host);
192 return status;
193 }
194
195 /*
196 * Wait while server is in grace period
197 */
198 static inline int
199 nlmclnt_grace_wait(struct nlm_host *host)
200 {
201 if (!host->h_reclaiming)
202 interruptible_sleep_on_timeout(&host->h_gracewait, 10*HZ);
203 else
204 interruptible_sleep_on(&host->h_gracewait);
205 return signalled()? -ERESTARTSYS : 0;
206 }
207
208 /*
209 * Allocate an NLM RPC call struct
210 */
211 struct nlm_rqst *
212 nlmclnt_alloc_call(void)
213 {
214 struct nlm_rqst *call;
215
216 while (!signalled()) {
217 call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
218 if (call) {
219 memset(call, 0, sizeof(*call));
220 locks_init_lock(&call->a_args.lock.fl);
221 locks_init_lock(&call->a_res.lock.fl);
222 return call;
223 }
224 printk("nlmclnt_alloc_call: failed, waiting for memory\n");
225 current->state = TASK_INTERRUPTIBLE;
226 schedule_timeout(5*HZ);
227 }
228 return NULL;
229 }
230
231 /*
232 * Generic NLM call
233 */
234 int
235 nlmclnt_call(struct nlm_rqst *req, u32 proc)
236 {
237 struct nlm_host *host = req->a_host;
238 struct rpc_clnt *clnt;
239 struct nlm_args *argp = &req->a_args;
240 struct nlm_res *resp = &req->a_res;
241 struct file *filp = argp->lock.fl.fl_file;
242 struct rpc_message msg;
243 int status;
244
245 dprintk("lockd: call procedure %s on %s\n",
246 nlm_procname(proc), host->h_name);
247
248 msg.rpc_proc = proc;
249 msg.rpc_argp = argp;
250 msg.rpc_resp = resp;
251 if (filp)
252 msg.rpc_cred = nfs_file_cred(filp);
253 else
254 msg.rpc_cred = NULL;
255
256 do {
257 if (host->h_reclaiming && !argp->reclaim) {
258 interruptible_sleep_on(&host->h_gracewait);
259 continue;
260 }
261
262 /* If we have no RPC client yet, create one. */
263 if ((clnt = nlm_bind_host(host)) == NULL)
264 return -ENOLCK;
265
266 /* Perform the RPC call. If an error occurs, try again */
267 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
268 dprintk("lockd: rpc_call returned error %d\n", -status);
269 switch (status) {
270 case -EPROTONOSUPPORT:
271 status = -EINVAL;
272 break;
273 case -ECONNREFUSED:
274 case -ETIMEDOUT:
275 case -ENOTCONN:
276 nlm_rebind_host(host);
277 status = -EAGAIN;
278 break;
279 case -ERESTARTSYS:
280 return signalled () ? -EINTR : status;
281 default:
282 break;
283 }
284 break;
285 } else
286 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
287 dprintk("lockd: server in grace period\n");
288 if (argp->reclaim) {
289 printk(KERN_WARNING
290 "lockd: spurious grace period reject?!\n");
291 return -ENOLCK;
292 }
293 } else {
294 dprintk("lockd: server returns status %d\n", resp->status);
295 return 0; /* Okay, call complete */
296 }
297
298 /* Back off a little and try again */
299 interruptible_sleep_on_timeout(&host->h_gracewait, 15*HZ);
300
301 /* When the lock requested by F_SETLKW isn't available,
302 we will wait until the request can be satisfied. If
303 a signal is received during wait, we should return
304 -EINTR. */
305 if (signalled ()) {
306 status = -EINTR;
307 break;
308 }
309 } while (1);
310
311 return status;
312 }
313
314 /*
315 * Generic NLM call, async version.
316 */
317 int
318 nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
319 {
320 struct nlm_host *host = req->a_host;
321 struct rpc_clnt *clnt;
322 struct nlm_args *argp = &req->a_args;
323 struct nlm_res *resp = &req->a_res;
324 struct rpc_message msg;
325 int status;
326
327 dprintk("lockd: call procedure %s on %s (async)\n",
328 nlm_procname(proc), host->h_name);
329
330 /* If we have no RPC client yet, create one. */
331 if ((clnt = nlm_bind_host(host)) == NULL)
332 return -ENOLCK;
333
334 /* bootstrap and kick off the async RPC call */
335 msg.rpc_proc = proc;
336 msg.rpc_argp = argp;
337 msg.rpc_resp =resp;
338 msg.rpc_cred = NULL;
339 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
340
341 return status;
342 }
343
344 int
345 nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
346 {
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;
353 int status;
354
355 dprintk("lockd: call procedure %s on %s (async)\n",
356 nlm_procname(proc), host->h_name);
357
358 /* If we have no RPC client yet, create one. */
359 if ((clnt = nlm_bind_host(host)) == NULL)
360 return -ENOLCK;
361
362 /* bootstrap and kick off the async RPC call */
363 msg.rpc_proc = proc;
364 msg.rpc_argp = argp;
365 msg.rpc_resp =resp;
366 if (file)
367 msg.rpc_cred = nfs_file_cred(file);
368 else
369 msg.rpc_cred = NULL;
370 /* Increment host refcount */
371 nlm_get_host(host);
372 status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
373 if (status < 0)
374 nlm_release_host(host);
375 return status;
376 }
377
378 /*
379 * TEST for the presence of a conflicting lock
380 */
381 static int
382 nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
383 {
384 int status;
385
386 if ((status = nlmclnt_call(req, NLMPROC_TEST)) < 0)
387 return status;
388
389 status = req->a_res.status;
390 if (status == NLM_LCK_GRANTED) {
391 fl->fl_type = F_UNLCK;
392 } if (status == NLM_LCK_DENIED) {
393 /*
394 * Report the conflicting lock back to the application.
395 * FIXME: Is it OK to report the pid back as well?
396 */
397 locks_copy_lock(fl, &req->a_res.lock.fl);
398 /* fl->fl_pid = 0; */
399 } else {
400 return nlm_stat_to_errno(req->a_res.status);
401 }
402
403 return 0;
404 }
405
406 static
407 void nlmclnt_insert_lock_callback(struct file_lock *fl)
408 {
409 nlm_get_host(fl->fl_u.nfs_fl.host);
410 }
411 static
412 void nlmclnt_remove_lock_callback(struct file_lock *fl)
413 {
414 if (fl->fl_u.nfs_fl.host) {
415 nlm_release_host(fl->fl_u.nfs_fl.host);
416 fl->fl_u.nfs_fl.host = NULL;
417 }
418 }
419
420 /*
421 * LOCK: Try to create a lock
422 *
423 * Programmer Harassment Alert
424 *
425 * When given a blocking lock request in a sync RPC call, the HPUX lockd
426 * will faithfully return LCK_BLOCKED but never cares to notify us when
427 * the lock could be granted. This way, our local process could hang
428 * around forever waiting for the callback.
429 *
430 * Solution A: Implement busy-waiting
431 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
432 *
433 * For now I am implementing solution A, because I hate the idea of
434 * re-implementing lockd for a third time in two months. The async
435 * calls shouldn't be too hard to do, however.
436 *
437 * This is one of the lovely things about standards in the NFS area:
438 * they're so soft and squishy you can't really blame HP for doing this.
439 */
440 static int
441 nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
442 {
443 struct nlm_host *host = req->a_host;
444 struct nlm_res *resp = &req->a_res;
445 int status;
446
447 if (!host->h_monitored && nsm_monitor(host) < 0) {
448 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
449 host->h_name);
450 return -ENOLCK;
451 }
452
453 do {
454 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
455 if (resp->status != NLM_LCK_BLOCKED)
456 break;
457 status = nlmclnt_block(host, fl, &resp->status);
458 }
459 if (status < 0)
460 return status;
461 } while (resp->status == NLM_LCK_BLOCKED);
462
463 if (resp->status == NLM_LCK_GRANTED) {
464 fl->fl_u.nfs_fl.state = host->h_state;
465 fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
466 fl->fl_u.nfs_fl.host = host;
467 fl->fl_insert = nlmclnt_insert_lock_callback;
468 fl->fl_remove = nlmclnt_remove_lock_callback;
469 }
470
471 return nlm_stat_to_errno(resp->status);
472 }
473
474 /*
475 * RECLAIM: Try to reclaim a lock
476 */
477 int
478 nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
479 {
480 struct nlm_rqst reqst, *req;
481 int status;
482
483 req = &reqst;
484 memset(req, 0, sizeof(*req));
485 locks_init_lock(&req->a_args.lock.fl);
486 locks_init_lock(&req->a_res.lock.fl);
487 req->a_host = host;
488 req->a_flags = 0;
489
490 /* Set up the argument struct */
491 nlmclnt_setlockargs(req, fl);
492 req->a_args.reclaim = 1;
493
494 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
495 && req->a_res.status == NLM_LCK_GRANTED)
496 return 0;
497
498 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
499 "(errno %d, status %d)\n", fl->fl_pid,
500 status, req->a_res.status);
501
502 /*
503 * FIXME: This is a serious failure. We can
504 *
505 * a. Ignore the problem
506 * b. Send the owning process some signal (Linux doesn't have
507 * SIGLOST, though...)
508 * c. Retry the operation
509 *
510 * Until someone comes up with a simple implementation
511 * for b or c, I'll choose option a.
512 */
513
514 return -ENOLCK;
515 }
516
517 /*
518 * UNLOCK: remove an existing lock
519 */
520 static int
521 nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
522 {
523 struct nlm_res *resp = &req->a_res;
524 int status;
525
526 /* Clean the GRANTED flag now so the lock doesn't get
527 * reclaimed while we're stuck in the unlock call. */
528 fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
529
530 if (req->a_flags & RPC_TASK_ASYNC) {
531 return nlmclnt_async_call(req, NLMPROC_UNLOCK,
532 nlmclnt_unlock_callback);
533 }
534
535 if ((status = nlmclnt_call(req, NLMPROC_UNLOCK)) < 0)
536 return status;
537
538 if (resp->status == NLM_LCK_GRANTED)
539 return 0;
540
541 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
542 printk("lockd: unexpected unlock status: %d\n", resp->status);
543
544 /* What to do now? I'm out of my depth... */
545
546 return -ENOLCK;
547 }
548
549 static void
550 nlmclnt_unlock_callback(struct rpc_task *task)
551 {
552 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
553 int status = req->a_res.status;
554
555 if (RPC_ASSASSINATED(task))
556 goto die;
557
558 if (task->tk_status < 0) {
559 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
560 goto retry_unlock;
561 }
562 if (status != NLM_LCK_GRANTED
563 && status != NLM_LCK_DENIED_GRACE_PERIOD) {
564 printk("lockd: unexpected unlock status: %d\n", status);
565 }
566
567 die:
568 nlm_release_host(req->a_host);
569 kfree(req);
570 return;
571 retry_unlock:
572 nlm_rebind_host(req->a_host);
573 rpc_restart_call(task);
574 }
575
576 /*
577 * Cancel a blocked lock request.
578 * We always use an async RPC call for this in order not to hang a
579 * process that has been Ctrl-C'ed.
580 */
581 int
582 nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
583 {
584 struct nlm_rqst *req;
585 unsigned long flags;
586 sigset_t oldset;
587 int status;
588
589 /* Block all signals while setting up call */
590 spin_lock_irqsave(¤t->sigmask_lock, flags);
591 oldset = current->blocked;
592 sigfillset(¤t->blocked);
593 recalc_sigpending(current);
594 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
595
596 req = nlmclnt_alloc_call();
597 if (!req)
598 return -ENOMEM;
599 req->a_host = host;
600 req->a_flags = RPC_TASK_ASYNC;
601
602 nlmclnt_setlockargs(req, fl);
603
604 status = nlmclnt_async_call(req, NLMPROC_CANCEL,
605 nlmclnt_cancel_callback);
606 if (status < 0)
607 kfree(req);
608
609 spin_lock_irqsave(¤t->sigmask_lock, flags);
610 current->blocked = oldset;
611 recalc_sigpending(current);
612 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
613
614 return status;
615 }
616
617 static void
618 nlmclnt_cancel_callback(struct rpc_task *task)
619 {
620 struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
621
622 if (RPC_ASSASSINATED(task))
623 goto die;
624
625 if (task->tk_status < 0) {
626 dprintk("lockd: CANCEL call error %d, retrying.\n",
627 task->tk_status);
628 goto retry_cancel;
629 }
630
631 dprintk("lockd: cancel status %d (task %d)\n",
632 req->a_res.status, task->tk_pid);
633
634 switch (req->a_res.status) {
635 case NLM_LCK_GRANTED:
636 case NLM_LCK_DENIED_GRACE_PERIOD:
637 /* Everything's good */
638 break;
639 case NLM_LCK_DENIED_NOLOCKS:
640 dprintk("lockd: CANCEL failed (server has no locks)\n");
641 goto retry_cancel;
642 default:
643 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
644 req->a_res.status);
645 }
646
647 die:
648 nlm_release_host(req->a_host);
649 kfree(req);
650 return;
651
652 retry_cancel:
653 nlm_rebind_host(req->a_host);
654 rpc_restart_call(task);
655 rpc_delay(task, 30 * HZ);
656 }
657
658 /*
659 * Convert an NLM status code to a generic kernel errno
660 */
661 static int
662 nlm_stat_to_errno(u32 status)
663 {
664 switch(status) {
665 case NLM_LCK_GRANTED:
666 return 0;
667 case NLM_LCK_DENIED:
668 return -EAGAIN;
669 case NLM_LCK_DENIED_NOLOCKS:
670 case NLM_LCK_DENIED_GRACE_PERIOD:
671 return -ENOLCK;
672 case NLM_LCK_BLOCKED:
673 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
674 return -ENOLCK;
675 }
676 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
677 return -ENOLCK;
678 }
679