crush.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef _CRUSH_CRUSH_H
  2. #define _CRUSH_CRUSH_H
  3. #include <linux/types.h>
  4. /*
  5. * CRUSH is a pseudo-random data distribution algorithm that
  6. * efficiently distributes input values (typically, data objects)
  7. * across a heterogeneous, structured storage cluster.
  8. *
  9. * The algorithm was originally described in detail in this paper
  10. * (although the algorithm has evolved somewhat since then):
  11. *
  12. * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf
  13. *
  14. * LGPL2
  15. */
  16. #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */
  17. #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */
  18. #define CRUSH_MAX_SET 10 /* max size of a mapping result */
  19. /*
  20. * CRUSH uses user-defined "rules" to describe how inputs should be
  21. * mapped to devices. A rule consists of sequence of steps to perform
  22. * to generate the set of output devices.
  23. */
  24. struct crush_rule_step {
  25. __u32 op;
  26. __s32 arg1;
  27. __s32 arg2;
  28. };
  29. /* step op codes */
  30. enum {
  31. CRUSH_RULE_NOOP = 0,
  32. CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */
  33. CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */
  34. /* arg2 = type */
  35. CRUSH_RULE_CHOOSE_INDEP = 3, /* same */
  36. CRUSH_RULE_EMIT = 4, /* no args */
  37. CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6,
  38. CRUSH_RULE_CHOOSE_LEAF_INDEP = 7,
  39. };
  40. /*
  41. * for specifying choose num (arg1) relative to the max parameter
  42. * passed to do_rule
  43. */
  44. #define CRUSH_CHOOSE_N 0
  45. #define CRUSH_CHOOSE_N_MINUS(x) (-(x))
  46. /*
  47. * The rule mask is used to describe what the rule is intended for.
  48. * Given a ruleset and size of output set, we search through the
  49. * rule list for a matching rule_mask.
  50. */
  51. struct crush_rule_mask {
  52. __u8 ruleset;
  53. __u8 type;
  54. __u8 min_size;
  55. __u8 max_size;
  56. };
  57. struct crush_rule {
  58. __u32 len;
  59. struct crush_rule_mask mask;
  60. struct crush_rule_step steps[0];
  61. };
  62. #define crush_rule_size(len) (sizeof(struct crush_rule) + \
  63. (len)*sizeof(struct crush_rule_step))
  64. /*
  65. * A bucket is a named container of other items (either devices or
  66. * other buckets). Items within a bucket are chosen using one of a
  67. * few different algorithms. The table summarizes how the speed of
  68. * each option measures up against mapping stability when items are
  69. * added or removed.
  70. *
  71. * Bucket Alg Speed Additions Removals
  72. * ------------------------------------------------
  73. * uniform O(1) poor poor
  74. * list O(n) optimal poor
  75. * tree O(log n) good good
  76. * straw O(n) optimal optimal
  77. */
  78. enum {
  79. CRUSH_BUCKET_UNIFORM = 1,
  80. CRUSH_BUCKET_LIST = 2,
  81. CRUSH_BUCKET_TREE = 3,
  82. CRUSH_BUCKET_STRAW = 4
  83. };
  84. static inline const char *crush_bucket_alg_name(int alg)
  85. {
  86. switch (alg) {
  87. case CRUSH_BUCKET_UNIFORM: return "uniform";
  88. case CRUSH_BUCKET_LIST: return "list";
  89. case CRUSH_BUCKET_TREE: return "tree";
  90. case CRUSH_BUCKET_STRAW: return "straw";
  91. default: return "unknown";
  92. }
  93. }
  94. struct crush_bucket {
  95. __s32 id; /* this'll be negative */
  96. __u16 type; /* non-zero; type=0 is reserved for devices */
  97. __u16 alg; /* one of CRUSH_BUCKET_* */
  98. __u32 weight; /* 16-bit fixed point */
  99. __u32 size; /* num items */
  100. __s32 *items;
  101. /*
  102. * cached random permutation: used for uniform bucket and for
  103. * the linear search fallback for the other bucket types.
  104. */
  105. __u32 perm_x; /* @x for which *perm is defined */
  106. __u32 perm_n; /* num elements of *perm that are permuted/defined */
  107. __u32 *perm;
  108. };
  109. struct crush_bucket_uniform {
  110. struct crush_bucket h;
  111. __u32 item_weight; /* 16-bit fixed point; all items equally weighted */
  112. };
  113. struct crush_bucket_list {
  114. struct crush_bucket h;
  115. __u32 *item_weights; /* 16-bit fixed point */
  116. __u32 *sum_weights; /* 16-bit fixed point. element i is sum
  117. of weights 0..i, inclusive */
  118. };
  119. struct crush_bucket_tree {
  120. struct crush_bucket h; /* note: h.size is _tree_ size, not number of
  121. actual items */
  122. __u8 num_nodes;
  123. __u32 *node_weights;
  124. };
  125. struct crush_bucket_straw {
  126. struct crush_bucket h;
  127. __u32 *item_weights; /* 16-bit fixed point */
  128. __u32 *straws; /* 16-bit fixed point */
  129. };
  130. /*
  131. * CRUSH map includes all buckets, rules, etc.
  132. */
  133. struct crush_map {
  134. struct crush_bucket **buckets;
  135. struct crush_rule **rules;
  136. /*
  137. * Parent pointers to identify the parent bucket a device or
  138. * bucket in the hierarchy. If an item appears more than
  139. * once, this is the _last_ time it appeared (where buckets
  140. * are processed in bucket id order, from -1 on down to
  141. * -max_buckets.
  142. */
  143. __u32 *bucket_parents;
  144. __u32 *device_parents;
  145. __s32 max_buckets;
  146. __u32 max_rules;
  147. __s32 max_devices;
  148. };
  149. /* crush.c */
  150. extern int crush_get_bucket_item_weight(struct crush_bucket *b, int pos);
  151. extern void crush_calc_parents(struct crush_map *map);
  152. extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b);
  153. extern void crush_destroy_bucket_list(struct crush_bucket_list *b);
  154. extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b);
  155. extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b);
  156. extern void crush_destroy_bucket(struct crush_bucket *b);
  157. extern void crush_destroy(struct crush_map *map);
  158. #endif