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 = get_random_u32_below(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(struct ceph_mds_client *mdsc, void **p, 118 void *end, bool msgr2) 119 { 120 struct ceph_mdsmap *m; 121 const void *start = *p; 122 int i, j, n; 123 int err; 124 u8 mdsmap_v; 125 u16 mdsmap_ev; 126 u32 target; 127 128 m = kzalloc(sizeof(*m), GFP_NOFS); 129 if (!m) 130 return ERR_PTR(-ENOMEM); 131 132 ceph_decode_need(p, end, 1 + 1, bad); 133 mdsmap_v = ceph_decode_8(p); 134 *p += sizeof(u8); /* mdsmap_cv */ 135 if (mdsmap_v >= 4) { 136 u32 mdsmap_len; 137 ceph_decode_32_safe(p, end, mdsmap_len, bad); 138 if (end < *p + mdsmap_len) 139 goto bad; 140 end = *p + mdsmap_len; 141 } 142 143 ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); 144 m->m_epoch = ceph_decode_32(p); 145 m->m_client_epoch = ceph_decode_32(p); 146 m->m_last_failure = ceph_decode_32(p); 147 m->m_root = ceph_decode_32(p); 148 m->m_session_timeout = ceph_decode_32(p); 149 m->m_session_autoclose = ceph_decode_32(p); 150 m->m_max_file_size = ceph_decode_64(p); 151 m->m_max_mds = ceph_decode_32(p); 152 153 /* 154 * pick out the active nodes as the m_num_active_mds, the 155 * m_num_active_mds maybe larger than m_max_mds when decreasing 156 * the max_mds in cluster side, in other case it should less 157 * than or equal to m_max_mds. 158 */ 159 m->m_num_active_mds = n = ceph_decode_32(p); 160 161 /* 162 * the possible max rank, it maybe larger than the m_num_active_mds, 163 * for example if the mds_max == 2 in the cluster, when the MDS(0) 164 * was laggy and being replaced by a new MDS, we will temporarily 165 * receive a new mds map with n_num_mds == 1 and the active MDS(1), 166 * and the mds rank >= m_num_active_mds. 167 */ 168 m->possible_max_rank = max(m->m_num_active_mds, m->m_max_mds); 169 170 m->m_info = kcalloc(m->possible_max_rank, sizeof(*m->m_info), GFP_NOFS); 171 if (!m->m_info) 172 goto nomem; 173 174 /* pick out active nodes from mds_info (state > 0) */ 175 for (i = 0; i < n; i++) { 176 u64 global_id; 177 u32 namelen; 178 s32 mds, inc, state; 179 u8 info_v; 180 void *info_end = NULL; 181 struct ceph_entity_addr addr; 182 u32 num_export_targets; 183 void *pexport_targets = NULL; 184 struct ceph_timespec laggy_since; 185 struct ceph_mds_info *info; 186 bool laggy; 187 188 ceph_decode_need(p, end, sizeof(u64) + 1, bad); 189 global_id = ceph_decode_64(p); 190 info_v= ceph_decode_8(p); 191 if (info_v >= 4) { 192 u32 info_len; 193 ceph_decode_need(p, end, 1 + sizeof(u32), bad); 194 *p += sizeof(u8); /* info_cv */ 195 info_len = ceph_decode_32(p); 196 info_end = *p + info_len; 197 if (info_end > end) 198 goto bad; 199 } 200 201 ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad); 202 *p += sizeof(u64); 203 namelen = ceph_decode_32(p); /* skip mds name */ 204 *p += namelen; 205 206 ceph_decode_32_safe(p, end, mds, bad); 207 ceph_decode_32_safe(p, end, inc, bad); 208 ceph_decode_32_safe(p, end, state, bad); 209 *p += sizeof(u64); /* state_seq */ 210 if (info_v >= 8) 211 err = ceph_decode_entity_addrvec(p, end, msgr2, &addr); 212 else 213 err = ceph_decode_entity_addr(p, end, &addr); 214 if (err) 215 goto corrupt; 216 217 ceph_decode_copy_safe(p, end, &laggy_since, sizeof(laggy_since), 218 bad); 219 laggy = laggy_since.tv_sec != 0 || laggy_since.tv_nsec != 0; 220 *p += sizeof(u32); 221 ceph_decode_32_safe(p, end, namelen, bad); 222 *p += namelen; 223 if (info_v >= 2) { 224 ceph_decode_32_safe(p, end, num_export_targets, bad); 225 pexport_targets = *p; 226 *p += num_export_targets * sizeof(u32); 227 } else { 228 num_export_targets = 0; 229 } 230 231 if (info_end && *p != info_end) { 232 if (*p > info_end) 233 goto bad; 234 *p = info_end; 235 } 236 237 dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s%s\n", 238 i+1, n, global_id, mds, inc, 239 ceph_pr_addr(&addr), 240 ceph_mds_state_name(state), 241 laggy ? "(laggy)" : ""); 242 243 if (mds < 0 || mds >= m->possible_max_rank) { 244 pr_warn("mdsmap_decode got incorrect mds(%d)\n", mds); 245 continue; 246 } 247 248 if (state <= 0) { 249 dout("mdsmap_decode got incorrect state(%s)\n", 250 ceph_mds_state_name(state)); 251 continue; 252 } 253 254 info = &m->m_info[mds]; 255 info->global_id = global_id; 256 info->state = state; 257 info->addr = addr; 258 info->laggy = laggy; 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) 264 goto nomem; 265 for (j = 0; j < num_export_targets; j++) { 266 target = ceph_decode_32(&pexport_targets); 267 info->export_targets[j] = target; 268 } 269 } else { 270 info->export_targets = NULL; 271 } 272 } 273 274 /* pg_pools */ 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) 279 goto nomem; 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; 285 286 mdsmap_ev = 1; 287 if (mdsmap_v >= 2) { 288 ceph_decode_16_safe(p, end, mdsmap_ev, bad_ext); 289 } 290 if (mdsmap_ev >= 3) { 291 if (__decode_and_drop_compat_set(p, end) < 0) 292 goto bad_ext; 293 } 294 /* metadata_pool */ 295 if (mdsmap_ev < 5) { 296 __decode_and_drop_type(p, end, u32, bad_ext); 297 } else { 298 __decode_and_drop_type(p, end, u64, bad_ext); 299 } 300 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); 305 306 /* in */ 307 { 308 int num_laggy = 0; 309 ceph_decode_32_safe(p, end, n, bad_ext); 310 ceph_decode_need(p, end, sizeof(u32) * n, bad_ext); 311 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) 316 num_laggy++; 317 } 318 } 319 m->m_num_laggy = num_laggy; 320 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); 325 if (!new_m_info) 326 goto nomem; 327 m->m_info = new_m_info; 328 } 329 m->possible_max_rank = n; 330 } 331 332 /* inc */ 333 __decode_and_drop_map(p, end, u32, u32, bad_ext); 334 /* up */ 335 __decode_and_drop_map(p, end, u32, u64, bad_ext); 336 /* failed */ 337 __decode_and_drop_set(p, end, u32, bad_ext); 338 /* stopped */ 339 __decode_and_drop_set(p, end, u32, bad_ext); 340 341 if (mdsmap_ev >= 4) { 342 /* last_failure_osd_epoch */ 343 __decode_and_drop_type(p, end, u32, bad_ext); 344 } 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); 350 } 351 if (mdsmap_ev >= 7) { 352 /* inline_data_enabled */ 353 __decode_and_drop_type(p, end, u8, bad_ext); 354 } 355 if (mdsmap_ev >= 8) { 356 /* enabled */ 357 ceph_decode_8_safe(p, end, m->m_enabled, bad_ext); 358 /* fs_name */ 359 ceph_decode_skip_string(p, end, bad_ext); 360 } 361 /* damaged */ 362 if (mdsmap_ev >= 9) { 363 size_t need; 364 ceph_decode_32_safe(p, end, n, bad_ext); 365 need = sizeof(u32) * n; 366 ceph_decode_need(p, end, need, bad_ext); 367 *p += need; 368 m->m_damaged = n > 0; 369 } else { 370 m->m_damaged = false; 371 } 372 if (mdsmap_ev >= 17) { 373 /* balancer */ 374 ceph_decode_skip_string(p, end, bad_ext); 375 /* standby_count_wanted */ 376 ceph_decode_skip_32(p, end, bad_ext); 377 /* old_max_mds */ 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); 383 /* bal_rank_mask */ 384 ceph_decode_skip_string(p, end, bad_ext); 385 } 386 if (mdsmap_ev >= 18) { 387 ceph_decode_64_safe(p, end, m->m_max_xattr_size, bad_ext); 388 } 389 bad_ext: 390 dout("mdsmap_decode m_enabled: %d, m_damaged: %d, m_num_laggy: %d\n", 391 !!m->m_enabled, !!m->m_damaged, m->m_num_laggy); 392 *p = end; 393 dout("mdsmap_decode success epoch %u\n", m->m_epoch); 394 return m; 395 nomem: 396 err = -ENOMEM; 397 goto out_err; 398 corrupt: 399 pr_err("corrupt mdsmap\n"); 400 print_hex_dump(KERN_DEBUG, "mdsmap: ", 401 DUMP_PREFIX_OFFSET, 16, 1, 402 start, end - start, true); 403 out_err: 404 ceph_mdsmap_destroy(m); 405 return ERR_PTR(err); 406 bad: 407 err = -EINVAL; 408 goto corrupt; 409 } 410 411 void ceph_mdsmap_destroy(struct ceph_mdsmap *m) 412 { 413 int i; 414 415 if (m->m_info) { 416 for (i = 0; i < m->possible_max_rank; i++) 417 kfree(m->m_info[i].export_targets); 418 kfree(m->m_info); 419 } 420 kfree(m->m_data_pg_pools); 421 kfree(m); 422 } 423 424 bool ceph_mdsmap_is_cluster_available(struct ceph_mdsmap *m) 425 { 426 int i, nr_active = 0; 427 if (!m->m_enabled) 428 return false; 429 if (m->m_damaged) 430 return false; 431 if (m->m_num_laggy == m->m_num_active_mds) 432 return false; 433 for (i = 0; i < m->possible_max_rank; i++) { 434 if (m->m_info[i].state == CEPH_MDS_STATE_ACTIVE) 435 nr_active++; 436 } 437 return nr_active > 0; 438 } 439