1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __CEPH_DECODE_H
3 #define __CEPH_DECODE_H
7 #include <linux/slab.h>
8 #include <linux/time.h>
9 #include <asm/unaligned.h>
11 #include <linux/ceph/types.h>
15 * void **p pointer to position pointer
16 * void *end pointer to end of buffer (last byte + 1)
19 static inline u64
ceph_decode_64(void **p
)
21 u64 v
= get_unaligned_le64(*p
);
25 static inline u32
ceph_decode_32(void **p
)
27 u32 v
= get_unaligned_le32(*p
);
31 static inline u16
ceph_decode_16(void **p
)
33 u16 v
= get_unaligned_le16(*p
);
37 static inline u8
ceph_decode_8(void **p
)
43 static inline void ceph_decode_copy(void **p
, void *pv
, size_t n
)
52 static inline bool ceph_has_room(void **p
, void *end
, size_t n
)
54 return end
>= *p
&& n
<= end
- *p
;
57 #define ceph_decode_need(p, end, n, bad) \
59 if (!likely(ceph_has_room(p, end, n))) \
63 #define ceph_decode_64_safe(p, end, v, bad) \
65 ceph_decode_need(p, end, sizeof(u64), bad); \
66 v = ceph_decode_64(p); \
68 #define ceph_decode_32_safe(p, end, v, bad) \
70 ceph_decode_need(p, end, sizeof(u32), bad); \
71 v = ceph_decode_32(p); \
73 #define ceph_decode_16_safe(p, end, v, bad) \
75 ceph_decode_need(p, end, sizeof(u16), bad); \
76 v = ceph_decode_16(p); \
78 #define ceph_decode_8_safe(p, end, v, bad) \
80 ceph_decode_need(p, end, sizeof(u8), bad); \
81 v = ceph_decode_8(p); \
84 #define ceph_decode_copy_safe(p, end, pv, n, bad) \
86 ceph_decode_need(p, end, n, bad); \
87 ceph_decode_copy(p, pv, n); \
91 * Allocate a buffer big enough to hold the wire-encoded string, and
92 * decode the string into it. The resulting string will always be
93 * terminated with '\0'. If successful, *p will be advanced
94 * past the decoded data. Also, if lenp is not a null pointer, the
95 * length (not including the terminating '\0') will be recorded in
96 * *lenp. Note that a zero-length string is a valid return value.
98 * Returns a pointer to the newly-allocated string buffer, or a
99 * pointer-coded errno if an error occurs. Neither *p nor *lenp
100 * will have been updated if an error is returned.
102 * There are two possible failures:
103 * - converting the string would require accessing memory at or
104 * beyond the "end" pointer provided (-ERANGE)
105 * - memory could not be allocated for the result (-ENOMEM)
107 static inline char *ceph_extract_encoded_string(void **p
, void *end
,
108 size_t *lenp
, gfp_t gfp
)
114 ceph_decode_32_safe(&sp
, end
, len
, bad
);
115 if (!ceph_has_room(&sp
, end
, len
))
118 buf
= kmalloc(len
+ 1, gfp
);
120 return ERR_PTR(-ENOMEM
);
123 memcpy(buf
, sp
, len
);
126 *p
= (char *) *p
+ sizeof (u32
) + len
;
128 *lenp
= (size_t) len
;
133 return ERR_PTR(-ERANGE
);
139 #define ceph_decode_skip_n(p, end, n, bad) \
141 ceph_decode_need(p, end, n, bad); \
145 #define ceph_decode_skip_64(p, end, bad) \
146 ceph_decode_skip_n(p, end, sizeof(u64), bad)
148 #define ceph_decode_skip_32(p, end, bad) \
149 ceph_decode_skip_n(p, end, sizeof(u32), bad)
151 #define ceph_decode_skip_16(p, end, bad) \
152 ceph_decode_skip_n(p, end, sizeof(u16), bad)
154 #define ceph_decode_skip_8(p, end, bad) \
155 ceph_decode_skip_n(p, end, sizeof(u8), bad)
157 #define ceph_decode_skip_string(p, end, bad) \
161 ceph_decode_32_safe(p, end, len, bad); \
162 ceph_decode_skip_n(p, end, len, bad); \
165 #define ceph_decode_skip_set(p, end, type, bad) \
169 ceph_decode_32_safe(p, end, len, bad); \
171 ceph_decode_skip_##type(p, end, bad); \
174 #define ceph_decode_skip_map(p, end, ktype, vtype, bad) \
178 ceph_decode_32_safe(p, end, len, bad); \
180 ceph_decode_skip_##ktype(p, end, bad); \
181 ceph_decode_skip_##vtype(p, end, bad); \
185 #define ceph_decode_skip_map_of_map(p, end, ktype1, ktype2, vtype2, bad) \
189 ceph_decode_32_safe(p, end, len, bad); \
191 ceph_decode_skip_##ktype1(p, end, bad); \
192 ceph_decode_skip_map(p, end, ktype2, vtype2, bad); \
197 * struct ceph_timespec <-> struct timespec
199 static inline void ceph_decode_timespec(struct timespec
*ts
,
200 const struct ceph_timespec
*tv
)
202 ts
->tv_sec
= (__kernel_time_t
)le32_to_cpu(tv
->tv_sec
);
203 ts
->tv_nsec
= (long)le32_to_cpu(tv
->tv_nsec
);
205 static inline void ceph_encode_timespec(struct ceph_timespec
*tv
,
206 const struct timespec
*ts
)
208 tv
->tv_sec
= cpu_to_le32((u32
)ts
->tv_sec
);
209 tv
->tv_nsec
= cpu_to_le32((u32
)ts
->tv_nsec
);
213 * sockaddr_storage <-> ceph_sockaddr
215 static inline void ceph_encode_addr(struct ceph_entity_addr
*a
)
217 __be16 ss_family
= htons(a
->in_addr
.ss_family
);
218 a
->in_addr
.ss_family
= *(__u16
*)&ss_family
;
220 static inline void ceph_decode_addr(struct ceph_entity_addr
*a
)
222 __be16 ss_family
= *(__be16
*)&a
->in_addr
.ss_family
;
223 a
->in_addr
.ss_family
= ntohs(ss_family
);
224 WARN_ON(a
->in_addr
.ss_family
== 512);
230 static inline void ceph_encode_64(void **p
, u64 v
)
232 put_unaligned_le64(v
, (__le64
*)*p
);
235 static inline void ceph_encode_32(void **p
, u32 v
)
237 put_unaligned_le32(v
, (__le32
*)*p
);
240 static inline void ceph_encode_16(void **p
, u16 v
)
242 put_unaligned_le16(v
, (__le16
*)*p
);
245 static inline void ceph_encode_8(void **p
, u8 v
)
250 static inline void ceph_encode_copy(void **p
, const void *s
, int len
)
257 * filepath, string encoders
259 static inline void ceph_encode_filepath(void **p
, void *end
,
260 u64 ino
, const char *path
)
262 u32 len
= path
? strlen(path
) : 0;
263 BUG_ON(*p
+ 1 + sizeof(ino
) + sizeof(len
) + len
> end
);
265 ceph_encode_64(p
, ino
);
266 ceph_encode_32(p
, len
);
268 memcpy(*p
, path
, len
);
272 static inline void ceph_encode_string(void **p
, void *end
,
273 const char *s
, u32 len
)
275 BUG_ON(*p
+ sizeof(len
) + len
> end
);
276 ceph_encode_32(p
, len
);
283 * version and length starting block encoders/decoders
286 /* current code version (u8) + compat code version (u8) + len of struct (u32) */
287 #define CEPH_ENCODING_START_BLK_LEN 6
290 * ceph_start_encoding - start encoding block
291 * @struct_v: current (code) version of the encoding
292 * @struct_compat: oldest code version that can decode it
293 * @struct_len: length of struct encoding
295 static inline void ceph_start_encoding(void **p
, u8 struct_v
, u8 struct_compat
,
298 ceph_encode_8(p
, struct_v
);
299 ceph_encode_8(p
, struct_compat
);
300 ceph_encode_32(p
, struct_len
);
304 * ceph_start_decoding - start decoding block
305 * @v: current version of the encoding that the code supports
306 * @name: name of the struct (free-form)
307 * @struct_v: out param for the encoding version
308 * @struct_len: out param for the length of struct encoding
310 * Validates the length of struct encoding, so unsafe ceph_decode_*
311 * variants can be used for decoding.
313 static inline int ceph_start_decoding(void **p
, void *end
, u8 v
,
314 const char *name
, u8
*struct_v
,
319 ceph_decode_need(p
, end
, CEPH_ENCODING_START_BLK_LEN
, bad
);
320 *struct_v
= ceph_decode_8(p
);
321 struct_compat
= ceph_decode_8(p
);
322 if (v
< struct_compat
) {
323 pr_warn("got struct_v %d struct_compat %d > %d of %s\n",
324 *struct_v
, struct_compat
, v
, name
);
328 *struct_len
= ceph_decode_32(p
);
329 ceph_decode_need(p
, end
, *struct_len
, bad
);
336 #define ceph_encode_need(p, end, n, bad) \
338 if (!likely(ceph_has_room(p, end, n))) \
342 #define ceph_encode_64_safe(p, end, v, bad) \
344 ceph_encode_need(p, end, sizeof(u64), bad); \
345 ceph_encode_64(p, v); \
347 #define ceph_encode_32_safe(p, end, v, bad) \
349 ceph_encode_need(p, end, sizeof(u32), bad); \
350 ceph_encode_32(p, v); \
352 #define ceph_encode_16_safe(p, end, v, bad) \
354 ceph_encode_need(p, end, sizeof(u16), bad); \
355 ceph_encode_16(p, v); \
357 #define ceph_encode_8_safe(p, end, v, bad) \
359 ceph_encode_need(p, end, sizeof(u8), bad); \
360 ceph_encode_8(p, v); \
363 #define ceph_encode_copy_safe(p, end, pv, n, bad) \
365 ceph_encode_need(p, end, n, bad); \
366 ceph_encode_copy(p, pv, n); \
368 #define ceph_encode_string_safe(p, end, s, n, bad) \
370 ceph_encode_need(p, end, n, bad); \
371 ceph_encode_string(p, end, s, n); \