by jjnoakes · 10y ago · 0 comments
I thought that requirement was from POSIX.
Does xfs not conform to POSIX?
I think this is true in many filesytems, not just `xfs`.
If your atomicity requirement you never want the file to disappear from the POV of an external process, then the OP's method is sufficient. If you want crash-proofing as well, then you will need an fsync() -- preferrably on the tempfile BEFORE the rename().
d=open("."), unlink("t");
/* 1 */ symlink("new","t");
/* 2 */ rename("t", "link");
/* 3 */ fsync(d);
/* 4 */ close(d);
Crashing, at (1) nothing has happened yet, (2) we might have "t" or we might not, (3) we might have "t", or not, and we might have "link" pointing to "new" or "old", but we can't have "link" pointing to anything else (or empty), and finally at (4) the change cannot be reverted.You can insert a second fsync() where you suggest at point (2), but all this will guarantee is that we will have "t" in the directory because the symlink contents are part of the directory they live in. This might be useful for some applications, but the cost of two disk writes is high enough it may be worth redesigning your application.
Now on modern filesystems, a non-huge symlink will be stored in the inode itself and presumably enjoys some sort of atomicity. But there is nothing in the standard about that.
No, I don't think you can, bugs notwithstanding. A "link" (§3.130) is what POSIX calls a directory entry.
> But there is nothing in the standard about that.
The "standard" (POSIX) doesn't talk much about crashing, however if mkdir("a") could destroy "b" – even during a system crash (§3.387), then users would complain.
The way I understand the article's proposal is this:
1. Create new symlink pointing to desired file (assumed to already exist in a stable state).
2. Move new symlink over old symlink.
They exist as directory entries with a small in-directory content only, thus syncing the directory they exist in is sufficient to persist them.
The fact that it does or does not should actually be in the documentation IMHO; probably a good enhancement request.
POSIX does (for some strange reason) permit a symbolic link to have an "inode" (well, d_ino), but there is no way to open this "inode", and no UNIX implementations to my knowledge do this.
Edit: If you want to fsync the symlink you can do an open with nofollow iirc (on my phone so I can't check) so you get the actual symlink and not its target.
HOWEVER, looking into it it seems that the target may actually be stored in the inode. This mean the symlink, though a file, has no contents and thus requires no fsync. Does this sound about right?