@@ -711,9 +711,16 @@ static u32 true_sectors_per_clst(const struct NTFS_BOOT *boot)
711711
712712/*
713713 * ntfs_init_from_boot - Init internal info from on-disk boot sector.
714+ *
715+ * NTFS mount begins from boot - special formatted 512 bytes.
716+ * There are two boots: the first and the last 512 bytes of volume.
717+ * The content of boot is not changed during ntfs life.
718+ *
719+ * NOTE: ntfs.sys checks only first (primary) boot.
720+ * chkdsk checks both boots.
714721 */
715722static int ntfs_init_from_boot (struct super_block * sb , u32 sector_size ,
716- u64 dev_size )
723+ u64 dev_size , struct NTFS_BOOT * * boot2 )
717724{
718725 struct ntfs_sb_info * sbi = sb -> s_fs_info ;
719726 int err ;
@@ -937,23 +944,11 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
937944
938945 if (bh -> b_blocknr && !sb_rdonly (sb )) {
939946 /*
940- * Alternative boot is ok but primary is not ok.
941- * Update primary boot.
942- */
943- struct buffer_head * bh0 = sb_getblk (sb , 0 );
944- if (bh0 ) {
945- if (buffer_locked (bh0 ))
946- __wait_on_buffer (bh0 );
947-
948- lock_buffer (bh0 );
949- memcpy (bh0 -> b_data , boot , sizeof (* boot ));
950- set_buffer_uptodate (bh0 );
951- mark_buffer_dirty (bh0 );
952- unlock_buffer (bh0 );
953- if (!sync_dirty_buffer (bh0 ))
954- ntfs_warn (sb , "primary boot is updated" );
955- put_bh (bh0 );
956- }
947+ * Alternative boot is ok but primary is not ok.
948+ * Do not update primary boot here 'cause it may be faked boot.
949+ * Let ntfs to be mounted and update boot later.
950+ */
951+ * boot2 = kmemdup (boot , sizeof (* boot ), GFP_NOFS | __GFP_NOWARN );
957952 }
958953
959954out :
@@ -1000,6 +995,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1000995 u16 * shared ;
1001996 struct MFT_REF ref ;
1002997 bool ro = sb_rdonly (sb );
998+ struct NTFS_BOOT * boot2 = NULL ;
1003999
10041000 ref .high = 0 ;
10051001
@@ -1030,7 +1026,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
10301026
10311027 /* Parse boot. */
10321028 err = ntfs_init_from_boot (sb , bdev_logical_block_size (bdev ),
1033- bdev_nr_bytes (bdev ));
1029+ bdev_nr_bytes (bdev ), & boot2 );
10341030 if (err )
10351031 goto out ;
10361032
@@ -1412,6 +1408,29 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
14121408 goto put_inode_out ;
14131409 }
14141410
1411+ if (boot2 ) {
1412+ /*
1413+ * Alternative boot is ok but primary is not ok.
1414+ * Volume is recognized as NTFS. Update primary boot.
1415+ */
1416+ struct buffer_head * bh0 = sb_getblk (sb , 0 );
1417+ if (bh0 ) {
1418+ if (buffer_locked (bh0 ))
1419+ __wait_on_buffer (bh0 );
1420+
1421+ lock_buffer (bh0 );
1422+ memcpy (bh0 -> b_data , boot2 , sizeof (* boot2 ));
1423+ set_buffer_uptodate (bh0 );
1424+ mark_buffer_dirty (bh0 );
1425+ unlock_buffer (bh0 );
1426+ if (!sync_dirty_buffer (bh0 ))
1427+ ntfs_warn (sb , "primary boot is updated" );
1428+ put_bh (bh0 );
1429+ }
1430+
1431+ kfree (boot2 );
1432+ }
1433+
14151434 return 0 ;
14161435
14171436put_inode_out :
@@ -1424,6 +1443,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
14241443 put_mount_options (sbi -> options );
14251444 put_ntfs (sbi );
14261445 sb -> s_fs_info = NULL ;
1446+ kfree (boot2 );
14271447
14281448 return err ;
14291449}
0 commit comments