Re: [PATCH v7 7/7] ovl: implement containerized syncfs for overlayfs

From: Amir Goldstein

Date: Wed Nov 18 2026 - 13:09:40 EST


On Wed, Nov 18, 2026 at 6:03 PM Josef Bacik <josef@xxxxxxxxxxxxxx> wrote:
>
> From: Chengguang Xu <charliecgxu@xxxxxxxxxxx>
>
> Now overlayfs can only sync own dirty inodes during syncfs,
> so remove unnecessary sync_filesystem() on upper file system.
>
> Do that with fsync=relaxed only, which is what the default, fsync=auto,
> selects on most filesystems. fsync=on brings the old behaviour back.
>
> Take the steps of sync_filesystem() for the upper filesystem, but only for
> the tracked inodes, and go about it like the page cache does.
>
> Without wait, ->sync_fs() gives every inode that is marked dirty the
> towrite mark. That is what this sync has to write. Then it starts
> writeback of the upper inode of each of them, and replaces dirty and
> towrite by the writeback mark.
>
> With wait, for every inode with the writeback mark, it calls
> write_inode_for_sync(), which also hands over the timestamps that lazytime
> has deferred, and waits for the data, whoever has started to write it.
>
> Both end with sync_fs_and_bdev(), which is what sync_filesystem() does
> after the inodes. Do the same for unmount and for a read-only remount.
>
> A sync tags once, so tasks that modify files do not keep it going. The
> marks are shared by all syncs that are running, so it also writes and waits
> for what another one tags ahead of it. A walk only goes up, so it ends.
>
> In the page cache the completion of the I/O clears the writeback mark.
> Here whoever has waited does, and another sync may have started the inode
> again meanwhile. So the mark only goes if the upper inode is seen clean.
> Until then every sync sees the inode and waits for it as well, so they do
> not have to wait for each other. Otherwise the inode is marked dirty
> again, which also covers an upper inode that is open for writing: stores
> through a shared mapping do not call into overlayfs.
>
> That does not cover pages that are pinned through such a mapping. A direct
> read into it that completes after the last munmap() and close() dirties
> them when the inode may have left the set, and the next syncfs() does not
> write them.
>
> syncfs() now also returns the error of writeback that the flusher of the
> upper filesystem has done before, which it did not do so far. It compares
> the wb_err of the upper mapping with what it was when the inode entered the
> set. Unlike the error flags of the mapping, nobody can take that away, an
> fsync() for one. The error is recorded in the superblock of the overlay
> under the lock that protects the cursor, so another syncfs() that runs at
> the same time gets it as well.
>
> A read-only remount of the overlay syncs it the same way, so it now fails
> with such an error, once.
>
> Keep a freeze and a read-only remount of the upper filesystem out while an
> inode is handed over. If one has already begun when ->sync_fs() is to
> wait, sync the whole upper filesystem, like before. If one kept it from
> starting an inode and has gone away since, start that inode then.
>
> Upper inodes that have no overlay inode, like whiteouts and the work
> directories, are not tracked. It is up to ->sync_fs() and the block device
> to get them to disk. ext4, xfs and btrfs log all metadata, and ext4
> without a journal keeps it in the buffers of the block device.
>
> With many containers on one upper filesystem, stopping one of them no
> longer writes out the dirty data of all the others.
>
> Signed-off-by: Chengguang Xu <charliecgxu@xxxxxxxxxxx>
> [josef: everything after the first paragraph]
> Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> ---
> Documentation/filesystems/overlayfs.rst | 7 +-
> fs/overlayfs/overlayfs.h | 1 +
> fs/overlayfs/params.c | 14 +--
> fs/overlayfs/super.c | 3 +
> fs/overlayfs/sync.c | 217 ++++++++++++++++++++++++++++++++
> 5 files changed, 230 insertions(+), 12 deletions(-)
>
> diff --git a/Documentation/filesystems/overlayfs.rst b/Documentation/filesystems/overlayfs.rst
> index 8a007fbc5257..00e2cba65e2c 100644
> --- a/Documentation/filesystems/overlayfs.rst
> +++ b/Documentation/filesystems/overlayfs.rst
> @@ -830,7 +830,12 @@ Traditionally, syncfs(2) and unmount of an overlay sync the entire upper
> filesystem, including the dirty data of other overlays that share it.
> Overlayfs can be told to only write back the files that were modified
> through the overlay, and then ask the upper filesystem to commit its
> -metadata.
> +metadata and write its block device.
> +
> +In that mode syncfs(2) also returns the error of writeback of such a file
> +that has failed earlier, like fsync(2) on the file does. As after fsync(2),
> +a file that is opened on the upper filesystem later does not get that error
> +any more. A read-only remount of the overlay fails with such an error once.
>
> On traditional local filesystems with a single journal (e.g. ext4, xfs),
> fsync on a file also persists the parent directory changes, because they
> diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h
> index f4770b4c0222..63200551a7d0 100644
> --- a/fs/overlayfs/overlayfs.h
> +++ b/fs/overlayfs/overlayfs.h
> @@ -899,6 +899,7 @@ void ovl_forget_inode(struct inode *inode);
> void ovl_drop_tracked(struct ovl_fs *ofs);
> long ovl_nr_cached_objects(struct super_block *sb, struct shrink_control *sc);
> long ovl_free_cached_objects(struct super_block *sb, struct shrink_control *sc);
> +int ovl_sync_tracked(struct super_block *sb, int wait);
>
> /* vfs fileattr flags read from overlay.protattr xattr to ovl inode */
> #define OVL_PROT_I_FLAGS_MASK (S_APPEND | S_IMMUTABLE)
> diff --git a/fs/overlayfs/params.c b/fs/overlayfs/params.c
> index 370dd6341392..90111c480440 100644
> --- a/fs/overlayfs/params.c
> +++ b/fs/overlayfs/params.c
> @@ -774,22 +774,14 @@ static int ovl_reconfigure(struct fs_context *fc)
> {
> struct super_block *sb = fc->root->d_sb;
> struct ovl_fs *ofs = OVL_FS(sb);
> - struct super_block *upper_sb;
> - int ret = 0;
>
> if (!(fc->sb_flags & SB_RDONLY) && ovl_force_readonly(ofs))
> return -EROFS;
>
> - if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb)) {
> - upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
> - if (ovl_should_sync(ofs)) {
> - down_read(&upper_sb->s_umount);
> - ret = sync_filesystem(upper_sb);
> - up_read(&upper_sb->s_umount);
> - }
> - }
> + if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb) && ovl_should_sync(ofs))
> + return sync_filesystem(sb);
>
> - return ret;
> + return 0;
> }
>
> static const struct fs_context_operations ovl_context_ops = {
> diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
> index 7f7d3993bc7a..a2d527f62d75 100644
> --- a/fs/overlayfs/super.c
> +++ b/fs/overlayfs/super.c
> @@ -287,6 +287,9 @@ static int ovl_sync_fs(struct super_block *sb, int wait)
> if (!ret)
> return ret;
>
> + if (!ovl_should_sync_upper_fs(ofs))
> + return ovl_sync_tracked(sb, wait);
> +
> /*
> * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
> * All the super blocks will be iterated, including upper_sb.

Maybe also move ovl_sync_fs() to sync.c makes this
a very nice finale ;)

Thanks,
Amir.