]>
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> |
e9be9d5e MS |
20 | #include "overlayfs.h" |
21 | ||
22 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
23 | MODULE_DESCRIPTION("Overlay filesystem"); | |
24 | MODULE_LICENSE("GPL"); | |
25 | ||
e9be9d5e MS |
26 | |
27 | struct ovl_dir_cache; | |
28 | ||
a78d9f0d MS |
29 | #define OVL_MAX_STACK 500 |
30 | ||
688ea0e5 MS |
31 | static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); |
32 | module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); | |
33 | MODULE_PARM_DESC(ovl_redirect_dir_def, | |
34 | "Default to on or off for the redirect_dir feature"); | |
e9be9d5e | 35 | |
438c84c2 MS |
36 | static bool ovl_redirect_always_follow = |
37 | IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); | |
38 | module_param_named(redirect_always_follow, ovl_redirect_always_follow, | |
39 | bool, 0644); | |
40 | MODULE_PARM_DESC(ovl_redirect_always_follow, | |
41 | "Follow redirects even if redirect_dir feature is turned off"); | |
42 | ||
02bcd157 AG |
43 | static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); |
44 | module_param_named(index, ovl_index_def, bool, 0644); | |
45 | MODULE_PARM_DESC(ovl_index_def, | |
46 | "Default to on or off for the inodes index feature"); | |
47 | ||
4155c10a MS |
48 | static void ovl_entry_stack_free(struct ovl_entry *oe) |
49 | { | |
50 | unsigned int i; | |
51 | ||
52 | for (i = 0; i < oe->numlower; i++) | |
53 | dput(oe->lowerstack[i].dentry); | |
54 | } | |
55 | ||
e9be9d5e MS |
56 | static void ovl_dentry_release(struct dentry *dentry) |
57 | { | |
58 | struct ovl_entry *oe = dentry->d_fsdata; | |
59 | ||
60 | if (oe) { | |
4155c10a | 61 | ovl_entry_stack_free(oe); |
e9be9d5e MS |
62 | kfree_rcu(oe, rcu); |
63 | } | |
64 | } | |
65 | ||
b0990fbb AG |
66 | static int ovl_check_append_only(struct inode *inode, int flag) |
67 | { | |
68 | /* | |
69 | * This test was moot in vfs may_open() because overlay inode does | |
70 | * not have the S_APPEND flag, so re-check on real upper inode | |
71 | */ | |
72 | if (IS_APPEND(inode)) { | |
73 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) | |
74 | return -EPERM; | |
75 | if (flag & O_TRUNC) | |
76 | return -EPERM; | |
77 | } | |
78 | ||
79 | return 0; | |
80 | } | |
81 | ||
2d902671 MS |
82 | static struct dentry *ovl_d_real(struct dentry *dentry, |
83 | const struct inode *inode, | |
495e6429 | 84 | unsigned int open_flags, unsigned int flags) |
d101a125 MS |
85 | { |
86 | struct dentry *real; | |
b0990fbb | 87 | int err; |
d101a125 | 88 | |
cd91304e MS |
89 | if (flags & D_REAL_UPPER) |
90 | return ovl_dentry_upper(dentry); | |
91 | ||
ca4c8a3a | 92 | if (!d_is_reg(dentry)) { |
d101a125 MS |
93 | if (!inode || inode == d_inode(dentry)) |
94 | return dentry; | |
95 | goto bug; | |
96 | } | |
97 | ||
2d902671 | 98 | if (open_flags) { |
b0990fbb | 99 | err = ovl_open_maybe_copy_up(dentry, open_flags); |
2d902671 MS |
100 | if (err) |
101 | return ERR_PTR(err); | |
102 | } | |
103 | ||
d101a125 | 104 | real = ovl_dentry_upper(dentry); |
b0990fbb AG |
105 | if (real && (!inode || inode == d_inode(real))) { |
106 | if (!inode) { | |
107 | err = ovl_check_append_only(d_inode(real), open_flags); | |
108 | if (err) | |
109 | return ERR_PTR(err); | |
110 | } | |
d101a125 | 111 | return real; |
b0990fbb | 112 | } |
d101a125 MS |
113 | |
114 | real = ovl_dentry_lower(dentry); | |
115 | if (!real) | |
116 | goto bug; | |
117 | ||
c4fcfc16 | 118 | /* Handle recursion */ |
495e6429 | 119 | real = d_real(real, inode, open_flags, 0); |
c4fcfc16 | 120 | |
d101a125 MS |
121 | if (!inode || inode == d_inode(real)) |
122 | return real; | |
d101a125 | 123 | bug: |
656189d2 | 124 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
d101a125 MS |
125 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
126 | return dentry; | |
127 | } | |
128 | ||
7c03b5d4 MS |
129 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
130 | { | |
131 | struct ovl_entry *oe = dentry->d_fsdata; | |
132 | unsigned int i; | |
133 | int ret = 1; | |
134 | ||
135 | for (i = 0; i < oe->numlower; i++) { | |
136 | struct dentry *d = oe->lowerstack[i].dentry; | |
137 | ||
138 | if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
139 | ret = d->d_op->d_revalidate(d, flags); | |
140 | if (ret < 0) | |
141 | return ret; | |
142 | if (!ret) { | |
143 | if (!(flags & LOOKUP_RCU)) | |
144 | d_invalidate(d); | |
145 | return -ESTALE; | |
146 | } | |
147 | } | |
148 | } | |
149 | return 1; | |
150 | } | |
151 | ||
152 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
153 | { | |
154 | struct ovl_entry *oe = dentry->d_fsdata; | |
155 | unsigned int i; | |
156 | int ret = 1; | |
157 | ||
158 | for (i = 0; i < oe->numlower; i++) { | |
159 | struct dentry *d = oe->lowerstack[i].dentry; | |
160 | ||
161 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { | |
162 | ret = d->d_op->d_weak_revalidate(d, flags); | |
163 | if (ret <= 0) | |
164 | break; | |
165 | } | |
166 | } | |
167 | return ret; | |
168 | } | |
169 | ||
e9be9d5e MS |
170 | static const struct dentry_operations ovl_dentry_operations = { |
171 | .d_release = ovl_dentry_release, | |
d101a125 | 172 | .d_real = ovl_d_real, |
e9be9d5e MS |
173 | }; |
174 | ||
7c03b5d4 MS |
175 | static const struct dentry_operations ovl_reval_dentry_operations = { |
176 | .d_release = ovl_dentry_release, | |
d101a125 | 177 | .d_real = ovl_d_real, |
7c03b5d4 MS |
178 | .d_revalidate = ovl_dentry_revalidate, |
179 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
180 | }; | |
181 | ||
13cf199d AG |
182 | static struct kmem_cache *ovl_inode_cachep; |
183 | ||
184 | static struct inode *ovl_alloc_inode(struct super_block *sb) | |
185 | { | |
186 | struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL); | |
187 | ||
b3885bd6 HN |
188 | if (!oi) |
189 | return NULL; | |
190 | ||
04a01ac7 | 191 | oi->cache = NULL; |
cf31c463 | 192 | oi->redirect = NULL; |
04a01ac7 | 193 | oi->version = 0; |
13c72075 | 194 | oi->flags = 0; |
09d8b586 | 195 | oi->__upperdentry = NULL; |
25b7713a | 196 | oi->lower = NULL; |
a015dafc | 197 | mutex_init(&oi->lock); |
25b7713a | 198 | |
13cf199d AG |
199 | return &oi->vfs_inode; |
200 | } | |
201 | ||
202 | static void ovl_i_callback(struct rcu_head *head) | |
203 | { | |
204 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
205 | ||
206 | kmem_cache_free(ovl_inode_cachep, OVL_I(inode)); | |
207 | } | |
208 | ||
209 | static void ovl_destroy_inode(struct inode *inode) | |
210 | { | |
09d8b586 MS |
211 | struct ovl_inode *oi = OVL_I(inode); |
212 | ||
213 | dput(oi->__upperdentry); | |
cf31c463 | 214 | kfree(oi->redirect); |
4edb83bb | 215 | ovl_dir_cache_free(inode); |
a015dafc | 216 | mutex_destroy(&oi->lock); |
09d8b586 | 217 | |
13cf199d AG |
218 | call_rcu(&inode->i_rcu, ovl_i_callback); |
219 | } | |
220 | ||
ad204488 | 221 | static void ovl_free_fs(struct ovl_fs *ofs) |
e9be9d5e | 222 | { |
dd662667 | 223 | unsigned i; |
e9be9d5e | 224 | |
ad204488 MS |
225 | dput(ofs->indexdir); |
226 | dput(ofs->workdir); | |
227 | if (ofs->workdir_locked) | |
228 | ovl_inuse_unlock(ofs->workbasedir); | |
229 | dput(ofs->workbasedir); | |
230 | if (ofs->upperdir_locked) | |
231 | ovl_inuse_unlock(ofs->upper_mnt->mnt_root); | |
232 | mntput(ofs->upper_mnt); | |
233 | for (i = 0; i < ofs->numlower; i++) { | |
234 | mntput(ofs->lower_layers[i].mnt); | |
235 | free_anon_bdev(ofs->lower_layers[i].pseudo_dev); | |
2a9c6d06 | 236 | } |
ad204488 MS |
237 | kfree(ofs->lower_layers); |
238 | ||
239 | kfree(ofs->config.lowerdir); | |
240 | kfree(ofs->config.upperdir); | |
241 | kfree(ofs->config.workdir); | |
438c84c2 | 242 | kfree(ofs->config.redirect_mode); |
ad204488 MS |
243 | if (ofs->creator_cred) |
244 | put_cred(ofs->creator_cred); | |
245 | kfree(ofs); | |
e9be9d5e MS |
246 | } |
247 | ||
a9075cdb MS |
248 | static void ovl_put_super(struct super_block *sb) |
249 | { | |
250 | struct ovl_fs *ofs = sb->s_fs_info; | |
251 | ||
252 | ovl_free_fs(ofs); | |
253 | } | |
254 | ||
e8d4bfe3 | 255 | /* Sync real dirty inodes in upper filesystem (if it exists) */ |
e593b2bf AG |
256 | static int ovl_sync_fs(struct super_block *sb, int wait) |
257 | { | |
ad204488 | 258 | struct ovl_fs *ofs = sb->s_fs_info; |
e593b2bf AG |
259 | struct super_block *upper_sb; |
260 | int ret; | |
261 | ||
ad204488 | 262 | if (!ofs->upper_mnt) |
e593b2bf | 263 | return 0; |
e8d4bfe3 CX |
264 | |
265 | /* | |
266 | * If this is a sync(2) call or an emergency sync, all the super blocks | |
267 | * will be iterated, including upper_sb, so no need to do anything. | |
268 | * | |
269 | * If this is a syncfs(2) call, then we do need to call | |
270 | * sync_filesystem() on upper_sb, but enough if we do it when being | |
271 | * called with wait == 1. | |
272 | */ | |
273 | if (!wait) | |
e593b2bf AG |
274 | return 0; |
275 | ||
e8d4bfe3 CX |
276 | upper_sb = ofs->upper_mnt->mnt_sb; |
277 | ||
e593b2bf | 278 | down_read(&upper_sb->s_umount); |
e8d4bfe3 | 279 | ret = sync_filesystem(upper_sb); |
e593b2bf | 280 | up_read(&upper_sb->s_umount); |
e8d4bfe3 | 281 | |
e593b2bf AG |
282 | return ret; |
283 | } | |
284 | ||
cc259639 AW |
285 | /** |
286 | * ovl_statfs | |
287 | * @sb: The overlayfs super block | |
288 | * @buf: The struct kstatfs to fill in with stats | |
289 | * | |
290 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 291 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
292 | */ |
293 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
294 | { | |
295 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
296 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
297 | struct path path; | |
298 | int err; | |
299 | ||
4ebc5818 | 300 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
301 | |
302 | err = vfs_statfs(&path, buf); | |
303 | if (!err) { | |
6b2d5fe4 | 304 | buf->f_namelen = ofs->namelen; |
cc259639 AW |
305 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
306 | } | |
307 | ||
308 | return err; | |
309 | } | |
310 | ||
02bcd157 | 311 | /* Will this overlay be forced to mount/remount ro? */ |
ad204488 | 312 | static bool ovl_force_readonly(struct ovl_fs *ofs) |
02bcd157 | 313 | { |
ad204488 | 314 | return (!ofs->upper_mnt || !ofs->workdir); |
02bcd157 AG |
315 | } |
316 | ||
438c84c2 MS |
317 | static const char *ovl_redirect_mode_def(void) |
318 | { | |
319 | return ovl_redirect_dir_def ? "on" : "off"; | |
320 | } | |
321 | ||
f45827e8 EZ |
322 | /** |
323 | * ovl_show_options | |
324 | * | |
325 | * Prints the mount options for a given superblock. | |
326 | * Returns zero; does not fail. | |
327 | */ | |
328 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
329 | { | |
330 | struct super_block *sb = dentry->d_sb; | |
ad204488 | 331 | struct ovl_fs *ofs = sb->s_fs_info; |
f45827e8 | 332 | |
ad204488 MS |
333 | seq_show_option(m, "lowerdir", ofs->config.lowerdir); |
334 | if (ofs->config.upperdir) { | |
335 | seq_show_option(m, "upperdir", ofs->config.upperdir); | |
336 | seq_show_option(m, "workdir", ofs->config.workdir); | |
53a08cb9 | 337 | } |
ad204488 | 338 | if (ofs->config.default_permissions) |
8d3095f4 | 339 | seq_puts(m, ",default_permissions"); |
438c84c2 MS |
340 | if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) |
341 | seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); | |
ad204488 | 342 | if (ofs->config.index != ovl_index_def) |
438c84c2 | 343 | seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); |
f45827e8 EZ |
344 | return 0; |
345 | } | |
346 | ||
3cdf6fe9 SL |
347 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
348 | { | |
ad204488 | 349 | struct ovl_fs *ofs = sb->s_fs_info; |
3cdf6fe9 | 350 | |
1751e8a6 | 351 | if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) |
3cdf6fe9 SL |
352 | return -EROFS; |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
e9be9d5e | 357 | static const struct super_operations ovl_super_operations = { |
13cf199d AG |
358 | .alloc_inode = ovl_alloc_inode, |
359 | .destroy_inode = ovl_destroy_inode, | |
360 | .drop_inode = generic_delete_inode, | |
e9be9d5e | 361 | .put_super = ovl_put_super, |
e593b2bf | 362 | .sync_fs = ovl_sync_fs, |
cc259639 | 363 | .statfs = ovl_statfs, |
f45827e8 | 364 | .show_options = ovl_show_options, |
3cdf6fe9 | 365 | .remount_fs = ovl_remount, |
e9be9d5e MS |
366 | }; |
367 | ||
368 | enum { | |
369 | OPT_LOWERDIR, | |
370 | OPT_UPPERDIR, | |
371 | OPT_WORKDIR, | |
8d3095f4 | 372 | OPT_DEFAULT_PERMISSIONS, |
438c84c2 | 373 | OPT_REDIRECT_DIR, |
02bcd157 AG |
374 | OPT_INDEX_ON, |
375 | OPT_INDEX_OFF, | |
e9be9d5e MS |
376 | OPT_ERR, |
377 | }; | |
378 | ||
379 | static const match_table_t ovl_tokens = { | |
380 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
381 | {OPT_UPPERDIR, "upperdir=%s"}, | |
382 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 383 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
438c84c2 | 384 | {OPT_REDIRECT_DIR, "redirect_dir=%s"}, |
02bcd157 AG |
385 | {OPT_INDEX_ON, "index=on"}, |
386 | {OPT_INDEX_OFF, "index=off"}, | |
e9be9d5e MS |
387 | {OPT_ERR, NULL} |
388 | }; | |
389 | ||
91c77947 MS |
390 | static char *ovl_next_opt(char **s) |
391 | { | |
392 | char *sbegin = *s; | |
393 | char *p; | |
394 | ||
395 | if (sbegin == NULL) | |
396 | return NULL; | |
397 | ||
398 | for (p = sbegin; *p; p++) { | |
399 | if (*p == '\\') { | |
400 | p++; | |
401 | if (!*p) | |
402 | break; | |
403 | } else if (*p == ',') { | |
404 | *p = '\0'; | |
405 | *s = p + 1; | |
406 | return sbegin; | |
407 | } | |
408 | } | |
409 | *s = NULL; | |
410 | return sbegin; | |
411 | } | |
412 | ||
438c84c2 MS |
413 | static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) |
414 | { | |
415 | if (strcmp(mode, "on") == 0) { | |
416 | config->redirect_dir = true; | |
417 | /* | |
418 | * Does not make sense to have redirect creation without | |
419 | * redirect following. | |
420 | */ | |
421 | config->redirect_follow = true; | |
422 | } else if (strcmp(mode, "follow") == 0) { | |
423 | config->redirect_follow = true; | |
424 | } else if (strcmp(mode, "off") == 0) { | |
425 | if (ovl_redirect_always_follow) | |
426 | config->redirect_follow = true; | |
427 | } else if (strcmp(mode, "nofollow") != 0) { | |
428 | pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n", | |
429 | mode); | |
430 | return -EINVAL; | |
431 | } | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
e9be9d5e MS |
436 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
437 | { | |
438 | char *p; | |
439 | ||
438c84c2 MS |
440 | config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); |
441 | if (!config->redirect_mode) | |
442 | return -ENOMEM; | |
443 | ||
91c77947 | 444 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
445 | int token; |
446 | substring_t args[MAX_OPT_ARGS]; | |
447 | ||
448 | if (!*p) | |
449 | continue; | |
450 | ||
451 | token = match_token(p, ovl_tokens, args); | |
452 | switch (token) { | |
453 | case OPT_UPPERDIR: | |
454 | kfree(config->upperdir); | |
455 | config->upperdir = match_strdup(&args[0]); | |
456 | if (!config->upperdir) | |
457 | return -ENOMEM; | |
458 | break; | |
459 | ||
460 | case OPT_LOWERDIR: | |
461 | kfree(config->lowerdir); | |
462 | config->lowerdir = match_strdup(&args[0]); | |
463 | if (!config->lowerdir) | |
464 | return -ENOMEM; | |
465 | break; | |
466 | ||
467 | case OPT_WORKDIR: | |
468 | kfree(config->workdir); | |
469 | config->workdir = match_strdup(&args[0]); | |
470 | if (!config->workdir) | |
471 | return -ENOMEM; | |
472 | break; | |
473 | ||
8d3095f4 MS |
474 | case OPT_DEFAULT_PERMISSIONS: |
475 | config->default_permissions = true; | |
476 | break; | |
477 | ||
438c84c2 MS |
478 | case OPT_REDIRECT_DIR: |
479 | kfree(config->redirect_mode); | |
480 | config->redirect_mode = match_strdup(&args[0]); | |
481 | if (!config->redirect_mode) | |
482 | return -ENOMEM; | |
a6c60655 MS |
483 | break; |
484 | ||
02bcd157 AG |
485 | case OPT_INDEX_ON: |
486 | config->index = true; | |
487 | break; | |
488 | ||
489 | case OPT_INDEX_OFF: | |
490 | config->index = false; | |
491 | break; | |
492 | ||
e9be9d5e | 493 | default: |
bead55ef | 494 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
495 | return -EINVAL; |
496 | } | |
497 | } | |
71cbad7e | 498 | |
499 | /* Workdir is useless in non-upper mount */ | |
500 | if (!config->upperdir && config->workdir) { | |
501 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
502 | config->workdir); | |
503 | kfree(config->workdir); | |
504 | config->workdir = NULL; | |
505 | } | |
506 | ||
438c84c2 | 507 | return ovl_parse_redirect_mode(config, config->redirect_mode); |
e9be9d5e MS |
508 | } |
509 | ||
510 | #define OVL_WORKDIR_NAME "work" | |
02bcd157 | 511 | #define OVL_INDEXDIR_NAME "index" |
e9be9d5e | 512 | |
ad204488 | 513 | static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, |
6b8aa129 | 514 | const char *name, bool persist) |
e9be9d5e | 515 | { |
ad204488 MS |
516 | struct inode *dir = ofs->workbasedir->d_inode; |
517 | struct vfsmount *mnt = ofs->upper_mnt; | |
e9be9d5e MS |
518 | struct dentry *work; |
519 | int err; | |
520 | bool retried = false; | |
6b8aa129 | 521 | bool locked = false; |
e9be9d5e MS |
522 | |
523 | err = mnt_want_write(mnt); | |
524 | if (err) | |
6b8aa129 | 525 | goto out_err; |
e9be9d5e | 526 | |
5955102c | 527 | inode_lock_nested(dir, I_MUTEX_PARENT); |
6b8aa129 AG |
528 | locked = true; |
529 | ||
e9be9d5e | 530 | retry: |
ad204488 | 531 | work = lookup_one_len(name, ofs->workbasedir, strlen(name)); |
e9be9d5e MS |
532 | |
533 | if (!IS_ERR(work)) { | |
c11b9fdd MS |
534 | struct iattr attr = { |
535 | .ia_valid = ATTR_MODE, | |
32a3d848 | 536 | .ia_mode = S_IFDIR | 0, |
c11b9fdd | 537 | }; |
e9be9d5e MS |
538 | |
539 | if (work->d_inode) { | |
540 | err = -EEXIST; | |
541 | if (retried) | |
542 | goto out_dput; | |
543 | ||
6b8aa129 AG |
544 | if (persist) |
545 | goto out_unlock; | |
546 | ||
e9be9d5e | 547 | retried = true; |
eea2fb48 | 548 | ovl_workdir_cleanup(dir, mnt, work, 0); |
e9be9d5e MS |
549 | dput(work); |
550 | goto retry; | |
551 | } | |
552 | ||
32a3d848 AV |
553 | err = ovl_create_real(dir, work, |
554 | &(struct cattr){.mode = S_IFDIR | 0}, | |
555 | NULL, true); | |
e9be9d5e MS |
556 | if (err) |
557 | goto out_dput; | |
c11b9fdd | 558 | |
cb348edb MS |
559 | /* |
560 | * Try to remove POSIX ACL xattrs from workdir. We are good if: | |
561 | * | |
562 | * a) success (there was a POSIX ACL xattr and was removed) | |
563 | * b) -ENODATA (there was no POSIX ACL xattr) | |
564 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) | |
565 | * | |
566 | * There are various other error values that could effectively | |
567 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned | |
568 | * if the xattr name is too long), but the set of filesystems | |
569 | * allowed as upper are limited to "normal" ones, where checking | |
570 | * for the above two errors is sufficient. | |
571 | */ | |
c11b9fdd | 572 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
e1ff3dd1 | 573 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
574 | goto out_dput; |
575 | ||
576 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); | |
e1ff3dd1 | 577 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
578 | goto out_dput; |
579 | ||
580 | /* Clear any inherited mode bits */ | |
581 | inode_lock(work->d_inode); | |
582 | err = notify_change(work, &attr, NULL); | |
583 | inode_unlock(work->d_inode); | |
584 | if (err) | |
585 | goto out_dput; | |
6b8aa129 AG |
586 | } else { |
587 | err = PTR_ERR(work); | |
588 | goto out_err; | |
e9be9d5e MS |
589 | } |
590 | out_unlock: | |
e9be9d5e | 591 | mnt_drop_write(mnt); |
6b8aa129 AG |
592 | if (locked) |
593 | inode_unlock(dir); | |
e9be9d5e MS |
594 | |
595 | return work; | |
596 | ||
597 | out_dput: | |
598 | dput(work); | |
6b8aa129 AG |
599 | out_err: |
600 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", | |
ad204488 | 601 | ofs->config.workdir, name, -err); |
6b8aa129 | 602 | work = NULL; |
e9be9d5e MS |
603 | goto out_unlock; |
604 | } | |
605 | ||
91c77947 MS |
606 | static void ovl_unescape(char *s) |
607 | { | |
608 | char *d = s; | |
609 | ||
610 | for (;; s++, d++) { | |
611 | if (*s == '\\') | |
612 | s++; | |
613 | *d = *s; | |
614 | if (!*s) | |
615 | break; | |
616 | } | |
617 | } | |
618 | ||
ab508822 MS |
619 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
620 | { | |
a78d9f0d | 621 | int err = -EINVAL; |
ab508822 | 622 | |
a78d9f0d MS |
623 | if (!*name) { |
624 | pr_err("overlayfs: empty lowerdir\n"); | |
625 | goto out; | |
626 | } | |
ab508822 MS |
627 | err = kern_path(name, LOOKUP_FOLLOW, path); |
628 | if (err) { | |
629 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
630 | goto out; | |
631 | } | |
632 | err = -EINVAL; | |
7c03b5d4 | 633 | if (ovl_dentry_weird(path->dentry)) { |
ab508822 MS |
634 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
635 | goto out_put; | |
636 | } | |
2b8c30e9 | 637 | if (!d_is_dir(path->dentry)) { |
ab508822 MS |
638 | pr_err("overlayfs: '%s' not a directory\n", name); |
639 | goto out_put; | |
640 | } | |
641 | return 0; | |
642 | ||
643 | out_put: | |
8aafcb59 | 644 | path_put_init(path); |
ab508822 MS |
645 | out: |
646 | return err; | |
647 | } | |
648 | ||
649 | static int ovl_mount_dir(const char *name, struct path *path) | |
650 | { | |
651 | int err = -ENOMEM; | |
652 | char *tmp = kstrdup(name, GFP_KERNEL); | |
653 | ||
654 | if (tmp) { | |
655 | ovl_unescape(tmp); | |
656 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 MS |
657 | |
658 | if (!err) | |
659 | if (ovl_dentry_remote(path->dentry)) { | |
660 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", | |
661 | tmp); | |
8aafcb59 | 662 | path_put_init(path); |
7c03b5d4 MS |
663 | err = -EINVAL; |
664 | } | |
ab508822 MS |
665 | kfree(tmp); |
666 | } | |
667 | return err; | |
668 | } | |
669 | ||
6b2d5fe4 MS |
670 | static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, |
671 | const char *name) | |
ab508822 | 672 | { |
ab508822 | 673 | struct kstatfs statfs; |
6b2d5fe4 MS |
674 | int err = vfs_statfs(path, &statfs); |
675 | ||
676 | if (err) | |
677 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
678 | else | |
679 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); | |
680 | ||
681 | return err; | |
682 | } | |
683 | ||
684 | static int ovl_lower_dir(const char *name, struct path *path, | |
685 | struct ovl_fs *ofs, int *stack_depth, bool *remote) | |
686 | { | |
687 | int err; | |
ab508822 | 688 | |
a78d9f0d | 689 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
690 | if (err) |
691 | goto out; | |
692 | ||
6b2d5fe4 MS |
693 | err = ovl_check_namelen(path, ofs, name); |
694 | if (err) | |
ab508822 | 695 | goto out_put; |
6b2d5fe4 | 696 | |
ab508822 MS |
697 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
698 | ||
7c03b5d4 MS |
699 | if (ovl_dentry_remote(path->dentry)) |
700 | *remote = true; | |
701 | ||
02bcd157 AG |
702 | /* |
703 | * The inodes index feature needs to encode and decode file | |
704 | * handles, so it requires that all layers support them. | |
705 | */ | |
706 | if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) { | |
707 | ofs->config.index = false; | |
708 | pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name); | |
709 | } | |
710 | ||
ab508822 MS |
711 | return 0; |
712 | ||
713 | out_put: | |
8aafcb59 | 714 | path_put_init(path); |
ab508822 MS |
715 | out: |
716 | return err; | |
717 | } | |
718 | ||
e9be9d5e MS |
719 | /* Workdir should not be subdir of upperdir and vice versa */ |
720 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
721 | { | |
722 | bool ok = false; | |
723 | ||
724 | if (workdir != upperdir) { | |
725 | ok = (lock_rename(workdir, upperdir) == NULL); | |
726 | unlock_rename(workdir, upperdir); | |
727 | } | |
728 | return ok; | |
729 | } | |
730 | ||
a78d9f0d MS |
731 | static unsigned int ovl_split_lowerdirs(char *str) |
732 | { | |
733 | unsigned int ctr = 1; | |
734 | char *s, *d; | |
735 | ||
736 | for (s = d = str;; s++, d++) { | |
737 | if (*s == '\\') { | |
738 | s++; | |
739 | } else if (*s == ':') { | |
740 | *d = '\0'; | |
741 | ctr++; | |
742 | continue; | |
743 | } | |
744 | *d = *s; | |
745 | if (!*s) | |
746 | break; | |
747 | } | |
748 | return ctr; | |
749 | } | |
750 | ||
0eb45fc3 AG |
751 | static int __maybe_unused |
752 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, | |
753 | struct dentry *dentry, struct inode *inode, | |
754 | const char *name, void *buffer, size_t size) | |
755 | { | |
1d88f183 | 756 | return ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
0eb45fc3 AG |
757 | } |
758 | ||
0c97be22 AG |
759 | static int __maybe_unused |
760 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, | |
761 | struct dentry *dentry, struct inode *inode, | |
762 | const char *name, const void *value, | |
763 | size_t size, int flags) | |
d837a49b MS |
764 | { |
765 | struct dentry *workdir = ovl_workdir(dentry); | |
09d8b586 | 766 | struct inode *realinode = ovl_inode_real(inode); |
d837a49b MS |
767 | struct posix_acl *acl = NULL; |
768 | int err; | |
769 | ||
770 | /* Check that everything is OK before copy-up */ | |
771 | if (value) { | |
772 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
773 | if (IS_ERR(acl)) | |
774 | return PTR_ERR(acl); | |
775 | } | |
776 | err = -EOPNOTSUPP; | |
777 | if (!IS_POSIXACL(d_inode(workdir))) | |
778 | goto out_acl_release; | |
779 | if (!realinode->i_op->set_acl) | |
780 | goto out_acl_release; | |
781 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { | |
782 | err = acl ? -EACCES : 0; | |
783 | goto out_acl_release; | |
784 | } | |
785 | err = -EPERM; | |
786 | if (!inode_owner_or_capable(inode)) | |
787 | goto out_acl_release; | |
788 | ||
789 | posix_acl_release(acl); | |
790 | ||
fd3220d3 MS |
791 | /* |
792 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
793 | * be done with mounter's capabilities and so that won't do it for us). | |
794 | */ | |
795 | if (unlikely(inode->i_mode & S_ISGID) && | |
796 | handler->flags == ACL_TYPE_ACCESS && | |
797 | !in_group_p(inode->i_gid) && | |
798 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { | |
799 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; | |
800 | ||
801 | err = ovl_setattr(dentry, &iattr); | |
802 | if (err) | |
803 | return err; | |
804 | } | |
805 | ||
1d88f183 | 806 | err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); |
ce31513a | 807 | if (!err) |
09d8b586 | 808 | ovl_copyattr(ovl_inode_real(inode), inode); |
ce31513a MS |
809 | |
810 | return err; | |
d837a49b MS |
811 | |
812 | out_acl_release: | |
813 | posix_acl_release(acl); | |
814 | return err; | |
815 | } | |
816 | ||
0eb45fc3 AG |
817 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
818 | struct dentry *dentry, struct inode *inode, | |
819 | const char *name, void *buffer, size_t size) | |
820 | { | |
48fab5d7 | 821 | return -EOPNOTSUPP; |
0eb45fc3 AG |
822 | } |
823 | ||
d837a49b MS |
824 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
825 | struct dentry *dentry, struct inode *inode, | |
826 | const char *name, const void *value, | |
827 | size_t size, int flags) | |
828 | { | |
48fab5d7 | 829 | return -EOPNOTSUPP; |
d837a49b MS |
830 | } |
831 | ||
0eb45fc3 AG |
832 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
833 | struct dentry *dentry, struct inode *inode, | |
834 | const char *name, void *buffer, size_t size) | |
835 | { | |
1d88f183 | 836 | return ovl_xattr_get(dentry, inode, name, buffer, size); |
0eb45fc3 AG |
837 | } |
838 | ||
0e585ccc AG |
839 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
840 | struct dentry *dentry, struct inode *inode, | |
841 | const char *name, const void *value, | |
842 | size_t size, int flags) | |
843 | { | |
1d88f183 | 844 | return ovl_xattr_set(dentry, inode, name, value, size, flags); |
0e585ccc AG |
845 | } |
846 | ||
0c97be22 AG |
847 | static const struct xattr_handler __maybe_unused |
848 | ovl_posix_acl_access_xattr_handler = { | |
d837a49b MS |
849 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
850 | .flags = ACL_TYPE_ACCESS, | |
0eb45fc3 | 851 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
852 | .set = ovl_posix_acl_xattr_set, |
853 | }; | |
854 | ||
0c97be22 AG |
855 | static const struct xattr_handler __maybe_unused |
856 | ovl_posix_acl_default_xattr_handler = { | |
d837a49b MS |
857 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
858 | .flags = ACL_TYPE_DEFAULT, | |
0eb45fc3 | 859 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
860 | .set = ovl_posix_acl_xattr_set, |
861 | }; | |
862 | ||
863 | static const struct xattr_handler ovl_own_xattr_handler = { | |
864 | .prefix = OVL_XATTR_PREFIX, | |
0eb45fc3 | 865 | .get = ovl_own_xattr_get, |
d837a49b MS |
866 | .set = ovl_own_xattr_set, |
867 | }; | |
868 | ||
869 | static const struct xattr_handler ovl_other_xattr_handler = { | |
870 | .prefix = "", /* catch all */ | |
0eb45fc3 | 871 | .get = ovl_other_xattr_get, |
d837a49b MS |
872 | .set = ovl_other_xattr_set, |
873 | }; | |
874 | ||
875 | static const struct xattr_handler *ovl_xattr_handlers[] = { | |
0c97be22 | 876 | #ifdef CONFIG_FS_POSIX_ACL |
d837a49b MS |
877 | &ovl_posix_acl_access_xattr_handler, |
878 | &ovl_posix_acl_default_xattr_handler, | |
0c97be22 | 879 | #endif |
d837a49b MS |
880 | &ovl_own_xattr_handler, |
881 | &ovl_other_xattr_handler, | |
882 | NULL | |
883 | }; | |
884 | ||
ad204488 | 885 | static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath) |
6ee8acf0 | 886 | { |
5064975e | 887 | struct vfsmount *upper_mnt; |
6ee8acf0 MS |
888 | int err; |
889 | ||
ad204488 | 890 | err = ovl_mount_dir(ofs->config.upperdir, upperpath); |
6ee8acf0 MS |
891 | if (err) |
892 | goto out; | |
893 | ||
894 | /* Upper fs should not be r/o */ | |
895 | if (sb_rdonly(upperpath->mnt->mnt_sb)) { | |
896 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
897 | err = -EINVAL; | |
898 | goto out; | |
899 | } | |
900 | ||
ad204488 | 901 | err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); |
6ee8acf0 MS |
902 | if (err) |
903 | goto out; | |
904 | ||
905 | err = -EBUSY; | |
906 | if (ovl_inuse_trylock(upperpath->dentry)) { | |
ad204488 MS |
907 | ofs->upperdir_locked = true; |
908 | } else if (ofs->config.index) { | |
6ee8acf0 MS |
909 | pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n"); |
910 | goto out; | |
911 | } else { | |
912 | pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); | |
913 | } | |
5064975e MS |
914 | |
915 | upper_mnt = clone_private_mount(upperpath); | |
916 | err = PTR_ERR(upper_mnt); | |
917 | if (IS_ERR(upper_mnt)) { | |
918 | pr_err("overlayfs: failed to clone upperpath\n"); | |
919 | goto out; | |
920 | } | |
921 | ||
922 | /* Don't inherit atime flags */ | |
923 | upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
ad204488 | 924 | ofs->upper_mnt = upper_mnt; |
6ee8acf0 MS |
925 | err = 0; |
926 | out: | |
927 | return err; | |
928 | } | |
929 | ||
ad204488 | 930 | static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath) |
8ed61dc3 MS |
931 | { |
932 | struct dentry *temp; | |
933 | int err; | |
934 | ||
ad204488 MS |
935 | ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); |
936 | if (!ofs->workdir) | |
8ed61dc3 MS |
937 | return 0; |
938 | ||
939 | /* | |
940 | * Upper should support d_type, else whiteouts are visible. Given | |
941 | * workdir and upper are on same fs, we can do iterate_dir() on | |
942 | * workdir. This check requires successful creation of workdir in | |
943 | * previous step. | |
944 | */ | |
945 | err = ovl_check_d_type_supported(workpath); | |
946 | if (err < 0) | |
947 | return err; | |
948 | ||
949 | /* | |
950 | * We allowed this configuration and don't want to break users over | |
951 | * kernel upgrade. So warn instead of erroring out. | |
952 | */ | |
953 | if (!err) | |
954 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); | |
955 | ||
956 | /* Check if upper/work fs supports O_TMPFILE */ | |
ad204488 MS |
957 | temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); |
958 | ofs->tmpfile = !IS_ERR(temp); | |
959 | if (ofs->tmpfile) | |
8ed61dc3 MS |
960 | dput(temp); |
961 | else | |
962 | pr_warn("overlayfs: upper fs does not support tmpfile.\n"); | |
963 | ||
964 | /* | |
965 | * Check if upper/work fs supports trusted.overlay.* xattr | |
966 | */ | |
ad204488 | 967 | err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0); |
8ed61dc3 | 968 | if (err) { |
ad204488 | 969 | ofs->noxattr = true; |
8ed61dc3 MS |
970 | pr_warn("overlayfs: upper fs does not support xattr.\n"); |
971 | } else { | |
ad204488 | 972 | vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE); |
8ed61dc3 MS |
973 | } |
974 | ||
975 | /* Check if upper/work fs supports file handles */ | |
ad204488 MS |
976 | if (ofs->config.index && |
977 | !ovl_can_decode_fh(ofs->workdir->d_sb)) { | |
978 | ofs->config.index = false; | |
8ed61dc3 MS |
979 | pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); |
980 | } | |
981 | ||
982 | return 0; | |
983 | } | |
984 | ||
ad204488 | 985 | static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath) |
520d7c86 MS |
986 | { |
987 | int err; | |
bca44b52 | 988 | struct path workpath = { }; |
520d7c86 | 989 | |
ad204488 | 990 | err = ovl_mount_dir(ofs->config.workdir, &workpath); |
520d7c86 MS |
991 | if (err) |
992 | goto out; | |
993 | ||
994 | err = -EINVAL; | |
bca44b52 | 995 | if (upperpath->mnt != workpath.mnt) { |
520d7c86 MS |
996 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
997 | goto out; | |
998 | } | |
bca44b52 | 999 | if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { |
520d7c86 MS |
1000 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
1001 | goto out; | |
1002 | } | |
1003 | ||
1004 | err = -EBUSY; | |
bca44b52 | 1005 | if (ovl_inuse_trylock(workpath.dentry)) { |
ad204488 MS |
1006 | ofs->workdir_locked = true; |
1007 | } else if (ofs->config.index) { | |
520d7c86 MS |
1008 | pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n"); |
1009 | goto out; | |
1010 | } else { | |
1011 | pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); | |
1012 | } | |
1013 | ||
ad204488 MS |
1014 | ofs->workbasedir = dget(workpath.dentry); |
1015 | err = ovl_make_workdir(ofs, &workpath); | |
bca44b52 MS |
1016 | if (err) |
1017 | goto out; | |
1018 | ||
520d7c86 MS |
1019 | err = 0; |
1020 | out: | |
bca44b52 MS |
1021 | path_put(&workpath); |
1022 | ||
520d7c86 MS |
1023 | return err; |
1024 | } | |
1025 | ||
ad204488 | 1026 | static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe, |
95e6d417 | 1027 | struct path *upperpath) |
f7e3a7d9 MS |
1028 | { |
1029 | int err; | |
1030 | ||
1031 | /* Verify lower root is upper root origin */ | |
d9768076 | 1032 | err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry, |
f7e3a7d9 MS |
1033 | false, true); |
1034 | if (err) { | |
1035 | pr_err("overlayfs: failed to verify upper root origin\n"); | |
1036 | goto out; | |
1037 | } | |
1038 | ||
ad204488 MS |
1039 | ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); |
1040 | if (ofs->indexdir) { | |
f7e3a7d9 | 1041 | /* Verify upper root is index dir origin */ |
d9768076 AG |
1042 | err = ovl_verify_origin(ofs->indexdir, upperpath->dentry, |
1043 | true, true); | |
f7e3a7d9 MS |
1044 | if (err) |
1045 | pr_err("overlayfs: failed to verify index dir origin\n"); | |
1046 | ||
1047 | /* Cleanup bad/stale/orphan index entries */ | |
1048 | if (!err) | |
ad204488 MS |
1049 | err = ovl_indexdir_cleanup(ofs->indexdir, |
1050 | ofs->upper_mnt, | |
f7e3a7d9 MS |
1051 | oe->lowerstack, |
1052 | oe->numlower); | |
1053 | } | |
ad204488 | 1054 | if (err || !ofs->indexdir) |
f7e3a7d9 MS |
1055 | pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); |
1056 | ||
1057 | out: | |
1058 | return err; | |
1059 | } | |
1060 | ||
ad204488 | 1061 | static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack, |
520d7c86 MS |
1062 | unsigned int numlower) |
1063 | { | |
1064 | int err; | |
1065 | unsigned int i; | |
1066 | ||
1067 | err = -ENOMEM; | |
ad204488 | 1068 | ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer), |
520d7c86 | 1069 | GFP_KERNEL); |
ad204488 | 1070 | if (ofs->lower_layers == NULL) |
520d7c86 MS |
1071 | goto out; |
1072 | for (i = 0; i < numlower; i++) { | |
1073 | struct vfsmount *mnt; | |
1074 | dev_t dev; | |
1075 | ||
1076 | err = get_anon_bdev(&dev); | |
1077 | if (err) { | |
1078 | pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n"); | |
1079 | goto out; | |
1080 | } | |
1081 | ||
1082 | mnt = clone_private_mount(&stack[i]); | |
1083 | err = PTR_ERR(mnt); | |
1084 | if (IS_ERR(mnt)) { | |
1085 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
1086 | free_anon_bdev(dev); | |
1087 | goto out; | |
1088 | } | |
1089 | /* | |
1090 | * Make lower layers R/O. That way fchmod/fchown on lower file | |
1091 | * will fail instead of modifying lower fs. | |
1092 | */ | |
1093 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; | |
1094 | ||
ad204488 MS |
1095 | ofs->lower_layers[ofs->numlower].mnt = mnt; |
1096 | ofs->lower_layers[ofs->numlower].pseudo_dev = dev; | |
1097 | ofs->numlower++; | |
520d7c86 MS |
1098 | |
1099 | /* Check if all lower layers are on same sb */ | |
1100 | if (i == 0) | |
ad204488 MS |
1101 | ofs->same_sb = mnt->mnt_sb; |
1102 | else if (ofs->same_sb != mnt->mnt_sb) | |
1103 | ofs->same_sb = NULL; | |
520d7c86 MS |
1104 | } |
1105 | err = 0; | |
1106 | out: | |
1107 | return err; | |
1108 | } | |
1109 | ||
4155c10a | 1110 | static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, |
ad204488 | 1111 | struct ovl_fs *ofs) |
53dbb0b4 MS |
1112 | { |
1113 | int err; | |
1114 | char *lowertmp, *lower; | |
4155c10a MS |
1115 | struct path *stack = NULL; |
1116 | unsigned int stacklen, numlower = 0, i; | |
53dbb0b4 | 1117 | bool remote = false; |
4155c10a | 1118 | struct ovl_entry *oe; |
53dbb0b4 MS |
1119 | |
1120 | err = -ENOMEM; | |
ad204488 | 1121 | lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL); |
53dbb0b4 | 1122 | if (!lowertmp) |
4155c10a | 1123 | goto out_err; |
53dbb0b4 MS |
1124 | |
1125 | err = -EINVAL; | |
1126 | stacklen = ovl_split_lowerdirs(lowertmp); | |
1127 | if (stacklen > OVL_MAX_STACK) { | |
1128 | pr_err("overlayfs: too many lower directories, limit is %d\n", | |
1129 | OVL_MAX_STACK); | |
4155c10a | 1130 | goto out_err; |
ad204488 | 1131 | } else if (!ofs->config.upperdir && stacklen == 1) { |
53dbb0b4 | 1132 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
4155c10a | 1133 | goto out_err; |
53dbb0b4 MS |
1134 | } |
1135 | ||
1136 | err = -ENOMEM; | |
1137 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
1138 | if (!stack) | |
4155c10a | 1139 | goto out_err; |
53dbb0b4 MS |
1140 | |
1141 | err = -EINVAL; | |
1142 | lower = lowertmp; | |
1143 | for (numlower = 0; numlower < stacklen; numlower++) { | |
ad204488 | 1144 | err = ovl_lower_dir(lower, &stack[numlower], ofs, |
53dbb0b4 MS |
1145 | &sb->s_stack_depth, &remote); |
1146 | if (err) | |
4155c10a | 1147 | goto out_err; |
53dbb0b4 MS |
1148 | |
1149 | lower = strchr(lower, '\0') + 1; | |
1150 | } | |
1151 | ||
1152 | err = -EINVAL; | |
1153 | sb->s_stack_depth++; | |
1154 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { | |
1155 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
4155c10a | 1156 | goto out_err; |
53dbb0b4 MS |
1157 | } |
1158 | ||
ad204488 | 1159 | err = ovl_get_lower_layers(ofs, stack, numlower); |
4155c10a MS |
1160 | if (err) |
1161 | goto out_err; | |
1162 | ||
1163 | err = -ENOMEM; | |
1164 | oe = ovl_alloc_entry(numlower); | |
1165 | if (!oe) | |
1166 | goto out_err; | |
1167 | ||
1168 | for (i = 0; i < numlower; i++) { | |
1169 | oe->lowerstack[i].dentry = dget(stack[i].dentry); | |
ad204488 | 1170 | oe->lowerstack[i].layer = &ofs->lower_layers[i]; |
4155c10a | 1171 | } |
53dbb0b4 MS |
1172 | |
1173 | if (remote) | |
1174 | sb->s_d_op = &ovl_reval_dentry_operations; | |
1175 | else | |
1176 | sb->s_d_op = &ovl_dentry_operations; | |
1177 | ||
53dbb0b4 | 1178 | out: |
53dbb0b4 MS |
1179 | for (i = 0; i < numlower; i++) |
1180 | path_put(&stack[i]); | |
1181 | kfree(stack); | |
4155c10a MS |
1182 | kfree(lowertmp); |
1183 | ||
1184 | return oe; | |
1185 | ||
1186 | out_err: | |
1187 | oe = ERR_PTR(err); | |
53dbb0b4 MS |
1188 | goto out; |
1189 | } | |
1190 | ||
e9be9d5e MS |
1191 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
1192 | { | |
33006cdf | 1193 | struct path upperpath = { }; |
e9be9d5e | 1194 | struct dentry *root_dentry; |
4155c10a | 1195 | struct ovl_entry *oe; |
ad204488 | 1196 | struct ovl_fs *ofs; |
51f8f3c4 | 1197 | struct cred *cred; |
e9be9d5e MS |
1198 | int err; |
1199 | ||
f45827e8 | 1200 | err = -ENOMEM; |
ad204488 MS |
1201 | ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
1202 | if (!ofs) | |
e9be9d5e MS |
1203 | goto out; |
1204 | ||
ad204488 | 1205 | ofs->creator_cred = cred = prepare_creds(); |
c6fe6254 MS |
1206 | if (!cred) |
1207 | goto out_err; | |
1208 | ||
ad204488 MS |
1209 | ofs->config.index = ovl_index_def; |
1210 | err = ovl_parse_opt((char *) data, &ofs->config); | |
f45827e8 | 1211 | if (err) |
a9075cdb | 1212 | goto out_err; |
f45827e8 | 1213 | |
e9be9d5e | 1214 | err = -EINVAL; |
ad204488 | 1215 | if (!ofs->config.lowerdir) { |
07f2af7b KK |
1216 | if (!silent) |
1217 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
a9075cdb | 1218 | goto out_err; |
e9be9d5e MS |
1219 | } |
1220 | ||
53a08cb9 | 1221 | sb->s_stack_depth = 0; |
cf9a6784 | 1222 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
ad204488 MS |
1223 | if (ofs->config.upperdir) { |
1224 | if (!ofs->config.workdir) { | |
53a08cb9 | 1225 | pr_err("overlayfs: missing 'workdir'\n"); |
a9075cdb | 1226 | goto out_err; |
53a08cb9 | 1227 | } |
e9be9d5e | 1228 | |
ad204488 | 1229 | err = ovl_get_upper(ofs, &upperpath); |
53a08cb9 | 1230 | if (err) |
a9075cdb | 1231 | goto out_err; |
2cac0c00 | 1232 | |
ad204488 | 1233 | err = ovl_get_workdir(ofs, &upperpath); |
8ed61dc3 | 1234 | if (err) |
a9075cdb | 1235 | goto out_err; |
c6fe6254 | 1236 | |
ad204488 | 1237 | if (!ofs->workdir) |
1751e8a6 | 1238 | sb->s_flags |= SB_RDONLY; |
6e88256e | 1239 | |
ad204488 MS |
1240 | sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth; |
1241 | sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran; | |
c6fe6254 | 1242 | |
e9be9d5e | 1243 | } |
ad204488 | 1244 | oe = ovl_get_lowerstack(sb, ofs); |
4155c10a MS |
1245 | err = PTR_ERR(oe); |
1246 | if (IS_ERR(oe)) | |
a9075cdb | 1247 | goto out_err; |
e9be9d5e | 1248 | |
71cbad7e | 1249 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
ad204488 | 1250 | if (!ofs->upper_mnt) |
1751e8a6 | 1251 | sb->s_flags |= SB_RDONLY; |
ad204488 MS |
1252 | else if (ofs->upper_mnt->mnt_sb != ofs->same_sb) |
1253 | ofs->same_sb = NULL; | |
e9be9d5e | 1254 | |
ad204488 MS |
1255 | if (!(ovl_force_readonly(ofs)) && ofs->config.index) { |
1256 | err = ovl_get_indexdir(ofs, oe, &upperpath); | |
54fb347e | 1257 | if (err) |
4155c10a | 1258 | goto out_free_oe; |
6e88256e | 1259 | |
ad204488 | 1260 | if (!ofs->indexdir) |
1751e8a6 | 1261 | sb->s_flags |= SB_RDONLY; |
02bcd157 AG |
1262 | } |
1263 | ||
1264 | /* Show index=off/on in /proc/mounts for any of the reasons above */ | |
ad204488 MS |
1265 | if (!ofs->indexdir) |
1266 | ofs->config.index = false; | |
02bcd157 | 1267 | |
51f8f3c4 KK |
1268 | /* Never override disk quota limits or use reserved space */ |
1269 | cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); | |
1270 | ||
655042cc VG |
1271 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
1272 | sb->s_op = &ovl_super_operations; | |
1273 | sb->s_xattr = ovl_xattr_handlers; | |
ad204488 | 1274 | sb->s_fs_info = ofs; |
1751e8a6 | 1275 | sb->s_flags |= SB_POSIXACL | SB_NOREMOTELOCK; |
655042cc | 1276 | |
c6fe6254 | 1277 | err = -ENOMEM; |
ca4c8a3a | 1278 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
e9be9d5e | 1279 | if (!root_dentry) |
4155c10a | 1280 | goto out_free_oe; |
e9be9d5e MS |
1281 | |
1282 | mntput(upperpath.mnt); | |
f3a15685 | 1283 | if (upperpath.dentry) { |
55acc661 | 1284 | oe->has_upper = true; |
13c72075 MS |
1285 | if (ovl_is_impuredir(upperpath.dentry)) |
1286 | ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); | |
f3a15685 | 1287 | } |
e9be9d5e MS |
1288 | |
1289 | root_dentry->d_fsdata = oe; | |
1290 | ||
b79e05aa AG |
1291 | /* Root is always merge -> can have whiteouts */ |
1292 | ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); | |
09d8b586 MS |
1293 | ovl_inode_init(d_inode(root_dentry), upperpath.dentry, |
1294 | ovl_dentry_lower(root_dentry)); | |
ed06e069 | 1295 | |
e9be9d5e | 1296 | sb->s_root = root_dentry; |
e9be9d5e MS |
1297 | |
1298 | return 0; | |
1299 | ||
4155c10a MS |
1300 | out_free_oe: |
1301 | ovl_entry_stack_free(oe); | |
b9343632 | 1302 | kfree(oe); |
4155c10a | 1303 | out_err: |
e9be9d5e | 1304 | path_put(&upperpath); |
ad204488 | 1305 | ovl_free_fs(ofs); |
e9be9d5e MS |
1306 | out: |
1307 | return err; | |
1308 | } | |
1309 | ||
1310 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1311 | const char *dev_name, void *raw_data) | |
1312 | { | |
1313 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1314 | } | |
1315 | ||
1316 | static struct file_system_type ovl_fs_type = { | |
1317 | .owner = THIS_MODULE, | |
ef94b186 | 1318 | .name = "overlay", |
e9be9d5e MS |
1319 | .mount = ovl_mount, |
1320 | .kill_sb = kill_anon_super, | |
1321 | }; | |
ef94b186 | 1322 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e | 1323 | |
13cf199d AG |
1324 | static void ovl_inode_init_once(void *foo) |
1325 | { | |
1326 | struct ovl_inode *oi = foo; | |
1327 | ||
1328 | inode_init_once(&oi->vfs_inode); | |
1329 | } | |
1330 | ||
e9be9d5e MS |
1331 | static int __init ovl_init(void) |
1332 | { | |
13cf199d AG |
1333 | int err; |
1334 | ||
1335 | ovl_inode_cachep = kmem_cache_create("ovl_inode", | |
1336 | sizeof(struct ovl_inode), 0, | |
1337 | (SLAB_RECLAIM_ACCOUNT| | |
1338 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), | |
1339 | ovl_inode_init_once); | |
1340 | if (ovl_inode_cachep == NULL) | |
1341 | return -ENOMEM; | |
1342 | ||
1343 | err = register_filesystem(&ovl_fs_type); | |
1344 | if (err) | |
1345 | kmem_cache_destroy(ovl_inode_cachep); | |
1346 | ||
1347 | return err; | |
e9be9d5e MS |
1348 | } |
1349 | ||
1350 | static void __exit ovl_exit(void) | |
1351 | { | |
1352 | unregister_filesystem(&ovl_fs_type); | |
13cf199d AG |
1353 | |
1354 | /* | |
1355 | * Make sure all delayed rcu free inodes are flushed before we | |
1356 | * destroy cache. | |
1357 | */ | |
1358 | rcu_barrier(); | |
1359 | kmem_cache_destroy(ovl_inode_cachep); | |
1360 | ||
e9be9d5e MS |
1361 | } |
1362 | ||
1363 | module_init(ovl_init); | |
1364 | module_exit(ovl_exit); |