staging: comedi: drivers: rtd520: Removed variables that is never used
[linux-2.6/btrfs-unstable.git] / fs / overlayfs / super.c
blobf16d318b71f8bbe4e77f8a3214e616101848e49c
1 /*
3 * Copyright (C) 2011 Novell Inc.
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 */
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 <linux/statfs.h>
20 #include <linux/seq_file.h>
21 #include "overlayfs.h"
23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
27 #define OVERLAYFS_SUPER_MAGIC 0x794c7630
29 struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
35 /* private information held for overlayfs's superblock */
36 struct ovl_fs {
37 struct vfsmount *upper_mnt;
38 struct vfsmount *lower_mnt;
39 struct dentry *workdir;
40 long lower_namelen;
41 /* pathnames of lower and upper dirs, for show_options */
42 struct ovl_config config;
45 struct ovl_dir_cache;
47 /* private information held for every overlayfs dentry */
48 struct 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;
57 struct rcu_head rcu;
61 const char *ovl_opaque_xattr = "trusted.overlay.opaque";
64 enum ovl_path_type ovl_path_type(struct dentry *dentry)
66 struct ovl_entry *oe = dentry->d_fsdata;
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;
80 } else {
81 return OVL_PATH_LOWER;
85 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
87 return lockless_dereference(oe->__upperdentry);
90 void ovl_path_upper(struct dentry *dentry, struct path *path)
92 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
93 struct ovl_entry *oe = dentry->d_fsdata;
95 path->mnt = ofs->upper_mnt;
96 path->dentry = ovl_upperdentry_dereference(oe);
99 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
102 enum ovl_path_type type = ovl_path_type(dentry);
104 if (type == OVL_PATH_LOWER)
105 ovl_path_lower(dentry, path);
106 else
107 ovl_path_upper(dentry, path);
109 return type;
112 struct dentry *ovl_dentry_upper(struct dentry *dentry)
114 struct ovl_entry *oe = dentry->d_fsdata;
116 return ovl_upperdentry_dereference(oe);
119 struct dentry *ovl_dentry_lower(struct dentry *dentry)
121 struct ovl_entry *oe = dentry->d_fsdata;
123 return oe->lowerdentry;
126 struct dentry *ovl_dentry_real(struct dentry *dentry)
128 struct ovl_entry *oe = dentry->d_fsdata;
129 struct dentry *realdentry;
131 realdentry = ovl_upperdentry_dereference(oe);
132 if (!realdentry)
133 realdentry = oe->lowerdentry;
135 return realdentry;
138 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
140 struct dentry *realdentry;
142 realdentry = ovl_upperdentry_dereference(oe);
143 if (realdentry) {
144 *is_upper = true;
145 } else {
146 realdentry = oe->lowerdentry;
147 *is_upper = false;
149 return realdentry;
152 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
154 struct ovl_entry *oe = dentry->d_fsdata;
156 return oe->cache;
159 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
161 struct ovl_entry *oe = dentry->d_fsdata;
163 oe->cache = cache;
166 void ovl_path_lower(struct dentry *dentry, struct path *path)
168 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
169 struct ovl_entry *oe = dentry->d_fsdata;
171 path->mnt = ofs->lower_mnt;
172 path->dentry = oe->lowerdentry;
175 int ovl_want_write(struct dentry *dentry)
177 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
178 return mnt_want_write(ofs->upper_mnt);
181 void ovl_drop_write(struct dentry *dentry)
183 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
184 mnt_drop_write(ofs->upper_mnt);
187 struct dentry *ovl_workdir(struct dentry *dentry)
189 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
190 return ofs->workdir;
193 bool ovl_dentry_is_opaque(struct dentry *dentry)
195 struct ovl_entry *oe = dentry->d_fsdata;
196 return oe->opaque;
199 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
201 struct ovl_entry *oe = dentry->d_fsdata;
202 oe->opaque = opaque;
205 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
207 struct ovl_entry *oe = dentry->d_fsdata;
209 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
210 WARN_ON(oe->__upperdentry);
211 BUG_ON(!upperdentry->d_inode);
213 * Make sure upperdentry is consistent before making it visible to
214 * ovl_upperdentry_dereference().
216 smp_wmb();
217 oe->__upperdentry = upperdentry;
220 void ovl_dentry_version_inc(struct dentry *dentry)
222 struct ovl_entry *oe = dentry->d_fsdata;
224 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
225 oe->version++;
228 u64 ovl_dentry_version_get(struct dentry *dentry)
230 struct ovl_entry *oe = dentry->d_fsdata;
232 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
233 return oe->version;
236 bool ovl_is_whiteout(struct dentry *dentry)
238 struct inode *inode = dentry->d_inode;
240 return inode && IS_WHITEOUT(inode);
243 static bool ovl_is_opaquedir(struct dentry *dentry)
245 int res;
246 char val;
247 struct inode *inode = dentry->d_inode;
249 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
250 return false;
252 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
253 if (res == 1 && val == 'y')
254 return true;
256 return false;
259 static void ovl_dentry_release(struct dentry *dentry)
261 struct ovl_entry *oe = dentry->d_fsdata;
263 if (oe) {
264 dput(oe->__upperdentry);
265 dput(oe->lowerdentry);
266 kfree_rcu(oe, rcu);
270 static const struct dentry_operations ovl_dentry_operations = {
271 .d_release = ovl_dentry_release,
274 static struct ovl_entry *ovl_alloc_entry(void)
276 return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
279 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
280 struct qstr *name)
282 struct dentry *dentry;
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);
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;
295 return dentry;
298 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
299 unsigned int flags)
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;
309 err = -ENOMEM;
310 oe = ovl_alloc_entry();
311 if (!oe)
312 goto out;
314 upperdir = ovl_dentry_upper(dentry->d_parent);
315 lowerdir = ovl_dentry_lower(dentry->d_parent);
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;
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;
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;
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;
348 if (lowerdentry || upperdentry) {
349 struct dentry *realdentry;
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);
360 oe->__upperdentry = upperdentry;
361 oe->lowerdentry = lowerdentry;
363 dentry->d_fsdata = oe;
364 d_add(dentry, inode);
366 return NULL;
368 out_dput:
369 dput(lowerdentry);
370 out_dput_upper:
371 dput(upperdentry);
372 out_put_dir:
373 kfree(oe);
374 out:
375 return ERR_PTR(err);
378 struct file *ovl_path_open(struct path *path, int flags)
380 return dentry_open(path, flags, current_cred());
383 static void ovl_put_super(struct super_block *sb)
385 struct ovl_fs *ufs = sb->s_fs_info;
387 dput(ufs->workdir);
388 mntput(ufs->upper_mnt);
389 mntput(ufs->lower_mnt);
391 kfree(ufs->config.lowerdir);
392 kfree(ufs->config.upperdir);
393 kfree(ufs->config.workdir);
394 kfree(ufs);
398 * ovl_statfs
399 * @sb: The overlayfs super block
400 * @buf: The struct kstatfs to fill in with stats
402 * Get the filesystem statistics. As writes always target the upper layer
403 * filesystem pass the statfs to the same filesystem.
405 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
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;
412 ovl_path_upper(root_dentry, &path);
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;
420 return err;
424 * ovl_show_options
426 * Prints the mount options for a given superblock.
427 * Returns zero; does not fail.
429 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
431 struct super_block *sb = dentry->d_sb;
432 struct ovl_fs *ufs = sb->s_fs_info;
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;
440 static const struct super_operations ovl_super_operations = {
441 .put_super = ovl_put_super,
442 .statfs = ovl_statfs,
443 .show_options = ovl_show_options,
446 enum {
447 OPT_LOWERDIR,
448 OPT_UPPERDIR,
449 OPT_WORKDIR,
450 OPT_ERR,
453 static 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}
460 static char *ovl_next_opt(char **s)
462 char *sbegin = *s;
463 char *p;
465 if (sbegin == NULL)
466 return NULL;
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;
479 *s = NULL;
480 return sbegin;
483 static int ovl_parse_opt(char *opt, struct ovl_config *config)
485 char *p;
487 while ((p = ovl_next_opt(&opt)) != NULL) {
488 int token;
489 substring_t args[MAX_OPT_ARGS];
491 if (!*p)
492 continue;
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;
503 case OPT_LOWERDIR:
504 kfree(config->lowerdir);
505 config->lowerdir = match_strdup(&args[0]);
506 if (!config->lowerdir)
507 return -ENOMEM;
508 break;
510 case OPT_WORKDIR:
511 kfree(config->workdir);
512 config->workdir = match_strdup(&args[0]);
513 if (!config->workdir)
514 return -ENOMEM;
515 break;
517 default:
518 return -EINVAL;
521 return 0;
524 #define OVL_WORKDIR_NAME "work"
526 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
527 struct dentry *dentry)
529 struct inode *dir = dentry->d_inode;
530 struct dentry *work;
531 int err;
532 bool retried = false;
534 err = mnt_want_write(mnt);
535 if (err)
536 return ERR_PTR(err);
538 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
539 retry:
540 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
541 strlen(OVL_WORKDIR_NAME));
543 if (!IS_ERR(work)) {
544 struct kstat stat = {
545 .mode = S_IFDIR | 0,
548 if (work->d_inode) {
549 err = -EEXIST;
550 if (retried)
551 goto out_dput;
553 retried = true;
554 ovl_cleanup(dir, work);
555 dput(work);
556 goto retry;
559 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
560 if (err)
561 goto out_dput;
563 out_unlock:
564 mutex_unlock(&dir->i_mutex);
565 mnt_drop_write(mnt);
567 return work;
569 out_dput:
570 dput(work);
571 work = ERR_PTR(err);
572 goto out_unlock;
575 static void ovl_unescape(char *s)
577 char *d = s;
579 for (;; s++, d++) {
580 if (*s == '\\')
581 s++;
582 *d = *s;
583 if (!*s)
584 break;
588 static int ovl_mount_dir(const char *name, struct path *path)
590 int err;
591 char *tmp = kstrdup(name, GFP_KERNEL);
593 if (!tmp)
594 return -ENOMEM;
596 ovl_unescape(tmp);
597 err = kern_path(tmp, LOOKUP_FOLLOW, path);
598 if (err) {
599 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
600 err = -EINVAL;
602 kfree(tmp);
603 return err;
606 static bool ovl_is_allowed_fs_type(struct dentry *root)
608 const struct dentry_operations *dop = root->d_op;
611 * We don't support:
612 * - automount filesystems
613 * - filesystems with revalidate (FIXME for lower layer)
614 * - filesystems with case insensitive names
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;
622 return true;
625 /* Workdir should not be subdir of upperdir and vice versa */
626 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
628 bool ok = false;
630 if (workdir != upperdir) {
631 ok = (lock_rename(workdir, upperdir) == NULL);
632 unlock_rename(workdir, upperdir);
634 return ok;
637 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
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;
646 struct kstatfs statfs;
647 int err;
649 err = -ENOMEM;
650 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
651 if (!ufs)
652 goto out;
654 err = ovl_parse_opt((char *) data, &ufs->config);
655 if (err)
656 goto out_free_config;
658 /* FIXME: workdir is not needed for a R/O mount */
659 err = -EINVAL;
660 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
661 !ufs->config.workdir) {
662 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
663 goto out_free_config;
666 err = -ENOMEM;
667 oe = ovl_alloc_entry();
668 if (oe == NULL)
669 goto out_free_config;
671 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
672 if (err)
673 goto out_free_oe;
675 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
676 if (err)
677 goto out_put_upperpath;
679 err = ovl_mount_dir(ufs->config.workdir, &workpath);
680 if (err)
681 goto out_put_lowerpath;
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;
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;
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;
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;
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;
710 err = vfs_statfs(&lowerpath, &statfs);
711 if (err) {
712 pr_err("overlayfs: statfs failed on lowerpath\n");
713 goto out_put_workpath;
715 ufs->lower_namelen = statfs.f_namelen;
717 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
718 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
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;
726 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;
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;
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",
744 ufs->config.workdir, OVL_WORKDIR_NAME);
745 goto out_put_lower_mnt;
749 * Make lower_mnt R/O. That way fchmod/fchown on lower file
750 * will fail instead of modifying lower fs.
752 ufs->lower_mnt->mnt_flags |= MNT_READONLY;
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;
758 sb->s_d_op = &ovl_dentry_operations;
760 err = -ENOMEM;
761 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
762 if (!root_inode)
763 goto out_put_workdir;
765 root_dentry = d_make_root(root_inode);
766 if (!root_dentry)
767 goto out_put_workdir;
769 mntput(upperpath.mnt);
770 mntput(lowerpath.mnt);
771 path_put(&workpath);
773 oe->__upperdentry = upperpath.dentry;
774 oe->lowerdentry = lowerpath.dentry;
776 root_dentry->d_fsdata = oe;
778 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
779 sb->s_op = &ovl_super_operations;
780 sb->s_root = root_dentry;
781 sb->s_fs_info = ufs;
783 return 0;
785 out_put_workdir:
786 dput(ufs->workdir);
787 out_put_lower_mnt:
788 mntput(ufs->lower_mnt);
789 out_put_upper_mnt:
790 mntput(ufs->upper_mnt);
791 out_put_workpath:
792 path_put(&workpath);
793 out_put_lowerpath:
794 path_put(&lowerpath);
795 out_put_upperpath:
796 path_put(&upperpath);
797 out_free_oe:
798 kfree(oe);
799 out_free_config:
800 kfree(ufs->config.lowerdir);
801 kfree(ufs->config.upperdir);
802 kfree(ufs->config.workdir);
803 kfree(ufs);
804 out:
805 return err;
808 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
809 const char *dev_name, void *raw_data)
811 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
814 static struct file_system_type ovl_fs_type = {
815 .owner = THIS_MODULE,
816 .name = "overlay",
817 .mount = ovl_mount,
818 .kill_sb = kill_anon_super,
820 MODULE_ALIAS_FS("overlay");
822 static int __init ovl_init(void)
824 return register_filesystem(&ovl_fs_type);
827 static void __exit ovl_exit(void)
829 unregister_filesystem(&ovl_fs_type);
832 module_init(ovl_init);
833 module_exit(ovl_exit);