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