From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795676903; b=Akj5GgAvKjlxmJFlGbEbntaZqPjbTnuvN89fTZTz3QY2l2iYt17Tb9WFUzGNf82zKwfg EOW5qebaOq0dBMEYkCOqoPeyk7smn2xm7DsH+TK3EWrKDs8PoimR99AhLdRyWM6WyW/o2 ebyhmOxIVoryQljlnbstIEGstKYKDlSUDepW3pg20VqVgtRqv3FjGyzWZ1dyRfX5nsOXz pNdySm1UE79x4JtRfiYpD7pUIe7e6bkP3/X2k5mG4xtWgQyn5u1EMoNQGR3+9YXUpyck6 3V+c2exM/WpkBv+9bDeKjoIfS0ba/mH2BQ+SVSLBqfIqniQR3dcgTSczqkz8JgfBmvw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795676903; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=97S8jXV0HnQEJfXyqtanW4x108PvFGwuKrpw7epybL8=; b=LUpSAiSS282Li28jr1rAL37hGhOxVveOfmBbT1jBEy1g1IVml8dbivXg2ls9vUYTuxUV cbUCjI/xKanF7/LnkoGejhOl5DnDx5uOU0UlKRXuBLeDV000JxtBIVd/GtrQT9phMZp/0 6WulFjLUIMCx7CMVH+YABwKKLPPRlwrw4DXZ9dNgyCsF+WfHDDu50Q4oPT6f3WudEOPRZ ZqGUxlkTRyO58wMH+UManEDeNLoxRymAVttz40h44A+3gpw/73pOAF+I3aKyCCRdtbUA7 dD822metqj2/QyDS0UCshdgkNiWmlyC5jtDoYeh5+QLlVSOGEoXGqaBciGCo2GHhZQw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Sk42BAhb; arc=none smtp.remote-ip=209.85.128.54 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27F45606A321 for ; Thu, 26 Nov 2026 07:08:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 27F45606A321 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Sk42BAhb Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-4bb6ba3b244so67563405e9.3 for ; Wed, 25 Nov 2026 23:08:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795676900; x=1796281700; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=97S8jXV0HnQEJfXyqtanW4x108PvFGwuKrpw7epybL8=; b=Sk42BAhbX5Ibcfu5TaWmwn9qZ32/fhdXhf/yj4d2AyJzyNKUgUHEXNTl8BVk4zRb9Q ugAKktFkIB+H938OccBMyhMeeKioyJzlRXiszTTo7wZs7x8a2ZSB5wpJie7imvaBWYKh qEbPK5hQV8RXBDRyO/seB+kEuy7b4xhYOU+Cg7Er4NBHDplNsCbzB/aoDMrQIRZYf9ep Q1vkDoimmOykCBs1/KhGsZeWCtM9yapDFH4nPgnYJMsvU8xFb5vWxd/CkyoSPgu77MS1 LxLybqzZjV2I+94FIU04W6HKZdheT9SiLr29NvDBVaskefwFRtQD7L0S8sbyC5Aw0DV4 jgTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795676900; x=1796281700; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=97S8jXV0HnQEJfXyqtanW4x108PvFGwuKrpw7epybL8=; b=va3X9CS9u1R/OyeDLHR0FSupIllTRAE26o5+QXD7khC91fFMoWAtd2b2Z//cycOyOl 1/eJRgmExW/YjDFFj3s78YNoHOQ/F/eAnYWhdiQeV1h+FE0hGwJzyrQ1bj2tOG43t8up jE6be/6tDNqWyCofS8/9B/r11QnOXL1HHXFnofOqX8hhenHmZ/w9CNP4yVv/bGZkH/zm dUPy5omaQrdbzXZfyE9YSMDkcBIqjZ+v2Axh8J1qcOozPSZlxRyC6wRmeUrYjjpJGL0K BTc+Gq8u952wnVQMG8oj1mKTRGWjBVr3icrSJFbaE0BVNONAXYVEbAolvGDqCWym2Ts4 wd3w== X-Forwarded-Encrypted: i=1; AL+hErGB2r+07/c9tyZRWooAyQSYfzagWYhrG1TfI0toJelzZvYJniOqUf8KYQwgXjqpJZEoGAVXAC0=@vger.kernel.org X-Gm-Message-State: APuU4Z9l+oIkt8cJkFUhc2Rs2CrzNwSAd3EXhvftX1+dIocxyS2K2+sg aQhOT9I/EgSK0D4YyPMFATdxeMmOiIQ+z19yTwzNgDMoXt+J3MHwMB5/ X-Gm-Gg: AZFdTZdCPHWzHOejD/qTsnoaW9hDRCedYGwgdNqbNkxT7yn/7cgCd9/vSlhdEIwo5fU Auw3zetnmoREKUo6bVMJZwRDOwJZJqGjn11S6J5naHGEFj8QqmVZpJNarOFQbymjVV0rP3McJN0 Ea5OvlZmJz45w/ljUduD+L8HSf8TdWx1Dyoae7q9JFRf0J2huuvmkPMYnnG9+YmKXUmthpKM+86 0l21agjVibKqPR75+Ez7Jf2bm5STz2Ig0CxoiyB/GZ+FWxHEUb9XWdPJeMavgKCOXFQ8KBbc4OC Qr/IrErUIsdG74MjBWDcRaOz0A+9bFtnR0V5GeykUXLXAg8zX0zWdErDJk7ri6HGEAbxIkc8pf1 G1Me3mUle4lMqIsTxgMTxRu1H8UKbhEOryiJmpP1ZQ7S9IjhCzNqHZojlgDrqiYh1H0CxJnGAXb t2i6KMQJ88OhCANlnFHixk+OsxwI/vy3H3mobDQIbDloDm5xZiiUcQNIx51IIUm30RUN0sLbu80 1XM2k3jDt4f39bmlg2dOnWgTm4mY0qEF4CAqntef/5GMUfxoolwk8j+I/yu7ck8qjqNzZ3h5VdX iEO9BzyfIy4uhQ60oPKGmCx00fVXkmpSKCFPMlOwaLa8wu9awA== X-Received: by 2002:a05:600c:154e:b0:4a3:59ee:86b with SMTP id 5b1f17b1804b1-4bb887c3b63mr430887455e9.4.1795676900395; Wed, 25 Nov 2026 23:08:20 -0800 (PST) Received: from DESKTOP-V28CTA4.dbs.corp ([110.93.227.54]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb8538b09bsm1071077895e9.4.2026.11.25.23.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 23:08:19 -0800 (PST) From: Arbab Haider To: tytso@mit.edu Cc: adilger.kernel@dilger.ca, libaokun@linux.alibaba.com, jack@suse.cz, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, yebin10@huawei.com, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v4 2/5] ext4: fix delayed-allocation inline data conversion races Date: Thu, 26 Nov 2026 12:07:46 +0500 Message-ID: <20261126070748.272109-4-arbabhaider649@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261126070748.272109-2-arbabhaider649@gmail.com> References: <20261126070748.272109-2-arbabhaider649@gmail.com> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Commit ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages") attempted to prevent ext4_convert_inline_data() from racing with delayed-allocation inline data conversion by calling filemap_flush() when EXT4_STATE_MAY_INLINE_DATA is already cleared. However, several races remain in the delayed-allocation inline handoff: 1. filemap_flush() is asynchronous (WB_SYNC_NONE) and can return 0 while folio 0 is locked by a concurrent buffered write and ext4_has_inline_data(inode) is still true. ext4_convert_inline_data() then proceeds to ext4_convert_inline_data_nolock() even though EXT4_STATE_MAY_INLINE_DATA is already cleared (meaning ext4_da_convert_inline_data_to_extent() has already moved the data into dirty folio 0), either copying stale on-disk inline data or rolling back via ext4_restore_inline_data() and re-setting EXT4_STATE_MAY_INLINE_DATA while writeback is pending, triggering: kernel BUG at fs/ext4/inode.c:3028! RIP: 0010:ext4_do_writepages+0x25c0/0x39a0 Call Trace: ext4_writepages+0x285/0x500 do_writepages+0x205/0x520 filemap_writeback+0x1a6/0x250 file_write_and_wait_range+0x8c/0x150 ext4_sync_file+0x2cf/0x950 ext4_buffered_write_iter+0x428/0x5d0 ext4_file_write_iter+0x7cb/0x1450 2. In ext4_da_convert_inline_data_to_extent(), folio_mark_dirty(folio) was called before ext4_clear_inode_state(EXT4_STATE_MAY_INLINE_DATA), and ext4_do_writepages() tested EXT4_STATE_MAY_INLINE_DATA before acquiring xattr_sem. On concurrent or weakly ordered CPUs, writeback can observe PAGECACHE_TAG_DIRTY with EXT4_STATE_MAY_INLINE_DATA still set and hit BUG_ON() in ext4_do_writepages(). 3. When an inode already in delayed allocation (!EXT4_STATE_MAY_INLINE_DATA) is truncated to i_size == 0 in ext4_inline_data_truncate() or drops a failed write at i_size == 0 in ext4_da_truncate_failed_write(), dirty folio 0 and delayed extents are dropped while EXT4_INODE_INLINE_DATA remains set with !EXT4_STATE_MAY_INLINE_DATA and no dirty pages. Also, when truncated to i_size == inline_size, ext4_inline_data_truncate() skipped removing delayed extents beyond i_size because ext4_es_remove_extent() was inside if (i_size < inline_size). Fix the delayed-allocation handoff: - In ext4_da_convert_inline_data_to_extent(), hold xattr_sem for write and clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty, and in writeback validate the handoff and remove inline data under the same xattr_sem write lock. Return an error if the handoff is incomplete or removing inline data fails. - In ext4_convert_inline_data(), serialize the initial and post-writeback state checks under down_read(&xattr_sem) (with a lockless fast path when neither EXT4_INODE_INLINE_DATA nor EXT4_STATE_MAY_INLINE_DATA is set), use synchronous filemap_write_and_wait() when !EXT4_STATE_MAY_INLINE_DATA, return ext4_emergency_state() or -EIO if ext4_has_inline_data(inode) still holds after writeback, and re-check EXT4_STATE_MAY_INLINE_DATA under ext4_write_lock_xattr() with a retry. - In ext4_inline_data_truncate(), finish destroying inline data via ext4_destroy_inline_data_nolock() when !EXT4_STATE_MAY_INLINE_DATA and inode->i_size == 0, and remove delayed extents beyond i_size even when i_size == inline_size. In ext4_da_truncate_failed_write(), call ext4_truncate() when inode->i_size == 0 with EXT4_INODE_INLINE_DATA set and !EXT4_STATE_MAY_INLINE_DATA (falling back to ext4_es_remove_extent() if ext4_truncate() fails). - Update test_convert_inline_data_and_rollback() in fs/ext4/inline-test.c to match the new -EIO behavior when !EXT4_STATE_MAY_INLINE_DATA remains inline after writeback, and also cover normal conversion with EXT4_STATE_MAY_INLINE_DATA set. Fixes: ef09ed5d37b8 ("ext4: fix bug_on in ext4_writepages") Cc: stable@vger.kernel.org Signed-off-by: Arbab Haider --- fs/ext4/inline-test.c | 17 ++++++- fs/ext4/inline.c | 104 ++++++++++++++++++++++++++++++++---------- fs/ext4/inode.c | 18 ++++++-- 3 files changed, 111 insertions(+), 28 deletions(-) diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c index 0198ce831..006c5b3ea 100644 --- a/fs/ext4/inline-test.c +++ b/fs/ext4/inline-test.c @@ -717,6 +717,11 @@ static void test_write_and_destroy_inline_data(struct kunit *test) KUNIT_ASSERT_FALSE(test, IS_ERR(handle)); ret = ext4_get_inode_loc(inode, &iloc); KUNIT_ASSERT_EQ(test, ret, 0); + ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); + ret = ext4_destroy_inline_data(handle, inode); + KUNIT_EXPECT_EQ(test, ret, -EIO); + KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode)); + ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); ret = ext4_destroy_inline_data(handle, inode); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode)); @@ -741,6 +746,7 @@ static void test_write_and_destroy_inline_data(struct kunit *test) handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); KUNIT_ASSERT_FALSE(test, IS_ERR(handle)); + ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); ret = ext4_destroy_inline_data(handle, inode); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_FALSE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); @@ -1139,14 +1145,23 @@ static void test_convert_inline_data_and_rollback(struct kunit *test) /* * 2. Inline inode without EXT4_STATE_MAY_INLINE_DATA -> calls - * filemap_flush() and finishes converting inline data if still inline. + * filemap_write_and_wait() and returns -EIO if still inline instead of + * falling through to ext4_convert_inline_data_nolock(). With + * EXT4_STATE_MAY_INLINE_DATA set, converts inline data to an extent. */ ret = create_inline_file(inode, buf, sizeof(buf)); KUNIT_ASSERT_EQ(test, ret, 0); ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); ret = ext4_convert_inline_data(inode); + KUNIT_EXPECT_EQ(test, ret, -EIO); + KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode)); + + ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); + ret = ext4_convert_inline_data(inode); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode)); + KUNIT_EXPECT_FALSE(test, + ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)); KUNIT_EXPECT_MEMEQ(test, ctx->data_block_buf, buf, sizeof(buf)); /* diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c index acdc65ce4..df50b0483 100644 --- a/fs/ext4/inline.c +++ b/fs/ext4/inline.c @@ -921,7 +921,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, if (IS_ERR(folio)) return PTR_ERR(folio); - down_read(&EXT4_I(inode)->xattr_sem); + down_write(&EXT4_I(inode)->xattr_sem); if (!ext4_has_inline_data(inode)) { ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); goto out; @@ -938,7 +938,7 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, ret = ext4_block_write_begin(NULL, folio, 0, inline_size, ext4_da_get_block_prep); if (ret) { - up_read(&EXT4_I(inode)->xattr_sem); + up_write(&EXT4_I(inode)->xattr_sem); folio_unlock(folio); folio_put(folio); ext4_truncate_failed_write(inode); @@ -946,12 +946,19 @@ static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, } clear_buffer_new(folio_buffers(folio)); + /* + * Clear EXT4_STATE_MAY_INLINE_DATA before marking folio 0 dirty so + * concurrent writeback cannot see PAGECACHE_TAG_DIRTY while + * EXT4_STATE_MAY_INLINE_DATA is still set. Holding xattr_sem for + * write prevents ext4_convert_inline_data() from observing the + * cleared state before folio 0 is dirtied. + */ + ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); folio_mark_dirty(folio); folio_mark_uptodate(folio); - ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); out: - up_read(&EXT4_I(inode)->xattr_sem); + up_write(&EXT4_I(inode)->xattr_sem); if (folio) { folio_unlock(folio); folio_put(folio); @@ -1842,10 +1849,18 @@ bool empty_inline_dir(struct inode *dir, int *has_inline_data) int ext4_destroy_inline_data(handle_t *handle, struct inode *inode) { - int ret, no_expand; + int ret = 0, no_expand; ext4_write_lock_xattr(inode, &no_expand); + if (!ext4_has_inline_data(inode)) + goto out; + /* Only writeback of a completed delayed-allocation handoff may proceed. */ + if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + ret = -EIO; + goto out; + } ret = ext4_destroy_inline_data_nolock(handle, inode); +out: ext4_write_unlock_xattr(inode, &no_expand); return ret; @@ -1914,6 +1929,25 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline) if ((err = ext4_orphan_add(handle, inode)) != 0) goto out; + /* + * If the file was already moved to delayed allocation + * (!EXT4_STATE_MAY_INLINE_DATA) and is now truncated to zero, + * truncate_pagecache() has already dropped dirty folio 0. Finish + * converting the empty inode out of inline data here instead of + * leaving EXT4_INODE_INLINE_DATA set with !EXT4_STATE_MAY_INLINE_DATA + * and no dirty pages. + */ + if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && + inode->i_size == 0) { + down_write(&EXT4_I(inode)->i_data_sem); + EXT4_I(inode)->i_disksize = 0; + ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); + up_write(&EXT4_I(inode)->i_data_sem); + + err = ext4_destroy_inline_data_nolock(handle, inode); + goto out; + } + if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0) goto out; @@ -1922,20 +1956,19 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline) inline_size = ext4_get_inline_size(inode); EXT4_I(inode)->i_disksize = i_size; - if (i_size < inline_size) { - /* - * if there's inline data to truncate and this file was - * converted to extents after that inline data was written, - * the extent status cache must be cleared to avoid leaving - * behind stale delayed allocated extent entries - */ - if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { - ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, i_size); + /* + * If this file was moved to delayed allocation + * (!EXT4_STATE_MAY_INLINE_DATA), remove delayed extents beyond the + * new i_size while keeping block 0 if i_size > 0 (folio 0 is still + * dirty in the page cache). + */ + if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + ext4_lblk_t lblk = EXT4_B_TO_LBLK(inode, inode->i_size); - ext4_es_remove_extent(inode, lblk, - EXT_MAX_BLOCKS - lblk); - } + ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk); + } + if (i_size < inline_size) { /* Clear the content in the xattr space. */ if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) { if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0) @@ -2005,22 +2038,40 @@ int ext4_convert_inline_data(struct inode *inode) handle_t *handle; struct ext4_iloc iloc; +retry: + if (!ext4_has_inline_data(inode) && + !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) + return 0; + + down_read(&EXT4_I(inode)->xattr_sem); if (!ext4_has_inline_data(inode)) { ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); + up_read(&EXT4_I(inode)->xattr_sem); return 0; - } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + } + if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + up_read(&EXT4_I(inode)->xattr_sem); /* * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is * cleared. This means we are in the middle of moving of - * inline data to delay allocated block. Just force writeout - * here to finish conversion. + * inline data to delay allocated block. Force synchronous + * writeout here to finish conversion. */ - error = filemap_flush(inode->i_mapping); + error = filemap_write_and_wait(inode->i_mapping); if (error) return error; - if (!ext4_has_inline_data(inode)) + down_read(&EXT4_I(inode)->xattr_sem); + if (!ext4_has_inline_data(inode)) { + up_read(&EXT4_I(inode)->xattr_sem); return 0; + } + up_read(&EXT4_I(inode)->xattr_sem); + error = ext4_emergency_state(inode->i_sb); + if (error) + return error; + return -EIO; } + up_read(&EXT4_I(inode)->xattr_sem); needed_blocks = ext4_chunk_trans_extent(inode, 1); @@ -2036,8 +2087,15 @@ int ext4_convert_inline_data(struct inode *inode) } ext4_write_lock_xattr(inode, &no_expand); - if (ext4_has_inline_data(inode)) + if (ext4_has_inline_data(inode)) { + if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + ext4_write_unlock_xattr(inode, &no_expand); + ext4_journal_stop(handle); + brelse(iloc.bh); + goto retry; + } error = ext4_convert_inline_data_nolock(handle, inode, &iloc); + } ext4_write_unlock_xattr(inode, &no_expand); ext4_journal_stop(handle); out_free: diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 07a62d856..f27aeae02 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -3019,16 +3019,20 @@ static int ext4_do_writepages(struct mpage_da_data *mpd) * we'd better clear the inline data here. */ if (ext4_has_inline_data(inode)) { + int stop_err; + /* Just inode will be modified... */ handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); if (IS_ERR(handle)) { ret = PTR_ERR(handle); goto out_writepages; } - BUG_ON(ext4_test_inode_state(inode, - EXT4_STATE_MAY_INLINE_DATA)); - ext4_destroy_inline_data(handle, inode); - ext4_journal_stop(handle); + ret = ext4_destroy_inline_data(handle, inode); + stop_err = ext4_journal_stop(handle); + if (!ret) + ret = stop_err; + if (ret) + goto out_writepages; } /* @@ -3437,9 +3441,15 @@ static void ext4_da_truncate_failed_write(struct inode *inode) filemap_invalidate_lock(mapping); truncate_inode_pages(mapping, inode->i_size); + if (inode->i_size == 0 && ext4_has_inline_data(inode) && + !ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { + if (!ext4_truncate(inode)) + goto out_unlock; + } down_write(&EXT4_I(inode)->i_data_sem); ext4_es_remove_extent(inode, lblk, EXT_MAX_BLOCKS - lblk); up_write(&EXT4_I(inode)->i_data_sem); +out_unlock: filemap_invalidate_unlock(mapping); } -- 2.53.0