Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp8120252imu; Tue, 4 Dec 2018 03:20:59 -0800 (PST) X-Google-Smtp-Source: AFSGD/UXcCVEb++D5a4vWiRkFRcaG7r1z62KX9XGsJFT7RWzw0L1eW/x60+8pExg89SgN2qwNjr8 X-Received: by 2002:a62:1d4c:: with SMTP id d73mr19847785pfd.90.1543922459543; Tue, 04 Dec 2018 03:20:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543922459; cv=none; d=google.com; s=arc-20160816; b=J1z8xt30wnRpRGskoGrxwqbJ95g7du6LpUWNmT8y7ZVJ3QZOFQHY3DTYatvOF5fTlY UC1SQ7cC47MLnXJwX+QZBRQAdEhA/cxppxq6ortcNAFvm2RlbgrFojUTuiFZx3Z2FcUj jMgVahfmlCicCNti/+GYXVOFeWkEhpqa4dC7vS+pTgQGox/fYGxW0fLWKrUQCV0oHDsm xfIlYTulrsrEELdYa+eC3On/0WMtZHncemsahJY7HmpULDpLDHu0lv07wzp9WSL+o9yx mHctm2Q21BEfvzderFZnROInxw8MQyfaETuSrbtQuGCQOJRB76pcmcsx9o4lS/WT2M7H 7IwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LwB0/i7D3BqHeFsBqH7VUAB8CwLQzrkeVXhFMMnJL9w=; b=NkcZt0CxyePCKm6WDYNfep5vOHLGpToRoYwa+CgAiwBWPbdTPy8uoy7S/AbGfBw6TE vW/DQFD28FgZKNOSgUZydLI69Tj+qMgjTMviApfEQQuluFGg1cdmDj+5Epja7/ahYpni Ld2RIWvKFcGi+DbWIYcFAb5/DNBRT/QL/OrT6jXpZeBg7l78npAPGjeAtT6J52kM4Ea9 dU74Ti7jMSG6XietX0HeFTfzJU3PynYu5VN1tr1GMXna5PXg04dnUCY4ptWb9Twqxl6p AOpxzOd+kAXjfMoqhDFy46f6lNiOR90LYNtSVhJ5dc+N4CDL2gBD+FsFAvJ9iwkRio4h T2Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BTbWg45V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f1si14310108pgq.553.2018.12.04.03.20.44; Tue, 04 Dec 2018 03:20:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=BTbWg45V; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727492AbeLDLUI (ORCPT + 99 others); Tue, 4 Dec 2018 06:20:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:50276 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727295AbeLDLDm (ORCPT ); Tue, 4 Dec 2018 06:03:42 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AB0152087F; Tue, 4 Dec 2018 11:03:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543921421; bh=R4mzaICfOqnGpEJTPuQVAG8MEhYlOOLWbVw6fmnva/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BTbWg45VKqyjzQC+LcffoRvBvSE8nxfOoyaS9KRMV6UZVjx4zj0eXEQdHZFdKIbSC vyBkNUet461Nga7TuaJTwK3eP20miwQ36wXL2Pk16lsAl88JMuoOVN1bQKoyF7NdjQ q2h6dvqWAj547b1dtdUnPW1nW7i6jIew0wo5bPyw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qu Wenruo , Nikolay Borisov , David Sterba , Ben Hutchings , Sasha Levin Subject: [PATCH 4.14 029/146] btrfs: Refactor check_leaf function for later expansion Date: Tue, 4 Dec 2018 11:48:35 +0100 Message-Id: <20181204103728.033382562@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181204103726.750894136@linuxfoundation.org> References: <20181204103726.750894136@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ commit c3267bbaa9cae09b62960eafe33ad19196803285 upstream. Current check_leaf() function does a good job checking key order and item offset/size. However it only checks from slot 0 to the last but one slot, this is good but makes later expansion hard. So this refactoring iterates from slot 0 to the last slot. For key comparison, it uses a key with all 0 as initial key, so all valid keys should be larger than that. And for item size/offset checks, it compares current item end with previous item offset. For slot 0, use leaf end as a special case. This makes later item/key offset checks and item size checks easier to be implemented. Also, makes check_leaf() to return -EUCLEAN other than -EIO to indicate error. Signed-off-by: Qu Wenruo Reviewed-by: Nikolay Borisov Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Ben Hutchings Signed-off-by: Sasha Levin --- fs/btrfs/disk-io.c | 50 +++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 0e67cee73c53..4a1e63df1183 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -554,8 +554,9 @@ static noinline int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf) { struct btrfs_fs_info *fs_info = root->fs_info; + /* No valid key type is 0, so all key should be larger than this key */ + struct btrfs_key prev_key = {0, 0, 0}; struct btrfs_key key; - struct btrfs_key leaf_key; u32 nritems = btrfs_header_nritems(leaf); int slot; @@ -588,7 +589,7 @@ static noinline int check_leaf(struct btrfs_root *root, CORRUPT("non-root leaf's nritems is 0", leaf, check_root, 0); free_extent_buffer(eb); - return -EIO; + return -EUCLEAN; } free_extent_buffer(eb); } @@ -598,28 +599,23 @@ static noinline int check_leaf(struct btrfs_root *root, if (nritems == 0) return 0; - /* Check the 0 item */ - if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != - BTRFS_LEAF_DATA_SIZE(fs_info)) { - CORRUPT("invalid item offset size pair", leaf, root, 0); - return -EIO; - } - /* - * Check to make sure each items keys are in the correct order and their - * offsets make sense. We only have to loop through nritems-1 because - * we check the current slot against the next slot, which verifies the - * next slot's offset+size makes sense and that the current's slot - * offset is correct. + * Check the following things to make sure this is a good leaf, and + * leaf users won't need to bother with similar sanity checks: + * + * 1) key order + * 2) item offset and size + * No overlap, no hole, all inside the leaf. */ - for (slot = 0; slot < nritems - 1; slot++) { - btrfs_item_key_to_cpu(leaf, &leaf_key, slot); - btrfs_item_key_to_cpu(leaf, &key, slot + 1); + for (slot = 0; slot < nritems; slot++) { + u32 item_end_expected; + + btrfs_item_key_to_cpu(leaf, &key, slot); /* Make sure the keys are in the right order */ - if (btrfs_comp_cpu_keys(&leaf_key, &key) >= 0) { + if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { CORRUPT("bad key order", leaf, root, slot); - return -EIO; + return -EUCLEAN; } /* @@ -627,10 +623,14 @@ static noinline int check_leaf(struct btrfs_root *root, * item data starts at the end of the leaf and grows towards the * front. */ - if (btrfs_item_offset_nr(leaf, slot) != - btrfs_item_end_nr(leaf, slot + 1)) { + if (slot == 0) + item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); + else + item_end_expected = btrfs_item_offset_nr(leaf, + slot - 1); + if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { CORRUPT("slot offset bad", leaf, root, slot); - return -EIO; + return -EUCLEAN; } /* @@ -641,8 +641,12 @@ static noinline int check_leaf(struct btrfs_root *root, if (btrfs_item_end_nr(leaf, slot) > BTRFS_LEAF_DATA_SIZE(fs_info)) { CORRUPT("slot end outside of leaf", leaf, root, slot); - return -EIO; + return -EUCLEAN; } + + prev_key.objectid = key.objectid; + prev_key.type = key.type; + prev_key.offset = key.offset; } return 0; -- 2.17.1