Skip to content

Commit 04f0b2e

Browse files
qiuyangsJaegeuk Kim
authored andcommitted
f2fs: ioctl for removing a range from F2FS
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. History of the patch: 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. - Implement this interface as ext4's, and change the parameter from shrunk bytes to new block count of F2FS. - 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. - 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. - In free_segment_range(), reduce granularity of gc_mutex. - Add protection on curseg migration. - 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. - No need to clear CP_FSCK_FLAG in update_ckpt_flags(). - Clean up the sb and fs metadata update functions for resize_fs. Geert Uytterhoeven: - Use div_u64*() for 64-bit divisions Arnd Bergmann: - Not all architectures support get_user() with a 64-bit argument: ERROR: "__get_user_bad" [fs/f2fs/f2fs.ko] undefined! Use copy_from_user() here, this will always work. Signed-off-by: Qiuyang Sun <sunqiuyang@huawei.com> Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Sahitya Tummala <stummala@codeaurora.org> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Reviewed-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 5043a96 commit 04f0b2e

7 files changed

Lines changed: 260 additions & 5 deletions

File tree

‎fs/f2fs/checkpoint.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,8 @@ static void update_ckpt_flags(struct f2fs_sb_info *sbi, struct cp_control *cpc)
13021302
else
13031303
__clear_ckpt_flags(ckpt, CP_ORPHAN_PRESENT_FLAG);
13041304

1305-
if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1305+
if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) ||
1306+
is_sbi_flag_set(sbi, SBI_IS_RESIZEFS))
13061307
__set_ckpt_flags(ckpt, CP_FSCK_FLAG);
13071308

13081309
if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))

