blob: f990f2cc907a404bd55dc9c88f147648e73e9245 [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>
Ilya Dryomovb2aa5d02016-06-07 21:57:15 +02006#include <linux/slab.h>
Sage Weilc7e337d2010-02-02 16:11:19 -08007#include <linux/time.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05008#include <asm/unaligned.h>
Sage Weil31b80062009-10-06 11:31:13 -07009
David Howellsa1ce3922012-10-02 18:01:25 +010010#include <linux/ceph/types.h>
Sage Weilc89136e2009-10-14 09:59:09 -070011
Sage Weil31b80062009-10-06 11:31:13 -070012/*
13 * in all cases,
14 * void **p pointer to position pointer
15 * void *end pointer to end of buffer (last byte + 1)
16 */
17
Sage Weilc89136e2009-10-14 09:59:09 -070018static inline u64 ceph_decode_64(void **p)
19{
20 u64 v = get_unaligned_le64(*p);
21 *p += sizeof(u64);
22 return v;
23}
24static inline u32 ceph_decode_32(void **p)
25{
26 u32 v = get_unaligned_le32(*p);
27 *p += sizeof(u32);
28 return v;
29}
30static inline u16 ceph_decode_16(void **p)
31{
32 u16 v = get_unaligned_le16(*p);
33 *p += sizeof(u16);
34 return v;
35}
36static inline u8 ceph_decode_8(void **p)
37{
38 u8 v = *(u8 *)*p;
39 (*p)++;
40 return v;
41}
42static inline void ceph_decode_copy(void **p, void *pv, size_t n)
43{
44 memcpy(pv, *p, n);
45 *p += n;
46}
47
Sage Weil31b80062009-10-06 11:31:13 -070048/*
49 * bounds check input.
50 */
Zhang Zhuoyu3b33f692016-03-25 05:18:39 -040051static inline bool ceph_has_room(void **p, void *end, size_t n)
Xi Wang76aa5422012-04-20 15:49:44 -050052{
53 return end >= *p && n <= end - *p;
54}
55
Alex Elderdd5f0492012-11-01 08:39:27 -050056#define ceph_decode_need(p, end, n, bad) \
57 do { \
58 if (!likely(ceph_has_room(p, end, n))) \
59 goto bad; \
Sage Weil31b80062009-10-06 11:31:13 -070060 } while (0)
61
Sage Weil31b80062009-10-06 11:31:13 -070062#define ceph_decode_64_safe(p, end, v, bad) \
63 do { \
64 ceph_decode_need(p, end, sizeof(u64), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070065 v = ceph_decode_64(p); \
Sage Weil31b80062009-10-06 11:31:13 -070066 } while (0)
67#define ceph_decode_32_safe(p, end, v, bad) \
68 do { \
69 ceph_decode_need(p, end, sizeof(u32), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070070 v = ceph_decode_32(p); \
Sage Weil31b80062009-10-06 11:31:13 -070071 } while (0)
72#define ceph_decode_16_safe(p, end, v, bad) \
73 do { \
74 ceph_decode_need(p, end, sizeof(u16), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070075 v = ceph_decode_16(p); \
Sage Weil31b80062009-10-06 11:31:13 -070076 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -080077#define ceph_decode_8_safe(p, end, v, bad) \
78 do { \
79 ceph_decode_need(p, end, sizeof(u8), bad); \
80 v = ceph_decode_8(p); \
81 } while (0)
Sage Weil31b80062009-10-06 11:31:13 -070082
83#define ceph_decode_copy_safe(p, end, pv, n, bad) \
84 do { \
85 ceph_decode_need(p, end, n, bad); \
86 ceph_decode_copy(p, pv, n); \
87 } while (0)
88
89/*
Alex Elderf8c36c52012-07-11 08:24:45 -050090 * Allocate a buffer big enough to hold the wire-encoded string, and
91 * decode the string into it. The resulting string will always be
92 * terminated with '\0'. If successful, *p will be advanced
93 * past the decoded data. Also, if lenp is not a null pointer, the
94 * length (not including the terminating '\0') will be recorded in
95 * *lenp. Note that a zero-length string is a valid return value.
96 *
97 * Returns a pointer to the newly-allocated string buffer, or a
98 * pointer-coded errno if an error occurs. Neither *p nor *lenp
99 * will have been updated if an error is returned.
100 *
101 * There are two possible failures:
102 * - converting the string would require accessing memory at or
Alex Elderdd5f0492012-11-01 08:39:27 -0500103 * beyond the "end" pointer provided (-ERANGE)
104 * - memory could not be allocated for the result (-ENOMEM)
Alex Elderf8c36c52012-07-11 08:24:45 -0500105 */
106static inline char *ceph_extract_encoded_string(void **p, void *end,
107 size_t *lenp, gfp_t gfp)
108{
109 u32 len;
110 void *sp = *p;
111 char *buf;
112
113 ceph_decode_32_safe(&sp, end, len, bad);
114 if (!ceph_has_room(&sp, end, len))
115 goto bad;
116
117 buf = kmalloc(len + 1, gfp);
118 if (!buf)
119 return ERR_PTR(-ENOMEM);
120
121 if (len)
122 memcpy(buf, sp, len);
123 buf[len] = '\0';
124
125 *p = (char *) *p + sizeof (u32) + len;
126 if (lenp)
127 *lenp = (size_t) len;
128
129 return buf;
130
131bad:
132 return ERR_PTR(-ERANGE);
133}
134
135/*
Sage Weil31b80062009-10-06 11:31:13 -0700136 * struct ceph_timespec <-> struct timespec
137 */
Sage Weilc89136e2009-10-14 09:59:09 -0700138static inline void ceph_decode_timespec(struct timespec *ts,
Sage Weil63f2d212009-11-03 15:17:56 -0800139 const struct ceph_timespec *tv)
Sage Weilc89136e2009-10-14 09:59:09 -0700140{
Alex Elderc3f56102013-04-19 15:34:50 -0500141 ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
142 ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
Sage Weilc89136e2009-10-14 09:59:09 -0700143}
144static inline void ceph_encode_timespec(struct ceph_timespec *tv,
Sage Weil63f2d212009-11-03 15:17:56 -0800145 const struct timespec *ts)
Sage Weilc89136e2009-10-14 09:59:09 -0700146{
Alex Elderc3f56102013-04-19 15:34:50 -0500147 tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
148 tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
Sage Weilc89136e2009-10-14 09:59:09 -0700149}
Sage Weil31b80062009-10-06 11:31:13 -0700150
151/*
Sage Weil63f2d212009-11-03 15:17:56 -0800152 * sockaddr_storage <-> ceph_sockaddr
153 */
154static inline void ceph_encode_addr(struct ceph_entity_addr *a)
155{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700156 __be16 ss_family = htons(a->in_addr.ss_family);
157 a->in_addr.ss_family = *(__u16 *)&ss_family;
Sage Weil63f2d212009-11-03 15:17:56 -0800158}
159static inline void ceph_decode_addr(struct ceph_entity_addr *a)
160{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700161 __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
162 a->in_addr.ss_family = ntohs(ss_family);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800163 WARN_ON(a->in_addr.ss_family == 512);
Sage Weil63f2d212009-11-03 15:17:56 -0800164}
165
166/*
Sage Weil31b80062009-10-06 11:31:13 -0700167 * encoders
168 */
Sage Weilc89136e2009-10-14 09:59:09 -0700169static inline void ceph_encode_64(void **p, u64 v)
170{
171 put_unaligned_le64(v, (__le64 *)*p);
172 *p += sizeof(u64);
173}
174static inline void ceph_encode_32(void **p, u32 v)
175{
176 put_unaligned_le32(v, (__le32 *)*p);
177 *p += sizeof(u32);
178}
179static inline void ceph_encode_16(void **p, u16 v)
180{
181 put_unaligned_le16(v, (__le16 *)*p);
182 *p += sizeof(u16);
183}
184static inline void ceph_encode_8(void **p, u8 v)
185{
186 *(u8 *)*p = v;
187 (*p)++;
188}
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800189static inline void ceph_encode_copy(void **p, const void *s, int len)
190{
191 memcpy(*p, s, len);
192 *p += len;
193}
Sage Weil31b80062009-10-06 11:31:13 -0700194
195/*
196 * filepath, string encoders
197 */
198static inline void ceph_encode_filepath(void **p, void *end,
199 u64 ino, const char *path)
200{
201 u32 len = path ? strlen(path) : 0;
Alex Elderc61a1ab2012-07-03 16:01:18 -0500202 BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
Sage Weilac8839d2010-01-27 14:28:10 -0800203 ceph_encode_8(p, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700204 ceph_encode_64(p, ino);
205 ceph_encode_32(p, len);
206 if (len)
207 memcpy(*p, path, len);
208 *p += len;
209}
210
211static inline void ceph_encode_string(void **p, void *end,
212 const char *s, u32 len)
213{
214 BUG_ON(*p + sizeof(len) + len > end);
215 ceph_encode_32(p, len);
216 if (len)
217 memcpy(*p, s, len);
218 *p += len;
219}
220
Ilya Dryomov22748f92016-06-02 14:23:32 +0200221/*
222 * version and length starting block encoders/decoders
223 */
224
225/* current code version (u8) + compat code version (u8) + len of struct (u32) */
226#define CEPH_ENCODING_START_BLK_LEN 6
227
228/**
229 * ceph_start_encoding - start encoding block
230 * @struct_v: current (code) version of the encoding
231 * @struct_compat: oldest code version that can decode it
232 * @struct_len: length of struct encoding
233 */
234static inline void ceph_start_encoding(void **p, u8 struct_v, u8 struct_compat,
235 u32 struct_len)
236{
237 ceph_encode_8(p, struct_v);
238 ceph_encode_8(p, struct_compat);
239 ceph_encode_32(p, struct_len);
240}
241
242/**
243 * ceph_start_decoding - start decoding block
244 * @v: current version of the encoding that the code supports
245 * @name: name of the struct (free-form)
246 * @struct_v: out param for the encoding version
247 * @struct_len: out param for the length of struct encoding
248 *
249 * Validates the length of struct encoding, so unsafe ceph_decode_*
250 * variants can be used for decoding.
251 */
252static inline int ceph_start_decoding(void **p, void *end, u8 v,
253 const char *name, u8 *struct_v,
254 u32 *struct_len)
255{
256 u8 struct_compat;
257
258 ceph_decode_need(p, end, CEPH_ENCODING_START_BLK_LEN, bad);
259 *struct_v = ceph_decode_8(p);
260 struct_compat = ceph_decode_8(p);
261 if (v < struct_compat) {
262 pr_warn("got struct_v %d struct_compat %d > %d of %s\n",
263 *struct_v, struct_compat, v, name);
264 return -EINVAL;
265 }
266
267 *struct_len = ceph_decode_32(p);
268 ceph_decode_need(p, end, *struct_len, bad);
269 return 0;
270
271bad:
272 return -ERANGE;
273}
274
Alex Elderdd5f0492012-11-01 08:39:27 -0500275#define ceph_encode_need(p, end, n, bad) \
276 do { \
277 if (!likely(ceph_has_room(p, end, n))) \
278 goto bad; \
Sage Weilc7e337d2010-02-02 16:11:19 -0800279 } while (0)
280
281#define ceph_encode_64_safe(p, end, v, bad) \
282 do { \
283 ceph_encode_need(p, end, sizeof(u64), bad); \
284 ceph_encode_64(p, v); \
285 } while (0)
286#define ceph_encode_32_safe(p, end, v, bad) \
287 do { \
288 ceph_encode_need(p, end, sizeof(u32), bad); \
Alex Elderdd5f0492012-11-01 08:39:27 -0500289 ceph_encode_32(p, v); \
Sage Weilc7e337d2010-02-02 16:11:19 -0800290 } while (0)
291#define ceph_encode_16_safe(p, end, v, bad) \
292 do { \
293 ceph_encode_need(p, end, sizeof(u16), bad); \
Alex Elderdd5f0492012-11-01 08:39:27 -0500294 ceph_encode_16(p, v); \
295 } while (0)
296#define ceph_encode_8_safe(p, end, v, bad) \
297 do { \
298 ceph_encode_need(p, end, sizeof(u8), bad); \
299 ceph_encode_8(p, v); \
Sage Weilc7e337d2010-02-02 16:11:19 -0800300 } while (0)
301
302#define ceph_encode_copy_safe(p, end, pv, n, bad) \
303 do { \
304 ceph_encode_need(p, end, n, bad); \
305 ceph_encode_copy(p, pv, n); \
306 } while (0)
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700307#define ceph_encode_string_safe(p, end, s, n, bad) \
308 do { \
309 ceph_encode_need(p, end, n, bad); \
310 ceph_encode_string(p, end, s, n); \
311 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -0800312
Sage Weil31b80062009-10-06 11:31:13 -0700313
314#endif