libkernel: flush files in fsync and export chmod, fchmod and rmdir #1177
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1177
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1177/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@overlordxrz-source on GitHub · 2026-10-08 01:14 UTC · original
overlordxrz-source:libkernel/fsync-chmod-exports→main· +291 −21 · 4 files · 2 commits · closed without mergingsceKernelFsync(fTx66l5iWIA) is a stub that returns 0 without touching the file. The POSIXfsync(juWbTNM+8hw),fdatasync(KIbJFQ0I1Cg),fchmod(n01yNbQO5W4) andrmdir(c7ZnT7V1B98), andsceKernelFdatasync(30Rh4ixbKy4),sceKernelChmod(fgIsQ10xYVA) andsceKernelFchmod(UtszJWHrDcA) are not registered at all, so games that import them get the unresolved-import stub.This adds them next to the other libkernel file calls:
KernelFsyncmoves tofileSystem.cppand flushes the host file of a writable descriptor. It backssceKernelFsync,sceKernelFdatasync,fsyncandfdatasync. Errors: EBADF for a bad descriptor, EINVAL for a socket, EIO if the flush fails. Read-only, directory and special descriptors and the standard streams return 0.KernelChmodandKernelFchmodcheck the path or descriptor: EFAULT for a null path, ENOENT for a missing or unmapped file, EBADF for a bad descriptor, EROFS on archive mounts. Guest permissions aren't emulated (statreports 0777), so they don't change host permissions. The existing POSIXchmodexport (z0dtnPxYgtg) was a plain stub and now uses the same checks.KernelRmdirreturns ENOTEMPTY for a non-empty directory (it returned EIO) and ENOTDIR for a regular file (it returned ENOENT)Evidence
Unresolved import stub called: juWbTNM+8hw[Posix_v1][libkernel_v1.1][Func]. I'm not claiming this makes Wolverine boot.Unresolved import stub called: fgIsQ10xYVA[libkernel_v1][libkernel_v1.1][Func], repeated many timeslibc.prximportssceKernelChmodandrmdirin all 43 game logs I scannedTesting
tests/KernelFileSystemTests.cpp:TestFsyncChmod: resolves all of the new NIDs throughInitLibKernel_1. It checks thatsceKernelFsyncputs buffered writes in the host file, that the fsync variants accept writable, read-only and standard-stream descriptors and reject bad ones, that chmod/fchmod accept existing files and directories and reject missing, null and unmapped paths and bad descriptors (with the POSIX errno), and thatrmdirkeeps a non-empty directory, rejects a file, removes an empty directory and reports a missing one.CheckArchiveMountalso expects EROFS fromKernelChmodandKernelFchmodon an archive member, and 0 fromKernelFsyncI couldn't run
kernel_file_system_testsitself. It needs a Vulkan device and the full emulator, and my machine is aarch64 Linux. Here is what I did instead:fileSystem.cppandlibKernel.cppforx86_64-linux-gnuandx86_64-windows-gnu(mingw), and the updated test file forx86_64-linux-gnu, with clang (zig c++) at-Wall -Wextra. The changed lines add no warnings.fileSystem.cppplussrc/common, with stubs for network, memory and ZArchive. It runs the same checks asTestFsyncChmodagainst a temporary/savedata0mount, calling theKernel*functions directly, and all 18 pass. The NID registration is only covered by the test above.src/lines are clean undersrc/.clang-format(clang-format 23.1.3)Not built on macOS. This touches the same test function as #1170 (
sceKernelTruncate), but the two merge cleanly in either order.AI assistance
An AI assistant helped me find the missing exports in the issue logs, write the change and the test, and run the builds above. I reviewed the code and the results.
@coderabbitai[bot] on GitHub · 2026-10-08 01:18 UTC · original · edited 2026-10-08 01:41 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
7522c853-e047-43cf-a09a-b968b1335897📥 Commits
Reviewing files that changed from the base of the PR and between
af76dcdb2dand0ad2c61189.📒 Files selected for processing (2)
src/kernel/fileSystem.cpptests/KernelFileSystemTests.cpp🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review.
📝 Walkthrough
Walkthrough
The change adds kernel implementations for
fsync,chmod, andfchmod, and updatesrmdirerror results. POSIX wrappers and filesystem-library exports now resolve to the filesystem operations. Tests cover operation results for descriptors, paths, directories, and archive-backed files.Priority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
0ad2cThis change adds fsync, chmod, and fchmod support and refines the rmdir error codes. No concrete merge-blocking risk was identified. The author could not run the full test target, so normal CI should confirm it before merge.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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 helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-08 01:18 UTC · original · edited 2026-10-08 01:41 UTC
On
src/kernel/fileSystem.cppline 1471 (outdated):🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Keep the descriptor alive during
KernelFsyncandKernelFchmod.GetFile(d)releases the descriptor-table lock before these functions use the returnedFile*. If another thread callsKernelClose(d), close can delete theFilewhileKernelFsyncflushes it orKernelFchmodreadsreal_name. Theopenedcheck andFile::mutexdo not prevent this use-after-free. Pin the descriptor for the full operation, and make close wait for active operations before deletion.✅ Addressed in commit
0ad2c61@coderabbitai[bot] on GitHub · 2026-10-08 01:18 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
86d271bd-f8a4-4c77-9c93-e89472dfe14e📥 Commits
Reviewing files that changed from the base of the PR and between
5a8808086aandaf76dcdb2d.📒 Files selected for processing (4)
src/kernel/fileSystem.cppsrc/kernel/fileSystem.hsrc/libs/libKernel.cpptests/KernelFileSystemTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@overlordxrz-source on GitHub · 2026-10-08 01:39 UTC · original
On
src/kernel/fileSystem.cppline 1471 (outdated):Agreed, thanks. Fixed in "libkernel: keep the file alive in fsync and fchmod during a close".
KernelFsync and KernelFchmod now do their work with the descriptor table lock held, as in #1170. A new FileDescriptors::UseFile handles that, and since DeleteDescriptor takes the same lock, close can't delete the File mid-call. That alone didn't cover fsync, because KernelClose closed the host file without any lock, so the flush could still race with the close. KernelClose now closes the host file and clears opened under the file lock. The lock order stays table lock then file lock, and close never holds both at once.
The test now keeps closing and reopening one descriptor on another thread while calling fsync and fchmod on it, and expects only OK or EBADF. I also ran the same race in a standalone harness built from fileSystem.cpp. The previous version crashed on every run. With the table lock alone it still crashed, and with this commit it passed 25 runs of 20,000 cycles.
Other descriptor calls (read, write, lseek, fstat) still use GetFile without the table lock. That's older and I left it out of this PR.
@coderabbitai[bot] on GitHub · 2026-10-08 01:42 UTC · original
On
src/kernel/fileSystem.cppline 1471 (outdated):@overlordxrz-source, thanks for the fix and the scope clarification. The supplied code addresses both races:UseFileprevents deletion duringKernelFsyncandKernelFchmod, and the file lock serializes the flush with host-file closure.KernelClosereleases the file lock beforeDeleteDescriptoracquires the descriptor-table lock.The older
GetFilecallers can remain outside this PR. Would you like a follow-up issue for descriptor-lifetime protection in read, write, lseek, and fstat?✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@Nmzik on GitHub · 2026-10-08 15:01 UTC · original
No games requires proper impl
Pull request closed