Skip to content

Hdd fixes - #976

Open
AKuHAK wants to merge 5 commits into
ps2dev:masterfrom
AKuHAK:hdd-fixes
Open

AKuHAK wants to merge 5 commits into
ps2dev:masterfrom
AKuHAK:hdd-fixes

Conversation

@AKuHAK

@AKuHAK AKuHAK commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

This PR fixes bugs in the HDD modules that I found while testing PFS volumes that have sub-partitions (partitions grown with HIOCADDSUB). The biggest one is in fsck: it reported every such volume as damaged, and its repair then damaged the volume for real.

fsck: check the bitmaps of sub-partitions at the right place

  • pfsBitmapReadPartition() added the 4MB APA area of the main partition to the bitmap address of every subpart. A sub-partition's bitmap starts at its zone 1, so for subparts >= 1 fsck read, and could "fix", blocks that are not bitmap ("not marked as used").
  • fsckCompareBitmap() looked up subpart i in the computed bitmap at zone offset i instead of ZoneMap[i], so subparts >= 1 were compared with the wrong part of it ("bitmap unmatch"), and Replace wrote that wrong part back.
  • ZoneSizes/ZoneMap are now cleared completely, not just their first 16 bytes.

fsck: fix "Remove rest of file"

  • fsckFillInode() wrote the size of the remaining data zones into number_segdesg, which is overwritten two lines later, instead of limiting the file size. The cut file kept a size beyond its zones and could not be read. The size is now limited to the data zones that are left.
  • When the next SEGI could not be read, fsckFillInode() was called with NULL, and the previous descriptor had already been released. fsck now keeps the previous descriptor and cuts the file there.
  • fsckCheckZones() marked zones while it was still looking for an overlap. When an extent overlapped, its first zones stayed marked even though the file is cut before that extent, so they stayed allocated without an owner. It now checks the whole extent first and marks it afterwards. The bitmap buffer is also released on that early return.

libpfs: mark all reserved zones in pfsFormatSub()

Only the first bitmap sector (4096 zones) got the reserved bits. With more reserved zones (small zones on a large main partition), the rest stayed free, and with more than 8192 the loop wrote past the 1KB buffer. Each bitmap sector is now filled and marked separately. The output is the same when 4096 or fewer zones are reserved.

apa: no endless loop in hddAddPartitionHere()

If the end of the partition list is not a multiple of the smallest partition size (lists made by other tools), it can never be aligned and the loop never ends. The function now returns -EINVAL.

libhdd: out-of-bounds reads in hddMakeFilesystem() / hddExpandFilesystem()

  • sizesMB[-1] was read when no size fitted, and after -ENOSPC.
  • maxIndex became 9 when the driver's maximum size was not in the list. It now takes the largest listed size not above the maximum.
  • A size of 0 MB went below index 0.
  • An unexpected HIOCADDSUB error was counted as an added sub-partition. It now stops instead.

Testing

  • fsck.irx, ps2hdd.irx, ps2fs.irx and libhdd.a build with the current toolchain.
  • fsck built for the host (the same way pfsshell builds the IOP sources), run against volumes made by pfsshell mkpart with sub-partitions and with files written by pfsshell:
    • before: "NOT CLEAN" right after the format;
    • after: clean.
  • With leaked or unmarked zones placed in a sub-partition, the fixed fsck reports "bitmap unmatch" for that sub-partition.
  • Volumes without sub-partitions give the same results as before.

🤖 Generated with Claude Code

AKuHAK and others added 5 commits November 21, 2026 16:38
A PFS volume with sub-partitions (a partition grown with HIOCADDSUB) was
always reported as damaged, even right after pfsFormat, and repairing it
damaged it for real:

- pfsBitmapReadPartition() added the 4MB APA area of the main partition
  to the bitmap address of every subpart. A sub-partition's bitmap starts
  at its zone 1, so for subparts >= 1 fsck read and changed blocks that
  are not bitmap ("not marked as used").
- fsckCompareBitmap() looked up subpart i in the computed bitmap at zone
  offset i instead of ZoneMap[i], so subparts >= 1 were compared with the
  wrong part of it ("bitmap unmatch"), and Replace wrote that part back.

Also clear the whole ZoneSizes/ZoneMap arrays instead of 16 bytes.

Tested with a host build of fsck (as in pfsshell) on volumes made by
pfsshell mkpart with sub-partitions: before, NOT CLEAN right after the
format; now clean, and leaked or unmarked zones in a sub-partition are
found and fixed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- fsckFillInode() stored the size of the remaining data zones into
  number_segdesg (overwritten two lines later) instead of limiting the
  file size. The cut file kept its old size, beyond its zones, and could
  not be read. The size is now limited to the data zones that are left.
- When the next SEGI could not be read, fsckFillInode() was called with
  NULL (which crashed before a NULL check was added, and since then did
  nothing), and the cache entry of the previous descriptor had already
  been released. Keep the previous descriptor and cut the file there.
- fsckCheckZones() marked zones in the computed bitmap while looking for
  an overlap. When an extent overlapped, its first zones stayed marked
  although the file is cut before that extent, so they stayed allocated
  without an owner. Check the whole extent first, then mark it. Also
  release the bitmap buffer on that early return.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reserved zones (APA area, superblock, bitmap, log, root) were marked
in one pass over the cache buffer, but only its first 512 bytes (4096
zones) were written as the first bitmap sector; the following sectors
were refilled with the fragment pattern. With more than 4096 reserved
zones (small zones on a large main partition) the rest stayed free in
the bitmap, and with more than 8192 the loop wrote past the 1KB buffer.

Fill and mark every bitmap sector separately. The output is unchanged
when 4096 or fewer zones are reserved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the end of the partition list is not a multiple of the smallest
partition size (lists made by other tools), no free block can align it
and the outer while loop never ends. Return -EINVAL instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The main partition loop read sizesMB[-1] in its increment step when no
  size fitted, and the sub-partition loops read sizesMB[-1] before
  checking useIndex < 0 after -ENOSPC.
- maxIndex became 9 (past the array) when the driver's maximum partition
  size was not in the list; take the largest listed size not above it.
- A size of 0 MB passed the "% 128" check and walked below index 0.
- An unexpected error from HIOCADDSUB was counted as an added
  sub-partition; stop instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant