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"
14 #include "print-tree.h"
16 int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
18 struct btrfs_fs_info *fs_info = trans->fs_info;
19 struct btrfs_root *stripe_root = fs_info->stripe_root;
20 struct btrfs_path *path;
22 struct extent_buffer *leaf;
25 u64 end = start + length;
32 path = btrfs_alloc_path();
38 key.type = BTRFS_RAID_STRIPE_KEY;
41 ret = btrfs_search_slot(trans, stripe_root, &key, path, -1, 1);
46 if (path->slots[0] == 0)
51 leaf = path->nodes[0];
52 slot = path->slots[0];
53 btrfs_item_key_to_cpu(leaf, &key, slot);
54 found_start = key.objectid;
55 found_end = found_start + key.offset;
57 /* That stripe ends before we start, we're done. */
58 if (found_end <= start)
61 trace_btrfs_raid_extent_delete(fs_info, start, end,
62 found_start, found_end);
64 ASSERT(found_start >= start && found_end <= end);
65 ret = btrfs_del_item(trans, stripe_root, path);
69 btrfs_release_path(path);
72 btrfs_free_path(path);
76 static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
77 struct btrfs_io_context *bioc)
79 struct btrfs_fs_info *fs_info = trans->fs_info;
80 struct btrfs_key stripe_key;
81 struct btrfs_root *stripe_root = fs_info->stripe_root;
82 const int num_stripes = btrfs_bg_type_to_factor(bioc->map_type);
83 u8 encoding = btrfs_bg_flags_to_raid_index(bioc->map_type);
84 struct btrfs_stripe_extent *stripe_extent;
85 const size_t item_size = struct_size(stripe_extent, strides, num_stripes);
88 stripe_extent = kzalloc(item_size, GFP_NOFS);
90 btrfs_abort_transaction(trans, -ENOMEM);
91 btrfs_end_transaction(trans);
95 trace_btrfs_insert_one_raid_extent(fs_info, bioc->logical, bioc->size,
97 btrfs_set_stack_stripe_extent_encoding(stripe_extent, encoding);
98 for (int i = 0; i < num_stripes; i++) {
99 u64 devid = bioc->stripes[i].dev->devid;
100 u64 physical = bioc->stripes[i].physical;
101 u64 length = bioc->stripes[i].length;
102 struct btrfs_raid_stride *raid_stride = &stripe_extent->strides[i];
107 btrfs_set_stack_raid_stride_devid(raid_stride, devid);
108 btrfs_set_stack_raid_stride_physical(raid_stride, physical);
111 stripe_key.objectid = bioc->logical;
112 stripe_key.type = BTRFS_RAID_STRIPE_KEY;
113 stripe_key.offset = bioc->size;
115 ret = btrfs_insert_item(trans, stripe_root, &stripe_key, stripe_extent,
118 btrfs_abort_transaction(trans, ret);
120 kfree(stripe_extent);
125 int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
126 struct btrfs_ordered_extent *ordered_extent)
128 struct btrfs_io_context *bioc;
131 if (!btrfs_fs_incompat(trans->fs_info, RAID_STRIPE_TREE))
134 list_for_each_entry(bioc, &ordered_extent->bioc_list, rst_ordered_entry) {
135 ret = btrfs_insert_one_raid_extent(trans, bioc);
140 while (!list_empty(&ordered_extent->bioc_list)) {
141 bioc = list_first_entry(&ordered_extent->bioc_list,
142 typeof(*bioc), rst_ordered_entry);
143 list_del(&bioc->rst_ordered_entry);
144 btrfs_put_bioc(bioc);
150 int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
151 u64 logical, u64 *length, u64 map_type,
152 u32 stripe_index, struct btrfs_io_stripe *stripe)
154 struct btrfs_root *stripe_root = fs_info->stripe_root;
155 struct btrfs_stripe_extent *stripe_extent;
156 struct btrfs_key stripe_key;
157 struct btrfs_key found_key;
158 struct btrfs_path *path;
159 struct extent_buffer *leaf;
160 const u64 end = logical + *length;
170 stripe_key.objectid = logical;
171 stripe_key.type = BTRFS_RAID_STRIPE_KEY;
172 stripe_key.offset = 0;
174 path = btrfs_alloc_path();
178 if (stripe->is_scrub) {
179 path->skip_locking = 1;
180 path->search_commit_root = 1;
183 ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
187 if (path->slots[0] != 0)
192 leaf = path->nodes[0];
193 slot = path->slots[0];
195 btrfs_item_key_to_cpu(leaf, &found_key, slot);
196 found_logical = found_key.objectid;
197 found_length = found_key.offset;
198 found_end = found_logical + found_length;
200 if (found_logical > end) {
205 if (in_range(logical, found_logical, found_length))
208 ret = btrfs_next_item(stripe_root, path);
213 offset = logical - found_logical;
216 * If we have a logically contiguous, but physically non-continuous
217 * range, we need to split the bio. Record the length after which we
218 * must split the bio.
221 *length -= end - found_end;
223 num_stripes = btrfs_num_raid_stripes(btrfs_item_size(leaf, slot));
224 stripe_extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
225 encoding = btrfs_stripe_extent_encoding(leaf, stripe_extent);
227 if (encoding != btrfs_bg_flags_to_raid_index(map_type)) {
229 btrfs_handle_fs_error(fs_info, ret,
230 "on-disk stripe encoding %d doesn't match RAID index %d",
232 btrfs_bg_flags_to_raid_index(map_type));
236 for (int i = 0; i < num_stripes; i++) {
237 struct btrfs_raid_stride *stride = &stripe_extent->strides[i];
238 u64 devid = btrfs_raid_stride_devid(leaf, stride);
239 u64 physical = btrfs_raid_stride_physical(leaf, stride);
241 if (devid != stripe->dev->devid)
244 if ((map_type & BTRFS_BLOCK_GROUP_DUP) && stripe_index != i)
247 stripe->physical = physical + offset;
249 trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
250 stripe->physical, devid);
256 /* If we're here, we haven't found the requested devid in the stripe. */
261 if (ret && ret != -EIO && !stripe->is_scrub) {
262 if (IS_ENABLED(CONFIG_BTRFS_DEBUG))
263 btrfs_print_tree(leaf, 1);
265 "cannot find raid-stripe for logical [%llu, %llu] devid %llu, profile %s",
266 logical, logical + *length, stripe->dev->devid,
267 btrfs_bg_type_to_raid_name(map_type));
270 btrfs_free_path(path);