Skip to content

Commit 81a3ca9

Browse files
ebiggersjosephhz
authored andcommitted
fs: fix lazytime expiration handling in __writeback_single_inode()
ANBZ: torvalds#752 commit 1e249cb 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. Fixes: 0ae45f6 ("vfs: add support for a lazytime mount option") Cc: [email protected] Depends-on: 5afced3 ("writeback: Avoid skipping inode writeback") Link: https://lore.kernel.org/r/[email protected] Suggested-by: Jan Kara <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Jan Kara <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Joseph Qi <[email protected]> Reviewed-by: Gao Xiang <[email protected]>
1 parent 2030fca commit 81a3ca9

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

fs/fs-writeback.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,21 +1537,25 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
15371537
}
15381538

15391539
/*
1540-
* Some filesystems may redirty the inode during the writeback
1541-
* due to delalloc, clear dirty metadata flags right before
1542-
* write_inode()
1540+
* If the inode has dirty timestamps and we need to write them, call
1541+
* mark_inode_dirty_sync() to notify the filesystem about it and to
1542+
* change I_DIRTY_TIME into I_DIRTY_SYNC.
15431543
*/
1544-
spin_lock(&inode->i_lock);
1545-
1546-
dirty = inode->i_state & I_DIRTY;
15471544
if ((inode->i_state & I_DIRTY_TIME) &&
1548-
((dirty & I_DIRTY_INODE) ||
1549-
wbc->sync_mode == WB_SYNC_ALL || wbc->for_sync ||
1545+
(wbc->sync_mode == WB_SYNC_ALL || wbc->for_sync ||
15501546
time_after(jiffies, inode->dirtied_time_when +
15511547
dirtytime_expire_interval * HZ))) {
1552-
dirty |= I_DIRTY_TIME;
15531548
trace_writeback_lazytime(inode);
1549+
mark_inode_dirty_sync(inode);
15541550
}
1551+
1552+
/*
1553+
* Some filesystems may redirty the inode during the writeback
1554+
* due to delalloc, clear dirty metadata flags right before
1555+
* write_inode()
1556+
*/
1557+
spin_lock(&inode->i_lock);
1558+
dirty = inode->i_state & I_DIRTY;
15551559
inode->i_state &= ~dirty;
15561560

15571561
/*
@@ -1572,8 +1576,6 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
15721576

15731577
spin_unlock(&inode->i_lock);
15741578

1575-
if (dirty & I_DIRTY_TIME)
1576-
mark_inode_dirty_sync(inode);
15771579
/* Don't write the inode if only I_DIRTY_PAGES was set */
15781580
if (dirty & ~I_DIRTY_PAGES) {
15791581
int err = write_inode(inode, wbc);

0 commit comments

Comments
 (0)