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

* 'for-linus-unmerged' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable: (45 commits)
Btrfs: fix __btrfs_map_block on 32 bit machines
btrfs: fix possible deadlock by clearing __GFP_FS flag
btrfs: check link counter overflow in link(2)
btrfs: don't mess with i_nlink of unlocked inode in rename()
Btrfs: check return value of btrfs_alloc_path()
Btrfs: fix OOPS of empty filesystem after balance
Btrfs: fix memory leak of empty filesystem after balance
Btrfs: fix return value of setflags ioctl
Btrfs: fix uncheck memory allocations
btrfs: make inode ref log recovery faster
Btrfs: add btrfs_trim_fs() to handle FITRIM
Btrfs: adjust btrfs_discard_extent() return errors and trimmed bytes
Btrfs: make btrfs_map_block() return entire free extent for each device of RAID0/1/10/DUP
Btrfs: make update_reserved_bytes() public
btrfs: return EXDEV when linking from different subvolumes
Btrfs: Per file/directory controls for COW and compression
Btrfs: add datacow flag in inode flag
btrfs: use GFP_NOFS instead of GFP_KERNEL
Btrfs: check return value of read_tree_block()
btrfs: properly access unaligned checksum buffer
...

Fix up trivial conflicts in fs/btrfs/volumes.c due to plug removal in
the block layer.

