Skip to contentSkip to conversation

ocfs2: fix data corruption by fallocate

PATCH Superseded Junxiao Bi 3 participants 10 messages May 21, 2021 – May 26, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Junxiao Bi

author
#0 lore raw

PATCH v2 1/1 ocfs2: fix data corruption by fallocate

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

    qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
             -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

    fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
    fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

 fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +47 -2
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
 	return ret;
 }
 
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
 /*
  * Parts of this function taken from xfs_change_file_space()
  */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 {
 	int ret;
 	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
 	struct buffer_head *di_bh = NULL;
 	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		goto out_inode_unlock;
 	}
 
+	orig_isize = i_size_read(inode);
 	switch (sr->l_whence) {
 	case 0: /*SEEK_SET*/
 		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		sr->l_start += f_pos;
 		break;
 	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
 		break;
 	default:
 		ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 	default:
 		ret = -EINVAL;
 	}
+
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size)
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
 	if (ret) {
 		mlog_errno(ret);
Signature · 1 line
2.24.3 (Apple Git-128)
maintainer
#1 lore raw

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 122 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

    qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
             -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

    fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
    fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

 fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +47 -2
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
 	return ret;
 }
 
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
 /*
  * Parts of this function taken from xfs_change_file_space()
  */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 {
 	int ret;
 	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
 	struct buffer_head *di_bh = NULL;
 	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		goto out_inode_unlock;
 	}
 
+	orig_isize = i_size_read(inode);
 	switch (sr->l_whence) {
 	case 0: /*SEEK_SET*/
 		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		sr->l_start += f_pos;
 		break;
 	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
 		break;
 	default:
 		ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 	default:
 		ret = -EINVAL;
 	}
+
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size)
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
 	if (ret) {
 		mlog_errno(ret);
author
#2 lore raw

That will not work, buffer write zero first, then update i_size, in between writeback could be kicked in and clear those dirty buffers because they were out of i_size. Beside that, OCFS2_IOC_RESVSP64 was never doing right job, it didn't take care eof blocks in the last cluster, that made even a simple fallocate to extend file size could cause corruption. This patch fixed both issues.

Thanks,

Junxiao.

Quoted 122 lines · Joseph Qi

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 114 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

     qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
              -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

     fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
     fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

  fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +43 -2
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
  	return ret;
  }
 
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
  /*
   * Parts of this function taken from xfs_change_file_space()
   */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  {
  	int ret;
  	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	struct buffer_head *di_bh = NULL;
  	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  		goto out_inode_unlock;
  	}
 
+	orig_isize = i_size_read(inode);
  	switch (sr->l_whence) {
  	case 0: /*SEEK_SET*/
  		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  		sr->l_start += f_pos;
  		break;
  	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
  		break;
  	default:
  		ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  	default:
  		ret = -EINVAL;
  	}
+
Quoted 8 lines

fs/ocfs2/file.c static int __ocfs2_change_file_space(struct file *file, struct inode *inode, · line 2007

Quoted 8 lines
fs/ocfs2/file.c +4 -0
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size)
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
  	up_write(&OCFS2_I(inode)->ip_alloc_sem);
  	if (ret) {
  		mlog_errno(ret);

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

maintainer Reviewed-by
#3 lore raw

Thanks for the explanations.
A tiny cleanup, we can use 'orig_isize' instead of i_size_read() later in __ocfs2_change_file_space().
Other looks good to me.

Quoted 135 lines · Junxiao Bi

That will not work, buffer write zero first, then update i_size, in between writeback could be kicked in and clear those dirty buffers because they were out of i_size. Beside that, OCFS2_IOC_RESVSP64 was never doing right job, it didn't take care eof blocks in the last cluster, that made even a simple fallocate to extend file size could cause corruption. This patch fixed both issues.

Thanks,

Junxiao.

Quoted 128 lines · Joseph Qi

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 120 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

     qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
              -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

     fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

  fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--

  1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +0 -0
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
       return ret;
   }

  +/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */

