覆盖ext4上的一个小文件是原子的吗? [英] Is overwriting a small file atomic on ext4?

查看:59
本文介绍了覆盖ext4上的一个小文件是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个 FILE_SIZE 个字节的文件,并且:

Assume we have a file of FILE_SIZE bytes, and:

  • FILE_SIZE< =分钟(页面大小,物理块大小);
  • 文件大小永远不会改变(即永远不会执行 truncate()或追加 write()))
  • 文件只能通过以下方式完全覆盖其内容来修改:

  • FILE_SIZE <= min(page_size, physical_block_size);
  • file size never changes (i.e. truncate() or append write() are never performed);
  • file is modified only by completly overwriting its contents using:


pwrite(fd, buf, FILE_SIZE, 0);

是否在 ext4 上保证:

  1. 相对于并发读取,这种写入是原子的吗?
  2. 这样的写是否与系统崩溃有关?

  1. Such writes are atomic with respect to concurrent reads?
  2. Such writes are transactional with respect to a system crash?

(即,崩溃后文件的内容完全来自先前的写入操作,我们将永远不会看到部分写入或空文件)

(i.e., after a crash the file's contents is completely from some previous write and we'll never see a partial write or empty file)

是第二个事实:

  • 具有 data = ordered ?
  • 具有 data = journal 还是为单个文件启用了日记功能?

  • with data=ordered?
  • with data=journal or alternatively with journaling enabled for a single file?

(使用 ioctl(fd,EXT4_IOC_SETFLAGS,EXT4_JOURNAL_DATA_FL))

physical_block_size<FILE_SIZE< = page_size ?

我发现了相关问题,它链接了从2011年开始讨论.但是:

I've found related question which links discussion from 2011. However:

  • 我没有找到我的问题 2 的明确答案.
  • 我想知道,如果上面的说法是正确的,是否在某个地方记录了?
  • I didn't find an explicit answer for my question 2.
  • I wonder, if the above is true, is it documented somewhere?

推荐答案

从我的实验来看,它不是原子的.

From my experiment it was not atomic.

基本上,我的实验是有两个过程,一个作家和一个读者.编写器以循环方式写入文件,而读取器从文件中读取

Basically my experiment was to have two processes, one writer and one reader. The writer writes to a file in a loop and reader reads from the file

作家过程:

char buf[][18] = {
    "xxxxxxxxxxxxxxxx",
    "yyyyyyyyyyyyyyyy"
};
i = 0;
while (1) {
   pwrite(fd, buf[i], 18, 0);
   i = (i + 1) % 2;
}

阅读器进程

while(1) {
    pread(fd, readbuf, 18, 0);
    //check if readbuf is either buf[0] or buf[1]
}

在运行两个进程一段时间后,我可以看到 readbuf xxxxxxxxxxxxxxxxxxyy yyyyyyyyyyyyyyyxx .

After a while of running both processes, I could see that the readbuf is either xxxxxxxxxxxxxxxxyy or yyyyyyyyyyyyyyyyxx.

因此,它明确表明该写入不是原子的.在我的情况下,16字节写入始终是原子的.

So it definitively shows that the writes are not atomic. In my case 16byte writes were always atomic.

答案是:POSIX并不要求对原子进行读写操作(管道除外).我看到的16字节原子性是特定于内核的,将来可能会改变.

The answer was: POSIX doesn't mandate atomicity for writes/reads except for pipes. The 16 byte atomicity that I saw was kernel specific and may/can change in future.

实际帖子中答案的详细信息: linux中进程之间的write(2)/read(2)原子性

Details of the answer in the actual post: write(2)/read(2) atomicity between processes in linux

这篇关于覆盖ext4上的一个小文件是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