Skip to content

RFC block: add llseek(SEEK_HOLE/SEEK_DATA) support

Stefan Hajnoczi dm-devel linux-block lkml oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest9 patches 29 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/9block: add llseek(SEEK_HOLE/SEEK_DATA) supportblock/fops.c include/linux/blkdev.h+49 -11 reply
2/9loop: add llseek(SEEK_HOLE/SEEK_DATA) supportdrivers/block/loop.c+30 -62 replies
3/9selftests: block_seek_hole: add loop block driver teststools/testing/selftests/Makefile tools/testing/selftests/block_seek_hole/Makefile tools/testing/selftests/block_seek_hole/config +2 more+159 -04 replies
4/9dm: add llseek(SEEK_HOLE/SEEK_DATA) supportdrivers/md/dm.c include/linux/device-mapper.h+73 -05 replies
5/9selftests: block_seek_hole: add dm-zero testtools/testing/selftests/block_seek_hole/Makefile tools/testing/selftests/block_seek_hole/config tools/testing/selftests/block_seek_hole/dm_zero.sh+34 -12 replies
6/9dm-linear: add llseek(SEEK_HOLE/SEEK_DATA) supportdrivers/md/dm-linear.c+25 -03 replies
7/9selftests: block_seek_hole: add dm-linear testtools/testing/selftests/block_seek_hole/test.py+15 -12 replies
8/9dm thin: add llseek(SEEK_HOLE/SEEK_DATA) supportdrivers/md/dm-thin.c+77 -02 replies
9/9selftests: block_seek_hole: add dm-thin testtools/testing/selftests/block_seek_hole/Makefile tools/testing/selftests/block_seek_hole/dm_thin.sh+81 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC 0/9] block: add llseek(SEEK_HOLE/SEEK_DATA) support

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
Signature · 1 line
2.44.0