overlayfs.txt 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. whiteouts and opaque directories
  67. --------------------------------
  68. In order to support rm and rmdir without changing the lower
  69. filesystem, an overlay filesystem needs to record in the upper filesystem
  70. that files have been removed. This is done using whiteouts and opaque
  71. directories (non-directories are always opaque).
  72. A whiteout is created as a character device with 0/0 device number.
  73. When a whiteout is found in the upper level of a merged directory, any
  74. matching name in the lower level is ignored, and the whiteout itself
  75. is also hidden.
  76. A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  77. to "y". Where the upper filesystem contains an opaque directory, any
  78. directory in the lower filesystem with the same name is ignored.
  79. readdir
  80. -------
  81. When a 'readdir' request is made on a merged directory, the upper and
  82. lower directories are each read and the name lists merged in the
  83. obvious way (upper is read first, then lower - entries that already
  84. exist are not re-added). This merged name list is cached in the
  85. 'struct file' and so remains as long as the file is kept open. If the
  86. directory is opened and read by two processes at the same time, they
  87. will each have separate caches. A seekdir to the start of the
  88. directory (offset 0) followed by a readdir will cause the cache to be
  89. discarded and rebuilt.
  90. This means that changes to the merged directory do not appear while a
  91. directory is being read. This is unlikely to be noticed by many
  92. programs.
  93. seek offsets are assigned sequentially when the directories are read.
  94. Thus if
  95. - read part of a directory
  96. - remember an offset, and close the directory
  97. - re-open the directory some time later
  98. - seek to the remembered offset
  99. there may be little correlation between the old and new locations in
  100. the list of filenames, particularly if anything has changed in the
  101. directory.
  102. Readdir on directories that are not merged is simply handled by the
  103. underlying directory (upper or lower).
  104. Non-directories
  105. ---------------
  106. Objects that are not directories (files, symlinks, device-special
  107. files etc.) are presented either from the upper or lower filesystem as
  108. appropriate. When a file in the lower filesystem is accessed in a way
  109. the requires write-access, such as opening for write access, changing
  110. some metadata etc., the file is first copied from the lower filesystem
  111. to the upper filesystem (copy_up). Note that creating a hard-link
  112. also requires copy_up, though of course creation of a symlink does
  113. not.
  114. The copy_up may turn out to be unnecessary, for example if the file is
  115. opened for read-write but the data is not modified.
  116. The copy_up process first makes sure that the containing directory
  117. exists in the upper filesystem - creating it and any parents as
  118. necessary. It then creates the object with the same metadata (owner,
  119. mode, mtime, symlink-target etc.) and then if the object is a file, the
  120. data is copied from the lower to the upper filesystem. Finally any
  121. extended attributes are copied up.
  122. Once the copy_up is complete, the overlay filesystem simply
  123. provides direct access to the newly created file in the upper
  124. filesystem - future operations on the file are barely noticed by the
  125. overlay filesystem (though an operation on the name of the file such as
  126. rename or unlink will of course be noticed and handled).
  127. Multiple lower layers
  128. ---------------------
  129. Multiple lower layers can now be given using the the colon (":") as a
  130. separator character between the directory names. For example:
  131. mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
  132. As the example shows, "upperdir=" and "workdir=" may be omitted. In
  133. that case the overlay will be read-only.
  134. The specified lower directories will be stacked beginning from the
  135. rightmost one and going left. In the above example lower1 will be the
  136. top, lower2 the middle and lower3 the bottom layer.
  137. Non-standard behavior
  138. ---------------------
  139. The copy_up operation essentially creates a new, identical file and
  140. moves it over to the old name. The new file may be on a different
  141. filesystem, so both st_dev and st_ino of the file may change.
  142. Any open files referring to this inode will access the old data and
  143. metadata. Similarly any file locks obtained before copy_up will not
  144. apply to the copied up file.
  145. On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
  146. fsetxattr(2) will fail with EROFS.
  147. If a file with multiple hard links is copied up, then this will
  148. "break" the link. Changes will not be propagated to other names
  149. referring to the same inode.
  150. Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
  151. object in overlayfs will not contain valid absolute paths, only
  152. relative paths leading up to the filesystem's root. This will be
  153. fixed in the future.
  154. Some operations are not atomic, for example a crash during copy_up or
  155. rename will leave the filesystem in an inconsistent state. This will
  156. be addressed in the future.
  157. Changes to underlying filesystems
  158. ---------------------------------
  159. Offline changes, when the overlay is not mounted, are allowed to either
  160. the upper or the lower trees.
  161. Changes to the underlying filesystems while part of a mounted overlay
  162. filesystem are not allowed. If the underlying filesystem is changed,
  163. the behavior of the overlay is undefined, though it will not result in
  164. a crash or deadlock.
  165. Testsuite
  166. ---------
  167. There's testsuite developed by David Howells at:
  168. git://git.infradead.org/users/dhowells/unionmount-testsuite.git
  169. Run as root:
  170. # cd unionmount-testsuite
  171. # ./run --ov