From 2467d2e4861d8cb6c7251d934fbb309a212aa157 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 18 Jun 2023 02:37:22 -0500 Subject: [PATCH] Added a separate tag encoding for the mtree This helps with debugging and can avoid weird issues if a file btree ever accidentally ends up attached to id -1 (due to fs bug). Though a separate encoding isn't strictly necessary, maybe this should be reverted at some point. --- lfs.c | 24 +++++++++++++++++++----- scripts/dbgbtree.py | 6 ++++-- scripts/dbgmtree.py | 36 +++++++++++++++++++++--------------- scripts/dbgrbyd.py | 6 ++++-- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/lfs.c b/lfs.c index cce1e705..7e7f2822 100644 --- a/lfs.c +++ b/lfs.c @@ -590,7 +590,6 @@ enum lfsr_tag_type { LFSR_TAG_SUPERMAGIC = 0x0003, LFSR_TAG_SUPERCONFIG = 0x0004, - LFSR_TAG_MROOT = 0x0304, LFSR_TAG_NAME = 0x0100, LFSR_TAG_BRANCH = 0x0100, @@ -603,8 +602,11 @@ enum lfsr_tag_type { LFSR_TAG_BLOCK = 0x0302, LFSR_TAG_BTREE = 0x0303, LFSR_TAG_RMBTREE = 0x1303, + LFSR_TAG_MROOT = 0x0304, LFSR_TAG_MDIR = 0x0305, LFSR_TAG_RMMDIR = 0x1305, + LFSR_TAG_MTREE = 0x0306, + LFSR_TAG_RMMTREE = 0x1306, LFSR_TAG_UATTR = 0x0400, LFSR_TAG_GROWUATTR = 0x2400, // test only? TODO @@ -5111,6 +5113,11 @@ static int lfsr_mdir_commit(lfs_t *lfs, lfsr_mdir_t *mdir, lfs_ssize_t *rid, if (d < 0) { return d; } + + // TODO something better than this + if (tag == LFSR_TAG_BTREE) { + tag = LFSR_TAG_MTREE; + } } // TODO yeah we're going to need a wide-rm @@ -5118,7 +5125,7 @@ static int lfsr_mdir_commit(lfs_t *lfs, lfsr_mdir_t *mdir, lfs_ssize_t *rid, mdir, attrs, attr_count, LFSR_ATTRS( LFSR_ATTR(-1, RMMDIR, 0, NULL, 0), - LFSR_ATTR(-1, RMBTREE, 0, NULL, 0), + LFSR_ATTR(-1, RMMTREE, 0, NULL, 0), (!lfsr_btree_isnull(&mtree_) ? LFSR_ATTR_(-1, tag, 0, buf, d) : LFSR_ATTR_NOOP))); @@ -5326,12 +5333,17 @@ static int lfsr_mdir_commit(lfs_t *lfs, lfsr_mdir_t *mdir, lfs_ssize_t *rid, if (d < 0) { return d; } + + // TODO something better than this + if (tag == LFSR_TAG_BTREE) { + tag = LFSR_TAG_MTREE; + } } // TODO yeah we're going to need a wide-rm err = lfsr_mdir_commit_(lfs, &mroot_, NULL, NULL, LFSR_ATTRS( LFSR_ATTR(-1, RMMDIR, 0, NULL, 0), - LFSR_ATTR(-1, RMBTREE, 0, NULL, 0), + LFSR_ATTR(-1, RMMTREE, 0, NULL, 0), (!lfsr_btree_isnull(&mtree_) ? LFSR_ATTR_(-1, tag, 0, buf, d) : LFSR_ATTR_NOOP))); @@ -5609,13 +5621,15 @@ static int lfsr_mtree_traversal_next(lfs_t *lfs, if (err != LFS_ERR_NOENT && rid == -1 && lfsr_tag_suptype(tag) == LFSR_TAG_STRUCT) { - if (tag != LFSR_TAG_MDIR && tag != LFSR_TAG_BTREE) { + if (tag != LFSR_TAG_MDIR && tag != LFSR_TAG_MTREE) { LFS_ERROR("Weird mstruct? (0x%"PRIx32")", tag); return LFS_ERR_CORRUPT; } lfs_ssize_t d = lfsr_btree_fromdisk(lfs, &lfs->mtree, - tag, 1, data); + // TODO something better than this + (tag == LFSR_TAG_MTREE ? LFSR_TAG_BTREE : tag), + 1, data); if (d < 0) { return d; } diff --git a/scripts/dbgbtree.py b/scripts/dbgbtree.py index e50cddaf..6c4e6801 100755 --- a/scripts/dbgbtree.py +++ b/scripts/dbgbtree.py @@ -11,7 +11,6 @@ import struct TAG_NULL = 0x0000 TAG_SUPERMAGIC = 0x0003 TAG_SUPERCONFIG = 0x0004 -TAG_MROOT = 0x0304 TAG_NAME = 0x0100 TAG_BRANCH = 0x0100 TAG_REG = 0x0101 @@ -20,7 +19,9 @@ TAG_STRUCT = 0x0300 TAG_INLINED = 0x0300 TAG_BLOCK = 0x0302 TAG_BTREE = 0x0303 +TAG_MROOT = 0x0304 TAG_MDIR = 0x0305 +TAG_MTREE = 0x0306 TAG_UATTR = 0x0400 TAG_ALT = 0x4000 TAG_ALTA = 0x6000 @@ -136,8 +137,9 @@ def tagrepr(tag, w, size, off=None): 'inlined' if tag == TAG_INLINED else 'block' if tag == TAG_BLOCK else 'btree' if tag == TAG_BTREE - else 'mdir' if tag == TAG_MROOT + else 'mroot' if tag == TAG_MROOT else 'mdir' if tag == TAG_MDIR + else 'mtree' if tag == TAG_MTREE else 'struct 0x%02x' % (tag & 0xff), ' w%d' % w if w else '', size) diff --git a/scripts/dbgmtree.py b/scripts/dbgmtree.py index 79ce3868..86eb416c 100755 --- a/scripts/dbgmtree.py +++ b/scripts/dbgmtree.py @@ -11,7 +11,6 @@ import struct TAG_NULL = 0x0000 TAG_SUPERMAGIC = 0x0003 TAG_SUPERCONFIG = 0x0004 -TAG_MROOT = 0x0304 TAG_NAME = 0x0100 TAG_BRANCH = 0x0100 TAG_REG = 0x0101 @@ -20,7 +19,9 @@ TAG_STRUCT = 0x0300 TAG_INLINED = 0x0300 TAG_BLOCK = 0x0302 TAG_BTREE = 0x0303 +TAG_MROOT = 0x0304 TAG_MDIR = 0x0305 +TAG_MTREE = 0x0306 TAG_UATTR = 0x0400 TAG_ALT = 0x4000 TAG_ALTA = 0x6000 @@ -145,8 +146,9 @@ def tagrepr(tag, w, size, off=None): 'inlined' if tag == TAG_INLINED else 'block' if tag == TAG_BLOCK else 'btree' if tag == TAG_BTREE - else 'mdir' if tag == TAG_MROOT + else 'mroot' if tag == TAG_MROOT else 'mdir' if tag == TAG_MDIR + else 'mtree' if tag == TAG_MTREE else 'struct 0x%02x' % (tag & 0xff), ' w%d' % w if w else '', size) @@ -785,8 +787,8 @@ def main(disk, mroots=None, *, # fetch the actual mtree, if there is one mtree = None if not args.get('depth') or mdepth < args.get('depth'): - done, rid, tag, w, j, d, data, _ = mroot.lookup(-1, TAG_BTREE) - if not done and rid == -1 and tag == TAG_BTREE: + done, rid, tag, w, j, d, data, _ = mroot.lookup(-1, TAG_MTREE) + if not done and rid == -1 and tag == TAG_MTREE: w, trunk, block, crc = frombtree(data) mtree = Rbyd.fetch(f, block_size, block, trunk) @@ -796,7 +798,8 @@ def main(disk, mroots=None, *, mid = -1 while True: done, mid, w, rbyd, rid, tags, path = mtree.btree_lookup( - f, block_size, mid+1, depth=args.get('depth')-mdepth) + f, block_size, mid+1, + depth=args.get('depth', mdepth)-mdepth) if done: break @@ -915,7 +918,7 @@ def main(disk, mroots=None, *, if mtree: tree_, tdepth = mtree.btree_tree( f, block_size, - depth=args.get('depth')-mdepth, + depth=args.get('depth', mdepth)-mdepth, inner=args.get('inner')) # connect a branch to the root of the tree @@ -923,7 +926,7 @@ def main(disk, mroots=None, *, if root: r_bid, r_bd, r_rid, r_tag = root.a tree.add(TBranch( - a=(-1, d, 0, -1, TAG_BTREE), + a=(-1, d, 0, -1, TAG_MTREE), b=(r_bid, r_bd, r_rid, 0, r_tag), d=d_-1, c='b', @@ -945,7 +948,8 @@ def main(disk, mroots=None, *, mid = -1 while True: done, mid, w, rbyd, rid, tags, path = mtree.btree_lookup( - f, block_size, mid+1, depth=args.get('depth')-mdepth) + f, block_size, mid+1, + depth=args.get('depth', mdepth)-mdepth) if done: break @@ -974,7 +978,8 @@ def main(disk, mroots=None, *, mid = -1 while True: done, mid, w, rbyd, rid, tags, path = mtree.btree_lookup( - f, block_size, mid+1, depth=args.get('depth')-mdepth) + f, block_size, mid+1, + depth=args.get('depth', mdepth)-mdepth) if done: break @@ -1132,7 +1137,7 @@ def main(disk, mroots=None, *, if mtree: tree_, tdepth = mtree.btree_btree( f, block_size, - depth=args.get('depth')-mdepth, + depth=args.get('depth', mdepth)-mdepth, inner=args.get('inner')) # connect a branch to the root of the tree @@ -1140,7 +1145,7 @@ def main(disk, mroots=None, *, if root: r_bid, r_bd, r_rid, r_tag = root.a tree.add(TBranch( - a=(-1, d, 0, -1, TAG_BTREE), + a=(-1, d, 0, -1, TAG_MTREE), b=(r_bid, r_bd, r_rid, 0, r_tag), d=0, c='b', @@ -1164,7 +1169,7 @@ def main(disk, mroots=None, *, done, mid, w, rbyd, rid, tags, path = ( mtree.btree_lookup( f, block_size, mid+1, - depth=args.get('depth')-mdepth)) + depth=args.get('depth', mdepth)-mdepth)) if done: break @@ -1459,8 +1464,8 @@ def main(disk, mroots=None, *, # fetch the actual mtree, if there is one if not args.get('depth') or mdepth < args.get('depth'): - done, rid, tag, w, j, d, data, _ = mroot.lookup(-1, TAG_BTREE) - if not done and rid == -1 and tag == TAG_BTREE: + done, rid, tag, w, j, d, data, _ = mroot.lookup(-1, TAG_MTREE) + if not done and rid == -1 and tag == TAG_MTREE: w, trunk, block, crc = frombtree(data) mtree = Rbyd.fetch(f, block_size, block, trunk) @@ -1468,7 +1473,8 @@ def main(disk, mroots=None, *, mid = -1 while True: done, mid, w, rbyd, rid, tags, path = mtree.btree_lookup( - f, block_size, mid+1, depth=args.get('depth')-mdepth) + f, block_size, mid+1, + depth=args.get('depth', mdepth)-mdepth) if done: break diff --git a/scripts/dbgrbyd.py b/scripts/dbgrbyd.py index 65f0181b..67978faa 100755 --- a/scripts/dbgrbyd.py +++ b/scripts/dbgrbyd.py @@ -20,7 +20,6 @@ COLORS = [ TAG_NULL = 0x0000 TAG_SUPERMAGIC = 0x0003 TAG_SUPERCONFIG = 0x0004 -TAG_MROOT = 0x0304 TAG_NAME = 0x0100 TAG_BRANCH = 0x0100 TAG_REG = 0x0101 @@ -29,7 +28,9 @@ TAG_STRUCT = 0x0300 TAG_INLINED = 0x0300 TAG_BLOCK = 0x0302 TAG_BTREE = 0x0303 +TAG_MROOT = 0x0304 TAG_MDIR = 0x0305 +TAG_MTREE = 0x0306 TAG_UATTR = 0x0400 TAG_ALT = 0x4000 TAG_ALTA = 0x6000 @@ -138,8 +139,9 @@ def tagrepr(tag, w, size, off=None): 'inlined' if tag == TAG_INLINED else 'block' if tag == TAG_BLOCK else 'btree' if tag == TAG_BTREE - else 'mdir' if tag == TAG_MROOT + else 'mroot' if tag == TAG_MROOT else 'mdir' if tag == TAG_MDIR + else 'mtree' if tag == TAG_MTREE else 'struct 0x%02x' % (tag & 0xff), ' w%d' % w if w else '', size)