Skip to content

Commit 3976690

Browse files
jankaraJiri Slaby
authored andcommitted
vfs: fix data corruption when blocksize < pagesize for mmaped data
commit 90a8020 upstream. ->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. Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 4f111bb commit 3976690

3 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎fs/buffer.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,7 @@ int generic_write_end(struct file *file, struct address_space *mapping,
20892089
struct page *page, void *fsdata)
20902090
{
20912091
struct inode *inode = mapping->host;
2092+
loff_t old_size = inode->i_size;
20922093
int i_size_changed = 0;
20932094

20942095
copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
@@ -2108,6 +2109,8 @@ int generic_write_end(struct file *file, struct address_space *mapping,
21082109
unlock_page(page);
21092110
page_cache_release(page);
21102111

2112+
if (old_size < pos)
2113+
pagecache_isize_extended(inode, old_size, pos);
21112114
/*
21122115
* Don't mark the inode dirty under page lock. First, it unnecessarily
21132116
* makes the holding time of page lock longer. Second, it forces lock

‎include/linux/mm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
10091009

10101010
extern void truncate_pagecache(struct inode *inode, loff_t new);
10111011
extern void truncate_setsize(struct inode *inode, loff_t newsize);
1012+
void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
10121013
void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
10131014
int truncate_inode_page(struct address_space *mapping, struct page *page);
10141015
int generic_error_remove_page(struct address_space *mapping, struct page *page);

‎mm/truncate.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/buffer_head.h> /* grr. try_to_release_page,
2121
do_invalidatepage */
2222
#include <linux/cleancache.h>
23+
#include <linux/rmap.h>
2324
#include "internal.h"
2425

2526
static void clear_exceptional_entry(struct address_space *mapping,
@@ -661,11 +662,67 @@ EXPORT_SYMBOL(truncate_pagecache);
661662
*/
662663
void truncate_setsize(struct inode *inode, loff_t newsize)
663664
{
665+
loff_t oldsize = inode->i_size;
666+
664667
i_size_write(inode, newsize);
668+
if (newsize > oldsize)
669+
pagecache_isize_extended(inode, oldsize, newsize);
665670
truncate_pagecache(inode, newsize);
666671
}
667672
EXPORT_SYMBOL(truncate_setsize);
668673

674+
/**
675+
* pagecache_isize_extended - update pagecache after extension of i_size
676+
* @inode: inode for which i_size was extended
677+
* @from: original inode size
678+
* @to: new inode size
679+
*
680+
* Handle extension of inode size either caused by extending truncate or by
681+
* write starting after current i_size. We mark the page straddling current
682+
* i_size RO so that page_mkwrite() is called on the nearest write access to
683+
* the page. This way filesystem can be sure that page_mkwrite() is called on
684+
* the page before user writes to the page via mmap after the i_size has been
685+
* changed.
686+
*
687+
* The function must be called after i_size is updated so that page fault
688+
* coming after we unlock the page will already see the new i_size.
689+
* The function must be called while we still hold i_mutex - this not only
690+
* makes sure i_size is stable but also that userspace cannot observe new
691+
* i_size value before we are prepared to store mmap writes at new inode size.
692+
*/
693+
void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
694+
{
695+
int bsize = 1 << inode->i_blkbits;
696+
loff_t rounded_from;
697+
struct page *page;
698+
pgoff_t index;
699+
700+
WARN_ON(!mutex_is_locked(&inode->i_mutex));
701+
WARN_ON(to > inode->i_size);
702+
703+
if (from >= to || bsize == PAGE_CACHE_SIZE)
704+
return;
705+
/* Page straddling @from will not have any hole block created? */
706+
rounded_from = round_up(from, bsize);
707+
if (to <= rounded_from || !(rounded_from & (PAGE_CACHE_SIZE - 1)))
708+
return;
709+
710+
index = from >> PAGE_CACHE_SHIFT;
711+
page = find_lock_page(inode->i_mapping, index);
712+
/* Page not cached? Nothing to do */
713+
if (!page)
714+
return;
715+
/*
716+
* See clear_page_dirty_for_io() for details why set_page_dirty()
717+
* is needed.
718+
*/
719+
if (page_mkclean(page))
720+
set_page_dirty(page);
721+
unlock_page(page);
722+
page_cache_release(page);
723+
}
724+
EXPORT_SYMBOL(pagecache_isize_extended);
725+
669726
/**
670727
* truncate_pagecache_range - unmap and remove pagecache that is hole-punched
671728
* @inode: inode

0 commit comments

Comments
 (0)