8

Assuming

  • we have huge file F.
  • we like to put "around" seek s a new empty (filled with zeros) block in O(1) time (i.e. without rewriting all remining part)
  • "around" means we can round s to nearest filesystem blocksisze, and block to insert could be also filesystem blocksize

Is there a systemcall/filesystem that would allow such?

If not, is it good idea to mimic such behaviour using btrfs_clone (mentined here ) and how?

  • 1
    If you only modify, you can use dd with seek, see http://stackoverflow.com/a/17203140/3812704 . However, if you add a block, that will only work with xfs (linux >= 4.1) or ext4 (linux >= 4.2)as per other answer there http://stackoverflow.com/a/33339013/3812704 – Dani_l May 07 '16 at 11:12
  • see this answer. – meuh May 07 '16 at 15:38

1 Answers1

3

Quote from this answer :

As of Linux 4.1, fallocate(2) supports the FALLOC_FL_INSERT_RANGE flag, which allows one to insert a hole of a given length in the middle of a file without rewriting the following data. However, it is rather limited: the hole must be inserted at a filesystem block boundary, and the size of the inserted hole must be a multiple of the filesystem block size. Additionally, in 4.1, this feature was only supported by the XFS filesystem, with Ext4 support added in 4.2.

From fallocate(1) :

fallocate -d [-o offset] [-l length] filename
(...)
       -d, --dig-holes
              Detect  and  dig  holes.   This  makes  the  file  sparse in-place, without using extra disk space.  The minimum size of the hole depends on
              filesystem I/O block size (usually 4096 bytes).  Also, when using this option, --keep-size is implied.  If no range is specified by --offset
              and --length, then the entire file is analyzed for holes.

              You  can  think  of  this option as doing a "cp --sparse" and then renaming the destination file to the original, without the need for extra
              disk space.

              See --punch-hole for a list of supported filesystems.
(...)
       -p, --punch-hole
(...)
              Supported for XFS (since Linux 2.6.38), ext4 (since Linux 3.0), Btrfs (since Linux 3.7) and tmpfs (since Linux 3.5).