fx_fault_tolerant_recover.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************************************************** | ||
| 2 | * Copyright (c) 2024 Microsoft Corporation | ||
| 3 | * Copyright (c) 2026-present Eclipse ThreadX contributors | ||
| 4 | * | ||
| 5 | * This program and the accompanying materials are made available under the | ||
| 6 | * terms of the MIT License which is available at | ||
| 7 | * https://opensource.org/licenses/MIT. | ||
| 8 | * | ||
| 9 | * SPDX-License-Identifier: MIT | ||
| 10 | **************************************************************************/ | ||
| 11 | |||
| 12 | |||
| 13 | /**************************************************************************/ | ||
| 14 | /**************************************************************************/ | ||
| 15 | /** */ | ||
| 16 | /** FileX Component */ | ||
| 17 | /** */ | ||
| 18 | /** Fault Tolerant */ | ||
| 19 | /** */ | ||
| 20 | /**************************************************************************/ | ||
| 21 | /**************************************************************************/ | ||
| 22 | |||
| 23 | #define FX_SOURCE_CODE | ||
| 24 | |||
| 25 | #include "fx_api.h" | ||
| 26 | #include "fx_utility.h" | ||
| 27 | #include "fx_fault_tolerant.h" | ||
| 28 | |||
| 29 | |||
| 30 | #ifdef FX_ENABLE_FAULT_TOLERANT | ||
| 31 | /**************************************************************************/ | ||
| 32 | /* */ | ||
| 33 | /* FUNCTION RELEASE */ | ||
| 34 | /* */ | ||
| 35 | /* _fx_fault_tolerant_recover PORTABLE C */ | ||
| 36 | /* 6.1 */ | ||
| 37 | /* AUTHOR */ | ||
| 38 | /* */ | ||
| 39 | /* William E. Lamie, Microsoft Corporation */ | ||
| 40 | /* */ | ||
| 41 | /* DESCRIPTION */ | ||
| 42 | /* */ | ||
| 43 | /* This function is called after the FAT chain is been updated but */ | ||
| 44 | /* the rest of the write operation fails. At this point, there is */ | ||
| 45 | /* need to undo the FAT chain operation, which includes: */ | ||
| 46 | /* (1) Remove newly allocated FAT entries; */ | ||
| 47 | /* (2) Restore the origianl FAT chain removed during the FAT chain */ | ||
| 48 | /* update; */ | ||
| 49 | /* */ | ||
| 50 | /* INPUT */ | ||
| 51 | /* */ | ||
| 52 | /* media_ptr Media control block pointer */ | ||
| 53 | /* */ | ||
| 54 | /* OUTPUT */ | ||
| 55 | /* */ | ||
| 56 | /* return status */ | ||
| 57 | /* */ | ||
| 58 | /* CALLS */ | ||
| 59 | /* */ | ||
| 60 | /* _fx_fault_tolerant_cleanup_FAT_chain Cleanup FAT chain */ | ||
| 61 | /* _fx_utility_32_unsigned_read Read a ULONG from memory */ | ||
| 62 | /* _fx_utility_FAT_entry_write Write a FAT entry */ | ||
| 63 | /* _fx_utility_FAT_flush Flush written FAT entries */ | ||
| 64 | /* */ | ||
| 65 | /* CALLED BY */ | ||
| 66 | /* */ | ||
| 67 | /* _fx_directory_attributes_set */ | ||
| 68 | /* _fx_directory_create */ | ||
| 69 | /* _fx_directory_delete */ | ||
| 70 | /* _fx_directory_rename */ | ||
| 71 | /* _fx_file_allocate */ | ||
| 72 | /* _fx_file_attributes_set */ | ||
| 73 | /* _fx_file_best_effort_allocate */ | ||
| 74 | /* _fx_file_create */ | ||
| 75 | /* _fx_file_delete */ | ||
| 76 | /* _fx_file_rename */ | ||
| 77 | /* _fx_file_truncate */ | ||
| 78 | /* _fx_file_truncate_release */ | ||
| 79 | /* _fx_file_write */ | ||
| 80 | /* _fx_unicode_directory_create */ | ||
| 81 | /* _fx_unicode_file_create */ | ||
| 82 | /* _fx_fault_tolerant_enable */ | ||
| 83 | /* */ | ||
| 84 | /**************************************************************************/ | ||
| 85 | 1189 | UINT _fx_fault_tolerant_recover(FX_MEDIA *media_ptr) | |
| 86 | { | ||
| 87 | UINT status; | ||
| 88 | ULONG insertion_front; | ||
| 89 | ULONG origianl_head_cluster; | ||
| 90 | FX_FAULT_TOLERANT_FAT_CHAIN *FAT_chain; | ||
| 91 | |||
| 92 | |||
| 93 | /* Set fault tolerant state to IDLE. */ | ||
| 94 | 1189 | media_ptr -> fx_media_fault_tolerant_state = FX_FAULT_TOLERANT_STATE_IDLE; | |
| 95 | |||
| 96 | /* Set FAT chain pointer. */ | ||
| 97 | 1189 | FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET); | |
| 98 | |||
| 99 | /* Whether or not the supplied FAT chain is valid. */ | ||
| 100 |
2/2✓ Branch 2 → 3 taken 689 times.
✓ Branch 2 → 4 taken 500 times.
|
1189 | if (!(FAT_chain -> fx_fault_tolerant_FAT_chain_flag & FX_FAULT_TOLERANT_FLAG_FAT_CHAIN_VALID)) |
| 101 | { | ||
| 102 | |||
| 103 | /* Invalid, which indiates the FAT chain has been cleaned up already. In this case, just return. */ | ||
| 104 | 689 | return(FX_SUCCESS); | |
| 105 | } | ||
| 106 | |||
| 107 | /* Set FAT chain pointer. */ | ||
| 108 | 500 | FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET); | |
| 109 | |||
| 110 | /* Recover FAT chain. */ | ||
| 111 | 500 | status = _fx_fault_tolerant_cleanup_FAT_chain(media_ptr, FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER); | |
| 112 |
2/2✓ Branch 5 → 6 taken 4 times.
✓ Branch 5 → 7 taken 484 times.
|
488 | if (status != FX_SUCCESS) |
| 113 | { | ||
| 114 | |||
| 115 | /* Return the error status. */ | ||
| 116 | 4 | return(status); | |
| 117 | } | ||
| 118 | |||
| 119 | /* Now, link the front of the insertion point back to the origianl FAT chain. */ | ||
| 120 | 484 | insertion_front = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_front); | |
| 121 | 484 | origianl_head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original); | |
| 122 | |||
| 123 |
2/2✓ Branch 9 → 10 taken 228 times.
✓ Branch 9 → 13 taken 256 times.
|
484 | if (insertion_front != FX_FREE_CLUSTER) |
| 124 | { | ||
| 125 | |||
| 126 | /* Front of the insertion point exists. Link the origianl chain back to the front of the insertion point. */ | ||
| 127 | 228 | status = _fx_utility_FAT_entry_write(media_ptr, insertion_front, origianl_head_cluster); | |
| 128 |
2/2✓ Branch 11 → 12 taken 4 times.
✓ Branch 11 → 13 taken 224 times.
|
228 | if (status != FX_SUCCESS) |
| 129 | { | ||
| 130 | |||
| 131 | /* Return the error status. */ | ||
| 132 | 4 | return(status); | |
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | /* New FAT chain is always linked by FAT entries. */ | ||
| 137 | |||
| 138 | /* Flush FAT table. */ | ||
| 139 | #ifdef FX_FAULT_TOLERANT | ||
| 140 | |||
| 141 | /* Ensure the new FAT chain is properly written to the media. */ | ||
| 142 | |||
| 143 | /* Flush the cached individual FAT entries */ | ||
| 144 | 480 | _fx_utility_FAT_flush(media_ptr); | |
| 145 | #endif | ||
| 146 | |||
| 147 | /* Return success. */ | ||
| 148 | 480 | return(FX_SUCCESS); | |
| 149 | } | ||
| 150 | #endif /* FX_ENABLE_FAULT_TOLERANT */ | ||
| 151 | |||
| 152 |