fs/ocfs2/file.c +32 -0
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+                    u64 start, u64 len)
+{
+    int ret;
+    u64 start_block, end_block, nr_blocks;
+    u64 p_block, offset;
+    u32 cluster, p_cluster, nr_clusters;
+    struct super_block *sb = inode->i_sb;
+    u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+    if (start + len < end)
+        end = start + len;
+
+    start_block = ocfs2_blocks_for_bytes(sb, start);
+    end_block = ocfs2_blocks_for_bytes(sb, end);
+    nr_blocks = end_block - start_block;
+    if (!nr_blocks)
+        return 0;
+
+    cluster = ocfs2_bytes_to_clusters(sb, start);
+    ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+                &nr_clusters, NULL);
+    if (ret)
+        return ret;
+    if (!p_cluster)
+        return 0;
+
+    offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+    p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+    return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+

  /*
   * Parts of this function taken from xfs_change_file_space() */

fs/ocfs2/file.c +1 -1
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
   {
       int ret;
       s64 llen;
-    loff_t size;
+    loff_t size, orig_isize;
       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
       struct buffer_head *di_bh = NULL;
       handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
           goto out_inode_unlock;
       }
   +    orig_isize = i_size_read(inode);
       switch (sr->l_whence) {

      case 0: /*SEEK_SET*/
          break;

fs/ocfs2/file.c +0 -0
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
           sr->l_start += f_pos;
           break;

      case 2: /*SEEK_END*/
-        sr->l_start += i_size_read(inode);
+        sr->l_start += orig_isize;
          break;
      default:
          ret = -EINVAL;

fs/ocfs2/file.c +0 -0
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,

      default:
          ret = -EINVAL;
      }
+
+    /* zeroout eof blocks in the cluster. */
+    if (!ret && change_size && orig_isize < size)
+        ret = ocfs2_zeroout_partial_cluster(inode, orig_isize, +                    size - orig_isize); up_write(&OCFS2_I(inode)->ip_alloc_sem);
      if (ret) {
          mlog_errno(ret);

author
#4 lore raw

I would like make the following change to the patch, is that ok to you?

fs/ocfs2/file.c +0 -0
index 17469fc7b20e..775657943057 100644
@@ -1999,9 +1999,12 @@ static int __ocfs2_change_file_space(struct file 

*file, struct inode *inode,
        }

        /* zeroout eof blocks in the cluster. */
-       if (!ret && change_size && orig_isize < size)
+       if (!ret && change_size && orig_isize < size) {
                ret = ocfs2_zeroout_partial_cluster(inode, orig_isize, size - orig_isize);
+               if (!ret)
+                       i_size_write(inode, size);
+       }
        up_write(&OCFS2_I(inode)->ip_alloc_sem);
        if (ret) {
                mlog_errno(ret);
@@ -2018,9 +2021,6 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
                goto out_inode_unlock;
        }

-       if (change_size && i_size_read(inode) < size) -               i_size_write(inode, size);
-
        inode->i_ctime = inode->i_mtime = current_time(inode); ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); if (ret < 0)

Thanks,

Junxiao.

Quoted 134 lines · Joseph Qi

Thanks for the explanations.
A tiny cleanup, we can use 'orig_isize' instead of i_size_read() later in __ocfs2_change_file_space().
Other looks good to me.

Quoted 127 lines · Junxiao Bi

That will not work, buffer write zero first, then update i_size, in between writeback could be kicked in and clear those dirty buffers because they were out of i_size. Beside that, OCFS2_IOC_RESVSP64 was never doing right job, it didn't take care eof blocks in the last cluster, that made even a simple fallocate to extend file size could cause corruption. This patch fixed both issues.

Thanks,

Junxiao.

Quoted 120 lines · Joseph Qi

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 112 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

     qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
              -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

     fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

   fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
   1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +41 -2
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
       return ret;
   }
   +/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+                    u64 start, u64 len)
