Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What filesystem operations are required to be atomic?

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

like image 263
Dan D. Avatar asked Mar 08 '11 11:03

Dan D.


People also ask

How are atomic operations used in operating systems?

Atomic operations are often used in the kernel, the primary component of most operating systems. However, most computer hardware, compilers and libraries also provide varying levels of atomic operations. In loading and storing, computer hardware carries out writing and reading to a word-sized memory.

What are atomic operations in C++?

Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.

What does atomic mean in Linux?

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 Show activity on this post.

What file types are atomic in Linux?

mv, link, symlink, mkdir and some ways of opening files are atomic. Show activity on this post. 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.


2 Answers

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.

like image 83
Kornel Avatar answered Oct 19 '22 16:10

Kornel


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'? :)

like image 1
sarnold Avatar answered Oct 19 '22 17:10

sarnold