aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/file.c')
-rw-r--r--fs/btrfs/file.c75
1 files changed, 61 insertions, 14 deletions
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index aeb84469d2c4..ade03e6f7bd2 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -30,11 +30,11 @@
30#include <linux/statfs.h> 30#include <linux/statfs.h>
31#include <linux/compat.h> 31#include <linux/compat.h>
32#include <linux/slab.h> 32#include <linux/slab.h>
33#include <linux/btrfs.h>
33#include "ctree.h" 34#include "ctree.h"
34#include "disk-io.h" 35#include "disk-io.h"
35#include "transaction.h" 36#include "transaction.h"
36#include "btrfs_inode.h" 37#include "btrfs_inode.h"
37#include "ioctl.h"
38#include "print-tree.h" 38#include "print-tree.h"
39#include "tree-log.h" 39#include "tree-log.h"
40#include "locking.h" 40#include "locking.h"
@@ -374,6 +374,11 @@ int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
374 374
375 atomic_inc(&fs_info->defrag_running); 375 atomic_inc(&fs_info->defrag_running);
376 while(1) { 376 while(1) {
377 /* Pause the auto defragger. */
378 if (test_bit(BTRFS_FS_STATE_REMOUNTING,
379 &fs_info->fs_state))
380 break;
381
377 if (!__need_auto_defrag(fs_info->tree_root)) 382 if (!__need_auto_defrag(fs_info->tree_root))
378 break; 383 break;
379 384
@@ -505,8 +510,7 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
505 loff_t isize = i_size_read(inode); 510 loff_t isize = i_size_read(inode);
506 511
507 start_pos = pos & ~((u64)root->sectorsize - 1); 512 start_pos = pos & ~((u64)root->sectorsize - 1);
508 num_bytes = (write_bytes + pos - start_pos + 513 num_bytes = ALIGN(write_bytes + pos - start_pos, root->sectorsize);
509 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
510 514
511 end_of_last_block = start_pos + num_bytes - 1; 515 end_of_last_block = start_pos + num_bytes - 1;
512 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 516 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
@@ -587,6 +591,7 @@ void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
587 } 591 }
588 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 592 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
589 clear_bit(EXTENT_FLAG_PINNED, &em->flags); 593 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
594 clear_bit(EXTENT_FLAG_LOGGING, &flags);
590 remove_extent_mapping(em_tree, em); 595 remove_extent_mapping(em_tree, em);
591 if (no_splits) 596 if (no_splits)
592 goto next; 597 goto next;
@@ -1225,7 +1230,7 @@ static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
1225 struct extent_state *cached_state = NULL; 1230 struct extent_state *cached_state = NULL;
1226 int i; 1231 int i;
1227 unsigned long index = pos >> PAGE_CACHE_SHIFT; 1232 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1228 struct inode *inode = fdentry(file)->d_inode; 1233 struct inode *inode = file_inode(file);
1229 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); 1234 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
1230 int err = 0; 1235 int err = 0;
1231 int faili = 0; 1236 int faili = 0;
@@ -1312,7 +1317,7 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1312 struct iov_iter *i, 1317 struct iov_iter *i,
1313 loff_t pos) 1318 loff_t pos)
1314{ 1319{
1315 struct inode *inode = fdentry(file)->d_inode; 1320 struct inode *inode = file_inode(file);
1316 struct btrfs_root *root = BTRFS_I(inode)->root; 1321 struct btrfs_root *root = BTRFS_I(inode)->root;
1317 struct page **pages = NULL; 1322 struct page **pages = NULL;
1318 unsigned long first_index; 1323 unsigned long first_index;
@@ -1500,7 +1505,7 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1500 unsigned long nr_segs, loff_t pos) 1505 unsigned long nr_segs, loff_t pos)
1501{ 1506{
1502 struct file *file = iocb->ki_filp; 1507 struct file *file = iocb->ki_filp;
1503 struct inode *inode = fdentry(file)->d_inode; 1508 struct inode *inode = file_inode(file);
1504 struct btrfs_root *root = BTRFS_I(inode)->root; 1509 struct btrfs_root *root = BTRFS_I(inode)->root;
1505 loff_t *ppos = &iocb->ki_pos; 1510 loff_t *ppos = &iocb->ki_pos;
1506 u64 start_pos; 1511 u64 start_pos;
@@ -1544,7 +1549,7 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1544 * although we have opened a file as writable, we have 1549 * although we have opened a file as writable, we have
1545 * to stop this write operation to ensure FS consistency. 1550 * to stop this write operation to ensure FS consistency.
1546 */ 1551 */
1547 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 1552 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) {
1548 mutex_unlock(&inode->i_mutex); 1553 mutex_unlock(&inode->i_mutex);
1549 err = -EROFS; 1554 err = -EROFS;
1550 goto out; 1555 goto out;
@@ -1627,7 +1632,20 @@ int btrfs_release_file(struct inode *inode, struct file *filp)
1627 */ 1632 */
1628 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE, 1633 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1629 &BTRFS_I(inode)->runtime_flags)) { 1634 &BTRFS_I(inode)->runtime_flags)) {
1630 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode); 1635 struct btrfs_trans_handle *trans;
1636 struct btrfs_root *root = BTRFS_I(inode)->root;
1637
1638 /*
1639 * We need to block on a committing transaction to keep us from
1640 * throwing a ordered operation on to the list and causing
1641 * something like sync to deadlock trying to flush out this
1642 * inode.
1643 */
1644 trans = btrfs_start_transaction(root, 0);
1645 if (IS_ERR(trans))
1646 return PTR_ERR(trans);
1647 btrfs_add_ordered_operation(trans, BTRFS_I(inode)->root, inode);
1648 btrfs_end_transaction(trans, root);
1631 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) 1649 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1632 filemap_flush(inode->i_mapping); 1650 filemap_flush(inode->i_mapping);
1633 } 1651 }
@@ -1654,16 +1672,21 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1654 struct btrfs_root *root = BTRFS_I(inode)->root; 1672 struct btrfs_root *root = BTRFS_I(inode)->root;
1655 int ret = 0; 1673 int ret = 0;
1656 struct btrfs_trans_handle *trans; 1674 struct btrfs_trans_handle *trans;
1675 bool full_sync = 0;
1657 1676
1658 trace_btrfs_sync_file(file, datasync); 1677 trace_btrfs_sync_file(file, datasync);
1659 1678
1660 /* 1679 /*
1661 * We write the dirty pages in the range and wait until they complete 1680 * We write the dirty pages in the range and wait until they complete
1662 * out of the ->i_mutex. If so, we can flush the dirty pages by 1681 * out of the ->i_mutex. If so, we can flush the dirty pages by
1663 * multi-task, and make the performance up. 1682 * multi-task, and make the performance up. See
1683 * btrfs_wait_ordered_range for an explanation of the ASYNC check.
1664 */ 1684 */
1665 atomic_inc(&BTRFS_I(inode)->sync_writers); 1685 atomic_inc(&BTRFS_I(inode)->sync_writers);
1666 ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 1686 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1687 if (!ret && test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1688 &BTRFS_I(inode)->runtime_flags))
1689 ret = filemap_fdatawrite_range(inode->i_mapping, start, end);
1667 atomic_dec(&BTRFS_I(inode)->sync_writers); 1690 atomic_dec(&BTRFS_I(inode)->sync_writers);
1668 if (ret) 1691 if (ret)
1669 return ret; 1692 return ret;
@@ -1675,7 +1698,10 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1675 * range being left. 1698 * range being left.
1676 */ 1699 */
1677 atomic_inc(&root->log_batch); 1700 atomic_inc(&root->log_batch);
1678 btrfs_wait_ordered_range(inode, start, end - start + 1); 1701 full_sync = test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1702 &BTRFS_I(inode)->runtime_flags);
1703 if (full_sync)
1704 btrfs_wait_ordered_range(inode, start, end - start + 1);
1679 atomic_inc(&root->log_batch); 1705 atomic_inc(&root->log_batch);
1680 1706
1681 /* 1707 /*
@@ -1742,13 +1768,25 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1742 1768
1743 if (ret != BTRFS_NO_LOG_SYNC) { 1769 if (ret != BTRFS_NO_LOG_SYNC) {
1744 if (ret > 0) { 1770 if (ret > 0) {
1771 /*
1772 * If we didn't already wait for ordered extents we need
1773 * to do that now.
1774 */
1775 if (!full_sync)
1776 btrfs_wait_ordered_range(inode, start,
1777 end - start + 1);
1745 ret = btrfs_commit_transaction(trans, root); 1778 ret = btrfs_commit_transaction(trans, root);
1746 } else { 1779 } else {
1747 ret = btrfs_sync_log(trans, root); 1780 ret = btrfs_sync_log(trans, root);
1748 if (ret == 0) 1781 if (ret == 0) {
1749 ret = btrfs_end_transaction(trans, root); 1782 ret = btrfs_end_transaction(trans, root);
1750 else 1783 } else {
1784 if (!full_sync)
1785 btrfs_wait_ordered_range(inode, start,
1786 end -
1787 start + 1);
1751 ret = btrfs_commit_transaction(trans, root); 1788 ret = btrfs_commit_transaction(trans, root);
1789 }
1752 } 1790 }
1753 } else { 1791 } else {
1754 ret = btrfs_end_transaction(trans, root); 1792 ret = btrfs_end_transaction(trans, root);
@@ -2102,8 +2140,9 @@ out:
2102static long btrfs_fallocate(struct file *file, int mode, 2140static long btrfs_fallocate(struct file *file, int mode,
2103 loff_t offset, loff_t len) 2141 loff_t offset, loff_t len)
2104{ 2142{
2105 struct inode *inode = file->f_path.dentry->d_inode; 2143 struct inode *inode = file_inode(file);
2106 struct extent_state *cached_state = NULL; 2144 struct extent_state *cached_state = NULL;
2145 struct btrfs_root *root = BTRFS_I(inode)->root;
2107 u64 cur_offset; 2146 u64 cur_offset;
2108 u64 last_byte; 2147 u64 last_byte;
2109 u64 alloc_start; 2148 u64 alloc_start;
@@ -2131,6 +2170,11 @@ static long btrfs_fallocate(struct file *file, int mode,
2131 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start); 2170 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
2132 if (ret) 2171 if (ret)
2133 return ret; 2172 return ret;
2173 if (root->fs_info->quota_enabled) {
2174 ret = btrfs_qgroup_reserve(root, alloc_end - alloc_start);
2175 if (ret)
2176 goto out_reserve_fail;
2177 }
2134 2178
2135 /* 2179 /*
2136 * wait for ordered IO before we have any locks. We'll loop again 2180 * wait for ordered IO before we have any locks. We'll loop again
@@ -2234,6 +2278,9 @@ static long btrfs_fallocate(struct file *file, int mode,
2234 &cached_state, GFP_NOFS); 2278 &cached_state, GFP_NOFS);
2235out: 2279out:
2236 mutex_unlock(&inode->i_mutex); 2280 mutex_unlock(&inode->i_mutex);
2281 if (root->fs_info->quota_enabled)
2282 btrfs_qgroup_free(root, alloc_end - alloc_start);
2283out_reserve_fail:
2237 /* Let go of our reservation. */ 2284 /* Let go of our reservation. */
2238 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start); 2285 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
2239 return ret; 2286 return ret;