fix(libSceSaveData.native): count PS5 save data in 64 KiB blocks, at most 16384 #7960

Merged
Lindamea1 merged 1 commit from upstream-pr/7960 into main 2026-10-20 15:08:03 +00:00
Lindamea1 commented 2026-10-19 16:06:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lindamea1 on 2026-10-19T16:06:28Z.


What

libSceSaveData.native counts save data in PS4 units. SAVE_DATA_BLOCK_BYTES is 32768 and SAVE_DATA_BLOCKS_MAX is 32768: the PS4's 32 KiB block and the top of its 96 to 32768-block range (shadPS4 savedata.cpp). SharpEmu uses a 64 KiB block, and SharpEmu and KytyPS5 allow 48 to 16384 blocks per save, which at 64 KiB a block is 3 MiB to 1 GiB, the PS4's byte range:

On main, the sceSaveDataDirNameSearch infos and sceSaveDataGetMountInfo therefore count up to twice the 64 KiB blocks a save's files use. A save without a recorded size reports 32768 blocks, which is 2 GiB at 64 KiB a block. A scratch program run against main 5eba12d6b creates a save with sceSaveDataMount3 (blocks 48, mode 0x21), writes a 1,000,000-byte file, unmounts and searches. The search returns blocks=48 free=17, so 31 blocks are used. With 64 KiB blocks the file takes 16 and leaves 32 free.

