]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/callback_xdr.c
NFS add session back channel draining
[karo-tx-linux.git] / fs / nfs / callback_xdr.c
1 /*
2  * linux/fs/nfs/callback_xdr.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFSv4 callback encode/decode procedures
7  */
8 #include <linux/kernel.h>
9 #include <linux/sunrpc/svc.h>
10 #include <linux/nfs4.h>
11 #include <linux/nfs_fs.h>
12 #include <linux/slab.h>
13 #include <linux/sunrpc/bc_xprt.h>
14 #include "nfs4_fs.h"
15 #include "callback.h"
16 #include "internal.h"
17
18 #define CB_OP_TAGLEN_MAXSZ      (512)
19 #define CB_OP_HDR_RES_MAXSZ     (2 + CB_OP_TAGLEN_MAXSZ)
20 #define CB_OP_GETATTR_BITMAP_MAXSZ      (4)
21 #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
22                                 CB_OP_GETATTR_BITMAP_MAXSZ + \
23                                 2 + 2 + 3 + 3)
24 #define CB_OP_RECALL_RES_MAXSZ  (CB_OP_HDR_RES_MAXSZ)
25
26 #if defined(CONFIG_NFS_V4_1)
27 #define CB_OP_SEQUENCE_RES_MAXSZ        (CB_OP_HDR_RES_MAXSZ + \
28                                         4 + 1 + 3)
29 #define CB_OP_RECALLANY_RES_MAXSZ       (CB_OP_HDR_RES_MAXSZ)
30 #define CB_OP_RECALLSLOT_RES_MAXSZ      (CB_OP_HDR_RES_MAXSZ)
31 #endif /* CONFIG_NFS_V4_1 */
32
33 #define NFSDBG_FACILITY NFSDBG_CALLBACK
34
35 /* Internal error code */
36 #define NFS4ERR_RESOURCE_HDR    11050
37
38 typedef __be32 (*callback_process_op_t)(void *, void *,
39                                         struct cb_process_state *);
40 typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
41 typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
42
43
44 struct callback_op {
45         callback_process_op_t process_op;
46         callback_decode_arg_t decode_args;
47         callback_encode_res_t encode_res;
48         long res_maxsize;
49 };
50
51 static struct callback_op callback_ops[];
52
53 static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
54 {
55         return htonl(NFS4_OK);
56 }
57
58 static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
59 {
60         return xdr_argsize_check(rqstp, p);
61 }
62
63 static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
64 {
65         return xdr_ressize_check(rqstp, p);
66 }
67
68 static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
69 {
70         __be32 *p;
71
72         p = xdr_inline_decode(xdr, nbytes);
73         if (unlikely(p == NULL))
74                 printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
75         return p;
76 }
77
78 static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
79 {
80         __be32 *p;
81
82         p = read_buf(xdr, 4);
83         if (unlikely(p == NULL))
84                 return htonl(NFS4ERR_RESOURCE);
85         *len = ntohl(*p);
86
87         if (*len != 0) {
88                 p = read_buf(xdr, *len);
89                 if (unlikely(p == NULL))
90                         return htonl(NFS4ERR_RESOURCE);
91                 *str = (const char *)p;
92         } else
93                 *str = NULL;
94
95         return 0;
96 }
97
98 static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
99 {
100         __be32 *p;
101
102         p = read_buf(xdr, 4);
103         if (unlikely(p == NULL))
104                 return htonl(NFS4ERR_RESOURCE);
105         fh->size = ntohl(*p);
106         if (fh->size > NFS4_FHSIZE)
107                 return htonl(NFS4ERR_BADHANDLE);
108         p = read_buf(xdr, fh->size);
109         if (unlikely(p == NULL))
110                 return htonl(NFS4ERR_RESOURCE);
111         memcpy(&fh->data[0], p, fh->size);
112         memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
113         return 0;
114 }
115
116 static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
117 {
118         __be32 *p;
119         unsigned int attrlen;
120
121         p = read_buf(xdr, 4);
122         if (unlikely(p == NULL))
123                 return htonl(NFS4ERR_RESOURCE);
124         attrlen = ntohl(*p);
125         p = read_buf(xdr, attrlen << 2);
126         if (unlikely(p == NULL))
127                 return htonl(NFS4ERR_RESOURCE);
128         if (likely(attrlen > 0))
129                 bitmap[0] = ntohl(*p++);
130         if (attrlen > 1)
131                 bitmap[1] = ntohl(*p);
132         return 0;
133 }
134
135 static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
136 {
137         __be32 *p;
138
139         p = read_buf(xdr, 16);
140         if (unlikely(p == NULL))
141                 return htonl(NFS4ERR_RESOURCE);
142         memcpy(stateid->data, p, 16);
143         return 0;
144 }
145
146 static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
147 {
148         __be32 *p;
149         __be32 status;
150
151         status = decode_string(xdr, &hdr->taglen, &hdr->tag);
152         if (unlikely(status != 0))
153                 return status;
154         /* We do not like overly long tags! */
155         if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
156                 printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
157                                 __func__, hdr->taglen);
158                 return htonl(NFS4ERR_RESOURCE);
159         }
160         p = read_buf(xdr, 12);
161         if (unlikely(p == NULL))
162                 return htonl(NFS4ERR_RESOURCE);
163         hdr->minorversion = ntohl(*p++);
164         /* Check minor version is zero or one. */
165         if (hdr->minorversion <= 1) {
166                 hdr->cb_ident = ntohl(*p++); /* ignored by v4.1 */
167         } else {
168                 printk(KERN_WARNING "%s: NFSv4 server callback with "
169                         "illegal minor version %u!\n",
170                         __func__, hdr->minorversion);
171                 return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
172         }
173         hdr->nops = ntohl(*p);
174         dprintk("%s: minorversion %d nops %d\n", __func__,
175                 hdr->minorversion, hdr->nops);
176         return 0;
177 }
178
179 static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
180 {
181         __be32 *p;
182         p = read_buf(xdr, 4);
183         if (unlikely(p == NULL))
184                 return htonl(NFS4ERR_RESOURCE_HDR);
185         *op = ntohl(*p);
186         return 0;
187 }
188
189 static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
190 {
191         __be32 status;
192
193         status = decode_fh(xdr, &args->fh);
194         if (unlikely(status != 0))
195                 goto out;
196         args->addr = svc_addr(rqstp);
197         status = decode_bitmap(xdr, args->bitmap);
198 out:
199         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
200         return status;
201 }
202
203 static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
204 {
205         __be32 *p;
206         __be32 status;
207
208         args->addr = svc_addr(rqstp);
209         status = decode_stateid(xdr, &args->stateid);
210         if (unlikely(status != 0))
211                 goto out;
212         p = read_buf(xdr, 4);
213         if (unlikely(p == NULL)) {
214                 status = htonl(NFS4ERR_RESOURCE);
215                 goto out;
216         }
217         args->truncate = ntohl(*p);
218         status = decode_fh(xdr, &args->fh);
219 out:
220         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
221         return status;
222 }
223
224 #if defined(CONFIG_NFS_V4_1)
225
226 static __be32 decode_sessionid(struct xdr_stream *xdr,
227                                  struct nfs4_sessionid *sid)
228 {
229         __be32 *p;
230         int len = NFS4_MAX_SESSIONID_LEN;
231
232         p = read_buf(xdr, len);
233         if (unlikely(p == NULL))
234                 return htonl(NFS4ERR_RESOURCE);
235
236         memcpy(sid->data, p, len);
237         return 0;
238 }
239
240 static __be32 decode_rc_list(struct xdr_stream *xdr,
241                                struct referring_call_list *rc_list)
242 {
243         __be32 *p;
244         int i;
245         __be32 status;
246
247         status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
248         if (status)
249                 goto out;
250
251         status = htonl(NFS4ERR_RESOURCE);
252         p = read_buf(xdr, sizeof(uint32_t));
253         if (unlikely(p == NULL))
254                 goto out;
255
256         rc_list->rcl_nrefcalls = ntohl(*p++);
257         if (rc_list->rcl_nrefcalls) {
258                 p = read_buf(xdr,
259                              rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
260                 if (unlikely(p == NULL))
261                         goto out;
262                 rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
263                                                 sizeof(*rc_list->rcl_refcalls),
264                                                 GFP_KERNEL);
265                 if (unlikely(rc_list->rcl_refcalls == NULL))
266                         goto out;
267                 for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
268                         rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
269                         rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
270                 }
271         }
272         status = 0;
273
274 out:
275         return status;
276 }
277
278 static __be32 decode_cb_sequence_args(struct svc_rqst *rqstp,
279                                         struct xdr_stream *xdr,
280                                         struct cb_sequenceargs *args)
281 {
282         __be32 *p;
283         int i;
284         __be32 status;
285
286         status = decode_sessionid(xdr, &args->csa_sessionid);
287         if (status)
288                 goto out;
289
290         status = htonl(NFS4ERR_RESOURCE);
291         p = read_buf(xdr, 5 * sizeof(uint32_t));
292         if (unlikely(p == NULL))
293                 goto out;
294
295         args->csa_addr = svc_addr(rqstp);
296         args->csa_sequenceid = ntohl(*p++);
297         args->csa_slotid = ntohl(*p++);
298         args->csa_highestslotid = ntohl(*p++);
299         args->csa_cachethis = ntohl(*p++);
300         args->csa_nrclists = ntohl(*p++);
301         args->csa_rclists = NULL;
302         if (args->csa_nrclists) {
303                 args->csa_rclists = kmalloc(args->csa_nrclists *
304                                             sizeof(*args->csa_rclists),
305                                             GFP_KERNEL);
306                 if (unlikely(args->csa_rclists == NULL))
307                         goto out;
308
309                 for (i = 0; i < args->csa_nrclists; i++) {
310                         status = decode_rc_list(xdr, &args->csa_rclists[i]);
311                         if (status)
312                                 goto out_free;
313                 }
314         }
315         status = 0;
316
317         dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
318                 "highestslotid %u cachethis %d nrclists %u\n",
319                 __func__,
320                 ((u32 *)&args->csa_sessionid)[0],
321                 ((u32 *)&args->csa_sessionid)[1],
322                 ((u32 *)&args->csa_sessionid)[2],
323                 ((u32 *)&args->csa_sessionid)[3],
324                 args->csa_sequenceid, args->csa_slotid,
325                 args->csa_highestslotid, args->csa_cachethis,
326                 args->csa_nrclists);
327 out:
328         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
329         return status;
330
331 out_free:
332         for (i = 0; i < args->csa_nrclists; i++)
333                 kfree(args->csa_rclists[i].rcl_refcalls);
334         kfree(args->csa_rclists);
335         goto out;
336 }
337
338 static __be32 decode_recallany_args(struct svc_rqst *rqstp,
339                                       struct xdr_stream *xdr,
340                                       struct cb_recallanyargs *args)
341 {
342         __be32 *p;
343
344         args->craa_addr = svc_addr(rqstp);
345         p = read_buf(xdr, 4);
346         if (unlikely(p == NULL))
347                 return htonl(NFS4ERR_BADXDR);
348         args->craa_objs_to_keep = ntohl(*p++);
349         p = read_buf(xdr, 4);
350         if (unlikely(p == NULL))
351                 return htonl(NFS4ERR_BADXDR);
352         args->craa_type_mask = ntohl(*p);
353
354         return 0;
355 }
356
357 static __be32 decode_recallslot_args(struct svc_rqst *rqstp,
358                                         struct xdr_stream *xdr,
359                                         struct cb_recallslotargs *args)
360 {
361         __be32 *p;
362
363         args->crsa_addr = svc_addr(rqstp);
364         p = read_buf(xdr, 4);
365         if (unlikely(p == NULL))
366                 return htonl(NFS4ERR_BADXDR);
367         args->crsa_target_max_slots = ntohl(*p++);
368         return 0;
369 }
370
371 #endif /* CONFIG_NFS_V4_1 */
372
373 static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
374 {
375         __be32 *p;
376
377         p = xdr_reserve_space(xdr, 4 + len);
378         if (unlikely(p == NULL))
379                 return htonl(NFS4ERR_RESOURCE);
380         xdr_encode_opaque(p, str, len);
381         return 0;
382 }
383
384 #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
385 #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
386 static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
387 {
388         __be32 bm[2];
389         __be32 *p;
390
391         bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
392         bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
393         if (bm[1] != 0) {
394                 p = xdr_reserve_space(xdr, 16);
395                 if (unlikely(p == NULL))
396                         return htonl(NFS4ERR_RESOURCE);
397                 *p++ = htonl(2);
398                 *p++ = bm[0];
399                 *p++ = bm[1];
400         } else if (bm[0] != 0) {
401                 p = xdr_reserve_space(xdr, 12);
402                 if (unlikely(p == NULL))
403                         return htonl(NFS4ERR_RESOURCE);
404                 *p++ = htonl(1);
405                 *p++ = bm[0];
406         } else {
407                 p = xdr_reserve_space(xdr, 8);
408                 if (unlikely(p == NULL))
409                         return htonl(NFS4ERR_RESOURCE);
410                 *p++ = htonl(0);
411         }
412         *savep = p;
413         return 0;
414 }
415
416 static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
417 {
418         __be32 *p;
419
420         if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
421                 return 0;
422         p = xdr_reserve_space(xdr, 8);
423         if (unlikely(!p))
424                 return htonl(NFS4ERR_RESOURCE);
425         p = xdr_encode_hyper(p, change);
426         return 0;
427 }
428
429 static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
430 {
431         __be32 *p;
432
433         if (!(bitmap[0] & FATTR4_WORD0_SIZE))
434                 return 0;
435         p = xdr_reserve_space(xdr, 8);
436         if (unlikely(!p))
437                 return htonl(NFS4ERR_RESOURCE);
438         p = xdr_encode_hyper(p, size);
439         return 0;
440 }
441
442 static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
443 {
444         __be32 *p;
445
446         p = xdr_reserve_space(xdr, 12);
447         if (unlikely(!p))
448                 return htonl(NFS4ERR_RESOURCE);
449         p = xdr_encode_hyper(p, time->tv_sec);
450         *p = htonl(time->tv_nsec);
451         return 0;
452 }
453
454 static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
455 {
456         if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
457                 return 0;
458         return encode_attr_time(xdr,time);
459 }
460
461 static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
462 {
463         if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
464                 return 0;
465         return encode_attr_time(xdr,time);
466 }
467
468 static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
469 {
470         __be32 status;
471
472         hdr->status = xdr_reserve_space(xdr, 4);
473         if (unlikely(hdr->status == NULL))
474                 return htonl(NFS4ERR_RESOURCE);
475         status = encode_string(xdr, hdr->taglen, hdr->tag);
476         if (unlikely(status != 0))
477                 return status;
478         hdr->nops = xdr_reserve_space(xdr, 4);
479         if (unlikely(hdr->nops == NULL))
480                 return htonl(NFS4ERR_RESOURCE);
481         return 0;
482 }
483
484 static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
485 {
486         __be32 *p;
487         
488         p = xdr_reserve_space(xdr, 8);
489         if (unlikely(p == NULL))
490                 return htonl(NFS4ERR_RESOURCE_HDR);
491         *p++ = htonl(op);
492         *p = res;
493         return 0;
494 }
495
496 static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
497 {
498         __be32 *savep = NULL;
499         __be32 status = res->status;
500         
501         if (unlikely(status != 0))
502                 goto out;
503         status = encode_attr_bitmap(xdr, res->bitmap, &savep);
504         if (unlikely(status != 0))
505                 goto out;
506         status = encode_attr_change(xdr, res->bitmap, res->change_attr);
507         if (unlikely(status != 0))
508                 goto out;
509         status = encode_attr_size(xdr, res->bitmap, res->size);
510         if (unlikely(status != 0))
511                 goto out;
512         status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
513         if (unlikely(status != 0))
514                 goto out;
515         status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
516         *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
517 out:
518         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
519         return status;
520 }
521
522 #if defined(CONFIG_NFS_V4_1)
523
524 static __be32 encode_sessionid(struct xdr_stream *xdr,
525                                  const struct nfs4_sessionid *sid)
526 {
527         __be32 *p;
528         int len = NFS4_MAX_SESSIONID_LEN;
529
530         p = xdr_reserve_space(xdr, len);
531         if (unlikely(p == NULL))
532                 return htonl(NFS4ERR_RESOURCE);
533
534         memcpy(p, sid, len);
535         return 0;
536 }
537
538 static __be32 encode_cb_sequence_res(struct svc_rqst *rqstp,
539                                        struct xdr_stream *xdr,
540                                        const struct cb_sequenceres *res)
541 {
542         __be32 *p;
543         unsigned status = res->csr_status;
544
545         if (unlikely(status != 0))
546                 goto out;
547
548         encode_sessionid(xdr, &res->csr_sessionid);
549
550         p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
551         if (unlikely(p == NULL))
552                 return htonl(NFS4ERR_RESOURCE);
553
554         *p++ = htonl(res->csr_sequenceid);
555         *p++ = htonl(res->csr_slotid);
556         *p++ = htonl(res->csr_highestslotid);
557         *p++ = htonl(res->csr_target_highestslotid);
558 out:
559         dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
560         return status;
561 }
562
563 static __be32
564 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
565 {
566         if (op_nr == OP_CB_SEQUENCE) {
567                 if (nop != 0)
568                         return htonl(NFS4ERR_SEQUENCE_POS);
569         } else {
570                 if (nop == 0)
571                         return htonl(NFS4ERR_OP_NOT_IN_SESSION);
572         }
573
574         switch (op_nr) {
575         case OP_CB_GETATTR:
576         case OP_CB_RECALL:
577         case OP_CB_SEQUENCE:
578         case OP_CB_RECALL_ANY:
579         case OP_CB_RECALL_SLOT:
580                 *op = &callback_ops[op_nr];
581                 break;
582
583         case OP_CB_LAYOUTRECALL:
584         case OP_CB_NOTIFY_DEVICEID:
585         case OP_CB_NOTIFY:
586         case OP_CB_PUSH_DELEG:
587         case OP_CB_RECALLABLE_OBJ_AVAIL:
588         case OP_CB_WANTS_CANCELLED:
589         case OP_CB_NOTIFY_LOCK:
590                 return htonl(NFS4ERR_NOTSUPP);
591
592         default:
593                 return htonl(NFS4ERR_OP_ILLEGAL);
594         }
595
596         return htonl(NFS_OK);
597 }
598
599 static void nfs4_callback_free_slot(struct nfs4_session *session)
600 {
601         struct nfs4_slot_table *tbl = &session->bc_slot_table;
602
603         spin_lock(&tbl->slot_tbl_lock);
604         /*
605          * Let the state manager know callback processing done.
606          * A single slot, so highest used slotid is either 0 or -1
607          */
608         tbl->highest_used_slotid--;
609         nfs4_check_drain_bc_complete(session);
610         spin_unlock(&tbl->slot_tbl_lock);
611 }
612
613 static void nfs4_cb_free_slot(struct nfs_client *clp)
614 {
615         if (clp && clp->cl_session)
616                 nfs4_callback_free_slot(clp->cl_session);
617 }
618
619 /* A single slot, so highest used slotid is either 0 or -1 */
620 void nfs4_cb_take_slot(struct nfs_client *clp)
621 {
622         struct nfs4_slot_table *tbl = &clp->cl_session->bc_slot_table;
623
624         spin_lock(&tbl->slot_tbl_lock);
625         tbl->highest_used_slotid++;
626         BUG_ON(tbl->highest_used_slotid != 0);
627         spin_unlock(&tbl->slot_tbl_lock);
628 }
629
630 #else /* CONFIG_NFS_V4_1 */
631
632 static __be32
633 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
634 {
635         return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
636 }
637
638 static void nfs4_cb_free_slot(struct nfs_client *clp)
639 {
640 }
641 #endif /* CONFIG_NFS_V4_1 */
642
643 static __be32
644 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
645 {
646         switch (op_nr) {
647         case OP_CB_GETATTR:
648         case OP_CB_RECALL:
649                 *op = &callback_ops[op_nr];
650                 break;
651         default:
652                 return htonl(NFS4ERR_OP_ILLEGAL);
653         }
654
655         return htonl(NFS_OK);
656 }
657
658 static __be32 process_op(uint32_t minorversion, int nop,
659                 struct svc_rqst *rqstp,
660                 struct xdr_stream *xdr_in, void *argp,
661                 struct xdr_stream *xdr_out, void *resp,
662                 struct cb_process_state *cps)
663 {
664         struct callback_op *op = &callback_ops[0];
665         unsigned int op_nr;
666         __be32 status;
667         long maxlen;
668         __be32 res;
669
670         dprintk("%s: start\n", __func__);
671         status = decode_op_hdr(xdr_in, &op_nr);
672         if (unlikely(status))
673                 return status;
674
675         dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
676                 __func__, minorversion, nop, op_nr);
677
678         status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
679                                 preprocess_nfs4_op(op_nr, &op);
680         if (status == htonl(NFS4ERR_OP_ILLEGAL))
681                 op_nr = OP_CB_ILLEGAL;
682         if (status)
683                 goto encode_hdr;
684
685         if (cps->drc_status) {
686                 status = cps->drc_status;
687                 goto encode_hdr;
688         }
689
690         maxlen = xdr_out->end - xdr_out->p;
691         if (maxlen > 0 && maxlen < PAGE_SIZE) {
692                 status = op->decode_args(rqstp, xdr_in, argp);
693                 if (likely(status == 0))
694                         status = op->process_op(argp, resp, cps);
695         } else
696                 status = htonl(NFS4ERR_RESOURCE);
697
698 encode_hdr:
699         res = encode_op_hdr(xdr_out, op_nr, status);
700         if (unlikely(res))
701                 return res;
702         if (op->encode_res != NULL && status == 0)
703                 status = op->encode_res(rqstp, xdr_out, resp);
704         dprintk("%s: done, status = %d\n", __func__, ntohl(status));
705         return status;
706 }
707
708 /*
709  * Decode, process and encode a COMPOUND
710  */
711 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
712 {
713         struct cb_compound_hdr_arg hdr_arg = { 0 };
714         struct cb_compound_hdr_res hdr_res = { NULL };
715         struct xdr_stream xdr_in, xdr_out;
716         __be32 *p, status;
717         struct cb_process_state cps = {
718                 .drc_status = 0,
719                 .clp = NULL,
720         };
721         unsigned int nops = 0;
722
723         dprintk("%s: start\n", __func__);
724
725         xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
726
727         p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
728         xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
729
730         status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
731         if (status == __constant_htonl(NFS4ERR_RESOURCE))
732                 return rpc_garbage_args;
733
734         if (hdr_arg.minorversion == 0) {
735                 cps.clp = nfs4_find_client_ident(hdr_arg.cb_ident);
736                 if (!cps.clp)
737                         return rpc_drop_reply;
738         } else
739                 cps.svc_sid = bc_xprt_sid(rqstp);
740
741         hdr_res.taglen = hdr_arg.taglen;
742         hdr_res.tag = hdr_arg.tag;
743         if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
744                 return rpc_system_err;
745
746         while (status == 0 && nops != hdr_arg.nops) {
747                 status = process_op(hdr_arg.minorversion, nops, rqstp,
748                                     &xdr_in, argp, &xdr_out, resp, &cps);
749                 nops++;
750         }
751
752         /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
753         * resource error in cb_compound status without returning op */
754         if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
755                 status = htonl(NFS4ERR_RESOURCE);
756                 nops--;
757         }
758
759         *hdr_res.status = status;
760         *hdr_res.nops = htonl(nops);
761         nfs4_cb_free_slot(cps.clp);
762         nfs_put_client(cps.clp);
763         dprintk("%s: done, status = %u\n", __func__, ntohl(status));
764         return rpc_success;
765 }
766
767 /*
768  * Define NFS4 callback COMPOUND ops.
769  */
770 static struct callback_op callback_ops[] = {
771         [0] = {
772                 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
773         },
774         [OP_CB_GETATTR] = {
775                 .process_op = (callback_process_op_t)nfs4_callback_getattr,
776                 .decode_args = (callback_decode_arg_t)decode_getattr_args,
777                 .encode_res = (callback_encode_res_t)encode_getattr_res,
778                 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
779         },
780         [OP_CB_RECALL] = {
781                 .process_op = (callback_process_op_t)nfs4_callback_recall,
782                 .decode_args = (callback_decode_arg_t)decode_recall_args,
783                 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
784         },
785 #if defined(CONFIG_NFS_V4_1)
786         [OP_CB_SEQUENCE] = {
787                 .process_op = (callback_process_op_t)nfs4_callback_sequence,
788                 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
789                 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
790                 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
791         },
792         [OP_CB_RECALL_ANY] = {
793                 .process_op = (callback_process_op_t)nfs4_callback_recallany,
794                 .decode_args = (callback_decode_arg_t)decode_recallany_args,
795                 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
796         },
797         [OP_CB_RECALL_SLOT] = {
798                 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
799                 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
800                 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
801         },
802 #endif /* CONFIG_NFS_V4_1 */
803 };
804
805 /*
806  * Define NFS4 callback procedures
807  */
808 static struct svc_procedure nfs4_callback_procedures1[] = {
809         [CB_NULL] = {
810                 .pc_func = nfs4_callback_null,
811                 .pc_decode = (kxdrproc_t)nfs4_decode_void,
812                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
813                 .pc_xdrressize = 1,
814         },
815         [CB_COMPOUND] = {
816                 .pc_func = nfs4_callback_compound,
817                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
818                 .pc_argsize = 256,
819                 .pc_ressize = 256,
820                 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
821         }
822 };
823
824 struct svc_version nfs4_callback_version1 = {
825         .vs_vers = 1,
826         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
827         .vs_proc = nfs4_callback_procedures1,
828         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
829         .vs_dispatch = NULL,
830         .vs_hidden = 1,
831 };
832
833 struct svc_version nfs4_callback_version4 = {
834         .vs_vers = 4,
835         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
836         .vs_proc = nfs4_callback_procedures1,
837         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
838         .vs_dispatch = NULL,
839 };