]>
Commit | Line | Data |
---|---|---|
e9be9d5e MS |
1 | /* |
2 | * | |
3 | * Copyright (C) 2011 Novell Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
9 | ||
5b825c3a | 10 | #include <uapi/linux/magic.h> |
e9be9d5e MS |
11 | #include <linux/fs.h> |
12 | #include <linux/namei.h> | |
13 | #include <linux/xattr.h> | |
e9be9d5e | 14 | #include <linux/mount.h> |
e9be9d5e MS |
15 | #include <linux/parser.h> |
16 | #include <linux/module.h> | |
cc259639 | 17 | #include <linux/statfs.h> |
f45827e8 | 18 | #include <linux/seq_file.h> |
d837a49b | 19 | #include <linux/posix_acl_xattr.h> |
e487d889 | 20 | #include <linux/exportfs.h> |
e9be9d5e MS |
21 | #include "overlayfs.h" |
22 | ||
23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
24 | MODULE_DESCRIPTION("Overlay filesystem"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
e9be9d5e MS |
27 | |
28 | struct ovl_dir_cache; | |
29 | ||
a78d9f0d MS |
30 | #define OVL_MAX_STACK 500 |
31 | ||
688ea0e5 MS |
32 | static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); |
33 | module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); | |
253e7483 | 34 | MODULE_PARM_DESC(redirect_dir, |
688ea0e5 | 35 | "Default to on or off for the redirect_dir feature"); |
e9be9d5e | 36 | |
438c84c2 MS |
37 | static bool ovl_redirect_always_follow = |
38 | IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); | |
39 | module_param_named(redirect_always_follow, ovl_redirect_always_follow, | |
40 | bool, 0644); | |
253e7483 | 41 | MODULE_PARM_DESC(redirect_always_follow, |
438c84c2 MS |
42 | "Follow redirects even if redirect_dir feature is turned off"); |
43 | ||
02bcd157 AG |
44 | static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); |
45 | module_param_named(index, ovl_index_def, bool, 0644); | |
253e7483 | 46 | MODULE_PARM_DESC(index, |
02bcd157 AG |
47 | "Default to on or off for the inodes index feature"); |
48 | ||
f168f109 AG |
49 | static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); |
50 | module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); | |
253e7483 | 51 | MODULE_PARM_DESC(nfs_export, |
f168f109 AG |
52 | "Default to on or off for the NFS export feature"); |
53 | ||
795939a9 AG |
54 | static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); |
55 | module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); | |
253e7483 | 56 | MODULE_PARM_DESC(xino_auto, |
795939a9 AG |
57 | "Auto enable xino feature"); |
58 | ||
4155c10a MS |
59 | static void ovl_entry_stack_free(struct ovl_entry *oe) |
60 | { | |
61 | unsigned int i; | |
62 | ||
63 | for (i = 0; i < oe->numlower; i++) | |
64 | dput(oe->lowerstack[i].dentry); | |
65 | } | |
66 | ||
d5791044 VG |
67 | static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); |
68 | module_param_named(metacopy, ovl_metacopy_def, bool, 0644); | |
253e7483 | 69 | MODULE_PARM_DESC(metacopy, |
d5791044 VG |
70 | "Default to on or off for the metadata only copy up feature"); |
71 | ||
e9be9d5e MS |
72 | static void ovl_dentry_release(struct dentry *dentry) |
73 | { | |
74 | struct ovl_entry *oe = dentry->d_fsdata; | |
75 | ||
76 | if (oe) { | |
4155c10a | 77 | ovl_entry_stack_free(oe); |
e9be9d5e MS |
78 | kfree_rcu(oe, rcu); |
79 | } | |
80 | } | |
81 | ||
2d902671 | 82 | static struct dentry *ovl_d_real(struct dentry *dentry, |
fb16043b | 83 | const struct inode *inode) |
d101a125 MS |
84 | { |
85 | struct dentry *real; | |
86 | ||
e8c985ba MS |
87 | /* It's an overlay file */ |
88 | if (inode && d_inode(dentry) == inode) | |
89 | return dentry; | |
90 | ||
ca4c8a3a | 91 | if (!d_is_reg(dentry)) { |
d101a125 MS |
92 | if (!inode || inode == d_inode(dentry)) |
93 | return dentry; | |
94 | goto bug; | |
95 | } | |
96 | ||
97 | real = ovl_dentry_upper(dentry); | |
2c3d7358 | 98 | if (real && (inode == d_inode(real))) |
d101a125 MS |
99 | return real; |
100 | ||
2c3d7358 VG |
101 | if (real && !inode && ovl_has_upperdata(d_inode(dentry))) |
102 | return real; | |
103 | ||
104 | real = ovl_dentry_lowerdata(dentry); | |
d101a125 MS |
105 | if (!real) |
106 | goto bug; | |
107 | ||
c4fcfc16 | 108 | /* Handle recursion */ |
fb16043b | 109 | real = d_real(real, inode); |
c4fcfc16 | 110 | |
d101a125 MS |
111 | if (!inode || inode == d_inode(real)) |
112 | return real; | |
d101a125 | 113 | bug: |
656189d2 | 114 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
d101a125 MS |
115 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
116 | return dentry; | |
117 | } | |
118 | ||
7c03b5d4 MS |
119 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
120 | { | |
121 | struct ovl_entry *oe = dentry->d_fsdata; | |
122 | unsigned int i; | |
123 | int ret = 1; | |
124 | ||
125 | for (i = 0; i < oe->numlower; i++) { | |
126 | struct dentry *d = oe->lowerstack[i].dentry; | |
127 | ||
128 | if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
129 | ret = d->d_op->d_revalidate(d, flags); | |
130 | if (ret < 0) | |
131 | return ret; | |
132 | if (!ret) { | |
133 | if (!(flags & LOOKUP_RCU)) | |
134 | d_invalidate(d); | |
135 | return -ESTALE; | |
136 | } | |
137 | } | |
138 | } | |
139 | return 1; | |
140 | } | |
141 | ||
142 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
143 | { | |
144 | struct ovl_entry *oe = dentry->d_fsdata; | |
145 | unsigned int i; | |
146 | int ret = 1; | |
147 | ||
148 | for (i = 0; i < oe->numlower; i++) { | |
149 | struct dentry *d = oe->lowerstack[i].dentry; | |
150 | ||
151 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { | |
152 | ret = d->d_op->d_weak_revalidate(d, flags); | |
153 | if (ret <= 0) | |
154 | break; | |
155 | } | |
156 | } | |
157 | return ret; | |
158 | } | |
159 | ||
e9be9d5e MS |
160 | static const struct dentry_operations ovl_dentry_operations = { |
161 | .d_release = ovl_dentry_release, | |
d101a125 | 162 | .d_real = ovl_d_real, |
e9be9d5e MS |
163 | }; |
164 | ||
7c03b5d4 MS |
165 | static const struct dentry_operations ovl_reval_dentry_operations = { |
166 | .d_release = ovl_dentry_release, | |
d101a125 | 167 | .d_real = ovl_d_real, |
7c03b5d4 MS |
168 | .d_revalidate = ovl_dentry_revalidate, |
169 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
170 | }; | |
171 | ||
13cf199d AG |
172 | static struct kmem_cache *ovl_inode_cachep; |
173 | ||
174 | static struct inode *ovl_alloc_inode(struct super_block *sb) | |
175 | { | |
176 | struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL); | |
177 | ||
b3885bd6 HN |
178 | if (!oi) |
179 | return NULL; | |
180 | ||
04a01ac7 | 181 | oi->cache = NULL; |
cf31c463 | 182 | oi->redirect = NULL; |
04a01ac7 | 183 | oi->version = 0; |
13c72075 | 184 | oi->flags = 0; |
09d8b586 | 185 | oi->__upperdentry = NULL; |
25b7713a | 186 | oi->lower = NULL; |
2664bd08 | 187 | oi->lowerdata = NULL; |
a015dafc | 188 | mutex_init(&oi->lock); |
25b7713a | 189 | |
13cf199d AG |
190 | return &oi->vfs_inode; |
191 | } | |
192 | ||
0b269ded | 193 | static void ovl_free_inode(struct inode *inode) |
13cf199d | 194 | { |
0b269ded | 195 | struct ovl_inode *oi = OVL_I(inode); |
13cf199d | 196 | |
0b269ded AV |
197 | kfree(oi->redirect); |
198 | mutex_destroy(&oi->lock); | |
199 | kmem_cache_free(ovl_inode_cachep, oi); | |
13cf199d AG |
200 | } |
201 | ||
202 | static void ovl_destroy_inode(struct inode *inode) | |
203 | { | |
09d8b586 MS |
204 | struct ovl_inode *oi = OVL_I(inode); |
205 | ||
206 | dput(oi->__upperdentry); | |
31747eda | 207 | iput(oi->lower); |
2664bd08 VG |
208 | if (S_ISDIR(inode->i_mode)) |
209 | ovl_dir_cache_free(inode); | |
210 | else | |
211 | iput(oi->lowerdata); | |
13cf199d AG |
212 | } |
213 | ||
ad204488 | 214 | static void ovl_free_fs(struct ovl_fs *ofs) |
e9be9d5e | 215 | { |
dd662667 | 216 | unsigned i; |
e9be9d5e | 217 | |
146d62e5 AG |
218 | iput(ofs->indexdir_trap); |
219 | iput(ofs->workdir_trap); | |
220 | iput(ofs->upperdir_trap); | |
ad204488 MS |
221 | dput(ofs->indexdir); |
222 | dput(ofs->workdir); | |
223 | if (ofs->workdir_locked) | |
224 | ovl_inuse_unlock(ofs->workbasedir); | |
225 | dput(ofs->workbasedir); | |
226 | if (ofs->upperdir_locked) | |
227 | ovl_inuse_unlock(ofs->upper_mnt->mnt_root); | |
228 | mntput(ofs->upper_mnt); | |
146d62e5 AG |
229 | for (i = 0; i < ofs->numlower; i++) { |
230 | iput(ofs->lower_layers[i].trap); | |
ad204488 | 231 | mntput(ofs->lower_layers[i].mnt); |
146d62e5 | 232 | } |
5148626b AG |
233 | for (i = 0; i < ofs->numlowerfs; i++) |
234 | free_anon_bdev(ofs->lower_fs[i].pseudo_dev); | |
ad204488 | 235 | kfree(ofs->lower_layers); |
5148626b | 236 | kfree(ofs->lower_fs); |
ad204488 MS |
237 | |
238 | kfree(ofs->config.lowerdir); | |
239 | kfree(ofs->config.upperdir); | |
240 | kfree(ofs->config.workdir); | |
438c84c2 | 241 | kfree(ofs->config.redirect_mode); |
ad204488 MS |
242 | if (ofs->creator_cred) |
243 | put_cred(ofs->creator_cred); | |
244 | kfree(ofs); | |
e9be9d5e MS |
245 | } |
246 | ||
a9075cdb MS |
247 | static void ovl_put_super(struct super_block *sb) |
248 | { | |
249 | struct ovl_fs *ofs = sb->s_fs_info; | |
250 | ||
251 | ovl_free_fs(ofs); | |
252 | } | |
253 | ||
e8d4bfe3 | 254 | /* Sync real dirty inodes in upper filesystem (if it exists) */ |
e593b2bf AG |
255 | static int ovl_sync_fs(struct super_block *sb, int wait) |
256 | { | |
ad204488 | 257 | struct ovl_fs *ofs = sb->s_fs_info; |
e593b2bf AG |
258 | struct super_block *upper_sb; |
259 | int ret; | |
260 | ||
ad204488 | 261 | if (!ofs->upper_mnt) |
e593b2bf | 262 | return 0; |
e8d4bfe3 CX |
263 | |
264 | /* | |
265 | * If this is a sync(2) call or an emergency sync, all the super blocks | |
266 | * will be iterated, including upper_sb, so no need to do anything. | |
267 | * | |
268 | * If this is a syncfs(2) call, then we do need to call | |
269 | * sync_filesystem() on upper_sb, but enough if we do it when being | |
270 | * called with wait == 1. | |
271 | */ | |
272 | if (!wait) | |
e593b2bf AG |
273 | return 0; |
274 | ||
e8d4bfe3 CX |
275 | upper_sb = ofs->upper_mnt->mnt_sb; |
276 | ||
e593b2bf | 277 | down_read(&upper_sb->s_umount); |
e8d4bfe3 | 278 | ret = sync_filesystem(upper_sb); |
e593b2bf | 279 | up_read(&upper_sb->s_umount); |
e8d4bfe3 | 280 | |
e593b2bf AG |
281 | return ret; |
282 | } | |
283 | ||
cc259639 AW |
284 | /** |
285 | * ovl_statfs | |
286 | * @sb: The overlayfs super block | |
287 | * @buf: The struct kstatfs to fill in with stats | |
288 | * | |
289 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 290 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
291 | */ |
292 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
293 | { | |
294 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
295 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
296 | struct path path; | |
297 | int err; | |
298 | ||
4ebc5818 | 299 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
300 | |
301 | err = vfs_statfs(&path, buf); | |
302 | if (!err) { | |
6b2d5fe4 | 303 | buf->f_namelen = ofs->namelen; |
cc259639 AW |
304 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
305 | } | |
306 | ||
307 | return err; | |
308 | } | |
309 | ||
02bcd157 | 310 | /* Will this overlay be forced to mount/remount ro? */ |
ad204488 | 311 | static bool ovl_force_readonly(struct ovl_fs *ofs) |
02bcd157 | 312 | { |
ad204488 | 313 | return (!ofs->upper_mnt || !ofs->workdir); |
02bcd157 AG |
314 | } |
315 | ||
438c84c2 MS |
316 | static const char *ovl_redirect_mode_def(void) |
317 | { | |
318 | return ovl_redirect_dir_def ? "on" : "off"; | |
319 | } | |
320 | ||
795939a9 AG |
321 | enum { |
322 | OVL_XINO_OFF, | |
323 | OVL_XINO_AUTO, | |
324 | OVL_XINO_ON, | |
325 | }; | |
326 | ||
327 | static const char * const ovl_xino_str[] = { | |
328 | "off", | |
329 | "auto", | |
330 | "on", | |
331 | }; | |
332 | ||
333 | static inline int ovl_xino_def(void) | |
334 | { | |
335 | return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; | |
336 | } | |
337 | ||
f45827e8 EZ |
338 | /** |
339 | * ovl_show_options | |
340 | * | |
341 | * Prints the mount options for a given superblock. | |
342 | * Returns zero; does not fail. | |
343 | */ | |
344 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
345 | { | |
346 | struct super_block *sb = dentry->d_sb; | |
ad204488 | 347 | struct ovl_fs *ofs = sb->s_fs_info; |
f45827e8 | 348 | |
ad204488 MS |
349 | seq_show_option(m, "lowerdir", ofs->config.lowerdir); |
350 | if (ofs->config.upperdir) { | |
351 | seq_show_option(m, "upperdir", ofs->config.upperdir); | |
352 | seq_show_option(m, "workdir", ofs->config.workdir); | |
53a08cb9 | 353 | } |
ad204488 | 354 | if (ofs->config.default_permissions) |
8d3095f4 | 355 | seq_puts(m, ",default_permissions"); |
438c84c2 MS |
356 | if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) |
357 | seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); | |
ad204488 | 358 | if (ofs->config.index != ovl_index_def) |
438c84c2 | 359 | seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); |
f168f109 AG |
360 | if (ofs->config.nfs_export != ovl_nfs_export_def) |
361 | seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ? | |
362 | "on" : "off"); | |
795939a9 AG |
363 | if (ofs->config.xino != ovl_xino_def()) |
364 | seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); | |
d5791044 VG |
365 | if (ofs->config.metacopy != ovl_metacopy_def) |
366 | seq_printf(m, ",metacopy=%s", | |
367 | ofs->config.metacopy ? "on" : "off"); | |
f45827e8 EZ |
368 | return 0; |
369 | } | |
370 | ||
3cdf6fe9 SL |
371 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
372 | { | |
ad204488 | 373 | struct ovl_fs *ofs = sb->s_fs_info; |
3cdf6fe9 | 374 | |
1751e8a6 | 375 | if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) |
3cdf6fe9 SL |
376 | return -EROFS; |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
e9be9d5e | 381 | static const struct super_operations ovl_super_operations = { |
13cf199d | 382 | .alloc_inode = ovl_alloc_inode, |
0b269ded | 383 | .free_inode = ovl_free_inode, |
13cf199d AG |
384 | .destroy_inode = ovl_destroy_inode, |
385 | .drop_inode = generic_delete_inode, | |
e9be9d5e | 386 | .put_super = ovl_put_super, |
e593b2bf | 387 | .sync_fs = ovl_sync_fs, |
cc259639 | 388 | .statfs = ovl_statfs, |
f45827e8 | 389 | .show_options = ovl_show_options, |
3cdf6fe9 | 390 | .remount_fs = ovl_remount, |
e9be9d5e MS |
391 | }; |
392 | ||
393 | enum { | |
394 | OPT_LOWERDIR, | |
395 | OPT_UPPERDIR, | |
396 | OPT_WORKDIR, | |
8d3095f4 | 397 | OPT_DEFAULT_PERMISSIONS, |
438c84c2 | 398 | OPT_REDIRECT_DIR, |
02bcd157 AG |
399 | OPT_INDEX_ON, |
400 | OPT_INDEX_OFF, | |
f168f109 AG |
401 | OPT_NFS_EXPORT_ON, |
402 | OPT_NFS_EXPORT_OFF, | |
795939a9 AG |
403 | OPT_XINO_ON, |
404 | OPT_XINO_OFF, | |
405 | OPT_XINO_AUTO, | |
d5791044 VG |
406 | OPT_METACOPY_ON, |
407 | OPT_METACOPY_OFF, | |
e9be9d5e MS |
408 | OPT_ERR, |
409 | }; | |
410 | ||
411 | static const match_table_t ovl_tokens = { | |
412 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
413 | {OPT_UPPERDIR, "upperdir=%s"}, | |
414 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 415 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
438c84c2 | 416 | {OPT_REDIRECT_DIR, "redirect_dir=%s"}, |
02bcd157 AG |
417 | {OPT_INDEX_ON, "index=on"}, |
418 | {OPT_INDEX_OFF, "index=off"}, | |
f168f109 AG |
419 | {OPT_NFS_EXPORT_ON, "nfs_export=on"}, |
420 | {OPT_NFS_EXPORT_OFF, "nfs_export=off"}, | |
795939a9 AG |
421 | {OPT_XINO_ON, "xino=on"}, |
422 | {OPT_XINO_OFF, "xino=off"}, | |
423 | {OPT_XINO_AUTO, "xino=auto"}, | |
d5791044 VG |
424 | {OPT_METACOPY_ON, "metacopy=on"}, |
425 | {OPT_METACOPY_OFF, "metacopy=off"}, | |
e9be9d5e MS |
426 | {OPT_ERR, NULL} |
427 | }; | |
428 | ||
91c77947 MS |
429 | static char *ovl_next_opt(char **s) |
430 | { | |
431 | char *sbegin = *s; | |
432 | char *p; | |
433 | ||
434 | if (sbegin == NULL) | |
435 | return NULL; | |
436 | ||
437 | for (p = sbegin; *p; p++) { | |
438 | if (*p == '\\') { | |
439 | p++; | |
440 | if (!*p) | |
441 | break; | |
442 | } else if (*p == ',') { | |
443 | *p = '\0'; | |
444 | *s = p + 1; | |
445 | return sbegin; | |
446 | } | |
447 | } | |
448 | *s = NULL; | |
449 | return sbegin; | |
450 | } | |
451 | ||
438c84c2 MS |
452 | static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) |
453 | { | |
454 | if (strcmp(mode, "on") == 0) { | |
455 | config->redirect_dir = true; | |
456 | /* | |
457 | * Does not make sense to have redirect creation without | |
458 | * redirect following. | |
459 | */ | |
460 | config->redirect_follow = true; | |
461 | } else if (strcmp(mode, "follow") == 0) { | |
462 | config->redirect_follow = true; | |
463 | } else if (strcmp(mode, "off") == 0) { | |
464 | if (ovl_redirect_always_follow) | |
465 | config->redirect_follow = true; | |
466 | } else if (strcmp(mode, "nofollow") != 0) { | |
467 | pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n", | |
468 | mode); | |
469 | return -EINVAL; | |
470 | } | |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
e9be9d5e MS |
475 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
476 | { | |
477 | char *p; | |
d5791044 | 478 | int err; |
d47748e5 | 479 | bool metacopy_opt = false, redirect_opt = false; |
e9be9d5e | 480 | |
438c84c2 MS |
481 | config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); |
482 | if (!config->redirect_mode) | |
483 | return -ENOMEM; | |
484 | ||
91c77947 | 485 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
486 | int token; |
487 | substring_t args[MAX_OPT_ARGS]; | |
488 | ||
489 | if (!*p) | |
490 | continue; | |
491 | ||
492 | token = match_token(p, ovl_tokens, args); | |
493 | switch (token) { | |
494 | case OPT_UPPERDIR: | |
495 | kfree(config->upperdir); | |
496 | config->upperdir = match_strdup(&args[0]); | |
497 | if (!config->upperdir) | |
498 | return -ENOMEM; | |
499 | break; | |
500 | ||
501 | case OPT_LOWERDIR: | |
502 | kfree(config->lowerdir); | |
503 | config->lowerdir = match_strdup(&args[0]); | |
504 | if (!config->lowerdir) | |
505 | return -ENOMEM; | |
506 | break; | |
507 | ||
508 | case OPT_WORKDIR: | |
509 | kfree(config->workdir); | |
510 | config->workdir = match_strdup(&args[0]); | |
511 | if (!config->workdir) | |
512 | return -ENOMEM; | |
513 | break; | |
514 | ||
8d3095f4 MS |
515 | case OPT_DEFAULT_PERMISSIONS: |
516 | config->default_permissions = true; | |
517 | break; | |
518 | ||
438c84c2 MS |
519 | case OPT_REDIRECT_DIR: |
520 | kfree(config->redirect_mode); | |
521 | config->redirect_mode = match_strdup(&args[0]); | |
522 | if (!config->redirect_mode) | |
523 | return -ENOMEM; | |
d47748e5 | 524 | redirect_opt = true; |
a6c60655 MS |
525 | break; |
526 | ||
02bcd157 AG |
527 | case OPT_INDEX_ON: |
528 | config->index = true; | |
529 | break; | |
530 | ||
531 | case OPT_INDEX_OFF: | |
532 | config->index = false; | |
533 | break; | |
534 | ||
f168f109 AG |
535 | case OPT_NFS_EXPORT_ON: |
536 | config->nfs_export = true; | |
537 | break; | |
538 | ||
539 | case OPT_NFS_EXPORT_OFF: | |
540 | config->nfs_export = false; | |
541 | break; | |
542 | ||
795939a9 AG |
543 | case OPT_XINO_ON: |
544 | config->xino = OVL_XINO_ON; | |
545 | break; | |
546 | ||
547 | case OPT_XINO_OFF: | |
548 | config->xino = OVL_XINO_OFF; | |
549 | break; | |
550 | ||
551 | case OPT_XINO_AUTO: | |
552 | config->xino = OVL_XINO_AUTO; | |
553 | break; | |
554 | ||
d5791044 VG |
555 | case OPT_METACOPY_ON: |
556 | config->metacopy = true; | |
d47748e5 | 557 | metacopy_opt = true; |
d5791044 VG |
558 | break; |
559 | ||
560 | case OPT_METACOPY_OFF: | |
561 | config->metacopy = false; | |
562 | break; | |
563 | ||
e9be9d5e | 564 | default: |
bead55ef | 565 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
566 | return -EINVAL; |
567 | } | |
568 | } | |
71cbad7e | 569 | |
570 | /* Workdir is useless in non-upper mount */ | |
571 | if (!config->upperdir && config->workdir) { | |
572 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
573 | config->workdir); | |
574 | kfree(config->workdir); | |
575 | config->workdir = NULL; | |
576 | } | |
577 | ||
d5791044 VG |
578 | err = ovl_parse_redirect_mode(config, config->redirect_mode); |
579 | if (err) | |
580 | return err; | |
581 | ||
d47748e5 MS |
582 | /* |
583 | * This is to make the logic below simpler. It doesn't make any other | |
584 | * difference, since config->redirect_dir is only used for upper. | |
585 | */ | |
586 | if (!config->upperdir && config->redirect_follow) | |
587 | config->redirect_dir = true; | |
588 | ||
589 | /* Resolve metacopy -> redirect_dir dependency */ | |
590 | if (config->metacopy && !config->redirect_dir) { | |
591 | if (metacopy_opt && redirect_opt) { | |
592 | pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n", | |
593 | config->redirect_mode); | |
594 | return -EINVAL; | |
595 | } | |
596 | if (redirect_opt) { | |
597 | /* | |
598 | * There was an explicit redirect_dir=... that resulted | |
599 | * in this conflict. | |
600 | */ | |
601 | pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n", | |
602 | config->redirect_mode); | |
603 | config->metacopy = false; | |
604 | } else { | |
605 | /* Automatically enable redirect otherwise. */ | |
606 | config->redirect_follow = config->redirect_dir = true; | |
607 | } | |
d5791044 VG |
608 | } |
609 | ||
610 | return 0; | |
e9be9d5e MS |
611 | } |
612 | ||
613 | #define OVL_WORKDIR_NAME "work" | |
02bcd157 | 614 | #define OVL_INDEXDIR_NAME "index" |
e9be9d5e | 615 | |
ad204488 | 616 | static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, |
6b8aa129 | 617 | const char *name, bool persist) |
e9be9d5e | 618 | { |
ad204488 MS |
619 | struct inode *dir = ofs->workbasedir->d_inode; |
620 | struct vfsmount *mnt = ofs->upper_mnt; | |
e9be9d5e MS |
621 | struct dentry *work; |
622 | int err; | |
623 | bool retried = false; | |
6b8aa129 | 624 | bool locked = false; |
e9be9d5e | 625 | |
5955102c | 626 | inode_lock_nested(dir, I_MUTEX_PARENT); |
6b8aa129 AG |
627 | locked = true; |
628 | ||
e9be9d5e | 629 | retry: |
ad204488 | 630 | work = lookup_one_len(name, ofs->workbasedir, strlen(name)); |
e9be9d5e MS |
631 | |
632 | if (!IS_ERR(work)) { | |
c11b9fdd MS |
633 | struct iattr attr = { |
634 | .ia_valid = ATTR_MODE, | |
32a3d848 | 635 | .ia_mode = S_IFDIR | 0, |
c11b9fdd | 636 | }; |
e9be9d5e MS |
637 | |
638 | if (work->d_inode) { | |
639 | err = -EEXIST; | |
640 | if (retried) | |
641 | goto out_dput; | |
642 | ||
6b8aa129 AG |
643 | if (persist) |
644 | goto out_unlock; | |
645 | ||
e9be9d5e | 646 | retried = true; |
eea2fb48 | 647 | ovl_workdir_cleanup(dir, mnt, work, 0); |
e9be9d5e MS |
648 | dput(work); |
649 | goto retry; | |
650 | } | |
651 | ||
95a1c815 MS |
652 | work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode)); |
653 | err = PTR_ERR(work); | |
654 | if (IS_ERR(work)) | |
655 | goto out_err; | |
c11b9fdd | 656 | |
cb348edb MS |
657 | /* |
658 | * Try to remove POSIX ACL xattrs from workdir. We are good if: | |
659 | * | |
660 | * a) success (there was a POSIX ACL xattr and was removed) | |
661 | * b) -ENODATA (there was no POSIX ACL xattr) | |
662 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) | |
663 | * | |
664 | * There are various other error values that could effectively | |
665 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned | |
666 | * if the xattr name is too long), but the set of filesystems | |
667 | * allowed as upper are limited to "normal" ones, where checking | |
668 | * for the above two errors is sufficient. | |
669 | */ | |
c11b9fdd | 670 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
e1ff3dd1 | 671 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
672 | goto out_dput; |
673 | ||
674 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); | |
e1ff3dd1 | 675 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
676 | goto out_dput; |
677 | ||
678 | /* Clear any inherited mode bits */ | |
679 | inode_lock(work->d_inode); | |
680 | err = notify_change(work, &attr, NULL); | |
681 | inode_unlock(work->d_inode); | |
682 | if (err) | |
683 | goto out_dput; | |
6b8aa129 AG |
684 | } else { |
685 | err = PTR_ERR(work); | |
686 | goto out_err; | |
e9be9d5e MS |
687 | } |
688 | out_unlock: | |
6b8aa129 AG |
689 | if (locked) |
690 | inode_unlock(dir); | |
e9be9d5e MS |
691 | |
692 | return work; | |
693 | ||
694 | out_dput: | |
695 | dput(work); | |
6b8aa129 AG |
696 | out_err: |
697 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", | |
ad204488 | 698 | ofs->config.workdir, name, -err); |
6b8aa129 | 699 | work = NULL; |
e9be9d5e MS |
700 | goto out_unlock; |
701 | } | |
702 | ||
91c77947 MS |
703 | static void ovl_unescape(char *s) |
704 | { | |
705 | char *d = s; | |
706 | ||
707 | for (;; s++, d++) { | |
708 | if (*s == '\\') | |
709 | s++; | |
710 | *d = *s; | |
711 | if (!*s) | |
712 | break; | |
713 | } | |
714 | } | |
715 | ||
ab508822 MS |
716 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
717 | { | |
a78d9f0d | 718 | int err = -EINVAL; |
ab508822 | 719 | |
a78d9f0d MS |
720 | if (!*name) { |
721 | pr_err("overlayfs: empty lowerdir\n"); | |
722 | goto out; | |
723 | } | |
ab508822 MS |
724 | err = kern_path(name, LOOKUP_FOLLOW, path); |
725 | if (err) { | |
726 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
727 | goto out; | |
728 | } | |
729 | err = -EINVAL; | |
7c03b5d4 | 730 | if (ovl_dentry_weird(path->dentry)) { |
ab508822 MS |
731 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
732 | goto out_put; | |
733 | } | |
2b8c30e9 | 734 | if (!d_is_dir(path->dentry)) { |
ab508822 MS |
735 | pr_err("overlayfs: '%s' not a directory\n", name); |
736 | goto out_put; | |
737 | } | |
738 | return 0; | |
739 | ||
740 | out_put: | |
8aafcb59 | 741 | path_put_init(path); |
ab508822 MS |
742 | out: |
743 | return err; | |
744 | } | |
745 | ||
746 | static int ovl_mount_dir(const char *name, struct path *path) | |
747 | { | |
748 | int err = -ENOMEM; | |
749 | char *tmp = kstrdup(name, GFP_KERNEL); | |
750 | ||
751 | if (tmp) { | |
752 | ovl_unescape(tmp); | |
753 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 MS |
754 | |
755 | if (!err) | |
756 | if (ovl_dentry_remote(path->dentry)) { | |
757 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", | |
758 | tmp); | |
8aafcb59 | 759 | path_put_init(path); |
7c03b5d4 MS |
760 | err = -EINVAL; |
761 | } | |
ab508822 MS |
762 | kfree(tmp); |
763 | } | |
764 | return err; | |
765 | } | |
766 | ||
6b2d5fe4 MS |
767 | static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, |
768 | const char *name) | |
ab508822 | 769 | { |
ab508822 | 770 | struct kstatfs statfs; |
6b2d5fe4 MS |
771 | int err = vfs_statfs(path, &statfs); |
772 | ||
773 | if (err) | |
774 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
775 | else | |
776 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); | |
777 | ||
778 | return err; | |
779 | } | |
780 | ||
781 | static int ovl_lower_dir(const char *name, struct path *path, | |
782 | struct ovl_fs *ofs, int *stack_depth, bool *remote) | |
783 | { | |
e487d889 | 784 | int fh_type; |
6b2d5fe4 | 785 | int err; |
ab508822 | 786 | |
a78d9f0d | 787 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
788 | if (err) |
789 | goto out; | |
790 | ||
6b2d5fe4 MS |
791 | err = ovl_check_namelen(path, ofs, name); |
792 | if (err) | |
ab508822 | 793 | goto out_put; |
6b2d5fe4 | 794 | |
ab508822 MS |
795 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
796 | ||
7c03b5d4 MS |
797 | if (ovl_dentry_remote(path->dentry)) |
798 | *remote = true; | |
799 | ||
02bcd157 | 800 | /* |
f168f109 AG |
801 | * The inodes index feature and NFS export need to encode and decode |
802 | * file handles, so they require that all layers support them. | |
02bcd157 | 803 | */ |
e487d889 | 804 | fh_type = ovl_can_decode_fh(path->dentry->d_sb); |
f168f109 | 805 | if ((ofs->config.nfs_export || |
e487d889 | 806 | (ofs->config.index && ofs->config.upperdir)) && !fh_type) { |
02bcd157 | 807 | ofs->config.index = false; |
f168f109 AG |
808 | ofs->config.nfs_export = false; |
809 | pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", | |
810 | name); | |
02bcd157 AG |
811 | } |
812 | ||
e487d889 AG |
813 | /* Check if lower fs has 32bit inode numbers */ |
814 | if (fh_type != FILEID_INO32_GEN) | |
815 | ofs->xino_bits = 0; | |
816 | ||
ab508822 MS |
817 | return 0; |
818 | ||
819 | out_put: | |
8aafcb59 | 820 | path_put_init(path); |
ab508822 MS |
821 | out: |
822 | return err; | |
823 | } | |
824 | ||
e9be9d5e MS |
825 | /* Workdir should not be subdir of upperdir and vice versa */ |
826 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
827 | { | |
828 | bool ok = false; | |
829 | ||
830 | if (workdir != upperdir) { | |
831 | ok = (lock_rename(workdir, upperdir) == NULL); | |
832 | unlock_rename(workdir, upperdir); | |
833 | } | |
834 | return ok; | |
835 | } | |
836 | ||
a78d9f0d MS |
837 | static unsigned int ovl_split_lowerdirs(char *str) |
838 | { | |
839 | unsigned int ctr = 1; | |
840 | char *s, *d; | |
841 | ||
842 | for (s = d = str;; s++, d++) { | |
843 | if (*s == '\\') { | |
844 | s++; | |
845 | } else if (*s == ':') { | |
846 | *d = '\0'; | |
847 | ctr++; | |
848 | continue; | |
849 | } | |
850 | *d = *s; | |
851 | if (!*s) | |
852 | break; | |
853 | } | |
854 | return ctr; | |
855 | } | |
856 | ||
0eb45fc3 AG |
857 | static int __maybe_unused |
858 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, | |
859 | struct dentry *dentry, struct inode *inode, | |
860 | const char *name, void *buffer, size_t size) | |
861 | { | |
1d88f183 | 862 | return ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
0eb45fc3 AG |
863 | } |
864 | ||
0c97be22 AG |
865 | static int __maybe_unused |
866 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, | |
867 | struct dentry *dentry, struct inode *inode, | |
868 | const char *name, const void *value, | |
869 | size_t size, int flags) | |
d837a49b MS |
870 | { |
871 | struct dentry *workdir = ovl_workdir(dentry); | |
09d8b586 | 872 | struct inode *realinode = ovl_inode_real(inode); |
d837a49b MS |
873 | struct posix_acl *acl = NULL; |
874 | int err; | |
875 | ||
876 | /* Check that everything is OK before copy-up */ | |
877 | if (value) { | |
878 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
879 | if (IS_ERR(acl)) | |
880 | return PTR_ERR(acl); | |
881 | } | |
882 | err = -EOPNOTSUPP; | |
883 | if (!IS_POSIXACL(d_inode(workdir))) | |
884 | goto out_acl_release; | |
885 | if (!realinode->i_op->set_acl) | |
886 | goto out_acl_release; | |
887 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { | |
888 | err = acl ? -EACCES : 0; | |
889 | goto out_acl_release; | |
890 | } | |
891 | err = -EPERM; | |
892 | if (!inode_owner_or_capable(inode)) | |
893 | goto out_acl_release; | |
894 | ||
895 | posix_acl_release(acl); | |
896 | ||
fd3220d3 MS |
897 | /* |
898 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
899 | * be done with mounter's capabilities and so that won't do it for us). | |
900 | */ | |
901 | if (unlikely(inode->i_mode & S_ISGID) && | |
902 | handler->flags == ACL_TYPE_ACCESS && | |
903 | !in_group_p(inode->i_gid) && | |
904 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { | |
905 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; | |
906 | ||
907 | err = ovl_setattr(dentry, &iattr); | |
908 | if (err) | |
909 | return err; | |
910 | } | |
911 | ||
1d88f183 | 912 | err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); |
ce31513a | 913 | if (!err) |
09d8b586 | 914 | ovl_copyattr(ovl_inode_real(inode), inode); |
ce31513a MS |
915 | |
916 | return err; | |
d837a49b MS |
917 | |
918 | out_acl_release: | |
919 | posix_acl_release(acl); | |
920 | return err; | |
921 | } | |
922 | ||
0eb45fc3 AG |
923 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
924 | struct dentry *dentry, struct inode *inode, | |
925 | const char *name, void *buffer, size_t size) | |
926 | { | |
48fab5d7 | 927 | return -EOPNOTSUPP; |
0eb45fc3 AG |
928 | } |
929 | ||
d837a49b MS |
930 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
931 | struct dentry *dentry, struct inode *inode, | |
932 | const char *name, const void *value, | |
933 | size_t size, int flags) | |
934 | { | |
48fab5d7 | 935 | return -EOPNOTSUPP; |
d837a49b MS |
936 | } |
937 | ||
0eb45fc3 AG |
938 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
939 | struct dentry *dentry, struct inode *inode, | |
940 | const char *name, void *buffer, size_t size) | |
941 | { | |
1d88f183 | 942 | return ovl_xattr_get(dentry, inode, name, buffer, size); |
0eb45fc3 AG |
943 | } |
944 | ||
0e585ccc AG |
945 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
946 | struct dentry *dentry, struct inode *inode, | |
947 | const char *name, const void *value, | |
948 | size_t size, int flags) | |
949 | { | |
1d88f183 | 950 | return ovl_xattr_set(dentry, inode, name, value, size, flags); |
0e585ccc AG |
951 | } |
952 | ||
0c97be22 AG |
953 | static const struct xattr_handler __maybe_unused |
954 | ovl_posix_acl_access_xattr_handler = { | |
d837a49b MS |
955 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
956 | .flags = ACL_TYPE_ACCESS, | |
0eb45fc3 | 957 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
958 | .set = ovl_posix_acl_xattr_set, |
959 | }; | |
960 | ||
0c97be22 AG |
961 | static const struct xattr_handler __maybe_unused |
962 | ovl_posix_acl_default_xattr_handler = { | |
d837a49b MS |
963 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
964 | .flags = ACL_TYPE_DEFAULT, | |
0eb45fc3 | 965 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
966 | .set = ovl_posix_acl_xattr_set, |
967 | }; | |
968 | ||
969 | static const struct xattr_handler ovl_own_xattr_handler = { | |
970 | .prefix = OVL_XATTR_PREFIX, | |
0eb45fc3 | 971 | .get = ovl_own_xattr_get, |
d837a49b MS |
972 | .set = ovl_own_xattr_set, |
973 | }; | |
974 | ||
975 | static const struct xattr_handler ovl_other_xattr_handler = { | |
976 | .prefix = "", /* catch all */ | |
0eb45fc3 | 977 | .get = ovl_other_xattr_get, |
d837a49b MS |
978 | .set = ovl_other_xattr_set, |
979 | }; | |
980 | ||
981 | static const struct xattr_handler *ovl_xattr_handlers[] = { | |
0c97be22 | 982 | #ifdef CONFIG_FS_POSIX_ACL |
d837a49b MS |
983 | &ovl_posix_acl_access_xattr_handler, |
984 | &ovl_posix_acl_default_xattr_handler, | |
0c97be22 | 985 | #endif |
d837a49b MS |
986 | &ovl_own_xattr_handler, |
987 | &ovl_other_xattr_handler, | |
988 | NULL | |
989 | }; | |
990 | ||
146d62e5 AG |
991 | static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, |
992 | struct inode **ptrap, const char *name) | |
993 | { | |
994 | struct inode *trap; | |
995 | int err; | |
996 | ||
997 | trap = ovl_get_trap_inode(sb, dir); | |
1dac6f5b AB |
998 | err = PTR_ERR_OR_ZERO(trap); |
999 | if (err) { | |
146d62e5 AG |
1000 | if (err == -ELOOP) |
1001 | pr_err("overlayfs: conflicting %s path\n", name); | |
1002 | return err; | |
1003 | } | |
1004 | ||
1005 | *ptrap = trap; | |
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, | |
1010 | struct path *upperpath) | |
6ee8acf0 | 1011 | { |
5064975e | 1012 | struct vfsmount *upper_mnt; |
6ee8acf0 MS |
1013 | int err; |
1014 | ||
ad204488 | 1015 | err = ovl_mount_dir(ofs->config.upperdir, upperpath); |
6ee8acf0 MS |
1016 | if (err) |
1017 | goto out; | |
1018 | ||
1019 | /* Upper fs should not be r/o */ | |
1020 | if (sb_rdonly(upperpath->mnt->mnt_sb)) { | |
1021 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
1022 | err = -EINVAL; | |
1023 | goto out; | |
1024 | } | |
1025 | ||
ad204488 | 1026 | err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); |
6ee8acf0 MS |
1027 | if (err) |
1028 | goto out; | |
1029 | ||
146d62e5 AG |
1030 | err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap, |
1031 | "upperdir"); | |
1032 | if (err) | |
1033 | goto out; | |
1034 | ||
5064975e MS |
1035 | upper_mnt = clone_private_mount(upperpath); |
1036 | err = PTR_ERR(upper_mnt); | |
1037 | if (IS_ERR(upper_mnt)) { | |
1038 | pr_err("overlayfs: failed to clone upperpath\n"); | |
1039 | goto out; | |
1040 | } | |
1041 | ||
1042 | /* Don't inherit atime flags */ | |
1043 | upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
ad204488 | 1044 | ofs->upper_mnt = upper_mnt; |
8c25741a MS |
1045 | |
1046 | err = -EBUSY; | |
1047 | if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) { | |
1048 | ofs->upperdir_locked = true; | |
1049 | } else if (ofs->config.index) { | |
1050 | pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n"); | |
1051 | goto out; | |
1052 | } else { | |
1053 | pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); | |
1054 | } | |
1055 | ||
6ee8acf0 MS |
1056 | err = 0; |
1057 | out: | |
1058 | return err; | |
1059 | } | |
1060 | ||
146d62e5 AG |
1061 | static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, |
1062 | struct path *workpath) | |
8ed61dc3 | 1063 | { |
2ba9d57e | 1064 | struct vfsmount *mnt = ofs->upper_mnt; |
8ed61dc3 | 1065 | struct dentry *temp; |
e487d889 | 1066 | int fh_type; |
8ed61dc3 MS |
1067 | int err; |
1068 | ||
2ba9d57e AG |
1069 | err = mnt_want_write(mnt); |
1070 | if (err) | |
1071 | return err; | |
1072 | ||
ad204488 MS |
1073 | ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); |
1074 | if (!ofs->workdir) | |
2ba9d57e | 1075 | goto out; |
8ed61dc3 | 1076 | |
146d62e5 AG |
1077 | err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); |
1078 | if (err) | |
1079 | goto out; | |
1080 | ||
8ed61dc3 MS |
1081 | /* |
1082 | * Upper should support d_type, else whiteouts are visible. Given | |
1083 | * workdir and upper are on same fs, we can do iterate_dir() on | |
1084 | * workdir. This check requires successful creation of workdir in | |
1085 | * previous step. | |
1086 | */ | |
1087 | err = ovl_check_d_type_supported(workpath); | |
1088 | if (err < 0) | |
2ba9d57e | 1089 | goto out; |
8ed61dc3 MS |
1090 | |
1091 | /* | |
1092 | * We allowed this configuration and don't want to break users over | |
1093 | * kernel upgrade. So warn instead of erroring out. | |
1094 | */ | |
1095 | if (!err) | |
1096 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); | |
1097 | ||
1098 | /* Check if upper/work fs supports O_TMPFILE */ | |
ad204488 MS |
1099 | temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); |
1100 | ofs->tmpfile = !IS_ERR(temp); | |
1101 | if (ofs->tmpfile) | |
8ed61dc3 MS |
1102 | dput(temp); |
1103 | else | |
1104 | pr_warn("overlayfs: upper fs does not support tmpfile.\n"); | |
1105 | ||
1106 | /* | |
1107 | * Check if upper/work fs supports trusted.overlay.* xattr | |
1108 | */ | |
ad204488 | 1109 | err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0); |
8ed61dc3 | 1110 | if (err) { |
ad204488 | 1111 | ofs->noxattr = true; |
a683737b | 1112 | ofs->config.index = false; |
d5791044 VG |
1113 | ofs->config.metacopy = false; |
1114 | pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n"); | |
2ba9d57e | 1115 | err = 0; |
8ed61dc3 | 1116 | } else { |
ad204488 | 1117 | vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE); |
8ed61dc3 MS |
1118 | } |
1119 | ||
1120 | /* Check if upper/work fs supports file handles */ | |
e487d889 AG |
1121 | fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); |
1122 | if (ofs->config.index && !fh_type) { | |
ad204488 | 1123 | ofs->config.index = false; |
8ed61dc3 MS |
1124 | pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); |
1125 | } | |
1126 | ||
e487d889 AG |
1127 | /* Check if upper fs has 32bit inode numbers */ |
1128 | if (fh_type != FILEID_INO32_GEN) | |
1129 | ofs->xino_bits = 0; | |
1130 | ||
f168f109 AG |
1131 | /* NFS export of r/w mount depends on index */ |
1132 | if (ofs->config.nfs_export && !ofs->config.index) { | |
1133 | pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n"); | |
1134 | ofs->config.nfs_export = false; | |
1135 | } | |
2ba9d57e AG |
1136 | out: |
1137 | mnt_drop_write(mnt); | |
1138 | return err; | |
8ed61dc3 MS |
1139 | } |
1140 | ||
146d62e5 AG |
1141 | static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, |
1142 | struct path *upperpath) | |
520d7c86 MS |
1143 | { |
1144 | int err; | |
bca44b52 | 1145 | struct path workpath = { }; |
520d7c86 | 1146 | |
ad204488 | 1147 | err = ovl_mount_dir(ofs->config.workdir, &workpath); |
520d7c86 MS |
1148 | if (err) |
1149 | goto out; | |
1150 | ||
1151 | err = -EINVAL; | |
bca44b52 | 1152 | if (upperpath->mnt != workpath.mnt) { |
520d7c86 MS |
1153 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
1154 | goto out; | |
1155 | } | |
bca44b52 | 1156 | if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { |
520d7c86 MS |
1157 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
1158 | goto out; | |
1159 | } | |
1160 | ||
8c25741a MS |
1161 | ofs->workbasedir = dget(workpath.dentry); |
1162 | ||
520d7c86 | 1163 | err = -EBUSY; |
8c25741a | 1164 | if (ovl_inuse_trylock(ofs->workbasedir)) { |
ad204488 MS |
1165 | ofs->workdir_locked = true; |
1166 | } else if (ofs->config.index) { | |
520d7c86 MS |
1167 | pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n"); |
1168 | goto out; | |
1169 | } else { | |
1170 | pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); | |
1171 | } | |
1172 | ||
146d62e5 | 1173 | err = ovl_make_workdir(sb, ofs, &workpath); |
bca44b52 | 1174 | |
520d7c86 | 1175 | out: |
bca44b52 MS |
1176 | path_put(&workpath); |
1177 | ||
520d7c86 MS |
1178 | return err; |
1179 | } | |
1180 | ||
146d62e5 AG |
1181 | static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, |
1182 | struct ovl_entry *oe, struct path *upperpath) | |
f7e3a7d9 | 1183 | { |
2ba9d57e | 1184 | struct vfsmount *mnt = ofs->upper_mnt; |
f7e3a7d9 MS |
1185 | int err; |
1186 | ||
2ba9d57e AG |
1187 | err = mnt_want_write(mnt); |
1188 | if (err) | |
1189 | return err; | |
1190 | ||
f7e3a7d9 | 1191 | /* Verify lower root is upper root origin */ |
d9768076 | 1192 | err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry, |
05122443 | 1193 | true); |
f7e3a7d9 MS |
1194 | if (err) { |
1195 | pr_err("overlayfs: failed to verify upper root origin\n"); | |
1196 | goto out; | |
1197 | } | |
1198 | ||
ad204488 MS |
1199 | ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); |
1200 | if (ofs->indexdir) { | |
146d62e5 AG |
1201 | err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap, |
1202 | "indexdir"); | |
1203 | if (err) | |
1204 | goto out; | |
1205 | ||
ad1d615c AG |
1206 | /* |
1207 | * Verify upper root is exclusively associated with index dir. | |
1208 | * Older kernels stored upper fh in "trusted.overlay.origin" | |
1209 | * xattr. If that xattr exists, verify that it is a match to | |
1210 | * upper dir file handle. In any case, verify or set xattr | |
1211 | * "trusted.overlay.upper" to indicate that index may have | |
1212 | * directory entries. | |
1213 | */ | |
1214 | if (ovl_check_origin_xattr(ofs->indexdir)) { | |
1215 | err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN, | |
1216 | upperpath->dentry, true, false); | |
1217 | if (err) | |
1218 | pr_err("overlayfs: failed to verify index dir 'origin' xattr\n"); | |
1219 | } | |
1220 | err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true); | |
f7e3a7d9 | 1221 | if (err) |
ad1d615c | 1222 | pr_err("overlayfs: failed to verify index dir 'upper' xattr\n"); |
f7e3a7d9 MS |
1223 | |
1224 | /* Cleanup bad/stale/orphan index entries */ | |
1225 | if (!err) | |
1eff1a1d | 1226 | err = ovl_indexdir_cleanup(ofs); |
f7e3a7d9 | 1227 | } |
ad204488 | 1228 | if (err || !ofs->indexdir) |
f7e3a7d9 MS |
1229 | pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); |
1230 | ||
1231 | out: | |
2ba9d57e | 1232 | mnt_drop_write(mnt); |
f7e3a7d9 MS |
1233 | return err; |
1234 | } | |
1235 | ||
9df085f3 AG |
1236 | static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid) |
1237 | { | |
1238 | unsigned int i; | |
1239 | ||
1240 | if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt)) | |
1241 | return true; | |
1242 | ||
1243 | for (i = 0; i < ofs->numlowerfs; i++) { | |
1244 | /* | |
1245 | * We use uuid to associate an overlay lower file handle with a | |
1246 | * lower layer, so we can accept lower fs with null uuid as long | |
1247 | * as all lower layers with null uuid are on the same fs. | |
1248 | */ | |
1249 | if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid)) | |
1250 | return false; | |
1251 | } | |
1252 | return true; | |
1253 | } | |
1254 | ||
5148626b | 1255 | /* Get a unique fsid for the layer */ |
9df085f3 | 1256 | static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path) |
5148626b | 1257 | { |
9df085f3 | 1258 | struct super_block *sb = path->mnt->mnt_sb; |
5148626b AG |
1259 | unsigned int i; |
1260 | dev_t dev; | |
1261 | int err; | |
1262 | ||
1263 | /* fsid 0 is reserved for upper fs even with non upper overlay */ | |
1264 | if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb) | |
1265 | return 0; | |
1266 | ||
1267 | for (i = 0; i < ofs->numlowerfs; i++) { | |
1268 | if (ofs->lower_fs[i].sb == sb) | |
1269 | return i + 1; | |
1270 | } | |
1271 | ||
9df085f3 AG |
1272 | if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) { |
1273 | ofs->config.index = false; | |
1274 | ofs->config.nfs_export = false; | |
1275 | pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n", | |
1276 | uuid_is_null(&sb->s_uuid) ? "null" : "conflicting", | |
1277 | path->dentry); | |
1278 | } | |
1279 | ||
5148626b AG |
1280 | err = get_anon_bdev(&dev); |
1281 | if (err) { | |
1282 | pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n"); | |
1283 | return err; | |
1284 | } | |
1285 | ||
1286 | ofs->lower_fs[ofs->numlowerfs].sb = sb; | |
1287 | ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev; | |
1288 | ofs->numlowerfs++; | |
1289 | ||
1290 | return ofs->numlowerfs; | |
1291 | } | |
1292 | ||
146d62e5 AG |
1293 | static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs, |
1294 | struct path *stack, unsigned int numlower) | |
520d7c86 MS |
1295 | { |
1296 | int err; | |
1297 | unsigned int i; | |
1298 | ||
1299 | err = -ENOMEM; | |
ad204488 | 1300 | ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer), |
520d7c86 | 1301 | GFP_KERNEL); |
ad204488 | 1302 | if (ofs->lower_layers == NULL) |
520d7c86 | 1303 | goto out; |
5148626b AG |
1304 | |
1305 | ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb), | |
1306 | GFP_KERNEL); | |
1307 | if (ofs->lower_fs == NULL) | |
1308 | goto out; | |
1309 | ||
520d7c86 MS |
1310 | for (i = 0; i < numlower; i++) { |
1311 | struct vfsmount *mnt; | |
146d62e5 | 1312 | struct inode *trap; |
5148626b | 1313 | int fsid; |
520d7c86 | 1314 | |
9df085f3 | 1315 | err = fsid = ovl_get_fsid(ofs, &stack[i]); |
5148626b | 1316 | if (err < 0) |
520d7c86 | 1317 | goto out; |
520d7c86 | 1318 | |
146d62e5 AG |
1319 | err = -EBUSY; |
1320 | if (ovl_is_inuse(stack[i].dentry)) { | |
1321 | pr_err("overlayfs: lowerdir is in-use as upperdir/workdir\n"); | |
1322 | goto out; | |
1323 | } | |
1324 | ||
1325 | err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir"); | |
1326 | if (err) | |
1327 | goto out; | |
1328 | ||
520d7c86 MS |
1329 | mnt = clone_private_mount(&stack[i]); |
1330 | err = PTR_ERR(mnt); | |
1331 | if (IS_ERR(mnt)) { | |
1332 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
146d62e5 | 1333 | iput(trap); |
520d7c86 MS |
1334 | goto out; |
1335 | } | |
5148626b | 1336 | |
520d7c86 MS |
1337 | /* |
1338 | * Make lower layers R/O. That way fchmod/fchown on lower file | |
1339 | * will fail instead of modifying lower fs. | |
1340 | */ | |
1341 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; | |
1342 | ||
146d62e5 | 1343 | ofs->lower_layers[ofs->numlower].trap = trap; |
ad204488 | 1344 | ofs->lower_layers[ofs->numlower].mnt = mnt; |
d583ed7d | 1345 | ofs->lower_layers[ofs->numlower].idx = i + 1; |
5148626b AG |
1346 | ofs->lower_layers[ofs->numlower].fsid = fsid; |
1347 | if (fsid) { | |
1348 | ofs->lower_layers[ofs->numlower].fs = | |
1349 | &ofs->lower_fs[fsid - 1]; | |
1350 | } | |
ad204488 | 1351 | ofs->numlower++; |
520d7c86 | 1352 | } |
e487d889 | 1353 | |
795939a9 AG |
1354 | /* |
1355 | * When all layers on same fs, overlay can use real inode numbers. | |
1356 | * With mount option "xino=on", mounter declares that there are enough | |
1357 | * free high bits in underlying fs to hold the unique fsid. | |
1358 | * If overlayfs does encounter underlying inodes using the high xino | |
1359 | * bits reserved for fsid, it emits a warning and uses the original | |
1360 | * inode number. | |
1361 | */ | |
1362 | if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) { | |
e487d889 | 1363 | ofs->xino_bits = 0; |
795939a9 AG |
1364 | ofs->config.xino = OVL_XINO_OFF; |
1365 | } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) { | |
1366 | /* | |
1367 | * This is a roundup of number of bits needed for numlowerfs+1 | |
1368 | * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for | |
1369 | * upper fs even with non upper overlay. | |
1370 | */ | |
1371 | BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31); | |
1372 | ofs->xino_bits = ilog2(ofs->numlowerfs) + 1; | |
1373 | } | |
1374 | ||
1375 | if (ofs->xino_bits) { | |
1376 | pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n", | |
1377 | ofs->xino_bits); | |
1378 | } | |
e487d889 | 1379 | |
520d7c86 MS |
1380 | err = 0; |
1381 | out: | |
1382 | return err; | |
1383 | } | |
1384 | ||
4155c10a | 1385 | static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, |
ad204488 | 1386 | struct ovl_fs *ofs) |
53dbb0b4 MS |
1387 | { |
1388 | int err; | |
1389 | char *lowertmp, *lower; | |
4155c10a MS |
1390 | struct path *stack = NULL; |
1391 | unsigned int stacklen, numlower = 0, i; | |
53dbb0b4 | 1392 | bool remote = false; |
4155c10a | 1393 | struct ovl_entry *oe; |
53dbb0b4 MS |
1394 | |
1395 | err = -ENOMEM; | |
ad204488 | 1396 | lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL); |
53dbb0b4 | 1397 | if (!lowertmp) |
4155c10a | 1398 | goto out_err; |
53dbb0b4 MS |
1399 | |
1400 | err = -EINVAL; | |
1401 | stacklen = ovl_split_lowerdirs(lowertmp); | |
1402 | if (stacklen > OVL_MAX_STACK) { | |
1403 | pr_err("overlayfs: too many lower directories, limit is %d\n", | |
1404 | OVL_MAX_STACK); | |
4155c10a | 1405 | goto out_err; |
ad204488 | 1406 | } else if (!ofs->config.upperdir && stacklen == 1) { |
53dbb0b4 | 1407 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
4155c10a | 1408 | goto out_err; |
f168f109 AG |
1409 | } else if (!ofs->config.upperdir && ofs->config.nfs_export && |
1410 | ofs->config.redirect_follow) { | |
1411 | pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); | |
1412 | ofs->config.nfs_export = false; | |
53dbb0b4 MS |
1413 | } |
1414 | ||
1415 | err = -ENOMEM; | |
1416 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
1417 | if (!stack) | |
4155c10a | 1418 | goto out_err; |
53dbb0b4 MS |
1419 | |
1420 | err = -EINVAL; | |
1421 | lower = lowertmp; | |
1422 | for (numlower = 0; numlower < stacklen; numlower++) { | |
ad204488 | 1423 | err = ovl_lower_dir(lower, &stack[numlower], ofs, |
53dbb0b4 MS |
1424 | &sb->s_stack_depth, &remote); |
1425 | if (err) | |
4155c10a | 1426 | goto out_err; |
53dbb0b4 MS |
1427 | |
1428 | lower = strchr(lower, '\0') + 1; | |
1429 | } | |
1430 | ||
1431 | err = -EINVAL; | |
1432 | sb->s_stack_depth++; | |
1433 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { | |
1434 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
4155c10a | 1435 | goto out_err; |
53dbb0b4 MS |
1436 | } |
1437 | ||
146d62e5 | 1438 | err = ovl_get_lower_layers(sb, ofs, stack, numlower); |
4155c10a MS |
1439 | if (err) |
1440 | goto out_err; | |
1441 | ||
1442 | err = -ENOMEM; | |
1443 | oe = ovl_alloc_entry(numlower); | |
1444 | if (!oe) | |
1445 | goto out_err; | |
1446 | ||
1447 | for (i = 0; i < numlower; i++) { | |
1448 | oe->lowerstack[i].dentry = dget(stack[i].dentry); | |
ad204488 | 1449 | oe->lowerstack[i].layer = &ofs->lower_layers[i]; |
4155c10a | 1450 | } |
53dbb0b4 MS |
1451 | |
1452 | if (remote) | |
1453 | sb->s_d_op = &ovl_reval_dentry_operations; | |
1454 | else | |
1455 | sb->s_d_op = &ovl_dentry_operations; | |
1456 | ||
53dbb0b4 | 1457 | out: |
53dbb0b4 MS |
1458 | for (i = 0; i < numlower; i++) |
1459 | path_put(&stack[i]); | |
1460 | kfree(stack); | |
4155c10a MS |
1461 | kfree(lowertmp); |
1462 | ||
1463 | return oe; | |
1464 | ||
1465 | out_err: | |
1466 | oe = ERR_PTR(err); | |
53dbb0b4 MS |
1467 | goto out; |
1468 | } | |
1469 | ||
146d62e5 AG |
1470 | /* |
1471 | * Check if this layer root is a descendant of: | |
1472 | * - another layer of this overlayfs instance | |
1473 | * - upper/work dir of any overlayfs instance | |
146d62e5 AG |
1474 | */ |
1475 | static int ovl_check_layer(struct super_block *sb, struct dentry *dentry, | |
1476 | const char *name) | |
1477 | { | |
9179c21d | 1478 | struct dentry *next = dentry, *parent; |
146d62e5 AG |
1479 | int err = 0; |
1480 | ||
9179c21d | 1481 | if (!dentry) |
146d62e5 AG |
1482 | return 0; |
1483 | ||
9179c21d MS |
1484 | parent = dget_parent(next); |
1485 | ||
1486 | /* Walk back ancestors to root (inclusive) looking for traps */ | |
1487 | while (!err && parent != next) { | |
146d62e5 AG |
1488 | if (ovl_is_inuse(parent)) { |
1489 | err = -EBUSY; | |
1490 | pr_err("overlayfs: %s path overlapping in-use upperdir/workdir\n", | |
1491 | name); | |
1492 | } else if (ovl_lookup_trap_inode(sb, parent)) { | |
1493 | err = -ELOOP; | |
1494 | pr_err("overlayfs: overlapping %s path\n", name); | |
1495 | } | |
146d62e5 | 1496 | next = parent; |
9179c21d MS |
1497 | parent = dget_parent(next); |
1498 | dput(next); | |
146d62e5 AG |
1499 | } |
1500 | ||
9179c21d | 1501 | dput(parent); |
146d62e5 AG |
1502 | |
1503 | return err; | |
1504 | } | |
1505 | ||
1506 | /* | |
1507 | * Check if any of the layers or work dirs overlap. | |
1508 | */ | |
1509 | static int ovl_check_overlapping_layers(struct super_block *sb, | |
1510 | struct ovl_fs *ofs) | |
1511 | { | |
1512 | int i, err; | |
1513 | ||
1514 | if (ofs->upper_mnt) { | |
1515 | err = ovl_check_layer(sb, ofs->upper_mnt->mnt_root, "upperdir"); | |
1516 | if (err) | |
1517 | return err; | |
1518 | ||
1519 | /* | |
1520 | * Checking workbasedir avoids hitting ovl_is_inuse(parent) of | |
1521 | * this instance and covers overlapping work and index dirs, | |
1522 | * unless work or index dir have been moved since created inside | |
1523 | * workbasedir. In that case, we already have their traps in | |
1524 | * inode cache and we will catch that case on lookup. | |
1525 | */ | |
1526 | err = ovl_check_layer(sb, ofs->workbasedir, "workdir"); | |
1527 | if (err) | |
1528 | return err; | |
1529 | } | |
1530 | ||
1531 | for (i = 0; i < ofs->numlower; i++) { | |
1532 | err = ovl_check_layer(sb, ofs->lower_layers[i].mnt->mnt_root, | |
1533 | "lowerdir"); | |
1534 | if (err) | |
1535 | return err; | |
1536 | } | |
1537 | ||
1538 | return 0; | |
1539 | } | |
1540 | ||
e9be9d5e MS |
1541 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
1542 | { | |
33006cdf | 1543 | struct path upperpath = { }; |
e9be9d5e | 1544 | struct dentry *root_dentry; |
4155c10a | 1545 | struct ovl_entry *oe; |
ad204488 | 1546 | struct ovl_fs *ofs; |
51f8f3c4 | 1547 | struct cred *cred; |
e9be9d5e MS |
1548 | int err; |
1549 | ||
f45827e8 | 1550 | err = -ENOMEM; |
ad204488 MS |
1551 | ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
1552 | if (!ofs) | |
e9be9d5e MS |
1553 | goto out; |
1554 | ||
ad204488 | 1555 | ofs->creator_cred = cred = prepare_creds(); |
c6fe6254 MS |
1556 | if (!cred) |
1557 | goto out_err; | |
1558 | ||
ad204488 | 1559 | ofs->config.index = ovl_index_def; |
f168f109 | 1560 | ofs->config.nfs_export = ovl_nfs_export_def; |
795939a9 | 1561 | ofs->config.xino = ovl_xino_def(); |
d5791044 | 1562 | ofs->config.metacopy = ovl_metacopy_def; |
ad204488 | 1563 | err = ovl_parse_opt((char *) data, &ofs->config); |
f45827e8 | 1564 | if (err) |
a9075cdb | 1565 | goto out_err; |
f45827e8 | 1566 | |
e9be9d5e | 1567 | err = -EINVAL; |
ad204488 | 1568 | if (!ofs->config.lowerdir) { |
07f2af7b KK |
1569 | if (!silent) |
1570 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
a9075cdb | 1571 | goto out_err; |
e9be9d5e MS |
1572 | } |
1573 | ||
53a08cb9 | 1574 | sb->s_stack_depth = 0; |
cf9a6784 | 1575 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
e487d889 | 1576 | /* Assume underlaying fs uses 32bit inodes unless proven otherwise */ |
795939a9 AG |
1577 | if (ofs->config.xino != OVL_XINO_OFF) |
1578 | ofs->xino_bits = BITS_PER_LONG - 32; | |
1579 | ||
146d62e5 AG |
1580 | /* alloc/destroy_inode needed for setting up traps in inode cache */ |
1581 | sb->s_op = &ovl_super_operations; | |
1582 | ||
ad204488 MS |
1583 | if (ofs->config.upperdir) { |
1584 | if (!ofs->config.workdir) { | |
53a08cb9 | 1585 | pr_err("overlayfs: missing 'workdir'\n"); |
a9075cdb | 1586 | goto out_err; |
53a08cb9 | 1587 | } |
e9be9d5e | 1588 | |
146d62e5 | 1589 | err = ovl_get_upper(sb, ofs, &upperpath); |
53a08cb9 | 1590 | if (err) |
a9075cdb | 1591 | goto out_err; |
2cac0c00 | 1592 | |
146d62e5 | 1593 | err = ovl_get_workdir(sb, ofs, &upperpath); |
8ed61dc3 | 1594 | if (err) |
a9075cdb | 1595 | goto out_err; |
c6fe6254 | 1596 | |
ad204488 | 1597 | if (!ofs->workdir) |
1751e8a6 | 1598 | sb->s_flags |= SB_RDONLY; |
6e88256e | 1599 | |
ad204488 MS |
1600 | sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth; |
1601 | sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran; | |
c6fe6254 | 1602 | |
e9be9d5e | 1603 | } |
ad204488 | 1604 | oe = ovl_get_lowerstack(sb, ofs); |
4155c10a MS |
1605 | err = PTR_ERR(oe); |
1606 | if (IS_ERR(oe)) | |
a9075cdb | 1607 | goto out_err; |
e9be9d5e | 1608 | |
71cbad7e | 1609 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
ad204488 | 1610 | if (!ofs->upper_mnt) |
1751e8a6 | 1611 | sb->s_flags |= SB_RDONLY; |
e9be9d5e | 1612 | |
ad204488 | 1613 | if (!(ovl_force_readonly(ofs)) && ofs->config.index) { |
146d62e5 | 1614 | err = ovl_get_indexdir(sb, ofs, oe, &upperpath); |
54fb347e | 1615 | if (err) |
4155c10a | 1616 | goto out_free_oe; |
6e88256e | 1617 | |
972d0093 AG |
1618 | /* Force r/o mount with no index dir */ |
1619 | if (!ofs->indexdir) { | |
1620 | dput(ofs->workdir); | |
1621 | ofs->workdir = NULL; | |
1751e8a6 | 1622 | sb->s_flags |= SB_RDONLY; |
972d0093 AG |
1623 | } |
1624 | ||
02bcd157 AG |
1625 | } |
1626 | ||
146d62e5 AG |
1627 | err = ovl_check_overlapping_layers(sb, ofs); |
1628 | if (err) | |
1629 | goto out_free_oe; | |
1630 | ||
972d0093 | 1631 | /* Show index=off in /proc/mounts for forced r/o mount */ |
f168f109 | 1632 | if (!ofs->indexdir) { |
ad204488 | 1633 | ofs->config.index = false; |
f168f109 AG |
1634 | if (ofs->upper_mnt && ofs->config.nfs_export) { |
1635 | pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n"); | |
1636 | ofs->config.nfs_export = false; | |
1637 | } | |
1638 | } | |
02bcd157 | 1639 | |
d5791044 VG |
1640 | if (ofs->config.metacopy && ofs->config.nfs_export) { |
1641 | pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); | |
1642 | ofs->config.nfs_export = false; | |
1643 | } | |
1644 | ||
8383f174 AG |
1645 | if (ofs->config.nfs_export) |
1646 | sb->s_export_op = &ovl_export_operations; | |
1647 | ||
51f8f3c4 KK |
1648 | /* Never override disk quota limits or use reserved space */ |
1649 | cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); | |
1650 | ||
655042cc | 1651 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
655042cc | 1652 | sb->s_xattr = ovl_xattr_handlers; |
ad204488 | 1653 | sb->s_fs_info = ofs; |
de2a4a50 | 1654 | sb->s_flags |= SB_POSIXACL; |
655042cc | 1655 | |
c6fe6254 | 1656 | err = -ENOMEM; |
ca4c8a3a | 1657 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
e9be9d5e | 1658 | if (!root_dentry) |
4155c10a | 1659 | goto out_free_oe; |
e9be9d5e | 1660 | |
c62520a8 AG |
1661 | root_dentry->d_fsdata = oe; |
1662 | ||
e9be9d5e | 1663 | mntput(upperpath.mnt); |
f3a15685 | 1664 | if (upperpath.dentry) { |
c62520a8 | 1665 | ovl_dentry_set_upper_alias(root_dentry); |
13c72075 MS |
1666 | if (ovl_is_impuredir(upperpath.dentry)) |
1667 | ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); | |
f3a15685 | 1668 | } |
e9be9d5e | 1669 | |
b79e05aa AG |
1670 | /* Root is always merge -> can have whiteouts */ |
1671 | ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); | |
2ca3c148 | 1672 | ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry); |
0c288874 | 1673 | ovl_set_upperdata(d_inode(root_dentry)); |
09d8b586 | 1674 | ovl_inode_init(d_inode(root_dentry), upperpath.dentry, |
2664bd08 | 1675 | ovl_dentry_lower(root_dentry), NULL); |
ed06e069 | 1676 | |
e9be9d5e | 1677 | sb->s_root = root_dentry; |
e9be9d5e MS |
1678 | |
1679 | return 0; | |
1680 | ||
4155c10a MS |
1681 | out_free_oe: |
1682 | ovl_entry_stack_free(oe); | |
b9343632 | 1683 | kfree(oe); |
4155c10a | 1684 | out_err: |
e9be9d5e | 1685 | path_put(&upperpath); |
ad204488 | 1686 | ovl_free_fs(ofs); |
e9be9d5e MS |
1687 | out: |
1688 | return err; | |
1689 | } | |
1690 | ||
1691 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1692 | const char *dev_name, void *raw_data) | |
1693 | { | |
1694 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1695 | } | |
1696 | ||
1697 | static struct file_system_type ovl_fs_type = { | |
1698 | .owner = THIS_MODULE, | |
ef94b186 | 1699 | .name = "overlay", |
e9be9d5e MS |
1700 | .mount = ovl_mount, |
1701 | .kill_sb = kill_anon_super, | |
1702 | }; | |
ef94b186 | 1703 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e | 1704 | |
13cf199d AG |
1705 | static void ovl_inode_init_once(void *foo) |
1706 | { | |
1707 | struct ovl_inode *oi = foo; | |
1708 | ||
1709 | inode_init_once(&oi->vfs_inode); | |
1710 | } | |
1711 | ||
e9be9d5e MS |
1712 | static int __init ovl_init(void) |
1713 | { | |
13cf199d AG |
1714 | int err; |
1715 | ||
1716 | ovl_inode_cachep = kmem_cache_create("ovl_inode", | |
1717 | sizeof(struct ovl_inode), 0, | |
1718 | (SLAB_RECLAIM_ACCOUNT| | |
1719 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), | |
1720 | ovl_inode_init_once); | |
1721 | if (ovl_inode_cachep == NULL) | |
1722 | return -ENOMEM; | |
1723 | ||
1724 | err = register_filesystem(&ovl_fs_type); | |
1725 | if (err) | |
1726 | kmem_cache_destroy(ovl_inode_cachep); | |
1727 | ||
1728 | return err; | |
e9be9d5e MS |
1729 | } |
1730 | ||
1731 | static void __exit ovl_exit(void) | |
1732 | { | |
1733 | unregister_filesystem(&ovl_fs_type); | |
13cf199d AG |
1734 | |
1735 | /* | |
1736 | * Make sure all delayed rcu free inodes are flushed before we | |
1737 | * destroy cache. | |
1738 | */ | |
1739 | rcu_barrier(); | |
1740 | kmem_cache_destroy(ovl_inode_cachep); | |
1741 | ||
e9be9d5e MS |
1742 | } |
1743 | ||
1744 | module_init(ovl_init); | |
1745 | module_exit(ovl_exit); |