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.c702
1 files changed, 284 insertions, 418 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index dc8fb2b3a145..a83e44bf3206 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -37,6 +37,7 @@
37#include <linux/posix_acl.h> 37#include <linux/posix_acl.h>
38#include <linux/falloc.h> 38#include <linux/falloc.h>
39#include <linux/slab.h> 39#include <linux/slab.h>
40#include <linux/ratelimit.h>
40#include "compat.h" 41#include "compat.h"
41#include "ctree.h" 42#include "ctree.h"
42#include "disk-io.h" 43#include "disk-io.h"
@@ -51,6 +52,7 @@
51#include "compression.h" 52#include "compression.h"
52#include "locking.h" 53#include "locking.h"
53#include "free-space-cache.h" 54#include "free-space-cache.h"
55#include "inode-map.h"
54 56
55struct btrfs_iget_args { 57struct btrfs_iget_args {
56 u64 ino; 58 u64 ino;
@@ -137,7 +139,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
137 139
138 path->leave_spinning = 1; 140 path->leave_spinning = 1;
139 141
140 key.objectid = inode->i_ino; 142 key.objectid = btrfs_ino(inode);
141 key.offset = start; 143 key.offset = start;
142 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
143 datasize = btrfs_file_extent_calc_inline_size(cur_size); 145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
@@ -339,6 +341,10 @@ static noinline int compress_file_range(struct inode *inode,
339 int will_compress; 341 int will_compress;
340 int compress_type = root->fs_info->compress_type; 342 int compress_type = root->fs_info->compress_type;
341 343
344 /* if this is a small write inside eof, kick off a defragbot */
345 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
346 btrfs_add_inode_defrag(NULL, inode);
347
342 actual_end = min_t(u64, isize, end + 1); 348 actual_end = min_t(u64, isize, end + 1);
343again: 349again:
344 will_compress = 0; 350 will_compress = 0;
@@ -648,7 +654,7 @@ retry:
648 async_extent->start + 654 async_extent->start +
649 async_extent->ram_size - 1, 0); 655 async_extent->ram_size - 1, 0);
650 656
651 em = alloc_extent_map(GFP_NOFS); 657 em = alloc_extent_map();
652 BUG_ON(!em); 658 BUG_ON(!em);
653 em->start = async_extent->start; 659 em->start = async_extent->start;
654 em->len = async_extent->ram_size; 660 em->len = async_extent->ram_size;
@@ -744,6 +750,15 @@ static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
744 return alloc_hint; 750 return alloc_hint;
745} 751}
746 752
753static inline bool is_free_space_inode(struct btrfs_root *root,
754 struct inode *inode)
755{
756 if (root == root->fs_info->tree_root ||
757 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
758 return true;
759 return false;
760}
761
747/* 762/*
748 * when extent_io.c finds a delayed allocation range in the file, 763 * when extent_io.c finds a delayed allocation range in the file,
749 * the call backs end up in this code. The basic idea is to 764 * the call backs end up in this code. The basic idea is to
@@ -776,7 +791,7 @@ static noinline int cow_file_range(struct inode *inode,
776 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 791 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
777 int ret = 0; 792 int ret = 0;
778 793
779 BUG_ON(root == root->fs_info->tree_root); 794 BUG_ON(is_free_space_inode(root, inode));
780 trans = btrfs_join_transaction(root); 795 trans = btrfs_join_transaction(root);
781 BUG_ON(IS_ERR(trans)); 796 BUG_ON(IS_ERR(trans));
782 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 797 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
@@ -786,6 +801,10 @@ static noinline int cow_file_range(struct inode *inode,
786 disk_num_bytes = num_bytes; 801 disk_num_bytes = num_bytes;
787 ret = 0; 802 ret = 0;
788 803
804 /* if this is a small write inside eof, kick off defrag */
805 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
806 btrfs_add_inode_defrag(trans, inode);
807
789 if (start == 0) { 808 if (start == 0) {
790 /* lets try to make an inline extent */ 809 /* lets try to make an inline extent */
791 ret = cow_file_range_inline(trans, root, inode, 810 ret = cow_file_range_inline(trans, root, inode,
@@ -824,7 +843,7 @@ static noinline int cow_file_range(struct inode *inode,
824 (u64)-1, &ins, 1); 843 (u64)-1, &ins, 1);
825 BUG_ON(ret); 844 BUG_ON(ret);
826 845
827 em = alloc_extent_map(GFP_NOFS); 846 em = alloc_extent_map();
828 BUG_ON(!em); 847 BUG_ON(!em);
829 em->start = start; 848 em->start = start;
830 em->orig_start = em->start; 849 em->orig_start = em->start;
@@ -1006,7 +1025,7 @@ static noinline int csum_exist_in_range(struct btrfs_root *root,
1006 LIST_HEAD(list); 1025 LIST_HEAD(list);
1007 1026
1008 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, 1027 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1009 bytenr + num_bytes - 1, &list); 1028 bytenr + num_bytes - 1, &list, 0);
1010 if (ret == 0 && list_empty(&list)) 1029 if (ret == 0 && list_empty(&list))
1011 return 0; 1030 return 0;
1012 1031
@@ -1047,30 +1066,33 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1047 int type; 1066 int type;
1048 int nocow; 1067 int nocow;
1049 int check_prev = 1; 1068 int check_prev = 1;
1050 bool nolock = false; 1069 bool nolock;
1070 u64 ino = btrfs_ino(inode);
1051 1071
1052 path = btrfs_alloc_path(); 1072 path = btrfs_alloc_path();
1053 BUG_ON(!path); 1073 BUG_ON(!path);
1054 if (root == root->fs_info->tree_root) { 1074
1055 nolock = true; 1075 nolock = is_free_space_inode(root, inode);
1076
1077 if (nolock)
1056 trans = btrfs_join_transaction_nolock(root); 1078 trans = btrfs_join_transaction_nolock(root);
1057 } else { 1079 else
1058 trans = btrfs_join_transaction(root); 1080 trans = btrfs_join_transaction(root);
1059 } 1081
1060 BUG_ON(IS_ERR(trans)); 1082 BUG_ON(IS_ERR(trans));
1061 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1083 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1062 1084
1063 cow_start = (u64)-1; 1085 cow_start = (u64)-1;
1064 cur_offset = start; 1086 cur_offset = start;
1065 while (1) { 1087 while (1) {
1066 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 1088 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1067 cur_offset, 0); 1089 cur_offset, 0);
1068 BUG_ON(ret < 0); 1090 BUG_ON(ret < 0);
1069 if (ret > 0 && path->slots[0] > 0 && check_prev) { 1091 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1070 leaf = path->nodes[0]; 1092 leaf = path->nodes[0];
1071 btrfs_item_key_to_cpu(leaf, &found_key, 1093 btrfs_item_key_to_cpu(leaf, &found_key,
1072 path->slots[0] - 1); 1094 path->slots[0] - 1);
1073 if (found_key.objectid == inode->i_ino && 1095 if (found_key.objectid == ino &&
1074 found_key.type == BTRFS_EXTENT_DATA_KEY) 1096 found_key.type == BTRFS_EXTENT_DATA_KEY)
1075 path->slots[0]--; 1097 path->slots[0]--;
1076 } 1098 }
@@ -1091,7 +1113,7 @@ next_slot:
1091 num_bytes = 0; 1113 num_bytes = 0;
1092 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1114 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1093 1115
1094 if (found_key.objectid > inode->i_ino || 1116 if (found_key.objectid > ino ||
1095 found_key.type > BTRFS_EXTENT_DATA_KEY || 1117 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1096 found_key.offset > end) 1118 found_key.offset > end)
1097 break; 1119 break;
@@ -1126,7 +1148,7 @@ next_slot:
1126 goto out_check; 1148 goto out_check;
1127 if (btrfs_extent_readonly(root, disk_bytenr)) 1149 if (btrfs_extent_readonly(root, disk_bytenr))
1128 goto out_check; 1150 goto out_check;
1129 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 1151 if (btrfs_cross_ref_exist(trans, root, ino,
1130 found_key.offset - 1152 found_key.offset -
1131 extent_offset, disk_bytenr)) 1153 extent_offset, disk_bytenr))
1132 goto out_check; 1154 goto out_check;
@@ -1163,7 +1185,7 @@ out_check:
1163 goto next_slot; 1185 goto next_slot;
1164 } 1186 }
1165 1187
1166 btrfs_release_path(root, path); 1188 btrfs_release_path(path);
1167 if (cow_start != (u64)-1) { 1189 if (cow_start != (u64)-1) {
1168 ret = cow_file_range(inode, locked_page, cow_start, 1190 ret = cow_file_range(inode, locked_page, cow_start,
1169 found_key.offset - 1, page_started, 1191 found_key.offset - 1, page_started,
@@ -1176,7 +1198,7 @@ out_check:
1176 struct extent_map *em; 1198 struct extent_map *em;
1177 struct extent_map_tree *em_tree; 1199 struct extent_map_tree *em_tree;
1178 em_tree = &BTRFS_I(inode)->extent_tree; 1200 em_tree = &BTRFS_I(inode)->extent_tree;
1179 em = alloc_extent_map(GFP_NOFS); 1201 em = alloc_extent_map();
1180 BUG_ON(!em); 1202 BUG_ON(!em);
1181 em->start = cur_offset; 1203 em->start = cur_offset;
1182 em->orig_start = em->start; 1204 em->orig_start = em->start;
@@ -1221,7 +1243,7 @@ out_check:
1221 if (cur_offset > end) 1243 if (cur_offset > end)
1222 break; 1244 break;
1223 } 1245 }
1224 btrfs_release_path(root, path); 1246 btrfs_release_path(path);
1225 1247
1226 if (cur_offset <= end && cow_start == (u64)-1) 1248 if (cur_offset <= end && cow_start == (u64)-1)
1227 cow_start = cur_offset; 1249 cow_start = cur_offset;
@@ -1309,14 +1331,13 @@ static int btrfs_set_bit_hook(struct inode *inode,
1309 1331
1310 /* 1332 /*
1311 * set_bit and clear bit hooks normally require _irqsave/restore 1333 * set_bit and clear bit hooks normally require _irqsave/restore
1312 * but in this case, we are only testeing for the DELALLOC 1334 * but in this case, we are only testing for the DELALLOC
1313 * bit, which is only set or cleared with irqs on 1335 * bit, which is only set or cleared with irqs on
1314 */ 1336 */
1315 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1337 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1316 struct btrfs_root *root = BTRFS_I(inode)->root; 1338 struct btrfs_root *root = BTRFS_I(inode)->root;
1317 u64 len = state->end + 1 - state->start; 1339 u64 len = state->end + 1 - state->start;
1318 int do_list = (root->root_key.objectid != 1340 bool do_list = !is_free_space_inode(root, inode);
1319 BTRFS_ROOT_TREE_OBJECTID);
1320 1341
1321 if (*bits & EXTENT_FIRST_DELALLOC) 1342 if (*bits & EXTENT_FIRST_DELALLOC)
1322 *bits &= ~EXTENT_FIRST_DELALLOC; 1343 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1343,14 +1364,13 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1343{ 1364{
1344 /* 1365 /*
1345 * set_bit and clear bit hooks normally require _irqsave/restore 1366 * set_bit and clear bit hooks normally require _irqsave/restore
1346 * but in this case, we are only testeing for the DELALLOC 1367 * but in this case, we are only testing for the DELALLOC
1347 * bit, which is only set or cleared with irqs on 1368 * bit, which is only set or cleared with irqs on
1348 */ 1369 */
1349 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1370 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1350 struct btrfs_root *root = BTRFS_I(inode)->root; 1371 struct btrfs_root *root = BTRFS_I(inode)->root;
1351 u64 len = state->end + 1 - state->start; 1372 u64 len = state->end + 1 - state->start;
1352 int do_list = (root->root_key.objectid != 1373 bool do_list = !is_free_space_inode(root, inode);
1353 BTRFS_ROOT_TREE_OBJECTID);
1354 1374
1355 if (*bits & EXTENT_FIRST_DELALLOC) 1375 if (*bits & EXTENT_FIRST_DELALLOC)
1356 *bits &= ~EXTENT_FIRST_DELALLOC; 1376 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1457,7 +1477,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1457 1477
1458 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 1478 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1459 1479
1460 if (root == root->fs_info->tree_root) 1480 if (is_free_space_inode(root, inode))
1461 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); 1481 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1462 else 1482 else
1463 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 1483 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
@@ -1641,7 +1661,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1641 &hint, 0); 1661 &hint, 0);
1642 BUG_ON(ret); 1662 BUG_ON(ret);
1643 1663
1644 ins.objectid = inode->i_ino; 1664 ins.objectid = btrfs_ino(inode);
1645 ins.offset = file_pos; 1665 ins.offset = file_pos;
1646 ins.type = BTRFS_EXTENT_DATA_KEY; 1666 ins.type = BTRFS_EXTENT_DATA_KEY;
1647 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1667 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
@@ -1672,7 +1692,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1672 ins.type = BTRFS_EXTENT_ITEM_KEY; 1692 ins.type = BTRFS_EXTENT_ITEM_KEY;
1673 ret = btrfs_alloc_reserved_file_extent(trans, root, 1693 ret = btrfs_alloc_reserved_file_extent(trans, root,
1674 root->root_key.objectid, 1694 root->root_key.objectid,
1675 inode->i_ino, file_pos, &ins); 1695 btrfs_ino(inode), file_pos, &ins);
1676 BUG_ON(ret); 1696 BUG_ON(ret);
1677 btrfs_free_path(path); 1697 btrfs_free_path(path);
1678 1698
@@ -1698,7 +1718,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1698 struct extent_state *cached_state = NULL; 1718 struct extent_state *cached_state = NULL;
1699 int compress_type = 0; 1719 int compress_type = 0;
1700 int ret; 1720 int ret;
1701 bool nolock = false; 1721 bool nolock;
1702 1722
1703 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, 1723 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1704 end - start + 1); 1724 end - start + 1);
@@ -1706,7 +1726,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1706 return 0; 1726 return 0;
1707 BUG_ON(!ordered_extent); 1727 BUG_ON(!ordered_extent);
1708 1728
1709 nolock = (root == root->fs_info->tree_root); 1729 nolock = is_free_space_inode(root, inode);
1710 1730
1711 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1731 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1712 BUG_ON(!list_empty(&ordered_extent->list)); 1732 BUG_ON(!list_empty(&ordered_extent->list));
@@ -1850,7 +1870,7 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1850 } 1870 }
1851 read_unlock(&em_tree->lock); 1871 read_unlock(&em_tree->lock);
1852 1872
1853 if (!em || IS_ERR(em)) { 1873 if (IS_ERR_OR_NULL(em)) {
1854 kfree(failrec); 1874 kfree(failrec);
1855 return -EIO; 1875 return -EIO;
1856 } 1876 }
@@ -1999,12 +2019,11 @@ good:
1999 return 0; 2019 return 0;
2000 2020
2001zeroit: 2021zeroit:
2002 if (printk_ratelimit()) { 2022 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2003 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " 2023 "private %llu\n",
2004 "private %llu\n", page->mapping->host->i_ino, 2024 (unsigned long long)btrfs_ino(page->mapping->host),
2005 (unsigned long long)start, csum, 2025 (unsigned long long)start, csum,
2006 (unsigned long long)private); 2026 (unsigned long long)private);
2007 }
2008 memset(kaddr + offset, 1, end - start + 1); 2027 memset(kaddr + offset, 1, end - start + 1);
2009 flush_dcache_page(page); 2028 flush_dcache_page(page);
2010 kunmap_atomic(kaddr, KM_USER0); 2029 kunmap_atomic(kaddr, KM_USER0);
@@ -2239,7 +2258,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2239 2258
2240 /* insert an orphan item to track this unlinked/truncated file */ 2259 /* insert an orphan item to track this unlinked/truncated file */
2241 if (insert >= 1) { 2260 if (insert >= 1) {
2242 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); 2261 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2243 BUG_ON(ret); 2262 BUG_ON(ret);
2244 } 2263 }
2245 2264
@@ -2276,7 +2295,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2276 spin_unlock(&root->orphan_lock); 2295 spin_unlock(&root->orphan_lock);
2277 2296
2278 if (trans && delete_item) { 2297 if (trans && delete_item) {
2279 ret = btrfs_del_orphan_item(trans, root, inode->i_ino); 2298 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2280 BUG_ON(ret); 2299 BUG_ON(ret);
2281 } 2300 }
2282 2301
@@ -2341,7 +2360,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2341 break; 2360 break;
2342 2361
2343 /* release the path since we're done with it */ 2362 /* release the path since we're done with it */
2344 btrfs_release_path(root, path); 2363 btrfs_release_path(path);
2345 2364
2346 /* 2365 /*
2347 * this is where we are basically btrfs_lookup, without the 2366 * this is where we are basically btrfs_lookup, without the
@@ -2542,7 +2561,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
2542 * try to precache a NULL acl entry for files that don't have 2561 * try to precache a NULL acl entry for files that don't have
2543 * any xattrs or acls 2562 * any xattrs or acls
2544 */ 2563 */
2545 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino); 2564 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2565 btrfs_ino(inode));
2546 if (!maybe_acls) 2566 if (!maybe_acls)
2547 cache_no_acl(inode); 2567 cache_no_acl(inode);
2548 2568
@@ -2649,11 +2669,26 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2649 struct extent_buffer *leaf; 2669 struct extent_buffer *leaf;
2650 int ret; 2670 int ret;
2651 2671
2672 /*
2673 * If root is tree root, it means this inode is used to
2674 * store free space information. And these inodes are updated
2675 * when committing the transaction, so they needn't delaye to
2676 * be updated, or deadlock will occured.
2677 */
2678 if (!is_free_space_inode(root, inode)) {
2679 ret = btrfs_delayed_update_inode(trans, root, inode);
2680 if (!ret)
2681 btrfs_set_inode_last_trans(trans, inode);
2682 return ret;
2683 }
2684
2652 path = btrfs_alloc_path(); 2685 path = btrfs_alloc_path();
2653 BUG_ON(!path); 2686 if (!path)
2687 return -ENOMEM;
2688
2654 path->leave_spinning = 1; 2689 path->leave_spinning = 1;
2655 ret = btrfs_lookup_inode(trans, root, path, 2690 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2656 &BTRFS_I(inode)->location, 1); 2691 1);
2657 if (ret) { 2692 if (ret) {
2658 if (ret > 0) 2693 if (ret > 0)
2659 ret = -ENOENT; 2694 ret = -ENOENT;
@@ -2663,7 +2698,7 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2663 btrfs_unlock_up_safe(path, 1); 2698 btrfs_unlock_up_safe(path, 1);
2664 leaf = path->nodes[0]; 2699 leaf = path->nodes[0];
2665 inode_item = btrfs_item_ptr(leaf, path->slots[0], 2700 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2666 struct btrfs_inode_item); 2701 struct btrfs_inode_item);
2667 2702
2668 fill_inode_item(trans, leaf, inode_item, inode); 2703 fill_inode_item(trans, leaf, inode_item, inode);
2669 btrfs_mark_buffer_dirty(leaf); 2704 btrfs_mark_buffer_dirty(leaf);
@@ -2674,7 +2709,6 @@ failed:
2674 return ret; 2709 return ret;
2675} 2710}
2676 2711
2677
2678/* 2712/*
2679 * unlink helper that gets used here in inode.c and in the tree logging 2713 * unlink helper that gets used here in inode.c and in the tree logging
2680 * recovery code. It remove a link in a directory with a given name, and 2714 * recovery code. It remove a link in a directory with a given name, and
@@ -2691,6 +2725,8 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2691 struct btrfs_dir_item *di; 2725 struct btrfs_dir_item *di;
2692 struct btrfs_key key; 2726 struct btrfs_key key;
2693 u64 index; 2727 u64 index;
2728 u64 ino = btrfs_ino(inode);
2729 u64 dir_ino = btrfs_ino(dir);
2694 2730
2695 path = btrfs_alloc_path(); 2731 path = btrfs_alloc_path();
2696 if (!path) { 2732 if (!path) {
@@ -2699,7 +2735,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2699 } 2735 }
2700 2736
2701 path->leave_spinning = 1; 2737 path->leave_spinning = 1;
2702 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2738 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2703 name, name_len, -1); 2739 name, name_len, -1);
2704 if (IS_ERR(di)) { 2740 if (IS_ERR(di)) {
2705 ret = PTR_ERR(di); 2741 ret = PTR_ERR(di);
@@ -2714,33 +2750,23 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2714 ret = btrfs_delete_one_dir_name(trans, root, path, di); 2750 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2715 if (ret) 2751 if (ret)
2716 goto err; 2752 goto err;
2717 btrfs_release_path(root, path); 2753 btrfs_release_path(path);
2718 2754
2719 ret = btrfs_del_inode_ref(trans, root, name, name_len, 2755 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2720 inode->i_ino, 2756 dir_ino, &index);
2721 dir->i_ino, &index);
2722 if (ret) { 2757 if (ret) {
2723 printk(KERN_INFO "btrfs failed to delete reference to %.*s, " 2758 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2724 "inode %lu parent %lu\n", name_len, name, 2759 "inode %llu parent %llu\n", name_len, name,
2725 inode->i_ino, dir->i_ino); 2760 (unsigned long long)ino, (unsigned long long)dir_ino);
2726 goto err; 2761 goto err;
2727 } 2762 }
2728 2763
2729 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 2764 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2730 index, name, name_len, -1); 2765 if (ret)
2731 if (IS_ERR(di)) {
2732 ret = PTR_ERR(di);
2733 goto err;
2734 }
2735 if (!di) {
2736 ret = -ENOENT;
2737 goto err; 2766 goto err;
2738 }
2739 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2740 btrfs_release_path(root, path);
2741 2767
2742 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, 2768 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2743 inode, dir->i_ino); 2769 inode, dir_ino);
2744 BUG_ON(ret != 0 && ret != -ENOENT); 2770 BUG_ON(ret != 0 && ret != -ENOENT);
2745 2771
2746 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, 2772 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
@@ -2818,12 +2844,14 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2818 int check_link = 1; 2844 int check_link = 1;
2819 int err = -ENOSPC; 2845 int err = -ENOSPC;
2820 int ret; 2846 int ret;
2847 u64 ino = btrfs_ino(inode);
2848 u64 dir_ino = btrfs_ino(dir);
2821 2849
2822 trans = btrfs_start_transaction(root, 10); 2850 trans = btrfs_start_transaction(root, 10);
2823 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 2851 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2824 return trans; 2852 return trans;
2825 2853
2826 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 2854 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2827 return ERR_PTR(-ENOSPC); 2855 return ERR_PTR(-ENOSPC);
2828 2856
2829 /* check if there is someone else holds reference */ 2857 /* check if there is someone else holds reference */
@@ -2864,7 +2892,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2864 } else { 2892 } else {
2865 check_link = 0; 2893 check_link = 0;
2866 } 2894 }
2867 btrfs_release_path(root, path); 2895 btrfs_release_path(path);
2868 2896
2869 ret = btrfs_lookup_inode(trans, root, path, 2897 ret = btrfs_lookup_inode(trans, root, path,
2870 &BTRFS_I(inode)->location, 0); 2898 &BTRFS_I(inode)->location, 0);
@@ -2878,11 +2906,11 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2878 } else { 2906 } else {
2879 check_link = 0; 2907 check_link = 0;
2880 } 2908 }
2881 btrfs_release_path(root, path); 2909 btrfs_release_path(path);
2882 2910
2883 if (ret == 0 && S_ISREG(inode->i_mode)) { 2911 if (ret == 0 && S_ISREG(inode->i_mode)) {
2884 ret = btrfs_lookup_file_extent(trans, root, path, 2912 ret = btrfs_lookup_file_extent(trans, root, path,
2885 inode->i_ino, (u64)-1, 0); 2913 ino, (u64)-1, 0);
2886 if (ret < 0) { 2914 if (ret < 0) {
2887 err = ret; 2915 err = ret;
2888 goto out; 2916 goto out;
@@ -2890,7 +2918,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2890 BUG_ON(ret == 0); 2918 BUG_ON(ret == 0);
2891 if (check_path_shared(root, path)) 2919 if (check_path_shared(root, path))
2892 goto out; 2920 goto out;
2893 btrfs_release_path(root, path); 2921 btrfs_release_path(path);
2894 } 2922 }
2895 2923
2896 if (!check_link) { 2924 if (!check_link) {
@@ -2898,7 +2926,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2898 goto out; 2926 goto out;
2899 } 2927 }
2900 2928
2901 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2929 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2902 dentry->d_name.name, dentry->d_name.len, 0); 2930 dentry->d_name.name, dentry->d_name.len, 0);
2903 if (IS_ERR(di)) { 2931 if (IS_ERR(di)) {
2904 err = PTR_ERR(di); 2932 err = PTR_ERR(di);
@@ -2911,11 +2939,11 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2911 err = 0; 2939 err = 0;
2912 goto out; 2940 goto out;
2913 } 2941 }
2914 btrfs_release_path(root, path); 2942 btrfs_release_path(path);
2915 2943
2916 ref = btrfs_lookup_inode_ref(trans, root, path, 2944 ref = btrfs_lookup_inode_ref(trans, root, path,
2917 dentry->d_name.name, dentry->d_name.len, 2945 dentry->d_name.name, dentry->d_name.len,
2918 inode->i_ino, dir->i_ino, 0); 2946 ino, dir_ino, 0);
2919 if (IS_ERR(ref)) { 2947 if (IS_ERR(ref)) {
2920 err = PTR_ERR(ref); 2948 err = PTR_ERR(ref);
2921 goto out; 2949 goto out;
@@ -2924,9 +2952,17 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2924 if (check_path_shared(root, path)) 2952 if (check_path_shared(root, path))
2925 goto out; 2953 goto out;
2926 index = btrfs_inode_ref_index(path->nodes[0], ref); 2954 index = btrfs_inode_ref_index(path->nodes[0], ref);
2927 btrfs_release_path(root, path); 2955 btrfs_release_path(path);
2928 2956
2929 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, 2957 /*
2958 * This is a commit root search, if we can lookup inode item and other
2959 * relative items in the commit root, it means the transaction of
2960 * dir/file creation has been committed, and the dir index item that we
2961 * delay to insert has also been inserted into the commit root. So
2962 * we needn't worry about the delayed insertion of the dir index item
2963 * here.
2964 */
2965 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2930 dentry->d_name.name, dentry->d_name.len, 0); 2966 dentry->d_name.name, dentry->d_name.len, 0);
2931 if (IS_ERR(di)) { 2967 if (IS_ERR(di)) {
2932 err = PTR_ERR(di); 2968 err = PTR_ERR(di);
@@ -2999,54 +3035,47 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2999 struct btrfs_key key; 3035 struct btrfs_key key;
3000 u64 index; 3036 u64 index;
3001 int ret; 3037 int ret;
3038 u64 dir_ino = btrfs_ino(dir);
3002 3039
3003 path = btrfs_alloc_path(); 3040 path = btrfs_alloc_path();
3004 if (!path) 3041 if (!path)
3005 return -ENOMEM; 3042 return -ENOMEM;
3006 3043
3007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 3044 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3008 name, name_len, -1); 3045 name, name_len, -1);
3009 BUG_ON(!di || IS_ERR(di)); 3046 BUG_ON(IS_ERR_OR_NULL(di));
3010 3047
3011 leaf = path->nodes[0]; 3048 leaf = path->nodes[0];
3012 btrfs_dir_item_key_to_cpu(leaf, di, &key); 3049 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3013 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); 3050 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3014 ret = btrfs_delete_one_dir_name(trans, root, path, di); 3051 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3015 BUG_ON(ret); 3052 BUG_ON(ret);
3016 btrfs_release_path(root, path); 3053 btrfs_release_path(path);
3017 3054
3018 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, 3055 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3019 objectid, root->root_key.objectid, 3056 objectid, root->root_key.objectid,
3020 dir->i_ino, &index, name, name_len); 3057 dir_ino, &index, name, name_len);
3021 if (ret < 0) { 3058 if (ret < 0) {
3022 BUG_ON(ret != -ENOENT); 3059 BUG_ON(ret != -ENOENT);
3023 di = btrfs_search_dir_index_item(root, path, dir->i_ino, 3060 di = btrfs_search_dir_index_item(root, path, dir_ino,
3024 name, name_len); 3061 name, name_len);
3025 BUG_ON(!di || IS_ERR(di)); 3062 BUG_ON(IS_ERR_OR_NULL(di));
3026 3063
3027 leaf = path->nodes[0]; 3064 leaf = path->nodes[0];
3028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3065 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029 btrfs_release_path(root, path); 3066 btrfs_release_path(path);
3030 index = key.offset; 3067 index = key.offset;
3031 } 3068 }
3069 btrfs_release_path(path);
3032 3070
3033 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 3071 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3034 index, name, name_len, -1);
3035 BUG_ON(!di || IS_ERR(di));
3036
3037 leaf = path->nodes[0];
3038 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3039 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3040 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3041 BUG_ON(ret); 3072 BUG_ON(ret);
3042 btrfs_release_path(root, path);
3043 3073
3044 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 3074 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3045 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 3075 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3046 ret = btrfs_update_inode(trans, root, dir); 3076 ret = btrfs_update_inode(trans, root, dir);
3047 BUG_ON(ret); 3077 BUG_ON(ret);
3048 3078
3049 btrfs_free_path(path);
3050 return 0; 3079 return 0;
3051} 3080}
3052 3081
@@ -3059,14 +3088,14 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3059 unsigned long nr = 0; 3088 unsigned long nr = 0;
3060 3089
3061 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 3090 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3062 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 3091 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3063 return -ENOTEMPTY; 3092 return -ENOTEMPTY;
3064 3093
3065 trans = __unlink_start_trans(dir, dentry); 3094 trans = __unlink_start_trans(dir, dentry);
3066 if (IS_ERR(trans)) 3095 if (IS_ERR(trans))
3067 return PTR_ERR(trans); 3096 return PTR_ERR(trans);
3068 3097
3069 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3098 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3070 err = btrfs_unlink_subvol(trans, root, dir, 3099 err = btrfs_unlink_subvol(trans, root, dir,
3071 BTRFS_I(inode)->location.objectid, 3100 BTRFS_I(inode)->location.objectid,
3072 dentry->d_name.name, 3101 dentry->d_name.name,
@@ -3091,178 +3120,6 @@ out:
3091 return err; 3120 return err;
3092} 3121}
3093 3122
3094#if 0
3095/*
3096 * when truncating bytes in a file, it is possible to avoid reading
3097 * the leaves that contain only checksum items. This can be the
3098 * majority of the IO required to delete a large file, but it must
3099 * be done carefully.
3100 *
3101 * The keys in the level just above the leaves are checked to make sure
3102 * the lowest key in a given leaf is a csum key, and starts at an offset
3103 * after the new size.
3104 *
3105 * Then the key for the next leaf is checked to make sure it also has
3106 * a checksum item for the same file. If it does, we know our target leaf
3107 * contains only checksum items, and it can be safely freed without reading
3108 * it.
3109 *
3110 * This is just an optimization targeted at large files. It may do
3111 * nothing. It will return 0 unless things went badly.
3112 */
3113static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3114 struct btrfs_root *root,
3115 struct btrfs_path *path,
3116 struct inode *inode, u64 new_size)
3117{
3118 struct btrfs_key key;
3119 int ret;
3120 int nritems;
3121 struct btrfs_key found_key;
3122 struct btrfs_key other_key;
3123 struct btrfs_leaf_ref *ref;
3124 u64 leaf_gen;
3125 u64 leaf_start;
3126
3127 path->lowest_level = 1;
3128 key.objectid = inode->i_ino;
3129 key.type = BTRFS_CSUM_ITEM_KEY;
3130 key.offset = new_size;
3131again:
3132 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3133 if (ret < 0)
3134 goto out;
3135
3136 if (path->nodes[1] == NULL) {
3137 ret = 0;
3138 goto out;
3139 }
3140 ret = 0;
3141 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3142 nritems = btrfs_header_nritems(path->nodes[1]);
3143
3144 if (!nritems)
3145 goto out;
3146
3147 if (path->slots[1] >= nritems)
3148 goto next_node;
3149
3150 /* did we find a key greater than anything we want to delete? */
3151 if (found_key.objectid > inode->i_ino ||
3152 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3153 goto out;
3154
3155 /* we check the next key in the node to make sure the leave contains
3156 * only checksum items. This comparison doesn't work if our
3157 * leaf is the last one in the node
3158 */
3159 if (path->slots[1] + 1 >= nritems) {
3160next_node:
3161 /* search forward from the last key in the node, this
3162 * will bring us into the next node in the tree
3163 */
3164 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3165
3166 /* unlikely, but we inc below, so check to be safe */
3167 if (found_key.offset == (u64)-1)
3168 goto out;
3169
3170 /* search_forward needs a path with locks held, do the
3171 * search again for the original key. It is possible
3172 * this will race with a balance and return a path that
3173 * we could modify, but this drop is just an optimization
3174 * and is allowed to miss some leaves.
3175 */
3176 btrfs_release_path(root, path);
3177 found_key.offset++;
3178
3179 /* setup a max key for search_forward */
3180 other_key.offset = (u64)-1;
3181 other_key.type = key.type;
3182 other_key.objectid = key.objectid;
3183
3184 path->keep_locks = 1;
3185 ret = btrfs_search_forward(root, &found_key, &other_key,
3186 path, 0, 0);
3187 path->keep_locks = 0;
3188 if (ret || found_key.objectid != key.objectid ||
3189 found_key.type != key.type) {
3190 ret = 0;
3191 goto out;
3192 }
3193
3194 key.offset = found_key.offset;
3195 btrfs_release_path(root, path);
3196 cond_resched();
3197 goto again;
3198 }
3199
3200 /* we know there's one more slot after us in the tree,
3201 * read that key so we can verify it is also a checksum item
3202 */
3203 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3204
3205 if (found_key.objectid < inode->i_ino)
3206 goto next_key;
3207
3208 if (found_key.type != key.type || found_key.offset < new_size)
3209 goto next_key;
3210
3211 /*
3212 * if the key for the next leaf isn't a csum key from this objectid,
3213 * we can't be sure there aren't good items inside this leaf.
3214 * Bail out
3215 */
3216 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3217 goto out;
3218
3219 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3220 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3221 /*
3222 * it is safe to delete this leaf, it contains only
3223 * csum items from this inode at an offset >= new_size
3224 */
3225 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3226 BUG_ON(ret);
3227
3228 if (root->ref_cows && leaf_gen < trans->transid) {
3229 ref = btrfs_alloc_leaf_ref(root, 0);
3230 if (ref) {
3231 ref->root_gen = root->root_key.offset;
3232 ref->bytenr = leaf_start;
3233 ref->owner = 0;
3234 ref->generation = leaf_gen;
3235 ref->nritems = 0;
3236
3237 btrfs_sort_leaf_ref(ref);
3238
3239 ret = btrfs_add_leaf_ref(root, ref, 0);
3240 WARN_ON(ret);
3241 btrfs_free_leaf_ref(root, ref);
3242 } else {
3243 WARN_ON(1);
3244 }
3245 }
3246next_key:
3247 btrfs_release_path(root, path);
3248
3249 if (other_key.objectid == inode->i_ino &&
3250 other_key.type == key.type && other_key.offset > key.offset) {
3251 key.offset = other_key.offset;
3252 cond_resched();
3253 goto again;
3254 }
3255 ret = 0;
3256out:
3257 /* fixup any changes we've made to the path */
3258 path->lowest_level = 0;
3259 path->keep_locks = 0;
3260 btrfs_release_path(root, path);
3261 return ret;
3262}
3263
3264#endif
3265
3266/* 3123/*
3267 * this can truncate away extent items, csum items and directory items. 3124 * this can truncate away extent items, csum items and directory items.
3268 * It starts at a high offset and removes keys until it can't find 3125 * It starts at a high offset and removes keys until it can't find
@@ -3298,17 +3155,27 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3298 int encoding; 3155 int encoding;
3299 int ret; 3156 int ret;
3300 int err = 0; 3157 int err = 0;
3158 u64 ino = btrfs_ino(inode);
3301 3159
3302 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); 3160 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3303 3161
3304 if (root->ref_cows || root == root->fs_info->tree_root) 3162 if (root->ref_cows || root == root->fs_info->tree_root)
3305 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); 3163 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3306 3164
3165 /*
3166 * This function is also used to drop the items in the log tree before
3167 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3168 * it is used to drop the loged items. So we shouldn't kill the delayed
3169 * items.
3170 */
3171 if (min_type == 0 && root == BTRFS_I(inode)->root)
3172 btrfs_kill_delayed_inode_items(inode);
3173
3307 path = btrfs_alloc_path(); 3174 path = btrfs_alloc_path();
3308 BUG_ON(!path); 3175 BUG_ON(!path);
3309 path->reada = -1; 3176 path->reada = -1;
3310 3177
3311 key.objectid = inode->i_ino; 3178 key.objectid = ino;
3312 key.offset = (u64)-1; 3179 key.offset = (u64)-1;
3313 key.type = (u8)-1; 3180 key.type = (u8)-1;
3314 3181
@@ -3336,7 +3203,7 @@ search_again:
3336 found_type = btrfs_key_type(&found_key); 3203 found_type = btrfs_key_type(&found_key);
3337 encoding = 0; 3204 encoding = 0;
3338 3205
3339 if (found_key.objectid != inode->i_ino) 3206 if (found_key.objectid != ino)
3340 break; 3207 break;
3341 3208
3342 if (found_type < min_type) 3209 if (found_type < min_type)
@@ -3426,7 +3293,6 @@ search_again:
3426 btrfs_file_extent_calc_inline_size(size); 3293 btrfs_file_extent_calc_inline_size(size);
3427 ret = btrfs_truncate_item(trans, root, path, 3294 ret = btrfs_truncate_item(trans, root, path,
3428 size, 1); 3295 size, 1);
3429 BUG_ON(ret);
3430 } else if (root->ref_cows) { 3296 } else if (root->ref_cows) {
3431 inode_sub_bytes(inode, item_end + 1 - 3297 inode_sub_bytes(inode, item_end + 1 -
3432 found_key.offset); 3298 found_key.offset);
@@ -3455,7 +3321,7 @@ delete:
3455 ret = btrfs_free_extent(trans, root, extent_start, 3321 ret = btrfs_free_extent(trans, root, extent_start,
3456 extent_num_bytes, 0, 3322 extent_num_bytes, 0,
3457 btrfs_header_owner(leaf), 3323 btrfs_header_owner(leaf),
3458 inode->i_ino, extent_offset); 3324 ino, extent_offset);
3459 BUG_ON(ret); 3325 BUG_ON(ret);
3460 } 3326 }
3461 3327
@@ -3464,7 +3330,9 @@ delete:
3464 3330
3465 if (path->slots[0] == 0 || 3331 if (path->slots[0] == 0 ||
3466 path->slots[0] != pending_del_slot) { 3332 path->slots[0] != pending_del_slot) {
3467 if (root->ref_cows) { 3333 if (root->ref_cows &&
3334 BTRFS_I(inode)->location.objectid !=
3335 BTRFS_FREE_INO_OBJECTID) {
3468 err = -EAGAIN; 3336 err = -EAGAIN;
3469 goto out; 3337 goto out;
3470 } 3338 }
@@ -3475,7 +3343,7 @@ delete:
3475 BUG_ON(ret); 3343 BUG_ON(ret);
3476 pending_del_nr = 0; 3344 pending_del_nr = 0;
3477 } 3345 }
3478 btrfs_release_path(root, path); 3346 btrfs_release_path(path);
3479 goto search_again; 3347 goto search_again;
3480 } else { 3348 } else {
3481 path->slots[0]--; 3349 path->slots[0]--;
@@ -3633,7 +3501,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3633 while (1) { 3501 while (1) {
3634 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 3502 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3635 block_end - cur_offset, 0); 3503 block_end - cur_offset, 0);
3636 BUG_ON(IS_ERR(em) || !em); 3504 BUG_ON(IS_ERR_OR_NULL(em));
3637 last_byte = min(extent_map_end(em), block_end); 3505 last_byte = min(extent_map_end(em), block_end);
3638 last_byte = (last_byte + mask) & ~mask; 3506 last_byte = (last_byte + mask) & ~mask;
3639 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { 3507 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
@@ -3653,7 +3521,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3653 break; 3521 break;
3654 3522
3655 err = btrfs_insert_file_extent(trans, root, 3523 err = btrfs_insert_file_extent(trans, root,
3656 inode->i_ino, cur_offset, 0, 3524 btrfs_ino(inode), cur_offset, 0,
3657 0, hole_size, 0, hole_size, 3525 0, hole_size, 0, hole_size,
3658 0, 0, 0); 3526 0, 0, 0);
3659 if (err) 3527 if (err)
@@ -3755,7 +3623,7 @@ void btrfs_evict_inode(struct inode *inode)
3755 3623
3756 truncate_inode_pages(&inode->i_data, 0); 3624 truncate_inode_pages(&inode->i_data, 0);
3757 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || 3625 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3758 root == root->fs_info->tree_root)) 3626 is_free_space_inode(root, inode)))
3759 goto no_delete; 3627 goto no_delete;
3760 3628
3761 if (is_bad_inode(inode)) { 3629 if (is_bad_inode(inode)) {
@@ -3807,6 +3675,10 @@ void btrfs_evict_inode(struct inode *inode)
3807 BUG_ON(ret); 3675 BUG_ON(ret);
3808 } 3676 }
3809 3677
3678 if (!(root == root->fs_info->tree_root ||
3679 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3680 btrfs_return_ino(root, btrfs_ino(inode));
3681
3810 nr = trans->blocks_used; 3682 nr = trans->blocks_used;
3811 btrfs_end_transaction(trans, root); 3683 btrfs_end_transaction(trans, root);
3812 btrfs_btree_balance_dirty(root, nr); 3684 btrfs_btree_balance_dirty(root, nr);
@@ -3832,12 +3704,12 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3832 path = btrfs_alloc_path(); 3704 path = btrfs_alloc_path();
3833 BUG_ON(!path); 3705 BUG_ON(!path);
3834 3706
3835 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, 3707 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3836 namelen, 0); 3708 namelen, 0);
3837 if (IS_ERR(di)) 3709 if (IS_ERR(di))
3838 ret = PTR_ERR(di); 3710 ret = PTR_ERR(di);
3839 3711
3840 if (!di || IS_ERR(di)) 3712 if (IS_ERR_OR_NULL(di))
3841 goto out_err; 3713 goto out_err;
3842 3714
3843 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); 3715 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
@@ -3885,7 +3757,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3885 3757
3886 leaf = path->nodes[0]; 3758 leaf = path->nodes[0];
3887 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); 3759 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3888 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || 3760 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3889 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) 3761 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3890 goto out; 3762 goto out;
3891 3763
@@ -3895,7 +3767,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3895 if (ret) 3767 if (ret)
3896 goto out; 3768 goto out;
3897 3769
3898 btrfs_release_path(root->fs_info->tree_root, path); 3770 btrfs_release_path(path);
3899 3771
3900 new_root = btrfs_read_fs_root_no_name(root->fs_info, location); 3772 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3901 if (IS_ERR(new_root)) { 3773 if (IS_ERR(new_root)) {
@@ -3924,6 +3796,7 @@ static void inode_tree_add(struct inode *inode)
3924 struct btrfs_inode *entry; 3796 struct btrfs_inode *entry;
3925 struct rb_node **p; 3797 struct rb_node **p;
3926 struct rb_node *parent; 3798 struct rb_node *parent;
3799 u64 ino = btrfs_ino(inode);
3927again: 3800again:
3928 p = &root->inode_tree.rb_node; 3801 p = &root->inode_tree.rb_node;
3929 parent = NULL; 3802 parent = NULL;
@@ -3936,9 +3809,9 @@ again:
3936 parent = *p; 3809 parent = *p;
3937 entry = rb_entry(parent, struct btrfs_inode, rb_node); 3810 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3938 3811
3939 if (inode->i_ino < entry->vfs_inode.i_ino) 3812 if (ino < btrfs_ino(&entry->vfs_inode))
3940 p = &parent->rb_left; 3813 p = &parent->rb_left;
3941 else if (inode->i_ino > entry->vfs_inode.i_ino) 3814 else if (ino > btrfs_ino(&entry->vfs_inode))
3942 p = &parent->rb_right; 3815 p = &parent->rb_right;
3943 else { 3816 else {
3944 WARN_ON(!(entry->vfs_inode.i_state & 3817 WARN_ON(!(entry->vfs_inode.i_state &
@@ -4002,9 +3875,9 @@ again:
4002 prev = node; 3875 prev = node;
4003 entry = rb_entry(node, struct btrfs_inode, rb_node); 3876 entry = rb_entry(node, struct btrfs_inode, rb_node);
4004 3877
4005 if (objectid < entry->vfs_inode.i_ino) 3878 if (objectid < btrfs_ino(&entry->vfs_inode))
4006 node = node->rb_left; 3879 node = node->rb_left;
4007 else if (objectid > entry->vfs_inode.i_ino) 3880 else if (objectid > btrfs_ino(&entry->vfs_inode))
4008 node = node->rb_right; 3881 node = node->rb_right;
4009 else 3882 else
4010 break; 3883 break;
@@ -4012,7 +3885,7 @@ again:
4012 if (!node) { 3885 if (!node) {
4013 while (prev) { 3886 while (prev) {
4014 entry = rb_entry(prev, struct btrfs_inode, rb_node); 3887 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4015 if (objectid <= entry->vfs_inode.i_ino) { 3888 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4016 node = prev; 3889 node = prev;
4017 break; 3890 break;
4018 } 3891 }
@@ -4021,7 +3894,7 @@ again:
4021 } 3894 }
4022 while (node) { 3895 while (node) {
4023 entry = rb_entry(node, struct btrfs_inode, rb_node); 3896 entry = rb_entry(node, struct btrfs_inode, rb_node);
4024 objectid = entry->vfs_inode.i_ino + 1; 3897 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4025 inode = igrab(&entry->vfs_inode); 3898 inode = igrab(&entry->vfs_inode);
4026 if (inode) { 3899 if (inode) {
4027 spin_unlock(&root->inode_lock); 3900 spin_unlock(&root->inode_lock);
@@ -4059,7 +3932,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
4059static int btrfs_find_actor(struct inode *inode, void *opaque) 3932static int btrfs_find_actor(struct inode *inode, void *opaque)
4060{ 3933{
4061 struct btrfs_iget_args *args = opaque; 3934 struct btrfs_iget_args *args = opaque;
4062 return args->ino == inode->i_ino && 3935 return args->ino == btrfs_ino(inode) &&
4063 args->root == BTRFS_I(inode)->root; 3936 args->root == BTRFS_I(inode)->root;
4064} 3937}
4065 3938
@@ -4204,7 +4077,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4204 return d_splice_alias(inode, dentry); 4077 return d_splice_alias(inode, dentry);
4205} 4078}
4206 4079
4207static unsigned char btrfs_filetype_table[] = { 4080unsigned char btrfs_filetype_table[] = {
4208 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK 4081 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4209}; 4082};
4210 4083
@@ -4218,6 +4091,8 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4218 struct btrfs_key key; 4091 struct btrfs_key key;
4219 struct btrfs_key found_key; 4092 struct btrfs_key found_key;
4220 struct btrfs_path *path; 4093 struct btrfs_path *path;
4094 struct list_head ins_list;
4095 struct list_head del_list;
4221 int ret; 4096 int ret;
4222 struct extent_buffer *leaf; 4097 struct extent_buffer *leaf;
4223 int slot; 4098 int slot;
@@ -4230,6 +4105,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4230 char tmp_name[32]; 4105 char tmp_name[32];
4231 char *name_ptr; 4106 char *name_ptr;
4232 int name_len; 4107 int name_len;
4108 int is_curr = 0; /* filp->f_pos points to the current index? */
4233 4109
4234 /* FIXME, use a real flag for deciding about the key type */ 4110 /* FIXME, use a real flag for deciding about the key type */
4235 if (root->fs_info->tree_root == root) 4111 if (root->fs_info->tree_root == root)
@@ -4237,9 +4113,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4237 4113
4238 /* special case for "." */ 4114 /* special case for "." */
4239 if (filp->f_pos == 0) { 4115 if (filp->f_pos == 0) {
4240 over = filldir(dirent, ".", 1, 4116 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4241 1, inode->i_ino,
4242 DT_DIR);
4243 if (over) 4117 if (over)
4244 return 0; 4118 return 0;
4245 filp->f_pos = 1; 4119 filp->f_pos = 1;
@@ -4256,11 +4130,18 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4256 path = btrfs_alloc_path(); 4130 path = btrfs_alloc_path();
4257 if (!path) 4131 if (!path)
4258 return -ENOMEM; 4132 return -ENOMEM;
4133
4259 path->reada = 1; 4134 path->reada = 1;
4260 4135
4136 if (key_type == BTRFS_DIR_INDEX_KEY) {
4137 INIT_LIST_HEAD(&ins_list);
4138 INIT_LIST_HEAD(&del_list);
4139 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4140 }
4141
4261 btrfs_set_key_type(&key, key_type); 4142 btrfs_set_key_type(&key, key_type);
4262 key.offset = filp->f_pos; 4143 key.offset = filp->f_pos;
4263 key.objectid = inode->i_ino; 4144 key.objectid = btrfs_ino(inode);
4264 4145
4265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4146 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4266 if (ret < 0) 4147 if (ret < 0)
@@ -4287,8 +4168,13 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4287 break; 4168 break;
4288 if (found_key.offset < filp->f_pos) 4169 if (found_key.offset < filp->f_pos)
4289 goto next; 4170 goto next;
4171 if (key_type == BTRFS_DIR_INDEX_KEY &&
4172 btrfs_should_delete_dir_index(&del_list,
4173 found_key.offset))
4174 goto next;
4290 4175
4291 filp->f_pos = found_key.offset; 4176 filp->f_pos = found_key.offset;
4177 is_curr = 1;
4292 4178
4293 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 4179 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4294 di_cur = 0; 4180 di_cur = 0;
@@ -4343,6 +4229,15 @@ next:
4343 path->slots[0]++; 4229 path->slots[0]++;
4344 } 4230 }
4345 4231
4232 if (key_type == BTRFS_DIR_INDEX_KEY) {
4233 if (is_curr)
4234 filp->f_pos++;
4235 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4236 &ins_list);
4237 if (ret)
4238 goto nopos;
4239 }
4240
4346 /* Reached end of directory/root. Bump pos past the last item. */ 4241 /* Reached end of directory/root. Bump pos past the last item. */
4347 if (key_type == BTRFS_DIR_INDEX_KEY) 4242 if (key_type == BTRFS_DIR_INDEX_KEY)
4348 /* 4243 /*
@@ -4355,6 +4250,8 @@ next:
4355nopos: 4250nopos:
4356 ret = 0; 4251 ret = 0;
4357err: 4252err:
4253 if (key_type == BTRFS_DIR_INDEX_KEY)
4254 btrfs_put_delayed_items(&ins_list, &del_list);
4358 btrfs_free_path(path); 4255 btrfs_free_path(path);
4359 return ret; 4256 return ret;
4360} 4257}
@@ -4370,7 +4267,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4370 return 0; 4267 return 0;
4371 4268
4372 smp_mb(); 4269 smp_mb();
4373 nolock = (root->fs_info->closing && root == root->fs_info->tree_root); 4270 if (root->fs_info->closing && is_free_space_inode(root, inode))
4271 nolock = true;
4374 4272
4375 if (wbc->sync_mode == WB_SYNC_ALL) { 4273 if (wbc->sync_mode == WB_SYNC_ALL) {
4376 if (nolock) 4274 if (nolock)
@@ -4411,24 +4309,24 @@ void btrfs_dirty_inode(struct inode *inode)
4411 btrfs_end_transaction(trans, root); 4309 btrfs_end_transaction(trans, root);
4412 trans = btrfs_start_transaction(root, 1); 4310 trans = btrfs_start_transaction(root, 1);
4413 if (IS_ERR(trans)) { 4311 if (IS_ERR(trans)) {
4414 if (printk_ratelimit()) { 4312 printk_ratelimited(KERN_ERR "btrfs: fail to "
4415 printk(KERN_ERR "btrfs: fail to " 4313 "dirty inode %llu error %ld\n",
4416 "dirty inode %lu error %ld\n", 4314 (unsigned long long)btrfs_ino(inode),
4417 inode->i_ino, PTR_ERR(trans)); 4315 PTR_ERR(trans));
4418 }
4419 return; 4316 return;
4420 } 4317 }
4421 4318
4422 ret = btrfs_update_inode(trans, root, inode); 4319 ret = btrfs_update_inode(trans, root, inode);
4423 if (ret) { 4320 if (ret) {
4424 if (printk_ratelimit()) { 4321 printk_ratelimited(KERN_ERR "btrfs: fail to "
4425 printk(KERN_ERR "btrfs: fail to " 4322 "dirty inode %llu error %d\n",
4426 "dirty inode %lu error %d\n", 4323 (unsigned long long)btrfs_ino(inode),
4427 inode->i_ino, ret); 4324 ret);
4428 }
4429 } 4325 }
4430 } 4326 }
4431 btrfs_end_transaction(trans, root); 4327 btrfs_end_transaction(trans, root);
4328 if (BTRFS_I(inode)->delayed_node)
4329 btrfs_balance_delayed_items(root);
4432} 4330}
4433 4331
4434/* 4332/*
@@ -4444,7 +4342,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4444 struct extent_buffer *leaf; 4342 struct extent_buffer *leaf;
4445 int ret; 4343 int ret;
4446 4344
4447 key.objectid = inode->i_ino; 4345 key.objectid = btrfs_ino(inode);
4448 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); 4346 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4449 key.offset = (u64)-1; 4347 key.offset = (u64)-1;
4450 4348
@@ -4476,7 +4374,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4476 leaf = path->nodes[0]; 4374 leaf = path->nodes[0];
4477 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4375 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4478 4376
4479 if (found_key.objectid != inode->i_ino || 4377 if (found_key.objectid != btrfs_ino(inode) ||
4480 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { 4378 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4481 BTRFS_I(inode)->index_cnt = 2; 4379 BTRFS_I(inode)->index_cnt = 2;
4482 goto out; 4380 goto out;
@@ -4497,9 +4395,12 @@ int btrfs_set_inode_index(struct inode *dir, u64 *index)
4497 int ret = 0; 4395 int ret = 0;
4498 4396
4499 if (BTRFS_I(dir)->index_cnt == (u64)-1) { 4397 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4500 ret = btrfs_set_inode_index_count(dir); 4398 ret = btrfs_inode_delayed_dir_index_count(dir);
4501 if (ret) 4399 if (ret) {
4502 return ret; 4400 ret = btrfs_set_inode_index_count(dir);
4401 if (ret)
4402 return ret;
4403 }
4503 } 4404 }
4504 4405
4505 *index = BTRFS_I(dir)->index_cnt; 4406 *index = BTRFS_I(dir)->index_cnt;
@@ -4535,6 +4436,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4535 return ERR_PTR(-ENOMEM); 4436 return ERR_PTR(-ENOMEM);
4536 } 4437 }
4537 4438
4439 /*
4440 * we have to initialize this early, so we can reclaim the inode
4441 * number if we fail afterwards in this function.
4442 */
4443 inode->i_ino = objectid;
4444
4538 if (dir) { 4445 if (dir) {
4539 trace_btrfs_inode_request(dir); 4446 trace_btrfs_inode_request(dir);
4540 4447
@@ -4578,7 +4485,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4578 goto fail; 4485 goto fail;
4579 4486
4580 inode_init_owner(inode, dir, mode); 4487 inode_init_owner(inode, dir, mode);
4581 inode->i_ino = objectid;
4582 inode_set_bytes(inode, 0); 4488 inode_set_bytes(inode, 0);
4583 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 4489 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4584 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 4490 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
@@ -4642,29 +4548,29 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4642 int ret = 0; 4548 int ret = 0;
4643 struct btrfs_key key; 4549 struct btrfs_key key;
4644 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4550 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4551 u64 ino = btrfs_ino(inode);
4552 u64 parent_ino = btrfs_ino(parent_inode);
4645 4553
4646 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4554 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4647 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); 4555 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4648 } else { 4556 } else {
4649 key.objectid = inode->i_ino; 4557 key.objectid = ino;
4650 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4558 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4651 key.offset = 0; 4559 key.offset = 0;
4652 } 4560 }
4653 4561
4654 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4562 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4655 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4563 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4656 key.objectid, root->root_key.objectid, 4564 key.objectid, root->root_key.objectid,
4657 parent_inode->i_ino, 4565 parent_ino, index, name, name_len);
4658 index, name, name_len);
4659 } else if (add_backref) { 4566 } else if (add_backref) {
4660 ret = btrfs_insert_inode_ref(trans, root, 4567 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4661 name, name_len, inode->i_ino, 4568 parent_ino, index);
4662 parent_inode->i_ino, index);
4663 } 4569 }
4664 4570
4665 if (ret == 0) { 4571 if (ret == 0) {
4666 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4572 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4667 parent_inode->i_ino, &key, 4573 parent_inode, &key,
4668 btrfs_inode_type(inode), index); 4574 btrfs_inode_type(inode), index);
4669 BUG_ON(ret); 4575 BUG_ON(ret);
4670 4576
@@ -4707,10 +4613,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4707 if (!new_valid_dev(rdev)) 4613 if (!new_valid_dev(rdev))
4708 return -EINVAL; 4614 return -EINVAL;
4709 4615
4710 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4711 if (err)
4712 return err;
4713
4714 /* 4616 /*
4715 * 2 for inode item and ref 4617 * 2 for inode item and ref
4716 * 2 for dir items 4618 * 2 for dir items
@@ -4720,8 +4622,12 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4720 if (IS_ERR(trans)) 4622 if (IS_ERR(trans))
4721 return PTR_ERR(trans); 4623 return PTR_ERR(trans);
4722 4624
4625 err = btrfs_find_free_ino(root, &objectid);
4626 if (err)
4627 goto out_unlock;
4628
4723 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4629 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4724 dentry->d_name.len, dir->i_ino, objectid, 4630 dentry->d_name.len, btrfs_ino(dir), objectid,
4725 mode, &index); 4631 mode, &index);
4726 if (IS_ERR(inode)) { 4632 if (IS_ERR(inode)) {
4727 err = PTR_ERR(inode); 4633 err = PTR_ERR(inode);
@@ -4765,9 +4671,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4765 u64 objectid; 4671 u64 objectid;
4766 u64 index = 0; 4672 u64 index = 0;
4767 4673
4768 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4769 if (err)
4770 return err;
4771 /* 4674 /*
4772 * 2 for inode item and ref 4675 * 2 for inode item and ref
4773 * 2 for dir items 4676 * 2 for dir items
@@ -4777,8 +4680,12 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4777 if (IS_ERR(trans)) 4680 if (IS_ERR(trans))
4778 return PTR_ERR(trans); 4681 return PTR_ERR(trans);
4779 4682
4683 err = btrfs_find_free_ino(root, &objectid);
4684 if (err)
4685 goto out_unlock;
4686
4780 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4687 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4781 dentry->d_name.len, dir->i_ino, objectid, 4688 dentry->d_name.len, btrfs_ino(dir), objectid,
4782 mode, &index); 4689 mode, &index);
4783 if (IS_ERR(inode)) { 4690 if (IS_ERR(inode)) {
4784 err = PTR_ERR(inode); 4691 err = PTR_ERR(inode);
@@ -4883,10 +4790,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4883 u64 index = 0; 4790 u64 index = 0;
4884 unsigned long nr = 1; 4791 unsigned long nr = 1;
4885 4792
4886 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4887 if (err)
4888 return err;
4889
4890 /* 4793 /*
4891 * 2 items for inode and ref 4794 * 2 items for inode and ref
4892 * 2 items for dir items 4795 * 2 items for dir items
@@ -4896,8 +4799,12 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4896 if (IS_ERR(trans)) 4799 if (IS_ERR(trans))
4897 return PTR_ERR(trans); 4800 return PTR_ERR(trans);
4898 4801
4802 err = btrfs_find_free_ino(root, &objectid);
4803 if (err)
4804 goto out_fail;
4805
4899 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4806 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4900 dentry->d_name.len, dir->i_ino, objectid, 4807 dentry->d_name.len, btrfs_ino(dir), objectid,
4901 S_IFDIR | mode, &index); 4808 S_IFDIR | mode, &index);
4902 if (IS_ERR(inode)) { 4809 if (IS_ERR(inode)) {
4903 err = PTR_ERR(inode); 4810 err = PTR_ERR(inode);
@@ -5016,7 +4923,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5016 u64 bytenr; 4923 u64 bytenr;
5017 u64 extent_start = 0; 4924 u64 extent_start = 0;
5018 u64 extent_end = 0; 4925 u64 extent_end = 0;
5019 u64 objectid = inode->i_ino; 4926 u64 objectid = btrfs_ino(inode);
5020 u32 found_type; 4927 u32 found_type;
5021 struct btrfs_path *path = NULL; 4928 struct btrfs_path *path = NULL;
5022 struct btrfs_root *root = BTRFS_I(inode)->root; 4929 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -5044,7 +4951,7 @@ again:
5044 else 4951 else
5045 goto out; 4952 goto out;
5046 } 4953 }
5047 em = alloc_extent_map(GFP_NOFS); 4954 em = alloc_extent_map();
5048 if (!em) { 4955 if (!em) {
5049 err = -ENOMEM; 4956 err = -ENOMEM;
5050 goto out; 4957 goto out;
@@ -5206,8 +5113,10 @@ again:
5206 kunmap(page); 5113 kunmap(page);
5207 free_extent_map(em); 5114 free_extent_map(em);
5208 em = NULL; 5115 em = NULL;
5209 btrfs_release_path(root, path); 5116
5117 btrfs_release_path(path);
5210 trans = btrfs_join_transaction(root); 5118 trans = btrfs_join_transaction(root);
5119
5211 if (IS_ERR(trans)) 5120 if (IS_ERR(trans))
5212 return ERR_CAST(trans); 5121 return ERR_CAST(trans);
5213 goto again; 5122 goto again;
@@ -5232,7 +5141,7 @@ not_found_em:
5232 em->block_start = EXTENT_MAP_HOLE; 5141 em->block_start = EXTENT_MAP_HOLE;
5233 set_bit(EXTENT_FLAG_VACANCY, &em->flags); 5142 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5234insert: 5143insert:
5235 btrfs_release_path(root, path); 5144 btrfs_release_path(path);
5236 if (em->start > start || extent_map_end(em) <= start) { 5145 if (em->start > start || extent_map_end(em) <= start) {
5237 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " 5146 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5238 "[%llu %llu]\n", (unsigned long long)em->start, 5147 "[%llu %llu]\n", (unsigned long long)em->start,
@@ -5365,7 +5274,7 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5365 u64 hole_start = start; 5274 u64 hole_start = start;
5366 u64 hole_len = len; 5275 u64 hole_len = len;
5367 5276
5368 em = alloc_extent_map(GFP_NOFS); 5277 em = alloc_extent_map();
5369 if (!em) { 5278 if (!em) {
5370 err = -ENOMEM; 5279 err = -ENOMEM;
5371 goto out; 5280 goto out;
@@ -5455,6 +5364,9 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5455 if (IS_ERR(trans)) 5364 if (IS_ERR(trans))
5456 return ERR_CAST(trans); 5365 return ERR_CAST(trans);
5457 5366
5367 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5368 btrfs_add_inode_defrag(trans, inode);
5369
5458 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 5370 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5459 5371
5460 alloc_hint = get_extent_allocation_hint(inode, start, len); 5372 alloc_hint = get_extent_allocation_hint(inode, start, len);
@@ -5466,7 +5378,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5466 } 5378 }
5467 5379
5468 if (!em) { 5380 if (!em) {
5469 em = alloc_extent_map(GFP_NOFS); 5381 em = alloc_extent_map();
5470 if (!em) { 5382 if (!em) {
5471 em = ERR_PTR(-ENOMEM); 5383 em = ERR_PTR(-ENOMEM);
5472 goto out; 5384 goto out;
@@ -5532,7 +5444,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5532 if (!path) 5444 if (!path)
5533 return -ENOMEM; 5445 return -ENOMEM;
5534 5446
5535 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 5447 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5536 offset, 0); 5448 offset, 0);
5537 if (ret < 0) 5449 if (ret < 0)
5538 goto out; 5450 goto out;
@@ -5549,7 +5461,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5549 ret = 0; 5461 ret = 0;
5550 leaf = path->nodes[0]; 5462 leaf = path->nodes[0];
5551 btrfs_item_key_to_cpu(leaf, &key, slot); 5463 btrfs_item_key_to_cpu(leaf, &key, slot);
5552 if (key.objectid != inode->i_ino || 5464 if (key.objectid != btrfs_ino(inode) ||
5553 key.type != BTRFS_EXTENT_DATA_KEY) { 5465 key.type != BTRFS_EXTENT_DATA_KEY) {
5554 /* not our file or wrong item type, must cow */ 5466 /* not our file or wrong item type, must cow */
5555 goto out; 5467 goto out;
@@ -5583,7 +5495,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5583 * look for other files referencing this extent, if we 5495 * look for other files referencing this extent, if we
5584 * find any we must cow 5496 * find any we must cow
5585 */ 5497 */
5586 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 5498 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5587 key.offset - backref_offset, disk_bytenr)) 5499 key.offset - backref_offset, disk_bytenr))
5588 goto out; 5500 goto out;
5589 5501
@@ -5773,9 +5685,10 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
5773 5685
5774 flush_dcache_page(bvec->bv_page); 5686 flush_dcache_page(bvec->bv_page);
5775 if (csum != *private) { 5687 if (csum != *private) {
5776 printk(KERN_ERR "btrfs csum failed ino %lu off" 5688 printk(KERN_ERR "btrfs csum failed ino %llu off"
5777 " %llu csum %u private %u\n", 5689 " %llu csum %u private %u\n",
5778 inode->i_ino, (unsigned long long)start, 5690 (unsigned long long)btrfs_ino(inode),
5691 (unsigned long long)start,
5779 csum, *private); 5692 csum, *private);
5780 err = -EIO; 5693 err = -EIO;
5781 } 5694 }
@@ -5922,9 +5835,9 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
5922 struct btrfs_dio_private *dip = bio->bi_private; 5835 struct btrfs_dio_private *dip = bio->bi_private;
5923 5836
5924 if (err) { 5837 if (err) {
5925 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " 5838 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5926 "sector %#Lx len %u err no %d\n", 5839 "sector %#Lx len %u err no %d\n",
5927 dip->inode->i_ino, bio->bi_rw, 5840 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5928 (unsigned long long)bio->bi_sector, bio->bi_size, err); 5841 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5929 dip->errors = 1; 5842 dip->errors = 1;
5930 5843
@@ -6817,12 +6730,15 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6817 ei->ordered_data_close = 0; 6730 ei->ordered_data_close = 0;
6818 ei->orphan_meta_reserved = 0; 6731 ei->orphan_meta_reserved = 0;
6819 ei->dummy_inode = 0; 6732 ei->dummy_inode = 0;
6733 ei->in_defrag = 0;
6820 ei->force_compress = BTRFS_COMPRESS_NONE; 6734 ei->force_compress = BTRFS_COMPRESS_NONE;
6821 6735
6736 ei->delayed_node = NULL;
6737
6822 inode = &ei->vfs_inode; 6738 inode = &ei->vfs_inode;
6823 extent_map_tree_init(&ei->extent_tree, GFP_NOFS); 6739 extent_map_tree_init(&ei->extent_tree);
6824 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); 6740 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6825 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); 6741 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
6826 mutex_init(&ei->log_mutex); 6742 mutex_init(&ei->log_mutex);
6827 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 6743 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6828 INIT_LIST_HEAD(&ei->i_orphan); 6744 INIT_LIST_HEAD(&ei->i_orphan);
@@ -6871,8 +6787,8 @@ void btrfs_destroy_inode(struct inode *inode)
6871 6787
6872 spin_lock(&root->orphan_lock); 6788 spin_lock(&root->orphan_lock);
6873 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 6789 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6874 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", 6790 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6875 inode->i_ino); 6791 (unsigned long long)btrfs_ino(inode));
6876 list_del_init(&BTRFS_I(inode)->i_orphan); 6792 list_del_init(&BTRFS_I(inode)->i_orphan);
6877 } 6793 }
6878 spin_unlock(&root->orphan_lock); 6794 spin_unlock(&root->orphan_lock);
@@ -6894,6 +6810,7 @@ void btrfs_destroy_inode(struct inode *inode)
6894 inode_tree_del(inode); 6810 inode_tree_del(inode);
6895 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 6811 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6896free: 6812free:
6813 btrfs_remove_delayed_node(inode);
6897 call_rcu(&inode->i_rcu, btrfs_i_callback); 6814 call_rcu(&inode->i_rcu, btrfs_i_callback);
6898} 6815}
6899 6816
@@ -6902,7 +6819,7 @@ int btrfs_drop_inode(struct inode *inode)
6902 struct btrfs_root *root = BTRFS_I(inode)->root; 6819 struct btrfs_root *root = BTRFS_I(inode)->root;
6903 6820
6904 if (btrfs_root_refs(&root->root_item) == 0 && 6821 if (btrfs_root_refs(&root->root_item) == 0 &&
6905 root != root->fs_info->tree_root) 6822 !is_free_space_inode(root, inode))
6906 return 1; 6823 return 1;
6907 else 6824 else
6908 return generic_drop_inode(inode); 6825 return generic_drop_inode(inode);
@@ -7011,16 +6928,17 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7011 u64 index = 0; 6928 u64 index = 0;
7012 u64 root_objectid; 6929 u64 root_objectid;
7013 int ret; 6930 int ret;
6931 u64 old_ino = btrfs_ino(old_inode);
7014 6932
7015 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 6933 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7016 return -EPERM; 6934 return -EPERM;
7017 6935
7018 /* we only allow rename subvolume link between subvolumes */ 6936 /* we only allow rename subvolume link between subvolumes */
7019 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) 6937 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7020 return -EXDEV; 6938 return -EXDEV;
7021 6939
7022 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || 6940 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7023 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) 6941 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7024 return -ENOTEMPTY; 6942 return -ENOTEMPTY;
7025 6943
7026 if (S_ISDIR(old_inode->i_mode) && new_inode && 6944 if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -7036,7 +6954,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7036 filemap_flush(old_inode->i_mapping); 6954 filemap_flush(old_inode->i_mapping);
7037 6955
7038 /* close the racy window with snapshot create/destroy ioctl */ 6956 /* close the racy window with snapshot create/destroy ioctl */
7039 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 6957 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7040 down_read(&root->fs_info->subvol_sem); 6958 down_read(&root->fs_info->subvol_sem);
7041 /* 6959 /*
7042 * We want to reserve the absolute worst case amount of items. So if 6960 * We want to reserve the absolute worst case amount of items. So if
@@ -7059,15 +6977,15 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7059 if (ret) 6977 if (ret)
7060 goto out_fail; 6978 goto out_fail;
7061 6979
7062 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 6980 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7063 /* force full log commit if subvolume involved. */ 6981 /* force full log commit if subvolume involved. */
7064 root->fs_info->last_trans_log_full_commit = trans->transid; 6982 root->fs_info->last_trans_log_full_commit = trans->transid;
7065 } else { 6983 } else {
7066 ret = btrfs_insert_inode_ref(trans, dest, 6984 ret = btrfs_insert_inode_ref(trans, dest,
7067 new_dentry->d_name.name, 6985 new_dentry->d_name.name,
7068 new_dentry->d_name.len, 6986 new_dentry->d_name.len,
7069 old_inode->i_ino, 6987 old_ino,
7070 new_dir->i_ino, index); 6988 btrfs_ino(new_dir), index);
7071 if (ret) 6989 if (ret)
7072 goto out_fail; 6990 goto out_fail;
7073 /* 6991 /*
@@ -7083,10 +7001,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7083 * make sure the inode gets flushed if it is replacing 7001 * make sure the inode gets flushed if it is replacing
7084 * something. 7002 * something.
7085 */ 7003 */
7086 if (new_inode && new_inode->i_size && 7004 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7087 old_inode && S_ISREG(old_inode->i_mode)) {
7088 btrfs_add_ordered_operation(trans, root, old_inode); 7005 btrfs_add_ordered_operation(trans, root, old_inode);
7089 }
7090 7006
7091 old_dir->i_ctime = old_dir->i_mtime = ctime; 7007 old_dir->i_ctime = old_dir->i_mtime = ctime;
7092 new_dir->i_ctime = new_dir->i_mtime = ctime; 7008 new_dir->i_ctime = new_dir->i_mtime = ctime;
@@ -7095,7 +7011,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7095 if (old_dentry->d_parent != new_dentry->d_parent) 7011 if (old_dentry->d_parent != new_dentry->d_parent)
7096 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 7012 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7097 7013
7098 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 7014 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7099 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; 7015 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7100 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, 7016 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7101 old_dentry->d_name.name, 7017 old_dentry->d_name.name,
@@ -7112,7 +7028,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7112 7028
7113 if (new_inode) { 7029 if (new_inode) {
7114 new_inode->i_ctime = CURRENT_TIME; 7030 new_inode->i_ctime = CURRENT_TIME;
7115 if (unlikely(new_inode->i_ino == 7031 if (unlikely(btrfs_ino(new_inode) ==
7116 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 7032 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7117 root_objectid = BTRFS_I(new_inode)->location.objectid; 7033 root_objectid = BTRFS_I(new_inode)->location.objectid;
7118 ret = btrfs_unlink_subvol(trans, dest, new_dir, 7034 ret = btrfs_unlink_subvol(trans, dest, new_dir,
@@ -7140,7 +7056,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7140 new_dentry->d_name.len, 0, index); 7056 new_dentry->d_name.len, 0, index);
7141 BUG_ON(ret); 7057 BUG_ON(ret);
7142 7058
7143 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { 7059 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7144 struct dentry *parent = dget_parent(new_dentry); 7060 struct dentry *parent = dget_parent(new_dentry);
7145 btrfs_log_new_name(trans, old_inode, old_dir, parent); 7061 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7146 dput(parent); 7062 dput(parent);
@@ -7149,7 +7065,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7149out_fail: 7065out_fail:
7150 btrfs_end_transaction_throttle(trans, root); 7066 btrfs_end_transaction_throttle(trans, root);
7151out_notrans: 7067out_notrans:
7152 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 7068 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7153 up_read(&root->fs_info->subvol_sem); 7069 up_read(&root->fs_info->subvol_sem);
7154 7070
7155 return ret; 7071 return ret;
@@ -7203,58 +7119,6 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7203 return 0; 7119 return 0;
7204} 7120}
7205 7121
7206int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7207 int sync)
7208{
7209 struct btrfs_inode *binode;
7210 struct inode *inode = NULL;
7211
7212 spin_lock(&root->fs_info->delalloc_lock);
7213 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7214 binode = list_entry(root->fs_info->delalloc_inodes.next,
7215 struct btrfs_inode, delalloc_inodes);
7216 inode = igrab(&binode->vfs_inode);
7217 if (inode) {
7218 list_move_tail(&binode->delalloc_inodes,
7219 &root->fs_info->delalloc_inodes);
7220 break;
7221 }
7222
7223 list_del_init(&binode->delalloc_inodes);
7224 cond_resched_lock(&root->fs_info->delalloc_lock);
7225 }
7226 spin_unlock(&root->fs_info->delalloc_lock);
7227
7228 if (inode) {
7229 if (sync) {
7230 filemap_write_and_wait(inode->i_mapping);
7231 /*
7232 * We have to do this because compression doesn't
7233 * actually set PG_writeback until it submits the pages
7234 * for IO, which happens in an async thread, so we could
7235 * race and not actually wait for any writeback pages
7236 * because they've not been submitted yet. Technically
7237 * this could still be the case for the ordered stuff
7238 * since the async thread may not have started to do its
7239 * work yet. If this becomes the case then we need to
7240 * figure out a way to make sure that in writepage we
7241 * wait for any async pages to be submitted before
7242 * returning so that fdatawait does what its supposed to
7243 * do.
7244 */
7245 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7246 } else {
7247 filemap_flush(inode->i_mapping);
7248 }
7249 if (delay_iput)
7250 btrfs_add_delayed_iput(inode);
7251 else
7252 iput(inode);
7253 return 1;
7254 }
7255 return 0;
7256}
7257
7258static int btrfs_symlink(struct inode *dir, struct dentry *dentry, 7122static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7259 const char *symname) 7123 const char *symname)
7260{ 7124{
@@ -7278,9 +7142,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7278 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 7142 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7279 return -ENAMETOOLONG; 7143 return -ENAMETOOLONG;
7280 7144
7281 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7282 if (err)
7283 return err;
7284 /* 7145 /*
7285 * 2 items for inode item and ref 7146 * 2 items for inode item and ref
7286 * 2 items for dir items 7147 * 2 items for dir items
@@ -7290,8 +7151,12 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7290 if (IS_ERR(trans)) 7151 if (IS_ERR(trans))
7291 return PTR_ERR(trans); 7152 return PTR_ERR(trans);
7292 7153
7154 err = btrfs_find_free_ino(root, &objectid);
7155 if (err)
7156 goto out_unlock;
7157
7293 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7158 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7294 dentry->d_name.len, dir->i_ino, objectid, 7159 dentry->d_name.len, btrfs_ino(dir), objectid,
7295 S_IFLNK|S_IRWXUGO, &index); 7160 S_IFLNK|S_IRWXUGO, &index);
7296 if (IS_ERR(inode)) { 7161 if (IS_ERR(inode)) {
7297 err = PTR_ERR(inode); 7162 err = PTR_ERR(inode);
@@ -7319,7 +7184,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7319 7184
7320 path = btrfs_alloc_path(); 7185 path = btrfs_alloc_path();
7321 BUG_ON(!path); 7186 BUG_ON(!path);
7322 key.objectid = inode->i_ino; 7187 key.objectid = btrfs_ino(inode);
7323 key.offset = 0; 7188 key.offset = 0;
7324 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 7189 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7325 datasize = btrfs_file_extent_calc_inline_size(name_len); 7190 datasize = btrfs_file_extent_calc_inline_size(name_len);
@@ -7327,6 +7192,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7327 datasize); 7192 datasize);
7328 if (err) { 7193 if (err) {
7329 drop_inode = 1; 7194 drop_inode = 1;
7195 btrfs_free_path(path);
7330 goto out_unlock; 7196 goto out_unlock;
7331 } 7197 }
7332 leaf = path->nodes[0]; 7198 leaf = path->nodes[0];