blob: f5cc0898bc53c166a07f7c9cc9420e8ad2a253f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Benny Halevy97363c62009-09-23 14:36:38 -040014#include <asm/unaligned.h>
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -050015#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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
27struct 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 Dobriyand8ed0292006-09-26 22:29:38 -070037typedef int (*kxdrproc_t)(void *rqstp, __be32 *data, void *obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Basic structure for transmission/reception of a client XDR message.
41 * Features a header (for a linear buffer containing RPC headers
42 * and the data payload for short messages), and then an array of
43 * pages.
44 * The tail iovec allows you to append data after the page array. Its
45 * main interest is for appending padding to the pages in order to
46 * satisfy the int_32-alignment requirements in RFC1832.
47 *
48 * For the future, we might want to string several of these together
49 * in a list if anybody wants to make use of NFSv4 COMPOUND
50 * operations and/or has a need for scatter/gather involving pages.
51 */
52struct xdr_buf {
53 struct kvec head[1], /* RPC header + non-page data */
54 tail[1]; /* Appended after page data */
55
56 struct page ** pages; /* Array of contiguous pages */
57 unsigned int page_base, /* Start of page data */
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -040058 page_len, /* Length of page data */
59 flags; /* Flags for data disposition */
60#define XDRBUF_READ 0x01 /* target of file read */
61#define XDRBUF_WRITE 0x02 /* source of file write */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 unsigned int buflen, /* Total length of storage buffer */
64 len; /* Length of XDR encoded message */
65
66};
67
68/*
69 * pre-xdr'ed macros.
70 */
71
Harvey Harrison77f18f52009-02-11 17:16:58 -080072#define xdr_zero cpu_to_be32(0)
73#define xdr_one cpu_to_be32(1)
74#define xdr_two cpu_to_be32(2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Harvey Harrison77f18f52009-02-11 17:16:58 -080076#define rpc_success cpu_to_be32(RPC_SUCCESS)
77#define rpc_prog_unavail cpu_to_be32(RPC_PROG_UNAVAIL)
78#define rpc_prog_mismatch cpu_to_be32(RPC_PROG_MISMATCH)
79#define rpc_proc_unavail cpu_to_be32(RPC_PROC_UNAVAIL)
80#define rpc_garbage_args cpu_to_be32(RPC_GARBAGE_ARGS)
81#define rpc_system_err cpu_to_be32(RPC_SYSTEM_ERR)
82#define rpc_drop_reply cpu_to_be32(RPC_DROP_REPLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Harvey Harrison77f18f52009-02-11 17:16:58 -080084#define rpc_auth_ok cpu_to_be32(RPC_AUTH_OK)
85#define rpc_autherr_badcred cpu_to_be32(RPC_AUTH_BADCRED)
86#define rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
87#define rpc_autherr_badverf cpu_to_be32(RPC_AUTH_BADVERF)
88#define rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
89#define rpc_autherr_tooweak cpu_to_be32(RPC_AUTH_TOOWEAK)
90#define rpcsec_gsserr_credproblem cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
91#define rpcsec_gsserr_ctxproblem cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
92#define rpc_autherr_oldseqnum cpu_to_be32(101)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * Miscellaneous XDR helper functions
96 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070097__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
98__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
99__be32 *xdr_encode_string(__be32 *p, const char *s);
Chuck Levere5cff482007-11-01 16:56:47 -0400100__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
101 unsigned int maxlen);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700102__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
103__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105void xdr_encode_pages(struct xdr_buf *, struct page **, unsigned int,
106 unsigned int);
107void xdr_inline_pages(struct xdr_buf *, unsigned int,
108 struct page **, unsigned int, unsigned int);
109
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700110static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 return xdr_encode_opaque(p, s, len);
113}
114
115/*
116 * Decode 64bit quantities (NFSv3 support)
117 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700118static inline __be32 *
119xdr_encode_hyper(__be32 *p, __u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Benny Halevy97363c62009-09-23 14:36:38 -0400121 put_unaligned_be64(val, p);
Benny Halevy9f162d22009-08-14 17:18:44 +0300122 return p + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700125static inline __be32 *
126xdr_decode_hyper(__be32 *p, __u64 *valp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Benny Halevy97363c62009-09-23 14:36:38 -0400128 *valp = get_unaligned_be64(p);
Benny Halevy98866b52009-08-14 17:18:49 +0300129 return p + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/*
133 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
134 */
135static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700136xdr_adjust_iovec(struct kvec *iov, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
139}
140
141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * XDR buffer helper functions
143 */
144extern void xdr_shift_buf(struct xdr_buf *, size_t);
145extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
Trond Myklebust1e789572006-08-31 15:09:19 -0400146extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
147extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
148extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
149extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * Helper structure for copying from an sk_buff.
153 */
Chuck Leverdd456472006-12-05 16:35:44 -0500154struct xdr_skb_reader {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct sk_buff *skb;
156 unsigned int offset;
157 size_t count;
Al Viro44bb9362006-11-14 21:36:14 -0800158 __wsum csum;
Chuck Leverdd456472006-12-05 16:35:44 -0500159};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Chuck Leverdd456472006-12-05 16:35:44 -0500161typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Chuck Leverdd456472006-12-05 16:35:44 -0500163size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len);
Chuck Lever094bb202005-08-11 16:25:20 -0400164extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
Trond Myklebust7e06b532005-06-22 17:16:24 +0000165extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
Chuck Leverdd456472006-12-05 16:35:44 -0500166 struct xdr_skb_reader *, xdr_skb_read_actor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Trond Myklebust1e789572006-08-31 15:09:19 -0400168extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
169extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000170
171struct xdr_array2_desc;
172typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
173struct xdr_array2_desc {
174 unsigned int elem_size;
175 unsigned int array_len;
Trond Myklebust58fcb8d2005-08-10 18:15:12 -0400176 unsigned int array_maxlen;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000177 xdr_xcode_elem_t xcode;
178};
179
180extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
181 struct xdr_array2_desc *desc);
182extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
183 struct xdr_array2_desc *desc);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * Provide some simple tools for XDR buffer overflow-checking etc.
187 */
188struct xdr_stream {
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700189 __be32 *p; /* start of available buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct xdr_buf *buf; /* XDR buffer to read/write */
191
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700192 __be32 *end; /* end of available buffer space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct kvec *iov; /* pointer to the current kvec */
194};
195
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700196extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
197extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
199 unsigned int base, unsigned int len);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700200extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
201extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202extern void xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400203extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -0500204extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206#endif /* __KERNEL__ */
207
208#endif /* _SUNRPC_XDR_H_ */