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
95
96
97
98
99
100
101
106
107
108
109
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
155
156
157
158
159
160
161
162
163
165
166
167
168
169
174
175
176
177
178
184
185
186
187
188
189
190
191
192
193
194
195
196
197
204
205
206
207
208
209
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
233
234
235
236
237
238
239
240
241
242
243
244
246
247
248
249
251
252
253
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
302
303
304
305
306
307
308
310
311
312
316
317
318
322
323
324
325
326
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
352
353
354
355
356
357
358
359
360
361
362
363
370
371
372
373
374
375
376
377
378
380
381
382
386
387
388
392
393
394
395
396
397
402
403
404
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
...
...
...
#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_logical_sector_write(FX_MEDIA *media_ptr, ULONG64 logical_sector,
VOID *buffer_ptr, ULONG sectors, UCHAR sector_type)
{
#ifndef FX_DISABLE_CACHE
FX_CACHED_SECTOR *cache_entry;
UINT cache_size;
UINT index;
UINT i;
UCHAR cache_found = FX_FALSE;/* ... */
#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
if (sector_type == FX_FAT_SECTOR)
{
media_ptr -> fx_media_fat_sector_writes++;
}if (sector_type == FX_FAT_SECTOR) { ... }
media_ptr -> fx_media_logical_sector_writes++;/* ... */
#endif
FX_UTILITY_LOGICAL_SECTOR_WRITE_EXTENSION
#ifndef FX_DISABLE_CACHE
if ((((UCHAR *)buffer_ptr) >= media_ptr -> fx_media_memory_buffer) &&
(((UCHAR *)buffer_ptr) <= media_ptr -> fx_media_sector_cache_end))
{
if (media_ptr -> fx_media_sector_cache_hashed)
{
index = (ULONG)(logical_sector & media_ptr -> fx_media_sector_cache_hash_mask) * FX_SECTOR_CACHE_DEPTH;
cache_entry = &(media_ptr -> fx_media_sector_cache[index]);
for (i = 0; i < FX_SECTOR_CACHE_DEPTH; i++, cache_entry++)
{
/* ... */
if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector == logical_sector))
{
cache_found = FX_TRUE;
break;
}if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector == logical_sector)) { ... }
}for (i = 0; i < FX_SECTOR_CACHE_DEPTH; i++, cache_entry++) { ... }
}if (media_ptr -> fx_media_sector_cache_hashed) { ... }
else
{
cache_size = media_ptr -> fx_media_sector_cache_size;
cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
/* ... */
while (cache_size--)
{
if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector == logical_sector))
{
cache_found = FX_TRUE;
break;
}if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector == logical_sector)) { ... }
if (cache_entry -> fx_cached_sector_next_used)
{
cache_entry = cache_entry -> fx_cached_sector_next_used;
}if (cache_entry -> fx_cached_sector_next_used) { ... }
}while (cache_size--) { ... }
}else { ... }
#ifdef FX_ENABLE_FAULT_TOLERANT
if (media_ptr -> fx_media_fault_tolerant_enabled &&
(media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED) &&
(sector_type == FX_DATA_SECTOR) &&
!(cache_found && (cache_entry -> fx_cached_sector_memory_buffer == buffer_ptr)))
{
if (cache_found)
{
cache_entry -> fx_cached_sector_valid = FX_FALSE;
cache_found = FX_FALSE;
}if (cache_found) { ... }
cache_size = media_ptr -> fx_media_sector_cache_size;
cache_entry = media_ptr -> fx_media_sector_cache_list_ptr;
/* ... */
while (cache_size--)
{
if ((cache_entry -> fx_cached_sector_valid) &&
(cache_entry -> fx_cached_sector_memory_buffer == buffer_ptr))
{
cache_entry -> fx_cached_sector_valid = FX_FALSE;
break;
}if ((cache_entry -> fx_cached_sector_valid) && (cache_entry -> fx_cached_sector_memory_buffer == buffer_ptr)) { ... }
if (cache_entry -> fx_cached_sector_next_used)
{
cache_entry = cache_entry -> fx_cached_sector_next_used;
}if (cache_entry -> fx_cached_sector_next_used) { ... }
}while (cache_size--) { ... }
}if (media_ptr -> fx_media_fault_tolerant_enabled && (media_ptr -> fx_media_fault_tolerant_state & FX_FAULT_TOLERANT_STATE_STARTED) && (sector_type == FX_DATA_SECTOR) && !(cache_found && (cache_entry -> fx_cached_sector_memory_buffer == buffer_ptr))) { ... }
/* ... */#endif
if (cache_found)
{
#ifdef FX_FAULT_TOLERANT
/* ... */
if (sector_type != FX_DATA_SECTOR)
{
/* ... */
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_write_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = cache_entry -> fx_cached_sector_memory_buffer;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = logical_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector;
#endif
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = sector_type;
/* ... */
media_ptr -> fx_media_driver_system_write = FX_TRUE;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, logical_sector, 1, cache_entry -> fx_cached_sector_memory_buffer, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
return(media_ptr -> fx_media_driver_status);
}if (sector_type != FX_DATA_SECTOR) { ... }
/* ... */#endif
if (cache_entry -> fx_cached_sector_buffer_dirty == FX_FALSE)
{
media_ptr -> fx_media_sector_cache_dirty_count++;
cache_entry -> fx_cached_sector_buffer_dirty = FX_TRUE;
}if (cache_entry -> fx_cached_sector_buffer_dirty == FX_FALSE) { ... }
/* ... */
return(FX_SUCCESS);
}if (cache_found) { ... }
/* ... */
if ((logical_sector == 0) || (logical_sector == ((ULONG)0xFFFFFFFF)))
{
return(FX_SECTOR_INVALID);
}if ((logical_sector == 0) || (logical_sector == ((ULONG)0xFFFFFFFF))) { ... }
if ((logical_sector + sectors - 1) >= media_ptr -> fx_media_total_sectors)
{
return(FX_SECTOR_INVALID);
}if ((logical_sector + sectors - 1) >= media_ptr -> fx_media_total_sectors) { ... }
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_write_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = buffer_ptr;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = logical_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector;
#endif
media_ptr -> fx_media_driver_sectors = sectors;
media_ptr -> fx_media_driver_sector_type = sector_type;
if (sector_type != FX_DATA_SECTOR)
{
/* ... */
media_ptr -> fx_media_driver_system_write = FX_TRUE;
}if (sector_type != FX_DATA_SECTOR) { ... }
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, logical_sector, sectors, buffer_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
if (media_ptr -> fx_media_driver_status)
{
/* ... */
return(media_ptr -> fx_media_driver_status);
}if (media_ptr -> fx_media_driver_status) { ... }
return(FX_SUCCESS);
}if ((((UCHAR *)buffer_ptr) >= media_ptr -> fx_media_memory_buffer) && (((UCHAR *)buffer_ptr) <= media_ptr -> fx_media_sector_cache_end)) { ... }
else
#endif
{
/* ... */
if ((logical_sector == 0) || (logical_sector == ((ULONG)0xFFFFFFFF)))
{
return(FX_SECTOR_INVALID);
}if ((logical_sector == 0) || (logical_sector == ((ULONG)0xFFFFFFFF))) { ... }
if ((logical_sector + sectors - 1) >= media_ptr -> fx_media_total_sectors)
{
return(FX_SECTOR_INVALID);
}if ((logical_sector + sectors - 1) >= media_ptr -> fx_media_total_sectors) { ... }
_fx_utility_logical_sector_flush(media_ptr, logical_sector, (ULONG64) sectors, FX_TRUE);
#ifdef FX_DISABLE_CACHE
if ((logical_sector <= media_ptr -> fx_media_memory_buffer_sector) && (logical_sector + sectors >= media_ptr -> fx_media_memory_buffer_sector))
{
media_ptr -> fx_media_memory_buffer_sector = (ULONG64)-1;
}if ((logical_sector <= media_ptr -> fx_media_memory_buffer_sector) && (logical_sector + sectors >= media_ptr -> fx_media_memory_buffer_sector)) { ... }
/* ... */#endif
#ifndef FX_MEDIA_STATISTICS_DISABLE
media_ptr -> fx_media_driver_write_requests++;/* ... */
#endif
media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = buffer_ptr;
#ifdef FX_DRIVER_USE_64BIT_LBA
media_ptr -> fx_media_driver_logical_sector = logical_sector;
#else
media_ptr -> fx_media_driver_logical_sector = (ULONG)logical_sector;
#endif
media_ptr -> fx_media_driver_sectors = sectors;
media_ptr -> fx_media_driver_sector_type = sector_type;
if (sector_type != FX_DATA_SECTOR)
{
/* ... */
media_ptr -> fx_media_driver_system_write = FX_TRUE;
}if (sector_type != FX_DATA_SECTOR) { ... }
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, logical_sector, sectors, buffer_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
media_ptr -> fx_media_driver_system_write = FX_FALSE;
return(media_ptr -> fx_media_driver_status);
}else { ... }
}{ ... }