ceph_frag.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef FS_CEPH_FRAG_H
  3. #define FS_CEPH_FRAG_H
  4. /*
  5. * "Frags" are a way to describe a subset of a 32-bit number space,
  6. * using a mask and a value to match against that mask. Any given frag
  7. * (subset of the number space) can be partitioned into 2^n sub-frags.
  8. *
  9. * Frags are encoded into a 32-bit word:
  10. * 8 upper bits = "bits"
  11. * 24 lower bits = "value"
  12. * (We could go to 5+27 bits, but who cares.)
  13. *
  14. * We use the _most_ significant bits of the 24 bit value. This makes
  15. * values logically sort.
  16. *
  17. * Unfortunately, because the "bits" field is still in the high bits, we
  18. * can't sort encoded frags numerically. However, it does allow you
  19. * to feed encoded frags as values into frag_contains_value.
  20. */
  21. static inline __u32 ceph_frag_make(__u32 b, __u32 v)
  22. {
  23. return (b << 24) |
  24. (v & (0xffffffu << (24-b)) & 0xffffffu);
  25. }
  26. static inline __u32 ceph_frag_bits(__u32 f)
  27. {
  28. return f >> 24;
  29. }
  30. static inline __u32 ceph_frag_value(__u32 f)
  31. {
  32. return f & 0xffffffu;
  33. }
  34. static inline __u32 ceph_frag_mask(__u32 f)
  35. {
  36. return (0xffffffu << (24-ceph_frag_bits(f))) & 0xffffffu;
  37. }
  38. static inline __u32 ceph_frag_mask_shift(__u32 f)
  39. {
  40. return 24 - ceph_frag_bits(f);
  41. }
  42. static inline bool ceph_frag_contains_value(__u32 f, __u32 v)
  43. {
  44. return (v & ceph_frag_mask(f)) == ceph_frag_value(f);
  45. }
  46. static inline __u32 ceph_frag_make_child(__u32 f, int by, int i)
  47. {
  48. int newbits = ceph_frag_bits(f) + by;
  49. return ceph_frag_make(newbits,
  50. ceph_frag_value(f) | (i << (24 - newbits)));
  51. }
  52. static inline bool ceph_frag_is_leftmost(__u32 f)
  53. {
  54. return ceph_frag_value(f) == 0;
  55. }
  56. static inline bool ceph_frag_is_rightmost(__u32 f)
  57. {
  58. return ceph_frag_value(f) == ceph_frag_mask(f);
  59. }
  60. static inline __u32 ceph_frag_next(__u32 f)
  61. {
  62. return ceph_frag_make(ceph_frag_bits(f),
  63. ceph_frag_value(f) + (0x1000000 >> ceph_frag_bits(f)));
  64. }
  65. /*
  66. * comparator to sort frags logically, as when traversing the
  67. * number space in ascending order...
  68. */
  69. int ceph_frag_compare(__u32 a, __u32 b);
  70. #endif