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