fs: Introduce discard_range()
Subsystems (2)
PATCH 1/4 fs: Introduce discard_range()
discard_range() is a way to tell the lower layers that we are no longer interested in a data range of a file, so that the lower layers can discard the underlying data if desired.
This is mainly designed to bypass the deficiencies of our block layer. We cache the block data in chunks of multiple KiB (16 currently) if we fall into the block layer with write requests smaller than that we have to read/modify/write a chunk. With the help of discard_range() code writing files to a raw block device can now discard the range the file will be written to. The block layer then no longer has to read the chunks first that are inside the discard range.
--- fs/fs.c | 25 +++++++++++++++++++++++++ include/fs.h | 3 +++ 2 files changed, 28 insertions(+)
fs/fs.c +25 -0
@@ -497,6 +497,31 @@ int protect(int fd, size_t count, loff_t offset, int prot) }EXPORT_SYMBOL (protect); +int discard_range (int fd,loff_t count,loff_t offset) +{ +struct fs_driver_d *fsdrv; +FILE *f =fd_to_file (fd); +int ret; + +if (IS_ERR (f)) +return -errno; +if (offset >= f->size) +return 0 ; +if (count > f->size - offset) + count = f->size - offset; + + fsdrv = f->fsdev->driver; +if (fsdrv->discard_range) + ret = fsdrv->discard_range (&f->fsdev->dev, f, count, offset); +else + ret = -ENOSYS ; + +if (ret) + errno = -ret; + +return ret; +} +int protect_file (const char *file,int prot) {int fd, ret;
include/fs.h +3 -0
@@ -60,6 +60,8 @@ struct fs_driver_d { loff_t offset);int (*protect)(struct device_d *dev,FILE *f,size_t count,loff_t offset,int prot); +int (*discard_range)(struct device_d *dev,FILE *f,loff_t count, +loff_t offset);int (*memmap)(struct device_d *dev,FILE *f,void **map,int flags);
@@ -127,6 +129,7 @@ int umount_by_cdev(struct cdev *cdev);#define ERASE_SIZE_ALL ((loff_t) - 1)int erase (int fd,loff_t count,loff_t offset);int protect (int fd,size_t count,loff_t offset,int prot); +int discard_range (int fd,loff_t count,loff_t offset);int protect_file (const char *file,int prot);void *memmap (int fd,int flags);
Signature · 7 lines
2.25.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/4 cdev: Add discard_range hook
To pass though discard_range() to the underlying drivers add a discard_range hook to struct cdev_operations.
--- fs/devfs.c | 21 +++++++++++++++++++++ include/driver.h | 1 + 2 files changed, 22 insertions(+)
fs/devfs.c +21 -0
@@ -94,6 +94,26 @@ static int devfs_protect(struct device_d *_dev, FILE *f, size_t count, loff_t ofreturn cdev->ops->protect (cdev, count, offset + cdev->offset, prot); } +static int devfs_discard_range (struct device_d *dev,FILE *f,loff_t count, +loff_t offset) +{ +struct cdev *cdev = f->priv; + +if (!cdev->ops->discard_range) +return -ENOSYS ; + +if (cdev->flags &DEVFS_PARTITION_READONLY ) +return -EPERM ; + +if (offset >= cdev->size) +return 0 ; + +if (count + offset > cdev->size) + count = cdev->size - offset; + +return cdev->ops->discard_range (cdev, count, offset + cdev->offset); +} +static int devfs_memmap (struct device_d *_dev,FILE *f,void **map,int flags) {struct cdev *cdev = f->priv;
@@ -329,6 +349,7 @@ static struct fs_driver_d devfs_driver = { .truncate = devfs_truncate, .erase = devfs_erase, .protect = devfs_protect, + .discard_range = devfs_discard_range, .memmap = devfs_memmap, .flags = FS_DRIVER_NO_DEV, .drv = {
include/driver.h +1 -0
@@ -441,6 +441,7 @@ struct cdev_operations {int (*flush)(struct cdev *);int (*erase)(struct cdev *,loff_t count,loff_t offset);int (*protect)(struct cdev *,size_t count,loff_t offset,int prot); +int (*discard_range)(struct cdev *,loff_t count,loff_t offset);int (*memmap)(struct cdev *,void **map,int flags);int (*truncate)(struct cdev *,size_t size); };
Signature · 7 lines
2.25.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/4 block: Implement discard_range
In reply to Sascha HauerThis implements the discard_range hook. When a range of data is discarded then we do not have to read it from the device and can pass a zeroed buffer instead.
--- common/block.c | 21 +++++++++++++++++++++ include/block.h | 3 +++ 2 files changed, 24 insertions(+)
common/block.c +21 -0
@@ -161,6 +161,14 @@ static int block_cache(struct block_device *blk, int block)dev_dbg (blk->dev,"%s: %d to %d\n" , __func__, chunk->block_start, chunk->num); +if (chunk->block_start *BLOCKSIZE (blk) >= blk->discard_start && + chunk->block_start *BLOCKSIZE (blk) +writebuffer_io_len (blk, chunk) + <= blk->discard_start + blk->discard_size) { +memset (chunk->data,0 ,writebuffer_io_len (blk, chunk)); +list_add (&chunk->list, &blk->buffered_blocks); +return 0 ; + } + ret = blk->ops->read (blk, chunk->data, chunk->block_start,writebuffer_io_len (blk, chunk));if (ret) {
@@ -337,11 +345,23 @@ static int block_op_flush(struct cdev *cdev) {struct block_device *blk = cdev->priv; + blk->discard_start = blk->discard_size =0 ; +return writebuffer_flush (blk); } static int block_op_close(struct cdev *cdev) __alias(block_op_flush); +static int block_op_discard_range (struct cdev *cdev,loff_t count,loff_t offset) +{ +struct block_device *blk = cdev->priv; + + blk->discard_start = offset; + blk->discard_size = count; + +return 0 ; +} +static struct cdev_operations block_ops = { .read = block_op_read, #ifdef CONFIG_BLOCK_WRITE
@@ -349,6 +369,7 @@ static struct cdev_operations block_ops = {#endif .close = block_op_close, .flush = block_op_flush, + .discard_range = block_op_discard_range, };int blockdevice_register (struct block_device *blk)
include/block.h +3 -0
@@ -23,6 +23,9 @@ struct block_device {int rdbufsize;int blkmask; +loff_t discard_start; +loff_t discard_size; +struct list_head buffered_blocks;struct list_head idle_blocks;
Signature · 7 lines
2.25.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 4/4 copy_file: call discard_range on destination file
In reply to Sascha Hauerdiscard the range in the output file we are going to overwrite anyway.
--- lib/libfile.c | 2 ++ 1 file changed, 2 insertions(+)
lib/libfile.c +2 -0
@@ -367,6 +367,8 @@ int copy_file(const char *src, const char *dst, int verbose) goto out; } +discard_range (dstfd, srcstat.st_size,0 ); +if (verbose) {if (stat (src, &srcstat) <0 ) srcstat.st_size =0 ;
Signature · 7 lines
2.25.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox