xdr.h 6.4 KB

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