overlayfs.rst 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Written by: Neil Brown
  3. Please see MAINTAINERS file for where to send questions.
  4. Overlay Filesystem
  5. ==================
  6. This document describes a prototype for a new approach to providing
  7. overlay-filesystem functionality in Linux (sometimes referred to as
  8. union-filesystems). An overlay-filesystem tries to present a
  9. filesystem which is the result over overlaying one filesystem on top
  10. of the other.
  11. Overlay objects
  12. ---------------
  13. The overlay filesystem approach is 'hybrid', because the objects that
  14. appear in the filesystem do not always appear to belong to that filesystem.
  15. In many 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. On 64bit systems, even if all overlay layers are not on the same
  31. underlying filesystem, the same compliant behavior could be achieved
  32. with the "xino" feature. The "xino" feature composes a unique object
  33. identifier from the real object st_ino and an underlying fsid number.
  34. The "xino" feature uses the high inode number bits for fsid, because the
  35. underlying filesystems rarely use the high inode number bits. In case
  36. the underlying inode number does overflow into the high xino bits, overlay
  37. filesystem will fall back to the non xino behavior for that inode.
  38. The "xino" feature can be enabled with the "-o xino=on" overlay mount option.
  39. If all underlying filesystems support NFS file handles, the value of st_ino
  40. for overlay filesystem objects is not only unique, but also persistent over
  41. the lifetime of the filesystem. The "-o xino=auto" overlay mount option
  42. enables the "xino" feature only if the persistent st_ino requirement is met.
  43. The following table summarizes what can be expected in different overlay
  44. configurations.
  45. Inode properties
  46. ````````````````
  47. +--------------+------------+------------+-----------------+----------------+
  48. |Configuration | Persistent | Uniform | st_ino == d_ino | d_ino == i_ino |
  49. | | st_ino | st_dev | | [*] |
  50. +==============+=====+======+=====+======+========+========+========+=======+
  51. | | dir | !dir | dir | !dir | dir + !dir | dir | !dir |
  52. +--------------+-----+------+-----+------+--------+--------+--------+-------+
  53. | All layers | Y | Y | Y | Y | Y | Y | Y | Y |
  54. | on same fs | | | | | | | | |
  55. +--------------+-----+------+-----+------+--------+--------+--------+-------+
  56. | Layers not | N | N | Y | N | N | Y | N | Y |
  57. | on same fs, | | | | | | | | |
  58. | xino=off | | | | | | | | |
  59. +--------------+-----+------+-----+------+--------+--------+--------+-------+
  60. | xino=on/auto | Y | Y | Y | Y | Y | Y | Y | Y |
  61. +--------------+-----+------+-----+------+--------+--------+--------+-------+
  62. | xino=on/auto,| N | N | Y | N | N | Y | N | Y |
  63. | ino overflow | | | | | | | | |
  64. +--------------+-----+------+-----+------+--------+--------+--------+-------+
  65. [*] nfsd v3 readdirplus verifies d_ino == i_ino. i_ino is exposed via several
  66. /proc files, such as /proc/locks and /proc/self/fdinfo/<fd> of an inotify
  67. file descriptor.
  68. Upper and Lower
  69. ---------------
  70. An overlay filesystem combines two filesystems - an 'upper' filesystem
  71. and a 'lower' filesystem. When a name exists in both filesystems, the
  72. object in the 'upper' filesystem is visible while the object in the
  73. 'lower' filesystem is either hidden or, in the case of directories,
  74. merged with the 'upper' object.
  75. It would be more correct to refer to an upper and lower 'directory
  76. tree' rather than 'filesystem' as it is quite possible for both
  77. directory trees to be in the same filesystem and there is no
  78. requirement that the root of a filesystem be given for either upper or
  79. lower.
  80. A wide range of filesystems supported by Linux can be the lower filesystem,
  81. but not all filesystems that are mountable by Linux have the features
  82. needed for OverlayFS to work. The lower filesystem does not need to be
  83. writable. The lower filesystem can even be another overlayfs. The upper
  84. filesystem will normally be writable and if it is it must support the
  85. creation of trusted.* and/or user.* extended attributes, and must provide
  86. valid d_type in readdir responses, so NFS is not suitable.
  87. A read-only overlay of two read-only filesystems may use any
  88. filesystem type.
  89. Directories
  90. -----------
  91. Overlaying mainly involves directories. If a given name appears in both
  92. upper and lower filesystems and refers to a non-directory in either,
  93. then the lower object is hidden - the name refers only to the upper
  94. object.
  95. Where both upper and lower objects are directories, a merged directory
  96. is formed.
  97. At mount time, the two directories given as mount options "lowerdir" and
  98. "upperdir" are combined into a merged directory::
  99. mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
  100. workdir=/work /merged
  101. The "workdir" needs to be an empty directory on the same filesystem
  102. as upperdir.
  103. Then whenever a lookup is requested in such a merged directory, the
  104. lookup is performed in each actual directory and the combined result
  105. is cached in the dentry belonging to the overlay filesystem. If both
  106. actual lookups find directories, both are stored and a merged
  107. directory is created, otherwise only one is stored: the upper if it
  108. exists, else the lower.
  109. Only the lists of names from directories are merged. Other content
  110. such as metadata and extended attributes are reported for the upper
  111. directory only. These attributes of the lower directory are hidden.
  112. whiteouts and opaque directories
  113. --------------------------------
  114. In order to support rm and rmdir without changing the lower
  115. filesystem, an overlay filesystem needs to record in the upper filesystem
  116. that files have been removed. This is done using whiteouts and opaque
  117. directories (non-directories are always opaque).
  118. A whiteout is created as a character device with 0/0 device number or
  119. as a zero-size regular file with the xattr "trusted.overlay.whiteout".
  120. When a whiteout is found in the upper level of a merged directory, any
  121. matching name in the lower level is ignored, and the whiteout itself
  122. is also hidden.
  123. A directory is made opaque by setting the xattr "trusted.overlay.opaque"
  124. to "y". Where the upper filesystem contains an opaque directory, any
  125. directory in the lower filesystem with the same name is ignored.
  126. An opaque directory should not conntain any whiteouts, because they do not
  127. serve any purpose. A merge directory containing regular files with the xattr
  128. "trusted.overlay.whiteout", should be additionally marked by setting the xattr
  129. "trusted.overlay.opaque" to "x" on the merge directory itself.
  130. This is needed to avoid the overhead of checking the "trusted.overlay.whiteout"
  131. on all entries during readdir in the common case.
  132. readdir
  133. -------
  134. When a 'readdir' request is made on a merged directory, the upper and
  135. lower directories are each read and the name lists merged in the
  136. obvious way (upper is read first, then lower - entries that already
  137. exist are not re-added). This merged name list is cached in the
  138. 'struct file' and so remains as long as the file is kept open. If the
  139. directory is opened and read by two processes at the same time, they
  140. will each have separate caches. A seekdir to the start of the
  141. directory (offset 0) followed by a readdir will cause the cache to be
  142. discarded and rebuilt.
  143. This means that changes to the merged directory do not appear while a
  144. directory is being read. This is unlikely to be noticed by many
  145. programs.
  146. seek offsets are assigned sequentially when the directories are read.
  147. Thus if:
  148. - read part of a directory
  149. - remember an offset, and close the directory
  150. - re-open the directory some time later
  151. - seek to the remembered offset
  152. there may be little correlation between the old and new locations in
  153. the list of filenames, particularly if anything has changed in the
  154. directory.
  155. Readdir on directories that are not merged is simply handled by the
  156. underlying directory (upper or lower).
  157. renaming directories
  158. --------------------
  159. When renaming a directory that is on the lower layer or merged (i.e. the
  160. directory was not created on the upper layer to start with) overlayfs can
  161. handle it in two different ways:
  162. 1. return EXDEV error: this error is returned by rename(2) when trying to
  163. move a file or directory across filesystem boundaries. Hence
  164. applications are usually prepared to handle this error (mv(1) for example
  165. recursively copies the directory tree). This is the default behavior.
  166. 2. If the "redirect_dir" feature is enabled, then the directory will be
  167. copied up (but not the contents). Then the "trusted.overlay.redirect"
  168. extended attribute is set to the path of the original location from the
  169. root of the overlay. Finally the directory is moved to the new
  170. location.
  171. There are several ways to tune the "redirect_dir" feature.
  172. Kernel config options:
  173. - OVERLAY_FS_REDIRECT_DIR:
  174. If this is enabled, then redirect_dir is turned on by default.
  175. - OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW:
  176. If this is enabled, then redirects are always followed by default. Enabling
  177. this results in a less secure configuration. Enable this option only when
  178. worried about backward compatibility with kernels that have the redirect_dir
  179. feature and follow redirects even if turned off.
  180. Module options (can also be changed through /sys/module/overlay/parameters/):
  181. - "redirect_dir=BOOL":
  182. See OVERLAY_FS_REDIRECT_DIR kernel config option above.
  183. - "redirect_always_follow=BOOL":
  184. See OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW kernel config option above.
  185. - "redirect_max=NUM":
  186. The maximum number of bytes in an absolute redirect (default is 256).
  187. Mount options:
  188. - "redirect_dir=on":
  189. Redirects are enabled.
  190. - "redirect_dir=follow":
  191. Redirects are not created, but followed.
  192. - "redirect_dir=nofollow":
  193. Redirects are not created and not followed.
  194. - "redirect_dir=off":
  195. If "redirect_always_follow" is enabled in the kernel/module config,
  196. this "off" translates to "follow", otherwise it translates to "nofollow".
  197. When the NFS export feature is enabled, every copied up directory is
  198. indexed by the file handle of the lower inode and a file handle of the
  199. upper directory is stored in a "trusted.overlay.upper" extended attribute
  200. on the index entry. On lookup of a merged directory, if the upper
  201. directory does not match the file handle stores in the index, that is an
  202. indication that multiple upper directories may be redirected to the same
  203. lower directory. In that case, lookup returns an error and warns about
  204. a possible inconsistency.
  205. Because lower layer redirects cannot be verified with the index, enabling
  206. NFS export support on an overlay filesystem with no upper layer requires
  207. turning off redirect follow (e.g. "redirect_dir=nofollow").
  208. Non-directories
  209. ---------------
  210. Objects that are not directories (files, symlinks, device-special
  211. files etc.) are presented either from the upper or lower filesystem as
  212. appropriate. When a file in the lower filesystem is accessed in a way
  213. the requires write-access, such as opening for write access, changing
  214. some metadata etc., the file is first copied from the lower filesystem
  215. to the upper filesystem (copy_up). Note that creating a hard-link
  216. also requires copy_up, though of course creation of a symlink does
  217. not.
  218. The copy_up may turn out to be unnecessary, for example if the file is
  219. opened for read-write but the data is not modified.
  220. The copy_up process first makes sure that the containing directory
  221. exists in the upper filesystem - creating it and any parents as
  222. necessary. It then creates the object with the same metadata (owner,
  223. mode, mtime, symlink-target etc.) and then if the object is a file, the
  224. data is copied from the lower to the upper filesystem. Finally any
  225. extended attributes are copied up.
  226. Once the copy_up is complete, the overlay filesystem simply
  227. provides direct access to the newly created file in the upper
  228. filesystem - future operations on the file are barely noticed by the
  229. overlay filesystem (though an operation on the name of the file such as
  230. rename or unlink will of course be noticed and handled).
  231. Permission model
  232. ----------------
  233. Permission checking in the overlay filesystem follows these principles:
  234. 1) permission check SHOULD return the same result before and after copy up
  235. 2) task creating the overlay mount MUST NOT gain additional privileges
  236. 3) non-mounting task MAY gain additional privileges through the overlay,
  237. compared to direct access on underlying lower or upper filesystems
  238. This is achieved by performing two permission checks on each access:
  239. a) check if current task is allowed access based on local DAC (owner,
  240. group, mode and posix acl), as well as MAC checks
  241. b) check if mounting task would be allowed real operation on lower or
  242. upper layer based on underlying filesystem permissions, again including
  243. MAC checks
  244. Check (a) ensures consistency (1) since owner, group, mode and posix acls
  245. are copied up. On the other hand it can result in server enforced
  246. permissions (used by NFS, for example) being ignored (3).
  247. Check (b) ensures that no task gains permissions to underlying layers that
  248. the mounting task does not have (2). This also means that it is possible
  249. to create setups where the consistency rule (1) does not hold; normally,
  250. however, the mounting task will have sufficient privileges to perform all
  251. operations.
  252. Another way to demonstrate this model is drawing parallels between::
  253. mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,... /merged
  254. and::
  255. cp -a /lower /upper
  256. mount --bind /upper /merged
  257. The resulting access permissions should be the same. The difference is in
  258. the time of copy (on-demand vs. up-front).
  259. Multiple lower layers
  260. ---------------------
  261. Multiple lower layers can now be given using the colon (":") as a
  262. separator character between the directory names. For example::
  263. mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
  264. As the example shows, "upperdir=" and "workdir=" may be omitted. In
  265. that case the overlay will be read-only.
  266. The specified lower directories will be stacked beginning from the
  267. rightmost one and going left. In the above example lower1 will be the
  268. top, lower2 the middle and lower3 the bottom layer.
  269. Note: directory names containing colons can be provided as lower layer by
  270. escaping the colons with a single backslash. For example::
  271. mount -t overlay overlay -olowerdir=/a\:lower\:\:dir /merged
  272. Since kernel version v6.8, directory names containing colons can also
  273. be configured as lower layer using the "lowerdir+" mount options and the
  274. fsconfig syscall from new mount api. For example::
  275. fsconfig(fs_fd, FSCONFIG_SET_STRING, "lowerdir+", "/a:lower::dir", 0);
  276. In the latter case, colons in lower layer directory names will be escaped
  277. as an octal characters (\072) when displayed in /proc/self/mountinfo.
  278. Metadata only copy up
  279. ---------------------
  280. When the "metacopy" feature is enabled, overlayfs will only copy
  281. up metadata (as opposed to whole file), when a metadata specific operation
  282. like chown/chmod is performed. An upper file in this state is marked with
  283. "trusted.overlayfs.metacopy" xattr which indicates that the upper file
  284. contains no data. The data will be copied up later when file is opened for
  285. WRITE operation. After the lower file's data is copied up,
  286. the "trusted.overlayfs.metacopy" xattr is removed from the upper file.
  287. In other words, this is delayed data copy up operation and data is copied
  288. up when there is a need to actually modify data.
  289. There are multiple ways to enable/disable this feature. A config option
  290. CONFIG_OVERLAY_FS_METACOPY can be set/unset to enable/disable this feature
  291. by default. Or one can enable/disable it at module load time with module
  292. parameter metacopy=on/off. Lastly, there is also a per mount option
  293. metacopy=on/off to enable/disable this feature per mount.
  294. Do not use metacopy=on with untrusted upper/lower directories. Otherwise
  295. it is possible that an attacker can create a handcrafted file with
  296. appropriate REDIRECT and METACOPY xattrs, and gain access to file on lower
  297. pointed by REDIRECT. This should not be possible on local system as setting
  298. "trusted." xattrs will require CAP_SYS_ADMIN. But it should be possible
  299. for untrusted layers like from a pen drive.
  300. Note: redirect_dir={off|nofollow|follow[*]} and nfs_export=on mount options
  301. conflict with metacopy=on, and will result in an error.
  302. [*] redirect_dir=follow only conflicts with metacopy=on if upperdir=... is
  303. given.
  304. Data-only lower layers
  305. ----------------------
  306. With "metacopy" feature enabled, an overlayfs regular file may be a composition
  307. of information from up to three different layers:
  308. 1) metadata from a file in the upper layer
  309. 2) st_ino and st_dev object identifier from a file in a lower layer
  310. 3) data from a file in another lower layer (further below)
  311. The "lower data" file can be on any lower layer, except from the top most
  312. lower layer.
  313. Below the top most lower layer, any number of lower most layers may be defined
  314. as "data-only" lower layers, using double colon ("::") separators.
  315. A normal lower layer is not allowed to be below a data-only layer, so single
  316. colon separators are not allowed to the right of double colon ("::") separators.
  317. For example::
  318. mount -t overlay overlay -olowerdir=/l1:/l2:/l3::/do1::/do2 /merged
  319. The paths of files in the "data-only" lower layers are not visible in the
  320. merged overlayfs directories and the metadata and st_ino/st_dev of files
  321. in the "data-only" lower layers are not visible in overlayfs inodes.
  322. Only the data of the files in the "data-only" lower layers may be visible
  323. when a "metacopy" file in one of the lower layers above it, has a "redirect"
  324. to the absolute path of the "lower data" file in the "data-only" lower layer.
  325. Since kernel version v6.8, "data-only" lower layers can also be added using
  326. the "datadir+" mount options and the fsconfig syscall from new mount api.
  327. For example::
  328. fsconfig(fs_fd, FSCONFIG_SET_STRING, "lowerdir+", "/l1", 0);
  329. fsconfig(fs_fd, FSCONFIG_SET_STRING, "lowerdir+", "/l2", 0);
  330. fsconfig(fs_fd, FSCONFIG_SET_STRING, "lowerdir+", "/l3", 0);
  331. fsconfig(fs_fd, FSCONFIG_SET_STRING, "datadir+", "/do1", 0);
  332. fsconfig(fs_fd, FSCONFIG_SET_STRING, "datadir+", "/do2", 0);
  333. fs-verity support
  334. -----------------
  335. During metadata copy up of a lower file, if the source file has
  336. fs-verity enabled and overlay verity support is enabled, then the
  337. digest of the lower file is added to the "trusted.overlay.metacopy"
  338. xattr. This is then used to verify the content of the lower file
  339. each the time the metacopy file is opened.
  340. When a layer containing verity xattrs is used, it means that any such
  341. metacopy file in the upper layer is guaranteed to match the content
  342. that was in the lower at the time of the copy-up. If at any time
  343. (during a mount, after a remount, etc) such a file in the lower is
  344. replaced or modified in any way, access to the corresponding file in
  345. overlayfs will result in EIO errors (either on open, due to overlayfs
  346. digest check, or from a later read due to fs-verity) and a detailed
  347. error is printed to the kernel logs. For more details of how fs-verity
  348. file access works, see :ref:`Documentation/filesystems/fsverity.rst
  349. <accessing_verity_files>`.
  350. Verity can be used as a general robustness check to detect accidental
  351. changes in the overlayfs directories in use. But, with additional care
  352. it can also give more powerful guarantees. For example, if the upper
  353. layer is fully trusted (by using dm-verity or something similar), then
  354. an untrusted lower layer can be used to supply validated file content
  355. for all metacopy files. If additionally the untrusted lower
  356. directories are specified as "Data-only", then they can only supply
  357. such file content, and the entire mount can be trusted to match the
  358. upper layer.
  359. This feature is controlled by the "verity" mount option, which
  360. supports these values:
  361. - "off":
  362. The metacopy digest is never generated or used. This is the
  363. default if verity option is not specified.
  364. - "on":
  365. Whenever a metacopy files specifies an expected digest, the
  366. corresponding data file must match the specified digest. When
  367. generating a metacopy file the verity digest will be set in it
  368. based on the source file (if it has one).
  369. - "require":
  370. Same as "on", but additionally all metacopy files must specify a
  371. digest (or EIO is returned on open). This means metadata copy up
  372. will only be used if the data file has fs-verity enabled,
  373. otherwise a full copy-up is used.
  374. Sharing and copying layers
  375. --------------------------
  376. Lower layers may be shared among several overlay mounts and that is indeed
  377. a very common practice. An overlay mount may use the same lower layer
  378. path as another overlay mount and it may use a lower layer path that is
  379. beneath or above the path of another overlay lower layer path.
  380. Using an upper layer path and/or a workdir path that are already used by
  381. another overlay mount is not allowed and may fail with EBUSY. Using
  382. partially overlapping paths is not allowed and may fail with EBUSY.
  383. If files are accessed from two overlayfs mounts which share or overlap the
  384. upper layer and/or workdir path the behavior of the overlay is undefined,
  385. though it will not result in a crash or deadlock.
  386. Mounting an overlay using an upper layer path, where the upper layer path
  387. was previously used by another mounted overlay in combination with a
  388. different lower layer path, is allowed, unless the "index" or "metacopy"
  389. features are enabled.
  390. With the "index" feature, on the first time mount, an NFS file
  391. handle of the lower layer root directory, along with the UUID of the lower
  392. filesystem, are encoded and stored in the "trusted.overlay.origin" extended
  393. attribute on the upper layer root directory. On subsequent mount attempts,
  394. the lower root directory file handle and lower filesystem UUID are compared
  395. to the stored origin in upper root directory. On failure to verify the
  396. lower root origin, mount will fail with ESTALE. An overlayfs mount with
  397. "index" enabled will fail with EOPNOTSUPP if the lower filesystem
  398. does not support NFS export, lower filesystem does not have a valid UUID or
  399. if the upper filesystem does not support extended attributes.
  400. For the "metacopy" feature, there is no verification mechanism at
  401. mount time. So if same upper is mounted with different set of lower, mount
  402. probably will succeed but expect the unexpected later on. So don't do it.
  403. It is quite a common practice to copy overlay layers to a different
  404. directory tree on the same or different underlying filesystem, and even
  405. to a different machine. With the "index" feature, trying to mount
  406. the copied layers will fail the verification of the lower root file handle.
  407. Nesting overlayfs mounts
  408. ------------------------
  409. It is possible to use a lower directory that is stored on an overlayfs
  410. mount. For regular files this does not need any special care. However, files
  411. that have overlayfs attributes, such as whiteouts or "overlay.*" xattrs will be
  412. interpreted by the underlying overlayfs mount and stripped out. In order to
  413. allow the second overlayfs mount to see the attributes they must be escaped.
  414. Overlayfs specific xattrs are escaped by using a special prefix of
  415. "overlay.overlay.". So, a file with a "trusted.overlay.overlay.metacopy" xattr
  416. in the lower dir will be exposed as a regular file with a
  417. "trusted.overlay.metacopy" xattr in the overlayfs mount. This can be nested by
  418. repeating the prefix multiple time, as each instance only removes one prefix.
  419. A lower dir with a regular whiteout will always be handled by the overlayfs
  420. mount, so to support storing an effective whiteout file in an overlayfs mount an
  421. alternative form of whiteout is supported. This form is a regular, zero-size
  422. file with the "overlay.whiteout" xattr set, inside a directory with the
  423. "overlay.opaque" xattr set to "x" (see `whiteouts and opaque directories`_).
  424. These alternative whiteouts are never created by overlayfs, but can be used by
  425. userspace tools (like containers) that generate lower layers.
  426. These alternative whiteouts can be escaped using the standard xattr escape
  427. mechanism in order to properly nest to any depth.
  428. Non-standard behavior
  429. ---------------------
  430. Current version of overlayfs can act as a mostly POSIX compliant
  431. filesystem.
  432. This is the list of cases that overlayfs doesn't currently handle:
  433. a) POSIX mandates updating st_atime for reads. This is currently not
  434. done in the case when the file resides on a lower layer.
  435. b) If a file residing on a lower layer is opened for read-only and then
  436. memory mapped with MAP_SHARED, then subsequent changes to the file are not
  437. reflected in the memory mapping.
  438. c) If a file residing on a lower layer is being executed, then opening that
  439. file for write or truncating the file will not be denied with ETXTBSY.
  440. The following options allow overlayfs to act more like a standards
  441. compliant filesystem:
  442. redirect_dir
  443. ````````````
  444. Enabled with the mount option or module option: "redirect_dir=on" or with
  445. the kernel config option CONFIG_OVERLAY_FS_REDIRECT_DIR=y.
  446. If this feature is disabled, then rename(2) on a lower or merged directory
  447. will fail with EXDEV ("Invalid cross-device link").
  448. index
  449. `````
  450. Enabled with the mount option or module option "index=on" or with the
  451. kernel config option CONFIG_OVERLAY_FS_INDEX=y.
  452. If this feature is disabled and a file with multiple hard links is copied
  453. up, then this will "break" the link. Changes will not be propagated to
  454. other names referring to the same inode.
  455. xino
  456. ````
  457. Enabled with the mount option "xino=auto" or "xino=on", with the module
  458. option "xino_auto=on" or with the kernel config option
  459. CONFIG_OVERLAY_FS_XINO_AUTO=y. Also implicitly enabled by using the same
  460. underlying filesystem for all layers making up the overlay.
  461. If this feature is disabled or the underlying filesystem doesn't have
  462. enough free bits in the inode number, then overlayfs will not be able to
  463. guarantee that the values of st_ino and st_dev returned by stat(2) and the
  464. value of d_ino returned by readdir(3) will act like on a normal filesystem.
  465. E.g. the value of st_dev may be different for two objects in the same
  466. overlay filesystem and the value of st_ino for filesystem objects may not be
  467. persistent and could change even while the overlay filesystem is mounted, as
  468. summarized in the `Inode properties`_ table above.
  469. Changes to underlying filesystems
  470. ---------------------------------
  471. Changes to the underlying filesystems while part of a mounted overlay
  472. filesystem are not allowed. If the underlying filesystem is changed,
  473. the behavior of the overlay is undefined, though it will not result in
  474. a crash or deadlock.
  475. Offline changes, when the overlay is not mounted, are allowed to the
  476. upper tree. Offline changes to the lower tree are only allowed if the
  477. "metacopy", "index", "xino" and "redirect_dir" features
  478. have not been used. If the lower tree is modified and any of these
  479. features has been used, the behavior of the overlay is undefined,
  480. though it will not result in a crash or deadlock.
  481. When the overlay NFS export feature is enabled, overlay filesystems
  482. behavior on offline changes of the underlying lower layer is different
  483. than the behavior when NFS export is disabled.
  484. On every copy_up, an NFS file handle of the lower inode, along with the
  485. UUID of the lower filesystem, are encoded and stored in an extended
  486. attribute "trusted.overlay.origin" on the upper inode.
  487. When the NFS export feature is enabled, a lookup of a merged directory,
  488. that found a lower directory at the lookup path or at the path pointed
  489. to by the "trusted.overlay.redirect" extended attribute, will verify
  490. that the found lower directory file handle and lower filesystem UUID
  491. match the origin file handle that was stored at copy_up time. If a
  492. found lower directory does not match the stored origin, that directory
  493. will not be merged with the upper directory.
  494. NFS export
  495. ----------
  496. When the underlying filesystems supports NFS export and the "nfs_export"
  497. feature is enabled, an overlay filesystem may be exported to NFS.
  498. With the "nfs_export" feature, on copy_up of any lower object, an index
  499. entry is created under the index directory. The index entry name is the
  500. hexadecimal representation of the copy up origin file handle. For a
  501. non-directory object, the index entry is a hard link to the upper inode.
  502. For a directory object, the index entry has an extended attribute
  503. "trusted.overlay.upper" with an encoded file handle of the upper
  504. directory inode.
  505. When encoding a file handle from an overlay filesystem object, the
  506. following rules apply:
  507. 1. For a non-upper object, encode a lower file handle from lower inode
  508. 2. For an indexed object, encode a lower file handle from copy_up origin
  509. 3. For a pure-upper object and for an existing non-indexed upper object,
  510. encode an upper file handle from upper inode
  511. The encoded overlay file handle includes:
  512. - Header including path type information (e.g. lower/upper)
  513. - UUID of the underlying filesystem
  514. - Underlying filesystem encoding of underlying inode
  515. This encoding format is identical to the encoding format file handles that
  516. are stored in extended attribute "trusted.overlay.origin".
  517. When decoding an overlay file handle, the following steps are followed:
  518. 1. Find underlying layer by UUID and path type information.
  519. 2. Decode the underlying filesystem file handle to underlying dentry.
  520. 3. For a lower file handle, lookup the handle in index directory by name.
  521. 4. If a whiteout is found in index, return ESTALE. This represents an
  522. overlay object that was deleted after its file handle was encoded.
  523. 5. For a non-directory, instantiate a disconnected overlay dentry from the
  524. decoded underlying dentry, the path type and index inode, if found.
  525. 6. For a directory, use the connected underlying decoded dentry, path type
  526. and index, to lookup a connected overlay dentry.
  527. Decoding a non-directory file handle may return a disconnected dentry.
  528. copy_up of that disconnected dentry will create an upper index entry with
  529. no upper alias.
  530. When overlay filesystem has multiple lower layers, a middle layer
  531. directory may have a "redirect" to lower directory. Because middle layer
  532. "redirects" are not indexed, a lower file handle that was encoded from the
  533. "redirect" origin directory, cannot be used to find the middle or upper
  534. layer directory. Similarly, a lower file handle that was encoded from a
  535. descendant of the "redirect" origin directory, cannot be used to
  536. reconstruct a connected overlay path. To mitigate the cases of
  537. directories that cannot be decoded from a lower file handle, these
  538. directories are copied up on encode and encoded as an upper file handle.
  539. On an overlay filesystem with no upper layer this mitigation cannot be
  540. used NFS export in this setup requires turning off redirect follow (e.g.
  541. "redirect_dir=nofollow").
  542. The overlay filesystem does not support non-directory connectable file
  543. handles, so exporting with the 'subtree_check' exportfs configuration will
  544. cause failures to lookup files over NFS.
  545. When the NFS export feature is enabled, all directory index entries are
  546. verified on mount time to check that upper file handles are not stale.
  547. This verification may cause significant overhead in some cases.
  548. Note: the mount options index=off,nfs_export=on are conflicting for a
  549. read-write mount and will result in an error.
  550. Note: the mount option uuid=off can be used to replace UUID of the underlying
  551. filesystem in file handles with null, and effectively disable UUID checks. This
  552. can be useful in case the underlying disk is copied and the UUID of this copy
  553. is changed. This is only applicable if all lower/upper/work directories are on
  554. the same filesystem, otherwise it will fallback to normal behaviour.
  555. UUID and fsid
  556. -------------
  557. The UUID of overlayfs instance itself and the fsid reported by statfs(2) are
  558. controlled by the "uuid" mount option, which supports these values:
  559. - "null":
  560. UUID of overlayfs is null. fsid is taken from upper most filesystem.
  561. - "off":
  562. UUID of overlayfs is null. fsid is taken from upper most filesystem.
  563. UUID of underlying layers is ignored.
  564. - "on":
  565. UUID of overlayfs is generated and used to report a unique fsid.
  566. UUID is stored in xattr "trusted.overlay.uuid", making overlayfs fsid
  567. unique and persistent. This option requires an overlayfs with upper
  568. filesystem that supports xattrs.
  569. - "auto": (default)
  570. UUID is taken from xattr "trusted.overlay.uuid" if it exists.
  571. Upgrade to "uuid=on" on first time mount of new overlay filesystem that
  572. meets the prerequites.
  573. Downgrade to "uuid=null" for existing overlay filesystems that were never
  574. mounted with "uuid=on".
  575. Volatile mount
  576. --------------
  577. This is enabled with the "volatile" mount option. Volatile mounts are not
  578. guaranteed to survive a crash. It is strongly recommended that volatile
  579. mounts are only used if data written to the overlay can be recreated
  580. without significant effort.
  581. The advantage of mounting with the "volatile" option is that all forms of
  582. sync calls to the upper filesystem are omitted.
  583. In order to avoid a giving a false sense of safety, the syncfs (and fsync)
  584. semantics of volatile mounts are slightly different than that of the rest of
  585. VFS. If any writeback error occurs on the upperdir's filesystem after a
  586. volatile mount takes place, all sync functions will return an error. Once this
  587. condition is reached, the filesystem will not recover, and every subsequent sync
  588. call will return an error, even if the upperdir has not experience a new error
  589. since the last sync call.
  590. When overlay is mounted with "volatile" option, the directory
  591. "$workdir/work/incompat/volatile" is created. During next mount, overlay
  592. checks for this directory and refuses to mount if present. This is a strong
  593. indicator that user should throw away upper and work directories and create
  594. fresh one. In very limited cases where the user knows that the system has
  595. not crashed and contents of upperdir are intact, The "volatile" directory
  596. can be removed.
  597. User xattr
  598. ----------
  599. The "-o userxattr" mount option forces overlayfs to use the
  600. "user.overlay." xattr namespace instead of "trusted.overlay.". This is
  601. useful for unprivileged mounting of overlayfs.
  602. Testsuite
  603. ---------
  604. There's a testsuite originally developed by David Howells and currently
  605. maintained by Amir Goldstein at:
  606. https://github.com/amir73il/unionmount-testsuite.git
  607. Run as root::
  608. # cd unionmount-testsuite
  609. # ./run --ov --verify