Skip to content

Commit 105ddc9

Browse files
Ashish Samanttorvalds
authored andcommitted
ocfs2: fstrim: Fix start offset of first cluster group during fstrim
The first cluster group descriptor is not stored at the start of the group but at an offset from the start. We need to take this into account while doing fstrim on the first cluster group. Otherwise we will wrongly start fstrim a few blocks after the desired start block and the range can cross over into the next cluster group and zero out the group descriptor there. This can cause filesytem corruption that cannot be fixed by fsck. Link: http://lkml.kernel.org/r/1507835579-7308-1-git-send-email-ashish.samant@oracle.com Signed-off-by: Ashish Samant <ashish.samant@oracle.com> Reviewed-by: Junxiao Bi <junxiao.bi@oracle.com> Reviewed-by: Joseph Qi <jiangqi903@gmail.com> Cc: Mark Fasheh <mfasheh@versity.com> Cc: Joel Becker <jlbec@evilplan.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b83d7e4 commit 105ddc9

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

fs/ocfs2/alloc.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7304,21 +7304,32 @@ int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
73047304

73057305
static int ocfs2_trim_extent(struct super_block *sb,
73067306
struct ocfs2_group_desc *gd,
7307-
u32 start, u32 count)
7307+
u64 group, u32 start, u32 count)
73087308
{
73097309
u64 discard, bcount;
7310+
struct ocfs2_super *osb = OCFS2_SB(sb);
73107311

73117312
bcount = ocfs2_clusters_to_blocks(sb, count);
7312-
discard = le64_to_cpu(gd->bg_blkno) +
7313-
ocfs2_clusters_to_blocks(sb, start);
7313+
discard = ocfs2_clusters_to_blocks(sb, start);
7314+
7315+
/*
7316+
* For the first cluster group, the gd->bg_blkno is not at the start
7317+
* of the group, but at an offset from the start. If we add it while
7318+
* calculating discard for first group, we will wrongly start fstrim a
7319+
* few blocks after the desried start block and the range can cross
7320+
* over into the next cluster group. So, add it only if this is not
7321+
* the first cluster group.
7322+
*/
7323+
if (group != osb->first_cluster_group_blkno)
7324+
discard += le64_to_cpu(gd->bg_blkno);
73147325

73157326
trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
73167327

73177328
return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
73187329
}
73197330

73207331
static int ocfs2_trim_group(struct super_block *sb,
7321-
struct ocfs2_group_desc *gd,
7332+
struct ocfs2_group_desc *gd, u64 group,
73227333
u32 start, u32 max, u32 minbits)
73237334
{
73247335
int ret = 0, count = 0, next;
@@ -7337,7 +7348,7 @@ static int ocfs2_trim_group(struct super_block *sb,
73377348
next = ocfs2_find_next_bit(bitmap, max, start);
73387349

73397350
if ((next - start) >= minbits) {
7340-
ret = ocfs2_trim_extent(sb, gd,
7351+
ret = ocfs2_trim_extent(sb, gd, group,
73417352
start, next - start);
73427353
if (ret < 0) {
73437354
mlog_errno(ret);
@@ -7435,7 +7446,8 @@ int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
74357446
}
74367447

74377448
gd = (struct ocfs2_group_desc *)gd_bh->b_data;
7438-
cnt = ocfs2_trim_group(sb, gd, first_bit, last_bit, minlen);
7449+
cnt = ocfs2_trim_group(sb, gd, group,
7450+
first_bit, last_bit, minlen);
74397451
brelse(gd_bh);
74407452
gd_bh = NULL;
74417453
if (cnt < 0) {

0 commit comments

Comments
 (0)