aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c156
1 files changed, 117 insertions, 39 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 54bcf639d1cf..a85c23dfcddb 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -1530,10 +1530,45 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1530static void btrfs_split_extent_hook(struct inode *inode, 1530static void btrfs_split_extent_hook(struct inode *inode,
1531 struct extent_state *orig, u64 split) 1531 struct extent_state *orig, u64 split)
1532{ 1532{
1533 u64 size;
1534
1533 /* not delalloc, ignore it */ 1535 /* not delalloc, ignore it */
1534 if (!(orig->state & EXTENT_DELALLOC)) 1536 if (!(orig->state & EXTENT_DELALLOC))
1535 return; 1537 return;
1536 1538
1539 size = orig->end - orig->start + 1;
1540 if (size > BTRFS_MAX_EXTENT_SIZE) {
1541 u64 num_extents;
1542 u64 new_size;
1543
1544 /*
1545 * We need the largest size of the remaining extent to see if we
1546 * need to add a new outstanding extent. Think of the following
1547 * case
1548 *
1549 * [MEAX_EXTENT_SIZEx2 - 4k][4k]
1550 *
1551 * The new_size would just be 4k and we'd think we had enough
1552 * outstanding extents for this if we only took one side of the
1553 * split, same goes for the other direction. We need to see if
1554 * the larger size still is the same amount of extents as the
1555 * original size, because if it is we need to add a new
1556 * outstanding extent. But if we split up and the larger size
1557 * is less than the original then we are good to go since we've
1558 * already accounted for the extra extent in our original
1559 * accounting.
1560 */
1561 new_size = orig->end - split + 1;
1562 if ((split - orig->start) > new_size)
1563 new_size = split - orig->start;
1564
1565 num_extents = div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1566 BTRFS_MAX_EXTENT_SIZE);
1567 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1568 BTRFS_MAX_EXTENT_SIZE) < num_extents)
1569 return;
1570 }
1571
1537 spin_lock(&BTRFS_I(inode)->lock); 1572 spin_lock(&BTRFS_I(inode)->lock);
1538 BTRFS_I(inode)->outstanding_extents++; 1573 BTRFS_I(inode)->outstanding_extents++;
1539 spin_unlock(&BTRFS_I(inode)->lock); 1574 spin_unlock(&BTRFS_I(inode)->lock);
@@ -1549,10 +1584,34 @@ static void btrfs_merge_extent_hook(struct inode *inode,
1549 struct extent_state *new, 1584 struct extent_state *new,
1550 struct extent_state *other) 1585 struct extent_state *other)
1551{ 1586{
1587 u64 new_size, old_size;
1588 u64 num_extents;
1589
1552 /* not delalloc, ignore it */ 1590 /* not delalloc, ignore it */
1553 if (!(other->state & EXTENT_DELALLOC)) 1591 if (!(other->state & EXTENT_DELALLOC))
1554 return; 1592 return;
1555 1593
1594 old_size = other->end - other->start + 1;
1595 new_size = old_size + (new->end - new->start + 1);
1596
1597 /* we're not bigger than the max, unreserve the space and go */
1598 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1599 spin_lock(&BTRFS_I(inode)->lock);
1600 BTRFS_I(inode)->outstanding_extents--;
1601 spin_unlock(&BTRFS_I(inode)->lock);
1602 return;
1603 }
1604
1605 /*
1606 * If we grew by another max_extent, just return, we want to keep that
1607 * reserved amount.
1608 */
1609 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1610 BTRFS_MAX_EXTENT_SIZE);
1611 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1612 BTRFS_MAX_EXTENT_SIZE) > num_extents)
1613 return;
1614
1556 spin_lock(&BTRFS_I(inode)->lock); 1615 spin_lock(&BTRFS_I(inode)->lock);
1557 BTRFS_I(inode)->outstanding_extents--; 1616 BTRFS_I(inode)->outstanding_extents--;
1558 spin_unlock(&BTRFS_I(inode)->lock); 1617 spin_unlock(&BTRFS_I(inode)->lock);
@@ -1604,7 +1663,7 @@ static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1604 * have pending delalloc work to be done. 1663 * have pending delalloc work to be done.
1605 */ 1664 */
1606static void btrfs_set_bit_hook(struct inode *inode, 1665static void btrfs_set_bit_hook(struct inode *inode,
1607 struct extent_state *state, unsigned long *bits) 1666 struct extent_state *state, unsigned *bits)
1608{ 1667{
1609 1668
1610 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) 1669 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
@@ -1645,9 +1704,11 @@ static void btrfs_set_bit_hook(struct inode *inode,
1645 */ 1704 */
1646static void btrfs_clear_bit_hook(struct inode *inode, 1705static void btrfs_clear_bit_hook(struct inode *inode,
1647 struct extent_state *state, 1706 struct extent_state *state,
1648 unsigned long *bits) 1707 unsigned *bits)
1649{ 1708{
1650 u64 len = state->end + 1 - state->start; 1709 u64 len = state->end + 1 - state->start;
1710 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1711 BTRFS_MAX_EXTENT_SIZE);
1651 1712
1652 spin_lock(&BTRFS_I(inode)->lock); 1713 spin_lock(&BTRFS_I(inode)->lock);
1653 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) 1714 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
@@ -1667,7 +1728,7 @@ static void btrfs_clear_bit_hook(struct inode *inode,
1667 *bits &= ~EXTENT_FIRST_DELALLOC; 1728 *bits &= ~EXTENT_FIRST_DELALLOC;
1668 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) { 1729 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1669 spin_lock(&BTRFS_I(inode)->lock); 1730 spin_lock(&BTRFS_I(inode)->lock);
1670 BTRFS_I(inode)->outstanding_extents--; 1731 BTRFS_I(inode)->outstanding_extents -= num_extents;
1671 spin_unlock(&BTRFS_I(inode)->lock); 1732 spin_unlock(&BTRFS_I(inode)->lock);
1672 } 1733 }
1673 1734
@@ -2945,7 +3006,7 @@ static int __readpage_endio_check(struct inode *inode,
2945 return 0; 3006 return 0;
2946zeroit: 3007zeroit:
2947 if (__ratelimit(&_rs)) 3008 if (__ratelimit(&_rs))
2948 btrfs_info(BTRFS_I(inode)->root->fs_info, 3009 btrfs_warn(BTRFS_I(inode)->root->fs_info,
2949 "csum failed ino %llu off %llu csum %u expected csum %u", 3010 "csum failed ino %llu off %llu csum %u expected csum %u",
2950 btrfs_ino(inode), start, csum, csum_expected); 3011 btrfs_ino(inode), start, csum, csum_expected);
2951 memset(kaddr + pgoff, 1, len); 3012 memset(kaddr + pgoff, 1, len);
@@ -3407,7 +3468,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3407 3468
3408out: 3469out:
3409 if (ret) 3470 if (ret)
3410 btrfs_crit(root->fs_info, 3471 btrfs_err(root->fs_info,
3411 "could not do orphan cleanup %d", ret); 3472 "could not do orphan cleanup %d", ret);
3412 btrfs_free_path(path); 3473 btrfs_free_path(path);
3413 return ret; 3474 return ret;
@@ -3490,7 +3551,6 @@ static void btrfs_read_locked_inode(struct inode *inode)
3490 struct btrfs_path *path; 3551 struct btrfs_path *path;
3491 struct extent_buffer *leaf; 3552 struct extent_buffer *leaf;
3492 struct btrfs_inode_item *inode_item; 3553 struct btrfs_inode_item *inode_item;
3493 struct btrfs_timespec *tspec;
3494 struct btrfs_root *root = BTRFS_I(inode)->root; 3554 struct btrfs_root *root = BTRFS_I(inode)->root;
3495 struct btrfs_key location; 3555 struct btrfs_key location;
3496 unsigned long ptr; 3556 unsigned long ptr;
@@ -3527,17 +3587,19 @@ static void btrfs_read_locked_inode(struct inode *inode)
3527 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item)); 3587 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3528 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); 3588 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3529 3589
3530 tspec = btrfs_inode_atime(inode_item); 3590 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3531 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); 3591 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3532 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); 3592
3593 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3594 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3533 3595
3534 tspec = btrfs_inode_mtime(inode_item); 3596 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3535 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); 3597 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3536 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3537 3598
3538 tspec = btrfs_inode_ctime(inode_item); 3599 BTRFS_I(inode)->i_otime.tv_sec =
3539 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); 3600 btrfs_timespec_sec(leaf, &inode_item->otime);
3540 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); 3601 BTRFS_I(inode)->i_otime.tv_nsec =
3602 btrfs_timespec_nsec(leaf, &inode_item->otime);
3541 3603
3542 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item)); 3604 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3543 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item); 3605 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
@@ -3656,21 +3718,26 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
3656 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token); 3718 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3657 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token); 3719 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3658 3720
3659 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item), 3721 btrfs_set_token_timespec_sec(leaf, &item->atime,
3660 inode->i_atime.tv_sec, &token); 3722 inode->i_atime.tv_sec, &token);
3661 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item), 3723 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3662 inode->i_atime.tv_nsec, &token); 3724 inode->i_atime.tv_nsec, &token);
3663 3725
3664 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item), 3726 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3665 inode->i_mtime.tv_sec, &token); 3727 inode->i_mtime.tv_sec, &token);
3666 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item), 3728 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3667 inode->i_mtime.tv_nsec, &token); 3729 inode->i_mtime.tv_nsec, &token);
3668 3730
3669 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item), 3731 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3670 inode->i_ctime.tv_sec, &token); 3732 inode->i_ctime.tv_sec, &token);
3671 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item), 3733 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3672 inode->i_ctime.tv_nsec, &token); 3734 inode->i_ctime.tv_nsec, &token);
3673 3735
3736 btrfs_set_token_timespec_sec(leaf, &item->otime,
3737 BTRFS_I(inode)->i_otime.tv_sec, &token);
3738 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3739 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3740
3674 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode), 3741 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3675 &token); 3742 &token);
3676 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation, 3743 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
@@ -5007,6 +5074,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
5007 struct btrfs_root *new_root; 5074 struct btrfs_root *new_root;
5008 struct btrfs_root_ref *ref; 5075 struct btrfs_root_ref *ref;
5009 struct extent_buffer *leaf; 5076 struct extent_buffer *leaf;
5077 struct btrfs_key key;
5010 int ret; 5078 int ret;
5011 int err = 0; 5079 int err = 0;
5012 5080
@@ -5017,9 +5085,12 @@ static int fixup_tree_root_location(struct btrfs_root *root,
5017 } 5085 }
5018 5086
5019 err = -ENOENT; 5087 err = -ENOENT;
5020 ret = btrfs_find_item(root->fs_info->tree_root, path, 5088 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5021 BTRFS_I(dir)->root->root_key.objectid, 5089 key.type = BTRFS_ROOT_REF_KEY;
5022 location->objectid, BTRFS_ROOT_REF_KEY, NULL); 5090 key.offset = location->objectid;
5091
5092 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5093 0, 0);
5023 if (ret) { 5094 if (ret) {
5024 if (ret < 0) 5095 if (ret < 0)
5025 err = ret; 5096 err = ret;
@@ -5258,7 +5329,10 @@ static struct inode *new_simple_dir(struct super_block *s,
5258 inode->i_op = &btrfs_dir_ro_inode_operations; 5329 inode->i_op = &btrfs_dir_ro_inode_operations;
5259 inode->i_fop = &simple_dir_operations; 5330 inode->i_fop = &simple_dir_operations;
5260 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO; 5331 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5261 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 5332 inode->i_mtime = CURRENT_TIME;
5333 inode->i_atime = inode->i_mtime;
5334 inode->i_ctime = inode->i_mtime;
5335 BTRFS_I(inode)->i_otime = inode->i_mtime;
5262 5336
5263 return inode; 5337 return inode;
5264} 5338}
@@ -5826,7 +5900,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5826 5900
5827 inode_init_owner(inode, dir, mode); 5901 inode_init_owner(inode, dir, mode);
5828 inode_set_bytes(inode, 0); 5902 inode_set_bytes(inode, 0);
5829 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 5903
5904 inode->i_mtime = CURRENT_TIME;
5905 inode->i_atime = inode->i_mtime;
5906 inode->i_ctime = inode->i_mtime;
5907 BTRFS_I(inode)->i_otime = inode->i_mtime;
5908
5830 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 5909 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5831 struct btrfs_inode_item); 5910 struct btrfs_inode_item);
5832 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item, 5911 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
@@ -7134,11 +7213,12 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7134 u64 start = iblock << inode->i_blkbits; 7213 u64 start = iblock << inode->i_blkbits;
7135 u64 lockstart, lockend; 7214 u64 lockstart, lockend;
7136 u64 len = bh_result->b_size; 7215 u64 len = bh_result->b_size;
7216 u64 orig_len = len;
7137 int unlock_bits = EXTENT_LOCKED; 7217 int unlock_bits = EXTENT_LOCKED;
7138 int ret = 0; 7218 int ret = 0;
7139 7219
7140 if (create) 7220 if (create)
7141 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY; 7221 unlock_bits |= EXTENT_DIRTY;
7142 else 7222 else
7143 len = min_t(u64, len, root->sectorsize); 7223 len = min_t(u64, len, root->sectorsize);
7144 7224
@@ -7269,14 +7349,12 @@ unlock:
7269 if (start + len > i_size_read(inode)) 7349 if (start + len > i_size_read(inode))
7270 i_size_write(inode, start + len); 7350 i_size_write(inode, start + len);
7271 7351
7272 spin_lock(&BTRFS_I(inode)->lock); 7352 if (len < orig_len) {
7273 BTRFS_I(inode)->outstanding_extents++; 7353 spin_lock(&BTRFS_I(inode)->lock);
7274 spin_unlock(&BTRFS_I(inode)->lock); 7354 BTRFS_I(inode)->outstanding_extents++;
7275 7355 spin_unlock(&BTRFS_I(inode)->lock);
7276 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, 7356 }
7277 lockstart + len - 1, EXTENT_DELALLOC, NULL, 7357 btrfs_free_reserved_data_space(inode, len);
7278 &cached_state, GFP_NOFS);
7279 BUG_ON(ret);
7280 } 7358 }
7281 7359
7282 /* 7360 /*
@@ -7805,8 +7883,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7805 } 7883 }
7806 7884
7807 /* async crcs make it difficult to collect full stripe writes. */ 7885 /* async crcs make it difficult to collect full stripe writes. */
7808 if (btrfs_get_alloc_profile(root, 1) & 7886 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
7809 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7810 async_submit = 0; 7887 async_submit = 0;
7811 else 7888 else
7812 async_submit = 1; 7889 async_submit = 1;
@@ -8053,8 +8130,6 @@ static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
8053 else if (ret >= 0 && (size_t)ret < count) 8130 else if (ret >= 0 && (size_t)ret < count)
8054 btrfs_delalloc_release_space(inode, 8131 btrfs_delalloc_release_space(inode,
8055 count - (size_t)ret); 8132 count - (size_t)ret);
8056 else
8057 btrfs_delalloc_release_metadata(inode, 0);
8058 } 8133 }
8059out: 8134out:
8060 if (wakeup) 8135 if (wakeup)
@@ -8575,6 +8650,9 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
8575 8650
8576 ei->delayed_node = NULL; 8651 ei->delayed_node = NULL;
8577 8652
8653 ei->i_otime.tv_sec = 0;
8654 ei->i_otime.tv_nsec = 0;
8655
8578 inode = &ei->vfs_inode; 8656 inode = &ei->vfs_inode;
8579 extent_map_tree_init(&ei->extent_tree); 8657 extent_map_tree_init(&ei->extent_tree);
8580 extent_io_tree_init(&ei->io_tree, &inode->i_data); 8658 extent_io_tree_init(&ei->io_tree, &inode->i_data);