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