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.c282
1 files changed, 186 insertions, 96 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 95c212037095..17ad018da0a2 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -392,20 +392,23 @@ static noinline int add_async_extent(struct async_cow *cow,
392 return 0; 392 return 0;
393} 393}
394 394
395static inline int inode_need_compress(struct inode *inode) 395static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
396{ 396{
397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
398 398
399 /* force compress */ 399 /* force compress */
400 if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) 400 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
401 return 1; 401 return 1;
402 /* defrag ioctl */
403 if (BTRFS_I(inode)->defrag_compress)
404 return 1;
402 /* bad compression ratios */ 405 /* bad compression ratios */
403 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) 406 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
404 return 0; 407 return 0;
405 if (btrfs_test_opt(fs_info, COMPRESS) || 408 if (btrfs_test_opt(fs_info, COMPRESS) ||
406 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || 409 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
407 BTRFS_I(inode)->force_compress) 410 BTRFS_I(inode)->prop_compress)
408 return 1; 411 return btrfs_compress_heuristic(inode, start, end);
409 return 0; 412 return 0;
410} 413}
411 414
@@ -503,7 +506,7 @@ again:
503 * inode has not been flagged as nocompress. This flag can 506 * inode has not been flagged as nocompress. This flag can
504 * change at any time if we discover bad compression ratios. 507 * change at any time if we discover bad compression ratios.
505 */ 508 */
506 if (inode_need_compress(inode)) { 509 if (inode_need_compress(inode, start, end)) {
507 WARN_ON(pages); 510 WARN_ON(pages);
508 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS); 511 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
509 if (!pages) { 512 if (!pages) {
@@ -511,8 +514,10 @@ again:
511 goto cont; 514 goto cont;
512 } 515 }
513 516
514 if (BTRFS_I(inode)->force_compress) 517 if (BTRFS_I(inode)->defrag_compress)
515 compress_type = BTRFS_I(inode)->force_compress; 518 compress_type = BTRFS_I(inode)->defrag_compress;
519 else if (BTRFS_I(inode)->prop_compress)
520 compress_type = BTRFS_I(inode)->prop_compress;
516 521
517 /* 522 /*
518 * we need to call clear_page_dirty_for_io on each 523 * we need to call clear_page_dirty_for_io on each
@@ -645,7 +650,7 @@ cont:
645 650
646 /* flag the file so we don't compress in the future */ 651 /* flag the file so we don't compress in the future */
647 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) && 652 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
648 !(BTRFS_I(inode)->force_compress)) { 653 !(BTRFS_I(inode)->prop_compress)) {
649 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 654 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
650 } 655 }
651 } 656 }
@@ -1381,7 +1386,7 @@ next_slot:
1381 * we fall into common COW way. 1386 * we fall into common COW way.
1382 */ 1387 */
1383 if (!nolock) { 1388 if (!nolock) {
1384 err = btrfs_start_write_no_snapshoting(root); 1389 err = btrfs_start_write_no_snapshotting(root);
1385 if (!err) 1390 if (!err)
1386 goto out_check; 1391 goto out_check;
1387 } 1392 }
@@ -1393,12 +1398,12 @@ next_slot:
1393 if (csum_exist_in_range(fs_info, disk_bytenr, 1398 if (csum_exist_in_range(fs_info, disk_bytenr,
1394 num_bytes)) { 1399 num_bytes)) {
1395 if (!nolock) 1400 if (!nolock)
1396 btrfs_end_write_no_snapshoting(root); 1401 btrfs_end_write_no_snapshotting(root);
1397 goto out_check; 1402 goto out_check;
1398 } 1403 }
1399 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) { 1404 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1400 if (!nolock) 1405 if (!nolock)
1401 btrfs_end_write_no_snapshoting(root); 1406 btrfs_end_write_no_snapshotting(root);
1402 goto out_check; 1407 goto out_check;
1403 } 1408 }
1404 nocow = 1; 1409 nocow = 1;
@@ -1415,7 +1420,7 @@ out_check:
1415 if (extent_end <= start) { 1420 if (extent_end <= start) {
1416 path->slots[0]++; 1421 path->slots[0]++;
1417 if (!nolock && nocow) 1422 if (!nolock && nocow)
1418 btrfs_end_write_no_snapshoting(root); 1423 btrfs_end_write_no_snapshotting(root);
1419 if (nocow) 1424 if (nocow)
1420 btrfs_dec_nocow_writers(fs_info, disk_bytenr); 1425 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1421 goto next_slot; 1426 goto next_slot;
@@ -1438,7 +1443,7 @@ out_check:
1438 NULL); 1443 NULL);
1439 if (ret) { 1444 if (ret) {
1440 if (!nolock && nocow) 1445 if (!nolock && nocow)
1441 btrfs_end_write_no_snapshoting(root); 1446 btrfs_end_write_no_snapshotting(root);
1442 if (nocow) 1447 if (nocow)
1443 btrfs_dec_nocow_writers(fs_info, 1448 btrfs_dec_nocow_writers(fs_info,
1444 disk_bytenr); 1449 disk_bytenr);
@@ -1459,7 +1464,7 @@ out_check:
1459 BTRFS_ORDERED_PREALLOC); 1464 BTRFS_ORDERED_PREALLOC);
1460 if (IS_ERR(em)) { 1465 if (IS_ERR(em)) {
1461 if (!nolock && nocow) 1466 if (!nolock && nocow)
1462 btrfs_end_write_no_snapshoting(root); 1467 btrfs_end_write_no_snapshotting(root);
1463 if (nocow) 1468 if (nocow)
1464 btrfs_dec_nocow_writers(fs_info, 1469 btrfs_dec_nocow_writers(fs_info,
1465 disk_bytenr); 1470 disk_bytenr);
@@ -1499,7 +1504,7 @@ out_check:
1499 PAGE_UNLOCK | PAGE_SET_PRIVATE2); 1504 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1500 1505
1501 if (!nolock && nocow) 1506 if (!nolock && nocow)
1502 btrfs_end_write_no_snapshoting(root); 1507 btrfs_end_write_no_snapshotting(root);
1503 cur_offset = extent_end; 1508 cur_offset = extent_end;
1504 1509
1505 /* 1510 /*
@@ -1576,7 +1581,7 @@ static int run_delalloc_range(void *private_data, struct page *locked_page,
1576 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { 1581 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1577 ret = run_delalloc_nocow(inode, locked_page, start, end, 1582 ret = run_delalloc_nocow(inode, locked_page, start, end,
1578 page_started, 0, nr_written); 1583 page_started, 0, nr_written);
1579 } else if (!inode_need_compress(inode)) { 1584 } else if (!inode_need_compress(inode, start, end)) {
1580 ret = cow_file_range(inode, locked_page, start, end, end, 1585 ret = cow_file_range(inode, locked_page, start, end, end,
1581 page_started, nr_written, 1, NULL); 1586 page_started, nr_written, 1, NULL);
1582 } else { 1587 } else {
@@ -1796,10 +1801,11 @@ static void btrfs_clear_bit_hook(void *private_data,
1796 u64 len = state->end + 1 - state->start; 1801 u64 len = state->end + 1 - state->start;
1797 u32 num_extents = count_max_extents(len); 1802 u32 num_extents = count_max_extents(len);
1798 1803
1799 spin_lock(&inode->lock); 1804 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1800 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) 1805 spin_lock(&inode->lock);
1801 inode->defrag_bytes -= len; 1806 inode->defrag_bytes -= len;
1802 spin_unlock(&inode->lock); 1807 spin_unlock(&inode->lock);
1808 }
1803 1809
1804 /* 1810 /*
1805 * set_bit and clear bit hooks normally require _irqsave/restore 1811 * set_bit and clear bit hooks normally require _irqsave/restore
@@ -3159,8 +3165,6 @@ zeroit:
3159 memset(kaddr + pgoff, 1, len); 3165 memset(kaddr + pgoff, 1, len);
3160 flush_dcache_page(page); 3166 flush_dcache_page(page);
3161 kunmap_atomic(kaddr); 3167 kunmap_atomic(kaddr);
3162 if (csum_expected == 0)
3163 return 0;
3164 return -EIO; 3168 return -EIO;
3165} 3169}
3166 3170
@@ -5055,7 +5059,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5055 5059
5056 if (newsize > oldsize) { 5060 if (newsize > oldsize) {
5057 /* 5061 /*
5058 * Don't do an expanding truncate while snapshoting is ongoing. 5062 * Don't do an expanding truncate while snapshotting is ongoing.
5059 * This is to ensure the snapshot captures a fully consistent 5063 * This is to ensure the snapshot captures a fully consistent
5060 * state of this file - if the snapshot captures this expanding 5064 * state of this file - if the snapshot captures this expanding
5061 * truncation, it must capture all writes that happened before 5065 * truncation, it must capture all writes that happened before
@@ -5064,13 +5068,13 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5064 btrfs_wait_for_snapshot_creation(root); 5068 btrfs_wait_for_snapshot_creation(root);
5065 ret = btrfs_cont_expand(inode, oldsize, newsize); 5069 ret = btrfs_cont_expand(inode, oldsize, newsize);
5066 if (ret) { 5070 if (ret) {
5067 btrfs_end_write_no_snapshoting(root); 5071 btrfs_end_write_no_snapshotting(root);
5068 return ret; 5072 return ret;
5069 } 5073 }
5070 5074
5071 trans = btrfs_start_transaction(root, 1); 5075 trans = btrfs_start_transaction(root, 1);
5072 if (IS_ERR(trans)) { 5076 if (IS_ERR(trans)) {
5073 btrfs_end_write_no_snapshoting(root); 5077 btrfs_end_write_no_snapshotting(root);
5074 return PTR_ERR(trans); 5078 return PTR_ERR(trans);
5075 } 5079 }
5076 5080
@@ -5078,7 +5082,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5078 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL); 5082 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
5079 pagecache_isize_extended(inode, oldsize, newsize); 5083 pagecache_isize_extended(inode, oldsize, newsize);
5080 ret = btrfs_update_inode(trans, root, inode); 5084 ret = btrfs_update_inode(trans, root, inode);
5081 btrfs_end_write_no_snapshoting(root); 5085 btrfs_end_write_no_snapshotting(root);
5082 btrfs_end_transaction(trans); 5086 btrfs_end_transaction(trans);
5083 } else { 5087 } else {
5084 5088
@@ -5873,25 +5877,74 @@ unsigned char btrfs_filetype_table[] = {
5873 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK 5877 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5874}; 5878};
5875 5879
5880/*
5881 * All this infrastructure exists because dir_emit can fault, and we are holding
5882 * the tree lock when doing readdir. For now just allocate a buffer and copy
5883 * our information into that, and then dir_emit from the buffer. This is
5884 * similar to what NFS does, only we don't keep the buffer around in pagecache
5885 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5886 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5887 * tree lock.
5888 */
5889static int btrfs_opendir(struct inode *inode, struct file *file)
5890{
5891 struct btrfs_file_private *private;
5892
5893 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5894 if (!private)
5895 return -ENOMEM;
5896 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5897 if (!private->filldir_buf) {
5898 kfree(private);
5899 return -ENOMEM;
5900 }
5901 file->private_data = private;
5902 return 0;
5903}
5904
5905struct dir_entry {
5906 u64 ino;
5907 u64 offset;
5908 unsigned type;
5909 int name_len;
5910};
5911
5912static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5913{
5914 while (entries--) {
5915 struct dir_entry *entry = addr;
5916 char *name = (char *)(entry + 1);
5917
5918 ctx->pos = entry->offset;
5919 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5920 entry->type))
5921 return 1;
5922 addr += sizeof(struct dir_entry) + entry->name_len;
5923 ctx->pos++;
5924 }
5925 return 0;
5926}
5927
5876static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) 5928static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5877{ 5929{
5878 struct inode *inode = file_inode(file); 5930 struct inode *inode = file_inode(file);
5879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 5931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5880 struct btrfs_root *root = BTRFS_I(inode)->root; 5932 struct btrfs_root *root = BTRFS_I(inode)->root;
5933 struct btrfs_file_private *private = file->private_data;
5881 struct btrfs_dir_item *di; 5934 struct btrfs_dir_item *di;
5882 struct btrfs_key key; 5935 struct btrfs_key key;
5883 struct btrfs_key found_key; 5936 struct btrfs_key found_key;
5884 struct btrfs_path *path; 5937 struct btrfs_path *path;
5938 void *addr;
5885 struct list_head ins_list; 5939 struct list_head ins_list;
5886 struct list_head del_list; 5940 struct list_head del_list;
5887 int ret; 5941 int ret;
5888 struct extent_buffer *leaf; 5942 struct extent_buffer *leaf;
5889 int slot; 5943 int slot;
5890 unsigned char d_type;
5891 int over = 0;
5892 char tmp_name[32];
5893 char *name_ptr; 5944 char *name_ptr;
5894 int name_len; 5945 int name_len;
5946 int entries = 0;
5947 int total_len = 0;
5895 bool put = false; 5948 bool put = false;
5896 struct btrfs_key location; 5949 struct btrfs_key location;
5897 5950
@@ -5902,12 +5955,14 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5902 if (!path) 5955 if (!path)
5903 return -ENOMEM; 5956 return -ENOMEM;
5904 5957
5958 addr = private->filldir_buf;
5905 path->reada = READA_FORWARD; 5959 path->reada = READA_FORWARD;
5906 5960
5907 INIT_LIST_HEAD(&ins_list); 5961 INIT_LIST_HEAD(&ins_list);
5908 INIT_LIST_HEAD(&del_list); 5962 INIT_LIST_HEAD(&del_list);
5909 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list); 5963 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
5910 5964
5965again:
5911 key.type = BTRFS_DIR_INDEX_KEY; 5966 key.type = BTRFS_DIR_INDEX_KEY;
5912 key.offset = ctx->pos; 5967 key.offset = ctx->pos;
5913 key.objectid = btrfs_ino(BTRFS_I(inode)); 5968 key.objectid = btrfs_ino(BTRFS_I(inode));
@@ -5917,6 +5972,8 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5917 goto err; 5972 goto err;
5918 5973
5919 while (1) { 5974 while (1) {
5975 struct dir_entry *entry;
5976
5920 leaf = path->nodes[0]; 5977 leaf = path->nodes[0];
5921 slot = path->slots[0]; 5978 slot = path->slots[0];
5922 if (slot >= btrfs_header_nritems(leaf)) { 5979 if (slot >= btrfs_header_nritems(leaf)) {
@@ -5938,41 +5995,43 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5938 goto next; 5995 goto next;
5939 if (btrfs_should_delete_dir_index(&del_list, found_key.offset)) 5996 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5940 goto next; 5997 goto next;
5941
5942 ctx->pos = found_key.offset;
5943
5944 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 5998 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5945 if (verify_dir_item(fs_info, leaf, slot, di)) 5999 if (verify_dir_item(fs_info, leaf, slot, di))
5946 goto next; 6000 goto next;
5947 6001
5948 name_len = btrfs_dir_name_len(leaf, di); 6002 name_len = btrfs_dir_name_len(leaf, di);
5949 if (name_len <= sizeof(tmp_name)) { 6003 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5950 name_ptr = tmp_name; 6004 PAGE_SIZE) {
5951 } else { 6005 btrfs_release_path(path);
5952 name_ptr = kmalloc(name_len, GFP_KERNEL); 6006 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5953 if (!name_ptr) { 6007 if (ret)
5954 ret = -ENOMEM; 6008 goto nopos;
5955 goto err; 6009 addr = private->filldir_buf;
5956 } 6010 entries = 0;
6011 total_len = 0;
6012 goto again;
5957 } 6013 }
6014
6015 entry = addr;
6016 entry->name_len = name_len;
6017 name_ptr = (char *)(entry + 1);
5958 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), 6018 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5959 name_len); 6019 name_len);
5960 6020 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5961 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5962 btrfs_dir_item_key_to_cpu(leaf, di, &location); 6021 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5963 6022 entry->ino = location.objectid;
5964 over = !dir_emit(ctx, name_ptr, name_len, location.objectid, 6023 entry->offset = found_key.offset;
5965 d_type); 6024 entries++;
5966 6025 addr += sizeof(struct dir_entry) + name_len;
5967 if (name_ptr != tmp_name) 6026 total_len += sizeof(struct dir_entry) + name_len;
5968 kfree(name_ptr);
5969
5970 if (over)
5971 goto nopos;
5972 ctx->pos++;
5973next: 6027next:
5974 path->slots[0]++; 6028 path->slots[0]++;
5975 } 6029 }
6030 btrfs_release_path(path);
6031
6032 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6033 if (ret)
6034 goto nopos;
5976 6035
5977 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list); 6036 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5978 if (ret) 6037 if (ret)
@@ -6185,6 +6244,37 @@ static int btrfs_insert_inode_locked(struct inode *inode)
6185 btrfs_find_actor, &args); 6244 btrfs_find_actor, &args);
6186} 6245}
6187 6246
6247/*
6248 * Inherit flags from the parent inode.
6249 *
6250 * Currently only the compression flags and the cow flags are inherited.
6251 */
6252static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6253{
6254 unsigned int flags;
6255
6256 if (!dir)
6257 return;
6258
6259 flags = BTRFS_I(dir)->flags;
6260
6261 if (flags & BTRFS_INODE_NOCOMPRESS) {
6262 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6263 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6264 } else if (flags & BTRFS_INODE_COMPRESS) {
6265 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6266 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6267 }
6268
6269 if (flags & BTRFS_INODE_NODATACOW) {
6270 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6271 if (S_ISREG(inode->i_mode))
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6273 }
6274
6275 btrfs_update_iflags(inode);
6276}
6277
6188static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, 6278static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6189 struct btrfs_root *root, 6279 struct btrfs_root *root,
6190 struct inode *dir, 6280 struct inode *dir,
@@ -7924,11 +8014,12 @@ err:
7924 return ret; 8014 return ret;
7925} 8015}
7926 8016
7927static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, 8017static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7928 int mirror_num) 8018 struct bio *bio,
8019 int mirror_num)
7929{ 8020{
7930 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8021 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7931 int ret; 8022 blk_status_t ret;
7932 8023
7933 BUG_ON(bio_op(bio) == REQ_OP_WRITE); 8024 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7934 8025
@@ -7980,31 +8071,32 @@ static int btrfs_check_dio_repairable(struct inode *inode,
7980 return 1; 8071 return 1;
7981} 8072}
7982 8073
7983static int dio_read_error(struct inode *inode, struct bio *failed_bio, 8074static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7984 struct page *page, unsigned int pgoff, 8075 struct page *page, unsigned int pgoff,
7985 u64 start, u64 end, int failed_mirror, 8076 u64 start, u64 end, int failed_mirror,
7986 bio_end_io_t *repair_endio, void *repair_arg) 8077 bio_end_io_t *repair_endio, void *repair_arg)
7987{ 8078{
7988 struct io_failure_record *failrec; 8079 struct io_failure_record *failrec;
7989 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8080 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7990 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; 8081 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7991 struct bio *bio; 8082 struct bio *bio;
7992 int isector; 8083 int isector;
7993 int read_mode = 0; 8084 unsigned int read_mode = 0;
7994 int segs; 8085 int segs;
7995 int ret; 8086 int ret;
8087 blk_status_t status;
7996 8088
7997 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE); 8089 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
7998 8090
7999 ret = btrfs_get_io_failure_record(inode, start, end, &failrec); 8091 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8000 if (ret) 8092 if (ret)
8001 return ret; 8093 return errno_to_blk_status(ret);
8002 8094
8003 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec, 8095 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8004 failed_mirror); 8096 failed_mirror);
8005 if (!ret) { 8097 if (!ret) {
8006 free_io_failure(failure_tree, io_tree, failrec); 8098 free_io_failure(failure_tree, io_tree, failrec);
8007 return -EIO; 8099 return BLK_STS_IOERR;
8008 } 8100 }
8009 8101
8010 segs = bio_segments(failed_bio); 8102 segs = bio_segments(failed_bio);
@@ -8019,16 +8111,16 @@ static int dio_read_error(struct inode *inode, struct bio *failed_bio,
8019 bio_set_op_attrs(bio, REQ_OP_READ, read_mode); 8111 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
8020 8112
8021 btrfs_debug(BTRFS_I(inode)->root->fs_info, 8113 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8022 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n", 8114 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
8023 read_mode, failrec->this_mirror, failrec->in_validation); 8115 read_mode, failrec->this_mirror, failrec->in_validation);
8024 8116
8025 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror); 8117 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8026 if (ret) { 8118 if (status) {
8027 free_io_failure(failure_tree, io_tree, failrec); 8119 free_io_failure(failure_tree, io_tree, failrec);
8028 bio_put(bio); 8120 bio_put(bio);
8029 } 8121 }
8030 8122
8031 return ret; 8123 return status;
8032} 8124}
8033 8125
8034struct btrfs_retry_complete { 8126struct btrfs_retry_complete {
@@ -8065,8 +8157,8 @@ end:
8065 bio_put(bio); 8157 bio_put(bio);
8066} 8158}
8067 8159
8068static int __btrfs_correct_data_nocsum(struct inode *inode, 8160static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8069 struct btrfs_io_bio *io_bio) 8161 struct btrfs_io_bio *io_bio)
8070{ 8162{
8071 struct btrfs_fs_info *fs_info; 8163 struct btrfs_fs_info *fs_info;
8072 struct bio_vec bvec; 8164 struct bio_vec bvec;
@@ -8076,8 +8168,8 @@ static int __btrfs_correct_data_nocsum(struct inode *inode,
8076 unsigned int pgoff; 8168 unsigned int pgoff;
8077 u32 sectorsize; 8169 u32 sectorsize;
8078 int nr_sectors; 8170 int nr_sectors;
8079 int ret; 8171 blk_status_t ret;
8080 int err = 0; 8172 blk_status_t err = BLK_STS_OK;
8081 8173
8082 fs_info = BTRFS_I(inode)->root->fs_info; 8174 fs_info = BTRFS_I(inode)->root->fs_info;
8083 sectorsize = fs_info->sectorsize; 8175 sectorsize = fs_info->sectorsize;
@@ -8104,7 +8196,7 @@ next_block_or_try_again:
8104 goto next; 8196 goto next;
8105 } 8197 }
8106 8198
8107 wait_for_completion(&done.done); 8199 wait_for_completion_io(&done.done);
8108 8200
8109 if (!done.uptodate) { 8201 if (!done.uptodate) {
8110 /* We might have another mirror, so try again */ 8202 /* We might have another mirror, so try again */
@@ -8183,11 +8275,12 @@ static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8183 int csum_pos; 8275 int csum_pos;
8184 bool uptodate = (err == 0); 8276 bool uptodate = (err == 0);
8185 int ret; 8277 int ret;
8278 blk_status_t status;
8186 8279
8187 fs_info = BTRFS_I(inode)->root->fs_info; 8280 fs_info = BTRFS_I(inode)->root->fs_info;
8188 sectorsize = fs_info->sectorsize; 8281 sectorsize = fs_info->sectorsize;
8189 8282
8190 err = 0; 8283 err = BLK_STS_OK;
8191 start = io_bio->logical; 8284 start = io_bio->logical;
8192 done.inode = inode; 8285 done.inode = inode;
8193 io_bio->bio.bi_iter = io_bio->iter; 8286 io_bio->bio.bi_iter = io_bio->iter;
@@ -8209,16 +8302,16 @@ try_again:
8209 done.start = start; 8302 done.start = start;
8210 init_completion(&done.done); 8303 init_completion(&done.done);
8211 8304
8212 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page, 8305 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8213 pgoff, start, start + sectorsize - 1, 8306 pgoff, start, start + sectorsize - 1,
8214 io_bio->mirror_num, 8307 io_bio->mirror_num, btrfs_retry_endio,
8215 btrfs_retry_endio, &done); 8308 &done);
8216 if (ret) { 8309 if (status) {
8217 err = errno_to_blk_status(ret); 8310 err = status;
8218 goto next; 8311 goto next;
8219 } 8312 }
8220 8313
8221 wait_for_completion(&done.done); 8314 wait_for_completion_io(&done.done);
8222 8315
8223 if (!done.uptodate) { 8316 if (!done.uptodate) {
8224 /* We might have another mirror, so try again */ 8317 /* We might have another mirror, so try again */
@@ -8250,7 +8343,7 @@ static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8250 if (unlikely(err)) 8343 if (unlikely(err))
8251 return __btrfs_correct_data_nocsum(inode, io_bio); 8344 return __btrfs_correct_data_nocsum(inode, io_bio);
8252 else 8345 else
8253 return 0; 8346 return BLK_STS_OK;
8254 } else { 8347 } else {
8255 return __btrfs_subio_endio_read(inode, io_bio, err); 8348 return __btrfs_subio_endio_read(inode, io_bio, err);
8256 } 8349 }
@@ -8423,9 +8516,9 @@ static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8423 return 0; 8516 return 0;
8424} 8517}
8425 8518
8426static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, 8519static inline blk_status_t
8427 u64 file_offset, int skip_sum, 8520__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
8428 int async_submit) 8521 int async_submit)
8429{ 8522{
8430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8523 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8431 struct btrfs_dio_private *dip = bio->bi_private; 8524 struct btrfs_dio_private *dip = bio->bi_private;
@@ -8443,7 +8536,7 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8443 goto err; 8536 goto err;
8444 } 8537 }
8445 8538
8446 if (skip_sum) 8539 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
8447 goto map; 8540 goto map;
8448 8541
8449 if (write && async_submit) { 8542 if (write && async_submit) {
@@ -8473,8 +8566,7 @@ err:
8473 return ret; 8566 return ret;
8474} 8567}
8475 8568
8476static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip, 8569static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
8477 int skip_sum)
8478{ 8570{
8479 struct inode *inode = dip->inode; 8571 struct inode *inode = dip->inode;
8480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8572 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
@@ -8488,6 +8580,7 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8488 int clone_offset = 0; 8580 int clone_offset = 0;
8489 int clone_len; 8581 int clone_len;
8490 int ret; 8582 int ret;
8583 blk_status_t status;
8491 8584
8492 map_length = orig_bio->bi_iter.bi_size; 8585 map_length = orig_bio->bi_iter.bi_size;
8493 submit_len = map_length; 8586 submit_len = map_length;
@@ -8537,9 +8630,9 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8537 */ 8630 */
8538 atomic_inc(&dip->pending_bios); 8631 atomic_inc(&dip->pending_bios);
8539 8632
8540 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, 8633 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
8541 async_submit); 8634 async_submit);
8542 if (ret) { 8635 if (status) {
8543 bio_put(bio); 8636 bio_put(bio);
8544 atomic_dec(&dip->pending_bios); 8637 atomic_dec(&dip->pending_bios);
8545 goto out_err; 8638 goto out_err;
@@ -8557,9 +8650,8 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8557 } while (submit_len > 0); 8650 } while (submit_len > 0);
8558 8651
8559submit: 8652submit:
8560 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum, 8653 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
8561 async_submit); 8654 if (!status)
8562 if (!ret)
8563 return 0; 8655 return 0;
8564 8656
8565 bio_put(bio); 8657 bio_put(bio);
@@ -8583,12 +8675,9 @@ static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8583 struct btrfs_dio_private *dip = NULL; 8675 struct btrfs_dio_private *dip = NULL;
8584 struct bio *bio = NULL; 8676 struct bio *bio = NULL;
8585 struct btrfs_io_bio *io_bio; 8677 struct btrfs_io_bio *io_bio;
8586 int skip_sum;
8587 bool write = (bio_op(dio_bio) == REQ_OP_WRITE); 8678 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8588 int ret = 0; 8679 int ret = 0;
8589 8680
8590 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8591
8592 bio = btrfs_bio_clone(dio_bio); 8681 bio = btrfs_bio_clone(dio_bio);
8593 8682
8594 dip = kzalloc(sizeof(*dip), GFP_NOFS); 8683 dip = kzalloc(sizeof(*dip), GFP_NOFS);
@@ -8631,7 +8720,7 @@ static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8631 dio_data->unsubmitted_oe_range_end; 8720 dio_data->unsubmitted_oe_range_end;
8632 } 8721 }
8633 8722
8634 ret = btrfs_submit_direct_hook(dip, skip_sum); 8723 ret = btrfs_submit_direct_hook(dip);
8635 if (!ret) 8724 if (!ret)
8636 return; 8725 return;
8637 8726
@@ -8731,7 +8820,6 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8731 return 0; 8820 return 0;
8732 8821
8733 inode_dio_begin(inode); 8822 inode_dio_begin(inode);
8734 smp_mb__after_atomic();
8735 8823
8736 /* 8824 /*
8737 * The generic stuff only does filemap_write_and_wait_range, which 8825 * The generic stuff only does filemap_write_and_wait_range, which
@@ -9404,7 +9492,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
9404 ei->reserved_extents = 0; 9492 ei->reserved_extents = 0;
9405 9493
9406 ei->runtime_flags = 0; 9494 ei->runtime_flags = 0;
9407 ei->force_compress = BTRFS_COMPRESS_NONE; 9495 ei->prop_compress = BTRFS_COMPRESS_NONE;
9496 ei->defrag_compress = BTRFS_COMPRESS_NONE;
9408 9497
9409 ei->delayed_node = NULL; 9498 ei->delayed_node = NULL;
9410 9499
@@ -10744,6 +10833,7 @@ static const struct file_operations btrfs_dir_file_operations = {
10744 .llseek = generic_file_llseek, 10833 .llseek = generic_file_llseek,
10745 .read = generic_read_dir, 10834 .read = generic_read_dir,
10746 .iterate_shared = btrfs_real_readdir, 10835 .iterate_shared = btrfs_real_readdir,
10836 .open = btrfs_opendir,
10747 .unlocked_ioctl = btrfs_ioctl, 10837 .unlocked_ioctl = btrfs_ioctl,
10748#ifdef CONFIG_COMPAT 10838#ifdef CONFIG_COMPAT
10749 .compat_ioctl = btrfs_compat_ioctl, 10839 .compat_ioctl = btrfs_compat_ioctl,