projects/linux·commit 70f2ae1f

Merge tag 'ovl-fixes-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs

70f2ae1f · Linus Torvalds <[email protected]> · 2018-06-07 08:53:50 -07:00

Pull overlayfs fixes from Miklos Szeredi:
 "This contains a fix for the vfs_mkdir() issue discovered by Al, as
  well as other fixes and cleanups"

* tag 'ovl-fixes-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs:
  ovl: use inode_insert5() to hash a newly created inode
  ovl: Pass argument to ovl_get_inode() in a structure
  vfs: factor out inode_insert5()
  ovl: clean up copy-up error paths
  ovl: return EIO on internal error
  ovl: make ovl_create_real() cope with vfs_mkdir() safely
  ovl: create helper ovl_create_temp()
  ovl: return dentry from ovl_create_real()
  ovl: struct cattr cleanups
  ovl: strip debug argument from ovl_do_ helpers
  ovl: remove WARN_ON() real inode attributes mismatch
  ovl: Kconfig documentation fixes
  ovl: update documentation for unionmount-testsuite
Commit
70f2ae1f002b0ed4b4382210df8e4b6e54079012
Parents
da315f6e · 01b39dcc

Merge commit. Changes below are compared with the first parent: da315f6e.

Changes

11 files changed · +319 −273

Only the first 600 diff lines are shown. Download the complete patch.

Documentation/filesystems/overlayfs.txt

View file
diff --git a/Documentation/filesystems/overlayfs.txt b/Documentation/filesystems/overlayfs.txt
index 961b287..72615a2 100644
--- a/Documentation/filesystems/overlayfs.txt
+++ b/Documentation/filesystems/overlayfs.txt
@@ -429,11 +429,12 @@ This verification may cause significant overhead in some cases.
Testsuite
---------
-There's testsuite developed by David Howells at:
+There's a testsuite originally developed by David Howells and currently
+maintained by Amir Goldstein at:
- git://git.infradead.org/users/dhowells/unionmount-testsuite.git
+ https://github.com/amir73il/unionmount-testsuite.git
Run as root:
# cd unionmount-testsuite
- # ./run --ov
+ # ./run --ov --verify

fs/inode.c