‎fs/f2fs/debug.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ static DEFINE_MUTEX(f2fs_stat_mutex);
2727
static void update_general_status(struct f2fs_sb_info *sbi)
2828
{
2929
struct f2fs_stat_info *si = F2FS_STAT(sbi);
30+
struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
3031
int i;
3132

33+
/* these will be changed if online resize is done */
34+
si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
35+
si->main_area_sections = le32_to_cpu(raw_super->section_count);
36+
si->main_area_zones = si->main_area_sections /
37+
le32_to_cpu(raw_super->secs_per_zone);
38+
3239
/* validation check of the segment numbers */
3340
si->hit_largest = atomic64_read(&sbi->read_hit_largest);
3441
si->hit_cached = atomic64_read(&sbi->read_hit_cached);

‎fs/f2fs/f2fs.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
415415
#define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
416416
#define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
417417
#define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
418+
#define F2FS_IOC_RESIZE_FS _IOW(F2FS_IOCTL_MAGIC, 16, __u64)
418419

419420
#define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
420421
#define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
@@ -1116,6 +1117,7 @@ enum {
11161117
SBI_QUOTA_NEED_FLUSH, /* need to flush quota info in CP */
11171118
SBI_QUOTA_SKIP_FLUSH, /* skip flushing quota in current CP */
11181119
SBI_QUOTA_NEED_REPAIR, /* quota file may be corrupted */
1120+
SBI_IS_RESIZEFS, /* resizefs is in process */
11191121
};
11201122

11211123
enum {
@@ -1236,6 +1238,7 @@ struct f2fs_sb_info {
12361238
unsigned int segs_per_sec; /* segments per section */
12371239
unsigned int secs_per_zone; /* sections per zone */
12381240
unsigned int total_sections; /* total section count */
1241+
struct mutex resize_mutex; /* for resize exclusion */
12391242
unsigned int total_node_count; /* total node block count */
12401243
unsigned int total_valid_node_count; /* valid node block count */
12411244
loff_t max_file_blocks; /* max block index of file */
@@ -3058,6 +3061,8 @@ block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
30583061
int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
30593062
void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
30603063
int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
3064+
void allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
3065+
unsigned int start, unsigned int end);
30613066
void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
30623067
int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
30633068
bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
@@ -3196,6 +3201,7 @@ block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
31963201
int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
31973202
unsigned int segno);
31983203
void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
3204+
int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
31993205

32003206
/*
32013207
* recovery.c

‎fs/f2fs/file.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,6 +3059,27 @@ static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
30593059
return f2fs_precache_extents(file_inode(filp));
30603060
}
30613061

3062+
static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
3063+
{
3064+
struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
3065+
__u64 block_count;
3066+
int ret;
3067+
3068+
if (!capable(CAP_SYS_ADMIN))
3069+
return -EPERM;
3070+
3071+
if (f2fs_readonly(sbi->sb))
3072+
return -EROFS;
3073+
3074+
if (copy_from_user(&block_count, (void __user *)arg,
3075+
sizeof(block_count)))
3076+
return -EFAULT;
3077+
3078+
ret = f2fs_resize_fs(sbi, block_count);
3079+
3080+
return ret;
3081+
}
3082+
30623083
long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
30633084
{
30643085
if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
@@ -3115,6 +3136,8 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
31153136
return f2fs_ioc_set_pin_file(filp, arg);
31163137
case F2FS_IOC_PRECACHE_EXTENTS:
31173138
return f2fs_ioc_precache_extents(filp, arg);
3139+
case F2FS_IOC_RESIZE_FS:
3140+
return f2fs_ioc_resize_fs(filp, arg);
31183141
default:
31193142
return -ENOTTY;
31203143
}
@@ -3228,6 +3251,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
32283251
case F2FS_IOC_GET_PIN_FILE:
32293252
case F2FS_IOC_SET_PIN_FILE:
32303253
case F2FS_IOC_PRECACHE_EXTENTS:
3254+
case F2FS_IOC_RESIZE_FS:
32313255
break;
32323256
default:
32333257
return -ENOIOCTLCMD;

‎fs/f2fs/gc.c‎

Lines changed: 180 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,11 @@ static int get_victim_by_default(struct f2fs_sb_info *sbi,
311311
struct sit_info *sm = SIT_I(sbi);
312312
struct victim_sel_policy p;
313313
unsigned int secno, last_victim;
314-
unsigned int last_segment = MAIN_SEGS(sbi);
314+
unsigned int last_segment;
315315
unsigned int nsearched = 0;
316316

317317
mutex_lock(&dirty_i->seglist_lock);
318+
last_segment = MAIN_SECS(sbi) * sbi->segs_per_sec;
318319

319320
p.alloc_mode = alloc_mode;
320321
select_policy(sbi, gc_type, type, &p);
@@ -405,7 +406,8 @@ static int get_victim_by_default(struct f2fs_sb_info *sbi,
405406
sm->last_victim[p.gc_mode] = last_victim + 1;
406407
else
407408
sm->last_victim[p.gc_mode] = segno + 1;
408-
sm->last_victim[p.gc_mode] %= MAIN_SEGS(sbi);
409+
sm->last_victim[p.gc_mode] %=
410+
(MAIN_SECS(sbi) * sbi->segs_per_sec);
409411
break;
410412
}
411413
}
@@ -1361,3 +1363,179 @@ void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
13611363
SIT_I(sbi)->last_victim[ALLOC_NEXT] =
13621364
GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
13631365
}
1366+
1367+
static int free_segment_range(struct f2fs_sb_info *sbi, unsigned int start,
1368+
unsigned int end)
1369+
{
1370+
int type;
1371+
unsigned int segno, next_inuse;
1372+
int err = 0;
1373+
1374+
/* Move out cursegs from the target range */
1375+
for (type = CURSEG_HOT_DATA; type < NR_CURSEG_TYPE; type++)
1376+
allocate_segment_for_resize(sbi, type, start, end);
1377+
1378+
/* do GC to move out valid blocks in the range */
1379+
for (segno = start; segno <= end; segno += sbi->segs_per_sec) {
1380+
struct gc_inode_list gc_list = {
1381+
.ilist = LIST_HEAD_INIT(gc_list.ilist),
1382+
.iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS),
1383+
};
1384+
1385+
mutex_lock(&sbi->gc_mutex);
1386+
do_garbage_collect(sbi, segno, &gc_list, FG_GC);
1387+
mutex_unlock(&sbi->gc_mutex);
1388+
put_gc_inode(&gc_list);
1389+
1390+
if (get_valid_blocks(sbi, segno, true))
1391+
return -EAGAIN;
1392+
}
1393+
1394+
err = f2fs_sync_fs(sbi->sb, 1);
1395+
if (err)
1396+
return err;
1397+
1398+
next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start);
1399+
if (next_inuse <= end) {
1400+
f2fs_msg(sbi->sb, KERN_ERR,
1401+
"segno %u should be free but still inuse!", next_inuse);
1402+
f2fs_bug_on(sbi, 1);
1403+
}
1404+
return err;
1405+
}
1406+
1407+
static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs)
1408+
{
1409+
struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi);
1410+
int section_count = le32_to_cpu(raw_sb->section_count);
1411+
int segment_count = le32_to_cpu(raw_sb->segment_count);
1412+
int segment_count_main = le32_to_cpu(raw_sb->segment_count_main);
1413+
long long block_count = le64_to_cpu(raw_sb->block_count);
1414+
int segs = secs * sbi->segs_per_sec;
1415+
1416+
raw_sb->section_count = cpu_to_le32(section_count + secs);
1417+
raw_sb->segment_count = cpu_to_le32(segment_count + segs);
1418+
raw_sb->segment_count_main = cpu_to_le32(segment_count_main + segs);
1419+
raw_sb->block_count = cpu_to_le64(block_count +
1420+
(long long)segs * sbi->blocks_per_seg);
1421+
}
1422+
1423+
static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
1424+
{
1425+
int segs = secs * sbi->segs_per_sec;
1426+
long long user_block_count =
1427+
le64_to_cpu(F2FS_CKPT(sbi)->user_block_count);
1428+
1429+
SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs;
1430+
MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs;
1431+
FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
1432+
FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs;
1433+
F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count +
1434+
(long long)segs * sbi->blocks_per_seg);
1435+
}
1436+
1437+
int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count)
1438+
{
1439+
__u64 old_block_count, shrunk_blocks;
1440+
unsigned int secs;
1441+
int gc_mode, gc_type;
1442+
int err = 0;
1443+
__u32 rem;
1444+
1445+
old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
1446+
if (block_count > old_block_count)
1447+
return -EINVAL;
1448+
1449+
/* new fs size should align to section size */
1450+
div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem);
1451+
if (rem)
1452+
return -EINVAL;
1453+
1454+
if (block_count == old_block_count)
1455+
return 0;
1456+
1457+
if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1458+
f2fs_msg(sbi->sb, KERN_ERR,
1459+
"Should run fsck to repair first.");
1460+
return -EINVAL;
1461+
}
1462+
1463+
if (test_opt(sbi, DISABLE_CHECKPOINT)) {
1464+
f2fs_msg(sbi->sb, KERN_ERR,
1465+
"Checkpoint should be enabled.");
1466+
return -EINVAL;
1467+
}
1468+
1469+
freeze_bdev(sbi->sb->s_bdev);
1470+
1471+
shrunk_blocks = old_block_count - block_count;
1472+
secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
1473+
spin_lock(&sbi->stat_lock);
1474+
if (shrunk_blocks + valid_user_blocks(sbi) +
1475+
sbi->current_reserved_blocks + sbi->unusable_block_count +
1476+
F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count)
1477+
err = -ENOSPC;
1478+
else
1479+
sbi->user_block_count -= shrunk_blocks;
1480+
spin_unlock(&sbi->stat_lock);
1481+
if (err) {
1482+
thaw_bdev(sbi->sb->s_bdev, sbi->sb);
1483+
return err;
1484+
}
1485+
1486+
mutex_lock(&sbi->resize_mutex);
1487+
set_sbi_flag(sbi, SBI_IS_RESIZEFS);
1488+
1489+
mutex_lock(&DIRTY_I(sbi)->seglist_lock);
1490+
1491+
MAIN_SECS(sbi) -= secs;
1492+
1493+
for (gc_mode = 0; gc_mode < MAX_GC_POLICY; gc_mode++)
1494+
if (SIT_I(sbi)->last_victim[gc_mode] >=
1495+
MAIN_SECS(sbi) * sbi->segs_per_sec)
1496+
SIT_I(sbi)->last_victim[gc_mode] = 0;
1497+
1498+
for (gc_type = BG_GC; gc_type <= FG_GC; gc_type++)
1499+
if (sbi->next_victim_seg[gc_type] >=
1500+
MAIN_SECS(sbi) * sbi->segs_per_sec)
1501+
sbi->next_victim_seg[gc_type] = NULL_SEGNO;
1502+
1503+
mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
1504+
1505+
err = free_segment_range(sbi, MAIN_SECS(sbi) * sbi->segs_per_sec,
1506+
MAIN_SEGS(sbi) - 1);
1507+
if (err)
1508+
goto out;
1509+
1510+
update_sb_metadata(sbi, -secs);
1511+
1512+
err = f2fs_commit_super(sbi, false);
1513+
if (err) {
1514+
update_sb_metadata(sbi, secs);
1515+
goto out;
1516+
}
1517+
1518+
update_fs_metadata(sbi, -secs);
1519+
clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
1520+
err = f2fs_sync_fs(sbi->sb, 1);
1521+
if (err) {
1522+
update_fs_metadata(sbi, secs);
1523+
update_sb_metadata(sbi, secs);
1524+
f2fs_commit_super(sbi, false);
1525+
}
1526+
out:
1527+
if (err) {
1528+
set_sbi_flag(sbi, SBI_NEED_FSCK);
1529+
f2fs_msg(sbi->sb, KERN_ERR,
1530+
"resize_fs failed, should run fsck to repair!");
1531+
1532+
MAIN_SECS(sbi) += secs;
1533+
spin_lock(&sbi->stat_lock);
1534+
sbi->user_block_count += shrunk_blocks;
1535+
spin_unlock(&sbi->stat_lock);
1536+
}
1537+
clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
1538+
mutex_unlock(&sbi->resize_mutex);
1539+
thaw_bdev(sbi->sb->s_bdev, sbi->sb);
1540+
return err;
1541+
}

