1 /* SPDX-License-Identifier: GPL-2.0 */
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.
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.)
15 * We use the _most_ significant bits of the 24 bit value. This makes
16 * values logically sort.
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.
22 static inline __u32
ceph_frag_make(__u32 b
, __u32 v
)
25 (v
& (0xffffffu
<< (24-b
)) & 0xffffffu
);
27 static inline __u32
ceph_frag_bits(__u32 f
)
31 static inline __u32
ceph_frag_value(__u32 f
)
35 static inline __u32
ceph_frag_mask(__u32 f
)
37 return (0xffffffu
<< (24-ceph_frag_bits(f
))) & 0xffffffu
;
39 static inline __u32
ceph_frag_mask_shift(__u32 f
)
41 return 24 - ceph_frag_bits(f
);
44 static inline bool ceph_frag_contains_value(__u32 f
, __u32 v
)
46 return (v
& ceph_frag_mask(f
)) == ceph_frag_value(f
);
49 static inline __u32
ceph_frag_make_child(__u32 f
, int by
, int i
)
51 int newbits
= ceph_frag_bits(f
) + by
;
52 return ceph_frag_make(newbits
,
53 ceph_frag_value(f
) | (i
<< (24 - newbits
)));
55 static inline bool ceph_frag_is_leftmost(__u32 f
)
57 return ceph_frag_value(f
) == 0;
59 static inline bool ceph_frag_is_rightmost(__u32 f
)
61 return ceph_frag_value(f
) == ceph_frag_mask(f
);
63 static inline __u32
ceph_frag_next(__u32 f
)
65 return ceph_frag_make(ceph_frag_bits(f
),
66 ceph_frag_value(f
) + (0x1000000 >> ceph_frag_bits(f
)));
70 * comparator to sort frags logically, as when traversing the
71 * number space in ascending order...
73 int ceph_frag_compare(__u32 a
, __u32 b
);