Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1 | #ifndef __CEPH_DECODE_H |
| 2 | #define __CEPH_DECODE_H |
| 3 | |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 4 | #include <linux/bug.h> |
Sage Weil | c7e337d | 2010-02-02 16:11:19 -0800 | [diff] [blame] | 5 | #include <linux/time.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 6 | #include <asm/unaligned.h> |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 7 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 8 | #include "types.h" |
| 9 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 10 | /* |
| 11 | * in all cases, |
| 12 | * void **p pointer to position pointer |
| 13 | * void *end pointer to end of buffer (last byte + 1) |
| 14 | */ |
| 15 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 16 | static inline u64 ceph_decode_64(void **p) |
| 17 | { |
| 18 | u64 v = get_unaligned_le64(*p); |
| 19 | *p += sizeof(u64); |
| 20 | return v; |
| 21 | } |
| 22 | static inline u32 ceph_decode_32(void **p) |
| 23 | { |
| 24 | u32 v = get_unaligned_le32(*p); |
| 25 | *p += sizeof(u32); |
| 26 | return v; |
| 27 | } |
| 28 | static inline u16 ceph_decode_16(void **p) |
| 29 | { |
| 30 | u16 v = get_unaligned_le16(*p); |
| 31 | *p += sizeof(u16); |
| 32 | return v; |
| 33 | } |
| 34 | static inline u8 ceph_decode_8(void **p) |
| 35 | { |
| 36 | u8 v = *(u8 *)*p; |
| 37 | (*p)++; |
| 38 | return v; |
| 39 | } |
| 40 | static inline void ceph_decode_copy(void **p, void *pv, size_t n) |
| 41 | { |
| 42 | memcpy(pv, *p, n); |
| 43 | *p += n; |
| 44 | } |
| 45 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 46 | /* |
| 47 | * bounds check input. |
| 48 | */ |
Xi Wang | 76aa542 | 2012-04-20 15:49:44 -0500 | [diff] [blame] | 49 | static inline int ceph_has_room(void **p, void *end, size_t n) |
| 50 | { |
| 51 | return end >= *p && n <= end - *p; |
| 52 | } |
| 53 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 54 | #define ceph_decode_need(p, end, n, bad) \ |
| 55 | do { \ |
Xi Wang | 76aa542 | 2012-04-20 15:49:44 -0500 | [diff] [blame] | 56 | if (!likely(ceph_has_room(p, end, n))) \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 57 | goto bad; \ |
| 58 | } while (0) |
| 59 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 60 | #define ceph_decode_64_safe(p, end, v, bad) \ |
| 61 | do { \ |
| 62 | ceph_decode_need(p, end, sizeof(u64), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 63 | v = ceph_decode_64(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 64 | } while (0) |
| 65 | #define ceph_decode_32_safe(p, end, v, bad) \ |
| 66 | do { \ |
| 67 | ceph_decode_need(p, end, sizeof(u32), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 68 | v = ceph_decode_32(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 69 | } while (0) |
| 70 | #define ceph_decode_16_safe(p, end, v, bad) \ |
| 71 | do { \ |
| 72 | ceph_decode_need(p, end, sizeof(u16), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 73 | v = ceph_decode_16(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 74 | } while (0) |
Sage Weil | c7e337d | 2010-02-02 16:11:19 -0800 | [diff] [blame] | 75 | #define ceph_decode_8_safe(p, end, v, bad) \ |
| 76 | do { \ |
| 77 | ceph_decode_need(p, end, sizeof(u8), bad); \ |
| 78 | v = ceph_decode_8(p); \ |
| 79 | } while (0) |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 80 | |
| 81 | #define ceph_decode_copy_safe(p, end, pv, n, bad) \ |
| 82 | do { \ |
| 83 | ceph_decode_need(p, end, n, bad); \ |
| 84 | ceph_decode_copy(p, pv, n); \ |
| 85 | } while (0) |
| 86 | |
| 87 | /* |
| 88 | * struct ceph_timespec <-> struct timespec |
| 89 | */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 90 | static inline void ceph_decode_timespec(struct timespec *ts, |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 91 | const struct ceph_timespec *tv) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 92 | { |
| 93 | ts->tv_sec = le32_to_cpu(tv->tv_sec); |
| 94 | ts->tv_nsec = le32_to_cpu(tv->tv_nsec); |
| 95 | } |
| 96 | static inline void ceph_encode_timespec(struct ceph_timespec *tv, |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 97 | const struct timespec *ts) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 98 | { |
| 99 | tv->tv_sec = cpu_to_le32(ts->tv_sec); |
| 100 | tv->tv_nsec = cpu_to_le32(ts->tv_nsec); |
| 101 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 104 | * sockaddr_storage <-> ceph_sockaddr |
| 105 | */ |
| 106 | static inline void ceph_encode_addr(struct ceph_entity_addr *a) |
| 107 | { |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 108 | __be16 ss_family = htons(a->in_addr.ss_family); |
| 109 | a->in_addr.ss_family = *(__u16 *)&ss_family; |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 110 | } |
| 111 | static inline void ceph_decode_addr(struct ceph_entity_addr *a) |
| 112 | { |
Yehuda Sadeh | cd84db6 | 2010-06-11 16:58:48 -0700 | [diff] [blame] | 113 | __be16 ss_family = *(__be16 *)&a->in_addr.ss_family; |
| 114 | a->in_addr.ss_family = ntohs(ss_family); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 115 | WARN_ON(a->in_addr.ss_family == 512); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 119 | * encoders |
| 120 | */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 121 | static inline void ceph_encode_64(void **p, u64 v) |
| 122 | { |
| 123 | put_unaligned_le64(v, (__le64 *)*p); |
| 124 | *p += sizeof(u64); |
| 125 | } |
| 126 | static inline void ceph_encode_32(void **p, u32 v) |
| 127 | { |
| 128 | put_unaligned_le32(v, (__le32 *)*p); |
| 129 | *p += sizeof(u32); |
| 130 | } |
| 131 | static inline void ceph_encode_16(void **p, u16 v) |
| 132 | { |
| 133 | put_unaligned_le16(v, (__le16 *)*p); |
| 134 | *p += sizeof(u16); |
| 135 | } |
| 136 | static inline void ceph_encode_8(void **p, u8 v) |
| 137 | { |
| 138 | *(u8 *)*p = v; |
| 139 | (*p)++; |
| 140 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 141 | static inline void ceph_encode_copy(void **p, const void *s, int len) |
| 142 | { |
| 143 | memcpy(*p, s, len); |
| 144 | *p += len; |
| 145 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * filepath, string encoders |
| 149 | */ |
| 150 | static inline void ceph_encode_filepath(void **p, void *end, |
| 151 | u64 ino, const char *path) |
| 152 | { |
| 153 | u32 len = path ? strlen(path) : 0; |
| 154 | BUG_ON(*p + sizeof(ino) + sizeof(len) + len > end); |
Sage Weil | ac8839d | 2010-01-27 14:28:10 -0800 | [diff] [blame] | 155 | ceph_encode_8(p, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 156 | ceph_encode_64(p, ino); |
| 157 | ceph_encode_32(p, len); |
| 158 | if (len) |
| 159 | memcpy(*p, path, len); |
| 160 | *p += len; |
| 161 | } |
| 162 | |
| 163 | static inline void ceph_encode_string(void **p, void *end, |
| 164 | const char *s, u32 len) |
| 165 | { |
| 166 | BUG_ON(*p + sizeof(len) + len > end); |
| 167 | ceph_encode_32(p, len); |
| 168 | if (len) |
| 169 | memcpy(*p, s, len); |
| 170 | *p += len; |
| 171 | } |
| 172 | |
Sage Weil | c7e337d | 2010-02-02 16:11:19 -0800 | [diff] [blame] | 173 | #define ceph_encode_need(p, end, n, bad) \ |
| 174 | do { \ |
Xi Wang | 76aa542 | 2012-04-20 15:49:44 -0500 | [diff] [blame] | 175 | if (!likely(ceph_has_room(p, end, n))) \ |
Sage Weil | c7e337d | 2010-02-02 16:11:19 -0800 | [diff] [blame] | 176 | goto bad; \ |
| 177 | } while (0) |
| 178 | |
| 179 | #define ceph_encode_64_safe(p, end, v, bad) \ |
| 180 | do { \ |
| 181 | ceph_encode_need(p, end, sizeof(u64), bad); \ |
| 182 | ceph_encode_64(p, v); \ |
| 183 | } while (0) |
| 184 | #define ceph_encode_32_safe(p, end, v, bad) \ |
| 185 | do { \ |
| 186 | ceph_encode_need(p, end, sizeof(u32), bad); \ |
| 187 | ceph_encode_32(p, v); \ |
| 188 | } while (0) |
| 189 | #define ceph_encode_16_safe(p, end, v, bad) \ |
| 190 | do { \ |
| 191 | ceph_encode_need(p, end, sizeof(u16), bad); \ |
| 192 | ceph_encode_16(p, v); \ |
| 193 | } while (0) |
| 194 | |
| 195 | #define ceph_encode_copy_safe(p, end, pv, n, bad) \ |
| 196 | do { \ |
| 197 | ceph_encode_need(p, end, n, bad); \ |
| 198 | ceph_encode_copy(p, pv, n); \ |
| 199 | } while (0) |
Yehuda Sadeh | ae1533b | 2010-05-18 16:38:08 -0700 | [diff] [blame] | 200 | #define ceph_encode_string_safe(p, end, s, n, bad) \ |
| 201 | do { \ |
| 202 | ceph_encode_need(p, end, n, bad); \ |
| 203 | ceph_encode_string(p, end, s, n); \ |
| 204 | } while (0) |
Sage Weil | c7e337d | 2010-02-02 16:11:19 -0800 | [diff] [blame] | 205 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 206 | |
| 207 | #endif |