1

I'm writing my own data store directly on top of a block device. To ensure durability I want to sync to disk. But here's the thing: I want to sync only part of it.

I'm keeping a journal for crash recovery, and write my future changes to the journal before applying them to the actual place on disk. Then I want to ensure the journal changes are written to disk, and only then make the actual changes to the rest of the disk (which I don't care about fsyncing, until I checkpoint my journal).

I could simply fsync the entire block device, but that forces a lot of things that aren't urgent to be written out.

I have thought of two options, but I'm surprised there is no partial fsync(2) call and nobody asking for it from what I've found.

  1. mmap(2) the full block device and use msync(2) to sync part of it.
  2. open(2) the block device twice, once with O_SYNC and use one for lazy writes and one for my journal writes.
3
  • This looks like a question for stackoverflow.com Commented Jun 20, 2023 at 18:43
  • @ArtemS.Tashkinov I say you're right, that's definitely a software programming question, but also, it asks about specific operating system capabilities of Linux, and or POSIX, so I think it's actually very well here :) Commented Jun 20, 2023 at 19:04
  • Msync just synchronizes memory view and filesystem view of the opened file. Does it really enforce a flush to disk? Commented Jun 20, 2023 at 19:15

1 Answer 1

1

There is a Linux-specific system call: sync_file_range()

(Sidenote, using block devices is not portable to FreeBSD)

2
  • 1
    (Also note that sync_file_range() doesn't send write cache flushes like fsync() does so it doesn't offer the same durability guarantees) Commented Jul 17, 2024 at 8:16
  • 1
    Huh. "does not provide any data integrity on systems with volatile disk write caches". Which you should assume is generally the case, unless you have disabled it etc. Commented Jul 17, 2024 at 8:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.