Skip to content

Commit 01db5e5

Browse files
author
Kent Overstreet
committed
bcachefs: Fix insufficient disk reservation with compression + snapshots
When overwriting and splitting existing extents, we weren't correctly accounting for a 3 way split of a compressed extent. Signed-off-by: Kent Overstreet <[email protected]>
1 parent b0c279f commit 01db5e5

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

fs/bcachefs/btree_update.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,21 @@ int bch2_trans_update_extent_overwrite(struct btree_trans *trans,
186186
enum btree_id btree_id = iter->btree_id;
187187
struct bkey_i *update;
188188
struct bpos new_start = bkey_start_pos(new.k);
189-
bool front_split = bkey_lt(bkey_start_pos(old.k), new_start);
190-
bool back_split = bkey_gt(old.k->p, new.k->p);
189+
unsigned front_split = bkey_lt(bkey_start_pos(old.k), new_start);
190+
unsigned back_split = bkey_gt(old.k->p, new.k->p);
191+
unsigned middle_split = (front_split || back_split) &&
192+
old.k->p.snapshot != new.k->p.snapshot;
193+
unsigned nr_splits = front_split + back_split + middle_split;
191194
int ret = 0, compressed_sectors;
192195

193196
/*
194197
* If we're going to be splitting a compressed extent, note it
195198
* so that __bch2_trans_commit() can increase our disk
196199
* reservation:
197200
*/
198-
if (((front_split && back_split) ||
199-
((front_split || back_split) && old.k->p.snapshot != new.k->p.snapshot)) &&
201+
if (nr_splits > 1 &&
200202
(compressed_sectors = bch2_bkey_sectors_compressed(old)))
201-
trans->extra_journal_res += compressed_sectors;
203+
trans->extra_journal_res += compressed_sectors * (nr_splits - 1);
202204

203205
if (front_split) {
204206
update = bch2_bkey_make_mut_noupdate(trans, old);
@@ -216,8 +218,7 @@ int bch2_trans_update_extent_overwrite(struct btree_trans *trans,
216218
}
217219

218220
/* If we're overwriting in a different snapshot - middle split: */
219-
if (old.k->p.snapshot != new.k->p.snapshot &&
220-
(front_split || back_split)) {
221+
if (middle_split) {
221222
update = bch2_bkey_make_mut_noupdate(trans, old);
222223
if ((ret = PTR_ERR_OR_ZERO(update)))
223224
return ret;

0 commit comments

Comments
 (0)