Skip to content

Commit 1f2f92e

Browse files
author
Kent Overstreet
committed
bcachefs: PTR_BUCKET_POS() now takes bch_dev
Signed-off-by: Kent Overstreet <[email protected]>
1 parent fa6cce0 commit 1f2f92e

File tree

8 files changed

+57
-49
lines changed

8 files changed

+57
-49
lines changed

fs/bcachefs/backpointers.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ static bool extent_matches_bp(struct bch_fs *c,
3030
if (p.ptr.cached)
3131
continue;
3232

33-
bch2_extent_ptr_to_bp(c, btree_id, level, k, p, entry, &bucket2, &bp2);
33+
struct bch_dev *ca = bch2_dev_bkey_exists(c, p.ptr.dev);
34+
35+
bch2_extent_ptr_to_bp(c, ca, btree_id, level, k, p, entry, &bucket2, &bp2);
3436
if (bpos_eq(bucket, bucket2) &&
3537
!memcmp(&bp, &bp2, sizeof(bp)))
3638
return true;
@@ -666,7 +668,8 @@ static int check_extent_to_backpointers(struct btree_trans *trans,
666668
if (p.ptr.cached)
667669
continue;
668670

669-
bch2_extent_ptr_to_bp(c, btree, level, k, p, entry, &bucket_pos, &bp);
671+
struct bch_dev *ca = bch2_dev_bkey_exists(c, p.ptr.dev);
672+
bch2_extent_ptr_to_bp(c, ca, btree, level, k, p, entry, &bucket_pos, &bp);
670673

671674
ret = check_bp_exists(trans, s, bucket_pos, bp, k);
672675
if (ret)

fs/bcachefs/backpointers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static inline enum bch_data_type bch2_bkey_ptr_data_type(struct bkey_s_c k,
120120
}
121121
}
122122

123-
static inline void bch2_extent_ptr_to_bp(struct bch_fs *c,
123+
static inline void bch2_extent_ptr_to_bp(struct bch_fs *c, struct bch_dev *ca,
124124
enum btree_id btree_id, unsigned level,
125125
struct bkey_s_c k, struct extent_ptr_decoded p,
126126
const union bch_extent_entry *entry,
@@ -130,7 +130,7 @@ static inline void bch2_extent_ptr_to_bp(struct bch_fs *c,
130130
s64 sectors = level ? btree_sectors(c) : k.k->size;
131131
u32 bucket_offset;
132132

133-
*bucket_pos = PTR_BUCKET_POS_OFFSET(c, &p.ptr, &bucket_offset);
133+
*bucket_pos = PTR_BUCKET_POS_OFFSET(ca, &p.ptr, &bucket_offset);
134134
*bp = (struct bch_backpointer) {
135135
.btree_id = btree_id,
136136
.level = level,

fs/bcachefs/buckets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ static int bch2_trigger_pointer(struct btree_trans *trans,
977977

978978
struct bpos bucket;
979979
struct bch_backpointer bp;
980-
bch2_extent_ptr_to_bp(trans->c, btree_id, level, k, p, entry, &bucket, &bp);
980+
bch2_extent_ptr_to_bp(trans->c, ca, btree_id, level, k, p, entry, &bucket, &bp);
981981
*sectors = insert ? bp.bucket_len : -((s64) bp.bucket_len);
982982

983983
if (flags & BTREE_TRIGGER_transactional) {

fs/bcachefs/buckets.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,16 @@ static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca,
120120
return sector_to_bucket(ca, ptr->offset);
121121
}
122122

123-
static inline struct bpos PTR_BUCKET_POS(const struct bch_fs *c,
124-
const struct bch_extent_ptr *ptr)
123+
static inline struct bpos PTR_BUCKET_POS(const struct bch_dev *ca,
124+
const struct bch_extent_ptr *ptr)
125125
{
126-
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
127-
128126
return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr));
129127
}
130128

131-
static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_fs *c,
129+
static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_dev *ca,
132130
const struct bch_extent_ptr *ptr,
133131
u32 *bucket_offset)
134132
{
135-
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
136-
137133
return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset));
138134
}
139135

fs/bcachefs/data_update.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,11 @@ void bch2_data_update_exit(struct data_update *update)
357357
bch2_bkey_ptrs_c(bkey_i_to_s_c(update->k.k));
358358

359359
bkey_for_each_ptr(ptrs, ptr) {
360+
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
360361
if (c->opts.nocow_enabled)
361362
bch2_bucket_nocow_unlock(&c->nocow_locks,
362-
PTR_BUCKET_POS(c, ptr), 0);
363-
bch2_dev_put(bch2_dev_bkey_exists(c, ptr->dev));
363+
PTR_BUCKET_POS(ca, ptr), 0);
364+
bch2_dev_put(ca);
364365
}
365366

