Skip to content

Commit 0e57996

Browse files
author
Kent Overstreet
committed
bcachefs: bch2_dev_get_ioref2(); alloc_background.c
Signed-off-by: Kent Overstreet <[email protected]>
1 parent b6fb426 commit 0e57996

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,9 +1657,8 @@ static void discard_buckets_next_dev(struct bch_fs *c, struct discard_buckets_st
16571657
bch2_dev_usage_read(s->ca).d[BCH_DATA_free].buckets)
16581658
bch2_journal_flush_async(&c->journal, NULL);
16591659

1660-
bch2_dev_put(s->ca);
1661-
if (ca)
1662-
bch2_dev_get(ca);
1660+
if (s->ca)
1661+
percpu_ref_put(&s->ca->io_ref);
16631662
s->ca = ca;
16641663
s->need_journal_commit_this_dev = 0;
16651664
}
@@ -1673,15 +1672,15 @@ static int bch2_discard_one_bucket(struct btree_trans *trans,
16731672
struct bpos pos = need_discard_iter->pos;
16741673
struct btree_iter iter = { NULL };
16751674
struct bkey_s_c k;
1676-
struct bch_dev *ca;
16771675
struct bkey_i_alloc_v4 *a;
16781676
struct printbuf buf = PRINTBUF;
16791677
bool discard_locked = false;
16801678
int ret = 0;
16811679

1682-
ca = bch2_dev_bkey_exists(c, pos.inode);
1683-
1684-
if (!percpu_ref_tryget(&ca->io_ref)) {
1680+
struct bch_dev *ca = s->ca && s->ca->dev_idx == pos.inode
1681+
? s->ca
1682+
: bch2_dev_get_ioref2(c, pos.inode, WRITE);
1683+
if (!ca) {
16851684
bch2_btree_iter_set_pos(need_discard_iter, POS(pos.inode + 1, 0));
16861685
return 0;
16871686
}
@@ -1787,7 +1786,6 @@ static int bch2_discard_one_bucket(struct btree_trans *trans,
17871786
discard_in_flight_remove(c, iter.pos);
17881787
s->seen++;
17891788
bch2_trans_iter_exit(trans, &iter);
1790-
percpu_ref_put(&ca->io_ref);
17911789
printbuf_exit(&buf);
17921790
return ret;
17931791
}
@@ -1862,9 +1860,8 @@ static void bch2_do_discards_fast_work(struct work_struct *work)
18621860
if (i->snapshot)
18631861
continue;
18641862

1865-
ca = bch2_dev_bkey_exists(c, i->inode);
1866-
1867-
if (!percpu_ref_tryget(&ca->io_ref)) {
1863+
ca = bch2_dev_get_ioref2(c, i->inode, WRITE);
1864+
if (!ca) {
18681865
darray_remove_item(&c->discard_buckets_in_flight, i);
18691866
continue;
18701867
}

fs/bcachefs/sb-members.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ static inline struct bch_dev *bch2_dev_iterate(struct bch_fs *c, struct bch_dev
285285
return bch2_dev_tryget(c, dev_idx);
286286
}
287287

288+
static inline struct bch_dev *bch2_dev_get_ioref2(struct bch_fs *c, unsigned dev, int rw)
289+
{
290+
rcu_read_lock();
291+
struct bch_dev *ca = bch2_dev_rcu(c, dev);
292+
if (ca && !percpu_ref_tryget(&ca->io_ref))
293+
ca = NULL;
294+
rcu_read_unlock();
295+
296+
if (ca &&
297+
(ca->mi.state == BCH_MEMBER_STATE_rw ||
298+
(ca->mi.state == BCH_MEMBER_STATE_ro && rw == READ)))
299+
return ca;
300+
301+
if (ca)
302+
percpu_ref_put(&ca->io_ref);
303+
return NULL;
304+
}
305+
288306
/* XXX kill, move to struct bch_fs */
289307
static inline struct bch_devs_mask bch2_online_devs(struct bch_fs *c)
290308
{

0 commit comments

Comments
 (0)