+{
+    int ret;
+    u64 start_block, end_block, nr_blocks;
+    u64 p_block, offset;
+    u32 cluster, p_cluster, nr_clusters;
+    struct super_block *sb = inode->i_sb;
+    u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+    if (start + len < end)
+        end = start + len;
+
+    start_block = ocfs2_blocks_for_bytes(sb, start);
+    end_block = ocfs2_blocks_for_bytes(sb, end);
+    nr_blocks = end_block - start_block;
+    if (!nr_blocks)
+        return 0;
+
+    cluster = ocfs2_bytes_to_clusters(sb, start);
+    ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+                &nr_clusters, NULL);
+    if (ret)
+        return ret;
+    if (!p_cluster)
+        return 0;
+
+    offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+    p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+    return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
   /*
    * Parts of this function taken from xfs_change_file_space()
    */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
   {
       int ret;
       s64 llen;
-    loff_t size;
+    loff_t size, orig_isize;
       struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
       struct buffer_head *di_bh = NULL;
       handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
           goto out_inode_unlock;
       }
   +    orig_isize = i_size_read(inode);
       switch (sr->l_whence) {
       case 0: /*SEEK_SET*/
           break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
           sr->l_start += f_pos;
           break;
       case 2: /*SEEK_END*/
-        sr->l_start += i_size_read(inode);
+        sr->l_start += orig_isize;
           break;
       default:
           ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
       default:
           ret = -EINVAL;
       }
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

fs/ocfs2/file.c static int __ocfs2_change_file_space(struct file *file, struct inode *inode, · line 2007

Quoted 8 lines
fs/ocfs2/file.c +4 -0
+    /* zeroout eof blocks in the cluster. */
+    if (!ret && change_size && orig_isize < size)
+        ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+                    size - orig_isize);
       up_write(&OCFS2_I(inode)->ip_alloc_sem);
       if (ret) {
           mlog_errno(ret);

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

maintainer
#5 lore raw

Can we simply replace i_size_read() with 'orig_isize' and leave isize update along with other dirty inode operations?
I think this makes more comfortable for the dirty inode transaction.

Thanks,
Joseph

Quoted 178 lines · Junxiao Bi

I would like make the following change to the patch, is that ok to you?

fs/ocfs2/file.c +4 -4
index 17469fc7b20e..775657943057 100644
@@ -1999,9 +1999,12 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
         }
 
         /* zeroout eof blocks in the cluster. */
-       if (!ret && change_size && orig_isize < size)
+       if (!ret && change_size && orig_isize < size) {
                 ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
                                         size - orig_isize);
+               if (!ret)
+                       i_size_write(inode, size);
+       }
         up_write(&OCFS2_I(inode)->ip_alloc_sem);
         if (ret) {
                 mlog_errno(ret);
@@ -2018,9 +2021,6 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
                 goto out_inode_unlock;
         }
 
-       if (change_size && i_size_read(inode) < size)
-               i_size_write(inode, size);
-
         inode->i_ctime = inode->i_mtime = current_time(inode);
         ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
         if (ret < 0)

Thanks,

Junxiao.

Quoted 142 lines · Joseph Qi

Thanks for the explanations.
A tiny cleanup, we can use 'orig_isize' instead of i_size_read() later in __ocfs2_change_file_space().
Other looks good to me.

Quoted 135 lines · Junxiao Bi

That will not work, buffer write zero first, then update i_size, in between writeback could be kicked in and clear those dirty buffers because they were out of i_size. Beside that, OCFS2_IOC_RESVSP64 was never doing right job, it didn't take care eof blocks in the last cluster, that made even a simple fallocate to extend file size could cause corruption. This patch fixed both issues.

Thanks,

Junxiao.

Quoted 128 lines · Joseph Qi

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 120 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

      qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
               -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

      fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

   fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--

   1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +0 -0
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
        return ret;
    }

   +/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */

fs/ocfs2/file.c +32 -0
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+                    u64 start, u64 len)
+{
+    int ret;
+    u64 start_block, end_block, nr_blocks;
+    u64 p_block, offset;
+    u32 cluster, p_cluster, nr_clusters;
+    struct super_block *sb = inode->i_sb;
+    u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+    if (start + len < end)
+        end = start + len;
+
+    start_block = ocfs2_blocks_for_bytes(sb, start);
+    end_block = ocfs2_blocks_for_bytes(sb, end);
+    nr_blocks = end_block - start_block;
+    if (!nr_blocks)
+        return 0;
+
+    cluster = ocfs2_bytes_to_clusters(sb, start);
+    ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+                &nr_clusters, NULL);
+    if (ret)
+        return ret;
+    if (!p_cluster)
+        return 0;
+
+    offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+    p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+    return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+

   /*
    * Parts of this function taken from xfs_change_file_space() */

