📄 crashtst.c
字号:
STAVFS_InitParams_t InitParams; STAVFS_OpenParams_t OpenParams; STAVFS_TermParams_t TermParams; strcpy (InitParams.EVTName, EVT_DEVICE_NAME); strcpy (InitParams.ATAPIName, ATAPI_DEVICE_NAME); InitParams.Flags = 0; InitParams.MemoryPartition = system_partition; InitParams.Protocol = 1; InitParams.UnitNumber = 0; InitParams.PartitionNumber = PARTITION_NUM; OpenParams.Flags = 0; TermParams.Flags = 0; if (ST_NO_ERROR != STAVFS_Init (DEV_NAME, &InitParams)) { STTBX_Print (("CheckAfterCrash: Error initialising partition\n")); Result = TEST_FAILED; } else { static BOOL BadClose = FALSE; if ((CrashPoint == CP_CLOSE) && BadClose) { /* * Last time round we had a bad close * and it obviously was not the last write of the close * because we have got another close */ STTBX_Print (("Managed to Open dirty partition" " without fsck first (last iteration)\n")); Result = TEST_FAILED; } BadClose = FALSE; if ((CrashPoint >= CP_OPEN) && (CrashPoint <= CP_CLOSE)) { /* file system left in 'open' state - confirm we're not allowed to open it without an fsck first. With Backup On Write, a crash on the first write leaves the real copy consistent (in the old, closed state). Without, nothing was written, so again the partition counts as closed until a crash with IterationIdx >= 1. Close's last action should be to mark the filesystem clean, so it should only count as such if that completed without crashing */ if (ST_NO_ERROR == STAVFS_Open (DEV_NAME, &OpenParams, &DiskHandle)) { if (Iteration == 1) { /* On the first iteration we don't even get it open */ } else if (CrashPoint == CP_CLOSE) { /* * If the close was incomplete we should have to FSCK * But the write could be the last write of the close * So lets wait and see if we get another close */ BadClose = TRUE; } else { STTBX_Print (("Managed to Open dirty partition" " without fsck first\n")); Result = TEST_FAILED; } /* we'll still do an fsck - close first */ if (ST_NO_ERROR != STAVFS_Close (DiskHandle)) { STTBX_Print (("Error closing it\n")); /* but carry on ... */ } } if (ST_NO_ERROR != STAVFS_FileSystemCheck (DEV_NAME, STAVFS_FSCK_DEFAULT)) { STTBX_Print(("CheckAfterCrash: File system check failed\n")); Result = TEST_FAILED; /* STAVFS_Open will probably fail below */ } /* explicitly don't use fsck if we shouldn't need it, to confirm Closed leaves partition in a valid, closed state. An initial fsck in EstablishBackgroundFile should lose us the effects of any previous test */ } if (CrashPoint == CP_ERROR) { /* just do an fsck. CheckWorkingFile then deletes the working file, CheckBackgroundFile checks the error didn't affect the background file, and CrashTest will give up */ if (ST_NO_ERROR != STAVFS_FileSystemCheck (DEV_NAME, STAVFS_FSCK_DEFAULT)) { STTBX_Print(("CheckAfterCrash: File system check failed\n")); Result = TEST_FAILED; /* STAVFS_Open will probably fail below */ } } if (ST_NO_ERROR != STAVFS_Open (DEV_NAME, &OpenParams, &DiskHandle)) { STTBX_Print (("CheckAfterCrash: Error opening partition\n")); Result = TEST_FAILED; } else { /* try each of the following indepedently */ if (TEST_PASSED != CheckWorkingFile(DiskHandle)) { Result = TEST_FAILED; } if (TEST_PASSED != CheckBackgroundFile(DiskHandle)) { Result = TEST_FAILED; } } if (ST_NO_ERROR != STAVFS_Term(DEV_NAME, &TermParams)) { STTBX_Print (("CheckAfterCrash: Error terminating partition \n")); Result = TEST_FAILED; } } return (Result);}/******************************************************************************Function Name : CheckBackgroundFile Description : Confirm that the 'background' file is still intact, and contains exactly the right data Parameters : disk handle******************************************************************************/static TestResult_t CheckBackgroundFile(STAVFS_Handle_t DiskHandle){ TestResult_t Result = TEST_PASSED; S32 NumChunks; if (TEST_PASSED != ScanFile (DiskHandle, BACKGROUND_FILE_NAME, BACKGROUND_CHUNK_SIZE, "CheckBackgroundFile: ", &NumChunks, FALSE)) { Result = TEST_FAILED; } else if (NumChunks == DOESNT_EXIST) { /* deleted somewhere, possibly as a result of the two cluster chains joining */ STTBX_Print (("Background file has disappeared\n")); Result = TEST_FAILED; } else if (NumChunks != BACKGROUND_NUM_CHUNKS) { /* corrupted somewhere during crash or fsck */ STTBX_Print (("Background file contains the wrong amount of data %d %d\n", NumChunks, BACKGROUND_NUM_CHUNKS)); Result = TEST_FAILED; } return (Result);}/******************************************************************************Function Name : CheckWorkingFile Description : Confirm that the 'working' file is in a state appropriate to where the crash took place. Leave it properly deleted if possible Parameters : disk handle******************************************************************************/static TestResult_t CheckWorkingFile(STAVFS_Handle_t DiskHandle){ TestResult_t Result = TEST_PASSED; ST_ErrorCode_t Error; U64 FreeDiskSpace; S32 NumChunks; BOOL BadLastSector = FALSE; if (CP_WRITE_POSTSYNC_FIRST) { /* The sector write will have corrupted the last sector of the file */ BadLastSector = TRUE; } if (CrashPoint != CP_ERROR) { /* check the cluster chain yields the correct data, and that EOF is in the right place for where the crash took place */ if (TEST_PASSED != ScanFile (DiskHandle, WORKING_FILE_NAME, WORKING_CHUNK_SIZE, "CheckWorkingFile: ", &NumChunks, BadLastSector)) { Result = TEST_FAILED; } else if (CrashPoint < CP_CREATE_FILE || CrashPoint > CP_DELETE_FILE) { /* file creation never got started / file deletion completed - there should be nothing there */ if (NumChunks != DOESNT_EXIST) { STTBX_Print (("Working file shouldn't exist\n")); Result = TEST_FAILED; } } else if (CrashPoint == CP_CREATE_FILE) { /* crash during file creation - allow no file or no data */ if ((NumChunks != DOESNT_EXIST) && NumChunks != 0) { STTBX_Print (("Working file shouldn't contain data\n")); Result = TEST_FAILED; } } else if (CrashPoint == CP_WRITE_PRESYNC) { /* crash after file creation but before sync */ if (NumChunks != 0) { STTBX_Print (("Working file should exist" " but contain no data\n")); Result = TEST_FAILED; } } else if (CrashPoint == CP_SYNC_FILE) { /* crash during sync operation - allow no data or sync quantity */ if ((NumChunks != 0) && (NumChunks != WORKING_NUM_CHUNKS_PRESYNC)) { STTBX_Print (("Working file contains the wrong amount of data\n")); Result = TEST_FAILED; } } else if ((CrashPoint == CP_WRITE_POSTSYNC_FIRST) || (CrashPoint == CP_WRITE_POSTSYNC)) { /* should keep sync data size until next sync or close */ if (NumChunks != WORKING_NUM_CHUNKS_PRESYNC) { STTBX_Print (("Working file should contain data written" " up to synchronisation call %d %d\n", NumChunks, WORKING_NUM_CHUNKS_PRESYNC)); Result = TEST_FAILED; } } else if (CrashPoint == CP_CLOSE_FILE) { /* allow sync quantity or close quantity */ if ((NumChunks != WORKING_NUM_CHUNKS_PRESYNC) && (NumChunks != (WORKING_NUM_CHUNKS_POSTSYNC + WORKING_NUM_CHUNKS_PRESYNC))) { STTBX_Print (("Working file contains the wrong amount of data\n")); Result = TEST_FAILED; } } /* note that without a crash between CloseFile and DeleteFile, we can't confirm the data is stable if a crash occurs then */ else if (CrashPoint == CP_DELETE_FILE) { /* allow close quantity or non-existant */ if ((NumChunks != DOESNT_EXIST) && (NumChunks != (WORKING_NUM_CHUNKS_POSTSYNC + WORKING_NUM_CHUNKS_PRESYNC))) { STTBX_Print (("Working file should contain all the data" " written or have disappeared\n")); Result = TEST_FAILED; } } else { assert(0); /* bad CrashPoint value */ } } /* The correctness of whether the file exists or not has been covered above, now delete it if it does exist, without any clever logic to ensure we're left in a clean state */ Error = STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, WORKING_FILE_NAME); if ((Error != ST_NO_ERROR) && (Error != STAVFS_ERROR_NO_SUCH_FILE)) { STTBX_Print (("Unexpected error deleting working file\n")); Result = TEST_FAILED; } /* ... the real test is that this frees up all the space. This should apply even if an error occurred: */ if (ST_NO_ERROR != STAVFS_GetFreeDiskSpace(DiskHandle, &FreeDiskSpace)) { STTBX_Print (("Error getting free disk space\n")); Result = TEST_FAILED; } else if (!I64_AreEqual(BaseDiskSpace, FreeDiskSpace)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -