Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp884504pxb; Sat, 9 Jan 2021 00:02:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJz3vhgbbmVj+Y7w5Yj6hZSYx2CbbOiTwIuwRVno5NreoNO8vWDo6x0wFPFAhXDfBVl7EU9f X-Received: by 2002:a50:ee1a:: with SMTP id g26mr7876719eds.68.1610179348191; Sat, 09 Jan 2021 00:02:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610179348; cv=none; d=google.com; s=arc-20160816; b=y1leICYAM3uR9B1d7BV94H6xcycoImidmJ7+0/28XlhEMqNXaRIIppipdvA3lW3fX4 M2Wo4ctoK7ZgqIS1aOJiA3RI4ycWZdwdqPDq86nASR6gEjD812gTwBCe09POIAEA5b78 tyn54At3rP7vbIgczZMOxHRuvsZgcsHx1eIK8sweMBGusw4iEAFSREsXDIY6ELCn9DyB QjQzZvLzKcbeCDe4OkPaJ/2FcowgybV4W3hUNKLTOSogI0lSqKER7txBL/VnGpS/AytA FhNbeBMej8ySfwTw6qBlkWSVraWhiz8V6dmvZalFpOc52JTI7spagpQhEN6lObBZRzvf P47Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Vyz3PvZWX+0nrTyJk7a/rK3gYW5EZxMAqY3V4u2l0pg=; b=k0oiJ7jkgh5rWw/tBH/totb2nyQfSd1ZF1jTbH5Qi3+AWjzSV19/kvZM1wku9UebHN KvW/AKS0vfwskHPyAC7N33LNYE5lyMbBLxgETOUF2AM1+WOoh0u4i9NbhEVp8GoeCtmj sJZ2MvyrlJNaSjkuVFVEYpc4KPRJ9U8kU7WYHS0VsCbJHwCHI2Uo5xmQYR7HYYhP072q oUbQIipLtuOFfyUnDaLqcGMbVw1XgTNb2XYvcf+0mWaYytj5EqONkGwwd4nK+KGxPiWg GzvIML5TschufYWH4Uo/gWBp62/QBFNIfkcmJQuKXczdDzGjOLX9IK8bzNjAsU7bZVQM PygQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XU4zvhFa; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a89si4522040edf.215.2021.01.09.00.02.05; Sat, 09 Jan 2021 00:02:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XU4zvhFa; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726607AbhAIIAZ (ORCPT + 99 others); Sat, 9 Jan 2021 03:00:25 -0500 Received: from mail.kernel.org ([198.145.29.99]:40952 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726283AbhAIIAZ (ORCPT ); Sat, 9 Jan 2021 03:00:25 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F40B223A5A; Sat, 9 Jan 2021 07:59:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610179184; bh=1rb2w5b9UOxpC3OxzW9+QP89tf5CQ3zdJFICcmtU3TU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XU4zvhFa1Zu0ptpTjjqYS8ozs0xKWCB+TFS9gf093u1w81EM6X7h4H2qdOLWWE/Zw r+r9r2d75JEO0osibm5OsbqFmeQmEqBGWvhnpseMYlYerw5gwllg0QNPOS4+Q9hW/f HKKhlNjA4ZPIQzThQLG/YuHu4L1Ebkk/l/dBh/J8etQSQbebHTKWWNsLFs6LtoezSt ho9FJkzPYfpY2XMJztYi1ku80+d+uD5YnAV8uK/wXxExhklYJG3IPrW6K+dYgwv629 NQe9UrO7uxQvrNkuOJCJNS6jL+Kqf0yVhY8L66xdn563eD2OVRY1r8x4DyCAWAVSnR OSKnQsiESAarw== From: Eric Biggers To: linux-fsdevel@vger.kernel.org Cc: linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Theodore Ts'o , Christoph Hellwig , stable@vger.kernel.org, Jan Kara Subject: [PATCH v2 01/12] fs: fix lazytime expiration handling in __writeback_single_inode() Date: Fri, 8 Jan 2021 23:58:52 -0800 Message-Id: <20210109075903.208222-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210109075903.208222-1-ebiggers@kernel.org> References: <20210109075903.208222-1-ebiggers@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org From: Eric Biggers 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/20200306004555.GB225345@gmail.com), 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_DIRTY_TIME will contain i_state 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/20210105005818.92978-1-ebiggers@kernel.org). 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: 0ae45f63d4ef ("vfs: add support for a lazytime mount option") Cc: stable@vger.kernel.org Depends-on: 5afced3bf281 ("writeback: Avoid skipping inode writeback") Suggested-by: Jan Kara Signed-off-by: Eric Biggers --- fs/fs-writeback.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c index acfb55834af23..c41cb887eb7d3 100644 --- a/fs/fs-writeback.c +++ b/fs/fs-writeback.c @@ -1474,21 +1474,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; /* @@ -1509,8 +1513,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); -- 2.30.0