blob: d210cf304e922f3f2eca906f0ec025cf2832f865 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/svcproc.c
3 *
4 * Lockd server procedures. We don't implement the NLM_*_RES
5 * procedures because we don't use the async procedures.
6 *
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8 */
9
10#include <linux/config.h>
11#include <linux/types.h>
12#include <linux/time.h>
13#include <linux/slab.h>
14#include <linux/in.h>
15#include <linux/sunrpc/svc.h>
16#include <linux/sunrpc/clnt.h>
17#include <linux/nfsd/nfsd.h>
18#include <linux/lockd/lockd.h>
19#include <linux/lockd/share.h>
20#include <linux/lockd/sm_inter.h>
21
22
23#define NLMDBG_FACILITY NLMDBG_CLIENT
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_LOCKD_V4
26static u32
27cast_to_nlm(u32 status, u32 vers)
28{
29 /* Note: status is assumed to be in network byte order !!! */
30 if (vers != 4){
31 switch (status) {
32 case nlm_granted:
33 case nlm_lck_denied:
34 case nlm_lck_denied_nolocks:
35 case nlm_lck_blocked:
36 case nlm_lck_denied_grace_period:
37 break;
38 case nlm4_deadlock:
39 status = nlm_lck_denied;
40 break;
41 default:
42 status = nlm_lck_denied_nolocks;
43 }
44 }
45
46 return (status);
47}
48#define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
49#else
50#define cast_status(status) (status)
51#endif
52
53/*
54 * Obtain client and file from arguments
55 */
56static u32
57nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
58 struct nlm_host **hostp, struct nlm_file **filp)
59{
60 struct nlm_host *host = NULL;
61 struct nlm_file *file = NULL;
62 struct nlm_lock *lock = &argp->lock;
63 u32 error;
64
65 /* nfsd callbacks must have been installed for this procedure */
66 if (!nlmsvc_ops)
67 return nlm_lck_denied_nolocks;
68
69 /* Obtain host handle */
70 if (!(host = nlmsvc_lookup_host(rqstp))
71 || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
72 goto no_locks;
73 *hostp = host;
74
75 /* Obtain file pointer. Not used by FREE_ALL call. */
76 if (filp != NULL) {
77 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
78 goto no_locks;
79 *filp = file;
80
81 /* Set up the missing parts of the file_lock structure */
82 lock->fl.fl_file = file->f_file;
83 lock->fl.fl_owner = (fl_owner_t) host;
84 lock->fl.fl_lmops = &nlmsvc_lock_operations;
85 }
86
87 return 0;
88
89no_locks:
90 if (host)
91 nlm_release_host(host);
92 return nlm_lck_denied_nolocks;
93}
94
95/*
96 * NULL: Test for presence of service
97 */
98static int
99nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
100{
101 dprintk("lockd: NULL called\n");
102 return rpc_success;
103}
104
105/*
106 * TEST: Check for conflicting lock
107 */
108static int
109nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
110 struct nlm_res *resp)
111{
112 struct nlm_host *host;
113 struct nlm_file *file;
114
115 dprintk("lockd: TEST called\n");
116 resp->cookie = argp->cookie;
117
118 /* Don't accept test requests during grace period */
119 if (nlmsvc_grace_period) {
120 resp->status = nlm_lck_denied_grace_period;
121 return rpc_success;
122 }
123
124 /* Obtain client and file */
125 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
126 return rpc_success;
127
128 /* Now check for conflicting locks */
129 resp->status = cast_status(nlmsvc_testlock(file, &argp->lock, &resp->lock));
130
131 dprintk("lockd: TEST status %d vers %d\n",
132 ntohl(resp->status), rqstp->rq_vers);
133 nlm_release_host(host);
134 nlm_release_file(file);
135 return rpc_success;
136}
137
138static int
139nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
140 struct nlm_res *resp)
141{
142 struct nlm_host *host;
143 struct nlm_file *file;
144
145 dprintk("lockd: LOCK called\n");
146
147 resp->cookie = argp->cookie;
148
149 /* Don't accept new lock requests during grace period */
150 if (nlmsvc_grace_period && !argp->reclaim) {
151 resp->status = nlm_lck_denied_grace_period;
152 return rpc_success;
153 }
154
155 /* Obtain client and file */
156 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
157 return rpc_success;
158
159#if 0
160 /* If supplied state doesn't match current state, we assume it's
161 * an old request that time-warped somehow. Any error return would
162 * do in this case because it's irrelevant anyway.
163 *
164 * NB: We don't retrieve the remote host's state yet.
165 */
166 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
167 resp->status = nlm_lck_denied_nolocks;
168 } else
169#endif
170
171 /* Now try to lock the file */
172 resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock,
173 argp->block, &argp->cookie));
174
175 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
176 nlm_release_host(host);
177 nlm_release_file(file);
178 return rpc_success;
179}
180
181static int
182nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
183 struct nlm_res *resp)
184{
185 struct nlm_host *host;
186 struct nlm_file *file;
187
188 dprintk("lockd: CANCEL called\n");
189
190 resp->cookie = argp->cookie;
191
192 /* Don't accept requests during grace period */
193 if (nlmsvc_grace_period) {
194 resp->status = nlm_lck_denied_grace_period;
195 return rpc_success;
196 }
197
198 /* Obtain client and file */
199 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
200 return rpc_success;
201
202 /* Try to cancel request. */
203 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
204
205 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
206 nlm_release_host(host);
207 nlm_release_file(file);
208 return rpc_success;
209}
210
211/*
212 * UNLOCK: release a lock
213 */
214static int
215nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
216 struct nlm_res *resp)
217{
218 struct nlm_host *host;
219 struct nlm_file *file;
220
221 dprintk("lockd: UNLOCK called\n");
222
223 resp->cookie = argp->cookie;
224
225 /* Don't accept new lock requests during grace period */
226 if (nlmsvc_grace_period) {
227 resp->status = nlm_lck_denied_grace_period;
228 return rpc_success;
229 }
230
231 /* Obtain client and file */
232 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
233 return rpc_success;
234
235 /* Now try to remove the lock */
236 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
237
238 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
239 nlm_release_host(host);
240 nlm_release_file(file);
241 return rpc_success;
242}
243
244/*
245 * GRANTED: A server calls us to tell that a process' lock request
246 * was granted
247 */
248static int
249nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
250 struct nlm_res *resp)
251{
252 resp->cookie = argp->cookie;
253
254 dprintk("lockd: GRANTED called\n");
Trond Myklebust5ac5f9d2006-02-14 13:53:04 -0800255 resp->status = nlmclnt_grant(&rqstp->rq_addr, &argp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
257 return rpc_success;
258}
259
260/*
Trond Myklebustd4716622006-03-20 13:44:45 -0500261 * This is the generic lockd callback for async RPC calls
262 */
263static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
264{
265 dprintk("lockd: %4d callback returned %d\n", task->tk_pid,
266 -task->tk_status);
267}
268
269static void nlmsvc_callback_release(void *data)
270{
271 nlm_release_call(data);
272}
273
274static const struct rpc_call_ops nlmsvc_callback_ops = {
275 .rpc_call_done = nlmsvc_callback_exit,
276 .rpc_release = nlmsvc_callback_release,
277};
278
279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * `Async' versions of the above service routines. They aren't really,
281 * because we send the callback before the reply proper. I hope this
282 * doesn't break any clients.
283 */
Trond Myklebustd4716622006-03-20 13:44:45 -0500284static int nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
285 int (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res *))
286{
287 struct nlm_host *host;
288 struct nlm_rqst *call;
289 int stat;
290
291 host = nlmsvc_lookup_host(rqstp);
292 if (host == NULL)
293 return rpc_system_err;
294
295 call = nlm_alloc_call(host);
296 if (call == NULL)
297 return rpc_system_err;
298
299 stat = func(rqstp, argp, &call->a_res);
300 if (stat != 0) {
301 nlm_release_call(call);
302 return stat;
303 }
304
305 call->a_flags = RPC_TASK_ASYNC;
306 if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
307 return rpc_system_err;
308 return rpc_success;
309}
310
311static int nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 void *resp)
313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 dprintk("lockd: TEST_MSG called\n");
Trond Myklebustd4716622006-03-20 13:44:45 -0500315 return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, argp, nlmsvc_proc_test);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Trond Myklebustd4716622006-03-20 13:44:45 -0500318static int nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 void *resp)
320{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dprintk("lockd: LOCK_MSG called\n");
Trond Myklebustd4716622006-03-20 13:44:45 -0500322 return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlmsvc_proc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Trond Myklebustd4716622006-03-20 13:44:45 -0500325static int nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 void *resp)
327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 dprintk("lockd: CANCEL_MSG called\n");
Trond Myklebustd4716622006-03-20 13:44:45 -0500329 return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlmsvc_proc_cancel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static int
333nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
334 void *resp)
335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dprintk("lockd: UNLOCK_MSG called\n");
Trond Myklebustd4716622006-03-20 13:44:45 -0500337 return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlmsvc_proc_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340static int
341nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
342 void *resp)
343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 dprintk("lockd: GRANTED_MSG called\n");
Trond Myklebustd4716622006-03-20 13:44:45 -0500345 return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlmsvc_proc_granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/*
349 * SHARE: create a DOS share or alter existing share.
350 */
351static int
352nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
353 struct nlm_res *resp)
354{
355 struct nlm_host *host;
356 struct nlm_file *file;
357
358 dprintk("lockd: SHARE called\n");
359
360 resp->cookie = argp->cookie;
361
362 /* Don't accept new lock requests during grace period */
363 if (nlmsvc_grace_period && !argp->reclaim) {
364 resp->status = nlm_lck_denied_grace_period;
365 return rpc_success;
366 }
367
368 /* Obtain client and file */
369 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
370 return rpc_success;
371
372 /* Now try to create the share */
373 resp->status = cast_status(nlmsvc_share_file(host, file, argp));
374
375 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
376 nlm_release_host(host);
377 nlm_release_file(file);
378 return rpc_success;
379}
380
381/*
382 * UNSHARE: Release a DOS share.
383 */
384static int
385nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
386 struct nlm_res *resp)
387{
388 struct nlm_host *host;
389 struct nlm_file *file;
390
391 dprintk("lockd: UNSHARE called\n");
392
393 resp->cookie = argp->cookie;
394
395 /* Don't accept requests during grace period */
396 if (nlmsvc_grace_period) {
397 resp->status = nlm_lck_denied_grace_period;
398 return rpc_success;
399 }
400
401 /* Obtain client and file */
402 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
403 return rpc_success;
404
405 /* Now try to unshare the file */
406 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
407
408 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
409 nlm_release_host(host);
410 nlm_release_file(file);
411 return rpc_success;
412}
413
414/*
415 * NM_LOCK: Create an unmonitored lock
416 */
417static int
418nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
419 struct nlm_res *resp)
420{
421 dprintk("lockd: NM_LOCK called\n");
422
423 argp->monitor = 0; /* just clean the monitor flag */
424 return nlmsvc_proc_lock(rqstp, argp, resp);
425}
426
427/*
428 * FREE_ALL: Release all locks and shares held by client
429 */
430static int
431nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
432 void *resp)
433{
434 struct nlm_host *host;
435
436 /* Obtain client */
437 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
438 return rpc_success;
439
440 nlmsvc_free_host_resources(host);
441 nlm_release_host(host);
442 return rpc_success;
443}
444
445/*
446 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
447 */
448static int
449nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
450 void *resp)
451{
452 struct sockaddr_in saddr = rqstp->rq_addr;
453 int vers = argp->vers;
454 int prot = argp->proto >> 1;
455 struct nlm_host *host;
456
457 dprintk("lockd: SM_NOTIFY called\n");
458 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
459 || ntohs(saddr.sin_port) >= 1024) {
460 printk(KERN_WARNING
461 "lockd: rejected NSM callback from %08x:%d\n",
462 ntohl(rqstp->rq_addr.sin_addr.s_addr),
463 ntohs(rqstp->rq_addr.sin_port));
464 return rpc_system_err;
465 }
466
467 /* Obtain the host pointer for this NFS server and try to
468 * reclaim all locks we hold on this server.
469 */
470 saddr.sin_addr.s_addr = argp->addr;
471 if ((argp->proto & 1)==0) {
472 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
473 nlmclnt_recovery(host, argp->state);
474 nlm_release_host(host);
475 }
476 } else {
477 /* If we run on an NFS server, delete all locks held by the client */
478 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
479 nlmsvc_free_host_resources(host);
480 nlm_release_host(host);
481 }
482 }
483
484 return rpc_success;
485}
486
487/*
488 * client sent a GRANTED_RES, let's remove the associated block
489 */
490static int
491nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
492 void *resp)
493{
494 if (!nlmsvc_ops)
495 return rpc_success;
496
497 dprintk("lockd: GRANTED_RES called\n");
498
499 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
500 return rpc_success;
501}
502
503/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * NLM Server procedures.
505 */
506
507#define nlmsvc_encode_norep nlmsvc_encode_void
508#define nlmsvc_decode_norep nlmsvc_decode_void
509#define nlmsvc_decode_testres nlmsvc_decode_void
510#define nlmsvc_decode_lockres nlmsvc_decode_void
511#define nlmsvc_decode_unlockres nlmsvc_decode_void
512#define nlmsvc_decode_cancelres nlmsvc_decode_void
513#define nlmsvc_decode_grantedres nlmsvc_decode_void
514
515#define nlmsvc_proc_none nlmsvc_proc_null
516#define nlmsvc_proc_test_res nlmsvc_proc_null
517#define nlmsvc_proc_lock_res nlmsvc_proc_null
518#define nlmsvc_proc_cancel_res nlmsvc_proc_null
519#define nlmsvc_proc_unlock_res nlmsvc_proc_null
520
521struct nlm_void { int dummy; };
522
523#define PROC(name, xargt, xrest, argt, rest, respsize) \
524 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
525 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
526 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
527 .pc_release = NULL, \
528 .pc_argsize = sizeof(struct nlm_##argt), \
529 .pc_ressize = sizeof(struct nlm_##rest), \
530 .pc_xdrressize = respsize, \
531 }
532
533#define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
534#define St 1 /* status */
535#define No (1+1024/4) /* Net Obj */
536#define Rg 2 /* range - offset + size */
537
538struct svc_procedure nlmsvc_procedures[] = {
539 PROC(null, void, void, void, void, 1),
540 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
541 PROC(lock, lockargs, res, args, res, Ck+St),
542 PROC(cancel, cancargs, res, args, res, Ck+St),
543 PROC(unlock, unlockargs, res, args, res, Ck+St),
544 PROC(granted, testargs, res, args, res, Ck+St),
545 PROC(test_msg, testargs, norep, args, void, 1),
546 PROC(lock_msg, lockargs, norep, args, void, 1),
547 PROC(cancel_msg, cancargs, norep, args, void, 1),
548 PROC(unlock_msg, unlockargs, norep, args, void, 1),
549 PROC(granted_msg, testargs, norep, args, void, 1),
550 PROC(test_res, testres, norep, res, void, 1),
551 PROC(lock_res, lockres, norep, res, void, 1),
552 PROC(cancel_res, cancelres, norep, res, void, 1),
553 PROC(unlock_res, unlockres, norep, res, void, 1),
554 PROC(granted_res, res, norep, res, void, 1),
555 /* statd callback */
556 PROC(sm_notify, reboot, void, reboot, void, 1),
557 PROC(none, void, void, void, void, 1),
558 PROC(none, void, void, void, void, 1),
559 PROC(none, void, void, void, void, 1),
560 PROC(share, shareargs, shareres, args, res, Ck+St+1),
561 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
562 PROC(nm_lock, lockargs, res, args, res, Ck+St),
563 PROC(free_all, notify, void, args, void, 0),
564
565};