Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable: (25 commits)
btrfs: fix uninitialized variable warning
btrfs: add helper for fs_info->closing
Btrfs: add mount -o inode_cache
btrfs: scrub: add explicit plugging
btrfs: use btrfs_ino to access inode number
Btrfs: don't save the inode cache if we are deleting this root
btrfs: false BUG_ON when degraded
Btrfs: don't save the inode cache in non-FS roots
Btrfs: make sure we don't overflow the free space cache crc page
Btrfs: fix uninit variable in the delayed inode code
btrfs: scrub: don't reuse bios and pages
Btrfs: leave spinning on lookup and map the leaf
Btrfs: check for duplicate entries in the free space cache
Btrfs: don't try to allocate from a block group that doesn't have enough space
Btrfs: don't always do readahead
Btrfs: try not to sleep as much when doing slow caching
Btrfs: kill BTRFS_I(inode)->block_group
Btrfs: don't look at the extent buffer level 3 times in a row
Btrfs: map the node block when looking for readahead targets
Btrfs: set range_start to the right start in count_range_bits
...

+635 -468
-3
fs/btrfs/btrfs_inode.h
··· 121 121 */ 122 122 u64 index_cnt; 123 123 124 - /* the start of block group preferred for allocations. */ 125 - u64 block_group; 126 - 127 124 /* the fsync log has some corner cases that mean we have to check 128 125 * directories to see if any unlinks have been done before 129 126 * the directory was logged. See tree-log.c for all the
+21 -7
fs/btrfs/ctree.c
··· 43 43 { 44 44 struct btrfs_path *path; 45 45 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); 46 - if (path) 47 - path->reada = 1; 48 46 return path; 49 47 } 50 48 ··· 1222 1224 u64 search; 1223 1225 u64 target; 1224 1226 u64 nread = 0; 1227 + u64 gen; 1225 1228 int direction = path->reada; 1226 1229 struct extent_buffer *eb; 1227 1230 u32 nr; ··· 1250 1251 nritems = btrfs_header_nritems(node); 1251 1252 nr = slot; 1252 1253 while (1) { 1254 + if (!node->map_token) { 1255 + unsigned long offset = btrfs_node_key_ptr_offset(nr); 1256 + map_private_extent_buffer(node, offset, 1257 + sizeof(struct btrfs_key_ptr), 1258 + &node->map_token, 1259 + &node->kaddr, 1260 + &node->map_start, 1261 + &node->map_len, KM_USER1); 1262 + } 1253 1263 if (direction < 0) { 1254 1264 if (nr == 0) 1255 1265 break; ··· 1276 1268 search = btrfs_node_blockptr(node, nr); 1277 1269 if ((search <= target && target - search <= 65536) || 1278 1270 (search > target && search - target <= 65536)) { 1279 - readahead_tree_block(root, search, blocksize, 1280 - btrfs_node_ptr_generation(node, nr)); 1271 + gen = btrfs_node_ptr_generation(node, nr); 1272 + if (node->map_token) { 1273 + unmap_extent_buffer(node, node->map_token, 1274 + KM_USER1); 1275 + node->map_token = NULL; 1276 + } 1277 + readahead_tree_block(root, search, blocksize, gen); 1281 1278 nread += blocksize; 1282 1279 } 1283 1280 nscan++; 1284 1281 if ((nread > 65536 || nscan > 32)) 1285 1282 break; 1283 + } 1284 + if (node->map_token) { 1285 + unmap_extent_buffer(node, node->map_token, KM_USER1); 1286 + node->map_token = NULL; 1286 1287 } 1287 1288 } 1288 1289 ··· 1665 1648 } 1666 1649 cow_done: 1667 1650 BUG_ON(!cow && ins_len); 1668 - if (level != btrfs_header_level(b)) 1669 - WARN_ON(1); 1670 - level = btrfs_header_level(b); 1671 1651 1672 1652 p->nodes[level] = b; 1673 1653 if (!p->skip_locking)
+17 -5
fs/btrfs/ctree.h
··· 930 930 * is required instead of the faster short fsync log commits 931 931 */ 932 932 u64 last_trans_log_full_commit; 933 - u64 open_ioctl_trans; 934 933 unsigned long mount_opt:20; 935 934 unsigned long compress_type:4; 936 935 u64 max_inline; ··· 946 947 struct super_block *sb; 947 948 struct inode *btree_inode; 948 949 struct backing_dev_info bdi; 949 - struct mutex trans_mutex; 950 950 struct mutex tree_log_mutex; 951 951 struct mutex transaction_kthread_mutex; 952 952 struct mutex cleaner_mutex; ··· 966 968 struct rw_semaphore subvol_sem; 967 969 struct srcu_struct subvol_srcu; 968 970 971 + spinlock_t trans_lock; 969 972 struct list_head trans_list; 970 973 struct list_head hashers; 971 974 struct list_head dead_roots; ··· 979 980 atomic_t async_submit_draining; 980 981 atomic_t nr_async_bios; 981 982 atomic_t async_delalloc_pages; 983 + atomic_t open_ioctl_trans; 982 984 983 985 /* 984 986 * this is used by the balancing code to wait for all the pending ··· 1044 1044 int closing; 1045 1045 int log_root_recovering; 1046 1046 int enospc_unlink; 1047 + int trans_no_join; 1047 1048 1048 1049 u64 total_pinned; 1049 1050 ··· 1066 1065 struct reloc_control *reloc_ctl; 1067 1066 1068 1067 spinlock_t delalloc_lock; 1069 - spinlock_t new_trans_lock; 1070 1068 u64 delalloc_bytes; 1071 1069 1072 1070 /* data_alloc_cluster is only used in ssd mode */ ··· 1340 1340 #define BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED (1 << 14) 1341 1341 #define BTRFS_MOUNT_ENOSPC_DEBUG (1 << 15) 1342 1342 #define BTRFS_MOUNT_AUTO_DEFRAG (1 << 16) 1343 + #define BTRFS_MOUNT_INODE_MAP_CACHE (1 << 17) 1343 1344 1344 1345 #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt) 1345 1346 #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt) ··· 2239 2238 void btrfs_block_rsv_release(struct btrfs_root *root, 2240 2239 struct btrfs_block_rsv *block_rsv, 2241 2240 u64 num_bytes); 2241 + int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, 2242 + struct btrfs_root *root, 2243 + struct btrfs_block_rsv *rsv); 2242 2244 int btrfs_set_block_group_ro(struct btrfs_root *root, 2243 2245 struct btrfs_block_group_cache *cache); 2244 2246 int btrfs_set_block_group_rw(struct btrfs_root *root, ··· 2354 2350 struct btrfs_root *root, 2355 2351 struct extent_buffer *node, 2356 2352 struct extent_buffer *parent); 2353 + static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info) 2354 + { 2355 + /* 2356 + * Get synced with close_ctree() 2357 + */ 2358 + smp_mb(); 2359 + return fs_info->closing; 2360 + } 2361 + 2357 2362 /* root-item.c */ 2358 2363 int btrfs_find_root_ref(struct btrfs_root *tree_root, 2359 2364 struct btrfs_path *path, ··· 2525 2512 int btrfs_writepages(struct address_space *mapping, 2526 2513 struct writeback_control *wbc); 2527 2514 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 2528 - struct btrfs_root *new_root, 2529 - u64 new_dirid, u64 alloc_hint); 2515 + struct btrfs_root *new_root, u64 new_dirid); 2530 2516 int btrfs_merge_bio_hook(struct page *page, unsigned long offset, 2531 2517 size_t size, struct bio *bio, unsigned long bio_flags); 2532 2518
+4 -4
fs/btrfs/delayed-inode.c
··· 678 678 INIT_LIST_HEAD(&head); 679 679 680 680 next = item; 681 + nitems = 0; 681 682 682 683 /* 683 684 * count the number of the continuous items that we can insert in batch ··· 1130 1129 delayed_node = async_node->delayed_node; 1131 1130 root = delayed_node->root; 1132 1131 1133 - trans = btrfs_join_transaction(root, 0); 1132 + trans = btrfs_join_transaction(root); 1134 1133 if (IS_ERR(trans)) 1135 1134 goto free_path; 1136 1135 ··· 1573 1572 btrfs_set_stack_inode_transid(inode_item, trans->transid); 1574 1573 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev); 1575 1574 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags); 1576 - btrfs_set_stack_inode_block_group(inode_item, 1577 - BTRFS_I(inode)->block_group); 1575 + btrfs_set_stack_inode_block_group(inode_item, 0); 1578 1576 1579 1577 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item), 1580 1578 inode->i_atime.tv_sec); ··· 1595 1595 struct btrfs_root *root, struct inode *inode) 1596 1596 { 1597 1597 struct btrfs_delayed_node *delayed_node; 1598 - int ret; 1598 + int ret = 0; 1599 1599 1600 1600 delayed_node = btrfs_get_or_create_delayed_node(inode); 1601 1601 if (IS_ERR(delayed_node))
+18 -18
fs/btrfs/disk-io.c
··· 1505 1505 vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); 1506 1506 mutex_lock(&root->fs_info->transaction_kthread_mutex); 1507 1507 1508 - spin_lock(&root->fs_info->new_trans_lock); 1508 + spin_lock(&root->fs_info->trans_lock); 1509 1509 cur = root->fs_info->running_transaction; 1510 1510 if (!cur) { 1511 - spin_unlock(&root->fs_info->new_trans_lock); 1511 + spin_unlock(&root->fs_info->trans_lock); 1512 1512 goto sleep; 1513 1513 } 1514 1514 1515 1515 now = get_seconds(); 1516 1516 if (!cur->blocked && 1517 1517 (now < cur->start_time || now - cur->start_time < 30)) { 1518 - spin_unlock(&root->fs_info->new_trans_lock); 1518 + spin_unlock(&root->fs_info->trans_lock); 1519 1519 delay = HZ * 5; 1520 1520 goto sleep; 1521 1521 } 1522 1522 transid = cur->transid; 1523 - spin_unlock(&root->fs_info->new_trans_lock); 1523 + spin_unlock(&root->fs_info->trans_lock); 1524 1524 1525 - trans = btrfs_join_transaction(root, 1); 1525 + trans = btrfs_join_transaction(root); 1526 1526 BUG_ON(IS_ERR(trans)); 1527 1527 if (transid == trans->transid) { 1528 1528 ret = btrfs_commit_transaction(trans, root); ··· 1613 1613 INIT_LIST_HEAD(&fs_info->ordered_operations); 1614 1614 INIT_LIST_HEAD(&fs_info->caching_block_groups); 1615 1615 spin_lock_init(&fs_info->delalloc_lock); 1616 - spin_lock_init(&fs_info->new_trans_lock); 1616 + spin_lock_init(&fs_info->trans_lock); 1617 1617 spin_lock_init(&fs_info->ref_cache_lock); 1618 1618 spin_lock_init(&fs_info->fs_roots_radix_lock); 1619 1619 spin_lock_init(&fs_info->delayed_iput_lock); ··· 1645 1645 fs_info->max_inline = 8192 * 1024; 1646 1646 fs_info->metadata_ratio = 0; 1647 1647 fs_info->defrag_inodes = RB_ROOT; 1648 + fs_info->trans_no_join = 0; 1648 1649 1649 1650 fs_info->thread_pool_size = min_t(unsigned long, 1650 1651 num_online_cpus() + 2, 8); ··· 1710 1709 fs_info->do_barriers = 1; 1711 1710 1712 1711 1713 - mutex_init(&fs_info->trans_mutex); 1714 1712 mutex_init(&fs_info->ordered_operations_mutex); 1715 1713 mutex_init(&fs_info->tree_log_mutex); 1716 1714 mutex_init(&fs_info->chunk_mutex); ··· 2479 2479 down_write(&root->fs_info->cleanup_work_sem); 2480 2480 up_write(&root->fs_info->cleanup_work_sem); 2481 2481 2482 - trans = btrfs_join_transaction(root, 1); 2482 + trans = btrfs_join_transaction(root); 2483 2483 if (IS_ERR(trans)) 2484 2484 return PTR_ERR(trans); 2485 2485 ret = btrfs_commit_transaction(trans, root); 2486 2486 BUG_ON(ret); 2487 2487 /* run commit again to drop the original snapshot */ 2488 - trans = btrfs_join_transaction(root, 1); 2488 + trans = btrfs_join_transaction(root); 2489 2489 if (IS_ERR(trans)) 2490 2490 return PTR_ERR(trans); 2491 2491 btrfs_commit_transaction(trans, root); ··· 3024 3024 3025 3025 WARN_ON(1); 3026 3026 3027 - mutex_lock(&root->fs_info->trans_mutex); 3028 3027 mutex_lock(&root->fs_info->transaction_kthread_mutex); 3029 3028 3029 + spin_lock(&root->fs_info->trans_lock); 3030 3030 list_splice_init(&root->fs_info->trans_list, &list); 3031 + root->fs_info->trans_no_join = 1; 3032 + spin_unlock(&root->fs_info->trans_lock); 3033 + 3031 3034 while (!list_empty(&list)) { 3032 3035 t = list_entry(list.next, struct btrfs_transaction, list); 3033 3036 if (!t) ··· 3055 3052 t->blocked = 0; 3056 3053 if (waitqueue_active(&root->fs_info->transaction_wait)) 3057 3054 wake_up(&root->fs_info->transaction_wait); 3058 - mutex_unlock(&root->fs_info->trans_mutex); 3059 3055 3060 - mutex_lock(&root->fs_info->trans_mutex); 3061 3056 t->commit_done = 1; 3062 3057 if (waitqueue_active(&t->commit_wait)) 3063 3058 wake_up(&t->commit_wait); 3064 - mutex_unlock(&root->fs_info->trans_mutex); 3065 - 3066 - mutex_lock(&root->fs_info->trans_mutex); 3067 3059 3068 3060 btrfs_destroy_pending_snapshots(t); 3069 3061 3070 3062 btrfs_destroy_delalloc_inodes(root); 3071 3063 3072 - spin_lock(&root->fs_info->new_trans_lock); 3064 + spin_lock(&root->fs_info->trans_lock); 3073 3065 root->fs_info->running_transaction = NULL; 3074 - spin_unlock(&root->fs_info->new_trans_lock); 3066 + spin_unlock(&root->fs_info->trans_lock); 3075 3067 3076 3068 btrfs_destroy_marked_extents(root, &t->dirty_pages, 3077 3069 EXTENT_DIRTY); ··· 3080 3082 kmem_cache_free(btrfs_transaction_cachep, t); 3081 3083 } 3082 3084 3085 + spin_lock(&root->fs_info->trans_lock); 3086 + root->fs_info->trans_no_join = 0; 3087 + spin_unlock(&root->fs_info->trans_lock); 3083 3088 mutex_unlock(&root->fs_info->transaction_kthread_mutex); 3084 - mutex_unlock(&root->fs_info->trans_mutex); 3085 3089 3086 3090 return 0; 3087 3091 }
+68 -35
fs/btrfs/extent-tree.c
··· 348 348 */ 349 349 path->skip_locking = 1; 350 350 path->search_commit_root = 1; 351 - path->reada = 2; 351 + path->reada = 1; 352 352 353 353 key.objectid = last; 354 354 key.offset = 0; ··· 366 366 nritems = btrfs_header_nritems(leaf); 367 367 368 368 while (1) { 369 - smp_mb(); 370 - if (fs_info->closing > 1) { 369 + if (btrfs_fs_closing(fs_info) > 1) { 371 370 last = (u64)-1; 372 371 break; 373 372 } ··· 378 379 if (ret) 379 380 break; 380 381 381 - caching_ctl->progress = last; 382 - btrfs_release_path(path); 383 - up_read(&fs_info->extent_commit_sem); 384 - mutex_unlock(&caching_ctl->mutex); 385 - if (btrfs_transaction_in_commit(fs_info)) 386 - schedule_timeout(1); 387 - else 382 + if (need_resched() || 383 + btrfs_next_leaf(extent_root, path)) { 384 + caching_ctl->progress = last; 385 + btrfs_release_path(path); 386 + up_read(&fs_info->extent_commit_sem); 387 + mutex_unlock(&caching_ctl->mutex); 388 388 cond_resched(); 389 - goto again; 389 + goto again; 390 + } 391 + leaf = path->nodes[0]; 392 + nritems = btrfs_header_nritems(leaf); 393 + continue; 390 394 } 391 395 392 396 if (key.objectid < block_group->key.objectid) { ··· 3067 3065 spin_unlock(&data_sinfo->lock); 3068 3066 alloc: 3069 3067 alloc_target = btrfs_get_alloc_profile(root, 1); 3070 - trans = btrfs_join_transaction(root, 1); 3068 + trans = btrfs_join_transaction(root); 3071 3069 if (IS_ERR(trans)) 3072 3070 return PTR_ERR(trans); 3073 3071 ··· 3093 3091 3094 3092 /* commit the current transaction and try again */ 3095 3093 commit_trans: 3096 - if (!committed && !root->fs_info->open_ioctl_trans) { 3094 + if (!committed && 3095 + !atomic_read(&root->fs_info->open_ioctl_trans)) { 3097 3096 committed = 1; 3098 - trans = btrfs_join_transaction(root, 1); 3097 + trans = btrfs_join_transaction(root); 3099 3098 if (IS_ERR(trans)) 3100 3099 return PTR_ERR(trans); 3101 3100 ret = btrfs_commit_transaction(trans, root); ··· 3475 3472 goto out; 3476 3473 3477 3474 ret = -ENOSPC; 3478 - trans = btrfs_join_transaction(root, 1); 3475 + trans = btrfs_join_transaction(root); 3479 3476 if (IS_ERR(trans)) 3480 3477 goto out; 3481 3478 ret = btrfs_commit_transaction(trans, root); ··· 3702 3699 if (trans) 3703 3700 return -EAGAIN; 3704 3701 3705 - trans = btrfs_join_transaction(root, 1); 3702 + trans = btrfs_join_transaction(root); 3706 3703 BUG_ON(IS_ERR(trans)); 3707 3704 ret = btrfs_commit_transaction(trans, root); 3708 3705 return 0; ··· 3840 3837 WARN_ON(fs_info->chunk_block_rsv.reserved > 0); 3841 3838 } 3842 3839 3840 + int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, 3841 + struct btrfs_root *root, 3842 + struct btrfs_block_rsv *rsv) 3843 + { 3844 + struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv; 3845 + u64 num_bytes; 3846 + int ret; 3847 + 3848 + /* 3849 + * Truncate should be freeing data, but give us 2 items just in case it 3850 + * needs to use some space. We may want to be smarter about this in the 3851 + * future. 3852 + */ 3853 + num_bytes = btrfs_calc_trans_metadata_size(root, 2); 3854 + 3855 + /* We already have enough bytes, just return */ 3856 + if (rsv->reserved >= num_bytes) 3857 + return 0; 3858 + 3859 + num_bytes -= rsv->reserved; 3860 + 3861 + /* 3862 + * You should have reserved enough space before hand to do this, so this 3863 + * should not fail. 3864 + */ 3865 + ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes); 3866 + BUG_ON(ret); 3867 + 3868 + return 0; 3869 + } 3870 + 3843 3871 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, 3844 3872 struct btrfs_root *root, 3845 3873 int num_items) ··· 3911 3877 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; 3912 3878 3913 3879 /* 3914 - * one for deleting orphan item, one for updating inode and 3915 - * two for calling btrfs_truncate_inode_items. 3916 - * 3917 - * btrfs_truncate_inode_items is a delete operation, it frees 3918 - * more space than it uses in most cases. So two units of 3919 - * metadata space should be enough for calling it many times. 3920 - * If all of the metadata space is used, we can commit 3921 - * transaction and use space it freed. 3880 + * We need to hold space in order to delete our orphan item once we've 3881 + * added it, so this takes the reservation so we can release it later 3882 + * when we are truly done with the orphan item. 3922 3883 */ 3923 - u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); 3884 + u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); 3924 3885 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); 3925 3886 } 3926 3887 3927 3888 void btrfs_orphan_release_metadata(struct inode *inode) 3928 3889 { 3929 3890 struct btrfs_root *root = BTRFS_I(inode)->root; 3930 - u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4); 3891 + u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); 3931 3892 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); 3932 3893 } 3933 3894 ··· 5016 4987 if (unlikely(block_group->ro)) 5017 4988 goto loop; 5018 4989 4990 + spin_lock(&block_group->free_space_ctl->tree_lock); 4991 + if (cached && 4992 + block_group->free_space_ctl->free_space < 4993 + num_bytes + empty_size) { 4994 + spin_unlock(&block_group->free_space_ctl->tree_lock); 4995 + goto loop; 4996 + } 4997 + spin_unlock(&block_group->free_space_ctl->tree_lock); 4998 + 5019 4999 /* 5020 5000 * Ok we want to try and use the cluster allocator, so lets look 5021 5001 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will ··· 5188 5150 btrfs_add_free_space(block_group, offset, 5189 5151 search_start - offset); 5190 5152 BUG_ON(offset > search_start); 5153 + btrfs_put_block_group(block_group); 5191 5154 break; 5192 5155 loop: 5193 5156 failed_cluster_refill = false; ··· 5281 5242 ret = -ENOSPC; 5282 5243 } else if (!ins->objectid) { 5283 5244 ret = -ENOSPC; 5284 - } 5285 - 5286 - /* we found what we needed */ 5287 - if (ins->objectid) { 5288 - if (!(data & BTRFS_BLOCK_GROUP_DATA)) 5289 - trans->block_group = block_group->key.objectid; 5290 - 5291 - btrfs_put_block_group(block_group); 5245 + } else if (ins->objectid) { 5292 5246 ret = 0; 5293 5247 } 5294 5248 ··· 6558 6526 6559 6527 BUG_ON(cache->ro); 6560 6528 6561 - trans = btrfs_join_transaction(root, 1); 6529 + trans = btrfs_join_transaction(root); 6562 6530 BUG_ON(IS_ERR(trans)); 6563 6531 6564 6532 alloc_flags = update_block_group_flags(root, cache->flags); ··· 6914 6882 path = btrfs_alloc_path(); 6915 6883 if (!path) 6916 6884 return -ENOMEM; 6885 + path->reada = 1; 6917 6886 6918 6887 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); 6919 6888 if (cache_gen != 0 &&
+1 -1
fs/btrfs/extent_io.c
··· 1476 1476 if (total_bytes >= max_bytes) 1477 1477 break; 1478 1478 if (!found) { 1479 - *start = state->start; 1479 + *start = max(cur_start, state->start); 1480 1480 found = 1; 1481 1481 } 1482 1482 last = state->end;
+4 -6
fs/btrfs/file.c
··· 129 129 if (!btrfs_test_opt(root, AUTO_DEFRAG)) 130 130 return 0; 131 131 132 - if (root->fs_info->closing) 132 + if (btrfs_fs_closing(root->fs_info)) 133 133 return 0; 134 134 135 135 if (BTRFS_I(inode)->in_defrag) ··· 144 144 if (!defrag) 145 145 return -ENOMEM; 146 146 147 - defrag->ino = inode->i_ino; 147 + defrag->ino = btrfs_ino(inode); 148 148 defrag->transid = transid; 149 149 defrag->root = root->root_key.objectid; 150 150 ··· 229 229 first_ino = defrag->ino + 1; 230 230 rb_erase(&defrag->rb_node, &fs_info->defrag_inodes); 231 231 232 - if (fs_info->closing) 232 + if (btrfs_fs_closing(fs_info)) 233 233 goto next_free; 234 234 235 235 spin_unlock(&fs_info->defrag_inodes_lock); ··· 1480 1480 * the current transaction, we can bail out now without any 1481 1481 * syncing 1482 1482 */ 1483 - mutex_lock(&root->fs_info->trans_mutex); 1483 + smp_mb(); 1484 1484 if (BTRFS_I(inode)->last_trans <= 1485 1485 root->fs_info->last_trans_committed) { 1486 1486 BTRFS_I(inode)->last_trans = 0; 1487 - mutex_unlock(&root->fs_info->trans_mutex); 1488 1487 goto out; 1489 1488 } 1490 - mutex_unlock(&root->fs_info->trans_mutex); 1491 1489 1492 1490 /* 1493 1491 * ok we haven't committed the transaction yet, lets do a commit
+53 -17
fs/btrfs/free-space-cache.c
··· 98 98 return inode; 99 99 100 100 spin_lock(&block_group->lock); 101 - if (!root->fs_info->closing) { 101 + if (!btrfs_fs_closing(root->fs_info)) { 102 102 block_group->inode = igrab(inode); 103 103 block_group->iref = 1; 104 104 } ··· 402 402 spin_lock(&ctl->tree_lock); 403 403 ret = link_free_space(ctl, e); 404 404 spin_unlock(&ctl->tree_lock); 405 - BUG_ON(ret); 405 + if (ret) { 406 + printk(KERN_ERR "Duplicate entries in " 407 + "free space cache, dumping\n"); 408 + kunmap(page); 409 + unlock_page(page); 410 + page_cache_release(page); 411 + goto free_cache; 412 + } 406 413 } else { 407 414 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); 408 415 if (!e->bitmap) { ··· 426 419 ctl->op->recalc_thresholds(ctl); 427 420 spin_unlock(&ctl->tree_lock); 428 421 list_add_tail(&e->list, &bitmaps); 422 + if (ret) { 423 + printk(KERN_ERR "Duplicate entries in " 424 + "free space cache, dumping\n"); 425 + kunmap(page); 426 + unlock_page(page); 427 + page_cache_release(page); 428 + goto free_cache; 429 + } 429 430 } 430 431 431 432 num_entries--; ··· 493 478 * If we're unmounting then just return, since this does a search on the 494 479 * normal root and not the commit root and we could deadlock. 495 480 */ 496 - smp_mb(); 497 - if (fs_info->closing) 481 + if (btrfs_fs_closing(fs_info)) 498 482 return 0; 499 483 500 484 /* ··· 589 575 590 576 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 591 577 PAGE_CACHE_SHIFT; 578 + 579 + /* Since the first page has all of our checksums and our generation we 580 + * need to calculate the offset into the page that we can start writing 581 + * our entries. 582 + */ 583 + first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64); 584 + 592 585 filemap_write_and_wait(inode->i_mapping); 593 586 btrfs_wait_ordered_range(inode, inode->i_size & 594 587 ~(root->sectorsize - 1), (u64)-1); 588 + 589 + /* make sure we don't overflow that first page */ 590 + if (first_page_offset + sizeof(struct btrfs_free_space_entry) >= PAGE_CACHE_SIZE) { 591 + /* this is really the same as running out of space, where we also return 0 */ 592 + printk(KERN_CRIT "Btrfs: free space cache was too big for the crc page\n"); 593 + ret = 0; 594 + goto out_update; 595 + } 595 596 596 597 /* We need a checksum per page. */ 597 598 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS); ··· 618 589 kfree(crc); 619 590 return -1; 620 591 } 621 - 622 - /* Since the first page has all of our checksums and our generation we 623 - * need to calculate the offset into the page that we can start writing 624 - * our entries. 625 - */ 626 - first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64); 627 592 628 593 /* Get the cluster for this block_group if it exists */ 629 594 if (block_group && !list_empty(&block_group->cluster_list)) ··· 880 857 ret = 1; 881 858 882 859 out_free: 860 + kfree(checksums); 861 + kfree(pages); 862 + 863 + out_update: 883 864 if (ret != 1) { 884 865 invalidate_inode_pages2_range(inode->i_mapping, 0, index); 885 866 BTRFS_I(inode)->generation = 0; 886 867 } 887 - kfree(checksums); 888 - kfree(pages); 889 868 btrfs_update_inode(trans, root, inode); 890 869 return ret; 891 870 } ··· 988 963 * logically. 989 964 */ 990 965 if (bitmap) { 991 - WARN_ON(info->bitmap); 966 + if (info->bitmap) { 967 + WARN_ON_ONCE(1); 968 + return -EEXIST; 969 + } 992 970 p = &(*p)->rb_right; 993 971 } else { 994 - WARN_ON(!info->bitmap); 972 + if (!info->bitmap) { 973 + WARN_ON_ONCE(1); 974 + return -EEXIST; 975 + } 995 976 p = &(*p)->rb_left; 996 977 } 997 978 } ··· 2512 2481 return inode; 2513 2482 2514 2483 spin_lock(&root->cache_lock); 2515 - if (!root->fs_info->closing) 2484 + if (!btrfs_fs_closing(root->fs_info)) 2516 2485 root->cache_inode = igrab(inode); 2517 2486 spin_unlock(&root->cache_lock); 2518 2487 ··· 2535 2504 int ret = 0; 2536 2505 u64 root_gen = btrfs_root_generation(&root->root_item); 2537 2506 2507 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 2508 + return 0; 2509 + 2538 2510 /* 2539 2511 * If we're unmounting then just return, since this does a search on the 2540 2512 * normal root and not the commit root and we could deadlock. 2541 2513 */ 2542 - smp_mb(); 2543 - if (fs_info->closing) 2514 + if (btrfs_fs_closing(fs_info)) 2544 2515 return 0; 2545 2516 2546 2517 path = btrfs_alloc_path(); ··· 2575 2542 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; 2576 2543 struct inode *inode; 2577 2544 int ret; 2545 + 2546 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 2547 + return 0; 2578 2548 2579 2549 inode = lookup_free_ino_inode(root, path); 2580 2550 if (IS_ERR(inode))
+32 -2
fs/btrfs/inode-map.c
··· 38 38 int slot; 39 39 int ret; 40 40 41 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 42 + return 0; 43 + 41 44 path = btrfs_alloc_path(); 42 45 if (!path) 43 46 return -ENOMEM; ··· 62 59 goto out; 63 60 64 61 while (1) { 65 - smp_mb(); 66 - if (fs_info->closing) 62 + if (btrfs_fs_closing(fs_info)) 67 63 goto out; 68 64 69 65 leaf = path->nodes[0]; ··· 143 141 int ret; 144 142 u64 objectid; 145 143 144 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 145 + return; 146 + 146 147 spin_lock(&root->cache_lock); 147 148 if (root->cached != BTRFS_CACHE_NO) { 148 149 spin_unlock(&root->cache_lock); ··· 183 178 184 179 int btrfs_find_free_ino(struct btrfs_root *root, u64 *objectid) 185 180 { 181 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 182 + return btrfs_find_free_objectid(root, objectid); 183 + 186 184 again: 187 185 *objectid = btrfs_find_ino_for_alloc(root); 188 186 ··· 209 201 { 210 202 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; 211 203 struct btrfs_free_space_ctl *pinned = root->free_ino_pinned; 204 + 205 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 206 + return; 207 + 212 208 again: 213 209 if (root->cached == BTRFS_CACHE_FINISHED) { 214 210 __btrfs_add_free_space(ctl, objectid, 1); ··· 261 249 struct btrfs_free_space *info; 262 250 struct rb_node *n; 263 251 u64 count; 252 + 253 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 254 + return; 264 255 265 256 while (1) { 266 257 n = rb_first(rbroot); ··· 403 388 int prealloc; 404 389 bool retry = false; 405 390 391 + /* only fs tree and subvol/snap needs ino cache */ 392 + if (root->root_key.objectid != BTRFS_FS_TREE_OBJECTID && 393 + (root->root_key.objectid < BTRFS_FIRST_FREE_OBJECTID || 394 + root->root_key.objectid > BTRFS_LAST_FREE_OBJECTID)) 395 + return 0; 396 + 397 + /* Don't save inode cache if we are deleting this root */ 398 + if (btrfs_root_refs(&root->root_item) == 0 && 399 + root != root->fs_info->tree_root) 400 + return 0; 401 + 402 + if (!btrfs_test_opt(root, INODE_MAP_CACHE)) 403 + return 0; 404 + 406 405 path = btrfs_alloc_path(); 407 406 if (!path) 408 407 return -ENOMEM; 408 + 409 409 again: 410 410 inode = lookup_free_ino_inode(root, path); 411 411 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
+137 -124
fs/btrfs/inode.c
··· 138 138 return -ENOMEM; 139 139 140 140 path->leave_spinning = 1; 141 - btrfs_set_trans_block_group(trans, inode); 142 141 143 142 key.objectid = btrfs_ino(inode); 144 143 key.offset = start; ··· 425 426 } 426 427 } 427 428 if (start == 0) { 428 - trans = btrfs_join_transaction(root, 1); 429 + trans = btrfs_join_transaction(root); 429 430 BUG_ON(IS_ERR(trans)); 430 - btrfs_set_trans_block_group(trans, inode); 431 431 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 432 432 433 433 /* lets try to make an inline extent */ ··· 621 623 async_extent->start + async_extent->ram_size - 1, 622 624 GFP_NOFS); 623 625 624 - trans = btrfs_join_transaction(root, 1); 626 + trans = btrfs_join_transaction(root); 625 627 BUG_ON(IS_ERR(trans)); 628 + trans->block_rsv = &root->fs_info->delalloc_block_rsv; 626 629 ret = btrfs_reserve_extent(trans, root, 627 630 async_extent->compressed_size, 628 631 async_extent->compressed_size, ··· 792 793 int ret = 0; 793 794 794 795 BUG_ON(is_free_space_inode(root, inode)); 795 - trans = btrfs_join_transaction(root, 1); 796 + trans = btrfs_join_transaction(root); 796 797 BUG_ON(IS_ERR(trans)); 797 - btrfs_set_trans_block_group(trans, inode); 798 798 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 799 799 800 800 num_bytes = (end - start + blocksize) & ~(blocksize - 1); ··· 1075 1077 nolock = is_free_space_inode(root, inode); 1076 1078 1077 1079 if (nolock) 1078 - trans = btrfs_join_transaction_nolock(root, 1); 1080 + trans = btrfs_join_transaction_nolock(root); 1079 1081 else 1080 - trans = btrfs_join_transaction(root, 1); 1082 + trans = btrfs_join_transaction(root); 1083 + 1081 1084 BUG_ON(IS_ERR(trans)); 1085 + trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1082 1086 1083 1087 cow_start = (u64)-1; 1084 1088 cur_offset = start; ··· 1519 1519 { 1520 1520 struct btrfs_ordered_sum *sum; 1521 1521 1522 - btrfs_set_trans_block_group(trans, inode); 1523 - 1524 1522 list_for_each_entry(sum, list, list) { 1525 1523 btrfs_csum_file_blocks(trans, 1526 1524 BTRFS_I(inode)->root->fs_info->csum_root, sum); ··· 1733 1735 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1734 1736 if (!ret) { 1735 1737 if (nolock) 1736 - trans = btrfs_join_transaction_nolock(root, 1); 1738 + trans = btrfs_join_transaction_nolock(root); 1737 1739 else 1738 - trans = btrfs_join_transaction(root, 1); 1740 + trans = btrfs_join_transaction(root); 1739 1741 BUG_ON(IS_ERR(trans)); 1740 - btrfs_set_trans_block_group(trans, inode); 1741 1742 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1742 1743 ret = btrfs_update_inode(trans, root, inode); 1743 1744 BUG_ON(ret); ··· 1749 1752 0, &cached_state, GFP_NOFS); 1750 1753 1751 1754 if (nolock) 1752 - trans = btrfs_join_transaction_nolock(root, 1); 1755 + trans = btrfs_join_transaction_nolock(root); 1753 1756 else 1754 - trans = btrfs_join_transaction(root, 1); 1757 + trans = btrfs_join_transaction(root); 1755 1758 BUG_ON(IS_ERR(trans)); 1756 - btrfs_set_trans_block_group(trans, inode); 1757 1759 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1758 1760 1759 1761 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) ··· 2427 2431 (u64)-1); 2428 2432 2429 2433 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2430 - trans = btrfs_join_transaction(root, 1); 2434 + trans = btrfs_join_transaction(root); 2431 2435 if (!IS_ERR(trans)) 2432 2436 btrfs_end_transaction(trans, root); 2433 2437 } ··· 2507 2511 struct btrfs_root *root = BTRFS_I(inode)->root; 2508 2512 struct btrfs_key location; 2509 2513 int maybe_acls; 2510 - u64 alloc_group_block; 2511 2514 u32 rdev; 2512 2515 int ret; 2513 2516 2514 2517 path = btrfs_alloc_path(); 2515 2518 BUG_ON(!path); 2519 + path->leave_spinning = 1; 2516 2520 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); 2517 2521 2518 2522 ret = btrfs_lookup_inode(NULL, root, path, &location, 0); ··· 2522 2526 leaf = path->nodes[0]; 2523 2527 inode_item = btrfs_item_ptr(leaf, path->slots[0], 2524 2528 struct btrfs_inode_item); 2529 + if (!leaf->map_token) 2530 + map_private_extent_buffer(leaf, (unsigned long)inode_item, 2531 + sizeof(struct btrfs_inode_item), 2532 + &leaf->map_token, &leaf->kaddr, 2533 + &leaf->map_start, &leaf->map_len, 2534 + KM_USER1); 2525 2535 2526 2536 inode->i_mode = btrfs_inode_mode(leaf, inode_item); 2527 2537 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); ··· 2557 2555 BTRFS_I(inode)->index_cnt = (u64)-1; 2558 2556 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 2559 2557 2560 - alloc_group_block = btrfs_inode_block_group(leaf, inode_item); 2561 - 2562 2558 /* 2563 2559 * try to precache a NULL acl entry for files that don't have 2564 2560 * any xattrs or acls ··· 2566 2566 if (!maybe_acls) 2567 2567 cache_no_acl(inode); 2568 2568 2569 - BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, 2570 - alloc_group_block, 0); 2569 + if (leaf->map_token) { 2570 + unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2571 + leaf->map_token = NULL; 2572 + } 2573 + 2571 2574 btrfs_free_path(path); 2572 2575 inode_item = NULL; 2573 2576 ··· 2650 2647 btrfs_set_inode_transid(leaf, item, trans->transid); 2651 2648 btrfs_set_inode_rdev(leaf, item, inode->i_rdev); 2652 2649 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); 2653 - btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); 2650 + btrfs_set_inode_block_group(leaf, item, 0); 2654 2651 2655 2652 if (leaf->map_token) { 2656 2653 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); ··· 3007 3004 if (IS_ERR(trans)) 3008 3005 return PTR_ERR(trans); 3009 3006 3010 - btrfs_set_trans_block_group(trans, dir); 3011 - 3012 3007 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); 3013 3008 3014 3009 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, ··· 3094 3093 trans = __unlink_start_trans(dir, dentry); 3095 3094 if (IS_ERR(trans)) 3096 3095 return PTR_ERR(trans); 3097 - 3098 - btrfs_set_trans_block_group(trans, dir); 3099 3096 3100 3097 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3101 3098 err = btrfs_unlink_subvol(trans, root, dir, ··· 3513 3514 err = PTR_ERR(trans); 3514 3515 break; 3515 3516 } 3516 - btrfs_set_trans_block_group(trans, inode); 3517 3517 3518 3518 err = btrfs_drop_extents(trans, inode, cur_offset, 3519 3519 cur_offset + hole_size, ··· 3648 3650 while (1) { 3649 3651 trans = btrfs_start_transaction(root, 0); 3650 3652 BUG_ON(IS_ERR(trans)); 3651 - btrfs_set_trans_block_group(trans, inode); 3652 3653 trans->block_rsv = root->orphan_block_rsv; 3653 3654 3654 3655 ret = btrfs_block_rsv_check(trans, root, ··· 4130 4133 path = btrfs_alloc_path(); 4131 4134 if (!path) 4132 4135 return -ENOMEM; 4133 - path->reada = 2; 4136 + 4137 + path->reada = 1; 4134 4138 4135 4139 if (key_type == BTRFS_DIR_INDEX_KEY) { 4136 4140 INIT_LIST_HEAD(&ins_list); ··· 4266 4268 if (BTRFS_I(inode)->dummy_inode) 4267 4269 return 0; 4268 4270 4269 - smp_mb(); 4270 - if (root->fs_info->closing && is_free_space_inode(root, inode)) 4271 + if (btrfs_fs_closing(root->fs_info) && is_free_space_inode(root, inode)) 4271 4272 nolock = true; 4272 4273 4273 4274 if (wbc->sync_mode == WB_SYNC_ALL) { 4274 4275 if (nolock) 4275 - trans = btrfs_join_transaction_nolock(root, 1); 4276 + trans = btrfs_join_transaction_nolock(root); 4276 4277 else 4277 - trans = btrfs_join_transaction(root, 1); 4278 + trans = btrfs_join_transaction(root); 4278 4279 if (IS_ERR(trans)) 4279 4280 return PTR_ERR(trans); 4280 - btrfs_set_trans_block_group(trans, inode); 4281 4281 if (nolock) 4282 4282 ret = btrfs_end_transaction_nolock(trans, root); 4283 4283 else ··· 4299 4303 if (BTRFS_I(inode)->dummy_inode) 4300 4304 return; 4301 4305 4302 - trans = btrfs_join_transaction(root, 1); 4306 + trans = btrfs_join_transaction(root); 4303 4307 BUG_ON(IS_ERR(trans)); 4304 - btrfs_set_trans_block_group(trans, inode); 4305 4308 4306 4309 ret = btrfs_update_inode(trans, root, inode); 4307 4310 if (ret && ret == -ENOSPC) { ··· 4314 4319 PTR_ERR(trans)); 4315 4320 return; 4316 4321 } 4317 - btrfs_set_trans_block_group(trans, inode); 4318 4322 4319 4323 ret = btrfs_update_inode(trans, root, inode); 4320 4324 if (ret) { ··· 4412 4418 struct btrfs_root *root, 4413 4419 struct inode *dir, 4414 4420 const char *name, int name_len, 4415 - u64 ref_objectid, u64 objectid, 4416 - u64 alloc_hint, int mode, u64 *index) 4421 + u64 ref_objectid, u64 objectid, int mode, 4422 + u64 *index) 4417 4423 { 4418 4424 struct inode *inode; 4419 4425 struct btrfs_inode_item *inode_item; ··· 4466 4472 owner = 0; 4467 4473 else 4468 4474 owner = 1; 4469 - BTRFS_I(inode)->block_group = 4470 - btrfs_find_block_group(root, 0, alloc_hint, owner); 4471 4475 4472 4476 key[0].objectid = objectid; 4473 4477 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); ··· 4621 4629 if (IS_ERR(trans)) 4622 4630 return PTR_ERR(trans); 4623 4631 4624 - btrfs_set_trans_block_group(trans, dir); 4625 - 4626 4632 err = btrfs_find_free_ino(root, &objectid); 4627 4633 if (err) 4628 4634 goto out_unlock; 4629 4635 4630 4636 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4631 4637 dentry->d_name.len, btrfs_ino(dir), objectid, 4632 - BTRFS_I(dir)->block_group, mode, &index); 4638 + mode, &index); 4633 4639 if (IS_ERR(inode)) { 4634 4640 err = PTR_ERR(inode); 4635 4641 goto out_unlock; ··· 4639 4649 goto out_unlock; 4640 4650 } 4641 4651 4642 - btrfs_set_trans_block_group(trans, inode); 4643 4652 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 4644 4653 if (err) 4645 4654 drop_inode = 1; ··· 4647 4658 init_special_inode(inode, inode->i_mode, rdev); 4648 4659 btrfs_update_inode(trans, root, inode); 4649 4660 } 4650 - btrfs_update_inode_block_group(trans, inode); 4651 - btrfs_update_inode_block_group(trans, dir); 4652 4661 out_unlock: 4653 4662 nr = trans->blocks_used; 4654 4663 btrfs_end_transaction_throttle(trans, root); ··· 4679 4692 if (IS_ERR(trans)) 4680 4693 return PTR_ERR(trans); 4681 4694 4682 - btrfs_set_trans_block_group(trans, dir); 4683 - 4684 4695 err = btrfs_find_free_ino(root, &objectid); 4685 4696 if (err) 4686 4697 goto out_unlock; 4687 4698 4688 4699 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4689 4700 dentry->d_name.len, btrfs_ino(dir), objectid, 4690 - BTRFS_I(dir)->block_group, mode, &index); 4701 + mode, &index); 4691 4702 if (IS_ERR(inode)) { 4692 4703 err = PTR_ERR(inode); 4693 4704 goto out_unlock; ··· 4697 4712 goto out_unlock; 4698 4713 } 4699 4714 4700 - btrfs_set_trans_block_group(trans, inode); 4701 4715 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 4702 4716 if (err) 4703 4717 drop_inode = 1; ··· 4707 4723 inode->i_op = &btrfs_file_inode_operations; 4708 4724 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 4709 4725 } 4710 - btrfs_update_inode_block_group(trans, inode); 4711 - btrfs_update_inode_block_group(trans, dir); 4712 4726 out_unlock: 4713 4727 nr = trans->blocks_used; 4714 4728 btrfs_end_transaction_throttle(trans, root); ··· 4753 4771 4754 4772 btrfs_inc_nlink(inode); 4755 4773 inode->i_ctime = CURRENT_TIME; 4756 - 4757 - btrfs_set_trans_block_group(trans, dir); 4758 4774 ihold(inode); 4759 4775 4760 4776 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); ··· 4761 4781 drop_inode = 1; 4762 4782 } else { 4763 4783 struct dentry *parent = dget_parent(dentry); 4764 - btrfs_update_inode_block_group(trans, dir); 4765 4784 err = btrfs_update_inode(trans, root, inode); 4766 4785 BUG_ON(err); 4767 4786 btrfs_log_new_name(trans, inode, NULL, parent); ··· 4797 4818 trans = btrfs_start_transaction(root, 5); 4798 4819 if (IS_ERR(trans)) 4799 4820 return PTR_ERR(trans); 4800 - btrfs_set_trans_block_group(trans, dir); 4801 4821 4802 4822 err = btrfs_find_free_ino(root, &objectid); 4803 4823 if (err) ··· 4804 4826 4805 4827 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4806 4828 dentry->d_name.len, btrfs_ino(dir), objectid, 4807 - BTRFS_I(dir)->block_group, S_IFDIR | mode, 4808 - &index); 4829 + S_IFDIR | mode, &index); 4809 4830 if (IS_ERR(inode)) { 4810 4831 err = PTR_ERR(inode); 4811 4832 goto out_fail; ··· 4818 4841 4819 4842 inode->i_op = &btrfs_dir_inode_operations; 4820 4843 inode->i_fop = &btrfs_dir_file_operations; 4821 - btrfs_set_trans_block_group(trans, inode); 4822 4844 4823 4845 btrfs_i_size_write(inode, 0); 4824 4846 err = btrfs_update_inode(trans, root, inode); ··· 4831 4855 4832 4856 d_instantiate(dentry, inode); 4833 4857 drop_on_err = 0; 4834 - btrfs_update_inode_block_group(trans, inode); 4835 - btrfs_update_inode_block_group(trans, dir); 4836 4858 4837 4859 out_fail: 4838 4860 nr = trans->blocks_used; ··· 4963 4989 4964 4990 if (!path) { 4965 4991 path = btrfs_alloc_path(); 4966 - BUG_ON(!path); 4992 + if (!path) { 4993 + err = -ENOMEM; 4994 + goto out; 4995 + } 4996 + /* 4997 + * Chances are we'll be called again, so go ahead and do 4998 + * readahead 4999 + */ 5000 + path->reada = 1; 4967 5001 } 4968 5002 4969 5003 ret = btrfs_lookup_file_extent(trans, root, path, ··· 5112 5130 kunmap(page); 5113 5131 free_extent_map(em); 5114 5132 em = NULL; 5133 + 5115 5134 btrfs_release_path(path); 5116 - trans = btrfs_join_transaction(root, 1); 5135 + trans = btrfs_join_transaction(root); 5136 + 5117 5137 if (IS_ERR(trans)) 5118 5138 return ERR_CAST(trans); 5119 5139 goto again; ··· 5359 5375 btrfs_drop_extent_cache(inode, start, start + len - 1, 0); 5360 5376 } 5361 5377 5362 - trans = btrfs_join_transaction(root, 0); 5378 + trans = btrfs_join_transaction(root); 5363 5379 if (IS_ERR(trans)) 5364 5380 return ERR_CAST(trans); 5365 5381 ··· 5595 5611 * to make sure the current transaction stays open 5596 5612 * while we look for nocow cross refs 5597 5613 */ 5598 - trans = btrfs_join_transaction(root, 0); 5614 + trans = btrfs_join_transaction(root); 5599 5615 if (IS_ERR(trans)) 5600 5616 goto must_cow; 5601 5617 ··· 5734 5750 5735 5751 BUG_ON(!ordered); 5736 5752 5737 - trans = btrfs_join_transaction(root, 1); 5753 + trans = btrfs_join_transaction(root); 5738 5754 if (IS_ERR(trans)) { 5739 5755 err = -ENOMEM; 5740 5756 goto out; ··· 6484 6500 static int btrfs_truncate(struct inode *inode) 6485 6501 { 6486 6502 struct btrfs_root *root = BTRFS_I(inode)->root; 6503 + struct btrfs_block_rsv *rsv; 6487 6504 int ret; 6488 6505 int err = 0; 6489 6506 struct btrfs_trans_handle *trans; ··· 6498 6513 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 6499 6514 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 6500 6515 6501 - trans = btrfs_start_transaction(root, 5); 6502 - if (IS_ERR(trans)) 6503 - return PTR_ERR(trans); 6516 + /* 6517 + * Yes ladies and gentelment, this is indeed ugly. The fact is we have 6518 + * 3 things going on here 6519 + * 6520 + * 1) We need to reserve space for our orphan item and the space to 6521 + * delete our orphan item. Lord knows we don't want to have a dangling 6522 + * orphan item because we didn't reserve space to remove it. 6523 + * 6524 + * 2) We need to reserve space to update our inode. 6525 + * 6526 + * 3) We need to have something to cache all the space that is going to 6527 + * be free'd up by the truncate operation, but also have some slack 6528 + * space reserved in case it uses space during the truncate (thank you 6529 + * very much snapshotting). 6530 + * 6531 + * And we need these to all be seperate. The fact is we can use alot of 6532 + * space doing the truncate, and we have no earthly idea how much space 6533 + * we will use, so we need the truncate reservation to be seperate so it 6534 + * doesn't end up using space reserved for updating the inode or 6535 + * removing the orphan item. We also need to be able to stop the 6536 + * transaction and start a new one, which means we need to be able to 6537 + * update the inode several times, and we have no idea of knowing how 6538 + * many times that will be, so we can't just reserve 1 item for the 6539 + * entirety of the opration, so that has to be done seperately as well. 6540 + * Then there is the orphan item, which does indeed need to be held on 6541 + * to for the whole operation, and we need nobody to touch this reserved 6542 + * space except the orphan code. 6543 + * 6544 + * So that leaves us with 6545 + * 6546 + * 1) root->orphan_block_rsv - for the orphan deletion. 6547 + * 2) rsv - for the truncate reservation, which we will steal from the 6548 + * transaction reservation. 6549 + * 3) fs_info->trans_block_rsv - this will have 1 items worth left for 6550 + * updating the inode. 6551 + */ 6552 + rsv = btrfs_alloc_block_rsv(root); 6553 + if (!rsv) 6554 + return -ENOMEM; 6555 + btrfs_add_durable_block_rsv(root->fs_info, rsv); 6504 6556 6505 - btrfs_set_trans_block_group(trans, inode); 6557 + trans = btrfs_start_transaction(root, 4); 6558 + if (IS_ERR(trans)) { 6559 + err = PTR_ERR(trans); 6560 + goto out; 6561 + } 6562 + 6563 + /* 6564 + * Reserve space for the truncate process. Truncate should be adding 6565 + * space, but if there are snapshots it may end up using space. 6566 + */ 6567 + ret = btrfs_truncate_reserve_metadata(trans, root, rsv); 6568 + BUG_ON(ret); 6506 6569 6507 6570 ret = btrfs_orphan_add(trans, inode); 6508 6571 if (ret) { 6509 6572 btrfs_end_transaction(trans, root); 6510 - return ret; 6573 + goto out; 6511 6574 } 6512 6575 6513 6576 nr = trans->blocks_used; 6514 6577 btrfs_end_transaction(trans, root); 6515 6578 btrfs_btree_balance_dirty(root, nr); 6516 6579 6517 - /* Now start a transaction for the truncate */ 6518 - trans = btrfs_start_transaction(root, 0); 6519 - if (IS_ERR(trans)) 6520 - return PTR_ERR(trans); 6521 - btrfs_set_trans_block_group(trans, inode); 6522 - trans->block_rsv = root->orphan_block_rsv; 6580 + /* 6581 + * Ok so we've already migrated our bytes over for the truncate, so here 6582 + * just reserve the one slot we need for updating the inode. 6583 + */ 6584 + trans = btrfs_start_transaction(root, 1); 6585 + if (IS_ERR(trans)) { 6586 + err = PTR_ERR(trans); 6587 + goto out; 6588 + } 6589 + trans->block_rsv = rsv; 6523 6590 6524 6591 /* 6525 6592 * setattr is responsible for setting the ordered_data_close flag, ··· 6595 6558 6596 6559 while (1) { 6597 6560 if (!trans) { 6598 - trans = btrfs_start_transaction(root, 0); 6599 - if (IS_ERR(trans)) 6600 - return PTR_ERR(trans); 6601 - btrfs_set_trans_block_group(trans, inode); 6602 - trans->block_rsv = root->orphan_block_rsv; 6603 - } 6561 + trans = btrfs_start_transaction(root, 3); 6562 + if (IS_ERR(trans)) { 6563 + err = PTR_ERR(trans); 6564 + goto out; 6565 + } 6604 6566 6605 - ret = btrfs_block_rsv_check(trans, root, 6606 - root->orphan_block_rsv, 0, 5); 6607 - if (ret == -EAGAIN) { 6608 - ret = btrfs_commit_transaction(trans, root); 6609 - if (ret) 6610 - return ret; 6611 - trans = NULL; 6612 - continue; 6613 - } else if (ret) { 6614 - err = ret; 6615 - break; 6567 + ret = btrfs_truncate_reserve_metadata(trans, root, 6568 + rsv); 6569 + BUG_ON(ret); 6570 + 6571 + trans->block_rsv = rsv; 6616 6572 } 6617 6573 6618 6574 ret = btrfs_truncate_inode_items(trans, root, inode, ··· 6616 6586 break; 6617 6587 } 6618 6588 6589 + trans->block_rsv = &root->fs_info->trans_block_rsv; 6619 6590 ret = btrfs_update_inode(trans, root, inode); 6620 6591 if (ret) { 6621 6592 err = ret; ··· 6630 6599 } 6631 6600 6632 6601 if (ret == 0 && inode->i_nlink > 0) { 6602 + trans->block_rsv = root->orphan_block_rsv; 6633 6603 ret = btrfs_orphan_del(trans, inode); 6634 6604 if (ret) 6635 6605 err = ret; ··· 6642 6610 ret = btrfs_orphan_del(NULL, inode); 6643 6611 } 6644 6612 6613 + trans->block_rsv = &root->fs_info->trans_block_rsv; 6645 6614 ret = btrfs_update_inode(trans, root, inode); 6646 6615 if (ret && !err) 6647 6616 err = ret; 6648 6617 6649 6618 nr = trans->blocks_used; 6650 6619 ret = btrfs_end_transaction_throttle(trans, root); 6620 + btrfs_btree_balance_dirty(root, nr); 6621 + 6622 + out: 6623 + btrfs_free_block_rsv(root, rsv); 6624 + 6651 6625 if (ret && !err) 6652 6626 err = ret; 6653 - btrfs_btree_balance_dirty(root, nr); 6654 6627 6655 6628 return err; 6656 6629 } ··· 6664 6627 * create a new subvolume directory/inode (helper for the ioctl). 6665 6628 */ 6666 6629 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 6667 - struct btrfs_root *new_root, 6668 - u64 new_dirid, u64 alloc_hint) 6630 + struct btrfs_root *new_root, u64 new_dirid) 6669 6631 { 6670 6632 struct inode *inode; 6671 6633 int err; 6672 6634 u64 index = 0; 6673 6635 6674 6636 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, 6675 - new_dirid, alloc_hint, S_IFDIR | 0700, &index); 6637 + new_dirid, S_IFDIR | 0700, &index); 6676 6638 if (IS_ERR(inode)) 6677 6639 return PTR_ERR(inode); 6678 6640 inode->i_op = &btrfs_dir_inode_operations; ··· 6782 6746 spin_lock(&root->fs_info->ordered_extent_lock); 6783 6747 list_del_init(&BTRFS_I(inode)->ordered_operations); 6784 6748 spin_unlock(&root->fs_info->ordered_extent_lock); 6785 - } 6786 - 6787 - if (root == root->fs_info->tree_root) { 6788 - struct btrfs_block_group_cache *block_group; 6789 - 6790 - block_group = btrfs_lookup_block_group(root->fs_info, 6791 - BTRFS_I(inode)->block_group); 6792 - if (block_group && block_group->inode == inode) { 6793 - spin_lock(&block_group->lock); 6794 - block_group->inode = NULL; 6795 - spin_unlock(&block_group->lock); 6796 - btrfs_put_block_group(block_group); 6797 - } else if (block_group) { 6798 - btrfs_put_block_group(block_group); 6799 - } 6800 6749 } 6801 6750 6802 6751 spin_lock(&root->orphan_lock); ··· 6969 6948 goto out_notrans; 6970 6949 } 6971 6950 6972 - btrfs_set_trans_block_group(trans, new_dir); 6973 - 6974 6951 if (dest != root) 6975 6952 btrfs_record_root_in_trans(trans, dest); 6976 6953 ··· 7150 7131 if (IS_ERR(trans)) 7151 7132 return PTR_ERR(trans); 7152 7133 7153 - btrfs_set_trans_block_group(trans, dir); 7154 - 7155 7134 err = btrfs_find_free_ino(root, &objectid); 7156 7135 if (err) 7157 7136 goto out_unlock; 7158 7137 7159 7138 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7160 7139 dentry->d_name.len, btrfs_ino(dir), objectid, 7161 - BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, 7162 - &index); 7140 + S_IFLNK|S_IRWXUGO, &index); 7163 7141 if (IS_ERR(inode)) { 7164 7142 err = PTR_ERR(inode); 7165 7143 goto out_unlock; ··· 7168 7152 goto out_unlock; 7169 7153 } 7170 7154 7171 - btrfs_set_trans_block_group(trans, inode); 7172 7155 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 7173 7156 if (err) 7174 7157 drop_inode = 1; ··· 7178 7163 inode->i_op = &btrfs_file_inode_operations; 7179 7164 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 7180 7165 } 7181 - btrfs_update_inode_block_group(trans, inode); 7182 - btrfs_update_inode_block_group(trans, dir); 7183 7166 if (drop_inode) 7184 7167 goto out_unlock; 7185 7168
+10 -16
fs/btrfs/ioctl.c
··· 243 243 ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); 244 244 } 245 245 246 - trans = btrfs_join_transaction(root, 1); 246 + trans = btrfs_join_transaction(root); 247 247 BUG_ON(IS_ERR(trans)); 248 248 249 249 ret = btrfs_update_inode(trans, root, inode); ··· 414 414 415 415 btrfs_record_root_in_trans(trans, new_root); 416 416 417 - ret = btrfs_create_subvol_root(trans, new_root, new_dirid, 418 - BTRFS_I(dir)->block_group); 417 + ret = btrfs_create_subvol_root(trans, new_root, new_dirid); 419 418 /* 420 419 * insert the directory item 421 420 */ ··· 706 707 struct btrfs_file_extent_item *extent; 707 708 int type; 708 709 int ret; 710 + u64 ino = btrfs_ino(inode); 709 711 710 712 path = btrfs_alloc_path(); 711 713 if (!path) 712 714 return -ENOMEM; 713 715 714 - min_key.objectid = inode->i_ino; 716 + min_key.objectid = ino; 715 717 min_key.type = BTRFS_EXTENT_DATA_KEY; 716 718 min_key.offset = *off; 717 719 718 - max_key.objectid = inode->i_ino; 720 + max_key.objectid = ino; 719 721 max_key.type = (u8)-1; 720 722 max_key.offset = (u64)-1; 721 723 ··· 727 727 path, 0, newer_than); 728 728 if (ret != 0) 729 729 goto none; 730 - if (min_key.objectid != inode->i_ino) 730 + if (min_key.objectid != ino) 731 731 goto none; 732 732 if (min_key.type != BTRFS_EXTENT_DATA_KEY) 733 733 goto none; ··· 2489 2489 if (ret) 2490 2490 goto out; 2491 2491 2492 - mutex_lock(&root->fs_info->trans_mutex); 2493 - root->fs_info->open_ioctl_trans++; 2494 - mutex_unlock(&root->fs_info->trans_mutex); 2492 + atomic_inc(&root->fs_info->open_ioctl_trans); 2495 2493 2496 2494 ret = -ENOMEM; 2497 - trans = btrfs_start_ioctl_transaction(root, 0); 2495 + trans = btrfs_start_ioctl_transaction(root); 2498 2496 if (IS_ERR(trans)) 2499 2497 goto out_drop; 2500 2498 ··· 2500 2502 return 0; 2501 2503 2502 2504 out_drop: 2503 - mutex_lock(&root->fs_info->trans_mutex); 2504 - root->fs_info->open_ioctl_trans--; 2505 - mutex_unlock(&root->fs_info->trans_mutex); 2505 + atomic_dec(&root->fs_info->open_ioctl_trans); 2506 2506 mnt_drop_write(file->f_path.mnt); 2507 2507 out: 2508 2508 return ret; ··· 2734 2738 2735 2739 btrfs_end_transaction(trans, root); 2736 2740 2737 - mutex_lock(&root->fs_info->trans_mutex); 2738 - root->fs_info->open_ioctl_trans--; 2739 - mutex_unlock(&root->fs_info->trans_mutex); 2741 + atomic_dec(&root->fs_info->open_ioctl_trans); 2740 2742 2741 2743 mnt_drop_write(file->f_path.mnt); 2742 2744 return 0;
+20 -14
fs/btrfs/relocation.c
··· 677 677 err = -ENOMEM; 678 678 goto out; 679 679 } 680 + path1->reada = 1; 681 + path2->reada = 2; 680 682 681 683 node = alloc_backref_node(cache); 682 684 if (!node) { ··· 2001 1999 path = btrfs_alloc_path(); 2002 2000 if (!path) 2003 2001 return -ENOMEM; 2002 + path->reada = 1; 2004 2003 2005 2004 reloc_root = root->reloc_root; 2006 2005 root_item = &reloc_root->root_item; ··· 2142 2139 u64 num_bytes = 0; 2143 2140 int ret; 2144 2141 2145 - mutex_lock(&root->fs_info->trans_mutex); 2142 + spin_lock(&root->fs_info->trans_lock); 2146 2143 rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; 2147 2144 rc->merging_rsv_size += rc->nodes_relocated * 2; 2148 - mutex_unlock(&root->fs_info->trans_mutex); 2145 + spin_unlock(&root->fs_info->trans_lock); 2149 2146 again: 2150 2147 if (!err) { 2151 2148 num_bytes = rc->merging_rsv_size; ··· 2155 2152 err = ret; 2156 2153 } 2157 2154 2158 - trans = btrfs_join_transaction(rc->extent_root, 1); 2155 + trans = btrfs_join_transaction(rc->extent_root); 2159 2156 if (IS_ERR(trans)) { 2160 2157 if (!err) 2161 2158 btrfs_block_rsv_release(rc->extent_root, ··· 2214 2211 int ret; 2215 2212 again: 2216 2213 root = rc->extent_root; 2217 - mutex_lock(&root->fs_info->trans_mutex); 2214 + spin_lock(&root->fs_info->trans_lock); 2218 2215 list_splice_init(&rc->reloc_roots, &reloc_roots); 2219 - mutex_unlock(&root->fs_info->trans_mutex); 2216 + spin_unlock(&root->fs_info->trans_lock); 2220 2217 2221 2218 while (!list_empty(&reloc_roots)) { 2222 2219 found = 1; ··· 3239 3236 goto out; 3240 3237 } 3241 3238 3242 - trans = btrfs_join_transaction(root, 0); 3239 + trans = btrfs_join_transaction(root); 3243 3240 if (IS_ERR(trans)) { 3244 3241 btrfs_free_path(path); 3245 3242 ret = PTR_ERR(trans); ··· 3303 3300 path = btrfs_alloc_path(); 3304 3301 if (!path) 3305 3302 return -ENOMEM; 3303 + path->reada = 1; 3306 3304 3307 3305 root = read_fs_root(rc->extent_root->fs_info, ref_root); 3308 3306 if (IS_ERR(root)) { ··· 3590 3586 static void set_reloc_control(struct reloc_control *rc) 3591 3587 { 3592 3588 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 3593 - mutex_lock(&fs_info->trans_mutex); 3589 + spin_lock(&fs_info->trans_lock); 3594 3590 fs_info->reloc_ctl = rc; 3595 - mutex_unlock(&fs_info->trans_mutex); 3591 + spin_unlock(&fs_info->trans_lock); 3596 3592 } 3597 3593 3598 3594 static void unset_reloc_control(struct reloc_control *rc) 3599 3595 { 3600 3596 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info; 3601 - mutex_lock(&fs_info->trans_mutex); 3597 + spin_lock(&fs_info->trans_lock); 3602 3598 fs_info->reloc_ctl = NULL; 3603 - mutex_unlock(&fs_info->trans_mutex); 3599 + spin_unlock(&fs_info->trans_lock); 3604 3600 } 3605 3601 3606 3602 static int check_extent_flags(u64 flags) ··· 3649 3645 rc->create_reloc_tree = 1; 3650 3646 set_reloc_control(rc); 3651 3647 3652 - trans = btrfs_join_transaction(rc->extent_root, 1); 3648 + trans = btrfs_join_transaction(rc->extent_root); 3653 3649 BUG_ON(IS_ERR(trans)); 3654 3650 btrfs_commit_transaction(trans, rc->extent_root); 3655 3651 return 0; ··· 3672 3668 path = btrfs_alloc_path(); 3673 3669 if (!path) 3674 3670 return -ENOMEM; 3671 + path->reada = 1; 3675 3672 3676 3673 ret = prepare_to_relocate(rc); 3677 3674 if (ret) { ··· 3839 3834 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1); 3840 3835 3841 3836 /* get rid of pinned extents */ 3842 - trans = btrfs_join_transaction(rc->extent_root, 1); 3837 + trans = btrfs_join_transaction(rc->extent_root); 3843 3838 if (IS_ERR(trans)) 3844 3839 err = PTR_ERR(trans); 3845 3840 else ··· 4098 4093 path = btrfs_alloc_path(); 4099 4094 if (!path) 4100 4095 return -ENOMEM; 4096 + path->reada = -1; 4101 4097 4102 4098 key.objectid = BTRFS_TREE_RELOC_OBJECTID; 4103 4099 key.type = BTRFS_ROOT_ITEM_KEY; ··· 4165 4159 4166 4160 set_reloc_control(rc); 4167 4161 4168 - trans = btrfs_join_transaction(rc->extent_root, 1); 4162 + trans = btrfs_join_transaction(rc->extent_root); 4169 4163 if (IS_ERR(trans)) { 4170 4164 unset_reloc_control(rc); 4171 4165 err = PTR_ERR(trans); ··· 4199 4193 4200 4194 unset_reloc_control(rc); 4201 4195 4202 - trans = btrfs_join_transaction(rc->extent_root, 1); 4196 + trans = btrfs_join_transaction(rc->extent_root); 4203 4197 if (IS_ERR(trans)) 4204 4198 err = PTR_ERR(trans); 4205 4199 else
+70 -53
fs/btrfs/scrub.c
··· 117 117 } 118 118 } 119 119 120 + static void scrub_free_bio(struct bio *bio) 121 + { 122 + int i; 123 + struct page *last_page = NULL; 124 + 125 + if (!bio) 126 + return; 127 + 128 + for (i = 0; i < bio->bi_vcnt; ++i) { 129 + if (bio->bi_io_vec[i].bv_page == last_page) 130 + continue; 131 + last_page = bio->bi_io_vec[i].bv_page; 132 + __free_page(last_page); 133 + } 134 + bio_put(bio); 135 + } 136 + 120 137 static noinline_for_stack void scrub_free_dev(struct scrub_dev *sdev) 121 138 { 122 139 int i; 123 - int j; 124 - struct page *last_page; 125 140 126 141 if (!sdev) 127 142 return; 128 143 129 144 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) { 130 145 struct scrub_bio *sbio = sdev->bios[i]; 131 - struct bio *bio; 132 146 133 147 if (!sbio) 134 148 break; 135 149 136 - bio = sbio->bio; 137 - if (bio) { 138 - last_page = NULL; 139 - for (j = 0; j < bio->bi_vcnt; ++j) { 140 - if (bio->bi_io_vec[j].bv_page == last_page) 141 - continue; 142 - last_page = bio->bi_io_vec[j].bv_page; 143 - __free_page(last_page); 144 - } 145 - bio_put(bio); 146 - } 150 + scrub_free_bio(sbio->bio); 147 151 kfree(sbio); 148 152 } 149 153 ··· 160 156 { 161 157 struct scrub_dev *sdev; 162 158 int i; 163 - int j; 164 - int ret; 165 159 struct btrfs_fs_info *fs_info = dev->dev_root->fs_info; 166 160 167 161 sdev = kzalloc(sizeof(*sdev), GFP_NOFS); ··· 167 165 goto nomem; 168 166 sdev->dev = dev; 169 167 for (i = 0; i < SCRUB_BIOS_PER_DEV; ++i) { 170 - struct bio *bio; 171 168 struct scrub_bio *sbio; 172 169 173 170 sbio = kzalloc(sizeof(*sbio), GFP_NOFS); ··· 174 173 goto nomem; 175 174 sdev->bios[i] = sbio; 176 175 177 - bio = bio_kmalloc(GFP_NOFS, SCRUB_PAGES_PER_BIO); 178 - if (!bio) 179 - goto nomem; 180 - 181 176 sbio->index = i; 182 177 sbio->sdev = sdev; 183 - sbio->bio = bio; 184 178 sbio->count = 0; 185 179 sbio->work.func = scrub_checksum; 186 - bio->bi_private = sdev->bios[i]; 187 - bio->bi_end_io = scrub_bio_end_io; 188 - bio->bi_sector = 0; 189 - bio->bi_bdev = dev->bdev; 190 - bio->bi_size = 0; 191 - 192 - for (j = 0; j < SCRUB_PAGES_PER_BIO; ++j) { 193 - struct page *page; 194 - page = alloc_page(GFP_NOFS); 195 - if (!page) 196 - goto nomem; 197 - 198 - ret = bio_add_page(bio, page, PAGE_SIZE, 0); 199 - if (!ret) 200 - goto nomem; 201 - } 202 - WARN_ON(bio->bi_vcnt != SCRUB_PAGES_PER_BIO); 203 180 204 181 if (i != SCRUB_BIOS_PER_DEV-1) 205 182 sdev->bios[i]->next_free = i + 1; ··· 348 369 int ret; 349 370 DECLARE_COMPLETION_ONSTACK(complete); 350 371 351 - /* we are going to wait on this IO */ 352 - rw |= REQ_SYNC; 353 - 354 372 bio = bio_alloc(GFP_NOFS, 1); 355 373 bio->bi_bdev = bdev; 356 374 bio->bi_sector = sector; ··· 356 380 bio->bi_private = &complete; 357 381 submit_bio(rw, bio); 358 382 383 + /* this will also unplug the queue */ 359 384 wait_for_completion(&complete); 360 385 361 386 ret = !test_bit(BIO_UPTODATE, &bio->bi_flags); ··· 371 394 struct btrfs_fs_info *fs_info = sdev->dev->dev_root->fs_info; 372 395 373 396 sbio->err = err; 397 + sbio->bio = bio; 374 398 375 399 btrfs_queue_worker(&fs_info->scrub_workers, &sbio->work); 376 400 } ··· 431 453 } 432 454 433 455 out: 456 + scrub_free_bio(sbio->bio); 457 + sbio->bio = NULL; 434 458 spin_lock(&sdev->list_lock); 435 459 sbio->next_free = sdev->first_free; 436 460 sdev->first_free = sbio->index; ··· 563 583 static int scrub_submit(struct scrub_dev *sdev) 564 584 { 565 585 struct scrub_bio *sbio; 586 + struct bio *bio; 587 + int i; 566 588 567 589 if (sdev->curr == -1) 568 590 return 0; 569 591 570 592 sbio = sdev->bios[sdev->curr]; 571 593 572 - sbio->bio->bi_sector = sbio->physical >> 9; 573 - sbio->bio->bi_size = sbio->count * PAGE_SIZE; 574 - sbio->bio->bi_next = NULL; 575 - sbio->bio->bi_flags |= 1 << BIO_UPTODATE; 576 - sbio->bio->bi_comp_cpu = -1; 577 - sbio->bio->bi_bdev = sdev->dev->bdev; 594 + bio = bio_alloc(GFP_NOFS, sbio->count); 595 + if (!bio) 596 + goto nomem; 597 + 598 + bio->bi_private = sbio; 599 + bio->bi_end_io = scrub_bio_end_io; 600 + bio->bi_bdev = sdev->dev->bdev; 601 + bio->bi_sector = sbio->physical >> 9; 602 + 603 + for (i = 0; i < sbio->count; ++i) { 604 + struct page *page; 605 + int ret; 606 + 607 + page = alloc_page(GFP_NOFS); 608 + if (!page) 609 + goto nomem; 610 + 611 + ret = bio_add_page(bio, page, PAGE_SIZE, 0); 612 + if (!ret) { 613 + __free_page(page); 614 + goto nomem; 615 + } 616 + } 617 + 578 618 sbio->err = 0; 579 619 sdev->curr = -1; 580 620 atomic_inc(&sdev->in_flight); 581 621 582 - submit_bio(0, sbio->bio); 622 + submit_bio(READ, bio); 583 623 584 624 return 0; 625 + 626 + nomem: 627 + scrub_free_bio(bio); 628 + 629 + return -ENOMEM; 585 630 } 586 631 587 632 static int scrub_page(struct scrub_dev *sdev, u64 logical, u64 len, ··· 638 633 sbio->logical = logical; 639 634 } else if (sbio->physical + sbio->count * PAGE_SIZE != physical || 640 635 sbio->logical + sbio->count * PAGE_SIZE != logical) { 641 - scrub_submit(sdev); 636 + int ret; 637 + 638 + ret = scrub_submit(sdev); 639 + if (ret) 640 + return ret; 642 641 goto again; 643 642 } 644 643 sbio->spag[sbio->count].flags = flags; ··· 654 645 memcpy(sbio->spag[sbio->count].csum, csum, sdev->csum_size); 655 646 } 656 647 ++sbio->count; 657 - if (sbio->count == SCRUB_PAGES_PER_BIO || force) 658 - scrub_submit(sdev); 648 + if (sbio->count == SCRUB_PAGES_PER_BIO || force) { 649 + int ret; 650 + 651 + ret = scrub_submit(sdev); 652 + if (ret) 653 + return ret; 654 + } 659 655 660 656 return 0; 661 657 } ··· 741 727 struct btrfs_root *root = fs_info->extent_root; 742 728 struct btrfs_root *csum_root = fs_info->csum_root; 743 729 struct btrfs_extent_item *extent; 730 + struct blk_plug plug; 744 731 u64 flags; 745 732 int ret; 746 733 int slot; ··· 846 831 * the scrub. This might currently (crc32) end up to be about 1MB 847 832 */ 848 833 start_stripe = 0; 834 + blk_start_plug(&plug); 849 835 again: 850 836 logical = base + offset + start_stripe * increment; 851 837 for (i = start_stripe; i < nstripes; ++i) { ··· 988 972 scrub_submit(sdev); 989 973 990 974 out: 975 + blk_finish_plug(&plug); 991 976 btrfs_free_path(path); 992 977 return ret < 0 ? ret : 0; 993 978 } ··· 1183 1166 int ret; 1184 1167 struct btrfs_device *dev; 1185 1168 1186 - if (root->fs_info->closing) 1169 + if (btrfs_fs_closing(root->fs_info)) 1187 1170 return -EINVAL; 1188 1171 1189 1172 /*
+7 -1
fs/btrfs/super.c
··· 161 161 Opt_compress_type, Opt_compress_force, Opt_compress_force_type, 162 162 Opt_notreelog, Opt_ratio, Opt_flushoncommit, Opt_discard, 163 163 Opt_space_cache, Opt_clear_cache, Opt_user_subvol_rm_allowed, 164 - Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, Opt_err, 164 + Opt_enospc_debug, Opt_subvolrootid, Opt_defrag, 165 + Opt_inode_cache, Opt_err, 165 166 }; 166 167 167 168 static match_table_t tokens = { ··· 194 193 {Opt_enospc_debug, "enospc_debug"}, 195 194 {Opt_subvolrootid, "subvolrootid=%d"}, 196 195 {Opt_defrag, "autodefrag"}, 196 + {Opt_inode_cache, "inode_cache"}, 197 197 {Opt_err, NULL}, 198 198 }; 199 199 ··· 362 360 case Opt_space_cache: 363 361 printk(KERN_INFO "btrfs: enabling disk space caching\n"); 364 362 btrfs_set_opt(info->mount_opt, SPACE_CACHE); 363 + break; 364 + case Opt_inode_cache: 365 + printk(KERN_INFO "btrfs: enabling inode map caching\n"); 366 + btrfs_set_opt(info->mount_opt, INODE_MAP_CACHE); 365 367 break; 366 368 case Opt_clear_cache: 367 369 printk(KERN_INFO "btrfs: force clearing of disk cache\n");
+164 -138
fs/btrfs/transaction.c
··· 35 35 { 36 36 WARN_ON(atomic_read(&transaction->use_count) == 0); 37 37 if (atomic_dec_and_test(&transaction->use_count)) { 38 + BUG_ON(!list_empty(&transaction->list)); 38 39 memset(transaction, 0, sizeof(*transaction)); 39 40 kmem_cache_free(btrfs_transaction_cachep, transaction); 40 41 } ··· 50 49 /* 51 50 * either allocate a new transaction or hop into the existing one 52 51 */ 53 - static noinline int join_transaction(struct btrfs_root *root) 52 + static noinline int join_transaction(struct btrfs_root *root, int nofail) 54 53 { 55 54 struct btrfs_transaction *cur_trans; 55 + 56 + spin_lock(&root->fs_info->trans_lock); 57 + if (root->fs_info->trans_no_join) { 58 + if (!nofail) { 59 + spin_unlock(&root->fs_info->trans_lock); 60 + return -EBUSY; 61 + } 62 + } 63 + 56 64 cur_trans = root->fs_info->running_transaction; 57 - if (!cur_trans) { 58 - cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 59 - GFP_NOFS); 60 - if (!cur_trans) 61 - return -ENOMEM; 62 - root->fs_info->generation++; 63 - atomic_set(&cur_trans->num_writers, 1); 64 - cur_trans->num_joined = 0; 65 - cur_trans->transid = root->fs_info->generation; 66 - init_waitqueue_head(&cur_trans->writer_wait); 67 - init_waitqueue_head(&cur_trans->commit_wait); 68 - cur_trans->in_commit = 0; 69 - cur_trans->blocked = 0; 70 - atomic_set(&cur_trans->use_count, 1); 71 - cur_trans->commit_done = 0; 72 - cur_trans->start_time = get_seconds(); 73 - 74 - cur_trans->delayed_refs.root = RB_ROOT; 75 - cur_trans->delayed_refs.num_entries = 0; 76 - cur_trans->delayed_refs.num_heads_ready = 0; 77 - cur_trans->delayed_refs.num_heads = 0; 78 - cur_trans->delayed_refs.flushing = 0; 79 - cur_trans->delayed_refs.run_delayed_start = 0; 80 - spin_lock_init(&cur_trans->delayed_refs.lock); 81 - 82 - INIT_LIST_HEAD(&cur_trans->pending_snapshots); 83 - list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 84 - extent_io_tree_init(&cur_trans->dirty_pages, 85 - root->fs_info->btree_inode->i_mapping); 86 - spin_lock(&root->fs_info->new_trans_lock); 87 - root->fs_info->running_transaction = cur_trans; 88 - spin_unlock(&root->fs_info->new_trans_lock); 89 - } else { 65 + if (cur_trans) { 66 + atomic_inc(&cur_trans->use_count); 90 67 atomic_inc(&cur_trans->num_writers); 91 68 cur_trans->num_joined++; 69 + spin_unlock(&root->fs_info->trans_lock); 70 + return 0; 92 71 } 72 + spin_unlock(&root->fs_info->trans_lock); 73 + 74 + cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS); 75 + if (!cur_trans) 76 + return -ENOMEM; 77 + spin_lock(&root->fs_info->trans_lock); 78 + if (root->fs_info->running_transaction) { 79 + kmem_cache_free(btrfs_transaction_cachep, cur_trans); 80 + cur_trans = root->fs_info->running_transaction; 81 + atomic_inc(&cur_trans->use_count); 82 + atomic_inc(&cur_trans->num_writers); 83 + cur_trans->num_joined++; 84 + spin_unlock(&root->fs_info->trans_lock); 85 + return 0; 86 + } 87 + atomic_set(&cur_trans->num_writers, 1); 88 + cur_trans->num_joined = 0; 89 + init_waitqueue_head(&cur_trans->writer_wait); 90 + init_waitqueue_head(&cur_trans->commit_wait); 91 + cur_trans->in_commit = 0; 92 + cur_trans->blocked = 0; 93 + /* 94 + * One for this trans handle, one so it will live on until we 95 + * commit the transaction. 96 + */ 97 + atomic_set(&cur_trans->use_count, 2); 98 + cur_trans->commit_done = 0; 99 + cur_trans->start_time = get_seconds(); 100 + 101 + cur_trans->delayed_refs.root = RB_ROOT; 102 + cur_trans->delayed_refs.num_entries = 0; 103 + cur_trans->delayed_refs.num_heads_ready = 0; 104 + cur_trans->delayed_refs.num_heads = 0; 105 + cur_trans->delayed_refs.flushing = 0; 106 + cur_trans->delayed_refs.run_delayed_start = 0; 107 + spin_lock_init(&cur_trans->commit_lock); 108 + spin_lock_init(&cur_trans->delayed_refs.lock); 109 + 110 + INIT_LIST_HEAD(&cur_trans->pending_snapshots); 111 + list_add_tail(&cur_trans->list, &root->fs_info->trans_list); 112 + extent_io_tree_init(&cur_trans->dirty_pages, 113 + root->fs_info->btree_inode->i_mapping); 114 + root->fs_info->generation++; 115 + cur_trans->transid = root->fs_info->generation; 116 + root->fs_info->running_transaction = cur_trans; 117 + spin_unlock(&root->fs_info->trans_lock); 93 118 94 119 return 0; 95 120 } ··· 126 99 * to make sure the old root from before we joined the transaction is deleted 127 100 * when the transaction commits 128 101 */ 129 - static noinline int record_root_in_trans(struct btrfs_trans_handle *trans, 130 - struct btrfs_root *root) 102 + int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 103 + struct btrfs_root *root) 131 104 { 132 105 if (root->ref_cows && root->last_trans < trans->transid) { 133 106 WARN_ON(root == root->fs_info->extent_root); 134 107 WARN_ON(root->commit_root != root->node); 135 108 109 + spin_lock(&root->fs_info->fs_roots_radix_lock); 110 + if (root->last_trans == trans->transid) { 111 + spin_unlock(&root->fs_info->fs_roots_radix_lock); 112 + return 0; 113 + } 114 + root->last_trans = trans->transid; 136 115 radix_tree_tag_set(&root->fs_info->fs_roots_radix, 137 116 (unsigned long)root->root_key.objectid, 138 117 BTRFS_ROOT_TRANS_TAG); 139 - root->last_trans = trans->transid; 118 + spin_unlock(&root->fs_info->fs_roots_radix_lock); 140 119 btrfs_init_reloc_root(trans, root); 141 120 } 142 - return 0; 143 - } 144 - 145 - int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 146 - struct btrfs_root *root) 147 - { 148 - if (!root->ref_cows) 149 - return 0; 150 - 151 - mutex_lock(&root->fs_info->trans_mutex); 152 - if (root->last_trans == trans->transid) { 153 - mutex_unlock(&root->fs_info->trans_mutex); 154 - return 0; 155 - } 156 - 157 - record_root_in_trans(trans, root); 158 - mutex_unlock(&root->fs_info->trans_mutex); 159 121 return 0; 160 122 } 161 123 ··· 156 140 { 157 141 struct btrfs_transaction *cur_trans; 158 142 143 + spin_lock(&root->fs_info->trans_lock); 159 144 cur_trans = root->fs_info->running_transaction; 160 145 if (cur_trans && cur_trans->blocked) { 161 146 DEFINE_WAIT(wait); 162 147 atomic_inc(&cur_trans->use_count); 148 + spin_unlock(&root->fs_info->trans_lock); 163 149 while (1) { 164 150 prepare_to_wait(&root->fs_info->transaction_wait, &wait, 165 151 TASK_UNINTERRUPTIBLE); 166 152 if (!cur_trans->blocked) 167 153 break; 168 - mutex_unlock(&root->fs_info->trans_mutex); 169 154 schedule(); 170 - mutex_lock(&root->fs_info->trans_mutex); 171 155 } 172 156 finish_wait(&root->fs_info->transaction_wait, &wait); 173 157 put_transaction(cur_trans); 158 + } else { 159 + spin_unlock(&root->fs_info->trans_lock); 174 160 } 175 161 } 176 162 ··· 185 167 186 168 static int may_wait_transaction(struct btrfs_root *root, int type) 187 169 { 188 - if (!root->fs_info->log_root_recovering && 189 - ((type == TRANS_START && !root->fs_info->open_ioctl_trans) || 190 - type == TRANS_USERSPACE)) 170 + if (root->fs_info->log_root_recovering) 171 + return 0; 172 + 173 + if (type == TRANS_USERSPACE) 191 174 return 1; 175 + 176 + if (type == TRANS_START && 177 + !atomic_read(&root->fs_info->open_ioctl_trans)) 178 + return 1; 179 + 192 180 return 0; 193 181 } 194 182 ··· 208 184 209 185 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) 210 186 return ERR_PTR(-EROFS); 187 + 188 + if (current->journal_info) { 189 + WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK); 190 + h = current->journal_info; 191 + h->use_count++; 192 + h->orig_rsv = h->block_rsv; 193 + h->block_rsv = NULL; 194 + goto got_it; 195 + } 211 196 again: 212 197 h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS); 213 198 if (!h) 214 199 return ERR_PTR(-ENOMEM); 215 200 216 - if (type != TRANS_JOIN_NOLOCK) 217 - mutex_lock(&root->fs_info->trans_mutex); 218 201 if (may_wait_transaction(root, type)) 219 202 wait_current_trans(root); 220 203 221 - ret = join_transaction(root); 204 + do { 205 + ret = join_transaction(root, type == TRANS_JOIN_NOLOCK); 206 + if (ret == -EBUSY) 207 + wait_current_trans(root); 208 + } while (ret == -EBUSY); 209 + 222 210 if (ret < 0) { 223 211 kmem_cache_free(btrfs_trans_handle_cachep, h); 224 - if (type != TRANS_JOIN_NOLOCK) 225 - mutex_unlock(&root->fs_info->trans_mutex); 226 212 return ERR_PTR(ret); 227 213 } 228 214 229 215 cur_trans = root->fs_info->running_transaction; 230 - atomic_inc(&cur_trans->use_count); 231 - if (type != TRANS_JOIN_NOLOCK) 232 - mutex_unlock(&root->fs_info->trans_mutex); 233 216 234 217 h->transid = cur_trans->transid; 235 218 h->transaction = cur_trans; 236 219 h->blocks_used = 0; 237 - h->block_group = 0; 238 220 h->bytes_reserved = 0; 239 221 h->delayed_ref_updates = 0; 222 + h->use_count = 1; 240 223 h->block_rsv = NULL; 224 + h->orig_rsv = NULL; 241 225 242 226 smp_mb(); 243 227 if (cur_trans->blocked && may_wait_transaction(root, type)) { ··· 273 241 } 274 242 } 275 243 276 - if (type != TRANS_JOIN_NOLOCK) 277 - mutex_lock(&root->fs_info->trans_mutex); 278 - record_root_in_trans(h, root); 279 - if (type != TRANS_JOIN_NOLOCK) 280 - mutex_unlock(&root->fs_info->trans_mutex); 244 + got_it: 245 + btrfs_record_root_in_trans(h, root); 281 246 282 247 if (!current->journal_info && type != TRANS_USERSPACE) 283 248 current->journal_info = h; ··· 286 257 { 287 258 return start_transaction(root, num_items, TRANS_START); 288 259 } 289 - struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 290 - int num_blocks) 260 + struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root) 291 261 { 292 262 return start_transaction(root, 0, TRANS_JOIN); 293 263 } 294 264 295 - struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root, 296 - int num_blocks) 265 + struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root) 297 266 { 298 267 return start_transaction(root, 0, TRANS_JOIN_NOLOCK); 299 268 } 300 269 301 - struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 302 - int num_blocks) 270 + struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root) 303 271 { 304 - return start_transaction(r, 0, TRANS_USERSPACE); 272 + return start_transaction(root, 0, TRANS_USERSPACE); 305 273 } 306 274 307 275 /* wait for a transaction commit to be fully complete */ ··· 306 280 struct btrfs_transaction *commit) 307 281 { 308 282 DEFINE_WAIT(wait); 309 - mutex_lock(&root->fs_info->trans_mutex); 310 283 while (!commit->commit_done) { 311 284 prepare_to_wait(&commit->commit_wait, &wait, 312 285 TASK_UNINTERRUPTIBLE); 313 286 if (commit->commit_done) 314 287 break; 315 - mutex_unlock(&root->fs_info->trans_mutex); 316 288 schedule(); 317 - mutex_lock(&root->fs_info->trans_mutex); 318 289 } 319 - mutex_unlock(&root->fs_info->trans_mutex); 320 290 finish_wait(&commit->commit_wait, &wait); 321 291 return 0; 322 292 } ··· 322 300 struct btrfs_transaction *cur_trans = NULL, *t; 323 301 int ret; 324 302 325 - mutex_lock(&root->fs_info->trans_mutex); 326 - 327 303 ret = 0; 328 304 if (transid) { 329 305 if (transid <= root->fs_info->last_trans_committed) 330 - goto out_unlock; 306 + goto out; 331 307 332 308 /* find specified transaction */ 309 + spin_lock(&root->fs_info->trans_lock); 333 310 list_for_each_entry(t, &root->fs_info->trans_list, list) { 334 311 if (t->transid == transid) { 335 312 cur_trans = t; 313 + atomic_inc(&cur_trans->use_count); 336 314 break; 337 315 } 338 316 if (t->transid > transid) 339 317 break; 340 318 } 319 + spin_unlock(&root->fs_info->trans_lock); 341 320 ret = -EINVAL; 342 321 if (!cur_trans) 343 - goto out_unlock; /* bad transid */ 322 + goto out; /* bad transid */ 344 323 } else { 345 324 /* find newest transaction that is committing | committed */ 325 + spin_lock(&root->fs_info->trans_lock); 346 326 list_for_each_entry_reverse(t, &root->fs_info->trans_list, 347 327 list) { 348 328 if (t->in_commit) { 349 329 if (t->commit_done) 350 - goto out_unlock; 330 + goto out; 351 331 cur_trans = t; 332 + atomic_inc(&cur_trans->use_count); 352 333 break; 353 334 } 354 335 } 336 + spin_unlock(&root->fs_info->trans_lock); 355 337 if (!cur_trans) 356 - goto out_unlock; /* nothing committing|committed */ 338 + goto out; /* nothing committing|committed */ 357 339 } 358 - 359 - atomic_inc(&cur_trans->use_count); 360 - mutex_unlock(&root->fs_info->trans_mutex); 361 340 362 341 wait_for_commit(root, cur_trans); 363 342 364 - mutex_lock(&root->fs_info->trans_mutex); 365 343 put_transaction(cur_trans); 366 344 ret = 0; 367 - out_unlock: 368 - mutex_unlock(&root->fs_info->trans_mutex); 345 + out: 369 346 return ret; 370 347 } 371 348 372 349 void btrfs_throttle(struct btrfs_root *root) 373 350 { 374 - mutex_lock(&root->fs_info->trans_mutex); 375 - if (!root->fs_info->open_ioctl_trans) 351 + if (!atomic_read(&root->fs_info->open_ioctl_trans)) 376 352 wait_current_trans(root); 377 - mutex_unlock(&root->fs_info->trans_mutex); 378 353 } 379 354 380 355 static int should_end_transaction(struct btrfs_trans_handle *trans, ··· 389 370 struct btrfs_transaction *cur_trans = trans->transaction; 390 371 int updates; 391 372 373 + smp_mb(); 392 374 if (cur_trans->blocked || cur_trans->delayed_refs.flushing) 393 375 return 1; 394 376 ··· 407 387 struct btrfs_transaction *cur_trans = trans->transaction; 408 388 struct btrfs_fs_info *info = root->fs_info; 409 389 int count = 0; 390 + 391 + if (--trans->use_count) { 392 + trans->block_rsv = trans->orig_rsv; 393 + return 0; 394 + } 410 395 411 396 while (count < 4) { 412 397 unsigned long cur = trans->delayed_ref_updates; ··· 435 410 436 411 btrfs_trans_release_metadata(trans, root); 437 412 438 - if (lock && !root->fs_info->open_ioctl_trans && 439 - should_end_transaction(trans, root)) 413 + if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 414 + should_end_transaction(trans, root)) { 440 415 trans->transaction->blocked = 1; 416 + smp_wmb(); 417 + } 441 418 442 419 if (lock && cur_trans->blocked && !cur_trans->in_commit) { 443 420 if (throttle) ··· 730 703 */ 731 704 int btrfs_add_dead_root(struct btrfs_root *root) 732 705 { 733 - mutex_lock(&root->fs_info->trans_mutex); 706 + spin_lock(&root->fs_info->trans_lock); 734 707 list_add(&root->root_list, &root->fs_info->dead_roots); 735 - mutex_unlock(&root->fs_info->trans_mutex); 708 + spin_unlock(&root->fs_info->trans_lock); 736 709 return 0; 737 710 } 738 711 ··· 748 721 int ret; 749 722 int err = 0; 750 723 724 + spin_lock(&fs_info->fs_roots_radix_lock); 751 725 while (1) { 752 726 ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix, 753 727 (void **)gang, 0, ··· 761 733 radix_tree_tag_clear(&fs_info->fs_roots_radix, 762 734 (unsigned long)root->root_key.objectid, 763 735 BTRFS_ROOT_TRANS_TAG); 736 + spin_unlock(&fs_info->fs_roots_radix_lock); 764 737 765 738 btrfs_free_log(trans, root); 766 739 btrfs_update_reloc_root(trans, root); ··· 782 753 err = btrfs_update_root(trans, fs_info->tree_root, 783 754 &root->root_key, 784 755 &root->root_item); 756 + spin_lock(&fs_info->fs_roots_radix_lock); 785 757 if (err) 786 758 break; 787 759 } 788 760 } 761 + spin_unlock(&fs_info->fs_roots_radix_lock); 789 762 return err; 790 763 } 791 764 ··· 817 786 btrfs_btree_balance_dirty(info->tree_root, nr); 818 787 cond_resched(); 819 788 820 - if (root->fs_info->closing || ret != -EAGAIN) 789 + if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN) 821 790 break; 822 791 } 823 792 root->defrag_running = 0; ··· 882 851 parent = dget_parent(dentry); 883 852 parent_inode = parent->d_inode; 884 853 parent_root = BTRFS_I(parent_inode)->root; 885 - record_root_in_trans(trans, parent_root); 854 + btrfs_record_root_in_trans(trans, parent_root); 886 855 887 856 /* 888 857 * insert the directory item ··· 900 869 ret = btrfs_update_inode(trans, parent_root, parent_inode); 901 870 BUG_ON(ret); 902 871 903 - record_root_in_trans(trans, root); 872 + btrfs_record_root_in_trans(trans, root); 904 873 btrfs_set_root_last_snapshot(&root->root_item, trans->transid); 905 874 memcpy(new_root_item, &root->root_item, sizeof(*new_root_item)); 906 875 btrfs_check_and_init_root_item(new_root_item); ··· 998 967 int btrfs_transaction_in_commit(struct btrfs_fs_info *info) 999 968 { 1000 969 int ret = 0; 1001 - spin_lock(&info->new_trans_lock); 970 + spin_lock(&info->trans_lock); 1002 971 if (info->running_transaction) 1003 972 ret = info->running_transaction->in_commit; 1004 - spin_unlock(&info->new_trans_lock); 973 + spin_unlock(&info->trans_lock); 1005 974 return ret; 1006 975 } 1007 976 1008 977 int btrfs_transaction_blocked(struct btrfs_fs_info *info) 1009 978 { 1010 979 int ret = 0; 1011 - spin_lock(&info->new_trans_lock); 980 + spin_lock(&info->trans_lock); 1012 981 if (info->running_transaction) 1013 982 ret = info->running_transaction->blocked; 1014 - spin_unlock(&info->new_trans_lock); 983 + spin_unlock(&info->trans_lock); 1015 984 return ret; 1016 985 } 1017 986 ··· 1035 1004 &wait); 1036 1005 break; 1037 1006 } 1038 - mutex_unlock(&root->fs_info->trans_mutex); 1039 1007 schedule(); 1040 - mutex_lock(&root->fs_info->trans_mutex); 1041 1008 finish_wait(&root->fs_info->transaction_blocked_wait, &wait); 1042 1009 } 1043 1010 } ··· 1061 1032 &wait); 1062 1033 break; 1063 1034 } 1064 - mutex_unlock(&root->fs_info->trans_mutex); 1065 1035 schedule(); 1066 - mutex_lock(&root->fs_info->trans_mutex); 1067 1036 finish_wait(&root->fs_info->transaction_wait, 1068 1037 &wait); 1069 1038 } ··· 1099 1072 1100 1073 INIT_DELAYED_WORK(&ac->work, do_async_commit); 1101 1074 ac->root = root; 1102 - ac->newtrans = btrfs_join_transaction(root, 0); 1075 + ac->newtrans = btrfs_join_transaction(root); 1103 1076 if (IS_ERR(ac->newtrans)) { 1104 1077 int err = PTR_ERR(ac->newtrans); 1105 1078 kfree(ac); ··· 1107 1080 } 1108 1081 1109 1082 /* take transaction reference */ 1110 - mutex_lock(&root->fs_info->trans_mutex); 1111 1083 cur_trans = trans->transaction; 1112 1084 atomic_inc(&cur_trans->use_count); 1113 - mutex_unlock(&root->fs_info->trans_mutex); 1114 1085 1115 1086 btrfs_end_transaction(trans, root); 1116 1087 schedule_delayed_work(&ac->work, 0); 1117 1088 1118 1089 /* wait for transaction to start and unblock */ 1119 - mutex_lock(&root->fs_info->trans_mutex); 1120 1090 if (wait_for_unblock) 1121 1091 wait_current_trans_commit_start_and_unblock(root, cur_trans); 1122 1092 else 1123 1093 wait_current_trans_commit_start(root, cur_trans); 1124 1094 put_transaction(cur_trans); 1125 - mutex_unlock(&root->fs_info->trans_mutex); 1126 1095 1127 1096 return 0; 1128 1097 } ··· 1162 1139 ret = btrfs_run_delayed_refs(trans, root, 0); 1163 1140 BUG_ON(ret); 1164 1141 1165 - mutex_lock(&root->fs_info->trans_mutex); 1142 + spin_lock(&cur_trans->commit_lock); 1166 1143 if (cur_trans->in_commit) { 1144 + spin_unlock(&cur_trans->commit_lock); 1167 1145 atomic_inc(&cur_trans->use_count); 1168 - mutex_unlock(&root->fs_info->trans_mutex); 1169 1146 btrfs_end_transaction(trans, root); 1170 1147 1171 1148 ret = wait_for_commit(root, cur_trans); 1172 1149 BUG_ON(ret); 1173 1150 1174 - mutex_lock(&root->fs_info->trans_mutex); 1175 1151 put_transaction(cur_trans); 1176 - mutex_unlock(&root->fs_info->trans_mutex); 1177 1152 1178 1153 return 0; 1179 1154 } 1180 1155 1181 1156 trans->transaction->in_commit = 1; 1182 1157 trans->transaction->blocked = 1; 1158 + spin_unlock(&cur_trans->commit_lock); 1183 1159 wake_up(&root->fs_info->transaction_blocked_wait); 1184 1160 1161 + spin_lock(&root->fs_info->trans_lock); 1185 1162 if (cur_trans->list.prev != &root->fs_info->trans_list) { 1186 1163 prev_trans = list_entry(cur_trans->list.prev, 1187 1164 struct btrfs_transaction, list); 1188 1165 if (!prev_trans->commit_done) { 1189 1166 atomic_inc(&prev_trans->use_count); 1190 - mutex_unlock(&root->fs_info->trans_mutex); 1167 + spin_unlock(&root->fs_info->trans_lock); 1191 1168 1192 1169 wait_for_commit(root, prev_trans); 1193 1170 1194 - mutex_lock(&root->fs_info->trans_mutex); 1195 1171 put_transaction(prev_trans); 1172 + } else { 1173 + spin_unlock(&root->fs_info->trans_lock); 1196 1174 } 1175 + } else { 1176 + spin_unlock(&root->fs_info->trans_lock); 1197 1177 } 1198 1178 1199 1179 if (now < cur_trans->start_time || now - cur_trans->start_time < 1) ··· 1204 1178 1205 1179 do { 1206 1180 int snap_pending = 0; 1181 + 1207 1182 joined = cur_trans->num_joined; 1208 1183 if (!list_empty(&trans->transaction->pending_snapshots)) 1209 1184 snap_pending = 1; 1210 1185 1211 1186 WARN_ON(cur_trans != trans->transaction); 1212 - mutex_unlock(&root->fs_info->trans_mutex); 1213 1187 1214 1188 if (flush_on_commit || snap_pending) { 1215 1189 btrfs_start_delalloc_inodes(root, 1); ··· 1232 1206 prepare_to_wait(&cur_trans->writer_wait, &wait, 1233 1207 TASK_UNINTERRUPTIBLE); 1234 1208 1235 - smp_mb(); 1236 1209 if (atomic_read(&cur_trans->num_writers) > 1) 1237 1210 schedule_timeout(MAX_SCHEDULE_TIMEOUT); 1238 1211 else if (should_grow) 1239 1212 schedule_timeout(1); 1240 1213 1241 - mutex_lock(&root->fs_info->trans_mutex); 1242 1214 finish_wait(&cur_trans->writer_wait, &wait); 1215 + spin_lock(&root->fs_info->trans_lock); 1216 + root->fs_info->trans_no_join = 1; 1217 + spin_unlock(&root->fs_info->trans_lock); 1243 1218 } while (atomic_read(&cur_trans->num_writers) > 1 || 1244 1219 (should_grow && cur_trans->num_joined != joined)); 1245 1220 ··· 1285 1258 btrfs_prepare_extent_commit(trans, root); 1286 1259 1287 1260 cur_trans = root->fs_info->running_transaction; 1288 - spin_lock(&root->fs_info->new_trans_lock); 1289 - root->fs_info->running_transaction = NULL; 1290 - spin_unlock(&root->fs_info->new_trans_lock); 1291 1261 1292 1262 btrfs_set_root_node(&root->fs_info->tree_root->root_item, 1293 1263 root->fs_info->tree_root->node); ··· 1305 1281 sizeof(root->fs_info->super_copy)); 1306 1282 1307 1283 trans->transaction->blocked = 0; 1284 + spin_lock(&root->fs_info->trans_lock); 1285 + root->fs_info->running_transaction = NULL; 1286 + root->fs_info->trans_no_join = 0; 1287 + spin_unlock(&root->fs_info->trans_lock); 1308 1288 1309 1289 wake_up(&root->fs_info->transaction_wait); 1310 1290 1311 - mutex_unlock(&root->fs_info->trans_mutex); 1312 1291 ret = btrfs_write_and_wait_transaction(trans, root); 1313 1292 BUG_ON(ret); 1314 1293 write_ctree_super(trans, root, 0); ··· 1324 1297 1325 1298 btrfs_finish_extent_commit(trans, root); 1326 1299 1327 - mutex_lock(&root->fs_info->trans_mutex); 1328 - 1329 1300 cur_trans->commit_done = 1; 1330 1301 1331 1302 root->fs_info->last_trans_committed = cur_trans->transid; 1332 1303 1333 1304 wake_up(&cur_trans->commit_wait); 1334 1305 1306 + spin_lock(&root->fs_info->trans_lock); 1335 1307 list_del_init(&cur_trans->list); 1308 + spin_unlock(&root->fs_info->trans_lock); 1309 + 1336 1310 put_transaction(cur_trans); 1337 1311 put_transaction(cur_trans); 1338 1312 1339 1313 trace_btrfs_transaction_commit(root); 1340 - 1341 - mutex_unlock(&root->fs_info->trans_mutex); 1342 1314 1343 1315 btrfs_scrub_continue(root); 1344 1316 ··· 1360 1334 LIST_HEAD(list); 1361 1335 struct btrfs_fs_info *fs_info = root->fs_info; 1362 1336 1363 - mutex_lock(&fs_info->trans_mutex); 1337 + spin_lock(&fs_info->trans_lock); 1364 1338 list_splice_init(&fs_info->dead_roots, &list); 1365 - mutex_unlock(&fs_info->trans_mutex); 1339 + spin_unlock(&fs_info->trans_lock); 1366 1340 1367 1341 while (!list_empty(&list)) { 1368 1342 root = list_entry(list.next, struct btrfs_root, root_list);
+8 -21
fs/btrfs/transaction.h
··· 28 28 * transaction can end 29 29 */ 30 30 atomic_t num_writers; 31 + atomic_t use_count; 31 32 32 33 unsigned long num_joined; 34 + 35 + spinlock_t commit_lock; 33 36 int in_commit; 34 - atomic_t use_count; 35 37 int commit_done; 36 38 int blocked; 37 39 struct list_head list; ··· 47 45 48 46 struct btrfs_trans_handle { 49 47 u64 transid; 50 - u64 block_group; 51 48 u64 bytes_reserved; 49 + unsigned long use_count; 52 50 unsigned long blocks_reserved; 53 51 unsigned long blocks_used; 54 52 unsigned long delayed_ref_updates; 55 53 struct btrfs_transaction *transaction; 56 54 struct btrfs_block_rsv *block_rsv; 55 + struct btrfs_block_rsv *orig_rsv; 57 56 }; 58 57 59 58 struct btrfs_pending_snapshot { ··· 69 66 struct list_head list; 70 67 }; 71 68 72 - static inline void btrfs_set_trans_block_group(struct btrfs_trans_handle *trans, 73 - struct inode *inode) 74 - { 75 - trans->block_group = BTRFS_I(inode)->block_group; 76 - } 77 - 78 - static inline void btrfs_update_inode_block_group( 79 - struct btrfs_trans_handle *trans, 80 - struct inode *inode) 81 - { 82 - BTRFS_I(inode)->block_group = trans->block_group; 83 - } 84 - 85 69 static inline void btrfs_set_inode_last_trans(struct btrfs_trans_handle *trans, 86 70 struct inode *inode) 87 71 { ··· 82 92 struct btrfs_root *root); 83 93 struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root, 84 94 int num_items); 85 - struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root, 86 - int num_blocks); 87 - struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root, 88 - int num_blocks); 89 - struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r, 90 - int num_blocks); 95 + struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root); 96 + struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root); 97 + struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root); 91 98 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid); 92 99 int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 93 100 struct btrfs_root *root);
+1 -1
fs/btrfs/volumes.c
··· 504 504 BUG_ON(!new_device); 505 505 memcpy(new_device, device, sizeof(*new_device)); 506 506 new_device->name = kstrdup(device->name, GFP_NOFS); 507 - BUG_ON(!new_device->name); 507 + BUG_ON(device->name && !new_device->name); 508 508 new_device->bdev = NULL; 509 509 new_device->writeable = 0; 510 510 new_device->in_fs_metadata = 0;
-2
fs/btrfs/xattr.c
··· 158 158 if (IS_ERR(trans)) 159 159 return PTR_ERR(trans); 160 160 161 - btrfs_set_trans_block_group(trans, inode); 162 - 163 161 ret = do_setxattr(trans, inode, name, value, size, flags); 164 162 if (ret) 165 163 goto out;