1
10
13
14
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
124
125
126
127
128
129
130
131
132
133
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
157
158
159
160
162
163
164
165
167
168
169
172
173
174
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
196
197
198
203
204
205
206
207
208
209
211
212
213
214
220
221
222
223
224
225
226
227
228
229
230
231
232
233
235
236
237
238
240
241
242
245
246
247
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_utility.h"
#ifdef FX_ENABLE_FAULT_TOLERANT
#include "fx_fault_tolerant.h"
#endif
...
...
UINT _fx_utility_FAT_entry_write(FX_MEDIA *media_ptr, ULONG cluster, ULONG next_cluster)
{
UINT status, index, i;
FX_FAT_CACHE_ENTRY *cache_entry_ptr;
#ifdef FX_ENABLE_FAULT_TOLERANT
ULONG FAT_sector;
if (media_ptr -> fx_media_fault_tolerant_enabled &&
(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED))
{
if (!(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN))
{
return(_fx_fault_tolerant_add_FAT_log(media_ptr, cluster, next_cluster));
}if (!(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN)) { ... }
FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cluster);
if (FAT_sector != media_ptr -> fx_media_fault_tolerant_cached_FAT_sector)
{
if (media_ptr -> fx_media_fault_tolerant_cached_FAT_sector)
{
_fx_utility_FAT_flush(media_ptr);
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
_fx_utility_exFAT_bitmap_flush(media_ptr);
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
}if (media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { ... }
media_ptr -> fx_media_fault_tolerant_cached_FAT_sector = FAT_sector;
}if (FAT_sector != media_ptr -> fx_media_fault_tolerant_cached_FAT_sector) { ... }
}if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED)) { ... }
/* ... */#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_fat_entry_writes++;
media_ptr -> fx_media_fat_entry_cache_write_hits++;/* ... */
#endif
FX_UTILITY_FAT_ENTRY_WRITE_EXTENSION
index = (cluster & FX_FAT_CACHE_HASH_MASK) * FX_FAT_CACHE_DEPTH;
cache_entry_ptr = &media_ptr -> fx_media_fat_cache[index];
for (i = 0; i < FX_FAT_CACHE_DEPTH; i++)
{
if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster)
{
/* ... */
(cache_entry_ptr + i) -> fx_fat_cache_entry_value = next_cluster;
(cache_entry_ptr + i) -> fx_fat_cache_entry_dirty = 1;
/* ... */
if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER))
{
/* ... */
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_release_sectors_requests++;/* ... */
#endif
/* ... */
media_ptr -> fx_media_driver_request = FX_DRIVER_RELEASE_SECTORS;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_logical_sector = (media_ptr -> fx_media_data_sector_start +
((cluster - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster));
media_ptr -> fx_media_driver_sectors = media_ptr -> fx_media_sectors_per_cluster;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_RELEASE_SECTORS, media_ptr, media_ptr -> fx_media_driver_logical_sector, media_ptr -> fx_media_driver_sectors, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry)(media_ptr);
}if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { ... }
return(FX_SUCCESS);
}if (((cache_entry_ptr + i) -> fx_fat_cache_entry_cluster) == cluster) { ... }
}for (i = 0; i < FX_FAT_CACHE_DEPTH; i++) { ... }
/* ... */
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_fat_entry_cache_write_hits--;
media_ptr -> fx_media_fat_entry_cache_write_misses++;/* ... */
#endif
if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty == 1)
{
/* ... */
status = _fx_utility_FAT_flush(media_ptr);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_fat_cache[index + 3].fx_fat_cache_entry_dirty == 1) { ... }
*(cache_entry_ptr + 3) = *(cache_entry_ptr + 2);
*(cache_entry_ptr + 2) = *(cache_entry_ptr + 1);
*(cache_entry_ptr + 1) = *(cache_entry_ptr);
cache_entry_ptr -> fx_fat_cache_entry_dirty = 1;
cache_entry_ptr -> fx_fat_cache_entry_cluster = cluster;
cache_entry_ptr -> fx_fat_cache_entry_value = next_cluster;
/* ... */
if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER))
{
/* ... */
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_release_sectors_requests++;/* ... */
#endif
/* ... */
media_ptr -> fx_media_driver_request = FX_DRIVER_RELEASE_SECTORS;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_logical_sector = (media_ptr -> fx_media_data_sector_start +
((cluster - FX_FAT_ENTRY_START) * media_ptr -> fx_media_sectors_per_cluster));
media_ptr -> fx_media_driver_sectors = media_ptr -> fx_media_sectors_per_cluster;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_RELEASE_SECTORS, media_ptr, media_ptr -> fx_media_driver_logical_sector, media_ptr -> fx_media_driver_sectors, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry)(media_ptr);
}if ((media_ptr -> fx_media_driver_free_sector_update) && (next_cluster == FX_FREE_CLUSTER)) { ... }
return(FX_SUCCESS);
}{ ... }