Skip to content

Commit c55f139

Browse files
Miao XieChris Mason
authored andcommitted
Btrfs: fix deadlock when mounting a degraded fs
The deadlock happened when we mount degraded filesystem, the reproduced steps are following: # mkfs.btrfs -f -m raid1 -d raid1 <dev0> <dev1> # echo 1 > /sys/block/`basename <dev0>`/device/delete # mount -o degraded <dev1> <mnt> The reason was that the counter -- bi_remaining was wrong. If the missing or unwriteable device was the last device in the mapping array, we would not submit the original bio, so we shouldn't increase bi_remaining of it in btrfs_end_bio(), or we would skip the final endio handle. Fix this problem by adding a flag into btrfs bio structure. If we submit the original bio, we will set the flag, and we increase bi_remaining counter, or we don't. Though there is another way to fix it -- decrease bi_remaining counter of the original bio when we make sure the original bio is not submitted, this method need add more check and is easy to make mistake. Signed-off-by: Miao Xie <miaox@cn.fujitsu.com> Reviewed-by: Liu Bo <bo.li.liu@oracle.com> Signed-off-by: Chris Mason <clm@fb.com>
1 parent e990f16 commit c55f139

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎fs/btrfs/volumes.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5415,8 +5415,12 @@ static void btrfs_end_bio(struct bio *bio, int err)
54155415
set_bit(BIO_UPTODATE, &bio->bi_flags);
54165416
err = 0;
54175417
}
5418+
5419+
if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5420+
bio_endio_nodec(bio, err);
5421+
else
5422+
bio_endio(bio, err);
54185423
kfree(bbio);
5419-
bio_endio_nodec(bio, err);
54205424
} else if (!is_orig_bio) {
54215425
bio_put(bio);
54225426
}
@@ -5671,6 +5675,7 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
56715675
BUG_ON(!bio); /* -ENOMEM */
56725676
} else {
56735677
bio = first_bio;
5678+
bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
56745679
}
56755680

56765681
submit_stripe_bio(root, bbio, bio,

‎fs/btrfs/volumes.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,14 @@ struct btrfs_bio_stripe {
190190
struct btrfs_bio;
191191
typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
192192

193+
#define BTRFS_BIO_ORIG_BIO_SUBMITTED 0x1
194+
193195
struct btrfs_bio {
194196
atomic_t stripes_pending;
195197
struct btrfs_fs_info *fs_info;
196198
bio_end_io_t *end_io;
197199
struct bio *orig_bio;
200+
unsigned long flags;
198201
void *private;
199202
atomic_t error;
200203
int max_errors;

0 commit comments

Comments
 (0)