Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Chuck Lever | 9605a06 | 2010-05-07 13:33:30 -0400 | [diff] [blame] | 2 | * XDR standard data types and function declarations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de> |
Chuck Lever | 9605a06 | 2010-05-07 13:33:30 -0400 | [diff] [blame] | 5 | * |
| 6 | * Based on: |
| 7 | * RFC 4506 "XDR: External Data Representation Standard", May 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef _SUNRPC_XDR_H_ |
| 11 | #define _SUNRPC_XDR_H_ |
| 12 | |
| 13 | #ifdef __KERNEL__ |
| 14 | |
| 15 | #include <linux/uio.h> |
| 16 | #include <asm/byteorder.h> |
Benny Halevy | 97363c6 | 2009-09-23 14:36:38 -0400 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 18 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Buffer adjustment |
| 22 | */ |
| 23 | #define XDR_QUADLEN(l) (((l) + 3) >> 2) |
| 24 | |
| 25 | /* |
| 26 | * Generic opaque `network object.' At the kernel level, this type |
| 27 | * is used only by lockd. |
| 28 | */ |
| 29 | #define XDR_MAX_NETOBJ 1024 |
| 30 | struct xdr_netobj { |
| 31 | unsigned int len; |
| 32 | u8 * data; |
| 33 | }; |
| 34 | |
| 35 | /* |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 36 | * This is the legacy generic XDR function. rqstp is either a rpc_rqst |
| 37 | * (client side) or svc_rqst pointer (server side). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * Encode functions always assume there's enough room in the buffer. |
| 39 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 40 | typedef int (*kxdrproc_t)(void *rqstp, __be32 *data, void *obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Basic structure for transmission/reception of a client XDR message. |
| 44 | * Features a header (for a linear buffer containing RPC headers |
| 45 | * and the data payload for short messages), and then an array of |
| 46 | * pages. |
| 47 | * The tail iovec allows you to append data after the page array. Its |
| 48 | * main interest is for appending padding to the pages in order to |
| 49 | * satisfy the int_32-alignment requirements in RFC1832. |
| 50 | * |
| 51 | * For the future, we might want to string several of these together |
| 52 | * in a list if anybody wants to make use of NFSv4 COMPOUND |
| 53 | * operations and/or has a need for scatter/gather involving pages. |
| 54 | */ |
| 55 | struct xdr_buf { |
| 56 | struct kvec head[1], /* RPC header + non-page data */ |
| 57 | tail[1]; /* Appended after page data */ |
| 58 | |
| 59 | struct page ** pages; /* Array of contiguous pages */ |
| 60 | unsigned int page_base, /* Start of page data */ |
\"Talpey, Thomas\ | 4f22ccc | 2007-09-10 13:44:58 -0400 | [diff] [blame] | 61 | page_len, /* Length of page data */ |
| 62 | flags; /* Flags for data disposition */ |
| 63 | #define XDRBUF_READ 0x01 /* target of file read */ |
| 64 | #define XDRBUF_WRITE 0x02 /* source of file write */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | unsigned int buflen, /* Total length of storage buffer */ |
| 67 | len; /* Length of XDR encoded message */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * pre-xdr'ed macros. |
| 72 | */ |
| 73 | |
Harvey Harrison | 77f18f5 | 2009-02-11 17:16:58 -0800 | [diff] [blame] | 74 | #define xdr_zero cpu_to_be32(0) |
| 75 | #define xdr_one cpu_to_be32(1) |
| 76 | #define xdr_two cpu_to_be32(2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Harvey Harrison | 77f18f5 | 2009-02-11 17:16:58 -0800 | [diff] [blame] | 78 | #define rpc_success cpu_to_be32(RPC_SUCCESS) |
| 79 | #define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL) |
| 80 | #define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH) |
| 81 | #define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL) |
| 82 | #define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS) |
| 83 | #define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR) |
| 84 | #define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Harvey Harrison | 77f18f5 | 2009-02-11 17:16:58 -0800 | [diff] [blame] | 86 | #define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK) |
| 87 | #define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED) |
| 88 | #define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED) |
| 89 | #define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF) |
| 90 | #define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF) |
| 91 | #define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK) |
| 92 | #define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM) |
| 93 | #define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM) |
| 94 | #define rpc_autherr_oldseqnum cpu_to_be32(101) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Miscellaneous XDR helper functions |
| 98 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 99 | __be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len); |
| 100 | __be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len); |
| 101 | __be32 *xdr_encode_string(__be32 *p, const char *s); |
Chuck Lever | e5cff48 | 2007-11-01 16:56:47 -0400 | [diff] [blame] | 102 | __be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp, |
| 103 | unsigned int maxlen); |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 104 | __be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *); |
| 105 | __be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | void xdr_inline_pages(struct xdr_buf *, unsigned int, |
| 108 | struct page **, unsigned int, unsigned int); |
Chuck Lever | b4687da | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 109 | void xdr_terminate_string(struct xdr_buf *, const u32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 111 | static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | return xdr_encode_opaque(p, s, len); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Decode 64bit quantities (NFSv3 support) |
| 118 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 119 | static inline __be32 * |
| 120 | xdr_encode_hyper(__be32 *p, __u64 val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Benny Halevy | 97363c6 | 2009-09-23 14:36:38 -0400 | [diff] [blame] | 122 | put_unaligned_be64(val, p); |
Benny Halevy | 9f162d2 | 2009-08-14 17:18:44 +0300 | [diff] [blame] | 123 | return p + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 126 | static inline __be32 * |
| 127 | xdr_decode_hyper(__be32 *p, __u64 *valp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Benny Halevy | 97363c6 | 2009-09-23 14:36:38 -0400 | [diff] [blame] | 129 | *valp = get_unaligned_be64(p); |
Benny Halevy | 98866b5 | 2009-08-14 17:18:49 +0300 | [diff] [blame] | 130 | return p + 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Benny Halevy | 35b61e6 | 2010-10-20 00:17:54 -0400 | [diff] [blame] | 133 | static inline __be32 * |
| 134 | xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len) |
| 135 | { |
| 136 | memcpy(ptr, p, len); |
| 137 | return p + XDR_QUADLEN(len); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
| 141 | * Adjust kvec to reflect end of xdr'ed data (RPC client XDR) |
| 142 | */ |
| 143 | static inline int |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 144 | xdr_adjust_iovec(struct kvec *iov, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base); |
| 147 | } |
| 148 | |
| 149 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * XDR buffer helper functions |
| 151 | */ |
| 152 | extern void xdr_shift_buf(struct xdr_buf *, size_t); |
| 153 | extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *); |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 154 | extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int); |
| 155 | extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int); |
| 156 | extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int); |
| 157 | extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * Helper structure for copying from an sk_buff. |
| 161 | */ |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 162 | struct xdr_skb_reader { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | struct sk_buff *skb; |
| 164 | unsigned int offset; |
| 165 | size_t count; |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 166 | __wsum csum; |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 167 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 169 | typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 171 | size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len); |
Chuck Lever | 094bb20 | 2005-08-11 16:25:20 -0400 | [diff] [blame] | 172 | extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *); |
Trond Myklebust | 7e06b53 | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 173 | extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int, |
Chuck Lever | dd45647 | 2006-12-05 16:35:44 -0500 | [diff] [blame] | 174 | struct xdr_skb_reader *, xdr_skb_read_actor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Trond Myklebust | 1e78957 | 2006-08-31 15:09:19 -0400 | [diff] [blame] | 176 | extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32); |
| 177 | extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 178 | |
| 179 | struct xdr_array2_desc; |
| 180 | typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem); |
| 181 | struct xdr_array2_desc { |
| 182 | unsigned int elem_size; |
| 183 | unsigned int array_len; |
Trond Myklebust | 58fcb8d | 2005-08-10 18:15:12 -0400 | [diff] [blame] | 184 | unsigned int array_maxlen; |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 185 | xdr_xcode_elem_t xcode; |
| 186 | }; |
| 187 | |
| 188 | extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base, |
Chuck Lever | 9605a06 | 2010-05-07 13:33:30 -0400 | [diff] [blame] | 189 | struct xdr_array2_desc *desc); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 190 | extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base, |
| 191 | struct xdr_array2_desc *desc); |
Andy Adamson | bf118a3 | 2011-12-07 11:55:27 -0500 | [diff] [blame] | 192 | extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase, |
| 193 | size_t len); |
Andreas Gruenbacher | bd8100e | 2005-06-22 17:16:24 +0000 | [diff] [blame] | 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Provide some simple tools for XDR buffer overflow-checking etc. |
| 197 | */ |
| 198 | struct xdr_stream { |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 199 | __be32 *p; /* start of available buffer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct xdr_buf *buf; /* XDR buffer to read/write */ |
| 201 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 202 | __be32 *end; /* end of available buffer space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct kvec *iov; /* pointer to the current kvec */ |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 204 | struct kvec scratch; /* Scratch buffer */ |
| 205 | struct page **page_ptr; /* pointer to the current page */ |
Trond Myklebust | bfeea1d | 2012-06-20 09:58:35 -0400 | [diff] [blame] | 206 | unsigned int nwords; /* Remaining decode buffer length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 209 | /* |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 210 | * These are the xdr_stream style generic XDR encode and decode functions. |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 211 | */ |
| 212 | typedef void (*kxdreproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj); |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 213 | typedef int (*kxdrdproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj); |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 214 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 215 | extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p); |
| 216 | extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, |
| 218 | unsigned int base, unsigned int len); |
Trond Myklebust | 4517d52 | 2012-06-21 17:14:46 -0400 | [diff] [blame] | 219 | extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr); |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 220 | extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p); |
Benny Halevy | f7da7a1 | 2011-05-19 14:16:47 -0400 | [diff] [blame] | 221 | extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf, |
| 222 | struct page **pages, unsigned int len); |
Trond Myklebust | 6650239 | 2011-01-08 17:45:38 -0500 | [diff] [blame] | 223 | extern void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen); |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 224 | extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes); |
Trond Myklebust | c337d36 | 2012-06-21 17:05:37 -0400 | [diff] [blame] | 225 | extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len); |
Trond Myklebust | 8b23ea7 | 2006-06-09 09:34:21 -0400 | [diff] [blame] | 226 | extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len); |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 227 | extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | #endif /* __KERNEL__ */ |
| 230 | |
| 231 | #endif /* _SUNRPC_XDR_H_ */ |