Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 1 | #ifndef __CEPH_DECODE_H |
| 2 | #define __CEPH_DECODE_H |
| 3 | |
| 4 | #include <asm/unaligned.h> |
| 5 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 6 | #include "types.h" |
| 7 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 8 | /* |
| 9 | * in all cases, |
| 10 | * void **p pointer to position pointer |
| 11 | * void *end pointer to end of buffer (last byte + 1) |
| 12 | */ |
| 13 | |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 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 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 44 | /* |
| 45 | * bounds check input. |
| 46 | */ |
| 47 | #define ceph_decode_need(p, end, n, bad) \ |
| 48 | do { \ |
| 49 | if (unlikely(*(p) + (n) > (end))) \ |
| 50 | goto bad; \ |
| 51 | } while (0) |
| 52 | |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 53 | #define ceph_decode_64_safe(p, end, v, bad) \ |
| 54 | do { \ |
| 55 | ceph_decode_need(p, end, sizeof(u64), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 56 | v = ceph_decode_64(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 57 | } while (0) |
| 58 | #define ceph_decode_32_safe(p, end, v, bad) \ |
| 59 | do { \ |
| 60 | ceph_decode_need(p, end, sizeof(u32), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 61 | v = ceph_decode_32(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 62 | } while (0) |
| 63 | #define ceph_decode_16_safe(p, end, v, bad) \ |
| 64 | do { \ |
| 65 | ceph_decode_need(p, end, sizeof(u16), bad); \ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 66 | v = ceph_decode_16(p); \ |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 67 | } while (0) |
| 68 | |
| 69 | #define ceph_decode_copy_safe(p, end, pv, n, bad) \ |
| 70 | do { \ |
| 71 | ceph_decode_need(p, end, n, bad); \ |
| 72 | ceph_decode_copy(p, pv, n); \ |
| 73 | } while (0) |
| 74 | |
| 75 | /* |
| 76 | * struct ceph_timespec <-> struct timespec |
| 77 | */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 78 | static inline void ceph_decode_timespec(struct timespec *ts, |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 79 | const struct ceph_timespec *tv) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 80 | { |
| 81 | ts->tv_sec = le32_to_cpu(tv->tv_sec); |
| 82 | ts->tv_nsec = le32_to_cpu(tv->tv_nsec); |
| 83 | } |
| 84 | static inline void ceph_encode_timespec(struct ceph_timespec *tv, |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 85 | const struct timespec *ts) |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 86 | { |
| 87 | tv->tv_sec = cpu_to_le32(ts->tv_sec); |
| 88 | tv->tv_nsec = cpu_to_le32(ts->tv_nsec); |
| 89 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 90 | |
| 91 | /* |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 92 | * sockaddr_storage <-> ceph_sockaddr |
| 93 | */ |
| 94 | static inline void ceph_encode_addr(struct ceph_entity_addr *a) |
| 95 | { |
| 96 | a->in_addr.ss_family = htons(a->in_addr.ss_family); |
| 97 | } |
| 98 | static inline void ceph_decode_addr(struct ceph_entity_addr *a) |
| 99 | { |
| 100 | a->in_addr.ss_family = ntohs(a->in_addr.ss_family); |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 101 | WARN_ON(a->in_addr.ss_family == 512); |
Sage Weil | 63f2d21 | 2009-11-03 15:17:56 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 105 | * encoders |
| 106 | */ |
Sage Weil | c89136e | 2009-10-14 09:59:09 -0700 | [diff] [blame] | 107 | static inline void ceph_encode_64(void **p, u64 v) |
| 108 | { |
| 109 | put_unaligned_le64(v, (__le64 *)*p); |
| 110 | *p += sizeof(u64); |
| 111 | } |
| 112 | static inline void ceph_encode_32(void **p, u32 v) |
| 113 | { |
| 114 | put_unaligned_le32(v, (__le32 *)*p); |
| 115 | *p += sizeof(u32); |
| 116 | } |
| 117 | static inline void ceph_encode_16(void **p, u16 v) |
| 118 | { |
| 119 | put_unaligned_le16(v, (__le16 *)*p); |
| 120 | *p += sizeof(u16); |
| 121 | } |
| 122 | static inline void ceph_encode_8(void **p, u8 v) |
| 123 | { |
| 124 | *(u8 *)*p = v; |
| 125 | (*p)++; |
| 126 | } |
Sage Weil | 4e7a5dc | 2009-11-18 16:19:57 -0800 | [diff] [blame] | 127 | static inline void ceph_encode_copy(void **p, const void *s, int len) |
| 128 | { |
| 129 | memcpy(*p, s, len); |
| 130 | *p += len; |
| 131 | } |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * filepath, string encoders |
| 135 | */ |
| 136 | static inline void ceph_encode_filepath(void **p, void *end, |
| 137 | u64 ino, const char *path) |
| 138 | { |
| 139 | u32 len = path ? strlen(path) : 0; |
| 140 | BUG_ON(*p + sizeof(ino) + sizeof(len) + len > end); |
Sage Weil | ac8839d | 2010-01-27 14:28:10 -0800 | [diff] [blame^] | 141 | ceph_encode_8(p, 1); |
Sage Weil | 31b8006 | 2009-10-06 11:31:13 -0700 | [diff] [blame] | 142 | ceph_encode_64(p, ino); |
| 143 | ceph_encode_32(p, len); |
| 144 | if (len) |
| 145 | memcpy(*p, path, len); |
| 146 | *p += len; |
| 147 | } |
| 148 | |
| 149 | static inline void ceph_encode_string(void **p, void *end, |
| 150 | const char *s, u32 len) |
| 151 | { |
| 152 | BUG_ON(*p + sizeof(len) + len > end); |
| 153 | ceph_encode_32(p, len); |
| 154 | if (len) |
| 155 | memcpy(*p, s, len); |
| 156 | *p += len; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | #endif |