Background
With the development of storage technology, mass storage media are widely used in embedded systems to store relatively large data such as pictures, videos, and recording data, and these storage media include various pluggable memory cards (TF card, SD card, etc.) and a mass storage chip with a storage management function (eMMC chip). After the large-capacity storage media appear, the storage capacity and the read-write speed are both greatly improved, and meanwhile, the management controller is integrated in the chip, so that the chip has the wear-leveling capability, the problem that one sector is written to be bad due to repeated writing is avoided, and the operation of any file system on the storage media is possible, and the special file system such as a YAFFS file system is not relied on.
At the same time, these mass storage media require a suitable file system to manage. Since these storage media are no longer subject to a special file system, the generic file system FatFs can run on these storage media. Because the FatFs code is open-source and easy to transplant, needs few system resources to run, is stable and reliable, and is a common file system in an embedded system, as shown in FIG. 1, the FatFs code is in a file format.
If the FatFs is directly used in an embedded system, a defect exists, because the FatFs is not a log-type file system, the file system is damaged by sudden power failure, and the whole file system is unusable in severe cases. This is because most of the write operations are completed step by step, and if power failure occurs when the write operations are not completely completed, the operations are incomplete, which causes data confusion and damages the file system. Fig. 2 is a schematic diagram of a power loss occurring when a file is deleted. The defects of the FatFs are determined by an operation mechanism of the FatFs, logs are not recorded during write operation of the FatFs, and therefore after abnormal power failure, a file system cannot be restored according to the logs after being electrified again, so that the FatFs is unsafe in power failure and cannot be directly used in occasions requiring power failure safety, such as the fields of electric power, military industry and the like.
Disclosure of Invention
Based on the above situation in the prior art, an object of the present invention is to provide a method, a system, and a storage medium for designing a log-type embedded file system, so as to solve the problem that after an abnormal power failure occurs, the file system can be restored to a power on state according to the log, and thus the file system has the efficacy of power failure safety.
A first aspect of the present invention provides a method for designing a journaled embedded file system, where the file system includes reserved sectors and data areas, the method including the steps of:
selecting a target sector from reserved sectors of a file system as a log sector;
when the file system performs erasing/reading/writing operation, recording the operation details of each step operation in a log sector in real time;
and after the new step operation is completed, replacing the old record content in the log sector with the operation details.
Further, the record content of the log sector includes the current step sequence number and the data information required for completing the current complete operation.
Further, the data information includes a target sector number, a directory entry location, a state machine, and a log sector CRC check value.
Furthermore, when power failure occurs in a certain substep, after the system is powered on, the information of the log sector is read, the state of the previous substep is recovered, and the next substep operation is continued.
Further, two target sectors are selected from the reserved sectors to serve as log sectors, and a log is recorded in each target sector, wherein one log sector serves as a main log sector, and the other log sector serves as a standby log sector.
Further, the step of reading the log sector includes:
reading a main log sector;
judging whether the check CRC value is correct:
if the data is correct, using the recorded data of the main log sector;
if not, read backup log sector: judging whether the check CRC value is correct: if the data is correct, using the recorded data of the backup log sector; if not, the system reports an error.
Further, a target sector is selected as a log sector in a data area of the file system.
A second aspect of the present invention provides a log-type embedded file system design system, including:
a memory and one or more processors;
wherein the memory is communicatively coupled to the one or more processors and has stored therein instructions executable by the one or more processors to cause the one or more processors to perform the method of any of claims 1-7.
A third aspect of the invention provides a computer-readable storage medium having stored thereon computer-executable instructions operable, when executed by a computing device, to perform a method as previously described.
In summary, the present invention provides a method, a system and a storage medium for designing a journal type embedded file system, where the file system includes reserved sectors and data areas, and the method includes: selecting a target sector from reserved sectors of a file system as a log sector; when the file system performs erasing/reading/writing operation, recording the operation details of each step operation in a log sector in real time; and after the new step operation is completed, replacing the old recorded content in the log sector with the new step operation. The system is configured to perform the method and the storage medium is configured to store computer-executable instructions for performing the method. After the abnormal power failure, the file system can be recovered according to the log after being electrified again, so that the file system has the power failure safety effect.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention will be described in further detail with reference to the accompanying drawings in conjunction with the following detailed description. It should be understood that the description is intended to be exemplary only, and is not intended to limit the scope of the present invention. Moreover, in the following description, descriptions of well-known structures and techniques are omitted so as to not unnecessarily obscure the concepts of the present invention.
A first aspect of the present invention provides a method for designing a journaled embedded file system, where the file system includes reserved sectors and data areas, and the method includes the following steps, as shown in fig. 3:
s100, selecting a target sector from reserved sectors of a file system as a log sector;
step S200, when the file system performs erasing/reading/writing operation, recording the operation details of each step operation into a log sector in real time;
step S300, after the new step operation is completed, the old record content in the log sector is replaced with the new step operation.
Specifically, the journal embedded file system takes the FatFs file system as an example for further description.
The FatFs has open source and good stability, needs to be transformed when used in an embedded occasion requiring power failure safety, and has the characteristic of power failure safety.
As can be seen from FIG. 1, there are reserved sectors in FatFs, and a target sector is selected in the reserved sectors in FatFs to record a write operation log, which is a log sector. And when the FatFs is in write operation, the operation details of each step operation are written into the log sector in real time, the content of the log sector always stores the latest operation details of the step, and the operation details of the historical steps are not reserved. When the new write operation is completed in steps, the old log sector record content is flushed by the new record content.
The log sector record content comprises the current step sequence number and the data information required for completing the current complete write operation, the log sector record content is shown in table 1, and the data information comprises a target sector number, a directory entry position, a state machine and a log sector CRC check value.
Table 1 log sector record content
Furthermore, when power failure occurs in a certain substep, after the system is powered on, the information of the log sector is read, the state of the previous substep is recovered, and the next substep operation is continued. Specifically, the power failure shown in fig. 2 occurs in the FatFs file system after the log is added, and the operation steps are shown in fig. 4. Since power failure occurs in the step of "modifying cluster records in the FAT table", the "flush log" preceding the step of "modifying cluster records in the FAT table" is recorded and stored in the log sector. After the system is powered on again, the information stored in the log sector is read, and according to the record information of the log sector, it is known that power failure occurs when the file is deleted and the substep 2 (modifying the cluster record in the FAT table) is performed, so that two remaining substeps of "modifying the cluster record in the FAT table" and "modifying the remaining cluster number of the FAT table" need to be continued, so that the operation of deleting the file is complete. When two remaining step operations are continued, logs are also recorded, so that incomplete operation caused by power failure in the repair process is prevented.
Further, two target sectors are selected from the reserved sectors to serve as log sectors, and a log is recorded in each target sector, wherein one log sector serves as a main log sector, and the other log sector serves as a standby log sector. In view of the fact that log sector data is important, in order to guarantee reliability of log record contents, a method of recording double logs is adopted, two sectors are found in a reserved sector of FatFs, each sector records one log, one sector is a main log sector, the other sector is a standby sector, when the logs are refreshed, contents of the two sectors are sequentially refreshed in real time, the contents are the same, and when the contents of one log sector are damaged, the contents of the other log sector can be used for recovery operation. Meanwhile, CRC is added to the log content, whether the CRC value is correct or not is checked before the log content is used, and the correctness of the log sector content is guaranteed.
FIG. 5 is a flow chart for reading log sectors after system power up. The step of reading the log sector includes:
reading a main log sector;
judging whether the check CRC value is correct:
if the data is correct, using the recorded data of the main log sector;
if not, read backup log sector: judging whether the check CRC value is correct: if the data is correct, using the recorded data of the backup log sector; if not, the system reports an error.
Because the log is recorded in the reserved sector, and the format of the original file system of the FatFs is not damaged, the FatFs file system added with the log can still be identified and read and written by the mainstream operating systems such as Windows and the like, and the compatibility of the FatFs is ensured.
Further, a target sector is selected as a log sector in a data area of the file system.
A second aspect of the present invention provides a log-type embedded file system design system, including:
a memory and one or more processors;
wherein the memory is communicatively coupled to the one or more processors and has stored therein instructions executable by the one or more processors to cause the one or more processors to perform the method as previously described.
A third aspect of the invention provides a computer-readable storage medium having stored thereon computer-executable instructions operable, when executed by a computing device, to perform a method as previously described.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In summary, the present invention provides a method, a system and a storage medium for designing a journal type embedded file system, where the file system includes reserved sectors and data areas, and the method includes: selecting a target sector from reserved sectors of a file system as a log sector; when the file system performs erasing/reading/writing operation, recording the operation details of each step operation in a log sector in real time; and after the new step operation is completed, replacing the old recorded content in the log sector with the new step operation. The system is configured to perform the method and the storage medium is configured to store computer-executable instructions for performing the method. After the abnormal power failure, the file system can be recovered according to the log after being electrified again, so that the file system has the power failure safety effect.
It is to be understood that the above-described embodiments of the present invention are merely illustrative of or explaining the principles of the invention and are not to be construed as limiting the invention. Therefore, any modification, equivalent replacement, improvement and the like made without departing from the spirit and scope of the present invention should be included in the protection scope of the present invention. Further, it is intended that the appended claims cover all such variations and modifications as fall within the scope and boundaries of the appended claims or the equivalents of such scope and boundaries.