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