From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795676902; b=1sRaIb9HkMdTc6zQhrMXNfxAkzpEQP6XIExuAo3pmJ6CvAWGMaXgurgXIWuvl9oXzqEg Pi+MB5VaFth0SyeVQqKelkGxckrpt9ibeMan24pEn0ejY3uEqyxEYFDqUZbU/Ki3IlkOf ZHWZk4aYDMcwltr/qGFpCL/WvMhmO9+jvce3/tw8JoMP9kQQp/A34gUKtI3u0k8YQ7D+k 38xg/9Q/IjroSZqHBwTxsFYu/NyW/WJQ5RjAM8h/32nCjJxYY1yqZ2YWhrLvr4r+YIZhZ Wc0FhQ5xyPHVWb461LP5YLsvpMHoh+F+1JXetFXsx0qfMdznxtyXJBdOC4uPCwGbGQQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795676902; 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=gb+EV1+6NHIxjkTH0lgx2V3qnG3SsiP/wRGLLIXWLKfJ5FhdIqEjHrbG5tjb6X8Kuhd+ 0EjUWRTEUe7SftMA3BiZozaXYJh0seaICJJEYH2VBLaN8q406D+rkVUl4Ap3LH2JGf/mB YWj6hCtv3tQXnOYw7dsVqxsQZ+US0SX5UvoPie4EznLtcqVTCg9xo9GQpmqYF6fVCQD+I e49BhaaTzOS1LJlOuY4t9hJ5khNQErjm71t++o5PXo+F1gB8CEtvHjsT8V8rQ1CqlAtGW xGPt2p4241kuuEVIs1pJNboZ2c180VxH6NXz+Cw0iBFnKaYE5SIf1XH9Rdr0cB6zjUA== 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.44 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 29C13606A323 for ; Thu, 26 Nov 2026 07:08:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29C13606A323 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-f44.google.com with SMTP id 5b1f17b1804b1-4a207b02a2cso47189595e9.1 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=o9+g5kh3070zHCU4UnHjhmUqvSMLQrVjFFFMnDryNUHsS4IQMQ+HvzD0hXn83ZK2tq b7VJwzEgUN1FSdU78x/SPkaL9fBXdCymqznzqUiT9mVQ7pseur2yzqneEsy6cFKZ88z5 BsLIB8HlXeyOdAWFG7/s0/HRu4UgHgJ6cOgxYLt2k8q/KJHn93uPc3V+lJAYg+10gdE6 1tjzCl5QXpJVtQn5YiY6qFTrmxl6EmDiMeXTzQ3a9hol7ca4EjIG4q9SyrLyUeRCzHFT zB1DUKHNuha7Kk+2tjOKemiVa4AapuyVgPkCWw1H4G7d6avlt9Murczti6478e8P9rsa 8SUw== X-Forwarded-Encrypted: i=1; AL+hErHDbBLIm8+lXDh9gIbDyLV3ndPn9yBA9eupUrdAEWfpyk8nksCg9S54SqwXK4FkDYS1H3JKmQ2v2kSePP8=@vger.kernel.org X-Gm-Message-State: APuU4Z/cnEVLUPPSaiZG74LkVfii1xTMDOvyR9JwIbhrM0jzva51KuDU sFWJkR0LM8901o9l05E61Gwpipq7hFUQQVNoqbLmGrRVxeoEoiB3mqEM X-Gm-Gg: AZFdTZdv9xKqdKuqO+EV97ApLY/XSOhwF9mzEJTWpHstz4F7Oj8jbjUpjy+RazfV7DF eVMgr4WlpHGVBpxUhnLMuG/MjZm3wIHa6Q3vdVpKfbBlvPb4R6kFeQVZ42CLpP2DM0neNTVj82i VZCvsizxX4wtLYgJM08Q8YaAWLSIESNKnsOxVYi6FvrLTWFhYMT4cnPBO3m7u1OiOg7lIaC9ltX acpFNzZMPYxie/uFQqd/5ynFMS/48AzkXxm89+MgUbUtznIWiZA5/c/VPDupt33D9tOPZlmpUcH VduuMrPISrW/mDxvV60CR9AlLmShpgJFtWIxOhT6A7TD7Sis3hWuqA6jEA9p9+y6drPoc83uzyu l44/9L+BQZZuAnvhFi64sacSkWN7aGIqdKVoUleyUGKh7x+oh1DwfXhwdVYomdBF/D21/3rTie8 gRa7wRIOr0zGxPpkRDL1Kv2o7sIb5HFKJywM0ZCz73gWcMSJf5yjwr6WVjrwkOcavkEPh98Aptr X0fMYqgEgO1jBty3EvpPQ5PiACE3YxJ7+9RlWPJ2PWj2xD21Cd5rOef7+UqSqclp6/JgRUYYiHh tKlGwvUR8IPqxGU6snkTNIsy5cTaIr6jtPYH40q+987Rc4nWJw== 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: linux-kernel@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