xdr.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 legacy generic XDR function. rqstp is either a rpc_rqst
  31. * (client 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 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_inline_pages(struct xdr_buf *, unsigned int,
  93. struct page **, unsigned int, unsigned int);
  94. void xdr_terminate_string(struct xdr_buf *, const u32);
  95. static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
  96. {
  97. return xdr_encode_opaque(p, s, len);
  98. }
  99. /*
  100. * Decode 64bit quantities (NFSv3 support)
  101. */
  102. static inline __be32 *
  103. xdr_encode_hyper(__be32 *p, __u64 val)
  104. {
  105. put_unaligned_be64(val, p);
  106. return p + 2;
  107. }
  108. static inline __be32 *
  109. xdr_decode_hyper(__be32 *p, __u64 *valp)
  110. {
  111. *valp = get_unaligned_be64(p);
  112. return p + 2;
  113. }
  114. static inline __be32 *
  115. xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
  116. {
  117. memcpy(ptr, p, len);
  118. return p + XDR_QUADLEN(len);
  119. }
  120. /*
  121. * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
  122. */
  123. static inline int
  124. xdr_adjust_iovec(struct kvec *iov, __be32 *p)
  125. {
  126. return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
  127. }
  128. /*
  129. * XDR buffer helper functions
  130. */
  131. extern void xdr_shift_buf(struct xdr_buf *, size_t);
  132. extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
  133. extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
  134. extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
  135. extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
  136. extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
  137. extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
  138. /*
  139. * Helper structure for copying from an sk_buff.
  140. */
  141. struct xdr_skb_reader {
  142. struct sk_buff *skb;
  143. unsigned int offset;
  144. size_t count;
  145. __wsum csum;
  146. };
  147. typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len);
  148. size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len);
  149. extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
  150. extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
  151. struct xdr_skb_reader *, xdr_skb_read_actor);
  152. extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
  153. extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
  154. struct xdr_array2_desc;
  155. typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
  156. struct xdr_array2_desc {
  157. unsigned int elem_size;
  158. unsigned int array_len;
  159. unsigned int array_maxlen;
  160. xdr_xcode_elem_t xcode;
  161. };
  162. extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
  163. struct xdr_array2_desc *desc);
  164. extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
  165. struct xdr_array2_desc *desc);
  166. extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
  167. size_t len);
  168. /*
  169. * Provide some simple tools for XDR buffer overflow-checking etc.
  170. */
  171. struct xdr_stream {
  172. __be32 *p; /* start of available buffer */
  173. struct xdr_buf *buf; /* XDR buffer to read/write */
  174. __be32 *end; /* end of available buffer space */
  175. struct kvec *iov; /* pointer to the current kvec */
  176. struct kvec scratch; /* Scratch buffer */
  177. struct page **page_ptr; /* pointer to the current page */
  178. unsigned int nwords; /* Remaining decode buffer length */
  179. };
  180. /*
  181. * These are the xdr_stream style generic XDR encode and decode functions.
  182. */
  183. typedef void (*kxdreproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj);
  184. typedef int (*kxdrdproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj);
  185. extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
  186. extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
  187. extern void xdr_commit_encode(struct xdr_stream *xdr);
  188. extern void xdr_truncate_encode(struct xdr_stream *xdr, size_t len);
  189. extern int xdr_restrict_buflen(struct xdr_stream *xdr, int newbuflen);
  190. extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
  191. unsigned int base, unsigned int len);
  192. extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
  193. extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
  194. extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
  195. struct page **pages, unsigned int len);
  196. extern void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen);
  197. extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
  198. extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
  199. extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
  200. extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
  201. #endif /* __KERNEL__ */
  202. #endif /* _SUNRPC_XDR_H_ */