David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 6 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
| 9 | #include <linux/highmem.h> |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 10 | #include <linux/sched/mm.h> |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 11 | #include "ctree.h" |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 12 | #include "disk-io.h" |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 13 | #include "transaction.h" |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 14 | #include "volumes.h" |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 15 | #include "print-tree.h" |
Anand Jain | ebb8765 | 2016-03-10 17:26:59 +0800 | [diff] [blame] | 16 | #include "compression.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 17 | |
Chris Mason | 42049bf | 2016-08-03 14:05:46 -0700 | [diff] [blame] | 18 | #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \ |
| 19 | sizeof(struct btrfs_item) * 2) / \ |
| 20 | size) - 1)) |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 21 | |
Zach Brown | 221b831 | 2012-09-20 14:33:00 -0600 | [diff] [blame] | 22 | #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 23 | PAGE_SIZE)) |
Chris Mason | 7ca4be4 | 2012-01-31 20:19:02 -0500 | [diff] [blame] | 24 | |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 25 | #define MAX_ORDERED_SUM_BYTES(fs_info) ((PAGE_SIZE - \ |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 26 | sizeof(struct btrfs_ordered_sum)) / \ |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 27 | sizeof(u32) * (fs_info)->sectorsize) |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 28 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 29 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 30 | struct btrfs_root *root, |
| 31 | u64 objectid, u64 pos, |
| 32 | u64 disk_offset, u64 disk_num_bytes, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 33 | u64 num_bytes, u64 offset, u64 ram_bytes, |
| 34 | u8 compression, u8 encryption, u16 other_encoding) |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 35 | { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 36 | int ret = 0; |
| 37 | struct btrfs_file_extent_item *item; |
| 38 | struct btrfs_key file_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 39 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 40 | struct extent_buffer *leaf; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 41 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 42 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 43 | if (!path) |
| 44 | return -ENOMEM; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 45 | file_key.objectid = objectid; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 46 | file_key.offset = pos; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 47 | file_key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 48 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 49 | path->leave_spinning = 1; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 50 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 51 | sizeof(*item)); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 52 | if (ret < 0) |
| 53 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 54 | BUG_ON(ret); /* Can't happen */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 55 | leaf = path->nodes[0]; |
| 56 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 57 | struct btrfs_file_extent_item); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 58 | btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 59 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 60 | btrfs_set_file_extent_offset(leaf, item, offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 61 | btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 62 | btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 63 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 64 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 65 | btrfs_set_file_extent_compression(leaf, item, compression); |
| 66 | btrfs_set_file_extent_encryption(leaf, item, encryption); |
| 67 | btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); |
| 68 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 69 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 70 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 71 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 72 | return ret; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 73 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 74 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 75 | static struct btrfs_csum_item * |
| 76 | btrfs_lookup_csum(struct btrfs_trans_handle *trans, |
| 77 | struct btrfs_root *root, |
| 78 | struct btrfs_path *path, |
| 79 | u64 bytenr, int cow) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 80 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 81 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 82 | int ret; |
| 83 | struct btrfs_key file_key; |
| 84 | struct btrfs_key found_key; |
| 85 | struct btrfs_csum_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 86 | struct extent_buffer *leaf; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 87 | u64 csum_offset = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 88 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 89 | int csums_in_item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 90 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 91 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 92 | file_key.offset = bytenr; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 93 | file_key.type = BTRFS_EXTENT_CSUM_KEY; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 94 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 95 | if (ret < 0) |
| 96 | goto fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 97 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 98 | if (ret > 0) { |
| 99 | ret = 1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 100 | if (path->slots[0] == 0) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 101 | goto fail; |
| 102 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 103 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 104 | if (found_key.type != BTRFS_EXTENT_CSUM_KEY) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 105 | goto fail; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 106 | |
| 107 | csum_offset = (bytenr - found_key.offset) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 108 | fs_info->sb->s_blocksize_bits; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 109 | csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 110 | csums_in_item /= csum_size; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 111 | |
Miao Xie | 82d130f | 2013-03-28 08:12:15 +0000 | [diff] [blame] | 112 | if (csum_offset == csums_in_item) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 113 | ret = -EFBIG; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 114 | goto fail; |
Miao Xie | 82d130f | 2013-03-28 08:12:15 +0000 | [diff] [blame] | 115 | } else if (csum_offset > csums_in_item) { |
| 116 | goto fail; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 120 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 121 | csum_offset * csum_size); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 122 | return item; |
| 123 | fail: |
| 124 | if (ret > 0) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 125 | ret = -ENOENT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 126 | return ERR_PTR(ret); |
| 127 | } |
| 128 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 129 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 130 | struct btrfs_root *root, |
| 131 | struct btrfs_path *path, u64 objectid, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 132 | u64 offset, int mod) |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 133 | { |
| 134 | int ret; |
| 135 | struct btrfs_key file_key; |
| 136 | int ins_len = mod < 0 ? -1 : 0; |
| 137 | int cow = mod != 0; |
| 138 | |
| 139 | file_key.objectid = objectid; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 140 | file_key.offset = offset; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 141 | file_key.type = BTRFS_EXTENT_DATA_KEY; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 142 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); |
| 143 | return ret; |
| 144 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 145 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 146 | static blk_status_t __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 147 | u64 logical_offset, u32 *dst, int dio) |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 148 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 149 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 150 | struct bio_vec bvec; |
| 151 | struct bvec_iter iter; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 152 | struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); |
| 153 | struct btrfs_csum_item *item = NULL; |
| 154 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 155 | struct btrfs_path *path; |
| 156 | u8 *csum; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 157 | u64 offset = 0; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 158 | u64 item_start_offset = 0; |
| 159 | u64 item_last_offset = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 160 | u64 disk_bytenr; |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 161 | u64 page_bytes_left; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 162 | u32 diff; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 163 | int nblocks; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 164 | int count = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 165 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 166 | |
| 167 | path = btrfs_alloc_path(); |
Tsutomu Itoh | c2db107 | 2011-03-01 06:48:31 +0000 | [diff] [blame] | 168 | if (!path) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 169 | return BLK_STS_RESOURCE; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 170 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 171 | nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 172 | if (!dst) { |
| 173 | if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) { |
David Sterba | 31feccc | 2018-11-22 17:16:46 +0100 | [diff] [blame] | 174 | btrfs_bio->csum = kmalloc_array(nblocks, csum_size, |
| 175 | GFP_NOFS); |
| 176 | if (!btrfs_bio->csum) { |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 177 | btrfs_free_path(path); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 178 | return BLK_STS_RESOURCE; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 179 | } |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 180 | } else { |
| 181 | btrfs_bio->csum = btrfs_bio->csum_inline; |
| 182 | } |
| 183 | csum = btrfs_bio->csum; |
| 184 | } else { |
| 185 | csum = (u8 *)dst; |
| 186 | } |
| 187 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 188 | if (bio->bi_iter.bi_size > PAGE_SIZE * 8) |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 189 | path->reada = READA_FORWARD; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 190 | |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 191 | /* |
| 192 | * the free space stuff is only read when it hasn't been |
| 193 | * updated in the current transaction. So, we can safely |
| 194 | * read from the commit root and sidestep a nasty deadlock |
| 195 | * between reading the free space cache and updating the csum tree. |
| 196 | */ |
Nikolay Borisov | 70ddc55 | 2017-02-20 13:50:35 +0200 | [diff] [blame] | 197 | if (btrfs_is_free_space_inode(BTRFS_I(inode))) { |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 198 | path->search_commit_root = 1; |
Josef Bacik | ddf23b3 | 2011-09-11 10:52:24 -0400 | [diff] [blame] | 199 | path->skip_locking = 1; |
| 200 | } |
Chris Mason | 2cf8572 | 2011-07-26 15:35:09 -0400 | [diff] [blame] | 201 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 202 | disk_bytenr = (u64)bio->bi_iter.bi_sector << 9; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 203 | if (dio) |
| 204 | offset = logical_offset; |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 205 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 206 | bio_for_each_segment(bvec, bio, iter) { |
| 207 | page_bytes_left = bvec.bv_len; |
Christoph Hellwig | 4989d27 | 2016-11-25 09:07:52 +0100 | [diff] [blame] | 208 | if (count) |
| 209 | goto next; |
| 210 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 211 | if (!dio) |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 212 | offset = page_offset(bvec.bv_page) + bvec.bv_offset; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 213 | count = btrfs_find_ordered_sum(inode, offset, disk_bytenr, |
| 214 | (u32 *)csum, nblocks); |
Miao Xie | e4100d9 | 2013-04-05 07:20:56 +0000 | [diff] [blame] | 215 | if (count) |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 216 | goto found; |
| 217 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 218 | if (!item || disk_bytenr < item_start_offset || |
| 219 | disk_bytenr >= item_last_offset) { |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 220 | struct btrfs_key found_key; |
| 221 | u32 item_size; |
| 222 | |
| 223 | if (item) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 224 | btrfs_release_path(path); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 225 | item = btrfs_lookup_csum(NULL, fs_info->csum_root, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 226 | path, disk_bytenr, 0); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 227 | if (IS_ERR(item)) { |
Miao Xie | e4100d9 | 2013-04-05 07:20:56 +0000 | [diff] [blame] | 228 | count = 1; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 229 | memset(csum, 0, csum_size); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 230 | if (BTRFS_I(inode)->root->root_key.objectid == |
| 231 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 232 | set_extent_bits(io_tree, offset, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 233 | offset + fs_info->sectorsize - 1, |
David Sterba | ceeb0ae | 2016-04-26 23:54:39 +0200 | [diff] [blame] | 234 | EXTENT_NODATASUM); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 235 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 236 | btrfs_info_rl(fs_info, |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 237 | "no csum found for inode %llu start %llu", |
Nikolay Borisov | 4a0cc7c | 2017-01-10 20:35:31 +0200 | [diff] [blame] | 238 | btrfs_ino(BTRFS_I(inode)), offset); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 239 | } |
Chris Mason | 6dab815 | 2008-08-04 08:35:53 -0400 | [diff] [blame] | 240 | item = NULL; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 241 | btrfs_release_path(path); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 242 | goto found; |
| 243 | } |
| 244 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 245 | path->slots[0]); |
| 246 | |
| 247 | item_start_offset = found_key.offset; |
| 248 | item_size = btrfs_item_size_nr(path->nodes[0], |
| 249 | path->slots[0]); |
| 250 | item_last_offset = item_start_offset + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 251 | (item_size / csum_size) * |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 252 | fs_info->sectorsize; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 253 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 254 | struct btrfs_csum_item); |
| 255 | } |
| 256 | /* |
| 257 | * this byte range must be able to fit inside |
| 258 | * a single leaf so it will also fit inside a u32 |
| 259 | */ |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 260 | diff = disk_bytenr - item_start_offset; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 261 | diff = diff / fs_info->sectorsize; |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 262 | diff = diff * csum_size; |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 263 | count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> |
| 264 | inode->i_sb->s_blocksize_bits); |
| 265 | read_extent_buffer(path->nodes[0], csum, |
Chris Mason | 3de9d6b | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 266 | ((unsigned long)item) + diff, |
Miao Xie | e4100d9 | 2013-04-05 07:20:56 +0000 | [diff] [blame] | 267 | csum_size * count); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 268 | found: |
Miao Xie | facc8a22 | 2013-07-25 19:22:34 +0800 | [diff] [blame] | 269 | csum += count * csum_size; |
| 270 | nblocks -= count; |
Christoph Hellwig | 4989d27 | 2016-11-25 09:07:52 +0100 | [diff] [blame] | 271 | next: |
Miao Xie | e4100d9 | 2013-04-05 07:20:56 +0000 | [diff] [blame] | 272 | while (count--) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 273 | disk_bytenr += fs_info->sectorsize; |
| 274 | offset += fs_info->sectorsize; |
| 275 | page_bytes_left -= fs_info->sectorsize; |
Christoph Hellwig | 4989d27 | 2016-11-25 09:07:52 +0100 | [diff] [blame] | 276 | if (!page_bytes_left) |
| 277 | break; /* move to next bio */ |
Miao Xie | e4100d9 | 2013-04-05 07:20:56 +0000 | [diff] [blame] | 278 | } |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 279 | } |
Chris Mason | 389f239 | 2016-03-21 06:59:09 -0700 | [diff] [blame] | 280 | |
Christoph Hellwig | 4989d27 | 2016-11-25 09:07:52 +0100 | [diff] [blame] | 281 | WARN_ON_ONCE(count); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 282 | btrfs_free_path(path); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 286 | blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 287 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 288 | return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 291 | blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 292 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 293 | return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 294 | } |
| 295 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 296 | int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 297 | struct list_head *list, int search_commit) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 298 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 299 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 300 | struct btrfs_key key; |
| 301 | struct btrfs_path *path; |
| 302 | struct extent_buffer *leaf; |
| 303 | struct btrfs_ordered_sum *sums; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 304 | struct btrfs_csum_item *item; |
Mark Fasheh | 0678b61 | 2011-08-05 15:46:16 -0700 | [diff] [blame] | 305 | LIST_HEAD(tmplist); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 306 | unsigned long offset; |
| 307 | int ret; |
| 308 | size_t size; |
| 309 | u64 csum_end; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 310 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 311 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 312 | ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && |
| 313 | IS_ALIGNED(end + 1, fs_info->sectorsize)); |
Josef Bacik | 4277a9c | 2013-10-15 09:36:40 -0400 | [diff] [blame] | 314 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 315 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 316 | if (!path) |
| 317 | return -ENOMEM; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 318 | |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 319 | if (search_commit) { |
| 320 | path->skip_locking = 1; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 321 | path->reada = READA_FORWARD; |
Arne Jansen | a2de733 | 2011-03-08 14:14:00 +0100 | [diff] [blame] | 322 | path->search_commit_root = 1; |
| 323 | } |
| 324 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 325 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 326 | key.offset = start; |
| 327 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 328 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 329 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 330 | if (ret < 0) |
| 331 | goto fail; |
| 332 | if (ret > 0 && path->slots[0] > 0) { |
| 333 | leaf = path->nodes[0]; |
| 334 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
| 335 | if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && |
| 336 | key.type == BTRFS_EXTENT_CSUM_KEY) { |
| 337 | offset = (start - key.offset) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 338 | fs_info->sb->s_blocksize_bits; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 339 | if (offset * csum_size < |
| 340 | btrfs_item_size_nr(leaf, path->slots[0] - 1)) |
| 341 | path->slots[0]--; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | while (start <= end) { |
| 346 | leaf = path->nodes[0]; |
| 347 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 348 | ret = btrfs_next_leaf(root, path); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 349 | if (ret < 0) |
| 350 | goto fail; |
| 351 | if (ret > 0) |
| 352 | break; |
| 353 | leaf = path->nodes[0]; |
| 354 | } |
| 355 | |
| 356 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 357 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
Zhi Yong Wu | 628c828 | 2013-03-18 09:18:09 +0000 | [diff] [blame] | 358 | key.type != BTRFS_EXTENT_CSUM_KEY || |
| 359 | key.offset > end) |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 360 | break; |
| 361 | |
| 362 | if (key.offset > start) |
| 363 | start = key.offset; |
| 364 | |
| 365 | size = btrfs_item_size_nr(leaf, path->slots[0]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 366 | csum_end = key.offset + (size / csum_size) * fs_info->sectorsize; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 367 | if (csum_end <= start) { |
| 368 | path->slots[0]++; |
| 369 | continue; |
| 370 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 371 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 372 | csum_end = min(csum_end, end + 1); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 373 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 374 | struct btrfs_csum_item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 375 | while (start < csum_end) { |
| 376 | size = min_t(size_t, csum_end - start, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 377 | MAX_ORDERED_SUM_BYTES(fs_info)); |
| 378 | sums = kzalloc(btrfs_ordered_sum_size(fs_info, size), |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 379 | GFP_NOFS); |
Mark Fasheh | 0678b61 | 2011-08-05 15:46:16 -0700 | [diff] [blame] | 380 | if (!sums) { |
| 381 | ret = -ENOMEM; |
| 382 | goto fail; |
| 383 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 384 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 385 | sums->bytenr = start; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 386 | sums->len = (int)size; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 387 | |
| 388 | offset = (start - key.offset) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 389 | fs_info->sb->s_blocksize_bits; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 390 | offset *= csum_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 391 | size >>= fs_info->sb->s_blocksize_bits; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 392 | |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 393 | read_extent_buffer(path->nodes[0], |
| 394 | sums->sums, |
| 395 | ((unsigned long)item) + offset, |
| 396 | csum_size * size); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 397 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 398 | start += fs_info->sectorsize * size; |
Mark Fasheh | 0678b61 | 2011-08-05 15:46:16 -0700 | [diff] [blame] | 399 | list_add_tail(&sums->list, &tmplist); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 400 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 401 | path->slots[0]++; |
| 402 | } |
| 403 | ret = 0; |
| 404 | fail: |
Mark Fasheh | 0678b61 | 2011-08-05 15:46:16 -0700 | [diff] [blame] | 405 | while (ret < 0 && !list_empty(&tmplist)) { |
Chris Mason | 6e5aafb | 2014-11-04 06:59:04 -0800 | [diff] [blame] | 406 | sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); |
Mark Fasheh | 0678b61 | 2011-08-05 15:46:16 -0700 | [diff] [blame] | 407 | list_del(&sums->list); |
| 408 | kfree(sums); |
| 409 | } |
| 410 | list_splice_tail(&tmplist, list); |
| 411 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 412 | btrfs_free_path(path); |
| 413 | return ret; |
| 414 | } |
| 415 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 416 | blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 417 | u64 file_start, int contig) |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 418 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 419 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 420 | struct btrfs_ordered_sum *sums; |
Christoph Hellwig | 6cd7ce4 | 2016-11-25 09:07:49 +0100 | [diff] [blame] | 421 | struct btrfs_ordered_extent *ordered = NULL; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 422 | char *data; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 423 | struct bvec_iter iter; |
| 424 | struct bio_vec bvec; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 425 | int index; |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 426 | int nr_sectors; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 427 | unsigned long total_bytes = 0; |
| 428 | unsigned long this_sum_bytes = 0; |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 429 | int i; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 430 | u64 offset; |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 431 | unsigned nofs_flag; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 432 | |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 433 | nofs_flag = memalloc_nofs_save(); |
| 434 | sums = kvzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size), |
| 435 | GFP_KERNEL); |
| 436 | memalloc_nofs_restore(nofs_flag); |
| 437 | |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 438 | if (!sums) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 439 | return BLK_STS_RESOURCE; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 440 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 441 | sums->len = bio->bi_iter.bi_size; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 442 | INIT_LIST_HEAD(&sums->list); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 443 | |
| 444 | if (contig) |
| 445 | offset = file_start; |
| 446 | else |
Christoph Hellwig | 6cd7ce4 | 2016-11-25 09:07:49 +0100 | [diff] [blame] | 447 | offset = 0; /* shut up gcc */ |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 448 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 449 | sums->bytenr = (u64)bio->bi_iter.bi_sector << 9; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 450 | index = 0; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 451 | |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 452 | bio_for_each_segment(bvec, bio, iter) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 453 | if (!contig) |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 454 | offset = page_offset(bvec.bv_page) + bvec.bv_offset; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 455 | |
Christoph Hellwig | 6cd7ce4 | 2016-11-25 09:07:49 +0100 | [diff] [blame] | 456 | if (!ordered) { |
| 457 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
| 458 | BUG_ON(!ordered); /* Logic error */ |
| 459 | } |
| 460 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 461 | nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 462 | bvec.bv_len + fs_info->sectorsize |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 463 | - 1); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 464 | |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 465 | for (i = 0; i < nr_sectors; i++) { |
| 466 | if (offset >= ordered->file_offset + ordered->len || |
| 467 | offset < ordered->file_offset) { |
| 468 | unsigned long bytes_left; |
| 469 | |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 470 | sums->len = this_sum_bytes; |
| 471 | this_sum_bytes = 0; |
Nikolay Borisov | f975626 | 2019-04-10 16:16:11 +0300 | [diff] [blame] | 472 | btrfs_add_ordered_sum(ordered, sums); |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 473 | btrfs_put_ordered_extent(ordered); |
| 474 | |
| 475 | bytes_left = bio->bi_iter.bi_size - total_bytes; |
| 476 | |
Nikolay Borisov | a3d46ae | 2019-04-01 11:29:58 +0300 | [diff] [blame] | 477 | nofs_flag = memalloc_nofs_save(); |
| 478 | sums = kvzalloc(btrfs_ordered_sum_size(fs_info, |
| 479 | bytes_left), GFP_KERNEL); |
| 480 | memalloc_nofs_restore(nofs_flag); |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 481 | BUG_ON(!sums); /* -ENOMEM */ |
| 482 | sums->len = bytes_left; |
| 483 | ordered = btrfs_lookup_ordered_extent(inode, |
| 484 | offset); |
| 485 | ASSERT(ordered); /* Logic error */ |
| 486 | sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9) |
| 487 | + total_bytes; |
| 488 | index = 0; |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | sums->sums[index] = ~(u32)0; |
Johannes Thumshirn | 443c8e2 | 2019-03-07 17:14:00 +0100 | [diff] [blame] | 492 | data = kmap_atomic(bvec.bv_page); |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 493 | sums->sums[index] |
Liu Bo | 17347ce | 2017-05-15 15:33:27 -0700 | [diff] [blame] | 494 | = btrfs_csum_data(data + bvec.bv_offset |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 495 | + (i * fs_info->sectorsize), |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 496 | sums->sums[index], |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 497 | fs_info->sectorsize); |
Johannes Thumshirn | 443c8e2 | 2019-03-07 17:14:00 +0100 | [diff] [blame] | 498 | kunmap_atomic(data); |
Chandan Rajendra | c40a3d3 | 2016-01-21 15:55:54 +0530 | [diff] [blame] | 499 | btrfs_csum_final(sums->sums[index], |
| 500 | (char *)(sums->sums + index)); |
| 501 | index++; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 502 | offset += fs_info->sectorsize; |
| 503 | this_sum_bytes += fs_info->sectorsize; |
| 504 | total_bytes += fs_info->sectorsize; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 507 | } |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 508 | this_sum_bytes = 0; |
Nikolay Borisov | f975626 | 2019-04-10 16:16:11 +0300 | [diff] [blame] | 509 | btrfs_add_ordered_sum(ordered, sums); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 510 | btrfs_put_ordered_extent(ordered); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 511 | return 0; |
| 512 | } |
| 513 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 514 | /* |
| 515 | * helper function for csum removal, this expects the |
| 516 | * key to describe the csum pointed to by the path, and it expects |
| 517 | * the csum to overlap the range [bytenr, len] |
| 518 | * |
| 519 | * The csum should not be entirely contained in the range and the |
| 520 | * range should not be entirely contained in the csum. |
| 521 | * |
| 522 | * This calls btrfs_truncate_item with the correct args based on the |
| 523 | * overlap, and fixes up the key as required. |
| 524 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 525 | static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 526 | struct btrfs_path *path, |
| 527 | struct btrfs_key *key, |
| 528 | u64 bytenr, u64 len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 529 | { |
| 530 | struct extent_buffer *leaf; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 531 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 532 | u64 csum_end; |
| 533 | u64 end_byte = bytenr + len; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 534 | u32 blocksize_bits = fs_info->sb->s_blocksize_bits; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 535 | |
| 536 | leaf = path->nodes[0]; |
| 537 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 538 | csum_end <<= fs_info->sb->s_blocksize_bits; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 539 | csum_end += key->offset; |
| 540 | |
| 541 | if (key->offset < bytenr && csum_end <= end_byte) { |
| 542 | /* |
| 543 | * [ bytenr - len ] |
| 544 | * [ ] |
| 545 | * [csum ] |
| 546 | * A simple truncate off the end of the item |
| 547 | */ |
| 548 | u32 new_size = (bytenr - key->offset) >> blocksize_bits; |
| 549 | new_size *= csum_size; |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame^] | 550 | btrfs_truncate_item(path, new_size, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 551 | } else if (key->offset >= bytenr && csum_end > end_byte && |
| 552 | end_byte > key->offset) { |
| 553 | /* |
| 554 | * [ bytenr - len ] |
| 555 | * [ ] |
| 556 | * [csum ] |
| 557 | * we need to truncate from the beginning of the csum |
| 558 | */ |
| 559 | u32 new_size = (csum_end - end_byte) >> blocksize_bits; |
| 560 | new_size *= csum_size; |
| 561 | |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame^] | 562 | btrfs_truncate_item(path, new_size, 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 563 | |
| 564 | key->offset = end_byte; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 565 | btrfs_set_item_key_safe(fs_info, path, key); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 566 | } else { |
| 567 | BUG(); |
| 568 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | * deletes the csum items from the csum tree for a given |
| 573 | * range of bytes. |
| 574 | */ |
| 575 | int btrfs_del_csums(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 576 | struct btrfs_fs_info *fs_info, u64 bytenr, u64 len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 577 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 578 | struct btrfs_root *root = fs_info->csum_root; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 579 | struct btrfs_path *path; |
| 580 | struct btrfs_key key; |
| 581 | u64 end_byte = bytenr + len; |
| 582 | u64 csum_end; |
| 583 | struct extent_buffer *leaf; |
| 584 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 585 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
| 586 | int blocksize_bits = fs_info->sb->s_blocksize_bits; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 587 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 588 | path = btrfs_alloc_path(); |
liubo | 2a29edc | 2011-01-26 06:22:08 +0000 | [diff] [blame] | 589 | if (!path) |
| 590 | return -ENOMEM; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 591 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 592 | while (1) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 593 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 594 | key.offset = end_byte - 1; |
| 595 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 596 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 597 | path->leave_spinning = 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 598 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 599 | if (ret > 0) { |
| 600 | if (path->slots[0] == 0) |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 601 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 602 | path->slots[0]--; |
Josef Bacik | ad0397a | 2011-01-28 18:44:44 +0000 | [diff] [blame] | 603 | } else if (ret < 0) { |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 604 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 605 | } |
Josef Bacik | ad0397a | 2011-01-28 18:44:44 +0000 | [diff] [blame] | 606 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 607 | leaf = path->nodes[0]; |
| 608 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 609 | |
| 610 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
| 611 | key.type != BTRFS_EXTENT_CSUM_KEY) { |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | if (key.offset >= end_byte) |
| 616 | break; |
| 617 | |
| 618 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; |
| 619 | csum_end <<= blocksize_bits; |
| 620 | csum_end += key.offset; |
| 621 | |
| 622 | /* this csum ends before we start, we're done */ |
| 623 | if (csum_end <= bytenr) |
| 624 | break; |
| 625 | |
| 626 | /* delete the entire item, it is inside our range */ |
| 627 | if (key.offset >= bytenr && csum_end <= end_byte) { |
Filipe Manana | 6f54621 | 2017-01-28 01:47:56 +0000 | [diff] [blame] | 628 | int del_nr = 1; |
| 629 | |
| 630 | /* |
| 631 | * Check how many csum items preceding this one in this |
| 632 | * leaf correspond to our range and then delete them all |
| 633 | * at once. |
| 634 | */ |
| 635 | if (key.offset > bytenr && path->slots[0] > 0) { |
| 636 | int slot = path->slots[0] - 1; |
| 637 | |
| 638 | while (slot >= 0) { |
| 639 | struct btrfs_key pk; |
| 640 | |
| 641 | btrfs_item_key_to_cpu(leaf, &pk, slot); |
| 642 | if (pk.offset < bytenr || |
| 643 | pk.type != BTRFS_EXTENT_CSUM_KEY || |
| 644 | pk.objectid != |
| 645 | BTRFS_EXTENT_CSUM_OBJECTID) |
| 646 | break; |
| 647 | path->slots[0] = slot; |
| 648 | del_nr++; |
| 649 | key.offset = pk.offset; |
| 650 | slot--; |
| 651 | } |
| 652 | } |
| 653 | ret = btrfs_del_items(trans, root, path, |
| 654 | path->slots[0], del_nr); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 655 | if (ret) |
| 656 | goto out; |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 657 | if (key.offset == bytenr) |
| 658 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 659 | } else if (key.offset < bytenr && csum_end > end_byte) { |
| 660 | unsigned long offset; |
| 661 | unsigned long shift_len; |
| 662 | unsigned long item_offset; |
| 663 | /* |
| 664 | * [ bytenr - len ] |
| 665 | * [csum ] |
| 666 | * |
| 667 | * Our bytes are in the middle of the csum, |
| 668 | * we need to split this item and insert a new one. |
| 669 | * |
| 670 | * But we can't drop the path because the |
| 671 | * csum could change, get removed, extended etc. |
| 672 | * |
| 673 | * The trick here is the max size of a csum item leaves |
| 674 | * enough room in the tree block for a single |
| 675 | * item header. So, we split the item in place, |
| 676 | * adding a new header pointing to the existing |
| 677 | * bytes. Then we loop around again and we have |
| 678 | * a nicely formed csum item that we can neatly |
| 679 | * truncate. |
| 680 | */ |
| 681 | offset = (bytenr - key.offset) >> blocksize_bits; |
| 682 | offset *= csum_size; |
| 683 | |
| 684 | shift_len = (len >> blocksize_bits) * csum_size; |
| 685 | |
| 686 | item_offset = btrfs_item_ptr_offset(leaf, |
| 687 | path->slots[0]); |
| 688 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 689 | memzero_extent_buffer(leaf, item_offset + offset, |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 690 | shift_len); |
| 691 | key.offset = bytenr; |
| 692 | |
| 693 | /* |
| 694 | * btrfs_split_item returns -EAGAIN when the |
| 695 | * item changed size or key |
| 696 | */ |
| 697 | ret = btrfs_split_item(trans, root, path, &key, offset); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 698 | if (ret && ret != -EAGAIN) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 699 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 700 | goto out; |
| 701 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 702 | |
| 703 | key.offset = end_byte - 1; |
| 704 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 705 | truncate_one_csum(fs_info, path, &key, bytenr, len); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 706 | if (key.offset < bytenr) |
| 707 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 708 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 709 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 710 | } |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 711 | ret = 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 712 | out: |
| 713 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 714 | return ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 715 | } |
| 716 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 717 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 718 | struct btrfs_root *root, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 719 | struct btrfs_ordered_sum *sums) |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 720 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 721 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 722 | struct btrfs_key file_key; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 723 | struct btrfs_key found_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 724 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 725 | struct btrfs_csum_item *item; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 726 | struct btrfs_csum_item *item_end; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 727 | struct extent_buffer *leaf = NULL; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 728 | u64 next_offset; |
| 729 | u64 total_bytes = 0; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 730 | u64 csum_offset; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 731 | u64 bytenr; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 732 | u32 nritems; |
| 733 | u32 ins_size; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 734 | int index = 0; |
| 735 | int found_next; |
| 736 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 737 | u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 738 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 739 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 740 | if (!path) |
| 741 | return -ENOMEM; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 742 | again: |
| 743 | next_offset = (u64)-1; |
| 744 | found_next = 0; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 745 | bytenr = sums->bytenr + total_bytes; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 746 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 747 | file_key.offset = bytenr; |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 748 | file_key.type = BTRFS_EXTENT_CSUM_KEY; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 749 | |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 750 | item = btrfs_lookup_csum(trans, root, path, bytenr, 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 751 | if (!IS_ERR(item)) { |
Chris Mason | 639cb58 | 2008-08-28 06:15:25 -0400 | [diff] [blame] | 752 | ret = 0; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 753 | leaf = path->nodes[0]; |
| 754 | item_end = btrfs_item_ptr(leaf, path->slots[0], |
| 755 | struct btrfs_csum_item); |
| 756 | item_end = (struct btrfs_csum_item *)((char *)item_end + |
| 757 | btrfs_item_size_nr(leaf, path->slots[0])); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 758 | goto found; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 759 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 760 | ret = PTR_ERR(item); |
Yan, Zheng | 4a500fd | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 761 | if (ret != -EFBIG && ret != -ENOENT) |
| 762 | goto fail_unlock; |
| 763 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 764 | if (ret == -EFBIG) { |
| 765 | u32 item_size; |
| 766 | /* we found one, but it isn't big enough yet */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 767 | leaf = path->nodes[0]; |
| 768 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 769 | if ((item_size / csum_size) >= |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 770 | MAX_CSUM_ITEMS(fs_info, csum_size)) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 771 | /* already at max size, make a new one */ |
| 772 | goto insert; |
| 773 | } |
| 774 | } else { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 775 | int slot = path->slots[0] + 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 776 | /* we didn't find a csum item, insert one */ |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 777 | nritems = btrfs_header_nritems(path->nodes[0]); |
Filipe Manana | 35045bf | 2014-04-09 14:38:34 +0100 | [diff] [blame] | 778 | if (!nritems || (path->slots[0] >= nritems - 1)) { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 779 | ret = btrfs_next_leaf(root, path); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 780 | if (ret == 1) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 781 | found_next = 1; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 782 | if (ret != 0) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 783 | goto insert; |
Filipe Manana | 27b9a81 | 2014-08-09 21:22:27 +0100 | [diff] [blame] | 784 | slot = path->slots[0]; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 785 | } |
| 786 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 787 | if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
| 788 | found_key.type != BTRFS_EXTENT_CSUM_KEY) { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 789 | found_next = 1; |
| 790 | goto insert; |
| 791 | } |
| 792 | next_offset = found_key.offset; |
| 793 | found_next = 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 794 | goto insert; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * at this point, we know the tree has an item, but it isn't big |
| 799 | * enough yet to put our csum in. Grow it |
| 800 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 801 | btrfs_release_path(path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 802 | ret = btrfs_search_slot(trans, root, &file_key, path, |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 803 | csum_size, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 804 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 805 | goto fail_unlock; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 806 | |
| 807 | if (ret > 0) { |
| 808 | if (path->slots[0] == 0) |
| 809 | goto insert; |
| 810 | path->slots[0]--; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 811 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 812 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 813 | leaf = path->nodes[0]; |
| 814 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 815 | csum_offset = (bytenr - found_key.offset) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 816 | fs_info->sb->s_blocksize_bits; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 817 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 818 | if (found_key.type != BTRFS_EXTENT_CSUM_KEY || |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 819 | found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 820 | csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 821 | goto insert; |
| 822 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 823 | |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 824 | if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) / |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 825 | csum_size) { |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 826 | int extend_nr; |
| 827 | u64 tmp; |
| 828 | u32 diff; |
| 829 | u32 free_space; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 830 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 831 | if (btrfs_leaf_free_space(leaf) < |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 832 | sizeof(struct btrfs_item) + csum_size * 2) |
| 833 | goto insert; |
| 834 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 835 | free_space = btrfs_leaf_free_space(leaf) - |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 836 | sizeof(struct btrfs_item) - csum_size; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 837 | tmp = sums->len - total_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 838 | tmp >>= fs_info->sb->s_blocksize_bits; |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 839 | WARN_ON(tmp < 1); |
| 840 | |
| 841 | extend_nr = max_t(int, 1, (int)tmp); |
| 842 | diff = (csum_offset + extend_nr) * csum_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 843 | diff = min(diff, |
| 844 | MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 845 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 846 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 847 | diff = min(free_space, diff); |
| 848 | diff /= csum_size; |
| 849 | diff *= csum_size; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 850 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 851 | btrfs_extend_item(fs_info, path, diff); |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 852 | ret = 0; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 853 | goto csum; |
| 854 | } |
| 855 | |
| 856 | insert: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 857 | btrfs_release_path(path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 858 | csum_offset = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 859 | if (found_next) { |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 860 | u64 tmp; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 861 | |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 862 | tmp = sums->len - total_bytes; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 863 | tmp >>= fs_info->sb->s_blocksize_bits; |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 864 | tmp = min(tmp, (next_offset - file_key.offset) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 865 | fs_info->sb->s_blocksize_bits); |
Liu Bo | 2f697dc | 2013-02-04 13:12:18 +0000 | [diff] [blame] | 866 | |
Seraphime Kirkovski | 50d0446 | 2016-12-15 14:38:28 +0100 | [diff] [blame] | 867 | tmp = max_t(u64, 1, tmp); |
| 868 | tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size)); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 869 | ins_size = csum_size * tmp; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 870 | } else { |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 871 | ins_size = csum_size; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 872 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 873 | path->leave_spinning = 1; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 874 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 875 | ins_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 876 | path->leave_spinning = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 877 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 878 | goto fail_unlock; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 879 | if (WARN_ON(ret != 0)) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 880 | goto fail_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 881 | leaf = path->nodes[0]; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 882 | csum: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 883 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 884 | item_end = (struct btrfs_csum_item *)((unsigned char *)item + |
| 885 | btrfs_item_size_nr(leaf, path->slots[0])); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 886 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 887 | csum_offset * csum_size); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 888 | found: |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 889 | ins_size = (u32)(sums->len - total_bytes) >> |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 890 | fs_info->sb->s_blocksize_bits; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 891 | ins_size *= csum_size; |
| 892 | ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, |
| 893 | ins_size); |
| 894 | write_extent_buffer(leaf, sums->sums + index, (unsigned long)item, |
| 895 | ins_size); |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 896 | |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 897 | ins_size /= csum_size; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 898 | total_bytes += ins_size * fs_info->sectorsize; |
Miao Xie | f51a4a1 | 2013-06-19 10:36:09 +0800 | [diff] [blame] | 899 | index += ins_size; |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 900 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 901 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 902 | if (total_bytes < sums->len) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 903 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 904 | cond_resched(); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 905 | goto again; |
| 906 | } |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 907 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 908 | btrfs_free_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 909 | return ret; |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 910 | |
| 911 | fail_unlock: |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 912 | goto out; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 913 | } |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 914 | |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 915 | void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 916 | const struct btrfs_path *path, |
| 917 | struct btrfs_file_extent_item *fi, |
| 918 | const bool new_inline, |
| 919 | struct extent_map *em) |
| 920 | { |
David Sterba | 3ffbd68 | 2018-06-29 10:56:42 +0200 | [diff] [blame] | 921 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 922 | struct btrfs_root *root = inode->root; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 923 | struct extent_buffer *leaf = path->nodes[0]; |
| 924 | const int slot = path->slots[0]; |
| 925 | struct btrfs_key key; |
| 926 | u64 extent_start, extent_end; |
| 927 | u64 bytenr; |
| 928 | u8 type = btrfs_file_extent_type(leaf, fi); |
| 929 | int compress_type = btrfs_file_extent_compression(leaf, fi); |
| 930 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 931 | em->bdev = fs_info->fs_devices->latest_bdev; |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 932 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 933 | extent_start = key.offset; |
| 934 | |
| 935 | if (type == BTRFS_FILE_EXTENT_REG || |
| 936 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 937 | extent_end = extent_start + |
| 938 | btrfs_file_extent_num_bytes(leaf, fi); |
| 939 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 940 | size_t size; |
Qu Wenruo | e41ca58 | 2018-06-06 15:41:49 +0800 | [diff] [blame] | 941 | size = btrfs_file_extent_ram_bytes(leaf, fi); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 942 | extent_end = ALIGN(extent_start + size, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 943 | fs_info->sectorsize); |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 947 | if (type == BTRFS_FILE_EXTENT_REG || |
| 948 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 949 | em->start = extent_start; |
| 950 | em->len = extent_end - extent_start; |
| 951 | em->orig_start = extent_start - |
| 952 | btrfs_file_extent_offset(leaf, fi); |
| 953 | em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 954 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 955 | if (bytenr == 0) { |
| 956 | em->block_start = EXTENT_MAP_HOLE; |
| 957 | return; |
| 958 | } |
| 959 | if (compress_type != BTRFS_COMPRESS_NONE) { |
| 960 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 961 | em->compress_type = compress_type; |
| 962 | em->block_start = bytenr; |
| 963 | em->block_len = em->orig_block_len; |
| 964 | } else { |
| 965 | bytenr += btrfs_file_extent_offset(leaf, fi); |
| 966 | em->block_start = bytenr; |
| 967 | em->block_len = em->len; |
| 968 | if (type == BTRFS_FILE_EXTENT_PREALLOC) |
| 969 | set_bit(EXTENT_FLAG_PREALLOC, &em->flags); |
| 970 | } |
| 971 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
| 972 | em->block_start = EXTENT_MAP_INLINE; |
| 973 | em->start = extent_start; |
| 974 | em->len = extent_end - extent_start; |
| 975 | /* |
| 976 | * Initialize orig_start and block_len with the same values |
| 977 | * as in inode.c:btrfs_get_extent(). |
| 978 | */ |
| 979 | em->orig_start = EXTENT_MAP_HOLE; |
| 980 | em->block_len = (u64)-1; |
| 981 | if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) { |
| 982 | set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
| 983 | em->compress_type = compress_type; |
| 984 | } |
| 985 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 986 | btrfs_err(fs_info, |
Nikolay Borisov | 9cdc512 | 2017-02-20 13:51:02 +0200 | [diff] [blame] | 987 | "unknown file extent item type %d, inode %llu, offset %llu, " |
| 988 | "root %llu", type, btrfs_ino(inode), extent_start, |
Filipe Manana | 7ffbb59 | 2014-06-09 03:48:05 +0100 | [diff] [blame] | 989 | root->root_key.objectid); |
| 990 | } |
| 991 | } |