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.c500
1 files changed, 352 insertions, 148 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index f1a77449d032..d3d44486290b 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);
@@ -1577,7 +1597,7 @@ int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
1577 unsigned long bio_flags) 1597 unsigned long bio_flags)
1578{ 1598{
1579 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 1599 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1580 u64 logical = (u64)bio->bi_sector << 9; 1600 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1581 u64 length = 0; 1601 u64 length = 0;
1582 u64 map_length; 1602 u64 map_length;
1583 int ret; 1603 int ret;
@@ -1585,7 +1605,7 @@ int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
1585 if (bio_flags & EXTENT_BIO_COMPRESSED) 1605 if (bio_flags & EXTENT_BIO_COMPRESSED)
1586 return 0; 1606 return 0;
1587 1607
1588 length = bio->bi_size; 1608 length = bio->bi_iter.bi_size;
1589 map_length = length; 1609 map_length = length;
1590 ret = btrfs_map_block(root->fs_info, rw, logical, 1610 ret = btrfs_map_block(root->fs_info, rw, logical,
1591 &map_length, NULL, 0); 1611 &map_length, NULL, 0);
@@ -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.
@@ -2610,7 +2629,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2610 EXTENT_DEFRAG, 1, cached_state); 2629 EXTENT_DEFRAG, 1, cached_state);
2611 if (ret) { 2630 if (ret) {
2612 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); 2631 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2613 if (last_snapshot >= BTRFS_I(inode)->generation) 2632 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2614 /* the inode is shared */ 2633 /* the inode is shared */
2615 new = record_old_file_extents(inode, ordered_extent); 2634 new = record_old_file_extents(inode, ordered_extent);
2616 2635
@@ -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);
@@ -4354,8 +4454,12 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4354 * these flags set. For all other operations the VFS set these flags 4454 * these flags set. For all other operations the VFS set these flags
4355 * explicitly if it wants a timestamp update. 4455 * explicitly if it wants a timestamp update.
4356 */ 4456 */
4357 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) 4457 if (newsize != oldsize) {
4358 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); 4458 inode_inc_iversion(inode);
4459 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4460 inode->i_ctime = inode->i_mtime =
4461 current_fs_time(inode->i_sb);
4462 }
4359 4463
4360 if (newsize > oldsize) { 4464 if (newsize > oldsize) {
4361 truncate_pagecache(inode, newsize); 4465 truncate_pagecache(inode, newsize);
@@ -4464,12 +4568,70 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4464 err = btrfs_dirty_inode(inode); 4568 err = btrfs_dirty_inode(inode);
4465 4569
4466 if (!err && attr->ia_valid & ATTR_MODE) 4570 if (!err && attr->ia_valid & ATTR_MODE)
4467 err = btrfs_acl_chmod(inode); 4571 err = posix_acl_chmod(inode, inode->i_mode);
4468 } 4572 }
4469 4573
4470 return err; 4574 return err;
4471} 4575}
4472 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
4473void btrfs_evict_inode(struct inode *inode) 4635void btrfs_evict_inode(struct inode *inode)
4474{ 4636{
4475 struct btrfs_trans_handle *trans; 4637 struct btrfs_trans_handle *trans;
@@ -4480,7 +4642,8 @@ void btrfs_evict_inode(struct inode *inode)
4480 4642
4481 trace_btrfs_inode_evict(inode); 4643 trace_btrfs_inode_evict(inode);
4482 4644
4483 truncate_inode_pages(&inode->i_data, 0); 4645 evict_inode_truncate_pages(inode);
4646
4484 if (inode->i_nlink && 4647 if (inode->i_nlink &&
4485 ((btrfs_root_refs(&root->root_item) != 0 && 4648 ((btrfs_root_refs(&root->root_item) != 0 &&
4486 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) || 4649 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
@@ -4655,9 +4818,9 @@ static int fixup_tree_root_location(struct btrfs_root *root,
4655 } 4818 }
4656 4819
4657 err = -ENOENT; 4820 err = -ENOENT;
4658 ret = btrfs_find_root_ref(root->fs_info->tree_root, path, 4821 ret = btrfs_find_item(root->fs_info->tree_root, path,
4659 BTRFS_I(dir)->root->root_key.objectid, 4822 BTRFS_I(dir)->root->root_key.objectid,
4660 location->objectid); 4823 location->objectid, BTRFS_ROOT_REF_KEY, NULL);
4661 if (ret) { 4824 if (ret) {
4662 if (ret < 0) 4825 if (ret < 0)
4663 err = ret; 4826 err = ret;
@@ -4818,7 +4981,9 @@ again:
4818static int btrfs_init_locked_inode(struct inode *inode, void *p) 4981static int btrfs_init_locked_inode(struct inode *inode, void *p)
4819{ 4982{
4820 struct btrfs_iget_args *args = p; 4983 struct btrfs_iget_args *args = p;
4821 inode->i_ino = args->ino; 4984 inode->i_ino = args->location->objectid;
4985 memcpy(&BTRFS_I(inode)->location, args->location,
4986 sizeof(*args->location));
4822 BTRFS_I(inode)->root = args->root; 4987 BTRFS_I(inode)->root = args->root;
4823 return 0; 4988 return 0;
4824} 4989}
@@ -4826,19 +4991,19 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
4826static int btrfs_find_actor(struct inode *inode, void *opaque) 4991static int btrfs_find_actor(struct inode *inode, void *opaque)
4827{ 4992{
4828 struct btrfs_iget_args *args = opaque; 4993 struct btrfs_iget_args *args = opaque;
4829 return args->ino == btrfs_ino(inode) && 4994 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
4830 args->root == BTRFS_I(inode)->root; 4995 args->root == BTRFS_I(inode)->root;
4831} 4996}
4832 4997
4833static struct inode *btrfs_iget_locked(struct super_block *s, 4998static struct inode *btrfs_iget_locked(struct super_block *s,
4834 u64 objectid, 4999 struct btrfs_key *location,
4835 struct btrfs_root *root) 5000 struct btrfs_root *root)
4836{ 5001{
4837 struct inode *inode; 5002 struct inode *inode;
4838 struct btrfs_iget_args args; 5003 struct btrfs_iget_args args;
4839 unsigned long hashval = btrfs_inode_hash(objectid, root); 5004 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
4840 5005
4841 args.ino = objectid; 5006 args.location = location;
4842 args.root = root; 5007 args.root = root;
4843 5008
4844 inode = iget5_locked(s, hashval, btrfs_find_actor, 5009 inode = iget5_locked(s, hashval, btrfs_find_actor,
@@ -4855,13 +5020,11 @@ struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4855{ 5020{
4856 struct inode *inode; 5021 struct inode *inode;
4857 5022
4858 inode = btrfs_iget_locked(s, location->objectid, root); 5023 inode = btrfs_iget_locked(s, location, root);
4859 if (!inode) 5024 if (!inode)
4860 return ERR_PTR(-ENOMEM); 5025 return ERR_PTR(-ENOMEM);
4861 5026
4862 if (inode->i_state & I_NEW) { 5027 if (inode->i_state & I_NEW) {
4863 BTRFS_I(inode)->root = root;
4864 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4865 btrfs_read_locked_inode(inode); 5028 btrfs_read_locked_inode(inode);
4866 if (!is_bad_inode(inode)) { 5029 if (!is_bad_inode(inode)) {
4867 inode_tree_add(inode); 5030 inode_tree_add(inode);
@@ -4917,7 +5080,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4917 return ERR_PTR(ret); 5080 return ERR_PTR(ret);
4918 5081
4919 if (location.objectid == 0) 5082 if (location.objectid == 0)
4920 return NULL; 5083 return ERR_PTR(-ENOENT);
4921 5084
4922 if (location.type == BTRFS_INODE_ITEM_KEY) { 5085 if (location.type == BTRFS_INODE_ITEM_KEY) {
4923 inode = btrfs_iget(dir->i_sb, &location, root, NULL); 5086 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
@@ -4981,10 +5144,17 @@ static void btrfs_dentry_release(struct dentry *dentry)
4981static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, 5144static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4982 unsigned int flags) 5145 unsigned int flags)
4983{ 5146{
4984 struct dentry *ret; 5147 struct inode *inode;
4985 5148
4986 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry); 5149 inode = btrfs_lookup_dentry(dir, dentry);
4987 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_materialise_unique(dentry, inode);
4988} 5158}
4989 5159
4990unsigned char btrfs_filetype_table[] = { 5160unsigned char btrfs_filetype_table[] = {
@@ -5354,7 +5524,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5354 u32 sizes[2]; 5524 u32 sizes[2];
5355 unsigned long ptr; 5525 unsigned long ptr;
5356 int ret; 5526 int ret;
5357 int owner;
5358 5527
5359 path = btrfs_alloc_path(); 5528 path = btrfs_alloc_path();
5360 if (!path) 5529 if (!path)
@@ -5388,6 +5557,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5388 * number 5557 * number
5389 */ 5558 */
5390 BTRFS_I(inode)->index_cnt = 2; 5559 BTRFS_I(inode)->index_cnt = 2;
5560 BTRFS_I(inode)->dir_index = *index;
5391 BTRFS_I(inode)->root = root; 5561 BTRFS_I(inode)->root = root;
5392 BTRFS_I(inode)->generation = trans->transid; 5562 BTRFS_I(inode)->generation = trans->transid;
5393 inode->i_generation = BTRFS_I(inode)->generation; 5563 inode->i_generation = BTRFS_I(inode)->generation;
@@ -5400,11 +5570,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5400 */ 5570 */
5401 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);
5402 5572
5403 if (S_ISDIR(mode))
5404 owner = 0;
5405 else
5406 owner = 1;
5407
5408 key[0].objectid = objectid; 5573 key[0].objectid = objectid;
5409 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); 5574 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5410 key[0].offset = 0; 5575 key[0].offset = 0;
@@ -5469,6 +5634,12 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5469 5634
5470 btrfs_update_root_times(trans, root); 5635 btrfs_update_root_times(trans, root);
5471 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
5472 return inode; 5643 return inode;
5473fail: 5644fail:
5474 if (dir) 5645 if (dir)
@@ -5737,6 +5908,8 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5737 goto fail; 5908 goto fail;
5738 } 5909 }
5739 5910
5911 /* There are several dir indexes for this inode, clear the cache. */
5912 BTRFS_I(inode)->dir_index = 0ULL;
5740 inc_nlink(inode); 5913 inc_nlink(inode);
5741 inode_inc_iversion(inode); 5914 inode_inc_iversion(inode);
5742 inode->i_ctime = CURRENT_TIME; 5915 inode->i_ctime = CURRENT_TIME;
@@ -6000,7 +6173,7 @@ again:
6000 btrfs_file_extent_num_bytes(leaf, item); 6173 btrfs_file_extent_num_bytes(leaf, item);
6001 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { 6174 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6002 size_t size; 6175 size_t size;
6003 size = btrfs_file_extent_inline_len(leaf, item); 6176 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6004 extent_end = ALIGN(extent_start + size, root->sectorsize); 6177 extent_end = ALIGN(extent_start + size, root->sectorsize);
6005 } 6178 }
6006next: 6179next:
@@ -6069,7 +6242,7 @@ next:
6069 goto out; 6242 goto out;
6070 } 6243 }
6071 6244
6072 size = btrfs_file_extent_inline_len(leaf, item); 6245 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6073 extent_offset = page_offset(page) + pg_offset - extent_start; 6246 extent_offset = page_offset(page) + pg_offset - extent_start;
6074 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, 6247 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
6075 size - extent_offset); 6248 size - extent_offset);
@@ -6386,6 +6559,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6386 int slot; 6559 int slot;
6387 int found_type; 6560 int found_type;
6388 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW); 6561 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
6562
6389 path = btrfs_alloc_path(); 6563 path = btrfs_alloc_path();
6390 if (!path) 6564 if (!path)
6391 return -ENOMEM; 6565 return -ENOMEM;
@@ -6429,6 +6603,10 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6429 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG) 6603 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6430 goto out; 6604 goto out;
6431 6605
6606 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6607 if (extent_end <= offset)
6608 goto out;
6609
6432 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); 6610 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6433 if (disk_bytenr == 0) 6611 if (disk_bytenr == 0)
6434 goto out; 6612 goto out;
@@ -6446,8 +6624,6 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
6446 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 6624 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6447 } 6625 }
6448 6626
6449 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6450
6451 if (btrfs_extent_readonly(root, disk_bytenr)) 6627 if (btrfs_extent_readonly(root, disk_bytenr))
6452 goto out; 6628 goto out;
6453 btrfs_release_path(path); 6629 btrfs_release_path(path);
@@ -6779,17 +6955,16 @@ unlock_err:
6779static void btrfs_endio_direct_read(struct bio *bio, int err) 6955static void btrfs_endio_direct_read(struct bio *bio, int err)
6780{ 6956{
6781 struct btrfs_dio_private *dip = bio->bi_private; 6957 struct btrfs_dio_private *dip = bio->bi_private;
6782 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1; 6958 struct bio_vec *bvec;
6783 struct bio_vec *bvec = bio->bi_io_vec;
6784 struct inode *inode = dip->inode; 6959 struct inode *inode = dip->inode;
6785 struct btrfs_root *root = BTRFS_I(inode)->root; 6960 struct btrfs_root *root = BTRFS_I(inode)->root;
6786 struct bio *dio_bio; 6961 struct bio *dio_bio;
6787 u32 *csums = (u32 *)dip->csum; 6962 u32 *csums = (u32 *)dip->csum;
6788 int index = 0;
6789 u64 start; 6963 u64 start;
6964 int i;
6790 6965
6791 start = dip->logical_offset; 6966 start = dip->logical_offset;
6792 do { 6967 bio_for_each_segment_all(bvec, bio, i) {
6793 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 6968 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6794 struct page *page = bvec->bv_page; 6969 struct page *page = bvec->bv_page;
6795 char *kaddr; 6970 char *kaddr;
@@ -6805,18 +6980,16 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
6805 local_irq_restore(flags); 6980 local_irq_restore(flags);
6806 6981
6807 flush_dcache_page(bvec->bv_page); 6982 flush_dcache_page(bvec->bv_page);
6808 if (csum != csums[index]) { 6983 if (csum != csums[i]) {
6809 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u", 6984 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
6810 btrfs_ino(inode), start, csum, 6985 btrfs_ino(inode), start, csum,
6811 csums[index]); 6986 csums[i]);
6812 err = -EIO; 6987 err = -EIO;
6813 } 6988 }
6814 } 6989 }
6815 6990
6816 start += bvec->bv_len; 6991 start += bvec->bv_len;
6817 bvec++; 6992 }
6818 index++;
6819 } while (bvec <= bvec_end);
6820 6993
6821 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset, 6994 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
6822 dip->logical_offset + dip->bytes - 1); 6995 dip->logical_offset + dip->bytes - 1);
@@ -6894,10 +7067,11 @@ static void btrfs_end_dio_bio(struct bio *bio, int err)
6894 struct btrfs_dio_private *dip = bio->bi_private; 7067 struct btrfs_dio_private *dip = bio->bi_private;
6895 7068
6896 if (err) { 7069 if (err) {
6897 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu " 7070 btrfs_err(BTRFS_I(dip->inode)->root->fs_info,
6898 "sector %#Lx len %u err no %d\n", 7071 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
6899 btrfs_ino(dip->inode), bio->bi_rw, 7072 btrfs_ino(dip->inode), bio->bi_rw,
6900 (unsigned long long)bio->bi_sector, bio->bi_size, err); 7073 (unsigned long long)bio->bi_iter.bi_sector,
7074 bio->bi_iter.bi_size, err);
6901 dip->errors = 1; 7075 dip->errors = 1;
6902 7076
6903 /* 7077 /*
@@ -6988,7 +7162,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6988 struct bio *bio; 7162 struct bio *bio;
6989 struct bio *orig_bio = dip->orig_bio; 7163 struct bio *orig_bio = dip->orig_bio;
6990 struct bio_vec *bvec = orig_bio->bi_io_vec; 7164 struct bio_vec *bvec = orig_bio->bi_io_vec;
6991 u64 start_sector = orig_bio->bi_sector; 7165 u64 start_sector = orig_bio->bi_iter.bi_sector;
6992 u64 file_offset = dip->logical_offset; 7166 u64 file_offset = dip->logical_offset;
6993 u64 submit_len = 0; 7167 u64 submit_len = 0;
6994 u64 map_length; 7168 u64 map_length;
@@ -6996,7 +7170,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6996 int ret = 0; 7170 int ret = 0;
6997 int async_submit = 0; 7171 int async_submit = 0;
6998 7172
6999 map_length = orig_bio->bi_size; 7173 map_length = orig_bio->bi_iter.bi_size;
7000 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9, 7174 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
7001 &map_length, NULL, 0); 7175 &map_length, NULL, 0);
7002 if (ret) { 7176 if (ret) {
@@ -7004,7 +7178,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7004 return -EIO; 7178 return -EIO;
7005 } 7179 }
7006 7180
7007 if (map_length >= orig_bio->bi_size) { 7181 if (map_length >= orig_bio->bi_iter.bi_size) {
7008 bio = orig_bio; 7182 bio = orig_bio;
7009 goto submit; 7183 goto submit;
7010 } 7184 }
@@ -7056,7 +7230,7 @@ static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7056 bio->bi_private = dip; 7230 bio->bi_private = dip;
7057 bio->bi_end_io = btrfs_end_dio_bio; 7231 bio->bi_end_io = btrfs_end_dio_bio;
7058 7232
7059 map_length = orig_bio->bi_size; 7233 map_length = orig_bio->bi_iter.bi_size;
7060 ret = btrfs_map_block(root->fs_info, rw, 7234 ret = btrfs_map_block(root->fs_info, rw,
7061 start_sector << 9, 7235 start_sector << 9,
7062 &map_length, NULL, 0); 7236 &map_length, NULL, 0);
@@ -7114,7 +7288,8 @@ static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7114 7288
7115 if (!skip_sum && !write) { 7289 if (!skip_sum && !write) {
7116 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 7290 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7117 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits; 7291 sum_len = dio_bio->bi_iter.bi_size >>
7292 inode->i_sb->s_blocksize_bits;
7118 sum_len *= csum_size; 7293 sum_len *= csum_size;
7119 } else { 7294 } else {
7120 sum_len = 0; 7295 sum_len = 0;
@@ -7129,8 +7304,8 @@ static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7129 dip->private = dio_bio->bi_private; 7304 dip->private = dio_bio->bi_private;
7130 dip->inode = inode; 7305 dip->inode = inode;
7131 dip->logical_offset = file_offset; 7306 dip->logical_offset = file_offset;
7132 dip->bytes = dio_bio->bi_size; 7307 dip->bytes = dio_bio->bi_iter.bi_size;
7133 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9; 7308 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
7134 io_bio->bi_private = dip; 7309 io_bio->bi_private = dip;
7135 dip->errors = 0; 7310 dip->errors = 0;
7136 dip->orig_bio = io_bio; 7311 dip->orig_bio = io_bio;
@@ -7367,6 +7542,7 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7367 struct extent_state *cached_state = NULL; 7542 struct extent_state *cached_state = NULL;
7368 u64 page_start = page_offset(page); 7543 u64 page_start = page_offset(page);
7369 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;
7370 7546
7371 /* 7547 /*
7372 * we have the page locked, so new writeback can't start, 7548 * we have the page locked, so new writeback can't start,
@@ -7382,17 +7558,21 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7382 btrfs_releasepage(page, GFP_NOFS); 7558 btrfs_releasepage(page, GFP_NOFS);
7383 return; 7559 return;
7384 } 7560 }
7385 lock_extent_bits(tree, page_start, page_end, 0, &cached_state); 7561
7386 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);
7387 if (ordered) { 7565 if (ordered) {
7388 /* 7566 /*
7389 * IO on this page will never be started, so we need 7567 * IO on this page will never be started, so we need
7390 * to account for any ordered extents now 7568 * to account for any ordered extents now
7391 */ 7569 */
7392 clear_extent_bit(tree, page_start, page_end, 7570 if (!inode_evicting)
7393 EXTENT_DIRTY | EXTENT_DELALLOC | 7571 clear_extent_bit(tree, page_start, page_end,
7394 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | 7572 EXTENT_DIRTY | EXTENT_DELALLOC |
7395 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS); 7573 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7574 EXTENT_DEFRAG, 1, 0, &cached_state,
7575 GFP_NOFS);
7396 /* 7576 /*
7397 * whoever cleared the private bit is responsible 7577 * whoever cleared the private bit is responsible
7398 * for the finish_ordered_io 7578 * for the finish_ordered_io
@@ -7416,14 +7596,22 @@ static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7416 btrfs_finish_ordered_io(ordered); 7596 btrfs_finish_ordered_io(ordered);
7417 } 7597 }
7418 btrfs_put_ordered_extent(ordered); 7598 btrfs_put_ordered_extent(ordered);
7419 cached_state = NULL; 7599 if (!inode_evicting) {
7420 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);
7421 } 7614 }
7422 clear_extent_bit(tree, page_start, page_end,
7423 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
7424 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7425 &cached_state, GFP_NOFS);
7426 __btrfs_releasepage(page, GFP_NOFS);
7427 7615
7428 ClearPageChecked(page); 7616 ClearPageChecked(page);
7429 if (PagePrivate(page)) { 7617 if (PagePrivate(page)) {
@@ -7733,7 +7921,9 @@ out:
7733 * create a new subvolume directory/inode (helper for the ioctl). 7921 * create a new subvolume directory/inode (helper for the ioctl).
7734 */ 7922 */
7735int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 7923int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7736 struct btrfs_root *new_root, u64 new_dirid) 7924 struct btrfs_root *new_root,
7925 struct btrfs_root *parent_root,
7926 u64 new_dirid)
7737{ 7927{
7738 struct inode *inode; 7928 struct inode *inode;
7739 int err; 7929 int err;
@@ -7751,6 +7941,12 @@ int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7751 set_nlink(inode, 1); 7941 set_nlink(inode, 1);
7752 btrfs_i_size_write(inode, 0); 7942 btrfs_i_size_write(inode, 0);
7753 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
7754 err = btrfs_update_inode(trans, new_root, inode); 7950 err = btrfs_update_inode(trans, new_root, inode);
7755 7951
7756 iput(inode); 7952 iput(inode);
@@ -7776,6 +7972,7 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
7776 ei->flags = 0; 7972 ei->flags = 0;
7777 ei->csum_bytes = 0; 7973 ei->csum_bytes = 0;
7778 ei->index_cnt = (u64)-1; 7974 ei->index_cnt = (u64)-1;
7975 ei->dir_index = 0;
7779 ei->last_unlink_trans = 0; 7976 ei->last_unlink_trans = 0;
7780 ei->last_log_commit = 0; 7977 ei->last_log_commit = 0;
7781 7978
@@ -8063,6 +8260,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8063 if (ret) 8260 if (ret)
8064 goto out_fail; 8261 goto out_fail;
8065 8262
8263 BTRFS_I(old_inode)->dir_index = 0ULL;
8066 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { 8264 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8067 /* force full log commit if subvolume involved. */ 8265 /* force full log commit if subvolume involved. */
8068 root->fs_info->last_trans_log_full_commit = trans->transid; 8266 root->fs_info->last_trans_log_full_commit = trans->transid;
@@ -8151,6 +8349,9 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
8151 goto out_fail; 8349 goto out_fail;
8152 } 8350 }
8153 8351
8352 if (old_inode->i_nlink == 1)
8353 BTRFS_I(old_inode)->dir_index = index;
8354
8154 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) { 8355 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
8155 struct dentry *parent = new_dentry->d_parent; 8356 struct dentry *parent = new_dentry->d_parent;
8156 btrfs_log_new_name(trans, old_inode, old_dir, parent); 8357 btrfs_log_new_name(trans, old_inode, old_dir, parent);
@@ -8286,7 +8487,7 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8286{ 8487{
8287 int ret; 8488 int ret;
8288 8489
8289 if (root->fs_info->sb->s_flags & MS_RDONLY) 8490 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
8290 return -EROFS; 8491 return -EROFS;
8291 8492
8292 ret = __start_delalloc_inodes(root, delay_iput); 8493 ret = __start_delalloc_inodes(root, delay_iput);
@@ -8312,7 +8513,7 @@ int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput)
8312 struct list_head splice; 8513 struct list_head splice;
8313 int ret; 8514 int ret;
8314 8515
8315 if (fs_info->sb->s_flags & MS_RDONLY) 8516 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
8316 return -EROFS; 8517 return -EROFS;
8317 8518
8318 INIT_LIST_HEAD(&splice); 8519 INIT_LIST_HEAD(&splice);
@@ -8649,12 +8850,14 @@ static const struct inode_operations btrfs_dir_inode_operations = {
8649 .removexattr = btrfs_removexattr, 8850 .removexattr = btrfs_removexattr,
8650 .permission = btrfs_permission, 8851 .permission = btrfs_permission,
8651 .get_acl = btrfs_get_acl, 8852 .get_acl = btrfs_get_acl,
8853 .set_acl = btrfs_set_acl,
8652 .update_time = btrfs_update_time, 8854 .update_time = btrfs_update_time,
8653}; 8855};
8654static const struct inode_operations btrfs_dir_ro_inode_operations = { 8856static const struct inode_operations btrfs_dir_ro_inode_operations = {
8655 .lookup = btrfs_lookup, 8857 .lookup = btrfs_lookup,
8656 .permission = btrfs_permission, 8858 .permission = btrfs_permission,
8657 .get_acl = btrfs_get_acl, 8859 .get_acl = btrfs_get_acl,
8860 .set_acl = btrfs_set_acl,
8658 .update_time = btrfs_update_time, 8861 .update_time = btrfs_update_time,
8659}; 8862};
8660 8863
@@ -8724,6 +8927,7 @@ static const struct inode_operations btrfs_file_inode_operations = {
8724 .permission = btrfs_permission, 8927 .permission = btrfs_permission,
8725 .fiemap = btrfs_fiemap, 8928 .fiemap = btrfs_fiemap,
8726 .get_acl = btrfs_get_acl, 8929 .get_acl = btrfs_get_acl,
8930 .set_acl = btrfs_set_acl,
8727 .update_time = btrfs_update_time, 8931 .update_time = btrfs_update_time,
8728}; 8932};
8729static const struct inode_operations btrfs_special_inode_operations = { 8933static const struct inode_operations btrfs_special_inode_operations = {
@@ -8735,6 +8939,7 @@ static const struct inode_operations btrfs_special_inode_operations = {
8735 .listxattr = btrfs_listxattr, 8939 .listxattr = btrfs_listxattr,
8736 .removexattr = btrfs_removexattr, 8940 .removexattr = btrfs_removexattr,
8737 .get_acl = btrfs_get_acl, 8941 .get_acl = btrfs_get_acl,
8942 .set_acl = btrfs_set_acl,
8738 .update_time = btrfs_update_time, 8943 .update_time = btrfs_update_time,
8739}; 8944};
8740static const struct inode_operations btrfs_symlink_inode_operations = { 8945static const struct inode_operations btrfs_symlink_inode_operations = {
@@ -8748,7 +8953,6 @@ static const struct inode_operations btrfs_symlink_inode_operations = {
8748 .getxattr = btrfs_getxattr, 8953 .getxattr = btrfs_getxattr,
8749 .listxattr = btrfs_listxattr, 8954 .listxattr = btrfs_listxattr,
8750 .removexattr = btrfs_removexattr, 8955 .removexattr = btrfs_removexattr,
8751 .get_acl = btrfs_get_acl,
8752 .update_time = btrfs_update_time, 8956 .update_time = btrfs_update_time,
8753}; 8957};
8754 8958