Skip to content

Commit 6691d94

Browse files
Daeho JeongJaegeuk Kim
authored andcommitted
f2fs: introduce fragment allocation mode mount option
Added two options into "mode=" mount option to make it possible for developers to simulate filesystem fragmentation/after-GC situation itself. The developers use these modes to understand filesystem fragmentation/after-GC condition well, and eventually get some insights to handle them better. "fragment:segment": f2fs allocates a new segment in ramdom position. With this, we can simulate the after-GC condition. "fragment:block" : We can scatter block allocation with "max_fragment_chunk" and "max_fragment_hole" sysfs nodes. f2fs will allocate 1..<max_fragment_chunk> blocks in a chunk and make a hole in the length of 1..<max_fragment_hole> by turns in a newly allocated free segment. Plus, this mode implicitly enables "fragment:segment" option for more randomness. Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Daeho Jeong <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 84eab2a commit 6691d94

File tree

8 files changed

+104
-5
lines changed

8 files changed

+104
-5
lines changed

Documentation/ABI/testing/sysfs-fs-f2fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,19 @@ Date: July 2021
512512
Contact: "Daeho Jeong" <[email protected]>
513513
Description: You can control the multiplier value of bdi device readahead window size
514514
between 2 (default) and 256 for POSIX_FADV_SEQUENTIAL advise option.
515+
516+
What: /sys/fs/f2fs/<disk>/max_fragment_chunk
517+
Date: August 2021
518+
Contact: "Daeho Jeong" <[email protected]>
519+
Description: With "mode=fragment:block" mount options, we can scatter block allocation.
520+
f2fs will allocate 1..<max_fragment_chunk> blocks in a chunk and make a hole
521+
in the length of 1..<max_fragment_hole> by turns. This value can be set
522+
between 1..512 and the default value is 4.
523+
524+
What: /sys/fs/f2fs/<disk>/max_fragment_hole
525+
Date: August 2021
526+
Contact: "Daeho Jeong" <[email protected]>
527+
Description: With "mode=fragment:block" mount options, we can scatter block allocation.
528+
f2fs will allocate 1..<max_fragment_chunk> blocks in a chunk and make a hole
529+
in the length of 1..<max_fragment_hole> by turns. This value can be set
530+
between 1..512 and the default value is 4.

Documentation/filesystems/f2fs.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ fault_type=%d Support configuring fault injection type, should be
201201
mode=%s Control block allocation mode which supports "adaptive"
202202
and "lfs". In "lfs" mode, there should be no random
203203
writes towards main area.
204+
"fragment:segment" and "fragment:block" are newly added here.
205+
These are developer options for experiments to simulate filesystem
206+
fragmentation/after-GC situation itself. The developers use these
207+
modes to understand filesystem fragmentation/after-GC condition well,
208+
and eventually get some insights to handle them better.
209+
In "fragment:segment", f2fs allocates a new segment in ramdom
210+
position. With this, we can simulate the after-GC condition.
211+
In "fragment:block", we can scatter block allocation with
212+
"max_fragment_chunk" and "max_fragment_hole" sysfs nodes.
213+
We added some randomness to both chunk and hole size to make
214+
it close to realistic IO pattern. So, in this mode, f2fs will allocate
215+
1..<max_fragment_chunk> blocks in a chunk and make a hole in the
216+
length of 1..<max_fragment_hole> by turns. With this, the newly
217+
allocated blocks will be scattered throughout the whole partition.
218+
Note that "fragment:block" implicitly enables "fragment:segment"
219+
option for more randomness.
220+
Please, use these options for your experiments and we strongly
221+
recommend to re-format the filesystem after using these options.
204222
io_bits=%u Set the bit size of write IO requests. It should be set
205223
with "mode=lfs".
206224
usrquota Enable plain user disk quota accounting.

fs/f2fs/f2fs.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,10 @@ enum {
12871287
};
12881288

