Skip to content

Commit a9f2a29

Browse files
jankaratytso
authored andcommitted
ext4: use locality group preallocation for small closed files
Curently we don't use any preallocation when a file is already closed when allocating blocks (from writeback code when converting delayed allocation). However for small files, using locality group preallocation is actually desirable as that is not specific to a particular file. Rather it is a method to pack small files together to reduce fragmentation and for that the fact the file is closed is actually even stronger hint the file would benefit from packing. So change the logic to allow locality group preallocation in this case. Fixes: 196e402 ("ext4: improve cr 0 / cr 1 group scanning") CC: stable@kernel.org Reported-and-tested-by: Stefan Wahren <stefan.wahren@i2se.com> Tested-by: Ojaswin Mujoo <ojaswin@linux.ibm.com> Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/all/0d81a7c2-46b7-6010-62a4-3e6cfc1628d6@i2se.com/ Link: https://lore.kernel.org/r/20220908092136.11770-4-jack@suse.cz Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 613c5a8 commit a9f2a29

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

fs/ext4/mballoc.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,32 +5195,35 @@ static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
51955195
struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
51965196
int bsbits = ac->ac_sb->s_blocksize_bits;
51975197
loff_t size, isize;
5198+
bool inode_pa_eligible, group_pa_eligible;
51985199

51995200
if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
52005201
return;
52015202

52025203
if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
52035204
return;
52045205

5206+
group_pa_eligible = sbi->s_mb_group_prealloc > 0;
5207+
inode_pa_eligible = true;
52055208
size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
52065209
isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
52075210
>> bsbits;
52085211

5212+
/* No point in using inode preallocation for closed files */
52095213
if ((size == isize) && !ext4_fs_is_busy(sbi) &&
5210-
!inode_is_open_for_write(ac->ac_inode)) {
5211-
ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
5212-
return;
5213-
}
5214+
!inode_is_open_for_write(ac->ac_inode))
5215+
inode_pa_eligible = false;
52145216

5215-
if (sbi->s_mb_group_prealloc <= 0) {
5216-
ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
5217-
return;
5218-
}
5219-
5220-
/* don't use group allocation for large files */
52215217
size = max(size, isize);
5222-
if (size > sbi->s_mb_stream_request) {
5223-
ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
5218+
/* Don't use group allocation for large files */
5219+
if (size > sbi->s_mb_stream_request)
5220+
group_pa_eligible = false;
5221+
5222+
if (!group_pa_eligible) {
5223+
if (inode_pa_eligible)
5224+
ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
5225+
else
5226+
ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
52245227
return;
52255228
}
52265229

0 commit comments

Comments
 (0)