diff options
author | Sage Weil <sage@newdream.net> | 2010-02-02 19:11:19 -0500 |
---|---|---|
committer | Sage Weil <sage@newdream.net> | 2010-02-10 18:04:39 -0500 |
commit | c7e337d6490d6f2f5e66ddf1b04d00b0dbd10108 (patch) | |
tree | 3a2e12219e38e807c71d8b2c30b89e7efbe82bdd | |
parent | 79788c698b290426320e60374ed1324e4b5c69eb (diff) |
ceph: buffer decoding helpers
Helper for decoding into a ceph_buffer, and other misc decoding helpers
we will need.
Signed-off-by: Yehuda Sadeh <yehuda@hq.newdream.net>
Signed-off-by: Sage Weil <sage@newdream.net>
-rw-r--r-- | fs/ceph/buffer.c | 17 | ||||
-rw-r--r-- | fs/ceph/buffer.h | 2 | ||||
-rw-r--r-- | fs/ceph/decode.h | 34 |
3 files changed, 53 insertions, 0 deletions
diff --git a/fs/ceph/buffer.c b/fs/ceph/buffer.c index 2576bd452cb8..b98086c7aeba 100644 --- a/fs/ceph/buffer.c +++ b/fs/ceph/buffer.c | |||
@@ -1,6 +1,7 @@ | |||
1 | 1 | ||
2 | #include "ceph_debug.h" | 2 | #include "ceph_debug.h" |
3 | #include "buffer.h" | 3 | #include "buffer.h" |
4 | #include "decode.h" | ||
4 | 5 | ||
5 | struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) | 6 | struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) |
6 | { | 7 | { |
@@ -59,3 +60,19 @@ int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) | |||
59 | return 0; | 60 | return 0; |
60 | } | 61 | } |
61 | 62 | ||
63 | int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end) | ||
64 | { | ||
65 | size_t len; | ||
66 | |||
67 | ceph_decode_need(p, end, sizeof(u32), bad); | ||
68 | len = ceph_decode_32(p); | ||
69 | dout("decode_buffer len %d\n", (int)len); | ||
70 | ceph_decode_need(p, end, len, bad); | ||
71 | *b = ceph_buffer_new(len, GFP_NOFS); | ||
72 | if (!*b) | ||
73 | return -ENOMEM; | ||
74 | ceph_decode_copy(p, (*b)->vec.iov_base, len); | ||
75 | return 0; | ||
76 | bad: | ||
77 | return -EINVAL; | ||
78 | } | ||
diff --git a/fs/ceph/buffer.h b/fs/ceph/buffer.h index 47b9514c5bbd..58d19014068f 100644 --- a/fs/ceph/buffer.h +++ b/fs/ceph/buffer.h | |||
@@ -34,4 +34,6 @@ static inline void ceph_buffer_put(struct ceph_buffer *b) | |||
34 | kref_put(&b->kref, ceph_buffer_release); | 34 | kref_put(&b->kref, ceph_buffer_release); |
35 | } | 35 | } |
36 | 36 | ||
37 | extern int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end); | ||
38 | |||
37 | #endif | 39 | #endif |
diff --git a/fs/ceph/decode.h b/fs/ceph/decode.h index b90a33b948c7..65b3e022eaf5 100644 --- a/fs/ceph/decode.h +++ b/fs/ceph/decode.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #define __CEPH_DECODE_H | 2 | #define __CEPH_DECODE_H |
3 | 3 | ||
4 | #include <asm/unaligned.h> | 4 | #include <asm/unaligned.h> |
5 | #include <linux/time.h> | ||
5 | 6 | ||
6 | #include "types.h" | 7 | #include "types.h" |
7 | 8 | ||
@@ -65,6 +66,11 @@ static inline void ceph_decode_copy(void **p, void *pv, size_t n) | |||
65 | ceph_decode_need(p, end, sizeof(u16), bad); \ | 66 | ceph_decode_need(p, end, sizeof(u16), bad); \ |
66 | v = ceph_decode_16(p); \ | 67 | v = ceph_decode_16(p); \ |
67 | } while (0) | 68 | } while (0) |
69 | #define ceph_decode_8_safe(p, end, v, bad) \ | ||
70 | do { \ | ||
71 | ceph_decode_need(p, end, sizeof(u8), bad); \ | ||
72 | v = ceph_decode_8(p); \ | ||
73 | } while (0) | ||
68 | 74 | ||
69 | #define ceph_decode_copy_safe(p, end, pv, n, bad) \ | 75 | #define ceph_decode_copy_safe(p, end, pv, n, bad) \ |
70 | do { \ | 76 | do { \ |
@@ -156,5 +162,33 @@ static inline void ceph_encode_string(void **p, void *end, | |||
156 | *p += len; | 162 | *p += len; |
157 | } | 163 | } |
158 | 164 | ||
165 | #define ceph_encode_need(p, end, n, bad) \ | ||
166 | do { \ | ||
167 | if (unlikely(*(p) + (n) > (end))) \ | ||
168 | goto bad; \ | ||
169 | } while (0) | ||
170 | |||
171 | #define ceph_encode_64_safe(p, end, v, bad) \ | ||
172 | do { \ | ||
173 | ceph_encode_need(p, end, sizeof(u64), bad); \ | ||
174 | ceph_encode_64(p, v); \ | ||
175 | } while (0) | ||
176 | #define ceph_encode_32_safe(p, end, v, bad) \ | ||
177 | do { \ | ||
178 | ceph_encode_need(p, end, sizeof(u32), bad); \ | ||
179 | ceph_encode_32(p, v); \ | ||
180 | } while (0) | ||
181 | #define ceph_encode_16_safe(p, end, v, bad) \ | ||
182 | do { \ | ||
183 | ceph_encode_need(p, end, sizeof(u16), bad); \ | ||
184 | ceph_encode_16(p, v); \ | ||
185 | } while (0) | ||
186 | |||
187 | #define ceph_encode_copy_safe(p, end, pv, n, bad) \ | ||
188 | do { \ | ||
189 | ceph_encode_need(p, end, n, bad); \ | ||
190 | ceph_encode_copy(p, pv, n); \ | ||
191 | } while (0) | ||
192 | |||
159 | 193 | ||
160 | #endif | 194 | #endif |