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_reset_log_file                   PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    William E. Lamie, Microsoft Corporation                             */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function removes all log entries from the log file, and        */
44 /*    it to its initial state.                                            */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    media_ptr                             Media control block pointer   */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    return status                                                       */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _fx_utility_16_unsigned_write         Write a USHORT from memory    */
57 /*    _fx_utility_32_unsigned_write         Write a ULONG from memory     */
58 /*    _fx_fault_tolerant_calculate_checksum Compute Checksum of data      */
59 /*    _fx_fault_tolerant_write_log_file     Write log file                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _fx_directory_attributes_set                                        */
64 /*    _fx_directory_create                                                */
65 /*    _fx_directory_delete                                                */
66 /*    _fx_directory_rename                                                */
67 /*    _fx_file_allocate                                                   */
68 /*    _fx_file_attributes_set                                             */
69 /*    _fx_file_best_effort_allocate                                       */
70 /*    _fx_file_create                                                     */
71 /*    _fx_file_delete                                                     */
72 /*    _fx_file_rename                                                     */
73 /*    _fx_file_truncate                                                   */
74 /*    _fx_file_truncate_release                                           */
75 /*    _fx_file_write                                                      */
76 /*    _fx_unicode_directory_create                                        */
77 /*    _fx_unicode_file_create                                             */
78 /*    _fx_fault_tolerant_enable                                           */
79 /*    _fx_fault_tolerant_transaction_end                                  */
80 /*                                                                        */
81 /*  RELEASE HISTORY                                                       */
82 /*                                                                        */
83 /*    DATE              NAME                      DESCRIPTION             */
84 /*                                                                        */
85 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
86 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
87 /*                                            resulting in version 6.1    */
88 /*                                                                        */
89 /**************************************************************************/
90 
_fx_fault_tolerant_reset_log_file(FX_MEDIA * media_ptr)91 UINT _fx_fault_tolerant_reset_log_file(FX_MEDIA *media_ptr)
92 {
93 UINT                           status;
94 USHORT                         checksum;
95 FX_FAULT_TOLERANT_LOG_HEADER  *log_header;
96 FX_FAULT_TOLERANT_FAT_CHAIN   *FAT_chain;
97 FX_FAULT_TOLERANT_LOG_CONTENT *log_content;
98 
99     /* Set log header, FAT chain and log content pointer. */
100     log_header = (FX_FAULT_TOLERANT_LOG_HEADER *)media_ptr -> fx_media_fault_tolerant_memory_buffer;
101     FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer +
102                                                 FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET);
103     log_content = (FX_FAULT_TOLERANT_LOG_CONTENT *)(media_ptr -> fx_media_fault_tolerant_memory_buffer +
104                                                     FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET);
105 
106     /* Reset the log file header. */
107     _fx_utility_32_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_id, FX_FAULT_TOLERANT_ID);
108     _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_total_size,
109                                   (FX_FAULT_TOLERANT_LOG_HEADER_SIZE + FX_FAULT_TOLERANT_FAT_CHAIN_SIZE));
110     _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_checksum, 0);
111     log_header -> fx_fault_tolerant_log_header_version_major = FX_FAULT_TOLERANT_VERSION_MAJOR;
112     log_header -> fx_fault_tolerant_log_header_version_minor = FX_FAULT_TOLERANT_VERSION_MINOR;
113     _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_reserved, 0);
114     checksum = _fx_fault_tolerant_calculate_checksum((UCHAR *)log_header, FX_FAULT_TOLERANT_LOG_HEADER_SIZE);
115     _fx_utility_16_unsigned_write((UCHAR *)&log_header -> fx_fault_tolerant_log_header_checksum, checksum);
116 
117     /* Reset the undo log section. */
118     _fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, 0xFFFF);
119     FAT_chain -> fx_fault_tolerant_FAT_chain_flag = 0;
120     FAT_chain -> fx_fault_tolerant_FAT_chain_reserved = 0;
121     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_front, 0);
122     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new, 0);
123     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original, 0);
124     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_back, 0);
125     _fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion, 0);
126 
127     /* Reset log content header. */
128     _fx_utility_16_unsigned_write((UCHAR *)&log_content -> fx_fault_tolerant_log_content_checksum, 0xFFFF);
129     _fx_utility_16_unsigned_write((UCHAR *)&log_content -> fx_fault_tolerant_log_content_count, 0xFFFF);
130 
131     /* No matter success or fail, close this transaction. */
132     media_ptr -> fx_media_fault_tolerant_state = FX_FAULT_TOLERANT_STATE_IDLE;
133 
134     /* Write the log header and FAT chain.  */
135     status =  _fx_fault_tolerant_write_log_file(media_ptr, 0);
136 
137     /* Return the status.  */
138     return(status);
139 }
140 #endif /* FX_ENABLE_FAULT_TOLERANT */
141 
142