Commit 20572300 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Improve alloc_mem_to_key()

This moves some common code into alloc_mem_to_key(), which translates
from the in-memory format for a bucket to the btree key format.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
parent fb0e4808
...@@ -393,9 +393,6 @@ static int bch2_alloc_write_key(struct btree_trans *trans, ...@@ -393,9 +393,6 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct bkey_s_c k; struct bkey_s_c k;
struct bch_dev *ca;
struct bucket *g;
struct bucket_mark m;
struct bkey_alloc_unpacked old_u, new_u; struct bkey_alloc_unpacked old_u, new_u;
int ret; int ret;
retry: retry:
...@@ -411,14 +408,8 @@ static int bch2_alloc_write_key(struct btree_trans *trans, ...@@ -411,14 +408,8 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
if (ret) if (ret)
goto err; goto err;
old_u = bch2_alloc_unpack(k); old_u = bch2_alloc_unpack(k);
new_u = alloc_mem_to_key(c, iter);
percpu_down_read(&c->mark_lock);
ca = bch_dev_bkey_exists(c, iter->pos.inode);
g = bucket(ca, iter->pos.offset);
m = READ_ONCE(g->mark);
new_u = alloc_mem_to_key(iter, g, m);
percpu_up_read(&c->mark_lock);
if (!bkey_alloc_unpacked_cmp(old_u, new_u)) if (!bkey_alloc_unpacked_cmp(old_u, new_u))
return 0; return 0;
...@@ -470,9 +461,7 @@ int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev, ...@@ -470,9 +461,7 @@ int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
size_t bucket_nr, int rw) size_t bucket_nr, int rw)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct bch_dev *ca = bch_dev_bkey_exists(c, dev);
struct btree_iter iter; struct btree_iter iter;
struct bucket *g;
struct bkey_alloc_unpacked u; struct bkey_alloc_unpacked u;
u64 *time, now; u64 *time, now;
int ret = 0; int ret = 0;
...@@ -485,10 +474,7 @@ int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev, ...@@ -485,10 +474,7 @@ int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
if (ret) if (ret)
goto out; goto out;
percpu_down_read(&c->mark_lock); u = alloc_mem_to_key(c, &iter);
g = bucket(ca, bucket_nr);
u = alloc_mem_to_key(&iter, g, READ_ONCE(g->mark));
percpu_up_read(&c->mark_lock);
time = rw == READ ? &u.read_time : &u.write_time; time = rw == READ ? &u.read_time : &u.write_time;
now = atomic64_read(&c->io_clock[rw].now); now = atomic64_read(&c->io_clock[rw].now);
...@@ -766,8 +752,6 @@ static int bucket_invalidate_btree(struct btree_trans *trans, ...@@ -766,8 +752,6 @@ static int bucket_invalidate_btree(struct btree_trans *trans,
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct bkey_alloc_unpacked u; struct bkey_alloc_unpacked u;
struct bucket *g;
struct bucket_mark m;
struct btree_iter iter; struct btree_iter iter;
int ret; int ret;
...@@ -781,11 +765,7 @@ static int bucket_invalidate_btree(struct btree_trans *trans, ...@@ -781,11 +765,7 @@ static int bucket_invalidate_btree(struct btree_trans *trans,
if (ret) if (ret)
goto err; goto err;
percpu_down_read(&c->mark_lock); u = alloc_mem_to_key(c, &iter);
g = bucket(ca, b);
m = READ_ONCE(g->mark);
u = alloc_mem_to_key(&iter, g, m);
percpu_up_read(&c->mark_lock);
u.gen++; u.gen++;
u.data_type = 0; u.data_type = 0;
......
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
#include "bcachefs.h" #include "bcachefs.h"
#include "alloc_types.h" #include "alloc_types.h"
#include "buckets.h"
#include "debug.h" #include "debug.h"
#include "super.h"
extern const char * const bch2_allocator_states[]; extern const char * const bch2_allocator_states[];
...@@ -43,22 +45,31 @@ int bch2_alloc_write(struct btree_trans *, struct btree_iter *, ...@@ -43,22 +45,31 @@ int bch2_alloc_write(struct btree_trans *, struct btree_iter *,
int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int); int bch2_bucket_io_time_reset(struct btree_trans *, unsigned, size_t, int);
static inline struct bkey_alloc_unpacked static inline struct bkey_alloc_unpacked
alloc_mem_to_key(struct btree_iter *iter, alloc_mem_to_key(struct bch_fs *c, struct btree_iter *iter)
struct bucket *g, struct bucket_mark m)
{ {
return (struct bkey_alloc_unpacked) { struct bch_dev *ca;
struct bucket *g;
struct bkey_alloc_unpacked ret;
percpu_down_read(&c->mark_lock);
ca = bch_dev_bkey_exists(c, iter->pos.inode);
g = bucket(ca, iter->pos.offset);
ret = (struct bkey_alloc_unpacked) {
.dev = iter->pos.inode, .dev = iter->pos.inode,
.bucket = iter->pos.offset, .bucket = iter->pos.offset,
.gen = m.gen, .gen = g->mark.gen,
.oldest_gen = g->oldest_gen, .oldest_gen = g->oldest_gen,
.data_type = m.data_type, .data_type = g->mark.data_type,
.dirty_sectors = m.dirty_sectors, .dirty_sectors = g->mark.dirty_sectors,
.cached_sectors = m.cached_sectors, .cached_sectors = g->mark.cached_sectors,
.read_time = g->io_time[READ], .read_time = g->io_time[READ],
.write_time = g->io_time[WRITE], .write_time = g->io_time[WRITE],
.stripe = g->stripe, .stripe = g->stripe,
.stripe_redundancy = g->stripe_redundancy, .stripe_redundancy = g->stripe_redundancy,
}; };
percpu_up_read(&c->mark_lock);
return ret;
} }
#define ALLOC_SCAN_BATCH(ca) max_t(size_t, 1, (ca)->mi.nbuckets >> 9) #define ALLOC_SCAN_BATCH(ca) max_t(size_t, 1, (ca)->mi.nbuckets >> 9)
......
...@@ -1492,7 +1492,6 @@ static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree ...@@ -1492,7 +1492,6 @@ static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev); struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
struct bpos pos = POS(ptr->dev, PTR_BUCKET_NR(ca, ptr)); struct bpos pos = POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
struct bucket *g;
struct bkey_i *update; struct bkey_i *update;
int ret; int ret;
...@@ -1507,14 +1506,9 @@ static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree ...@@ -1507,14 +1506,9 @@ static int bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree
} }
update = __bch2_btree_trans_peek_updates(iter); update = __bch2_btree_trans_peek_updates(iter);
if (update && !bpos_cmp(update->k.p, pos)) { *u = update && !bpos_cmp(update->k.p, pos)
*u = bch2_alloc_unpack(bkey_i_to_s_c(update)); ? bch2_alloc_unpack(bkey_i_to_s_c(update))
} else { : alloc_mem_to_key(c, iter);
percpu_down_read(&c->mark_lock);
g = bucket(ca, pos.offset);
*u = alloc_mem_to_key(iter, g, READ_ONCE(g->mark));
percpu_up_read(&c->mark_lock);
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment