From 7b0701707857a3205cbd4fc69c08429a193c5bff Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 16 Aug 2025 20:23:15 -0400 Subject: kernel: disk: Introduce virtual blocks This commit implements the concept of virtual blocks which may be bigger than the hardware block size (e.g., 512 bytes). The virtual block size must be a multiple of the hardware blocksize. The system will panic if this condition is not met. Signed-off-by: Ian Moffett --- sys/include/sys/disk.h | 24 ++++++++++++++++++++++++ sys/kern/kern_disk.c | 14 ++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/sys/include/sys/disk.h b/sys/include/sys/disk.h index f6ed126..e503e21 100644 --- a/sys/include/sys/disk.h +++ b/sys/include/sys/disk.h @@ -33,9 +33,33 @@ #include #include #include +#include #include #include #include +#if defined(_KERNEL) +#include +#endif /* _KERNEL */ + +/* + * V_BSIZE is the virtual block size in bytes used + * by the disk framework. The virtual block size is a + * multiple of the hardware block size and defines + * how many bytes a virtual block is made up of. + * + * A virtual block is simply a unit specific to + * the disk framework that represents multiple + * hardware disk blocks. + */ +#if defined(__V_BSIZE) +#define V_BSIZE __V_BSIZE +#else +#define V_BSIZE 4096 +#endif /* __V_BSIZE */ + +/* Sanitize the silly human's input */ +_Static_assert(V_BSIZE > 512, "V_BSIZE must be > 512"); +_Static_assert((V_BSIZE & 1) == 0, "V_BSIZE must be a power of two"); #define DISK_PRIMARY 0 /* ID of primary disk */ diff --git a/sys/kern/kern_disk.c b/sys/kern/kern_disk.c index b956c02..af2aa13 100644 --- a/sys/kern/kern_disk.c +++ b/sys/kern/kern_disk.c @@ -171,6 +171,10 @@ __disk_get_id(diskid_t id) * @buf: Buffer to read data into * @len: Number of bytes to read * @write: If true, do a write + * + * XXX: The size in which blocks are read at is in + * virtual blocks which is defined by V_BSIZE + * in sys/disk.h */ static ssize_t disk_rw(diskid_t id, blkoff_t blk, void *buf, size_t len, bool write) @@ -180,6 +184,8 @@ disk_rw(diskid_t id, blkoff_t blk, void *buf, size_t len, bool write) struct disk *dp; int error; + len = ALIGN_UP(len, V_BSIZE); + /* Attempt to grab the disk object */ error = disk_get_id(id, &dp); if (error < 0) { @@ -268,6 +274,10 @@ disk_add(const char *name, dev_t dev, const struct bdevsw *bdev, int flags) dp->id = disk_count++; dp->bsize = DEFAULT_BSIZE; + if ((V_BSIZE & (dp->bsize - 1)) != 0) { + panic("virtual block size not hw bsize aligned\n"); + } + /* Now we can add it to the queue */ spinlock_acquire(&diskq_lock); TAILQ_INSERT_TAIL(&diskq, dp, link); @@ -343,9 +353,9 @@ disk_buf_alloc(diskid_t id, size_t len) /* * Here we will align the buffer size by the - * disk's block size to ensure it is big enough. + * virtual block size to ensure it is big enough. */ - len = ALIGN_UP(len, dp->bsize); + len = ALIGN_UP(len, V_BSIZE); buf = dynalloc(len); return buf; } -- cgit v1.2.3