12891289
enum {
1290-
FS_MODE_ADAPTIVE, /* use both lfs/ssr allocation */
1291-
FS_MODE_LFS, /* use lfs allocation only */
1290+
FS_MODE_ADAPTIVE, /* use both lfs/ssr allocation */
1291+
FS_MODE_LFS, /* use lfs allocation only */
1292+
FS_MODE_FRAGMENT_SEG, /* segment fragmentation mode */
1293+
FS_MODE_FRAGMENT_BLK, /* block fragmentation mode */
12921294
};
12931295

12941296
enum {
@@ -1759,6 +1761,9 @@ struct f2fs_sb_info {
17591761

17601762
unsigned long seq_file_ra_mul; /* multiplier for ra_pages of seq. files in fadvise */
17611763

1764+
int max_fragment_chunk; /* max chunk size for block fragmentation mode */
1765+
int max_fragment_hole; /* max hole size for block fragmentation mode */
1766+
17621767
#ifdef CONFIG_F2FS_FS_COMPRESSION
17631768
struct kmem_cache *page_array_slab; /* page array entry */
17641769
unsigned int page_array_slab_size; /* default page array slab size */
@@ -3519,6 +3524,16 @@ unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
35193524
unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
35203525
unsigned int segno);
35213526

3527+
#define DEF_FRAGMENT_SIZE 4
3528+
#define MIN_FRAGMENT_SIZE 1
3529+
#define MAX_FRAGMENT_SIZE 512
3530+
3531+
static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
3532+
{
3533+
return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
3534+
F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
3535+
}
3536+
35223537
/*
35233538
* checkpoint.c
35243539
*/

fs/f2fs/gc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/delay.h>
1515
#include <linux/freezer.h>
1616
#include <linux/sched/signal.h>
17+
#include <linux/random.h>
1718

1819
#include "f2fs.h"
1920
#include "node.h"
@@ -257,7 +258,9 @@ static void select_policy(struct f2fs_sb_info *sbi, int gc_type,
257258
p->max_search = sbi->max_victim_search;
258259

259260
/* let's select beginning hot/small space first in no_heap mode*/
260-
if (test_opt(sbi, NOHEAP) &&
261+
if (f2fs_need_rand_seg(sbi))
262+
p->offset = prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
263+
else if (test_opt(sbi, NOHEAP) &&
261264
(type == CURSEG_HOT_DATA || IS_NODESEG(type)))
262265
p->offset = 0;
263266
else

fs/f2fs/segment.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/timer.h>
1616
#include <linux/freezer.h>
1717
#include <linux/sched/signal.h>
18+
#include <linux/random.h>
1819

1920
#include "f2fs.h"
2021
#include "segment.h"
@@ -2649,6 +2650,8 @@ static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
26492650
unsigned short seg_type = curseg->seg_type;
26502651

26512652
sanity_check_seg_type(sbi, seg_type);
2653+
if (f2fs_need_rand_seg(sbi))
2654+
return prandom_u32() % (MAIN_SECS(sbi) * sbi->segs_per_sec);
26522655

26532656
/* if segs_per_sec is large than 1, we need to keep original policy. */
26542657
if (__is_large_section(sbi))
@@ -2700,6 +2703,9 @@ static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
27002703
curseg->next_segno = segno;
27012704
reset_curseg(sbi, type, 1);
27022705
curseg->alloc_type = LFS;
2706+
if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2707+
curseg->fragment_remained_chunk =
2708+
prandom_u32() % sbi->max_fragment_chunk + 1;
27032709
}
27042710

27052711
static int __next_free_blkoff(struct f2fs_sb_info *sbi,
@@ -2726,12 +2732,22 @@ static int __next_free_blkoff(struct f2fs_sb_info *sbi,
27262732
static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
27272733
struct curseg_info *seg)
27282734
{
2729-
if (seg->alloc_type == SSR)
2735+
if (seg->alloc_type == SSR) {
27302736
seg->next_blkoff =
27312737
__next_free_blkoff(sbi, seg->segno,
27322738
seg->next_blkoff + 1);
2733-
else
2739+
} else {
27342740
seg->next_blkoff++;
2741+
if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) {
2742+
/* To allocate block chunks in different sizes, use random number */
2743+
if (--seg->fragment_remained_chunk <= 0) {
2744+
seg->fragment_remained_chunk =
2745+
prandom_u32() % sbi->max_fragment_chunk + 1;
2746+
seg->next_blkoff +=
2747+
prandom_u32() % sbi->max_fragment_hole + 1;
2748+
}
2749+
}
2750+
}
27352751
}
27362752

27372753
bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)

fs/f2fs/segment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ struct curseg_info {
314314
unsigned short next_blkoff; /* next block offset to write */
315315
unsigned int zone; /* current zone number */
316316
unsigned int next_segno; /* preallocated segment */
317+
int fragment_remained_chunk; /* remained block size in a chunk for block fragmentation mode */
317318
bool inited; /* indicate inmem log is inited */
318319
};
319320

fs/f2fs/super.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,10 @@ static int parse_options(struct super_block *sb, char *options, bool is_remount)
817817
F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
818818
} else if (!strcmp(name, "lfs")) {
819819
F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
820+
} else if (!strcmp(name, "fragment:segment")) {
821+
F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_SEG;
822+
} else if (!strcmp(name, "fragment:block")) {
823+
F2FS_OPTION(sbi).fs_mode = FS_MODE_FRAGMENT_BLK;
820824
} else {
821825
kfree(name);
822826
return -EINVAL;
@@ -1896,6 +1900,10 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
18961900
seq_puts(seq, "adaptive");
18971901
else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
18981902
seq_puts(seq, "lfs");
1903+
else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
1904+
seq_puts(seq, "fragment:segment");
1905+
else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
1906+
seq_puts(seq, "fragment:block");
18991907
seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
19001908
if (test_opt(sbi, RESERVE_ROOT))
19011909
seq_printf(seq, ",reserve_root=%u,resuid=%u,resgid=%u",
@@ -3523,6 +3531,8 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
35233531
sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
35243532
sbi->migration_granularity = sbi->segs_per_sec;
35253533
sbi->seq_file_ra_mul = MIN_RA_MUL;
3534+
sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
3535+
sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
35263536

35273537
sbi->dir_level = DEF_DIR_LEVEL;
35283538
sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;

fs/f2fs/sysfs.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,22 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
551551
return count;
552552
}
553553

554+
if (!strcmp(a->attr.name, "max_fragment_chunk")) {
555+
if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
556+
sbi->max_fragment_chunk = t;
557+
else
558+
return -EINVAL;
559+
return count;
560+
}
561+
562+
if (!strcmp(a->attr.name, "max_fragment_hole")) {
563+
if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
564+
sbi->max_fragment_hole = t;
565+
else
566+
return -EINVAL;
567+
return count;
568+
}
569+
554570
*ui = (unsigned int)t;
555571

556572
return count;
@@ -781,6 +797,8 @@ F2FS_RW_ATTR(ATGC_INFO, atgc_management, atgc_age_threshold, age_threshold);
781797
F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, seq_file_ra_mul, seq_file_ra_mul);
782798
F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_segment_mode, gc_segment_mode);
783799
F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_reclaimed_segments, gc_reclaimed_segs);
800+
F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_chunk, max_fragment_chunk);
801+
F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_fragment_hole, max_fragment_hole);
784802

785803
#define ATTR_LIST(name) (&f2fs_attr_##name.attr)
786804
static struct attribute *f2fs_attrs[] = {
@@ -859,6 +877,8 @@ static struct attribute *f2fs_attrs[] = {
859877
ATTR_LIST(seq_file_ra_mul),
860878
ATTR_LIST(gc_segment_mode),
861879
ATTR_LIST(gc_reclaimed_segments),
880+
ATTR_LIST(max_fragment_chunk),
881+
ATTR_LIST(max_fragment_hole),
862882
NULL,
863883
};
864884
ATTRIBUTE_GROUPS(f2fs);

0 commit comments

Comments
 (0)