ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / lockd / svcproc.c
1 /*
2  * linux/fs/lockd/svcproc.c
3  *
4  * Lockd server procedures. We don't implement the NLM_*_RES 
5  * procedures because we don't use the async procedures.
6  *
7  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8  */
9
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>
21
22
23 #define NLMDBG_FACILITY         NLMDBG_CLIENT
24
25 static u32      nlmsvc_callback(struct svc_rqst *, u32, struct nlm_res *);
26 static void     nlmsvc_callback_exit(struct rpc_task *);
27
28 #ifdef CONFIG_LOCKD_V4
29 static u32
30 cast_to_nlm(u32 status, u32 vers)
31 {
32         /* Note: status is assumed to be in network byte order !!! */
33         if (vers != 4){
34                 switch (status) {
35                 case nlm_granted:
36                 case nlm_lck_denied:
37                 case nlm_lck_denied_nolocks:
38                 case nlm_lck_blocked:
39                 case nlm_lck_denied_grace_period:
40                         break;
41                 case nlm4_deadlock:
42                         status = nlm_lck_denied;
43                         break;
44                 default:
45                         status = nlm_lck_denied_nolocks;
46                 }
47         }
48
49         return (status);
50 }
51 #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
52 #else
53 #define cast_status(status) (status)
54 #endif
55
56 /*
57  * Obtain client and file from arguments
58  */
59 static u32
60 nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
61                         struct nlm_host **hostp, struct nlm_file **filp)
62 {
63         struct nlm_host         *host = NULL;
64         struct nlm_file         *file = NULL;
65         struct nlm_lock         *lock = &argp->lock;
66         u32                     error;
67
68         /* nfsd callbacks must have been installed for this procedure */
69         if (!nlmsvc_ops)
70                 return nlm_lck_denied_nolocks;
71
72         /* Obtain host handle */
73         if (!(host = nlmsvc_lookup_host(rqstp))
74          || (argp->monitor && !host->h_monitored && nsm_monitor(host) < 0))
75                 goto no_locks;
76         *hostp = host;
77
78         /* Obtain file pointer. Not used by FREE_ALL call. */
79         if (filp != NULL) {
80                 if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
81                         goto no_locks;
82                 *filp = file;
83
84                 /* Set up the missing parts of the file_lock structure */
85                 lock->fl.fl_file  = &file->f_file;
86                 lock->fl.fl_owner = (fl_owner_t) host;
87         }
88
89         return 0;
90
91 no_locks:
92         if (host)
93                 nlm_release_host(host);
94         return nlm_lck_denied_nolocks;
95 }
96
97 /*
98  * NULL: Test for presence of service
99  */
100 static int
101 nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
102 {
103         dprintk("lockd: NULL          called\n");
104         return rpc_success;
105 }
106
107 /*
108  * TEST: Check for conflicting lock
109  */
110 static int
111 nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
112                                          struct nlm_res  *resp)
113 {
114         struct nlm_host *host;
115         struct nlm_file *file;
116
117         dprintk("lockd: TEST          called\n");
118         resp->cookie = argp->cookie;
119
120         /* Don't accept test requests during grace period */
121         if (nlmsvc_grace_period) {
122                 resp->status = nlm_lck_denied_grace_period;
123                 return rpc_success;
124         }
125
126         /* Obtain client and file */
127         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
128                 return rpc_success;
129
130         /* Now check for conflicting locks */
131         resp->status = cast_status(nlmsvc_testlock(file, &argp->lock, &resp->lock));
132
133         dprintk("lockd: TEST          status %d vers %d\n",
134                 ntohl(resp->status), rqstp->rq_vers);
135         nlm_release_host(host);
136         nlm_release_file(file);
137         return rpc_success;
138 }
139
140 static int
141 nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
142                                          struct nlm_res  *resp)
143 {
144         struct nlm_host *host;
145         struct nlm_file *file;
146
147         dprintk("lockd: LOCK          called\n");
148
149         resp->cookie = argp->cookie;
150
151         /* Don't accept new lock requests during grace period */
152         if (nlmsvc_grace_period && !argp->reclaim) {
153                 resp->status = nlm_lck_denied_grace_period;
154                 return rpc_success;
155         }
156
157         /* Obtain client and file */
158         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
159                 return rpc_success;
160
161 #if 0
162         /* If supplied state doesn't match current state, we assume it's
163          * an old request that time-warped somehow. Any error return would
164          * do in this case because it's irrelevant anyway.
165          *
166          * NB: We don't retrieve the remote host's state yet.
167          */
168         if (host->h_nsmstate && host->h_nsmstate != argp->state) {
169                 resp->status = nlm_lck_denied_nolocks;
170         } else
171 #endif
172
173         /* Now try to lock the file */
174         resp->status = cast_status(nlmsvc_lock(rqstp, file, &argp->lock,
175                                                argp->block, &argp->cookie));
176
177         dprintk("lockd: LOCK          status %d\n", ntohl(resp->status));
178         nlm_release_host(host);
179         nlm_release_file(file);
180         return rpc_success;
181 }
182
183 static int
184 nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
185                                            struct nlm_res  *resp)
186 {
187         struct nlm_host *host;
188         struct nlm_file *file;
189
190         dprintk("lockd: CANCEL        called\n");
191
192         resp->cookie = argp->cookie;
193
194         /* Don't accept requests during grace period */
195         if (nlmsvc_grace_period) {
196                 resp->status = nlm_lck_denied_grace_period;
197                 return rpc_success;
198         }
199
200         /* Obtain client and file */
201         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
202                 return rpc_success;
203
204         /* Try to cancel request. */
205         resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
206
207         dprintk("lockd: CANCEL        status %d\n", ntohl(resp->status));
208         nlm_release_host(host);
209         nlm_release_file(file);
210         return rpc_success;
211 }
212
213 /*
214  * UNLOCK: release a lock
215  */
216 static int
217 nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
218                                            struct nlm_res  *resp)
219 {
220         struct nlm_host *host;
221         struct nlm_file *file;
222
223         dprintk("lockd: UNLOCK        called\n");
224
225         resp->cookie = argp->cookie;
226
227         /* Don't accept new lock requests during grace period */
228         if (nlmsvc_grace_period) {
229                 resp->status = nlm_lck_denied_grace_period;
230                 return rpc_success;
231         }
232
233         /* Obtain client and file */
234         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
235                 return rpc_success;
236
237         /* Now try to remove the lock */
238         resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
239
240         dprintk("lockd: UNLOCK        status %d\n", ntohl(resp->status));
241         nlm_release_host(host);
242         nlm_release_file(file);
243         return rpc_success;
244 }
245
246 /*
247  * GRANTED: A server calls us to tell that a process' lock request
248  * was granted
249  */
250 static int
251 nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
252                                             struct nlm_res  *resp)
253 {
254         resp->cookie = argp->cookie;
255
256         dprintk("lockd: GRANTED       called\n");
257         resp->status = nlmclnt_grant(&argp->lock);
258         dprintk("lockd: GRANTED       status %d\n", ntohl(resp->status));
259         return rpc_success;
260 }
261
262 /*
263  * `Async' versions of the above service routines. They aren't really,
264  * because we send the callback before the reply proper. I hope this
265  * doesn't break any clients.
266  */
267 static int
268 nlmsvc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
269                                              void            *resp)
270 {
271         struct nlm_res  res;
272         u32             stat;
273
274         dprintk("lockd: TEST_MSG      called\n");
275         memset(&res, 0, sizeof(res));
276
277         if ((stat = nlmsvc_proc_test(rqstp, argp, &res)) == 0)
278                 stat = nlmsvc_callback(rqstp, NLMPROC_TEST_RES, &res);
279         return stat;
280 }
281
282 static int
283 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
284                                              void            *resp)
285 {
286         struct nlm_res  res;
287         u32             stat;
288
289         dprintk("lockd: LOCK_MSG      called\n");
290         memset(&res, 0, sizeof(res));
291
292         if ((stat = nlmsvc_proc_lock(rqstp, argp, &res)) == 0)
293                 stat = nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, &res);
294         return stat;
295 }
296
297 static int
298 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
299                                                void            *resp)
300 {
301         struct nlm_res  res;
302         u32             stat;
303
304         dprintk("lockd: CANCEL_MSG    called\n");
305         memset(&res, 0, sizeof(res));
306
307         if ((stat = nlmsvc_proc_cancel(rqstp, argp, &res)) == 0)
308                 stat = nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, &res);
309         return stat;
310 }
311
312 static int
313 nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
314                                                void            *resp)
315 {
316         struct nlm_res  res;
317         u32             stat;
318
319         dprintk("lockd: UNLOCK_MSG    called\n");
320         memset(&res, 0, sizeof(res));
321
322         if ((stat = nlmsvc_proc_unlock(rqstp, argp, &res)) == 0)
323                 stat = nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, &res);
324         return stat;
325 }
326
327 static int
328 nlmsvc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
329                                                 void            *resp)
330 {
331         struct nlm_res  res;
332         u32             stat;
333
334         dprintk("lockd: GRANTED_MSG   called\n");
335         memset(&res, 0, sizeof(res));
336
337         if ((stat = nlmsvc_proc_granted(rqstp, argp, &res)) == 0)
338                 stat = nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, &res);
339         return stat;
340 }
341
342 /*
343  * SHARE: create a DOS share or alter existing share.
344  */
345 static int
346 nlmsvc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
347                                           struct nlm_res  *resp)
348 {
349         struct nlm_host *host;
350         struct nlm_file *file;
351
352         dprintk("lockd: SHARE         called\n");
353
354         resp->cookie = argp->cookie;
355
356         /* Don't accept new lock requests during grace period */
357         if (nlmsvc_grace_period && !argp->reclaim) {
358                 resp->status = nlm_lck_denied_grace_period;
359                 return rpc_success;
360         }
361
362         /* Obtain client and file */
363         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
364                 return rpc_success;
365
366         /* Now try to create the share */
367         resp->status = cast_status(nlmsvc_share_file(host, file, argp));
368
369         dprintk("lockd: SHARE         status %d\n", ntohl(resp->status));
370         nlm_release_host(host);
371         nlm_release_file(file);
372         return rpc_success;
373 }
374
375 /*
376  * UNSHARE: Release a DOS share.
377  */
378 static int
379 nlmsvc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
380                                             struct nlm_res  *resp)
381 {
382         struct nlm_host *host;
383         struct nlm_file *file;
384
385         dprintk("lockd: UNSHARE       called\n");
386
387         resp->cookie = argp->cookie;
388
389         /* Don't accept requests during grace period */
390         if (nlmsvc_grace_period) {
391                 resp->status = nlm_lck_denied_grace_period;
392                 return rpc_success;
393         }
394
395         /* Obtain client and file */
396         if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
397                 return rpc_success;
398
399         /* Now try to unshare the file */
400         resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
401
402         dprintk("lockd: UNSHARE       status %d\n", ntohl(resp->status));
403         nlm_release_host(host);
404         nlm_release_file(file);
405         return rpc_success;
406 }
407
408 /*
409  * NM_LOCK: Create an unmonitored lock
410  */
411 static int
412 nlmsvc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
413                                             struct nlm_res  *resp)
414 {
415         dprintk("lockd: NM_LOCK       called\n");
416
417         argp->monitor = 0;              /* just clean the monitor flag */
418         return nlmsvc_proc_lock(rqstp, argp, resp);
419 }
420
421 /*
422  * FREE_ALL: Release all locks and shares held by client
423  */
424 static int
425 nlmsvc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
426                                              void            *resp)
427 {
428         struct nlm_host *host;
429
430         /* Obtain client */
431         if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
432                 return rpc_success;
433
434         nlmsvc_free_host_resources(host);
435         nlm_release_host(host);
436         return rpc_success;
437 }
438
439 /*
440  * SM_NOTIFY: private callback from statd (not part of official NLM proto)
441  */
442 static int
443 nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
444                                               void              *resp)
445 {
446         struct sockaddr_in      saddr = rqstp->rq_addr;
447         int                     vers = argp->vers;
448         int                     prot = argp->proto >> 1;
449         struct nlm_host         *host;
450
451         dprintk("lockd: SM_NOTIFY     called\n");
452         if (saddr.sin_addr.s_addr != htonl(INADDR_LOOPBACK)
453          || ntohs(saddr.sin_port) >= 1024) {
454                 printk(KERN_WARNING
455                         "lockd: rejected NSM callback from %08x:%d\n",
456                         ntohl(rqstp->rq_addr.sin_addr.s_addr),
457                         ntohs(rqstp->rq_addr.sin_port));
458                 return rpc_system_err;
459         }
460
461         /* Obtain the host pointer for this NFS server and try to
462          * reclaim all locks we hold on this server.
463          */
464         saddr.sin_addr.s_addr = argp->addr;
465         if ((argp->proto & 1)==0) {
466                 if ((host = nlmclnt_lookup_host(&saddr, prot, vers)) != NULL) {
467                         nlmclnt_recovery(host, argp->state);
468                         nlm_release_host(host);
469                 }
470         } else {
471                 /* If we run on an NFS server, delete all locks held by the client */
472                 if ((host = nlm_lookup_host(1, &saddr, prot, vers)) != NULL) {
473                         nlmsvc_free_host_resources(host);
474                         nlm_release_host(host);
475                 }
476         }
477
478         return rpc_success;
479 }
480
481 /*
482  * client sent a GRANTED_RES, let's remove the associated block
483  */
484 static int
485 nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res  *argp,
486                                                 void            *resp)
487 {
488         if (!nlmsvc_ops)
489                 return rpc_success;
490
491         dprintk("lockd: GRANTED_RES   called\n");
492
493         nlmsvc_grant_reply(rqstp, &argp->cookie, argp->status);
494         return rpc_success;
495 }
496
497 /*
498  * This is the generic lockd callback for async RPC calls
499  */
500 static u32
501 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_res *resp)
502 {
503         struct nlm_host *host;
504         struct nlm_rqst *call;
505
506         if (!(call = nlmclnt_alloc_call()))
507                 return rpc_system_err;
508
509         host = nlmclnt_lookup_host(&rqstp->rq_addr,
510                                 rqstp->rq_prot, rqstp->rq_vers);
511         if (!host) {
512                 kfree(call);
513                 return rpc_system_err;
514         }
515
516         call->a_flags = RPC_TASK_ASYNC;
517         call->a_host  = host;
518         memcpy(&call->a_args, resp, sizeof(*resp));
519
520         if (nlmsvc_async_call(call, proc, nlmsvc_callback_exit) < 0)
521                 goto error;
522
523         return rpc_success;
524  error:
525         nlm_release_host(host);
526         kfree(call);
527         return rpc_system_err;
528 }
529
530 static void
531 nlmsvc_callback_exit(struct rpc_task *task)
532 {
533         struct nlm_rqst *call = (struct nlm_rqst *) task->tk_calldata;
534
535         if (task->tk_status < 0) {
536                 dprintk("lockd: %4d callback failed (errno = %d)\n",
537                                         task->tk_pid, -task->tk_status);
538         }
539         nlm_release_host(call->a_host);
540         kfree(call);
541 }
542
543 /*
544  * NLM Server procedures.
545  */
546
547 #define nlmsvc_encode_norep     nlmsvc_encode_void
548 #define nlmsvc_decode_norep     nlmsvc_decode_void
549 #define nlmsvc_decode_testres   nlmsvc_decode_void
550 #define nlmsvc_decode_lockres   nlmsvc_decode_void
551 #define nlmsvc_decode_unlockres nlmsvc_decode_void
552 #define nlmsvc_decode_cancelres nlmsvc_decode_void
553 #define nlmsvc_decode_grantedres        nlmsvc_decode_void
554
555 #define nlmsvc_proc_none        nlmsvc_proc_null
556 #define nlmsvc_proc_test_res    nlmsvc_proc_null
557 #define nlmsvc_proc_lock_res    nlmsvc_proc_null
558 #define nlmsvc_proc_cancel_res  nlmsvc_proc_null
559 #define nlmsvc_proc_unlock_res  nlmsvc_proc_null
560
561 struct nlm_void                 { int dummy; };
562
563 #define PROC(name, xargt, xrest, argt, rest, respsize)  \
564  { .pc_func     = (svc_procfunc) nlmsvc_proc_##name,    \
565    .pc_decode   = (kxdrproc_t) nlmsvc_decode_##xargt,   \
566    .pc_encode   = (kxdrproc_t) nlmsvc_encode_##xrest,   \
567    .pc_release  = NULL,                                 \
568    .pc_argsize  = sizeof(struct nlm_##argt),            \
569    .pc_ressize  = sizeof(struct nlm_##rest),            \
570    .pc_xdrressize = respsize,                           \
571  }
572
573 #define Ck      (1+8)   /* cookie */
574 #define St      1       /* status */
575 #define No      (1+1024/4) /* Net Obj */
576 #define Rg      2       /* range - offset + size */
577
578 struct svc_procedure            nlmsvc_procedures[] = {
579   PROC(null,            void,           void,           void,   void, 1),
580   PROC(test,            testargs,       testres,        args,   res, Ck+St+2+No+Rg),
581   PROC(lock,            lockargs,       res,            args,   res, Ck+St),
582   PROC(cancel,          cancargs,       res,            args,   res, Ck+St),
583   PROC(unlock,          unlockargs,     res,            args,   res, Ck+St),
584   PROC(granted,         testargs,       res,            args,   res, Ck+St),
585   PROC(test_msg,        testargs,       norep,          args,   void, 1),
586   PROC(lock_msg,        lockargs,       norep,          args,   void, 1),
587   PROC(cancel_msg,      cancargs,       norep,          args,   void, 1),
588   PROC(unlock_msg,      unlockargs,     norep,          args,   void, 1),
589   PROC(granted_msg,     testargs,       norep,          args,   void, 1),
590   PROC(test_res,        testres,        norep,          res,    void, 1),
591   PROC(lock_res,        lockres,        norep,          res,    void, 1),
592   PROC(cancel_res,      cancelres,      norep,          res,    void, 1),
593   PROC(unlock_res,      unlockres,      norep,          res,    void, 1),
594   PROC(granted_res,     res,            norep,          res,    void, 1),
595   /* statd callback */
596   PROC(sm_notify,       reboot,         void,           reboot, void, 1),
597   PROC(none,            void,           void,           void,   void, 1),
598   PROC(none,            void,           void,           void,   void, 1),
599   PROC(none,            void,           void,           void,   void, 1),
600   PROC(share,           shareargs,      shareres,       args,   res, Ck+St+1),
601   PROC(unshare,         shareargs,      shareres,       args,   res, Ck+St+1),
602   PROC(nm_lock,         lockargs,       res,            args,   res, Ck+St),
603   PROC(free_all,        notify,         void,           args,   void, 0),
604
605 };