fx_fault_tolerant_create_log_file.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_directory.h" | ||
| 28 | #include "fx_fault_tolerant.h" | ||
| 29 | |||
| 30 | |||
| 31 | #ifdef FX_ENABLE_FAULT_TOLERANT | ||
| 32 | /**************************************************************************/ | ||
| 33 | /* */ | ||
| 34 | /* FUNCTION RELEASE */ | ||
| 35 | /* */ | ||
| 36 | /* _fx_fault_tolerant_create_log_file PORTABLE C */ | ||
| 37 | /* 6.1.11 */ | ||
| 38 | /* AUTHOR */ | ||
| 39 | /* */ | ||
| 40 | /* William E. Lamie, Microsoft Corporation */ | ||
| 41 | /* */ | ||
| 42 | /* DESCRIPTION */ | ||
| 43 | /* */ | ||
| 44 | /* This function finds an available cluster used by log file. Then it */ | ||
| 45 | /* sets the cluster number to boot sector. */ | ||
| 46 | /* */ | ||
| 47 | /* INPUT */ | ||
| 48 | /* */ | ||
| 49 | /* media_ptr Media control block pointer */ | ||
| 50 | /* */ | ||
| 51 | /* OUTPUT */ | ||
| 52 | /* */ | ||
| 53 | /* return status */ | ||
| 54 | /* */ | ||
| 55 | /* CALLS */ | ||
| 56 | /* */ | ||
| 57 | /* _fx_utility_FAT_entry_read Read a FAT entry */ | ||
| 58 | /* _fx_utility_FAT_entry_write Write a FAT entry */ | ||
| 59 | /* _fx_utility_FAT_flush Flush written FAT entries */ | ||
| 60 | /* _fx_utility_32_unsigned_write Write a ULONG from memory */ | ||
| 61 | /* _fx_utility_logical_sector_read Read a logical sector */ | ||
| 62 | /* */ | ||
| 63 | /* CALLED BY */ | ||
| 64 | /* */ | ||
| 65 | /* _fx_fault_tolerant_enable */ | ||
| 66 | /* */ | ||
| 67 | /**************************************************************************/ | ||
| 68 | 1506 | UINT _fx_fault_tolerant_create_log_file(FX_MEDIA *media_ptr) | |
| 69 | { | ||
| 70 | ULONG clusters; | ||
| 71 | ULONG FAT_index; | ||
| 72 | 1506 | ULONG FAT_value = 0; | |
| 73 | UINT found; | ||
| 74 | UINT status; | ||
| 75 | UINT i; | ||
| 76 | |||
| 77 | /* Yes. Create a log file. */ | ||
| 78 | /* First find a free cluster. */ | ||
| 79 |
2/2✓ Branch 2 → 3 taken 16 times.
✓ Branch 2 → 4 taken 1490 times.
|
1506 | if (media_ptr -> fx_media_available_clusters < media_ptr -> fx_media_fault_tolerant_clusters) |
| 80 | { | ||
| 81 | |||
| 82 | /* Out of disk space. */ | ||
| 83 | 16 | return(FX_NO_MORE_SPACE); | |
| 84 | } | ||
| 85 | |||
| 86 | /* Now we need to find the consecutive clusters. */ | ||
| 87 | 1490 | clusters = media_ptr -> fx_media_fault_tolerant_clusters; | |
| 88 | 1490 | FAT_index = FX_FAT_ENTRY_START; | |
| 89 | 1490 | found = FX_FALSE; | |
| 90 |
2/2✓ Branch 16 → 5 taken 18760 times.
✓ Branch 16 → 17 taken 12 times.
|
18772 | while (FAT_index <= (media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) |
| 91 | { | ||
| 92 | |||
| 93 | /* Determine if enough consecutive FAT entries are available. */ | ||
| 94 | 18760 | i = 0; | |
| 95 | |||
| 96 | |||
| 97 | do | ||
| 98 | { | ||
| 99 | |||
| 100 | /* Read a FAT entry. */ | ||
| 101 | 20310 | status = _fx_utility_FAT_entry_read(media_ptr, (FAT_index + i), &FAT_value); | |
| 102 | |||
| 103 | /* Check for a successful status. */ | ||
| 104 |
2/2✓ Branch 7 → 8 taken 4 times.
✓ Branch 7 → 9 taken 20306 times.
|
20310 | if (status != FX_SUCCESS) |
| 105 | { | ||
| 106 | |||
| 107 | /* Return the error status. */ | ||
| 108 | 4 | return(status); | |
| 109 | } | ||
| 110 | |||
| 111 | /* Determine if the entry is free. */ | ||
| 112 |
2/2✓ Branch 9 → 10 taken 17282 times.
✓ Branch 9 → 11 taken 3024 times.
|
20306 | if (FAT_value != FX_FREE_CLUSTER) |
| 113 | { | ||
| 114 | 17282 | break; | |
| 115 | } | ||
| 116 | |||
| 117 | /* Otherwise, increment the consecutive FAT indices. */ | ||
| 118 | 3024 | i++; | |
| 119 |
2/2✓ Branch 11 → 12 taken 1550 times.
✓ Branch 11 → 13 taken 1474 times.
|
3024 | } while (i < clusters); |
| 120 | |||
| 121 | /* Determine if we found enough FAT entries. */ | ||
| 122 |
2/2✓ Branch 13 → 14 taken 1474 times.
✓ Branch 13 → 15 taken 17282 times.
|
18756 | if (i >= clusters) |
| 123 | { | ||
| 124 | |||
| 125 | /* Yes, we have found enough FAT entries - set the found | ||
| 126 | flag and get out of this loop. */ | ||
| 127 | 1474 | found = FX_TRUE; | |
| 128 | 1474 | break; | |
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | /* Position to the next possibly free FAT entry. */ | ||
| 133 | 17282 | FAT_index = FAT_index + i + 1; | |
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | /* Determine if we found enough consecutive clusters to satisfy the | ||
| 138 | request. */ | ||
| 139 |
2/2✓ Branch 17 → 18 taken 12 times.
✓ Branch 17 → 19 taken 1474 times.
|
1486 | if (!found) |
| 140 | { | ||
| 141 | |||
| 142 | /* Not enough contiguous space on the media. Return error status. */ | ||
| 143 | 12 | return(FX_NO_MORE_SPACE); | |
| 144 | } | ||
| 145 | |||
| 146 | /* Update the link pointers in the new clusters. */ | ||
| 147 |
2/2✓ Branch 24 → 20 taken 1550 times.
✓ Branch 24 → 25 taken 1470 times.
|
3020 | for (i = 0; i < (clusters - 1); i++) |
| 148 | { | ||
| 149 | |||
| 150 | /* Update the cluster links. Since the allocation is | ||
| 151 | sequential, we just have to link each FAT entry to the | ||
| 152 | next one. */ | ||
| 153 | 1550 | status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + i, FAT_index + i + 1); | |
| 154 | |||
| 155 | /* Check for a bad status. */ | ||
| 156 |
2/2✓ Branch 21 → 22 taken 4 times.
✓ Branch 21 → 23 taken 1546 times.
|
1550 | if (status != FX_SUCCESS) |
| 157 | { | ||
| 158 | |||
| 159 | /* Return the error status. */ | ||
| 160 | 4 | return(status); | |
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | /* Now place an EOF in the last cluster entry. */ | ||
| 166 | 1470 | status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + clusters - 1, media_ptr -> fx_media_fat_last); | |
| 167 | |||
| 168 | /* Check for a bad status. */ | ||
| 169 |
2/2✓ Branch 26 → 27 taken 4 times.
✓ Branch 26 → 28 taken 1466 times.
|
1470 | if (status != FX_SUCCESS) |
| 170 | { | ||
| 171 | |||
| 172 | /* Return the error status. */ | ||
| 173 | 4 | return(status); | |
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | #ifdef FX_FAULT_TOLERANT | ||
| 178 | |||
| 179 | /* Flush the cached individual FAT entries */ | ||
| 180 | 1466 | _fx_utility_FAT_flush(media_ptr); | |
| 181 | |||
| 182 | #endif /* FX_FAULT_TOLERANT */ | ||
| 183 | |||
| 184 | /* Write start cluster for the file tolerant log file into the boot sector. */ | ||
| 185 | 1466 | _fx_utility_32_unsigned_write(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_BOOT_INDEX, FAT_index); | |
| 186 | |||
| 187 | /* Write the boot sector. */ | ||
| 188 | 1466 | media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_WRITE; | |
| 189 | 1466 | media_ptr -> fx_media_driver_status = FX_IO_ERROR; | |
| 190 | 1466 | media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_fault_tolerant_memory_buffer; | |
| 191 | 1466 | media_ptr -> fx_media_driver_sectors = 1; | |
| 192 | 1466 | media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR; | |
| 193 | |||
| 194 | /* If trace is enabled, insert this event into the trace buffer. */ | ||
| 195 | FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_WRITE, media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) | ||
| 196 | |||
| 197 | /* Invoke the driver to read the boot sector. */ | ||
| 198 | 1466 | (media_ptr -> fx_media_driver_entry) (media_ptr); | |
| 199 | |||
| 200 | /* Determine if the boot sector was read correctly. */ | ||
| 201 |
2/2✓ Branch 31 → 32 taken 4 times.
✓ Branch 31 → 33 taken 1462 times.
|
1466 | if (media_ptr -> fx_media_driver_status != FX_SUCCESS) |
| 202 | { | ||
| 203 | |||
| 204 | /* Return the boot sector error status. */ | ||
| 205 | 4 | return(FX_BOOT_ERROR); | |
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | /* Set the start cluster. */ | ||
| 210 | 1462 | media_ptr -> fx_media_fault_tolerant_start_cluster = FAT_index; | |
| 211 | |||
| 212 | /* Decrease available cluster. */ | ||
| 213 | 1462 | media_ptr -> fx_media_available_clusters = | |
| 214 | 1462 | media_ptr -> fx_media_available_clusters - clusters; | |
| 215 | |||
| 216 | /* Return success. */ | ||
| 217 | 1462 | return(FX_SUCCESS); | |
| 218 | } | ||
| 219 | #endif /* FX_ENABLE_FAULT_TOLERANT */ | ||
| 220 | |||
| 221 |