xref: /openbmc/linux/fs/ceph/mdsmap.c (revision a5b2c10c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3 
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 
10 #include <linux/ceph/mdsmap.h>
11 #include <linux/ceph/messenger.h>
12 #include <linux/ceph/decode.h>
13 
14 #include "super.h"
15 
16 #define CEPH_MDS_IS_READY(i, ignore_laggy) \
17 	(m->m_info[i].state > 0 && ignore_laggy ? true : !m->m_info[i].laggy)
18 
19 static int __mdsmap_get_random_mds(struct ceph_mdsmap *m, bool ignore_laggy)
20 {
21 	int n = 0;
22 	int i, j;
23 
24 	/* count */
25 	for (i = 0; i < m->possible_max_rank; i++)
26 		if (CEPH_MDS_IS_READY(i, ignore_laggy))
27 			n++;
28 	if (n == 0)
29 		return -1;
30 
31 	/* pick */
32 	n = prandom_u32() % n;
33 	for (j = 0, i = 0; i < m->possible_max_rank; i++) {
34 		if (CEPH_MDS_IS_READY(i, ignore_laggy))
35 			j++;
36 		if (j > n)
37 			break;
38 	}
39 
40 	return i;
41 }
42 
43 /*
44  * choose a random mds that is "up" (i.e. has a state > 0), or -1.
45  */
46 int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
47 {
48 	int mds;
49 
50 	mds = __mdsmap_get_random_mds(m, false);
51 	if (mds == m->possible_max_rank || mds == -1)
52 		mds = __mdsmap_get_random_mds(m, true);
53 
54 	return mds == m->possible_max_rank ? -1 : mds;
55 }
56 
57 #define __decode_and_drop_type(p, end, type, bad)		\
58 	do {							\
59 		if (*p + sizeof(type) > end)			\
60 			goto bad;				\
61 		*p += sizeof(type);				\
62 	} while (0)
63 
64 #define __decode_and_drop_set(p, end, type, bad)		\
65 	do {							\
66 		u32 n;						\
67 		size_t need;					\
68 		ceph_decode_32_safe(p, end, n, bad);		\
69 		need = sizeof(type) * n;			\
70 		ceph_decode_need(p, end, need, bad);		\
71 		*p += need;					\
72 	} while (0)
73 
74 #define __decode_and_drop_map(p, end, ktype, vtype, bad)	\
75 	do {							\
76 		u32 n;						\
77 		size_t need;					\
78 		ceph_decode_32_safe(p, end, n, bad);		\
79 		need = (sizeof(ktype) + sizeof(vtype)) * n;	\
80 		ceph_decode_need(p, end, need, bad);		\
81 		*p += need;					\
82 	} while (0)
83 
84 
85 static int __decode_and_drop_compat_set(void **p, void* end)
86 {
87 	int i;
88 	/* compat, ro_compat, incompat*/
89 	for (i = 0; i < 3; i++) {
90 		u32 n;
91 		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
92 		/* mask */
93 		*p += sizeof(u64);
94 		/* names (map<u64, string>) */
95 		n = ceph_decode_32(p);
96 		while (n-- > 0) {
97 			u32 len;
98 			ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
99 					 bad);
100 			*p += sizeof(u64);
101 			len = ceph_decode_32(p);
102 			ceph_decode_need(p, end, len, bad);
103 			*p += len;
104 		}
105 	}
106 	return 0;
107 bad:
108 	return -1;
109 }
110 
111 /*
112  * Decode an MDS map
113  *
114  * Ignore any fields we don't care about (there are quite a few of
115  * them).
116  */
117 struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
118 {
119 	struct ceph_mdsmap *m;
120 	const void *start = *p;
121 	int i, j, n;
122 	int err;
123 	u8 mdsmap_v;
124 	u16 mdsmap_ev;
125 
126 	m = kzalloc(sizeof(*m), GFP_NOFS);
127 	if (!m)
128 		return ERR_PTR(-ENOMEM);
129 
130 	ceph_decode_need(p, end, 1 + 1, bad);
131 	mdsmap_v = ceph_decode_8(p);
132 	*p += sizeof(u8);			/* mdsmap_cv */
133 	if (mdsmap_v >= 4) {
134 	       u32 mdsmap_len;
135 	       ceph_decode_32_safe(p, end, mdsmap_len, bad);
136 	       if (end < *p + mdsmap_len)
137 		       goto bad;
138 	       end = *p + mdsmap_len;
139 	}
140 
141 	ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
142 	m->m_epoch = ceph_decode_32(p);
143 	m->m_client_epoch = ceph_decode_32(p);
144 	m->m_last_failure = ceph_decode_32(p);
145 	m->m_root = ceph_decode_32(p);
146 	m->m_session_timeout = ceph_decode_32(p);
147 	m->m_session_autoclose = ceph_decode_32(p);
148 	m->m_max_file_size = ceph_decode_64(p);
149 	m->m_max_mds = ceph_decode_32(p);
150 
151 	/*
152 	 * pick out the active nodes as the m_num_active_mds, the
153 	 * m_num_active_mds maybe larger than m_max_mds when decreasing
154 	 * the max_mds in cluster side, in other case it should less
155 	 * than or equal to m_max_mds.
156 	 */
157 	m->m_num_active_mds = n = ceph_decode_32(p);
158 
159 	/*
160 	 * the possible max rank, it maybe larger than the m_num_active_mds,
161 	 * for example if the mds_max == 2 in the cluster, when the MDS(0)
162 	 * was laggy and being replaced by a new MDS, we will temporarily
163 	 * receive a new mds map with n_num_mds == 1 and the active MDS(1),
164 	 * and the mds rank >= m_num_active_mds.
165 	 */
166 	m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds);
167 
168 	m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS);
169 	if (!m->m_info)
170 		goto nomem;
171 
172 	/* pick out active nodes from mds_info (state > 0) */
173 	for (i = 0; i < n; i++) {
174 		u64 global_id;
175 		u32 namelen;
176 		s32 mds, inc, state;
177 		u8 info_v;
178 		void *info_end = NULL;
179 		struct ceph_entity_addr addr;
180 		u32 num_export_targets;
181 		void *pexport_targets = NULL;
182 		struct ceph_timespec laggy_since;
183 		struct ceph_mds_info *info;
184 		bool laggy;
185 
186 		ceph_decode_need(p, end, sizeof(u64) + 1, bad);
187 		global_id = ceph_decode_64(p);
188 		info_v= ceph_decode_8(p);
189 		if (info_v >= 4) {
190 			u32 info_len;
191 			ceph_decode_need(p, end, 1 + sizeof(u32), bad);
192 			*p += sizeof(u8);	/* info_cv */
193 			info_len = ceph_decode_32(p);
194 			info_end = *p + info_len;
195 			if (info_end > end)
196 				goto bad;
197 		}
198 
199 		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
200 		*p += sizeof(u64);
201 		namelen = ceph_decode_32(p);  /* skip mds name */
202 		*p += namelen;
203 
204 		ceph_decode_need(p, end,
205 				 4*sizeof(u32) + sizeof(u64) +
206 				 sizeof(addr) + sizeof(struct ceph_timespec),
207 				 bad);
208 		mds = ceph_decode_32(p);
209 		inc = ceph_decode_32(p);
210 		state = ceph_decode_32(p);
211 		*p += sizeof(u64);		/* state_seq */
212 		err = ceph_decode_entity_addr(p, end, &addr);
213 		if (err)
214 			goto corrupt;
215 		ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
216 		laggy = laggy_since.tv_sec != 0 || laggy_since.tv_nsec != 0;
217 		*p += sizeof(u32);
218 		ceph_decode_32_safe(p, end, namelen, bad);
219 		*p += namelen;
220 		if (info_v >= 2) {
221 			ceph_decode_32_safe(p, end, num_export_targets, bad);
222 			pexport_targets = *p;
223 			*p += num_export_targets * sizeof(u32);
224 		} else {
225 			num_export_targets = 0;
226 		}
227 
228 		if (info_end && *p != info_end) {
229 			if (*p > info_end)
230 				goto bad;
231 			*p = info_end;
232 		}
233 
234 		dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s%s\n",
235 		     i+1, n, global_id, mds, inc,
236 		     ceph_pr_addr(&addr),
237 		     ceph_mds_state_name(state),
238 		     laggy ? "(laggy)" : "");
239 
240 		if (mds < 0 || mds >= m->possible_max_rank) {
241 			pr_warn("mdsmap_decode got incorrect mds(%d)\n", mds);
242 			continue;
243 		}
244 
245 		if (state <= 0) {
246 			pr_warn("mdsmap_decode got incorrect state(%s)\n",
247 				ceph_mds_state_name(state));
248 			continue;
249 		}
250 
251 		info = &m->m_info[mds];
252 		info->global_id = global_id;
253 		info->state = state;
254 		info->addr = addr;
255 		info->laggy = laggy;
256 		info->num_export_targets = num_export_targets;
257 		if (num_export_targets) {
258 			info->export_targets = kcalloc(num_export_targets,
259 						       sizeof(u32), GFP_NOFS);
260 			if (!info->export_targets)
261 				goto nomem;
262 			for (j = 0; j < num_export_targets; j++)
263 				info->export_targets[j] =
264 				       ceph_decode_32(&pexport_targets);
265 		} else {
266 			info->export_targets = NULL;
267 		}
268 	}
269 
270 	/* pg_pools */
271 	ceph_decode_32_safe(p, end, n, bad);
272 	m->m_num_data_pg_pools = n;
273 	m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
274 	if (!m->m_data_pg_pools)
275 		goto nomem;
276 	ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
277 	for (i = 0; i < n; i++)
278 		m->m_data_pg_pools[i] = ceph_decode_64(p);
279 	m->m_cas_pg_pool = ceph_decode_64(p);
280 	m->m_enabled = m->m_epoch > 1;
281 
282 	mdsmap_ev = 1;
283 	if (mdsmap_v >= 2) {
284 		ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
285 	}
286 	if (mdsmap_ev >= 3) {
287 		if (__decode_and_drop_compat_set(p, end) < 0)
288 			goto bad_ext;
289 	}
290 	/* metadata_pool */
291 	if (mdsmap_ev < 5) {
292 		__decode_and_drop_type(p, end, u32, bad_ext);
293 	} else {
294 		__decode_and_drop_type(p, end, u64, bad_ext);
295 	}
296 
297 	/* created + modified + tableserver */
298 	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
299 	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
300 	__decode_and_drop_type(p, end, u32, bad_ext);
301 
302 	/* in */
303 	{
304 		int num_laggy = 0;
305 		ceph_decode_32_safe(p, end, n, bad_ext);
306 		ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);
307 
308 		for (i = 0; i < n; i++) {
309 			s32 mds = ceph_decode_32(p);
310 			if (mds >= 0 && mds < m->possible_max_rank) {
311 				if (m->m_info[mds].laggy)
312 					num_laggy++;
313 			}
314 		}
315 		m->m_num_laggy = num_laggy;
316 
317 		if (n > m->possible_max_rank) {
318 			void *new_m_info = krealloc(m->m_info,
319 						    n * sizeof(*m->m_info),
320 						    GFP_NOFS | __GFP_ZERO);
321 			if (!new_m_info)
322 				goto nomem;
323 			m->m_info = new_m_info;
324 		}
325 		m->possible_max_rank = n;
326 	}
327 
328 	/* inc */
329 	__decode_and_drop_map(p, end, u32, u32, bad_ext);
330 	/* up */
331 	__decode_and_drop_map(p, end, u32, u64, bad_ext);
332 	/* failed */
333 	__decode_and_drop_set(p, end, u32, bad_ext);
334 	/* stopped */
335 	__decode_and_drop_set(p, end, u32, bad_ext);
336 
337 	if (mdsmap_ev >= 4) {
338 		/* last_failure_osd_epoch */
339 		__decode_and_drop_type(p, end, u32, bad_ext);
340 	}
341 	if (mdsmap_ev >= 6) {
342 		/* ever_allowed_snaps */
343 		__decode_and_drop_type(p, end, u8, bad_ext);
344 		/* explicitly_allowed_snaps */
345 		__decode_and_drop_type(p, end, u8, bad_ext);
346 	}
347 	if (mdsmap_ev >= 7) {
348 		/* inline_data_enabled */
349 		__decode_and_drop_type(p, end, u8, bad_ext);
350 	}
351 	if (mdsmap_ev >= 8) {
352 		u32 name_len;
353 		/* enabled */
354 		ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
355 		ceph_decode_32_safe(p, end, name_len, bad_ext);
356 		ceph_decode_need(p, end, name_len, bad_ext);
357 		*p += name_len;
358 	}
359 	/* damaged */
360 	if (mdsmap_ev >= 9) {
361 		size_t need;
362 		ceph_decode_32_safe(p, end, n, bad_ext);
363 		need = sizeof(u32) * n;
364 		ceph_decode_need(p, end, need, bad_ext);
365 		*p += need;
366 		m->m_damaged = n > 0;
367 	} else {
368 		m->m_damaged = false;
369 	}
370 bad_ext:
371 	dout("mdsmap_decode m_enabled: %d, m_damaged: %d, m_num_laggy: %d\n",
372 	     !!m->m_enabled, !!m->m_damaged, m->m_num_laggy);
373 	*p = end;
374 	dout("mdsmap_decode success epoch %u\n", m->m_epoch);
375 	return m;
376 nomem:
377 	err = -ENOMEM;
378 	goto out_err;
379 corrupt:
380 	pr_err("corrupt mdsmap\n");
381 	print_hex_dump(KERN_DEBUG, "mdsmap: ",
382 		       DUMP_PREFIX_OFFSET, 16, 1,
383 		       start, end - start, true);
384 out_err:
385 	ceph_mdsmap_destroy(m);
386 	return ERR_PTR(err);
387 bad:
388 	err = -EINVAL;
389 	goto corrupt;
390 }
391 
392 void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
393 {
394 	int i;
395 
396 	for (i = 0; i < m->possible_max_rank; i++)
397 		kfree(m->m_info[i].export_targets);
398 	kfree(m->m_info);
399 	kfree(m->m_data_pg_pools);
400 	kfree(m);
401 }
402 
403 bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
404 {
405 	int i, nr_active = 0;
406 	if (!m->m_enabled)
407 		return false;
408 	if (m->m_damaged)
409 		return false;
410 	if (m->m_num_laggy == m->m_num_active_mds)
411 		return false;
412 	for (i = 0; i < m->possible_max_rank; i++) {
413 		if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
414 			nr_active++;
415 	}
416 	return nr_active > 0;
417 }
418