mdsmap.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/bug.h>
  3. #include <linux/err.h>
  4. #include <linux/random.h>
  5. #include <linux/slab.h>
  6. #include <linux/types.h>
  7. #include <linux/ceph/mdsmap.h>
  8. #include <linux/ceph/messenger.h>
  9. #include <linux/ceph/decode.h>
  10. #include "super.h"
  11. /*
  12. * choose a random mds that is "up" (i.e. has a state > 0), or -1.
  13. */
  14. int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m)
  15. {
  16. int n = 0;
  17. int i;
  18. /* special case for one mds */
  19. if (1 == m->m_max_mds && m->m_info[0].state > 0)
  20. return 0;
  21. /* count */
  22. for (i = 0; i < m->m_max_mds; i++)
  23. if (m->m_info[i].state > 0)
  24. n++;
  25. if (n == 0)
  26. return -1;
  27. /* pick */
  28. n = prandom_u32() % n;
  29. i = 0;
  30. for (i = 0; n > 0; i++, n--)
  31. while (m->m_info[i].state <= 0)
  32. i++;
  33. return i;
  34. }
  35. /*
  36. * Decode an MDS map
  37. *
  38. * Ignore any fields we don't care about (there are quite a few of
  39. * them).
  40. */
  41. struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
  42. {
  43. struct ceph_mdsmap *m;
  44. const void *start = *p;
  45. int i, j, n;
  46. int err = -EINVAL;
  47. u8 mdsmap_v, mdsmap_cv;
  48. m = kzalloc(sizeof(*m), GFP_NOFS);
  49. if (m == NULL)
  50. return ERR_PTR(-ENOMEM);
  51. ceph_decode_need(p, end, 1 + 1, bad);
  52. mdsmap_v = ceph_decode_8(p);
  53. mdsmap_cv = ceph_decode_8(p);
  54. if (mdsmap_v >= 4) {
  55. u32 mdsmap_len;
  56. ceph_decode_32_safe(p, end, mdsmap_len, bad);
  57. if (end < *p + mdsmap_len)
  58. goto bad;
  59. end = *p + mdsmap_len;
  60. }
  61. ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
  62. m->m_epoch = ceph_decode_32(p);
  63. m->m_client_epoch = ceph_decode_32(p);
  64. m->m_last_failure = ceph_decode_32(p);
  65. m->m_root = ceph_decode_32(p);
  66. m->m_session_timeout = ceph_decode_32(p);
  67. m->m_session_autoclose = ceph_decode_32(p);
  68. m->m_max_file_size = ceph_decode_64(p);
  69. m->m_max_mds = ceph_decode_32(p);
  70. m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
  71. if (m->m_info == NULL)
  72. goto badmem;
  73. /* pick out active nodes from mds_info (state > 0) */
  74. n = ceph_decode_32(p);
  75. for (i = 0; i < n; i++) {
  76. u64 global_id;
  77. u32 namelen;
  78. s32 mds, inc, state;
  79. u64 state_seq;
  80. u8 info_v;
  81. void *info_end = NULL;
  82. struct ceph_entity_addr addr;
  83. u32 num_export_targets;
  84. void *pexport_targets = NULL;
  85. struct ceph_timespec laggy_since;
  86. struct ceph_mds_info *info;
  87. ceph_decode_need(p, end, sizeof(u64) + 1, bad);
  88. global_id = ceph_decode_64(p);
  89. info_v= ceph_decode_8(p);
  90. if (info_v >= 4) {
  91. u32 info_len;
  92. u8 info_cv;
  93. ceph_decode_need(p, end, 1 + sizeof(u32), bad);
  94. info_cv = ceph_decode_8(p);
  95. info_len = ceph_decode_32(p);
  96. info_end = *p + info_len;
  97. if (info_end > end)
  98. goto bad;
  99. }
  100. ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad);
  101. *p += sizeof(u64);
  102. namelen = ceph_decode_32(p); /* skip mds name */
  103. *p += namelen;
  104. ceph_decode_need(p, end,
  105. 4*sizeof(u32) + sizeof(u64) +
  106. sizeof(addr) + sizeof(struct ceph_timespec),
  107. bad);
  108. mds = ceph_decode_32(p);
  109. inc = ceph_decode_32(p);
  110. state = ceph_decode_32(p);
  111. state_seq = ceph_decode_64(p);
  112. ceph_decode_copy(p, &addr, sizeof(addr));
  113. ceph_decode_addr(&addr);
  114. ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
  115. *p += sizeof(u32);
  116. ceph_decode_32_safe(p, end, namelen, bad);
  117. *p += namelen;
  118. if (info_v >= 2) {
  119. ceph_decode_32_safe(p, end, num_export_targets, bad);
  120. pexport_targets = *p;
  121. *p += num_export_targets * sizeof(u32);
  122. } else {
  123. num_export_targets = 0;
  124. }
  125. if (info_end && *p != info_end) {
  126. if (*p > info_end)
  127. goto bad;
  128. *p = info_end;
  129. }
  130. dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
  131. i+1, n, global_id, mds, inc,
  132. ceph_pr_addr(&addr.in_addr),
  133. ceph_mds_state_name(state));
  134. if (mds < 0 || mds >= m->m_max_mds || state <= 0)
  135. continue;
  136. info = &m->m_info[mds];
  137. info->global_id = global_id;
  138. info->state = state;
  139. info->addr = addr;
  140. info->laggy = (laggy_since.tv_sec != 0 ||
  141. laggy_since.tv_nsec != 0);
  142. info->num_export_targets = num_export_targets;
  143. if (num_export_targets) {
  144. info->export_targets = kcalloc(num_export_targets,
  145. sizeof(u32), GFP_NOFS);
  146. if (info->export_targets == NULL)
  147. goto badmem;
  148. for (j = 0; j < num_export_targets; j++)
  149. info->export_targets[j] =
  150. ceph_decode_32(&pexport_targets);
  151. } else {
  152. info->export_targets = NULL;
  153. }
  154. }
  155. /* pg_pools */
  156. ceph_decode_32_safe(p, end, n, bad);
  157. m->m_num_data_pg_pools = n;
  158. m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
  159. if (!m->m_data_pg_pools)
  160. goto badmem;
  161. ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
  162. for (i = 0; i < n; i++)
  163. m->m_data_pg_pools[i] = ceph_decode_64(p);
  164. m->m_cas_pg_pool = ceph_decode_64(p);
  165. /* ok, we don't care about the rest. */
  166. *p = end;
  167. dout("mdsmap_decode success epoch %u\n", m->m_epoch);
  168. return m;
  169. badmem:
  170. err = -ENOMEM;
  171. bad:
  172. pr_err("corrupt mdsmap\n");
  173. print_hex_dump(KERN_DEBUG, "mdsmap: ",
  174. DUMP_PREFIX_OFFSET, 16, 1,
  175. start, end - start, true);
  176. ceph_mdsmap_destroy(m);
  177. return ERR_PTR(err);
  178. }
  179. void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
  180. {
  181. int i;
  182. for (i = 0; i < m->m_max_mds; i++)
  183. kfree(m->m_info[i].export_targets);
  184. kfree(m->m_info);
  185. kfree(m->m_data_pg_pools);
  186. kfree(m);
  187. }