feat: better rtc framework which aims to remove single rtc restrictions.
[lunaix-os.git] / lunaix-os / kernel / block / block.c
index 6203fc71a6862891000457bc64ddecce559f7bea..460e94bf3c4690394b4bf2240113ace6ae5a5397 100644 (file)
@@ -1,16 +1,18 @@
-#include <hal/ahci/hba.h>
 #include <klibc/stdio.h>
 #include <klibc/string.h>
+
+#include <hal/ahci/hba.h>
+
 #include <lib/crc.h>
+
+#include <lunaix/blkpart_gpt.h>
 #include <lunaix/block.h>
 #include <lunaix/fs/twifs.h>
 #include <lunaix/mm/cake.h>
+#include <lunaix/mm/page.h>
 #include <lunaix/mm/valloc.h>
-#include <lunaix/syslog.h>
-
-#include <lunaix/blkpart_gpt.h>
-
 #include <lunaix/spike.h>
+#include <lunaix/syslog.h>
 
 #define BLOCK_EREAD 1
 #define BLOCK_ESIG 2
@@ -24,6 +26,7 @@ LOG_MODULE("BLOCK")
 static struct cake_pile* lbd_pile;
 static struct block_dev** dev_registry;
 static struct twifs_node* blk_sysroot;
+static struct device* blk_parent_dev;
 
 int free_slot = 0;
 
@@ -41,6 +44,7 @@ block_init()
     dev_registry = vcalloc(sizeof(struct block_dev*), MAX_DEV);
     free_slot = 0;
     blk_sysroot = twifs_dir_node(NULL, "block");
+    blk_parent_dev = device_addcat(NULL, "block");
 }
 
 int
@@ -140,6 +144,72 @@ __block_write(struct device* dev, void* buf, size_t offset, size_t len)
     return errno;
 }
 
+int
+__block_read_page(struct device* dev, void* buf, size_t offset)
+{
+    struct vecbuf* vbuf = NULL;
+    struct block_dev* bdev = (struct block_dev*)dev->underlay;
+
+    u32_t lba = offset / bdev->blk_size + bdev->start_lba;
+    u32_t rd_lba = MIN(lba + PG_SIZE / bdev->blk_size, bdev->end_lba);
+
+    if (rd_lba <= lba) {
+        return 0;
+    }
+
+    rd_lba -= lba;
+
+    vbuf_alloc(&vbuf, buf, rd_lba * bdev->blk_size);
+
+    struct blkio_req* req = blkio_vrd(vbuf, lba, NULL, NULL, 0);
+
+    blkio_commit(bdev->blkio, req, BLKIO_WAIT);
+
+    int errno = req->errcode;
+    if (!errno) {
+        errno = rd_lba * bdev->blk_size;
+    } else {
+        errno = -errno;
+    }
+
+    blkio_free_req(req);
+    vbuf_free(vbuf);
+    return errno;
+}
+
+int
+__block_write_page(struct device* dev, void* buf, size_t offset)
+{
+    struct vecbuf* vbuf = NULL;
+    struct block_dev* bdev = (struct block_dev*)dev->underlay;
+
+    u32_t lba = offset / bdev->blk_size + bdev->start_lba;
+    u32_t rd_lba = MIN(lba + PG_SIZE / bdev->blk_size, bdev->end_lba);
+
+    if (rd_lba <= lba) {
+        return 0;
+    }
+
+    rd_lba -= lba;
+
+    vbuf_alloc(&vbuf, buf, rd_lba * bdev->blk_size);
+
+    struct blkio_req* req = blkio_vwr(vbuf, lba, NULL, NULL, 0);
+
+    blkio_commit(bdev->blkio, req, BLKIO_WAIT);
+
+    int errno = req->errcode;
+    if (!errno) {
+        errno = rd_lba * bdev->blk_size;
+    } else {
+        errno = -errno;
+    }
+
+    blkio_free_req(req);
+    vbuf_free(vbuf);
+    return errno;
+}
+
 int
 __block_rd_lb(struct block_dev* bdev, void* buf, u64_t start, size_t count)
 {
@@ -248,9 +318,12 @@ __block_register(struct block_dev* bdev)
         return 0;
     }
 
-    struct device* dev = device_addvol(NULL, bdev, "sd%c", 'a' + free_slot);
-    dev->write = __block_write;
-    dev->read = __block_read;
+    struct device* dev =
+      device_addvol(blk_parent_dev, bdev, "sd%c", 'a' + free_slot);
+    dev->ops.write = __block_write;
+    dev->ops.write_page = __block_write_page;
+    dev->ops.read = __block_read;
+    dev->ops.read_page = __block_read_page;
 
     bdev->dev = dev;
     strcpy(bdev->bdev_id, dev->name_val);
@@ -270,8 +343,10 @@ blk_mount_part(struct block_dev* bdev,
 
     struct device* dev =
       device_addvol(NULL, pbdev, "%sp%d", bdev->bdev_id, index + 1);
-    dev->write = __block_write;
-    dev->read = __block_read;
+    dev->ops.write = __block_write;
+    dev->ops.write_page = __block_write_page;
+    dev->ops.read = __block_read;
+    dev->ops.read_page = __block_read_page;
 
     pbdev->start_lba = start_lba;
     pbdev->end_lba = end_lba;