blob: 5454e2327a5f77874d63344802709697ceb64de1 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07002
3#include <linux/bug.h>
4#include <linux/err.h>
5#include <linux/random.h>
6#include <linux/slab.h>
7#include <linux/types.h>
8
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07009#include <linux/ceph/mdsmap.h>
10#include <linux/ceph/messenger.h>
11#include <linux/ceph/decode.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070012
13#include "super.h"
14
15
16/*
17 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
18 */
19int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
20{
21 int n = 0;
22 int i;
Sam Langa84cd292013-04-09 16:49:11 -050023
24 /* special case for one mds */
25 if (1 == m->m_max_mds && m->m_info[0].state > 0)
26 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -070027
28 /* count */
29 for (i = 0; i < m->m_max_mds; i++)
30 if (m->m_info[i].state > 0)
31 n++;
32 if (n == 0)
33 return -1;
34
35 /* pick */
Sam Langa84cd292013-04-09 16:49:11 -050036 n = prandom_u32() % n;
Sage Weil2f2dc052009-10-06 11:31:09 -070037 i = 0;
38 for (i = 0; n > 0; i++, n--)
39 while (m->m_info[i].state <= 0)
40 i++;
41
42 return i;
43}
44
Yan, Zhenge9e427f2016-11-10 16:02:06 +080045#define __decode_and_drop_type(p, end, type, bad) \
46 do { \
47 if (*p + sizeof(type) > end) \
48 goto bad; \
49 *p += sizeof(type); \
50 } while (0)
51
52#define __decode_and_drop_set(p, end, type, bad) \
53 do { \
54 u32 n; \
55 size_t need; \
56 ceph_decode_32_safe(p, end, n, bad); \
57 need = sizeof(type) * n; \
58 ceph_decode_need(p, end, need, bad); \
59 *p += need; \
60 } while (0)
61
62#define __decode_and_drop_map(p, end, ktype, vtype, bad) \
63 do { \
64 u32 n; \
65 size_t need; \
66 ceph_decode_32_safe(p, end, n, bad); \
67 need = (sizeof(ktype) + sizeof(vtype)) * n; \
68 ceph_decode_need(p, end, need, bad); \
69 *p += need; \
70 } while (0)
71
72
73static int __decode_and_drop_compat_set(void **p, void* end)
74{
75 int i;
76 /* compat, ro_compat, incompat*/
77 for (i = 0; i < 3; i++) {
78 u32 n;
79 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
80 /* mask */
81 *p += sizeof(u64);
82 /* names (map<u64, string>) */
83 n = ceph_decode_32(p);
84 while (n-- > 0) {
85 u32 len;
86 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
87 bad);
88 *p += sizeof(u64);
89 len = ceph_decode_32(p);
90 ceph_decode_need(p, end, len, bad);
91 *p += len;
92 }
93 }
94 return 0;
95bad:
96 return -1;
97}
98
Sage Weil2f2dc052009-10-06 11:31:09 -070099/*
100 * Decode an MDS map
101 *
102 * Ignore any fields we don't care about (there are quite a few of
103 * them).
104 */
105struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
106{
107 struct ceph_mdsmap *m;
Sage Weil9ec7cab2009-12-14 15:13:47 -0800108 const void *start = *p;
Sage Weil2f2dc052009-10-06 11:31:09 -0700109 int i, j, n;
110 int err = -EINVAL;
Yan, Zhengd463a432016-03-31 15:53:01 +0800111 u8 mdsmap_v, mdsmap_cv;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800112 u16 mdsmap_ev;
Sage Weil2f2dc052009-10-06 11:31:09 -0700113
114 m = kzalloc(sizeof(*m), GFP_NOFS);
115 if (m == NULL)
116 return ERR_PTR(-ENOMEM);
117
Yan, Zhengd463a432016-03-31 15:53:01 +0800118 ceph_decode_need(p, end, 1 + 1, bad);
119 mdsmap_v = ceph_decode_8(p);
120 mdsmap_cv = ceph_decode_8(p);
121 if (mdsmap_v >= 4) {
122 u32 mdsmap_len;
123 ceph_decode_32_safe(p, end, mdsmap_len, bad);
124 if (end < *p + mdsmap_len)
125 goto bad;
126 end = *p + mdsmap_len;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800127 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700128
129 ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700130 m->m_epoch = ceph_decode_32(p);
131 m->m_client_epoch = ceph_decode_32(p);
132 m->m_last_failure = ceph_decode_32(p);
133 m->m_root = ceph_decode_32(p);
134 m->m_session_timeout = ceph_decode_32(p);
135 m->m_session_autoclose = ceph_decode_32(p);
136 m->m_max_file_size = ceph_decode_64(p);
137 m->m_max_mds = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700138
139 m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
140 if (m->m_info == NULL)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800141 goto nomem;
Sage Weil2f2dc052009-10-06 11:31:09 -0700142
143 /* pick out active nodes from mds_info (state > 0) */
Sage Weilc89136e2009-10-14 09:59:09 -0700144 n = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700145 for (i = 0; i < n; i++) {
Sage Weil94045e12009-11-19 15:31:50 -0800146 u64 global_id;
Sage Weil2f2dc052009-10-06 11:31:09 -0700147 u32 namelen;
148 s32 mds, inc, state;
149 u64 state_seq;
Yan, Zhengd463a432016-03-31 15:53:01 +0800150 u8 info_v;
151 void *info_end = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700152 struct ceph_entity_addr addr;
153 u32 num_export_targets;
154 void *pexport_targets = NULL;
Sage Weil0deb01c2010-06-17 14:19:01 -0700155 struct ceph_timespec laggy_since;
Dan Carpenter6af86522013-05-29 06:46:56 -0500156 struct ceph_mds_info *info;
Sage Weil2f2dc052009-10-06 11:31:09 -0700157
Yan, Zhengd463a432016-03-31 15:53:01 +0800158 ceph_decode_need(p, end, sizeof(u64) + 1, bad);
Sage Weil94045e12009-11-19 15:31:50 -0800159 global_id = ceph_decode_64(p);
Yan, Zhengd463a432016-03-31 15:53:01 +0800160 info_v= ceph_decode_8(p);
161 if (info_v >= 4) {
162 u32 info_len;
163 u8 info_cv;
164 ceph_decode_need(p, end, 1 + sizeof(u32), bad);
165 info_cv = ceph_decode_8(p);
166 info_len = ceph_decode_32(p);
167 info_end = *p + info_len;
168 if (info_end > end)
169 goto bad;
170 }
171
172 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil94045e12009-11-19 15:31:50 -0800173 *p += sizeof(u64);
Sage Weilc89136e2009-10-14 09:59:09 -0700174 namelen = ceph_decode_32(p); /* skip mds name */
Sage Weil2f2dc052009-10-06 11:31:09 -0700175 *p += namelen;
176
177 ceph_decode_need(p, end,
Sage Weile251e282009-10-07 16:38:19 -0700178 4*sizeof(u32) + sizeof(u64) +
Sage Weil2f2dc052009-10-06 11:31:09 -0700179 sizeof(addr) + sizeof(struct ceph_timespec),
180 bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700181 mds = ceph_decode_32(p);
182 inc = ceph_decode_32(p);
183 state = ceph_decode_32(p);
184 state_seq = ceph_decode_64(p);
Sage Weil94045e12009-11-19 15:31:50 -0800185 ceph_decode_copy(p, &addr, sizeof(addr));
186 ceph_decode_addr(&addr);
Sage Weil0deb01c2010-06-17 14:19:01 -0700187 ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
Sage Weil2f2dc052009-10-06 11:31:09 -0700188 *p += sizeof(u32);
189 ceph_decode_32_safe(p, end, namelen, bad);
Sage Weile251e282009-10-07 16:38:19 -0700190 *p += namelen;
Yan, Zhengd463a432016-03-31 15:53:01 +0800191 if (info_v >= 2) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700192 ceph_decode_32_safe(p, end, num_export_targets, bad);
193 pexport_targets = *p;
Sage Weile251e282009-10-07 16:38:19 -0700194 *p += num_export_targets * sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -0700195 } else {
196 num_export_targets = 0;
197 }
198
Yan, Zhengd463a432016-03-31 15:53:01 +0800199 if (info_end && *p != info_end) {
200 if (*p > info_end)
201 goto bad;
202 *p = info_end;
203 }
204
Sage Weil94045e12009-11-19 15:31:50 -0800205 dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700206 i+1, n, global_id, mds, inc,
207 ceph_pr_addr(&addr.in_addr),
Sage Weil2f2dc052009-10-06 11:31:09 -0700208 ceph_mds_state_name(state));
Dan Carpenter6af86522013-05-29 06:46:56 -0500209
210 if (mds < 0 || mds >= m->m_max_mds || state <= 0)
211 continue;
212
213 info = &m->m_info[mds];
214 info->global_id = global_id;
215 info->state = state;
216 info->addr = addr;
217 info->laggy = (laggy_since.tv_sec != 0 ||
218 laggy_since.tv_nsec != 0);
219 info->num_export_targets = num_export_targets;
220 if (num_export_targets) {
221 info->export_targets = kcalloc(num_export_targets,
222 sizeof(u32), GFP_NOFS);
223 if (info->export_targets == NULL)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800224 goto nomem;
Dan Carpenter6af86522013-05-29 06:46:56 -0500225 for (j = 0; j < num_export_targets; j++)
226 info->export_targets[j] =
227 ceph_decode_32(&pexport_targets);
228 } else {
229 info->export_targets = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700230 }
231 }
232
233 /* pg_pools */
234 ceph_decode_32_safe(p, end, n, bad);
235 m->m_num_data_pg_pools = n;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800236 m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -0700237 if (!m->m_data_pg_pools)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800238 goto nomem;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800239 ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700240 for (i = 0; i < n; i++)
Sage Weil4f6a7e52013-02-23 10:41:09 -0800241 m->m_data_pg_pools[i] = ceph_decode_64(p);
242 m->m_cas_pg_pool = ceph_decode_64(p);
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800243 m->m_enabled = m->m_epoch > 1;
Sage Weil2f2dc052009-10-06 11:31:09 -0700244
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800245 mdsmap_ev = 1;
246 if (mdsmap_v >= 2) {
247 ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
248 }
249 if (mdsmap_ev >= 3) {
250 if (__decode_and_drop_compat_set(p, end) < 0)
251 goto bad_ext;
252 }
253 /* metadata_pool */
254 if (mdsmap_ev < 5) {
255 __decode_and_drop_type(p, end, u32, bad_ext);
256 } else {
257 __decode_and_drop_type(p, end, u64, bad_ext);
258 }
259
260 /* created + modified + tableserver */
261 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
262 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
263 __decode_and_drop_type(p, end, u32, bad_ext);
264
265 /* in */
266 {
267 int num_laggy = 0;
268 ceph_decode_32_safe(p, end, n, bad_ext);
269 ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);
270
271 for (i = 0; i < n; i++) {
272 s32 mds = ceph_decode_32(p);
273 if (mds >= 0 && mds < m->m_max_mds) {
274 if (m->m_info[mds].laggy)
275 num_laggy++;
276 }
277 }
278 m->m_num_laggy = num_laggy;
279 }
280
281 /* inc */
282 __decode_and_drop_map(p, end, u32, u32, bad_ext);
283 /* up */
284 __decode_and_drop_map(p, end, u32, u64, bad_ext);
285 /* failed */
286 __decode_and_drop_set(p, end, u32, bad_ext);
287 /* stopped */
288 __decode_and_drop_set(p, end, u32, bad_ext);
289
290 if (mdsmap_ev >= 4) {
291 /* last_failure_osd_epoch */
292 __decode_and_drop_type(p, end, u32, bad_ext);
293 }
294 if (mdsmap_ev >= 6) {
295 /* ever_allowed_snaps */
296 __decode_and_drop_type(p, end, u8, bad_ext);
297 /* explicitly_allowed_snaps */
298 __decode_and_drop_type(p, end, u8, bad_ext);
299 }
300 if (mdsmap_ev >= 7) {
301 /* inline_data_enabled */
302 __decode_and_drop_type(p, end, u8, bad_ext);
303 }
304 if (mdsmap_ev >= 8) {
305 u32 name_len;
306 /* enabled */
307 ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
308 ceph_decode_32_safe(p, end, name_len, bad_ext);
309 ceph_decode_need(p, end, name_len, bad_ext);
310 *p += name_len;
311 }
312 /* damaged */
313 if (mdsmap_ev >= 9) {
314 size_t need;
315 ceph_decode_32_safe(p, end, n, bad_ext);
316 need = sizeof(u32) * n;
317 ceph_decode_need(p, end, need, bad_ext);
318 *p += need;
319 m->m_damaged = n > 0;
320 } else {
321 m->m_damaged = false;
322 }
323bad_ext:
Yan, Zhengd463a432016-03-31 15:53:01 +0800324 *p = end;
Sage Weil2f2dc052009-10-06 11:31:09 -0700325 dout("mdsmap_decode success epoch %u\n", m->m_epoch);
326 return m;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800327nomem:
Sage Weil2f2dc052009-10-06 11:31:09 -0700328 err = -ENOMEM;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800329 goto out_err;
Sage Weil2f2dc052009-10-06 11:31:09 -0700330bad:
331 pr_err("corrupt mdsmap\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800332 print_hex_dump(KERN_DEBUG, "mdsmap: ",
333 DUMP_PREFIX_OFFSET, 16, 1,
334 start, end - start, true);
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800335out_err:
Sage Weil2f2dc052009-10-06 11:31:09 -0700336 ceph_mdsmap_destroy(m);
Emil Goodec213b502013-05-28 16:59:00 +0200337 return ERR_PTR(err);
Sage Weil2f2dc052009-10-06 11:31:09 -0700338}
339
340void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
341{
342 int i;
343
344 for (i = 0; i < m->m_max_mds; i++)
345 kfree(m->m_info[i].export_targets);
346 kfree(m->m_info);
347 kfree(m->m_data_pg_pools);
348 kfree(m);
349}
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800350
351bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
352{
353 int i, nr_active = 0;
354 if (!m->m_enabled)
355 return false;
356 if (m->m_damaged)
357 return false;
358 if (m->m_num_laggy > 0)
359 return false;
360 for (i = 0; i < m->m_max_mds; i++) {
361 if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
362 nr_active++;
363 }
364 return nr_active > 0;
365}