ntfs: trim the sizes after a short write
Subsystems (2)
PATCH v2 1/1 ntfs: trim the sizes after a short write
From: Zhan Xusheng <[email protected]>
ntfs_file_write_iter() moves the data size and, for a non-resident uncompressed file, the initialized size to pos + count before the copy. __ntfs_extend_initialized_size() only zeroes [old initialized size, pos), so the range [pos, pos + count) is initialized only by the copy itself. When iomap_file_buffered_write() copies less than count, it returns a positive short count and the error rollback at out: does not run. The file keeps the size pos + count, and [pos + ret, pos + count) reads back whatever the freed clusters held before, also after a remount. A local user can read the old contents of freed clusters this way.
Run the existing rollback for a short write as well, and roll the two sizes back to the end of what was written instead of to the old sizes. A short copy that ends inside a block can also leave the rest of that block cached as read from disk, so clear the page cache beyond the new initialized size before shrinking it.
---
v2: also clear the cached tail of the last block, which v1 left in place when the write stayed inside the old data size (Baolin).
v1: https://lore.kernel.org/all/[email protected]/
Tested in a VM on ntfs-next b025f526c5d0, with all free space filled with a known pattern first. Short buffered writes of 4M, old data readable after drop_caches and after remount, 3 runs each:
unfixed v1 v2 fallocated, ends block-aligned 3137536 0 0 fallocated, ends inside a block 3141332 212 0 fallocated, unaligned start 3137536 412 0 append 3137536 0 0
A short write 1M past EOF, O_DIRECT, resident and compressed files and full writes give the same results with v2 as with v1.
--- fs/ntfs/file.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-)
fs/ntfs/file.c +26 -7
@@ -928,10 +928,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) ret =iomap_file_buffered_write (iocb, from, &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops,NULL ); out: -if (ret <0 && ret != -EIOCBQUEUED ) { +/* +* Both sizes were moved to end before the copy, so a short write, +* like a failed one, must take back what it did not write. +*/ +if (ret < count && ret != -EIOCBQUEUED ) { +loff_t new_init_size = old_init_size; +loff_t new_data_size = old_data_size;int init_err =0 ;int data_err =0 ; +if (ret >0 ) { + new_init_size =max (old_init_size, pos + ret); + new_data_size =max (old_data_size, pos + ret); + } +/* * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs() * expects the invalidate lock held across the size
@@ -940,13 +951,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)filemap_invalidate_lock (vi->i_mapping);/* ntfs_attr_set_initialized_size() rejects a resident one. */ -if (ni->initialized_size != old_init_size && +if (ni->initialized_size != new_init_size &&NInoNonResident (ni)) {struct ntfs_inode *mrec_ni =ntfs_base_inode (ni); +/* +* A short copy can leave the rest of the last block +* cached as read from disk. Clear what is no longer +* initialized before shrinking the initialized size. +*/ +if (ni->initialized_size > new_init_size) +truncate_pagecache_range (vi, new_init_size, + ni->initialized_size -1 );mutex_lock (&mrec_ni->mrec_lock); init_err =ntfs_attr_set_initialized_size (ni, - old_init_size); + new_init_size);mutex_unlock (&mrec_ni->mrec_lock); }/*
@@ -955,13 +974,13 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)* the uninitialized tail, so skip it unless the rollback * above restored that size. */ -if (ni->data_size != old_data_size && !init_err) { +if (ni->data_size != new_data_size && !init_err) {struct ntfs_inode *mrec_ni =ntfs_base_inode (ni); -truncate_setsize (vi, old_data_size); +truncate_setsize (vi, new_data_size);mutex_lock (&mrec_ni->mrec_lock); - data_err =__ntfs_attr_truncate_vfs (ni, old_data_size, - old_data_size); + data_err =__ntfs_attr_truncate_vfs (ni, new_data_size, + new_data_size);mutex_unlock (&mrec_ni->mrec_lock);/* Like ntfs_setattr_size(): stay on the durable size. */ if (data_err)
base-commit: b025f526c5d04929212c9d5fb7ffc96608f3eacc
Signature · 1 line
2.43.0
Quoted 77 lines · Zhan Xusheng
From: Zhan Xusheng <[email protected]>
ntfs_file_write_iter()moves the data size and, for a non-resident uncompressed file, the initialized size to pos + count before the copy.__ntfs_extend_initialized_size()only zeroes [old initialized size, pos), so the range [pos, pos + count) is initialized only by the copy itself. Wheniomap_file_buffered_write()copies less than count, it returns a positive short count and the error rollback at out: does not run. The file keeps the size pos + count, and [pos + ret, pos + count) reads back whatever the freed clusters held before, also after a remount. A local user can read the old contents of freed clusters this way.Run the existing rollback for a short write as well, and roll the two sizes back to the end of what was written instead of to the old sizes. A short copy that ends inside a block can also leave the rest of that block cached as read from disk, so clear the page cache beyond the new initialized size before shrinking it.
Fixes b041ca562526 ("ntfs: update iomap and address space operations")Signed-off-by Zhan Xusheng <[email protected]>---
v2: also clear the cached tail of the last block, which v1 left in place when the write stayed inside the old data size (Baolin).v1: https://lore.kernel.org/all/[email protected]/
Tested in a VM on ntfs-next
b025f526c5d0, with all free space filled with a known pattern first. Short buffered writes of 4M, old data readable afterdrop_cachesand after remount, 3 runs each:unfixed v1 v2 fallocated, ends block-aligned 3137536 0 0 fallocated, ends inside a block 3141332 212 0 fallocated, unaligned start 3137536 412 0 append 3137536 0 0A short write 1M past EOF,
O_DIRECT, resident and compressed files and full writes give the same results with v2 as with v1.--- fs/ntfs/file.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-)fs/ntfs/file.c +13
-2@@ -928,10 +928,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) ret =iomap_file_buffered_write (iocb, from, &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops,NULL ); out: -if (ret <0 && ret != -EIOCBQUEUED ) { +/* +* Both sizes were moved to end before the copy, so a short write, +* like a failed one, must take back what it did not write. +*/ +if (ret < count && ret != -EIOCBQUEUED ) { +loff_t new_init_size = old_init_size; +loff_t new_data_size = old_data_size;int init_err =0 ;int data_err =0 ; +if (ret >0 ) { + new_init_size =max (old_init_size, pos + ret); + new_data_size =max (old_data_size, pos + ret); + } +/* * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs() * expects the invalidate lock held across the size @@ -940,13 +951,21 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)filemap_invalidate_lock (vi->i_mapping);/* ntfs_attr_set_initialized_size() rejects a resident one. */ -if (ni->initialized_size != old_init_size && +if (ni->initialized_size != new_init_size &&NInoNonResident (ni)) {
fs/ntfs/file.c ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 965
Quoted 10 lines
fs/ntfs/file.c +8
-0struct ntfs_inode *mrec_ni =ntfs_base_inode (ni); +/* +* A short copy can leave the rest of the last block +* cached as read from disk. Clear what is no longer +* initialized before shrinking the initialized size. +*/ +if (ni->initialized_size > new_init_size) +truncate_pagecache_range (vi, new_init_size, + ni->initialized_size -1 );
Hi Xusheng,
Could the upper bound here still leave cached bytes beyond the
temporarily extended initialized size?
For example, with 512-byte blocks and 4 KiB pages, consider an
allocated file with:
old_data_size = 8192
old_init_size = 0
pos = 0 count = 5000 ret = 4200
Before copying, initialized_size becomes 5000. When preparing the
last partial page, __iomap_write_begin() can read [4096, 5120) and mark those blocks uptodate. After the short-copy retry, the successful write ends at 4200. ntfs_iomap_put_folio_non_resident() still sees initialized_size == 5000, which lies beyond the last successfully written block, so it does not clear the tail.
This call then clears only [4200, 5000), leaving [5000, 5120) cached
with the contents read from disk. Since data_size remains 8192, the later truncate_setsize() is skipped, and a subsequent buffered read can still return those cached bytes.
Could we extend the cache cleanup through EOF, using an upper bound
such as max_t(loff_t, i_size_read(vi), ni->initialized_size) - 1, similar to the cleanup in __ntfs_extend_initialized_size()?
Thanks,
Baolin.
Quoted 27 lines
fs/ntfs/file.c +5
-5mutex_lock (&mrec_ni->mrec_lock); init_err =ntfs_attr_set_initialized_size (ni, - old_init_size); + new_init_size);mutex_unlock (&mrec_ni->mrec_lock); }/* @@ -955,13 +974,13 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)* the uninitialized tail, so skip it unless the rollback * above restored that size. */ -if (ni->data_size != old_data_size && !init_err) { +if (ni->data_size != new_data_size && !init_err) {struct ntfs_inode *mrec_ni =ntfs_base_inode (ni); -truncate_setsize (vi, old_data_size); +truncate_setsize (vi, new_data_size);mutex_lock (&mrec_ni->mrec_lock); - data_err =__ntfs_attr_truncate_vfs (ni, old_data_size, - old_data_size); + data_err =__ntfs_attr_truncate_vfs (ni, new_data_size, + new_data_size);mutex_unlock (&mrec_ni->mrec_lock);/* Like ntfs_setattr_size(): stay on the durable size. */ if (data_err)base-commit:
b025f526c5d04929212c9d5fb7ffc96608f3eacc