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.c700
1 files changed, 282 insertions, 418 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 7cd8ab0ef04d..bb51bb1fa44f 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;
@@ -138,7 +140,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
138 path->leave_spinning = 1; 140 path->leave_spinning = 1;
139 btrfs_set_trans_block_group(trans, inode); 141 btrfs_set_trans_block_group(trans, inode);
140 142
141 key.objectid = inode->i_ino; 143 key.objectid = btrfs_ino(inode);
142 key.offset = start; 144 key.offset = start;
143 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 145 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
144 datasize = btrfs_file_extent_calc_inline_size(cur_size); 146 datasize = btrfs_file_extent_calc_inline_size(cur_size);
@@ -340,6 +342,10 @@ static noinline int compress_file_range(struct inode *inode,
340 int will_compress; 342 int will_compress;
341 int compress_type = root->fs_info->compress_type; 343 int compress_type = root->fs_info->compress_type;
342 344
345 /* if this is a small write inside eof, kick off a defragbot */
346 if (end <= BTRFS_I(inode)->disk_i_size && (end - start + 1) < 16 * 1024)
347 btrfs_add_inode_defrag(NULL, inode);
348
343 actual_end = min_t(u64, isize, end + 1); 349 actual_end = min_t(u64, isize, end + 1);
344again: 350again:
345 will_compress = 0; 351 will_compress = 0;
@@ -649,7 +655,7 @@ retry:
649 async_extent->start + 655 async_extent->start +
650 async_extent->ram_size - 1, 0); 656 async_extent->ram_size - 1, 0);
651 657
652 em = alloc_extent_map(GFP_NOFS); 658 em = alloc_extent_map();
653 BUG_ON(!em); 659 BUG_ON(!em);
654 em->start = async_extent->start; 660 em->start = async_extent->start;
655 em->len = async_extent->ram_size; 661 em->len = async_extent->ram_size;
@@ -745,6 +751,15 @@ static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
745 return alloc_hint; 751 return alloc_hint;
746} 752}
747 753
754static inline bool is_free_space_inode(struct btrfs_root *root,
755 struct inode *inode)
756{
757 if (root == root->fs_info->tree_root ||
758 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
759 return true;
760 return false;
761}
762
748/* 763/*
749 * when extent_io.c finds a delayed allocation range in the file, 764 * when extent_io.c finds a delayed allocation range in the file,
750 * the call backs end up in this code. The basic idea is to 765 * the call backs end up in this code. The basic idea is to
@@ -777,7 +792,7 @@ static noinline int cow_file_range(struct inode *inode,
777 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 792 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
778 int ret = 0; 793 int ret = 0;
779 794
780 BUG_ON(root == root->fs_info->tree_root); 795 BUG_ON(is_free_space_inode(root, inode));
781 trans = btrfs_join_transaction(root, 1); 796 trans = btrfs_join_transaction(root, 1);
782 BUG_ON(IS_ERR(trans)); 797 BUG_ON(IS_ERR(trans));
783 btrfs_set_trans_block_group(trans, inode); 798 btrfs_set_trans_block_group(trans, inode);
@@ -788,6 +803,10 @@ static noinline int cow_file_range(struct inode *inode,
788 disk_num_bytes = num_bytes; 803 disk_num_bytes = num_bytes;
789 ret = 0; 804 ret = 0;
790 805
806 /* if this is a small write inside eof, kick off defrag */
807 if (end <= BTRFS_I(inode)->disk_i_size && num_bytes < 64 * 1024)
808 btrfs_add_inode_defrag(trans, inode);
809
791 if (start == 0) { 810 if (start == 0) {
792 /* lets try to make an inline extent */ 811 /* lets try to make an inline extent */
793 ret = cow_file_range_inline(trans, root, inode, 812 ret = cow_file_range_inline(trans, root, inode,
@@ -826,7 +845,7 @@ static noinline int cow_file_range(struct inode *inode,
826 (u64)-1, &ins, 1); 845 (u64)-1, &ins, 1);
827 BUG_ON(ret); 846 BUG_ON(ret);
828 847
829 em = alloc_extent_map(GFP_NOFS); 848 em = alloc_extent_map();
830 BUG_ON(!em); 849 BUG_ON(!em);
831 em->start = start; 850 em->start = start;
832 em->orig_start = em->start; 851 em->orig_start = em->start;
@@ -1008,7 +1027,7 @@ static noinline int csum_exist_in_range(struct btrfs_root *root,
1008 LIST_HEAD(list); 1027 LIST_HEAD(list);
1009 1028
1010 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, 1029 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1011 bytenr + num_bytes - 1, &list); 1030 bytenr + num_bytes - 1, &list, 0);
1012 if (ret == 0 && list_empty(&list)) 1031 if (ret == 0 && list_empty(&list))
1013 return 0; 1032 return 0;
1014 1033
@@ -1049,29 +1068,31 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1049 int type; 1068 int type;
1050 int nocow; 1069 int nocow;
1051 int check_prev = 1; 1070 int check_prev = 1;
1052 bool nolock = false; 1071 bool nolock;
1072 u64 ino = btrfs_ino(inode);
1053 1073
1054 path = btrfs_alloc_path(); 1074 path = btrfs_alloc_path();
1055 BUG_ON(!path); 1075 BUG_ON(!path);
1056 if (root == root->fs_info->tree_root) { 1076
1057 nolock = true; 1077 nolock = is_free_space_inode(root, inode);
1078
1079 if (nolock)
1058 trans = btrfs_join_transaction_nolock(root, 1); 1080 trans = btrfs_join_transaction_nolock(root, 1);
1059 } else { 1081 else
1060 trans = btrfs_join_transaction(root, 1); 1082 trans = btrfs_join_transaction(root, 1);
1061 }
1062 BUG_ON(IS_ERR(trans)); 1083 BUG_ON(IS_ERR(trans));
1063 1084
1064 cow_start = (u64)-1; 1085 cow_start = (u64)-1;
1065 cur_offset = start; 1086 cur_offset = start;
1066 while (1) { 1087 while (1) {
1067 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 1088 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1068 cur_offset, 0); 1089 cur_offset, 0);
1069 BUG_ON(ret < 0); 1090 BUG_ON(ret < 0);
1070 if (ret > 0 && path->slots[0] > 0 && check_prev) { 1091 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1071 leaf = path->nodes[0]; 1092 leaf = path->nodes[0];
1072 btrfs_item_key_to_cpu(leaf, &found_key, 1093 btrfs_item_key_to_cpu(leaf, &found_key,
1073 path->slots[0] - 1); 1094 path->slots[0] - 1);
1074 if (found_key.objectid == inode->i_ino && 1095 if (found_key.objectid == ino &&
1075 found_key.type == BTRFS_EXTENT_DATA_KEY) 1096 found_key.type == BTRFS_EXTENT_DATA_KEY)
1076 path->slots[0]--; 1097 path->slots[0]--;
1077 } 1098 }
@@ -1092,7 +1113,7 @@ next_slot:
1092 num_bytes = 0; 1113 num_bytes = 0;
1093 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 1114 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1094 1115
1095 if (found_key.objectid > inode->i_ino || 1116 if (found_key.objectid > ino ||
1096 found_key.type > BTRFS_EXTENT_DATA_KEY || 1117 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1097 found_key.offset > end) 1118 found_key.offset > end)
1098 break; 1119 break;
@@ -1127,7 +1148,7 @@ next_slot:
1127 goto out_check; 1148 goto out_check;
1128 if (btrfs_extent_readonly(root, disk_bytenr)) 1149 if (btrfs_extent_readonly(root, disk_bytenr))
1129 goto out_check; 1150 goto out_check;
1130 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 1151 if (btrfs_cross_ref_exist(trans, root, ino,
1131 found_key.offset - 1152 found_key.offset -
1132 extent_offset, disk_bytenr)) 1153 extent_offset, disk_bytenr))
1133 goto out_check; 1154 goto out_check;
@@ -1164,7 +1185,7 @@ out_check:
1164 goto next_slot; 1185 goto next_slot;
1165 } 1186 }
1166 1187
1167 btrfs_release_path(root, path); 1188 btrfs_release_path(path);
1168 if (cow_start != (u64)-1) { 1189 if (cow_start != (u64)-1) {
1169 ret = cow_file_range(inode, locked_page, cow_start, 1190 ret = cow_file_range(inode, locked_page, cow_start,
1170 found_key.offset - 1, page_started, 1191 found_key.offset - 1, page_started,
@@ -1177,7 +1198,7 @@ out_check:
1177 struct extent_map *em; 1198 struct extent_map *em;
1178 struct extent_map_tree *em_tree; 1199 struct extent_map_tree *em_tree;
1179 em_tree = &BTRFS_I(inode)->extent_tree; 1200 em_tree = &BTRFS_I(inode)->extent_tree;
1180 em = alloc_extent_map(GFP_NOFS); 1201 em = alloc_extent_map();
1181 BUG_ON(!em); 1202 BUG_ON(!em);
1182 em->start = cur_offset; 1203 em->start = cur_offset;
1183 em->orig_start = em->start; 1204 em->orig_start = em->start;
@@ -1222,7 +1243,7 @@ out_check:
1222 if (cur_offset > end) 1243 if (cur_offset > end)
1223 break; 1244 break;
1224 } 1245 }
1225 btrfs_release_path(root, path); 1246 btrfs_release_path(path);
1226 1247
1227 if (cur_offset <= end && cow_start == (u64)-1) 1248 if (cur_offset <= end && cow_start == (u64)-1)
1228 cow_start = cur_offset; 1249 cow_start = cur_offset;
@@ -1310,14 +1331,13 @@ static int btrfs_set_bit_hook(struct inode *inode,
1310 1331
1311 /* 1332 /*
1312 * set_bit and clear bit hooks normally require _irqsave/restore 1333 * set_bit and clear bit hooks normally require _irqsave/restore
1313 * but in this case, we are only testeing for the DELALLOC 1334 * but in this case, we are only testing for the DELALLOC
1314 * bit, which is only set or cleared with irqs on 1335 * bit, which is only set or cleared with irqs on
1315 */ 1336 */
1316 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1337 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1317 struct btrfs_root *root = BTRFS_I(inode)->root; 1338 struct btrfs_root *root = BTRFS_I(inode)->root;
1318 u64 len = state->end + 1 - state->start; 1339 u64 len = state->end + 1 - state->start;
1319 int do_list = (root->root_key.objectid != 1340 bool do_list = !is_free_space_inode(root, inode);
1320 BTRFS_ROOT_TREE_OBJECTID);
1321 1341
1322 if (*bits & EXTENT_FIRST_DELALLOC) 1342 if (*bits & EXTENT_FIRST_DELALLOC)
1323 *bits &= ~EXTENT_FIRST_DELALLOC; 1343 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1344,14 +1364,13 @@ static int btrfs_clear_bit_hook(struct inode *inode,
1344{ 1364{
1345 /* 1365 /*
1346 * set_bit and clear bit hooks normally require _irqsave/restore 1366 * set_bit and clear bit hooks normally require _irqsave/restore
1347 * but in this case, we are only testeing for the DELALLOC 1367 * but in this case, we are only testing for the DELALLOC
1348 * bit, which is only set or cleared with irqs on 1368 * bit, which is only set or cleared with irqs on
1349 */ 1369 */
1350 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) { 1370 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1351 struct btrfs_root *root = BTRFS_I(inode)->root; 1371 struct btrfs_root *root = BTRFS_I(inode)->root;
1352 u64 len = state->end + 1 - state->start; 1372 u64 len = state->end + 1 - state->start;
1353 int do_list = (root->root_key.objectid != 1373 bool do_list = !is_free_space_inode(root, inode);
1354 BTRFS_ROOT_TREE_OBJECTID);
1355 1374
1356 if (*bits & EXTENT_FIRST_DELALLOC) 1375 if (*bits & EXTENT_FIRST_DELALLOC)
1357 *bits &= ~EXTENT_FIRST_DELALLOC; 1376 *bits &= ~EXTENT_FIRST_DELALLOC;
@@ -1458,7 +1477,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1458 1477
1459 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 1478 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1460 1479
1461 if (root == root->fs_info->tree_root) 1480 if (is_free_space_inode(root, inode))
1462 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2); 1481 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1463 else 1482 else
1464 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); 1483 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
@@ -1644,7 +1663,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1644 &hint, 0); 1663 &hint, 0);
1645 BUG_ON(ret); 1664 BUG_ON(ret);
1646 1665
1647 ins.objectid = inode->i_ino; 1666 ins.objectid = btrfs_ino(inode);
1648 ins.offset = file_pos; 1667 ins.offset = file_pos;
1649 ins.type = BTRFS_EXTENT_DATA_KEY; 1668 ins.type = BTRFS_EXTENT_DATA_KEY;
1650 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1669 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
@@ -1675,7 +1694,7 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1675 ins.type = BTRFS_EXTENT_ITEM_KEY; 1694 ins.type = BTRFS_EXTENT_ITEM_KEY;
1676 ret = btrfs_alloc_reserved_file_extent(trans, root, 1695 ret = btrfs_alloc_reserved_file_extent(trans, root,
1677 root->root_key.objectid, 1696 root->root_key.objectid,
1678 inode->i_ino, file_pos, &ins); 1697 btrfs_ino(inode), file_pos, &ins);
1679 BUG_ON(ret); 1698 BUG_ON(ret);
1680 btrfs_free_path(path); 1699 btrfs_free_path(path);
1681 1700
@@ -1701,7 +1720,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1701 struct extent_state *cached_state = NULL; 1720 struct extent_state *cached_state = NULL;
1702 int compress_type = 0; 1721 int compress_type = 0;
1703 int ret; 1722 int ret;
1704 bool nolock = false; 1723 bool nolock;
1705 1724
1706 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, 1725 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1707 end - start + 1); 1726 end - start + 1);
@@ -1709,7 +1728,7 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1709 return 0; 1728 return 0;
1710 BUG_ON(!ordered_extent); 1729 BUG_ON(!ordered_extent);
1711 1730
1712 nolock = (root == root->fs_info->tree_root); 1731 nolock = is_free_space_inode(root, inode);
1713 1732
1714 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { 1733 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1715 BUG_ON(!list_empty(&ordered_extent->list)); 1734 BUG_ON(!list_empty(&ordered_extent->list));
@@ -1855,7 +1874,7 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1855 } 1874 }
1856 read_unlock(&em_tree->lock); 1875 read_unlock(&em_tree->lock);
1857 1876
1858 if (!em || IS_ERR(em)) { 1877 if (IS_ERR_OR_NULL(em)) {
1859 kfree(failrec); 1878 kfree(failrec);
1860 return -EIO; 1879 return -EIO;
1861 } 1880 }
@@ -2004,12 +2023,11 @@ good:
2004 return 0; 2023 return 0;
2005 2024
2006zeroit: 2025zeroit:
2007 if (printk_ratelimit()) { 2026 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2008 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u " 2027 "private %llu\n",
2009 "private %llu\n", page->mapping->host->i_ino, 2028 (unsigned long long)btrfs_ino(page->mapping->host),
2010 (unsigned long long)start, csum, 2029 (unsigned long long)start, csum,
2011 (unsigned long long)private); 2030 (unsigned long long)private);
2012 }
2013 memset(kaddr + offset, 1, end - start + 1); 2031 memset(kaddr + offset, 1, end - start + 1);
2014 flush_dcache_page(page); 2032 flush_dcache_page(page);
2015 kunmap_atomic(kaddr, KM_USER0); 2033 kunmap_atomic(kaddr, KM_USER0);
@@ -2244,7 +2262,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2244 2262
2245 /* insert an orphan item to track this unlinked/truncated file */ 2263 /* insert an orphan item to track this unlinked/truncated file */
2246 if (insert >= 1) { 2264 if (insert >= 1) {
2247 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); 2265 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2248 BUG_ON(ret); 2266 BUG_ON(ret);
2249 } 2267 }
2250 2268
@@ -2281,7 +2299,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2281 spin_unlock(&root->orphan_lock); 2299 spin_unlock(&root->orphan_lock);
2282 2300
2283 if (trans && delete_item) { 2301 if (trans && delete_item) {
2284 ret = btrfs_del_orphan_item(trans, root, inode->i_ino); 2302 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2285 BUG_ON(ret); 2303 BUG_ON(ret);
2286 } 2304 }
2287 2305
@@ -2346,7 +2364,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2346 break; 2364 break;
2347 2365
2348 /* release the path since we're done with it */ 2366 /* release the path since we're done with it */
2349 btrfs_release_path(root, path); 2367 btrfs_release_path(path);
2350 2368
2351 /* 2369 /*
2352 * this is where we are basically btrfs_lookup, without the 2370 * this is where we are basically btrfs_lookup, without the
@@ -2543,7 +2561,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
2543 * 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
2544 * any xattrs or acls 2562 * any xattrs or acls
2545 */ 2563 */
2546 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));
2547 if (!maybe_acls) 2566 if (!maybe_acls)
2548 cache_no_acl(inode); 2567 cache_no_acl(inode);
2549 2568
@@ -2647,11 +2666,26 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2647 struct extent_buffer *leaf; 2666 struct extent_buffer *leaf;
2648 int ret; 2667 int ret;
2649 2668
2669 /*
2670 * If root is tree root, it means this inode is used to
2671 * store free space information. And these inodes are updated
2672 * when committing the transaction, so they needn't delaye to
2673 * be updated, or deadlock will occured.
2674 */
2675 if (!is_free_space_inode(root, inode)) {
2676 ret = btrfs_delayed_update_inode(trans, root, inode);
2677 if (!ret)
2678 btrfs_set_inode_last_trans(trans, inode);
2679 return ret;
2680 }
2681
2650 path = btrfs_alloc_path(); 2682 path = btrfs_alloc_path();
2651 BUG_ON(!path); 2683 if (!path)
2684 return -ENOMEM;
2685
2652 path->leave_spinning = 1; 2686 path->leave_spinning = 1;
2653 ret = btrfs_lookup_inode(trans, root, path, 2687 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2654 &BTRFS_I(inode)->location, 1); 2688 1);
2655 if (ret) { 2689 if (ret) {
2656 if (ret > 0) 2690 if (ret > 0)
2657 ret = -ENOENT; 2691 ret = -ENOENT;
@@ -2661,7 +2695,7 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2661 btrfs_unlock_up_safe(path, 1); 2695 btrfs_unlock_up_safe(path, 1);
2662 leaf = path->nodes[0]; 2696 leaf = path->nodes[0];
2663 inode_item = btrfs_item_ptr(leaf, path->slots[0], 2697 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2664 struct btrfs_inode_item); 2698 struct btrfs_inode_item);
2665 2699
2666 fill_inode_item(trans, leaf, inode_item, inode); 2700 fill_inode_item(trans, leaf, inode_item, inode);
2667 btrfs_mark_buffer_dirty(leaf); 2701 btrfs_mark_buffer_dirty(leaf);
@@ -2672,7 +2706,6 @@ failed:
2672 return ret; 2706 return ret;
2673} 2707}
2674 2708
2675
2676/* 2709/*
2677 * unlink helper that gets used here in inode.c and in the tree logging 2710 * unlink helper that gets used here in inode.c and in the tree logging
2678 * recovery code. It remove a link in a directory with a given name, and 2711 * recovery code. It remove a link in a directory with a given name, and
@@ -2689,6 +2722,8 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2689 struct btrfs_dir_item *di; 2722 struct btrfs_dir_item *di;
2690 struct btrfs_key key; 2723 struct btrfs_key key;
2691 u64 index; 2724 u64 index;
2725 u64 ino = btrfs_ino(inode);
2726 u64 dir_ino = btrfs_ino(dir);
2692 2727
2693 path = btrfs_alloc_path(); 2728 path = btrfs_alloc_path();
2694 if (!path) { 2729 if (!path) {
@@ -2697,7 +2732,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2697 } 2732 }
2698 2733
2699 path->leave_spinning = 1; 2734 path->leave_spinning = 1;
2700 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2735 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2701 name, name_len, -1); 2736 name, name_len, -1);
2702 if (IS_ERR(di)) { 2737 if (IS_ERR(di)) {
2703 ret = PTR_ERR(di); 2738 ret = PTR_ERR(di);
@@ -2712,33 +2747,23 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2712 ret = btrfs_delete_one_dir_name(trans, root, path, di); 2747 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2713 if (ret) 2748 if (ret)
2714 goto err; 2749 goto err;
2715 btrfs_release_path(root, path); 2750 btrfs_release_path(path);
2716 2751
2717 ret = btrfs_del_inode_ref(trans, root, name, name_len, 2752 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2718 inode->i_ino, 2753 dir_ino, &index);
2719 dir->i_ino, &index);
2720 if (ret) { 2754 if (ret) {
2721 printk(KERN_INFO "btrfs failed to delete reference to %.*s, " 2755 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2722 "inode %lu parent %lu\n", name_len, name, 2756 "inode %llu parent %llu\n", name_len, name,
2723 inode->i_ino, dir->i_ino); 2757 (unsigned long long)ino, (unsigned long long)dir_ino);
2724 goto err; 2758 goto err;
2725 } 2759 }
2726 2760
2727 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 2761 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2728 index, name, name_len, -1); 2762 if (ret)
2729 if (IS_ERR(di)) {
2730 ret = PTR_ERR(di);
2731 goto err;
2732 }
2733 if (!di) {
2734 ret = -ENOENT;
2735 goto err; 2763 goto err;
2736 }
2737 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2738 btrfs_release_path(root, path);
2739 2764
2740 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, 2765 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2741 inode, dir->i_ino); 2766 inode, dir_ino);
2742 BUG_ON(ret != 0 && ret != -ENOENT); 2767 BUG_ON(ret != 0 && ret != -ENOENT);
2743 2768
2744 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, 2769 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
@@ -2816,12 +2841,14 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2816 int check_link = 1; 2841 int check_link = 1;
2817 int err = -ENOSPC; 2842 int err = -ENOSPC;
2818 int ret; 2843 int ret;
2844 u64 ino = btrfs_ino(inode);
2845 u64 dir_ino = btrfs_ino(dir);
2819 2846
2820 trans = btrfs_start_transaction(root, 10); 2847 trans = btrfs_start_transaction(root, 10);
2821 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC) 2848 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2822 return trans; 2849 return trans;
2823 2850
2824 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 2851 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2825 return ERR_PTR(-ENOSPC); 2852 return ERR_PTR(-ENOSPC);
2826 2853
2827 /* check if there is someone else holds reference */ 2854 /* check if there is someone else holds reference */
@@ -2862,7 +2889,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2862 } else { 2889 } else {
2863 check_link = 0; 2890 check_link = 0;
2864 } 2891 }
2865 btrfs_release_path(root, path); 2892 btrfs_release_path(path);
2866 2893
2867 ret = btrfs_lookup_inode(trans, root, path, 2894 ret = btrfs_lookup_inode(trans, root, path,
2868 &BTRFS_I(inode)->location, 0); 2895 &BTRFS_I(inode)->location, 0);
@@ -2876,11 +2903,11 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2876 } else { 2903 } else {
2877 check_link = 0; 2904 check_link = 0;
2878 } 2905 }
2879 btrfs_release_path(root, path); 2906 btrfs_release_path(path);
2880 2907
2881 if (ret == 0 && S_ISREG(inode->i_mode)) { 2908 if (ret == 0 && S_ISREG(inode->i_mode)) {
2882 ret = btrfs_lookup_file_extent(trans, root, path, 2909 ret = btrfs_lookup_file_extent(trans, root, path,
2883 inode->i_ino, (u64)-1, 0); 2910 ino, (u64)-1, 0);
2884 if (ret < 0) { 2911 if (ret < 0) {
2885 err = ret; 2912 err = ret;
2886 goto out; 2913 goto out;
@@ -2888,7 +2915,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2888 BUG_ON(ret == 0); 2915 BUG_ON(ret == 0);
2889 if (check_path_shared(root, path)) 2916 if (check_path_shared(root, path))
2890 goto out; 2917 goto out;
2891 btrfs_release_path(root, path); 2918 btrfs_release_path(path);
2892 } 2919 }
2893 2920
2894 if (!check_link) { 2921 if (!check_link) {
@@ -2896,7 +2923,7 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2896 goto out; 2923 goto out;
2897 } 2924 }
2898 2925
2899 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 2926 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2900 dentry->d_name.name, dentry->d_name.len, 0); 2927 dentry->d_name.name, dentry->d_name.len, 0);
2901 if (IS_ERR(di)) { 2928 if (IS_ERR(di)) {
2902 err = PTR_ERR(di); 2929 err = PTR_ERR(di);
@@ -2909,11 +2936,11 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2909 err = 0; 2936 err = 0;
2910 goto out; 2937 goto out;
2911 } 2938 }
2912 btrfs_release_path(root, path); 2939 btrfs_release_path(path);
2913 2940
2914 ref = btrfs_lookup_inode_ref(trans, root, path, 2941 ref = btrfs_lookup_inode_ref(trans, root, path,
2915 dentry->d_name.name, dentry->d_name.len, 2942 dentry->d_name.name, dentry->d_name.len,
2916 inode->i_ino, dir->i_ino, 0); 2943 ino, dir_ino, 0);
2917 if (IS_ERR(ref)) { 2944 if (IS_ERR(ref)) {
2918 err = PTR_ERR(ref); 2945 err = PTR_ERR(ref);
2919 goto out; 2946 goto out;
@@ -2922,9 +2949,17 @@ static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2922 if (check_path_shared(root, path)) 2949 if (check_path_shared(root, path))
2923 goto out; 2950 goto out;
2924 index = btrfs_inode_ref_index(path->nodes[0], ref); 2951 index = btrfs_inode_ref_index(path->nodes[0], ref);
2925 btrfs_release_path(root, path); 2952 btrfs_release_path(path);
2926 2953
2927 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index, 2954 /*
2955 * This is a commit root search, if we can lookup inode item and other
2956 * relative items in the commit root, it means the transaction of
2957 * dir/file creation has been committed, and the dir index item that we
2958 * delay to insert has also been inserted into the commit root. So
2959 * we needn't worry about the delayed insertion of the dir index item
2960 * here.
2961 */
2962 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2928 dentry->d_name.name, dentry->d_name.len, 0); 2963 dentry->d_name.name, dentry->d_name.len, 0);
2929 if (IS_ERR(di)) { 2964 if (IS_ERR(di)) {
2930 err = PTR_ERR(di); 2965 err = PTR_ERR(di);
@@ -2999,54 +3034,47 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2999 struct btrfs_key key; 3034 struct btrfs_key key;
3000 u64 index; 3035 u64 index;
3001 int ret; 3036 int ret;
3037 u64 dir_ino = btrfs_ino(dir);
3002 3038
3003 path = btrfs_alloc_path(); 3039 path = btrfs_alloc_path();
3004 if (!path) 3040 if (!path)
3005 return -ENOMEM; 3041 return -ENOMEM;
3006 3042
3007 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 3043 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3008 name, name_len, -1); 3044 name, name_len, -1);
3009 BUG_ON(!di || IS_ERR(di)); 3045 BUG_ON(IS_ERR_OR_NULL(di));
3010 3046
3011 leaf = path->nodes[0]; 3047 leaf = path->nodes[0];
3012 btrfs_dir_item_key_to_cpu(leaf, di, &key); 3048 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3013 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid); 3049 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3014 ret = btrfs_delete_one_dir_name(trans, root, path, di); 3050 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3015 BUG_ON(ret); 3051 BUG_ON(ret);
3016 btrfs_release_path(root, path); 3052 btrfs_release_path(path);
3017 3053
3018 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root, 3054 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3019 objectid, root->root_key.objectid, 3055 objectid, root->root_key.objectid,
3020 dir->i_ino, &index, name, name_len); 3056 dir_ino, &index, name, name_len);
3021 if (ret < 0) { 3057 if (ret < 0) {
3022 BUG_ON(ret != -ENOENT); 3058 BUG_ON(ret != -ENOENT);
3023 di = btrfs_search_dir_index_item(root, path, dir->i_ino, 3059 di = btrfs_search_dir_index_item(root, path, dir_ino,
3024 name, name_len); 3060 name, name_len);
3025 BUG_ON(!di || IS_ERR(di)); 3061 BUG_ON(IS_ERR_OR_NULL(di));
3026 3062
3027 leaf = path->nodes[0]; 3063 leaf = path->nodes[0];
3028 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); 3064 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3029 btrfs_release_path(root, path); 3065 btrfs_release_path(path);
3030 index = key.offset; 3066 index = key.offset;
3031 } 3067 }
3068 btrfs_release_path(path);
3032 3069
3033 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 3070 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); 3071 BUG_ON(ret);
3042 btrfs_release_path(root, path);
3043 3072
3044 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 3073 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3045 dir->i_mtime = dir->i_ctime = CURRENT_TIME; 3074 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3046 ret = btrfs_update_inode(trans, root, dir); 3075 ret = btrfs_update_inode(trans, root, dir);
3047 BUG_ON(ret); 3076 BUG_ON(ret);
3048 3077
3049 btrfs_free_path(path);
3050 return 0; 3078 return 0;
3051} 3079}
3052 3080
@@ -3059,7 +3087,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3059 unsigned long nr = 0; 3087 unsigned long nr = 0;
3060 3088
3061 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE || 3089 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3062 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 3090 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3063 return -ENOTEMPTY; 3091 return -ENOTEMPTY;
3064 3092
3065 trans = __unlink_start_trans(dir, dentry); 3093 trans = __unlink_start_trans(dir, dentry);
@@ -3068,7 +3096,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3068 3096
3069 btrfs_set_trans_block_group(trans, dir); 3097 btrfs_set_trans_block_group(trans, dir);
3070 3098
3071 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3099 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3072 err = btrfs_unlink_subvol(trans, root, dir, 3100 err = btrfs_unlink_subvol(trans, root, dir,
3073 BTRFS_I(inode)->location.objectid, 3101 BTRFS_I(inode)->location.objectid,
3074 dentry->d_name.name, 3102 dentry->d_name.name,
@@ -3093,178 +3121,6 @@ out:
3093 return err; 3121 return err;
3094} 3122}
3095 3123
3096#if 0
3097/*
3098 * when truncating bytes in a file, it is possible to avoid reading
3099 * the leaves that contain only checksum items. This can be the
3100 * majority of the IO required to delete a large file, but it must
3101 * be done carefully.
3102 *
3103 * The keys in the level just above the leaves are checked to make sure
3104 * the lowest key in a given leaf is a csum key, and starts at an offset
3105 * after the new size.
3106 *
3107 * Then the key for the next leaf is checked to make sure it also has
3108 * a checksum item for the same file. If it does, we know our target leaf
3109 * contains only checksum items, and it can be safely freed without reading
3110 * it.
3111 *
3112 * This is just an optimization targeted at large files. It may do
3113 * nothing. It will return 0 unless things went badly.
3114 */
3115static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root,
3117 struct btrfs_path *path,
3118 struct inode *inode, u64 new_size)
3119{
3120 struct btrfs_key key;
3121 int ret;
3122 int nritems;
3123 struct btrfs_key found_key;
3124 struct btrfs_key other_key;
3125 struct btrfs_leaf_ref *ref;
3126 u64 leaf_gen;
3127 u64 leaf_start;
3128
3129 path->lowest_level = 1;
3130 key.objectid = inode->i_ino;
3131 key.type = BTRFS_CSUM_ITEM_KEY;
3132 key.offset = new_size;
3133again:
3134 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3135 if (ret < 0)
3136 goto out;
3137
3138 if (path->nodes[1] == NULL) {
3139 ret = 0;
3140 goto out;
3141 }
3142 ret = 0;
3143 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3144 nritems = btrfs_header_nritems(path->nodes[1]);
3145
3146 if (!nritems)
3147 goto out;
3148
3149 if (path->slots[1] >= nritems)
3150 goto next_node;
3151
3152 /* did we find a key greater than anything we want to delete? */
3153 if (found_key.objectid > inode->i_ino ||
3154 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3155 goto out;
3156
3157 /* we check the next key in the node to make sure the leave contains
3158 * only checksum items. This comparison doesn't work if our
3159 * leaf is the last one in the node
3160 */
3161 if (path->slots[1] + 1 >= nritems) {
3162next_node:
3163 /* search forward from the last key in the node, this
3164 * will bring us into the next node in the tree
3165 */
3166 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3167
3168 /* unlikely, but we inc below, so check to be safe */
3169 if (found_key.offset == (u64)-1)
3170 goto out;
3171
3172 /* search_forward needs a path with locks held, do the
3173 * search again for the original key. It is possible
3174 * this will race with a balance and return a path that
3175 * we could modify, but this drop is just an optimization
3176 * and is allowed to miss some leaves.
3177 */
3178 btrfs_release_path(root, path);
3179 found_key.offset++;
3180
3181 /* setup a max key for search_forward */
3182 other_key.offset = (u64)-1;
3183 other_key.type = key.type;
3184 other_key.objectid = key.objectid;
3185
3186 path->keep_locks = 1;
3187 ret = btrfs_search_forward(root, &found_key, &other_key,
3188 path, 0, 0);
3189 path->keep_locks = 0;
3190 if (ret || found_key.objectid != key.objectid ||
3191 found_key.type != key.type) {
3192 ret = 0;
3193 goto out;
3194 }
3195
3196 key.offset = found_key.offset;
3197 btrfs_release_path(root, path);
3198 cond_resched();
3199 goto again;
3200 }
3201
3202 /* we know there's one more slot after us in the tree,
3203 * read that key so we can verify it is also a checksum item
3204 */
3205 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3206
3207 if (found_key.objectid < inode->i_ino)
3208 goto next_key;
3209
3210 if (found_key.type != key.type || found_key.offset < new_size)
3211 goto next_key;
3212
3213 /*
3214 * if the key for the next leaf isn't a csum key from this objectid,
3215 * we can't be sure there aren't good items inside this leaf.
3216 * Bail out
3217 */
3218 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3219 goto out;
3220
3221 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3222 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3223 /*
3224 * it is safe to delete this leaf, it contains only
3225 * csum items from this inode at an offset >= new_size
3226 */
3227 ret = btrfs_del_leaf(trans, root, path, leaf_start);
3228 BUG_ON(ret);
3229
3230 if (root->ref_cows && leaf_gen < trans->transid) {
3231 ref = btrfs_alloc_leaf_ref(root, 0);
3232 if (ref) {
3233 ref->root_gen = root->root_key.offset;
3234 ref->bytenr = leaf_start;
3235 ref->owner = 0;
3236 ref->generation = leaf_gen;
3237 ref->nritems = 0;
3238
3239 btrfs_sort_leaf_ref(ref);
3240
3241 ret = btrfs_add_leaf_ref(root, ref, 0);
3242 WARN_ON(ret);
3243 btrfs_free_leaf_ref(root, ref);
3244 } else {
3245 WARN_ON(1);
3246 }
3247 }
3248next_key:
3249 btrfs_release_path(root, path);
3250
3251 if (other_key.objectid == inode->i_ino &&
3252 other_key.type == key.type && other_key.offset > key.offset) {
3253 key.offset = other_key.offset;
3254 cond_resched();
3255 goto again;
3256 }
3257 ret = 0;
3258out:
3259 /* fixup any changes we've made to the path */
3260 path->lowest_level = 0;
3261 path->keep_locks = 0;
3262 btrfs_release_path(root, path);
3263 return ret;
3264}
3265
3266#endif
3267
3268/* 3124/*
3269 * this can truncate away extent items, csum items and directory items. 3125 * this can truncate away extent items, csum items and directory items.
3270 * It starts at a high offset and removes keys until it can't find 3126 * It starts at a high offset and removes keys until it can't find
@@ -3300,17 +3156,27 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3300 int encoding; 3156 int encoding;
3301 int ret; 3157 int ret;
3302 int err = 0; 3158 int err = 0;
3159 u64 ino = btrfs_ino(inode);
3303 3160
3304 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY); 3161 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3305 3162
3306 if (root->ref_cows || root == root->fs_info->tree_root) 3163 if (root->ref_cows || root == root->fs_info->tree_root)
3307 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0); 3164 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3308 3165
3166 /*
3167 * This function is also used to drop the items in the log tree before
3168 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3169 * it is used to drop the loged items. So we shouldn't kill the delayed
3170 * items.
3171 */
3172 if (min_type == 0 && root == BTRFS_I(inode)->root)
3173 btrfs_kill_delayed_inode_items(inode);
3174
3309 path = btrfs_alloc_path(); 3175 path = btrfs_alloc_path();
3310 BUG_ON(!path); 3176 BUG_ON(!path);
3311 path->reada = -1; 3177 path->reada = -1;
3312 3178
3313 key.objectid = inode->i_ino; 3179 key.objectid = ino;
3314 key.offset = (u64)-1; 3180 key.offset = (u64)-1;
3315 key.type = (u8)-1; 3181 key.type = (u8)-1;
3316 3182
@@ -3338,7 +3204,7 @@ search_again:
3338 found_type = btrfs_key_type(&found_key); 3204 found_type = btrfs_key_type(&found_key);
3339 encoding = 0; 3205 encoding = 0;
3340 3206
3341 if (found_key.objectid != inode->i_ino) 3207 if (found_key.objectid != ino)
3342 break; 3208 break;
3343 3209
3344 if (found_type < min_type) 3210 if (found_type < min_type)
@@ -3428,7 +3294,6 @@ search_again:
3428 btrfs_file_extent_calc_inline_size(size); 3294 btrfs_file_extent_calc_inline_size(size);
3429 ret = btrfs_truncate_item(trans, root, path, 3295 ret = btrfs_truncate_item(trans, root, path,
3430 size, 1); 3296 size, 1);
3431 BUG_ON(ret);
3432 } else if (root->ref_cows) { 3297 } else if (root->ref_cows) {
3433 inode_sub_bytes(inode, item_end + 1 - 3298 inode_sub_bytes(inode, item_end + 1 -
3434 found_key.offset); 3299 found_key.offset);
@@ -3457,7 +3322,7 @@ delete:
3457 ret = btrfs_free_extent(trans, root, extent_start, 3322 ret = btrfs_free_extent(trans, root, extent_start,
3458 extent_num_bytes, 0, 3323 extent_num_bytes, 0,
3459 btrfs_header_owner(leaf), 3324 btrfs_header_owner(leaf),
3460 inode->i_ino, extent_offset); 3325 ino, extent_offset);
3461 BUG_ON(ret); 3326 BUG_ON(ret);
3462 } 3327 }
3463 3328
@@ -3466,7 +3331,9 @@ delete:
3466 3331
3467 if (path->slots[0] == 0 || 3332 if (path->slots[0] == 0 ||
3468 path->slots[0] != pending_del_slot) { 3333 path->slots[0] != pending_del_slot) {
3469 if (root->ref_cows) { 3334 if (root->ref_cows &&
3335 BTRFS_I(inode)->location.objectid !=
3336 BTRFS_FREE_INO_OBJECTID) {
3470 err = -EAGAIN; 3337 err = -EAGAIN;
3471 goto out; 3338 goto out;
3472 } 3339 }
@@ -3477,7 +3344,7 @@ delete:
3477 BUG_ON(ret); 3344 BUG_ON(ret);
3478 pending_del_nr = 0; 3345 pending_del_nr = 0;
3479 } 3346 }
3480 btrfs_release_path(root, path); 3347 btrfs_release_path(path);
3481 goto search_again; 3348 goto search_again;
3482 } else { 3349 } else {
3483 path->slots[0]--; 3350 path->slots[0]--;
@@ -3635,7 +3502,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3635 while (1) { 3502 while (1) {
3636 em = btrfs_get_extent(inode, NULL, 0, cur_offset, 3503 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3637 block_end - cur_offset, 0); 3504 block_end - cur_offset, 0);
3638 BUG_ON(IS_ERR(em) || !em); 3505 BUG_ON(IS_ERR_OR_NULL(em));
3639 last_byte = min(extent_map_end(em), block_end); 3506 last_byte = min(extent_map_end(em), block_end);
3640 last_byte = (last_byte + mask) & ~mask; 3507 last_byte = (last_byte + mask) & ~mask;
3641 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { 3508 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
@@ -3656,7 +3523,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3656 break; 3523 break;
3657 3524
3658 err = btrfs_insert_file_extent(trans, root, 3525 err = btrfs_insert_file_extent(trans, root,
3659 inode->i_ino, cur_offset, 0, 3526 btrfs_ino(inode), cur_offset, 0,
3660 0, hole_size, 0, hole_size, 3527 0, hole_size, 0, hole_size,
3661 0, 0, 0); 3528 0, 0, 0);
3662 if (err) 3529 if (err)
@@ -3758,7 +3625,7 @@ void btrfs_evict_inode(struct inode *inode)
3758 3625
3759 truncate_inode_pages(&inode->i_data, 0); 3626 truncate_inode_pages(&inode->i_data, 0);
3760 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || 3627 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3761 root == root->fs_info->tree_root)) 3628 is_free_space_inode(root, inode)))
3762 goto no_delete; 3629 goto no_delete;
3763 3630
3764 if (is_bad_inode(inode)) { 3631 if (is_bad_inode(inode)) {
@@ -3811,6 +3678,10 @@ void btrfs_evict_inode(struct inode *inode)
3811 BUG_ON(ret); 3678 BUG_ON(ret);
3812 } 3679 }
3813 3680
3681 if (!(root == root->fs_info->tree_root ||
3682 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3683 btrfs_return_ino(root, btrfs_ino(inode));
3684
3814 nr = trans->blocks_used; 3685 nr = trans->blocks_used;
3815 btrfs_end_transaction(trans, root); 3686 btrfs_end_transaction(trans, root);
3816 btrfs_btree_balance_dirty(root, nr); 3687 btrfs_btree_balance_dirty(root, nr);
@@ -3836,12 +3707,12 @@ static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3836 path = btrfs_alloc_path(); 3707 path = btrfs_alloc_path();
3837 BUG_ON(!path); 3708 BUG_ON(!path);
3838 3709
3839 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, 3710 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3840 namelen, 0); 3711 namelen, 0);
3841 if (IS_ERR(di)) 3712 if (IS_ERR(di))
3842 ret = PTR_ERR(di); 3713 ret = PTR_ERR(di);
3843 3714
3844 if (!di || IS_ERR(di)) 3715 if (IS_ERR_OR_NULL(di))
3845 goto out_err; 3716 goto out_err;
3846 3717
3847 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); 3718 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
@@ -3889,7 +3760,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3889 3760
3890 leaf = path->nodes[0]; 3761 leaf = path->nodes[0];
3891 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); 3762 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3892 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino || 3763 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3893 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len) 3764 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3894 goto out; 3765 goto out;
3895 3766
@@ -3899,7 +3770,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
3899 if (ret) 3770 if (ret)
3900 goto out; 3771 goto out;
3901 3772
3902 btrfs_release_path(root->fs_info->tree_root, path); 3773 btrfs_release_path(path);
3903 3774
3904 new_root = btrfs_read_fs_root_no_name(root->fs_info, location); 3775 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3905 if (IS_ERR(new_root)) { 3776 if (IS_ERR(new_root)) {
@@ -3928,6 +3799,7 @@ static void inode_tree_add(struct inode *inode)
3928 struct btrfs_inode *entry; 3799 struct btrfs_inode *entry;
3929 struct rb_node **p; 3800 struct rb_node **p;
3930 struct rb_node *parent; 3801 struct rb_node *parent;
3802 u64 ino = btrfs_ino(inode);
3931again: 3803again:
3932 p = &root->inode_tree.rb_node; 3804 p = &root->inode_tree.rb_node;
3933 parent = NULL; 3805 parent = NULL;
@@ -3940,9 +3812,9 @@ again:
3940 parent = *p; 3812 parent = *p;
3941 entry = rb_entry(parent, struct btrfs_inode, rb_node); 3813 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3942 3814
3943 if (inode->i_ino < entry->vfs_inode.i_ino) 3815 if (ino < btrfs_ino(&entry->vfs_inode))
3944 p = &parent->rb_left; 3816 p = &parent->rb_left;
3945 else if (inode->i_ino > entry->vfs_inode.i_ino) 3817 else if (ino > btrfs_ino(&entry->vfs_inode))
3946 p = &parent->rb_right; 3818 p = &parent->rb_right;
3947 else { 3819 else {
3948 WARN_ON(!(entry->vfs_inode.i_state & 3820 WARN_ON(!(entry->vfs_inode.i_state &
@@ -4006,9 +3878,9 @@ again:
4006 prev = node; 3878 prev = node;
4007 entry = rb_entry(node, struct btrfs_inode, rb_node); 3879 entry = rb_entry(node, struct btrfs_inode, rb_node);
4008 3880
4009 if (objectid < entry->vfs_inode.i_ino) 3881 if (objectid < btrfs_ino(&entry->vfs_inode))
4010 node = node->rb_left; 3882 node = node->rb_left;
4011 else if (objectid > entry->vfs_inode.i_ino) 3883 else if (objectid > btrfs_ino(&entry->vfs_inode))
4012 node = node->rb_right; 3884 node = node->rb_right;
4013 else 3885 else
4014 break; 3886 break;
@@ -4016,7 +3888,7 @@ again:
4016 if (!node) { 3888 if (!node) {
4017 while (prev) { 3889 while (prev) {
4018 entry = rb_entry(prev, struct btrfs_inode, rb_node); 3890 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4019 if (objectid <= entry->vfs_inode.i_ino) { 3891 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4020 node = prev; 3892 node = prev;
4021 break; 3893 break;
4022 } 3894 }
@@ -4025,7 +3897,7 @@ again:
4025 } 3897 }
4026 while (node) { 3898 while (node) {
4027 entry = rb_entry(node, struct btrfs_inode, rb_node); 3899 entry = rb_entry(node, struct btrfs_inode, rb_node);
4028 objectid = entry->vfs_inode.i_ino + 1; 3900 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4029 inode = igrab(&entry->vfs_inode); 3901 inode = igrab(&entry->vfs_inode);
4030 if (inode) { 3902 if (inode) {
4031 spin_unlock(&root->inode_lock); 3903 spin_unlock(&root->inode_lock);
@@ -4063,7 +3935,7 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
4063static int btrfs_find_actor(struct inode *inode, void *opaque) 3935static int btrfs_find_actor(struct inode *inode, void *opaque)
4064{ 3936{
4065 struct btrfs_iget_args *args = opaque; 3937 struct btrfs_iget_args *args = opaque;
4066 return args->ino == inode->i_ino && 3938 return args->ino == btrfs_ino(inode) &&
4067 args->root == BTRFS_I(inode)->root; 3939 args->root == BTRFS_I(inode)->root;
4068} 3940}
4069 3941
@@ -4208,7 +4080,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4208 return d_splice_alias(inode, dentry); 4080 return d_splice_alias(inode, dentry);
4209} 4081}
4210 4082
4211static unsigned char btrfs_filetype_table[] = { 4083unsigned char btrfs_filetype_table[] = {
4212 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK 4084 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4213}; 4085};
4214 4086
@@ -4222,6 +4094,8 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4222 struct btrfs_key key; 4094 struct btrfs_key key;
4223 struct btrfs_key found_key; 4095 struct btrfs_key found_key;
4224 struct btrfs_path *path; 4096 struct btrfs_path *path;
4097 struct list_head ins_list;
4098 struct list_head del_list;
4225 int ret; 4099 int ret;
4226 struct extent_buffer *leaf; 4100 struct extent_buffer *leaf;
4227 int slot; 4101 int slot;
@@ -4234,6 +4108,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4234 char tmp_name[32]; 4108 char tmp_name[32];
4235 char *name_ptr; 4109 char *name_ptr;
4236 int name_len; 4110 int name_len;
4111 int is_curr = 0; /* filp->f_pos points to the current index? */
4237 4112
4238 /* FIXME, use a real flag for deciding about the key type */ 4113 /* FIXME, use a real flag for deciding about the key type */
4239 if (root->fs_info->tree_root == root) 4114 if (root->fs_info->tree_root == root)
@@ -4241,9 +4116,7 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4241 4116
4242 /* special case for "." */ 4117 /* special case for "." */
4243 if (filp->f_pos == 0) { 4118 if (filp->f_pos == 0) {
4244 over = filldir(dirent, ".", 1, 4119 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4245 1, inode->i_ino,
4246 DT_DIR);
4247 if (over) 4120 if (over)
4248 return 0; 4121 return 0;
4249 filp->f_pos = 1; 4122 filp->f_pos = 1;
@@ -4258,11 +4131,19 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4258 filp->f_pos = 2; 4131 filp->f_pos = 2;
4259 } 4132 }
4260 path = btrfs_alloc_path(); 4133 path = btrfs_alloc_path();
4134 if (!path)
4135 return -ENOMEM;
4261 path->reada = 2; 4136 path->reada = 2;
4262 4137
4138 if (key_type == BTRFS_DIR_INDEX_KEY) {
4139 INIT_LIST_HEAD(&ins_list);
4140 INIT_LIST_HEAD(&del_list);
4141 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4142 }
4143
4263 btrfs_set_key_type(&key, key_type); 4144 btrfs_set_key_type(&key, key_type);
4264 key.offset = filp->f_pos; 4145 key.offset = filp->f_pos;
4265 key.objectid = inode->i_ino; 4146 key.objectid = btrfs_ino(inode);
4266 4147
4267 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4148 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4268 if (ret < 0) 4149 if (ret < 0)
@@ -4289,8 +4170,13 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4289 break; 4170 break;
4290 if (found_key.offset < filp->f_pos) 4171 if (found_key.offset < filp->f_pos)
4291 goto next; 4172 goto next;
4173 if (key_type == BTRFS_DIR_INDEX_KEY &&
4174 btrfs_should_delete_dir_index(&del_list,
4175 found_key.offset))
4176 goto next;
4292 4177
4293 filp->f_pos = found_key.offset; 4178 filp->f_pos = found_key.offset;
4179 is_curr = 1;
4294 4180
4295 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 4181 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4296 di_cur = 0; 4182 di_cur = 0;
@@ -4345,6 +4231,15 @@ next:
4345 path->slots[0]++; 4231 path->slots[0]++;
4346 } 4232 }
4347 4233
4234 if (key_type == BTRFS_DIR_INDEX_KEY) {
4235 if (is_curr)
4236 filp->f_pos++;
4237 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4238 &ins_list);
4239 if (ret)
4240 goto nopos;
4241 }
4242
4348 /* Reached end of directory/root. Bump pos past the last item. */ 4243 /* Reached end of directory/root. Bump pos past the last item. */
4349 if (key_type == BTRFS_DIR_INDEX_KEY) 4244 if (key_type == BTRFS_DIR_INDEX_KEY)
4350 /* 4245 /*
@@ -4357,6 +4252,8 @@ next:
4357nopos: 4252nopos:
4358 ret = 0; 4253 ret = 0;
4359err: 4254err:
4255 if (key_type == BTRFS_DIR_INDEX_KEY)
4256 btrfs_put_delayed_items(&ins_list, &del_list);
4360 btrfs_free_path(path); 4257 btrfs_free_path(path);
4361 return ret; 4258 return ret;
4362} 4259}
@@ -4372,7 +4269,8 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4372 return 0; 4269 return 0;
4373 4270
4374 smp_mb(); 4271 smp_mb();
4375 nolock = (root->fs_info->closing && root == root->fs_info->tree_root); 4272 if (root->fs_info->closing && is_free_space_inode(root, inode))
4273 nolock = true;
4376 4274
4377 if (wbc->sync_mode == WB_SYNC_ALL) { 4275 if (wbc->sync_mode == WB_SYNC_ALL) {
4378 if (nolock) 4276 if (nolock)
@@ -4415,25 +4313,25 @@ void btrfs_dirty_inode(struct inode *inode)
4415 btrfs_end_transaction(trans, root); 4313 btrfs_end_transaction(trans, root);
4416 trans = btrfs_start_transaction(root, 1); 4314 trans = btrfs_start_transaction(root, 1);
4417 if (IS_ERR(trans)) { 4315 if (IS_ERR(trans)) {
4418 if (printk_ratelimit()) { 4316 printk_ratelimited(KERN_ERR "btrfs: fail to "
4419 printk(KERN_ERR "btrfs: fail to " 4317 "dirty inode %llu error %ld\n",
4420 "dirty inode %lu error %ld\n", 4318 (unsigned long long)btrfs_ino(inode),
4421 inode->i_ino, PTR_ERR(trans)); 4319 PTR_ERR(trans));
4422 }
4423 return; 4320 return;
4424 } 4321 }
4425 btrfs_set_trans_block_group(trans, inode); 4322 btrfs_set_trans_block_group(trans, inode);
4426 4323
4427 ret = btrfs_update_inode(trans, root, inode); 4324 ret = btrfs_update_inode(trans, root, inode);
4428 if (ret) { 4325 if (ret) {
4429 if (printk_ratelimit()) { 4326 printk_ratelimited(KERN_ERR "btrfs: fail to "
4430 printk(KERN_ERR "btrfs: fail to " 4327 "dirty inode %llu error %d\n",
4431 "dirty inode %lu error %d\n", 4328 (unsigned long long)btrfs_ino(inode),
4432 inode->i_ino, ret); 4329 ret);
4433 }
4434 } 4330 }
4435 } 4331 }
4436 btrfs_end_transaction(trans, root); 4332 btrfs_end_transaction(trans, root);
4333 if (BTRFS_I(inode)->delayed_node)
4334 btrfs_balance_delayed_items(root);
4437} 4335}
4438 4336
4439/* 4337/*
@@ -4449,7 +4347,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4449 struct extent_buffer *leaf; 4347 struct extent_buffer *leaf;
4450 int ret; 4348 int ret;
4451 4349
4452 key.objectid = inode->i_ino; 4350 key.objectid = btrfs_ino(inode);
4453 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); 4351 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4454 key.offset = (u64)-1; 4352 key.offset = (u64)-1;
4455 4353
@@ -4481,7 +4379,7 @@ static int btrfs_set_inode_index_count(struct inode *inode)
4481 leaf = path->nodes[0]; 4379 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 4380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4483 4381
4484 if (found_key.objectid != inode->i_ino || 4382 if (found_key.objectid != btrfs_ino(inode) ||
4485 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { 4383 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4486 BTRFS_I(inode)->index_cnt = 2; 4384 BTRFS_I(inode)->index_cnt = 2;
4487 goto out; 4385 goto out;
@@ -4502,9 +4400,12 @@ int btrfs_set_inode_index(struct inode *dir, u64 *index)
4502 int ret = 0; 4400 int ret = 0;
4503 4401
4504 if (BTRFS_I(dir)->index_cnt == (u64)-1) { 4402 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4505 ret = btrfs_set_inode_index_count(dir); 4403 ret = btrfs_inode_delayed_dir_index_count(dir);
4506 if (ret) 4404 if (ret) {
4507 return ret; 4405 ret = btrfs_set_inode_index_count(dir);
4406 if (ret)
4407 return ret;
4408 }
4508 } 4409 }
4509 4410
4510 *index = BTRFS_I(dir)->index_cnt; 4411 *index = BTRFS_I(dir)->index_cnt;
@@ -4540,6 +4441,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4540 return ERR_PTR(-ENOMEM); 4441 return ERR_PTR(-ENOMEM);
4541 } 4442 }
4542 4443
4444 /*
4445 * we have to initialize this early, so we can reclaim the inode
4446 * number if we fail afterwards in this function.
4447 */
4448 inode->i_ino = objectid;
4449
4543 if (dir) { 4450 if (dir) {
4544 trace_btrfs_inode_request(dir); 4451 trace_btrfs_inode_request(dir);
4545 4452
@@ -4585,7 +4492,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4585 goto fail; 4492 goto fail;
4586 4493
4587 inode_init_owner(inode, dir, mode); 4494 inode_init_owner(inode, dir, mode);
4588 inode->i_ino = objectid;
4589 inode_set_bytes(inode, 0); 4495 inode_set_bytes(inode, 0);
4590 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 4496 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4591 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], 4497 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
@@ -4649,29 +4555,29 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
4649 int ret = 0; 4555 int ret = 0;
4650 struct btrfs_key key; 4556 struct btrfs_key key;
4651 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 4557 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4558 u64 ino = btrfs_ino(inode);
4559 u64 parent_ino = btrfs_ino(parent_inode);
4652 4560
4653 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4561 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4654 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key)); 4562 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4655 } else { 4563 } else {
4656 key.objectid = inode->i_ino; 4564 key.objectid = ino;
4657 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 4565 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4658 key.offset = 0; 4566 key.offset = 0;
4659 } 4567 }
4660 4568
4661 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 4569 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4662 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, 4570 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4663 key.objectid, root->root_key.objectid, 4571 key.objectid, root->root_key.objectid,
4664 parent_inode->i_ino, 4572 parent_ino, index, name, name_len);
4665 index, name, name_len);
4666 } else if (add_backref) { 4573 } else if (add_backref) {
4667 ret = btrfs_insert_inode_ref(trans, root, 4574 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4668 name, name_len, inode->i_ino, 4575 parent_ino, index);
4669 parent_inode->i_ino, index);
4670 } 4576 }
4671 4577
4672 if (ret == 0) { 4578 if (ret == 0) {
4673 ret = btrfs_insert_dir_item(trans, root, name, name_len, 4579 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4674 parent_inode->i_ino, &key, 4580 parent_inode, &key,
4675 btrfs_inode_type(inode), index); 4581 btrfs_inode_type(inode), index);
4676 BUG_ON(ret); 4582 BUG_ON(ret);
4677 4583
@@ -4714,10 +4620,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4714 if (!new_valid_dev(rdev)) 4620 if (!new_valid_dev(rdev))
4715 return -EINVAL; 4621 return -EINVAL;
4716 4622
4717 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4718 if (err)
4719 return err;
4720
4721 /* 4623 /*
4722 * 2 for inode item and ref 4624 * 2 for inode item and ref
4723 * 2 for dir items 4625 * 2 for dir items
@@ -4729,8 +4631,12 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4729 4631
4730 btrfs_set_trans_block_group(trans, dir); 4632 btrfs_set_trans_block_group(trans, dir);
4731 4633
4634 err = btrfs_find_free_ino(root, &objectid);
4635 if (err)
4636 goto out_unlock;
4637
4732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4733 dentry->d_name.len, dir->i_ino, objectid, 4639 dentry->d_name.len, btrfs_ino(dir), objectid,
4734 BTRFS_I(dir)->block_group, mode, &index); 4640 BTRFS_I(dir)->block_group, mode, &index);
4735 if (IS_ERR(inode)) { 4641 if (IS_ERR(inode)) {
4736 err = PTR_ERR(inode); 4642 err = PTR_ERR(inode);
@@ -4777,9 +4683,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4777 u64 objectid; 4683 u64 objectid;
4778 u64 index = 0; 4684 u64 index = 0;
4779 4685
4780 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4781 if (err)
4782 return err;
4783 /* 4686 /*
4784 * 2 for inode item and ref 4687 * 2 for inode item and ref
4785 * 2 for dir items 4688 * 2 for dir items
@@ -4791,8 +4694,12 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4791 4694
4792 btrfs_set_trans_block_group(trans, dir); 4695 btrfs_set_trans_block_group(trans, dir);
4793 4696
4697 err = btrfs_find_free_ino(root, &objectid);
4698 if (err)
4699 goto out_unlock;
4700
4794 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4701 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4795 dentry->d_name.len, dir->i_ino, objectid, 4702 dentry->d_name.len, btrfs_ino(dir), objectid,
4796 BTRFS_I(dir)->block_group, mode, &index); 4703 BTRFS_I(dir)->block_group, mode, &index);
4797 if (IS_ERR(inode)) { 4704 if (IS_ERR(inode)) {
4798 err = PTR_ERR(inode); 4705 err = PTR_ERR(inode);
@@ -4903,10 +4810,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4903 u64 index = 0; 4810 u64 index = 0;
4904 unsigned long nr = 1; 4811 unsigned long nr = 1;
4905 4812
4906 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4907 if (err)
4908 return err;
4909
4910 /* 4813 /*
4911 * 2 items for inode and ref 4814 * 2 items for inode and ref
4912 * 2 items for dir items 4815 * 2 items for dir items
@@ -4917,8 +4820,12 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4917 return PTR_ERR(trans); 4820 return PTR_ERR(trans);
4918 btrfs_set_trans_block_group(trans, dir); 4821 btrfs_set_trans_block_group(trans, dir);
4919 4822
4823 err = btrfs_find_free_ino(root, &objectid);
4824 if (err)
4825 goto out_fail;
4826
4920 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4827 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4921 dentry->d_name.len, dir->i_ino, objectid, 4828 dentry->d_name.len, btrfs_ino(dir), objectid,
4922 BTRFS_I(dir)->block_group, S_IFDIR | mode, 4829 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4923 &index); 4830 &index);
4924 if (IS_ERR(inode)) { 4831 if (IS_ERR(inode)) {
@@ -5041,7 +4948,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5041 u64 bytenr; 4948 u64 bytenr;
5042 u64 extent_start = 0; 4949 u64 extent_start = 0;
5043 u64 extent_end = 0; 4950 u64 extent_end = 0;
5044 u64 objectid = inode->i_ino; 4951 u64 objectid = btrfs_ino(inode);
5045 u32 found_type; 4952 u32 found_type;
5046 struct btrfs_path *path = NULL; 4953 struct btrfs_path *path = NULL;
5047 struct btrfs_root *root = BTRFS_I(inode)->root; 4954 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -5069,7 +4976,7 @@ again:
5069 else 4976 else
5070 goto out; 4977 goto out;
5071 } 4978 }
5072 em = alloc_extent_map(GFP_NOFS); 4979 em = alloc_extent_map();
5073 if (!em) { 4980 if (!em) {
5074 err = -ENOMEM; 4981 err = -ENOMEM;
5075 goto out; 4982 goto out;
@@ -5223,7 +5130,7 @@ again:
5223 kunmap(page); 5130 kunmap(page);
5224 free_extent_map(em); 5131 free_extent_map(em);
5225 em = NULL; 5132 em = NULL;
5226 btrfs_release_path(root, path); 5133 btrfs_release_path(path);
5227 trans = btrfs_join_transaction(root, 1); 5134 trans = btrfs_join_transaction(root, 1);
5228 if (IS_ERR(trans)) 5135 if (IS_ERR(trans))
5229 return ERR_CAST(trans); 5136 return ERR_CAST(trans);
@@ -5249,7 +5156,7 @@ not_found_em:
5249 em->block_start = EXTENT_MAP_HOLE; 5156 em->block_start = EXTENT_MAP_HOLE;
5250 set_bit(EXTENT_FLAG_VACANCY, &em->flags); 5157 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5251insert: 5158insert:
5252 btrfs_release_path(root, path); 5159 btrfs_release_path(path);
5253 if (em->start > start || extent_map_end(em) <= start) { 5160 if (em->start > start || extent_map_end(em) <= start) {
5254 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed " 5161 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5255 "[%llu %llu]\n", (unsigned long long)em->start, 5162 "[%llu %llu]\n", (unsigned long long)em->start,
@@ -5382,7 +5289,7 @@ struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *pag
5382 u64 hole_start = start; 5289 u64 hole_start = start;
5383 u64 hole_len = len; 5290 u64 hole_len = len;
5384 5291
5385 em = alloc_extent_map(GFP_NOFS); 5292 em = alloc_extent_map();
5386 if (!em) { 5293 if (!em) {
5387 err = -ENOMEM; 5294 err = -ENOMEM;
5388 goto out; 5295 goto out;
@@ -5472,6 +5379,9 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5472 if (IS_ERR(trans)) 5379 if (IS_ERR(trans))
5473 return ERR_CAST(trans); 5380 return ERR_CAST(trans);
5474 5381
5382 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5383 btrfs_add_inode_defrag(trans, inode);
5384
5475 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 5385 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5476 5386
5477 alloc_hint = get_extent_allocation_hint(inode, start, len); 5387 alloc_hint = get_extent_allocation_hint(inode, start, len);
@@ -5483,7 +5393,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5483 } 5393 }
5484 5394
5485 if (!em) { 5395 if (!em) {
5486 em = alloc_extent_map(GFP_NOFS); 5396 em = alloc_extent_map();
5487 if (!em) { 5397 if (!em) {
5488 em = ERR_PTR(-ENOMEM); 5398 em = ERR_PTR(-ENOMEM);
5489 goto out; 5399 goto out;
@@ -5549,7 +5459,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5549 if (!path) 5459 if (!path)
5550 return -ENOMEM; 5460 return -ENOMEM;
5551 5461
5552 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, 5462 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5553 offset, 0); 5463 offset, 0);
5554 if (ret < 0) 5464 if (ret < 0)
5555 goto out; 5465 goto out;
@@ -5566,7 +5476,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5566 ret = 0; 5476 ret = 0;
5567 leaf = path->nodes[0]; 5477 leaf = path->nodes[0];
5568 btrfs_item_key_to_cpu(leaf, &key, slot); 5478 btrfs_item_key_to_cpu(leaf, &key, slot);
5569 if (key.objectid != inode->i_ino || 5479 if (key.objectid != btrfs_ino(inode) ||
5570 key.type != BTRFS_EXTENT_DATA_KEY) { 5480 key.type != BTRFS_EXTENT_DATA_KEY) {
5571 /* not our file or wrong item type, must cow */ 5481 /* not our file or wrong item type, must cow */
5572 goto out; 5482 goto out;
@@ -5600,7 +5510,7 @@ static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5600 * look for other files referencing this extent, if we 5510 * look for other files referencing this extent, if we
5601 * find any we must cow 5511 * find any we must cow
5602 */ 5512 */
5603 if (btrfs_cross_ref_exist(trans, root, inode->i_ino, 5513 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5604 key.offset - backref_offset, disk_bytenr)) 5514 key.offset - backref_offset, disk_bytenr))
5605 goto out; 5515 goto out;
5606 5516
@@ -5790,9 +5700,10 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
5790 5700
5791 flush_dcache_page(bvec->bv_page); 5701 flush_dcache_page(bvec->bv_page);
5792 if (csum != *private) { 5702 if (csum != *private) {
5793 printk(KERN_ERR "btrfs csum failed ino %lu off" 5703 printk(KERN_ERR "btrfs csum failed ino %llu off"
5794 " %llu csum %u private %u\n", 5704 " %llu csum %u private %u\n",
5795 inode->i_ino, (unsigned long long)start, 5705 (unsigned long long)btrfs_ino(inode),
5706 (unsigned long long)start,
5796 csum, *private); 5707 csum, *private);
5797 err = -EIO; 5708 err = -EIO;
5798 } 5709 }
@@ -5939,9 +5850,9 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
5939 struct btrfs_dio_private *dip = bio->bi_private; 5850 struct btrfs_dio_private *dip = bio->bi_private;
5940 5851
5941 if (err) { 5852 if (err) {
5942 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu " 5853 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
5943 "sector %#Lx len %u err no %d\n", 5854 "sector %#Lx len %u err no %d\n",
5944 dip->inode->i_ino, bio->bi_rw, 5855 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
5945 (unsigned long long)bio->bi_sector, bio->bi_size, err); 5856 (unsigned long long)bio->bi_sector, bio->bi_size, err);
5946 dip->errors = 1; 5857 dip->errors = 1;
5947 5858
@@ -6782,12 +6693,15 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6782 ei->ordered_data_close = 0; 6693 ei->ordered_data_close = 0;
6783 ei->orphan_meta_reserved = 0; 6694 ei->orphan_meta_reserved = 0;
6784 ei->dummy_inode = 0; 6695 ei->dummy_inode = 0;
6696 ei->in_defrag = 0;
6785 ei->force_compress = BTRFS_COMPRESS_NONE; 6697 ei->force_compress = BTRFS_COMPRESS_NONE;
6786 6698
6699 ei->delayed_node = NULL;
6700
6787 inode = &ei->vfs_inode; 6701 inode = &ei->vfs_inode;
6788 extent_map_tree_init(&ei->extent_tree, GFP_NOFS); 6702 extent_map_tree_init(&ei->extent_tree);
6789 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS); 6703 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6790 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS); 6704 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
6791 mutex_init(&ei->log_mutex); 6705 mutex_init(&ei->log_mutex);
6792 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 6706 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6793 INIT_LIST_HEAD(&ei->i_orphan); 6707 INIT_LIST_HEAD(&ei->i_orphan);
@@ -6851,8 +6765,8 @@ void btrfs_destroy_inode(struct inode *inode)
6851 6765
6852 spin_lock(&root->orphan_lock); 6766 spin_lock(&root->orphan_lock);
6853 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 6767 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6854 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n", 6768 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6855 inode->i_ino); 6769 (unsigned long long)btrfs_ino(inode));
6856 list_del_init(&BTRFS_I(inode)->i_orphan); 6770 list_del_init(&BTRFS_I(inode)->i_orphan);
6857 } 6771 }
6858 spin_unlock(&root->orphan_lock); 6772 spin_unlock(&root->orphan_lock);
@@ -6874,6 +6788,7 @@ void btrfs_destroy_inode(struct inode *inode)
6874 inode_tree_del(inode); 6788 inode_tree_del(inode);
6875 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0); 6789 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6876free: 6790free:
6791 btrfs_remove_delayed_node(inode);
6877 call_rcu(&inode->i_rcu, btrfs_i_callback); 6792 call_rcu(&inode->i_rcu, btrfs_i_callback);
6878} 6793}
6879 6794
@@ -6882,7 +6797,7 @@ int btrfs_drop_inode(struct inode *inode)
6882 struct btrfs_root *root = BTRFS_I(inode)->root; 6797 struct btrfs_root *root = BTRFS_I(inode)->root;
6883 6798
6884 if (btrfs_root_refs(&root->root_item) == 0 && 6799 if (btrfs_root_refs(&root->root_item) == 0 &&
6885 root != root->fs_info->tree_root) 6800 !is_free_space_inode(root, inode))
6886 return 1; 6801 return 1;
6887 else 6802 else
6888 return generic_drop_inode(inode); 6803 return generic_drop_inode(inode);
@@ -6991,16 +6906,17 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6991 u64 index = 0; 6906 u64 index = 0;
6992 u64 root_objectid; 6907 u64 root_objectid;
6993 int ret; 6908 int ret;
6909 u64 old_ino = btrfs_ino(old_inode);
6994 6910
6995 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) 6911 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6996 return -EPERM; 6912 return -EPERM;
6997 6913
6998 /* we only allow rename subvolume link between subvolumes */ 6914 /* we only allow rename subvolume link between subvolumes */
6999 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest) 6915 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7000 return -EXDEV; 6916 return -EXDEV;
7001 6917
7002 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID || 6918 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7003 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) 6919 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7004 return -ENOTEMPTY; 6920 return -ENOTEMPTY;
7005 6921
7006 if (S_ISDIR(old_inode->i_mode) && new_inode && 6922 if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -7016,7 +6932,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7016 filemap_flush(old_inode->i_mapping); 6932 filemap_flush(old_inode->i_mapping);
7017 6933
7018 /* close the racy window with snapshot create/destroy ioctl */ 6934 /* close the racy window with snapshot create/destroy ioctl */
7019 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 6935 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7020 down_read(&root->fs_info->subvol_sem); 6936 down_read(&root->fs_info->subvol_sem);
7021 /* 6937 /*
7022 * We want to reserve the absolute worst case amount of items. So if 6938 * We want to reserve the absolute worst case amount of items. So if
@@ -7041,15 +6957,15 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7041 if (ret) 6957 if (ret)
7042 goto out_fail; 6958 goto out_fail;
7043 6959
7044 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 6960 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7045 /* force full log commit if subvolume involved. */ 6961 /* force full log commit if subvolume involved. */
7046 root->fs_info->last_trans_log_full_commit = trans->transid; 6962 root->fs_info->last_trans_log_full_commit = trans->transid;
7047 } else { 6963 } else {
7048 ret = btrfs_insert_inode_ref(trans, dest, 6964 ret = btrfs_insert_inode_ref(trans, dest,
7049 new_dentry->d_name.name, 6965 new_dentry->d_name.name,
7050 new_dentry->d_name.len, 6966 new_dentry->d_name.len,
7051 old_inode->i_ino, 6967 old_ino,
7052 new_dir->i_ino, index); 6968 btrfs_ino(new_dir), index);
7053 if (ret) 6969 if (ret)
7054 goto out_fail; 6970 goto out_fail;
7055 /* 6971 /*
@@ -7065,10 +6981,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7065 * make sure the inode gets flushed if it is replacing 6981 * make sure the inode gets flushed if it is replacing
7066 * something. 6982 * something.
7067 */ 6983 */
7068 if (new_inode && new_inode->i_size && 6984 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7069 old_inode && S_ISREG(old_inode->i_mode)) {
7070 btrfs_add_ordered_operation(trans, root, old_inode); 6985 btrfs_add_ordered_operation(trans, root, old_inode);
7071 }
7072 6986
7073 old_dir->i_ctime = old_dir->i_mtime = ctime; 6987 old_dir->i_ctime = old_dir->i_mtime = ctime;
7074 new_dir->i_ctime = new_dir->i_mtime = ctime; 6988 new_dir->i_ctime = new_dir->i_mtime = ctime;
@@ -7077,7 +6991,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7077 if (old_dentry->d_parent != new_dentry->d_parent) 6991 if (old_dentry->d_parent != new_dentry->d_parent)
7078 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1); 6992 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7079 6993
7080 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) { 6994 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7081 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid; 6995 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7082 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid, 6996 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7083 old_dentry->d_name.name, 6997 old_dentry->d_name.name,
@@ -7094,7 +7008,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7094 7008
7095 if (new_inode) { 7009 if (new_inode) {
7096 new_inode->i_ctime = CURRENT_TIME; 7010 new_inode->i_ctime = CURRENT_TIME;
7097 if (unlikely(new_inode->i_ino == 7011 if (unlikely(btrfs_ino(new_inode) ==
7098 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 7012 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7099 root_objectid = BTRFS_I(new_inode)->location.objectid; 7013 root_objectid = BTRFS_I(new_inode)->location.objectid;
7100 ret = btrfs_unlink_subvol(trans, dest, new_dir, 7014 ret = btrfs_unlink_subvol(trans, dest, new_dir,
@@ -7122,7 +7036,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7122 new_dentry->d_name.len, 0, index); 7036 new_dentry->d_name.len, 0, index);
7123 BUG_ON(ret); 7037 BUG_ON(ret);
7124 7038
7125 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { 7039 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7126 struct dentry *parent = dget_parent(new_dentry); 7040 struct dentry *parent = dget_parent(new_dentry);
7127 btrfs_log_new_name(trans, old_inode, old_dir, parent); 7041 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7128 dput(parent); 7042 dput(parent);
@@ -7131,7 +7045,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7131out_fail: 7045out_fail:
7132 btrfs_end_transaction_throttle(trans, root); 7046 btrfs_end_transaction_throttle(trans, root);
7133out_notrans: 7047out_notrans:
7134 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 7048 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7135 up_read(&root->fs_info->subvol_sem); 7049 up_read(&root->fs_info->subvol_sem);
7136 7050
7137 return ret; 7051 return ret;
@@ -7185,58 +7099,6 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7185 return 0; 7099 return 0;
7186} 7100}
7187 7101
7188int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7189 int sync)
7190{
7191 struct btrfs_inode *binode;
7192 struct inode *inode = NULL;
7193
7194 spin_lock(&root->fs_info->delalloc_lock);
7195 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7196 binode = list_entry(root->fs_info->delalloc_inodes.next,
7197 struct btrfs_inode, delalloc_inodes);
7198 inode = igrab(&binode->vfs_inode);
7199 if (inode) {
7200 list_move_tail(&binode->delalloc_inodes,
7201 &root->fs_info->delalloc_inodes);
7202 break;
7203 }
7204
7205 list_del_init(&binode->delalloc_inodes);
7206 cond_resched_lock(&root->fs_info->delalloc_lock);
7207 }
7208 spin_unlock(&root->fs_info->delalloc_lock);
7209
7210 if (inode) {
7211 if (sync) {
7212 filemap_write_and_wait(inode->i_mapping);
7213 /*
7214 * We have to do this because compression doesn't
7215 * actually set PG_writeback until it submits the pages
7216 * for IO, which happens in an async thread, so we could
7217 * race and not actually wait for any writeback pages
7218 * because they've not been submitted yet. Technically
7219 * this could still be the case for the ordered stuff
7220 * since the async thread may not have started to do its
7221 * work yet. If this becomes the case then we need to
7222 * figure out a way to make sure that in writepage we
7223 * wait for any async pages to be submitted before
7224 * returning so that fdatawait does what its supposed to
7225 * do.
7226 */
7227 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7228 } else {
7229 filemap_flush(inode->i_mapping);
7230 }
7231 if (delay_iput)
7232 btrfs_add_delayed_iput(inode);
7233 else
7234 iput(inode);
7235 return 1;
7236 }
7237 return 0;
7238}
7239
7240static int btrfs_symlink(struct inode *dir, struct dentry *dentry, 7102static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7241 const char *symname) 7103 const char *symname)
7242{ 7104{
@@ -7260,9 +7122,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7260 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 7122 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7261 return -ENAMETOOLONG; 7123 return -ENAMETOOLONG;
7262 7124
7263 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7264 if (err)
7265 return err;
7266 /* 7125 /*
7267 * 2 items for inode item and ref 7126 * 2 items for inode item and ref
7268 * 2 items for dir items 7127 * 2 items for dir items
@@ -7274,8 +7133,12 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7274 7133
7275 btrfs_set_trans_block_group(trans, dir); 7134 btrfs_set_trans_block_group(trans, dir);
7276 7135
7136 err = btrfs_find_free_ino(root, &objectid);
7137 if (err)
7138 goto out_unlock;
7139
7277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7140 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7278 dentry->d_name.len, dir->i_ino, objectid, 7141 dentry->d_name.len, btrfs_ino(dir), objectid,
7279 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, 7142 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7280 &index); 7143 &index);
7281 if (IS_ERR(inode)) { 7144 if (IS_ERR(inode)) {
@@ -7307,7 +7170,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7307 7170
7308 path = btrfs_alloc_path(); 7171 path = btrfs_alloc_path();
7309 BUG_ON(!path); 7172 BUG_ON(!path);
7310 key.objectid = inode->i_ino; 7173 key.objectid = btrfs_ino(inode);
7311 key.offset = 0; 7174 key.offset = 0;
7312 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 7175 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7313 datasize = btrfs_file_extent_calc_inline_size(name_len); 7176 datasize = btrfs_file_extent_calc_inline_size(name_len);
@@ -7315,6 +7178,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7315 datasize); 7178 datasize);
7316 if (err) { 7179 if (err) {
7317 drop_inode = 1; 7180 drop_inode = 1;
7181 btrfs_free_path(path);
7318 goto out_unlock; 7182 goto out_unlock;
7319 } 7183 }
7320 leaf = path->nodes[0]; 7184 leaf = path->nodes[0];