【问题标题】:Does fsync(fd) work on a file created by external program?fsync(fd) 是否适用于外部程序创建的文件?
【发布时间】:2015-05-11 20:12:52
【问题描述】:

我有一个禁用写入缓存的 SATA 硬盘:

hdparm -W0 /dev/foo

我正在使用这些挂载选项(以及其他选项)在 ext4 分区上进行操作:

data=ordered
auto_da_alloc

Linux 内核版本为2.6.32-5-686

现在,我有一个无法修改的外部程序,但我知道它会通过以下方式创建一个文件:

int fd = open(path);
write(fd, data, data_size);
close(fd);

即它在关闭之前不同步。所以此时,数据可能在 RAM 中,在内核/fs 缓存中的某个位置。

注意:元数据还不是问题:最终的元数据将在我确定数据已到达磁盘盘之后写入和同步。数据本身就是问题。

所以问题是,我怎样才能帮助数据到达实际的磁盘盘片?

我想过以后运行这个单独的程序:

int fd = open(path);
fsync(fd);
close(fd);

这将有助于刷新数据,还是我应该使用不同的方法?

【问题讨论】:

    标签: linux ext4 fsync sata


    【解决方案1】:

    这是否有助于刷新数据,

    是的,谁做 fsync 无关紧要。

    请注意,您可能还希望同步文件所在的目录,以便同步文件的元数据。

    【讨论】:

      【解决方案2】:

      来自man fsync

      Calling fsync() does not necessarily ensure that the entry in the
      directory containing the file has also reached disk.  For that an
      explicit fsync() on a file descriptor for the directory is also
      needed.
      

      【讨论】:

        猜你喜欢
        • 2020-02-05
        • 2016-06-25
        • 2013-12-02
        • 2015-07-25
        • 2015-03-11
        • 2012-04-06
        • 2020-07-11
        • 2023-03-29
        • 2011-10-19
        相关资源
        最近更新 更多