Skip to content

Commit 468035c

Browse files
author
Kent Overstreet
committed
bcachefs: Start gc, copygc, rebalance threads after initing writes ref
This fixes a bug where copygc would occasionally race with going read-write and die, thinking we were read only, because it couldn't take a ref on c->writes. It's not necessary for copygc (or rebalance, or copygc) to take write refs; they could run with BCH_TRANS_COMMIT_nocheck_rw, but this is an easier fix that making sure that flag is passed correctly everywhere. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 202a7c2 commit 468035c

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

fs/bcachefs/super.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ static int __bch2_fs_read_write(struct bch_fs *c, bool early)
423423
bch2_dev_allocator_add(c, ca);
424424
bch2_recalc_capacity(c);
425425

426+
set_bit(BCH_FS_RW, &c->flags);
427+
set_bit(BCH_FS_WAS_RW, &c->flags);
428+
429+
#ifndef BCH_WRITE_REF_DEBUG
430+
percpu_ref_reinit(&c->writes);
431+
#else
432+
for (i = 0; i < BCH_WRITE_REF_NR; i++) {
433+
BUG_ON(atomic_long_read(&c->writes[i]));
434+
atomic_long_inc(&c->writes[i]);
435+
}
436+
#endif
437+
426438
ret = bch2_gc_thread_start(c);
427439
if (ret) {
428440
bch_err(c, "error starting gc thread");
@@ -439,24 +451,16 @@ static int __bch2_fs_read_write(struct bch_fs *c, bool early)
439451
goto err;
440452
}
441453

442-
#ifndef BCH_WRITE_REF_DEBUG
443-
percpu_ref_reinit(&c->writes);
444-
#else
445-
for (i = 0; i < BCH_WRITE_REF_NR; i++) {
446-
BUG_ON(atomic_long_read(&c->writes[i]));
447-
atomic_long_inc(&c->writes[i]);
448-
}
449-
#endif
450-
set_bit(BCH_FS_RW, &c->flags);
451-
set_bit(BCH_FS_WAS_RW, &c->flags);
452-
453454
bch2_do_discards(c);
454455
bch2_do_invalidates(c);
455456
bch2_do_stripe_deletes(c);
456457
bch2_do_pending_node_rewrites(c);
457458
return 0;
458459
err:
459-
__bch2_fs_read_only(c);
460+
if (test_bit(BCH_FS_RW, &c->flags))
461+
bch2_fs_read_only(c);
462+
else
463+
__bch2_fs_read_only(c);
460464
return ret;
461465
}
462466

0 commit comments

Comments
 (0)