]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/callback_xdr.c
NFS refactor nfs_find_client and reference client across callback processing
[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 #else /* CONFIG_NFS_V4_1 */
600
601 static __be32
602 preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
603 {
604         return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
605 }
606
607 #endif /* CONFIG_NFS_V4_1 */
608
609 static __be32
610 preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
611 {
612         switch (op_nr) {
613         case OP_CB_GETATTR:
614         case OP_CB_RECALL:
615                 *op = &callback_ops[op_nr];
616                 break;
617         default:
618                 return htonl(NFS4ERR_OP_ILLEGAL);
619         }
620
621         return htonl(NFS_OK);
622 }
623
624 static __be32 process_op(uint32_t minorversion, int nop,
625                 struct svc_rqst *rqstp,
626                 struct xdr_stream *xdr_in, void *argp,
627                 struct xdr_stream *xdr_out, void *resp,
628                 struct cb_process_state *cps)
629 {
630         struct callback_op *op = &callback_ops[0];
631         unsigned int op_nr;
632         __be32 status;
633         long maxlen;
634         __be32 res;
635
636         dprintk("%s: start\n", __func__);
637         status = decode_op_hdr(xdr_in, &op_nr);
638         if (unlikely(status))
639                 return status;
640
641         dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
642                 __func__, minorversion, nop, op_nr);
643
644         status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
645                                 preprocess_nfs4_op(op_nr, &op);
646         if (status == htonl(NFS4ERR_OP_ILLEGAL))
647                 op_nr = OP_CB_ILLEGAL;
648         if (status)
649                 goto encode_hdr;
650
651         if (cps->drc_status) {
652                 status = cps->drc_status;
653                 goto encode_hdr;
654         }
655
656         maxlen = xdr_out->end - xdr_out->p;
657         if (maxlen > 0 && maxlen < PAGE_SIZE) {
658                 status = op->decode_args(rqstp, xdr_in, argp);
659                 if (likely(status == 0))
660                         status = op->process_op(argp, resp, cps);
661         } else
662                 status = htonl(NFS4ERR_RESOURCE);
663
664 encode_hdr:
665         res = encode_op_hdr(xdr_out, op_nr, status);
666         if (unlikely(res))
667                 return res;
668         if (op->encode_res != NULL && status == 0)
669                 status = op->encode_res(rqstp, xdr_out, resp);
670         dprintk("%s: done, status = %d\n", __func__, ntohl(status));
671         return status;
672 }
673
674 /*
675  * Decode, process and encode a COMPOUND
676  */
677 static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
678 {
679         struct cb_compound_hdr_arg hdr_arg = { 0 };
680         struct cb_compound_hdr_res hdr_res = { NULL };
681         struct xdr_stream xdr_in, xdr_out;
682         __be32 *p, status;
683         struct cb_process_state cps = {
684                 .drc_status = 0,
685                 .clp = NULL,
686         };
687         unsigned int nops = 0;
688
689         dprintk("%s: start\n", __func__);
690
691         xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
692
693         p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
694         xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
695
696         status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
697         if (status == __constant_htonl(NFS4ERR_RESOURCE))
698                 return rpc_garbage_args;
699
700         if (hdr_arg.minorversion == 0) {
701                 cps.clp = nfs4_find_client_ident(hdr_arg.cb_ident);
702                 if (!cps.clp)
703                         return rpc_drop_reply;
704         } else
705                 cps.svc_sid = bc_xprt_sid(rqstp);
706
707         hdr_res.taglen = hdr_arg.taglen;
708         hdr_res.tag = hdr_arg.tag;
709         if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
710                 return rpc_system_err;
711
712         while (status == 0 && nops != hdr_arg.nops) {
713                 status = process_op(hdr_arg.minorversion, nops, rqstp,
714                                     &xdr_in, argp, &xdr_out, resp, &cps);
715                 nops++;
716         }
717
718         /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
719         * resource error in cb_compound status without returning op */
720         if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
721                 status = htonl(NFS4ERR_RESOURCE);
722                 nops--;
723         }
724
725         *hdr_res.status = status;
726         *hdr_res.nops = htonl(nops);
727         nfs_put_client(cps.clp);
728         dprintk("%s: done, status = %u\n", __func__, ntohl(status));
729         return rpc_success;
730 }
731
732 /*
733  * Define NFS4 callback COMPOUND ops.
734  */
735 static struct callback_op callback_ops[] = {
736         [0] = {
737                 .res_maxsize = CB_OP_HDR_RES_MAXSZ,
738         },
739         [OP_CB_GETATTR] = {
740                 .process_op = (callback_process_op_t)nfs4_callback_getattr,
741                 .decode_args = (callback_decode_arg_t)decode_getattr_args,
742                 .encode_res = (callback_encode_res_t)encode_getattr_res,
743                 .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
744         },
745         [OP_CB_RECALL] = {
746                 .process_op = (callback_process_op_t)nfs4_callback_recall,
747                 .decode_args = (callback_decode_arg_t)decode_recall_args,
748                 .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
749         },
750 #if defined(CONFIG_NFS_V4_1)
751         [OP_CB_SEQUENCE] = {
752                 .process_op = (callback_process_op_t)nfs4_callback_sequence,
753                 .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
754                 .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
755                 .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
756         },
757         [OP_CB_RECALL_ANY] = {
758                 .process_op = (callback_process_op_t)nfs4_callback_recallany,
759                 .decode_args = (callback_decode_arg_t)decode_recallany_args,
760                 .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
761         },
762         [OP_CB_RECALL_SLOT] = {
763                 .process_op = (callback_process_op_t)nfs4_callback_recallslot,
764                 .decode_args = (callback_decode_arg_t)decode_recallslot_args,
765                 .res_maxsize = CB_OP_RECALLSLOT_RES_MAXSZ,
766         },
767 #endif /* CONFIG_NFS_V4_1 */
768 };
769
770 /*
771  * Define NFS4 callback procedures
772  */
773 static struct svc_procedure nfs4_callback_procedures1[] = {
774         [CB_NULL] = {
775                 .pc_func = nfs4_callback_null,
776                 .pc_decode = (kxdrproc_t)nfs4_decode_void,
777                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
778                 .pc_xdrressize = 1,
779         },
780         [CB_COMPOUND] = {
781                 .pc_func = nfs4_callback_compound,
782                 .pc_encode = (kxdrproc_t)nfs4_encode_void,
783                 .pc_argsize = 256,
784                 .pc_ressize = 256,
785                 .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
786         }
787 };
788
789 struct svc_version nfs4_callback_version1 = {
790         .vs_vers = 1,
791         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
792         .vs_proc = nfs4_callback_procedures1,
793         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
794         .vs_dispatch = NULL,
795         .vs_hidden = 1,
796 };
797
798 struct svc_version nfs4_callback_version4 = {
799         .vs_vers = 4,
800         .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
801         .vs_proc = nfs4_callback_procedures1,
802         .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
803         .vs_dispatch = NULL,
804 };