@@ -341,13 +341,12 @@ static void exfat_hash_init(struct super_block *sb)
341341 INIT_HLIST_HEAD (& sbi -> inode_hashtable [i ]);
342342}
343343
344- static int exfat_read_root (struct inode * inode )
344+ static int exfat_read_root (struct inode * inode , struct exfat_chain * root_clu )
345345{
346346 struct super_block * sb = inode -> i_sb ;
347347 struct exfat_sb_info * sbi = EXFAT_SB (sb );
348348 struct exfat_inode_info * ei = EXFAT_I (inode );
349- struct exfat_chain cdir ;
350- int num_subdirs , num_clu = 0 ;
349+ int num_subdirs ;
351350
352351 exfat_chain_set (& ei -> dir , sbi -> root_dir , 0 , ALLOC_FAT_CHAIN );
353352 ei -> entry = -1 ;
@@ -360,12 +359,9 @@ static int exfat_read_root(struct inode *inode)
360359 ei -> hint_stat .clu = sbi -> root_dir ;
361360 ei -> hint_femp .eidx = EXFAT_HINT_NONE ;
362361
363- exfat_chain_set (& cdir , sbi -> root_dir , 0 , ALLOC_FAT_CHAIN );
364- if (exfat_count_num_clusters (sb , & cdir , & num_clu ))
365- return - EIO ;
366- i_size_write (inode , num_clu << sbi -> cluster_size_bits );
362+ i_size_write (inode , EXFAT_CLU_TO_B (root_clu -> size , sbi ));
367363
368- num_subdirs = exfat_count_dir_entries (sb , & cdir );
364+ num_subdirs = exfat_count_dir_entries (sb , root_clu );
369365 if (num_subdirs < 0 )
370366 return - EIO ;
371367 set_nlink (inode , num_subdirs + EXFAT_MIN_SUBDIR );
@@ -578,7 +574,8 @@ static int exfat_verify_boot_region(struct super_block *sb)
578574}
579575
580576/* mount the file system volume */
581- static int __exfat_fill_super (struct super_block * sb )
577+ static int __exfat_fill_super (struct super_block * sb ,
578+ struct exfat_chain * root_clu )
582579{
583580 int ret ;
584581 struct exfat_sb_info * sbi = EXFAT_SB (sb );
@@ -595,6 +592,18 @@ static int __exfat_fill_super(struct super_block *sb)
595592 goto free_bh ;
596593 }
597594
595+ /*
596+ * Call exfat_count_num_cluster() before searching for up-case and
597+ * bitmap directory entries to avoid infinite loop if they are missing
598+ * and the cluster chain includes a loop.
599+ */
600+ exfat_chain_set (root_clu , sbi -> root_dir , 0 , ALLOC_FAT_CHAIN );
601+ ret = exfat_count_num_clusters (sb , root_clu , & root_clu -> size );
602+ if (ret ) {
603+ exfat_err (sb , "failed to count the number of clusters in root" );
604+ goto free_bh ;
605+ }
606+
598607 ret = exfat_create_upcase_table (sb );
599608 if (ret ) {
600609 exfat_err (sb , "failed to load upcase table" );
@@ -627,6 +636,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
627636 struct exfat_sb_info * sbi = sb -> s_fs_info ;
628637 struct exfat_mount_options * opts = & sbi -> options ;
629638 struct inode * root_inode ;
639+ struct exfat_chain root_clu ;
630640 int err ;
631641
632642 if (opts -> allow_utime == (unsigned short )-1 )
@@ -645,7 +655,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
645655 sb -> s_time_min = EXFAT_MIN_TIMESTAMP_SECS ;
646656 sb -> s_time_max = EXFAT_MAX_TIMESTAMP_SECS ;
647657
648- err = __exfat_fill_super (sb );
658+ err = __exfat_fill_super (sb , & root_clu );
649659 if (err ) {
650660 exfat_err (sb , "failed to recognize exfat type" );
651661 goto check_nls_io ;
@@ -680,7 +690,7 @@ static int exfat_fill_super(struct super_block *sb, struct fs_context *fc)
680690
681691 root_inode -> i_ino = EXFAT_ROOT_INO ;
682692 inode_set_iversion (root_inode , 1 );
683- err = exfat_read_root (root_inode );
693+ err = exfat_read_root (root_inode , & root_clu );
684694 if (err ) {
685695 exfat_err (sb , "failed to initialize root inode" );
686696 goto put_inode ;
0 commit comments