mdsmap.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. char r;
  19. /* count */
  20. for (i = 0; i < m->m_max_mds; i++)
  21. if (m->m_info[i].state > 0)
  22. n++;
  23. if (n == 0)
  24. return -1;
  25. /* pick */
  26. get_random_bytes(&r, 1);
  27. n = r % n;
  28. i = 0;
  29. for (i = 0; n > 0; i++, n--)
  30. while (m->m_info[i].state <= 0)
  31. i++;
  32. return i;
  33. }
  34. /*
  35. * Decode an MDS map
  36. *
  37. * Ignore any fields we don't care about (there are quite a few of
  38. * them).
  39. */
  40. struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end)
  41. {
  42. struct ceph_mdsmap *m;
  43. const void *start = *p;
  44. int i, j, n;
  45. int err = -EINVAL;
  46. u16 version;
  47. m = kzalloc(sizeof(*m), GFP_NOFS);
  48. if (m == NULL)
  49. return ERR_PTR(-ENOMEM);
  50. ceph_decode_16_safe(p, end, version, bad);
  51. ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad);
  52. m->m_epoch = ceph_decode_32(p);
  53. m->m_client_epoch = ceph_decode_32(p);
  54. m->m_last_failure = ceph_decode_32(p);
  55. m->m_root = ceph_decode_32(p);
  56. m->m_session_timeout = ceph_decode_32(p);
  57. m->m_session_autoclose = ceph_decode_32(p);
  58. m->m_max_file_size = ceph_decode_64(p);
  59. m->m_max_mds = ceph_decode_32(p);
  60. m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS);
  61. if (m->m_info == NULL)
  62. goto badmem;
  63. /* pick out active nodes from mds_info (state > 0) */
  64. n = ceph_decode_32(p);
  65. for (i = 0; i < n; i++) {
  66. u64 global_id;
  67. u32 namelen;
  68. s32 mds, inc, state;
  69. u64 state_seq;
  70. u8 infoversion;
  71. struct ceph_entity_addr addr;
  72. u32 num_export_targets;
  73. void *pexport_targets = NULL;
  74. struct ceph_timespec laggy_since;
  75. ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad);
  76. global_id = ceph_decode_64(p);
  77. infoversion = ceph_decode_8(p);
  78. *p += sizeof(u64);
  79. namelen = ceph_decode_32(p); /* skip mds name */
  80. *p += namelen;
  81. ceph_decode_need(p, end,
  82. 4*sizeof(u32) + sizeof(u64) +
  83. sizeof(addr) + sizeof(struct ceph_timespec),
  84. bad);
  85. mds = ceph_decode_32(p);
  86. inc = ceph_decode_32(p);
  87. state = ceph_decode_32(p);
  88. state_seq = ceph_decode_64(p);
  89. ceph_decode_copy(p, &addr, sizeof(addr));
  90. ceph_decode_addr(&addr);
  91. ceph_decode_copy(p, &laggy_since, sizeof(laggy_since));
  92. *p += sizeof(u32);
  93. ceph_decode_32_safe(p, end, namelen, bad);
  94. *p += namelen;
  95. if (infoversion >= 2) {
  96. ceph_decode_32_safe(p, end, num_export_targets, bad);
  97. pexport_targets = *p;
  98. *p += num_export_targets * sizeof(u32);
  99. } else {
  100. num_export_targets = 0;
  101. }
  102. dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n",
  103. i+1, n, global_id, mds, inc,
  104. ceph_pr_addr(&addr.in_addr),
  105. ceph_mds_state_name(state));
  106. if (mds >= 0 && mds < m->m_max_mds && state > 0) {
  107. m->m_info[mds].global_id = global_id;
  108. m->m_info[mds].state = state;
  109. m->m_info[mds].addr = addr;
  110. m->m_info[mds].laggy =
  111. (laggy_since.tv_sec != 0 ||
  112. laggy_since.tv_nsec != 0);
  113. m->m_info[mds].num_export_targets = num_export_targets;
  114. if (num_export_targets) {
  115. m->m_info[mds].export_targets =
  116. kcalloc(num_export_targets, sizeof(u32),
  117. GFP_NOFS);
  118. for (j = 0; j < num_export_targets; j++)
  119. m->m_info[mds].export_targets[j] =
  120. ceph_decode_32(&pexport_targets);
  121. } else {
  122. m->m_info[mds].export_targets = NULL;
  123. }
  124. }
  125. }
  126. /* pg_pools */
  127. ceph_decode_32_safe(p, end, n, bad);
  128. m->m_num_data_pg_pools = n;
  129. m->m_data_pg_pools = kcalloc(n, sizeof(u32), GFP_NOFS);
  130. if (!m->m_data_pg_pools)
  131. goto badmem;
  132. ceph_decode_need(p, end, sizeof(u32)*(n+1), bad);
  133. for (i = 0; i < n; i++)
  134. m->m_data_pg_pools[i] = ceph_decode_32(p);
  135. m->m_cas_pg_pool = ceph_decode_32(p);
  136. /* ok, we don't care about the rest. */
  137. dout("mdsmap_decode success epoch %u\n", m->m_epoch);
  138. return m;
  139. badmem:
  140. err = -ENOMEM;
  141. bad:
  142. pr_err("corrupt mdsmap\n");
  143. print_hex_dump(KERN_DEBUG, "mdsmap: ",
  144. DUMP_PREFIX_OFFSET, 16, 1,
  145. start, end - start, true);
  146. ceph_mdsmap_destroy(m);
  147. return ERR_PTR(-EINVAL);
  148. }
  149. void ceph_mdsmap_destroy(struct ceph_mdsmap *m)
  150. {
  151. int i;
  152. for (i = 0; i < m->m_max_mds; i++)
  153. kfree(m->m_info[i].export_targets);
  154. kfree(m->m_info);
  155. kfree(m->m_data_pg_pools);
  156. kfree(m);
  157. }