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
)))
135 /* If supplied state doesn't match current state, we assume it's
136 * an old request that time-warped somehow. Any error return would
137 * do in this case because it's irrelevant anyway.
139 * NB: We don't retrieve the remote host's state yet.
141 if (host
->h_nsmstate
&& host
->h_nsmstate
!= argp
->state
) {
142 resp
->status
= nlm_lck_denied_nolocks
;
146 /* Now try to lock the file */
147 resp
->status
= nlmsvc_lock(rqstp
, file
, &argp
->lock
,
148 argp
->block
, &argp
->cookie
);
150 dprintk("lockd: LOCK status %d\n", ntohl(resp
->status
));
151 nlm_release_host(host
);
152 nlm_release_file(file
);
157 nlm4svc_proc_cancel(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
158 struct nlm_res
*resp
)
160 struct nlm_host
*host
;
161 struct nlm_file
*file
;
163 dprintk("lockd: CANCEL called\n");
165 resp
->cookie
= argp
->cookie
;
167 /* Don't accept requests during grace period */
168 if (nlmsvc_grace_period
) {
169 resp
->status
= nlm_lck_denied_grace_period
;
173 /* Obtain client and file */
174 if ((resp
->status
= nlm4svc_retrieve_args(rqstp
, argp
, &host
, &file
)))
177 /* Try to cancel request. */
178 resp
->status
= nlmsvc_cancel_blocked(file
, &argp
->lock
);
180 dprintk("lockd: CANCEL status %d\n", ntohl(resp
->status
));
181 nlm_release_host(host
);
182 nlm_release_file(file
);
187 * UNLOCK: release a lock
190 nlm4svc_proc_unlock(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
191 struct nlm_res
*resp
)
193 struct nlm_host
*host
;
194 struct nlm_file
*file
;
196 dprintk("lockd: UNLOCK called\n");
198 resp
->cookie
= argp
->cookie
;
200 /* Don't accept new lock requests during grace period */
201 if (nlmsvc_grace_period
) {
202 resp
->status
= nlm_lck_denied_grace_period
;
206 /* Obtain client and file */
207 if ((resp
->status
= nlm4svc_retrieve_args(rqstp
, argp
, &host
, &file
)))
210 /* Now try to remove the lock */
211 resp
->status
= nlmsvc_unlock(file
, &argp
->lock
);
213 dprintk("lockd: UNLOCK status %d\n", ntohl(resp
->status
));
214 nlm_release_host(host
);
215 nlm_release_file(file
);
220 * GRANTED: A server calls us to tell that a process' lock request
224 nlm4svc_proc_granted(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
225 struct nlm_res
*resp
)
227 resp
->cookie
= argp
->cookie
;
229 dprintk("lockd: GRANTED called\n");
230 resp
->status
= nlmclnt_grant(&argp
->lock
);
231 dprintk("lockd: GRANTED status %d\n", ntohl(resp
->status
));
236 * `Async' versions of the above service routines. They aren't really,
237 * because we send the callback before the reply proper. I hope this
238 * doesn't break any clients.
241 nlm4svc_proc_test_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
247 dprintk("lockd: TEST_MSG called\n");
248 memset(&res
, 0, sizeof(res
));
250 if ((stat
= nlm4svc_proc_test(rqstp
, argp
, &res
)) == 0)
251 stat
= nlm4svc_callback(rqstp
, NLMPROC_TEST_RES
, &res
);
256 nlm4svc_proc_lock_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
262 dprintk("lockd: LOCK_MSG called\n");
263 memset(&res
, 0, sizeof(res
));
265 if ((stat
= nlm4svc_proc_lock(rqstp
, argp
, &res
)) == 0)
266 stat
= nlm4svc_callback(rqstp
, NLMPROC_LOCK_RES
, &res
);
271 nlm4svc_proc_cancel_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
277 dprintk("lockd: CANCEL_MSG called\n");
278 memset(&res
, 0, sizeof(res
));
280 if ((stat
= nlm4svc_proc_cancel(rqstp
, argp
, &res
)) == 0)
281 stat
= nlm4svc_callback(rqstp
, NLMPROC_CANCEL_RES
, &res
);
286 nlm4svc_proc_unlock_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
292 dprintk("lockd: UNLOCK_MSG called\n");
293 memset(&res
, 0, sizeof(res
));
295 if ((stat
= nlm4svc_proc_unlock(rqstp
, argp
, &res
)) == 0)
296 stat
= nlm4svc_callback(rqstp
, NLMPROC_UNLOCK_RES
, &res
);
301 nlm4svc_proc_granted_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
307 dprintk("lockd: GRANTED_MSG called\n");
308 memset(&res
, 0, sizeof(res
));
310 if ((stat
= nlm4svc_proc_granted(rqstp
, argp
, &res
)) == 0)
311 stat
= nlm4svc_callback(rqstp
, NLMPROC_GRANTED_RES
, &res
);
316 * SHARE: create a DOS share or alter existing share.
319 nlm4svc_proc_share(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
320 struct nlm_res
*resp
)
322 struct nlm_host
*host
;
323 struct nlm_file
*file
;
325 dprintk("lockd: SHARE called\n");
327 resp
->cookie
= argp
->cookie
;
329 /* Don't accept new lock requests during grace period */
330 if (nlmsvc_grace_period
&& !argp
->reclaim
) {
331 resp
->status
= nlm_lck_denied_grace_period
;
335 /* Obtain client and file */
336 if ((resp
->status
= nlm4svc_retrieve_args(rqstp
, argp
, &host
, &file
)))
339 /* Now try to create the share */
340 resp
->status
= nlmsvc_share_file(host
, file
, argp
);
342 dprintk("lockd: SHARE status %d\n", ntohl(resp
->status
));
343 nlm_release_host(host
);
344 nlm_release_file(file
);
349 * UNSHARE: Release a DOS share.
352 nlm4svc_proc_unshare(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
353 struct nlm_res
*resp
)
355 struct nlm_host
*host
;
356 struct nlm_file
*file
;
358 dprintk("lockd: UNSHARE called\n");
360 resp
->cookie
= argp
->cookie
;
362 /* Don't accept requests during grace period */
363 if (nlmsvc_grace_period
) {
364 resp
->status
= nlm_lck_denied_grace_period
;
368 /* Obtain client and file */
369 if ((resp
->status
= nlm4svc_retrieve_args(rqstp
, argp
, &host
, &file
)))
372 /* Now try to lock the file */
373 resp
->status
= nlmsvc_unshare_file(host
, file
, argp
);
375 dprintk("lockd: UNSHARE status %d\n", ntohl(resp
->status
));
376 nlm_release_host(host
);
377 nlm_release_file(file
);
382 * NM_LOCK: Create an unmonitored lock
385 nlm4svc_proc_nm_lock(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
386 struct nlm_res
*resp
)
388 dprintk("lockd: NM_LOCK called\n");
390 argp
->monitor
= 0; /* just clean the monitor flag */
391 return nlm4svc_proc_lock(rqstp
, argp
, resp
);
395 * FREE_ALL: Release all locks and shares held by client
398 nlm4svc_proc_free_all(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
401 struct nlm_host
*host
;
404 if (nlm4svc_retrieve_args(rqstp
, argp
, &host
, NULL
))
407 nlmsvc_free_host_resources(host
);
408 nlm_release_host(host
);
413 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
416 nlm4svc_proc_sm_notify(struct svc_rqst
*rqstp
, struct nlm_reboot
*argp
,
419 struct sockaddr_in saddr
= rqstp
->rq_addr
;
420 int vers
= argp
->vers
;
421 int prot
= argp
->proto
>> 1;
423 struct nlm_host
*host
;
425 dprintk("lockd: SM_NOTIFY called\n");
426 if (saddr
.sin_addr
.s_addr
!= htonl(INADDR_LOOPBACK
)
427 || ntohs(saddr
.sin_port
) >= 1024) {
429 "lockd: rejected NSM callback from %08x:%d\n",
430 ntohl(rqstp
->rq_addr
.sin_addr
.s_addr
),
431 ntohs(rqstp
->rq_addr
.sin_port
));
432 return rpc_system_err
;
435 /* Obtain the host pointer for this NFS server and try to
436 * reclaim all locks we hold on this server.
438 saddr
.sin_addr
.s_addr
= argp
->addr
;
440 if ((argp
->proto
& 1)==0) {
441 if ((host
= nlmclnt_lookup_host(&saddr
, prot
, vers
)) != NULL
) {
442 nlmclnt_recovery(host
, argp
->state
);
443 nlm_release_host(host
);
446 /* If we run on an NFS server, delete all locks held by the client */
448 if ((host
= nlm_lookup_host(1, &saddr
, prot
, vers
)) != NULL
) {
449 nlmsvc_free_host_resources(host
);
450 nlm_release_host(host
);
457 * client sent a GRANTED_RES, let's remove the associated block
460 nlm4svc_proc_granted_res(struct svc_rqst
*rqstp
, struct nlm_res
*argp
,
466 dprintk("lockd: GRANTED_RES called\n");
468 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 if (!(call
= nlmclnt_alloc_call()))
484 return rpc_system_err
;
486 host
= nlmclnt_lookup_host(&rqstp
->rq_addr
,
487 rqstp
->rq_prot
, rqstp
->rq_vers
);
490 return rpc_system_err
;
493 call
->a_flags
= RPC_TASK_ASYNC
;
495 memcpy(&call
->a_args
, resp
, sizeof(*resp
));
497 if (nlmsvc_async_call(call
, proc
, nlm4svc_callback_exit
) < 0)
503 nlm_release_host(host
);
504 return rpc_system_err
;
508 nlm4svc_callback_exit(struct rpc_task
*task
)
510 struct nlm_rqst
*call
= (struct nlm_rqst
*) task
->tk_calldata
;
512 if (task
->tk_status
< 0) {
513 dprintk("lockd: %4d callback failed (errno = %d)\n",
514 task
->tk_pid
, -task
->tk_status
);
516 nlm_release_host(call
->a_host
);
521 * NLM Server procedures.
524 #define nlm4svc_encode_norep nlm4svc_encode_void
525 #define nlm4svc_decode_norep nlm4svc_decode_void
526 #define nlm4svc_decode_testres nlm4svc_decode_void
527 #define nlm4svc_decode_lockres nlm4svc_decode_void
528 #define nlm4svc_decode_unlockres nlm4svc_decode_void
529 #define nlm4svc_decode_cancelres nlm4svc_decode_void
530 #define nlm4svc_decode_grantedres nlm4svc_decode_void
532 #define nlm4svc_proc_none nlm4svc_proc_null
533 #define nlm4svc_proc_test_res nlm4svc_proc_null
534 #define nlm4svc_proc_lock_res nlm4svc_proc_null
535 #define nlm4svc_proc_cancel_res nlm4svc_proc_null
536 #define nlm4svc_proc_unlock_res nlm4svc_proc_null
538 struct nlm_void
{ int dummy
; };
540 #define PROC(name, xargt, xrest, argt, rest, respsize) \
541 { .pc_func = (svc_procfunc) nlm4svc_proc_##name, \
542 .pc_decode = (kxdrproc_t) nlm4svc_decode_##xargt, \
543 .pc_encode = (kxdrproc_t) nlm4svc_encode_##xrest, \
544 .pc_release = NULL, \
545 .pc_argsize = sizeof(struct nlm_##argt), \
546 .pc_ressize = sizeof(struct nlm_##rest), \
547 .pc_xdrressize = respsize, \
549 #define Ck (1+8) /* cookie */
550 #define No (1+1024/4) /* netobj */
551 #define St 1 /* status */
552 #define Rg 4 /* range (offset + length) */
553 struct svc_procedure nlmsvc_procedures4
[] = {
554 PROC(null
, void, void, void, void, 1),
555 PROC(test
, testargs
, testres
, args
, res
, Ck
+St
+2+No
+Rg
),
556 PROC(lock
, lockargs
, res
, args
, res
, Ck
+St
),
557 PROC(cancel
, cancargs
, res
, args
, res
, Ck
+St
),
558 PROC(unlock
, unlockargs
, res
, args
, res
, Ck
+St
),
559 PROC(granted
, testargs
, res
, args
, res
, Ck
+St
),
560 PROC(test_msg
, testargs
, norep
, args
, void, 1),
561 PROC(lock_msg
, lockargs
, norep
, args
, void, 1),
562 PROC(cancel_msg
, cancargs
, norep
, args
, void, 1),
563 PROC(unlock_msg
, unlockargs
, norep
, args
, void, 1),
564 PROC(granted_msg
, testargs
, norep
, args
, void, 1),
565 PROC(test_res
, testres
, norep
, res
, void, 1),
566 PROC(lock_res
, lockres
, norep
, res
, void, 1),
567 PROC(cancel_res
, cancelres
, norep
, res
, void, 1),
568 PROC(unlock_res
, unlockres
, norep
, res
, void, 1),
569 PROC(granted_res
, res
, norep
, res
, void, 1),
571 PROC(sm_notify
, reboot
, void, reboot
, void, 1),
572 PROC(none
, void, void, void, void, 0),
573 PROC(none
, void, void, void, void, 0),
574 PROC(none
, void, void, void, void, 0),
575 PROC(share
, shareargs
, shareres
, args
, res
, Ck
+St
+1),
576 PROC(unshare
, shareargs
, shareres
, args
, res
, Ck
+St
+1),
577 PROC(nm_lock
, lockargs
, res
, args
, res
, Ck
+St
),
578 PROC(free_all
, notify
, void, args
, void, 1),