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