Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
...
Files
loading...
SourceVuSTM32 Libraries and Samplesfilexcommon/src/fx_fault_tolerant_cleanup_FAT_chain.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Fault Tolerant */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE #include "fx_api.h" #include "fx_utility.h" #include "fx_fault_tolerant.h" #ifdef FX_ENABLE_FAULT_TOLERANT /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_fault_tolerant_cleanup_FAT_chain PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function cleans up FAT chain. In order to prevent failures */ /* during the link-list walk, and to improve efficiency, this function */ /* frees the FAT entry chain in sections. For each section, it frees */ /* FAT entry from the last entry in the chain and works backwards. */ /* Using this method, if the system fails in the middle of the free */ /* operation, the head is preserved, and fault tolerant module can */ /* "redo" this operation next time it starts up. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* operation Recover a failure operation or*/ /* cleanup old FAT chain */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_exFAT_cluster_state_get Get state of exFAT cluster */ /* _fx_utility_exFAT_cluster_state_set Set state of exFAT cluster */ /* _fx_utility_FAT_entry_read Read a FAT entry */ /* _fx_utility_FAT_entry_write Write a FAT entry */ /* _fx_utility_16_unsigned_write Write a USHORT from memory */ /* _fx_utility_32_unsigned_write Write a ULONG from memory */ /* _fx_utility_32_unsigned_read Read a ULONG from memory */ /* _fx_utility_exFAT_bitmap_flush Flush exFAT allocation bitmap */ /* _fx_utility_FAT_flush Flush written FAT entries */ /* _fx_fault_tolerant_calculate_checksum Compute Checksum of data */ /* _fx_fault_tolerant_write_log_file Write log file */ /* */ /* CALLED BY */ /* */ /* _fx_fault_tolerant_apply_logs */ /* _fx_fault_tolerant_recover */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_fault_tolerant_cleanup_FAT_chain(FX_MEDIA *media_ptr, UINT operation) { UINT status; ULONG current_cluster; ULONG next_cluster = 0; ULONG head_cluster; ULONG tail_cluster; ULONG cache_count; ULONG cache_max = FX_FAULT_TOLERANT_CACHE_SIZE >> 2; ULONG *cache_ptr = media_ptr -> fx_media_fault_tolerant_cache; ULONG next_session; USHORT checksum; ULONG i; FX_FAULT_TOLERANT_FAT_CHAIN *FAT_chain; ULONG FAT_sector; ULONG last_FAT_sector; /* Set FAT chain pointer. */ FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET); /* Get head and tail cluster. */ if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { /* Recover phase. Cleanup (remove) new FAT chain. */ head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new); }if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { ... } else { /* Cleanup phase. Cleanup (remove) old FAT chain. */ head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original); }else { ... } /* At this point, the head_cluster points to the cluster chain that is to be removed. */ /* Tail cluster points to the back of the origianl FAT chain where the new chain is attached to. The remove process terminates once this cluster is encountered. *//* ... */ tail_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_back); /* Are there any clusters to free? */ if ((head_cluster < FX_FAT_ENTRY_START) || (head_cluster >= media_ptr -> fx_media_fat_reserved)) { return(FX_SUCCESS); }if ((head_cluster < FX_FAT_ENTRY_START) || (head_cluster >= media_ptr -> fx_media_fat_reserved)) { ... } /* To opimize the deletion process of a long FAT chain, the deletion is done in sessions. During one session, a set of FAT entries are removed. If one session is interrupted, after restart the deletion process resumes and picks up from where it left. During one deletion session, all FAT entries are between head_cluster(inclusive) and next_session (exclusive). *//* ... */ next_session = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion); /* Loop to cleanup all FAT entries. */ while ((head_cluster >= FX_FAT_ENTRY_START) && (head_cluster < media_ptr -> fx_media_fat_reserved)) { /* Initialize. */ current_cluster = head_cluster; cache_count = 0; /* Loop from head cluster to find entries to cleanup and store this information into cache. */ do { /* Get the next FAT entry. */ status = _fx_utility_FAT_entry_read(media_ptr, current_cluster, &next_cluster); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Whether or not the cluster is occupied. */ if (next_cluster == FX_FREE_CLUSTER) { /* Current cluster has already been released. */ break; }if (next_cluster == FX_FREE_CLUSTER) { ... } /* Cache the FAT list. */ cache_ptr[cache_count++] = current_cluster; /* Move to next cluster. */ current_cluster = next_cluster; ...} while ((next_cluster >= FX_FAT_ENTRY_START) && (next_cluster < media_ptr -> fx_media_fat_reserved) && (next_cluster != tail_cluster) && (cache_count < cache_max)); /* Get next session. */ if (cache_count == cache_max) { next_session = next_cluster; }if (cache_count == cache_max) { ... } /* Update head cluster and next session into log file. */ _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion, next_session); if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new, head_cluster); }if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { ... } else { _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original, head_cluster); }else { ... } /* Update checksum. */ _fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, 0); checksum = _fx_fault_tolerant_calculate_checksum((UCHAR *)FAT_chain, FX_FAULT_TOLERANT_FAT_CHAIN_SIZE); _fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, checksum); /* Flush the fault tolerant log into the file system. */ _fx_fault_tolerant_write_log_file(media_ptr, 0); /* Loop to free FAT in cache from back to front. */ if (cache_count > 0) { /* Get sector of last FAT entry. */ last_FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cache_ptr[cache_count - 1]); i = cache_count; while (i > 0) { i--; /* Get sector of current FAT entry. */ FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cache_ptr[i]); if (FAT_sector != last_FAT_sector) { /* Current FAT entry is located in different sector. Force flush. */ /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr); #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Flush exFAT bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Update sector of current FAT entry. */ last_FAT_sector = FAT_sector; }if (FAT_sector != last_FAT_sector) { ... } /* Free current_cluster. */ status = _fx_utility_FAT_entry_write(media_ptr, cache_ptr[i], FX_FREE_CLUSTER); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Free bitmap. */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, cache_ptr[i], FX_EXFAT_BITMAP_CLUSTER_FREE); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ }while (i > 0) { ... } /* Increase the available clusters in the media control block. */ media_ptr -> fx_media_available_clusters += cache_count; }if (cache_count > 0) { ... } /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr); #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Flush exFAT bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Get head cluster. */ if (head_cluster == next_session) { break; }if (head_cluster == next_session) { ... } head_cluster = next_session; }while ((head_cluster >= FX_FAT_ENTRY_START) && (head_cluster < media_ptr -> fx_media_fat_reserved)) { ... } return(FX_SUCCESS); }_fx_fault_tolerant_cleanup_FAT_chain (FX_MEDIA *media_ptr, UINT operation) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.