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