From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03802C433F5 for ; Tue, 8 Feb 2022 16:39:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244419AbiBHQjv (ORCPT ); Tue, 8 Feb 2022 11:39:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232725AbiBHQju (ORCPT ); Tue, 8 Feb 2022 11:39:50 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B9D1C061576 for ; Tue, 8 Feb 2022 08:39:49 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1DF3C616C5 for ; Tue, 8 Feb 2022 16:39:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4D1DC004E1; Tue, 8 Feb 2022 16:39:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1644338388; bh=yn77t9Z9Vz1B8wv4KJXJDZhkV91IBWUIyBQlH+zUuBs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WSM9wZy4Nf1tltf7YVvbp1PhSV9/rHPEK+sB8nYR3k8xMRYksc+j9Oap1dPYtJoUR kf2kyM5Y97P15z9EtIdxFn0qC2qjmTKVm1opBseE1axtdCPKvQNZZqiLslFIVouYPh vPPqiMk2iAR6Gz9nd+vbzDarmhBzsEHIIRxA4s7+cpuxwRqvl5o2zPwncZyt2M8JZs XuZB6WPWULZHyrSGosl2ONH5CFWZXRuTdEhtnnzji5W2dKg2UeXLHSBsbv3PfuBUum AdRWr+HqALUbxNXih9C1DbTgbObOMXaJ4huOYhFPpALM2PQy4rEHF9q4tT5HeibhDb zj1gvgSv7IzvA== Date: Tue, 8 Feb 2022 16:39:45 +0000 From: Filipe Manana To: Qu Wenruo Cc: linux-btrfs@vger.kernel.org Subject: Re: [PATCH v2] btrfs: populate extent_map::generation when reading from disk Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On Tue, Feb 08, 2022 at 01:31:19PM +0800, Qu Wenruo wrote: > [WEIRD BEHAVIOR] > > When btrfs_get_extent() tries to get some file extent from disk, it > never populates extent_map::generation , leaving the value to be 0. > > On the other hand, for extent map generated by IO, it will get its > generation properly set at finish_ordered_io() > > finish_ordered_io() > |- unpin_extent_cache(gen = trans->transid) > |- em->generation = gen; > > [CAUSE] > Since extent_map::generation is mostly used by fsync code, and for fsync > they only care about modified extents, which all have their em::generation > 0. > > Thus it's fine to not populate em read from disk for fsync. > > [CORNER CASE] > However autodefrag also relies on em::geneartion to determine if one extent > needs to be defragged. em::geneartion -> em::generation > > This unpopulated extent_map::geneartion can prevent the following autodefrag > case from working: Same here. > > mkfs.btrfs -f $dev > mount $dev $mnt -o autodefrag > > # initial write to queue the inode for autodefrag > xfs_io -f -c "pwrite 0 4k" $mnt/file > sync > > # Real fragmented write > xfs_io -f -s -c "pwrite -b 4096 0 32k" $mnt/file > sync > echo "=== before autodefrag ===" > xfs_io -c "fiemap -v" $mnt/file > > # Drop cache to force em to be read from disk > echo 3 > /proc/sys/vm/drop_caches > mount -o remount,commit=1 $mnt > sleep 3 > sync > > echo "=== After autodefrag ===" > xfs_io -c "fiemap -v" $mnt/file > umount $mnt > > The result looks like this: > > === before autodefrag === > /mnt/btrfs/file: > EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS > 0: [0..15]: 26672..26687 16 0x0 > 1: [16..31]: 26656..26671 16 0x0 > 2: [32..47]: 26640..26655 16 0x0 > 3: [48..63]: 26624..26639 16 0x1 > === After autodefrag === > /mnt/btrfs/file: > EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS > 0: [0..15]: 26672..26687 16 0x0 > 1: [16..31]: 26656..26671 16 0x0 > 2: [32..47]: 26640..26655 16 0x0 > 3: [48..63]: 26624..26639 16 0x1 > > This fragmented 32K will not be defragged by autodefrag. > > [FIX] > To make things less weird, just populate extent_map::generation when > reading file extents from disk. > > This would make above fragmented extents to be properly defragged: > > == before autodefrag === > /mnt/btrfs/file: > EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS > 0: [0..15]: 26672..26687 16 0x0 > 1: [16..31]: 26656..26671 16 0x0 > 2: [32..47]: 26640..26655 16 0x0 > 3: [48..63]: 26624..26639 16 0x1 > === After autodefrag === > /mnt/btrfs/file: > EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS > 0: [0..63]: 26688..26751 64 0x1 > > Signed-off-by: Qu Wenruo I don't want to make you send yet another version because only of a typo in the changelog, so: Reviewed-by: Filipe Manana Thanks. > --- > Changelog: > v2: > - Update the commit message to include a reproducer > Although this is not what we want (to reduce autodefrag IO), > the behavior still worthy fixing anyway. > --- > fs/btrfs/file-item.c | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c > index 90c5c38836ab..9a3de652ada8 100644 > --- a/fs/btrfs/file-item.c > +++ b/fs/btrfs/file-item.c > @@ -1211,6 +1211,7 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, > extent_start = key.offset; > extent_end = btrfs_file_extent_end(path); > em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); > + em->generation = btrfs_file_extent_generation(leaf, fi); > if (type == BTRFS_FILE_EXTENT_REG || > type == BTRFS_FILE_EXTENT_PREALLOC) { > em->start = extent_start; > -- > 2.35.0 >