diff mbox series

[RFC,1/9] block: add llseek(SEEK_HOLE/SEEK_DATA) support

Message ID 20240328203910.2370087-2-stefanha@redhat.com (mailing list archive)
State Deferred, archived
Delegated to: Mike Snitzer
Headers show
Series block: add llseek(SEEK_HOLE/SEEK_DATA) support | expand

Commit Message

Stefan Hajnoczi March 28, 2024, 8:39 p.m. UTC
The SEEK_HOLE/SEEK_DATA interface is used by userspace applications to
detect sparseness. This makes copying and backup applications faster and
reduces space consumption because only ranges that do not contain data
can be skipped.

Handle SEEK_HOLE/SEEK_DATA for block devices. No block drivers implement
the new callback yet so the entire block device will appear to contain
data. Later patches will add support to drivers so this actually becomes
useful.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 include/linux/blkdev.h |  7 +++++++
 block/fops.c           | 43 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)

Comments

Eric Blake March 28, 2024, 11:50 p.m. UTC | #1
On Thu, Mar 28, 2024 at 04:39:02PM -0400, Stefan Hajnoczi wrote:
> The SEEK_HOLE/SEEK_DATA interface is used by userspace applications to
> detect sparseness. This makes copying and backup applications faster and
> reduces space consumption because only ranges that do not contain data
> can be skipped.

s/only //

> 
> Handle SEEK_HOLE/SEEK_DATA for block devices. No block drivers implement
> the new callback yet so the entire block device will appear to contain
> data. Later patches will add support to drivers so this actually becomes
> useful.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  include/linux/blkdev.h |  7 +++++++
>  block/fops.c           | 43 +++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 49 insertions(+), 1 deletion(-)
>
diff mbox series

Patch

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index c3e8f7cf96be9..eecfbf9c27fc4 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -332,6 +332,9 @@  int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
 int blk_revalidate_disk_zones(struct gendisk *disk,
 		void (*update_driver_data)(struct gendisk *disk));
 
+loff_t blkdev_seek_hole_data(struct block_device *bdev, loff_t offset,
+		int whence);
+
 /*
  * Independent access ranges: struct blk_independent_access_range describes
  * a range of contiguous sectors that can be accessed using device command
@@ -1432,6 +1435,10 @@  struct block_device_operations {
 	 * driver.
 	 */
 	int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
+
+	/* Like llseek(SEEK_HOLE/SEEK_DATA). This callback may be NULL. */
+	loff_t (*seek_hole_data)(struct block_device *bdev, loff_t offset,
+			int whence);
 };
 
 #ifdef CONFIG_COMPAT
diff --git a/block/fops.c b/block/fops.c
index 679d9b752fe82..8ffbfec6b4c25 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -523,6 +523,43 @@  const struct address_space_operations def_blk_aops = {
 };
 #endif /* CONFIG_BUFFER_HEAD */
 
+/* Like llseek(SEEK_HOLE/SEEK_DATA) */
+loff_t blkdev_seek_hole_data(struct block_device *bdev, loff_t offset,
+		int whence)
+{
+	const struct block_device_operations *fops = bdev->bd_disk->fops;
+	loff_t size;
+
+	if (fops->seek_hole_data)
+		return fops->seek_hole_data(bdev, offset, whence);
+
+	size = bdev_nr_bytes(bdev);
+
+	switch (whence) {
+	case SEEK_DATA:
+		if ((unsigned long long)offset >= size)
+			return -ENXIO;
+		return offset;
+	case SEEK_HOLE:
+		if ((unsigned long long)offset >= size)
+			return -ENXIO;
+		return size;
+	default:
+		return -EINVAL;
+	}
+}
+
+static loff_t blkdev_llseek_hole_data(struct file *file, loff_t offset,
+		int whence)
+{
+	struct block_device *bdev = file_bdev(file);
+
+	offset = blkdev_seek_hole_data(bdev, offset, whence);
+	if (offset >= 0)
+		offset = vfs_setpos(file, offset, bdev_nr_bytes(bdev));
+	return offset;
+}
+
 /*
  * for a block special file file_inode(file)->i_size is zero
  * so we compute the size by hand (just as in block_read/write above)
@@ -533,7 +570,11 @@  static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
 	loff_t retval;
 
 	inode_lock(bd_inode);
-	retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
+	if (whence == SEEK_HOLE || whence == SEEK_DATA)
+		retval = blkdev_llseek_hole_data(file, offset, whence);
+	else
+		retval = fixed_size_llseek(file, offset, whence,
+					   i_size_read(bd_inode));
 	inode_unlock(bd_inode);
 	return retval;
 }