1 #ifndef __CEPH_DECODE_H
2 #define __CEPH_DECODE_H
6 #include <linux/time.h>
7 #include <asm/unaligned.h>
9 #include <linux/ceph/types.h>
11 /* This seemed to be the easiest place to define these */
13 #define U8_MAX ((u8)(~0U))
14 #define U16_MAX ((u16)(~0U))
15 #define U32_MAX ((u32)(~0U))
16 #define U64_MAX ((u64)(~0ULL))
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))
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))
30 * void **p pointer to position pointer
31 * void *end pointer to end of buffer (last byte + 1)
34 static inline u64 ceph_decode_64(void **p)
36 u64 v = get_unaligned_le64(*p);
40 static inline u32 ceph_decode_32(void **p)
42 u32 v = get_unaligned_le32(*p);
46 static inline u16 ceph_decode_16(void **p)
48 u16 v = get_unaligned_le16(*p);
52 static inline u8 ceph_decode_8(void **p)
58 static inline void ceph_decode_copy(void **p, void *pv, size_t n)
67 static inline int ceph_has_room(void **p, void *end, size_t n)
69 return end >= *p && n <= end - *p;
72 #define ceph_decode_need(p, end, n, bad) \
74 if (!likely(ceph_has_room(p, end, n))) \
78 #define ceph_decode_64_safe(p, end, v, bad) \
80 ceph_decode_need(p, end, sizeof(u64), bad); \
81 v = ceph_decode_64(p); \
83 #define ceph_decode_32_safe(p, end, v, bad) \
85 ceph_decode_need(p, end, sizeof(u32), bad); \
86 v = ceph_decode_32(p); \
88 #define ceph_decode_16_safe(p, end, v, bad) \
90 ceph_decode_need(p, end, sizeof(u16), bad); \
91 v = ceph_decode_16(p); \
93 #define ceph_decode_8_safe(p, end, v, bad) \
95 ceph_decode_need(p, end, sizeof(u8), bad); \
96 v = ceph_decode_8(p); \
99 #define ceph_decode_copy_safe(p, end, pv, n, bad) \
101 ceph_decode_need(p, end, n, bad); \
102 ceph_decode_copy(p, pv, n); \
106 * 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.
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.
117 * There are two possible failures:
118 * - converting the string would require accessing memory at or
119 * beyond the "end" pointer provided (-ERANGE)
120 * - memory could not be allocated for the result (-ENOMEM)
122 static inline char *ceph_extract_encoded_string(void **p, void *end,
123 size_t *lenp, gfp_t gfp)
129 ceph_decode_32_safe(&sp, end, len, bad);
130 if (!ceph_has_room(&sp, end, len))
133 buf = kmalloc(len + 1, gfp);
135 return ERR_PTR(-ENOMEM);
138 memcpy(buf, sp, len);
141 *p = (char *) *p + sizeof (u32) + len;
143 *lenp = (size_t) len;
148 return ERR_PTR(-ERANGE);
152 * struct ceph_timespec <-> struct timespec
154 static inline void ceph_decode_timespec(struct timespec *ts,
155 const struct ceph_timespec *tv)
157 ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
158 ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
160 static inline void ceph_encode_timespec(struct ceph_timespec *tv,
161 const struct timespec *ts)
163 tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
164 tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
168 * sockaddr_storage <-> ceph_sockaddr
170 static inline void ceph_encode_addr(struct ceph_entity_addr *a)
172 __be16 ss_family = htons(a->in_addr.ss_family);
173 a->in_addr.ss_family = *(__u16 *)&ss_family;
175 static inline void ceph_decode_addr(struct ceph_entity_addr *a)
177 __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
178 a->in_addr.ss_family = ntohs(ss_family);
179 WARN_ON(a->in_addr.ss_family == 512);
185 static inline void ceph_encode_64(void **p, u64 v)
187 put_unaligned_le64(v, (__le64 *)*p);
190 static inline void ceph_encode_32(void **p, u32 v)
192 put_unaligned_le32(v, (__le32 *)*p);
195 static inline void ceph_encode_16(void **p, u16 v)
197 put_unaligned_le16(v, (__le16 *)*p);
200 static inline void ceph_encode_8(void **p, u8 v)
205 static inline void ceph_encode_copy(void **p, const void *s, int len)
212 * filepath, string encoders
214 static inline void ceph_encode_filepath(void **p, void *end,
215 u64 ino, const char *path)
217 u32 len = path ? strlen(path) : 0;
218 BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
220 ceph_encode_64(p, ino);
221 ceph_encode_32(p, len);
223 memcpy(*p, path, len);
227 static inline void ceph_encode_string(void **p, void *end,
228 const char *s, u32 len)
230 BUG_ON(*p + sizeof(len) + len > end);
231 ceph_encode_32(p, len);
237 #define ceph_encode_need(p, end, n, bad) \
239 if (!likely(ceph_has_room(p, end, n))) \
243 #define ceph_encode_64_safe(p, end, v, bad) \
245 ceph_encode_need(p, end, sizeof(u64), bad); \
246 ceph_encode_64(p, v); \
248 #define ceph_encode_32_safe(p, end, v, bad) \
250 ceph_encode_need(p, end, sizeof(u32), bad); \
251 ceph_encode_32(p, v); \
253 #define ceph_encode_16_safe(p, end, v, bad) \
255 ceph_encode_need(p, end, sizeof(u16), bad); \
256 ceph_encode_16(p, v); \
258 #define ceph_encode_8_safe(p, end, v, bad) \
260 ceph_encode_need(p, end, sizeof(u8), bad); \
261 ceph_encode_8(p, v); \
264 #define ceph_encode_copy_safe(p, end, pv, n, bad) \
266 ceph_encode_need(p, end, n, bad); \
267 ceph_encode_copy(p, pv, n); \
269 #define ceph_encode_string_safe(p, end, s, n, bad) \
271 ceph_encode_need(p, end, n, bad); \
272 ceph_encode_string(p, end, s, n); \