17

Are unlink, fsync, and rename the only ones that are by definition atomic?

Edit: atomic means that an operation either succeeds and has an effect or has fails and has no effect; an operation must not fail and have an effect.

clearly if the kernel panics, the program can't handle the error of the operation failing but it must consider in this case that it did fail

Dan D.
  • 73,243
  • 15
  • 104
  • 123

2 Answers2

20

Here's an article listing some atomic file operations:

http://rcrowley.org/2010/01/06/things-unix-can-do-atomically.html

mv, link, symlink, mkdir and some ways of opening files are atomic.

Kornel
  • 97,764
  • 37
  • 219
  • 309
3

I'm not sure fsync(2) is atomic; if a file has 100 megabytes dirty in the buffer cache, it'll take several seconds to write that data out, and the kernel may crash while the transfer to disk is in progress. Perhaps the DMA engine on board can only handle 4-megabyte writes. Perhaps there is no DMA support, and the CPU must schedule every write via 512 byte blocks.

What do you mean by 'atomic'?

mkdir is probably 'atomic', either the directory exists on disk and is linked in to a parent directory, or the directory data structure isn't yet linked into a parent directory, and is therefore unreachable --> doesn't exist.

Same might go for mount(2): it would be hard to find a mount(2) half-way complete, and if it fails, the entire mount fails: either the filesystem is mounted, or it isn't.

umount(2) is funny, it can be done lazily, but once it is unmounted, it cannot be used for open(2) or creat(2) calls.

So, I guess it comes down to, what do you mean by 'atomic'? :)

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 2
    AFAIK atomic operations CAN fail, but the next time the driver checks the filesystem it will revert those operations. AFAIK there were systems that kept a sort of journal with "started" and "finished" operations, if an operation is not "finished" but the filesystem is booting up it surely must be reverted! – EmmanuelMess Dec 31 '19 at 14:08