overlayfs.txt 8.8 KB

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