Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D19A34BA34 for ; Mon, 13 Oct 2025 20:46:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760388415; cv=none; b=H6qeVVtxq38E4SwkjmLunNNHPgJekvlTCdPFcbzS0x4pbfpENMOxeqBA88w8hmyQzZuSuxMyNN3wUfqtvbvBcBPlhyPUplh7dlwFNIH2SfH2BvKc3qHJvsVyKVemRxVWOsnPPKgTw8fV9LZTg9/9Nrjar+og8JsDS5BWpeZU5Fo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760388415; c=relaxed/simple; bh=S88bpwWo2lsZU7waDnJmHznFfQRYpLE03oM/+DmqMCE=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=thmJgJYHQNzH1k1us+HRv1Fsr2ZZhBCy9Uuts79EY1Bjj9gm0BeFsDCWUm5liEeO2M+ToVvu0lL8IjCO8jiD6LA/Kcg4RDH+li20wng16QXaoGvxeoFCiuDQooSCzd7hahgJGnE4bcZvejgppEpp/cuxu/LuZHF51zG4TJ61utQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com; spf=pass smtp.mailfrom=gmx.com; dkim=pass (2048-bit key) header.d=gmx.com header.i=quwenruo.btrfs@gmx.com header.b=Hwj+7b2L; arc=none smtp.client-ip=212.227.17.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.com header.i=quwenruo.btrfs@gmx.com header.b="Hwj+7b2L" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.com; s=s31663417; t=1760388410; x=1760993210; i=quwenruo.btrfs@gmx.com; bh=UaPlfPELfnPGMgD5VIBq3AYrKF97aQYWPt/lz8A0FJU=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=Hwj+7b2LwvNqXkf+EyzISe5QsIIwmdd4XuBUrtMGSlN3D1IEv+W8V+iA/OdxTVGR zQ9fSDK2xbdMt9lmCHt/U21vfjQEPWIIVu9RVuCGqAAX28JFGrWdYk91zzpk1g0pI fw0YvX2jN2fez5T0cMjCbSLvahaRdGc8ESposD9TGy/oDi6REj9H1wBy5MrSpwePy ORT/VGFqextnkxo+yHVL8Wa4OxT2G/1ksMWLEGkX1uqEQ8RUP3BlcoNNZxKgcNRpk hzxMv+rs1i3MTeTZ9JsK7dFh58c+4nYJYTecHc2lGA8yc21EhVk7k3EBy1kM75URJ yodj5IuMVz59LVmUgw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [172.16.0.229] ([159.196.52.54]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1Mk0JM-1uOKAr34e0-00fZdx; Mon, 13 Oct 2025 22:46:50 +0200 Message-ID: <1028b98a-2bab-4ad0-bdcd-c77c87b29571@gmx.com> Date: Tue, 14 Oct 2025 07:16:46 +1030 Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/7] btrfs: truncate ordered extent when skipping writeback past i_size To: fdmanana@kernel.org, linux-btrfs@vger.kernel.org References: Content-Language: en-US From: Qu Wenruo Autocrypt: addr=quwenruo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ik6DmeTG3WJO2BQRNvXA0pXrJH1fN GSsRb+pKEKHKtL1803x71zQxCwLh+zLP1iXHVM5j8gX9zqupigQR/Cel2XPS44zWcDW8r7B0 q1eW4Jrv0x19p4P923voqn+joIAostyNTUjCeSrUdKth9jcdlam9X2DziA/DHDFfS5eq4fEv ABEBAAHCwHwEGAEIACYCGwwWIQQt33LlpaVbqJ2qQuHCPZHzoSX+qAUCZxF1gQUJEP5a0gAK CRDCPZHzoSX+qHGpB/kB8A7M7KGL5qzat+jBRoLwB0Y3Zax0QWuANVdZM3eJDlKJKJ4HKzjo B2Pcn4JXL2apSan2uJftaMbNQbwotvabLXkE7cPpnppnBq7iovmBw++/d8zQjLQLWInQ5kNq Vmi36kmq8o5c0f97QVjMryHlmSlEZ2Wwc1kURAe4lsRG2dNeAd4CAqmTw0cMIrR6R/Dpt3ma +8oGXJOmwWuDFKNV4G2XLKcghqrtcRf2zAGNogg3KulCykHHripG3kPKsb7fYVcSQtlt5R6v HZStaZBzw4PcDiaAF3pPDBd+0fIKS6BlpeNRSFG94RYrt84Qw77JWDOAZsyNfEIEE0J6LSR/ In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:5D6Muqi5UPQPcWiuXn3LxKctpgvKc/JEocjsfvIfo587o6v8I8I w7CINN6xVIz3/dfYmJrrnMyJ4CSTvYEbT5RYspWl0H/Zc45HcLGYMOIt6NQ8iRgdTAwRfgH +VhhA1pGP5SkwD+liaQ60xpSJ18Xb8mdOAKPgNQXHV0mnKt+M6zNXoQMI8QeE1igZWqivf2 twEKL1KTjOwzGxq+zHQlw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:eWfsEdVF9uQ=;E+84QVB8CIq+uYP1SnWYHohYLpB jj8Xjz1vawV6wbuGVa9ddzLoIbY6cX0UsX75d+PvXk1jH5Opdra1JPZ7i0LbbIJ8VcQP9UHcT fMPEMPANqBCoZ/ITD5H+rXBYUzD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jjXv50yIUl6kDavYihfzd04IT7X4rBxqWFtfi2alUX9iMj0bFOJd34BPo1Jfra yotJ145PkbpJskG3sGEiMHtQTANXv5IU54dPjWkjMJiGOon6Z+rfrIDAcjp+usR6tvWIgDxGE MKw5Zx+Ja4mAdsqV9RE7ieots2fJVGh2+FqbUe7o3VZPXMJj6Kl9XBLCWKlHCLuLDRGCgVfjC f4q8XI1S4HXJ0Zhna8x7ls5ss1ag7sXDyFDWyiS+jXShPPAEpm/+PqgByqT0BVN6MlOx/5IMW t/z/kvpB6648Sd8PQPkjImiTxoGXtaOG7Vvq/ibhjLfZBNFL2FzZqlacs08Ctn5/gisHXpmal RyuqHB8cBKz+vHnCn3S42qsxVrJMESriT4TOkQvagAQUN7lchn4JWTIEc7jyH9+EOWHwSI4uD rZxWMhQhrK1IuGupuvCL0csQLAv2OvIM8BG0j/VZQfgqUTqTme32up8YcQ2OxHohW5wSURNQf rzgvNHP8/nkRauoaG6eQATur7NbvNeo8mwIz9/tKTcgYs3oMTkpW9U3btn4f3+Rs2ESVue0em LR1nPEJy/yOH68w6A413xcxiTPSuxrvZurLsCJLNInr8NWVx/7lP6fZNDBDG515grh1z3z9yk V1okvaP6OSLOo8LtaO7dpYGQrnkrbqWxaDXRuZhm4uM+zEDL4Qb5XBCku3c+u8x+ZVZ9DJ2G9 kebOQ== =E5=9C=A8 2025/10/13 22:35, fdmanana@kernel.org =E5=86=99=E9=81=93: > From: Filipe Manana >=20 > While running test case btrfs/192 from fstests with support for large > folios (needs CONFIG_BTRFS_EXPERIMENTAL=3Dy) I ended up getting very spo= radic > btrfs check failures reporting that csum items were missing. Looking int= o > the issue it turned out that btrfs check searches for csum items of a fi= le > extent item with a range that spans beyond the i_size of a file and we > don't have any, because the kernel's writeback code skips submitting bio= s > for ranges beyond eof. It's not expected however to find a file extent i= tem > that crosses the rounded up (by the sector size) i_size value, but there= is > a short time window where we can end up with a transaction commit leavin= g > this small inconsistency between the i_size and the last file extent ite= m. >=20 > Example btrfs check output when this happens: >=20 > $ btrfs check /dev/sdc > Opening filesystem to check... > Checking filesystem on /dev/sdc > UUID: 69642c61-5efb-4367-aa31-cdfd4067f713 > [1/8] checking log skipped (none written) > [2/8] checking root items > [3/8] checking extents > [4/8] checking free space tree > [5/8] checking fs roots > root 5 inode 332 errors 1000, some csum missing > ERROR: errors found in fs roots > (...) >=20 > Looking at a tree dump of the fs tree (root 5) for inode 332 we have: >=20 > $ btrfs inspect-internal dump-tree -t 5 /dev/sdc > (...) > item 28 key (332 INODE_ITEM 0) itemoff 2006 itemsize 160 > generation 17 transid 19 size 610969 nbytes 86016 > block group 0 mode 100666 links 1 uid 0 gid 0 rdev 0 > sequence 11 flags 0x0(none) > atime 1759851068.391327881 (2025-10-07 16:31:08) > ctime 1759851068.410098267 (2025-10-07 16:31:08) > mtime 1759851068.410098267 (2025-10-07 16:31:08) > otime 1759851068.391327881 (2025-10-07 16:31:08) > item 29 key (332 INODE_REF 340) itemoff 1993 itemsize 13 > index 2 namelen 3 name: f1f > item 30 key (332 EXTENT_DATA 589824) itemoff 1940 itemsize 53 > generation 19 type 1 (regular) > extent data disk byte 21745664 nr 65536 > extent data offset 0 nr 65536 ram 65536 > extent compression 0 (none) > (...) >=20 > We can see that the file extent item for file offset 589824 has a length= of > 64K and its number of bytes is 64K. Looking at the inode item we see tha= t > its i_size is 610969 bytes which falls within the range of that file ext= ent > item [589824, 655360[. >=20 > Looking into the csum tree: >=20 > $ btrfs inspect-internal dump-tree /dev/sdc > (...) > item 15 key (EXTENT_CSUM EXTENT_CSUM 21565440) itemoff 991 item= size 200 > range start 21565440 end 21770240 length 204800 > item 16 key (EXTENT_CSUM EXTENT_CSUM 1104576512) itemoff 983= itemsize 8 > range start 1104576512 end 1104584704 length 8192 > (..) >=20 > We see that the csum item number 15 covers the first 24K of the file ext= ent > item - it ends at offset 21770240 and the extent's disk_bytenr is 217456= 64, > so we have: >=20 > 21770240 - 21745664 =3D 24K >=20 > We see that the next csum item (number 16) is completely outside the ran= ge, > so the remaining 40K of the extent doesn't have csum items in the tree. >=20 > If we round up the i_size to the sector size, we get: >=20 > round_up(610969, 4096) =3D 614400 >=20 > If we subtract from that the file offset for the extent item we get: >=20 > 614400 - 589824 =3D 24K >=20 > So the missing 40K corresponds to the end of the file extent item's rang= e > minus the rounded up i_size: >=20 > 655360 - 614400 =3D 40K >=20 > Normally we don't expect a file extent item to span over the rounded up > i_size of an inode, since when truncating, doing hole punching and other > operations that trim a file extent item, the number of bytes is adjusted= . >=20 > There is however a short time window where the kernel can end up, > temporarily,persisting an inode with an i_size that falls in the middle = of > the last file extent item and the file extent item was not yet trimmed (= its > number of bytes reduced so that it doesn't cross i_size rounded up by th= e > sector size). >=20 > The steps (in the kernel) that lead to such scenario are the following: >=20 > 1) We have inode I as an empty file, no allocated extents, i_size is 0= ; >=20 > 2) A buffered write is done for file range [589824, 655360[ (length of > 64K) and the i_size is updated to 655360. Note that we got a single > large folio for the range (64K); >=20 > 3) A truncate operation starts that reduces the inode's i_size down to > 610969 bytes. The truncate sets the inode's new i_size at > btrfs_setsize() by calling truncate_setsize() and before calling > btrfs_truncate(); >=20 > 4) At btrfs_truncate() we trigger writeback for the range starting at > 610304 (which is the new i_size rounded down to the sector size) an= d > ending at (u64)-1; >=20 > 5) During the writeback, at extent_write_cache_pages(), we get from th= e > call to filemap_get_folios_tag(), the 64K folio that starts at file > offset 589824 since it contains the start offset of the writeback > range (610304); >=20 > 6) At writepage_delalloc() we find the whole range of the folio is dir= ty > and therefore we run delalloc for that 64K range ([589824, 655360[)= , > reserving a 64K extent, creating an ordered extent, etc; >=20 > 7) At extent_writepage_io() we submit IO only for subrange [589824, 61= 4400[ > because the inode's i_size is 610969 bytes (rounded up by sector si= ze > is 614400). There, in the while loop we intentionally skip IO beyon= d > i_size to avoid any unnecessay work and just call > btrfs_mark_ordered_io_finished() for the range [614400, 655360[ (wh= ich > has a 40K length); >=20 > 8) Once the IO finishes we finish the ordered extent by ending up at > btrfs_finish_one_ordered(), join transaction N, insert a file exten= t > item in the inode's subvolume tree for file offset 589824 with a nu= mber > of bytes of 64K, and update the inode's delayed inode item or direc= tly > the inode item with a call to btrfs_update_inode_fallback(), which > results in storing the new i_size of 610969 bytes; >=20 > 9) Transaction N is committed either by the transaction kthread or som= e > other task committed it (in response to a sync or fsync for example= ). >=20 > At this point we have inode I persisted with an i_size of 610969 by= tes > and file extent item that starts at file offset 589824 and has a nu= mber > of bytes of 64K, ending at an offset of 655360 which is beyond the > i_size rounded up to the sector size (614400). >=20 > --> So after a crash or power failure here, the btrfs check program > reports that error about missing checksum items for this inode,= as > it tries to lookup for checksums covering the whole range of the > extent; >=20 > 10) Only after transaction N is committed that at btrfs_truncate() the > call to btrfs_start_transaction() starts a new transaction, N + 1, > instead of joining transaction N. And it's with transaction N + 1 t= hat > it calls btrfs_truncate_inode_items() which updates the file extent > item at file offset 589824 to reduce its number of bytes from 64K d= own > to 24K, so that the file extent item's range ends at the i_size > rounded up to the sector size (614400 bytes). >=20 > Fix this by truncating the ordered extent at extent_writepage_io() when = we > skip writeback because the current offset in the folio is beyond i_size. > This ensures we don't ever persist a file extent item with a number of > bytes beyond the rounded up (by sector size) value of the i_size. Reviewed-by: Qu Wenruo Thanks, Qu >=20 > Signed-off-by: Filipe Manana [...] > @@ -1724,6 +1724,23 @@ static noinline_for_stack int extent_writepage_io= (struct btrfs_inode *inode, > cur =3D folio_pos(folio) + (bit << fs_info->sectorsize_bits); > =20 > if (cur >=3D i_size) { > + struct btrfs_ordered_extent *ordered; > + unsigned long flags; > + > + ordered =3D btrfs_lookup_first_ordered_range(inode, cur, > + folio_end - cur); > + /* > + * We have just run delalloc before getting here, so > + * there must be an ordered extent. > + */ > + ASSERT(ordered !=3D NULL); > + spin_lock_irqsave(&inode->ordered_tree_lock, flags); > + set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags); > + ordered->truncated_len =3D min(ordered->truncated_len, > + cur - ordered->file_offset); > + spin_unlock_irqrestore(&inode->ordered_tree_lock, flags); > + btrfs_put_ordered_extent(ordered); > + > btrfs_mark_ordered_io_finished(inode, folio, cur, > start + len - cur, true); > /* > diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c > index 2829f20d7bb5..8a8aa6ed405b 100644 > --- a/fs/btrfs/ordered-data.c > +++ b/fs/btrfs/ordered-data.c > @@ -1098,8 +1098,9 @@ struct btrfs_ordered_extent *btrfs_lookup_first_or= dered_range( > struct rb_node *prev; > struct rb_node *next; > struct btrfs_ordered_extent *entry =3D NULL; > + unsigned long flags; > =20 > - spin_lock_irq(&inode->ordered_tree_lock); > + spin_lock_irqsave(&inode->ordered_tree_lock, flags); > node =3D inode->ordered_tree.rb_node; > /* > * Here we don't want to use tree_search() which will use tree->last > @@ -1154,7 +1155,7 @@ struct btrfs_ordered_extent *btrfs_lookup_first_or= dered_range( > trace_btrfs_ordered_extent_lookup_first_range(inode, entry); > } > =20 > - spin_unlock_irq(&inode->ordered_tree_lock); > + spin_unlock_irqrestore(&inode->ordered_tree_lock, flags); > return entry; > } > =20