366367
bch2_bkey_buf_exit(&update->k, c);
@@ -547,6 +548,8 @@ int bch2_data_update_init(struct btree_trans *trans,
547548

548549
i = 0;
549550
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
551+
struct bch_dev *ca = bch2_dev_bkey_exists(c, p.ptr.dev);
552+
struct bpos bucket = PTR_BUCKET_POS(ca, &p.ptr);
550553
bool locked;
551554

552555
if (((1U << i) & m->data_opts.rewrite_ptrs)) {
@@ -580,15 +583,13 @@ int bch2_data_update_init(struct btree_trans *trans,
580583
if (ctxt) {
581584
move_ctxt_wait_event(ctxt,
582585
(locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
583-
PTR_BUCKET_POS(c, &p.ptr), 0)) ||
586+
bucket, 0)) ||
584587
list_empty(&ctxt->ios));
585588

586589
if (!locked)
587-
bch2_bucket_nocow_lock(&c->nocow_locks,
588-
PTR_BUCKET_POS(c, &p.ptr), 0);
590+
bch2_bucket_nocow_lock(&c->nocow_locks, bucket, 0);
589591
} else {
590-
if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
591-
PTR_BUCKET_POS(c, &p.ptr), 0)) {
592+
if (!bch2_bucket_nocow_trylock(&c->nocow_locks, bucket, 0)) {
592593
ret = -BCH_ERR_nocow_lock_blocked;
593594
goto err;
594595
}
@@ -650,10 +651,11 @@ int bch2_data_update_init(struct btree_trans *trans,
650651
err:
651652
i = 0;
652653
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
654+
struct bch_dev *ca = bch2_dev_bkey_exists(c, p.ptr.dev);
655+
struct bpos bucket = PTR_BUCKET_POS(ca, &p.ptr);
653656
if ((1U << i) & ptrs_locked)
654-
bch2_bucket_nocow_unlock(&c->nocow_locks,
655-
PTR_BUCKET_POS(c, &p.ptr), 0);
656-
bch2_dev_put(bch2_dev_bkey_exists(c, p.ptr.dev));
657+
bch2_bucket_nocow_unlock(&c->nocow_locks, bucket, 0);
658+
bch2_dev_put(ca);
657659
i++;
658660
}
659661

fs/bcachefs/ec.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
164164
/* Triggers: */
165165

166166
static int __mark_stripe_bucket(struct btree_trans *trans,
167+
struct bch_dev *ca,
167168
struct bkey_s_c_stripe s,
168169
unsigned ptr_idx, bool deleting,
169170
struct bpos bucket,
@@ -179,13 +180,6 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
179180
int ret = 0;
180181

181182
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-
189183
if (deleting)
190184
sectors = -sectors;
191185

@@ -263,7 +257,6 @@ static int __mark_stripe_bucket(struct btree_trans *trans,
263257

264258
alloc_data_type_set(a, data_type);
265259
err:
266-
bch2_dev_put(ca);
267260
printbuf_exit(&buf);
268261
return ret;
269262
}
@@ -275,34 +268,40 @@ static int mark_stripe_bucket(struct btree_trans *trans,
275268
{
276269
struct bch_fs *c = trans->c;
277270
const struct bch_extent_ptr *ptr = s.v->ptrs + ptr_idx;
278-
struct bpos bucket = PTR_BUCKET_POS(c, ptr);
271+
int ret = 0;
272+
273+
struct bch_dev *ca = bch2_dev_tryget(c, ptr->dev);
274+
if (unlikely(!ca)) {
275+
if (!(flags & BTREE_TRIGGER_overwrite))
276+
ret = -EIO;
277+
goto err;
278+
}
279+
280+
struct bpos bucket = PTR_BUCKET_POS(ca, ptr);
279281

280282
if (flags & BTREE_TRIGGER_transactional) {
281283
struct bkey_i_alloc_v4 *a =
282284
bch2_trans_start_alloc_update(trans, bucket);
283-
return PTR_ERR_OR_ZERO(a) ?:
284-
__mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &a->v, flags);
285+
ret = PTR_ERR_OR_ZERO(a) ?:
286+
__mark_stripe_bucket(trans, ca, s, ptr_idx, deleting, bucket, &a->v, flags);
285287
}
286288

287289
if (flags & BTREE_TRIGGER_gc) {
288-
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
289-
290290
percpu_down_read(&c->mark_lock);
291291
struct bucket *g = gc_bucket(ca, bucket.offset);
292292
bucket_lock(g);
293293
struct bch_alloc_v4 old = bucket_m_to_alloc(*g), new = old;
294-
int ret = __mark_stripe_bucket(trans, s, ptr_idx, deleting, bucket, &new, flags);
294+
ret = __mark_stripe_bucket(trans, ca, s, ptr_idx, deleting, bucket, &new, flags);
295295
if (!ret) {
296296
alloc_to_bucket(g, new);
297297
bch2_dev_usage_update(c, ca, &old, &new, 0, true);
298298
}
299299
bucket_unlock(g);
300300
percpu_up_read(&c->mark_lock);
301-
return ret;
302301
}
303-
304-
BUG();
305-
return 0;
302+
err:
303+
bch2_dev_put(ca);
304+
return ret;
306305
}
307306

308307
static int mark_stripe_buckets(struct btree_trans *trans,
@@ -1298,17 +1297,21 @@ static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_b
12981297
{
12991298
struct bch_fs *c = trans->c;
13001299
struct bch_stripe *v = &bkey_i_to_stripe(&s->key)->v;
1301-
struct bch_extent_ptr bucket = v->ptrs[block];
1302-
struct bpos bucket_pos = PTR_BUCKET_POS(c, &bucket);
1300+
struct bch_extent_ptr ptr = v->ptrs[block];
13031301
struct bpos bp_pos = POS_MIN;
13041302
int ret = 0;
13051303

1304+
struct bch_dev *ca = bch2_dev_tryget(c, ptr.dev);
1305+
if (!ca)
1306+
return -EIO;
1307+
1308+
struct bpos bucket_pos = PTR_BUCKET_POS(ca, &ptr);
1309+
13061310
while (1) {
13071311
ret = commit_do(trans, NULL, NULL,
13081312
BCH_TRANS_COMMIT_no_check_rw|
13091313
BCH_TRANS_COMMIT_no_enospc,
1310-
ec_stripe_update_extent(trans, bucket_pos, bucket.gen,
1311-
s, &bp_pos));
1314+
ec_stripe_update_extent(trans, bucket_pos, ptr.gen, s, &bp_pos));
13121315
if (ret)
13131316
break;
13141317
if (bkey_eq(bp_pos, POS_MAX))
@@ -1317,6 +1320,7 @@ static int ec_stripe_update_bucket(struct btree_trans *trans, struct ec_stripe_b
13171320
bp_pos = bpos_nosnap_successor(bp_pos);
13181321
}
13191322

1323+
bch2_dev_put(ca);
13201324
return ret;
13211325
}
13221326

fs/bcachefs/io_read.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
768768
int ret;
769769

770770
bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
771-
PTR_BUCKET_POS(c, &ptr),
771+
PTR_BUCKET_POS(ca, &ptr),
772772
BTREE_ITER_cached);
773773

774774
prt_printf(&buf, "Attempting to read from stale dirty pointer:\n");

fs/bcachefs/io_write.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,10 +1117,12 @@ static inline void bch2_nocow_write_unlock(struct bch_write_op *op)
11171117
for_each_keylist_key(&op->insert_keys, k) {
11181118
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
11191119

1120-
bkey_for_each_ptr(ptrs, ptr)
1120+
bkey_for_each_ptr(ptrs, ptr) {
1121+
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
11211122
bch2_bucket_nocow_unlock(&c->nocow_locks,
1122-
PTR_BUCKET_POS(c, ptr),
1123+
PTR_BUCKET_POS(ca, ptr),
11231124
BUCKET_NOCOW_LOCK_UPDATE);
1125+
}
11241126
}
11251127
}
11261128

@@ -1270,12 +1272,13 @@ static void bch2_nocow_write(struct bch_write_op *op)
12701272
/* Get iorefs before dropping btree locks: */
12711273
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
12721274
bkey_for_each_ptr(ptrs, ptr) {
1273-
struct bpos b = PTR_BUCKET_POS(c, ptr);
1275+
struct bch_dev *ca = bch2_dev_bkey_exists(c, ptr->dev);
1276+
struct bpos b = PTR_BUCKET_POS(ca, ptr);
12741277
struct nocow_lock_bucket *l =
12751278
bucket_nocow_lock(&c->nocow_locks, bucket_to_u64(b));
12761279
prefetch(l);
12771280

1278-
if (unlikely(!bch2_dev_get_ioref(bch2_dev_bkey_exists(c, ptr->dev), WRITE)))
1281+
if (unlikely(!bch2_dev_get_ioref(ca, WRITE)))
12791282
goto err_get_ioref;
12801283

12811284
/* XXX allocating memory with btree locks held - rare */

0 commit comments

Comments
 (0)