5

On Linux I can dd a file on my hard drive and delete it in Nautilus while the dd is still going on.

Can Linux enforce a mandatory file lock to protect R/W?

[EDIT] The original question wasn't about linux file locking capabilities but about a supposed bug in linux, reproducing it here as it is responded below and others may have the same question.

People keep telling me Linux/Unix is better OS. I am coding Java on Linux now and come across a problem, that I can easily reproduce: I can dd a file on my hard drive and delete it in Nautilus while the dd is still going on. How come linux cannot enforce a mandatory file lock to protect R/W??

olivecoder
  • 2,739
  • 21
  • 21
Frank
  • 6,637
  • 9
  • 44
  • 56
  • AFAIK, Linux has locks, for example you cannot open two package managers (Synaptic) at once. That is a matter of implementation by each application instead of via the OS, I think. – heltonbiker Aug 21 '12 at 20:25
  • It can. You'd have to use a system call to do it, it's something java might not support natively (without using native library integration and whatever else). The system call is fcntl or flock. – Wug Aug 21 '12 at 20:27

3 Answers3

16

To do mandatory locking on Linux, the filesystem must be mounted with the -o mand option, and you must set g-x,g+s permissions on the file. That is, you must disable group execute, and enable setgid. Once this is performed, all access will either block or error with EAGAIN based on the value of O_NONBLOCK on the file descriptor. But beware: "The implementation of mandatory locking in all known versions of Linux is subject to race conditions which render it unreliable... It is therefore inadvisable to rely on mandatory locking." See fcntl(2).

amoe
  • 4,123
  • 3
  • 29
  • 49
11

You don't need locking. This is not a bug but a choice, your assumptions are wrong.

The file system uses reference counting and it will mark a file as free only when all hard links to the file are removed and all file descriptors are closed.

This approach allows safe file system operations that Windows, for example, doesn't. Operations like delete, move and rename over files in use without needing locking or breaking anything.

Your dd operation is going to succeed despite the file removal, which will actually be deferred till the dd finishes.

http://en.wikipedia.org/wiki/Reference_counting#Disk_operating_systems

[EDIT] My response doesn't make much sense now as the question was edited by someone else. The original question was about a supposed bug in linux and not about if linux can lock a file:

People keep telling me Linux/Unix is better OS. I am coding Java on Linux now and come across a problem, that I can easily reproduce: I can dd a file on my hard drive and delete it in Nautilus while the dd is still going on. How come linux cannot enforce a mandatory file lock to protect R/W??

olivecoder
  • 2,739
  • 21
  • 21
1

Linux and Unix OS's can enforce file locks, but it does not do so by default becuase of its multiuser design. Try reading the manual pages for flock and fcntl. That might get you started.

gbtimmon
  • 4,098
  • 1
  • 18
  • 34
  • 1
    `fcntl` is __advisory__ lock (cooperative), `flock` is __mandatory__ lock (competitive) – Sandburg Apr 13 '18 at 13:33
  • @Sandburg It's the other way around. Correct is `flock` is cooperative locking and `fcntl` can be used for mandatory locking. – sema Apr 14 '21 at 16:14