dev: implement reading from block devices

reading from block devices is done using the block cache (bcache).
This cache stores sectors from a block device in pages of memory
marked as 'cached', which will allow them to be reclaimed when
memory pressure is high (TODO).

while block device drivers implement callbacks allowing reading/writing
at block-granularity, the device subsystem uses the block cache to
implement reading/writing at byte-granularity in a driver-agnostic way.

block drivers can disable the block cache for their devices, but this
will require that any clients communicate with the devices at
block-granularity.

also added an offset parameter to device and object read/write functions/callbacks.
This commit is contained in:
2023-07-09 21:58:40 +01:00
parent 53440653f2
commit 3233169f25
14 changed files with 435 additions and 52 deletions

View File

@@ -6,6 +6,7 @@
#include <socks/status.h>
#include <socks/bitmap.h>
#include <socks/object.h>
#include <socks/block.h>
#include <socks/fb.h>
#include <socks/ringbuffer.h>
@@ -49,14 +50,14 @@ struct iovec {
};
struct device_type_ops {
kern_status_t(*read)(struct device *, void *, size_t, size_t *, socks_flags_t);
kern_status_t(*write)(struct device *, const void *, size_t, size_t *, socks_flags_t);
kern_status_t(*read)(struct device *, void *, size_t, size_t, size_t *, socks_flags_t);
kern_status_t(*write)(struct device *, const void *, size_t, size_t, size_t *, socks_flags_t);
kern_status_t(*register_device)(struct device *);
};
struct block_device_ops {
kern_status_t(*read_blocks)(struct device *, sectors_t, size_t, struct iovec *, size_t, socks_flags_t);
kern_status_t(*write_blocks)(struct device *, sectors_t, size_t, struct iovec *, size_t, socks_flags_t);
kern_status_t(*read_blocks)(struct device *, sectors_t, size_t *, struct iovec *, size_t, socks_flags_t);
kern_status_t(*write_blocks)(struct device *, sectors_t, size_t *, struct iovec *, size_t, socks_flags_t);
kern_status_t(*ioctl)(struct device *, unsigned int, void *);
};
@@ -68,8 +69,8 @@ struct net_device_ops {
};
struct char_device_ops {
kern_status_t(*read)(struct device *, void *, size_t, size_t *, socks_flags_t);
kern_status_t(*write)(struct device *, const void *, size_t, size_t *, socks_flags_t);
kern_status_t(*read)(struct device *, void *, size_t, size_t, size_t *, socks_flags_t);
kern_status_t(*write)(struct device *, const void *, size_t, size_t, size_t *, socks_flags_t);
};
struct input_device_ops {
@@ -86,9 +87,11 @@ struct framebuffer_device_ops {
struct block_device {
struct block_device_ops *b_ops;
struct bcache b_cache;
enum block_device_flags b_flags;
unsigned int b_id;
unsigned int sector_size;
sectors_t capacity;
unsigned int b_sector_size;
sectors_t b_capacity;
};
struct char_device {
@@ -195,8 +198,8 @@ static inline void device_unlock_irqrestore(struct device *dev, unsigned long fl
object_unlock_irqrestore(&dev->dev_base, flags);
}
extern kern_status_t device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags);
extern kern_status_t device_write(struct device *dev, const void *buf, size_t size, size_t *bytes_written, socks_flags_t flags);
extern kern_status_t device_read(struct device *dev, void *buf, size_t offset, size_t size, size_t *bytes_read, socks_flags_t flags);
extern kern_status_t device_write(struct device *dev, const void *buf, size_t offset, size_t size, size_t *bytes_written, socks_flags_t flags);
extern struct device *cast_to_device(struct object *obj);
@@ -286,7 +289,8 @@ static inline void device_deref(struct device *dev)
}
extern kern_status_t input_device_report_event(struct input_device *dev, const struct input_event *ev, bool noblock);
extern kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags);
extern kern_status_t input_device_read(struct device *dev, void *buf, size_t offset,
size_t size, size_t *bytes_read, socks_flags_t flags);
extern kern_status_t input_device_add_hook(struct device *dev, struct input_event_hook *hook);
extern kern_status_t input_device_remove_hook(struct device *dev, struct input_event_hook *hook);