1
2
3
4
5
6
7
8
9
10
11#ifndef _SUNRPC_XDR_H_
12#define _SUNRPC_XDR_H_
13
14#include <linux/uio.h>
15#include <asm/byteorder.h>
16#include <asm/unaligned.h>
17#include <linux/scatterlist.h>
18
19struct bio_vec;
20struct rpc_rqst;
21
22
23
24
25
26
27#define XDR_UNIT sizeof(__be32)
28
29
30
31
32#define XDR_QUADLEN(l) (((l) + 3) >> 2)
33
34
35
36
37#define XDR_MAX_NETOBJ 1024
38struct xdr_netobj {
39 unsigned int len;
40 u8 * data;
41};
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56struct xdr_buf {
57 struct kvec head[1],
58 tail[1];
59
60 struct bio_vec *bvec;
61 struct page ** pages;
62 unsigned int page_base,
63 page_len,
64 flags;
65#define XDRBUF_READ 0x01
66#define XDRBUF_WRITE 0x02
67#define XDRBUF_SPARSE_PAGES 0x04
68
69 unsigned int buflen,
70 len;
71};
72
73static inline void
74xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
75{
76 buf->head[0].iov_base = start;
77 buf->head[0].iov_len = len;
78 buf->tail[0].iov_len = 0;
79 buf->pages = NULL;
80 buf->page_len = 0;
81 buf->flags = 0;
82 buf->len = 0;
83 buf->buflen = len;
84}
85
86
87
88
89
90#define xdr_zero cpu_to_be32(0)
91#define xdr_one cpu_to_be32(1)
92#define xdr_two cpu_to_be32(2)
93
94#define rpc_auth_null cpu_to_be32(RPC_AUTH_NULL)
95#define rpc_auth_unix cpu_to_be32(RPC_AUTH_UNIX)
96#define rpc_auth_short cpu_to_be32(RPC_AUTH_SHORT)
97#define rpc_auth_gss cpu_to_be32(RPC_AUTH_GSS)
98#define rpc_auth_tls cpu_to_be32(RPC_AUTH_TLS)
99
100#define rpc_call cpu_to_be32(RPC_CALL)
101#define rpc_reply cpu_to_be32(RPC_REPLY)
102
103#define rpc_msg_accepted cpu_to_be32(RPC_MSG_ACCEPTED)
104
105#define rpc_success cpu_to_be32(RPC_SUCCESS)
106#define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL)
107#define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH)
108#define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL)
109#define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS)
110#define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR)
111#define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY)
112
113#define rpc_mismatch cpu_to_be32(RPC_MISMATCH)
114#define rpc_auth_error cpu_to_be32(RPC_AUTH_ERROR)
115
116#define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK)
117#define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED)
118#define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
119#define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF)
120#define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
121#define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK)
122#define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
123#define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
124
125
126
127
128__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
129__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
130__be32 *xdr_encode_string(__be32 *p, const char *s);
131__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
132 unsigned int maxlen);
133__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
134__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
135
136void xdr_inline_pages(struct xdr_buf *, unsigned int,
137 struct page **, unsigned int, unsigned int);
138void xdr_terminate_string(const struct xdr_buf *, const u32);
139size_t xdr_buf_pagecount(const struct xdr_buf *buf);
140int xdr_alloc_bvec(struct xdr_buf *buf, gfp_t gfp);
141void xdr_free_bvec(struct xdr_buf *buf);
142
143static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
144{
145 return xdr_encode_opaque(p, s, len);
146}
147
148
149
150
151static inline __be32 *
152xdr_encode_hyper(__be32 *p, __u64 val)
153{
154 put_unaligned_be64(val, p);
155 return p + 2;
156}
157
158static inline __be32 *
159xdr_decode_hyper(__be32 *p, __u64 *valp)
160{
161 *valp = get_unaligned_be64(p);
162 return p + 2;
163}
164
165static inline __be32 *
166xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
167{
168 memcpy(ptr, p, len);
169 return p + XDR_QUADLEN(len);
170}
171
172static inline void xdr_netobj_dup(struct xdr_netobj *dst,
173 struct xdr_netobj *src, gfp_t gfp_mask)
174{
175 dst->data = kmemdup(src->data, src->len, gfp_mask);
176 dst->len = src->len;
177}
178
179
180
181
182static inline int
183xdr_adjust_iovec(struct kvec *iov, __be32 *p)
184{
185 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
186}
187
188
189
190
191extern void xdr_shift_buf(struct xdr_buf *, size_t);
192extern void xdr_buf_from_iov(const struct kvec *, struct xdr_buf *);
193extern int xdr_buf_subsegment(const struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
194extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
195extern int read_bytes_from_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
196extern int write_bytes_to_xdr_buf(const struct xdr_buf *, unsigned int, void *, unsigned int);
197
198extern int xdr_encode_word(const struct xdr_buf *, unsigned int, u32);
199extern int xdr_decode_word(const struct xdr_buf *, unsigned int, u32 *);
200
201struct xdr_array2_desc;
202typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
203struct xdr_array2_desc {
204 unsigned int elem_size;
205 unsigned int array_len;
206 unsigned int array_maxlen;
207 xdr_xcode_elem_t xcode;
208};
209
210extern int xdr_decode_array2(const struct xdr_buf *buf, unsigned int base,
211 struct xdr_array2_desc *desc);
212extern int xdr_encode_array2(const struct xdr_buf *buf, unsigned int base,
213 struct xdr_array2_desc *desc);
214extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
215 size_t len);
216
217
218
219
220struct xdr_stream {
221 __be32 *p;
222 struct xdr_buf *buf;
223
224 __be32 *end;
225 struct kvec *iov;
226 struct kvec scratch;
227 struct page **page_ptr;
228 unsigned int nwords;
229
230 struct rpc_rqst *rqst;
231};
232
233
234
235
236typedef void (*kxdreproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
237 const void *obj);
238typedef int (*kxdrdproc_t)(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
239 void *obj);
240
241extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf,
242 __be32 *p, struct rpc_rqst *rqst);
243extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
244extern int xdr_reserve_space_vec(struct xdr_stream *xdr, struct kvec *vec,
245 size_t nbytes);
246extern void xdr_commit_encode(struct xdr_stream *xdr);
247extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
248extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
249extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
250 unsigned int base, unsigned int len);
251extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
252extern unsigned int xdr_page_pos(const struct xdr_stream *xdr);
253extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf,
254 __be32 *p, struct rpc_rqst *rqst);
255extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
256 struct page **pages, unsigned int len);
257extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
258extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
259extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
260extern int xdr_process_buf(const struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
261extern unsigned int xdr_align_data(struct xdr_stream *, unsigned int offset, unsigned int length);
262extern unsigned int xdr_expand_hole(struct xdr_stream *, unsigned int offset, unsigned int length);
263extern bool xdr_stream_subsegment(struct xdr_stream *xdr, struct xdr_buf *subbuf,
264 unsigned int len);
265
266
267
268
269
270
271
272
273
274
275
276
277static inline void
278xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen)
279{
280 xdr->scratch.iov_base = buf;
281 xdr->scratch.iov_len = buflen;
282}
283
284
285
286
287
288
289
290
291static inline void
292xdr_set_scratch_page(struct xdr_stream *xdr, struct page *page)
293{
294 xdr_set_scratch_buffer(xdr, page_address(page), PAGE_SIZE);
295}
296
297
298
299
300
301
302
303static inline void
304xdr_reset_scratch_buffer(struct xdr_stream *xdr)
305{
306 xdr_set_scratch_buffer(xdr, NULL, 0);
307}
308
309
310
311
312
313
314
315
316static inline size_t
317xdr_stream_remaining(const struct xdr_stream *xdr)
318{
319 return xdr->nwords << 2;
320}
321
322ssize_t xdr_stream_decode_opaque(struct xdr_stream *xdr, void *ptr,
323 size_t size);
324ssize_t xdr_stream_decode_opaque_dup(struct xdr_stream *xdr, void **ptr,
325 size_t maxlen, gfp_t gfp_flags);
326ssize_t xdr_stream_decode_string(struct xdr_stream *xdr, char *str,
327 size_t size);
328ssize_t xdr_stream_decode_string_dup(struct xdr_stream *xdr, char **str,
329 size_t maxlen, gfp_t gfp_flags);
330
331
332
333
334
335
336
337static inline size_t
338xdr_align_size(size_t n)
339{
340 const size_t mask = XDR_UNIT - 1;
341
342 return (n + mask) & ~mask;
343}
344
345
346
347
348
349
350
351
352
353
354
355static inline size_t xdr_pad_size(size_t n)
356{
357 return xdr_align_size(n) - n;
358}
359
360
361
362
363
364
365
366
367
368static inline ssize_t xdr_stream_encode_item_present(struct xdr_stream *xdr)
369{
370 const size_t len = XDR_UNIT;
371 __be32 *p = xdr_reserve_space(xdr, len);
372
373 if (unlikely(!p))
374 return -EMSGSIZE;
375 *p = xdr_one;
376 return len;
377}
378
379
380
381
382
383
384
385
386
387static inline int xdr_stream_encode_item_absent(struct xdr_stream *xdr)
388{
389 const size_t len = XDR_UNIT;
390 __be32 *p = xdr_reserve_space(xdr, len);
391
392 if (unlikely(!p))
393 return -EMSGSIZE;
394 *p = xdr_zero;
395 return len;
396}
397
398
399
400
401
402
403
404
405
406static inline __be32 *xdr_encode_bool(__be32 *p, u32 n)
407{
408 *p = n ? xdr_one : xdr_zero;
409 return p++;
410}
411
412
413
414
415
416
417
418
419
420
421static inline int xdr_stream_encode_bool(struct xdr_stream *xdr, __u32 n)
422{
423 const size_t len = XDR_UNIT;
424 __be32 *p = xdr_reserve_space(xdr, len);
425
426 if (unlikely(!p))
427 return -EMSGSIZE;
428 xdr_encode_bool(p, n);
429 return len;
430}
431
432
433
434
435
436
437
438
439
440
441static inline ssize_t
442xdr_stream_encode_u32(struct xdr_stream *xdr, __u32 n)
443{
444 const size_t len = sizeof(n);
445 __be32 *p = xdr_reserve_space(xdr, len);
446
447 if (unlikely(!p))
448 return -EMSGSIZE;
449 *p = cpu_to_be32(n);
450 return len;
451}
452
453
454
455
456
457
458
459
460
461
462static inline ssize_t
463xdr_stream_encode_u64(struct xdr_stream *xdr, __u64 n)
464{
465 const size_t len = sizeof(n);
466 __be32 *p = xdr_reserve_space(xdr, len);
467
468 if (unlikely(!p))
469 return -EMSGSIZE;
470 xdr_encode_hyper(p, n);
471 return len;
472}
473
474
475
476
477
478
479
480
481
482
483
484static inline ssize_t
485xdr_stream_encode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t len)
486{
487 size_t count = sizeof(__u32) + xdr_align_size(len);
488 __be32 *p = xdr_reserve_space(xdr, count);
489
490 if (unlikely(!p)) {
491 *ptr = NULL;
492 return -EMSGSIZE;
493 }
494 xdr_encode_opaque(p, NULL, len);
495 *ptr = ++p;
496 return count;
497}
498
499
500
501
502
503
504
505
506
507
508
509static inline ssize_t
510xdr_stream_encode_opaque_fixed(struct xdr_stream *xdr, const void *ptr, size_t len)
511{
512 __be32 *p = xdr_reserve_space(xdr, len);
513
514 if (unlikely(!p))
515 return -EMSGSIZE;
516 xdr_encode_opaque_fixed(p, ptr, len);
517 return xdr_align_size(len);
518}
519
520
521
522
523
524
525
526
527
528
529
530static inline ssize_t
531xdr_stream_encode_opaque(struct xdr_stream *xdr, const void *ptr, size_t len)
532{
533 size_t count = sizeof(__u32) + xdr_align_size(len);
534 __be32 *p = xdr_reserve_space(xdr, count);
535
536 if (unlikely(!p))
537 return -EMSGSIZE;
538 xdr_encode_opaque(p, ptr, len);
539 return count;
540}
541
542
543
544
545
546
547
548
549
550
551
552static inline ssize_t
553xdr_stream_encode_uint32_array(struct xdr_stream *xdr,
554 const __u32 *array, size_t array_size)
555{
556 ssize_t ret = (array_size+1) * sizeof(__u32);
557 __be32 *p = xdr_reserve_space(xdr, ret);
558
559 if (unlikely(!p))
560 return -EMSGSIZE;
561 *p++ = cpu_to_be32(array_size);
562 for (; array_size > 0; p++, array++, array_size--)
563 *p = cpu_to_be32p(array);
564 return ret;
565}
566
567
568
569
570
571
572
573
574
575static inline bool xdr_item_is_absent(const __be32 *p)
576{
577 return *p == xdr_zero;
578}
579
580
581
582
583
584
585
586
587
588static inline bool xdr_item_is_present(const __be32 *p)
589{
590 return *p != xdr_zero;
591}
592
593
594
595
596
597
598
599
600
601
602static inline ssize_t
603xdr_stream_decode_bool(struct xdr_stream *xdr, __u32 *ptr)
604{
605 const size_t count = sizeof(*ptr);
606 __be32 *p = xdr_inline_decode(xdr, count);
607
608 if (unlikely(!p))
609 return -EBADMSG;
610 *ptr = (*p != xdr_zero);
611 return 0;
612}
613
614
615
616
617
618
619
620
621
622
623static inline ssize_t
624xdr_stream_decode_u32(struct xdr_stream *xdr, __u32 *ptr)
625{
626 const size_t count = sizeof(*ptr);
627 __be32 *p = xdr_inline_decode(xdr, count);
628
629 if (unlikely(!p))
630 return -EBADMSG;
631 *ptr = be32_to_cpup(p);
632 return 0;
633}
634
635
636
637
638
639
640
641
642
643
644static inline ssize_t
645xdr_stream_decode_u64(struct xdr_stream *xdr, __u64 *ptr)
646{
647 const size_t count = sizeof(*ptr);
648 __be32 *p = xdr_inline_decode(xdr, count);
649
650 if (unlikely(!p))
651 return -EBADMSG;
652 xdr_decode_hyper(p, ptr);
653 return 0;
654}
655
656
657
658
659
660
661
662
663
664
665
666static inline ssize_t
667xdr_stream_decode_opaque_fixed(struct xdr_stream *xdr, void *ptr, size_t len)
668{
669 __be32 *p = xdr_inline_decode(xdr, len);
670
671 if (unlikely(!p))
672 return -EBADMSG;
673 xdr_decode_opaque_fixed(p, ptr, len);
674 return len;
675}
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693static inline ssize_t
694xdr_stream_decode_opaque_inline(struct xdr_stream *xdr, void **ptr, size_t maxlen)
695{
696 __be32 *p;
697 __u32 len;
698
699 *ptr = NULL;
700 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
701 return -EBADMSG;
702 if (len != 0) {
703 p = xdr_inline_decode(xdr, len);
704 if (unlikely(!p))
705 return -EBADMSG;
706 if (unlikely(len > maxlen))
707 return -EMSGSIZE;
708 *ptr = p;
709 }
710 return len;
711}
712
713
714
715
716
717
718
719
720
721
722
723
724static inline ssize_t
725xdr_stream_decode_uint32_array(struct xdr_stream *xdr,
726 __u32 *array, size_t array_size)
727{
728 __be32 *p;
729 __u32 len;
730 ssize_t retval;
731
732 if (unlikely(xdr_stream_decode_u32(xdr, &len) < 0))
733 return -EBADMSG;
734 p = xdr_inline_decode(xdr, len * sizeof(*p));
735 if (unlikely(!p))
736 return -EBADMSG;
737 if (array == NULL)
738 return len;
739 if (len <= array_size) {
740 if (len < array_size)
741 memset(array+len, 0, (array_size-len)*sizeof(*array));
742 array_size = len;
743 retval = len;
744 } else
745 retval = -EMSGSIZE;
746 for (; array_size > 0; p++, array++, array_size--)
747 *array = be32_to_cpup(p);
748 return retval;
749}
750
751#endif
752