Skip to content

Commit 4874360

Browse files
Jaegeuk Kimpundiramit
authored andcommitted
f2fs: return fs_trim if there is no candidate
commit 25290fa5591d81767713db304e0d567bf991786f upstream. If there is no candidate to submit discard command during f2sf_trim_fs, let's return without checkpoint. Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent bab2a03 commit 4874360

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

fs/f2fs/checkpoint.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,11 @@ int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc)
12501250

12511251
/* this is the case of multiple fstrims without any changes */
12521252
if (cpc->reason == CP_DISCARD) {
1253+
if (!exist_trim_candidates(sbi, cpc)) {
1254+
unblock_operations(sbi);
1255+
goto out;
1256+
}
1257+
12531258
if (NM_I(sbi)->dirty_nat_cnt == 0 &&
12541259
SIT_I(sbi)->dirty_sentries == 0 &&
12551260
prefree_segments(sbi) == 0) {

fs/f2fs/f2fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,7 @@ void release_discard_addrs(struct f2fs_sb_info *);
21862186
int npages_for_summary_flush(struct f2fs_sb_info *, bool);
21872187
void allocate_new_segments(struct f2fs_sb_info *);
21882188
int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
2189+
bool exist_trim_candidates(struct f2fs_sb_info *, struct cp_control *);
21892190
struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
21902191
void update_meta_page(struct f2fs_sb_info *, void *, block_t);
21912192
void write_meta_page(struct f2fs_sb_info *, struct page *);

fs/f2fs/segment.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,8 @@ static void __add_discard_entry(struct f2fs_sb_info *sbi,
914914
SM_I(sbi)->nr_discards += end - start;
915915
}
916916

917-
static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
917+
static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
918+
bool check_only)
918919
{
919920
int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
920921
int max_blocks = sbi->blocks_per_seg;
@@ -928,12 +929,12 @@ static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
928929
int i;
929930

930931
if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
931-
return;
932+
return false;
932933

933934
if (!force) {
934935
if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
935936
SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
936-
return;
937+
return false;
937938
}
938939

939940
/* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
@@ -951,8 +952,12 @@ static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
951952
&& (end - start) < cpc->trim_minlen)
952953
continue;
953954

955+
if (check_only)
956+
return true;
957+
954958
__add_discard_entry(sbi, cpc, se, start, end);
955959
}
960+
return false;
956961
}
957962

958963
void release_discard_addrs(struct f2fs_sb_info *sbi)
@@ -1533,6 +1538,19 @@ static const struct segment_allocation default_salloc_ops = {
15331538
.allocate_segment = allocate_segment_by_default,
15341539
};
15351540

1541+
bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
1542+
{
1543+
__u64 trim_start = cpc->trim_start;
1544+
1545+
mutex_lock(&SIT_I(sbi)->sentry_lock);
1546+
for (; trim_start <= cpc->trim_end; trim_start++)
1547+
if (add_discard_addrs(sbi, cpc, true))
1548+
break;
1549+
mutex_unlock(&SIT_I(sbi)->sentry_lock);
1550+
1551+
return trim_start <= cpc->trim_end;
1552+
}
1553+
15361554
int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
15371555
{
15381556
__u64 start = F2FS_BYTES_TO_BLK(range->start);
@@ -2329,7 +2347,7 @@ void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
23292347
/* add discard candidates */
23302348
if (cpc->reason != CP_DISCARD) {
23312349
cpc->trim_start = segno;
2332-
add_discard_addrs(sbi, cpc);
2350+
add_discard_addrs(sbi, cpc, false);
23332351
}
23342352

23352353
if (to_journal) {
@@ -2367,7 +2385,7 @@ void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
23672385
__u64 trim_start = cpc->trim_start;
23682386

23692387
for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
2370-
add_discard_addrs(sbi, cpc);
2388+
add_discard_addrs(sbi, cpc, false);
23712389

23722390
cpc->trim_start = trim_start;
23732391
}

0 commit comments

Comments
 (0)