decode.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #ifndef __CEPH_DECODE_H
  2. #define __CEPH_DECODE_H
  3. #include <linux/err.h>
  4. #include <linux/bug.h>
  5. #include <linux/slab.h>
  6. #include <linux/time.h>
  7. #include <asm/unaligned.h>
  8. #include <linux/ceph/types.h>
  9. /*
  10. * in all cases,
  11. * void **p pointer to position pointer
  12. * void *end pointer to end of buffer (last byte + 1)
  13. */
  14. static inline u64 ceph_decode_64(void **p)
  15. {
  16. u64 v = get_unaligned_le64(*p);
  17. *p += sizeof(u64);
  18. return v;
  19. }
  20. static inline u32 ceph_decode_32(void **p)
  21. {
  22. u32 v = get_unaligned_le32(*p);
  23. *p += sizeof(u32);
  24. return v;
  25. }
  26. static inline u16 ceph_decode_16(void **p)
  27. {
  28. u16 v = get_unaligned_le16(*p);
  29. *p += sizeof(u16);
  30. return v;
  31. }
  32. static inline u8 ceph_decode_8(void **p)
  33. {
  34. u8 v = *(u8 *)*p;
  35. (*p)++;
  36. return v;
  37. }
  38. static inline void ceph_decode_copy(void **p, void *pv, size_t n)
  39. {
  40. memcpy(pv, *p, n);
  41. *p += n;
  42. }
  43. /*
  44. * bounds check input.
  45. */
  46. static inline bool ceph_has_room(void **p, void *end, size_t n)
  47. {
  48. return end >= *p && n <= end - *p;
  49. }
  50. #define ceph_decode_need(p, end, n, bad) \
  51. do { \
  52. if (!likely(ceph_has_room(p, end, n))) \
  53. goto bad; \
  54. } while (0)
  55. #define ceph_decode_64_safe(p, end, v, bad) \
  56. do { \
  57. ceph_decode_need(p, end, sizeof(u64), bad); \
  58. v = ceph_decode_64(p); \
  59. } while (0)
  60. #define ceph_decode_32_safe(p, end, v, bad) \
  61. do { \
  62. ceph_decode_need(p, end, sizeof(u32), bad); \
  63. v = ceph_decode_32(p); \
  64. } while (0)
  65. #define ceph_decode_16_safe(p, end, v, bad) \
  66. do { \
  67. ceph_decode_need(p, end, sizeof(u16), bad); \
  68. v = ceph_decode_16(p); \
  69. } while (0)
  70. #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)
  75. #define ceph_decode_copy_safe(p, end, pv, n, bad) \
  76. do { \
  77. ceph_decode_need(p, end, n, bad); \
  78. ceph_decode_copy(p, pv, n); \
  79. } while (0)
  80. /*
  81. * Allocate a buffer big enough to hold the wire-encoded string, and
  82. * decode the string into it. The resulting string will always be
  83. * terminated with '\0'. If successful, *p will be advanced
  84. * past the decoded data. Also, if lenp is not a null pointer, the
  85. * length (not including the terminating '\0') will be recorded in
  86. * *lenp. Note that a zero-length string is a valid return value.
  87. *
  88. * Returns a pointer to the newly-allocated string buffer, or a
  89. * pointer-coded errno if an error occurs. Neither *p nor *lenp
  90. * will have been updated if an error is returned.
  91. *
  92. * There are two possible failures:
  93. * - converting the string would require accessing memory at or
  94. * beyond the "end" pointer provided (-ERANGE)
  95. * - memory could not be allocated for the result (-ENOMEM)
  96. */
  97. static inline char *ceph_extract_encoded_string(void **p, void *end,
  98. size_t *lenp, gfp_t gfp)
  99. {
  100. u32 len;
  101. void *sp = *p;
  102. char *buf;
  103. ceph_decode_32_safe(&sp, end, len, bad);
  104. if (!ceph_has_room(&sp, end, len))
  105. goto bad;
  106. buf = kmalloc(len + 1, gfp);
  107. if (!buf)
  108. return ERR_PTR(-ENOMEM);
  109. if (len)
  110. memcpy(buf, sp, len);
  111. buf[len] = '\0';
  112. *p = (char *) *p + sizeof (u32) + len;
  113. if (lenp)
  114. *lenp = (size_t) len;
  115. return buf;
  116. bad:
  117. return ERR_PTR(-ERANGE);
  118. }
  119. /*
  120. * struct ceph_timespec <-> struct timespec
  121. */
  122. static inline void ceph_decode_timespec(struct timespec *ts,
  123. const struct ceph_timespec *tv)
  124. {
  125. ts->tv_sec = (__kernel_time_t)le32_to_cpu(tv->tv_sec);
  126. ts->tv_nsec = (long)le32_to_cpu(tv->tv_nsec);
  127. }
  128. static inline void ceph_encode_timespec(struct ceph_timespec *tv,
  129. const struct timespec *ts)
  130. {
  131. tv->tv_sec = cpu_to_le32((u32)ts->tv_sec);
  132. tv->tv_nsec = cpu_to_le32((u32)ts->tv_nsec);
  133. }
  134. /*
  135. * sockaddr_storage <-> ceph_sockaddr
  136. */
  137. static inline void ceph_encode_addr(struct ceph_entity_addr *a)
  138. {
  139. __be16 ss_family = htons(a->in_addr.ss_family);
  140. a->in_addr.ss_family = *(__u16 *)&ss_family;
  141. }
  142. static inline void ceph_decode_addr(struct ceph_entity_addr *a)
  143. {
  144. __be16 ss_family = *(__be16 *)&a->in_addr.ss_family;
  145. a->in_addr.ss_family = ntohs(ss_family);
  146. WARN_ON(a->in_addr.ss_family == 512);
  147. }
  148. /*
  149. * encoders
  150. */
  151. static inline void ceph_encode_64(void **p, u64 v)
  152. {
  153. put_unaligned_le64(v, (__le64 *)*p);
  154. *p += sizeof(u64);
  155. }
  156. static inline void ceph_encode_32(void **p, u32 v)
  157. {
  158. put_unaligned_le32(v, (__le32 *)*p);
  159. *p += sizeof(u32);
  160. }
  161. static inline void ceph_encode_16(void **p, u16 v)
  162. {
  163. put_unaligned_le16(v, (__le16 *)*p);
  164. *p += sizeof(u16);
  165. }
  166. static inline void ceph_encode_8(void **p, u8 v)
  167. {
  168. *(u8 *)*p = v;
  169. (*p)++;
  170. }
  171. static inline void ceph_encode_copy(void **p, const void *s, int len)
  172. {
  173. memcpy(*p, s, len);
  174. *p += len;
  175. }
  176. /*
  177. * filepath, string encoders
  178. */
  179. static inline void ceph_encode_filepath(void **p, void *end,
  180. u64 ino, const char *path)
  181. {
  182. u32 len = path ? strlen(path) : 0;
  183. BUG_ON(*p + 1 + sizeof(ino) + sizeof(len) + len > end);
  184. ceph_encode_8(p, 1);
  185. ceph_encode_64(p, ino);
  186. ceph_encode_32(p, len);
  187. if (len)
  188. memcpy(*p, path, len);
  189. *p += len;
  190. }
  191. static inline void ceph_encode_string(void **p, void *end,
  192. const char *s, u32 len)
  193. {
  194. BUG_ON(*p + sizeof(len) + len > end);
  195. ceph_encode_32(p, len);
  196. if (len)
  197. memcpy(*p, s, len);
  198. *p += len;
  199. }
  200. /*
  201. * version and length starting block encoders/decoders
  202. */
  203. /* current code version (u8) + compat code version (u8) + len of struct (u32) */
  204. #define CEPH_ENCODING_START_BLK_LEN 6
  205. /**
  206. * ceph_start_encoding - start encoding block
  207. * @struct_v: current (code) version of the encoding
  208. * @struct_compat: oldest code version that can decode it
  209. * @struct_len: length of struct encoding
  210. */
  211. static inline void ceph_start_encoding(void **p, u8 struct_v, u8 struct_compat,
  212. u32 struct_len)
  213. {
  214. ceph_encode_8(p, struct_v);
  215. ceph_encode_8(p, struct_compat);
  216. ceph_encode_32(p, struct_len);
  217. }
  218. /**
  219. * ceph_start_decoding - start decoding block
  220. * @v: current version of the encoding that the code supports
  221. * @name: name of the struct (free-form)
  222. * @struct_v: out param for the encoding version
  223. * @struct_len: out param for the length of struct encoding
  224. *
  225. * Validates the length of struct encoding, so unsafe ceph_decode_*
  226. * variants can be used for decoding.
  227. */
  228. static inline int ceph_start_decoding(void **p, void *end, u8 v,
  229. const char *name, u8 *struct_v,
  230. u32 *struct_len)
  231. {
  232. u8 struct_compat;
  233. ceph_decode_need(p, end, CEPH_ENCODING_START_BLK_LEN, bad);
  234. *struct_v = ceph_decode_8(p);
  235. struct_compat = ceph_decode_8(p);
  236. if (v < struct_compat) {
  237. pr_warn("got struct_v %d struct_compat %d > %d of %s\n",
  238. *struct_v, struct_compat, v, name);
  239. return -EINVAL;
  240. }
  241. *struct_len = ceph_decode_32(p);
  242. ceph_decode_need(p, end, *struct_len, bad);
  243. return 0;
  244. bad:
  245. return -ERANGE;
  246. }
  247. #define ceph_encode_need(p, end, n, bad) \
  248. do { \
  249. if (!likely(ceph_has_room(p, end, n))) \
  250. goto bad; \
  251. } while (0)
  252. #define ceph_encode_64_safe(p, end, v, bad) \
  253. do { \
  254. ceph_encode_need(p, end, sizeof(u64), bad); \
  255. ceph_encode_64(p, v); \
  256. } while (0)
  257. #define ceph_encode_32_safe(p, end, v, bad) \
  258. do { \
  259. ceph_encode_need(p, end, sizeof(u32), bad); \
  260. ceph_encode_32(p, v); \
  261. } while (0)
  262. #define ceph_encode_16_safe(p, end, v, bad) \
  263. do { \
  264. ceph_encode_need(p, end, sizeof(u16), bad); \
  265. ceph_encode_16(p, v); \
  266. } while (0)
  267. #define ceph_encode_8_safe(p, end, v, bad) \
  268. do { \
  269. ceph_encode_need(p, end, sizeof(u8), bad); \
  270. ceph_encode_8(p, v); \
  271. } while (0)
  272. #define ceph_encode_copy_safe(p, end, pv, n, bad) \
  273. do { \
  274. ceph_encode_need(p, end, n, bad); \
  275. ceph_encode_copy(p, pv, n); \
  276. } while (0)
  277. #define ceph_encode_string_safe(p, end, s, n, bad) \
  278. do { \
  279. ceph_encode_need(p, end, n, bad); \
  280. ceph_encode_string(p, end, s, n); \
  281. } while (0)
  282. #endif