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