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