Skip to content

Commit d63c00e

Browse files
Dmitry Monakhovtytso
authored andcommitted
ext4: mark group as trimmed only if it was fully scanned
Otherwise nonaligned fstrim calls will works inconveniently for iterative scanners, for example: // trim [0,16MB] for group-1, but mark full group as trimmed fstrim -o $((1024*1024*128)) -l $((1024*1024*16)) ./m // handle [16MB,16MB] for group-1, do nothing because group already has the flag. fstrim -o $((1024*1024*144)) -l $((1024*1024*16)) ./m [ Update function documentation for ext4_trim_all_free -- TYT ] Signed-off-by: Dmitry Monakhov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]> Cc: [email protected]
1 parent 0be698e commit d63c00e

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

fs/ext4/mballoc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6395,6 +6395,7 @@ __releases(ext4_group_lock_ptr(sb, e4b->bd_group))
63956395
* @start: first group block to examine
63966396
* @max: last group block to examine
63976397
* @minblocks: minimum extent block count
6398+
* @set_trimmed: set the trimmed flag if at least one block is trimmed
63986399
*
63996400
* ext4_trim_all_free walks through group's block bitmap searching for free
64006401
* extents. When the free extent is found, mark it as used in group buddy
@@ -6404,7 +6405,7 @@ __releases(ext4_group_lock_ptr(sb, e4b->bd_group))
64046405
static ext4_grpblk_t
64056406
ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
64066407
ext4_grpblk_t start, ext4_grpblk_t max,
6407-
ext4_grpblk_t minblocks)
6408+
ext4_grpblk_t minblocks, bool set_trimmed)
64086409
{
64096410
struct ext4_buddy e4b;
64106411
int ret;
@@ -6423,7 +6424,7 @@ ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
64236424
if (!EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) ||
64246425
minblocks < EXT4_SB(sb)->s_last_trim_minblks) {
64256426
ret = ext4_try_to_trim_range(sb, &e4b, start, max, minblocks);
6426-
if (ret >= 0)
6427+
if (ret >= 0 && set_trimmed)
64276428
EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
64286429
} else {
64296430
ret = 0;
@@ -6460,6 +6461,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
64606461
ext4_fsblk_t first_data_blk =
64616462
le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
64626463
ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
6464+
bool whole_group, eof = false;
64636465
int ret = 0;
64646466

64656467
start = range->start >> sb->s_blocksize_bits;
@@ -6478,8 +6480,10 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
64786480
if (minlen > EXT4_CLUSTERS_PER_GROUP(sb))
64796481
goto out;
64806482
}
6481-
if (end >= max_blks)
6483+
if (end >= max_blks - 1) {
64826484
end = max_blks - 1;
6485+
eof = true;
6486+
}
64836487
if (end <= first_data_blk)
64846488
goto out;
64856489
if (start < first_data_blk)
@@ -6493,6 +6497,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
64936497

64946498
/* end now represents the last cluster to discard in this group */
64956499
end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
6500+
whole_group = true;
64966501

64976502
for (group = first_group; group <= last_group; group++) {
64986503
grp = ext4_get_group_info(sb, group);
@@ -6509,12 +6514,13 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
65096514
* change it for the last group, note that last_cluster is
65106515
* already computed earlier by ext4_get_group_no_and_offset()
65116516
*/
6512-
if (group == last_group)
6517+
if (group == last_group) {
65136518
end = last_cluster;
6514-
6519+
whole_group = eof ? true : end == EXT4_CLUSTERS_PER_GROUP(sb) - 1;
6520+
}
65156521
if (grp->bb_free >= minlen) {
65166522
cnt = ext4_trim_all_free(sb, group, first_cluster,
6517-
end, minlen);
6523+
end, minlen, whole_group);
65186524
if (cnt < 0) {
65196525
ret = cnt;
65206526
break;

0 commit comments

Comments
 (0)