Skip to content

Commit 07d7c4d

Browse files
author
Kent Overstreet
committed
bcachefs: bch2_bucket_ref_update() now takes bch_dev
Signed-off-by: Kent Overstreet <[email protected]>
1 parent a7f1c26 commit 07d7c4d

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

fs/bcachefs/buckets.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -700,15 +700,14 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
700700
return ret;
701701
}
702702

703-
int bch2_bucket_ref_update(struct btree_trans *trans,
704-
struct bkey_s_c k,
705-
const struct bch_extent_ptr *ptr,
706-
s64 sectors, enum bch_data_type ptr_data_type,
707-
u8 b_gen, u8 bucket_data_type,
708-
u32 *bucket_sectors)
703+
int bch2_bucket_ref_update(struct btree_trans *trans, struct bch_dev *ca,
704+
struct bkey_s_c k,
705+
const struct bch_extent_ptr *ptr,
706+
s64 sectors, enum bch_data_type ptr_data_type,
707+
u8 b_gen, u8 bucket_data_type,
708+
u32 *bucket_sectors)
709709
{
710710
struct bch_fs *c = trans->c;
711-
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
712711
size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
713712
struct printbuf buf = PRINTBUF;
714713
bool inserting = sectors > 0;
@@ -939,7 +938,7 @@ int bch2_trans_fs_usage_apply(struct btree_trans *trans,
939938

940939
/* KEY_TYPE_extent: */
941940

942-
static int __mark_pointer(struct btree_trans *trans,
941+
static int __mark_pointer(struct btree_trans *trans, struct bch_dev *ca,
943942
struct bkey_s_c k,
944943
const struct bch_extent_ptr *ptr,
945944
s64 sectors, enum bch_data_type ptr_data_type,
@@ -948,7 +947,7 @@ static int __mark_pointer(struct btree_trans *trans,
948947
u32 *dst_sectors = !ptr->cached
949948
? &a->dirty_sectors
950949
: &a->cached_sectors;
951-
int ret = bch2_bucket_ref_update(trans, k, ptr, sectors, ptr_data_type,
950+
int ret = bch2_bucket_ref_update(trans, ca, k, ptr, sectors, ptr_data_type,
952951
a->gen, a->data_type, dst_sectors);
953952

954953
if (ret)
@@ -966,45 +965,51 @@ static int bch2_trigger_pointer(struct btree_trans *trans,
966965
enum btree_iter_update_trigger_flags flags)
967966
{
968967
bool insert = !(flags & BTREE_TRIGGER_overwrite);
968+
int ret = 0;
969+
970+
struct bch_fs *c = trans->c;
971+
struct bch_dev *ca = bch2_dev_tryget(c, p.ptr.dev);
972+
if (unlikely(!ca)) {
973+
if (insert)
974+
ret = -EIO;
975+
goto err;
976+
}
977+
969978
struct bpos bucket;
970979
struct bch_backpointer bp;
971-
972980
bch2_extent_ptr_to_bp(trans->c, btree_id, level, k, p, entry, &bucket, &bp);
973981
*sectors = insert ? bp.bucket_len : -((s64) bp.bucket_len);
974982

975983
if (flags & BTREE_TRIGGER_transactional) {
976984
struct bkey_i_alloc_v4 *a = bch2_trans_start_alloc_update(trans, bucket);
977-
int ret = PTR_ERR_OR_ZERO(a) ?:
978-
__mark_pointer(trans, k, &p.ptr, *sectors, bp.data_type, &a->v);
985+
ret = PTR_ERR_OR_ZERO(a) ?:
986+
__mark_pointer(trans, ca, k, &p.ptr, *sectors, bp.data_type, &a->v);
979987
if (ret)
980-
return ret;
988+
goto err;
981989

982990
if (!p.ptr.cached) {
983991
ret = bch2_bucket_backpointer_mod(trans, bucket, bp, k, insert);
984992
if (ret)
985-
return ret;
993+
goto err;
986994
}
987995
}
988996

989997
if (flags & BTREE_TRIGGER_gc) {
990-
struct bch_fs *c = trans->c;
991-
struct bch_dev *ca = bch2_dev_bkey_exists(c, p.ptr.dev);
992-
993998
percpu_down_read(&c->mark_lock);
994999
struct bucket *g = gc_bucket(ca, bucket.offset);
9951000
bucket_lock(g);
9961001
struct bch_alloc_v4 old = bucket_m_to_alloc(*g), new = old;
997-
int ret = __mark_pointer(trans, k, &p.ptr, *sectors, bp.data_type, &new);
1002+
ret = __mark_pointer(trans, ca, k, &p.ptr, *sectors, bp.data_type, &new);
9981003
if (!ret) {
9991004
alloc_to_bucket(g, new);
10001005
bch2_dev_usage_update(c, ca, &old, &new, 0, true);
10011006
}
10021007
bucket_unlock(g);
10031008
percpu_up_read(&c->mark_lock);
1004-
return ret;
10051009
}
1006-
1007-
return 0;
1010+
err:
1011+
bch2_dev_put(ca);
1012+
return ret;
10081013
}
10091014

10101015
static int bch2_trigger_stripe_ptr(struct btree_trans *trans,

fs/bcachefs/buckets.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ int bch2_replicas_deltas_realloc(struct btree_trans *, unsigned);
330330

331331
void bch2_fs_usage_initialize(struct bch_fs *);
332332

333-
int bch2_bucket_ref_update(struct btree_trans *, struct bkey_s_c,
334-
const struct bch_extent_ptr *,
333+
int bch2_bucket_ref_update(struct btree_trans *, struct bch_dev *,
334+
struct bkey_s_c, const struct bch_extent_ptr *,
335335
s64, enum bch_data_type, u8, u8, u32 *);
336336

337337
int bch2_check_fix_ptrs(struct btree_trans *,

fs/bcachefs/ec.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
167167
struct bkey_s_c_stripe s,
168168
unsigned ptr_idx, bool deleting,
169169
struct bpos bucket,
170-
struct bch_alloc_v4 *a)
170+
struct bch_alloc_v4 *a,
171+
enum btree_iter_update_trigger_flags flags)
171172
{
172-
struct bch_fs *c = trans->c;
173173
const struct bch_extent_ptr *ptr = s.v->ptrs + ptr_idx;
174174
unsigned nr_data = s.v->nr_blocks - s.v->nr_redundant;
175175
bool parity = ptr_idx >= nr_data;
@@ -178,6 +178,14 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
178178
struct printbuf buf = PRINTBUF;
179179
int ret = 0;
180180

181+
struct bch_fs *c = trans->c;
182+
struct bch_dev *ca = bch2_dev_tryget(c, ptr->dev);
183+
if (unlikely(!ca)) {
184+
if (!(flags & BTREE_TRIGGER_overwrite))
185+
ret = -EIO;
186+
goto err;
187+
}
188+
181189
if (deleting)
182190
sectors = -sectors;
183191

@@ -239,7 +247,7 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
239247
}
240248

241249
if (sectors) {
242-
ret = bch2_bucket_ref_update(trans, s.s_c, ptr, sectors, data_type,
250+
ret = bch2_bucket_ref_update(trans, ca, s.s_c, ptr, sectors, data_type,
243251
a->gen, a->data_type, &a->dirty_sectors);
244252
if (ret)
245253
goto err;
@@ -255,6 +263,7 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
255263

256264
alloc_data_type_set(a, data_type);
257265
err:
266+
bch2_dev_put(ca);
258267
printbuf_exit(&buf);
259268
return ret;
260269
}
@@ -272,7 +281,7 @@ static int mark_stripe_bucket(struct btree_trans *trans,
272281
struct bkey_i_alloc_v4 *a =
273282
bch2_trans_start_alloc_update(trans, bucket);
274283
return PTR_ERR_OR_ZERO(a) ?:
275-
__mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &a->v);
284+
__mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &a->v, flags);
276285
}
277286

278287
if (flags & BTREE_TRIGGER_gc) {
@@ -282,7 +291,7 @@ static int mark_stripe_bucket(struct btree_trans *trans,
282291
struct bucket *g = gc_bucket(ca, bucket.offset);
283292
bucket_lock(g);
284293
struct bch_alloc_v4 old = bucket_m_to_alloc(*g), new = old;
285-
int ret = __mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &new);
294+
int ret = __mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &new, flags);
286295
if (!ret) {
287296
alloc_to_bucket(g, new);
288297
bch2_dev_usage_update(c, ca, &old, &new, 0, true);

0 commit comments

Comments
 (0)