blob: c2c71be75e3d6faaa80c04276edee94b9ee2f530 [file] [log] [blame]
NeilBrowna907c902015-11-07 17:38:58 +11001Written by: Neil Brown
2Please see MAINTAINERS file for where to send questions.
Neil Brown7c37fbd2014-10-24 00:14:39 +02003
4Overlay Filesystem
5==================
6
7This document describes a prototype for a new approach to providing
8overlay-filesystem functionality in Linux (sometimes referred to as
9union-filesystems). An overlay-filesystem tries to present a
10filesystem which is the result over overlaying one filesystem on top
11of the other.
12
Amir Goldstein16149012018-03-29 16:36:56 +030013
14Overlay objects
15---------------
16
17The overlay filesystem approach is 'hybrid', because the objects that
18appear in the filesystem do not always appear to belong to that filesystem.
19In many cases, an object accessed in the union will be indistinguishable
Neil Brown7c37fbd2014-10-24 00:14:39 +020020from accessing the corresponding object from the original filesystem.
21This is most obvious from the 'st_dev' field returned by stat(2).
22
23While directories will report an st_dev from the overlay-filesystem,
Amir Goldstein65f26732017-04-25 17:28:31 +030024non-directory objects may report an st_dev from the lower filesystem or
Neil Brown7c37fbd2014-10-24 00:14:39 +020025upper filesystem that is providing the object. Similarly st_ino will
26only be unique when combined with st_dev, and both of these can change
27over the lifetime of a non-directory object. Many applications and
28tools ignore these values and will not be affected.
29
Amir Goldstein65f26732017-04-25 17:28:31 +030030In the special case of all overlay layers on the same underlying
31filesystem, all objects will report an st_dev from the overlay
32filesystem and st_ino from the underlying filesystem. This will
33make the overlay mount more compliant with filesystem scanners and
34overlay objects will be distinguishable from the corresponding
35objects in the original filesystem.
36
Amir Goldstein16149012018-03-29 16:36:56 +030037On 64bit systems, even if all overlay layers are not on the same
38underlying filesystem, the same compliant behavior could be achieved
39with the "xino" feature. The "xino" feature composes a unique object
40identifier from the real object st_ino and an underlying fsid index.
41If all underlying filesystems support NFS file handles and export file
42handles with 32bit inode number encoding (e.g. ext4), overlay filesystem
43will use the high inode number bits for fsid. Even when the underlying
44filesystem uses 64bit inode numbers, users can still enable the "xino"
45feature with the "-o xino=on" overlay mount option. That is useful for the
46case of underlying filesystems like xfs and tmpfs, which use 64bit inode
47numbers, but are very unlikely to use the high inode number bit.
48
49
Neil Brown7c37fbd2014-10-24 00:14:39 +020050Upper and Lower
51---------------
52
53An overlay filesystem combines two filesystems - an 'upper' filesystem
54and a 'lower' filesystem. When a name exists in both filesystems, the
55object in the 'upper' filesystem is visible while the object in the
56'lower' filesystem is either hidden or, in the case of directories,
57merged with the 'upper' object.
58
59It would be more correct to refer to an upper and lower 'directory
60tree' rather than 'filesystem' as it is quite possible for both
61directory trees to be in the same filesystem and there is no
62requirement that the root of a filesystem be given for either upper or
63lower.
64
65The lower filesystem can be any filesystem supported by Linux and does
66not need to be writable. The lower filesystem can even be another
67overlayfs. The upper filesystem will normally be writable and if it
68is it must support the creation of trusted.* extended attributes, and
69must provide valid d_type in readdir responses, so NFS is not suitable.
70
71A read-only overlay of two read-only filesystems may use any
72filesystem type.
73
74Directories
75-----------
76
77Overlaying mainly involves directories. If a given name appears in both
78upper and lower filesystems and refers to a non-directory in either,
79then the lower object is hidden - the name refers only to the upper
80object.
81
82Where both upper and lower objects are directories, a merged directory
83is formed.
84
85At mount time, the two directories given as mount options "lowerdir" and
86"upperdir" are combined into a merged directory:
87
Miklos Szeredief94b182014-11-20 16:39:59 +010088 mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
Amir Goldsteinc3c86992016-12-08 09:49:51 +020089 workdir=/work /merged
Neil Brown7c37fbd2014-10-24 00:14:39 +020090
91The "workdir" needs to be an empty directory on the same filesystem
92as upperdir.
93
94Then whenever a lookup is requested in such a merged directory, the
95lookup is performed in each actual directory and the combined result
96is cached in the dentry belonging to the overlay filesystem. If both
97actual lookups find directories, both are stored and a merged
98directory is created, otherwise only one is stored: the upper if it
99exists, else the lower.
100
101Only the lists of names from directories are merged. Other content
102such as metadata and extended attributes are reported for the upper
103directory only. These attributes of the lower directory are hidden.
104
105whiteouts and opaque directories
106--------------------------------
107
108In order to support rm and rmdir without changing the lower
109filesystem, an overlay filesystem needs to record in the upper filesystem
110that files have been removed. This is done using whiteouts and opaque
111directories (non-directories are always opaque).
112
113A whiteout is created as a character device with 0/0 device number.
114When a whiteout is found in the upper level of a merged directory, any
115matching name in the lower level is ignored, and the whiteout itself
116is also hidden.
117
118A directory is made opaque by setting the xattr "trusted.overlay.opaque"
119to "y". Where the upper filesystem contains an opaque directory, any
120directory in the lower filesystem with the same name is ignored.
121
122readdir
123-------
124
125When a 'readdir' request is made on a merged directory, the upper and
126lower directories are each read and the name lists merged in the
127obvious way (upper is read first, then lower - entries that already
128exist are not re-added). This merged name list is cached in the
129'struct file' and so remains as long as the file is kept open. If the
130directory is opened and read by two processes at the same time, they
131will each have separate caches. A seekdir to the start of the
132directory (offset 0) followed by a readdir will cause the cache to be
133discarded and rebuilt.
134
135This means that changes to the merged directory do not appear while a
136directory is being read. This is unlikely to be noticed by many
137programs.
138
139seek offsets are assigned sequentially when the directories are read.
140Thus if
Amir Goldsteinc3c86992016-12-08 09:49:51 +0200141
Neil Brown7c37fbd2014-10-24 00:14:39 +0200142 - read part of a directory
143 - remember an offset, and close the directory
144 - re-open the directory some time later
145 - seek to the remembered offset
146
147there may be little correlation between the old and new locations in
148the list of filenames, particularly if anything has changed in the
149directory.
150
151Readdir on directories that are not merged is simply handled by the
152underlying directory (upper or lower).
153
Miklos Szeredia6c60652016-12-16 11:02:56 +0100154renaming directories
155--------------------
156
157When renaming a directory that is on the lower layer or merged (i.e. the
158directory was not created on the upper layer to start with) overlayfs can
159handle it in two different ways:
160
Amir Goldsteinc3c86992016-12-08 09:49:51 +02001611. return EXDEV error: this error is returned by rename(2) when trying to
Miklos Szeredia6c60652016-12-16 11:02:56 +0100162 move a file or directory across filesystem boundaries. Hence
163 applications are usually prepared to hande this error (mv(1) for example
164 recursively copies the directory tree). This is the default behavior.
165
Amir Goldsteinc3c86992016-12-08 09:49:51 +02001662. If the "redirect_dir" feature is enabled, then the directory will be
Miklos Szeredia6c60652016-12-16 11:02:56 +0100167 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.
Neil Brown7c37fbd2014-10-24 00:14:39 +0200171
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100172There are several ways to tune the "redirect_dir" feature.
173
174Kernel config options:
175
176- OVERLAY_FS_REDIRECT_DIR:
177 If this is enabled, then redirect_dir is turned on by default.
178- OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW:
179 If this is enabled, then redirects are always followed by default. Enabling
180 this results in a less secure configuration. Enable this option only when
181 worried about backward compatibility with kernels that have the redirect_dir
182 feature and follow redirects even if turned off.
183
184Module options (can also be changed through /sys/module/overlay/parameters/*):
185
186- "redirect_dir=BOOL":
187 See OVERLAY_FS_REDIRECT_DIR kernel config option above.
188- "redirect_always_follow=BOOL":
189 See OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW kernel config option above.
190- "redirect_max=NUM":
191 The maximum number of bytes in an absolute redirect (default is 256).
192
193Mount options:
194
195- "redirect_dir=on":
196 Redirects are enabled.
197- "redirect_dir=follow":
198 Redirects are not created, but followed.
199- "redirect_dir=off":
200 Redirects are not created and only followed if "redirect_always_follow"
201 feature is enabled in the kernel/module config.
202- "redirect_dir=nofollow":
203 Redirects are not created and not followed (equivalent to "redirect_dir=off"
204 if "redirect_always_follow" feature is not enabled).
205
Amir Goldsteinf168f102018-01-19 11:26:53 +0200206When the NFS export feature is enabled, every copied up directory is
207indexed by the file handle of the lower inode and a file handle of the
208upper directory is stored in a "trusted.overlay.upper" extended attribute
209on the index entry. On lookup of a merged directory, if the upper
210directory does not match the file handle stores in the index, that is an
211indication that multiple upper directories may be redirected to the same
212lower directory. In that case, lookup returns an error and warns about
213a possible inconsistency.
214
215Because lower layer redirects cannot be verified with the index, enabling
216NFS export support on an overlay filesystem with no upper layer requires
217turning off redirect follow (e.g. "redirect_dir=nofollow").
218
Amir Goldsteinf168f102018-01-19 11:26:53 +0200219
Neil Brown7c37fbd2014-10-24 00:14:39 +0200220Non-directories
221---------------
222
223Objects that are not directories (files, symlinks, device-special
224files etc.) are presented either from the upper or lower filesystem as
225appropriate. When a file in the lower filesystem is accessed in a way
226the requires write-access, such as opening for write access, changing
227some metadata etc., the file is first copied from the lower filesystem
228to the upper filesystem (copy_up). Note that creating a hard-link
229also requires copy_up, though of course creation of a symlink does
230not.
231
232The copy_up may turn out to be unnecessary, for example if the file is
233opened for read-write but the data is not modified.
234
235The copy_up process first makes sure that the containing directory
236exists in the upper filesystem - creating it and any parents as
237necessary. It then creates the object with the same metadata (owner,
238mode, mtime, symlink-target etc.) and then if the object is a file, the
239data is copied from the lower to the upper filesystem. Finally any
240extended attributes are copied up.
241
242Once the copy_up is complete, the overlay filesystem simply
243provides direct access to the newly created file in the upper
244filesystem - future operations on the file are barely noticed by the
245overlay filesystem (though an operation on the name of the file such as
246rename or unlink will of course be noticed and handled).
247
248
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100249Multiple lower layers
250---------------------
251
252Multiple lower layers can now be given using the the colon (":") as a
253separator character between the directory names. For example:
254
255 mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
256
Miklos Szeredi6d900f5a2015-01-08 15:09:15 +0100257As the example shows, "upperdir=" and "workdir=" may be omitted. In
258that case the overlay will be read-only.
259
260The specified lower directories will be stacked beginning from the
261rightmost one and going left. In the above example lower1 will be the
262top, lower2 the middle and lower3 the bottom layer.
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100263
264
Amir Goldstein94128122017-05-25 15:08:24 +0300265Sharing and copying layers
266--------------------------
267
268Lower layers may be shared among several overlay mounts and that is indeed
269a very common practice. An overlay mount may use the same lower layer
270path as another overlay mount and it may use a lower layer path that is
271beneath or above the path of another overlay lower layer path.
272
273Using an upper layer path and/or a workdir path that are already used by
Amir Goldstein85fdee12017-09-29 10:21:21 +0300274another overlay mount is not allowed and may fail with EBUSY. Using
Amir Goldstein94128122017-05-25 15:08:24 +0300275partially overlapping paths is not allowed but will not fail with EBUSY.
Amir Goldstein85fdee12017-09-29 10:21:21 +0300276If files are accessed from two overlayfs mounts which share or overlap the
277upper layer and/or workdir path the behavior of the overlay is undefined,
278though it will not result in a crash or deadlock.
Amir Goldstein94128122017-05-25 15:08:24 +0300279
280Mounting an overlay using an upper layer path, where the upper layer path
281was previously used by another mounted overlay in combination with a
282different lower layer path, is allowed, unless the "inodes index" feature
283is enabled.
284
285With the "inodes index" feature, on the first time mount, an NFS file
286handle of the lower layer root directory, along with the UUID of the lower
287filesystem, are encoded and stored in the "trusted.overlay.origin" extended
288attribute on the upper layer root directory. On subsequent mount attempts,
289the lower root directory file handle and lower filesystem UUID are compared
290to the stored origin in upper root directory. On failure to verify the
291lower root origin, mount will fail with ESTALE. An overlayfs mount with
292"inodes index" enabled will fail with EOPNOTSUPP if the lower filesystem
293does not support NFS export, lower filesystem does not have a valid UUID or
294if the upper filesystem does not support extended attributes.
295
296It is quite a common practice to copy overlay layers to a different
297directory tree on the same or different underlying filesystem, and even
298to a different machine. With the "inodes index" feature, trying to mount
299the copied layers will fail the verification of the lower root file handle.
300
301
Neil Brown7c37fbd2014-10-24 00:14:39 +0200302Non-standard behavior
303---------------------
304
Miklos Szeredi0c31d672018-07-18 15:44:44 +0200305Overlayfs can now act as a POSIX compliant filesystem with the following
306features turned on:
Neil Brown7c37fbd2014-10-24 00:14:39 +0200307
Miklos Szeredi0c31d672018-07-18 15:44:44 +02003081) "redirect_dir"
Amir Goldstein16149012018-03-29 16:36:56 +0300309
Miklos Szeredi0c31d672018-07-18 15:44:44 +0200310Enabled with the mount option or module option: "redirect_dir=on" or with
311the kernel config option CONFIG_OVERLAY_FS_REDIRECT_DIR=y.
Neil Brown7c37fbd2014-10-24 00:14:39 +0200312
Miklos Szeredi0c31d672018-07-18 15:44:44 +0200313If this feature is disabled, then rename(2) on a lower or merged directory
314will fail with EXDEV ("Invalid cross-device link").
Neil Brown7c37fbd2014-10-24 00:14:39 +0200315
Miklos Szeredi0c31d672018-07-18 15:44:44 +02003162) "inode index"
317
318Enabled with the mount option or module option "index=on" or with the
319kernel config option CONFIG_OVERLAY_FS_INDEX=y.
320
321If this feature is disabled and a file with multiple hard links is copied
322up, then this will "break" the link. Changes will not be propagated to
323other names referring to the same inode.
324
3253) "xino"
326
327Enabled with the mount option "xino=auto" or "xino=on", with the module
328option "xino_auto=on" or with the kernel config option
329CONFIG_OVERLAY_FS_XINO_AUTO=y. Also implicitly enabled by using the same
330underlying filesystem for all layers making up the overlay.
331
332If this feature is disabled or the underlying filesystem doesn't have
333enough free bits in the inode number, then overlayfs will not be able to
334guarantee that the values of st_ino and st_dev returned by stat(2) and the
335value of d_ino returned by readdir(3) will act like on a normal filesystem.
336E.g. the value of st_dev may be different for two objects in the same
337overlay filesystem and the value of st_ino for directory objects may not be
338persistent and could change even while the overlay filesystem is mounted.
Miklos Szeredi2d8f2902016-12-16 11:02:54 +0100339
Amir Goldstein16149012018-03-29 16:36:56 +0300340
Neil Brown7c37fbd2014-10-24 00:14:39 +0200341Changes to underlying filesystems
342---------------------------------
343
344Offline changes, when the overlay is not mounted, are allowed to either
345the upper or the lower trees.
346
347Changes to the underlying filesystems while part of a mounted overlay
348filesystem are not allowed. If the underlying filesystem is changed,
349the behavior of the overlay is undefined, though it will not result in
350a crash or deadlock.
Miklos Szeredi2b7a8f362014-12-13 00:59:53 +0100351
Amir Goldsteinf168f102018-01-19 11:26:53 +0200352When the overlay NFS export feature is enabled, overlay filesystems
353behavior on offline changes of the underlying lower layer is different
354than the behavior when NFS export is disabled.
355
356On every copy_up, an NFS file handle of the lower inode, along with the
357UUID of the lower filesystem, are encoded and stored in an extended
358attribute "trusted.overlay.origin" on the upper inode.
359
360When the NFS export feature is enabled, a lookup of a merged directory,
361that found a lower directory at the lookup path or at the path pointed
362to by the "trusted.overlay.redirect" extended attribute, will verify
363that the found lower directory file handle and lower filesystem UUID
364match the origin file handle that was stored at copy_up time. If a
365found lower directory does not match the stored origin, that directory
366will not be merged with the upper directory.
367
368
Amir Goldsteina01f64b2017-05-25 22:39:21 +0300369
370NFS export
371----------
372
373When the underlying filesystems supports NFS export and the "nfs_export"
374feature is enabled, an overlay filesystem may be exported to NFS.
375
376With the "nfs_export" feature, on copy_up of any lower object, an index
377entry is created under the index directory. The index entry name is the
378hexadecimal representation of the copy up origin file handle. For a
379non-directory object, the index entry is a hard link to the upper inode.
380For a directory object, the index entry has an extended attribute
381"trusted.overlay.upper" with an encoded file handle of the upper
382directory inode.
383
384When encoding a file handle from an overlay filesystem object, the
385following rules apply:
386
3871. For a non-upper object, encode a lower file handle from lower inode
3882. For an indexed object, encode a lower file handle from copy_up origin
3893. For a pure-upper object and for an existing non-indexed upper object,
390 encode an upper file handle from upper inode
391
392The encoded overlay file handle includes:
393 - Header including path type information (e.g. lower/upper)
394 - UUID of the underlying filesystem
395 - Underlying filesystem encoding of underlying inode
396
397This encoding format is identical to the encoding format file handles that
398are stored in extended attribute "trusted.overlay.origin".
399
400When decoding an overlay file handle, the following steps are followed:
401
4021. Find underlying layer by UUID and path type information.
4032. Decode the underlying filesystem file handle to underlying dentry.
4043. For a lower file handle, lookup the handle in index directory by name.
4054. If a whiteout is found in index, return ESTALE. This represents an
406 overlay object that was deleted after its file handle was encoded.
4075. For a non-directory, instantiate a disconnected overlay dentry from the
408 decoded underlying dentry, the path type and index inode, if found.
4096. For a directory, use the connected underlying decoded dentry, path type
410 and index, to lookup a connected overlay dentry.
411
412Decoding a non-directory file handle may return a disconnected dentry.
413copy_up of that disconnected dentry will create an upper index entry with
414no upper alias.
415
416When overlay filesystem has multiple lower layers, a middle layer
417directory may have a "redirect" to lower directory. Because middle layer
418"redirects" are not indexed, a lower file handle that was encoded from the
419"redirect" origin directory, cannot be used to find the middle or upper
420layer directory. Similarly, a lower file handle that was encoded from a
421descendant of the "redirect" origin directory, cannot be used to
422reconstruct a connected overlay path. To mitigate the cases of
423directories that cannot be decoded from a lower file handle, these
424directories are copied up on encode and encoded as an upper file handle.
425On an overlay filesystem with no upper layer this mitigation cannot be
426used NFS export in this setup requires turning off redirect follow (e.g.
427"redirect_dir=nofollow").
428
429The overlay filesystem does not support non-directory connectable file
430handles, so exporting with the 'subtree_check' exportfs configuration will
431cause failures to lookup files over NFS.
432
433When the NFS export feature is enabled, all directory index entries are
434verified on mount time to check that upper file handles are not stale.
435This verification may cause significant overhead in some cases.
436
437
Miklos Szeredi2b7a8f362014-12-13 00:59:53 +0100438Testsuite
439---------
440
Amir Goldstein05af4fe2018-05-07 13:57:44 +0300441There's a testsuite originally developed by David Howells and currently
442maintained by Amir Goldstein at:
Miklos Szeredi2b7a8f362014-12-13 00:59:53 +0100443
Amir Goldstein05af4fe2018-05-07 13:57:44 +0300444 https://github.com/amir73il/unionmount-testsuite.git
Miklos Szeredi2b7a8f362014-12-13 00:59:53 +0100445
446Run as root:
447
448 # cd unionmount-testsuite
Amir Goldstein05af4fe2018-05-07 13:57:44 +0300449 # ./run --ov --verify