Commit a71754fc authored by Josef Bacik's avatar Josef Bacik

Btrfs: move btrfs_truncate_page to btrfs_cont_expand instead of btrfs_truncate

This has plagued us forever and I'm so over working around it.  When we truncate
down to a non-page aligned offset we will call btrfs_truncate_page to zero out
the end of the page and write it back to disk, this will keep us from exposing
stale data if we truncate back up from that point.  The problem with this is it
requires data space to do this, and people don't really expect to get ENOSPC
from truncate() for these sort of things.  This also tends to bite the orphan
cleanup stuff too which keeps people from mounting.  To get around this we can
just move this into btrfs_cont_expand() to make sure if we are truncating up
from a non-page size aligned i_size we will zero out the rest of this page so
that we don't expose stale data.  This will give ENOSPC if you try to truncate()
up or if you try to write past the end of isize, which is much more reasonable.
This fixes xfstests generic/083 failing to mount because of the orphan cleanup
failing.  Thanks,
Signed-off-by: default avatarJosef Bacik <jbacik@fusionio.com>
parent 0b08851f
...@@ -2477,11 +2477,12 @@ static void end_bio_extent_readpage(struct bio *bio, int err) ...@@ -2477,11 +2477,12 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
struct extent_state *cached = NULL; struct extent_state *cached = NULL;
struct extent_state *state; struct extent_state *state;
struct btrfs_io_bio *io_bio = btrfs_io_bio(bio); struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
struct inode *inode = page->mapping->host;
pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, " pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
"mirror=%lu\n", (u64)bio->bi_sector, err, "mirror=%lu\n", (u64)bio->bi_sector, err,
io_bio->mirror_num); io_bio->mirror_num);
tree = &BTRFS_I(page->mapping->host)->io_tree; tree = &BTRFS_I(inode)->io_tree;
/* We always issue full-page reads, but if some block /* We always issue full-page reads, but if some block
* in a page fails to read, blk_update_request() will * in a page fails to read, blk_update_request() will
...@@ -2555,6 +2556,14 @@ static void end_bio_extent_readpage(struct bio *bio, int err) ...@@ -2555,6 +2556,14 @@ static void end_bio_extent_readpage(struct bio *bio, int err)
unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC); unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
if (uptodate) { if (uptodate) {
loff_t i_size = i_size_read(inode);
pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
unsigned offset;
/* Zero out the end if this page straddles i_size */
offset = i_size & (PAGE_CACHE_SIZE-1);
if (page->index == end_index && offset)
zero_user_segment(page, offset, PAGE_CACHE_SIZE);
SetPageUptodate(page); SetPageUptodate(page);
} else { } else {
ClearPageUptodate(page); ClearPageUptodate(page);
......
...@@ -2173,12 +2173,6 @@ static long btrfs_fallocate(struct file *file, int mode, ...@@ -2173,12 +2173,6 @@ static long btrfs_fallocate(struct file *file, int mode,
goto out_reserve_fail; goto out_reserve_fail;
} }
/*
* wait for ordered IO before we have any locks. We'll loop again
* below with the locks held.
*/
btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
mutex_lock(&inode->i_mutex); mutex_lock(&inode->i_mutex);
ret = inode_newsize_ok(inode, alloc_end); ret = inode_newsize_ok(inode, alloc_end);
if (ret) if (ret)
...@@ -2189,8 +2183,23 @@ static long btrfs_fallocate(struct file *file, int mode, ...@@ -2189,8 +2183,23 @@ static long btrfs_fallocate(struct file *file, int mode,
alloc_start); alloc_start);
if (ret) if (ret)
goto out; goto out;
} else {
/*
* If we are fallocating from the end of the file onward we
* need to zero out the end of the page if i_size lands in the
* middle of a page.
*/
ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
if (ret)
goto out;
} }
/*
* wait for ordered IO before we have any locks. We'll loop again
* below with the locks held.
*/
btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
locked_end = alloc_end - 1; locked_end = alloc_end - 1;
while (1) { while (1) {
struct btrfs_ordered_extent *ordered; struct btrfs_ordered_extent *ordered;
......
...@@ -4253,6 +4253,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) ...@@ -4253,6 +4253,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
u64 hole_size; u64 hole_size;
int err = 0; int err = 0;
/*
* If our size started in the middle of a page we need to zero out the
* rest of the page before we expand the i_size, otherwise we could
* expose stale data.
*/
err = btrfs_truncate_page(inode, oldsize, 0, 0);
if (err)
return err;
if (size <= hole_start) if (size <= hole_start)
return 0; return 0;
...@@ -7565,16 +7574,12 @@ static int btrfs_truncate(struct inode *inode) ...@@ -7565,16 +7574,12 @@ static int btrfs_truncate(struct inode *inode)
{ {
struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_block_rsv *rsv; struct btrfs_block_rsv *rsv;
int ret; int ret = 0;
int err = 0; int err = 0;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
u64 mask = root->sectorsize - 1; u64 mask = root->sectorsize - 1;
u64 min_size = btrfs_calc_trunc_metadata_size(root, 1); u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
if (ret)
return ret;
btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
btrfs_ordered_update_i_size(inode, inode->i_size, NULL); btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment