overlayfs.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. non-directory objects may report an st_dev from the lower filesystem 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. In the special case of all overlay layers on the same underlying
  25. filesystem, all objects will report an st_dev from the overlay
  26. filesystem and st_ino from the underlying filesystem. This will
  27. make the overlay mount more compliant with filesystem scanners and
  28. overlay objects will be distinguishable from the corresponding
  29. objects in the original filesystem.
  30. Upper and Lower
  31. ---------------
  32. An overlay filesystem combines two filesystems - an 'upper' filesystem
  33. and a 'lower' filesystem. When a name exists in both filesystems, the
  34. object in the 'upper' filesystem is visible while the object in the
  35. 'lower' filesystem is either hidden or, in the case of directories,
  36. merged with the 'upper' object.
  37. It would be more correct to refer to an upper and lower 'directory
  38. tree' rather than 'filesystem' as it is quite possible for both
  39. directory trees to be in the same filesystem and there is no
  40. requirement that the root of a filesystem be given for either upper or
  41. lower.
  42. The lower filesystem can be any filesystem supported by Linux and does
  43. not need to be writable. The lower filesystem can even be another
  44. overlayfs. The upper filesystem will normally be writable and if it
  45. is it must support the creation of trusted.* extended attributes, and
  46. must provide valid d_type in readdir responses, so NFS is not suitable.
  47. A read-only overlay of two read-only filesystems may use any
  48. filesystem type.
  49. Directories
  50. -----------
  51. Overlaying mainly involves directories. If a given name appears in both
  52. upper and lower filesystems and refers to a non-directory in either,
  53. then the lower object is hidden - the name refers only to the upper
  54. object.
  55. Where both upper and lower objects are directories, a merged directory
  56. is formed.
  57. At mount time, the two directories given as mount options "lowerdir" and
  58. "upperdir" are combined into a merged directory:
  59. mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
  60. workdir=/work /merged
  61. The "workdir" needs to be an empty directory on the same filesystem
  62. as upperdir.
  63. Then whenever a lookup is requested in such a merged directory, the
  64. lookup is performed in each actual directory and the combined result
  65. is cached in the dentry belonging to the overlay filesystem. If both
  66. actual lookups find directories, both are stored and a merged
  67. directory is created, otherwise only one is stored: the upper if it
  68. exists, else the lower.
  69. Only the lists of names from directories are merged. Other content
  70. such as metadata and extended attributes are reported for the upper
  71. directory only. These attributes of the lower directory are hidden.
  72. credentials
  73. -----------
  74. By default, all access to the upper, lower and work directories is the
  75. recorded mounter's MAC and DAC credentials. The incoming accesses are
  76. checked against the caller's credentials.
  77. In the case where caller MAC or DAC credentials do not overlap, a
  78. use case available in older versions of the driver, the
  79. override_creds mount flag can be turned off and help when the use
  80. pattern has caller with legitimate credentials where the mounter
  81. does not. Several unintended side effects will occur though. The
  82. caller without certain key capabilities or lower privilege will not
  83. always be able to delete files or directories, create nodes, or
  84. search some restricted directories. The ability to search and read
  85. a directory entry is spotty as a result of the cache mechanism not
  86. retesting the credentials because of the assumption, a privileged
  87. caller can fill cache, then a lower privilege can read the directory
  88. cache. The uneven security model where cache, upperdir and workdir
  89. are opened at privilege, but accessed without creating a form of
  90. privilege escalation, should only be used with strict understanding
  91. of the side effects and of the security policies.
  92. whiteouts and opaque directories
  93. --------------------------------
  94. In order to support rm and rmdir without changing the lower
  95. filesystem, an overlay filesystem needs to record in the upper filesystem
  96. that files have been removed. This is done using whiteouts and opaque
  97. directories (non-directories are always opaque).
  98. A whiteout is created as a character device with 0/0 device number.
  99. When a whiteout is found in the upper level of a merged directory, any
  100. matching name in the lower level is ignored, and the whiteout itself
  101. is also hidden.
  102. A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  103. to "y". Where the upper filesystem contains an opaque directory, any
  104. directory in the lower filesystem with the same name is ignored.
  105. readdir
  106. -------
  107. When a 'readdir' request is made on a merged directory, the upper and
  108. lower directories are each read and the name lists merged in the
  109. obvious way (upper is read first, then lower - entries that already
  110. exist are not re-added). This merged name list is cached in the
  111. 'struct file' and so remains as long as the file is kept open. If the
  112. directory is opened and read by two processes at the same time, they
  113. will each have separate caches. A seekdir to the start of the
  114. directory (offset 0) followed by a readdir will cause the cache to be
  115. discarded and rebuilt.
  116. This means that changes to the merged directory do not appear while a
  117. directory is being read. This is unlikely to be noticed by many
  118. programs.
  119. seek offsets are assigned sequentially when the directories are read.
  120. Thus if
  121. - read part of a directory
  122. - remember an offset, and close the directory
  123. - re-open the directory some time later
  124. - seek to the remembered offset
  125. there may be little correlation between the old and new locations in
  126. the list of filenames, particularly if anything has changed in the
  127. directory.
  128. Readdir on directories that are not merged is simply handled by the
  129. underlying directory (upper or lower).
  130. renaming directories
  131. --------------------
  132. When renaming a directory that is on the lower layer or merged (i.e. the
  133. directory was not created on the upper layer to start with) overlayfs can
  134. handle it in two different ways:
  135. 1. return EXDEV error: this error is returned by rename(2) when trying to
  136. move a file or directory across filesystem boundaries. Hence
  137. applications are usually prepared to hande this error (mv(1) for example
  138. recursively copies the directory tree). This is the default behavior.
  139. 2. If the "redirect_dir" feature is enabled, then the directory will be
  140. copied up (but not the contents). Then the "trusted.overlay.redirect"
  141. extended attribute is set to the path of the original location from the
  142. root of the overlay. Finally the directory is moved to the new
  143. location.
  144. Non-directories
  145. ---------------
  146. Objects that are not directories (files, symlinks, device-special
  147. files etc.) are presented either from the upper or lower filesystem as
  148. appropriate. When a file in the lower filesystem is accessed in a way
  149. the requires write-access, such as opening for write access, changing
  150. some metadata etc., the file is first copied from the lower filesystem
  151. to the upper filesystem (copy_up). Note that creating a hard-link
  152. also requires copy_up, though of course creation of a symlink does
  153. not.
  154. The copy_up may turn out to be unnecessary, for example if the file is
  155. opened for read-write but the data is not modified.
  156. The copy_up process first makes sure that the containing directory
  157. exists in the upper filesystem - creating it and any parents as
  158. necessary. It then creates the object with the same metadata (owner,
  159. mode, mtime, symlink-target etc.) and then if the object is a file, the
  160. data is copied from the lower to the upper filesystem. Finally any
  161. extended attributes are copied up.
  162. Once the copy_up is complete, the overlay filesystem simply
  163. provides direct access to the newly created file in the upper
  164. filesystem - future operations on the file are barely noticed by the
  165. overlay filesystem (though an operation on the name of the file such as
  166. rename or unlink will of course be noticed and handled).
  167. Multiple lower layers
  168. ---------------------
  169. Multiple lower layers can now be given using the the colon (":") as a
  170. separator character between the directory names. For example:
  171. mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
  172. As the example shows, "upperdir=" and "workdir=" may be omitted. In
  173. that case the overlay will be read-only.
  174. The specified lower directories will be stacked beginning from the
  175. rightmost one and going left. In the above example lower1 will be the
  176. top, lower2 the middle and lower3 the bottom layer.
  177. Sharing and copying layers
  178. --------------------------
  179. Lower layers may be shared among several overlay mounts and that is indeed
  180. a very common practice. An overlay mount may use the same lower layer
  181. path as another overlay mount and it may use a lower layer path that is
  182. beneath or above the path of another overlay lower layer path.
  183. Using an upper layer path and/or a workdir path that are already used by
  184. another overlay mount is not allowed and may fail with EBUSY. Using
  185. partially overlapping paths is not allowed but will not fail with EBUSY.
  186. If files are accessed from two overlayfs mounts which share or overlap the
  187. upper layer and/or workdir path the behavior of the overlay is undefined,
  188. though it will not result in a crash or deadlock.
  189. Mounting an overlay using an upper layer path, where the upper layer path
  190. was previously used by another mounted overlay in combination with a
  191. different lower layer path, is allowed, unless the "inodes index" feature
  192. is enabled.
  193. With the "inodes index" feature, on the first time mount, an NFS file
  194. handle of the lower layer root directory, along with the UUID of the lower
  195. filesystem, are encoded and stored in the "trusted.overlay.origin" extended
  196. attribute on the upper layer root directory. On subsequent mount attempts,
  197. the lower root directory file handle and lower filesystem UUID are compared
  198. to the stored origin in upper root directory. On failure to verify the
  199. lower root origin, mount will fail with ESTALE. An overlayfs mount with
  200. "inodes index" enabled will fail with EOPNOTSUPP if the lower filesystem
  201. does not support NFS export, lower filesystem does not have a valid UUID or
  202. if the upper filesystem does not support extended attributes.
  203. It is quite a common practice to copy overlay layers to a different
  204. directory tree on the same or different underlying filesystem, and even
  205. to a different machine. With the "inodes index" feature, trying to mount
  206. the copied layers will fail the verification of the lower root file handle.
  207. Non-standard behavior
  208. ---------------------
  209. The copy_up operation essentially creates a new, identical file and
  210. moves it over to the old name. The new file may be on a different
  211. filesystem, so both st_dev and st_ino of the file may change.
  212. Any open files referring to this inode will access the old data.
  213. If a file with multiple hard links is copied up, then this will
  214. "break" the link. Changes will not be propagated to other names
  215. referring to the same inode.
  216. Unless "redirect_dir" feature is enabled, rename(2) on a lower or merged
  217. directory will fail with EXDEV.
  218. Changes to underlying filesystems
  219. ---------------------------------
  220. Offline changes, when the overlay is not mounted, are allowed to either
  221. the upper or the lower trees.
  222. Changes to the underlying filesystems while part of a mounted overlay
  223. filesystem are not allowed. If the underlying filesystem is changed,
  224. the behavior of the overlay is undefined, though it will not result in
  225. a crash or deadlock.
  226. Testsuite
  227. ---------
  228. There's testsuite developed by David Howells at:
  229. git://git.infradead.org/users/dhowells/unionmount-testsuite.git
  230. Run as root:
  231. # cd unionmount-testsuite
  232. # ./run --ov