at v4.13 9.2 kB view raw
1#ifndef __CEPH_DECODE_H 2#define __CEPH_DECODE_H 3 4#include <linux/err.h> 5#include <linux/bug.h> 6#include <linux/slab.h> 7#include <linux/time.h> 8#include <asm/unaligned.h> 9 10#include <linux/ceph/types.h> 11 12/* 13 * in all cases, 14 * void **p pointer to position pointer 15 * void *end pointer to end of buffer (last byte + 1) 16 */ 17 18static 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 48/* 49 * bounds check input. 50 */ 51static inline bool ceph_has_room(void **p, void *end, size_t n) 52{ 53 return end >= *p && n <= end - *p; 54} 55 56#define ceph_decode_need(p, end, n, bad) \ 57 do { \ 58 if (!likely(ceph_has_room(p, end, n))) \ 59 goto bad; \ 60 } while (0) 61 62#define ceph_decode_64_safe(p, end, v, bad) \ 63 do { \ 64 ceph_decode_need(p, end, sizeof(u64), bad); \ 65 v = ceph_decode_64(p); \ 66 } while (0) 67#define ceph_decode_32_safe(p, end, v, bad) \ 68 do { \ 69 ceph_decode_need(p, end, sizeof(u32), bad); \ 70 v = ceph_decode_32(p); \ 71 } while (0) 72#define ceph_decode_16_safe(p, end, v, bad) \ 73 do { \ 74 ceph_decode_need(p, end, sizeof(u16), bad); \ 75 v = ceph_decode_16(p); \ 76 } while (0) 77#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) 82 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/* 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. 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 103 * beyond the "end" pointer provided (-ERANGE) 104 * - memory could not be allocated for the result (-ENOMEM) 105 */ 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/* 136 * skip helpers 137 */ 138#define ceph_decode_skip_n(p, end, n, bad) \ 139 do { \ 140 ceph_decode_need(p, end, n, bad); \ 141 *p += n; \ 142 } while (0) 143 144#define ceph_decode_skip_64(p, end, bad) \ 145ceph_decode_skip_n(p, end, sizeof(u64), bad) 146 147#define ceph_decode_skip_32(p, end, bad) \ 148ceph_decode_skip_n(p, end, sizeof(u32), bad) 149 150#define ceph_decode_skip_16(p, end, bad) \ 151ceph_decode_skip_n(p, end, sizeof(u16), bad) 152 153#define ceph_decode_skip_8(p, end, bad) \ 154ceph_decode_skip_n(p, end, sizeof(u8), bad) 155 156#define ceph_decode_skip_string(p, end, bad) \ 157 do { \ 158 u32 len; \ 159 \ 160 ceph_decode_32_safe(p, end, len, bad); \ 161 ceph_decode_skip_n(p, end, len, bad); \ 162 } while (0) 163 164#define ceph_decode_skip_set(p, end, type, bad) \ 165 do { \ 166 u32 len; \ 167 \ 168 ceph_decode_32_safe(p, end, len, bad); \ 169 while (len--) \ 170 ceph_decode_skip_##type(p, end, bad); \ 171 } while (0) 172 173#define ceph_decode_skip_map(p, end, ktype, vtype, bad) \ 174 do { \ 175 u32 len; \ 176 \ 177 ceph_decode_32_safe(p, end, len, bad); \ 178 while (len--) { \ 179 ceph_decode_skip_##ktype(p, end, bad); \ 180 ceph_decode_skip_##vtype(p, end, bad); \ 181 } \ 182 } while (0) 183 184#define ceph_decode_skip_map_of_map(p, end, ktype1, ktype2, vtype2, bad) \ 185 do { \ 186 u32 len; \ 187 \ 188 ceph_decode_32_safe(p, end, len, bad); \ 189 while (len--) { \ 190 ceph_decode_skip_##ktype1(p, end, bad); \ 191 ceph_decode_skip_map(p, end, ktype2, vtype2, bad); \ 192 } \ 193 } while (0) 194 195/* 196 * struct ceph_timespec <-> struct timespec 197 */ 198static inline void ceph_decode_timespec(struct timespec *ts, 199 const struct ceph_timespec *tv) 200{ 201 ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec); 202 ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec); 203} 204static inline void ceph_encode_timespec(struct ceph_timespec *tv, 205 const struct timespec *ts) 206{ 207 tv->tv_sec = cpu_to_le32((u32)ts->tv_sec); 208 tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec); 209} 210 211/* 212 * sockaddr_storage <-> ceph_sockaddr 213 */ 214static inline void ceph_encode_addr(struct ceph_entity_addr *a) 215{ 216 __be16 ss_family = htons(a->in_addr.ss_family); 217 a->in_addr.ss_family = *(__u16 *)&ss_family; 218} 219static inline void ceph_decode_addr(struct ceph_entity_addr *a) 220{ 221 __be16 ss_family = *(__be16 *)&a->in_addr.ss_family; 222 a->in_addr.ss_family = ntohs(ss_family); 223 WARN_ON(a->in_addr.ss_family == 512); 224} 225 226/* 227 * encoders 228 */ 229static inline void ceph_encode_64(void **p, u64 v) 230{ 231 put_unaligned_le64(v, (__le64 *)*p); 232 *p += sizeof(u64); 233} 234static inline void ceph_encode_32(void **p, u32 v) 235{ 236 put_unaligned_le32(v, (__le32 *)*p); 237 *p += sizeof(u32); 238} 239static inline void ceph_encode_16(void **p, u16 v) 240{ 241 put_unaligned_le16(v, (__le16 *)*p); 242 *p += sizeof(u16); 243} 244static inline void ceph_encode_8(void **p, u8 v) 245{ 246 *(u8 *)*p = v; 247 (*p)++; 248} 249static inline void ceph_encode_copy(void **p, const void *s, int len) 250{ 251 memcpy(*p, s, len); 252 *p += len; 253} 254 255/* 256 * filepath, string encoders 257 */ 258static inline void ceph_encode_filepath(void **p, void *end, 259 u64 ino, const char *path) 260{ 261 u32 len = path ? strlen(path) : 0; 262 BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end); 263 ceph_encode_8(p, 1); 264 ceph_encode_64(p, ino); 265 ceph_encode_32(p, len); 266 if (len) 267 memcpy(*p, path, len); 268 *p += len; 269} 270 271static inline void ceph_encode_string(void **p, void *end, 272 const char *s, u32 len) 273{ 274 BUG_ON(*p + sizeof(len) + len > end); 275 ceph_encode_32(p, len); 276 if (len) 277 memcpy(*p, s, len); 278 *p += len; 279} 280 281/* 282 * version and length starting block encoders/decoders 283 */ 284 285/* current code version (u8) + compat code version (u8) + len of struct (u32) */ 286#define CEPH_ENCODING_START_BLK_LEN 6 287 288/** 289 * ceph_start_encoding - start encoding block 290 * @struct_v: current (code) version of the encoding 291 * @struct_compat: oldest code version that can decode it 292 * @struct_len: length of struct encoding 293 */ 294static inline void ceph_start_encoding(void **p, u8 struct_v, u8 struct_compat, 295 u32 struct_len) 296{ 297 ceph_encode_8(p, struct_v); 298 ceph_encode_8(p, struct_compat); 299 ceph_encode_32(p, struct_len); 300} 301 302/** 303 * ceph_start_decoding - start decoding block 304 * @v: current version of the encoding that the code supports 305 * @name: name of the struct (free-form) 306 * @struct_v: out param for the encoding version 307 * @struct_len: out param for the length of struct encoding 308 * 309 * Validates the length of struct encoding, so unsafe ceph_decode_* 310 * variants can be used for decoding. 311 */ 312static inline int ceph_start_decoding(void **p, void *end, u8 v, 313 const char *name, u8 *struct_v, 314 u32 *struct_len) 315{ 316 u8 struct_compat; 317 318 ceph_decode_need(p, end, CEPH_ENCODING_START_BLK_LEN, bad); 319 *struct_v = ceph_decode_8(p); 320 struct_compat = ceph_decode_8(p); 321 if (v < struct_compat) { 322 pr_warn("got struct_v %d struct_compat %d > %d of %s\n", 323 *struct_v, struct_compat, v, name); 324 return -EINVAL; 325 } 326 327 *struct_len = ceph_decode_32(p); 328 ceph_decode_need(p, end, *struct_len, bad); 329 return 0; 330 331bad: 332 return -ERANGE; 333} 334 335#define ceph_encode_need(p, end, n, bad) \ 336 do { \ 337 if (!likely(ceph_has_room(p, end, n))) \ 338 goto bad; \ 339 } while (0) 340 341#define ceph_encode_64_safe(p, end, v, bad) \ 342 do { \ 343 ceph_encode_need(p, end, sizeof(u64), bad); \ 344 ceph_encode_64(p, v); \ 345 } while (0) 346#define ceph_encode_32_safe(p, end, v, bad) \ 347 do { \ 348 ceph_encode_need(p, end, sizeof(u32), bad); \ 349 ceph_encode_32(p, v); \ 350 } while (0) 351#define ceph_encode_16_safe(p, end, v, bad) \ 352 do { \ 353 ceph_encode_need(p, end, sizeof(u16), bad); \ 354 ceph_encode_16(p, v); \ 355 } while (0) 356#define ceph_encode_8_safe(p, end, v, bad) \ 357 do { \ 358 ceph_encode_need(p, end, sizeof(u8), bad); \ 359 ceph_encode_8(p, v); \ 360 } while (0) 361 362#define ceph_encode_copy_safe(p, end, pv, n, bad) \ 363 do { \ 364 ceph_encode_need(p, end, n, bad); \ 365 ceph_encode_copy(p, pv, n); \ 366 } while (0) 367#define ceph_encode_string_safe(p, end, s, n, bad) \ 368 do { \ 369 ceph_encode_need(p, end, n, bad); \ 370 ceph_encode_string(p, end, s, n); \ 371 } while (0) 372 373 374#endif