overlayfs.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Written by: Neil Brown
  2. Please see MAINTAINERS file for where to send questions.
  3. Overlay Filesystem
  4. ==================
  5. This document describes a prototype for a new approach to providing
  6. overlay-filesystem functionality in Linux (sometimes referred to as
  7. union-filesystems). An overlay-filesystem tries to present a
  8. filesystem which is the result over overlaying one filesystem on top
  9. of the other.
  10. The result will inevitably fail to look exactly like a normal
  11. filesystem for various technical reasons. The expectation is that
  12. many use cases will be able to ignore these differences.
  13. This approach is 'hybrid' because the objects that appear in the
  14. filesystem do not all appear to belong to that filesystem. In many
  15. cases an object accessed in the union will be indistinguishable
  16. from accessing the corresponding object from the original filesystem.
  17. This is most obvious from the 'st_dev' field returned by stat(2).
  18. While directories will report an st_dev from the overlay-filesystem,
  19. all non-directory objects will report an st_dev from the lower or
  20. upper filesystem that is providing the object. Similarly st_ino will
  21. only be unique when combined with st_dev, and both of these can change
  22. over the lifetime of a non-directory object. Many applications and
  23. tools ignore these values and will not be affected.
  24. Upper and Lower
  25. ---------------
  26. An overlay filesystem combines two filesystems - an 'upper' filesystem
  27. and a 'lower' filesystem. When a name exists in both filesystems, the
  28. object in the 'upper' filesystem is visible while the object in the
  29. 'lower' filesystem is either hidden or, in the case of directories,
  30. merged with the 'upper' object.
  31. It would be more correct to refer to an upper and lower 'directory
  32. tree' rather than 'filesystem' as it is quite possible for both
  33. directory trees to be in the same filesystem and there is no
  34. requirement that the root of a filesystem be given for either upper or
  35. lower.
  36. The lower filesystem can be any filesystem supported by Linux and does
  37. not need to be writable. The lower filesystem can even be another
  38. overlayfs. The upper filesystem will normally be writable and if it
  39. is it must support the creation of trusted.* extended attributes, and
  40. must provide valid d_type in readdir responses, so NFS is not suitable.
  41. A read-only overlay of two read-only filesystems may use any
  42. filesystem type.
  43. Directories
  44. -----------
  45. Overlaying mainly involves directories. If a given name appears in both
  46. upper and lower filesystems and refers to a non-directory in either,
  47. then the lower object is hidden - the name refers only to the upper
  48. object.
  49. Where both upper and lower objects are directories, a merged directory
  50. is formed.
  51. At mount time, the two directories given as mount options "lowerdir" and
  52. "upperdir" are combined into a merged directory:
  53. mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
  54. workdir=/work /merged
  55. The "workdir" needs to be an empty directory on the same filesystem
  56. as upperdir.
  57. Then whenever a lookup is requested in such a merged directory, the
  58. lookup is performed in each actual directory and the combined result
  59. is cached in the dentry belonging to the overlay filesystem. If both
  60. actual lookups find directories, both are stored and a merged
  61. directory is created, otherwise only one is stored: the upper if it
  62. exists, else the lower.
  63. Only the lists of names from directories are merged. Other content
  64. such as metadata and extended attributes are reported for the upper
  65. directory only. These attributes of the lower directory are hidden.
  66. credentials
  67. -----------
  68. By default, all access to the upper, lower and work directories is the
  69. recorded mounter's MAC and DAC credentials. The incoming accesses are
  70. checked against the caller's credentials.
  71. In the case where caller MAC or DAC credentials do not overlap, a
  72. use case available in older versions of the driver, the
  73. override_creds mount flag can be turned off and help when the use
  74. pattern has caller with legitimate credentials where the mounter
  75. does not. Several unintended side effects will occur though. The
  76. caller without certain key capabilities or lower privilege will not
  77. always be able to delete files or directories, create nodes, or
  78. search some restricted directories. The ability to search and read
  79. a directory entry is spotty as a result of the cache mechanism not
  80. retesting the credentials because of the assumption, a privileged
  81. caller can fill cache, then a lower privilege can read the directory
  82. cache. The uneven security model where cache, upperdir and workdir
  83. are opened at privilege, but accessed without creating a form of
  84. privilege escalation, should only be used with strict understanding
  85. of the side effects and of the security policies.
  86. whiteouts and opaque directories
  87. --------------------------------
  88. In order to support rm and rmdir without changing the lower
  89. filesystem, an overlay filesystem needs to record in the upper filesystem
  90. that files have been removed. This is done using whiteouts and opaque
  91. directories (non-directories are always opaque).
  92. A whiteout is created as a character device with 0/0 device number.
  93. When a whiteout is found in the upper level of a merged directory, any
  94. matching name in the lower level is ignored, and the whiteout itself
  95. is also hidden.
  96. A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  97. to "y". Where the upper filesystem contains an opaque directory, any
  98. directory in the lower filesystem with the same name is ignored.
  99. readdir
  100. -------
  101. When a 'readdir' request is made on a merged directory, the upper and
  102. lower directories are each read and the name lists merged in the
  103. obvious way (upper is read first, then lower - entries that already
  104. exist are not re-added). This merged name list is cached in the
  105. 'struct file' and so remains as long as the file is kept open. If the
  106. directory is opened and read by two processes at the same time, they
  107. will each have separate caches. A seekdir to the start of the
  108. directory (offset 0) followed by a readdir will cause the cache to be
  109. discarded and rebuilt.
  110. This means that changes to the merged directory do not appear while a
  111. directory is being read. This is unlikely to be noticed by many
  112. programs.
  113. seek offsets are assigned sequentially when the directories are read.
  114. Thus if
  115. - read part of a directory
  116. - remember an offset, and close the directory
  117. - re-open the directory some time later
  118. - seek to the remembered offset
  119. there may be little correlation between the old and new locations in
  120. the list of filenames, particularly if anything has changed in the
  121. directory.
  122. Readdir on directories that are not merged is simply handled by the
  123. underlying directory (upper or lower).
  124. Non-directories
  125. ---------------
  126. Objects that are not directories (files, symlinks, device-special
  127. files etc.) are presented either from the upper or lower filesystem as
  128. appropriate. When a file in the lower filesystem is accessed in a way
  129. the requires write-access, such as opening for write access, changing
  130. some metadata etc., the file is first copied from the lower filesystem
  131. to the upper filesystem (copy_up). Note that creating a hard-link
  132. also requires copy_up, though of course creation of a symlink does
  133. not.
  134. The copy_up may turn out to be unnecessary, for example if the file is
  135. opened for read-write but the data is not modified.
  136. The copy_up process first makes sure that the containing directory
  137. exists in the upper filesystem - creating it and any parents as
  138. necessary. It then creates the object with the same metadata (owner,
  139. mode, mtime, symlink-target etc.) and then if the object is a file, the
  140. data is copied from the lower to the upper filesystem. Finally any
  141. extended attributes are copied up.
  142. Once the copy_up is complete, the overlay filesystem simply
  143. provides direct access to the newly created file in the upper
  144. filesystem - future operations on the file are barely noticed by the
  145. overlay filesystem (though an operation on the name of the file such as
  146. rename or unlink will of course be noticed and handled).
  147. Multiple lower layers
  148. ---------------------
  149. Multiple lower layers can now be given using the the colon (":") as a
  150. separator character between the directory names. For example:
  151. mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
  152. As the example shows, "upperdir=" and "workdir=" may be omitted. In
  153. that case the overlay will be read-only.
  154. The specified lower directories will be stacked beginning from the
  155. rightmost one and going left. In the above example lower1 will be the
  156. top, lower2 the middle and lower3 the bottom layer.
  157. Non-standard behavior
  158. ---------------------
  159. The copy_up operation essentially creates a new, identical file and
  160. moves it over to the old name. The new file may be on a different
  161. filesystem, so both st_dev and st_ino of the file may change.
  162. Any open files referring to this inode will access the old data.
  163. Any file locks (and leases) obtained before copy_up will not apply
  164. to the copied up file.
  165. If a file with multiple hard links is copied up, then this will
  166. "break" the link. Changes will not be propagated to other names
  167. referring to the same inode.
  168. Changes to underlying filesystems
  169. ---------------------------------
  170. Offline changes, when the overlay is not mounted, are allowed to either
  171. the upper or the lower trees.
  172. Changes to the underlying filesystems while part of a mounted overlay
  173. filesystem are not allowed. If the underlying filesystem is changed,
  174. the behavior of the overlay is undefined, though it will not result in
  175. a crash or deadlock.
  176. Testsuite
  177. ---------
  178. There's testsuite developed by David Howells at:
  179. git://git.infradead.org/users/dhowells/unionmount-testsuite.git
  180. Run as root:
  181. # cd unionmount-testsuite
  182. # ./run --ov