Rev 423 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
423 | giacomo | 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> |
||
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 |
||
25 | struct 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 | */ |
||
35 | typedef int (*kxdrproc_t)(void *rqstp, u32 *data, void *obj); |
||
36 | |||
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 | */ |
||
50 | struct xdr_buf { |
||
51 | struct iovec 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 len; /* Total length of data */ |
||
59 | |||
60 | }; |
||
61 | |||
62 | /* |
||
63 | * pre-xdr'ed macros. |
||
64 | */ |
||
65 | |||
66 | #define xdr_zero __constant_htonl(0) |
||
67 | #define xdr_one __constant_htonl(1) |
||
68 | #define xdr_two __constant_htonl(2) |
||
69 | |||
70 | #define rpc_success __constant_htonl(RPC_SUCCESS) |
||
71 | #define rpc_prog_unavail __constant_htonl(RPC_PROG_UNAVAIL) |
||
72 | #define rpc_prog_mismatch __constant_htonl(RPC_PROG_MISMATCH) |
||
73 | #define rpc_proc_unavail __constant_htonl(RPC_PROC_UNAVAIL) |
||
74 | #define rpc_garbage_args __constant_htonl(RPC_GARBAGE_ARGS) |
||
75 | #define rpc_system_err __constant_htonl(RPC_SYSTEM_ERR) |
||
76 | |||
77 | #define rpc_auth_ok __constant_htonl(RPC_AUTH_OK) |
||
78 | #define rpc_autherr_badcred __constant_htonl(RPC_AUTH_BADCRED) |
||
79 | #define rpc_autherr_rejectedcred __constant_htonl(RPC_AUTH_REJECTEDCRED) |
||
80 | #define rpc_autherr_badverf __constant_htonl(RPC_AUTH_BADVERF) |
||
81 | #define rpc_autherr_rejectedverf __constant_htonl(RPC_AUTH_REJECTEDVERF) |
||
82 | #define rpc_autherr_tooweak __constant_htonl(RPC_AUTH_TOOWEAK) |
||
83 | #define rpcsec_gsserr_credproblem __constant_htonl(RPCSEC_GSS_CREDPROBLEM) |
||
84 | #define rpcsec_gsserr_ctxproblem __constant_htonl(RPCSEC_GSS_CTXPROBLEM) |
||
85 | #define rpc_autherr_oldseqnum __constant_htonl(101) |
||
86 | |||
87 | /* |
||
88 | * Miscellaneous XDR helper functions |
||
89 | */ |
||
90 | u32 * xdr_encode_array(u32 *p, const char *s, unsigned int len); |
||
91 | u32 * xdr_encode_string(u32 *p, const char *s); |
||
92 | u32 * xdr_decode_string(u32 *p, char **sp, int *lenp, int maxlen); |
||
93 | u32 * xdr_decode_string_inplace(u32 *p, char **sp, int *lenp, int maxlen); |
||
94 | u32 * xdr_encode_netobj(u32 *p, const struct xdr_netobj *); |
||
95 | u32 * xdr_decode_netobj(u32 *p, struct xdr_netobj *); |
||
96 | u32 * xdr_decode_netobj_fixed(u32 *p, void *obj, unsigned int len); |
||
97 | |||
98 | void xdr_encode_pages(struct xdr_buf *, struct page **, unsigned int, |
||
99 | unsigned int); |
||
100 | void xdr_inline_pages(struct xdr_buf *, unsigned int, |
||
101 | struct page **, unsigned int, unsigned int); |
||
102 | |||
103 | /* |
||
104 | * Decode 64bit quantities (NFSv3 support) |
||
105 | */ |
||
106 | static inline u32 * |
||
107 | xdr_encode_hyper(u32 *p, __u64 val) |
||
108 | { |
||
109 | *p++ = htonl(val >> 32); |
||
110 | *p++ = htonl(val & 0xFFFFFFFF); |
||
111 | return p; |
||
112 | } |
||
113 | |||
114 | static inline u32 * |
||
115 | xdr_decode_hyper(u32 *p, __u64 *valp) |
||
116 | { |
||
117 | *valp = ((__u64) ntohl(*p++)) << 32; |
||
118 | *valp |= ntohl(*p++); |
||
119 | return p; |
||
120 | } |
||
121 | |||
122 | /* |
||
123 | * Adjust iovec to reflect end of xdr'ed data (RPC client XDR) |
||
124 | */ |
||
125 | static inline int |
||
126 | xdr_adjust_iovec(struct iovec *iov, u32 *p) |
||
127 | { |
||
128 | return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base); |
||
129 | } |
||
130 | |||
131 | void xdr_shift_iovec(struct iovec *, int, size_t); |
||
132 | |||
133 | /* |
||
134 | * Maximum number of iov's we use. |
||
135 | */ |
||
136 | #define MAX_IOVEC (12) |
||
137 | |||
138 | /* |
||
139 | * XDR buffer helper functions |
||
140 | */ |
||
141 | extern int xdr_kmap(struct iovec *, struct xdr_buf *, size_t); |
||
142 | extern void xdr_kunmap(struct xdr_buf *, size_t); |
||
143 | extern void xdr_shift_buf(struct xdr_buf *, size_t); |
||
144 | |||
145 | /* |
||
146 | * Helper structure for copying from an sk_buff. |
||
147 | */ |
||
148 | typedef struct { |
||
149 | struct sk_buff *skb; |
||
150 | unsigned int offset; |
||
151 | size_t count; |
||
152 | unsigned int csum; |
||
153 | } skb_reader_t; |
||
154 | |||
155 | typedef size_t (*skb_read_actor_t)(skb_reader_t *desc, void *to, size_t len); |
||
156 | |||
157 | extern void xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int, |
||
158 | skb_reader_t *, skb_read_actor_t); |
||
159 | |||
160 | struct socket; |
||
161 | struct sockaddr; |
||
162 | extern int xdr_sendpages(struct socket *, struct sockaddr *, int, |
||
163 | struct xdr_buf *, unsigned int, int); |
||
164 | |||
165 | /* |
||
166 | * Provide some simple tools for XDR buffer overflow-checking etc. |
||
167 | */ |
||
168 | struct xdr_stream { |
||
169 | uint32_t *p; /* start of available buffer */ |
||
170 | struct xdr_buf *buf; /* XDR buffer to read/write */ |
||
171 | |||
172 | uint32_t *end; /* end of available buffer space */ |
||
173 | struct iovec *iov; /* pointer to the current iovec */ |
||
174 | }; |
||
175 | |||
176 | /* |
||
177 | * Initialize an xdr_stream for encoding data. |
||
178 | * |
||
179 | * Note: at the moment the RPC client only passes the length of our |
||
180 | * scratch buffer in the xdr_buf's header iovec. Previously this |
||
181 | * meant we needed to call xdr_adjust_iovec() after encoding the |
||
182 | * data. With the new scheme, the xdr_stream manages the details |
||
183 | * of the buffer length, and takes care of adjusting the iovec |
||
184 | * length for us. |
||
185 | */ |
||
186 | static inline void |
||
187 | xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) |
||
188 | { |
||
189 | struct iovec *iov = buf->head; |
||
190 | |||
191 | xdr->buf = buf; |
||
192 | xdr->iov = iov; |
||
193 | xdr->end = (uint32_t *)((char *)iov->iov_base + iov->iov_len); |
||
194 | buf->len = iov->iov_len = (char *)p - (char *)iov->iov_base; |
||
195 | xdr->p = p; |
||
196 | } |
||
197 | |||
198 | /* |
||
199 | * Check that we have enough buffer space to encode 'nbytes' more |
||
200 | * bytes of data. If so, update the total xdr_buf length, and |
||
201 | * adjust the length of the current iovec. |
||
202 | */ |
||
203 | static inline uint32_t * |
||
204 | xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes) |
||
205 | { |
||
206 | uint32_t *p = xdr->p; |
||
207 | uint32_t *q; |
||
208 | |||
209 | /* align nbytes on the next 32-bit boundary */ |
||
210 | nbytes += 3; |
||
211 | nbytes &= ~3; |
||
212 | q = p + (nbytes >> 2); |
||
213 | if (unlikely(q > xdr->end || q < p)) |
||
214 | return NULL; |
||
215 | xdr->p = q; |
||
216 | xdr->iov->iov_len += nbytes; |
||
217 | xdr->buf->len += nbytes; |
||
218 | return p; |
||
219 | } |
||
220 | |||
221 | extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages, |
||
222 | unsigned int base, unsigned int len); |
||
223 | extern void xdr_read_pages(struct xdr_stream *xdr, unsigned int len); |
||
224 | |||
225 | /* |
||
226 | * Initialize an xdr_stream for decoding data. |
||
227 | */ |
||
228 | static inline void |
||
229 | xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, uint32_t *p) |
||
230 | { |
||
231 | struct iovec *iov = buf->head; |
||
232 | xdr->buf = buf; |
||
233 | xdr->iov = iov; |
||
234 | xdr->p = p; |
||
235 | xdr->end = (uint32_t *)((char *)iov->iov_base + iov->iov_len); |
||
236 | } |
||
237 | |||
238 | /* |
||
239 | * Check if the input buffer is long enough to enable us to decode |
||
240 | * 'nbytes' more bytes of data starting at the current position. |
||
241 | * If so return the current pointer, then update the current |
||
242 | * position. |
||
243 | */ |
||
244 | static inline uint32_t * |
||
245 | xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes) |
||
246 | { |
||
247 | uint32_t *p = xdr->p; |
||
248 | uint32_t *q = p + XDR_QUADLEN(nbytes); |
||
249 | |||
250 | if (unlikely(q > xdr->end || q < p)) |
||
251 | return NULL; |
||
252 | xdr->p = q; |
||
253 | return p; |
||
254 | } |
||
255 | |||
256 | #endif /* __KERNEL__ */ |
||
257 | |||
258 | #endif /* _SUNRPC_XDR_H_ */ |