blob: 471bac335fae6ed51b8db1f289e7ec3b94244450 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weil2f2dc052009-10-06 11:31:09 -07003
4#include <linux/bug.h>
5#include <linux/err.h>
6#include <linux/random.h>
7#include <linux/slab.h>
8#include <linux/types.h>
9
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070010#include <linux/ceph/mdsmap.h>
11#include <linux/ceph/messenger.h>
12#include <linux/ceph/decode.h>
Sage Weil2f2dc052009-10-06 11:31:09 -070013
14#include "super.h"
15
16
17/*
18 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
19 */
20int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
21{
22 int n = 0;
Xiubo Li74d6f032019-11-11 06:51:05 -050023 int i, j;
Sam Langa84cd292013-04-09 16:49:11 -050024
25 /* special case for one mds */
Yan, Zheng76201b62017-03-28 17:04:13 +080026 if (1 == m->m_num_mds && m->m_info[0].state > 0)
Sam Langa84cd292013-04-09 16:49:11 -050027 return 0;
Sage Weil2f2dc052009-10-06 11:31:09 -070028
29 /* count */
Yan, Zheng76201b62017-03-28 17:04:13 +080030 for (i = 0; i < m->m_num_mds; i++)
Sage Weil2f2dc052009-10-06 11:31:09 -070031 if (m->m_info[i].state > 0)
32 n++;
33 if (n == 0)
34 return -1;
35
36 /* pick */
Sam Langa84cd292013-04-09 16:49:11 -050037 n = prandom_u32() % n;
Xiubo Li74d6f032019-11-11 06:51:05 -050038 for (j = 0, i = 0; i < m->m_num_mds; i++) {
39 if (m->m_info[i].state > 0)
40 j++;
41 if (j > n)
42 break;
43 }
Sage Weil2f2dc052009-10-06 11:31:09 -070044
45 return i;
46}
47
Yan, Zhenge9e427f2016-11-10 16:02:06 +080048#define __decode_and_drop_type(p, end, type, bad) \
49 do { \
50 if (*p + sizeof(type) > end) \
51 goto bad; \
52 *p += sizeof(type); \
53 } while (0)
54
55#define __decode_and_drop_set(p, end, type, bad) \
56 do { \
57 u32 n; \
58 size_t need; \
59 ceph_decode_32_safe(p, end, n, bad); \
60 need = sizeof(type) * n; \
61 ceph_decode_need(p, end, need, bad); \
62 *p += need; \
63 } while (0)
64
65#define __decode_and_drop_map(p, end, ktype, vtype, bad) \
66 do { \
67 u32 n; \
68 size_t need; \
69 ceph_decode_32_safe(p, end, n, bad); \
70 need = (sizeof(ktype) + sizeof(vtype)) * n; \
71 ceph_decode_need(p, end, need, bad); \
72 *p += need; \
73 } while (0)
74
75
76static int __decode_and_drop_compat_set(void **p, void* end)
77{
78 int i;
79 /* compat, ro_compat, incompat*/
80 for (i = 0; i < 3; i++) {
81 u32 n;
82 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
83 /* mask */
84 *p += sizeof(u64);
85 /* names (map<u64, string>) */
86 n = ceph_decode_32(p);
87 while (n-- > 0) {
88 u32 len;
89 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
90 bad);
91 *p += sizeof(u64);
92 len = ceph_decode_32(p);
93 ceph_decode_need(p, end, len, bad);
94 *p += len;
95 }
96 }
97 return 0;
98bad:
99 return -1;
100}
101
Sage Weil2f2dc052009-10-06 11:31:09 -0700102/*
103 * Decode an MDS map
104 *
105 * Ignore any fields we don't care about (there are quite a few of
106 * them).
107 */
108struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
109{
110 struct ceph_mdsmap *m;
Sage Weil9ec7cab2009-12-14 15:13:47 -0800111 const void *start = *p;
Sage Weil2f2dc052009-10-06 11:31:09 -0700112 int i, j, n;
Jeff Laytonf3848af2019-06-04 11:26:36 -0400113 int err;
Yan, Zhengd463a432016-03-31 15:53:01 +0800114 u8 mdsmap_v, mdsmap_cv;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800115 u16 mdsmap_ev;
Sage Weil2f2dc052009-10-06 11:31:09 -0700116
117 m = kzalloc(sizeof(*m), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200118 if (!m)
Sage Weil2f2dc052009-10-06 11:31:09 -0700119 return ERR_PTR(-ENOMEM);
120
Yan, Zhengd463a432016-03-31 15:53:01 +0800121 ceph_decode_need(p, end, 1 + 1, bad);
122 mdsmap_v = ceph_decode_8(p);
123 mdsmap_cv = ceph_decode_8(p);
124 if (mdsmap_v >= 4) {
125 u32 mdsmap_len;
126 ceph_decode_32_safe(p, end, mdsmap_len, bad);
127 if (end < *p + mdsmap_len)
128 goto bad;
129 end = *p + mdsmap_len;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800130 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700131
132 ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700133 m->m_epoch = ceph_decode_32(p);
134 m->m_client_epoch = ceph_decode_32(p);
135 m->m_last_failure = ceph_decode_32(p);
136 m->m_root = ceph_decode_32(p);
137 m->m_session_timeout = ceph_decode_32(p);
138 m->m_session_autoclose = ceph_decode_32(p);
139 m->m_max_file_size = ceph_decode_64(p);
140 m->m_max_mds = ceph_decode_32(p);
Yan, Zheng76201b62017-03-28 17:04:13 +0800141 m->m_num_mds = m->m_max_mds;
Sage Weil2f2dc052009-10-06 11:31:09 -0700142
Yan, Zheng76201b62017-03-28 17:04:13 +0800143 m->m_info = kcalloc(m->m_num_mds, sizeof(*m->m_info), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200144 if (!m->m_info)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800145 goto nomem;
Sage Weil2f2dc052009-10-06 11:31:09 -0700146
147 /* pick out active nodes from mds_info (state > 0) */
Sage Weilc89136e2009-10-14 09:59:09 -0700148 n = ceph_decode_32(p);
Sage Weil2f2dc052009-10-06 11:31:09 -0700149 for (i = 0; i < n; i++) {
Sage Weil94045e12009-11-19 15:31:50 -0800150 u64 global_id;
Sage Weil2f2dc052009-10-06 11:31:09 -0700151 u32 namelen;
152 s32 mds, inc, state;
153 u64 state_seq;
Yan, Zhengd463a432016-03-31 15:53:01 +0800154 u8 info_v;
155 void *info_end = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700156 struct ceph_entity_addr addr;
157 u32 num_export_targets;
158 void *pexport_targets = NULL;
Sage Weil0deb01c2010-06-17 14:19:01 -0700159 struct ceph_timespec laggy_since;
Dan Carpenter6af86522013-05-29 06:46:56 -0500160 struct ceph_mds_info *info;
Xiubo Lida08e1e2019-11-26 07:24:20 -0500161 bool laggy;
Sage Weil2f2dc052009-10-06 11:31:09 -0700162
Yan, Zhengd463a432016-03-31 15:53:01 +0800163 ceph_decode_need(p, end, sizeof(u64) + 1, bad);
Sage Weil94045e12009-11-19 15:31:50 -0800164 global_id = ceph_decode_64(p);
Yan, Zhengd463a432016-03-31 15:53:01 +0800165 info_v= ceph_decode_8(p);
166 if (info_v >= 4) {
167 u32 info_len;
168 u8 info_cv;
169 ceph_decode_need(p, end, 1 + sizeof(u32), bad);
170 info_cv = ceph_decode_8(p);
171 info_len = ceph_decode_32(p);
172 info_end = *p + info_len;
173 if (info_end > end)
174 goto bad;
175 }
176
177 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
Sage Weil94045e12009-11-19 15:31:50 -0800178 *p += sizeof(u64);
Sage Weilc89136e2009-10-14 09:59:09 -0700179 namelen = ceph_decode_32(p); /* skip mds name */
Sage Weil2f2dc052009-10-06 11:31:09 -0700180 *p += namelen;
181
182 ceph_decode_need(p, end,
Sage Weile251e282009-10-07 16:38:19 -0700183 4*sizeof(u32) + sizeof(u64) +
Sage Weil2f2dc052009-10-06 11:31:09 -0700184 sizeof(addr) + sizeof(struct ceph_timespec),
185 bad);
Sage Weilc89136e2009-10-14 09:59:09 -0700186 mds = ceph_decode_32(p);
187 inc = ceph_decode_32(p);
188 state = ceph_decode_32(p);
189 state_seq = ceph_decode_64(p);
Jeff Laytonf3848af2019-06-04 11:26:36 -0400190 err = ceph_decode_entity_addr(p, end, &addr);
191 if (err)
192 goto corrupt;
Sage Weil0deb01c2010-06-17 14:19:01 -0700193 ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
Xiubo Lida08e1e2019-11-26 07:24:20 -0500194 laggy = laggy_since.tv_sec != 0 || laggy_since.tv_nsec != 0;
Sage Weil2f2dc052009-10-06 11:31:09 -0700195 *p += sizeof(u32);
196 ceph_decode_32_safe(p, end, namelen, bad);
Sage Weile251e282009-10-07 16:38:19 -0700197 *p += namelen;
Yan, Zhengd463a432016-03-31 15:53:01 +0800198 if (info_v >= 2) {
Sage Weil2f2dc052009-10-06 11:31:09 -0700199 ceph_decode_32_safe(p, end, num_export_targets, bad);
200 pexport_targets = *p;
Sage Weile251e282009-10-07 16:38:19 -0700201 *p += num_export_targets * sizeof(u32);
Sage Weil2f2dc052009-10-06 11:31:09 -0700202 } else {
203 num_export_targets = 0;
204 }
205
Yan, Zhengd463a432016-03-31 15:53:01 +0800206 if (info_end && *p != info_end) {
207 if (*p > info_end)
208 goto bad;
209 *p = info_end;
210 }
211
Xiubo Lida08e1e2019-11-26 07:24:20 -0500212 dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s%s\n",
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700213 i+1, n, global_id, mds, inc,
Jeff Laytonb726ec92019-05-06 09:38:47 -0400214 ceph_pr_addr(&addr),
Xiubo Lida08e1e2019-11-26 07:24:20 -0500215 ceph_mds_state_name(state),
216 laggy ? "(laggy)" : "");
Dan Carpenter6af86522013-05-29 06:46:56 -0500217
Yan, Zheng76201b62017-03-28 17:04:13 +0800218 if (mds < 0 || state <= 0)
Dan Carpenter6af86522013-05-29 06:46:56 -0500219 continue;
220
Yan, Zheng76201b62017-03-28 17:04:13 +0800221 if (mds >= m->m_num_mds) {
222 int new_num = max(mds + 1, m->m_num_mds * 2);
223 void *new_m_info = krealloc(m->m_info,
224 new_num * sizeof(*m->m_info),
225 GFP_NOFS | __GFP_ZERO);
226 if (!new_m_info)
227 goto nomem;
228 m->m_info = new_m_info;
229 m->m_num_mds = new_num;
230 }
231
Dan Carpenter6af86522013-05-29 06:46:56 -0500232 info = &m->m_info[mds];
233 info->global_id = global_id;
234 info->state = state;
235 info->addr = addr;
Xiubo Lida08e1e2019-11-26 07:24:20 -0500236 info->laggy = laggy;
Dan Carpenter6af86522013-05-29 06:46:56 -0500237 info->num_export_targets = num_export_targets;
238 if (num_export_targets) {
239 info->export_targets = kcalloc(num_export_targets,
240 sizeof(u32), GFP_NOFS);
Markus Elfringd37b1d92017-08-20 20:22:02 +0200241 if (!info->export_targets)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800242 goto nomem;
Dan Carpenter6af86522013-05-29 06:46:56 -0500243 for (j = 0; j < num_export_targets; j++)
244 info->export_targets[j] =
245 ceph_decode_32(&pexport_targets);
246 } else {
247 info->export_targets = NULL;
Sage Weil2f2dc052009-10-06 11:31:09 -0700248 }
249 }
Yan, Zheng76201b62017-03-28 17:04:13 +0800250 if (m->m_num_mds > m->m_max_mds) {
251 /* find max up mds */
252 for (i = m->m_num_mds; i >= m->m_max_mds; i--) {
253 if (i == 0 || m->m_info[i-1].state > 0)
254 break;
255 }
256 m->m_num_mds = i;
257 }
Sage Weil2f2dc052009-10-06 11:31:09 -0700258
259 /* pg_pools */
260 ceph_decode_32_safe(p, end, n, bad);
261 m->m_num_data_pg_pools = n;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800262 m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
Sage Weil2f2dc052009-10-06 11:31:09 -0700263 if (!m->m_data_pg_pools)
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800264 goto nomem;
Sage Weil4f6a7e52013-02-23 10:41:09 -0800265 ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
Sage Weil2f2dc052009-10-06 11:31:09 -0700266 for (i = 0; i < n; i++)
Sage Weil4f6a7e52013-02-23 10:41:09 -0800267 m->m_data_pg_pools[i] = ceph_decode_64(p);
268 m->m_cas_pg_pool = ceph_decode_64(p);
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800269 m->m_enabled = m->m_epoch > 1;
Sage Weil2f2dc052009-10-06 11:31:09 -0700270
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800271 mdsmap_ev = 1;
272 if (mdsmap_v >= 2) {
273 ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
274 }
275 if (mdsmap_ev >= 3) {
276 if (__decode_and_drop_compat_set(p, end) < 0)
277 goto bad_ext;
278 }
279 /* metadata_pool */
280 if (mdsmap_ev < 5) {
281 __decode_and_drop_type(p, end, u32, bad_ext);
282 } else {
283 __decode_and_drop_type(p, end, u64, bad_ext);
284 }
285
286 /* created + modified + tableserver */
287 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
288 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
289 __decode_and_drop_type(p, end, u32, bad_ext);
290
291 /* in */
292 {
293 int num_laggy = 0;
294 ceph_decode_32_safe(p, end, n, bad_ext);
295 ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);
296
297 for (i = 0; i < n; i++) {
298 s32 mds = ceph_decode_32(p);
Yan, Zheng76201b62017-03-28 17:04:13 +0800299 if (mds >= 0 && mds < m->m_num_mds) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800300 if (m->m_info[mds].laggy)
301 num_laggy++;
302 }
303 }
304 m->m_num_laggy = num_laggy;
Yan, Zheng76201b62017-03-28 17:04:13 +0800305
306 if (n > m->m_num_mds) {
307 void *new_m_info = krealloc(m->m_info,
308 n * sizeof(*m->m_info),
309 GFP_NOFS | __GFP_ZERO);
310 if (!new_m_info)
311 goto nomem;
312 m->m_info = new_m_info;
313 }
314 m->m_num_mds = n;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800315 }
316
317 /* inc */
318 __decode_and_drop_map(p, end, u32, u32, bad_ext);
319 /* up */
320 __decode_and_drop_map(p, end, u32, u64, bad_ext);
321 /* failed */
322 __decode_and_drop_set(p, end, u32, bad_ext);
323 /* stopped */
324 __decode_and_drop_set(p, end, u32, bad_ext);
325
326 if (mdsmap_ev >= 4) {
327 /* last_failure_osd_epoch */
328 __decode_and_drop_type(p, end, u32, bad_ext);
329 }
330 if (mdsmap_ev >= 6) {
331 /* ever_allowed_snaps */
332 __decode_and_drop_type(p, end, u8, bad_ext);
333 /* explicitly_allowed_snaps */
334 __decode_and_drop_type(p, end, u8, bad_ext);
335 }
336 if (mdsmap_ev >= 7) {
337 /* inline_data_enabled */
338 __decode_and_drop_type(p, end, u8, bad_ext);
339 }
340 if (mdsmap_ev >= 8) {
341 u32 name_len;
342 /* enabled */
343 ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
344 ceph_decode_32_safe(p, end, name_len, bad_ext);
345 ceph_decode_need(p, end, name_len, bad_ext);
346 *p += name_len;
347 }
348 /* damaged */
349 if (mdsmap_ev >= 9) {
350 size_t need;
351 ceph_decode_32_safe(p, end, n, bad_ext);
352 need = sizeof(u32) * n;
353 ceph_decode_need(p, end, need, bad_ext);
354 *p += need;
355 m->m_damaged = n > 0;
356 } else {
357 m->m_damaged = false;
358 }
359bad_ext:
Xiubo Lida08e1e2019-11-26 07:24:20 -0500360 dout("mdsmap_decode m_enabled: %d, m_damaged: %d, m_num_laggy: %d\n",
361 !!m->m_enabled, !!m->m_damaged, m->m_num_laggy);
Yan, Zhengd463a432016-03-31 15:53:01 +0800362 *p = end;
Sage Weil2f2dc052009-10-06 11:31:09 -0700363 dout("mdsmap_decode success epoch %u\n", m->m_epoch);
364 return m;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800365nomem:
Sage Weil2f2dc052009-10-06 11:31:09 -0700366 err = -ENOMEM;
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800367 goto out_err;
Jeff Laytonf3848af2019-06-04 11:26:36 -0400368corrupt:
Sage Weil2f2dc052009-10-06 11:31:09 -0700369 pr_err("corrupt mdsmap\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -0800370 print_hex_dump(KERN_DEBUG, "mdsmap: ",
371 DUMP_PREFIX_OFFSET, 16, 1,
372 start, end - start, true);
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800373out_err:
Sage Weil2f2dc052009-10-06 11:31:09 -0700374 ceph_mdsmap_destroy(m);
Emil Goodec213b502013-05-28 16:59:00 +0200375 return ERR_PTR(err);
Jeff Laytonf3848af2019-06-04 11:26:36 -0400376bad:
377 err = -EINVAL;
378 goto corrupt;
Sage Weil2f2dc052009-10-06 11:31:09 -0700379}
380
381void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
382{
383 int i;
384
Yan, Zheng76201b62017-03-28 17:04:13 +0800385 for (i = 0; i < m->m_num_mds; i++)
Sage Weil2f2dc052009-10-06 11:31:09 -0700386 kfree(m->m_info[i].export_targets);
387 kfree(m->m_info);
388 kfree(m->m_data_pg_pools);
389 kfree(m);
390}
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800391
392bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
393{
394 int i, nr_active = 0;
395 if (!m->m_enabled)
396 return false;
397 if (m->m_damaged)
398 return false;
399 if (m->m_num_laggy > 0)
400 return false;
Yan, Zheng76201b62017-03-28 17:04:13 +0800401 for (i = 0; i < m->m_num_mds; i++) {
Yan, Zhenge9e427f2016-11-10 16:02:06 +0800402 if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
403 nr_active++;
404 }
405 return nr_active > 0;
406}