Skip to contentSkip to conversation

ovl: fsync parent directory in copy-up

RFC PATCH In review Chengguang Xu 3 participants 7 messages Feb 26, 2022 – Mar 1, 2022 Overlay filesystem

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 ovl: fsync parent directory in copy-up

Calling fsync for parent directory in copy-up to
ensure the change get synced.

---
 fs/overlayfs/copy_up.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/overlayfs/copy_up.c +15 -0
index e040970408d4..52ca915f04a3 100644
@@ -944,6 +944,7 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
 {
 	int err;
 	DEFINE_DELAYED_CALL(done);
+	struct file *parent_file = NULL;
 	struct path parentpath;
 	struct ovl_copy_up_ctx ctx = {
 		.parent = parent,
@@ -972,6 +973,12 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
 				  AT_STATX_SYNC_AS_STAT);
 		if (err)
 			return err;
+
+		parent_file = ovl_path_open(&parentpath, O_WRONLY);
+		if (IS_ERR(parent_file)) {
+			err = PTR_ERR(parent_file);
+			return err;
+		}
 	}
 
 	/* maybe truncate regular file. this has no effect on dirs */
@@ -998,6 +1005,14 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
 			err = ovl_copy_up_meta_inode_data(&ctx);
 		ovl_copy_up_end(dentry);
 	}
+
+	if (!err) {
+		if (parent_file) {
+			vfs_fsync(parent_file, 0);
+			fput(parent_file);
+		}
+	}
+
 	do_delayed_call(&done);
 
 	return err;
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 3 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

This is not true for btrfs, but there are fewer users using overlayfs over btrfs (at least in the container world).

So while your patch is certainly "correct", for most users its effects will be only negative - performance penalty without fixing anything. So I think this change should be opt-in with Kconfig/module/mount option.

Unfortunately, there is currently no way to know whether the underlying filesystem needs the parent dir fsync or not.
I was trying to promote a VFS API for a weaker version of fsync for the parent dir [2] but that effort did not converge.

Thanks,
Amir.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 50 lines
---
 fs/overlayfs/copy_up.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
fs/overlayfs/copy_up.c +15 -1
index e040970408d4..52ca915f04a3 100644
@@ -944,6 +944,7 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
 {
        int err;
        DEFINE_DELAYED_CALL(done);
+       struct file *parent_file = NULL;
        struct path parentpath;
        struct ovl_copy_up_ctx ctx = {
                .parent = parent,
@@ -972,6 +973,12 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
                                  AT_STATX_SYNC_AS_STAT);
                if (err)
                        return err;
+
+               parent_file = ovl_path_open(&parentpath, O_WRONLY);
+               if (IS_ERR(parent_file)) {
+                       err = PTR_ERR(parent_file);
+                       return err;
+               }
        }
 
        /* maybe truncate regular file. this has no effect on dirs */
@@ -998,6 +1005,14 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
                        err = ovl_copy_up_meta_inode_data(&ctx);
                ovl_copy_up_end(dentry);
        }
+
+       if (!err) {
+               if (parent_file) {
+                       vfs_fsync(parent_file, 0);
+                       fput(parent_file);
+               }
+       }
+
        do_delayed_call(&done);
 
        return err;
--

2.27.0

#2 lore raw
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

I found this issue by eyeball scan when I was looking for the places which need to mark overlay inode dirty in change.

However, I think there are still some real world cases will be impacted by this kind of issue, for example, using docker build to make new docker image and power failure makes new image inconsistant.

Quoted 8 lines

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Quoted 6 lines

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

In the new test case (079) which I posted, I've tried xfs as underlying fs and found the parent of copy-up file didn't present after power failure. Am I missing something?

Quoted 7 lines

This is not true for btrfs, but there are fewer users using overlayfs over btrfs (at least in the container world).

So while your patch is certainly "correct", for most users its effects will be only negative - performance penalty without fixing anything. So I think this change should be opt-in with Kconfig/module/mount option.

I prefer to add this as standard option, but if Miklos and you guys all think it should be opt-in then mount option is also acceptable for me.

Thanks,
Chengguang

Quoted 61 lines

