backport lazytime fix to 4.19
Subsystems (2)
cover backport lazytime fix to 4.19
Backport a lazytime fix from upstream to 4.19-stable. The first commit had a trivial conflict in fs/xfs/ due to a file being renamed. Following that, the second commit is a clean cherry-pick.
Eric Biggers (1):
fs: fix lazytime expiration handling in __writeback_single_inode()
Jan Kara (1):
writeback: Drop I_DIRTY_TIME_EXPIRE
fs/ext4/inode.c | 2 +- fs/fs-writeback.c | 36 ++++++++++++++------------------ fs/xfs/xfs_trans_inode.c | 4 ++-- include/linux/fs.h | 1 - include/trace/events/writeback.h | 1 - 5 files changed, 19 insertions(+), 25 deletions(-)
Signature · 1 line
2.30.0
PATCH 1/2 writeback: Drop I_DIRTY_TIME_EXPIRE
From: Jan Kara <[email protected]>
commit 5fcd57505c002efc5823a7355e21f48dd02d5a51 upstream.
The only use of I_DIRTY_TIME_EXPIRE is to detect in __writeback_single_inode() that inode got there because flush worker decided it's time to writeback the dirty inode time stamps (either because we are syncing or because of age). However we can detect this directly in __writeback_single_inode() and there's no need for the strange propagation with I_DIRTY_TIME_EXPIRE flag.
--- fs/ext4/inode.c | 2 +- fs/fs-writeback.c | 28 +++++++++++----------------- fs/xfs/xfs_trans_inode.c | 4 ++-- include/linux/fs.h | 1 - include/trace/events/writeback.h | 1 - 5 files changed, 14 insertions(+), 22 deletions(-)
fs/ext4/inode.c +1 -1
@@ -5209,7 +5209,7 @@ static int other_inode_match(struct inode * inode, unsigned long ino, (inode->i_state & I_DIRTY_TIME)) {struct ext4_inode_info *ei =EXT4_I (inode); - inode->i_state &= ~(I_DIRTY_TIME |I_DIRTY_TIME_EXPIRED ); + inode->i_state &= ~I_DIRTY_TIME ;spin_unlock (&inode->i_lock);spin_lock (&ei->i_raw_lock);
fs/fs-writeback.c +11 -17
@@ -1157,7 +1157,7 @@ static bool inode_dirtied_after(struct inode *inode, unsigned long t)*/ static int move_expired_inodes (struct list_head *delaying_queue,struct list_head *dispatch_queue, -int flags,unsigned long dirtied_before) +unsigned long dirtied_before) {LIST_HEAD (tmp);struct list_head *pos, *node;
@@ -1173,8 +1173,6 @@ static int move_expired_inodes(struct list_head *delaying_queue,list_move (&inode->i_io_list, &tmp); moved++;spin_lock (&inode->i_lock); -if (flags &EXPIRE_DIRTY_ATIME ) - inode->i_state |=I_DIRTY_TIME_EXPIRED ; inode->i_state |=I_SYNC_QUEUED ;spin_unlock (&inode->i_lock);if (sb_is_blkdev_sb (inode->i_sb))
@@ -1222,11 +1220,11 @@ static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work,assert_spin_locked (&wb->list_lock);list_splice_init (&wb->b_more_io, &wb->b_io); - moved =move_expired_inodes (&wb->b_dirty, &wb->b_io,0 , dirtied_before); + moved =move_expired_inodes (&wb->b_dirty, &wb->b_io, dirtied_before);if (!work->for_sync) time_expire_jif = jiffies - dirtytime_expire_interval *HZ ; moved +=move_expired_inodes (&wb->b_dirty_time, &wb->b_io, -EXPIRE_DIRTY_ATIME , time_expire_jif); + time_expire_jif);if (moved)wb_io_lists_populated (wb);trace_writeback_queue_io (wb, work, dirtied_before, moved);
@@ -1402,18 +1400,14 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)spin_lock (&inode->i_lock); dirty = inode->i_state &I_DIRTY ; -if (inode->i_state &I_DIRTY_TIME ) { -if ((dirty &I_DIRTY_INODE ) || - wbc->sync_mode ==WB_SYNC_ALL || -unlikely (inode->i_state &I_DIRTY_TIME_EXPIRED ) || -unlikely (time_after (jiffies, - (inode->dirtied_time_when + - dirtytime_expire_interval *HZ )))) { - dirty |=I_DIRTY_TIME |I_DIRTY_TIME_EXPIRED ; -trace_writeback_lazytime (inode); - } - }else - inode->i_state &= ~I_DIRTY_TIME_EXPIRED ; +if ((inode->i_state &I_DIRTY_TIME ) && + ((dirty &I_DIRTY_INODE ) || + wbc->sync_mode ==WB_SYNC_ALL || wbc->for_sync || +time_after (jiffies, inode->dirtied_time_when + + dirtytime_expire_interval *HZ ))) { + dirty |=I_DIRTY_TIME ; +trace_writeback_lazytime (inode); + } inode->i_state &= ~dirty;/*
fs/xfs/xfs_trans_inode.c +2 -2
@@ -99,9 +99,9 @@ xfs_trans_log_inode(* to log the timestamps, or will clear already cleared fields in the * worst case. */ -if (inode->i_state & (I_DIRTY_TIME |I_DIRTY_TIME_EXPIRED )) { +if (inode->i_state &I_DIRTY_TIME ) {spin_lock (&inode->i_lock); - inode->i_state &= ~(I_DIRTY_TIME |I_DIRTY_TIME_EXPIRED ); + inode->i_state &= ~I_DIRTY_TIME ;spin_unlock (&inode->i_lock); }
include/linux/fs.h +0 -1
@@ -2071,7 +2071,6 @@ static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)#define I_DIO_WAKEUP (1 << __I_DIO_WAKEUP)#define I_LINKABLE (1 << 10)#define I_DIRTY_TIME (1 << 11) -#define I_DIRTY_TIME_EXPIRED (1 << 12)#define I_WB_SWITCH (1 << 13)#define I_OVL_INUSE (1 << 14)#define I_CREATING (1 << 15)
include/trace/events/writeback.h +0 -1
@@ -20,7 +20,6 @@ {I_CLEAR, "I_CLEAR"}, \ {I_SYNC, "I_SYNC"}, \ {I_DIRTY_TIME, "I_DIRTY_TIME"}, \ - {I_DIRTY_TIME_EXPIRED, "I_DIRTY_TIME_EXPIRED"}, \ {I_REFERENCED, "I_REFERENCED"} \ )
Signature · 1 line
2.30.0
PATCH 2/2 fs: fix lazytime expiration handling in __writeback_single_inode()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
commit 1e249cb5b7fc09ff216aa5a12f6c302e434e88f9 upstream.
When lazytime is enabled and an inode is being written due to its in-memory updated timestamps having expired, either due to a sync() or syncfs() system call or due to dirtytime_expire_interval having elapsed, the VFS needs to inform the filesystem so that the filesystem can copy the inode's timestamps out to the on-disk data structures.
This is done by __writeback_single_inode() calling mark_inode_dirty_sync(), which then calls ->dirty_inode(I_DIRTY_SYNC).
However, this occurs after __writeback_single_inode() has already cleared the dirty flags from ->i_state. This causes two bugs:
- mark_inode_dirty_sync() redirties the inode, causing it to remain dirty. This wastefully causes the inode to be written twice. But more importantly, it breaks cases where sync_filesystem() is expected to clean dirty inodes. This includes the FS_IOC_REMOVE_ENCRYPTION_KEY ioctl (as reported at
https://lore.kernel.org/r/[email protected]), as well as possibly filesystem freezing (freeze_super()).
- Since ->i_state doesn't contain I_DIRTY_TIME when ->dirty_inode() is called from __writeback_single_inode() for lazytime expiration, xfs_fs_dirty_inode() ignores the notification. (XFS only cares about lazytime expirations, and it assumes that i_state will contain I_DIRTY_TIME during those.) Therefore, lazy timestamps aren't persisted by sync(), syncfs(), or dirtytime_expire_interval on XFS.
Fix this by moving the call to mark_inode_dirty_sync() to earlier in __writeback_single_inode(), before the dirty flags are cleared from i_state. This makes filesystems be properly notified of the timestamp expiration, and it avoids incorrectly redirtying the inode.
This fixes xfstest generic/580 (which tests FS_IOC_REMOVE_ENCRYPTION_KEY) when run on ext4 or f2fs with lazytime enabled. It also fixes the new lazytime xfstest I've proposed, which reproduces the above-mentioned XFS bug
(https://lore.kernel.org/r/[email protected]).
Alternatively, we could call ->dirty_inode(I_DIRTY_SYNC) directly. But due to the introduction of I_SYNC_QUEUED, mark_inode_dirty_sync() is the right thing to do because mark_inode_dirty_sync() now knows not to move the inode to a writeback list if it is currently queued for sync.
Depends-on: 5afced3bf281 ("writeback: Avoid skipping inode writeback")
--- fs/fs-writeback.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
fs/fs-writeback.c +13 -11
@@ -1393,21 +1393,25 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) }/* -* Some filesystems may redirty the inode during the writeback -* due to delalloc, clear dirty metadata flags right before -* write_inode() +* If the inode has dirty timestamps and we need to write them, call +* mark_inode_dirty_sync() to notify the filesystem about it and to +* change I_DIRTY_TIME into I_DIRTY_SYNC. */ -spin_lock (&inode->i_lock); - - dirty = inode->i_state &I_DIRTY ;if ((inode->i_state &I_DIRTY_TIME ) && - ((dirty &I_DIRTY_INODE ) || - wbc->sync_mode ==WB_SYNC_ALL || wbc->for_sync || + (wbc->sync_mode ==WB_SYNC_ALL || wbc->for_sync ||time_after (jiffies, inode->dirtied_time_when + dirtytime_expire_interval *HZ ))) { - dirty |=I_DIRTY_TIME ;trace_writeback_lazytime (inode); +mark_inode_dirty_sync (inode); } + +/* +* Some filesystems may redirty the inode during the writeback +* due to delalloc, clear dirty metadata flags right before +* write_inode() +*/ +spin_lock (&inode->i_lock); + dirty = inode->i_state &I_DIRTY ; inode->i_state &= ~dirty;/*
@@ -1428,8 +1432,6 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)spin_unlock (&inode->i_lock); -if (dirty &I_DIRTY_TIME ) -mark_inode_dirty_sync (inode);/* Don't write the inode if only I_DIRTY_PAGES was set */ if (dirty & ~I_DIRTY_PAGES ) {int err =write_inode (inode, wbc);
Signature · 1 line
2.30.0