hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/ext2/super.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/ext2/super.c
34 *
....@@ -73,7 +74,7 @@
7374
7475 if (test_opt(sb, ERRORS_PANIC))
7576 panic("EXT2-fs: panic from previous error\n");
76
- if (test_opt(sb, ERRORS_RO)) {
77
+ if (!sb_rdonly(sb) && test_opt(sb, ERRORS_RO)) {
7778 ext2_msg(sb, KERN_CRIT,
7879 "error: remounting filesystem read-only");
7980 sb->s_flags |= SB_RDONLY;
....@@ -148,10 +149,9 @@
148149
149150 ext2_quota_off_umount(sb);
150151
151
- if (sbi->s_ea_block_cache) {
152
- ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
153
- sbi->s_ea_block_cache = NULL;
154
- }
152
+ ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
153
+ sbi->s_ea_block_cache = NULL;
154
+
155155 if (!sb_rdonly(sb)) {
156156 struct ext2_super_block *es = sbi->s_es;
157157
....@@ -162,8 +162,7 @@
162162 }
163163 db_count = sbi->s_gdb_count;
164164 for (i = 0; i < db_count; i++)
165
- if (sbi->s_group_desc[i])
166
- brelse (sbi->s_group_desc[i]);
165
+ brelse(sbi->s_group_desc[i]);
167166 kfree(sbi->s_group_desc);
168167 kfree(sbi->s_debts);
169168 percpu_counter_destroy(&sbi->s_freeblocks_counter);
....@@ -193,15 +192,9 @@
193192 return &ei->vfs_inode;
194193 }
195194
196
-static void ext2_i_callback(struct rcu_head *head)
195
+static void ext2_free_in_core_inode(struct inode *inode)
197196 {
198
- struct inode *inode = container_of(head, struct inode, i_rcu);
199197 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
200
-}
201
-
202
-static void ext2_destroy_inode(struct inode *inode)
203
-{
204
- call_rcu(&inode->i_rcu, ext2_i_callback);
205198 }
206199
207200 static void init_once(void *foo)
....@@ -309,20 +302,17 @@
309302 if (test_opt(sb, NOBH))
310303 seq_puts(seq, ",nobh");
311304
312
-#if defined(CONFIG_QUOTA)
313
- if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
305
+ if (test_opt(sb, USRQUOTA))
314306 seq_puts(seq, ",usrquota");
315307
316
- if (sbi->s_mount_opt & EXT2_MOUNT_GRPQUOTA)
308
+ if (test_opt(sb, GRPQUOTA))
317309 seq_puts(seq, ",grpquota");
318
-#endif
319310
320
-#ifdef CONFIG_FS_DAX
321
- if (sbi->s_mount_opt & EXT2_MOUNT_XIP)
311
+ if (test_opt(sb, XIP))
322312 seq_puts(seq, ",xip");
323
- if (sbi->s_mount_opt & EXT2_MOUNT_DAX)
313
+
314
+ if (test_opt(sb, DAX))
324315 seq_puts(seq, ",dax");
325
-#endif
326316
327317 if (!test_opt(sb, RESERVATION))
328318 seq_puts(seq, ",noreservation");
....@@ -355,7 +345,7 @@
355345
356346 static const struct super_operations ext2_sops = {
357347 .alloc_inode = ext2_alloc_inode,
358
- .destroy_inode = ext2_destroy_inode,
348
+ .free_inode = ext2_free_in_core_inode,
359349 .write_inode = ext2_write_inode,
360350 .evict_inode = ext2_evict_inode,
361351 .put_super = ext2_put_super,
....@@ -441,7 +431,7 @@
441431 enum {
442432 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
443433 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
444
- Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
434
+ Opt_err_ro, Opt_nouid32, Opt_debug,
445435 Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
446436 Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
447437 Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
....@@ -461,8 +451,6 @@
461451 {Opt_err_panic, "errors=panic"},
462452 {Opt_err_ro, "errors=remount-ro"},
463453 {Opt_nouid32, "nouid32"},
464
- {Opt_nocheck, "check=none"},
465
- {Opt_nocheck, "nocheck"},
466454 {Opt_debug, "debug"},
467455 {Opt_oldalloc, "oldalloc"},
468456 {Opt_orlov, "orlov"},
....@@ -556,12 +544,6 @@
556544 case Opt_nouid32:
557545 set_opt (opts->s_mount_opt, NO_UID32);
558546 break;
559
- case Opt_nocheck:
560
- ext2_msg(sb, KERN_WARNING,
561
- "Option nocheck/check=none is deprecated and"
562
- " will be removed in June 2020.");
563
- clear_opt (opts->s_mount_opt, CHECK);
564
- break;
565547 case Opt_debug:
566548 set_opt (opts->s_mount_opt, DEBUG);
567549 break;
....@@ -605,7 +587,7 @@
605587 case Opt_xip:
606588 ext2_msg(sb, KERN_INFO, "use dax instead of xip");
607589 set_opt(opts->s_mount_opt, XIP);
608
- /* Fall through */
590
+ fallthrough;
609591 case Opt_dax:
610592 #ifdef CONFIG_FS_DAX
611593 ext2_msg(sb, KERN_WARNING,
....@@ -691,10 +673,9 @@
691673 es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
692674 le16_add_cpu(&es->s_mnt_count, 1);
693675 if (test_opt (sb, DEBUG))
694
- ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
676
+ ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, gc=%lu, "
695677 "bpg=%lu, ipg=%lu, mo=%04lx]",
696678 EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
697
- sbi->s_frag_size,
698679 sbi->s_groups_count,
699680 EXT2_BLOCKS_PER_GROUP(sb),
700681 EXT2_INODES_PER_GROUP(sb),
....@@ -712,13 +693,7 @@
712693 for (i = 0; i < sbi->s_groups_count; i++) {
713694 struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
714695 ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
715
- ext2_fsblk_t last_block;
716
-
717
- if (i == sbi->s_groups_count - 1)
718
- last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
719
- else
720
- last_block = first_block +
721
- (EXT2_BLOCKS_PER_GROUP(sb) - 1);
696
+ ext2_fsblk_t last_block = ext2_group_last_block_no(sb, i);
722697
723698 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
724699 le32_to_cpu(gdp->bg_block_bitmap) > last_block)
....@@ -780,8 +755,12 @@
780755 res += 1LL << (bits-2);
781756 res += 1LL << (2*(bits-2));
782757 res += 1LL << (3*(bits-2));
758
+ /* Compute how many metadata blocks are needed */
759
+ meta_blocks = 1;
760
+ meta_blocks += 1 + ppb;
761
+ meta_blocks += 1 + ppb + ppb * ppb;
783762 /* Does block tree limit file size? */
784
- if (res < upper_limit)
763
+ if (res + meta_blocks <= upper_limit)
785764 goto check_lfs;
786765
787766 res = upper_limit;
....@@ -816,7 +795,6 @@
816795 {
817796 struct ext2_sb_info *sbi = EXT2_SB(sb);
818797 unsigned long bg, first_meta_bg;
819
- int has_super = 0;
820798
821799 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
822800
....@@ -824,10 +802,8 @@
824802 nr < first_meta_bg)
825803 return (logic_sb_block + nr + 1);
826804 bg = sbi->s_desc_per_block * nr;
827
- if (ext2_bg_has_super(sb, bg))
828
- has_super = 1;
829805
830
- return ext2_group_first_block_no(sb, bg) + has_super;
806
+ return ext2_group_first_block_no(sb, bg) + ext2_bg_has_super(sb, bg);
831807 }
832808
833809 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
....@@ -944,8 +920,7 @@
944920 sbi->s_resgid = opts.s_resgid;
945921
946922 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
947
- ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
948
- SB_POSIXACL : 0);
923
+ (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
949924 sb->s_iflags |= SB_I_CGROUPWB;
950925
951926 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
....@@ -974,13 +949,20 @@
974949 goto failed_mount;
975950 }
976951
952
+ if (le32_to_cpu(es->s_log_block_size) >
953
+ (EXT2_MAX_BLOCK_LOG_SIZE - BLOCK_SIZE_BITS)) {
954
+ ext2_msg(sb, KERN_ERR,
955
+ "Invalid log block size: %u",
956
+ le32_to_cpu(es->s_log_block_size));
957
+ goto failed_mount;
958
+ }
977959 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
978960
979
- if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
961
+ if (test_opt(sb, DAX)) {
980962 if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
981963 ext2_msg(sb, KERN_ERR,
982964 "DAX unsupported by block device. Turning off DAX.");
983
- sbi->s_mount_opt &= ~EXT2_MOUNT_DAX;
965
+ clear_opt(sbi->s_mount_opt, DAX);
984966 }
985967 }
986968
....@@ -1012,6 +994,8 @@
1012994
1013995 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
1014996 sb->s_max_links = EXT2_LINK_MAX;
997
+ sb->s_time_min = S32_MIN;
998
+ sb->s_time_max = S32_MAX;
1015999
10161000 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
10171001 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
....@@ -1029,18 +1013,9 @@
10291013 }
10301014 }
10311015
1032
- sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
1033
- le32_to_cpu(es->s_log_frag_size);
1034
- if (sbi->s_frag_size == 0)
1035
- goto cantfind_ext2;
1036
- sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
1037
-
10381016 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1039
- sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
10401017 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
10411018
1042
- if (EXT2_INODE_SIZE(sb) == 0)
1043
- goto cantfind_ext2;
10441019 sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
10451020 if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
10461021 goto cantfind_ext2;
....@@ -1064,11 +1039,10 @@
10641039 goto failed_mount;
10651040 }
10661041
1067
- if (sb->s_blocksize != sbi->s_frag_size) {
1042
+ if (es->s_log_frag_size != es->s_log_block_size) {
10681043 ext2_msg(sb, KERN_ERR,
1069
- "error: fragsize %lu != blocksize %lu"
1070
- "(not supported yet)",
1071
- sbi->s_frag_size, sb->s_blocksize);
1044
+ "error: fragsize log %u != blocksize log %u",
1045
+ le32_to_cpu(es->s_log_frag_size), sb->s_blocksize_bits);
10721046 goto failed_mount;
10731047 }
10741048
....@@ -1078,15 +1052,10 @@
10781052 sbi->s_blocks_per_group);
10791053 goto failed_mount;
10801054 }
1081
- if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
1055
+ if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
1056
+ sbi->s_inodes_per_group > sb->s_blocksize * 8) {
10821057 ext2_msg(sb, KERN_ERR,
1083
- "error: #fragments per group too big: %lu",
1084
- sbi->s_frags_per_group);
1085
- goto failed_mount;
1086
- }
1087
- if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
1088
- ext2_msg(sb, KERN_ERR,
1089
- "error: #inodes per group too big: %lu",
1058
+ "error: invalid #inodes per group: %lu",
10901059 sbi->s_inodes_per_group);
10911060 goto failed_mount;
10921061 }
....@@ -1096,18 +1065,27 @@
10961065 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
10971066 le32_to_cpu(es->s_first_data_block) - 1)
10981067 / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
1068
+ if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group !=
1069
+ le32_to_cpu(es->s_inodes_count)) {
1070
+ ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu",
1071
+ le32_to_cpu(es->s_inodes_count),
1072
+ (u64)sbi->s_groups_count * sbi->s_inodes_per_group);
1073
+ goto failed_mount;
1074
+ }
10991075 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
11001076 EXT2_DESC_PER_BLOCK(sb);
11011077 sbi->s_group_desc = kmalloc_array (db_count,
11021078 sizeof(struct buffer_head *),
11031079 GFP_KERNEL);
11041080 if (sbi->s_group_desc == NULL) {
1081
+ ret = -ENOMEM;
11051082 ext2_msg(sb, KERN_ERR, "error: not enough memory");
11061083 goto failed_mount;
11071084 }
11081085 bgl_lock_init(sbi->s_blockgroup_lock);
11091086 sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
11101087 if (!sbi->s_debts) {
1088
+ ret = -ENOMEM;
11111089 ext2_msg(sb, KERN_ERR, "error: not enough memory");
11121090 goto failed_mount_group_desc;
11131091 }
....@@ -1156,6 +1134,7 @@
11561134 ext2_count_dirs(sb), GFP_KERNEL);
11571135 }
11581136 if (err) {
1137
+ ret = err;
11591138 ext2_msg(sb, KERN_ERR, "error: insufficient memory");
11601139 goto failed_mount3;
11611140 }
....@@ -1163,6 +1142,7 @@
11631142 #ifdef CONFIG_EXT2_FS_XATTR
11641143 sbi->s_ea_block_cache = ext2_xattr_create_cache();
11651144 if (!sbi->s_ea_block_cache) {
1145
+ ret = -ENOMEM;
11661146 ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache");
11671147 goto failed_mount3;
11681148 }
....@@ -1212,8 +1192,7 @@
12121192 sb->s_id);
12131193 goto failed_mount;
12141194 failed_mount3:
1215
- if (sbi->s_ea_block_cache)
1216
- ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
1195
+ ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
12171196 percpu_counter_destroy(&sbi->s_freeblocks_counter);
12181197 percpu_counter_destroy(&sbi->s_freeinodes_counter);
12191198 percpu_counter_destroy(&sbi->s_dirs_counter);
....@@ -1413,7 +1392,7 @@
14131392 sbi->s_resuid = new_opts.s_resuid;
14141393 sbi->s_resgid = new_opts.s_resgid;
14151394 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1416
- ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? SB_POSIXACL : 0);
1395
+ (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
14171396 spin_unlock(&sbi->s_lock);
14181397
14191398 return 0;
....@@ -1480,8 +1459,7 @@
14801459 buf->f_namelen = EXT2_NAME_LEN;
14811460 fsid = le64_to_cpup((void *)es->s_uuid) ^
14821461 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
1483
- buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1484
- buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1462
+ buf->f_fsid = u64_to_fsid(fsid);
14851463 spin_unlock(&sbi->s_lock);
14861464 return 0;
14871465 }
....@@ -1676,5 +1654,6 @@
16761654 MODULE_AUTHOR("Remy Card and others");
16771655 MODULE_DESCRIPTION("Second Extended Filesystem");
16781656 MODULE_LICENSE("GPL");
1657
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
16791658 module_init(init_ext2_fs)
16801659 module_exit(exit_ext2_fs)