Commit 243d5067 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs

Pull overlayfs updates from Miklos Szeredi:
 "Various fixes and tweaks"

* 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs:
  ovl: cleanup unused var in rename2
  ovl: rename is_merge to is_lowest
  ovl: fixed coding style warning
  ovl: Ensure upper filesystem supports d_type
  ovl: Warn on copy up if a process has a R/O fd open to the lower file
  ovl: honor flag MS_SILENT at mount
  ovl: verify upper dentry before unlink and rename
parents 9f15dec8 6986c012
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* the Free Software Foundation. * the Free Software Foundation.
*/ */
#include <linux/module.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/file.h> #include <linux/file.h>
...@@ -16,10 +17,41 @@ ...@@ -16,10 +17,41 @@
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/namei.h> #include <linux/namei.h>
#include <linux/fdtable.h>
#include <linux/ratelimit.h>
#include "overlayfs.h" #include "overlayfs.h"
#define OVL_COPY_UP_CHUNK_SIZE (1 << 20) #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
static bool __read_mostly ovl_check_copy_up;
module_param_named(check_copy_up, ovl_check_copy_up, bool,
S_IWUSR | S_IRUGO);
MODULE_PARM_DESC(ovl_check_copy_up,
"Warn on copy-up when causing process also has a R/O fd open");
static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
{
const struct dentry *dentry = data;
if (f->f_inode == d_inode(dentry))
pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
f, fd, current->pid, current->comm);
return 0;
}
/*
* Check the fds open by this process and warn if something like the following
* scenario is about to occur:
*
* fd1 = open("foo", O_RDONLY);
* fd2 = open("foo", O_RDWR);
*/
static void ovl_do_check_copy_up(struct dentry *dentry)
{
if (ovl_check_copy_up)
iterate_fd(current->files, 0, ovl_check_fd, dentry);
}
int ovl_copy_xattr(struct dentry *old, struct dentry *new) int ovl_copy_xattr(struct dentry *old, struct dentry *new)
{ {
ssize_t list_size, size, value_size = 0; ssize_t list_size, size, value_size = 0;
...@@ -235,6 +267,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, ...@@ -235,6 +267,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
if (S_ISREG(stat->mode)) { if (S_ISREG(stat->mode)) {
struct path upperpath; struct path upperpath;
ovl_path_upper(dentry, &upperpath); ovl_path_upper(dentry, &upperpath);
BUG_ON(upperpath.dentry != NULL); BUG_ON(upperpath.dentry != NULL);
upperpath.dentry = newdentry; upperpath.dentry = newdentry;
...@@ -309,6 +342,8 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, ...@@ -309,6 +342,8 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
if (WARN_ON(!workdir)) if (WARN_ON(!workdir))
return -EROFS; return -EROFS;
ovl_do_check_copy_up(lowerpath->dentry);
ovl_path_upper(parent, &parentpath); ovl_path_upper(parent, &parentpath);
upperdir = parentpath.dentry; upperdir = parentpath.dentry;
......
...@@ -596,21 +596,25 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir) ...@@ -596,21 +596,25 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
{ {
struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
struct inode *dir = upperdir->d_inode; struct inode *dir = upperdir->d_inode;
struct dentry *upper = ovl_dentry_upper(dentry); struct dentry *upper;
int err; int err;
inode_lock_nested(dir, I_MUTEX_PARENT); inode_lock_nested(dir, I_MUTEX_PARENT);
upper = lookup_one_len(dentry->d_name.name, upperdir,
dentry->d_name.len);
err = PTR_ERR(upper);
if (IS_ERR(upper))
goto out_unlock;
err = -ESTALE; err = -ESTALE;
if (upper->d_parent == upperdir) { if (upper == ovl_dentry_upper(dentry)) {
/* Don't let d_delete() think it can reset d_inode */
dget(upper);
if (is_dir) if (is_dir)
err = vfs_rmdir(dir, upper); err = vfs_rmdir(dir, upper);
else else
err = vfs_unlink(dir, upper, NULL); err = vfs_unlink(dir, upper, NULL);
dput(upper);
ovl_dentry_version_inc(dentry->d_parent); ovl_dentry_version_inc(dentry->d_parent);
} }
dput(upper);
/* /*
* Keeping this dentry hashed would mean having to release * Keeping this dentry hashed would mean having to release
...@@ -620,6 +624,7 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir) ...@@ -620,6 +624,7 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
*/ */
if (!err) if (!err)
d_drop(dentry); d_drop(dentry);
out_unlock:
inode_unlock(dir); inode_unlock(dir);
return err; return err;
...@@ -714,7 +719,6 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, ...@@ -714,7 +719,6 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
struct dentry *trap; struct dentry *trap;
bool old_opaque; bool old_opaque;
bool new_opaque; bool new_opaque;
bool new_create = false;
bool cleanup_whiteout = false; bool cleanup_whiteout = false;
bool overwrite = !(flags & RENAME_EXCHANGE); bool overwrite = !(flags & RENAME_EXCHANGE);
bool is_dir = d_is_dir(old); bool is_dir = d_is_dir(old);
...@@ -840,29 +844,38 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, ...@@ -840,29 +844,38 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
trap = lock_rename(new_upperdir, old_upperdir); trap = lock_rename(new_upperdir, old_upperdir);
olddentry = ovl_dentry_upper(old);
newdentry = ovl_dentry_upper(new); olddentry = lookup_one_len(old->d_name.name, old_upperdir,
if (newdentry) { old->d_name.len);
err = PTR_ERR(olddentry);
if (IS_ERR(olddentry))
goto out_unlock;
err = -ESTALE;
if (olddentry != ovl_dentry_upper(old))
goto out_dput_old;
newdentry = lookup_one_len(new->d_name.name, new_upperdir,
new->d_name.len);
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_dput_old;
err = -ESTALE;
if (ovl_dentry_upper(new)) {
if (opaquedir) { if (opaquedir) {
newdentry = opaquedir; if (newdentry != opaquedir)
opaquedir = NULL; goto out_dput;
} else { } else {
dget(newdentry); if (newdentry != ovl_dentry_upper(new))
goto out_dput;
} }
} else { } else {
new_create = true; if (!d_is_negative(newdentry) &&
newdentry = lookup_one_len(new->d_name.name, new_upperdir, (!new_opaque || !ovl_is_whiteout(newdentry)))
new->d_name.len); goto out_dput;
err = PTR_ERR(newdentry);
if (IS_ERR(newdentry))
goto out_unlock;
} }
err = -ESTALE;
if (olddentry->d_parent != old_upperdir)
goto out_dput;
if (newdentry->d_parent != new_upperdir)
goto out_dput;
if (olddentry == trap) if (olddentry == trap)
goto out_dput; goto out_dput;
if (newdentry == trap) if (newdentry == trap)
...@@ -925,6 +938,8 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, ...@@ -925,6 +938,8 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
out_dput: out_dput:
dput(newdentry); dput(newdentry);
out_dput_old:
dput(olddentry);
out_unlock: out_unlock:
unlock_rename(new_upperdir, old_upperdir); unlock_rename(new_upperdir, old_upperdir);
out_revert_creds: out_revert_creds:
......
...@@ -166,6 +166,7 @@ extern const struct file_operations ovl_dir_operations; ...@@ -166,6 +166,7 @@ extern const struct file_operations ovl_dir_operations;
int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list); int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list); void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
void ovl_cache_free(struct list_head *list); void ovl_cache_free(struct list_head *list);
int ovl_check_d_type_supported(struct path *realpath);
/* inode.c */ /* inode.c */
int ovl_setattr(struct dentry *dentry, struct iattr *attr); int ovl_setattr(struct dentry *dentry, struct iattr *attr);
......
...@@ -36,13 +36,14 @@ struct ovl_dir_cache { ...@@ -36,13 +36,14 @@ struct ovl_dir_cache {
struct ovl_readdir_data { struct ovl_readdir_data {
struct dir_context ctx; struct dir_context ctx;
bool is_merge; bool is_lowest;
struct rb_root root; struct rb_root root;
struct list_head *list; struct list_head *list;
struct list_head middle; struct list_head middle;
struct ovl_cache_entry *first_maybe_whiteout; struct ovl_cache_entry *first_maybe_whiteout;
int count; int count;
int err; int err;
bool d_type_supported;
}; };
struct ovl_dir_file { struct ovl_dir_file {
...@@ -139,9 +140,9 @@ static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, ...@@ -139,9 +140,9 @@ static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
return 0; return 0;
} }
static int ovl_fill_lower(struct ovl_readdir_data *rdd, static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
const char *name, int namelen, const char *name, int namelen,
loff_t offset, u64 ino, unsigned int d_type) loff_t offset, u64 ino, unsigned int d_type)
{ {
struct ovl_cache_entry *p; struct ovl_cache_entry *p;
...@@ -193,10 +194,10 @@ static int ovl_fill_merge(struct dir_context *ctx, const char *name, ...@@ -193,10 +194,10 @@ static int ovl_fill_merge(struct dir_context *ctx, const char *name,
container_of(ctx, struct ovl_readdir_data, ctx); container_of(ctx, struct ovl_readdir_data, ctx);
rdd->count++; rdd->count++;
if (!rdd->is_merge) if (!rdd->is_lowest)
return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type); return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
else else
return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type); return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
} }
static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd) static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
...@@ -289,7 +290,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list) ...@@ -289,7 +290,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
.ctx.actor = ovl_fill_merge, .ctx.actor = ovl_fill_merge,
.list = list, .list = list,
.root = RB_ROOT, .root = RB_ROOT,
.is_merge = false, .is_lowest = false,
}; };
int idx, next; int idx, next;
...@@ -306,7 +307,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list) ...@@ -306,7 +307,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
* allows offsets to be reasonably constant * allows offsets to be reasonably constant
*/ */
list_add(&rdd.middle, rdd.list); list_add(&rdd.middle, rdd.list);
rdd.is_merge = true; rdd.is_lowest = true;
err = ovl_dir_read(&realpath, &rdd); err = ovl_dir_read(&realpath, &rdd);
list_del(&rdd.middle); list_del(&rdd.middle);
} }
...@@ -577,3 +578,39 @@ void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list) ...@@ -577,3 +578,39 @@ void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
} }
inode_unlock(upper->d_inode); inode_unlock(upper->d_inode);
} }
static int ovl_check_d_type(struct dir_context *ctx, const char *name,
int namelen, loff_t offset, u64 ino,
unsigned int d_type)
{
struct ovl_readdir_data *rdd =
container_of(ctx, struct ovl_readdir_data, ctx);
/* Even if d_type is not supported, DT_DIR is returned for . and .. */
if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
return 0;
if (d_type != DT_UNKNOWN)
rdd->d_type_supported = true;
return 0;
}
/*
* Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
* if error is encountered.
*/
int ovl_check_d_type_supported(struct path *realpath)
{
int err;
struct ovl_readdir_data rdd = {
.ctx.actor = ovl_check_d_type,
.d_type_supported = false,
};
err = ovl_dir_read(realpath, &rdd);
if (err)
return err;
return rdd.d_type_supported;
}
...@@ -936,7 +936,8 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) ...@@ -936,7 +936,8 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
err = -EINVAL; err = -EINVAL;
if (!ufs->config.lowerdir) { if (!ufs->config.lowerdir) {
pr_err("overlayfs: missing 'lowerdir'\n"); if (!silent)
pr_err("overlayfs: missing 'lowerdir'\n");
goto out_free_config; goto out_free_config;
} }
...@@ -1028,6 +1029,21 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) ...@@ -1028,6 +1029,21 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
sb->s_flags |= MS_RDONLY; sb->s_flags |= MS_RDONLY;
ufs->workdir = NULL; ufs->workdir = NULL;
} }
/*
* Upper should support d_type, else whiteouts are visible.
* Given workdir and upper are on same fs, we can do
* iterate_dir() on workdir.
*/
err = ovl_check_d_type_supported(&workpath);
if (err < 0)
goto out_put_workdir;
if (!err) {
pr_err("overlayfs: upper fs needs to support d_type.\n");
err = -EINVAL;
goto out_put_workdir;
}
} }
err = -ENOMEM; err = -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment