bcachefs: Fix refcounting in discard path

bch_dev->io_ref does not protect against the filesystem going away;
bch_fs->writes does.

Thus the filesystem write ref needs to be the last ref we release.

Reported-by: syzbot+9e0404b505e604f67e41@syzkaller.appspotmail.com
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>

+6 -6
+6 -6
fs/bcachefs/alloc_background.c
··· 1874 1874 trace_discard_buckets(c, s.seen, s.open, s.need_journal_commit, s.discarded, 1875 1875 bch2_err_str(ret)); 1876 1876 1877 - bch2_write_ref_put(c, BCH_WRITE_REF_discard); 1878 1877 percpu_ref_put(&ca->io_ref); 1878 + bch2_write_ref_put(c, BCH_WRITE_REF_discard); 1879 1879 } 1880 1880 1881 1881 void bch2_dev_do_discards(struct bch_dev *ca) 1882 1882 { 1883 1883 struct bch_fs *c = ca->fs; 1884 1884 1885 - if (!bch2_dev_get_ioref(c, ca->dev_idx, WRITE)) 1885 + if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_discard)) 1886 1886 return; 1887 1887 1888 - if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_discard)) 1889 - goto put_ioref; 1888 + if (!bch2_dev_get_ioref(c, ca->dev_idx, WRITE)) 1889 + goto put_write_ref; 1890 1890 1891 1891 if (queue_work(c->write_ref_wq, &ca->discard_work)) 1892 1892 return; 1893 1893 1894 - bch2_write_ref_put(c, BCH_WRITE_REF_discard); 1895 - put_ioref: 1896 1894 percpu_ref_put(&ca->io_ref); 1895 + put_write_ref: 1896 + bch2_write_ref_put(c, BCH_WRITE_REF_discard); 1897 1897 } 1898 1898 1899 1899 void bch2_do_discards(struct bch_fs *c)