blob: 220ae21e819b1fb2623d19d8cf4f619862f11c42 [file] [log] [blame]
Sage Weil31b80062009-10-06 11:31:13 -07001#ifndef __CEPH_DECODE_H
2#define __CEPH_DECODE_H
3
Paul Gortmaker187f1882011-11-23 20:12:59 -05004#include <linux/bug.h>
Sage Weilc7e337d2010-02-02 16:11:19 -08005#include <linux/time.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -05006#include <asm/unaligned.h>
Sage Weil31b80062009-10-06 11:31:13 -07007
Sage Weilc89136e2009-10-14 09:59:09 -07008#include "types.h"
9
Sage Weil31b80062009-10-06 11:31:13 -070010/*
11 * in all cases,
12 * void **p pointer to position pointer
13 * void *end pointer to end of buffer (last byte + 1)
14 */
15
Sage Weilc89136e2009-10-14 09:59:09 -070016static inline u64 ceph_decode_64(void **p)
17{
18 u64 v = get_unaligned_le64(*p);
19 *p += sizeof(u64);
20 return v;
21}
22static inline u32 ceph_decode_32(void **p)
23{
24 u32 v = get_unaligned_le32(*p);
25 *p += sizeof(u32);
26 return v;
27}
28static inline u16 ceph_decode_16(void **p)
29{
30 u16 v = get_unaligned_le16(*p);
31 *p += sizeof(u16);
32 return v;
33}
34static inline u8 ceph_decode_8(void **p)
35{
36 u8 v = *(u8 *)*p;
37 (*p)++;
38 return v;
39}
40static inline void ceph_decode_copy(void **p, void *pv, size_t n)
41{
42 memcpy(pv, *p, n);
43 *p += n;
44}
45
Sage Weil31b80062009-10-06 11:31:13 -070046/*
47 * bounds check input.
48 */
49#define ceph_decode_need(p, end, n, bad) \
50 do { \
51 if (unlikely(*(p) + (n) > (end))) \
52 goto bad; \
53 } while (0)
54
Sage Weil31b80062009-10-06 11:31:13 -070055#define ceph_decode_64_safe(p, end, v, bad) \
56 do { \
57 ceph_decode_need(p, end, sizeof(u64), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070058 v = ceph_decode_64(p); \
Sage Weil31b80062009-10-06 11:31:13 -070059 } while (0)
60#define ceph_decode_32_safe(p, end, v, bad) \
61 do { \
62 ceph_decode_need(p, end, sizeof(u32), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070063 v = ceph_decode_32(p); \
Sage Weil31b80062009-10-06 11:31:13 -070064 } while (0)
65#define ceph_decode_16_safe(p, end, v, bad) \
66 do { \
67 ceph_decode_need(p, end, sizeof(u16), bad); \
Sage Weilc89136e2009-10-14 09:59:09 -070068 v = ceph_decode_16(p); \
Sage Weil31b80062009-10-06 11:31:13 -070069 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -080070#define ceph_decode_8_safe(p, end, v, bad) \
71 do { \
72 ceph_decode_need(p, end, sizeof(u8), bad); \
73 v = ceph_decode_8(p); \
74 } while (0)
Sage Weil31b80062009-10-06 11:31:13 -070075
76#define ceph_decode_copy_safe(p, end, pv, n, bad) \
77 do { \
78 ceph_decode_need(p, end, n, bad); \
79 ceph_decode_copy(p, pv, n); \
80 } while (0)
81
82/*
83 * struct ceph_timespec <-> struct timespec
84 */
Sage Weilc89136e2009-10-14 09:59:09 -070085static inline void ceph_decode_timespec(struct timespec *ts,
Sage Weil63f2d212009-11-03 15:17:56 -080086 const struct ceph_timespec *tv)
Sage Weilc89136e2009-10-14 09:59:09 -070087{
88 ts->tv_sec = le32_to_cpu(tv->tv_sec);
89 ts->tv_nsec = le32_to_cpu(tv->tv_nsec);
90}
91static inline void ceph_encode_timespec(struct ceph_timespec *tv,
Sage Weil63f2d212009-11-03 15:17:56 -080092 const struct timespec *ts)
Sage Weilc89136e2009-10-14 09:59:09 -070093{
94 tv->tv_sec = cpu_to_le32(ts->tv_sec);
95 tv->tv_nsec = cpu_to_le32(ts->tv_nsec);
96}
Sage Weil31b80062009-10-06 11:31:13 -070097
98/*
Sage Weil63f2d212009-11-03 15:17:56 -080099 * sockaddr_storage <-> ceph_sockaddr
100 */
101static inline void ceph_encode_addr(struct ceph_entity_addr *a)
102{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700103 __be16 ss_family = htons(a->in_addr.ss_family);
104 a->in_addr.ss_family = *(__u16 *)&ss_family;
Sage Weil63f2d212009-11-03 15:17:56 -0800105}
106static inline void ceph_decode_addr(struct ceph_entity_addr *a)
107{
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700108 __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
109 a->in_addr.ss_family = ntohs(ss_family);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800110 WARN_ON(a->in_addr.ss_family == 512);
Sage Weil63f2d212009-11-03 15:17:56 -0800111}
112
113/*
Sage Weil31b80062009-10-06 11:31:13 -0700114 * encoders
115 */
Sage Weilc89136e2009-10-14 09:59:09 -0700116static inline void ceph_encode_64(void **p, u64 v)
117{
118 put_unaligned_le64(v, (__le64 *)*p);
119 *p += sizeof(u64);
120}
121static inline void ceph_encode_32(void **p, u32 v)
122{
123 put_unaligned_le32(v, (__le32 *)*p);
124 *p += sizeof(u32);
125}
126static inline void ceph_encode_16(void **p, u16 v)
127{
128 put_unaligned_le16(v, (__le16 *)*p);
129 *p += sizeof(u16);
130}
131static inline void ceph_encode_8(void **p, u8 v)
132{
133 *(u8 *)*p = v;
134 (*p)++;
135}
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800136static inline void ceph_encode_copy(void **p, const void *s, int len)
137{
138 memcpy(*p, s, len);
139 *p += len;
140}
Sage Weil31b80062009-10-06 11:31:13 -0700141
142/*
143 * filepath, string encoders
144 */
145static inline void ceph_encode_filepath(void **p, void *end,
146 u64 ino, const char *path)
147{
148 u32 len = path ? strlen(path) : 0;
149 BUG_ON(*p + sizeof(ino) + sizeof(len) + len > end);
Sage Weilac8839d2010-01-27 14:28:10 -0800150 ceph_encode_8(p, 1);
Sage Weil31b80062009-10-06 11:31:13 -0700151 ceph_encode_64(p, ino);
152 ceph_encode_32(p, len);
153 if (len)
154 memcpy(*p, path, len);
155 *p += len;
156}
157
158static inline void ceph_encode_string(void **p, void *end,
159 const char *s, u32 len)
160{
161 BUG_ON(*p + sizeof(len) + len > end);
162 ceph_encode_32(p, len);
163 if (len)
164 memcpy(*p, s, len);
165 *p += len;
166}
167
Sage Weilc7e337d2010-02-02 16:11:19 -0800168#define ceph_encode_need(p, end, n, bad) \
169 do { \
170 if (unlikely(*(p) + (n) > (end))) \
171 goto bad; \
172 } while (0)
173
174#define ceph_encode_64_safe(p, end, v, bad) \
175 do { \
176 ceph_encode_need(p, end, sizeof(u64), bad); \
177 ceph_encode_64(p, v); \
178 } while (0)
179#define ceph_encode_32_safe(p, end, v, bad) \
180 do { \
181 ceph_encode_need(p, end, sizeof(u32), bad); \
182 ceph_encode_32(p, v); \
183 } while (0)
184#define ceph_encode_16_safe(p, end, v, bad) \
185 do { \
186 ceph_encode_need(p, end, sizeof(u16), bad); \
187 ceph_encode_16(p, v); \
188 } while (0)
189
190#define ceph_encode_copy_safe(p, end, pv, n, bad) \
191 do { \
192 ceph_encode_need(p, end, n, bad); \
193 ceph_encode_copy(p, pv, n); \
194 } while (0)
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700195#define ceph_encode_string_safe(p, end, s, n, bad) \
196 do { \
197 ceph_encode_need(p, end, n, bad); \
198 ceph_encode_string(p, end, s, n); \
199 } while (0)
Sage Weilc7e337d2010-02-02 16:11:19 -0800200
Sage Weil31b80062009-10-06 11:31:13 -0700201
202#endif