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