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;
87 lock->fl.fl_lmops = &nlmsvc_lock_operations;
94 nlm_release_host(host);
95 return nlm_lck_denied_nolocks;
99 * NULL: Test for presence of service
102 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
104 dprintk("lockd: NULL called\n");
109 * TEST: Check for conflicting lock
112 nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
113 struct nlm_res *resp)
115 struct nlm_host *host;
116 struct nlm_file *file;
118 dprintk("lockd: TEST called\n");
119 resp->cookie = argp->cookie;
121 /* Don't accept test requests during grace period */
122 if (nlmsvc_grace_period) {
123 resp->status = nlm_lck_denied_grace_period;
127 /* Obtain client and file */
128 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
131 /* Now check for conflicting locks */
132 resp->status = cast_status(nlmsvc_testlock(file, &argp->lock, &resp->lock));
134 dprintk("lockd: TEST status %d vers %d\n",
135 ntohl(resp->status), rqstp->rq_vers);
136 nlm_release_host(host);
137 nlm_release_file(file);
142 nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
143 struct nlm_res *resp)
145 struct nlm_host *host;
146 struct nlm_file *file;
148 dprintk("lockd: LOCK called\n");
150 resp->cookie = argp->cookie;
152 /* Don't accept new lock requests during grace period */
153 if (nlmsvc_grace_period && !argp->reclaim) {
154 resp->status = nlm_lck_denied_grace_period;
158 /* Obtain client and file */
159 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
163 /* If supplied state doesn't match current state, we assume it's
164 * an old request that time-warped somehow. Any error return would
165 * do in this case because it's irrelevant anyway.
167 * NB: We don't retrieve the remote host's state yet.
169 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
170 resp->status = nlm_lck_denied_nolocks;
174 /* Now try to lock the file */
175 resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock,
176 argp->block, &argp->cookie));
178 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
179 nlm_release_host(host);
180 nlm_release_file(file);
185 nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
186 struct nlm_res *resp)
188 struct nlm_host *host;
189 struct nlm_file *file;
191 dprintk("lockd: CANCEL called\n");
193 resp->cookie = argp->cookie;
195 /* Don't accept requests during grace period */
196 if (nlmsvc_grace_period) {
197 resp->status = nlm_lck_denied_grace_period;
201 /* Obtain client and file */
202 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
205 /* Try to cancel request. */
206 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
208 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
209 nlm_release_host(host);
210 nlm_release_file(file);
215 * UNLOCK: release a lock
218 nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
219 struct nlm_res *resp)
221 struct nlm_host *host;
222 struct nlm_file *file;
224 dprintk("lockd: UNLOCK called\n");
226 resp->cookie = argp->cookie;
228 /* Don't accept new lock requests during grace period */
229 if (nlmsvc_grace_period) {
230 resp->status = nlm_lck_denied_grace_period;
234 /* Obtain client and file */
235 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
238 /* Now try to remove the lock */
239 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
241 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
242 nlm_release_host(host);
243 nlm_release_file(file);
248 * GRANTED: A server calls us to tell that a process' lock request
252 nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
253 struct nlm_res *resp)
255 resp->cookie = argp->cookie;
257 dprintk("lockd: GRANTED called\n");
258 resp->status = nlmclnt_grant(&argp->lock);
259 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
264 * `Async' versions of the above service routines. They aren't really,
265 * because we send the callback before the reply proper. I hope this
266 * doesn't break any clients.
269 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
275 dprintk("lockd: TEST_MSG called\n");
276 memset(&res, 0, sizeof(res));
278 if ((stat = nlmsvc_proc_test(rqstp, argp, &res)) == 0)
279 stat = nlmsvc_callback(rqstp, NLMPROC_TEST_RES, &res);
284 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
290 dprintk("lockd: LOCK_MSG called\n");
291 memset(&res, 0, sizeof(res));
293 if ((stat = nlmsvc_proc_lock(rqstp, argp, &res)) == 0)
294 stat = nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, &res);
299 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
305 dprintk("lockd: CANCEL_MSG called\n");
306 memset(&res, 0, sizeof(res));
308 if ((stat = nlmsvc_proc_cancel(rqstp, argp, &res)) == 0)
309 stat = nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
314 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
320 dprintk("lockd: UNLOCK_MSG called\n");
321 memset(&res, 0, sizeof(res));
323 if ((stat = nlmsvc_proc_unlock(rqstp, argp, &res)) == 0)
324 stat = nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
329 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
335 dprintk("lockd: GRANTED_MSG called\n");
336 memset(&res, 0, sizeof(res));
338 if ((stat = nlmsvc_proc_granted(rqstp, argp, &res)) == 0)
339 stat = nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
344 * SHARE: create a DOS share or alter existing share.
347 nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
348 struct nlm_res *resp)
350 struct nlm_host *host;
351 struct nlm_file *file;
353 dprintk("lockd: SHARE called\n");
355 resp->cookie = argp->cookie;
357 /* Don't accept new lock requests during grace period */
358 if (nlmsvc_grace_period && !argp->reclaim) {
359 resp->status = nlm_lck_denied_grace_period;
363 /* Obtain client and file */
364 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
367 /* Now try to create the share */
368 resp->status = cast_status(nlmsvc_share_file(host, file, argp));
370 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
371 nlm_release_host(host);
372 nlm_release_file(file);
377 * UNSHARE: Release a DOS share.
380 nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
381 struct nlm_res *resp)
383 struct nlm_host *host;
384 struct nlm_file *file;
386 dprintk("lockd: UNSHARE called\n");
388 resp->cookie = argp->cookie;
390 /* Don't accept requests during grace period */
391 if (nlmsvc_grace_period) {
392 resp->status = nlm_lck_denied_grace_period;
396 /* Obtain client and file */
397 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
400 /* Now try to unshare the file */
401 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
403 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
404 nlm_release_host(host);
405 nlm_release_file(file);
410 * NM_LOCK: Create an unmonitored lock
413 nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
414 struct nlm_res *resp)
416 dprintk("lockd: NM_LOCK called\n");
418 argp->monitor = 0; /* just clean the monitor flag */
419 return nlmsvc_proc_lock(rqstp, argp, resp);
423 * FREE_ALL: Release all locks and shares held by client
426 nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
429 struct nlm_host *host;
432 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
435 nlmsvc_free_host_resources(host);
436 nlm_release_host(host);
441 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
444 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
447 struct sockaddr_in saddr = rqstp->rq_addr;
448 int vers = argp->vers;
449 int prot = argp->proto >> 1;
450 struct nlm_host *host;
452 dprintk("lockd: SM_NOTIFY called\n");
453 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
454 || ntohs(saddr.sin_port) >= 1024) {
456 "lockd: rejected NSM callback from %08x:%d\n",
457 ntohl(rqstp->rq_addr.sin_addr.s_addr),
458 ntohs(rqstp->rq_addr.sin_port));
459 return rpc_system_err;
462 /* Obtain the host pointer for this NFS server and try to
463 * reclaim all locks we hold on this server.
465 saddr.sin_addr.s_addr = argp->addr;
466 if ((argp->proto & 1)==0) {
467 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
468 nlmclnt_recovery(host, argp->state);
469 nlm_release_host(host);
472 /* If we run on an NFS server, delete all locks held by the client */
473 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
474 nlmsvc_free_host_resources(host);
475 nlm_release_host(host);
483 * client sent a GRANTED_RES, let's remove the associated block
486 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
492 dprintk("lockd: GRANTED_RES called\n");
494 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
499 * This is the generic lockd callback for async RPC calls
502 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
504 struct nlm_host *host;
505 struct nlm_rqst *call;
507 if (!(call = nlmclnt_alloc_call()))
508 return rpc_system_err;
510 host = nlmclnt_lookup_host(&rqstp->rq_addr,
511 rqstp->rq_prot, rqstp->rq_vers);
514 return rpc_system_err;
517 call->a_flags = RPC_TASK_ASYNC;
519 memcpy(&call->a_args, resp, sizeof(*resp));
521 if (nlmsvc_async_call(call, proc, nlmsvc_callback_exit) < 0)
526 nlm_release_host(host);
528 return rpc_system_err;
532 nlmsvc_callback_exit(struct rpc_task *task)
534 struct nlm_rqst *call = (struct nlm_rqst *) task->tk_calldata;
536 if (task->tk_status < 0) {
537 dprintk("lockd: %4d callback failed (errno = %d)\n",
538 task->tk_pid, -task->tk_status);
540 nlm_release_host(call->a_host);
545 * NLM Server procedures.
548 #define nlmsvc_encode_norep nlmsvc_encode_void
549 #define nlmsvc_decode_norep nlmsvc_decode_void
550 #define nlmsvc_decode_testres nlmsvc_decode_void
551 #define nlmsvc_decode_lockres nlmsvc_decode_void
552 #define nlmsvc_decode_unlockres nlmsvc_decode_void
553 #define nlmsvc_decode_cancelres nlmsvc_decode_void
554 #define nlmsvc_decode_grantedres nlmsvc_decode_void
556 #define nlmsvc_proc_none nlmsvc_proc_null
557 #define nlmsvc_proc_test_res nlmsvc_proc_null
558 #define nlmsvc_proc_lock_res nlmsvc_proc_null
559 #define nlmsvc_proc_cancel_res nlmsvc_proc_null
560 #define nlmsvc_proc_unlock_res nlmsvc_proc_null
562 struct nlm_void { int dummy; };
564 #define PROC(name, xargt, xrest, argt, rest, respsize) \
565 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
566 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
567 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
568 .pc_release = NULL, \
569 .pc_argsize = sizeof(struct nlm_##argt), \
570 .pc_ressize = sizeof(struct nlm_##rest), \
571 .pc_xdrressize = respsize, \
574 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
575 #define St 1 /* status */
576 #define No (1+1024/4) /* Net Obj */
577 #define Rg 2 /* range - offset + size */
579 struct svc_procedure nlmsvc_procedures[] = {
580 PROC(null, void, void, void, void, 1),
581 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
582 PROC(lock, lockargs, res, args, res, Ck+St),
583 PROC(cancel, cancargs, res, args, res, Ck+St),
584 PROC(unlock, unlockargs, res, args, res, Ck+St),
585 PROC(granted, testargs, res, args, res, Ck+St),
586 PROC(test_msg, testargs, norep, args, void, 1),
587 PROC(lock_msg, lockargs, norep, args, void, 1),
588 PROC(cancel_msg, cancargs, norep, args, void, 1),
589 PROC(unlock_msg, unlockargs, norep, args, void, 1),
590 PROC(granted_msg, testargs, norep, args, void, 1),
591 PROC(test_res, testres, norep, res, void, 1),
592 PROC(lock_res, lockres, norep, res, void, 1),
593 PROC(cancel_res, cancelres, norep, res, void, 1),
594 PROC(unlock_res, unlockres, norep, res, void, 1),
595 PROC(granted_res, res, norep, res, void, 1),
597 PROC(sm_notify, reboot, void, reboot, void, 1),
598 PROC(none, void, void, void, void, 1),
599 PROC(none, void, void, void, void, 1),
600 PROC(none, void, void, void, void, 1),
601 PROC(share, shareargs, shareres, args, res, Ck+St+1),
602 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
603 PROC(nm_lock, lockargs, res, args, res, Ck+St),
604 PROC(free_all, notify, void, args, void, 0),