forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
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,
....@@ -712,13 +694,7 @@
712694 for (i = 0; i < sbi->s_groups_count; i++) {
713695 struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
714696 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);
697
+ ext2_fsblk_t last_block = ext2_group_last_block_no(sb, i);
722698
723699 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
724700 le32_to_cpu(gdp->bg_block_bitmap) > last_block)
....@@ -780,8 +756,12 @@
780756 res += 1LL << (bits-2);
781757 res += 1LL << (2*(bits-2));
782758 res += 1LL << (3*(bits-2));
759
+ /* Compute how many metadata blocks are needed */
760
+ meta_blocks = 1;
761
+ meta_blocks += 1 + ppb;
762
+ meta_blocks += 1 + ppb + ppb * ppb;
783763 /* Does block tree limit file size? */
784
- if (res < upper_limit)
764
+ if (res + meta_blocks <= upper_limit)
785765 goto check_lfs;
786766
787767 res = upper_limit;
....@@ -816,7 +796,6 @@
816796 {
817797 struct ext2_sb_info *sbi = EXT2_SB(sb);
818798 unsigned long bg, first_meta_bg;
819
- int has_super = 0;
820799
821800 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
822801
....@@ -824,10 +803,8 @@
824803 nr < first_meta_bg)
825804 return (logic_sb_block + nr + 1);
826805 bg = sbi->s_desc_per_block * nr;
827
- if (ext2_bg_has_super(sb, bg))
828
- has_super = 1;
829806
830
- return ext2_group_first_block_no(sb, bg) + has_super;
807
+ return ext2_group_first_block_no(sb, bg) + ext2_bg_has_super(sb, bg);
831808 }
832809
833810 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
....@@ -944,8 +921,7 @@
944921 sbi->s_resgid = opts.s_resgid;
945922
946923 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
947
- ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
948
- SB_POSIXACL : 0);
924
+ (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
949925 sb->s_iflags |= SB_I_CGROUPWB;
950926
951927 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
....@@ -976,11 +952,11 @@
976952
977953 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
978954
979
- if (sbi->s_mount_opt & EXT2_MOUNT_DAX) {
955
+ if (test_opt(sb, DAX)) {
980956 if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
981957 ext2_msg(sb, KERN_ERR,
982958 "DAX unsupported by block device. Turning off DAX.");
983
- sbi->s_mount_opt &= ~EXT2_MOUNT_DAX;
959
+ clear_opt(sbi->s_mount_opt, DAX);
984960 }
985961 }
986962
....@@ -1012,6 +988,8 @@
1012988
1013989 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
1014990 sb->s_max_links = EXT2_LINK_MAX;
991
+ sb->s_time_min = S32_MIN;
992
+ sb->s_time_max = S32_MAX;
1015993
1016994 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
1017995 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
....@@ -1039,8 +1017,6 @@
10391017 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
10401018 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
10411019
1042
- if (EXT2_INODE_SIZE(sb) == 0)
1043
- goto cantfind_ext2;
10441020 sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
10451021 if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
10461022 goto cantfind_ext2;
....@@ -1084,9 +1060,10 @@
10841060 sbi->s_frags_per_group);
10851061 goto failed_mount;
10861062 }
1087
- if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
1063
+ if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
1064
+ sbi->s_inodes_per_group > sb->s_blocksize * 8) {
10881065 ext2_msg(sb, KERN_ERR,
1089
- "error: #inodes per group too big: %lu",
1066
+ "error: invalid #inodes per group: %lu",
10901067 sbi->s_inodes_per_group);
10911068 goto failed_mount;
10921069 }
....@@ -1096,18 +1073,27 @@
10961073 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
10971074 le32_to_cpu(es->s_first_data_block) - 1)
10981075 / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
1076
+ if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group !=
1077
+ le32_to_cpu(es->s_inodes_count)) {
1078
+ ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu",
1079
+ le32_to_cpu(es->s_inodes_count),
1080
+ (u64)sbi->s_groups_count * sbi->s_inodes_per_group);
1081
+ goto failed_mount;
1082
+ }
10991083 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
11001084 EXT2_DESC_PER_BLOCK(sb);
11011085 sbi->s_group_desc = kmalloc_array (db_count,
11021086 sizeof(struct buffer_head *),
11031087 GFP_KERNEL);
11041088 if (sbi->s_group_desc == NULL) {
1089
+ ret = -ENOMEM;
11051090 ext2_msg(sb, KERN_ERR, "error: not enough memory");
11061091 goto failed_mount;
11071092 }
11081093 bgl_lock_init(sbi->s_blockgroup_lock);
11091094 sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
11101095 if (!sbi->s_debts) {
1096
+ ret = -ENOMEM;
11111097 ext2_msg(sb, KERN_ERR, "error: not enough memory");
11121098 goto failed_mount_group_desc;
11131099 }
....@@ -1156,6 +1142,7 @@
11561142 ext2_count_dirs(sb), GFP_KERNEL);
11571143 }
11581144 if (err) {
1145
+ ret = err;
11591146 ext2_msg(sb, KERN_ERR, "error: insufficient memory");
11601147 goto failed_mount3;
11611148 }
....@@ -1163,6 +1150,7 @@
11631150 #ifdef CONFIG_EXT2_FS_XATTR
11641151 sbi->s_ea_block_cache = ext2_xattr_create_cache();
11651152 if (!sbi->s_ea_block_cache) {
1153
+ ret = -ENOMEM;
11661154 ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache");
11671155 goto failed_mount3;
11681156 }
....@@ -1212,8 +1200,7 @@
12121200 sb->s_id);
12131201 goto failed_mount;
12141202 failed_mount3:
1215
- if (sbi->s_ea_block_cache)
1216
- ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
1203
+ ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
12171204 percpu_counter_destroy(&sbi->s_freeblocks_counter);
12181205 percpu_counter_destroy(&sbi->s_freeinodes_counter);
12191206 percpu_counter_destroy(&sbi->s_dirs_counter);
....@@ -1413,7 +1400,7 @@
14131400 sbi->s_resuid = new_opts.s_resuid;
14141401 sbi->s_resgid = new_opts.s_resgid;
14151402 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1416
- ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? SB_POSIXACL : 0);
1403
+ (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
14171404 spin_unlock(&sbi->s_lock);
14181405
14191406 return 0;
....@@ -1480,8 +1467,7 @@
14801467 buf->f_namelen = EXT2_NAME_LEN;
14811468 fsid = le64_to_cpup((void *)es->s_uuid) ^
14821469 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;
1470
+ buf->f_fsid = u64_to_fsid(fsid);
14851471 spin_unlock(&sbi->s_lock);
14861472 return 0;
14871473 }
....@@ -1676,5 +1662,6 @@
16761662 MODULE_AUTHOR("Remy Card and others");
16771663 MODULE_DESCRIPTION("Second Extended Filesystem");
16781664 MODULE_LICENSE("GPL");
1665
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
16791666 module_init(init_ext2_fs)
16801667 module_exit(exit_ext2_fs)