fs/ocfs2/file.c +1 -1
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
    {
        int ret;
        s64 llen;
-    loff_t size;
+    loff_t size, orig_isize;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct buffer_head *di_bh = NULL;
        handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
            goto out_inode_unlock;
        }
    +    orig_isize = i_size_read(inode);
        switch (sr->l_whence) {

       case 0: /*SEEK_SET*/
           break;

fs/ocfs2/file.c +0 -0
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
            sr->l_start += f_pos;
            break;

       case 2: /*SEEK_END*/
-        sr->l_start += i_size_read(inode);
+        sr->l_start += orig_isize;
           break;
       default:
           ret = -EINVAL;

fs/ocfs2/file.c +0 -0
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,

       default:
           ret = -EINVAL;
       }
+
+    /* zeroout eof blocks in the cluster. */
+    if (!ret && change_size && orig_isize < size)
+        ret = ocfs2_zeroout_partial_cluster(inode, orig_isize, +                    size - orig_isize); up_write(&OCFS2_I(inode)->ip_alloc_sem);
       if (ret) {
           mlog_errno(ret);

author
#6 lore raw

After moving there, i_size_write will be protected by ip_alloc_sem, ocfs2_dio_end_io_write will update i_size without holding inode lock, but it does holding ip_alloc_sem.

Thanks,

Junxiao.

Quoted 186 lines · Joseph Qi

Can we simply replace i_size_read() with 'orig_isize' and leave isize update along with other dirty inode operations?
I think this makes more comfortable for the dirty inode transaction.

Thanks,
Joseph

Quoted 178 lines · Junxiao Bi

I would like make the following change to the patch, is that ok to you?

fs/ocfs2/file.c +4 -4
index 17469fc7b20e..775657943057 100644
@@ -1999,9 +1999,12 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
         }
 
         /* zeroout eof blocks in the cluster. */
-       if (!ret && change_size && orig_isize < size)
+       if (!ret && change_size && orig_isize < size) {
                 ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
                                         size - orig_isize);
+               if (!ret)
+                       i_size_write(inode, size);
+       }
         up_write(&OCFS2_I(inode)->ip_alloc_sem);
         if (ret) {
                 mlog_errno(ret);
@@ -2018,9 +2021,6 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
                 goto out_inode_unlock;
         }
 
-       if (change_size && i_size_read(inode) < size)
-               i_size_write(inode, size);
-
         inode->i_ctime = inode->i_mtime = current_time(inode);
         ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
         if (ret < 0)

Thanks,

Junxiao.

Quoted 142 lines · Joseph Qi

Thanks for the explanations.
A tiny cleanup, we can use 'orig_isize' instead of i_size_read() later in __ocfs2_change_file_space().
Other looks good to me.

Quoted 135 lines · Junxiao Bi

That will not work, buffer write zero first, then update i_size, in between writeback could be kicked in and clear those dirty buffers because they were out of i_size. Beside that, OCFS2_IOC_RESVSP64 was never doing right job, it didn't take care eof blocks in the last cluster, that made even a simple fallocate to extend file size could cause corruption. This patch fixed both issues.

Thanks,

Junxiao.

Quoted 128 lines · Joseph Qi

Hi Junxiao,
If change_size is true (!FALLOC_FL_KEEP_SIZE), it will update isize in __ocfs2_change_file_space(). Why do we have to zeroout first?

Thanks,
Joseph

Quoted 120 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

      qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
               -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

      fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0 fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

    fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
    1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +45 -2
index f17c3d33fb18..17469fc7b20e 100644
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
        return ret;
    }
    +/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+                    u64 start, u64 len)
