aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-01-30 23:08:20 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2014-01-30 23:08:20 -0500
commite7651b819e90da924991d727d3c007200a18670d (patch)
treee7a943b5bb56c384972944fd86767a3f079b8a98 /fs/btrfs/inode.c
parent060e8e3b6f8fc0ba97de2276249fbd80fa25b0a2 (diff)
parentcf93da7bcf450cb4595055d491a0519cb39e68ed (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs
Pull btrfs updates from Chris Mason: "This is a pretty big pull, and most of these changes have been floating in btrfs-next for a long time. Filipe's properties work is a cool building block for inheriting attributes like compression down on a per inode basis. Jeff Mahoney kicked in code to export filesystem info into sysfs. Otherwise, lots of performance improvements, cleanups and bug fixes. Looks like there are still a few other small pending incrementals, but I wanted to get the bulk of this in first" * 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: (149 commits) Btrfs: fix spin_unlock in check_ref_cleanup Btrfs: setup inode location during btrfs_init_inode_locked Btrfs: don't use ram_bytes for uncompressed inline items Btrfs: fix btrfs_search_slot_for_read backwards iteration Btrfs: do not export ulist functions Btrfs: rework ulist with list+rb_tree Btrfs: fix memory leaks on walking backrefs failure Btrfs: fix send file hole detection leading to data corruption Btrfs: add a reschedule point in btrfs_find_all_roots() Btrfs: make send's file extent item search more efficient Btrfs: fix to catch all errors when resolving indirect ref Btrfs: fix protection between walking backrefs and root deletion btrfs: fix warning while merging two adjacent extents Btrfs: fix infinite path build loops in incremental send btrfs: undo sysfs when open_ctree() fails Btrfs: fix snprintf usage by send's gen_unique_name btrfs: fix defrag 32-bit integer overflow btrfs: sysfs: list the NO_HOLES feature btrfs: sysfs: don't show reserved incompat feature btrfs: call permission checks earlier in ioctls and return EPERM ...
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c446
1 files changed, 322 insertions, 124 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index d546d8c3038b..5c4ab9c18940 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -58,9 +58,10 @@
58#include "inode-map.h" 58#include "inode-map.h"
59#include "backref.h" 59#include "backref.h"
60#include "hash.h" 60#include "hash.h"
61#include "props.h"
61 62
62struct btrfs_iget_args { 63struct btrfs_iget_args {
63 u64 ino; 64 struct btrfs_key *location;
64 struct btrfs_root *root; 65 struct btrfs_root *root;
65}; 66};
66 67
@@ -125,13 +126,12 @@ static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
125 * no overlapping inline items exist in the btree 126 * no overlapping inline items exist in the btree
126 */ 127 */
127static noinline int insert_inline_extent(struct btrfs_trans_handle *trans, 128static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
129 struct btrfs_path *path, int extent_inserted,
128 struct btrfs_root *root, struct inode *inode, 130 struct btrfs_root *root, struct inode *inode,
129 u64 start, size_t size, size_t compressed_size, 131 u64 start, size_t size, size_t compressed_size,
130 int compress_type, 132 int compress_type,
131 struct page **compressed_pages) 133 struct page **compressed_pages)
132{ 134{
133 struct btrfs_key key;
134 struct btrfs_path *path;
135 struct extent_buffer *leaf; 135 struct extent_buffer *leaf;
136 struct page *page = NULL; 136 struct page *page = NULL;
137 char *kaddr; 137 char *kaddr;
@@ -140,29 +140,29 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
140 int err = 0; 140 int err = 0;
141 int ret; 141 int ret;
142 size_t cur_size = size; 142 size_t cur_size = size;
143 size_t datasize;
144 unsigned long offset; 143 unsigned long offset;
145 144
146 if (compressed_size && compressed_pages) 145 if (compressed_size && compressed_pages)
147 cur_size = compressed_size; 146 cur_size = compressed_size;
148 147
149 path = btrfs_alloc_path(); 148 inode_add_bytes(inode, size);
150 if (!path)
151 return -ENOMEM;
152 149
153 path->leave_spinning = 1; 150 if (!extent_inserted) {
151 struct btrfs_key key;
152 size_t datasize;
154 153
155 key.objectid = btrfs_ino(inode); 154 key.objectid = btrfs_ino(inode);
156 key.offset = start; 155 key.offset = start;
157 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); 156 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
159 157
160 inode_add_bytes(inode, size); 158 datasize = btrfs_file_extent_calc_inline_size(cur_size);
161 ret = btrfs_insert_empty_item(trans, root, path, &key, 159 path->leave_spinning = 1;
162 datasize); 160 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 if (ret) { 161 datasize);
164 err = ret; 162 if (ret) {
165 goto fail; 163 err = ret;
164 goto fail;
165 }
166 } 166 }
167 leaf = path->nodes[0]; 167 leaf = path->nodes[0];
168 ei = btrfs_item_ptr(leaf, path->slots[0], 168 ei = btrfs_item_ptr(leaf, path->slots[0],
@@ -203,7 +203,7 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
203 page_cache_release(page); 203 page_cache_release(page);
204 } 204 }
205 btrfs_mark_buffer_dirty(leaf); 205 btrfs_mark_buffer_dirty(leaf);
206 btrfs_free_path(path); 206 btrfs_release_path(path);
207 207
208 /* 208 /*
209 * we're an inline extent, so nobody can 209 * we're an inline extent, so nobody can
@@ -219,7 +219,6 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
219 219
220 return ret; 220 return ret;
221fail: 221fail:
222 btrfs_free_path(path);
223 return err; 222 return err;
224} 223}
225 224
@@ -242,6 +241,9 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
242 u64 aligned_end = ALIGN(end, root->sectorsize); 241 u64 aligned_end = ALIGN(end, root->sectorsize);
243 u64 data_len = inline_len; 242 u64 data_len = inline_len;
244 int ret; 243 int ret;
244 struct btrfs_path *path;
245 int extent_inserted = 0;
246 u32 extent_item_size;
245 247
246 if (compressed_size) 248 if (compressed_size)
247 data_len = compressed_size; 249 data_len = compressed_size;
@@ -256,12 +258,27 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
256 return 1; 258 return 1;
257 } 259 }
258 260
261 path = btrfs_alloc_path();
262 if (!path)
263 return -ENOMEM;
264
259 trans = btrfs_join_transaction(root); 265 trans = btrfs_join_transaction(root);
260 if (IS_ERR(trans)) 266 if (IS_ERR(trans)) {
267 btrfs_free_path(path);
261 return PTR_ERR(trans); 268 return PTR_ERR(trans);
269 }
262 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 270 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
263 271
264 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1); 272 if (compressed_size && compressed_pages)
273 extent_item_size = btrfs_file_extent_calc_inline_size(
274 compressed_size);
275 else
276 extent_item_size = btrfs_file_extent_calc_inline_size(
277 inline_len);
278
279 ret = __btrfs_drop_extents(trans, root, inode, path,
280 start, aligned_end, NULL,
281 1, 1, extent_item_size, &extent_inserted);
265 if (ret) { 282 if (ret) {
266 btrfs_abort_transaction(trans, root, ret); 283 btrfs_abort_transaction(trans, root, ret);
267 goto out; 284 goto out;
@@ -269,7 +286,8 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
269 286
270 if (isize > actual_end) 287 if (isize > actual_end)
271 inline_len = min_t(u64, isize, actual_end); 288 inline_len = min_t(u64, isize, actual_end);
272 ret = insert_inline_extent(trans, root, inode, start, 289 ret = insert_inline_extent(trans, path, extent_inserted,
290 root, inode, start,
273 inline_len, compressed_size, 291 inline_len, compressed_size,
274 compress_type, compressed_pages); 292 compress_type, compressed_pages);
275 if (ret && ret != -ENOSPC) { 293 if (ret && ret != -ENOSPC) {
@@ -284,6 +302,7 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
284 btrfs_delalloc_release_metadata(inode, end + 1 - start); 302 btrfs_delalloc_release_metadata(inode, end + 1 - start);
285 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0); 303 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
286out: 304out:
305 btrfs_free_path(path);
287 btrfs_end_transaction(trans, root); 306 btrfs_end_transaction(trans, root);
288 return ret; 307 return ret;
289} 308}
@@ -1262,7 +1281,8 @@ next_slot:
1262 nocow = 1; 1281 nocow = 1;
1263 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 1282 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1264 extent_end = found_key.offset + 1283 extent_end = found_key.offset +
1265 btrfs_file_extent_inline_len(leaf, fi); 1284 btrfs_file_extent_inline_len(leaf,
1285 path->slots[0], fi);
1266 extent_end = ALIGN(extent_end, root->sectorsize); 1286 extent_end = ALIGN(extent_end, root->sectorsize);
1267 } else { 1287 } else {
1268 BUG_ON(1); 1288 BUG_ON(1);
@@ -1841,14 +1861,13 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1841 struct btrfs_path *path; 1861 struct btrfs_path *path;
1842 struct extent_buffer *leaf; 1862 struct extent_buffer *leaf;
1843 struct btrfs_key ins; 1863 struct btrfs_key ins;
1864 int extent_inserted = 0;
1844 int ret; 1865 int ret;
1845 1866
1846 path = btrfs_alloc_path(); 1867 path = btrfs_alloc_path();
1847 if (!path) 1868 if (!path)
1848 return -ENOMEM; 1869 return -ENOMEM;
1849 1870
1850 path->leave_spinning = 1;
1851
1852 /* 1871 /*
1853 * we may be replacing one extent in the tree with another. 1872 * we may be replacing one extent in the tree with another.
1854 * The new extent is pinned in the extent map, and we don't want 1873 * The new extent is pinned in the extent map, and we don't want
@@ -1858,17 +1877,23 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1858 * the caller is expected to unpin it and allow it to be merged 1877 * the caller is expected to unpin it and allow it to be merged
1859 * with the others. 1878 * with the others.
1860 */ 1879 */
1861 ret = btrfs_drop_extents(trans, root, inode, file_pos, 1880 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
1862 file_pos + num_bytes, 0); 1881 file_pos + num_bytes, NULL, 0,
1882 1, sizeof(*fi), &extent_inserted);
1863 if (ret) 1883 if (ret)
1864 goto out; 1884 goto out;
1865 1885
1866 ins.objectid = btrfs_ino(inode); 1886 if (!extent_inserted) {
1867 ins.offset = file_pos; 1887 ins.objectid = btrfs_ino(inode);
1868 ins.type = BTRFS_EXTENT_DATA_KEY; 1888 ins.offset = file_pos;
1869 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi)); 1889 ins.type = BTRFS_EXTENT_DATA_KEY;
1870 if (ret) 1890
1871 goto out; 1891 path->leave_spinning = 1;
1892 ret = btrfs_insert_empty_item(trans, root, path, &ins,
1893 sizeof(*fi));
1894 if (ret)
1895 goto out;
1896 }
1872 leaf = path->nodes[0]; 1897 leaf = path->nodes[0];
1873 fi = btrfs_item_ptr(leaf, path->slots[0], 1898 fi = btrfs_item_ptr(leaf, path->slots[0],
1874 struct btrfs_file_extent_item); 1899 struct btrfs_file_extent_item);
@@ -2290,7 +2315,7 @@ again:
2290 u64 extent_len; 2315 u64 extent_len;
2291 struct btrfs_key found_key; 2316 struct btrfs_key found_key;
2292 2317
2293 ret = btrfs_search_slot(trans, root, &key, path, 1, 1); 2318 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2294 if (ret < 0) 2319 if (ret < 0)
2295 goto out_free_path; 2320 goto out_free_path;
2296 2321
@@ -2543,12 +2568,6 @@ out_kfree:
2543 return NULL; 2568 return NULL;
2544} 2569}
2545 2570
2546/*
2547 * helper function for btrfs_finish_ordered_io, this
2548 * just reads in some of the csum leaves to prime them into ram
2549 * before we start the transaction. It limits the amount of btree
2550 * reads required while inside the transaction.
2551 */
2552/* as ordered data IO finishes, this gets called so we can finish 2571/* as ordered data IO finishes, this gets called so we can finish
2553 * an ordered extent if the range of bytes in the file it covers are 2572 * an ordered extent if the range of bytes in the file it covers are
2554 * fully written. 2573 * fully written.
@@ -3248,7 +3267,8 @@ out:
3248 * slot is the slot the inode is in, objectid is the objectid of the inode 3267 * slot is the slot the inode is in, objectid is the objectid of the inode
3249 */ 3268 */
3250static noinline int acls_after_inode_item(struct extent_buffer *leaf, 3269static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3251 int slot, u64 objectid) 3270 int slot, u64 objectid,
3271 int *first_xattr_slot)
3252{ 3272{
3253 u32 nritems = btrfs_header_nritems(leaf); 3273 u32 nritems = btrfs_header_nritems(leaf);
3254 struct btrfs_key found_key; 3274 struct btrfs_key found_key;
@@ -3264,6 +3284,7 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3264 } 3284 }
3265 3285
3266 slot++; 3286 slot++;
3287 *first_xattr_slot = -1;
3267 while (slot < nritems) { 3288 while (slot < nritems) {
3268 btrfs_item_key_to_cpu(leaf, &found_key, slot); 3289 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3269 3290
@@ -3273,6 +3294,8 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3273 3294
3274 /* we found an xattr, assume we've got an acl */ 3295 /* we found an xattr, assume we've got an acl */
3275 if (found_key.type == BTRFS_XATTR_ITEM_KEY) { 3296 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3297 if (*first_xattr_slot == -1)
3298 *first_xattr_slot = slot;
3276 if (found_key.offset == xattr_access || 3299 if (found_key.offset == xattr_access ||
3277 found_key.offset == xattr_default) 3300 found_key.offset == xattr_default)
3278 return 1; 3301 return 1;
@@ -3301,6 +3324,8 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3301 * something larger than an xattr. We have to assume the inode 3324 * something larger than an xattr. We have to assume the inode
3302 * has acls 3325 * has acls
3303 */ 3326 */
3327 if (*first_xattr_slot == -1)
3328 *first_xattr_slot = slot;
3304 return 1; 3329 return 1;
3305} 3330}
3306 3331
@@ -3315,10 +3340,12 @@ static void btrfs_read_locked_inode(struct inode *inode)
3315 struct btrfs_timespec *tspec; 3340 struct btrfs_timespec *tspec;
3316 struct btrfs_root *root = BTRFS_I(inode)->root; 3341 struct btrfs_root *root = BTRFS_I(inode)->root;
3317 struct btrfs_key location; 3342 struct btrfs_key location;
3343 unsigned long ptr;
3318 int maybe_acls; 3344 int maybe_acls;
3319 u32 rdev; 3345 u32 rdev;
3320 int ret; 3346 int ret;
3321 bool filled = false; 3347 bool filled = false;
3348 int first_xattr_slot;
3322 3349
3323 ret = btrfs_fill_inode(inode, &rdev); 3350 ret = btrfs_fill_inode(inode, &rdev);
3324 if (!ret) 3351 if (!ret)
@@ -3328,7 +3355,6 @@ static void btrfs_read_locked_inode(struct inode *inode)
3328 if (!path) 3355 if (!path)
3329 goto make_bad; 3356 goto make_bad;
3330 3357
3331 path->leave_spinning = 1;
3332 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); 3358 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3333 3359
3334 ret = btrfs_lookup_inode(NULL, root, path, &location, 0); 3360 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
@@ -3338,7 +3364,7 @@ static void btrfs_read_locked_inode(struct inode *inode)
3338 leaf = path->nodes[0]; 3364 leaf = path->nodes[0];
3339 3365
3340 if (filled) 3366 if (filled)
3341 goto cache_acl; 3367 goto cache_index;
3342 3368
3343 inode_item = btrfs_item_ptr(leaf, path->slots[0], 3369 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3344 struct btrfs_inode_item); 3370 struct btrfs_inode_item);
@@ -3381,18 +3407,51 @@ static void btrfs_read_locked_inode(struct inode *inode)
3381 3407
3382 BTRFS_I(inode)->index_cnt = (u64)-1; 3408 BTRFS_I(inode)->index_cnt = (u64)-1;
3383 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 3409 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3410
3411cache_index:
3412 path->slots[0]++;
3413 if (inode->i_nlink != 1 ||
3414 path->slots[0] >= btrfs_header_nritems(leaf))
3415 goto cache_acl;
3416
3417 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3418 if (location.objectid != btrfs_ino(inode))
3419 goto cache_acl;
3420
3421 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3422 if (location.type == BTRFS_INODE_REF_KEY) {
3423 struct btrfs_inode_ref *ref;
3424
3425 ref = (struct btrfs_inode_ref *)ptr;
3426 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3427 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3428 struct btrfs_inode_extref *extref;
3429
3430 extref = (struct btrfs_inode_extref *)ptr;
3431 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3432 extref);
3433 }
3384cache_acl: 3434cache_acl:
3385 /* 3435 /*
3386 * try to precache a NULL acl entry for files that don't have 3436 * try to precache a NULL acl entry for files that don't have
3387 * any xattrs or acls 3437 * any xattrs or acls
3388 */ 3438 */
3389 maybe_acls = acls_after_inode_item(leaf, path->slots[0], 3439 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3390 btrfs_ino(inode)); 3440 btrfs_ino(inode), &first_xattr_slot);
3441 if (first_xattr_slot != -1) {
3442 path->slots[0] = first_xattr_slot;
3443 ret = btrfs_load_inode_props(inode, path);
3444 if (ret)
3445 btrfs_err(root->fs_info,
3446 "error loading props for ino %llu (root %llu): %d\n",
3447 btrfs_ino(inode),
3448 root->root_key.objectid, ret);
3449 }
3450 btrfs_free_path(path);
3451
3391 if (!maybe_acls) 3452 if (!maybe_acls)
3392 cache_no_acl(inode); 3453 cache_no_acl(inode);
3393 3454
3394 btrfs_free_path(path);
3395
3396 switch (inode->i_mode & S_IFMT) { 3455 switch (inode->i_mode & S_IFMT) {
3397 case S_IFREG: 3456 case S_IFREG:
3398 inode->i_mapping->a_ops = &btrfs_aops; 3457 inode->i_mapping->a_ops = &btrfs_aops;
@@ -3496,7 +3555,6 @@ static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3496 goto failed; 3555 goto failed;
3497 } 3556 }
3498 3557
3499 btrfs_unlock_up_safe(path, 1);
3500 leaf = path->nodes[0]; 3558 leaf = path->nodes[0];
3501 inode_item = btrfs_item_ptr(leaf, path->slots[0], 3559 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3502 struct btrfs_inode_item); 3560 struct btrfs_inode_item);
@@ -3593,6 +3651,24 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3593 goto err; 3651 goto err;
3594 btrfs_release_path(path); 3652 btrfs_release_path(path);
3595 3653
3654 /*
3655 * If we don't have dir index, we have to get it by looking up
3656 * the inode ref, since we get the inode ref, remove it directly,
3657 * it is unnecessary to do delayed deletion.
3658 *
3659 * But if we have dir index, needn't search inode ref to get it.
3660 * Since the inode ref is close to the inode item, it is better
3661 * that we delay to delete it, and just do this deletion when
3662 * we update the inode item.
3663 */
3664 if (BTRFS_I(inode)->dir_index) {
3665 ret = btrfs_delayed_delete_inode_ref(inode);
3666 if (!ret) {
3667 index = BTRFS_I(inode)->dir_index;
3668 goto skip_backref;
3669 }
3670 }
3671
3596 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, 3672 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3597 dir_ino, &index); 3673 dir_ino, &index);
3598 if (ret) { 3674 if (ret) {
@@ -3602,7 +3678,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3602 btrfs_abort_transaction(trans, root, ret); 3678 btrfs_abort_transaction(trans, root, ret);
3603 goto err; 3679 goto err;
3604 } 3680 }
3605 3681skip_backref:
3606 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); 3682 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3607 if (ret) { 3683 if (ret) {
3608 btrfs_abort_transaction(trans, root, ret); 3684 btrfs_abort_transaction(trans, root, ret);
@@ -3948,7 +4024,7 @@ search_again:
3948 btrfs_file_extent_num_bytes(leaf, fi); 4024 btrfs_file_extent_num_bytes(leaf, fi);
3949 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 4025 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3950 item_end += btrfs_file_extent_inline_len(leaf, 4026 item_end += btrfs_file_extent_inline_len(leaf,
3951 fi); 4027 path->slots[0], fi);
3952 } 4028 }
3953 item_end--; 4029 item_end--;
3954 } 4030 }
@@ -4018,6 +4094,12 @@ search_again:
4018 inode_sub_bytes(inode, item_end + 1 - 4094 inode_sub_bytes(inode, item_end + 1 -
4019 new_size); 4095 new_size);
4020 } 4096 }
4097
4098 /*
4099 * update the ram bytes to properly reflect
4100 * the new size of our item
4101 */
4102 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4021 size = 4103 size =
4022 btrfs_file_extent_calc_inline_size(size); 4104 btrfs_file_extent_calc_inline_size(size);
4023 btrfs_truncate_item(root, path, size, 1); 4105 btrfs_truncate_item(root, path, size, 1);
@@ -4203,6 +4285,49 @@ out:
4203 return ret; 4285 return ret;
4204} 4286}
4205 4287
4288static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4289 u64 offset, u64 len)
4290{
4291 struct btrfs_trans_handle *trans;
4292 int ret;
4293
4294 /*
4295 * Still need to make sure the inode looks like it's been updated so
4296 * that any holes get logged if we fsync.
4297 */
4298 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4299 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4300 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4301 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4302 return 0;
4303 }
4304
4305 /*
4306 * 1 - for the one we're dropping
4307 * 1 - for the one we're adding
4308 * 1 - for updating the inode.
4309 */
4310 trans = btrfs_start_transaction(root, 3);
4311 if (IS_ERR(trans))
4312 return PTR_ERR(trans);
4313
4314 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4315 if (ret) {
4316 btrfs_abort_transaction(trans, root, ret);
4317 btrfs_end_transaction(trans, root);
4318 return ret;
4319 }
4320
4321 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4322 0, 0, len, 0, len, 0, 0, 0);
4323 if (ret)
4324 btrfs_abort_transaction(trans, root, ret);
4325 else
4326 btrfs_update_inode(trans, root, inode);
4327 btrfs_end_transaction(trans, root);
4328 return ret;
4329}
4330
4206/* 4331/*
4207 * This function puts in dummy file extents for the area we're creating a hole 4332 * This function puts in dummy file extents for the area we're creating a hole
4208 * for. So if we are truncating this file to a larger size we need to insert 4333 * for. So if we are truncating this file to a larger size we need to insert
@@ -4211,7 +4336,6 @@ out:
4211 */ 4336 */
4212int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) 4337int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4213{ 4338{
4214 struct btrfs_trans_handle *trans;
4215 struct btrfs_root *root = BTRFS_I(inode)->root; 4339 struct btrfs_root *root = BTRFS_I(inode)->root;
4216 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 4340 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4217 struct extent_map *em = NULL; 4341 struct extent_map *em = NULL;
@@ -4266,31 +4390,10 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4266 struct extent_map *hole_em; 4390 struct extent_map *hole_em;
4267 hole_size = last_byte - cur_offset; 4391 hole_size = last_byte - cur_offset;
4268 4392
4269 trans = btrfs_start_transaction(root, 3); 4393 err = maybe_insert_hole(root, inode, cur_offset,
4270 if (IS_ERR(trans)) { 4394 hole_size);
4271 err = PTR_ERR(trans); 4395 if (err)
4272 break;
4273 }
4274
4275 err = btrfs_drop_extents(trans, root, inode,
4276 cur_offset,
4277 cur_offset + hole_size, 1);
4278 if (err) {
4279 btrfs_abort_transaction(trans, root, err);
4280 btrfs_end_transaction(trans, root);
4281 break;
4282 }
4283
4284 err = btrfs_insert_file_extent(trans, root,
4285 btrfs_ino(inode), cur_offset, 0,
4286 0, hole_size, 0, hole_size,
4287 0, 0, 0);
4288 if (err) {
4289 btrfs_abort_transaction(trans, root, err);
4290 btrfs_end_transaction(trans, root);
4291 break; 4396 break;
4292 }
4293
4294 btrfs_drop_extent_cache(inode, cur_offset, 4397 btrfs_drop_extent_cache(inode, cur_offset,
4295 cur_offset + hole_size - 1, 0); 4398 cur_offset + hole_size - 1, 0);
4296 hole_em = alloc_extent_map(); 4399 hole_em = alloc_extent_map();
@@ -4309,7 +4412,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4309 hole_em->ram_bytes = hole_size; 4412 hole_em->ram_bytes = hole_size;
4310 hole_em->bdev = root->fs_info->fs_devices->latest_bdev; 4413 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4311 hole_em->compress_type = BTRFS_COMPRESS_NONE; 4414 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4312 hole_em->generation = trans->transid; 4415 hole_em->generation = root->fs_info->generation;
4313 4416
4314 while (1) { 4417 while (1) {
4315 write_lock(&em_tree->lock); 4418 write_lock(&em_tree->lock);
@@ -4322,17 +4425,14 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4322 hole_size - 1, 0); 4425 hole_size - 1, 0);
4323 } 4426 }
4324 free_extent_map(hole_em); 4427 free_extent_map(hole_em);
4325next:
4326 btrfs_update_inode(trans, root, inode);
4327 btrfs_end_transaction(trans, root);
4328 } 4428 }
4429next:
4329 free_extent_map(em); 4430 free_extent_map(em);
4330 em = NULL; 4431 em = NULL;
4331 cur_offset = last_byte; 4432 cur_offset = last_byte;
4332 if (cur_offset >= block_end) 4433 if (cur_offset >= block_end)
4333 break; 4434 break;
4334 } 4435 }
4335
4336 free_extent_map(em); 4436 free_extent_map(em);
4337 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, 4437 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4338 GFP_NOFS); 4438 GFP_NOFS);
@@ -4474,6 +4574,64 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4474 return err; 4574 return err;
4475} 4575}
4476 4576
4577/*
4578 * While truncating the inode pages during eviction, we get the VFS calling
4579 * btrfs_invalidatepage() against each page of the inode. This is slow because
4580 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4581 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4582 * extent_state structures over and over, wasting lots of time.
4583 *
4584 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4585 * those expensive operations on a per page basis and do only the ordered io
4586 * finishing, while we release here the extent_map and extent_state structures,
4587 * without the excessive merging and splitting.
4588 */
4589static void evict_inode_truncate_pages(struct inode *inode)
4590{
4591 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4592 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4593 struct rb_node *node;
4594
4595 ASSERT(inode->i_state & I_FREEING);
4596 truncate_inode_pages(&inode->i_data, 0);
4597
4598 write_lock(&map_tree->lock);
4599 while (!RB_EMPTY_ROOT(&map_tree->map)) {
4600 struct extent_map *em;
4601
4602 node = rb_first(&map_tree->map);
4603 em = rb_entry(node, struct extent_map, rb_node);
4604 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4605 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
4606 remove_extent_mapping(map_tree, em);
4607 free_extent_map(em);
4608 }
4609 write_unlock(&map_tree->lock);
4610
4611 spin_lock(&io_tree->lock);
4612 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4613 struct extent_state *state;
4614 struct extent_state *cached_state = NULL;
4615
4616 node = rb_first(&io_tree->state);
4617 state = rb_entry(node, struct extent_state, rb_node);
4618 atomic_inc(&state->refs);
4619 spin_unlock(&io_tree->lock);
4620
4621 lock_extent_bits(io_tree, state->start, state->end,
4622 0, &cached_state);
4623 clear_extent_bit(io_tree, state->start, state->end,
4624 EXTENT_LOCKED | EXTENT_DIRTY |
4625 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
4626 EXTENT_DEFRAG, 1, 1,
4627 &cached_state, GFP_NOFS);
4628 free_extent_state(state);
4629
4630 spin_lock(&io_tree->lock);
4631 }
4632 spin_unlock(&io_tree->lock);
4633}
4634
4477void btrfs_evict_inode(struct inode *inode) 4635void btrfs_evict_inode(struct inode *inode)
4478{ 4636{
4479 struct btrfs_trans_handle *trans; 4637 struct btrfs_trans_handle *trans;
@@ -4484,7 +4642,8 @@ void btrfs_evict_inode(struct inode *inode)
4484 4642
4485 trace_btrfs_inode_evict(inode); 4643 trace_btrfs_inode_evict(inode);
4486 4644
4487 truncate_inode_pages(&inode->i_data, 0); 4645 evict_inode_truncate_pages(inode);
4646
4488 if (inode->i_nlink && 4647 if (inode->i_nlink &&
4489 ((btrfs_root_refs(&root->root_item) != 0 && 4648 ((btrfs_root_refs(&root->root_item) != 0 &&
4490 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || 4649 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
@@ -4659,9 +4818,9 @@ static int fixup_tree_root_location(struct btrfs_root *root,
4659 } 4818 }
4660 4819
4661 err = -ENOENT; 4820 err = -ENOENT;
4662 ret = btrfs_find_root_ref(root->fs_info->tree_root, path, 4821 ret = btrfs_find_item(root->fs_info->tree_root, path,
4663 BTRFS_I(dir)->root->root_key.objectid, 4822 BTRFS_I(dir)->root->root_key.objectid,
4664 location->objectid); 4823 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
4665 if (ret) { 4824 if (ret) {
4666 if (ret < 0) 4825 if (ret < 0)
4667 err = ret; 4826 err = ret;
@@ -4822,7 +4981,9 @@ again:
4822static int btrfs_init_locked_inode(struct inode *inode, void *p) 4981static int btrfs_init_locked_inode(struct inode *inode, void *p)
4823{ 4982{
4824 struct btrfs_iget_args *args = p; 4983 struct btrfs_iget_args *args = p;
4825 inode->i_ino = args->ino; 4984 inode->i_ino = args->location->objectid;
4985 memcpy(&BTRFS_I(inode)->location, args->location,
4986 sizeof(*args->location));
4826 BTRFS_I(inode)->root = args->root; 4987 BTRFS_I(inode)->root = args->root;
4827 return 0; 4988 return 0;
4828} 4989}
@@ -4830,19 +4991,19 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
4830static int btrfs_find_actor(struct inode *inode, void *opaque) 4991static int btrfs_find_actor(struct inode *inode, void *opaque)
4831{ 4992{
4832 struct btrfs_iget_args *args = opaque; 4993 struct btrfs_iget_args *args = opaque;
4833 return args->ino == btrfs_ino(inode) && 4994 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
4834 args->root == BTRFS_I(inode)->root; 4995 args->root == BTRFS_I(inode)->root;
4835} 4996}
4836 4997
4837static struct inode *btrfs_iget_locked(struct super_block *s, 4998static struct inode *btrfs_iget_locked(struct super_block *s,
4838 u64 objectid, 4999 struct btrfs_key *location,
4839 struct btrfs_root *root) 5000 struct btrfs_root *root)
4840{ 5001{
4841 struct inode *inode; 5002 struct inode *inode;
4842 struct btrfs_iget_args args; 5003 struct btrfs_iget_args args;
4843 unsigned long hashval = btrfs_inode_hash(objectid, root); 5004 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
4844 5005
4845 args.ino = objectid; 5006 args.location = location;
4846 args.root = root; 5007 args.root = root;
4847 5008
4848 inode = iget5_locked(s, hashval, btrfs_find_actor, 5009 inode = iget5_locked(s, hashval, btrfs_find_actor,
@@ -4859,13 +5020,11 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4859{ 5020{
4860 struct inode *inode; 5021 struct inode *inode;
4861 5022
4862 inode = btrfs_iget_locked(s, location->objectid, root); 5023 inode = btrfs_iget_locked(s, location, root);
4863 if (!inode) 5024 if (!inode)
4864 return ERR_PTR(-ENOMEM); 5025 return ERR_PTR(-ENOMEM);
4865 5026
4866 if (inode->i_state & I_NEW) { 5027 if (inode->i_state & I_NEW) {
4867 BTRFS_I(inode)->root = root;
4868 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4869 btrfs_read_locked_inode(inode); 5028 btrfs_read_locked_inode(inode);
4870 if (!is_bad_inode(inode)) { 5029 if (!is_bad_inode(inode)) {
4871 inode_tree_add(inode); 5030 inode_tree_add(inode);
@@ -4921,7 +5080,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4921 return ERR_PTR(ret); 5080 return ERR_PTR(ret);
4922 5081
4923 if (location.objectid == 0) 5082 if (location.objectid == 0)
4924 return NULL; 5083 return ERR_PTR(-ENOENT);
4925 5084
4926 if (location.type == BTRFS_INODE_ITEM_KEY) { 5085 if (location.type == BTRFS_INODE_ITEM_KEY) {
4927 inode = btrfs_iget(dir->i_sb, &location, root, NULL); 5086 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
@@ -4985,10 +5144,17 @@ static void btrfs_dentry_release(struct dentry *dentry)
4985static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, 5144static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4986 unsigned int flags) 5145 unsigned int flags)
4987{ 5146{
4988 struct dentry *ret; 5147 struct inode *inode;
4989 5148
4990 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); 5149 inode = btrfs_lookup_dentry(dir, dentry);
4991 return ret; 5150 if (IS_ERR(inode)) {
5151 if (PTR_ERR(inode) == -ENOENT)
5152 inode = NULL;
5153 else
5154 return ERR_CAST(inode);
5155 }
5156
5157 return d_splice_alias(inode, dentry);
4992} 5158}
4993 5159
4994unsigned char btrfs_filetype_table[] = { 5160unsigned char btrfs_filetype_table[] = {
@@ -5358,7 +5524,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5358 u32 sizes[2]; 5524 u32 sizes[2];
5359 unsigned long ptr; 5525 unsigned long ptr;
5360 int ret; 5526 int ret;
5361 int owner;
5362 5527
5363 path = btrfs_alloc_path(); 5528 path = btrfs_alloc_path();
5364 if (!path) 5529 if (!path)
@@ -5392,6 +5557,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5392 * number 5557 * number
5393 */ 5558 */
5394 BTRFS_I(inode)->index_cnt = 2; 5559 BTRFS_I(inode)->index_cnt = 2;
5560 BTRFS_I(inode)->dir_index = *index;
5395 BTRFS_I(inode)->root = root; 5561 BTRFS_I(inode)->root = root;
5396 BTRFS_I(inode)->generation = trans->transid; 5562 BTRFS_I(inode)->generation = trans->transid;
5397 inode->i_generation = BTRFS_I(inode)->generation; 5563 inode->i_generation = BTRFS_I(inode)->generation;
@@ -5404,11 +5570,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5404 */ 5570 */
5405 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags); 5571 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5406 5572
5407 if (S_ISDIR(mode))
5408 owner = 0;
5409 else
5410 owner = 1;
5411
5412 key[0].objectid = objectid; 5573 key[0].objectid = objectid;
5413 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); 5574 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5414 key[0].offset = 0; 5575 key[0].offset = 0;
@@ -5473,6 +5634,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5473 5634
5474 btrfs_update_root_times(trans, root); 5635 btrfs_update_root_times(trans, root);
5475 5636
5637 ret = btrfs_inode_inherit_props(trans, inode, dir);
5638 if (ret)
5639 btrfs_err(root->fs_info,
5640 "error inheriting props for ino %llu (root %llu): %d",
5641 btrfs_ino(inode), root->root_key.objectid, ret);
5642
5476 return inode; 5643 return inode;
5477fail: 5644fail:
5478 if (dir) 5645 if (dir)
@@ -5741,6 +5908,8 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5741 goto fail; 5908 goto fail;
5742 } 5909 }
5743 5910
5911 /* There are several dir indexes for this inode, clear the cache. */
5912 BTRFS_I(inode)->dir_index = 0ULL;
5744 inc_nlink(inode); 5913 inc_nlink(inode);
5745 inode_inc_iversion(inode); 5914 inode_inc_iversion(inode);
5746 inode->i_ctime = CURRENT_TIME; 5915 inode->i_ctime = CURRENT_TIME;
@@ -6004,7 +6173,7 @@ again:
6004 btrfs_file_extent_num_bytes(leaf, item); 6173 btrfs_file_extent_num_bytes(leaf, item);
6005 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { 6174 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6006 size_t size; 6175 size_t size;
6007 size = btrfs_file_extent_inline_len(leaf, item); 6176 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6008 extent_end = ALIGN(extent_start + size, root->sectorsize); 6177 extent_end = ALIGN(extent_start + size, root->sectorsize);
6009 } 6178 }
6010next: 6179next:
@@ -6073,7 +6242,7 @@ next:
6073 goto out; 6242 goto out;
6074 } 6243 }
6075 6244
6076 size = btrfs_file_extent_inline_len(leaf, item); 6245 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6077 extent_offset = page_offset(page) + pg_offset - extent_start; 6246 extent_offset = page_offset(page) + pg_offset - extent_start;
6078 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, 6247 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
6079 size - extent_offset); 6248 size - extent_offset);
@@ -6390,6 +6559,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6390 int slot; 6559 int slot;
6391 int found_type; 6560 int found_type;
6392 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); 6561 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
6562
6393 path = btrfs_alloc_path(); 6563 path = btrfs_alloc_path();
6394 if (!path) 6564 if (!path)
6395 return -ENOMEM; 6565 return -ENOMEM;
@@ -6433,6 +6603,10 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6433 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) 6603 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6434 goto out; 6604 goto out;
6435 6605
6606 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6607 if (extent_end <= offset)
6608 goto out;
6609
6436 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 6610 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6437 if (disk_bytenr == 0) 6611 if (disk_bytenr == 0)
6438 goto out; 6612 goto out;
@@ -6450,8 +6624,6 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6450 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 6624 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6451 } 6625 }
6452 6626
6453 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6454
6455 if (btrfs_extent_readonly(root, disk_bytenr)) 6627 if (btrfs_extent_readonly(root, disk_bytenr))
6456 goto out; 6628 goto out;
6457 btrfs_release_path(path); 6629 btrfs_release_path(path);
@@ -6895,8 +7067,8 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
6895 struct btrfs_dio_private *dip = bio->bi_private; 7067 struct btrfs_dio_private *dip = bio->bi_private;
6896 7068
6897 if (err) { 7069 if (err) {
6898 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " 7070 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
6899 "sector %#Lx len %u err no %d\n", 7071 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
6900 btrfs_ino(dip->inode), bio->bi_rw, 7072 btrfs_ino(dip->inode), bio->bi_rw,
6901 (unsigned long long)bio->bi_iter.bi_sector, 7073 (unsigned long long)bio->bi_iter.bi_sector,
6902 bio->bi_iter.bi_size, err); 7074 bio->bi_iter.bi_size, err);
@@ -7370,6 +7542,7 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7370 struct extent_state *cached_state = NULL; 7542 struct extent_state *cached_state = NULL;
7371 u64 page_start = page_offset(page); 7543 u64 page_start = page_offset(page);
7372 u64 page_end = page_start + PAGE_CACHE_SIZE - 1; 7544 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
7545 int inode_evicting = inode->i_state & I_FREEING;
7373 7546
7374 /* 7547 /*
7375 * we have the page locked, so new writeback can't start, 7548 * we have the page locked, so new writeback can't start,
@@ -7385,17 +7558,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7385 btrfs_releasepage(page, GFP_NOFS); 7558 btrfs_releasepage(page, GFP_NOFS);
7386 return; 7559 return;
7387 } 7560 }
7388 lock_extent_bits(tree, page_start, page_end, 0, &cached_state); 7561
7389 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page)); 7562 if (!inode_evicting)
7563 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7564 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7390 if (ordered) { 7565 if (ordered) {
7391 /* 7566 /*
7392 * IO on this page will never be started, so we need 7567 * IO on this page will never be started, so we need
7393 * to account for any ordered extents now 7568 * to account for any ordered extents now
7394 */ 7569 */
7395 clear_extent_bit(tree, page_start, page_end, 7570 if (!inode_evicting)
7396 EXTENT_DIRTY | EXTENT_DELALLOC | 7571 clear_extent_bit(tree, page_start, page_end,
7397 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | 7572 EXTENT_DIRTY | EXTENT_DELALLOC |
7398 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); 7573 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7574 EXTENT_DEFRAG, 1, 0, &cached_state,
7575 GFP_NOFS);
7399 /* 7576 /*
7400 * whoever cleared the private bit is responsible 7577 * whoever cleared the private bit is responsible
7401 * for the finish_ordered_io 7578 * for the finish_ordered_io
@@ -7419,14 +7596,22 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7419 btrfs_finish_ordered_io(ordered); 7596 btrfs_finish_ordered_io(ordered);
7420 } 7597 }
7421 btrfs_put_ordered_extent(ordered); 7598 btrfs_put_ordered_extent(ordered);
7422 cached_state = NULL; 7599 if (!inode_evicting) {
7423 lock_extent_bits(tree, page_start, page_end, 0, &cached_state); 7600 cached_state = NULL;
7601 lock_extent_bits(tree, page_start, page_end, 0,
7602 &cached_state);
7603 }
7604 }
7605
7606 if (!inode_evicting) {
7607 clear_extent_bit(tree, page_start, page_end,
7608 EXTENT_LOCKED | EXTENT_DIRTY |
7609 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
7610 EXTENT_DEFRAG, 1, 1,
7611 &cached_state, GFP_NOFS);
7612
7613 __btrfs_releasepage(page, GFP_NOFS);
7424 } 7614 }
7425 clear_extent_bit(tree, page_start, page_end,
7426 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
7427 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7428 &cached_state, GFP_NOFS);
7429 __btrfs_releasepage(page, GFP_NOFS);
7430 7615
7431 ClearPageChecked(page); 7616 ClearPageChecked(page);
7432 if (PagePrivate(page)) { 7617 if (PagePrivate(page)) {
@@ -7736,7 +7921,9 @@ out:
7736 * create a new subvolume directory/inode (helper for the ioctl). 7921 * create a new subvolume directory/inode (helper for the ioctl).
7737 */ 7922 */
7738int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 7923int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7739 struct btrfs_root *new_root, u64 new_dirid) 7924 struct btrfs_root *new_root,
7925 struct btrfs_root *parent_root,
7926 u64 new_dirid)
7740{ 7927{
7741 struct inode *inode; 7928 struct inode *inode;
7742 int err; 7929 int err;
@@ -7754,6 +7941,12 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7754 set_nlink(inode, 1); 7941 set_nlink(inode, 1);
7755 btrfs_i_size_write(inode, 0); 7942 btrfs_i_size_write(inode, 0);
7756 7943
7944 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
7945 if (err)
7946 btrfs_err(new_root->fs_info,
7947 "error inheriting subvolume %llu properties: %d\n",
7948 new_root->root_key.objectid, err);
7949
7757 err = btrfs_update_inode(trans, new_root, inode); 7950 err = btrfs_update_inode(trans, new_root, inode);
7758 7951
7759 iput(inode); 7952 iput(inode);
@@ -7779,6 +7972,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
7779 ei->flags = 0; 7972 ei->flags = 0;
7780 ei->csum_bytes = 0; 7973 ei->csum_bytes = 0;
7781 ei->index_cnt = (u64)-1; 7974 ei->index_cnt = (u64)-1;
7975 ei->dir_index = 0;
7782 ei->last_unlink_trans = 0; 7976 ei->last_unlink_trans = 0;
7783 ei->last_log_commit = 0; 7977 ei->last_log_commit = 0;
7784 7978
@@ -8066,6 +8260,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8066 if (ret) 8260 if (ret)
8067 goto out_fail; 8261 goto out_fail;
8068 8262
8263 BTRFS_I(old_inode)->dir_index = 0ULL;
8069 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { 8264 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8070 /* force full log commit if subvolume involved. */ 8265 /* force full log commit if subvolume involved. */
8071 root->fs_info->last_trans_log_full_commit = trans->transid; 8266 root->fs_info->last_trans_log_full_commit = trans->transid;
@@ -8154,6 +8349,9 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8154 goto out_fail; 8349 goto out_fail;
8155 } 8350 }
8156 8351
8352 if (old_inode->i_nlink == 1)
8353 BTRFS_I(old_inode)->dir_index = index;
8354
8157 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { 8355 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
8158 struct dentry *parent = new_dentry->d_parent; 8356 struct dentry *parent = new_dentry->d_parent;
8159 btrfs_log_new_name(trans, old_inode, old_dir, parent); 8357 btrfs_log_new_name(trans, old_inode, old_dir, parent);
@@ -8289,7 +8487,7 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8289{ 8487{
8290 int ret; 8488 int ret;
8291 8489
8292 if (root->fs_info->sb->s_flags & MS_RDONLY) 8490 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
8293 return -EROFS; 8491 return -EROFS;
8294 8492
8295 ret = __start_delalloc_inodes(root, delay_iput); 8493 ret = __start_delalloc_inodes(root, delay_iput);
@@ -8315,7 +8513,7 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
8315 struct list_head splice; 8513 struct list_head splice;
8316 int ret; 8514 int ret;
8317 8515
8318 if (fs_info->sb->s_flags & MS_RDONLY) 8516 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
8319 return -EROFS; 8517 return -EROFS;
8320 8518
8321 INIT_LIST_HEAD(&splice); 8519 INIT_LIST_HEAD(&splice);