Skip to content

Commit

Permalink
Update bcachefs sources to 62439c6f1a6d bcachefs: Simplify bch2_xattr…
Browse files Browse the repository at this point in the history
…_emit() implementation

Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
Kent Overstreet committed Aug 19, 2024
1 parent e2e1493 commit 8e0023e
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .bcachefs_revision
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e7015d0df31c410789666b4c8ad0683aae87b4da
62439c6f1a6dba3fca1e57f352745d6e36dd1e31
4 changes: 2 additions & 2 deletions libbcachefs/alloc_background.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ int bch2_bucket_gens_init(struct bch_fs *c)
struct bpos pos = alloc_gens_pos(iter.pos, &offset);
int ret2 = 0;

if (have_bucket_gens_key && bkey_cmp(iter.pos, pos)) {
if (have_bucket_gens_key && !bkey_eq(g.k.p, pos)) {
ret2 = bch2_btree_insert_trans(trans, BTREE_ID_bucket_gens, &g.k_i, 0) ?:
bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
if (ret2)
Expand Down Expand Up @@ -829,7 +829,7 @@ int bch2_trigger_alloc(struct btree_trans *trans,
if (likely(new.k->type == KEY_TYPE_alloc_v4)) {
new_a = bkey_s_to_alloc_v4(new).v;
} else {
BUG_ON(!(flags & BTREE_TRIGGER_gc));
BUG_ON(!(flags & (BTREE_TRIGGER_gc|BTREE_TRIGGER_check_repair)));

struct bkey_i_alloc_v4 *new_ka = bch2_alloc_to_v4_mut_inlined(trans, new.s_c);
ret = PTR_ERR_OR_ZERO(new_ka);
Expand Down
4 changes: 4 additions & 0 deletions libbcachefs/btree_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ static struct btree *__bch2_btree_node_get(struct btree_trans *trans, struct btr

bch2_btree_node_wait_on_read(b);

ret = bch2_trans_relock(trans);
if (ret)
return ERR_PTR(ret);

/*
* should_be_locked is not set on this path yet, so we need to
* relock it specifically:
Expand Down
4 changes: 3 additions & 1 deletion libbcachefs/buckets_waiting_for_journal.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int bch2_set_bucket_needs_journal_commit(struct buckets_waiting_for_journal *b,
nr_elements += t->d[i].journal_seq > flushed_seq;

new_bits = ilog2(roundup_pow_of_two(nr_elements * 3));

realloc:
n = kvmalloc(sizeof(*n) + (sizeof(n->d[0]) << new_bits), GFP_KERNEL);
if (!n) {
ret = -BCH_ERR_ENOMEM_buckets_waiting_for_journal_set;
Expand All @@ -118,6 +118,8 @@ int bch2_set_bucket_needs_journal_commit(struct buckets_waiting_for_journal *b,
if (nr_rehashes_this_size == 3) {
new_bits++;
nr_rehashes_this_size = 0;
kvfree(n);
goto realloc;
}

nr_rehashes++;
Expand Down
207 changes: 113 additions & 94 deletions libbcachefs/data_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,74 @@
#include "subvolume.h"
#include "trace.h"

static void bkey_put_dev_refs(struct bch_fs *c, struct bkey_s_c k)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);

bkey_for_each_ptr(ptrs, ptr)
bch2_dev_put(bch2_dev_have_ref(c, ptr->dev));
}

static bool bkey_get_dev_refs(struct bch_fs *c, struct bkey_s_c k)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);

bkey_for_each_ptr(ptrs, ptr) {
if (!bch2_dev_tryget(c, ptr->dev)) {
bkey_for_each_ptr(ptrs, ptr2) {
if (ptr2 == ptr)
break;
bch2_dev_put(bch2_dev_have_ref(c, ptr2->dev));
}
return false;
}
}
return true;
}

static void bkey_nocow_unlock(struct bch_fs *c, struct bkey_s_c k)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);

bkey_for_each_ptr(ptrs, ptr) {
struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev);
struct bpos bucket = PTR_BUCKET_POS(ca, ptr);

bch2_bucket_nocow_unlock(&c->nocow_locks, bucket, 0);
}
}

static bool bkey_nocow_lock(struct bch_fs *c, struct moving_context *ctxt, struct bkey_s_c k)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);

bkey_for_each_ptr(ptrs, ptr) {
struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev);
struct bpos bucket = PTR_BUCKET_POS(ca, ptr);

if (ctxt) {
bool locked;

move_ctxt_wait_event(ctxt,
(locked = bch2_bucket_nocow_trylock(&c->nocow_locks, bucket, 0)) ||
list_empty(&ctxt->ios));

if (!locked)
bch2_bucket_nocow_lock(&c->nocow_locks, bucket, 0);
} else {
if (!bch2_bucket_nocow_trylock(&c->nocow_locks, bucket, 0)) {
bkey_for_each_ptr(ptrs, ptr2) {
if (ptr2 == ptr)
break;
bch2_bucket_nocow_unlock(&c->nocow_locks, bucket, 0);
}
return false;
}
}
}
return true;
}

static void trace_move_extent_finish2(struct bch_fs *c, struct bkey_s_c k)
{
if (trace_move_extent_finish_enabled()) {
Expand Down Expand Up @@ -355,17 +423,11 @@ void bch2_data_update_read_done(struct data_update *m,
void bch2_data_update_exit(struct data_update *update)
{
struct bch_fs *c = update->op.c;
struct bkey_ptrs_c ptrs =
bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));

bkey_for_each_ptr(ptrs, ptr) {
struct bch_dev *ca = bch2_dev_have_ref(c, ptr->dev);
if (c->opts.nocow_enabled)
bch2_bucket_nocow_unlock(&c->nocow_locks,
PTR_BUCKET_POS(ca, ptr), 0);
bch2_dev_put(ca);
}
struct bkey_s_c k = bkey_i_to_s_c(update->k.k);

if (c->opts.nocow_enabled)
bkey_nocow_unlock(c, k);
bkey_put_dev_refs(c, k);
bch2_bkey_buf_exit(&update->k, c);
bch2_disk_reservation_put(c, &update->op.res);
bch2_bio_free_pages_pool(c, &update->op.wbio.bio);
Expand Down Expand Up @@ -475,6 +537,9 @@ void bch2_data_update_opts_to_text(struct printbuf *out, struct bch_fs *c,
bch2_compression_opt_to_text(out, background_compression(*io_opts));
prt_newline(out);

prt_str(out, "opts.replicas:\t");
prt_u64(out, io_opts->data_replicas);

prt_str(out, "extra replicas:\t");
prt_u64(out, data_opts->extra_replicas);
}
Expand Down Expand Up @@ -543,7 +608,6 @@ int bch2_data_update_init(struct btree_trans *trans,
const union bch_extent_entry *entry;
struct extent_ptr_decoded p;
unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
unsigned ptrs_locked = 0;
int ret = 0;

/*
Expand All @@ -554,6 +618,15 @@ int bch2_data_update_init(struct btree_trans *trans,
if (unlikely(k.k->p.snapshot && !bch2_snapshot_equiv(c, k.k->p.snapshot)))
return -BCH_ERR_data_update_done;

if (!bkey_get_dev_refs(c, k))
return -BCH_ERR_data_update_done;

if (c->opts.nocow_enabled &&
!bkey_nocow_lock(c, ctxt, k)) {
bkey_put_dev_refs(c, k);
return -BCH_ERR_nocow_lock_blocked;
}

bch2_bkey_buf_init(&m->k);
bch2_bkey_buf_reassemble(&m->k, c, k);
m->btree_id = btree_id;
Expand All @@ -575,40 +648,24 @@ int bch2_data_update_init(struct btree_trans *trans,
m->op.compression_opt = background_compression(io_opts);
m->op.watermark = m->data_opts.btree_insert_flags & BCH_WATERMARK_MASK;

bkey_for_each_ptr(ptrs, ptr) {
if (!bch2_dev_tryget(c, ptr->dev)) {
bkey_for_each_ptr(ptrs, ptr2) {
if (ptr2 == ptr)
break;
bch2_dev_put(bch2_dev_have_ref(c, ptr2->dev));
}
return -BCH_ERR_data_update_done;
}
}

unsigned durability_have = 0, durability_removing = 0;

i = 0;
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
struct bch_dev *ca = bch2_dev_have_ref(c, p.ptr.dev);
struct bpos bucket = PTR_BUCKET_POS(ca, &p.ptr);
bool locked;

rcu_read_lock();
if (((1U << i) & m->data_opts.rewrite_ptrs)) {
BUG_ON(p.ptr.cached);

if (crc_is_compressed(p.crc))
reserve_sectors += k.k->size;

m->op.nr_replicas += bch2_extent_ptr_desired_durability(c, &p);
durability_removing += bch2_extent_ptr_desired_durability(c, &p);
} else if (!p.ptr.cached &&
!((1U << i) & m->data_opts.kill_ptrs)) {
bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
durability_have += bch2_extent_ptr_durability(c, &p);
if (!p.ptr.cached) {
rcu_read_lock();
if (BIT(i) & m->data_opts.rewrite_ptrs) {
if (crc_is_compressed(p.crc))
reserve_sectors += k.k->size;

m->op.nr_replicas += bch2_extent_ptr_desired_durability(c, &p);
durability_removing += bch2_extent_ptr_desired_durability(c, &p);
} else if (!(BIT(i) & m->data_opts.kill_ptrs)) {
bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
durability_have += bch2_extent_ptr_durability(c, &p);
}
rcu_read_unlock();
}
rcu_read_unlock();

/*
* op->csum_type is normally initialized from the fs/file's
Expand All @@ -623,24 +680,6 @@ int bch2_data_update_init(struct btree_trans *trans,
if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
m->op.incompressible = true;

if (c->opts.nocow_enabled) {
if (ctxt) {
move_ctxt_wait_event(ctxt,
(locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
bucket, 0)) ||
list_empty(&ctxt->ios));

if (!locked)
bch2_bucket_nocow_lock(&c->nocow_locks, bucket, 0);
} else {
if (!bch2_bucket_nocow_trylock(&c->nocow_locks, bucket, 0)) {
ret = -BCH_ERR_nocow_lock_blocked;
goto err;
}
}
ptrs_locked |= (1U << i);
}

i++;
}

Expand All @@ -654,16 +693,6 @@ int bch2_data_update_init(struct btree_trans *trans,
* Increasing replication is an explicit operation triggered by
* rereplicate, currently, so that users don't get an unexpected -ENOSPC
*/
if (!(m->data_opts.write_flags & BCH_WRITE_CACHED) &&
!durability_required) {
m->data_opts.kill_ptrs |= m->data_opts.rewrite_ptrs;
m->data_opts.rewrite_ptrs = 0;
/* if iter == NULL, it's just a promote */
if (iter)
ret = bch2_extent_drop_ptrs(trans, iter, k, m->data_opts);
goto done;
}

m->op.nr_replicas = min(durability_removing, durability_required) +
m->data_opts.extra_replicas;

Expand All @@ -675,48 +704,38 @@ int bch2_data_update_init(struct btree_trans *trans,
if (!(durability_have + durability_removing))
m->op.nr_replicas = max((unsigned) m->op.nr_replicas, 1);

if (!m->op.nr_replicas) {
struct printbuf buf = PRINTBUF;
m->op.nr_replicas_required = m->op.nr_replicas;

bch2_data_update_to_text(&buf, m);
WARN(1, "trying to move an extent, but nr_replicas=0\n%s", buf.buf);
printbuf_exit(&buf);
ret = -BCH_ERR_data_update_done;
goto done;
/*
* It might turn out that we don't need any new replicas, if the
* replicas or durability settings have been changed since the extent
* was written:
*/
if (!m->op.nr_replicas) {
m->data_opts.kill_ptrs |= m->data_opts.rewrite_ptrs;
m->data_opts.rewrite_ptrs = 0;
/* if iter == NULL, it's just a promote */
if (iter)
ret = bch2_extent_drop_ptrs(trans, iter, k, m->data_opts);
goto out;
}

m->op.nr_replicas_required = m->op.nr_replicas;

if (reserve_sectors) {
ret = bch2_disk_reservation_add(c, &m->op.res, reserve_sectors,
m->data_opts.extra_replicas
? 0
: BCH_DISK_RESERVATION_NOFAIL);
if (ret)
goto err;
goto out;
}

if (bkey_extent_is_unwritten(k)) {
bch2_update_unwritten_extent(trans, m);
goto done;
goto out;
}

return 0;
err:
i = 0;
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
struct bch_dev *ca = bch2_dev_have_ref(c, p.ptr.dev);
struct bpos bucket = PTR_BUCKET_POS(ca, &p.ptr);
if ((1U << i) & ptrs_locked)
bch2_bucket_nocow_unlock(&c->nocow_locks, bucket, 0);
bch2_dev_put(ca);
i++;
}

bch2_bkey_buf_exit(&m->k, c);
bch2_bio_free_pages_pool(c, &m->op.wbio.bio);
return ret;
done:
out:
bch2_data_update_exit(m);
return ret ?: -BCH_ERR_data_update_done;
}
Expand Down
2 changes: 2 additions & 0 deletions libbcachefs/extents.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ void bch2_extent_ptr_to_text(struct printbuf *out, struct bch_fs *c, const struc

prt_printf(out, "ptr: %u:%llu:%u gen %u",
ptr->dev, b, offset, ptr->gen);
if (ca->mi.durability != 1)
prt_printf(out, " d=%u", ca->mi.durability);
if (ptr->cached)
prt_str(out, " cached");
if (ptr->unwritten)
Expand Down
2 changes: 1 addition & 1 deletion libbcachefs/fs-io-buffered.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ static int __bch2_writepage(struct folio *folio,

if (f_sectors > w->tmp_sectors) {
kfree(w->tmp);
w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), __GFP_NOFAIL);
w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), GFP_NOFS|__GFP_NOFAIL);
w->tmp_sectors = f_sectors;
}

Expand Down
3 changes: 2 additions & 1 deletion libbcachefs/replicas.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ int bch2_replicas_gc2(struct bch_fs *c)
.type = BCH_DISK_ACCOUNTING_replicas,
};

memcpy(&k.replicas, e, replicas_entry_bytes(e));
unsafe_memcpy(&k.replicas, e, replicas_entry_bytes(e),
"embedded variable length struct");

struct bpos p = disk_accounting_pos_to_bpos(&k);

Expand Down
12 changes: 11 additions & 1 deletion libbcachefs/xattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,20 @@ static int bch2_xattr_bcachefs_get_effective(
name, buffer, size, true);
}

/* Noop - xattrs in the bcachefs_effective namespace are inherited */
static int bch2_xattr_bcachefs_set_effective(const struct xattr_handler *handler,
struct mnt_idmap *idmap,
struct dentry *dentry, struct inode *vinode,
const char *name, const void *value,
size_t size, int flags)
{
return 0;
}

static const struct xattr_handler bch_xattr_bcachefs_effective_handler = {
.prefix = "bcachefs_effective.",
.get = bch2_xattr_bcachefs_get_effective,
.set = bch2_xattr_bcachefs_set,
.set = bch2_xattr_bcachefs_set_effective,
};

#endif /* NO_BCACHEFS_FS */
Expand Down

0 comments on commit 8e0023e

Please sign in to comment.