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 *);
25 static void nlm4svc_callback_exit(struct rpc_task *);
28 * Obtain client and file from arguments
31 nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
32 struct nlm_host **hostp, struct nlm_file **filp)
34 struct nlm_host *host = NULL;
35 struct nlm_file *file = NULL;
36 struct nlm_lock *lock = &argp->lock;
39 /* nfsd callbacks must have been installed for this procedure */
41 return nlm_lck_denied_nolocks;
43 /* Obtain host handle */
44 if (!(host = nlmsvc_lookup_host(rqstp))
45 || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
49 /* Obtain file pointer. Not used by FREE_ALL call. */
51 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
55 /* Set up the missing parts of the file_lock structure */
56 lock->fl.fl_file = file->f_file;
57 lock->fl.fl_owner = (fl_owner_t) host;
58 lock->fl.fl_lmops = &nlmsvc_lock_operations;
65 nlm_release_host(host);
68 return nlm_lck_denied_nolocks;
72 * NULL: Test for presence of service
75 nlm4svc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
77 dprintk("lockd: NULL called\n");
82 * TEST: Check for conflicting lock
85 nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
88 struct nlm_host *host;
89 struct nlm_file *file;
91 dprintk("lockd: TEST4 called\n");
92 resp->cookie = argp->cookie;
94 /* Don't accept test requests during grace period */
95 if (nlmsvc_grace_period) {
96 resp->status = nlm_lck_denied_grace_period;
100 /* Obtain client and file */
101 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
104 /* Now check for conflicting locks */
105 resp->status = nlmsvc_testlock(file, &argp->lock, &resp->lock);
107 dprintk("lockd: TEST4 status %d\n", ntohl(resp->status));
108 nlm_release_host(host);
109 nlm_release_file(file);
114 nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
115 struct nlm_res *resp)
117 struct nlm_host *host;
118 struct nlm_file *file;
120 dprintk("lockd: LOCK called\n");
122 resp->cookie = argp->cookie;
124 /* Don't accept new lock requests during grace period */
125 if (nlmsvc_grace_period && !argp->reclaim) {
126 resp->status = nlm_lck_denied_grace_period;
130 /* Obtain client and file */
131 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file))) {
132 dprintk("lockd: LOCK(args) status %d\n", ntohl(resp->status));
133 if (resp->status == nlm_lck_dropit)
134 return nlm_lck_dropit;
138 /* If supplied state doesn't match current state, we assume it's
139 * an old request that time-warped somehow. Any error return would
140 * do in this case because it's irrelevant anyway.
142 * NB: We don't retrieve the remote host's state yet.
144 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
145 resp->status = nlm_lck_denied_nolocks;
149 /* Now try to lock the file */
150 resp->status = nlmsvc_lock(rqstp, file, &argp->lock,
151 argp->block, &argp->cookie);
153 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
154 nlm_release_host(host);
155 nlm_release_file(file);
160 nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
161 struct nlm_res *resp)
163 struct nlm_host *host;
164 struct nlm_file *file;
166 dprintk("lockd: CANCEL called\n");
168 resp->cookie = argp->cookie;
170 /* Don't accept requests during grace period */
171 if (nlmsvc_grace_period) {
172 resp->status = nlm_lck_denied_grace_period;
176 /* Obtain client and file */
177 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
180 /* Try to cancel request. */
181 resp->status = nlmsvc_cancel_blocked(file, &argp->lock);
183 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
184 nlm_release_host(host);
185 nlm_release_file(file);
190 * UNLOCK: release a lock
193 nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
194 struct nlm_res *resp)
196 struct nlm_host *host;
197 struct nlm_file *file;
199 dprintk("lockd: UNLOCK called\n");
201 resp->cookie = argp->cookie;
203 /* Don't accept new lock requests during grace period */
204 if (nlmsvc_grace_period) {
205 resp->status = nlm_lck_denied_grace_period;
209 /* Obtain client and file */
210 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
213 /* Now try to remove the lock */
214 resp->status = nlmsvc_unlock(file, &argp->lock);
216 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
217 nlm_release_host(host);
218 nlm_release_file(file);
223 * GRANTED: A server calls us to tell that a process' lock request
227 nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
228 struct nlm_res *resp)
230 resp->cookie = argp->cookie;
232 dprintk("lockd: GRANTED called\n");
233 resp->status = nlmclnt_grant(&argp->lock);
234 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
239 * `Async' versions of the above service routines. They aren't really,
240 * because we send the callback before the reply proper. I hope this
241 * doesn't break any clients.
244 nlm4svc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
250 dprintk("lockd: TEST_MSG called\n");
251 memset(&res, 0, sizeof(res));
253 if ((stat = nlm4svc_proc_test(rqstp, argp, &res)) == 0)
254 stat = nlm4svc_callback(rqstp, NLMPROC_TEST_RES, &res);
259 nlm4svc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
265 dprintk("lockd: LOCK_MSG called\n");
266 memset(&res, 0, sizeof(res));
268 if ((stat = nlm4svc_proc_lock(rqstp, argp, &res)) == 0)
269 stat = nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, &res);
274 nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
280 dprintk("lockd: CANCEL_MSG called\n");
281 memset(&res, 0, sizeof(res));
283 if ((stat = nlm4svc_proc_cancel(rqstp, argp, &res)) == 0)
284 stat = nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
289 nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
295 dprintk("lockd: UNLOCK_MSG called\n");
296 memset(&res, 0, sizeof(res));
298 if ((stat = nlm4svc_proc_unlock(rqstp, argp, &res)) == 0)
299 stat = nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
304 nlm4svc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
310 dprintk("lockd: GRANTED_MSG called\n");
311 memset(&res, 0, sizeof(res));
313 if ((stat = nlm4svc_proc_granted(rqstp, argp, &res)) == 0)
314 stat = nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
319 * SHARE: create a DOS share or alter existing share.
322 nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
323 struct nlm_res *resp)
325 struct nlm_host *host;
326 struct nlm_file *file;
328 dprintk("lockd: SHARE called\n");
330 resp->cookie = argp->cookie;
332 /* Don't accept new lock requests during grace period */
333 if (nlmsvc_grace_period && !argp->reclaim) {
334 resp->status = nlm_lck_denied_grace_period;
338 /* Obtain client and file */
339 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
342 /* Now try to create the share */
343 resp->status = nlmsvc_share_file(host, file, argp);
345 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
346 nlm_release_host(host);
347 nlm_release_file(file);
352 * UNSHARE: Release a DOS share.
355 nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
356 struct nlm_res *resp)
358 struct nlm_host *host;
359 struct nlm_file *file;
361 dprintk("lockd: UNSHARE called\n");
363 resp->cookie = argp->cookie;
365 /* Don't accept requests during grace period */
366 if (nlmsvc_grace_period) {
367 resp->status = nlm_lck_denied_grace_period;
371 /* Obtain client and file */
372 if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
375 /* Now try to lock the file */
376 resp->status = nlmsvc_unshare_file(host, file, argp);
378 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
379 nlm_release_host(host);
380 nlm_release_file(file);
385 * NM_LOCK: Create an unmonitored lock
388 nlm4svc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
389 struct nlm_res *resp)
391 dprintk("lockd: NM_LOCK called\n");
393 argp->monitor = 0; /* just clean the monitor flag */
394 return nlm4svc_proc_lock(rqstp, argp, resp);
398 * FREE_ALL: Release all locks and shares held by client
401 nlm4svc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
404 struct nlm_host *host;
407 if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
410 nlmsvc_free_host_resources(host);
411 nlm_release_host(host);
416 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
419 nlm4svc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
422 struct sockaddr_in saddr = rqstp->rq_addr;
423 int vers = argp->vers;
424 int prot = argp->proto >> 1;
426 struct nlm_host *host;
428 dprintk("lockd: SM_NOTIFY called\n");
429 if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
430 || ntohs(saddr.sin_port) >= 1024) {
432 "lockd: rejected NSM callback from %08x:%d\n",
433 ntohl(rqstp->rq_addr.sin_addr.s_addr),
434 ntohs(rqstp->rq_addr.sin_port));
435 return rpc_system_err;
438 /* Obtain the host pointer for this NFS server and try to
439 * reclaim all locks we hold on this server.
441 saddr.sin_addr.s_addr = argp->addr;
443 if ((argp->proto & 1)==0) {
444 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
445 nlmclnt_recovery(host, argp->state);
446 nlm_release_host(host);
449 /* If we run on an NFS server, delete all locks held by the client */
451 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
452 nlmsvc_free_host_resources(host);
453 nlm_release_host(host);
460 * client sent a GRANTED_RES, let's remove the associated block
463 nlm4svc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
469 dprintk("lockd: GRANTED_RES called\n");
471 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
478 * This is the generic lockd callback for async RPC calls
481 nlm4svc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
483 struct nlm_host *host;
484 struct nlm_rqst *call;
486 if (!(call = nlmclnt_alloc_call()))
487 return rpc_system_err;
489 host = nlmclnt_lookup_host(&rqstp->rq_addr,
490 rqstp->rq_prot, rqstp->rq_vers);
493 return rpc_system_err;
496 call->a_flags = RPC_TASK_ASYNC;
498 memcpy(&call->a_args, resp, sizeof(*resp));
500 if (nlmsvc_async_call(call, proc, nlm4svc_callback_exit) < 0)
506 nlm_release_host(host);
507 return rpc_system_err;
511 nlm4svc_callback_exit(struct rpc_task *task)
513 struct nlm_rqst *call = (struct nlm_rqst *) task->tk_calldata;
515 if (task->tk_status < 0) {
516 dprintk("lockd: %4d callback failed (errno = %d)\n",
517 task->tk_pid, -task->tk_status);
519 nlm_release_host(call->a_host);
524 * NLM Server procedures.
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
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
541 struct nlm_void { int dummy; };
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, \
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) */
556 struct 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),
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),