Re: [PATCH v2] hfs: avoid reusing occupied CNIDs after deletion

From: Viacheslav Dubeyko

Date: Mon Nov 23 2026 - 17:18:17 EST


On Sat, 2026-11-21 at 05:19 -0500, Hengyu Liang wrote:
> Hi Slava,
>
> Thanks for the review. OK to all remarks about empty lines. The other
> points are below; there is one I would like to check with you before
> v3 (the flag).
>
> On Mon, 2026-11-16 at 17:02 -0800, Viacheslav Dubeyko wrote:
> > > +static u32 hfs_brec_cnid(struct hfs_bnode *node, u32 off)
> [...]
> > This function is completely not clear. You try to read the
> > hfs_cat_rec. Simply read it and analyze the record. Now it looks
> > like a
> > dark magic.
>
> OK.
>
> > Is this insert argument used anywhere?
>
> Yes, in the loop below: an added CNID can only raise the maximum, a
> removed one can only lower it.
>
> > > + if (tree->cnid != HFS_CAT_CNID || node->type !=
> > > HFS_NODE_LEAF)
> >
> > I would like to see some inline function with good name for this
> > check.
>
> OK.
>
> > > + if (!cnid)
> >
> > The #0 is reserved CNID. It's not good choice for such check.
>
> OK.
>
> > I assume that we limited by tree's height here. Could we used it
> > instead of infinite loop?
>
> OK.
>
> > I dislike insert like a boolean value. Why do we need to inform
> > this
> > function about insert? Why this function cannot make this decision
> > alone?
>
> OK. In v3 it will run after the record is added or removed, recompute
> the leaf's maximum, and compare it with the old value, so it no
> longer
> needs to know whether a record was added or removed.
>
> > You don't need in done if you are limited by b-tree height.
>
> OK. The loop will still stop early when a node does not change,
> because
> then the nodes above it do not change either.
>
> On Tue, 2026-11-17 at 16:20 -0800, Viacheslav Dubeyko wrote:
> > > + * of an index node. If a child has no max_cnid yet, return the
> > > number of
> > > + * that child in @child instead.
> >
> > Do you mean that we return CNID? I don't quite follow. We always
> > have
> > any CNID in the node that will be maximum one for node. How is it
> > possible "If a child has no max_cnid yet, return the number of that
> > child in @child instead."?
>
> No, @child is a node number, not a CNID, and this only happens for
> index nodes. An index record holds a key and the child's node number,
> but no file or folder CNID, so an index node's max_cnid is the
> largest
> max_cnid among its children. If a child has no cached max_cnid yet
> (for example, it has just been read from disk), the function returns
> that
> child's node number; hfs_btree_max_cnid() computes the child first
> and
> then comes back to the parent. I will make the name and the comment
> clearer.
>
> > If it is the index node, then we don't need to traverse the items.
> > No?
>
> We do need to: each record in an index node points to a child node,
> so the loop goes over those records to find the largest max_cnid
> among
> the children.
>
> > It looks like internal error but not I/O error.
>
> OK, I will return -EFSCORRUPTED.
>
> > The max CNID cannot be lower that HFS_FIRSTUSER_CNID.
> [...]
> > I still don't see a necessity in HFS_BNODE_MAX_CNID flag.
>
> These two are related, so let me answer them together.
>
> max_cnid is not stored on disk, so a node that hfs_bnode_find() has
> just read has no max_cnid yet. For a leaf it could be computed right
> away, but
> an index node needs all nodes below it, so computing it at read time
> would read the whole catalog. That is why the code needs a "not
> computed yet" state.
>
> I can drop the flag and use max_cnid == 0 to mean "not computed yet",
> by starting the computation of each maximum from HFS_FIRSTUSER_CNID -
> 1
> (15) instead of 0, so a computed value is never 0. It has to be 15,
> not
> HFS_FIRSTUSER_CNID: a node without user files or folders (only the
> root
> folder, CNID 2, or only thread records) then gets 15, and on a volume
> without user files the root gets 15, so next_id stays 16 (15 + 1).
> Starting from 16 would give 17 there. Would this work for you?

Yes, I think that "max_cnid == 0" looks better. And we can introduce a
constant HFS_INVALID_CNID == 0 (or something like this).

>
> > We need to have comments at one place for the whole structure. Here
> > it
> > looks not good at all.
>
> OK.
>
> > I still don't see the point why should we change anything here?
>
> Changing `<` to `!=` does not change the result:
> hfs_set_next_unused_CNID() updates next_id with cmpxchg only if
> next_id is still cnid + 1. The `!=` check only skips computing the
> maximum when cnid + 1 > next_id, where nothing would be updated
> anyway. I will drop this change.
>
> > The max_cnid or max_cnid + 1?
>
> max_cnid. hfs_set_next_unused_CNID() adds 1 itself (and uses
> HFS_FIRSTUSER_CNID for anything lower), as the old code expected.
>
>

OK. Let me take another look and double check it when you'll send the
v3 of the patch.

Thanks,
Slava.