bio中除了可以使用真实的block dev,还可以使用memory中的block dev,这样每次可以读写block count 的数据,加快读写的速度。
int create_membdev(const char *name, void *ptr, size_t len)
{
mem_bdev_t *mem = malloc(sizeof(mem_bdev_t));
/* set up the base device */
bio_initialize_bdev(&mem->dev, name, BLOCKSIZE, len / BLOCKSIZE);
/* our bits */
mem->ptr = ptr;
mem->dev.read = mem_bdev_read;
mem->dev.read_block = mem_bdev_read_block;
mem->dev.write = mem_bdev_write;
mem->dev.write_block = mem_bdev_write_block;
/* register it */
bio_register_device(&mem->dev);
return 0;
}
create_membdev会创建一个membdev,和前面讲的一样先调用bio_initialize_bdev,然后调用bio_register_device
我们重点看看mem->dev.read是如何实现的
static ssize_t mem_bdev_read(bdev_t *bdev, void *buf, off_t offset, size_t len)
{
mem_bdev_t *mem = (mem_bdev_t *)bdev;
LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len);
memcpy(buf, (uint8_t *)mem->ptr + offset, len);
return len;
}
原来只要memcpy就搞定了,因为原本就在memory中呀,不牵扯真实的dev
前面讲了bio的两种case,实际使用的时候是使用subdev,是对真实dev的一层封装,我们看看具体是怎么封装的。
status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len)
{
LTRACEF("parent %s, sub %s, startblock %u, len %zd\n", parent_dev, subdev, startblock, len);
bdev_t *parent = bio_open(parent_dev);
if (!parent)
return -1;
/* make sure we're able to do this */
if (startblock + len > parent->block_count)
return -1;
subdev_t *sub = malloc(sizeof(subdev_t));
bio_initialize_bdev(&sub->dev, subdev, parent->block_size, len);
sub->parent = parent;
sub->offset = startblock;
sub->dev.read = &subdev_read;
sub->dev.read_block = &subdev_read_block;
sub->dev.write = &subdev_write;
sub->dev.write_block = &subdev_write_block;
sub->dev.erase = &subdev_erase;
sub->dev.close = &subdev_close;
bio_register_device(&sub->dev);
return 0;
}
原来直接使用bio_publish_subdevice,就可以了,不用在分两步了,bio_publish_subdevice里面已经调用了bio_initialize_bdev和bio_register_device
我们看看其subdev_read的实现
static ssize_t subdev_read(struct bdev *_dev, void *buf, off_t offset, size_t len)
{
subdev_t *subdev = (subdev_t *)_dev;
return bio_read(subdev->parent, buf, offset + subdev->offset * subdev->dev.block_size, len);
}
调用bio_read,继续看其实现
ssize_t bio_read(bdev_t *dev, void *buf, off_t offset, size_t len)
{
LTRACEF("dev '%s', buf %p, offset %lld, len %zd\n", dev->name, buf, offset, len);
DEBUG_ASSERT(dev->ref > 0);
/* range check */
if (offset < 0)
return -1;
if (offset >= dev->size)
return 0;
if (len == 0)
return 0;
if (offset + len > dev->size)
len = dev->size - offset;
return dev->read(dev, buf, offset, len);
}
最终还是调用dev->read
所以bio使用的时候直接调用bio_publish_subdevice就可以了
int create_membdev(const char *name, void *ptr, size_t len)
{
mem_bdev_t *mem = malloc(sizeof(mem_bdev_t));
/* set up the base device */
bio_initialize_bdev(&mem->dev, name, BLOCKSIZE, len / BLOCKSIZE);
/* our bits */
mem->ptr = ptr;
mem->dev.read = mem_bdev_read;
mem->dev.read_block = mem_bdev_read_block;
mem->dev.write = mem_bdev_write;
mem->dev.write_block = mem_bdev_write_block;
/* register it */
bio_register_device(&mem->dev);
return 0;
}
create_membdev会创建一个membdev,和前面讲的一样先调用bio_initialize_bdev,然后调用bio_register_device
我们重点看看mem->dev.read是如何实现的
static ssize_t mem_bdev_read(bdev_t *bdev, void *buf, off_t offset, size_t len)
{
mem_bdev_t *mem = (mem_bdev_t *)bdev;
LTRACEF("bdev %s, buf %p, offset %lld, len %zu\n", bdev->name, buf, offset, len);
memcpy(buf, (uint8_t *)mem->ptr + offset, len);
return len;
}
原来只要memcpy就搞定了,因为原本就在memory中呀,不牵扯真实的dev
前面讲了bio的两种case,实际使用的时候是使用subdev,是对真实dev的一层封装,我们看看具体是怎么封装的。
status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len)
{
LTRACEF("parent %s, sub %s, startblock %u, len %zd\n", parent_dev, subdev, startblock, len);
bdev_t *parent = bio_open(parent_dev);
if (!parent)
return -1;
/* make sure we're able to do this */
if (startblock + len > parent->block_count)
return -1;
subdev_t *sub = malloc(sizeof(subdev_t));
bio_initialize_bdev(&sub->dev, subdev, parent->block_size, len);
sub->parent = parent;
sub->offset = startblock;
sub->dev.read = &subdev_read;
sub->dev.read_block = &subdev_read_block;
sub->dev.write = &subdev_write;
sub->dev.write_block = &subdev_write_block;
sub->dev.erase = &subdev_erase;
sub->dev.close = &subdev_close;
bio_register_device(&sub->dev);
return 0;
}
原来直接使用bio_publish_subdevice,就可以了,不用在分两步了,bio_publish_subdevice里面已经调用了bio_initialize_bdev和bio_register_device
我们看看其subdev_read的实现
static ssize_t subdev_read(struct bdev *_dev, void *buf, off_t offset, size_t len)
{
subdev_t *subdev = (subdev_t *)_dev;
return bio_read(subdev->parent, buf, offset + subdev->offset * subdev->dev.block_size, len);
}
调用bio_read,继续看其实现
ssize_t bio_read(bdev_t *dev, void *buf, off_t offset, size_t len)
{
LTRACEF("dev '%s', buf %p, offset %lld, len %zd\n", dev->name, buf, offset, len);
DEBUG_ASSERT(dev->ref > 0);
/* range check */
if (offset < 0)
return -1;
if (offset >= dev->size)
return 0;
if (len == 0)
return 0;
if (offset + len > dev->size)
len = dev->size - offset;
return dev->read(dev, buf, offset, len);
}
最终还是调用dev->read
所以bio使用的时候直接调用bio_publish_subdevice就可以了