Skip to content

Commit 0d94230

Browse files
Lukas Czernergregkh
authored andcommitted
fs: record I_DIRTY_TIME even if inode already has I_DIRTY_INODE
commit cbfecb9 upstream. Currently the I_DIRTY_TIME will never get set if the inode already has I_DIRTY_INODE with assumption that it supersedes I_DIRTY_TIME. That's true, however ext4 will only update the on-disk inode in ->dirty_inode(), not on actual writeback. As a result if the inode already has I_DIRTY_INODE state by the time we get to __mark_inode_dirty() only with I_DIRTY_TIME, the time was already filled into on-disk inode and will not get updated until the next I_DIRTY_INODE update, which might never come if we crash or get a power failure. The problem can be reproduced on ext4 by running xfstest generic/622 with -o iversion mount option. Fix it by allowing I_DIRTY_TIME to be set even if the inode already has I_DIRTY_INODE. Also make sure that the case is properly handled in writeback_single_inode() as well. Additionally changes in xfs_fs_dirty_inode() was made to accommodate for I_DIRTY_TIME in flag. Thanks Jan Kara for suggestions on how to make this work properly. Cc: Dave Chinner <david@fromorbit.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: stable@kernel.org Signed-off-by: Lukas Czerner <lczerner@redhat.com> Suggested-by: Jan Kara <jack@suse.cz> Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20220825100657.44217-1-lczerner@redhat.com Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 95a520b commit 0d94230

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

Documentation/filesystems/vfs.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ or bottom half).
274274
This is specifically for the inode itself being marked dirty,
275275
not its data. If the update needs to be persisted by fdatasync(),
276276
then I_DIRTY_DATASYNC will be set in the flags argument.
277+
I_DIRTY_TIME will be set in the flags in case lazytime is enabled
278+
and struct inode has times updated since the last ->dirty_inode
279+
call.
277280

278281
``write_inode``
279282
this method is called when the VFS needs to write an inode to

