[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240328203910.2370087-2-stefanha@redhat.com>
Date: Thu, 28 Mar 2024 16:39:02 -0400
From: Stefan Hajnoczi <stefanha@...hat.com>
To: linux-block@...r.kernel.org
Cc: linux-kernel@...r.kernel.org,
eblake@...hat.com,
Alasdair Kergon <agk@...hat.com>,
Mikulas Patocka <mpatocka@...hat.com>,
dm-devel@...ts.linux.dev,
David Teigland <teigland@...hat.com>,
Mike Snitzer <snitzer@...nel.org>,
Jens Axboe <axboe@...nel.dk>,
Christoph Hellwig <hch@....de>,
Joe Thornber <ejt@...hat.com>,
Stefan Hajnoczi <stefanha@...hat.com>
Subject: [RFC 1/9] block: add llseek(SEEK_HOLE/SEEK_DATA) support
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@...hat.com>
---
include/linux/blkdev.h | 7 +++++++
block/fops.c | 43 +++++++++++++++++++++++++++++++++++++++++-
2 files changed, 49 insertions(+), 1 deletion(-)
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;
}
--
2.44.0
Powered by blists - more mailing lists