+2140 -815
+1 -2
fs/btrfs/btrfs_inode.h
··· 136 * items we think we'll end up using, and reserved_extents is the number 137 * of extent items we've reserved metadata for. 138 */ 139 - spinlock_t accounting_lock; 140 atomic_t outstanding_extents; 141 - int reserved_extents; 142 143 /* 144 * ordered_data_close is set by truncate when a file that used
··· 136 * items we think we'll end up using, and reserved_extents is the number 137 * of extent items we've reserved metadata for. 138 */ 139 atomic_t outstanding_extents; 140 + atomic_t reserved_extents; 141 142 /* 143 * ordered_data_close is set by truncate when a file that used
+13 -4
fs/btrfs/compression.c
··· 340 341 WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1)); 342 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS); 343 atomic_set(&cb->pending_bios, 0); 344 cb->errors = 0; 345 cb->inode = inode; ··· 356 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 357 358 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS); 359 bio->bi_private = cb; 360 bio->bi_end_io = end_compressed_bio_write; 361 atomic_inc(&cb->pending_bios); ··· 663 atomic_inc(&cb->pending_bios); 664 665 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 666 - btrfs_lookup_bio_sums(root, inode, comp_bio, 667 - sums); 668 } 669 sums += (comp_bio->bi_size + root->sectorsize - 1) / 670 root->sectorsize; ··· 690 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0); 691 BUG_ON(ret); 692 693 - if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) 694 - btrfs_lookup_bio_sums(root, inode, comp_bio, sums); 695 696 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0); 697 BUG_ON(ret);
··· 340 341 WARN_ON(start & ((u64)PAGE_CACHE_SIZE - 1)); 342 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS); 343 + if (!cb) 344 + return -ENOMEM; 345 atomic_set(&cb->pending_bios, 0); 346 cb->errors = 0; 347 cb->inode = inode; ··· 354 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 355 356 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS); 357 + if(!bio) { 358 + kfree(cb); 359 + return -ENOMEM; 360 + } 361 bio->bi_private = cb; 362 bio->bi_end_io = end_compressed_bio_write; 363 atomic_inc(&cb->pending_bios); ··· 657 atomic_inc(&cb->pending_bios); 658 659 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 660 + ret = btrfs_lookup_bio_sums(root, inode, 661 + comp_bio, sums); 662 + BUG_ON(ret); 663 } 664 sums += (comp_bio->bi_size + root->sectorsize - 1) / 665 root->sectorsize; ··· 683 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 0); 684 BUG_ON(ret); 685 686 + if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 687 + ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums); 688 + BUG_ON(ret); 689 + } 690 691 ret = btrfs_map_bio(root, READ, comp_bio, mirror_num, 0); 692 BUG_ON(ret);
+16 -143
fs/btrfs/ctree.c
··· 147 struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 148 { 149 struct extent_buffer *eb; 150 - spin_lock(&root->node_lock); 151 - eb = root->node; 152 extent_buffer_get(eb); 153 - spin_unlock(&root->node_lock); 154 return eb; 155 } 156 ··· 166 while (1) { 167 eb = btrfs_root_node(root); 168 btrfs_tree_lock(eb); 169 - 170 - spin_lock(&root->node_lock); 171 - if (eb == root->node) { 172 - spin_unlock(&root->node_lock); 173 break; 174 - } 175 - spin_unlock(&root->node_lock); 176 - 177 btrfs_tree_unlock(eb); 178 free_extent_buffer(eb); 179 } ··· 453 else 454 parent_start = 0; 455 456 - spin_lock(&root->node_lock); 457 - root->node = cow; 458 extent_buffer_get(cow); 459 - spin_unlock(&root->node_lock); 460 461 btrfs_free_tree_block(trans, root, buf, parent_start, 462 last_ref); ··· 535 536 ret = __btrfs_cow_block(trans, root, buf, parent, 537 parent_slot, cow_ret, search_start, 0); 538 return ret; 539 } 540 ··· 682 if (!cur) { 683 cur = read_tree_block(root, blocknr, 684 blocksize, gen); 685 } else if (!uptodate) { 686 btrfs_read_buffer(cur, gen); 687 } ··· 730 return btrfs_item_offset_nr(leaf, nr - 1); 731 } 732 733 - /* 734 - * extra debugging checks to make sure all the items in a key are 735 - * well formed and in the proper order 736 - */ 737 - static int check_node(struct btrfs_root *root, struct btrfs_path *path, 738 - int level) 739 - { 740 - struct extent_buffer *parent = NULL; 741 - struct extent_buffer *node = path->nodes[level]; 742 - struct btrfs_disk_key parent_key; 743 - struct btrfs_disk_key node_key; 744 - int parent_slot; 745 - int slot; 746 - struct btrfs_key cpukey; 747 - u32 nritems = btrfs_header_nritems(node); 748 - 749 - if (path->nodes[level + 1]) 750 - parent = path->nodes[level + 1]; 751 - 752 - slot = path->slots[level]; 753 - BUG_ON(nritems == 0); 754 - if (parent) { 755 - parent_slot = path->slots[level + 1]; 756 - btrfs_node_key(parent, &parent_key, parent_slot); 757 - btrfs_node_key(node, &node_key, 0); 758 - BUG_ON(memcmp(&parent_key, &node_key, 759 - sizeof(struct btrfs_disk_key))); 760 - BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 761 - btrfs_header_bytenr(node)); 762 - } 763 - BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root)); 764 - if (slot != 0) { 765 - btrfs_node_key_to_cpu(node, &cpukey, slot - 1); 766 - btrfs_node_key(node, &node_key, slot); 767 - BUG_ON(comp_keys(&node_key, &cpukey) <= 0); 768 - } 769 - if (slot < nritems - 1) { 770 - btrfs_node_key_to_cpu(node, &cpukey, slot + 1); 771 - btrfs_node_key(node, &node_key, slot); 772 - BUG_ON(comp_keys(&node_key, &cpukey) >= 0); 773 - } 774 - return 0; 775 - } 776 - 777 - /* 778 - * extra checking to make sure all the items in a leaf are 779 - * well formed and in the proper order 780 - */ 781 - static int check_leaf(struct btrfs_root *root, struct btrfs_path *path, 782 - int level) 783 - { 784 - struct extent_buffer *leaf = path->nodes[level]; 785 - struct extent_buffer *parent = NULL; 786 - int parent_slot; 787 - struct btrfs_key cpukey; 788 - struct btrfs_disk_key parent_key; 789 - struct btrfs_disk_key leaf_key; 790 - int slot = path->slots[0]; 791 - 792 - u32 nritems = btrfs_header_nritems(leaf); 793 - 794 - if (path->nodes[level + 1]) 795 - parent = path->nodes[level + 1]; 796 - 797 - if (nritems == 0) 798 - return 0; 799 - 800 - if (parent) { 801 - parent_slot = path->slots[level + 1]; 802 - btrfs_node_key(parent, &parent_key, parent_slot); 803 - btrfs_item_key(leaf, &leaf_key, 0); 804 - 805 - BUG_ON(memcmp(&parent_key, &leaf_key, 806 - sizeof(struct btrfs_disk_key))); 807 - BUG_ON(btrfs_node_blockptr(parent, parent_slot) != 808 - btrfs_header_bytenr(leaf)); 809 - } 810 - if (slot != 0 && slot < nritems - 1) { 811 - btrfs_item_key(leaf, &leaf_key, slot); 812 - btrfs_item_key_to_cpu(leaf, &cpukey, slot - 1); 813 - if (comp_keys(&leaf_key, &cpukey) <= 0) { 814 - btrfs_print_leaf(root, leaf); 815 - printk(KERN_CRIT "slot %d offset bad key\n", slot); 816 - BUG_ON(1); 817 - } 818 - if (btrfs_item_offset_nr(leaf, slot - 1) != 819 - btrfs_item_end_nr(leaf, slot)) { 820 - btrfs_print_leaf(root, leaf); 821 - printk(KERN_CRIT "slot %d offset bad\n", slot); 822 - BUG_ON(1); 823 - } 824 - } 825 - if (slot < nritems - 1) { 826 - btrfs_item_key(leaf, &leaf_key, slot); 827 - btrfs_item_key_to_cpu(leaf, &cpukey, slot + 1); 828 - BUG_ON(comp_keys(&leaf_key, &cpukey) >= 0); 829 - if (btrfs_item_offset_nr(leaf, slot) != 830 - btrfs_item_end_nr(leaf, slot + 1)) { 831 - btrfs_print_leaf(root, leaf); 832 - printk(KERN_CRIT "slot %d offset bad\n", slot); 833 - BUG_ON(1); 834 - } 835 - } 836 - BUG_ON(btrfs_item_offset_nr(leaf, 0) + 837 - btrfs_item_size_nr(leaf, 0) != BTRFS_LEAF_DATA_SIZE(root)); 838 - return 0; 839 - } 840 - 841 - static noinline int check_block(struct btrfs_root *root, 842 - struct btrfs_path *path, int level) 843 - { 844 - return 0; 845 - if (level == 0) 846 - return check_leaf(root, path, level); 847 - return check_node(root, path, level); 848 - } 849 850 /* 851 * search for key in the extent_buffer. The items start at offset p, ··· 928 goto enospc; 929 } 930 931 - spin_lock(&root->node_lock); 932 - root->node = child; 933 - spin_unlock(&root->node_lock); 934 935 add_root_to_dirty_list(root); 936 btrfs_tree_unlock(child); ··· 1068 } 1069 } 1070 /* double check we haven't messed things up */ 1071 - check_block(root, path, level); 1072 if (orig_ptr != 1073 btrfs_node_blockptr(path->nodes[level], path->slots[level])) 1074 BUG(); ··· 1677 if (!cow) 1678 btrfs_unlock_up_safe(p, level + 1); 1679 1680 - ret = check_block(root, p, level); 1681 - if (ret) { 1682 - ret = -1; 1683 - goto done; 1684 - } 1685 - 1686 ret = bin_search(b, key, level, &slot); 1687 1688 if (level != 0) { ··· 2003 2004 btrfs_mark_buffer_dirty(c); 2005 2006 - spin_lock(&root->node_lock); 2007 old = root->node; 2008 - root->node = c; 2009 - spin_unlock(&root->node_lock); 2010 2011 /* the super has an extra ref to root->node */ 2012 free_extent_buffer(old); ··· 3711 unsigned long ptr; 3712 3713 path = btrfs_alloc_path(); 3714 - BUG_ON(!path); 3715 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 3716 if (!ret) { 3717 leaf = path->nodes[0]; ··· 4089 } 4090 btrfs_set_path_blocking(path); 4091 cur = read_node_slot(root, cur, slot); 4092 4093 btrfs_tree_lock(cur); 4094
··· 147 struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 148 { 149 struct extent_buffer *eb; 150 + 151 + rcu_read_lock(); 152 + eb = rcu_dereference(root->node); 153 extent_buffer_get(eb); 154 + rcu_read_unlock(); 155 return eb; 156 } 157 ··· 165 while (1) { 166 eb = btrfs_root_node(root); 167 btrfs_tree_lock(eb); 168 + if (eb == root->node) 169 break; 170 btrfs_tree_unlock(eb); 171 free_extent_buffer(eb); 172 } ··· 458 else 459 parent_start = 0; 460 461 extent_buffer_get(cow); 462 + rcu_assign_pointer(root->node, cow); 463 464 btrfs_free_tree_block(trans, root, buf, parent_start, 465 last_ref); ··· 542 543 ret = __btrfs_cow_block(trans, root, buf, parent, 544 parent_slot, cow_ret, search_start, 0); 545 + 546 + trace_btrfs_cow_block(root, buf, *cow_ret); 547 + 548 return ret; 549 } 550 ··· 686 if (!cur) { 687 cur = read_tree_block(root, blocknr, 688 blocksize, gen); 689 + if (!cur) 690 + return -EIO; 691 } else if (!uptodate) { 692 btrfs_read_buffer(cur, gen); 693 } ··· 732 return btrfs_item_offset_nr(leaf, nr - 1); 733 } 734 735 736 /* 737 * search for key in the extent_buffer. The items start at offset p, ··· 1046 goto enospc; 1047 } 1048 1049 + rcu_assign_pointer(root->node, child); 1050 1051 add_root_to_dirty_list(root); 1052 btrfs_tree_unlock(child); ··· 1188 } 1189 } 1190 /* double check we haven't messed things up */ 1191 if (orig_ptr != 1192 btrfs_node_blockptr(path->nodes[level], path->slots[level])) 1193 BUG(); ··· 1798 if (!cow) 1799 btrfs_unlock_up_safe(p, level + 1); 1800 1801 ret = bin_search(b, key, level, &slot); 1802 1803 if (level != 0) { ··· 2130 2131 btrfs_mark_buffer_dirty(c); 2132 2133 old = root->node; 2134 + rcu_assign_pointer(root->node, c); 2135 2136 /* the super has an extra ref to root->node */ 2137 free_extent_buffer(old); ··· 3840 unsigned long ptr; 3841 3842 path = btrfs_alloc_path(); 3843 + if (!path) 3844 + return -ENOMEM; 3845 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 3846 if (!ret) { 3847 leaf = path->nodes[0]; ··· 4217 } 4218 btrfs_set_path_blocking(path); 4219 cur = read_node_slot(root, cur, slot); 4220 + BUG_ON(!cur); 4221 4222 btrfs_tree_lock(cur); 4223
+13 -6
fs/btrfs/ctree.h
··· 28 #include <linux/wait.h> 29 #include <linux/slab.h> 30 #include <linux/kobject.h> 31 #include <asm/kmap_types.h> 32 #include "extent_io.h" 33 #include "extent_map.h" ··· 41 extern struct kmem_cache *btrfs_transaction_cachep; 42 extern struct kmem_cache *btrfs_bit_radix_cachep; 43 extern struct kmem_cache *btrfs_path_cachep; 44 struct btrfs_ordered_sum; 45 46 #define BTRFS_MAGIC "_BHRfS_M" ··· 784 /* first extent starting offset */ 785 u64 window_start; 786 787 - /* if this cluster simply points at a bitmap in the block group */ 788 - bool points_to_bitmap; 789 - 790 struct btrfs_block_group_cache *block_group; 791 /* 792 * when a cluster is allocated from a block group, we put the ··· 1282 #define BTRFS_INODE_NODUMP (1 << 8) 1283 #define BTRFS_INODE_NOATIME (1 << 9) 1284 #define BTRFS_INODE_DIRSYNC (1 << 10) 1285 1286 /* some macros to generate set/get funcs for the struct fields. This 1287 * assumes there is a lefoo_to_cpu for every type, so lets make a simple ··· 2157 u64 root_objectid, u64 owner, u64 offset); 2158 2159 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len); 2160 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 2161 struct btrfs_root *root); 2162 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, ··· 2229 int btrfs_error_unpin_extent_range(struct btrfs_root *root, 2230 u64 start, u64 end); 2231 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, 2232 - u64 num_bytes); 2233 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, 2234 struct btrfs_root *root, u64 type); 2235 2236 /* ctree.c */ 2237 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, 2238 int level, int *slot); ··· 2396 struct btrfs_path *path, u64 dir, 2397 const char *name, u16 name_len, 2398 int mod); 2399 2400 /* orphan.c */ 2401 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans, ··· 2535 struct inode *inode); 2536 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode); 2537 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode); 2538 - void btrfs_orphan_cleanup(struct btrfs_root *root); 2539 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, 2540 struct btrfs_pending_snapshot *pending, 2541 u64 *bytes_to_reserve); ··· 2543 struct btrfs_pending_snapshot *pending); 2544 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, 2545 struct btrfs_root *root); 2546 - int btrfs_cont_expand(struct inode *inode, loff_t size); 2547 int btrfs_invalidate_inodes(struct btrfs_root *root); 2548 void btrfs_add_delayed_iput(struct inode *inode); 2549 void btrfs_run_delayed_iputs(struct btrfs_root *root);
··· 28 #include <linux/wait.h> 29 #include <linux/slab.h> 30 #include <linux/kobject.h> 31 + #include <trace/events/btrfs.h> 32 #include <asm/kmap_types.h> 33 #include "extent_io.h" 34 #include "extent_map.h" ··· 40 extern struct kmem_cache *btrfs_transaction_cachep; 41 extern struct kmem_cache *btrfs_bit_radix_cachep; 42 extern struct kmem_cache *btrfs_path_cachep; 43 + extern struct kmem_cache *btrfs_free_space_cachep; 44 struct btrfs_ordered_sum; 45 46 #define BTRFS_MAGIC "_BHRfS_M" ··· 782 /* first extent starting offset */ 783 u64 window_start; 784 785 struct btrfs_block_group_cache *block_group; 786 /* 787 * when a cluster is allocated from a block group, we put the ··· 1283 #define BTRFS_INODE_NODUMP (1 << 8) 1284 #define BTRFS_INODE_NOATIME (1 << 9) 1285 #define BTRFS_INODE_DIRSYNC (1 << 10) 1286 + #define BTRFS_INODE_COMPRESS (1 << 11) 1287 1288 /* some macros to generate set/get funcs for the struct fields. This 1289 * assumes there is a lefoo_to_cpu for every type, so lets make a simple ··· 2157 u64 root_objectid, u64 owner, u64 offset); 2158 2159 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len); 2160 + int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 2161 + u64 num_bytes, int reserve, int sinfo); 2162 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 2163 struct btrfs_root *root); 2164 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, ··· 2227 int btrfs_error_unpin_extent_range(struct btrfs_root *root, 2228 u64 start, u64 end); 2229 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, 2230 + u64 num_bytes, u64 *actual_bytes); 2231 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, 2232 struct btrfs_root *root, u64 type); 2233 + int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range); 2234 2235 + int btrfs_init_space_info(struct btrfs_fs_info *fs_info); 2236 /* ctree.c */ 2237 int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key, 2238 int level, int *slot); ··· 2392 struct btrfs_path *path, u64 dir, 2393 const char *name, u16 name_len, 2394 int mod); 2395 + int verify_dir_item(struct btrfs_root *root, 2396 + struct extent_buffer *leaf, 2397 + struct btrfs_dir_item *dir_item); 2398 2399 /* orphan.c */ 2400 int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans, ··· 2528 struct inode *inode); 2529 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode); 2530 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode); 2531 + int btrfs_orphan_cleanup(struct btrfs_root *root); 2532 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans, 2533 struct btrfs_pending_snapshot *pending, 2534 u64 *bytes_to_reserve); ··· 2536 struct btrfs_pending_snapshot *pending); 2537 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, 2538 struct btrfs_root *root); 2539 + int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size); 2540 int btrfs_invalidate_inodes(struct btrfs_root *root); 2541 void btrfs_add_delayed_iput(struct inode *inode); 2542 void btrfs_run_delayed_iputs(struct btrfs_root *root);
+6
fs/btrfs/delayed-ref.c
··· 483 INIT_LIST_HEAD(&head_ref->cluster); 484 mutex_init(&head_ref->mutex); 485 486 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 487 488 if (existing) { ··· 539 } 540 full_ref->level = level; 541 542 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 543 544 if (existing) { ··· 594 } 595 full_ref->objectid = owner; 596 full_ref->offset = offset; 597 598 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 599
··· 483 INIT_LIST_HEAD(&head_ref->cluster); 484 mutex_init(&head_ref->mutex); 485 486 + trace_btrfs_delayed_ref_head(ref, head_ref, action); 487 + 488 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 489 490 if (existing) { ··· 537 } 538 full_ref->level = level; 539 540 + trace_btrfs_delayed_tree_ref(ref, full_ref, action); 541 + 542 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 543 544 if (existing) { ··· 590 } 591 full_ref->objectid = owner; 592 full_ref->offset = offset; 593 + 594 + trace_btrfs_delayed_data_ref(ref, full_ref, action); 595 596 existing = tree_insert(&delayed_refs->root, &ref->rb_node); 597
+41 -4
fs/btrfs/dir-item.c
··· 151 ret = PTR_ERR(dir_item); 152 if (ret == -EEXIST) 153 goto second_insert; 154 - goto out; 155 } 156 157 leaf = path->nodes[0]; ··· 170 /* FIXME, use some real flag for selecting the extra index */ 171 if (root == root->fs_info->tree_root) { 172 ret = 0; 173 - goto out; 174 } 175 btrfs_release_path(root, path); 176 ··· 180 name, name_len); 181 if (IS_ERR(dir_item)) { 182 ret2 = PTR_ERR(dir_item); 183 - goto out; 184 } 185 leaf = path->nodes[0]; 186 btrfs_cpu_key_to_disk(&disk_key, location); ··· 192 name_ptr = (unsigned long)(dir_item + 1); 193 write_extent_buffer(leaf, name, name_ptr, name_len); 194 btrfs_mark_buffer_dirty(leaf); 195 - out: 196 btrfs_free_path(path); 197 if (ret) 198 return ret; ··· 379 380 leaf = path->nodes[0]; 381 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); 382 total_len = btrfs_item_size_nr(leaf, path->slots[0]); 383 while (cur < total_len) { 384 this_len = sizeof(*dir_item) + ··· 433 item_len - sub_item_len, 1); 434 } 435 return ret; 436 }
··· 151 ret = PTR_ERR(dir_item); 152 if (ret == -EEXIST) 153 goto second_insert; 154 + goto out_free; 155 } 156 157 leaf = path->nodes[0]; ··· 170 /* FIXME, use some real flag for selecting the extra index */ 171 if (root == root->fs_info->tree_root) { 172 ret = 0; 173 + goto out_free; 174 } 175 btrfs_release_path(root, path); 176 ··· 180 name, name_len); 181 if (IS_ERR(dir_item)) { 182 ret2 = PTR_ERR(dir_item); 183 + goto out_free; 184 } 185 leaf = path->nodes[0]; 186 btrfs_cpu_key_to_disk(&disk_key, location); ··· 192 name_ptr = (unsigned long)(dir_item + 1); 193 write_extent_buffer(leaf, name, name_ptr, name_len); 194 btrfs_mark_buffer_dirty(leaf); 195 + 196 + out_free: 197 + 198 btrfs_free_path(path); 199 if (ret) 200 return ret; ··· 377 378 leaf = path->nodes[0]; 379 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); 380 + if (verify_dir_item(root, leaf, dir_item)) 381 + return NULL; 382 + 383 total_len = btrfs_item_size_nr(leaf, path->slots[0]); 384 while (cur < total_len) { 385 this_len = sizeof(*dir_item) + ··· 428 item_len - sub_item_len, 1); 429 } 430 return ret; 431 + } 432 + 433 + int verify_dir_item(struct btrfs_root *root, 434 + struct extent_buffer *leaf, 435 + struct btrfs_dir_item *dir_item) 436 + { 437 + u16 namelen = BTRFS_NAME_LEN; 438 + u8 type = btrfs_dir_type(leaf, dir_item); 439 + 440 + if (type >= BTRFS_FT_MAX) { 441 + printk(KERN_CRIT "btrfs: invalid dir item type: %d\n", 442 + (int)type); 443 + return 1; 444 + } 445 + 446 + if (type == BTRFS_FT_XATTR) 447 + namelen = XATTR_NAME_MAX; 448 + 449 + if (btrfs_dir_name_len(leaf, dir_item) > namelen) { 450 + printk(KERN_CRIT "btrfS: invalid dir item name len: %u\n", 451 + (unsigned)btrfs_dir_data_len(leaf, dir_item)); 452 + return 1; 453 + } 454 + 455 + /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */ 456 + if (btrfs_dir_data_len(leaf, dir_item) > BTRFS_MAX_XATTR_SIZE(root)) { 457 + printk(KERN_CRIT "btrfs: invalid dir item data len: %u\n", 458 + (unsigned)btrfs_dir_data_len(leaf, dir_item)); 459 + return 1; 460 + } 461 + 462 + return 0; 463 }
+125 -7
fs/btrfs/disk-io.c
··· 29 #include <linux/crc32c.h> 30 #include <linux/slab.h> 31 #include <linux/migrate.h> 32 #include "compat.h" 33 #include "ctree.h" 34 #include "disk-io.h" ··· 199 200 void btrfs_csum_final(u32 crc, char *result) 201 { 202 - *(__le32 *)result = ~cpu_to_le32(crc); 203 } 204 205 /* ··· 324 int num_copies = 0; 325 int mirror_num = 0; 326 327 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 328 while (1) { 329 ret = read_extent_buffer_pages(io_tree, eb, start, 1, 330 btree_get_extent, mirror_num); 331 if (!ret && 332 !verify_parent_transid(io_tree, eb, parent_transid)) 333 return ret; 334 335 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, ··· 429 return ret; 430 } 431 432 #ifdef CONFIG_DEBUG_LOCK_ALLOC 433 void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level) 434 { ··· 562 btrfs_set_buffer_lockdep_class(eb, found_level); 563 564 ret = csum_tree_block(root, eb, 1); 565 - if (ret) 566 ret = -EIO; 567 568 end = min_t(u64, eb->len, PAGE_CACHE_SIZE); 569 end = eb->start + end - 1; ··· 1248 root, fs_info, location->objectid); 1249 1250 path = btrfs_alloc_path(); 1251 - BUG_ON(!path); 1252 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 1253 if (ret == 0) { 1254 l = path->nodes[0]; ··· 1645 goto fail_bdi; 1646 } 1647 1648 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 1649 INIT_LIST_HEAD(&fs_info->trans_list); 1650 INIT_LIST_HEAD(&fs_info->dead_roots); ··· 1776 fs_info->fs_state |= btrfs_super_flags(disk_super); 1777 1778 btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 1779 1780 ret = btrfs_parse_options(tree_root, options); 1781 if (ret) { ··· 1988 fs_info->metadata_alloc_profile = (u64)-1; 1989 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1990 1991 ret = btrfs_read_block_groups(extent_root); 1992 if (ret) { 1993 printk(KERN_ERR "Failed to read block groups: %d\n", ret); ··· 2085 2086 if (!(sb->s_flags & MS_RDONLY)) { 2087 down_read(&fs_info->cleanup_work_sem); 2088 - btrfs_orphan_cleanup(fs_info->fs_root); 2089 - btrfs_orphan_cleanup(fs_info->tree_root); 2090 up_read(&fs_info->cleanup_work_sem); 2091 } 2092 2093 return tree_root; ··· 2467 2468 root_objectid = gang[ret - 1]->root_key.objectid + 1; 2469 for (i = 0; i < ret; i++) { 2470 root_objectid = gang[i]->root_key.objectid; 2471 - btrfs_orphan_cleanup(gang[i]); 2472 } 2473 root_objectid++; 2474 } ··· 2983 break; 2984 2985 /* opt_discard */ 2986 - ret = btrfs_error_discard_extent(root, start, end + 1 - start); 2987 2988 clear_extent_dirty(unpin, start, end, GFP_NOFS); 2989 btrfs_error_unpin_extent_range(root, start, end);
··· 29 #include <linux/crc32c.h> 30 #include <linux/slab.h> 31 #include <linux/migrate.h> 32 + #include <asm/unaligned.h> 33 #include "compat.h" 34 #include "ctree.h" 35 #include "disk-io.h" ··· 198 199 void btrfs_csum_final(u32 crc, char *result) 200 { 201 + put_unaligned_le32(~crc, result); 202 } 203 204 /* ··· 323 int num_copies = 0; 324 int mirror_num = 0; 325 326 + clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 327 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 328 while (1) { 329 ret = read_extent_buffer_pages(io_tree, eb, start, 1, 330 btree_get_extent, mirror_num); 331 if (!ret && 332 !verify_parent_transid(io_tree, eb, parent_transid)) 333 + return ret; 334 + 335 + /* 336 + * This buffer's crc is fine, but its contents are corrupted, so 337 + * there is no reason to read the other copies, they won't be 338 + * any less wrong. 339 + */ 340 + if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 341 return ret; 342 343 num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, ··· 419 return ret; 420 } 421 422 + #define CORRUPT(reason, eb, root, slot) \ 423 + printk(KERN_CRIT "btrfs: corrupt leaf, %s: block=%llu," \ 424 + "root=%llu, slot=%d\n", reason, \ 425 + (unsigned long long)btrfs_header_bytenr(eb), \ 426 + (unsigned long long)root->objectid, slot) 427 + 428 + static noinline int check_leaf(struct btrfs_root *root, 429 + struct extent_buffer *leaf) 430 + { 431 + struct btrfs_key key; 432 + struct btrfs_key leaf_key; 433 + u32 nritems = btrfs_header_nritems(leaf); 434 + int slot; 435 + 436 + if (nritems == 0) 437 + return 0; 438 + 439 + /* Check the 0 item */ 440 + if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 441 + BTRFS_LEAF_DATA_SIZE(root)) { 442 + CORRUPT("invalid item offset size pair", leaf, root, 0); 443 + return -EIO; 444 + } 445 + 446 + /* 447 + * Check to make sure each items keys are in the correct order and their 448 + * offsets make sense. We only have to loop through nritems-1 because 449 + * we check the current slot against the next slot, which verifies the 450 + * next slot's offset+size makes sense and that the current's slot 451 + * offset is correct. 452 + */ 453 + for (slot = 0; slot < nritems - 1; slot++) { 454 + btrfs_item_key_to_cpu(leaf, &leaf_key, slot); 455 + btrfs_item_key_to_cpu(leaf, &key, slot + 1); 456 + 457 + /* Make sure the keys are in the right order */ 458 + if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { 459 + CORRUPT("bad key order", leaf, root, slot); 460 + return -EIO; 461 + } 462 + 463 + /* 464 + * Make sure the offset and ends are right, remember that the 465 + * item data starts at the end of the leaf and grows towards the 466 + * front. 467 + */ 468 + if (btrfs_item_offset_nr(leaf, slot) != 469 + btrfs_item_end_nr(leaf, slot + 1)) { 470 + CORRUPT("slot offset bad", leaf, root, slot); 471 + return -EIO; 472 + } 473 + 474 + /* 475 + * Check to make sure that we don't point outside of the leaf, 476 + * just incase all the items are consistent to eachother, but 477 + * all point outside of the leaf. 478 + */ 479 + if (btrfs_item_end_nr(leaf, slot) > 480 + BTRFS_LEAF_DATA_SIZE(root)) { 481 + CORRUPT("slot end outside of leaf", leaf, root, slot); 482 + return -EIO; 483 + } 484 + } 485 + 486 + return 0; 487 + } 488 + 489 #ifdef CONFIG_DEBUG_LOCK_ALLOC 490 void btrfs_set_buffer_lockdep_class(struct extent_buffer *eb, int level) 491 { ··· 485 btrfs_set_buffer_lockdep_class(eb, found_level); 486 487 ret = csum_tree_block(root, eb, 1); 488 + if (ret) { 489 ret = -EIO; 490 + goto err; 491 + } 492 + 493 + /* 494 + * If this is a leaf block and it is corrupt, set the corrupt bit so 495 + * that we don't try and read the other copies of this block, just 496 + * return -EIO. 497 + */ 498 + if (found_level == 0 && check_leaf(root, eb)) { 499 + set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 500 + ret = -EIO; 501 + } 502 503 end = min_t(u64, eb->len, PAGE_CACHE_SIZE); 504 end = eb->start + end - 1; ··· 1159 root, fs_info, location->objectid); 1160 1161 path = btrfs_alloc_path(); 1162 + if (!path) { 1163 + kfree(root); 1164 + return ERR_PTR(-ENOMEM); 1165 + } 1166 ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0); 1167 if (ret == 0) { 1168 l = path->nodes[0]; ··· 1553 goto fail_bdi; 1554 } 1555 1556 + fs_info->btree_inode->i_mapping->flags &= ~__GFP_FS; 1557 + 1558 INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC); 1559 INIT_LIST_HEAD(&fs_info->trans_list); 1560 INIT_LIST_HEAD(&fs_info->dead_roots); ··· 1682 fs_info->fs_state |= btrfs_super_flags(disk_super); 1683 1684 btrfs_check_super_valid(fs_info, sb->s_flags & MS_RDONLY); 1685 + 1686 + /* 1687 + * In the long term, we'll store the compression type in the super 1688 + * block, and it'll be used for per file compression control. 1689 + */ 1690 + fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 1691 1692 ret = btrfs_parse_options(tree_root, options); 1693 if (ret) { ··· 1888 fs_info->metadata_alloc_profile = (u64)-1; 1889 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1890 1891 + ret = btrfs_init_space_info(fs_info); 1892 + if (ret) { 1893 + printk(KERN_ERR "Failed to initial space info: %d\n", ret); 1894 + goto fail_block_groups; 1895 + } 1896 + 1897 ret = btrfs_read_block_groups(extent_root); 1898 if (ret) { 1899 printk(KERN_ERR "Failed to read block groups: %d\n", ret); ··· 1979 1980 if (!(sb->s_flags & MS_RDONLY)) { 1981 down_read(&fs_info->cleanup_work_sem); 1982 + err = btrfs_orphan_cleanup(fs_info->fs_root); 1983 + if (!err) 1984 + err = btrfs_orphan_cleanup(fs_info->tree_root); 1985 up_read(&fs_info->cleanup_work_sem); 1986 + if (err) { 1987 + close_ctree(tree_root); 1988 + return ERR_PTR(err); 1989 + } 1990 } 1991 1992 return tree_root; ··· 2356 2357 root_objectid = gang[ret - 1]->root_key.objectid + 1; 2358 for (i = 0; i < ret; i++) { 2359 + int err; 2360 + 2361 root_objectid = gang[i]->root_key.objectid; 2362 + err = btrfs_orphan_cleanup(gang[i]); 2363 + if (err) 2364 + return err; 2365 } 2366 root_objectid++; 2367 } ··· 2868 break; 2869 2870 /* opt_discard */ 2871 + if (btrfs_test_opt(root, DISCARD)) 2872 + ret = btrfs_error_discard_extent(root, start, 2873 + end + 1 - start, 2874 + NULL); 2875 2876 clear_extent_dirty(unpin, start, end, GFP_NOFS); 2877 btrfs_error_unpin_extent_range(root, start, end);
+177 -52
fs/btrfs/extent-tree.c
··· 36 static int update_block_group(struct btrfs_trans_handle *trans, 37 struct btrfs_root *root, 38 u64 bytenr, u64 num_bytes, int alloc); 39 - static int update_reserved_bytes(struct btrfs_block_group_cache *cache, 40 - u64 num_bytes, int reserve, int sinfo); 41 static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 42 struct btrfs_root *root, 43 u64 bytenr, u64 num_bytes, u64 parent, ··· 440 * allocate blocks for the tree root we can't do the fast caching since 441 * we likely hold important locks. 442 */ 443 - if (!trans->transaction->in_commit && 444 (root && root != root->fs_info->tree_root)) { 445 spin_lock(&cache->lock); 446 if (cache->cached != BTRFS_CACHE_NO) { ··· 469 if (load_cache_only) 470 return 0; 471 472 - caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); 473 BUG_ON(!caching_ctl); 474 475 INIT_LIST_HEAD(&caching_ctl->list); ··· 1738 return ret; 1739 } 1740 1741 - static void btrfs_issue_discard(struct block_device *bdev, 1742 u64 start, u64 len) 1743 { 1744 - blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, 0); 1745 } 1746 1747 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, 1748 - u64 num_bytes) 1749 { 1750 int ret; 1751 - u64 map_length = num_bytes; 1752 struct btrfs_multi_bio *multi = NULL; 1753 1754 - if (!btrfs_test_opt(root, DISCARD)) 1755 - return 0; 1756 1757 /* Tell the block device(s) that the sectors can be discarded */ 1758 - ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, 1759 - bytenr, &map_length, &multi, 0); 1760 if (!ret) { 1761 struct btrfs_bio_stripe *stripe = multi->stripes; 1762 int i; 1763 1764 - if (map_length > num_bytes) 1765 - map_length = num_bytes; 1766 1767 for (i = 0; i < multi->num_stripes; i++, stripe++) { 1768 - btrfs_issue_discard(stripe->dev->bdev, 1769 - stripe->physical, 1770 - map_length); 1771 } 1772 kfree(multi); 1773 } 1774 1775 return ret; 1776 } ··· 4000 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; 4001 u64 to_reserve; 4002 int nr_extents; 4003 int ret; 4004 4005 if (btrfs_transaction_in_commit(root->fs_info)) ··· 4008 4009 num_bytes = ALIGN(num_bytes, root->sectorsize); 4010 4011 - spin_lock(&BTRFS_I(inode)->accounting_lock); 4012 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; 4013 - if (nr_extents > BTRFS_I(inode)->reserved_extents) { 4014 - nr_extents -= BTRFS_I(inode)->reserved_extents; 4015 to_reserve = calc_trans_metadata_size(root, nr_extents); 4016 } else { 4017 nr_extents = 0; 4018 to_reserve = 0; 4019 } 4020 - spin_unlock(&BTRFS_I(inode)->accounting_lock); 4021 to_reserve += calc_csum_metadata_size(inode, num_bytes); 4022 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); 4023 if (ret) 4024 return ret; 4025 4026 - spin_lock(&BTRFS_I(inode)->accounting_lock); 4027 - BTRFS_I(inode)->reserved_extents += nr_extents; 4028 atomic_inc(&BTRFS_I(inode)->outstanding_extents); 4029 - spin_unlock(&BTRFS_I(inode)->accounting_lock); 4030 4031 block_rsv_add_bytes(block_rsv, to_reserve, 1); 4032 ··· 4040 struct btrfs_root *root = BTRFS_I(inode)->root; 4041 u64 to_free; 4042 int nr_extents; 4043 4044 num_bytes = ALIGN(num_bytes, root->sectorsize); 4045 atomic_dec(&BTRFS_I(inode)->outstanding_extents); 4046 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0); 4047 4048 - spin_lock(&BTRFS_I(inode)->accounting_lock); 4049 - nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); 4050 - if (nr_extents < BTRFS_I(inode)->reserved_extents) { 4051 - nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents; 4052 - BTRFS_I(inode)->reserved_extents -= nr_extents; 4053 - } else { 4054 - nr_extents = 0; 4055 - } 4056 - spin_unlock(&BTRFS_I(inode)->accounting_lock); 4057 4058 to_free = calc_csum_metadata_size(inode, num_bytes); 4059 if (nr_extents > 0) ··· 4237 * update size of reserved extents. this function may return -EAGAIN 4238 * if 'reserve' is true or 'sinfo' is false. 4239 */ 4240 - static int update_reserved_bytes(struct btrfs_block_group_cache *cache, 4241 - u64 num_bytes, int reserve, int sinfo) 4242 { 4243 int ret = 0; 4244 if (sinfo) { ··· 4377 if (ret) 4378 break; 4379 4380 - ret = btrfs_discard_extent(root, start, end + 1 - start); 4381 4382 clear_extent_dirty(unpin, start, end, GFP_NOFS); 4383 unpin_extent_range(root, start, end); ··· 4720 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 4721 4722 btrfs_add_free_space(cache, buf->start, buf->len); 4723 - ret = update_reserved_bytes(cache, buf->len, 0, 0); 4724 if (ret == -EAGAIN) { 4725 /* block group became read-only */ 4726 - update_reserved_bytes(cache, buf->len, 0, 1); 4727 goto out; 4728 } 4729 ··· 4760 } 4761 } 4762 out: 4763 btrfs_put_block_group(cache); 4764 } 4765 ··· 5212 search_start - offset); 5213 BUG_ON(offset > search_start); 5214 5215 - ret = update_reserved_bytes(block_group, num_bytes, 1, 5216 (data & BTRFS_BLOCK_GROUP_DATA)); 5217 if (ret == -EAGAIN) { 5218 btrfs_add_free_space(block_group, offset, num_bytes); ··· 5418 dump_space_info(sinfo, num_bytes, 1); 5419 } 5420 5421 return ret; 5422 } 5423 ··· 5435 return -ENOSPC; 5436 } 5437 5438 - ret = btrfs_discard_extent(root, start, len); 5439 5440 btrfs_add_free_space(cache, start, len); 5441 - update_reserved_bytes(cache, len, 0, 1); 5442 btrfs_put_block_group(cache); 5443 5444 return ret; 5445 } ··· 5470 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); 5471 5472 path = btrfs_alloc_path(); 5473 - BUG_ON(!path); 5474 5475 path->leave_spinning = 1; 5476 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, ··· 5641 put_caching_control(caching_ctl); 5642 } 5643 5644 - ret = update_reserved_bytes(block_group, ins->offset, 1, 1); 5645 BUG_ON(ret); 5646 btrfs_put_block_group(block_group); 5647 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, ··· 6074 if (reada && level == 1) 6075 reada_walk_down(trans, root, wc, path); 6076 next = read_tree_block(root, bytenr, blocksize, generation); 6077 btrfs_tree_lock(next); 6078 btrfs_set_lock_blocking(next); 6079 } ··· 6467 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); 6468 6469 path = btrfs_alloc_path(); 6470 - BUG_ON(!path); 6471 6472 wc = kzalloc(sizeof(*wc), GFP_NOFS); 6473 - BUG_ON(!wc); 6474 6475 btrfs_assert_tree_locked(parent); 6476 parent_level = btrfs_header_level(parent); ··· 6932 } 6933 6934 path = btrfs_alloc_path(); 6935 - BUG_ON(!path); 6936 6937 cur_pos = extent_key->objectid - offset; 6938 last_byte = extent_key->objectid + extent_key->offset; ··· 6978 struct disk_extent *old = exts; 6979 max *= 2; 6980 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); 6981 memcpy(exts, old, sizeof(*exts) * nr); 6982 if (old != *extents) 6983 kfree(old); ··· 7464 int ret; 7465 7466 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); 7467 - BUG_ON(!new_extent); 7468 7469 ref = btrfs_lookup_leaf_ref(root, leaf->start); 7470 BUG_ON(!ref); ··· 7651 7652 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 7653 BUG_ON(!reloc_root); 7654 - btrfs_orphan_cleanup(reloc_root); 7655 return 0; 7656 } 7657 ··· 7670 return 0; 7671 7672 root_item = kmalloc(sizeof(*root_item), GFP_NOFS); 7673 - BUG_ON(!root_item); 7674 7675 ret = btrfs_copy_root(trans, root, root->commit_root, 7676 &eb, BTRFS_TREE_RELOC_OBJECTID); ··· 7697 7698 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, 7699 &root_key); 7700 - BUG_ON(!reloc_root); 7701 reloc_root->last_trans = trans->transid; 7702 reloc_root->commit_root = NULL; 7703 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; ··· 7950 7951 eb = read_tree_block(found_root, block_start, 7952 block_size, 0); 7953 btrfs_tree_lock(eb); 7954 BUG_ON(level != btrfs_header_level(eb)); 7955 ··· 8669 BUG_ON(!block_group); 8670 BUG_ON(!block_group->ro); 8671 8672 memcpy(&key, &block_group->key, sizeof(key)); 8673 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | 8674 BTRFS_BLOCK_GROUP_RAID1 | ··· 8778 return ret; 8779 } 8780 8781 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) 8782 { 8783 return unpin_extent_range(root, start, end); 8784 } 8785 8786 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, 8787 - u64 num_bytes) 8788 { 8789 - return btrfs_discard_extent(root, bytenr, num_bytes); 8790 }
··· 36 static int update_block_group(struct btrfs_trans_handle *trans, 37 struct btrfs_root *root, 38 u64 bytenr, u64 num_bytes, int alloc); 39 static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 40 struct btrfs_root *root, 41 u64 bytenr, u64 num_bytes, u64 parent, ··· 442 * allocate blocks for the tree root we can't do the fast caching since 443 * we likely hold important locks. 444 */ 445 + if (trans && (!trans->transaction->in_commit) && 446 (root && root != root->fs_info->tree_root)) { 447 spin_lock(&cache->lock); 448 if (cache->cached != BTRFS_CACHE_NO) { ··· 471 if (load_cache_only) 472 return 0; 473 474 + caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 475 BUG_ON(!caching_ctl); 476 477 INIT_LIST_HEAD(&caching_ctl->list); ··· 1740 return ret; 1741 } 1742 1743 + static int btrfs_issue_discard(struct block_device *bdev, 1744 u64 start, u64 len) 1745 { 1746 + return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); 1747 } 1748 1749 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, 1750 + u64 num_bytes, u64 *actual_bytes) 1751 { 1752 int ret; 1753 + u64 discarded_bytes = 0; 1754 struct btrfs_multi_bio *multi = NULL; 1755 1756 1757 /* Tell the block device(s) that the sectors can be discarded */ 1758 + ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, 1759 + bytenr, &num_bytes, &multi, 0); 1760 if (!ret) { 1761 struct btrfs_bio_stripe *stripe = multi->stripes; 1762 int i; 1763 1764 1765 for (i = 0; i < multi->num_stripes; i++, stripe++) { 1766 + ret = btrfs_issue_discard(stripe->dev->bdev, 1767 + stripe->physical, 1768 + stripe->length); 1769 + if (!ret) 1770 + discarded_bytes += stripe->length; 1771 + else if (ret != -EOPNOTSUPP) 1772 + break; 1773 } 1774 kfree(multi); 1775 } 1776 + if (discarded_bytes && ret == -EOPNOTSUPP) 1777 + ret = 0; 1778 + 1779 + if (actual_bytes) 1780 + *actual_bytes = discarded_bytes; 1781 + 1782 1783 return ret; 1784 } ··· 3996 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; 3997 u64 to_reserve; 3998 int nr_extents; 3999 + int reserved_extents; 4000 int ret; 4001 4002 if (btrfs_transaction_in_commit(root->fs_info)) ··· 4003 4004 num_bytes = ALIGN(num_bytes, root->sectorsize); 4005 4006 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; 4007 + reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); 4008 + 4009 + if (nr_extents > reserved_extents) { 4010 + nr_extents -= reserved_extents; 4011 to_reserve = calc_trans_metadata_size(root, nr_extents); 4012 } else { 4013 nr_extents = 0; 4014 to_reserve = 0; 4015 } 4016 + 4017 to_reserve += calc_csum_metadata_size(inode, num_bytes); 4018 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); 4019 if (ret) 4020 return ret; 4021 4022 + atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents); 4023 atomic_inc(&BTRFS_I(inode)->outstanding_extents); 4024 4025 block_rsv_add_bytes(block_rsv, to_reserve, 1); 4026 ··· 4036 struct btrfs_root *root = BTRFS_I(inode)->root; 4037 u64 to_free; 4038 int nr_extents; 4039 + int reserved_extents; 4040 4041 num_bytes = ALIGN(num_bytes, root->sectorsize); 4042 atomic_dec(&BTRFS_I(inode)->outstanding_extents); 4043 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0); 4044 4045 + reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); 4046 + do { 4047 + int old, new; 4048 + 4049 + nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); 4050 + if (nr_extents >= reserved_extents) { 4051 + nr_extents = 0; 4052 + break; 4053 + } 4054 + old = reserved_extents; 4055 + nr_extents = reserved_extents - nr_extents; 4056 + new = reserved_extents - nr_extents; 4057 + old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents, 4058 + reserved_extents, new); 4059 + if (likely(old == reserved_extents)) 4060 + break; 4061 + reserved_extents = old; 4062 + } while (1); 4063 4064 to_free = calc_csum_metadata_size(inode, num_bytes); 4065 if (nr_extents > 0) ··· 4223 * update size of reserved extents. this function may return -EAGAIN 4224 * if 'reserve' is true or 'sinfo' is false. 4225 */ 4226 + int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 4227 + u64 num_bytes, int reserve, int sinfo) 4228 { 4229 int ret = 0; 4230 if (sinfo) { ··· 4363 if (ret) 4364 break; 4365 4366 + if (btrfs_test_opt(root, DISCARD)) 4367 + ret = btrfs_discard_extent(root, start, 4368 + end + 1 - start, NULL); 4369 4370 clear_extent_dirty(unpin, start, end, GFP_NOFS); 4371 unpin_extent_range(root, start, end); ··· 4704 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 4705 4706 btrfs_add_free_space(cache, buf->start, buf->len); 4707 + ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0); 4708 if (ret == -EAGAIN) { 4709 /* block group became read-only */ 4710 + btrfs_update_reserved_bytes(cache, buf->len, 0, 1); 4711 goto out; 4712 } 4713 ··· 4744 } 4745 } 4746 out: 4747 + /* 4748 + * Deleting the buffer, clear the corrupt flag since it doesn't matter 4749 + * anymore. 4750 + */ 4751 + clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); 4752 btrfs_put_block_group(cache); 4753 } 4754 ··· 5191 search_start - offset); 5192 BUG_ON(offset > search_start); 5193 5194 + ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1, 5195 (data & BTRFS_BLOCK_GROUP_DATA)); 5196 if (ret == -EAGAIN) { 5197 btrfs_add_free_space(block_group, offset, num_bytes); ··· 5397 dump_space_info(sinfo, num_bytes, 1); 5398 } 5399 5400 + trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); 5401 + 5402 return ret; 5403 } 5404 ··· 5412 return -ENOSPC; 5413 } 5414 5415 + if (btrfs_test_opt(root, DISCARD)) 5416 + ret = btrfs_discard_extent(root, start, len, NULL); 5417 5418 btrfs_add_free_space(cache, start, len); 5419 + btrfs_update_reserved_bytes(cache, len, 0, 1); 5420 btrfs_put_block_group(cache); 5421 + 5422 + trace_btrfs_reserved_extent_free(root, start, len); 5423 5424 return ret; 5425 } ··· 5444 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); 5445 5446 path = btrfs_alloc_path(); 5447 + if (!path) 5448 + return -ENOMEM; 5449 5450 path->leave_spinning = 1; 5451 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, ··· 5614 put_caching_control(caching_ctl); 5615 } 5616 5617 + ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1); 5618 BUG_ON(ret); 5619 btrfs_put_block_group(block_group); 5620 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, ··· 6047 if (reada && level == 1) 6048 reada_walk_down(trans, root, wc, path); 6049 next = read_tree_block(root, bytenr, blocksize, generation); 6050 + if (!next) 6051 + return -EIO; 6052 btrfs_tree_lock(next); 6053 btrfs_set_lock_blocking(next); 6054 } ··· 6438 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); 6439 6440 path = btrfs_alloc_path(); 6441 + if (!path) 6442 + return -ENOMEM; 6443 6444 wc = kzalloc(sizeof(*wc), GFP_NOFS); 6445 + if (!wc) { 6446 + btrfs_free_path(path); 6447 + return -ENOMEM; 6448 + } 6449 6450 btrfs_assert_tree_locked(parent); 6451 parent_level = btrfs_header_level(parent); ··· 6899 } 6900 6901 path = btrfs_alloc_path(); 6902 + if (!path) { 6903 + if (exts != *extents) 6904 + kfree(exts); 6905 + return -ENOMEM; 6906 + } 6907 6908 cur_pos = extent_key->objectid - offset; 6909 last_byte = extent_key->objectid + extent_key->offset; ··· 6941 struct disk_extent *old = exts; 6942 max *= 2; 6943 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); 6944 + if (!exts) { 6945 + ret = -ENOMEM; 6946 + goto out; 6947 + } 6948 memcpy(exts, old, sizeof(*exts) * nr); 6949 if (old != *extents) 6950 kfree(old); ··· 7423 int ret; 7424 7425 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); 7426 + if (!new_extent) 7427 + return -ENOMEM; 7428 7429 ref = btrfs_lookup_leaf_ref(root, leaf->start); 7430 BUG_ON(!ref); ··· 7609 7610 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 7611 BUG_ON(!reloc_root); 7612 + ret = btrfs_orphan_cleanup(reloc_root); 7613 + BUG_ON(ret); 7614 return 0; 7615 } 7616 ··· 7627 return 0; 7628 7629 root_item = kmalloc(sizeof(*root_item), GFP_NOFS); 7630 + if (!root_item) 7631 + return -ENOMEM; 7632 7633 ret = btrfs_copy_root(trans, root, root->commit_root, 7634 &eb, BTRFS_TREE_RELOC_OBJECTID); ··· 7653 7654 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, 7655 &root_key); 7656 + BUG_ON(IS_ERR(reloc_root)); 7657 reloc_root->last_trans = trans->transid; 7658 reloc_root->commit_root = NULL; 7659 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; ··· 7906 7907 eb = read_tree_block(found_root, block_start, 7908 block_size, 0); 7909 + if (!eb) { 7910 + ret = -EIO; 7911 + goto out; 7912 + } 7913 btrfs_tree_lock(eb); 7914 BUG_ON(level != btrfs_header_level(eb)); 7915 ··· 8621 BUG_ON(!block_group); 8622 BUG_ON(!block_group->ro); 8623 8624 + /* 8625 + * Free the reserved super bytes from this block group before 8626 + * remove it. 8627 + */ 8628 + free_excluded_extents(root, block_group); 8629 + 8630 memcpy(&key, &block_group->key, sizeof(key)); 8631 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | 8632 BTRFS_BLOCK_GROUP_RAID1 | ··· 8724 return ret; 8725 } 8726 8727 + int btrfs_init_space_info(struct btrfs_fs_info *fs_info) 8728 + { 8729 + struct btrfs_space_info *space_info; 8730 + int ret; 8731 + 8732 + ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0, 8733 + &space_info); 8734 + if (ret) 8735 + return ret; 8736 + 8737 + ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0, 8738 + &space_info); 8739 + if (ret) 8740 + return ret; 8741 + 8742 + ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0, 8743 + &space_info); 8744 + if (ret) 8745 + return ret; 8746 + 8747 + return ret; 8748 + } 8749 + 8750 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) 8751 { 8752 return unpin_extent_range(root, start, end); 8753 } 8754 8755 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, 8756 + u64 num_bytes, u64 *actual_bytes) 8757 { 8758 + return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); 8759 + } 8760 + 8761 + int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) 8762 + { 8763 + struct btrfs_fs_info *fs_info = root->fs_info; 8764 + struct btrfs_block_group_cache *cache = NULL; 8765 + u64 group_trimmed; 8766 + u64 start; 8767 + u64 end; 8768 + u64 trimmed = 0; 8769 + int ret = 0; 8770 + 8771 + cache = btrfs_lookup_block_group(fs_info, range->start); 8772 + 8773 + while (cache) { 8774 + if (cache->key.objectid >= (range->start + range->len)) { 8775 + btrfs_put_block_group(cache); 8776 + break; 8777 + } 8778 + 8779 + start = max(range->start, cache->key.objectid); 8780 + end = min(range->start + range->len, 8781 + cache->key.objectid + cache->key.offset); 8782 + 8783 + if (end - start >= range->minlen) { 8784 + if (!block_group_cache_done(cache)) { 8785 + ret = cache_block_group(cache, NULL, root, 0); 8786 + if (!ret) 8787 + wait_block_group_cache_done(cache); 8788 + } 8789 + ret = btrfs_trim_block_group(cache, 8790 + &group_trimmed, 8791 + start, 8792 + end, 8793 + range->minlen); 8794 + 8795 + trimmed += group_trimmed; 8796 + if (ret) { 8797 + btrfs_put_block_group(cache); 8798 + break; 8799 + } 8800 + } 8801 + 8802 + cache = next_block_group(fs_info->tree_root, cache); 8803 + } 8804 + 8805 + range->len = trimmed; 8806 + return ret; 8807 }
+3
fs/btrfs/extent_io.c
··· 2192 else 2193 write_flags = WRITE; 2194 2195 WARN_ON(!PageLocked(page)); 2196 pg_offset = i_size & (PAGE_CACHE_SIZE - 1); 2197 if (page->index > end_index || ··· 3692 "wanted %lu %lu\n", (unsigned long long)eb->start, 3693 eb->len, start, min_len); 3694 WARN_ON(1); 3695 } 3696 3697 p = extent_buffer_page(eb, i);
··· 2192 else 2193 write_flags = WRITE; 2194 2195 + trace___extent_writepage(page, inode, wbc); 2196 + 2197 WARN_ON(!PageLocked(page)); 2198 pg_offset = i_size & (PAGE_CACHE_SIZE - 1); 2199 if (page->index > end_index || ··· 3690 "wanted %lu %lu\n", (unsigned long long)eb->start, 3691 eb->len, start, min_len); 3692 WARN_ON(1); 3693 + return -EINVAL; 3694 } 3695 3696 p = extent_buffer_page(eb, i);
+1
fs/btrfs/extent_io.h
··· 31 #define EXTENT_BUFFER_UPTODATE 0 32 #define EXTENT_BUFFER_BLOCKING 1 33 #define EXTENT_BUFFER_DIRTY 2 34 35 /* these are flags for extent_clear_unlock_delalloc */ 36 #define EXTENT_CLEAR_UNLOCK_PAGE 0x1
··· 31 #define EXTENT_BUFFER_UPTODATE 0 32 #define EXTENT_BUFFER_BLOCKING 1 33 #define EXTENT_BUFFER_DIRTY 2 34 + #define EXTENT_BUFFER_CORRUPT 3 35 36 /* these are flags for extent_clear_unlock_delalloc */ 37 #define EXTENT_CLEAR_UNLOCK_PAGE 0x1
+4 -1
fs/btrfs/file-item.c
··· 48 struct extent_buffer *leaf; 49 50 path = btrfs_alloc_path(); 51 - BUG_ON(!path); 52 file_key.objectid = objectid; 53 file_key.offset = pos; 54 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); ··· 170 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 171 172 path = btrfs_alloc_path(); 173 if (bio->bi_size > PAGE_CACHE_SIZE * 8) 174 path->reada = 2; 175
··· 48 struct extent_buffer *leaf; 49 50 path = btrfs_alloc_path(); 51 + if (!path) 52 + return -ENOMEM; 53 file_key.objectid = objectid; 54 file_key.offset = pos; 55 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); ··· 169 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 170 171 path = btrfs_alloc_path(); 172 + if (!path) 173 + return -ENOMEM; 174 if (bio->bi_size > PAGE_CACHE_SIZE * 8) 175 path->reada = 2; 176
+200 -190
fs/btrfs/file.c
··· 45 * and be replaced with calls into generic code. 46 */ 47 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, 48 - int write_bytes, 49 struct page **prepared_pages, 50 struct iov_iter *i) 51 { 52 size_t copied = 0; 53 int pg = 0; 54 int offset = pos & (PAGE_CACHE_SIZE - 1); 55 - int total_copied = 0; 56 57 while (write_bytes > 0) { 58 size_t count = min_t(size_t, ··· 88 total_copied += copied; 89 90 /* Return to btrfs_file_aio_write to fault page */ 91 - if (unlikely(copied == 0)) { 92 break; 93 - } 94 95 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { 96 offset += copied; ··· 108 { 109 size_t i; 110 for (i = 0; i < num_pages; i++) { 111 - if (!pages[i]) 112 - break; 113 /* page checked is some magic around finding pages that 114 * have been modified without going through btrfs_set_page_dirty 115 * clear it here ··· 127 * this also makes the decision about creating an inline extent vs 128 * doing real data extents, marking pages dirty and delalloc as required. 129 */ 130 - static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, 131 - struct btrfs_root *root, 132 - struct file *file, 133 - struct page **pages, 134 - size_t num_pages, 135 - loff_t pos, 136 - size_t write_bytes) 137 { 138 int err = 0; 139 int i; ··· 150 end_of_last_block = start_pos + num_bytes - 1; 151 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 152 NULL); 153 - BUG_ON(err); 154 155 for (i = 0; i < num_pages; i++) { 156 struct page *p = pages[i]; ··· 159 ClearPageChecked(p); 160 set_page_dirty(p); 161 } 162 - if (end_pos > isize) { 163 i_size_write(inode, end_pos); 164 - /* we've only changed i_size in ram, and we haven't updated 165 - * the disk i_size. There is no need to log the inode 166 - * at this time. 167 - */ 168 - } 169 return 0; 170 } 171 ··· 608 key.offset = split; 609 610 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 611 if (ret > 0 && path->slots[0] > 0) 612 path->slots[0]--; 613 ··· 819 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; 820 821 if (start_pos > inode->i_size) { 822 - err = btrfs_cont_expand(inode, start_pos); 823 if (err) 824 return err; 825 } 826 827 - memset(pages, 0, num_pages * sizeof(struct page *)); 828 again: 829 for (i = 0; i < num_pages; i++) { 830 pages[i] = grab_cache_page(inode->i_mapping, index + i); ··· 895 896 } 897 898 - static ssize_t btrfs_file_aio_write(struct kiocb *iocb, 899 - const struct iovec *iov, 900 - unsigned long nr_segs, loff_t pos) 901 { 902 - struct file *file = iocb->ki_filp; 903 struct inode *inode = fdentry(file)->d_inode; 904 struct btrfs_root *root = BTRFS_I(inode)->root; 905 struct page **pages = NULL; 906 - struct iov_iter i; 907 - loff_t *ppos = &iocb->ki_pos; 908 - loff_t start_pos; 909 - ssize_t num_written = 0; 910 - ssize_t err = 0; 911 - size_t count; 912 - size_t ocount; 913 - int ret = 0; 914 - int nrptrs; 915 unsigned long first_index; 916 unsigned long last_index; 917 - int will_write; 918 - int buffered = 0; 919 - int copied = 0; 920 - int dirty_pages = 0; 921 922 - will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) || 923 - (file->f_flags & O_DIRECT)); 924 - 925 - start_pos = pos; 926 - 927 - vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 928 - 929 - mutex_lock(&inode->i_mutex); 930 - 931 - err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); 932 - if (err) 933 - goto out; 934 - count = ocount; 935 - 936 - current->backing_dev_info = inode->i_mapping->backing_dev_info; 937 - err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); 938 - if (err) 939 - goto out; 940 - 941 - if (count == 0) 942 - goto out; 943 - 944 - err = file_remove_suid(file); 945 - if (err) 946 - goto out; 947 - 948 - /* 949 - * If BTRFS flips readonly due to some impossible error 950 - * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), 951 - * although we have opened a file as writable, we have 952 - * to stop this write operation to ensure FS consistency. 953 - */ 954 - if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 955 - err = -EROFS; 956 - goto out; 957 - } 958 - 959 - file_update_time(file); 960 - BTRFS_I(inode)->sequence++; 961 - 962 - if (unlikely(file->f_flags & O_DIRECT)) { 963 - num_written = generic_file_direct_write(iocb, iov, &nr_segs, 964 - pos, ppos, count, 965 - ocount); 966 - /* 967 - * the generic O_DIRECT will update in-memory i_size after the 968 - * DIOs are done. But our endio handlers that update the on 969 - * disk i_size never update past the in memory i_size. So we 970 - * need one more update here to catch any additions to the 971 - * file 972 - */ 973 - if (inode->i_size != BTRFS_I(inode)->disk_i_size) { 974 - btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 975 - mark_inode_dirty(inode); 976 - } 977 - 978 - if (num_written < 0) { 979 - ret = num_written; 980 - num_written = 0; 981 - goto out; 982 - } else if (num_written == count) { 983 - /* pick up pos changes done by the generic code */ 984 - pos = *ppos; 985 - goto out; 986 - } 987 - /* 988 - * We are going to do buffered for the rest of the range, so we 989 - * need to make sure to invalidate the buffered pages when we're 990 - * done. 991 - */ 992 - buffered = 1; 993 - pos += num_written; 994 - } 995 - 996 - iov_iter_init(&i, iov, nr_segs, count, num_written); 997 - nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) / 998 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / 999 (sizeof(struct page *))); 1000 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); 1001 - if (!pages) { 1002 - ret = -ENOMEM; 1003 - goto out; 1004 - } 1005 - 1006 - /* generic_write_checks can change our pos */ 1007 - start_pos = pos; 1008 1009 first_index = pos >> PAGE_CACHE_SHIFT; 1010 - last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT; 1011 1012 - while (iov_iter_count(&i) > 0) { 1013 size_t offset = pos & (PAGE_CACHE_SIZE - 1); 1014 - size_t write_bytes = min(iov_iter_count(&i), 1015 nrptrs * (size_t)PAGE_CACHE_SIZE - 1016 offset); 1017 size_t num_pages = (write_bytes + offset + 1018 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 1019 1020 WARN_ON(num_pages > nrptrs); 1021 - memset(pages, 0, sizeof(struct page *) * nrptrs); 1022 1023 /* 1024 * Fault pages before locking them in prepare_pages 1025 * to avoid recursive lock 1026 */ 1027 - if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) { 1028 ret = -EFAULT; 1029 - goto out; 1030 } 1031 1032 ret = btrfs_delalloc_reserve_space(inode, 1033 num_pages << PAGE_CACHE_SHIFT); 1034 if (ret) 1035 - goto out; 1036 1037 ret = prepare_pages(root, file, pages, num_pages, 1038 pos, first_index, last_index, 1039 write_bytes); 1040 if (ret) { 1041 btrfs_delalloc_release_space(inode, 1042 num_pages << PAGE_CACHE_SHIFT); 1043 - goto out; 1044 } 1045 1046 copied = btrfs_copy_from_user(pos, num_pages, 1047 - write_bytes, pages, &i); 1048 1049 /* 1050 * if we have trouble faulting in the pages, fall ··· 975 PAGE_CACHE_SIZE - 1) >> 976 PAGE_CACHE_SHIFT; 977 978 if (num_pages > dirty_pages) { 979 if (copied > 0) 980 atomic_inc( ··· 992 } 993 994 if (copied > 0) { 995 - dirty_and_release_pages(NULL, root, file, pages, 996 - dirty_pages, pos, copied); 997 } 998 999 btrfs_drop_pages(pages, num_pages); 1000 1001 - if (copied > 0) { 1002 - if (will_write) { 1003 - filemap_fdatawrite_range(inode->i_mapping, pos, 1004 - pos + copied - 1); 1005 - } else { 1006 - balance_dirty_pages_ratelimited_nr( 1007 - inode->i_mapping, 1008 - dirty_pages); 1009 - if (dirty_pages < 1010 - (root->leafsize >> PAGE_CACHE_SHIFT) + 1) 1011 - btrfs_btree_balance_dirty(root, 1); 1012 - btrfs_throttle(root); 1013 - } 1014 - } 1015 1016 pos += copied; 1017 num_written += copied; 1018 - 1019 - cond_resched(); 1020 } 1021 - out: 1022 - mutex_unlock(&inode->i_mutex); 1023 - if (ret) 1024 - err = ret; 1025 1026 kfree(pages); 1027 - *ppos = pos; 1028 1029 /* 1030 * we want to make sure fsync finds this change ··· 1157 * one running right now. 1158 */ 1159 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 1160 - 1161 - if (num_written > 0 && will_write) { 1162 - struct btrfs_trans_handle *trans; 1163 - 1164 - err = btrfs_wait_ordered_range(inode, start_pos, num_written); 1165 - if (err) 1166 num_written = err; 1167 - 1168 - if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { 1169 - trans = btrfs_start_transaction(root, 0); 1170 - if (IS_ERR(trans)) { 1171 - num_written = PTR_ERR(trans); 1172 - goto done; 1173 - } 1174 - mutex_lock(&inode->i_mutex); 1175 - ret = btrfs_log_dentry_safe(trans, root, 1176 - file->f_dentry); 1177 - mutex_unlock(&inode->i_mutex); 1178 - if (ret == 0) { 1179 - ret = btrfs_sync_log(trans, root); 1180 - if (ret == 0) 1181 - btrfs_end_transaction(trans, root); 1182 - else 1183 - btrfs_commit_transaction(trans, root); 1184 - } else if (ret != BTRFS_NO_LOG_SYNC) { 1185 - btrfs_commit_transaction(trans, root); 1186 - } else { 1187 - btrfs_end_transaction(trans, root); 1188 - } 1189 - } 1190 - if (file->f_flags & O_DIRECT && buffered) { 1191 - invalidate_mapping_pages(inode->i_mapping, 1192 - start_pos >> PAGE_CACHE_SHIFT, 1193 - (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT); 1194 - } 1195 } 1196 - done: 1197 current->backing_dev_info = NULL; 1198 return num_written ? num_written : err; 1199 } ··· 1205 int ret = 0; 1206 struct btrfs_trans_handle *trans; 1207 1208 1209 /* we wait first, since the writeback may change the inode */ 1210 root->log_batch++; ··· 1333 goto out; 1334 1335 if (alloc_start > inode->i_size) { 1336 - ret = btrfs_cont_expand(inode, alloc_start); 1337 if (ret) 1338 goto out; 1339 }
··· 45 * and be replaced with calls into generic code. 46 */ 47 static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, 48 + size_t write_bytes, 49 struct page **prepared_pages, 50 struct iov_iter *i) 51 { 52 size_t copied = 0; 53 + size_t total_copied = 0; 54 int pg = 0; 55 int offset = pos & (PAGE_CACHE_SIZE - 1); 56 57 while (write_bytes > 0) { 58 size_t count = min_t(size_t, ··· 88 total_copied += copied; 89 90 /* Return to btrfs_file_aio_write to fault page */ 91 + if (unlikely(copied == 0)) 92 break; 93 94 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { 95 offset += copied; ··· 109 { 110 size_t i; 111 for (i = 0; i < num_pages; i++) { 112 /* page checked is some magic around finding pages that 113 * have been modified without going through btrfs_set_page_dirty 114 * clear it here ··· 130 * this also makes the decision about creating an inline extent vs 131 * doing real data extents, marking pages dirty and delalloc as required. 132 */ 133 + static noinline int dirty_and_release_pages(struct btrfs_root *root, 134 + struct file *file, 135 + struct page **pages, 136 + size_t num_pages, 137 + loff_t pos, 138 + size_t write_bytes) 139 { 140 int err = 0; 141 int i; ··· 154 end_of_last_block = start_pos + num_bytes - 1; 155 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 156 NULL); 157 + if (err) 158 + return err; 159 160 for (i = 0; i < num_pages; i++) { 161 struct page *p = pages[i]; ··· 162 ClearPageChecked(p); 163 set_page_dirty(p); 164 } 165 + 166 + /* 167 + * we've only changed i_size in ram, and we haven't updated 168 + * the disk i_size. There is no need to log the inode 169 + * at this time. 170 + */ 171 + if (end_pos > isize) 172 i_size_write(inode, end_pos); 173 return 0; 174 } 175 ··· 610 key.offset = split; 611 612 ret = btrfs_search_slot(trans, root, &key, path, -1, 1); 613 + if (ret < 0) 614 + goto out; 615 if (ret > 0 && path->slots[0] > 0) 616 path->slots[0]--; 617 ··· 819 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; 820 821 if (start_pos > inode->i_size) { 822 + err = btrfs_cont_expand(inode, i_size_read(inode), start_pos); 823 if (err) 824 return err; 825 } 826 827 again: 828 for (i = 0; i < num_pages; i++) { 829 pages[i] = grab_cache_page(inode->i_mapping, index + i); ··· 896 897 } 898 899 + static noinline ssize_t __btrfs_buffered_write(struct file *file, 900 + struct iov_iter *i, 901 + loff_t pos) 902 { 903 struct inode *inode = fdentry(file)->d_inode; 904 struct btrfs_root *root = BTRFS_I(inode)->root; 905 struct page **pages = NULL; 906 unsigned long first_index; 907 unsigned long last_index; 908 + size_t num_written = 0; 909 + int nrptrs; 910 + int ret; 911 912 + nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) / 913 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / 914 (sizeof(struct page *))); 915 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); 916 + if (!pages) 917 + return -ENOMEM; 918 919 first_index = pos >> PAGE_CACHE_SHIFT; 920 + last_index = (pos + iov_iter_count(i)) >> PAGE_CACHE_SHIFT; 921 922 + while (iov_iter_count(i) > 0) { 923 size_t offset = pos & (PAGE_CACHE_SIZE - 1); 924 + size_t write_bytes = min(iov_iter_count(i), 925 nrptrs * (size_t)PAGE_CACHE_SIZE - 926 offset); 927 size_t num_pages = (write_bytes + offset + 928 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 929 + size_t dirty_pages; 930 + size_t copied; 931 932 WARN_ON(num_pages > nrptrs); 933 934 /* 935 * Fault pages before locking them in prepare_pages 936 * to avoid recursive lock 937 */ 938 + if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) { 939 ret = -EFAULT; 940 + break; 941 } 942 943 ret = btrfs_delalloc_reserve_space(inode, 944 num_pages << PAGE_CACHE_SHIFT); 945 if (ret) 946 + break; 947 948 + /* 949 + * This is going to setup the pages array with the number of 950 + * pages we want, so we don't really need to worry about the 951 + * contents of pages from loop to loop 952 + */ 953 ret = prepare_pages(root, file, pages, num_pages, 954 pos, first_index, last_index, 955 write_bytes); 956 if (ret) { 957 btrfs_delalloc_release_space(inode, 958 num_pages << PAGE_CACHE_SHIFT); 959 + break; 960 } 961 962 copied = btrfs_copy_from_user(pos, num_pages, 963 + write_bytes, pages, i); 964 965 /* 966 * if we have trouble faulting in the pages, fall ··· 1061 PAGE_CACHE_SIZE - 1) >> 1062 PAGE_CACHE_SHIFT; 1063 1064 + /* 1065 + * If we had a short copy we need to release the excess delaloc 1066 + * bytes we reserved. We need to increment outstanding_extents 1067 + * because btrfs_delalloc_release_space will decrement it, but 1068 + * we still have an outstanding extent for the chunk we actually 1069 + * managed to copy. 1070 + */ 1071 if (num_pages > dirty_pages) { 1072 if (copied > 0) 1073 atomic_inc( ··· 1071 } 1072 1073 if (copied > 0) { 1074 + ret = dirty_and_release_pages(root, file, pages, 1075 + dirty_pages, pos, 1076 + copied); 1077 + if (ret) { 1078 + btrfs_delalloc_release_space(inode, 1079 + dirty_pages << PAGE_CACHE_SHIFT); 1080 + btrfs_drop_pages(pages, num_pages); 1081 + break; 1082 + } 1083 } 1084 1085 btrfs_drop_pages(pages, num_pages); 1086 1087 + cond_resched(); 1088 + 1089 + balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1090 + dirty_pages); 1091 + if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1) 1092 + btrfs_btree_balance_dirty(root, 1); 1093 + btrfs_throttle(root); 1094 1095 pos += copied; 1096 num_written += copied; 1097 } 1098 1099 kfree(pages); 1100 + 1101 + return num_written ? num_written : ret; 1102 + } 1103 + 1104 + static ssize_t __btrfs_direct_write(struct kiocb *iocb, 1105 + const struct iovec *iov, 1106 + unsigned long nr_segs, loff_t pos, 1107 + loff_t *ppos, size_t count, size_t ocount) 1108 + { 1109 + struct file *file = iocb->ki_filp; 1110 + struct inode *inode = fdentry(file)->d_inode; 1111 + struct iov_iter i; 1112 + ssize_t written; 1113 + ssize_t written_buffered; 1114 + loff_t endbyte; 1115 + int err; 1116 + 1117 + written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos, 1118 + count, ocount); 1119 + 1120 + /* 1121 + * the generic O_DIRECT will update in-memory i_size after the 1122 + * DIOs are done. But our endio handlers that update the on 1123 + * disk i_size never update past the in memory i_size. So we 1124 + * need one more update here to catch any additions to the 1125 + * file 1126 + */ 1127 + if (inode->i_size != BTRFS_I(inode)->disk_i_size) { 1128 + btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 1129 + mark_inode_dirty(inode); 1130 + } 1131 + 1132 + if (written < 0 || written == count) 1133 + return written; 1134 + 1135 + pos += written; 1136 + count -= written; 1137 + iov_iter_init(&i, iov, nr_segs, count, written); 1138 + written_buffered = __btrfs_buffered_write(file, &i, pos); 1139 + if (written_buffered < 0) { 1140 + err = written_buffered; 1141 + goto out; 1142 + } 1143 + endbyte = pos + written_buffered - 1; 1144 + err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte); 1145 + if (err) 1146 + goto out; 1147 + written += written_buffered; 1148 + *ppos = pos + written_buffered; 1149 + invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT, 1150 + endbyte >> PAGE_CACHE_SHIFT); 1151 + out: 1152 + return written ? written : err; 1153 + } 1154 + 1155 + static ssize_t btrfs_file_aio_write(struct kiocb *iocb, 1156 + const struct iovec *iov, 1157 + unsigned long nr_segs, loff_t pos) 1158 + { 1159 + struct file *file = iocb->ki_filp; 1160 + struct inode *inode = fdentry(file)->d_inode; 1161 + struct btrfs_root *root = BTRFS_I(inode)->root; 1162 + loff_t *ppos = &iocb->ki_pos; 1163 + ssize_t num_written = 0; 1164 + ssize_t err = 0; 1165 + size_t count, ocount; 1166 + 1167 + vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); 1168 + 1169 + mutex_lock(&inode->i_mutex); 1170 + 1171 + err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); 1172 + if (err) { 1173 + mutex_unlock(&inode->i_mutex); 1174 + goto out; 1175 + } 1176 + count = ocount; 1177 + 1178 + current->backing_dev_info = inode->i_mapping->backing_dev_info; 1179 + err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); 1180 + if (err) { 1181 + mutex_unlock(&inode->i_mutex); 1182 + goto out; 1183 + } 1184 + 1185 + if (count == 0) { 1186 + mutex_unlock(&inode->i_mutex); 1187 + goto out; 1188 + } 1189 + 1190 + err = file_remove_suid(file); 1191 + if (err) { 1192 + mutex_unlock(&inode->i_mutex); 1193 + goto out; 1194 + } 1195 + 1196 + /* 1197 + * If BTRFS flips readonly due to some impossible error 1198 + * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR), 1199 + * although we have opened a file as writable, we have 1200 + * to stop this write operation to ensure FS consistency. 1201 + */ 1202 + if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) { 1203 + mutex_unlock(&inode->i_mutex); 1204 + err = -EROFS; 1205 + goto out; 1206 + } 1207 + 1208 + file_update_time(file); 1209 + BTRFS_I(inode)->sequence++; 1210 + 1211 + if (unlikely(file->f_flags & O_DIRECT)) { 1212 + num_written = __btrfs_direct_write(iocb, iov, nr_segs, 1213 + pos, ppos, count, ocount); 1214 + } else { 1215 + struct iov_iter i; 1216 + 1217 + iov_iter_init(&i, iov, nr_segs, count, num_written); 1218 + 1219 + num_written = __btrfs_buffered_write(file, &i, pos); 1220 + if (num_written > 0) 1221 + *ppos = pos + num_written; 1222 + } 1223 + 1224 + mutex_unlock(&inode->i_mutex); 1225 1226 /* 1227 * we want to make sure fsync finds this change ··· 1118 * one running right now. 1119 */ 1120 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; 1121 + if (num_written > 0 || num_written == -EIOCBQUEUED) { 1122 + err = generic_write_sync(file, pos, num_written); 1123 + if (err < 0 && num_written > 0) 1124 num_written = err; 1125 } 1126 + out: 1127 current->backing_dev_info = NULL; 1128 return num_written ? num_written : err; 1129 } ··· 1197 int ret = 0; 1198 struct btrfs_trans_handle *trans; 1199 1200 + trace_btrfs_sync_file(file, datasync); 1201 1202 /* we wait first, since the writeback may change the inode */ 1203 root->log_batch++; ··· 1324 goto out; 1325 1326 if (alloc_start > inode->i_size) { 1327 + ret = btrfs_cont_expand(inode, i_size_read(inode), 1328 + alloc_start); 1329 if (ret) 1330 goto out; 1331 }
+322 -206
fs/btrfs/free-space-cache.c
··· 393 break; 394 395 need_loop = 1; 396 - e = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS); 397 if (!e) { 398 kunmap(page); 399 unlock_page(page); ··· 406 e->bytes = le64_to_cpu(entry->bytes); 407 if (!e->bytes) { 408 kunmap(page); 409 - kfree(e); 410 unlock_page(page); 411 page_cache_release(page); 412 goto free_cache; ··· 421 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); 422 if (!e->bitmap) { 423 kunmap(page); 424 - kfree(e); 425 unlock_page(page); 426 page_cache_release(page); 427 goto free_cache; ··· 1189 { 1190 unlink_free_space(block_group, bitmap_info); 1191 kfree(bitmap_info->bitmap); 1192 - kfree(bitmap_info); 1193 block_group->total_bitmaps--; 1194 recalculate_thresholds(block_group); 1195 } ··· 1287 * If we are below the extents threshold then we can add this as an 1288 * extent, and don't have to deal with the bitmap 1289 */ 1290 - if (block_group->free_extents < block_group->extents_thresh && 1291 - info->bytes > block_group->sectorsize * 4) 1292 - return 0; 1293 1294 /* 1295 * some block groups are so tiny they can't be enveloped by a bitmap, so ··· 1357 1358 /* no pre-allocated info, allocate a new one */ 1359 if (!info) { 1360 - info = kzalloc(sizeof(struct btrfs_free_space), 1361 - GFP_NOFS); 1362 if (!info) { 1363 spin_lock(&block_group->tree_lock); 1364 ret = -ENOMEM; ··· 1380 if (info) { 1381 if (info->bitmap) 1382 kfree(info->bitmap); 1383 - kfree(info); 1384 } 1385 1386 return ret; ··· 1413 else 1414 __unlink_free_space(block_group, right_info); 1415 info->bytes += right_info->bytes; 1416 - kfree(right_info); 1417 merged = true; 1418 } 1419 ··· 1425 __unlink_free_space(block_group, left_info); 1426 info->offset = left_info->offset; 1427 info->bytes += left_info->bytes; 1428 - kfree(left_info); 1429 merged = true; 1430 } 1431 ··· 1438 struct btrfs_free_space *info; 1439 int ret = 0; 1440 1441 - info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS); 1442 if (!info) 1443 return -ENOMEM; 1444 ··· 1465 link: 1466 ret = link_free_space(block_group, info); 1467 if (ret) 1468 - kfree(info); 1469 out: 1470 spin_unlock(&block_group->tree_lock); 1471 ··· 1535 kfree(info->bitmap); 1536 block_group->total_bitmaps--; 1537 } 1538 - kfree(info); 1539 goto out_lock; 1540 } 1541 ··· 1571 /* the hole we're creating ends at the end 1572 * of the info struct, just free the info 1573 */ 1574 - kfree(info); 1575 } 1576 spin_unlock(&block_group->tree_lock); 1577 ··· 1644 { 1645 struct btrfs_free_space *entry; 1646 struct rb_node *node; 1647 - bool bitmap; 1648 1649 spin_lock(&cluster->lock); 1650 if (cluster->block_group != block_group) 1651 goto out; 1652 1653 - bitmap = cluster->points_to_bitmap; 1654 cluster->block_group = NULL; 1655 cluster->window_start = 0; 1656 list_del_init(&cluster->block_group_list); 1657 - cluster->points_to_bitmap = false; 1658 - 1659 - if (bitmap) 1660 - goto out; 1661 1662 node = rb_first(&cluster->root); 1663 while (node) { 1664 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1665 node = rb_next(&entry->offset_index); 1666 rb_erase(&entry->offset_index, &cluster->root); 1667 - BUG_ON(entry->bitmap); 1668 - try_merge_free_space(block_group, entry, false); 1669 tree_insert_offset(&block_group->free_space_offset, 1670 - entry->offset, &entry->offset_index, 0); 1671 } 1672 cluster->root = RB_ROOT; 1673 ··· 1702 unlink_free_space(block_group, info); 1703 if (info->bitmap) 1704 kfree(info->bitmap); 1705 - kfree(info); 1706 if (need_resched()) { 1707 spin_unlock(&block_group->tree_lock); 1708 cond_resched(); ··· 1735 entry->offset += bytes; 1736 entry->bytes -= bytes; 1737 if (!entry->bytes) 1738 - kfree(entry); 1739 else 1740 link_free_space(block_group, entry); 1741 } ··· 1788 1789 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, 1790 struct btrfs_free_cluster *cluster, 1791 u64 bytes, u64 min_start) 1792 { 1793 - struct btrfs_free_space *entry; 1794 int err; 1795 u64 search_start = cluster->window_start; 1796 u64 search_bytes = bytes; 1797 u64 ret = 0; 1798 - 1799 - spin_lock(&block_group->tree_lock); 1800 - spin_lock(&cluster->lock); 1801 - 1802 - if (!cluster->points_to_bitmap) 1803 - goto out; 1804 - 1805 - if (cluster->block_group != block_group) 1806 - goto out; 1807 - 1808 - /* 1809 - * search_start is the beginning of the bitmap, but at some point it may 1810 - * be a good idea to point to the actual start of the free area in the 1811 - * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only 1812 - * to 1 to make sure we get the bitmap entry 1813 - */ 1814 - entry = tree_search_offset(block_group, 1815 - offset_to_bitmap(block_group, search_start), 1816 - 1, 0); 1817 - if (!entry || !entry->bitmap) 1818 - goto out; 1819 1820 search_start = min_start; 1821 search_bytes = bytes; ··· 1802 err = search_bitmap(block_group, entry, &search_start, 1803 &search_bytes); 1804 if (err) 1805 - goto out; 1806 1807 ret = search_start; 1808 bitmap_clear_bits(block_group, entry, ret, bytes); 1809 - if (entry->bytes == 0) 1810 - free_bitmap(block_group, entry); 1811 - out: 1812 - spin_unlock(&cluster->lock); 1813 - spin_unlock(&block_group->tree_lock); 1814 1815 return ret; 1816 } ··· 1823 struct rb_node *node; 1824 u64 ret = 0; 1825 1826 - if (cluster->points_to_bitmap) 1827 - return btrfs_alloc_from_bitmap(block_group, cluster, bytes, 1828 - min_start); 1829 - 1830 spin_lock(&cluster->lock); 1831 if (bytes > cluster->max_size) 1832 goto out; ··· 1835 goto out; 1836 1837 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1838 - 1839 while(1) { 1840 - if (entry->bytes < bytes || entry->offset < min_start) { 1841 struct rb_node *node; 1842 1843 node = rb_next(&entry->offset_index); ··· 1847 offset_index); 1848 continue; 1849 } 1850 - ret = entry->offset; 1851 1852 - entry->offset += bytes; 1853 - entry->bytes -= bytes; 1854 1855 if (entry->bytes == 0) 1856 rb_erase(&entry->offset_index, &cluster->root); ··· 1884 block_group->free_space -= bytes; 1885 if (entry->bytes == 0) { 1886 block_group->free_extents--; 1887 - kfree(entry); 1888 } 1889 1890 spin_unlock(&block_group->tree_lock); ··· 1909 unsigned long found_bits; 1910 unsigned long start = 0; 1911 unsigned long total_found = 0; 1912 bool found = false; 1913 1914 i = offset_to_bit(entry->offset, block_group->sectorsize, 1915 max_t(u64, offset, entry->offset)); 1916 - search_bits = bytes_to_bits(min_bytes, block_group->sectorsize); 1917 - total_bits = bytes_to_bits(bytes, block_group->sectorsize); 1918 1919 again: 1920 found_bits = 0; ··· 1932 } 1933 1934 if (!found_bits) 1935 - return -1; 1936 1937 if (!found) { 1938 start = i; ··· 1956 1957 cluster->window_start = start * block_group->sectorsize + 1958 entry->offset; 1959 - cluster->points_to_bitmap = true; 1960 1961 return 0; 1962 } 1963 1964 /* ··· 2108 struct btrfs_free_cluster *cluster, 2109 u64 offset, u64 bytes, u64 empty_size) 2110 { 2111 - struct btrfs_free_space *entry = NULL; 2112 - struct rb_node *node; 2113 - struct btrfs_free_space *next; 2114 - struct btrfs_free_space *last = NULL; 2115 u64 min_bytes; 2116 - u64 window_start; 2117 - u64 window_free; 2118 - u64 max_extent = 0; 2119 - bool found_bitmap = false; 2120 int ret; 2121 2122 /* for metadata, allow allocates with more holes */ ··· 2128 min_bytes = max(bytes, (bytes + empty_size) >> 2); 2129 2130 spin_lock(&block_group->tree_lock); 2131 spin_lock(&cluster->lock); 2132 2133 /* someone already found a cluster, hooray */ ··· 2145 ret = 0; 2146 goto out; 2147 } 2148 - again: 2149 - entry = tree_search_offset(block_group, offset, found_bitmap, 1); 2150 - if (!entry) { 2151 - ret = -ENOSPC; 2152 - goto out; 2153 } 2154 - 2155 - /* 2156 - * If found_bitmap is true, we exhausted our search for extent entries, 2157 - * and we just want to search all of the bitmaps that we can find, and 2158 - * ignore any extent entries we find. 2159 - */ 2160 - while (entry->bitmap || found_bitmap || 2161 - (!entry->bitmap && entry->bytes < min_bytes)) { 2162 - struct rb_node *node = rb_next(&entry->offset_index); 2163 - 2164 - if (entry->bitmap && entry->bytes > bytes + empty_size) { 2165 - ret = btrfs_bitmap_cluster(block_group, entry, cluster, 2166 - offset, bytes + empty_size, 2167 - min_bytes); 2168 - if (!ret) 2169 - goto got_it; 2170 - } 2171 - 2172 - if (!node) { 2173 - ret = -ENOSPC; 2174 - goto out; 2175 - } 2176 - entry = rb_entry(node, struct btrfs_free_space, offset_index); 2177 - } 2178 - 2179 - /* 2180 - * We already searched all the extent entries from the passed in offset 2181 - * to the end and didn't find enough space for the cluster, and we also 2182 - * didn't find any bitmaps that met our criteria, just go ahead and exit 2183 - */ 2184 - if (found_bitmap) { 2185 - ret = -ENOSPC; 2186 - goto out; 2187 - } 2188 - 2189 - cluster->points_to_bitmap = false; 2190 - window_start = entry->offset; 2191 - window_free = entry->bytes; 2192 - last = entry; 2193 - max_extent = entry->bytes; 2194 - 2195 - while (1) { 2196 - /* out window is just right, lets fill it */ 2197 - if (window_free >= bytes + empty_size) 2198 - break; 2199 - 2200 - node = rb_next(&last->offset_index); 2201 - if (!node) { 2202 - if (found_bitmap) 2203 - goto again; 2204 - ret = -ENOSPC; 2205 - goto out; 2206 - } 2207 - next = rb_entry(node, struct btrfs_free_space, offset_index); 2208 - 2209 - /* 2210 - * we found a bitmap, so if this search doesn't result in a 2211 - * cluster, we know to go and search again for the bitmaps and 2212 - * start looking for space there 2213 - */ 2214 - if (next->bitmap) { 2215 - if (!found_bitmap) 2216 - offset = next->offset; 2217 - found_bitmap = true; 2218 - last = next; 2219 - continue; 2220 - } 2221 - 2222 - /* 2223 - * we haven't filled the empty size and the window is 2224 - * very large. reset and try again 2225 - */ 2226 - if (next->offset - (last->offset + last->bytes) > 128 * 1024 || 2227 - next->offset - window_start > (bytes + empty_size) * 2) { 2228 - entry = next; 2229 - window_start = entry->offset; 2230 - window_free = entry->bytes; 2231 - last = entry; 2232 - max_extent = entry->bytes; 2233 - } else { 2234 - last = next; 2235 - window_free += next->bytes; 2236 - if (entry->bytes > max_extent) 2237 - max_extent = entry->bytes; 2238 - } 2239 - } 2240 - 2241 - cluster->window_start = entry->offset; 2242 - 2243 - /* 2244 - * now we've found our entries, pull them out of the free space 2245 - * cache and put them into the cluster rbtree 2246 - * 2247 - * The cluster includes an rbtree, but only uses the offset index 2248 - * of each free space cache entry. 2249 - */ 2250 - while (1) { 2251 - node = rb_next(&entry->offset_index); 2252 - if (entry->bitmap && node) { 2253 - entry = rb_entry(node, struct btrfs_free_space, 2254 - offset_index); 2255 - continue; 2256 - } else if (entry->bitmap && !node) { 2257 - break; 2258 - } 2259 - 2260 - rb_erase(&entry->offset_index, &block_group->free_space_offset); 2261 - ret = tree_insert_offset(&cluster->root, entry->offset, 2262 - &entry->offset_index, 0); 2263 - BUG_ON(ret); 2264 - 2265 - if (!node || entry == last) 2266 - break; 2267 - 2268 - entry = rb_entry(node, struct btrfs_free_space, offset_index); 2269 - } 2270 - 2271 - cluster->max_size = max_extent; 2272 - got_it: 2273 - ret = 0; 2274 - atomic_inc(&block_group->count); 2275 - list_add_tail(&cluster->block_group_list, &block_group->cluster_list); 2276 - cluster->block_group = block_group; 2277 out: 2278 spin_unlock(&cluster->lock); 2279 spin_unlock(&block_group->tree_lock); ··· 2174 spin_lock_init(&cluster->refill_lock); 2175 cluster->root = RB_ROOT; 2176 cluster->max_size = 0; 2177 - cluster->points_to_bitmap = false; 2178 INIT_LIST_HEAD(&cluster->block_group_list); 2179 cluster->block_group = NULL; 2180 } 2181
··· 393 break; 394 395 need_loop = 1; 396 + e = kmem_cache_zalloc(btrfs_free_space_cachep, 397 + GFP_NOFS); 398 if (!e) { 399 kunmap(page); 400 unlock_page(page); ··· 405 e->bytes = le64_to_cpu(entry->bytes); 406 if (!e->bytes) { 407 kunmap(page); 408 + kmem_cache_free(btrfs_free_space_cachep, e); 409 unlock_page(page); 410 page_cache_release(page); 411 goto free_cache; ··· 420 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); 421 if (!e->bitmap) { 422 kunmap(page); 423 + kmem_cache_free( 424 + btrfs_free_space_cachep, e); 425 unlock_page(page); 426 page_cache_release(page); 427 goto free_cache; ··· 1187 { 1188 unlink_free_space(block_group, bitmap_info); 1189 kfree(bitmap_info->bitmap); 1190 + kmem_cache_free(btrfs_free_space_cachep, bitmap_info); 1191 block_group->total_bitmaps--; 1192 recalculate_thresholds(block_group); 1193 } ··· 1285 * If we are below the extents threshold then we can add this as an 1286 * extent, and don't have to deal with the bitmap 1287 */ 1288 + if (block_group->free_extents < block_group->extents_thresh) { 1289 + /* 1290 + * If this block group has some small extents we don't want to 1291 + * use up all of our free slots in the cache with them, we want 1292 + * to reserve them to larger extents, however if we have plent 1293 + * of cache left then go ahead an dadd them, no sense in adding 1294 + * the overhead of a bitmap if we don't have to. 1295 + */ 1296 + if (info->bytes <= block_group->sectorsize * 4) { 1297 + if (block_group->free_extents * 2 <= 1298 + block_group->extents_thresh) 1299 + return 0; 1300 + } else { 1301 + return 0; 1302 + } 1303 + } 1304 1305 /* 1306 * some block groups are so tiny they can't be enveloped by a bitmap, so ··· 1342 1343 /* no pre-allocated info, allocate a new one */ 1344 if (!info) { 1345 + info = kmem_cache_zalloc(btrfs_free_space_cachep, 1346 + GFP_NOFS); 1347 if (!info) { 1348 spin_lock(&block_group->tree_lock); 1349 ret = -ENOMEM; ··· 1365 if (info) { 1366 if (info->bitmap) 1367 kfree(info->bitmap); 1368 + kmem_cache_free(btrfs_free_space_cachep, info); 1369 } 1370 1371 return ret; ··· 1398 else 1399 __unlink_free_space(block_group, right_info); 1400 info->bytes += right_info->bytes; 1401 + kmem_cache_free(btrfs_free_space_cachep, right_info); 1402 merged = true; 1403 } 1404 ··· 1410 __unlink_free_space(block_group, left_info); 1411 info->offset = left_info->offset; 1412 info->bytes += left_info->bytes; 1413 + kmem_cache_free(btrfs_free_space_cachep, left_info); 1414 merged = true; 1415 } 1416 ··· 1423 struct btrfs_free_space *info; 1424 int ret = 0; 1425 1426 + info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); 1427 if (!info) 1428 return -ENOMEM; 1429 ··· 1450 link: 1451 ret = link_free_space(block_group, info); 1452 if (ret) 1453 + kmem_cache_free(btrfs_free_space_cachep, info); 1454 out: 1455 spin_unlock(&block_group->tree_lock); 1456 ··· 1520 kfree(info->bitmap); 1521 block_group->total_bitmaps--; 1522 } 1523 + kmem_cache_free(btrfs_free_space_cachep, info); 1524 goto out_lock; 1525 } 1526 ··· 1556 /* the hole we're creating ends at the end 1557 * of the info struct, just free the info 1558 */ 1559 + kmem_cache_free(btrfs_free_space_cachep, info); 1560 } 1561 spin_unlock(&block_group->tree_lock); 1562 ··· 1629 { 1630 struct btrfs_free_space *entry; 1631 struct rb_node *node; 1632 1633 spin_lock(&cluster->lock); 1634 if (cluster->block_group != block_group) 1635 goto out; 1636 1637 cluster->block_group = NULL; 1638 cluster->window_start = 0; 1639 list_del_init(&cluster->block_group_list); 1640 1641 node = rb_first(&cluster->root); 1642 while (node) { 1643 + bool bitmap; 1644 + 1645 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1646 node = rb_next(&entry->offset_index); 1647 rb_erase(&entry->offset_index, &cluster->root); 1648 + 1649 + bitmap = (entry->bitmap != NULL); 1650 + if (!bitmap) 1651 + try_merge_free_space(block_group, entry, false); 1652 tree_insert_offset(&block_group->free_space_offset, 1653 + entry->offset, &entry->offset_index, bitmap); 1654 } 1655 cluster->root = RB_ROOT; 1656 ··· 1689 unlink_free_space(block_group, info); 1690 if (info->bitmap) 1691 kfree(info->bitmap); 1692 + kmem_cache_free(btrfs_free_space_cachep, info); 1693 if (need_resched()) { 1694 spin_unlock(&block_group->tree_lock); 1695 cond_resched(); ··· 1722 entry->offset += bytes; 1723 entry->bytes -= bytes; 1724 if (!entry->bytes) 1725 + kmem_cache_free(btrfs_free_space_cachep, entry); 1726 else 1727 link_free_space(block_group, entry); 1728 } ··· 1775 1776 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, 1777 struct btrfs_free_cluster *cluster, 1778 + struct btrfs_free_space *entry, 1779 u64 bytes, u64 min_start) 1780 { 1781 int err; 1782 u64 search_start = cluster->window_start; 1783 u64 search_bytes = bytes; 1784 u64 ret = 0; 1785 1786 search_start = min_start; 1787 search_bytes = bytes; ··· 1810 err = search_bitmap(block_group, entry, &search_start, 1811 &search_bytes); 1812 if (err) 1813 + return 0; 1814 1815 ret = search_start; 1816 bitmap_clear_bits(block_group, entry, ret, bytes); 1817 1818 return ret; 1819 } ··· 1836 struct rb_node *node; 1837 u64 ret = 0; 1838 1839 spin_lock(&cluster->lock); 1840 if (bytes > cluster->max_size) 1841 goto out; ··· 1852 goto out; 1853 1854 entry = rb_entry(node, struct btrfs_free_space, offset_index); 1855 while(1) { 1856 + if (entry->bytes < bytes || 1857 + (!entry->bitmap && entry->offset < min_start)) { 1858 struct rb_node *node; 1859 1860 node = rb_next(&entry->offset_index); ··· 1864 offset_index); 1865 continue; 1866 } 1867 1868 + if (entry->bitmap) { 1869 + ret = btrfs_alloc_from_bitmap(block_group, 1870 + cluster, entry, bytes, 1871 + min_start); 1872 + if (ret == 0) { 1873 + struct rb_node *node; 1874 + node = rb_next(&entry->offset_index); 1875 + if (!node) 1876 + break; 1877 + entry = rb_entry(node, struct btrfs_free_space, 1878 + offset_index); 1879 + continue; 1880 + } 1881 + } else { 1882 + 1883 + ret = entry->offset; 1884 + 1885 + entry->offset += bytes; 1886 + entry->bytes -= bytes; 1887 + } 1888 1889 if (entry->bytes == 0) 1890 rb_erase(&entry->offset_index, &cluster->root); ··· 1884 block_group->free_space -= bytes; 1885 if (entry->bytes == 0) { 1886 block_group->free_extents--; 1887 + if (entry->bitmap) { 1888 + kfree(entry->bitmap); 1889 + block_group->total_bitmaps--; 1890 + recalculate_thresholds(block_group); 1891 + } 1892 + kmem_cache_free(btrfs_free_space_cachep, entry); 1893 } 1894 1895 spin_unlock(&block_group->tree_lock); ··· 1904 unsigned long found_bits; 1905 unsigned long start = 0; 1906 unsigned long total_found = 0; 1907 + int ret; 1908 bool found = false; 1909 1910 i = offset_to_bit(entry->offset, block_group->sectorsize, 1911 max_t(u64, offset, entry->offset)); 1912 + search_bits = bytes_to_bits(bytes, block_group->sectorsize); 1913 + total_bits = bytes_to_bits(min_bytes, block_group->sectorsize); 1914 1915 again: 1916 found_bits = 0; ··· 1926 } 1927 1928 if (!found_bits) 1929 + return -ENOSPC; 1930 1931 if (!found) { 1932 start = i; ··· 1950 1951 cluster->window_start = start * block_group->sectorsize + 1952 entry->offset; 1953 + rb_erase(&entry->offset_index, &block_group->free_space_offset); 1954 + ret = tree_insert_offset(&cluster->root, entry->offset, 1955 + &entry->offset_index, 1); 1956 + BUG_ON(ret); 1957 1958 return 0; 1959 + } 1960 + 1961 + /* 1962 + * This searches the block group for just extents to fill the cluster with. 1963 + */ 1964 + static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, 1965 + struct btrfs_free_cluster *cluster, 1966 + u64 offset, u64 bytes, u64 min_bytes) 1967 + { 1968 + struct btrfs_free_space *first = NULL; 1969 + struct btrfs_free_space *entry = NULL; 1970 + struct btrfs_free_space *prev = NULL; 1971 + struct btrfs_free_space *last; 1972 + struct rb_node *node; 1973 + u64 window_start; 1974 + u64 window_free; 1975 + u64 max_extent; 1976 + u64 max_gap = 128 * 1024; 1977 + 1978 + entry = tree_search_offset(block_group, offset, 0, 1); 1979 + if (!entry) 1980 + return -ENOSPC; 1981 + 1982 + /* 1983 + * We don't want bitmaps, so just move along until we find a normal 1984 + * extent entry. 1985 + */ 1986 + while (entry->bitmap) { 1987 + node = rb_next(&entry->offset_index); 1988 + if (!node) 1989 + return -ENOSPC; 1990 + entry = rb_entry(node, struct btrfs_free_space, offset_index); 1991 + } 1992 + 1993 + window_start = entry->offset; 1994 + window_free = entry->bytes; 1995 + max_extent = entry->bytes; 1996 + first = entry; 1997 + last = entry; 1998 + prev = entry; 1999 + 2000 + while (window_free <= min_bytes) { 2001 + node = rb_next(&entry->offset_index); 2002 + if (!node) 2003 + return -ENOSPC; 2004 + entry = rb_entry(node, struct btrfs_free_space, offset_index); 2005 + 2006 + if (entry->bitmap) 2007 + continue; 2008 + /* 2009 + * we haven't filled the empty size and the window is 2010 + * very large. reset and try again 2011 + */ 2012 + if (entry->offset - (prev->offset + prev->bytes) > max_gap || 2013 + entry->offset - window_start > (min_bytes * 2)) { 2014 + first = entry; 2015 + window_start = entry->offset; 2016 + window_free = entry->bytes; 2017 + last = entry; 2018 + max_extent = entry->bytes; 2019 + } else { 2020 + last = entry; 2021 + window_free += entry->bytes; 2022 + if (entry->bytes > max_extent) 2023 + max_extent = entry->bytes; 2024 + } 2025 + prev = entry; 2026 + } 2027 + 2028 + cluster->window_start = first->offset; 2029 + 2030 + node = &first->offset_index; 2031 + 2032 + /* 2033 + * now we've found our entries, pull them out of the free space 2034 + * cache and put them into the cluster rbtree 2035 + */ 2036 + do { 2037 + int ret; 2038 + 2039 + entry = rb_entry(node, struct btrfs_free_space, offset_index); 2040 + node = rb_next(&entry->offset_index); 2041 + if (entry->bitmap) 2042 + continue; 2043 + 2044 + rb_erase(&entry->offset_index, &block_group->free_space_offset); 2045 + ret = tree_insert_offset(&cluster->root, entry->offset, 2046 + &entry->offset_index, 0); 2047 + BUG_ON(ret); 2048 + } while (node && entry != last); 2049 + 2050 + cluster->max_size = max_extent; 2051 + 2052 + return 0; 2053 + } 2054 + 2055 + /* 2056 + * This specifically looks for bitmaps that may work in the cluster, we assume 2057 + * that we have already failed to find extents that will work. 2058 + */ 2059 + static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, 2060 + struct btrfs_free_cluster *cluster, 2061 + u64 offset, u64 bytes, u64 min_bytes) 2062 + { 2063 + struct btrfs_free_space *entry; 2064 + struct rb_node *node; 2065 + int ret = -ENOSPC; 2066 + 2067 + if (block_group->total_bitmaps == 0) 2068 + return -ENOSPC; 2069 + 2070 + entry = tree_search_offset(block_group, 2071 + offset_to_bitmap(block_group, offset), 2072 + 0, 1); 2073 + if (!entry) 2074 + return -ENOSPC; 2075 + 2076 + node = &entry->offset_index; 2077 + do { 2078 + entry = rb_entry(node, struct btrfs_free_space, offset_index); 2079 + node = rb_next(&entry->offset_index); 2080 + if (!entry->bitmap) 2081 + continue; 2082 + if (entry->bytes < min_bytes) 2083 + continue; 2084 + ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, 2085 + bytes, min_bytes); 2086 + } while (ret && node); 2087 + 2088 + return ret; 2089 } 2090 2091 /* ··· 1969 struct btrfs_free_cluster *cluster, 1970 u64 offset, u64 bytes, u64 empty_size) 1971 { 1972 u64 min_bytes; 1973 int ret; 1974 1975 /* for metadata, allow allocates with more holes */ ··· 1997 min_bytes = max(bytes, (bytes + empty_size) >> 2); 1998 1999 spin_lock(&block_group->tree_lock); 2000 + 2001 + /* 2002 + * If we know we don't have enough space to make a cluster don't even 2003 + * bother doing all the work to try and find one. 2004 + */ 2005 + if (block_group->free_space < min_bytes) { 2006 + spin_unlock(&block_group->tree_lock); 2007 + return -ENOSPC; 2008 + } 2009 + 2010 spin_lock(&cluster->lock); 2011 2012 /* someone already found a cluster, hooray */ ··· 2004 ret = 0; 2005 goto out; 2006 } 2007 + 2008 + ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes, 2009 + min_bytes); 2010 + if (ret) 2011 + ret = setup_cluster_bitmap(block_group, cluster, offset, 2012 + bytes, min_bytes); 2013 + 2014 + if (!ret) { 2015 + atomic_inc(&block_group->count); 2016 + list_add_tail(&cluster->block_group_list, 2017 + &block_group->cluster_list); 2018 + cluster->block_group = block_group; 2019 } 2020 out: 2021 spin_unlock(&cluster->lock); 2022 spin_unlock(&block_group->tree_lock); ··· 2149 spin_lock_init(&cluster->refill_lock); 2150 cluster->root = RB_ROOT; 2151 cluster->max_size = 0; 2152 INIT_LIST_HEAD(&cluster->block_group_list); 2153 cluster->block_group = NULL; 2154 } 2155 2156 + int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, 2157 + u64 *trimmed, u64 start, u64 end, u64 minlen) 2158 + { 2159 + struct btrfs_free_space *entry = NULL; 2160 + struct btrfs_fs_info *fs_info = block_group->fs_info; 2161 + u64 bytes = 0; 2162 + u64 actually_trimmed; 2163 + int ret = 0; 2164 + 2165 + *trimmed = 0; 2166 + 2167 + while (start < end) { 2168 + spin_lock(&block_group->tree_lock); 2169 + 2170 + if (block_group->free_space < minlen) { 2171 + spin_unlock(&block_group->tree_lock); 2172 + break; 2173 + } 2174 + 2175 + entry = tree_search_offset(block_group, start, 0, 1); 2176 + if (!entry) 2177 + entry = tree_search_offset(block_group, 2178 + offset_to_bitmap(block_group, 2179 + start), 2180 + 1, 1); 2181 + 2182 + if (!entry || entry->offset >= end) { 2183 + spin_unlock(&block_group->tree_lock); 2184 + break; 2185 + } 2186 + 2187 + if (entry->bitmap) { 2188 + ret = search_bitmap(block_group, entry, &start, &bytes); 2189 + if (!ret) { 2190 + if (start >= end) { 2191 + spin_unlock(&block_group->tree_lock); 2192 + break; 2193 + } 2194 + bytes = min(bytes, end - start); 2195 + bitmap_clear_bits(block_group, entry, 2196 + start, bytes); 2197 + if (entry->bytes == 0) 2198 + free_bitmap(block_group, entry); 2199 + } else { 2200 + start = entry->offset + BITS_PER_BITMAP * 2201 + block_group->sectorsize; 2202 + spin_unlock(&block_group->tree_lock); 2203 + ret = 0; 2204 + continue; 2205 + } 2206 + } else { 2207 + start = entry->offset; 2208 + bytes = min(entry->bytes, end - start); 2209 + unlink_free_space(block_group, entry); 2210 + kfree(entry); 2211 + } 2212 + 2213 + spin_unlock(&block_group->tree_lock); 2214 + 2215 + if (bytes >= minlen) { 2216 + int update_ret; 2217 + update_ret = btrfs_update_reserved_bytes(block_group, 2218 + bytes, 1, 1); 2219 + 2220 + ret = btrfs_error_discard_extent(fs_info->extent_root, 2221 + start, 2222 + bytes, 2223 + &actually_trimmed); 2224 + 2225 + btrfs_add_free_space(block_group, 2226 + start, bytes); 2227 + if (!update_ret) 2228 + btrfs_update_reserved_bytes(block_group, 2229 + bytes, 0, 1); 2230 + 2231 + if (ret) 2232 + break; 2233 + *trimmed += actually_trimmed; 2234 + } 2235 + start += bytes; 2236 + bytes = 0; 2237 + 2238 + if (fatal_signal_pending(current)) { 2239 + ret = -ERESTARTSYS; 2240 + break; 2241 + } 2242 + 2243 + cond_resched(); 2244 + } 2245 + 2246 + return ret; 2247 + }
+2
fs/btrfs/free-space-cache.h
··· 68 int btrfs_return_cluster_to_free_space( 69 struct btrfs_block_group_cache *block_group, 70 struct btrfs_free_cluster *cluster); 71 #endif
··· 68 int btrfs_return_cluster_to_free_space( 69 struct btrfs_block_group_cache *block_group, 70 struct btrfs_free_cluster *cluster); 71 + int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, 72 + u64 *trimmed, u64 start, u64 end, u64 minlen); 73 #endif
+2 -1
fs/btrfs/inode-map.c
··· 30 int slot; 31 32 path = btrfs_alloc_path(); 33 - BUG_ON(!path); 34 35 search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 36 search_key.type = -1;
··· 30 int slot; 31 32 path = btrfs_alloc_path(); 33 + if (!path) 34 + return -ENOMEM; 35 36 search_key.objectid = BTRFS_LAST_FREE_OBJECTID; 37 search_key.type = -1;
+237 -129
fs/btrfs/inode.c
··· 50 #include "tree-log.h" 51 #include "compression.h" 52 #include "locking.h" 53 54 struct btrfs_iget_args { 55 u64 ino; ··· 71 struct kmem_cache *btrfs_trans_handle_cachep; 72 struct kmem_cache *btrfs_transaction_cachep; 73 struct kmem_cache *btrfs_path_cachep; 74 75 #define S_SHIFT 12 76 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { ··· 84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 85 }; 86 87 - static void btrfs_truncate(struct inode *inode); 88 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); 89 static noinline int cow_file_range(struct inode *inode, 90 struct page *locked_page, ··· 291 struct async_extent *async_extent; 292 293 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); 294 async_extent->start = start; 295 async_extent->ram_size = ram_size; 296 async_extent->compressed_size = compressed_size; ··· 386 */ 387 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && 388 (btrfs_test_opt(root, COMPRESS) || 389 - (BTRFS_I(inode)->force_compress))) { 390 WARN_ON(pages); 391 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 392 393 if (BTRFS_I(inode)->force_compress) 394 compress_type = BTRFS_I(inode)->force_compress; ··· 1260 ret = run_delalloc_nocow(inode, locked_page, start, end, 1261 page_started, 0, nr_written); 1262 else if (!btrfs_test_opt(root, COMPRESS) && 1263 - !(BTRFS_I(inode)->force_compress)) 1264 ret = cow_file_range(inode, locked_page, start, end, 1265 page_started, nr_written, 1); 1266 else ··· 1468 if (bio_flags & EXTENT_BIO_COMPRESSED) { 1469 return btrfs_submit_compressed_read(inode, bio, 1470 mirror_num, bio_flags); 1471 - } else if (!skip_sum) 1472 - btrfs_lookup_bio_sums(root, inode, bio, NULL); 1473 goto mapit; 1474 } else if (!skip_sum) { 1475 /* csum items have already been cloned */ ··· 1795 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1796 struct extent_state *state, int uptodate) 1797 { 1798 ClearPagePrivate2(page); 1799 return btrfs_finish_ordered_io(page->mapping->host, start, end); 1800 } ··· 1907 else 1908 rw = READ; 1909 1910 - BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, 1911 failrec->last_mirror, 1912 failrec->bio_flags, 0); 1913 - return 0; 1914 } 1915 1916 /* ··· 2294 * this cleans up any orphans that may be left on the list from the last use 2295 * of this root. 2296 */ 2297 - void btrfs_orphan_cleanup(struct btrfs_root *root) 2298 { 2299 struct btrfs_path *path; 2300 struct extent_buffer *leaf; ··· 2304 int ret = 0, nr_unlink = 0, nr_truncate = 0; 2305 2306 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) 2307 - return; 2308 2309 path = btrfs_alloc_path(); 2310 - BUG_ON(!path); 2311 path->reada = -1; 2312 2313 key.objectid = BTRFS_ORPHAN_OBJECTID; ··· 2319 2320 while (1) { 2321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2322 - if (ret < 0) { 2323 - printk(KERN_ERR "Error searching slot for orphan: %d" 2324 - "\n", ret); 2325 - break; 2326 - } 2327 2328 /* 2329 * if ret == 0 means we found what we were searching for, which ··· 2328 * find the key and see if we have stuff that matches 2329 */ 2330 if (ret > 0) { 2331 if (path->slots[0] == 0) 2332 break; 2333 path->slots[0]--; ··· 2356 found_key.type = BTRFS_INODE_ITEM_KEY; 2357 found_key.offset = 0; 2358 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); 2359 - BUG_ON(IS_ERR(inode)); 2360 2361 /* 2362 * add this inode to the orphan list so btrfs_orphan_del does ··· 2377 */ 2378 if (is_bad_inode(inode)) { 2379 trans = btrfs_start_transaction(root, 0); 2380 - BUG_ON(IS_ERR(trans)); 2381 btrfs_orphan_del(trans, inode); 2382 btrfs_end_transaction(trans, root); 2383 iput(inode); ··· 2389 2390 /* if we have links, this was a truncate, lets do that */ 2391 if (inode->i_nlink) { 2392 nr_truncate++; 2393 - btrfs_truncate(inode); 2394 } else { 2395 nr_unlink++; 2396 } 2397 2398 /* this will do delete_inode and everything for us */ 2399 iput(inode); 2400 } 2401 - btrfs_free_path(path); 2402 - 2403 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; 2404 2405 if (root->orphan_block_rsv) ··· 2413 2414 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2415 trans = btrfs_join_transaction(root, 1); 2416 - BUG_ON(IS_ERR(trans)); 2417 - btrfs_end_transaction(trans, root); 2418 } 2419 2420 if (nr_unlink) 2421 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); 2422 if (nr_truncate) 2423 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); 2424 } 2425 2426 /* ··· 2537 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 2538 2539 alloc_group_block = btrfs_inode_block_group(leaf, inode_item); 2540 2541 /* 2542 * try to precache a NULL acl entry for files that don't have ··· 2667 * recovery code. It remove a link in a directory with a given name, and 2668 * also drops the back refs in the inode to the directory 2669 */ 2670 - int btrfs_unlink_inode(struct btrfs_trans_handle *trans, 2671 - struct btrfs_root *root, 2672 - struct inode *dir, struct inode *inode, 2673 - const char *name, int name_len) 2674 { 2675 struct btrfs_path *path; 2676 int ret = 0; ··· 2742 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 2743 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; 2744 btrfs_update_inode(trans, root, dir); 2745 - btrfs_drop_nlink(inode); 2746 - ret = btrfs_update_inode(trans, root, inode); 2747 out: 2748 return ret; 2749 } 2750 2751 /* helper to check if there is any shared block in the path */ 2752 static int check_path_shared(struct btrfs_root *root, ··· 3582 return ret; 3583 } 3584 3585 - int btrfs_cont_expand(struct inode *inode, loff_t size) 3586 { 3587 struct btrfs_trans_handle *trans; 3588 struct btrfs_root *root = BTRFS_I(inode)->root; ··· 3596 struct extent_map *em = NULL; 3597 struct extent_state *cached_state = NULL; 3598 u64 mask = root->sectorsize - 1; 3599 - u64 hole_start = (inode->i_size + mask) & ~mask; 3600 u64 block_end = (size + mask) & ~mask; 3601 u64 last_byte; 3602 u64 cur_offset; ··· 3641 err = btrfs_drop_extents(trans, inode, cur_offset, 3642 cur_offset + hole_size, 3643 &hint_byte, 1); 3644 - BUG_ON(err); 3645 3646 err = btrfs_insert_file_extent(trans, root, 3647 inode->i_ino, cur_offset, 0, 3648 0, hole_size, 0, hole_size, 3649 0, 0, 0); 3650 - BUG_ON(err); 3651 3652 btrfs_drop_extent_cache(inode, hole_start, 3653 last_byte - 1, 0); ··· 3669 return err; 3670 } 3671 3672 - static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) 3673 { 3674 - struct btrfs_root *root = BTRFS_I(inode)->root; 3675 - struct btrfs_trans_handle *trans; 3676 - unsigned long nr; 3677 int ret; 3678 3679 - if (attr->ia_size == inode->i_size) 3680 return 0; 3681 3682 - if (attr->ia_size > inode->i_size) { 3683 - unsigned long limit; 3684 - limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 3685 - if (attr->ia_size > inode->i_sb->s_maxbytes) 3686 - return -EFBIG; 3687 - if (limit != RLIM_INFINITY && attr->ia_size > limit) { 3688 - send_sig(SIGXFSZ, current, 0); 3689 - return -EFBIG; 3690 - } 3691 - } 3692 - 3693 - trans = btrfs_start_transaction(root, 5); 3694 - if (IS_ERR(trans)) 3695 - return PTR_ERR(trans); 3696 - 3697 - btrfs_set_trans_block_group(trans, inode); 3698 - 3699 - ret = btrfs_orphan_add(trans, inode); 3700 - BUG_ON(ret); 3701 - 3702 - nr = trans->blocks_used; 3703 - btrfs_end_transaction(trans, root); 3704 - btrfs_btree_balance_dirty(root, nr); 3705 - 3706 - if (attr->ia_size > inode->i_size) { 3707 - ret = btrfs_cont_expand(inode, attr->ia_size); 3708 if (ret) { 3709 - btrfs_truncate(inode); 3710 return ret; 3711 } 3712 3713 - i_size_write(inode, attr->ia_size); 3714 - btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 3715 3716 - trans = btrfs_start_transaction(root, 0); 3717 - BUG_ON(IS_ERR(trans)); 3718 - btrfs_set_trans_block_group(trans, inode); 3719 - trans->block_rsv = root->orphan_block_rsv; 3720 - BUG_ON(!trans->block_rsv); 3721 3722 - ret = btrfs_update_inode(trans, root, inode); 3723 - BUG_ON(ret); 3724 - if (inode->i_nlink > 0) { 3725 - ret = btrfs_orphan_del(trans, inode); 3726 - BUG_ON(ret); 3727 - } 3728 - nr = trans->blocks_used; 3729 - btrfs_end_transaction(trans, root); 3730 - btrfs_btree_balance_dirty(root, nr); 3731 - return 0; 3732 } 3733 3734 - /* 3735 - * We're truncating a file that used to have good data down to 3736 - * zero. Make sure it gets into the ordered flush list so that 3737 - * any new writes get down to disk quickly. 3738 - */ 3739 - if (attr->ia_size == 0) 3740 - BTRFS_I(inode)->ordered_data_close = 1; 3741 - 3742 - /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3743 - ret = vmtruncate(inode, attr->ia_size); 3744 - BUG_ON(ret); 3745 - 3746 - return 0; 3747 } 3748 3749 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) ··· 3720 return err; 3721 3722 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3723 - err = btrfs_setattr_size(inode, attr); 3724 if (err) 3725 return err; 3726 } ··· 3742 struct btrfs_root *root = BTRFS_I(inode)->root; 3743 unsigned long nr; 3744 int ret; 3745 3746 truncate_inode_pages(&inode->i_data, 0); 3747 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || ··· 4087 BTRFS_I(inode)->root = root; 4088 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); 4089 btrfs_read_locked_inode(inode); 4090 - 4091 inode_tree_add(inode); 4092 unlock_new_inode(inode); 4093 if (new) ··· 4161 if (!IS_ERR(inode) && root != sub_root) { 4162 down_read(&root->fs_info->cleanup_work_sem); 4163 if (!(inode->i_sb->s_flags & MS_RDONLY)) 4164 - btrfs_orphan_cleanup(sub_root); 4165 up_read(&root->fs_info->cleanup_work_sem); 4166 } 4167 4168 return inode; ··· 4297 4298 while (di_cur < di_total) { 4299 struct btrfs_key location; 4300 4301 name_len = btrfs_dir_name_len(leaf, di); 4302 if (name_len <= sizeof(tmp_name)) { ··· 4536 return ERR_PTR(-ENOMEM); 4537 4538 if (dir) { 4539 ret = btrfs_set_inode_index(dir, index); 4540 if (ret) { 4541 iput(inode); ··· 4606 if ((mode & S_IFREG)) { 4607 if (btrfs_test_opt(root, NODATASUM)) 4608 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 4609 - if (btrfs_test_opt(root, NODATACOW)) 4610 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 4611 } 4612 4613 insert_inode_hash(inode); 4614 inode_tree_add(inode); 4615 return inode; 4616 fail: 4617 if (dir) ··· 4834 4835 /* do not allow sys_link's with other subvols of the same device */ 4836 if (root->objectid != BTRFS_I(inode)->root->objectid) 4837 - return -EPERM; 4838 4839 btrfs_inc_nlink(inode); 4840 inode->i_ctime = CURRENT_TIME; ··· 5293 } 5294 write_unlock(&em_tree->lock); 5295 out: 5296 if (path) 5297 btrfs_free_path(path); 5298 if (trans) { ··· 5779 5780 kfree(dip->csums); 5781 kfree(dip); 5782 dio_end_io(bio, err); 5783 } 5784 ··· 5884 5885 kfree(dip->csums); 5886 kfree(dip); 5887 dio_end_io(bio, err); 5888 } 5889 ··· 5961 __btrfs_submit_bio_start_direct_io, 5962 __btrfs_submit_bio_done); 5963 goto err; 5964 - } else if (!skip_sum) 5965 - btrfs_lookup_bio_sums_dio(root, inode, bio, 5966 file_offset, csums); 5967 5968 ret = btrfs_map_bio(root, rw, bio, 0, 1); 5969 err: ··· 5990 int nr_pages = 0; 5991 u32 *csums = dip->csums; 5992 int ret = 0; 5993 5994 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); 5995 if (!bio) ··· 6027 goto out_err; 6028 } 6029 6030 - if (!skip_sum) 6031 csums = csums + nr_pages; 6032 start_sector += submit_len >> 9; 6033 file_offset += submit_len; ··· 6096 } 6097 dip->csums = NULL; 6098 6099 - if (!skip_sum) { 6100 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); 6101 if (!dip->csums) { 6102 kfree(dip); ··· 6519 return ret; 6520 } 6521 6522 - static void btrfs_truncate(struct inode *inode) 6523 { 6524 struct btrfs_root *root = BTRFS_I(inode)->root; 6525 int ret; 6526 struct btrfs_trans_handle *trans; 6527 unsigned long nr; 6528 u64 mask = root->sectorsize - 1; 6529 6530 - if (!S_ISREG(inode->i_mode)) { 6531 - WARN_ON(1); 6532 - return; 6533 - } 6534 - 6535 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); 6536 if (ret) 6537 - return; 6538 6539 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 6540 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 6541 6542 trans = btrfs_start_transaction(root, 0); 6543 - BUG_ON(IS_ERR(trans)); 6544 btrfs_set_trans_block_group(trans, inode); 6545 trans->block_rsv = root->orphan_block_rsv; 6546 ··· 6581 while (1) { 6582 if (!trans) { 6583 trans = btrfs_start_transaction(root, 0); 6584 - BUG_ON(IS_ERR(trans)); 6585 btrfs_set_trans_block_group(trans, inode); 6586 trans->block_rsv = root->orphan_block_rsv; 6587 } 6588 6589 ret = btrfs_block_rsv_check(trans, root, 6590 root->orphan_block_rsv, 0, 5); 6591 - if (ret) { 6592 - BUG_ON(ret != -EAGAIN); 6593 ret = btrfs_commit_transaction(trans, root); 6594 - BUG_ON(ret); 6595 trans = NULL; 6596 continue; 6597 } 6598 6599 ret = btrfs_truncate_inode_items(trans, root, inode, 6600 inode->i_size, 6601 BTRFS_EXTENT_DATA_KEY); 6602 - if (ret != -EAGAIN) 6603 break; 6604 6605 ret = btrfs_update_inode(trans, root, inode); 6606 - BUG_ON(ret); 6607 6608 nr = trans->blocks_used; 6609 btrfs_end_transaction(trans, root); ··· 6622 6623 if (ret == 0 && inode->i_nlink > 0) { 6624 ret = btrfs_orphan_del(trans, inode); 6625 - BUG_ON(ret); 6626 } 6627 6628 ret = btrfs_update_inode(trans, root, inode); 6629 - BUG_ON(ret); 6630 6631 nr = trans->blocks_used; 6632 ret = btrfs_end_transaction_throttle(trans, root); 6633 - BUG_ON(ret); 6634 btrfs_btree_balance_dirty(root, nr); 6635 } 6636 6637 /* ··· 6709 ei->index_cnt = (u64)-1; 6710 ei->last_unlink_trans = 0; 6711 6712 - spin_lock_init(&ei->accounting_lock); 6713 atomic_set(&ei->outstanding_extents, 0); 6714 - ei->reserved_extents = 0; 6715 6716 ei->ordered_data_close = 0; 6717 ei->orphan_meta_reserved = 0; ··· 6746 WARN_ON(!list_empty(&inode->i_dentry)); 6747 WARN_ON(inode->i_data.nrpages); 6748 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); 6749 - WARN_ON(BTRFS_I(inode)->reserved_extents); 6750 6751 /* 6752 * This can happen where we create an inode, but somebody else also ··· 6838 kmem_cache_destroy(btrfs_transaction_cachep); 6839 if (btrfs_path_cachep) 6840 kmem_cache_destroy(btrfs_path_cachep); 6841 } 6842 6843 int btrfs_init_cachep(void) ··· 6868 if (!btrfs_path_cachep) 6869 goto fail; 6870 6871 return 0; 6872 fail: 6873 btrfs_destroy_cachep(); ··· 6890 stat->blocks = (inode_get_bytes(inode) + 6891 BTRFS_I(inode)->delalloc_bytes) >> 9; 6892 return 0; 6893 } 6894 6895 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, ··· 7014 old_dentry->d_name.name, 7015 old_dentry->d_name.len); 7016 } else { 7017 - btrfs_inc_nlink(old_dentry->d_inode); 7018 - ret = btrfs_unlink_inode(trans, root, old_dir, 7019 - old_dentry->d_inode, 7020 - old_dentry->d_name.name, 7021 - old_dentry->d_name.len); 7022 } 7023 BUG_ON(ret); 7024 ··· 7045 BUG_ON(ret); 7046 } 7047 } 7048 7049 ret = btrfs_add_link(trans, new_dir, old_inode, 7050 new_dentry->d_name.name, ··· 7464 }; 7465 7466 static const struct inode_operations btrfs_file_inode_operations = { 7467 - .truncate = btrfs_truncate, 7468 .getattr = btrfs_getattr, 7469 .setattr = btrfs_setattr, 7470 .setxattr = btrfs_setxattr,
··· 50 #include "tree-log.h" 51 #include "compression.h" 52 #include "locking.h" 53 + #include "free-space-cache.h" 54 55 struct btrfs_iget_args { 56 u64 ino; ··· 70 struct kmem_cache *btrfs_trans_handle_cachep; 71 struct kmem_cache *btrfs_transaction_cachep; 72 struct kmem_cache *btrfs_path_cachep; 73 + struct kmem_cache *btrfs_free_space_cachep; 74 75 #define S_SHIFT 12 76 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { ··· 82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 83 }; 84 85 + static int btrfs_setsize(struct inode *inode, loff_t newsize); 86 + static int btrfs_truncate(struct inode *inode); 87 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); 88 static noinline int cow_file_range(struct inode *inode, 89 struct page *locked_page, ··· 288 struct async_extent *async_extent; 289 290 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); 291 + BUG_ON(!async_extent); 292 async_extent->start = start; 293 async_extent->ram_size = ram_size; 294 async_extent->compressed_size = compressed_size; ··· 382 */ 383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && 384 (btrfs_test_opt(root, COMPRESS) || 385 + (BTRFS_I(inode)->force_compress) || 386 + (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) { 387 WARN_ON(pages); 388 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 389 + BUG_ON(!pages); 390 391 if (BTRFS_I(inode)->force_compress) 392 compress_type = BTRFS_I(inode)->force_compress; ··· 1254 ret = run_delalloc_nocow(inode, locked_page, start, end, 1255 page_started, 0, nr_written); 1256 else if (!btrfs_test_opt(root, COMPRESS) && 1257 + !(BTRFS_I(inode)->force_compress) && 1258 + !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) 1259 ret = cow_file_range(inode, locked_page, start, end, 1260 page_started, nr_written, 1); 1261 else ··· 1461 if (bio_flags & EXTENT_BIO_COMPRESSED) { 1462 return btrfs_submit_compressed_read(inode, bio, 1463 mirror_num, bio_flags); 1464 + } else if (!skip_sum) { 1465 + ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); 1466 + if (ret) 1467 + return ret; 1468 + } 1469 goto mapit; 1470 } else if (!skip_sum) { 1471 /* csum items have already been cloned */ ··· 1785 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1786 struct extent_state *state, int uptodate) 1787 { 1788 + trace_btrfs_writepage_end_io_hook(page, start, end, uptodate); 1789 + 1790 ClearPagePrivate2(page); 1791 return btrfs_finish_ordered_io(page->mapping->host, start, end); 1792 } ··· 1895 else 1896 rw = READ; 1897 1898 + ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, 1899 failrec->last_mirror, 1900 failrec->bio_flags, 0); 1901 + return ret; 1902 } 1903 1904 /* ··· 2282 * this cleans up any orphans that may be left on the list from the last use 2283 * of this root. 2284 */ 2285 + int btrfs_orphan_cleanup(struct btrfs_root *root) 2286 { 2287 struct btrfs_path *path; 2288 struct extent_buffer *leaf; ··· 2292 int ret = 0, nr_unlink = 0, nr_truncate = 0; 2293 2294 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) 2295 + return 0; 2296 2297 path = btrfs_alloc_path(); 2298 + if (!path) { 2299 + ret = -ENOMEM; 2300 + goto out; 2301 + } 2302 path->reada = -1; 2303 2304 key.objectid = BTRFS_ORPHAN_OBJECTID; ··· 2304 2305 while (1) { 2306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2307 + if (ret < 0) 2308 + goto out; 2309 2310 /* 2311 * if ret == 0 means we found what we were searching for, which ··· 2316 * find the key and see if we have stuff that matches 2317 */ 2318 if (ret > 0) { 2319 + ret = 0; 2320 if (path->slots[0] == 0) 2321 break; 2322 path->slots[0]--; ··· 2343 found_key.type = BTRFS_INODE_ITEM_KEY; 2344 found_key.offset = 0; 2345 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); 2346 + if (IS_ERR(inode)) { 2347 + ret = PTR_ERR(inode); 2348 + goto out; 2349 + } 2350 2351 /* 2352 * add this inode to the orphan list so btrfs_orphan_del does ··· 2361 */ 2362 if (is_bad_inode(inode)) { 2363 trans = btrfs_start_transaction(root, 0); 2364 + if (IS_ERR(trans)) { 2365 + ret = PTR_ERR(trans); 2366 + goto out; 2367 + } 2368 btrfs_orphan_del(trans, inode); 2369 btrfs_end_transaction(trans, root); 2370 iput(inode); ··· 2370 2371 /* if we have links, this was a truncate, lets do that */ 2372 if (inode->i_nlink) { 2373 + if (!S_ISREG(inode->i_mode)) { 2374 + WARN_ON(1); 2375 + iput(inode); 2376 + continue; 2377 + } 2378 nr_truncate++; 2379 + ret = btrfs_truncate(inode); 2380 } else { 2381 nr_unlink++; 2382 } 2383 2384 /* this will do delete_inode and everything for us */ 2385 iput(inode); 2386 + if (ret) 2387 + goto out; 2388 } 2389 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; 2390 2391 if (root->orphan_block_rsv) ··· 2389 2390 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2391 trans = btrfs_join_transaction(root, 1); 2392 + if (!IS_ERR(trans)) 2393 + btrfs_end_transaction(trans, root); 2394 } 2395 2396 if (nr_unlink) 2397 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); 2398 if (nr_truncate) 2399 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); 2400 + 2401 + out: 2402 + if (ret) 2403 + printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret); 2404 + btrfs_free_path(path); 2405 + return ret; 2406 } 2407 2408 /* ··· 2507 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 2508 2509 alloc_group_block = btrfs_inode_block_group(leaf, inode_item); 2510 + if (location.objectid == BTRFS_FREE_SPACE_OBJECTID) 2511 + inode->i_mapping->flags &= ~__GFP_FS; 2512 2513 /* 2514 * try to precache a NULL acl entry for files that don't have ··· 2635 * recovery code. It remove a link in a directory with a given name, and 2636 * also drops the back refs in the inode to the directory 2637 */ 2638 + static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans, 2639 + struct btrfs_root *root, 2640 + struct inode *dir, struct inode *inode, 2641 + const char *name, int name_len) 2642 { 2643 struct btrfs_path *path; 2644 int ret = 0; ··· 2710 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 2711 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; 2712 btrfs_update_inode(trans, root, dir); 2713 out: 2714 return ret; 2715 } 2716 + 2717 + int btrfs_unlink_inode(struct btrfs_trans_handle *trans, 2718 + struct btrfs_root *root, 2719 + struct inode *dir, struct inode *inode, 2720 + const char *name, int name_len) 2721 + { 2722 + int ret; 2723 + ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len); 2724 + if (!ret) { 2725 + btrfs_drop_nlink(inode); 2726 + ret = btrfs_update_inode(trans, root, inode); 2727 + } 2728 + return ret; 2729 + } 2730 + 2731 2732 /* helper to check if there is any shared block in the path */ 2733 static int check_path_shared(struct btrfs_root *root, ··· 3537 return ret; 3538 } 3539 3540 + /* 3541 + * This function puts in dummy file extents for the area we're creating a hole 3542 + * for. So if we are truncating this file to a larger size we need to insert 3543 + * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for 3544 + * the range between oldsize and size 3545 + */ 3546 + int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) 3547 { 3548 struct btrfs_trans_handle *trans; 3549 struct btrfs_root *root = BTRFS_I(inode)->root; ··· 3545 struct extent_map *em = NULL; 3546 struct extent_state *cached_state = NULL; 3547 u64 mask = root->sectorsize - 1; 3548 + u64 hole_start = (oldsize + mask) & ~mask; 3549 u64 block_end = (size + mask) & ~mask; 3550 u64 last_byte; 3551 u64 cur_offset; ··· 3590 err = btrfs_drop_extents(trans, inode, cur_offset, 3591 cur_offset + hole_size, 3592 &hint_byte, 1); 3593 + if (err) 3594 + break; 3595 3596 err = btrfs_insert_file_extent(trans, root, 3597 inode->i_ino, cur_offset, 0, 3598 0, hole_size, 0, hole_size, 3599 0, 0, 0); 3600 + if (err) 3601 + break; 3602 3603 btrfs_drop_extent_cache(inode, hole_start, 3604 last_byte - 1, 0); ··· 3616 return err; 3617 } 3618 3619 + static int btrfs_setsize(struct inode *inode, loff_t newsize) 3620 { 3621 + loff_t oldsize = i_size_read(inode); 3622 int ret; 3623 3624 + if (newsize == oldsize) 3625 return 0; 3626 3627 + if (newsize > oldsize) { 3628 + i_size_write(inode, newsize); 3629 + btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); 3630 + truncate_pagecache(inode, oldsize, newsize); 3631 + ret = btrfs_cont_expand(inode, oldsize, newsize); 3632 if (ret) { 3633 + btrfs_setsize(inode, oldsize); 3634 return ret; 3635 } 3636 3637 + mark_inode_dirty(inode); 3638 + } else { 3639 3640 + /* 3641 + * We're truncating a file that used to have good data down to 3642 + * zero. Make sure it gets into the ordered flush list so that 3643 + * any new writes get down to disk quickly. 3644 + */ 3645 + if (newsize == 0) 3646 + BTRFS_I(inode)->ordered_data_close = 1; 3647 3648 + /* we don't support swapfiles, so vmtruncate shouldn't fail */ 3649 + truncate_setsize(inode, newsize); 3650 + ret = btrfs_truncate(inode); 3651 } 3652 3653 + return ret; 3654 } 3655 3656 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) ··· 3707 return err; 3708 3709 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3710 + err = btrfs_setsize(inode, attr->ia_size); 3711 if (err) 3712 return err; 3713 } ··· 3729 struct btrfs_root *root = BTRFS_I(inode)->root; 3730 unsigned long nr; 3731 int ret; 3732 + 3733 + trace_btrfs_inode_evict(inode); 3734 3735 truncate_inode_pages(&inode->i_data, 0); 3736 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || ··· 4072 BTRFS_I(inode)->root = root; 4073 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); 4074 btrfs_read_locked_inode(inode); 4075 inode_tree_add(inode); 4076 unlock_new_inode(inode); 4077 if (new) ··· 4147 if (!IS_ERR(inode) && root != sub_root) { 4148 down_read(&root->fs_info->cleanup_work_sem); 4149 if (!(inode->i_sb->s_flags & MS_RDONLY)) 4150 + ret = btrfs_orphan_cleanup(sub_root); 4151 up_read(&root->fs_info->cleanup_work_sem); 4152 + if (ret) 4153 + inode = ERR_PTR(ret); 4154 } 4155 4156 return inode; ··· 4281 4282 while (di_cur < di_total) { 4283 struct btrfs_key location; 4284 + 4285 + if (verify_dir_item(root, leaf, di)) 4286 + break; 4287 4288 name_len = btrfs_dir_name_len(leaf, di); 4289 if (name_len <= sizeof(tmp_name)) { ··· 4517 return ERR_PTR(-ENOMEM); 4518 4519 if (dir) { 4520 + trace_btrfs_inode_request(dir); 4521 + 4522 ret = btrfs_set_inode_index(dir, index); 4523 if (ret) { 4524 iput(inode); ··· 4585 if ((mode & S_IFREG)) { 4586 if (btrfs_test_opt(root, NODATASUM)) 4587 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 4588 + if (btrfs_test_opt(root, NODATACOW) || 4589 + (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW)) 4590 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 4591 } 4592 4593 insert_inode_hash(inode); 4594 inode_tree_add(inode); 4595 + 4596 + trace_btrfs_inode_new(inode); 4597 + 4598 return inode; 4599 fail: 4600 if (dir) ··· 4809 4810 /* do not allow sys_link's with other subvols of the same device */ 4811 if (root->objectid != BTRFS_I(inode)->root->objectid) 4812 + return -EXDEV; 4813 + 4814 + if (inode->i_nlink == ~0U) 4815 + return -EMLINK; 4816 4817 btrfs_inc_nlink(inode); 4818 inode->i_ctime = CURRENT_TIME; ··· 5265 } 5266 write_unlock(&em_tree->lock); 5267 out: 5268 + 5269 + trace_btrfs_get_extent(root, em); 5270 + 5271 if (path) 5272 btrfs_free_path(path); 5273 if (trans) { ··· 5748 5749 kfree(dip->csums); 5750 kfree(dip); 5751 + 5752 + /* If we had a csum failure make sure to clear the uptodate flag */ 5753 + if (err) 5754 + clear_bit(BIO_UPTODATE, &bio->bi_flags); 5755 dio_end_io(bio, err); 5756 } 5757 ··· 5849 5850 kfree(dip->csums); 5851 kfree(dip); 5852 + 5853 + /* If we had an error make sure to clear the uptodate flag */ 5854 + if (err) 5855 + clear_bit(BIO_UPTODATE, &bio->bi_flags); 5856 dio_end_io(bio, err); 5857 } 5858 ··· 5922 __btrfs_submit_bio_start_direct_io, 5923 __btrfs_submit_bio_done); 5924 goto err; 5925 + } else if (!skip_sum) { 5926 + ret = btrfs_lookup_bio_sums_dio(root, inode, bio, 5927 file_offset, csums); 5928 + if (ret) 5929 + goto err; 5930 + } 5931 5932 ret = btrfs_map_bio(root, rw, bio, 0, 1); 5933 err: ··· 5948 int nr_pages = 0; 5949 u32 *csums = dip->csums; 5950 int ret = 0; 5951 + int write = rw & REQ_WRITE; 5952 5953 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); 5954 if (!bio) ··· 5984 goto out_err; 5985 } 5986 5987 + /* Write's use the ordered csums */ 5988 + if (!write && !skip_sum) 5989 csums = csums + nr_pages; 5990 start_sector += submit_len >> 9; 5991 file_offset += submit_len; ··· 6052 } 6053 dip->csums = NULL; 6054 6055 + /* Write's use the ordered csum stuff, so we don't need dip->csums */ 6056 + if (!write && !skip_sum) { 6057 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); 6058 if (!dip->csums) { 6059 kfree(dip); ··· 6474 return ret; 6475 } 6476 6477 + static int btrfs_truncate(struct inode *inode) 6478 { 6479 struct btrfs_root *root = BTRFS_I(inode)->root; 6480 int ret; 6481 + int err = 0; 6482 struct btrfs_trans_handle *trans; 6483 unsigned long nr; 6484 u64 mask = root->sectorsize - 1; 6485 6486 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); 6487 if (ret) 6488 + return ret; 6489 6490 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 6491 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 6492 6493 + trans = btrfs_start_transaction(root, 5); 6494 + if (IS_ERR(trans)) 6495 + return PTR_ERR(trans); 6496 + 6497 + btrfs_set_trans_block_group(trans, inode); 6498 + 6499 + ret = btrfs_orphan_add(trans, inode); 6500 + if (ret) { 6501 + btrfs_end_transaction(trans, root); 6502 + return ret; 6503 + } 6504 + 6505 + nr = trans->blocks_used; 6506 + btrfs_end_transaction(trans, root); 6507 + btrfs_btree_balance_dirty(root, nr); 6508 + 6509 + /* Now start a transaction for the truncate */ 6510 trans = btrfs_start_transaction(root, 0); 6511 + if (IS_ERR(trans)) 6512 + return PTR_ERR(trans); 6513 btrfs_set_trans_block_group(trans, inode); 6514 trans->block_rsv = root->orphan_block_rsv; 6515 ··· 6522 while (1) { 6523 if (!trans) { 6524 trans = btrfs_start_transaction(root, 0); 6525 + if (IS_ERR(trans)) 6526 + return PTR_ERR(trans); 6527 btrfs_set_trans_block_group(trans, inode); 6528 trans->block_rsv = root->orphan_block_rsv; 6529 } 6530 6531 ret = btrfs_block_rsv_check(trans, root, 6532 root->orphan_block_rsv, 0, 5); 6533 + if (ret == -EAGAIN) { 6534 ret = btrfs_commit_transaction(trans, root); 6535 + if (ret) 6536 + return ret; 6537 trans = NULL; 6538 continue; 6539 + } else if (ret) { 6540 + err = ret; 6541 + break; 6542 } 6543 6544 ret = btrfs_truncate_inode_items(trans, root, inode, 6545 inode->i_size, 6546 BTRFS_EXTENT_DATA_KEY); 6547 + if (ret != -EAGAIN) { 6548 + err = ret; 6549 break; 6550 + } 6551 6552 ret = btrfs_update_inode(trans, root, inode); 6553 + if (ret) { 6554 + err = ret; 6555 + break; 6556 + } 6557 6558 nr = trans->blocks_used; 6559 btrfs_end_transaction(trans, root); ··· 6554 6555 if (ret == 0 && inode->i_nlink > 0) { 6556 ret = btrfs_orphan_del(trans, inode); 6557 + if (ret) 6558 + err = ret; 6559 + } else if (ret && inode->i_nlink > 0) { 6560 + /* 6561 + * Failed to do the truncate, remove us from the in memory 6562 + * orphan list. 6563 + */ 6564 + ret = btrfs_orphan_del(NULL, inode); 6565 } 6566 6567 ret = btrfs_update_inode(trans, root, inode); 6568 + if (ret && !err) 6569 + err = ret; 6570 6571 nr = trans->blocks_used; 6572 ret = btrfs_end_transaction_throttle(trans, root); 6573 + if (ret && !err) 6574 + err = ret; 6575 btrfs_btree_balance_dirty(root, nr); 6576 + 6577 + return err; 6578 } 6579 6580 /* ··· 6630 ei->index_cnt = (u64)-1; 6631 ei->last_unlink_trans = 0; 6632 6633 atomic_set(&ei->outstanding_extents, 0); 6634 + atomic_set(&ei->reserved_extents, 0); 6635 6636 ei->ordered_data_close = 0; 6637 ei->orphan_meta_reserved = 0; ··· 6668 WARN_ON(!list_empty(&inode->i_dentry)); 6669 WARN_ON(inode->i_data.nrpages); 6670 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); 6671 + WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents)); 6672 6673 /* 6674 * This can happen where we create an inode, but somebody else also ··· 6760 kmem_cache_destroy(btrfs_transaction_cachep); 6761 if (btrfs_path_cachep) 6762 kmem_cache_destroy(btrfs_path_cachep); 6763 + if (btrfs_free_space_cachep) 6764 + kmem_cache_destroy(btrfs_free_space_cachep); 6765 } 6766 6767 int btrfs_init_cachep(void) ··· 6788 if (!btrfs_path_cachep) 6789 goto fail; 6790 6791 + btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache", 6792 + sizeof(struct btrfs_free_space), 0, 6793 + SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); 6794 + if (!btrfs_free_space_cachep) 6795 + goto fail; 6796 + 6797 return 0; 6798 fail: 6799 btrfs_destroy_cachep(); ··· 6804 stat->blocks = (inode_get_bytes(inode) + 6805 BTRFS_I(inode)->delalloc_bytes) >> 9; 6806 return 0; 6807 + } 6808 + 6809 + /* 6810 + * If a file is moved, it will inherit the cow and compression flags of the new 6811 + * directory. 6812 + */ 6813 + static void fixup_inode_flags(struct inode *dir, struct inode *inode) 6814 + { 6815 + struct btrfs_inode *b_dir = BTRFS_I(dir); 6816 + struct btrfs_inode *b_inode = BTRFS_I(inode); 6817 + 6818 + if (b_dir->flags & BTRFS_INODE_NODATACOW) 6819 + b_inode->flags |= BTRFS_INODE_NODATACOW; 6820 + else 6821 + b_inode->flags &= ~BTRFS_INODE_NODATACOW; 6822 + 6823 + if (b_dir->flags & BTRFS_INODE_COMPRESS) 6824 + b_inode->flags |= BTRFS_INODE_COMPRESS; 6825 + else 6826 + b_inode->flags &= ~BTRFS_INODE_COMPRESS; 6827 } 6828 6829 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, ··· 6908 old_dentry->d_name.name, 6909 old_dentry->d_name.len); 6910 } else { 6911 + ret = __btrfs_unlink_inode(trans, root, old_dir, 6912 + old_dentry->d_inode, 6913 + old_dentry->d_name.name, 6914 + old_dentry->d_name.len); 6915 + if (!ret) 6916 + ret = btrfs_update_inode(trans, root, old_inode); 6917 } 6918 BUG_ON(ret); 6919 ··· 6938 BUG_ON(ret); 6939 } 6940 } 6941 + 6942 + fixup_inode_flags(new_dir, old_inode); 6943 6944 ret = btrfs_add_link(trans, new_dir, old_inode, 6945 new_dentry->d_name.name, ··· 7355 }; 7356 7357 static const struct inode_operations btrfs_file_inode_operations = { 7358 .getattr = btrfs_getattr, 7359 .setattr = btrfs_setattr, 7360 .setxattr = btrfs_setxattr,
+93 -7
fs/btrfs/ioctl.c
··· 40 #include <linux/xattr.h> 41 #include <linux/vmalloc.h> 42 #include <linux/slab.h> 43 #include "compat.h" 44 #include "ctree.h" 45 #include "disk-io.h" ··· 139 return 0; 140 } 141 142 static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 143 { 144 struct inode *inode = file->f_path.dentry->d_inode; ··· 172 if (copy_from_user(&flags, arg, sizeof(flags))) 173 return -EFAULT; 174 175 - if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 176 - FS_NOATIME_FL | FS_NODUMP_FL | \ 177 - FS_SYNC_FL | FS_DIRSYNC_FL)) 178 - return -EOPNOTSUPP; 179 180 if (!inode_owner_or_capable(inode)) 181 return -EACCES; ··· 219 else 220 ip->flags &= ~BTRFS_INODE_DIRSYNC; 221 222 223 trans = btrfs_join_transaction(root, 1); 224 BUG_ON(IS_ERR(trans)); ··· 247 btrfs_end_transaction(trans, root); 248 249 mnt_drop_write(file->f_path.mnt); 250 out_unlock: 251 mutex_unlock(&inode->i_mutex); 252 - return 0; 253 } 254 255 static int btrfs_ioctl_getversion(struct file *file, int __user *arg) ··· 259 struct inode *inode = file->f_path.dentry->d_inode; 260 261 return put_user(inode->i_generation, arg); 262 } 263 264 static noinline int create_subvol(struct btrfs_root *root, ··· 488 if (ret) 489 goto fail; 490 491 - btrfs_orphan_cleanup(pending_snapshot->snap); 492 493 parent = dget_parent(dentry); 494 inode = btrfs_lookup_dentry(parent->d_inode, dentry); ··· 2429 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 2430 struct btrfs_trans_handle *trans; 2431 u64 transid; 2432 2433 trans = btrfs_start_transaction(root, 0); 2434 if (IS_ERR(trans)) 2435 return PTR_ERR(trans); 2436 transid = trans->transid; 2437 - btrfs_commit_transaction_async(trans, root, 0); 2438 2439 if (argp) 2440 if (copy_to_user(argp, &transid, sizeof(transid))) ··· 2472 return btrfs_ioctl_setflags(file, argp); 2473 case FS_IOC_GETVERSION: 2474 return btrfs_ioctl_getversion(file, argp); 2475 case BTRFS_IOC_SNAP_CREATE: 2476 return btrfs_ioctl_snap_create(file, argp, 0); 2477 case BTRFS_IOC_SNAP_CREATE_V2:
··· 40 #include <linux/xattr.h> 41 #include <linux/vmalloc.h> 42 #include <linux/slab.h> 43 + #include <linux/blkdev.h> 44 #include "compat.h" 45 #include "ctree.h" 46 #include "disk-io.h" ··· 138 return 0; 139 } 140 141 + static int check_flags(unsigned int flags) 142 + { 143 + if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ 144 + FS_NOATIME_FL | FS_NODUMP_FL | \ 145 + FS_SYNC_FL | FS_DIRSYNC_FL | \ 146 + FS_NOCOMP_FL | FS_COMPR_FL | \ 147 + FS_NOCOW_FL | FS_COW_FL)) 148 + return -EOPNOTSUPP; 149 + 150 + if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) 151 + return -EINVAL; 152 + 153 + if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL)) 154 + return -EINVAL; 155 + 156 + return 0; 157 + } 158 + 159 static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 160 { 161 struct inode *inode = file->f_path.dentry->d_inode; ··· 153 if (copy_from_user(&flags, arg, sizeof(flags))) 154 return -EFAULT; 155 156 + ret = check_flags(flags); 157 + if (ret) 158 + return ret; 159 160 if (!inode_owner_or_capable(inode)) 161 return -EACCES; ··· 201 else 202 ip->flags &= ~BTRFS_INODE_DIRSYNC; 203 204 + /* 205 + * The COMPRESS flag can only be changed by users, while the NOCOMPRESS 206 + * flag may be changed automatically if compression code won't make 207 + * things smaller. 208 + */ 209 + if (flags & FS_NOCOMP_FL) { 210 + ip->flags &= ~BTRFS_INODE_COMPRESS; 211 + ip->flags |= BTRFS_INODE_NOCOMPRESS; 212 + } else if (flags & FS_COMPR_FL) { 213 + ip->flags |= BTRFS_INODE_COMPRESS; 214 + ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 215 + } 216 + if (flags & FS_NOCOW_FL) 217 + ip->flags |= BTRFS_INODE_NODATACOW; 218 + else if (flags & FS_COW_FL) 219 + ip->flags &= ~BTRFS_INODE_NODATACOW; 220 221 trans = btrfs_join_transaction(root, 1); 222 BUG_ON(IS_ERR(trans)); ··· 213 btrfs_end_transaction(trans, root); 214 215 mnt_drop_write(file->f_path.mnt); 216 + 217 + ret = 0; 218 out_unlock: 219 mutex_unlock(&inode->i_mutex); 220 + return ret; 221 } 222 223 static int btrfs_ioctl_getversion(struct file *file, int __user *arg) ··· 223 struct inode *inode = file->f_path.dentry->d_inode; 224 225 return put_user(inode->i_generation, arg); 226 + } 227 + 228 + static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 229 + { 230 + struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info; 231 + struct btrfs_fs_info *fs_info = root->fs_info; 232 + struct btrfs_device *device; 233 + struct request_queue *q; 234 + struct fstrim_range range; 235 + u64 minlen = ULLONG_MAX; 236 + u64 num_devices = 0; 237 + int ret; 238 + 239 + if (!capable(CAP_SYS_ADMIN)) 240 + return -EPERM; 241 + 242 + mutex_lock(&fs_info->fs_devices->device_list_mutex); 243 + list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) { 244 + if (!device->bdev) 245 + continue; 246 + q = bdev_get_queue(device->bdev); 247 + if (blk_queue_discard(q)) { 248 + num_devices++; 249 + minlen = min((u64)q->limits.discard_granularity, 250 + minlen); 251 + } 252 + } 253 + mutex_unlock(&fs_info->fs_devices->device_list_mutex); 254 + if (!num_devices) 255 + return -EOPNOTSUPP; 256 + 257 + if (copy_from_user(&range, arg, sizeof(range))) 258 + return -EFAULT; 259 + 260 + range.minlen = max(range.minlen, minlen); 261 + ret = btrfs_trim_fs(root, &range); 262 + if (ret < 0) 263 + return ret; 264 + 265 + if (copy_to_user(arg, &range, sizeof(range))) 266 + return -EFAULT; 267 + 268 + return 0; 269 } 270 271 static noinline int create_subvol(struct btrfs_root *root, ··· 409 if (ret) 410 goto fail; 411 412 + ret = btrfs_orphan_cleanup(pending_snapshot->snap); 413 + if (ret) 414 + goto fail; 415 416 parent = dget_parent(dentry); 417 inode = btrfs_lookup_dentry(parent->d_inode, dentry); ··· 2348 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; 2349 struct btrfs_trans_handle *trans; 2350 u64 transid; 2351 + int ret; 2352 2353 trans = btrfs_start_transaction(root, 0); 2354 if (IS_ERR(trans)) 2355 return PTR_ERR(trans); 2356 transid = trans->transid; 2357 + ret = btrfs_commit_transaction_async(trans, root, 0); 2358 + if (ret) 2359 + return ret; 2360 2361 if (argp) 2362 if (copy_to_user(argp, &transid, sizeof(transid))) ··· 2388 return btrfs_ioctl_setflags(file, argp); 2389 case FS_IOC_GETVERSION: 2390 return btrfs_ioctl_getversion(file, argp); 2391 + case FITRIM: 2392 + return btrfs_ioctl_fitrim(file, argp); 2393 case BTRFS_IOC_SNAP_CREATE: 2394 return btrfs_ioctl_snap_create(file, argp, 0); 2395 case BTRFS_IOC_SNAP_CREATE_V2:
+8
fs/btrfs/ordered-data.c
··· 202 INIT_LIST_HEAD(&entry->list); 203 INIT_LIST_HEAD(&entry->root_extent_list); 204 205 spin_lock(&tree->lock); 206 node = tree_insert(&tree->tree, file_offset, 207 &entry->rb_node); ··· 389 struct list_head *cur; 390 struct btrfs_ordered_sum *sum; 391 392 if (atomic_dec_and_test(&entry->refs)) { 393 while (!list_empty(&entry->list)) { 394 cur = entry->list.next; ··· 423 424 spin_lock(&root->fs_info->ordered_extent_lock); 425 list_del_init(&entry->root_extent_list); 426 427 /* 428 * we have no more ordered extents for this inode and ··· 590 { 591 u64 start = entry->file_offset; 592 u64 end = start + entry->len - 1; 593 594 /* 595 * pages in the range can be dirty, clean or writeback. We
··· 202 INIT_LIST_HEAD(&entry->list); 203 INIT_LIST_HEAD(&entry->root_extent_list); 204 205 + trace_btrfs_ordered_extent_add(inode, entry); 206 + 207 spin_lock(&tree->lock); 208 node = tree_insert(&tree->tree, file_offset, 209 &entry->rb_node); ··· 387 struct list_head *cur; 388 struct btrfs_ordered_sum *sum; 389 390 + trace_btrfs_ordered_extent_put(entry->inode, entry); 391 + 392 if (atomic_dec_and_test(&entry->refs)) { 393 while (!list_empty(&entry->list)) { 394 cur = entry->list.next; ··· 419 420 spin_lock(&root->fs_info->ordered_extent_lock); 421 list_del_init(&entry->root_extent_list); 422 + 423 + trace_btrfs_ordered_extent_remove(inode, entry); 424 425 /* 426 * we have no more ordered extents for this inode and ··· 584 { 585 u64 start = entry->file_offset; 586 u64 end = start + entry->len - 1; 587 + 588 + trace_btrfs_ordered_extent_start(inode, entry); 589 590 /* 591 * pages in the range can be dirty, clean or writeback. We
+7 -1
fs/btrfs/relocation.c
··· 1724 1725 eb = read_tree_block(dest, old_bytenr, blocksize, 1726 old_ptr_gen); 1727 btrfs_tree_lock(eb); 1728 if (cow) { 1729 ret = btrfs_cow_block(trans, dest, eb, parent, ··· 2514 blocksize = btrfs_level_size(root, node->level); 2515 generation = btrfs_node_ptr_generation(upper->eb, slot); 2516 eb = read_tree_block(root, bytenr, blocksize, generation); 2517 btrfs_tree_lock(eb); 2518 btrfs_set_lock_blocking(eb); 2519 ··· 2675 BUG_ON(block->key_ready); 2676 eb = read_tree_block(rc->extent_root, block->bytenr, 2677 block->key.objectid, block->key.offset); 2678 WARN_ON(btrfs_header_level(eb) != block->level); 2679 if (block->level == 0) 2680 btrfs_item_key_to_cpu(eb, &block->key, 0); ··· 4215 if (IS_ERR(fs_root)) 4216 err = PTR_ERR(fs_root); 4217 else 4218 - btrfs_orphan_cleanup(fs_root); 4219 } 4220 return err; 4221 }
··· 1724 1725 eb = read_tree_block(dest, old_bytenr, blocksize, 1726 old_ptr_gen); 1727 + BUG_ON(!eb); 1728 btrfs_tree_lock(eb); 1729 if (cow) { 1730 ret = btrfs_cow_block(trans, dest, eb, parent, ··· 2513 blocksize = btrfs_level_size(root, node->level); 2514 generation = btrfs_node_ptr_generation(upper->eb, slot); 2515 eb = read_tree_block(root, bytenr, blocksize, generation); 2516 + if (!eb) { 2517 + err = -EIO; 2518 + goto next; 2519 + } 2520 btrfs_tree_lock(eb); 2521 btrfs_set_lock_blocking(eb); 2522 ··· 2670 BUG_ON(block->key_ready); 2671 eb = read_tree_block(rc->extent_root, block->bytenr, 2672 block->key.objectid, block->key.offset); 2673 + BUG_ON(!eb); 2674 WARN_ON(btrfs_header_level(eb) != block->level); 2675 if (block->level == 0) 2676 btrfs_item_key_to_cpu(eb, &block->key, 0); ··· 4209 if (IS_ERR(fs_root)) 4210 err = PTR_ERR(fs_root); 4211 else 4212 + err = btrfs_orphan_cleanup(fs_root); 4213 } 4214 return err; 4215 }
+4 -2
fs/btrfs/root-tree.c
··· 88 search_key.offset = (u64)-1; 89 90 path = btrfs_alloc_path(); 91 - BUG_ON(!path); 92 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 93 if (ret < 0) 94 goto out; ··· 333 struct extent_buffer *leaf; 334 335 path = btrfs_alloc_path(); 336 - BUG_ON(!path); 337 ret = btrfs_search_slot(trans, root, key, path, -1, 1); 338 if (ret < 0) 339 goto out;
··· 88 search_key.offset = (u64)-1; 89 90 path = btrfs_alloc_path(); 91 + if (!path) 92 + return -ENOMEM; 93 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); 94 if (ret < 0) 95 goto out; ··· 332 struct extent_buffer *leaf; 333 334 path = btrfs_alloc_path(); 335 + if (!path) 336 + return -ENOMEM; 337 ret = btrfs_search_slot(trans, root, key, path, -1, 1); 338 if (ret < 0) 339 goto out;
+5
fs/btrfs/super.c
··· 52 #include "export.h" 53 #include "compression.h" 54 55 static const struct super_operations btrfs_super_ops; 56 57 static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno, ··· 622 struct btrfs_trans_handle *trans; 623 struct btrfs_root *root = btrfs_sb(sb); 624 int ret; 625 626 if (!wait) { 627 filemap_flush(root->fs_info->btree_inode->i_mapping);
··· 52 #include "export.h" 53 #include "compression.h" 54 55 + #define CREATE_TRACE_POINTS 56 + #include <trace/events/btrfs.h> 57 + 58 static const struct super_operations btrfs_super_ops; 59 60 static const char *btrfs_decode_error(struct btrfs_fs_info *fs_info, int errno, ··· 619 struct btrfs_trans_handle *trans; 620 struct btrfs_root *root = btrfs_sb(sb); 621 int ret; 622 + 623 + trace_btrfs_sync_fs(wait); 624 625 if (!wait) { 626 filemap_flush(root->fs_info->btree_inode->i_mapping);
+11 -3
fs/btrfs/transaction.c
··· 57 if (!cur_trans) { 58 cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, 59 GFP_NOFS); 60 - BUG_ON(!cur_trans); 61 root->fs_info->generation++; 62 cur_trans->num_writers = 1; 63 cur_trans->num_joined = 0; ··· 196 wait_current_trans(root); 197 198 ret = join_transaction(root); 199 - BUG_ON(ret); 200 201 cur_trans = root->fs_info->running_transaction; 202 cur_trans->use_count++; ··· 1161 struct btrfs_transaction *cur_trans; 1162 1163 ac = kmalloc(sizeof(*ac), GFP_NOFS); 1164 - BUG_ON(!ac); 1165 1166 INIT_DELAYED_WORK(&ac->work, do_async_commit); 1167 ac->root = root; ··· 1394 1395 put_transaction(cur_trans); 1396 put_transaction(cur_trans); 1397 1398 mutex_unlock(&root->fs_info->trans_mutex); 1399
··· 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 cur_trans->num_writers = 1; 64 cur_trans->num_joined = 0; ··· 195 wait_current_trans(root); 196 197 ret = join_transaction(root); 198 + if (ret < 0) { 199 + if (type != TRANS_JOIN_NOLOCK) 200 + mutex_unlock(&root->fs_info->trans_mutex); 201 + return ERR_PTR(ret); 202 + } 203 204 cur_trans = root->fs_info->running_transaction; 205 cur_trans->use_count++; ··· 1156 struct btrfs_transaction *cur_trans; 1157 1158 ac = kmalloc(sizeof(*ac), GFP_NOFS); 1159 + if (!ac) 1160 + return -ENOMEM; 1161 1162 INIT_DELAYED_WORK(&ac->work, do_async_commit); 1163 ac->root = root; ··· 1388 1389 put_transaction(cur_trans); 1390 put_transaction(cur_trans); 1391 + 1392 + trace_btrfs_transaction_commit(root); 1393 1394 mutex_unlock(&root->fs_info->trans_mutex); 1395
+28 -31
fs/btrfs/tree-log.c
··· 799 struct inode *dir; 800 int ret; 801 struct btrfs_inode_ref *ref; 802 - struct btrfs_dir_item *di; 803 struct inode *inode; 804 char *name; 805 int namelen; 806 unsigned long ref_ptr; 807 unsigned long ref_end; 808 809 /* 810 * it is possible that we didn't log all the parent directories ··· 845 * existing back reference, and we don't want to create 846 * dangling pointers in the directory. 847 */ 848 - conflict_again: 849 ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 850 if (ret == 0) { 851 char *victim_name; ··· 889 ret = btrfs_unlink_inode(trans, root, dir, 890 inode, victim_name, 891 victim_name_len); 892 - kfree(victim_name); 893 - btrfs_release_path(root, path); 894 - goto conflict_again; 895 } 896 kfree(victim_name); 897 ptr = (unsigned long)(victim_ref + 1) + victim_name_len; 898 } 899 BUG_ON(ret); 900 } 901 btrfs_release_path(root, path); 902 903 - /* look for a conflicting sequence number */ 904 - di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, 905 - btrfs_inode_ref_index(eb, ref), 906 - name, namelen, 0); 907 - if (di && !IS_ERR(di)) { 908 - ret = drop_one_dir_item(trans, root, path, dir, di); 909 - BUG_ON(ret); 910 - } 911 - btrfs_release_path(root, path); 912 - 913 - 914 - /* look for a conflicting name */ 915 - di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, 916 - name, namelen, 0); 917 - if (di && !IS_ERR(di)) { 918 - ret = drop_one_dir_item(trans, root, path, dir, di); 919 - BUG_ON(ret); 920 - } 921 - btrfs_release_path(root, path); 922 - 923 /* insert our name */ 924 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, 925 btrfs_inode_ref_index(eb, ref)); ··· 1273 ptr_end = ptr + item_size; 1274 while (ptr < ptr_end) { 1275 di = (struct btrfs_dir_item *)ptr; 1276 name_len = btrfs_dir_name_len(eb, di); 1277 ret = replay_one_name(trans, root, path, eb, di, key); 1278 BUG_ON(ret); ··· 1401 ptr_end = ptr + item_size; 1402 while (ptr < ptr_end) { 1403 di = (struct btrfs_dir_item *)ptr; 1404 name_len = btrfs_dir_name_len(eb, di); 1405 name = kmalloc(name_len, GFP_NOFS); 1406 if (!name) { ··· 1815 int orig_level; 1816 1817 path = btrfs_alloc_path(); 1818 - BUG_ON(!path); 1819 1820 level = btrfs_header_level(log->node); 1821 orig_level = level; ··· 3102 .stage = 0, 3103 }; 3104 3105 - fs_info->log_root_recovering = 1; 3106 path = btrfs_alloc_path(); 3107 - BUG_ON(!path); 3108 3109 trans = btrfs_start_transaction(fs_info->tree_root, 0); 3110 BUG_ON(IS_ERR(trans)); ··· 3114 wc.trans = trans; 3115 wc.pin = 1; 3116 3117 - walk_log_tree(trans, log_root_tree, &wc); 3118 3119 again: 3120 key.objectid = BTRFS_TREE_LOG_OBJECTID; ··· 3139 3140 log = btrfs_read_fs_root_no_radix(log_root_tree, 3141 &found_key); 3142 - BUG_ON(!log); 3143 - 3144 3145 tmp_key.objectid = found_key.offset; 3146 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
··· 799 struct inode *dir; 800 int ret; 801 struct btrfs_inode_ref *ref; 802 struct inode *inode; 803 char *name; 804 int namelen; 805 unsigned long ref_ptr; 806 unsigned long ref_end; 807 + int search_done = 0; 808 809 /* 810 * it is possible that we didn't log all the parent directories ··· 845 * existing back reference, and we don't want to create 846 * dangling pointers in the directory. 847 */ 848 + 849 + if (search_done) 850 + goto insert; 851 + 852 ret = btrfs_search_slot(NULL, root, key, path, 0, 0); 853 if (ret == 0) { 854 char *victim_name; ··· 886 ret = btrfs_unlink_inode(trans, root, dir, 887 inode, victim_name, 888 victim_name_len); 889 } 890 kfree(victim_name); 891 ptr = (unsigned long)(victim_ref + 1) + victim_name_len; 892 } 893 BUG_ON(ret); 894 + 895 + /* 896 + * NOTE: we have searched root tree and checked the 897 + * coresponding ref, it does not need to check again. 898 + */ 899 + search_done = 1; 900 } 901 btrfs_release_path(root, path); 902 903 + insert: 904 /* insert our name */ 905 ret = btrfs_add_link(trans, dir, inode, name, namelen, 0, 906 btrfs_inode_ref_index(eb, ref)); ··· 1286 ptr_end = ptr + item_size; 1287 while (ptr < ptr_end) { 1288 di = (struct btrfs_dir_item *)ptr; 1289 + if (verify_dir_item(root, eb, di)) 1290 + return -EIO; 1291 name_len = btrfs_dir_name_len(eb, di); 1292 ret = replay_one_name(trans, root, path, eb, di, key); 1293 BUG_ON(ret); ··· 1412 ptr_end = ptr + item_size; 1413 while (ptr < ptr_end) { 1414 di = (struct btrfs_dir_item *)ptr; 1415 + if (verify_dir_item(root, eb, di)) { 1416 + ret = -EIO; 1417 + goto out; 1418 + } 1419 + 1420 name_len = btrfs_dir_name_len(eb, di); 1421 name = kmalloc(name_len, GFP_NOFS); 1422 if (!name) { ··· 1821 int orig_level; 1822 1823 path = btrfs_alloc_path(); 1824 + if (!path) 1825 + return -ENOMEM; 1826 1827 level = btrfs_header_level(log->node); 1828 orig_level = level; ··· 3107 .stage = 0, 3108 }; 3109 3110 path = btrfs_alloc_path(); 3111 + if (!path) 3112 + return -ENOMEM; 3113 + 3114 + fs_info->log_root_recovering = 1; 3115 3116 trans = btrfs_start_transaction(fs_info->tree_root, 0); 3117 BUG_ON(IS_ERR(trans)); ··· 3117 wc.trans = trans; 3118 wc.pin = 1; 3119 3120 + ret = walk_log_tree(trans, log_root_tree, &wc); 3121 + BUG_ON(ret); 3122 3123 again: 3124 key.objectid = BTRFS_TREE_LOG_OBJECTID; ··· 3141 3142 log = btrfs_read_fs_root_no_radix(log_root_tree, 3143 &found_key); 3144 + BUG_ON(IS_ERR(log)); 3145 3146 tmp_key.objectid = found_key.offset; 3147 tmp_key.type = BTRFS_ROOT_ITEM_KEY;
+138 -26
fs/btrfs/volumes.c
··· 33 #include "volumes.h" 34 #include "async-thread.h" 35 36 - struct map_lookup { 37 - u64 type; 38 - int io_align; 39 - int io_width; 40 - int stripe_len; 41 - int sector_size; 42 - int num_stripes; 43 - int sub_stripes; 44 - struct btrfs_bio_stripe stripes[]; 45 - }; 46 - 47 static int init_first_rw_device(struct btrfs_trans_handle *trans, 48 struct btrfs_root *root, 49 struct btrfs_device *device); ··· 1868 1869 BUG_ON(ret); 1870 1871 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { 1872 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); 1873 BUG_ON(ret); ··· 2597 *num_bytes = chunk_bytes_by_type(type, calc_size, 2598 map->num_stripes, sub_stripes); 2599 2600 em = alloc_extent_map(GFP_NOFS); 2601 if (!em) { 2602 ret = -ENOMEM; ··· 2707 item_size); 2708 BUG_ON(ret); 2709 } 2710 kfree(chunk); 2711 return 0; 2712 } ··· 2912 struct extent_map_tree *em_tree = &map_tree->map_tree; 2913 u64 offset; 2914 u64 stripe_offset; 2915 u64 stripe_nr; 2916 int stripes_allocated = 8; 2917 int stripes_required = 1; 2918 int stripe_index; ··· 2924 int max_errors = 0; 2925 struct btrfs_multi_bio *multi = NULL; 2926 2927 - if (multi_ret && !(rw & REQ_WRITE)) 2928 stripes_allocated = 1; 2929 again: 2930 if (multi_ret) { ··· 2965 max_errors = 1; 2966 } 2967 } 2968 - if (multi_ret && (rw & REQ_WRITE) && 2969 stripes_allocated < stripes_required) { 2970 stripes_allocated = map->num_stripes; 2971 free_extent_map(em); ··· 2993 /* stripe_offset is the offset of this block in its stripe*/ 2994 stripe_offset = offset - stripe_offset; 2995 2996 - if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 | 2997 - BTRFS_BLOCK_GROUP_RAID10 | 2998 - BTRFS_BLOCK_GROUP_DUP)) { 2999 /* we limit the length of each bio to what fits in a stripe */ 3000 *length = min_t(u64, em->len - offset, 3001 - map->stripe_len - stripe_offset); 3002 } else { 3003 *length = em->len - offset; 3004 } ··· 3011 3012 num_stripes = 1; 3013 stripe_index = 0; 3014 - if (map->type & BTRFS_BLOCK_GROUP_RAID1) { 3015 - if (rw & REQ_WRITE) 3016 num_stripes = map->num_stripes; 3017 else if (mirror_num) 3018 stripe_index = mirror_num - 1; ··· 3034 } 3035 3036 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { 3037 - if (rw & REQ_WRITE) 3038 num_stripes = map->num_stripes; 3039 else if (mirror_num) 3040 stripe_index = mirror_num - 1; ··· 3047 3048 if (rw & REQ_WRITE) 3049 num_stripes = map->sub_stripes; 3050 else if (mirror_num) 3051 stripe_index += mirror_num - 1; 3052 else { ··· 3068 } 3069 BUG_ON(stripe_index >= map->num_stripes); 3070 3071 - for (i = 0; i < num_stripes; i++) { 3072 - multi->stripes[i].physical = 3073 - map->stripes[stripe_index].physical + 3074 - stripe_offset + stripe_nr * map->stripe_len; 3075 - multi->stripes[i].dev = map->stripes[stripe_index].dev; 3076 - stripe_index++; 3077 } 3078 if (multi_ret) { 3079 *multi_ret = multi;
··· 33 #include "volumes.h" 34 #include "async-thread.h" 35 36 static int init_first_rw_device(struct btrfs_trans_handle *trans, 37 struct btrfs_root *root, 38 struct btrfs_device *device); ··· 1879 1880 BUG_ON(ret); 1881 1882 + trace_btrfs_chunk_free(root, map, chunk_offset, em->len); 1883 + 1884 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { 1885 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); 1886 BUG_ON(ret); ··· 2606 *num_bytes = chunk_bytes_by_type(type, calc_size, 2607 map->num_stripes, sub_stripes); 2608 2609 + trace_btrfs_chunk_alloc(info->chunk_root, map, start, *num_bytes); 2610 + 2611 em = alloc_extent_map(GFP_NOFS); 2612 if (!em) { 2613 ret = -ENOMEM; ··· 2714 item_size); 2715 BUG_ON(ret); 2716 } 2717 + 2718 kfree(chunk); 2719 return 0; 2720 } ··· 2918 struct extent_map_tree *em_tree = &map_tree->map_tree; 2919 u64 offset; 2920 u64 stripe_offset; 2921 + u64 stripe_end_offset; 2922 u64 stripe_nr; 2923 + u64 stripe_nr_orig; 2924 + u64 stripe_nr_end; 2925 int stripes_allocated = 8; 2926 int stripes_required = 1; 2927 int stripe_index; ··· 2927 int max_errors = 0; 2928 struct btrfs_multi_bio *multi = NULL; 2929 2930 + if (multi_ret && !(rw & (REQ_WRITE | REQ_DISCARD))) 2931 stripes_allocated = 1; 2932 again: 2933 if (multi_ret) { ··· 2968 max_errors = 1; 2969 } 2970 } 2971 + if (rw & REQ_DISCARD) { 2972 + if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | 2973 + BTRFS_BLOCK_GROUP_RAID1 | 2974 + BTRFS_BLOCK_GROUP_DUP | 2975 + BTRFS_BLOCK_GROUP_RAID10)) { 2976 + stripes_required = map->num_stripes; 2977 + } 2978 + } 2979 + if (multi_ret && (rw & (REQ_WRITE | REQ_DISCARD)) && 2980 stripes_allocated < stripes_required) { 2981 stripes_allocated = map->num_stripes; 2982 free_extent_map(em); ··· 2988 /* stripe_offset is the offset of this block in its stripe*/ 2989 stripe_offset = offset - stripe_offset; 2990 2991 + if (rw & REQ_DISCARD) 2992 + *length = min_t(u64, em->len - offset, *length); 2993 + else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | 2994 + BTRFS_BLOCK_GROUP_RAID1 | 2995 + BTRFS_BLOCK_GROUP_RAID10 | 2996 + BTRFS_BLOCK_GROUP_DUP)) { 2997 /* we limit the length of each bio to what fits in a stripe */ 2998 *length = min_t(u64, em->len - offset, 2999 + map->stripe_len - stripe_offset); 3000 } else { 3001 *length = em->len - offset; 3002 } ··· 3003 3004 num_stripes = 1; 3005 stripe_index = 0; 3006 + stripe_nr_orig = stripe_nr; 3007 + stripe_nr_end = (offset + *length + map->stripe_len - 1) & 3008 + (~(map->stripe_len - 1)); 3009 + do_div(stripe_nr_end, map->stripe_len); 3010 + stripe_end_offset = stripe_nr_end * map->stripe_len - 3011 + (offset + *length); 3012 + if (map->type & BTRFS_BLOCK_GROUP_RAID0) { 3013 + if (rw & REQ_DISCARD) 3014 + num_stripes = min_t(u64, map->num_stripes, 3015 + stripe_nr_end - stripe_nr_orig); 3016 + stripe_index = do_div(stripe_nr, map->num_stripes); 3017 + } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { 3018 + if (rw & (REQ_WRITE | REQ_DISCARD)) 3019 num_stripes = map->num_stripes; 3020 else if (mirror_num) 3021 stripe_index = mirror_num - 1; ··· 3015 } 3016 3017 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { 3018 + if (rw & (REQ_WRITE | REQ_DISCARD)) 3019 num_stripes = map->num_stripes; 3020 else if (mirror_num) 3021 stripe_index = mirror_num - 1; ··· 3028 3029 if (rw & REQ_WRITE) 3030 num_stripes = map->sub_stripes; 3031 + else if (rw & REQ_DISCARD) 3032 + num_stripes = min_t(u64, map->sub_stripes * 3033 + (stripe_nr_end - stripe_nr_orig), 3034 + map->num_stripes); 3035 else if (mirror_num) 3036 stripe_index += mirror_num - 1; 3037 else { ··· 3045 } 3046 BUG_ON(stripe_index >= map->num_stripes); 3047 3048 + if (rw & REQ_DISCARD) { 3049 + for (i = 0; i < num_stripes; i++) { 3050 + multi->stripes[i].physical = 3051 + map->stripes[stripe_index].physical + 3052 + stripe_offset + stripe_nr * map->stripe_len; 3053 + multi->stripes[i].dev = map->stripes[stripe_index].dev; 3054 + 3055 + if (map->type & BTRFS_BLOCK_GROUP_RAID0) { 3056 + u64 stripes; 3057 + u32 last_stripe = 0; 3058 + int j; 3059 + 3060 + div_u64_rem(stripe_nr_end - 1, 3061 + map->num_stripes, 3062 + &last_stripe); 3063 + 3064 + for (j = 0; j < map->num_stripes; j++) { 3065 + u32 test; 3066 + 3067 + div_u64_rem(stripe_nr_end - 1 - j, 3068 + map->num_stripes, &test); 3069 + if (test == stripe_index) 3070 + break; 3071 + } 3072 + stripes = stripe_nr_end - 1 - j; 3073 + do_div(stripes, map->num_stripes); 3074 + multi->stripes[i].length = map->stripe_len * 3075 + (stripes - stripe_nr + 1); 3076 + 3077 + if (i == 0) { 3078 + multi->stripes[i].length -= 3079 + stripe_offset; 3080 + stripe_offset = 0; 3081 + } 3082 + if (stripe_index == last_stripe) 3083 + multi->stripes[i].length -= 3084 + stripe_end_offset; 3085 + } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { 3086 + u64 stripes; 3087 + int j; 3088 + int factor = map->num_stripes / 3089 + map->sub_stripes; 3090 + u32 last_stripe = 0; 3091 + 3092 + div_u64_rem(stripe_nr_end - 1, 3093 + factor, &last_stripe); 3094 + last_stripe *= map->sub_stripes; 3095 + 3096 + for (j = 0; j < factor; j++) { 3097 + u32 test; 3098 + 3099 + div_u64_rem(stripe_nr_end - 1 - j, 3100 + factor, &test); 3101 + 3102 + if (test == 3103 + stripe_index / map->sub_stripes) 3104 + break; 3105 + } 3106 + stripes = stripe_nr_end - 1 - j; 3107 + do_div(stripes, factor); 3108 + multi->stripes[i].length = map->stripe_len * 3109 + (stripes - stripe_nr + 1); 3110 + 3111 + if (i < map->sub_stripes) { 3112 + multi->stripes[i].length -= 3113 + stripe_offset; 3114 + if (i == map->sub_stripes - 1) 3115 + stripe_offset = 0; 3116 + } 3117 + if (stripe_index >= last_stripe && 3118 + stripe_index <= (last_stripe + 3119 + map->sub_stripes - 1)) { 3120 + multi->stripes[i].length -= 3121 + stripe_end_offset; 3122 + } 3123 + } else 3124 + multi->stripes[i].length = *length; 3125 + 3126 + stripe_index++; 3127 + if (stripe_index == map->num_stripes) { 3128 + /* This could only happen for RAID0/10 */ 3129 + stripe_index = 0; 3130 + stripe_nr++; 3131 + } 3132 + } 3133 + } else { 3134 + for (i = 0; i < num_stripes; i++) { 3135 + multi->stripes[i].physical = 3136 + map->stripes[stripe_index].physical + 3137 + stripe_offset + 3138 + stripe_nr * map->stripe_len; 3139 + multi->stripes[i].dev = 3140 + map->stripes[stripe_index].dev; 3141 + stripe_index++; 3142 + } 3143 } 3144 if (multi_ret) { 3145 *multi_ret = multi;
+12
fs/btrfs/volumes.h
··· 126 struct btrfs_bio_stripe { 127 struct btrfs_device *dev; 128 u64 physical; 129 }; 130 131 struct btrfs_multi_bio { ··· 144 struct btrfs_device *dev; 145 u64 dev_offset; 146 u64 max_avail; 147 }; 148 149 /* Used to sort the devices by max_avail(descending sort) */
··· 126 struct btrfs_bio_stripe { 127 struct btrfs_device *dev; 128 u64 physical; 129 + u64 length; /* only used for discard mappings */ 130 }; 131 132 struct btrfs_multi_bio { ··· 143 struct btrfs_device *dev; 144 u64 dev_offset; 145 u64 max_avail; 146 + }; 147 + 148 + struct map_lookup { 149 + u64 type; 150 + int io_align; 151 + int io_width; 152 + int stripe_len; 153 + int sector_size; 154 + int num_stripes; 155 + int sub_stripes; 156 + struct btrfs_bio_stripe stripes[]; 157 }; 158 159 /* Used to sort the devices by max_avail(descending sort) */
+2
fs/btrfs/xattr.c
··· 242 break; 243 244 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 245 246 name_len = btrfs_dir_name_len(leaf, di); 247 total_size += name_len + 1;
··· 242 break; 243 244 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 245 + if (verify_dir_item(root, leaf, di)) 246 + continue; 247 248 name_len = btrfs_dir_name_len(leaf, di); 249 total_size += name_len + 1;
+2
include/linux/fs.h
··· 357 #define FS_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/ 358 #define FS_EXTENT_FL 0x00080000 /* Extents */ 359 #define FS_DIRECTIO_FL 0x00100000 /* Use direct i/o */ 360 #define FS_RESERVED_FL 0x80000000 /* reserved for ext2 lib */ 361 362 #define FS_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
··· 357 #define FS_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/ 358 #define FS_EXTENT_FL 0x00080000 /* Extents */ 359 #define FS_DIRECTIO_FL 0x00100000 /* Use direct i/o */ 360 + #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ 361 + #define FS_COW_FL 0x02000000 /* Cow file */ 362 #define FS_RESERVED_FL 0x80000000 /* reserved for ext2 lib */ 363 364 #define FS_FL_USER_VISIBLE 0x0003DFFF /* User visible flags */
+667
include/trace/events/btrfs.h
···
··· 1 + #undef TRACE_SYSTEM 2 + #define TRACE_SYSTEM btrfs 3 + 4 + #if !defined(_TRACE_BTRFS_H) || defined(TRACE_HEADER_MULTI_READ) 5 + #define _TRACE_BTRFS_H 6 + 7 + #include <linux/writeback.h> 8 + #include <linux/tracepoint.h> 9 + 10 + struct btrfs_root; 11 + struct btrfs_fs_info; 12 + struct btrfs_inode; 13 + struct extent_map; 14 + struct btrfs_ordered_extent; 15 + struct btrfs_delayed_ref_node; 16 + struct btrfs_delayed_tree_ref; 17 + struct btrfs_delayed_data_ref; 18 + struct btrfs_delayed_ref_head; 19 + struct map_lookup; 20 + struct extent_buffer; 21 + 22 + #define show_ref_type(type) \ 23 + __print_symbolic(type, \ 24 + { BTRFS_TREE_BLOCK_REF_KEY, "TREE_BLOCK_REF" }, \ 25 + { BTRFS_EXTENT_DATA_REF_KEY, "EXTENT_DATA_REF" }, \ 26 + { BTRFS_EXTENT_REF_V0_KEY, "EXTENT_REF_V0" }, \ 27 + { BTRFS_SHARED_BLOCK_REF_KEY, "SHARED_BLOCK_REF" }, \ 28 + { BTRFS_SHARED_DATA_REF_KEY, "SHARED_DATA_REF" }) 29 + 30 + #define __show_root_type(obj) \ 31 + __print_symbolic(obj, \ 32 + { BTRFS_ROOT_TREE_OBJECTID, "ROOT_TREE" }, \ 33 + { BTRFS_EXTENT_TREE_OBJECTID, "EXTENT_TREE" }, \ 34 + { BTRFS_CHUNK_TREE_OBJECTID, "CHUNK_TREE" }, \ 35 + { BTRFS_DEV_TREE_OBJECTID, "DEV_TREE" }, \ 36 + { BTRFS_FS_TREE_OBJECTID, "FS_TREE" }, \ 37 + { BTRFS_ROOT_TREE_DIR_OBJECTID, "ROOT_TREE_DIR" }, \ 38 + { BTRFS_CSUM_TREE_OBJECTID, "CSUM_TREE" }, \ 39 + { BTRFS_TREE_LOG_OBJECTID, "TREE_LOG" }, \ 40 + { BTRFS_TREE_RELOC_OBJECTID, "TREE_RELOC" }, \ 41 + { BTRFS_DATA_RELOC_TREE_OBJECTID, "DATA_RELOC_TREE" }) 42 + 43 + #define show_root_type(obj) \ 44 + obj, ((obj >= BTRFS_DATA_RELOC_TREE_OBJECTID) || \ 45 + (obj <= BTRFS_CSUM_TREE_OBJECTID )) ? __show_root_type(obj) : "-" 46 + 47 + TRACE_EVENT(btrfs_transaction_commit, 48 + 49 + TP_PROTO(struct btrfs_root *root), 50 + 51 + TP_ARGS(root), 52 + 53 + TP_STRUCT__entry( 54 + __field( u64, generation ) 55 + __field( u64, root_objectid ) 56 + ), 57 + 58 + TP_fast_assign( 59 + __entry->generation = root->fs_info->generation; 60 + __entry->root_objectid = root->root_key.objectid; 61 + ), 62 + 63 + TP_printk("root = %llu(%s), gen = %llu", 64 + show_root_type(__entry->root_objectid), 65 + (unsigned long long)__entry->generation) 66 + ); 67 + 68 + DECLARE_EVENT_CLASS(btrfs__inode, 69 + 70 + TP_PROTO(struct inode *inode), 71 + 72 + TP_ARGS(inode), 73 + 74 + TP_STRUCT__entry( 75 + __field( ino_t, ino ) 76 + __field( blkcnt_t, blocks ) 77 + __field( u64, disk_i_size ) 78 + __field( u64, generation ) 79 + __field( u64, last_trans ) 80 + __field( u64, logged_trans ) 81 + __field( u64, root_objectid ) 82 + ), 83 + 84 + TP_fast_assign( 85 + __entry->ino = inode->i_ino; 86 + __entry->blocks = inode->i_blocks; 87 + __entry->disk_i_size = BTRFS_I(inode)->disk_i_size; 88 + __entry->generation = BTRFS_I(inode)->generation; 89 + __entry->last_trans = BTRFS_I(inode)->last_trans; 90 + __entry->logged_trans = BTRFS_I(inode)->logged_trans; 91 + __entry->root_objectid = 92 + BTRFS_I(inode)->root->root_key.objectid; 93 + ), 94 + 95 + TP_printk("root = %llu(%s), gen = %llu, ino = %lu, blocks = %llu, " 96 + "disk_i_size = %llu, last_trans = %llu, logged_trans = %llu", 97 + show_root_type(__entry->root_objectid), 98 + (unsigned long long)__entry->generation, 99 + (unsigned long)__entry->ino, 100 + (unsigned long long)__entry->blocks, 101 + (unsigned long long)__entry->disk_i_size, 102 + (unsigned long long)__entry->last_trans, 103 + (unsigned long long)__entry->logged_trans) 104 + ); 105 + 106 + DEFINE_EVENT(btrfs__inode, btrfs_inode_new, 107 + 108 + TP_PROTO(struct inode *inode), 109 + 110 + TP_ARGS(inode) 111 + ); 112 + 113 + DEFINE_EVENT(btrfs__inode, btrfs_inode_request, 114 + 115 + TP_PROTO(struct inode *inode), 116 + 117 + TP_ARGS(inode) 118 + ); 119 + 120 + DEFINE_EVENT(btrfs__inode, btrfs_inode_evict, 121 + 122 + TP_PROTO(struct inode *inode), 123 + 124 + TP_ARGS(inode) 125 + ); 126 + 127 + #define __show_map_type(type) \ 128 + __print_symbolic(type, \ 129 + { EXTENT_MAP_LAST_BYTE, "LAST_BYTE" }, \ 130 + { EXTENT_MAP_HOLE, "HOLE" }, \ 131 + { EXTENT_MAP_INLINE, "INLINE" }, \ 132 + { EXTENT_MAP_DELALLOC, "DELALLOC" }) 133 + 134 + #define show_map_type(type) \ 135 + type, (type >= EXTENT_MAP_LAST_BYTE) ? "-" : __show_map_type(type) 136 + 137 + #define show_map_flags(flag) \ 138 + __print_flags(flag, "|", \ 139 + { EXTENT_FLAG_PINNED, "PINNED" }, \ 140 + { EXTENT_FLAG_COMPRESSED, "COMPRESSED" }, \ 141 + { EXTENT_FLAG_VACANCY, "VACANCY" }, \ 142 + { EXTENT_FLAG_PREALLOC, "PREALLOC" }) 143 + 144 + TRACE_EVENT(btrfs_get_extent, 145 + 146 + TP_PROTO(struct btrfs_root *root, struct extent_map *map), 147 + 148 + TP_ARGS(root, map), 149 + 150 + TP_STRUCT__entry( 151 + __field( u64, root_objectid ) 152 + __field( u64, start ) 153 + __field( u64, len ) 154 + __field( u64, orig_start ) 155 + __field( u64, block_start ) 156 + __field( u64, block_len ) 157 + __field( unsigned long, flags ) 158 + __field( int, refs ) 159 + __field( unsigned int, compress_type ) 160 + ), 161 + 162 + TP_fast_assign( 163 + __entry->root_objectid = root->root_key.objectid; 164 + __entry->start = map->start; 165 + __entry->len = map->len; 166 + __entry->orig_start = map->orig_start; 167 + __entry->block_start = map->block_start; 168 + __entry->block_len = map->block_len; 169 + __entry->flags = map->flags; 170 + __entry->refs = atomic_read(&map->refs); 171 + __entry->compress_type = map->compress_type; 172 + ), 173 + 174 + TP_printk("root = %llu(%s), start = %llu, len = %llu, " 175 + "orig_start = %llu, block_start = %llu(%s), " 176 + "block_len = %llu, flags = %s, refs = %u, " 177 + "compress_type = %u", 178 + show_root_type(__entry->root_objectid), 179 + (unsigned long long)__entry->start, 180 + (unsigned long long)__entry->len, 181 + (unsigned long long)__entry->orig_start, 182 + show_map_type(__entry->block_start), 183 + (unsigned long long)__entry->block_len, 184 + show_map_flags(__entry->flags), 185 + __entry->refs, __entry->compress_type) 186 + ); 187 + 188 + #define show_ordered_flags(flags) \ 189 + __print_symbolic(flags, \ 190 + { BTRFS_ORDERED_IO_DONE, "IO_DONE" }, \ 191 + { BTRFS_ORDERED_COMPLETE, "COMPLETE" }, \ 192 + { BTRFS_ORDERED_NOCOW, "NOCOW" }, \ 193 + { BTRFS_ORDERED_COMPRESSED, "COMPRESSED" }, \ 194 + { BTRFS_ORDERED_PREALLOC, "PREALLOC" }, \ 195 + { BTRFS_ORDERED_DIRECT, "DIRECT" }) 196 + 197 + DECLARE_EVENT_CLASS(btrfs__ordered_extent, 198 + 199 + TP_PROTO(struct inode *inode, struct btrfs_ordered_extent *ordered), 200 + 201 + TP_ARGS(inode, ordered), 202 + 203 + TP_STRUCT__entry( 204 + __field( ino_t, ino ) 205 + __field( u64, file_offset ) 206 + __field( u64, start ) 207 + __field( u64, len ) 208 + __field( u64, disk_len ) 209 + __field( u64, bytes_left ) 210 + __field( unsigned long, flags ) 211 + __field( int, compress_type ) 212 + __field( int, refs ) 213 + __field( u64, root_objectid ) 214 + ), 215 + 216 + TP_fast_assign( 217 + __entry->ino = inode->i_ino; 218 + __entry->file_offset = ordered->file_offset; 219 + __entry->start = ordered->start; 220 + __entry->len = ordered->len; 221 + __entry->disk_len = ordered->disk_len; 222 + __entry->bytes_left = ordered->bytes_left; 223 + __entry->flags = ordered->flags; 224 + __entry->compress_type = ordered->compress_type; 225 + __entry->refs = atomic_read(&ordered->refs); 226 + __entry->root_objectid = 227 + BTRFS_I(inode)->root->root_key.objectid; 228 + ), 229 + 230 + TP_printk("root = %llu(%s), ino = %llu, file_offset = %llu, " 231 + "start = %llu, len = %llu, disk_len = %llu, " 232 + "bytes_left = %llu, flags = %s, compress_type = %d, " 233 + "refs = %d", 234 + show_root_type(__entry->root_objectid), 235 + (unsigned long long)__entry->ino, 236 + (unsigned long long)__entry->file_offset, 237 + (unsigned long long)__entry->start, 238 + (unsigned long long)__entry->len, 239 + (unsigned long long)__entry->disk_len, 240 + (unsigned long long)__entry->bytes_left, 241 + show_ordered_flags(__entry->flags), 242 + __entry->compress_type, __entry->refs) 243 + ); 244 + 245 + DEFINE_EVENT(btrfs__ordered_extent, btrfs_ordered_extent_add, 246 + 247 + TP_PROTO(struct inode *inode, struct btrfs_ordered_extent *ordered), 248 + 249 + TP_ARGS(inode, ordered) 250 + ); 251 + 252 + DEFINE_EVENT(btrfs__ordered_extent, btrfs_ordered_extent_remove, 253 + 254 + TP_PROTO(struct inode *inode, struct btrfs_ordered_extent *ordered), 255 + 256 + TP_ARGS(inode, ordered) 257 + ); 258 + 259 + DEFINE_EVENT(btrfs__ordered_extent, btrfs_ordered_extent_start, 260 + 261 + TP_PROTO(struct inode *inode, struct btrfs_ordered_extent *ordered), 262 + 263 + TP_ARGS(inode, ordered) 264 + ); 265 + 266 + DEFINE_EVENT(btrfs__ordered_extent, btrfs_ordered_extent_put, 267 + 268 + TP_PROTO(struct inode *inode, struct btrfs_ordered_extent *ordered), 269 + 270 + TP_ARGS(inode, ordered) 271 + ); 272 + 273 + DECLARE_EVENT_CLASS(btrfs__writepage, 274 + 275 + TP_PROTO(struct page *page, struct inode *inode, 276 + struct writeback_control *wbc), 277 + 278 + TP_ARGS(page, inode, wbc), 279 + 280 + TP_STRUCT__entry( 281 + __field( ino_t, ino ) 282 + __field( pgoff_t, index ) 283 + __field( long, nr_to_write ) 284 + __field( long, pages_skipped ) 285 + __field( loff_t, range_start ) 286 + __field( loff_t, range_end ) 287 + __field( char, nonblocking ) 288 + __field( char, for_kupdate ) 289 + __field( char, for_reclaim ) 290 + __field( char, range_cyclic ) 291 + __field( pgoff_t, writeback_index ) 292 + __field( u64, root_objectid ) 293 + ), 294 + 295 + TP_fast_assign( 296 + __entry->ino = inode->i_ino; 297 + __entry->index = page->index; 298 + __entry->nr_to_write = wbc->nr_to_write; 299 + __entry->pages_skipped = wbc->pages_skipped; 300 + __entry->range_start = wbc->range_start; 301 + __entry->range_end = wbc->range_end; 302 + __entry->nonblocking = wbc->nonblocking; 303 + __entry->for_kupdate = wbc->for_kupdate; 304 + __entry->for_reclaim = wbc->for_reclaim; 305 + __entry->range_cyclic = wbc->range_cyclic; 306 + __entry->writeback_index = inode->i_mapping->writeback_index; 307 + __entry->root_objectid = 308 + BTRFS_I(inode)->root->root_key.objectid; 309 + ), 310 + 311 + TP_printk("root = %llu(%s), ino = %lu, page_index = %lu, " 312 + "nr_to_write = %ld, pages_skipped = %ld, range_start = %llu, " 313 + "range_end = %llu, nonblocking = %d, for_kupdate = %d, " 314 + "for_reclaim = %d, range_cyclic = %d, writeback_index = %lu", 315 + show_root_type(__entry->root_objectid), 316 + (unsigned long)__entry->ino, __entry->index, 317 + __entry->nr_to_write, __entry->pages_skipped, 318 + __entry->range_start, __entry->range_end, 319 + __entry->nonblocking, __entry->for_kupdate, 320 + __entry->for_reclaim, __entry->range_cyclic, 321 + (unsigned long)__entry->writeback_index) 322 + ); 323 + 324 + DEFINE_EVENT(btrfs__writepage, __extent_writepage, 325 + 326 + TP_PROTO(struct page *page, struct inode *inode, 327 + struct writeback_control *wbc), 328 + 329 + TP_ARGS(page, inode, wbc) 330 + ); 331 + 332 + TRACE_EVENT(btrfs_writepage_end_io_hook, 333 + 334 + TP_PROTO(struct page *page, u64 start, u64 end, int uptodate), 335 + 336 + TP_ARGS(page, start, end, uptodate), 337 + 338 + TP_STRUCT__entry( 339 + __field( ino_t, ino ) 340 + __field( pgoff_t, index ) 341 + __field( u64, start ) 342 + __field( u64, end ) 343 + __field( int, uptodate ) 344 + __field( u64, root_objectid ) 345 + ), 346 + 347 + TP_fast_assign( 348 + __entry->ino = page->mapping->host->i_ino; 349 + __entry->index = page->index; 350 + __entry->start = start; 351 + __entry->end = end; 352 + __entry->uptodate = uptodate; 353 + __entry->root_objectid = 354 + BTRFS_I(page->mapping->host)->root->root_key.objectid; 355 + ), 356 + 357 + TP_printk("root = %llu(%s), ino = %lu, page_index = %lu, start = %llu, " 358 + "end = %llu, uptodate = %d", 359 + show_root_type(__entry->root_objectid), 360 + (unsigned long)__entry->ino, (unsigned long)__entry->index, 361 + (unsigned long long)__entry->start, 362 + (unsigned long long)__entry->end, __entry->uptodate) 363 + ); 364 + 365 + TRACE_EVENT(btrfs_sync_file, 366 + 367 + TP_PROTO(struct file *file, int datasync), 368 + 369 + TP_ARGS(file, datasync), 370 + 371 + TP_STRUCT__entry( 372 + __field( ino_t, ino ) 373 + __field( ino_t, parent ) 374 + __field( int, datasync ) 375 + __field( u64, root_objectid ) 376 + ), 377 + 378 + TP_fast_assign( 379 + struct dentry *dentry = file->f_path.dentry; 380 + struct inode *inode = dentry->d_inode; 381 + 382 + __entry->ino = inode->i_ino; 383 + __entry->parent = dentry->d_parent->d_inode->i_ino; 384 + __entry->datasync = datasync; 385 + __entry->root_objectid = 386 + BTRFS_I(inode)->root->root_key.objectid; 387 + ), 388 + 389 + TP_printk("root = %llu(%s), ino = %ld, parent = %ld, datasync = %d", 390 + show_root_type(__entry->root_objectid), 391 + (unsigned long)__entry->ino, (unsigned long)__entry->parent, 392 + __entry->datasync) 393 + ); 394 + 395 + TRACE_EVENT(btrfs_sync_fs, 396 + 397 + TP_PROTO(int wait), 398 + 399 + TP_ARGS(wait), 400 + 401 + TP_STRUCT__entry( 402 + __field( int, wait ) 403 + ), 404 + 405 + TP_fast_assign( 406 + __entry->wait = wait; 407 + ), 408 + 409 + TP_printk("wait = %d", __entry->wait) 410 + ); 411 + 412 + #define show_ref_action(action) \ 413 + __print_symbolic(action, \ 414 + { BTRFS_ADD_DELAYED_REF, "ADD_DELAYED_REF" }, \ 415 + { BTRFS_DROP_DELAYED_REF, "DROP_DELAYED_REF" }, \ 416 + { BTRFS_ADD_DELAYED_EXTENT, "ADD_DELAYED_EXTENT" }, \ 417 + { BTRFS_UPDATE_DELAYED_HEAD, "UPDATE_DELAYED_HEAD" }) 418 + 419 + 420 + TRACE_EVENT(btrfs_delayed_tree_ref, 421 + 422 + TP_PROTO(struct btrfs_delayed_ref_node *ref, 423 + struct btrfs_delayed_tree_ref *full_ref, 424 + int action), 425 + 426 + TP_ARGS(ref, full_ref, action), 427 + 428 + TP_STRUCT__entry( 429 + __field( u64, bytenr ) 430 + __field( u64, num_bytes ) 431 + __field( int, action ) 432 + __field( u64, parent ) 433 + __field( u64, ref_root ) 434 + __field( int, level ) 435 + __field( int, type ) 436 + ), 437 + 438 + TP_fast_assign( 439 + __entry->bytenr = ref->bytenr; 440 + __entry->num_bytes = ref->num_bytes; 441 + __entry->action = action; 442 + __entry->parent = full_ref->parent; 443 + __entry->ref_root = full_ref->root; 444 + __entry->level = full_ref->level; 445 + __entry->type = ref->type; 446 + ), 447 + 448 + TP_printk("bytenr = %llu, num_bytes = %llu, action = %s, " 449 + "parent = %llu(%s), ref_root = %llu(%s), level = %d, " 450 + "type = %s", 451 + (unsigned long long)__entry->bytenr, 452 + (unsigned long long)__entry->num_bytes, 453 + show_ref_action(__entry->action), 454 + show_root_type(__entry->parent), 455 + show_root_type(__entry->ref_root), 456 + __entry->level, show_ref_type(__entry->type)) 457 + ); 458 + 459 + TRACE_EVENT(btrfs_delayed_data_ref, 460 + 461 + TP_PROTO(struct btrfs_delayed_ref_node *ref, 462 + struct btrfs_delayed_data_ref *full_ref, 463 + int action), 464 + 465 + TP_ARGS(ref, full_ref, action), 466 + 467 + TP_STRUCT__entry( 468 + __field( u64, bytenr ) 469 + __field( u64, num_bytes ) 470 + __field( int, action ) 471 + __field( u64, parent ) 472 + __field( u64, ref_root ) 473 + __field( u64, owner ) 474 + __field( u64, offset ) 475 + __field( int, type ) 476 + ), 477 + 478 + TP_fast_assign( 479 + __entry->bytenr = ref->bytenr; 480 + __entry->num_bytes = ref->num_bytes; 481 + __entry->action = action; 482 + __entry->parent = full_ref->parent; 483 + __entry->ref_root = full_ref->root; 484 + __entry->owner = full_ref->objectid; 485 + __entry->offset = full_ref->offset; 486 + __entry->type = ref->type; 487 + ), 488 + 489 + TP_printk("bytenr = %llu, num_bytes = %llu, action = %s, " 490 + "parent = %llu(%s), ref_root = %llu(%s), owner = %llu, " 491 + "offset = %llu, type = %s", 492 + (unsigned long long)__entry->bytenr, 493 + (unsigned long long)__entry->num_bytes, 494 + show_ref_action(__entry->action), 495 + show_root_type(__entry->parent), 496 + show_root_type(__entry->ref_root), 497 + (unsigned long long)__entry->owner, 498 + (unsigned long long)__entry->offset, 499 + show_ref_type(__entry->type)) 500 + ); 501 + 502 + TRACE_EVENT(btrfs_delayed_ref_head, 503 + 504 + TP_PROTO(struct btrfs_delayed_ref_node *ref, 505 + struct btrfs_delayed_ref_head *head_ref, 506 + int action), 507 + 508 + TP_ARGS(ref, head_ref, action), 509 + 510 + TP_STRUCT__entry( 511 + __field( u64, bytenr ) 512 + __field( u64, num_bytes ) 513 + __field( int, action ) 514 + __field( int, is_data ) 515 + ), 516 + 517 + TP_fast_assign( 518 + __entry->bytenr = ref->bytenr; 519 + __entry->num_bytes = ref->num_bytes; 520 + __entry->action = action; 521 + __entry->is_data = head_ref->is_data; 522 + ), 523 + 524 + TP_printk("bytenr = %llu, num_bytes = %llu, action = %s, is_data = %d", 525 + (unsigned long long)__entry->bytenr, 526 + (unsigned long long)__entry->num_bytes, 527 + show_ref_action(__entry->action), 528 + __entry->is_data) 529 + ); 530 + 531 + #define show_chunk_type(type) \ 532 + __print_flags(type, "|", \ 533 + { BTRFS_BLOCK_GROUP_DATA, "DATA" }, \ 534 + { BTRFS_BLOCK_GROUP_SYSTEM, "SYSTEM"}, \ 535 + { BTRFS_BLOCK_GROUP_METADATA, "METADATA"}, \ 536 + { BTRFS_BLOCK_GROUP_RAID0, "RAID0" }, \ 537 + { BTRFS_BLOCK_GROUP_RAID1, "RAID1" }, \ 538 + { BTRFS_BLOCK_GROUP_DUP, "DUP" }, \ 539 + { BTRFS_BLOCK_GROUP_RAID10, "RAID10"}) 540 + 541 + DECLARE_EVENT_CLASS(btrfs__chunk, 542 + 543 + TP_PROTO(struct btrfs_root *root, struct map_lookup *map, 544 + u64 offset, u64 size), 545 + 546 + TP_ARGS(root, map, offset, size), 547 + 548 + TP_STRUCT__entry( 549 + __field( int, num_stripes ) 550 + __field( u64, type ) 551 + __field( int, sub_stripes ) 552 + __field( u64, offset ) 553 + __field( u64, size ) 554 + __field( u64, root_objectid ) 555 + ), 556 + 557 + TP_fast_assign( 558 + __entry->num_stripes = map->num_stripes; 559 + __entry->type = map->type; 560 + __entry->sub_stripes = map->sub_stripes; 561 + __entry->offset = offset; 562 + __entry->size = size; 563 + __entry->root_objectid = root->root_key.objectid; 564 + ), 565 + 566 + TP_printk("root = %llu(%s), offset = %llu, size = %llu, " 567 + "num_stripes = %d, sub_stripes = %d, type = %s", 568 + show_root_type(__entry->root_objectid), 569 + (unsigned long long)__entry->offset, 570 + (unsigned long long)__entry->size, 571 + __entry->num_stripes, __entry->sub_stripes, 572 + show_chunk_type(__entry->type)) 573 + ); 574 + 575 + DEFINE_EVENT(btrfs__chunk, btrfs_chunk_alloc, 576 + 577 + TP_PROTO(struct btrfs_root *root, struct map_lookup *map, 578 + u64 offset, u64 size), 579 + 580 + TP_ARGS(root, map, offset, size) 581 + ); 582 + 583 + DEFINE_EVENT(btrfs__chunk, btrfs_chunk_free, 584 + 585 + TP_PROTO(struct btrfs_root *root, struct map_lookup *map, 586 + u64 offset, u64 size), 587 + 588 + TP_ARGS(root, map, offset, size) 589 + ); 590 + 591 + TRACE_EVENT(btrfs_cow_block, 592 + 593 + TP_PROTO(struct btrfs_root *root, struct extent_buffer *buf, 594 + struct extent_buffer *cow), 595 + 596 + TP_ARGS(root, buf, cow), 597 + 598 + TP_STRUCT__entry( 599 + __field( u64, root_objectid ) 600 + __field( u64, buf_start ) 601 + __field( int, refs ) 602 + __field( u64, cow_start ) 603 + __field( int, buf_level ) 604 + __field( int, cow_level ) 605 + ), 606 + 607 + TP_fast_assign( 608 + __entry->root_objectid = root->root_key.objectid; 609 + __entry->buf_start = buf->start; 610 + __entry->refs = atomic_read(&buf->refs); 611 + __entry->cow_start = cow->start; 612 + __entry->buf_level = btrfs_header_level(buf); 613 + __entry->cow_level = btrfs_header_level(cow); 614 + ), 615 + 616 + TP_printk("root = %llu(%s), refs = %d, orig_buf = %llu " 617 + "(orig_level = %d), cow_buf = %llu (cow_level = %d)", 618 + show_root_type(__entry->root_objectid), 619 + __entry->refs, 620 + (unsigned long long)__entry->buf_start, 621 + __entry->buf_level, 622 + (unsigned long long)__entry->cow_start, 623 + __entry->cow_level) 624 + ); 625 + 626 + DECLARE_EVENT_CLASS(btrfs__reserved_extent, 627 + 628 + TP_PROTO(struct btrfs_root *root, u64 start, u64 len), 629 + 630 + TP_ARGS(root, start, len), 631 + 632 + TP_STRUCT__entry( 633 + __field( u64, root_objectid ) 634 + __field( u64, start ) 635 + __field( u64, len ) 636 + ), 637 + 638 + TP_fast_assign( 639 + __entry->root_objectid = root->root_key.objectid; 640 + __entry->start = start; 641 + __entry->len = len; 642 + ), 643 + 644 + TP_printk("root = %llu(%s), start = %llu, len = %llu", 645 + show_root_type(__entry->root_objectid), 646 + (unsigned long long)__entry->start, 647 + (unsigned long long)__entry->len) 648 + ); 649 + 650 + DEFINE_EVENT(btrfs__reserved_extent, btrfs_reserved_extent_alloc, 651 + 652 + TP_PROTO(struct btrfs_root *root, u64 start, u64 len), 653 + 654 + TP_ARGS(root, start, len) 655 + ); 656 + 657 + DEFINE_EVENT(btrfs__reserved_extent, btrfs_reserved_extent_free, 658 + 659 + TP_PROTO(struct btrfs_root *root, u64 start, u64 len), 660 + 661 + TP_ARGS(root, start, len) 662 + ); 663 + 664 + #endif /* _TRACE_BTRFS_H */ 665 + 666 + /* This part must be outside protection */ 667 + #include <trace/define_trace.h>