Re: [PATCH v4] ntfs: handle a failed size rollback in the write error path
From: Hongling Zeng
Date: Tue Oct 20 2026 - 22:56:45 EST
在 2026年10月21日 09:52, Hyunchul Lee 写道:
ok,you are right, I will roll the initialized size back first, and skip the data size rollback when it fails. With the initialized size restored, the clamp keeps it at the old initialized size at most,diff --git a/fs/ntfs/file.c b/fs/ntfs/file.cThe shrink above can clamp initialzied_size to old_data_size. If
index d810ca5904bd..ad032cccfb15 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -934,23 +934,46 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
&ntfs_iomap_folio_ops, NULL);
out:
if (ret < 0 && ret != -EIOCBQUEUED) {
- if (ni->initialized_size != old_init_size) {
- struct ntfs_inode *mrec_ni =
- ntfs_base_inode(ni);
+ int init_err = 0;
+ int data_err = 0;
+ if (ni->data_size != old_data_size) {
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
+
+ truncate_setsize(vi, old_data_size);
+ /*
+ * ntfs_attr_truncate() would reject a compressed
+ * attribute.
+ */
mutex_lock(&mrec_ni->mrec_lock);
- ntfs_attr_set_initialized_size(ni, old_init_size);
+ data_err = __ntfs_attr_truncate_vfs(ni, old_data_size,
+ old_data_size);
mutex_unlock(&mrec_ni->mrec_lock);
+ /* Like ntfs_setattr_size(): stay on the durable size. */
+ if (data_err)
+ i_size_write(vi, ni->data_size);
}
- if (ni->data_size != old_data_size) {
- struct ntfs_inode *mrec_ni =
- ntfs_base_inode(ni);
+ /*
+ * The shrink above may leave the initialized size above
+ * the old one; ntfs_attr_set_initialized_size() rejects
+ * a resident attribute.
+ */
+ if (ni->initialized_size != old_init_size &&
+ NInoNonResident(ni)) {
+ struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
- truncate_setsize(vi, old_data_size);
mutex_lock(&mrec_ni->mrec_lock);
- ntfs_attr_truncate(ni, old_data_size);
+ init_err = ntfs_attr_set_initialized_size(ni,
+ old_init_size);
ntfs_attr_set_initialized_size() fails, the initialized_size remains
instead of being restored to old_init_size. For a compressed file,
this can expose stale data.
Could you restore the initialized_size first and perform the shink only
if that rollback succeeds?
and when the restore fails, both sizes stay extended, which keeps the on-disk initialized size below the data size. Will post v5.
Thanks!