xdr.h 7.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * XDR standard data types and function declarations
L
Linus Torvalds 已提交
3 4
 *
 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
5 6 7
 *
 * Based on:
 *   RFC 4506 "XDR: External Data Representation Standard", May 2006
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16
 */

#ifndef _SUNRPC_XDR_H_
#define _SUNRPC_XDR_H_

#ifdef __KERNEL__

#include <linux/uio.h>
#include <asm/byteorder.h>
17
#include <asm/unaligned.h>
O
Olga Kornievskaia 已提交
18
#include <linux/scatterlist.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

/*
 * Buffer adjustment
 */
#define XDR_QUADLEN(l)		(((l) + 3) >> 2)

/*
 * Generic opaque `network object.' At the kernel level, this type
 * is used only by lockd.
 */
#define XDR_MAX_NETOBJ		1024
struct xdr_netobj {
	unsigned int		len;
	u8 *			data;
};

/*
36 37
 * This is the legacy generic XDR function. rqstp is either a rpc_rqst
 * (client side) or svc_rqst pointer (server side).
L
Linus Torvalds 已提交
38 39
 * Encode functions always assume there's enough room in the buffer.
 */
40
typedef int	(*kxdrproc_t)(void *rqstp, __be32 *data, void *obj);
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

/*
 * Basic structure for transmission/reception of a client XDR message.
 * Features a header (for a linear buffer containing RPC headers
 * and the data payload for short messages), and then an array of
 * pages.
 * The tail iovec allows you to append data after the page array. Its
 * main interest is for appending padding to the pages in order to
 * satisfy the int_32-alignment requirements in RFC1832.
 *
 * For the future, we might want to string several of these together
 * in a list if anybody wants to make use of NFSv4 COMPOUND
 * operations and/or has a need for scatter/gather involving pages.
 */
struct xdr_buf {
	struct kvec	head[1],	/* RPC header + non-page data */
			tail[1];	/* Appended after page data */

59
	struct page **	pages;		/* Array of pages */
L
Linus Torvalds 已提交
60
	unsigned int	page_base,	/* Start of page data */
61 62 63 64
			page_len,	/* Length of page data */
			flags;		/* Flags for data disposition */
#define XDRBUF_READ		0x01		/* target of file read */
#define XDRBUF_WRITE		0x02		/* source of file write */
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73

	unsigned int	buflen,		/* Total length of storage buffer */
			len;		/* Length of XDR encoded message */
};

/*
 * pre-xdr'ed macros.
 */

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#define	xdr_zero	cpu_to_be32(0)
#define	xdr_one		cpu_to_be32(1)
#define	xdr_two		cpu_to_be32(2)

#define	rpc_success		cpu_to_be32(RPC_SUCCESS)
#define	rpc_prog_unavail	cpu_to_be32(RPC_PROG_UNAVAIL)
#define	rpc_prog_mismatch	cpu_to_be32(RPC_PROG_MISMATCH)
#define	rpc_proc_unavail	cpu_to_be32(RPC_PROC_UNAVAIL)
#define	rpc_garbage_args	cpu_to_be32(RPC_GARBAGE_ARGS)
#define	rpc_system_err		cpu_to_be32(RPC_SYSTEM_ERR)
#define	rpc_drop_reply		cpu_to_be32(RPC_DROP_REPLY)

#define	rpc_auth_ok		cpu_to_be32(RPC_AUTH_OK)
#define	rpc_autherr_badcred	cpu_to_be32(RPC_AUTH_BADCRED)
#define	rpc_autherr_rejectedcred cpu_to_be32(RPC_AUTH_REJECTEDCRED)
#define	rpc_autherr_badverf	cpu_to_be32(RPC_AUTH_BADVERF)
#define	rpc_autherr_rejectedverf cpu_to_be32(RPC_AUTH_REJECTEDVERF)
#define	rpc_autherr_tooweak	cpu_to_be32(RPC_AUTH_TOOWEAK)
#define	rpcsec_gsserr_credproblem	cpu_to_be32(RPCSEC_GSS_CREDPROBLEM)
#define	rpcsec_gsserr_ctxproblem	cpu_to_be32(RPCSEC_GSS_CTXPROBLEM)
#define	rpc_autherr_oldseqnum	cpu_to_be32(101)
L
Linus Torvalds 已提交
95 96 97 98

