aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c366
1 files changed, 237 insertions, 129 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 119520bdb9a5..93c28a1d6bdc 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -50,6 +50,7 @@
50#include "tree-log.h" 50#include "tree-log.h"
51#include "compression.h" 51#include "compression.h"
52#include "locking.h" 52#include "locking.h"
53#include "free-space-cache.h"
53 54
54struct btrfs_iget_args { 55struct btrfs_iget_args {
55 u64 ino; 56 u64 ino;
@@ -70,6 +71,7 @@ static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep; 71struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep; 72struct kmem_cache *btrfs_transaction_cachep;
72struct kmem_cache *btrfs_path_cachep; 73struct kmem_cache *btrfs_path_cachep;
74struct kmem_cache *btrfs_free_space_cachep;
73 75
74#define S_SHIFT 12 76#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { 77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
@@ -82,7 +84,8 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, 84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83}; 85};
84 86
85static void btrfs_truncate(struct inode *inode); 87static int btrfs_setsize(struct inode *inode, loff_t newsize);
88static int btrfs_truncate(struct inode *inode);
86static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end); 89static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
87static noinline int cow_file_range(struct inode *inode, 90static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page, 91 struct page *locked_page,
@@ -288,6 +291,7 @@ static noinline int add_async_extent(struct async_cow *cow,
288 struct async_extent *async_extent; 291 struct async_extent *async_extent;
289 292
290 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS); 293 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
294 BUG_ON(!async_extent);
291 async_extent->start = start; 295 async_extent->start = start;
292 async_extent->ram_size = ram_size; 296 async_extent->ram_size = ram_size;
293 async_extent->compressed_size = compressed_size; 297 async_extent->compressed_size = compressed_size;
@@ -382,9 +386,11 @@ again:
382 */ 386 */
383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) && 387 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
384 (btrfs_test_opt(root, COMPRESS) || 388 (btrfs_test_opt(root, COMPRESS) ||
385 (BTRFS_I(inode)->force_compress))) { 389 (BTRFS_I(inode)->force_compress) ||
390 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
386 WARN_ON(pages); 391 WARN_ON(pages);
387 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS); 392 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
393 BUG_ON(!pages);
388 394
389 if (BTRFS_I(inode)->force_compress) 395 if (BTRFS_I(inode)->force_compress)
390 compress_type = BTRFS_I(inode)->force_compress; 396 compress_type = BTRFS_I(inode)->force_compress;
@@ -1254,7 +1260,8 @@ static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1254 ret = run_delalloc_nocow(inode, locked_page, start, end, 1260 ret = run_delalloc_nocow(inode, locked_page, start, end,
1255 page_started, 0, nr_written); 1261 page_started, 0, nr_written);
1256 else if (!btrfs_test_opt(root, COMPRESS) && 1262 else if (!btrfs_test_opt(root, COMPRESS) &&
1257 !(BTRFS_I(inode)->force_compress)) 1263 !(BTRFS_I(inode)->force_compress) &&
1264 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1258 ret = cow_file_range(inode, locked_page, start, end, 1265 ret = cow_file_range(inode, locked_page, start, end,
1259 page_started, nr_written, 1); 1266 page_started, nr_written, 1);
1260 else 1267 else
@@ -1461,8 +1468,11 @@ static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1461 if (bio_flags & EXTENT_BIO_COMPRESSED) { 1468 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1462 return btrfs_submit_compressed_read(inode, bio, 1469 return btrfs_submit_compressed_read(inode, bio,
1463 mirror_num, bio_flags); 1470 mirror_num, bio_flags);
1464 } else if (!skip_sum) 1471 } else if (!skip_sum) {
1465 btrfs_lookup_bio_sums(root, inode, bio, NULL); 1472 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1473 if (ret)
1474 return ret;
1475 }
1466 goto mapit; 1476 goto mapit;
1467 } else if (!skip_sum) { 1477 } else if (!skip_sum) {
1468 /* csum items have already been cloned */ 1478 /* csum items have already been cloned */
@@ -1785,6 +1795,8 @@ out:
1785static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, 1795static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1786 struct extent_state *state, int uptodate) 1796 struct extent_state *state, int uptodate)
1787{ 1797{
1798 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1799
1788 ClearPagePrivate2(page); 1800 ClearPagePrivate2(page);
1789 return btrfs_finish_ordered_io(page->mapping->host, start, end); 1801 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1790} 1802}
@@ -1895,10 +1907,10 @@ static int btrfs_io_failed_hook(struct bio *failed_bio,
1895 else 1907 else
1896 rw = READ; 1908 rw = READ;
1897 1909
1898 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, 1910 ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1899 failrec->last_mirror, 1911 failrec->last_mirror,
1900 failrec->bio_flags, 0); 1912 failrec->bio_flags, 0);
1901 return 0; 1913 return ret;
1902} 1914}
1903 1915
1904/* 1916/*
@@ -2282,7 +2294,7 @@ int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2282 * this cleans up any orphans that may be left on the list from the last use 2294 * this cleans up any orphans that may be left on the list from the last use
2283 * of this root. 2295 * of this root.
2284 */ 2296 */
2285void btrfs_orphan_cleanup(struct btrfs_root *root) 2297int btrfs_orphan_cleanup(struct btrfs_root *root)
2286{ 2298{
2287 struct btrfs_path *path; 2299 struct btrfs_path *path;
2288 struct extent_buffer *leaf; 2300 struct extent_buffer *leaf;
@@ -2292,10 +2304,13 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2292 int ret = 0, nr_unlink = 0, nr_truncate = 0; 2304 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2293 2305
2294 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED)) 2306 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2295 return; 2307 return 0;
2296 2308
2297 path = btrfs_alloc_path(); 2309 path = btrfs_alloc_path();
2298 BUG_ON(!path); 2310 if (!path) {
2311 ret = -ENOMEM;
2312 goto out;
2313 }
2299 path->reada = -1; 2314 path->reada = -1;
2300 2315
2301 key.objectid = BTRFS_ORPHAN_OBJECTID; 2316 key.objectid = BTRFS_ORPHAN_OBJECTID;
@@ -2304,11 +2319,8 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2304 2319
2305 while (1) { 2320 while (1) {
2306 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2307 if (ret < 0) { 2322 if (ret < 0)
2308 printk(KERN_ERR "Error searching slot for orphan: %d" 2323 goto out;
2309 "\n", ret);
2310 break;
2311 }
2312 2324
2313 /* 2325 /*
2314 * if ret == 0 means we found what we were searching for, which 2326 * if ret == 0 means we found what we were searching for, which
@@ -2316,6 +2328,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2316 * find the key and see if we have stuff that matches 2328 * find the key and see if we have stuff that matches
2317 */ 2329 */
2318 if (ret > 0) { 2330 if (ret > 0) {
2331 ret = 0;
2319 if (path->slots[0] == 0) 2332 if (path->slots[0] == 0)
2320 break; 2333 break;
2321 path->slots[0]--; 2334 path->slots[0]--;
@@ -2343,7 +2356,10 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2343 found_key.type = BTRFS_INODE_ITEM_KEY; 2356 found_key.type = BTRFS_INODE_ITEM_KEY;
2344 found_key.offset = 0; 2357 found_key.offset = 0;
2345 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); 2358 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2346 BUG_ON(IS_ERR(inode)); 2359 if (IS_ERR(inode)) {
2360 ret = PTR_ERR(inode);
2361 goto out;
2362 }
2347 2363
2348 /* 2364 /*
2349 * add this inode to the orphan list so btrfs_orphan_del does 2365 * add this inode to the orphan list so btrfs_orphan_del does
@@ -2361,7 +2377,10 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2361 */ 2377 */
2362 if (is_bad_inode(inode)) { 2378 if (is_bad_inode(inode)) {
2363 trans = btrfs_start_transaction(root, 0); 2379 trans = btrfs_start_transaction(root, 0);
2364 BUG_ON(IS_ERR(trans)); 2380 if (IS_ERR(trans)) {
2381 ret = PTR_ERR(trans);
2382 goto out;
2383 }
2365 btrfs_orphan_del(trans, inode); 2384 btrfs_orphan_del(trans, inode);
2366 btrfs_end_transaction(trans, root); 2385 btrfs_end_transaction(trans, root);
2367 iput(inode); 2386 iput(inode);
@@ -2370,17 +2389,22 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2370 2389
2371 /* if we have links, this was a truncate, lets do that */ 2390 /* if we have links, this was a truncate, lets do that */
2372 if (inode->i_nlink) { 2391 if (inode->i_nlink) {
2392 if (!S_ISREG(inode->i_mode)) {
2393 WARN_ON(1);
2394 iput(inode);
2395 continue;
2396 }
2373 nr_truncate++; 2397 nr_truncate++;
2374 btrfs_truncate(inode); 2398 ret = btrfs_truncate(inode);
2375 } else { 2399 } else {
2376 nr_unlink++; 2400 nr_unlink++;
2377 } 2401 }
2378 2402
2379 /* this will do delete_inode and everything for us */ 2403 /* this will do delete_inode and everything for us */
2380 iput(inode); 2404 iput(inode);
2405 if (ret)
2406 goto out;
2381 } 2407 }
2382 btrfs_free_path(path);
2383
2384 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; 2408 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2385 2409
2386 if (root->orphan_block_rsv) 2410 if (root->orphan_block_rsv)
@@ -2389,14 +2413,20 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2389 2413
2390 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2414 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2391 trans = btrfs_join_transaction(root, 1); 2415 trans = btrfs_join_transaction(root, 1);
2392 BUG_ON(IS_ERR(trans)); 2416 if (!IS_ERR(trans))
2393 btrfs_end_transaction(trans, root); 2417 btrfs_end_transaction(trans, root);
2394 } 2418 }
2395 2419
2396 if (nr_unlink) 2420 if (nr_unlink)
2397 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); 2421 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2398 if (nr_truncate) 2422 if (nr_truncate)
2399 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); 2423 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2424
2425out:
2426 if (ret)
2427 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2428 btrfs_free_path(path);
2429 return ret;
2400} 2430}
2401 2431
2402/* 2432/*
@@ -2507,6 +2537,8 @@ static void btrfs_read_locked_inode(struct inode *inode)
2507 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 2537 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2508 2538
2509 alloc_group_block = btrfs_inode_block_group(leaf, inode_item); 2539 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2540 if (location.objectid == BTRFS_FREE_SPACE_OBJECTID)
2541 inode->i_mapping->flags &= ~__GFP_FS;
2510 2542
2511 /* 2543 /*
2512 * try to precache a NULL acl entry for files that don't have 2544 * try to precache a NULL acl entry for files that don't have
@@ -2635,10 +2667,10 @@ failed:
2635 * recovery code. It remove a link in a directory with a given name, and 2667 * recovery code. It remove a link in a directory with a given name, and
2636 * also drops the back refs in the inode to the directory 2668 * also drops the back refs in the inode to the directory
2637 */ 2669 */
2638int btrfs_unlink_inode(struct btrfs_trans_handle *trans, 2670static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2639 struct btrfs_root *root, 2671 struct btrfs_root *root,
2640 struct inode *dir, struct inode *inode, 2672 struct inode *dir, struct inode *inode,
2641 const char *name, int name_len) 2673 const char *name, int name_len)
2642{ 2674{
2643 struct btrfs_path *path; 2675 struct btrfs_path *path;
2644 int ret = 0; 2676 int ret = 0;
@@ -2710,12 +2742,25 @@ err:
2710 btrfs_i_size_write(dir, dir->i_size - name_len * 2); 2742 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2711 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME; 2743 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2712 btrfs_update_inode(trans, root, dir); 2744 btrfs_update_inode(trans, root, dir);
2713 btrfs_drop_nlink(inode);
2714 ret = btrfs_update_inode(trans, root, inode);
2715out: 2745out:
2716 return ret; 2746 return ret;
2717} 2747}
2718 2748
2749int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2750 struct btrfs_root *root,
2751 struct inode *dir, struct inode *inode,
2752 const char *name, int name_len)
2753{
2754 int ret;
2755 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2756 if (!ret) {
2757 btrfs_drop_nlink(inode);
2758 ret = btrfs_update_inode(trans, root, inode);
2759 }
2760 return ret;
2761}
2762
2763
2719/* helper to check if there is any shared block in the path */ 2764/* helper to check if there is any shared block in the path */
2720static int check_path_shared(struct btrfs_root *root, 2765static int check_path_shared(struct btrfs_root *root,
2721 struct btrfs_path *path) 2766 struct btrfs_path *path)
@@ -3537,7 +3582,13 @@ out:
3537 return ret; 3582 return ret;
3538} 3583}
3539 3584
3540int btrfs_cont_expand(struct inode *inode, loff_t size) 3585/*
3586 * This function puts in dummy file extents for the area we're creating a hole
3587 * for. So if we are truncating this file to a larger size we need to insert
3588 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3589 * the range between oldsize and size
3590 */
3591int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3541{ 3592{
3542 struct btrfs_trans_handle *trans; 3593 struct btrfs_trans_handle *trans;
3543 struct btrfs_root *root = BTRFS_I(inode)->root; 3594 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -3545,7 +3596,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3545 struct extent_map *em = NULL; 3596 struct extent_map *em = NULL;
3546 struct extent_state *cached_state = NULL; 3597 struct extent_state *cached_state = NULL;
3547 u64 mask = root->sectorsize - 1; 3598 u64 mask = root->sectorsize - 1;
3548 u64 hole_start = (inode->i_size + mask) & ~mask; 3599 u64 hole_start = (oldsize + mask) & ~mask;
3549 u64 block_end = (size + mask) & ~mask; 3600 u64 block_end = (size + mask) & ~mask;
3550 u64 last_byte; 3601 u64 last_byte;
3551 u64 cur_offset; 3602 u64 cur_offset;
@@ -3590,13 +3641,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3590 err = btrfs_drop_extents(trans, inode, cur_offset, 3641 err = btrfs_drop_extents(trans, inode, cur_offset,
3591 cur_offset + hole_size, 3642 cur_offset + hole_size,
3592 &hint_byte, 1); 3643 &hint_byte, 1);
3593 BUG_ON(err); 3644 if (err)
3645 break;
3594 3646
3595 err = btrfs_insert_file_extent(trans, root, 3647 err = btrfs_insert_file_extent(trans, root,
3596 inode->i_ino, cur_offset, 0, 3648 inode->i_ino, cur_offset, 0,
3597 0, hole_size, 0, hole_size, 3649 0, hole_size, 0, hole_size,
3598 0, 0, 0); 3650 0, 0, 0);
3599 BUG_ON(err); 3651 if (err)
3652 break;
3600 3653
3601 btrfs_drop_extent_cache(inode, hole_start, 3654 btrfs_drop_extent_cache(inode, hole_start,
3602 last_byte - 1, 0); 3655 last_byte - 1, 0);
@@ -3616,81 +3669,41 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3616 return err; 3669 return err;
3617} 3670}
3618 3671
3619static int btrfs_setattr_size(struct inode *inode, struct iattr *attr) 3672static int btrfs_setsize(struct inode *inode, loff_t newsize)
3620{ 3673{
3621 struct btrfs_root *root = BTRFS_I(inode)->root; 3674 loff_t oldsize = i_size_read(inode);
3622 struct btrfs_trans_handle *trans;
3623 unsigned long nr;
3624 int ret; 3675 int ret;
3625 3676
3626 if (attr->ia_size == inode->i_size) 3677 if (newsize == oldsize)
3627 return 0; 3678 return 0;
3628 3679
3629 if (attr->ia_size > inode->i_size) { 3680 if (newsize > oldsize) {
3630 unsigned long limit; 3681 i_size_write(inode, newsize);
3631 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; 3682 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3632 if (attr->ia_size > inode->i_sb->s_maxbytes) 3683 truncate_pagecache(inode, oldsize, newsize);
3633 return -EFBIG; 3684 ret = btrfs_cont_expand(inode, oldsize, newsize);
3634 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3635 send_sig(SIGXFSZ, current, 0);
3636 return -EFBIG;
3637 }
3638 }
3639
3640 trans = btrfs_start_transaction(root, 5);
3641 if (IS_ERR(trans))
3642 return PTR_ERR(trans);
3643
3644 btrfs_set_trans_block_group(trans, inode);
3645
3646 ret = btrfs_orphan_add(trans, inode);
3647 BUG_ON(ret);
3648
3649 nr = trans->blocks_used;
3650 btrfs_end_transaction(trans, root);
3651 btrfs_btree_balance_dirty(root, nr);
3652
3653 if (attr->ia_size > inode->i_size) {
3654 ret = btrfs_cont_expand(inode, attr->ia_size);
3655 if (ret) { 3685 if (ret) {
3656 btrfs_truncate(inode); 3686 btrfs_setsize(inode, oldsize);
3657 return ret; 3687 return ret;
3658 } 3688 }
3659 3689
3660 i_size_write(inode, attr->ia_size); 3690 mark_inode_dirty(inode);
3661 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 3691 } else {
3662 3692
3663 trans = btrfs_start_transaction(root, 0); 3693 /*
3664 BUG_ON(IS_ERR(trans)); 3694 * We're truncating a file that used to have good data down to
3665 btrfs_set_trans_block_group(trans, inode); 3695 * zero. Make sure it gets into the ordered flush list so that
3666 trans->block_rsv = root->orphan_block_rsv; 3696 * any new writes get down to disk quickly.
3667 BUG_ON(!trans->block_rsv); 3697 */
3698 if (newsize == 0)
3699 BTRFS_I(inode)->ordered_data_close = 1;
3668 3700
3669 ret = btrfs_update_inode(trans, root, inode); 3701 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3670 BUG_ON(ret); 3702 truncate_setsize(inode, newsize);
3671 if (inode->i_nlink > 0) { 3703 ret = btrfs_truncate(inode);
3672 ret = btrfs_orphan_del(trans, inode);
3673 BUG_ON(ret);
3674 }
3675 nr = trans->blocks_used;
3676 btrfs_end_transaction(trans, root);
3677 btrfs_btree_balance_dirty(root, nr);
3678 return 0;
3679 } 3704 }
3680 3705
3681 /* 3706 return ret;
3682 * We're truncating a file that used to have good data down to
3683 * zero. Make sure it gets into the ordered flush list so that
3684 * any new writes get down to disk quickly.
3685 */
3686 if (attr->ia_size == 0)
3687 BTRFS_I(inode)->ordered_data_close = 1;
3688
3689 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3690 ret = vmtruncate(inode, attr->ia_size);
3691 BUG_ON(ret);
3692
3693 return 0;
3694} 3707}
3695 3708
3696static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) 3709static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
@@ -3707,7 +3720,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3707 return err; 3720 return err;
3708 3721
3709 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 3722 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3710 err = btrfs_setattr_size(inode, attr); 3723 err = btrfs_setsize(inode, attr->ia_size);
3711 if (err) 3724 if (err)
3712 return err; 3725 return err;
3713 } 3726 }
@@ -3730,6 +3743,8 @@ void btrfs_evict_inode(struct inode *inode)
3730 unsigned long nr; 3743 unsigned long nr;
3731 int ret; 3744 int ret;
3732 3745
3746 trace_btrfs_inode_evict(inode);
3747
3733 truncate_inode_pages(&inode->i_data, 0); 3748 truncate_inode_pages(&inode->i_data, 0);
3734 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 || 3749 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3735 root == root->fs_info->tree_root)) 3750 root == root->fs_info->tree_root))
@@ -4072,7 +4087,6 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4072 BTRFS_I(inode)->root = root; 4087 BTRFS_I(inode)->root = root;
4073 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); 4088 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4074 btrfs_read_locked_inode(inode); 4089 btrfs_read_locked_inode(inode);
4075
4076 inode_tree_add(inode); 4090 inode_tree_add(inode);
4077 unlock_new_inode(inode); 4091 unlock_new_inode(inode);
4078 if (new) 4092 if (new)
@@ -4147,8 +4161,10 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4147 if (!IS_ERR(inode) && root != sub_root) { 4161 if (!IS_ERR(inode) && root != sub_root) {
4148 down_read(&root->fs_info->cleanup_work_sem); 4162 down_read(&root->fs_info->cleanup_work_sem);
4149 if (!(inode->i_sb->s_flags & MS_RDONLY)) 4163 if (!(inode->i_sb->s_flags & MS_RDONLY))
4150 btrfs_orphan_cleanup(sub_root); 4164 ret = btrfs_orphan_cleanup(sub_root);
4151 up_read(&root->fs_info->cleanup_work_sem); 4165 up_read(&root->fs_info->cleanup_work_sem);
4166 if (ret)
4167 inode = ERR_PTR(ret);
4152 } 4168 }
4153 4169
4154 return inode; 4170 return inode;
@@ -4282,6 +4298,9 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4282 while (di_cur < di_total) { 4298 while (di_cur < di_total) {
4283 struct btrfs_key location; 4299 struct btrfs_key location;
4284 4300
4301 if (verify_dir_item(root, leaf, di))
4302 break;
4303
4285 name_len = btrfs_dir_name_len(leaf, di); 4304 name_len = btrfs_dir_name_len(leaf, di);
4286 if (name_len <= sizeof(tmp_name)) { 4305 if (name_len <= sizeof(tmp_name)) {
4287 name_ptr = tmp_name; 4306 name_ptr = tmp_name;
@@ -4517,6 +4536,8 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4517 return ERR_PTR(-ENOMEM); 4536 return ERR_PTR(-ENOMEM);
4518 4537
4519 if (dir) { 4538 if (dir) {
4539 trace_btrfs_inode_request(dir);
4540
4520 ret = btrfs_set_inode_index(dir, index); 4541 ret = btrfs_set_inode_index(dir, index);
4521 if (ret) { 4542 if (ret) {
4522 iput(inode); 4543 iput(inode);
@@ -4585,12 +4606,16 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4585 if ((mode & S_IFREG)) { 4606 if ((mode & S_IFREG)) {
4586 if (btrfs_test_opt(root, NODATASUM)) 4607 if (btrfs_test_opt(root, NODATASUM))
4587 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 4608 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4588 if (btrfs_test_opt(root, NODATACOW)) 4609 if (btrfs_test_opt(root, NODATACOW) ||
4610 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4589 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; 4611 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4590 } 4612 }
4591 4613
4592 insert_inode_hash(inode); 4614 insert_inode_hash(inode);
4593 inode_tree_add(inode); 4615 inode_tree_add(inode);
4616
4617 trace_btrfs_inode_new(inode);
4618
4594 return inode; 4619 return inode;
4595fail: 4620fail:
4596 if (dir) 4621 if (dir)
@@ -4809,7 +4834,10 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4809 4834
4810 /* do not allow sys_link's with other subvols of the same device */ 4835 /* do not allow sys_link's with other subvols of the same device */
4811 if (root->objectid != BTRFS_I(inode)->root->objectid) 4836 if (root->objectid != BTRFS_I(inode)->root->objectid)
4812 return -EPERM; 4837 return -EXDEV;
4838
4839 if (inode->i_nlink == ~0U)
4840 return -EMLINK;
4813 4841
4814 btrfs_inc_nlink(inode); 4842 btrfs_inc_nlink(inode);
4815 inode->i_ctime = CURRENT_TIME; 4843 inode->i_ctime = CURRENT_TIME;
@@ -5265,6 +5293,9 @@ insert:
5265 } 5293 }
5266 write_unlock(&em_tree->lock); 5294 write_unlock(&em_tree->lock);
5267out: 5295out:
5296
5297 trace_btrfs_get_extent(root, em);
5298
5268 if (path) 5299 if (path)
5269 btrfs_free_path(path); 5300 btrfs_free_path(path);
5270 if (trans) { 5301 if (trans) {
@@ -5748,6 +5779,10 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
5748 5779
5749 kfree(dip->csums); 5780 kfree(dip->csums);
5750 kfree(dip); 5781 kfree(dip);
5782
5783 /* If we had a csum failure make sure to clear the uptodate flag */
5784 if (err)
5785 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5751 dio_end_io(bio, err); 5786 dio_end_io(bio, err);
5752} 5787}
5753 5788
@@ -5849,6 +5884,10 @@ out_done:
5849 5884
5850 kfree(dip->csums); 5885 kfree(dip->csums);
5851 kfree(dip); 5886 kfree(dip);
5887
5888 /* If we had an error make sure to clear the uptodate flag */
5889 if (err)
5890 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5852 dio_end_io(bio, err); 5891 dio_end_io(bio, err);
5853} 5892}
5854 5893
@@ -5922,9 +5961,12 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5922 __btrfs_submit_bio_start_direct_io, 5961 __btrfs_submit_bio_start_direct_io,
5923 __btrfs_submit_bio_done); 5962 __btrfs_submit_bio_done);
5924 goto err; 5963 goto err;
5925 } else if (!skip_sum) 5964 } else if (!skip_sum) {
5926 btrfs_lookup_bio_sums_dio(root, inode, bio, 5965 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
5927 file_offset, csums); 5966 file_offset, csums);
5967 if (ret)
5968 goto err;
5969 }
5928 5970
5929 ret = btrfs_map_bio(root, rw, bio, 0, 1); 5971 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5930err: 5972err:
@@ -5948,6 +5990,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5948 int nr_pages = 0; 5990 int nr_pages = 0;
5949 u32 *csums = dip->csums; 5991 u32 *csums = dip->csums;
5950 int ret = 0; 5992 int ret = 0;
5993 int write = rw & REQ_WRITE;
5951 5994
5952 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS); 5995 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5953 if (!bio) 5996 if (!bio)
@@ -5984,7 +6027,8 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5984 goto out_err; 6027 goto out_err;
5985 } 6028 }
5986 6029
5987 if (!skip_sum) 6030 /* Write's use the ordered csums */
6031 if (!write && !skip_sum)
5988 csums = csums + nr_pages; 6032 csums = csums + nr_pages;
5989 start_sector += submit_len >> 9; 6033 start_sector += submit_len >> 9;
5990 file_offset += submit_len; 6034 file_offset += submit_len;
@@ -6052,7 +6096,8 @@ static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6052 } 6096 }
6053 dip->csums = NULL; 6097 dip->csums = NULL;
6054 6098
6055 if (!skip_sum) { 6099 /* Write's use the ordered csum stuff, so we don't need dip->csums */
6100 if (!write && !skip_sum) {
6056 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS); 6101 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6057 if (!dip->csums) { 6102 if (!dip->csums) {
6058 kfree(dip); 6103 kfree(dip);
@@ -6474,28 +6519,42 @@ out:
6474 return ret; 6519 return ret;
6475} 6520}
6476 6521
6477static void btrfs_truncate(struct inode *inode) 6522static int btrfs_truncate(struct inode *inode)
6478{ 6523{
6479 struct btrfs_root *root = BTRFS_I(inode)->root; 6524 struct btrfs_root *root = BTRFS_I(inode)->root;
6480 int ret; 6525 int ret;
6526 int err = 0;
6481 struct btrfs_trans_handle *trans; 6527 struct btrfs_trans_handle *trans;
6482 unsigned long nr; 6528 unsigned long nr;
6483 u64 mask = root->sectorsize - 1; 6529 u64 mask = root->sectorsize - 1;
6484 6530
6485 if (!S_ISREG(inode->i_mode)) {
6486 WARN_ON(1);
6487 return;
6488 }
6489
6490 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size); 6531 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6491 if (ret) 6532 if (ret)
6492 return; 6533 return ret;
6493 6534
6494 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 6535 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6495 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 6536 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6496 6537
6538 trans = btrfs_start_transaction(root, 5);
6539 if (IS_ERR(trans))
6540 return PTR_ERR(trans);
6541
6542 btrfs_set_trans_block_group(trans, inode);
6543
6544 ret = btrfs_orphan_add(trans, inode);
6545 if (ret) {
6546 btrfs_end_transaction(trans, root);
6547 return ret;
6548 }
6549
6550 nr = trans->blocks_used;
6551 btrfs_end_transaction(trans, root);
6552 btrfs_btree_balance_dirty(root, nr);
6553
6554 /* Now start a transaction for the truncate */
6497 trans = btrfs_start_transaction(root, 0); 6555 trans = btrfs_start_transaction(root, 0);
6498 BUG_ON(IS_ERR(trans)); 6556 if (IS_ERR(trans))
6557 return PTR_ERR(trans);
6499 btrfs_set_trans_block_group(trans, inode); 6558 btrfs_set_trans_block_group(trans, inode);
6500 trans->block_rsv = root->orphan_block_rsv; 6559 trans->block_rsv = root->orphan_block_rsv;
6501 6560
@@ -6522,29 +6581,38 @@ static void btrfs_truncate(struct inode *inode)
6522 while (1) { 6581 while (1) {
6523 if (!trans) { 6582 if (!trans) {
6524 trans = btrfs_start_transaction(root, 0); 6583 trans = btrfs_start_transaction(root, 0);
6525 BUG_ON(IS_ERR(trans)); 6584 if (IS_ERR(trans))
6585 return PTR_ERR(trans);
6526 btrfs_set_trans_block_group(trans, inode); 6586 btrfs_set_trans_block_group(trans, inode);
6527 trans->block_rsv = root->orphan_block_rsv; 6587 trans->block_rsv = root->orphan_block_rsv;
6528 } 6588 }
6529 6589
6530 ret = btrfs_block_rsv_check(trans, root, 6590 ret = btrfs_block_rsv_check(trans, root,
6531 root->orphan_block_rsv, 0, 5); 6591 root->orphan_block_rsv, 0, 5);
6532 if (ret) { 6592 if (ret == -EAGAIN) {
6533 BUG_ON(ret != -EAGAIN);
6534 ret = btrfs_commit_transaction(trans, root); 6593 ret = btrfs_commit_transaction(trans, root);
6535 BUG_ON(ret); 6594 if (ret)
6595 return ret;
6536 trans = NULL; 6596 trans = NULL;
6537 continue; 6597 continue;
6598 } else if (ret) {
6599 err = ret;
6600 break;
6538 } 6601 }
6539 6602
6540 ret = btrfs_truncate_inode_items(trans, root, inode, 6603 ret = btrfs_truncate_inode_items(trans, root, inode,
6541 inode->i_size, 6604 inode->i_size,
6542 BTRFS_EXTENT_DATA_KEY); 6605 BTRFS_EXTENT_DATA_KEY);
6543 if (ret != -EAGAIN) 6606 if (ret != -EAGAIN) {
6607 err = ret;
6544 break; 6608 break;
6609 }
6545 6610
6546 ret = btrfs_update_inode(trans, root, inode); 6611 ret = btrfs_update_inode(trans, root, inode);
6547 BUG_ON(ret); 6612 if (ret) {
6613 err = ret;
6614 break;
6615 }
6548 6616
6549 nr = trans->blocks_used; 6617 nr = trans->blocks_used;
6550 btrfs_end_transaction(trans, root); 6618 btrfs_end_transaction(trans, root);
@@ -6554,16 +6622,27 @@ static void btrfs_truncate(struct inode *inode)
6554 6622
6555 if (ret == 0 && inode->i_nlink > 0) { 6623 if (ret == 0 && inode->i_nlink > 0) {
6556 ret = btrfs_orphan_del(trans, inode); 6624 ret = btrfs_orphan_del(trans, inode);
6557 BUG_ON(ret); 6625 if (ret)
6626 err = ret;
6627 } else if (ret && inode->i_nlink > 0) {
6628 /*
6629 * Failed to do the truncate, remove us from the in memory
6630 * orphan list.
6631 */
6632 ret = btrfs_orphan_del(NULL, inode);
6558 } 6633 }
6559 6634
6560 ret = btrfs_update_inode(trans, root, inode); 6635 ret = btrfs_update_inode(trans, root, inode);
6561 BUG_ON(ret); 6636 if (ret && !err)
6637 err = ret;
6562 6638
6563 nr = trans->blocks_used; 6639 nr = trans->blocks_used;
6564 ret = btrfs_end_transaction_throttle(trans, root); 6640 ret = btrfs_end_transaction_throttle(trans, root);
6565 BUG_ON(ret); 6641 if (ret && !err)
6642 err = ret;
6566 btrfs_btree_balance_dirty(root, nr); 6643 btrfs_btree_balance_dirty(root, nr);
6644
6645 return err;
6567} 6646}
6568 6647
6569/* 6648/*
@@ -6630,9 +6709,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6630 ei->index_cnt = (u64)-1; 6709 ei->index_cnt = (u64)-1;
6631 ei->last_unlink_trans = 0; 6710 ei->last_unlink_trans = 0;
6632 6711
6633 spin_lock_init(&ei->accounting_lock);
6634 atomic_set(&ei->outstanding_extents, 0); 6712 atomic_set(&ei->outstanding_extents, 0);
6635 ei->reserved_extents = 0; 6713 atomic_set(&ei->reserved_extents, 0);
6636 6714
6637 ei->ordered_data_close = 0; 6715 ei->ordered_data_close = 0;
6638 ei->orphan_meta_reserved = 0; 6716 ei->orphan_meta_reserved = 0;
@@ -6668,7 +6746,7 @@ void btrfs_destroy_inode(struct inode *inode)
6668 WARN_ON(!list_empty(&inode->i_dentry)); 6746 WARN_ON(!list_empty(&inode->i_dentry));
6669 WARN_ON(inode->i_data.nrpages); 6747 WARN_ON(inode->i_data.nrpages);
6670 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents)); 6748 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6671 WARN_ON(BTRFS_I(inode)->reserved_extents); 6749 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
6672 6750
6673 /* 6751 /*
6674 * This can happen where we create an inode, but somebody else also 6752 * This can happen where we create an inode, but somebody else also
@@ -6760,6 +6838,8 @@ void btrfs_destroy_cachep(void)
6760 kmem_cache_destroy(btrfs_transaction_cachep); 6838 kmem_cache_destroy(btrfs_transaction_cachep);
6761 if (btrfs_path_cachep) 6839 if (btrfs_path_cachep)
6762 kmem_cache_destroy(btrfs_path_cachep); 6840 kmem_cache_destroy(btrfs_path_cachep);
6841 if (btrfs_free_space_cachep)
6842 kmem_cache_destroy(btrfs_free_space_cachep);
6763} 6843}
6764 6844
6765int btrfs_init_cachep(void) 6845int btrfs_init_cachep(void)
@@ -6788,6 +6868,12 @@ int btrfs_init_cachep(void)
6788 if (!btrfs_path_cachep) 6868 if (!btrfs_path_cachep)
6789 goto fail; 6869 goto fail;
6790 6870
6871 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6872 sizeof(struct btrfs_free_space), 0,
6873 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6874 if (!btrfs_free_space_cachep)
6875 goto fail;
6876
6791 return 0; 6877 return 0;
6792fail: 6878fail:
6793 btrfs_destroy_cachep(); 6879 btrfs_destroy_cachep();
@@ -6806,6 +6892,26 @@ static int btrfs_getattr(struct vfsmount *mnt,
6806 return 0; 6892 return 0;
6807} 6893}
6808 6894
6895/*
6896 * If a file is moved, it will inherit the cow and compression flags of the new
6897 * directory.
6898 */
6899static void fixup_inode_flags(struct inode *dir, struct inode *inode)
6900{
6901 struct btrfs_inode *b_dir = BTRFS_I(dir);
6902 struct btrfs_inode *b_inode = BTRFS_I(inode);
6903
6904 if (b_dir->flags & BTRFS_INODE_NODATACOW)
6905 b_inode->flags |= BTRFS_INODE_NODATACOW;
6906 else
6907 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
6908
6909 if (b_dir->flags & BTRFS_INODE_COMPRESS)
6910 b_inode->flags |= BTRFS_INODE_COMPRESS;
6911 else
6912 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
6913}
6914
6809static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry, 6915static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6810 struct inode *new_dir, struct dentry *new_dentry) 6916 struct inode *new_dir, struct dentry *new_dentry)
6811{ 6917{
@@ -6908,11 +7014,12 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6908 old_dentry->d_name.name, 7014 old_dentry->d_name.name,
6909 old_dentry->d_name.len); 7015 old_dentry->d_name.len);
6910 } else { 7016 } else {
6911 btrfs_inc_nlink(old_dentry->d_inode); 7017 ret = __btrfs_unlink_inode(trans, root, old_dir,
6912 ret = btrfs_unlink_inode(trans, root, old_dir, 7018 old_dentry->d_inode,
6913 old_dentry->d_inode, 7019 old_dentry->d_name.name,
6914 old_dentry->d_name.name, 7020 old_dentry->d_name.len);
6915 old_dentry->d_name.len); 7021 if (!ret)
7022 ret = btrfs_update_inode(trans, root, old_inode);
6916 } 7023 }
6917 BUG_ON(ret); 7024 BUG_ON(ret);
6918 7025
@@ -6939,6 +7046,8 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6939 } 7046 }
6940 } 7047 }
6941 7048
7049 fixup_inode_flags(new_dir, old_inode);
7050
6942 ret = btrfs_add_link(trans, new_dir, old_inode, 7051 ret = btrfs_add_link(trans, new_dir, old_inode,
6943 new_dentry->d_name.name, 7052 new_dentry->d_name.name,
6944 new_dentry->d_name.len, 0, index); 7053 new_dentry->d_name.len, 0, index);
@@ -7355,7 +7464,6 @@ static const struct address_space_operations btrfs_symlink_aops = {
7355}; 7464};
7356 7465
7357static const struct inode_operations btrfs_file_inode_operations = { 7466static const struct inode_operations btrfs_file_inode_operations = {
7358 .truncate = btrfs_truncate,
7359 .getattr = btrfs_getattr, 7467 .getattr = btrfs_getattr,
7360 .setattr = btrfs_setattr, 7468 .setattr = btrfs_setattr,
7361 .setxattr = btrfs_setxattr, 7469 .setxattr = btrfs_setxattr,