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 /**   Fault Tolerant                                                      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define FX_SOURCE_CODE
23 
24 #include "fx_api.h"
25 #include "fx_utility.h"
26 #include "fx_directory.h"
27 #include "fx_fault_tolerant.h"
28 
29 
30 #ifdef FX_ENABLE_FAULT_TOLERANT
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _fx_fault_tolerant_write_log_file                   PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function writes data of one sector of fault tolerant data      */
44 /*    from memory to log file in file system.                             */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    media_ptr                             Media control block pointer   */
49 /*    relative_sector                       Relative sector number of the */
50 /*                                          log file to write to          */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    return status                                                       */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _fx_utility_logical_sector_write      Write a logical sector        */
59 /*    _fx_utility_logical_sector_flush      Flush written logical sectors */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _fx_fault_tolerant_cleanup_FAT_chain                                */
64 /*    _fx_fault_tolerant_reset_log_file                                   */
65 /*    _fx_fault_tolerant_set_FAT_chain                                    */
66 /*    _fx_fault_tolerant_transaction_end                                  */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
73 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_fx_fault_tolerant_write_log_file(FX_MEDIA * media_ptr,ULONG relative_sector)77 UINT _fx_fault_tolerant_write_log_file(FX_MEDIA *media_ptr, ULONG relative_sector)
78 {
79 UINT  status;
80 ULONG start_sector;
81 
82     /* Calculate the start sector of log file. */
83     start_sector = (media_ptr -> fx_media_fault_tolerant_start_cluster - FX_FAT_ENTRY_START) *
84                    media_ptr -> fx_media_sectors_per_cluster +
85                    media_ptr -> fx_media_data_sector_start;
86 
87 
88     /* Write sector directly. */
89     status =  _fx_utility_logical_sector_write(media_ptr, (ULONG64) (start_sector + relative_sector),
90                                                media_ptr -> fx_media_fault_tolerant_memory_buffer +
91                                                media_ptr -> fx_media_bytes_per_sector * relative_sector,
92                                                ((ULONG) 1), FX_DATA_SECTOR);
93 
94     /* Check for a bad status.  */
95     if (status != FX_SUCCESS)
96     {
97 
98         /* Return the bad status.  */
99         return(status);
100     }
101 
102     /* Flush the internal logical sector cache.  */
103     status =  _fx_utility_logical_sector_flush(media_ptr, (ULONG64) start_sector, ((ULONG64) 1), FX_FALSE);
104 
105     FX_FAULT_TOLERANT_WRITE_LOG_FILE_EXTENSION
106 
107     /* Return the status.  */
108     return(status);
109 }
110 #endif /* FX_ENABLE_FAULT_TOLERANT */
111 
112