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