From aa2df9c1bbf014e243abdc726aae593a7ef1e363 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 14 Aug 2025 22:45:47 -0400 Subject: kernel: Introduce disk management framework This commit introduces the initial Hyra disk management framework. The goal of this framework is to provide a simplistic API for the management of storage devices in a way that is more suitable for complex operations. Upon detection and initialization of a block-based storage medium, the driver may create a named disk object using the 'disk_add()' function. Disks that have been registered with the system may be acquired via the 'disk_get_id()' function. As of the current revision, each disk is given a monotonically incremental ID for identification and lookups. A disk ID of zero indicates a primary disk on the system. Disks may be written to or read from using the 'disk_read()' and 'disk_write()' functions. Memory buffers used for these operations must be created with the 'disk_buf_alloc()' function and freed with the 'disk_buf_free()' function. Signed-off-by: Ian Moffett --- sys/include/sys/disk.h | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 sys/include/sys/disk.h (limited to 'sys/include') diff --git a/sys/include/sys/disk.h b/sys/include/sys/disk.h new file mode 100644 index 0000000..09319f5 --- /dev/null +++ b/sys/include/sys/disk.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_DISK_H_ +#define _SYS_DISK_H_ + +#include +#include +#include +#include + +#define DISK_PRIMARY 0 /* ID of primary disk */ + +/* + * A disk identifier is a zero-based index into + * the disk registry. + */ +typedef uint16_t diskid_t; + +/* + * Block offset / LBA + */ +typedef off_t blkoff_t; + +/* + * Represents a block storage device + * + * @name: Name of disk + * @cookie: Used internally to ensure validity + * @bsize: Block size (defaults to 512 bytes) + * @dev: Device minor + * @id: Disk ID (zero-based index) + * @bdev: Block device operations + * @link: TAILQ link + */ +struct disk { + char name[NAME_MAX]; + uint32_t cookie; + uint16_t bsize; + dev_t dev; + diskid_t id; + const struct bdevsw *bdev; + TAILQ_ENTRY(disk) link; +}; + +void *disk_buf_alloc(diskid_t id, size_t len); +void disk_buf_free(void *p); + +ssize_t disk_read(diskid_t id, blkoff_t blk, void *buf, size_t len); +ssize_t disk_write(diskid_t id, blkoff_t blk, const void *buf, size_t len); + +int disk_add(const char *name, dev_t dev, const struct bdevsw *bdev, int flags); +int disk_get_id(diskid_t id, struct disk **res); + +#endif /* !_SYS_DISK_H_ */ -- cgit v1.2.3