mandatory-locking.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. Mandatory File Locking For The Linux Operating System
  2. Andy Walker <andy@lysaker.kvaerner.no>
  3. 15 April 1996
  4. (Updated September 2007)
  5. 0. Why you should avoid mandatory locking
  6. -----------------------------------------
  7. The Linux implementation is prey to a number of difficult-to-fix race
  8. conditions which in practice make it not dependable:
  9. - The write system call checks for a mandatory lock only once
  10. at its start. It is therefore possible for a lock request to
  11. be granted after this check but before the data is modified.
  12. A process may then see file data change even while a mandatory
  13. lock was held.
  14. - Similarly, an exclusive lock may be granted on a file after
  15. the kernel has decided to proceed with a read, but before the
  16. read has actually completed, and the reading process may see
  17. the file data in a state which should not have been visible
  18. to it.
  19. - Similar races make the claimed mutual exclusion between lock
  20. and mmap similarly unreliable.
  21. 1. What is mandatory locking?
  22. ------------------------------
  23. Mandatory locking is kernel enforced file locking, as opposed to the more usual
  24. cooperative file locking used to guarantee sequential access to files among
  25. processes. File locks are applied using the flock() and fcntl() system calls
  26. (and the lockf() library routine which is a wrapper around fcntl().) It is
  27. normally a process' responsibility to check for locks on a file it wishes to
  28. update, before applying its own lock, updating the file and unlocking it again.
  29. The most commonly used example of this (and in the case of sendmail, the most
  30. troublesome) is access to a user's mailbox. The mail user agent and the mail
  31. transfer agent must guard against updating the mailbox at the same time, and
  32. prevent reading the mailbox while it is being updated.
  33. In a perfect world all processes would use and honour a cooperative, or
  34. "advisory" locking scheme. However, the world isn't perfect, and there's
  35. a lot of poorly written code out there.
  36. In trying to address this problem, the designers of System V UNIX came up
  37. with a "mandatory" locking scheme, whereby the operating system kernel would
  38. block attempts by a process to write to a file that another process holds a
  39. "read" -or- "shared" lock on, and block attempts to both read and write to a
  40. file that a process holds a "write " -or- "exclusive" lock on.
  41. The System V mandatory locking scheme was intended to have as little impact as
  42. possible on existing user code. The scheme is based on marking individual files
  43. as candidates for mandatory locking, and using the existing fcntl()/lockf()
  44. interface for applying locks just as if they were normal, advisory locks.
  45. Note 1: In saying "file" in the paragraphs above I am actually not telling
  46. the whole truth. System V locking is based on fcntl(). The granularity of
  47. fcntl() is such that it allows the locking of byte ranges in files, in addition
  48. to entire files, so the mandatory locking rules also have byte level
  49. granularity.
  50. Note 2: POSIX.1 does not specify any scheme for mandatory locking, despite
  51. borrowing the fcntl() locking scheme from System V. The mandatory locking
  52. scheme is defined by the System V Interface Definition (SVID) Version 3.
  53. 2. Marking a file for mandatory locking
  54. ---------------------------------------
  55. A file is marked as a candidate for mandatory locking by setting the group-id
  56. bit in its file mode but removing the group-execute bit. This is an otherwise
  57. meaningless combination, and was chosen by the System V implementors so as not
  58. to break existing user programs.
  59. Note that the group-id bit is usually automatically cleared by the kernel when
  60. a setgid file is written to. This is a security measure. The kernel has been
  61. modified to recognize the special case of a mandatory lock candidate and to
  62. refrain from clearing this bit. Similarly the kernel has been modified not
  63. to run mandatory lock candidates with setgid privileges.
  64. 3. Available implementations
  65. ----------------------------
  66. I have considered the implementations of mandatory locking available with
  67. SunOS 4.1.x, Solaris 2.x and HP-UX 9.x.
  68. Generally I have tried to make the most sense out of the behaviour exhibited
  69. by these three reference systems. There are many anomalies.
  70. All the reference systems reject all calls to open() for a file on which
  71. another process has outstanding mandatory locks. This is in direct
  72. contravention of SVID 3, which states that only calls to open() with the
  73. O_TRUNC flag set should be rejected. The Linux implementation follows the SVID
  74. definition, which is the "Right Thing", since only calls with O_TRUNC can
  75. modify the contents of the file.
  76. HP-UX even disallows open() with O_TRUNC for a file with advisory locks, not
  77. just mandatory locks. That would appear to contravene POSIX.1.
  78. mmap() is another interesting case. All the operating systems mentioned
  79. prevent mandatory locks from being applied to an mmap()'ed file, but HP-UX
  80. also disallows advisory locks for such a file. SVID actually specifies the
  81. paranoid HP-UX behaviour.
  82. In my opinion only MAP_SHARED mappings should be immune from locking, and then
  83. only from mandatory locks - that is what is currently implemented.
  84. SunOS is so hopeless that it doesn't even honour the O_NONBLOCK flag for
  85. mandatory locks, so reads and writes to locked files always block when they
  86. should return EAGAIN.
  87. I'm afraid that this is such an esoteric area that the semantics described
  88. below are just as valid as any others, so long as the main points seem to
  89. agree.
  90. 4. Semantics
  91. ------------
  92. 1. Mandatory locks can only be applied via the fcntl()/lockf() locking
  93. interface - in other words the System V/POSIX interface. BSD style
  94. locks using flock() never result in a mandatory lock.
  95. 2. If a process has locked a region of a file with a mandatory read lock, then
  96. other processes are permitted to read from that region. If any of these
  97. processes attempts to write to the region it will block until the lock is
  98. released, unless the process has opened the file with the O_NONBLOCK
  99. flag in which case the system call will return immediately with the error
  100. status EAGAIN.
  101. 3. If a process has locked a region of a file with a mandatory write lock, all
  102. attempts to read or write to that region block until the lock is released,
  103. unless a process has opened the file with the O_NONBLOCK flag in which case
  104. the system call will return immediately with the error status EAGAIN.
  105. 4. Calls to open() with O_TRUNC, or to creat(), on a existing file that has
  106. any mandatory locks owned by other processes will be rejected with the
  107. error status EAGAIN.
  108. 5. Attempts to apply a mandatory lock to a file that is memory mapped and
  109. shared (via mmap() with MAP_SHARED) will be rejected with the error status
  110. EAGAIN.
  111. 6. Attempts to create a shared memory map of a file (via mmap() with MAP_SHARED)
  112. that has any mandatory locks in effect will be rejected with the error status
  113. EAGAIN.
  114. 5. Which system calls are affected?
  115. -----------------------------------
  116. Those which modify a file's contents, not just the inode. That gives read(),
  117. write(), readv(), writev(), open(), creat(), mmap(), truncate() and
  118. ftruncate(). truncate() and ftruncate() are considered to be "write" actions
  119. for the purposes of mandatory locking.
  120. The affected region is usually defined as stretching from the current position
  121. for the total number of bytes read or written. For the truncate calls it is
  122. defined as the bytes of a file removed or added (we must also consider bytes
  123. added, as a lock can specify just "the whole file", rather than a specific
  124. range of bytes.)
  125. Note 3: I may have overlooked some system calls that need mandatory lock
  126. checking in my eagerness to get this code out the door. Please let me know, or
  127. better still fix the system calls yourself and submit a patch to me or Linus.
  128. 6. Warning!
  129. -----------
  130. Not even root can override a mandatory lock, so runaway processes can wreak
  131. havoc if they lock crucial files. The way around it is to change the file
  132. permissions (remove the setgid bit) before trying to read or write to it.
  133. Of course, that might be a bit tricky if the system is hung :-(