OSDN Git Service

Revert "usb: dwc3: turn off VBUS when leaving host mode"
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / Documentation / filesystems / overlayfs.txt
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 The result will inevitably fail to look exactly like a normal
14 filesystem for various technical reasons.  The expectation is that
15 many use cases will be able to ignore these differences.
16
17 This approach is 'hybrid' because the objects that appear in the
18 filesystem do not all appear to belong to that filesystem.  In many
19 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 all non-directory objects will report an st_dev from the lower 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 Upper and Lower
31 ---------------
32
33 An overlay filesystem combines two filesystems - an 'upper' filesystem
34 and a 'lower' filesystem.  When a name exists in both filesystems, the
35 object in the 'upper' filesystem is visible while the object in the
36 'lower' filesystem is either hidden or, in the case of directories,
37 merged with the 'upper' object.
38
39 It would be more correct to refer to an upper and lower 'directory
40 tree' rather than 'filesystem' as it is quite possible for both
41 directory trees to be in the same filesystem and there is no
42 requirement that the root of a filesystem be given for either upper or
43 lower.
44
45 The lower filesystem can be any filesystem supported by Linux and does
46 not need to be writable.  The lower filesystem can even be another
47 overlayfs.  The upper filesystem will normally be writable and if it
48 is it must support the creation of trusted.* extended attributes, and
49 must provide valid d_type in readdir responses, so NFS is not suitable.
50
51 A read-only overlay of two read-only filesystems may use any
52 filesystem type.
53
54 Directories
55 -----------
56
57 Overlaying mainly involves directories.  If a given name appears in both
58 upper and lower filesystems and refers to a non-directory in either,
59 then the lower object is hidden - the name refers only to the upper
60 object.
61
62 Where both upper and lower objects are directories, a merged directory
63 is formed.
64
65 At mount time, the two directories given as mount options "lowerdir" and
66 "upperdir" are combined into a merged directory:
67
68   mount -t overlay overlay -olowerdir=/lower,upperdir=/upper,\
69 workdir=/work /merged
70
71 The "workdir" needs to be an empty directory on the same filesystem
72 as upperdir.
73
74 Then whenever a lookup is requested in such a merged directory, the
75 lookup is performed in each actual directory and the combined result
76 is cached in the dentry belonging to the overlay filesystem.  If both
77 actual lookups find directories, both are stored and a merged
78 directory is created, otherwise only one is stored: the upper if it
79 exists, else the lower.
80
81 Only the lists of names from directories are merged.  Other content
82 such as metadata and extended attributes are reported for the upper
83 directory only.  These attributes of the lower directory are hidden.
84
85 credentials
86 -----------
87
88 By default, all access to the upper, lower and work directories is the
89 recorded mounter's MAC and DAC credentials.  The incoming accesses are
90 checked against the caller's credentials.
91
92 In the case where caller MAC or DAC credentials do not overlap, a
93 use case available in older versions of the driver, the
94 override_creds mount flag can be turned off and help when the use
95 pattern has caller with legitimate credentials where the mounter
96 does not.  Several unintended side effects will occur though.  The
97 caller without certain key capabilities or lower privilege will not
98 always be able to delete files or directories, create nodes, or
99 search some restricted directories.  The ability to search and read
100 a directory entry is spotty as a result of the cache mechanism not
101 retesting the credentials because of the assumption, a privileged
102 caller can fill cache, then a lower privilege can read the directory
103 cache.  The uneven security model where cache, upperdir and workdir
104 are opened at privilege, but accessed without creating a form of
105 privilege escalation, should only be used with strict understanding
106 of the side effects and of the security policies.
107
108 whiteouts and opaque directories
109 --------------------------------
110
111 In order to support rm and rmdir without changing the lower
112 filesystem, an overlay filesystem needs to record in the upper filesystem
113 that files have been removed.  This is done using whiteouts and opaque
114 directories (non-directories are always opaque).
115
116 A whiteout is created as a character device with 0/0 device number.
117 When a whiteout is found in the upper level of a merged directory, any
118 matching name in the lower level is ignored, and the whiteout itself
119 is also hidden.
120
121 A directory is made opaque by setting the xattr "trusted.overlay.opaque"
122 to "y".  Where the upper filesystem contains an opaque directory, any
123 directory in the lower filesystem with the same name is ignored.
124
125 readdir
126 -------
127
128 When a 'readdir' request is made on a merged directory, the upper and
129 lower directories are each read and the name lists merged in the
130 obvious way (upper is read first, then lower - entries that already
131 exist are not re-added).  This merged name list is cached in the
132 'struct file' and so remains as long as the file is kept open.  If the
133 directory is opened and read by two processes at the same time, they
134 will each have separate caches.  A seekdir to the start of the
135 directory (offset 0) followed by a readdir will cause the cache to be
136 discarded and rebuilt.
137
138 This means that changes to the merged directory do not appear while a
139 directory is being read.  This is unlikely to be noticed by many
140 programs.
141
142 seek offsets are assigned sequentially when the directories are read.
143 Thus if
144   - read part of a directory
145   - remember an offset, and close the directory
146   - re-open the directory some time later
147   - seek to the remembered offset
148
149 there may be little correlation between the old and new locations in
150 the list of filenames, particularly if anything has changed in the
151 directory.
152
153 Readdir on directories that are not merged is simply handled by the
154 underlying directory (upper or lower).
155
156
157 Non-directories
158 ---------------
159
160 Objects that are not directories (files, symlinks, device-special
161 files etc.) are presented either from the upper or lower filesystem as
162 appropriate.  When a file in the lower filesystem is accessed in a way
163 the requires write-access, such as opening for write access, changing
164 some metadata etc., the file is first copied from the lower filesystem
165 to the upper filesystem (copy_up).  Note that creating a hard-link
166 also requires copy_up, though of course creation of a symlink does
167 not.
168
169 The copy_up may turn out to be unnecessary, for example if the file is
170 opened for read-write but the data is not modified.
171
172 The copy_up process first makes sure that the containing directory
173 exists in the upper filesystem - creating it and any parents as
174 necessary.  It then creates the object with the same metadata (owner,
175 mode, mtime, symlink-target etc.) and then if the object is a file, the
176 data is copied from the lower to the upper filesystem.  Finally any
177 extended attributes are copied up.
178
179 Once the copy_up is complete, the overlay filesystem simply
180 provides direct access to the newly created file in the upper
181 filesystem - future operations on the file are barely noticed by the
182 overlay filesystem (though an operation on the name of the file such as
183 rename or unlink will of course be noticed and handled).
184
185
186 Multiple lower layers
187 ---------------------
188
189 Multiple lower layers can now be given using the the colon (":") as a
190 separator character between the directory names.  For example:
191
192   mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
193
194 As the example shows, "upperdir=" and "workdir=" may be omitted.  In
195 that case the overlay will be read-only.
196
197 The specified lower directories will be stacked beginning from the
198 rightmost one and going left.  In the above example lower1 will be the
199 top, lower2 the middle and lower3 the bottom layer.
200
201
202 Non-standard behavior
203 ---------------------
204
205 The copy_up operation essentially creates a new, identical file and
206 moves it over to the old name.  The new file may be on a different
207 filesystem, so both st_dev and st_ino of the file may change.
208
209 Any open files referring to this inode will access the old data and
210 metadata.  Similarly any file locks obtained before copy_up will not
211 apply to the copied up file.
212
213 On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
214 fsetxattr(2) will fail with EROFS.
215
216 If a file with multiple hard links is copied up, then this will
217 "break" the link.  Changes will not be propagated to other names
218 referring to the same inode.
219
220 Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
221 object in overlayfs will not contain valid absolute paths, only
222 relative paths leading up to the filesystem's root.  This will be
223 fixed in the future.
224
225 Some operations are not atomic, for example a crash during copy_up or
226 rename will leave the filesystem in an inconsistent state.  This will
227 be addressed in the future.
228
229 Changes to underlying filesystems
230 ---------------------------------
231
232 Offline changes, when the overlay is not mounted, are allowed to either
233 the upper or the lower trees.
234
235 Changes to the underlying filesystems while part of a mounted overlay
236 filesystem are not allowed.  If the underlying filesystem is changed,
237 the behavior of the overlay is undefined, though it will not result in
238 a crash or deadlock.
239
240 Testsuite
241 ---------
242
243 There's testsuite developed by David Howells at:
244
245   git://git.infradead.org/users/dhowells/unionmount-testsuite.git
246
247 Run as root:
248
249   # cd unionmount-testsuite
250   # ./run --ov