Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit c40b199

Browse files
committed
Merge tag 'bcachefs-2024-05-24' of https://evilpiepirate.org/git/bcachefs
Pull bcachefs fixes from Kent Overstreet: "Nothing exciting, just syzbot fixes (except for the one FMODE_CAN_ODIRECT patch). Looks like syzbot reports have slowed down; this is all catch up from two weeks of conferences. Next hardening project is using Thomas's error injection tooling to torture test repair" * tag 'bcachefs-2024-05-24' of https://evilpiepirate.org/git/bcachefs: bcachefs: Fix race path in bch2_inode_insert() bcachefs: Ensure we're RW before journalling bcachefs: Fix shutdown ordering bcachefs: Fix unsafety in bch2_dirent_name_bytes() bcachefs: Fix stack oob in __bch2_encrypt_bio() bcachefs: Fix btree_trans leak in bch2_readahead() bcachefs: Fix bogus verify_replicas_entry() assert bcachefs: Check for subvolues with bogus snapshot/inode fields bcachefs: bch2_checksum() returns 0 for unknown checksum type bcachefs: Fix bch2_alloc_ciphers() bcachefs: Add missing guard in bch2_snapshot_has_children() bcachefs: Fix missing parens in drop_locks_do() bcachefs: Improve bch2_assert_pos_locked() bcachefs: Fix shift overflows in replicas.c bcachefs: Fix shift overflow in btree_lost_data() bcachefs: Fix ref in trans_mark_dev_sbs() error path bcachefs: set FMODE_CAN_ODIRECT instead of a dummy direct_IO method bcachefs: Fix rcu splat in check_fix_ptrs()
2 parents 9ea370f + d93ff5f commit c40b199

15 files changed

+96
-57
lines changed

fs/bcachefs/bcachefs_format.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,12 @@ enum btree_id {
15551555
BTREE_ID_NR
15561556
};
15571557

