From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
To: linux-btrfs@vger.kernel.org
Cc: Naohiro Aota <naohiro.aota@wdc.com>,
Damien Le Moal <damien.lemoal@wdc.com>,
Johannes Thumshirn <johannes.thumshirn@wdc.com>,
Qu Wenruo <wqu@suse.com>, Christoph Hellwig <hch@lst.de>,
Josef Bacik <josef@toxicpanda.com>
Subject: [PATCH RFC v2 6/8] btrfs: add code to read raid extent
Date: Wed, 29 Jun 2022 07:41:12 -0700 [thread overview]
Message-ID: <1f61c93be9354db6e53da4f404aa0f129f43a1e0.1656513330.git.johannes.thumshirn@wdc.com> (raw)
In-Reply-To: <cover.1656513330.git.johannes.thumshirn@wdc.com>
Add boilerplate code to lookup the physical address from the
raid-stripe-tree when a read on an RAID volume formatted with the
raid-stripe-tree was attempted.
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
fs/btrfs/raid-stripe-tree.c | 65 +++++++++++++++++++++++++++++++++++++
fs/btrfs/raid-stripe-tree.h | 3 ++
fs/btrfs/volumes.c | 22 +++++++++++--
3 files changed, 88 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/raid-stripe-tree.c b/fs/btrfs/raid-stripe-tree.c
index a673aaf8e703..5ee630a792fc 100644
--- a/fs/btrfs/raid-stripe-tree.c
+++ b/fs/btrfs/raid-stripe-tree.c
@@ -7,6 +7,7 @@
#include "disk-io.h"
#include "raid-stripe-tree.h"
#include "volumes.h"
+#include "misc.h"
static struct rb_node *stripe_tree_insert(struct rb_root *root, u64 logical,
struct rb_node *node)
@@ -251,3 +252,67 @@ void btrfs_raid_stripe_update(struct work_struct *work)
btrfs_put_bioc(bioc);
}
+int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
+ u64 logical, u64 length, u64 map_type,
+ u64 devid, u64 *physical)
+{
+ struct btrfs_root *stripe_root = fs_info->stripe_root;
+ struct btrfs_dp_stripe *raid_stripe;
+ struct btrfs_key stripe_key;
+ struct btrfs_key found_key;
+ struct btrfs_path *path;
+ struct extent_buffer *leaf;
+ u64 offset;
+ u64 found_logical, found_length;
+ int num_stripes;
+ int slot;
+ int ret;
+ int i;
+
+ stripe_key.objectid = logical;
+ stripe_key.type = BTRFS_RAID_STRIPE_KEY;
+ stripe_key.offset = length;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ num_stripes = btrfs_bg_type_to_factor(map_type);
+
+ ret = btrfs_search_slot_for_read(stripe_root, &stripe_key, path, 0, 0);
+ if (ret < 0) {
+ goto out;
+ }
+
+ if (ret == 1)
+ ret = 0;
+
+ while (1) {
+ leaf = path->nodes[0];
+ slot = path->slots[0];
+
+ btrfs_item_key_to_cpu(leaf, &found_key, slot);
+ found_logical = found_key.objectid;
+ found_length = found_key.offset;
+
+ if (!in_range(logical, found_logical, found_length))
+ goto next;
+ offset = logical - found_logical;
+
+ raid_stripe = btrfs_item_ptr(leaf, slot, struct btrfs_dp_stripe);
+ for (i = 0; i < num_stripes; i++) {
+ if (btrfs_stripe_extent_devid_nr(leaf, raid_stripe, i) != devid)
+ continue;
+ *physical = btrfs_stripe_extent_physical_nr(leaf, raid_stripe, i) + offset;
+ goto out;
+ }
+next:
+ ret = btrfs_next_item(stripe_root, path);
+ if (ret)
+ break;
+ }
+out:
+ btrfs_free_path(path);
+
+ return ret;
+}
diff --git a/fs/btrfs/raid-stripe-tree.h b/fs/btrfs/raid-stripe-tree.h
index d3cc24e37de1..75e17cad283a 100644
--- a/fs/btrfs/raid-stripe-tree.h
+++ b/fs/btrfs/raid-stripe-tree.h
@@ -16,6 +16,9 @@ struct btrfs_ordered_stripe {
refcount_t ref;
};
+int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
+ u64 logical, u64 length, u64 map_type,
+ u64 devid, u64 *physical);
int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start,
u64 length);
int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index b8d4e92c7196..2569ef564c97 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -6526,9 +6526,27 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
}
for (i = 0; i < num_stripes; i++) {
- bioc->stripes[i].physical = map->stripes[stripe_index].physical +
- stripe_offset + stripe_nr * map->stripe_len;
+ u64 physical;
+
bioc->stripes[i].dev = map->stripes[stripe_index].dev;
+
+ if (fs_info->stripe_root && op == BTRFS_MAP_READ &&
+ btrfs_need_stripe_tree_update(bioc->fs_info,
+ map->type)) {
+ ret = btrfs_get_raid_extent_offset(fs_info, logical,
+ map->stripe_len,
+ map->type,
+ bioc->stripes[i].dev->devid,
+ &physical);
+ if (ret) {
+ btrfs_put_bioc(bioc);
+ goto out;
+ }
+ } else {
+ physical = map->stripes[stripe_index].physical +
+ stripe_offset + stripe_nr * map->stripe_len;
+ }
+ bioc->stripes[i].physical = physical;
stripe_index++;
}
--
2.35.3
next prev parent reply other threads:[~2022-06-29 14:41 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-29 14:41 [PATCH RFC v2 0/8] btrfs: raid-stripe-tree draft patches Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 1/8] btrfs: add raid stripe tree definitions Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 2/8] btrfs: read raid-stripe-tree from disk Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 3/8] btrfs: add boilerplate code to insert raid extent Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 4/8] btrfs: add boilerplate code to insert stripe entries for preallocated extents Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 5/8] btrfs: add code to delete raid extent Johannes Thumshirn
2022-06-29 14:41 ` Johannes Thumshirn [this message]
2022-06-29 14:41 ` [PATCH RFC v2 7/8] btrfs: zoned: allow zoned RAID1 Johannes Thumshirn
2022-06-29 14:41 ` [PATCH RFC v2 8/8] btrfs: add raid stripe tree pretty printer Johannes Thumshirn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1f61c93be9354db6e53da4f404aa0f129f43a1e0.1656513330.git.johannes.thumshirn@wdc.com \
--to=johannes.thumshirn@wdc.com \
--cc=damien.lemoal@wdc.com \
--cc=hch@lst.de \
--cc=josef@toxicpanda.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=naohiro.aota@wdc.com \
--cc=wqu@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).