‎fs/f2fs/segment.c‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,40 @@ static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
26572657
stat_inc_seg_type(sbi, curseg);
26582658
}
26592659

2660+
void allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
2661+
unsigned int start, unsigned int end)
2662+
{
2663+
struct curseg_info *curseg = CURSEG_I(sbi, type);
2664+
unsigned int segno;
2665+
2666+
down_read(&SM_I(sbi)->curseg_lock);
2667+
mutex_lock(&curseg->curseg_mutex);
2668+
down_write(&SIT_I(sbi)->sentry_lock);
2669+
2670+
segno = CURSEG_I(sbi, type)->segno;
2671+
if (segno < start || segno > end)
2672+
goto unlock;
2673+
2674+
if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
2675+
change_curseg(sbi, type);
2676+
else
2677+
new_curseg(sbi, type, true);
2678+
2679+
stat_inc_seg_type(sbi, curseg);
2680+
2681+
locate_dirty_segment(sbi, segno);
2682+
unlock:
2683+
up_write(&SIT_I(sbi)->sentry_lock);
2684+
2685+
if (segno != curseg->segno)
2686+
f2fs_msg(sbi->sb, KERN_NOTICE,
2687+
"For resize: curseg of type %d: %u ==> %u",
2688+
type, segno, curseg->segno);
2689+
2690+
mutex_unlock(&curseg->curseg_mutex);
2691+
up_read(&SM_I(sbi)->curseg_lock);
2692+
}
2693+
26602694
void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
26612695
{
26622696
struct curseg_info *curseg;
@@ -3786,7 +3820,7 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
37863820
struct f2fs_journal *journal = curseg->journal;
37873821
struct sit_entry_set *ses, *tmp;
37883822
struct list_head *head = &SM_I(sbi)->sit_entry_set;
3789-
bool to_journal = true;
3823+
bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
37903824
struct seg_entry *se;
37913825

37923826
down_write(&sit_i->sentry_lock);
@@ -3805,7 +3839,8 @@ void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
38053839
* entries, remove all entries from journal and add and account
38063840
* them in sit entry set.
38073841
*/
3808-
if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
3842+
if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
3843+
!to_journal)
38093844
remove_sits_in_journal(sbi);
38103845

38113846
/*

‎fs/f2fs/super.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,6 +3218,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
32183218
mutex_init(&sbi->gc_mutex);
32193219
mutex_init(&sbi->writepages);
32203220
mutex_init(&sbi->cp_mutex);
3221+
mutex_init(&sbi->resize_mutex);
32213222
init_rwsem(&sbi->node_write);
32223223
init_rwsem(&sbi->node_change);
32233224

@@ -3290,6 +3291,9 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
32903291
sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
32913292
}
32923293

3294+
if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
3295+
set_sbi_flag(sbi, SBI_NEED_FSCK);
3296+
32933297
/* Initialize device list */
32943298
err = f2fs_scan_devices(sbi);
32953299
if (err) {

0 commit comments

Comments
 (0)