2 * linux/fs/lockd/svc4proc.c
4 * Lockd server procedures. We don't implement the NLM_*_RES
5 * procedures because we don't use the async procedures.
7 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
10 #include <linux/types.h>
11 #include <linux/time.h>
12 #include <linux/slab.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>
22 #define NLMDBG_FACILITY NLMDBG_CLIENT
24 static u32 nlm4svc_callback(struct svc_rqst *, u32, struct nlm_res *);
26 static const struct rpc_call_ops nlm4svc_callback_ops;
29 * Obtain client and file from arguments
32 nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
33 struct nlm_host **hostp, struct nlm_file **filp)
35 struct nlm_host *host = NULL;
36 struct nlm_file *file = NULL;
37 struct nlm_lock *lock = &argp->lock;
40 /* nfsd callbacks must have been installed for this procedure */
42 return nlm_lck_denied_nolocks;
44 /* Obtain host handle */
45 if (!(host = nlmsvc_lookup_host(rqstp))
46 || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
50 /* Obtain file pointer. Not used by FREE_ALL call. */
52 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
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;
66 nlm_release_host(host);
69 return nlm_lck_denied_nolocks;
73 * NULL: Test for presence of service
76 nlm4svc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
78 dprintk("lockd: NULL called\n");
83 * TEST: Check for conflicting lock
86 nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
89 struct nlm_host *host;
90 struct nlm_file *file;
92 dprintk("lockd: TEST4 called\n");
93 resp->cookie = argp->cookie;
95 /* Don't accept test requests during grace period */
96 if (nlmsvc_grace_period) {
97 resp->status = nlm_lck_denied_grace_period;
101 /* Obtain client and file */
102 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
105 /* Now check for conflicting locks */
106 resp->status = nlmsvc_testlock(file, &argp->lock, &resp->lock);
108 dprintk("lockd: TEST4 status %d\n", ntohl(resp->status));
109 nlm_release_host(host);
110 nlm_release_file(file);
115 nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
116 struct nlm_res *resp)
118 struct nlm_host *host;
119 struct nlm_file *file;
121 dprintk("lockd: LOCK called\n");
123 resp->cookie = argp->cookie;
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;
131 /* Obtain client and file */
132 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
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.
140 * NB: We don't retrieve the remote host's state yet.
142 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
143 resp->status = nlm_lck_denied_nolocks;
147 /* Now try to lock the file */
148 resp->status = nlmsvc_lock(rqstp, file, &argp->lock,
149 argp->block, &argp->cookie);
151 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
152 nlm_release_host(host);
153 nlm_release_file(file);
158 nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
159 struct nlm_res *resp)
161 struct nlm_host *host;
162 struct nlm_file *file;
164 dprintk("lockd: CANCEL called\n");
166 resp->cookie = argp->cookie;
168 /* Don't accept requests during grace period */
169 if (nlmsvc_grace_period) {
170 resp->status = nlm_lck_denied_grace_period;
174 /* Obtain client and file */
175 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
178 /* Try to cancel request. */
179 resp->status = nlmsvc_cancel_blocked(file, &argp->lock);
181 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
182 nlm_release_host(host);
183 nlm_release_file(file);
188 * UNLOCK: release a lock
191 nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
192 struct nlm_res *resp)
194 struct nlm_host *host;
195 struct nlm_file *file;
197 dprintk("lockd: UNLOCK called\n");
199 resp->cookie = argp->cookie;
201 /* Don't accept new lock requests during grace period */
202 if (nlmsvc_grace_period) {
203 resp->status = nlm_lck_denied_grace_period;
207 /* Obtain client and file */
208 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
211 /* Now try to remove the lock */
212 resp->status = nlmsvc_unlock(file, &argp->lock);
214 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
215 nlm_release_host(host);
216 nlm_release_file(file);
221 * GRANTED: A server calls us to tell that a process' lock request
225 nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
226 struct nlm_res *resp)
228 resp->cookie = argp->cookie;
230 dprintk("lockd: GRANTED called\n");
231 resp->status = nlmclnt_grant(&rqstp->rq_addr, &argp->lock);
232 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
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.
242 nlm4svc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
248 dprintk("lockd: TEST_MSG called\n");
249 memset(&res, 0, sizeof(res));
251 if ((stat = nlm4svc_proc_test(rqstp, argp, &res)) == 0)
252 stat = nlm4svc_callback(rqstp, NLMPROC_TEST_RES, &res);
257 nlm4svc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
263 dprintk("lockd: LOCK_MSG called\n");
264 memset(&res, 0, sizeof(res));
266 if ((stat = nlm4svc_proc_lock(rqstp, argp, &res)) == 0)
267 stat = nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, &res);
272 nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
278 dprintk("lockd: CANCEL_MSG called\n");
279 memset(&res, 0, sizeof(res));
281 if ((stat = nlm4svc_proc_cancel(rqstp, argp, &res)) == 0)
282 stat = nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
287 nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
293 dprintk("lockd: UNLOCK_MSG called\n");
294 memset(&res, 0, sizeof(res));
296 if ((stat = nlm4svc_proc_unlock(rqstp, argp, &res)) == 0)
297 stat = nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
302 nlm4svc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
308 dprintk("lockd: GRANTED_MSG called\n");
309 memset(&res, 0, sizeof(res));
311 if ((stat = nlm4svc_proc_granted(rqstp, argp, &res)) == 0)
312 stat = nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
317 * SHARE: create a DOS share or alter existing share.
320 nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
321 struct nlm_res *resp)
323 struct nlm_host *host;
324 struct nlm_file *file;
326 dprintk("lockd: SHARE called\n");
328 resp->cookie = argp->cookie;
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;
336 /* Obtain client and file */
337 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
340 /* Now try to create the share */
341 resp->status = nlmsvc_share_file(host, file, argp);
343 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
344 nlm_release_host(host);
345 nlm_release_file(file);
350 * UNSHARE: Release a DOS share.
353 nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
354 struct nlm_res *resp)
356 struct nlm_host *host;
357 struct nlm_file *file;
359 dprintk("lockd: UNSHARE called\n");
361 resp->cookie = argp->cookie;
363 /* Don't accept requests during grace period */
364 if (nlmsvc_grace_period) {
365 resp->status = nlm_lck_denied_grace_period;
369 /* Obtain client and file */
370 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
373 /* Now try to lock the file */
374 resp->status = nlmsvc_unshare_file(host, file, argp);
376 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
377 nlm_release_host(host);
378 nlm_release_file(file);
383 * NM_LOCK: Create an unmonitored lock
386 nlm4svc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
387 struct nlm_res *resp)
389 dprintk("lockd: NM_LOCK called\n");
391 argp->monitor = 0; /* just clean the monitor flag */
392 return nlm4svc_proc_lock(rqstp, argp, resp);
396 * FREE_ALL: Release all locks and shares held by client
399 nlm4svc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
402 struct nlm_host *host;
405 if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
408 nlmsvc_free_host_resources(host);
409 nlm_release_host(host);
414 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
417 nlm4svc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
420 struct sockaddr_in saddr = rqstp->rq_addr;
421 int vers = argp->vers;
422 int prot = argp->proto >> 1;
424 struct nlm_host *host;
426 dprintk("lockd: SM_NOTIFY called\n");
427 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
428 || ntohs(saddr.sin_port) >= 1024) {
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;
436 /* Obtain the host pointer for this NFS server and try to
437 * reclaim all locks we hold on this server.
439 saddr.sin_addr.s_addr = argp->addr;
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);
447 /* If we run on an NFS server, delete all locks held by the client */
449 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
450 nlmsvc_free_host_resources(host);
451 nlm_release_host(host);
458 * client sent a GRANTED_RES, let's remove the associated block
461 nlm4svc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
467 dprintk("lockd: GRANTED_RES called\n");
469 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
475 * This is the generic lockd callback for async RPC calls
478 nlm4svc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
480 struct nlm_host *host;
481 struct nlm_rqst *call;
483 host = nlmsvc_lookup_host(rqstp);
485 return rpc_system_err;
487 call = nlm_alloc_call(host);
489 return rpc_system_err;
492 call->a_flags = RPC_TASK_ASYNC;
493 memcpy(&call->a_args, resp, sizeof(*resp));
495 if (nlm_async_call(call, proc, &nlm4svc_callback_ops) < 0)
496 return rpc_system_err;
500 static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
502 dprintk("lockd: %4d callback returned %d\n", task->tk_pid,
506 static void nlm4svc_callback_release(void *data)
508 nlm_release_call(data);
511 static const struct rpc_call_ops nlm4svc_callback_ops = {
512 .rpc_call_done = nlm4svc_callback_exit,
513 .rpc_release = nlm4svc_callback_release,
517 * NLM Server procedures.
520 #define nlm4svc_encode_norep nlm4svc_encode_void
521 #define nlm4svc_decode_norep nlm4svc_decode_void
522 #define nlm4svc_decode_testres nlm4svc_decode_void
523 #define nlm4svc_decode_lockres nlm4svc_decode_void
524 #define nlm4svc_decode_unlockres nlm4svc_decode_void
525 #define nlm4svc_decode_cancelres nlm4svc_decode_void
526 #define nlm4svc_decode_grantedres nlm4svc_decode_void
528 #define nlm4svc_proc_none nlm4svc_proc_null
529 #define nlm4svc_proc_test_res nlm4svc_proc_null
530 #define nlm4svc_proc_lock_res nlm4svc_proc_null
531 #define nlm4svc_proc_cancel_res nlm4svc_proc_null
532 #define nlm4svc_proc_unlock_res nlm4svc_proc_null
534 struct nlm_void { int dummy; };
536 #define PROC(name, xargt, xrest, argt, rest, respsize) \
537 { .pc_func = (svc_procfunc) nlm4svc_proc_##name, \
538 .pc_decode = (kxdrproc_t) nlm4svc_decode_##xargt, \
539 .pc_encode = (kxdrproc_t) nlm4svc_encode_##xrest, \
540 .pc_release = NULL, \
541 .pc_argsize = sizeof(struct nlm_##argt), \
542 .pc_ressize = sizeof(struct nlm_##rest), \
543 .pc_xdrressize = respsize, \
545 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
546 #define No (1+1024/4) /* netobj */
547 #define St 1 /* status */
548 #define Rg 4 /* range (offset + length) */
549 struct svc_procedure nlmsvc_procedures4[] = {
550 PROC(null, void, void, void, void, 1),
551 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
552 PROC(lock, lockargs, res, args, res, Ck+St),
553 PROC(cancel, cancargs, res, args, res, Ck+St),
554 PROC(unlock, unlockargs, res, args, res, Ck+St),
555 PROC(granted, testargs, res, args, res, Ck+St),
556 PROC(test_msg, testargs, norep, args, void, 1),
557 PROC(lock_msg, lockargs, norep, args, void, 1),
558 PROC(cancel_msg, cancargs, norep, args, void, 1),
559 PROC(unlock_msg, unlockargs, norep, args, void, 1),
560 PROC(granted_msg, testargs, norep, args, void, 1),
561 PROC(test_res, testres, norep, res, void, 1),
562 PROC(lock_res, lockres, norep, res, void, 1),
563 PROC(cancel_res, cancelres, norep, res, void, 1),
564 PROC(unlock_res, unlockres, norep, res, void, 1),
565 PROC(granted_res, res, norep, res, void, 1),
567 PROC(sm_notify, reboot, void, reboot, void, 1),
568 PROC(none, void, void, void, void, 0),
569 PROC(none, void, void, void, void, 0),
570 PROC(none, void, void, void, void, 0),
571 PROC(share, shareargs, shareres, args, res, Ck+St+1),
572 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
573 PROC(nm_lock, lockargs, res, args, res, Ck+St),
574 PROC(free_all, notify, void, args, void, 1),