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 #ifdef CONFIG_LOCKD_V4
27 cast_to_nlm(u32 status
, u32 vers
)
29 /* Note: status is assumed to be in network byte order !!! */
34 case nlm_lck_denied_nolocks
:
36 case nlm_lck_denied_grace_period
:
39 status
= nlm_lck_denied
;
42 status
= nlm_lck_denied_nolocks
;
48 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
50 #define cast_status(status) (status)
54 * Obtain client and file from arguments
57 nlmsvc_retrieve_args(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
58 struct nlm_host
**hostp
, struct nlm_file
**filp
)
60 struct nlm_host
*host
= NULL
;
61 struct nlm_file
*file
= NULL
;
62 struct nlm_lock
*lock
= &argp
->lock
;
65 /* nfsd callbacks must have been installed for this procedure */
67 return nlm_lck_denied_nolocks
;
69 /* Obtain host handle */
70 if (!(host
= nlmsvc_lookup_host(rqstp
))
71 || (argp
->monitor
&& !host
->h_monitored
&& nsm_monitor(host
) < 0))
75 /* Obtain file pointer. Not used by FREE_ALL call. */
77 if ((error
= nlm_lookup_file(rqstp
, &file
, &lock
->fh
)) != 0)
81 /* Set up the missing parts of the file_lock structure */
82 lock
->fl
.fl_file
= file
->f_file
;
83 lock
->fl
.fl_owner
= (fl_owner_t
) host
;
84 lock
->fl
.fl_lmops
= &nlmsvc_lock_operations
;
91 nlm_release_host(host
);
92 return nlm_lck_denied_nolocks
;
96 * NULL: Test for presence of service
99 nlmsvc_proc_null(struct svc_rqst
*rqstp
, void *argp
, void *resp
)
101 dprintk("lockd: NULL called\n");
106 * TEST: Check for conflicting lock
109 nlmsvc_proc_test(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
110 struct nlm_res
*resp
)
112 struct nlm_host
*host
;
113 struct nlm_file
*file
;
115 dprintk("lockd: TEST called\n");
116 resp
->cookie
= argp
->cookie
;
118 /* Don't accept test requests during grace period */
119 if (nlmsvc_grace_period
) {
120 resp
->status
= nlm_lck_denied_grace_period
;
124 /* Obtain client and file */
125 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
128 /* Now check for conflicting locks */
129 resp
->status
= cast_status(nlmsvc_testlock(file
, &argp
->lock
, &resp
->lock
));
131 dprintk("lockd: TEST status %d vers %d\n",
132 ntohl(resp
->status
), rqstp
->rq_vers
);
133 nlm_release_host(host
);
134 nlm_release_file(file
);
139 nlmsvc_proc_lock(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
140 struct nlm_res
*resp
)
142 struct nlm_host
*host
;
143 struct nlm_file
*file
;
145 dprintk("lockd: LOCK called\n");
147 resp
->cookie
= argp
->cookie
;
149 /* Don't accept new lock requests during grace period */
150 if (nlmsvc_grace_period
&& !argp
->reclaim
) {
151 resp
->status
= nlm_lck_denied_grace_period
;
155 /* Obtain client and file */
156 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
160 /* If supplied state doesn't match current state, we assume it's
161 * an old request that time-warped somehow. Any error return would
162 * do in this case because it's irrelevant anyway.
164 * NB: We don't retrieve the remote host's state yet.
166 if (host
->h_nsmstate
&& host
->h_nsmstate
!= argp
->state
) {
167 resp
->status
= nlm_lck_denied_nolocks
;
171 /* Now try to lock the file */
172 resp
->status
= cast_status(nlmsvc_lock(rqstp
, file
, &argp
->lock
,
173 argp
->block
, &argp
->cookie
));
175 dprintk("lockd: LOCK status %d\n", ntohl(resp
->status
));
176 nlm_release_host(host
);
177 nlm_release_file(file
);
182 nlmsvc_proc_cancel(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
183 struct nlm_res
*resp
)
185 struct nlm_host
*host
;
186 struct nlm_file
*file
;
188 dprintk("lockd: CANCEL called\n");
190 resp
->cookie
= argp
->cookie
;
192 /* Don't accept requests during grace period */
193 if (nlmsvc_grace_period
) {
194 resp
->status
= nlm_lck_denied_grace_period
;
198 /* Obtain client and file */
199 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
202 /* Try to cancel request. */
203 resp
->status
= cast_status(nlmsvc_cancel_blocked(file
, &argp
->lock
));
205 dprintk("lockd: CANCEL status %d\n", ntohl(resp
->status
));
206 nlm_release_host(host
);
207 nlm_release_file(file
);
212 * UNLOCK: release a lock
215 nlmsvc_proc_unlock(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
216 struct nlm_res
*resp
)
218 struct nlm_host
*host
;
219 struct nlm_file
*file
;
221 dprintk("lockd: UNLOCK called\n");
223 resp
->cookie
= argp
->cookie
;
225 /* Don't accept new lock requests during grace period */
226 if (nlmsvc_grace_period
) {
227 resp
->status
= nlm_lck_denied_grace_period
;
231 /* Obtain client and file */
232 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
235 /* Now try to remove the lock */
236 resp
->status
= cast_status(nlmsvc_unlock(file
, &argp
->lock
));
238 dprintk("lockd: UNLOCK status %d\n", ntohl(resp
->status
));
239 nlm_release_host(host
);
240 nlm_release_file(file
);
245 * GRANTED: A server calls us to tell that a process' lock request
249 nlmsvc_proc_granted(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
250 struct nlm_res
*resp
)
252 resp
->cookie
= argp
->cookie
;
254 dprintk("lockd: GRANTED called\n");
255 resp
->status
= nlmclnt_grant(&rqstp
->rq_addr
, &argp
->lock
);
256 dprintk("lockd: GRANTED status %d\n", ntohl(resp
->status
));
261 * This is the generic lockd callback for async RPC calls
263 static void nlmsvc_callback_exit(struct rpc_task
*task
, void *data
)
265 dprintk("lockd: %4d callback returned %d\n", task
->tk_pid
,
269 static void nlmsvc_callback_release(void *data
)
271 nlm_release_call(data
);
274 static const struct rpc_call_ops nlmsvc_callback_ops
= {
275 .rpc_call_done
= nlmsvc_callback_exit
,
276 .rpc_release
= nlmsvc_callback_release
,
280 * `Async' versions of the above service routines. They aren't really,
281 * because we send the callback before the reply proper. I hope this
282 * doesn't break any clients.
284 static int nlmsvc_callback(struct svc_rqst
*rqstp
, u32 proc
, struct nlm_args
*argp
,
285 int (*func
)(struct svc_rqst
*, struct nlm_args
*, struct nlm_res
*))
287 struct nlm_host
*host
;
288 struct nlm_rqst
*call
;
291 host
= nlmsvc_lookup_host(rqstp
);
293 return rpc_system_err
;
295 call
= nlm_alloc_call(host
);
297 return rpc_system_err
;
299 stat
= func(rqstp
, argp
, &call
->a_res
);
301 nlm_release_call(call
);
305 call
->a_flags
= RPC_TASK_ASYNC
;
306 if (nlm_async_reply(call
, proc
, &nlmsvc_callback_ops
) < 0)
307 return rpc_system_err
;
311 static int nlmsvc_proc_test_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
314 dprintk("lockd: TEST_MSG called\n");
315 return nlmsvc_callback(rqstp
, NLMPROC_TEST_RES
, argp
, nlmsvc_proc_test
);
318 static int nlmsvc_proc_lock_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
321 dprintk("lockd: LOCK_MSG called\n");
322 return nlmsvc_callback(rqstp
, NLMPROC_LOCK_RES
, argp
, nlmsvc_proc_lock
);
325 static int nlmsvc_proc_cancel_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
328 dprintk("lockd: CANCEL_MSG called\n");
329 return nlmsvc_callback(rqstp
, NLMPROC_CANCEL_RES
, argp
, nlmsvc_proc_cancel
);
333 nlmsvc_proc_unlock_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
336 dprintk("lockd: UNLOCK_MSG called\n");
337 return nlmsvc_callback(rqstp
, NLMPROC_UNLOCK_RES
, argp
, nlmsvc_proc_unlock
);
341 nlmsvc_proc_granted_msg(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
344 dprintk("lockd: GRANTED_MSG called\n");
345 return nlmsvc_callback(rqstp
, NLMPROC_GRANTED_RES
, argp
, nlmsvc_proc_granted
);
349 * SHARE: create a DOS share or alter existing share.
352 nlmsvc_proc_share(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: SHARE called\n");
360 resp
->cookie
= argp
->cookie
;
362 /* Don't accept new lock requests during grace period */
363 if (nlmsvc_grace_period
&& !argp
->reclaim
) {
364 resp
->status
= nlm_lck_denied_grace_period
;
368 /* Obtain client and file */
369 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
372 /* Now try to create the share */
373 resp
->status
= cast_status(nlmsvc_share_file(host
, file
, argp
));
375 dprintk("lockd: SHARE status %d\n", ntohl(resp
->status
));
376 nlm_release_host(host
);
377 nlm_release_file(file
);
382 * UNSHARE: Release a DOS share.
385 nlmsvc_proc_unshare(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
386 struct nlm_res
*resp
)
388 struct nlm_host
*host
;
389 struct nlm_file
*file
;
391 dprintk("lockd: UNSHARE called\n");
393 resp
->cookie
= argp
->cookie
;
395 /* Don't accept requests during grace period */
396 if (nlmsvc_grace_period
) {
397 resp
->status
= nlm_lck_denied_grace_period
;
401 /* Obtain client and file */
402 if ((resp
->status
= nlmsvc_retrieve_args(rqstp
, argp
, &host
, &file
)))
405 /* Now try to unshare the file */
406 resp
->status
= cast_status(nlmsvc_unshare_file(host
, file
, argp
));
408 dprintk("lockd: UNSHARE status %d\n", ntohl(resp
->status
));
409 nlm_release_host(host
);
410 nlm_release_file(file
);
415 * NM_LOCK: Create an unmonitored lock
418 nlmsvc_proc_nm_lock(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
419 struct nlm_res
*resp
)
421 dprintk("lockd: NM_LOCK called\n");
423 argp
->monitor
= 0; /* just clean the monitor flag */
424 return nlmsvc_proc_lock(rqstp
, argp
, resp
);
428 * FREE_ALL: Release all locks and shares held by client
431 nlmsvc_proc_free_all(struct svc_rqst
*rqstp
, struct nlm_args
*argp
,
434 struct nlm_host
*host
;
437 if (nlmsvc_retrieve_args(rqstp
, argp
, &host
, NULL
))
440 nlmsvc_free_host_resources(host
);
441 nlm_release_host(host
);
446 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
449 nlmsvc_proc_sm_notify(struct svc_rqst
*rqstp
, struct nlm_reboot
*argp
,
452 struct sockaddr_in saddr
= rqstp
->rq_addr
;
453 int vers
= argp
->vers
;
454 int prot
= argp
->proto
>> 1;
455 struct nlm_host
*host
;
457 dprintk("lockd: SM_NOTIFY called\n");
458 if (saddr
.sin_addr
.s_addr
!= htonl(INADDR_LOOPBACK
)
459 || ntohs(saddr
.sin_port
) >= 1024) {
461 "lockd: rejected NSM callback from %08x:%d\n",
462 ntohl(rqstp
->rq_addr
.sin_addr
.s_addr
),
463 ntohs(rqstp
->rq_addr
.sin_port
));
464 return rpc_system_err
;
467 /* Obtain the host pointer for this NFS server and try to
468 * reclaim all locks we hold on this server.
470 saddr
.sin_addr
.s_addr
= argp
->addr
;
471 if ((argp
->proto
& 1)==0) {
472 if ((host
= nlmclnt_lookup_host(&saddr
, prot
, vers
)) != NULL
) {
473 nlmclnt_recovery(host
, argp
->state
);
474 nlm_release_host(host
);
477 /* If we run on an NFS server, delete all locks held by the client */
478 if ((host
= nlm_lookup_host(1, &saddr
, prot
, vers
)) != NULL
) {
479 nlmsvc_free_host_resources(host
);
480 nlm_release_host(host
);
488 * client sent a GRANTED_RES, let's remove the associated block
491 nlmsvc_proc_granted_res(struct svc_rqst
*rqstp
, struct nlm_res
*argp
,
497 dprintk("lockd: GRANTED_RES called\n");
499 nlmsvc_grant_reply(rqstp
, &argp
->cookie
, argp
->status
);
504 * NLM Server procedures.
507 #define nlmsvc_encode_norep nlmsvc_encode_void
508 #define nlmsvc_decode_norep nlmsvc_decode_void
509 #define nlmsvc_decode_testres nlmsvc_decode_void
510 #define nlmsvc_decode_lockres nlmsvc_decode_void
511 #define nlmsvc_decode_unlockres nlmsvc_decode_void
512 #define nlmsvc_decode_cancelres nlmsvc_decode_void
513 #define nlmsvc_decode_grantedres nlmsvc_decode_void
515 #define nlmsvc_proc_none nlmsvc_proc_null
516 #define nlmsvc_proc_test_res nlmsvc_proc_null
517 #define nlmsvc_proc_lock_res nlmsvc_proc_null
518 #define nlmsvc_proc_cancel_res nlmsvc_proc_null
519 #define nlmsvc_proc_unlock_res nlmsvc_proc_null
521 struct nlm_void
{ int dummy
; };
523 #define PROC(name, xargt, xrest, argt, rest, respsize) \
524 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
525 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
526 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
527 .pc_release = NULL, \
528 .pc_argsize = sizeof(struct nlm_##argt), \
529 .pc_ressize = sizeof(struct nlm_##rest), \
530 .pc_xdrressize = respsize, \
533 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
534 #define St 1 /* status */
535 #define No (1+1024/4) /* Net Obj */
536 #define Rg 2 /* range - offset + size */
538 struct svc_procedure nlmsvc_procedures
[] = {
539 PROC(null
, void, void, void, void, 1),
540 PROC(test
, testargs
, testres
, args
, res
, Ck
+St
+2+No
+Rg
),
541 PROC(lock
, lockargs
, res
, args
, res
, Ck
+St
),
542 PROC(cancel
, cancargs
, res
, args
, res
, Ck
+St
),
543 PROC(unlock
, unlockargs
, res
, args
, res
, Ck
+St
),
544 PROC(granted
, testargs
, res
, args
, res
, Ck
+St
),
545 PROC(test_msg
, testargs
, norep
, args
, void, 1),
546 PROC(lock_msg
, lockargs
, norep
, args
, void, 1),
547 PROC(cancel_msg
, cancargs
, norep
, args
, void, 1),
548 PROC(unlock_msg
, unlockargs
, norep
, args
, void, 1),
549 PROC(granted_msg
, testargs
, norep
, args
, void, 1),
550 PROC(test_res
, testres
, norep
, res
, void, 1),
551 PROC(lock_res
, lockres
, norep
, res
, void, 1),
552 PROC(cancel_res
, cancelres
, norep
, res
, void, 1),
553 PROC(unlock_res
, unlockres
, norep
, res
, void, 1),
554 PROC(granted_res
, res
, norep
, res
, void, 1),
556 PROC(sm_notify
, reboot
, void, reboot
, void, 1),
557 PROC(none
, void, void, void, void, 1),
558 PROC(none
, void, void, void, void, 1),
559 PROC(none
, void, void, void, void, 1),
560 PROC(share
, shareargs
, shareres
, args
, res
, Ck
+St
+1),
561 PROC(unshare
, shareargs
, shareres
, args
, res
, Ck
+St
+1),
562 PROC(nm_lock
, lockargs
, res
, args
, res
, Ck
+St
),
563 PROC(free_all
, notify
, void, args
, void, 0),