如何在 POSIX 中持久地重命名文件?
在 POSIX 文件系统中持久重命名文件的正确方法是什么?特别想知道目录上的 fsync。 (如果这取决于操作系统/文件系统,我问的是 Linux 和 ext3/ext4)。
注意:StackOverflow 上还有其他关于持久重命名的问题,但据我所知,他们没有解决目录的 fsync 问题(这对我来说很重要 - 我什至没有修改文件数据)。
我目前有(在Python中):
dstdirfd = open(dstdirpath, O_DIRECTORY|O_RDONLY)
rename(srcdirpath + '/' + filename, dstdirpath + '/' + filename)
fsync(dstdirfd)
具体问题:
- 这是否也隐式同步源目录?或者我最终可能会在电源循环后文件显示在两个目录中(这意味着我必须检查硬链接计数并手动执行恢复),即不可能保证持久的原子移动操作?
- 如果我 fsync 源目录而不是目标目录,是否也会隐式 fsync 目标目录?
- 是否有任何有用的相关测试/调试/学习工具(故障注入器、内省工具、模拟文件系统等)?
提前致谢。
What's the correct way to durably rename a file in a POSIX file system? Specifically wondering about fsyncs on the directories. (If this depends on the OS/FS, I'm asking about Linux and ext3/ext4).
Note: there are other questions on StackOverflow about durable renames, but AFAICT they don't address fsync-ing the directories (which is what matters to me - I'm not even modifying file data).
I currently have (in Python):
dstdirfd = open(dstdirpath, O_DIRECTORY|O_RDONLY)
rename(srcdirpath + '/' + filename, dstdirpath + '/' + filename)
fsync(dstdirfd)
Specific questions:
- Does this also implicitly fsync the source directory? Or might I end up with the file showing up in both directories after a power cycle (meaning I'd have to check the hard link count and manually perform recovery), i.e. it's impossible to guarantee a durably atomic move operation?
- If I fsync the source directory instead of the destination directory, will that also implicitly fsync the destination directory?
- Are there any useful related testing/debugging/learning tools (fault injectors, introspection tools, mock filesystems, etc.)?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,Dave 的答案是错误的。
并非所有 POSIX 系统都具有持久存储。如果他们这样做了,系统崩溃后仍然“允许”对其进行冲洗。对于这些系统,无操作 fsync() 是有意义的,并且 POSIX 下明确允许此类 fsync()。文件在旧目录、新目录、两者或任何其他位置中可恢复也是合法的。 POSIX 不保证系统崩溃或文件系统恢复。
真正的问题应该是:
如何在通过 POSIX API 支持的系统上进行持久重命名?
您需要在源和目标上执行 fsync()目录,因为这些 fsync() 至少应该做的是保持源目录或目标目录的外观。
fsync(destdirfd) 是否也隐式同步源目录?
或者我最终可能会在电源循环后文件显示在两个目录中(“崩溃”),即不可能保证持久的原子移动操作?
如果我 fsync 源目录而不是目标目录,是否也会隐式 fsync 目标目录?
是否有任何有用的相关测试/调试/学习工具(故障注入器、内省工具、模拟文件系统等)?
对于真正的崩溃,不会。顺便说一句,真正的崩溃超出了内核的视角。硬件可能会重新排序写入(并且无法写入所有内容),从而损坏文件系统。 Ext4 对此做好了更好的准备,因为它默认启用写入限制(挂载选项)(ext3 则不启用),并且可以通过日志校验和(也是一个挂载选项)检测损坏。
为了学习:找出这两个变化是否在日志中以某种方式联系在一起! :-P
Unfortunately Dave’s answer is wrong.
Not all POSIX systems might even have a durable storage. And if they do, it is still “allowed” to be hosed after a system crash. For those systems a no-op fsync() makes sense, and such fsync() is explicitly allowed under POSIX. It is also legal for the file to be recoverable in the old directory, the new directory, both, or any other location. POSIX makes no guarantees for system crashes or file system recoveries.
The real question should be:
How to do a durable rename on systems which support that through the POSIX API?
You need to do a fsync() on both, source and destination directory, because the minimum those fsync()s are supposed to do is persist how source or destination directory should look like.
Does a fsync(destdirfd) also implicitly fsync the source directory?
Or might I end up with the file showing up in both directories after a power cycle (“crash”), i.e. it's impossible to guarantee a durably atomic move operation?
If I fsync the source directory instead of the destination directory, will that also implicitly fsync the destination directory?
Are there any useful related testing/debugging/learning tools (fault injectors, introspection tools, mock filesystems, etc.)?
For a real crash, no. By the way, a real crash goes beyond the viewpoint of the kernel. The hardware might reorder writes (and fail to write everything), corrupting the filesystem. Ext4 is better prepared against this, because it enables write barries (mount options) by default (ext3 does not) and can detect corruption with journal checksums (also a mount option).
And for learning: find out if both changes are somehow linked in the journal! :-P
POSIX 定义重命名函数必须是原子的。
因此,如果您重命名(A,B),在任何情况下您都不会看到文件在两个目录中或两个目录中都没有的状态。无论您如何使用 fsync() 或者系统是否崩溃,总会有一个。
但这并不能解决确保 rename() 操作持久的问题。 POSIX 回答了这个问题:
因此,如果您 fsync() 一个目录,则挂起的重命名操作必须在返回时传输到磁盘。任一目录的 fsync() 都应该足够了,因为 rename() 操作的原子性要求两个目录的更改以原子方式同步。
最后,与另一个答案中提到的博客文章中的主张相反,其基本原理解释如下:
一个声称符合 POSIX 标准并且认为完成 fsync() 且不会在系统崩溃时保留这些更改的正确行为(即不是错误或硬件故障)的系统必须在规范方面故意歪曲自己。
(更新了附加信息:Linux 特定与可移植行为)
POSIX defines that the rename function must be atomic.
So if you rename(A, B), under no circumstances should you ever see a state with the file in both directories or neither directory. There will always be exactly one, no matter what you do with fsync() or whether the system crashes.
But that doesn't solve the problem of making sure the rename() operation is durable. POSIX answers this question:
So if you fsync() a directory, pending rename operations must be transferred to disk by the time this returns. fsync() of either directory should be sufficient because atomicity of the rename() operation would require that both directories' changes be synced atomically.
Finally, in contrast to the claim in the blog post mentioned in another answer, the rationale for this explains the following:
A system that claimed to be POSIX compliant and that considered it correct behavior (i.e. not a bug or hardware failure) to complete an fsync() and not persist those changes across a system crash would have to be deliberately misrepresenting itself with respect to the spec.
(updated with additional info re: Linux-specific vs. portable behavior)
您问题的答案在很大程度上取决于所使用的特定操作系统、所使用的文件系统类型以及源和目标是否位于同一设备上。
我首先阅读您正在使用的平台上的 rename(2) 手册页。
The answer to your question is going to depend a lot on the specific OS being used, the type of filesystem being used and whether the source and dest are on the same device or not.
I'd start by reading the rename(2) man page on the platform you're using.
在我看来,你正在尝试完成文件系统的工作。如果您移动文件,则内核和文件系统将负责原子操作和故障恢复,而不是您的代码。
无论如何,这篇文章似乎解决了您有关 fsync 的问题:
http://blogs.gnome.org/ alexl/2009/03/16/ext4-vs-fsync-my-take/
It sounds to me like you're trying to do the job of the filesystem. If you move a file the kernel and file-system are responsible for atomic operation and fault-recovery, not your code.
Anyway, this article seems to address your questions regarding fsync:
http://blogs.gnome.org/alexl/2009/03/16/ext4-vs-fsync-my-take/