x86, apic: Fix spurious error interrupts triggering on all non-boot APs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / lockd / svcproc.c
blob56c9519d900a32ea203112f24b4866227b1bdd33
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/types.h>
11 #include <linux/time.h>
12 #include <linux/slab.h>
13 #include <linux/smp_lock.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/lockd/share.h>
17 #define NLMDBG_FACILITY NLMDBG_CLIENT
19 #ifdef CONFIG_LOCKD_V4
20 static __be32
21 cast_to_nlm(__be32 status, u32 vers)
23 /* Note: status is assumed to be in network byte order !!! */
24 if (vers != 4){
25 switch (status) {
26 case nlm_granted:
27 case nlm_lck_denied:
28 case nlm_lck_denied_nolocks:
29 case nlm_lck_blocked:
30 case nlm_lck_denied_grace_period:
31 case nlm_drop_reply:
32 break;
33 case nlm4_deadlock:
34 status = nlm_lck_denied;
35 break;
36 default:
37 status = nlm_lck_denied_nolocks;
41 return (status);
43 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
44 #else
45 #define cast_status(status) (status)
46 #endif
49 * Obtain client and file from arguments
51 static __be32
52 nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
53 struct nlm_host **hostp, struct nlm_file **filp)
55 struct nlm_host *host = NULL;
56 struct nlm_file *file = NULL;
57 struct nlm_lock *lock = &argp->lock;
58 __be32 error = 0;
60 /* nfsd callbacks must have been installed for this procedure */
61 if (!nlmsvc_ops)
62 return nlm_lck_denied_nolocks;
64 /* Obtain host handle */
65 if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
66 || (argp->monitor && nsm_monitor(host) < 0))
67 goto no_locks;
68 *hostp = host;
70 /* Obtain file pointer. Not used by FREE_ALL call. */
71 if (filp != NULL) {
72 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
73 goto no_locks;
74 *filp = file;
76 /* Set up the missing parts of the file_lock structure */
77 lock->fl.fl_file = file->f_file;
78 lock->fl.fl_owner = (fl_owner_t) host;
79 lock->fl.fl_lmops = &nlmsvc_lock_operations;
82 return 0;
84 no_locks:
85 nlm_release_host(host);
86 if (error)
87 return error;
88 return nlm_lck_denied_nolocks;
92 * NULL: Test for presence of service
94 static __be32
95 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
97 dprintk("lockd: NULL called\n");
98 return rpc_success;
102 * TEST: Check for conflicting lock
104 static __be32
105 nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
106 struct nlm_res *resp)
108 struct nlm_host *host;
109 struct nlm_file *file;
110 __be32 rc = rpc_success;
112 dprintk("lockd: TEST called\n");
113 resp->cookie = argp->cookie;
115 /* Obtain client and file */
116 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
117 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
119 /* Now check for conflicting locks */
120 resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie));
121 if (resp->status == nlm_drop_reply)
122 rc = rpc_drop_reply;
123 else
124 dprintk("lockd: TEST status %d vers %d\n",
125 ntohl(resp->status), rqstp->rq_vers);
127 nlm_release_host(host);
128 nlm_release_file(file);
129 return rc;
132 static __be32
133 nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
134 struct nlm_res *resp)
136 struct nlm_host *host;
137 struct nlm_file *file;
138 __be32 rc = rpc_success;
140 dprintk("lockd: LOCK called\n");
142 resp->cookie = argp->cookie;
144 /* Obtain client and file */
145 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
146 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
148 #if 0
149 /* If supplied state doesn't match current state, we assume it's
150 * an old request that time-warped somehow. Any error return would
151 * do in this case because it's irrelevant anyway.
153 * NB: We don't retrieve the remote host's state yet.
155 if (host->h_nsmstate && host->h_nsmstate != argp->state) {
156 resp->status = nlm_lck_denied_nolocks;
157 } else
158 #endif
160 /* Now try to lock the file */
161 resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock,
162 argp->block, &argp->cookie,
163 argp->reclaim));
164 if (resp->status == nlm_drop_reply)
165 rc = rpc_drop_reply;
166 else
167 dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
169 nlm_release_host(host);
170 nlm_release_file(file);
171 return rc;
174 static __be32
175 nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
176 struct nlm_res *resp)
178 struct nlm_host *host;
179 struct nlm_file *file;
181 dprintk("lockd: CANCEL called\n");
183 resp->cookie = argp->cookie;
185 /* Don't accept requests during grace period */
186 if (locks_in_grace()) {
187 resp->status = nlm_lck_denied_grace_period;
188 return rpc_success;
191 /* Obtain client and file */
192 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
193 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
195 /* Try to cancel request. */
196 resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
198 dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
199 nlm_release_host(host);
200 nlm_release_file(file);
201 return rpc_success;
205 * UNLOCK: release a lock
207 static __be32
208 nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
209 struct nlm_res *resp)
211 struct nlm_host *host;
212 struct nlm_file *file;
214 dprintk("lockd: UNLOCK called\n");
216 resp->cookie = argp->cookie;
218 /* Don't accept new lock requests during grace period */
219 if (locks_in_grace()) {
220 resp->status = nlm_lck_denied_grace_period;
221 return rpc_success;
224 /* Obtain client and file */
225 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
226 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
228 /* Now try to remove the lock */
229 resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
231 dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
232 nlm_release_host(host);
233 nlm_release_file(file);
234 return rpc_success;
238 * GRANTED: A server calls us to tell that a process' lock request
239 * was granted
241 static __be32
242 nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
243 struct nlm_res *resp)
245 resp->cookie = argp->cookie;
247 dprintk("lockd: GRANTED called\n");
248 resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
249 dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
250 return rpc_success;
254 * This is the generic lockd callback for async RPC calls
256 static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
258 dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
259 -task->tk_status);
262 static void nlmsvc_callback_release(void *data)
264 lock_kernel();
265 nlm_release_call(data);
266 unlock_kernel();
269 static const struct rpc_call_ops nlmsvc_callback_ops = {
270 .rpc_call_done = nlmsvc_callback_exit,
271 .rpc_release = nlmsvc_callback_release,
275 * `Async' versions of the above service routines. They aren't really,
276 * because we send the callback before the reply proper. I hope this
277 * doesn't break any clients.
279 static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
280 __be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res *))
282 struct nlm_host *host;
283 struct nlm_rqst *call;
284 __be32 stat;
286 host = nlmsvc_lookup_host(rqstp,
287 argp->lock.caller,
288 argp->lock.len);
289 if (host == NULL)
290 return rpc_system_err;
292 call = nlm_alloc_call(host);
293 if (call == NULL)
294 return rpc_system_err;
296 stat = func(rqstp, argp, &call->a_res);
297 if (stat != 0) {
298 nlm_release_call(call);
299 return stat;
302 call->a_flags = RPC_TASK_ASYNC;
303 if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
304 return rpc_system_err;
305 return rpc_success;
308 static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
309 void *resp)
311 dprintk("lockd: TEST_MSG called\n");
312 return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, argp, nlmsvc_proc_test);
315 static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
316 void *resp)
318 dprintk("lockd: LOCK_MSG called\n");
319 return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlmsvc_proc_lock);
322 static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
323 void *resp)
325 dprintk("lockd: CANCEL_MSG called\n");
326 return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlmsvc_proc_cancel);
329 static __be32
330 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
331 void *resp)
333 dprintk("lockd: UNLOCK_MSG called\n");
334 return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlmsvc_proc_unlock);
337 static __be32
338 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
339 void *resp)
341 dprintk("lockd: GRANTED_MSG called\n");
342 return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlmsvc_proc_granted);
346 * SHARE: create a DOS share or alter existing share.
348 static __be32
349 nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
350 struct nlm_res *resp)
352 struct nlm_host *host;
353 struct nlm_file *file;
355 dprintk("lockd: SHARE called\n");
357 resp->cookie = argp->cookie;
359 /* Don't accept new lock requests during grace period */
360 if (locks_in_grace() && !argp->reclaim) {
361 resp->status = nlm_lck_denied_grace_period;
362 return rpc_success;
365 /* Obtain client and file */
366 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
367 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
369 /* Now try to create the share */
370 resp->status = cast_status(nlmsvc_share_file(host, file, argp));
372 dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
373 nlm_release_host(host);
374 nlm_release_file(file);
375 return rpc_success;
379 * UNSHARE: Release a DOS share.
381 static __be32
382 nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
383 struct nlm_res *resp)
385 struct nlm_host *host;
386 struct nlm_file *file;
388 dprintk("lockd: UNSHARE called\n");
390 resp->cookie = argp->cookie;
392 /* Don't accept requests during grace period */
393 if (locks_in_grace()) {
394 resp->status = nlm_lck_denied_grace_period;
395 return rpc_success;
398 /* Obtain client and file */
399 if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
400 return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
402 /* Now try to unshare the file */
403 resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
405 dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
406 nlm_release_host(host);
407 nlm_release_file(file);
408 return rpc_success;
412 * NM_LOCK: Create an unmonitored lock
414 static __be32
415 nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
416 struct nlm_res *resp)
418 dprintk("lockd: NM_LOCK called\n");
420 argp->monitor = 0; /* just clean the monitor flag */
421 return nlmsvc_proc_lock(rqstp, argp, resp);
425 * FREE_ALL: Release all locks and shares held by client
427 static __be32
428 nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
429 void *resp)
431 struct nlm_host *host;
433 /* Obtain client */
434 if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
435 return rpc_success;
437 nlmsvc_free_host_resources(host);
438 nlm_release_host(host);
439 return rpc_success;
443 * SM_NOTIFY: private callback from statd (not part of official NLM proto)
445 static __be32
446 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
447 void *resp)
449 dprintk("lockd: SM_NOTIFY called\n");
451 if (!nlm_privileged_requester(rqstp)) {
452 char buf[RPC_MAX_ADDRBUFLEN];
453 printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
454 svc_print_addr(rqstp, buf, sizeof(buf)));
455 return rpc_system_err;
458 nlm_host_rebooted(argp);
459 return rpc_success;
463 * client sent a GRANTED_RES, let's remove the associated block
465 static __be32
466 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
467 void *resp)
469 if (!nlmsvc_ops)
470 return rpc_success;
472 dprintk("lockd: GRANTED_RES called\n");
474 nlmsvc_grant_reply(&argp->cookie, argp->status);
475 return rpc_success;
479 * NLM Server procedures.
482 #define nlmsvc_encode_norep nlmsvc_encode_void
483 #define nlmsvc_decode_norep nlmsvc_decode_void
484 #define nlmsvc_decode_testres nlmsvc_decode_void
485 #define nlmsvc_decode_lockres nlmsvc_decode_void
486 #define nlmsvc_decode_unlockres nlmsvc_decode_void
487 #define nlmsvc_decode_cancelres nlmsvc_decode_void
488 #define nlmsvc_decode_grantedres nlmsvc_decode_void
490 #define nlmsvc_proc_none nlmsvc_proc_null
491 #define nlmsvc_proc_test_res nlmsvc_proc_null
492 #define nlmsvc_proc_lock_res nlmsvc_proc_null
493 #define nlmsvc_proc_cancel_res nlmsvc_proc_null
494 #define nlmsvc_proc_unlock_res nlmsvc_proc_null
496 struct nlm_void { int dummy; };
498 #define PROC(name, xargt, xrest, argt, rest, respsize) \
499 { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
500 .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
501 .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
502 .pc_release = NULL, \
503 .pc_argsize = sizeof(struct nlm_##argt), \
504 .pc_ressize = sizeof(struct nlm_##rest), \
505 .pc_xdrressize = respsize, \
508 #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
509 #define St 1 /* status */
510 #define No (1+1024/4) /* Net Obj */
511 #define Rg 2 /* range - offset + size */
513 struct svc_procedure nlmsvc_procedures[] = {
514 PROC(null, void, void, void, void, 1),
515 PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
516 PROC(lock, lockargs, res, args, res, Ck+St),
517 PROC(cancel, cancargs, res, args, res, Ck+St),
518 PROC(unlock, unlockargs, res, args, res, Ck+St),
519 PROC(granted, testargs, res, args, res, Ck+St),
520 PROC(test_msg, testargs, norep, args, void, 1),
521 PROC(lock_msg, lockargs, norep, args, void, 1),
522 PROC(cancel_msg, cancargs, norep, args, void, 1),
523 PROC(unlock_msg, unlockargs, norep, args, void, 1),
524 PROC(granted_msg, testargs, norep, args, void, 1),
525 PROC(test_res, testres, norep, res, void, 1),
526 PROC(lock_res, lockres, norep, res, void, 1),
527 PROC(cancel_res, cancelres, norep, res, void, 1),
528 PROC(unlock_res, unlockres, norep, res, void, 1),
529 PROC(granted_res, res, norep, res, void, 1),
530 /* statd callback */
531 PROC(sm_notify, reboot, void, reboot, void, 1),
532 PROC(none, void, void, void, void, 1),
533 PROC(none, void, void, void, void, 1),
534 PROC(none, void, void, void, void, 1),
535 PROC(share, shareargs, shareres, args, res, Ck+St+1),
536 PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
537 PROC(nm_lock, lockargs, res, args, res, Ck+St),
538 PROC(free_all, notify, void, args, void, 0),