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