Skip to content

Commit

Permalink
fs/ntfs3: Improve alternative boot processing
Browse files Browse the repository at this point in the history
[ Upstream commit c39de95 ]

Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
aalexandrovich authored and gregkh committed Mar 1, 2024
1 parent 4390f74 commit 15735a6
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions fs/ntfs3/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,18 +865,19 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
u16 fn, ao;
u8 cluster_bits;
u32 boot_off = 0;
sector_t boot_block = 0;
const char *hint = "Primary boot";

/* Save original dev_size. Used with alternative boot. */
dev_size0 = dev_size;

sbi->volume.blocks = dev_size >> PAGE_SHIFT;

bh = ntfs_bread(sb, 0);
read_boot:
bh = ntfs_bread(sb, boot_block);
if (!bh)
return -EIO;
return boot_block ? -EINVAL : -EIO;

check_boot:
err = -EINVAL;

/* Corrupted image; do not read OOB */
Expand Down Expand Up @@ -1107,26 +1108,24 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
}

out:
if (err == -EINVAL && !bh->b_blocknr && dev_size0 > PAGE_SHIFT) {
brelse(bh);

if (err == -EINVAL && !boot_block && dev_size0 > PAGE_SHIFT) {
u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
u64 lbo = dev_size0 - sizeof(*boot);

/*
* Try alternative boot (last sector)
*/
brelse(bh);

sb_set_blocksize(sb, block_size);
bh = ntfs_bread(sb, lbo >> blksize_bits(block_size));
if (!bh)
return -EINVAL;

boot_block = lbo >> blksize_bits(block_size);
boot_off = lbo & (block_size - 1);
hint = "Alternative boot";
dev_size = dev_size0; /* restore original size. */
goto check_boot;
if (boot_block && block_size >= boot_off + sizeof(*boot)) {
/*
* Try alternative boot (last sector)
*/
sb_set_blocksize(sb, block_size);
hint = "Alternative boot";
dev_size = dev_size0; /* restore original size. */
goto read_boot;
}
}
brelse(bh);

return err;
}
Expand Down

0 comments on commit 15735a6

Please sign in to comment.