xdr.h 7.3 KB

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