Introduce durable_rename() and durable_link_or_rename().
authorAndres Freund
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
committerAndres Freund
Thu, 10 Mar 2016 02:53:54 +0000 (18:53 -0800)
commite069848a3966bf64b4f4dc24d66a353d50878312
tree0fbd6e7814b5767f5f7fa19ec079206fb8cec389
parent1ab7a160f9d983ba738022c0b4dc62a67848b932
Introduce durable_rename() and durable_link_or_rename().

Renaming a file using rename(2) is not guaranteed to be durable in face
of crashes; especially on filesystems like xfs and ext4 when mounted
with data=writeback. To be certain that a rename() atomically replaces
the previous file contents in the face of crashes and different
filesystems, one has to fsync the old filename, rename the file, fsync
the new filename, fsync the containing directory.  This sequence is not
generally adhered to currently; which exposes us to data loss risks. To
avoid having to repeat this arduous sequence, introduce
durable_rename(), which wraps all that.

Also add durable_link_or_rename(). Several places use link() (with a
fallback to rename()) to rename a file, trying to avoid replacing the
target file out of paranoia. Some of those rename sequences need to be
durable as well. There seems little reason extend several copies of the
same logic, so centralize the link() callers.

This commit does not yet make use of the new functions; they're used in
a followup commit.

Author: Michael Paquier, Andres Freund
Discussion: 56583BDD.9060302@2ndquadrant.com
Backpatch: All supported branches
src/backend/storage/file/fd.c
src/backend/storage/file/reinit.c
src/include/storage/fd.h