Re: [PATCH v7 4/7] fs: add write_inode_for_sync()

From: Jan Kara

Date: Thu Nov 19 2026 - 04:15:04 EST


On Wed 18-11-26 17:03:27, Josef Bacik wrote:
> sync_inodes_sb() has the flusher write every dirty inode of a superblock
> with WB_SYNC_ALL and for_sync set. That writes the pages without waiting
> for them, hands over the timestamps that lazytime has deferred while it
> holds I_SYNC, and calls ->write_inode() in a way that tells the filesystem
> that ->sync_fs() follows, so ext4 does not commit a transaction for every
> inode.
>
> overlayfs is about to sync some of the inodes of its upper filesystem, and
> none of the exported functions does that for one inode. write_inode_now()
> and sync_inode_metadata() either do not wait for somebody else who is
> writing the inode and leave deferred timestamps alone, or they wait for the
> pages, clear the error flags of the mapping and make ext4 commit.
>
> Add a function that writes one inode the way a sync does.
>
> Nothing calls it yet.
>
> Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>

Looks good. Feel free to add:

Reviewed-by: Jan Kara <jack@xxxxxxx>

Actually Ogawa wanted the very same function for FAT flushing of dirty data
on close so it looks like a generally useful primitive to have. Just the
description of the function is really hard to parse (at least to me as a
non-native speaker).

> +/**
> + * write_inode_for_sync - write an inode like a sync of its filesystem does
> + * @inode: inode to write to disk
> + *
> + * This does for one inode what sync_inodes_sb() has the flusher do for every
> + * dirty inode of a superblock. It writes all dirty pages, the timestamps that
> + * lazytime has deferred and the inode. It waits for somebody else who is
> + * writing the inode, but not for the pages it writes, and it does not ask the
> + * filesystem to commit anything.
> + *
> + * This is for a caller that syncs some of the inodes of a filesystem. It has
> + * to wait for the pages with filemap_fdatawait_keep_errors(), like
> + * wait_sb_inodes() does, and to call sync_fs_and_bdev() in the end.

I'd write:
This writes out inode like e.g. sync(2) would do it. On high level that
means we make sure anything dirty is submitted to the storage but we don't
wait for any completion. In particular, it means we wait if there's
somebody else submitting writeback for the inode (I_SYNC state set), we
make sure all dirty pages are submitted for writeback waiting for locks,
and we treat the inode as dirty if it has dirty timestamps. However we
don't wait for any IO to complete and we don't persist other inode-related
metadata through ->sync_inode_metadata(). If the caller wants data
persistency guarantee, it has to wait for IO completion (e.g. using
filemap_fdatawait_keep_errors()) and also persist common filesystem
metadata using sync_fs_and_bdev().

> + *
> + * The caller must have a ref on the inode.
> + *
> + * Return: 0 on success, a negative error code otherwise.

Honza
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR