I am calling write(fd, buf, N) on a normal file descriptor.

The file pointer before calling is not at the beginning or end but instead somewhere in the middle of the file.

The call is than interrupted by a power cut (or the task is killed). Is there any guarantee about what will happen with the data in the file before and after the N bytes? Is it guaranteed they keep unchanged?

  • 2
    In general, after a power cut, no, there are no guarantees. In general, it's not guaranteed your filesystem is even usable after a power cut. In general, most standards and specifications don't say anything about any behaviour when the system isn't running. E.g. stuff like access controls are really hard to enforce when the system is shut down. Sure, relatively modern systems try to do at least something to keep things together, but it still depends on the OS, the filesystem and the storage device.
    – ilkkachu
    May 15 at 13:31
  • CoW filesystems will most likely "recover" with a completely empty file or no file at all on the next boot. Normal filesystems? It depends. The file could be partially restored into lost+found, you'll find it empty or you won't find it at all. Such a behavior is not defined. You cannot define something which is out of your control. In the past a power cut could physically destroy your HDD (the very first HDDs didn't park heads automatically). May 15 at 14:08

1 Answer 1


I see two different questions:

  1. What happens if the power is cut in the middle of a write?

There is no guarantee what happens on a power cut -- you could even lose the entire filesystem.

However, there are typical behaviors. If the filesystem is allocating new blocks for the written region, the filesystem metadata for the new blocks may not be written when the power is cut and the file may be lost or truncated. However, if you are writing to the middle of the file and the blocks are already allocated, it should be fine, but there is no guarantee how much of that write made it to disk. Could be some, all, or none.

As noted in comments, behavior may differ here for special filesystems like CoW or network filesystems. Journaled filesystems make an attempt at making recovery from issues like this more graceful. Also, raid arrays typically have buffering hardware that can hold incomplete writes across a power outage.

  1. What happens if the process is killed in the middle of a write?

There are two cases here. If you mean write(2) system call, this is mostly atomic, and it may complete before the process is actually killed, or it may not start at all. In some cases, the write may be interruptible, but usually not.

If you mean a library call such as fwrite(3) using stdio buffering, then the process may have unwritten buffered data that will not be written when the process is killed. Typically stdio buffers data to disk in blocks; when the buffer has complete blocks, it calls write(2).

  • Not just the filesystem, but the behaviour of the drive itself. One might hope drives would be smart enough to stop writing to the disk itself on a power cut, and not e.g. trash areas that weren't being written. But even that might leave some sectors/blocks in an inconsistent state wrt. error correction, so the data there might not be readable... I'm not sure if anyone has ever tested how different makes and models of HDDs and SSDs behave under hard power outs, but it would be an interesting read...
    – ilkkachu
    May 15 at 14:52
  • 1
    I've heard that some disks use the momentum of the platter to power the controller after a power outage and are able to flush their memory buffers to disk before it fully loses power.
    – user10489
    May 15 at 14:53
  • @user10489 some hard drives use their platters’ momentum to provide power for a controlled head retraction (I don’t have a link off-hand); see this Q&A on Retro.SE for a discussion of systems capable of writing after a power loss (with the help of capacitors, not the spinning platters’ momentum). High-end (“enterprise”) SSDs tend to have capacitors so that they can flush their DRAM caches to flash on power loss. May 15 at 15:45
  • Regarding journaling file systems, they are often configured to journal metadata only, so after a power loss the file might have the right attributes, but not the right contents. May 15 at 15:48
  • metadata journaling: Typically thre freelist updates are written to the journal, then the data is written, then the file metadata written. So likely if the file data is not written, the file metadata is not updated either. This would affect a growing file by truncating it, and and if pre-existing blocks are reallocated, it would leave the file with the old contents.
    – user10489
    May 15 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.