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.
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/seq_file.h>
19
#include "overlayfs.h"
21
MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22
MODULE_DESCRIPTION("Overlay filesystem");
23
MODULE_LICENSE("GPL");
30
/* private information held for overlayfs's superblock */
32
struct vfsmount *upper_mnt;
33
struct vfsmount *lower_mnt;
34
/* pathnames of lower and upper dirs, for show_options */
35
struct ovl_config config;
38
/* private information held for every overlayfs dentry */
41
* Keep "double reference" on upper dentries, so that
42
* d_delete() doesn't think it's OK to reset d_inode to NULL.
44
struct dentry *__upperdentry;
45
struct dentry *lowerdentry;
55
const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
56
const char *ovl_opaque_xattr = "trusted.overlay.opaque";
59
enum ovl_path_type ovl_path_type(struct dentry *dentry)
61
struct ovl_entry *oe = dentry->d_fsdata;
63
if (oe->__upperdentry) {
64
if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
65
return OVL_PATH_MERGE;
67
return OVL_PATH_UPPER;
69
return OVL_PATH_LOWER;
73
static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
75
struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
76
smp_read_barrier_depends();
80
void ovl_path_upper(struct dentry *dentry, struct path *path)
82
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
83
struct ovl_entry *oe = dentry->d_fsdata;
85
path->mnt = ofs->upper_mnt;
86
path->dentry = ovl_upperdentry_dereference(oe);
89
void ovl_path_lower(struct dentry *dentry, struct path *path)
91
struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
92
struct ovl_entry *oe = dentry->d_fsdata;
94
path->mnt = ofs->lower_mnt;
95
path->dentry = oe->lowerdentry;
98
enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
101
enum ovl_path_type type = ovl_path_type(dentry);
103
if (type == OVL_PATH_LOWER)
104
ovl_path_lower(dentry, path);
106
ovl_path_upper(dentry, path);
111
struct dentry *ovl_dentry_upper(struct dentry *dentry)
113
struct ovl_entry *oe = dentry->d_fsdata;
115
return ovl_upperdentry_dereference(oe);
118
struct dentry *ovl_dentry_lower(struct dentry *dentry)
120
struct ovl_entry *oe = dentry->d_fsdata;
122
return oe->lowerdentry;
125
struct dentry *ovl_dentry_real(struct dentry *dentry)
127
struct ovl_entry *oe = dentry->d_fsdata;
128
struct dentry *realdentry;
130
realdentry = ovl_upperdentry_dereference(oe);
132
realdentry = oe->lowerdentry;
137
struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
139
struct dentry *realdentry;
141
realdentry = ovl_upperdentry_dereference(oe);
145
realdentry = oe->lowerdentry;
151
bool ovl_dentry_is_opaque(struct dentry *dentry)
153
struct ovl_entry *oe = dentry->d_fsdata;
157
void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
159
struct ovl_entry *oe = dentry->d_fsdata;
163
void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
165
struct ovl_entry *oe = dentry->d_fsdata;
167
WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
168
WARN_ON(oe->__upperdentry);
169
BUG_ON(!upperdentry->d_inode);
171
oe->__upperdentry = dget(upperdentry);
174
void ovl_dentry_version_inc(struct dentry *dentry)
176
struct ovl_entry *oe = dentry->d_fsdata;
178
WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
182
u64 ovl_dentry_version_get(struct dentry *dentry)
184
struct ovl_entry *oe = dentry->d_fsdata;
186
WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
190
bool ovl_is_whiteout(struct dentry *dentry)
197
if (!dentry->d_inode)
199
if (!S_ISLNK(dentry->d_inode->i_mode))
202
res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
203
if (res == 1 && val == 'y')
209
static bool ovl_is_opaquedir(struct dentry *dentry)
214
if (!S_ISDIR(dentry->d_inode->i_mode))
217
res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
218
if (res == 1 && val == 'y')
224
static void ovl_entry_free(struct rcu_head *head)
226
struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
230
static void ovl_dentry_release(struct dentry *dentry)
232
struct ovl_entry *oe = dentry->d_fsdata;
235
dput(oe->__upperdentry);
236
dput(oe->__upperdentry);
237
dput(oe->lowerdentry);
238
call_rcu(&oe->rcu, ovl_entry_free);
242
const struct dentry_operations ovl_dentry_operations = {
243
.d_release = ovl_dentry_release,
246
static struct ovl_entry *ovl_alloc_entry(void)
248
return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
251
static inline struct dentry *ovl_lookup_real(struct dentry *dir,
254
struct dentry *dentry;
256
mutex_lock(&dir->d_inode->i_mutex);
257
dentry = lookup_one_len(name->name, dir, name->len);
258
mutex_unlock(&dir->d_inode->i_mutex);
260
if (IS_ERR(dentry)) {
261
if (PTR_ERR(dentry) == -ENOENT)
263
} else if (!dentry->d_inode) {
270
static int ovl_do_lookup(struct dentry *dentry)
272
struct ovl_entry *oe;
273
struct dentry *upperdir;
274
struct dentry *lowerdir;
275
struct dentry *upperdentry = NULL;
276
struct dentry *lowerdentry = NULL;
277
struct inode *inode = NULL;
281
oe = ovl_alloc_entry();
285
upperdir = ovl_dentry_upper(dentry->d_parent);
286
lowerdir = ovl_dentry_lower(dentry->d_parent);
289
upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
290
err = PTR_ERR(upperdentry);
291
if (IS_ERR(upperdentry))
294
if (lowerdir && upperdentry &&
295
(S_ISLNK(upperdentry->d_inode->i_mode) ||
296
S_ISDIR(upperdentry->d_inode->i_mode))) {
297
const struct cred *old_cred;
298
struct cred *override_cred;
301
override_cred = prepare_creds();
305
/* CAP_SYS_ADMIN needed for getxattr */
306
cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
307
old_cred = override_creds(override_cred);
309
if (ovl_is_opaquedir(upperdentry)) {
311
} else if (ovl_is_whiteout(upperdentry)) {
316
revert_creds(old_cred);
317
put_cred(override_cred);
320
if (lowerdir && !oe->opaque) {
321
lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
322
err = PTR_ERR(lowerdentry);
323
if (IS_ERR(lowerdentry))
327
if (lowerdentry && upperdentry &&
328
(!S_ISDIR(upperdentry->d_inode->i_mode) ||
329
!S_ISDIR(lowerdentry->d_inode->i_mode))) {
335
if (lowerdentry || upperdentry) {
336
struct dentry *realdentry;
338
realdentry = upperdentry ? upperdentry : lowerdentry;
340
inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
347
oe->__upperdentry = dget(upperdentry);
350
oe->lowerdentry = lowerdentry;
352
dentry->d_fsdata = oe;
353
dentry->d_op = &ovl_dentry_operations;
354
d_add(dentry, inode);
368
struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
369
struct nameidata *nd)
371
int err = ovl_do_lookup(dentry);
379
struct file *ovl_path_open(struct path *path, int flags)
382
return dentry_open(path->dentry, path->mnt, flags, current_cred());
385
static void ovl_put_super(struct super_block *sb)
387
struct ovl_fs *ufs = sb->s_fs_info;
389
if (!(sb->s_flags & MS_RDONLY))
390
mnt_drop_write(ufs->upper_mnt);
392
mntput(ufs->upper_mnt);
393
mntput(ufs->lower_mnt);
395
kfree(ufs->config.lowerdir);
396
kfree(ufs->config.upperdir);
400
static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
403
struct ovl_fs *ufs = sb->s_fs_info;
405
/* When remounting rw or ro, we need to adjust the write access to the
408
if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
409
/* No change to readonly status */
412
if (flags & MS_RDONLY) {
413
mnt_drop_write(ufs->upper_mnt);
416
return mnt_want_write(ufs->upper_mnt);
421
* @sb: The overlayfs super block
422
* @buf: The struct kstatfs to fill in with stats
424
* Get the filesystem statistics. As writes always target the upper layer
425
* filesystem pass the statfs to the same filesystem.
427
static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
429
struct dentry *root_dentry = dentry->d_sb->s_root;
431
ovl_path_upper(root_dentry, &path);
433
if (!path.dentry->d_sb->s_op->statfs)
435
return path.dentry->d_sb->s_op->statfs(path.dentry, buf);
441
* Prints the mount options for a given superblock.
442
* Returns zero; does not fail.
444
static int ovl_show_options(struct seq_file *m, struct vfsmount *mnt)
446
struct super_block *sb = mnt->mnt_sb;
447
struct ovl_fs *ufs = sb->s_fs_info;
449
seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
450
seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
454
static const struct super_operations ovl_super_operations = {
455
.put_super = ovl_put_super,
456
.remount_fs = ovl_remount_fs,
457
.statfs = ovl_statfs,
458
.show_options = ovl_show_options,
467
static const match_table_t ovl_tokens = {
468
{Opt_lowerdir, "lowerdir=%s"},
469
{Opt_upperdir, "upperdir=%s"},
473
static int ovl_parse_opt(char *opt, struct ovl_config *config)
477
config->upperdir = NULL;
478
config->lowerdir = NULL;
480
while ((p = strsep(&opt, ",")) != NULL) {
482
substring_t args[MAX_OPT_ARGS];
487
token = match_token(p, ovl_tokens, args);
490
kfree(config->upperdir);
491
config->upperdir = match_strdup(&args[0]);
492
if (!config->upperdir)
497
kfree(config->lowerdir);
498
config->lowerdir = match_strdup(&args[0]);
499
if (!config->lowerdir)
510
static int ovl_fill_super(struct super_block *sb, void *data, int silent)
512
struct path lowerpath;
513
struct path upperpath;
514
struct inode *root_inode;
515
struct dentry *root_dentry;
516
struct ovl_entry *oe;
521
ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
525
err = ovl_parse_opt((char *) data, &ufs->config);
530
if (!ufs->config.upperdir || !ufs->config.lowerdir) {
531
printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
532
goto out_free_config;
535
oe = ovl_alloc_entry();
537
goto out_free_config;
539
root_inode = ovl_new_inode(sb, S_IFDIR, oe);
543
err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
547
err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
549
goto out_put_upperpath;
552
if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
553
!S_ISDIR(lowerpath.dentry->d_inode->i_mode))
554
goto out_put_lowerpath;
556
sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
557
lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
560
if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
561
printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
562
goto out_put_lowerpath;
566
ufs->upper_mnt = clone_private_mount(&upperpath);
567
err = PTR_ERR(ufs->upper_mnt);
568
if (IS_ERR(ufs->upper_mnt)) {
569
printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
570
goto out_put_lowerpath;
573
ufs->lower_mnt = clone_private_mount(&lowerpath);
574
err = PTR_ERR(ufs->lower_mnt);
575
if (IS_ERR(ufs->lower_mnt)) {
576
printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
577
goto out_put_upper_mnt;
581
* Make lower_mnt R/O. That way fchmod/fchown on lower file
582
* will fail instead of modifying lower fs.
584
ufs->lower_mnt->mnt_flags |= MNT_READONLY;
586
/* If the upper fs is r/o, we mark overlayfs r/o too */
587
if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
588
sb->s_flags |= MS_RDONLY;
590
if (!(sb->s_flags & MS_RDONLY)) {
591
err = mnt_want_write(ufs->upper_mnt);
593
goto out_put_lower_mnt;
597
root_dentry = d_alloc_root(root_inode);
601
mntput(upperpath.mnt);
602
mntput(lowerpath.mnt);
604
oe->__upperdentry = dget(upperpath.dentry);
605
oe->lowerdentry = lowerpath.dentry;
607
root_dentry->d_fsdata = oe;
608
root_dentry->d_op = &ovl_dentry_operations;
610
sb->s_op = &ovl_super_operations;
611
sb->s_root = root_dentry;
617
if (!(sb->s_flags & MS_RDONLY))
618
mnt_drop_write(ufs->upper_mnt);
620
mntput(ufs->lower_mnt);
622
mntput(ufs->upper_mnt);
624
path_put(&lowerpath);
626
path_put(&upperpath);
632
kfree(ufs->config.lowerdir);
633
kfree(ufs->config.upperdir);
640
static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
641
const char *dev_name, void *raw_data)
643
return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
646
static struct file_system_type ovl_fs_type = {
647
.owner = THIS_MODULE,
650
.kill_sb = kill_anon_super,
653
static int __init ovl_init(void)
655
return register_filesystem(&ovl_fs_type);
658
static void __exit ovl_exit(void)
660
unregister_filesystem(&ovl_fs_type);
663
module_init(ovl_init);
664
module_exit(ovl_exit);