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