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