forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/fs/f2fs/super.c
....@@ -24,13 +24,15 @@
2424 #include <linux/sysfs.h>
2525 #include <linux/quota.h>
2626 #include <linux/unicode.h>
27
+#include <linux/part_stat.h>
28
+#include <linux/zstd.h>
29
+#include <linux/lz4.h>
2730
2831 #include "f2fs.h"
2932 #include "node.h"
3033 #include "segment.h"
3134 #include "xattr.h"
3235 #include "gc.h"
33
-#include "trace.h"
3436
3537 #define CREATE_TRACE_POINTS
3638 #include <trace/events/f2fs.h>
....@@ -44,7 +46,6 @@
4446 [FAULT_KVMALLOC] = "kvmalloc",
4547 [FAULT_PAGE_ALLOC] = "page alloc",
4648 [FAULT_PAGE_GET] = "page get",
47
- [FAULT_ALLOC_BIO] = "alloc bio",
4849 [FAULT_ALLOC_NID] = "alloc nid",
4950 [FAULT_ORPHAN] = "orphan",
5051 [FAULT_BLOCK] = "no more block",
....@@ -142,9 +143,19 @@
142143 Opt_checkpoint_disable_cap,
143144 Opt_checkpoint_disable_cap_perc,
144145 Opt_checkpoint_enable,
146
+ Opt_checkpoint_merge,
147
+ Opt_nocheckpoint_merge,
145148 Opt_compress_algorithm,
146149 Opt_compress_log_size,
147150 Opt_compress_extension,
151
+ Opt_compress_chksum,
152
+ Opt_compress_mode,
153
+ Opt_compress_cache,
154
+ Opt_atgc,
155
+ Opt_gc_merge,
156
+ Opt_nogc_merge,
157
+ Opt_memory_mode,
158
+ Opt_age_extent_cache,
148159 Opt_err,
149160 };
150161
....@@ -209,9 +220,19 @@
209220 {Opt_checkpoint_disable_cap, "checkpoint=disable:%u"},
210221 {Opt_checkpoint_disable_cap_perc, "checkpoint=disable:%u%%"},
211222 {Opt_checkpoint_enable, "checkpoint=enable"},
223
+ {Opt_checkpoint_merge, "checkpoint_merge"},
224
+ {Opt_nocheckpoint_merge, "nocheckpoint_merge"},
212225 {Opt_compress_algorithm, "compress_algorithm=%s"},
213226 {Opt_compress_log_size, "compress_log_size=%u"},
214227 {Opt_compress_extension, "compress_extension=%s"},
228
+ {Opt_compress_chksum, "compress_chksum"},
229
+ {Opt_compress_mode, "compress_mode=%s"},
230
+ {Opt_compress_cache, "compress_cache"},
231
+ {Opt_atgc, "atgc"},
232
+ {Opt_gc_merge, "gc_merge"},
233
+ {Opt_nogc_merge, "nogc_merge"},
234
+ {Opt_memory_mode, "memory=%s"},
235
+ {Opt_age_extent_cache, "age_extent_cache"},
215236 {Opt_err, NULL},
216237 };
217238
....@@ -260,17 +281,35 @@
260281
261282 return 0;
262283 }
284
+
285
+struct kmem_cache *f2fs_cf_name_slab;
286
+static int __init f2fs_create_casefold_cache(void)
287
+{
288
+ f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
289
+ F2FS_NAME_LEN);
290
+ if (!f2fs_cf_name_slab)
291
+ return -ENOMEM;
292
+ return 0;
293
+}
294
+
295
+static void f2fs_destroy_casefold_cache(void)
296
+{
297
+ kmem_cache_destroy(f2fs_cf_name_slab);
298
+}
299
+#else
300
+static int __init f2fs_create_casefold_cache(void) { return 0; }
301
+static void f2fs_destroy_casefold_cache(void) { }
263302 #endif
264303
265304 static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
266305 {
267
- block_t limit = min((sbi->user_block_count << 1) / 1000,
306
+ block_t limit = min((sbi->user_block_count >> 3),
268307 sbi->user_block_count - sbi->reserved_blocks);
269308
270
- /* limit is 0.2%, and cannot be too small */
271
- limit = max(limit, MIN_ROOT_RESERVED_BLOCKS);
309
+ /* limit is 12.5% */
272310 if (test_opt(sbi, RESERVE_ROOT) &&
273
- F2FS_OPTION(sbi).root_reserved_blocks > limit) {
311
+ F2FS_OPTION(sbi).root_reserved_blocks > limit &&
312
+ F2FS_OPTION(sbi).root_reserved_blocks > MIN_ROOT_RESERVED_BLOCKS) {
274313 F2FS_OPTION(sbi).root_reserved_blocks = limit;
275314 f2fs_info(sbi, "Reduce reserved blocks for root = %u",
276315 F2FS_OPTION(sbi).root_reserved_blocks);
....@@ -285,6 +324,46 @@
285324 F2FS_OPTION(sbi).s_resuid),
286325 from_kgid_munged(&init_user_ns,
287326 F2FS_OPTION(sbi).s_resgid));
327
+}
328
+
329
+static inline int adjust_reserved_segment(struct f2fs_sb_info *sbi)
330
+{
331
+ unsigned int sec_blks = sbi->blocks_per_seg * sbi->segs_per_sec;
332
+ unsigned int avg_vblocks;
333
+ unsigned int wanted_reserved_segments;
334
+ block_t avail_user_block_count;
335
+
336
+ if (!F2FS_IO_ALIGNED(sbi))
337
+ return 0;
338
+
339
+ /* average valid block count in section in worst case */
340
+ avg_vblocks = sec_blks / F2FS_IO_SIZE(sbi);
341
+
342
+ /*
343
+ * we need enough free space when migrating one section in worst case
344
+ */
345
+ wanted_reserved_segments = (F2FS_IO_SIZE(sbi) / avg_vblocks) *
346
+ reserved_segments(sbi);
347
+ wanted_reserved_segments -= reserved_segments(sbi);
348
+
349
+ avail_user_block_count = sbi->user_block_count -
350
+ sbi->current_reserved_blocks -
351
+ F2FS_OPTION(sbi).root_reserved_blocks;
352
+
353
+ if (wanted_reserved_segments * sbi->blocks_per_seg >
354
+ avail_user_block_count) {
355
+ f2fs_err(sbi, "IO align feature can't grab additional reserved segment: %u, available segments: %u",
356
+ wanted_reserved_segments,
357
+ avail_user_block_count >> sbi->log_blocks_per_seg);
358
+ return -ENOSPC;
359
+ }
360
+
361
+ SM_I(sbi)->additional_reserved_segments = wanted_reserved_segments;
362
+
363
+ f2fs_info(sbi, "IO align feature needs additional reserved segment: %u",
364
+ wanted_reserved_segments);
365
+
366
+ return 0;
288367 }
289368
290369 static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
....@@ -350,7 +429,7 @@
350429 set_opt(sbi, QUOTA);
351430 return 0;
352431 errout:
353
- kvfree(qname);
432
+ kfree(qname);
354433 return ret;
355434 }
356435
....@@ -362,7 +441,7 @@
362441 f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
363442 return -EINVAL;
364443 }
365
- kvfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
444
+ kfree(F2FS_OPTION(sbi).s_qf_names[qtype]);
366445 F2FS_OPTION(sbi).s_qf_names[qtype] = NULL;
367446 return 0;
368447 }
....@@ -433,12 +512,12 @@
433512 * needed to allow it to be set or changed during remount. We do allow
434513 * it to be specified during remount, but only if there is no change.
435514 */
436
- if (is_remount && !F2FS_OPTION(sbi).dummy_enc_ctx.ctx) {
515
+ if (is_remount && !F2FS_OPTION(sbi).dummy_enc_policy.policy) {
437516 f2fs_warn(sbi, "Can't set test_dummy_encryption on remount");
438517 return -EINVAL;
439518 }
440519 err = fscrypt_set_test_dummy_encryption(
441
- sb, arg, &F2FS_OPTION(sbi).dummy_enc_ctx);
520
+ sb, arg->from, &F2FS_OPTION(sbi).dummy_enc_policy);
442521 if (err) {
443522 if (err == -EEXIST)
444523 f2fs_warn(sbi,
....@@ -458,22 +537,94 @@
458537 return 0;
459538 }
460539
540
+#ifdef CONFIG_F2FS_FS_COMPRESSION
541
+#ifdef CONFIG_F2FS_FS_LZ4
542
+static int f2fs_set_lz4hc_level(struct f2fs_sb_info *sbi, const char *str)
543
+{
544
+#ifdef CONFIG_F2FS_FS_LZ4HC
545
+ unsigned int level;
546
+#endif
547
+
548
+ if (strlen(str) == 3) {
549
+ F2FS_OPTION(sbi).compress_level = 0;
550
+ return 0;
551
+ }
552
+
553
+#ifdef CONFIG_F2FS_FS_LZ4HC
554
+ str += 3;
555
+
556
+ if (str[0] != ':') {
557
+ f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
558
+ return -EINVAL;
559
+ }
560
+ if (kstrtouint(str + 1, 10, &level))
561
+ return -EINVAL;
562
+
563
+ if (level < LZ4HC_MIN_CLEVEL || level > LZ4HC_MAX_CLEVEL) {
564
+ f2fs_info(sbi, "invalid lz4hc compress level: %d", level);
565
+ return -EINVAL;
566
+ }
567
+
568
+ F2FS_OPTION(sbi).compress_level = level;
569
+ return 0;
570
+#else
571
+ f2fs_info(sbi, "kernel doesn't support lz4hc compression");
572
+ return -EINVAL;
573
+#endif
574
+}
575
+#endif
576
+
577
+#ifdef CONFIG_F2FS_FS_ZSTD
578
+static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
579
+{
580
+ unsigned int level;
581
+ int len = 4;
582
+
583
+ if (strlen(str) == len) {
584
+ F2FS_OPTION(sbi).compress_level = 0;
585
+ return 0;
586
+ }
587
+
588
+ str += len;
589
+
590
+ if (str[0] != ':') {
591
+ f2fs_info(sbi, "wrong format, e.g. <alg_name>:<compr_level>");
592
+ return -EINVAL;
593
+ }
594
+ if (kstrtouint(str + 1, 10, &level))
595
+ return -EINVAL;
596
+
597
+ if (!level || level > ZSTD_maxCLevel()) {
598
+ f2fs_info(sbi, "invalid zstd compress level: %d", level);
599
+ return -EINVAL;
600
+ }
601
+
602
+ F2FS_OPTION(sbi).compress_level = level;
603
+ return 0;
604
+}
605
+#endif
606
+#endif
607
+
461608 static int parse_options(struct super_block *sb, char *options, bool is_remount)
462609 {
463610 struct f2fs_sb_info *sbi = F2FS_SB(sb);
464611 substring_t args[MAX_OPT_ARGS];
612
+#ifdef CONFIG_F2FS_FS_COMPRESSION
465613 unsigned char (*ext)[F2FS_EXTENSION_LEN];
614
+ int ext_cnt;
615
+#endif
466616 char *p, *name;
467
- int arg = 0, ext_cnt;
617
+ int arg = 0;
468618 kuid_t uid;
469619 kgid_t gid;
470620 int ret;
471621
472622 if (!options)
473
- return 0;
623
+ goto default_check;
474624
475625 while ((p = strsep(&options, ",")) != NULL) {
476626 int token;
627
+
477628 if (!*p)
478629 continue;
479630 /*
....@@ -496,10 +647,10 @@
496647 } else if (!strcmp(name, "sync")) {
497648 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_SYNC;
498649 } else {
499
- kvfree(name);
650
+ kfree(name);
500651 return -EINVAL;
501652 }
502
- kvfree(name);
653
+ kfree(name);
503654 break;
504655 case Opt_disable_roll_forward:
505656 set_opt(sbi, DISABLE_ROLL_FORWARD);
....@@ -577,7 +728,8 @@
577728 case Opt_active_logs:
578729 if (args->from && match_int(args, &arg))
579730 return -EINVAL;
580
- if (arg != 2 && arg != 4 && arg != NR_CURSEG_TYPE)
731
+ if (arg != 2 && arg != 4 &&
732
+ arg != NR_CURSEG_PERSIST_TYPE)
581733 return -EINVAL;
582734 F2FS_OPTION(sbi).active_logs = arg;
583735 break;
....@@ -606,10 +758,10 @@
606758 set_opt(sbi, FASTBOOT);
607759 break;
608760 case Opt_extent_cache:
609
- set_opt(sbi, EXTENT_CACHE);
761
+ set_opt(sbi, READ_EXTENT_CACHE);
610762 break;
611763 case Opt_noextent_cache:
612
- clear_opt(sbi, EXTENT_CACHE);
764
+ clear_opt(sbi, READ_EXTENT_CACHE);
613765 break;
614766 case Opt_noinline_data:
615767 clear_opt(sbi, INLINE_DATA);
....@@ -656,17 +808,17 @@
656808 if (!strcmp(name, "adaptive")) {
657809 if (f2fs_sb_has_blkzoned(sbi)) {
658810 f2fs_warn(sbi, "adaptive mode is not allowed with zoned block device feature");
659
- kvfree(name);
811
+ kfree(name);
660812 return -EINVAL;
661813 }
662814 F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
663815 } else if (!strcmp(name, "lfs")) {
664816 F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
665817 } else {
666
- kvfree(name);
818
+ kfree(name);
667819 return -EINVAL;
668820 }
669
- kvfree(name);
821
+ kfree(name);
670822 break;
671823 case Opt_io_size_bits:
672824 if (args->from && match_int(args, &arg))
....@@ -792,10 +944,10 @@
792944 } else if (!strcmp(name, "fs-based")) {
793945 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_FS;
794946 } else {
795
- kvfree(name);
947
+ kfree(name);
796948 return -EINVAL;
797949 }
798
- kvfree(name);
950
+ kfree(name);
799951 break;
800952 case Opt_alloc:
801953 name = match_strdup(&args[0]);
....@@ -807,10 +959,10 @@
807959 } else if (!strcmp(name, "reuse")) {
808960 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
809961 } else {
810
- kvfree(name);
962
+ kfree(name);
811963 return -EINVAL;
812964 }
813
- kvfree(name);
965
+ kfree(name);
814966 break;
815967 case Opt_fsync:
816968 name = match_strdup(&args[0]);
....@@ -824,10 +976,10 @@
824976 F2FS_OPTION(sbi).fsync_mode =
825977 FSYNC_MODE_NOBARRIER;
826978 } else {
827
- kvfree(name);
979
+ kfree(name);
828980 return -EINVAL;
829981 }
830
- kvfree(name);
982
+ kfree(name);
831983 break;
832984 case Opt_test_dummy_encryption:
833985 ret = f2fs_set_test_dummy_encryption(sb, p, &args[0],
....@@ -837,7 +989,7 @@
837989 break;
838990 case Opt_inlinecrypt:
839991 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
840
- F2FS_OPTION(sbi).inlinecrypt = true;
992
+ sb->s_flags |= SB_INLINECRYPT;
841993 #else
842994 f2fs_info(sbi, "inline encryption not supported");
843995 #endif
....@@ -862,23 +1014,61 @@
8621014 case Opt_checkpoint_enable:
8631015 clear_opt(sbi, DISABLE_CHECKPOINT);
8641016 break;
1017
+ case Opt_checkpoint_merge:
1018
+ set_opt(sbi, MERGE_CHECKPOINT);
1019
+ break;
1020
+ case Opt_nocheckpoint_merge:
1021
+ clear_opt(sbi, MERGE_CHECKPOINT);
1022
+ break;
1023
+#ifdef CONFIG_F2FS_FS_COMPRESSION
8651024 case Opt_compress_algorithm:
8661025 if (!f2fs_sb_has_compression(sbi)) {
867
- f2fs_err(sbi, "Compression feature if off");
868
- return -EINVAL;
1026
+ f2fs_info(sbi, "Image doesn't support compression");
1027
+ break;
8691028 }
8701029 name = match_strdup(&args[0]);
8711030 if (!name)
8721031 return -ENOMEM;
8731032 if (!strcmp(name, "lzo")) {
1033
+#ifdef CONFIG_F2FS_FS_LZO
1034
+ F2FS_OPTION(sbi).compress_level = 0;
8741035 F2FS_OPTION(sbi).compress_algorithm =
8751036 COMPRESS_LZO;
876
- } else if (!strcmp(name, "lz4")) {
1037
+#else
1038
+ f2fs_info(sbi, "kernel doesn't support lzo compression");
1039
+#endif
1040
+ } else if (!strncmp(name, "lz4", 3)) {
1041
+#ifdef CONFIG_F2FS_FS_LZ4
1042
+ ret = f2fs_set_lz4hc_level(sbi, name);
1043
+ if (ret) {
1044
+ kfree(name);
1045
+ return -EINVAL;
1046
+ }
8771047 F2FS_OPTION(sbi).compress_algorithm =
8781048 COMPRESS_LZ4;
879
- } else if (!strcmp(name, "zstd")) {
1049
+#else
1050
+ f2fs_info(sbi, "kernel doesn't support lz4 compression");
1051
+#endif
1052
+ } else if (!strncmp(name, "zstd", 4)) {
1053
+#ifdef CONFIG_F2FS_FS_ZSTD
1054
+ ret = f2fs_set_zstd_level(sbi, name);
1055
+ if (ret) {
1056
+ kfree(name);
1057
+ return -EINVAL;
1058
+ }
8801059 F2FS_OPTION(sbi).compress_algorithm =
8811060 COMPRESS_ZSTD;
1061
+#else
1062
+ f2fs_info(sbi, "kernel doesn't support zstd compression");
1063
+#endif
1064
+ } else if (!strcmp(name, "lzo-rle")) {
1065
+#ifdef CONFIG_F2FS_FS_LZORLE
1066
+ F2FS_OPTION(sbi).compress_level = 0;
1067
+ F2FS_OPTION(sbi).compress_algorithm =
1068
+ COMPRESS_LZORLE;
1069
+#else
1070
+ f2fs_info(sbi, "kernel doesn't support lzorle compression");
1071
+#endif
8821072 } else {
8831073 kfree(name);
8841074 return -EINVAL;
....@@ -887,8 +1077,8 @@
8871077 break;
8881078 case Opt_compress_log_size:
8891079 if (!f2fs_sb_has_compression(sbi)) {
890
- f2fs_err(sbi, "Compression feature is off");
891
- return -EINVAL;
1080
+ f2fs_info(sbi, "Image doesn't support compression");
1081
+ break;
8921082 }
8931083 if (args->from && match_int(args, &arg))
8941084 return -EINVAL;
....@@ -902,8 +1092,8 @@
9021092 break;
9031093 case Opt_compress_extension:
9041094 if (!f2fs_sb_has_compression(sbi)) {
905
- f2fs_err(sbi, "Compression feature is off");
906
- return -EINVAL;
1095
+ f2fs_info(sbi, "Image doesn't support compression");
1096
+ break;
9071097 }
9081098 name = match_strdup(&args[0]);
9091099 if (!name)
....@@ -924,12 +1114,71 @@
9241114 F2FS_OPTION(sbi).compress_ext_cnt++;
9251115 kfree(name);
9261116 break;
1117
+ case Opt_compress_chksum:
1118
+ F2FS_OPTION(sbi).compress_chksum = true;
1119
+ break;
1120
+ case Opt_compress_mode:
1121
+ name = match_strdup(&args[0]);
1122
+ if (!name)
1123
+ return -ENOMEM;
1124
+ if (!strcmp(name, "fs")) {
1125
+ F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
1126
+ } else if (!strcmp(name, "user")) {
1127
+ F2FS_OPTION(sbi).compress_mode = COMPR_MODE_USER;
1128
+ } else {
1129
+ kfree(name);
1130
+ return -EINVAL;
1131
+ }
1132
+ kfree(name);
1133
+ break;
1134
+ case Opt_compress_cache:
1135
+ set_opt(sbi, COMPRESS_CACHE);
1136
+ break;
1137
+#else
1138
+ case Opt_compress_algorithm:
1139
+ case Opt_compress_log_size:
1140
+ case Opt_compress_extension:
1141
+ case Opt_compress_chksum:
1142
+ case Opt_compress_mode:
1143
+ case Opt_compress_cache:
1144
+ f2fs_info(sbi, "compression options not supported");
1145
+ break;
1146
+#endif
1147
+ case Opt_atgc:
1148
+ set_opt(sbi, ATGC);
1149
+ break;
1150
+ case Opt_gc_merge:
1151
+ set_opt(sbi, GC_MERGE);
1152
+ break;
1153
+ case Opt_nogc_merge:
1154
+ clear_opt(sbi, GC_MERGE);
1155
+ break;
1156
+ case Opt_age_extent_cache:
1157
+ set_opt(sbi, AGE_EXTENT_CACHE);
1158
+ break;
1159
+ case Opt_memory_mode:
1160
+ name = match_strdup(&args[0]);
1161
+ if (!name)
1162
+ return -ENOMEM;
1163
+ if (!strcmp(name, "normal")) {
1164
+ F2FS_OPTION(sbi).memory_mode =
1165
+ MEMORY_MODE_NORMAL;
1166
+ } else if (!strcmp(name, "low")) {
1167
+ F2FS_OPTION(sbi).memory_mode =
1168
+ MEMORY_MODE_LOW;
1169
+ } else {
1170
+ kfree(name);
1171
+ return -EINVAL;
1172
+ }
1173
+ kfree(name);
1174
+ break;
9271175 default:
9281176 f2fs_err(sbi, "Unrecognized mount option \"%s\" or missing value",
9291177 p);
9301178 return -EINVAL;
9311179 }
9321180 }
1181
+default_check:
9331182 #ifdef CONFIG_QUOTA
9341183 if (f2fs_check_quota_options(sbi))
9351184 return -EINVAL;
....@@ -947,6 +1196,17 @@
9471196 if (f2fs_sb_has_casefold(sbi)) {
9481197 f2fs_err(sbi,
9491198 "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
1199
+ return -EINVAL;
1200
+ }
1201
+#endif
1202
+ /*
1203
+ * The BLKZONED feature indicates that the drive was formatted with
1204
+ * zone alignment optimization. This is optional for host-aware
1205
+ * devices, but mandatory for host-managed zoned block devices.
1206
+ */
1207
+#ifndef CONFIG_BLK_DEV_ZONED
1208
+ if (f2fs_sb_has_blkzoned(sbi)) {
1209
+ f2fs_err(sbi, "Zoned block device support is not enabled");
9501210 return -EINVAL;
9511211 }
9521212 #endif
....@@ -982,15 +1242,20 @@
9821242 }
9831243
9841244 if (test_opt(sbi, DISABLE_CHECKPOINT) && f2fs_lfs_mode(sbi)) {
985
- f2fs_err(sbi, "LFS not compatible with checkpoint=disable\n");
1245
+ f2fs_err(sbi, "LFS not compatible with checkpoint=disable");
9861246 return -EINVAL;
9871247 }
9881248
9891249 /* Not pass down write hints if the number of active logs is lesser
990
- * than NR_CURSEG_TYPE.
1250
+ * than NR_CURSEG_PERSIST_TYPE.
9911251 */
992
- if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_TYPE)
1252
+ if (F2FS_OPTION(sbi).active_logs != NR_CURSEG_PERSIST_TYPE)
9931253 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
1254
+
1255
+ if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
1256
+ f2fs_err(sbi, "Allow to mount readonly mode only");
1257
+ return -EROFS;
1258
+ }
9941259 return 0;
9951260 }
9961261
....@@ -1006,17 +1271,18 @@
10061271
10071272 /* Initialize f2fs-specific inode info */
10081273 atomic_set(&fi->dirty_pages, 0);
1009
- init_rwsem(&fi->i_sem);
1274
+ atomic_set(&fi->i_compr_blocks, 0);
1275
+ init_f2fs_rwsem(&fi->i_sem);
10101276 spin_lock_init(&fi->i_size_lock);
10111277 INIT_LIST_HEAD(&fi->dirty_list);
10121278 INIT_LIST_HEAD(&fi->gdirty_list);
10131279 INIT_LIST_HEAD(&fi->inmem_ilist);
10141280 INIT_LIST_HEAD(&fi->inmem_pages);
10151281 mutex_init(&fi->inmem_lock);
1016
- init_rwsem(&fi->i_gc_rwsem[READ]);
1017
- init_rwsem(&fi->i_gc_rwsem[WRITE]);
1018
- init_rwsem(&fi->i_mmap_sem);
1019
- init_rwsem(&fi->i_xattr_sem);
1282
+ init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
1283
+ init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
1284
+ init_f2fs_rwsem(&fi->i_mmap_sem);
1285
+ init_f2fs_rwsem(&fi->i_xattr_sem);
10201286
10211287 /* Will be used by directory only */
10221288 fi->i_dir_level = F2FS_SB(sb)->dir_level;
....@@ -1148,18 +1414,10 @@
11481414 f2fs_inode_dirtied(inode, false);
11491415 }
11501416
1151
-static void f2fs_i_callback(struct rcu_head *head)
1417
+static void f2fs_free_inode(struct inode *inode)
11521418 {
1153
- struct inode *inode = container_of(head, struct inode, i_rcu);
1154
-
11551419 fscrypt_free_inode(inode);
1156
-
11571420 kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
1158
-}
1159
-
1160
-static void f2fs_destroy_inode(struct inode *inode)
1161
-{
1162
- call_rcu(&inode->i_rcu, f2fs_i_callback);
11631421 }
11641422
11651423 static void destroy_percpu_info(struct f2fs_sb_info *sbi)
....@@ -1176,6 +1434,7 @@
11761434 blkdev_put(FDEV(i).bdev, FMODE_EXCL);
11771435 #ifdef CONFIG_BLK_DEV_ZONED
11781436 kvfree(FDEV(i).blkz_seq);
1437
+ kfree(FDEV(i).zone_capacity_blocks);
11791438 #endif
11801439 }
11811440 kvfree(sbi->devs);
....@@ -1194,6 +1453,12 @@
11941453
11951454 /* prevent remaining shrinker jobs */
11961455 mutex_lock(&sbi->umount_mutex);
1456
+
1457
+ /*
1458
+ * flush all issued checkpoints and stop checkpoint issue thread.
1459
+ * after then, all checkpoints should be done by each process context.
1460
+ */
1461
+ f2fs_stop_ckpt_thread(sbi);
11971462
11981463 /*
11991464 * We don't need to do checkpoint when superblock is clean.
....@@ -1235,6 +1500,8 @@
12351500
12361501 f2fs_bug_on(sbi, sbi->fsync_node_num);
12371502
1503
+ f2fs_destroy_compress_inode(sbi);
1504
+
12381505 iput(sbi->node_inode);
12391506 sbi->node_inode = NULL;
12401507
....@@ -1258,23 +1525,24 @@
12581525 sb->s_fs_info = NULL;
12591526 if (sbi->s_chksum_driver)
12601527 crypto_free_shash(sbi->s_chksum_driver);
1261
- kvfree(sbi->raw_super);
1528
+ kfree(sbi->raw_super);
12621529
12631530 destroy_device_list(sbi);
1531
+ f2fs_destroy_page_array_cache(sbi);
12641532 f2fs_destroy_xattr_caches(sbi);
12651533 mempool_destroy(sbi->write_io_dummy);
12661534 #ifdef CONFIG_QUOTA
12671535 for (i = 0; i < MAXQUOTAS; i++)
1268
- kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
1536
+ kfree(F2FS_OPTION(sbi).s_qf_names[i]);
12691537 #endif
1270
- fscrypt_free_dummy_context(&F2FS_OPTION(sbi).dummy_enc_ctx);
1538
+ fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
12711539 destroy_percpu_info(sbi);
12721540 for (i = 0; i < NR_PAGE_TYPE; i++)
12731541 kvfree(sbi->write_io[i]);
12741542 #ifdef CONFIG_UNICODE
12751543 utf8_unload(sb->s_encoding);
12761544 #endif
1277
- kvfree(sbi);
1545
+ kfree(sbi);
12781546 }
12791547
12801548 int f2fs_sync_fs(struct super_block *sb, int sync)
....@@ -1293,16 +1561,9 @@
12931561 return -EAGAIN;
12941562
12951563 if (sync) {
1296
- struct cp_control cpc;
1297
-
1298
- cpc.reason = __get_cp_reason(sbi);
1299
-
1564
+ err = f2fs_issue_checkpoint(sbi);
13001565 atomic_set(&sbi->no_cp_fsync_pages, 0);
1301
- down_write(&sbi->gc_lock);
1302
- err = f2fs_write_checkpoint(sbi, &cpc);
1303
- up_write(&sbi->gc_lock);
13041566 }
1305
- f2fs_trace_ios(NULL, 1);
13061567
13071568 return err;
13081569 }
....@@ -1319,11 +1580,18 @@
13191580 /* must be clean, since sync_filesystem() was already called */
13201581 if (is_sbi_flag_set(F2FS_SB(sb), SBI_IS_DIRTY))
13211582 return -EINVAL;
1583
+
1584
+ /* Let's flush checkpoints and stop the thread. */
1585
+ f2fs_flush_ckpt_thread(F2FS_SB(sb));
1586
+
1587
+ /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
1588
+ set_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
13221589 return 0;
13231590 }
13241591
13251592 static int f2fs_unfreeze(struct super_block *sb)
13261593 {
1594
+ clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
13271595 return 0;
13281596 }
13291597
....@@ -1416,8 +1684,7 @@
14161684 }
14171685
14181686 buf->f_namelen = F2FS_NAME_LEN;
1419
- buf->f_fsid.val[0] = (u32)id;
1420
- buf->f_fsid.val[1] = (u32)(id >> 32);
1687
+ buf->f_fsid = u64_to_fsid(id);
14211688
14221689 #ifdef CONFIG_QUOTA
14231690 if (is_inode_flag_set(dentry->d_inode, FI_PROJ_INHERIT) &&
....@@ -1465,6 +1732,7 @@
14651732 #endif
14661733 }
14671734
1735
+#ifdef CONFIG_F2FS_FS_COMPRESSION
14681736 static inline void f2fs_show_compress_options(struct seq_file *seq,
14691737 struct super_block *sb)
14701738 {
....@@ -1485,8 +1753,14 @@
14851753 case COMPRESS_ZSTD:
14861754 algtype = "zstd";
14871755 break;
1756
+ case COMPRESS_LZORLE:
1757
+ algtype = "lzo-rle";
1758
+ break;
14881759 }
14891760 seq_printf(seq, ",compress_algorithm=%s", algtype);
1761
+
1762
+ if (F2FS_OPTION(sbi).compress_level)
1763
+ seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
14901764
14911765 seq_printf(seq, ",compress_log_size=%u",
14921766 F2FS_OPTION(sbi).compress_log_size);
....@@ -1495,7 +1769,19 @@
14951769 seq_printf(seq, ",compress_extension=%s",
14961770 F2FS_OPTION(sbi).extensions[i]);
14971771 }
1772
+
1773
+ if (F2FS_OPTION(sbi).compress_chksum)
1774
+ seq_puts(seq, ",compress_chksum");
1775
+
1776
+ if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
1777
+ seq_printf(seq, ",compress_mode=%s", "fs");
1778
+ else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
1779
+ seq_printf(seq, ",compress_mode=%s", "user");
1780
+
1781
+ if (test_opt(sbi, COMPRESS_CACHE))
1782
+ seq_puts(seq, ",compress_cache");
14981783 }
1784
+#endif
14991785
15001786 static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
15011787 {
....@@ -1507,6 +1793,9 @@
15071793 seq_printf(seq, ",background_gc=%s", "on");
15081794 else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
15091795 seq_printf(seq, ",background_gc=%s", "off");
1796
+
1797
+ if (test_opt(sbi, GC_MERGE))
1798
+ seq_puts(seq, ",gc_merge");
15101799
15111800 if (test_opt(sbi, DISABLE_ROLL_FORWARD))
15121801 seq_puts(seq, ",disable_roll_forward");
....@@ -1555,10 +1844,12 @@
15551844 seq_puts(seq, ",nobarrier");
15561845 if (test_opt(sbi, FASTBOOT))
15571846 seq_puts(seq, ",fastboot");
1558
- if (test_opt(sbi, EXTENT_CACHE))
1847
+ if (test_opt(sbi, READ_EXTENT_CACHE))
15591848 seq_puts(seq, ",extent_cache");
15601849 else
15611850 seq_puts(seq, ",noextent_cache");
1851
+ if (test_opt(sbi, AGE_EXTENT_CACHE))
1852
+ seq_puts(seq, ",age_extent_cache");
15621853 if (test_opt(sbi, DATA_FLUSH))
15631854 seq_puts(seq, ",data_flush");
15641855
....@@ -1604,10 +1895,8 @@
16041895
16051896 fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
16061897
1607
-#ifdef CONFIG_FS_ENCRYPTION
1608
- if (F2FS_OPTION(sbi).inlinecrypt)
1898
+ if (sbi->sb->s_flags & SB_INLINECRYPT)
16091899 seq_puts(seq, ",inlinecrypt");
1610
-#endif
16111900
16121901 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
16131902 seq_printf(seq, ",alloc_mode=%s", "default");
....@@ -1617,6 +1906,10 @@
16171906 if (test_opt(sbi, DISABLE_CHECKPOINT))
16181907 seq_printf(seq, ",checkpoint=disable:%u",
16191908 F2FS_OPTION(sbi).unusable_cap);
1909
+ if (test_opt(sbi, MERGE_CHECKPOINT))
1910
+ seq_puts(seq, ",checkpoint_merge");
1911
+ else
1912
+ seq_puts(seq, ",nocheckpoint_merge");
16201913 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
16211914 seq_printf(seq, ",fsync_mode=%s", "posix");
16221915 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
....@@ -1624,34 +1917,51 @@
16241917 else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
16251918 seq_printf(seq, ",fsync_mode=%s", "nobarrier");
16261919
1920
+#ifdef CONFIG_F2FS_FS_COMPRESSION
16271921 f2fs_show_compress_options(seq, sbi->sb);
1922
+#endif
1923
+
1924
+ if (test_opt(sbi, ATGC))
1925
+ seq_puts(seq, ",atgc");
1926
+
1927
+ if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
1928
+ seq_printf(seq, ",memory=%s", "normal");
1929
+ else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
1930
+ seq_printf(seq, ",memory=%s", "low");
1931
+
16281932 return 0;
16291933 }
16301934
16311935 static void default_options(struct f2fs_sb_info *sbi)
16321936 {
16331937 /* init some FS parameters */
1634
- F2FS_OPTION(sbi).active_logs = NR_CURSEG_TYPE;
1938
+ if (f2fs_sb_has_readonly(sbi))
1939
+ F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
1940
+ else
1941
+ F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
1942
+
16351943 F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
16361944 F2FS_OPTION(sbi).whint_mode = WHINT_MODE_OFF;
16371945 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
16381946 F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
1639
-#ifdef CONFIG_FS_ENCRYPTION
1640
- F2FS_OPTION(sbi).inlinecrypt = false;
1641
-#endif
16421947 F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
16431948 F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
16441949 F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
16451950 F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
16461951 F2FS_OPTION(sbi).compress_ext_cnt = 0;
1952
+ F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
16471953 F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
1954
+ F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
1955
+
1956
+ sbi->sb->s_flags &= ~SB_INLINECRYPT;
16481957
16491958 set_opt(sbi, INLINE_XATTR);
16501959 set_opt(sbi, INLINE_DATA);
16511960 set_opt(sbi, INLINE_DENTRY);
1652
- set_opt(sbi, EXTENT_CACHE);
1961
+ set_opt(sbi, READ_EXTENT_CACHE);
16531962 set_opt(sbi, NOHEAP);
16541963 clear_opt(sbi, DISABLE_CHECKPOINT);
1964
+ set_opt(sbi, MERGE_CHECKPOINT);
16551965 F2FS_OPTION(sbi).unusable_cap = 0;
16561966 sbi->sb->s_flags |= SB_LAZYTIME;
16571967 set_opt(sbi, FLUSH_MERGE);
....@@ -1692,8 +2002,8 @@
16922002 f2fs_update_time(sbi, DISABLE_TIME);
16932003
16942004 while (!f2fs_time_over(sbi, DISABLE_TIME)) {
1695
- down_write(&sbi->gc_lock);
1696
- err = f2fs_gc(sbi, true, false, NULL_SEGNO);
2005
+ f2fs_down_write(&sbi->gc_lock);
2006
+ err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
16972007 if (err == -ENODATA) {
16982008 err = 0;
16992009 break;
....@@ -1704,7 +2014,7 @@
17042014
17052015 ret = sync_filesystem(sbi->sb);
17062016 if (ret || err) {
1707
- err = ret ? ret: err;
2017
+ err = ret ? ret : err;
17082018 goto restore_flag;
17092019 }
17102020
....@@ -1714,7 +2024,7 @@
17142024 goto restore_flag;
17152025 }
17162026
1717
- down_write(&sbi->gc_lock);
2027
+ f2fs_down_write(&sbi->gc_lock);
17182028 cpc.reason = CP_PAUSE;
17192029 set_sbi_flag(sbi, SBI_CP_DISABLED);
17202030 err = f2fs_write_checkpoint(sbi, &cpc);
....@@ -1726,7 +2036,7 @@
17262036 spin_unlock(&sbi->stat_lock);
17272037
17282038 out_unlock:
1729
- up_write(&sbi->gc_lock);
2039
+ f2fs_up_write(&sbi->gc_lock);
17302040 restore_flag:
17312041 sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
17322042 return err;
....@@ -1734,17 +2044,29 @@
17342044
17352045 static void f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
17362046 {
1737
- /* we should flush all the data to keep data consistency */
1738
- sync_inodes_sb(sbi->sb);
2047
+ int retry = DEFAULT_RETRY_IO_COUNT;
17392048
1740
- down_write(&sbi->gc_lock);
2049
+ /* we should flush all the data to keep data consistency */
2050
+ do {
2051
+ sync_inodes_sb(sbi->sb);
2052
+ cond_resched();
2053
+ congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2054
+ } while (get_pages(sbi, F2FS_DIRTY_DATA) && retry--);
2055
+
2056
+ if (unlikely(retry < 0))
2057
+ f2fs_warn(sbi, "checkpoint=enable has some unwritten data.");
2058
+
2059
+ f2fs_down_write(&sbi->gc_lock);
17412060 f2fs_dirty_to_prefree(sbi);
17422061
17432062 clear_sbi_flag(sbi, SBI_CP_DISABLED);
17442063 set_sbi_flag(sbi, SBI_IS_DIRTY);
1745
- up_write(&sbi->gc_lock);
2064
+ f2fs_up_write(&sbi->gc_lock);
17462065
17472066 f2fs_sync_fs(sbi->sb, 1);
2067
+
2068
+ /* Let's ensure there's no pending checkpoint anymore */
2069
+ f2fs_flush_ckpt_thread(sbi);
17482070 }
17492071
17502072 static int f2fs_remount(struct super_block *sb, int *flags, char *data)
....@@ -1753,11 +2075,15 @@
17532075 struct f2fs_mount_info org_mount_opt;
17542076 unsigned long old_sb_flags;
17552077 int err;
1756
- bool need_restart_gc = false;
1757
- bool need_stop_gc = false;
1758
- bool no_extent_cache = !test_opt(sbi, EXTENT_CACHE);
2078
+ bool need_restart_gc = false, need_stop_gc = false;
2079
+ bool need_restart_ckpt = false, need_stop_ckpt = false;
2080
+ bool need_restart_flush = false, need_stop_flush = false;
2081
+ bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
2082
+ bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
17592083 bool disable_checkpoint = test_opt(sbi, DISABLE_CHECKPOINT);
17602084 bool no_io_align = !F2FS_IO_ALIGNED(sbi);
2085
+ bool no_atgc = !test_opt(sbi, ATGC);
2086
+ bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
17612087 bool checkpoint_changed;
17622088 #ifdef CONFIG_QUOTA
17632089 int i, j;
....@@ -1779,7 +2105,7 @@
17792105 GFP_KERNEL);
17802106 if (!org_mount_opt.s_qf_names[i]) {
17812107 for (j = 0; j < i; j++)
1782
- kvfree(org_mount_opt.s_qf_names[j]);
2108
+ kfree(org_mount_opt.s_qf_names[j]);
17832109 return -ENOMEM;
17842110 }
17852111 } else {
....@@ -1813,6 +2139,11 @@
18132139 if (f2fs_readonly(sb) && (*flags & SB_RDONLY))
18142140 goto skip;
18152141
2142
+ if (f2fs_sb_has_readonly(sbi) && !(*flags & SB_RDONLY)) {
2143
+ err = -EROFS;
2144
+ goto restore_opts;
2145
+ }
2146
+
18162147 #ifdef CONFIG_QUOTA
18172148 if (!f2fs_readonly(sb) && (*flags & SB_RDONLY)) {
18182149 err = dquot_suspend(sb, -1);
....@@ -1830,16 +2161,35 @@
18302161 }
18312162 }
18322163 #endif
2164
+ /* disallow enable atgc dynamically */
2165
+ if (no_atgc == !!test_opt(sbi, ATGC)) {
2166
+ err = -EINVAL;
2167
+ f2fs_warn(sbi, "switch atgc option is not allowed");
2168
+ goto restore_opts;
2169
+ }
2170
+
18332171 /* disallow enable/disable extent_cache dynamically */
1834
- if (no_extent_cache == !!test_opt(sbi, EXTENT_CACHE)) {
2172
+ if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
18352173 err = -EINVAL;
18362174 f2fs_warn(sbi, "switch extent_cache option is not allowed");
2175
+ goto restore_opts;
2176
+ }
2177
+ /* disallow enable/disable age extent_cache dynamically */
2178
+ if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
2179
+ err = -EINVAL;
2180
+ f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
18372181 goto restore_opts;
18382182 }
18392183
18402184 if (no_io_align == !!F2FS_IO_ALIGNED(sbi)) {
18412185 err = -EINVAL;
18422186 f2fs_warn(sbi, "switch io_bits option is not allowed");
2187
+ goto restore_opts;
2188
+ }
2189
+
2190
+ if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
2191
+ err = -EINVAL;
2192
+ f2fs_warn(sbi, "switch compress_cache option is not allowed");
18432193 goto restore_opts;
18442194 }
18452195
....@@ -1855,7 +2205,8 @@
18552205 * option. Also sync the filesystem.
18562206 */
18572207 if ((*flags & SB_RDONLY) ||
1858
- F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF) {
2208
+ (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
2209
+ !test_opt(sbi, GC_MERGE))) {
18592210 if (sbi->gc_thread) {
18602211 f2fs_stop_gc_thread(sbi);
18612212 need_restart_gc = true;
....@@ -1869,7 +2220,6 @@
18692220
18702221 if (*flags & SB_RDONLY ||
18712222 F2FS_OPTION(sbi).whint_mode != org_mount_opt.whint_mode) {
1872
- writeback_inodes_sb(sb, WB_REASON_SYNC);
18732223 sync_inodes_sb(sb);
18742224
18752225 set_sbi_flag(sbi, SBI_IS_DIRTY);
....@@ -1878,14 +2228,22 @@
18782228 clear_sbi_flag(sbi, SBI_IS_CLOSE);
18792229 }
18802230
1881
- if (checkpoint_changed) {
1882
- if (test_opt(sbi, DISABLE_CHECKPOINT)) {
1883
- err = f2fs_disable_checkpoint(sbi);
1884
- if (err)
1885
- goto restore_gc;
1886
- } else {
1887
- f2fs_enable_checkpoint(sbi);
2231
+ if ((*flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
2232
+ !test_opt(sbi, MERGE_CHECKPOINT)) {
2233
+ f2fs_stop_ckpt_thread(sbi);
2234
+ need_restart_ckpt = true;
2235
+ } else {
2236
+ /* Flush if the prevous checkpoint, if exists. */
2237
+ f2fs_flush_ckpt_thread(sbi);
2238
+
2239
+ err = f2fs_start_ckpt_thread(sbi);
2240
+ if (err) {
2241
+ f2fs_err(sbi,
2242
+ "Failed to start F2FS issue_checkpoint_thread (%d)",
2243
+ err);
2244
+ goto restore_gc;
18882245 }
2246
+ need_stop_ckpt = true;
18892247 }
18902248
18912249 /*
....@@ -1895,16 +2253,29 @@
18952253 if ((*flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
18962254 clear_opt(sbi, FLUSH_MERGE);
18972255 f2fs_destroy_flush_cmd_control(sbi, false);
2256
+ need_restart_flush = true;
18982257 } else {
18992258 err = f2fs_create_flush_cmd_control(sbi);
19002259 if (err)
1901
- goto restore_gc;
2260
+ goto restore_ckpt;
2261
+ need_stop_flush = true;
19022262 }
2263
+
2264
+ if (checkpoint_changed) {
2265
+ if (test_opt(sbi, DISABLE_CHECKPOINT)) {
2266
+ err = f2fs_disable_checkpoint(sbi);
2267
+ if (err)
2268
+ goto restore_flush;
2269
+ } else {
2270
+ f2fs_enable_checkpoint(sbi);
2271
+ }
2272
+ }
2273
+
19032274 skip:
19042275 #ifdef CONFIG_QUOTA
19052276 /* Release old quota file names */
19062277 for (i = 0; i < MAXQUOTAS; i++)
1907
- kvfree(org_mount_opt.s_qf_names[i]);
2278
+ kfree(org_mount_opt.s_qf_names[i]);
19082279 #endif
19092280 /* Update the POSIXACL Flag */
19102281 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
....@@ -1914,6 +2285,21 @@
19142285 adjust_unusable_cap_perc(sbi);
19152286 *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
19162287 return 0;
2288
+restore_flush:
2289
+ if (need_restart_flush) {
2290
+ if (f2fs_create_flush_cmd_control(sbi))
2291
+ f2fs_warn(sbi, "background flush thread has stopped");
2292
+ } else if (need_stop_flush) {
2293
+ clear_opt(sbi, FLUSH_MERGE);
2294
+ f2fs_destroy_flush_cmd_control(sbi, false);
2295
+ }
2296
+restore_ckpt:
2297
+ if (need_restart_ckpt) {
2298
+ if (f2fs_start_ckpt_thread(sbi))
2299
+ f2fs_warn(sbi, "background ckpt thread has stopped");
2300
+ } else if (need_stop_ckpt) {
2301
+ f2fs_stop_ckpt_thread(sbi);
2302
+ }
19172303 restore_gc:
19182304 if (need_restart_gc) {
19192305 if (f2fs_start_gc_thread(sbi))
....@@ -1925,7 +2311,7 @@
19252311 #ifdef CONFIG_QUOTA
19262312 F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
19272313 for (i = 0; i < MAXQUOTAS; i++) {
1928
- kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
2314
+ kfree(F2FS_OPTION(sbi).s_qf_names[i]);
19292315 F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
19302316 }
19312317 #endif
....@@ -2116,7 +2502,7 @@
21162502
21172503 /* Don't account quota for quota files to avoid recursion */
21182504 qf_inode->i_flags |= S_NOQUOTA;
2119
- err = dquot_enable(qf_inode, type, format_id, flags);
2505
+ err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
21202506 iput(qf_inode);
21212507 return err;
21222508 }
....@@ -2159,64 +2545,78 @@
21592545 return 0;
21602546 }
21612547
2548
+static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
2549
+{
2550
+ struct quota_info *dqopt = sb_dqopt(sbi->sb);
2551
+ struct address_space *mapping = dqopt->files[type]->i_mapping;
2552
+ int ret = 0;
2553
+
2554
+ ret = dquot_writeback_dquots(sbi->sb, type);
2555
+ if (ret)
2556
+ goto out;
2557
+
2558
+ ret = filemap_fdatawrite(mapping);
2559
+ if (ret)
2560
+ goto out;
2561
+
2562
+ /* if we are using journalled quota */
2563
+ if (is_journalled_quota(sbi))
2564
+ goto out;
2565
+
2566
+ ret = filemap_fdatawait(mapping);
2567
+
2568
+ truncate_inode_pages(&dqopt->files[type]->i_data, 0);
2569
+out:
2570
+ if (ret)
2571
+ set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2572
+ return ret;
2573
+}
2574
+
21622575 int f2fs_quota_sync(struct super_block *sb, int type)
21632576 {
21642577 struct f2fs_sb_info *sbi = F2FS_SB(sb);
21652578 struct quota_info *dqopt = sb_dqopt(sb);
21662579 int cnt;
2167
- int ret;
2168
-
2169
- /*
2170
- * do_quotactl
2171
- * f2fs_quota_sync
2172
- * down_read(quota_sem)
2173
- * dquot_writeback_dquots()
2174
- * f2fs_dquot_commit
2175
- * block_operation
2176
- * down_read(quota_sem)
2177
- */
2178
- f2fs_lock_op(sbi);
2179
-
2180
- down_read(&sbi->quota_sem);
2181
- ret = dquot_writeback_dquots(sb, type);
2182
- if (ret)
2183
- goto out;
2580
+ int ret = 0;
21842581
21852582 /*
21862583 * Now when everything is written we can discard the pagecache so
21872584 * that userspace sees the changes.
21882585 */
21892586 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2190
- struct address_space *mapping;
21912587
21922588 if (type != -1 && cnt != type)
21932589 continue;
2590
+
21942591 if (!sb_has_quota_active(sb, cnt))
21952592 continue;
21962593
2197
- mapping = dqopt->files[cnt]->i_mapping;
2594
+ if (!f2fs_sb_has_quota_ino(sbi))
2595
+ inode_lock(dqopt->files[cnt]);
21982596
2199
- ret = filemap_fdatawrite(mapping);
2597
+ /*
2598
+ * do_quotactl
2599
+ * f2fs_quota_sync
2600
+ * f2fs_down_read(quota_sem)
2601
+ * dquot_writeback_dquots()
2602
+ * f2fs_dquot_commit
2603
+ * block_operation
2604
+ * f2fs_down_read(quota_sem)
2605
+ */
2606
+ f2fs_lock_op(sbi);
2607
+ f2fs_down_read(&sbi->quota_sem);
2608
+
2609
+ ret = f2fs_quota_sync_file(sbi, cnt);
2610
+
2611
+ f2fs_up_read(&sbi->quota_sem);
2612
+ f2fs_unlock_op(sbi);
2613
+
2614
+ if (!f2fs_sb_has_quota_ino(sbi))
2615
+ inode_unlock(dqopt->files[cnt]);
2616
+
22002617 if (ret)
2201
- goto out;
2202
-
2203
- /* if we are using journalled quota */
2204
- if (is_journalled_quota(sbi))
2205
- continue;
2206
-
2207
- ret = filemap_fdatawait(mapping);
2208
- if (ret)
2209
- set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2210
-
2211
- inode_lock(dqopt->files[cnt]);
2212
- truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2213
- inode_unlock(dqopt->files[cnt]);
2618
+ break;
22142619 }
2215
-out:
2216
- if (ret)
2217
- set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
2218
- up_read(&sbi->quota_sem);
2219
- f2fs_unlock_op(sbi);
22202620 return ret;
22212621 }
22222622
....@@ -2334,11 +2734,11 @@
23342734 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
23352735 int ret;
23362736
2337
- down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
2737
+ f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
23382738 ret = dquot_commit(dquot);
23392739 if (ret < 0)
23402740 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2341
- up_read(&sbi->quota_sem);
2741
+ f2fs_up_read(&sbi->quota_sem);
23422742 return ret;
23432743 }
23442744
....@@ -2347,11 +2747,11 @@
23472747 struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
23482748 int ret;
23492749
2350
- down_read(&sbi->quota_sem);
2750
+ f2fs_down_read(&sbi->quota_sem);
23512751 ret = dquot_acquire(dquot);
23522752 if (ret < 0)
23532753 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
2354
- up_read(&sbi->quota_sem);
2754
+ f2fs_up_read(&sbi->quota_sem);
23552755 return ret;
23562756 }
23572757
....@@ -2430,8 +2830,8 @@
24302830
24312831 static const struct super_operations f2fs_sops = {
24322832 .alloc_inode = f2fs_alloc_inode,
2833
+ .free_inode = f2fs_free_inode,
24332834 .drop_inode = f2fs_drop_inode,
2434
- .destroy_inode = f2fs_destroy_inode,
24352835 .write_inode = f2fs_write_inode,
24362836 .dirty_inode = f2fs_dirty_inode,
24372837 .show_options = f2fs_show_options,
....@@ -2477,10 +2877,9 @@
24772877 ctx, len, fs_data, XATTR_CREATE);
24782878 }
24792879
2480
-static const union fscrypt_context *
2481
-f2fs_get_dummy_context(struct super_block *sb)
2880
+static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
24822881 {
2483
- return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_ctx.ctx;
2882
+ return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
24842883 }
24852884
24862885 static bool f2fs_has_stable_inodes(struct super_block *sb)
....@@ -2493,11 +2892,6 @@
24932892 {
24942893 *ino_bits_ret = 8 * sizeof(nid_t);
24952894 *lblk_bits_ret = 8 * sizeof(block_t);
2496
-}
2497
-
2498
-static bool f2fs_inline_crypt_enabled(struct super_block *sb)
2499
-{
2500
- return F2FS_OPTION(F2FS_SB(sb)).inlinecrypt;
25012895 }
25022896
25032897 static int f2fs_get_num_devices(struct super_block *sb)
....@@ -2523,12 +2917,11 @@
25232917 .key_prefix = "f2fs:",
25242918 .get_context = f2fs_get_context,
25252919 .set_context = f2fs_set_context,
2526
- .get_dummy_context = f2fs_get_dummy_context,
2920
+ .get_dummy_policy = f2fs_get_dummy_policy,
25272921 .empty_dir = f2fs_empty_dir,
25282922 .max_namelen = F2FS_NAME_LEN,
25292923 .has_stable_inodes = f2fs_has_stable_inodes,
25302924 .get_ino_and_lblk_bits = f2fs_get_ino_and_lblk_bits,
2531
- .inline_crypt_enabled = f2fs_inline_crypt_enabled,
25322925 .get_num_devices = f2fs_get_num_devices,
25332926 .get_devices = f2fs_get_devices,
25342927 };
....@@ -2579,10 +2972,10 @@
25792972 .get_parent = f2fs_get_parent,
25802973 };
25812974
2582
-static loff_t max_file_blocks(void)
2975
+loff_t max_file_blocks(struct inode *inode)
25832976 {
25842977 loff_t result = 0;
2585
- loff_t leaf_count = DEF_ADDRS_PER_BLOCK;
2978
+ loff_t leaf_count;
25862979
25872980 /*
25882981 * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
....@@ -2590,6 +2983,11 @@
25902983 * space in inode.i_addr, it will be more safe to reassign
25912984 * result as zero.
25922985 */
2986
+
2987
+ if (inode && f2fs_compressed_file(inode))
2988
+ leaf_count = ADDRS_PER_BLOCK(inode);
2989
+ else
2990
+ leaf_count = DEF_ADDRS_PER_BLOCK;
25932991
25942992 /* two direct node blocks */
25952993 result += (leaf_count * 2);
....@@ -2681,10 +3079,8 @@
26813079 }
26823080
26833081 if (main_end_blkaddr > seg_end_blkaddr) {
2684
- f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%u) block(%u)",
2685
- main_blkaddr,
2686
- segment0_blkaddr +
2687
- (segment_count << log_blocks_per_seg),
3082
+ f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
3083
+ main_blkaddr, seg_end_blkaddr,
26883084 segment_count_main << log_blocks_per_seg);
26893085 return true;
26903086 } else if (main_end_blkaddr < seg_end_blkaddr) {
....@@ -2702,10 +3098,8 @@
27023098 err = __f2fs_commit_super(bh, NULL);
27033099 res = err ? "failed" : "done";
27043100 }
2705
- f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%u) block(%u)",
2706
- res, main_blkaddr,
2707
- segment0_blkaddr +
2708
- (segment_count << log_blocks_per_seg),
3101
+ f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
3102
+ res, main_blkaddr, seg_end_blkaddr,
27093103 segment_count_main << log_blocks_per_seg);
27103104 if (err)
27113105 return true;
....@@ -2716,11 +3110,10 @@
27163110 static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
27173111 struct buffer_head *bh)
27183112 {
2719
- block_t segment_count, segs_per_sec, secs_per_zone;
3113
+ block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
27203114 block_t total_sections, blocks_per_seg;
27213115 struct f2fs_super_block *raw_super = (struct f2fs_super_block *)
27223116 (bh->b_data + F2FS_SUPER_OFFSET);
2723
- unsigned int blocksize;
27243117 size_t crc_offset = 0;
27253118 __u32 crc = 0;
27263119
....@@ -2746,18 +3139,11 @@
27463139 }
27473140 }
27483141
2749
- /* Currently, support only 4KB page cache size */
2750
- if (F2FS_BLKSIZE != PAGE_SIZE) {
2751
- f2fs_info(sbi, "Invalid page_cache_size (%lu), supports only 4KB",
2752
- PAGE_SIZE);
2753
- return -EFSCORRUPTED;
2754
- }
2755
-
27563142 /* Currently, support only 4KB block size */
2757
- blocksize = 1 << le32_to_cpu(raw_super->log_blocksize);
2758
- if (blocksize != F2FS_BLKSIZE) {
2759
- f2fs_info(sbi, "Invalid blocksize (%u), supports only 4KB",
2760
- blocksize);
3143
+ if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
3144
+ f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
3145
+ le32_to_cpu(raw_super->log_blocksize),
3146
+ F2FS_BLKSIZE_BITS);
27613147 return -EFSCORRUPTED;
27623148 }
27633149
....@@ -2787,6 +3173,7 @@
27873173 }
27883174
27893175 segment_count = le32_to_cpu(raw_super->segment_count);
3176
+ segment_count_main = le32_to_cpu(raw_super->segment_count_main);
27903177 segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
27913178 secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
27923179 total_sections = le32_to_cpu(raw_super->section_count);
....@@ -2800,11 +3187,16 @@
28003187 return -EFSCORRUPTED;
28013188 }
28023189
2803
- if (total_sections > segment_count ||
2804
- total_sections < F2FS_MIN_SEGMENTS ||
3190
+ if (total_sections > segment_count_main || total_sections < 1 ||
28053191 segs_per_sec > segment_count || !segs_per_sec) {
28063192 f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
28073193 segment_count, total_sections, segs_per_sec);
3194
+ return -EFSCORRUPTED;
3195
+ }
3196
+
3197
+ if (segment_count_main != total_sections * segs_per_sec) {
3198
+ f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
3199
+ segment_count_main, total_sections, segs_per_sec);
28083200 return -EFSCORRUPTED;
28093201 }
28103202
....@@ -2833,6 +3225,12 @@
28333225 segment_count, dev_seg_count);
28343226 return -EFSCORRUPTED;
28353227 }
3228
+ } else {
3229
+ if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
3230
+ !bdev_is_zoned(sbi->sb->s_bdev)) {
3231
+ f2fs_info(sbi, "Zoned block device path is missing");
3232
+ return -EFSCORRUPTED;
3233
+ }
28363234 }
28373235
28383236 if (secs_per_zone > total_sections || !secs_per_zone) {
....@@ -2851,11 +3249,13 @@
28513249 return -EFSCORRUPTED;
28523250 }
28533251
2854
- if (le32_to_cpu(raw_super->cp_payload) >
2855
- (blocks_per_seg - F2FS_CP_PACKS)) {
2856
- f2fs_info(sbi, "Insane cp_payload (%u > %u)",
3252
+ if (le32_to_cpu(raw_super->cp_payload) >=
3253
+ (blocks_per_seg - F2FS_CP_PACKS -
3254
+ NR_CURSEG_PERSIST_TYPE)) {
3255
+ f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
28573256 le32_to_cpu(raw_super->cp_payload),
2858
- blocks_per_seg - F2FS_CP_PACKS);
3257
+ blocks_per_seg - F2FS_CP_PACKS -
3258
+ NR_CURSEG_PERSIST_TYPE);
28593259 return -EFSCORRUPTED;
28603260 }
28613261
....@@ -2891,6 +3291,7 @@
28913291 unsigned int cp_pack_start_sum, cp_payload;
28923292 block_t user_block_count, valid_user_blocks;
28933293 block_t avail_node_count, valid_node_count;
3294
+ unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
28943295 int i, j;
28953296
28963297 total = le32_to_cpu(raw_super->segment_count);
....@@ -2908,14 +3309,15 @@
29083309 ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
29093310 reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
29103311
2911
- if (unlikely(fsmeta < F2FS_MIN_SEGMENTS ||
3312
+ if (!f2fs_sb_has_readonly(sbi) &&
3313
+ unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
29123314 ovp_segments == 0 || reserved_segments == 0)) {
29133315 f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
29143316 return 1;
29153317 }
2916
-
29173318 user_block_count = le64_to_cpu(ckpt->user_block_count);
2918
- segment_count_main = le32_to_cpu(raw_super->segment_count_main);
3319
+ segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
3320
+ (f2fs_sb_has_readonly(sbi) ? 1 : 0);
29193321 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
29203322 if (!user_block_count || user_block_count >=
29213323 segment_count_main << log_blocks_per_seg) {
....@@ -2946,6 +3348,10 @@
29463348 if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
29473349 le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
29483350 return 1;
3351
+
3352
+ if (f2fs_sb_has_readonly(sbi))
3353
+ goto check_data;
3354
+
29493355 for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
29503356 if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
29513357 le32_to_cpu(ckpt->cur_node_segno[j])) {
....@@ -2956,10 +3362,15 @@
29563362 }
29573363 }
29583364 }
3365
+check_data:
29593366 for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
29603367 if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
29613368 le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
29623369 return 1;
3370
+
3371
+ if (f2fs_sb_has_readonly(sbi))
3372
+ goto skip_cross;
3373
+
29633374 for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
29643375 if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
29653376 le32_to_cpu(ckpt->cur_data_segno[j])) {
....@@ -2981,7 +3392,7 @@
29813392 }
29823393 }
29833394 }
2984
-
3395
+skip_cross:
29853396 sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
29863397 nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
29873398
....@@ -2996,7 +3407,7 @@
29963407 cp_payload = __cp_payload(sbi);
29973408 if (cp_pack_start_sum < cp_payload + 1 ||
29983409 cp_pack_start_sum > blocks_per_seg - 1 -
2999
- NR_CURSEG_TYPE) {
3410
+ NR_CURSEG_PERSIST_TYPE) {
30003411 f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
30013412 cp_pack_start_sum);
30023413 return 1;
....@@ -3008,6 +3419,17 @@
30083419 "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
30093420 "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
30103421 le32_to_cpu(ckpt->checksum_offset));
3422
+ return 1;
3423
+ }
3424
+
3425
+ nat_blocks = nat_segs << log_blocks_per_seg;
3426
+ nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
3427
+ nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
3428
+ if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
3429
+ (cp_payload + F2FS_CP_PACKS +
3430
+ NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
3431
+ f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
3432
+ cp_payload, nat_bits_blocks);
30113433 return 1;
30123434 }
30133435
....@@ -3035,9 +3457,9 @@
30353457 sbi->total_node_count =
30363458 (le32_to_cpu(raw_super->segment_count_nat) / 2)
30373459 * sbi->blocks_per_seg * NAT_ENTRY_PER_BLOCK;
3038
- sbi->root_ino_num = le32_to_cpu(raw_super->root_ino);
3039
- sbi->node_ino_num = le32_to_cpu(raw_super->node_ino);
3040
- sbi->meta_ino_num = le32_to_cpu(raw_super->meta_ino);
3460
+ F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
3461
+ F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
3462
+ F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
30413463 sbi->cur_victim_sec = NULL_SECNO;
30423464 sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
30433465 sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
....@@ -3062,14 +3484,14 @@
30623484
30633485 INIT_LIST_HEAD(&sbi->s_list);
30643486 mutex_init(&sbi->umount_mutex);
3065
- init_rwsem(&sbi->io_order_lock);
3487
+ init_f2fs_rwsem(&sbi->io_order_lock);
30663488 spin_lock_init(&sbi->cp_lock);
30673489
30683490 sbi->dirty_device = 0;
30693491 spin_lock_init(&sbi->dev_lock);
30703492
3071
- init_rwsem(&sbi->sb_lock);
3072
- init_rwsem(&sbi->pin_sem);
3493
+ init_f2fs_rwsem(&sbi->sb_lock);
3494
+ init_f2fs_rwsem(&sbi->pin_sem);
30733495 }
30743496
30753497 static int init_percpu_info(struct f2fs_sb_info *sbi)
....@@ -3089,15 +3511,35 @@
30893511 }
30903512
30913513 #ifdef CONFIG_BLK_DEV_ZONED
3514
+
3515
+struct f2fs_report_zones_args {
3516
+ struct f2fs_dev_info *dev;
3517
+ bool zone_cap_mismatch;
3518
+};
3519
+
3520
+static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
3521
+ void *data)
3522
+{
3523
+ struct f2fs_report_zones_args *rz_args = data;
3524
+
3525
+ if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
3526
+ return 0;
3527
+
3528
+ set_bit(idx, rz_args->dev->blkz_seq);
3529
+ rz_args->dev->zone_capacity_blocks[idx] = zone->capacity >>
3530
+ F2FS_LOG_SECTORS_PER_BLOCK;
3531
+ if (zone->len != zone->capacity && !rz_args->zone_cap_mismatch)
3532
+ rz_args->zone_cap_mismatch = true;
3533
+
3534
+ return 0;
3535
+}
3536
+
30923537 static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
30933538 {
30943539 struct block_device *bdev = FDEV(devi).bdev;
30953540 sector_t nr_sectors = bdev->bd_part->nr_sects;
3096
- sector_t sector = 0;
3097
- struct blk_zone *zones;
3098
- unsigned int i, nr_zones;
3099
- unsigned int n = 0;
3100
- int err = -EIO;
3541
+ struct f2fs_report_zones_args rep_zone_arg;
3542
+ int ret;
31013543
31023544 if (!f2fs_sb_has_blkzoned(sbi))
31033545 return 0;
....@@ -3122,40 +3564,27 @@
31223564 if (!FDEV(devi).blkz_seq)
31233565 return -ENOMEM;
31243566
3125
-#define F2FS_REPORT_NR_ZONES 4096
3126
-
3127
- zones = f2fs_kzalloc(sbi,
3128
- array_size(F2FS_REPORT_NR_ZONES,
3129
- sizeof(struct blk_zone)),
3130
- GFP_KERNEL);
3131
- if (!zones)
3567
+ /* Get block zones type and zone-capacity */
3568
+ FDEV(devi).zone_capacity_blocks = f2fs_kzalloc(sbi,
3569
+ FDEV(devi).nr_blkz * sizeof(block_t),
3570
+ GFP_KERNEL);
3571
+ if (!FDEV(devi).zone_capacity_blocks)
31323572 return -ENOMEM;
31333573
3134
- /* Get block zones type */
3135
- while (zones && sector < nr_sectors) {
3574
+ rep_zone_arg.dev = &FDEV(devi);
3575
+ rep_zone_arg.zone_cap_mismatch = false;
31363576
3137
- nr_zones = F2FS_REPORT_NR_ZONES;
3138
- err = blkdev_report_zones(bdev, sector,
3139
- zones, &nr_zones,
3140
- GFP_KERNEL);
3141
- if (err)
3142
- break;
3143
- if (!nr_zones) {
3144
- err = -EIO;
3145
- break;
3146
- }
3577
+ ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
3578
+ &rep_zone_arg);
3579
+ if (ret < 0)
3580
+ return ret;
31473581
3148
- for (i = 0; i < nr_zones; i++) {
3149
- if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL)
3150
- set_bit(n, FDEV(devi).blkz_seq);
3151
- sector += zones[i].len;
3152
- n++;
3153
- }
3582
+ if (!rep_zone_arg.zone_cap_mismatch) {
3583
+ kfree(FDEV(devi).zone_capacity_blocks);
3584
+ FDEV(devi).zone_capacity_blocks = NULL;
31543585 }
31553586
3156
- kvfree(zones);
3157
-
3158
- return err;
3587
+ return 0;
31593588 }
31603589 #endif
31613590
....@@ -3210,7 +3639,7 @@
32103639
32113640 /* No valid superblock */
32123641 if (!*raw_super)
3213
- kvfree(super);
3642
+ kfree(super);
32143643 else
32153644 err = 0;
32163645
....@@ -3254,6 +3683,26 @@
32543683 err = __f2fs_commit_super(bh, F2FS_RAW_SUPER(sbi));
32553684 brelse(bh);
32563685 return err;
3686
+}
3687
+
3688
+void f2fs_handle_stop(struct f2fs_sb_info *sbi, unsigned char reason)
3689
+{
3690
+ struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3691
+ int err;
3692
+
3693
+ f2fs_bug_on(sbi, reason >= MAX_STOP_REASON);
3694
+
3695
+ f2fs_down_write(&sbi->sb_lock);
3696
+
3697
+ if (raw_super->s_stop_reason[reason] < ((1 << BITS_PER_BYTE) - 1))
3698
+ raw_super->s_stop_reason[reason]++;
3699
+
3700
+ err = f2fs_commit_super(sbi, false);
3701
+ if (err)
3702
+ f2fs_err(sbi, "f2fs_commit_super fails to record reason:%u err:%d",
3703
+ reason, err);
3704
+
3705
+ f2fs_up_write(&sbi->sb_lock);
32573706 }
32583707
32593708 static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
....@@ -3319,7 +3768,7 @@
33193768 #ifdef CONFIG_BLK_DEV_ZONED
33203769 if (bdev_zoned_model(FDEV(i).bdev) == BLK_ZONED_HM &&
33213770 !f2fs_sb_has_blkzoned(sbi)) {
3322
- f2fs_err(sbi, "Zoned block device feature not enabled\n");
3771
+ f2fs_err(sbi, "Zoned block device feature not enabled");
33233772 return -EINVAL;
33243773 }
33253774 if (bdev_zoned_model(FDEV(i).bdev) != BLK_ZONED_NONE) {
....@@ -3396,7 +3845,8 @@
33963845 if (sm_i->main_segments <= SMALL_VOLUME_SEGMENTS) {
33973846 F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
33983847 sm_i->dcc_info->discard_granularity = 1;
3399
- sm_i->ipu_policy = 1 << F2FS_IPU_FORCE;
3848
+ sm_i->ipu_policy = 1 << F2FS_IPU_FORCE |
3849
+ 1 << F2FS_IPU_HONOR_OPU_WRITE;
34003850 }
34013851
34023852 sbi->readdir_ra = 1;
....@@ -3455,18 +3905,6 @@
34553905 sbi->s_chksum_seed = f2fs_chksum(sbi, ~0, raw_super->uuid,
34563906 sizeof(raw_super->uuid));
34573907
3458
- /*
3459
- * The BLKZONED feature indicates that the drive was formatted with
3460
- * zone alignment optimization. This is optional for host-aware
3461
- * devices, but mandatory for host-managed zoned block devices.
3462
- */
3463
-#ifndef CONFIG_BLK_DEV_ZONED
3464
- if (f2fs_sb_has_blkzoned(sbi)) {
3465
- f2fs_err(sbi, "Zoned block device support is not enabled");
3466
- err = -EOPNOTSUPP;
3467
- goto free_sb_buf;
3468
- }
3469
-#endif
34703908 default_options(sbi);
34713909 /* parse mount options */
34723910 options = kstrdup((const char *)data, GFP_KERNEL);
....@@ -3479,8 +3917,7 @@
34793917 if (err)
34803918 goto free_options;
34813919
3482
- sbi->max_file_blocks = max_file_blocks();
3483
- sb->s_maxbytes = sbi->max_file_blocks <<
3920
+ sb->s_maxbytes = max_file_blocks(NULL) <<
34843921 le32_to_cpu(raw_super->log_blocksize);
34853922 sb->s_max_links = F2FS_LINK_MAX;
34863923
....@@ -3519,11 +3956,11 @@
35193956
35203957 /* init f2fs-specific super block info */
35213958 sbi->valid_super_block = valid_super_block;
3522
- init_rwsem(&sbi->gc_lock);
3959
+ init_f2fs_rwsem(&sbi->gc_lock);
35233960 mutex_init(&sbi->writepages);
3524
- mutex_init(&sbi->cp_mutex);
3525
- init_rwsem(&sbi->node_write);
3526
- init_rwsem(&sbi->node_change);
3961
+ init_f2fs_rwsem(&sbi->cp_global_sem);
3962
+ init_f2fs_rwsem(&sbi->node_write);
3963
+ init_f2fs_rwsem(&sbi->node_change);
35273964
35283965 /* disallow all the data/node/meta page writes */
35293966 set_sbi_flag(sbi, SBI_POR_DOING);
....@@ -3535,7 +3972,7 @@
35353972 sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
35363973
35373974 for (i = 0; i < NR_PAGE_TYPE; i++) {
3538
- int n = (i == META) ? 1: NR_TEMP_TYPE;
3975
+ int n = (i == META) ? 1 : NR_TEMP_TYPE;
35393976 int j;
35403977
35413978 sbi->write_io[i] =
....@@ -3549,18 +3986,18 @@
35493986 }
35503987
35513988 for (j = HOT; j < n; j++) {
3552
- init_rwsem(&sbi->write_io[i][j].io_rwsem);
3989
+ init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem);
35533990 sbi->write_io[i][j].sbi = sbi;
35543991 sbi->write_io[i][j].bio = NULL;
35553992 spin_lock_init(&sbi->write_io[i][j].io_lock);
35563993 INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
35573994 INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
3558
- init_rwsem(&sbi->write_io[i][j].bio_list_lock);
3995
+ init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock);
35593996 }
35603997 }
35613998
3562
- init_rwsem(&sbi->cp_rwsem);
3563
- init_rwsem(&sbi->quota_sem);
3999
+ init_f2fs_rwsem(&sbi->cp_rwsem);
4000
+ init_f2fs_rwsem(&sbi->quota_sem);
35644001 init_waitqueue_head(&sbi->cp_wait);
35654002 init_sb_info(sbi);
35664003
....@@ -3581,13 +4018,16 @@
35814018 err = f2fs_init_xattr_caches(sbi);
35824019 if (err)
35834020 goto free_io_dummy;
4021
+ err = f2fs_init_page_array_cache(sbi);
4022
+ if (err)
4023
+ goto free_xattr_cache;
35844024
35854025 /* get an inode for meta space */
35864026 sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
35874027 if (IS_ERR(sbi->meta_inode)) {
35884028 f2fs_err(sbi, "Failed to read F2FS meta data inode");
35894029 err = PTR_ERR(sbi->meta_inode);
3590
- goto free_xattr_cache;
4030
+ goto free_page_array_cache;
35914031 }
35924032
35934033 err = f2fs_get_valid_checkpoint(sbi);
....@@ -3644,6 +4084,19 @@
36444084
36454085 f2fs_init_fsync_node_info(sbi);
36464086
4087
+ /* setup checkpoint request control and start checkpoint issue thread */
4088
+ f2fs_init_ckpt_req_control(sbi);
4089
+ if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
4090
+ test_opt(sbi, MERGE_CHECKPOINT)) {
4091
+ err = f2fs_start_ckpt_thread(sbi);
4092
+ if (err) {
4093
+ f2fs_err(sbi,
4094
+ "Failed to start F2FS issue_checkpoint_thread (%d)",
4095
+ err);
4096
+ goto stop_ckpt_thread;
4097
+ }
4098
+ }
4099
+
36474100 /* setup f2fs internal modules */
36484101 err = f2fs_build_segment_manager(sbi);
36494102 if (err) {
....@@ -3658,11 +4111,12 @@
36584111 goto free_nm;
36594112 }
36604113
4114
+ err = adjust_reserved_segment(sbi);
4115
+ if (err)
4116
+ goto free_nm;
4117
+
36614118 /* For write statistics */
3662
- if (sb->s_bdev->bd_part)
3663
- sbi->sectors_written_start =
3664
- (u64)part_stat_read(sb->s_bdev->bd_part,
3665
- sectors[STAT_WRITE]);
4119
+ sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
36664120
36674121 /* Read accumulated write IO statistics if exists */
36684122 seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
....@@ -3706,9 +4160,13 @@
37064160 goto free_node_inode;
37074161 }
37084162
3709
- err = f2fs_register_sysfs(sbi);
4163
+ err = f2fs_init_compress_inode(sbi);
37104164 if (err)
37114165 goto free_root_inode;
4166
+
4167
+ err = f2fs_register_sysfs(sbi);
4168
+ if (err)
4169
+ goto free_compress_inode;
37124170
37134171 #ifdef CONFIG_QUOTA
37144172 /* Enable quota usage during mount */
....@@ -3735,9 +4193,15 @@
37354193 */
37364194 if (f2fs_hw_is_readonly(sbi)) {
37374195 if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
3738
- err = -EROFS;
3739
- f2fs_err(sbi, "Need to recover fsync data, but write access unavailable");
3740
- goto free_meta;
4196
+ err = f2fs_recover_fsync_data(sbi, true);
4197
+ if (err > 0) {
4198
+ err = -EROFS;
4199
+ f2fs_err(sbi, "Need to recover fsync data, but "
4200
+ "write access unavailable, please try "
4201
+ "mount w/ disable_roll_forward or norecovery");
4202
+ }
4203
+ if (err < 0)
4204
+ goto free_meta;
37414205 }
37424206 f2fs_info(sbi, "write access unavailable, skipping recovery");
37434207 goto reset_checkpoint;
....@@ -3767,7 +4231,20 @@
37674231 goto free_meta;
37684232 }
37694233 }
4234
+
4235
+ /*
4236
+ * If the f2fs is not readonly and fsync data recovery succeeds,
4237
+ * check zoned block devices' write pointer consistency.
4238
+ */
4239
+ if (!err && !f2fs_readonly(sb) && f2fs_sb_has_blkzoned(sbi)) {
4240
+ err = f2fs_check_write_pointer(sbi);
4241
+ if (err)
4242
+ goto free_meta;
4243
+ }
4244
+
37704245 reset_checkpoint:
4246
+ f2fs_init_inmem_curseg(sbi);
4247
+
37714248 /* f2fs_recover_fsync_data() cleared this already */
37724249 clear_sbi_flag(sbi, SBI_POR_DOING);
37734250
....@@ -3783,7 +4260,8 @@
37834260 * If filesystem is not mounted as read-only then
37844261 * do start the gc_thread.
37854262 */
3786
- if (F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF && !f2fs_readonly(sb)) {
4263
+ if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
4264
+ test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
37874265 /* After POR, we can run background GC thread.*/
37884266 err = f2fs_start_gc_thread(sbi);
37894267 if (err)
....@@ -3830,6 +4308,8 @@
38304308 /* evict some inodes being cached by GC */
38314309 evict_inodes(sb);
38324310 f2fs_unregister_sysfs(sbi);
4311
+free_compress_inode:
4312
+ f2fs_destroy_compress_inode(sbi);
38334313 free_root_inode:
38344314 dput(sb->s_root);
38354315 sb->s_root = NULL;
....@@ -3841,10 +4321,14 @@
38414321 free_stats:
38424322 f2fs_destroy_stats(sbi);
38434323 free_nm:
4324
+ /* stop discard thread before destroying node manager */
4325
+ f2fs_stop_discard_thread(sbi);
38444326 f2fs_destroy_node_manager(sbi);
38454327 free_sm:
38464328 f2fs_destroy_segment_manager(sbi);
38474329 f2fs_destroy_post_read_wq(sbi);
4330
+stop_ckpt_thread:
4331
+ f2fs_stop_ckpt_thread(sbi);
38484332 free_devices:
38494333 destroy_device_list(sbi);
38504334 kvfree(sbi->ckpt);
....@@ -3852,6 +4336,8 @@
38524336 make_bad_inode(sbi->meta_inode);
38534337 iput(sbi->meta_inode);
38544338 sbi->meta_inode = NULL;
4339
+free_page_array_cache:
4340
+ f2fs_destroy_page_array_cache(sbi);
38554341 free_xattr_cache:
38564342 f2fs_destroy_xattr_caches(sbi);
38574343 free_io_dummy:
....@@ -3864,20 +4350,21 @@
38644350
38654351 #ifdef CONFIG_UNICODE
38664352 utf8_unload(sb->s_encoding);
4353
+ sb->s_encoding = NULL;
38674354 #endif
38684355 free_options:
38694356 #ifdef CONFIG_QUOTA
38704357 for (i = 0; i < MAXQUOTAS; i++)
3871
- kvfree(F2FS_OPTION(sbi).s_qf_names[i]);
4358
+ kfree(F2FS_OPTION(sbi).s_qf_names[i]);
38724359 #endif
3873
- fscrypt_free_dummy_context(&F2FS_OPTION(sbi).dummy_enc_ctx);
4360
+ fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
38744361 kvfree(options);
38754362 free_sb_buf:
3876
- kvfree(raw_super);
4363
+ kfree(raw_super);
38774364 free_sbi:
38784365 if (sbi->s_chksum_driver)
38794366 crypto_free_shash(sbi->s_chksum_driver);
3880
- kvfree(sbi);
4367
+ kfree(sbi);
38814368
38824369 /* give only one another chance */
38834370 if (retry_cnt > 0 && skip_recovery) {
....@@ -3902,6 +4389,15 @@
39024389 set_sbi_flag(sbi, SBI_IS_CLOSE);
39034390 f2fs_stop_gc_thread(sbi);
39044391 f2fs_stop_discard_thread(sbi);
4392
+
4393
+#ifdef CONFIG_F2FS_FS_COMPRESSION
4394
+ /*
4395
+ * latter evict_inode() can bypass checking and invalidating
4396
+ * compress inode cache.
4397
+ */
4398
+ if (test_opt(sbi, COMPRESS_CACHE))
4399
+ truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
4400
+#endif
39054401
39064402 if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
39074403 !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
....@@ -3956,8 +4452,6 @@
39564452 return -EINVAL;
39574453 }
39584454
3959
- f2fs_build_trace_ios();
3960
-
39614455 err = init_inodecache();
39624456 if (err)
39634457 goto fail;
....@@ -3970,12 +4464,18 @@
39704464 err = f2fs_create_checkpoint_caches();
39714465 if (err)
39724466 goto free_segment_manager_caches;
3973
- err = f2fs_create_extent_cache();
4467
+ err = f2fs_create_recovery_cache();
39744468 if (err)
39754469 goto free_checkpoint_caches;
3976
- err = f2fs_init_sysfs();
4470
+ err = f2fs_create_extent_cache();
4471
+ if (err)
4472
+ goto free_recovery_cache;
4473
+ err = f2fs_create_garbage_collection_cache();
39774474 if (err)
39784475 goto free_extent_cache;
4476
+ err = f2fs_init_sysfs();
4477
+ if (err)
4478
+ goto free_garbage_collection_cache;
39794479 err = register_shrinker(&f2fs_shrinker_info);
39804480 if (err)
39814481 goto free_sysfs;
....@@ -3995,7 +4495,17 @@
39954495 err = f2fs_init_compress_mempool();
39964496 if (err)
39974497 goto free_bioset;
4498
+ err = f2fs_init_compress_cache();
4499
+ if (err)
4500
+ goto free_compress_mempool;
4501
+ err = f2fs_create_casefold_cache();
4502
+ if (err)
4503
+ goto free_compress_cache;
39984504 return 0;
4505
+free_compress_cache:
4506
+ f2fs_destroy_compress_cache();
4507
+free_compress_mempool:
4508
+ f2fs_destroy_compress_mempool();
39994509 free_bioset:
40004510 f2fs_destroy_bioset();
40014511 free_bio_enrty_cache:
....@@ -4009,8 +4519,12 @@
40094519 unregister_shrinker(&f2fs_shrinker_info);
40104520 free_sysfs:
40114521 f2fs_exit_sysfs();
4522
+free_garbage_collection_cache:
4523
+ f2fs_destroy_garbage_collection_cache();
40124524 free_extent_cache:
40134525 f2fs_destroy_extent_cache();
4526
+free_recovery_cache:
4527
+ f2fs_destroy_recovery_cache();
40144528 free_checkpoint_caches:
40154529 f2fs_destroy_checkpoint_caches();
40164530 free_segment_manager_caches:
....@@ -4025,6 +4539,8 @@
40254539
40264540 static void __exit exit_f2fs_fs(void)
40274541 {
4542
+ f2fs_destroy_casefold_cache();
4543
+ f2fs_destroy_compress_cache();
40284544 f2fs_destroy_compress_mempool();
40294545 f2fs_destroy_bioset();
40304546 f2fs_destroy_bio_entry_cache();
....@@ -4033,12 +4549,13 @@
40334549 unregister_filesystem(&f2fs_fs_type);
40344550 unregister_shrinker(&f2fs_shrinker_info);
40354551 f2fs_exit_sysfs();
4552
+ f2fs_destroy_garbage_collection_cache();
40364553 f2fs_destroy_extent_cache();
4554
+ f2fs_destroy_recovery_cache();
40374555 f2fs_destroy_checkpoint_caches();
40384556 f2fs_destroy_segment_manager_caches();
40394557 f2fs_destroy_node_manager_caches();
40404558 destroy_inodecache();
4041
- f2fs_destroy_trace_ios();
40424559 }
40434560
40444561 module_init(init_f2fs_fs)
....@@ -4047,5 +4564,6 @@
40474564 MODULE_AUTHOR("Samsung Electronics's Praesto Team");
40484565 MODULE_DESCRIPTION("Flash Friendly File System");
40494566 MODULE_LICENSE("GPL");
4567
+MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
40504568 MODULE_SOFTDEP("pre: crc32");
40514569