// Dirty file-cache writeback regression test. // Verifies buffered writes dirty cache pages, explicit flush writes them back, and close flushes synchronously. #include #include #include #include #define PAGE_SIZE_BYTES 4096u #define TEST_SIZE (PAGE_SIZE_BYTES * 4u) #define REWRITE_SIZE (PAGE_SIZE_BYTES * 2u) #define CLOSE_SIZE (PAGE_SIZE_BYTES * 3u) static uint8_t gPatternA[TEST_SIZE]; static uint8_t gPatternB[TEST_SIZE]; static uint8_t gReadback[TEST_SIZE]; static void print(const char* s) { SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) }; SysWriteConsole(&req); } static void print_u64(uint64_t v) { char buf[32]; int i = 0; if (v == 0) { print("0"); return; } while (v && i < (int)sizeof(buf)) { buf[i++] = (char)('0' + (v % 10)); v /= 10; } while (i--) { char c[2] = { buf[i], 0 }; print(c); } } static void exit_now(int code) { for (;;) SysExitProcess(code); } static void fail(const char* msg) { print("dirty_writeback: "); print(msg); print("\n"); exit_now(1); } static void fail_status(const char* what, uint32_t status) { print("dirty_writeback: "); print(what); print(" failed status="); print_u64(status); print("\n"); exit_now(1); } static uint32_t query_cache(SYS_FILE_CACHE_STATS* out) { memset(out, 0, sizeof(*out)); return SysQueryKernelStats(SYS_KERNEL_STATS_FILE_CACHE, out, sizeof(*out), 0); } static void delete_path(const char* path) { SYS_PATH_REQUEST req = { SysSvFromCstr(path) }; (void)SysDeleteFile(&req); } static uint32_t create_temp(uint32_t* fileOut, const char** pathOut) { static const char* paths[] = { "dirty_writeback.tmp", "/dirty_writeback.tmp", "1:/dirty_writeback.tmp", "0:/dirty_writeback.tmp", }; for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) { uint32_t h = 0; delete_path(paths[i]); SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(paths[i]), &h }; uint32_t s = SysCreateFile(&req); if (s == SYS_STATUS_SUCCESS) { *fileOut = h; if (pathOut) *pathOut = paths[i]; return SYS_STATUS_SUCCESS; } } return SYS_STATUS_NOT_FOUND; } static uint32_t open_path(const char* path, uint32_t* fileOut) { uint32_t h = 0; SYS_PATH_HANDLE_REQUEST req = { SysSvFromCstr(path), &h }; uint32_t s = SysOpenFile(&req); if (s == SYS_STATUS_SUCCESS) *fileOut = h; return s; } static void fill_patterns(void) { for (size_t i = 0; i < sizeof(gPatternA); ++i) { gPatternA[i] = (uint8_t)(0x11u + (uint8_t)(i * 13u + (i >> 6))); gPatternB[i] = (uint8_t)(0x80u + (uint8_t)(i * 31u + (i >> 4))); } } static void checked_write(uint32_t file, const void* buffer, size_t size, const char* what) { size_t done = 0; SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &done); uint32_t s = SysWriteFile(&req); if (s != SYS_STATUS_SUCCESS) fail_status(what, s); if (done != size) fail("short write"); } static void checked_read(uint32_t file, void* buffer, size_t size, size_t* gotOut, const char* what) { size_t got = 0; memset(buffer, 0xCC, size); SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(file, buffer, size, &got); uint32_t s = SysReadFile(&req); if (s != SYS_STATUS_SUCCESS) fail_status(what, s); if (gotOut) *gotOut = got; } static void checked_seek(uint32_t file, uint64_t offset, const char* what) { uint32_t s = SysSeekFile(file, offset); if (s != SYS_STATUS_SUCCESS) fail_status(what, s); } static void expect_writeback_progress(const SYS_FILE_CACHE_STATS* before, const SYS_FILE_CACHE_STATS* after, const char* what) { if (after->FlushFileFailures != before->FlushFileFailures) fail("flush failure counter increased"); if (after->WritebackFailures != before->WritebackFailures) fail("writeback failure counter increased"); if (after->WritebackBytes <= before->WritebackBytes) { print("dirty_writeback: "); print(what); print(" did not write back bytes\n"); exit_now(1); } } int main(void) { print("dirty_writeback: start\n"); fill_patterns(); uint32_t file = 0; const char* path = 0; uint32_t s = create_temp(&file, &path); if (s != SYS_STATUS_SUCCESS) fail_status("create temp", s); print("dirty_writeback: using "); print(path ? path : ""); print("\n"); SYS_FILE_CACHE_STATS before; SYS_FILE_CACHE_STATS afterWrite; SYS_FILE_CACHE_STATS afterFlush; SYS_FILE_CACHE_STATS beforeClose; SYS_FILE_CACHE_STATS afterClose; if (query_cache(&before) != SYS_STATUS_SUCCESS) fail("initial stats query failed"); checked_write(file, gPatternA, sizeof(gPatternA), "write pattern A"); if (query_cache(&afterWrite) != SYS_STATUS_SUCCESS) fail("post-write stats query failed"); if (afterWrite.DirtyMarks <= before.DirtyMarks) fail("write did not mark dirty cache pages"); if (afterWrite.DirtyPages <= before.DirtyPages) fail("write did not leave dirty pages resident"); checked_seek(file, 0, "seek readback before flush"); size_t got = 0; checked_read(file, gReadback, sizeof(gReadback), &got, "read before flush"); if (got != sizeof(gPatternA)) fail("read before flush size mismatch"); if (memcmp(gReadback, gPatternA, sizeof(gPatternA)) != 0) fail("cached read before flush mismatch"); s = SysFlushFile(file); if (s != SYS_STATUS_SUCCESS) fail_status("explicit flush", s); if (query_cache(&afterFlush) != SYS_STATUS_SUCCESS) fail("post-flush stats query failed"); if (afterFlush.FlushFileCalls <= afterWrite.FlushFileCalls) fail("flush call counter did not increase"); expect_writeback_progress(&afterWrite, &afterFlush, "explicit flush"); checked_seek(file, 0, "seek rewrite"); checked_write(file, gPatternB, REWRITE_SIZE, "rewrite pattern B"); checked_seek(file, REWRITE_SIZE, "seek close-tail"); checked_write(file, gPatternA + REWRITE_SIZE, CLOSE_SIZE - REWRITE_SIZE, "write close tail"); if (query_cache(&beforeClose) != SYS_STATUS_SUCCESS) fail("pre-close stats query failed"); if (beforeClose.DirtyMarks <= afterFlush.DirtyMarks) fail("rewrite did not mark dirty pages"); s = SysCloseHandle(file); file = 0; if (s != SYS_STATUS_SUCCESS) fail_status("close", s); if (query_cache(&afterClose) != SYS_STATUS_SUCCESS) fail("post-close stats query failed"); if (afterClose.FlushFileCalls <= beforeClose.FlushFileCalls) fail("close did not flush file"); expect_writeback_progress(&beforeClose, &afterClose, "close flush"); s = open_path(path, &file); if (s != SYS_STATUS_SUCCESS) fail_status("reopen", s); checked_read(file, gReadback, CLOSE_SIZE, &got, "read after reopen"); if (got != CLOSE_SIZE) fail("read after reopen size mismatch"); if (memcmp(gReadback, gPatternB, REWRITE_SIZE) != 0) fail("rewrite data not durable after close"); if (memcmp(gReadback + REWRITE_SIZE, gPatternA + REWRITE_SIZE, CLOSE_SIZE - REWRITE_SIZE) != 0) fail("tail data not durable after close"); s = SysCloseHandle(file); file = 0; if (s != SYS_STATUS_SUCCESS) fail_status("close reopened", s); delete_path(path); print("dirty_writeback: dirty-marks+="); print_u64(afterClose.DirtyMarks - before.DirtyMarks); print(" writeback-bytes+="); print_u64(afterClose.WritebackBytes - before.WritebackBytes); print(" flushes+="); print_u64(afterClose.FlushFileCalls - before.FlushFileCalls); print("\n"); print("dirty_writeback: PASS\n"); exit_now(0); return 0; }