xref: /linux/fs/btrfs/raid-stripe-tree.c (revision 7a40974fd0efa3698de4c6d1d0ee0436bcc4445d)
102c372e1SJohannes Thumshirn // SPDX-License-Identifier: GPL-2.0
202c372e1SJohannes Thumshirn /*
302c372e1SJohannes Thumshirn  * Copyright (C) 2023 Western Digital Corporation or its affiliates.
402c372e1SJohannes Thumshirn  */
502c372e1SJohannes Thumshirn 
602c372e1SJohannes Thumshirn #include <linux/btrfs_tree.h>
702c372e1SJohannes Thumshirn #include "ctree.h"
802c372e1SJohannes Thumshirn #include "fs.h"
902c372e1SJohannes Thumshirn #include "accessors.h"
1002c372e1SJohannes Thumshirn #include "transaction.h"
1102c372e1SJohannes Thumshirn #include "disk-io.h"
1202c372e1SJohannes Thumshirn #include "raid-stripe-tree.h"
1302c372e1SJohannes Thumshirn #include "volumes.h"
1402c372e1SJohannes Thumshirn #include "print-tree.h"
1502c372e1SJohannes Thumshirn 
btrfs_delete_raid_extent(struct btrfs_trans_handle * trans,u64 start,u64 length)16ca41504eSJohannes Thumshirn int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 length)
17ca41504eSJohannes Thumshirn {
18ca41504eSJohannes Thumshirn 	struct btrfs_fs_info *fs_info = trans->fs_info;
19ca41504eSJohannes Thumshirn 	struct btrfs_root *stripe_root = fs_info->stripe_root;
20ca41504eSJohannes Thumshirn 	struct btrfs_path *path;
21ca41504eSJohannes Thumshirn 	struct btrfs_key key;
22ca41504eSJohannes Thumshirn 	struct extent_buffer *leaf;
23ca41504eSJohannes Thumshirn 	u64 found_start;
24ca41504eSJohannes Thumshirn 	u64 found_end;
25ca41504eSJohannes Thumshirn 	u64 end = start + length;
26ca41504eSJohannes Thumshirn 	int slot;
27ca41504eSJohannes Thumshirn 	int ret;
28ca41504eSJohannes Thumshirn 
29ca41504eSJohannes Thumshirn 	if (!stripe_root)
30ca41504eSJohannes Thumshirn 		return 0;
31ca41504eSJohannes Thumshirn 
32ca41504eSJohannes Thumshirn 	path = btrfs_alloc_path();
33ca41504eSJohannes Thumshirn 	if (!path)
34ca41504eSJohannes Thumshirn 		return -ENOMEM;
35ca41504eSJohannes Thumshirn 
36ca41504eSJohannes Thumshirn 	while (1) {
37ca41504eSJohannes Thumshirn 		key.objectid = start;
38ca41504eSJohannes Thumshirn 		key.type = BTRFS_RAID_STRIPE_KEY;
39ca41504eSJohannes Thumshirn 		key.offset = length;
40ca41504eSJohannes Thumshirn 
41ca41504eSJohannes Thumshirn 		ret = btrfs_search_slot(trans, stripe_root, &key, path, -1, 1);
42ca41504eSJohannes Thumshirn 		if (ret < 0)
43ca41504eSJohannes Thumshirn 			break;
44ca41504eSJohannes Thumshirn 		if (ret > 0) {
45ca41504eSJohannes Thumshirn 			ret = 0;
46ca41504eSJohannes Thumshirn 			if (path->slots[0] == 0)
47ca41504eSJohannes Thumshirn 				break;
48ca41504eSJohannes Thumshirn 			path->slots[0]--;
49ca41504eSJohannes Thumshirn 		}
50ca41504eSJohannes Thumshirn 
51ca41504eSJohannes Thumshirn 		leaf = path->nodes[0];
52ca41504eSJohannes Thumshirn 		slot = path->slots[0];
53ca41504eSJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &key, slot);
54ca41504eSJohannes Thumshirn 		found_start = key.objectid;
55ca41504eSJohannes Thumshirn 		found_end = found_start + key.offset;
56ca41504eSJohannes Thumshirn 
57ca41504eSJohannes Thumshirn 		/* That stripe ends before we start, we're done. */
58ca41504eSJohannes Thumshirn 		if (found_end <= start)
59ca41504eSJohannes Thumshirn 			break;
60ca41504eSJohannes Thumshirn 
61b5e2c2ffSJohannes Thumshirn 		trace_btrfs_raid_extent_delete(fs_info, start, end,
62b5e2c2ffSJohannes Thumshirn 					       found_start, found_end);
63b5e2c2ffSJohannes Thumshirn 
64ca41504eSJohannes Thumshirn 		ASSERT(found_start >= start && found_end <= end);
65ca41504eSJohannes Thumshirn 		ret = btrfs_del_item(trans, stripe_root, path);
66ca41504eSJohannes Thumshirn 		if (ret)
67ca41504eSJohannes Thumshirn 			break;
68ca41504eSJohannes Thumshirn 
697fa5230bSJohannes Thumshirn 		start += key.offset;
707fa5230bSJohannes Thumshirn 		length -= key.offset;
717fa5230bSJohannes Thumshirn 		if (length == 0)
727fa5230bSJohannes Thumshirn 			break;
737fa5230bSJohannes Thumshirn 
74ca41504eSJohannes Thumshirn 		btrfs_release_path(path);
75ca41504eSJohannes Thumshirn 	}
76ca41504eSJohannes Thumshirn 
77ca41504eSJohannes Thumshirn 	btrfs_free_path(path);
78ca41504eSJohannes Thumshirn 	return ret;
79ca41504eSJohannes Thumshirn }
80ca41504eSJohannes Thumshirn 
update_raid_extent_item(struct btrfs_trans_handle * trans,struct btrfs_key * key,struct btrfs_stripe_extent * stripe_extent,const size_t item_size)818c4cba2aSJohannes Thumshirn static int update_raid_extent_item(struct btrfs_trans_handle *trans,
828c4cba2aSJohannes Thumshirn 				   struct btrfs_key *key,
838c4cba2aSJohannes Thumshirn 				   struct btrfs_stripe_extent *stripe_extent,
848c4cba2aSJohannes Thumshirn 				   const size_t item_size)
858c4cba2aSJohannes Thumshirn {
868c4cba2aSJohannes Thumshirn 	struct btrfs_path *path;
878c4cba2aSJohannes Thumshirn 	struct extent_buffer *leaf;
888c4cba2aSJohannes Thumshirn 	int ret;
898c4cba2aSJohannes Thumshirn 	int slot;
908c4cba2aSJohannes Thumshirn 
918c4cba2aSJohannes Thumshirn 	path = btrfs_alloc_path();
928c4cba2aSJohannes Thumshirn 	if (!path)
938c4cba2aSJohannes Thumshirn 		return -ENOMEM;
948c4cba2aSJohannes Thumshirn 
958c4cba2aSJohannes Thumshirn 	ret = btrfs_search_slot(trans, trans->fs_info->stripe_root, key, path,
968c4cba2aSJohannes Thumshirn 				0, 1);
978c4cba2aSJohannes Thumshirn 	if (ret)
988c4cba2aSJohannes Thumshirn 		return (ret == 1 ? ret : -EINVAL);
998c4cba2aSJohannes Thumshirn 
1008c4cba2aSJohannes Thumshirn 	leaf = path->nodes[0];
1018c4cba2aSJohannes Thumshirn 	slot = path->slots[0];
1028c4cba2aSJohannes Thumshirn 
1038c4cba2aSJohannes Thumshirn 	write_extent_buffer(leaf, stripe_extent, btrfs_item_ptr_offset(leaf, slot),
1048c4cba2aSJohannes Thumshirn 			    item_size);
1058c4cba2aSJohannes Thumshirn 	btrfs_mark_buffer_dirty(trans, leaf);
1068c4cba2aSJohannes Thumshirn 	btrfs_free_path(path);
1078c4cba2aSJohannes Thumshirn 
1088c4cba2aSJohannes Thumshirn 	return ret;
1098c4cba2aSJohannes Thumshirn }
1108c4cba2aSJohannes Thumshirn 
btrfs_insert_one_raid_extent(struct btrfs_trans_handle * trans,struct btrfs_io_context * bioc)11102c372e1SJohannes Thumshirn static int btrfs_insert_one_raid_extent(struct btrfs_trans_handle *trans,
11202c372e1SJohannes Thumshirn 					struct btrfs_io_context *bioc)
11302c372e1SJohannes Thumshirn {
11402c372e1SJohannes Thumshirn 	struct btrfs_fs_info *fs_info = trans->fs_info;
11502c372e1SJohannes Thumshirn 	struct btrfs_key stripe_key;
11602c372e1SJohannes Thumshirn 	struct btrfs_root *stripe_root = fs_info->stripe_root;
11702c372e1SJohannes Thumshirn 	const int num_stripes = btrfs_bg_type_to_factor(bioc->map_type);
11802c372e1SJohannes Thumshirn 	struct btrfs_stripe_extent *stripe_extent;
11902c372e1SJohannes Thumshirn 	const size_t item_size = struct_size(stripe_extent, strides, num_stripes);
12002c372e1SJohannes Thumshirn 	int ret;
12102c372e1SJohannes Thumshirn 
12202c372e1SJohannes Thumshirn 	stripe_extent = kzalloc(item_size, GFP_NOFS);
12302c372e1SJohannes Thumshirn 	if (!stripe_extent) {
12402c372e1SJohannes Thumshirn 		btrfs_abort_transaction(trans, -ENOMEM);
12502c372e1SJohannes Thumshirn 		btrfs_end_transaction(trans);
12602c372e1SJohannes Thumshirn 		return -ENOMEM;
12702c372e1SJohannes Thumshirn 	}
12802c372e1SJohannes Thumshirn 
129b5e2c2ffSJohannes Thumshirn 	trace_btrfs_insert_one_raid_extent(fs_info, bioc->logical, bioc->size,
130b5e2c2ffSJohannes Thumshirn 					   num_stripes);
13102c372e1SJohannes Thumshirn 	for (int i = 0; i < num_stripes; i++) {
13202c372e1SJohannes Thumshirn 		u64 devid = bioc->stripes[i].dev->devid;
13302c372e1SJohannes Thumshirn 		u64 physical = bioc->stripes[i].physical;
13402c372e1SJohannes Thumshirn 		u64 length = bioc->stripes[i].length;
13502c372e1SJohannes Thumshirn 		struct btrfs_raid_stride *raid_stride = &stripe_extent->strides[i];
13602c372e1SJohannes Thumshirn 
13702c372e1SJohannes Thumshirn 		if (length == 0)
13802c372e1SJohannes Thumshirn 			length = bioc->size;
13902c372e1SJohannes Thumshirn 
14002c372e1SJohannes Thumshirn 		btrfs_set_stack_raid_stride_devid(raid_stride, devid);
14102c372e1SJohannes Thumshirn 		btrfs_set_stack_raid_stride_physical(raid_stride, physical);
14202c372e1SJohannes Thumshirn 	}
14302c372e1SJohannes Thumshirn 
14402c372e1SJohannes Thumshirn 	stripe_key.objectid = bioc->logical;
14502c372e1SJohannes Thumshirn 	stripe_key.type = BTRFS_RAID_STRIPE_KEY;
14602c372e1SJohannes Thumshirn 	stripe_key.offset = bioc->size;
14702c372e1SJohannes Thumshirn 
14802c372e1SJohannes Thumshirn 	ret = btrfs_insert_item(trans, stripe_root, &stripe_key, stripe_extent,
14902c372e1SJohannes Thumshirn 				item_size);
1508c4cba2aSJohannes Thumshirn 	if (ret == -EEXIST)
1518c4cba2aSJohannes Thumshirn 		ret = update_raid_extent_item(trans, &stripe_key, stripe_extent,
1528c4cba2aSJohannes Thumshirn 					      item_size);
15302c372e1SJohannes Thumshirn 	if (ret)
15402c372e1SJohannes Thumshirn 		btrfs_abort_transaction(trans, ret);
15502c372e1SJohannes Thumshirn 
15602c372e1SJohannes Thumshirn 	kfree(stripe_extent);
15702c372e1SJohannes Thumshirn 
15802c372e1SJohannes Thumshirn 	return ret;
15902c372e1SJohannes Thumshirn }
16002c372e1SJohannes Thumshirn 
btrfs_insert_raid_extent(struct btrfs_trans_handle * trans,struct btrfs_ordered_extent * ordered_extent)16102c372e1SJohannes Thumshirn int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
16202c372e1SJohannes Thumshirn 			     struct btrfs_ordered_extent *ordered_extent)
16302c372e1SJohannes Thumshirn {
16402c372e1SJohannes Thumshirn 	struct btrfs_io_context *bioc;
16502c372e1SJohannes Thumshirn 	int ret;
16602c372e1SJohannes Thumshirn 
16702c372e1SJohannes Thumshirn 	if (!btrfs_fs_incompat(trans->fs_info, RAID_STRIPE_TREE))
16802c372e1SJohannes Thumshirn 		return 0;
16902c372e1SJohannes Thumshirn 
17002c372e1SJohannes Thumshirn 	list_for_each_entry(bioc, &ordered_extent->bioc_list, rst_ordered_entry) {
17102c372e1SJohannes Thumshirn 		ret = btrfs_insert_one_raid_extent(trans, bioc);
17202c372e1SJohannes Thumshirn 		if (ret)
17302c372e1SJohannes Thumshirn 			return ret;
17402c372e1SJohannes Thumshirn 	}
17502c372e1SJohannes Thumshirn 
17602c372e1SJohannes Thumshirn 	while (!list_empty(&ordered_extent->bioc_list)) {
17702c372e1SJohannes Thumshirn 		bioc = list_first_entry(&ordered_extent->bioc_list,
17802c372e1SJohannes Thumshirn 					typeof(*bioc), rst_ordered_entry);
17902c372e1SJohannes Thumshirn 		list_del(&bioc->rst_ordered_entry);
18002c372e1SJohannes Thumshirn 		btrfs_put_bioc(bioc);
18102c372e1SJohannes Thumshirn 	}
18202c372e1SJohannes Thumshirn 
183b8212814SDan Carpenter 	return 0;
18402c372e1SJohannes Thumshirn }
18510e27980SJohannes Thumshirn 
btrfs_get_raid_extent_offset(struct btrfs_fs_info * fs_info,u64 logical,u64 * length,u64 map_type,u32 stripe_index,struct btrfs_io_stripe * stripe)18610e27980SJohannes Thumshirn int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
18710e27980SJohannes Thumshirn 				 u64 logical, u64 *length, u64 map_type,
18810e27980SJohannes Thumshirn 				 u32 stripe_index, struct btrfs_io_stripe *stripe)
18910e27980SJohannes Thumshirn {
19010e27980SJohannes Thumshirn 	struct btrfs_root *stripe_root = fs_info->stripe_root;
19110e27980SJohannes Thumshirn 	struct btrfs_stripe_extent *stripe_extent;
19210e27980SJohannes Thumshirn 	struct btrfs_key stripe_key;
19310e27980SJohannes Thumshirn 	struct btrfs_key found_key;
19410e27980SJohannes Thumshirn 	struct btrfs_path *path;
19510e27980SJohannes Thumshirn 	struct extent_buffer *leaf;
19610e27980SJohannes Thumshirn 	const u64 end = logical + *length;
19710e27980SJohannes Thumshirn 	int num_stripes;
19810e27980SJohannes Thumshirn 	u64 offset;
19910e27980SJohannes Thumshirn 	u64 found_logical;
20010e27980SJohannes Thumshirn 	u64 found_length;
20110e27980SJohannes Thumshirn 	u64 found_end;
20210e27980SJohannes Thumshirn 	int slot;
20310e27980SJohannes Thumshirn 	int ret;
20410e27980SJohannes Thumshirn 
20510e27980SJohannes Thumshirn 	stripe_key.objectid = logical;
20610e27980SJohannes Thumshirn 	stripe_key.type = BTRFS_RAID_STRIPE_KEY;
20710e27980SJohannes Thumshirn 	stripe_key.offset = 0;
20810e27980SJohannes Thumshirn 
20910e27980SJohannes Thumshirn 	path = btrfs_alloc_path();
21010e27980SJohannes Thumshirn 	if (!path)
21110e27980SJohannes Thumshirn 		return -ENOMEM;
21210e27980SJohannes Thumshirn 
213d6106f0dSJohannes Thumshirn 	if (stripe->rst_search_commit_root) {
2149acaa641SJohannes Thumshirn 		path->skip_locking = 1;
2159acaa641SJohannes Thumshirn 		path->search_commit_root = 1;
2169acaa641SJohannes Thumshirn 	}
2179acaa641SJohannes Thumshirn 
21810e27980SJohannes Thumshirn 	ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
21910e27980SJohannes Thumshirn 	if (ret < 0)
22010e27980SJohannes Thumshirn 		goto free_path;
22110e27980SJohannes Thumshirn 	if (ret) {
22210e27980SJohannes Thumshirn 		if (path->slots[0] != 0)
22310e27980SJohannes Thumshirn 			path->slots[0]--;
22410e27980SJohannes Thumshirn 	}
22510e27980SJohannes Thumshirn 
22610e27980SJohannes Thumshirn 	while (1) {
22710e27980SJohannes Thumshirn 		leaf = path->nodes[0];
22810e27980SJohannes Thumshirn 		slot = path->slots[0];
22910e27980SJohannes Thumshirn 
23010e27980SJohannes Thumshirn 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
23110e27980SJohannes Thumshirn 		found_logical = found_key.objectid;
23210e27980SJohannes Thumshirn 		found_length = found_key.offset;
23310e27980SJohannes Thumshirn 		found_end = found_logical + found_length;
23410e27980SJohannes Thumshirn 
23510e27980SJohannes Thumshirn 		if (found_logical > end) {
23610e27980SJohannes Thumshirn 			ret = -ENOENT;
23710e27980SJohannes Thumshirn 			goto out;
23810e27980SJohannes Thumshirn 		}
23910e27980SJohannes Thumshirn 
24010e27980SJohannes Thumshirn 		if (in_range(logical, found_logical, found_length))
24110e27980SJohannes Thumshirn 			break;
24210e27980SJohannes Thumshirn 
24310e27980SJohannes Thumshirn 		ret = btrfs_next_item(stripe_root, path);
24410e27980SJohannes Thumshirn 		if (ret)
24510e27980SJohannes Thumshirn 			goto out;
24610e27980SJohannes Thumshirn 	}
24710e27980SJohannes Thumshirn 
24810e27980SJohannes Thumshirn 	offset = logical - found_logical;
24910e27980SJohannes Thumshirn 
25010e27980SJohannes Thumshirn 	/*
25110e27980SJohannes Thumshirn 	 * If we have a logically contiguous, but physically non-continuous
25210e27980SJohannes Thumshirn 	 * range, we need to split the bio. Record the length after which we
25310e27980SJohannes Thumshirn 	 * must split the bio.
25410e27980SJohannes Thumshirn 	 */
25510e27980SJohannes Thumshirn 	if (end > found_end)
25610e27980SJohannes Thumshirn 		*length -= end - found_end;
25710e27980SJohannes Thumshirn 
25810e27980SJohannes Thumshirn 	num_stripes = btrfs_num_raid_stripes(btrfs_item_size(leaf, slot));
25910e27980SJohannes Thumshirn 	stripe_extent = btrfs_item_ptr(leaf, slot, struct btrfs_stripe_extent);
26010e27980SJohannes Thumshirn 
26110e27980SJohannes Thumshirn 	for (int i = 0; i < num_stripes; i++) {
26210e27980SJohannes Thumshirn 		struct btrfs_raid_stride *stride = &stripe_extent->strides[i];
26310e27980SJohannes Thumshirn 		u64 devid = btrfs_raid_stride_devid(leaf, stride);
26410e27980SJohannes Thumshirn 		u64 physical = btrfs_raid_stride_physical(leaf, stride);
26510e27980SJohannes Thumshirn 
26610e27980SJohannes Thumshirn 		if (devid != stripe->dev->devid)
26710e27980SJohannes Thumshirn 			continue;
26810e27980SJohannes Thumshirn 
26910e27980SJohannes Thumshirn 		if ((map_type & BTRFS_BLOCK_GROUP_DUP) && stripe_index != i)
27010e27980SJohannes Thumshirn 			continue;
27110e27980SJohannes Thumshirn 
27210e27980SJohannes Thumshirn 		stripe->physical = physical + offset;
27310e27980SJohannes Thumshirn 
274b5e2c2ffSJohannes Thumshirn 		trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
275b5e2c2ffSJohannes Thumshirn 						   stripe->physical, devid);
276b5e2c2ffSJohannes Thumshirn 
27710e27980SJohannes Thumshirn 		ret = 0;
27810e27980SJohannes Thumshirn 		goto free_path;
27910e27980SJohannes Thumshirn 	}
28010e27980SJohannes Thumshirn 
28110e27980SJohannes Thumshirn 	/* If we're here, we haven't found the requested devid in the stripe. */
28210e27980SJohannes Thumshirn 	ret = -ENOENT;
28310e27980SJohannes Thumshirn out:
28410e27980SJohannes Thumshirn 	if (ret > 0)
28510e27980SJohannes Thumshirn 		ret = -ENOENT;
286d6106f0dSJohannes Thumshirn 	if (ret && ret != -EIO && !stripe->rst_search_commit_root) {
287*0c749585SJohannes Thumshirn 		btrfs_debug(fs_info,
28810e27980SJohannes Thumshirn 		"cannot find raid-stripe for logical [%llu, %llu] devid %llu, profile %s",
28910e27980SJohannes Thumshirn 			  logical, logical + *length, stripe->dev->devid,
29010e27980SJohannes Thumshirn 			  btrfs_bg_type_to_raid_name(map_type));
29110e27980SJohannes Thumshirn 	}
29210e27980SJohannes Thumshirn free_path:
29310e27980SJohannes Thumshirn 	btrfs_free_path(path);
29410e27980SJohannes Thumshirn 
29510e27980SJohannes Thumshirn 	return ret;
29610e27980SJohannes Thumshirn }
297