fx_fault_tolerant_read_directory_sector.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_read_directory_sector PORTABLE C */ | ||
| 36 | /* 6.1 */ | ||
| 37 | /* AUTHOR */ | ||
| 38 | /* */ | ||
| 39 | /* William E. Lamie, Microsoft Corporation */ | ||
| 40 | /* */ | ||
| 41 | /* DESCRIPTION */ | ||
| 42 | /* */ | ||
| 43 | /* This function handles directory sector read requests. During file or*/ | ||
| 44 | /* directory operations with Fault Tolerant protection, intermediate */ | ||
| 45 | /* operations are written to the fault tolerant log files. Therefore, */ | ||
| 46 | /* sector read should search for fault tolerant log before the request */ | ||
| 47 | /* can be passed to normal directory entry read routine. */ | ||
| 48 | /* */ | ||
| 49 | /* INPUT */ | ||
| 50 | /* */ | ||
| 51 | /* media_ptr Media control block pointer */ | ||
| 52 | /* logical_sector Logical sector number */ | ||
| 53 | /* buffer_ptr Pointer of receiving buffer */ | ||
| 54 | /* sectors Number of sectors to read */ | ||
| 55 | /* */ | ||
| 56 | /* OUTPUT */ | ||
| 57 | /* */ | ||
| 58 | /* return status */ | ||
| 59 | /* */ | ||
| 60 | /* CALLS */ | ||
| 61 | /* */ | ||
| 62 | /* _fx_utility_16_unsigned_read Read a USHORT from memory */ | ||
| 63 | /* _fx_utility_32_unsigned_read Read a ULONG from memory */ | ||
| 64 | /* memcpy Memory Copy */ | ||
| 65 | /* */ | ||
| 66 | /* CALLED BY */ | ||
| 67 | /* */ | ||
| 68 | /* _fx_utility_logical_sector_read */ | ||
| 69 | /* */ | ||
| 70 | /**************************************************************************/ | ||
| 71 | 83035086 | UINT _fx_fault_tolerant_read_directory_sector(FX_MEDIA *media_ptr, ULONG64 logical_sector, | |
| 72 | VOID *buffer_ptr, ULONG64 sectors) | ||
| 73 | { | ||
| 74 | ULONG logs_remaining; | ||
| 75 | ULONG copy_offset; | ||
| 76 | ULONG copy_size; | ||
| 77 | UCHAR *current_ptr; | ||
| 78 | UCHAR *current_buffer_ptr; | ||
| 79 | ULONG size; | ||
| 80 | USHORT type; | ||
| 81 | ULONG log_len; | ||
| 82 | ULONG64 log_sector; | ||
| 83 | FX_FAULT_TOLERANT_DIR_LOG *dir_log; | ||
| 84 | |||
| 85 | /* Get fault tolerant data. */ | ||
| 86 | 83035086 | logs_remaining = media_ptr -> fx_media_fault_tolerant_total_logs; | |
| 87 | |||
| 88 | /* Any redo logs? */ | ||
| 89 |
2/2✓ Branch 2 → 3 taken 81317452 times.
✓ Branch 2 → 4 taken 1717634 times.
|
83035086 | if (logs_remaining == 0) |
| 90 | { | ||
| 91 | |||
| 92 | /* No. Just return. */ | ||
| 93 | 81317452 | return(FX_SUCCESS); | |
| 94 | } | ||
| 95 | |||
| 96 | /* Get size of all logs. */ | ||
| 97 | 1717634 | size = media_ptr -> fx_media_fault_tolerant_file_size - FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET; | |
| 98 | |||
| 99 | /* Get the first log pointer. */ | ||
| 100 | 1717634 | current_ptr = media_ptr -> fx_media_fault_tolerant_memory_buffer + | |
| 101 | FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET + | ||
| 102 | FX_FAULT_TOLERANT_LOG_CONTENT_HEADER_SIZE; | ||
| 103 | |||
| 104 | /* Loop throught all FAT logs. */ | ||
| 105 |
2/2✓ Branch 18 → 5 taken 1799830 times.
✓ Branch 18 → 19 taken 1717620 times.
|
3517450 | while (logs_remaining) |
| 106 | { | ||
| 107 | |||
| 108 | /* Check log type. */ | ||
| 109 | 1799830 | type = (USHORT)_fx_utility_16_unsigned_read(current_ptr); | |
| 110 | |||
| 111 | /* Get log length. */ | ||
| 112 | 1799830 | log_len = _fx_utility_16_unsigned_read(current_ptr + 2); | |
| 113 | |||
| 114 | /* Validate the size value. */ | ||
| 115 |
2/2✓ Branch 7 → 8 taken 10 times.
✓ Branch 7 → 9 taken 1799820 times.
|
1799830 | if (log_len > size) |
| 116 | { | ||
| 117 | |||
| 118 | /* Log file is corrupted. Nothing can be done. Return.*/ | ||
| 119 | 10 | return(FX_FILE_CORRUPT); | |
| 120 | } | ||
| 121 | 1799820 | size -= log_len; | |
| 122 | |||
| 123 | /* Check log type. */ | ||
| 124 |
2/2✓ Branch 9 → 10 taken 1791462 times.
✓ Branch 9 → 17 taken 8358 times.
|
1799820 | if (type == FX_FAULT_TOLERANT_DIR_LOG_TYPE) |
| 125 | { | ||
| 126 | |||
| 127 | /* Get the log pointer. */ | ||
| 128 | 1791462 | dir_log = (FX_FAULT_TOLERANT_DIR_LOG *)current_ptr; | |
| 129 | |||
| 130 | /* Get the sector of log. */ | ||
| 131 | 1791462 | log_sector = _fx_utility_64_unsigned_read((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_sector); | |
| 132 | |||
| 133 | /* Is this log sector in the range of sectors to read? */ | ||
| 134 |
4/4✓ Branch 11 → 12 taken 1760755 times.
✓ Branch 11 → 17 taken 30707 times.
✓ Branch 12 → 13 taken 1728087 times.
✓ Branch 12 → 17 taken 32668 times.
|
1791462 | if ((log_sector >= logical_sector) && (log_sector < logical_sector + sectors)) |
| 135 | { | ||
| 136 | |||
| 137 | /* Yes. Update the content in this sector. */ | ||
| 138 | 1728087 | current_buffer_ptr = ((UCHAR *)buffer_ptr) + (log_sector - logical_sector) * | |
| 139 | 1728087 | media_ptr -> fx_media_bytes_per_sector; | |
| 140 | |||
| 141 | /* Set copy information. */ | ||
| 142 | 1728087 | copy_offset = _fx_utility_32_unsigned_read((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_offset); | |
| 143 | |||
| 144 | 1728087 | copy_size = log_len - FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE; | |
| 145 | |||
| 146 |
2/2✓ Branch 14 → 15 taken 4 times.
✓ Branch 14 → 16 taken 1728083 times.
|
1728087 | if ((copy_offset + copy_size) > media_ptr -> fx_media_bytes_per_sector) |
| 147 | { | ||
| 148 | 4 | return(FX_FILE_CORRUPT); | |
| 149 | } | ||
| 150 | |||
| 151 | /* Copy data into destination sector. */ | ||
| 152 | 1728083 | memcpy(current_buffer_ptr + copy_offset, /* Use case of memcpy is verified. */ | |
| 153 | 1728083 | current_ptr + FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE, copy_size); | |
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | /* Decrease the logs_remaining counter. */ | ||
| 158 | 1799816 | logs_remaining--; | |
| 159 | |||
| 160 | /* Move to start position of next log entry. */ | ||
| 161 | 1799816 | current_ptr += log_len; | |
| 162 | } | ||
| 163 | |||
| 164 | /* Return success. */ | ||
| 165 | 1717620 | return(FX_SUCCESS); | |
| 166 | } | ||
| 167 | |||
| 168 | #endif /* FX_ENABLE_FAULT_TOLERANT */ | ||
| 169 | |||
| 170 |