+{
+    int ret;
+    u64 start_block, end_block, nr_blocks;
+    u64 p_block, offset;
+    u32 cluster, p_cluster, nr_clusters;
+    struct super_block *sb = inode->i_sb;
+    u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+    if (start + len < end)
+        end = start + len;
+
+    start_block = ocfs2_blocks_for_bytes(sb, start);
+    end_block = ocfs2_blocks_for_bytes(sb, end);
+    nr_blocks = end_block - start_block;
+    if (!nr_blocks)
+        return 0;
+
+    cluster = ocfs2_bytes_to_clusters(sb, start);
+    ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+                &nr_clusters, NULL);
+    if (ret)
+        return ret;
+    if (!p_cluster)
+        return 0;
+
+    offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+    p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+    return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
    /*
     * Parts of this function taken from xfs_change_file_space()
     */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
    {
        int ret;
        s64 llen;
-    loff_t size;
+    loff_t size, orig_isize;
        struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
        struct buffer_head *di_bh = NULL;
        handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
            goto out_inode_unlock;
        }
    +    orig_isize = i_size_read(inode);
        switch (sr->l_whence) {
        case 0: /*SEEK_SET*/
            break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
            sr->l_start += f_pos;
            break;
        case 2: /*SEEK_END*/
-        sr->l_start += i_size_read(inode);
+        sr->l_start += orig_isize;
            break;
        default:
            ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
        default:
            ret = -EINVAL;
        }
+
+    /* zeroout eof blocks in the cluster. */
+    if (!ret && change_size && orig_isize < size)
+        ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+                    size - orig_isize);
        up_write(&OCFS2_I(inode)->ip_alloc_sem);
        if (ret) {
            mlog_errno(ret);
reviewer
#7 lore raw
In reply to Junxiao Bi
Quoted 38 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

    qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
             -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

    fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
    fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

 fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +0 -0
index f17c3d33fb18..17469fc7b20e 100644

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1863

Quoted 9 lines
fs/ocfs2/file.c +6 -0
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
 	return ret;
 }
 
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.

You write this here but ...

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1876

Quoted 13 lines
fs/ocfs2/file.c +13 -0
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;

... here you check actually something else and I don't see where else would the trimming happen.

Honza

Quoted 64 lines
fs/ocfs2/file.c +28 -2
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
 /*
  * Parts of this function taken from xfs_change_file_space()
  */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 {
 	int ret;
 	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
 	struct buffer_head *di_bh = NULL;
 	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		goto out_inode_unlock;
 	}
 
+	orig_isize = i_size_read(inode);
 	switch (sr->l_whence) {
 	case 0: /*SEEK_SET*/
 		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 		sr->l_start += f_pos;
 		break;
 	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
 		break;
 	default:
 		ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
 	default:
 		ret = -EINVAL;
 	}
+
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size)
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
 	if (ret) {
 		mlog_errno(ret);
Signature · 1 line
2.24.3 (Apple Git-128)
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#8 lore raw
Quoted 50 lines · Jan Kara
Quoted 38 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

     qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
              -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

     fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
     fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

  fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +0 -0
index f17c3d33fb18..17469fc7b20e 100644

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1863

Quoted 9 lines
fs/ocfs2/file.c +6 -0
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
  	return ret;
  }
 
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.

You write this here but ...

Quoted 15 lines

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1876

Quoted 13 lines
fs/ocfs2/file.c +13 -0
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;

... here you check actually something else and I don't see where else would the trimming happen.

Before the "if", end = ocfs2_align_bytes_to_clusters(sb, start), that is the end of the cluster where "start" located.

Thanks,
Junxiao.

Quoted 67 lines

Honza

Quoted 64 lines
fs/ocfs2/file.c +28 -2
+
+	start_block = ocfs2_blocks_for_bytes(sb, start);
+	end_block = ocfs2_blocks_for_bytes(sb, end);
+	nr_blocks = end_block - start_block;
+	if (!nr_blocks)
+		return 0;
+
+	cluster = ocfs2_bytes_to_clusters(sb, start);
+	ret = ocfs2_get_clusters(inode, cluster, &p_cluster,
+				&nr_clusters, NULL);
+	if (ret)
+		return ret;
+	if (!p_cluster)
+		return 0;
+
+	offset = start_block - ocfs2_clusters_to_blocks(sb, cluster);
+	p_block = ocfs2_clusters_to_blocks(sb, p_cluster) + offset;
+	return sb_issue_zeroout(sb, p_block, nr_blocks, GFP_NOFS);
+}
+
  /*
   * Parts of this function taken from xfs_change_file_space()
   */
