mdsmap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. u16 version;
  48. m = kzalloc(sizeof(*m), GFP_NOFS);
  49. if (m == NULL)
  50. return ERR_PTR(-ENOMEM);
  51. ceph_decode_16_safe(p, end, version, bad);
  52. if (version > 3) {
  53. pr_warn("got mdsmap version %d > 3, failing", version);
  54. goto bad;
  55. }
  56. ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
  57. m->m_epoch = ceph_decode_32(p);
  58. m->m_client_epoch = ceph_decode_32(p);
  59. m->m_last_failure = ceph_decode_32(p);
  60. m->m_root = ceph_decode_32(p);
  61. m->m_session_timeout = ceph_decode_32(p);
  62. m->m_session_autoclose = ceph_decode_32(p);
  63. m->m_max_file_size = ceph_decode_64(p);
  64. m->m_max_mds = ceph_decode_32(p);
  65. m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
  66. if (m->m_info == NULL)
  67. goto badmem;
  68. /* pick out active nodes from mds_info (state > 0) */
  69. n = ceph_decode_32(p);
  70. for (i = 0; i < n; i++) {
  71. u64 global_id;
  72. u32 namelen;
  73. s32 mds, inc, state;
  74. u64 state_seq;
  75. u8 infoversion;
  76. struct ceph_entity_addr addr;
  77. u32 num_export_targets;
  78. void *pexport_targets = NULL;
  79. struct ceph_timespec laggy_since;
  80. struct ceph_mds_info *info;
  81. ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad);
  82. global_id = ceph_decode_64(p);
  83. infoversion = ceph_decode_8(p);
  84. *p += sizeof(u64);
  85. namelen = ceph_decode_32(p); /* skip mds name */
  86. *p += namelen;
  87. ceph_decode_need(p, end,
  88. 4*sizeof(u32) + sizeof(u64) +
  89. sizeof(addr) + sizeof(struct ceph_timespec),
  90. bad);
  91. mds = ceph_decode_32(p);
  92. inc = ceph_decode_32(p);
  93. state = ceph_decode_32(p);
  94. state_seq = ceph_decode_64(p);
  95. ceph_decode_copy(p, &addr, sizeof(addr));
  96. ceph_decode_addr(&addr);
  97. ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
  98. *p += sizeof(u32);
  99. ceph_decode_32_safe(p, end, namelen, bad);
  100. *p += namelen;
  101. if (infoversion >= 2) {
  102. ceph_decode_32_safe(p, end, num_export_targets, bad);
  103. pexport_targets = *p;
  104. *p += num_export_targets * sizeof(u32);
  105. } else {
  106. num_export_targets = 0;
  107. }
  108. dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
  109. i+1, n, global_id, mds, inc,
  110. ceph_pr_addr(&addr.in_addr),
  111. ceph_mds_state_name(state));
  112. if (mds < 0 || mds >= m->m_max_mds || state <= 0)
  113. continue;
  114. info = &m->m_info[mds];
  115. info->global_id = global_id;
  116. info->state = state;
  117. info->addr = addr;
  118. info->laggy = (laggy_since.tv_sec != 0 ||
  119. laggy_since.tv_nsec != 0);
  120. info->num_export_targets = num_export_targets;
  121. if (num_export_targets) {
  122. info->export_targets = kcalloc(num_export_targets,
  123. sizeof(u32), GFP_NOFS);
  124. if (info->export_targets == NULL)
  125. goto badmem;
  126. for (j = 0; j < num_export_targets; j++)
  127. info->export_targets[j] =
  128. ceph_decode_32(&pexport_targets);
  129. } else {
  130. info->export_targets = NULL;
  131. }
  132. }
  133. /* pg_pools */
  134. ceph_decode_32_safe(p, end, n, bad);
  135. m->m_num_data_pg_pools = n;
  136. m->m_data_pg_pools = kcalloc(n, sizeof(u64), GFP_NOFS);
  137. if (!m->m_data_pg_pools)
  138. goto badmem;
  139. ceph_decode_need(p, end, sizeof(u64)*(n+1), bad);
  140. for (i = 0; i < n; i++)
  141. m->m_data_pg_pools[i] = ceph_decode_64(p);
  142. m->m_cas_pg_pool = ceph_decode_64(p);
  143. /* ok, we don't care about the rest. */
  144. dout("mdsmap_decode success epoch %u\n", m->m_epoch);
  145. return m;
  146. badmem:
  147. err = -ENOMEM;
  148. bad:
  149. pr_err("corrupt mdsmap\n");
  150. print_hex_dump(KERN_DEBUG, "mdsmap: ",
  151. DUMP_PREFIX_OFFSET, 16, 1,
  152. start, end - start, true);
  153. ceph_mdsmap_destroy(m);
  154. return ERR_PTR(err);
  155. }
  156. void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
  157. {
  158. int i;
  159. for (i = 0; i < m->m_max_mds; i++)
  160. kfree(m->m_info[i].export_targets);
  161. kfree(m->m_info);
  162. kfree(m->m_data_pg_pools);
  163. kfree(m);
  164. }