/*
 * Miscellaneous XDR helper functions
 */
99 100 101
__be32 *xdr_encode_opaque_fixed(__be32 *p, const void *ptr, unsigned int len);
__be32 *xdr_encode_opaque(__be32 *p, const void *ptr, unsigned int len);
__be32 *xdr_encode_string(__be32 *p, const char *s);
102 103
__be32 *xdr_decode_string_inplace(__be32 *p, char **sp, unsigned int *lenp,
			unsigned int maxlen);
104 105
__be32 *xdr_encode_netobj(__be32 *p, const struct xdr_netobj *);
__be32 *xdr_decode_netobj(__be32 *p, struct xdr_netobj *);
L
Linus Torvalds 已提交
106 107 108

void	xdr_inline_pages(struct xdr_buf *, unsigned int,
			 struct page **, unsigned int, unsigned int);
109
void	xdr_terminate_string(struct xdr_buf *, const u32);
L
Linus Torvalds 已提交
110

111
static inline __be32 *xdr_encode_array(__be32 *p, const void *s, unsigned int len)
L
Linus Torvalds 已提交
112 113 114 115 116 117 118
{
	return xdr_encode_opaque(p, s, len);
}

/*
 * Decode 64bit quantities (NFSv3 support)
 */
119 120
static inline __be32 *
xdr_encode_hyper(__be32 *p, __u64 val)
L
Linus Torvalds 已提交
121
{
122
	put_unaligned_be64(val, p);
B
Benny Halevy 已提交
123
	return p + 2;
L
Linus Torvalds 已提交
124 125
}

126 127
static inline __be32 *
xdr_decode_hyper(__be32 *p, __u64 *valp)
L
Linus Torvalds 已提交
128
{
129
	*valp = get_unaligned_be64(p);
B
Benny Halevy 已提交
130
	return p + 2;
L
Linus Torvalds 已提交
131 132
}

133 134 135 136 137 138 139
static inline __be32 *
xdr_decode_opaque_fixed(__be32 *p, void *ptr, unsigned int len)
{
	memcpy(ptr, p, len);
	return p + XDR_QUADLEN(len);
}

L
Linus Torvalds 已提交
140 141 142 143
/*
 * Adjust kvec to reflect end of xdr'ed data (RPC client XDR)
 */
static inline int
144
xdr_adjust_iovec(struct kvec *iov, __be32 *p)
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153
{
	return iov->iov_len = ((u8 *) p - (u8 *) iov->iov_base);
}

/*
 * XDR buffer helper functions
 */
extern void xdr_shift_buf(struct xdr_buf *, size_t);
extern void xdr_buf_from_iov(struct kvec *, struct xdr_buf *);
154
extern int xdr_buf_subsegment(struct xdr_buf *, struct xdr_buf *, unsigned int, unsigned int);
155
extern void xdr_buf_trim(struct xdr_buf *, unsigned int);
156 157 158
extern int xdr_buf_read_netobj(struct xdr_buf *, struct xdr_netobj *, unsigned int);
extern int read_bytes_from_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
extern int write_bytes_to_xdr_buf(struct xdr_buf *, unsigned int, void *, unsigned int);
L
Linus Torvalds 已提交
159 160 161 162

/*
 * Helper structure for copying from an sk_buff.
 */
163
struct xdr_skb_reader {
L
Linus Torvalds 已提交
164 165 166
	struct sk_buff	*skb;
	unsigned int	offset;
	size_t		count;
167
	__wsum		csum;
168
};
L
Linus Torvalds 已提交
169

