1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2023 Western Digital Corporation or its affiliates.
6 #include <linux/btrfs_tree.h>
10 #include "transaction.h"
12 #include "raid-stripe-tree.h"
15 #include "print-tree.h"
17 int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
19 struct btrfs_fs_info *fs_info = trans->fs_info;
20 struct btrfs_root *stripe_root = fs_info->stripe_root;
21 struct btrfs_path *path;
23 struct extent_buffer *leaf;
26 u64 end = start + length;
33 path = btrfs_alloc_path();
39 key.type = BTRFS_RAID_STRIPE_KEY;
42 ret = btrfs_search_slot(trans, stripe_root, &key, path, -1, 1);
47 if (path->slots[0] == 0)
52 leaf = path->nodes[0];
53 slot = path->slots[0];
54 btrfs_item_key_to_cpu(leaf, &key, slot);
55 found_start = key.objectid;
56 found_end = found_start + key.offset;
58 /* That stripe ends before we start, we're done. */
59 if (found_end <= start)
62 trace_btrfs_raid_extent_delete(fs_info, start, end,
63 found_start, found_end);
65 ASSERT(found_start >= start && found_end <= end);
66 ret = btrfs_del_item(trans, stripe_root, path);
70 btrfs_release_path(path);
73 btrfs_free_path(path);
77 static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
78 struct btrfs_io_context *bioc)
80 struct btrfs_fs_info *fs_info = trans->fs_info;
81 struct btrfs_key stripe_key;
82 struct btrfs_root *stripe_root = fs_info->stripe_root;
83 const int num_stripes = btrfs_bg_type_to_factor(bioc->map_type);
84 u8 encoding = btrfs_bg_flags_to_raid_index(bioc->map_type);
85 struct btrfs_stripe_extent *stripe_extent;
86 const size_t item_size = struct_size(stripe_extent, strides, num_stripes);
89 stripe_extent = kzalloc(item_size, GFP_NOFS);
91 btrfs_abort_transaction(trans, -ENOMEM);
92 btrfs_end_transaction(trans);
96 trace_btrfs_insert_one_raid_extent(fs_info, bioc->logical, bioc->size,
98 btrfs_set_stack_stripe_extent_encoding(stripe_extent, encoding);
99 for (int i = 0; i < num_stripes; i++) {
100 u64 devid = bioc->stripes[i].dev->devid;
101 u64 physical = bioc->stripes[i].physical;
102 u64 length = bioc->stripes[i].length;
103 struct btrfs_raid_stride *raid_stride = &stripe_extent->strides[i];
108 btrfs_set_stack_raid_stride_devid(raid_stride, devid);
109 btrfs_set_stack_raid_stride_physical(raid_stride, physical);
112 stripe_key.objectid = bioc->logical;
113 stripe_key.type = BTRFS_RAID_STRIPE_KEY;
114 stripe_key.offset = bioc->size;
116 ret = btrfs_insert_item(trans, stripe_root, &stripe_key, stripe_extent,
119 btrfs_abort_transaction(trans, ret);
121 kfree(stripe_extent);
126 int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
127 struct btrfs_ordered_extent *ordered_extent)
129 struct btrfs_io_context *bioc;
132 if (!btrfs_fs_incompat(trans->fs_info, RAID_STRIPE_TREE))
135 list_for_each_entry(bioc, &ordered_extent->bioc_list, rst_ordered_entry) {
136 ret = btrfs_insert_one_raid_extent(trans, bioc);
141 while (!list_empty(&ordered_extent->bioc_list)) {
142 bioc = list_first_entry(&ordered_extent->bioc_list,
143 typeof(*bioc), rst_ordered_entry);
144 list_del(&bioc->rst_ordered_entry);
145 btrfs_put_bioc(bioc);
151 int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
152 u64 logical, u64 *length, u64 map_type,
153 u32 stripe_index, struct btrfs_io_stripe *stripe)
155 struct btrfs_root *stripe_root = fs_info->stripe_root;
156 struct btrfs_stripe_extent *stripe_extent;
157 struct btrfs_key stripe_key;
158 struct btrfs_key found_key;
159 struct btrfs_path *path;
160 struct extent_buffer *leaf;
161 const u64 end = logical + *length;
171 stripe_key.objectid = logical;
172 stripe_key.type = BTRFS_RAID_STRIPE_KEY;
173 stripe_key.offset = 0;
175 path = btrfs_alloc_path();
179 if (stripe->is_scrub) {
180 path->skip_locking = 1;
181 path->search_commit_root = 1;
184 ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
188 if (path->slots[0] != 0)
193 leaf = path->nodes[0];
194 slot = path->slots[0];
196 btrfs_item_key_to_cpu(leaf, &found_key, slot);
197 found_logical = found_key.objectid;
198 found_length = found_key.offset;
199 found_end = found_logical + found_length;
201 if (found_logical > end) {
206 if (in_range(logical, found_logical, found_length))
209 ret = btrfs_next_item(stripe_root, path);
214 offset = logical - found_logical;
217 * If we have a logically contiguous, but physically non-continuous
218 * range, we need to split the bio. Record the length after which we
219 * must split the bio.
222 *length -= end - found_end;
224 num_stripes = btrfs_num_raid_stripes(btrfs_item_size(leaf, slot));
225 stripe_extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
226 encoding = btrfs_stripe_extent_encoding(leaf, stripe_extent);
228 if (encoding != btrfs_bg_flags_to_raid_index(map_type)) {
230 btrfs_handle_fs_error(fs_info, ret,
231 "on-disk stripe encoding %d doesn't match RAID index %d",
233 btrfs_bg_flags_to_raid_index(map_type));
237 for (int i = 0; i < num_stripes; i++) {
238 struct btrfs_raid_stride *stride = &stripe_extent->strides[i];
239 u64 devid = btrfs_raid_stride_devid(leaf, stride);
240 u64 physical = btrfs_raid_stride_physical(leaf, stride);
242 if (devid != stripe->dev->devid)
245 if ((map_type & BTRFS_BLOCK_GROUP_DUP) && stripe_index != i)
248 stripe->physical = physical + offset;
250 trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
251 stripe->physical, devid);
257 /* If we're here, we haven't found the requested devid in the stripe. */
262 if (ret && ret != -EIO && !stripe->is_scrub) {
263 if (IS_ENABLED(CONFIG_BTRFS_DEBUG))
264 btrfs_print_tree(leaf, 1);
266 "cannot find raid-stripe for logical [%llu, %llu] devid %llu, profile %s",
267 logical, logical + *length, stripe->dev->devid,
268 btrfs_bg_type_to_raid_name(map_type));
271 btrfs_free_path(path);