ovl: add mutli-layer infrastructure
[deliverable/linux.git] / fs / overlayfs / super.c
CommitLineData
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
10#include <linux/fs.h>
11#include <linux/namei.h>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/mount.h>
15#include <linux/slab.h>
16#include <linux/parser.h>
17#include <linux/module.h>
18#include <linux/sched.h>
cc259639 19#include <linux/statfs.h>
f45827e8 20#include <linux/seq_file.h>
e9be9d5e
MS
21#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
ef94b186 27#define OVERLAYFS_SUPER_MAGIC 0x794c7630
cc259639 28
f45827e8
EZ
29struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
33};
34
e9be9d5e
MS
35/* private information held for overlayfs's superblock */
36struct ovl_fs {
37 struct vfsmount *upper_mnt;
dd662667
MS
38 unsigned numlower;
39 struct vfsmount **lower_mnt;
e9be9d5e 40 struct dentry *workdir;
cc259639 41 long lower_namelen;
f45827e8
EZ
42 /* pathnames of lower and upper dirs, for show_options */
43 struct ovl_config config;
e9be9d5e
MS
44};
45
46struct ovl_dir_cache;
47
48/* private information held for every overlayfs dentry */
49struct ovl_entry {
50 struct dentry *__upperdentry;
e9be9d5e
MS
51 struct ovl_dir_cache *cache;
52 union {
53 struct {
54 u64 version;
55 bool opaque;
56 };
57 struct rcu_head rcu;
58 };
dd662667
MS
59 unsigned numlower;
60 struct path lowerstack[];
e9be9d5e
MS
61};
62
63const char *ovl_opaque_xattr = "trusted.overlay.opaque";
64
dd662667
MS
65static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66{
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68}
e9be9d5e
MS
69
70enum ovl_path_type ovl_path_type(struct dentry *dentry)
71{
72 struct ovl_entry *oe = dentry->d_fsdata;
1afaba1e 73 enum ovl_path_type type = 0;
e9be9d5e
MS
74
75 if (oe->__upperdentry) {
1afaba1e
MS
76 type = __OVL_PATH_UPPER;
77
dd662667 78 if (oe->numlower) {
e9be9d5e 79 if (S_ISDIR(dentry->d_inode->i_mode))
1afaba1e
MS
80 type |= __OVL_PATH_MERGE;
81 } else if (!oe->opaque) {
82 type |= __OVL_PATH_PURE;
e9be9d5e 83 }
e9be9d5e 84 }
1afaba1e 85 return type;
e9be9d5e
MS
86}
87
88static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
89{
71d50928 90 return lockless_dereference(oe->__upperdentry);
e9be9d5e
MS
91}
92
93void ovl_path_upper(struct dentry *dentry, struct path *path)
94{
95 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
96 struct ovl_entry *oe = dentry->d_fsdata;
97
98 path->mnt = ofs->upper_mnt;
99 path->dentry = ovl_upperdentry_dereference(oe);
100}
101
102enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
103{
104
105 enum ovl_path_type type = ovl_path_type(dentry);
106
1afaba1e 107 if (!OVL_TYPE_UPPER(type))
e9be9d5e
MS
108 ovl_path_lower(dentry, path);
109 else
110 ovl_path_upper(dentry, path);
111
112 return type;
113}
114
115struct dentry *ovl_dentry_upper(struct dentry *dentry)
116{
117 struct ovl_entry *oe = dentry->d_fsdata;
118
119 return ovl_upperdentry_dereference(oe);
120}
121
122struct dentry *ovl_dentry_lower(struct dentry *dentry)
123{
124 struct ovl_entry *oe = dentry->d_fsdata;
125
dd662667 126 return __ovl_dentry_lower(oe);
e9be9d5e
MS
127}
128
129struct dentry *ovl_dentry_real(struct dentry *dentry)
130{
131 struct ovl_entry *oe = dentry->d_fsdata;
132 struct dentry *realdentry;
133
134 realdentry = ovl_upperdentry_dereference(oe);
135 if (!realdentry)
dd662667 136 realdentry = __ovl_dentry_lower(oe);
e9be9d5e
MS
137
138 return realdentry;
139}
140
141struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
142{
143 struct dentry *realdentry;
144
145 realdentry = ovl_upperdentry_dereference(oe);
146 if (realdentry) {
147 *is_upper = true;
148 } else {
dd662667 149 realdentry = __ovl_dentry_lower(oe);
e9be9d5e
MS
150 *is_upper = false;
151 }
152 return realdentry;
153}
154
155struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
156{
157 struct ovl_entry *oe = dentry->d_fsdata;
158
159 return oe->cache;
160}
161
162void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
163{
164 struct ovl_entry *oe = dentry->d_fsdata;
165
166 oe->cache = cache;
167}
168
169void ovl_path_lower(struct dentry *dentry, struct path *path)
170{
e9be9d5e
MS
171 struct ovl_entry *oe = dentry->d_fsdata;
172
dd662667 173 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
e9be9d5e
MS
174}
175
176int ovl_want_write(struct dentry *dentry)
177{
178 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
179 return mnt_want_write(ofs->upper_mnt);
180}
181
182void ovl_drop_write(struct dentry *dentry)
183{
184 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
185 mnt_drop_write(ofs->upper_mnt);
186}
187
188struct dentry *ovl_workdir(struct dentry *dentry)
189{
190 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
191 return ofs->workdir;
192}
193
194bool ovl_dentry_is_opaque(struct dentry *dentry)
195{
196 struct ovl_entry *oe = dentry->d_fsdata;
197 return oe->opaque;
198}
199
200void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
201{
202 struct ovl_entry *oe = dentry->d_fsdata;
203 oe->opaque = opaque;
204}
205
206void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
207{
208 struct ovl_entry *oe = dentry->d_fsdata;
209
210 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
211 WARN_ON(oe->__upperdentry);
212 BUG_ON(!upperdentry->d_inode);
213 /*
214 * Make sure upperdentry is consistent before making it visible to
215 * ovl_upperdentry_dereference().
216 */
217 smp_wmb();
218 oe->__upperdentry = upperdentry;
219}
220
221void ovl_dentry_version_inc(struct dentry *dentry)
222{
223 struct ovl_entry *oe = dentry->d_fsdata;
224
225 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
226 oe->version++;
227}
228
229u64 ovl_dentry_version_get(struct dentry *dentry)
230{
231 struct ovl_entry *oe = dentry->d_fsdata;
232
233 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
234 return oe->version;
235}
236
237bool ovl_is_whiteout(struct dentry *dentry)
238{
239 struct inode *inode = dentry->d_inode;
240
241 return inode && IS_WHITEOUT(inode);
242}
243
244static bool ovl_is_opaquedir(struct dentry *dentry)
245{
246 int res;
247 char val;
248 struct inode *inode = dentry->d_inode;
249
250 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
251 return false;
252
253 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
254 if (res == 1 && val == 'y')
255 return true;
256
257 return false;
258}
259
260static void ovl_dentry_release(struct dentry *dentry)
261{
262 struct ovl_entry *oe = dentry->d_fsdata;
263
264 if (oe) {
dd662667
MS
265 unsigned int i;
266
e9be9d5e 267 dput(oe->__upperdentry);
dd662667
MS
268 for (i = 0; i < oe->numlower; i++)
269 dput(oe->lowerstack[i].dentry);
e9be9d5e
MS
270 kfree_rcu(oe, rcu);
271 }
272}
273
274static const struct dentry_operations ovl_dentry_operations = {
275 .d_release = ovl_dentry_release,
276};
277
dd662667 278static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
e9be9d5e 279{
dd662667
MS
280 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
281 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
282
283 if (oe)
284 oe->numlower = numlower;
285
286 return oe;
e9be9d5e
MS
287}
288
289static inline struct dentry *ovl_lookup_real(struct dentry *dir,
290 struct qstr *name)
291{
292 struct dentry *dentry;
293
294 mutex_lock(&dir->d_inode->i_mutex);
295 dentry = lookup_one_len(name->name, dir, name->len);
296 mutex_unlock(&dir->d_inode->i_mutex);
297
298 if (IS_ERR(dentry)) {
299 if (PTR_ERR(dentry) == -ENOENT)
300 dentry = NULL;
301 } else if (!dentry->d_inode) {
302 dput(dentry);
303 dentry = NULL;
304 }
305 return dentry;
306}
307
308struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
309 unsigned int flags)
310{
311 struct ovl_entry *oe;
312 struct dentry *upperdir;
dd662667 313 struct path lowerdir;
e9be9d5e
MS
314 struct dentry *upperdentry = NULL;
315 struct dentry *lowerdentry = NULL;
316 struct inode *inode = NULL;
317 int err;
318
319 err = -ENOMEM;
dd662667 320 oe = ovl_alloc_entry(1);
e9be9d5e
MS
321 if (!oe)
322 goto out;
323
324 upperdir = ovl_dentry_upper(dentry->d_parent);
dd662667 325 ovl_path_lower(dentry->d_parent, &lowerdir);
e9be9d5e
MS
326
327 if (upperdir) {
328 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
329 err = PTR_ERR(upperdentry);
330 if (IS_ERR(upperdentry))
331 goto out_put_dir;
332
dd662667 333 if (lowerdir.dentry && upperdentry) {
e9be9d5e
MS
334 if (ovl_is_whiteout(upperdentry)) {
335 dput(upperdentry);
336 upperdentry = NULL;
337 oe->opaque = true;
338 } else if (ovl_is_opaquedir(upperdentry)) {
339 oe->opaque = true;
340 }
341 }
342 }
dd662667
MS
343 if (lowerdir.dentry && !oe->opaque) {
344 lowerdentry = ovl_lookup_real(lowerdir.dentry, &dentry->d_name);
e9be9d5e
MS
345 err = PTR_ERR(lowerdentry);
346 if (IS_ERR(lowerdentry))
347 goto out_dput_upper;
348 }
349
350 if (lowerdentry && upperdentry &&
351 (!S_ISDIR(upperdentry->d_inode->i_mode) ||
352 !S_ISDIR(lowerdentry->d_inode->i_mode))) {
353 dput(lowerdentry);
354 lowerdentry = NULL;
355 oe->opaque = true;
356 }
357
358 if (lowerdentry || upperdentry) {
359 struct dentry *realdentry;
360
361 realdentry = upperdentry ? upperdentry : lowerdentry;
362 err = -ENOMEM;
363 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
364 oe);
365 if (!inode)
366 goto out_dput;
367 ovl_copyattr(realdentry->d_inode, inode);
368 }
369
370 oe->__upperdentry = upperdentry;
dd662667
MS
371 if (lowerdentry) {
372 oe->lowerstack[0].dentry = lowerdentry;
373 oe->lowerstack[0].mnt = lowerdir.mnt;
374 } else {
375 oe->numlower = 0;
376 }
e9be9d5e
MS
377 dentry->d_fsdata = oe;
378 d_add(dentry, inode);
379
380 return NULL;
381
382out_dput:
383 dput(lowerdentry);
384out_dput_upper:
385 dput(upperdentry);
386out_put_dir:
387 kfree(oe);
388out:
389 return ERR_PTR(err);
390}
391
392struct file *ovl_path_open(struct path *path, int flags)
393{
394 return dentry_open(path, flags, current_cred());
395}
396
397static void ovl_put_super(struct super_block *sb)
398{
399 struct ovl_fs *ufs = sb->s_fs_info;
dd662667 400 unsigned i;
e9be9d5e
MS
401
402 dput(ufs->workdir);
403 mntput(ufs->upper_mnt);
dd662667
MS
404 for (i = 0; i < ufs->numlower; i++)
405 mntput(ufs->lower_mnt[i]);
e9be9d5e 406
f45827e8
EZ
407 kfree(ufs->config.lowerdir);
408 kfree(ufs->config.upperdir);
409 kfree(ufs->config.workdir);
e9be9d5e
MS
410 kfree(ufs);
411}
412
cc259639
AW
413/**
414 * ovl_statfs
415 * @sb: The overlayfs super block
416 * @buf: The struct kstatfs to fill in with stats
417 *
418 * Get the filesystem statistics. As writes always target the upper layer
419 * filesystem pass the statfs to the same filesystem.
420 */
421static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
422{
423 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
424 struct dentry *root_dentry = dentry->d_sb->s_root;
425 struct path path;
426 int err;
427
428 ovl_path_upper(root_dentry, &path);
429
430 err = vfs_statfs(&path, buf);
431 if (!err) {
432 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
433 buf->f_type = OVERLAYFS_SUPER_MAGIC;
434 }
435
436 return err;
437}
438
f45827e8
EZ
439/**
440 * ovl_show_options
441 *
442 * Prints the mount options for a given superblock.
443 * Returns zero; does not fail.
444 */
445static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
446{
447 struct super_block *sb = dentry->d_sb;
448 struct ovl_fs *ufs = sb->s_fs_info;
449
450 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
451 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
452 seq_printf(m, ",workdir=%s", ufs->config.workdir);
453 return 0;
454}
455
e9be9d5e
MS
456static const struct super_operations ovl_super_operations = {
457 .put_super = ovl_put_super,
cc259639 458 .statfs = ovl_statfs,
f45827e8 459 .show_options = ovl_show_options,
e9be9d5e
MS
460};
461
462enum {
463 OPT_LOWERDIR,
464 OPT_UPPERDIR,
465 OPT_WORKDIR,
466 OPT_ERR,
467};
468
469static const match_table_t ovl_tokens = {
470 {OPT_LOWERDIR, "lowerdir=%s"},
471 {OPT_UPPERDIR, "upperdir=%s"},
472 {OPT_WORKDIR, "workdir=%s"},
473 {OPT_ERR, NULL}
474};
475
91c77947
MS
476static char *ovl_next_opt(char **s)
477{
478 char *sbegin = *s;
479 char *p;
480
481 if (sbegin == NULL)
482 return NULL;
483
484 for (p = sbegin; *p; p++) {
485 if (*p == '\\') {
486 p++;
487 if (!*p)
488 break;
489 } else if (*p == ',') {
490 *p = '\0';
491 *s = p + 1;
492 return sbegin;
493 }
494 }
495 *s = NULL;
496 return sbegin;
497}
498
e9be9d5e
MS
499static int ovl_parse_opt(char *opt, struct ovl_config *config)
500{
501 char *p;
502
91c77947 503 while ((p = ovl_next_opt(&opt)) != NULL) {
e9be9d5e
MS
504 int token;
505 substring_t args[MAX_OPT_ARGS];
506
507 if (!*p)
508 continue;
509
510 token = match_token(p, ovl_tokens, args);
511 switch (token) {
512 case OPT_UPPERDIR:
513 kfree(config->upperdir);
514 config->upperdir = match_strdup(&args[0]);
515 if (!config->upperdir)
516 return -ENOMEM;
517 break;
518
519 case OPT_LOWERDIR:
520 kfree(config->lowerdir);
521 config->lowerdir = match_strdup(&args[0]);
522 if (!config->lowerdir)
523 return -ENOMEM;
524 break;
525
526 case OPT_WORKDIR:
527 kfree(config->workdir);
528 config->workdir = match_strdup(&args[0]);
529 if (!config->workdir)
530 return -ENOMEM;
531 break;
532
533 default:
534 return -EINVAL;
535 }
536 }
537 return 0;
538}
539
540#define OVL_WORKDIR_NAME "work"
541
542static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
543 struct dentry *dentry)
544{
545 struct inode *dir = dentry->d_inode;
546 struct dentry *work;
547 int err;
548 bool retried = false;
549
550 err = mnt_want_write(mnt);
551 if (err)
552 return ERR_PTR(err);
553
554 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
555retry:
556 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
557 strlen(OVL_WORKDIR_NAME));
558
559 if (!IS_ERR(work)) {
560 struct kstat stat = {
561 .mode = S_IFDIR | 0,
562 };
563
564 if (work->d_inode) {
565 err = -EEXIST;
566 if (retried)
567 goto out_dput;
568
569 retried = true;
570 ovl_cleanup(dir, work);
571 dput(work);
572 goto retry;
573 }
574
575 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
576 if (err)
577 goto out_dput;
578 }
579out_unlock:
580 mutex_unlock(&dir->i_mutex);
581 mnt_drop_write(mnt);
582
583 return work;
584
585out_dput:
586 dput(work);
587 work = ERR_PTR(err);
588 goto out_unlock;
589}
590
91c77947
MS
591static void ovl_unescape(char *s)
592{
593 char *d = s;
594
595 for (;; s++, d++) {
596 if (*s == '\\')
597 s++;
598 *d = *s;
599 if (!*s)
600 break;
601 }
602}
603
e9be9d5e
MS
604static int ovl_mount_dir(const char *name, struct path *path)
605{
606 int err;
91c77947
MS
607 char *tmp = kstrdup(name, GFP_KERNEL);
608
609 if (!tmp)
610 return -ENOMEM;
e9be9d5e 611
91c77947
MS
612 ovl_unescape(tmp);
613 err = kern_path(tmp, LOOKUP_FOLLOW, path);
e9be9d5e 614 if (err) {
91c77947 615 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
e9be9d5e
MS
616 err = -EINVAL;
617 }
91c77947 618 kfree(tmp);
e9be9d5e
MS
619 return err;
620}
621
622static bool ovl_is_allowed_fs_type(struct dentry *root)
623{
624 const struct dentry_operations *dop = root->d_op;
625
626 /*
627 * We don't support:
628 * - automount filesystems
629 * - filesystems with revalidate (FIXME for lower layer)
630 * - filesystems with case insensitive names
631 */
632 if (dop &&
633 (dop->d_manage || dop->d_automount ||
634 dop->d_revalidate || dop->d_weak_revalidate ||
635 dop->d_compare || dop->d_hash)) {
636 return false;
637 }
638 return true;
639}
640
641/* Workdir should not be subdir of upperdir and vice versa */
642static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
643{
644 bool ok = false;
645
646 if (workdir != upperdir) {
647 ok = (lock_rename(workdir, upperdir) == NULL);
648 unlock_rename(workdir, upperdir);
649 }
650 return ok;
651}
652
653static int ovl_fill_super(struct super_block *sb, void *data, int silent)
654{
655 struct path lowerpath;
656 struct path upperpath;
657 struct path workpath;
658 struct inode *root_inode;
659 struct dentry *root_dentry;
660 struct ovl_entry *oe;
661 struct ovl_fs *ufs;
cc259639 662 struct kstatfs statfs;
dd662667
MS
663 struct vfsmount *mnt;
664 unsigned int i;
e9be9d5e
MS
665 int err;
666
f45827e8
EZ
667 err = -ENOMEM;
668 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
669 if (!ufs)
e9be9d5e
MS
670 goto out;
671
f45827e8
EZ
672 err = ovl_parse_opt((char *) data, &ufs->config);
673 if (err)
674 goto out_free_config;
675
e9be9d5e
MS
676 /* FIXME: workdir is not needed for a R/O mount */
677 err = -EINVAL;
f45827e8
EZ
678 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
679 !ufs->config.workdir) {
e9be9d5e
MS
680 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
681 goto out_free_config;
682 }
683
684 err = -ENOMEM;
dd662667 685 oe = ovl_alloc_entry(1);
e9be9d5e 686 if (oe == NULL)
f45827e8 687 goto out_free_config;
e9be9d5e 688
f45827e8 689 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
e9be9d5e
MS
690 if (err)
691 goto out_free_oe;
692
f45827e8 693 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
e9be9d5e
MS
694 if (err)
695 goto out_put_upperpath;
696
f45827e8 697 err = ovl_mount_dir(ufs->config.workdir, &workpath);
e9be9d5e
MS
698 if (err)
699 goto out_put_lowerpath;
700
701 err = -EINVAL;
702 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
703 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
704 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
705 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
706 goto out_put_workpath;
707 }
708
709 if (upperpath.mnt != workpath.mnt) {
710 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
711 goto out_put_workpath;
712 }
713 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
714 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
715 goto out_put_workpath;
716 }
717
718 if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
719 pr_err("overlayfs: filesystem of upperdir is not supported\n");
720 goto out_put_workpath;
721 }
722
723 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
724 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
725 goto out_put_workpath;
726 }
727
cc259639
AW
728 err = vfs_statfs(&lowerpath, &statfs);
729 if (err) {
730 pr_err("overlayfs: statfs failed on lowerpath\n");
731 goto out_put_workpath;
732 }
733 ufs->lower_namelen = statfs.f_namelen;
734
69c433ed
MS
735 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
736 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
737
738 err = -EINVAL;
739 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
740 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
741 goto out_put_workpath;
742 }
743
e9be9d5e
MS
744 ufs->upper_mnt = clone_private_mount(&upperpath);
745 err = PTR_ERR(ufs->upper_mnt);
746 if (IS_ERR(ufs->upper_mnt)) {
747 pr_err("overlayfs: failed to clone upperpath\n");
748 goto out_put_workpath;
749 }
750
dd662667
MS
751 ufs->lower_mnt = kcalloc(1, sizeof(struct vfsmount *), GFP_KERNEL);
752 if (ufs->lower_mnt == NULL)
e9be9d5e 753 goto out_put_upper_mnt;
dd662667
MS
754
755 mnt = clone_private_mount(&lowerpath);
756 err = PTR_ERR(mnt);
757 if (IS_ERR(mnt)) {
758 pr_err("overlayfs: failed to clone lowerpath\n");
759 goto out_put_lower_mnt;
e9be9d5e 760 }
dd662667
MS
761 /*
762 * Make lower_mnt R/O. That way fchmod/fchown on lower file
763 * will fail instead of modifying lower fs.
764 */
765 mnt->mnt_flags |= MNT_READONLY;
766
767 ufs->lower_mnt[0] = mnt;
768 ufs->numlower = 1;
e9be9d5e
MS
769
770 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
771 err = PTR_ERR(ufs->workdir);
772 if (IS_ERR(ufs->workdir)) {
773 pr_err("overlayfs: failed to create directory %s/%s\n",
f45827e8 774 ufs->config.workdir, OVL_WORKDIR_NAME);
e9be9d5e
MS
775 goto out_put_lower_mnt;
776 }
777
e9be9d5e
MS
778 /* If the upper fs is r/o, we mark overlayfs r/o too */
779 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
780 sb->s_flags |= MS_RDONLY;
781
782 sb->s_d_op = &ovl_dentry_operations;
783
784 err = -ENOMEM;
785 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
786 if (!root_inode)
787 goto out_put_workdir;
788
789 root_dentry = d_make_root(root_inode);
790 if (!root_dentry)
791 goto out_put_workdir;
792
793 mntput(upperpath.mnt);
794 mntput(lowerpath.mnt);
795 path_put(&workpath);
796
797 oe->__upperdentry = upperpath.dentry;
dd662667
MS
798 oe->lowerstack[0].dentry = lowerpath.dentry;
799 oe->lowerstack[0].mnt = ufs->lower_mnt[0];
e9be9d5e
MS
800
801 root_dentry->d_fsdata = oe;
802
cc259639 803 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
e9be9d5e
MS
804 sb->s_op = &ovl_super_operations;
805 sb->s_root = root_dentry;
806 sb->s_fs_info = ufs;
807
808 return 0;
809
810out_put_workdir:
811 dput(ufs->workdir);
812out_put_lower_mnt:
dd662667
MS
813 for (i = 0; i < ufs->numlower; i++)
814 mntput(ufs->lower_mnt[i]);
815 kfree(ufs->lower_mnt);
e9be9d5e
MS
816out_put_upper_mnt:
817 mntput(ufs->upper_mnt);
818out_put_workpath:
819 path_put(&workpath);
820out_put_lowerpath:
821 path_put(&lowerpath);
822out_put_upperpath:
823 path_put(&upperpath);
824out_free_oe:
825 kfree(oe);
e9be9d5e 826out_free_config:
f45827e8
EZ
827 kfree(ufs->config.lowerdir);
828 kfree(ufs->config.upperdir);
829 kfree(ufs->config.workdir);
830 kfree(ufs);
e9be9d5e
MS
831out:
832 return err;
833}
834
835static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
836 const char *dev_name, void *raw_data)
837{
838 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
839}
840
841static struct file_system_type ovl_fs_type = {
842 .owner = THIS_MODULE,
ef94b186 843 .name = "overlay",
e9be9d5e
MS
844 .mount = ovl_mount,
845 .kill_sb = kill_anon_super,
846};
ef94b186 847MODULE_ALIAS_FS("overlay");
e9be9d5e
MS
848
849static int __init ovl_init(void)
850{
851 return register_filesystem(&ovl_fs_type);
852}
853
854static void __exit ovl_exit(void)
855{
856 unregister_filesystem(&ovl_fs_type);
857}
858
859module_init(ovl_init);
860module_exit(ovl_exit);
This page took 0.064224 seconds and 5 git commands to generate.