f2fs: ioctl for removing a range from F2FS
Subsystems (2)
PATCH v8 1/1 f2fs: ioctl for removing a range from F2FS
From: Qiuyang Sun <[email protected]>
This ioctl shrinks a given length (aligned to sections) from end of the main area. Any cursegs and valid blocks will be moved out before invalidating the range.
This feature can be used for adjusting partition sizes online. --
Changlog v1 ==> v2:
Sahitya Tummala:
- Add this ioctl for f2fs_compat_ioctl() as well.
- Fix debugfs status to reflect the online resize changes.
- Fix potential race between online resize path and allocate new data block path or gc path.
Others:
- Rename some identifiers.
- Add some error handling branches.
- Clear sbi->next_victim_seg[BG_GC/FG_GC] in shrinking range.
--
Changelog v2 ==> v3:
Implement this interface as ext4's, and change the parameter from shrunk bytes to new block count of F2FS.
--
Changelog v3 ==> v4:
- During resizing, force to empty sit_journal and forbid adding new entries to it, in order to avoid invalid segno in journal after resize.
- Reduce sbi->user_block_count before resize starts.
- Commit the updated superblock first, and then update in-memory metadata only when the former succeeds.
- Target block count must align to sections.
--
Changelog v4 ==> v5:
Write checkpoint before and after committing the new superblock, w/o CP_FSCK_FLAG respectively, so that the FS can be fixed by fsck even if resize fails after the new superblock is committed.
--
Changelog v5 ==> v6:
- In free_segment_range(), reduce granularity of gc_mutex.
- Add protection on curseg migration.
--
Changelog v6 ==> v7:
- Add freeze_bdev() and thaw_bdev() for resize fs.
- Remove CUR_MAIN_SECS and use MAIN_SECS directly for allocation.
- Recover super_block and FS metadata when resize fails.
--
Changelog v7 ==> v8:
- No need to clear CP_FSCK_FLAG in update_ckpt_flags().
- Clean up the sb and fs metadata update functions for resize_fs.
--- fs/f2fs/checkpoint.c | 3 +- fs/f2fs/debug.c | 7 ++ fs/f2fs/f2fs.h | 6 ++ fs/f2fs/file.c | 23 +++++++ fs/f2fs/gc.c | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++- fs/f2fs/segment.c | 39 ++++++++++- fs/f2fs/super.c | 4 ++ 7 files changed, 257 insertions(+), 5 deletions(-)
fs/f2fs/checkpoint.c +2 -1
@@ -1313,7 +1313,8 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)else __clear_ckpt_flags (ckpt,CP_ORPHAN_PRESENT_FLAG ); -if (is_sbi_flag_set (sbi,SBI_NEED_FSCK )) +if (is_sbi_flag_set (sbi,SBI_NEED_FSCK ) || +is_sbi_flag_set (sbi,SBI_IS_RESIZEFS ))__set_ckpt_flags (ckpt,CP_FSCK_FLAG );if (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))
fs/f2fs/debug.c +7 -0
@@ -27,8 +27,15 @@static void update_general_status (struct f2fs_sb_info *sbi) {struct f2fs_stat_info *si =F2FS_STAT (sbi); +struct f2fs_super_block *raw_super =F2FS_RAW_SUPER (sbi);int i; +/* these will be changed if online resize is done */ + si->main_area_segs =le32_to_cpu (raw_super->segment_count_main); + si->main_area_sections =le32_to_cpu (raw_super->section_count); + si->main_area_zones = si->main_area_sections / +le32_to_cpu (raw_super->secs_per_zone); +/* validation check of the segment numbers */ si->hit_largest =atomic64_read (&sbi->read_hit_largest); si->hit_cached =atomic64_read (&sbi->read_hit_cached);
fs/f2fs/f2fs.h +6 -0
@@ -423,6 +423,7 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15) +#define F2FS_IOC_RESIZE_FS _IOW(F2FS_IOCTL_MAGIC, 16, __u64)#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
@@ -1130,6 +1131,7 @@ enum {SBI_QUOTA_NEED_FLUSH ,/* need to flush quota info in CP */ SBI_QUOTA_SKIP_FLUSH ,/* skip flushing quota in current CP */ SBI_QUOTA_NEED_REPAIR ,/* quota file may be corrupted */ + SBI_IS_RESIZEFS, /* resizefs is in process */ }; enum {
@@ -1309,6 +1311,7 @@ struct f2fs_sb_info {unsigned int segs_per_sec;/* segments per section */ unsigned int secs_per_zone;/* sections per zone */ unsigned int total_sections;/* total section count */ +struct mutex resize_mutex;/* for resize exclusion */ unsigned int total_node_count;/* total node block count */ unsigned int total_valid_node_count;/* valid node block count */ loff_t max_file_blocks;/* max block index of file */
@@ -3175,6 +3178,8 @@ void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,int f2fs_disable_cp_again (struct f2fs_sb_info *sbi,block_t unusable);void f2fs_release_discard_addrs (struct f2fs_sb_info *sbi);int f2fs_npages_for_summary_flush (struct f2fs_sb_info *sbi,bool for_ra); +void allocate_segment_for_resize (struct f2fs_sb_info *sbi,int type, +unsigned int start,unsigned int end);void f2fs_allocate_new_segments (struct f2fs_sb_info *sbi);int f2fs_trim_fs (struct f2fs_sb_info *sbi,struct fstrim_range *range); bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
@@ -3318,6 +3323,7 @@ int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,int f2fs_gc (struct f2fs_sb_info *sbi,bool sync,bool background,unsigned int segno);void f2fs_build_gc_manager (struct f2fs_sb_info *sbi); +int f2fs_resize_fs (struct f2fs_sb_info *sbi,__u64 block_count);/* * recovery.c
fs/f2fs/file.c +23 -0
@@ -3013,6 +3013,26 @@ static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)return f2fs_precache_extents (file_inode (filp)); } +static int f2fs_ioc_resize_fs (struct file *filp,unsigned long arg) +{ +struct f2fs_sb_info *sbi =F2FS_I_SB (file_inode (filp)); +__u64 block_count; +int ret; + +if (!capable (CAP_SYS_ADMIN )) +return -EPERM ; + +if (f2fs_readonly (sbi->sb)) +return -EROFS ; + +if (get_user (block_count, (__u64 __user *)arg)) +return -EFAULT ; + + ret =f2fs_resize_fs (sbi, block_count); + +return ret; +} +long f2fs_ioctl (struct file *filp,unsigned int cmd,unsigned long arg) {if (unlikely (f2fs_cp_error (F2FS_I_SB (file_inode (filp)))))
@@ -3069,6 +3089,8 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)return f2fs_ioc_set_pin_file (filp, arg);case F2FS_IOC_PRECACHE_EXTENTS :return f2fs_ioc_precache_extents (filp, arg); +case F2FS_IOC_RESIZE_FS : +return f2fs_ioc_resize_fs (filp, arg);default :return -ENOTTY ; }
@@ -3182,6 +3204,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case F2FS_IOC_GET_PIN_FILE :case F2FS_IOC_SET_PIN_FILE :case F2FS_IOC_PRECACHE_EXTENTS : +case F2FS_IOC_RESIZE_FS :break ;default :return -ENOIOCTLCMD ;
fs/f2fs/gc.c +178 -2
198 lines not on this page · raw message
fs/f2fs/segment.c +37 -2
@@ -2651,6 +2651,40 @@ static void allocate_segment_by_default(struct f2fs_sb_info *sbi,stat_inc_seg_type (sbi, curseg); } +void allocate_segment_for_resize (struct f2fs_sb_info *sbi,int type, +unsigned int start,unsigned int end) +{ +struct curseg_info *curseg =CURSEG_I (sbi, type); +unsigned int segno; + +down_read (&SM_I (sbi)->curseg_lock); +mutex_lock (&curseg->curseg_mutex); +down_write (&SIT_I (sbi)->sentry_lock); + + segno =CURSEG_I (sbi, type)->segno; +if (segno < start || segno > end) + goto unlock; + +if (f2fs_need_SSR (sbi) &&get_ssr_segment (sbi, type)) +change_curseg (sbi, type); +else +new_curseg (sbi, type, true); + +stat_inc_seg_type (sbi, curseg); + +locate_dirty_segment (sbi, segno); +unlock: +up_write (&SIT_I (sbi)->sentry_lock); + +if (segno != curseg->segno) +f2fs_msg (sbi->sb,KERN_NOTICE , +"For resize: curseg of type %d: %u ==> %u" , + type, segno, curseg->segno); + +mutex_unlock (&curseg->curseg_mutex); +up_read (&SM_I (sbi)->curseg_lock); +} +void f2fs_allocate_new_segments (struct f2fs_sb_info *sbi) {struct curseg_info *curseg;
@@ -3774,7 +3808,7 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)struct f2fs_journal *journal = curseg->journal;struct sit_entry_set *ses, *tmp;struct list_head *head = &SM_I (sbi)->sit_entry_set; -bool to_journal = true; +bool to_journal = !is_sbi_flag_set (sbi,SBI_IS_RESIZEFS );struct seg_entry *se;down_write (&sit_i->sentry_lock);
@@ -3793,7 +3827,8 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)* entries, remove all entries from journal and add and account * them in sit entry set. */ -if (!__has_cursum_space (journal, sit_i->dirty_sentries,SIT_JOURNAL )) +if (!__has_cursum_space (journal, sit_i->dirty_sentries,SIT_JOURNAL ) || + !to_journal)remove_sits_in_journal (sbi);/*
fs/f2fs/super.c +4 -0
@@ -3296,6 +3296,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)mutex_init (&sbi->gc_mutex);mutex_init (&sbi->writepages);mutex_init (&sbi->cp_mutex); +mutex_init (&sbi->resize_mutex);init_rwsem (&sbi->node_write);init_rwsem (&sbi->node_change);
@@ -3368,6 +3369,9 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent) sbi->interval_time[DISABLE_TIME ] =DEF_DISABLE_QUICK_INTERVAL ; } +if (__is_set_ckpt_flags (F2FS_CKPT (sbi),CP_FSCK_FLAG )) +set_sbi_flag (sbi,SBI_NEED_FSCK ); +/* Initialize device list */ err =f2fs_scan_devices (sbi);if (err) {
Signature · 1 line
1.8.3.1
Quoted 51 lines · sunqiuyang
From: Qiuyang Sun <[email protected]>
This ioctl shrinks a given length (aligned to sections) from end of the main area. Any cursegs and valid blocks will be moved out before invalidating the range.
This feature can be used for adjusting partition sizes online. --
Changlog v1 ==> v2:Sahitya Tummala:
- Add this ioctl forf2fs_compat_ioctl()as well.
- Fix debugfs status to reflect the online resize changes.
- Fix potential race between online resize path and allocate new data block path or gc path.Others:
- Rename some identifiers.
- Add some error handling branches.
- Clear sbi->next_victim_seg[BG_GC/FG_GC] in shrinking range.
--
Changelog v2 ==> v3:
Implement this interface as ext4's, and change the parameter from shrunk bytes to new block count of F2FS.
--
Changelog v3 ==> v4:
- During resizing, force to emptysit_journaland forbid adding new entries to it, in order to avoid invalid segno in journal after resize.
- Reducesbi->user_block_countbefore resize starts.
- Commit the updated superblock first, and then update in-memory metadata only when the former succeeds.
- Target block count must align to sections.
--
Changelog v4 ==> v5:
Write checkpoint before and after committing the new superblock, w/oCP_FSCK_FLAGrespectively, so that the FS can be fixed by fsck even if resize fails after the new superblock is committed.
--
Changelog v5 ==> v6:
- Infree_segment_range(), reduce granularity ofgc_mutex.
- Add protection on curseg migration.
--
Changelog v6 ==> v7:
- Addfreeze_bdev()andthaw_bdev()for resize fs.
- RemoveCUR_MAIN_SECSand useMAIN_SECSdirectly for allocation.
- Recoversuper_blockand FS metadata when resize fails.
--
Changelog v7 ==> v8:
- No need to clearCP_FSCK_FLAGinupdate_ckpt_flags().
- Clean up the sb and fs metadata update functions forresize_fs.
Quoted 3 lines
Signed-off-by Qiuyang Sun <[email protected]>Signed-off-by Chao Yu <[email protected]>Signed-off-by Sahitya Tummala <[email protected]>
Qiuyang, could you please add one f2fs individual testcase in fstest suit to do simple test with this ioctl.
e.g.
- mkfs & mount
- fragment image
- resizefs ioctl
- check fs size via statfs
- umount & fsck
- maybe mount & check again
Thanks,