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