2 * linux/fs/lockd/svcproc.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/config.h>
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/slab.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>
23 #define NLMDBG_FACILITY NLMDBG_CLIENT
25 static u32 nlmsvc_callback(struct svc_rqst *, u32, struct nlm_res *);
26 static void nlmsvc_callback_exit(struct rpc_task *);
28 #ifdef CONFIG_LOCKD_V4
30 cast_to_nlm(u32 status, u32 vers)
32 /* Note: status is assumed to be in network byte order !!! */
37 case nlm_lck_denied_nolocks:
39 case nlm_lck_denied_grace_period:
42 status = nlm_lck_denied;
45 status = nlm_lck_denied_nolocks;
51 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
53 #define cast_status(status) (status)
57 * Obtain client and file from arguments
60 nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
61 struct nlm_host **hostp, struct nlm_file **filp)
63 struct nlm_host *host = NULL;
64 struct nlm_file *file = NULL;
65 struct nlm_lock *lock = &argp->lock;
68 /* nfsd callbacks must have been installed for this procedure */
70 return nlm_lck_denied_nolocks;
72 /* Obtain host handle */
73 if (!(host = nlmsvc_lookup_host(rqstp))
74 || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
78 /* Obtain file pointer. Not used by FREE_ALL call. */
80 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
84 /* Set up the missing parts of the file_lock structure */
85 lock->fl.fl_file = &file->f_file;
86 lock->fl.fl_owner = (fl_owner_t) host;
93 nlm_release_host(host);
94 return nlm_lck_denied_nolocks;
98 * NULL: Test for presence of service
101 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
103 dprintk("lockd: NULL called\n");
108 * TEST: Check for conflicting lock
111 nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
112 struct nlm_res *resp)
114 struct nlm_host *host;
115 struct nlm_file *file;
117 dprintk("lockd: TEST called\n");
118 resp->cookie = argp->cookie;
120 /* Don't accept test requests during grace period */
121 if (nlmsvc_grace_period) {
122 resp->status = nlm_lck_denied_grace_period;
126 /* Obtain client and file */
127 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
130 /* Now check for conflicting locks */
131 resp->status = cast_status(nlmsvc_testlock(file, &argp->lock, &resp->lock));
133 dprintk("lockd: TEST status %d vers %d\n",
134 ntohl(resp->status), rqstp->rq_vers);
135 nlm_release_host(host);
136 nlm_release_file(file);
141 nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
142 struct nlm_res *resp)
144 struct nlm_host *host;
145 struct nlm_file *file;
147 dprintk("lockd: LOCK called\n");
149 resp->cookie = argp->cookie;
151 /* Don't accept new lock requests during grace period */
152 if (nlmsvc_grace_period && !argp->reclaim) {
153 resp->status = nlm_lck_denied_grace_period;
157 /* Obtain client and file */
158 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
162 /* If supplied state doesn't match current state, we assume it's
163 * an old request that time-warped somehow. Any error return would
164 * do in this case because it's irrelevant anyway.
166 * NB: We don't retrieve the remote host's state yet.
168 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
169 resp->status = nlm_lck_denied_nolocks;
173 /* Now try to lock the file */
174 resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock,
175 argp->block, &argp->cookie));
177 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
178 nlm_release_host(host);
179 nlm_release_file(file);
184 nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
185 struct nlm_res *resp)
187 struct nlm_host *host;
188 struct nlm_file *file;
190 dprintk("lockd: CANCEL called\n");
192 resp->cookie = argp->cookie;
194 /* Don't accept requests during grace period */
195 if (nlmsvc_grace_period) {
196 resp->status = nlm_lck_denied_grace_period;
200 /* Obtain client and file */
201 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
204 /* Try to cancel request. */
205 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
207 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
208 nlm_release_host(host);
209 nlm_release_file(file);
214 * UNLOCK: release a lock
217 nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
218 struct nlm_res *resp)
220 struct nlm_host *host;
221 struct nlm_file *file;
223 dprintk("lockd: UNLOCK called\n");
225 resp->cookie = argp->cookie;
227 /* Don't accept new lock requests during grace period */
228 if (nlmsvc_grace_period) {
229 resp->status = nlm_lck_denied_grace_period;
233 /* Obtain client and file */
234 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
237 /* Now try to remove the lock */
238 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
240 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
241 nlm_release_host(host);
242 nlm_release_file(file);
247 * GRANTED: A server calls us to tell that a process' lock request
251 nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
252 struct nlm_res *resp)
254 resp->cookie = argp->cookie;
256 dprintk("lockd: GRANTED called\n");
257 resp->status = nlmclnt_grant(&argp->lock);
258 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
263 * `Async' versions of the above service routines. They aren't really,
264 * because we send the callback before the reply proper. I hope this
265 * doesn't break any clients.
268 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
274 dprintk("lockd: TEST_MSG called\n");
275 memset(&res, 0, sizeof(res));
277 if ((stat = nlmsvc_proc_test(rqstp, argp, &res)) == 0)
278 stat = nlmsvc_callback(rqstp, NLMPROC_TEST_RES, &res);
283 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
289 dprintk("lockd: LOCK_MSG called\n");
290 memset(&res, 0, sizeof(res));
292 if ((stat = nlmsvc_proc_lock(rqstp, argp, &res)) == 0)
293 stat = nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, &res);
298 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
304 dprintk("lockd: CANCEL_MSG called\n");
305 memset(&res, 0, sizeof(res));
307 if ((stat = nlmsvc_proc_cancel(rqstp, argp, &res)) == 0)
308 stat = nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
313 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
319 dprintk("lockd: UNLOCK_MSG called\n");
320 memset(&res, 0, sizeof(res));
322 if ((stat = nlmsvc_proc_unlock(rqstp, argp, &res)) == 0)
323 stat = nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
328 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
334 dprintk("lockd: GRANTED_MSG called\n");
335 memset(&res, 0, sizeof(res));
337 if ((stat = nlmsvc_proc_granted(rqstp, argp, &res)) == 0)
338 stat = nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
343 * SHARE: create a DOS share or alter existing share.
346 nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
347 struct nlm_res *resp)
349 struct nlm_host *host;
350 struct nlm_file *file;
352 dprintk("lockd: SHARE called\n");
354 resp->cookie = argp->cookie;
356 /* Don't accept new lock requests during grace period */
357 if (nlmsvc_grace_period && !argp->reclaim) {
358 resp->status = nlm_lck_denied_grace_period;
362 /* Obtain client and file */
363 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
366 /* Now try to create the share */
367 resp->status = cast_status(nlmsvc_share_file(host, file, argp));
369 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
370 nlm_release_host(host);
371 nlm_release_file(file);
376 * UNSHARE: Release a DOS share.
379 nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
380 struct nlm_res *resp)
382 struct nlm_host *host;
383 struct nlm_file *file;
385 dprintk("lockd: UNSHARE called\n");
387 resp->cookie = argp->cookie;
389 /* Don't accept requests during grace period */
390 if (nlmsvc_grace_period) {
391 resp->status = nlm_lck_denied_grace_period;
395 /* Obtain client and file */
396 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
399 /* Now try to unshare the file */
400 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
402 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
403 nlm_release_host(host);
404 nlm_release_file(file);
409 * NM_LOCK: Create an unmonitored lock
412 nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
413 struct nlm_res *resp)
415 dprintk("lockd: NM_LOCK called\n");
417 argp->monitor = 0; /* just clean the monitor flag */
418 return nlmsvc_proc_lock(rqstp, argp, resp);
422 * FREE_ALL: Release all locks and shares held by client
425 nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
428 struct nlm_host *host;
431 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
434 nlmsvc_free_host_resources(host);
435 nlm_release_host(host);
440 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
443 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
446 struct sockaddr_in saddr = rqstp->rq_addr;
447 int vers = argp->vers;
448 int prot = argp->proto >> 1;
449 struct nlm_host *host;
451 dprintk("lockd: SM_NOTIFY called\n");
452 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
453 || ntohs(saddr.sin_port) >= 1024) {
455 "lockd: rejected NSM callback from %08x:%d\n",
456 ntohl(rqstp->rq_addr.sin_addr.s_addr),
457 ntohs(rqstp->rq_addr.sin_port));
458 return rpc_system_err;
461 /* Obtain the host pointer for this NFS server and try to
462 * reclaim all locks we hold on this server.
464 saddr.sin_addr.s_addr = argp->addr;
465 if ((argp->proto & 1)==0) {
466 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
467 nlmclnt_recovery(host, argp->state);
468 nlm_release_host(host);
471 /* If we run on an NFS server, delete all locks held by the client */
472 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
473 nlmsvc_free_host_resources(host);
474 nlm_release_host(host);
482 * client sent a GRANTED_RES, let's remove the associated block
485 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
491 dprintk("lockd: GRANTED_RES called\n");
493 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
498 * This is the generic lockd callback for async RPC calls
501 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
503 struct nlm_host *host;
504 struct nlm_rqst *call;
506 if (!(call = nlmclnt_alloc_call()))
507 return rpc_system_err;
509 host = nlmclnt_lookup_host(&rqstp->rq_addr,
510 rqstp->rq_prot, rqstp->rq_vers);
513 return rpc_system_err;
516 call->a_flags = RPC_TASK_ASYNC;
518 memcpy(&call->a_args, resp, sizeof(*resp));
520 if (nlmsvc_async_call(call, proc, nlmsvc_callback_exit) < 0)
525 nlm_release_host(host);
527 return rpc_system_err;
531 nlmsvc_callback_exit(struct rpc_task *task)
533 struct nlm_rqst *call = (struct nlm_rqst *) task->tk_calldata;
535 if (task->tk_status < 0) {
536 dprintk("lockd: %4d callback failed (errno = %d)\n",
537 task->tk_pid, -task->tk_status);
539 nlm_release_host(call->a_host);
544 * NLM Server procedures.
547 #define nlmsvc_encode_norep nlmsvc_encode_void
548 #define nlmsvc_decode_norep nlmsvc_decode_void
549 #define nlmsvc_decode_testres nlmsvc_decode_void
550 #define nlmsvc_decode_lockres nlmsvc_decode_void
551 #define nlmsvc_decode_unlockres nlmsvc_decode_void
552 #define nlmsvc_decode_cancelres nlmsvc_decode_void
553 #define nlmsvc_decode_grantedres nlmsvc_decode_void
555 #define nlmsvc_proc_none nlmsvc_proc_null
556 #define nlmsvc_proc_test_res nlmsvc_proc_null
557 #define nlmsvc_proc_lock_res nlmsvc_proc_null
558 #define nlmsvc_proc_cancel_res nlmsvc_proc_null
559 #define nlmsvc_proc_unlock_res nlmsvc_proc_null
561 struct nlm_void { int dummy; };
563 #define PROC(name, xargt, xrest, argt, rest, respsize) \
564 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
565 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
566 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
567 .pc_release = NULL, \
568 .pc_argsize = sizeof(struct nlm_##argt), \
569 .pc_ressize = sizeof(struct nlm_##rest), \
570 .pc_xdrressize = respsize, \
573 #define Ck (1+8) /* cookie */
574 #define St 1 /* status */
575 #define No (1+1024/4) /* Net Obj */
576 #define Rg 2 /* range - offset + size */
578 struct svc_procedure nlmsvc_procedures[] = {
579 PROC(null, void, void, void, void, 1),
580 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
581 PROC(lock, lockargs, res, args, res, Ck+St),
582 PROC(cancel, cancargs, res, args, res, Ck+St),
583 PROC(unlock, unlockargs, res, args, res, Ck+St),
584 PROC(granted, testargs, res, args, res, Ck+St),
585 PROC(test_msg, testargs, norep, args, void, 1),
586 PROC(lock_msg, lockargs, norep, args, void, 1),
587 PROC(cancel_msg, cancargs, norep, args, void, 1),
588 PROC(unlock_msg, unlockargs, norep, args, void, 1),
589 PROC(granted_msg, testargs, norep, args, void, 1),
590 PROC(test_res, testres, norep, res, void, 1),
591 PROC(lock_res, lockres, norep, res, void, 1),
592 PROC(cancel_res, cancelres, norep, res, void, 1),
593 PROC(unlock_res, unlockres, norep, res, void, 1),
594 PROC(granted_res, res, norep, res, void, 1),
596 PROC(sm_notify, reboot, void, reboot, void, 1),
597 PROC(none, void, void, void, void, 1),
598 PROC(none, void, void, void, void, 1),
599 PROC(none, void, void, void, void, 1),
600 PROC(share, shareargs, shareres, args, res, Ck+St+1),
601 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
602 PROC(nm_lock, lockargs, res, args, res, Ck+St),
603 PROC(free_all, notify, void, args, void, 0),