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 /**   Media                                                               */
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_media.h"
29 #include "fx_utility.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _fx_media_cache_invalidate                          PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    William E. Lamie, Microsoft Corporation                             */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function examines the logical cache, flushing written sectors  */
45 /*    out to the media and invalidating all sectors in the logical cache. */
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_FAT_flush                 Flush cached FAT entries      */
58 /*    _fx_utility_FAT_map_flush             Flush primary FAT changes to  */
59 /*                                            secondary FAT(s)            */
60 /*    _fx_utility_logical_sector_flush      Flush logical sector cache    */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
71 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_fx_media_cache_invalidate(FX_MEDIA * media_ptr)75 UINT  _fx_media_cache_invalidate(FX_MEDIA *media_ptr)
76 {
77 
78 UINT status;
79 UINT i;
80 
81 
82 #ifndef FX_MEDIA_STATISTICS_DISABLE
83 
84     /* Increment the number of times this service has been called.  */
85     media_ptr -> fx_media_flushes++;
86 #endif
87 
88     /* Check the media to make sure it is open.  */
89     if (media_ptr -> fx_media_id != FX_MEDIA_ID)
90     {
91 
92         /* Return the media not opened error.  */
93         return(FX_MEDIA_NOT_OPEN);
94     }
95 
96     /* If trace is enabled, insert this event into the trace buffer.  */
97     FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_CACHE_INVALIDATE, media_ptr, 0, 0, 0, FX_TRACE_MEDIA_EVENTS, 0, 0)
98 
99     /* Protect against other threads accessing the media.  */
100     FX_PROTECT
101     /* Flush the cached individual FAT entries */
102     _fx_utility_FAT_flush(media_ptr);
103 
104     /* Flush changed sector(s) in the primary FAT to secondary FATs.  */
105     _fx_utility_FAT_map_flush(media_ptr);
106 
107     /* Clear the FAT cache entry array.  */
108     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
109     {
110 
111         /* Clear entry in the FAT cache.  */
112         media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster =   0;
113         media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value   =   0;
114     }
115 
116     /* Clear the secondary FAT update map.  */
117     for (i = 0; i < FX_FAT_MAP_SIZE; i++)
118     {
119 
120         /* Clear bit map entry for secondary FAT update.  */
121         media_ptr -> fx_media_fat_secondary_update_map[i] =  0;
122     }
123 
124     /* Call the logical sector flush to invalidate the logical sector cache.  */
125     status =  _fx_utility_logical_sector_flush(media_ptr, ((ULONG64) 1), (ULONG64) (media_ptr -> fx_media_total_sectors), FX_TRUE);
126 
127     /* Release media protection.  */
128     FX_UNPROTECT
129 
130     /* If we get here, return successful status to the caller.  */
131     return(status);
132 }
133 
134