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