From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245413; b=REIw8O4C/dCKW2n4phnQIA+IbfyJLsRpsN87vT0EyYnl3D5Ieoczj0n4ThDAe7EEjo/1 rK4b8jROPf+OszopICik4QEltxsZ9eYd79gwaxePvnthp21Z8zQvv1O/vG5pCPnMFz+ix /xRboRZZIY2hKHfnbx1VtmpRSok46hAlbTKcJvoo7ChWpw8QSfv/QEJemgQ3YdMyx489v pt6byDOLKzX9svV9YROCcDz12GozokVss07d+uZ+69T1XVDDgw93HkwoscRNpBjtsSbiB 6tEuAMPn90fRjS00kcWxuJ3doeeK922avQgzFM5xU7yUeHSGmGe4exJNaz+CGyx41gg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245413; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References:Autocrypt: Content-Type:Content-Transfer-Encoding:User-Agent:MIME-Version; bh=IyN54lSKqw7MvMwkunNGQppa8vtuB43jF/7iMfBwP8g=; b=yDRB5cT8LYYycE4jIx66NzRJkFaNI7GlwrU3uAGA26B9aopK5f1eVfWRvJWFGJ4EMj0R d+5yfXbYC8NK/toB9SfMwej0nzbJnXMg0PGBf6IseHOXjZ+PsCHlbsSW5QgOeruNRT9Rb QWV7t6BCgqJTkWSmeNLByKv7AwFH7BfSFlmJvq4d31jaU1ErMLX5xEGg2QDkRlqmLdWGY 9aDrYraH9QhOngIHjniLS9NByNC3Liem+sUcbf4JrZALN1DswsJNCpLqC2NAQevHu+dfa sCdaAz/fMWspavFS9l05tgtOH/kZAHqQVAT72pXotRTWVJiVHwEOA0bBuSWmCqim1TA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=dubeyko.com; spf=pass smtp.mailfrom=dubeyko.com; dkim=pass header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=Qpgdg10x; arc=none smtp.remote-ip=74.125.224.48 Received: from mail-yx1-f48.google.com (mail-yx1-f48.google.com [74.125.224.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62CF8606A322 for ; Wed, 2 Dec 2026 21:03:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 62CF8606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dubeyko.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=Qpgdg10x Received: by mail-yx1-f48.google.com with SMTP id 956f58d0204a3-67db35e7953so13848141d50.0 for ; Wed, 02 Dec 2026 13:03:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20260707.gappssmtp.com; s=20260707; t=1796245411; x=1796850211; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :from:to:cc:subject:date:message-id:reply-to:content-type; bh=IyN54lSKqw7MvMwkunNGQppa8vtuB43jF/7iMfBwP8g=; b=Qpgdg10xhqHrHLqIgFKTK9h5QJ+2g2IR0QbC0QiggnN7R6A42ycRSvLwHRb2gxJLYk DdjCugg0u3AZlN2OLMzBYUE2FWa+n8sMMEn8u5wk4avgCMKYpojSzGvGfAGiFhKKQ5zi qlJFEY6avseAOJJEwToNJ8FlIou1IRGMNmgm6BsFMKzNSMWIgAKktyMRC8Td7F4WlFE9 t4qRIQjPNPwMsAAMoGnaQFBcTHqV/eJCbLe2gtHZ+ihqb1xBFbS+WbdDtS5nfMwhf+gF XXZwkH/1yN/jCTm+cjc/pB7MZrHjfDCotWyKdLg7+HGI/QwToj5f9u0lRv8kfBPz/eCu 0azw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245411; x=1796850211; h=mime-version:user-agent:content-transfer-encoding:content-type :autocrypt:references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=IyN54lSKqw7MvMwkunNGQppa8vtuB43jF/7iMfBwP8g=; b=Abp7b26ZzuSgQK63jvM+Lwgh4RKm8laNW+98vJkXVnF1bxkVlpIDPnihT9gdTHA7FX Iq9SCXg5/OvswF3M1cnBEigH3g/TSQBYTy1tyZXfRzMVbPghNKuMzezOtTqWSd3hiayk AT4/rnOIFbvx5WhaitrZqYPTPqSfqdpu01mxJPOJlXBTZQHt8Qwd7QQ8jHza7/1rR9TY 7cXkSw0wKVFJfnLu1AC+NZBZwx+KWspLiVoB6jikbdiUGrcbYEbdpHA9wBDcb+LUumWP qufaZ8r1sStH7wLP73rX5txKJbeHpZhF4hb9337EPbuRteXmdNrM3hE29XpVbEj8LskK 5xnw== X-Forwarded-Encrypted: i=1; AL+hErEvOAiiFkiG8cspXoULaeKKKOtYxlRQZ4HzUpxT4yyw0s98mp1TKZsb4WBDZIXEAnXt0HqxdbKWbr0357k=@vger.kernel.org X-Gm-Message-State: APuU4Z+5ZGMvn8ABSo88WB5inE4xLNYTx8z0CqE74GXBhfz/nekIX0Ib Q2fK6Ir/VKcecAfV36RUUB/Dvo0db0ZgtpqB1zRIr0jzTYU4fmLnDLtHVIUZebayB+A= X-Gm-Gg: AZFdTZcp0zi3IE/vKnFD32KXHPLYBavVaAWQq4BhQJDEPqAx4SOSUHWJU2oErr9MzIE 7f97o47jqYvCKIvW8ONwX8Zs+e880RmJGHarTshfpvZ267vrET1zf42g9E8GlEUvAudIk2nIHCh bUWryT+Ompx/AWcJlrX7fXngrMh+Kv2JJlat3mKEt6LMXSmultOtjU7UxbC4wqeh4qCqX4lXU2E Shrmhy3lUqoJQ8RZW3rtEcB7dh2O/L5waqihsBD/ELiv5I2pbDQ45/eKzjOJSQrqynvVfkkBgoN ip0b9SJNOljlU/pxA7EN7nwvPx5rhRaX11XNYVevCNRwVwETKw4bq1Od5MaZOAe7UN47GK8SdFZ MSFPXrBSVwnYe1DmOfdQNXgzlxr6NzOfMUdEsOsobraMZfbfi8q/7WXSEVjRNkCubEdgD095j82 TmlpV8yMAM0lP2WEk+UYIsVC9ti5S2DPzwiZDeyqUUS45dRZgLnG28Z+ERXIrEXTEcyprokC8/4 neRZ87sa890wRgWPmUXDyirt7ORWpAdleKFT/pCGr5RozKyYo9L8OEr0lR8E+GoMgSYVTpj/9s5 1vUKGFAIlTYPAUAkEEv1ncIuVB1nLeF1KFGsKtyiHpKFfZw4PLGihKT7pYDREXITa3gigdp9Lt4 op9FZxEltyIz1Aye7beeG1yCplvtM3uEWb5bx3/wcXDrOXMfjYqs/AoiKt9yOzTvvbaxPtHlc9h +OByHGo6gTsbPQruugUi6MMS4xYKo7xCoxBe1LcZU= X-Received: by 2002:a05:690c:698c:b0:8b0:acc6:8e81 with SMTP id 00721157ae682-8b52031d1c0mr163659677b3.1.1796245411349; Wed, 02 Dec 2026 13:03:31 -0800 (PST) Received: from ?IPv6:2600:1700:6476:1430:f726:86c4:c7ef:c27? ([2600:1700:6476:1430:f726:86c4:c7ef:c27]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b52033f617sm160442267b3.39.2026.12.02.13.03.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 13:03:30 -0800 (PST) Message-ID: <1ea0a32aafb3c158f86f89ee7e6e2b2455590181.camel@dubeyko.com> Subject: Re: [PATCH v3] hfsplus: validate B-tree map chain at mount From: Viacheslav Dubeyko To: Shawn Shao Cc: John Paul Adrian Glaubitz , Yangtao Li , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 02 Dec 2026 13:03:29 -0800 In-Reply-To: <20261202075625.272796-1-shawnq1@163.com> References: <20261128082828.3075144-1-shawnq1@163.com> <26bf2beb0e958385812405d43eed469333f31b94.camel@dubeyko.com> <20261202075625.272796-1-shawnq1@163.com> Autocrypt: addr=slava@dubeyko.com; prefer-encrypt=mutual; keydata=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 NFdvziOa4FgUBC4X71upsZIoZJeii2V/Jtc3B4g== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2-9 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 On Wed, 2026-12-02 at 07:56 +0000, Shawn Shao wrote: > HFS+ map traversal follows the next links in the B-tree header and > map > nodes. A malformed link can make a map walk repeat indefinitely or > reach > a node outside the tree file. Checking only the first bitmap bit does > not > validate the chain used by later map operations. >=20 > Validate the header and complete map-node chain while opening the B- > tree. > Bound traversal by the declared node count and the complete nodes in > the > tree file, and require the map records to cover the declared node > count. > Use shared next/prev helpers to validate node type, file bounds, and > reciprocal links in leaf and index chains, and use them for B-tree > cursor > movement, node unlink/split, and map allocation/free. >=20 > When the map chain is corrupt, warn and keep the filesystem read-only > so > that it can be repaired with fsck.hfsplus. >=20 > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Signed-off-by: Shawn Shao >=20 > --- > Changes in v3: > - Replace the fast/slow pointer walk with a bounded map-chain check > at > =C2=A0 B-tree open. > - Add shared validated next/prev helpers and use them in node > traversal, > =C2=A0 split/unlink, and map allocation/free paths. > - Check physical node bounds and bitmap coverage; force read-only on > a > =C2=A0 malformed map chain and recommend fsck.hfsplus. >=20 > Testing: > - x86-64 bzImage build passed with CONFIG_HFSPLUS_FS=3Dy. > - Fixed cycle fixture mounted read-only and printed the fsck.hfsplus > =C2=A0 recommendation; its oracle reported CYCLE_MOUNTED_READ_ONLY. > - Normal-map control mounted writable; its oracle reported > =C2=A0 CONTROL_MOUNTED_WRITABLE. > - The initramfs fixture compiled with -static -O2 -Wall -Wextra - > Werror. > - git apply --check, git diff --check, and checkpatch --strict > passed. >=20 > Reproducer, configuration, and both console logs: > https://gist.github.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e >=20 > =C2=A0fs/hfsplus/bfind.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 32 +++++--= ---- > =C2=A0fs/hfsplus/bnode.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 145 > ++++++++++++++++++++++++++++++++++++++++-------- > =C2=A0fs/hfsplus/brec.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0=C2=A0= 5 +- > =C2=A0fs/hfsplus/btree.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 108 ++++++++++++= ++++++++++++++---------- > =C2=A0fs/hfsplus/hfsplus_fs.h |=C2=A0=C2=A0 4 ++ > =C2=A05 files changed, 220 insertions(+), 74 deletions(-) >=20 > diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c > index ca9813f58a6dd..d91fd95f853bc 100644 > --- a/fs/hfsplus/bfind.c > +++ b/fs/hfsplus/bfind.c > @@ -227,49 +227,45 @@ int hfs_brec_read(struct hfs_find_data *fd, > void *rec, u32 rec_len) > =C2=A0 > =C2=A0int hfs_brec_goto(struct hfs_find_data *fd, int cnt) > =C2=A0{ > - struct hfs_btree *tree; > - struct hfs_bnode *bnode; > - int idx, res =3D 0; > + struct hfs_bnode *bnode, *sibling; > + int res =3D 0; > =C2=A0 u16 off, len, keylen; > =C2=A0 > =C2=A0 bnode =3D fd->bnode; > - tree =3D bnode->tree; > =C2=A0 > =C2=A0 if (cnt < 0) { > =C2=A0 cnt =3D -cnt; > =C2=A0 while (cnt > fd->record) { > =C2=A0 cnt -=3D fd->record + 1; > =C2=A0 fd->record =3D bnode->num_recs - 1; > - idx =3D bnode->prev; > - if (!idx) { > + sibling =3D hfs_bnode_find_prev(bnode); > + if (!sibling) { > =C2=A0 res =3D -ENOENT; > =C2=A0 goto out; > =C2=A0 } > - hfs_bnode_put(bnode); > - bnode =3D hfs_bnode_find(tree, idx); > - if (IS_ERR(bnode)) { > - res =3D PTR_ERR(bnode); > - bnode =3D NULL; > + if (IS_ERR(sibling)) { > + res =3D PTR_ERR(sibling); > =C2=A0 goto out; > =C2=A0 } > + hfs_bnode_put(bnode); > + bnode =3D sibling; > =C2=A0 } > =C2=A0 fd->record -=3D cnt; > =C2=A0 } else { > =C2=A0 while (cnt >=3D bnode->num_recs - fd->record) { > =C2=A0 cnt -=3D bnode->num_recs - fd->record; > =C2=A0 fd->record =3D 0; > - idx =3D bnode->next; > - if (!idx) { > + sibling =3D hfs_bnode_find_next(bnode); > + if (!sibling) { > =C2=A0 res =3D -ENOENT; > =C2=A0 goto out; > =C2=A0 } > - hfs_bnode_put(bnode); > - bnode =3D hfs_bnode_find(tree, idx); > - if (IS_ERR(bnode)) { > - res =3D PTR_ERR(bnode); > - bnode =3D NULL; > + if (IS_ERR(sibling)) { > + res =3D PTR_ERR(sibling); > =C2=A0 goto out; > =C2=A0 } > + hfs_bnode_put(bnode); > + bnode =3D sibling; > =C2=A0 } > =C2=A0 fd->record +=3D cnt; > =C2=A0 } > diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c > index 3a1a1fa6f2e25..20dfe5493bbdd 100644 > --- a/fs/hfsplus/bnode.c > +++ b/fs/hfsplus/bnode.c > @@ -393,34 +393,39 @@ void hfs_bnode_dump(struct hfs_bnode *node) > =C2=A0 > =C2=A0void hfs_bnode_unlink(struct hfs_bnode *node) > =C2=A0{ > - struct hfs_btree *tree; > - struct hfs_bnode *tmp; > + struct hfs_btree *tree =3D node->tree; > + struct hfs_bnode *prev, *next; > =C2=A0 __be32 cnid; > =C2=A0 > - tree =3D node->tree; > - if (node->prev) { > - tmp =3D hfs_bnode_find(tree, node->prev); > - if (IS_ERR(tmp)) > - return; > - tmp->next =3D node->next; > - cnid =3D cpu_to_be32(tmp->next); > - hfs_bnode_write(tmp, &cnid, > - offsetof(struct hfs_bnode_desc, next), 4); > - hfs_bnode_put(tmp); > - } else if (node->type =3D=3D HFS_NODE_LEAF) > + prev =3D hfs_bnode_find_prev(node); > + if (IS_ERR(prev)) > + return; > + next =3D hfs_bnode_find_next(node); > + if (IS_ERR(next)) { > + hfs_bnode_put(prev); > + return; > + } > + > + if (prev) { > + prev->next =3D node->next; > + cnid =3D cpu_to_be32(prev->next); > + hfs_bnode_write(prev, &cnid, > + offsetof(struct hfs_bnode_desc, > next), 4); > + } else if (node->type =3D=3D HFS_NODE_LEAF) { > =C2=A0 tree->leaf_head =3D node->next; > + } > =C2=A0 > - if (node->next) { > - tmp =3D hfs_bnode_find(tree, node->next); > - if (IS_ERR(tmp)) > - return; > - tmp->prev =3D node->prev; > - cnid =3D cpu_to_be32(tmp->prev); > - hfs_bnode_write(tmp, &cnid, > - offsetof(struct hfs_bnode_desc, prev), 4); > - hfs_bnode_put(tmp); > - } else if (node->type =3D=3D HFS_NODE_LEAF) > + if (next) { > + next->prev =3D node->prev; > + cnid =3D cpu_to_be32(next->prev); > + hfs_bnode_write(next, &cnid, > + offsetof(struct hfs_bnode_desc, > prev), 4); > + } else if (node->type =3D=3D HFS_NODE_LEAF) { > =C2=A0 tree->leaf_tail =3D node->prev; > + } > + > + hfs_bnode_put(prev); > + hfs_bnode_put(next); > =C2=A0 > =C2=A0 /* move down? */ > =C2=A0 if (!node->prev && !node->next) > @@ -618,6 +623,100 @@ struct hfs_bnode *hfs_bnode_find(struct > hfs_btree *tree, u32 num) > =C2=A0 return ERR_PTR(-EIO); > =C2=A0} > =C2=A0 > +/* > + * Header and map nodes use a forward-only chain. Leaf and index > nodes > + * have reciprocal links to nodes of the same type and height. > + */ > +static struct hfs_bnode *hfs_bnode_find_sibling(struct hfs_bnode > *node, > + bool next) > +{ > + struct hfs_btree *tree =3D node->tree; > + struct hfs_bnode *sibling; > + u32 link =3D next ? node->next : node->prev; > + u32 back_link; > + u64 file_nodes; > + u64 node_limit; > + > + file_nodes =3D (u64)tree->inode->i_size >> tree- > >node_size_shift; > + node_limit =3D min_t(u64, tree->node_count, file_nodes); > + if (!node_limit || node->this >=3D node_limit) > + goto invalid_link; > + > + switch (node->type) { > + case HFS_NODE_HEADER: > + case HFS_NODE_MAP: > + if (!next || node->prev) > + goto invalid_link; > + break; > + case HFS_NODE_LEAF: > + case HFS_NODE_INDEX: > + break; > + default: > + goto invalid_link; > + } > + > + if (!link) > + return NULL; > + if (link =3D=3D node->this || link >=3D node_limit) > + goto invalid_link; > + > + sibling =3D hfs_bnode_find(tree, link); > + if (IS_ERR(sibling)) > + return sibling; > + > + switch (node->type) { > + case HFS_NODE_HEADER: > + case HFS_NODE_MAP: > + if (sibling->type !=3D HFS_NODE_MAP || sibling->prev) > + goto invalid_sibling; > + break; > + case HFS_NODE_LEAF: > + case HFS_NODE_INDEX: > + back_link =3D next ? sibling->prev : sibling->next; > + if (sibling->type !=3D node->type || > + =C2=A0=C2=A0=C2=A0 sibling->height !=3D node->height || > + =C2=A0=C2=A0=C2=A0 back_link !=3D node->this) > + goto invalid_sibling; > + break; > + default: > + goto invalid_sibling; > + } > + > + return sibling; > + > +invalid_sibling: > + hfs_bnode_put(sibling); > +invalid_link: > + pr_err("invalid %s link %u of node %u (type %u) in b-tree > %u\n", > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 next ? "next" : "prev", link, node= ->this, node->type, > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 tree->cnid); > + return ERR_PTR(-EIO); > +} > + > +/** > + * hfs_bnode_find_next - get a node linked after a B-tree node > + * @node: node whose next link is followed > + * > + * Return: the linked node with a reference held, NULL if there is > no > + * next node, or an ERR_PTR() if the link is invalid. > + */ > +struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node) > +{ > + return hfs_bnode_find_sibling(node, true); > +} > + > +/** > + * hfs_bnode_find_prev - get a node linked before a B-tree node > + * @node: leaf or index node whose previous link is followed > + * > + * Return: the linked node with a reference held, NULL if there is > no > + * previous node, or an ERR_PTR() if the link is invalid. > + */ > +struct hfs_bnode *hfs_bnode_find_prev(struct hfs_bnode *node) > +{ > + return hfs_bnode_find_sibling(node, false); > +} > + > =C2=A0void hfs_bnode_free(struct hfs_bnode *node) > =C2=A0{ > =C2=A0 int i; > diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c > index f416e562d4397..3bb7b282d85bb 100644 > --- a/fs/hfsplus/brec.c > +++ b/fs/hfsplus/brec.c > @@ -275,10 +275,7 @@ static struct hfs_bnode *hfs_bnode_split(struct > hfs_find_data *fd) > =C2=A0 new_node->type =3D node->type; > =C2=A0 new_node->height =3D node->height; > =C2=A0 > - if (node->next) > - next_node =3D hfs_bnode_find(tree, node->next); > - else > - next_node =3D NULL; > + next_node =3D hfs_bnode_find_next(node); > =C2=A0 > =C2=A0 if (IS_ERR(next_node)) { > =C2=A0 hfs_bnode_put(node); > diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c > index bd4dbdbc8f98c..1294200b2f600 100644 > --- a/fs/hfsplus/btree.c > +++ b/fs/hfsplus/btree.c > @@ -212,6 +212,70 @@ static bool hfs_bmap_test_bit(struct hfs_bnode > *node, u32 node_bit_idx) > =C2=A0} > =C2=A0 > =C2=A0 > +/* Validate the header and the complete forward chain of map nodes. > */ > +static int hfs_btree_check_map_chain(struct hfs_btree *tree) > +{ > + struct hfs_bmap_ctx ctx; > + struct hfs_bnode *node, *next; > + struct page *page; > + u64 file_nodes; > + u64 map_bits =3D 0; > + u64 nodes; > + int err; > + > + file_nodes =3D tree->inode->i_size >> tree->node_size_shift; > + if (!tree->node_count || tree->node_count > file_nodes) > + return -EIO; > + > + node =3D hfs_bnode_find(tree, HFSPLUS_TREE_HEAD); > + if (IS_ERR(node)) > + return PTR_ERR(node); > + > + if (node->type !=3D HFS_NODE_HEADER || node->prev || > + =C2=A0=C2=A0=C2=A0 !hfs_bmap_test_bit(node, 0)) { > + err =3D -EIO; > + goto out; > + } > + page =3D hfs_bmap_get_map_page(node, &ctx, 0); > + if (IS_ERR(page)) { > + err =3D PTR_ERR(page); > + goto out; > + } > + map_bits =3D (u64)ctx.len * BITS_PER_BYTE; > + > + for (nodes =3D 1; node->next; nodes++) { > + if (nodes >=3D tree->node_count) { > + err =3D -EIO; > + goto out; > + } > + > + next =3D hfs_bnode_find_next(node); > + if (IS_ERR(next)) { > + err =3D PTR_ERR(next); > + goto out; > + } > + if (!next) { > + err =3D -EIO; > + goto out; > + } > + > + hfs_bnode_put(node); > + node =3D next; > + > + page =3D hfs_bmap_get_map_page(node, &ctx, 0); > + if (IS_ERR(page)) { > + err =3D PTR_ERR(page); > + goto out; > + } > + map_bits +=3D (u64)ctx.len * BITS_PER_BYTE; > + } > + > + err =3D map_bits < tree->node_count ? -EIO : 0; > +out: > + hfs_bnode_put(node); > + return err; > +} > + > =C2=A0/** > =C2=A0 * hfs_bmap_clear_bit - clear a bit in the b-tree map > =C2=A0 * @node: the b-tree node containing the map record > @@ -270,10 +334,10 @@ struct hfs_btree *hfs_btree_open(struct > super_block *sb, u32 id) > =C2=A0 struct hfs_btree *tree; > =C2=A0 struct hfs_btree_header_rec *head; > =C2=A0 struct address_space *mapping; > - struct hfs_bnode *node; > =C2=A0 struct inode *inode; > =C2=A0 struct page *page; > =C2=A0 unsigned int size; > + int err; > =C2=A0 > =C2=A0 tree =3D kzalloc_obj(*tree); > =C2=A0 if (!tree) > @@ -381,19 +445,16 @@ struct hfs_btree *hfs_btree_open(struct > super_block *sb, u32 id) > =C2=A0 kunmap_local(head); > =C2=A0 put_page(page); > =C2=A0 > - node =3D hfs_bnode_find(tree, HFSPLUS_TREE_HEAD); > - if (IS_ERR(node)) > + err =3D hfs_btree_check_map_chain(tree); > + if (err =3D=3D -ENOMEM) > =C2=A0 goto free_inode; > - > - if (!hfs_bmap_test_bit(node, 0)) { > - pr_warn("(%s): %s (cnid 0x%x) map record invalid or > bitmap corruption detected, forcing read-only.\n", > - sb->s_id, hfs_btree_name(id), id); > + if (err) { > + pr_warn("(%s): %s (cnid 0x%x) map chain is invalid, > forcing read-only.\n", > + sb->s_id, hfs_btree_name(id), id); > =C2=A0 pr_warn("Run fsck.hfsplus to repair.\n"); > =C2=A0 sb->s_flags |=3D SB_RDONLY; > =C2=A0 } > =C2=A0 > - hfs_bnode_put(node); > - > =C2=A0 return tree; > =C2=A0 > =C2=A0 fail_page: > @@ -587,13 +648,12 @@ struct hfs_bnode *hfs_bmap_alloc(struct > hfs_btree *tree) > =C2=A0 ctx.len--; > =C2=A0 } > =C2=A0 kunmap_local(data); > - nidx =3D node->next; > - if (!nidx) { > + next_node =3D hfs_bnode_find_next(node); > + if (!next_node) { > =C2=A0 hfs_dbg("create new bmap node\n"); > =C2=A0 next_node =3D hfs_bmap_new_bmap(node, idx); > =C2=A0 hfs_btree_write(tree); > - } else > - next_node =3D hfs_bnode_find(tree, nidx); > + } > =C2=A0 hfs_bnode_put(node); > =C2=A0 if (IS_ERR(next_node)) > =C2=A0 return next_node; > @@ -632,30 +692,20 @@ void hfs_bmap_free(struct hfs_bnode *node) > =C2=A0 return; > =C2=A0 } > =C2=A0 while (nidx >=3D len * 8) { > - u32 i; > + struct hfs_bnode *next_node; > =C2=A0 > =C2=A0 nidx -=3D len * 8; > - i =3D node->next; > - if (!i) { > - /* panic */; > - pr_crit("unable to free bnode %u. " > - "bmap not found!\n", > + next_node =3D hfs_bnode_find_next(node); > + if (!next_node) { > + pr_crit("unable to free bnode %u. bmap not > found!\n", > =C2=A0 node->this); > =C2=A0 hfs_bnode_put(node); > =C2=A0 return; > =C2=A0 } > =C2=A0 hfs_bnode_put(node); > - node =3D hfs_bnode_find(tree, i); > - if (IS_ERR(node)) > - return; > - if (node->type !=3D HFS_NODE_MAP) { > - /* panic */; > - pr_crit("invalid bmap found! " > - "(%u,%d)\n", > - node->this, node->type); > - hfs_bnode_put(node); > + if (IS_ERR(next_node)) > =C2=A0 return; > - } > + node =3D next_node; > =C2=A0 len =3D hfs_brec_lenoff(node, 0, &off); > =C2=A0 if (hfs_brec_len_invalid(node, len)) { > =C2=A0 pr_err("invalid bmap record length: node %u, > len %u\n", > diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h > index 6f240f87f8ff1..1ebbd78b3e30b 100644 > --- a/fs/hfsplus/hfsplus_fs.h > +++ b/fs/hfsplus/hfsplus_fs.h > @@ -303,6 +303,8 @@ static inline unsigned short > hfsplus_min_io_size(struct super_block *sb) > =C2=A0#define hfs_bnode_unlink hfsplus_bnode_unlink > =C2=A0#define hfs_bnode_findhash hfsplus_bnode_findhash > =C2=A0#define hfs_bnode_find hfsplus_bnode_find > +#define hfs_bnode_find_next hfsplus_bnode_find_next > +#define hfs_bnode_find_prev hfsplus_bnode_find_prev > =C2=A0#define hfs_bnode_unhash hfsplus_bnode_unhash > =C2=A0#define hfs_bnode_free hfsplus_bnode_free > =C2=A0#define hfs_bnode_create hfsplus_bnode_create > @@ -384,6 +386,8 @@ void hfs_bnode_unlink(struct hfs_bnode *node); > =C2=A0struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 > cnid); > =C2=A0void hfs_bnode_unhash(struct hfs_bnode *node); > =C2=A0struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num); > +struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node); > +struct hfs_bnode *hfs_bnode_find_prev(struct hfs_bnode *node); > =C2=A0void hfs_bnode_free(struct hfs_bnode *node); > =C2=A0struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)= ; > =C2=A0void hfs_bnode_get(struct hfs_bnode *node); Sorry, I am going to reject your patch. Because, you cannot write and test all this code alone for several hours. I can ask my Claude AI agent to fix all of these issues. Why do I need your patches then? Don't get me wrong. I am not against of using AI to help us to fix bugs, to learn and to be more productive. But kernel work is not about asking AI to generate something without any understanding of this. Thanks, Slava.