blob: 379f71508995f1bd65a740c2c558792ca9061ff0 [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#ifndef __CEPH_DECODE_H
2#define __CEPH_DECODE_H
3
Alex Elderf8c36c52012-07-11 08:24:45 -05004#include <linux/err.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05005#include <linux/bug.h>
Sage Weilc7e337d2010-02-02 16:11:19 -08006#include <linux/time.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05007#include <asm/unaligned.h>
Sage Weil31b80062009-10-06 11:31:13 -07008
David Howellsa1ce3922012-10-02 18:01:25 +01009#include <linux/ceph/types.h>
Sage Weilc89136e2009-10-14 09:59:09 -070010
Alex Elderadfe6952013-03-13 20:50:00 -050011/* This seemed to be the easiest place to define these */
12
Alex Elderb5873982013-04-19 15:34:50 -050013#define U8_MAX ((u8)(~0U))
14#define U16_MAX ((u16)(~0U))
15#define U32_MAX ((u32)(~0U))
16#define U64_MAX ((u64)(~0ULL))
17
18#define S8_MAX ((s8)(U8_MAX >> 1))
19#define S16_MAX ((s16)(U16_MAX >> 1))
20#define S32_MAX ((s32)(U32_MAX >> 1))
21#define S64_MAX ((s64)(U64_MAX >> 1LL))
22
23#define S8_MIN ((s8)(-S8_MAX - 1))
24#define S16_MIN ((s16)(-S16_MAX - 1))
25#define S32_MIN ((s32)(-S32_MAX - 1))
26#define S64_MIN ((s64)(-S64_MAX - 1LL))
Alex Elderadfe6952013-03-13 20:50:00 -050027
Sage Weil31b80062009-10-06 11:31:13 -070028/*
29 * in all cases,
30 * void **p pointer to position pointer
31 * void *end pointer to end of buffer (last byte + 1)
32 */
33
Sage Weilc89136e2009-10-14 09:59:09 -070034static inline u64 ceph_decode_64(void **p)
35{
36 u64 v = get_unaligned_le64(*p);
37 *p += sizeof(u64);
38 return v;
39}
40static inline u32 ceph_decode_32(void **p)
41{
42 u32 v = get_unaligned_le32(*p);
43 *p += sizeof(u32);
44 return v;
45}
46static inline u16 ceph_decode_16(void **p)
47{
48 u16 v = get_unaligned_le16(*p);
49 *p += sizeof(u16);
50 return v;
51}
52static inline u8 ceph_decode_8(void **p)
53{
54 u8 v = *(u8 *)*p;
55 (*p)++;
56 return v;
57}
58static inline void ceph_decode_copy(void **p, void *pv, size_t n)
59{
60 memcpy(pv, *p, n);
61 *p += n;
62}
63
Sage Weil31b80062009-10-06 11:31:13 -070064/*
65 * bounds check input.
66 */
Xi Wang76aa5422012-04-20 15:49:44 -050067static inline int ceph_has_room(void **p, void *end, size_t n)
68{
69 return end >= *p && n <= end - *p;
70}
71
Alex Elderdd5f0492012-11-01 08:39:27 -050072#define ceph_decode_need(p, end, n, bad) \
73 do { \
74 if (!likely(ceph_has_room(p, end, n))) \
75 goto bad; \
Sage Weil31b80062009-10-06 11:31:13 -070076 } while (0)
77
Sage Weil31b80062009-10-06 11:31:13 -070078#define ceph_decode_64_safe(p, end, v, bad) \
79 do { \
80 ceph_decode_need(p, end, sizeof(u64), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070081 v = ceph_decode_64(p); \
Sage Weil31b80062009-10-06 11:31:13 -070082 } while (0)
83#define ceph_decode_32_safe(p, end, v, bad) \
84 do { \
85 ceph_decode_need(p, end, sizeof(u32), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070086 v = ceph_decode_32(p); \
Sage Weil31b80062009-10-06 11:31:13 -070087 } while (0)
88#define ceph_decode_16_safe(p, end, v, bad) \
89 do { \
90 ceph_decode_need(p, end, sizeof(u16), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070091 v = ceph_decode_16(p); \
Sage Weil31b80062009-10-06 11:31:13 -070092 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -080093#define ceph_decode_8_safe(p, end, v, bad) \
94 do { \
95 ceph_decode_need(p, end, sizeof(u8), bad); \
96 v = ceph_decode_8(p); \
97 } while (0)
Sage Weil31b80062009-10-06 11:31:13 -070098
99#define ceph_decode_copy_safe(p, end, pv, n, bad) \
100 do { \
101 ceph_decode_need(p, end, n, bad); \
102 ceph_decode_copy(p, pv, n); \
103 } while (0)
104
105/*
Alex Elderf8c36c52012-07-11 08:24:45 -0500106 * Allocate a buffer big enough to hold the wire-encoded string, and
107 * decode the string into it. The resulting string will always be
108 * terminated with '\0'. If successful, *p will be advanced
109 * past the decoded data. Also, if lenp is not a null pointer, the
110 * length (not including the terminating '\0') will be recorded in
111 * *lenp. Note that a zero-length string is a valid return value.
112 *
113 * Returns a pointer to the newly-allocated string buffer, or a
114 * pointer-coded errno if an error occurs. Neither *p nor *lenp
115 * will have been updated if an error is returned.
116 *
117 * There are two possible failures:
118 * - converting the string would require accessing memory at or
Alex Elderdd5f0492012-11-01 08:39:27 -0500119 * beyond the "end" pointer provided (-ERANGE)
120 * - memory could not be allocated for the result (-ENOMEM)
Alex Elderf8c36c52012-07-11 08:24:45 -0500121 */
122static inline char *ceph_extract_encoded_string(void **p, void *end,
123 size_t *lenp, gfp_t gfp)
124{
125 u32 len;
126 void *sp = *p;
127 char *buf;
128
129 ceph_decode_32_safe(&sp, end, len, bad);
130 if (!ceph_has_room(&sp, end, len))
131 goto bad;
132
133 buf = kmalloc(len + 1, gfp);
134 if (!buf)
135 return ERR_PTR(-ENOMEM);
136
137 if (len)
138 memcpy(buf, sp, len);
139 buf[len] = '\0';
140
141 *p = (char *) *p + sizeof (u32) + len;
142 if (lenp)
143 *lenp = (size_t) len;
144
145 return buf;
146
147bad:
148 return ERR_PTR(-ERANGE);
149}
150
151/*
Sage Weil31b80062009-10-06 11:31:13 -0700152 * struct ceph_timespec <-> struct timespec
153 */
Sage Weilc89136e2009-10-14 09:59:09 -0700154static inline void ceph_decode_timespec(struct timespec *ts,
Sage Weil63f2d212009-11-03 15:17:56 -0800155 const struct ceph_timespec *tv)
Sage Weilc89136e2009-10-14 09:59:09 -0700156{
Alex Elderc3f56102013-04-19 15:34:50 -0500157 ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
158 ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
Sage Weilc89136e2009-10-14 09:59:09 -0700159}
160static inline void ceph_encode_timespec(struct ceph_timespec *tv,
Sage Weil63f2d212009-11-03 15:17:56 -0800161 const struct timespec *ts)
Sage Weilc89136e2009-10-14 09:59:09 -0700162{
Alex Elderc3f56102013-04-19 15:34:50 -0500163 BUG_ON(ts->tv_sec < 0);
164 BUG_ON(ts->tv_sec > (__kernel_time_t)U32_MAX);
165 BUG_ON(ts->tv_nsec < 0);
166 BUG_ON(ts->tv_nsec > (long)U32_MAX);
167
168 tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
169 tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
Sage Weilc89136e2009-10-14 09:59:09 -0700170}
Sage Weil31b80062009-10-06 11:31:13 -0700171
172/*
Sage Weil63f2d212009-11-03 15:17:56 -0800173 * sockaddr_storage <-> ceph_sockaddr
174 */
175static inline void ceph_encode_addr(struct ceph_entity_addr *a)
176{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700177 __be16 ss_family = htons(a->in_addr.ss_family);
178 a->in_addr.ss_family = *(__u16 *)&ss_family;
Sage Weil63f2d212009-11-03 15:17:56 -0800179}
180static inline void ceph_decode_addr(struct ceph_entity_addr *a)
181{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700182 __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
183 a->in_addr.ss_family = ntohs(ss_family);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800184 WARN_ON(a->in_addr.ss_family == 512);
Sage Weil63f2d212009-11-03 15:17:56 -0800185}
186
187/*
Sage Weil31b80062009-10-06 11:31:13 -0700188 * encoders
189 */
Sage Weilc89136e2009-10-14 09:59:09 -0700190static inline void ceph_encode_64(void **p, u64 v)
191{
192 put_unaligned_le64(v, (__le64 *)*p);
193 *p += sizeof(u64);
194}
195static inline void ceph_encode_32(void **p, u32 v)
196{
197 put_unaligned_le32(v, (__le32 *)*p);
198 *p += sizeof(u32);
199}
200static inline void ceph_encode_16(void **p, u16 v)
201{
202 put_unaligned_le16(v, (__le16 *)*p);
203 *p += sizeof(u16);
204}
205static inline void ceph_encode_8(void **p, u8 v)
206{
207 *(u8 *)*p = v;
208 (*p)++;
209}
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800210static inline void ceph_encode_copy(void **p, const void *s, int len)
211{
212 memcpy(*p, s, len);
213 *p += len;
214}
Sage Weil31b80062009-10-06 11:31:13 -0700215
216/*
217 * filepath, string encoders
218 */
219static inline void ceph_encode_filepath(void **p, void *end,
220 u64 ino, const char *path)
221{
222 u32 len = path ? strlen(path) : 0;
Alex Elderc61a1ab2012-07-03 16:01:18 -0500223 BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
Sage Weilac8839d2010-01-27 14:28:10 -0800224 ceph_encode_8(p, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700225 ceph_encode_64(p, ino);
226 ceph_encode_32(p, len);
227 if (len)
228 memcpy(*p, path, len);
229 *p += len;
230}
231
232static inline void ceph_encode_string(void **p, void *end,
233 const char *s, u32 len)
234{
235 BUG_ON(*p + sizeof(len) + len > end);
236 ceph_encode_32(p, len);
237 if (len)
238 memcpy(*p, s, len);
239 *p += len;
240}
241
Alex Elderdd5f0492012-11-01 08:39:27 -0500242#define ceph_encode_need(p, end, n, bad) \
243 do { \
244 if (!likely(ceph_has_room(p, end, n))) \
245 goto bad; \
Sage Weilc7e337d2010-02-02 16:11:19 -0800246 } while (0)
247
248#define ceph_encode_64_safe(p, end, v, bad) \
249 do { \
250 ceph_encode_need(p, end, sizeof(u64), bad); \
251 ceph_encode_64(p, v); \
252 } while (0)
253#define ceph_encode_32_safe(p, end, v, bad) \
254 do { \
255 ceph_encode_need(p, end, sizeof(u32), bad); \
Alex Elderdd5f0492012-11-01 08:39:27 -0500256 ceph_encode_32(p, v); \
Sage Weilc7e337d2010-02-02 16:11:19 -0800257 } while (0)
258#define ceph_encode_16_safe(p, end, v, bad) \
259 do { \
260 ceph_encode_need(p, end, sizeof(u16), bad); \
Alex Elderdd5f0492012-11-01 08:39:27 -0500261 ceph_encode_16(p, v); \
262 } while (0)
263#define ceph_encode_8_safe(p, end, v, bad) \
264 do { \
265 ceph_encode_need(p, end, sizeof(u8), bad); \
266 ceph_encode_8(p, v); \
Sage Weilc7e337d2010-02-02 16:11:19 -0800267 } while (0)
268
269#define ceph_encode_copy_safe(p, end, pv, n, bad) \
270 do { \
271 ceph_encode_need(p, end, n, bad); \
272 ceph_encode_copy(p, pv, n); \
273 } while (0)
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700274#define ceph_encode_string_safe(p, end, s, n, bad) \
275 do { \
276 ceph_encode_need(p, end, n, bad); \
277 ceph_encode_string(p, end, s, n); \
278 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -0800279
Sage Weil31b80062009-10-06 11:31:13 -0700280
281#endif