aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/ctree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/ctree.c')
-rw-r--r--fs/btrfs/ctree.c277
1 files changed, 244 insertions, 33 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 9e46c0776816..35443cc4b9a9 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -54,6 +54,31 @@ struct btrfs_path *btrfs_alloc_path(void)
54 return path; 54 return path;
55} 55}
56 56
57/*
58 * set all locked nodes in the path to blocking locks. This should
59 * be done before scheduling
60 */
61noinline void btrfs_set_path_blocking(struct btrfs_path *p)
62{
63 int i;
64 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
65 if (p->nodes[i] && p->locks[i])
66 btrfs_set_lock_blocking(p->nodes[i]);
67 }
68}
69
70/*
71 * reset all the locked nodes in the patch to spinning locks.
72 */
73noinline void btrfs_clear_path_blocking(struct btrfs_path *p)
74{
75 int i;
76 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
77 if (p->nodes[i] && p->locks[i])
78 btrfs_clear_lock_blocking(p->nodes[i]);
79 }
80}
81
57/* this also releases the path */ 82/* this also releases the path */
58void btrfs_free_path(struct btrfs_path *p) 83void btrfs_free_path(struct btrfs_path *p)
59{ 84{
@@ -272,6 +297,8 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
272 if (IS_ERR(cow)) 297 if (IS_ERR(cow))
273 return PTR_ERR(cow); 298 return PTR_ERR(cow);
274 299
300 /* cow is set to blocking by btrfs_init_new_buffer */
301
275 copy_extent_buffer(cow, buf, 0, 0, cow->len); 302 copy_extent_buffer(cow, buf, 0, 0, cow->len);
276 btrfs_set_header_bytenr(cow, cow->start); 303 btrfs_set_header_bytenr(cow, cow->start);
277 btrfs_set_header_generation(cow, trans->transid); 304 btrfs_set_header_generation(cow, trans->transid);
@@ -388,17 +415,20 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
388 WARN_ON(1); 415 WARN_ON(1);
389 } 416 }
390 417
391 spin_lock(&root->fs_info->hash_lock);
392 if (btrfs_header_generation(buf) == trans->transid && 418 if (btrfs_header_generation(buf) == trans->transid &&
393 btrfs_header_owner(buf) == root->root_key.objectid && 419 btrfs_header_owner(buf) == root->root_key.objectid &&
394 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 420 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
395 *cow_ret = buf; 421 *cow_ret = buf;
396 spin_unlock(&root->fs_info->hash_lock);
397 WARN_ON(prealloc_dest); 422 WARN_ON(prealloc_dest);
398 return 0; 423 return 0;
399 } 424 }
400 spin_unlock(&root->fs_info->hash_lock); 425
401 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1); 426 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
427
428 if (parent)
429 btrfs_set_lock_blocking(parent);
430 btrfs_set_lock_blocking(buf);
431
402 ret = __btrfs_cow_block(trans, root, buf, parent, 432 ret = __btrfs_cow_block(trans, root, buf, parent,
403 parent_slot, cow_ret, search_start, 0, 433 parent_slot, cow_ret, search_start, 0,
404 prealloc_dest); 434 prealloc_dest);
@@ -504,6 +534,8 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
504 if (parent_nritems == 1) 534 if (parent_nritems == 1)
505 return 0; 535 return 0;
506 536
537 btrfs_set_lock_blocking(parent);
538
507 for (i = start_slot; i < end_slot; i++) { 539 for (i = start_slot; i < end_slot; i++) {
508 int close = 1; 540 int close = 1;
509 541
@@ -564,6 +596,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
564 search_start = last_block; 596 search_start = last_block;
565 597
566 btrfs_tree_lock(cur); 598 btrfs_tree_lock(cur);
599 btrfs_set_lock_blocking(cur);
567 err = __btrfs_cow_block(trans, root, cur, parent, i, 600 err = __btrfs_cow_block(trans, root, cur, parent, i,
568 &cur, search_start, 601 &cur, search_start,
569 min(16 * blocksize, 602 min(16 * blocksize,
@@ -862,6 +895,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
862 return 0; 895 return 0;
863 896
864 mid = path->nodes[level]; 897 mid = path->nodes[level];
898
865 WARN_ON(!path->locks[level]); 899 WARN_ON(!path->locks[level]);
866 WARN_ON(btrfs_header_generation(mid) != trans->transid); 900 WARN_ON(btrfs_header_generation(mid) != trans->transid);
867 901
@@ -884,6 +918,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
884 /* promote the child to a root */ 918 /* promote the child to a root */
885 child = read_node_slot(root, mid, 0); 919 child = read_node_slot(root, mid, 0);
886 btrfs_tree_lock(child); 920 btrfs_tree_lock(child);
921 btrfs_set_lock_blocking(child);
887 BUG_ON(!child); 922 BUG_ON(!child);
888 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0); 923 ret = btrfs_cow_block(trans, root, child, mid, 0, &child, 0);
889 BUG_ON(ret); 924 BUG_ON(ret);
@@ -900,6 +935,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
900 935
901 add_root_to_dirty_list(root); 936 add_root_to_dirty_list(root);
902 btrfs_tree_unlock(child); 937 btrfs_tree_unlock(child);
938
903 path->locks[level] = 0; 939 path->locks[level] = 0;
904 path->nodes[level] = NULL; 940 path->nodes[level] = NULL;
905 clean_tree_block(trans, root, mid); 941 clean_tree_block(trans, root, mid);
@@ -924,6 +960,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
924 left = read_node_slot(root, parent, pslot - 1); 960 left = read_node_slot(root, parent, pslot - 1);
925 if (left) { 961 if (left) {
926 btrfs_tree_lock(left); 962 btrfs_tree_lock(left);
963 btrfs_set_lock_blocking(left);
927 wret = btrfs_cow_block(trans, root, left, 964 wret = btrfs_cow_block(trans, root, left,
928 parent, pslot - 1, &left, 0); 965 parent, pslot - 1, &left, 0);
929 if (wret) { 966 if (wret) {
@@ -934,6 +971,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
934 right = read_node_slot(root, parent, pslot + 1); 971 right = read_node_slot(root, parent, pslot + 1);
935 if (right) { 972 if (right) {
936 btrfs_tree_lock(right); 973 btrfs_tree_lock(right);
974 btrfs_set_lock_blocking(right);
937 wret = btrfs_cow_block(trans, root, right, 975 wret = btrfs_cow_block(trans, root, right,
938 parent, pslot + 1, &right, 0); 976 parent, pslot + 1, &right, 0);
939 if (wret) { 977 if (wret) {
@@ -1109,6 +1147,8 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1109 u32 left_nr; 1147 u32 left_nr;
1110 1148
1111 btrfs_tree_lock(left); 1149 btrfs_tree_lock(left);
1150 btrfs_set_lock_blocking(left);
1151
1112 left_nr = btrfs_header_nritems(left); 1152 left_nr = btrfs_header_nritems(left);
1113 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 1153 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1114 wret = 1; 1154 wret = 1;
@@ -1155,7 +1195,10 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
1155 */ 1195 */
1156 if (right) { 1196 if (right) {
1157 u32 right_nr; 1197 u32 right_nr;
1198
1158 btrfs_tree_lock(right); 1199 btrfs_tree_lock(right);
1200 btrfs_set_lock_blocking(right);
1201
1159 right_nr = btrfs_header_nritems(right); 1202 right_nr = btrfs_header_nritems(right);
1160 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) { 1203 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1161 wret = 1; 1204 wret = 1;
@@ -1210,8 +1253,7 @@ static noinline void reada_for_search(struct btrfs_root *root,
1210 struct btrfs_disk_key disk_key; 1253 struct btrfs_disk_key disk_key;
1211 u32 nritems; 1254 u32 nritems;
1212 u64 search; 1255 u64 search;
1213 u64 lowest_read; 1256 u64 target;
1214 u64 highest_read;
1215 u64 nread = 0; 1257 u64 nread = 0;
1216 int direction = path->reada; 1258 int direction = path->reada;
1217 struct extent_buffer *eb; 1259 struct extent_buffer *eb;
@@ -1235,8 +1277,7 @@ static noinline void reada_for_search(struct btrfs_root *root,
1235 return; 1277 return;
1236 } 1278 }
1237 1279
1238 highest_read = search; 1280 target = search;
1239 lowest_read = search;
1240 1281
1241 nritems = btrfs_header_nritems(node); 1282 nritems = btrfs_header_nritems(node);
1242 nr = slot; 1283 nr = slot;
@@ -1256,27 +1297,80 @@ static noinline void reada_for_search(struct btrfs_root *root,
1256 break; 1297 break;
1257 } 1298 }
1258 search = btrfs_node_blockptr(node, nr); 1299 search = btrfs_node_blockptr(node, nr);
1259 if ((search >= lowest_read && search <= highest_read) || 1300 if ((search <= target && target - search <= 65536) ||
1260 (search < lowest_read && lowest_read - search <= 16384) || 1301 (search > target && search - target <= 65536)) {
1261 (search > highest_read && search - highest_read <= 16384)) {
1262 readahead_tree_block(root, search, blocksize, 1302 readahead_tree_block(root, search, blocksize,
1263 btrfs_node_ptr_generation(node, nr)); 1303 btrfs_node_ptr_generation(node, nr));
1264 nread += blocksize; 1304 nread += blocksize;
1265 } 1305 }
1266 nscan++; 1306 nscan++;
1267 if (path->reada < 2 && (nread > (64 * 1024) || nscan > 32)) 1307 if ((nread > 65536 || nscan > 32))
1268 break; 1308 break;
1309 }
1310}
1269 1311
1270 if (nread > (256 * 1024) || nscan > 128) 1312/*
1271 break; 1313 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1314 * cache
1315 */
1316static noinline int reada_for_balance(struct btrfs_root *root,
1317 struct btrfs_path *path, int level)
1318{
1319 int slot;
1320 int nritems;
1321 struct extent_buffer *parent;
1322 struct extent_buffer *eb;
1323 u64 gen;
1324 u64 block1 = 0;
1325 u64 block2 = 0;
1326 int ret = 0;
1327 int blocksize;
1272 1328
1273 if (search < lowest_read) 1329 parent = path->nodes[level - 1];
1274 lowest_read = search; 1330 if (!parent)
1275 if (search > highest_read) 1331 return 0;
1276 highest_read = search; 1332
1333 nritems = btrfs_header_nritems(parent);
1334 slot = path->slots[level];
1335 blocksize = btrfs_level_size(root, level);
1336
1337 if (slot > 0) {
1338 block1 = btrfs_node_blockptr(parent, slot - 1);
1339 gen = btrfs_node_ptr_generation(parent, slot - 1);
1340 eb = btrfs_find_tree_block(root, block1, blocksize);
1341 if (eb && btrfs_buffer_uptodate(eb, gen))
1342 block1 = 0;
1343 free_extent_buffer(eb);
1344 }
1345 if (slot < nritems) {
1346 block2 = btrfs_node_blockptr(parent, slot + 1);
1347 gen = btrfs_node_ptr_generation(parent, slot + 1);
1348 eb = btrfs_find_tree_block(root, block2, blocksize);
1349 if (eb && btrfs_buffer_uptodate(eb, gen))
1350 block2 = 0;
1351 free_extent_buffer(eb);
1352 }
1353 if (block1 || block2) {
1354 ret = -EAGAIN;
1355 btrfs_release_path(root, path);
1356 if (block1)
1357 readahead_tree_block(root, block1, blocksize, 0);
1358 if (block2)
1359 readahead_tree_block(root, block2, blocksize, 0);
1360
1361 if (block1) {
1362 eb = read_tree_block(root, block1, blocksize, 0);
1363 free_extent_buffer(eb);
1364 }
1365 if (block1) {
1366 eb = read_tree_block(root, block2, blocksize, 0);
1367 free_extent_buffer(eb);
1368 }
1277 } 1369 }
1370 return ret;
1278} 1371}
1279 1372
1373
1280/* 1374/*
1281 * when we walk down the tree, it is usually safe to unlock the higher layers 1375 * when we walk down the tree, it is usually safe to unlock the higher layers
1282 * in the tree. The exceptions are when our path goes through slot 0, because 1376 * in the tree. The exceptions are when our path goes through slot 0, because
@@ -1328,6 +1422,32 @@ static noinline void unlock_up(struct btrfs_path *path, int level,
1328} 1422}
1329 1423
1330/* 1424/*
1425 * This releases any locks held in the path starting at level and
1426 * going all the way up to the root.
1427 *
1428 * btrfs_search_slot will keep the lock held on higher nodes in a few
1429 * corner cases, such as COW of the block at slot zero in the node. This
1430 * ignores those rules, and it should only be called when there are no
1431 * more updates to be done higher up in the tree.
1432 */
1433noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1434{
1435 int i;
1436
1437 if (path->keep_locks || path->lowest_level)
1438 return;
1439
1440 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1441 if (!path->nodes[i])
1442 continue;
1443 if (!path->locks[i])
1444 continue;
1445 btrfs_tree_unlock(path->nodes[i]);
1446 path->locks[i] = 0;
1447 }
1448}
1449
1450/*
1331 * look for key in the tree. path is filled in with nodes along the way 1451 * look for key in the tree. path is filled in with nodes along the way
1332 * if key is found, we return zero and you can find the item in the leaf 1452 * if key is found, we return zero and you can find the item in the leaf
1333 * level of the path (level 0) 1453 * level of the path (level 0)
@@ -1387,32 +1507,30 @@ again:
1387 int wret; 1507 int wret;
1388 1508
1389 /* is a cow on this block not required */ 1509 /* is a cow on this block not required */
1390 spin_lock(&root->fs_info->hash_lock);
1391 if (btrfs_header_generation(b) == trans->transid && 1510 if (btrfs_header_generation(b) == trans->transid &&
1392 btrfs_header_owner(b) == root->root_key.objectid && 1511 btrfs_header_owner(b) == root->root_key.objectid &&
1393 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) { 1512 !btrfs_header_flag(b, BTRFS_HEADER_FLAG_WRITTEN)) {
1394 spin_unlock(&root->fs_info->hash_lock);
1395 goto cow_done; 1513 goto cow_done;
1396 } 1514 }
1397 spin_unlock(&root->fs_info->hash_lock);
1398 1515
1399 /* ok, we have to cow, is our old prealloc the right 1516 /* ok, we have to cow, is our old prealloc the right
1400 * size? 1517 * size?
1401 */ 1518 */
1402 if (prealloc_block.objectid && 1519 if (prealloc_block.objectid &&
1403 prealloc_block.offset != b->len) { 1520 prealloc_block.offset != b->len) {
1521 btrfs_release_path(root, p);
1404 btrfs_free_reserved_extent(root, 1522 btrfs_free_reserved_extent(root,
1405 prealloc_block.objectid, 1523 prealloc_block.objectid,
1406 prealloc_block.offset); 1524 prealloc_block.offset);
1407 prealloc_block.objectid = 0; 1525 prealloc_block.objectid = 0;
1526 goto again;
1408 } 1527 }
1409 1528
1410 /* 1529 /*
1411 * for higher level blocks, try not to allocate blocks 1530 * for higher level blocks, try not to allocate blocks
1412 * with the block and the parent locks held. 1531 * with the block and the parent locks held.
1413 */ 1532 */
1414 if (level > 1 && !prealloc_block.objectid && 1533 if (level > 0 && !prealloc_block.objectid) {
1415 btrfs_path_lock_waiting(p, level)) {
1416 u32 size = b->len; 1534 u32 size = b->len;
1417 u64 hint = b->start; 1535 u64 hint = b->start;
1418 1536
@@ -1425,6 +1543,8 @@ again:
1425 goto again; 1543 goto again;
1426 } 1544 }
1427 1545
1546 btrfs_set_path_blocking(p);
1547
1428 wret = btrfs_cow_block(trans, root, b, 1548 wret = btrfs_cow_block(trans, root, b,
1429 p->nodes[level + 1], 1549 p->nodes[level + 1],
1430 p->slots[level + 1], 1550 p->slots[level + 1],
@@ -1446,6 +1566,22 @@ cow_done:
1446 if (!p->skip_locking) 1566 if (!p->skip_locking)
1447 p->locks[level] = 1; 1567 p->locks[level] = 1;
1448 1568
1569 btrfs_clear_path_blocking(p);
1570
1571 /*
1572 * we have a lock on b and as long as we aren't changing
1573 * the tree, there is no way to for the items in b to change.
1574 * It is safe to drop the lock on our parent before we
1575 * go through the expensive btree search on b.
1576 *
1577 * If cow is true, then we might be changing slot zero,
1578 * which may require changing the parent. So, we can't
1579 * drop the lock until after we know which slot we're
1580 * operating on.
1581 */
1582 if (!cow)
1583 btrfs_unlock_up_safe(p, level + 1);
1584
1449 ret = check_block(root, p, level); 1585 ret = check_block(root, p, level);
1450 if (ret) { 1586 if (ret) {
1451 ret = -1; 1587 ret = -1;
@@ -1453,6 +1589,7 @@ cow_done:
1453 } 1589 }
1454 1590
1455 ret = bin_search(b, key, level, &slot); 1591 ret = bin_search(b, key, level, &slot);
1592
1456 if (level != 0) { 1593 if (level != 0) {
1457 if (ret && slot > 0) 1594 if (ret && slot > 0)
1458 slot -= 1; 1595 slot -= 1;
@@ -1460,7 +1597,16 @@ cow_done:
1460 if ((p->search_for_split || ins_len > 0) && 1597 if ((p->search_for_split || ins_len > 0) &&
1461 btrfs_header_nritems(b) >= 1598 btrfs_header_nritems(b) >=
1462 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) { 1599 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
1463 int sret = split_node(trans, root, p, level); 1600 int sret;
1601
1602 sret = reada_for_balance(root, p, level);
1603 if (sret)
1604 goto again;
1605
1606 btrfs_set_path_blocking(p);
1607 sret = split_node(trans, root, p, level);
1608 btrfs_clear_path_blocking(p);
1609
1464 BUG_ON(sret > 0); 1610 BUG_ON(sret > 0);
1465 if (sret) { 1611 if (sret) {
1466 ret = sret; 1612 ret = sret;
@@ -1468,9 +1614,19 @@ cow_done:
1468 } 1614 }
1469 b = p->nodes[level]; 1615 b = p->nodes[level];
1470 slot = p->slots[level]; 1616 slot = p->slots[level];
1471 } else if (ins_len < 0) { 1617 } else if (ins_len < 0 &&
1472 int sret = balance_level(trans, root, p, 1618 btrfs_header_nritems(b) <
1473 level); 1619 BTRFS_NODEPTRS_PER_BLOCK(root) / 4) {
1620 int sret;
1621
1622 sret = reada_for_balance(root, p, level);
1623 if (sret)
1624 goto again;
1625
1626 btrfs_set_path_blocking(p);
1627 sret = balance_level(trans, root, p, level);
1628 btrfs_clear_path_blocking(p);
1629
1474 if (sret) { 1630 if (sret) {
1475 ret = sret; 1631 ret = sret;
1476 goto done; 1632 goto done;
@@ -1504,7 +1660,7 @@ cow_done:
1504 * of the btree by dropping locks before 1660 * of the btree by dropping locks before
1505 * we read. 1661 * we read.
1506 */ 1662 */
1507 if (level > 1) { 1663 if (level > 0) {
1508 btrfs_release_path(NULL, p); 1664 btrfs_release_path(NULL, p);
1509 if (tmp) 1665 if (tmp)
1510 free_extent_buffer(tmp); 1666 free_extent_buffer(tmp);
@@ -1519,6 +1675,7 @@ cow_done:
1519 free_extent_buffer(tmp); 1675 free_extent_buffer(tmp);
1520 goto again; 1676 goto again;
1521 } else { 1677 } else {
1678 btrfs_set_path_blocking(p);
1522 if (tmp) 1679 if (tmp)
1523 free_extent_buffer(tmp); 1680 free_extent_buffer(tmp);
1524 if (should_reada) 1681 if (should_reada)
@@ -1528,14 +1685,29 @@ cow_done:
1528 b = read_node_slot(root, b, slot); 1685 b = read_node_slot(root, b, slot);
1529 } 1686 }
1530 } 1687 }
1531 if (!p->skip_locking) 1688 if (!p->skip_locking) {
1532 btrfs_tree_lock(b); 1689 int lret;
1690
1691 btrfs_clear_path_blocking(p);
1692 lret = btrfs_try_spin_lock(b);
1693
1694 if (!lret) {
1695 btrfs_set_path_blocking(p);
1696 btrfs_tree_lock(b);
1697 btrfs_clear_path_blocking(p);
1698 }
1699 }
1533 } else { 1700 } else {
1534 p->slots[level] = slot; 1701 p->slots[level] = slot;
1535 if (ins_len > 0 && 1702 if (ins_len > 0 &&
1536 btrfs_leaf_free_space(root, b) < ins_len) { 1703 btrfs_leaf_free_space(root, b) < ins_len) {
1537 int sret = split_leaf(trans, root, key, 1704 int sret;
1705
1706 btrfs_set_path_blocking(p);
1707 sret = split_leaf(trans, root, key,
1538 p, ins_len, ret == 0); 1708 p, ins_len, ret == 0);
1709 btrfs_clear_path_blocking(p);
1710
1539 BUG_ON(sret > 0); 1711 BUG_ON(sret > 0);
1540 if (sret) { 1712 if (sret) {
1541 ret = sret; 1713 ret = sret;
@@ -1549,12 +1721,16 @@ cow_done:
1549 } 1721 }
1550 ret = 1; 1722 ret = 1;
1551done: 1723done:
1724 /*
1725 * we don't really know what they plan on doing with the path
1726 * from here on, so for now just mark it as blocking
1727 */
1728 btrfs_set_path_blocking(p);
1552 if (prealloc_block.objectid) { 1729 if (prealloc_block.objectid) {
1553 btrfs_free_reserved_extent(root, 1730 btrfs_free_reserved_extent(root,
1554 prealloc_block.objectid, 1731 prealloc_block.objectid,
1555 prealloc_block.offset); 1732 prealloc_block.offset);
1556 } 1733 }
1557
1558 return ret; 1734 return ret;
1559} 1735}
1560 1736
@@ -1578,6 +1754,8 @@ int btrfs_merge_path(struct btrfs_trans_handle *trans,
1578 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0); 1754 ret = btrfs_cow_block(trans, root, eb, NULL, 0, &eb, 0);
1579 BUG_ON(ret); 1755 BUG_ON(ret);
1580 1756
1757 btrfs_set_lock_blocking(eb);
1758
1581 parent = eb; 1759 parent = eb;
1582 while (1) { 1760 while (1) {
1583 level = btrfs_header_level(parent); 1761 level = btrfs_header_level(parent);
@@ -1602,6 +1780,7 @@ int btrfs_merge_path(struct btrfs_trans_handle *trans,
1602 eb = read_tree_block(root, bytenr, blocksize, 1780 eb = read_tree_block(root, bytenr, blocksize,
1603 generation); 1781 generation);
1604 btrfs_tree_lock(eb); 1782 btrfs_tree_lock(eb);
1783 btrfs_set_lock_blocking(eb);
1605 } 1784 }
1606 1785
1607 /* 1786 /*
@@ -1626,6 +1805,7 @@ int btrfs_merge_path(struct btrfs_trans_handle *trans,
1626 eb = read_tree_block(root, bytenr, blocksize, 1805 eb = read_tree_block(root, bytenr, blocksize,
1627 generation); 1806 generation);
1628 btrfs_tree_lock(eb); 1807 btrfs_tree_lock(eb);
1808 btrfs_set_lock_blocking(eb);
1629 } 1809 }
1630 1810
1631 ret = btrfs_cow_block(trans, root, eb, parent, slot, 1811 ret = btrfs_cow_block(trans, root, eb, parent, slot,
@@ -2172,6 +2352,8 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
2172 2352
2173 right = read_node_slot(root, upper, slot + 1); 2353 right = read_node_slot(root, upper, slot + 1);
2174 btrfs_tree_lock(right); 2354 btrfs_tree_lock(right);
2355 btrfs_set_lock_blocking(right);
2356
2175 free_space = btrfs_leaf_free_space(root, right); 2357 free_space = btrfs_leaf_free_space(root, right);
2176 if (free_space < data_size) 2358 if (free_space < data_size)
2177 goto out_unlock; 2359 goto out_unlock;
@@ -2367,6 +2549,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
2367 2549
2368 left = read_node_slot(root, path->nodes[1], slot - 1); 2550 left = read_node_slot(root, path->nodes[1], slot - 1);
2369 btrfs_tree_lock(left); 2551 btrfs_tree_lock(left);
2552 btrfs_set_lock_blocking(left);
2553
2370 free_space = btrfs_leaf_free_space(root, left); 2554 free_space = btrfs_leaf_free_space(root, left);
2371 if (free_space < data_size) { 2555 if (free_space < data_size) {
2372 ret = 1; 2556 ret = 1;
@@ -2825,6 +3009,12 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
2825 path->keep_locks = 0; 3009 path->keep_locks = 0;
2826 BUG_ON(ret); 3010 BUG_ON(ret);
2827 3011
3012 /*
3013 * make sure any changes to the path from split_leaf leave it
3014 * in a blocking state
3015 */
3016 btrfs_set_path_blocking(path);
3017
2828 leaf = path->nodes[0]; 3018 leaf = path->nodes[0];
2829 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 3019 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
2830 3020
@@ -3354,6 +3544,7 @@ int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3354 BUG(); 3544 BUG();
3355 } 3545 }
3356out: 3546out:
3547 btrfs_unlock_up_safe(path, 1);
3357 return ret; 3548 return ret;
3358} 3549}
3359 3550
@@ -3441,15 +3632,22 @@ noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
3441{ 3632{
3442 int ret; 3633 int ret;
3443 u64 root_gen = btrfs_header_generation(path->nodes[1]); 3634 u64 root_gen = btrfs_header_generation(path->nodes[1]);
3635 u64 parent_start = path->nodes[1]->start;
3636 u64 parent_owner = btrfs_header_owner(path->nodes[1]);
3444 3637
3445 ret = del_ptr(trans, root, path, 1, path->slots[1]); 3638 ret = del_ptr(trans, root, path, 1, path->slots[1]);
3446 if (ret) 3639 if (ret)
3447 return ret; 3640 return ret;
3448 3641
3642 /*
3643 * btrfs_free_extent is expensive, we want to make sure we
3644 * aren't holding any locks when we call it
3645 */
3646 btrfs_unlock_up_safe(path, 0);
3647
3449 ret = btrfs_free_extent(trans, root, bytenr, 3648 ret = btrfs_free_extent(trans, root, bytenr,
3450 btrfs_level_size(root, 0), 3649 btrfs_level_size(root, 0),
3451 path->nodes[1]->start, 3650 parent_start, parent_owner,
3452 btrfs_header_owner(path->nodes[1]),
3453 root_gen, 0, 1); 3651 root_gen, 0, 1);
3454 return ret; 3652 return ret;
3455} 3653}
@@ -3721,12 +3919,14 @@ find_next_key:
3721 */ 3919 */
3722 if (slot >= nritems) { 3920 if (slot >= nritems) {
3723 path->slots[level] = slot; 3921 path->slots[level] = slot;
3922 btrfs_set_path_blocking(path);
3724 sret = btrfs_find_next_key(root, path, min_key, level, 3923 sret = btrfs_find_next_key(root, path, min_key, level,
3725 cache_only, min_trans); 3924 cache_only, min_trans);
3726 if (sret == 0) { 3925 if (sret == 0) {
3727 btrfs_release_path(root, path); 3926 btrfs_release_path(root, path);
3728 goto again; 3927 goto again;
3729 } else { 3928 } else {
3929 btrfs_clear_path_blocking(path);
3730 goto out; 3930 goto out;
3731 } 3931 }
3732 } 3932 }
@@ -3738,16 +3938,20 @@ find_next_key:
3738 unlock_up(path, level, 1); 3938 unlock_up(path, level, 1);
3739 goto out; 3939 goto out;
3740 } 3940 }
3941 btrfs_set_path_blocking(path);
3741 cur = read_node_slot(root, cur, slot); 3942 cur = read_node_slot(root, cur, slot);
3742 3943
3743 btrfs_tree_lock(cur); 3944 btrfs_tree_lock(cur);
3945
3744 path->locks[level - 1] = 1; 3946 path->locks[level - 1] = 1;
3745 path->nodes[level - 1] = cur; 3947 path->nodes[level - 1] = cur;
3746 unlock_up(path, level, 1); 3948 unlock_up(path, level, 1);
3949 btrfs_clear_path_blocking(path);
3747 } 3950 }
3748out: 3951out:
3749 if (ret == 0) 3952 if (ret == 0)
3750 memcpy(min_key, &found_key, sizeof(found_key)); 3953 memcpy(min_key, &found_key, sizeof(found_key));
3954 btrfs_set_path_blocking(path);
3751 return ret; 3955 return ret;
3752} 3956}
3753 3957
@@ -3843,6 +4047,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3843 if (ret < 0) 4047 if (ret < 0)
3844 return ret; 4048 return ret;
3845 4049
4050 btrfs_set_path_blocking(path);
3846 nritems = btrfs_header_nritems(path->nodes[0]); 4051 nritems = btrfs_header_nritems(path->nodes[0]);
3847 /* 4052 /*
3848 * by releasing the path above we dropped all our locks. A balance 4053 * by releasing the path above we dropped all our locks. A balance
@@ -3873,6 +4078,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3873 free_extent_buffer(next); 4078 free_extent_buffer(next);
3874 } 4079 }
3875 4080
4081 /* the path was set to blocking above */
3876 if (level == 1 && (path->locks[1] || path->skip_locking) && 4082 if (level == 1 && (path->locks[1] || path->skip_locking) &&
3877 path->reada) 4083 path->reada)
3878 reada_for_search(root, path, level, slot, 0); 4084 reada_for_search(root, path, level, slot, 0);
@@ -3881,6 +4087,7 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3881 if (!path->skip_locking) { 4087 if (!path->skip_locking) {
3882 WARN_ON(!btrfs_tree_locked(c)); 4088 WARN_ON(!btrfs_tree_locked(c));
3883 btrfs_tree_lock(next); 4089 btrfs_tree_lock(next);
4090 btrfs_set_lock_blocking(next);
3884 } 4091 }
3885 break; 4092 break;
3886 } 4093 }
@@ -3897,12 +4104,15 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3897 path->locks[level] = 1; 4104 path->locks[level] = 1;
3898 if (!level) 4105 if (!level)
3899 break; 4106 break;
4107
4108 btrfs_set_path_blocking(path);
3900 if (level == 1 && path->locks[1] && path->reada) 4109 if (level == 1 && path->locks[1] && path->reada)
3901 reada_for_search(root, path, level, slot, 0); 4110 reada_for_search(root, path, level, slot, 0);
3902 next = read_node_slot(root, next, 0); 4111 next = read_node_slot(root, next, 0);
3903 if (!path->skip_locking) { 4112 if (!path->skip_locking) {
3904 WARN_ON(!btrfs_tree_locked(path->nodes[level])); 4113 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3905 btrfs_tree_lock(next); 4114 btrfs_tree_lock(next);
4115 btrfs_set_lock_blocking(next);
3906 } 4116 }
3907 } 4117 }
3908done: 4118done:
@@ -3927,6 +4137,7 @@ int btrfs_previous_item(struct btrfs_root *root,
3927 4137
3928 while (1) { 4138 while (1) {
3929 if (path->slots[0] == 0) { 4139 if (path->slots[0] == 0) {
4140 btrfs_set_path_blocking(path);
3930 ret = btrfs_prev_leaf(root, path); 4141 ret = btrfs_prev_leaf(root, path);
3931 if (ret != 0) 4142 if (ret != 0)
3932 return ret; 4143 return ret;