From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=174.142.148.226 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796021541; b=errJea+NGMA1z24laKKPkd1S0WdXIy+ggtga9M6afR7vzRfFw9RQSL3Lg2j29u+Z2IIC donAqPs0zvjxvGV9LqPvCv1Pok9+C4xjRHAbzfFSnXZ/KTO2Ef0VuIUF607VAl7LtTmqV azU99zrUACJvlfgZl6VMilfJcRiK+MLNlYKiVVc5ODjF/0tdHbtK/HOZhJqyjilieXY44 G33zY7fPgKipjSOa30t1XEWQG+AUnXTRTj+dMVI6pYidi+hSEPBy8e/Or+1SLv0LJ4u0v lGLJyhOTxJzRiz2vHv8zoh/nea1dk+KM8Lz/MGNEfcWq5/PxXlhFjR9cHEQ4UuMLHBA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796021541; h=DMARC-Filter:Received:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=dr+DNEijJuIzgM6OP0k0m3P6AgMLoN8cLyBbEW2DE8o=; b=wrconXgW7q6CMwcNGbUg7+2KfZzg8EXdYL7kb78bYiLVxW+nAIYsY9QO69iLnA9ha5hL pOYnw1ePPgR/DfXg/qX1klCS2W6kegeU9KzBDnfz8bTTrDuuV9HF1DmeN8/sUfP7IIDgf C+1W3wOfYmxJ9L5sBzRuKqiV64j4wubi4ewa4d9RuAlxcU6J3+EsBCgHQvzFW+f5AUG5b FxC2mm3cy56fJbSBsnn6DGoEJmRoiBKPERgrZQfDVZmaUUbhEqCCV/+xICRcXPBjqLaOR OSTyGWuottgAGQyW1ZqbLOArm0AUkamZX5UAVk7zta5W/6ez3LcRWsvJO0NHrmM9CRw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=umail.furryterror.org; spf=pass smtp.mailfrom=drax.hungrycats.org; arc=none smtp.remote-ip=174.142.148.226 Received: from drax.kayaks.hungrycats.org (drax.kayaks.hungrycats.org [174.142.148.226]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2B748606A31C; Mon, 30 Nov 2026 06:52:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2B748606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=umail.furryterror.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=drax.hungrycats.org Received: by drax.kayaks.hungrycats.org (Postfix, from userid 1002) id 90B2E1D202C9; Mon, 30 Nov 2026 01:52:13 -0500 (EST) Date: Mon, 30 Nov 2026 01:52:13 -0500 From: Zygo Blaxell To: Ismael Luceno Cc: linux-btrfs@vger.kernel.org, David Sterba , Chris Mason , linux-kernel@vger.kernel.org, Qu Wenruo , Anand Jain , Filipe Manana Subject: Re: [PATCH 0/6] btrfs: trim unused space when relocating data extents Message-ID: References: <20261129133125.3293-1-ismael@iodev.co.uk> X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129133125.3293-1-ismael@iodev.co.uk> On Sun, Nov 29, 2026 at 02:31:13PM +0100, Ismael Luceno wrote: > A data balance copies every data extent whole, including bytes that no > file references. Those dead bytes stay allocated, and an overwritten > file keeps its old extents until every reference to them is gone. > > This series copies only the referenced bytes. For each old extent, the > referenced ranges are collected and laid out in one new allocation, and > each file extent is remapped to its own range. Dead bytes are not copied, > so they are freed with the old block group. Patch 4 walks the running transaction to find new refs, which needs the tree mod log to be reliable, as in the LOGICAL_INO ioctl. Even when it's working as designed, tree mod log is slow, and consumes increasing amounts of memory as long as it exists. A patch in for-next adds a flag to LOGICAL_INO_V2 to allow users to opt out of using the tree mod log. Using only commit roots avoids all the bugs and most of the performance issues associated with using the tree mod log--the missing tree mod log sequence in these patches (as Sashiko pointed out), and two pre-existing upstream that I know of. Relocation already handles new reflinks using only the commit roots. It runs a loop which picks up new refs that were added during each UPDATE_DATA_PTRS pass. Each pass starts with a commit, so all delayed refs in flight are committed, and can then be found in the commit root. This repeats until there are no refs left in the commit root. The transaction handle in patch 4 is still needed, to keep a commit from switching the roots during the walk and the leaf scan, but it should not be passed to the backref walk. relocate_block_group() already does this for add_data_references() in UPDATE_DATA_PTRS. New reflinks can only be created from reachable blocks, so any new reflinks that appear should consist entirely of reachable blocks that were copied in the MOVE_DATA_EXTENTS phase. Patch 3 already verifies that: get_new_location() returns -ENOENT if the range was not copied, and the CoW of the leaf aborts the transaction, before the loss of data can become permanent. In theory, that situation will never happen. In practice, there is a pre-existing bug that should show up when testing reflinks and balance, because the locks held by a reflink don't block the merge of reloc trees. Reflinks are the only race between adding refs and balance I know of. Snapshot metadata page CoW holds more locks, and it and the other cases read the item and add the refs in a single transaction, or run during log tree replay at mount. btrfs_clone() copies a file extent item, and adds the new reference in a transaction started later. If relocation merges the reloc tree in between, the last reference of the extent X is gone before the reflink adds its own: Reflink | Relocation ------------------------------------+-------------------------------------- btrfs_clone() | |- btrfs_search_slot() | |- read_extent_buffer() | | Item points to extent X | |- btrfs_release_path() | | No reference on X, no | | transaction handle | | | relocate_block_group() | | |- merge_reloc_roots() | | | \- merge_reloc_root() | | | |- replace_path() | | | | Source leaf replaced, the | | | | old leaf pointing to X is | | | | now in the reloc tree | | | | | | | \- invalidate_extent_cache() | | | Source range is not | | | locked, doesn't wait | | | | | |- btrfs_commit_current_transaction() | | \- clean_dirty_subvols() | | \- btrfs_drop_snapshot() | | Reloc tree dropped, and the | | last reference of X with it | | \- btrfs_replace_file_extents() | \- btrfs_insert_replace_extent() | \- btrfs_inc_extent_ref() | X no longer exists. | Too late. | The reflink holds the i_mmap_lock of the source inode, but that only stops replace_file_extents(), not replace_path(). Before commit 5d6f0e9890ed ("btrfs: stop locking the source extent range during reflink"), invalidate_extent_cache() waited for the source range. > Patches 2-6 support this change: a helper that preallocates one allocation > for several file extents, NOCOW writeback into relocation extents, a scan > of the running transaction for references, a retry for references that > survive the update stage, and a group length derived from its members. > > Testing > > Measured in QEMU guests, one run per kernel. Base is f2805cb. Series is > this six-patch series. Every run passed btrfs check. Timings are not > reported, because they varied more between runs than between kernels. > > - Holes: a 64 MiB extent with 16 dead 1 MiB holes, then a balance. Data > used 80 MiB -> 64 MiB (base -> series). Extents stay at 33. > - Random overwrites: a 256 MiB file with 600 random 4 KiB overwrites, then > a balance. Data used 258.32 MiB -> 258.32 MiB (base) and 258.32 MiB -> > 256.00 MiB (series). > - Read: a 64 MiB file written as 32 2 MiB chunks with 1 MiB overwritten in > the middle of each, then a direct sequential read. Device read requests > 136 -> 136 (base) and 136 -> 134 (series). Test cases for compression and reflinks would be good too.