Titles read these counts as 64 KiB blocks:

  • Monster Hunter Wilds (PPSA24191) sizes its save mounts as their byte size rounded up to 64 KiB. For each save the search returns, it converts blocks - free_blocks and free_blocks back to bytes at 65536 bytes a block. For the save above, that gives 2,031,616 used and 1,114,112 free bytes on main, and 1,048,576 and 2,097,152 with this change. What the title does with the two byte counts was not traced. It still shows only a flat grey screen (#7852), so no visible effect is known.
  • Minecraft Bedrock (PPSA17221, #5744) takes the search's blocks as 64 KiB each. #5173 read this out of the title with gdb: five saves at the 32768-block maximum summed to 10 GiB against a 9 GiB budget.

This change:

  • Sets SAVE_DATA_BLOCK_BYTES to 65536 and SAVE_DATA_BLOCKS_MAX to 16384. Each file is rounded up to 64 KiB blocks, and a save without a recorded size reports 16384.
  • A blocks above 16384 is now kept as 16384 (the existing clamp was at 32768), as SharpEmu clamps it. A save already recorded above 16384 now reports 16384.
  • Moves the block accounting out of the sceSaveDataDirNameSearch debt entry into its own entry for sceSaveDataDirNameSearch, sceSaveDataGetMountInfo and sceSaveDataMount3, which states the gaps first. Both entries stay under 1000 characters.

Not measured on a console. The console's file system overhead is not counted. A blocks above 16384 is clamped to 16384 and a non-zero one below 48 is kept as given, where KytyPS5 rejects such a creating mount; what the console does is unknown.

Interplay with #7780 (open): it builds libSceSaveData, which PS4 titles import, from these sources. With both merged, that module would also count 64 KiB blocks up to 16384, while PS4 saves use 32 KiB blocks up to 32768 (shadPS4 above; SD_BLOCKS_MAX is 32768 in the SaveDataCore.hpp that #7780 removes). On main only libSceSaveData.native is built from these sources, so the values stay constants in the module. Whichever of the two merges second can give the PS4 target its own values, for example as compile definitions set in #7780's SaveDataLibrary.cmake. #6977 (open) adds a PS4 sceSaveDataMount to this module itself, and it passes the title's blocks, counted in 32 KiB, to sceSaveDataMount3 unchanged. With this change those would be read as 64 KiB blocks.

Tested

Gentoo Linux, kernel 7.2.8, GCC 16.2.1, glibc 2.43, Ninja, Release, BUILD_TESTING=ON, on main d047690c6.

  • guest_savedata_search (core/libs/tests/GuestSaveDataSearch.cpp) now checks:
    • A save without a recorded size reports 16384 blocks, all free (was 32768).
    • The 96-block save with a 40,000-byte file reports 95 free blocks, both in the search infos and in sceSaveDataGetMountInfo (was 94).
    • New: a 48-block save with a 70,000-byte file reports 46 free. That is 2 blocks at 64 KiB, where 32 KiB gives 3.
    • New: a save created with 32768 blocks reports 16384.
    • The mount-info lines of the existing check move into a MountInfo helper that the new checks share.
  • Negative control, with the library rebuilt each time:
    • With Export.cpp and SaveData.hpp from main, the test fails with search info must report the save's blocks and free blocks. In a scratch copy of the test, Require reports and continues. That copy shows each of the 5 changed or new assertions failing on the main sources and every other assertion passing.
    • With only Export.cpp from main, the test fails with a save must report the blocks it was created with, less those its files take. The mount-info and 64 KiB checks fail too.
    • With only SaveData.hpp from main, it fails at the unsized-save check, and the 16384 check fails too.
    • With this change, every assertion passes.
  • The scratch program from What reports blocks=48 free=32.
  • Tests the change reaches, selected by tools/select_tests.py as CI selects them: 219 passed, 0 failed, 0 skipped. They include the six guest_savedata_* tests of libSceSaveData.native, the four savedata_memory_* tests, savedata_native_write_replacement and patched_libraries.
  • tools/check_compile.py --base upstream/main: 2 translation units compile.
  • Windows: Export.cpp and GuestSaveDataSearch.cpp compile without warnings with x86_64-w64-mingw32-g++ 16.2.0 -fsyntax-only -Wall -Wextra, as on main. This is a syntax check, not a Windows build or run.
  • tools/check_conventions.py --base upstream/main with this title and body: all checks passed.
  • tools/pr_overlap.py --branch reports no overlapping lines. It could not compare #6044, #6879 and #6977, which conflict with main; merged with this branch, #6879 and #6977 conflict in the same files as they do with main, and #7780 merges cleanly.

Not run on Windows, macOS or a console.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none (see the #7780 note above)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4923832017 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7960). Originally posted by [Lindamea1](https://github.com/Lindamea1) on 2026-10-19T16:06:28Z. --- ### What `libSceSaveData.native` counts save data in PS4 units. `SAVE_DATA_BLOCK_BYTES` is 32768 and `SAVE_DATA_BLOCKS_MAX` is 32768: the PS4's 32 KiB block and the top of its 96 to 32768-block range ([shadPS4 `savedata.cpp`](https://github.com/shadps4-emu/shadPS4/blob/ec334904604ba41f5d4753f4a2498ec82540e5d6/src/core/libraries/save_data/savedata.cpp#L48-L50)). SharpEmu uses a 64 KiB block, and SharpEmu and KytyPS5 allow 48 to 16384 blocks per save, which at 64 KiB a block is 3 MiB to 1 GiB, the PS4's byte range: - [SharpEmu `SaveDataExports.cs`](https://github.com/sharpemu/sharpemu/blob/c6d304cc3328bad62cb671327ace181110cf2ddb/src/SharpEmu.Libs/SaveData/SaveDataExports.cs#L82-L84) sets 65536, 16384 and 48, and [counts used blocks in 64 KiB](https://github.com/sharpemu/sharpemu/blob/c6d304cc3328bad62cb671327ace181110cf2ddb/src/SharpEmu.Libs/SaveData/SaveDataExports.cs#L1181-L1192). It rounds [the save's total size](https://github.com/sharpemu/sharpemu/blob/c6d304cc3328bad62cb671327ace181110cf2ddb/src/SharpEmu.Libs/SaveData/SaveDataExports.cs#L1194-L1203) once, while this module rounds each file. - [KytyPS5 `libSaveData.cpp`](https://github.com/KytyPS5/KytyPS5/blob/569ec04d1a77127c601eed522ad5bba187173f9d/src/libs/libSaveData.cpp#L30-L31) sets 48 and 16384, and its `sceSaveDataMount3` [rejects a creating mount outside that range](https://github.com/KytyPS5/KytyPS5/blob/569ec04d1a77127c601eed522ad5bba187173f9d/src/libs/libSaveData.cpp#L703-L705). It has no block size and [reports all of a save's blocks as free](https://github.com/KytyPS5/KytyPS5/blob/569ec04d1a77127c601eed522ad5bba187173f9d/src/libs/libSaveData.cpp#L644-L645), so the 64 KiB block comes from SharpEmu alone. On `main`, the `sceSaveDataDirNameSearch` infos and `sceSaveDataGetMountInfo` therefore count up to twice the 64 KiB blocks a save's files use. A save without a recorded size reports 32768 blocks, which is 2 GiB at 64 KiB a block. A scratch program run against `main` 5eba12d6b creates a save with `sceSaveDataMount3` (`blocks` 48, mode `0x21`), writes a 1,000,000-byte file, unmounts and searches. The search returns `blocks=48 free=17`, so 31 blocks are used. With 64 KiB blocks the file takes 16 and leaves 32 free. Titles read these counts as 64 KiB blocks: - Monster Hunter Wilds (PPSA24191) sizes its save mounts as their byte size rounded up to 64 KiB. For each save the search returns, it converts `blocks - free_blocks` and `free_blocks` back to bytes at 65536 bytes a block. For the save above, that gives 2,031,616 used and 1,114,112 free bytes on `main`, and 1,048,576 and 2,097,152 with this change. What the title does with the two byte counts was not traced. It still shows only a flat grey screen (#7852), so no visible effect is known. - Minecraft Bedrock (PPSA17221, #5744) takes the search's `blocks` as 64 KiB each. #5173 read this out of the title with gdb: five saves at the 32768-block maximum summed to 10 GiB against a 9 GiB budget. This change: - Sets `SAVE_DATA_BLOCK_BYTES` to 65536 and `SAVE_DATA_BLOCKS_MAX` to 16384. Each file is rounded up to 64 KiB blocks, and a save without a recorded size reports 16384. - A `blocks` above 16384 is now kept as 16384 (the existing clamp was at 32768), as [SharpEmu clamps it](https://github.com/sharpemu/sharpemu/blob/c6d304cc3328bad62cb671327ace181110cf2ddb/src/SharpEmu.Libs/SaveData/SaveDataExports.cs#L1168-L1178). A save already recorded above 16384 now reports 16384. - Moves the block accounting out of the `sceSaveDataDirNameSearch` debt entry into its own entry for `sceSaveDataDirNameSearch`, `sceSaveDataGetMountInfo` and `sceSaveDataMount3`, which states the gaps first. Both entries stay under 1000 characters. Not measured on a console. The console's file system overhead is not counted. A `blocks` above 16384 is clamped to 16384 and a non-zero one below 48 is kept as given, where KytyPS5 rejects such a creating mount; what the console does is unknown. Interplay with #7780 (open): it builds `libSceSaveData`, which PS4 titles import, from these sources. With both merged, that module would also count 64 KiB blocks up to 16384, while PS4 saves use 32 KiB blocks up to 32768 (shadPS4 above; `SD_BLOCKS_MAX` is 32768 in the `SaveDataCore.hpp` that #7780 removes). On `main` only `libSceSaveData.native` is built from these sources, so the values stay constants in the module. Whichever of the two merges second can give the PS4 target its own values, for example as compile definitions set in #7780's `SaveDataLibrary.cmake`. #6977 (open) adds a PS4 `sceSaveDataMount` to this module itself, and it passes the title's `blocks`, counted in 32 KiB, to `sceSaveDataMount3` unchanged. With this change those would be read as 64 KiB blocks. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Gentoo Linux, kernel 7.2.8, GCC 16.2.1, glibc 2.43, Ninja, Release, `BUILD_TESTING=ON`, on `main` d047690c6. - `guest_savedata_search` (`core/libs/tests/GuestSaveDataSearch.cpp`) now checks: - A save without a recorded size reports 16384 blocks, all free (was 32768). - The 96-block save with a 40,000-byte file reports 95 free blocks, both in the search infos and in `sceSaveDataGetMountInfo` (was 94). - New: a 48-block save with a 70,000-byte file reports 46 free. That is 2 blocks at 64 KiB, where 32 KiB gives 3. - New: a save created with 32768 blocks reports 16384. - The mount-info lines of the existing check move into a `MountInfo` helper that the new checks share. - Negative control, with the library rebuilt each time: - With `Export.cpp` and `SaveData.hpp` from `main`, the test fails with `search info must report the save's blocks and free blocks`. In a scratch copy of the test, `Require` reports and continues. That copy shows each of the 5 changed or new assertions failing on the `main` sources and every other assertion passing. - With only `Export.cpp` from `main`, the test fails with `a save must report the blocks it was created with, less those its files take`. The mount-info and 64 KiB checks fail too. - With only `SaveData.hpp` from `main`, it fails at the unsized-save check, and the 16384 check fails too. - With this change, every assertion passes. - The scratch program from What reports `blocks=48 free=32`. - Tests the change reaches, selected by `tools/select_tests.py` as CI selects them: 219 passed, 0 failed, 0 skipped. They include the six `guest_savedata_*` tests of `libSceSaveData.native`, the four `savedata_memory_*` tests, `savedata_native_write_replacement` and `patched_libraries`. - `tools/check_compile.py --base upstream/main`: 2 translation units compile. - Windows: `Export.cpp` and `GuestSaveDataSearch.cpp` compile without warnings with `x86_64-w64-mingw32-g++` 16.2.0 `-fsyntax-only -Wall -Wextra`, as on `main`. This is a syntax check, not a Windows build or run. - `tools/check_conventions.py --base upstream/main` with this title and body: all checks passed. - `tools/pr_overlap.py --branch` reports no overlapping lines. It could not compare #6044, #6879 and #6977, which conflict with `main`; merged with this branch, #6879 and #6977 conflict in the same files as they do with `main`, and #7780 merges cleanly. Not run on Windows, macOS or a console. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (see the #7780 note above) <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-19 16:25:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-19T16:25:59Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestSaveDataSearch.cpp
PR 6977 core/libs/prx/libSceSaveData.native/Export.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6271968529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7960#issuecomment-6271968529). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-19T16:25:59Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestSaveDataSearch.cpp` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libSceSaveData.native/Export.cpp` |
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7960
No description provided.