1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
6 #include <linux/random.h>
7 #include <linux/slab.h>
8 #include <linux/types.h>
10 #include <linux/ceph/messenger.h>
11 #include <linux/ceph/decode.h>
14 #include "mds_client.h"
17 #define CEPH_MDS_IS_READY(i, ignore_laggy) \
18 (m->m_info[i].state > 0 && ignore_laggy ? true : !m->m_info[i].laggy)
20 static int __mdsmap_get_random_mds(struct ceph_mdsmap *m, bool ignore_laggy)
26 for (i = 0; i < m->possible_max_rank; i++)
27 if (CEPH_MDS_IS_READY(i, ignore_laggy))
33 n = get_random_u32_below(n);
34 for (j = 0, i = 0; i < m->possible_max_rank; i++) {
35 if (CEPH_MDS_IS_READY(i, ignore_laggy))
45 * choose a random mds that is "up" (i.e. has a state > 0), or -1.
47 int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
51 mds = __mdsmap_get_random_mds(m, false);
52 if (mds == m->possible_max_rank || mds == -1)
53 mds = __mdsmap_get_random_mds(m, true);
55 return mds == m->possible_max_rank ? -1 : mds;
58 #define __decode_and_drop_type(p, end, type, bad) \
60 if (*p + sizeof(type) > end) \
65 #define __decode_and_drop_set(p, end, type, bad) \
69 ceph_decode_32_safe(p, end, n, bad); \
70 need = sizeof(type) * n; \
71 ceph_decode_need(p, end, need, bad); \
75 #define __decode_and_drop_map(p, end, ktype, vtype, bad) \
79 ceph_decode_32_safe(p, end, n, bad); \
80 need = (sizeof(ktype) + sizeof(vtype)) * n; \
81 ceph_decode_need(p, end, need, bad); \
86 static int __decode_and_drop_compat_set(void **p, void* end)
89 /* compat, ro_compat, incompat*/
90 for (i = 0; i < 3; i++) {
92 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
95 /* names (map<u64, string>) */
96 n = ceph_decode_32(p);
99 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32),
102 len = ceph_decode_32(p);
103 ceph_decode_need(p, end, len, bad);
115 * Ignore any fields we don't care about (there are quite a few of
118 struct ceph_mdsmap *ceph_mdsmap_decode(struct ceph_mds_client *mdsc, void **p,
119 void *end, bool msgr2)
121 struct ceph_client *cl = mdsc->fsc->client;
122 struct ceph_mdsmap *m;
123 const void *start = *p;
130 m = kzalloc(sizeof(*m), GFP_NOFS);
132 return ERR_PTR(-ENOMEM);
134 ceph_decode_need(p, end, 1 + 1, bad);
135 mdsmap_v = ceph_decode_8(p);
136 *p += sizeof(u8); /* mdsmap_cv */
139 ceph_decode_32_safe(p, end, mdsmap_len, bad);
140 if (end < *p + mdsmap_len)
142 end = *p + mdsmap_len;
145 ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
146 m->m_epoch = ceph_decode_32(p);
147 m->m_client_epoch = ceph_decode_32(p);
148 m->m_last_failure = ceph_decode_32(p);
149 m->m_root = ceph_decode_32(p);
150 m->m_session_timeout = ceph_decode_32(p);
151 m->m_session_autoclose = ceph_decode_32(p);
152 m->m_max_file_size = ceph_decode_64(p);
153 m->m_max_mds = ceph_decode_32(p);
156 * pick out the active nodes as the m_num_active_mds, the
157 * m_num_active_mds maybe larger than m_max_mds when decreasing
158 * the max_mds in cluster side, in other case it should less
159 * than or equal to m_max_mds.
161 m->m_num_active_mds = n = ceph_decode_32(p);
164 * the possible max rank, it maybe larger than the m_num_active_mds,
165 * for example if the mds_max == 2 in the cluster, when the MDS(0)
166 * was laggy and being replaced by a new MDS, we will temporarily
167 * receive a new mds map with n_num_mds == 1 and the active MDS(1),
168 * and the mds rank >= m_num_active_mds.
170 m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds);
172 m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS);
176 /* pick out active nodes from mds_info (state > 0) */
177 for (i = 0; i < n; i++) {
182 void *info_end = NULL;
183 struct ceph_entity_addr addr;
184 u32 num_export_targets;
185 void *pexport_targets = NULL;
186 struct ceph_timespec laggy_since;
187 struct ceph_mds_info *info;
190 ceph_decode_need(p, end, sizeof(u64) + 1, bad);
191 global_id = ceph_decode_64(p);
192 info_v= ceph_decode_8(p);
195 ceph_decode_need(p, end, 1 + sizeof(u32), bad);
196 *p += sizeof(u8); /* info_cv */
197 info_len = ceph_decode_32(p);
198 info_end = *p + info_len;
203 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
205 namelen = ceph_decode_32(p); /* skip mds name */
208 ceph_decode_32_safe(p, end, mds, bad);
209 ceph_decode_32_safe(p, end, inc, bad);
210 ceph_decode_32_safe(p, end, state, bad);
211 *p += sizeof(u64); /* state_seq */
213 err = ceph_decode_entity_addrvec(p, end, msgr2, &addr);
215 err = ceph_decode_entity_addr(p, end, &addr);
219 ceph_decode_copy_safe(p, end, &laggy_since, sizeof(laggy_since),
221 laggy = laggy_since.tv_sec != 0 || laggy_since.tv_nsec != 0;
223 ceph_decode_32_safe(p, end, namelen, bad);
226 ceph_decode_32_safe(p, end, num_export_targets, bad);
227 pexport_targets = *p;
228 *p += num_export_targets * sizeof(u32);
230 num_export_targets = 0;
233 if (info_end && *p != info_end) {
239 doutc(cl, "%d/%d %lld mds%d.%d %s %s%s\n", i+1, n, global_id,
240 mds, inc, ceph_pr_addr(&addr),
241 ceph_mds_state_name(state), laggy ? "(laggy)" : "");
243 if (mds < 0 || mds >= m->possible_max_rank) {
244 pr_warn_client(cl, "got incorrect mds(%d)\n", mds);
249 doutc(cl, "got incorrect state(%s)\n",
250 ceph_mds_state_name(state));
254 info = &m->m_info[mds];
255 info->global_id = global_id;
259 info->num_export_targets = num_export_targets;
260 if (num_export_targets) {
261 info->export_targets = kcalloc(num_export_targets,
262 sizeof(u32), GFP_NOFS);
263 if (!info->export_targets)
265 for (j = 0; j < num_export_targets; j++) {
266 target = ceph_decode_32(&pexport_targets);
267 info->export_targets[j] = target;
270 info->export_targets = NULL;
275 ceph_decode_32_safe(p, end, n, bad);
276 m->m_num_data_pg_pools = n;
277 m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
278 if (!m->m_data_pg_pools)
280 ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
281 for (i = 0; i < n; i++)
282 m->m_data_pg_pools[i] = ceph_decode_64(p);
283 m->m_cas_pg_pool = ceph_decode_64(p);
284 m->m_enabled = m->m_epoch > 1;
288 ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext);
290 if (mdsmap_ev >= 3) {
291 if (__decode_and_drop_compat_set(p, end) < 0)
296 __decode_and_drop_type(p, end, u32, bad_ext);
298 __decode_and_drop_type(p, end, u64, bad_ext);
301 /* created + modified + tableserver */
302 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
303 __decode_and_drop_type(p, end, struct ceph_timespec, bad_ext);
304 __decode_and_drop_type(p, end, u32, bad_ext);
309 ceph_decode_32_safe(p, end, n, bad_ext);
310 ceph_decode_need(p, end, sizeof(u32) * n, bad_ext);
312 for (i = 0; i < n; i++) {
313 s32 mds = ceph_decode_32(p);
314 if (mds >= 0 && mds < m->possible_max_rank) {
315 if (m->m_info[mds].laggy)
319 m->m_num_laggy = num_laggy;
321 if (n > m->possible_max_rank) {
322 void *new_m_info = krealloc(m->m_info,
323 n * sizeof(*m->m_info),
324 GFP_NOFS | __GFP_ZERO);
327 m->m_info = new_m_info;
329 m->possible_max_rank = n;
333 __decode_and_drop_map(p, end, u32, u32, bad_ext);
335 __decode_and_drop_map(p, end, u32, u64, bad_ext);
337 __decode_and_drop_set(p, end, u32, bad_ext);
339 __decode_and_drop_set(p, end, u32, bad_ext);
341 if (mdsmap_ev >= 4) {
342 /* last_failure_osd_epoch */
343 __decode_and_drop_type(p, end, u32, bad_ext);
345 if (mdsmap_ev >= 6) {
346 /* ever_allowed_snaps */
347 __decode_and_drop_type(p, end, u8, bad_ext);
348 /* explicitly_allowed_snaps */
349 __decode_and_drop_type(p, end, u8, bad_ext);
351 if (mdsmap_ev >= 7) {
352 /* inline_data_enabled */
353 __decode_and_drop_type(p, end, u8, bad_ext);
355 if (mdsmap_ev >= 8) {
357 ceph_decode_8_safe(p, end, m->m_enabled, bad_ext);
359 ceph_decode_skip_string(p, end, bad_ext);
362 if (mdsmap_ev >= 9) {
364 ceph_decode_32_safe(p, end, n, bad_ext);
365 need = sizeof(u32) * n;
366 ceph_decode_need(p, end, need, bad_ext);
368 m->m_damaged = n > 0;
370 m->m_damaged = false;
372 if (mdsmap_ev >= 17) {
374 ceph_decode_skip_string(p, end, bad_ext);
375 /* standby_count_wanted */
376 ceph_decode_skip_32(p, end, bad_ext);
378 ceph_decode_skip_32(p, end, bad_ext);
379 /* min_compat_client */
380 ceph_decode_skip_8(p, end, bad_ext);
381 /* required_client_features */
382 ceph_decode_skip_set(p, end, 64, bad_ext);
384 ceph_decode_skip_string(p, end, bad_ext);
386 if (mdsmap_ev >= 18) {
387 ceph_decode_64_safe(p, end, m->m_max_xattr_size, bad_ext);
390 doutc(cl, "m_enabled: %d, m_damaged: %d, m_num_laggy: %d\n",
391 !!m->m_enabled, !!m->m_damaged, m->m_num_laggy);
393 doutc(cl, "success epoch %u\n", m->m_epoch);
399 pr_err_client(cl, "corrupt mdsmap\n");
400 print_hex_dump(KERN_DEBUG, "mdsmap: ",
401 DUMP_PREFIX_OFFSET, 16, 1,
402 start, end - start, true);
404 ceph_mdsmap_destroy(m);
411 void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
416 for (i = 0; i < m->possible_max_rank; i++)
417 kfree(m->m_info[i].export_targets);
420 kfree(m->m_data_pg_pools);
424 bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m)
426 int i, nr_active = 0;
431 if (m->m_num_laggy == m->m_num_active_mds)
433 for (i = 0; i < m->possible_max_rank; i++) {
434 if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE)
437 return nr_active > 0;