]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - Documentation/filesystems/overlayfs.txt
objtool: Fix "noreturn" detection for recursive sibling calls
[mirror_ubuntu-bionic-kernel.git] / Documentation / filesystems / overlayfs.txt
CommitLineData
a907c907
N
1Written by: Neil Brown
2Please see MAINTAINERS file for where to send questions.
7c37fbda
NB
3
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
13The result will inevitably fail to look exactly like a normal
14filesystem for various technical reasons. The expectation is that
15many use cases will be able to ignore these differences.
16
17This approach is 'hybrid' because the objects that appear in the
18filesystem do not all appear to belong to that filesystem. In many
19cases an object accessed in the union will be indistinguishable
20from 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,
65f26738 24non-directory objects may report an st_dev from the lower filesystem or
7c37fbda
NB
25upper 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
65f26738
AG
30In 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
7c37fbda
NB
37Upper and Lower
38---------------
39
40An overlay filesystem combines two filesystems - an 'upper' filesystem
41and a 'lower' filesystem. When a name exists in both filesystems, the
42object in the 'upper' filesystem is visible while the object in the
43'lower' filesystem is either hidden or, in the case of directories,
44merged with the 'upper' object.
45
46It would be more correct to refer to an upper and lower 'directory
47tree' rather than 'filesystem' as it is quite possible for both
48directory trees to be in the same filesystem and there is no
49requirement that the root of a filesystem be given for either upper or
50lower.
51
52The lower filesystem can be any filesystem supported by Linux and does
53not need to be writable. The lower filesystem can even be another
54overlayfs. The upper filesystem will normally be writable and if it
55is it must support the creation of trusted.* extended attributes, and
56must provide valid d_type in readdir responses, so NFS is not suitable.
57
58A read-only overlay of two read-only filesystems may use any
59filesystem type.
60
61Directories
62-----------
63
64Overlaying mainly involves directories. If a given name appears in both
65upper and lower filesystems and refers to a non-directory in either,
66then the lower object is hidden - the name refers only to the upper
67object.
68
69Where both upper and lower objects are directories, a merged directory
70is formed.
71
72At mount time, the two directories given as mount options "lowerdir" and
73"upperdir" are combined into a merged directory:
74
ef94b186 75 mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
c3c86996 76 workdir=/work /merged
7c37fbda
NB
77
78The "workdir" needs to be an empty directory on the same filesystem
79as upperdir.
80
81Then whenever a lookup is requested in such a merged directory, the
82lookup is performed in each actual directory and the combined result
83is cached in the dentry belonging to the overlay filesystem. If both
84actual lookups find directories, both are stored and a merged
85directory is created, otherwise only one is stored: the upper if it
86exists, else the lower.
87
88Only the lists of names from directories are merged. Other content
89such as metadata and extended attributes are reported for the upper
90directory only. These attributes of the lower directory are hidden.
91
92whiteouts and opaque directories
93--------------------------------
94
95In order to support rm and rmdir without changing the lower
96filesystem, an overlay filesystem needs to record in the upper filesystem
97that files have been removed. This is done using whiteouts and opaque
98directories (non-directories are always opaque).
99
100A whiteout is created as a character device with 0/0 device number.
101When a whiteout is found in the upper level of a merged directory, any
102matching name in the lower level is ignored, and the whiteout itself
103is also hidden.
104
105A directory is made opaque by setting the xattr "trusted.overlay.opaque"
106to "y". Where the upper filesystem contains an opaque directory, any
107directory in the lower filesystem with the same name is ignored.
108
109readdir
110-------
111
112When a 'readdir' request is made on a merged directory, the upper and
113lower directories are each read and the name lists merged in the
114obvious way (upper is read first, then lower - entries that already
115exist are not re-added). This merged name list is cached in the
116'struct file' and so remains as long as the file is kept open. If the
117directory is opened and read by two processes at the same time, they
118will each have separate caches. A seekdir to the start of the
119directory (offset 0) followed by a readdir will cause the cache to be
120discarded and rebuilt.
121
122This means that changes to the merged directory do not appear while a
123directory is being read. This is unlikely to be noticed by many
124programs.
125
126seek offsets are assigned sequentially when the directories are read.
127Thus if
c3c86996 128
7c37fbda
NB
129 - read part of a directory
130 - remember an offset, and close the directory
131 - re-open the directory some time later
132 - seek to the remembered offset
133
134there may be little correlation between the old and new locations in
135the list of filenames, particularly if anything has changed in the
136directory.
137
138Readdir on directories that are not merged is simply handled by the
139underlying directory (upper or lower).
140
a6c60655
MS
141renaming directories
142--------------------
143
144When renaming a directory that is on the lower layer or merged (i.e. the
145directory was not created on the upper layer to start with) overlayfs can
146handle it in two different ways:
147
c3c86996 1481. return EXDEV error: this error is returned by rename(2) when trying to
a6c60655
MS
149 move a file or directory across filesystem boundaries. Hence
150 applications are usually prepared to hande this error (mv(1) for example
151 recursively copies the directory tree). This is the default behavior.
152
c3c86996 1532. If the "redirect_dir" feature is enabled, then the directory will be
a6c60655
MS
154 copied up (but not the contents). Then the "trusted.overlay.redirect"
155 extended attribute is set to the path of the original location from the
156 root of the overlay. Finally the directory is moved to the new
157 location.
7c37fbda 158
438c84c2
MS
159There are several ways to tune the "redirect_dir" feature.
160
161Kernel config options:
162
163- OVERLAY_FS_REDIRECT_DIR:
164 If this is enabled, then redirect_dir is turned on by default.
165- OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW:
166 If this is enabled, then redirects are always followed by default. Enabling
167 this results in a less secure configuration. Enable this option only when
168 worried about backward compatibility with kernels that have the redirect_dir
169 feature and follow redirects even if turned off.
170
171Module options (can also be changed through /sys/module/overlay/parameters/*):
172
173- "redirect_dir=BOOL":
174 See OVERLAY_FS_REDIRECT_DIR kernel config option above.
175- "redirect_always_follow=BOOL":
176 See OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW kernel config option above.
177- "redirect_max=NUM":
178 The maximum number of bytes in an absolute redirect (default is 256).
179
180Mount options:
181
182- "redirect_dir=on":
183 Redirects are enabled.
184- "redirect_dir=follow":
185 Redirects are not created, but followed.
186- "redirect_dir=off":
187 Redirects are not created and only followed if "redirect_always_follow"
188 feature is enabled in the kernel/module config.
189- "redirect_dir=nofollow":
190 Redirects are not created and not followed (equivalent to "redirect_dir=off"
191 if "redirect_always_follow" feature is not enabled).
192
7c37fbda
NB
193Non-directories
194---------------
195
196Objects that are not directories (files, symlinks, device-special
197files etc.) are presented either from the upper or lower filesystem as
198appropriate. When a file in the lower filesystem is accessed in a way
199the requires write-access, such as opening for write access, changing
200some metadata etc., the file is first copied from the lower filesystem
201to the upper filesystem (copy_up). Note that creating a hard-link
202also requires copy_up, though of course creation of a symlink does
203not.
204
205The copy_up may turn out to be unnecessary, for example if the file is
206opened for read-write but the data is not modified.
207
208The copy_up process first makes sure that the containing directory
209exists in the upper filesystem - creating it and any parents as
210necessary. It then creates the object with the same metadata (owner,
211mode, mtime, symlink-target etc.) and then if the object is a file, the
212data is copied from the lower to the upper filesystem. Finally any
213extended attributes are copied up.
214
215Once the copy_up is complete, the overlay filesystem simply
216provides direct access to the newly created file in the upper
217filesystem - future operations on the file are barely noticed by the
218overlay filesystem (though an operation on the name of the file such as
219rename or unlink will of course be noticed and handled).
220
221
a78d9f0d
MS
222Multiple lower layers
223---------------------
224
225Multiple lower layers can now be given using the the colon (":") as a
226separator character between the directory names. For example:
227
228 mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
229
6d900f5a
MS
230As the example shows, "upperdir=" and "workdir=" may be omitted. In
231that case the overlay will be read-only.
232
233The specified lower directories will be stacked beginning from the
234rightmost one and going left. In the above example lower1 will be the
235top, lower2 the middle and lower3 the bottom layer.
a78d9f0d
MS
236
237
9412812e
AG
238Sharing and copying layers
239--------------------------
240
241Lower layers may be shared among several overlay mounts and that is indeed
242a very common practice. An overlay mount may use the same lower layer
243path as another overlay mount and it may use a lower layer path that is
244beneath or above the path of another overlay lower layer path.
245
246Using an upper layer path and/or a workdir path that are already used by
85fdee1e 247another overlay mount is not allowed and may fail with EBUSY. Using
9412812e 248partially overlapping paths is not allowed but will not fail with EBUSY.
85fdee1e
AG
249If files are accessed from two overlayfs mounts which share or overlap the
250upper layer and/or workdir path the behavior of the overlay is undefined,
251though it will not result in a crash or deadlock.
9412812e
AG
252
253Mounting an overlay using an upper layer path, where the upper layer path
254was previously used by another mounted overlay in combination with a
255different lower layer path, is allowed, unless the "inodes index" feature
256is enabled.
257
258With the "inodes index" feature, on the first time mount, an NFS file
259handle of the lower layer root directory, along with the UUID of the lower
260filesystem, are encoded and stored in the "trusted.overlay.origin" extended
261attribute on the upper layer root directory. On subsequent mount attempts,
262the lower root directory file handle and lower filesystem UUID are compared
263to the stored origin in upper root directory. On failure to verify the
264lower root origin, mount will fail with ESTALE. An overlayfs mount with
265"inodes index" enabled will fail with EOPNOTSUPP if the lower filesystem
266does not support NFS export, lower filesystem does not have a valid UUID or
267if the upper filesystem does not support extended attributes.
268
269It is quite a common practice to copy overlay layers to a different
270directory tree on the same or different underlying filesystem, and even
271to a different machine. With the "inodes index" feature, trying to mount
272the copied layers will fail the verification of the lower root file handle.
273
274
7c37fbda
NB
275Non-standard behavior
276---------------------
277
278The copy_up operation essentially creates a new, identical file and
279moves it over to the old name. The new file may be on a different
280filesystem, so both st_dev and st_ino of the file may change.
281
026e5e0c 282Any open files referring to this inode will access the old data.
7c37fbda 283
7c37fbda
NB
284If a file with multiple hard links is copied up, then this will
285"break" the link. Changes will not be propagated to other names
286referring to the same inode.
287
a6c60655
MS
288Unless "redirect_dir" feature is enabled, rename(2) on a lower or merged
289directory will fail with EXDEV.
2d8f2908 290
7c37fbda
NB
291Changes to underlying filesystems
292---------------------------------
293
294Offline changes, when the overlay is not mounted, are allowed to either
295the upper or the lower trees.
296
297Changes to the underlying filesystems while part of a mounted overlay
298filesystem are not allowed. If the underlying filesystem is changed,
299the behavior of the overlay is undefined, though it will not result in
300a crash or deadlock.
2b7a8f36
MS
301
302Testsuite
303---------
304
305There's testsuite developed by David Howells at:
306
307 git://git.infradead.org/users/dhowells/unionmount-testsuite.git
308
309Run as root:
310
311 # cd unionmount-testsuite
312 # ./run --ov