1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** FileX Component */
16 /** */
17 /** Utility */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define FX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "fx_api.h"
28 #include "fx_system.h"
29 #include "fx_utility.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _fx_utility_FAT_map_flush PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* William E. Lamie, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function updates mirrors changes in the primary FAT to each of */
45 /* secondary FATs in the media. */
46 /* */
47 /* INPUT */
48 /* */
49 /* media_ptr Media control block pointer */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* return status */
54 /* */
55 /* CALLS */
56 /* */
57 /* _fx_utility_logical_sector_read Read FAT sector into memory */
58 /* _fx_utility_logical_sector_write Write FAT sector back to disk */
59 /* */
60 /* CALLED BY */
61 /* */
62 /* FileX System Functions */
63 /* */
64 /* RELEASE HISTORY */
65 /* */
66 /* DATE NAME DESCRIPTION */
67 /* */
68 /* 05-19-2020 William E. Lamie Initial Version 6.0 */
69 /* 09-30-2020 William E. Lamie Modified comment(s), */
70 /* resulting in version 6.1 */
71 /* */
72 /**************************************************************************/
_fx_utility_FAT_map_flush(FX_MEDIA * media_ptr)73 UINT _fx_utility_FAT_map_flush(FX_MEDIA *media_ptr)
74 {
75
76 ULONG FAT_sector, last_sector;
77 UINT i, status, FATs;
78 UCHAR sectors_per_bit;
79
80
81 /* Determine how many FAT sectors each bit in the bit map represents. Depending on
82 the number of sectors in the primary FAT, each bit in this map may represent one
83 or more primary FAT sectors. Because of this, it is possible some FAT sectors that
84 were not changed may get flushed out to the secondary FAT. However, this method
85 provides very nice performance benefits during normal operation and is much more
86 reasonable than performing a total copy of the primary FAT to each secondary FAT
87 on media flush and media close. */
88 if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0)
89 {
90 sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3));
91 }
92 else
93 {
94 sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1);
95 }
96
97 /* Loop through the FAT update map to mirror primary FAT sectors to secondary FAT(s). */
98 for (i = 0; i < FX_FAT_MAP_SIZE << 3; i++)
99 {
100
101 /* Determine if there are FAT changes specified by this entry. */
102 if ((media_ptr -> fx_media_fat_secondary_update_map[i >> 3] & (1 << (i & 7))) == 0)
103 {
104
105 /* No, look at the next bit map entry. */
106 continue;
107 }
108
109 /* Setup the parameters for performing the update. */
110 FAT_sector = i * sectors_per_bit + media_ptr -> fx_media_reserved_sectors;
111 last_sector = FAT_sector + sectors_per_bit;
112
113 /* Make sure the last update sector is within range. */
114 if (last_sector > (media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors))
115 {
116 last_sector = media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors;
117 }
118
119 /* Loop to mirror primary FAT sectors to secondary FAT(s). */
120 for (; FAT_sector < last_sector; FAT_sector++)
121 {
122
123 /* Read the FAT sector. */
124 status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
125 media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
126
127 /* Determine if an error occurred. */
128 if (status != FX_SUCCESS)
129 {
130 /* Return the error status. */
131 return(status);
132 }
133
134 /* Pickup how many secondary FATs there are. */
135 FATs = media_ptr -> fx_media_number_of_FATs - 1;
136
137 /* Loop to update additional FAT entries. */
138 while (FATs)
139 {
140
141 /* Mirror main FAT sector write into the additional FATs. */
142 status = _fx_utility_logical_sector_write(media_ptr,
143 ((ULONG64) FAT_sector) + ((ULONG64)FATs * (ULONG64)(media_ptr -> fx_media_sectors_per_FAT)),
144 media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
145
146 /* Determine if an error occurred. */
147 if (status != FX_SUCCESS)
148 {
149
150 /* Return the error status. */
151 return(status);
152 }
153
154 /* Decrement the number of FATs. */
155 FATs--;
156 }
157 }
158 }
159
160 /* Clear the bit map that indicates primary FAT updates. */
161 for (i = 0; i < FX_FAT_MAP_SIZE; i++)
162 {
163
164 /* Clear each entry in the bit map. */
165 media_ptr -> fx_media_fat_secondary_update_map[i] = 0;
166 }
167
168 /* Return a successful completion. */
169 return(FX_SUCCESS);
170 }
171
172