6 ms·
As a retrofit, that never worked out. I've suggested before that files should be divided into several types - unit, log, temp, and managed. This also addresse
by Animats 10y ago
As a retrofit, that never worked out.
I've suggested before that files should be divided into several types - unit, log, temp, and managed. This also addresses the locking problem.
Unit files are always updated as a unit - once written and closed, they can't be changed, only replaced. Using "creat" creates a new file, which replaces the old one on clean close. A crash or a program abort reverts to the old file. So they implicitly have a form of mandatory locking. Should two processes be able to create new updates at the same time? Probably not. This is the default kind of file.
Log files should be append-only. Multiple users can write, but only at the end. That deals with the locking problem there. That's what open for append should do.
Temp files disappear at reboot, and should have N-readers or 1 writer locking. Anything in /tmp gets this treatment.
Managed files are for databases. Shared access and partial file locking is supported. Managed files support an API where you get a callback when the data has been safely committed to disk, something ACID databases care about. Only a few programs use managed files, and you know which ones they are.
Managed file mode has to be explicitly turned on for a file.
That's how locking ought to work.
- sargun 10y agoHave you tried proposing this to be included in POSIX? Or tried implementing this in the VFS layer in Linux? I imagine all of these could be xattrs.
- Animats 10y agoThe usual objections are: - Unit file updates are not needed because of (complex non-portable workaround for atomic file replacement). - It's not backwards compatible with NFS. - Lock files are good enough. - If I want to seek while adding to a log file and rewrite in the middle, I should be allowed to do so. Database people like the idea of a callback when data is safely on disk. They work hard to get the effect of that, and struggle with "flush" calls either flushing too much, or return when the data reaches the drive controller, not the disk surface. Doing this right needs file system and driver support, though.
- daurnimator 10y ago> - Unit file updates are not needed because of (complex non-portable workaround for atomic file replacement). I agree with all your points except this. What is wrong with creating the file somewhere else and using rename() to atomically overwrite?
- FooBarWidget 10y agoBecause that is not portable and/or has performance problems. I'm not kidding you. I know the conventional wisdom is that a rename is enough. It is not. Depending on the filesystem, you may have to fsync() the directory entry too, or fsync the parent directory entry too. Or on other filesystems, don't do these because doing these may cause problems. And do all this in the right order, because if you get it slightly wrong then it doesn't do what you want in the event of a crash. The problem is maddening. There is an article out there that explains all this in much more detail. I think it is this one but I don't quite remember whether it is the same as the one I read: http://blog.httrack.com/blog/2013/11/15/everything-you-always-wanted-to-know-about-fsync/ http://blog.httrack.com/blog/2013/11/15/everything-you-alway... The performance problem comes from the fact that you need two fscyncs.
- tedunangst 10y agoMost of this is pretty easy to achieve. "Unit" files are just a temp file and rename away. Append only files have existed in BSD since forever (and honestly, they're more pita than anything).
- bogomipz 10y agoI'm not familiar with the term "unit" in this context, could you elaborate on this as a type?
- js2 10y agoAtomic. Either the old file or the new file is seen for a given path. Posix has this semantics already if you always write to a temp file on the same FS and then rename (I think this may not be true if the system crashes however, and it's not true for NFS).
- bogomipz 10y agoAh ok, unit as in an "atomic unit." Thanks.
- Animats 10y agoThat's the problem. Doesn't work for NFS. Rename is not atomic on some file systems. Crashes may leave temp files behind. Moving files from one directory to another may not be atomic. Renaming API for NTFS differs for different versions of Windows. See this two-year discussion of trying to get this to work right for the Python library.[1] Do not taunt Happy Fun Ball. This should Just Work. [1] https://bugs.python.org/issue8828 https://bugs.python.org/issue8828
- bogomipz 10y ago>"Moving files from one directory to another may not be atomic." I was intrigued that you said "may not", can you elaborate on when it is vs when it isn't? >"Do not taunt Happy Fun Ball." This really made me laugh :)
- lloeki 10y agoOn OS X / HFS+ it wasn't atomic before Snow Leopard, and bug-free before Lion[0] [0]: http://www.weirdnet.nl/apple/rename.html http://www.weirdnet.nl/apple/rename.html
- leoc 10y agoAre you familiar with the old Namesys scheme for transactions? "Transcrashes" and so on...
- peterwwillis 10y agoI don't understand, are you proposing some new kernel modes for i/o? Because the log file thing probably doesn't work as I don't think O_APPEND prevents multiple processes from clobbering each other's writes. And O_SYNC seems to do what your api/callback thing would do. And this isn't enough locking methods to satisfy all applications.
- Animats 10y agoO_APPEND semantics are not consistent for all systems when multiple processes are writing. In early UNIX systems, there was one seek pointer for processes which shared an open file descriptor. This is why multiple commands launched by the shell can write to the same standard output file without overwriting. But if two processes open the same file for append, in some older versions of Unix they can interfere. POSIX does not guarantee atomicity here. Linux does, mostly, except on NFS. Previous discussion on HN.[1] This, too, should Just Work. [1] https://news.ycombinator.com/item?id=12220489 https://news.ycombinator.com/item?id=12220489