GCC Code Coverage Report


Directory: common/src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 36 / 0 / 36
Functions: 100.0% 1 / 0 / 1
Branches: 100.0% 12 / 0 / 12

fx_fault_tolerant_transaction_end.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_fault_tolerant.h"
27 #include "fx_utility.h"
28
29 #ifdef FX_ENABLE_FAULT_TOLERANT
30 /**************************************************************************/
31 /* */
32 /* FUNCTION RELEASE */
33 /* */
34 /* _fx_fault_tolerant_transaction_end PORTABLE C */
35 /* 6.1 */
36 /* AUTHOR */
37 /* */
38 /* William E. Lamie, Microsoft Corporation */
39 /* */
40 /* DESCRIPTION */
41 /* */
42 /* This function completes a file system update operation protected */
43 /* by fault tolerant. This function first computes the checksum */
44 /* for the log entries, flush the log file to the file system (a */
45 /* necessary step to record vital information for the file system to */
46 /* recover, in case the update operation fails midway). After the */
47 /* log entries are written to the physical medium, the actualy file */
48 /* system changes (FAT table, directory entries) are applied. */
49 /* */
50 /* If the file system changes are successfully applied, the log */
51 /* entries can be removed. */
52 /* */
53 /* INPUT */
54 /* */
55 /* media_ptr Media control block pointer */
56 /* */
57 /* OUTPUT */
58 /* */
59 /* return status */
60 /* */
61 /* CALLS */
62 /* */
63 /* _fx_utility_16_unsigned_write Write a USHORT from memory */
64 /* _fx_fault_tolerant_apply_logs Apply logs into file system */
65 /* _fx_fault_tolerant_calculate_checksum Compute Checksum of data */
66 /* _fx_fault_tolerant_write_log_file Write log file */
67 /* _fx_fault_tolerant_reset_log_file Reset the log file */
68 /* */
69 /* CALLED BY */
70 /* */
71 /* _fx_directory_attributes_set */
72 /* _fx_directory_create */
73 /* _fx_directory_delete */
74 /* _fx_directory_rename */
75 /* _fx_file_allocate */
76 /* _fx_file_attributes_set */
77 /* _fx_file_best_effort_allocate */
78 /* _fx_file_create */
79 /* _fx_file_delete */
80 /* _fx_file_rename */
81 /* _fx_file_truncate */
82 /* _fx_file_truncate_release */
83 /* _fx_file_write */
84 /* _fx_unicode_directory_create */
85 /* _fx_unicode_file_create */
86 /* */
87 /**************************************************************************/
88 162688481 UINT _fx_fault_tolerant_transaction_end(FX_MEDIA *media_ptr)
89 {
90 ULONG relative_sector;
91 UINT status;
92 USHORT checksum;
93 UINT offset;
94 FX_FAULT_TOLERANT_LOG_HEADER *log_header;
95 FX_FAULT_TOLERANT_LOG_CONTENT *log_content;
96
97 /* Is fault tolerant enabled? */
98
2/2
✓ Branch 2 → 3 taken 2543262 times.
✓ Branch 2 → 4 taken 160145219 times.
162688481 if (media_ptr -> fx_media_fault_tolerant_enabled == FX_FALSE)
99 {
100 2543262 return(FX_SUCCESS);
101 }
102
103 /* Decrease the transaction. */
104 160145219 media_ptr -> fx_media_fault_tolerant_transaction_count--;
105
106 /* Is transaction finished? */
107
2/2
✓ Branch 4 → 5 taken 1392 times.
✓ Branch 4 → 6 taken 160143827 times.
160145219 if (media_ptr -> fx_media_fault_tolerant_transaction_count != 0)
108 {
109 1392 return(FX_SUCCESS);
110 }
111
112 /* Close this transaction. */
113 160143827 media_ptr -> fx_media_fault_tolerant_state = FX_FAULT_TOLERANT_STATE_IDLE;
114
115 /* Set log header and FAT chain pointer. */
116 160143827 log_header = (FX_FAULT_TOLERANT_LOG_HEADER *)media_ptr -> fx_media_fault_tolerant_memory_buffer;
117 160143827 log_content = (FX_FAULT_TOLERANT_LOG_CONTENT *)(media_ptr -> fx_media_fault_tolerant_memory_buffer +
118 FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET);
119
120 /* Reset checksum field and update log entry counter field. */
121 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_content -> fx_fault_tolerant_log_content_checksum, 0);
122 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_content -> fx_fault_tolerant_log_content_count,
123 160143827 media_ptr -> fx_media_fault_tolerant_total_logs);
124
125 /* Now calculate the checksum of log content. */
126 160143827 checksum = _fx_fault_tolerant_calculate_checksum((media_ptr -> fx_media_fault_tolerant_memory_buffer +
127 FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET),
128 160143827 (media_ptr -> fx_media_fault_tolerant_file_size -
129 FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET));
130
131 /* Record checksum field of log content. */
132 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_content -> fx_fault_tolerant_log_content_checksum, checksum);
133
134 /* Record log header. */
135 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_total_size,
136 160143827 media_ptr -> fx_media_fault_tolerant_file_size);
137 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_checksum, 0);
138
139 /* Calculate checksum of the header. */
140 160143827 checksum = _fx_fault_tolerant_calculate_checksum((UCHAR *)log_header, FX_FAULT_TOLERANT_LOG_HEADER_SIZE);
141
142 /* Record checksum field of the header. */
143 160143827 _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_checksum, checksum);
144
145 /* Flush log content and flush first sector at last. The first sector contains size of total log file.
146 * The log file will contain wrong value of size when it is interrupted after first sector flushed to file system. */
147 160143827 relative_sector = 1;
148 160143827 offset = media_ptr -> fx_media_bytes_per_sector;
149
2/2
✓ Branch 19 → 15 taken 2107 times.
✓ Branch 19 → 20 taken 160143742 times.
160145849 while (offset < media_ptr -> fx_media_fault_tolerant_file_size)
150 {
151
152 /* Write back the log. */
153 2107 status = _fx_fault_tolerant_write_log_file(media_ptr, relative_sector);
154
155 /* Check for good completion status. */
156
2/2
✓ Branch 16 → 17 taken 17 times.
✓ Branch 16 → 18 taken 2022 times.
2039 if (status != FX_SUCCESS)
157 {
158
159 /* Error. */
160 17 return(status);
161 }
162
163 /* Increase relative sector and offset. */
164 2022 relative_sector++;
165 2022 offset += media_ptr -> fx_media_bytes_per_sector;
166 }
167
168 /* Flush first sector. */
169 160143742 status = _fx_fault_tolerant_write_log_file(media_ptr, 0);
170
171 /* Check for good completion status. */
172
2/2
✓ Branch 21 → 22 taken 3 times.
✓ Branch 21 → 23 taken 160143562 times.
160143565 if (status != FX_SUCCESS)
173 {
174
175 /* Error. */
176 3 return(status);
177 }
178
179 /* At this pointer, the vital information has been flushed to the physical medium.
180 Update the file system (FAT table, directory entry) using information recorded in the
181 log file. */
182 160143562 status = _fx_fault_tolerant_apply_logs(media_ptr);
183
184 /* Check for a bad status. */
185
2/2
✓ Branch 24 → 25 taken 74 times.
✓ Branch 24 → 26 taken 160143118 times.
160143192 if (status != FX_SUCCESS)
186 {
187
188 /* Return the bad status. */
189 74 return(status);
190 }
191
192 /* The file system has been updated successfully. Remove and reset the fault tolerant
193 log file. */
194 160143118 status = _fx_fault_tolerant_reset_log_file(media_ptr);
195
196 160143054 return(status);
197 }
198 #endif /* FX_ENABLE_FAULT_TOLERANT */
199
200