Fix data corruption when blocksize < pagesize for mmapped data
Subsystems (5)
Jan Kara
maintainerv2 cover Fix data corruption when blocksize < pagesize for mmapped data
Hello,
this is a second version of the patches to fix data corruption in mmapped
data when blocksize < pagesize as tested by xfstests generic/030 test. The patchset fixes XFS and ext4. I've checked and btrfs doesn't need fixing because it doesn't support blocksize < pagesize. If that's ever going to change btrfs will likely need a similar treatment. ocfs2, ext2, ext3 are OK since they happily allocate blocks during writeback. For other filesystems like gfs2, ubifs, nilfs, ceph,... I'm not sure whether they support blocksize < pagesize at all. Interesting is also NFS which may care but I don't understand its ->page_mkwrite() handler good enough to judge.
Changes since v1:
- changed helper function name and moved it to mm/truncate.c - I originally thought we can make the helper function update i_size to simplify the interface but it's actually impossible due to generic_write_end() lock ordering constraints.
- used round_up() instead of ALIGN()
- taught truncate_setsize() to use the helper function
Honza
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Jan Kara
maintainerPATCH v2 1/2 vfs: Fix data corruption when blocksize < pagesize for mmaped data
->page_mkwrite() is used by filesystems to allocate blocks under a page which is becoming writeably mmapped in some process' address space. This allows a filesystem to return a page fault if there is not enough space available, user exceeds quota or similar problem happens, rather than silently discarding data later when writepage is called.
However VFS fails to call ->page_mkwrite() in all the cases where filesystems need it when blocksize < pagesize. For example when blocksize = 1024, pagesize = 4096 the following is problematic:
ftruncate(fd, 0);
pwrite(fd, buf, 1024, 0);
map = mmap(NULL, 1024, PROT_WRITE, MAP_SHARED, fd, 0);
map[0] = 'a'; ----> page_mkwrite() for index 0 is called
ftruncate(fd, 10000); /* or even pwrite(fd, buf, 1, 10000) */ mremap(map, 1024, 10000, 0);
map[4095] = 'a'; ----> no page_mkwrite() called
At the moment ->page_mkwrite() is called, filesystem can allocate only one block for the page because i_size == 1024. Otherwise it would create blocks beyond i_size which is generally undesirable. But later at ->writepage() time, we also need to store data at offset 4095 but we don't have block allocated for it.
This patch introduces a helper function filesystems can use to have ->page_mkwrite() called at all the necessary moments.
--- fs/buffer.c | 3 +++ include/linux/mm.h | 8 ++++++++ mm/truncate.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+)
fs/buffer.c +3 -0
@@ -2080,6 +2080,7 @@ int generic_write_end(struct file *file, struct address_space *mapping, struct page *page, void *fsdata) { struct inode *inode = mapping->host; +loff_t old_size = inode->i_size;int i_size_changed =0 ; copied =block_write_end (file, mapping, pos, len, copied, page, fsdata);
@@ -2099,6 +2100,8 @@ int generic_write_end(struct file *file, struct address_space *mapping,unlock_page (page);page_cache_release (page); +if (old_size < pos) +pagecache_isize_extended (inode, old_size, pos);/* * Don't mark the inode dirty under page lock. First, it unnecessarily * makes the holding time of page lock longer. Second, it forces lock
include/linux/mm.h +8 -0
@@ -1155,6 +1155,14 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,extern void truncate_pagecache (struct inode *inode,loff_t new);extern void truncate_setsize (struct inode *inode,loff_t newsize); +#ifdef CONFIG_MMU +void pagecache_isize_extended (struct inode *inode,loff_t from,loff_t to); +#else +static inline void pagecache_isize_extended (struct inode *inode,loff_t from, +loff_t to) +{ +} +#endif void truncate_pagecache_range (struct inode *inode,loff_t offset,loff_t end);int truncate_inode_page (struct address_space *mapping,struct page *page);int generic_error_remove_page (struct address_space *mapping,struct page *page);
mm/truncate.c +57 -0
75 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 1/2] vfs: Fix data corruption when blocksize < pagesize for mmaped data
Quoted 23 lines · Jan Kara
->page_mkwrite() is used by filesystems to allocate blocks under a page which is becoming writeably mmapped in some process' address space. This allows a filesystem to return a page fault if there is not enough space available, user exceeds quota or similar problem happens, rather than silently discarding data later when writepage is called.
However VFS fails to call ->page_mkwrite() in all the cases where filesystems need it when blocksize < pagesize. For example when blocksize = 1024, pagesize = 4096 the following is problematic:
ftruncate(fd, 0);
pwrite(fd, buf, 1024, 0);
map =mmap(NULL, 1024, PROT_WRITE, MAP_SHARED, fd, 0);
map[0] = 'a'; ---->page_mkwrite()for index 0 is called
ftruncate(fd, 10000); /* or evenpwrite(fd, buf, 1, 10000)*/mremap(map, 1024, 10000, 0);
map[4095] = 'a'; ----> nopage_mkwrite()calledAt the moment ->page_mkwrite() is called, filesystem can allocate only one block for the page because
i_size== 1024. Otherwise it would create blocks beyondi_sizewhich is generally undesirable. But later at ->writepage() time, we also need to store data at offset 4095 but we don't have block allocated for it.
Quoted 4 lines
This patch introduces a helper function filesystems can use to have ->page_mkwrite() called at all the necessary moments.
Signed-off-by Jan Kara <[email protected]>
Applied to the ext4 tree, thanks.
- Ted
Jan Kara
maintainerPATCH v2 2/2 ext4: Fix mmap data corruption when blocksize < pagesize
In reply to Jan KaraUse truncate_isize_extended() when hole is being created in a file so that ->page_mkwrite() will get called for the partial tail page if it is mmaped (see the first patch in the series for details).
--- fs/ext4/inode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext4/inode.c +5 -1
@@ -4536,8 +4536,12 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr)ext4_orphan_del (NULL , inode); goto err_out; } - }else + }else { +loff_t oldsize = inode->i_size; +i_size_write (inode, attr->ia_size); +pagecache_isize_extended (inode, oldsize, inode->i_size); + }/* * Blocks are going to be removed from the inode. Wait
Signature · 1 line
1.8.1.4
Re: [PATCH 2/2] ext4: Fix mmap data corruption when blocksize < pagesize
Quoted 5 lines · Jan Kara
Use
truncate_isize_extended()when hole is being created in a file so that ->page_mkwrite() will get called for the partial tail page if it is mmaped (see the first patch in the series for details).Signed-off-by Jan Kara <[email protected]>
Applied to the ext4 tree, thanks.
- Ted
_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs