ceph_frag.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef FS_CEPH_FRAG_H
  2. #define FS_CEPH_FRAG_H
  3. /*
  4. * "Frags" are a way to describe a subset of a 32-bit number space,
  5. * using a mask and a value to match against that mask. Any given frag
  6. * (subset of the number space) can be partitioned into 2^n sub-frags.
  7. *
  8. * Frags are encoded into a 32-bit word:
  9. * 8 upper bits = "bits"
  10. * 24 lower bits = "value"
  11. * (We could go to 5+27 bits, but who cares.)
  12. *
  13. * We use the _most_ significant bits of the 24 bit value. This makes
  14. * values logically sort.
  15. *
  16. * Unfortunately, because the "bits" field is still in the high bits, we
  17. * can't sort encoded frags numerically. However, it does allow you
  18. * to feed encoded frags as values into frag_contains_value.
  19. */
  20. static inline __u32 ceph_frag_make(__u32 b, __u32 v)
  21. {
  22. return (b << 24) |
  23. (v & (0xffffffu << (24-b)) & 0xffffffu);
  24. }
  25. static inline __u32 ceph_frag_bits(__u32 f)
  26. {
  27. return f >> 24;
  28. }
  29. static inline __u32 ceph_frag_value(__u32 f)
  30. {
  31. return f & 0xffffffu;
  32. }
  33. static inline __u32 ceph_frag_mask(__u32 f)
  34. {
  35. return (0xffffffu << (24-ceph_frag_bits(f))) & 0xffffffu;
  36. }
  37. static inline __u32 ceph_frag_mask_shift(__u32 f)
  38. {
  39. return 24 - ceph_frag_bits(f);
  40. }
  41. static inline int ceph_frag_contains_value(__u32 f, __u32 v)
  42. {
  43. return (v & ceph_frag_mask(f)) == ceph_frag_value(f);
  44. }
  45. static inline int ceph_frag_contains_frag(__u32 f, __u32 sub)
  46. {
  47. /* is sub as specific as us, and contained by us? */
  48. return ceph_frag_bits(sub) >= ceph_frag_bits(f) &&
  49. (ceph_frag_value(sub) & ceph_frag_mask(f)) == ceph_frag_value(f);
  50. }
  51. static inline __u32 ceph_frag_parent(__u32 f)
  52. {
  53. return ceph_frag_make(ceph_frag_bits(f) - 1,
  54. ceph_frag_value(f) & (ceph_frag_mask(f) << 1));
  55. }
  56. static inline int ceph_frag_is_left_child(__u32 f)
  57. {
  58. return ceph_frag_bits(f) > 0 &&
  59. (ceph_frag_value(f) & (0x1000000 >> ceph_frag_bits(f))) == 0;
  60. }
  61. static inline int ceph_frag_is_right_child(__u32 f)
  62. {
  63. return ceph_frag_bits(f) > 0 &&
  64. (ceph_frag_value(f) & (0x1000000 >> ceph_frag_bits(f))) == 1;
  65. }
  66. static inline __u32 ceph_frag_sibling(__u32 f)
  67. {
  68. return ceph_frag_make(ceph_frag_bits(f),
  69. ceph_frag_value(f) ^ (0x1000000 >> ceph_frag_bits(f)));
  70. }
  71. static inline __u32 ceph_frag_left_child(__u32 f)
  72. {
  73. return ceph_frag_make(ceph_frag_bits(f)+1, ceph_frag_value(f));
  74. }
  75. static inline __u32 ceph_frag_right_child(__u32 f)
  76. {
  77. return ceph_frag_make(ceph_frag_bits(f)+1,
  78. ceph_frag_value(f) | (0x1000000 >> (1+ceph_frag_bits(f))));
  79. }
  80. static inline __u32 ceph_frag_make_child(__u32 f, int by, int i)
  81. {
  82. int newbits = ceph_frag_bits(f) + by;
  83. return ceph_frag_make(newbits,
  84. ceph_frag_value(f) | (i << (24 - newbits)));
  85. }
  86. static inline int ceph_frag_is_leftmost(__u32 f)
  87. {
  88. return ceph_frag_value(f) == 0;
  89. }
  90. static inline int ceph_frag_is_rightmost(__u32 f)
  91. {
  92. return ceph_frag_value(f) == ceph_frag_mask(f);
  93. }
  94. static inline __u32 ceph_frag_next(__u32 f)
  95. {
  96. return ceph_frag_make(ceph_frag_bits(f),
  97. ceph_frag_value(f) + (0x1000000 >> ceph_frag_bits(f)));
  98. }
  99. /*
  100. * comparator to sort frags logically, as when traversing the
  101. * number space in ascending order...
  102. */
  103. int ceph_frag_compare(__u32 a, __u32 b);
  104. #endif