Commit c7e337d6 authored by Sage Weil's avatar Sage Weil

ceph: buffer decoding helpers

Helper for decoding into a ceph_buffer, and other misc decoding helpers
we will need.
Signed-off-by: default avatarYehuda Sadeh <yehuda@hq.newdream.net>
Signed-off-by: default avatarSage Weil <sage@newdream.net>
parent 79788c69
#include "ceph_debug.h" #include "ceph_debug.h"
#include "buffer.h" #include "buffer.h"
#include "decode.h"
struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp)
{ {
...@@ -59,3 +60,19 @@ int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) ...@@ -59,3 +60,19 @@ int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp)
return 0; return 0;
} }
int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end)
{
size_t len;
ceph_decode_need(p, end, sizeof(u32), bad);
len = ceph_decode_32(p);
dout("decode_buffer len %d\n", (int)len);
ceph_decode_need(p, end, len, bad);
*b = ceph_buffer_new(len, GFP_NOFS);
if (!*b)
return -ENOMEM;
ceph_decode_copy(p, (*b)->vec.iov_base, len);
return 0;
bad:
return -EINVAL;
}
...@@ -34,4 +34,6 @@ static inline void ceph_buffer_put(struct ceph_buffer *b) ...@@ -34,4 +34,6 @@ static inline void ceph_buffer_put(struct ceph_buffer *b)
kref_put(&b->kref, ceph_buffer_release); kref_put(&b->kref, ceph_buffer_release);
} }
extern int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end);
#endif #endif
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define __CEPH_DECODE_H #define __CEPH_DECODE_H
#include <asm/unaligned.h> #include <asm/unaligned.h>
#include <linux/time.h>
#include "types.h" #include "types.h"
...@@ -65,6 +66,11 @@ static inline void ceph_decode_copy(void **p, void *pv, size_t n) ...@@ -65,6 +66,11 @@ static inline void ceph_decode_copy(void **p, void *pv, size_t n)
ceph_decode_need(p, end, sizeof(u16), bad); \ ceph_decode_need(p, end, sizeof(u16), bad); \
v = ceph_decode_16(p); \ v = ceph_decode_16(p); \
} while (0) } while (0)
#define ceph_decode_8_safe(p, end, v, bad) \
do { \
ceph_decode_need(p, end, sizeof(u8), bad); \
v = ceph_decode_8(p); \
} while (0)
#define ceph_decode_copy_safe(p, end, pv, n, bad) \ #define ceph_decode_copy_safe(p, end, pv, n, bad) \
do { \ do { \
...@@ -156,5 +162,33 @@ static inline void ceph_encode_string(void **p, void *end, ...@@ -156,5 +162,33 @@ static inline void ceph_encode_string(void **p, void *end,
*p += len; *p += len;
} }
#define ceph_encode_need(p, end, n, bad) \
do { \
if (unlikely(*(p) + (n) > (end))) \
goto bad; \
} while (0)
#define ceph_encode_64_safe(p, end, v, bad) \
do { \
ceph_encode_need(p, end, sizeof(u64), bad); \
ceph_encode_64(p, v); \
} while (0)
#define ceph_encode_32_safe(p, end, v, bad) \
do { \
ceph_encode_need(p, end, sizeof(u32), bad); \
ceph_encode_32(p, v); \
} while (0)
#define ceph_encode_16_safe(p, end, v, bad) \
do { \
ceph_encode_need(p, end, sizeof(u16), bad); \
ceph_encode_16(p, v); \
} while (0)
#define ceph_encode_copy_safe(p, end, pv, n, bad) \
do { \
ceph_encode_need(p, end, n, bad); \
ceph_encode_copy(p, pv, n); \
} while (0)
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment