blob: ac69e55116060be1d29fb783a812d861011ac548 [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>
14
15/*
16 * Buffer adjustment
17 */
18#define XDR_QUADLEN(l) (((l) + 3) >> 2)
19
20/*
21 * Generic opaque `network object.' At the kernel level, this type
22 * is used only by lockd.
23 */
24#define XDR_MAX_NETOBJ 1024
25struct xdr_netobj {
26 unsigned int len;
27 u8 * data;
28};
29
30/*
31 * This is the generic XDR function. rqstp is either a rpc_rqst (client
32 * side) or svc_rqst pointer (server side).
33 * Encode functions always assume there's enough room in the buffer.
34 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070035typedef int (*kxdrproc_t)(void *rqstp, __be32 *data, void *obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Basic structure for transmission/reception of a client XDR message.
39 * Features a header (for a linear buffer containing RPC headers
40 * and the data payload for short messages), and then an array of
41 * pages.
42 * The tail iovec allows you to append data after the page array. Its
43 * main interest is for appending padding to the pages in order to
44 * satisfy the int_32-alignment requirements in RFC1832.
45 *
46 * For the future, we might want to string several of these together
47 * in a list if anybody wants to make use of NFSv4 COMPOUND
48 * operations and/or has a need for scatter/gather involving pages.
49 */
50struct xdr_buf {
51 struct kvec head[1], /* RPC header + non-page data */
52 tail[1]; /* Appended after page data */
53
54 struct page ** pages; /* Array of contiguous pages */
55 unsigned int page_base, /* Start of page data */
56 page_len; /* Length of page data */
57
58 unsigned int buflen, /* Total length of storage buffer */
59 len; /* Length of XDR encoded message */
60
61};
62
63/*
64 * pre-xdr'ed macros.
65 */
66
67#define xdr_zero __constant_htonl(0)
68#define xdr_one __constant_htonl(1)
69#define xdr_two __constant_htonl(2)
70
71#define rpc_success __constant_htonl(RPC_SUCCESS)
72#define rpc_prog_unavail __constant_htonl(RPC_PROG_UNAVAIL)
73#define rpc_prog_mismatch __constant_htonl(RPC_PROG_MISMATCH)
74#define rpc_proc_unavail __constant_htonl(RPC_PROC_UNAVAIL)
75#define rpc_garbage_args __constant_htonl(RPC_GARBAGE_ARGS)
76#define rpc_system_err __constant_htonl(RPC_SYSTEM_ERR)
NeilBrownd343fce2006-10-17 00:10:18 -070077#define rpc_drop_reply __constant_htonl(RPC_DROP_REPLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define rpc_auth_ok __constant_htonl(RPC_AUTH_OK)
80#define rpc_autherr_badcred __constant_htonl(RPC_AUTH_BADCRED)
81#define rpc_autherr_rejectedcred __constant_htonl(RPC_AUTH_REJECTEDCRED)
82#define rpc_autherr_badverf __constant_htonl(RPC_AUTH_BADVERF)
83#define rpc_autherr_rejectedverf __constant_htonl(RPC_AUTH_REJECTEDVERF)
84#define rpc_autherr_tooweak __constant_htonl(RPC_AUTH_TOOWEAK)
85#define rpcsec_gsserr_credproblem __constant_htonl(RPCSEC_GSS_CREDPROBLEM)
86#define rpcsec_gsserr_ctxproblem __constant_htonl(RPCSEC_GSS_CTXPROBLEM)
87#define rpc_autherr_oldseqnum __constant_htonl(101)
88
89/*
90 * Miscellaneous XDR helper functions
91 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070092__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
93__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
94__be32 *xdr_encode_string(__be32 *p, const char *s);
95__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, int *lenp, int maxlen);
96__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
97__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99void xdr_encode_pages(struct xdr_buf *, struct page **, unsigned int,
100 unsigned int);
101void xdr_inline_pages(struct xdr_buf *, unsigned int,
102 struct page **, unsigned int, unsigned int);
103
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700104static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 return xdr_encode_opaque(p, s, len);
107}
108
109/*
110 * Decode 64bit quantities (NFSv3 support)
111 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700112static inline __be32 *
113xdr_encode_hyper(__be32 *p, __u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 *p++ = htonl(val >> 32);
116 *p++ = htonl(val & 0xFFFFFFFF);
117 return p;
118}
119
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700120static inline __be32 *
121xdr_decode_hyper(__be32 *p, __u64 *valp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 *valp = ((__u64) ntohl(*p++)) << 32;
124 *valp |= ntohl(*p++);
125 return p;
126}
127
128/*
129 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
130 */
131static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700132xdr_adjust_iovec(struct kvec *iov, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
135}
136
137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * XDR buffer helper functions
139 */
140extern void xdr_shift_buf(struct xdr_buf *, size_t);
141extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
142extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, int, int);
143extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, int);
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000144extern int read_bytes_from_xdr_buf(struct xdr_buf *, int, void *, int);
145extern int write_bytes_to_xdr_buf(struct xdr_buf *, int, void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * Helper structure for copying from an sk_buff.
149 */
150typedef struct {
151 struct sk_buff *skb;
152 unsigned int offset;
153 size_t count;
154 unsigned int csum;
155} skb_reader_t;
156
157typedef size_t (*skb_read_actor_t)(skb_reader_t *desc, void *to, size_t len);
158
Chuck Lever094bb202005-08-11 16:25:20 -0400159extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
Trond Myklebust7e06b532005-06-22 17:16:24 +0000160extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 skb_reader_t *, skb_read_actor_t);
162
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000163extern int xdr_encode_word(struct xdr_buf *, int, u32);
164extern int xdr_decode_word(struct xdr_buf *, int, u32 *);
165
166struct xdr_array2_desc;
167typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
168struct xdr_array2_desc {
169 unsigned int elem_size;
170 unsigned int array_len;
Trond Myklebust58fcb8d2005-08-10 18:15:12 -0400171 unsigned int array_maxlen;
Andreas Gruenbacherbd8100e2005-06-22 17:16:24 +0000172 xdr_xcode_elem_t xcode;
173};
174
175extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
176 struct xdr_array2_desc *desc);
177extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
178 struct xdr_array2_desc *desc);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * Provide some simple tools for XDR buffer overflow-checking etc.
182 */
183struct xdr_stream {
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700184 __be32 *p; /* start of available buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct xdr_buf *buf; /* XDR buffer to read/write */
186
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700187 __be32 *end; /* end of available buffer space */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 struct kvec *iov; /* pointer to the current kvec */
189};
190
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700191extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
192extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
194 unsigned int base, unsigned int len);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700195extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
196extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197extern void xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
Trond Myklebust8b23ea72006-06-09 09:34:21 -0400198extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200#endif /* __KERNEL__ */
201
202#endif /* _SUNRPC_XDR_H_ */