Skip to contentSkip to conversation

ovl: create all new directories opaque

PATCH In review Amir Goldstein 2 participants 12 messages Nov 21, 2016 – Nov 30, 2016 Overlay filesystem

lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

PATCH 1/1 ovl: create all new directories opaque

There is really no good reason why not to make all directories opaque from birth.

The benefit of making directiories opaque on creation is that lookups can stop short when they reach the original created directory.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

---
 fs/overlayfs/dir.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
fs/overlayfs/dir.c +18 -7
index f1794af..cebcec0 100644
@@ -377,6 +377,8 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 	struct dentry *newdentry;
 	int err;
 	struct posix_acl *acl, *default_acl;
+	int is_dir = !hardlink && S_ISDIR(stat->mode);
+	int flags = RENAME_EXCHANGE;
 
 	if (WARN_ON(!workdir))
 		return -EROFS;
@@ -403,6 +405,10 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 	if (IS_ERR(upper))
 		goto out_dput;
 
+	/* mkdir can gets here with no upper whiteout - adjust exchange flag */
+	if (is_dir && d_is_negative(upper))
+		flags = 0;
+
 	err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
 	if (err)
 		goto out_dput2;
@@ -434,23 +440,26 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 			goto out_cleanup;
 	}
 
-	if (!hardlink && S_ISDIR(stat->mode)) {
+	if (is_dir) {
 		err = ovl_set_opaque(newdentry);
 		if (err)
 			goto out_cleanup;
 
-		err = ovl_do_rename(wdir, newdentry, udir, upper,
-				    RENAME_EXCHANGE);
+		err = ovl_do_rename(wdir, newdentry, udir, upper, flags);
 		if (err)
 			goto out_cleanup;
 
-		ovl_cleanup(wdir, upper);
+		if (!d_is_negative(upper))
+			ovl_cleanup(wdir, upper);
 	} else {
 		err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
 		if (err)
 			goto out_cleanup;
 	}
 	ovl_instantiate(dentry, inode, newdentry, !!hardlink);
+	/* All directories are created opaque */
+	if (is_dir)
+		ovl_dentry_set_opaque(dentry, true);
 	newdentry = NULL;
 out_dput2:
 	dput(upper);
@@ -477,6 +486,7 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 	int err;
 	const struct cred *old_cred;
 	struct cred *override_cred;
+	int is_dir = !hardlink && S_ISDIR(stat->mode);
 
 	err = ovl_copy_up(dentry->d_parent);
 	if (err)
@@ -500,7 +510,8 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 		put_cred(override_creds(override_cred));
 		put_cred(override_cred);
 
-		if (!ovl_dentry_is_whiteout(dentry))
+		/* Always create new directories opaque */
+		if (!ovl_dentry_is_whiteout(dentry) && !is_dir)
 			err = ovl_create_upper(dentry, inode, stat, link,
 						hardlink);
 		else
@@ -1017,7 +1028,7 @@ static int ovl_rename(struct inode *olddir, struct dentry *old,
 			err = ovl_set_redirect(old, samedir);
 			if (err)
 				goto out_dput;
-		} else if (!old_opaque && ovl_lower_positive(new)) {
+		} else if (!old_opaque) {
 			err = ovl_set_opaque(olddentry);
 			if (err)
 				goto out_dput;
@@ -1029,7 +1040,7 @@ static int ovl_rename(struct inode *olddir, struct dentry *old,
 			err = ovl_set_redirect(new, samedir);
 			if (err)
 				goto out_dput;
-		} else if (!new_opaque && ovl_lower_positive(old)) {
+		} else if (!new_opaque) {
 			err = ovl_set_opaque(newdentry);
 			if (err)
 				goto out_dput;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 10 lines · Amir Goldstein

There is really no good reason why not to make all directories opaque from birth.

The benefit of making directiories opaque on creation is that lookups can stop short when they reach the original created directory.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

That's true for directories created in a merged dir. Directories created in an opaque dir get no such advantage and creating the xattr will consume space and time.

So this should at least be conditional on the state of the parent.

Thanks,
Miklos

author maintainer
#2 lore raw
Quoted 12 lines · Miklos Szeredi
Quoted 10 lines · Amir Goldstein

There is really no good reason why not to make all directories opaque from birth.

The benefit of making directiories opaque on creation is that lookups can stop short when they reach the original created directory.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

Quoted 6 lines

That's true for directories created in a merged dir. Directories created in an opaque dir get no such advantage and creating the xattr will consume space and time.

So this should at least be conditional on the state of the parent.

Will do.
But do you at least agree to unconditionally set dir opaque on rename? There is really very little point is finding out if lower_is_positive()

Amir.

maintainer
#3 lore raw
Quoted 17 lines · Amir Goldstein
Quoted 16 lines · Miklos Szeredi
Quoted 10 lines · Amir Goldstein

There is really no good reason why not to make all directories opaque from birth.

The benefit of making directiories opaque on creation is that lookups can stop short when they reach the original created directory.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

That's true for directories created in a merged dir. Directories created in an opaque dir get no such advantage and creating the xattr will consume space and time.

Quoted 6 lines
Quoted 2 lines

So this should at least be conditional on the state of the parent.

Will do.
But do you at least agree to unconditionally set dir opaque on rename? There is really very little point is finding out if lower_is_positive()

Again: if dest parent is opaque, there's no need to set opaque on moved dir. Otherwise (if dest parent is merged) it does make sense to set opaque, regardless of whether it covers anything or not.

Thanks,
Miklos

author maintainer
#4 lore raw

PATCH v2 1/1 ovl: create directories inside merged parent opaque

The benefit of making directories opaque on creation is that lookups can stop short when they reach the original created directory, instead of continue lookup the entire depth of parent directory stack.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

---
 fs/overlayfs/dir.c | 51 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)
fs/overlayfs/dir.c +34 -17
index f1794af..c393605 100644
@@ -128,6 +128,13 @@ int ovl_create_real(struct inode *dir, struct dentry *newdentry,
 	return err;
 }
 
+static bool ovl_type_merge_or_lower(struct dentry *dentry)
+{
+	enum ovl_path_type type = ovl_path_type(dentry);
+
+	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
+}
+
 static int ovl_set_opaque(struct dentry *upperdentry)
 {
 	return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
@@ -377,6 +384,8 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 	struct dentry *newdentry;
 	int err;
 	struct posix_acl *acl, *default_acl;
+	int is_dir = !hardlink && S_ISDIR(stat->mode);
+	int flags = RENAME_EXCHANGE;
 
 	if (WARN_ON(!workdir))
 		return -EROFS;
@@ -403,6 +412,10 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 	if (IS_ERR(upper))
 		goto out_dput;
 
+	/* mkdir can get here with no upper whiteout - adjust exchange flag */
+	if (is_dir && d_is_negative(upper))
+		flags = 0;
+
 	err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
 	if (err)
 		goto out_dput2;
@@ -434,17 +447,19 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
 			goto out_cleanup;
 	}
 
-	if (!hardlink && S_ISDIR(stat->mode)) {
+	if (is_dir) {
 		err = ovl_set_opaque(newdentry);
 		if (err)
 			goto out_cleanup;
 
-		err = ovl_do_rename(wdir, newdentry, udir, upper,
-				    RENAME_EXCHANGE);
+		err = ovl_do_rename(wdir, newdentry, udir, upper, flags);
 		if (err)
 			goto out_cleanup;
 
-		ovl_cleanup(wdir, upper);
+		if (!d_is_negative(upper))
+			ovl_cleanup(wdir, upper);
+
+		ovl_dentry_set_opaque(dentry, true);
 	} else {
 		err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
 		if (err)
@@ -477,6 +492,7 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 	int err;
 	const struct cred *old_cred;
 	struct cred *override_cred;
+	int is_dir = !hardlink && S_ISDIR(stat->mode);
 
 	err = ovl_copy_up(dentry->d_parent);
 	if (err)
@@ -500,12 +516,14 @@ static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
 		put_cred(override_creds(override_cred));
 		put_cred(override_cred);
 
-		if (!ovl_dentry_is_whiteout(dentry))
-			err = ovl_create_upper(dentry, inode, stat, link,
-						hardlink);
-		else
+		/* Create new directories inside merged parent opaque */
+		if (ovl_dentry_is_whiteout(dentry) ||
+		    (is_dir && ovl_type_merge_or_lower(dentry->d_parent)))
 			err = ovl_create_over_whiteout(dentry, inode, stat,
 							link, hardlink);
+		else
+			err = ovl_create_upper(dentry, inode, stat, link,
+						hardlink);
 	}
 out_revert_creds:
 	revert_creds(old_cred);
@@ -782,13 +800,6 @@ static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
 	return ovl_do_remove(dentry, true);
 }
 
-static bool ovl_type_merge_or_lower(struct dentry *dentry)
-{
-	enum ovl_path_type type = ovl_path_type(dentry);
-
-	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
-}
-
 static bool ovl_can_move(struct dentry *dentry)
 {
 	return ovl_redirect_dir(dentry->d_sb) ||
@@ -1012,12 +1023,17 @@ static int ovl_rename(struct inode *olddir, struct dentry *old,
 	if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
 		goto out_dput;
 
+	/*
+	 * We need to make sure old/new dir is opaque if it is a pure upper
+	 * that is being moved into a merged directory
+	 */
 	if (is_dir) {
 		if (ovl_type_merge_or_lower(old)) {
 			err = ovl_set_redirect(old, samedir);
 			if (err)
 				goto out_dput;
-		} else if (!old_opaque && ovl_lower_positive(new)) {
+		} else if (!old_opaque &&
+			   ovl_type_merge_or_lower(new->d_parent)) {
 			err = ovl_set_opaque(olddentry);
 			if (err)
 				goto out_dput;
@@ -1029,7 +1045,8 @@ static int ovl_rename(struct inode *olddir, struct dentry *old,
 			err = ovl_set_redirect(new, samedir);
 			if (err)
 				goto out_dput;
-		} else if (!new_opaque && ovl_lower_positive(old)) {
+		} else if (!new_opaque &&
+			   ovl_type_merge_or_lower(old->d_parent)) {
 			err = ovl_set_opaque(newdentry);
 			if (err)
 				goto out_dput;
Signature · 1 line
2.7.4
maintainer
#5 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 8 lines · Amir Goldstein

The benefit of making directories opaque on creation is that lookups can stop short when they reach the original created directory, instead of continue lookup the entire depth of parent directory stack.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

I split this up, changed it around a bit and pushed the result to overlayfs-next.

Thanks,
Miklos

author maintainer
#6 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 6 lines · Miklos Szeredi
Quoted 5 lines · Amir Goldstein

The benefit of making directories opaque on creation is that lookups can stop short when they reach the original created directory, instead of continue lookup the entire depth of parent directory stack.

Quoted 7 lines
Quoted 3 lines

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

I split this up, changed it around a bit and pushed the result to overlayfs-next.

Nice cleanup. FYI, you left me as sole S-O-B on the change. Anyway, it passed my tests.

In case you are still interested, I rebase concurrent copy up on top of overlay-rorw and pushed to branch #concurrent_copyup on my github, with slightly improved commit message. But I guess this merge window is crowded enough already and I personally much rather see rorw merged than concurrent copy up.

Thanks,
Amir.

maintainer
#7 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 11 lines · Amir Goldstein
Quoted 10 lines · Miklos Szeredi
Quoted 8 lines · Amir Goldstein

The benefit of making directories opaque on creation is that lookups can stop short when they reach the original created directory, instead of continue lookup the entire depth of parent directory stack.

The best case is overlay with N layers, performing lookup for first level directory, which exists only in upper. In that case, there will be only one lookup instead of N.

Quoted 5 lines
Quoted 3 lines

I split this up, changed it around a bit and pushed the result to overlayfs-next.

Nice cleanup. FYI, you left me as sole S-O-B on the change.

Okay, fixed.

Quoted 7 lines

Anyway, it passed my tests.

In case you are still interested, I rebase concurrent copy up on top of overlay-rorw and pushed to branch #concurrent_copyup on my github, with slightly improved commit message. But I guess this merge window is crowded enough already and I personally much rather see rorw merged than concurrent copy up.

My problem with the concurrent copy up is the complexity of locking rules. Why not do some sort of overlay private lock here?

If a mutex is deemed too expensive, then just a bool "copying" in ovl_entry and a waitq in ovl_fs in would do.

Thanks,
Miklos

author maintainer
#8 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 8 lines · Miklos Szeredi
Quoted 6 lines · Amir Goldstein

In case you are still interested, I rebase concurrent copy up on top of overlay-rorw and pushed to branch #concurrent_copyup on my github, with slightly improved commit message. But I guess this merge window is crowded enough already and I personally much rather see rorw merged than concurrent copy up.

Quoted 6 lines

My problem with the concurrent copy up is the complexity of locking rules. Why not do some sort of overlay private lock here?

If a mutex is deemed too expensive, then just a bool "copying" in ovl_entry and a waitq in ovl_fs in would do.

I am quite certain that I was not able to explain my change properly and that if you understood it you wouldn't call it complex. If anything, it simplifies the locking rules.

Before my change, there is no clear definition of which locks are held when calling ovl_copy_up(), but for all call sites besides ovl_open_maybe_copy_up(), the overlay inode is already locked (by vfs).

My change only aligns the ovl_open_maybe_copy_up() case with the rest of the code paths by taking the inode lock before calling ovl_copy_up() and then the locking requirements for entry to ovl_copy_up() are uniform across the code (and documented).

So why invent a new private lock when the overlay inode lock is perfectly valid for the job?

After we have the inode lock in place, lock_rename() is taken when it is needed for a specific operation (when either upperdir or workdir need to be locked), so releasing it for data copy up is just a free gift.

Hope that I managed to clarify rather then confuse more.

Amir.

maintainer
#9 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 29 lines · Amir Goldstein
Quoted 8 lines · Miklos Szeredi
Quoted 6 lines · Amir Goldstein

In case you are still interested, I rebase concurrent copy up on top of overlay-rorw and pushed to branch #concurrent_copyup on my github, with slightly improved commit message. But I guess this merge window is crowded enough already and I personally much rather see rorw merged than concurrent copy up.

Quoted 6 lines

My problem with the concurrent copy up is the complexity of locking rules. Why not do some sort of overlay private lock here?

If a mutex is deemed too expensive, then just a bool "copying" in ovl_entry and a waitq in ovl_fs in would do.

I am quite certain that I was not able to explain my change properly and that if you understood it you wouldn't call it complex. If anything, it simplifies the locking rules.

Before my change, there is no clear definition of which locks are held when calling ovl_copy_up(), but for all call sites besides ovl_open_maybe_copy_up(), the overlay inode is already locked (by vfs).

My change only aligns the ovl_open_maybe_copy_up() case with the rest of the code paths by taking the inode lock before calling ovl_copy_up() and then the locking requirements for entry to ovl_copy_up() are uniform across the code (and documented).

Quoted 8 lines

So why invent a new private lock when the overlay inode lock is perfectly valid for the job?

After we have the inode lock in place, lock_rename() is taken when it is needed for a specific operation (when either upperdir or workdir need to be locked), so releasing it for data copy up is just a free gift.

Hope that I managed to clarify rather then confuse more.

It is documented and easy to verify that inode lock is held by the VFS for all but one case of copy_up(). However that one exception is not documented and pretty hard to verify. I.e. if there's a call path that results in d_real() being called with inode lock, it's going to deadlock. And if that call path is hard to trigger, then testing won't reveal this and the bug can remain undetected for a long time.

The current situation OTOH is perfectly fine, because copy up does not rely on overlay inode being locked, so it simply does not matter if the caller holds it or not.

Thanks,
Miklos

author maintainer
#10 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

Quoted 1 line · Miklos Szeredi

On Wed, Nov 30, 2016 at 3:49 PM, Amir Goldstein <[email protected]> wrote:

...

Quoted 8 lines
Quoted 8 lines

So why invent a new private lock when the overlay inode lock is perfectly valid for the job?

After we have the inode lock in place, lock_rename() is taken when it is needed for a specific operation (when either upperdir or workdir need to be locked), so releasing it for data copy up is just a free gift.

Quoted 9 lines
Quoted 1 line

Hope that I managed to clarify rather then confuse more.

It is documented and easy to verify that inode lock is held by the VFS for all but one case of copy_up(). However that one exception is not documented and pretty hard to verify. I.e. if there's a call path that results in d_real() being called with inode lock, it's going to deadlock. And if that call path is hard to trigger, then testing won't reveal this and the bug can remain undetected for a long time.

I see. I stand corrected. So I'll let you find the right candidate for copy up lock.

Amir.

author maintainer
#11 lore raw

Re: [PATCH v2] ovl: create directories inside merged parent opaque

In reply to Miklos Szeredi
Quoted 7 lines · Miklos Szeredi
Quoted 2 lines · Amir Goldstein

Nice cleanup. FYI, you left me as sole S-O-B on the change.

Okay, fixed.

Not pushed? yet?