From patchwork Tue Sep 3 07:28:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13788167 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 5DC761D6DBA for ; Tue, 3 Sep 2024 07:29:28 +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=1725348570; cv=none; b=GZcg6TZVib9eaYdnb8zF+s4feQj3QKTciVrbVEhAX2O1V+0M9GhU6ZF5FNoKpHdFRjWCTQK6++79Cnm7/7n2teqNPnbWUlRwHkfpF5HVrt++T0ZtJmi+zsMULiyGCa4Z3/6jBLbEtO1SPlrUBe70GEE0Eu9IMQcEnLxug1aIEiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725348570; c=relaxed/simple; bh=0g4lMkQBFrv4bXsndjTgh5Bo5C4a1pwOcPhhkH5ovvU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=H/o/Biku4jLujpqUTW94FpzoxMg2+hcEWCvppQVhkwUaYXVtj3UuLBAYHDqJRJusyhnZfCyRSg7BrZGpVPX+56bhT96GhVwbEILdIQH1iDKWcnRjDTBhb4wte3zDMbS7mCG4ySIoTyqXzzeBTaswHjIY8EQ+K+lzM3H9RW7IUs0= 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=WGr5FNji; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=WGr5FNji; 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="WGr5FNji"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="WGr5FNji" Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (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 76EBE21A79 for ; Tue, 3 Sep 2024 07:29:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348566; 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=D3Yxigyi1LYdEAywnUWC/5LsywGBwXbC7kGFyVkwqpI=; b=WGr5FNjim1tq4v7Z6EwdiIel6gmvJQ2xioBaRttWB+dMJv7RHwUih4punClqtiI6zNMsuN kfb79D+O3I3eqj0a0mLUkL+Wq4WVzn5PAt6ZntFFkSMNlJyD2ub1A+I78MEctAmXB5epAD K1wkVgMBOQB0D2ydunvzVURnagjAGZs= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=WGr5FNji DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348566; 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=D3Yxigyi1LYdEAywnUWC/5LsywGBwXbC7kGFyVkwqpI=; b=WGr5FNjim1tq4v7Z6EwdiIel6gmvJQ2xioBaRttWB+dMJv7RHwUih4punClqtiI6zNMsuN kfb79D+O3I3eqj0a0mLUkL+Wq4WVzn5PAt6ZntFFkSMNlJyD2ub1A+I78MEctAmXB5epAD K1wkVgMBOQB0D2ydunvzVURnagjAGZs= Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id B510F13A52 for ; Tue, 3 Sep 2024 07:29:25 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id mGTCHdW61mY2TwAAD6G6ig (envelope-from ) for ; Tue, 03 Sep 2024 07:29:25 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/4] btrfs-progs: convert: fix inline extent size for symbol link Date: Tue, 3 Sep 2024 16:58:59 +0930 Message-ID: X-Mailer: git-send-email 2.46.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-Rspamd-Queue-Id: 76EBE21A79 X-Spam-Score: -3.01 X-Rspamd-Action: no action X-Spamd-Result: default: False [-3.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_TRACE(0.00)[suse.com:+]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; TO_DN_NONE(0.00)[]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_VIA_SMTP_AUTH(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:mid,suse.com:email] X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Level: [BUG] Sometimes test case btrfs/012 fails randomly, with the failure to read a softlink: QA output created by 012 Checking converted btrfs against the original one: -OK +readlink: Structure needs cleaning Checking saved ext2 image against the original one: OK Furthermore, this will trigger a kernel error message: BTRFS critical (device dm-2): regular/prealloc extent found for non-regular inode 133081 [CAUSE] For that specific inode 133081, the tree dump looks like this: item 127 key (133081 INODE_ITEM 0) itemoff 40984 itemsize 160 generation 1 transid 1 size 4095 nbytes 4096 block group 0 mode 120777 links 1 uid 0 gid 0 rdev 0 sequence 0 flags 0x0(none) item 128 key (133081 INODE_REF 133080) itemoff 40972 itemsize 12 index 2 namelen 2 name: l3 item 129 key (133081 EXTENT_DATA 0) itemoff 40919 itemsize 53 generation 4 type 1 (regular) extent data disk byte 2147483648 nr 38080512 extent data offset 37974016 nr 4096 ram 38080512 extent compression 0 (none) Note that, the soft link inode size is 4095, just one by smaller than the sector size, but its nbytes is 4096, larger than the upper limit of inline extent. Thus it results the creation of a regular extent, but for btrfs we do not accept a soft link with a regular/preallocated extent, thus kernel rejects such read and failed the readlink call. The root cause is in the convert code, where for soft links we always create a data extent with its size + 1, causing the above problem. I guess the original code is to handle the terminating NUL, but in btrfs we never need to bother terminating NUL for inline extents. Thus this pitfall in btrfs-convert leads to the above invalid data extent and fail the test case. [FIX] Fix the corner case by removing the terminating NUL when inserting an inline extent. Furthermore, to prevent such problem from happening again, do extra UASSERT() for btrfs_insert_inline_extent() to detect such problem. Signed-off-by: Qu Wenruo --- convert/source-ext2.c | 12 +++++++++--- convert/source-reiserfs.c | 4 ++-- kernel-shared/file-item.c | 3 +++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/convert/source-ext2.c b/convert/source-ext2.c index acba5db7ee81..dfad64a6d226 100644 --- a/convert/source-ext2.c +++ b/convert/source-ext2.c @@ -476,8 +476,14 @@ static int ext2_create_symlink(struct btrfs_trans_handle *trans, int ret; char *pathname; u64 inode_size = btrfs_stack_inode_size(btrfs_inode); + if (ext2fs_inode_data_blocks2(ext2_fs, ext2_inode)) { - btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1); + if (inode_size >= trans->fs_info->sectorsize) { + error("inode size too large for symbol link, has %llu expect [1, %u]", + inode_size, trans->fs_info->sectorsize - 1); + return -EUCLEAN; + } + btrfs_set_stack_inode_size(btrfs_inode, inode_size); ret = ext2_create_file_extents(trans, root, objectid, btrfs_inode, ext2_fs, ext2_ino, CONVERT_FLAG_DATACSUM | @@ -489,8 +495,8 @@ static int ext2_create_symlink(struct btrfs_trans_handle *trans, pathname = (char *)&(ext2_inode->i_block[0]); BUG_ON(pathname[inode_size] != 0); ret = btrfs_insert_inline_extent(trans, root, objectid, 0, - pathname, inode_size + 1); - btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1); + pathname, inode_size); + btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size); return ret; } diff --git a/convert/source-reiserfs.c b/convert/source-reiserfs.c index 3edc72ed08a5..e2f07bfda188 100644 --- a/convert/source-reiserfs.c +++ b/convert/source-reiserfs.c @@ -538,8 +538,8 @@ static int reiserfs_copy_symlink(struct btrfs_trans_handle *trans, len = get_ih_item_len(tp_item_head(&path)); ret = btrfs_insert_inline_extent(trans, root, objectid, 0, - symlink, len + 1); - btrfs_set_stack_inode_nbytes(btrfs_inode, len + 1); + symlink, len); + btrfs_set_stack_inode_nbytes(btrfs_inode, len); fail: pathrelse(&path); return ret; diff --git a/kernel-shared/file-item.c b/kernel-shared/file-item.c index d2da56e1f523..22fa1b03bed7 100644 --- a/kernel-shared/file-item.c +++ b/kernel-shared/file-item.c @@ -26,6 +26,7 @@ #include "kernel-shared/extent_io.h" #include "kernel-shared/uapi/btrfs.h" #include "common/internal.h" +#include "common/messages.h" #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r->fs_info) - \ sizeof(struct btrfs_item) * 2) / \ @@ -97,6 +98,8 @@ int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans, int err = 0; int ret; + UASSERT(size < trans->fs_info->sectorsize); + path = btrfs_alloc_path(); if (!path) return -ENOMEM; From patchwork Tue Sep 3 07:29:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13788168 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 6AEF71DAC75 for ; Tue, 3 Sep 2024 07:29:29 +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=1725348571; cv=none; b=qoeZ052uSEQGx2XcT+vWxSRfUb3PUboxYI35yK6sWesfgCt9D82BvwoYJvnUePdAvlt7rb6WVgSyi6lanYtRVWD+ZhDb5y+1WdwBjObyipa0wNLTvtE1Rs+scyNgCFuwlDNnHmeaHr8+2jq+3DTHug/JRH9ODWR1Av6Kcp0/YQk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725348571; c=relaxed/simple; bh=Sg1sYL/NQjcQoSnyEqU3YuSPYWd/oqwWECSk2HqgJtQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lPPBn6wJtmDuG/GmOkbOeqRBT48g2nQXysABGnRMFK1FKAhKBHVsC0CkHae4YJ6PWKCeB1R8LqOvnaBGprmDNZrw3QpClEvpdUelIw0kDk9vfNly/NzUZxBsCDQeDqEmX8OPMQ9UxWUnCxjXfH1r0/CFk5n9d5VvzwH9n96E80E= 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=mhMbVABv; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=mhMbVABv; 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="mhMbVABv"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="mhMbVABv" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 AB21621B82 for ; Tue, 3 Sep 2024 07:29:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348567; 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=WAd2/Ipad3Nx0DnLeNeJ4cndkfT0cQfKsRsaALyRJ2U=; b=mhMbVABvOQ6+ZLIA84sMBidy2h/KhO2t+N0rb76wIUTrs5q45Oy6WxQmkBOb5hCBODwknt DWmC/LGWysINIWGjjFZYeVQV3Gpuwv0KhIlpUz0Fqfgx7qidi5NPDmM7gJ+5GfAMSadZKv 8uIL/V9FK/CXJ2nCQBqgucqqKH9EaHY= Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348567; 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=WAd2/Ipad3Nx0DnLeNeJ4cndkfT0cQfKsRsaALyRJ2U=; b=mhMbVABvOQ6+ZLIA84sMBidy2h/KhO2t+N0rb76wIUTrs5q45Oy6WxQmkBOb5hCBODwknt DWmC/LGWysINIWGjjFZYeVQV3Gpuwv0KhIlpUz0Fqfgx7qidi5NPDmM7gJ+5GfAMSadZKv 8uIL/V9FK/CXJ2nCQBqgucqqKH9EaHY= Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E95AB13A52 for ; Tue, 3 Sep 2024 07:29:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 8HKLKta61mY2TwAAD6G6ig (envelope-from ) for ; Tue, 03 Sep 2024 07:29:26 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/4] btrfs-progs: check/original: detect invalid file extent items for symbol links Date: Tue, 3 Sep 2024 16:59:00 +0930 Message-ID: <82d4efde3f7f4cf14767629f647fb01698e31312.1725348299.git.wqu@suse.com> X-Mailer: git-send-email 2.46.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-Level: X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:email,suse.com:mid]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: -2.80 X-Spam-Flag: NO [BUG] There is a recent bug that btrfs/012 fails and kernel rejects to read a symbol link which is backed by a regular extent. Furthremore in that case, "btrfs check" doesn't detect such problem at all. [CAUSE] For symbol links, we only allow inline file extents, and this means we should only have a symbol link target which is smaller than 4K. But btrfs check doesn't handle symbol link inodes any differently, thus it doesn't check if the file extents are inlined or not, nor reporting this problem as an error. [FIX] When processing data extents, if we find the owning inode is a symbol link, and the file extent is regular/preallocated, mark the inode with I_ERR_FILE_EXTENT_TOO_LARGE error. Signed-off-by: Qu Wenruo --- check/main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/check/main.c b/check/main.c index 205bbb4a3c73..93da49b70fc4 100644 --- a/check/main.c +++ b/check/main.c @@ -1745,6 +1745,13 @@ static int process_file_extent(struct btrfs_root *root, rec->errors |= I_ERR_BAD_FILE_EXTENT; if (disk_bytenr > 0) rec->found_size += num_bytes; + /* + * Symbol link should only have inlined extent. + * A regular extent means it's already too large to + * be inlined. + */ + if (S_ISLNK(rec->imode)) + rec->errors |= I_ERR_FILE_EXTENT_TOO_LARGE; } else { rec->errors |= I_ERR_BAD_FILE_EXTENT; } From patchwork Tue Sep 3 07:29:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13788169 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 B0194200105 for ; Tue, 3 Sep 2024 07:29:30 +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=1725348572; cv=none; b=ipSP4/t6GTsby0XrlVgI5cj/RXGa3LaBx3qRuswvZytfUZuEPxL8sRequuV/D5YyyyCrjwZjGR+VoX0/2Czi436xAR0GLy4/A1+E7XjbAZyzz5IYyb+A7o6RCicDr0KYqwidxfPL9zqPJDRySu32IDJii//ziucVdbMwzEel3GQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725348572; c=relaxed/simple; bh=pH1M09rD3gj+Hldor4uc2wZ7uRhVPubf67ZovSUuUWQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N5bsWf7NPkYKawDVAxSbaRU7eU6zoSnG/qVjTlA/w7WvS/YTh2ZiVZBHL7jqX+FB/hW0AIVsx57J9jwnOWLXL1YS3pimH4T5A3o3/fBfRm04557uexaGOyIFc52Qu+ozRD3uRy3BKj+tDR4ZLElLzdGB8IrKcaTn6zUytfQavR0= 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=ovVdwtfF; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=ovVdwtfF; 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="ovVdwtfF"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="ovVdwtfF" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 DF63E1FCEE for ; Tue, 3 Sep 2024 07:29:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348568; 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=XOGIkAsVz3bHpC559NMm5hInpAGk7TG1nvqGylkyBaA=; b=ovVdwtfFDN2OmzeG/xmHF/Gkz5MSuj7WwOgXz0EJBoRuOs4mV38kQ2EiFzKXHpEZNCA2MP E9PrarxqcCRy5cYHRgO88cQ7wcJDDwTp78bhZga4sBYGJkG63cp23XFgi/ptO/G9GLJgm7 jaCVUFwin4Z2L2cEge+wkEUH8cnKADc= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348568; 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=XOGIkAsVz3bHpC559NMm5hInpAGk7TG1nvqGylkyBaA=; b=ovVdwtfFDN2OmzeG/xmHF/Gkz5MSuj7WwOgXz0EJBoRuOs4mV38kQ2EiFzKXHpEZNCA2MP E9PrarxqcCRy5cYHRgO88cQ7wcJDDwTp78bhZga4sBYGJkG63cp23XFgi/ptO/G9GLJgm7 jaCVUFwin4Z2L2cEge+wkEUH8cnKADc= Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 28F7613A52 for ; Tue, 3 Sep 2024 07:29:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id cC8pN9e61mY2TwAAD6G6ig (envelope-from ) for ; Tue, 03 Sep 2024 07:29:27 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/4] btrfs-progs: check/lowmem: detect invalid file extents for symbol links Date: Tue, 3 Sep 2024 16:59:01 +0930 Message-ID: <044b77cd6c56bcec3ba011820b9f7977182280b6.1725348299.git.wqu@suse.com> X-Mailer: git-send-email 2.46.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: -2.80 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:mid,suse.com:email]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; RCVD_TLS_ALL(0.00)[] X-Spam-Flag: NO X-Spam-Level: [BUG] There is a recent bug that btrfs/012 fails and kernel rejects to read a symbol link which is backed by a regular extent. Furthremore in that case, "btrfs check --mode=lowmem" doesn't detect such problem at all. [CAUSE] For symbol links, we only allow inline extents, and this means we should only have a symbol link target which is smaller than 4K. But lowmem mode btrfs check doesn't handle symbol link inodes any differently, thus it doesn't check if the file extents are inlined or not, nor reporting this problem as an error. [FIX] When processing data extents, if we find the owning inode is a symbol link, and the file extent is regular/preallocated, report an error for the bad file extent item. Signed-off-by: Qu Wenruo --- check/mode-lowmem.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c index a9908eaf629d..a5e49f03355d 100644 --- a/check/mode-lowmem.c +++ b/check/mode-lowmem.c @@ -3351,6 +3351,31 @@ out_no_release: return err; } +static int grab_inode_item(struct btrfs_root *root, + u64 ino, struct btrfs_inode_item *ret_ii) +{ + struct btrfs_path path = { 0 }; + struct btrfs_key key = { + .objectid = ino, + .type = BTRFS_INODE_ITEM_KEY, + .offset = 0 + }; + int ret; + + ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); + if (ret > 0) + ret = -ENOENT; + if (ret < 0) + goto out; + + read_extent_buffer(path.nodes[0], ret_ii, + btrfs_item_ptr_offset(path.nodes[0], path.slots[0]), + sizeof(*ret_ii)); +out: + btrfs_release_path(&path); + return ret; +} + /* * Check EXTENT_DATA item, mainly for its dbackref in extent tree * @@ -3371,6 +3396,7 @@ static int check_extent_data_item(struct btrfs_root *root, struct btrfs_extent_item *ei; struct btrfs_extent_inline_ref *iref; struct btrfs_extent_data_ref *dref; + struct btrfs_inode_item inode_item; u64 owner; u64 disk_bytenr; u64 disk_num_bytes; @@ -3400,6 +3426,24 @@ static int check_extent_data_item(struct btrfs_root *root, extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi); offset = btrfs_file_extent_offset(eb, fi); + /* + * There is a regular/preallocated data extent. Make sure the owning + * inode is not a symbol link. + * As symbol links can only have inline data extents. + */ + ret = grab_inode_item(root, fi_key.objectid, &inode_item); + if (ret < 0) { + errno = -ret; + error("failed to grab the inode item for inode %llu: %m", + fi_key.objectid); + err |= INODE_ITEM_MISSING; + } + if (S_ISLNK(inode_item.mode)) { + error("symbol link at root %lld ino %llu has regular/preallocated extents", + root->root_key.objectid, fi_key.objectid); + err |= FILE_EXTENT_ERROR; + } + /* Check unaligned disk_bytenr, disk_num_bytes and num_bytes */ if (!IS_ALIGNED(disk_bytenr, gfs_info->sectorsize)) { error( From patchwork Tue Sep 3 07:29:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 13788170 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 AAC8E1DAC75 for ; Tue, 3 Sep 2024 07:29:31 +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=1725348574; cv=none; b=ps01CHml6FQ/GnjYiveZPiAI/q7PnRTeSc2kStsoTTI14hUWRTW73FZvg7lafBbWMgYrBgYl/isAaOaTiSLIoZvjfLyjGo0CVJkY18yEXsAKvg7TKqOggxwaKzbTrq2RLyhs4hVTX8qYg8XqgVRYRXoXQZW+g3wEKrRxuHhm/l4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725348574; c=relaxed/simple; bh=+Z7CH9mZp0NktwpUoLUfi2rmlUiwBwVGKPEGSTrk6TI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ElSlHjQxLzP1URc+30Wv/3BODD7CknBnohwsA6sJT+4nclcwnPAEtlwHi3INOSNMbZ1h3bLX9uJ0mexA/ky7rVwte0FYcZ9ZMpDmFEmS03lNiX9k93IO4w1DtyTAiZHw1pZyWBNR+GuBl0uoEogJ4k5u7Yomtw4sWOIxrUyVmRc= 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=NMa+Tggf; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=NMa+Tggf; 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="NMa+Tggf"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="NMa+Tggf" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 1EAC71FCF0 for ; Tue, 3 Sep 2024 07:29:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348570; 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=pZjnjnMKIDk8n6pMeBglt/owzdcB/3QiKV0j/LoUDSY=; b=NMa+Tggfk0vfqO2L8DjLMakrTlPmuF2xLo4v89qCubfA4DFdrjISs+SWvE35qCY5Ob/YDO E3E5JBYCIiXXiNIEHZ3jyxLRhtGE+AXz3Z03pryfuCjONRAZZ8qMVJN5ZLer8m0B8BqoRU GOrDCS53DZjnK2PFlS83bxIR1i432/M= Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1725348570; 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=pZjnjnMKIDk8n6pMeBglt/owzdcB/3QiKV0j/LoUDSY=; b=NMa+Tggfk0vfqO2L8DjLMakrTlPmuF2xLo4v89qCubfA4DFdrjISs+SWvE35qCY5Ob/YDO E3E5JBYCIiXXiNIEHZ3jyxLRhtGE+AXz3Z03pryfuCjONRAZZ8qMVJN5ZLer8m0B8BqoRU GOrDCS53DZjnK2PFlS83bxIR1i432/M= Received: from imap1.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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 5D55313A52 for ; Tue, 3 Sep 2024 07:29:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id cBZiCNm61mY2TwAAD6G6ig (envelope-from ) for ; Tue, 03 Sep 2024 07:29:29 +0000 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH 4/4] btrfs-progs: convert-tests: add a test case to verify large symbol link handling Date: Tue, 3 Sep 2024 16:59:02 +0930 Message-ID: <715a310387bb3138bd0e41809e21bfef3825df1d.1725348299.git.wqu@suse.com> X-Mailer: git-send-email 2.46.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: -2.80 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[99.99%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_HAS_DN(0.00)[]; RCVD_TLS_ALL(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-btrfs@vger.kernel.org]; FROM_EQ_ENVFROM(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:mid,suse.com:email] X-Spam-Flag: NO X-Spam-Level: The new test case will: - Create a symbol which contains a 4095 bytes sized target on ext4 - Convert the ext4 to btrfs - Make sure we can still read the symbol link For unpatched btrfs-convert, the resulted symbol link will be rejected by kernel and fail. Signed-off-by: Qu Wenruo --- .../027-large-symbol-link/test.sh | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 tests/convert-tests/027-large-symbol-link/test.sh diff --git a/tests/convert-tests/027-large-symbol-link/test.sh b/tests/convert-tests/027-large-symbol-link/test.sh new file mode 100755 index 000000000000..85f7329a86d2 --- /dev/null +++ b/tests/convert-tests/027-large-symbol-link/test.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# Make sure btrfs-convert can handle a symbol link which is 4095 bytes large + +source "$TEST_TOP/common" || exit +source "$TEST_TOP/common.convert" || exit + +setup_root_helper +prepare_test_dev 1G +check_global_prereq mkfs.ext4 + +link_target="" + +# Soft link in btrfs can only have inlined data extent. +# So here we create a symbol link whose target is 4095 bytes length. +for ((i = 0; i < 4095; i++)); do + link_target+="b" +done + +convert_test_prep_fs ext4 mke2fs -t ext4 -b 4096 +run_check $SUDO_HELPER ln -s "$link_target" "$TEST_MNT/symbol_link" +run_check_umount_test_dev + +# For unpatched btrfs-convert, it will always append one byte to the +# link target, causing above 4095 target to be 4096, exactly one sector, +# resulting a regular file extent. +convert_test_do_convert + +run_check_mount_test_dev +# If the unpatched btrfs-convert created a regular extent, and the kernel is +# newer enough, such readlink will be rejected by kernel. +run_check $SUDO_HELPER readlink "$TEST_MNT/symbol_link" +run_check_umount_test_dev +