1 #ifndef __CEPH_DECODE_H
2 #define __CEPH_DECODE_H
6 #include <linux/slab.h>
7 #include <linux/time.h>
8 #include <asm/unaligned.h>
10 #include <linux/ceph/types.h>
14 * void **p pointer to position pointer
15 * void *end pointer to end of buffer (last byte + 1)
18 static inline u64
ceph_decode_64(void **p
)
20 u64 v
= get_unaligned_le64(*p
);
24 static inline u32
ceph_decode_32(void **p
)
26 u32 v
= get_unaligned_le32(*p
);
30 static inline u16
ceph_decode_16(void **p
)
32 u16 v
= get_unaligned_le16(*p
);
36 static inline u8
ceph_decode_8(void **p
)
42 static inline void ceph_decode_copy(void **p
, void *pv
, size_t n
)
51 static inline bool ceph_has_room(void **p
, void *end
, size_t n
)
53 return end
>= *p
&& n
<= end
- *p
;
56 #define ceph_decode_need(p, end, n, bad) \
58 if (!likely(ceph_has_room(p, end, n))) \
62 #define ceph_decode_64_safe(p, end, v, bad) \
64 ceph_decode_need(p, end, sizeof(u64), bad); \
65 v = ceph_decode_64(p); \
67 #define ceph_decode_32_safe(p, end, v, bad) \
69 ceph_decode_need(p, end, sizeof(u32), bad); \
70 v = ceph_decode_32(p); \
72 #define ceph_decode_16_safe(p, end, v, bad) \
74 ceph_decode_need(p, end, sizeof(u16), bad); \
75 v = ceph_decode_16(p); \
77 #define ceph_decode_8_safe(p, end, v, bad) \
79 ceph_decode_need(p, end, sizeof(u8), bad); \
80 v = ceph_decode_8(p); \
83 #define ceph_decode_copy_safe(p, end, pv, n, bad) \
85 ceph_decode_need(p, end, n, bad); \
86 ceph_decode_copy(p, pv, n); \
90 * 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.
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.
101 * There are two possible failures:
102 * - converting the string would require accessing memory at or
103 * beyond the "end" pointer provided (-ERANGE)
104 * - memory could not be allocated for the result (-ENOMEM)
106 static inline char *ceph_extract_encoded_string(void **p
, void *end
,
107 size_t *lenp
, gfp_t gfp
)
113 ceph_decode_32_safe(&sp
, end
, len
, bad
);
114 if (!ceph_has_room(&sp
, end
, len
))
117 buf
= kmalloc(len
+ 1, gfp
);
119 return ERR_PTR(-ENOMEM
);
122 memcpy(buf
, sp
, len
);
125 *p
= (char *) *p
+ sizeof (u32
) + len
;
127 *lenp
= (size_t) len
;
132 return ERR_PTR(-ERANGE
);
136 * struct ceph_timespec <-> struct timespec
138 static inline void ceph_decode_timespec(struct timespec
*ts
,
139 const struct ceph_timespec
*tv
)
141 ts
->tv_sec
= (__kernel_time_t
)le32_to_cpu(tv
->tv_sec
);
142 ts
->tv_nsec
= (long)le32_to_cpu(tv
->tv_nsec
);
144 static inline void ceph_encode_timespec(struct ceph_timespec
*tv
,
145 const struct timespec
*ts
)
147 tv
->tv_sec
= cpu_to_le32((u32
)ts
->tv_sec
);
148 tv
->tv_nsec
= cpu_to_le32((u32
)ts
->tv_nsec
);
152 * sockaddr_storage <-> ceph_sockaddr
154 static inline void ceph_encode_addr(struct ceph_entity_addr
*a
)
156 __be16 ss_family
= htons(a
->in_addr
.ss_family
);
157 a
->in_addr
.ss_family
= *(__u16
*)&ss_family
;
159 static inline void ceph_decode_addr(struct ceph_entity_addr
*a
)
161 __be16 ss_family
= *(__be16
*)&a
->in_addr
.ss_family
;
162 a
->in_addr
.ss_family
= ntohs(ss_family
);
163 WARN_ON(a
->in_addr
.ss_family
== 512);
169 static inline void ceph_encode_64(void **p
, u64 v
)
171 put_unaligned_le64(v
, (__le64
*)*p
);
174 static inline void ceph_encode_32(void **p
, u32 v
)
176 put_unaligned_le32(v
, (__le32
*)*p
);
179 static inline void ceph_encode_16(void **p
, u16 v
)
181 put_unaligned_le16(v
, (__le16
*)*p
);
184 static inline void ceph_encode_8(void **p
, u8 v
)
189 static inline void ceph_encode_copy(void **p
, const void *s
, int len
)
196 * filepath, string encoders
198 static inline void ceph_encode_filepath(void **p
, void *end
,
199 u64 ino
, const char *path
)
201 u32 len
= path
? strlen(path
) : 0;
202 BUG_ON(*p
+ 1 + sizeof(ino
) + sizeof(len
) + len
> end
);
204 ceph_encode_64(p
, ino
);
205 ceph_encode_32(p
, len
);
207 memcpy(*p
, path
, len
);
211 static inline void ceph_encode_string(void **p
, void *end
,
212 const char *s
, u32 len
)
214 BUG_ON(*p
+ sizeof(len
) + len
> end
);
215 ceph_encode_32(p
, len
);
222 * version and length starting block encoders/decoders
225 /* current code version (u8) + compat code version (u8) + len of struct (u32) */
226 #define CEPH_ENCODING_START_BLK_LEN 6
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
234 static inline void ceph_start_encoding(void **p
, u8 struct_v
, u8 struct_compat
,
237 ceph_encode_8(p
, struct_v
);
238 ceph_encode_8(p
, struct_compat
);
239 ceph_encode_32(p
, struct_len
);
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
249 * Validates the length of struct encoding, so unsafe ceph_decode_*
250 * variants can be used for decoding.
252 static inline int ceph_start_decoding(void **p
, void *end
, u8 v
,
253 const char *name
, u8
*struct_v
,
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
);
267 *struct_len
= ceph_decode_32(p
);
268 ceph_decode_need(p
, end
, *struct_len
, bad
);
275 #define ceph_encode_need(p, end, n, bad) \
277 if (!likely(ceph_has_room(p, end, n))) \
281 #define ceph_encode_64_safe(p, end, v, bad) \
283 ceph_encode_need(p, end, sizeof(u64), bad); \
284 ceph_encode_64(p, v); \
286 #define ceph_encode_32_safe(p, end, v, bad) \
288 ceph_encode_need(p, end, sizeof(u32), bad); \
289 ceph_encode_32(p, v); \
291 #define ceph_encode_16_safe(p, end, v, bad) \
293 ceph_encode_need(p, end, sizeof(u16), bad); \
294 ceph_encode_16(p, v); \
296 #define ceph_encode_8_safe(p, end, v, bad) \
298 ceph_encode_need(p, end, sizeof(u8), bad); \
299 ceph_encode_8(p, v); \
302 #define ceph_encode_copy_safe(p, end, pv, n, bad) \
304 ceph_encode_need(p, end, n, bad); \
305 ceph_encode_copy(p, pv, n); \
307 #define ceph_encode_string_safe(p, end, s, n, bad) \
309 ceph_encode_need(p, end, n, bad); \
310 ceph_encode_string(p, end, s, n); \