Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable

* git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable: (30 commits)
Btrfs: fix the inode ref searches done by btrfs_search_path_in_tree
Btrfs: allow treeid==0 in the inode lookup ioctl
Btrfs: return keys for large items to the search ioctl
Btrfs: fix key checks and advance in the search ioctl
Btrfs: buffer results in the space_info ioctl
Btrfs: use __u64 types in ioctl.h
Btrfs: fix search_ioctl key advance
Btrfs: fix gfp flags masking in the compression code
Btrfs: don't look at bio flags after submit_bio
btrfs: using btrfs_stack_device_id() get devid
btrfs: use memparse
Btrfs: add a "df" ioctl for btrfs
Btrfs: cache the extent state everywhere we possibly can V2
Btrfs: cache ordered extent when completing io
Btrfs: cache extent state in find_delalloc_range
Btrfs: change the ordered tree to use a spinlock instead of a mutex
Btrfs: finish read pages in the order they are submitted
btrfs: fix btrfs_mkdir goto for no free objectids
Btrfs: flush data on snapshot creation
Btrfs: make df be a little bit more understandable
...

+1226 -230
+5
fs/btrfs/btrfs_inode.h
··· 153 unsigned ordered_data_close:1; 154 unsigned dummy_inode:1; 155 156 struct inode vfs_inode; 157 }; 158
··· 153 unsigned ordered_data_close:1; 154 unsigned dummy_inode:1; 155 156 + /* 157 + * always compress this one file 158 + */ 159 + unsigned force_compress:1; 160 + 161 struct inode vfs_inode; 162 }; 163
+1 -1
fs/btrfs/compression.c
··· 478 goto next; 479 } 480 481 - page = alloc_page(mapping_gfp_mask(mapping) | GFP_NOFS); 482 if (!page) 483 break; 484
··· 478 goto next; 479 } 480 481 + page = alloc_page(mapping_gfp_mask(mapping) & ~__GFP_FS); 482 if (!page) 483 break; 484
+7 -6
fs/btrfs/ctree.h
··· 373 * ones specified below then we will fail to mount 374 */ 375 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF (1ULL << 0) 376 377 #define BTRFS_FEATURE_COMPAT_SUPP 0ULL 378 #define BTRFS_FEATURE_COMPAT_RO_SUPP 0ULL 379 #define BTRFS_FEATURE_INCOMPAT_SUPP \ 380 - BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF 381 382 /* 383 * A leaf is full of items. offset and size tell us where to find ··· 1184 #define BTRFS_INODE_NOATIME (1 << 9) 1185 #define BTRFS_INODE_DIRSYNC (1 << 10) 1186 1187 - 1188 /* some macros to generate set/get funcs for the struct fields. This 1189 * assumes there is a lefoo_to_cpu for every type, so lets make a simple 1190 * one for u8: ··· 1843 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block, 1844 compat_flags, 64); 1845 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block, 1846 - compat_flags, 64); 1847 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block, 1848 incompat_flags, 64); 1849 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block, ··· 2311 u32 min_type); 2312 2313 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput); 2314 - int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end); 2315 int btrfs_writepages(struct address_space *mapping, 2316 struct writeback_control *wbc); 2317 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, ··· 2337 void btrfs_destroy_cachep(void); 2338 long btrfs_ioctl_trans_end(struct file *file); 2339 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, 2340 - struct btrfs_root *root); 2341 int btrfs_commit_write(struct file *file, struct page *page, 2342 unsigned from, unsigned to); 2343 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ··· 2388 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 2389 2390 /* super.c */ 2391 - u64 btrfs_parse_size(char *str); 2392 int btrfs_parse_options(struct btrfs_root *root, char *options); 2393 int btrfs_sync_fs(struct super_block *sb, int wait); 2394
··· 373 * ones specified below then we will fail to mount 374 */ 375 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF (1ULL << 0) 376 + #define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL (2ULL << 0) 377 378 #define BTRFS_FEATURE_COMPAT_SUPP 0ULL 379 #define BTRFS_FEATURE_COMPAT_RO_SUPP 0ULL 380 #define BTRFS_FEATURE_INCOMPAT_SUPP \ 381 + (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \ 382 + BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL) 383 384 /* 385 * A leaf is full of items. offset and size tell us where to find ··· 1182 #define BTRFS_INODE_NOATIME (1 << 9) 1183 #define BTRFS_INODE_DIRSYNC (1 << 10) 1184 1185 /* some macros to generate set/get funcs for the struct fields. This 1186 * assumes there is a lefoo_to_cpu for every type, so lets make a simple 1187 * one for u8: ··· 1842 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block, 1843 compat_flags, 64); 1844 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block, 1845 + compat_ro_flags, 64); 1846 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block, 1847 incompat_flags, 64); 1848 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block, ··· 2310 u32 min_type); 2311 2312 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput); 2313 + int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, 2314 + struct extent_state **cached_state); 2315 int btrfs_writepages(struct address_space *mapping, 2316 struct writeback_control *wbc); 2317 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, ··· 2335 void btrfs_destroy_cachep(void); 2336 long btrfs_ioctl_trans_end(struct file *file); 2337 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, 2338 + struct btrfs_root *root, int *was_new); 2339 int btrfs_commit_write(struct file *file, struct page *page, 2340 unsigned from, unsigned to); 2341 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, ··· 2386 ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 2387 2388 /* super.c */ 2389 int btrfs_parse_options(struct btrfs_root *root, char *options); 2390 int btrfs_sync_fs(struct super_block *sb, int wait); 2391
+9 -6
fs/btrfs/disk-io.c
··· 263 static int verify_parent_transid(struct extent_io_tree *io_tree, 264 struct extent_buffer *eb, u64 parent_transid) 265 { 266 int ret; 267 268 if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 269 return 0; 270 271 - lock_extent(io_tree, eb->start, eb->start + eb->len - 1, GFP_NOFS); 272 - if (extent_buffer_uptodate(io_tree, eb) && 273 btrfs_header_generation(eb) == parent_transid) { 274 ret = 0; 275 goto out; ··· 284 (unsigned long long)btrfs_header_generation(eb)); 285 } 286 ret = 1; 287 - clear_extent_buffer_uptodate(io_tree, eb); 288 out: 289 - unlock_extent(io_tree, eb->start, eb->start + eb->len - 1, 290 - GFP_NOFS); 291 return ret; 292 } 293 ··· 2499 int ret; 2500 struct inode *btree_inode = buf->first_page->mapping->host; 2501 2502 - ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf); 2503 if (!ret) 2504 return ret; 2505
··· 263 static int verify_parent_transid(struct extent_io_tree *io_tree, 264 struct extent_buffer *eb, u64 parent_transid) 265 { 266 + struct extent_state *cached_state = NULL; 267 int ret; 268 269 if (!parent_transid || btrfs_header_generation(eb) == parent_transid) 270 return 0; 271 272 + lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 273 + 0, &cached_state, GFP_NOFS); 274 + if (extent_buffer_uptodate(io_tree, eb, cached_state) && 275 btrfs_header_generation(eb) == parent_transid) { 276 ret = 0; 277 goto out; ··· 282 (unsigned long long)btrfs_header_generation(eb)); 283 } 284 ret = 1; 285 + clear_extent_buffer_uptodate(io_tree, eb, &cached_state); 286 out: 287 + unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 288 + &cached_state, GFP_NOFS); 289 return ret; 290 } 291 ··· 2497 int ret; 2498 struct inode *btree_inode = buf->first_page->mapping->host; 2499 2500 + ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf, 2501 + NULL); 2502 if (!ret) 2503 return ret; 2504
+2 -2
fs/btrfs/export.c
··· 95 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 96 key.offset = 0; 97 98 - inode = btrfs_iget(sb, &key, root); 99 if (IS_ERR(inode)) { 100 err = PTR_ERR(inode); 101 goto fail; ··· 223 224 key.type = BTRFS_INODE_ITEM_KEY; 225 key.offset = 0; 226 - dentry = d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root)); 227 if (!IS_ERR(dentry)) 228 dentry->d_op = &btrfs_dentry_operations; 229 return dentry;
··· 95 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); 96 key.offset = 0; 97 98 + inode = btrfs_iget(sb, &key, root, NULL); 99 if (IS_ERR(inode)) { 100 err = PTR_ERR(inode); 101 goto fail; ··· 223 224 key.type = BTRFS_INODE_ITEM_KEY; 225 key.offset = 0; 226 + dentry = d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL)); 227 if (!IS_ERR(dentry)) 228 dentry->d_op = &btrfs_dentry_operations; 229 return dentry;
+7 -4
fs/btrfs/extent-tree.c
··· 6561 struct btrfs_key key; 6562 struct inode *inode = NULL; 6563 struct btrfs_file_extent_item *fi; 6564 u64 num_bytes; 6565 u64 skip_objectid = 0; 6566 u32 nritems; ··· 6590 } 6591 num_bytes = btrfs_file_extent_num_bytes(leaf, fi); 6592 6593 - lock_extent(&BTRFS_I(inode)->io_tree, key.offset, 6594 - key.offset + num_bytes - 1, GFP_NOFS); 6595 btrfs_drop_extent_cache(inode, key.offset, 6596 key.offset + num_bytes - 1, 1); 6597 - unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, 6598 - key.offset + num_bytes - 1, GFP_NOFS); 6599 cond_resched(); 6600 } 6601 iput(inode);
··· 6561 struct btrfs_key key; 6562 struct inode *inode = NULL; 6563 struct btrfs_file_extent_item *fi; 6564 + struct extent_state *cached_state = NULL; 6565 u64 num_bytes; 6566 u64 skip_objectid = 0; 6567 u32 nritems; ··· 6589 } 6590 num_bytes = btrfs_file_extent_num_bytes(leaf, fi); 6591 6592 + lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, 6593 + key.offset + num_bytes - 1, 0, &cached_state, 6594 + GFP_NOFS); 6595 btrfs_drop_extent_cache(inode, key.offset, 6596 key.offset + num_bytes - 1, 1); 6597 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, 6598 + key.offset + num_bytes - 1, &cached_state, 6599 + GFP_NOFS); 6600 cond_resched(); 6601 } 6602 iput(inode);
+50 -29
fs/btrfs/extent_io.c
··· 513 u64 last_end; 514 int err; 515 int set = 0; 516 517 again: 518 if (!prealloc && (mask & __GFP_WAIT)) { 519 prealloc = alloc_extent_state(mask); ··· 527 spin_lock(&tree->lock); 528 if (cached_state) { 529 cached = *cached_state; 530 - *cached_state = NULL; 531 - cached_state = NULL; 532 if (cached && cached->tree && cached->start == start) { 533 - atomic_dec(&cached->refs); 534 state = cached; 535 goto hit_next; 536 } 537 - free_extent_state(cached); 538 } 539 /* 540 * this search will find the extents that end after ··· 955 } 956 957 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, 958 - gfp_t mask) 959 { 960 return set_extent_bit(tree, start, end, 961 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE, 962 - 0, NULL, NULL, mask); 963 } 964 965 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, ··· 993 } 994 995 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, 996 - u64 end, gfp_t mask) 997 { 998 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, 999 - NULL, mask); 1000 } 1001 1002 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) ··· 1181 * 1 is returned if we find something, 0 if nothing was in the tree 1182 */ 1183 static noinline u64 find_delalloc_range(struct extent_io_tree *tree, 1184 - u64 *start, u64 *end, u64 max_bytes) 1185 { 1186 struct rb_node *node; 1187 struct extent_state *state; ··· 1214 *end = state->end; 1215 goto out; 1216 } 1217 - if (!found) 1218 *start = state->start; 1219 found++; 1220 *end = state->end; 1221 cur_start = state->end + 1; ··· 1350 delalloc_start = *start; 1351 delalloc_end = 0; 1352 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, 1353 - max_bytes); 1354 if (!found || delalloc_end <= *start) { 1355 *start = delalloc_start; 1356 *end = delalloc_end; 1357 return found; 1358 } 1359 ··· 1737 } 1738 1739 if (!uptodate) { 1740 - clear_extent_uptodate(tree, start, end, GFP_NOFS); 1741 ClearPageUptodate(page); 1742 SetPageError(page); 1743 } ··· 1765 static void end_bio_extent_readpage(struct bio *bio, int err) 1766 { 1767 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1768 - struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1; 1769 struct extent_io_tree *tree; 1770 u64 start; 1771 u64 end; ··· 1789 else 1790 whole_page = 0; 1791 1792 - if (--bvec >= bio->bi_io_vec) 1793 prefetchw(&bvec->bv_page->flags); 1794 1795 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { ··· 1834 } 1835 check_page_locked(tree, page); 1836 } 1837 - } while (bvec >= bio->bi_io_vec); 1838 1839 bio_put(bio); 1840 } ··· 2720 int extent_invalidatepage(struct extent_io_tree *tree, 2721 struct page *page, unsigned long offset) 2722 { 2723 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); 2724 u64 end = start + PAGE_CACHE_SIZE - 1; 2725 size_t blocksize = page->mapping->host->i_sb->s_blocksize; ··· 2729 if (start > end) 2730 return 0; 2731 2732 - lock_extent(tree, start, end, GFP_NOFS); 2733 wait_on_page_writeback(page); 2734 clear_extent_bit(tree, start, end, 2735 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 2736 EXTENT_DO_ACCOUNTING, 2737 - 1, 1, NULL, GFP_NOFS); 2738 return 0; 2739 } 2740 ··· 2937 get_extent_t *get_extent) 2938 { 2939 struct inode *inode = mapping->host; 2940 u64 start = iblock << inode->i_blkbits; 2941 sector_t sector = 0; 2942 size_t blksize = (1 << inode->i_blkbits); 2943 struct extent_map *em; 2944 2945 - lock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1, 2946 - GFP_NOFS); 2947 em = get_extent(inode, NULL, 0, start, blksize, 0); 2948 - unlock_extent(&BTRFS_I(inode)->io_tree, start, start + blksize - 1, 2949 - GFP_NOFS); 2950 if (!em || IS_ERR(em)) 2951 return 0; 2952 ··· 2969 u32 flags = 0; 2970 u64 disko = 0; 2971 struct extent_map *em = NULL; 2972 int end = 0; 2973 u64 em_start = 0, em_len = 0; 2974 unsigned long emflags; ··· 2978 if (len == 0) 2979 return -EINVAL; 2980 2981 - lock_extent(&BTRFS_I(inode)->io_tree, start, start + len, 2982 - GFP_NOFS); 2983 em = get_extent(inode, NULL, 0, off, max - off, 0); 2984 if (!em) 2985 goto out; ··· 3042 out_free: 3043 free_extent_map(em); 3044 out: 3045 - unlock_extent(&BTRFS_I(inode)->io_tree, start, start + len, 3046 - GFP_NOFS); 3047 return ret; 3048 } 3049 ··· 3283 } 3284 3285 int clear_extent_buffer_uptodate(struct extent_io_tree *tree, 3286 - struct extent_buffer *eb) 3287 { 3288 unsigned long i; 3289 struct page *page; ··· 3294 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 3295 3296 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, 3297 - GFP_NOFS); 3298 for (i = 0; i < num_pages; i++) { 3299 page = extent_buffer_page(eb, i); 3300 if (page) ··· 3354 } 3355 3356 int extent_buffer_uptodate(struct extent_io_tree *tree, 3357 - struct extent_buffer *eb) 3358 { 3359 int ret = 0; 3360 unsigned long num_pages; ··· 3367 return 1; 3368 3369 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, 3370 - EXTENT_UPTODATE, 1, NULL); 3371 if (ret) 3372 return ret; 3373
··· 513 u64 last_end; 514 int err; 515 int set = 0; 516 + int clear = 0; 517 518 + if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY)) 519 + clear = 1; 520 again: 521 if (!prealloc && (mask & __GFP_WAIT)) { 522 prealloc = alloc_extent_state(mask); ··· 524 spin_lock(&tree->lock); 525 if (cached_state) { 526 cached = *cached_state; 527 + 528 + if (clear) { 529 + *cached_state = NULL; 530 + cached_state = NULL; 531 + } 532 + 533 if (cached && cached->tree && cached->start == start) { 534 + if (clear) 535 + atomic_dec(&cached->refs); 536 state = cached; 537 goto hit_next; 538 } 539 + if (clear) 540 + free_extent_state(cached); 541 } 542 /* 543 * this search will find the extents that end after ··· 946 } 947 948 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, 949 + struct extent_state **cached_state, gfp_t mask) 950 { 951 return set_extent_bit(tree, start, end, 952 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE, 953 + 0, NULL, cached_state, mask); 954 } 955 956 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end, ··· 984 } 985 986 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, 987 + u64 end, struct extent_state **cached_state, 988 + gfp_t mask) 989 { 990 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, 991 + cached_state, mask); 992 } 993 994 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end) ··· 1171 * 1 is returned if we find something, 0 if nothing was in the tree 1172 */ 1173 static noinline u64 find_delalloc_range(struct extent_io_tree *tree, 1174 + u64 *start, u64 *end, u64 max_bytes, 1175 + struct extent_state **cached_state) 1176 { 1177 struct rb_node *node; 1178 struct extent_state *state; ··· 1203 *end = state->end; 1204 goto out; 1205 } 1206 + if (!found) { 1207 *start = state->start; 1208 + *cached_state = state; 1209 + atomic_inc(&state->refs); 1210 + } 1211 found++; 1212 *end = state->end; 1213 cur_start = state->end + 1; ··· 1336 delalloc_start = *start; 1337 delalloc_end = 0; 1338 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end, 1339 + max_bytes, &cached_state); 1340 if (!found || delalloc_end <= *start) { 1341 *start = delalloc_start; 1342 *end = delalloc_end; 1343 + free_extent_state(cached_state); 1344 return found; 1345 } 1346 ··· 1722 } 1723 1724 if (!uptodate) { 1725 + clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS); 1726 ClearPageUptodate(page); 1727 SetPageError(page); 1728 } ··· 1750 static void end_bio_extent_readpage(struct bio *bio, int err) 1751 { 1752 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); 1753 + struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; 1754 + struct bio_vec *bvec = bio->bi_io_vec; 1755 struct extent_io_tree *tree; 1756 u64 start; 1757 u64 end; ··· 1773 else 1774 whole_page = 0; 1775 1776 + if (++bvec <= bvec_end) 1777 prefetchw(&bvec->bv_page->flags); 1778 1779 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) { ··· 1818 } 1819 check_page_locked(tree, page); 1820 } 1821 + } while (bvec <= bvec_end); 1822 1823 bio_put(bio); 1824 } ··· 2704 int extent_invalidatepage(struct extent_io_tree *tree, 2705 struct page *page, unsigned long offset) 2706 { 2707 + struct extent_state *cached_state = NULL; 2708 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT); 2709 u64 end = start + PAGE_CACHE_SIZE - 1; 2710 size_t blocksize = page->mapping->host->i_sb->s_blocksize; ··· 2712 if (start > end) 2713 return 0; 2714 2715 + lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS); 2716 wait_on_page_writeback(page); 2717 clear_extent_bit(tree, start, end, 2718 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 2719 EXTENT_DO_ACCOUNTING, 2720 + 1, 1, &cached_state, GFP_NOFS); 2721 return 0; 2722 } 2723 ··· 2920 get_extent_t *get_extent) 2921 { 2922 struct inode *inode = mapping->host; 2923 + struct extent_state *cached_state = NULL; 2924 u64 start = iblock << inode->i_blkbits; 2925 sector_t sector = 0; 2926 size_t blksize = (1 << inode->i_blkbits); 2927 struct extent_map *em; 2928 2929 + lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1, 2930 + 0, &cached_state, GFP_NOFS); 2931 em = get_extent(inode, NULL, 0, start, blksize, 0); 2932 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, 2933 + start + blksize - 1, &cached_state, GFP_NOFS); 2934 if (!em || IS_ERR(em)) 2935 return 0; 2936 ··· 2951 u32 flags = 0; 2952 u64 disko = 0; 2953 struct extent_map *em = NULL; 2954 + struct extent_state *cached_state = NULL; 2955 int end = 0; 2956 u64 em_start = 0, em_len = 0; 2957 unsigned long emflags; ··· 2959 if (len == 0) 2960 return -EINVAL; 2961 2962 + lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0, 2963 + &cached_state, GFP_NOFS); 2964 em = get_extent(inode, NULL, 0, off, max - off, 0); 2965 if (!em) 2966 goto out; ··· 3023 out_free: 3024 free_extent_map(em); 3025 out: 3026 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len, 3027 + &cached_state, GFP_NOFS); 3028 return ret; 3029 } 3030 ··· 3264 } 3265 3266 int clear_extent_buffer_uptodate(struct extent_io_tree *tree, 3267 + struct extent_buffer *eb, 3268 + struct extent_state **cached_state) 3269 { 3270 unsigned long i; 3271 struct page *page; ··· 3274 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 3275 3276 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1, 3277 + cached_state, GFP_NOFS); 3278 for (i = 0; i < num_pages; i++) { 3279 page = extent_buffer_page(eb, i); 3280 if (page) ··· 3334 } 3335 3336 int extent_buffer_uptodate(struct extent_io_tree *tree, 3337 + struct extent_buffer *eb, 3338 + struct extent_state *cached_state) 3339 { 3340 int ret = 0; 3341 unsigned long num_pages; ··· 3346 return 1; 3347 3348 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1, 3349 + EXTENT_UPTODATE, 1, cached_state); 3350 if (ret) 3351 return ret; 3352
+7 -3
fs/btrfs/extent_io.h
··· 163 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 164 int bits, struct extent_state **cached, gfp_t mask); 165 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask); 166 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 167 gfp_t mask); 168 int extent_read_full_page(struct extent_io_tree *tree, struct page *page, ··· 198 int clear_extent_ordered_metadata(struct extent_io_tree *tree, u64 start, 199 u64 end, gfp_t mask); 200 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, 201 - gfp_t mask); 202 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, 203 gfp_t mask); 204 int find_first_extent_bit(struct extent_io_tree *tree, u64 start, ··· 283 int set_extent_buffer_uptodate(struct extent_io_tree *tree, 284 struct extent_buffer *eb); 285 int clear_extent_buffer_uptodate(struct extent_io_tree *tree, 286 - struct extent_buffer *eb); 287 int extent_buffer_uptodate(struct extent_io_tree *tree, 288 - struct extent_buffer *eb); 289 int map_extent_buffer(struct extent_buffer *eb, unsigned long offset, 290 unsigned long min_len, char **token, char **map, 291 unsigned long *map_start,
··· 163 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, 164 int bits, struct extent_state **cached, gfp_t mask); 165 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask); 166 + int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end, 167 + struct extent_state **cached, gfp_t mask); 168 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end, 169 gfp_t mask); 170 int extent_read_full_page(struct extent_io_tree *tree, struct page *page, ··· 196 int clear_extent_ordered_metadata(struct extent_io_tree *tree, u64 start, 197 u64 end, gfp_t mask); 198 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end, 199 + struct extent_state **cached_state, gfp_t mask); 200 int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end, 201 gfp_t mask); 202 int find_first_extent_bit(struct extent_io_tree *tree, u64 start, ··· 281 int set_extent_buffer_uptodate(struct extent_io_tree *tree, 282 struct extent_buffer *eb); 283 int clear_extent_buffer_uptodate(struct extent_io_tree *tree, 284 + struct extent_buffer *eb, 285 + struct extent_state **cached_state); 286 int extent_buffer_uptodate(struct extent_io_tree *tree, 287 + struct extent_buffer *eb, 288 + struct extent_state *cached_state); 289 int map_extent_buffer(struct extent_buffer *eb, unsigned long offset, 290 unsigned long min_len, char **token, char **map, 291 unsigned long *map_start,
+14 -9
fs/btrfs/file.c
··· 123 root->sectorsize - 1) & ~((u64)root->sectorsize - 1); 124 125 end_of_last_block = start_pos + num_bytes - 1; 126 - err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block); 127 if (err) 128 return err; 129 ··· 754 loff_t pos, unsigned long first_index, 755 unsigned long last_index, size_t write_bytes) 756 { 757 int i; 758 unsigned long index = pos >> PAGE_CACHE_SHIFT; 759 struct inode *inode = fdentry(file)->d_inode; ··· 783 } 784 if (start_pos < inode->i_size) { 785 struct btrfs_ordered_extent *ordered; 786 - lock_extent(&BTRFS_I(inode)->io_tree, 787 - start_pos, last_pos - 1, GFP_NOFS); 788 ordered = btrfs_lookup_first_ordered_extent(inode, 789 last_pos - 1); 790 if (ordered && 791 ordered->file_offset + ordered->len > start_pos && 792 ordered->file_offset < last_pos) { 793 btrfs_put_ordered_extent(ordered); 794 - unlock_extent(&BTRFS_I(inode)->io_tree, 795 - start_pos, last_pos - 1, GFP_NOFS); 796 for (i = 0; i < num_pages; i++) { 797 unlock_page(pages[i]); 798 page_cache_release(pages[i]); ··· 806 if (ordered) 807 btrfs_put_ordered_extent(ordered); 808 809 - clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos, 810 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 811 - EXTENT_DO_ACCOUNTING, 812 GFP_NOFS); 813 - unlock_extent(&BTRFS_I(inode)->io_tree, 814 - start_pos, last_pos - 1, GFP_NOFS); 815 } 816 for (i = 0; i < num_pages; i++) { 817 clear_page_dirty_for_io(pages[i]);
··· 123 root->sectorsize - 1) & ~((u64)root->sectorsize - 1); 124 125 end_of_last_block = start_pos + num_bytes - 1; 126 + err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 127 + NULL); 128 if (err) 129 return err; 130 ··· 753 loff_t pos, unsigned long first_index, 754 unsigned long last_index, size_t write_bytes) 755 { 756 + struct extent_state *cached_state = NULL; 757 int i; 758 unsigned long index = pos >> PAGE_CACHE_SHIFT; 759 struct inode *inode = fdentry(file)->d_inode; ··· 781 } 782 if (start_pos < inode->i_size) { 783 struct btrfs_ordered_extent *ordered; 784 + lock_extent_bits(&BTRFS_I(inode)->io_tree, 785 + start_pos, last_pos - 1, 0, &cached_state, 786 + GFP_NOFS); 787 ordered = btrfs_lookup_first_ordered_extent(inode, 788 last_pos - 1); 789 if (ordered && 790 ordered->file_offset + ordered->len > start_pos && 791 ordered->file_offset < last_pos) { 792 btrfs_put_ordered_extent(ordered); 793 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, 794 + start_pos, last_pos - 1, 795 + &cached_state, GFP_NOFS); 796 for (i = 0; i < num_pages; i++) { 797 unlock_page(pages[i]); 798 page_cache_release(pages[i]); ··· 802 if (ordered) 803 btrfs_put_ordered_extent(ordered); 804 805 + clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos, 806 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC | 807 + EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, 808 GFP_NOFS); 809 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, 810 + start_pos, last_pos - 1, &cached_state, 811 + GFP_NOFS); 812 } 813 for (i = 0; i < num_pages; i++) { 814 clear_page_dirty_for_io(pages[i]);
+86 -53
fs/btrfs/inode.c
··· 379 * change at any time if we discover bad compression ratios. 380 */ 381 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && 382 - btrfs_test_opt(root, COMPRESS)) { 383 WARN_ON(pages); 384 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 385 ··· 484 nr_pages_ret = 0; 485 486 /* flag the file so we don't compress in the future */ 487 - if (!btrfs_test_opt(root, FORCE_COMPRESS)) 488 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 489 } 490 if (will_compress) { 491 *num_added += 1; ··· 573 unsigned long nr_written = 0; 574 575 lock_extent(io_tree, async_extent->start, 576 - async_extent->start + 577 - async_extent->ram_size - 1, GFP_NOFS); 578 579 /* allocate blocks */ 580 ret = cow_file_range(inode, async_cow->locked_page, ··· 1214 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) 1215 ret = run_delalloc_nocow(inode, locked_page, start, end, 1216 page_started, 0, nr_written); 1217 - else if (!btrfs_test_opt(root, COMPRESS)) 1218 ret = cow_file_range(inode, locked_page, start, end, 1219 page_started, nr_written, 1); 1220 else ··· 1512 return 0; 1513 } 1514 1515 - int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) 1516 { 1517 if ((end & (PAGE_CACHE_SIZE - 1)) == 0) 1518 WARN_ON(1); 1519 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, 1520 - GFP_NOFS); 1521 } 1522 1523 /* see btrfs_writepage_start_hook for details on why this is required */ ··· 1531 { 1532 struct btrfs_writepage_fixup *fixup; 1533 struct btrfs_ordered_extent *ordered; 1534 struct page *page; 1535 struct inode *inode; 1536 u64 page_start; ··· 1550 page_start = page_offset(page); 1551 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; 1552 1553 - lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); 1554 1555 /* already ordered? We're done */ 1556 if (PagePrivate2(page)) ··· 1559 1560 ordered = btrfs_lookup_ordered_extent(inode, page_start); 1561 if (ordered) { 1562 - unlock_extent(&BTRFS_I(inode)->io_tree, page_start, 1563 - page_end, GFP_NOFS); 1564 unlock_page(page); 1565 btrfs_start_ordered_extent(inode, ordered, 1); 1566 goto again; 1567 } 1568 1569 - btrfs_set_extent_delalloc(inode, page_start, page_end); 1570 ClearPageChecked(page); 1571 out: 1572 - unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); 1573 out_page: 1574 unlock_page(page); 1575 page_cache_release(page); ··· 1699 struct btrfs_trans_handle *trans; 1700 struct btrfs_ordered_extent *ordered_extent = NULL; 1701 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 1702 int compressed = 0; 1703 int ret; 1704 1705 - ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); 1706 if (!ret) 1707 return 0; 1708 - 1709 - ordered_extent = btrfs_lookup_ordered_extent(inode, start); 1710 BUG_ON(!ordered_extent); 1711 1712 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { ··· 1721 goto out; 1722 } 1723 1724 - lock_extent(io_tree, ordered_extent->file_offset, 1725 - ordered_extent->file_offset + ordered_extent->len - 1, 1726 - GFP_NOFS); 1727 1728 trans = btrfs_join_transaction(root, 1); 1729 ··· 1750 ordered_extent->len); 1751 BUG_ON(ret); 1752 } 1753 - unlock_extent(io_tree, ordered_extent->file_offset, 1754 - ordered_extent->file_offset + ordered_extent->len - 1, 1755 - GFP_NOFS); 1756 add_pending_csums(trans, inode, ordered_extent->file_offset, 1757 &ordered_extent->list); 1758 ··· 2162 found_key.objectid = found_key.offset; 2163 found_key.type = BTRFS_INODE_ITEM_KEY; 2164 found_key.offset = 0; 2165 - inode = btrfs_iget(root->fs_info->sb, &found_key, root); 2166 if (IS_ERR(inode)) 2167 break; 2168 ··· 3090 struct btrfs_root *root = BTRFS_I(inode)->root; 3091 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3092 struct btrfs_ordered_extent *ordered; 3093 char *kaddr; 3094 u32 blocksize = root->sectorsize; 3095 pgoff_t index = from >> PAGE_CACHE_SHIFT; ··· 3137 } 3138 wait_on_page_writeback(page); 3139 3140 - lock_extent(io_tree, page_start, page_end, GFP_NOFS); 3141 set_page_extent_mapped(page); 3142 3143 ordered = btrfs_lookup_ordered_extent(inode, page_start); 3144 if (ordered) { 3145 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3146 unlock_page(page); 3147 page_cache_release(page); 3148 btrfs_start_ordered_extent(inode, ordered, 1); ··· 3152 goto again; 3153 } 3154 3155 - clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 3156 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, 3157 - GFP_NOFS); 3158 3159 - ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 3160 if (ret) { 3161 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3162 goto out_unlock; 3163 } 3164 ··· 3173 } 3174 ClearPageChecked(page); 3175 set_page_dirty(page); 3176 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 3177 3178 out_unlock: 3179 if (ret) ··· 3192 struct btrfs_root *root = BTRFS_I(inode)->root; 3193 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3194 struct extent_map *em; 3195 u64 mask = root->sectorsize - 1; 3196 u64 hole_start = (inode->i_size + mask) & ~mask; 3197 u64 block_end = (size + mask) & ~mask; ··· 3208 struct btrfs_ordered_extent *ordered; 3209 btrfs_wait_ordered_range(inode, hole_start, 3210 block_end - hole_start); 3211 - lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 3212 ordered = btrfs_lookup_ordered_extent(inode, hole_start); 3213 if (!ordered) 3214 break; 3215 - unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 3216 btrfs_put_ordered_extent(ordered); 3217 } 3218 ··· 3259 break; 3260 } 3261 3262 - unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 3263 return err; 3264 } 3265 ··· 3658 bi->index_cnt = (u64)-1; 3659 bi->last_unlink_trans = 0; 3660 bi->ordered_data_close = 0; 3661 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); 3662 extent_io_tree_init(&BTRFS_I(inode)->io_tree, 3663 inode->i_mapping, GFP_NOFS); ··· 3707 * Returns in *is_new if the inode was read from disk 3708 */ 3709 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, 3710 - struct btrfs_root *root) 3711 { 3712 struct inode *inode; 3713 ··· 3722 3723 inode_tree_add(inode); 3724 unlock_new_inode(inode); 3725 } 3726 3727 return inode; ··· 3776 return NULL; 3777 3778 if (location.type == BTRFS_INODE_ITEM_KEY) { 3779 - inode = btrfs_iget(dir->i_sb, &location, root); 3780 return inode; 3781 } 3782 ··· 3791 else 3792 inode = new_simple_dir(dir->i_sb, &location, sub_root); 3793 } else { 3794 - inode = btrfs_iget(dir->i_sb, &location, sub_root); 3795 } 3796 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 3797 ··· 4523 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4524 if (err) { 4525 err = -ENOSPC; 4526 - goto out_unlock; 4527 } 4528 4529 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, ··· 5001 { 5002 struct extent_io_tree *tree; 5003 struct btrfs_ordered_extent *ordered; 5004 u64 page_start = page_offset(page); 5005 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 5006 ··· 5020 btrfs_releasepage(page, GFP_NOFS); 5021 return; 5022 } 5023 - lock_extent(tree, page_start, page_end, GFP_NOFS); 5024 ordered = btrfs_lookup_ordered_extent(page->mapping->host, 5025 page_offset(page)); 5026 if (ordered) { ··· 5032 clear_extent_bit(tree, page_start, page_end, 5033 EXTENT_DIRTY | EXTENT_DELALLOC | 5034 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, 5035 - NULL, GFP_NOFS); 5036 /* 5037 * whoever cleared the private bit is responsible 5038 * for the finish_ordered_io ··· 5042 page_start, page_end); 5043 } 5044 btrfs_put_ordered_extent(ordered); 5045 - lock_extent(tree, page_start, page_end, GFP_NOFS); 5046 } 5047 clear_extent_bit(tree, page_start, page_end, 5048 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 5049 - EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS); 5050 __btrfs_releasepage(page, GFP_NOFS); 5051 5052 ClearPageChecked(page); ··· 5081 struct btrfs_root *root = BTRFS_I(inode)->root; 5082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 5083 struct btrfs_ordered_extent *ordered; 5084 char *kaddr; 5085 unsigned long zero_start; 5086 loff_t size; ··· 5120 } 5121 wait_on_page_writeback(page); 5122 5123 - lock_extent(io_tree, page_start, page_end, GFP_NOFS); 5124 set_page_extent_mapped(page); 5125 5126 /* ··· 5130 */ 5131 ordered = btrfs_lookup_ordered_extent(inode, page_start); 5132 if (ordered) { 5133 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5134 unlock_page(page); 5135 btrfs_start_ordered_extent(inode, ordered, 1); 5136 btrfs_put_ordered_extent(ordered); ··· 5145 * is probably a better way to do this, but for now keep consistent with 5146 * prepare_pages in the normal write path. 5147 */ 5148 - clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 5149 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, 5150 - GFP_NOFS); 5151 5152 - ret = btrfs_set_extent_delalloc(inode, page_start, page_end); 5153 if (ret) { 5154 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5155 ret = VM_FAULT_SIGBUS; 5156 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); 5157 goto out_unlock; ··· 5179 BTRFS_I(inode)->last_trans = root->fs_info->generation; 5180 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; 5181 5182 - unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 5183 5184 out_unlock: 5185 btrfs_unreserve_metadata_for_delalloc(root, inode, 1); ··· 5858 static long btrfs_fallocate(struct inode *inode, int mode, 5859 loff_t offset, loff_t len) 5860 { 5861 u64 cur_offset; 5862 u64 last_byte; 5863 u64 alloc_start; ··· 5897 /* the extent lock is ordered inside the running 5898 * transaction 5899 */ 5900 - lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 5901 - GFP_NOFS); 5902 ordered = btrfs_lookup_first_ordered_extent(inode, 5903 alloc_end - 1); 5904 if (ordered && 5905 ordered->file_offset + ordered->len > alloc_start && 5906 ordered->file_offset < alloc_end) { 5907 btrfs_put_ordered_extent(ordered); 5908 - unlock_extent(&BTRFS_I(inode)->io_tree, 5909 - alloc_start, locked_end, GFP_NOFS); 5910 /* 5911 * we can't wait on the range with the transaction 5912 * running or with the extent lock held ··· 5949 break; 5950 } 5951 } 5952 - unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 5953 - GFP_NOFS); 5954 5955 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode, 5956 alloc_end - alloc_start);
··· 379 * change at any time if we discover bad compression ratios. 380 */ 381 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && 382 + (btrfs_test_opt(root, COMPRESS) || 383 + (BTRFS_I(inode)->force_compress))) { 384 WARN_ON(pages); 385 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 386 ··· 483 nr_pages_ret = 0; 484 485 /* flag the file so we don't compress in the future */ 486 + if (!btrfs_test_opt(root, FORCE_COMPRESS) && 487 + !(BTRFS_I(inode)->force_compress)) { 488 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 489 + } 490 } 491 if (will_compress) { 492 *num_added += 1; ··· 570 unsigned long nr_written = 0; 571 572 lock_extent(io_tree, async_extent->start, 573 + async_extent->start + 574 + async_extent->ram_size - 1, GFP_NOFS); 575 576 /* allocate blocks */ 577 ret = cow_file_range(inode, async_cow->locked_page, ··· 1211 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) 1212 ret = run_delalloc_nocow(inode, locked_page, start, end, 1213 page_started, 0, nr_written); 1214 + else if (!btrfs_test_opt(root, COMPRESS) && 1215 + !(BTRFS_I(inode)->force_compress)) 1216 ret = cow_file_range(inode, locked_page, start, end, 1217 page_started, nr_written, 1); 1218 else ··· 1508 return 0; 1509 } 1510 1511 + int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end, 1512 + struct extent_state **cached_state) 1513 { 1514 if ((end & (PAGE_CACHE_SIZE - 1)) == 0) 1515 WARN_ON(1); 1516 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, 1517 + cached_state, GFP_NOFS); 1518 } 1519 1520 /* see btrfs_writepage_start_hook for details on why this is required */ ··· 1526 { 1527 struct btrfs_writepage_fixup *fixup; 1528 struct btrfs_ordered_extent *ordered; 1529 + struct extent_state *cached_state = NULL; 1530 struct page *page; 1531 struct inode *inode; 1532 u64 page_start; ··· 1544 page_start = page_offset(page); 1545 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; 1546 1547 + lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0, 1548 + &cached_state, GFP_NOFS); 1549 1550 /* already ordered? We're done */ 1551 if (PagePrivate2(page)) ··· 1552 1553 ordered = btrfs_lookup_ordered_extent(inode, page_start); 1554 if (ordered) { 1555 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, 1556 + page_end, &cached_state, GFP_NOFS); 1557 unlock_page(page); 1558 btrfs_start_ordered_extent(inode, ordered, 1); 1559 goto again; 1560 } 1561 1562 + btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state); 1563 ClearPageChecked(page); 1564 out: 1565 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, 1566 + &cached_state, GFP_NOFS); 1567 out_page: 1568 unlock_page(page); 1569 page_cache_release(page); ··· 1691 struct btrfs_trans_handle *trans; 1692 struct btrfs_ordered_extent *ordered_extent = NULL; 1693 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 1694 + struct extent_state *cached_state = NULL; 1695 int compressed = 0; 1696 int ret; 1697 1698 + ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start, 1699 + end - start + 1); 1700 if (!ret) 1701 return 0; 1702 BUG_ON(!ordered_extent); 1703 1704 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) { ··· 1713 goto out; 1714 } 1715 1716 + lock_extent_bits(io_tree, ordered_extent->file_offset, 1717 + ordered_extent->file_offset + ordered_extent->len - 1, 1718 + 0, &cached_state, GFP_NOFS); 1719 1720 trans = btrfs_join_transaction(root, 1); 1721 ··· 1742 ordered_extent->len); 1743 BUG_ON(ret); 1744 } 1745 + unlock_extent_cached(io_tree, ordered_extent->file_offset, 1746 + ordered_extent->file_offset + 1747 + ordered_extent->len - 1, &cached_state, GFP_NOFS); 1748 + 1749 add_pending_csums(trans, inode, ordered_extent->file_offset, 1750 &ordered_extent->list); 1751 ··· 2153 found_key.objectid = found_key.offset; 2154 found_key.type = BTRFS_INODE_ITEM_KEY; 2155 found_key.offset = 0; 2156 + inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); 2157 if (IS_ERR(inode)) 2158 break; 2159 ··· 3081 struct btrfs_root *root = BTRFS_I(inode)->root; 3082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3083 struct btrfs_ordered_extent *ordered; 3084 + struct extent_state *cached_state = NULL; 3085 char *kaddr; 3086 u32 blocksize = root->sectorsize; 3087 pgoff_t index = from >> PAGE_CACHE_SHIFT; ··· 3127 } 3128 wait_on_page_writeback(page); 3129 3130 + lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, 3131 + GFP_NOFS); 3132 set_page_extent_mapped(page); 3133 3134 ordered = btrfs_lookup_ordered_extent(inode, page_start); 3135 if (ordered) { 3136 + unlock_extent_cached(io_tree, page_start, page_end, 3137 + &cached_state, GFP_NOFS); 3138 unlock_page(page); 3139 page_cache_release(page); 3140 btrfs_start_ordered_extent(inode, ordered, 1); ··· 3140 goto again; 3141 } 3142 3143 + clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 3144 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, 3145 + 0, 0, &cached_state, GFP_NOFS); 3146 3147 + ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 3148 + &cached_state); 3149 if (ret) { 3150 + unlock_extent_cached(io_tree, page_start, page_end, 3151 + &cached_state, GFP_NOFS); 3152 goto out_unlock; 3153 } 3154 ··· 3159 } 3160 ClearPageChecked(page); 3161 set_page_dirty(page); 3162 + unlock_extent_cached(io_tree, page_start, page_end, &cached_state, 3163 + GFP_NOFS); 3164 3165 out_unlock: 3166 if (ret) ··· 3177 struct btrfs_root *root = BTRFS_I(inode)->root; 3178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3179 struct extent_map *em; 3180 + struct extent_state *cached_state = NULL; 3181 u64 mask = root->sectorsize - 1; 3182 u64 hole_start = (inode->i_size + mask) & ~mask; 3183 u64 block_end = (size + mask) & ~mask; ··· 3192 struct btrfs_ordered_extent *ordered; 3193 btrfs_wait_ordered_range(inode, hole_start, 3194 block_end - hole_start); 3195 + lock_extent_bits(io_tree, hole_start, block_end - 1, 0, 3196 + &cached_state, GFP_NOFS); 3197 ordered = btrfs_lookup_ordered_extent(inode, hole_start); 3198 if (!ordered) 3199 break; 3200 + unlock_extent_cached(io_tree, hole_start, block_end - 1, 3201 + &cached_state, GFP_NOFS); 3202 btrfs_put_ordered_extent(ordered); 3203 } 3204 ··· 3241 break; 3242 } 3243 3244 + unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, 3245 + GFP_NOFS); 3246 return err; 3247 } 3248 ··· 3639 bi->index_cnt = (u64)-1; 3640 bi->last_unlink_trans = 0; 3641 bi->ordered_data_close = 0; 3642 + bi->force_compress = 0; 3643 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); 3644 extent_io_tree_init(&BTRFS_I(inode)->io_tree, 3645 inode->i_mapping, GFP_NOFS); ··· 3687 * Returns in *is_new if the inode was read from disk 3688 */ 3689 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, 3690 + struct btrfs_root *root, int *new) 3691 { 3692 struct inode *inode; 3693 ··· 3702 3703 inode_tree_add(inode); 3704 unlock_new_inode(inode); 3705 + if (new) 3706 + *new = 1; 3707 } 3708 3709 return inode; ··· 3754 return NULL; 3755 3756 if (location.type == BTRFS_INODE_ITEM_KEY) { 3757 + inode = btrfs_iget(dir->i_sb, &location, root, NULL); 3758 return inode; 3759 } 3760 ··· 3769 else 3770 inode = new_simple_dir(dir->i_sb, &location, sub_root); 3771 } else { 3772 + inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); 3773 } 3774 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 3775 ··· 4501 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); 4502 if (err) { 4503 err = -ENOSPC; 4504 + goto out_fail; 4505 } 4506 4507 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, ··· 4979 { 4980 struct extent_io_tree *tree; 4981 struct btrfs_ordered_extent *ordered; 4982 + struct extent_state *cached_state = NULL; 4983 u64 page_start = page_offset(page); 4984 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 4985 ··· 4997 btrfs_releasepage(page, GFP_NOFS); 4998 return; 4999 } 5000 + lock_extent_bits(tree, page_start, page_end, 0, &cached_state, 5001 + GFP_NOFS); 5002 ordered = btrfs_lookup_ordered_extent(page->mapping->host, 5003 page_offset(page)); 5004 if (ordered) { ··· 5008 clear_extent_bit(tree, page_start, page_end, 5009 EXTENT_DIRTY | EXTENT_DELALLOC | 5010 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0, 5011 + &cached_state, GFP_NOFS); 5012 /* 5013 * whoever cleared the private bit is responsible 5014 * for the finish_ordered_io ··· 5018 page_start, page_end); 5019 } 5020 btrfs_put_ordered_extent(ordered); 5021 + cached_state = NULL; 5022 + lock_extent_bits(tree, page_start, page_end, 0, &cached_state, 5023 + GFP_NOFS); 5024 } 5025 clear_extent_bit(tree, page_start, page_end, 5026 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | 5027 + EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS); 5028 __btrfs_releasepage(page, GFP_NOFS); 5029 5030 ClearPageChecked(page); ··· 5055 struct btrfs_root *root = BTRFS_I(inode)->root; 5056 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 5057 struct btrfs_ordered_extent *ordered; 5058 + struct extent_state *cached_state = NULL; 5059 char *kaddr; 5060 unsigned long zero_start; 5061 loff_t size; ··· 5093 } 5094 wait_on_page_writeback(page); 5095 5096 + lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state, 5097 + GFP_NOFS); 5098 set_page_extent_mapped(page); 5099 5100 /* ··· 5102 */ 5103 ordered = btrfs_lookup_ordered_extent(inode, page_start); 5104 if (ordered) { 5105 + unlock_extent_cached(io_tree, page_start, page_end, 5106 + &cached_state, GFP_NOFS); 5107 unlock_page(page); 5108 btrfs_start_ordered_extent(inode, ordered, 1); 5109 btrfs_put_ordered_extent(ordered); ··· 5116 * is probably a better way to do this, but for now keep consistent with 5117 * prepare_pages in the normal write path. 5118 */ 5119 + clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, 5120 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING, 5121 + 0, 0, &cached_state, GFP_NOFS); 5122 5123 + ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 5124 + &cached_state); 5125 if (ret) { 5126 + unlock_extent_cached(io_tree, page_start, page_end, 5127 + &cached_state, GFP_NOFS); 5128 ret = VM_FAULT_SIGBUS; 5129 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); 5130 goto out_unlock; ··· 5148 BTRFS_I(inode)->last_trans = root->fs_info->generation; 5149 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; 5150 5151 + unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS); 5152 5153 out_unlock: 5154 btrfs_unreserve_metadata_for_delalloc(root, inode, 1); ··· 5827 static long btrfs_fallocate(struct inode *inode, int mode, 5828 loff_t offset, loff_t len) 5829 { 5830 + struct extent_state *cached_state = NULL; 5831 u64 cur_offset; 5832 u64 last_byte; 5833 u64 alloc_start; ··· 5865 /* the extent lock is ordered inside the running 5866 * transaction 5867 */ 5868 + lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start, 5869 + locked_end, 0, &cached_state, GFP_NOFS); 5870 ordered = btrfs_lookup_first_ordered_extent(inode, 5871 alloc_end - 1); 5872 if (ordered && 5873 ordered->file_offset + ordered->len > alloc_start && 5874 ordered->file_offset < alloc_end) { 5875 btrfs_put_ordered_extent(ordered); 5876 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, 5877 + alloc_start, locked_end, 5878 + &cached_state, GFP_NOFS); 5879 /* 5880 * we can't wait on the range with the transaction 5881 * running or with the extent lock held ··· 5916 break; 5917 } 5918 } 5919 + unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end, 5920 + &cached_state, GFP_NOFS); 5921 5922 btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode, 5923 alloc_end - alloc_start);
+690 -16
fs/btrfs/ioctl.c
··· 48 #include "print-tree.h" 49 #include "volumes.h" 50 #include "locking.h" 51 52 /* Mask out flags that are inappropriate for the given type of inode. */ 53 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) ··· 475 return error; 476 } 477 478 - static int btrfs_defrag_file(struct file *file) 479 { 480 struct inode *inode = fdentry(file)->d_inode; 481 struct btrfs_root *root = BTRFS_I(inode)->root; ··· 559 unsigned long total_read = 0; 560 u64 page_start; 561 u64 page_end; 562 unsigned long i; 563 int ret; 564 565 - ret = btrfs_check_data_free_space(root, inode, inode->i_size); 566 - if (ret) 567 - return -ENOSPC; 568 569 - mutex_lock(&inode->i_mutex); 570 - last_index = inode->i_size >> PAGE_CACHE_SHIFT; 571 - for (i = 0; i <= last_index; i++) { 572 if (total_read % ra_pages == 0) { 573 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, 574 min(last_index, i + ra_pages - 1)); 575 } 576 total_read++; 577 again: 578 page = grab_cache_page(inode->i_mapping, i); 579 if (!page) 580 - goto out_unlock; 581 if (!PageUptodate(page)) { 582 btrfs_readpage(NULL, page); 583 lock_page(page); 584 if (!PageUptodate(page)) { 585 unlock_page(page); 586 page_cache_release(page); 587 - goto out_unlock; 588 } 589 } 590 591 wait_on_page_writeback(page); 592 593 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 594 page_end = page_start + PAGE_CACHE_SIZE - 1; ··· 669 * page if it is dirtied again later 670 */ 671 clear_page_dirty_for_io(page); 672 673 - btrfs_set_extent_delalloc(inode, page_start, page_end); 674 set_page_dirty(page); 675 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 676 unlock_page(page); 677 page_cache_release(page); 678 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); 679 } 680 681 - out_unlock: 682 - mutex_unlock(&inode->i_mutex); 683 return 0; 684 } 685 686 static noinline int btrfs_ioctl_resize(struct btrfs_root *root, ··· 776 mod = 1; 777 sizestr++; 778 } 779 - new_size = btrfs_parse_size(sizestr); 780 if (new_size == 0) { 781 ret = -EINVAL; 782 goto out_unlock; ··· 911 return ret; 912 } 913 914 static noinline int btrfs_ioctl_snap_destroy(struct file *file, 915 void __user *arg) 916 { ··· 1338 return err; 1339 } 1340 1341 - static int btrfs_ioctl_defrag(struct file *file) 1342 { 1343 struct inode *inode = fdentry(file)->d_inode; 1344 struct btrfs_root *root = BTRFS_I(inode)->root; 1345 int ret; 1346 1347 ret = mnt_want_write(file->f_path.mnt); ··· 1363 ret = -EINVAL; 1364 goto out; 1365 } 1366 - btrfs_defrag_file(file); 1367 break; 1368 } 1369 out: ··· 1787 return ret; 1788 } 1789 1790 /* 1791 * there are many ways the trans_start and trans_end ioctls can lead 1792 * to deadlocks. They should only be used by applications that ··· 1984 return btrfs_ioctl_snap_create(file, argp, 1); 1985 case BTRFS_IOC_SNAP_DESTROY: 1986 return btrfs_ioctl_snap_destroy(file, argp); 1987 case BTRFS_IOC_DEFRAG: 1988 - return btrfs_ioctl_defrag(file); 1989 case BTRFS_IOC_RESIZE: 1990 return btrfs_ioctl_resize(root, argp); 1991 case BTRFS_IOC_ADD_DEV: ··· 2006 return btrfs_ioctl_trans_start(file); 2007 case BTRFS_IOC_TRANS_END: 2008 return btrfs_ioctl_trans_end(file); 2009 case BTRFS_IOC_SYNC: 2010 btrfs_sync_fs(file->f_dentry->d_sb, 1); 2011 return 0;
··· 48 #include "print-tree.h" 49 #include "volumes.h" 50 #include "locking.h" 51 + #include "ctree.h" 52 53 /* Mask out flags that are inappropriate for the given type of inode. */ 54 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) ··· 474 return error; 475 } 476 477 + static int should_defrag_range(struct inode *inode, u64 start, u64 len, 478 + int thresh, u64 *last_len, u64 *skip, 479 + u64 *defrag_end) 480 + { 481 + struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 482 + struct extent_map *em = NULL; 483 + struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 484 + int ret = 1; 485 + 486 + 487 + if (thresh == 0) 488 + thresh = 256 * 1024; 489 + 490 + /* 491 + * make sure that once we start defragging and extent, we keep on 492 + * defragging it 493 + */ 494 + if (start < *defrag_end) 495 + return 1; 496 + 497 + *skip = 0; 498 + 499 + /* 500 + * hopefully we have this extent in the tree already, try without 501 + * the full extent lock 502 + */ 503 + read_lock(&em_tree->lock); 504 + em = lookup_extent_mapping(em_tree, start, len); 505 + read_unlock(&em_tree->lock); 506 + 507 + if (!em) { 508 + /* get the big lock and read metadata off disk */ 509 + lock_extent(io_tree, start, start + len - 1, GFP_NOFS); 510 + em = btrfs_get_extent(inode, NULL, 0, start, len, 0); 511 + unlock_extent(io_tree, start, start + len - 1, GFP_NOFS); 512 + 513 + if (!em) 514 + return 0; 515 + } 516 + 517 + /* this will cover holes, and inline extents */ 518 + if (em->block_start >= EXTENT_MAP_LAST_BYTE) 519 + ret = 0; 520 + 521 + /* 522 + * we hit a real extent, if it is big don't bother defragging it again 523 + */ 524 + if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) 525 + ret = 0; 526 + 527 + /* 528 + * last_len ends up being a counter of how many bytes we've defragged. 529 + * every time we choose not to defrag an extent, we reset *last_len 530 + * so that the next tiny extent will force a defrag. 531 + * 532 + * The end result of this is that tiny extents before a single big 533 + * extent will force at least part of that big extent to be defragged. 534 + */ 535 + if (ret) { 536 + *last_len += len; 537 + *defrag_end = extent_map_end(em); 538 + } else { 539 + *last_len = 0; 540 + *skip = extent_map_end(em); 541 + *defrag_end = 0; 542 + } 543 + 544 + free_extent_map(em); 545 + return ret; 546 + } 547 + 548 + static int btrfs_defrag_file(struct file *file, 549 + struct btrfs_ioctl_defrag_range_args *range) 550 { 551 struct inode *inode = fdentry(file)->d_inode; 552 struct btrfs_root *root = BTRFS_I(inode)->root; ··· 486 unsigned long total_read = 0; 487 u64 page_start; 488 u64 page_end; 489 + u64 last_len = 0; 490 + u64 skip = 0; 491 + u64 defrag_end = 0; 492 unsigned long i; 493 int ret; 494 495 + if (inode->i_size == 0) 496 + return 0; 497 498 + if (range->start + range->len > range->start) { 499 + last_index = min_t(u64, inode->i_size - 1, 500 + range->start + range->len - 1) >> PAGE_CACHE_SHIFT; 501 + } else { 502 + last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT; 503 + } 504 + 505 + i = range->start >> PAGE_CACHE_SHIFT; 506 + while (i <= last_index) { 507 + if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, 508 + PAGE_CACHE_SIZE, 509 + range->extent_thresh, 510 + &last_len, &skip, 511 + &defrag_end)) { 512 + unsigned long next; 513 + /* 514 + * the should_defrag function tells us how much to skip 515 + * bump our counter by the suggested amount 516 + */ 517 + next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 518 + i = max(i + 1, next); 519 + continue; 520 + } 521 + 522 if (total_read % ra_pages == 0) { 523 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, 524 min(last_index, i + ra_pages - 1)); 525 } 526 total_read++; 527 + mutex_lock(&inode->i_mutex); 528 + if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) 529 + BTRFS_I(inode)->force_compress = 1; 530 + 531 + ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE); 532 + if (ret) { 533 + ret = -ENOSPC; 534 + break; 535 + } 536 + 537 + ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1); 538 + if (ret) { 539 + btrfs_free_reserved_data_space(root, inode, 540 + PAGE_CACHE_SIZE); 541 + ret = -ENOSPC; 542 + break; 543 + } 544 again: 545 + if (inode->i_size == 0 || 546 + i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) { 547 + ret = 0; 548 + goto err_reservations; 549 + } 550 + 551 page = grab_cache_page(inode->i_mapping, i); 552 if (!page) 553 + goto err_reservations; 554 + 555 if (!PageUptodate(page)) { 556 btrfs_readpage(NULL, page); 557 lock_page(page); 558 if (!PageUptodate(page)) { 559 unlock_page(page); 560 page_cache_release(page); 561 + goto err_reservations; 562 } 563 } 564 565 + if (page->mapping != inode->i_mapping) { 566 + unlock_page(page); 567 + page_cache_release(page); 568 + goto again; 569 + } 570 + 571 wait_on_page_writeback(page); 572 + 573 + if (PageDirty(page)) { 574 + btrfs_free_reserved_data_space(root, inode, 575 + PAGE_CACHE_SIZE); 576 + goto loop_unlock; 577 + } 578 579 page_start = (u64)page->index << PAGE_CACHE_SHIFT; 580 page_end = page_start + PAGE_CACHE_SIZE - 1; ··· 537 * page if it is dirtied again later 538 */ 539 clear_page_dirty_for_io(page); 540 + clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, 541 + page_end, EXTENT_DIRTY | EXTENT_DELALLOC | 542 + EXTENT_DO_ACCOUNTING, GFP_NOFS); 543 544 + btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); 545 + ClearPageChecked(page); 546 set_page_dirty(page); 547 unlock_extent(io_tree, page_start, page_end, GFP_NOFS); 548 + 549 + loop_unlock: 550 unlock_page(page); 551 page_cache_release(page); 552 + mutex_unlock(&inode->i_mutex); 553 + 554 + btrfs_unreserve_metadata_for_delalloc(root, inode, 1); 555 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); 556 + i++; 557 } 558 559 + if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) 560 + filemap_flush(inode->i_mapping); 561 + 562 + if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 563 + /* the filemap_flush will queue IO into the worker threads, but 564 + * we have to make sure the IO is actually started and that 565 + * ordered extents get created before we return 566 + */ 567 + atomic_inc(&root->fs_info->async_submit_draining); 568 + while (atomic_read(&root->fs_info->nr_async_submits) || 569 + atomic_read(&root->fs_info->async_delalloc_pages)) { 570 + wait_event(root->fs_info->async_submit_wait, 571 + (atomic_read(&root->fs_info->nr_async_submits) == 0 && 572 + atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 573 + } 574 + atomic_dec(&root->fs_info->async_submit_draining); 575 + 576 + mutex_lock(&inode->i_mutex); 577 + BTRFS_I(inode)->force_compress = 0; 578 + mutex_unlock(&inode->i_mutex); 579 + } 580 + 581 return 0; 582 + 583 + err_reservations: 584 + mutex_unlock(&inode->i_mutex); 585 + btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE); 586 + btrfs_unreserve_metadata_for_delalloc(root, inode, 1); 587 + return ret; 588 } 589 590 static noinline int btrfs_ioctl_resize(struct btrfs_root *root, ··· 608 mod = 1; 609 sizestr++; 610 } 611 + new_size = memparse(sizestr, NULL); 612 if (new_size == 0) { 613 ret = -EINVAL; 614 goto out_unlock; ··· 743 return ret; 744 } 745 746 + static noinline int key_in_sk(struct btrfs_key *key, 747 + struct btrfs_ioctl_search_key *sk) 748 + { 749 + struct btrfs_key test; 750 + int ret; 751 + 752 + test.objectid = sk->min_objectid; 753 + test.type = sk->min_type; 754 + test.offset = sk->min_offset; 755 + 756 + ret = btrfs_comp_cpu_keys(key, &test); 757 + if (ret < 0) 758 + return 0; 759 + 760 + test.objectid = sk->max_objectid; 761 + test.type = sk->max_type; 762 + test.offset = sk->max_offset; 763 + 764 + ret = btrfs_comp_cpu_keys(key, &test); 765 + if (ret > 0) 766 + return 0; 767 + return 1; 768 + } 769 + 770 + static noinline int copy_to_sk(struct btrfs_root *root, 771 + struct btrfs_path *path, 772 + struct btrfs_key *key, 773 + struct btrfs_ioctl_search_key *sk, 774 + char *buf, 775 + unsigned long *sk_offset, 776 + int *num_found) 777 + { 778 + u64 found_transid; 779 + struct extent_buffer *leaf; 780 + struct btrfs_ioctl_search_header sh; 781 + unsigned long item_off; 782 + unsigned long item_len; 783 + int nritems; 784 + int i; 785 + int slot; 786 + int found = 0; 787 + int ret = 0; 788 + 789 + leaf = path->nodes[0]; 790 + slot = path->slots[0]; 791 + nritems = btrfs_header_nritems(leaf); 792 + 793 + if (btrfs_header_generation(leaf) > sk->max_transid) { 794 + i = nritems; 795 + goto advance_key; 796 + } 797 + found_transid = btrfs_header_generation(leaf); 798 + 799 + for (i = slot; i < nritems; i++) { 800 + item_off = btrfs_item_ptr_offset(leaf, i); 801 + item_len = btrfs_item_size_nr(leaf, i); 802 + 803 + if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) 804 + item_len = 0; 805 + 806 + if (sizeof(sh) + item_len + *sk_offset > 807 + BTRFS_SEARCH_ARGS_BUFSIZE) { 808 + ret = 1; 809 + goto overflow; 810 + } 811 + 812 + btrfs_item_key_to_cpu(leaf, key, i); 813 + if (!key_in_sk(key, sk)) 814 + continue; 815 + 816 + sh.objectid = key->objectid; 817 + sh.offset = key->offset; 818 + sh.type = key->type; 819 + sh.len = item_len; 820 + sh.transid = found_transid; 821 + 822 + /* copy search result header */ 823 + memcpy(buf + *sk_offset, &sh, sizeof(sh)); 824 + *sk_offset += sizeof(sh); 825 + 826 + if (item_len) { 827 + char *p = buf + *sk_offset; 828 + /* copy the item */ 829 + read_extent_buffer(leaf, p, 830 + item_off, item_len); 831 + *sk_offset += item_len; 832 + } 833 + found++; 834 + 835 + if (*num_found >= sk->nr_items) 836 + break; 837 + } 838 + advance_key: 839 + ret = 0; 840 + if (key->offset < (u64)-1 && key->offset < sk->max_offset) 841 + key->offset++; 842 + else if (key->type < (u8)-1 && key->type < sk->max_type) { 843 + key->offset = 0; 844 + key->type++; 845 + } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { 846 + key->offset = 0; 847 + key->type = 0; 848 + key->objectid++; 849 + } else 850 + ret = 1; 851 + overflow: 852 + *num_found += found; 853 + return ret; 854 + } 855 + 856 + static noinline int search_ioctl(struct inode *inode, 857 + struct btrfs_ioctl_search_args *args) 858 + { 859 + struct btrfs_root *root; 860 + struct btrfs_key key; 861 + struct btrfs_key max_key; 862 + struct btrfs_path *path; 863 + struct btrfs_ioctl_search_key *sk = &args->key; 864 + struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; 865 + int ret; 866 + int num_found = 0; 867 + unsigned long sk_offset = 0; 868 + 869 + path = btrfs_alloc_path(); 870 + if (!path) 871 + return -ENOMEM; 872 + 873 + if (sk->tree_id == 0) { 874 + /* search the root of the inode that was passed */ 875 + root = BTRFS_I(inode)->root; 876 + } else { 877 + key.objectid = sk->tree_id; 878 + key.type = BTRFS_ROOT_ITEM_KEY; 879 + key.offset = (u64)-1; 880 + root = btrfs_read_fs_root_no_name(info, &key); 881 + if (IS_ERR(root)) { 882 + printk(KERN_ERR "could not find root %llu\n", 883 + sk->tree_id); 884 + btrfs_free_path(path); 885 + return -ENOENT; 886 + } 887 + } 888 + 889 + key.objectid = sk->min_objectid; 890 + key.type = sk->min_type; 891 + key.offset = sk->min_offset; 892 + 893 + max_key.objectid = sk->max_objectid; 894 + max_key.type = sk->max_type; 895 + max_key.offset = sk->max_offset; 896 + 897 + path->keep_locks = 1; 898 + 899 + while(1) { 900 + ret = btrfs_search_forward(root, &key, &max_key, path, 0, 901 + sk->min_transid); 902 + if (ret != 0) { 903 + if (ret > 0) 904 + ret = 0; 905 + goto err; 906 + } 907 + ret = copy_to_sk(root, path, &key, sk, args->buf, 908 + &sk_offset, &num_found); 909 + btrfs_release_path(root, path); 910 + if (ret || num_found >= sk->nr_items) 911 + break; 912 + 913 + } 914 + ret = 0; 915 + err: 916 + sk->nr_items = num_found; 917 + btrfs_free_path(path); 918 + return ret; 919 + } 920 + 921 + static noinline int btrfs_ioctl_tree_search(struct file *file, 922 + void __user *argp) 923 + { 924 + struct btrfs_ioctl_search_args *args; 925 + struct inode *inode; 926 + int ret; 927 + 928 + if (!capable(CAP_SYS_ADMIN)) 929 + return -EPERM; 930 + 931 + args = kmalloc(sizeof(*args), GFP_KERNEL); 932 + if (!args) 933 + return -ENOMEM; 934 + 935 + if (copy_from_user(args, argp, sizeof(*args))) { 936 + kfree(args); 937 + return -EFAULT; 938 + } 939 + inode = fdentry(file)->d_inode; 940 + ret = search_ioctl(inode, args); 941 + if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 942 + ret = -EFAULT; 943 + kfree(args); 944 + return ret; 945 + } 946 + 947 + /* 948 + * Search INODE_REFs to identify path name of 'dirid' directory 949 + * in a 'tree_id' tree. and sets path name to 'name'. 950 + */ 951 + static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, 952 + u64 tree_id, u64 dirid, char *name) 953 + { 954 + struct btrfs_root *root; 955 + struct btrfs_key key; 956 + char *ptr; 957 + int ret = -1; 958 + int slot; 959 + int len; 960 + int total_len = 0; 961 + struct btrfs_inode_ref *iref; 962 + struct extent_buffer *l; 963 + struct btrfs_path *path; 964 + 965 + if (dirid == BTRFS_FIRST_FREE_OBJECTID) { 966 + name[0]='\0'; 967 + return 0; 968 + } 969 + 970 + path = btrfs_alloc_path(); 971 + if (!path) 972 + return -ENOMEM; 973 + 974 + ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; 975 + 976 + key.objectid = tree_id; 977 + key.type = BTRFS_ROOT_ITEM_KEY; 978 + key.offset = (u64)-1; 979 + root = btrfs_read_fs_root_no_name(info, &key); 980 + if (IS_ERR(root)) { 981 + printk(KERN_ERR "could not find root %llu\n", tree_id); 982 + ret = -ENOENT; 983 + goto out; 984 + } 985 + 986 + key.objectid = dirid; 987 + key.type = BTRFS_INODE_REF_KEY; 988 + key.offset = (u64)-1; 989 + 990 + while(1) { 991 + ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 992 + if (ret < 0) 993 + goto out; 994 + 995 + l = path->nodes[0]; 996 + slot = path->slots[0]; 997 + if (ret > 0 && slot > 0) 998 + slot--; 999 + btrfs_item_key_to_cpu(l, &key, slot); 1000 + 1001 + if (ret > 0 && (key.objectid != dirid || 1002 + key.type != BTRFS_INODE_REF_KEY)) { 1003 + ret = -ENOENT; 1004 + goto out; 1005 + } 1006 + 1007 + iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); 1008 + len = btrfs_inode_ref_name_len(l, iref); 1009 + ptr -= len + 1; 1010 + total_len += len + 1; 1011 + if (ptr < name) 1012 + goto out; 1013 + 1014 + *(ptr + len) = '/'; 1015 + read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); 1016 + 1017 + if (key.offset == BTRFS_FIRST_FREE_OBJECTID) 1018 + break; 1019 + 1020 + btrfs_release_path(root, path); 1021 + key.objectid = key.offset; 1022 + key.offset = (u64)-1; 1023 + dirid = key.objectid; 1024 + 1025 + } 1026 + if (ptr < name) 1027 + goto out; 1028 + memcpy(name, ptr, total_len); 1029 + name[total_len]='\0'; 1030 + ret = 0; 1031 + out: 1032 + btrfs_free_path(path); 1033 + return ret; 1034 + } 1035 + 1036 + static noinline int btrfs_ioctl_ino_lookup(struct file *file, 1037 + void __user *argp) 1038 + { 1039 + struct btrfs_ioctl_ino_lookup_args *args; 1040 + struct inode *inode; 1041 + int ret; 1042 + 1043 + if (!capable(CAP_SYS_ADMIN)) 1044 + return -EPERM; 1045 + 1046 + args = kmalloc(sizeof(*args), GFP_KERNEL); 1047 + if (copy_from_user(args, argp, sizeof(*args))) { 1048 + kfree(args); 1049 + return -EFAULT; 1050 + } 1051 + inode = fdentry(file)->d_inode; 1052 + 1053 + if (args->treeid == 0) 1054 + args->treeid = BTRFS_I(inode)->root->root_key.objectid; 1055 + 1056 + ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, 1057 + args->treeid, args->objectid, 1058 + args->name); 1059 + 1060 + if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) 1061 + ret = -EFAULT; 1062 + 1063 + kfree(args); 1064 + return ret; 1065 + } 1066 + 1067 static noinline int btrfs_ioctl_snap_destroy(struct file *file, 1068 void __user *arg) 1069 { ··· 849 return err; 850 } 851 852 + static int btrfs_ioctl_defrag(struct file *file, void __user *argp) 853 { 854 struct inode *inode = fdentry(file)->d_inode; 855 struct btrfs_root *root = BTRFS_I(inode)->root; 856 + struct btrfs_ioctl_defrag_range_args *range; 857 int ret; 858 859 ret = mnt_want_write(file->f_path.mnt); ··· 873 ret = -EINVAL; 874 goto out; 875 } 876 + 877 + range = kzalloc(sizeof(*range), GFP_KERNEL); 878 + if (!range) { 879 + ret = -ENOMEM; 880 + goto out; 881 + } 882 + 883 + if (argp) { 884 + if (copy_from_user(range, argp, 885 + sizeof(*range))) { 886 + ret = -EFAULT; 887 + kfree(range); 888 + } 889 + /* compression requires us to start the IO */ 890 + if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { 891 + range->flags |= BTRFS_DEFRAG_RANGE_START_IO; 892 + range->extent_thresh = (u32)-1; 893 + } 894 + } else { 895 + /* the rest are all set to zero by kzalloc */ 896 + range->len = (u64)-1; 897 + } 898 + btrfs_defrag_file(file, range); 899 + kfree(range); 900 break; 901 } 902 out: ··· 1274 return ret; 1275 } 1276 1277 + static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 1278 + { 1279 + struct inode *inode = fdentry(file)->d_inode; 1280 + struct btrfs_root *root = BTRFS_I(inode)->root; 1281 + struct btrfs_root *new_root; 1282 + struct btrfs_dir_item *di; 1283 + struct btrfs_trans_handle *trans; 1284 + struct btrfs_path *path; 1285 + struct btrfs_key location; 1286 + struct btrfs_disk_key disk_key; 1287 + struct btrfs_super_block *disk_super; 1288 + u64 features; 1289 + u64 objectid = 0; 1290 + u64 dir_id; 1291 + 1292 + if (!capable(CAP_SYS_ADMIN)) 1293 + return -EPERM; 1294 + 1295 + if (copy_from_user(&objectid, argp, sizeof(objectid))) 1296 + return -EFAULT; 1297 + 1298 + if (!objectid) 1299 + objectid = root->root_key.objectid; 1300 + 1301 + location.objectid = objectid; 1302 + location.type = BTRFS_ROOT_ITEM_KEY; 1303 + location.offset = (u64)-1; 1304 + 1305 + new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 1306 + if (IS_ERR(new_root)) 1307 + return PTR_ERR(new_root); 1308 + 1309 + if (btrfs_root_refs(&new_root->root_item) == 0) 1310 + return -ENOENT; 1311 + 1312 + path = btrfs_alloc_path(); 1313 + if (!path) 1314 + return -ENOMEM; 1315 + path->leave_spinning = 1; 1316 + 1317 + trans = btrfs_start_transaction(root, 1); 1318 + if (!trans) { 1319 + btrfs_free_path(path); 1320 + return -ENOMEM; 1321 + } 1322 + 1323 + dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); 1324 + di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 1325 + dir_id, "default", 7, 1); 1326 + if (!di) { 1327 + btrfs_free_path(path); 1328 + btrfs_end_transaction(trans, root); 1329 + printk(KERN_ERR "Umm, you don't have the default dir item, " 1330 + "this isn't going to work\n"); 1331 + return -ENOENT; 1332 + } 1333 + 1334 + btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); 1335 + btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); 1336 + btrfs_mark_buffer_dirty(path->nodes[0]); 1337 + btrfs_free_path(path); 1338 + 1339 + disk_super = &root->fs_info->super_copy; 1340 + features = btrfs_super_incompat_flags(disk_super); 1341 + if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { 1342 + features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; 1343 + btrfs_set_super_incompat_flags(disk_super, features); 1344 + } 1345 + btrfs_end_transaction(trans, root); 1346 + 1347 + return 0; 1348 + } 1349 + 1350 + long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 1351 + { 1352 + struct btrfs_ioctl_space_args space_args; 1353 + struct btrfs_ioctl_space_info space; 1354 + struct btrfs_ioctl_space_info *dest; 1355 + struct btrfs_ioctl_space_info *dest_orig; 1356 + struct btrfs_ioctl_space_info *user_dest; 1357 + struct btrfs_space_info *info; 1358 + int alloc_size; 1359 + int ret = 0; 1360 + int slot_count = 0; 1361 + 1362 + if (copy_from_user(&space_args, 1363 + (struct btrfs_ioctl_space_args __user *)arg, 1364 + sizeof(space_args))) 1365 + return -EFAULT; 1366 + 1367 + /* first we count slots */ 1368 + rcu_read_lock(); 1369 + list_for_each_entry_rcu(info, &root->fs_info->space_info, list) 1370 + slot_count++; 1371 + rcu_read_unlock(); 1372 + 1373 + /* space_slots == 0 means they are asking for a count */ 1374 + if (space_args.space_slots == 0) { 1375 + space_args.total_spaces = slot_count; 1376 + goto out; 1377 + } 1378 + alloc_size = sizeof(*dest) * slot_count; 1379 + /* we generally have at most 6 or so space infos, one for each raid 1380 + * level. So, a whole page should be more than enough for everyone 1381 + */ 1382 + if (alloc_size > PAGE_CACHE_SIZE) 1383 + return -ENOMEM; 1384 + 1385 + space_args.total_spaces = 0; 1386 + dest = kmalloc(alloc_size, GFP_NOFS); 1387 + if (!dest) 1388 + return -ENOMEM; 1389 + dest_orig = dest; 1390 + 1391 + /* now we have a buffer to copy into */ 1392 + rcu_read_lock(); 1393 + list_for_each_entry_rcu(info, &root->fs_info->space_info, list) { 1394 + /* make sure we don't copy more than we allocated 1395 + * in our buffer 1396 + */ 1397 + if (slot_count == 0) 1398 + break; 1399 + slot_count--; 1400 + 1401 + /* make sure userland has enough room in their buffer */ 1402 + if (space_args.total_spaces >= space_args.space_slots) 1403 + break; 1404 + 1405 + space.flags = info->flags; 1406 + space.total_bytes = info->total_bytes; 1407 + space.used_bytes = info->bytes_used; 1408 + memcpy(dest, &space, sizeof(space)); 1409 + dest++; 1410 + space_args.total_spaces++; 1411 + } 1412 + rcu_read_unlock(); 1413 + 1414 + user_dest = (struct btrfs_ioctl_space_info *) 1415 + (arg + sizeof(struct btrfs_ioctl_space_args)); 1416 + 1417 + if (copy_to_user(user_dest, dest_orig, alloc_size)) 1418 + ret = -EFAULT; 1419 + 1420 + kfree(dest_orig); 1421 + out: 1422 + if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) 1423 + ret = -EFAULT; 1424 + 1425 + return ret; 1426 + } 1427 + 1428 /* 1429 * there are many ways the trans_start and trans_end ioctls can lead 1430 * to deadlocks. They should only be used by applications that ··· 1320 return btrfs_ioctl_snap_create(file, argp, 1); 1321 case BTRFS_IOC_SNAP_DESTROY: 1322 return btrfs_ioctl_snap_destroy(file, argp); 1323 + case BTRFS_IOC_DEFAULT_SUBVOL: 1324 + return btrfs_ioctl_default_subvol(file, argp); 1325 case BTRFS_IOC_DEFRAG: 1326 + return btrfs_ioctl_defrag(file, NULL); 1327 + case BTRFS_IOC_DEFRAG_RANGE: 1328 + return btrfs_ioctl_defrag(file, argp); 1329 case BTRFS_IOC_RESIZE: 1330 return btrfs_ioctl_resize(root, argp); 1331 case BTRFS_IOC_ADD_DEV: ··· 1338 return btrfs_ioctl_trans_start(file); 1339 case BTRFS_IOC_TRANS_END: 1340 return btrfs_ioctl_trans_end(file); 1341 + case BTRFS_IOC_TREE_SEARCH: 1342 + return btrfs_ioctl_tree_search(file, argp); 1343 + case BTRFS_IOC_INO_LOOKUP: 1344 + return btrfs_ioctl_ino_lookup(file, argp); 1345 + case BTRFS_IOC_SPACE_INFO: 1346 + return btrfs_ioctl_space_info(root, argp); 1347 case BTRFS_IOC_SYNC: 1348 btrfs_sync_fs(file->f_dentry->d_sb, 1); 1349 return 0;
+111
fs/btrfs/ioctl.h
··· 30 char name[BTRFS_PATH_NAME_MAX + 1]; 31 }; 32 33 struct btrfs_ioctl_clone_range_args { 34 __s64 src_fd; 35 __u64 src_offset, src_length; 36 __u64 dest_offset; 37 }; 38 39 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ ··· 169 struct btrfs_ioctl_vol_args) 170 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \ 171 struct btrfs_ioctl_vol_args) 172 #endif
··· 30 char name[BTRFS_PATH_NAME_MAX + 1]; 31 }; 32 33 + #define BTRFS_INO_LOOKUP_PATH_MAX 4080 34 + struct btrfs_ioctl_ino_lookup_args { 35 + __u64 treeid; 36 + __u64 objectid; 37 + char name[BTRFS_INO_LOOKUP_PATH_MAX]; 38 + }; 39 + 40 + struct btrfs_ioctl_search_key { 41 + /* which root are we searching. 0 is the tree of tree roots */ 42 + __u64 tree_id; 43 + 44 + /* keys returned will be >= min and <= max */ 45 + __u64 min_objectid; 46 + __u64 max_objectid; 47 + 48 + /* keys returned will be >= min and <= max */ 49 + __u64 min_offset; 50 + __u64 max_offset; 51 + 52 + /* max and min transids to search for */ 53 + __u64 min_transid; 54 + __u64 max_transid; 55 + 56 + /* keys returned will be >= min and <= max */ 57 + __u32 min_type; 58 + __u32 max_type; 59 + 60 + /* 61 + * how many items did userland ask for, and how many are we 62 + * returning 63 + */ 64 + __u32 nr_items; 65 + 66 + /* align to 64 bits */ 67 + __u32 unused; 68 + 69 + /* some extra for later */ 70 + __u64 unused1; 71 + __u64 unused2; 72 + __u64 unused3; 73 + __u64 unused4; 74 + }; 75 + 76 + struct btrfs_ioctl_search_header { 77 + __u64 transid; 78 + __u64 objectid; 79 + __u64 offset; 80 + __u32 type; 81 + __u32 len; 82 + }; 83 + 84 + #define BTRFS_SEARCH_ARGS_BUFSIZE (4096 - sizeof(struct btrfs_ioctl_search_key)) 85 + /* 86 + * the buf is an array of search headers where 87 + * each header is followed by the actual item 88 + * the type field is expanded to 32 bits for alignment 89 + */ 90 + struct btrfs_ioctl_search_args { 91 + struct btrfs_ioctl_search_key key; 92 + char buf[BTRFS_SEARCH_ARGS_BUFSIZE]; 93 + }; 94 + 95 struct btrfs_ioctl_clone_range_args { 96 __s64 src_fd; 97 __u64 src_offset, src_length; 98 __u64 dest_offset; 99 + }; 100 + 101 + /* flags for the defrag range ioctl */ 102 + #define BTRFS_DEFRAG_RANGE_COMPRESS 1 103 + #define BTRFS_DEFRAG_RANGE_START_IO 2 104 + 105 + struct btrfs_ioctl_defrag_range_args { 106 + /* start of the defrag operation */ 107 + __u64 start; 108 + 109 + /* number of bytes to defrag, use (u64)-1 to say all */ 110 + __u64 len; 111 + 112 + /* 113 + * flags for the operation, which can include turning 114 + * on compression for this one defrag 115 + */ 116 + __u64 flags; 117 + 118 + /* 119 + * any extent bigger than this will be considered 120 + * already defragged. Use 0 to take the kernel default 121 + * Use 1 to say every single extent must be rewritten 122 + */ 123 + __u32 extent_thresh; 124 + 125 + /* spare for later */ 126 + __u32 unused[5]; 127 + }; 128 + 129 + struct btrfs_ioctl_space_info { 130 + __u64 flags; 131 + __u64 total_bytes; 132 + __u64 used_bytes; 133 + }; 134 + 135 + struct btrfs_ioctl_space_args { 136 + __u64 space_slots; 137 + __u64 total_spaces; 138 + struct btrfs_ioctl_space_info spaces[0]; 139 }; 140 141 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \ ··· 67 struct btrfs_ioctl_vol_args) 68 #define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \ 69 struct btrfs_ioctl_vol_args) 70 + #define BTRFS_IOC_DEFRAG_RANGE _IOW(BTRFS_IOCTL_MAGIC, 16, \ 71 + struct btrfs_ioctl_defrag_range_args) 72 + #define BTRFS_IOC_TREE_SEARCH _IOWR(BTRFS_IOCTL_MAGIC, 17, \ 73 + struct btrfs_ioctl_search_args) 74 + #define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \ 75 + struct btrfs_ioctl_ino_lookup_args) 76 + #define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64) 77 + #define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \ 78 + struct btrfs_ioctl_space_args) 79 #endif
+23 -18
fs/btrfs/ordered-data.c
··· 174 if (!entry) 175 return -ENOMEM; 176 177 - mutex_lock(&tree->mutex); 178 entry->file_offset = file_offset; 179 entry->start = start; 180 entry->len = len; ··· 189 INIT_LIST_HEAD(&entry->list); 190 INIT_LIST_HEAD(&entry->root_extent_list); 191 192 node = tree_insert(&tree->tree, file_offset, 193 &entry->rb_node); 194 BUG_ON(node); 195 196 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 197 list_add_tail(&entry->root_extent_list, 198 &BTRFS_I(inode)->root->fs_info->ordered_extents); 199 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 200 201 - mutex_unlock(&tree->mutex); 202 BUG_ON(node); 203 return 0; 204 } ··· 216 struct btrfs_ordered_inode_tree *tree; 217 218 tree = &BTRFS_I(inode)->ordered_tree; 219 - mutex_lock(&tree->mutex); 220 list_add_tail(&sum->list, &entry->list); 221 - mutex_unlock(&tree->mutex); 222 return 0; 223 } 224 ··· 232 * to make sure this function only returns 1 once for a given ordered extent. 233 */ 234 int btrfs_dec_test_ordered_pending(struct inode *inode, 235 u64 file_offset, u64 io_size) 236 { 237 struct btrfs_ordered_inode_tree *tree; 238 struct rb_node *node; 239 - struct btrfs_ordered_extent *entry; 240 int ret; 241 242 tree = &BTRFS_I(inode)->ordered_tree; 243 - mutex_lock(&tree->mutex); 244 node = tree_search(tree, file_offset); 245 if (!node) { 246 ret = 1; ··· 265 else 266 ret = 1; 267 out: 268 - mutex_unlock(&tree->mutex); 269 return ret == 0; 270 } 271 ··· 296 297 /* 298 * remove an ordered extent from the tree. No references are dropped 299 - * and you must wake_up entry->wait. You must hold the tree mutex 300 * while you call this function. 301 */ 302 static int __btrfs_remove_ordered_extent(struct inode *inode, ··· 345 int ret; 346 347 tree = &BTRFS_I(inode)->ordered_tree; 348 - mutex_lock(&tree->mutex); 349 ret = __btrfs_remove_ordered_extent(inode, entry); 350 - mutex_unlock(&tree->mutex); 351 wake_up(&entry->wait); 352 353 return ret; ··· 572 struct btrfs_ordered_extent *entry = NULL; 573 574 tree = &BTRFS_I(inode)->ordered_tree; 575 - mutex_lock(&tree->mutex); 576 node = tree_search(tree, file_offset); 577 if (!node) 578 goto out; ··· 583 if (entry) 584 atomic_inc(&entry->refs); 585 out: 586 - mutex_unlock(&tree->mutex); 587 return entry; 588 } 589 ··· 599 struct btrfs_ordered_extent *entry = NULL; 600 601 tree = &BTRFS_I(inode)->ordered_tree; 602 - mutex_lock(&tree->mutex); 603 node = tree_search(tree, file_offset); 604 if (!node) 605 goto out; ··· 607 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); 608 atomic_inc(&entry->refs); 609 out: 610 - mutex_unlock(&tree->mutex); 611 return entry; 612 } 613 ··· 634 else 635 offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize); 636 637 - mutex_lock(&tree->mutex); 638 disk_i_size = BTRFS_I(inode)->disk_i_size; 639 640 /* truncate file */ ··· 740 */ 741 if (ordered) 742 __btrfs_remove_ordered_extent(inode, ordered); 743 - mutex_unlock(&tree->mutex); 744 if (ordered) 745 wake_up(&ordered->wait); 746 return ret; ··· 767 if (!ordered) 768 return 1; 769 770 - mutex_lock(&tree->mutex); 771 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) { 772 if (disk_bytenr >= ordered_sum->bytenr) { 773 num_sectors = ordered_sum->len / sectorsize; ··· 782 } 783 } 784 out: 785 - mutex_unlock(&tree->mutex); 786 btrfs_put_ordered_extent(ordered); 787 return ret; 788 }
··· 174 if (!entry) 175 return -ENOMEM; 176 177 entry->file_offset = file_offset; 178 entry->start = start; 179 entry->len = len; ··· 190 INIT_LIST_HEAD(&entry->list); 191 INIT_LIST_HEAD(&entry->root_extent_list); 192 193 + spin_lock(&tree->lock); 194 node = tree_insert(&tree->tree, file_offset, 195 &entry->rb_node); 196 BUG_ON(node); 197 + spin_unlock(&tree->lock); 198 199 spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 200 list_add_tail(&entry->root_extent_list, 201 &BTRFS_I(inode)->root->fs_info->ordered_extents); 202 spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock); 203 204 BUG_ON(node); 205 return 0; 206 } ··· 216 struct btrfs_ordered_inode_tree *tree; 217 218 tree = &BTRFS_I(inode)->ordered_tree; 219 + spin_lock(&tree->lock); 220 list_add_tail(&sum->list, &entry->list); 221 + spin_unlock(&tree->lock); 222 return 0; 223 } 224 ··· 232 * to make sure this function only returns 1 once for a given ordered extent. 233 */ 234 int btrfs_dec_test_ordered_pending(struct inode *inode, 235 + struct btrfs_ordered_extent **cached, 236 u64 file_offset, u64 io_size) 237 { 238 struct btrfs_ordered_inode_tree *tree; 239 struct rb_node *node; 240 + struct btrfs_ordered_extent *entry = NULL; 241 int ret; 242 243 tree = &BTRFS_I(inode)->ordered_tree; 244 + spin_lock(&tree->lock); 245 node = tree_search(tree, file_offset); 246 if (!node) { 247 ret = 1; ··· 264 else 265 ret = 1; 266 out: 267 + if (!ret && cached && entry) { 268 + *cached = entry; 269 + atomic_inc(&entry->refs); 270 + } 271 + spin_unlock(&tree->lock); 272 return ret == 0; 273 } 274 ··· 291 292 /* 293 * remove an ordered extent from the tree. No references are dropped 294 + * and you must wake_up entry->wait. You must hold the tree lock 295 * while you call this function. 296 */ 297 static int __btrfs_remove_ordered_extent(struct inode *inode, ··· 340 int ret; 341 342 tree = &BTRFS_I(inode)->ordered_tree; 343 + spin_lock(&tree->lock); 344 ret = __btrfs_remove_ordered_extent(inode, entry); 345 + spin_unlock(&tree->lock); 346 wake_up(&entry->wait); 347 348 return ret; ··· 567 struct btrfs_ordered_extent *entry = NULL; 568 569 tree = &BTRFS_I(inode)->ordered_tree; 570 + spin_lock(&tree->lock); 571 node = tree_search(tree, file_offset); 572 if (!node) 573 goto out; ··· 578 if (entry) 579 atomic_inc(&entry->refs); 580 out: 581 + spin_unlock(&tree->lock); 582 return entry; 583 } 584 ··· 594 struct btrfs_ordered_extent *entry = NULL; 595 596 tree = &BTRFS_I(inode)->ordered_tree; 597 + spin_lock(&tree->lock); 598 node = tree_search(tree, file_offset); 599 if (!node) 600 goto out; ··· 602 entry = rb_entry(node, struct btrfs_ordered_extent, rb_node); 603 atomic_inc(&entry->refs); 604 out: 605 + spin_unlock(&tree->lock); 606 return entry; 607 } 608 ··· 629 else 630 offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize); 631 632 + spin_lock(&tree->lock); 633 disk_i_size = BTRFS_I(inode)->disk_i_size; 634 635 /* truncate file */ ··· 735 */ 736 if (ordered) 737 __btrfs_remove_ordered_extent(inode, ordered); 738 + spin_unlock(&tree->lock); 739 if (ordered) 740 wake_up(&ordered->wait); 741 return ret; ··· 762 if (!ordered) 763 return 1; 764 765 + spin_lock(&tree->lock); 766 list_for_each_entry_reverse(ordered_sum, &ordered->list, list) { 767 if (disk_bytenr >= ordered_sum->bytenr) { 768 num_sectors = ordered_sum->len / sectorsize; ··· 777 } 778 } 779 out: 780 + spin_unlock(&tree->lock); 781 btrfs_put_ordered_extent(ordered); 782 return ret; 783 }
+4 -3
fs/btrfs/ordered-data.h
··· 21 22 /* one of these per inode */ 23 struct btrfs_ordered_inode_tree { 24 - struct mutex mutex; 25 struct rb_root tree; 26 struct rb_node *last; 27 }; ··· 128 static inline void 129 btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t) 130 { 131 - mutex_init(&t->mutex); 132 t->tree = RB_ROOT; 133 t->last = NULL; 134 } ··· 137 int btrfs_remove_ordered_extent(struct inode *inode, 138 struct btrfs_ordered_extent *entry); 139 int btrfs_dec_test_ordered_pending(struct inode *inode, 140 - u64 file_offset, u64 io_size); 141 int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, 142 u64 start, u64 len, u64 disk_len, int tyep); 143 int btrfs_add_ordered_sum(struct inode *inode,
··· 21 22 /* one of these per inode */ 23 struct btrfs_ordered_inode_tree { 24 + spinlock_t lock; 25 struct rb_root tree; 26 struct rb_node *last; 27 }; ··· 128 static inline void 129 btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t) 130 { 131 + spin_lock_init(&t->lock); 132 t->tree = RB_ROOT; 133 t->last = NULL; 134 } ··· 137 int btrfs_remove_ordered_extent(struct inode *inode, 138 struct btrfs_ordered_extent *entry); 139 int btrfs_dec_test_ordered_pending(struct inode *inode, 140 + struct btrfs_ordered_extent **cached, 141 + u64 file_offset, u64 io_size); 142 int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset, 143 u64 start, u64 len, u64 disk_len, int tyep); 144 int btrfs_add_ordered_sum(struct inode *inode,
+2 -2
fs/btrfs/relocation.c
··· 2659 EXTENT_BOUNDARY, GFP_NOFS); 2660 nr++; 2661 } 2662 - btrfs_set_extent_delalloc(inode, page_start, page_end); 2663 2664 set_page_dirty(page); 2665 dirty_page++; ··· 3487 key.objectid = objectid; 3488 key.type = BTRFS_INODE_ITEM_KEY; 3489 key.offset = 0; 3490 - inode = btrfs_iget(root->fs_info->sb, &key, root); 3491 BUG_ON(IS_ERR(inode) || is_bad_inode(inode)); 3492 BTRFS_I(inode)->index_cnt = group->key.objectid; 3493
··· 2659 EXTENT_BOUNDARY, GFP_NOFS); 2660 nr++; 2661 } 2662 + btrfs_set_extent_delalloc(inode, page_start, page_end, NULL); 2663 2664 set_page_dirty(page); 2665 dirty_page++; ··· 3487 key.objectid = objectid; 3488 key.type = BTRFS_INODE_ITEM_KEY; 3489 key.offset = 0; 3490 + inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); 3491 BUG_ON(IS_ERR(inode) || is_bad_inode(inode)); 3492 BTRFS_I(inode)->index_cnt = group->key.objectid; 3493
+183 -57
fs/btrfs/super.c
··· 63 } 64 65 enum { 66 - Opt_degraded, Opt_subvol, Opt_device, Opt_nodatasum, Opt_nodatacow, 67 - Opt_max_extent, Opt_max_inline, Opt_alloc_start, Opt_nobarrier, 68 - Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, Opt_noacl, 69 - Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio, 70 Opt_flushoncommit, 71 Opt_discard, Opt_err, 72 }; ··· 74 static match_table_t tokens = { 75 {Opt_degraded, "degraded"}, 76 {Opt_subvol, "subvol=%s"}, 77 {Opt_device, "device=%s"}, 78 {Opt_nodatasum, "nodatasum"}, 79 {Opt_nodatacow, "nodatacow"}, ··· 95 {Opt_discard, "discard"}, 96 {Opt_err, NULL}, 97 }; 98 - 99 - u64 btrfs_parse_size(char *str) 100 - { 101 - u64 res; 102 - int mult = 1; 103 - char *end; 104 - char last; 105 - 106 - res = simple_strtoul(str, &end, 10); 107 - 108 - last = end[0]; 109 - if (isalpha(last)) { 110 - last = tolower(last); 111 - switch (last) { 112 - case 'g': 113 - mult *= 1024; 114 - case 'm': 115 - mult *= 1024; 116 - case 'k': 117 - mult *= 1024; 118 - } 119 - res = res * mult; 120 - } 121 - return res; 122 - } 123 124 /* 125 * Regular mount options parser. Everything that is needed only when ··· 133 btrfs_set_opt(info->mount_opt, DEGRADED); 134 break; 135 case Opt_subvol: 136 case Opt_device: 137 /* 138 * These are parsed by btrfs_parse_early_options ··· 191 case Opt_max_extent: 192 num = match_strdup(&args[0]); 193 if (num) { 194 - info->max_extent = btrfs_parse_size(num); 195 kfree(num); 196 197 info->max_extent = max_t(u64, ··· 203 case Opt_max_inline: 204 num = match_strdup(&args[0]); 205 if (num) { 206 - info->max_inline = btrfs_parse_size(num); 207 kfree(num); 208 209 if (info->max_inline) { ··· 218 case Opt_alloc_start: 219 num = match_strdup(&args[0]); 220 if (num) { 221 - info->alloc_start = btrfs_parse_size(num); 222 kfree(num); 223 printk(KERN_INFO 224 "btrfs: allocations start at %llu\n", ··· 269 * only when we need to allocate a new super block. 270 */ 271 static int btrfs_parse_early_options(const char *options, fmode_t flags, 272 - void *holder, char **subvol_name, 273 struct btrfs_fs_devices **fs_devices) 274 { 275 substring_t args[MAX_OPT_ARGS]; 276 char *opts, *p; 277 int error = 0; 278 279 if (!options) 280 goto out; ··· 297 switch (token) { 298 case Opt_subvol: 299 *subvol_name = match_strdup(&args[0]); 300 break; 301 case Opt_device: 302 error = btrfs_scan_one_device(match_strdup(&args[0]), ··· 335 return -ENOMEM; 336 } 337 return error; 338 } 339 340 static int btrfs_fill_super(struct super_block *sb, ··· 474 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 475 key.type = BTRFS_INODE_ITEM_KEY; 476 key.offset = 0; 477 - inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root); 478 if (IS_ERR(inode)) { 479 err = PTR_ERR(inode); 480 goto fail_close; ··· 486 err = -ENOMEM; 487 goto fail_close; 488 } 489 - #if 0 490 - /* this does the super kobj at the same time */ 491 - err = btrfs_sysfs_add_super(tree_root->fs_info); 492 - if (err) 493 - goto fail_close; 494 - #endif 495 496 sb->s_root = root_dentry; 497 ··· 577 static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 578 const char *dev_name, void *data, struct vfsmount *mnt) 579 { 580 - char *subvol_name = NULL; 581 struct block_device *bdev = NULL; 582 struct super_block *s; 583 struct dentry *root; 584 struct btrfs_fs_devices *fs_devices = NULL; 585 fmode_t mode = FMODE_READ; 586 int error = 0; 587 588 if (!(flags & MS_RDONLY)) 589 mode |= FMODE_WRITE; 590 591 error = btrfs_parse_early_options(data, mode, fs_type, 592 - &subvol_name, &fs_devices); 593 if (error) 594 return error; 595 ··· 621 goto error_close_devices; 622 } 623 624 btrfs_close_devices(fs_devices); 625 } else { 626 char b[BDEVNAME_SIZE]; ··· 639 s->s_flags |= MS_ACTIVE; 640 } 641 642 - if (!strcmp(subvol_name, ".")) 643 - root = dget(s->s_root); 644 - else { 645 - mutex_lock(&s->s_root->d_inode->i_mutex); 646 - root = lookup_one_len(subvol_name, s->s_root, 647 strlen(subvol_name)); 648 - mutex_unlock(&s->s_root->d_inode->i_mutex); 649 650 - if (IS_ERR(root)) { 651 deactivate_locked_super(s); 652 - error = PTR_ERR(root); 653 - goto error_free_subvol_name; 654 - } 655 - if (!root->d_inode) { 656 dput(root); 657 deactivate_locked_super(s); 658 error = -ENXIO; 659 - goto error_free_subvol_name; 660 } 661 } 662 663 mnt->mnt_sb = s; ··· 682 btrfs_close_devices(fs_devices); 683 error_free_subvol_name: 684 kfree(subvol_name); 685 return error; 686 } 687 ··· 727 { 728 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 729 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 730 int bits = dentry->d_sb->s_blocksize_bits; 731 __be32 *fsid = (__be32 *)root->fs_info->fsid; 732 733 buf->f_namelen = BTRFS_NAME_LEN; 734 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 735 - buf->f_bfree = buf->f_blocks - 736 - (btrfs_super_bytes_used(disk_super) >> bits); 737 - buf->f_bavail = buf->f_bfree; 738 buf->f_bsize = dentry->d_sb->s_blocksize; 739 buf->f_type = BTRFS_SUPER_MAGIC; 740
··· 63 } 64 65 enum { 66 + Opt_degraded, Opt_subvol, Opt_subvolid, Opt_device, Opt_nodatasum, 67 + Opt_nodatacow, Opt_max_extent, Opt_max_inline, Opt_alloc_start, 68 + Opt_nobarrier, Opt_ssd, Opt_nossd, Opt_ssd_spread, Opt_thread_pool, 69 + Opt_noacl, Opt_compress, Opt_compress_force, Opt_notreelog, Opt_ratio, 70 Opt_flushoncommit, 71 Opt_discard, Opt_err, 72 }; ··· 74 static match_table_t tokens = { 75 {Opt_degraded, "degraded"}, 76 {Opt_subvol, "subvol=%s"}, 77 + {Opt_subvolid, "subvolid=%d"}, 78 {Opt_device, "device=%s"}, 79 {Opt_nodatasum, "nodatasum"}, 80 {Opt_nodatacow, "nodatacow"}, ··· 94 {Opt_discard, "discard"}, 95 {Opt_err, NULL}, 96 }; 97 98 /* 99 * Regular mount options parser. Everything that is needed only when ··· 157 btrfs_set_opt(info->mount_opt, DEGRADED); 158 break; 159 case Opt_subvol: 160 + case Opt_subvolid: 161 case Opt_device: 162 /* 163 * These are parsed by btrfs_parse_early_options ··· 214 case Opt_max_extent: 215 num = match_strdup(&args[0]); 216 if (num) { 217 + info->max_extent = memparse(num, NULL); 218 kfree(num); 219 220 info->max_extent = max_t(u64, ··· 226 case Opt_max_inline: 227 num = match_strdup(&args[0]); 228 if (num) { 229 + info->max_inline = memparse(num, NULL); 230 kfree(num); 231 232 if (info->max_inline) { ··· 241 case Opt_alloc_start: 242 num = match_strdup(&args[0]); 243 if (num) { 244 + info->alloc_start = memparse(num, NULL); 245 kfree(num); 246 printk(KERN_INFO 247 "btrfs: allocations start at %llu\n", ··· 292 * only when we need to allocate a new super block. 293 */ 294 static int btrfs_parse_early_options(const char *options, fmode_t flags, 295 + void *holder, char **subvol_name, u64 *subvol_objectid, 296 struct btrfs_fs_devices **fs_devices) 297 { 298 substring_t args[MAX_OPT_ARGS]; 299 char *opts, *p; 300 int error = 0; 301 + int intarg; 302 303 if (!options) 304 goto out; ··· 319 switch (token) { 320 case Opt_subvol: 321 *subvol_name = match_strdup(&args[0]); 322 + break; 323 + case Opt_subvolid: 324 + intarg = 0; 325 + error = match_int(&args[0], &intarg); 326 + if (!error) { 327 + /* we want the original fs_tree */ 328 + if (!intarg) 329 + *subvol_objectid = 330 + BTRFS_FS_TREE_OBJECTID; 331 + else 332 + *subvol_objectid = intarg; 333 + } 334 break; 335 case Opt_device: 336 error = btrfs_scan_one_device(match_strdup(&args[0]), ··· 345 return -ENOMEM; 346 } 347 return error; 348 + } 349 + 350 + static struct dentry *get_default_root(struct super_block *sb, 351 + u64 subvol_objectid) 352 + { 353 + struct btrfs_root *root = sb->s_fs_info; 354 + struct btrfs_root *new_root; 355 + struct btrfs_dir_item *di; 356 + struct btrfs_path *path; 357 + struct btrfs_key location; 358 + struct inode *inode; 359 + struct dentry *dentry; 360 + u64 dir_id; 361 + int new = 0; 362 + 363 + /* 364 + * We have a specific subvol we want to mount, just setup location and 365 + * go look up the root. 366 + */ 367 + if (subvol_objectid) { 368 + location.objectid = subvol_objectid; 369 + location.type = BTRFS_ROOT_ITEM_KEY; 370 + location.offset = (u64)-1; 371 + goto find_root; 372 + } 373 + 374 + path = btrfs_alloc_path(); 375 + if (!path) 376 + return ERR_PTR(-ENOMEM); 377 + path->leave_spinning = 1; 378 + 379 + /* 380 + * Find the "default" dir item which points to the root item that we 381 + * will mount by default if we haven't been given a specific subvolume 382 + * to mount. 383 + */ 384 + dir_id = btrfs_super_root_dir(&root->fs_info->super_copy); 385 + di = btrfs_lookup_dir_item(NULL, root, path, dir_id, "default", 7, 0); 386 + if (!di) { 387 + /* 388 + * Ok the default dir item isn't there. This is weird since 389 + * it's always been there, but don't freak out, just try and 390 + * mount to root most subvolume. 391 + */ 392 + btrfs_free_path(path); 393 + dir_id = BTRFS_FIRST_FREE_OBJECTID; 394 + new_root = root->fs_info->fs_root; 395 + goto setup_root; 396 + } 397 + 398 + btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location); 399 + btrfs_free_path(path); 400 + 401 + find_root: 402 + new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 403 + if (IS_ERR(new_root)) 404 + return ERR_PTR(PTR_ERR(new_root)); 405 + 406 + if (btrfs_root_refs(&new_root->root_item) == 0) 407 + return ERR_PTR(-ENOENT); 408 + 409 + dir_id = btrfs_root_dirid(&new_root->root_item); 410 + setup_root: 411 + location.objectid = dir_id; 412 + location.type = BTRFS_INODE_ITEM_KEY; 413 + location.offset = 0; 414 + 415 + inode = btrfs_iget(sb, &location, new_root, &new); 416 + if (!inode) 417 + return ERR_PTR(-ENOMEM); 418 + 419 + /* 420 + * If we're just mounting the root most subvol put the inode and return 421 + * a reference to the dentry. We will have already gotten a reference 422 + * to the inode in btrfs_fill_super so we're good to go. 423 + */ 424 + if (!new && sb->s_root->d_inode == inode) { 425 + iput(inode); 426 + return dget(sb->s_root); 427 + } 428 + 429 + if (new) { 430 + const struct qstr name = { .name = "/", .len = 1 }; 431 + 432 + /* 433 + * New inode, we need to make the dentry a sibling of s_root so 434 + * everything gets cleaned up properly on unmount. 435 + */ 436 + dentry = d_alloc(sb->s_root, &name); 437 + if (!dentry) { 438 + iput(inode); 439 + return ERR_PTR(-ENOMEM); 440 + } 441 + d_splice_alias(inode, dentry); 442 + } else { 443 + /* 444 + * We found the inode in cache, just find a dentry for it and 445 + * put the reference to the inode we just got. 446 + */ 447 + dentry = d_find_alias(inode); 448 + iput(inode); 449 + } 450 + 451 + return dentry; 452 } 453 454 static int btrfs_fill_super(struct super_block *sb, ··· 380 key.objectid = BTRFS_FIRST_FREE_OBJECTID; 381 key.type = BTRFS_INODE_ITEM_KEY; 382 key.offset = 0; 383 + inode = btrfs_iget(sb, &key, tree_root->fs_info->fs_root, NULL); 384 if (IS_ERR(inode)) { 385 err = PTR_ERR(inode); 386 goto fail_close; ··· 392 err = -ENOMEM; 393 goto fail_close; 394 } 395 396 sb->s_root = root_dentry; 397 ··· 489 static int btrfs_get_sb(struct file_system_type *fs_type, int flags, 490 const char *dev_name, void *data, struct vfsmount *mnt) 491 { 492 struct block_device *bdev = NULL; 493 struct super_block *s; 494 struct dentry *root; 495 struct btrfs_fs_devices *fs_devices = NULL; 496 fmode_t mode = FMODE_READ; 497 + char *subvol_name = NULL; 498 + u64 subvol_objectid = 0; 499 int error = 0; 500 + int found = 0; 501 502 if (!(flags & MS_RDONLY)) 503 mode |= FMODE_WRITE; 504 505 error = btrfs_parse_early_options(data, mode, fs_type, 506 + &subvol_name, &subvol_objectid, 507 + &fs_devices); 508 if (error) 509 return error; 510 ··· 530 goto error_close_devices; 531 } 532 533 + found = 1; 534 btrfs_close_devices(fs_devices); 535 } else { 536 char b[BDEVNAME_SIZE]; ··· 547 s->s_flags |= MS_ACTIVE; 548 } 549 550 + root = get_default_root(s, subvol_objectid); 551 + if (IS_ERR(root)) { 552 + error = PTR_ERR(root); 553 + deactivate_locked_super(s); 554 + goto error; 555 + } 556 + /* if they gave us a subvolume name bind mount into that */ 557 + if (strcmp(subvol_name, ".")) { 558 + struct dentry *new_root; 559 + mutex_lock(&root->d_inode->i_mutex); 560 + new_root = lookup_one_len(subvol_name, root, 561 strlen(subvol_name)); 562 + mutex_unlock(&root->d_inode->i_mutex); 563 564 + if (IS_ERR(new_root)) { 565 deactivate_locked_super(s); 566 + error = PTR_ERR(new_root); 567 dput(root); 568 + goto error_close_devices; 569 + } 570 + if (!new_root->d_inode) { 571 + dput(root); 572 + dput(new_root); 573 deactivate_locked_super(s); 574 error = -ENXIO; 575 + goto error_close_devices; 576 } 577 + dput(root); 578 + root = new_root; 579 } 580 581 mnt->mnt_sb = s; ··· 580 btrfs_close_devices(fs_devices); 581 error_free_subvol_name: 582 kfree(subvol_name); 583 + error: 584 return error; 585 } 586 ··· 624 { 625 struct btrfs_root *root = btrfs_sb(dentry->d_sb); 626 struct btrfs_super_block *disk_super = &root->fs_info->super_copy; 627 + struct list_head *head = &root->fs_info->space_info; 628 + struct btrfs_space_info *found; 629 + u64 total_used = 0; 630 + u64 data_used = 0; 631 int bits = dentry->d_sb->s_blocksize_bits; 632 __be32 *fsid = (__be32 *)root->fs_info->fsid; 633 634 + rcu_read_lock(); 635 + list_for_each_entry_rcu(found, head, list) { 636 + if (found->flags & (BTRFS_BLOCK_GROUP_DUP| 637 + BTRFS_BLOCK_GROUP_RAID10| 638 + BTRFS_BLOCK_GROUP_RAID1)) { 639 + total_used += found->bytes_used; 640 + if (found->flags & BTRFS_BLOCK_GROUP_DATA) 641 + data_used += found->bytes_used; 642 + else 643 + data_used += found->total_bytes; 644 + } 645 + 646 + total_used += found->bytes_used; 647 + if (found->flags & BTRFS_BLOCK_GROUP_DATA) 648 + data_used += found->bytes_used; 649 + else 650 + data_used += found->total_bytes; 651 + } 652 + rcu_read_unlock(); 653 + 654 buf->f_namelen = BTRFS_NAME_LEN; 655 buf->f_blocks = btrfs_super_total_bytes(disk_super) >> bits; 656 + buf->f_bfree = buf->f_blocks - (total_used >> bits); 657 + buf->f_bavail = buf->f_blocks - (data_used >> bits); 658 buf->f_bsize = dentry->d_sb->s_blocksize; 659 buf->f_type = BTRFS_SUPER_MAGIC; 660
+1 -4
fs/btrfs/transaction.c
··· 997 998 mutex_unlock(&root->fs_info->trans_mutex); 999 1000 - if (flush_on_commit) { 1001 btrfs_start_delalloc_inodes(root, 1); 1002 - ret = btrfs_wait_ordered_extents(root, 0, 1); 1003 - BUG_ON(ret); 1004 - } else if (snap_pending) { 1005 ret = btrfs_wait_ordered_extents(root, 0, 1); 1006 BUG_ON(ret); 1007 }
··· 997 998 mutex_unlock(&root->fs_info->trans_mutex); 999 1000 + if (flush_on_commit || snap_pending) { 1001 btrfs_start_delalloc_inodes(root, 1); 1002 ret = btrfs_wait_ordered_extents(root, 0, 1); 1003 BUG_ON(ret); 1004 }
+1 -1
fs/btrfs/tree-log.c
··· 445 key.objectid = objectid; 446 key.type = BTRFS_INODE_ITEM_KEY; 447 key.offset = 0; 448 - inode = btrfs_iget(root->fs_info->sb, &key, root); 449 if (IS_ERR(inode)) { 450 inode = NULL; 451 } else if (is_bad_inode(inode)) {
··· 445 key.objectid = objectid; 446 key.type = BTRFS_INODE_ITEM_KEY; 447 key.offset = 0; 448 + inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); 449 if (IS_ERR(inode)) { 450 inode = NULL; 451 } else if (is_bad_inode(inode)) {
+23 -16
fs/btrfs/volumes.c
··· 256 wake_up(&fs_info->async_submit_wait); 257 258 BUG_ON(atomic_read(&cur->bi_cnt) == 0); 259 - submit_bio(cur->bi_rw, cur); 260 - num_run++; 261 - batch_run++; 262 263 if (bio_rw_flagged(cur, BIO_RW_SYNCIO)) 264 num_sync_run++; 265 266 if (need_resched()) { 267 if (num_sync_run) { 268 blk_run_backing_dev(bdi, NULL); ··· 325 num_sync_run = 0; 326 blk_run_backing_dev(bdi, NULL); 327 } 328 - 329 - cond_resched(); 330 - if (again) 331 - goto loop; 332 - 333 - spin_lock(&device->io_lock); 334 - if (device->pending_bios.head || device->pending_sync_bios.head) 335 - goto loop_lock; 336 - spin_unlock(&device->io_lock); 337 - 338 /* 339 * IO has already been through a long path to get here. Checksumming, 340 * async helper threads, perhaps compression. We've done a pretty ··· 336 * cared about found its way down here. 337 */ 338 blk_run_backing_dev(bdi, NULL); 339 done: 340 return 0; 341 } ··· 365 struct btrfs_device *device; 366 struct btrfs_fs_devices *fs_devices; 367 u64 found_transid = btrfs_super_generation(disk_super); 368 369 fs_devices = find_fsid(disk_super->fsid); 370 if (!fs_devices) { ··· 412 413 device->fs_devices = fs_devices; 414 fs_devices->num_devices++; 415 } 416 417 if (found_transid > fs_devices->latest_trans) { ··· 599 goto error_close; 600 601 disk_super = (struct btrfs_super_block *)bh->b_data; 602 - devid = le64_to_cpu(disk_super->dev_item.devid); 603 if (devid != device->devid) 604 goto error_brelse; 605 ··· 701 goto error_close; 702 } 703 disk_super = (struct btrfs_super_block *)bh->b_data; 704 - devid = le64_to_cpu(disk_super->dev_item.devid); 705 transid = btrfs_super_generation(disk_super); 706 if (disk_super->label[0]) 707 printk(KERN_INFO "device label %s ", disk_super->label); ··· 1194 goto error_close; 1195 } 1196 disk_super = (struct btrfs_super_block *)bh->b_data; 1197 - devid = le64_to_cpu(disk_super->dev_item.devid); 1198 dev_uuid = disk_super->dev_item.uuid; 1199 device = btrfs_find_device(root, devid, dev_uuid, 1200 disk_super->fsid);
··· 256 wake_up(&fs_info->async_submit_wait); 257 258 BUG_ON(atomic_read(&cur->bi_cnt) == 0); 259 260 if (bio_rw_flagged(cur, BIO_RW_SYNCIO)) 261 num_sync_run++; 262 263 + submit_bio(cur->bi_rw, cur); 264 + num_run++; 265 + batch_run++; 266 if (need_resched()) { 267 if (num_sync_run) { 268 blk_run_backing_dev(bdi, NULL); ··· 325 num_sync_run = 0; 326 blk_run_backing_dev(bdi, NULL); 327 } 328 /* 329 * IO has already been through a long path to get here. Checksumming, 330 * async helper threads, perhaps compression. We've done a pretty ··· 346 * cared about found its way down here. 347 */ 348 blk_run_backing_dev(bdi, NULL); 349 + 350 + cond_resched(); 351 + if (again) 352 + goto loop; 353 + 354 + spin_lock(&device->io_lock); 355 + if (device->pending_bios.head || device->pending_sync_bios.head) 356 + goto loop_lock; 357 + spin_unlock(&device->io_lock); 358 + 359 done: 360 return 0; 361 } ··· 365 struct btrfs_device *device; 366 struct btrfs_fs_devices *fs_devices; 367 u64 found_transid = btrfs_super_generation(disk_super); 368 + char *name; 369 370 fs_devices = find_fsid(disk_super->fsid); 371 if (!fs_devices) { ··· 411 412 device->fs_devices = fs_devices; 413 fs_devices->num_devices++; 414 + } else if (strcmp(device->name, path)) { 415 + name = kstrdup(path, GFP_NOFS); 416 + if (!name) 417 + return -ENOMEM; 418 + kfree(device->name); 419 + device->name = name; 420 } 421 422 if (found_transid > fs_devices->latest_trans) { ··· 592 goto error_close; 593 594 disk_super = (struct btrfs_super_block *)bh->b_data; 595 + devid = btrfs_stack_device_id(&disk_super->dev_item); 596 if (devid != device->devid) 597 goto error_brelse; 598 ··· 694 goto error_close; 695 } 696 disk_super = (struct btrfs_super_block *)bh->b_data; 697 + devid = btrfs_stack_device_id(&disk_super->dev_item); 698 transid = btrfs_super_generation(disk_super); 699 if (disk_super->label[0]) 700 printk(KERN_INFO "device label %s ", disk_super->label); ··· 1187 goto error_close; 1188 } 1189 disk_super = (struct btrfs_super_block *)bh->b_data; 1190 + devid = btrfs_stack_device_id(&disk_super->dev_item); 1191 dev_uuid = disk_super->dev_item.uuid; 1192 device = btrfs_find_device(root, devid, dev_uuid, 1193 disk_super->fsid);