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