fs/fs-writeback.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,9 +1745,14 @@ static int writeback_single_inode(struct inode *inode,
17451745
*/
17461746
if (!(inode->i_state & I_DIRTY_ALL))
17471747
inode_cgwb_move_to_attached(inode, wb);
1748-
else if (!(inode->i_state & I_SYNC_QUEUED) &&
1749-
(inode->i_state & I_DIRTY))
1750-
redirty_tail_locked(inode, wb);
1748+
else if (!(inode->i_state & I_SYNC_QUEUED)) {
1749+
if ((inode->i_state & I_DIRTY))
1750+
redirty_tail_locked(inode, wb);
1751+
else if (inode->i_state & I_DIRTY_TIME) {
1752+
inode->dirtied_when = jiffies;
1753+
inode_io_list_move_locked(inode, wb, &wb->b_dirty_time);
1754+
}
1755+
}
17511756

17521757
spin_unlock(&wb->list_lock);
17531758
inode_sync_complete(inode);
@@ -2400,6 +2405,20 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24002405
trace_writeback_mark_inode_dirty(inode, flags);
24012406

24022407
if (flags & I_DIRTY_INODE) {
2408+
/*
2409+
* Inode timestamp update will piggback on this dirtying.
2410+
* We tell ->dirty_inode callback that timestamps need to
2411+
* be updated by setting I_DIRTY_TIME in flags.
2412+
*/
2413+
if (inode->i_state & I_DIRTY_TIME) {
2414+
spin_lock(&inode->i_lock);
2415+
if (inode->i_state & I_DIRTY_TIME) {
2416+
inode->i_state &= ~I_DIRTY_TIME;
2417+
flags |= I_DIRTY_TIME;
2418+
}
2419+
spin_unlock(&inode->i_lock);
2420+
}
2421+
24032422
/*
24042423
* Notify the filesystem about the inode being dirtied, so that
24052424
* (if needed) it can update on-disk fields and journal the
@@ -2409,7 +2428,8 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24092428
*/
24102429
trace_writeback_dirty_inode_start(inode, flags);
24112430
if (sb->s_op->dirty_inode)
2412-
sb->s_op->dirty_inode(inode, flags & I_DIRTY_INODE);
2431+
sb->s_op->dirty_inode(inode,
2432+
flags & (I_DIRTY_INODE | I_DIRTY_TIME));
24132433
trace_writeback_dirty_inode(inode, flags);
24142434

24152435
/* I_DIRTY_INODE supersedes I_DIRTY_TIME. */
@@ -2430,21 +2450,15 @@ void __mark_inode_dirty(struct inode *inode, int flags)
24302450
*/
24312451
smp_mb();
24322452

2433-
if (((inode->i_state & flags) == flags) ||
2434-
(dirtytime && (inode->i_state & I_DIRTY_INODE)))
2453+
if ((inode->i_state & flags) == flags)
24352454
return;
24362455

24372456
spin_lock(&inode->i_lock);
2438-
if (dirtytime && (inode->i_state & I_DIRTY_INODE))
2439-
goto out_unlock_inode;
24402457
if ((inode->i_state & flags) != flags) {
24412458
const int was_dirty = inode->i_state & I_DIRTY;
24422459

24432460
inode_attach_wb(inode, NULL);
24442461

2445-
/* I_DIRTY_INODE supersedes I_DIRTY_TIME. */
2446-
if (flags & I_DIRTY_INODE)
2447-
inode->i_state &= ~I_DIRTY_TIME;
24482462
inode->i_state |= flags;
24492463

24502464
/*
@@ -2517,7 +2531,6 @@ void __mark_inode_dirty(struct inode *inode, int flags)
25172531
out_unlock:
25182532
if (wb)
25192533
spin_unlock(&wb->list_lock);
2520-
out_unlock_inode:
25212534
spin_unlock(&inode->i_lock);
25222535
}
25232536
EXPORT_SYMBOL(__mark_inode_dirty);

fs/xfs/xfs_super.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,21 @@ xfs_fs_destroy_inode(
642642
static void
643643
xfs_fs_dirty_inode(
644644
struct inode *inode,
645-
int flag)
645+
int flags)
646646
{
647647
struct xfs_inode *ip = XFS_I(inode);
648648
struct xfs_mount *mp = ip->i_mount;
649649
struct xfs_trans *tp;
650650

651651
if (!(inode->i_sb->s_flags & SB_LAZYTIME))
652652
return;
653-
if (flag != I_DIRTY_SYNC || !(inode->i_state & I_DIRTY_TIME))
653+
654+
/*
655+
* Only do the timestamp update if the inode is dirty (I_DIRTY_SYNC)
656+
* and has dirty timestamp (I_DIRTY_TIME). I_DIRTY_TIME can be passed
657+
* in flags possibly together with I_DIRTY_SYNC.
658+
*/
659+
if ((flags & ~I_DIRTY_TIME) != I_DIRTY_SYNC || !(flags & I_DIRTY_TIME))
654660
return;
655661

656662
if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))

include/linux/fs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,13 +2288,14 @@ static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
22882288
* don't have to write inode on fdatasync() when only
22892289
* e.g. the timestamps have changed.
22902290
* I_DIRTY_PAGES Inode has dirty pages. Inode itself may be clean.
2291-
* I_DIRTY_TIME The inode itself only has dirty timestamps, and the
2291+
* I_DIRTY_TIME The inode itself has dirty timestamps, and the
22922292
* lazytime mount option is enabled. We keep track of this
22932293
* separately from I_DIRTY_SYNC in order to implement
22942294
* lazytime. This gets cleared if I_DIRTY_INODE
2295-
* (I_DIRTY_SYNC and/or I_DIRTY_DATASYNC) gets set. I.e.
2296-
* either I_DIRTY_TIME *or* I_DIRTY_INODE can be set in
2297-
* i_state, but not both. I_DIRTY_PAGES may still be set.
2295+
* (I_DIRTY_SYNC and/or I_DIRTY_DATASYNC) gets set. But
2296+
* I_DIRTY_TIME can still be set if I_DIRTY_SYNC is already
2297+
* in place because writeback might already be in progress
2298+
* and we don't want to lose the time update
22982299
* I_NEW Serves as both a mutex and completion notification.
22992300
* New inodes set I_NEW. If two processes both create
23002301
* the same inode, one of them will release its inode and

0 commit comments

Comments
 (0)