cp(1) and backup tools use llseek(SEEK_HOLE/SEEK_DATA) to skip holes in files. This can speed up the process by reducing the amount of data read and it preserves sparseness when writing to the output file. This patch series is an initial attempt at implementing llseek(SEEK_HOLE/SEEK_DATA) for block devices. I'm looking for feedback on this approach and suggestions for resolving the open issues. In the block device world there are similar concepts to holes: - SCSI has Logical Block Provisioning where the "mapped" state would be considered data and other states would be considered holes. - NBD has NBD_CMD_BLOCK_STATUS for querying whether blocks are present. - Linux loop block devices and dm-linear targets can pass through queries to the backing file. - dm-thin targets can query metadata to find holes. - ...and you may be able to think of more examples. Therefore it is possible to offer this functionality in block drivers. In my use case a QEMU process in userspace copies the contents of a dm-thin target. QEMU already uses SEEK_HOLE but that doesn't work on dm-thin targets without this patch series. Others have also wished for block device support for SEEK_HOLE. Here is an open issue from the BorgBackup project: https://github.com/borgbackup/borg/issues/5609 With these patches userspace can identify holes in loop, dm-linear, and dm-thin devices. This is done by adding a seek_hole_data() callback to struct block_device_operations. When the callback is NULL the entire device is considered data. Device-mapper is extended along the same lines so that targets can provide seek_hole_data() callbacks. I'm unfamiliar with much of this code and have probably missed locking requirements. Since llseek() executes synchronously like ioctl() and is not an asynchronous I/O request it's possible that my changes to the loop block driver and dm-thin are broken (e.g. what if the loop device fd is changed during llseek()?). To run the tests: # make TARGETS=block_seek_hole -C tools/testing/selftests run_tests The code is also available here: https://gitlab.com/stefanha/linux/-/tree/block-seek-hole Please take a look and let me know your thoughts. Thanks! Stefan Hajnoczi (9): block: add llseek(SEEK_HOLE/SEEK_DATA) support loop: add llseek(SEEK_HOLE/SEEK_DATA) support selftests: block_seek_hole: add loop block driver tests dm: add llseek(SEEK_HOLE/SEEK_DATA) support selftests: block_seek_hole: add dm-zero test dm-linear: add llseek(SEEK_HOLE/SEEK_DATA) support selftests: block_seek_hole: add dm-linear test dm thin: add llseek(SEEK_HOLE/SEEK_DATA) support selftests: block_seek_hole: add dm-thin test tools/testing/selftests/Makefile | 1 + .../selftests/block_seek_hole/Makefile | 17 +++ include/linux/blkdev.h | 7 ++ include/linux/device-mapper.h | 5 + block/fops.c | 43 ++++++- drivers/block/loop.c | 36 +++++- drivers/md/dm-linear.c | 25 ++++ drivers/md/dm-thin.c | 77 ++++++++++++ drivers/md/dm.c | 68 ++++++++++ .../testing/selftests/block_seek_hole/config | 3 + .../selftests/block_seek_hole/dm_thin.sh | 80 ++++++++++++ .../selftests/block_seek_hole/dm_zero.sh | 31 +++++ .../selftests/block_seek_hole/map_holes.py | 37 ++++++ .../testing/selftests/block_seek_hole/test.py | 117 ++++++++++++++++++ 14 files changed, 540 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/block_seek_hole/Makefile create mode 100644 tools/testing/selftests/block_seek_hole/config create mode 100755 tools/testing/selftests/block_seek_hole/dm_thin.sh create mode 100755 tools/testing/selftests/block_seek_hole/dm_zero.sh create mode 100755 tools/testing/selftests/block_seek_hole/map_holes.py create mode 100755 tools/testing/selftests/block_seek_hole/test.py -- 2.44.0