Unfortunately, there is currently no way to know whether the underlying filesystem needs the parent dir fsync or not.
I was trying to promote a VFS API for a weaker version of fsync for the parent dir [2] but that effort did not converge.

Thanks,
Amir.

[1] https://lore.kernel.org/linux-fsdevel/[email protected]/
[2] https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 49 lines
---
  fs/overlayfs/copy_up.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
fs/overlayfs/copy_up.c +15 -1
index e040970408d4..52ca915f04a3 100644
@@ -944,6 +944,7 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  {
         int err;
         DEFINE_DELAYED_CALL(done);
+       struct file *parent_file = NULL;
         struct path parentpath;
         struct ovl_copy_up_ctx ctx = {
                 .parent = parent,
@@ -972,6 +973,12 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
                                   AT_STATX_SYNC_AS_STAT);
                 if (err)
                         return err;
+
+               parent_file = ovl_path_open(&parentpath, O_WRONLY);
+               if (IS_ERR(parent_file)) {
+                       err = PTR_ERR(parent_file);
+                       return err;
+               }
         }
 
         /* maybe truncate regular file. this has no effect on dirs */
@@ -998,6 +1005,14 @@ static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
                         err = ovl_copy_up_meta_inode_data(&ctx);
                 ovl_copy_up_end(dentry);
         }
+
+       if (!err) {
+               if (parent_file) {
+                       vfs_fsync(parent_file, 0);
+                       fput(parent_file);
+               }
+       }
+
         do_delayed_call(&done);
 
         return err;
--

2.27.0

maintainer
#3 lore raw
Quoted 8 lines · Chengguang Xu
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

Quoted 8 lines

I found this issue by eyeball scan when I was looking for the places which need to mark overlay inode dirty in change.

However, I think there are still some real world cases will be impacted by this kind of issue,
for example, using docker build to make new docker image and power failure makes new
image inconsistant.

A very good example where the fsync of parent will be counter productive. The efficient way of building a docker image would be:
1. Write/copy up all the files
2. Writeback all the upper inodes
3. syncfs() upper fs

2 and 3 should happen on overlayfs umount as long as we properly marked all the copied up overlayfs inodes dirty.

So the question is not if parent dir needs fsync, but if it needs to be dirtied.

Quoted 13 lines
Quoted 13 lines

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

Quoted 8 lines
Quoted 3 lines

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

In the new test case (079) which I posted, I've tried xfs as underlying fs and found the parent of
copy-up file didn't present after power failure. Am I missing something?

I think you are.
The test does not reproduce an inconsistency.
The test reproduced changes that did not persist to storage after power failure and that is the expected behavior when a user does not fsync after making changes - unless the 'sync' or 'dirsync' mount options have been used.

I think your test will become correct if you use -o sync for the overlayfs mount and your patch will become correct if you do:

