xref: /linux/fs/ceph/mdsmap.c (revision d93231a6bc8a452323d5fef16cca7107ce483a27)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d14c5d2SYehuda Sadeh #include <linux/ceph/ceph_debug.h>
32f2dc053SSage Weil 
42f2dc053SSage Weil #include <linux/bug.h>
52f2dc053SSage Weil #include <linux/err.h>
62f2dc053SSage Weil #include <linux/random.h>
72f2dc053SSage Weil #include <linux/slab.h>
82f2dc053SSage Weil #include <linux/types.h>
92f2dc053SSage Weil 
103d14c5d2SYehuda Sadeh #include <linux/ceph/mdsmap.h>
113d14c5d2SYehuda Sadeh #include <linux/ceph/messenger.h>
123d14c5d2SYehuda Sadeh #include <linux/ceph/decode.h>
132f2dc053SSage Weil 
142f2dc053SSage Weil #include "super.h"
152f2dc053SSage Weil 
165d47648fSXiubo Li #define CEPH_MDS_IS_READY(i, ignore_laggy) \
17b38c9eb4SXiubo Li 	(m->m_info[i].state > 0 && ignore_laggy ? true : !m->m_info[i].laggy)
182f2dc053SSage Weil 
195d47648fSXiubo Li static int __mdsmap_get_random_mds(struct ceph_mdsmap *m, bool ignore_laggy)
202f2dc053SSage Weil {
212f2dc053SSage Weil 	int n = 0;
2274d6f030SXiubo Li 	int i, j;
23a84cd293SSam Lang 
242f2dc053SSage Weil 	/* count */
25b38c9eb4SXiubo Li 	for (i = 0; i < m->possible_max_rank; i++)
265d47648fSXiubo Li 		if (CEPH_MDS_IS_READY(i, ignore_laggy))
272f2dc053SSage Weil 			n++;
282f2dc053SSage Weil 	if (n == 0)
292f2dc053SSage Weil 		return -1;
302f2dc053SSage Weil 
312f2dc053SSage Weil 	/* pick */
32a84cd293SSam Lang 	n = prandom_u32() % n;
33b38c9eb4SXiubo Li 	for (j = 0, i = 0; i < m->possible_max_rank; i++) {
345d47648fSXiubo Li 		if (CEPH_MDS_IS_READY(i, ignore_laggy))
3574d6f030SXiubo Li 			j++;
3674d6f030SXiubo Li 		if (j > n)
3774d6f030SXiubo Li 			break;
3874d6f030SXiubo Li 	}
392f2dc053SSage Weil 
402f2dc053SSage Weil 	return i;
412f2dc053SSage Weil }
422f2dc053SSage Weil 
435d47648fSXiubo Li /*
445d47648fSXiubo Li  * choose a random mds that is "up" (i.e. has a state > 0), or -1.
455d47648fSXiubo Li  */
465d47648fSXiubo Li int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
475d47648fSXiubo Li {
485d47648fSXiubo Li 	int mds;
495d47648fSXiubo Li 
505d47648fSXiubo Li 	mds = __mdsmap_get_random_mds(m, false);
51b38c9eb4SXiubo Li 	if (mds == m->possible_max_rank || mds == -1)
525d47648fSXiubo Li 		mds = __mdsmap_get_random_mds(m, true);
535d47648fSXiubo Li 
54b38c9eb4SXiubo Li 	return mds == m->possible_max_rank ? -1 : mds;
555d47648fSXiubo Li }
565d47648fSXiubo Li 
57e9e427f0SYan, Zheng #define __decode_and_drop_type(p, end, type, bad)		\
58e9e427f0SYan, Zheng 	do {							\
59e9e427f0SYan, Zheng 		if (*p + sizeof(type) > end)			\
60e9e427f0SYan, Zheng 			goto bad;				\
61e9e427f0SYan, Zheng 		*p += sizeof(type);				\
62e9e427f0SYan, Zheng 	} while (0)
63e9e427f0SYan, Zheng 
64e9e427f0SYan, Zheng #define __decode_and_drop_set(p, end, type, bad)		\
65e9e427f0SYan, Zheng 	do {							\
66e9e427f0SYan, Zheng 		u32 n;						\
67e9e427f0SYan, Zheng 		size_t need;					\
68e9e427f0SYan, Zheng 		ceph_decode_32_safe(p, end, n, bad);		\
69e9e427f0SYan, Zheng 		need = sizeof(type) * n;			\
70e9e427f0SYan, Zheng 		ceph_decode_need(p, end, need, bad);		\
71e9e427f0SYan, Zheng 		*p += need;					\
72e9e427f0SYan, Zheng 	} while (0)
73e9e427f0SYan, Zheng 
74e9e427f0SYan, Zheng #define __decode_and_drop_map(p, end, ktype, vtype, bad)	\
75e9e427f0SYan, Zheng 	do {							\
76e9e427f0SYan, Zheng 		u32 n;						\
77e9e427f0SYan, Zheng 		size_t need;					\
78e9e427f0SYan, Zheng 		ceph_decode_32_safe(p, end, n, bad);		\
79e9e427f0SYan, Zheng 		need = (sizeof(ktype) + sizeof(vtype)) * n;	\
80e9e427f0SYan, Zheng 		ceph_decode_need(p, end, need, bad);		\
81e9e427f0SYan, Zheng 		*p += need;					\
82e9e427f0SYan, Zheng 	} while (0)
83e9e427f0SYan, Zheng 
84e9e427f0SYan, Zheng 
85e9e427f0SYan, Zheng static int __decode_and_drop_compat_set(void **p, void* end)
86e9e427f0SYan, Zheng {
87e9e427f0SYan, Zheng 	int i;
88e9e427f0SYan, Zheng 	/* compat, ro_compat, incompat*/
89e9e427f0SYan, Zheng 	for (i = 0; i < 3; i++) {
90e9e427f0SYan, Zheng 		u32 n;
91e9e427f0SYan, Zheng 		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
92e9e427f0SYan, Zheng 		/* mask */
93e9e427f0SYan, Zheng 		*p += sizeof(u64);
94e9e427f0SYan, Zheng 		/* names (map<u64, string>) */
95e9e427f0SYan, Zheng 		n = ceph_decode_32(p);
96e9e427f0SYan, Zheng 		while (n-- > 0) {
97e9e427f0SYan, Zheng 			u32 len;
98e9e427f0SYan, Zheng 			ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
99e9e427f0SYan, Zheng 					 bad);
100e9e427f0SYan, Zheng 			*p += sizeof(u64);
101e9e427f0SYan, Zheng 			len = ceph_decode_32(p);
102e9e427f0SYan, Zheng 			ceph_decode_need(p, end, len, bad);
103e9e427f0SYan, Zheng 			*p += len;
104e9e427f0SYan, Zheng 		}
105e9e427f0SYan, Zheng 	}
106e9e427f0SYan, Zheng 	return 0;
107e9e427f0SYan, Zheng bad:
108e9e427f0SYan, Zheng 	return -1;
109e9e427f0SYan, Zheng }
110e9e427f0SYan, Zheng 
1112f2dc053SSage Weil /*
1122f2dc053SSage Weil  * Decode an MDS map
1132f2dc053SSage Weil  *
1142f2dc053SSage Weil  * Ignore any fields we don't care about (there are quite a few of
1152f2dc053SSage Weil  * them).
1162f2dc053SSage Weil  */
117a5cbd5fcSIlya Dryomov struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end, bool msgr2)
1182f2dc053SSage Weil {
1192f2dc053SSage Weil 	struct ceph_mdsmap *m;
1209ec7cab1SSage Weil 	const void *start = *p;
1212f2dc053SSage Weil 	int i, j, n;
122f3848af1SJeff Layton 	int err;
1238e298debSJia Yang 	u8 mdsmap_v;
124e9e427f0SYan, Zheng 	u16 mdsmap_ev;
125d517b398SXiubo Li 	u32 target;
1262f2dc053SSage Weil 
1272f2dc053SSage Weil 	m = kzalloc(sizeof(*m), GFP_NOFS);
128d37b1d99SMarkus Elfring 	if (!m)
1292f2dc053SSage Weil 		return ERR_PTR(-ENOMEM);
1302f2dc053SSage Weil 
131d463a43dSYan, Zheng 	ceph_decode_need(p, end, 1 + 1, bad);
132d463a43dSYan, Zheng 	mdsmap_v = ceph_decode_8(p);
1338e298debSJia Yang 	*p += sizeof(u8);			/* mdsmap_cv */
134d463a43dSYan, Zheng 	if (mdsmap_v >= 4) {
135d463a43dSYan, Zheng 	       u32 mdsmap_len;
136d463a43dSYan, Zheng 	       ceph_decode_32_safe(p, end, mdsmap_len, bad);
137d463a43dSYan, Zheng 	       if (end < *p + mdsmap_len)
1384f6a7e5eSSage Weil 		       goto bad;
139d463a43dSYan, Zheng 	       end = *p + mdsmap_len;
1404f6a7e5eSSage Weil 	}
1412f2dc053SSage Weil 
1422f2dc053SSage Weil 	ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
143c89136eaSSage Weil 	m->m_epoch = ceph_decode_32(p);
144c89136eaSSage Weil 	m->m_client_epoch = ceph_decode_32(p);
145c89136eaSSage Weil 	m->m_last_failure = ceph_decode_32(p);
146c89136eaSSage Weil 	m->m_root = ceph_decode_32(p);
147c89136eaSSage Weil 	m->m_session_timeout = ceph_decode_32(p);
148c89136eaSSage Weil 	m->m_session_autoclose = ceph_decode_32(p);
149c89136eaSSage Weil 	m->m_max_file_size = ceph_decode_64(p);
150c89136eaSSage Weil 	m->m_max_mds = ceph_decode_32(p);
1514d7ace02SXiubo Li 
1524d7ace02SXiubo Li 	/*
153b38c9eb4SXiubo Li 	 * pick out the active nodes as the m_num_active_mds, the
154b38c9eb4SXiubo Li 	 * m_num_active_mds maybe larger than m_max_mds when decreasing
155b38c9eb4SXiubo Li 	 * the max_mds in cluster side, in other case it should less
156b38c9eb4SXiubo Li 	 * than or equal to m_max_mds.
1574d7ace02SXiubo Li 	 */
158b38c9eb4SXiubo Li 	m->m_num_active_mds = n = ceph_decode_32(p);
1594d7ace02SXiubo Li 
1604d7ace02SXiubo Li 	/*
161b38c9eb4SXiubo Li 	 * the possible max rank, it maybe larger than the m_num_active_mds,
1624d7ace02SXiubo Li 	 * for example if the mds_max == 2 in the cluster, when the MDS(0)
1634d7ace02SXiubo Li 	 * was laggy and being replaced by a new MDS, we will temporarily
1644d7ace02SXiubo Li 	 * receive a new mds map with n_num_mds == 1 and the active MDS(1),
165b38c9eb4SXiubo Li 	 * and the mds rank >= m_num_active_mds.
1664d7ace02SXiubo Li 	 */
167b38c9eb4SXiubo Li 	m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds);
1682f2dc053SSage Weil 
169b38c9eb4SXiubo Li 	m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS);
170d37b1d99SMarkus Elfring 	if (!m->m_info)
171e9e427f0SYan, Zheng 		goto nomem;
1722f2dc053SSage Weil 
1732f2dc053SSage Weil 	/* pick out active nodes from mds_info (state > 0) */
1742f2dc053SSage Weil 	for (i = 0; i < n; i++) {
17594045e11SSage Weil 		u64 global_id;
1762f2dc053SSage Weil 		u32 namelen;
1772f2dc053SSage Weil 		s32 mds, inc, state;
178d463a43dSYan, Zheng 		u8 info_v;
179d463a43dSYan, Zheng 		void *info_end = NULL;
1802f2dc053SSage Weil 		struct ceph_entity_addr addr;
1812f2dc053SSage Weil 		u32 num_export_targets;
1822f2dc053SSage Weil 		void *pexport_targets = NULL;
1830deb01c9SSage Weil 		struct ceph_timespec laggy_since;
1846af86528SDan Carpenter 		struct ceph_mds_info *info;
185da08e1e1SXiubo Li 		bool laggy;
1862f2dc053SSage Weil 
187d463a43dSYan, Zheng 		ceph_decode_need(p, end, sizeof(u64) + 1, bad);
18894045e11SSage Weil 		global_id = ceph_decode_64(p);
189d463a43dSYan, Zheng 		info_v= ceph_decode_8(p);
190d463a43dSYan, Zheng 		if (info_v >= 4) {
191d463a43dSYan, Zheng 			u32 info_len;
192d463a43dSYan, Zheng 			ceph_decode_need(p, end, 1 + sizeof(u32), bad);
1938e298debSJia Yang 			*p += sizeof(u8);	/* info_cv */
194d463a43dSYan, Zheng 			info_len = ceph_decode_32(p);
195d463a43dSYan, Zheng 			info_end = *p + info_len;
196d463a43dSYan, Zheng 			if (info_end > end)
197d463a43dSYan, Zheng 				goto bad;
198d463a43dSYan, Zheng 		}
199d463a43dSYan, Zheng 
200d463a43dSYan, Zheng 		ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
20194045e11SSage Weil 		*p += sizeof(u64);
202c89136eaSSage Weil 		namelen = ceph_decode_32(p);  /* skip mds name */
2032f2dc053SSage Weil 		*p += namelen;
2042f2dc053SSage Weil 
205a5cbd5fcSIlya Dryomov 		ceph_decode_32_safe(p, end, mds, bad);
206a5cbd5fcSIlya Dryomov 		ceph_decode_32_safe(p, end, inc, bad);
207a5cbd5fcSIlya Dryomov 		ceph_decode_32_safe(p, end, state, bad);
2088e298debSJia Yang 		*p += sizeof(u64);		/* state_seq */
209a5cbd5fcSIlya Dryomov 		if (info_v >= 8)
210a5cbd5fcSIlya Dryomov 			err = ceph_decode_entity_addrvec(p, end, msgr2, &addr);
211a5cbd5fcSIlya Dryomov 		else
212f3848af1SJeff Layton 			err = ceph_decode_entity_addr(p, end, &addr);
213f3848af1SJeff Layton 		if (err)
214f3848af1SJeff Layton 			goto corrupt;
215a5cbd5fcSIlya Dryomov 
216a5cbd5fcSIlya Dryomov 		ceph_decode_copy_safe(p, end, &laggy_since, sizeof(laggy_since),
217a5cbd5fcSIlya Dryomov 				      bad);
218da08e1e1SXiubo Li 		laggy = laggy_since.tv_sec != 0 || laggy_since.tv_nsec != 0;
2192f2dc053SSage Weil 		*p += sizeof(u32);
2202f2dc053SSage Weil 		ceph_decode_32_safe(p, end, namelen, bad);
221e251e288SSage Weil 		*p += namelen;
222d463a43dSYan, Zheng 		if (info_v >= 2) {
2232f2dc053SSage Weil 			ceph_decode_32_safe(p, end, num_export_targets, bad);
2242f2dc053SSage Weil 			pexport_targets = *p;
225e251e288SSage Weil 			*p += num_export_targets * sizeof(u32);
2262f2dc053SSage Weil 		} else {
2272f2dc053SSage Weil 			num_export_targets = 0;
2282f2dc053SSage Weil 		}
2292f2dc053SSage Weil 
230d463a43dSYan, Zheng 		if (info_end && *p != info_end) {
231d463a43dSYan, Zheng 			if (*p > info_end)
232d463a43dSYan, Zheng 				goto bad;
233d463a43dSYan, Zheng 			*p = info_end;
234d463a43dSYan, Zheng 		}
235d463a43dSYan, Zheng 
236da08e1e1SXiubo Li 		dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s%s\n",
2373d14c5d2SYehuda Sadeh 		     i+1, n, global_id, mds, inc,
238b726ec97SJeff Layton 		     ceph_pr_addr(&addr),
239da08e1e1SXiubo Li 		     ceph_mds_state_name(state),
240da08e1e1SXiubo Li 		     laggy ? "(laggy)" : "");
2416af86528SDan Carpenter 
242b38c9eb4SXiubo Li 		if (mds < 0 || mds >= m->possible_max_rank) {
2434d7ace02SXiubo Li 			pr_warn("mdsmap_decode got incorrect mds(%d)\n", mds);
2446af86528SDan Carpenter 			continue;
2454d7ace02SXiubo Li 		}
2466af86528SDan Carpenter 
2474d7ace02SXiubo Li 		if (state <= 0) {
248ccd1acdfSLuis Henriques 			dout("mdsmap_decode got incorrect state(%s)\n",
2494d7ace02SXiubo Li 			     ceph_mds_state_name(state));
2504d7ace02SXiubo Li 			continue;
25176201b63SYan, Zheng 		}
25276201b63SYan, Zheng 
2536af86528SDan Carpenter 		info = &m->m_info[mds];
2546af86528SDan Carpenter 		info->global_id = global_id;
2556af86528SDan Carpenter 		info->state = state;
2566af86528SDan Carpenter 		info->addr = addr;
257da08e1e1SXiubo Li 		info->laggy = laggy;
2586af86528SDan Carpenter 		info->num_export_targets = num_export_targets;
2592f2dc053SSage Weil 		if (num_export_targets) {
2606af86528SDan Carpenter 			info->export_targets = kcalloc(num_export_targets,
2616af86528SDan Carpenter 						       sizeof(u32), GFP_NOFS);
262d37b1d99SMarkus Elfring 			if (!info->export_targets)
263e9e427f0SYan, Zheng 				goto nomem;
264d517b398SXiubo Li 			for (j = 0; j < num_export_targets; j++) {
265d517b398SXiubo Li 				target = ceph_decode_32(&pexport_targets);
266d517b398SXiubo Li 				info->export_targets[j] = target;
267d517b398SXiubo Li 			}
2682f2dc053SSage Weil 		} else {
2696af86528SDan Carpenter 			info->export_targets = NULL;
2702f2dc053SSage Weil 		}
2712f2dc053SSage Weil 	}
2722f2dc053SSage Weil 
2732f2dc053SSage Weil 	/* pg_pools */
2742f2dc053SSage Weil 	ceph_decode_32_safe(p, end, n, bad);
2752f2dc053SSage Weil 	m->m_num_data_pg_pools = n;
2764f6a7e5eSSage Weil 	m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
2772f2dc053SSage Weil 	if (!m->m_data_pg_pools)
278e9e427f0SYan, Zheng 		goto nomem;
2794f6a7e5eSSage Weil 	ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
2802f2dc053SSage Weil 	for (i = 0; i < n; i++)
2814f6a7e5eSSage Weil 		m->m_data_pg_pools[i] = ceph_decode_64(p);
2824f6a7e5eSSage Weil 	m->m_cas_pg_pool = ceph_decode_64(p);
283e9e427f0SYan, Zheng 	m->m_enabled = m->m_epoch > 1;
2842f2dc053SSage Weil 
285e9e427f0SYan, Zheng 	mdsmap_ev = 1;
286e9e427f0SYan, Zheng 	if (mdsmap_v >= 2) {
287e9e427f0SYan, Zheng 		ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
288e9e427f0SYan, Zheng 	}
289e9e427f0SYan, Zheng 	if (mdsmap_ev >= 3) {
290e9e427f0SYan, Zheng 		if (__decode_and_drop_compat_set(p, end) < 0)
291e9e427f0SYan, Zheng 			goto bad_ext;
292e9e427f0SYan, Zheng 	}
293e9e427f0SYan, Zheng 	/* metadata_pool */
294e9e427f0SYan, Zheng 	if (mdsmap_ev < 5) {
295e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u32, bad_ext);
296e9e427f0SYan, Zheng 	} else {
297e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u64, bad_ext);
298e9e427f0SYan, Zheng 	}
299e9e427f0SYan, Zheng 
300e9e427f0SYan, Zheng 	/* created + modified + tableserver */
301e9e427f0SYan, Zheng 	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
302e9e427f0SYan, Zheng 	__decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
303e9e427f0SYan, Zheng 	__decode_and_drop_type(p, end, u32, bad_ext);
304e9e427f0SYan, Zheng 
305e9e427f0SYan, Zheng 	/* in */
306e9e427f0SYan, Zheng 	{
307e9e427f0SYan, Zheng 		int num_laggy = 0;
308e9e427f0SYan, Zheng 		ceph_decode_32_safe(p, end, n, bad_ext);
309e9e427f0SYan, Zheng 		ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);
310e9e427f0SYan, Zheng 
311e9e427f0SYan, Zheng 		for (i = 0; i < n; i++) {
312e9e427f0SYan, Zheng 			s32 mds = ceph_decode_32(p);
313b38c9eb4SXiubo Li 			if (mds >= 0 && mds < m->possible_max_rank) {
314e9e427f0SYan, Zheng 				if (m->m_info[mds].laggy)
315e9e427f0SYan, Zheng 					num_laggy++;
316e9e427f0SYan, Zheng 			}
317e9e427f0SYan, Zheng 		}
318e9e427f0SYan, Zheng 		m->m_num_laggy = num_laggy;
31976201b63SYan, Zheng 
320b38c9eb4SXiubo Li 		if (n > m->possible_max_rank) {
32176201b63SYan, Zheng 			void *new_m_info = krealloc(m->m_info,
32276201b63SYan, Zheng 						    n * sizeof(*m->m_info),
32376201b63SYan, Zheng 						    GFP_NOFS | __GFP_ZERO);
32476201b63SYan, Zheng 			if (!new_m_info)
32576201b63SYan, Zheng 				goto nomem;
32676201b63SYan, Zheng 			m->m_info = new_m_info;
32776201b63SYan, Zheng 		}
328b38c9eb4SXiubo Li 		m->possible_max_rank = n;
329e9e427f0SYan, Zheng 	}
330e9e427f0SYan, Zheng 
331e9e427f0SYan, Zheng 	/* inc */
332e9e427f0SYan, Zheng 	__decode_and_drop_map(p, end, u32, u32, bad_ext);
333e9e427f0SYan, Zheng 	/* up */
334e9e427f0SYan, Zheng 	__decode_and_drop_map(p, end, u32, u64, bad_ext);
335e9e427f0SYan, Zheng 	/* failed */
336e9e427f0SYan, Zheng 	__decode_and_drop_set(p, end, u32, bad_ext);
337e9e427f0SYan, Zheng 	/* stopped */
338e9e427f0SYan, Zheng 	__decode_and_drop_set(p, end, u32, bad_ext);
339e9e427f0SYan, Zheng 
340e9e427f0SYan, Zheng 	if (mdsmap_ev >= 4) {
341e9e427f0SYan, Zheng 		/* last_failure_osd_epoch */
342e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u32, bad_ext);
343e9e427f0SYan, Zheng 	}
344e9e427f0SYan, Zheng 	if (mdsmap_ev >= 6) {
345e9e427f0SYan, Zheng 		/* ever_allowed_snaps */
346e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u8, bad_ext);
347e9e427f0SYan, Zheng 		/* explicitly_allowed_snaps */
348e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u8, bad_ext);
349e9e427f0SYan, Zheng 	}
350e9e427f0SYan, Zheng 	if (mdsmap_ev >= 7) {
351e9e427f0SYan, Zheng 		/* inline_data_enabled */
352e9e427f0SYan, Zheng 		__decode_and_drop_type(p, end, u8, bad_ext);
353e9e427f0SYan, Zheng 	}
354e9e427f0SYan, Zheng 	if (mdsmap_ev >= 8) {
355e9e427f0SYan, Zheng 		/* enabled */
356e9e427f0SYan, Zheng 		ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
357*d93231a6SLuís Henriques 		/* fs_name */
358*d93231a6SLuís Henriques 		ceph_decode_skip_string(p, end, bad_ext);
359e9e427f0SYan, Zheng 	}
360e9e427f0SYan, Zheng 	/* damaged */
361e9e427f0SYan, Zheng 	if (mdsmap_ev >= 9) {
362e9e427f0SYan, Zheng 		size_t need;
363e9e427f0SYan, Zheng 		ceph_decode_32_safe(p, end, n, bad_ext);
364e9e427f0SYan, Zheng 		need = sizeof(u32) * n;
365e9e427f0SYan, Zheng 		ceph_decode_need(p, end, need, bad_ext);
366e9e427f0SYan, Zheng 		*p += need;
367e9e427f0SYan, Zheng 		m->m_damaged = n > 0;
368e9e427f0SYan, Zheng 	} else {
369e9e427f0SYan, Zheng 		m->m_damaged = false;
370e9e427f0SYan, Zheng 	}
371*d93231a6SLuís Henriques 	if (mdsmap_ev >= 17) {
372*d93231a6SLuís Henriques 		/* balancer */
373*d93231a6SLuís Henriques 		ceph_decode_skip_string(p, end, bad_ext);
374*d93231a6SLuís Henriques 		/* standby_count_wanted */
375*d93231a6SLuís Henriques 		ceph_decode_skip_32(p, end, bad_ext);
376*d93231a6SLuís Henriques 		/* old_max_mds */
377*d93231a6SLuís Henriques 		ceph_decode_skip_32(p, end, bad_ext);
378*d93231a6SLuís Henriques 		/* min_compat_client */
379*d93231a6SLuís Henriques 		ceph_decode_skip_8(p, end, bad_ext);
380*d93231a6SLuís Henriques 		/* required_client_features */
381*d93231a6SLuís Henriques 		ceph_decode_skip_set(p, end, 64, bad_ext);
382*d93231a6SLuís Henriques 		ceph_decode_64_safe(p, end, m->m_max_xattr_size, bad_ext);
383*d93231a6SLuís Henriques 	} else {
384*d93231a6SLuís Henriques 		/* This forces the usage of the (sync) SETXATTR Op */
385*d93231a6SLuís Henriques 		m->m_max_xattr_size = 0;
386*d93231a6SLuís Henriques 	}
387e9e427f0SYan, Zheng bad_ext:
388da08e1e1SXiubo Li 	dout("mdsmap_decode m_enabled: %d, m_damaged: %d, m_num_laggy: %d\n",
389da08e1e1SXiubo Li 	     !!m->m_enabled, !!m->m_damaged, m->m_num_laggy);
390d463a43dSYan, Zheng 	*p = end;
3912f2dc053SSage Weil 	dout("mdsmap_decode success epoch %u\n", m->m_epoch);
3922f2dc053SSage Weil 	return m;
393e9e427f0SYan, Zheng nomem:
3942f2dc053SSage Weil 	err = -ENOMEM;
395e9e427f0SYan, Zheng 	goto out_err;
396f3848af1SJeff Layton corrupt:
3972f2dc053SSage Weil 	pr_err("corrupt mdsmap\n");
3989ec7cab1SSage Weil 	print_hex_dump(KERN_DEBUG, "mdsmap: ",
3999ec7cab1SSage Weil 		       DUMP_PREFIX_OFFSET, 16, 1,
4009ec7cab1SSage Weil 		       start, end - start, true);
401e9e427f0SYan, Zheng out_err:
4022f2dc053SSage Weil 	ceph_mdsmap_destroy(m);
403c213b50bSEmil Goode 	return ERR_PTR(err);
404f3848af1SJeff Layton bad:
405f3848af1SJeff Layton 	err = -EINVAL;
406f3848af1SJeff Layton 	goto corrupt;
4072f2dc053SSage Weil }
4082f2dc053SSage Weil 
4092f2dc053SSage Weil void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
4102f2dc053SSage Weil {
4112f2dc053SSage Weil 	int i;
4122f2dc053SSage Weil 
413a9e6ffbcSTuo Li 	if (m->m_info) {
414b38c9eb4SXiubo Li 		for (i = 0; i < m->possible_max_rank; i++)
4152f2dc053SSage Weil 			kfree(m->m_info[i].export_targets);
4162f2dc053SSage Weil 		kfree(m->m_info);
417a9e6ffbcSTuo Li 	}
4182f2dc053SSage Weil 	kfree(m->m_data_pg_pools);
4192f2dc053SSage Weil 	kfree(m);
4202f2dc053SSage Weil }
421e9e427f0SYan, Zheng 
422e9e427f0SYan, Zheng bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
423e9e427f0SYan, Zheng {
424e9e427f0SYan, Zheng 	int i, nr_active = 0;
425e9e427f0SYan, Zheng 	if (!m->m_enabled)
426e9e427f0SYan, Zheng 		return false;
427e9e427f0SYan, Zheng 	if (m->m_damaged)
428e9e427f0SYan, Zheng 		return false;
4294d7ace02SXiubo Li 	if (m->m_num_laggy == m->m_num_active_mds)
430e9e427f0SYan, Zheng 		return false;
431b38c9eb4SXiubo Li 	for (i = 0; i < m->possible_max_rank; i++) {
432e9e427f0SYan, Zheng 		if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
433e9e427f0SYan, Zheng 			nr_active++;
434e9e427f0SYan, Zheng 	}
435e9e427f0SYan, Zheng 	return nr_active > 0;
436e9e427f0SYan, Zheng }
437