Efficiently inserting blocks into the middle of a file
Asked Answered
O

1

5

I'm looking for, essentially, the ext4 equivalent of mremap().

I have a big mmap()'d file that I'm allocating arrays in, and the arrays need to grow. So I want to make the first array larger at its current location, and budge all the other arrays along in the file and the address space to make room.

If this was just anonymous memory, I could use mremap() to budge over whole pages in constant time, as long as I'm inserting a whole number of memory pages. But this is a disk-backed file, so the data needs to move in the file as well as in memory.

I don't actually want to read and then rewrite whole blocks of data to and from the physical disk. I want the data to stay on disk in the physical sectors it is in, and to induce the filesystem to adjust the file metadata to insert new sectors where I need the extra space. If I have to keep my inserts to some multiple of a filesystem-dependent disk sector size, that's fine. If I end up having to copy O(N) sector or extent references around to make room for the inserted extent, that's fine. I just don't want to have 2 gigabytes move from and back to the disk in order to insert a block in the middle of a 4 gigabyte file.

How do I accomplish an efficient insert by manipulating file metadata? Is a general API for this actually exposed in Linux? Or one that works if the filesystem happens to be e.g. ext4? Will a write() call given a source address in the memory-mapped file reduce to the sort of efficient shift I want under the right circumstances?

Is there a C or C++ API function with the semantics "copy bytes from here to there and leave the source with an undefined value" that I should be calling in case this optimization gets added to the standard library and the kernel in the future?

I've considered just always allocating new pages at the end of the file, and mapping them at the right place in memory. But then I would need to work out some way to reconstruct that series of mappings when I reload the file. Also, shrinking the data structure would be a nontrivial problem. At that point, I would be writing a database page manager.

Onshore answered 8/12, 2020 at 19:41 Comment(0)
O
7

I think I actually may have figured it out.

I went looking for "linux make a file sparse", which led me to this answer on Unix & Linux Stack Exchange which mentioned the fallocate command line tool. The fallocate tool has a --dig-holes option, which turns parts of a file that could be represented by holes into holes.

I then went looking for "fallocate dig holes" to find out how that works, and I got the fallocate man page. I noticed it also offers a way to insert a hole of some size:

-i, --insert-range
              Insert a hole of length bytes from offset, shifting existing
              data.

If a command line tool can do it, Linux can do it, so I dug into the source code for fallocate, which you can find on Github:

case 'i':
    mode |= FALLOC_FL_INSERT_RANGE;
    break;

It looks like the fallocate tool accomplishes a cheap hole insert (and a move of all the other file data) by calling the fallocate() Linux-specific function with the FALLOC_FL_INSERT_RANGE flag, added in Linux 4.1. This flag won't work on all filesystems, but it does work on ext4 and it does exactly what I want: adjust the file metadata to efficiently free up some space in the file's offset space at a certain point.

It's not immediately clear to me how this interacts with currently memory-mapped pages, but I think I can work with this.

Onshore answered 8/12, 2020 at 20:7 Comment(3)
Hey, I'm wondering, did it work out? Were you able to insert a page and have it available in mmap?Dame
Unfortunately I never tested this with mmap; I ended up adjusting my data structures in the file to let me actually just always add new space at the end instead.Onshore
I can't imagine it would just grow the mmap from the beginning/middle. That would be very messy for anyone holding references to the map.Dame

© 2022 - 2024 — McMap. All rights reserved.