]> Git Repo - linux.git/blob - fs/overlayfs/super.c
ovl: show redirect_dir mount option
[linux.git] / fs / overlayfs / super.c
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
10 #include <linux/fs.h>
11 #include <linux/namei.h>
12 #include <linux/xattr.h>
13 #include <linux/mount.h>
14 #include <linux/parser.h>
15 #include <linux/module.h>
16 #include <linux/statfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/posix_acl_xattr.h>
19 #include "overlayfs.h"
20 #include "ovl_entry.h"
21
22 MODULE_AUTHOR("Miklos Szeredi <[email protected]>");
23 MODULE_DESCRIPTION("Overlay filesystem");
24 MODULE_LICENSE("GPL");
25
26
27 struct ovl_dir_cache;
28
29 #define OVL_MAX_STACK 500
30
31 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33 MODULE_PARM_DESC(ovl_redirect_dir_def,
34                  "Default to on or off for the redirect_dir feature");
35
36 static void ovl_dentry_release(struct dentry *dentry)
37 {
38         struct ovl_entry *oe = dentry->d_fsdata;
39
40         if (oe) {
41                 unsigned int i;
42
43                 dput(oe->__upperdentry);
44                 kfree(oe->redirect);
45                 for (i = 0; i < oe->numlower; i++)
46                         dput(oe->lowerstack[i].dentry);
47                 kfree_rcu(oe, rcu);
48         }
49 }
50
51 static struct dentry *ovl_d_real(struct dentry *dentry,
52                                  const struct inode *inode,
53                                  unsigned int open_flags)
54 {
55         struct dentry *real;
56
57         if (!d_is_reg(dentry)) {
58                 if (!inode || inode == d_inode(dentry))
59                         return dentry;
60                 goto bug;
61         }
62
63         if (d_is_negative(dentry))
64                 return dentry;
65
66         if (open_flags) {
67                 int err = ovl_open_maybe_copy_up(dentry, open_flags);
68
69                 if (err)
70                         return ERR_PTR(err);
71         }
72
73         real = ovl_dentry_upper(dentry);
74         if (real && (!inode || inode == d_inode(real)))
75                 return real;
76
77         real = ovl_dentry_lower(dentry);
78         if (!real)
79                 goto bug;
80
81         /* Handle recursion */
82         real = d_real(real, inode, open_flags);
83
84         if (!inode || inode == d_inode(real))
85                 return real;
86 bug:
87         WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
88              inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
89         return dentry;
90 }
91
92 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
93 {
94         struct ovl_entry *oe = dentry->d_fsdata;
95         unsigned int i;
96         int ret = 1;
97
98         for (i = 0; i < oe->numlower; i++) {
99                 struct dentry *d = oe->lowerstack[i].dentry;
100
101                 if (d->d_flags & DCACHE_OP_REVALIDATE) {
102                         ret = d->d_op->d_revalidate(d, flags);
103                         if (ret < 0)
104                                 return ret;
105                         if (!ret) {
106                                 if (!(flags & LOOKUP_RCU))
107                                         d_invalidate(d);
108                                 return -ESTALE;
109                         }
110                 }
111         }
112         return 1;
113 }
114
115 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
116 {
117         struct ovl_entry *oe = dentry->d_fsdata;
118         unsigned int i;
119         int ret = 1;
120
121         for (i = 0; i < oe->numlower; i++) {
122                 struct dentry *d = oe->lowerstack[i].dentry;
123
124                 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
125                         ret = d->d_op->d_weak_revalidate(d, flags);
126                         if (ret <= 0)
127                                 break;
128                 }
129         }
130         return ret;
131 }
132
133 static const struct dentry_operations ovl_dentry_operations = {
134         .d_release = ovl_dentry_release,
135         .d_real = ovl_d_real,
136 };
137
138 static const struct dentry_operations ovl_reval_dentry_operations = {
139         .d_release = ovl_dentry_release,
140         .d_real = ovl_d_real,
141         .d_revalidate = ovl_dentry_revalidate,
142         .d_weak_revalidate = ovl_dentry_weak_revalidate,
143 };
144
145 static void ovl_put_super(struct super_block *sb)
146 {
147         struct ovl_fs *ufs = sb->s_fs_info;
148         unsigned i;
149
150         dput(ufs->workdir);
151         mntput(ufs->upper_mnt);
152         for (i = 0; i < ufs->numlower; i++)
153                 mntput(ufs->lower_mnt[i]);
154         kfree(ufs->lower_mnt);
155
156         kfree(ufs->config.lowerdir);
157         kfree(ufs->config.upperdir);
158         kfree(ufs->config.workdir);
159         put_cred(ufs->creator_cred);
160         kfree(ufs);
161 }
162
163 /**
164  * ovl_statfs
165  * @sb: The overlayfs super block
166  * @buf: The struct kstatfs to fill in with stats
167  *
168  * Get the filesystem statistics.  As writes always target the upper layer
169  * filesystem pass the statfs to the upper filesystem (if it exists)
170  */
171 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
172 {
173         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
174         struct dentry *root_dentry = dentry->d_sb->s_root;
175         struct path path;
176         int err;
177
178         ovl_path_real(root_dentry, &path);
179
180         err = vfs_statfs(&path, buf);
181         if (!err) {
182                 buf->f_namelen = ofs->namelen;
183                 buf->f_type = OVERLAYFS_SUPER_MAGIC;
184         }
185
186         return err;
187 }
188
189 /**
190  * ovl_show_options
191  *
192  * Prints the mount options for a given superblock.
193  * Returns zero; does not fail.
194  */
195 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
196 {
197         struct super_block *sb = dentry->d_sb;
198         struct ovl_fs *ufs = sb->s_fs_info;
199
200         seq_show_option(m, "lowerdir", ufs->config.lowerdir);
201         if (ufs->config.upperdir) {
202                 seq_show_option(m, "upperdir", ufs->config.upperdir);
203                 seq_show_option(m, "workdir", ufs->config.workdir);
204         }
205         if (ufs->config.default_permissions)
206                 seq_puts(m, ",default_permissions");
207         if (ufs->config.redirect_dir != ovl_redirect_dir_def)
208                 seq_printf(m, ",redirect_dir=%s",
209                            ufs->config.redirect_dir ? "on" : "off");
210         return 0;
211 }
212
213 static int ovl_remount(struct super_block *sb, int *flags, char *data)
214 {
215         struct ovl_fs *ufs = sb->s_fs_info;
216
217         if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
218                 return -EROFS;
219
220         return 0;
221 }
222
223 static const struct super_operations ovl_super_operations = {
224         .put_super      = ovl_put_super,
225         .statfs         = ovl_statfs,
226         .show_options   = ovl_show_options,
227         .remount_fs     = ovl_remount,
228         .drop_inode     = generic_delete_inode,
229 };
230
231 enum {
232         OPT_LOWERDIR,
233         OPT_UPPERDIR,
234         OPT_WORKDIR,
235         OPT_DEFAULT_PERMISSIONS,
236         OPT_REDIRECT_DIR_ON,
237         OPT_REDIRECT_DIR_OFF,
238         OPT_ERR,
239 };
240
241 static const match_table_t ovl_tokens = {
242         {OPT_LOWERDIR,                  "lowerdir=%s"},
243         {OPT_UPPERDIR,                  "upperdir=%s"},
244         {OPT_WORKDIR,                   "workdir=%s"},
245         {OPT_DEFAULT_PERMISSIONS,       "default_permissions"},
246         {OPT_REDIRECT_DIR_ON,           "redirect_dir=on"},
247         {OPT_REDIRECT_DIR_OFF,          "redirect_dir=off"},
248         {OPT_ERR,                       NULL}
249 };
250
251 static char *ovl_next_opt(char **s)
252 {
253         char *sbegin = *s;
254         char *p;
255
256         if (sbegin == NULL)
257                 return NULL;
258
259         for (p = sbegin; *p; p++) {
260                 if (*p == '\\') {
261                         p++;
262                         if (!*p)
263                                 break;
264                 } else if (*p == ',') {
265                         *p = '\0';
266                         *s = p + 1;
267                         return sbegin;
268                 }
269         }
270         *s = NULL;
271         return sbegin;
272 }
273
274 static int ovl_parse_opt(char *opt, struct ovl_config *config)
275 {
276         char *p;
277
278         while ((p = ovl_next_opt(&opt)) != NULL) {
279                 int token;
280                 substring_t args[MAX_OPT_ARGS];
281
282                 if (!*p)
283                         continue;
284
285                 token = match_token(p, ovl_tokens, args);
286                 switch (token) {
287                 case OPT_UPPERDIR:
288                         kfree(config->upperdir);
289                         config->upperdir = match_strdup(&args[0]);
290                         if (!config->upperdir)
291                                 return -ENOMEM;
292                         break;
293
294                 case OPT_LOWERDIR:
295                         kfree(config->lowerdir);
296                         config->lowerdir = match_strdup(&args[0]);
297                         if (!config->lowerdir)
298                                 return -ENOMEM;
299                         break;
300
301                 case OPT_WORKDIR:
302                         kfree(config->workdir);
303                         config->workdir = match_strdup(&args[0]);
304                         if (!config->workdir)
305                                 return -ENOMEM;
306                         break;
307
308                 case OPT_DEFAULT_PERMISSIONS:
309                         config->default_permissions = true;
310                         break;
311
312                 case OPT_REDIRECT_DIR_ON:
313                         config->redirect_dir = true;
314                         break;
315
316                 case OPT_REDIRECT_DIR_OFF:
317                         config->redirect_dir = false;
318                         break;
319
320                 default:
321                         pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
322                         return -EINVAL;
323                 }
324         }
325
326         /* Workdir is useless in non-upper mount */
327         if (!config->upperdir && config->workdir) {
328                 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
329                         config->workdir);
330                 kfree(config->workdir);
331                 config->workdir = NULL;
332         }
333
334         return 0;
335 }
336
337 #define OVL_WORKDIR_NAME "work"
338
339 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
340                                          struct dentry *dentry)
341 {
342         struct inode *dir = dentry->d_inode;
343         struct dentry *work;
344         int err;
345         bool retried = false;
346
347         err = mnt_want_write(mnt);
348         if (err)
349                 return ERR_PTR(err);
350
351         inode_lock_nested(dir, I_MUTEX_PARENT);
352 retry:
353         work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
354                               strlen(OVL_WORKDIR_NAME));
355
356         if (!IS_ERR(work)) {
357                 struct kstat stat = {
358                         .mode = S_IFDIR | 0,
359                 };
360                 struct iattr attr = {
361                         .ia_valid = ATTR_MODE,
362                         .ia_mode = stat.mode,
363                 };
364
365                 if (work->d_inode) {
366                         err = -EEXIST;
367                         if (retried)
368                                 goto out_dput;
369
370                         retried = true;
371                         ovl_workdir_cleanup(dir, mnt, work, 0);
372                         dput(work);
373                         goto retry;
374                 }
375
376                 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
377                 if (err)
378                         goto out_dput;
379
380                 /*
381                  * Try to remove POSIX ACL xattrs from workdir.  We are good if:
382                  *
383                  * a) success (there was a POSIX ACL xattr and was removed)
384                  * b) -ENODATA (there was no POSIX ACL xattr)
385                  * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
386                  *
387                  * There are various other error values that could effectively
388                  * mean that the xattr doesn't exist (e.g. -ERANGE is returned
389                  * if the xattr name is too long), but the set of filesystems
390                  * allowed as upper are limited to "normal" ones, where checking
391                  * for the above two errors is sufficient.
392                  */
393                 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
394                 if (err && err != -ENODATA && err != -EOPNOTSUPP)
395                         goto out_dput;
396
397                 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
398                 if (err && err != -ENODATA && err != -EOPNOTSUPP)
399                         goto out_dput;
400
401                 /* Clear any inherited mode bits */
402                 inode_lock(work->d_inode);
403                 err = notify_change(work, &attr, NULL);
404                 inode_unlock(work->d_inode);
405                 if (err)
406                         goto out_dput;
407         }
408 out_unlock:
409         inode_unlock(dir);
410         mnt_drop_write(mnt);
411
412         return work;
413
414 out_dput:
415         dput(work);
416         work = ERR_PTR(err);
417         goto out_unlock;
418 }
419
420 static void ovl_unescape(char *s)
421 {
422         char *d = s;
423
424         for (;; s++, d++) {
425                 if (*s == '\\')
426                         s++;
427                 *d = *s;
428                 if (!*s)
429                         break;
430         }
431 }
432
433 static int ovl_mount_dir_noesc(const char *name, struct path *path)
434 {
435         int err = -EINVAL;
436
437         if (!*name) {
438                 pr_err("overlayfs: empty lowerdir\n");
439                 goto out;
440         }
441         err = kern_path(name, LOOKUP_FOLLOW, path);
442         if (err) {
443                 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
444                 goto out;
445         }
446         err = -EINVAL;
447         if (ovl_dentry_weird(path->dentry)) {
448                 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
449                 goto out_put;
450         }
451         if (!d_is_dir(path->dentry)) {
452                 pr_err("overlayfs: '%s' not a directory\n", name);
453                 goto out_put;
454         }
455         return 0;
456
457 out_put:
458         path_put(path);
459 out:
460         return err;
461 }
462
463 static int ovl_mount_dir(const char *name, struct path *path)
464 {
465         int err = -ENOMEM;
466         char *tmp = kstrdup(name, GFP_KERNEL);
467
468         if (tmp) {
469                 ovl_unescape(tmp);
470                 err = ovl_mount_dir_noesc(tmp, path);
471
472                 if (!err)
473                         if (ovl_dentry_remote(path->dentry)) {
474                                 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
475                                        tmp);
476                                 path_put(path);
477                                 err = -EINVAL;
478                         }
479                 kfree(tmp);
480         }
481         return err;
482 }
483
484 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
485                              const char *name)
486 {
487         struct kstatfs statfs;
488         int err = vfs_statfs(path, &statfs);
489
490         if (err)
491                 pr_err("overlayfs: statfs failed on '%s'\n", name);
492         else
493                 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
494
495         return err;
496 }
497
498 static int ovl_lower_dir(const char *name, struct path *path,
499                          struct ovl_fs *ofs, int *stack_depth, bool *remote)
500 {
501         int err;
502
503         err = ovl_mount_dir_noesc(name, path);
504         if (err)
505                 goto out;
506
507         err = ovl_check_namelen(path, ofs, name);
508         if (err)
509                 goto out_put;
510
511         *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
512
513         if (ovl_dentry_remote(path->dentry))
514                 *remote = true;
515
516         return 0;
517
518 out_put:
519         path_put(path);
520 out:
521         return err;
522 }
523
524 /* Workdir should not be subdir of upperdir and vice versa */
525 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
526 {
527         bool ok = false;
528
529         if (workdir != upperdir) {
530                 ok = (lock_rename(workdir, upperdir) == NULL);
531                 unlock_rename(workdir, upperdir);
532         }
533         return ok;
534 }
535
536 static unsigned int ovl_split_lowerdirs(char *str)
537 {
538         unsigned int ctr = 1;
539         char *s, *d;
540
541         for (s = d = str;; s++, d++) {
542                 if (*s == '\\') {
543                         s++;
544                 } else if (*s == ':') {
545                         *d = '\0';
546                         ctr++;
547                         continue;
548                 }
549                 *d = *s;
550                 if (!*s)
551                         break;
552         }
553         return ctr;
554 }
555
556 static int __maybe_unused
557 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
558                         struct dentry *dentry, struct inode *inode,
559                         const char *name, void *buffer, size_t size)
560 {
561         return ovl_xattr_get(dentry, handler->name, buffer, size);
562 }
563
564 static int __maybe_unused
565 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
566                         struct dentry *dentry, struct inode *inode,
567                         const char *name, const void *value,
568                         size_t size, int flags)
569 {
570         struct dentry *workdir = ovl_workdir(dentry);
571         struct inode *realinode = ovl_inode_real(inode, NULL);
572         struct posix_acl *acl = NULL;
573         int err;
574
575         /* Check that everything is OK before copy-up */
576         if (value) {
577                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
578                 if (IS_ERR(acl))
579                         return PTR_ERR(acl);
580         }
581         err = -EOPNOTSUPP;
582         if (!IS_POSIXACL(d_inode(workdir)))
583                 goto out_acl_release;
584         if (!realinode->i_op->set_acl)
585                 goto out_acl_release;
586         if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
587                 err = acl ? -EACCES : 0;
588                 goto out_acl_release;
589         }
590         err = -EPERM;
591         if (!inode_owner_or_capable(inode))
592                 goto out_acl_release;
593
594         posix_acl_release(acl);
595
596         /*
597          * Check if sgid bit needs to be cleared (actual setacl operation will
598          * be done with mounter's capabilities and so that won't do it for us).
599          */
600         if (unlikely(inode->i_mode & S_ISGID) &&
601             handler->flags == ACL_TYPE_ACCESS &&
602             !in_group_p(inode->i_gid) &&
603             !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
604                 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
605
606                 err = ovl_setattr(dentry, &iattr);
607                 if (err)
608                         return err;
609         }
610
611         err = ovl_xattr_set(dentry, handler->name, value, size, flags);
612         if (!err)
613                 ovl_copyattr(ovl_inode_real(inode, NULL), inode);
614
615         return err;
616
617 out_acl_release:
618         posix_acl_release(acl);
619         return err;
620 }
621
622 static int ovl_own_xattr_get(const struct xattr_handler *handler,
623                              struct dentry *dentry, struct inode *inode,
624                              const char *name, void *buffer, size_t size)
625 {
626         return -EOPNOTSUPP;
627 }
628
629 static int ovl_own_xattr_set(const struct xattr_handler *handler,
630                              struct dentry *dentry, struct inode *inode,
631                              const char *name, const void *value,
632                              size_t size, int flags)
633 {
634         return -EOPNOTSUPP;
635 }
636
637 static int ovl_other_xattr_get(const struct xattr_handler *handler,
638                                struct dentry *dentry, struct inode *inode,
639                                const char *name, void *buffer, size_t size)
640 {
641         return ovl_xattr_get(dentry, name, buffer, size);
642 }
643
644 static int ovl_other_xattr_set(const struct xattr_handler *handler,
645                                struct dentry *dentry, struct inode *inode,
646                                const char *name, const void *value,
647                                size_t size, int flags)
648 {
649         return ovl_xattr_set(dentry, name, value, size, flags);
650 }
651
652 static const struct xattr_handler __maybe_unused
653 ovl_posix_acl_access_xattr_handler = {
654         .name = XATTR_NAME_POSIX_ACL_ACCESS,
655         .flags = ACL_TYPE_ACCESS,
656         .get = ovl_posix_acl_xattr_get,
657         .set = ovl_posix_acl_xattr_set,
658 };
659
660 static const struct xattr_handler __maybe_unused
661 ovl_posix_acl_default_xattr_handler = {
662         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
663         .flags = ACL_TYPE_DEFAULT,
664         .get = ovl_posix_acl_xattr_get,
665         .set = ovl_posix_acl_xattr_set,
666 };
667
668 static const struct xattr_handler ovl_own_xattr_handler = {
669         .prefix = OVL_XATTR_PREFIX,
670         .get = ovl_own_xattr_get,
671         .set = ovl_own_xattr_set,
672 };
673
674 static const struct xattr_handler ovl_other_xattr_handler = {
675         .prefix = "", /* catch all */
676         .get = ovl_other_xattr_get,
677         .set = ovl_other_xattr_set,
678 };
679
680 static const struct xattr_handler *ovl_xattr_handlers[] = {
681 #ifdef CONFIG_FS_POSIX_ACL
682         &ovl_posix_acl_access_xattr_handler,
683         &ovl_posix_acl_default_xattr_handler,
684 #endif
685         &ovl_own_xattr_handler,
686         &ovl_other_xattr_handler,
687         NULL
688 };
689
690 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
691 {
692         struct path upperpath = { NULL, NULL };
693         struct path workpath = { NULL, NULL };
694         struct dentry *root_dentry;
695         struct inode *realinode;
696         struct ovl_entry *oe;
697         struct ovl_fs *ufs;
698         struct path *stack = NULL;
699         char *lowertmp;
700         char *lower;
701         unsigned int numlower;
702         unsigned int stacklen = 0;
703         unsigned int i;
704         bool remote = false;
705         int err;
706
707         err = -ENOMEM;
708         ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
709         if (!ufs)
710                 goto out;
711
712         ufs->config.redirect_dir = ovl_redirect_dir_def;
713         err = ovl_parse_opt((char *) data, &ufs->config);
714         if (err)
715                 goto out_free_config;
716
717         err = -EINVAL;
718         if (!ufs->config.lowerdir) {
719                 if (!silent)
720                         pr_err("overlayfs: missing 'lowerdir'\n");
721                 goto out_free_config;
722         }
723
724         sb->s_stack_depth = 0;
725         sb->s_maxbytes = MAX_LFS_FILESIZE;
726         if (ufs->config.upperdir) {
727                 if (!ufs->config.workdir) {
728                         pr_err("overlayfs: missing 'workdir'\n");
729                         goto out_free_config;
730                 }
731
732                 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
733                 if (err)
734                         goto out_free_config;
735
736                 /* Upper fs should not be r/o */
737                 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
738                         pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
739                         err = -EINVAL;
740                         goto out_put_upperpath;
741                 }
742
743                 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
744                 if (err)
745                         goto out_put_upperpath;
746
747                 err = ovl_mount_dir(ufs->config.workdir, &workpath);
748                 if (err)
749                         goto out_put_upperpath;
750
751                 err = -EINVAL;
752                 if (upperpath.mnt != workpath.mnt) {
753                         pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
754                         goto out_put_workpath;
755                 }
756                 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
757                         pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
758                         goto out_put_workpath;
759                 }
760                 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
761         }
762         err = -ENOMEM;
763         lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
764         if (!lowertmp)
765                 goto out_put_workpath;
766
767         err = -EINVAL;
768         stacklen = ovl_split_lowerdirs(lowertmp);
769         if (stacklen > OVL_MAX_STACK) {
770                 pr_err("overlayfs: too many lower directories, limit is %d\n",
771                        OVL_MAX_STACK);
772                 goto out_free_lowertmp;
773         } else if (!ufs->config.upperdir && stacklen == 1) {
774                 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
775                 goto out_free_lowertmp;
776         }
777
778         stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
779         if (!stack)
780                 goto out_free_lowertmp;
781
782         lower = lowertmp;
783         for (numlower = 0; numlower < stacklen; numlower++) {
784                 err = ovl_lower_dir(lower, &stack[numlower], ufs,
785                                     &sb->s_stack_depth, &remote);
786                 if (err)
787                         goto out_put_lowerpath;
788
789                 lower = strchr(lower, '\0') + 1;
790         }
791
792         err = -EINVAL;
793         sb->s_stack_depth++;
794         if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
795                 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
796                 goto out_put_lowerpath;
797         }
798
799         if (ufs->config.upperdir) {
800                 ufs->upper_mnt = clone_private_mount(&upperpath);
801                 err = PTR_ERR(ufs->upper_mnt);
802                 if (IS_ERR(ufs->upper_mnt)) {
803                         pr_err("overlayfs: failed to clone upperpath\n");
804                         goto out_put_lowerpath;
805                 }
806                 /* Don't inherit atime flags */
807                 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
808
809                 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
810
811                 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
812                 err = PTR_ERR(ufs->workdir);
813                 if (IS_ERR(ufs->workdir)) {
814                         pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
815                                 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
816                         sb->s_flags |= MS_RDONLY;
817                         ufs->workdir = NULL;
818                 }
819
820                 /*
821                  * Upper should support d_type, else whiteouts are visible.
822                  * Given workdir and upper are on same fs, we can do
823                  * iterate_dir() on workdir. This check requires successful
824                  * creation of workdir in previous step.
825                  */
826                 if (ufs->workdir) {
827                         err = ovl_check_d_type_supported(&workpath);
828                         if (err < 0)
829                                 goto out_put_workdir;
830
831                         /*
832                          * We allowed this configuration and don't want to
833                          * break users over kernel upgrade. So warn instead
834                          * of erroring out.
835                          */
836                         if (!err)
837                                 pr_warn("overlayfs: upper fs needs to support d_type.\n");
838                 }
839         }
840
841         err = -ENOMEM;
842         ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
843         if (ufs->lower_mnt == NULL)
844                 goto out_put_workdir;
845         for (i = 0; i < numlower; i++) {
846                 struct vfsmount *mnt = clone_private_mount(&stack[i]);
847
848                 err = PTR_ERR(mnt);
849                 if (IS_ERR(mnt)) {
850                         pr_err("overlayfs: failed to clone lowerpath\n");
851                         goto out_put_lower_mnt;
852                 }
853                 /*
854                  * Make lower_mnt R/O.  That way fchmod/fchown on lower file
855                  * will fail instead of modifying lower fs.
856                  */
857                 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
858
859                 ufs->lower_mnt[ufs->numlower] = mnt;
860                 ufs->numlower++;
861         }
862
863         /* If the upper fs is nonexistent, we mark overlayfs r/o too */
864         if (!ufs->upper_mnt)
865                 sb->s_flags |= MS_RDONLY;
866
867         if (remote)
868                 sb->s_d_op = &ovl_reval_dentry_operations;
869         else
870                 sb->s_d_op = &ovl_dentry_operations;
871
872         ufs->creator_cred = prepare_creds();
873         if (!ufs->creator_cred)
874                 goto out_put_lower_mnt;
875
876         err = -ENOMEM;
877         oe = ovl_alloc_entry(numlower);
878         if (!oe)
879                 goto out_put_cred;
880
881         sb->s_magic = OVERLAYFS_SUPER_MAGIC;
882         sb->s_op = &ovl_super_operations;
883         sb->s_xattr = ovl_xattr_handlers;
884         sb->s_fs_info = ufs;
885         sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
886
887         root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
888         if (!root_dentry)
889                 goto out_free_oe;
890
891         mntput(upperpath.mnt);
892         for (i = 0; i < numlower; i++)
893                 mntput(stack[i].mnt);
894         path_put(&workpath);
895         kfree(lowertmp);
896
897         oe->__upperdentry = upperpath.dentry;
898         for (i = 0; i < numlower; i++) {
899                 oe->lowerstack[i].dentry = stack[i].dentry;
900                 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
901         }
902         kfree(stack);
903
904         root_dentry->d_fsdata = oe;
905
906         realinode = d_inode(ovl_dentry_real(root_dentry));
907         ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
908         ovl_copyattr(realinode, d_inode(root_dentry));
909
910         sb->s_root = root_dentry;
911
912         return 0;
913
914 out_free_oe:
915         kfree(oe);
916 out_put_cred:
917         put_cred(ufs->creator_cred);
918 out_put_lower_mnt:
919         for (i = 0; i < ufs->numlower; i++)
920                 mntput(ufs->lower_mnt[i]);
921         kfree(ufs->lower_mnt);
922 out_put_workdir:
923         dput(ufs->workdir);
924         mntput(ufs->upper_mnt);
925 out_put_lowerpath:
926         for (i = 0; i < numlower; i++)
927                 path_put(&stack[i]);
928         kfree(stack);
929 out_free_lowertmp:
930         kfree(lowertmp);
931 out_put_workpath:
932         path_put(&workpath);
933 out_put_upperpath:
934         path_put(&upperpath);
935 out_free_config:
936         kfree(ufs->config.lowerdir);
937         kfree(ufs->config.upperdir);
938         kfree(ufs->config.workdir);
939         kfree(ufs);
940 out:
941         return err;
942 }
943
944 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
945                                 const char *dev_name, void *raw_data)
946 {
947         return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
948 }
949
950 static struct file_system_type ovl_fs_type = {
951         .owner          = THIS_MODULE,
952         .name           = "overlay",
953         .mount          = ovl_mount,
954         .kill_sb        = kill_anon_super,
955 };
956 MODULE_ALIAS_FS("overlay");
957
958 static int __init ovl_init(void)
959 {
960         return register_filesystem(&ovl_fs_type);
961 }
962
963 static void __exit ovl_exit(void)
964 {
965         unregister_filesystem(&ovl_fs_type);
966 }
967
968 module_init(ovl_init);
969 module_exit(ovl_exit);
This page took 0.091774 seconds and 4 git commands to generate.