1

Given any file on an ext4 filesystem, it is possible (using filefrag -v) to get the list of real offsets+lengths where that file is located on the underlying block device.

Is it safe to open the device and write to them, all that while the filesystem is mounted read-write? Can it cause fs corruption? I'm asking because I'm going to implement an alternative loop driver, which will bypass the filesystem layer completely, therefore having much better performance.

If I remember correctly, swapfile is implemented exactly that way. Please correct me if told something wrong.
Is the answer filesystem-dependent? What can happen if the file is suddenly deleted, and these offsets become reused for some metadata?
Finally, is there a way to lock a file from being relocated by e4defrag or similar things? What is the best way to prevent a file from being deleted (being in kernel space)? Is there some kernel internals I can use to get the list of file's extents?

1
  • If it is safe, there is even no need to implement a driver. We can just setup device mappings using dm-linear. But how to protect the file from being deleted or relocated by the FS layer? Commented Mar 18, 2024 at 18:11

1 Answer 1

1

If the writes are only to the blocks of the file, then it wouldn't corrupt the ext4 filesystem. However, there is a definitely a bigger risk that some error in the code could corrupt the filesystem, which wouldn't happen with a regular loop device that is only using the file mapping.

The question is whether writing directly to the block device will actually make a difference in performance?

You can prevent the file from being deleted by marking it immutable with chattr +i FILENAME.

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.