1 #ifndef _FS_CEPH_FRAG_H
2 #define _FS_CEPH_FRAG_H
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.
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.)
14 * We use the _most_ significant bits of the 24 bit value. This makes
15 * values logically sort.
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.
21 static inline __u32
ceph_frag_make(__u32 b
, __u32 v
)
24 (v
& (0xffffffu
<< (24-b
)) & 0xffffffu
);
26 static inline __u32
ceph_frag_bits(__u32 f
)
30 static inline __u32
ceph_frag_value(__u32 f
)
34 static inline __u32
ceph_frag_mask(__u32 f
)
36 return (0xffffffu
<< (24-ceph_frag_bits(f
))) & 0xffffffu
;
38 static inline __u32
ceph_frag_mask_shift(__u32 f
)
40 return 24 - ceph_frag_bits(f
);
43 static inline int ceph_frag_contains_value(__u32 f
, __u32 v
)
45 return (v
& ceph_frag_mask(f
)) == ceph_frag_value(f
);
47 static inline int ceph_frag_contains_frag(__u32 f
, __u32 sub
)
49 /* is sub as specific as us, and contained by us? */
50 return ceph_frag_bits(sub
) >= ceph_frag_bits(f
) &&
51 (ceph_frag_value(sub
) & ceph_frag_mask(f
)) == ceph_frag_value(f
);
54 static inline __u32
ceph_frag_parent(__u32 f
)
56 return ceph_frag_make(ceph_frag_bits(f
) - 1,
57 ceph_frag_value(f
) & (ceph_frag_mask(f
) << 1));
59 static inline int ceph_frag_is_left_child(__u32 f
)
61 return ceph_frag_bits(f
) > 0 &&
62 (ceph_frag_value(f
) & (0x1000000 >> ceph_frag_bits(f
))) == 0;
64 static inline int ceph_frag_is_right_child(__u32 f
)
66 return ceph_frag_bits(f
) > 0 &&
67 (ceph_frag_value(f
) & (0x1000000 >> ceph_frag_bits(f
))) == 1;
69 static inline __u32
ceph_frag_sibling(__u32 f
)
71 return ceph_frag_make(ceph_frag_bits(f
),
72 ceph_frag_value(f
) ^ (0x1000000 >> ceph_frag_bits(f
)));
74 static inline __u32
ceph_frag_left_child(__u32 f
)
76 return ceph_frag_make(ceph_frag_bits(f
)+1, ceph_frag_value(f
));
78 static inline __u32
ceph_frag_right_child(__u32 f
)
80 return ceph_frag_make(ceph_frag_bits(f
)+1,
81 ceph_frag_value(f
) | (0x1000000 >> (1+ceph_frag_bits(f
))));
83 static inline __u32
ceph_frag_make_child(__u32 f
, int by
, int i
)
85 int newbits
= ceph_frag_bits(f
) + by
;
86 return ceph_frag_make(newbits
,
87 ceph_frag_value(f
) | (i
<< (24 - newbits
)));
89 static inline int ceph_frag_is_leftmost(__u32 f
)
91 return ceph_frag_value(f
) == 0;
93 static inline int ceph_frag_is_rightmost(__u32 f
)
95 return ceph_frag_value(f
) == ceph_frag_mask(f
);
97 static inline __u32
ceph_frag_next(__u32 f
)
99 return ceph_frag_make(ceph_frag_bits(f
),
100 ceph_frag_value(f
) + (0x1000000 >> ceph_frag_bits(f
)));
104 * comparator to sort frags logically, as when traversing the
105 * number space in ascending order...
107 int ceph_frag_compare(__u32 a
, __u32 b
);