1
2
3
4
5
6
7#include <uapi/linux/magic.h>
8#include <linux/fs.h>
9#include <linux/namei.h>
10#include <linux/xattr.h>
11#include <linux/mount.h>
12#include <linux/parser.h>
13#include <linux/module.h>
14#include <linux/statfs.h>
15#include <linux/seq_file.h>
16#include <linux/posix_acl_xattr.h>
17#include <linux/exportfs.h>
18#include "overlayfs.h"
19
20MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21MODULE_DESCRIPTION("Overlay filesystem");
22MODULE_LICENSE("GPL");
23
24
25struct ovl_dir_cache;
26
27#define OVL_MAX_STACK 500
28
29static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
30module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
31MODULE_PARM_DESC(redirect_dir,
32 "Default to on or off for the redirect_dir feature");
33
34static bool ovl_redirect_always_follow =
35 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
36module_param_named(redirect_always_follow, ovl_redirect_always_follow,
37 bool, 0644);
38MODULE_PARM_DESC(redirect_always_follow,
39 "Follow redirects even if redirect_dir feature is turned off");
40
41static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
42module_param_named(index, ovl_index_def, bool, 0644);
43MODULE_PARM_DESC(index,
44 "Default to on or off for the inodes index feature");
45
46static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
47module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
48MODULE_PARM_DESC(nfs_export,
49 "Default to on or off for the NFS export feature");
50
51static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
52module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
53MODULE_PARM_DESC(xino_auto,
54 "Auto enable xino feature");
55
56static void ovl_entry_stack_free(struct ovl_entry *oe)
57{
58 unsigned int i;
59
60 for (i = 0; i < oe->numlower; i++)
61 dput(oe->lowerstack[i].dentry);
62}
63
64static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
65module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
66MODULE_PARM_DESC(metacopy,
67 "Default to on or off for the metadata only copy up feature");
68
69static void ovl_dentry_release(struct dentry *dentry)
70{
71 struct ovl_entry *oe = dentry->d_fsdata;
72
73 if (oe) {
74 ovl_entry_stack_free(oe);
75 kfree_rcu(oe, rcu);
76 }
77}
78
79static struct dentry *ovl_d_real(struct dentry *dentry,
80 const struct inode *inode)
81{
82 struct dentry *real;
83
84
85 if (inode && d_inode(dentry) == inode)
86 return dentry;
87
88 if (!d_is_reg(dentry)) {
89 if (!inode || inode == d_inode(dentry))
90 return dentry;
91 goto bug;
92 }
93
94 real = ovl_dentry_upper(dentry);
95 if (real && (inode == d_inode(real)))
96 return real;
97
98 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
99 return real;
100
101 real = ovl_dentry_lowerdata(dentry);
102 if (!real)
103 goto bug;
104
105
106 real = d_real(real, inode);
107
108 if (!inode || inode == d_inode(real))
109 return real;
110bug:
111 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
112 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
113 return dentry;
114}
115
116static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
117{
118 struct ovl_entry *oe = dentry->d_fsdata;
119 unsigned int i;
120 int ret = 1;
121
122 for (i = 0; i < oe->numlower; i++) {
123 struct dentry *d = oe->lowerstack[i].dentry;
124
125 if (d->d_flags & DCACHE_OP_REVALIDATE) {
126 ret = d->d_op->d_revalidate(d, flags);
127 if (ret < 0)
128 return ret;
129 if (!ret) {
130 if (!(flags & LOOKUP_RCU))
131 d_invalidate(d);
132 return -ESTALE;
133 }
134 }
135 }
136 return 1;
137}
138
139static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
140{
141 struct ovl_entry *oe = dentry->d_fsdata;
142 unsigned int i;
143 int ret = 1;
144
145 for (i = 0; i < oe->numlower; i++) {
146 struct dentry *d = oe->lowerstack[i].dentry;
147
148 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
149 ret = d->d_op->d_weak_revalidate(d, flags);
150 if (ret <= 0)
151 break;
152 }
153 }
154 return ret;
155}
156
157static const struct dentry_operations ovl_dentry_operations = {
158 .d_release = ovl_dentry_release,
159 .d_real = ovl_d_real,
160};
161
162static const struct dentry_operations ovl_reval_dentry_operations = {
163 .d_release = ovl_dentry_release,
164 .d_real = ovl_d_real,
165 .d_revalidate = ovl_dentry_revalidate,
166 .d_weak_revalidate = ovl_dentry_weak_revalidate,
167};
168
169static struct kmem_cache *ovl_inode_cachep;
170
171static struct inode *ovl_alloc_inode(struct super_block *sb)
172{
173 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
174
175 if (!oi)
176 return NULL;
177
178 oi->cache = NULL;
179 oi->redirect = NULL;
180 oi->version = 0;
181 oi->flags = 0;
182 oi->__upperdentry = NULL;
183 oi->lower = NULL;
184 oi->lowerdata = NULL;
185 mutex_init(&oi->lock);
186
187 return &oi->vfs_inode;
188}
189
190static void ovl_free_inode(struct inode *inode)
191{
192 struct ovl_inode *oi = OVL_I(inode);
193
194 kfree(oi->redirect);
195 mutex_destroy(&oi->lock);
196 kmem_cache_free(ovl_inode_cachep, oi);
197}
198
199static void ovl_destroy_inode(struct inode *inode)
200{
201 struct ovl_inode *oi = OVL_I(inode);
202
203 dput(oi->__upperdentry);
204 iput(oi->lower);
205 if (S_ISDIR(inode->i_mode))
206 ovl_dir_cache_free(inode);
207 else
208 iput(oi->lowerdata);
209}
210
211static void ovl_free_fs(struct ovl_fs *ofs)
212{
213 unsigned i;
214
215 iput(ofs->indexdir_trap);
216 iput(ofs->workdir_trap);
217 iput(ofs->upperdir_trap);
218 dput(ofs->indexdir);
219 dput(ofs->workdir);
220 if (ofs->workdir_locked)
221 ovl_inuse_unlock(ofs->workbasedir);
222 dput(ofs->workbasedir);
223 if (ofs->upperdir_locked)
224 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
225 mntput(ofs->upper_mnt);
226 for (i = 0; i < ofs->numlower; i++) {
227 iput(ofs->lower_layers[i].trap);
228 mntput(ofs->lower_layers[i].mnt);
229 }
230 for (i = 0; i < ofs->numlowerfs; i++)
231 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
232 kfree(ofs->lower_layers);
233 kfree(ofs->lower_fs);
234
235 kfree(ofs->config.lowerdir);
236 kfree(ofs->config.upperdir);
237 kfree(ofs->config.workdir);
238 kfree(ofs->config.redirect_mode);
239 if (ofs->creator_cred)
240 put_cred(ofs->creator_cred);
241 kfree(ofs);
242}
243
244static void ovl_put_super(struct super_block *sb)
245{
246 struct ovl_fs *ofs = sb->s_fs_info;
247
248 ovl_free_fs(ofs);
249}
250
251
252static int ovl_sync_fs(struct super_block *sb, int wait)
253{
254 struct ovl_fs *ofs = sb->s_fs_info;
255 struct super_block *upper_sb;
256 int ret;
257
258 if (!ofs->upper_mnt)
259 return 0;
260
261
262
263
264
265
266
267
268
269 if (!wait)
270 return 0;
271
272 upper_sb = ofs->upper_mnt->mnt_sb;
273
274 down_read(&upper_sb->s_umount);
275 ret = sync_filesystem(upper_sb);
276 up_read(&upper_sb->s_umount);
277
278 return ret;
279}
280
281
282
283
284
285
286
287
288
289static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
290{
291 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
292 struct dentry *root_dentry = dentry->d_sb->s_root;
293 struct path path;
294 int err;
295
296 ovl_path_real(root_dentry, &path);
297
298 err = vfs_statfs(&path, buf);
299 if (!err) {
300 buf->f_namelen = ofs->namelen;
301 buf->f_type = OVERLAYFS_SUPER_MAGIC;
302 }
303
304 return err;
305}
306
307
308static bool ovl_force_readonly(struct ovl_fs *ofs)
309{
310 return (!ofs->upper_mnt || !ofs->workdir);
311}
312
313static const char *ovl_redirect_mode_def(void)
314{
315 return ovl_redirect_dir_def ? "on" : "off";
316}
317
318enum {
319 OVL_XINO_OFF,
320 OVL_XINO_AUTO,
321 OVL_XINO_ON,
322};
323
324static const char * const ovl_xino_str[] = {
325 "off",
326 "auto",
327 "on",
328};
329
330static inline int ovl_xino_def(void)
331{
332 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
333}
334
335
336
337
338
339
340
341static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
342{
343 struct super_block *sb = dentry->d_sb;
344 struct ovl_fs *ofs = sb->s_fs_info;
345
346 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
347 if (ofs->config.upperdir) {
348 seq_show_option(m, "upperdir", ofs->config.upperdir);
349 seq_show_option(m, "workdir", ofs->config.workdir);
350 }
351 if (ofs->config.default_permissions)
352 seq_puts(m, ",default_permissions");
353 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
354 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
355 if (ofs->config.index != ovl_index_def)
356 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
357 if (ofs->config.nfs_export != ovl_nfs_export_def)
358 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
359 "on" : "off");
360 if (ofs->config.xino != ovl_xino_def())
361 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
362 if (ofs->config.metacopy != ovl_metacopy_def)
363 seq_printf(m, ",metacopy=%s",
364 ofs->config.metacopy ? "on" : "off");
365 return 0;
366}
367
368static int ovl_remount(struct super_block *sb, int *flags, char *data)
369{
370 struct ovl_fs *ofs = sb->s_fs_info;
371
372 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
373 return -EROFS;
374
375 return 0;
376}
377
378static const struct super_operations ovl_super_operations = {
379 .alloc_inode = ovl_alloc_inode,
380 .free_inode = ovl_free_inode,
381 .destroy_inode = ovl_destroy_inode,
382 .drop_inode = generic_delete_inode,
383 .put_super = ovl_put_super,
384 .sync_fs = ovl_sync_fs,
385 .statfs = ovl_statfs,
386 .show_options = ovl_show_options,
387 .remount_fs = ovl_remount,
388};
389
390enum {
391 OPT_LOWERDIR,
392 OPT_UPPERDIR,
393 OPT_WORKDIR,
394 OPT_DEFAULT_PERMISSIONS,
395 OPT_REDIRECT_DIR,
396 OPT_INDEX_ON,
397 OPT_INDEX_OFF,
398 OPT_NFS_EXPORT_ON,
399 OPT_NFS_EXPORT_OFF,
400 OPT_XINO_ON,
401 OPT_XINO_OFF,
402 OPT_XINO_AUTO,
403 OPT_METACOPY_ON,
404 OPT_METACOPY_OFF,
405 OPT_ERR,
406};
407
408static const match_table_t ovl_tokens = {
409 {OPT_LOWERDIR, "lowerdir=%s"},
410 {OPT_UPPERDIR, "upperdir=%s"},
411 {OPT_WORKDIR, "workdir=%s"},
412 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
413 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
414 {OPT_INDEX_ON, "index=on"},
415 {OPT_INDEX_OFF, "index=off"},
416 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
417 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
418 {OPT_XINO_ON, "xino=on"},
419 {OPT_XINO_OFF, "xino=off"},
420 {OPT_XINO_AUTO, "xino=auto"},
421 {OPT_METACOPY_ON, "metacopy=on"},
422 {OPT_METACOPY_OFF, "metacopy=off"},
423 {OPT_ERR, NULL}
424};
425
426static char *ovl_next_opt(char **s)
427{
428 char *sbegin = *s;
429 char *p;
430
431 if (sbegin == NULL)
432 return NULL;
433
434 for (p = sbegin; *p; p++) {
435 if (*p == '\\') {
436 p++;
437 if (!*p)
438 break;
439 } else if (*p == ',') {
440 *p = '\0';
441 *s = p + 1;
442 return sbegin;
443 }
444 }
445 *s = NULL;
446 return sbegin;
447}
448
449static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
450{
451 if (strcmp(mode, "on") == 0) {
452 config->redirect_dir = true;
453
454
455
456
457 config->redirect_follow = true;
458 } else if (strcmp(mode, "follow") == 0) {
459 config->redirect_follow = true;
460 } else if (strcmp(mode, "off") == 0) {
461 if (ovl_redirect_always_follow)
462 config->redirect_follow = true;
463 } else if (strcmp(mode, "nofollow") != 0) {
464 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
465 mode);
466 return -EINVAL;
467 }
468
469 return 0;
470}
471
472static int ovl_parse_opt(char *opt, struct ovl_config *config)
473{
474 char *p;
475 int err;
476 bool metacopy_opt = false, redirect_opt = false;
477
478 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
479 if (!config->redirect_mode)
480 return -ENOMEM;
481
482 while ((p = ovl_next_opt(&opt)) != NULL) {
483 int token;
484 substring_t args[MAX_OPT_ARGS];
485
486 if (!*p)
487 continue;
488
489 token = match_token(p, ovl_tokens, args);
490 switch (token) {
491 case OPT_UPPERDIR:
492 kfree(config->upperdir);
493 config->upperdir = match_strdup(&args[0]);
494 if (!config->upperdir)
495 return -ENOMEM;
496 break;
497
498 case OPT_LOWERDIR:
499 kfree(config->lowerdir);
500 config->lowerdir = match_strdup(&args[0]);
501 if (!config->lowerdir)
502 return -ENOMEM;
503 break;
504
505 case OPT_WORKDIR:
506 kfree(config->workdir);
507 config->workdir = match_strdup(&args[0]);
508 if (!config->workdir)
509 return -ENOMEM;
510 break;
511
512 case OPT_DEFAULT_PERMISSIONS:
513 config->default_permissions = true;
514 break;
515
516 case OPT_REDIRECT_DIR:
517 kfree(config->redirect_mode);
518 config->redirect_mode = match_strdup(&args[0]);
519 if (!config->redirect_mode)
520 return -ENOMEM;
521 redirect_opt = true;
522 break;
523
524 case OPT_INDEX_ON:
525 config->index = true;
526 break;
527
528 case OPT_INDEX_OFF:
529 config->index = false;
530 break;
531
532 case OPT_NFS_EXPORT_ON:
533 config->nfs_export = true;
534 break;
535
536 case OPT_NFS_EXPORT_OFF:
537 config->nfs_export = false;
538 break;
539
540 case OPT_XINO_ON:
541 config->xino = OVL_XINO_ON;
542 break;
543
544 case OPT_XINO_OFF:
545 config->xino = OVL_XINO_OFF;
546 break;
547
548 case OPT_XINO_AUTO:
549 config->xino = OVL_XINO_AUTO;
550 break;
551
552 case OPT_METACOPY_ON:
553 config->metacopy = true;
554 metacopy_opt = true;
555 break;
556
557 case OPT_METACOPY_OFF:
558 config->metacopy = false;
559 break;
560
561 default:
562 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
563 return -EINVAL;
564 }
565 }
566
567
568 if (!config->upperdir && config->workdir) {
569 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
570 config->workdir);
571 kfree(config->workdir);
572 config->workdir = NULL;
573 }
574
575 err = ovl_parse_redirect_mode(config, config->redirect_mode);
576 if (err)
577 return err;
578
579
580
581
582
583 if (!config->upperdir && config->redirect_follow)
584 config->redirect_dir = true;
585
586
587 if (config->metacopy && !config->redirect_dir) {
588 if (metacopy_opt && redirect_opt) {
589 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
590 config->redirect_mode);
591 return -EINVAL;
592 }
593 if (redirect_opt) {
594
595
596
597
598 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
599 config->redirect_mode);
600 config->metacopy = false;
601 } else {
602
603 config->redirect_follow = config->redirect_dir = true;
604 }
605 }
606
607 return 0;
608}
609
610#define OVL_WORKDIR_NAME "work"
611#define OVL_INDEXDIR_NAME "index"
612
613static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
614 const char *name, bool persist)
615{
616 struct inode *dir = ofs->workbasedir->d_inode;
617 struct vfsmount *mnt = ofs->upper_mnt;
618 struct dentry *work;
619 int err;
620 bool retried = false;
621 bool locked = false;
622
623 inode_lock_nested(dir, I_MUTEX_PARENT);
624 locked = true;
625
626retry:
627 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
628
629 if (!IS_ERR(work)) {
630 struct iattr attr = {
631 .ia_valid = ATTR_MODE,
632 .ia_mode = S_IFDIR | 0,
633 };
634
635 if (work->d_inode) {
636 err = -EEXIST;
637 if (retried)
638 goto out_dput;
639
640 if (persist)
641 goto out_unlock;
642
643 retried = true;
644 ovl_workdir_cleanup(dir, mnt, work, 0);
645 dput(work);
646 goto retry;
647 }
648
649 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
650 err = PTR_ERR(work);
651 if (IS_ERR(work))
652 goto out_err;
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
668 if (err && err != -ENODATA && err != -EOPNOTSUPP)
669 goto out_dput;
670
671 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
672 if (err && err != -ENODATA && err != -EOPNOTSUPP)
673 goto out_dput;
674
675
676 inode_lock(work->d_inode);
677 err = notify_change(work, &attr, NULL);
678 inode_unlock(work->d_inode);
679 if (err)
680 goto out_dput;
681 } else {
682 err = PTR_ERR(work);
683 goto out_err;
684 }
685out_unlock:
686 if (locked)
687 inode_unlock(dir);
688
689 return work;
690
691out_dput:
692 dput(work);
693out_err:
694 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
695 ofs->config.workdir, name, -err);
696 work = NULL;
697 goto out_unlock;
698}
699
700static void ovl_unescape(char *s)
701{
702 char *d = s;
703
704 for (;; s++, d++) {
705 if (*s == '\\')
706 s++;
707 *d = *s;
708 if (!*s)
709 break;
710 }
711}
712
713static int ovl_mount_dir_noesc(const char *name, struct path *path)
714{
715 int err = -EINVAL;
716
717 if (!*name) {
718 pr_err("overlayfs: empty lowerdir\n");
719 goto out;
720 }
721 err = kern_path(name, LOOKUP_FOLLOW, path);
722 if (err) {
723 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
724 goto out;
725 }
726 err = -EINVAL;
727 if (ovl_dentry_weird(path->dentry)) {
728 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
729 goto out_put;
730 }
731 if (!d_is_dir(path->dentry)) {
732 pr_err("overlayfs: '%s' not a directory\n", name);
733 goto out_put;
734 }
735 return 0;
736
737out_put:
738 path_put_init(path);
739out:
740 return err;
741}
742
743static int ovl_mount_dir(const char *name, struct path *path)
744{
745 int err = -ENOMEM;
746 char *tmp = kstrdup(name, GFP_KERNEL);
747
748 if (tmp) {
749 ovl_unescape(tmp);
750 err = ovl_mount_dir_noesc(tmp, path);
751
752 if (!err)
753 if (ovl_dentry_remote(path->dentry)) {
754 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
755 tmp);
756 path_put_init(path);
757 err = -EINVAL;
758 }
759 kfree(tmp);
760 }
761 return err;
762}
763
764static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
765 const char *name)
766{
767 struct kstatfs statfs;
768 int err = vfs_statfs(path, &statfs);
769
770 if (err)
771 pr_err("overlayfs: statfs failed on '%s'\n", name);
772 else
773 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
774
775 return err;
776}
777
778static int ovl_lower_dir(const char *name, struct path *path,
779 struct ovl_fs *ofs, int *stack_depth, bool *remote)
780{
781 int fh_type;
782 int err;
783
784 err = ovl_mount_dir_noesc(name, path);
785 if (err)
786 goto out;
787
788 err = ovl_check_namelen(path, ofs, name);
789 if (err)
790 goto out_put;
791
792 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
793
794 if (ovl_dentry_remote(path->dentry))
795 *remote = true;
796
797
798
799
800
801 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
802 if ((ofs->config.nfs_export ||
803 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
804 ofs->config.index = false;
805 ofs->config.nfs_export = false;
806 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
807 name);
808 }
809
810
811 if (fh_type != FILEID_INO32_GEN)
812 ofs->xino_bits = 0;
813
814 return 0;
815
816out_put:
817 path_put_init(path);
818out:
819 return err;
820}
821
822
823static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
824{
825 bool ok = false;
826
827 if (workdir != upperdir) {
828 ok = (lock_rename(workdir, upperdir) == NULL);
829 unlock_rename(workdir, upperdir);
830 }
831 return ok;
832}
833
834static unsigned int ovl_split_lowerdirs(char *str)
835{
836 unsigned int ctr = 1;
837 char *s, *d;
838
839 for (s = d = str;; s++, d++) {
840 if (*s == '\\') {
841 s++;
842 } else if (*s == ':') {
843 *d = '\0';
844 ctr++;
845 continue;
846 }
847 *d = *s;
848 if (!*s)
849 break;
850 }
851 return ctr;
852}
853
854static int __maybe_unused
855ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
856 struct dentry *dentry, struct inode *inode,
857 const char *name, void *buffer, size_t size)
858{
859 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
860}
861
862static int __maybe_unused
863ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
864 struct dentry *dentry, struct inode *inode,
865 const char *name, const void *value,
866 size_t size, int flags)
867{
868 struct dentry *workdir = ovl_workdir(dentry);
869 struct inode *realinode = ovl_inode_real(inode);
870 struct posix_acl *acl = NULL;
871 int err;
872
873
874 if (value) {
875 acl = posix_acl_from_xattr(&init_user_ns, value, size);
876 if (IS_ERR(acl))
877 return PTR_ERR(acl);
878 }
879 err = -EOPNOTSUPP;
880 if (!IS_POSIXACL(d_inode(workdir)))
881 goto out_acl_release;
882 if (!realinode->i_op->set_acl)
883 goto out_acl_release;
884 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
885 err = acl ? -EACCES : 0;
886 goto out_acl_release;
887 }
888 err = -EPERM;
889 if (!inode_owner_or_capable(inode))
890 goto out_acl_release;
891
892 posix_acl_release(acl);
893
894
895
896
897
898 if (unlikely(inode->i_mode & S_ISGID) &&
899 handler->flags == ACL_TYPE_ACCESS &&
900 !in_group_p(inode->i_gid) &&
901 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
902 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
903
904 err = ovl_setattr(dentry, &iattr);
905 if (err)
906 return err;
907 }
908
909 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
910 if (!err)
911 ovl_copyattr(ovl_inode_real(inode), inode);
912
913 return err;
914
915out_acl_release:
916 posix_acl_release(acl);
917 return err;
918}
919
920static int ovl_own_xattr_get(const struct xattr_handler *handler,
921 struct dentry *dentry, struct inode *inode,
922 const char *name, void *buffer, size_t size)
923{
924 return -EOPNOTSUPP;
925}
926
927static int ovl_own_xattr_set(const struct xattr_handler *handler,
928 struct dentry *dentry, struct inode *inode,
929 const char *name, const void *value,
930 size_t size, int flags)
931{
932 return -EOPNOTSUPP;
933}
934
935static int ovl_other_xattr_get(const struct xattr_handler *handler,
936 struct dentry *dentry, struct inode *inode,
937 const char *name, void *buffer, size_t size)
938{
939 return ovl_xattr_get(dentry, inode, name, buffer, size);
940}
941
942static int ovl_other_xattr_set(const struct xattr_handler *handler,
943 struct dentry *dentry, struct inode *inode,
944 const char *name, const void *value,
945 size_t size, int flags)
946{
947 return ovl_xattr_set(dentry, inode, name, value, size, flags);
948}
949
950static const struct xattr_handler __maybe_unused
951ovl_posix_acl_access_xattr_handler = {
952 .name = XATTR_NAME_POSIX_ACL_ACCESS,
953 .flags = ACL_TYPE_ACCESS,
954 .get = ovl_posix_acl_xattr_get,
955 .set = ovl_posix_acl_xattr_set,
956};
957
958static const struct xattr_handler __maybe_unused
959ovl_posix_acl_default_xattr_handler = {
960 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
961 .flags = ACL_TYPE_DEFAULT,
962 .get = ovl_posix_acl_xattr_get,
963 .set = ovl_posix_acl_xattr_set,
964};
965
966static const struct xattr_handler ovl_own_xattr_handler = {
967 .prefix = OVL_XATTR_PREFIX,
968 .get = ovl_own_xattr_get,
969 .set = ovl_own_xattr_set,
970};
971
972static const struct xattr_handler ovl_other_xattr_handler = {
973 .prefix = "",
974 .get = ovl_other_xattr_get,
975 .set = ovl_other_xattr_set,
976};
977
978static const struct xattr_handler *ovl_xattr_handlers[] = {
979#ifdef CONFIG_FS_POSIX_ACL
980 &ovl_posix_acl_access_xattr_handler,
981 &ovl_posix_acl_default_xattr_handler,
982#endif
983 &ovl_own_xattr_handler,
984 &ovl_other_xattr_handler,
985 NULL
986};
987
988static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
989 struct inode **ptrap, const char *name)
990{
991 struct inode *trap;
992 int err;
993
994 trap = ovl_get_trap_inode(sb, dir);
995 err = PTR_ERR_OR_ZERO(trap);
996 if (err) {
997 if (err == -ELOOP)
998 pr_err("overlayfs: conflicting %s path\n", name);
999 return err;
1000 }
1001
1002 *ptrap = trap;
1003 return 0;
1004}
1005
1006static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1007 struct path *upperpath)
1008{
1009 struct vfsmount *upper_mnt;
1010 int err;
1011
1012 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1013 if (err)
1014 goto out;
1015
1016
1017 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
1018 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1019 err = -EINVAL;
1020 goto out;
1021 }
1022
1023 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1024 if (err)
1025 goto out;
1026
1027 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap,
1028 "upperdir");
1029 if (err)
1030 goto out;
1031
1032 upper_mnt = clone_private_mount(upperpath);
1033 err = PTR_ERR(upper_mnt);
1034 if (IS_ERR(upper_mnt)) {
1035 pr_err("overlayfs: failed to clone upperpath\n");
1036 goto out;
1037 }
1038
1039
1040 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1041 ofs->upper_mnt = upper_mnt;
1042
1043 err = -EBUSY;
1044 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1045 ofs->upperdir_locked = true;
1046 } else if (ofs->config.index) {
1047 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1048 goto out;
1049 } else {
1050 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1051 }
1052
1053 err = 0;
1054out:
1055 return err;
1056}
1057
1058static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1059 struct path *workpath)
1060{
1061 struct vfsmount *mnt = ofs->upper_mnt;
1062 struct dentry *temp;
1063 int fh_type;
1064 int err;
1065
1066 err = mnt_want_write(mnt);
1067 if (err)
1068 return err;
1069
1070 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1071 if (!ofs->workdir)
1072 goto out;
1073
1074 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1075 if (err)
1076 goto out;
1077
1078
1079
1080
1081
1082
1083
1084 err = ovl_check_d_type_supported(workpath);
1085 if (err < 0)
1086 goto out;
1087
1088
1089
1090
1091
1092 if (!err)
1093 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1094
1095
1096 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1097 ofs->tmpfile = !IS_ERR(temp);
1098 if (ofs->tmpfile)
1099 dput(temp);
1100 else
1101 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1102
1103
1104
1105
1106 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
1107 if (err) {
1108 ofs->noxattr = true;
1109 ofs->config.index = false;
1110 ofs->config.metacopy = false;
1111 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1112 err = 0;
1113 } else {
1114 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
1115 }
1116
1117
1118 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1119 if (ofs->config.index && !fh_type) {
1120 ofs->config.index = false;
1121 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1122 }
1123
1124
1125 if (fh_type != FILEID_INO32_GEN)
1126 ofs->xino_bits = 0;
1127
1128
1129 if (ofs->config.nfs_export && !ofs->config.index) {
1130 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1131 ofs->config.nfs_export = false;
1132 }
1133out:
1134 mnt_drop_write(mnt);
1135 return err;
1136}
1137
1138static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1139 struct path *upperpath)
1140{
1141 int err;
1142 struct path workpath = { };
1143
1144 err = ovl_mount_dir(ofs->config.workdir, &workpath);
1145 if (err)
1146 goto out;
1147
1148 err = -EINVAL;
1149 if (upperpath->mnt != workpath.mnt) {
1150 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1151 goto out;
1152 }
1153 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1154 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1155 goto out;
1156 }
1157
1158 ofs->workbasedir = dget(workpath.dentry);
1159
1160 err = -EBUSY;
1161 if (ovl_inuse_trylock(ofs->workbasedir)) {
1162 ofs->workdir_locked = true;
1163 } else if (ofs->config.index) {
1164 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1165 goto out;
1166 } else {
1167 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1168 }
1169
1170 err = ovl_make_workdir(sb, ofs, &workpath);
1171
1172out:
1173 path_put(&workpath);
1174
1175 return err;
1176}
1177
1178static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1179 struct ovl_entry *oe, struct path *upperpath)
1180{
1181 struct vfsmount *mnt = ofs->upper_mnt;
1182 int err;
1183
1184 err = mnt_want_write(mnt);
1185 if (err)
1186 return err;
1187
1188
1189 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
1190 true);
1191 if (err) {
1192 pr_err("overlayfs: failed to verify upper root origin\n");
1193 goto out;
1194 }
1195
1196 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1197 if (ofs->indexdir) {
1198 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1199 "indexdir");
1200 if (err)
1201 goto out;
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211 if (ovl_check_origin_xattr(ofs->indexdir)) {
1212 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1213 upperpath->dentry, true, false);
1214 if (err)
1215 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1216 }
1217 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
1218 if (err)
1219 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1220
1221
1222 if (!err)
1223 err = ovl_indexdir_cleanup(ofs);
1224 }
1225 if (err || !ofs->indexdir)
1226 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1227
1228out:
1229 mnt_drop_write(mnt);
1230 return err;
1231}
1232
1233static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1234{
1235 unsigned int i;
1236
1237 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1238 return true;
1239
1240 for (i = 0; i < ofs->numlowerfs; i++) {
1241
1242
1243
1244
1245
1246 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1247 return false;
1248 }
1249 return true;
1250}
1251
1252
1253static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1254{
1255 struct super_block *sb = path->mnt->mnt_sb;
1256 unsigned int i;
1257 dev_t dev;
1258 int err;
1259
1260
1261 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1262 return 0;
1263
1264 for (i = 0; i < ofs->numlowerfs; i++) {
1265 if (ofs->lower_fs[i].sb == sb)
1266 return i + 1;
1267 }
1268
1269 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1270 ofs->config.index = false;
1271 ofs->config.nfs_export = false;
1272 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1273 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1274 path->dentry);
1275 }
1276
1277 err = get_anon_bdev(&dev);
1278 if (err) {
1279 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1280 return err;
1281 }
1282
1283 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1284 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1285 ofs->numlowerfs++;
1286
1287 return ofs->numlowerfs;
1288}
1289
1290static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs,
1291 struct path *stack, unsigned int numlower)
1292{
1293 int err;
1294 unsigned int i;
1295
1296 err = -ENOMEM;
1297 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1298 GFP_KERNEL);
1299 if (ofs->lower_layers == NULL)
1300 goto out;
1301
1302 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1303 GFP_KERNEL);
1304 if (ofs->lower_fs == NULL)
1305 goto out;
1306
1307 for (i = 0; i < numlower; i++) {
1308 struct vfsmount *mnt;
1309 struct inode *trap;
1310 int fsid;
1311
1312 err = fsid = ovl_get_fsid(ofs, &stack[i]);
1313 if (err < 0)
1314 goto out;
1315
1316 err = -EBUSY;
1317 if (ovl_is_inuse(stack[i].dentry)) {
1318 pr_err("overlayfs: lowerdir is in-use as upperdir/workdir\n");
1319 goto out;
1320 }
1321
1322 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1323 if (err)
1324 goto out;
1325
1326 mnt = clone_private_mount(&stack[i]);
1327 err = PTR_ERR(mnt);
1328 if (IS_ERR(mnt)) {
1329 pr_err("overlayfs: failed to clone lowerpath\n");
1330 iput(trap);
1331 goto out;
1332 }
1333
1334
1335
1336
1337
1338 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1339
1340 ofs->lower_layers[ofs->numlower].trap = trap;
1341 ofs->lower_layers[ofs->numlower].mnt = mnt;
1342 ofs->lower_layers[ofs->numlower].idx = i + 1;
1343 ofs->lower_layers[ofs->numlower].fsid = fsid;
1344 if (fsid) {
1345 ofs->lower_layers[ofs->numlower].fs =
1346 &ofs->lower_fs[fsid - 1];
1347 }
1348 ofs->numlower++;
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
1360 ofs->xino_bits = 0;
1361 ofs->config.xino = OVL_XINO_OFF;
1362 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1363
1364
1365
1366
1367
1368 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1369 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1370 }
1371
1372 if (ofs->xino_bits) {
1373 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1374 ofs->xino_bits);
1375 }
1376
1377 err = 0;
1378out:
1379 return err;
1380}
1381
1382static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1383 struct ovl_fs *ofs)
1384{
1385 int err;
1386 char *lowertmp, *lower;
1387 struct path *stack = NULL;
1388 unsigned int stacklen, numlower = 0, i;
1389 bool remote = false;
1390 struct ovl_entry *oe;
1391
1392 err = -ENOMEM;
1393 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1394 if (!lowertmp)
1395 goto out_err;
1396
1397 err = -EINVAL;
1398 stacklen = ovl_split_lowerdirs(lowertmp);
1399 if (stacklen > OVL_MAX_STACK) {
1400 pr_err("overlayfs: too many lower directories, limit is %d\n",
1401 OVL_MAX_STACK);
1402 goto out_err;
1403 } else if (!ofs->config.upperdir && stacklen == 1) {
1404 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1405 goto out_err;
1406 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1407 ofs->config.redirect_follow) {
1408 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1409 ofs->config.nfs_export = false;
1410 }
1411
1412 err = -ENOMEM;
1413 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1414 if (!stack)
1415 goto out_err;
1416
1417 err = -EINVAL;
1418 lower = lowertmp;
1419 for (numlower = 0; numlower < stacklen; numlower++) {
1420 err = ovl_lower_dir(lower, &stack[numlower], ofs,
1421 &sb->s_stack_depth, &remote);
1422 if (err)
1423 goto out_err;
1424
1425 lower = strchr(lower, '\0') + 1;
1426 }
1427
1428 err = -EINVAL;
1429 sb->s_stack_depth++;
1430 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1431 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1432 goto out_err;
1433 }
1434
1435 err = ovl_get_lower_layers(sb, ofs, stack, numlower);
1436 if (err)
1437 goto out_err;
1438
1439 err = -ENOMEM;
1440 oe = ovl_alloc_entry(numlower);
1441 if (!oe)
1442 goto out_err;
1443
1444 for (i = 0; i < numlower; i++) {
1445 oe->lowerstack[i].dentry = dget(stack[i].dentry);
1446 oe->lowerstack[i].layer = &ofs->lower_layers[i];
1447 }
1448
1449 if (remote)
1450 sb->s_d_op = &ovl_reval_dentry_operations;
1451 else
1452 sb->s_d_op = &ovl_dentry_operations;
1453
1454out:
1455 for (i = 0; i < numlower; i++)
1456 path_put(&stack[i]);
1457 kfree(stack);
1458 kfree(lowertmp);
1459
1460 return oe;
1461
1462out_err:
1463 oe = ERR_PTR(err);
1464 goto out;
1465}
1466
1467
1468
1469
1470
1471
1472static int ovl_check_layer(struct super_block *sb, struct dentry *dentry,
1473 const char *name)
1474{
1475 struct dentry *next = dentry, *parent;
1476 int err = 0;
1477
1478 if (!dentry)
1479 return 0;
1480
1481 parent = dget_parent(next);
1482
1483
1484 while (!err && parent != next) {
1485 if (ovl_is_inuse(parent)) {
1486 err = -EBUSY;
1487 pr_err("overlayfs: %s path overlapping in-use upperdir/workdir\n",
1488 name);
1489 } else if (ovl_lookup_trap_inode(sb, parent)) {
1490 err = -ELOOP;
1491 pr_err("overlayfs: overlapping %s path\n", name);
1492 }
1493 next = parent;
1494 parent = dget_parent(next);
1495 dput(next);
1496 }
1497
1498 dput(parent);
1499
1500 return err;
1501}
1502
1503
1504
1505
1506static int ovl_check_overlapping_layers(struct super_block *sb,
1507 struct ovl_fs *ofs)
1508{
1509 int i, err;
1510
1511 if (ofs->upper_mnt) {
1512 err = ovl_check_layer(sb, ofs->upper_mnt->mnt_root, "upperdir");
1513 if (err)
1514 return err;
1515
1516
1517
1518
1519
1520
1521
1522
1523 err = ovl_check_layer(sb, ofs->workbasedir, "workdir");
1524 if (err)
1525 return err;
1526 }
1527
1528 for (i = 0; i < ofs->numlower; i++) {
1529 err = ovl_check_layer(sb, ofs->lower_layers[i].mnt->mnt_root,
1530 "lowerdir");
1531 if (err)
1532 return err;
1533 }
1534
1535 return 0;
1536}
1537
1538static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1539{
1540 struct path upperpath = { };
1541 struct dentry *root_dentry;
1542 struct ovl_entry *oe;
1543 struct ovl_fs *ofs;
1544 struct cred *cred;
1545 int err;
1546
1547 err = -ENOMEM;
1548 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1549 if (!ofs)
1550 goto out;
1551
1552 ofs->creator_cred = cred = prepare_creds();
1553 if (!cred)
1554 goto out_err;
1555
1556 ofs->config.index = ovl_index_def;
1557 ofs->config.nfs_export = ovl_nfs_export_def;
1558 ofs->config.xino = ovl_xino_def();
1559 ofs->config.metacopy = ovl_metacopy_def;
1560 err = ovl_parse_opt((char *) data, &ofs->config);
1561 if (err)
1562 goto out_err;
1563
1564 err = -EINVAL;
1565 if (!ofs->config.lowerdir) {
1566 if (!silent)
1567 pr_err("overlayfs: missing 'lowerdir'\n");
1568 goto out_err;
1569 }
1570
1571 sb->s_stack_depth = 0;
1572 sb->s_maxbytes = MAX_LFS_FILESIZE;
1573
1574 if (ofs->config.xino != OVL_XINO_OFF)
1575 ofs->xino_bits = BITS_PER_LONG - 32;
1576
1577
1578 sb->s_op = &ovl_super_operations;
1579
1580 if (ofs->config.upperdir) {
1581 if (!ofs->config.workdir) {
1582 pr_err("overlayfs: missing 'workdir'\n");
1583 goto out_err;
1584 }
1585
1586 err = ovl_get_upper(sb, ofs, &upperpath);
1587 if (err)
1588 goto out_err;
1589
1590 err = ovl_get_workdir(sb, ofs, &upperpath);
1591 if (err)
1592 goto out_err;
1593
1594 if (!ofs->workdir)
1595 sb->s_flags |= SB_RDONLY;
1596
1597 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1598 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1599
1600 }
1601 oe = ovl_get_lowerstack(sb, ofs);
1602 err = PTR_ERR(oe);
1603 if (IS_ERR(oe))
1604 goto out_err;
1605
1606
1607 if (!ofs->upper_mnt)
1608 sb->s_flags |= SB_RDONLY;
1609
1610 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1611 err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
1612 if (err)
1613 goto out_free_oe;
1614
1615
1616 if (!ofs->indexdir) {
1617 dput(ofs->workdir);
1618 ofs->workdir = NULL;
1619 sb->s_flags |= SB_RDONLY;
1620 }
1621
1622 }
1623
1624 err = ovl_check_overlapping_layers(sb, ofs);
1625 if (err)
1626 goto out_free_oe;
1627
1628
1629 if (!ofs->indexdir) {
1630 ofs->config.index = false;
1631 if (ofs->upper_mnt && ofs->config.nfs_export) {
1632 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1633 ofs->config.nfs_export = false;
1634 }
1635 }
1636
1637 if (ofs->config.metacopy && ofs->config.nfs_export) {
1638 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1639 ofs->config.nfs_export = false;
1640 }
1641
1642 if (ofs->config.nfs_export)
1643 sb->s_export_op = &ovl_export_operations;
1644
1645
1646 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1647
1648 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1649 sb->s_xattr = ovl_xattr_handlers;
1650 sb->s_fs_info = ofs;
1651 sb->s_flags |= SB_POSIXACL;
1652
1653 err = -ENOMEM;
1654 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1655 if (!root_dentry)
1656 goto out_free_oe;
1657
1658 root_dentry->d_fsdata = oe;
1659
1660 mntput(upperpath.mnt);
1661 if (upperpath.dentry) {
1662 ovl_dentry_set_upper_alias(root_dentry);
1663 if (ovl_is_impuredir(upperpath.dentry))
1664 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1665 }
1666
1667
1668 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1669 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1670 ovl_set_upperdata(d_inode(root_dentry));
1671 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1672 ovl_dentry_lower(root_dentry), NULL);
1673
1674 sb->s_root = root_dentry;
1675
1676 return 0;
1677
1678out_free_oe:
1679 ovl_entry_stack_free(oe);
1680 kfree(oe);
1681out_err:
1682 path_put(&upperpath);
1683 ovl_free_fs(ofs);
1684out:
1685 return err;
1686}
1687
1688static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1689 const char *dev_name, void *raw_data)
1690{
1691 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1692}
1693
1694static struct file_system_type ovl_fs_type = {
1695 .owner = THIS_MODULE,
1696 .name = "overlay",
1697 .mount = ovl_mount,
1698 .kill_sb = kill_anon_super,
1699};
1700MODULE_ALIAS_FS("overlay");
1701
1702static void ovl_inode_init_once(void *foo)
1703{
1704 struct ovl_inode *oi = foo;
1705
1706 inode_init_once(&oi->vfs_inode);
1707}
1708
1709static int __init ovl_init(void)
1710{
1711 int err;
1712
1713 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1714 sizeof(struct ovl_inode), 0,
1715 (SLAB_RECLAIM_ACCOUNT|
1716 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1717 ovl_inode_init_once);
1718 if (ovl_inode_cachep == NULL)
1719 return -ENOMEM;
1720
1721 err = register_filesystem(&ovl_fs_type);
1722 if (err)
1723 kmem_cache_destroy(ovl_inode_cachep);
1724
1725 return err;
1726}
1727
1728static void __exit ovl_exit(void)
1729{
1730 unregister_filesystem(&ovl_fs_type);
1731
1732
1733
1734
1735
1736 rcu_barrier();
1737 kmem_cache_destroy(ovl_inode_cachep);
1738
1739}
1740
1741module_init(ovl_init);
1742module_exit(ovl_exit);
1743