diff options
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r-- | fs/btrfs/disk-io.c | 486 |
1 files changed, 283 insertions, 203 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index b8b60b660c8f..6b092a1c4e37 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c | |||
@@ -1013,7 +1013,8 @@ static int btree_releasepage(struct page *page, gfp_t gfp_flags) | |||
1013 | return try_release_extent_buffer(page); | 1013 | return try_release_extent_buffer(page); |
1014 | } | 1014 | } |
1015 | 1015 | ||
1016 | static void btree_invalidatepage(struct page *page, unsigned long offset) | 1016 | static void btree_invalidatepage(struct page *page, unsigned int offset, |
1017 | unsigned int length) | ||
1017 | { | 1018 | { |
1018 | struct extent_io_tree *tree; | 1019 | struct extent_io_tree *tree; |
1019 | tree = &BTRFS_I(page->mapping->host)->io_tree; | 1020 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
@@ -1191,6 +1192,8 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | |||
1191 | root->objectid = objectid; | 1192 | root->objectid = objectid; |
1192 | root->last_trans = 0; | 1193 | root->last_trans = 0; |
1193 | root->highest_objectid = 0; | 1194 | root->highest_objectid = 0; |
1195 | root->nr_delalloc_inodes = 0; | ||
1196 | root->nr_ordered_extents = 0; | ||
1194 | root->name = NULL; | 1197 | root->name = NULL; |
1195 | root->inode_tree = RB_ROOT; | 1198 | root->inode_tree = RB_ROOT; |
1196 | INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); | 1199 | INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC); |
@@ -1199,10 +1202,16 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | |||
1199 | 1202 | ||
1200 | INIT_LIST_HEAD(&root->dirty_list); | 1203 | INIT_LIST_HEAD(&root->dirty_list); |
1201 | INIT_LIST_HEAD(&root->root_list); | 1204 | INIT_LIST_HEAD(&root->root_list); |
1205 | INIT_LIST_HEAD(&root->delalloc_inodes); | ||
1206 | INIT_LIST_HEAD(&root->delalloc_root); | ||
1207 | INIT_LIST_HEAD(&root->ordered_extents); | ||
1208 | INIT_LIST_HEAD(&root->ordered_root); | ||
1202 | INIT_LIST_HEAD(&root->logged_list[0]); | 1209 | INIT_LIST_HEAD(&root->logged_list[0]); |
1203 | INIT_LIST_HEAD(&root->logged_list[1]); | 1210 | INIT_LIST_HEAD(&root->logged_list[1]); |
1204 | spin_lock_init(&root->orphan_lock); | 1211 | spin_lock_init(&root->orphan_lock); |
1205 | spin_lock_init(&root->inode_lock); | 1212 | spin_lock_init(&root->inode_lock); |
1213 | spin_lock_init(&root->delalloc_lock); | ||
1214 | spin_lock_init(&root->ordered_extent_lock); | ||
1206 | spin_lock_init(&root->accounting_lock); | 1215 | spin_lock_init(&root->accounting_lock); |
1207 | spin_lock_init(&root->log_extents_lock[0]); | 1216 | spin_lock_init(&root->log_extents_lock[0]); |
1208 | spin_lock_init(&root->log_extents_lock[1]); | 1217 | spin_lock_init(&root->log_extents_lock[1]); |
@@ -1216,6 +1225,7 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | |||
1216 | atomic_set(&root->log_writers, 0); | 1225 | atomic_set(&root->log_writers, 0); |
1217 | atomic_set(&root->log_batch, 0); | 1226 | atomic_set(&root->log_batch, 0); |
1218 | atomic_set(&root->orphan_inodes, 0); | 1227 | atomic_set(&root->orphan_inodes, 0); |
1228 | atomic_set(&root->refs, 1); | ||
1219 | root->log_transid = 0; | 1229 | root->log_transid = 0; |
1220 | root->last_log_commit = 0; | 1230 | root->last_log_commit = 0; |
1221 | extent_io_tree_init(&root->dirty_log_pages, | 1231 | extent_io_tree_init(&root->dirty_log_pages, |
@@ -1234,39 +1244,6 @@ static void __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize, | |||
1234 | spin_lock_init(&root->root_item_lock); | 1244 | spin_lock_init(&root->root_item_lock); |
1235 | } | 1245 | } |
1236 | 1246 | ||
1237 | static int __must_check find_and_setup_root(struct btrfs_root *tree_root, | ||
1238 | struct btrfs_fs_info *fs_info, | ||
1239 | u64 objectid, | ||
1240 | struct btrfs_root *root) | ||
1241 | { | ||
1242 | int ret; | ||
1243 | u32 blocksize; | ||
1244 | u64 generation; | ||
1245 | |||
1246 | __setup_root(tree_root->nodesize, tree_root->leafsize, | ||
1247 | tree_root->sectorsize, tree_root->stripesize, | ||
1248 | root, fs_info, objectid); | ||
1249 | ret = btrfs_find_last_root(tree_root, objectid, | ||
1250 | &root->root_item, &root->root_key); | ||
1251 | if (ret > 0) | ||
1252 | return -ENOENT; | ||
1253 | else if (ret < 0) | ||
1254 | return ret; | ||
1255 | |||
1256 | generation = btrfs_root_generation(&root->root_item); | ||
1257 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); | ||
1258 | root->commit_root = NULL; | ||
1259 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | ||
1260 | blocksize, generation); | ||
1261 | if (!root->node || !btrfs_buffer_uptodate(root->node, generation, 0)) { | ||
1262 | free_extent_buffer(root->node); | ||
1263 | root->node = NULL; | ||
1264 | return -EIO; | ||
1265 | } | ||
1266 | root->commit_root = btrfs_root_node(root); | ||
1267 | return 0; | ||
1268 | } | ||
1269 | |||
1270 | static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) | 1247 | static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info) |
1271 | { | 1248 | { |
1272 | struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); | 1249 | struct btrfs_root *root = kzalloc(sizeof(*root), GFP_NOFS); |
@@ -1451,70 +1428,73 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans, | |||
1451 | return 0; | 1428 | return 0; |
1452 | } | 1429 | } |
1453 | 1430 | ||
1454 | struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root, | 1431 | struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, |
1455 | struct btrfs_key *location) | 1432 | struct btrfs_key *key) |
1456 | { | 1433 | { |
1457 | struct btrfs_root *root; | 1434 | struct btrfs_root *root; |
1458 | struct btrfs_fs_info *fs_info = tree_root->fs_info; | 1435 | struct btrfs_fs_info *fs_info = tree_root->fs_info; |
1459 | struct btrfs_path *path; | 1436 | struct btrfs_path *path; |
1460 | struct extent_buffer *l; | ||
1461 | u64 generation; | 1437 | u64 generation; |
1462 | u32 blocksize; | 1438 | u32 blocksize; |
1463 | int ret = 0; | 1439 | int ret; |
1464 | int slot; | ||
1465 | 1440 | ||
1466 | root = btrfs_alloc_root(fs_info); | 1441 | path = btrfs_alloc_path(); |
1467 | if (!root) | 1442 | if (!path) |
1468 | return ERR_PTR(-ENOMEM); | 1443 | return ERR_PTR(-ENOMEM); |
1469 | if (location->offset == (u64)-1) { | 1444 | |
1470 | ret = find_and_setup_root(tree_root, fs_info, | 1445 | root = btrfs_alloc_root(fs_info); |
1471 | location->objectid, root); | 1446 | if (!root) { |
1472 | if (ret) { | 1447 | ret = -ENOMEM; |
1473 | kfree(root); | 1448 | goto alloc_fail; |
1474 | return ERR_PTR(ret); | ||
1475 | } | ||
1476 | goto out; | ||
1477 | } | 1449 | } |
1478 | 1450 | ||
1479 | __setup_root(tree_root->nodesize, tree_root->leafsize, | 1451 | __setup_root(tree_root->nodesize, tree_root->leafsize, |
1480 | tree_root->sectorsize, tree_root->stripesize, | 1452 | tree_root->sectorsize, tree_root->stripesize, |
1481 | root, fs_info, location->objectid); | 1453 | root, fs_info, key->objectid); |
1482 | 1454 | ||
1483 | path = btrfs_alloc_path(); | 1455 | ret = btrfs_find_root(tree_root, key, path, |
1484 | if (!path) { | 1456 | &root->root_item, &root->root_key); |
1485 | kfree(root); | ||
1486 | return ERR_PTR(-ENOMEM); | ||
1487 | } | ||
1488 | ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); | ||
1489 | if (ret == 0) { | ||
1490 | l = path->nodes[0]; | ||
1491 | slot = path->slots[0]; | ||
1492 | btrfs_read_root_item(l, slot, &root->root_item); | ||
1493 | memcpy(&root->root_key, location, sizeof(*location)); | ||
1494 | } | ||
1495 | btrfs_free_path(path); | ||
1496 | if (ret) { | 1457 | if (ret) { |
1497 | kfree(root); | ||
1498 | if (ret > 0) | 1458 | if (ret > 0) |
1499 | ret = -ENOENT; | 1459 | ret = -ENOENT; |
1500 | return ERR_PTR(ret); | 1460 | goto find_fail; |
1501 | } | 1461 | } |
1502 | 1462 | ||
1503 | generation = btrfs_root_generation(&root->root_item); | 1463 | generation = btrfs_root_generation(&root->root_item); |
1504 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); | 1464 | blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); |
1505 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), | 1465 | root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), |
1506 | blocksize, generation); | 1466 | blocksize, generation); |
1507 | if (!root->node || !extent_buffer_uptodate(root->node)) { | 1467 | if (!root->node) { |
1508 | ret = (!root->node) ? -ENOMEM : -EIO; | 1468 | ret = -ENOMEM; |
1509 | 1469 | goto find_fail; | |
1510 | free_extent_buffer(root->node); | 1470 | } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) { |
1511 | kfree(root); | 1471 | ret = -EIO; |
1512 | return ERR_PTR(ret); | 1472 | goto read_fail; |
1513 | } | 1473 | } |
1514 | |||
1515 | root->commit_root = btrfs_root_node(root); | 1474 | root->commit_root = btrfs_root_node(root); |
1516 | out: | 1475 | out: |
1517 | if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { | 1476 | btrfs_free_path(path); |
1477 | return root; | ||
1478 | |||
1479 | read_fail: | ||
1480 | free_extent_buffer(root->node); | ||
1481 | find_fail: | ||
1482 | kfree(root); | ||
1483 | alloc_fail: | ||
1484 | root = ERR_PTR(ret); | ||
1485 | goto out; | ||
1486 | } | ||
1487 | |||
1488 | struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root, | ||
1489 | struct btrfs_key *location) | ||
1490 | { | ||
1491 | struct btrfs_root *root; | ||
1492 | |||
1493 | root = btrfs_read_tree_root(tree_root, location); | ||
1494 | if (IS_ERR(root)) | ||
1495 | return root; | ||
1496 | |||
1497 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | ||
1518 | root->ref_cows = 1; | 1498 | root->ref_cows = 1; |
1519 | btrfs_check_and_init_root_item(&root->root_item); | 1499 | btrfs_check_and_init_root_item(&root->root_item); |
1520 | } | 1500 | } |
@@ -1522,6 +1502,66 @@ out: | |||
1522 | return root; | 1502 | return root; |
1523 | } | 1503 | } |
1524 | 1504 | ||
1505 | int btrfs_init_fs_root(struct btrfs_root *root) | ||
1506 | { | ||
1507 | int ret; | ||
1508 | |||
1509 | root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); | ||
1510 | root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), | ||
1511 | GFP_NOFS); | ||
1512 | if (!root->free_ino_pinned || !root->free_ino_ctl) { | ||
1513 | ret = -ENOMEM; | ||
1514 | goto fail; | ||
1515 | } | ||
1516 | |||
1517 | btrfs_init_free_ino_ctl(root); | ||
1518 | mutex_init(&root->fs_commit_mutex); | ||
1519 | spin_lock_init(&root->cache_lock); | ||
1520 | init_waitqueue_head(&root->cache_wait); | ||
1521 | |||
1522 | ret = get_anon_bdev(&root->anon_dev); | ||
1523 | if (ret) | ||
1524 | goto fail; | ||
1525 | return 0; | ||
1526 | fail: | ||
1527 | kfree(root->free_ino_ctl); | ||
1528 | kfree(root->free_ino_pinned); | ||
1529 | return ret; | ||
1530 | } | ||
1531 | |||
1532 | struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info, | ||
1533 | u64 root_id) | ||
1534 | { | ||
1535 | struct btrfs_root *root; | ||
1536 | |||
1537 | spin_lock(&fs_info->fs_roots_radix_lock); | ||
1538 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | ||
1539 | (unsigned long)root_id); | ||
1540 | spin_unlock(&fs_info->fs_roots_radix_lock); | ||
1541 | return root; | ||
1542 | } | ||
1543 | |||
1544 | int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, | ||
1545 | struct btrfs_root *root) | ||
1546 | { | ||
1547 | int ret; | ||
1548 | |||
1549 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); | ||
1550 | if (ret) | ||
1551 | return ret; | ||
1552 | |||
1553 | spin_lock(&fs_info->fs_roots_radix_lock); | ||
1554 | ret = radix_tree_insert(&fs_info->fs_roots_radix, | ||
1555 | (unsigned long)root->root_key.objectid, | ||
1556 | root); | ||
1557 | if (ret == 0) | ||
1558 | root->in_radix = 1; | ||
1559 | spin_unlock(&fs_info->fs_roots_radix_lock); | ||
1560 | radix_tree_preload_end(); | ||
1561 | |||
1562 | return ret; | ||
1563 | } | ||
1564 | |||
1525 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, | 1565 | struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, |
1526 | struct btrfs_key *location) | 1566 | struct btrfs_key *location) |
1527 | { | 1567 | { |
@@ -1542,58 +1582,30 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info, | |||
1542 | return fs_info->quota_root ? fs_info->quota_root : | 1582 | return fs_info->quota_root ? fs_info->quota_root : |
1543 | ERR_PTR(-ENOENT); | 1583 | ERR_PTR(-ENOENT); |
1544 | again: | 1584 | again: |
1545 | spin_lock(&fs_info->fs_roots_radix_lock); | 1585 | root = btrfs_lookup_fs_root(fs_info, location->objectid); |
1546 | root = radix_tree_lookup(&fs_info->fs_roots_radix, | ||
1547 | (unsigned long)location->objectid); | ||
1548 | spin_unlock(&fs_info->fs_roots_radix_lock); | ||
1549 | if (root) | 1586 | if (root) |
1550 | return root; | 1587 | return root; |
1551 | 1588 | ||
1552 | root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); | 1589 | root = btrfs_read_fs_root(fs_info->tree_root, location); |
1553 | if (IS_ERR(root)) | 1590 | if (IS_ERR(root)) |
1554 | return root; | 1591 | return root; |
1555 | 1592 | ||
1556 | root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS); | 1593 | if (btrfs_root_refs(&root->root_item) == 0) { |
1557 | root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned), | 1594 | ret = -ENOENT; |
1558 | GFP_NOFS); | ||
1559 | if (!root->free_ino_pinned || !root->free_ino_ctl) { | ||
1560 | ret = -ENOMEM; | ||
1561 | goto fail; | 1595 | goto fail; |
1562 | } | 1596 | } |
1563 | 1597 | ||
1564 | btrfs_init_free_ino_ctl(root); | 1598 | ret = btrfs_init_fs_root(root); |
1565 | mutex_init(&root->fs_commit_mutex); | ||
1566 | spin_lock_init(&root->cache_lock); | ||
1567 | init_waitqueue_head(&root->cache_wait); | ||
1568 | |||
1569 | ret = get_anon_bdev(&root->anon_dev); | ||
1570 | if (ret) | 1599 | if (ret) |
1571 | goto fail; | 1600 | goto fail; |
1572 | 1601 | ||
1573 | if (btrfs_root_refs(&root->root_item) == 0) { | ||
1574 | ret = -ENOENT; | ||
1575 | goto fail; | ||
1576 | } | ||
1577 | |||
1578 | ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); | 1602 | ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid); |
1579 | if (ret < 0) | 1603 | if (ret < 0) |
1580 | goto fail; | 1604 | goto fail; |
1581 | if (ret == 0) | 1605 | if (ret == 0) |
1582 | root->orphan_item_inserted = 1; | 1606 | root->orphan_item_inserted = 1; |
1583 | 1607 | ||
1584 | ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); | 1608 | ret = btrfs_insert_fs_root(fs_info, root); |
1585 | if (ret) | ||
1586 | goto fail; | ||
1587 | |||
1588 | spin_lock(&fs_info->fs_roots_radix_lock); | ||
1589 | ret = radix_tree_insert(&fs_info->fs_roots_radix, | ||
1590 | (unsigned long)root->root_key.objectid, | ||
1591 | root); | ||
1592 | if (ret == 0) | ||
1593 | root->in_radix = 1; | ||
1594 | |||
1595 | spin_unlock(&fs_info->fs_roots_radix_lock); | ||
1596 | radix_tree_preload_end(); | ||
1597 | if (ret) { | 1609 | if (ret) { |
1598 | if (ret == -EEXIST) { | 1610 | if (ret == -EEXIST) { |
1599 | free_fs_root(root); | 1611 | free_fs_root(root); |
@@ -1601,10 +1613,6 @@ again: | |||
1601 | } | 1613 | } |
1602 | goto fail; | 1614 | goto fail; |
1603 | } | 1615 | } |
1604 | |||
1605 | ret = btrfs_find_dead_roots(fs_info->tree_root, | ||
1606 | root->root_key.objectid); | ||
1607 | WARN_ON(ret); | ||
1608 | return root; | 1616 | return root; |
1609 | fail: | 1617 | fail: |
1610 | free_fs_root(root); | 1618 | free_fs_root(root); |
@@ -1676,21 +1684,37 @@ static void end_workqueue_fn(struct btrfs_work *work) | |||
1676 | static int cleaner_kthread(void *arg) | 1684 | static int cleaner_kthread(void *arg) |
1677 | { | 1685 | { |
1678 | struct btrfs_root *root = arg; | 1686 | struct btrfs_root *root = arg; |
1687 | int again; | ||
1679 | 1688 | ||
1680 | do { | 1689 | do { |
1681 | int again = 0; | 1690 | again = 0; |
1682 | 1691 | ||
1683 | if (!(root->fs_info->sb->s_flags & MS_RDONLY) && | 1692 | /* Make the cleaner go to sleep early. */ |
1684 | down_read_trylock(&root->fs_info->sb->s_umount)) { | 1693 | if (btrfs_need_cleaner_sleep(root)) |
1685 | if (mutex_trylock(&root->fs_info->cleaner_mutex)) { | 1694 | goto sleep; |
1686 | btrfs_run_delayed_iputs(root); | 1695 | |
1687 | again = btrfs_clean_one_deleted_snapshot(root); | 1696 | if (!mutex_trylock(&root->fs_info->cleaner_mutex)) |
1688 | mutex_unlock(&root->fs_info->cleaner_mutex); | 1697 | goto sleep; |
1689 | } | 1698 | |
1690 | btrfs_run_defrag_inodes(root->fs_info); | 1699 | /* |
1691 | up_read(&root->fs_info->sb->s_umount); | 1700 | * Avoid the problem that we change the status of the fs |
1701 | * during the above check and trylock. | ||
1702 | */ | ||
1703 | if (btrfs_need_cleaner_sleep(root)) { | ||
1704 | mutex_unlock(&root->fs_info->cleaner_mutex); | ||
1705 | goto sleep; | ||
1692 | } | 1706 | } |
1693 | 1707 | ||
1708 | btrfs_run_delayed_iputs(root); | ||
1709 | again = btrfs_clean_one_deleted_snapshot(root); | ||
1710 | mutex_unlock(&root->fs_info->cleaner_mutex); | ||
1711 | |||
1712 | /* | ||
1713 | * The defragger has dealt with the R/O remount and umount, | ||
1714 | * needn't do anything special here. | ||
1715 | */ | ||
1716 | btrfs_run_defrag_inodes(root->fs_info); | ||
1717 | sleep: | ||
1694 | if (!try_to_freeze() && !again) { | 1718 | if (!try_to_freeze() && !again) { |
1695 | set_current_state(TASK_INTERRUPTIBLE); | 1719 | set_current_state(TASK_INTERRUPTIBLE); |
1696 | if (!kthread_should_stop()) | 1720 | if (!kthread_should_stop()) |
@@ -1724,7 +1748,7 @@ static int transaction_kthread(void *arg) | |||
1724 | } | 1748 | } |
1725 | 1749 | ||
1726 | now = get_seconds(); | 1750 | now = get_seconds(); |
1727 | if (!cur->blocked && | 1751 | if (cur->state < TRANS_STATE_BLOCKED && |
1728 | (now < cur->start_time || now - cur->start_time < 30)) { | 1752 | (now < cur->start_time || now - cur->start_time < 30)) { |
1729 | spin_unlock(&root->fs_info->trans_lock); | 1753 | spin_unlock(&root->fs_info->trans_lock); |
1730 | delay = HZ * 5; | 1754 | delay = HZ * 5; |
@@ -2034,11 +2058,11 @@ static void del_fs_roots(struct btrfs_fs_info *fs_info) | |||
2034 | list_del(&gang[0]->root_list); | 2058 | list_del(&gang[0]->root_list); |
2035 | 2059 | ||
2036 | if (gang[0]->in_radix) { | 2060 | if (gang[0]->in_radix) { |
2037 | btrfs_free_fs_root(fs_info, gang[0]); | 2061 | btrfs_drop_and_free_fs_root(fs_info, gang[0]); |
2038 | } else { | 2062 | } else { |
2039 | free_extent_buffer(gang[0]->node); | 2063 | free_extent_buffer(gang[0]->node); |
2040 | free_extent_buffer(gang[0]->commit_root); | 2064 | free_extent_buffer(gang[0]->commit_root); |
2041 | kfree(gang[0]); | 2065 | btrfs_put_fs_root(gang[0]); |
2042 | } | 2066 | } |
2043 | } | 2067 | } |
2044 | 2068 | ||
@@ -2049,7 +2073,7 @@ static void del_fs_roots(struct btrfs_fs_info *fs_info) | |||
2049 | if (!ret) | 2073 | if (!ret) |
2050 | break; | 2074 | break; |
2051 | for (i = 0; i < ret; i++) | 2075 | for (i = 0; i < ret; i++) |
2052 | btrfs_free_fs_root(fs_info, gang[i]); | 2076 | btrfs_drop_and_free_fs_root(fs_info, gang[i]); |
2053 | } | 2077 | } |
2054 | } | 2078 | } |
2055 | 2079 | ||
@@ -2081,14 +2105,8 @@ int open_ctree(struct super_block *sb, | |||
2081 | int backup_index = 0; | 2105 | int backup_index = 0; |
2082 | 2106 | ||
2083 | tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); | 2107 | tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info); |
2084 | extent_root = fs_info->extent_root = btrfs_alloc_root(fs_info); | ||
2085 | csum_root = fs_info->csum_root = btrfs_alloc_root(fs_info); | ||
2086 | chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); | 2108 | chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info); |
2087 | dev_root = fs_info->dev_root = btrfs_alloc_root(fs_info); | 2109 | if (!tree_root || !chunk_root) { |
2088 | quota_root = fs_info->quota_root = btrfs_alloc_root(fs_info); | ||
2089 | |||
2090 | if (!tree_root || !extent_root || !csum_root || | ||
2091 | !chunk_root || !dev_root || !quota_root) { | ||
2092 | err = -ENOMEM; | 2110 | err = -ENOMEM; |
2093 | goto fail; | 2111 | goto fail; |
2094 | } | 2112 | } |
@@ -2131,9 +2149,9 @@ int open_ctree(struct super_block *sb, | |||
2131 | INIT_LIST_HEAD(&fs_info->trans_list); | 2149 | INIT_LIST_HEAD(&fs_info->trans_list); |
2132 | INIT_LIST_HEAD(&fs_info->dead_roots); | 2150 | INIT_LIST_HEAD(&fs_info->dead_roots); |
2133 | INIT_LIST_HEAD(&fs_info->delayed_iputs); | 2151 | INIT_LIST_HEAD(&fs_info->delayed_iputs); |
2134 | INIT_LIST_HEAD(&fs_info->delalloc_inodes); | 2152 | INIT_LIST_HEAD(&fs_info->delalloc_roots); |
2135 | INIT_LIST_HEAD(&fs_info->caching_block_groups); | 2153 | INIT_LIST_HEAD(&fs_info->caching_block_groups); |
2136 | spin_lock_init(&fs_info->delalloc_lock); | 2154 | spin_lock_init(&fs_info->delalloc_root_lock); |
2137 | spin_lock_init(&fs_info->trans_lock); | 2155 | spin_lock_init(&fs_info->trans_lock); |
2138 | spin_lock_init(&fs_info->fs_roots_radix_lock); | 2156 | spin_lock_init(&fs_info->fs_roots_radix_lock); |
2139 | spin_lock_init(&fs_info->delayed_iput_lock); | 2157 | spin_lock_init(&fs_info->delayed_iput_lock); |
@@ -2169,7 +2187,6 @@ int open_ctree(struct super_block *sb, | |||
2169 | fs_info->max_inline = 8192 * 1024; | 2187 | fs_info->max_inline = 8192 * 1024; |
2170 | fs_info->metadata_ratio = 0; | 2188 | fs_info->metadata_ratio = 0; |
2171 | fs_info->defrag_inodes = RB_ROOT; | 2189 | fs_info->defrag_inodes = RB_ROOT; |
2172 | fs_info->trans_no_join = 0; | ||
2173 | fs_info->free_chunk_space = 0; | 2190 | fs_info->free_chunk_space = 0; |
2174 | fs_info->tree_mod_log = RB_ROOT; | 2191 | fs_info->tree_mod_log = RB_ROOT; |
2175 | 2192 | ||
@@ -2180,8 +2197,8 @@ int open_ctree(struct super_block *sb, | |||
2180 | fs_info->thread_pool_size = min_t(unsigned long, | 2197 | fs_info->thread_pool_size = min_t(unsigned long, |
2181 | num_online_cpus() + 2, 8); | 2198 | num_online_cpus() + 2, 8); |
2182 | 2199 | ||
2183 | INIT_LIST_HEAD(&fs_info->ordered_extents); | 2200 | INIT_LIST_HEAD(&fs_info->ordered_roots); |
2184 | spin_lock_init(&fs_info->ordered_extent_lock); | 2201 | spin_lock_init(&fs_info->ordered_root_lock); |
2185 | fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), | 2202 | fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root), |
2186 | GFP_NOFS); | 2203 | GFP_NOFS); |
2187 | if (!fs_info->delayed_root) { | 2204 | if (!fs_info->delayed_root) { |
@@ -2274,6 +2291,7 @@ int open_ctree(struct super_block *sb, | |||
2274 | fs_info->qgroup_seq = 1; | 2291 | fs_info->qgroup_seq = 1; |
2275 | fs_info->quota_enabled = 0; | 2292 | fs_info->quota_enabled = 0; |
2276 | fs_info->pending_quota_state = 0; | 2293 | fs_info->pending_quota_state = 0; |
2294 | fs_info->qgroup_ulist = NULL; | ||
2277 | mutex_init(&fs_info->qgroup_rescan_lock); | 2295 | mutex_init(&fs_info->qgroup_rescan_lock); |
2278 | 2296 | ||
2279 | btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); | 2297 | btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); |
@@ -2638,33 +2656,44 @@ retry_root_backup: | |||
2638 | btrfs_set_root_node(&tree_root->root_item, tree_root->node); | 2656 | btrfs_set_root_node(&tree_root->root_item, tree_root->node); |
2639 | tree_root->commit_root = btrfs_root_node(tree_root); | 2657 | tree_root->commit_root = btrfs_root_node(tree_root); |
2640 | 2658 | ||
2641 | ret = find_and_setup_root(tree_root, fs_info, | 2659 | location.objectid = BTRFS_EXTENT_TREE_OBJECTID; |
2642 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); | 2660 | location.type = BTRFS_ROOT_ITEM_KEY; |
2643 | if (ret) | 2661 | location.offset = 0; |
2662 | |||
2663 | extent_root = btrfs_read_tree_root(tree_root, &location); | ||
2664 | if (IS_ERR(extent_root)) { | ||
2665 | ret = PTR_ERR(extent_root); | ||
2644 | goto recovery_tree_root; | 2666 | goto recovery_tree_root; |
2667 | } | ||
2645 | extent_root->track_dirty = 1; | 2668 | extent_root->track_dirty = 1; |
2669 | fs_info->extent_root = extent_root; | ||
2646 | 2670 | ||
2647 | ret = find_and_setup_root(tree_root, fs_info, | 2671 | location.objectid = BTRFS_DEV_TREE_OBJECTID; |
2648 | BTRFS_DEV_TREE_OBJECTID, dev_root); | 2672 | dev_root = btrfs_read_tree_root(tree_root, &location); |
2649 | if (ret) | 2673 | if (IS_ERR(dev_root)) { |
2674 | ret = PTR_ERR(dev_root); | ||
2650 | goto recovery_tree_root; | 2675 | goto recovery_tree_root; |
2676 | } | ||
2651 | dev_root->track_dirty = 1; | 2677 | dev_root->track_dirty = 1; |
2678 | fs_info->dev_root = dev_root; | ||
2679 | btrfs_init_devices_late(fs_info); | ||
2652 | 2680 | ||
2653 | ret = find_and_setup_root(tree_root, fs_info, | 2681 | location.objectid = BTRFS_CSUM_TREE_OBJECTID; |
2654 | BTRFS_CSUM_TREE_OBJECTID, csum_root); | 2682 | csum_root = btrfs_read_tree_root(tree_root, &location); |
2655 | if (ret) | 2683 | if (IS_ERR(csum_root)) { |
2684 | ret = PTR_ERR(csum_root); | ||
2656 | goto recovery_tree_root; | 2685 | goto recovery_tree_root; |
2686 | } | ||
2657 | csum_root->track_dirty = 1; | 2687 | csum_root->track_dirty = 1; |
2688 | fs_info->csum_root = csum_root; | ||
2658 | 2689 | ||
2659 | ret = find_and_setup_root(tree_root, fs_info, | 2690 | location.objectid = BTRFS_QUOTA_TREE_OBJECTID; |
2660 | BTRFS_QUOTA_TREE_OBJECTID, quota_root); | 2691 | quota_root = btrfs_read_tree_root(tree_root, &location); |
2661 | if (ret) { | 2692 | if (!IS_ERR(quota_root)) { |
2662 | kfree(quota_root); | ||
2663 | quota_root = fs_info->quota_root = NULL; | ||
2664 | } else { | ||
2665 | quota_root->track_dirty = 1; | 2693 | quota_root->track_dirty = 1; |
2666 | fs_info->quota_enabled = 1; | 2694 | fs_info->quota_enabled = 1; |
2667 | fs_info->pending_quota_state = 1; | 2695 | fs_info->pending_quota_state = 1; |
2696 | fs_info->quota_root = quota_root; | ||
2668 | } | 2697 | } |
2669 | 2698 | ||
2670 | fs_info->generation = generation; | 2699 | fs_info->generation = generation; |
@@ -2817,11 +2846,9 @@ retry_root_backup: | |||
2817 | 2846 | ||
2818 | location.objectid = BTRFS_FS_TREE_OBJECTID; | 2847 | location.objectid = BTRFS_FS_TREE_OBJECTID; |
2819 | location.type = BTRFS_ROOT_ITEM_KEY; | 2848 | location.type = BTRFS_ROOT_ITEM_KEY; |
2820 | location.offset = (u64)-1; | 2849 | location.offset = 0; |
2821 | 2850 | ||
2822 | fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); | 2851 | fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location); |
2823 | if (!fs_info->fs_root) | ||
2824 | goto fail_qgroup; | ||
2825 | if (IS_ERR(fs_info->fs_root)) { | 2852 | if (IS_ERR(fs_info->fs_root)) { |
2826 | err = PTR_ERR(fs_info->fs_root); | 2853 | err = PTR_ERR(fs_info->fs_root); |
2827 | goto fail_qgroup; | 2854 | goto fail_qgroup; |
@@ -2853,6 +2880,8 @@ retry_root_backup: | |||
2853 | return ret; | 2880 | return ret; |
2854 | } | 2881 | } |
2855 | 2882 | ||
2883 | btrfs_qgroup_rescan_resume(fs_info); | ||
2884 | |||
2856 | return 0; | 2885 | return 0; |
2857 | 2886 | ||
2858 | fail_qgroup: | 2887 | fail_qgroup: |
@@ -3258,7 +3287,7 @@ int btrfs_calc_num_tolerated_disk_barrier_failures( | |||
3258 | BTRFS_BLOCK_GROUP_RAID10)) { | 3287 | BTRFS_BLOCK_GROUP_RAID10)) { |
3259 | num_tolerated_disk_barrier_failures = 1; | 3288 | num_tolerated_disk_barrier_failures = 1; |
3260 | } else if (flags & | 3289 | } else if (flags & |
3261 | BTRFS_BLOCK_GROUP_RAID5) { | 3290 | BTRFS_BLOCK_GROUP_RAID6) { |
3262 | num_tolerated_disk_barrier_failures = 2; | 3291 | num_tolerated_disk_barrier_failures = 2; |
3263 | } | 3292 | } |
3264 | } | 3293 | } |
@@ -3366,7 +3395,9 @@ int write_ctree_super(struct btrfs_trans_handle *trans, | |||
3366 | return ret; | 3395 | return ret; |
3367 | } | 3396 | } |
3368 | 3397 | ||
3369 | void btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) | 3398 | /* Drop a fs root from the radix tree and free it. */ |
3399 | void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, | ||
3400 | struct btrfs_root *root) | ||
3370 | { | 3401 | { |
3371 | spin_lock(&fs_info->fs_roots_radix_lock); | 3402 | spin_lock(&fs_info->fs_roots_radix_lock); |
3372 | radix_tree_delete(&fs_info->fs_roots_radix, | 3403 | radix_tree_delete(&fs_info->fs_roots_radix, |
@@ -3397,7 +3428,12 @@ static void free_fs_root(struct btrfs_root *root) | |||
3397 | kfree(root->free_ino_ctl); | 3428 | kfree(root->free_ino_ctl); |
3398 | kfree(root->free_ino_pinned); | 3429 | kfree(root->free_ino_pinned); |
3399 | kfree(root->name); | 3430 | kfree(root->name); |
3400 | kfree(root); | 3431 | btrfs_put_fs_root(root); |
3432 | } | ||
3433 | |||
3434 | void btrfs_free_fs_root(struct btrfs_root *root) | ||
3435 | { | ||
3436 | free_fs_root(root); | ||
3401 | } | 3437 | } |
3402 | 3438 | ||
3403 | int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) | 3439 | int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info) |
@@ -3653,7 +3689,7 @@ static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, | |||
3653 | INIT_LIST_HEAD(&splice); | 3689 | INIT_LIST_HEAD(&splice); |
3654 | 3690 | ||
3655 | mutex_lock(&root->fs_info->ordered_operations_mutex); | 3691 | mutex_lock(&root->fs_info->ordered_operations_mutex); |
3656 | spin_lock(&root->fs_info->ordered_extent_lock); | 3692 | spin_lock(&root->fs_info->ordered_root_lock); |
3657 | 3693 | ||
3658 | list_splice_init(&t->ordered_operations, &splice); | 3694 | list_splice_init(&t->ordered_operations, &splice); |
3659 | while (!list_empty(&splice)) { | 3695 | while (!list_empty(&splice)) { |
@@ -3661,14 +3697,14 @@ static void btrfs_destroy_ordered_operations(struct btrfs_transaction *t, | |||
3661 | ordered_operations); | 3697 | ordered_operations); |
3662 | 3698 | ||
3663 | list_del_init(&btrfs_inode->ordered_operations); | 3699 | list_del_init(&btrfs_inode->ordered_operations); |
3664 | spin_unlock(&root->fs_info->ordered_extent_lock); | 3700 | spin_unlock(&root->fs_info->ordered_root_lock); |
3665 | 3701 | ||
3666 | btrfs_invalidate_inodes(btrfs_inode->root); | 3702 | btrfs_invalidate_inodes(btrfs_inode->root); |
3667 | 3703 | ||
3668 | spin_lock(&root->fs_info->ordered_extent_lock); | 3704 | spin_lock(&root->fs_info->ordered_root_lock); |
3669 | } | 3705 | } |
3670 | 3706 | ||
3671 | spin_unlock(&root->fs_info->ordered_extent_lock); | 3707 | spin_unlock(&root->fs_info->ordered_root_lock); |
3672 | mutex_unlock(&root->fs_info->ordered_operations_mutex); | 3708 | mutex_unlock(&root->fs_info->ordered_operations_mutex); |
3673 | } | 3709 | } |
3674 | 3710 | ||
@@ -3676,15 +3712,36 @@ static void btrfs_destroy_ordered_extents(struct btrfs_root *root) | |||
3676 | { | 3712 | { |
3677 | struct btrfs_ordered_extent *ordered; | 3713 | struct btrfs_ordered_extent *ordered; |
3678 | 3714 | ||
3679 | spin_lock(&root->fs_info->ordered_extent_lock); | 3715 | spin_lock(&root->ordered_extent_lock); |
3680 | /* | 3716 | /* |
3681 | * This will just short circuit the ordered completion stuff which will | 3717 | * This will just short circuit the ordered completion stuff which will |
3682 | * make sure the ordered extent gets properly cleaned up. | 3718 | * make sure the ordered extent gets properly cleaned up. |
3683 | */ | 3719 | */ |
3684 | list_for_each_entry(ordered, &root->fs_info->ordered_extents, | 3720 | list_for_each_entry(ordered, &root->ordered_extents, |
3685 | root_extent_list) | 3721 | root_extent_list) |
3686 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); | 3722 | set_bit(BTRFS_ORDERED_IOERR, &ordered->flags); |
3687 | spin_unlock(&root->fs_info->ordered_extent_lock); | 3723 | spin_unlock(&root->ordered_extent_lock); |
3724 | } | ||
3725 | |||
3726 | static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info) | ||
3727 | { | ||
3728 | struct btrfs_root *root; | ||
3729 | struct list_head splice; | ||
3730 | |||
3731 | INIT_LIST_HEAD(&splice); | ||
3732 | |||
3733 | spin_lock(&fs_info->ordered_root_lock); | ||
3734 | list_splice_init(&fs_info->ordered_roots, &splice); | ||
3735 | while (!list_empty(&splice)) { | ||
3736 | root = list_first_entry(&splice, struct btrfs_root, | ||
3737 | ordered_root); | ||
3738 | list_del_init(&root->ordered_root); | ||
3739 | |||
3740 | btrfs_destroy_ordered_extents(root); | ||
3741 | |||
3742 | cond_resched_lock(&fs_info->ordered_root_lock); | ||
3743 | } | ||
3744 | spin_unlock(&fs_info->ordered_root_lock); | ||
3688 | } | 3745 | } |
3689 | 3746 | ||
3690 | int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, | 3747 | int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, |
@@ -3706,6 +3763,7 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, | |||
3706 | 3763 | ||
3707 | while ((node = rb_first(&delayed_refs->root)) != NULL) { | 3764 | while ((node = rb_first(&delayed_refs->root)) != NULL) { |
3708 | struct btrfs_delayed_ref_head *head = NULL; | 3765 | struct btrfs_delayed_ref_head *head = NULL; |
3766 | bool pin_bytes = false; | ||
3709 | 3767 | ||
3710 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | 3768 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
3711 | atomic_set(&ref->refs, 1); | 3769 | atomic_set(&ref->refs, 1); |
@@ -3726,8 +3784,7 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, | |||
3726 | } | 3784 | } |
3727 | 3785 | ||
3728 | if (head->must_insert_reserved) | 3786 | if (head->must_insert_reserved) |
3729 | btrfs_pin_extent(root, ref->bytenr, | 3787 | pin_bytes = true; |
3730 | ref->num_bytes, 1); | ||
3731 | btrfs_free_delayed_extent_op(head->extent_op); | 3788 | btrfs_free_delayed_extent_op(head->extent_op); |
3732 | delayed_refs->num_heads--; | 3789 | delayed_refs->num_heads--; |
3733 | if (list_empty(&head->cluster)) | 3790 | if (list_empty(&head->cluster)) |
@@ -3738,9 +3795,13 @@ int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, | |||
3738 | ref->in_tree = 0; | 3795 | ref->in_tree = 0; |
3739 | rb_erase(&ref->rb_node, &delayed_refs->root); | 3796 | rb_erase(&ref->rb_node, &delayed_refs->root); |
3740 | delayed_refs->num_entries--; | 3797 | delayed_refs->num_entries--; |
3741 | if (head) | ||
3742 | mutex_unlock(&head->mutex); | ||
3743 | spin_unlock(&delayed_refs->lock); | 3798 | spin_unlock(&delayed_refs->lock); |
3799 | if (head) { | ||
3800 | if (pin_bytes) | ||
3801 | btrfs_pin_extent(root, ref->bytenr, | ||
3802 | ref->num_bytes, 1); | ||
3803 | mutex_unlock(&head->mutex); | ||
3804 | } | ||
3744 | btrfs_put_delayed_ref(ref); | 3805 | btrfs_put_delayed_ref(ref); |
3745 | 3806 | ||
3746 | cond_resched(); | 3807 | cond_resched(); |
@@ -3777,24 +3838,49 @@ static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root) | |||
3777 | 3838 | ||
3778 | INIT_LIST_HEAD(&splice); | 3839 | INIT_LIST_HEAD(&splice); |
3779 | 3840 | ||
3780 | spin_lock(&root->fs_info->delalloc_lock); | 3841 | spin_lock(&root->delalloc_lock); |
3781 | list_splice_init(&root->fs_info->delalloc_inodes, &splice); | 3842 | list_splice_init(&root->delalloc_inodes, &splice); |
3782 | 3843 | ||
3783 | while (!list_empty(&splice)) { | 3844 | while (!list_empty(&splice)) { |
3784 | btrfs_inode = list_entry(splice.next, struct btrfs_inode, | 3845 | btrfs_inode = list_first_entry(&splice, struct btrfs_inode, |
3785 | delalloc_inodes); | 3846 | delalloc_inodes); |
3786 | 3847 | ||
3787 | list_del_init(&btrfs_inode->delalloc_inodes); | 3848 | list_del_init(&btrfs_inode->delalloc_inodes); |
3788 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, | 3849 | clear_bit(BTRFS_INODE_IN_DELALLOC_LIST, |
3789 | &btrfs_inode->runtime_flags); | 3850 | &btrfs_inode->runtime_flags); |
3790 | spin_unlock(&root->fs_info->delalloc_lock); | 3851 | spin_unlock(&root->delalloc_lock); |
3791 | 3852 | ||
3792 | btrfs_invalidate_inodes(btrfs_inode->root); | 3853 | btrfs_invalidate_inodes(btrfs_inode->root); |
3793 | 3854 | ||
3794 | spin_lock(&root->fs_info->delalloc_lock); | 3855 | spin_lock(&root->delalloc_lock); |
3795 | } | 3856 | } |
3796 | 3857 | ||
3797 | spin_unlock(&root->fs_info->delalloc_lock); | 3858 | spin_unlock(&root->delalloc_lock); |
3859 | } | ||
3860 | |||
3861 | static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info) | ||
3862 | { | ||
3863 | struct btrfs_root *root; | ||
3864 | struct list_head splice; | ||
3865 | |||
3866 | INIT_LIST_HEAD(&splice); | ||
3867 | |||
3868 | spin_lock(&fs_info->delalloc_root_lock); | ||
3869 | list_splice_init(&fs_info->delalloc_roots, &splice); | ||
3870 | while (!list_empty(&splice)) { | ||
3871 | root = list_first_entry(&splice, struct btrfs_root, | ||
3872 | delalloc_root); | ||
3873 | list_del_init(&root->delalloc_root); | ||
3874 | root = btrfs_grab_fs_root(root); | ||
3875 | BUG_ON(!root); | ||
3876 | spin_unlock(&fs_info->delalloc_root_lock); | ||
3877 | |||
3878 | btrfs_destroy_delalloc_inodes(root); | ||
3879 | btrfs_put_fs_root(root); | ||
3880 | |||
3881 | spin_lock(&fs_info->delalloc_root_lock); | ||
3882 | } | ||
3883 | spin_unlock(&fs_info->delalloc_root_lock); | ||
3798 | } | 3884 | } |
3799 | 3885 | ||
3800 | static int btrfs_destroy_marked_extents(struct btrfs_root *root, | 3886 | static int btrfs_destroy_marked_extents(struct btrfs_root *root, |
@@ -3878,19 +3964,14 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, | |||
3878 | btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, | 3964 | btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, |
3879 | cur_trans->dirty_pages.dirty_bytes); | 3965 | cur_trans->dirty_pages.dirty_bytes); |
3880 | 3966 | ||
3881 | /* FIXME: cleanup wait for commit */ | 3967 | cur_trans->state = TRANS_STATE_COMMIT_START; |
3882 | cur_trans->in_commit = 1; | ||
3883 | cur_trans->blocked = 1; | ||
3884 | wake_up(&root->fs_info->transaction_blocked_wait); | 3968 | wake_up(&root->fs_info->transaction_blocked_wait); |
3885 | 3969 | ||
3886 | btrfs_evict_pending_snapshots(cur_trans); | 3970 | btrfs_evict_pending_snapshots(cur_trans); |
3887 | 3971 | ||
3888 | cur_trans->blocked = 0; | 3972 | cur_trans->state = TRANS_STATE_UNBLOCKED; |
3889 | wake_up(&root->fs_info->transaction_wait); | 3973 | wake_up(&root->fs_info->transaction_wait); |
3890 | 3974 | ||
3891 | cur_trans->commit_done = 1; | ||
3892 | wake_up(&cur_trans->commit_wait); | ||
3893 | |||
3894 | btrfs_destroy_delayed_inodes(root); | 3975 | btrfs_destroy_delayed_inodes(root); |
3895 | btrfs_assert_delayed_root_empty(root); | 3976 | btrfs_assert_delayed_root_empty(root); |
3896 | 3977 | ||
@@ -3899,6 +3980,9 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, | |||
3899 | btrfs_destroy_pinned_extent(root, | 3980 | btrfs_destroy_pinned_extent(root, |
3900 | root->fs_info->pinned_extents); | 3981 | root->fs_info->pinned_extents); |
3901 | 3982 | ||
3983 | cur_trans->state =TRANS_STATE_COMPLETED; | ||
3984 | wake_up(&cur_trans->commit_wait); | ||
3985 | |||
3902 | /* | 3986 | /* |
3903 | memset(cur_trans, 0, sizeof(*cur_trans)); | 3987 | memset(cur_trans, 0, sizeof(*cur_trans)); |
3904 | kmem_cache_free(btrfs_transaction_cachep, cur_trans); | 3988 | kmem_cache_free(btrfs_transaction_cachep, cur_trans); |
@@ -3914,7 +3998,7 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root) | |||
3914 | 3998 | ||
3915 | spin_lock(&root->fs_info->trans_lock); | 3999 | spin_lock(&root->fs_info->trans_lock); |
3916 | list_splice_init(&root->fs_info->trans_list, &list); | 4000 | list_splice_init(&root->fs_info->trans_list, &list); |
3917 | root->fs_info->trans_no_join = 1; | 4001 | root->fs_info->running_transaction = NULL; |
3918 | spin_unlock(&root->fs_info->trans_lock); | 4002 | spin_unlock(&root->fs_info->trans_lock); |
3919 | 4003 | ||
3920 | while (!list_empty(&list)) { | 4004 | while (!list_empty(&list)) { |
@@ -3922,37 +4006,31 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root) | |||
3922 | 4006 | ||
3923 | btrfs_destroy_ordered_operations(t, root); | 4007 | btrfs_destroy_ordered_operations(t, root); |
3924 | 4008 | ||
3925 | btrfs_destroy_ordered_extents(root); | 4009 | btrfs_destroy_all_ordered_extents(root->fs_info); |
3926 | 4010 | ||
3927 | btrfs_destroy_delayed_refs(t, root); | 4011 | btrfs_destroy_delayed_refs(t, root); |
3928 | 4012 | ||
3929 | /* FIXME: cleanup wait for commit */ | 4013 | /* |
3930 | t->in_commit = 1; | 4014 | * FIXME: cleanup wait for commit |
3931 | t->blocked = 1; | 4015 | * We needn't acquire the lock here, because we are during |
4016 | * the umount, there is no other task which will change it. | ||
4017 | */ | ||
4018 | t->state = TRANS_STATE_COMMIT_START; | ||
3932 | smp_mb(); | 4019 | smp_mb(); |
3933 | if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) | 4020 | if (waitqueue_active(&root->fs_info->transaction_blocked_wait)) |
3934 | wake_up(&root->fs_info->transaction_blocked_wait); | 4021 | wake_up(&root->fs_info->transaction_blocked_wait); |
3935 | 4022 | ||
3936 | btrfs_evict_pending_snapshots(t); | 4023 | btrfs_evict_pending_snapshots(t); |
3937 | 4024 | ||
3938 | t->blocked = 0; | 4025 | t->state = TRANS_STATE_UNBLOCKED; |
3939 | smp_mb(); | 4026 | smp_mb(); |
3940 | if (waitqueue_active(&root->fs_info->transaction_wait)) | 4027 | if (waitqueue_active(&root->fs_info->transaction_wait)) |
3941 | wake_up(&root->fs_info->transaction_wait); | 4028 | wake_up(&root->fs_info->transaction_wait); |
3942 | 4029 | ||
3943 | t->commit_done = 1; | ||
3944 | smp_mb(); | ||
3945 | if (waitqueue_active(&t->commit_wait)) | ||
3946 | wake_up(&t->commit_wait); | ||
3947 | |||
3948 | btrfs_destroy_delayed_inodes(root); | 4030 | btrfs_destroy_delayed_inodes(root); |
3949 | btrfs_assert_delayed_root_empty(root); | 4031 | btrfs_assert_delayed_root_empty(root); |
3950 | 4032 | ||
3951 | btrfs_destroy_delalloc_inodes(root); | 4033 | btrfs_destroy_all_delalloc_inodes(root->fs_info); |
3952 | |||
3953 | spin_lock(&root->fs_info->trans_lock); | ||
3954 | root->fs_info->running_transaction = NULL; | ||
3955 | spin_unlock(&root->fs_info->trans_lock); | ||
3956 | 4034 | ||
3957 | btrfs_destroy_marked_extents(root, &t->dirty_pages, | 4035 | btrfs_destroy_marked_extents(root, &t->dirty_pages, |
3958 | EXTENT_DIRTY); | 4036 | EXTENT_DIRTY); |
@@ -3960,15 +4038,17 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root) | |||
3960 | btrfs_destroy_pinned_extent(root, | 4038 | btrfs_destroy_pinned_extent(root, |
3961 | root->fs_info->pinned_extents); | 4039 | root->fs_info->pinned_extents); |
3962 | 4040 | ||
4041 | t->state = TRANS_STATE_COMPLETED; | ||
4042 | smp_mb(); | ||
4043 | if (waitqueue_active(&t->commit_wait)) | ||
4044 | wake_up(&t->commit_wait); | ||
4045 | |||
3963 | atomic_set(&t->use_count, 0); | 4046 | atomic_set(&t->use_count, 0); |
3964 | list_del_init(&t->list); | 4047 | list_del_init(&t->list); |
3965 | memset(t, 0, sizeof(*t)); | 4048 | memset(t, 0, sizeof(*t)); |
3966 | kmem_cache_free(btrfs_transaction_cachep, t); | 4049 | kmem_cache_free(btrfs_transaction_cachep, t); |
3967 | } | 4050 | } |
3968 | 4051 | ||
3969 | spin_lock(&root->fs_info->trans_lock); | ||
3970 | root->fs_info->trans_no_join = 0; | ||
3971 | spin_unlock(&root->fs_info->trans_lock); | ||
3972 | mutex_unlock(&root->fs_info->transaction_kthread_mutex); | 4052 | mutex_unlock(&root->fs_info->transaction_kthread_mutex); |
3973 | 4053 | ||
3974 | return 0; | 4054 | return 0; |