aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/disk-io.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/disk-io.c')
-rw-r--r--fs/btrfs/disk-io.c283
1 files changed, 184 insertions, 99 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 8b8192790011..02b6afbd7450 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -41,6 +41,7 @@
41 41
42static struct extent_io_ops btree_extent_io_ops; 42static struct extent_io_ops btree_extent_io_ops;
43static void end_workqueue_fn(struct btrfs_work *work); 43static void end_workqueue_fn(struct btrfs_work *work);
44static void free_fs_root(struct btrfs_root *root);
44 45
45static atomic_t btrfs_bdi_num = ATOMIC_INIT(0); 46static atomic_t btrfs_bdi_num = ATOMIC_INIT(0);
46 47
@@ -123,15 +124,15 @@ static struct extent_map *btree_get_extent(struct inode *inode,
123 struct extent_map *em; 124 struct extent_map *em;
124 int ret; 125 int ret;
125 126
126 spin_lock(&em_tree->lock); 127 read_lock(&em_tree->lock);
127 em = lookup_extent_mapping(em_tree, start, len); 128 em = lookup_extent_mapping(em_tree, start, len);
128 if (em) { 129 if (em) {
129 em->bdev = 130 em->bdev =
130 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 131 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
131 spin_unlock(&em_tree->lock); 132 read_unlock(&em_tree->lock);
132 goto out; 133 goto out;
133 } 134 }
134 spin_unlock(&em_tree->lock); 135 read_unlock(&em_tree->lock);
135 136
136 em = alloc_extent_map(GFP_NOFS); 137 em = alloc_extent_map(GFP_NOFS);
137 if (!em) { 138 if (!em) {
@@ -144,7 +145,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
144 em->block_start = 0; 145 em->block_start = 0;
145 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 146 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
146 147
147 spin_lock(&em_tree->lock); 148 write_lock(&em_tree->lock);
148 ret = add_extent_mapping(em_tree, em); 149 ret = add_extent_mapping(em_tree, em);
149 if (ret == -EEXIST) { 150 if (ret == -EEXIST) {
150 u64 failed_start = em->start; 151 u64 failed_start = em->start;
@@ -163,7 +164,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
163 free_extent_map(em); 164 free_extent_map(em);
164 em = NULL; 165 em = NULL;
165 } 166 }
166 spin_unlock(&em_tree->lock); 167 write_unlock(&em_tree->lock);
167 168
168 if (ret) 169 if (ret)
169 em = ERR_PTR(ret); 170 em = ERR_PTR(ret);
@@ -772,7 +773,7 @@ static void btree_invalidatepage(struct page *page, unsigned long offset)
772 } 773 }
773} 774}
774 775
775static struct address_space_operations btree_aops = { 776static const struct address_space_operations btree_aops = {
776 .readpage = btree_readpage, 777 .readpage = btree_readpage,
777 .writepage = btree_writepage, 778 .writepage = btree_writepage,
778 .writepages = btree_writepages, 779 .writepages = btree_writepages,
@@ -821,14 +822,14 @@ struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
821 822
822int btrfs_write_tree_block(struct extent_buffer *buf) 823int btrfs_write_tree_block(struct extent_buffer *buf)
823{ 824{
824 return btrfs_fdatawrite_range(buf->first_page->mapping, buf->start, 825 return filemap_fdatawrite_range(buf->first_page->mapping, buf->start,
825 buf->start + buf->len - 1, WB_SYNC_ALL); 826 buf->start + buf->len - 1);
826} 827}
827 828
828int btrfs_wait_tree_block_writeback(struct extent_buffer *buf) 829int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
829{ 830{
830 return btrfs_wait_on_page_writeback_range(buf->first_page->mapping, 831 return filemap_fdatawait_range(buf->first_page->mapping,
831 buf->start, buf->start + buf->len - 1); 832 buf->start, buf->start + buf->len - 1);
832} 833}
833 834
834struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 835struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
@@ -895,8 +896,7 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
895 root->fs_info = fs_info; 896 root->fs_info = fs_info;
896 root->objectid = objectid; 897 root->objectid = objectid;
897 root->last_trans = 0; 898 root->last_trans = 0;
898 root->highest_inode = 0; 899 root->highest_objectid = 0;
899 root->last_inode_alloc = 0;
900 root->name = NULL; 900 root->name = NULL;
901 root->in_sysfs = 0; 901 root->in_sysfs = 0;
902 root->inode_tree.rb_node = NULL; 902 root->inode_tree.rb_node = NULL;
@@ -917,6 +917,7 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
917 atomic_set(&root->log_writers, 0); 917 atomic_set(&root->log_writers, 0);
918 root->log_batch = 0; 918 root->log_batch = 0;
919 root->log_transid = 0; 919 root->log_transid = 0;
920 root->last_log_commit = 0;
920 extent_io_tree_init(&root->dirty_log_pages, 921 extent_io_tree_init(&root->dirty_log_pages,
921 fs_info->btree_inode->i_mapping, GFP_NOFS); 922 fs_info->btree_inode->i_mapping, GFP_NOFS);
922 923
@@ -952,14 +953,16 @@ static int find_and_setup_root(struct btrfs_root *tree_root,
952 root, fs_info, objectid); 953 root, fs_info, objectid);
953 ret = btrfs_find_last_root(tree_root, objectid, 954 ret = btrfs_find_last_root(tree_root, objectid,
954 &root->root_item, &root->root_key); 955 &root->root_item, &root->root_key);
956 if (ret > 0)
957 return -ENOENT;
955 BUG_ON(ret); 958 BUG_ON(ret);
956 959
957 generation = btrfs_root_generation(&root->root_item); 960 generation = btrfs_root_generation(&root->root_item);
958 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 961 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
959 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 962 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
960 blocksize, generation); 963 blocksize, generation);
961 root->commit_root = btrfs_root_node(root);
962 BUG_ON(!root->node); 964 BUG_ON(!root->node);
965 root->commit_root = btrfs_root_node(root);
963 return 0; 966 return 0;
964} 967}
965 968
@@ -1085,6 +1088,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1085 WARN_ON(root->log_root); 1088 WARN_ON(root->log_root);
1086 root->log_root = log_root; 1089 root->log_root = log_root;
1087 root->log_transid = 0; 1090 root->log_transid = 0;
1091 root->last_log_commit = 0;
1088 return 0; 1092 return 0;
1089} 1093}
1090 1094
@@ -1095,7 +1099,6 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1095 struct btrfs_fs_info *fs_info = tree_root->fs_info; 1099 struct btrfs_fs_info *fs_info = tree_root->fs_info;
1096 struct btrfs_path *path; 1100 struct btrfs_path *path;
1097 struct extent_buffer *l; 1101 struct extent_buffer *l;
1098 u64 highest_inode;
1099 u64 generation; 1102 u64 generation;
1100 u32 blocksize; 1103 u32 blocksize;
1101 int ret = 0; 1104 int ret = 0;
@@ -1110,7 +1113,7 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1110 kfree(root); 1113 kfree(root);
1111 return ERR_PTR(ret); 1114 return ERR_PTR(ret);
1112 } 1115 }
1113 goto insert; 1116 goto out;
1114 } 1117 }
1115 1118
1116 __setup_root(tree_root->nodesize, tree_root->leafsize, 1119 __setup_root(tree_root->nodesize, tree_root->leafsize,
@@ -1120,39 +1123,30 @@ struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_root *tree_root,
1120 path = btrfs_alloc_path(); 1123 path = btrfs_alloc_path();
1121 BUG_ON(!path); 1124 BUG_ON(!path);
1122 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 1125 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
1123 if (ret != 0) { 1126 if (ret == 0) {
1124 if (ret > 0) 1127 l = path->nodes[0];
1125 ret = -ENOENT; 1128 read_extent_buffer(l, &root->root_item,
1126 goto out; 1129 btrfs_item_ptr_offset(l, path->slots[0]),
1130 sizeof(root->root_item));
1131 memcpy(&root->root_key, location, sizeof(*location));
1127 } 1132 }
1128 l = path->nodes[0];
1129 read_extent_buffer(l, &root->root_item,
1130 btrfs_item_ptr_offset(l, path->slots[0]),
1131 sizeof(root->root_item));
1132 memcpy(&root->root_key, location, sizeof(*location));
1133 ret = 0;
1134out:
1135 btrfs_release_path(root, path);
1136 btrfs_free_path(path); 1133 btrfs_free_path(path);
1137 if (ret) { 1134 if (ret) {
1138 kfree(root); 1135 if (ret > 0)
1136 ret = -ENOENT;
1139 return ERR_PTR(ret); 1137 return ERR_PTR(ret);
1140 } 1138 }
1139
1141 generation = btrfs_root_generation(&root->root_item); 1140 generation = btrfs_root_generation(&root->root_item);
1142 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item)); 1141 blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
1143 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1142 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
1144 blocksize, generation); 1143 blocksize, generation);
1145 root->commit_root = btrfs_root_node(root); 1144 root->commit_root = btrfs_root_node(root);
1146 BUG_ON(!root->node); 1145 BUG_ON(!root->node);
1147insert: 1146out:
1148 if (location->objectid != BTRFS_TREE_LOG_OBJECTID) { 1147 if (location->objectid != BTRFS_TREE_LOG_OBJECTID)
1149 root->ref_cows = 1; 1148 root->ref_cows = 1;
1150 ret = btrfs_find_highest_inode(root, &highest_inode); 1149
1151 if (ret == 0) {
1152 root->highest_inode = highest_inode;
1153 root->last_inode_alloc = highest_inode;
1154 }
1155 }
1156 return root; 1150 return root;
1157} 1151}
1158 1152
@@ -1187,39 +1181,66 @@ struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
1187 return fs_info->dev_root; 1181 return fs_info->dev_root;
1188 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID) 1182 if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1189 return fs_info->csum_root; 1183 return fs_info->csum_root;
1190 1184again:
1185 spin_lock(&fs_info->fs_roots_radix_lock);
1191 root = radix_tree_lookup(&fs_info->fs_roots_radix, 1186 root = radix_tree_lookup(&fs_info->fs_roots_radix,
1192 (unsigned long)location->objectid); 1187 (unsigned long)location->objectid);
1188 spin_unlock(&fs_info->fs_roots_radix_lock);
1193 if (root) 1189 if (root)
1194 return root; 1190 return root;
1195 1191
1192 ret = btrfs_find_orphan_item(fs_info->tree_root, location->objectid);
1193 if (ret == 0)
1194 ret = -ENOENT;
1195 if (ret < 0)
1196 return ERR_PTR(ret);
1197
1196 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location); 1198 root = btrfs_read_fs_root_no_radix(fs_info->tree_root, location);
1197 if (IS_ERR(root)) 1199 if (IS_ERR(root))
1198 return root; 1200 return root;
1199 1201
1202 WARN_ON(btrfs_root_refs(&root->root_item) == 0);
1200 set_anon_super(&root->anon_super, NULL); 1203 set_anon_super(&root->anon_super, NULL);
1201 1204
1205 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
1206 if (ret)
1207 goto fail;
1208
1209 spin_lock(&fs_info->fs_roots_radix_lock);
1202 ret = radix_tree_insert(&fs_info->fs_roots_radix, 1210 ret = radix_tree_insert(&fs_info->fs_roots_radix,
1203 (unsigned long)root->root_key.objectid, 1211 (unsigned long)root->root_key.objectid,
1204 root); 1212 root);
1213 if (ret == 0)
1214 root->in_radix = 1;
1215 spin_unlock(&fs_info->fs_roots_radix_lock);
1216 radix_tree_preload_end();
1205 if (ret) { 1217 if (ret) {
1206 free_extent_buffer(root->node); 1218 if (ret == -EEXIST) {
1207 kfree(root); 1219 free_fs_root(root);
1208 return ERR_PTR(ret); 1220 goto again;
1221 }
1222 goto fail;
1209 } 1223 }
1210 if (!(fs_info->sb->s_flags & MS_RDONLY)) { 1224
1211 ret = btrfs_find_dead_roots(fs_info->tree_root, 1225 ret = btrfs_find_dead_roots(fs_info->tree_root,
1212 root->root_key.objectid); 1226 root->root_key.objectid);
1213 BUG_ON(ret); 1227 WARN_ON(ret);
1228
1229 if (!(fs_info->sb->s_flags & MS_RDONLY))
1214 btrfs_orphan_cleanup(root); 1230 btrfs_orphan_cleanup(root);
1215 } 1231
1216 return root; 1232 return root;
1233fail:
1234 free_fs_root(root);
1235 return ERR_PTR(ret);
1217} 1236}
1218 1237
1219struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info, 1238struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1220 struct btrfs_key *location, 1239 struct btrfs_key *location,
1221 const char *name, int namelen) 1240 const char *name, int namelen)
1222{ 1241{
1242 return btrfs_read_fs_root_no_name(fs_info, location);
1243#if 0
1223 struct btrfs_root *root; 1244 struct btrfs_root *root;
1224 int ret; 1245 int ret;
1225 1246
@@ -1236,7 +1257,7 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1236 kfree(root); 1257 kfree(root);
1237 return ERR_PTR(ret); 1258 return ERR_PTR(ret);
1238 } 1259 }
1239#if 0 1260
1240 ret = btrfs_sysfs_add_root(root); 1261 ret = btrfs_sysfs_add_root(root);
1241 if (ret) { 1262 if (ret) {
1242 free_extent_buffer(root->node); 1263 free_extent_buffer(root->node);
@@ -1244,9 +1265,9 @@ struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
1244 kfree(root); 1265 kfree(root);
1245 return ERR_PTR(ret); 1266 return ERR_PTR(ret);
1246 } 1267 }
1247#endif
1248 root->in_sysfs = 1; 1268 root->in_sysfs = 1;
1249 return root; 1269 return root;
1270#endif
1250} 1271}
1251 1272
1252static int btrfs_congested_fn(void *congested_data, int bdi_bits) 1273static int btrfs_congested_fn(void *congested_data, int bdi_bits)
@@ -1325,9 +1346,9 @@ static void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1325 offset = page_offset(page); 1346 offset = page_offset(page);
1326 1347
1327 em_tree = &BTRFS_I(inode)->extent_tree; 1348 em_tree = &BTRFS_I(inode)->extent_tree;
1328 spin_lock(&em_tree->lock); 1349 read_lock(&em_tree->lock);
1329 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); 1350 em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE);
1330 spin_unlock(&em_tree->lock); 1351 read_unlock(&em_tree->lock);
1331 if (!em) { 1352 if (!em) {
1332 __unplug_io_fn(bdi, page); 1353 __unplug_io_fn(bdi, page);
1333 return; 1354 return;
@@ -1360,8 +1381,10 @@ static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
1360 1381
1361 err = bdi_register(bdi, NULL, "btrfs-%d", 1382 err = bdi_register(bdi, NULL, "btrfs-%d",
1362 atomic_inc_return(&btrfs_bdi_num)); 1383 atomic_inc_return(&btrfs_bdi_num));
1363 if (err) 1384 if (err) {
1385 bdi_destroy(bdi);
1364 return err; 1386 return err;
1387 }
1365 1388
1366 bdi->ra_pages = default_backing_dev_info.ra_pages; 1389 bdi->ra_pages = default_backing_dev_info.ra_pages;
1367 bdi->unplug_io_fn = btrfs_unplug_io_fn; 1390 bdi->unplug_io_fn = btrfs_unplug_io_fn;
@@ -1451,9 +1474,12 @@ static int cleaner_kthread(void *arg)
1451 break; 1474 break;
1452 1475
1453 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1476 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE);
1454 mutex_lock(&root->fs_info->cleaner_mutex); 1477
1455 btrfs_clean_old_snapshots(root); 1478 if (!(root->fs_info->sb->s_flags & MS_RDONLY) &&
1456 mutex_unlock(&root->fs_info->cleaner_mutex); 1479 mutex_trylock(&root->fs_info->cleaner_mutex)) {
1480 btrfs_clean_old_snapshots(root);
1481 mutex_unlock(&root->fs_info->cleaner_mutex);
1482 }
1457 1483
1458 if (freezing(current)) { 1484 if (freezing(current)) {
1459 refrigerator(); 1485 refrigerator();
@@ -1558,15 +1584,36 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1558 err = -ENOMEM; 1584 err = -ENOMEM;
1559 goto fail; 1585 goto fail;
1560 } 1586 }
1561 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS); 1587
1588 ret = init_srcu_struct(&fs_info->subvol_srcu);
1589 if (ret) {
1590 err = ret;
1591 goto fail;
1592 }
1593
1594 ret = setup_bdi(fs_info, &fs_info->bdi);
1595 if (ret) {
1596 err = ret;
1597 goto fail_srcu;
1598 }
1599
1600 fs_info->btree_inode = new_inode(sb);
1601 if (!fs_info->btree_inode) {
1602 err = -ENOMEM;
1603 goto fail_bdi;
1604 }
1605
1606 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
1562 INIT_LIST_HEAD(&fs_info->trans_list); 1607 INIT_LIST_HEAD(&fs_info->trans_list);
1563 INIT_LIST_HEAD(&fs_info->dead_roots); 1608 INIT_LIST_HEAD(&fs_info->dead_roots);
1564 INIT_LIST_HEAD(&fs_info->hashers); 1609 INIT_LIST_HEAD(&fs_info->hashers);
1565 INIT_LIST_HEAD(&fs_info->delalloc_inodes); 1610 INIT_LIST_HEAD(&fs_info->delalloc_inodes);
1566 INIT_LIST_HEAD(&fs_info->ordered_operations); 1611 INIT_LIST_HEAD(&fs_info->ordered_operations);
1612 INIT_LIST_HEAD(&fs_info->caching_block_groups);
1567 spin_lock_init(&fs_info->delalloc_lock); 1613 spin_lock_init(&fs_info->delalloc_lock);
1568 spin_lock_init(&fs_info->new_trans_lock); 1614 spin_lock_init(&fs_info->new_trans_lock);
1569 spin_lock_init(&fs_info->ref_cache_lock); 1615 spin_lock_init(&fs_info->ref_cache_lock);
1616 spin_lock_init(&fs_info->fs_roots_radix_lock);
1570 1617
1571 init_completion(&fs_info->kobj_unregister); 1618 init_completion(&fs_info->kobj_unregister);
1572 fs_info->tree_root = tree_root; 1619 fs_info->tree_root = tree_root;
@@ -1585,12 +1632,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1585 fs_info->sb = sb; 1632 fs_info->sb = sb;
1586 fs_info->max_extent = (u64)-1; 1633 fs_info->max_extent = (u64)-1;
1587 fs_info->max_inline = 8192 * 1024; 1634 fs_info->max_inline = 8192 * 1024;
1588 if (setup_bdi(fs_info, &fs_info->bdi)) 1635 fs_info->metadata_ratio = 0;
1589 goto fail_bdi;
1590 fs_info->btree_inode = new_inode(sb);
1591 fs_info->btree_inode->i_ino = 1;
1592 fs_info->btree_inode->i_nlink = 1;
1593 fs_info->metadata_ratio = 8;
1594 1636
1595 fs_info->thread_pool_size = min_t(unsigned long, 1637 fs_info->thread_pool_size = min_t(unsigned long,
1596 num_online_cpus() + 2, 8); 1638 num_online_cpus() + 2, 8);
@@ -1602,6 +1644,8 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1602 sb->s_blocksize_bits = blksize_bits(4096); 1644 sb->s_blocksize_bits = blksize_bits(4096);
1603 sb->s_bdi = &fs_info->bdi; 1645 sb->s_bdi = &fs_info->bdi;
1604 1646
1647 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
1648 fs_info->btree_inode->i_nlink = 1;
1605 /* 1649 /*
1606 * we set the i_size on the btree inode to the max possible int. 1650 * we set the i_size on the btree inode to the max possible int.
1607 * the real end of the address space is determined by all of 1651 * the real end of the address space is determined by all of
@@ -1620,28 +1664,32 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1620 1664
1621 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 1665 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
1622 1666
1667 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1668 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1669 sizeof(struct btrfs_key));
1670 BTRFS_I(fs_info->btree_inode)->dummy_inode = 1;
1671 insert_inode_hash(fs_info->btree_inode);
1672
1623 spin_lock_init(&fs_info->block_group_cache_lock); 1673 spin_lock_init(&fs_info->block_group_cache_lock);
1624 fs_info->block_group_cache_tree.rb_node = NULL; 1674 fs_info->block_group_cache_tree.rb_node = NULL;
1625 1675
1626 extent_io_tree_init(&fs_info->pinned_extents, 1676 extent_io_tree_init(&fs_info->freed_extents[0],
1627 fs_info->btree_inode->i_mapping, GFP_NOFS); 1677 fs_info->btree_inode->i_mapping, GFP_NOFS);
1678 extent_io_tree_init(&fs_info->freed_extents[1],
1679 fs_info->btree_inode->i_mapping, GFP_NOFS);
1680 fs_info->pinned_extents = &fs_info->freed_extents[0];
1628 fs_info->do_barriers = 1; 1681 fs_info->do_barriers = 1;
1629 1682
1630 BTRFS_I(fs_info->btree_inode)->root = tree_root;
1631 memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
1632 sizeof(struct btrfs_key));
1633 insert_inode_hash(fs_info->btree_inode);
1634 1683
1635 mutex_init(&fs_info->trans_mutex); 1684 mutex_init(&fs_info->trans_mutex);
1636 mutex_init(&fs_info->ordered_operations_mutex); 1685 mutex_init(&fs_info->ordered_operations_mutex);
1637 mutex_init(&fs_info->tree_log_mutex); 1686 mutex_init(&fs_info->tree_log_mutex);
1638 mutex_init(&fs_info->drop_mutex);
1639 mutex_init(&fs_info->chunk_mutex); 1687 mutex_init(&fs_info->chunk_mutex);
1640 mutex_init(&fs_info->transaction_kthread_mutex); 1688 mutex_init(&fs_info->transaction_kthread_mutex);
1641 mutex_init(&fs_info->cleaner_mutex); 1689 mutex_init(&fs_info->cleaner_mutex);
1642 mutex_init(&fs_info->volume_mutex); 1690 mutex_init(&fs_info->volume_mutex);
1643 mutex_init(&fs_info->tree_reloc_mutex);
1644 init_rwsem(&fs_info->extent_commit_sem); 1691 init_rwsem(&fs_info->extent_commit_sem);
1692 init_rwsem(&fs_info->subvol_sem);
1645 1693
1646 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster); 1694 btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
1647 btrfs_init_free_cluster(&fs_info->data_alloc_cluster); 1695 btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
@@ -1701,20 +1749,24 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1701 goto fail_iput; 1749 goto fail_iput;
1702 } 1750 }
1703 1751
1704 /* 1752 btrfs_init_workers(&fs_info->generic_worker,
1705 * we need to start all the end_io workers up front because the 1753 "genwork", 1, NULL);
1706 * queue work function gets called at interrupt time, and so it 1754
1707 * cannot dynamically grow.
1708 */
1709 btrfs_init_workers(&fs_info->workers, "worker", 1755 btrfs_init_workers(&fs_info->workers, "worker",
1710 fs_info->thread_pool_size); 1756 fs_info->thread_pool_size,
1757 &fs_info->generic_worker);
1711 1758
1712 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc", 1759 btrfs_init_workers(&fs_info->delalloc_workers, "delalloc",
1713 fs_info->thread_pool_size); 1760 fs_info->thread_pool_size,
1761 &fs_info->generic_worker);
1714 1762
1715 btrfs_init_workers(&fs_info->submit_workers, "submit", 1763 btrfs_init_workers(&fs_info->submit_workers, "submit",
1716 min_t(u64, fs_devices->num_devices, 1764 min_t(u64, fs_devices->num_devices,
1717 fs_info->thread_pool_size)); 1765 fs_info->thread_pool_size),
1766 &fs_info->generic_worker);
1767 btrfs_init_workers(&fs_info->enospc_workers, "enospc",
1768 fs_info->thread_pool_size,
1769 &fs_info->generic_worker);
1718 1770
1719 /* a higher idle thresh on the submit workers makes it much more 1771 /* a higher idle thresh on the submit workers makes it much more
1720 * likely that bios will be send down in a sane order to the 1772 * likely that bios will be send down in a sane order to the
@@ -1728,15 +1780,20 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1728 fs_info->delalloc_workers.idle_thresh = 2; 1780 fs_info->delalloc_workers.idle_thresh = 2;
1729 fs_info->delalloc_workers.ordered = 1; 1781 fs_info->delalloc_workers.ordered = 1;
1730 1782
1731 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1); 1783 btrfs_init_workers(&fs_info->fixup_workers, "fixup", 1,
1784 &fs_info->generic_worker);
1732 btrfs_init_workers(&fs_info->endio_workers, "endio", 1785 btrfs_init_workers(&fs_info->endio_workers, "endio",
1733 fs_info->thread_pool_size); 1786 fs_info->thread_pool_size,
1787 &fs_info->generic_worker);
1734 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta", 1788 btrfs_init_workers(&fs_info->endio_meta_workers, "endio-meta",
1735 fs_info->thread_pool_size); 1789 fs_info->thread_pool_size,
1790 &fs_info->generic_worker);
1736 btrfs_init_workers(&fs_info->endio_meta_write_workers, 1791 btrfs_init_workers(&fs_info->endio_meta_write_workers,
1737 "endio-meta-write", fs_info->thread_pool_size); 1792 "endio-meta-write", fs_info->thread_pool_size,
1793 &fs_info->generic_worker);
1738 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write", 1794 btrfs_init_workers(&fs_info->endio_write_workers, "endio-write",
1739 fs_info->thread_pool_size); 1795 fs_info->thread_pool_size,
1796 &fs_info->generic_worker);
1740 1797
1741 /* 1798 /*
1742 * endios are largely parallel and should have a very 1799 * endios are largely parallel and should have a very
@@ -1745,20 +1802,19 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1745 fs_info->endio_workers.idle_thresh = 4; 1802 fs_info->endio_workers.idle_thresh = 4;
1746 fs_info->endio_meta_workers.idle_thresh = 4; 1803 fs_info->endio_meta_workers.idle_thresh = 4;
1747 1804
1748 fs_info->endio_write_workers.idle_thresh = 64; 1805 fs_info->endio_write_workers.idle_thresh = 2;
1749 fs_info->endio_meta_write_workers.idle_thresh = 64; 1806 fs_info->endio_meta_write_workers.idle_thresh = 2;
1750 1807
1751 btrfs_start_workers(&fs_info->workers, 1); 1808 btrfs_start_workers(&fs_info->workers, 1);
1809 btrfs_start_workers(&fs_info->generic_worker, 1);
1752 btrfs_start_workers(&fs_info->submit_workers, 1); 1810 btrfs_start_workers(&fs_info->submit_workers, 1);
1753 btrfs_start_workers(&fs_info->delalloc_workers, 1); 1811 btrfs_start_workers(&fs_info->delalloc_workers, 1);
1754 btrfs_start_workers(&fs_info->fixup_workers, 1); 1812 btrfs_start_workers(&fs_info->fixup_workers, 1);
1755 btrfs_start_workers(&fs_info->endio_workers, fs_info->thread_pool_size); 1813 btrfs_start_workers(&fs_info->endio_workers, 1);
1756 btrfs_start_workers(&fs_info->endio_meta_workers, 1814 btrfs_start_workers(&fs_info->endio_meta_workers, 1);
1757 fs_info->thread_pool_size); 1815 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1);
1758 btrfs_start_workers(&fs_info->endio_meta_write_workers, 1816 btrfs_start_workers(&fs_info->endio_write_workers, 1);
1759 fs_info->thread_pool_size); 1817 btrfs_start_workers(&fs_info->enospc_workers, 1);
1760 btrfs_start_workers(&fs_info->endio_write_workers,
1761 fs_info->thread_pool_size);
1762 1818
1763 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super); 1819 fs_info->bdi.ra_pages *= btrfs_super_num_devices(disk_super);
1764 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages, 1820 fs_info->bdi.ra_pages = max(fs_info->bdi.ra_pages,
@@ -1918,6 +1974,9 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1918 } 1974 }
1919 } 1975 }
1920 1976
1977 ret = btrfs_find_orphan_roots(tree_root);
1978 BUG_ON(ret);
1979
1921 if (!(sb->s_flags & MS_RDONLY)) { 1980 if (!(sb->s_flags & MS_RDONLY)) {
1922 ret = btrfs_recover_relocation(tree_root); 1981 ret = btrfs_recover_relocation(tree_root);
1923 BUG_ON(ret); 1982 BUG_ON(ret);
@@ -1961,6 +2020,7 @@ fail_chunk_root:
1961 free_extent_buffer(chunk_root->node); 2020 free_extent_buffer(chunk_root->node);
1962 free_extent_buffer(chunk_root->commit_root); 2021 free_extent_buffer(chunk_root->commit_root);
1963fail_sb_buffer: 2022fail_sb_buffer:
2023 btrfs_stop_workers(&fs_info->generic_worker);
1964 btrfs_stop_workers(&fs_info->fixup_workers); 2024 btrfs_stop_workers(&fs_info->fixup_workers);
1965 btrfs_stop_workers(&fs_info->delalloc_workers); 2025 btrfs_stop_workers(&fs_info->delalloc_workers);
1966 btrfs_stop_workers(&fs_info->workers); 2026 btrfs_stop_workers(&fs_info->workers);
@@ -1969,6 +2029,7 @@ fail_sb_buffer:
1969 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2029 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
1970 btrfs_stop_workers(&fs_info->endio_write_workers); 2030 btrfs_stop_workers(&fs_info->endio_write_workers);
1971 btrfs_stop_workers(&fs_info->submit_workers); 2031 btrfs_stop_workers(&fs_info->submit_workers);
2032 btrfs_stop_workers(&fs_info->enospc_workers);
1972fail_iput: 2033fail_iput:
1973 invalidate_inode_pages2(fs_info->btree_inode->i_mapping); 2034 invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
1974 iput(fs_info->btree_inode); 2035 iput(fs_info->btree_inode);
@@ -1977,6 +2038,8 @@ fail_iput:
1977 btrfs_mapping_tree_free(&fs_info->mapping_tree); 2038 btrfs_mapping_tree_free(&fs_info->mapping_tree);
1978fail_bdi: 2039fail_bdi:
1979 bdi_destroy(&fs_info->bdi); 2040 bdi_destroy(&fs_info->bdi);
2041fail_srcu:
2042 cleanup_srcu_struct(&fs_info->subvol_srcu);
1980fail: 2043fail:
1981 kfree(extent_root); 2044 kfree(extent_root);
1982 kfree(tree_root); 2045 kfree(tree_root);
@@ -2236,20 +2299,29 @@ int write_ctree_super(struct btrfs_trans_handle *trans,
2236 2299
2237int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root) 2300int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2238{ 2301{
2239 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 2302 spin_lock(&fs_info->fs_roots_radix_lock);
2240 radix_tree_delete(&fs_info->fs_roots_radix, 2303 radix_tree_delete(&fs_info->fs_roots_radix,
2241 (unsigned long)root->root_key.objectid); 2304 (unsigned long)root->root_key.objectid);
2305 spin_unlock(&fs_info->fs_roots_radix_lock);
2306
2307 if (btrfs_root_refs(&root->root_item) == 0)
2308 synchronize_srcu(&fs_info->subvol_srcu);
2309
2310 free_fs_root(root);
2311 return 0;
2312}
2313
2314static void free_fs_root(struct btrfs_root *root)
2315{
2316 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
2242 if (root->anon_super.s_dev) { 2317 if (root->anon_super.s_dev) {
2243 down_write(&root->anon_super.s_umount); 2318 down_write(&root->anon_super.s_umount);
2244 kill_anon_super(&root->anon_super); 2319 kill_anon_super(&root->anon_super);
2245 } 2320 }
2246 if (root->node) 2321 free_extent_buffer(root->node);
2247 free_extent_buffer(root->node); 2322 free_extent_buffer(root->commit_root);
2248 if (root->commit_root)
2249 free_extent_buffer(root->commit_root);
2250 kfree(root->name); 2323 kfree(root->name);
2251 kfree(root); 2324 kfree(root);
2252 return 0;
2253} 2325}
2254 2326
2255static int del_fs_roots(struct btrfs_fs_info *fs_info) 2327static int del_fs_roots(struct btrfs_fs_info *fs_info)
@@ -2258,6 +2330,20 @@ static int del_fs_roots(struct btrfs_fs_info *fs_info)
2258 struct btrfs_root *gang[8]; 2330 struct btrfs_root *gang[8];
2259 int i; 2331 int i;
2260 2332
2333 while (!list_empty(&fs_info->dead_roots)) {
2334 gang[0] = list_entry(fs_info->dead_roots.next,
2335 struct btrfs_root, root_list);
2336 list_del(&gang[0]->root_list);
2337
2338 if (gang[0]->in_radix) {
2339 btrfs_free_fs_root(fs_info, gang[0]);
2340 } else {
2341 free_extent_buffer(gang[0]->node);
2342 free_extent_buffer(gang[0]->commit_root);
2343 kfree(gang[0]);
2344 }
2345 }
2346
2261 while (1) { 2347 while (1) {
2262 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix, 2348 ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
2263 (void **)gang, 0, 2349 (void **)gang, 0,
@@ -2287,9 +2373,6 @@ int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
2287 root_objectid = gang[ret - 1]->root_key.objectid + 1; 2373 root_objectid = gang[ret - 1]->root_key.objectid + 1;
2288 for (i = 0; i < ret; i++) { 2374 for (i = 0; i < ret; i++) {
2289 root_objectid = gang[i]->root_key.objectid; 2375 root_objectid = gang[i]->root_key.objectid;
2290 ret = btrfs_find_dead_roots(fs_info->tree_root,
2291 root_objectid);
2292 BUG_ON(ret);
2293 btrfs_orphan_cleanup(gang[i]); 2376 btrfs_orphan_cleanup(gang[i]);
2294 } 2377 }
2295 root_objectid++; 2378 root_objectid++;
@@ -2359,12 +2442,12 @@ int close_ctree(struct btrfs_root *root)
2359 free_extent_buffer(root->fs_info->csum_root->commit_root); 2442 free_extent_buffer(root->fs_info->csum_root->commit_root);
2360 2443
2361 btrfs_free_block_groups(root->fs_info); 2444 btrfs_free_block_groups(root->fs_info);
2362 btrfs_free_pinned_extents(root->fs_info);
2363 2445
2364 del_fs_roots(fs_info); 2446 del_fs_roots(fs_info);
2365 2447
2366 iput(fs_info->btree_inode); 2448 iput(fs_info->btree_inode);
2367 2449
2450 btrfs_stop_workers(&fs_info->generic_worker);
2368 btrfs_stop_workers(&fs_info->fixup_workers); 2451 btrfs_stop_workers(&fs_info->fixup_workers);
2369 btrfs_stop_workers(&fs_info->delalloc_workers); 2452 btrfs_stop_workers(&fs_info->delalloc_workers);
2370 btrfs_stop_workers(&fs_info->workers); 2453 btrfs_stop_workers(&fs_info->workers);
@@ -2373,11 +2456,13 @@ int close_ctree(struct btrfs_root *root)
2373 btrfs_stop_workers(&fs_info->endio_meta_write_workers); 2456 btrfs_stop_workers(&fs_info->endio_meta_write_workers);
2374 btrfs_stop_workers(&fs_info->endio_write_workers); 2457 btrfs_stop_workers(&fs_info->endio_write_workers);
2375 btrfs_stop_workers(&fs_info->submit_workers); 2458 btrfs_stop_workers(&fs_info->submit_workers);
2459 btrfs_stop_workers(&fs_info->enospc_workers);
2376 2460
2377 btrfs_close_devices(fs_info->fs_devices); 2461 btrfs_close_devices(fs_info->fs_devices);
2378 btrfs_mapping_tree_free(&fs_info->mapping_tree); 2462 btrfs_mapping_tree_free(&fs_info->mapping_tree);
2379 2463
2380 bdi_destroy(&fs_info->bdi); 2464 bdi_destroy(&fs_info->bdi);
2465 cleanup_srcu_struct(&fs_info->subvol_srcu);
2381 2466
2382 kfree(fs_info->extent_root); 2467 kfree(fs_info->extent_root);
2383 kfree(fs_info->tree_root); 2468 kfree(fs_info->tree_root);