bcachefs: Split out bkey_sort.c

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2018-11-27 18:30:56 -05:00 committed by Kent Overstreet
parent 9d11058a78
commit 5b8a9227f8
8 changed files with 772 additions and 745 deletions

View File

@ -7,6 +7,7 @@ bcachefs-y := \
alloc_foreground.o \
bkey.o \
bkey_methods.o \
bkey_sort.o \
bset.o \
btree_cache.o \
btree_gc.o \

658
fs/bcachefs/bkey_sort.c Normal file
View File

@ -0,0 +1,658 @@
// SPDX-License-Identifier: GPL-2.0
#include "bcachefs.h"
#include "bkey_sort.h"
#include "bset.h"
#include "extents.h"
/* too many iterators, need to clean this up */
/* btree_node_iter_large: */
#define btree_node_iter_cmp_heap(h, _l, _r) btree_node_iter_cmp(b, _l, _r)
static inline bool
bch2_btree_node_iter_large_end(struct btree_node_iter_large *iter)
{
return !iter->used;
}
static inline struct bkey_packed *
bch2_btree_node_iter_large_peek_all(struct btree_node_iter_large *iter,
struct btree *b)
{
return bch2_btree_node_iter_large_end(iter)
? NULL
: __btree_node_offset_to_key(b, iter->data->k);
}
static void
bch2_btree_node_iter_large_advance(struct btree_node_iter_large *iter,
struct btree *b)
{
iter->data->k += __btree_node_offset_to_key(b, iter->data->k)->u64s;
EBUG_ON(!iter->used);
EBUG_ON(iter->data->k > iter->data->end);
if (iter->data->k == iter->data->end)
heap_del(iter, 0, btree_node_iter_cmp_heap, NULL);
else
heap_sift_down(iter, 0, btree_node_iter_cmp_heap, NULL);
}
static inline struct bkey_packed *
bch2_btree_node_iter_large_next_all(struct btree_node_iter_large *iter,
struct btree *b)
{
struct bkey_packed *ret = bch2_btree_node_iter_large_peek_all(iter, b);
if (ret)
bch2_btree_node_iter_large_advance(iter, b);
return ret;
}
void bch2_btree_node_iter_large_push(struct btree_node_iter_large *iter,
struct btree *b,
const struct bkey_packed *k,
const struct bkey_packed *end)
{
if (k != end) {
struct btree_node_iter_set n =
((struct btree_node_iter_set) {
__btree_node_key_to_offset(b, k),
__btree_node_key_to_offset(b, end)
});
__heap_add(iter, n, btree_node_iter_cmp_heap, NULL);
}
}
static void sort_key_next(struct btree_node_iter_large *iter,
struct btree *b,
struct btree_node_iter_set *i)
{
i->k += __btree_node_offset_to_key(b, i->k)->u64s;
if (i->k == i->end)
*i = iter->data[--iter->used];
}
/* regular sort_iters */
typedef int (*sort_cmp_fn)(struct btree *,
struct bkey_packed *,
struct bkey_packed *);
static inline void __sort_iter_sift(struct sort_iter *iter,
unsigned from,
sort_cmp_fn cmp)
{
unsigned i;
for (i = from;
i + 1 < iter->used &&
cmp(iter->b, iter->data[i].k, iter->data[i + 1].k) > 0;
i++)
swap(iter->data[i], iter->data[i + 1]);
}
static inline void sort_iter_sift(struct sort_iter *iter, sort_cmp_fn cmp)
{
__sort_iter_sift(iter, 0, cmp);
}
static inline void sort_iter_sort(struct sort_iter *iter, sort_cmp_fn cmp)
{
unsigned i = iter->used;
while (i--)
__sort_iter_sift(iter, i, cmp);
}
static inline struct bkey_packed *sort_iter_peek(struct sort_iter *iter)
{
return iter->used ? iter->data->k : NULL;
}
static inline void sort_iter_advance(struct sort_iter *iter, sort_cmp_fn cmp)
{
iter->data->k = bkey_next(iter->data->k);
BUG_ON(iter->data->k > iter->data->end);
if (iter->data->k == iter->data->end)
array_remove_item(iter->data, iter->used, 0);
else
sort_iter_sift(iter, cmp);
}
static inline struct bkey_packed *sort_iter_next(struct sort_iter *iter,
sort_cmp_fn cmp)
{
struct bkey_packed *ret = sort_iter_peek(iter);
if (ret)
sort_iter_advance(iter, cmp);
return ret;
}
/*
* Returns true if l > r - unless l == r, in which case returns true if l is
* older than r.
*
* Necessary for btree_sort_fixup() - if there are multiple keys that compare
* equal in different sets, we have to process them newest to oldest.
*/
#define key_sort_cmp(h, l, r) \
({ \
bkey_cmp_packed(b, \
__btree_node_offset_to_key(b, (l).k), \
__btree_node_offset_to_key(b, (r).k)) \
\
?: (l).k - (r).k; \
})
static inline bool should_drop_next_key(struct btree_node_iter_large *iter,
struct btree *b)
{
struct btree_node_iter_set *l = iter->data, *r = iter->data + 1;
struct bkey_packed *k = __btree_node_offset_to_key(b, l->k);
if (bkey_whiteout(k))
return true;
if (iter->used < 2)
return false;
if (iter->used > 2 &&
key_sort_cmp(iter, r[0], r[1]) >= 0)
r++;
/*
* key_sort_cmp() ensures that when keys compare equal the older key
* comes first; so if l->k compares equal to r->k then l->k is older and
* should be dropped.
*/
return !bkey_cmp_packed(b,
__btree_node_offset_to_key(b, l->k),
__btree_node_offset_to_key(b, r->k));
}
struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *dst,
struct btree *b,
struct btree_node_iter_large *iter)
{
struct bkey_packed *out = dst->start;
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
heap_resort(iter, key_sort_cmp, NULL);
while (!bch2_btree_node_iter_large_end(iter)) {
if (!should_drop_next_key(iter, b)) {
struct bkey_packed *k =
__btree_node_offset_to_key(b, iter->data->k);
bkey_copy(out, k);
btree_keys_account_key_add(&nr, 0, out);
out = bkey_next(out);
}
sort_key_next(iter, b, iter->data);
heap_sift_down(iter, 0, key_sort_cmp, NULL);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
/*
* If keys compare equal, compare by pointer order:
*
* Necessary for sort_fix_overlapping() - if there are multiple keys that
* compare equal in different sets, we have to process them newest to oldest.
*/
#define extent_sort_cmp(h, l, r) \
({ \
struct bkey _ul = bkey_unpack_key(b, \
__btree_node_offset_to_key(b, (l).k)); \
struct bkey _ur = bkey_unpack_key(b, \
__btree_node_offset_to_key(b, (r).k)); \
\
bkey_cmp(bkey_start_pos(&_ul), \
bkey_start_pos(&_ur)) ?: (r).k - (l).k; \
})
static inline void extent_sort_sift(struct btree_node_iter_large *iter,
struct btree *b, size_t i)
{
heap_sift_down(iter, i, extent_sort_cmp, NULL);
}
static inline void extent_sort_next(struct btree_node_iter_large *iter,
struct btree *b,
struct btree_node_iter_set *i)
{
sort_key_next(iter, b, i);
heap_sift_down(iter, i - iter->data, extent_sort_cmp, NULL);
}
static void extent_sort_append(struct bch_fs *c,
struct btree *b,
struct btree_nr_keys *nr,
struct bkey_packed *start,
struct bkey_packed **prev,
struct bkey_packed *k)
{
struct bkey_format *f = &b->format;
BKEY_PADDED(k) tmp;
if (bkey_whiteout(k))
return;
bch2_bkey_unpack(b, &tmp.k, k);
if (*prev &&
bch2_extent_merge(c, b, (void *) *prev, &tmp.k))
return;
if (*prev) {
bch2_bkey_pack(*prev, (void *) *prev, f);
btree_keys_account_key_add(nr, 0, *prev);
*prev = bkey_next(*prev);
} else {
*prev = start;
}
bkey_copy(*prev, &tmp.k);
}
struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
struct bset *dst,
struct btree *b,
struct btree_node_iter_large *iter)
{
struct bkey_format *f = &b->format;
struct btree_node_iter_set *_l = iter->data, *_r;
struct bkey_packed *prev = NULL, *out, *lk, *rk;
struct bkey l_unpacked, r_unpacked;
struct bkey_s l, r;
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
heap_resort(iter, extent_sort_cmp, NULL);
while (!bch2_btree_node_iter_large_end(iter)) {
lk = __btree_node_offset_to_key(b, _l->k);
if (iter->used == 1) {
extent_sort_append(c, b, &nr, dst->start, &prev, lk);
extent_sort_next(iter, b, _l);
continue;
}
_r = iter->data + 1;
if (iter->used > 2 &&
extent_sort_cmp(iter, _r[0], _r[1]) >= 0)
_r++;
rk = __btree_node_offset_to_key(b, _r->k);
l = __bkey_disassemble(b, lk, &l_unpacked);
r = __bkey_disassemble(b, rk, &r_unpacked);
/* If current key and next key don't overlap, just append */
if (bkey_cmp(l.k->p, bkey_start_pos(r.k)) <= 0) {
extent_sort_append(c, b, &nr, dst->start, &prev, lk);
extent_sort_next(iter, b, _l);
continue;
}
/* Skip 0 size keys */
if (!r.k->size) {
extent_sort_next(iter, b, _r);
continue;
}
/*
* overlap: keep the newer key and trim the older key so they
* don't overlap. comparing pointers tells us which one is
* newer, since the bsets are appended one after the other.
*/
/* can't happen because of comparison func */
BUG_ON(_l->k < _r->k &&
!bkey_cmp(bkey_start_pos(l.k), bkey_start_pos(r.k)));
if (_l->k > _r->k) {
/* l wins, trim r */
if (bkey_cmp(l.k->p, r.k->p) >= 0) {
sort_key_next(iter, b, _r);
} else {
__bch2_cut_front(l.k->p, r);
extent_save(b, rk, r.k);
}
extent_sort_sift(iter, b, _r - iter->data);
} else if (bkey_cmp(l.k->p, r.k->p) > 0) {
BKEY_PADDED(k) tmp;
/*
* r wins, but it overlaps in the middle of l - split l:
*/
bkey_reassemble(&tmp.k, l.s_c);
bch2_cut_back(bkey_start_pos(r.k), &tmp.k.k);
__bch2_cut_front(r.k->p, l);
extent_save(b, lk, l.k);
extent_sort_sift(iter, b, 0);
extent_sort_append(c, b, &nr, dst->start, &prev,
bkey_to_packed(&tmp.k));
} else {
bch2_cut_back(bkey_start_pos(r.k), l.k);
extent_save(b, lk, l.k);
}
}
if (prev) {
bch2_bkey_pack(prev, (void *) prev, f);
btree_keys_account_key_add(&nr, 0, prev);
out = bkey_next(prev);
} else {
out = dst->start;
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
/* Sort + repack in a new format: */
static struct btree_nr_keys
bch2_sort_repack(struct bset *dst, struct btree *src,
struct btree_node_iter *src_iter,
struct bkey_format *out_f,
bool filter_whiteouts)
{
struct bkey_format *in_f = &src->format;
struct bkey_packed *in, *out = vstruct_last(dst);
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
while ((in = bch2_btree_node_iter_next_all(src_iter, src))) {
if (filter_whiteouts && bkey_whiteout(in))
continue;
if (bch2_bkey_transform(out_f, out, bkey_packed(in)
? in_f : &bch2_bkey_format_current, in))
out->format = KEY_FORMAT_LOCAL_BTREE;
else
bch2_bkey_unpack(src, (void *) out, in);
btree_keys_account_key_add(&nr, 0, out);
out = bkey_next(out);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
/* Sort, repack, and merge: */
struct btree_nr_keys
bch2_sort_repack_merge(struct bch_fs *c,
struct bset *dst, struct btree *src,
struct btree_node_iter *iter,
struct bkey_format *out_f,
bool filter_whiteouts,
key_filter_fn filter,
key_merge_fn merge)
{
struct bkey_packed *k, *prev = NULL, *out;
struct btree_nr_keys nr;
BKEY_PADDED(k) tmp;
if (!filter && !merge)
return bch2_sort_repack(dst, src, iter, out_f,
filter_whiteouts);
memset(&nr, 0, sizeof(nr));
while ((k = bch2_btree_node_iter_next_all(iter, src))) {
if (filter_whiteouts && bkey_whiteout(k))
continue;
/*
* The filter might modify pointers, so we have to unpack the
* key and values to &tmp.k:
*/
bch2_bkey_unpack(src, &tmp.k, k);
if (filter && filter(c, src, bkey_i_to_s(&tmp.k)))
continue;
/* prev is always unpacked, for key merging: */
if (prev &&
merge &&
merge(c, src, (void *) prev, &tmp.k) == BCH_MERGE_MERGE)
continue;
/*
* the current key becomes the new prev: advance prev, then
* copy the current key - but first pack prev (in place):
*/
if (prev) {
bch2_bkey_pack(prev, (void *) prev, out_f);
btree_keys_account_key_add(&nr, 0, prev);
prev = bkey_next(prev);
} else {
prev = vstruct_last(dst);
}
bkey_copy(prev, &tmp.k);
}
if (prev) {
bch2_bkey_pack(prev, (void *) prev, out_f);
btree_keys_account_key_add(&nr, 0, prev);
out = bkey_next(prev);
} else {
out = vstruct_last(dst);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
static inline int sort_keys_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r) ?:
(int) bkey_whiteout(r) - (int) bkey_whiteout(l) ?:
(int) l->needs_whiteout - (int) r->needs_whiteout;
}
unsigned bch2_sort_keys(struct bkey_packed *dst,
struct sort_iter *iter,
bool filter_whiteouts)
{
const struct bkey_format *f = &iter->b->format;
struct bkey_packed *in, *next, *out = dst;
sort_iter_sort(iter, sort_keys_cmp);
while ((in = sort_iter_next(iter, sort_keys_cmp))) {
if (bkey_whiteout(in) &&
(filter_whiteouts || !in->needs_whiteout))
continue;
if (bkey_whiteout(in) &&
(next = sort_iter_peek(iter)) &&
!bkey_cmp_packed(iter->b, in, next)) {
BUG_ON(in->needs_whiteout &&
next->needs_whiteout);
/*
* XXX racy, called with read lock from write path
*
* leads to spurious BUG_ON() in bkey_unpack_key() in
* debug mode
*/
next->needs_whiteout |= in->needs_whiteout;
continue;
}
if (bkey_whiteout(in)) {
memcpy_u64s(out, in, bkeyp_key_u64s(f, in));
set_bkeyp_val_u64s(f, out, 0);
} else {
bkey_copy(out, in);
}
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static inline int sort_extents_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r) ?:
(int) bkey_deleted(l) - (int) bkey_deleted(r);
}
unsigned bch2_sort_extents(struct bkey_packed *dst,
struct sort_iter *iter,
bool filter_whiteouts)
{
struct bkey_packed *in, *out = dst;
sort_iter_sort(iter, sort_extents_cmp);
while ((in = sort_iter_next(iter, sort_extents_cmp))) {
if (bkey_deleted(in))
continue;
if (bkey_whiteout(in) &&
(filter_whiteouts || !in->needs_whiteout))
continue;
bkey_copy(out, in);
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static inline int sort_key_whiteouts_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r);
}
unsigned bch2_sort_key_whiteouts(struct bkey_packed *dst,
struct sort_iter *iter)
{
struct bkey_packed *in, *out = dst;
sort_iter_sort(iter, sort_key_whiteouts_cmp);
while ((in = sort_iter_next(iter, sort_key_whiteouts_cmp))) {
bkey_copy(out, in);
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static inline int sort_extent_whiteouts_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
struct bkey ul = bkey_unpack_key(b, l);
struct bkey ur = bkey_unpack_key(b, r);
return bkey_cmp(bkey_start_pos(&ul), bkey_start_pos(&ur));
}
unsigned bch2_sort_extent_whiteouts(struct bkey_packed *dst,
struct sort_iter *iter)
{
const struct bkey_format *f = &iter->b->format;
struct bkey_packed *in, *out = dst;
struct bkey_i l, r;
bool prev = false, l_packed = false;
u64 max_packed_size = bkey_field_max(f, BKEY_FIELD_SIZE);
u64 max_packed_offset = bkey_field_max(f, BKEY_FIELD_OFFSET);
u64 new_size;
max_packed_size = min_t(u64, max_packed_size, KEY_SIZE_MAX);
sort_iter_sort(iter, sort_extent_whiteouts_cmp);
while ((in = sort_iter_next(iter, sort_extent_whiteouts_cmp))) {
if (bkey_deleted(in))
continue;
EBUG_ON(bkeyp_val_u64s(f, in));
EBUG_ON(in->type != KEY_TYPE_DISCARD);
r.k = bkey_unpack_key(iter->b, in);
if (prev &&
bkey_cmp(l.k.p, bkey_start_pos(&r.k)) >= 0) {
if (bkey_cmp(l.k.p, r.k.p) >= 0)
continue;
new_size = l_packed
? min(max_packed_size, max_packed_offset -
bkey_start_offset(&l.k))
: KEY_SIZE_MAX;
new_size = min(new_size, r.k.p.offset -
bkey_start_offset(&l.k));
BUG_ON(new_size < l.k.size);
bch2_key_resize(&l.k, new_size);
if (bkey_cmp(l.k.p, r.k.p) >= 0)
continue;
bch2_cut_front(l.k.p, &r);
}
if (prev) {
if (!bch2_bkey_pack(out, &l, f)) {
BUG_ON(l_packed);
bkey_copy(out, &l);
}
out = bkey_next(out);
}
l = r;
prev = true;
l_packed = bkey_packed(in);
}
if (prev) {
if (!bch2_bkey_pack(out, &l, f)) {
BUG_ON(l_packed);
bkey_copy(out, &l);
}
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}

68
fs/bcachefs/bkey_sort.h Normal file
View File

@ -0,0 +1,68 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_BKEY_SORT_H
#define _BCACHEFS_BKEY_SORT_H
struct btree_node_iter_large {
u16 used;
struct btree_node_iter_set data[MAX_BSETS];
};
void bch2_btree_node_iter_large_push(struct btree_node_iter_large *,
struct btree *,
const struct bkey_packed *,
const struct bkey_packed *);
struct sort_iter {
struct btree *b;
unsigned used;
struct sort_iter_set {
struct bkey_packed *k, *end;
} data[MAX_BSETS + 1];
};
static inline void sort_iter_init(struct sort_iter *iter, struct btree *b)
{
memset(iter, 0, sizeof(*iter));
iter->b = b;
}
static inline void sort_iter_add(struct sort_iter *iter,
struct bkey_packed *k,
struct bkey_packed *end)
{
BUG_ON(iter->used >= ARRAY_SIZE(iter->data));
if (k != end)
iter->data[iter->used++] = (struct sort_iter_set) { k, end };
}
struct btree_nr_keys
bch2_key_sort_fix_overlapping(struct bset *, struct btree *,
struct btree_node_iter_large *);
struct btree_nr_keys
bch2_extent_sort_fix_overlapping(struct bch_fs *, struct bset *,
struct btree *,
struct btree_node_iter_large *);
struct btree_nr_keys
bch2_sort_repack_merge(struct bch_fs *,
struct bset *, struct btree *,
struct btree_node_iter *,
struct bkey_format *,
bool,
key_filter_fn,
key_merge_fn);
unsigned bch2_sort_keys(struct bkey_packed *,
struct sort_iter *, bool);
unsigned bch2_sort_extents(struct bkey_packed *,
struct sort_iter *, bool);
unsigned bch2_sort_key_whiteouts(struct bkey_packed *,
struct sort_iter *);
unsigned bch2_sort_extent_whiteouts(struct bkey_packed *,
struct sort_iter *);
#endif /* _BCACHEFS_BKEY_SORT_H */

View File

@ -2,6 +2,7 @@
#include "bcachefs.h"
#include "bkey_methods.h"
#include "bkey_sort.h"
#include "btree_cache.h"
#include "btree_io.h"
#include "btree_iter.h"
@ -19,40 +20,6 @@
#include "super-io.h"
#include "trace.h"
/* btree_node_iter_large: */
#define btree_node_iter_cmp_heap(h, _l, _r) btree_node_iter_cmp(b, _l, _r)
void bch2_btree_node_iter_large_push(struct btree_node_iter_large *iter,
struct btree *b,
const struct bkey_packed *k,
const struct bkey_packed *end)
{
if (k != end) {
struct btree_node_iter_set n =
((struct btree_node_iter_set) {
__btree_node_key_to_offset(b, k),
__btree_node_key_to_offset(b, end)
});
__heap_add(iter, n, btree_node_iter_cmp_heap, NULL);
}
}
void bch2_btree_node_iter_large_advance(struct btree_node_iter_large *iter,
struct btree *b)
{
iter->data->k += __btree_node_offset_to_key(b, iter->data->k)->u64s;
EBUG_ON(!iter->used);
EBUG_ON(iter->data->k > iter->data->end);
if (iter->data->k == iter->data->end)
heap_del(iter, 0, btree_node_iter_cmp_heap, NULL);
else
heap_sift_down(iter, 0, btree_node_iter_cmp_heap, NULL);
}
static void verify_no_dups(struct btree *b,
struct bkey_packed *start,
struct bkey_packed *end)
@ -113,193 +80,6 @@ static void *btree_bounce_alloc(struct bch_fs *c, unsigned order,
return mempool_alloc(&c->btree_bounce_pool, GFP_NOIO);
}
typedef int (*sort_cmp_fn)(struct btree *,
struct bkey_packed *,
struct bkey_packed *);
struct sort_iter {
struct btree *b;
unsigned used;
struct sort_iter_set {
struct bkey_packed *k, *end;
} data[MAX_BSETS + 1];
};
static void sort_iter_init(struct sort_iter *iter, struct btree *b)
{
memset(iter, 0, sizeof(*iter));
iter->b = b;
}
static inline void __sort_iter_sift(struct sort_iter *iter,
unsigned from,
sort_cmp_fn cmp)
{
unsigned i;
for (i = from;
i + 1 < iter->used &&
cmp(iter->b, iter->data[i].k, iter->data[i + 1].k) > 0;
i++)
swap(iter->data[i], iter->data[i + 1]);
}
static inline void sort_iter_sift(struct sort_iter *iter, sort_cmp_fn cmp)
{
__sort_iter_sift(iter, 0, cmp);
}
static inline void sort_iter_sort(struct sort_iter *iter, sort_cmp_fn cmp)
{
unsigned i = iter->used;
while (i--)
__sort_iter_sift(iter, i, cmp);
}
static void sort_iter_add(struct sort_iter *iter,
struct bkey_packed *k,
struct bkey_packed *end)
{
BUG_ON(iter->used >= ARRAY_SIZE(iter->data));
if (k != end)
iter->data[iter->used++] = (struct sort_iter_set) { k, end };
}
static inline struct bkey_packed *sort_iter_peek(struct sort_iter *iter)
{
return iter->used ? iter->data->k : NULL;
}
static inline void sort_iter_advance(struct sort_iter *iter, sort_cmp_fn cmp)
{
iter->data->k = bkey_next(iter->data->k);
BUG_ON(iter->data->k > iter->data->end);
if (iter->data->k == iter->data->end)
array_remove_item(iter->data, iter->used, 0);
else
sort_iter_sift(iter, cmp);
}
static inline struct bkey_packed *sort_iter_next(struct sort_iter *iter,
sort_cmp_fn cmp)
{
struct bkey_packed *ret = sort_iter_peek(iter);
if (ret)
sort_iter_advance(iter, cmp);
return ret;
}
static inline int sort_key_whiteouts_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r);
}
static unsigned sort_key_whiteouts(struct bkey_packed *dst,
struct sort_iter *iter)
{
struct bkey_packed *in, *out = dst;
sort_iter_sort(iter, sort_key_whiteouts_cmp);
while ((in = sort_iter_next(iter, sort_key_whiteouts_cmp))) {
bkey_copy(out, in);
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static inline int sort_extent_whiteouts_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
struct bkey ul = bkey_unpack_key(b, l);
struct bkey ur = bkey_unpack_key(b, r);
return bkey_cmp(bkey_start_pos(&ul), bkey_start_pos(&ur));
}
static unsigned sort_extent_whiteouts(struct bkey_packed *dst,
struct sort_iter *iter)
{
const struct bkey_format *f = &iter->b->format;
struct bkey_packed *in, *out = dst;
struct bkey_i l, r;
bool prev = false, l_packed = false;
u64 max_packed_size = bkey_field_max(f, BKEY_FIELD_SIZE);
u64 max_packed_offset = bkey_field_max(f, BKEY_FIELD_OFFSET);
u64 new_size;
max_packed_size = min_t(u64, max_packed_size, KEY_SIZE_MAX);
sort_iter_sort(iter, sort_extent_whiteouts_cmp);
while ((in = sort_iter_next(iter, sort_extent_whiteouts_cmp))) {
if (bkey_deleted(in))
continue;
EBUG_ON(bkeyp_val_u64s(f, in));
EBUG_ON(in->type != KEY_TYPE_DISCARD);
r.k = bkey_unpack_key(iter->b, in);
if (prev &&
bkey_cmp(l.k.p, bkey_start_pos(&r.k)) >= 0) {
if (bkey_cmp(l.k.p, r.k.p) >= 0)
continue;
new_size = l_packed
? min(max_packed_size, max_packed_offset -
bkey_start_offset(&l.k))
: KEY_SIZE_MAX;
new_size = min(new_size, r.k.p.offset -
bkey_start_offset(&l.k));
BUG_ON(new_size < l.k.size);
bch2_key_resize(&l.k, new_size);
if (bkey_cmp(l.k.p, r.k.p) >= 0)
continue;
bch2_cut_front(l.k.p, &r);
}
if (prev) {
if (!bch2_bkey_pack(out, &l, f)) {
BUG_ON(l_packed);
bkey_copy(out, &l);
}
out = bkey_next(out);
}
l = r;
prev = true;
l_packed = bkey_packed(in);
}
if (prev) {
if (!bch2_bkey_pack(out, &l, f)) {
BUG_ON(l_packed);
bkey_copy(out, &l);
}
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static unsigned should_compact_bset(struct btree *b, struct bset_tree *t,
bool compacting,
enum compact_mode mode)
@ -420,11 +200,10 @@ bool __bch2_compact_whiteouts(struct bch_fs *c, struct btree *b,
BUG_ON((void *) unwritten_whiteouts_start(c, b) <
(void *) btree_bkey_last(b, bset_tree_last(b)));
u64s = btree_node_is_extents(b)
? sort_extent_whiteouts(unwritten_whiteouts_start(c, b),
&sort_iter)
: sort_key_whiteouts(unwritten_whiteouts_start(c, b),
&sort_iter);
u64s = (btree_node_is_extents(b)
? bch2_sort_extent_whiteouts
: bch2_sort_key_whiteouts)(unwritten_whiteouts_start(c, b),
&sort_iter);
BUG_ON(u64s > b->whiteout_u64s);
BUG_ON(u64s != b->whiteout_u64s && !btree_node_is_extents(b));
@ -499,87 +278,6 @@ static bool bch2_drop_whiteouts(struct btree *b)
return ret;
}
static inline int sort_keys_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r) ?:
(int) bkey_whiteout(r) - (int) bkey_whiteout(l) ?:
(int) l->needs_whiteout - (int) r->needs_whiteout;
}
static unsigned sort_keys(struct bkey_packed *dst,
struct sort_iter *iter,
bool filter_whiteouts)
{
const struct bkey_format *f = &iter->b->format;
struct bkey_packed *in, *next, *out = dst;
sort_iter_sort(iter, sort_keys_cmp);
while ((in = sort_iter_next(iter, sort_keys_cmp))) {
if (bkey_whiteout(in) &&
(filter_whiteouts || !in->needs_whiteout))
continue;
if (bkey_whiteout(in) &&
(next = sort_iter_peek(iter)) &&
!bkey_cmp_packed(iter->b, in, next)) {
BUG_ON(in->needs_whiteout &&
next->needs_whiteout);
/*
* XXX racy, called with read lock from write path
*
* leads to spurious BUG_ON() in bkey_unpack_key() in
* debug mode
*/
next->needs_whiteout |= in->needs_whiteout;
continue;
}
if (bkey_whiteout(in)) {
memcpy_u64s(out, in, bkeyp_key_u64s(f, in));
set_bkeyp_val_u64s(f, out, 0);
} else {
bkey_copy(out, in);
}
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static inline int sort_extents_cmp(struct btree *b,
struct bkey_packed *l,
struct bkey_packed *r)
{
return bkey_cmp_packed(b, l, r) ?:
(int) bkey_deleted(l) - (int) bkey_deleted(r);
}
static unsigned sort_extents(struct bkey_packed *dst,
struct sort_iter *iter,
bool filter_whiteouts)
{
struct bkey_packed *in, *out = dst;
sort_iter_sort(iter, sort_extents_cmp);
while ((in = sort_iter_next(iter, sort_extents_cmp))) {
if (bkey_deleted(in))
continue;
if (bkey_whiteout(in) &&
(filter_whiteouts || !in->needs_whiteout))
continue;
bkey_copy(out, in);
out = bkey_next(out);
}
return (u64 *) out - (u64 *) dst;
}
static void btree_node_sort(struct bch_fs *c, struct btree *b,
struct btree_iter *iter,
unsigned start_idx,
@ -618,9 +316,11 @@ static void btree_node_sort(struct bch_fs *c, struct btree *b,
if (btree_node_is_extents(b))
filter_whiteouts = bset_written(b, start_bset);
u64s = btree_node_is_extents(b)
? sort_extents(out->keys.start, &sort_iter, filter_whiteouts)
: sort_keys(out->keys.start, &sort_iter, filter_whiteouts);
u64s = (btree_node_is_extents(b)
? bch2_sort_extents
: bch2_sort_keys)(out->keys.start,
&sort_iter,
filter_whiteouts);
out->keys.u64s = cpu_to_le16(u64s);
@ -678,101 +378,6 @@ static void btree_node_sort(struct bch_fs *c, struct btree *b,
bch2_verify_btree_nr_keys(b);
}
/* Sort + repack in a new format: */
static struct btree_nr_keys sort_repack(struct bset *dst,
struct btree *src,
struct btree_node_iter *src_iter,
struct bkey_format *out_f,
bool filter_whiteouts)
{
struct bkey_format *in_f = &src->format;
struct bkey_packed *in, *out = vstruct_last(dst);
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
while ((in = bch2_btree_node_iter_next_all(src_iter, src))) {
if (filter_whiteouts && bkey_whiteout(in))
continue;
if (bch2_bkey_transform(out_f, out, bkey_packed(in)
? in_f : &bch2_bkey_format_current, in))
out->format = KEY_FORMAT_LOCAL_BTREE;
else
bch2_bkey_unpack(src, (void *) out, in);
btree_keys_account_key_add(&nr, 0, out);
out = bkey_next(out);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
/* Sort, repack, and merge: */
static struct btree_nr_keys sort_repack_merge(struct bch_fs *c,
struct bset *dst,
struct btree *src,
struct btree_node_iter *iter,
struct bkey_format *out_f,
bool filter_whiteouts,
key_filter_fn filter,
key_merge_fn merge)
{
struct bkey_packed *k, *prev = NULL, *out;
struct btree_nr_keys nr;
BKEY_PADDED(k) tmp;
memset(&nr, 0, sizeof(nr));
while ((k = bch2_btree_node_iter_next_all(iter, src))) {
if (filter_whiteouts && bkey_whiteout(k))
continue;
/*
* The filter might modify pointers, so we have to unpack the
* key and values to &tmp.k:
*/
bch2_bkey_unpack(src, &tmp.k, k);
if (filter && filter(c, src, bkey_i_to_s(&tmp.k)))
continue;
/* prev is always unpacked, for key merging: */
if (prev &&
merge &&
merge(c, src, (void *) prev, &tmp.k) == BCH_MERGE_MERGE)
continue;
/*
* the current key becomes the new prev: advance prev, then
* copy the current key - but first pack prev (in place):
*/
if (prev) {
bch2_bkey_pack(prev, (void *) prev, out_f);
btree_keys_account_key_add(&nr, 0, prev);
prev = bkey_next(prev);
} else {
prev = vstruct_last(dst);
}
bkey_copy(prev, &tmp.k);
}
if (prev) {
bch2_bkey_pack(prev, (void *) prev, out_f);
btree_keys_account_key_add(&nr, 0, prev);
out = bkey_next(prev);
} else {
out = vstruct_last(dst);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
void bch2_btree_sort_into(struct bch_fs *c,
struct btree *dst,
struct btree *src)
@ -787,19 +392,12 @@ void bch2_btree_sort_into(struct bch_fs *c,
bch2_btree_node_iter_init_from_start(&src_iter, src);
if (btree_node_ops(src)->key_normalize ||
btree_node_ops(src)->key_merge)
nr = sort_repack_merge(c, btree_bset_first(dst),
src, &src_iter,
&dst->format,
true,
btree_node_ops(src)->key_normalize,
btree_node_ops(src)->key_merge);
else
nr = sort_repack(btree_bset_first(dst),
src, &src_iter,
&dst->format,
true);
nr = bch2_sort_repack_merge(c, btree_bset_first(dst),
src, &src_iter,
&dst->format,
true,
btree_node_ops(src)->key_normalize,
btree_node_ops(src)->key_merge);
bch2_time_stats_update(&c->times[BCH_TIME_btree_sort], start_time);
@ -1815,8 +1413,8 @@ void __bch2_btree_node_write(struct bch_fs *c, struct btree *b,
b->whiteout_u64s = 0;
u64s = btree_node_is_extents(b)
? sort_extents(vstruct_last(i), &sort_iter, false)
: sort_keys(i->start, &sort_iter, false);
? bch2_sort_extents(vstruct_last(i), &sort_iter, false)
: bch2_sort_keys(i->start, &sort_iter, false);
le16_add_cpu(&i->u64s, u64s);
clear_needs_whiteout(i);

View File

@ -143,46 +143,4 @@ void bch2_btree_flush_all_writes(struct bch_fs *);
void bch2_btree_verify_flushed(struct bch_fs *);
ssize_t bch2_dirty_btree_nodes_print(struct bch_fs *, char *);
/* Sorting */
struct btree_node_iter_large {
u16 used;
struct btree_node_iter_set data[MAX_BSETS];
};
void bch2_btree_node_iter_large_advance(struct btree_node_iter_large *,
struct btree *);
void bch2_btree_node_iter_large_push(struct btree_node_iter_large *,
struct btree *,
const struct bkey_packed *,
const struct bkey_packed *);
static inline bool bch2_btree_node_iter_large_end(struct btree_node_iter_large *iter)
{
return !iter->used;
}
static inline struct bkey_packed *
bch2_btree_node_iter_large_peek_all(struct btree_node_iter_large *iter,
struct btree *b)
{
return bch2_btree_node_iter_large_end(iter)
? NULL
: __btree_node_offset_to_key(b, iter->data->k);
}
static inline struct bkey_packed *
bch2_btree_node_iter_large_next_all(struct btree_node_iter_large *iter,
struct btree *b)
{
struct bkey_packed *ret = bch2_btree_node_iter_large_peek_all(iter, b);
if (ret)
bch2_btree_node_iter_large_advance(iter, b);
return ret;
}
#endif /* _BCACHEFS_BTREE_IO_H */

View File

@ -27,87 +27,6 @@
#include "util.h"
#include "xattr.h"
static void sort_key_next(struct btree_node_iter_large *iter,
struct btree *b,
struct btree_node_iter_set *i)
{
i->k += __btree_node_offset_to_key(b, i->k)->u64s;
if (i->k == i->end)
*i = iter->data[--iter->used];
}
/*
* Returns true if l > r - unless l == r, in which case returns true if l is
* older than r.
*
* Necessary for btree_sort_fixup() - if there are multiple keys that compare
* equal in different sets, we have to process them newest to oldest.
*/
#define key_sort_cmp(h, l, r) \
({ \
bkey_cmp_packed(b, \
__btree_node_offset_to_key(b, (l).k), \
__btree_node_offset_to_key(b, (r).k)) \
\
?: (l).k - (r).k; \
})
static inline bool should_drop_next_key(struct btree_node_iter_large *iter,
struct btree *b)
{
struct btree_node_iter_set *l = iter->data, *r = iter->data + 1;
struct bkey_packed *k = __btree_node_offset_to_key(b, l->k);
if (bkey_whiteout(k))
return true;
if (iter->used < 2)
return false;
if (iter->used > 2 &&
key_sort_cmp(iter, r[0], r[1]) >= 0)
r++;
/*
* key_sort_cmp() ensures that when keys compare equal the older key
* comes first; so if l->k compares equal to r->k then l->k is older and
* should be dropped.
*/
return !bkey_cmp_packed(b,
__btree_node_offset_to_key(b, l->k),
__btree_node_offset_to_key(b, r->k));
}
struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *dst,
struct btree *b,
struct btree_node_iter_large *iter)
{
struct bkey_packed *out = dst->start;
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
heap_resort(iter, key_sort_cmp, NULL);
while (!bch2_btree_node_iter_large_end(iter)) {
if (!should_drop_next_key(iter, b)) {
struct bkey_packed *k =
__btree_node_offset_to_key(b, iter->data->k);
bkey_copy(out, k);
btree_keys_account_key_add(&nr, 0, out);
out = bkey_next(out);
}
sort_key_next(iter, b, iter->data);
heap_sift_down(iter, 0, key_sort_cmp, NULL);
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
/* Common among btree and extent ptrs */
const struct bch_extent_ptr *
@ -777,7 +696,7 @@ int bch2_btree_pick_ptr(struct bch_fs *c, const struct btree *b,
/* Extents */
static bool __bch2_cut_front(struct bpos where, struct bkey_s k)
bool __bch2_cut_front(struct bpos where, struct bkey_s k)
{
u64 len = 0;
@ -830,11 +749,6 @@ static bool __bch2_cut_front(struct bpos where, struct bkey_s k)
return true;
}
bool bch2_cut_front(struct bpos where, struct bkey_i *k)
{
return __bch2_cut_front(where, bkey_i_to_s(k));
}
bool bch2_cut_back(struct bpos where, struct bkey *k)
{
u64 len = 0;
@ -870,24 +784,6 @@ void bch2_key_resize(struct bkey *k,
k->size = new_size;
}
/*
* In extent_sort_fix_overlapping(), insert_fixup_extent(),
* extent_merge_inline() - we're modifying keys in place that are packed. To do
* that we have to unpack the key, modify the unpacked key - then this
* copies/repacks the unpacked to the original as necessary.
*/
static void extent_save(struct btree *b, struct bkey_packed *dst,
struct bkey *src)
{
struct bkey_format *f = &b->format;
struct bkey_i *dst_unpacked;
if ((dst_unpacked = packed_to_bkey(dst)))
dst_unpacked->k = *src;
else
BUG_ON(!bch2_bkey_pack_key(dst, src, f));
}
static bool extent_i_save(struct btree *b, struct bkey_packed *dst,
struct bkey_i *src)
{
@ -906,170 +802,6 @@ static bool extent_i_save(struct btree *b, struct bkey_packed *dst,
return true;
}
/*
* If keys compare equal, compare by pointer order:
*
* Necessary for sort_fix_overlapping() - if there are multiple keys that
* compare equal in different sets, we have to process them newest to oldest.
*/
#define extent_sort_cmp(h, l, r) \
({ \
struct bkey _ul = bkey_unpack_key(b, \
__btree_node_offset_to_key(b, (l).k)); \
struct bkey _ur = bkey_unpack_key(b, \
__btree_node_offset_to_key(b, (r).k)); \
\
bkey_cmp(bkey_start_pos(&_ul), \
bkey_start_pos(&_ur)) ?: (r).k - (l).k; \
})
static inline void extent_sort_sift(struct btree_node_iter_large *iter,
struct btree *b, size_t i)
{
heap_sift_down(iter, i, extent_sort_cmp, NULL);
}
static inline void extent_sort_next(struct btree_node_iter_large *iter,
struct btree *b,
struct btree_node_iter_set *i)
{
sort_key_next(iter, b, i);
heap_sift_down(iter, i - iter->data, extent_sort_cmp, NULL);
}
static void extent_sort_append(struct bch_fs *c,
struct btree *b,
struct btree_nr_keys *nr,
struct bkey_packed *start,
struct bkey_packed **prev,
struct bkey_packed *k)
{
struct bkey_format *f = &b->format;
BKEY_PADDED(k) tmp;
if (bkey_whiteout(k))
return;
bch2_bkey_unpack(b, &tmp.k, k);
if (*prev &&
bch2_extent_merge(c, b, (void *) *prev, &tmp.k))
return;
if (*prev) {
bch2_bkey_pack(*prev, (void *) *prev, f);
btree_keys_account_key_add(nr, 0, *prev);
*prev = bkey_next(*prev);
} else {
*prev = start;
}
bkey_copy(*prev, &tmp.k);
}
struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
struct bset *dst,
struct btree *b,
struct btree_node_iter_large *iter)
{
struct bkey_format *f = &b->format;
struct btree_node_iter_set *_l = iter->data, *_r;
struct bkey_packed *prev = NULL, *out, *lk, *rk;
struct bkey l_unpacked, r_unpacked;
struct bkey_s l, r;
struct btree_nr_keys nr;
memset(&nr, 0, sizeof(nr));
heap_resort(iter, extent_sort_cmp, NULL);
while (!bch2_btree_node_iter_large_end(iter)) {
lk = __btree_node_offset_to_key(b, _l->k);
if (iter->used == 1) {
extent_sort_append(c, b, &nr, dst->start, &prev, lk);
extent_sort_next(iter, b, _l);
continue;
}
_r = iter->data + 1;
if (iter->used > 2 &&
extent_sort_cmp(iter, _r[0], _r[1]) >= 0)
_r++;
rk = __btree_node_offset_to_key(b, _r->k);
l = __bkey_disassemble(b, lk, &l_unpacked);
r = __bkey_disassemble(b, rk, &r_unpacked);
/* If current key and next key don't overlap, just append */
if (bkey_cmp(l.k->p, bkey_start_pos(r.k)) <= 0) {
extent_sort_append(c, b, &nr, dst->start, &prev, lk);
extent_sort_next(iter, b, _l);
continue;
}
/* Skip 0 size keys */
if (!r.k->size) {
extent_sort_next(iter, b, _r);
continue;
}
/*
* overlap: keep the newer key and trim the older key so they
* don't overlap. comparing pointers tells us which one is
* newer, since the bsets are appended one after the other.
*/
/* can't happen because of comparison func */
BUG_ON(_l->k < _r->k &&
!bkey_cmp(bkey_start_pos(l.k), bkey_start_pos(r.k)));
if (_l->k > _r->k) {
/* l wins, trim r */
if (bkey_cmp(l.k->p, r.k->p) >= 0) {
sort_key_next(iter, b, _r);
} else {
__bch2_cut_front(l.k->p, r);
extent_save(b, rk, r.k);
}
extent_sort_sift(iter, b, _r - iter->data);
} else if (bkey_cmp(l.k->p, r.k->p) > 0) {
BKEY_PADDED(k) tmp;
/*
* r wins, but it overlaps in the middle of l - split l:
*/
bkey_reassemble(&tmp.k, l.s_c);
bch2_cut_back(bkey_start_pos(r.k), &tmp.k.k);
__bch2_cut_front(r.k->p, l);
extent_save(b, lk, l.k);
extent_sort_sift(iter, b, 0);
extent_sort_append(c, b, &nr, dst->start, &prev,
bkey_to_packed(&tmp.k));
} else {
bch2_cut_back(bkey_start_pos(r.k), l.k);
extent_save(b, lk, l.k);
}
}
if (prev) {
bch2_bkey_pack(prev, (void *) prev, f);
btree_keys_account_key_add(&nr, 0, prev);
out = bkey_next(prev);
} else {
out = dst->start;
}
dst->u64s = cpu_to_le16((u64 *) out - dst->_data);
return nr;
}
struct extent_insert_state {
struct btree_insert *trans;
struct btree_insert_entry *insert;

View File

@ -7,13 +7,8 @@
#include "extents_types.h"
struct bch_fs;
struct journal_res;
struct btree_node_iter;
struct btree_node_iter_large;
struct btree_insert;
struct btree_insert_entry;
struct bch_devs_mask;
union bch_extent_crc;
const char *bch2_btree_ptr_invalid(const struct bch_fs *, struct bkey_s_c);
void bch2_btree_ptr_debugcheck(struct bch_fs *, struct btree *,
@ -46,14 +41,6 @@ enum merge_result bch2_extent_merge(struct bch_fs *, struct btree *,
.is_extents = true, \
}
struct btree_nr_keys bch2_key_sort_fix_overlapping(struct bset *,
struct btree *,
struct btree_node_iter_large *);
struct btree_nr_keys bch2_extent_sort_fix_overlapping(struct bch_fs *c,
struct bset *,
struct btree *,
struct btree_node_iter_large *);
void bch2_mark_io_failure(struct bch_io_failures *,
struct extent_ptr_decoded *);
int bch2_btree_pick_ptr(struct bch_fs *, const struct btree *,
@ -506,10 +493,34 @@ do { \
} \
} while (0)
bool bch2_cut_front(struct bpos, struct bkey_i *);
bool __bch2_cut_front(struct bpos, struct bkey_s);
static inline bool bch2_cut_front(struct bpos where, struct bkey_i *k)
{
return __bch2_cut_front(where, bkey_i_to_s(k));
}
bool bch2_cut_back(struct bpos, struct bkey *);
void bch2_key_resize(struct bkey *, unsigned);
/*
* In extent_sort_fix_overlapping(), insert_fixup_extent(),
* extent_merge_inline() - we're modifying keys in place that are packed. To do
* that we have to unpack the key, modify the unpacked key - then this
* copies/repacks the unpacked to the original as necessary.
*/
static inline void extent_save(struct btree *b, struct bkey_packed *dst,
struct bkey *src)
{
struct bkey_format *f = &b->format;
struct bkey_i *dst_unpacked;
if ((dst_unpacked = packed_to_bkey(dst)))
dst_unpacked->k = *src;
else
BUG_ON(!bch2_bkey_pack_key(dst, src, f));
}
int bch2_check_range_allocated(struct bch_fs *, struct bpos, u64);
#endif /* _BCACHEFS_EXTENTS_H */

View File

@ -10,6 +10,7 @@
#include "bcachefs.h"
#include "alloc_background.h"
#include "alloc_foreground.h"
#include "bkey_sort.h"
#include "btree_cache.h"
#include "btree_gc.h"
#include "btree_update_interior.h"