blob: 4d17846cd78a1edd9ab5444ebdda062dc1ced2ea [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>
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -050014#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * Buffer adjustment
18 */
19#define XDR_QUADLEN(l) (((l) + 3) >> 2)
20
21/*
22 * Generic opaque `network object.' At the kernel level, this type
23 * is used only by lockd.
24 */
25#define XDR_MAX_NETOBJ 1024
26struct xdr_netobj {
27 unsigned int len;
28 u8 * data;
29};
30
31/*
32 * This is the generic XDR function. rqstp is either a rpc_rqst (client
33 * side) or svc_rqst pointer (server side).
34 * Encode functions always assume there's enough room in the buffer.
35 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070036typedef int (*kxdrproc_t)(void *rqstp, __be32 *data, void *obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Basic structure for transmission/reception of a client XDR message.
40 * Features a header (for a linear buffer containing RPC headers
41 * and the data payload for short messages), and then an array of
42 * pages.
43 * The tail iovec allows you to append data after the page array. Its
44 * main interest is for appending padding to the pages in order to
45 * satisfy the int_32-alignment requirements in RFC1832.
46 *
47 * For the future, we might want to string several of these together
48 * in a list if anybody wants to make use of NFSv4 COMPOUND
49 * operations and/or has a need for scatter/gather involving pages.
50 */
51struct xdr_buf {
52 struct kvec head[1], /* RPC header + non-page data */
53 tail[1]; /* Appended after page data */
54
55 struct page ** pages; /* Array of contiguous pages */
56 unsigned int page_base, /* Start of page data */
57 page_len; /* Length of page data */
58
59 unsigned int buflen, /* Total length of storage buffer */
60 len; /* Length of XDR encoded message */
61
62};
63
64/*
65 * pre-xdr'ed macros.
66 */
67
68#define xdr_zero __constant_htonl(0)
69#define xdr_one __constant_htonl(1)
70#define xdr_two __constant_htonl(2)
71
72#define rpc_success __constant_htonl(RPC_SUCCESS)
73#define rpc_prog_unavail __constant_htonl(RPC_PROG_UNAVAIL)
74#define rpc_prog_mismatch __constant_htonl(RPC_PROG_MISMATCH)
75#define rpc_proc_unavail __constant_htonl(RPC_PROC_UNAVAIL)
76#define rpc_garbage_args __constant_htonl(RPC_GARBAGE_ARGS)
77#define rpc_system_err __constant_htonl(RPC_SYSTEM_ERR)
NeilBrownd343fce2006-10-17 00:10:18 -070078#define rpc_drop_reply __constant_htonl(RPC_DROP_REPLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define rpc_auth_ok __constant_htonl(RPC_AUTH_OK)
81#define rpc_autherr_badcred __constant_htonl(RPC_AUTH_BADCRED)
82#define rpc_autherr_rejectedcred __constant_htonl(RPC_AUTH_REJECTEDCRED)
83#define rpc_autherr_badverf __constant_htonl(RPC_AUTH_BADVERF)
84#define rpc_autherr_rejectedverf __constant_htonl(RPC_AUTH_REJECTEDVERF)
85#define rpc_autherr_tooweak __constant_htonl(RPC_AUTH_TOOWEAK)
86#define rpcsec_gsserr_credproblem __constant_htonl(RPCSEC_GSS_CREDPROBLEM)
87#define rpcsec_gsserr_ctxproblem __constant_htonl(RPCSEC_GSS_CTXPROBLEM)
88#define rpc_autherr_oldseqnum __constant_htonl(101)
89
90/*
91 * Miscellaneous XDR helper functions
92 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070093__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
94__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
95__be32 *xdr_encode_string(__be32 *p, const char *s);
96__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, int *lenp, int maxlen);
97__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
98__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100void xdr_encode_pages(struct xdr_buf *, struct page **, unsigned int,
101 unsigned int);
102void xdr_inline_pages(struct xdr_buf *, unsigned int,
103 struct page **, unsigned int, unsigned int);
104
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700105static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 return xdr_encode_opaque(p, s, len);
108}
109
110/*
111 * Decode 64bit quantities (NFSv3 support)
112 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700113static inline __be32 *
114xdr_encode_hyper(__be32 *p, __u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 *p++ = htonl(val >> 32);
117 *p++ = htonl(val & 0xFFFFFFFF);
118 return p;
119}
120
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700121static inline __be32 *
122xdr_decode_hyper(__be32 *p, __u64 *valp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 *valp = ((__u64) ntohl(*p++)) << 32;
125 *valp |= ntohl(*p++);
126 return p;
127}
128
129/*
130 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
131 */
132static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700133xdr_adjust_iovec(struct kvec *iov, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
136}
137
138/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * XDR buffer helper functions
140 */
141extern void xdr_shift_buf(struct xdr_buf *, size_t);
142extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
Trond Myklebust1e789572006-08-31 15:09:19 -0400143extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
144extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
145extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
146extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/*
149 * Helper structure for copying from an sk_buff.
150 */
151typedef struct {
152 struct sk_buff *skb;
153 unsigned int offset;
154 size_t count;
Al Viro44bb9362006-11-14 21:36:14 -0800155 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156} skb_reader_t;
157
158typedef size_t (*skb_read_actor_t)(skb_reader_t *desc, void *to, size_t len);
159
Chuck Lever094bb202005-08-11 16:25:20 -0400160extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
Trond Myklebust7e06b532005-06-22 17:16:24 +0000161extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 skb_reader_t *, skb_read_actor_t);
163
Trond Myklebust1e789572006-08-31 15:09:19 -0400164extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
165extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000166
167struct xdr_array2_desc;
168typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
169struct xdr_array2_desc {
170 unsigned int elem_size;
171 unsigned int array_len;
Trond Myklebust58fcb8d2005-08-10 18:15:12 -0400172 unsigned int array_maxlen;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000173 xdr_xcode_elem_t xcode;
174};
175
176extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
177 struct xdr_array2_desc *desc);
178extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
179 struct xdr_array2_desc *desc);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * Provide some simple tools for XDR buffer overflow-checking etc.
183 */
184struct xdr_stream {
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700185 __be32 *p; /* start of available buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct xdr_buf *buf; /* XDR buffer to read/write */
187
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700188 __be32 *end; /* end of available buffer space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct kvec *iov; /* pointer to the current kvec */
190};
191
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700192extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
193extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
195 unsigned int base, unsigned int len);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700196extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
197extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198extern void xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400199extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
Olga Kornievskaia37a4e6c2006-12-04 20:22:33 -0500200extern 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 -0700201
202#endif /* __KERNEL__ */
203
204#endif /* _SUNRPC_XDR_H_ */