2017-03-17 06:18:50 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include "bcachefs.h"
|
|
|
|
#include "btree_update.h"
|
|
|
|
#include "btree_update_interior.h"
|
|
|
|
#include "btree_io.h"
|
|
|
|
#include "btree_iter.h"
|
|
|
|
#include "btree_locking.h"
|
2018-11-05 07:31:48 +00:00
|
|
|
#include "buckets.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "debug.h"
|
2018-07-17 17:50:15 +00:00
|
|
|
#include "error.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "extents.h"
|
|
|
|
#include "journal.h"
|
|
|
|
#include "journal_reclaim.h"
|
|
|
|
#include "keylist.h"
|
2018-11-07 22:48:32 +00:00
|
|
|
#include "replicas.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "trace.h"
|
|
|
|
|
|
|
|
#include <linux/sort.h>
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static bool btree_trans_relock(struct btree_trans *);
|
|
|
|
static void btree_trans_unlock(struct btree_trans *);
|
2019-02-19 22:56:21 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Inserting into a given leaf node (last stage of insert): */
|
|
|
|
|
|
|
|
/* Handle overwrites and do insert, for non extents: */
|
|
|
|
bool bch2_btree_bset_insert_key(struct btree_iter *iter,
|
|
|
|
struct btree *b,
|
|
|
|
struct btree_node_iter *node_iter,
|
|
|
|
struct bkey_i *insert)
|
|
|
|
{
|
|
|
|
const struct bkey_format *f = &b->format;
|
|
|
|
struct bkey_packed *k;
|
|
|
|
unsigned clobber_u64s;
|
|
|
|
|
|
|
|
EBUG_ON(btree_node_just_written(b));
|
|
|
|
EBUG_ON(bset_written(b, btree_bset_last(b)));
|
|
|
|
EBUG_ON(bkey_deleted(&insert->k) && bkey_val_u64s(&insert->k));
|
|
|
|
EBUG_ON(bkey_cmp(bkey_start_pos(&insert->k), b->data->min_key) < 0 ||
|
|
|
|
bkey_cmp(insert->k.p, b->data->max_key) > 0);
|
|
|
|
|
|
|
|
k = bch2_btree_node_iter_peek_all(node_iter, b);
|
|
|
|
if (k && !bkey_cmp_packed(b, k, &insert->k)) {
|
|
|
|
BUG_ON(bkey_whiteout(k));
|
|
|
|
|
2018-08-06 02:23:44 +00:00
|
|
|
if (!bkey_written(b, k) &&
|
2017-03-17 06:18:50 +00:00
|
|
|
bkey_val_u64s(&insert->k) == bkeyp_val_u64s(f, k) &&
|
|
|
|
!bkey_whiteout(&insert->k)) {
|
|
|
|
k->type = insert->k.type;
|
|
|
|
memcpy_u64s(bkeyp_val(f, k), &insert->v,
|
|
|
|
bkey_val_u64s(&insert->k));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
insert->k.needs_whiteout = k->needs_whiteout;
|
|
|
|
|
2018-08-11 23:12:05 +00:00
|
|
|
btree_account_key_drop(b, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-08-11 23:12:05 +00:00
|
|
|
if (k >= btree_bset_last(b)->start) {
|
2017-03-17 06:18:50 +00:00
|
|
|
clobber_u64s = k->u64s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're deleting, and the key we're deleting doesn't
|
|
|
|
* need a whiteout (it wasn't overwriting a key that had
|
|
|
|
* been written to disk) - just delete it:
|
|
|
|
*/
|
|
|
|
if (bkey_whiteout(&insert->k) && !k->needs_whiteout) {
|
|
|
|
bch2_bset_delete(b, k, clobber_u64s);
|
2018-08-11 23:12:05 +00:00
|
|
|
bch2_btree_node_iter_fix(iter, b, node_iter,
|
2016-07-22 03:05:06 +00:00
|
|
|
k, clobber_u64s, 0);
|
|
|
|
bch2_btree_iter_verify(iter, b);
|
2017-03-17 06:18:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto overwrite;
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
k->type = KEY_TYPE_deleted;
|
2018-08-11 23:12:05 +00:00
|
|
|
bch2_btree_node_iter_fix(iter, b, node_iter, k,
|
2016-07-22 03:05:06 +00:00
|
|
|
k->u64s, k->u64s);
|
|
|
|
bch2_btree_iter_verify(iter, b);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (bkey_whiteout(&insert->k)) {
|
2018-08-06 02:23:44 +00:00
|
|
|
reserve_whiteout(b, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
k->needs_whiteout = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Deleting, but the key to delete wasn't found - nothing to do:
|
|
|
|
*/
|
|
|
|
if (bkey_whiteout(&insert->k))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
insert->k.needs_whiteout = false;
|
|
|
|
}
|
|
|
|
|
2018-08-11 23:12:05 +00:00
|
|
|
k = bch2_btree_node_iter_bset_pos(node_iter, b, bset_tree_last(b));
|
2017-03-17 06:18:50 +00:00
|
|
|
clobber_u64s = 0;
|
|
|
|
overwrite:
|
|
|
|
bch2_bset_insert(b, node_iter, k, insert, clobber_u64s);
|
|
|
|
if (k->u64s != clobber_u64s || bkey_whiteout(&insert->k))
|
2018-08-11 23:12:05 +00:00
|
|
|
bch2_btree_node_iter_fix(iter, b, node_iter, k,
|
2016-07-22 03:05:06 +00:00
|
|
|
clobber_u64s, k->u64s);
|
|
|
|
bch2_btree_iter_verify(iter, b);
|
2017-03-17 06:18:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __btree_node_flush(struct journal *j, struct journal_entry_pin *pin,
|
|
|
|
unsigned i, u64 seq)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = container_of(j, struct bch_fs, journal);
|
|
|
|
struct btree_write *w = container_of(pin, struct btree_write, journal);
|
|
|
|
struct btree *b = container_of(w, struct btree, writes[i]);
|
|
|
|
|
|
|
|
btree_node_lock_type(c, b, SIX_LOCK_read);
|
|
|
|
bch2_btree_node_write_cond(c, b,
|
2018-07-17 16:19:14 +00:00
|
|
|
(btree_current_write(b) == w && w->journal.seq == seq));
|
2017-03-17 06:18:50 +00:00
|
|
|
six_unlock_read(&b->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btree_node_flush0(struct journal *j, struct journal_entry_pin *pin, u64 seq)
|
|
|
|
{
|
|
|
|
return __btree_node_flush(j, pin, 0, seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btree_node_flush1(struct journal *j, struct journal_entry_pin *pin, u64 seq)
|
|
|
|
{
|
|
|
|
return __btree_node_flush(j, pin, 1, seq);
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static inline void __btree_journal_key(struct btree_trans *trans,
|
2018-07-17 17:50:15 +00:00
|
|
|
enum btree_id btree_id,
|
|
|
|
struct bkey_i *insert)
|
|
|
|
{
|
|
|
|
struct journal *j = &trans->c->journal;
|
|
|
|
u64 seq = trans->journal_res.seq;
|
|
|
|
bool needs_whiteout = insert->k.needs_whiteout;
|
|
|
|
|
|
|
|
/* ick */
|
|
|
|
insert->k.needs_whiteout = false;
|
|
|
|
bch2_journal_add_keys(j, &trans->journal_res,
|
|
|
|
btree_id, insert);
|
|
|
|
insert->k.needs_whiteout = needs_whiteout;
|
|
|
|
|
|
|
|
bch2_journal_set_has_inode(j, &trans->journal_res,
|
|
|
|
insert->k.p.inode);
|
|
|
|
|
|
|
|
if (trans->journal_seq)
|
|
|
|
*trans->journal_seq = seq;
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
void bch2_btree_journal_key(struct btree_trans *trans,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree_iter *iter,
|
|
|
|
struct bkey_i *insert)
|
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct journal *j = &c->journal;
|
|
|
|
struct btree *b = iter->l[0].b;
|
|
|
|
struct btree_write *w = btree_current_write(b);
|
|
|
|
|
|
|
|
EBUG_ON(iter->level || b->level);
|
|
|
|
EBUG_ON(trans->journal_res.ref !=
|
|
|
|
!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY));
|
|
|
|
|
|
|
|
if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
|
2018-07-17 17:50:15 +00:00
|
|
|
__btree_journal_key(trans, iter->btree_id, insert);
|
|
|
|
btree_bset_last(b)->journal_seq =
|
|
|
|
cpu_to_le64(trans->journal_res.seq);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!journal_pin_active(&w->journal))) {
|
|
|
|
u64 seq = likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))
|
|
|
|
? trans->journal_res.seq
|
|
|
|
: j->replay_journal_seq;
|
|
|
|
|
|
|
|
bch2_journal_pin_add(j, seq, &w->journal,
|
|
|
|
btree_node_write_idx(b) == 0
|
|
|
|
? btree_node_flush0
|
|
|
|
: btree_node_flush1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(!btree_node_dirty(b)))
|
|
|
|
set_btree_node_dirty(b);
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static void bch2_insert_fixup_key(struct btree_trans *trans,
|
2019-03-16 18:27:40 +00:00
|
|
|
struct btree_insert_entry *insert)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct btree_iter *iter = insert->iter;
|
|
|
|
struct btree_iter_level *l = &iter->l[0];
|
|
|
|
|
|
|
|
EBUG_ON(iter->level);
|
|
|
|
EBUG_ON(insert->k->k.u64s >
|
|
|
|
bch_btree_keys_u64s_remaining(trans->c, l->b));
|
|
|
|
|
|
|
|
if (bch2_btree_bset_insert_key(iter, l->b, &l->iter,
|
|
|
|
insert->k))
|
|
|
|
bch2_btree_journal_key(trans, iter, insert->k);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* btree_insert_key - insert a key one key into a leaf node
|
|
|
|
*/
|
2019-03-14 02:44:04 +00:00
|
|
|
static void btree_insert_key_leaf(struct btree_trans *trans,
|
2019-03-16 18:27:40 +00:00
|
|
|
struct btree_insert_entry *insert)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree_iter *iter = insert->iter;
|
|
|
|
struct btree *b = iter->l[0].b;
|
|
|
|
int old_u64s = le16_to_cpu(btree_bset_last(b)->u64s);
|
|
|
|
int old_live_u64s = b->nr.live_u64s;
|
|
|
|
int live_u64s_added, u64s_added;
|
|
|
|
|
2018-11-05 07:31:48 +00:00
|
|
|
bch2_mark_update(trans, insert);
|
|
|
|
|
2019-03-16 18:27:40 +00:00
|
|
|
if (!btree_node_is_extents(b))
|
|
|
|
bch2_insert_fixup_key(trans, insert);
|
|
|
|
else
|
|
|
|
bch2_insert_fixup_extent(trans, insert);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
live_u64s_added = (int) b->nr.live_u64s - old_live_u64s;
|
|
|
|
u64s_added = (int) le16_to_cpu(btree_bset_last(b)->u64s) - old_u64s;
|
|
|
|
|
|
|
|
if (b->sib_u64s[0] != U16_MAX && live_u64s_added < 0)
|
|
|
|
b->sib_u64s[0] = max(0, (int) b->sib_u64s[0] + live_u64s_added);
|
|
|
|
if (b->sib_u64s[1] != U16_MAX && live_u64s_added < 0)
|
|
|
|
b->sib_u64s[1] = max(0, (int) b->sib_u64s[1] + live_u64s_added);
|
|
|
|
|
|
|
|
if (u64s_added > live_u64s_added &&
|
|
|
|
bch2_maybe_compact_whiteouts(c, b))
|
|
|
|
bch2_btree_iter_reinit_node(iter, b);
|
|
|
|
|
|
|
|
trace_btree_insert_key(c, b, insert->k);
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
/* Deferred btree updates: */
|
|
|
|
|
|
|
|
static void deferred_update_flush(struct journal *j,
|
2019-02-19 22:56:21 +00:00
|
|
|
struct journal_entry_pin *pin,
|
|
|
|
u64 seq)
|
2018-07-17 17:50:15 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = container_of(j, struct bch_fs, journal);
|
|
|
|
struct deferred_update *d =
|
|
|
|
container_of(pin, struct deferred_update, journal);
|
2019-02-19 22:56:21 +00:00
|
|
|
struct journal_preres res = { 0 };
|
2018-07-17 17:50:15 +00:00
|
|
|
u64 tmp[32];
|
|
|
|
struct bkey_i *k = (void *) tmp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (d->allocated_u64s > ARRAY_SIZE(tmp)) {
|
|
|
|
k = kmalloc(d->allocated_u64s * sizeof(u64), GFP_NOFS);
|
|
|
|
|
|
|
|
BUG_ON(!k); /* XXX */
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&d->lock);
|
2019-02-19 22:56:21 +00:00
|
|
|
if (d->dirty) {
|
|
|
|
BUG_ON(jset_u64s(d->k.k.u64s) > d->res.u64s);
|
|
|
|
|
|
|
|
swap(res, d->res);
|
2018-07-17 17:50:15 +00:00
|
|
|
|
|
|
|
BUG_ON(d->k.k.u64s > d->allocated_u64s);
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
bkey_copy(k, &d->k);
|
|
|
|
d->dirty = false;
|
2018-07-17 17:50:15 +00:00
|
|
|
spin_unlock(&d->lock);
|
|
|
|
|
|
|
|
ret = bch2_btree_insert(c, d->btree_id, k, NULL, NULL,
|
2019-02-19 22:56:21 +00:00
|
|
|
BTREE_INSERT_NOFAIL|
|
|
|
|
BTREE_INSERT_USE_RESERVE|
|
|
|
|
BTREE_INSERT_JOURNAL_RESERVED);
|
2018-07-17 17:50:15 +00:00
|
|
|
bch2_fs_fatal_err_on(ret && !bch2_journal_error(j),
|
2019-02-19 22:56:21 +00:00
|
|
|
c, "error flushing deferred btree update: %i", ret);
|
2018-07-17 17:50:15 +00:00
|
|
|
|
|
|
|
spin_lock(&d->lock);
|
|
|
|
}
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
if (!d->dirty)
|
2018-07-17 17:50:15 +00:00
|
|
|
bch2_journal_pin_drop(j, &d->journal);
|
|
|
|
spin_unlock(&d->lock);
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
bch2_journal_preres_put(j, &res);
|
2018-07-17 17:50:15 +00:00
|
|
|
if (k != (void *) tmp)
|
|
|
|
kfree(k);
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static void btree_insert_key_deferred(struct btree_trans *trans,
|
2019-03-16 18:27:40 +00:00
|
|
|
struct btree_insert_entry *insert)
|
2018-07-17 17:50:15 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct journal *j = &c->journal;
|
|
|
|
struct deferred_update *d = insert->d;
|
2019-02-19 22:56:21 +00:00
|
|
|
int difference;
|
2018-07-17 17:50:15 +00:00
|
|
|
|
|
|
|
BUG_ON(trans->flags & BTREE_INSERT_JOURNAL_REPLAY);
|
|
|
|
BUG_ON(insert->k->u64s > d->allocated_u64s);
|
|
|
|
|
|
|
|
__btree_journal_key(trans, d->btree_id, insert->k);
|
|
|
|
|
|
|
|
spin_lock(&d->lock);
|
2019-02-19 22:56:21 +00:00
|
|
|
BUG_ON(jset_u64s(insert->k->u64s) >
|
|
|
|
trans->journal_preres.u64s);
|
|
|
|
|
|
|
|
difference = jset_u64s(insert->k->u64s) - d->res.u64s;
|
|
|
|
if (difference > 0) {
|
|
|
|
trans->journal_preres.u64s -= difference;
|
|
|
|
d->res.u64s += difference;
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
bkey_copy(&d->k, insert->k);
|
2019-02-19 22:56:21 +00:00
|
|
|
d->dirty = true;
|
2018-07-17 17:50:15 +00:00
|
|
|
|
|
|
|
bch2_journal_pin_update(j, trans->journal_res.seq, &d->journal,
|
|
|
|
deferred_update_flush);
|
2019-02-19 22:56:21 +00:00
|
|
|
spin_unlock(&d->lock);
|
2018-07-17 17:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bch2_deferred_update_free(struct bch_fs *c,
|
|
|
|
struct deferred_update *d)
|
|
|
|
{
|
|
|
|
deferred_update_flush(&c->journal, &d->journal, 0);
|
|
|
|
|
|
|
|
BUG_ON(journal_pin_active(&d->journal));
|
|
|
|
|
|
|
|
bch2_journal_pin_flush(&c->journal, &d->journal);
|
|
|
|
kfree(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct deferred_update *
|
|
|
|
bch2_deferred_update_alloc(struct bch_fs *c,
|
|
|
|
enum btree_id btree_id,
|
|
|
|
unsigned u64s)
|
|
|
|
{
|
|
|
|
struct deferred_update *d;
|
|
|
|
|
|
|
|
BUG_ON(u64s > U8_MAX);
|
|
|
|
|
|
|
|
d = kmalloc(offsetof(struct deferred_update, k) +
|
|
|
|
u64s * sizeof(u64), GFP_NOFS);
|
|
|
|
BUG_ON(!d);
|
|
|
|
|
|
|
|
memset(d, 0, offsetof(struct deferred_update, k));
|
|
|
|
|
|
|
|
spin_lock_init(&d->lock);
|
|
|
|
d->allocated_u64s = u64s;
|
|
|
|
d->btree_id = btree_id;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* struct btree_insert operations: */
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We sort transaction entries so that if multiple iterators point to the same
|
|
|
|
* leaf node they'll be adjacent:
|
|
|
|
*/
|
2019-03-14 02:44:04 +00:00
|
|
|
static bool same_leaf_as_prev(struct btree_trans *trans,
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree_insert_entry *i)
|
|
|
|
{
|
2019-03-14 02:44:04 +00:00
|
|
|
return i != trans->updates &&
|
2018-07-17 17:50:15 +00:00
|
|
|
!i->deferred &&
|
2017-03-17 06:18:50 +00:00
|
|
|
i[0].iter->l[0].b == i[-1].iter->l[0].b;
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
#define __trans_next_entry(_trans, _i, _filter) \
|
|
|
|
({ \
|
2019-03-14 02:44:04 +00:00
|
|
|
while ((_i) < (_trans)->updates + (_trans->nr_updates) && !(_filter))\
|
2018-07-17 17:50:15 +00:00
|
|
|
(_i)++; \
|
|
|
|
\
|
2019-03-14 02:44:04 +00:00
|
|
|
(_i) < (_trans)->updates + (_trans->nr_updates); \
|
2018-07-17 17:50:15 +00:00
|
|
|
})
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
#define __trans_for_each_entry(_trans, _i, _filter) \
|
2019-03-14 02:44:04 +00:00
|
|
|
for ((_i) = (_trans)->updates; \
|
2018-07-17 17:50:15 +00:00
|
|
|
__trans_next_entry(_trans, _i, _filter); \
|
|
|
|
(_i)++)
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
#define trans_for_each_entry(trans, i) \
|
|
|
|
__trans_for_each_entry(trans, i, true)
|
|
|
|
|
|
|
|
#define trans_for_each_iter(trans, i) \
|
|
|
|
__trans_for_each_entry(trans, i, !(i)->deferred)
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
#define trans_for_each_leaf(trans, i) \
|
2018-07-17 17:50:15 +00:00
|
|
|
__trans_for_each_entry(trans, i, !(i)->deferred && \
|
|
|
|
!same_leaf_as_prev(trans, i))
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
inline void bch2_btree_node_lock_for_insert(struct bch_fs *c, struct btree *b,
|
|
|
|
struct btree_iter *iter)
|
|
|
|
{
|
|
|
|
bch2_btree_node_lock_write(b, iter);
|
|
|
|
|
|
|
|
if (btree_node_just_written(b) &&
|
|
|
|
bch2_btree_post_write_cleanup(c, b))
|
|
|
|
bch2_btree_iter_reinit_node(iter, b);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the last bset has been written, or if it's gotten too big - start
|
|
|
|
* a new bset to insert into:
|
|
|
|
*/
|
|
|
|
if (want_new_bset(c, b))
|
|
|
|
bch2_btree_init_next(c, b, iter);
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static void multi_lock_write(struct bch_fs *c, struct btree_trans *trans)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct btree_insert_entry *i;
|
|
|
|
|
|
|
|
trans_for_each_leaf(trans, i)
|
|
|
|
bch2_btree_node_lock_for_insert(c, i->iter->l[0].b, i->iter);
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static void multi_unlock_write(struct btree_trans *trans)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct btree_insert_entry *i;
|
|
|
|
|
|
|
|
trans_for_each_leaf(trans, i)
|
|
|
|
bch2_btree_node_unlock_write(i->iter->l[0].b, i->iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int btree_trans_cmp(struct btree_insert_entry l,
|
|
|
|
struct btree_insert_entry r)
|
|
|
|
{
|
2018-07-17 17:50:15 +00:00
|
|
|
return (l.deferred > r.deferred) - (l.deferred < r.deferred) ?:
|
|
|
|
btree_iter_cmp(l.iter, r.iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static bool btree_trans_relock(struct btree_trans *trans)
|
2019-02-26 22:13:46 +00:00
|
|
|
{
|
|
|
|
struct btree_insert_entry *i;
|
|
|
|
|
|
|
|
trans_for_each_iter(trans, i)
|
|
|
|
return bch2_btree_iter_relock(i->iter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static void btree_trans_unlock(struct btree_trans *trans)
|
2019-02-26 22:13:46 +00:00
|
|
|
{
|
|
|
|
struct btree_insert_entry *i;
|
|
|
|
|
|
|
|
trans_for_each_iter(trans, i) {
|
|
|
|
bch2_btree_iter_unlock(i->iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Normal update interface: */
|
|
|
|
|
2018-08-03 23:41:44 +00:00
|
|
|
static enum btree_insert_ret
|
2019-03-14 02:44:04 +00:00
|
|
|
btree_key_can_insert(struct btree_trans *trans,
|
2018-11-07 22:48:32 +00:00
|
|
|
struct btree_insert_entry *insert,
|
|
|
|
unsigned *u64s)
|
2018-08-03 23:41:44 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree *b = insert->iter->l[0].b;
|
|
|
|
static enum btree_insert_ret ret;
|
|
|
|
|
|
|
|
if (unlikely(btree_node_fake(b)))
|
|
|
|
return BTREE_INSERT_BTREE_NODE_FULL;
|
|
|
|
|
2018-11-07 22:48:32 +00:00
|
|
|
if (!bch2_bkey_replicas_marked(c,
|
|
|
|
bkey_i_to_s_c(insert->k),
|
|
|
|
true))
|
|
|
|
return BTREE_INSERT_NEED_MARK_REPLICAS;
|
|
|
|
|
2018-08-03 23:41:44 +00:00
|
|
|
ret = !btree_node_is_extents(b)
|
|
|
|
? BTREE_INSERT_OK
|
|
|
|
: bch2_extent_can_insert(trans, insert, u64s);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (*u64s > bch_btree_keys_u64s_remaining(c, b))
|
|
|
|
return BTREE_INSERT_BTREE_NODE_FULL;
|
|
|
|
|
|
|
|
return BTREE_INSERT_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
static inline void do_btree_insert_one(struct btree_trans *trans,
|
2019-03-16 18:27:40 +00:00
|
|
|
struct btree_insert_entry *insert)
|
2018-07-17 17:50:15 +00:00
|
|
|
{
|
2019-03-16 18:27:40 +00:00
|
|
|
if (likely(!insert->deferred))
|
|
|
|
btree_insert_key_leaf(trans, insert);
|
|
|
|
else
|
|
|
|
btree_insert_key_deferred(trans, insert);
|
2018-07-17 17:50:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/*
|
|
|
|
* Get journal reservation, take write locks, and attempt to do btree update(s):
|
|
|
|
*/
|
2019-03-14 02:44:04 +00:00
|
|
|
static inline int do_btree_insert_at(struct btree_trans *trans,
|
2018-11-07 22:48:32 +00:00
|
|
|
struct btree_insert_entry **stopped_at)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree_insert_entry *i;
|
2018-11-23 10:19:25 +00:00
|
|
|
struct btree_iter *linked;
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned u64s;
|
|
|
|
int ret;
|
2019-02-26 22:13:46 +00:00
|
|
|
retry:
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i)
|
2017-03-17 06:18:50 +00:00
|
|
|
BUG_ON(i->iter->uptodate >= BTREE_ITER_NEED_RELOCK);
|
|
|
|
|
|
|
|
memset(&trans->journal_res, 0, sizeof(trans->journal_res));
|
|
|
|
|
|
|
|
multi_lock_write(c, trans);
|
|
|
|
|
|
|
|
if (race_fault()) {
|
|
|
|
ret = -EINTR;
|
2018-07-13 03:30:45 +00:00
|
|
|
trans_restart(" (race)");
|
2017-03-17 06:18:50 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-08-03 23:41:44 +00:00
|
|
|
/*
|
|
|
|
* Check if the insert will fit in the leaf node with the write lock
|
|
|
|
* held, otherwise another thread could write the node changing the
|
|
|
|
* amount of space available:
|
|
|
|
*/
|
2017-03-17 06:18:50 +00:00
|
|
|
u64s = 0;
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Multiple inserts might go to same leaf: */
|
|
|
|
if (!same_leaf_as_prev(trans, i))
|
|
|
|
u64s = 0;
|
|
|
|
|
2018-08-08 23:53:30 +00:00
|
|
|
u64s += i->k->k.u64s;
|
2018-11-07 22:48:32 +00:00
|
|
|
ret = btree_key_can_insert(trans, i, &u64s);
|
|
|
|
if (ret) {
|
|
|
|
*stopped_at = i;
|
2018-08-03 23:41:44 +00:00
|
|
|
goto out;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:13:46 +00:00
|
|
|
if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
|
2019-02-19 22:56:21 +00:00
|
|
|
unsigned flags = (trans->flags & BTREE_INSERT_JOURNAL_RESERVED)
|
|
|
|
? JOURNAL_RES_GET_RESERVED : 0;
|
|
|
|
|
2019-02-26 22:13:46 +00:00
|
|
|
u64s = 0;
|
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
u64s += jset_u64s(i->k->k.u64s);
|
|
|
|
|
|
|
|
ret = bch2_journal_res_get(&c->journal,
|
|
|
|
&trans->journal_res, u64s,
|
2019-02-19 22:56:21 +00:00
|
|
|
flags|JOURNAL_RES_GET_NONBLOCK);
|
2019-02-26 22:13:46 +00:00
|
|
|
if (likely(!ret))
|
|
|
|
goto got_journal_res;
|
|
|
|
if (ret != -EAGAIN)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
multi_unlock_write(trans);
|
|
|
|
btree_trans_unlock(trans);
|
|
|
|
|
|
|
|
ret = bch2_journal_res_get(&c->journal,
|
|
|
|
&trans->journal_res, u64s,
|
2019-02-19 22:56:21 +00:00
|
|
|
flags|JOURNAL_RES_GET_CHECK);
|
2019-02-26 22:13:46 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!btree_trans_relock(trans)) {
|
|
|
|
trans_restart(" (iter relock after journal res get blocked)");
|
|
|
|
return -EINTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
got_journal_res:
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)) {
|
|
|
|
if (journal_seq_verify(c))
|
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
i->k->k.version.lo = trans->journal_res.seq;
|
|
|
|
else if (inject_invalid_keys(c))
|
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
i->k->k.version = MAX_VERSION;
|
|
|
|
}
|
|
|
|
|
2018-11-23 10:19:25 +00:00
|
|
|
if (trans->flags & BTREE_INSERT_NOUNLOCK) {
|
|
|
|
/*
|
|
|
|
* linked iterators that weren't being updated may or may not
|
|
|
|
* have been traversed/locked, depending on what the caller was
|
|
|
|
* doing:
|
|
|
|
*/
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
|
|
|
for_each_btree_iter(i->iter, linked)
|
|
|
|
if (linked->uptodate < BTREE_ITER_NEED_RELOCK)
|
|
|
|
linked->flags |= BTREE_ITER_NOUNLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-23 10:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-16 18:27:40 +00:00
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
do_btree_insert_one(trans, i);
|
2017-03-17 06:18:50 +00:00
|
|
|
out:
|
2019-02-19 22:56:21 +00:00
|
|
|
BUG_ON(ret &&
|
|
|
|
(trans->flags & BTREE_INSERT_JOURNAL_RESERVED) &&
|
|
|
|
trans->journal_res.ref);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
multi_unlock_write(trans);
|
|
|
|
bch2_journal_res_put(&c->journal, &trans->journal_res);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void btree_insert_entry_checks(struct bch_fs *c,
|
|
|
|
struct btree_insert_entry *i)
|
|
|
|
{
|
2018-07-17 17:50:15 +00:00
|
|
|
enum btree_id btree_id = !i->deferred
|
|
|
|
? i->iter->btree_id
|
|
|
|
: i->d->btree_id;
|
|
|
|
|
|
|
|
if (!i->deferred) {
|
|
|
|
BUG_ON(i->iter->level);
|
|
|
|
BUG_ON(bkey_cmp(bkey_start_pos(&i->k->k), i->iter->pos));
|
2019-03-16 18:27:40 +00:00
|
|
|
EBUG_ON((i->iter->flags & BTREE_ITER_IS_EXTENTS) &&
|
|
|
|
!bch2_extent_is_atomic(i->k, i->iter));
|
2018-07-17 17:50:15 +00:00
|
|
|
|
|
|
|
bch2_btree_iter_verify_locks(i->iter);
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
BUG_ON(debug_check_bkeys(c) &&
|
|
|
|
!bkey_deleted(&i->k->k) &&
|
2018-07-17 17:50:15 +00:00
|
|
|
bch2_bkey_invalid(c, bkey_i_to_s_c(i->k), btree_id));
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __bch_btree_insert_at - insert keys at given iterator positions
|
|
|
|
*
|
|
|
|
* This is main entry point for btree updates.
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
* -EINTR: locking changed, this function should be called again. Only returned
|
|
|
|
* if passed BTREE_INSERT_ATOMIC.
|
|
|
|
* -EROFS: filesystem read only
|
|
|
|
* -EIO: journal or btree node IO error
|
|
|
|
*/
|
2019-03-14 02:44:04 +00:00
|
|
|
static int __bch2_btree_insert_at(struct btree_trans *trans)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bch_fs *c = trans->c;
|
|
|
|
struct btree_insert_entry *i;
|
2018-11-07 22:48:32 +00:00
|
|
|
struct btree_iter *linked;
|
2019-02-19 22:56:21 +00:00
|
|
|
unsigned flags, u64s = 0;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
BUG_ON(!trans->nr_updates);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/* for the sake of sanity: */
|
2019-03-14 02:44:04 +00:00
|
|
|
BUG_ON(trans->nr_updates > 1 && !(trans->flags & BTREE_INSERT_ATOMIC));
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-02-12 00:04:40 +00:00
|
|
|
if (trans->flags & BTREE_INSERT_GC_LOCK_HELD)
|
|
|
|
lockdep_assert_held(&c->gc_lock);
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
memset(&trans->journal_preres, 0, sizeof(trans->journal_preres));
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
bubble_sort(trans->updates, trans->nr_updates, btree_trans_cmp);
|
2018-07-17 17:50:15 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
btree_insert_entry_checks(c, i);
|
|
|
|
|
2019-02-19 22:56:21 +00:00
|
|
|
trans_for_each_entry(trans, i)
|
|
|
|
if (i->deferred)
|
|
|
|
u64s += jset_u64s(i->k->k.u64s);
|
|
|
|
|
|
|
|
if (u64s) {
|
|
|
|
ret = bch2_journal_preres_get(&c->journal,
|
|
|
|
&trans->journal_preres, u64s,
|
|
|
|
JOURNAL_RES_GET_NONBLOCK);
|
|
|
|
if (!ret)
|
|
|
|
goto got_journal_preres;
|
|
|
|
if (ret != -EAGAIN)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
btree_trans_unlock(trans);
|
|
|
|
ret = bch2_journal_preres_get(&c->journal,
|
|
|
|
&trans->journal_preres, u64s, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!btree_trans_relock(trans)) {
|
|
|
|
trans_restart(" (iter relock after journal preres get blocked)");
|
|
|
|
bch2_journal_preres_put(&c->journal, &trans->journal_preres);
|
|
|
|
return -EINTR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
got_journal_preres:
|
2018-11-19 06:31:41 +00:00
|
|
|
if (unlikely(!(trans->flags & BTREE_INSERT_NOCHECK_RW) &&
|
|
|
|
!percpu_ref_tryget(&c->writes)))
|
2017-03-17 06:18:50 +00:00
|
|
|
return -EROFS;
|
|
|
|
retry:
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
2018-07-13 03:30:45 +00:00
|
|
|
unsigned old_locks_want = i->iter->locks_want;
|
|
|
|
unsigned old_uptodate = i->iter->uptodate;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
if (!bch2_btree_iter_upgrade(i->iter, 1, true)) {
|
2018-07-13 03:30:45 +00:00
|
|
|
trans_restart(" (failed upgrade, locks_want %u uptodate %u)",
|
|
|
|
old_locks_want, old_uptodate);
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = -EINTR;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i->iter->flags & BTREE_ITER_ERROR) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 22:48:32 +00:00
|
|
|
ret = do_btree_insert_at(trans, &i);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (unlikely(ret))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
trans_for_each_leaf(trans, i)
|
|
|
|
bch2_foreground_maybe_merge(c, i->iter, 0, trans->flags);
|
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i)
|
2017-03-17 06:18:50 +00:00
|
|
|
bch2_btree_iter_downgrade(i->iter);
|
|
|
|
out:
|
2019-02-19 22:56:21 +00:00
|
|
|
bch2_journal_preres_put(&c->journal, &trans->journal_preres);
|
|
|
|
|
2018-11-19 06:31:41 +00:00
|
|
|
if (unlikely(!(trans->flags & BTREE_INSERT_NOCHECK_RW)))
|
|
|
|
percpu_ref_put(&c->writes);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-11-23 10:19:25 +00:00
|
|
|
/* make sure we didn't drop or screw up locks: */
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
|
|
|
bch2_btree_iter_verify_locks(i->iter);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-23 10:19:25 +00:00
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
|
|
|
for_each_btree_iter(i->iter, linked)
|
|
|
|
linked->flags &= ~BTREE_ITER_NOUNLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
BUG_ON(!(trans->flags & BTREE_INSERT_ATOMIC) && ret == -EINTR);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
flags = trans->flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BTREE_INSERT_NOUNLOCK means don't unlock _after_ successful btree
|
|
|
|
* update; if we haven't done anything yet it doesn't apply
|
|
|
|
*/
|
2019-03-11 20:25:42 +00:00
|
|
|
flags &= ~BTREE_INSERT_NOUNLOCK;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-11-07 22:48:32 +00:00
|
|
|
switch (ret) {
|
|
|
|
case BTREE_INSERT_BTREE_NODE_FULL:
|
|
|
|
ret = bch2_btree_split_leaf(c, i->iter, flags);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if the split succeeded without dropping locks the insert will
|
|
|
|
* still be atomic (in the BTREE_INSERT_ATOMIC sense, what the
|
|
|
|
* caller peeked() and is overwriting won't have changed)
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* XXX:
|
|
|
|
* split -> btree node merging (of parent node) might still drop
|
|
|
|
* locks when we're not passing it BTREE_INSERT_NOUNLOCK
|
2019-03-11 20:25:42 +00:00
|
|
|
*
|
|
|
|
* we don't want to pass BTREE_INSERT_NOUNLOCK to split as that
|
|
|
|
* will inhibit merging - but we don't have a reliable way yet
|
|
|
|
* (do we?) of checking if we dropped locks in this path
|
2017-03-17 06:18:50 +00:00
|
|
|
*/
|
2019-03-11 20:25:42 +00:00
|
|
|
if (!ret)
|
2017-03-17 06:18:50 +00:00
|
|
|
goto retry;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* don't care if we got ENOSPC because we told split it
|
|
|
|
* couldn't block:
|
|
|
|
*/
|
2018-07-13 03:30:45 +00:00
|
|
|
if (!ret || (flags & BTREE_INSERT_NOUNLOCK)) {
|
|
|
|
trans_restart(" (split)");
|
2017-03-17 06:18:50 +00:00
|
|
|
ret = -EINTR;
|
2018-07-13 03:30:45 +00:00
|
|
|
}
|
2018-11-07 22:48:32 +00:00
|
|
|
break;
|
|
|
|
case BTREE_INSERT_ENOSPC:
|
|
|
|
ret = -ENOSPC;
|
|
|
|
break;
|
|
|
|
case BTREE_INSERT_NEED_MARK_REPLICAS:
|
|
|
|
if (flags & BTREE_INSERT_NOUNLOCK) {
|
|
|
|
ret = -EINTR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
bch2_trans_unlock(trans);
|
2019-03-15 23:34:16 +00:00
|
|
|
ret = -EINTR;
|
|
|
|
|
|
|
|
trans_for_each_iter(trans, i) {
|
|
|
|
int ret2 = bch2_mark_bkey_replicas(c, bkey_i_to_s_c(i->k));
|
|
|
|
if (ret2)
|
|
|
|
ret = ret2;
|
|
|
|
}
|
2018-11-07 22:48:32 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG_ON(ret >= 0);
|
|
|
|
break;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == -EINTR) {
|
2018-07-13 03:30:45 +00:00
|
|
|
if (flags & BTREE_INSERT_NOUNLOCK) {
|
|
|
|
trans_restart(" (can't unlock)");
|
2017-03-17 06:18:50 +00:00
|
|
|
goto out;
|
2018-07-13 03:30:45 +00:00
|
|
|
}
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2018-07-17 17:50:15 +00:00
|
|
|
trans_for_each_iter(trans, i) {
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret2 = bch2_btree_iter_traverse(i->iter);
|
|
|
|
if (ret2) {
|
|
|
|
ret = ret2;
|
2018-07-13 03:30:45 +00:00
|
|
|
trans_restart(" (traverse)");
|
2017-03-17 06:18:50 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
BUG_ON(i->iter->uptodate > BTREE_ITER_NEED_PEEK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BTREE_ITER_ATOMIC means we have to return -EINTR if we
|
|
|
|
* dropped locks:
|
|
|
|
*/
|
|
|
|
if (!(flags & BTREE_INSERT_ATOMIC))
|
|
|
|
goto retry;
|
2018-07-13 03:30:45 +00:00
|
|
|
|
|
|
|
trans_restart(" (atomic)");
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_trans_commit(struct btree_trans *trans,
|
|
|
|
struct disk_reservation *disk_res,
|
|
|
|
u64 *journal_seq,
|
|
|
|
unsigned flags)
|
|
|
|
{
|
2019-03-14 02:44:04 +00:00
|
|
|
int ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
if (!trans->nr_updates)
|
|
|
|
return 0;
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
trans->disk_res = disk_res;
|
|
|
|
trans->journal_seq = journal_seq;
|
|
|
|
trans->flags = flags;
|
|
|
|
|
|
|
|
ret = __bch2_btree_insert_at(trans);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
trans->nr_updates = 0;
|
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
return ret;
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
int bch2_btree_delete_at(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter, unsigned flags)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
|
|
|
struct bkey_i k;
|
|
|
|
|
|
|
|
bkey_init(&k.k);
|
|
|
|
k.k.p = iter->pos;
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_trans_update(trans, BTREE_INSERT_ENTRY(iter, &k));
|
|
|
|
return bch2_trans_commit(trans, NULL, NULL,
|
|
|
|
BTREE_INSERT_NOFAIL|
|
|
|
|
BTREE_INSERT_USE_RESERVE|flags);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bch_btree_insert - insert keys into the extent btree
|
|
|
|
* @c: pointer to struct bch_fs
|
|
|
|
* @id: btree to insert into
|
|
|
|
* @insert_keys: list of keys to insert
|
|
|
|
* @hook: insert callback
|
|
|
|
*/
|
|
|
|
int bch2_btree_insert(struct bch_fs *c, enum btree_id id,
|
|
|
|
struct bkey_i *k,
|
|
|
|
struct disk_reservation *disk_res,
|
|
|
|
u64 *journal_seq, int flags)
|
|
|
|
{
|
2019-03-14 00:49:16 +00:00
|
|
|
struct btree_trans trans;
|
|
|
|
struct btree_iter *iter;
|
2017-03-17 06:18:50 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_trans_init(&trans, c);
|
|
|
|
|
|
|
|
iter = bch2_trans_get_iter(&trans, id, bkey_start_pos(&k->k),
|
|
|
|
BTREE_ITER_INTENT);
|
|
|
|
|
|
|
|
bch2_trans_update(&trans, BTREE_INSERT_ENTRY(iter, k));
|
|
|
|
|
|
|
|
ret = bch2_trans_commit(&trans, disk_res, journal_seq, flags);
|
|
|
|
bch2_trans_exit(&trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bch_btree_delete_range - delete everything within a given range
|
|
|
|
*
|
|
|
|
* Range is a half open interval - [start, end)
|
|
|
|
*/
|
|
|
|
int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id,
|
2018-08-08 23:53:30 +00:00
|
|
|
struct bpos start, struct bpos end,
|
|
|
|
u64 *journal_seq)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2019-03-14 00:49:16 +00:00
|
|
|
struct btree_trans trans;
|
|
|
|
struct btree_iter *iter;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct bkey_s_c k;
|
|
|
|
int ret = 0;
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_trans_init(&trans, c);
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
iter = bch2_trans_get_iter(&trans, id, start, BTREE_ITER_INTENT);
|
|
|
|
|
|
|
|
while ((k = bch2_btree_iter_peek(iter)).k &&
|
2018-08-08 23:53:30 +00:00
|
|
|
!(ret = btree_iter_err(k)) &&
|
2019-03-14 00:49:16 +00:00
|
|
|
bkey_cmp(iter->pos, end) < 0) {
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned max_sectors = KEY_SIZE_MAX & (~0 << c->block_bits);
|
|
|
|
/* really shouldn't be using a bare, unpadded bkey_i */
|
|
|
|
struct bkey_i delete;
|
|
|
|
|
|
|
|
bkey_init(&delete.k);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For extents, iter.pos won't necessarily be the same as
|
|
|
|
* bkey_start_pos(k.k) (for non extents they always will be the
|
|
|
|
* same). It's important that we delete starting from iter.pos
|
|
|
|
* because the range we want to delete could start in the middle
|
|
|
|
* of k.
|
|
|
|
*
|
|
|
|
* (bch2_btree_iter_peek() does guarantee that iter.pos >=
|
|
|
|
* bkey_start_pos(k.k)).
|
|
|
|
*/
|
2019-03-14 00:49:16 +00:00
|
|
|
delete.k.p = iter->pos;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
if (iter->flags & BTREE_ITER_IS_EXTENTS) {
|
2017-03-17 06:18:50 +00:00
|
|
|
/* create the biggest key we can */
|
|
|
|
bch2_key_resize(&delete.k, max_sectors);
|
|
|
|
bch2_cut_back(end, &delete.k);
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_extent_trim_atomic(&delete, iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_trans_update(&trans, BTREE_INSERT_ENTRY(iter, &delete));
|
|
|
|
|
|
|
|
ret = bch2_trans_commit(&trans, NULL, journal_seq,
|
|
|
|
BTREE_INSERT_NOFAIL);
|
2017-03-17 06:18:50 +00:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_btree_iter_cond_resched(iter);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 00:49:16 +00:00
|
|
|
bch2_trans_exit(&trans);
|
2017-03-17 06:18:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|