@@ -724,18 +724,21 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
724
724
struct MFT_REC * rec ;
725
725
u16 fn , ao ;
726
726
u8 cluster_bits ;
727
+ u32 boot_off = 0 ;
728
+ const char * hint = "Primary boot" ;
727
729
728
730
sbi -> volume .blocks = dev_size >> PAGE_SHIFT ;
729
731
730
732
bh = ntfs_bread (sb , 0 );
731
733
if (!bh )
732
734
return - EIO ;
733
735
736
+ check_boot :
734
737
err = - EINVAL ;
735
- boot = (struct NTFS_BOOT * )bh -> b_data ;
738
+ boot = (struct NTFS_BOOT * )Add2Ptr ( bh -> b_data , boot_off ) ;
736
739
737
740
if (memcmp (boot -> system_id , "NTFS " , sizeof ("NTFS " ) - 1 )) {
738
- ntfs_err (sb , "Boot' s signature is not NTFS." );
741
+ ntfs_err (sb , "% s signature is not NTFS." , hint );
739
742
goto out ;
740
743
}
741
744
@@ -748,14 +751,16 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
748
751
boot -> bytes_per_sector [0 ];
749
752
if (boot_sector_size < SECTOR_SIZE ||
750
753
!is_power_of_2 (boot_sector_size )) {
751
- ntfs_err (sb , "Invalid bytes per sector %u." , boot_sector_size );
754
+ ntfs_err (sb , "%s: invalid bytes per sector %u." , hint ,
755
+ boot_sector_size );
752
756
goto out ;
753
757
}
754
758
755
759
/* cluster size: 512, 1K, 2K, 4K, ... 2M */
756
760
sct_per_clst = true_sectors_per_clst (boot );
757
761
if ((int )sct_per_clst < 0 || !is_power_of_2 (sct_per_clst )) {
758
- ntfs_err (sb , "Invalid sectors per cluster %u." , sct_per_clst );
762
+ ntfs_err (sb , "%s: invalid sectors per cluster %u." , hint ,
763
+ sct_per_clst );
759
764
goto out ;
760
765
}
761
766
@@ -771,8 +776,8 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
771
776
if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >= sectors ) {
772
777
ntfs_err (
773
778
sb ,
774
- "Start of MFT 0x%llx (0x%llx) is out of volume 0x%llx." ,
775
- mlcn , mlcn2 , sectors );
779
+ "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx." ,
780
+ hint , mlcn , mlcn2 , sectors );
776
781
goto out ;
777
782
}
778
783
@@ -784,7 +789,7 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
784
789
785
790
/* Check MFT record size. */
786
791
if (record_size < SECTOR_SIZE || !is_power_of_2 (record_size )) {
787
- ntfs_err (sb , "Invalid bytes per MFT record %u (%d)." ,
792
+ ntfs_err (sb , "%s: invalid bytes per MFT record %u (%d)." , hint ,
788
793
record_size , boot -> record_size );
789
794
goto out ;
790
795
}
@@ -801,13 +806,13 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
801
806
802
807
/* Check index record size. */
803
808
if (sbi -> index_size < SECTOR_SIZE || !is_power_of_2 (sbi -> index_size )) {
804
- ntfs_err (sb , "Invalid bytes per index %u(%d)." , sbi -> index_size ,
805
- boot -> index_size );
809
+ ntfs_err (sb , "%s: invalid bytes per index %u(%d)." , hint ,
810
+ sbi -> index_size , boot -> index_size );
806
811
goto out ;
807
812
}
808
813
809
814
if (sbi -> index_size > MAXIMUM_BYTES_PER_INDEX ) {
810
- ntfs_err (sb , "Unsupported bytes per index %u." ,
815
+ ntfs_err (sb , "%s: unsupported bytes per index %u." , hint ,
811
816
sbi -> index_size );
812
817
goto out ;
813
818
}
@@ -834,7 +839,7 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
834
839
835
840
/* Compare boot's cluster and sector. */
836
841
if (sbi -> cluster_size < boot_sector_size ) {
837
- ntfs_err (sb , "Invalid bytes per cluster (%u)." ,
842
+ ntfs_err (sb , "%s: invalid bytes per cluster (%u)." , hint ,
838
843
sbi -> cluster_size );
839
844
goto out ;
840
845
}
@@ -930,7 +935,46 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
930
935
931
936
err = 0 ;
932
937
938
+ if (bh -> b_blocknr && !sb_rdonly (sb )) {
939
+ /*
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
+ }
957
+ }
958
+
933
959
out :
960
+ if (err == - EINVAL && !bh -> b_blocknr && dev_size > PAGE_SHIFT ) {
961
+ u32 block_size = min_t (u32 , sector_size , PAGE_SIZE );
962
+ u64 lbo = dev_size - sizeof (* boot );
963
+
964
+ /*
965
+ * Try alternative boot (last sector)
966
+ */
967
+ brelse (bh );
968
+
969
+ sb_set_blocksize (sb , block_size );
970
+ bh = ntfs_bread (sb , lbo >> blksize_bits (block_size ));
971
+ if (!bh )
972
+ return - EINVAL ;
973
+
974
+ boot_off = lbo & (block_size - 1 );
975
+ hint = "Alternative boot" ;
976
+ goto check_boot ;
977
+ }
934
978
brelse (bh );
935
979
936
980
return err ;
@@ -955,6 +999,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
955
999
struct ATTR_DEF_ENTRY * t ;
956
1000
u16 * shared ;
957
1001
struct MFT_REF ref ;
1002
+ bool ro = sb_rdonly (sb );
958
1003
959
1004
ref .high = 0 ;
960
1005
@@ -1035,6 +1080,10 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1035
1080
sbi -> volume .minor_ver = info -> minor_ver ;
1036
1081
sbi -> volume .flags = info -> flags ;
1037
1082
sbi -> volume .ni = ni ;
1083
+ if (info -> flags & VOLUME_FLAG_DIRTY ) {
1084
+ sbi -> volume .real_dirty = true;
1085
+ ntfs_info (sb , "It is recommened to use chkdsk." );
1086
+ }
1038
1087
1039
1088
/* Load $MFTMirr to estimate recs_mirr. */
1040
1089
ref .low = cpu_to_le32 (MFT_REC_MIRR );
@@ -1069,21 +1118,16 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1069
1118
1070
1119
iput (inode );
1071
1120
1072
- if (sbi -> flags & NTFS_FLAGS_NEED_REPLAY ) {
1073
- if (!sb_rdonly (sb )) {
1074
- ntfs_warn (sb ,
1075
- "failed to replay log file. Can't mount rw!" );
1076
- err = - EINVAL ;
1077
- goto out ;
1078
- }
1079
- } else if (sbi -> volume .flags & VOLUME_FLAG_DIRTY ) {
1080
- if (!sb_rdonly (sb ) && !options -> force ) {
1081
- ntfs_warn (
1082
- sb ,
1083
- "volume is dirty and \"force\" flag is not set!" );
1084
- err = - EINVAL ;
1085
- goto out ;
1086
- }
1121
+ if ((sbi -> flags & NTFS_FLAGS_NEED_REPLAY ) && !ro ) {
1122
+ ntfs_warn (sb , "failed to replay log file. Can't mount rw!" );
1123
+ err = - EINVAL ;
1124
+ goto out ;
1125
+ }
1126
+
1127
+ if ((sbi -> volume .flags & VOLUME_FLAG_DIRTY ) && !ro && !options -> force ) {
1128
+ ntfs_warn (sb , "volume is dirty and \"force\" flag is not set!" );
1129
+ err = - EINVAL ;
1130
+ goto out ;
1087
1131
}
1088
1132
1089
1133
/* Load $MFT. */
@@ -1173,7 +1217,7 @@ static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
1173
1217
1174
1218
bad_len += len ;
1175
1219
bad_frags += 1 ;
1176
- if (sb_rdonly ( sb ) )
1220
+ if (ro )
1177
1221
continue ;
1178
1222
1179
1223
if (wnd_set_used_safe (& sbi -> used .bitmap , lcn , len , & tt ) || tt ) {
0 commit comments