GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_utility_FAT_map_flush.c Lines: 27 27 100.0 %
Date: 2024-03-11 05:15:45 Branches: 18 18 100.0 %

Line Branch Exec Source
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
/**************************************************************************/
73
340691
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
340691
    if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0)
89
    {
90
4034
        sectors_per_bit =  (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3));
91
    }
92
    else
93
    {
94
336657
        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
3066195
    for (i = 0; i < FX_FAT_MAP_SIZE << 3; i++)
99
    {
100
101
        /* Determine if there are FAT changes specified by this entry.  */
102
2725507
        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
2490208
            continue;
107
        }
108
109
        /* Setup the parameters for performing the update.  */
110
235299
        FAT_sector =    i * sectors_per_bit + media_ptr -> fx_media_reserved_sectors;
111
235299
        last_sector =   FAT_sector + sectors_per_bit;
112
113
        /* Make sure the last update sector is within range.  */
114
235299
        if (last_sector > (media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors))
115
        {
116
15
            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
3529382
        for (; FAT_sector < last_sector; FAT_sector++)
121
        {
122
123
            /* Read the FAT sector.  */
124
3294086
            status =  _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector,
125
3294086
                                                      media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
126
127
            /* Determine if an error occurred.  */
128
3294086
            if (status != FX_SUCCESS)
129
            {
130
                /* Return the error status.  */
131
2
                return(status);
132
            }
133
134
            /* Pickup how many secondary FATs there are.  */
135
3294084
            FATs =  media_ptr -> fx_media_number_of_FATs - 1;
136
137
            /* Loop to update additional FAT entries.  */
138
6456936
            while (FATs)
139
            {
140
141
                /* Mirror main FAT sector write into the additional FATs.  */
142
3162853
                status =  _fx_utility_logical_sector_write(media_ptr,
143
3162853
                                                           ((ULONG64) FAT_sector) + ((ULONG64)FATs * (ULONG64)(media_ptr -> fx_media_sectors_per_FAT)),
144
3162853
                                                           media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR);
145
146
                /* Determine if an error occurred.  */
147
3162853
                if (status != FX_SUCCESS)
148
                {
149
150
                    /* Return the error status.  */
151
1
                    return(status);
152
                }
153
154
                /* Decrement the number of FATs.  */
155
3162852
                FATs--;
156
            }
157
        }
158
    }
159
160
    /* Clear the bit map that indicates primary FAT updates.  */
161
681376
    for (i = 0; i < FX_FAT_MAP_SIZE; i++)
162
    {
163
164
        /* Clear each entry in the bit map.  */
165
340688
        media_ptr -> fx_media_fat_secondary_update_map[i] =  0;
166
    }
167
168
    /* Return a successful completion.  */
169
340688
    return(FX_SUCCESS);
170
}
171