From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796171191; b=eiWvulk+fno84Vr+B/xOdBcDsF8ARVuBvlUgDxjtXgYaKalDpPqpluq9TF4cdssKvfn2 kgg+KvubItlonWqk5FsBjqujoRjqI3cktFHvFr4gXk/XlrAaSWN2BUWWsYJFXpG+FaDIo q5s/eiCB+4mvCbicVvSHTu9S3zszgo5klsq/DgNR99tyZuaeAzcrsYGZ6Xdx3ngSYVOTM tMRefdSxib/0Vq1Bl8Q19GCfNE4FP/iVVllAEHd27Ptfn5FaatGVlc0+A7mS6cfGq68qx xYMqgMI4iLk0uGyPValtG2QKB05bk1tvwEUMfKWw1bCWuMpihnPIxkfG1cCOeS/iuQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796171191; 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=B3fGebEnhwyxthde+oPOxArhkjBiY9ZxTK1x1wznLrQ=; b=1ovaiUD4eG/CIvArS+C5eBDJcIO4G1eP9qJ6BYRVhSsgv/h1C1KXS22t/L3gbtX/W7bW iOOdQ2dUwW3SOF7TKj1pRVb+NeGtYrTmH9uky/94mWHut9X4iTeBnPCSNWjlxBiIw4sCR 9GHSWrfDthdeAIehQcLrv5rt2X0cJdbVVfVNwRuYxfxNiNfpfmBEvmE4OsybdGz5kKvDy aa8HkDqw0RsUTqfhbl6sN4D4k76HfvzSvc99WRJEJI3laxqAu7GnOQOcuEp+2rz4A+y4z TVCwVsUTJ7Up2Rl6+YeVsInqSlqjNxEiPmlRLrIupZo0z5WsNFljIbaDal+8h/9ml2w== 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=dyuVBBpG; arc=none smtp.remote-ip=74.125.224.54 Received: from mail-yx1-f54.google.com (mail-yx1-f54.google.com [74.125.224.54]) (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 27D5E606A320 for ; Wed, 2 Dec 2026 00:26:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 27D5E606A320 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, unprotected) header.d=dubeyko-com.20260707.gappssmtp.com header.i=@dubeyko-com.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=dyuVBBpG Received: by mail-yx1-f54.google.com with SMTP id 956f58d0204a3-67db23d8892so13439881d50.2 for ; Tue, 01 Dec 2026 16:26:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dubeyko-com.20260707.gappssmtp.com; s=20260707; t=1796171190; x=1796775990; 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=B3fGebEnhwyxthde+oPOxArhkjBiY9ZxTK1x1wznLrQ=; b=dyuVBBpG1QGCKJdjDLxZPrCFtezV/TY+r0Qo0gV8ozcHqDrJHEcmhINxGQ3RRYMWKK pTE1L8IeFEff5NuQ3Tsf7avLL/PiQWs1ehAoggREAh7RuRpBZvytTrJnGoXE6/Ak7Tzc 9fALAKmhaCJM1Ou17PspsKNLSUJ+pMks5jYqqNVNP5A9Z7bylPHEkggs2/bX07A2vlfi ub7JYb2goR3BZ2JoUbyW/WYvMsUh0FCjvkXJREBfp76WnprfTMxQnTrJFJg9DCZX7Fkc 58n9iyx0eQji5uyO7dexWifI9wY+yDd9RniS215p35hCc7OS8NuW6tLYGmh8w0UIkVMe Xb0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796171190; x=1796775990; 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=B3fGebEnhwyxthde+oPOxArhkjBiY9ZxTK1x1wznLrQ=; b=B57r9Hun1lDswdrwYL9QEdUwNtoieGJzVz3+w7pSadHT5wsHBMgh4t8UHnq5vNO/jy Tid2WAlbsl9HRS3wTm1feH57zh183a2uJieSygsnCJ0orXyg1547rjprfT+2lgshNcvx adOJiOO5Sm3zdgdP8s8A0q3yicqUMNjMgiT8R/OojzWQSB51kTFAlhrwQg6KwkBscZto JtX9dl7Cw7qOChte6k0oq6sGcM2mF3zxX7qDg0m+bOz3fq7mrOSZu3sBv2ZifSmTbpMq 0NztWZltPwaQIkcdW6bkCDh630CezV7FC7I4RpMXxw5g9p570cnB+GvKK36aqxWRmGMv xQuA== X-Forwarded-Encrypted: i=1; AL+hErFxsry+K8M8GP343C+rlAcKQubdsZhJROnOsbY7jWoxXdlpW7KwSGHBTHyrI0GNyknX/5lih77UH9scDKs=@vger.kernel.org X-Gm-Message-State: APuU4Z+m4Jp4V54ly6dszilsTj81aX+tlkje9r2to0eZstqfxhbSLxgf BrcHcWm0MeEbv9Vii4nq/7QLb1I32gWGCl8Apv5SDbvgnZVnESuZ6xZ2f9fKfJMAMgo= X-Gm-Gg: AZFdTZcEaug5YYhmu0JfaBIRLzf1FqWlzMDIfKQrkuDs2etOGK7mS3aFPMwO/75xOk8 +uvVg1FL07ttBsqVlpA+nDwtkpO7m7aWzbxMqc8N/HVpkDv1vDauyFO3jI3LvHPXD7snjPjte+T 4PwM0YRUNdVr9RTawNfNebX6XYp8PppwwmSfjPk/A5i/0jpjowv43jO0ujd70kp8E0+29kK1dLW GzA6stZ0bm4XxaaoPCuSlp05PulFnAAfmbin4CNl9qxpSgU+7qK36Q1uVoVce3b+gF+7MpMU6Nu P9UwwjGLtaoArjhv7+bwqLcwxhLxSlNH/whsh1nuVrwTD7opWfXdn6BeXA645yjb67UNviVmg/y SOKa7GOwJVIeCYWqsibQra5vHIsOXCaCDK1qVaUMl2cK0gtQ1VTRnN8l82FFei5AIYBgy8UdJXU nwpXJ2UBCc6sXPpo8imSqHbYkN7SwC4m7CaaNrGUou2JejoOIJN0TNxH2CazPSX8tgN+npCg9KL Je2W1og9qpAu2yhlBvJWYg3ENUBxk6J9yuNehJBDZeughj9qeRBVuk/Y5QAkpw+k60eZZmxIPw3 HxYLFxq79OBC23GrqFrrAYJzw4Eqah61cRES3yv4BI9pSBsanwn4jFXl8whfSk0T2CyX3wGeEGd txtLnu+F9p3pMvYK7E0A4vXiTGN71lNHWDrzdULRVcN4h4vNh+CxYG1NUQ3iNDDD/jeDV0w== X-Received: by 2002:a05:690e:150a:b0:67d:ba55:4be2 with SMTP id 956f58d0204a3-67df448c8acmr18037025d50.84.1796171190196; Tue, 01 Dec 2026 16:26:30 -0800 (PST) Received: from system76-laptop.attlocal.net ([2600:1700:6476:1430:f726:86c4:c7ef:c27]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67df31fa7afsm17055244d50.1.2026.12.01.16.26.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 16:26:29 -0800 (PST) Message-ID: Subject: Re: [PATCH v2] 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: Tue, 01 Dec 2026 16:26:27 -0800 In-Reply-To: <179613469562.187341.18363138064359075616.1796134695.6-1-shawnq1@163.com> References: <20261128082828.3075144-1-shawnq1@163.com> <26bf2beb0e958385812405d43eed469333f31b94.camel@dubeyko.com> <179613469562.187341.18363138064359075616.1796134695.6-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 Tue, 2026-12-01 at 14:18 +0000, Shawn Shao wrote: > Hello Slava, >=20 > Thanks for the review.=C2=A0 Ditto. Please, send it as formal patch. > I moved validation of the B-tree node map chain into > `hfs_btree_open()`, so it runs while the volume is being mounted. The > check > bounds node indices by both the B-tree header's node count and the > number of > complete nodes in the tree file, verifies the header/map node types, > and > rejects out-of-range links and cycles. >=20 > If the map chain is malformed, the filesystem now emits a warning, > recommends > `fsck.hfsplus`, and sets `SB_RDONLY` before returning the tree. The > cycle > fixture completes mounting read-only instead of looping; the normal- > map > control still mounts writable. >=20 > Tested on Linux 7.4-rc4, commit > 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046, tree > 194227446ae61b4971eea1b7bed06856fe3cc477, with gcc 11.4.0 and QEMU > 6.2.0. > The x86-64 bzImage build completed. The reproducer, kernel > configuration, > patch, and fixed-kernel console logs are collected here: > https://gist.github.com/shaos26/9280cbfdae1690b6b20c86bfe0c8db8e >=20 > Changes in v2: > - Validate the B-tree map chain during mount. > - Force a read-only mount and recommend `fsck.hfsplus` on map > corruption. >=20 > Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") > Thanks, > Shawn Shao >=20 > Signed-off-by: Shawn Shao > --- > diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c > index bd4dbdbc8f98c..54d22755707a2 100644 > --- a/fs/hfsplus/btree.c > +++ b/fs/hfsplus/btree.c > @@ -211,6 +211,63 @@ static bool hfs_bmap_test_bit(struct hfs_bnode > *node, u32 node_bit_idx) > =C2=A0 return (byte & mask) !=3D 0; > =C2=A0} > =C2=A0 > +static int hfs_bmap_next_node(struct hfs_btree *tree, u32 index, > + =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 u32 max_nodes, u32 *next) > +{ > + struct hfs_bnode *node; > + int err =3D 0; > + > + if (index >=3D max_nodes) > + return -EIO; > + > + node =3D hfs_bnode_find(tree, index); > + if (IS_ERR(node)) > + return PTR_ERR(node); > + > + if ((index =3D=3D HFSPLUS_TREE_HEAD && node->type !=3D > HFS_NODE_HEADER) || > + =C2=A0=C2=A0=C2=A0 (index !=3D HFSPLUS_TREE_HEAD && node->type !=3D > HFS_NODE_MAP)) { > + err =3D -EIO; > + goto out; > + } > + > + *next =3D node->next; > + if (*next >=3D max_nodes && *next) > + err =3D -EIO; > +out: > + hfs_bnode_put(node); > + return err; > +} >=20 Frankly speaking, it will be good to make a cleanup. We need function(s) of getting next/prev node that can be used in multiple places. Something like this: diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c index ca9813f58a6d..d91fd95f853b 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) int hfs_brec_goto(struct hfs_find_data *fd, int cnt) { - struct hfs_btree *tree; - struct hfs_bnode *bnode; - int idx, res =3D 0; + struct hfs_bnode *bnode, *sibling; + int res =3D 0; u16 off, len, keylen; bnode =3D fd->bnode; - tree =3D bnode->tree; if (cnt < 0) { cnt =3D -cnt; while (cnt > fd->record) { cnt -=3D fd->record + 1; fd->record =3D bnode->num_recs - 1; - idx =3D bnode->prev; - if (!idx) { + sibling =3D hfs_bnode_find_prev(bnode); + if (!sibling) { res =3D -ENOENT; goto out; } - 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); goto out; } + hfs_bnode_put(bnode); + bnode =3D sibling; } fd->record -=3D cnt; } else { while (cnt >=3D bnode->num_recs - fd->record) { cnt -=3D bnode->num_recs - fd->record; fd->record =3D 0; - idx =3D bnode->next; - if (!idx) { + sibling =3D hfs_bnode_find_next(bnode); + if (!sibling) { res =3D -ENOENT; goto out; } - 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); goto out; } + hfs_bnode_put(bnode); + bnode =3D sibling; } fd->record +=3D cnt; } diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c index 3a1a1fa6f2e2..82705b6f5597 100644 --- a/fs/hfsplus/bnode.c +++ b/fs/hfsplus/bnode.c @@ -399,7 +399,7 @@ void hfs_bnode_unlink(struct hfs_bnode *node) tree =3D node->tree; if (node->prev) { - tmp =3D hfs_bnode_find(tree, node->prev); + tmp =3D hfs_bnode_find_prev(node); if (IS_ERR(tmp)) return; tmp->next =3D node->next; @@ -411,7 +411,7 @@ void hfs_bnode_unlink(struct hfs_bnode *node) tree->leaf_head =3D node->next; if (node->next) { - tmp =3D hfs_bnode_find(tree, node->next); + tmp =3D hfs_bnode_find_next(node); if (IS_ERR(tmp)) return; tmp->prev =3D node->prev; @@ -618,6 +618,89 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num) return ERR_PTR(-EIO); } +/* + * Leaf and index nodes are linked in both directions to the nodes of the + * same type and height. The header node and the map nodes form a chain + * through their next links only: hfs_bmap_new_bmap() leaves prev at 0. + */ +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; + + if (!link) + return NULL; + + if (link =3D=3D node->this || link >=3D tree->node_count) + 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 (!next || sibling->type !=3D HFS_NODE_MAP) + 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 || + sibling->height !=3D node->height || + 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 %d) in b-tree %u\n", + next ? "next" : "prev", link, node->this, node->type, + tree->cnid); + return ERR_PTR(-EIO); +} + +/** + * hfs_bnode_find_next - get the node that follows a node at its level + * @node: the node, the caller holds a reference to it + * + * Follow the next link of @node and check that the node found is the one + * that can follow @node: a leaf or index node at the same height that links + * back to @node, or a map node if @node is the header node or a map node. + * + * Return: the next node with a reference held, NULL if @node is the last + * node of its level, or an ERR_PTR() on error. + */ +struct hfs_bnode *hfs_bnode_find_next(struct hfs_bnode *node) +{ + return hfs_bnode_find_sibling(node, true); +} + +/** + * hfs_bnode_find_prev - get the node that precedes a node at its level + * @node: the leaf or index node, the caller holds a reference to it + * + * Follow the prev link of @node and check that the node found is a node + * of the same type and height that links back to @node. + * + * Return: the previous node with a reference held, NULL if @node is the + * first node of its level, or an ERR_PTR() on error. + */ +struct hfs_bnode *hfs_bnode_find_prev(struct hfs_bnode *node) +{ + return hfs_bnode_find_sibling(node, false); +} + void hfs_bnode_free(struct hfs_bnode *node) { int i; diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c index f416e562d439..56b694c2365b 100644 --- a/fs/hfsplus/brec.c +++ b/fs/hfsplus/brec.c @@ -275,11 +275,7 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd) new_node->type =3D node->type; new_node->height =3D node->height; - 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); if (IS_ERR(next_node)) { hfs_bnode_put(node); hfs_bnode_put(new_node); diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c index 0a9b32d5c5bf..9d9181b275e2 100644 --- a/fs/hfsplus/btree.c +++ b/fs/hfsplus/btree.c @@ -593,13 +593,12 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) ctx.len--; } kunmap_local(data); - nidx =3D node->next; - if (!nidx) { + next_node =3D hfs_bnode_find_next(node); + if (!next_node) { hfs_dbg("create new bmap node\n"); next_node =3D hfs_bmap_new_bmap(node, idx); hfs_btree_write(tree); - } else - next_node =3D hfs_bnode_find(tree, nidx); + } hfs_bnode_put(node); if (IS_ERR(next_node)) return next_node; @@ -638,30 +637,20 @@ void hfs_bmap_free(struct hfs_bnode *node) return; } while (nidx >=3D len * 8) { - u32 i; + struct hfs_bnode *next_node; 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", node->this); hfs_bnode_put(node); return; } 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)) return; - } + node =3D next_node; len =3D hfs_brec_lenoff(node, 0, &off); if (hfs_brec_len_invalid(node, len)) { 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 dd8f7179431a..adb0216b7db7 100644 --- a/fs/hfsplus/hfsplus_fs.h +++ b/fs/hfsplus/hfsplus_fs.h @@ -331,6 +331,8 @@ static inline unsigned short hfsplus_min_io_size(struct super_block *sb) #define hfs_bnode_unlink hfsplus_bnode_unlink #define hfs_bnode_findhash hfsplus_bnode_findhash #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 #define hfs_bnode_unhash hfsplus_bnode_unhash #define hfs_bnode_free hfsplus_bnode_free #define hfs_bnode_create hfsplus_bnode_create @@ -415,6 +417,8 @@ void hfs_bnode_unlink(struct hfs_bnode *node); struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid); void hfs_bnode_unhash(struct hfs_bnode *node); struct 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); void hfs_bnode_free(struct hfs_bnode *node); struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num); void hfs_bnode_get(struct hfs_bnode *node); > + > +static int hfs_bmap_check_map_chain(struct hfs_btree *tree) > +{ > + u32 max_nodes, slow =3D HFSPLUS_TREE_HEAD, fast =3D > HFSPLUS_TREE_HEAD; What these fast and slow mean? > + u32 steps; > + int err; > + > + max_nodes =3D min_t(u64, tree->node_count, > + =C2=A0 tree->inode->i_size >> tree- > >node_size_shift); > + if (!max_nodes || tree->node_count > max_nodes) > + return -EIO; > + > + for (steps =3D 0; steps < max_nodes; steps++) { > + err =3D hfs_bmap_next_node(tree, slow, max_nodes, > &slow); > + if (err || !slow) > + return err; > + > + err =3D hfs_bmap_next_node(tree, fast, max_nodes, > &fast); > + if (err || !fast) > + return err; > + err =3D hfs_bmap_next_node(tree, fast, max_nodes, > &fast); > + if (err || !fast) > + return err; Logic looks not clear and I don't quite follow top the logic. Please, rework it to be more simple and clear. > + > + if (slow =3D=3D fast) > + return -EIO; > + } > + > + return -EIO; > +} > + > =C2=A0 > =C2=A0/** > =C2=A0 * hfs_bmap_clear_bit - clear a bit in the b-tree map > @@ -274,6 +331,7 @@ struct hfs_btree *hfs_btree_open(struct > super_block *sb, u32 id) > =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) > @@ -385,7 +443,12 @@ struct hfs_btree *hfs_btree_open(struct > super_block *sb, u32 id) > =C2=A0 if (IS_ERR(node)) > =C2=A0 goto free_inode; > =C2=A0 > - if (!hfs_bmap_test_bit(node, 0)) { > + err =3D hfs_bmap_check_map_chain(tree); > + if (err =3D=3D -ENOMEM) { > + hfs_bnode_put(node); > + goto free_inode; > + } > + if (err || !hfs_bmap_test_bit(node, 0)) { I think we already need a dedicated function for all corruption check. Please, collect all checks there. Thanks, Slava. > =C2=A0 pr_warn("(%s): %s (cnid 0x%x) map record invalid or > bitmap corruption detected, forcing read-only.\n", > =C2=A0 sb->s_id, hfs_btree_name(id), id); > =C2=A0 pr_warn("Run fsck.hfsplus to repair.\n");