1558+
/*
1559+
* Maximum number of btrees that we will _ever_ have under the current scheme,
1560+
* where we refer to them with bitfields
1561+
*/
1562+
#define BTREE_ID_NR_MAX 64
1563+
15581564
static inline bool btree_id_is_alloc(enum btree_id id)
15591565
{
15601566
switch (id) {

fs/bcachefs/btree_iter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k
332332
void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
333333
struct bpos pos, bool key_cache)
334334
{
335+
bch2_trans_verify_not_unlocked(trans);
336+
335337
struct btree_path *path;
336338
struct trans_for_each_path_inorder_iter iter;
337339
struct printbuf buf = PRINTBUF;

fs/bcachefs/btree_iter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ __bch2_btree_iter_peek_and_restart(struct btree_trans *trans,
838838
#define drop_locks_do(_trans, _do) \
839839
({ \
840840
bch2_trans_unlock(_trans); \
841-
_do ?: bch2_trans_relock(_trans); \
841+
(_do) ?: bch2_trans_relock(_trans); \
842842
})
843843

844844
#define allocate_dropping_locks_errcode(_trans, _do) \

fs/bcachefs/buckets.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,8 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
479479

480480
percpu_down_read(&c->mark_lock);
481481

482-
rcu_read_lock();
483482
bkey_for_each_ptr_decode(k.k, ptrs_c, p, entry_c) {
484-
struct bch_dev *ca = bch2_dev_rcu(c, p.ptr.dev);
483+
struct bch_dev *ca = bch2_dev_tryget(c, p.ptr.dev);
485484
if (!ca) {
486485
if (fsck_err(c, ptr_to_invalid_device,
487486
"pointer to missing device %u\n"
@@ -558,7 +557,7 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
558557
do_update = true;
559558

560559
if (data_type != BCH_DATA_btree && p.ptr.gen != g->gen)
561-
continue;
560+
goto next;
562561

563562
if (fsck_err_on(bucket_data_type_mismatch(g->data_type, data_type),
564563
c, ptr_bucket_data_type_mismatch,
@@ -601,8 +600,9 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
601600
bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
602601
do_update = true;
603602
}
603+
next:
604+
bch2_dev_put(ca);
604605
}
605-
rcu_read_unlock();
606606

607607
if (do_update) {
608608
if (flags & BTREE_TRIGGER_is_root) {
@@ -638,9 +638,10 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
638638
} else {
639639
struct bkey_ptrs ptrs;
640640
union bch_extent_entry *entry;
641+
642+
rcu_read_lock();
641643
restart_drop_ptrs:
642644
ptrs = bch2_bkey_ptrs(bkey_i_to_s(new));
643-
rcu_read_lock();
644645
bkey_for_each_ptr_decode(bkey_i_to_s(new).k, ptrs, p, entry) {
645646
struct bch_dev *ca = bch2_dev_rcu(c, p.ptr.dev);
646647
struct bucket *g = PTR_GC_BUCKET(ca, &p.ptr);
@@ -1464,7 +1465,7 @@ int bch2_trans_mark_dev_sbs_flags(struct bch_fs *c,
14641465
for_each_online_member(c, ca) {
14651466
int ret = bch2_trans_mark_dev_sb(c, ca, flags);
14661467
if (ret) {
1467-
bch2_dev_put(ca);
1468+
percpu_ref_put(&ca->io_ref);
14681469
return ret;
14691470
}
14701471
}

fs/bcachefs/checksum.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
233233
return ret;
234234
}
235235
default:
236-
BUG();
236+
return (struct bch_csum) {};
237237
}
238238
}
239239

@@ -307,7 +307,7 @@ static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
307307
return ret;
308308
}
309309
default:
310-
BUG();
310+
return (struct bch_csum) {};
311311
}
312312
}
313313

@@ -352,8 +352,12 @@ int __bch2_encrypt_bio(struct bch_fs *c, unsigned type,
352352
bytes += bv.bv_len;
353353
}
354354

355-
sg_mark_end(sg - 1);
356-
return do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
355+
if (sg != sgl) {
356+
sg_mark_end(sg - 1);
357+
return do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
358+
}
359+
360+
return ret;
357361
}
358362

359363
struct bch_csum bch2_checksum_merge(unsigned type, struct bch_csum a,
@@ -648,26 +652,26 @@ int bch2_decrypt_sb_key(struct bch_fs *c,
648652

649653
static int bch2_alloc_ciphers(struct bch_fs *c)
650654
{
651-
int ret;
652-
653-
if (!c->chacha20)
654-
c->chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
655-
ret = PTR_ERR_OR_ZERO(c->chacha20);
655+
if (c->chacha20)
656+
return 0;
656657

658+
struct crypto_sync_skcipher *chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
659+
int ret = PTR_ERR_OR_ZERO(chacha20);
657660
if (ret) {
658661
bch_err(c, "error requesting chacha20 module: %s", bch2_err_str(ret));
659662
return ret;
660663
}
661664

662-
if (!c->poly1305)
663-
c->poly1305 = crypto_alloc_shash("poly1305", 0, 0);
664-
ret = PTR_ERR_OR_ZERO(c->poly1305);
665-
665+
struct crypto_shash *poly1305 = crypto_alloc_shash("poly1305", 0, 0);
666+
ret = PTR_ERR_OR_ZERO(poly1305);
666667
if (ret) {
667668
bch_err(c, "error requesting poly1305 module: %s", bch2_err_str(ret));
669+
crypto_free_sync_skcipher(chacha20);
668670
return ret;
669671
}
670672

673+
c->chacha20 = chacha20;
674+
c->poly1305 = poly1305;
671675
return 0;
672676
}
673677

@@ -762,11 +766,11 @@ int bch2_enable_encryption(struct bch_fs *c, bool keyed)
762766

763767
void bch2_fs_encryption_exit(struct bch_fs *c)
764768
{
765-
if (!IS_ERR_OR_NULL(c->poly1305))
769+
if (c->poly1305)
766770
crypto_free_shash(c->poly1305);
767-
if (!IS_ERR_OR_NULL(c->chacha20))
771+
if (c->chacha20)
768772
crypto_free_sync_skcipher(c->chacha20);
769-
if (!IS_ERR_OR_NULL(c->sha256))
773+
if (c->sha256)
770774
crypto_free_shash(c->sha256);
771775
}
772776

@@ -779,6 +783,7 @@ int bch2_fs_encryption_init(struct bch_fs *c)
779783
c->sha256 = crypto_alloc_shash("sha256", 0, 0);
780784
ret = PTR_ERR_OR_ZERO(c->sha256);
781785
if (ret) {
786+
c->sha256 = NULL;
782787
bch_err(c, "error requesting sha256 module: %s", bch2_err_str(ret));
783788
goto out;
784789
}

fs/bcachefs/dirent.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
static unsigned bch2_dirent_name_bytes(struct bkey_s_c_dirent d)
1717
{
18+
if (bkey_val_bytes(d.k) < offsetof(struct bch_dirent, d_name))
19+
return 0;
20+
1821
unsigned bkey_u64s = bkey_val_u64s(d.k);
1922
unsigned bkey_bytes = bkey_u64s * sizeof(u64);
2023
u64 last_u64 = ((u64*)d.v)[bkey_u64s - 1];

fs/bcachefs/fs-io-buffered.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ void bch2_readahead(struct readahead_control *ractl)
257257
struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
258258
struct bch_fs *c = inode->v.i_sb->s_fs_info;
259259
struct bch_io_opts opts;
260-
struct btree_trans *trans = bch2_trans_get(c);
261260
struct folio *folio;
262261
struct readpages_iter readpages_iter;
263262

@@ -269,6 +268,7 @@ void bch2_readahead(struct readahead_control *ractl)
269268

270269
bch2_pagecache_add_get(inode);
271270

271+
struct btree_trans *trans = bch2_trans_get(c);
272272
while ((folio = readpage_iter_peek(&readpages_iter))) {
273273
unsigned n = min_t(unsigned,
274274
readpages_iter.folios.nr -
@@ -289,10 +289,10 @@ void bch2_readahead(struct readahead_control *ractl)
289289
&readpages_iter);
290290
bch2_trans_unlock(trans);
291291
}
292+
bch2_trans_put(trans);
292293

293294
bch2_pagecache_add_put(inode);
294295

295-
bch2_trans_put(trans);
296296
darray_exit(&readpages_iter.folios);
297297
}
298298

fs/bcachefs/fs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_ino
188188
BUG_ON(!old);
189189

190190
if (unlikely(old != inode)) {
191-
__destroy_inode(&inode->v);
192-
kmem_cache_free(bch2_inode_cache, inode);
191+
discard_new_inode(&inode->v);
193192
inode = old;
194193
} else {
195194
mutex_lock(&c->vfs_inodes_lock);
@@ -1145,6 +1144,8 @@ static int bch2_open(struct inode *vinode, struct file *file)
11451144
return ret;
11461145
}
11471146

1147+
file->f_mode |= FMODE_CAN_ODIRECT;
1148+
11481149
return generic_file_open(vinode, file);
11491150
}
11501151

@@ -1237,7 +1238,6 @@ static const struct address_space_operations bch_address_space_operations = {
12371238
.write_end = bch2_write_end,
12381239
.invalidate_folio = bch2_invalidate_folio,
12391240
.release_folio = bch2_release_folio,
1240-
.direct_IO = noop_direct_IO,
12411241
#ifdef CONFIG_MIGRATION
12421242
.migrate_folio = filemap_migrate_folio,
12431243
#endif

fs/bcachefs/printbuf.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
4545

4646
unsigned new_size = roundup_pow_of_two(out->size + extra);
4747

48+
/* Sanity check... */
49+
if (new_size > PAGE_SIZE << MAX_PAGE_ORDER) {
50+
out->allocation_failure = true;
51+
out->overflow = true;
52+
return -ENOMEM;
53+
}
54+
4855
/*
4956
* Note: output buffer must be freeable with kfree(), it's not required
5057
* that the user use printbuf_exit().

fs/bcachefs/recovery.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
void bch2_btree_lost_data(struct bch_fs *c, enum btree_id btree)
3737
{
38+
if (btree >= BTREE_ID_NR_MAX)
39+
return;
40+
3841
u64 b = BIT_ULL(btree);
3942

4043
if (!(c->sb.btrees_lost_data & b)) {
@@ -808,9 +811,11 @@ int bch2_fs_recovery(struct bch_fs *c)
808811
clear_bit(BCH_FS_fsck_running, &c->flags);
809812

810813
/* fsync if we fixed errors */
811-
if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
814+
if (test_bit(BCH_FS_errors_fixed, &c->flags) &&
815+
bch2_write_ref_tryget(c, BCH_WRITE_REF_fsync)) {
812816
bch2_journal_flush_all_pins(&c->journal);
813817
bch2_journal_meta(&c->journal);
818+
bch2_write_ref_put(c, BCH_WRITE_REF_fsync);
814819
}
815820

816821
/* If we fixed errors, verify that fs is actually clean now: */

0 commit comments

Comments
 (0)