blob: 821719cc85374c417c0228144ecbf6827484dd1e [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;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010067 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020068
69 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010070 type = __OVL_PATH_UPPER;
71
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072 if (oe->lowerdentry) {
73 if (S_ISDIR(dentry->d_inode->i_mode))
Miklos Szeredi1afaba12014-12-13 00:59:42 +010074 type |= __OVL_PATH_MERGE;
75 } else if (!oe->opaque) {
76 type |= __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020077 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +020078 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010079 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020080}
81
82static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
83{
Miklos Szeredi71d50922014-11-20 16:40:01 +010084 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020085}
86
87void ovl_path_upper(struct dentry *dentry, struct path *path)
88{
89 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
90 struct ovl_entry *oe = dentry->d_fsdata;
91
92 path->mnt = ofs->upper_mnt;
93 path->dentry = ovl_upperdentry_dereference(oe);
94}
95
96enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
97{
98
99 enum ovl_path_type type = ovl_path_type(dentry);
100
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100101 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200102 ovl_path_lower(dentry, path);
103 else
104 ovl_path_upper(dentry, path);
105
106 return type;
107}
108
109struct dentry *ovl_dentry_upper(struct dentry *dentry)
110{
111 struct ovl_entry *oe = dentry->d_fsdata;
112
113 return ovl_upperdentry_dereference(oe);
114}
115
116struct dentry *ovl_dentry_lower(struct dentry *dentry)
117{
118 struct ovl_entry *oe = dentry->d_fsdata;
119
120 return oe->lowerdentry;
121}
122
123struct dentry *ovl_dentry_real(struct dentry *dentry)
124{
125 struct ovl_entry *oe = dentry->d_fsdata;
126 struct dentry *realdentry;
127
128 realdentry = ovl_upperdentry_dereference(oe);
129 if (!realdentry)
130 realdentry = oe->lowerdentry;
131
132 return realdentry;
133}
134
135struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
136{
137 struct dentry *realdentry;
138
139 realdentry = ovl_upperdentry_dereference(oe);
140 if (realdentry) {
141 *is_upper = true;
142 } else {
143 realdentry = oe->lowerdentry;
144 *is_upper = false;
145 }
146 return realdentry;
147}
148
149struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
150{
151 struct ovl_entry *oe = dentry->d_fsdata;
152
153 return oe->cache;
154}
155
156void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
157{
158 struct ovl_entry *oe = dentry->d_fsdata;
159
160 oe->cache = cache;
161}
162
163void ovl_path_lower(struct dentry *dentry, struct path *path)
164{
165 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
166 struct ovl_entry *oe = dentry->d_fsdata;
167
168 path->mnt = ofs->lower_mnt;
169 path->dentry = oe->lowerdentry;
170}
171
172int ovl_want_write(struct dentry *dentry)
173{
174 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
175 return mnt_want_write(ofs->upper_mnt);
176}
177
178void ovl_drop_write(struct dentry *dentry)
179{
180 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
181 mnt_drop_write(ofs->upper_mnt);
182}
183
184struct dentry *ovl_workdir(struct dentry *dentry)
185{
186 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
187 return ofs->workdir;
188}
189
190bool ovl_dentry_is_opaque(struct dentry *dentry)
191{
192 struct ovl_entry *oe = dentry->d_fsdata;
193 return oe->opaque;
194}
195
196void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
197{
198 struct ovl_entry *oe = dentry->d_fsdata;
199 oe->opaque = opaque;
200}
201
202void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
203{
204 struct ovl_entry *oe = dentry->d_fsdata;
205
206 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
207 WARN_ON(oe->__upperdentry);
208 BUG_ON(!upperdentry->d_inode);
209 /*
210 * Make sure upperdentry is consistent before making it visible to
211 * ovl_upperdentry_dereference().
212 */
213 smp_wmb();
214 oe->__upperdentry = upperdentry;
215}
216
217void ovl_dentry_version_inc(struct dentry *dentry)
218{
219 struct ovl_entry *oe = dentry->d_fsdata;
220
221 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
222 oe->version++;
223}
224
225u64 ovl_dentry_version_get(struct dentry *dentry)
226{
227 struct ovl_entry *oe = dentry->d_fsdata;
228
229 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
230 return oe->version;
231}
232
233bool ovl_is_whiteout(struct dentry *dentry)
234{
235 struct inode *inode = dentry->d_inode;
236
237 return inode && IS_WHITEOUT(inode);
238}
239
240static bool ovl_is_opaquedir(struct dentry *dentry)
241{
242 int res;
243 char val;
244 struct inode *inode = dentry->d_inode;
245
246 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
247 return false;
248
249 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
250 if (res == 1 && val == 'y')
251 return true;
252
253 return false;
254}
255
256static void ovl_dentry_release(struct dentry *dentry)
257{
258 struct ovl_entry *oe = dentry->d_fsdata;
259
260 if (oe) {
261 dput(oe->__upperdentry);
262 dput(oe->lowerdentry);
263 kfree_rcu(oe, rcu);
264 }
265}
266
267static const struct dentry_operations ovl_dentry_operations = {
268 .d_release = ovl_dentry_release,
269};
270
271static struct ovl_entry *ovl_alloc_entry(void)
272{
273 return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
274}
275
276static inline struct dentry *ovl_lookup_real(struct dentry *dir,
277 struct qstr *name)
278{
279 struct dentry *dentry;
280
281 mutex_lock(&dir->d_inode->i_mutex);
282 dentry = lookup_one_len(name->name, dir, name->len);
283 mutex_unlock(&dir->d_inode->i_mutex);
284
285 if (IS_ERR(dentry)) {
286 if (PTR_ERR(dentry) == -ENOENT)
287 dentry = NULL;
288 } else if (!dentry->d_inode) {
289 dput(dentry);
290 dentry = NULL;
291 }
292 return dentry;
293}
294
295struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
296 unsigned int flags)
297{
298 struct ovl_entry *oe;
299 struct dentry *upperdir;
300 struct dentry *lowerdir;
301 struct dentry *upperdentry = NULL;
302 struct dentry *lowerdentry = NULL;
303 struct inode *inode = NULL;
304 int err;
305
306 err = -ENOMEM;
307 oe = ovl_alloc_entry();
308 if (!oe)
309 goto out;
310
311 upperdir = ovl_dentry_upper(dentry->d_parent);
312 lowerdir = ovl_dentry_lower(dentry->d_parent);
313
314 if (upperdir) {
315 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
316 err = PTR_ERR(upperdentry);
317 if (IS_ERR(upperdentry))
318 goto out_put_dir;
319
320 if (lowerdir && upperdentry) {
321 if (ovl_is_whiteout(upperdentry)) {
322 dput(upperdentry);
323 upperdentry = NULL;
324 oe->opaque = true;
325 } else if (ovl_is_opaquedir(upperdentry)) {
326 oe->opaque = true;
327 }
328 }
329 }
330 if (lowerdir && !oe->opaque) {
331 lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
332 err = PTR_ERR(lowerdentry);
333 if (IS_ERR(lowerdentry))
334 goto out_dput_upper;
335 }
336
337 if (lowerdentry && upperdentry &&
338 (!S_ISDIR(upperdentry->d_inode->i_mode) ||
339 !S_ISDIR(lowerdentry->d_inode->i_mode))) {
340 dput(lowerdentry);
341 lowerdentry = NULL;
342 oe->opaque = true;
343 }
344
345 if (lowerdentry || upperdentry) {
346 struct dentry *realdentry;
347
348 realdentry = upperdentry ? upperdentry : lowerdentry;
349 err = -ENOMEM;
350 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
351 oe);
352 if (!inode)
353 goto out_dput;
354 ovl_copyattr(realdentry->d_inode, inode);
355 }
356
357 oe->__upperdentry = upperdentry;
358 oe->lowerdentry = lowerdentry;
359
360 dentry->d_fsdata = oe;
361 d_add(dentry, inode);
362
363 return NULL;
364
365out_dput:
366 dput(lowerdentry);
367out_dput_upper:
368 dput(upperdentry);
369out_put_dir:
370 kfree(oe);
371out:
372 return ERR_PTR(err);
373}
374
375struct file *ovl_path_open(struct path *path, int flags)
376{
377 return dentry_open(path, flags, current_cred());
378}
379
380static void ovl_put_super(struct super_block *sb)
381{
382 struct ovl_fs *ufs = sb->s_fs_info;
383
384 dput(ufs->workdir);
385 mntput(ufs->upper_mnt);
386 mntput(ufs->lower_mnt);
387
Erez Zadokf45827e82014-10-24 00:14:38 +0200388 kfree(ufs->config.lowerdir);
389 kfree(ufs->config.upperdir);
390 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391 kfree(ufs);
392}
393
Andy Whitcroftcc259632014-10-24 00:14:38 +0200394/**
395 * ovl_statfs
396 * @sb: The overlayfs super block
397 * @buf: The struct kstatfs to fill in with stats
398 *
399 * Get the filesystem statistics. As writes always target the upper layer
400 * filesystem pass the statfs to the same filesystem.
401 */
402static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
403{
404 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
405 struct dentry *root_dentry = dentry->d_sb->s_root;
406 struct path path;
407 int err;
408
409 ovl_path_upper(root_dentry, &path);
410
411 err = vfs_statfs(&path, buf);
412 if (!err) {
413 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
414 buf->f_type = OVERLAYFS_SUPER_MAGIC;
415 }
416
417 return err;
418}
419
Erez Zadokf45827e82014-10-24 00:14:38 +0200420/**
421 * ovl_show_options
422 *
423 * Prints the mount options for a given superblock.
424 * Returns zero; does not fail.
425 */
426static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
427{
428 struct super_block *sb = dentry->d_sb;
429 struct ovl_fs *ufs = sb->s_fs_info;
430
431 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
432 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
433 seq_printf(m, ",workdir=%s", ufs->config.workdir);
434 return 0;
435}
436
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200437static const struct super_operations ovl_super_operations = {
438 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200439 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200440 .show_options = ovl_show_options,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441};
442
443enum {
444 OPT_LOWERDIR,
445 OPT_UPPERDIR,
446 OPT_WORKDIR,
447 OPT_ERR,
448};
449
450static const match_table_t ovl_tokens = {
451 {OPT_LOWERDIR, "lowerdir=%s"},
452 {OPT_UPPERDIR, "upperdir=%s"},
453 {OPT_WORKDIR, "workdir=%s"},
454 {OPT_ERR, NULL}
455};
456
Miklos Szeredi91c77942014-11-20 16:40:00 +0100457static char *ovl_next_opt(char **s)
458{
459 char *sbegin = *s;
460 char *p;
461
462 if (sbegin == NULL)
463 return NULL;
464
465 for (p = sbegin; *p; p++) {
466 if (*p == '\\') {
467 p++;
468 if (!*p)
469 break;
470 } else if (*p == ',') {
471 *p = '\0';
472 *s = p + 1;
473 return sbegin;
474 }
475 }
476 *s = NULL;
477 return sbegin;
478}
479
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480static int ovl_parse_opt(char *opt, struct ovl_config *config)
481{
482 char *p;
483
Miklos Szeredi91c77942014-11-20 16:40:00 +0100484 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200485 int token;
486 substring_t args[MAX_OPT_ARGS];
487
488 if (!*p)
489 continue;
490
491 token = match_token(p, ovl_tokens, args);
492 switch (token) {
493 case OPT_UPPERDIR:
494 kfree(config->upperdir);
495 config->upperdir = match_strdup(&args[0]);
496 if (!config->upperdir)
497 return -ENOMEM;
498 break;
499
500 case OPT_LOWERDIR:
501 kfree(config->lowerdir);
502 config->lowerdir = match_strdup(&args[0]);
503 if (!config->lowerdir)
504 return -ENOMEM;
505 break;
506
507 case OPT_WORKDIR:
508 kfree(config->workdir);
509 config->workdir = match_strdup(&args[0]);
510 if (!config->workdir)
511 return -ENOMEM;
512 break;
513
514 default:
515 return -EINVAL;
516 }
517 }
518 return 0;
519}
520
521#define OVL_WORKDIR_NAME "work"
522
523static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
524 struct dentry *dentry)
525{
526 struct inode *dir = dentry->d_inode;
527 struct dentry *work;
528 int err;
529 bool retried = false;
530
531 err = mnt_want_write(mnt);
532 if (err)
533 return ERR_PTR(err);
534
535 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
536retry:
537 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
538 strlen(OVL_WORKDIR_NAME));
539
540 if (!IS_ERR(work)) {
541 struct kstat stat = {
542 .mode = S_IFDIR | 0,
543 };
544
545 if (work->d_inode) {
546 err = -EEXIST;
547 if (retried)
548 goto out_dput;
549
550 retried = true;
551 ovl_cleanup(dir, work);
552 dput(work);
553 goto retry;
554 }
555
556 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
557 if (err)
558 goto out_dput;
559 }
560out_unlock:
561 mutex_unlock(&dir->i_mutex);
562 mnt_drop_write(mnt);
563
564 return work;
565
566out_dput:
567 dput(work);
568 work = ERR_PTR(err);
569 goto out_unlock;
570}
571
Miklos Szeredi91c77942014-11-20 16:40:00 +0100572static void ovl_unescape(char *s)
573{
574 char *d = s;
575
576 for (;; s++, d++) {
577 if (*s == '\\')
578 s++;
579 *d = *s;
580 if (!*s)
581 break;
582 }
583}
584
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585static int ovl_mount_dir(const char *name, struct path *path)
586{
587 int err;
Miklos Szeredi91c77942014-11-20 16:40:00 +0100588 char *tmp = kstrdup(name, GFP_KERNEL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200589
Miklos Szeredi91c77942014-11-20 16:40:00 +0100590 if (!tmp)
591 return -ENOMEM;
592
593 ovl_unescape(tmp);
594 err = kern_path(tmp, LOOKUP_FOLLOW, path);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200595 if (err) {
Miklos Szeredi91c77942014-11-20 16:40:00 +0100596 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200597 err = -EINVAL;
598 }
Miklos Szeredi91c77942014-11-20 16:40:00 +0100599 kfree(tmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200600 return err;
601}
602
603static bool ovl_is_allowed_fs_type(struct dentry *root)
604{
605 const struct dentry_operations *dop = root->d_op;
606
607 /*
608 * We don't support:
609 * - automount filesystems
610 * - filesystems with revalidate (FIXME for lower layer)
611 * - filesystems with case insensitive names
612 */
613 if (dop &&
614 (dop->d_manage || dop->d_automount ||
615 dop->d_revalidate || dop->d_weak_revalidate ||
616 dop->d_compare || dop->d_hash)) {
617 return false;
618 }
619 return true;
620}
621
622/* Workdir should not be subdir of upperdir and vice versa */
623static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
624{
625 bool ok = false;
626
627 if (workdir != upperdir) {
628 ok = (lock_rename(workdir, upperdir) == NULL);
629 unlock_rename(workdir, upperdir);
630 }
631 return ok;
632}
633
634static int ovl_fill_super(struct super_block *sb, void *data, int silent)
635{
636 struct path lowerpath;
637 struct path upperpath;
638 struct path workpath;
639 struct inode *root_inode;
640 struct dentry *root_dentry;
641 struct ovl_entry *oe;
642 struct ovl_fs *ufs;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200643 struct kstatfs statfs;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200644 int err;
645
Erez Zadokf45827e82014-10-24 00:14:38 +0200646 err = -ENOMEM;
647 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
648 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200649 goto out;
650
Erez Zadokf45827e82014-10-24 00:14:38 +0200651 err = ovl_parse_opt((char *) data, &ufs->config);
652 if (err)
653 goto out_free_config;
654
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200655 /* FIXME: workdir is not needed for a R/O mount */
656 err = -EINVAL;
Erez Zadokf45827e82014-10-24 00:14:38 +0200657 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
658 !ufs->config.workdir) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200659 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
660 goto out_free_config;
661 }
662
663 err = -ENOMEM;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200664 oe = ovl_alloc_entry();
665 if (oe == NULL)
Erez Zadokf45827e82014-10-24 00:14:38 +0200666 goto out_free_config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200667
Erez Zadokf45827e82014-10-24 00:14:38 +0200668 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200669 if (err)
670 goto out_free_oe;
671
Erez Zadokf45827e82014-10-24 00:14:38 +0200672 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200673 if (err)
674 goto out_put_upperpath;
675
Erez Zadokf45827e82014-10-24 00:14:38 +0200676 err = ovl_mount_dir(ufs->config.workdir, &workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200677 if (err)
678 goto out_put_lowerpath;
679
680 err = -EINVAL;
681 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
682 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
683 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
684 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
685 goto out_put_workpath;
686 }
687
688 if (upperpath.mnt != workpath.mnt) {
689 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
690 goto out_put_workpath;
691 }
692 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
693 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
694 goto out_put_workpath;
695 }
696
697 if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
698 pr_err("overlayfs: filesystem of upperdir is not supported\n");
699 goto out_put_workpath;
700 }
701
702 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
703 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
704 goto out_put_workpath;
705 }
706
Andy Whitcroftcc259632014-10-24 00:14:38 +0200707 err = vfs_statfs(&lowerpath, &statfs);
708 if (err) {
709 pr_err("overlayfs: statfs failed on lowerpath\n");
710 goto out_put_workpath;
711 }
712 ufs->lower_namelen = statfs.f_namelen;
713
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200714 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
715 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
716
717 err = -EINVAL;
718 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
719 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
720 goto out_put_workpath;
721 }
722
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200723 ufs->upper_mnt = clone_private_mount(&upperpath);
724 err = PTR_ERR(ufs->upper_mnt);
725 if (IS_ERR(ufs->upper_mnt)) {
726 pr_err("overlayfs: failed to clone upperpath\n");
727 goto out_put_workpath;
728 }
729
730 ufs->lower_mnt = clone_private_mount(&lowerpath);
731 err = PTR_ERR(ufs->lower_mnt);
732 if (IS_ERR(ufs->lower_mnt)) {
733 pr_err("overlayfs: failed to clone lowerpath\n");
734 goto out_put_upper_mnt;
735 }
736
737 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
738 err = PTR_ERR(ufs->workdir);
739 if (IS_ERR(ufs->workdir)) {
740 pr_err("overlayfs: failed to create directory %s/%s\n",
Erez Zadokf45827e82014-10-24 00:14:38 +0200741 ufs->config.workdir, OVL_WORKDIR_NAME);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200742 goto out_put_lower_mnt;
743 }
744
745 /*
746 * Make lower_mnt R/O. That way fchmod/fchown on lower file
747 * will fail instead of modifying lower fs.
748 */
749 ufs->lower_mnt->mnt_flags |= MNT_READONLY;
750
751 /* If the upper fs is r/o, we mark overlayfs r/o too */
752 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
753 sb->s_flags |= MS_RDONLY;
754
755 sb->s_d_op = &ovl_dentry_operations;
756
757 err = -ENOMEM;
758 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
759 if (!root_inode)
760 goto out_put_workdir;
761
762 root_dentry = d_make_root(root_inode);
763 if (!root_dentry)
764 goto out_put_workdir;
765
766 mntput(upperpath.mnt);
767 mntput(lowerpath.mnt);
768 path_put(&workpath);
769
770 oe->__upperdentry = upperpath.dentry;
771 oe->lowerdentry = lowerpath.dentry;
772
773 root_dentry->d_fsdata = oe;
774
Andy Whitcroftcc259632014-10-24 00:14:38 +0200775 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200776 sb->s_op = &ovl_super_operations;
777 sb->s_root = root_dentry;
778 sb->s_fs_info = ufs;
779
780 return 0;
781
782out_put_workdir:
783 dput(ufs->workdir);
784out_put_lower_mnt:
785 mntput(ufs->lower_mnt);
786out_put_upper_mnt:
787 mntput(ufs->upper_mnt);
788out_put_workpath:
789 path_put(&workpath);
790out_put_lowerpath:
791 path_put(&lowerpath);
792out_put_upperpath:
793 path_put(&upperpath);
794out_free_oe:
795 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200796out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +0200797 kfree(ufs->config.lowerdir);
798 kfree(ufs->config.upperdir);
799 kfree(ufs->config.workdir);
800 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200801out:
802 return err;
803}
804
805static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
806 const char *dev_name, void *raw_data)
807{
808 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
809}
810
811static struct file_system_type ovl_fs_type = {
812 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +0100813 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200814 .mount = ovl_mount,
815 .kill_sb = kill_anon_super,
816};
Miklos Szeredief94b182014-11-20 16:39:59 +0100817MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200818
819static int __init ovl_init(void)
820{
821 return register_filesystem(&ovl_fs_type);
822}
823
824static void __exit ovl_exit(void)
825{
826 unregister_filesystem(&ovl_fs_type);
827}
828
829module_init(ovl_init);
830module_exit(ovl_exit);