@@ -1865,7 +1904,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  {
  	int ret;
  	s64 llen;
-	loff_t size;
+	loff_t size, orig_isize;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	struct buffer_head *di_bh = NULL;
  	handle_t *handle;
@@ -1896,6 +1935,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  		goto out_inode_unlock;
  	}
 
+	orig_isize = i_size_read(inode);
  	switch (sr->l_whence) {
  	case 0: /*SEEK_SET*/
  		break;
@@ -1903,7 +1943,7 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  		sr->l_start += f_pos;
  		break;
  	case 2: /*SEEK_END*/
-		sr->l_start += i_size_read(inode);
+		sr->l_start += orig_isize;
  		break;
  	default:
  		ret = -EINVAL;
@@ -1957,6 +1997,11 @@ static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  	default:
  		ret = -EINVAL;
  	}
+
+	/* zeroout eof blocks in the cluster. */
+	if (!ret && change_size && orig_isize < size)
+		ret = ocfs2_zeroout_partial_cluster(inode, orig_isize,
+					size - orig_isize);
  	up_write(&OCFS2_I(inode)->ip_alloc_sem);
  	if (ret) {
  		mlog_errno(ret);
Signature · 1 line
2.24.3 (Apple Git-128)
reviewer
#9 lore raw
Quoted 67 lines · Junxiao Bi
Quoted 49 lines · Jan Kara
Quoted 38 lines · Junxiao Bi

When fallocate punches holes out of inode size, if original isize is in the middle of last cluster, then the part from isize to the end of the cluster will be zeroed with buffer write, at that time isize is not yet updated to match the new size, if writeback is kicked in, it will invoke ocfs2_writepage()->block_write_full_page() where the pages out of inode size will be dropped. That will cause file corruption. Fix this by zero out eof blocks when extending the inode size.

Running the following command with qemu-image 4.2.1 can get a corrupted coverted image file easily.

     qemu-img convert -p -t none -T none -f qcow2 $qcow_image \
              -O qcow2 -o compat=1.1 $qcow_image.conv

The usage of fallocate in qemu is like this, it first punches holes out of inode size, then extend the inode size.

     fallocate(11, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 2276196352, 65536) = 0
     fallocate(11, 0, 2276196352, 65536) = 0

v1: https://www.spinics.net/lists/linux-fsdevel/msg193999.html

---

Changes in v2:
- suggested by Jan Kara, using sb_issue_zeroout to zero eof blocks in disk directly.

  fs/ocfs2/file.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 47 insertions(+), 2 deletions(-)
fs/ocfs2/file.c +0 -0
index f17c3d33fb18..17469fc7b20e 100644

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1862

Quoted 8 lines
fs/ocfs2/file.c +6 -0
@@ -1855,6 +1855,45 @@ int ocfs2_remove_inode_range(struct inode *inode,
  	return ret;
  }
+/*
+ * zero out partial blocks of one cluster.
+ *
+ * start: file offset where zero starts, will be made upper block aligned.
+ * len: it will be trimmed to the end of current cluster if "start + len"
+ *      is bigger than it.

You write this here but ...

Quoted 15 lines

fs/ocfs2/file.c int ocfs2_remove_inode_range(struct inode *inode, · line 1876

Quoted 13 lines
fs/ocfs2/file.c +13 -0
+ */
+static int ocfs2_zeroout_partial_cluster(struct inode *inode,
+					u64 start, u64 len)
+{
+	int ret;
+	u64 start_block, end_block, nr_blocks;
+	u64 p_block, offset;
+	u32 cluster, p_cluster, nr_clusters;
+	struct super_block *sb = inode->i_sb;
+	u64 end = ocfs2_align_bytes_to_clusters(sb, start);
+
+	if (start + len < end)
+		end = start + len;

... here you check actually something else and I don't see where else would the trimming happen.

Quoted 2 lines

Before the "if", end = ocfs2_align_bytes_to_clusters(sb, start), that is the end of the cluster where "start" located.

Ah sorry, I got confused. The code is correct.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR