overlayfs.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. Overlay objects
  11. ---------------
  12. The overlay filesystem approach is 'hybrid', because the objects that
  13. appear in the filesystem do not always appear to belong to that filesystem.
  14. In many 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. non-directory objects may report an st_dev from the lower filesystem 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. In the special case of all overlay layers on the same underlying
  24. filesystem, all objects will report an st_dev from the overlay
  25. filesystem and st_ino from the underlying filesystem. This will
  26. make the overlay mount more compliant with filesystem scanners and
  27. overlay objects will be distinguishable from the corresponding
  28. objects in the original filesystem.
  29. On 64bit systems, even if all overlay layers are not on the same
  30. underlying filesystem, the same compliant behavior could be achieved
  31. with the "xino" feature. The "xino" feature composes a unique object
  32. identifier from the real object st_ino and an underlying fsid index.
  33. If all underlying filesystems support NFS file handles and export file
  34. handles with 32bit inode number encoding (e.g. ext4), overlay filesystem
  35. will use the high inode number bits for fsid. Even when the underlying
  36. filesystem uses 64bit inode numbers, users can still enable the "xino"
  37. feature with the "-o xino=on" overlay mount option. That is useful for the
  38. case of underlying filesystems like xfs and tmpfs, which use 64bit inode
  39. numbers, but are very unlikely to use the high inode number bit.
  40. Upper and Lower
  41. ---------------
  42. An overlay filesystem combines two filesystems - an 'upper' filesystem
  43. and a 'lower' filesystem. When a name exists in both filesystems, the
  44. object in the 'upper' filesystem is visible while the object in the
  45. 'lower' filesystem is either hidden or, in the case of directories,
  46. merged with the 'upper' object.
  47. It would be more correct to refer to an upper and lower 'directory
  48. tree' rather than 'filesystem' as it is quite possible for both
  49. directory trees to be in the same filesystem and there is no
  50. requirement that the root of a filesystem be given for either upper or
  51. lower.
  52. The lower filesystem can be any filesystem supported by Linux and does
  53. not need to be writable. The lower filesystem can even be another
  54. overlayfs. The upper filesystem will normally be writable and if it
  55. is it must support the creation of trusted.* extended attributes, and
  56. must provide valid d_type in readdir responses, so NFS is not suitable.
  57. A read-only overlay of two read-only filesystems may use any
  58. filesystem type.
  59. Directories
  60. -----------
  61. Overlaying mainly involves directories. If a given name appears in both
  62. upper and lower filesystems and refers to a non-directory in either,
  63. then the lower object is hidden - the name refers only to the upper
  64. object.
  65. Where both upper and lower objects are directories, a merged directory
  66. is formed.
  67. At mount time, the two directories given as mount options "lowerdir" and
  68. "upperdir" are combined into a merged directory:
  69. mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
  70. workdir=/work /merged
  71. The "workdir" needs to be an empty directory on the same filesystem
  72. as upperdir.
  73. Then whenever a lookup is requested in such a merged directory, the
  74. lookup is performed in each actual directory and the combined result
  75. is cached in the dentry belonging to the overlay filesystem. If both
  76. actual lookups find directories, both are stored and a merged
  77. directory is created, otherwise only one is stored: the upper if it
  78. exists, else the lower.
  79. Only the lists of names from directories are merged. Other content
  80. such as metadata and extended attributes are reported for the upper
  81. directory only. These attributes of the lower directory are hidden.
  82. whiteouts and opaque directories
  83. --------------------------------
  84. In order to support rm and rmdir without changing the lower
  85. filesystem, an overlay filesystem needs to record in the upper filesystem
  86. that files have been removed. This is done using whiteouts and opaque
  87. directories (non-directories are always opaque).
  88. A whiteout is created as a character device with 0/0 device number.
  89. When a whiteout is found in the upper level of a merged directory, any
  90. matching name in the lower level is ignored, and the whiteout itself
  91. is also hidden.
  92. A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  93. to "y". Where the upper filesystem contains an opaque directory, any
  94. directory in the lower filesystem with the same name is ignored.
  95. readdir
  96. -------
  97. When a 'readdir' request is made on a merged directory, the upper and
  98. lower directories are each read and the name lists merged in the
  99. obvious way (upper is read first, then lower - entries that already
  100. exist are not re-added). This merged name list is cached in the
  101. 'struct file' and so remains as long as the file is kept open. If the
  102. directory is opened and read by two processes at the same time, they
  103. will each have separate caches. A seekdir to the start of the
  104. directory (offset 0) followed by a readdir will cause the cache to be
  105. discarded and rebuilt.
  106. This means that changes to the merged directory do not appear while a
  107. directory is being read. This is unlikely to be noticed by many
  108. programs.
  109. seek offsets are assigned sequentially when the directories are read.
  110. Thus if
  111. - read part of a directory
  112. - remember an offset, and close the directory
  113. - re-open the directory some time later
  114. - seek to the remembered offset
  115. there may be little correlation between the old and new locations in
  116. the list of filenames, particularly if anything has changed in the
  117. directory.
  118. Readdir on directories that are not merged is simply handled by the
  119. underlying directory (upper or lower).
  120. renaming directories
  121. --------------------
  122. When renaming a directory that is on the lower layer or merged (i.e. the
  123. directory was not created on the upper layer to start with) overlayfs can
  124. handle it in two different ways:
  125. 1. return EXDEV error: this error is returned by rename(2) when trying to
  126. move a file or directory across filesystem boundaries. Hence
  127. applications are usually prepared to hande this error (mv(1) for example
  128. recursively copies the directory tree). This is the default behavior.
  129. 2. If the "redirect_dir" feature is enabled, then the directory will be
  130. copied up (but not the contents). Then the "trusted.overlay.redirect"
  131. extended attribute is set to the path of the original location from the
  132. root of the overlay. Finally the directory is moved to the new
  133. location.
  134. There are several ways to tune the "redirect_dir" feature.
  135. Kernel config options:
  136. - OVERLAY_FS_REDIRECT_DIR:
  137. If this is enabled, then redirect_dir is turned on by default.
  138. - OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW:
  139. If this is enabled, then redirects are always followed by default. Enabling
  140. this results in a less secure configuration. Enable this option only when
  141. worried about backward compatibility with kernels that have the redirect_dir
  142. feature and follow redirects even if turned off.
  143. Module options (can also be changed through /sys/module/overlay/parameters/*):
  144. - "redirect_dir=BOOL":
  145. See OVERLAY_FS_REDIRECT_DIR kernel config option above.
  146. - "redirect_always_follow=BOOL":
  147. See OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW kernel config option above.
  148. - "redirect_max=NUM":
  149. The maximum number of bytes in an absolute redirect (default is 256).
  150. Mount options:
  151. - "redirect_dir=on":
  152. Redirects are enabled.
  153. - "redirect_dir=follow":
  154. Redirects are not created, but followed.
  155. - "redirect_dir=off":
  156. Redirects are not created and only followed if "redirect_always_follow"
  157. feature is enabled in the kernel/module config.
  158. - "redirect_dir=nofollow":
  159. Redirects are not created and not followed (equivalent to "redirect_dir=off"
  160. if "redirect_always_follow" feature is not enabled).
  161. When the NFS export feature is enabled, every copied up directory is
  162. indexed by the file handle of the lower inode and a file handle of the
  163. upper directory is stored in a "trusted.overlay.upper" extended attribute
  164. on the index entry. On lookup of a merged directory, if the upper
  165. directory does not match the file handle stores in the index, that is an
  166. indication that multiple upper directories may be redirected to the same
  167. lower directory. In that case, lookup returns an error and warns about
  168. a possible inconsistency.
  169. Because lower layer redirects cannot be verified with the index, enabling
  170. NFS export support on an overlay filesystem with no upper layer requires
  171. turning off redirect follow (e.g. "redirect_dir=nofollow").
  172. Non-directories
  173. ---------------
  174. Objects that are not directories (files, symlinks, device-special
  175. files etc.) are presented either from the upper or lower filesystem as
  176. appropriate. When a file in the lower filesystem is accessed in a way
  177. the requires write-access, such as opening for write access, changing
  178. some metadata etc., the file is first copied from the lower filesystem
  179. to the upper filesystem (copy_up). Note that creating a hard-link
  180. also requires copy_up, though of course creation of a symlink does
  181. not.
  182. The copy_up may turn out to be unnecessary, for example if the file is
  183. opened for read-write but the data is not modified.
  184. The copy_up process first makes sure that the containing directory
  185. exists in the upper filesystem - creating it and any parents as
  186. necessary. It then creates the object with the same metadata (owner,
  187. mode, mtime, symlink-target etc.) and then if the object is a file, the
  188. data is copied from the lower to the upper filesystem. Finally any
  189. extended attributes are copied up.
  190. Once the copy_up is complete, the overlay filesystem simply
  191. provides direct access to the newly created file in the upper
  192. filesystem - future operations on the file are barely noticed by the
  193. overlay filesystem (though an operation on the name of the file such as
  194. rename or unlink will of course be noticed and handled).
  195. Multiple lower layers
  196. ---------------------
  197. Multiple lower layers can now be given using the the colon (":") as a
  198. separator character between the directory names. For example:
  199. mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
  200. As the example shows, "upperdir=" and "workdir=" may be omitted. In
  201. that case the overlay will be read-only.
  202. The specified lower directories will be stacked beginning from the
  203. rightmost one and going left. In the above example lower1 will be the
  204. top, lower2 the middle and lower3 the bottom layer.
  205. Metadata only copy up
  206. --------------------
  207. When metadata only copy up feature is enabled, overlayfs will only copy
  208. up metadata (as opposed to whole file), when a metadata specific operation
  209. like chown/chmod is performed. Full file will be copied up later when
  210. file is opened for WRITE operation.
  211. In other words, this is delayed data copy up operation and data is copied
  212. up when there is a need to actually modify data.
  213. There are multiple ways to enable/disable this feature. A config option
  214. CONFIG_OVERLAY_FS_METACOPY can be set/unset to enable/disable this feature
  215. by default. Or one can enable/disable it at module load time with module
  216. parameter metacopy=on/off. Lastly, there is also a per mount option
  217. metacopy=on/off to enable/disable this feature per mount.
  218. Do not use metacopy=on with untrusted upper/lower directories. Otherwise
  219. it is possible that an attacker can create a handcrafted file with
  220. appropriate REDIRECT and METACOPY xattrs, and gain access to file on lower
  221. pointed by REDIRECT. This should not be possible on local system as setting
  222. "trusted." xattrs will require CAP_SYS_ADMIN. But it should be possible
  223. for untrusted layers like from a pen drive.
  224. Sharing and copying layers
  225. --------------------------
  226. Lower layers may be shared among several overlay mounts and that is indeed
  227. a very common practice. An overlay mount may use the same lower layer
  228. path as another overlay mount and it may use a lower layer path that is
  229. beneath or above the path of another overlay lower layer path.
  230. Using an upper layer path and/or a workdir path that are already used by
  231. another overlay mount is not allowed and may fail with EBUSY. Using
  232. partially overlapping paths is not allowed but will not fail with EBUSY.
  233. If files are accessed from two overlayfs mounts which share or overlap the
  234. upper layer and/or workdir path the behavior of the overlay is undefined,
  235. though it will not result in a crash or deadlock.
  236. Mounting an overlay using an upper layer path, where the upper layer path
  237. was previously used by another mounted overlay in combination with a
  238. different lower layer path, is allowed, unless the "inodes index" feature
  239. or "metadata only copy up" feature is enabled.
  240. With the "inodes index" feature, on the first time mount, an NFS file
  241. handle of the lower layer root directory, along with the UUID of the lower
  242. filesystem, are encoded and stored in the "trusted.overlay.origin" extended
  243. attribute on the upper layer root directory. On subsequent mount attempts,
  244. the lower root directory file handle and lower filesystem UUID are compared
  245. to the stored origin in upper root directory. On failure to verify the
  246. lower root origin, mount will fail with ESTALE. An overlayfs mount with
  247. "inodes index" enabled will fail with EOPNOTSUPP if the lower filesystem
  248. does not support NFS export, lower filesystem does not have a valid UUID or
  249. if the upper filesystem does not support extended attributes.
  250. For "metadata only copy up" feature there is no verification mechanism at
  251. mount time. So if same upper is mounted with different set of lower, mount
  252. probably will succeed but expect the unexpected later on. So don't do it.
  253. It is quite a common practice to copy overlay layers to a different
  254. directory tree on the same or different underlying filesystem, and even
  255. to a different machine. With the "inodes index" feature, trying to mount
  256. the copied layers will fail the verification of the lower root file handle.
  257. Non-standard behavior
  258. ---------------------
  259. Overlayfs can now act as a POSIX compliant filesystem with the following
  260. features turned on:
  261. 1) "redirect_dir"
  262. Enabled with the mount option or module option: "redirect_dir=on" or with
  263. the kernel config option CONFIG_OVERLAY_FS_REDIRECT_DIR=y.
  264. If this feature is disabled, then rename(2) on a lower or merged directory
  265. will fail with EXDEV ("Invalid cross-device link").
  266. 2) "inode index"
  267. Enabled with the mount option or module option "index=on" or with the
  268. kernel config option CONFIG_OVERLAY_FS_INDEX=y.
  269. If this feature is disabled and a file with multiple hard links is copied
  270. up, then this will "break" the link. Changes will not be propagated to
  271. other names referring to the same inode.
  272. 3) "xino"
  273. Enabled with the mount option "xino=auto" or "xino=on", with the module
  274. option "xino_auto=on" or with the kernel config option
  275. CONFIG_OVERLAY_FS_XINO_AUTO=y. Also implicitly enabled by using the same
  276. underlying filesystem for all layers making up the overlay.
  277. If this feature is disabled or the underlying filesystem doesn't have
  278. enough free bits in the inode number, then overlayfs will not be able to
  279. guarantee that the values of st_ino and st_dev returned by stat(2) and the
  280. value of d_ino returned by readdir(3) will act like on a normal filesystem.
  281. E.g. the value of st_dev may be different for two objects in the same
  282. overlay filesystem and the value of st_ino for directory objects may not be
  283. persistent and could change even while the overlay filesystem is mounted.
  284. Changes to underlying filesystems
  285. ---------------------------------
  286. Offline changes, when the overlay is not mounted, are allowed to either
  287. the upper or the lower trees.
  288. Changes to the underlying filesystems while part of a mounted overlay
  289. filesystem are not allowed. If the underlying filesystem is changed,
  290. the behavior of the overlay is undefined, though it will not result in
  291. a crash or deadlock.
  292. When the overlay NFS export feature is enabled, overlay filesystems
  293. behavior on offline changes of the underlying lower layer is different
  294. than the behavior when NFS export is disabled.
  295. On every copy_up, an NFS file handle of the lower inode, along with the
  296. UUID of the lower filesystem, are encoded and stored in an extended
  297. attribute "trusted.overlay.origin" on the upper inode.
  298. When the NFS export feature is enabled, a lookup of a merged directory,
  299. that found a lower directory at the lookup path or at the path pointed
  300. to by the "trusted.overlay.redirect" extended attribute, will verify
  301. that the found lower directory file handle and lower filesystem UUID
  302. match the origin file handle that was stored at copy_up time. If a
  303. found lower directory does not match the stored origin, that directory
  304. will not be merged with the upper directory.
  305. NFS export
  306. ----------
  307. When the underlying filesystems supports NFS export and the "nfs_export"
  308. feature is enabled, an overlay filesystem may be exported to NFS.
  309. With the "nfs_export" feature, on copy_up of any lower object, an index
  310. entry is created under the index directory. The index entry name is the
  311. hexadecimal representation of the copy up origin file handle. For a
  312. non-directory object, the index entry is a hard link to the upper inode.
  313. For a directory object, the index entry has an extended attribute
  314. "trusted.overlay.upper" with an encoded file handle of the upper
  315. directory inode.
  316. When encoding a file handle from an overlay filesystem object, the
  317. following rules apply:
  318. 1. For a non-upper object, encode a lower file handle from lower inode
  319. 2. For an indexed object, encode a lower file handle from copy_up origin
  320. 3. For a pure-upper object and for an existing non-indexed upper object,
  321. encode an upper file handle from upper inode
  322. The encoded overlay file handle includes:
  323. - Header including path type information (e.g. lower/upper)
  324. - UUID of the underlying filesystem
  325. - Underlying filesystem encoding of underlying inode
  326. This encoding format is identical to the encoding format file handles that
  327. are stored in extended attribute "trusted.overlay.origin".
  328. When decoding an overlay file handle, the following steps are followed:
  329. 1. Find underlying layer by UUID and path type information.
  330. 2. Decode the underlying filesystem file handle to underlying dentry.
  331. 3. For a lower file handle, lookup the handle in index directory by name.
  332. 4. If a whiteout is found in index, return ESTALE. This represents an
  333. overlay object that was deleted after its file handle was encoded.
  334. 5. For a non-directory, instantiate a disconnected overlay dentry from the
  335. decoded underlying dentry, the path type and index inode, if found.
  336. 6. For a directory, use the connected underlying decoded dentry, path type
  337. and index, to lookup a connected overlay dentry.
  338. Decoding a non-directory file handle may return a disconnected dentry.
  339. copy_up of that disconnected dentry will create an upper index entry with
  340. no upper alias.
  341. When overlay filesystem has multiple lower layers, a middle layer
  342. directory may have a "redirect" to lower directory. Because middle layer
  343. "redirects" are not indexed, a lower file handle that was encoded from the
  344. "redirect" origin directory, cannot be used to find the middle or upper
  345. layer directory. Similarly, a lower file handle that was encoded from a
  346. descendant of the "redirect" origin directory, cannot be used to
  347. reconstruct a connected overlay path. To mitigate the cases of
  348. directories that cannot be decoded from a lower file handle, these
  349. directories are copied up on encode and encoded as an upper file handle.
  350. On an overlay filesystem with no upper layer this mitigation cannot be
  351. used NFS export in this setup requires turning off redirect follow (e.g.
  352. "redirect_dir=nofollow").
  353. The overlay filesystem does not support non-directory connectable file
  354. handles, so exporting with the 'subtree_check' exportfs configuration will
  355. cause failures to lookup files over NFS.
  356. When the NFS export feature is enabled, all directory index entries are
  357. verified on mount time to check that upper file handles are not stale.
  358. This verification may cause significant overhead in some cases.
  359. Testsuite
  360. ---------
  361. There's a testsuite originally developed by David Howells and currently
  362. maintained by Amir Goldstein at:
  363. https://github.com/amir73il/unionmount-testsuite.git
  364. Run as root:
  365. # cd unionmount-testsuite
  366. # ./run --ov --verify