From patchwork Tue Apr 2 06:23:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13613418 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7359419477 for ; Tue, 2 Apr 2024 06:23:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712039029; cv=none; b=lLPTQEO/QX5ghjAsA+VFLzct3jybND03vTGycJPPKXBv8D8aeaO8fY20F72goLmxQoEGqgRQTgP0zdT6SXpAHbMAgjuAwtiDJgUqNRGfItHzWySjYMsBue0wHAvoDFb5M63wwruO80hzWlX7dCBuc4CpD57o7GUQuSkDhbLhGbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712039029; c=relaxed/simple; bh=nnroCldiv6B1R2ovsTvLXVsO4XvyPnojnIXxqEgPCAQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GISSHiCKW5538qBJBVLJzR/ythI0VqPZOGELDRXFyOJHJKZGAcKkJKIIueF/Qn/QZFMo5ivDKmDV/0s7zkPnDiRsSaSDILKiS+QM9tp7NT/38bF9fRjm8bFJzByqqgQCN+XStSXc3uSS/jfWzNrHNH+Iy0CXIYrjye+0Xx2IQhs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=aJPt6SkM; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="aJPt6SkM" Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 802E93433F for ; Tue, 2 Apr 2024 06:23:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1712039024; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UplTZfVJNmBEC3u0WY6fwUJkUEKxyilCT8SyjbA93is=; b=aJPt6SkMGDTv01YkF55ELFosSJbloq9V8yKD67W/gxfkV4kO38Oa8qQUP0Zmf/ZB7ArWx+ XQx5n299XYJSkBhsxSWRyROXMnp6tlzm1hfqojvUNyeAMMCvR46Kqu+4T92vU9xi8NsAAm HIrF3idGO28LWTegSMy5ZvcM5JcfMKw= Authentication-Results: smtp-out1.suse.de; none Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id A82BB13A90 for ; Tue, 2 Apr 2024 06:23:43 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id GKnrFm+kC2biOQAAn2gu4w (envelope-from ) for ; Tue, 02 Apr 2024 06:23:43 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/2] btrfs: add extra comments on extent_map members Date: Tue, 2 Apr 2024 16:53:19 +1030 Message-ID: <50b37f24a3ac5a2c68529a2373ad98d9c45e6f33.1712038308.git.wqu@suse.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Score: 0.82 X-Spamd-Result: default: False [0.82 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.08)[-0.392]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Flag: NO The extent_map structure is very critical to btrfs, as it is involved for both read and write paths. Unfortunately the structure is not properly explained, making it pretty hard to understand nor to do further improvement. This patch would add extra comments explaining the major numbers base on my code reading. Hopefully we can find more members to cleanup in the future. Signed-off-by: Qu Wenruo --- fs/btrfs/extent_map.h | 62 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent_map.h b/fs/btrfs/extent_map.h index c5a098c99cc6..30322defcd03 100644 --- a/fs/btrfs/extent_map.h +++ b/fs/btrfs/extent_map.h @@ -37,21 +37,81 @@ enum { }; /* + * This extent_map structure is an in-memory representation of file extents, + * it would represent all file extents (including holes, no matter if we have + * hole file extents). + * * Keep this structure as compact as possible, as we can have really large * amounts of allocated extent maps at any time. */ struct extent_map { struct rb_node rb_node; - /* all of these are in bytes */ + /* All of these are in bytes */ + + /* + * File offset of the file extent. matching key.offset of + * (INO EXTENT_DATA FILEPOS) key. + */ u64 start; + + /* + * Length of the file extent. + * For non-inlined file extents it's btrfs_file_extent_item::num_bytes. + * For inlined file extents it's sectorsize. (as there is no reliable + * btrfs_file_extent::num_bytes). + */ u64 len; + + /* + * The modified range start/length, these are in-memory-only + * members for fsync/logtree optimization. + */ u64 mod_start; u64 mod_len; + + /* + * The file offset of the original file extent before splitting. + * + * This is an in-memory-only member, mathcing + * em::start - btrfs_file_extent_item::offset for regular/preallocated + * extents. EXTENT_MAP_HOLE otherwise. + */ u64 orig_start; + + /* + * The full on-disk extent length, matching + * btrfs_file_extent_item::disk_num_bytes. + */ u64 orig_block_len; + + /* + * The decompressed size of the whole on-disk extent, matching + * btrfs_file_extent_item::ram_bytes. + * + * For non-compressed extents, this matches orig_block_len. + */ u64 ram_bytes; + + /* + * The on-disk logical bytenr for the file extent. + * + * For compressed extents it matches btrfs_file_extent_item::disk_bytenr. + * For uncompressed extents it matches + * btrfs_file_extent_item::disk_bytenr + btrfs_file_extent_item::offset + * + * For hole extents it is EXTENT_MAP_HOLE and for inline extents it is + * EXTENT_MAP_INLINE. + */ u64 block_start; + + /* + * The on-disk length for the file extent. + * + * For compressed extents it matches btrfs_file_extent_item::disk_num_bytes. + * For uncompressed extents it matches em::len. + * Otherwise -1 (aka doesn't make much sense). + */ u64 block_len; /* From patchwork Tue Apr 2 06:23:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13613419 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96B5E1B7F4 for ; Tue, 2 Apr 2024 06:23:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712039029; cv=none; b=HtfiI8x+3i6VFAltUYn8/eN6zv/5TXYkiMURJ1ryl0ujU46svFwatVfwCt2Dx3X2y6XCpNWKPFqBW2Zk2nAWUTr4tG7eEC/+Pckak/AY8Mc2BRq/TIqKzTRKiHQYs0fa6ZeYGIx7WXFtozy2imauzk1Xrq9ENaWlsTQIsZdlqyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712039029; c=relaxed/simple; bh=yIti+89+vMuzojZnhWzzRSJzAay6+yTq48+dmkwdCSo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uRqSnj3X28qPrOq3saeruaYKoNP4tXOwRnsD0wAHzavIVaIqCsigpowTSWDCYxwvnhCsTQbGtdmxAJjz85ohzhPGf5+a+kt3kePxI5AG6xxOWs5V6Jq/1qS6rkwfpIoB0sEgFze2EAF5L//6xSi3YiIWo+Xn3U+vBbQJ4wysBc8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=cH0NFuHf; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="cH0NFuHf" Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id DC10220CA6 for ; Tue, 2 Apr 2024 06:23:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1712039025; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kznuLZ73hKmTB9SfL1fcALUBng+SkeAiK5OEWDLW3Vs=; b=cH0NFuHf5RQAA1AGXBQ82WyIAqU1pzMHDFdCuHveK+LZayj7Mwqo0zqIHPOn9AEJLuHl/p i0U62z7IMwCwj1duCCwMdMxdpA20WmBBX+T93SIypu1KO226IoMTX//2mxDouaKN77RStd xck2nqI5FHWDEaGz8yr+uuBJXWWWfTA= Authentication-Results: smtp-out2.suse.de; none Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 04A5C13A90 for ; Tue, 2 Apr 2024 06:23:44 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id uO3wLHCkC2biOQAAn2gu4w (envelope-from ) for ; Tue, 02 Apr 2024 06:23:44 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs: simplify the inline extent map creation Date: Tue, 2 Apr 2024 16:53:20 +1030 Message-ID: X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spamd-Result: default: False [1.20 / 50.00]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; BAYES_HAM(-0.00)[33.17%]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_TWO(0.00)[2]; TO_DN_NONE(0.00)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,imap2.dmz-prg2.suse.org:rdns,imap2.dmz-prg2.suse.org:helo]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: 1.20 X-Spam-Level: * X-Spam-Flag: NO With the tree-checker ensuring all inline file extents starts at file offset 0 and has a length no larger than sectorsize, we can simplify the calculation to assigned those fixes values directly. Signed-off-by: Qu Wenruo --- fs/btrfs/file-item.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index e58fb5347e65..de3ccee38572 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -1265,18 +1265,19 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, struct extent_buffer *leaf = path->nodes[0]; const int slot = path->slots[0]; struct btrfs_key key; - u64 extent_start, extent_end; + u64 extent_start; u64 bytenr; u8 type = btrfs_file_extent_type(leaf, fi); int compress_type = btrfs_file_extent_compression(leaf, fi); btrfs_item_key_to_cpu(leaf, &key, slot); 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) { + u64 extent_end = btrfs_file_extent_end(path); + em->start = extent_start; em->len = extent_end - extent_start; em->orig_start = extent_start - @@ -1299,9 +1300,12 @@ void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode, em->flags |= EXTENT_FLAG_PREALLOC; } } else if (type == BTRFS_FILE_EXTENT_INLINE) { + /* Tree-checker has ensured this. */ + ASSERT(extent_start == 0); + em->block_start = EXTENT_MAP_INLINE; - em->start = extent_start; - em->len = extent_end - extent_start; + em->start = 0; + em->len = fs_info->sectorsize; /* * Initialize orig_start and block_len with the same values * as in inode.c:btrfs_get_extent(). @@ -1336,7 +1340,7 @@ u64 btrfs_file_extent_end(const struct btrfs_path *path) if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { end = btrfs_file_extent_ram_bytes(leaf, fi); - end = ALIGN(key.offset + end, leaf->fs_info->sectorsize); + end = leaf->fs_info->sectorsize; } else { end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); }