170
typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to, size_t len);
L
Linus Torvalds 已提交
171

172
size_t xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len);
173
extern int csum_partial_copy_to_xdr(struct xdr_buf *, struct sk_buff *);
174
extern ssize_t xdr_partial_copy_from_skb(struct xdr_buf *, unsigned int,
175
		struct xdr_skb_reader *, xdr_skb_read_actor);
L
Linus Torvalds 已提交
176

177 178
extern int xdr_encode_word(struct xdr_buf *, unsigned int, u32);
extern int xdr_decode_word(struct xdr_buf *, unsigned int, u32 *);
179 180 181 182 183 184

struct xdr_array2_desc;
typedef int (*xdr_xcode_elem_t)(struct xdr_array2_desc *desc, void *elem);
struct xdr_array2_desc {
	unsigned int elem_size;
	unsigned int array_len;
185
	unsigned int array_maxlen;
186 187 188 189
	xdr_xcode_elem_t xcode;
};

extern int xdr_decode_array2(struct xdr_buf *buf, unsigned int base,
190
			     struct xdr_array2_desc *desc);
191 192
extern int xdr_encode_array2(struct xdr_buf *buf, unsigned int base,
			     struct xdr_array2_desc *desc);
193 194
extern void _copy_from_pages(char *p, struct page **pages, size_t pgbase,
			     size_t len);
195

L
Linus Torvalds 已提交
196 197 198 199
/*
 * Provide some simple tools for XDR buffer overflow-checking etc.
 */
struct xdr_stream {
200
	__be32 *p;		/* start of available buffer */
L
Linus Torvalds 已提交
201 202
	struct xdr_buf *buf;	/* XDR buffer to read/write */

203
	__be32 *end;		/* end of available buffer space */
L
Linus Torvalds 已提交
204
	struct kvec *iov;	/* pointer to the current kvec */
205 206
	struct kvec scratch;	/* Scratch buffer */
	struct page **page_ptr;	/* pointer to the current page */
207
	unsigned int nwords;	/* Remaining decode buffer length */
L
Linus Torvalds 已提交
208 209
};

210
/*
211
 * These are the xdr_stream style generic XDR encode and decode functions.
212 213
 */
typedef void	(*kxdreproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj);
214
typedef int	(*kxdrdproc_t)(void *rqstp, struct xdr_stream *xdr, void *obj);
215

216 217
extern void xdr_init_encode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
extern __be32 *xdr_reserve_space(struct xdr_stream *xdr, size_t nbytes);
L
Linus Torvalds 已提交
218 219
extern void xdr_write_pages(struct xdr_stream *xdr, struct page **pages,
		unsigned int base, unsigned int len);
220
extern unsigned int xdr_stream_pos(const struct xdr_stream *xdr);
221
extern void xdr_init_decode(struct xdr_stream *xdr, struct xdr_buf *buf, __be32 *p);
222 223
extern void xdr_init_decode_pages(struct xdr_stream *xdr, struct xdr_buf *buf,
		struct page **pages, unsigned int len);
224
extern void xdr_set_scratch_buffer(struct xdr_stream *xdr, void *buf, size_t buflen);
225
extern __be32 *xdr_inline_decode(struct xdr_stream *xdr, size_t nbytes);
226
extern unsigned int xdr_read_pages(struct xdr_stream *xdr, unsigned int len);
227
extern void xdr_enter_page(struct xdr_stream *xdr, unsigned int len);
O
Olga Kornievskaia 已提交
228
extern int xdr_process_buf(struct xdr_buf *buf, unsigned int offset, unsigned int len, int (*actor)(struct scatterlist *, void *), void *data);
L
Linus Torvalds 已提交
229 230 231 232

#endif /* __KERNEL__ */

#endif /* _SUNRPC_XDR_H_ */