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 AA12FC433EF for ; Tue, 8 Feb 2022 18:46:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1385280AbiBHSqW (ORCPT ); Tue, 8 Feb 2022 13:46:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1385214AbiBHSqV (ORCPT ); Tue, 8 Feb 2022 13:46:21 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34DE2C06174F for ; Tue, 8 Feb 2022 10:46:21 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id E4F291F387; Tue, 8 Feb 2022 18:46:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1644345979; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/oRJj9Pf+QFwOBRiArUtiDJ2iO5R/e/rkOLgpmXHZPs=; b=WzzSzahK37xhy6VRljKQd7Y3g7w09MYee7oeEeo35OjFXRLhDkplJz4xVi74rJi+55vc6k lqKsY4seuuJkRgL+yRl7BeyhuuGJGrutoxFMdvfFn+o5bPnQyj8cELg8yGre8YFl/fS5sj lfh2JJJxasaKMPU88kBInuk28NfyjPw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1644345979; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/oRJj9Pf+QFwOBRiArUtiDJ2iO5R/e/rkOLgpmXHZPs=; b=Xu7A+IQFd0XgJgY39cEK11275ePEH/wRCMONZg3Z5a3vPxIbzn0Gp78yBwJNHE3rqh+Wvl 8uKb37sDrPqLLcAg== Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id DE78CA3B84; Tue, 8 Feb 2022 18:46:19 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 7AD46DAB3F; Tue, 8 Feb 2022 19:42:39 +0100 (CET) Date: Tue, 8 Feb 2022 19:42:39 +0100 From: David Sterba To: Qu Wenruo Cc: linux-btrfs@vger.kernel.org Subject: Re: [PATCH v2] btrfs: populate extent_map::generation when reading from disk Message-ID: <20220208184239.GE12643@twin.jikos.cz> Reply-To: dsterba@suse.cz Mail-Followup-To: dsterba@suse.cz, Qu Wenruo , linux-btrfs@vger.kernel.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) 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. > > This unpopulated extent_map::geneartion can prevent the following autodefrag > case from working: > > 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 > --- > 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. With the typos fixed, added to misc-next, thanks.