[PATCH v1 3/7] cbd: introduce cbd_segment

From: Dongsheng Yang
Date: Tue Jul 09 2024 - 09:05:01 EST


The `cbd_segments` is an abstraction of the data area in transport. The
data area in transport is divided into segments.

The specific use of this area is determined by `cbd_seg_type`. For example,
`cbd_blkdev` and `cbd_backend` data transfers
need to access a segment of the type `cbds_type_channel`.

The segment also allows for more scenarios and more segment types to be expanded.

Signed-off-by: Dongsheng Yang <dongsheng.yang@xxxxxxxxx>
---
drivers/block/cbd/cbd_segment.c | 108 ++++++++++++++++++++++++++++++++
1 file changed, 108 insertions(+)
create mode 100644 drivers/block/cbd/cbd_segment.c

diff --git a/drivers/block/cbd/cbd_segment.c b/drivers/block/cbd/cbd_segment.c
new file mode 100644
index 000000000000..855bfa473b4c
--- /dev/null
+++ b/drivers/block/cbd/cbd_segment.c
@@ -0,0 +1,108 @@
+#include "cbd_internal.h"
+
+static ssize_t cbd_seg_detail_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct cbd_segment_device *segment;
+ struct cbd_segment_info *segment_info;
+
+ segment = container_of(dev, struct cbd_segment_device, dev);
+ segment_info = segment->segment_info;
+
+ if (segment_info->state == cbd_segment_state_none)
+ return 0;
+
+ if (segment_info->type == cbds_type_channel)
+ return cbd_channel_seg_detail_show((struct cbd_channel_info *)segment_info, buf);
+
+ return 0;
+}
+
+static ssize_t cbd_seg_type_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct cbd_segment_device *segment;
+ struct cbd_segment_info *segment_info;
+
+ segment = container_of(dev, struct cbd_segment_device, dev);
+ segment_info = segment->segment_info;
+
+ if (segment_info->state == cbd_segment_state_none)
+ return 0;
+
+ return sprintf(buf, "%s\n", cbds_type_str(segment_info->type));
+}
+
+static DEVICE_ATTR(detail, 0400, cbd_seg_detail_show, NULL);
+static DEVICE_ATTR(type, 0400, cbd_seg_type_show, NULL);
+
+CBD_OBJ_HEARTBEAT(segment);
+
+static struct attribute *cbd_segment_attrs[] = {
+ &dev_attr_detail.attr,
+ &dev_attr_type.attr,
+ &dev_attr_alive.attr,
+ NULL
+};
+
+static struct attribute_group cbd_segment_attr_group = {
+ .attrs = cbd_segment_attrs,
+};
+
+static const struct attribute_group *cbd_segment_attr_groups[] = {
+ &cbd_segment_attr_group,
+ NULL
+};
+
+static void cbd_segment_release(struct device *dev)
+{
+}
+
+const struct device_type cbd_segment_type = {
+ .name = "cbd_segment",
+ .groups = cbd_segment_attr_groups,
+ .release = cbd_segment_release,
+};
+
+const struct device_type cbd_segments_type = {
+ .name = "cbd_segments",
+ .release = cbd_segment_release,
+};
+
+void cbd_segment_init(struct cbd_segment *segment, struct cbd_transport *cbdt, u32 seg_id)
+{
+ struct cbd_segment_info *segment_info = cbdt_get_segment_info(cbdt, seg_id);
+
+ segment->cbdt = cbdt;
+ segment->segment_info = segment_info;
+ segment->seg_id = seg_id;
+
+ segment_info->state = cbd_segment_state_running;
+
+ INIT_DELAYED_WORK(&segment->hb_work, segment_hb_workfn);
+ queue_delayed_work(cbd_wq, &segment->hb_work, 0);
+}
+
+void cbd_segment_exit(struct cbd_segment *segment)
+{
+ cancel_delayed_work_sync(&segment->hb_work);
+
+ segment->segment_info->state = cbd_segment_state_none;
+}
+
+int cbd_segment_clear(struct cbd_transport *cbdt, u32 seg_id)
+{
+ struct cbd_segment_info *segment_info;
+
+ segment_info = cbdt_get_segment_info(cbdt, seg_id);
+ if (cbd_segment_info_is_alive(segment_info)) {
+ cbdt_err(cbdt, "segment %u is still alive\n", seg_id);
+ return -EBUSY;
+ }
+
+ cbdt_zero_range(cbdt, segment_info, CBDT_SEG_SIZE);
+
+ return 0;
+}
--
2.34.1