X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/8ce769cc52e91ff3cdb8eda4b1f5d8fe58241688..69777bdcab284335651a8002e2896f3862fa423d:/lunaix-os/kernel/block/block.c diff --git a/lunaix-os/kernel/block/block.c b/lunaix-os/kernel/block/block.c index 6203fc7..8ca5117 100644 --- a/lunaix-os/kernel/block/block.c +++ b/lunaix-os/kernel/block/block.c @@ -1,16 +1,18 @@ -#include -#include +#include #include + +#include + #include + +#include #include #include #include +#include #include -#include - -#include - #include +#include #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_cat* blk_parent_dev; int free_slot = 0; @@ -41,6 +44,22 @@ 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"); +} + +static int +__block_commit(struct blkio_context* blkio, struct blkio_req* req, int flags) +{ + int errno; + blkio_commit(blkio, req, flags); + + if ((errno = req->errcode)) { + errno = -errno; + } + + vbuf_free(req->vbuf); + blkio_free_req(req); + return errno; } int @@ -74,21 +93,16 @@ __block_read(struct device* dev, void* buf, size_t offset, size_t len) } req = blkio_vrd(vbuf, rd_block, NULL, NULL, 0); - blkio_commit(bdev->blkio, req, BLKIO_WAIT); - if (!(errno = req->errcode)) { + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { memcpy(buf, head_buf + r, rd_size); errno = len; - } else { - errno = -errno; } if (head_buf) { vfree(head_buf); } - blkio_free_req(req); - vbuf_free(vbuf); return errno; } @@ -122,21 +136,70 @@ __block_write(struct device* dev, void* buf, size_t offset, size_t len) } req = blkio_vwr(vbuf, wr_block, NULL, NULL, 0); - blkio_commit(bdev->blkio, req, BLKIO_WAIT); - int errno = req->errcode; - if (!errno) { + int errno; + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { errno = len; - } else { - errno = -errno; } if (tmp_buf) { vfree(tmp_buf); } - blkio_free_req(req); - vbuf_free(vbuf); + 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 + PAGE_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); + + int errno; + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { + errno = rd_lba * bdev->blk_size; + } + + 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 wr_lba = MIN(lba + PAGE_SIZE / bdev->blk_size, bdev->end_lba); + + if (wr_lba <= lba) { + return 0; + } + + wr_lba -= lba; + + vbuf_alloc(&vbuf, buf, wr_lba * bdev->blk_size); + + struct blkio_req* req = blkio_vwr(vbuf, lba, NULL, NULL, 0); + + int errno; + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { + errno = wr_lba * bdev->blk_size; + } + return errno; } @@ -147,18 +210,12 @@ __block_rd_lb(struct block_dev* bdev, void* buf, u64_t start, size_t count) vbuf_alloc(&vbuf, buf, bdev->blk_size * count); struct blkio_req* req = blkio_vrd(vbuf, start, NULL, NULL, 0); - blkio_commit(bdev->blkio, req, BLKIO_WAIT); - int errno = req->errcode; - if (!errno) { + int errno; + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { errno = count; - } else { - errno = -errno; } - blkio_free_req(req); - vbuf_free(vbuf); - return errno; } @@ -169,18 +226,12 @@ __block_wr_lb(struct block_dev* bdev, void* buf, u64_t start, size_t count) vbuf_alloc(&vbuf, buf, bdev->blk_size * count); struct blkio_req* req = blkio_vwr(vbuf, start, NULL, NULL, 0); - blkio_commit(bdev->blkio, req, BLKIO_WAIT); - int errno = req->errcode; - if (!errno) { + int errno; + if (!(errno = __block_commit(bdev->blkio, req, BLKIO_WAIT))) { errno = count; - } else { - errno = -errno; } - blkio_free_req(req); - vbuf_free(vbuf); - return errno; } @@ -225,7 +276,7 @@ block_mount(struct block_dev* bdev, devfs_exporter fs_export) errno = blkpart_probegpt(bdev->dev); if (errno < 0) { - kprintf(KERROR "Fail to parse partition table (%d)\n", errno); + ERROR("Fail to parse partition table (%d)", errno); } else if (!errno) { // TODO try other PT parser... } @@ -237,7 +288,7 @@ block_mount(struct block_dev* bdev, devfs_exporter fs_export) return errno; error: - kprintf(KERROR "Fail to mount block device: %s (%x)\n", bdev->name, -errno); + ERROR("Fail to mount block device: %s (%x)", bdev->name, -errno); return errno; } @@ -248,13 +299,19 @@ __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_allocvol(dev_meta(blk_parent_dev), bdev); + 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); + + register_device(dev, bdev->class, "sd%c", 'a' + free_slot); dev_registry[free_slot++] = bdev; + + strcpy(bdev->bdev_id, dev->name_val); + return 1; } @@ -268,10 +325,11 @@ blk_mount_part(struct block_dev* bdev, struct block_dev* pbdev = cake_grab(lbd_pile); memcpy(pbdev, bdev, sizeof(*bdev)); - struct device* dev = - device_addvol(NULL, pbdev, "%sp%d", bdev->bdev_id, index + 1); - dev->write = __block_write; - dev->read = __block_read; + struct device* dev = device_allocvol(NULL, pbdev); + 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; @@ -284,5 +342,7 @@ blk_mount_part(struct block_dev* bdev, llist_append(&bdev->parts, &pbdev->parts); + register_device(dev, pbdev->class, "%sp%d", bdev->bdev_id, index + 1); + return pbdev; } \ No newline at end of file