From: Tobias Waldekranz <tobias@waldekranz.com>
To: barebox@lists.infradead.org
Subject: [PATCH 01/11] dm: Add helper to manage a lower device
Date: Thu, 18 Sep 2025 09:43:11 +0200 [thread overview]
Message-ID: <20250918074455.891780-2-tobias@waldekranz.com> (raw)
In-Reply-To: <20250918074455.891780-1-tobias@waldekranz.com>
With the upcoming support for dm-verity, some patterns started to emerge
about commonalities between target types when it comes to dealing with
lower devices:
- Transparently creating/removing loop devices when a regular file is
passed in place of a device special
- Validation of offsets and sizes
- Reading in chunks of block sizes (possibly different from 512B)
Therefore, add a generic way of dealing with this that all targets can
hook into.
Signed-off-by: Tobias Waldekranz <tobias@waldekranz.com>
---
drivers/block/dm/dm-core.c | 118 +++++++++++++++++++++++++++++++++++
drivers/block/dm/dm-target.h | 20 ++++++
2 files changed, 138 insertions(+)
diff --git a/drivers/block/dm/dm-core.c b/drivers/block/dm/dm-core.c
index 953673073b..1bc3446cb0 100644
--- a/drivers/block/dm/dm-core.c
+++ b/drivers/block/dm/dm-core.c
@@ -7,11 +7,129 @@
#include <stdio.h>
#include <string.h>
#include <xfuncs.h>
+#include <unistd.h>
#include <linux/kstrtox.h>
+#include <linux/stat.h>
#include "dm-target.h"
+int dm_cdev_read(struct dm_cdev *dmcdev, void *buf, sector_t block,
+ blkcnt_t num_blocks)
+{
+ ssize_t n;
+
+ n = cdev_read(dmcdev->cdev, buf, num_blocks << dmcdev->blk.bits,
+ (dmcdev->blk.start + block) << dmcdev->blk.bits, 0);
+ if (n < 0)
+ return n;
+
+ if (n < (num_blocks << dmcdev->blk.bits))
+ return -EIO;
+
+ return 0;
+}
+
+int dm_cdev_write(struct dm_cdev *dmcdev, const void *buf, sector_t block,
+ blkcnt_t num_blocks)
+{
+ ssize_t n;
+
+ n = cdev_write(dmcdev->cdev, buf, num_blocks << dmcdev->blk.bits,
+ (dmcdev->blk.start + block) << dmcdev->blk.bits, 0);
+ if (n < 0)
+ return n;
+
+ if (n < (num_blocks << dmcdev->blk.bits))
+ return -EIO;
+
+ return 0;
+}
+
+int dm_cdev_open(struct dm_cdev *dmcdev, const char *path, ulong flags,
+ sector_t start, blkcnt_t num_blocks, size_t blocksize, char **errmsg)
+{
+ struct stat st;
+ int err;
+
+ memset(dmcdev, 0, sizeof(*dmcdev));
+
+ err = stat(path, &st);
+ if (err) {
+ *errmsg = xasprintf("Cannot determine type: %m");
+ return err;
+ }
+
+ switch (st.st_mode & S_IFMT) {
+ case S_IFREG:
+ dmcdev->cdev = cdev_create_loop(path, flags, 0);
+ if (!dmcdev->cdev) {
+ *errmsg = xstrdup("Cannot create loop device");
+ return -ENODEV;
+ }
+ dmcdev->loop = true;
+ break;
+ case S_IFBLK:
+ case S_IFCHR:
+ dmcdev->cdev = cdev_open_by_path_name(path, flags);
+ if (!dmcdev->cdev) {
+ *errmsg = xstrdup("Cannot open device");
+ return -ENODEV;
+ }
+
+ dmcdev->cdev = cdev_readlink(dmcdev->cdev);
+ break;
+ default:
+ *errmsg = xstrdup("Only regular files and device specials are supported");
+ return -EINVAL;
+ }
+
+ if (blocksize == 0 || (blocksize & (blocksize - 1))) {
+ *errmsg = xasprintf("Invalid block size: %zu is not a power of 2",
+ blocksize);
+ goto err;
+ }
+
+ dmcdev->blk.bits = ffs(blocksize) - 1;
+ if (dmcdev->blk.bits < SECTOR_SHIFT) {
+ *errmsg = xasprintf("Invalid block size: %zu, must be at least %u",
+ blocksize, SECTOR_SIZE);
+ goto err;
+ }
+
+ dmcdev->blk.mask = (1 << (dmcdev->blk.bits - SECTOR_SHIFT)) - 1;
+
+ if (((start + num_blocks) << dmcdev->blk.bits) > dmcdev->cdev->size) {
+ *errmsg = xstrdup("# of blocks is larger than device");
+ err = -E2BIG;
+ goto err;
+ }
+
+ dmcdev->blk.start = start;
+ dmcdev->blk.num = num_blocks;
+ return 0;
+err:
+ if (dmcdev->cdev) {
+ if (dmcdev->loop)
+ cdev_remove_loop(dmcdev->cdev);
+ else
+ cdev_close(dmcdev->cdev);
+
+ memset(dmcdev, 0, sizeof(*dmcdev));
+ }
+ return err;
+}
+
+void dm_cdev_close(struct dm_cdev *dmcdev)
+{
+ if (dmcdev->loop)
+ cdev_remove_loop(dmcdev->cdev);
+ else
+ cdev_close(dmcdev->cdev);
+
+ memset(dmcdev, 0, sizeof(*dmcdev));
+}
+
static LIST_HEAD(dm_target_ops_list);
static struct dm_target_ops *dm_target_ops_find(const char *name)
diff --git a/drivers/block/dm/dm-target.h b/drivers/block/dm/dm-target.h
index 506e808b79..bd25208889 100644
--- a/drivers/block/dm/dm-target.h
+++ b/drivers/block/dm/dm-target.h
@@ -4,6 +4,26 @@
#ifndef __DM_TARGET_H
#define __DM_TARGET_H
+struct dm_cdev {
+ struct cdev *cdev;
+ bool loop;
+
+ struct {
+ sector_t start;
+ blkcnt_t num;
+ u32 mask;
+ u8 bits;
+ } blk;
+};
+
+int dm_cdev_read(struct dm_cdev *dmc, void *buf, sector_t block,
+ blkcnt_t num_blocks);
+int dm_cdev_write(struct dm_cdev *dmc, const void *buf, sector_t block,
+ blkcnt_t num_blocks);
+int dm_cdev_open(struct dm_cdev *dmcdev, const char *path, ulong flags,
+ sector_t start, blkcnt_t num_blocks, size_t blocksize, char **errmsg);
+void dm_cdev_close(struct dm_cdev *dmcdev);
+
struct dm_device;
struct dm_target_ops;
--
2.43.0
next prev parent reply other threads:[~2025-09-18 7:45 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-18 7:43 [PATCH 00/11] dm: verity: Add transparent integrity checking target Tobias Waldekranz
2025-09-18 7:43 ` Tobias Waldekranz [this message]
2025-09-18 7:43 ` [PATCH 02/11] dm: linear: Refactor to make use of the generalized cdev management Tobias Waldekranz
2025-09-18 7:43 ` [PATCH 03/11] dm: verity: Add transparent integrity checking target Tobias Waldekranz
2025-09-18 13:06 ` Sascha Hauer
2025-09-18 7:43 ` [PATCH 04/11] dm: verity: Add helper to parse superblock information Tobias Waldekranz
2025-09-18 7:43 ` [PATCH 05/11] commands: veritysetup: Create dm-verity devices Tobias Waldekranz
2025-09-18 7:43 ` [PATCH 06/11] ci: pytest: Open up testfs to more consumers than the FIT test Tobias Waldekranz
2025-09-22 15:38 ` Ahmad Fatoum
2025-09-18 7:43 ` [PATCH 07/11] ci: pytest: Enable testfs feature on malta boards Tobias Waldekranz
2025-09-22 15:40 ` Ahmad Fatoum
2025-09-18 7:43 ` [PATCH 08/11] ci: pytest: Generate test data for dm-verity Tobias Waldekranz
2025-09-22 15:41 ` Ahmad Fatoum
2025-09-18 7:43 ` [PATCH 09/11] test: pytest: add basic dm-verity test Tobias Waldekranz
2025-09-22 15:44 ` Ahmad Fatoum
2025-09-18 7:43 ` [PATCH 10/11] ci: pytest: Centralize feature discovery to a separate step Tobias Waldekranz
2025-09-22 15:45 ` Ahmad Fatoum
2025-09-18 7:43 ` [PATCH 11/11] ci: pytest: Enable device-mapper labgrid tests Tobias Waldekranz
2025-09-22 15:46 ` Ahmad Fatoum
2025-09-18 14:08 ` [PATCH 00/11] dm: verity: Add transparent integrity checking target Sascha Hauer
2025-09-18 15:38 ` Tobias Waldekranz
2025-09-23 6:30 ` Sascha Hauer
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=20250918074455.891780-2-tobias@waldekranz.com \
--to=tobias@waldekranz.com \
--cc=barebox@lists.infradead.org \
/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