kernel: flush synced writes and export the POSIX fsync #1478

Closed
github-bot wants to merge 2 commits from gh-pr/1478/head into gh-pr/1478/base
Owner

@⁠mariobob on GitHub · 2026-10-13 14:47 UTC · original
mariobob:kernel-fsync → main · +76 −9 · 4 files · 2 commits · closed without merging

Summary

sceKernelFsync (fTx66l5iWIA) returns OK without touching the descriptor, and the POSIX fsync (juWbTNM+8hw) is not registered, so titles that call it get the unresolved-import stub. Neither flushes anything.

That matters on Linux and macOS. There Common::File writes through stdio (fwrite in sysLinuxFileIO.cpp), so bytes a title writes stay in the emulator's user-space buffer until the descriptor is closed or the buffer fills. After a title writes a file and syncs it:

  • sceKernelStat on the path still reports the old size, because it asks the host;
  • a second descriptor on the same file reads the old contents;
  • if the process is killed before the descriptor is closed, for example after a hang, the synced data is lost. (EXIT paths are fine: DbgExit flushes every stream before _Exit.)

On Windows, WriteFile makes the data visible straight away, but fsync still does not reach the disk.

Changes:

  • KernelFsync moves into kernel/fileSystem.cpp next to KernelFtruncate and uses the same descriptor checks. Writable regular files are flushed with Common::File::Flush (fflush on Linux and macOS, FlushFileBuffers on Windows), the call O_SYNC writes already use. Read-only, directory and device descriptors return OK, unknown or closed descriptors return EBADF, and sockets return EINVAL.
  • The POSIX fsync is registered in the Posix module and wraps KernelFsync with POSIX_CALL, like ftruncate.

Evidence

Insomniac's titles call the POSIX export during startup, right after Title ID, and reach the stub:

The stub returns 0, which is also what fsync returns on success, so I'm not claiming this changes how far either game gets. What changes is that the data they sync is flushed, as it is on the console.

Testing

TestFsyncVisibility in tests/KernelFileSystemTests.cpp writes 15 bytes to a file under /savedata0 and syncs it. While the writer is still open it checks that KernelStat reports 15 bytes and that a second descriptor reads the payload. It also checks that a read-only descriptor syncs, that the POSIX export resolves and returns 0, and that a closed descriptor returns EBADF (and -1 with errno EBADF through POSIX).

I did not build the full emulator or run kernel_file_system_tests: it creates a Vulkan device first, and I only have macOS with MoltenVK (see #1448). CI does not build that target either. What I ran instead:

  • A native harness on macOS 15.8 (Apple M4 Max, Apple clang 17), built from the real src/kernel/fileSystem.cpp, src/common/file.cpp, src/common/platform/sysLinuxFileIO.cpp and src/common/dateTime.cpp, with logging, mutexes, archives, guest-memory invalidation and socket lookup stubbed. macOS builds use the same POSIX file backend as Linux (KYTY_PLATFORM_LINUX). It runs the same sequence as the test:
main (df5b9b92) this branch
stat size after write + fsync, writer still open 0 15
bytes read through a second descriptor 0 15
stat size after close 15 15
fsync of a read-only descriptor n/a OK
fsync of a closed descriptor n/a EBADF
  • clang++ -std=c++20 -Wall -Wextra -fsyntax-only for x86-64 on fileSystem.cpp, libKernel.cpp and KernelFileSystemTests.cpp against the pinned submodule headers: no errors and no warnings on the changed lines.
  • git clang-format reports no changes on the diff.
  • Not built on Windows or Linux.

AI assistance

Claude Code found the missing export in the issue logs, wrote the change, the test and the harness, and ran the checks above.

**@⁠mariobob** on GitHub · 2026-10-13 14:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1478) `mariobob:kernel-fsync` → `main` · +76 −9 · 4 files · 2 commits · closed without merging ## Summary `sceKernelFsync` (`fTx66l5iWIA`) returns OK without touching the descriptor, and the POSIX `fsync` (`juWbTNM+8hw`) is not registered, so titles that call it get the unresolved-import stub. Neither flushes anything. That matters on Linux and macOS. There `Common::File` writes through stdio (`fwrite` in `sysLinuxFileIO.cpp`), so bytes a title writes stay in the emulator's user-space buffer until the descriptor is closed or the buffer fills. After a title writes a file and syncs it: - `sceKernelStat` on the path still reports the old size, because it asks the host; - a second descriptor on the same file reads the old contents; - if the process is killed before the descriptor is closed, for example after a hang, the synced data is lost. (`EXIT` paths are fine: `DbgExit` flushes every stream before `_Exit`.) On Windows, `WriteFile` makes the data visible straight away, but `fsync` still does not reach the disk. Changes: - `KernelFsync` moves into `kernel/fileSystem.cpp` next to `KernelFtruncate` and uses the same descriptor checks. Writable regular files are flushed with `Common::File::Flush` (`fflush` on Linux and macOS, `FlushFileBuffers` on Windows), the call `O_SYNC` writes already use. Read-only, directory and device descriptors return OK, unknown or closed descriptors return `EBADF`, and sockets return `EINVAL`. - The POSIX `fsync` is registered in the Posix module and wraps `KernelFsync` with `POSIX_CALL`, like `ftruncate`. ## Evidence Insomniac's titles call the POSIX export during startup, right after `Title ID`, and reach the stub: - Marvel's Spider-Man Remastered (PPSA01467): #701, 4 calls - Marvel's Wolverine (PPSA03671): #932, #1208 and #1316 (4 calls each), plus #1009, #1131 and #1453 The stub returns 0, which is also what `fsync` returns on success, so I'm not claiming this changes how far either game gets. What changes is that the data they sync is flushed, as it is on the console. ## Testing `TestFsyncVisibility` in `tests/KernelFileSystemTests.cpp` writes 15 bytes to a file under `/savedata0` and syncs it. While the writer is still open it checks that `KernelStat` reports 15 bytes and that a second descriptor reads the payload. It also checks that a read-only descriptor syncs, that the POSIX export resolves and returns 0, and that a closed descriptor returns `EBADF` (and `-1` with errno `EBADF` through POSIX). I did not build the full emulator or run `kernel_file_system_tests`: it creates a Vulkan device first, and I only have macOS with MoltenVK (see #1448). CI does not build that target either. What I ran instead: - A native harness on macOS 15.8 (Apple M4 Max, Apple clang 17), built from the real `src/kernel/fileSystem.cpp`, `src/common/file.cpp`, `src/common/platform/sysLinuxFileIO.cpp` and `src/common/dateTime.cpp`, with logging, mutexes, archives, guest-memory invalidation and socket lookup stubbed. macOS builds use the same POSIX file backend as Linux (`KYTY_PLATFORM_LINUX`). It runs the same sequence as the test: | | `main` (df5b9b92) | this branch | |---|---|---| | `stat` size after write + fsync, writer still open | 0 | 15 | | bytes read through a second descriptor | 0 | 15 | | `stat` size after close | 15 | 15 | | fsync of a read-only descriptor | n/a | OK | | fsync of a closed descriptor | n/a | `EBADF` | - `clang++ -std=c++20 -Wall -Wextra -fsyntax-only` for x86-64 on `fileSystem.cpp`, `libKernel.cpp` and `KernelFileSystemTests.cpp` against the pinned submodule headers: no errors and no warnings on the changed lines. - `git clang-format` reports no changes on the diff. - Not built on Windows or Linux. ## AI assistance Claude Code found the missing export in the issue logs, wrote the change, the test and the harness, and ran the checks above.
sceKernelFsync returned OK without touching the descriptor. On Linux and
macOS guest writes go through stdio, so data a title had synced stayed
in the emulator's buffer: stat() on the path and reads through another
descriptor did not see it, and killing the process before the descriptor
was closed lost it.

