Commit 7426cc04 authored by Miao Xie's avatar Miao Xie Committed by Chris Mason

Btrfs: punch hole past the end of the file

Since we can pre-allocate the space past EOF, we should be able to reclaim
that space if we need. This patch implements it by removing the EOF check.

Though the manual of fallocate command says we can use truncate command to
reclaim the pre-allocated space which past EOF, but because truncate command
changes the file size, we must run several commands to reclaim the space if we
don't want to change the file size, so it is not a good choice.
Signed-off-by: default avatarMiao Xie <miaox@cn.fujitsu.com>
Signed-off-by: default avatarChris Mason <chris.mason@fusionio.com>
parent 0061280d
...@@ -1873,26 +1873,28 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) ...@@ -1873,26 +1873,28 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
btrfs_wait_ordered_range(inode, offset, len); btrfs_wait_ordered_range(inode, offset, len);
mutex_lock(&inode->i_mutex); mutex_lock(&inode->i_mutex);
if (offset >= inode->i_size) { /*
mutex_unlock(&inode->i_mutex); * We needn't truncate any page which is beyond the end of the file
return 0; * because we are sure there is no data there.
} */
/* /*
* Only do this if we are in the same page and we aren't doing the * Only do this if we are in the same page and we aren't doing the
* entire page. * entire page.
*/ */
if (same_page && len < PAGE_CACHE_SIZE) { if (same_page && len < PAGE_CACHE_SIZE) {
ret = btrfs_truncate_page(inode, offset, len, 0); if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE))
ret = btrfs_truncate_page(inode, offset, len, 0);
mutex_unlock(&inode->i_mutex); mutex_unlock(&inode->i_mutex);
return ret; return ret;
} }
/* zero back part of the first page */ /* zero back part of the first page */
ret = btrfs_truncate_page(inode, offset, 0, 0); if (offset < round_up(inode->i_size, PAGE_CACHE_SIZE)) {
if (ret) { ret = btrfs_truncate_page(inode, offset, 0, 0);
mutex_unlock(&inode->i_mutex); if (ret) {
return ret; mutex_unlock(&inode->i_mutex);
return ret;
}
} }
/* zero the front end of the last page */ /* zero the front end of the last page */
......
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