View file
diff --git a/fs/inode.c b/fs/inode.c
index 3b55391..0df41bb 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -1004,6 +1004,70 @@ void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
EXPORT_SYMBOL(unlock_two_nondirectories);
/**
+ * inode_insert5 - obtain an inode from a mounted file system
+ * @inode: pre-allocated inode to use for insert to cache
+ * @hashval: hash value (usually inode number) to get
+ * @test: callback used for comparisons between inodes
+ * @set: callback used to initialize a new struct inode
+ * @data: opaque data pointer to pass to @test and @set
+ *
+ * Search for the inode specified by @hashval and @data in the inode cache,
+ * and if present it is return it with an increased reference count. This is
+ * a variant of iget5_locked() for callers that don't want to fail on memory
+ * allocation of inode.
+ *
+ * If the inode is not in cache, insert the pre-allocated inode to cache and
+ * return it locked, hashed, and with the I_NEW flag set. The file system gets
+ * to fill it in before unlocking it via unlock_new_inode().
+ *
+ * Note both @test and @set are called with the inode_hash_lock held, so can't
+ * sleep.
+ */
+struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
+ int (*test)(struct inode *, void *),
+ int (*set)(struct inode *, void *), void *data)
+{
+ struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
+ struct inode *old;
+
+again:
+ spin_lock(&inode_hash_lock);
+ old = find_inode(inode->i_sb, head, test, data);
+ if (unlikely(old)) {
+ /*
+ * Uhhuh, somebody else created the same inode under us.
+ * Use the old inode instead of the preallocated one.
+ */
+ spin_unlock(&inode_hash_lock);
+ wait_on_inode(old);
+ if (unlikely(inode_unhashed(old))) {
+ iput(old);
+ goto again;
+ }
+ return old;
+ }
+
+ if (set && unlikely(set(inode, data))) {
+ inode = NULL;
+ goto unlock;
+ }
+
+ /*
+ * Return the locked inode with I_NEW set, the
+ * caller is responsible for filling in the contents
+ */
+ spin_lock(&inode->i_lock);
+ inode->i_state |= I_NEW;
+ hlist_add_head(&inode->i_hash, head);
+ spin_unlock(&inode->i_lock);
+unlock:
+ spin_unlock(&inode_hash_lock);
+
+ return inode;
+}
+EXPORT_SYMBOL(inode_insert5);
+
+/**
* iget5_locked - obtain an inode from a mounted file system
* @sb: super block of file system
* @hashval: hash value (usually inode number) to get
@@ -1027,66 +1091,18 @@ struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
int (*test)(struct inode *, void *),
int (*set)(struct inode *, void *), void *data)
{
- struct hlist_head *head = inode_hashtable + hash(sb, hashval);
- struct inode *inode;
-again:
- spin_lock(&inode_hash_lock);
- inode = find_inode(sb, head, test, data);
- spin_unlock(&inode_hash_lock);
+ struct inode *inode = ilookup5(sb, hashval, test, data);
- if (inode) {
- wait_on_inode(inode);
- if (unlikely(inode_unhashed(inode))) {
- iput(inode);
- goto again;
- }
- return inode;
- }
+ if (!inode) {
+ struct inode *new = new_inode(sb);
- inode = alloc_inode(sb);
- if (inode) {
- struct inode *old;
-
- spin_lock(&inode_hash_lock);
- /* We released the lock, so.. */
- old = find_inode(sb, head, test, data);
- if (!old) {
- if (set(inode, data))
- goto set_failed;
-
- spin_lock(&inode->i_lock);
- inode->i_state = I_NEW;
- hlist_add_head(&inode->i_hash, head);
- spin_unlock(&inode->i_lock);
- inode_sb_list_add(inode);
- spin_unlock(&inode_hash_lock);
-
- /* Return the locked inode with I_NEW set, the
- * caller is responsible for filling in the contents
- */
- return inode;
- }
-
- /*
- * Uhhuh, somebody else created the same inode under
- * us. Use the old inode instead of the one we just
- * allocated.
- */
- spin_unlock(&inode_hash_lock);
- destroy_inode(inode);
- inode = old;
- wait_on_inode(inode);
- if (unlikely(inode_unhashed(inode))) {
- iput(inode);
- goto again;
+ if (new) {
+ inode = inode_insert5(new, hashval, test, set, data);
+ if (unlikely(inode != new))
+ iput(new);
}
}
return inode;
-
-set_failed:
- spin_unlock(&inode_hash_lock);
- destroy_inode(inode);
- return NULL;
}
EXPORT_SYMBOL(iget5_locked);
@@ -1427,43 +1443,13 @@ EXPORT_SYMBOL(insert_inode_locked);
int insert_inode_locked4(struct inode *inode, unsigned long hashval,
int (*test)(struct inode *, void *), void *data)
{
- struct super_block *sb = inode->i_sb;
- struct hlist_head *head = inode_hashtable + hash(sb, hashval);
+ struct inode *old = inode_insert5(inode, hashval, test, NULL, data);
- while (1) {
- struct inode *old = NULL;
-
- spin_lock(&inode_hash_lock);
- hlist_for_each_entry(old, head, i_hash) {
- if (old->i_sb != sb)
- continue;
- if (!test(old, data))
- continue;
- spin_lock(&old->i_lock);
- if (old->i_state & (I_FREEING|I_WILL_FREE)) {
- spin_unlock(&old->i_lock);
- continue;
- }
- break;
- }
- if (likely(!old)) {
- spin_lock(&inode->i_lock);
- inode->i_state |= I_NEW;
- hlist_add_head(&inode->i_hash, head);
- spin_unlock(&inode->i_lock);
- spin_unlock(&inode_hash_lock);
- return 0;
- }
- __iget(old);
- spin_unlock(&old->i_lock);
- spin_unlock(&inode_hash_lock);
- wait_on_inode(old);
- if (unlikely(!inode_unhashed(old))) {
- iput(old);
- return -EBUSY;
- }
+ if (old != inode) {
iput(old);
+ return -EBUSY;
}
+ return 0;
}
EXPORT_SYMBOL(insert_inode_locked4);

fs/overlayfs/Kconfig

View file
diff --git a/fs/overlayfs/Kconfig b/fs/overlayfs/Kconfig
index 1703263..9384164 100644
--- a/fs/overlayfs/Kconfig
+++ b/fs/overlayfs/Kconfig
@@ -11,7 +11,7 @@ config OVERLAY_FS
For more information see Documentation/filesystems/overlayfs.txt
config OVERLAY_FS_REDIRECT_DIR
- bool "Overlayfs: turn on redirect dir feature by default"
+ bool "Overlayfs: turn on redirect directory feature by default"
depends on OVERLAY_FS
help
If this config option is enabled then overlay filesystems will use
@@ -46,7 +46,7 @@ config OVERLAY_FS_INDEX
depends on OVERLAY_FS
help
If this config option is enabled then overlay filesystems will use
- the inodes index dir to map lower inodes to upper inodes by default.
+ the index directory to map lower inodes to upper inodes by default.
In this case it is still possible to turn off index globally with the
"index=off" module option or on a filesystem instance basis with the
"index=off" mount option.
@@ -66,7 +66,7 @@ config OVERLAY_FS_NFS_EXPORT
depends on OVERLAY_FS_INDEX
help
If this config option is enabled then overlay filesystems will use
- the inodes index dir to decode overlay NFS file handles by default.
+ the index directory to decode overlay NFS file handles by default.
In this case, it is still possible to turn off NFS export support
globally with the "nfs_export=off" module option or on a filesystem
instance basis with the "nfs_export=off" mount option.

fs/overlayfs/copy_up.c

View file
diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c
index 8bede07..ddaddb4 100644
--- a/fs/overlayfs/copy_up.c
+++ b/fs/overlayfs/copy_up.c
@@ -365,17 +365,14 @@ static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
if (err)
return err;
- temp = ovl_lookup_temp(indexdir);
+ temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
+ err = PTR_ERR(temp);
if (IS_ERR(temp))
- goto temp_err;
-
- err = ovl_do_mkdir(dir, temp, S_IFDIR, true);
- if (err)
- goto out;
+ goto free_name;
err = ovl_set_upper_fh(upper, temp);
if (err)
- goto out_cleanup;
+ goto out;
index = lookup_one_len(name.name, indexdir, name.len);
if (IS_ERR(index)) {
@@ -384,23 +381,13 @@ static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
err = ovl_do_rename(dir, temp, dir, index, 0);
dput(index);
}
-
- if (err)
- goto out_cleanup;
-
out:
+ if (err)
+ ovl_cleanup(dir, temp);
dput(temp);
+free_name:
kfree(name.name);
return err;
-
-temp_err:
- err = PTR_ERR(temp);
- temp = NULL;
- goto out;
-
-out_cleanup:
- ovl_cleanup(dir, temp);
- goto out;
}
struct ovl_copy_up_ctx {
@@ -439,8 +426,7 @@ static int ovl_link_up(struct ovl_copy_up_ctx *c)
c->dentry->d_name.len);
err = PTR_ERR(upper);
if (!IS_ERR(upper)) {
- err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper,
- true);
+ err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
dput(upper);
if (!err) {
@@ -470,7 +456,7 @@ static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
return PTR_ERR(upper);
if (c->tmpfile)
- err = ovl_do_link(temp, udir, upper, true);
+ err = ovl_do_link(temp, udir, upper);
else
err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
@@ -481,13 +467,13 @@ static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
return err;
}
-static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
+static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
{
int err;
struct dentry *temp;
const struct cred *old_creds = NULL;
struct cred *new_creds = NULL;
- struct cattr cattr = {
+ struct ovl_cattr cattr = {
/* Can't properly set mode on creation because of the umask */
.mode = c->stat.mode & S_IFMT,
.rdev = c->stat.rdev,
@@ -495,41 +481,24 @@ static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
};
err = security_inode_copy_up(c->dentry, &new_creds);
+ temp = ERR_PTR(err);
if (err < 0)
goto out;
if (new_creds)
old_creds = override_creds(new_creds);
- if (c->tmpfile) {
+ if (c->tmpfile)
temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
- if (IS_ERR(temp))
- goto temp_err;
- } else {
- temp = ovl_lookup_temp(c->workdir);
- if (IS_ERR(temp))
- goto temp_err;
-
- err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
- NULL, true);
- if (err) {
- dput(temp);
- goto out;
- }
- }
- err = 0;
- *tempp = temp;
+ else
+ temp = ovl_create_temp(c->workdir, &cattr);
out:
if (new_creds) {
revert_creds(old_creds);
put_cred(new_creds);
}
- return err;
-
-temp_err:
- err = PTR_ERR(temp);
- goto out;
+ return temp;
}
static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
@@ -579,21 +548,21 @@ static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
struct inode *udir = c->destdir->d_inode;
struct inode *inode;
struct dentry *newdentry = NULL;
- struct dentry *temp = NULL;
+ struct dentry *temp;
int err;
- err = ovl_get_tmpfile(c, &temp);
- if (err)
- goto out;
+ temp = ovl_get_tmpfile(c);
+ if (IS_ERR(temp))
+ return PTR_ERR(temp);
err = ovl_copy_up_inode(c, temp);
if (err)
- goto out_cleanup;
+ goto out;
if (S_ISDIR(c->stat.mode) && c->indexed) {
err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
if (err)
- goto out_cleanup;
+ goto out;
}
if (c->tmpfile) {
@@ -604,7 +573,7 @@ static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
err = ovl_install_temp(c, temp, &newdentry);
}
if (err)
- goto out_cleanup;
+ goto out;
inode = d_inode(c->dentry);
ovl_inode_update(inode, newdentry);
@@ -612,13 +581,11 @@ static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
ovl_set_flag(OVL_WHITEOUTS, inode);
out:
+ if (err && !c->tmpfile)
+ ovl_cleanup(d_inode(c->workdir), temp);
dput(temp);
return err;
-out_cleanup:
- if (!c->tmpfile)
- ovl_cleanup(d_inode(c->workdir), temp);
- goto out;
}
/*

fs/overlayfs/dir.c

View file
diff --git a/fs/overlayfs/dir.c b/fs/overlayfs/dir.c
index 839709c..f480b1a 100644
--- a/fs/overlayfs/dir.c
+++ b/fs/overlayfs/dir.c
@@ -43,7 +43,7 @@ int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
return err;
}
-struct dentry *ovl_lookup_temp(struct dentry *workdir)
+static struct dentry *ovl_lookup_temp(struct dentry *workdir)
{
struct dentry *temp;
char name[20];
@@ -114,36 +114,72 @@ kill_whiteout:
goto out;
}
-int ovl_create_real(struct inode *dir, struct dentry *newdentry,
- struct cattr *attr, struct dentry *hardlink, bool debug)
+static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
+ umode_t mode)
{
int err;
+ struct dentry *d, *dentry = *newdentry;
+ err = ovl_do_mkdir(dir, dentry, mode);
+ if (err)
+ return err;
+
+ if (likely(!d_unhashed(dentry)))
+ return 0;
+
+ /*
+ * vfs_mkdir() may succeed and leave the dentry passed
+ * to it unhashed and negative. If that happens, try to
+ * lookup a new hashed and positive dentry.
+ */
+ d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
+ dentry->d_name.len);
+ if (IS_ERR(d)) {
+ pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
+ dentry, err);
+ return PTR_ERR(d);
+ }
+ dput(dentry);
+ *newdentry = d;
+
+ return 0;
+}
+
+struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
+ struct ovl_cattr *attr)
+{
+ int err;
+
+ if (IS_ERR(newdentry))
+ return newdentry;
+
+ err = -ESTALE;
if (newdentry->d_inode)
- return -ESTALE;
+ goto out;
- if (hardlink) {
- err = ovl_do_link(hardlink, dir, newdentry, debug);
+ if (attr->hardlink) {
+ err = ovl_do_link(attr->hardlink, dir, newdentry);
} else {
switch (attr->mode & S_IFMT) {
case S_IFREG:
- err = ovl_do_create(dir, newdentry, attr->mode, debug);
+ err = ovl_do_create(dir, newdentry, attr->mode);
break;
case S_IFDIR:
- err = ovl_do_mkdir(dir, newdentry, attr->mode, debug);
+ /* mkdir is special... */
+ err = ovl_mkdir_real(dir, &newdentry, attr->mode);
break;
case S_IFCHR:
case S_IFBLK:
case S_IFIFO:
case S_IFSOCK:
- err = ovl_do_mknod(dir, newdentry,
- attr->mode, attr->rdev, debug);
+ err = ovl_do_mknod(dir, newdentry, attr->mode,
+ attr->rdev);
break;
case S_IFLNK:
- err = ovl_do_symlink(dir, newdentry, attr->link, debug);
+ err = ovl_do_symlink(dir, newdentry, attr->link);
break;
default:
@@ -155,9 +191,20 @@ int ovl_create_real(struct inode *dir, struct dentry *newdentry,
* Not quite sure if non-instantiated dentry is legal or not.
* VFS doesn't seem to care so check and warn here.
*/
- err = -ENOENT;
+ err = -EIO;
}
- return err;
+out:
+ if (err) {
+ dput(newdentry);
+ return ERR_PTR(err);
+ }
+ return newdentry;
+}
+
+struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
+{
+ return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
+ attr);
}
static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
@@ -182,24 +229,54 @@ static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
}
-/* Common operations required to be done after creation of file on upper */
-static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
- struct dentry *newdentry, bool hardlink)
+/*
+ * Common operations required to be done after creation of file on upper.
+ * If @hardlink is false, then @inode is a pre-allocated inode, we may or
+ * may not use to instantiate the new dentry.
+ */
+static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
+ struct dentry *newdentry, bool hardlink)
{
+ struct ovl_inode_params oip = {
+ .upperdentry = newdentry,
+ .newinode = inode,
+ };
+
ovl_dentry_version_inc(dentry->d_parent, false);
ovl_dentry_set_upper_alias(dentry);
if (!hardlink) {
- ovl_inode_update(inode, newdentry);
- ovl_copyattr(newdentry->d_inode, inode);
+ /*
+ * ovl_obtain_alias() can be called after ovl_create_real()
+ * and before we get here, so we may get an inode from cache
+ * with the same real upperdentry that is not the inode we
+ * pre-allocated. In this case we will use the cached inode
+ * to instantiate the new dentry.
+ *
+ * XXX: if we ever use ovl_obtain_alias() to decode directory
+ * file handles, need to use ovl_get_inode_locked() and
+ * d_instantiate_new() here to prevent from creating two
+ * hashed directory inode aliases.
+ */
+ inode = ovl_get_inode(dentry->d_sb, &oip);
+ if (WARN_ON(IS_ERR(inode)))
+ return PTR_ERR(inode);
} else {
WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
dput(newdentry);
inc_nlink(inode);
}

This diff is incomplete. Download the complete patch.

fs/overlayfs/export.c

View file

This diff is incomplete. Download the complete patch.

fs/overlayfs/inode.c

View file

This diff is incomplete. Download the complete patch.

fs/overlayfs/namei.c

View file

This diff is incomplete. Download the complete patch.

fs/overlayfs/overlayfs.h

View file

This diff is incomplete. Download the complete patch.

fs/overlayfs/super.c

View file

This diff is incomplete. Download the complete patch.

include/linux/fs.h

View file

This diff is incomplete. Download the complete patch.