Skip to content

Commit 4e07447

Browse files
author
Kent Overstreet
committed
bcachefs: Clamp replicas_required to replicas
This prevents going emergency read only when the user has specified replicas_required > replicas. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 04eb579 commit 4e07447

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

fs/bcachefs/bcachefs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,18 @@ static inline struct stdio_redirect *bch2_fs_stdio_redirect(struct bch_fs *c)
12491249
return stdio;
12501250
}
12511251

1252+
static inline unsigned metadata_replicas_required(struct bch_fs *c)
1253+
{
1254+
return min(c->opts.metadata_replicas,
1255+
c->opts.metadata_replicas_required);
1256+
}
1257+
1258+
static inline unsigned data_replicas_required(struct bch_fs *c)
1259+
{
1260+
return min(c->opts.data_replicas,
1261+
c->opts.data_replicas_required);
1262+
}
1263+
12521264
#define BKEY_PADDED_ONSTACK(key, pad) \
12531265
struct { struct bkey_i key; __u64 key ## _pad[pad]; }
12541266

fs/bcachefs/btree_update_interior.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ static struct btree *__bch2_btree_node_alloc(struct btree_trans *trans,
280280
writepoint_ptr(&c->btree_write_point),
281281
&devs_have,
282282
res->nr_replicas,
283-
c->opts.metadata_replicas_required,
283+
min(res->nr_replicas,
284+
c->opts.metadata_replicas_required),
284285
watermark, 0, cl, &wp);
285286
if (unlikely(ret))
286287
return ERR_PTR(ret);

fs/bcachefs/io_write.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,7 @@ CLOSURE_CALLBACK(bch2_write)
15641564
BUG_ON(!op->write_point.v);
15651565
BUG_ON(bkey_eq(op->pos, POS_MAX));
15661566

1567+
op->nr_replicas_required = min_t(unsigned, op->nr_replicas_required, op->nr_replicas);
15671568
op->start_time = local_clock();
15681569
bch2_keylist_init(&op->insert_keys, op->inline_keys);
15691570
wbio_init(bio)->put_bio = false;

fs/bcachefs/journal_io.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,8 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w)
14781478
c->opts.foreground_target;
14791479
unsigned i, replicas = 0, replicas_want =
14801480
READ_ONCE(c->opts.metadata_replicas);
1481+
unsigned replicas_need = min_t(unsigned, replicas_want,
1482+
READ_ONCE(c->opts.metadata_replicas_required));
14811483

14821484
rcu_read_lock();
14831485
retry:
@@ -1526,7 +1528,7 @@ static int journal_write_alloc(struct journal *j, struct journal_buf *w)
15261528

15271529
BUG_ON(bkey_val_u64s(&w->key.k) > BCH_REPLICAS_MAX);
15281530

1529-
return replicas >= c->opts.metadata_replicas_required ? 0 : -EROFS;
1531+
return replicas >= replicas_need ? 0 : -EROFS;
15301532
}
15311533

15321534
static void journal_buf_realloc(struct journal *j, struct journal_buf *buf)

fs/bcachefs/journal_reclaim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void bch2_journal_space_available(struct journal *j)
205205

206206
j->can_discard = can_discard;
207207

208-
if (nr_online < c->opts.metadata_replicas_required) {
208+
if (nr_online < metadata_replicas_required(c)) {
209209
ret = JOURNAL_ERR_insufficient_devices;
210210
goto out;
211211
}

fs/bcachefs/super.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,10 +1428,10 @@ bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
14281428

14291429
required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
14301430
? c->opts.metadata_replicas
1431-
: c->opts.metadata_replicas_required,
1431+
: metadata_replicas_required(c),
14321432
!(flags & BCH_FORCE_IF_DATA_DEGRADED)
14331433
? c->opts.data_replicas
1434-
: c->opts.data_replicas_required);
1434+
: data_replicas_required(c));
14351435

14361436
return nr_rw >= required;
14371437
case BCH_MEMBER_STATE_failed:

0 commit comments

Comments
 (0)