Move the call into the file system next to KernelFtruncate. It flushes
writable regular files, succeeds for read-only, directory and device
descriptors, and returns EBADF for unknown descriptors and EINVAL for
sockets.
Marvel's Spider-Man Remastered and Marvel's Wolverine call fsync
(juWbTNM+8hw) from the Posix module during startup. It was not
registered, so they reached the unresolved-import stub, which returns 0
without flushing anything. Route it to KernelFsync like the other POSIX
file calls, reporting failures through errno.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:57:38 +00:00
github-bot closed this pull request 2026-11-26 09:57:38 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-13 14:50 UTC · original

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dd62dae5-0b53-4009-91df-2d1a3f34f881
📥 Commits

Reviewing files that changed from the base of the PR and between df5b9b92be and 27b16f67c8.

📒 Files selected for processing (4)
  • src/kernel/fileSystem.cpp
  • src/kernel/fileSystem.h
  • src/libs/libKernel.cpp
  • tests/KernelFileSystemTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The filesystem API now implements KernelFsync with descriptor validation and file flushing. POSIX fsync forwards to this API. The change registers both entry points and adds tests for file visibility, read-only descriptors, and closed-descriptor errors.

Suggested reviewers: nmzik

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 27b16

No concrete fsync issue remains that should block merging. Run the normal build and test checks.

Pre-merge checks · Passed 4 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly summarizes the main changes: flushing synced writes and exporting POSIX fsync.
Description check Passed The description directly explains the fsync behavior, export changes, test coverage, and validation performed.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-13 14:50 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1478#issuecomment-6148310027) <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1478?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary><strong>ℹ️ Recent review info</strong></summary> <dl> <dd> <details> <summary><strong>⚙️ Run configuration</strong></summary> <dl> <dd> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `dd62dae5-0b53-4009-91df-2d1a3f34f881` </dd> </dl> </details> <details> <summary><strong>📥 Commits</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between df5b9b92be7dfd463b47756b707d6d356a9f0550 and 27b16f67c85c07b3676f8043b5736c5b90393cf1. </dd> </dl> </details> <details> <summary><strong>📒 Files selected for processing (4)</strong></summary> <dl> <dd> * `src/kernel/fileSystem.cpp` * `src/kernel/fileSystem.h` * `src/libs/libKernel.cpp` * `tests/KernelFileSystemTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </dd> </dl> </details> <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> The filesystem API now implements `KernelFsync` with descriptor validation and file flushing. POSIX `fsync` forwards to this API. The change registers both entry points and adds tests for file visibility, read-only descriptors, and closed-descriptor errors. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ⬇️ Low <!-- change_assessment_commit:"27b16f67c85c07b3676f8043b5736c5b90393cf1" --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>⚪ Minimal</strong> · up to <code>27b16</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `27b16` --> <!-- final_review_risk_coverage:{"sourceCommitId":"27b16f67c85c07b3676f8043b5736c5b90393cf1","coveredCommitId":"27b16f67c85c07b3676f8043b5736c5b90393cf1","kind":"reviewed"} --> No concrete fsync issue remains that should block merging. Run the normal build and test checks. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks (1 warning)</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 4 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks (4 passed)</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly summarizes the main changes: flushing synced writes and exporting POSIX fsync. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the fsync behavior, export changes, test coverage, and validation performed. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1478)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-21 19:41 UTC · original

Superseded by 7675579e7.

**@⁠Nmzik** on GitHub · 2026-10-21 19:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1478#issuecomment-6324931200) Superseded by 7675579e7.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.