+ if (inode_needs_sync(d_inode(dentry)) {
+ parent_file = ovl_path_open(&parentpath, O_DIRECTORY|O_RDONLY);

Thanks,
Amir.

#4 lore raw
Quoted 55 lines · Amir Goldstein
Quoted 6 lines · Chengguang Xu
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

Quoted 8 lines

I found this issue by eyeball scan when I was looking for the places which need to mark overlay inode dirty in change.

However, I think there are still some real world cases will be impacted by this kind of issue,
for example, using docker build to make new docker image and power failure makes new
image inconsistant.

A very good example where the fsync of parent will be counter productive. The efficient way of building a docker image would be:
1. Write/copy up all the files
2. Writeback all the upper inodes
3. syncfs() upper fs

2 and 3 should happen on overlayfs umount as long as we properly marked all the copied up overlayfs inodes dirty.

So the question is not if parent dir needs fsync, but if it needs to be dirtied.

Quoted 11 lines
Quoted 11 lines

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

Quoted 7 lines
Quoted 3 lines

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

In the new test case (079) which I posted, I've tried xfs as underlying fs and found the parent of
copy-up file didn't present after power failure. Am I missing something?

I think you are.
The test does not reproduce an inconsistency.
The test reproduced changes that did not persist to storage after power failure and that is the expected behavior when a user does not fsync after making changes - unless the 'sync' or 'dirsync' mount options have been used.

I think your test will become correct if you use -o sync for the overlayfs mount and your patch will become correct if you do:

Review

Quoted 2 lines
(quoted diff) +2 -0
+        if (inode_needs_sync(d_inode(dentry)) {
+               parent_file = ovl_path_open(&parentpath, O_DIRECTORY|O_RDONLY);

Why should parent_file open with O_RDONLY flag? Meanwhile, I think the fix is not sufifcient for fully supporting 'dirsync' or 'sync' in overlayfs. Anyway, I think the description of expected behavior above makes sense so maybe the topic should turn to implement 'sync' or 'dirsync' mount options or reject specifying those options in overlayfs.

Thanks,
Chengguang

maintainer
#5 lore raw
Quoted 54 lines · Chengguang Xu
Quoted 52 lines · Amir Goldstein
Quoted 6 lines · Chengguang Xu
Quoted 5 lines · Amir Goldstein
Quoted 2 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

Quoted 8 lines

I found this issue by eyeball scan when I was looking for the places which need to mark overlay inode dirty in change.

However, I think there are still some real world cases will be impacted by this kind of issue,
for example, using docker build to make new docker image and power failure makes new
image inconsistant.

A very good example where the fsync of parent will be counter productive. The efficient way of building a docker image would be:
1. Write/copy up all the files
2. Writeback all the upper inodes
3. syncfs() upper fs

2 and 3 should happen on overlayfs umount as long as we properly marked all the copied up overlayfs inodes dirty.

So the question is not if parent dir needs fsync, but if it needs to be dirtied.

Quoted 11 lines
Quoted 11 lines

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

Quoted 7 lines
Quoted 3 lines

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

In the new test case (079) which I posted, I've tried xfs as underlying fs and found the parent of
copy-up file didn't present after power failure. Am I missing something?

I think you are.
The test does not reproduce an inconsistency.
The test reproduced changes that did not persist to storage after power failure and that is the expected behavior when a user does not fsync after making changes - unless the 'sync' or 'dirsync' mount options have been used.

Quoted 7 lines

Review

Quoted 5 lines

I think your test will become correct if you use -o sync for the overlayfs mount and your patch will become correct if you do:

(quoted diff) +2 -0
+        if (inode_needs_sync(d_inode(dentry)) {
+               parent_file = ovl_path_open(&parentpath, O_DIRECTORY|O_RDONLY);

Why should parent_file open with O_RDONLY flag? Meanwhile, I think the

It's a directory. What is the meaning of opening it for write? fsync doesn't need a writable fd.

Quoted 1 line

fix is not sufifcient for fully supporting 'dirsync' or 'sync' in overlayfs.

Right.

Quoted 3 lines

Anyway, I think the description of expected behavior above makes sense so maybe the topic should turn to implement 'sync' or 'dirsync' mount options or reject specifying those options in overlayfs.

If anything, I would reject them, unless you know of users that are going to use this functionality, but maybe you better complete the syncfs work before going into new adventures ;-)

Thanks,
Amir.

maintainer
#6 lore raw
In reply to Amir Goldstein
Quoted 23 lines · Amir Goldstein
Quoted 3 lines · Chengguang Xu

Calling fsync for parent directory in copy-up to
ensure the change get synced.

It is not clear to me that this change is really needed What if the reported problem?

Besides this can impact performance in some workloads.

The difference between parent copy up and file copy up is that failing to fsync to copied up data and linking/moving the upper file into place may result in corrupted data after power failure if temp file data is not synced.

Failing the fsync the parent dir OTOH may result in revert to lower file data after power failure.

The thing is, although POSIX gives you no such guarantee, with ext4/xfs fsync of the upper file itself will guarantee that parents will be present after power failure (see [1]).

Quoted 6 lines

This is not true for btrfs, but there are fewer users using overlayfs over btrfs (at least in the container world).

So while your patch is certainly "correct", for most users its effects will be only negative - performance penalty without fixing anything. So I think this change should be opt-in with Kconfig/module/mount option.

Probably should be made conditional on ovl_should_sync(ofs).

But before adding more options/complexity the performance impact
itself should be evaluated. My guess is that it will be minimal,
since directory copy ups will generally be orders of magnitude less frequent than file copy ups.

Thanks,
Miklos