btrfs: check-integrity: Fix NULL pointer dereference for degraded mount
authorQu Wenruo <>
Wed, 20 Jun 2018 07:38:58 +0000 (15:38 +0800)
committerDavid Sterba <>
Mon, 6 Aug 2018 11:12:38 +0000 (13:12 +0200)
Commit f8f84b2dfda5 ("btrfs: index check-integrity state hash by a dev_t")
changed how btrfsic indexes device state.

Now we need to access device->bdev->bd_dev, while for degraded mount
it's completely possible to have device->bdev as NULL, thus it will
trigger a NULL pointer dereference at mount time.

Fix it by checking if the device is degraded before accessing

There are a lot of other places accessing device->bdev->bd_dev, however
the other call sites have either checked device->bdev, or the
device->bdev is passed from btrfsic_map_block(), so it won't cause harm.

Fixes: f8f84b2dfda5 ("btrfs: index check-integrity state hash by a dev_t")
Signed-off-by: Qu Wenruo <>
Reviewed-by: David Sterba <>
Signed-off-by: David Sterba <>

index a3fdb4fe967d29cc87a4e2402d7898ddeb3b5670..daf45472bef9140d75beae120038e47b9b6ae2b7 100644 (file)
@@ -1539,7 +1539,12 @@ static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
        device = multi->stripes[0].dev;
-       block_ctx_out->dev = btrfsic_dev_state_lookup(device->bdev->bd_dev);
+       if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
+           !device->bdev || !device->name)
+               block_ctx_out->dev = NULL;
+       else
+               block_ctx_out->dev = btrfsic_dev_state_lookup(
+                                                       device->bdev->bd_dev);
        block_ctx_out->dev_bytenr = multi->stripes[0].physical;
        block_ctx_out->start = bytenr;
        block_ctx_out->len = len;