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