[PATCH] CodingStyle: add typedefs chapter
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / lockd / svcproc.c
blobd210cf304e922f3f2eca906f0ec025cf2832f865
1 /*
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>
8 */
10 #include <linux/config.h>
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/slab.h>
14 #include <linux/in.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
26 static u32
27 cast_to_nlm(u32 status, u32 vers)
29 /* Note: status is assumed to be in network byte order !!! */
30 if (vers != 4){
31 switch (status) {
32 case nlm_granted:
33 case nlm_lck_denied:
34 case nlm_lck_denied_nolocks:
35 case nlm_lck_blocked:
36 case nlm_lck_denied_grace_period:
37 break;
38 case nlm4_deadlock:
39 status = nlm_lck_denied;
40 break;
41 default:
42 status = nlm_lck_denied_nolocks;
46 return (status);
48 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
49 #else
50 #define cast_status(status) (status)
51 #endif
54 * Obtain client and file from arguments
56 static u32
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;
63 u32 error;
65 /* nfsd callbacks must have been installed for this procedure */
66 if (!nlmsvc_ops)
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))
72 goto no_locks;
73 *hostp = host;
75 /* Obtain file pointer. Not used by FREE_ALL call. */
76 if (filp != NULL) {
77 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
78 goto no_locks;
79 *filp = file;
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;
87 return 0;
89 no_locks:
90 if (host)
91 nlm_release_host(host);
92 return nlm_lck_denied_nolocks;
96 * NULL: Test for presence of service
98 static int
99 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
101 dprintk("lockd: NULL called\n");
102 return rpc_success;
106 * TEST: Check for conflicting lock
108 static int
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;
121 return rpc_success;
124 /* Obtain client and file */
125 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
126 return rpc_success;
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);
135 return rpc_success;
138 static int
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;
152 return rpc_success;
155 /* Obtain client and file */
156 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
157 return rpc_success;
159 #if 0
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;
168 } else
169 #endif
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);
178 return rpc_success;
181 static int
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;
195 return rpc_success;
198 /* Obtain client and file */
199 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
200 return rpc_success;
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);
208 return rpc_success;
212 * UNLOCK: release a lock
214 static int
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;
228 return rpc_success;
231 /* Obtain client and file */
232 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
233 return rpc_success;
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);
241 return rpc_success;
245 * GRANTED: A server calls us to tell that a process' lock request
246 * was granted
248 static int
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));
257 return rpc_success;
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,
266 -task->tk_status);
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;
289 int stat;
291 host = nlmsvc_lookup_host(rqstp);
292 if (host == NULL)
293 return rpc_system_err;
295 call = nlm_alloc_call(host);
296 if (call == NULL)
297 return rpc_system_err;
299 stat = func(rqstp, argp, &call->a_res);
300 if (stat != 0) {
301 nlm_release_call(call);
302 return stat;
305 call->a_flags = RPC_TASK_ASYNC;
306 if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
307 return rpc_system_err;
308 return rpc_success;
311 static int nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
312 void *resp)
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,
319 void *resp)
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,
326 void *resp)
328 dprintk("lockd: CANCEL_MSG called\n");
329 return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlmsvc_proc_cancel);
332 static int
333 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
334 void *resp)
336 dprintk("lockd: UNLOCK_MSG called\n");
337 return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlmsvc_proc_unlock);
340 static int
341 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
342 void *resp)
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.
351 static int
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;
365 return rpc_success;
368 /* Obtain client and file */
369 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
370 return rpc_success;
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);
378 return rpc_success;
382 * UNSHARE: Release a DOS share.
384 static int
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;
398 return rpc_success;
401 /* Obtain client and file */
402 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
403 return rpc_success;
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);
411 return rpc_success;
415 * NM_LOCK: Create an unmonitored lock
417 static int
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
430 static int
431 nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
432 void *resp)
434 struct nlm_host *host;
436 /* Obtain client */
437 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
438 return rpc_success;
440 nlmsvc_free_host_resources(host);
441 nlm_release_host(host);
442 return rpc_success;
446 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
448 static int
449 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
450 void *resp)
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) {
460 printk(KERN_WARNING
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);
476 } else {
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);
484 return rpc_success;
488 * client sent a GRANTED_RES, let's remove the associated block
490 static int
491 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
492 void *resp)
494 if (!nlmsvc_ops)
495 return rpc_success;
497 dprintk("lockd: GRANTED_RES called\n");
499 nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
500 return rpc_success;
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),
555 /* statd callback */
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),