]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
e9be9d5e MS |
2 | /* |
3 | * | |
4 | * Copyright (C) 2011 Novell Inc. | |
e9be9d5e MS |
5 | */ |
6 | ||
5b825c3a | 7 | #include <uapi/linux/magic.h> |
e9be9d5e MS |
8 | #include <linux/fs.h> |
9 | #include <linux/namei.h> | |
10 | #include <linux/xattr.h> | |
e9be9d5e | 11 | #include <linux/mount.h> |
e9be9d5e MS |
12 | #include <linux/parser.h> |
13 | #include <linux/module.h> | |
cc259639 | 14 | #include <linux/statfs.h> |
f45827e8 | 15 | #include <linux/seq_file.h> |
d837a49b | 16 | #include <linux/posix_acl_xattr.h> |
e487d889 | 17 | #include <linux/exportfs.h> |
2b1a7746 | 18 | #include <linux/file.h> |
e9be9d5e MS |
19 | #include "overlayfs.h" |
20 | ||
21 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
22 | MODULE_DESCRIPTION("Overlay filesystem"); | |
23 | MODULE_LICENSE("GPL"); | |
24 | ||
e9be9d5e MS |
25 | |
26 | struct ovl_dir_cache; | |
27 | ||
a78d9f0d MS |
28 | #define OVL_MAX_STACK 500 |
29 | ||
688ea0e5 MS |
30 | static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); |
31 | module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); | |
253e7483 | 32 | MODULE_PARM_DESC(redirect_dir, |
688ea0e5 | 33 | "Default to on or off for the redirect_dir feature"); |
e9be9d5e | 34 | |
438c84c2 MS |
35 | static bool ovl_redirect_always_follow = |
36 | IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); | |
37 | module_param_named(redirect_always_follow, ovl_redirect_always_follow, | |
38 | bool, 0644); | |
253e7483 | 39 | MODULE_PARM_DESC(redirect_always_follow, |
438c84c2 MS |
40 | "Follow redirects even if redirect_dir feature is turned off"); |
41 | ||
02bcd157 AG |
42 | static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); |
43 | module_param_named(index, ovl_index_def, bool, 0644); | |
253e7483 | 44 | MODULE_PARM_DESC(index, |
02bcd157 AG |
45 | "Default to on or off for the inodes index feature"); |
46 | ||
f168f109 AG |
47 | static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); |
48 | module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); | |
253e7483 | 49 | MODULE_PARM_DESC(nfs_export, |
f168f109 AG |
50 | "Default to on or off for the NFS export feature"); |
51 | ||
795939a9 AG |
52 | static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); |
53 | module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); | |
253e7483 | 54 | MODULE_PARM_DESC(xino_auto, |
795939a9 AG |
55 | "Auto enable xino feature"); |
56 | ||
4155c10a MS |
57 | static void ovl_entry_stack_free(struct ovl_entry *oe) |
58 | { | |
59 | unsigned int i; | |
60 | ||
61 | for (i = 0; i < oe->numlower; i++) | |
62 | dput(oe->lowerstack[i].dentry); | |
63 | } | |
64 | ||
d5791044 VG |
65 | static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); |
66 | module_param_named(metacopy, ovl_metacopy_def, bool, 0644); | |
253e7483 | 67 | MODULE_PARM_DESC(metacopy, |
d5791044 VG |
68 | "Default to on or off for the metadata only copy up feature"); |
69 | ||
e9be9d5e MS |
70 | static void ovl_dentry_release(struct dentry *dentry) |
71 | { | |
72 | struct ovl_entry *oe = dentry->d_fsdata; | |
73 | ||
74 | if (oe) { | |
4155c10a | 75 | ovl_entry_stack_free(oe); |
e9be9d5e MS |
76 | kfree_rcu(oe, rcu); |
77 | } | |
78 | } | |
79 | ||
2d902671 | 80 | static struct dentry *ovl_d_real(struct dentry *dentry, |
fb16043b | 81 | const struct inode *inode) |
d101a125 | 82 | { |
cef4cbff | 83 | struct dentry *real = NULL, *lower; |
d101a125 | 84 | |
e8c985ba MS |
85 | /* It's an overlay file */ |
86 | if (inode && d_inode(dentry) == inode) | |
87 | return dentry; | |
88 | ||
ca4c8a3a | 89 | if (!d_is_reg(dentry)) { |
d101a125 MS |
90 | if (!inode || inode == d_inode(dentry)) |
91 | return dentry; | |
92 | goto bug; | |
93 | } | |
94 | ||
95 | real = ovl_dentry_upper(dentry); | |
2c3d7358 | 96 | if (real && (inode == d_inode(real))) |
d101a125 MS |
97 | return real; |
98 | ||
2c3d7358 VG |
99 | if (real && !inode && ovl_has_upperdata(d_inode(dentry))) |
100 | return real; | |
101 | ||
cef4cbff MS |
102 | lower = ovl_dentry_lowerdata(dentry); |
103 | if (!lower) | |
d101a125 | 104 | goto bug; |
cef4cbff | 105 | real = lower; |
d101a125 | 106 | |
c4fcfc16 | 107 | /* Handle recursion */ |
fb16043b | 108 | real = d_real(real, inode); |
c4fcfc16 | 109 | |
d101a125 MS |
110 | if (!inode || inode == d_inode(real)) |
111 | return real; | |
d101a125 | 112 | bug: |
cef4cbff MS |
113 | WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n", |
114 | __func__, dentry, inode ? inode->i_sb->s_id : "NULL", | |
115 | inode ? inode->i_ino : 0, real, | |
116 | real && d_inode(real) ? d_inode(real)->i_ino : 0); | |
d101a125 MS |
117 | return dentry; |
118 | } | |
119 | ||
3bb7df92 | 120 | static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak) |
7c03b5d4 | 121 | { |
7c03b5d4 MS |
122 | int ret = 1; |
123 | ||
3bb7df92 MS |
124 | if (weak) { |
125 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) | |
126 | ret = d->d_op->d_weak_revalidate(d, flags); | |
127 | } else if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
128 | ret = d->d_op->d_revalidate(d, flags); | |
129 | if (!ret) { | |
130 | if (!(flags & LOOKUP_RCU)) | |
131 | d_invalidate(d); | |
132 | ret = -ESTALE; | |
7c03b5d4 MS |
133 | } |
134 | } | |
3bb7df92 | 135 | return ret; |
7c03b5d4 MS |
136 | } |
137 | ||
3bb7df92 MS |
138 | static int ovl_dentry_revalidate_common(struct dentry *dentry, |
139 | unsigned int flags, bool weak) | |
7c03b5d4 MS |
140 | { |
141 | struct ovl_entry *oe = dentry->d_fsdata; | |
bccece1e | 142 | struct dentry *upper; |
7c03b5d4 MS |
143 | unsigned int i; |
144 | int ret = 1; | |
145 | ||
bccece1e MS |
146 | upper = ovl_dentry_upper(dentry); |
147 | if (upper) | |
148 | ret = ovl_revalidate_real(upper, flags, weak); | |
149 | ||
3bb7df92 MS |
150 | for (i = 0; ret > 0 && i < oe->numlower; i++) { |
151 | ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags, | |
152 | weak); | |
7c03b5d4 MS |
153 | } |
154 | return ret; | |
155 | } | |
156 | ||
3bb7df92 MS |
157 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
158 | { | |
159 | return ovl_dentry_revalidate_common(dentry, flags, false); | |
160 | } | |
161 | ||
162 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
163 | { | |
164 | return ovl_dentry_revalidate_common(dentry, flags, true); | |
165 | } | |
166 | ||
e9be9d5e MS |
167 | static const struct dentry_operations ovl_dentry_operations = { |
168 | .d_release = ovl_dentry_release, | |
d101a125 | 169 | .d_real = ovl_d_real, |
7c03b5d4 MS |
170 | .d_revalidate = ovl_dentry_revalidate, |
171 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
172 | }; | |
173 | ||
13cf199d AG |
174 | static struct kmem_cache *ovl_inode_cachep; |
175 | ||
176 | static struct inode *ovl_alloc_inode(struct super_block *sb) | |
177 | { | |
fd60b288 | 178 | struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL); |
13cf199d | 179 | |
b3885bd6 HN |
180 | if (!oi) |
181 | return NULL; | |
182 | ||
04a01ac7 | 183 | oi->cache = NULL; |
cf31c463 | 184 | oi->redirect = NULL; |
04a01ac7 | 185 | oi->version = 0; |
13c72075 | 186 | oi->flags = 0; |
09d8b586 | 187 | oi->__upperdentry = NULL; |
ffa5723c AG |
188 | oi->lowerpath.dentry = NULL; |
189 | oi->lowerpath.layer = NULL; | |
2664bd08 | 190 | oi->lowerdata = NULL; |
a015dafc | 191 | mutex_init(&oi->lock); |
25b7713a | 192 | |
13cf199d AG |
193 | return &oi->vfs_inode; |
194 | } | |
195 | ||
0b269ded | 196 | static void ovl_free_inode(struct inode *inode) |
13cf199d | 197 | { |
0b269ded | 198 | struct ovl_inode *oi = OVL_I(inode); |
13cf199d | 199 | |
0b269ded AV |
200 | kfree(oi->redirect); |
201 | mutex_destroy(&oi->lock); | |
202 | kmem_cache_free(ovl_inode_cachep, oi); | |
13cf199d AG |
203 | } |
204 | ||
205 | static void ovl_destroy_inode(struct inode *inode) | |
206 | { | |
09d8b586 MS |
207 | struct ovl_inode *oi = OVL_I(inode); |
208 | ||
209 | dput(oi->__upperdentry); | |
ffa5723c | 210 | dput(oi->lowerpath.dentry); |
2664bd08 VG |
211 | if (S_ISDIR(inode->i_mode)) |
212 | ovl_dir_cache_free(inode); | |
213 | else | |
214 | iput(oi->lowerdata); | |
13cf199d AG |
215 | } |
216 | ||
ad204488 | 217 | static void ovl_free_fs(struct ovl_fs *ofs) |
e9be9d5e | 218 | { |
df820f8d | 219 | struct vfsmount **mounts; |
dd662667 | 220 | unsigned i; |
e9be9d5e | 221 | |
0be0bfd2 | 222 | iput(ofs->workbasedir_trap); |
146d62e5 AG |
223 | iput(ofs->indexdir_trap); |
224 | iput(ofs->workdir_trap); | |
c21c839b | 225 | dput(ofs->whiteout); |
ad204488 MS |
226 | dput(ofs->indexdir); |
227 | dput(ofs->workdir); | |
228 | if (ofs->workdir_locked) | |
229 | ovl_inuse_unlock(ofs->workbasedir); | |
230 | dput(ofs->workbasedir); | |
231 | if (ofs->upperdir_locked) | |
08f4c7c8 | 232 | ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root); |
df820f8d MS |
233 | |
234 | /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */ | |
235 | mounts = (struct vfsmount **) ofs->layers; | |
b8e42a65 | 236 | for (i = 0; i < ofs->numlayer; i++) { |
94375f9d | 237 | iput(ofs->layers[i].trap); |
df820f8d | 238 | mounts[i] = ofs->layers[i].mnt; |
146d62e5 | 239 | } |
df820f8d | 240 | kern_unmount_array(mounts, ofs->numlayer); |
94375f9d | 241 | kfree(ofs->layers); |
b7bf9908 | 242 | for (i = 0; i < ofs->numfs; i++) |
07f1e596 AG |
243 | free_anon_bdev(ofs->fs[i].pseudo_dev); |
244 | kfree(ofs->fs); | |
ad204488 MS |
245 | |
246 | kfree(ofs->config.lowerdir); | |
247 | kfree(ofs->config.upperdir); | |
248 | kfree(ofs->config.workdir); | |
438c84c2 | 249 | kfree(ofs->config.redirect_mode); |
ad204488 MS |
250 | if (ofs->creator_cred) |
251 | put_cred(ofs->creator_cred); | |
252 | kfree(ofs); | |
e9be9d5e MS |
253 | } |
254 | ||
a9075cdb MS |
255 | static void ovl_put_super(struct super_block *sb) |
256 | { | |
257 | struct ovl_fs *ofs = sb->s_fs_info; | |
258 | ||
259 | ovl_free_fs(ofs); | |
260 | } | |
261 | ||
e8d4bfe3 | 262 | /* Sync real dirty inodes in upper filesystem (if it exists) */ |
e593b2bf AG |
263 | static int ovl_sync_fs(struct super_block *sb, int wait) |
264 | { | |
ad204488 | 265 | struct ovl_fs *ofs = sb->s_fs_info; |
e593b2bf AG |
266 | struct super_block *upper_sb; |
267 | int ret; | |
268 | ||
335d3fc5 SD |
269 | ret = ovl_sync_status(ofs); |
270 | /* | |
271 | * We have to always set the err, because the return value isn't | |
272 | * checked in syncfs, and instead indirectly return an error via | |
273 | * the sb's writeback errseq, which VFS inspects after this call. | |
274 | */ | |
275 | if (ret < 0) { | |
276 | errseq_set(&sb->s_wb_err, -EIO); | |
277 | return -EIO; | |
278 | } | |
279 | ||
280 | if (!ret) | |
281 | return ret; | |
e8d4bfe3 CX |
282 | |
283 | /* | |
32b1924b KK |
284 | * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC). |
285 | * All the super blocks will be iterated, including upper_sb. | |
e8d4bfe3 CX |
286 | * |
287 | * If this is a syncfs(2) call, then we do need to call | |
288 | * sync_filesystem() on upper_sb, but enough if we do it when being | |
289 | * called with wait == 1. | |
290 | */ | |
291 | if (!wait) | |
e593b2bf AG |
292 | return 0; |
293 | ||
08f4c7c8 | 294 | upper_sb = ovl_upper_mnt(ofs)->mnt_sb; |
e8d4bfe3 | 295 | |
e593b2bf | 296 | down_read(&upper_sb->s_umount); |
e8d4bfe3 | 297 | ret = sync_filesystem(upper_sb); |
e593b2bf | 298 | up_read(&upper_sb->s_umount); |
e8d4bfe3 | 299 | |
e593b2bf AG |
300 | return ret; |
301 | } | |
302 | ||
cc259639 AW |
303 | /** |
304 | * ovl_statfs | |
9c5dd803 | 305 | * @dentry: The dentry to query |
cc259639 AW |
306 | * @buf: The struct kstatfs to fill in with stats |
307 | * | |
308 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 309 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
310 | */ |
311 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
312 | { | |
313 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
314 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
315 | struct path path; | |
316 | int err; | |
317 | ||
4ebc5818 | 318 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
319 | |
320 | err = vfs_statfs(&path, buf); | |
321 | if (!err) { | |
6b2d5fe4 | 322 | buf->f_namelen = ofs->namelen; |
cc259639 AW |
323 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
324 | } | |
325 | ||
326 | return err; | |
327 | } | |
328 | ||
02bcd157 | 329 | /* Will this overlay be forced to mount/remount ro? */ |
ad204488 | 330 | static bool ovl_force_readonly(struct ovl_fs *ofs) |
02bcd157 | 331 | { |
08f4c7c8 | 332 | return (!ovl_upper_mnt(ofs) || !ofs->workdir); |
02bcd157 AG |
333 | } |
334 | ||
438c84c2 MS |
335 | static const char *ovl_redirect_mode_def(void) |
336 | { | |
337 | return ovl_redirect_dir_def ? "on" : "off"; | |
338 | } | |
339 | ||
795939a9 AG |
340 | static const char * const ovl_xino_str[] = { |
341 | "off", | |
342 | "auto", | |
343 | "on", | |
344 | }; | |
345 | ||
346 | static inline int ovl_xino_def(void) | |
347 | { | |
348 | return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; | |
349 | } | |
350 | ||
f45827e8 EZ |
351 | /** |
352 | * ovl_show_options | |
9c5dd803 YL |
353 | * @m: the seq_file handle |
354 | * @dentry: The dentry to query | |
f45827e8 EZ |
355 | * |
356 | * Prints the mount options for a given superblock. | |
357 | * Returns zero; does not fail. | |
358 | */ | |
359 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
360 | { | |
361 | struct super_block *sb = dentry->d_sb; | |
ad204488 | 362 | struct ovl_fs *ofs = sb->s_fs_info; |
f45827e8 | 363 | |
ad204488 MS |
364 | seq_show_option(m, "lowerdir", ofs->config.lowerdir); |
365 | if (ofs->config.upperdir) { | |
366 | seq_show_option(m, "upperdir", ofs->config.upperdir); | |
367 | seq_show_option(m, "workdir", ofs->config.workdir); | |
53a08cb9 | 368 | } |
ad204488 | 369 | if (ofs->config.default_permissions) |
8d3095f4 | 370 | seq_puts(m, ",default_permissions"); |
438c84c2 MS |
371 | if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) |
372 | seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); | |
ad204488 | 373 | if (ofs->config.index != ovl_index_def) |
438c84c2 | 374 | seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); |
5830fb6b PT |
375 | if (!ofs->config.uuid) |
376 | seq_puts(m, ",uuid=off"); | |
f168f109 AG |
377 | if (ofs->config.nfs_export != ovl_nfs_export_def) |
378 | seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ? | |
379 | "on" : "off"); | |
0f831ec8 | 380 | if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb)) |
795939a9 | 381 | seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); |
d5791044 VG |
382 | if (ofs->config.metacopy != ovl_metacopy_def) |
383 | seq_printf(m, ",metacopy=%s", | |
384 | ofs->config.metacopy ? "on" : "off"); | |
c86243b0 VG |
385 | if (ofs->config.ovl_volatile) |
386 | seq_puts(m, ",volatile"); | |
321b46b9 GS |
387 | if (ofs->config.userxattr) |
388 | seq_puts(m, ",userxattr"); | |
f45827e8 EZ |
389 | return 0; |
390 | } | |
391 | ||
3cdf6fe9 SL |
392 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
393 | { | |
ad204488 | 394 | struct ovl_fs *ofs = sb->s_fs_info; |
399c109d CX |
395 | struct super_block *upper_sb; |
396 | int ret = 0; | |
3cdf6fe9 | 397 | |
1751e8a6 | 398 | if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) |
3cdf6fe9 SL |
399 | return -EROFS; |
400 | ||
399c109d | 401 | if (*flags & SB_RDONLY && !sb_rdonly(sb)) { |
08f4c7c8 | 402 | upper_sb = ovl_upper_mnt(ofs)->mnt_sb; |
c86243b0 VG |
403 | if (ovl_should_sync(ofs)) { |
404 | down_read(&upper_sb->s_umount); | |
405 | ret = sync_filesystem(upper_sb); | |
406 | up_read(&upper_sb->s_umount); | |
407 | } | |
399c109d CX |
408 | } |
409 | ||
410 | return ret; | |
3cdf6fe9 SL |
411 | } |
412 | ||
e9be9d5e | 413 | static const struct super_operations ovl_super_operations = { |
13cf199d | 414 | .alloc_inode = ovl_alloc_inode, |
0b269ded | 415 | .free_inode = ovl_free_inode, |
13cf199d AG |
416 | .destroy_inode = ovl_destroy_inode, |
417 | .drop_inode = generic_delete_inode, | |
e9be9d5e | 418 | .put_super = ovl_put_super, |
e593b2bf | 419 | .sync_fs = ovl_sync_fs, |
cc259639 | 420 | .statfs = ovl_statfs, |
f45827e8 | 421 | .show_options = ovl_show_options, |
3cdf6fe9 | 422 | .remount_fs = ovl_remount, |
e9be9d5e MS |
423 | }; |
424 | ||
425 | enum { | |
426 | OPT_LOWERDIR, | |
427 | OPT_UPPERDIR, | |
428 | OPT_WORKDIR, | |
8d3095f4 | 429 | OPT_DEFAULT_PERMISSIONS, |
438c84c2 | 430 | OPT_REDIRECT_DIR, |
02bcd157 AG |
431 | OPT_INDEX_ON, |
432 | OPT_INDEX_OFF, | |
5830fb6b PT |
433 | OPT_UUID_ON, |
434 | OPT_UUID_OFF, | |
f168f109 | 435 | OPT_NFS_EXPORT_ON, |
2d2f2d73 | 436 | OPT_USERXATTR, |
f168f109 | 437 | OPT_NFS_EXPORT_OFF, |
795939a9 AG |
438 | OPT_XINO_ON, |
439 | OPT_XINO_OFF, | |
440 | OPT_XINO_AUTO, | |
d5791044 VG |
441 | OPT_METACOPY_ON, |
442 | OPT_METACOPY_OFF, | |
c86243b0 | 443 | OPT_VOLATILE, |
e9be9d5e MS |
444 | OPT_ERR, |
445 | }; | |
446 | ||
447 | static const match_table_t ovl_tokens = { | |
448 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
449 | {OPT_UPPERDIR, "upperdir=%s"}, | |
450 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 451 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
438c84c2 | 452 | {OPT_REDIRECT_DIR, "redirect_dir=%s"}, |
02bcd157 AG |
453 | {OPT_INDEX_ON, "index=on"}, |
454 | {OPT_INDEX_OFF, "index=off"}, | |
2d2f2d73 | 455 | {OPT_USERXATTR, "userxattr"}, |
5830fb6b PT |
456 | {OPT_UUID_ON, "uuid=on"}, |
457 | {OPT_UUID_OFF, "uuid=off"}, | |
f168f109 AG |
458 | {OPT_NFS_EXPORT_ON, "nfs_export=on"}, |
459 | {OPT_NFS_EXPORT_OFF, "nfs_export=off"}, | |
795939a9 AG |
460 | {OPT_XINO_ON, "xino=on"}, |
461 | {OPT_XINO_OFF, "xino=off"}, | |
462 | {OPT_XINO_AUTO, "xino=auto"}, | |
d5791044 VG |
463 | {OPT_METACOPY_ON, "metacopy=on"}, |
464 | {OPT_METACOPY_OFF, "metacopy=off"}, | |
c86243b0 | 465 | {OPT_VOLATILE, "volatile"}, |
e9be9d5e MS |
466 | {OPT_ERR, NULL} |
467 | }; | |
468 | ||
91c77947 MS |
469 | static char *ovl_next_opt(char **s) |
470 | { | |
471 | char *sbegin = *s; | |
472 | char *p; | |
473 | ||
474 | if (sbegin == NULL) | |
475 | return NULL; | |
476 | ||
477 | for (p = sbegin; *p; p++) { | |
478 | if (*p == '\\') { | |
479 | p++; | |
480 | if (!*p) | |
481 | break; | |
482 | } else if (*p == ',') { | |
483 | *p = '\0'; | |
484 | *s = p + 1; | |
485 | return sbegin; | |
486 | } | |
487 | } | |
488 | *s = NULL; | |
489 | return sbegin; | |
490 | } | |
491 | ||
438c84c2 MS |
492 | static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) |
493 | { | |
494 | if (strcmp(mode, "on") == 0) { | |
495 | config->redirect_dir = true; | |
496 | /* | |
497 | * Does not make sense to have redirect creation without | |
498 | * redirect following. | |
499 | */ | |
500 | config->redirect_follow = true; | |
501 | } else if (strcmp(mode, "follow") == 0) { | |
502 | config->redirect_follow = true; | |
503 | } else if (strcmp(mode, "off") == 0) { | |
504 | if (ovl_redirect_always_follow) | |
505 | config->redirect_follow = true; | |
506 | } else if (strcmp(mode, "nofollow") != 0) { | |
1bd0a3ae | 507 | pr_err("bad mount option \"redirect_dir=%s\"\n", |
438c84c2 MS |
508 | mode); |
509 | return -EINVAL; | |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
e9be9d5e MS |
515 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
516 | { | |
517 | char *p; | |
d5791044 | 518 | int err; |
d47748e5 | 519 | bool metacopy_opt = false, redirect_opt = false; |
b0def88d | 520 | bool nfs_export_opt = false, index_opt = false; |
e9be9d5e | 521 | |
438c84c2 MS |
522 | config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); |
523 | if (!config->redirect_mode) | |
524 | return -ENOMEM; | |
525 | ||
91c77947 | 526 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
527 | int token; |
528 | substring_t args[MAX_OPT_ARGS]; | |
529 | ||
530 | if (!*p) | |
531 | continue; | |
532 | ||
533 | token = match_token(p, ovl_tokens, args); | |
534 | switch (token) { | |
535 | case OPT_UPPERDIR: | |
536 | kfree(config->upperdir); | |
537 | config->upperdir = match_strdup(&args[0]); | |
538 | if (!config->upperdir) | |
539 | return -ENOMEM; | |
540 | break; | |
541 | ||
542 | case OPT_LOWERDIR: | |
543 | kfree(config->lowerdir); | |
544 | config->lowerdir = match_strdup(&args[0]); | |
545 | if (!config->lowerdir) | |
546 | return -ENOMEM; | |
547 | break; | |
548 | ||
549 | case OPT_WORKDIR: | |
550 | kfree(config->workdir); | |
551 | config->workdir = match_strdup(&args[0]); | |
552 | if (!config->workdir) | |
553 | return -ENOMEM; | |
554 | break; | |
555 | ||
8d3095f4 MS |
556 | case OPT_DEFAULT_PERMISSIONS: |
557 | config->default_permissions = true; | |
558 | break; | |
559 | ||
438c84c2 MS |
560 | case OPT_REDIRECT_DIR: |
561 | kfree(config->redirect_mode); | |
562 | config->redirect_mode = match_strdup(&args[0]); | |
563 | if (!config->redirect_mode) | |
564 | return -ENOMEM; | |
d47748e5 | 565 | redirect_opt = true; |
a6c60655 MS |
566 | break; |
567 | ||
02bcd157 AG |
568 | case OPT_INDEX_ON: |
569 | config->index = true; | |
b0def88d | 570 | index_opt = true; |
02bcd157 AG |
571 | break; |
572 | ||
573 | case OPT_INDEX_OFF: | |
574 | config->index = false; | |
b0def88d | 575 | index_opt = true; |
02bcd157 AG |
576 | break; |
577 | ||
5830fb6b PT |
578 | case OPT_UUID_ON: |
579 | config->uuid = true; | |
580 | break; | |
581 | ||
582 | case OPT_UUID_OFF: | |
583 | config->uuid = false; | |
584 | break; | |
585 | ||
f168f109 AG |
586 | case OPT_NFS_EXPORT_ON: |
587 | config->nfs_export = true; | |
b0def88d | 588 | nfs_export_opt = true; |
f168f109 AG |
589 | break; |
590 | ||
591 | case OPT_NFS_EXPORT_OFF: | |
592 | config->nfs_export = false; | |
b0def88d | 593 | nfs_export_opt = true; |
f168f109 AG |
594 | break; |
595 | ||
795939a9 AG |
596 | case OPT_XINO_ON: |
597 | config->xino = OVL_XINO_ON; | |
598 | break; | |
599 | ||
600 | case OPT_XINO_OFF: | |
601 | config->xino = OVL_XINO_OFF; | |
602 | break; | |
603 | ||
604 | case OPT_XINO_AUTO: | |
605 | config->xino = OVL_XINO_AUTO; | |
606 | break; | |
607 | ||
d5791044 VG |
608 | case OPT_METACOPY_ON: |
609 | config->metacopy = true; | |
d47748e5 | 610 | metacopy_opt = true; |
d5791044 VG |
611 | break; |
612 | ||
613 | case OPT_METACOPY_OFF: | |
614 | config->metacopy = false; | |
b0def88d | 615 | metacopy_opt = true; |
d5791044 VG |
616 | break; |
617 | ||
c86243b0 VG |
618 | case OPT_VOLATILE: |
619 | config->ovl_volatile = true; | |
620 | break; | |
621 | ||
2d2f2d73 MS |
622 | case OPT_USERXATTR: |
623 | config->userxattr = true; | |
624 | break; | |
625 | ||
e9be9d5e | 626 | default: |
1bd0a3ae | 627 | pr_err("unrecognized mount option \"%s\" or missing value\n", |
628 | p); | |
e9be9d5e MS |
629 | return -EINVAL; |
630 | } | |
631 | } | |
71cbad7e | 632 | |
f0e1266e AG |
633 | /* Workdir/index are useless in non-upper mount */ |
634 | if (!config->upperdir) { | |
635 | if (config->workdir) { | |
636 | pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
637 | config->workdir); | |
638 | kfree(config->workdir); | |
639 | config->workdir = NULL; | |
640 | } | |
641 | if (config->index && index_opt) { | |
642 | pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n"); | |
643 | index_opt = false; | |
644 | } | |
645 | config->index = false; | |
71cbad7e | 646 | } |
647 | ||
c86243b0 VG |
648 | if (!config->upperdir && config->ovl_volatile) { |
649 | pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n"); | |
650 | config->ovl_volatile = false; | |
651 | } | |
652 | ||
d5791044 VG |
653 | err = ovl_parse_redirect_mode(config, config->redirect_mode); |
654 | if (err) | |
655 | return err; | |
656 | ||
d47748e5 MS |
657 | /* |
658 | * This is to make the logic below simpler. It doesn't make any other | |
659 | * difference, since config->redirect_dir is only used for upper. | |
660 | */ | |
661 | if (!config->upperdir && config->redirect_follow) | |
662 | config->redirect_dir = true; | |
663 | ||
664 | /* Resolve metacopy -> redirect_dir dependency */ | |
665 | if (config->metacopy && !config->redirect_dir) { | |
666 | if (metacopy_opt && redirect_opt) { | |
1bd0a3ae | 667 | pr_err("conflicting options: metacopy=on,redirect_dir=%s\n", |
d47748e5 MS |
668 | config->redirect_mode); |
669 | return -EINVAL; | |
670 | } | |
671 | if (redirect_opt) { | |
672 | /* | |
673 | * There was an explicit redirect_dir=... that resulted | |
674 | * in this conflict. | |
675 | */ | |
1bd0a3ae | 676 | pr_info("disabling metacopy due to redirect_dir=%s\n", |
d47748e5 MS |
677 | config->redirect_mode); |
678 | config->metacopy = false; | |
679 | } else { | |
680 | /* Automatically enable redirect otherwise. */ | |
681 | config->redirect_follow = config->redirect_dir = true; | |
682 | } | |
d5791044 VG |
683 | } |
684 | ||
b0def88d AG |
685 | /* Resolve nfs_export -> index dependency */ |
686 | if (config->nfs_export && !config->index) { | |
f0e1266e AG |
687 | if (!config->upperdir && config->redirect_follow) { |
688 | pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); | |
689 | config->nfs_export = false; | |
690 | } else if (nfs_export_opt && index_opt) { | |
b0def88d AG |
691 | pr_err("conflicting options: nfs_export=on,index=off\n"); |
692 | return -EINVAL; | |
f0e1266e | 693 | } else if (index_opt) { |
b0def88d AG |
694 | /* |
695 | * There was an explicit index=off that resulted | |
696 | * in this conflict. | |
697 | */ | |
698 | pr_info("disabling nfs_export due to index=off\n"); | |
699 | config->nfs_export = false; | |
700 | } else { | |
701 | /* Automatically enable index otherwise. */ | |
702 | config->index = true; | |
703 | } | |
704 | } | |
705 | ||
706 | /* Resolve nfs_export -> !metacopy dependency */ | |
707 | if (config->nfs_export && config->metacopy) { | |
708 | if (nfs_export_opt && metacopy_opt) { | |
709 | pr_err("conflicting options: nfs_export=on,metacopy=on\n"); | |
710 | return -EINVAL; | |
711 | } | |
712 | if (metacopy_opt) { | |
713 | /* | |
714 | * There was an explicit metacopy=on that resulted | |
715 | * in this conflict. | |
716 | */ | |
717 | pr_info("disabling nfs_export due to metacopy=on\n"); | |
718 | config->nfs_export = false; | |
719 | } else { | |
720 | /* | |
721 | * There was an explicit nfs_export=on that resulted | |
722 | * in this conflict. | |
723 | */ | |
724 | pr_info("disabling metacopy due to nfs_export=on\n"); | |
725 | config->metacopy = false; | |
726 | } | |
727 | } | |
728 | ||
2d2f2d73 MS |
729 | |
730 | /* Resolve userxattr -> !redirect && !metacopy dependency */ | |
731 | if (config->userxattr) { | |
732 | if (config->redirect_follow && redirect_opt) { | |
733 | pr_err("conflicting options: userxattr,redirect_dir=%s\n", | |
734 | config->redirect_mode); | |
735 | return -EINVAL; | |
736 | } | |
737 | if (config->metacopy && metacopy_opt) { | |
738 | pr_err("conflicting options: userxattr,metacopy=on\n"); | |
739 | return -EINVAL; | |
740 | } | |
741 | /* | |
742 | * Silently disable default setting of redirect and metacopy. | |
743 | * This shall be the default in the future as well: these | |
744 | * options must be explicitly enabled if used together with | |
745 | * userxattr. | |
746 | */ | |
747 | config->redirect_dir = config->redirect_follow = false; | |
748 | config->metacopy = false; | |
749 | } | |
750 | ||
d5791044 | 751 | return 0; |
e9be9d5e MS |
752 | } |
753 | ||
754 | #define OVL_WORKDIR_NAME "work" | |
02bcd157 | 755 | #define OVL_INDEXDIR_NAME "index" |
e9be9d5e | 756 | |
ad204488 | 757 | static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, |
6b8aa129 | 758 | const char *name, bool persist) |
e9be9d5e | 759 | { |
ad204488 | 760 | struct inode *dir = ofs->workbasedir->d_inode; |
08f4c7c8 | 761 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
e9be9d5e MS |
762 | struct dentry *work; |
763 | int err; | |
764 | bool retried = false; | |
765 | ||
5955102c | 766 | inode_lock_nested(dir, I_MUTEX_PARENT); |
e9be9d5e | 767 | retry: |
22f289ce | 768 | work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name)); |
e9be9d5e MS |
769 | |
770 | if (!IS_ERR(work)) { | |
c11b9fdd MS |
771 | struct iattr attr = { |
772 | .ia_valid = ATTR_MODE, | |
32a3d848 | 773 | .ia_mode = S_IFDIR | 0, |
c11b9fdd | 774 | }; |
e9be9d5e MS |
775 | |
776 | if (work->d_inode) { | |
777 | err = -EEXIST; | |
778 | if (retried) | |
779 | goto out_dput; | |
780 | ||
6b8aa129 AG |
781 | if (persist) |
782 | goto out_unlock; | |
783 | ||
e9be9d5e | 784 | retried = true; |
576bb263 | 785 | err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0); |
e9be9d5e | 786 | dput(work); |
235ce9ed AG |
787 | if (err == -EINVAL) { |
788 | work = ERR_PTR(err); | |
789 | goto out_unlock; | |
790 | } | |
e9be9d5e MS |
791 | goto retry; |
792 | } | |
793 | ||
576bb263 | 794 | err = ovl_mkdir_real(ofs, dir, &work, attr.ia_mode); |
1f5573cf MS |
795 | if (err) |
796 | goto out_dput; | |
797 | ||
798 | /* Weird filesystem returning with hashed negative (kernfs)? */ | |
799 | err = -EINVAL; | |
800 | if (d_really_is_negative(work)) | |
801 | goto out_dput; | |
c11b9fdd | 802 | |
cb348edb MS |
803 | /* |
804 | * Try to remove POSIX ACL xattrs from workdir. We are good if: | |
805 | * | |
806 | * a) success (there was a POSIX ACL xattr and was removed) | |
807 | * b) -ENODATA (there was no POSIX ACL xattr) | |
808 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) | |
809 | * | |
810 | * There are various other error values that could effectively | |
811 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned | |
812 | * if the xattr name is too long), but the set of filesystems | |
813 | * allowed as upper are limited to "normal" ones, where checking | |
814 | * for the above two errors is sufficient. | |
815 | */ | |
c914c0e2 AG |
816 | err = ovl_do_removexattr(ofs, work, |
817 | XATTR_NAME_POSIX_ACL_DEFAULT); | |
e1ff3dd1 | 818 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
819 | goto out_dput; |
820 | ||
c914c0e2 AG |
821 | err = ovl_do_removexattr(ofs, work, |
822 | XATTR_NAME_POSIX_ACL_ACCESS); | |
e1ff3dd1 | 823 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
824 | goto out_dput; |
825 | ||
826 | /* Clear any inherited mode bits */ | |
827 | inode_lock(work->d_inode); | |
a15506ea | 828 | err = ovl_do_notify_change(ofs, work, &attr); |
c11b9fdd MS |
829 | inode_unlock(work->d_inode); |
830 | if (err) | |
831 | goto out_dput; | |
6b8aa129 AG |
832 | } else { |
833 | err = PTR_ERR(work); | |
834 | goto out_err; | |
e9be9d5e MS |
835 | } |
836 | out_unlock: | |
2068cf7d | 837 | inode_unlock(dir); |
e9be9d5e MS |
838 | return work; |
839 | ||
840 | out_dput: | |
841 | dput(work); | |
6b8aa129 | 842 | out_err: |
1bd0a3ae | 843 | pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n", |
ad204488 | 844 | ofs->config.workdir, name, -err); |
6b8aa129 | 845 | work = NULL; |
e9be9d5e MS |
846 | goto out_unlock; |
847 | } | |
848 | ||
91c77947 MS |
849 | static void ovl_unescape(char *s) |
850 | { | |
851 | char *d = s; | |
852 | ||
853 | for (;; s++, d++) { | |
854 | if (*s == '\\') | |
855 | s++; | |
856 | *d = *s; | |
857 | if (!*s) | |
858 | break; | |
859 | } | |
860 | } | |
861 | ||
ab508822 MS |
862 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
863 | { | |
a78d9f0d | 864 | int err = -EINVAL; |
ab508822 | 865 | |
a78d9f0d | 866 | if (!*name) { |
1bd0a3ae | 867 | pr_err("empty lowerdir\n"); |
a78d9f0d MS |
868 | goto out; |
869 | } | |
ab508822 MS |
870 | err = kern_path(name, LOOKUP_FOLLOW, path); |
871 | if (err) { | |
1bd0a3ae | 872 | pr_err("failed to resolve '%s': %i\n", name, err); |
ab508822 MS |
873 | goto out; |
874 | } | |
875 | err = -EINVAL; | |
7c03b5d4 | 876 | if (ovl_dentry_weird(path->dentry)) { |
1bd0a3ae | 877 | pr_err("filesystem on '%s' not supported\n", name); |
ab508822 MS |
878 | goto out_put; |
879 | } | |
2b8c30e9 | 880 | if (!d_is_dir(path->dentry)) { |
1bd0a3ae | 881 | pr_err("'%s' not a directory\n", name); |
ab508822 MS |
882 | goto out_put; |
883 | } | |
884 | return 0; | |
885 | ||
886 | out_put: | |
8aafcb59 | 887 | path_put_init(path); |
ab508822 MS |
888 | out: |
889 | return err; | |
890 | } | |
891 | ||
892 | static int ovl_mount_dir(const char *name, struct path *path) | |
893 | { | |
894 | int err = -ENOMEM; | |
895 | char *tmp = kstrdup(name, GFP_KERNEL); | |
896 | ||
897 | if (tmp) { | |
898 | ovl_unescape(tmp); | |
899 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 | 900 | |
bccece1e | 901 | if (!err && path->dentry->d_flags & DCACHE_OP_REAL) { |
7925dad8 MS |
902 | pr_err("filesystem on '%s' not supported as upperdir\n", |
903 | tmp); | |
904 | path_put_init(path); | |
905 | err = -EINVAL; | |
906 | } | |
ab508822 MS |
907 | kfree(tmp); |
908 | } | |
909 | return err; | |
910 | } | |
911 | ||
2d343087 | 912 | static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs, |
6b2d5fe4 | 913 | const char *name) |
ab508822 | 914 | { |
ab508822 | 915 | struct kstatfs statfs; |
6b2d5fe4 MS |
916 | int err = vfs_statfs(path, &statfs); |
917 | ||
918 | if (err) | |
1bd0a3ae | 919 | pr_err("statfs failed on '%s'\n", name); |
6b2d5fe4 MS |
920 | else |
921 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); | |
922 | ||
923 | return err; | |
924 | } | |
925 | ||
926 | static int ovl_lower_dir(const char *name, struct path *path, | |
f4288844 | 927 | struct ovl_fs *ofs, int *stack_depth) |
6b2d5fe4 | 928 | { |
e487d889 | 929 | int fh_type; |
6b2d5fe4 | 930 | int err; |
ab508822 | 931 | |
a78d9f0d | 932 | err = ovl_mount_dir_noesc(name, path); |
ab508822 | 933 | if (err) |
b8e42a65 | 934 | return err; |
ab508822 | 935 | |
6b2d5fe4 MS |
936 | err = ovl_check_namelen(path, ofs, name); |
937 | if (err) | |
b8e42a65 | 938 | return err; |
6b2d5fe4 | 939 | |
ab508822 MS |
940 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
941 | ||
02bcd157 | 942 | /* |
f168f109 AG |
943 | * The inodes index feature and NFS export need to encode and decode |
944 | * file handles, so they require that all layers support them. | |
02bcd157 | 945 | */ |
e487d889 | 946 | fh_type = ovl_can_decode_fh(path->dentry->d_sb); |
f168f109 | 947 | if ((ofs->config.nfs_export || |
e487d889 | 948 | (ofs->config.index && ofs->config.upperdir)) && !fh_type) { |
02bcd157 | 949 | ofs->config.index = false; |
f168f109 | 950 | ofs->config.nfs_export = false; |
1bd0a3ae | 951 | pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", |
f168f109 | 952 | name); |
02bcd157 | 953 | } |
b0e0f697 AG |
954 | /* |
955 | * Decoding origin file handle is required for persistent st_ino. | |
956 | * Without persistent st_ino, xino=auto falls back to xino=off. | |
957 | */ | |
958 | if (ofs->config.xino == OVL_XINO_AUTO && | |
959 | ofs->config.upperdir && !fh_type) { | |
960 | ofs->config.xino = OVL_XINO_OFF; | |
961 | pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n", | |
962 | name); | |
963 | } | |
02bcd157 | 964 | |
e487d889 AG |
965 | /* Check if lower fs has 32bit inode numbers */ |
966 | if (fh_type != FILEID_INO32_GEN) | |
0f831ec8 | 967 | ofs->xino_mode = -1; |
e487d889 | 968 | |
ab508822 | 969 | return 0; |
ab508822 MS |
970 | } |
971 | ||
e9be9d5e MS |
972 | /* Workdir should not be subdir of upperdir and vice versa */ |
973 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
974 | { | |
975 | bool ok = false; | |
976 | ||
977 | if (workdir != upperdir) { | |
978 | ok = (lock_rename(workdir, upperdir) == NULL); | |
979 | unlock_rename(workdir, upperdir); | |
980 | } | |
981 | return ok; | |
982 | } | |
983 | ||
a78d9f0d MS |
984 | static unsigned int ovl_split_lowerdirs(char *str) |
985 | { | |
986 | unsigned int ctr = 1; | |
987 | char *s, *d; | |
988 | ||
989 | for (s = d = str;; s++, d++) { | |
990 | if (*s == '\\') { | |
991 | s++; | |
992 | } else if (*s == ':') { | |
993 | *d = '\0'; | |
994 | ctr++; | |
995 | continue; | |
996 | } | |
997 | *d = *s; | |
998 | if (!*s) | |
999 | break; | |
1000 | } | |
1001 | return ctr; | |
1002 | } | |
1003 | ||
0eb45fc3 AG |
1004 | static int __maybe_unused |
1005 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, | |
1006 | struct dentry *dentry, struct inode *inode, | |
1007 | const char *name, void *buffer, size_t size) | |
1008 | { | |
1d88f183 | 1009 | return ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
0eb45fc3 AG |
1010 | } |
1011 | ||
0c97be22 AG |
1012 | static int __maybe_unused |
1013 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, | |
e65ce2a5 | 1014 | struct user_namespace *mnt_userns, |
0c97be22 AG |
1015 | struct dentry *dentry, struct inode *inode, |
1016 | const char *name, const void *value, | |
1017 | size_t size, int flags) | |
d837a49b MS |
1018 | { |
1019 | struct dentry *workdir = ovl_workdir(dentry); | |
09d8b586 | 1020 | struct inode *realinode = ovl_inode_real(inode); |
d837a49b MS |
1021 | struct posix_acl *acl = NULL; |
1022 | int err; | |
1023 | ||
1024 | /* Check that everything is OK before copy-up */ | |
1025 | if (value) { | |
7e1401ac CB |
1026 | /* The above comment can be understood in two ways: |
1027 | * | |
1028 | * 1. We just want to check whether the basic POSIX ACL format | |
1029 | * is ok. For example, if the header is correct and the size | |
1030 | * is sane. | |
1031 | * 2. We want to know whether the ACL_{GROUP,USER} entries can | |
1032 | * be mapped according to the underlying filesystem. | |
1033 | * | |
1034 | * Currently, we only check 1. If we wanted to check 2. we | |
1035 | * would need to pass the mnt_userns and the fs_userns of the | |
1036 | * underlying filesystem. But frankly, I think checking 1. is | |
1037 | * enough to start the copy-up. | |
1038 | */ | |
1039 | acl = vfs_set_acl_prepare(&init_user_ns, &init_user_ns, value, size); | |
d837a49b MS |
1040 | if (IS_ERR(acl)) |
1041 | return PTR_ERR(acl); | |
1042 | } | |
1043 | err = -EOPNOTSUPP; | |
1044 | if (!IS_POSIXACL(d_inode(workdir))) | |
1045 | goto out_acl_release; | |
1046 | if (!realinode->i_op->set_acl) | |
1047 | goto out_acl_release; | |
1048 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { | |
1049 | err = acl ? -EACCES : 0; | |
1050 | goto out_acl_release; | |
1051 | } | |
1052 | err = -EPERM; | |
21cb47be | 1053 | if (!inode_owner_or_capable(&init_user_ns, inode)) |
d837a49b MS |
1054 | goto out_acl_release; |
1055 | ||
1056 | posix_acl_release(acl); | |
1057 | ||
fd3220d3 MS |
1058 | /* |
1059 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
1060 | * be done with mounter's capabilities and so that won't do it for us). | |
1061 | */ | |
1062 | if (unlikely(inode->i_mode & S_ISGID) && | |
1063 | handler->flags == ACL_TYPE_ACCESS && | |
1064 | !in_group_p(inode->i_gid) && | |
0558c1bf | 1065 | !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID)) { |
fd3220d3 MS |
1066 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; |
1067 | ||
549c7297 | 1068 | err = ovl_setattr(&init_user_ns, dentry, &iattr); |
fd3220d3 MS |
1069 | if (err) |
1070 | return err; | |
1071 | } | |
1072 | ||
1d88f183 | 1073 | err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); |
ce31513a | 1074 | return err; |
d837a49b MS |
1075 | |
1076 | out_acl_release: | |
1077 | posix_acl_release(acl); | |
1078 | return err; | |
1079 | } | |
1080 | ||
0eb45fc3 AG |
1081 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
1082 | struct dentry *dentry, struct inode *inode, | |
1083 | const char *name, void *buffer, size_t size) | |
1084 | { | |
48fab5d7 | 1085 | return -EOPNOTSUPP; |
0eb45fc3 AG |
1086 | } |
1087 | ||
d837a49b | 1088 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
e65ce2a5 | 1089 | struct user_namespace *mnt_userns, |
d837a49b MS |
1090 | struct dentry *dentry, struct inode *inode, |
1091 | const char *name, const void *value, | |
1092 | size_t size, int flags) | |
1093 | { | |
48fab5d7 | 1094 | return -EOPNOTSUPP; |
d837a49b MS |
1095 | } |
1096 | ||
0eb45fc3 AG |
1097 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
1098 | struct dentry *dentry, struct inode *inode, | |
1099 | const char *name, void *buffer, size_t size) | |
1100 | { | |
1d88f183 | 1101 | return ovl_xattr_get(dentry, inode, name, buffer, size); |
0eb45fc3 AG |
1102 | } |
1103 | ||
0e585ccc | 1104 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
e65ce2a5 | 1105 | struct user_namespace *mnt_userns, |
0e585ccc AG |
1106 | struct dentry *dentry, struct inode *inode, |
1107 | const char *name, const void *value, | |
1108 | size_t size, int flags) | |
1109 | { | |
1d88f183 | 1110 | return ovl_xattr_set(dentry, inode, name, value, size, flags); |
0e585ccc AG |
1111 | } |
1112 | ||
0c97be22 AG |
1113 | static const struct xattr_handler __maybe_unused |
1114 | ovl_posix_acl_access_xattr_handler = { | |
d837a49b MS |
1115 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
1116 | .flags = ACL_TYPE_ACCESS, | |
0eb45fc3 | 1117 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
1118 | .set = ovl_posix_acl_xattr_set, |
1119 | }; | |
1120 | ||
0c97be22 AG |
1121 | static const struct xattr_handler __maybe_unused |
1122 | ovl_posix_acl_default_xattr_handler = { | |
d837a49b MS |
1123 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
1124 | .flags = ACL_TYPE_DEFAULT, | |
0eb45fc3 | 1125 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
1126 | .set = ovl_posix_acl_xattr_set, |
1127 | }; | |
1128 | ||
2d2f2d73 MS |
1129 | static const struct xattr_handler ovl_own_trusted_xattr_handler = { |
1130 | .prefix = OVL_XATTR_TRUSTED_PREFIX, | |
1131 | .get = ovl_own_xattr_get, | |
1132 | .set = ovl_own_xattr_set, | |
1133 | }; | |
1134 | ||
1135 | static const struct xattr_handler ovl_own_user_xattr_handler = { | |
1136 | .prefix = OVL_XATTR_USER_PREFIX, | |
0eb45fc3 | 1137 | .get = ovl_own_xattr_get, |
d837a49b MS |
1138 | .set = ovl_own_xattr_set, |
1139 | }; | |
1140 | ||
1141 | static const struct xattr_handler ovl_other_xattr_handler = { | |
1142 | .prefix = "", /* catch all */ | |
0eb45fc3 | 1143 | .get = ovl_other_xattr_get, |
d837a49b MS |
1144 | .set = ovl_other_xattr_set, |
1145 | }; | |
1146 | ||
2d2f2d73 MS |
1147 | static const struct xattr_handler *ovl_trusted_xattr_handlers[] = { |
1148 | #ifdef CONFIG_FS_POSIX_ACL | |
1149 | &ovl_posix_acl_access_xattr_handler, | |
1150 | &ovl_posix_acl_default_xattr_handler, | |
1151 | #endif | |
1152 | &ovl_own_trusted_xattr_handler, | |
1153 | &ovl_other_xattr_handler, | |
1154 | NULL | |
1155 | }; | |
1156 | ||
1157 | static const struct xattr_handler *ovl_user_xattr_handlers[] = { | |
0c97be22 | 1158 | #ifdef CONFIG_FS_POSIX_ACL |
d837a49b MS |
1159 | &ovl_posix_acl_access_xattr_handler, |
1160 | &ovl_posix_acl_default_xattr_handler, | |
0c97be22 | 1161 | #endif |
2d2f2d73 | 1162 | &ovl_own_user_xattr_handler, |
d837a49b MS |
1163 | &ovl_other_xattr_handler, |
1164 | NULL | |
1165 | }; | |
1166 | ||
146d62e5 AG |
1167 | static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, |
1168 | struct inode **ptrap, const char *name) | |
1169 | { | |
1170 | struct inode *trap; | |
1171 | int err; | |
1172 | ||
1173 | trap = ovl_get_trap_inode(sb, dir); | |
1dac6f5b AB |
1174 | err = PTR_ERR_OR_ZERO(trap); |
1175 | if (err) { | |
146d62e5 | 1176 | if (err == -ELOOP) |
1bd0a3ae | 1177 | pr_err("conflicting %s path\n", name); |
146d62e5 AG |
1178 | return err; |
1179 | } | |
1180 | ||
1181 | *ptrap = trap; | |
1182 | return 0; | |
1183 | } | |
1184 | ||
0be0bfd2 AG |
1185 | /* |
1186 | * Determine how we treat concurrent use of upperdir/workdir based on the | |
1187 | * index feature. This is papering over mount leaks of container runtimes, | |
1188 | * for example, an old overlay mount is leaked and now its upperdir is | |
1189 | * attempted to be used as a lower layer in a new overlay mount. | |
1190 | */ | |
1191 | static int ovl_report_in_use(struct ovl_fs *ofs, const char *name) | |
1192 | { | |
1193 | if (ofs->config.index) { | |
1bd0a3ae | 1194 | pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n", |
0be0bfd2 AG |
1195 | name); |
1196 | return -EBUSY; | |
1197 | } else { | |
1bd0a3ae | 1198 | pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n", |
0be0bfd2 AG |
1199 | name); |
1200 | return 0; | |
1201 | } | |
1202 | } | |
1203 | ||
146d62e5 | 1204 | static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, |
b8e42a65 | 1205 | struct ovl_layer *upper_layer, struct path *upperpath) |
6ee8acf0 | 1206 | { |
5064975e | 1207 | struct vfsmount *upper_mnt; |
6ee8acf0 MS |
1208 | int err; |
1209 | ||
ad204488 | 1210 | err = ovl_mount_dir(ofs->config.upperdir, upperpath); |
6ee8acf0 MS |
1211 | if (err) |
1212 | goto out; | |
1213 | ||
e21a6c57 AG |
1214 | /* Upperdir path should not be r/o */ |
1215 | if (__mnt_is_readonly(upperpath->mnt)) { | |
1bd0a3ae | 1216 | pr_err("upper fs is r/o, try multi-lower layers mount\n"); |
6ee8acf0 MS |
1217 | err = -EINVAL; |
1218 | goto out; | |
1219 | } | |
1220 | ||
ad204488 | 1221 | err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); |
6ee8acf0 MS |
1222 | if (err) |
1223 | goto out; | |
1224 | ||
b8e42a65 | 1225 | err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap, |
146d62e5 AG |
1226 | "upperdir"); |
1227 | if (err) | |
1228 | goto out; | |
1229 | ||
5064975e MS |
1230 | upper_mnt = clone_private_mount(upperpath); |
1231 | err = PTR_ERR(upper_mnt); | |
1232 | if (IS_ERR(upper_mnt)) { | |
1bd0a3ae | 1233 | pr_err("failed to clone upperpath\n"); |
5064975e MS |
1234 | goto out; |
1235 | } | |
1236 | ||
1237 | /* Don't inherit atime flags */ | |
1238 | upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
b8e42a65 MS |
1239 | upper_layer->mnt = upper_mnt; |
1240 | upper_layer->idx = 0; | |
1241 | upper_layer->fsid = 0; | |
8c25741a | 1242 | |
654255fa JX |
1243 | /* |
1244 | * Inherit SB_NOSEC flag from upperdir. | |
1245 | * | |
1246 | * This optimization changes behavior when a security related attribute | |
1247 | * (suid/sgid/security.*) is changed on an underlying layer. This is | |
1248 | * okay because we don't yet have guarantees in that case, but it will | |
1249 | * need careful treatment once we want to honour changes to underlying | |
1250 | * filesystems. | |
1251 | */ | |
1252 | if (upper_mnt->mnt_sb->s_flags & SB_NOSEC) | |
1253 | sb->s_flags |= SB_NOSEC; | |
1254 | ||
08f4c7c8 | 1255 | if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) { |
8c25741a | 1256 | ofs->upperdir_locked = true; |
8c25741a | 1257 | } else { |
0be0bfd2 AG |
1258 | err = ovl_report_in_use(ofs, "upperdir"); |
1259 | if (err) | |
1260 | goto out; | |
8c25741a MS |
1261 | } |
1262 | ||
6ee8acf0 MS |
1263 | err = 0; |
1264 | out: | |
1265 | return err; | |
1266 | } | |
1267 | ||
cad218ab AG |
1268 | /* |
1269 | * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and | |
1270 | * negative values if error is encountered. | |
1271 | */ | |
576bb263 | 1272 | static int ovl_check_rename_whiteout(struct ovl_fs *ofs) |
cad218ab | 1273 | { |
576bb263 | 1274 | struct dentry *workdir = ofs->workdir; |
cad218ab AG |
1275 | struct inode *dir = d_inode(workdir); |
1276 | struct dentry *temp; | |
1277 | struct dentry *dest; | |
1278 | struct dentry *whiteout; | |
1279 | struct name_snapshot name; | |
1280 | int err; | |
1281 | ||
1282 | inode_lock_nested(dir, I_MUTEX_PARENT); | |
1283 | ||
576bb263 | 1284 | temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0)); |
cad218ab AG |
1285 | err = PTR_ERR(temp); |
1286 | if (IS_ERR(temp)) | |
1287 | goto out_unlock; | |
1288 | ||
576bb263 | 1289 | dest = ovl_lookup_temp(ofs, workdir); |
cad218ab AG |
1290 | err = PTR_ERR(dest); |
1291 | if (IS_ERR(dest)) { | |
1292 | dput(temp); | |
1293 | goto out_unlock; | |
1294 | } | |
1295 | ||
1296 | /* Name is inline and stable - using snapshot as a copy helper */ | |
1297 | take_dentry_name_snapshot(&name, temp); | |
576bb263 | 1298 | err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT); |
cad218ab AG |
1299 | if (err) { |
1300 | if (err == -EINVAL) | |
1301 | err = 0; | |
1302 | goto cleanup_temp; | |
1303 | } | |
1304 | ||
22f289ce | 1305 | whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len); |
cad218ab AG |
1306 | err = PTR_ERR(whiteout); |
1307 | if (IS_ERR(whiteout)) | |
1308 | goto cleanup_temp; | |
1309 | ||
1310 | err = ovl_is_whiteout(whiteout); | |
1311 | ||
1312 | /* Best effort cleanup of whiteout and temp file */ | |
1313 | if (err) | |
576bb263 | 1314 | ovl_cleanup(ofs, dir, whiteout); |
cad218ab AG |
1315 | dput(whiteout); |
1316 | ||
1317 | cleanup_temp: | |
576bb263 | 1318 | ovl_cleanup(ofs, dir, temp); |
cad218ab AG |
1319 | release_dentry_name_snapshot(&name); |
1320 | dput(temp); | |
1321 | dput(dest); | |
1322 | ||
1323 | out_unlock: | |
1324 | inode_unlock(dir); | |
1325 | ||
1326 | return err; | |
1327 | } | |
1328 | ||
576bb263 CB |
1329 | static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs, |
1330 | struct dentry *parent, | |
c86243b0 VG |
1331 | const char *name, umode_t mode) |
1332 | { | |
1333 | size_t len = strlen(name); | |
1334 | struct dentry *child; | |
1335 | ||
1336 | inode_lock_nested(parent->d_inode, I_MUTEX_PARENT); | |
22f289ce | 1337 | child = ovl_lookup_upper(ofs, name, parent, len); |
c86243b0 | 1338 | if (!IS_ERR(child) && !child->d_inode) |
576bb263 | 1339 | child = ovl_create_real(ofs, parent->d_inode, child, |
c86243b0 VG |
1340 | OVL_CATTR(mode)); |
1341 | inode_unlock(parent->d_inode); | |
1342 | dput(parent); | |
1343 | ||
1344 | return child; | |
1345 | } | |
1346 | ||
1347 | /* | |
1348 | * Creates $workdir/work/incompat/volatile/dirty file if it is not already | |
1349 | * present. | |
1350 | */ | |
1351 | static int ovl_create_volatile_dirty(struct ovl_fs *ofs) | |
1352 | { | |
1353 | unsigned int ctr; | |
1354 | struct dentry *d = dget(ofs->workbasedir); | |
1355 | static const char *const volatile_path[] = { | |
1356 | OVL_WORKDIR_NAME, "incompat", "volatile", "dirty" | |
1357 | }; | |
1358 | const char *const *name = volatile_path; | |
1359 | ||
1360 | for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) { | |
576bb263 | 1361 | d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG); |
c86243b0 VG |
1362 | if (IS_ERR(d)) |
1363 | return PTR_ERR(d); | |
1364 | } | |
1365 | dput(d); | |
1366 | return 0; | |
1367 | } | |
1368 | ||
146d62e5 | 1369 | static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, |
2d343087 | 1370 | const struct path *workpath) |
8ed61dc3 | 1371 | { |
08f4c7c8 | 1372 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
2b1a7746 MS |
1373 | struct dentry *workdir; |
1374 | struct file *tmpfile; | |
d80172c2 AG |
1375 | bool rename_whiteout; |
1376 | bool d_type; | |
e487d889 | 1377 | int fh_type; |
8ed61dc3 MS |
1378 | int err; |
1379 | ||
2ba9d57e AG |
1380 | err = mnt_want_write(mnt); |
1381 | if (err) | |
1382 | return err; | |
1383 | ||
235ce9ed AG |
1384 | workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); |
1385 | err = PTR_ERR(workdir); | |
1386 | if (IS_ERR_OR_NULL(workdir)) | |
2ba9d57e | 1387 | goto out; |
8ed61dc3 | 1388 | |
235ce9ed AG |
1389 | ofs->workdir = workdir; |
1390 | ||
146d62e5 AG |
1391 | err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); |
1392 | if (err) | |
1393 | goto out; | |
1394 | ||
8ed61dc3 MS |
1395 | /* |
1396 | * Upper should support d_type, else whiteouts are visible. Given | |
1397 | * workdir and upper are on same fs, we can do iterate_dir() on | |
1398 | * workdir. This check requires successful creation of workdir in | |
1399 | * previous step. | |
1400 | */ | |
1401 | err = ovl_check_d_type_supported(workpath); | |
1402 | if (err < 0) | |
2ba9d57e | 1403 | goto out; |
8ed61dc3 | 1404 | |
d80172c2 AG |
1405 | d_type = err; |
1406 | if (!d_type) | |
1bd0a3ae | 1407 | pr_warn("upper fs needs to support d_type.\n"); |
8ed61dc3 MS |
1408 | |
1409 | /* Check if upper/work fs supports O_TMPFILE */ | |
2b1a7746 MS |
1410 | tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0); |
1411 | ofs->tmpfile = !IS_ERR(tmpfile); | |
ad204488 | 1412 | if (ofs->tmpfile) |
2b1a7746 | 1413 | fput(tmpfile); |
8ed61dc3 | 1414 | else |
1bd0a3ae | 1415 | pr_warn("upper fs does not support tmpfile.\n"); |
8ed61dc3 | 1416 | |
cad218ab AG |
1417 | |
1418 | /* Check if upper/work fs supports RENAME_WHITEOUT */ | |
576bb263 | 1419 | err = ovl_check_rename_whiteout(ofs); |
cad218ab AG |
1420 | if (err < 0) |
1421 | goto out; | |
1422 | ||
d80172c2 AG |
1423 | rename_whiteout = err; |
1424 | if (!rename_whiteout) | |
cad218ab AG |
1425 | pr_warn("upper fs does not support RENAME_WHITEOUT.\n"); |
1426 | ||
8ed61dc3 | 1427 | /* |
2d2f2d73 | 1428 | * Check if upper/work fs supports (trusted|user).overlay.* xattr |
8ed61dc3 | 1429 | */ |
c914c0e2 | 1430 | err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1); |
8ed61dc3 | 1431 | if (err) { |
b10b85fe | 1432 | pr_warn("failed to set xattr on upper\n"); |
ad204488 | 1433 | ofs->noxattr = true; |
b0e0f697 AG |
1434 | if (ofs->config.index || ofs->config.metacopy) { |
1435 | ofs->config.index = false; | |
1436 | ofs->config.metacopy = false; | |
b10b85fe | 1437 | pr_warn("...falling back to index=off,metacopy=off.\n"); |
b0e0f697 AG |
1438 | } |
1439 | /* | |
1440 | * xattr support is required for persistent st_ino. | |
1441 | * Without persistent st_ino, xino=auto falls back to xino=off. | |
1442 | */ | |
1443 | if (ofs->config.xino == OVL_XINO_AUTO) { | |
1444 | ofs->config.xino = OVL_XINO_OFF; | |
b10b85fe | 1445 | pr_warn("...falling back to xino=off.\n"); |
b0e0f697 | 1446 | } |
b10b85fe MS |
1447 | if (err == -EPERM && !ofs->config.userxattr) |
1448 | pr_info("try mounting with 'userxattr' option\n"); | |
2ba9d57e | 1449 | err = 0; |
8ed61dc3 | 1450 | } else { |
c914c0e2 | 1451 | ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE); |
8ed61dc3 MS |
1452 | } |
1453 | ||
d80172c2 AG |
1454 | /* |
1455 | * We allowed sub-optimal upper fs configuration and don't want to break | |
1456 | * users over kernel upgrade, but we never allowed remote upper fs, so | |
1457 | * we can enforce strict requirements for remote upper fs. | |
1458 | */ | |
1459 | if (ovl_dentry_remote(ofs->workdir) && | |
1460 | (!d_type || !rename_whiteout || ofs->noxattr)) { | |
1461 | pr_err("upper fs missing required features.\n"); | |
1462 | err = -EINVAL; | |
1463 | goto out; | |
1464 | } | |
1465 | ||
c86243b0 VG |
1466 | /* |
1467 | * For volatile mount, create a incompat/volatile/dirty file to keep | |
1468 | * track of it. | |
1469 | */ | |
1470 | if (ofs->config.ovl_volatile) { | |
1471 | err = ovl_create_volatile_dirty(ofs); | |
1472 | if (err < 0) { | |
1473 | pr_err("Failed to create volatile/dirty file.\n"); | |
1474 | goto out; | |
1475 | } | |
1476 | } | |
1477 | ||
8ed61dc3 | 1478 | /* Check if upper/work fs supports file handles */ |
e487d889 AG |
1479 | fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); |
1480 | if (ofs->config.index && !fh_type) { | |
ad204488 | 1481 | ofs->config.index = false; |
1bd0a3ae | 1482 | pr_warn("upper fs does not support file handles, falling back to index=off.\n"); |
8ed61dc3 MS |
1483 | } |
1484 | ||
e487d889 AG |
1485 | /* Check if upper fs has 32bit inode numbers */ |
1486 | if (fh_type != FILEID_INO32_GEN) | |
0f831ec8 | 1487 | ofs->xino_mode = -1; |
e487d889 | 1488 | |
f168f109 AG |
1489 | /* NFS export of r/w mount depends on index */ |
1490 | if (ofs->config.nfs_export && !ofs->config.index) { | |
1bd0a3ae | 1491 | pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n"); |
f168f109 AG |
1492 | ofs->config.nfs_export = false; |
1493 | } | |
2ba9d57e AG |
1494 | out: |
1495 | mnt_drop_write(mnt); | |
1496 | return err; | |
8ed61dc3 MS |
1497 | } |
1498 | ||
146d62e5 | 1499 | static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, |
2d343087 | 1500 | const struct path *upperpath) |
520d7c86 MS |
1501 | { |
1502 | int err; | |
bca44b52 | 1503 | struct path workpath = { }; |
520d7c86 | 1504 | |
ad204488 | 1505 | err = ovl_mount_dir(ofs->config.workdir, &workpath); |
520d7c86 MS |
1506 | if (err) |
1507 | goto out; | |
1508 | ||
1509 | err = -EINVAL; | |
bca44b52 | 1510 | if (upperpath->mnt != workpath.mnt) { |
1bd0a3ae | 1511 | pr_err("workdir and upperdir must reside under the same mount\n"); |
520d7c86 MS |
1512 | goto out; |
1513 | } | |
bca44b52 | 1514 | if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { |
1bd0a3ae | 1515 | pr_err("workdir and upperdir must be separate subtrees\n"); |
520d7c86 MS |
1516 | goto out; |
1517 | } | |
1518 | ||
8c25741a MS |
1519 | ofs->workbasedir = dget(workpath.dentry); |
1520 | ||
8c25741a | 1521 | if (ovl_inuse_trylock(ofs->workbasedir)) { |
ad204488 | 1522 | ofs->workdir_locked = true; |
520d7c86 | 1523 | } else { |
0be0bfd2 AG |
1524 | err = ovl_report_in_use(ofs, "workdir"); |
1525 | if (err) | |
1526 | goto out; | |
520d7c86 MS |
1527 | } |
1528 | ||
0be0bfd2 AG |
1529 | err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap, |
1530 | "workdir"); | |
1531 | if (err) | |
1532 | goto out; | |
1533 | ||
146d62e5 | 1534 | err = ovl_make_workdir(sb, ofs, &workpath); |
bca44b52 | 1535 | |
520d7c86 | 1536 | out: |
bca44b52 MS |
1537 | path_put(&workpath); |
1538 | ||
520d7c86 MS |
1539 | return err; |
1540 | } | |
1541 | ||
146d62e5 | 1542 | static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, |
2d343087 | 1543 | struct ovl_entry *oe, const struct path *upperpath) |
f7e3a7d9 | 1544 | { |
08f4c7c8 | 1545 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
235ce9ed | 1546 | struct dentry *indexdir; |
f7e3a7d9 MS |
1547 | int err; |
1548 | ||
2ba9d57e AG |
1549 | err = mnt_want_write(mnt); |
1550 | if (err) | |
1551 | return err; | |
1552 | ||
f7e3a7d9 | 1553 | /* Verify lower root is upper root origin */ |
610afc0b MS |
1554 | err = ovl_verify_origin(ofs, upperpath->dentry, |
1555 | oe->lowerstack[0].dentry, true); | |
f7e3a7d9 | 1556 | if (err) { |
1bd0a3ae | 1557 | pr_err("failed to verify upper root origin\n"); |
f7e3a7d9 MS |
1558 | goto out; |
1559 | } | |
1560 | ||
470c1563 AG |
1561 | /* index dir will act also as workdir */ |
1562 | iput(ofs->workdir_trap); | |
1563 | ofs->workdir_trap = NULL; | |
1564 | dput(ofs->workdir); | |
1565 | ofs->workdir = NULL; | |
235ce9ed AG |
1566 | indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); |
1567 | if (IS_ERR(indexdir)) { | |
1568 | err = PTR_ERR(indexdir); | |
1569 | } else if (indexdir) { | |
1570 | ofs->indexdir = indexdir; | |
1571 | ofs->workdir = dget(indexdir); | |
20396365 | 1572 | |
146d62e5 AG |
1573 | err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap, |
1574 | "indexdir"); | |
1575 | if (err) | |
1576 | goto out; | |
1577 | ||
ad1d615c AG |
1578 | /* |
1579 | * Verify upper root is exclusively associated with index dir. | |
2d2f2d73 | 1580 | * Older kernels stored upper fh in ".overlay.origin" |
ad1d615c AG |
1581 | * xattr. If that xattr exists, verify that it is a match to |
1582 | * upper dir file handle. In any case, verify or set xattr | |
2d2f2d73 | 1583 | * ".overlay.upper" to indicate that index may have |
ad1d615c AG |
1584 | * directory entries. |
1585 | */ | |
610afc0b MS |
1586 | if (ovl_check_origin_xattr(ofs, ofs->indexdir)) { |
1587 | err = ovl_verify_set_fh(ofs, ofs->indexdir, | |
1588 | OVL_XATTR_ORIGIN, | |
ad1d615c AG |
1589 | upperpath->dentry, true, false); |
1590 | if (err) | |
1bd0a3ae | 1591 | pr_err("failed to verify index dir 'origin' xattr\n"); |
ad1d615c | 1592 | } |
610afc0b MS |
1593 | err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry, |
1594 | true); | |
f7e3a7d9 | 1595 | if (err) |
1bd0a3ae | 1596 | pr_err("failed to verify index dir 'upper' xattr\n"); |
f7e3a7d9 MS |
1597 | |
1598 | /* Cleanup bad/stale/orphan index entries */ | |
1599 | if (!err) | |
1eff1a1d | 1600 | err = ovl_indexdir_cleanup(ofs); |
f7e3a7d9 | 1601 | } |
ad204488 | 1602 | if (err || !ofs->indexdir) |
1bd0a3ae | 1603 | pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); |
f7e3a7d9 MS |
1604 | |
1605 | out: | |
2ba9d57e | 1606 | mnt_drop_write(mnt); |
f7e3a7d9 MS |
1607 | return err; |
1608 | } | |
1609 | ||
9df085f3 AG |
1610 | static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid) |
1611 | { | |
1612 | unsigned int i; | |
1613 | ||
08f4c7c8 | 1614 | if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs)) |
9df085f3 AG |
1615 | return true; |
1616 | ||
a888db31 AG |
1617 | /* |
1618 | * We allow using single lower with null uuid for index and nfs_export | |
1619 | * for example to support those features with single lower squashfs. | |
1620 | * To avoid regressions in setups of overlay with re-formatted lower | |
1621 | * squashfs, do not allow decoding origin with lower null uuid unless | |
1622 | * user opted-in to one of the new features that require following the | |
1623 | * lower inode of non-dir upper. | |
1624 | */ | |
ca45275c | 1625 | if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid)) |
a888db31 AG |
1626 | return false; |
1627 | ||
1b81dddd | 1628 | for (i = 0; i < ofs->numfs; i++) { |
9df085f3 AG |
1629 | /* |
1630 | * We use uuid to associate an overlay lower file handle with a | |
1631 | * lower layer, so we can accept lower fs with null uuid as long | |
1632 | * as all lower layers with null uuid are on the same fs. | |
7e63c87f AG |
1633 | * if we detect multiple lower fs with the same uuid, we |
1634 | * disable lower file handle decoding on all of them. | |
9df085f3 | 1635 | */ |
1b81dddd AG |
1636 | if (ofs->fs[i].is_lower && |
1637 | uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) { | |
07f1e596 | 1638 | ofs->fs[i].bad_uuid = true; |
9df085f3 | 1639 | return false; |
7e63c87f | 1640 | } |
9df085f3 AG |
1641 | } |
1642 | return true; | |
1643 | } | |
1644 | ||
5148626b | 1645 | /* Get a unique fsid for the layer */ |
9df085f3 | 1646 | static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path) |
5148626b | 1647 | { |
9df085f3 | 1648 | struct super_block *sb = path->mnt->mnt_sb; |
5148626b AG |
1649 | unsigned int i; |
1650 | dev_t dev; | |
1651 | int err; | |
7e63c87f | 1652 | bool bad_uuid = false; |
b0e0f697 | 1653 | bool warn = false; |
5148626b | 1654 | |
07f1e596 AG |
1655 | for (i = 0; i < ofs->numfs; i++) { |
1656 | if (ofs->fs[i].sb == sb) | |
1657 | return i; | |
5148626b AG |
1658 | } |
1659 | ||
9df085f3 | 1660 | if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) { |
7e63c87f | 1661 | bad_uuid = true; |
b0e0f697 AG |
1662 | if (ofs->config.xino == OVL_XINO_AUTO) { |
1663 | ofs->config.xino = OVL_XINO_OFF; | |
1664 | warn = true; | |
1665 | } | |
7e63c87f AG |
1666 | if (ofs->config.index || ofs->config.nfs_export) { |
1667 | ofs->config.index = false; | |
1668 | ofs->config.nfs_export = false; | |
b0e0f697 AG |
1669 | warn = true; |
1670 | } | |
1671 | if (warn) { | |
1672 | pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n", | |
7e63c87f AG |
1673 | uuid_is_null(&sb->s_uuid) ? "null" : |
1674 | "conflicting", | |
b0e0f697 | 1675 | path->dentry, ovl_xino_str[ofs->config.xino]); |
7e63c87f | 1676 | } |
9df085f3 AG |
1677 | } |
1678 | ||
5148626b AG |
1679 | err = get_anon_bdev(&dev); |
1680 | if (err) { | |
1bd0a3ae | 1681 | pr_err("failed to get anonymous bdev for lowerpath\n"); |
5148626b AG |
1682 | return err; |
1683 | } | |
1684 | ||
07f1e596 AG |
1685 | ofs->fs[ofs->numfs].sb = sb; |
1686 | ofs->fs[ofs->numfs].pseudo_dev = dev; | |
1687 | ofs->fs[ofs->numfs].bad_uuid = bad_uuid; | |
5148626b | 1688 | |
07f1e596 | 1689 | return ofs->numfs++; |
5148626b AG |
1690 | } |
1691 | ||
94375f9d | 1692 | static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs, |
b8e42a65 MS |
1693 | struct path *stack, unsigned int numlower, |
1694 | struct ovl_layer *layers) | |
520d7c86 MS |
1695 | { |
1696 | int err; | |
1697 | unsigned int i; | |
1698 | ||
1699 | err = -ENOMEM; | |
07f1e596 AG |
1700 | ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL); |
1701 | if (ofs->fs == NULL) | |
5148626b AG |
1702 | goto out; |
1703 | ||
07f1e596 AG |
1704 | /* idx/fsid 0 are reserved for upper fs even with lower only overlay */ |
1705 | ofs->numfs++; | |
1706 | ||
07f1e596 | 1707 | /* |
b7bf9908 AG |
1708 | * All lower layers that share the same fs as upper layer, use the same |
1709 | * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower | |
1710 | * only overlay to simplify ovl_fs_free(). | |
1b81dddd | 1711 | * is_lower will be set if upper fs is shared with a lower layer. |
07f1e596 | 1712 | */ |
b7bf9908 AG |
1713 | err = get_anon_bdev(&ofs->fs[0].pseudo_dev); |
1714 | if (err) { | |
1715 | pr_err("failed to get anonymous bdev for upper fs\n"); | |
1716 | goto out; | |
1717 | } | |
1718 | ||
08f4c7c8 MS |
1719 | if (ovl_upper_mnt(ofs)) { |
1720 | ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb; | |
1b81dddd | 1721 | ofs->fs[0].is_lower = false; |
07f1e596 AG |
1722 | } |
1723 | ||
520d7c86 MS |
1724 | for (i = 0; i < numlower; i++) { |
1725 | struct vfsmount *mnt; | |
146d62e5 | 1726 | struct inode *trap; |
5148626b | 1727 | int fsid; |
520d7c86 | 1728 | |
9df085f3 | 1729 | err = fsid = ovl_get_fsid(ofs, &stack[i]); |
5148626b | 1730 | if (err < 0) |
520d7c86 | 1731 | goto out; |
520d7c86 | 1732 | |
24f14009 | 1733 | /* |
1734 | * Check if lower root conflicts with this overlay layers before | |
1735 | * checking if it is in-use as upperdir/workdir of "another" | |
1736 | * mount, because we do not bother to check in ovl_is_inuse() if | |
1737 | * the upperdir/workdir is in fact in-use by our | |
1738 | * upperdir/workdir. | |
1739 | */ | |
146d62e5 AG |
1740 | err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir"); |
1741 | if (err) | |
1742 | goto out; | |
1743 | ||
0be0bfd2 AG |
1744 | if (ovl_is_inuse(stack[i].dentry)) { |
1745 | err = ovl_report_in_use(ofs, "lowerdir"); | |
24f14009 | 1746 | if (err) { |
1747 | iput(trap); | |
0be0bfd2 | 1748 | goto out; |
24f14009 | 1749 | } |
0be0bfd2 AG |
1750 | } |
1751 | ||
520d7c86 MS |
1752 | mnt = clone_private_mount(&stack[i]); |
1753 | err = PTR_ERR(mnt); | |
1754 | if (IS_ERR(mnt)) { | |
1bd0a3ae | 1755 | pr_err("failed to clone lowerpath\n"); |
146d62e5 | 1756 | iput(trap); |
520d7c86 MS |
1757 | goto out; |
1758 | } | |
5148626b | 1759 | |
520d7c86 MS |
1760 | /* |
1761 | * Make lower layers R/O. That way fchmod/fchown on lower file | |
1762 | * will fail instead of modifying lower fs. | |
1763 | */ | |
1764 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; | |
1765 | ||
13464165 MS |
1766 | layers[ofs->numlayer].trap = trap; |
1767 | layers[ofs->numlayer].mnt = mnt; | |
1768 | layers[ofs->numlayer].idx = ofs->numlayer; | |
1769 | layers[ofs->numlayer].fsid = fsid; | |
1770 | layers[ofs->numlayer].fs = &ofs->fs[fsid]; | |
94375f9d | 1771 | ofs->numlayer++; |
1b81dddd | 1772 | ofs->fs[fsid].is_lower = true; |
520d7c86 | 1773 | } |
e487d889 | 1774 | |
795939a9 AG |
1775 | /* |
1776 | * When all layers on same fs, overlay can use real inode numbers. | |
926e94d7 AG |
1777 | * With mount option "xino=<on|auto>", mounter declares that there are |
1778 | * enough free high bits in underlying fs to hold the unique fsid. | |
795939a9 AG |
1779 | * If overlayfs does encounter underlying inodes using the high xino |
1780 | * bits reserved for fsid, it emits a warning and uses the original | |
dfe51d47 AG |
1781 | * inode number or a non persistent inode number allocated from a |
1782 | * dedicated range. | |
795939a9 | 1783 | */ |
08f4c7c8 | 1784 | if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) { |
0f831ec8 AG |
1785 | if (ofs->config.xino == OVL_XINO_ON) |
1786 | pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n"); | |
1787 | ofs->xino_mode = 0; | |
53afcd31 AG |
1788 | } else if (ofs->config.xino == OVL_XINO_OFF) { |
1789 | ofs->xino_mode = -1; | |
926e94d7 | 1790 | } else if (ofs->xino_mode < 0) { |
795939a9 | 1791 | /* |
07f1e596 | 1792 | * This is a roundup of number of bits needed for encoding |
dfe51d47 AG |
1793 | * fsid, where fsid 0 is reserved for upper fs (even with |
1794 | * lower only overlay) +1 extra bit is reserved for the non | |
1795 | * persistent inode number range that is used for resolving | |
1796 | * xino lower bits overflow. | |
795939a9 | 1797 | */ |
dfe51d47 AG |
1798 | BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30); |
1799 | ofs->xino_mode = ilog2(ofs->numfs - 1) + 2; | |
795939a9 AG |
1800 | } |
1801 | ||
0f831ec8 | 1802 | if (ofs->xino_mode > 0) { |
1bd0a3ae | 1803 | pr_info("\"xino\" feature enabled using %d upper inode bits.\n", |
0f831ec8 | 1804 | ofs->xino_mode); |
795939a9 | 1805 | } |
e487d889 | 1806 | |
520d7c86 MS |
1807 | err = 0; |
1808 | out: | |
1809 | return err; | |
1810 | } | |
1811 | ||
4155c10a | 1812 | static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, |
b8e42a65 MS |
1813 | const char *lower, unsigned int numlower, |
1814 | struct ovl_fs *ofs, struct ovl_layer *layers) | |
53dbb0b4 MS |
1815 | { |
1816 | int err; | |
4155c10a | 1817 | struct path *stack = NULL; |
b8e42a65 | 1818 | unsigned int i; |
4155c10a | 1819 | struct ovl_entry *oe; |
53dbb0b4 | 1820 | |
b8e42a65 | 1821 | if (!ofs->config.upperdir && numlower == 1) { |
1bd0a3ae | 1822 | pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n"); |
b8e42a65 | 1823 | return ERR_PTR(-EINVAL); |
53dbb0b4 MS |
1824 | } |
1825 | ||
b8e42a65 | 1826 | stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL); |
53dbb0b4 | 1827 | if (!stack) |
b8e42a65 | 1828 | return ERR_PTR(-ENOMEM); |
53dbb0b4 MS |
1829 | |
1830 | err = -EINVAL; | |
b8e42a65 MS |
1831 | for (i = 0; i < numlower; i++) { |
1832 | err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth); | |
53dbb0b4 | 1833 | if (err) |
4155c10a | 1834 | goto out_err; |
53dbb0b4 MS |
1835 | |
1836 | lower = strchr(lower, '\0') + 1; | |
1837 | } | |
1838 | ||
1839 | err = -EINVAL; | |
1840 | sb->s_stack_depth++; | |
1841 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { | |
1bd0a3ae | 1842 | pr_err("maximum fs stacking depth exceeded\n"); |
4155c10a | 1843 | goto out_err; |
53dbb0b4 MS |
1844 | } |
1845 | ||
b8e42a65 | 1846 | err = ovl_get_layers(sb, ofs, stack, numlower, layers); |
4155c10a MS |
1847 | if (err) |
1848 | goto out_err; | |
1849 | ||
1850 | err = -ENOMEM; | |
1851 | oe = ovl_alloc_entry(numlower); | |
1852 | if (!oe) | |
1853 | goto out_err; | |
1854 | ||
1855 | for (i = 0; i < numlower; i++) { | |
1856 | oe->lowerstack[i].dentry = dget(stack[i].dentry); | |
94375f9d | 1857 | oe->lowerstack[i].layer = &ofs->layers[i+1]; |
4155c10a | 1858 | } |
53dbb0b4 | 1859 | |
53dbb0b4 | 1860 | out: |
53dbb0b4 MS |
1861 | for (i = 0; i < numlower; i++) |
1862 | path_put(&stack[i]); | |
1863 | kfree(stack); | |
4155c10a MS |
1864 | |
1865 | return oe; | |
1866 | ||
1867 | out_err: | |
1868 | oe = ERR_PTR(err); | |
53dbb0b4 MS |
1869 | goto out; |
1870 | } | |
1871 | ||
146d62e5 AG |
1872 | /* |
1873 | * Check if this layer root is a descendant of: | |
1874 | * - another layer of this overlayfs instance | |
1875 | * - upper/work dir of any overlayfs instance | |
146d62e5 | 1876 | */ |
0be0bfd2 | 1877 | static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs, |
708fa015 MS |
1878 | struct dentry *dentry, const char *name, |
1879 | bool is_lower) | |
146d62e5 | 1880 | { |
9179c21d | 1881 | struct dentry *next = dentry, *parent; |
146d62e5 AG |
1882 | int err = 0; |
1883 | ||
9179c21d | 1884 | if (!dentry) |
146d62e5 AG |
1885 | return 0; |
1886 | ||
9179c21d MS |
1887 | parent = dget_parent(next); |
1888 | ||
1889 | /* Walk back ancestors to root (inclusive) looking for traps */ | |
1890 | while (!err && parent != next) { | |
708fa015 | 1891 | if (is_lower && ovl_lookup_trap_inode(sb, parent)) { |
146d62e5 | 1892 | err = -ELOOP; |
1bd0a3ae | 1893 | pr_err("overlapping %s path\n", name); |
0be0bfd2 AG |
1894 | } else if (ovl_is_inuse(parent)) { |
1895 | err = ovl_report_in_use(ofs, name); | |
146d62e5 | 1896 | } |
146d62e5 | 1897 | next = parent; |
9179c21d MS |
1898 | parent = dget_parent(next); |
1899 | dput(next); | |
146d62e5 AG |
1900 | } |
1901 | ||
9179c21d | 1902 | dput(parent); |
146d62e5 AG |
1903 | |
1904 | return err; | |
1905 | } | |
1906 | ||
1907 | /* | |
1908 | * Check if any of the layers or work dirs overlap. | |
1909 | */ | |
1910 | static int ovl_check_overlapping_layers(struct super_block *sb, | |
1911 | struct ovl_fs *ofs) | |
1912 | { | |
1913 | int i, err; | |
1914 | ||
08f4c7c8 MS |
1915 | if (ovl_upper_mnt(ofs)) { |
1916 | err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root, | |
708fa015 | 1917 | "upperdir", false); |
146d62e5 AG |
1918 | if (err) |
1919 | return err; | |
1920 | ||
1921 | /* | |
1922 | * Checking workbasedir avoids hitting ovl_is_inuse(parent) of | |
1923 | * this instance and covers overlapping work and index dirs, | |
1924 | * unless work or index dir have been moved since created inside | |
1925 | * workbasedir. In that case, we already have their traps in | |
1926 | * inode cache and we will catch that case on lookup. | |
1927 | */ | |
708fa015 MS |
1928 | err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir", |
1929 | false); | |
146d62e5 AG |
1930 | if (err) |
1931 | return err; | |
1932 | } | |
1933 | ||
94375f9d | 1934 | for (i = 1; i < ofs->numlayer; i++) { |
0be0bfd2 | 1935 | err = ovl_check_layer(sb, ofs, |
94375f9d | 1936 | ofs->layers[i].mnt->mnt_root, |
708fa015 | 1937 | "lowerdir", true); |
146d62e5 AG |
1938 | if (err) |
1939 | return err; | |
1940 | } | |
1941 | ||
1942 | return 0; | |
1943 | } | |
1944 | ||
2effc5c2 AG |
1945 | static struct dentry *ovl_get_root(struct super_block *sb, |
1946 | struct dentry *upperdentry, | |
1947 | struct ovl_entry *oe) | |
1948 | { | |
1949 | struct dentry *root; | |
62c832ed AG |
1950 | struct ovl_path *lowerpath = &oe->lowerstack[0]; |
1951 | unsigned long ino = d_inode(lowerpath->dentry)->i_ino; | |
1952 | int fsid = lowerpath->layer->fsid; | |
1953 | struct ovl_inode_params oip = { | |
1954 | .upperdentry = upperdentry, | |
1955 | .lowerpath = lowerpath, | |
1956 | }; | |
2effc5c2 AG |
1957 | |
1958 | root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); | |
1959 | if (!root) | |
1960 | return NULL; | |
1961 | ||
1962 | root->d_fsdata = oe; | |
1963 | ||
1964 | if (upperdentry) { | |
62c832ed AG |
1965 | /* Root inode uses upper st_ino/i_ino */ |
1966 | ino = d_inode(upperdentry)->i_ino; | |
1967 | fsid = 0; | |
2effc5c2 | 1968 | ovl_dentry_set_upper_alias(root); |
610afc0b | 1969 | if (ovl_is_impuredir(sb, upperdentry)) |
2effc5c2 AG |
1970 | ovl_set_flag(OVL_IMPURE, d_inode(root)); |
1971 | } | |
1972 | ||
1973 | /* Root is always merge -> can have whiteouts */ | |
1974 | ovl_set_flag(OVL_WHITEOUTS, d_inode(root)); | |
1975 | ovl_dentry_set_flag(OVL_E_CONNECTED, root); | |
1976 | ovl_set_upperdata(d_inode(root)); | |
62c832ed | 1977 | ovl_inode_init(d_inode(root), &oip, ino, fsid); |
f4288844 | 1978 | ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE); |
2effc5c2 AG |
1979 | |
1980 | return root; | |
1981 | } | |
1982 | ||
e9be9d5e MS |
1983 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
1984 | { | |
33006cdf | 1985 | struct path upperpath = { }; |
e9be9d5e | 1986 | struct dentry *root_dentry; |
4155c10a | 1987 | struct ovl_entry *oe; |
ad204488 | 1988 | struct ovl_fs *ofs; |
b8e42a65 | 1989 | struct ovl_layer *layers; |
51f8f3c4 | 1990 | struct cred *cred; |
b8e42a65 MS |
1991 | char *splitlower = NULL; |
1992 | unsigned int numlower; | |
e9be9d5e MS |
1993 | int err; |
1994 | ||
9efb069d MS |
1995 | err = -EIO; |
1996 | if (WARN_ON(sb->s_user_ns != current_user_ns())) | |
1997 | goto out; | |
1998 | ||
f4288844 MS |
1999 | sb->s_d_op = &ovl_dentry_operations; |
2000 | ||
f45827e8 | 2001 | err = -ENOMEM; |
ad204488 MS |
2002 | ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
2003 | if (!ofs) | |
e9be9d5e MS |
2004 | goto out; |
2005 | ||
d7b49b10 | 2006 | err = -ENOMEM; |
ad204488 | 2007 | ofs->creator_cred = cred = prepare_creds(); |
c6fe6254 MS |
2008 | if (!cred) |
2009 | goto out_err; | |
2010 | ||
c21c839b CX |
2011 | /* Is there a reason anyone would want not to share whiteouts? */ |
2012 | ofs->share_whiteout = true; | |
2013 | ||
ad204488 | 2014 | ofs->config.index = ovl_index_def; |
5830fb6b | 2015 | ofs->config.uuid = true; |
f168f109 | 2016 | ofs->config.nfs_export = ovl_nfs_export_def; |
795939a9 | 2017 | ofs->config.xino = ovl_xino_def(); |
d5791044 | 2018 | ofs->config.metacopy = ovl_metacopy_def; |
ad204488 | 2019 | err = ovl_parse_opt((char *) data, &ofs->config); |
f45827e8 | 2020 | if (err) |
a9075cdb | 2021 | goto out_err; |
f45827e8 | 2022 | |
e9be9d5e | 2023 | err = -EINVAL; |
ad204488 | 2024 | if (!ofs->config.lowerdir) { |
07f2af7b | 2025 | if (!silent) |
1bd0a3ae | 2026 | pr_err("missing 'lowerdir'\n"); |
a9075cdb | 2027 | goto out_err; |
e9be9d5e MS |
2028 | } |
2029 | ||
b8e42a65 MS |
2030 | err = -ENOMEM; |
2031 | splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL); | |
2032 | if (!splitlower) | |
2033 | goto out_err; | |
2034 | ||
d7b49b10 | 2035 | err = -EINVAL; |
b8e42a65 MS |
2036 | numlower = ovl_split_lowerdirs(splitlower); |
2037 | if (numlower > OVL_MAX_STACK) { | |
2038 | pr_err("too many lower directories, limit is %d\n", | |
2039 | OVL_MAX_STACK); | |
2040 | goto out_err; | |
2041 | } | |
2042 | ||
d7b49b10 | 2043 | err = -ENOMEM; |
b8e42a65 MS |
2044 | layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL); |
2045 | if (!layers) | |
2046 | goto out_err; | |
2047 | ||
2048 | ofs->layers = layers; | |
2049 | /* Layer 0 is reserved for upper even if there's no upper */ | |
2050 | ofs->numlayer = 1; | |
2051 | ||
53a08cb9 | 2052 | sb->s_stack_depth = 0; |
cf9a6784 | 2053 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
4d314f78 | 2054 | atomic_long_set(&ofs->last_ino, 1); |
4f119628 | 2055 | /* Assume underlying fs uses 32bit inodes unless proven otherwise */ |
53afcd31 | 2056 | if (ofs->config.xino != OVL_XINO_OFF) { |
0f831ec8 | 2057 | ofs->xino_mode = BITS_PER_LONG - 32; |
53afcd31 AG |
2058 | if (!ofs->xino_mode) { |
2059 | pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n"); | |
2060 | ofs->config.xino = OVL_XINO_OFF; | |
2061 | } | |
2062 | } | |
795939a9 | 2063 | |
146d62e5 AG |
2064 | /* alloc/destroy_inode needed for setting up traps in inode cache */ |
2065 | sb->s_op = &ovl_super_operations; | |
2066 | ||
ad204488 | 2067 | if (ofs->config.upperdir) { |
335d3fc5 SD |
2068 | struct super_block *upper_sb; |
2069 | ||
d7b49b10 | 2070 | err = -EINVAL; |
ad204488 | 2071 | if (!ofs->config.workdir) { |
1bd0a3ae | 2072 | pr_err("missing 'workdir'\n"); |
a9075cdb | 2073 | goto out_err; |
53a08cb9 | 2074 | } |
e9be9d5e | 2075 | |
b8e42a65 | 2076 | err = ovl_get_upper(sb, ofs, &layers[0], &upperpath); |
53a08cb9 | 2077 | if (err) |
a9075cdb | 2078 | goto out_err; |
2cac0c00 | 2079 | |
335d3fc5 SD |
2080 | upper_sb = ovl_upper_mnt(ofs)->mnt_sb; |
2081 | if (!ovl_should_sync(ofs)) { | |
2082 | ofs->errseq = errseq_sample(&upper_sb->s_wb_err); | |
2083 | if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) { | |
2084 | err = -EIO; | |
2085 | pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n"); | |
2086 | goto out_err; | |
2087 | } | |
2088 | } | |
2089 | ||
146d62e5 | 2090 | err = ovl_get_workdir(sb, ofs, &upperpath); |
8ed61dc3 | 2091 | if (err) |
a9075cdb | 2092 | goto out_err; |
c6fe6254 | 2093 | |
ad204488 | 2094 | if (!ofs->workdir) |
1751e8a6 | 2095 | sb->s_flags |= SB_RDONLY; |
6e88256e | 2096 | |
335d3fc5 SD |
2097 | sb->s_stack_depth = upper_sb->s_stack_depth; |
2098 | sb->s_time_gran = upper_sb->s_time_gran; | |
e9be9d5e | 2099 | } |
b8e42a65 | 2100 | oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers); |
4155c10a MS |
2101 | err = PTR_ERR(oe); |
2102 | if (IS_ERR(oe)) | |
a9075cdb | 2103 | goto out_err; |
e9be9d5e | 2104 | |
71cbad7e | 2105 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
08f4c7c8 | 2106 | if (!ovl_upper_mnt(ofs)) |
1751e8a6 | 2107 | sb->s_flags |= SB_RDONLY; |
e9be9d5e | 2108 | |
5830fb6b PT |
2109 | if (!ofs->config.uuid && ofs->numfs > 1) { |
2110 | pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n"); | |
2111 | ofs->config.uuid = true; | |
2112 | } | |
2113 | ||
470c1563 | 2114 | if (!ovl_force_readonly(ofs) && ofs->config.index) { |
146d62e5 | 2115 | err = ovl_get_indexdir(sb, ofs, oe, &upperpath); |
54fb347e | 2116 | if (err) |
4155c10a | 2117 | goto out_free_oe; |
6e88256e | 2118 | |
972d0093 | 2119 | /* Force r/o mount with no index dir */ |
20396365 | 2120 | if (!ofs->indexdir) |
1751e8a6 | 2121 | sb->s_flags |= SB_RDONLY; |
02bcd157 AG |
2122 | } |
2123 | ||
146d62e5 AG |
2124 | err = ovl_check_overlapping_layers(sb, ofs); |
2125 | if (err) | |
2126 | goto out_free_oe; | |
2127 | ||
972d0093 | 2128 | /* Show index=off in /proc/mounts for forced r/o mount */ |
f168f109 | 2129 | if (!ofs->indexdir) { |
ad204488 | 2130 | ofs->config.index = false; |
08f4c7c8 | 2131 | if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) { |
1bd0a3ae | 2132 | pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n"); |
f168f109 AG |
2133 | ofs->config.nfs_export = false; |
2134 | } | |
2135 | } | |
02bcd157 | 2136 | |
d5791044 | 2137 | if (ofs->config.metacopy && ofs->config.nfs_export) { |
1bd0a3ae | 2138 | pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); |
d5791044 VG |
2139 | ofs->config.nfs_export = false; |
2140 | } | |
2141 | ||
8383f174 AG |
2142 | if (ofs->config.nfs_export) |
2143 | sb->s_export_op = &ovl_export_operations; | |
2144 | ||
51f8f3c4 KK |
2145 | /* Never override disk quota limits or use reserved space */ |
2146 | cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); | |
2147 | ||
655042cc | 2148 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
2d2f2d73 MS |
2149 | sb->s_xattr = ofs->config.userxattr ? ovl_user_xattr_handlers : |
2150 | ovl_trusted_xattr_handlers; | |
ad204488 | 2151 | sb->s_fs_info = ofs; |
7c4d37c2 | 2152 | sb->s_flags |= SB_POSIXACL; |
32b1924b | 2153 | sb->s_iflags |= SB_I_SKIP_SYNC; |
655042cc | 2154 | |
c6fe6254 | 2155 | err = -ENOMEM; |
2effc5c2 | 2156 | root_dentry = ovl_get_root(sb, upperpath.dentry, oe); |
e9be9d5e | 2157 | if (!root_dentry) |
4155c10a | 2158 | goto out_free_oe; |
e9be9d5e MS |
2159 | |
2160 | mntput(upperpath.mnt); | |
b8e42a65 | 2161 | kfree(splitlower); |
ed06e069 | 2162 | |
e9be9d5e | 2163 | sb->s_root = root_dentry; |
e9be9d5e MS |
2164 | |
2165 | return 0; | |
2166 | ||
4155c10a MS |
2167 | out_free_oe: |
2168 | ovl_entry_stack_free(oe); | |
b9343632 | 2169 | kfree(oe); |
4155c10a | 2170 | out_err: |
b8e42a65 | 2171 | kfree(splitlower); |
e9be9d5e | 2172 | path_put(&upperpath); |
ad204488 | 2173 | ovl_free_fs(ofs); |
e9be9d5e MS |
2174 | out: |
2175 | return err; | |
2176 | } | |
2177 | ||
2178 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
2179 | const char *dev_name, void *raw_data) | |
2180 | { | |
2181 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
2182 | } | |
2183 | ||
2184 | static struct file_system_type ovl_fs_type = { | |
2185 | .owner = THIS_MODULE, | |
ef94b186 | 2186 | .name = "overlay", |
459c7c56 | 2187 | .fs_flags = FS_USERNS_MOUNT, |
e9be9d5e MS |
2188 | .mount = ovl_mount, |
2189 | .kill_sb = kill_anon_super, | |
2190 | }; | |
ef94b186 | 2191 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e | 2192 | |
13cf199d AG |
2193 | static void ovl_inode_init_once(void *foo) |
2194 | { | |
2195 | struct ovl_inode *oi = foo; | |
2196 | ||
2197 | inode_init_once(&oi->vfs_inode); | |
2198 | } | |
2199 | ||
e9be9d5e MS |
2200 | static int __init ovl_init(void) |
2201 | { | |
13cf199d AG |
2202 | int err; |
2203 | ||
2204 | ovl_inode_cachep = kmem_cache_create("ovl_inode", | |
2205 | sizeof(struct ovl_inode), 0, | |
2206 | (SLAB_RECLAIM_ACCOUNT| | |
2207 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), | |
2208 | ovl_inode_init_once); | |
2209 | if (ovl_inode_cachep == NULL) | |
2210 | return -ENOMEM; | |
2211 | ||
2406a307 JX |
2212 | err = ovl_aio_request_cache_init(); |
2213 | if (!err) { | |
2214 | err = register_filesystem(&ovl_fs_type); | |
2215 | if (!err) | |
2216 | return 0; | |
2217 | ||
2218 | ovl_aio_request_cache_destroy(); | |
2219 | } | |
2220 | kmem_cache_destroy(ovl_inode_cachep); | |
13cf199d AG |
2221 | |
2222 | return err; | |
e9be9d5e MS |
2223 | } |
2224 | ||
2225 | static void __exit ovl_exit(void) | |
2226 | { | |
2227 | unregister_filesystem(&ovl_fs_type); | |
13cf199d AG |
2228 | |
2229 | /* | |
2230 | * Make sure all delayed rcu free inodes are flushed before we | |
2231 | * destroy cache. | |
2232 | */ | |
2233 | rcu_barrier(); | |
2234 | kmem_cache_destroy(ovl_inode_cachep); | |
2406a307 | 2235 | ovl_aio_request_cache_destroy(); |
e9be9d5e MS |
2236 | } |
2237 | ||
2238 | module_init(ovl_init); | |
2239 | module_exit(ovl_exit); |