blob: b10f913aa06ae44f7d797920ed9e981b610caa5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/svc4proc.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/types.h>
11#include <linux/time.h>
12#include <linux/slab.h>
13#include <linux/in.h>
14#include <linux/sunrpc/svc.h>
15#include <linux/sunrpc/clnt.h>
16#include <linux/nfsd/nfsd.h>
17#include <linux/lockd/lockd.h>
18#include <linux/lockd/share.h>
19#include <linux/lockd/sm_inter.h>
20
21
22#define NLMDBG_FACILITY NLMDBG_CLIENT
23
24static u32 nlm4svc_callback(struct svc_rqst *, u32, struct nlm_res *);
Trond Myklebust963d8fe2006-01-03 09:55:04 +010025
26static const struct rpc_call_ops nlm4svc_callback_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * Obtain client and file from arguments
30 */
31static u32
32nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
33 struct nlm_host **hostp, struct nlm_file **filp)
34{
35 struct nlm_host *host = NULL;
36 struct nlm_file *file = NULL;
37 struct nlm_lock *lock = &argp->lock;
38 u32 error = 0;
39
40 /* nfsd callbacks must have been installed for this procedure */
41 if (!nlmsvc_ops)
42 return nlm_lck_denied_nolocks;
43
44 /* Obtain host handle */
45 if (!(host = nlmsvc_lookup_host(rqstp))
46 || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
47 goto no_locks;
48 *hostp = host;
49
50 /* Obtain file pointer. Not used by FREE_ALL call. */
51 if (filp != NULL) {
52 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
53 goto no_locks;
54 *filp = file;
55
56 /* Set up the missing parts of the file_lock structure */
57 lock->fl.fl_file = file->f_file;
58 lock->fl.fl_owner = (fl_owner_t) host;
59 lock->fl.fl_lmops = &nlmsvc_lock_operations;
60 }
61
62 return 0;
63
64no_locks:
65 if (host)
66 nlm_release_host(host);
67 if (error)
68 return error;
69 return nlm_lck_denied_nolocks;
70}
71
72/*
73 * NULL: Test for presence of service
74 */
75static int
76nlm4svc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
77{
78 dprintk("lockd: NULL called\n");
79 return rpc_success;
80}
81
82/*
83 * TEST: Check for conflicting lock
84 */
85static int
86nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
87 struct nlm_res *resp)
88{
89 struct nlm_host *host;
90 struct nlm_file *file;
91
92 dprintk("lockd: TEST4 called\n");
93 resp->cookie = argp->cookie;
94
95 /* Don't accept test requests during grace period */
96 if (nlmsvc_grace_period) {
97 resp->status = nlm_lck_denied_grace_period;
98 return rpc_success;
99 }
100
101 /* Obtain client and file */
102 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
103 return rpc_success;
104
105 /* Now check for conflicting locks */
106 resp->status = nlmsvc_testlock(file, &argp->lock, &resp->lock);
107
108 dprintk("lockd: TEST4 status %d\n", ntohl(resp->status));
109 nlm_release_host(host);
110 nlm_release_file(file);
111 return rpc_success;
112}
113
114static int
115nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
116 struct nlm_res *resp)
117{
118 struct nlm_host *host;
119 struct nlm_file *file;
120
121 dprintk("lockd: LOCK called\n");
122
123 resp->cookie = argp->cookie;
124
125 /* Don't accept new lock requests during grace period */
126 if (nlmsvc_grace_period && !argp->reclaim) {
127 resp->status = nlm_lck_denied_grace_period;
128 return rpc_success;
129 }
130
131 /* Obtain client and file */
132 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
133 return rpc_success;
134
135#if 0
136 /* If supplied state doesn't match current state, we assume it's
137 * an old request that time-warped somehow. Any error return would
138 * do in this case because it's irrelevant anyway.
139 *
140 * NB: We don't retrieve the remote host's state yet.
141 */
142 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
143 resp->status = nlm_lck_denied_nolocks;
144 } else
145#endif
146
147 /* Now try to lock the file */
148 resp->status = nlmsvc_lock(rqstp, file, &argp->lock,
149 argp->block, &argp->cookie);
150
151 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
152 nlm_release_host(host);
153 nlm_release_file(file);
154 return rpc_success;
155}
156
157static int
158nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
159 struct nlm_res *resp)
160{
161 struct nlm_host *host;
162 struct nlm_file *file;
163
164 dprintk("lockd: CANCEL called\n");
165
166 resp->cookie = argp->cookie;
167
168 /* Don't accept requests during grace period */
169 if (nlmsvc_grace_period) {
170 resp->status = nlm_lck_denied_grace_period;
171 return rpc_success;
172 }
173
174 /* Obtain client and file */
175 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
176 return rpc_success;
177
178 /* Try to cancel request. */
179 resp->status = nlmsvc_cancel_blocked(file, &argp->lock);
180
181 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
182 nlm_release_host(host);
183 nlm_release_file(file);
184 return rpc_success;
185}
186
187/*
188 * UNLOCK: release a lock
189 */
190static int
191nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
192 struct nlm_res *resp)
193{
194 struct nlm_host *host;
195 struct nlm_file *file;
196
197 dprintk("lockd: UNLOCK called\n");
198
199 resp->cookie = argp->cookie;
200
201 /* Don't accept new lock requests during grace period */
202 if (nlmsvc_grace_period) {
203 resp->status = nlm_lck_denied_grace_period;
204 return rpc_success;
205 }
206
207 /* Obtain client and file */
208 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
209 return rpc_success;
210
211 /* Now try to remove the lock */
212 resp->status = nlmsvc_unlock(file, &argp->lock);
213
214 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
215 nlm_release_host(host);
216 nlm_release_file(file);
217 return rpc_success;
218}
219
220/*
221 * GRANTED: A server calls us to tell that a process' lock request
222 * was granted
223 */
224static int
225nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
226 struct nlm_res *resp)
227{
228 resp->cookie = argp->cookie;
229
230 dprintk("lockd: GRANTED called\n");
Trond Myklebust5ac5f9d2006-02-14 13:53:04 -0800231 resp->status = nlmclnt_grant(&rqstp->rq_addr, &argp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
233 return rpc_success;
234}
235
236/*
237 * `Async' versions of the above service routines. They aren't really,
238 * because we send the callback before the reply proper. I hope this
239 * doesn't break any clients.
240 */
241static int
242nlm4svc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
243 void *resp)
244{
245 struct nlm_res res;
246 u32 stat;
247
248 dprintk("lockd: TEST_MSG called\n");
249 memset(&res, 0, sizeof(res));
250
251 if ((stat = nlm4svc_proc_test(rqstp, argp, &res)) == 0)
252 stat = nlm4svc_callback(rqstp, NLMPROC_TEST_RES, &res);
253 return stat;
254}
255
256static int
257nlm4svc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
258 void *resp)
259{
260 struct nlm_res res;
261 u32 stat;
262
263 dprintk("lockd: LOCK_MSG called\n");
264 memset(&res, 0, sizeof(res));
265
266 if ((stat = nlm4svc_proc_lock(rqstp, argp, &res)) == 0)
267 stat = nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, &res);
268 return stat;
269}
270
271static int
272nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
273 void *resp)
274{
275 struct nlm_res res;
276 u32 stat;
277
278 dprintk("lockd: CANCEL_MSG called\n");
279 memset(&res, 0, sizeof(res));
280
281 if ((stat = nlm4svc_proc_cancel(rqstp, argp, &res)) == 0)
282 stat = nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
283 return stat;
284}
285
286static int
287nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
288 void *resp)
289{
290 struct nlm_res res;
291 u32 stat;
292
293 dprintk("lockd: UNLOCK_MSG called\n");
294 memset(&res, 0, sizeof(res));
295
296 if ((stat = nlm4svc_proc_unlock(rqstp, argp, &res)) == 0)
297 stat = nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
298 return stat;
299}
300
301static int
302nlm4svc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
303 void *resp)
304{
305 struct nlm_res res;
306 u32 stat;
307
308 dprintk("lockd: GRANTED_MSG called\n");
309 memset(&res, 0, sizeof(res));
310
311 if ((stat = nlm4svc_proc_granted(rqstp, argp, &res)) == 0)
312 stat = nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
313 return stat;
314}
315
316/*
317 * SHARE: create a DOS share or alter existing share.
318 */
319static int
320nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
321 struct nlm_res *resp)
322{
323 struct nlm_host *host;
324 struct nlm_file *file;
325
326 dprintk("lockd: SHARE called\n");
327
328 resp->cookie = argp->cookie;
329
330 /* Don't accept new lock requests during grace period */
331 if (nlmsvc_grace_period && !argp->reclaim) {
332 resp->status = nlm_lck_denied_grace_period;
333 return rpc_success;
334 }
335
336 /* Obtain client and file */
337 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
338 return rpc_success;
339
340 /* Now try to create the share */
341 resp->status = nlmsvc_share_file(host, file, argp);
342
343 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
344 nlm_release_host(host);
345 nlm_release_file(file);
346 return rpc_success;
347}
348
349/*
350 * UNSHARE: Release a DOS share.
351 */
352static int
353nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
354 struct nlm_res *resp)
355{
356 struct nlm_host *host;
357 struct nlm_file *file;
358
359 dprintk("lockd: UNSHARE called\n");
360
361 resp->cookie = argp->cookie;
362
363 /* Don't accept requests during grace period */
364 if (nlmsvc_grace_period) {
365 resp->status = nlm_lck_denied_grace_period;
366 return rpc_success;
367 }
368
369 /* Obtain client and file */
370 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
371 return rpc_success;
372
373 /* Now try to lock the file */
374 resp->status = nlmsvc_unshare_file(host, file, argp);
375
376 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
377 nlm_release_host(host);
378 nlm_release_file(file);
379 return rpc_success;
380}
381
382/*
383 * NM_LOCK: Create an unmonitored lock
384 */
385static int
386nlm4svc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
387 struct nlm_res *resp)
388{
389 dprintk("lockd: NM_LOCK called\n");
390
391 argp->monitor = 0; /* just clean the monitor flag */
392 return nlm4svc_proc_lock(rqstp, argp, resp);
393}
394
395/*
396 * FREE_ALL: Release all locks and shares held by client
397 */
398static int
399nlm4svc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
400 void *resp)
401{
402 struct nlm_host *host;
403
404 /* Obtain client */
405 if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
406 return rpc_success;
407
408 nlmsvc_free_host_resources(host);
409 nlm_release_host(host);
410 return rpc_success;
411}
412
413/*
414 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
415 */
416static int
417nlm4svc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
418 void *resp)
419{
420 struct sockaddr_in saddr = rqstp->rq_addr;
421 int vers = argp->vers;
422 int prot = argp->proto >> 1;
423
424 struct nlm_host *host;
425
426 dprintk("lockd: SM_NOTIFY called\n");
427 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
428 || ntohs(saddr.sin_port) >= 1024) {
429 printk(KERN_WARNING
430 "lockd: rejected NSM callback from %08x:%d\n",
431 ntohl(rqstp->rq_addr.sin_addr.s_addr),
432 ntohs(rqstp->rq_addr.sin_port));
433 return rpc_system_err;
434 }
435
436 /* Obtain the host pointer for this NFS server and try to
437 * reclaim all locks we hold on this server.
438 */
439 saddr.sin_addr.s_addr = argp->addr;
440
441 if ((argp->proto & 1)==0) {
442 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
443 nlmclnt_recovery(host, argp->state);
444 nlm_release_host(host);
445 }
446 } else {
447 /* If we run on an NFS server, delete all locks held by the client */
448
449 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
450 nlmsvc_free_host_resources(host);
451 nlm_release_host(host);
452 }
453 }
454 return rpc_success;
455}
456
457/*
458 * client sent a GRANTED_RES, let's remove the associated block
459 */
460static int
461nlm4svc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
462 void *resp)
463{
464 if (!nlmsvc_ops)
465 return rpc_success;
466
467 dprintk("lockd: GRANTED_RES called\n");
468
469 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
470 return rpc_success;
471}
472
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * This is the generic lockd callback for async RPC calls
476 */
477static u32
478nlm4svc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
479{
480 struct nlm_host *host;
481 struct nlm_rqst *call;
482
483 if (!(call = nlmclnt_alloc_call()))
484 return rpc_system_err;
485
486 host = nlmclnt_lookup_host(&rqstp->rq_addr,
487 rqstp->rq_prot, rqstp->rq_vers);
488 if (!host) {
489 kfree(call);
490 return rpc_system_err;
491 }
492
493 call->a_flags = RPC_TASK_ASYNC;
494 call->a_host = host;
495 memcpy(&call->a_args, resp, sizeof(*resp));
496
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100497 if (nlmsvc_async_call(call, proc, &nlm4svc_callback_ops) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto error;
499
500 return rpc_success;
501 error:
502 kfree(call);
503 nlm_release_host(host);
504 return rpc_system_err;
505}
506
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100507static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100509 struct nlm_rqst *call = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (task->tk_status < 0) {
512 dprintk("lockd: %4d callback failed (errno = %d)\n",
513 task->tk_pid, -task->tk_status);
514 }
515 nlm_release_host(call->a_host);
516 kfree(call);
517}
518
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100519static const struct rpc_call_ops nlm4svc_callback_ops = {
520 .rpc_call_done = nlm4svc_callback_exit,
521};
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*
524 * NLM Server procedures.
525 */
526
527#define nlm4svc_encode_norep nlm4svc_encode_void
528#define nlm4svc_decode_norep nlm4svc_decode_void
529#define nlm4svc_decode_testres nlm4svc_decode_void
530#define nlm4svc_decode_lockres nlm4svc_decode_void
531#define nlm4svc_decode_unlockres nlm4svc_decode_void
532#define nlm4svc_decode_cancelres nlm4svc_decode_void
533#define nlm4svc_decode_grantedres nlm4svc_decode_void
534
535#define nlm4svc_proc_none nlm4svc_proc_null
536#define nlm4svc_proc_test_res nlm4svc_proc_null
537#define nlm4svc_proc_lock_res nlm4svc_proc_null
538#define nlm4svc_proc_cancel_res nlm4svc_proc_null
539#define nlm4svc_proc_unlock_res nlm4svc_proc_null
540
541struct nlm_void { int dummy; };
542
543#define PROC(name, xargt, xrest, argt, rest, respsize) \
544 { .pc_func = (svc_procfunc) nlm4svc_proc_##name, \
545 .pc_decode = (kxdrproc_t) nlm4svc_decode_##xargt, \
546 .pc_encode = (kxdrproc_t) nlm4svc_encode_##xrest, \
547 .pc_release = NULL, \
548 .pc_argsize = sizeof(struct nlm_##argt), \
549 .pc_ressize = sizeof(struct nlm_##rest), \
550 .pc_xdrressize = respsize, \
551 }
552#define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
553#define No (1+1024/4) /* netobj */
554#define St 1 /* status */
555#define Rg 4 /* range (offset + length) */
556struct svc_procedure nlmsvc_procedures4[] = {
557 PROC(null, void, void, void, void, 1),
558 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
559 PROC(lock, lockargs, res, args, res, Ck+St),
560 PROC(cancel, cancargs, res, args, res, Ck+St),
561 PROC(unlock, unlockargs, res, args, res, Ck+St),
562 PROC(granted, testargs, res, args, res, Ck+St),
563 PROC(test_msg, testargs, norep, args, void, 1),
564 PROC(lock_msg, lockargs, norep, args, void, 1),
565 PROC(cancel_msg, cancargs, norep, args, void, 1),
566 PROC(unlock_msg, unlockargs, norep, args, void, 1),
567 PROC(granted_msg, testargs, norep, args, void, 1),
568 PROC(test_res, testres, norep, res, void, 1),
569 PROC(lock_res, lockres, norep, res, void, 1),
570 PROC(cancel_res, cancelres, norep, res, void, 1),
571 PROC(unlock_res, unlockres, norep, res, void, 1),
572 PROC(granted_res, res, norep, res, void, 1),
573 /* statd callback */
574 PROC(sm_notify, reboot, void, reboot, void, 1),
575 PROC(none, void, void, void, void, 0),
576 PROC(none, void, void, void, void, 0),
577 PROC(none, void, void, void, void, 0),
578 PROC(share, shareargs, shareres, args, res, Ck+St+1),
579 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
580 PROC(nm_lock, lockargs, res, args, res, Ck+St),
581 PROC(free_all, notify, void, args, void, 1),
582
583};