decode.h 6.6 KB
Newer Older
S
Sage Weil 已提交
1 2 3
#ifndef __CEPH_DECODE_H
#define __CEPH_DECODE_H

4
#include <linux/err.h>
5
#include <linux/bug.h>
S
Sage Weil 已提交
6
#include <linux/time.h>
7
#include <asm/unaligned.h>
S
Sage Weil 已提交
8

9
#include <linux/ceph/types.h>
10

A
Alex Elder 已提交
11 12
/* This seemed to be the easiest place to define these */

A
Alex Elder 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26
#define	U8_MAX	((u8)(~0U))
#define	U16_MAX	((u16)(~0U))
#define	U32_MAX	((u32)(~0U))
#define	U64_MAX	((u64)(~0ULL))

#define	S8_MAX	((s8)(U8_MAX >> 1))
#define	S16_MAX	((s16)(U16_MAX >> 1))
#define	S32_MAX	((s32)(U32_MAX >> 1))
#define	S64_MAX	((s64)(U64_MAX >> 1LL))

#define	S8_MIN	((s8)(-S8_MAX - 1))
#define	S16_MIN	((s16)(-S16_MAX - 1))
#define	S32_MIN	((s32)(-S32_MAX - 1))
#define	S64_MIN	((s64)(-S64_MAX - 1LL))
A
Alex Elder 已提交
27

S
Sage Weil 已提交
28 29 30 31 32 33
/*
 * in all cases,
 *   void **p     pointer to position pointer
 *   void *end    pointer to end of buffer (last byte + 1)
 */

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static inline u64 ceph_decode_64(void **p)
{
	u64 v = get_unaligned_le64(*p);
	*p += sizeof(u64);
	return v;
}
static inline u32 ceph_decode_32(void **p)
{
	u32 v = get_unaligned_le32(*p);
	*p += sizeof(u32);
	return v;
}
static inline u16 ceph_decode_16(void **p)
{
	u16 v = get_unaligned_le16(*p);
	*p += sizeof(u16);
	return v;
}
static inline u8 ceph_decode_8(void **p)
{
	u8 v = *(u8 *)*p;
	(*p)++;
	return v;
}
static inline void ceph_decode_copy(void **p, void *pv, size_t n)
{
	memcpy(pv, *p, n);
	*p += n;
}

S
Sage Weil 已提交
64 65 66
/*
 * bounds check input.
 */
67 68 69 70 71
static inline int ceph_has_room(void **p, void *end, size_t n)
{
	return end >= *p && n <= end - *p;
}

A
Alex Elder 已提交
72 73 74 75
#define ceph_decode_need(p, end, n, bad)			\
	do {							\
		if (!likely(ceph_has_room(p, end, n)))		\
			goto bad;				\
S
Sage Weil 已提交
76 77 78 79 80
	} while (0)

#define ceph_decode_64_safe(p, end, v, bad)			\
	do {							\
		ceph_decode_need(p, end, sizeof(u64), bad);	\
81
		v = ceph_decode_64(p);				\
S
Sage Weil 已提交
82 83 84 85
	} while (0)
#define ceph_decode_32_safe(p, end, v, bad)			\
	do {							\
		ceph_decode_need(p, end, sizeof(u32), bad);	\
86
		v = ceph_decode_32(p);				\
S
Sage Weil 已提交
87 88 89 90
	} while (0)
#define ceph_decode_16_safe(p, end, v, bad)			\
	do {							\
		ceph_decode_need(p, end, sizeof(u16), bad);	\
91
		v = ceph_decode_16(p);				\
S
Sage Weil 已提交
92
	} while (0)
S
Sage Weil 已提交
93 94 95 96 97
#define ceph_decode_8_safe(p, end, v, bad)			\
	do {							\
		ceph_decode_need(p, end, sizeof(u8), bad);	\
		v = ceph_decode_8(p);				\
	} while (0)
S
Sage Weil 已提交
98 99 100 101 102 103 104

#define ceph_decode_copy_safe(p, end, pv, n, bad)		\
	do {							\
		ceph_decode_need(p, end, n, bad);		\
		ceph_decode_copy(p, pv, n);			\
	} while (0)

105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*
 * Allocate a buffer big enough to hold the wire-encoded string, and
 * decode the string into it.  The resulting string will always be
 * terminated with '\0'.  If successful, *p will be advanced
 * past the decoded data.  Also, if lenp is not a null pointer, the
 * length (not including the terminating '\0') will be recorded in
 * *lenp.  Note that a zero-length string is a valid return value.
 *
 * Returns a pointer to the newly-allocated string buffer, or a
 * pointer-coded errno if an error occurs.  Neither *p nor *lenp
 * will have been updated if an error is returned.
 *
 * There are two possible failures:
 *   - converting the string would require accessing memory at or
A
Alex Elder 已提交
119 120
 *     beyond the "end" pointer provided (-ERANGE)
 *   - memory could not be allocated for the result (-ENOMEM)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
 */
