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