xdr.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * XDR standard data types and function declarations
  3. *
  4. * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
  5. *
  6. * Based on:
  7. * RFC 4506 "XDR: External Data Representation Standard", May 2006
  8. */
  9. #ifndef _SUNRPC_XDR_H_
  10. #define _SUNRPC_XDR_H_
  11. #ifdef __KERNEL__
  12. #include <linux/uio.h>
  13. #include <asm/byteorder.h>
  14. #include <asm/unaligned.h>
  15. #include <linux/scatterlist.h>
  16. /*
  17. * Buffer adjustment
  18. */
  19. #define XDR_QUADLEN(l) (((l) + 3) >> 2)
  20. /*
  21. * Generic opaque `network object.' At the kernel level, this type
  22. * is used only by lockd.
  23. */
  24. #define XDR_MAX_NETOBJ 1024
  25. struct xdr_netobj {
  26. unsigned int len;
  27. u8 * data;
  28. };
  29. /*
  30. * This is the generic XDR function. rqstp is either a rpc_rqst (client
  31. * side) or svc_rqst pointer (server side).
  32. * Encode functions always assume there's enough room in the buffer.
  33. */
  34. typedef int (*kxdrproc_t)(void *rqstp, __be32 *data, void *obj);
  35. /*
  36. * Basic structure for transmission/reception of a client XDR message.
  37. * Features a header (for a linear buffer containing RPC headers
  38. * and the data payload for short messages), and then an array of
  39. * pages.
  40. * The tail iovec allows you to append data after the page array. Its
  41. * main interest is for appending padding to the pages in order to
  42. * satisfy the int_32-alignment requirements in RFC1832.
  43. *
  44. * For the future, we might want to string several of these together
  45. * in a list if anybody wants to make use of NFSv4 COMPOUND
  46. * operations and/or has a need for scatter/gather involving pages.
  47. */
  48. struct xdr_buf {
  49. struct kvec head[1], /* RPC header + non-page data */
  50. tail[1]; /* Appended after page data */
  51. struct page ** pages; /* Array of contiguous pages */
  52. unsigned int page_base, /* Start of page data */
  53. page_len, /* Length of page data */
  54. flags; /* Flags for data disposition */
  55. #define XDRBUF_READ 0x01 /* target of file read */
  56. #define XDRBUF_WRITE 0x02 /* source of file write */
  57. unsigned int buflen, /* Total length of storage buffer */
  58. len; /* Length of XDR encoded message */
  59. };
  60. /*
  61. * pre-xdr'ed macros.
  62. */
  63. #define xdr_zero cpu_to_be32(0)
  64. #define xdr_one cpu_to_be32(1)
  65. #define xdr_two cpu_to_be32(2)
  66. #define rpc_success cpu_to_be32(RPC_SUCCESS)
  67. #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL)
  68. #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH)
  69. #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL)
  70. #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS)
  71. #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR)
  72. #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY)
  73. #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK)
  74. #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED)
  75. #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
  76. #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF)
  77. #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
  78. #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK)
  79. #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
  80. #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
  81. #define rpc_autherr_oldseqnum cpu_to_be32(101)
  82. /*
  83. * Miscellaneous XDR helper functions
  84. */
  85. __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
  86. __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
  87. __be32 *xdr_encode_string(__be32 *p, const char *s);
  88. __be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
  89. unsigned int maxlen);
  90. __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
  91. __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
  92. void xdr_encode_pages(struct xdr_buf *, struct page **, unsigned int,
  93. unsigned int);
  94. void xdr_inline_pages(struct xdr_buf *, unsigned int,
  95. struct page **, unsigned int, unsigned int);
  96. static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
  97. {
  98. return xdr_encode_opaque(p, s, len);
  99. }
  100. /*
  101. * Decode 64bit quantities (NFSv3 support)
  102. */
  103. static inline __be32 *
  104. xdr_encode_hyper(__be32 *p, __u64 val)
  105. {
  106. put_unaligned_be64(val, p);
  107. return p + 2;
  108. }
  109. static inline __be32 *
  110. xdr_decode_hyper(__be32 *p, __u64 *valp)
  111. {
  112. *valp = get_unaligned_be64(p);
  113. return p + 2;
  114. }
  115. /*
  116. * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
  117. */
  118. static inline int
  119. xdr_adjust_iovec(struct kvec *iov, __be32 *p)
  120. {
  121. return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
  122. }
  123. /*
  124. * XDR buffer helper functions
  125. */
  126. extern void xdr_shift_buf(struct xdr_buf *, size_t);
  127. extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
  128. extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
  129. extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
  130. extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
  131. extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
  132. /*
  133. * Helper structure for copying from an sk_buff.
  134. */
  135. struct xdr_skb_reader {
  136. struct sk_buff *skb;
  137. unsigned int offset;
  138. size_t count;
  139. __wsum csum;
  140. };
  141. typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len);
  142. size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len);
  143. extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
  144. extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
  145. struct xdr_skb_reader *, xdr_skb_read_actor);
  146. extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
  147. extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
  148. struct xdr_array2_desc;
  149. typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
  150. struct xdr_array2_desc {
  151. unsigned int elem_size;
  152. unsigned int array_len;
  153. unsigned int array_maxlen;
  154. xdr_xcode_elem_t xcode;
  155. };
  156. extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  157. struct xdr_array2_desc *desc);
  158. extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  159. struct xdr_array2_desc *desc);
  160. /*
  161. * Provide some simple tools for XDR buffer overflow-checking etc.
  162. */
  163. struct xdr_stream {
  164. __be32 *p; /* start of available buffer */
  165. struct xdr_buf *buf; /* XDR buffer to read/write */
  166. __be32 *end; /* end of available buffer space */
  167. struct kvec *iov; /* pointer to the current kvec */
  168. };
  169. extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
  170. extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
  171. extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
  172. unsigned int base, unsigned int len);
  173. extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
  174. extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
  175. extern void xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
  176. extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
  177. extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
  178. #endif /* __KERNEL__ */
  179. #endif /* _SUNRPC_XDR_H_ */