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>
diff --git a/fs/ceph/decode.h b/fs/ceph/decode.h
index b90a33b..65b3e02 100644
--- a/fs/ceph/decode.h
+++ b/fs/ceph/decode.h
@@ -2,6 +2,7 @@
 #define __CEPH_DECODE_H
 
 #include <asm/unaligned.h>
+#include <linux/time.h>
 
 #include "types.h"
 
@@ -65,6 +66,11 @@
 		ceph_decode_need(p, end, sizeof(u16), bad);	\
 		v = ceph_decode_16(p);				\
 	} 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)		\
 	do {							\
@@ -156,5 +162,33 @@
 	*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