static inline char *ceph_extract_encoded_string(void **p, void *end,
						size_t *lenp, gfp_t gfp)
{
	u32 len;
	void *sp = *p;
	char *buf;

	ceph_decode_32_safe(&sp, end, len, bad);
	if (!ceph_has_room(&sp, end, len))
		goto bad;

	buf = kmalloc(len + 1, gfp);
	if (!buf)
		return ERR_PTR(-ENOMEM);

	if (len)
		memcpy(buf, sp, len);
	buf[len] = '\0';

	*p = (char *) *p + sizeof (u32) + len;
	if (lenp)
		*lenp = (size_t) len;

	return buf;

bad:
	return ERR_PTR(-ERANGE);
}

S
Sage Weil 已提交
151 152 153
/*
 * struct ceph_timespec <-> struct timespec
 */
154
static inline void ceph_decode_timespec(struct timespec *ts,
155
					const struct ceph_timespec *tv)
156
{
157 158
	ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
	ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
159 160
}
static inline void ceph_encode_timespec(struct ceph_timespec *tv,
161
					const struct timespec *ts)
162
{
163 164 165 166 167 168 169
	BUG_ON(ts->tv_sec < 0);
	BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
	BUG_ON(ts->tv_nsec < 0);
	BUG_ON(ts->tv_nsec > (long)U32_MAX);

	tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
	tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
170
}
S
Sage Weil 已提交
171

172 173 174 175 176
/*
 * sockaddr_storage <-> ceph_sockaddr
 */
static inline void ceph_encode_addr(struct ceph_entity_addr *a)
{
Y
Yehuda Sadeh 已提交
177 178
	__be16 ss_family = htons(a->in_addr.ss_family);
	a->in_addr.ss_family = *(__u16 *)&ss_family;
179 180 181
}
static inline void ceph_decode_addr(struct ceph_entity_addr *a)
{
Y
Yehuda Sadeh 已提交
182 183
	__be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
	a->in_addr.ss_family = ntohs(ss_family);
184
	WARN_ON(a->in_addr.ss_family == 512);
185 186
}

S
Sage Weil 已提交
187 188 189
/*
 * encoders
 */
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
static inline void ceph_encode_64(void **p, u64 v)
{
	put_unaligned_le64(v, (__le64 *)*p);
	*p += sizeof(u64);
}
static inline void ceph_encode_32(void **p, u32 v)
{
	put_unaligned_le32(v, (__le32 *)*p);
	*p += sizeof(u32);
}
static inline void ceph_encode_16(void **p, u16 v)
{
	put_unaligned_le16(v, (__le16 *)*p);
	*p += sizeof(u16);
}
static inline void ceph_encode_8(void **p, u8 v)
{
	*(u8 *)*p = v;
	(*p)++;
}
210 211 212 213 214
static inline void ceph_encode_copy(void **p, const void *s, int len)
{
	memcpy(*p, s, len);
	*p += len;
}
S
Sage Weil 已提交
215 216 217 218 219 220 221 222

/*
 * filepath, string encoders
 */
static inline void ceph_encode_filepath(void **p, void *end,
					u64 ino, const char *path)
{
	u32 len = path ? strlen(path) : 0;
223
	BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
224
	ceph_encode_8(p, 1);
S
Sage Weil 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	ceph_encode_64(p, ino);
	ceph_encode_32(p, len);
	if (len)
		memcpy(*p, path, len);
	*p += len;
}

static inline void ceph_encode_string(void **p, void *end,
				      const char *s, u32 len)
{
	BUG_ON(*p + sizeof(len) + len > end);
	ceph_encode_32(p, len);
	if (len)
		memcpy(*p, s, len);
	*p += len;
}

A
Alex Elder 已提交
242 243 244 245
#define ceph_encode_need(p, end, n, bad)			\
	do {							\
		if (!likely(ceph_has_room(p, end, n)))		\
			goto bad;				\
S
Sage Weil 已提交
246 247 248 249 250 251 252 253 254 255
	} while (0)

#define ceph_encode_64_safe(p, end, v, bad)			\
	do {							\
		ceph_encode_need(p, end, sizeof(u64), bad);	\
		ceph_encode_64(p, v);				\
	} while (0)
#define ceph_encode_32_safe(p, end, v, bad)			\
	do {							\
		ceph_encode_need(p, end, sizeof(u32), bad);	\
A
Alex Elder 已提交
256
		ceph_encode_32(p, v);				\
S
Sage Weil 已提交
257 258 259 260
	} while (0)
#define ceph_encode_16_safe(p, end, v, bad)			\
	do {							\
		ceph_encode_need(p, end, sizeof(u16), bad);	\
A
Alex Elder 已提交
261 262 263 264 265 266
		ceph_encode_16(p, v);				\
	} while (0)
#define ceph_encode_8_safe(p, end, v, bad)			\
	do {							\
		ceph_encode_need(p, end, sizeof(u8), bad);	\
		ceph_encode_8(p, v);				\
S
Sage Weil 已提交
267 268 269 270 271 272 273
	} while (0)

#define ceph_encode_copy_safe(p, end, pv, n, bad)		\
	do {							\
		ceph_encode_need(p, end, n, bad);		\
		ceph_encode_copy(p, pv, n);			\
	} while (0)
274 275 276 277 278
#define ceph_encode_string_safe(p, end, s, n, bad)		\
	do {							\
		ceph_encode_need(p, end, n, bad);		\
		ceph_encode_string(p, end, s, n);		\
	} while (0)
S
Sage Weil 已提交
279

S
Sage Weil 已提交
280 281

#endif