123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- /*
- * Copyright (C) 2011 Novell Inc.
- * Copyright (C) 2016 Red Hat, Inc.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published by
- * the Free Software Foundation.
- */
- #include <linux/fs.h>
- #include <linux/mount.h>
- #include <linux/slab.h>
- #include <linux/cred.h>
- #include <linux/xattr.h>
- #include "overlayfs.h"
- #include "ovl_entry.h"
- int ovl_want_write(struct dentry *dentry)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- return mnt_want_write(ofs->upper_mnt);
- }
- void ovl_drop_write(struct dentry *dentry)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- mnt_drop_write(ofs->upper_mnt);
- }
- struct dentry *ovl_workdir(struct dentry *dentry)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- return ofs->workdir;
- }
- const struct cred *ovl_override_creds(struct super_block *sb)
- {
- struct ovl_fs *ofs = sb->s_fs_info;
- return override_creds(ofs->creator_cred);
- }
- struct super_block *ovl_same_sb(struct super_block *sb)
- {
- struct ovl_fs *ofs = sb->s_fs_info;
- return ofs->same_sb;
- }
- struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
- {
- size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
- struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
- if (oe)
- oe->numlower = numlower;
- return oe;
- }
- bool ovl_dentry_remote(struct dentry *dentry)
- {
- return dentry->d_flags &
- (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
- DCACHE_OP_REAL);
- }
- bool ovl_dentry_weird(struct dentry *dentry)
- {
- return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
- DCACHE_MANAGE_TRANSIT |
- DCACHE_OP_HASH |
- DCACHE_OP_COMPARE);
- }
- enum ovl_path_type ovl_path_type(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- enum ovl_path_type type = 0;
- if (ovl_dentry_upper(dentry)) {
- type = __OVL_PATH_UPPER;
- /*
- * Non-dir dentry can hold lower dentry of its copy up origin.
- */
- if (oe->numlower) {
- type |= __OVL_PATH_ORIGIN;
- if (d_is_dir(dentry))
- type |= __OVL_PATH_MERGE;
- }
- } else {
- if (oe->numlower > 1)
- type |= __OVL_PATH_MERGE;
- }
- return type;
- }
- void ovl_path_upper(struct dentry *dentry, struct path *path)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- path->mnt = ofs->upper_mnt;
- path->dentry = ovl_dentry_upper(dentry);
- }
- void ovl_path_lower(struct dentry *dentry, struct path *path)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
- }
- enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
- {
- enum ovl_path_type type = ovl_path_type(dentry);
- if (!OVL_TYPE_UPPER(type))
- ovl_path_lower(dentry, path);
- else
- ovl_path_upper(dentry, path);
- return type;
- }
- struct dentry *ovl_dentry_upper(struct dentry *dentry)
- {
- return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
- }
- struct dentry *ovl_dentry_lower(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- return oe->numlower ? oe->lowerstack[0].dentry : NULL;
- }
- struct dentry *ovl_dentry_real(struct dentry *dentry)
- {
- return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
- }
- struct inode *ovl_inode_upper(struct inode *inode)
- {
- struct dentry *upperdentry = ovl_upperdentry_dereference(OVL_I(inode));
- return upperdentry ? d_inode(upperdentry) : NULL;
- }
- struct inode *ovl_inode_lower(struct inode *inode)
- {
- return OVL_I(inode)->lower;
- }
- struct inode *ovl_inode_real(struct inode *inode)
- {
- return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
- }
- struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- return oe->cache;
- }
- void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- oe->cache = cache;
- }
- bool ovl_dentry_is_opaque(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- return oe->opaque;
- }
- bool ovl_dentry_is_impure(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- return oe->impure;
- }
- bool ovl_dentry_is_whiteout(struct dentry *dentry)
- {
- return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
- }
- void ovl_dentry_set_opaque(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- oe->opaque = true;
- }
- bool ovl_redirect_dir(struct super_block *sb)
- {
- struct ovl_fs *ofs = sb->s_fs_info;
- return ofs->config.redirect_dir && !ofs->noxattr;
- }
- const char *ovl_dentry_get_redirect(struct dentry *dentry)
- {
- return OVL_I(d_inode(dentry))->redirect;
- }
- void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
- {
- struct ovl_inode *oi = OVL_I(d_inode(dentry));
- kfree(oi->redirect);
- oi->redirect = redirect;
- }
- void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
- struct dentry *lowerdentry)
- {
- if (upperdentry)
- OVL_I(inode)->__upperdentry = upperdentry;
- if (lowerdentry)
- OVL_I(inode)->lower = d_inode(lowerdentry);
- ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
- }
- void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
- {
- struct inode *upperinode = d_inode(upperdentry);
- WARN_ON(!inode_unhashed(inode));
- WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
- WARN_ON(OVL_I(inode)->__upperdentry);
- /*
- * Make sure upperdentry is consistent before making it visible
- */
- smp_wmb();
- OVL_I(inode)->__upperdentry = upperdentry;
- if (!S_ISDIR(upperinode->i_mode)) {
- inode->i_private = upperinode;
- __insert_inode_hash(inode, (unsigned long) upperinode);
- }
- }
- void ovl_dentry_version_inc(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- WARN_ON(!inode_is_locked(dentry->d_inode));
- oe->version++;
- }
- u64 ovl_dentry_version_get(struct dentry *dentry)
- {
- struct ovl_entry *oe = dentry->d_fsdata;
- WARN_ON(!inode_is_locked(dentry->d_inode));
- return oe->version;
- }
- bool ovl_is_whiteout(struct dentry *dentry)
- {
- struct inode *inode = dentry->d_inode;
- return inode && IS_WHITEOUT(inode);
- }
- struct file *ovl_path_open(struct path *path, int flags)
- {
- return dentry_open(path, flags | O_NOATIME, current_cred());
- }
- int ovl_copy_up_start(struct dentry *dentry)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- struct ovl_entry *oe = dentry->d_fsdata;
- int err;
- spin_lock(&ofs->copyup_wq.lock);
- err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
- if (!err) {
- if (ovl_dentry_upper(dentry))
- err = 1; /* Already copied up */
- else
- oe->copying = true;
- }
- spin_unlock(&ofs->copyup_wq.lock);
- return err;
- }
- void ovl_copy_up_end(struct dentry *dentry)
- {
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- struct ovl_entry *oe = dentry->d_fsdata;
- spin_lock(&ofs->copyup_wq.lock);
- oe->copying = false;
- wake_up_locked(&ofs->copyup_wq);
- spin_unlock(&ofs->copyup_wq.lock);
- }
- bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
- {
- int res;
- char val;
- if (!d_is_dir(dentry))
- return false;
- res = vfs_getxattr(dentry, name, &val, 1);
- if (res == 1 && val == 'y')
- return true;
- return false;
- }
- int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
- const char *name, const void *value, size_t size,
- int xerr)
- {
- int err;
- struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
- if (ofs->noxattr)
- return xerr;
- err = ovl_do_setxattr(upperdentry, name, value, size, 0);
- if (err == -EOPNOTSUPP) {
- pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
- ofs->noxattr = true;
- return xerr;
- }
- return err;
- }
- int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
- {
- int err;
- struct ovl_entry *oe = dentry->d_fsdata;
- if (oe->impure)
- return 0;
- /*
- * Do not fail when upper doesn't support xattrs.
- * Upper inodes won't have origin nor redirect xattr anyway.
- */
- err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
- "y", 1, 0);
- if (!err)
- oe->impure = true;
- return err;
- }
|