mdsmap.c 9.2 KB

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