blob: 9f6ceb8eb45114ddf70c36c4557881994f3ae85f [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason065631f2008-02-20 12:07:25 -05006#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/pagemap.h>
9#include <linux/highmem.h>
Nikolay Borisova3d46ae2019-04-01 11:29:58 +030010#include <linux/sched/mm.h>
Chris Mason1e1d2702007-03-15 19:03:33 -040011#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -040012#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040013#include "transaction.h"
Miao Xiefacc8a222013-07-25 19:22:34 +080014#include "volumes.h"
Chris Mason1de037a2007-05-29 15:17:08 -040015#include "print-tree.h"
Anand Jainebb87652016-03-10 17:26:59 +080016#include "compression.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040017
Chris Mason42049bf2016-08-03 14:05:46 -070018#define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
19 sizeof(struct btrfs_item) * 2) / \
20 size) - 1))
Yan Zheng07d400a2009-01-06 11:42:00 -050021
Zach Brown221b8312012-09-20 14:33:00 -060022#define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030023 PAGE_SIZE))
Chris Mason7ca4be42012-01-31 20:19:02 -050024
Jeff Mahoneyda170662016-06-15 09:22:56 -040025#define MAX_ORDERED_SUM_BYTES(fs_info) ((PAGE_SIZE - \
Yan Zheng07d400a2009-01-06 11:42:00 -050026 sizeof(struct btrfs_ordered_sum)) / \
Jeff Mahoneyda170662016-06-15 09:22:56 -040027 sizeof(u32) * (fs_info)->sectorsize)
Yan Zheng07d400a2009-01-06 11:42:00 -050028
Chris Masonb18c6682007-04-17 13:26:50 -040029int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Sage Weilf2eb0a22008-05-02 14:43:14 -040030 struct btrfs_root *root,
31 u64 objectid, u64 pos,
32 u64 disk_offset, u64 disk_num_bytes,
Chris Masonc8b97812008-10-29 14:49:59 -040033 u64 num_bytes, u64 offset, u64 ram_bytes,
34 u8 compression, u8 encryption, u16 other_encoding)
Chris Mason9f5fae22007-03-20 14:38:32 -040035{
Chris Masondee26a92007-03-26 16:00:06 -040036 int ret = 0;
37 struct btrfs_file_extent_item *item;
38 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040039 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masondee26a92007-03-26 16:00:06 -040041
Chris Mason5caf2a02007-04-02 11:20:42 -040042 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +000043 if (!path)
44 return -ENOMEM;
Chris Masondee26a92007-03-26 16:00:06 -040045 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040046 file_key.offset = pos;
David Sterba962a2982014-06-04 18:41:45 +020047 file_key.type = BTRFS_EXTENT_DATA_KEY;
Chris Masondee26a92007-03-26 16:00:06 -040048
Chris Masonb9473432009-03-13 11:00:37 -040049 path->leave_spinning = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -040050 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040051 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040052 if (ret < 0)
53 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +010054 BUG_ON(ret); /* Can't happen */
Chris Mason5f39d392007-10-15 16:14:19 -040055 leaf = path->nodes[0];
56 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040057 struct btrfs_file_extent_item);
Sage Weilf2eb0a22008-05-02 14:43:14 -040058 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
Chris Masondb945352007-10-15 16:15:53 -040059 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
Sage Weilf2eb0a22008-05-02 14:43:14 -040060 btrfs_set_file_extent_offset(leaf, item, offset);
Chris Masondb945352007-10-15 16:15:53 -040061 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -040062 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040063 btrfs_set_file_extent_generation(leaf, item, trans->transid);
64 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
Chris Masonc8b97812008-10-29 14:49:59 -040065 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 Mason5f39d392007-10-15 16:14:19 -040069 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -040070out:
Chris Mason5caf2a02007-04-02 11:20:42 -040071 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040072 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040073}
Chris Masondee26a92007-03-26 16:00:06 -040074
Eric Sandeen48a3b632013-04-25 20:41:01 +000075static struct btrfs_csum_item *
76btrfs_lookup_csum(struct btrfs_trans_handle *trans,
77 struct btrfs_root *root,
78 struct btrfs_path *path,
79 u64 bytenr, int cow)
Chris Mason6567e832007-04-16 09:22:45 -040080{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040081 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6567e832007-04-16 09:22:45 -040082 int ret;
83 struct btrfs_key file_key;
84 struct btrfs_key found_key;
85 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040086 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040087 u64 csum_offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040088 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Masona429e512007-04-18 16:15:28 -040089 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040090
Chris Masond20f7042008-12-08 16:58:54 -050091 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
92 file_key.offset = bytenr;
David Sterba962a2982014-06-04 18:41:45 +020093 file_key.type = BTRFS_EXTENT_CSUM_KEY;
Chris Masonb18c6682007-04-17 13:26:50 -040094 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040095 if (ret < 0)
96 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -040097 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -040098 if (ret > 0) {
99 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400100 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -0400101 goto fail;
102 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400103 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +0200104 if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
Chris Mason6567e832007-04-16 09:22:45 -0400105 goto fail;
Chris Masond20f7042008-12-08 16:58:54 -0500106
107 csum_offset = (bytenr - found_key.offset) >>
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400108 fs_info->sb->s_blocksize_bits;
Chris Mason5f39d392007-10-15 16:14:19 -0400109 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500110 csums_in_item /= csum_size;
Chris Masona429e512007-04-18 16:15:28 -0400111
Miao Xie82d130f2013-03-28 08:12:15 +0000112 if (csum_offset == csums_in_item) {
Chris Masona429e512007-04-18 16:15:28 -0400113 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400114 goto fail;
Miao Xie82d130f2013-03-28 08:12:15 +0000115 } else if (csum_offset > csums_in_item) {
116 goto fail;
Chris Mason6567e832007-04-16 09:22:45 -0400117 }
118 }
119 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400120 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500121 csum_offset * csum_size);
Chris Mason6567e832007-04-16 09:22:45 -0400122 return item;
123fail:
124 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400125 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400126 return ERR_PTR(ret);
127}
128
Chris Masondee26a92007-03-26 16:00:06 -0400129int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
130 struct btrfs_root *root,
131 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400132 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400133{
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 Mason70b2bef2007-04-17 15:39:32 -0400140 file_key.offset = offset;
David Sterba962a2982014-06-04 18:41:45 +0200141 file_key.type = BTRFS_EXTENT_DATA_KEY;
Chris Masondee26a92007-03-26 16:00:06 -0400142 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
143 return ret;
144}
Chris Masonf254e522007-03-29 15:15:27 -0400145
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200146static blk_status_t __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400147 u64 logical_offset, u32 *dst, int dio)
Chris Mason61b49442008-07-31 15:42:53 -0400148{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400149 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo17347ce2017-05-15 15:33:27 -0700150 struct bio_vec bvec;
151 struct bvec_iter iter;
Miao Xiefacc8a222013-07-25 19:22:34 +0800152 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 Bacik4b46fce2010-05-23 11:00:55 -0400157 u64 offset = 0;
Chris Mason61b49442008-07-31 15:42:53 -0400158 u64 item_start_offset = 0;
159 u64 item_last_offset = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500160 u64 disk_bytenr;
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530161 u64 page_bytes_left;
Chris Mason61b49442008-07-31 15:42:53 -0400162 u32 diff;
Miao Xiefacc8a222013-07-25 19:22:34 +0800163 int nblocks;
Liu Bo17347ce2017-05-15 15:33:27 -0700164 int count = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400165 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Mason61b49442008-07-31 15:42:53 -0400166
167 path = btrfs_alloc_path();
Tsutomu Itohc2db1072011-03-01 06:48:31 +0000168 if (!path)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200169 return BLK_STS_RESOURCE;
Miao Xiefacc8a222013-07-25 19:22:34 +0800170
Kent Overstreet4f024f32013-10-11 15:44:27 -0700171 nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
Miao Xiefacc8a222013-07-25 19:22:34 +0800172 if (!dst) {
173 if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
David Sterba31feccc2018-11-22 17:16:46 +0100174 btrfs_bio->csum = kmalloc_array(nblocks, csum_size,
175 GFP_NOFS);
176 if (!btrfs_bio->csum) {
Miao Xiefacc8a222013-07-25 19:22:34 +0800177 btrfs_free_path(path);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200178 return BLK_STS_RESOURCE;
Miao Xiefacc8a222013-07-25 19:22:34 +0800179 }
Miao Xiefacc8a222013-07-25 19:22:34 +0800180 } 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. Shutemov09cbfea2016-04-01 15:29:47 +0300188 if (bio->bi_iter.bi_size > PAGE_SIZE * 8)
David Sterbae4058b52015-11-27 16:31:35 +0100189 path->reada = READA_FORWARD;
Chris Mason61b49442008-07-31 15:42:53 -0400190
Chris Mason2cf85722011-07-26 15:35:09 -0400191 /*
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 Borisov70ddc552017-02-20 13:50:35 +0200197 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Chris Mason2cf85722011-07-26 15:35:09 -0400198 path->search_commit_root = 1;
Josef Bacikddf23b32011-09-11 10:52:24 -0400199 path->skip_locking = 1;
200 }
Chris Mason2cf85722011-07-26 15:35:09 -0400201
Kent Overstreet4f024f32013-10-11 15:44:27 -0700202 disk_bytenr = (u64)bio->bi_iter.bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400203 if (dio)
204 offset = logical_offset;
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530205
Liu Bo17347ce2017-05-15 15:33:27 -0700206 bio_for_each_segment(bvec, bio, iter) {
207 page_bytes_left = bvec.bv_len;
Christoph Hellwig4989d272016-11-25 09:07:52 +0100208 if (count)
209 goto next;
210
Josef Bacik4b46fce2010-05-23 11:00:55 -0400211 if (!dio)
Liu Bo17347ce2017-05-15 15:33:27 -0700212 offset = page_offset(bvec.bv_page) + bvec.bv_offset;
Miao Xiefacc8a222013-07-25 19:22:34 +0800213 count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
214 (u32 *)csum, nblocks);
Miao Xiee4100d92013-04-05 07:20:56 +0000215 if (count)
Chris Mason61b49442008-07-31 15:42:53 -0400216 goto found;
217
Chris Masond20f7042008-12-08 16:58:54 -0500218 if (!item || disk_bytenr < item_start_offset ||
219 disk_bytenr >= item_last_offset) {
Chris Mason61b49442008-07-31 15:42:53 -0400220 struct btrfs_key found_key;
221 u32 item_size;
222
223 if (item)
David Sterbab3b4aa72011-04-21 01:20:15 +0200224 btrfs_release_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400225 item = btrfs_lookup_csum(NULL, fs_info->csum_root,
Chris Masond20f7042008-12-08 16:58:54 -0500226 path, disk_bytenr, 0);
Chris Mason61b49442008-07-31 15:42:53 -0400227 if (IS_ERR(item)) {
Miao Xiee4100d92013-04-05 07:20:56 +0000228 count = 1;
Miao Xiefacc8a222013-07-25 19:22:34 +0800229 memset(csum, 0, csum_size);
Yan Zheng17d217f2008-12-12 10:03:38 -0500230 if (BTRFS_I(inode)->root->root_key.objectid ==
231 BTRFS_DATA_RELOC_TREE_OBJECTID) {
232 set_extent_bits(io_tree, offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400233 offset + fs_info->sectorsize - 1,
David Sterbaceeb0ae2016-04-26 23:54:39 +0200234 EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -0500235 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400236 btrfs_info_rl(fs_info,
Frank Holtonefe120a2013-12-20 11:37:06 -0500237 "no csum found for inode %llu start %llu",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200238 btrfs_ino(BTRFS_I(inode)), offset);
Yan Zheng17d217f2008-12-12 10:03:38 -0500239 }
Chris Mason6dab8152008-08-04 08:35:53 -0400240 item = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +0200241 btrfs_release_path(path);
Chris Mason61b49442008-07-31 15:42:53 -0400242 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 Bacik607d4322008-12-02 07:17:45 -0500251 (item_size / csum_size) *
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400252 fs_info->sectorsize;
Chris Mason61b49442008-07-31 15:42:53 -0400253 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 Masond20f7042008-12-08 16:58:54 -0500260 diff = disk_bytenr - item_start_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400261 diff = diff / fs_info->sectorsize;
Josef Bacik607d4322008-12-02 07:17:45 -0500262 diff = diff * csum_size;
Miao Xiefacc8a222013-07-25 19:22:34 +0800263 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 Mason3de9d6b2008-08-04 23:17:27 -0400266 ((unsigned long)item) + diff,
Miao Xiee4100d92013-04-05 07:20:56 +0000267 csum_size * count);
Chris Mason61b49442008-07-31 15:42:53 -0400268found:
Miao Xiefacc8a222013-07-25 19:22:34 +0800269 csum += count * csum_size;
270 nblocks -= count;
Christoph Hellwig4989d272016-11-25 09:07:52 +0100271next:
Miao Xiee4100d92013-04-05 07:20:56 +0000272 while (count--) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 disk_bytenr += fs_info->sectorsize;
274 offset += fs_info->sectorsize;
275 page_bytes_left -= fs_info->sectorsize;
Christoph Hellwig4989d272016-11-25 09:07:52 +0100276 if (!page_bytes_left)
277 break; /* move to next bio */
Miao Xiee4100d92013-04-05 07:20:56 +0000278 }
Chris Mason61b49442008-07-31 15:42:53 -0400279 }
Chris Mason389f2392016-03-21 06:59:09 -0700280
Christoph Hellwig4989d272016-11-25 09:07:52 +0100281 WARN_ON_ONCE(count);
Chris Mason61b49442008-07-31 15:42:53 -0400282 btrfs_free_path(path);
283 return 0;
284}
285
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200286blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400287{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400288 return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400289}
290
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200291blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400292{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400293 return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400294}
295
Yan Zheng17d217f2008-12-12 10:03:38 -0500296int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
Arne Jansena2de7332011-03-08 14:14:00 +0100297 struct list_head *list, int search_commit)
Yan Zheng17d217f2008-12-12 10:03:38 -0500298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng17d217f2008-12-12 10:03:38 -0500300 struct btrfs_key key;
301 struct btrfs_path *path;
302 struct extent_buffer *leaf;
303 struct btrfs_ordered_sum *sums;
Yan Zheng17d217f2008-12-12 10:03:38 -0500304 struct btrfs_csum_item *item;
Mark Fasheh0678b612011-08-05 15:46:16 -0700305 LIST_HEAD(tmplist);
Yan Zheng17d217f2008-12-12 10:03:38 -0500306 unsigned long offset;
307 int ret;
308 size_t size;
309 u64 csum_end;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400310 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan Zheng17d217f2008-12-12 10:03:38 -0500311
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400312 ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
313 IS_ALIGNED(end + 1, fs_info->sectorsize));
Josef Bacik4277a9c2013-10-15 09:36:40 -0400314
Yan Zheng17d217f2008-12-12 10:03:38 -0500315 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700316 if (!path)
317 return -ENOMEM;
Yan Zheng17d217f2008-12-12 10:03:38 -0500318
Arne Jansena2de7332011-03-08 14:14:00 +0100319 if (search_commit) {
320 path->skip_locking = 1;
David Sterbae4058b52015-11-27 16:31:35 +0100321 path->reada = READA_FORWARD;
Arne Jansena2de7332011-03-08 14:14:00 +0100322 path->search_commit_root = 1;
323 }
324
Yan Zheng17d217f2008-12-12 10:03:38 -0500325 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
326 key.offset = start;
327 key.type = BTRFS_EXTENT_CSUM_KEY;
328
Yan Zheng07d400a2009-01-06 11:42:00 -0500329 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -0500330 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 Mahoney0b246af2016-06-22 18:54:23 -0400338 fs_info->sb->s_blocksize_bits;
Yan Zheng17d217f2008-12-12 10:03:38 -0500339 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 Zheng07d400a2009-01-06 11:42:00 -0500348 ret = btrfs_next_leaf(root, path);
Yan Zheng17d217f2008-12-12 10:03:38 -0500349 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 Wu628c8282013-03-18 09:18:09 +0000358 key.type != BTRFS_EXTENT_CSUM_KEY ||
359 key.offset > end)
Yan Zheng17d217f2008-12-12 10:03:38 -0500360 break;
361
362 if (key.offset > start)
363 start = key.offset;
364
365 size = btrfs_item_size_nr(leaf, path->slots[0]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400366 csum_end = key.offset + (size / csum_size) * fs_info->sectorsize;
Yan Zheng87b29b22008-12-17 10:21:48 -0500367 if (csum_end <= start) {
368 path->slots[0]++;
369 continue;
370 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500371
Yan Zheng07d400a2009-01-06 11:42:00 -0500372 csum_end = min(csum_end, end + 1);
Yan Zheng17d217f2008-12-12 10:03:38 -0500373 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
374 struct btrfs_csum_item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500375 while (start < csum_end) {
376 size = min_t(size_t, csum_end - start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400377 MAX_ORDERED_SUM_BYTES(fs_info));
378 sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
Miao Xief51a4a12013-06-19 10:36:09 +0800379 GFP_NOFS);
Mark Fasheh0678b612011-08-05 15:46:16 -0700380 if (!sums) {
381 ret = -ENOMEM;
382 goto fail;
383 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500384
Yan Zheng07d400a2009-01-06 11:42:00 -0500385 sums->bytenr = start;
Miao Xief51a4a12013-06-19 10:36:09 +0800386 sums->len = (int)size;
Yan Zheng07d400a2009-01-06 11:42:00 -0500387
388 offset = (start - key.offset) >>
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400389 fs_info->sb->s_blocksize_bits;
Yan Zheng07d400a2009-01-06 11:42:00 -0500390 offset *= csum_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400391 size >>= fs_info->sb->s_blocksize_bits;
Yan Zheng07d400a2009-01-06 11:42:00 -0500392
Miao Xief51a4a12013-06-19 10:36:09 +0800393 read_extent_buffer(path->nodes[0],
394 sums->sums,
395 ((unsigned long)item) + offset,
396 csum_size * size);
Yan Zheng07d400a2009-01-06 11:42:00 -0500397
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400398 start += fs_info->sectorsize * size;
Mark Fasheh0678b612011-08-05 15:46:16 -0700399 list_add_tail(&sums->list, &tmplist);
Yan Zheng17d217f2008-12-12 10:03:38 -0500400 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500401 path->slots[0]++;
402 }
403 ret = 0;
404fail:
Mark Fasheh0678b612011-08-05 15:46:16 -0700405 while (ret < 0 && !list_empty(&tmplist)) {
Chris Mason6e5aafb2014-11-04 06:59:04 -0800406 sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
Mark Fasheh0678b612011-08-05 15:46:16 -0700407 list_del(&sums->list);
408 kfree(sums);
409 }
410 list_splice_tail(&tmplist, list);
411
Yan Zheng17d217f2008-12-12 10:03:38 -0500412 btrfs_free_path(path);
413 return ret;
414}
415
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200416blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400417 u64 file_start, int contig)
Chris Masone0156402008-04-16 11:15:20 -0400418{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400419 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 struct btrfs_ordered_sum *sums;
Christoph Hellwig6cd7ce42016-11-25 09:07:49 +0100421 struct btrfs_ordered_extent *ordered = NULL;
Chris Masone0156402008-04-16 11:15:20 -0400422 char *data;
Liu Bo17347ce2017-05-15 15:33:27 -0700423 struct bvec_iter iter;
424 struct bio_vec bvec;
Miao Xief51a4a12013-06-19 10:36:09 +0800425 int index;
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530426 int nr_sectors;
Chris Mason3edf7d32008-07-18 06:17:13 -0400427 unsigned long total_bytes = 0;
428 unsigned long this_sum_bytes = 0;
Liu Bo17347ce2017-05-15 15:33:27 -0700429 int i;
Chris Mason3edf7d32008-07-18 06:17:13 -0400430 u64 offset;
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300431 unsigned nofs_flag;
Chris Masone0156402008-04-16 11:15:20 -0400432
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300433 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 Masone0156402008-04-16 11:15:20 -0400438 if (!sums)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200439 return BLK_STS_RESOURCE;
Chris Mason3edf7d32008-07-18 06:17:13 -0400440
Kent Overstreet4f024f32013-10-11 15:44:27 -0700441 sums->len = bio->bi_iter.bi_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400442 INIT_LIST_HEAD(&sums->list);
Chris Masond20f7042008-12-08 16:58:54 -0500443
444 if (contig)
445 offset = file_start;
446 else
Christoph Hellwig6cd7ce42016-11-25 09:07:49 +0100447 offset = 0; /* shut up gcc */
Chris Masond20f7042008-12-08 16:58:54 -0500448
Kent Overstreet4f024f32013-10-11 15:44:27 -0700449 sums->bytenr = (u64)bio->bi_iter.bi_sector << 9;
Miao Xief51a4a12013-06-19 10:36:09 +0800450 index = 0;
Chris Masone0156402008-04-16 11:15:20 -0400451
Liu Bo17347ce2017-05-15 15:33:27 -0700452 bio_for_each_segment(bvec, bio, iter) {
Chris Masond20f7042008-12-08 16:58:54 -0500453 if (!contig)
Liu Bo17347ce2017-05-15 15:33:27 -0700454 offset = page_offset(bvec.bv_page) + bvec.bv_offset;
Chris Masond20f7042008-12-08 16:58:54 -0500455
Christoph Hellwig6cd7ce42016-11-25 09:07:49 +0100456 if (!ordered) {
457 ordered = btrfs_lookup_ordered_extent(inode, offset);
458 BUG_ON(!ordered); /* Logic error */
459 }
460
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info,
Liu Bo17347ce2017-05-15 15:33:27 -0700462 bvec.bv_len + fs_info->sectorsize
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400463 - 1);
Chris Mason3edf7d32008-07-18 06:17:13 -0400464
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530465 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 Rajendrac40a3d32016-01-21 15:55:54 +0530470 sums->len = this_sum_bytes;
471 this_sum_bytes = 0;
Nikolay Borisovf9756262019-04-10 16:16:11 +0300472 btrfs_add_ordered_sum(ordered, sums);
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530473 btrfs_put_ordered_extent(ordered);
474
475 bytes_left = bio->bi_iter.bi_size - total_bytes;
476
Nikolay Borisova3d46ae2019-04-01 11:29:58 +0300477 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 Rajendrac40a3d32016-01-21 15:55:54 +0530481 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 Rajendrac40a3d32016-01-21 15:55:54 +0530489 }
490
491 sums->sums[index] = ~(u32)0;
Johannes Thumshirn443c8e22019-03-07 17:14:00 +0100492 data = kmap_atomic(bvec.bv_page);
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530493 sums->sums[index]
Liu Bo17347ce2017-05-15 15:33:27 -0700494 = btrfs_csum_data(data + bvec.bv_offset
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400495 + (i * fs_info->sectorsize),
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530496 sums->sums[index],
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400497 fs_info->sectorsize);
Johannes Thumshirn443c8e22019-03-07 17:14:00 +0100498 kunmap_atomic(data);
Chandan Rajendrac40a3d32016-01-21 15:55:54 +0530499 btrfs_csum_final(sums->sums[index],
500 (char *)(sums->sums + index));
501 index++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400502 offset += fs_info->sectorsize;
503 this_sum_bytes += fs_info->sectorsize;
504 total_bytes += fs_info->sectorsize;
Chris Mason3edf7d32008-07-18 06:17:13 -0400505 }
506
Chris Masone0156402008-04-16 11:15:20 -0400507 }
Chris Masoned98b562008-07-22 23:06:42 -0400508 this_sum_bytes = 0;
Nikolay Borisovf9756262019-04-10 16:16:11 +0300509 btrfs_add_ordered_sum(ordered, sums);
Chris Mason3edf7d32008-07-18 06:17:13 -0400510 btrfs_put_ordered_extent(ordered);
Chris Masone0156402008-04-16 11:15:20 -0400511 return 0;
512}
513
Chris Mason459931e2008-12-10 09:10:46 -0500514/*
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 Mahoney2ff7e612016-06-22 18:54:24 -0400525static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100526 struct btrfs_path *path,
527 struct btrfs_key *key,
528 u64 bytenr, u64 len)
Chris Mason459931e2008-12-10 09:10:46 -0500529{
530 struct extent_buffer *leaf;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400531 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Mason459931e2008-12-10 09:10:46 -0500532 u64 csum_end;
533 u64 end_byte = bytenr + len;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400534 u32 blocksize_bits = fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500535
536 leaf = path->nodes[0];
537 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400538 csum_end <<= fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500539 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 Sterba78ac4f92019-03-20 14:49:12 +0100550 btrfs_truncate_item(path, new_size, 1);
Chris Mason459931e2008-12-10 09:10:46 -0500551 } 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 Sterba78ac4f92019-03-20 14:49:12 +0100562 btrfs_truncate_item(path, new_size, 0);
Chris Mason459931e2008-12-10 09:10:46 -0500563
564 key->offset = end_byte;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400565 btrfs_set_item_key_safe(fs_info, path, key);
Chris Mason459931e2008-12-10 09:10:46 -0500566 } else {
567 BUG();
568 }
Chris Mason459931e2008-12-10 09:10:46 -0500569}
570
571/*
572 * deletes the csum items from the csum tree for a given
573 * range of bytes.
574 */
575int btrfs_del_csums(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400576 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len)
Chris Mason459931e2008-12-10 09:10:46 -0500577{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -0400578 struct btrfs_root *root = fs_info->csum_root;
Chris Mason459931e2008-12-10 09:10:46 -0500579 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 Mahoney0b246af2016-06-22 18:54:23 -0400585 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
586 int blocksize_bits = fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500587
Chris Mason459931e2008-12-10 09:10:46 -0500588 path = btrfs_alloc_path();
liubo2a29edc2011-01-26 06:22:08 +0000589 if (!path)
590 return -ENOMEM;
Chris Mason459931e2008-12-10 09:10:46 -0500591
Chris Masond3977122009-01-05 21:25:51 -0500592 while (1) {
Chris Mason459931e2008-12-10 09:10:46 -0500593 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
594 key.offset = end_byte - 1;
595 key.type = BTRFS_EXTENT_CSUM_KEY;
596
Chris Masonb9473432009-03-13 11:00:37 -0400597 path->leave_spinning = 1;
Chris Mason459931e2008-12-10 09:10:46 -0500598 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
599 if (ret > 0) {
600 if (path->slots[0] == 0)
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000601 break;
Chris Mason459931e2008-12-10 09:10:46 -0500602 path->slots[0]--;
Josef Bacikad0397a2011-01-28 18:44:44 +0000603 } else if (ret < 0) {
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000604 break;
Chris Mason459931e2008-12-10 09:10:46 -0500605 }
Josef Bacikad0397a2011-01-28 18:44:44 +0000606
Chris Mason459931e2008-12-10 09:10:46 -0500607 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 Manana6f546212017-01-28 01:47:56 +0000628 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 Itoh65a246c2011-05-19 04:37:44 +0000655 if (ret)
656 goto out;
Chris Masondcbdd4d2008-12-16 13:51:01 -0500657 if (key.offset == bytenr)
658 break;
Chris Mason459931e2008-12-10 09:10:46 -0500659 } 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 Sterbab159fa22016-11-08 18:09:03 +0100689 memzero_extent_buffer(leaf, item_offset + offset,
Chris Mason459931e2008-12-10 09:10:46 -0500690 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 Mahoney79787ea2012-03-12 16:03:00 +0100698 if (ret && ret != -EAGAIN) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400699 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100700 goto out;
701 }
Chris Mason459931e2008-12-10 09:10:46 -0500702
703 key.offset = end_byte - 1;
704 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400705 truncate_one_csum(fs_info, path, &key, bytenr, len);
Chris Masondcbdd4d2008-12-16 13:51:01 -0500706 if (key.offset < bytenr)
707 break;
Chris Mason459931e2008-12-10 09:10:46 -0500708 }
David Sterbab3b4aa72011-04-21 01:20:15 +0200709 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -0500710 }
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000711 ret = 0;
Chris Mason459931e2008-12-10 09:10:46 -0500712out:
713 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +0000714 return ret;
Chris Mason459931e2008-12-10 09:10:46 -0500715}
716
Chris Mason065631f2008-02-20 12:07:25 -0500717int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
Chris Masond20f7042008-12-08 16:58:54 -0500718 struct btrfs_root *root,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400719 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonf254e522007-03-29 15:15:27 -0400722 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400723 struct btrfs_key found_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400724 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400725 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500726 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400727 struct extent_buffer *leaf = NULL;
Miao Xief51a4a12013-06-19 10:36:09 +0800728 u64 next_offset;
729 u64 total_bytes = 0;
Chris Mason6567e832007-04-16 09:22:45 -0400730 u64 csum_offset;
Miao Xief51a4a12013-06-19 10:36:09 +0800731 u64 bytenr;
Chris Masonf578d4b2007-10-25 15:42:56 -0400732 u32 nritems;
733 u32 ins_size;
Miao Xief51a4a12013-06-19 10:36:09 +0800734 int index = 0;
735 int found_next;
736 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400737 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500738
Chris Mason5caf2a02007-04-02 11:20:42 -0400739 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700740 if (!path)
741 return -ENOMEM;
Chris Mason065631f2008-02-20 12:07:25 -0500742again:
743 next_offset = (u64)-1;
744 found_next = 0;
Miao Xief51a4a12013-06-19 10:36:09 +0800745 bytenr = sums->bytenr + total_bytes;
Chris Masond20f7042008-12-08 16:58:54 -0500746 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
Miao Xief51a4a12013-06-19 10:36:09 +0800747 file_key.offset = bytenr;
David Sterba962a2982014-06-04 18:41:45 +0200748 file_key.type = BTRFS_EXTENT_CSUM_KEY;
Chris Masona429e512007-04-18 16:15:28 -0400749
Miao Xief51a4a12013-06-19 10:36:09 +0800750 item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400751 if (!IS_ERR(item)) {
Chris Mason639cb582008-08-28 06:15:25 -0400752 ret = 0;
Miao Xief51a4a12013-06-19 10:36:09 +0800753 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 Masona429e512007-04-18 16:15:28 -0400758 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400759 }
Chris Masona429e512007-04-18 16:15:28 -0400760 ret = PTR_ERR(item);
Yan, Zheng4a500fd2010-05-16 10:49:59 -0400761 if (ret != -EFBIG && ret != -ENOENT)
762 goto fail_unlock;
763
Chris Masona429e512007-04-18 16:15:28 -0400764 if (ret == -EFBIG) {
765 u32 item_size;
766 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400767 leaf = path->nodes[0];
768 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500769 if ((item_size / csum_size) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400770 MAX_CSUM_ITEMS(fs_info, csum_size)) {
Chris Masona429e512007-04-18 16:15:28 -0400771 /* already at max size, make a new one */
772 goto insert;
773 }
774 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400775 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400776 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400777 nritems = btrfs_header_nritems(path->nodes[0]);
Filipe Manana35045bf2014-04-09 14:38:34 +0100778 if (!nritems || (path->slots[0] >= nritems - 1)) {
Chris Masonf578d4b2007-10-25 15:42:56 -0400779 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400780 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400781 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400782 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400783 goto insert;
Filipe Manana27b9a812014-08-09 21:22:27 +0100784 slot = path->slots[0];
Chris Masonf578d4b2007-10-25 15:42:56 -0400785 }
786 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500787 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
788 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
Chris Masonf578d4b2007-10-25 15:42:56 -0400789 found_next = 1;
790 goto insert;
791 }
792 next_offset = found_key.offset;
793 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400794 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 Sterbab3b4aa72011-04-21 01:20:15 +0200801 btrfs_release_path(path);
Chris Mason6567e832007-04-16 09:22:45 -0400802 ret = btrfs_search_slot(trans, root, &file_key, path,
Josef Bacik607d4322008-12-02 07:17:45 -0500803 csum_size, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400804 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400805 goto fail_unlock;
Chris Mason459931e2008-12-10 09:10:46 -0500806
807 if (ret > 0) {
808 if (path->slots[0] == 0)
809 goto insert;
810 path->slots[0]--;
Chris Mason6567e832007-04-16 09:22:45 -0400811 }
Chris Mason459931e2008-12-10 09:10:46 -0500812
Chris Mason5f39d392007-10-15 16:14:19 -0400813 leaf = path->nodes[0];
814 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500815 csum_offset = (bytenr - found_key.offset) >>
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400816 fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500817
David Sterba962a2982014-06-04 18:41:45 +0200818 if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
Chris Masond20f7042008-12-08 16:58:54 -0500819 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400820 csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
Chris Mason6567e832007-04-16 09:22:45 -0400821 goto insert;
822 }
Chris Mason459931e2008-12-10 09:10:46 -0500823
Liu Bo2f697dc2013-02-04 13:12:18 +0000824 if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
Josef Bacik607d4322008-12-02 07:17:45 -0500825 csum_size) {
Liu Bo2f697dc2013-02-04 13:12:18 +0000826 int extend_nr;
827 u64 tmp;
828 u32 diff;
829 u32 free_space;
Chris Mason459931e2008-12-10 09:10:46 -0500830
David Sterbae902baa2019-03-20 14:36:46 +0100831 if (btrfs_leaf_free_space(leaf) <
Liu Bo2f697dc2013-02-04 13:12:18 +0000832 sizeof(struct btrfs_item) + csum_size * 2)
833 goto insert;
834
David Sterbae902baa2019-03-20 14:36:46 +0100835 free_space = btrfs_leaf_free_space(leaf) -
Liu Bo2f697dc2013-02-04 13:12:18 +0000836 sizeof(struct btrfs_item) - csum_size;
Miao Xief51a4a12013-06-19 10:36:09 +0800837 tmp = sums->len - total_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400838 tmp >>= fs_info->sb->s_blocksize_bits;
Liu Bo2f697dc2013-02-04 13:12:18 +0000839 WARN_ON(tmp < 1);
840
841 extend_nr = max_t(int, 1, (int)tmp);
842 diff = (csum_offset + extend_nr) * csum_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 diff = min(diff,
844 MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
Chris Mason459931e2008-12-10 09:10:46 -0500845
Chris Mason5f39d392007-10-15 16:14:19 -0400846 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Liu Bo2f697dc2013-02-04 13:12:18 +0000847 diff = min(free_space, diff);
848 diff /= csum_size;
849 diff *= csum_size;
Chris Mason459931e2008-12-10 09:10:46 -0500850
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400851 btrfs_extend_item(fs_info, path, diff);
Miao Xief51a4a12013-06-19 10:36:09 +0800852 ret = 0;
Chris Mason6567e832007-04-16 09:22:45 -0400853 goto csum;
854 }
855
856insert:
David Sterbab3b4aa72011-04-21 01:20:15 +0200857 btrfs_release_path(path);
Chris Mason6567e832007-04-16 09:22:45 -0400858 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400859 if (found_next) {
Liu Bo2f697dc2013-02-04 13:12:18 +0000860 u64 tmp;
Chris Masond20f7042008-12-08 16:58:54 -0500861
Miao Xief51a4a12013-06-19 10:36:09 +0800862 tmp = sums->len - total_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400863 tmp >>= fs_info->sb->s_blocksize_bits;
Liu Bo2f697dc2013-02-04 13:12:18 +0000864 tmp = min(tmp, (next_offset - file_key.offset) >>
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400865 fs_info->sb->s_blocksize_bits);
Liu Bo2f697dc2013-02-04 13:12:18 +0000866
Seraphime Kirkovski50d04462016-12-15 14:38:28 +0100867 tmp = max_t(u64, 1, tmp);
868 tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
Josef Bacik607d4322008-12-02 07:17:45 -0500869 ins_size = csum_size * tmp;
Chris Masonf578d4b2007-10-25 15:42:56 -0400870 } else {
Josef Bacik607d4322008-12-02 07:17:45 -0500871 ins_size = csum_size;
Chris Masonf578d4b2007-10-25 15:42:56 -0400872 }
Chris Masonb9473432009-03-13 11:00:37 -0400873 path->leave_spinning = 1;
Chris Mason5caf2a02007-04-02 11:20:42 -0400874 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400875 ins_size);
Chris Masonb9473432009-03-13 11:00:37 -0400876 path->leave_spinning = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400877 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400878 goto fail_unlock;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530879 if (WARN_ON(ret != 0))
Chris Mason53863232008-08-15 15:34:18 -0400880 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -0400881 leaf = path->nodes[0];
Miao Xief51a4a12013-06-19 10:36:09 +0800882csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400883 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Miao Xief51a4a12013-06-19 10:36:09 +0800884 item_end = (struct btrfs_csum_item *)((unsigned char *)item +
885 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason509659c2007-05-10 12:36:17 -0400886 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500887 csum_offset * csum_size);
Chris Masonb18c6682007-04-17 13:26:50 -0400888found:
Miao Xief51a4a12013-06-19 10:36:09 +0800889 ins_size = (u32)(sums->len - total_bytes) >>
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400890 fs_info->sb->s_blocksize_bits;
Miao Xief51a4a12013-06-19 10:36:09 +0800891 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 Masonaadfeb62008-01-29 09:10:27 -0500896
Miao Xief51a4a12013-06-19 10:36:09 +0800897 ins_size /= csum_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400898 total_bytes += ins_size * fs_info->sectorsize;
Miao Xief51a4a12013-06-19 10:36:09 +0800899 index += ins_size;
Chris Masona6591712011-07-19 12:04:14 -0400900
Chris Mason5caf2a02007-04-02 11:20:42 -0400901 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400902 if (total_bytes < sums->len) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200903 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -0400904 cond_resched();
Chris Mason065631f2008-02-20 12:07:25 -0500905 goto again;
906 }
Chris Mason53863232008-08-15 15:34:18 -0400907out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400908 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400909 return ret;
Chris Mason53863232008-08-15 15:34:18 -0400910
911fail_unlock:
Chris Mason53863232008-08-15 15:34:18 -0400912 goto out;
Chris Masonf254e522007-03-29 15:15:27 -0400913}
Filipe Manana7ffbb592014-06-09 03:48:05 +0100914
Nikolay Borisov9cdc5122017-02-20 13:51:02 +0200915void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
Filipe Manana7ffbb592014-06-09 03:48:05 +0100916 const struct btrfs_path *path,
917 struct btrfs_file_extent_item *fi,
918 const bool new_inline,
919 struct extent_map *em)
920{
David Sterba3ffbd682018-06-29 10:56:42 +0200921 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov9cdc5122017-02-20 13:51:02 +0200922 struct btrfs_root *root = inode->root;
Filipe Manana7ffbb592014-06-09 03:48:05 +0100923 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 Mahoney0b246af2016-06-22 18:54:23 -0400931 em->bdev = fs_info->fs_devices->latest_bdev;
Filipe Manana7ffbb592014-06-09 03:48:05 +0100932 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 Wenruoe41ca582018-06-06 15:41:49 +0800941 size = btrfs_file_extent_ram_bytes(leaf, fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400942 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400943 fs_info->sectorsize);
Filipe Manana7ffbb592014-06-09 03:48:05 +0100944 }
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 Mahoney0b246af2016-06-22 18:54:23 -0400986 btrfs_err(fs_info,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +0200987 "unknown file extent item type %d, inode %llu, offset %llu, "
988 "root %llu", type, btrfs_ino(inode), extent_start,
Filipe Manana7ffbb592014-06-09 03:48:05 +0100989 root->root_key.objectid);
990 }
991}