Data storage methods, devices and electronic equipment
By carrying identification information in the physical blocks of NVM, the problem of data incompleteness caused by power failure during NVM data writing is solved, and the integrity verification and recovery of data after power failure is realized, ensuring the reliability and integrity of the data.
Patent Information
- Application Number
- CN202210711985.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-22
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2042-06-22
AI Technical Summary
During the data writing process in non-volatile memory (NVM), abnormal power loss may occur, resulting in incomplete or erroneous data. Power loss protection is required to achieve data recovery.
By carrying identification information, including logical address, physical block age, and integrity check code, in the physical blocks of NVM, the mapping relationship between logical address and physical address and the determination of the age of physical blocks are realized, and integrity verification is performed, thereby completing data writing or recovery after power failure.
It implements power-loss protection during erasure and write operations in non-volatile memory, ensuring data integrity and reliability and avoiding data errors.
Smart Images

Figure CN117312184B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a data storage method, apparatus, and electronic device. Background Technology
[0002] Non-volatile memory (NVM) overcomes the shortcomings of read-only memory (ROM) and random access memory (RAM). It not only has the ability to be electronically erased and programmed, but also retains data for a long time when there is no power. Therefore, it is widely used as a storage unit in chips.
[0003] However, during the writing process to NVM, an abnormal power outage may occur. This could result in incomplete or even erroneous data being read after the NVM is powered on again. Therefore, power-loss protection for NVM is necessary to recover data in case of anomalies. How to implement power-loss protection during the data writing process to NVM is a pressing issue that needs to be addressed. Summary of the Invention
[0004] This application provides a data storage method, apparatus, and electronic device that helps to achieve power-loss protection when erasing and writing NVM.
[0005] In a first aspect, embodiments of this application provide a data storage method applied to NVM, comprising:
[0006] When the NVM is initialized, first information and second information are obtained. The first information is determined by the identification information in the physical blocks of the NVM. The identification information includes at least a first identifier, a second identifier, and a third identifier. The first identifier is used to indicate the logical address corresponding to the physical block, the second identifier is used to indicate the newness or aging of the physical block, and the third identifier is used to indicate the integrity check code. The first information includes the mapping relationship between logical addresses and the physical addresses of occupied physical blocks. The second information is determined by the erase status of the physical blocks in the NVM. The second information includes the physical addresses of free physical blocks.
[0007] Based on the first information and the second information, the data to be written is written to the physical block of the NVM.
[0008] In this embodiment, by carrying identification information within the physical blocks containing written data, and using this identification information to indicate logical addresses, the age of physical blocks, and integrity check codes, the NVM can, after a power outage, find the mapping relationship between physical and logical addresses, determine the age of physical blocks, and perform integrity checks based on the identification information. After integrity checks, data writing or recovery of physical blocks can be completed using the mapping relationship between physical and logical addresses and the age of physical blocks. This facilitates power-loss protection during the erasure and write operations of non-volatile memory.
[0009] In one possible implementation, writing the data to be written into the physical block of the NVM based on the first information and the second information specifically includes:
[0010] If the first information does not include the mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, the data to be written is written to the target physical block, and the identification information is created in the target physical block, wherein the first logical address is the logical address of the data to be written;
[0011] In the first information, a mapping relationship is created between the first logical address and the physical address of the target physical block, and the physical address of the target physical block is deleted from the second information.
[0012] The embodiments of this application can realize the initial writing of a single piece of data.
[0013] In one possible implementation, the data to be written includes multiple data items written in a single transaction, which are used to write multiple physical blocks. The identification information also includes a fourth identifier, which is used to indicate whether it is the last physical block written in the transaction.
[0014] The embodiments of this application can achieve transaction-level first write.
[0015] In one possible implementation, writing the data to be written into the physical block of the NVM based on the first information and the second information specifically includes:
[0016] If the first information includes a first mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, wherein the first logical address is the logical address of the data to be written.
[0017] Write the data and identification information of the occupied physical blocks in the first mapping relationship into the cache, update the data of the occupied physical blocks in the first mapping relationship in the cache to the data to be written, update the identification information in the cache, and write the data to be written in the cache and the updated identification information into the target physical block. The second identifier of the occupied physical block in the first mapping relationship is used to identify the occupied physical block in the first mapping relationship as an old physical block, and the second identifier of the target physical block is used to identify the target physical block as a new physical block.
[0018] In the first information, the first mapping relationship is updated to the second mapping relationship, and the physical address of the target physical block in the second information is deleted, wherein the second mapping relationship is the mapping relationship between the first logical address and the physical address of the target physical block;
[0019] The data in the old physical block is erased.
[0020] The embodiments of this application can realize data updates for individual data.
[0021] In one possible implementation, the data to be written includes multiple data entries written in a single transaction, these multiple data entries being used to write multiple physical blocks. The identification information further includes a fourth identifier, which indicates whether it is the last physical block written in the transaction. The erasure of data from the old physical blocks specifically includes:
[0022] Once the physical address of any old physical block in this transaction is obtained from the first information, the physical address of the old physical block is stored in the third information.
[0023] If the fourth identifier of a newly written physical block indicates that the newly written physical block is the last physical block written in the transaction, then the data of all old physical blocks written in this transaction is erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
[0024] The embodiments of this application can realize transaction-level data updates.
[0025] In one possible implementation, obtaining the first information and the second information when initializing the NVM includes:
[0026] When the NVM is initialized, the physical blocks in the NVM are traversed.
[0027] During the traversal, first information and second information are created. If any physical block is in an erased state, the physical address of the currently traversed physical block is stored in the second information. Alternatively, if any physical block is in a non-erased state, and the physical block in the non-erased state successfully passes the integrity check based on the third identifier, the mapping relationship between the physical address of the currently traversed physical block and the second logical address is stored in the first information. The second information does not contain the physical address of the currently traversed physical block.
[0028] The second logical address is the logical address indicated by the first identifier of the physical block in the non-erased state.
[0029] In this embodiment, the first information and the second information are created based on the identification information by traversal, which can quickly create the first information and the second information and save NVM storage space.
[0030] In one possible implementation, the method further includes:
[0031] During the traversal, if any physical block fails the integrity check based on the third identifier, the physical address of the physical block that failed the integrity check is stored in the third information.
[0032] After the traversal is completed, the physical blocks that failed the integrity check are erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
[0033] In this embodiment of the application, integrity verification is performed based on a third identifier to avoid data errors after power failure, thereby achieving data protection after power failure.
[0034] In one possible implementation, the method further includes:
[0035] During the traversal, if there is a case where a logical address maps to two physical addresses, the physical address of the old physical block is stored in the third information, where the first information contains the mapping relationship between the physical address and the logical address of the new physical block.
[0036] In this embodiment, by detecting mapping conflicts between physical addresses and logical addresses, the situation where one logical address maps to multiple physical addresses is avoided after a power outage, thereby achieving data protection after a power outage.
[0037] In one possible implementation, the method further includes:
[0038] After the traversal is completed, the old physical block is erased based on the third information, and the physical address in the third information is deleted after erasure.
[0039] In one possible implementation, the identification information further includes a fourth identifier, which indicates whether it is the last physical block written in the transaction. The method further includes:
[0040] During the traversal, the fourth identifier is used to determine whether the current transaction write has been completed;
[0041] After the traversal is complete, if it is determined that the current transaction write is incomplete, then based on the physical address in the third information, the mapping relationship between the physical address and logical address of the old physical block is recreated in the first information. After creating the mapping relationship, the physical address of the old physical block is deleted from the third information, the physical address of the new physical block in the first information is stored in the third information, the new physical block is erased based on the physical address in the third information, and the physical address of the new physical block in the third information is deleted after erasure; or,
[0042] After the traversal is completed, if it is determined that the current transaction write has been completed, the old physical block is erased based on the physical address of the old physical block in the third information, and the physical address of the old physical block in the third information is deleted after erasure.
[0043] The embodiments of this application can achieve transaction-level power-loss protection.
[0044] In one possible implementation, determining whether the current transaction write has been completed based on the fourth identifier specifically includes:
[0045] If the fourth identifier of any new physical block indicates that the new physical block is the last physical block written in this transaction, then the transaction write is determined to be complete; or,
[0046] If the fourth identifier of all new physical blocks indicates that all new physical blocks are not the last physical blocks written in this transaction, then it is determined that the transaction write is incomplete.
[0047] In one possible implementation, the first information and the second information are stored in random access memory (RAM) or registers.
[0048] The embodiments of this application can speed up access to the first information and the second information, and can also avoid the first information and the second information occupying the storage space of the NVM.
[0049] In one possible implementation, the physical address of the free physical block in the second information is obtained by traversing from the random physical address of the NVM.
[0050] The embodiments of this application can avoid erasing and writing to the same physical block, which helps to achieve wear leveling.
[0051] In a second aspect, embodiments of this application provide a data storage device, including: an acquisition module and a writing module, the acquisition module and the writing module being used to execute the data storage method as described in the first aspect.
[0052] Thirdly, embodiments of this application provide an electronic device, including: a processor and a memory, wherein the memory is used to store a computer program; and the processor is used to run the computer program and execute the data storage method as described in the first aspect. Attached Figure Description
[0053] Figure 1 System architecture diagram provided for embodiments of this application;
[0054] Figure 2 This is a diagram illustrating the translation between logical addresses and physical addresses.
[0055] Figure 3 A flowchart illustrating an embodiment of the data storage method provided in this application;
[0056] Figure 4 A flowchart illustrating another embodiment of the data storage method provided in this application;
[0057] Figure 5 A flowchart illustrating another embodiment of the data storage method provided in this application;
[0058] Figure 6 A flowchart illustrating another embodiment of the data storage method provided in this application;
[0059] Figure 7 A flowchart illustrating another embodiment of the data storage method provided in this application;
[0060] Figures 8a-8e This is a schematic diagram of physical blocks in NVM provided in an embodiment of this application;
[0061] Figure 9 A schematic diagram of the structure of the data storage device provided in the embodiments of this application;
[0062] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0063] In this embodiment of the application, unless otherwise stated, the character " / " indicates that the preceding and following objects are in an OR relationship. For example, A / B can represent A or B. "AND / OR" describes the relationship between the associated objects, indicating that three relationships can exist. For example, A AND / OR B can represent: A existing alone, A and B existing simultaneously, and B existing alone.
[0064] It should be noted that the terms "first" and "second" used in the embodiments of this application are used only for distinguishing descriptive purposes and should not be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated, nor should they be construed as indicating or implying order.
[0065] In the embodiments of this application, "at least one" refers to one or more items, and "more than one" refers to two or more items. Furthermore, "at least one of the following" or similar expressions refer to any combination of these items, which may include any combination of a single item or a plurality of items. For example, at least one of A, B, or C can represent: A, B, C, A and B, A and C, B and C, or A, B, and C. Each of A, B, and C can be an element itself or a set containing one or more elements.
[0066] In this application, terms such as "exemplary," "in some embodiments," and "in another embodiment" are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" in this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the term "exemplary" is intended to present the concept in a concrete manner.
[0067] In the embodiments of this application, the terms "of," "corresponding (relevant)," and "corresponding" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction, their meanings are consistent. Similarly, in the embodiments of this application, "communication" and "transmission" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction, their meanings are consistent. For example, transmission can include sending and / or receiving, and can be a noun or a verb.
[0068] In the embodiments of this application, the term "equal to" can be used in conjunction with "greater than" to apply to technical solutions employing the condition of "greater than", and can also be used in conjunction with "less than" to apply to technical solutions employing the condition of "less than". It should be noted that when "equal to" is used with "greater than", it cannot be used with "less than"; and when "equal to" is used with "less than", it cannot be used with "greater than".
[0069] NVM overcomes the shortcomings of ROM and RAM, not only possessing electronic erasable and programmable capabilities, but also retaining data for a long time when power is off and there is no current. Therefore, it is widely used as a storage unit in chips.
[0070] However, during the writing process to NVM, an abnormal power outage may occur. This could result in incomplete or even erroneous data being read after the NVM is powered on again. Therefore, power-loss protection for NVM is necessary to recover data in case of anomalies. How to implement power-loss protection during the data writing process to NVM is a pressing issue that needs to be addressed.
[0071] Based on the above problems, this application proposes a data storage method applied to NVM. By carrying identification information in the physical blocks containing written data, and using the identification information to indicate logical addresses, the age of physical blocks, and integrity check codes, the NVM can find the mapping relationship between physical addresses and logical addresses, determine the age of physical blocks, and perform integrity checks after power failure. After integrity checks, the mapping relationship between physical addresses and logical addresses and the age of physical blocks can be used to complete the writing of data to physical blocks or the recovery of physical blocks, which helps to achieve power failure protection when erasing and writing non-volatile memory.
[0072] Now combined Figures 1-7 , Figures 8a-8e The data storage method provided in the embodiments of this application will be described.
[0073] Figure 1 This is a system architecture diagram provided for an embodiment of this application. Figure 1 As shown, the system architecture includes a non-volatile memory controller and an NVM. The non-volatile memory controller is used to read from and write to the NVM. For example, the non-volatile memory controller obtains the mapping relationship between logical addresses and physical addresses, and can read from and write to the NVM according to this mapping relationship. It can be understood that the logical address can be a virtual address used to store data, while the physical address is the actual address used to store data. Users access data by accessing logical addresses; the non-volatile memory controller translates the logical address into a physical address, and then retrieves the accessed data at the corresponding physical address in the NVM. This allows for erasing and rewriting of the data stored in the NVM.
[0074] Figure 2 This is a diagram illustrating the translation between logical addresses and physical addresses. For example... Figure 2As shown, NVM contains multiple physical blocks, each with a physical address. This physical address occupies the physical space of the NVM. Erasing or writing to the physical space corresponding to a physical address can cause wear and tear. For example, when the logical address is between 0 and 0x3F, it corresponds to logical block 0 and maps to physical block 3; when the logical address is between 0x40 and 0x7F, it corresponds to logical block 1 and maps to physical block 6; when the logical address is between 0x80 and 0xCF, it corresponds to logical block 2 and maps to physical block 0; and when the logical address is between 0xC0 and 0xFF, it corresponds to logical block 3 and maps to physical block 2. Since the logical address corresponds to logical space, erasing or writing to the logical space does not cause wear and tear. Therefore, by converting between logical and physical addresses, the excessive wear and tear caused by accessing the same physical address for a long time can be avoided.
[0075] like Figure 3 The diagram shown is a flowchart of an embodiment of the data storage method provided in this application, which specifically includes the following steps:
[0076] Step 301: When initializing the NVM, first information and second information are obtained. The first information is determined by the identification information in the physical blocks of the NVM. The identification information includes at least a first identifier, a second identifier, and a third identifier. The first identifier is used to indicate the logical address corresponding to the physical block where the identification information is located. The second identifier is used to indicate the newness or oldness of the physical block. The third identifier is used to indicate the integrity check code of the physical block. The first information includes the mapping relationship between the logical address and the physical address of the occupied physical block. The second information is determined by the erase status of the physical block. The second information includes the physical address of the free physical block.
[0077] Specifically, initialization includes, but is not limited to, scenarios such as normal power-on, abnormal power-on, reset, and resetting. In other words, NVM will perform initialization after a user performs a normal power-on, abnormal power-on, reset, or resetting. The specific methods for obtaining the first and second information can include the following two scenarios:
[0078] Scenario 1: The first and second information are stored in RAM or registers.
[0079] Specifically, to avoid the additional physical space required by storing the first and second information in the NVM, the first and second information can be stored in RAM or registers. Furthermore, since accessing the physical space of the NVM reduces data access efficiency, accessing RAM or registers can speed up data access. Storing the first and second information in the NVM would result in erasing and rewriting of the information, while RAM or registers have no lifespan; therefore, storing the first and second information in RAM or registers avoids the excessive wear and tear issues that would occur with storing them in the NVM.
[0080] In Scenario 1, during each NVM initialization, the first and second information can be obtained by traversing the physical blocks within the NVM. It's understandable that NVM initialization typically involves RAM or register initialization, and data stored in RAM or registers is lost after initialization. Therefore, the first and second information stored in RAM or registers will also be lost after NVM initialization. In this case, the first and second information can be obtained by traversing the physical blocks within the NVM.
[0081] In practical implementation, the specific method for obtaining the first and second information can be as follows: When traversing any physical block, if the currently traversed physical block is in an erased state, it means that the physical block is an empty physical block. An empty physical block contains no data, meaning it does not contain any identification information. Therefore, the second information can be created based on the erased state of the physical block. For example, the physical address of the currently traversed empty physical block can be stored in the second information; that is, the second information contains the physical address of the currently traversed empty physical block. It is understandable that since the currently traversed physical block does not contain identification information (e.g., a first identifier), there is no need to create a mapping relationship between the logical address and the physical address of the currently traversed physical block in the first information. In other words, the first information does not contain a mapping relationship between the logical address and the physical address of the currently traversed physical block.
[0082] If the currently traversed physical block is in a non-erased state, it indicates that the physical block is a non-empty physical block. This non-empty physical block stores data, therefore, the identification information within it can be read, and first information can be created from the read identification information. For example, a logical address can be read from the first identifier in the currently traversed physical block, and the mapping relationship between this logical address and the physical address of the currently traversed non-empty physical block can be stored in the first information. In other words, the first information contains the mapping relationship between the logical address and the physical address of the currently traversed non-empty physical block. It is understandable that since the currently traversed physical block is in a non-erased state, there is no need to store the physical address of the currently traversed non-empty physical block in the second information; that is, the second information does not contain the physical address of the currently traversed non-empty physical block.
[0083] It is understandable that the specific implementation of determining whether the currently traversed physical block is in an erased state can be as follows: read the data bits corresponding to the identification information in the physical block. For example, if the data bits corresponding to the identification information are preset values, such as all 1s or all 0s, then it is determined that the currently traversed physical block is in an erased state; if the data bits corresponding to the identification information are not preset values, then it is determined that the currently traversed physical block is in a non-erased state. The preset values mentioned above are illustrative and do not constitute a limitation on the embodiments of this application. In some embodiments, the preset values may be other values.
[0084] Scenario 2: The first and second information are stored in NVM.
[0085] Specifically, to avoid creating the first and second information through iteration during each initialization, the first and second information can be stored in the NVM. It's understood that the first and second information in the NVM can be created through iteration during the initial NVM initialization. Furthermore, since the data in the NVM is not lost after re-initialization, there's no need to recreate the first and second information through iteration after each re-initialization, thus improving efficiency. The specific implementation of creating the first and second information in Scenario 2 can be found in the relevant description in Scenario 1, and will not be repeated here.
[0086] Furthermore, since NVM requires an erase operation before writing data, with the smallest erase unit being a block (e.g., a physical block), and because the number of erase / write cycles for NVM is limited, if a physical block is written to frequently, reaching its maximum erase / write cycle, it will become a bad block, thus affecting the overall lifespan of the NVM. Therefore, wear leveling needs to be considered when performing write operations on the NVM. In this embodiment, when traversing physical blocks in the NVM, a random physical address can be started. This allows data erase / write operations to be performed on random physical blocks, avoiding data erase / write operations on the same physical block, thereby achieving wear leveling.
[0087] Step 302: Based on the first information and the second information, write the data to be written into the physical block of the NVM.
[0088] Specifically, after obtaining the first and second information, the user can perform operations in the NVM to write the data to be written into the NVM. The physical block used to write the data to the NVM can be an empty physical block, or it can be a non-empty physical block that already contains data, with no particular limitation in this embodiment.
[0089] For scenarios where data to be written is written to an empty physical block, the target physical block can be determined based on the second information, the data to be written is written to the target physical block, and identification information corresponding to the target physical block is created in the target physical block, such as a first identifier, a second identifier, and a third identifier. The first identifier is used to indicate the logical address, for example, the logical address of the data to be written; the second identifier is used to indicate the age of the physical blocks; the third identifier is used to indicate the integrity check code; and after the data to be written is successfully written to the target physical block, a mapping relationship between the logical address and the physical address of the target physical block can be created in the first information.
[0090] For scenarios involving updating data on non-empty physical blocks that already contain written data, the target physical block can be determined based on the second information. For ease of explanation, the aforementioned non-empty physical block containing written data will be referred to as the physical block to be updated. First, the data and identification information in the physical block to be updated are written to a cache. Then, the data in the cache (i.e., the data of the physical block to be updated) is updated with the data to be written, and the identification information is updated. The updated identification information and the data to be written are then written to the target physical block. Specifically, the second identifier in the target physical block indicates that it is a new physical block, while the second identifier in the physical block to be updated indicates that it is an old physical block. The third identifier in the target physical block indicates the integrity checksum of the target physical block, and the third identifier in the physical block to be updated indicates the integrity checksum of the physical block to be updated. It is understandable that the logical address indicated by the first identifier in the target physical block is the same as the logical address indicated by the first identifier in the physical block to be updated; and when the data to be written is successfully written to the target physical block, the mapping relationship between the first logical address and the physical address of the physical block to be updated can be updated in the first information to the mapping relationship between the first logical address and the physical address of the target physical block, the physical address of the target physical block in the second information can be deleted, and the physical block to be updated can be erased.
[0091] In this embodiment, by carrying identification information in the physical blocks containing written data, and using the identification information to indicate the logical address, the age of the physical blocks, and the integrity check code, the NVM can find the mapping relationship between the physical address and the logical address, determine the age of the physical blocks, and perform integrity checks after power failure based on the identification information. After integrity checks, the data writing or recovery of the physical blocks can be completed through the mapping relationship between the physical address and the logical address and the age of the physical blocks, which helps to achieve power failure protection when erasing and writing non-volatile memory.
[0092] Next, taking the storage of the first and second information in RAM or registers as an example, and combining... Figure 4 Figure 8 further illustrates the data storage method provided in the embodiments of this application.
[0093] Figure 4 A flowchart illustrating another embodiment of the data storage method provided in this application. Figure 4 In the illustrated embodiment, the application scenario can be writing to an empty physical block. Step 303 specifically includes the following steps:
[0094] Step 401: If the first information does not include the mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, the data to be written is written to the target physical block, and identification information is created in the target physical block, wherein the first logical address is the logical address of the data to be written.
[0095] Specifically, in a scenario where data is written to an empty physical block, a determination can be made based on the first information. If the first information does not include the mapping relationship between the first logical address and the physical address of an already occupied physical block, then an empty physical block can be selected as the target physical block from the second information, and the data to be written and the identification information can be written into the target physical block. The identification information may include a first identifier, a second identifier, and a third identifier.
[0096] In a specific implementation, the data to be written can first be written to a buffer, which can be RAM or a register. Next, identification information can be written to this buffer. Then, a free physical block is selected as the target physical block from the second information. The selected target physical block can be the physical block corresponding to the first physical address in the second information, the physical block corresponding to the last physical address in the second information, or any physical block corresponding to any of the middle physical addresses in the second information. This application embodiment does not impose any special limitations on the selection of the target physical block.
[0097] Once the target physical block is determined, the contents of the buffer can be written to the target physical block. The contents of the buffer can include the data to be written and identification information, thereby ensuring that the target physical block contains the data to be written and identification information.
[0098] In some optional embodiments, the user can also perform transaction operations, where a transaction operation is used to characterize a single transaction write. A single transaction write may include multiple pieces of data to be written, which are used to write multiple physical blocks. Therefore, when writing any piece of data to be written, the target physical block may also include a fourth identifier, which indicates whether it is the last physical block written in this transaction. For example, if the currently written physical block is not the last physical block written in this transaction, then the fourth identifier indicates that the currently written physical block is not the last physical block written in this transaction; for example, the value of the fourth identifier is 0. If the currently written physical block is the last physical block written in this transaction, then the fourth identifier indicates that the currently written physical block is the last physical block written in this transaction; for example, the value of the fourth identifier is 1. It is understood that the value of the fourth identifier is merely illustrative and does not constitute a limitation on the embodiments of this application. It should be noted that the storage order of the first to fourth identifiers in the identification information of a physical block can be flexibly set according to the actual situation. For example, the first to fourth identifiers can be stored sequentially, or they can be stored in the order of first identifier, second identifier, fourth identifier, and third identifier. This application embodiment does not limit this.
[0099] It should be noted that if the second information does not contain any selectable free physical blocks (i.e., the number of selectable free physical blocks in the second information is 0), or the number of selectable free physical blocks is insufficient to meet the number of physical blocks required for this transaction's write operation. For example, assuming this transaction involves writing 3 physical blocks, if the number of selectable free physical blocks in the second information is 2, it can be considered that the number of selectable free physical blocks is insufficient to meet the number of physical blocks required for this transaction's write operation. In this case, the physical blocks of the NVM can be further traversed to obtain free physical blocks, and the physical addresses of the traversed free physical blocks can be stored in the second information, thereby completing the data write operation. It is understood that the traversal method here can also start from a random physical address, thus avoiding the problem of excessive wear.
[0100] Step 402: Create a mapping relationship between the first logical address and the physical address of the target physical block in the first information, and delete the physical address of the target physical block in the second information.
[0101] Specifically, after the data to be written and the identification information are successfully written to the target physical block, a mapping relationship between the first logical address and the physical address of the target physical block can be created in the first information. Then, the physical address of the target physical block can be deleted from the second information, thereby updating the target physical block as an occupied physical block, thus completing the process of writing the data to be written to the NVM. Since the target physical block already contains identification information, the physical block can be processed according to the identification information after a power failure, thereby achieving power failure protection. The relevant processing after a power failure can be referred to the relevant description below, and will not be repeated here.
[0102] Figure 5 This is a flowchart illustrating another embodiment of the data storage method provided in this application. Again, taking the storage of first and second information in RAM or a register as an example, in... Figure 5 In the illustrated embodiment, the application scenario can be updating data on a non-empty physical block. Step 303 specifically includes the following steps:
[0103] Step 501: If the first information includes a first mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, wherein the first logical address is the logical address of the data to be written.
[0104] Specifically, in the scenario of updating data on a non-empty physical block, a determination can be made based on the first information. If the first information includes a first mapping relationship between the first logical address and the physical address of the occupied physical block, then an idle physical block can be selected as the target physical block from the second information so that the data to be updated can be written into the target physical block.
[0105] Step 502: Write the data and identification information of the occupied physical blocks in the first mapping relationship into the cache, update the data of the occupied physical blocks in the first mapping relationship in the cache to the data to be written, update the identification information in the cache, and write the data to be written in the cache and the updated identification information into the target physical block. The second identifier of the occupied physical block in the first mapping relationship indicates that the occupied physical block in the first mapping relationship is an old physical block, and the second identifier of the target physical block indicates that the target physical block is a new physical block.
[0106] Specifically, once the target physical block is determined, the data and identification information of the occupied physical blocks in the first mapping relationship can be written into a cache. The cache can be RAM or a register, and the identification information can include a first identifier, a second identifier, and a third identifier. Next, the data in the cache (i.e., the data of the occupied physical blocks in the first mapping relationship) can be updated to the data to be written based on the data to be written, and the identification information in the cache can be updated. For example, the second identifier of the occupied physical blocks in the first mapping relationship indicates that the occupied physical blocks in the first mapping relationship are old physical blocks, and the second identifier in the cache indicates that the target physical block is a new physical block. That is, the occupied physical blocks in the first mapping relationship can be considered as the physical blocks before the data update, and the target physical block can be considered as the physical blocks after the data update. The logical address indicated by the first identifier of the occupied physical blocks in the first mapping relationship is the same as the logical address indicated by the first identifier of the target physical block, and the integrity check code indicated by the third identifier of the occupied physical blocks in the first mapping relationship is different from the integrity check code indicated by the third identifier of the target physical block. Finally, the data to be written and the updated identification information in the cache are written into the target physical block.
[0107] For example, taking the second identifier as an index, the specific implementation of updating the second identifier can be: the index value of the new physical block = the index value of the old physical block + 1. If the index value of the old physical block is 0, then the index value of the new physical block is 1; if the index value of the old physical block is 1, then the index value of the new physical block is 2. This can distinguish between the old and new physical blocks.
[0108] In some optional embodiments, the storage space occupied increases as the index value increases. Therefore, to reduce the storage space occupied by the index value, the index value can be reused cyclically. For example, the cyclic range of the index value is 0-3. Assuming the index value of the old physical block is 3, the index value of the new physical block is 0. Then, the old and new physical blocks can be distinguished by incrementing the index value. The specific implementation method can be referred to the above embodiments, and will not be repeated here.
[0109] In practical implementation, the data and identification information of the occupied physical blocks in the first mapping relationship can be written to a buffer first. Then, the data in the buffer can be updated according to the data to be written. For example, some or all of the words or bytes in the data of the occupied physical blocks in the first mapping relationship can be updated, as well as the identification information can be updated. Next, the updated data and updated identification information in the buffer can be written to the target physical block.
[0110] In some optional embodiments, in a transaction write scenario, the target physical block after data writing may further include a fourth identifier. The specific method for writing the fourth identifier can be found in the above embodiments and will not be repeated here.
[0111] Step 503: Update the first mapping relationship to the second mapping relationship in the first information, and delete the physical address of the target physical block in the second information, wherein the second mapping relationship is the mapping relationship between the first logical address and the physical address of the target physical block.
[0112] Specifically, after the updated data and updated identification information in the cache are written to the target physical block, that is, after the data is successfully written, the first mapping relationship in the first information can be updated to the second mapping relationship, and the physical address of the target physical block in the second information can be deleted. The second mapping relationship is the mapping relationship between the first logical address and the physical address of the target physical block, which can indicate that the target physical block has been occupied.
[0113] Step 504: Erase the data in the old physical block.
[0114] Specifically, after retrieving the physical address of the old physical block from the first information, the physical address of the old physical block can be stored in the third information. Then, after the data in the new physical block is successfully written, the data in the old physical block can be erased based on the physical address in the third information, and the physical address in the third information can be deleted. It should be noted that the third information can also be stored in RAM or a register.
[0115] In some optional embodiments, in the scenario of transaction writing, after obtaining the physical address of any old physical block in this transaction writing from the first information, the physical address of the old physical block is stored in the third information.
[0116] If the third identifier of a newly written physical block indicates that the newly written physical block is the last physical block written in this transaction, then the data of all old physical blocks written in this transaction can be erased based on the physical address in the third information, and the physical address in the third information can be deleted after erasure. This can avoid the unrecoverable old physical blocks caused by premature erasure of old physical blocks and transaction failure during transaction writing.
[0117] The method provided in this application embodiment can achieve power loss protection when updating data on physical blocks that have already been written to.
[0118] Figure 6 This is a flowchart illustrating another embodiment of the data storage method provided in this application. Again, taking the storage of the first and second information in RAM or a register as an example, in... Figure 6 In the illustrated embodiment, the application scenario can be a normal power-on, reset, or reset after data update, and may also include the following steps:
[0119] Step 601: When initializing the NVM, the physical blocks in the NVM are traversed.
[0120] Specifically, when the NVM is powered on, reset, or reset normally, the NVM will initialize. During NVM initialization, the physical blocks within the NVM are traversed. The specific method for traversing the physical blocks in the NVM can be: sequentially reading the identification information of each physical block in the NVM. It is understood that the traversal can start from a random physical address within the NVM or from a pre-specified physical address; this embodiment does not impose any special limitations on this.
[0121] Step 602: During the traversal, if any physical block is in an erased state, the physical address of the currently traversed physical block is stored in the second information; or, if any physical block is in a non-erased state, and the physical block in the non-erased state successfully passes the integrity verification based on the third identifier, a mapping relationship between the second logical address and the physical address of the physical block in the non-erased state is created in the first information, wherein the second logical address is the logical address indicated by the first identifier of the physical block in the non-erased state.
[0122] Specifically, when traversing any physical block, if the currently traversed physical block is in an erased state, its physical address can be stored in the second information; if the currently traversed physical block is in a non-erased state, its integrity can be further verified. Taking the third identifier as the integrity check code as an example, the specific method for verifying the integrity of the currently traversed physical block can be: performing integrity verification on the currently traversed physical block based on the integrity check code. If the data in the currently traversed physical block does not match the integrity check code, the integrity verification of the physical block is considered to have failed, indicating that an error occurred in the data of the physical block during the power outage. Specifically, this could be due to a power outage during the writing or erasing process; if the data in the currently traversed physical block matches the check code, the integrity verification of the physical block is considered to have succeeded, indicating that no data error occurred in the physical block during the power outage. The integrity check code can be a Cyclic Redundancy Check (CRC) or an Error Detection Code (EDC), or other types of integrity check codes. This application does not impose any special limitations on this type of integrity check code.
[0123] Understandably, after traversing all physical blocks in the NVM, if all physical blocks successfully pass the integrity check based on the third identifier, and there is no case in the first information where a logical address maps to two physical addresses, then the data write can be considered successfully completed, and no further operation is required. Specifically, the method for detecting a case where a logical address maps to two physical addresses can be: read the second logical address indicated by the first identifier in each physical block; if two physical blocks have the same second logical address stored in their first identifiers, then it can be considered that a case exists where a logical address maps to two physical addresses.
[0124] Figure 7 This is a flowchart illustrating another embodiment of the data storage method provided in this application. Again, taking the storage of the first and second information in RAM or a register as an example, in... Figure 7 In the illustrated embodiment, the application scenario could be an abnormal power outage following a data update, and may also include the following steps:
[0125] Step 701: When initializing the NVM, the physical blocks in the NVM are traversed.
[0126] Specifically, when the NVM experiences an abnormal power outage and is subsequently powered on again, the NVM will be initialized. During the initialization of the NVM, the physical blocks within the NVM will be traversed.
[0127] Step 702: During the traversal, if any physical block fails the integrity check based on the third identifier, the physical address of the physical block that failed the integrity check is stored in the third information. After the traversal is completed, the physical block that failed the integrity check is erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure. Alternatively, during the traversal, if there is a case where one logical address maps to two physical addresses, the physical address of the old physical block is stored in the third information. After the traversal is completed, the old physical block is erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure. The first information contains the mapping relationship between the physical address and the logical address of the new physical block.
[0128] Specifically, during the traversal process, when any physical block is encountered, if the currently traversed physical block is in an erased state, its physical address can be stored in the second information; if the currently traversed physical block is in a non-erased state, an integrity check can be performed on the currently traversed physical block based on the third identifier. If the integrity check is successful, the mapping relationship between the second logical address and the physical address of the currently traversed physical block is stored in Table 1. If the integrity check fails, the physical address of the failed physical block is stored in the third information. The specific methods for determining whether a physical block is in an erased or non-erased state and for performing integrity checks on the physical block can be found in the above embodiments and will not be repeated here.
[0129] Since physical blocks that fail integrity checks are all data-incorrect physical blocks—for example, those where errors occurred during data writing or erasure—they can be erased after the traversal is complete. Specifically, erasing these blocks can be done based on their physical addresses from the third information, and then the physical addresses in the third information can be deleted after erasure to avoid duplicate erasure of physical blocks.
[0130] In some optional embodiments, if the integrity check succeeds, a mapping relationship can be created between the second logical address and the physical address of the currently traversed physical block. However, if the first information already stores the mapping relationship between the second logical address and the physical addresses of other physical blocks, a situation may occur where one logical address maps to two physical addresses, resulting in a conflict. Therefore, the second identifier in the physical block can be used to further determine which is the old physical block and which is the new physical block, and the physical address of the old physical block can be stored in the third information, while the first information stores the mapping relationship between the physical address of the new physical block and the second logical address. For example, if the currently traversed physical block is a new physical block, the physical addresses of other physical blocks corresponding to the second logical address in the first information are stored in the third information, and a mapping relationship between the physical address of the currently traversed physical block and the second logical address is created in the first information; if the currently traversed physical block is an old physical block, the mapping relationship between the second logical address and the physical addresses of other physical blocks is retained in the first information, and the physical address of the currently traversed physical block is stored in the third information.
[0131] After the traversal is complete, the old physical block can be erased based on the physical address in the third information, and the physical address in the third information can be deleted after erasure.
[0132] In some optional embodiments, during the transaction write scenario, the traversal process also needs to determine whether all the data to be written in this transaction has been successfully written. If all the data to be written in this transaction has been successfully written, the old physical block can be erased based on the physical address of the old physical block in the third information after the traversal is completed, and the physical address of the old physical block in the third information is deleted after erasure. If at least one of the data to be written in this transaction has failed to be written, the new physical block in this transaction that has been written with the data to be written can be restored after the traversal is completed. For example, the mapping relationship between the new physical block that has been written with the data to be written and the logical address can be restored to the mapping relationship between the old physical block that has not been written with the data to be written and the logical address. That is to say, even if some of the data to be written is successfully written and some of the data to be written fails in a transaction, the mapping relationship between the new physical block that has been successfully written and the logical address must be restored to the mapping relationship between the old physical block and the logical address, thereby ensuring the atomicity of the transaction and realizing transaction-level power loss protection.
[0133] In the specific implementation, during the traversal process, the fourth identifier is used to determine whether the current transaction write has been completed. For example, if the currently traversed physical block is a new physical block, and the fourth identifier indicates that the currently traversed physical block is the last physical block written in this transaction, then it can be determined that the current transaction write has been completed; if the fourth identifier of all new physical blocks indicates that all new physical blocks are not the last physical blocks written in this transaction, then it can be determined that the current transaction write has not been completed.
[0134] After traversing all physical blocks in the NVM, if it is determined that the current transaction write is incomplete, the mapping relationship between the physical address and logical address of the old physical block can be recreated in the first information based on the physical address in the third information. After creating the mapping relationship, the physical address of the old physical block can be deleted from the third information. Next, since the first information stores the mapping relationship between the physical address and logical address of the new physical block, in order to de-map the physical address and logical address of the new physical block, the physical address of the new physical block mapped by the logical address in the first information can be deleted, and the physical address of the new physical block can be stored in the third information. The new physical block can then be erased based on the physical address in the third information, and the physical address of the new physical block in the third information can be deleted after erasure. This allows for physical block recovery in the event of a transaction write failure. If the current transaction write is complete, the old physical block can be erased based on the physical address of the old physical block in the third information, and the physical address of the old physical block in the third information can be deleted after erasure.
[0135] In some optional embodiments, after erasing a new physical block, the physical address of the new physical block can be stored in the second information; or, after erasing an old physical block, the physical address of the old physical block can be stored in the second information. This ensures that there are sufficient free physical addresses in the second information, avoiding re-traversal due to lack of free physical addresses when writing data.
[0136] Now combined Figures 8a-8eThe power-loss protection process for transaction writes after power-on following an abnormal power outage is illustrated by an example. Assume that physical blocks 1 and 2 in the NVM store old data, where the identification information of physical blocks 1 and 2 includes a first identifier, a second identifier, a third identifier, and a fourth identifier. Taking a physical block as an example where the first identifier is the logical address, the second identifier is the index value, the third identifier is the integrity check code, and the fourth identifier indicates whether it is the last physical block written in a transaction using a Boolean value (e.g., 0 or 1), the first identifier of physical block 1 is logical address 1, indicating that the logical address corresponding to physical block 1 is logical address 1; the second identifier of physical block 1 is 1, indicating that the index value of physical block 1 is 1; the third identifier of physical block 1 is CRC1, indicating that the integrity check code of physical block 1 is CRC1; and the fourth identifier of physical block 1 is 0, indicating that physical block 1 is not the last physical block written in a transaction. Similarly, the first identifier of physical block 2 is logical address 2, indicating that the logical address corresponding to physical block 2 is logical address 2; the second identifier of physical block 2 is 1, indicating that the index value of physical block 2 is 1; the third identifier of physical block 2 is CRC2, indicating that the integrity check code of physical block 2 is CRC2; and the fourth identifier of physical block 2 is 1, indicating that physical block 2 is the last physical block written in a transaction. If this transaction contains two pieces of data to be written, for example, data to be written 1 and data to be written 2, and their corresponding logical addresses are logical address 1 and logical address 2 respectively, then this transaction updates the original data at logical addresses 1 and 2, i.e., it updates the data in physical block 1 and physical block 2. Data to be written 2 is the last piece of data written in this transaction. Furthermore, if data to be written 1 is successfully written during the transaction, but an abnormal power outage occurs during the writing of data to be written 2, then after power is restored, the data will be... Figure 8a The diagram shows a physical block representation in NVM. Figure 8aAs shown, the NVM contains physical block 1, physical block 2, physical block 3, and physical block 4. Data to be written 1 is written to physical block 3, and data to be written 2 is written to physical block 4. The first identifier of physical block 3 is logical address 1, indicating that the logical address corresponding to physical block 3 is logical address 1; the second identifier of physical block 3 is 2, indicating that the index value of physical block 3 is 2; the third identifier of physical block 3 is CRC3, indicating that the integrity check code of physical block 3 is CRC3; and the fourth identifier of physical block 3 is 0, indicating that physical block 3 is the last physical block written in a non-transactional write. Similarly, the first identifier of physical block 4 is logical address 2, indicating that the logical address corresponding to physical block 4 is logical address 2; the second identifier of physical block 4 is 2, indicating that the index value of physical block 4 is 2; the third identifier of physical block 4 is CRC4, indicating that the integrity check code of physical block 4 is CRC4; and the fourth identifier of physical block 4 is 1, indicating that physical block 4 is the last physical block written in a transactional write.
[0137] After traversing physical blocks 1 through 4 in the NVM, since the abnormal power loss occurred during the writing of data 2 to be written, the data in physical blocks 1 and 2 is correct. That is, the integrity verification of physical blocks 1 and 2 is successful. The mapping relationship between the physical address and logical address 1 of physical block 1, and the mapping relationship between the physical address and logical address 2 of physical block 2, can be stored in the first information. Furthermore, since data 1 to be written was successfully written to physical block 3, the integrity verification of physical block 3 is successful, and the mapping relationship between the physical address and logical address 1 of physical block 3 can be created. However, since the mapping relationship between the physical address and logical address 1 of physical block 1 is already stored in the first information, logical address 1 will simultaneously map to the physical address of physical block 1 and the physical address of physical block 3. By comparing the index value indicated by the second identifier of physical block 1 with the index value indicated by the second identifier of physical block 3, the index value indicated by the second identifier of physical block 3 equals the index value indicated by the second identifier of physical block 1 plus 1. In other words, by comparing physical block 3 and physical block 1, physical block 3 is the new physical block, and physical block 1 is the old physical block. Next, the mapping relationship between the physical address and logical address 1 of the new physical block (i.e., physical block 3) can be stored in the first information, and the physical address of the old physical block (i.e., physical block 1) can be stored in the third information. The physical address of the old physical block (i.e., physical block 1) is deleted from the first information.
[0138] If the integrity check of physical block 4 is successful, meaning that the data to be written 2 is also successfully written to physical block 4, then a mapping relationship between the physical address of physical block 4 and logical address 2 can be created. However, the first information already stores the mapping relationship between the physical address of physical block 2 and logical address 2. At this time, logical address 2 will simultaneously map to the physical address of physical block 2 and the physical address of physical block 4. By comparing the index value indicated by the second identifier of physical block 2 with the index value indicated by the second identifier of physical block 4, the index value indicated by the second identifier of physical block 4 = the index value indicated by the second identifier of physical block 2 + 1. That is, by comparing physical block 4 and physical block 2, physical block 4 is the new physical block, and physical block 2 is the old physical block. Next, the mapping relationship between the physical address of the new physical block (i.e., physical block 4) and logical address 2 can be stored in the first information, and the physical address of the old physical block (i.e., physical block 2) can be stored in the third information. The physical address of the old physical block (i.e., physical block 2) is deleted from the first information. Thus, we can obtain... Figure 8b The diagram shows a physical block representation. Furthermore, since physical block 4 is a new physical block, and its fourth identifier indicates it is the last physical block written in this transaction, once data 2 is successfully written to physical block 4, the transaction can be considered complete. Next, the old physical blocks can be erased based on the physical address in the third information; for example, physical blocks 1 and 2 can be erased. This yields the following result: Figure 8c The diagram shown illustrates the physical blocks, thus enabling the completion of the entire transaction write process. For example... Figure 8c As shown, the NVM contains physical block 1, physical block 2, physical block 3, and physical block 4. Physical blocks 1 and 2 have been erased; therefore, they are empty. Logical address 1 is no longer mapped to the physical address of physical block 1, and logical address 2 is no longer mapped to the physical address of physical block 2. Physical block 3 contains new data (i.e., data to be written 1). The first identifier of physical block 3 = logical address 1, the second identifier of physical block 3 = 2, the third identifier of physical block 3 = CRC3, and the fourth identifier of physical block 3 = 0. Physical block 4 contains new data (data to be written 2). The first identifier of physical block 4 = logical address 2, the second identifier of physical block 4 = 2, the third identifier of physical block 4 = CRC4, and the fourth identifier of physical block 4 = 1.
[0139] If the integrity check of physical block 4 fails, meaning that the data to be written, 2, was not successfully written to physical block 4 and the data in physical block 4 is incomplete, then the physical address of physical block 4 can be stored in the third information. In other words, the physical address of the failed physical block can be stored in the third information. The first information does not contain the mapping relationship between the physical address of physical block 4 and logical address 2. Therefore, we can obtain... Figure 8dThe diagram shows a physical block representation. Since the data to be written, 2, is part of the data being written in this transaction, but the write operation failed, the transaction is incomplete and all physical blocks involved in the transaction need to be recovered. At this point, the mapping relationship between the physical address and logical address of the old physical blocks in the third information can be stored in the first information. For example, the mapping relationship between the physical address and logical address 1 of physical block 1 can be stored in the first information, and the physical address of physical block 1 can be deleted from the third information. Next, the physical address of the new physical block can be stored in the third information. For example, the physical address of physical block 3 can be stored in the third information, and the physical address of the new physical block (e.g., physical block 3) in the first information can be deleted, thereby releasing the mapping relationship between the physical address and logical address 1 of physical block 3 in the first information. Finally, the physical blocks can be erased based on the physical addresses in the third information. For example, physical blocks 3 and 4 can be erased, thus completing data recovery and achieving power-loss protection for transaction writes. After data recovery is complete, the following can be obtained: Figure 8e The diagram shows the mapping between the physical addresses and logical addresses of a physical block. (See attached diagram.) Figure 8e As shown, the NVM contains physical block 1, physical block 2, physical block 3, and physical block 4. Physical blocks 3 and 4 have been erased; therefore, they are empty, and logical address 1 is no longer mapped to the physical address of physical block 3. Physical block 1 contains old data; its first identifier = logical address 1, second identifier = 1, third identifier = CRC1, and fourth identifier = 0. Physical block 2 also contains old data; its first identifier = logical address 2, second identifier = 1, third identifier = CRC2, and fourth identifier = 1.
[0140] Figure 9 This is a schematic diagram of the structure of one embodiment of the data storage device of this application, as shown below. Figure 9 As shown, the aforementioned data storage device 90 may include: an acquisition module 91 and a writing module 92; wherein,
[0141] The acquisition module 91 is used to acquire first information and second information when the NVM is initialized. The first information is determined by identification information in the physical blocks of the NVM. The identification information includes at least a first identifier, a second identifier, and a third identifier. The first identifier indicates the logical address corresponding to the physical block, the second identifier indicates the age of the physical block, and the third identifier indicates an integrity check code. The first information includes a mapping relationship between logical addresses and the physical addresses of occupied physical blocks. The second information is determined by the erase status of the physical blocks in the NVM and includes the physical addresses of free physical blocks.
[0142] The writing module 92 is used to write the data to be written into the physical block of the NVM based on the first information and the second information.
[0143] In one possible implementation, the writing module 92 is specifically used to determine the physical address of the target physical block based on the second information if the first information does not include the mapping relationship between the first logical address and the physical address of the occupied physical block, write the data to be written into the target physical block, and create the identification information in the target physical block, wherein the first logical address is the logical address of the data to be written.
[0144] In the first information, a mapping relationship is created between the first logical address and the physical address of the target physical block, and the physical address of the target physical block is deleted from the second information.
[0145] In one possible implementation, the data to be written includes multiple data items written in a single transaction, which are used to write multiple physical blocks. The identification information also includes a fourth identifier, which is used to indicate whether it is the last physical block written in the transaction.
[0146] In one possible implementation, the writing module 92 is specifically used to determine the physical address of the target physical block based on the second information if the first information includes a first mapping relationship between the first logical address and the physical address of the occupied physical block, wherein the first logical address is the logical address of the data to be written.
[0147] Write the data and identification information of the occupied physical blocks in the first mapping relationship into the cache, update the data of the occupied physical blocks in the first mapping relationship in the cache to the data to be written, update the identification information in the cache, and write the data to be written in the cache and the updated identification information into the target physical block. The second identifier of the occupied physical block in the first mapping relationship is used to identify the occupied physical block in the first mapping relationship as an old physical block, and the second identifier of the target physical block is used to identify the target physical block as a new physical block.
[0148] In the first information, the first mapping relationship is updated to the second mapping relationship, and the physical address of the target physical block in the second information is deleted, wherein the second mapping relationship is the mapping relationship between the first logical address and the physical address of the target physical block;
[0149] The data in the old physical block is erased.
[0150] In one possible implementation, the data to be written includes multiple data to be written in one transaction, the multiple data being used to write multiple physical blocks, and the identification information further includes a fourth identifier, the fourth identifier being used to indicate whether it is the last physical block written in the transaction; the writing module 92 is also used to store the physical address of any old physical block in the current transaction writing into the third information after obtaining the physical address of any old physical block in the first information.
[0151] If the fourth identifier of a newly written physical block indicates that the newly written physical block is the last physical block written in the transaction, then the data of all old physical blocks written in this transaction is erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
[0152] In one possible implementation, the acquisition module 91 is specifically used to traverse the physical blocks in the NVM when the NVM is initialized;
[0153] During the traversal, first information and second information are created. If any physical block is in an erased state, the physical address of the currently traversed physical block is stored in the second information. Alternatively, if any physical block is in a non-erased state, and the physical block in the non-erased state successfully passes the integrity check based on the third identifier, the mapping relationship between the physical address of the currently traversed physical block and the second logical address is stored in the first information. The second information does not contain the physical address of the currently traversed physical block.
[0154] The second logical address is the logical address indicated by the first identifier of the physical block in the non-erased state.
[0155] In one possible implementation, the acquisition module 91 is further configured to, during the traversal process, if any physical block fails the integrity check based on the third identifier, store the physical address of the physical block that failed the integrity check into the third information.
[0156] After the traversal is completed, the physical blocks that failed the integrity check are erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
[0157] In one possible implementation, the acquisition module 91 is further configured to, during the traversal process, if there is a case where a logical address maps to two physical addresses, store the physical address of the old physical block in the third information, wherein the first information contains the mapping relationship between the physical address and the logical address of the new physical block.
[0158] In one possible implementation, the acquisition module 91 is further configured to erase the old physical block based on the third information after the traversal is completed, and delete the physical address in the third information after erasure.
[0159] In one possible implementation, the identification information further includes a fourth identifier, which is used to indicate whether it is the last physical block written in the transaction. The acquisition module 91 is also used to determine whether the current transaction writing has been completed based on the fourth identifier during the traversal process.
[0160] After the traversal is complete, if it is determined that the current transaction write is incomplete, then based on the physical address in the third information, the mapping relationship between the physical address and logical address of the old physical block is recreated in the first information. After creating the mapping relationship, the physical address of the old physical block is deleted from the third information, the physical address of the new physical block in the first information is stored in the third information, the new physical block is erased based on the physical address in the third information, and the physical address of the new physical block in the third information is deleted after erasure; or,
[0161] After the traversal is completed, if it is determined that the current transaction write has been completed, the old physical block is erased based on the physical address of the old physical block in the third information, and the physical address of the old physical block in the third information is deleted after erasure.
[0162] In one possible implementation, the acquisition module 91 is further configured to determine that the current transaction write has been completed if the fourth identifier of any new physical block indicates that the new physical block is the last physical block written in this transaction; or,
[0163] If the fourth identifier of all new physical blocks indicates that all new physical blocks are not the last physical blocks written in this transaction, then it is determined that the transaction write is incomplete.
[0164] In one possible implementation, the first information and the second information are stored in random access memory (RAM) or registers.
[0165] In one possible implementation, the physical address of the free physical block in the second information is obtained by traversing from the random physical address of the NVM.
[0166] Figure 10This is a schematic diagram of the structure of an electronic device 1000 provided in an embodiment of this application. The electronic device 1000 may include: at least one processor; and at least one memory communicatively connected to the processor. The memory stores program instructions executable by the processor, which can call the program instructions to perform actions performed by the electronic device 1000 provided in this embodiment of the application.
[0167] like Figure 10 As shown, the electronic device 1000 can be in the form of a general-purpose computing device, or it can be a mobile terminal such as a mobile phone or tablet computer, or it can be a wearable device or a POS machine. This application embodiment does not specifically limit the form of the electronic device 1000. The components of the electronic device 1000 may include, but are not limited to: one or more processors 1010, a memory 1020, a communication bus 1040 connecting different system components (including the memory 1020 and the processor 1010), and a communication interface 1030.
[0168] The communication bus 1040 represents one or more of several bus architectures, including a memory bus or memory controller, a peripheral bus, a graphics acceleration port, a processor, or a local bus using any of the various bus architectures. Examples of these architectures include, but are not limited to, the Industry Standard Architecture (ISA) bus, the Micro Channel Architecture (MAC) bus, the Enhanced ISA bus, the Video Electronics Standards Association (VESA) local bus, and the Peripheral Component Interconnect (PCI) bus.
[0169] Electronic device 1000 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by electronic device 1000, including volatile and non-volatile media, removable and non-removable media.
[0170] Memory 1020 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) and / or cache memory. Electronic device 1000 may further include other removable / non-removable, volatile / non-volatile computer system storage media. Although Figure 10As not shown, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk") and an optical disc drive for reading and writing to a removable non-volatile optical disc (e.g., a compact disc read-only memory (CD-ROM), a digital video disc read-only memory (DVD-ROM), or other optical media) may be provided. In these cases, each drive may be connected to the communication bus 1040 via one or more data media interfaces. The memory 1020 may include at least one program product having a set (e.g., at least one) of program modules configured to perform the functions of the embodiments of this application.
[0171] A program / utility having a set (at least one) of program modules can be stored in memory 1020. Such program modules include, but are not limited to, an operating system, one or more applications, other program modules, and program data. Each or some combination of these examples may include an implementation of a network environment. The program modules typically perform the functions and / or methods described in the embodiments of this application.
[0172] Electronic device 1000 can also communicate with one or more external devices (e.g., keyboard, pointing device, display, etc.), and with one or more devices that enable a user to interact with electronic device 1000, and / or with any device that enables electronic device 1000 to communicate with one or more other computing devices (e.g., network card, modem, etc.). This communication can be performed through communication interface 1030. Furthermore, electronic device 1000 can also communicate through a network adapter (… Figure 10 (Not shown) communicates with one or more networks (e.g., Local Area Network (LAN), Wide Area Network (WAN), and / or public networks, such as the Internet). The aforementioned network adapter can communicate with other modules of the electronic device via the communication bus 1040. It should be understood that, although... Figure 10 As not shown, other hardware and / or software modules may be used in conjunction with the electronic device 1000, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, Redundant Arrays of Independent Drives (RAID) systems, tape drives, and data backup storage systems.
[0173] Through the above description of the embodiments, those skilled in the art will clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device, and unit described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0174] In the embodiments of this application, the functional units can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0175] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as flash memory, portable hard disk, read-only memory, random access memory, magnetic disk, or optical disk.
[0176] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A data storage method applied to non-volatile memory (NVM), characterized in that, The method includes: When the NVM is initialized, first information and second information are obtained. The first information is determined by the identification information in the physical blocks of the NVM. The identification information includes at least a first identifier, a second identifier, and a third identifier. The first identifier is used to indicate the logical address corresponding to the physical block, the second identifier is used to indicate the newness or aging of the physical block, and the third identifier is used to indicate the integrity check code. The first information includes the mapping relationship between logical addresses and the physical addresses of occupied physical blocks. The second information is determined by the erase status of the physical blocks in the NVM. The second information includes the physical addresses of free physical blocks. Based on the first information and the second information, the data to be written is written to the physical block of the NVM.
2. The method according to claim 1, characterized in that, The step of writing the data to be written into the physical block of the NVM based on the first information and the second information specifically includes: If the first information does not include the mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, the data to be written is written to the target physical block, and the identification information is created in the target physical block, wherein the first logical address is the logical address of the data to be written; In the first information, a mapping relationship is created between the first logical address and the physical address of the target physical block, and the physical address of the target physical block is deleted from the second information.
3. The method according to claim 2, characterized in that, The data to be written includes multiple data entries written in a single transaction. These multiple data entries are used to write multiple physical blocks. The identification information also includes a fourth identifier, which is used to indicate whether it is the last physical block written in the transaction.
4. The method according to claim 1, characterized in that, The step of writing the data to be written into the physical block of the NVM based on the first information and the second information specifically includes: If the first information includes a first mapping relationship between the first logical address and the physical address of the occupied physical block, then the physical address of the target physical block is determined based on the second information, wherein the first logical address is the logical address of the data to be written. Write the data and identification information of the occupied physical blocks in the first mapping relationship into the cache, update the data of the occupied physical blocks in the first mapping relationship in the cache to the data to be written, update the identification information in the cache, and write the data to be written in the cache and the updated identification information into the target physical block. The second identifier of the occupied physical block in the first mapping relationship is used to identify the occupied physical block in the first mapping relationship as an old physical block, and the second identifier of the target physical block is used to identify the target physical block as a new physical block. In the first information, the first mapping relationship is updated to the second mapping relationship, and the physical address of the target physical block in the second information is deleted, wherein the second mapping relationship is the mapping relationship between the first logical address and the physical address of the target physical block; The data in the old physical block is erased.
5. The method according to claim 4, characterized in that, The data to be written includes multiple data entries written in a single transaction. These multiple data entries are used to write multiple physical blocks. The identification information also includes a fourth identifier, which indicates whether it is the last physical block written in the transaction. The erasure of data in the old physical blocks specifically includes: Once the physical address of any old physical block in this transaction is obtained from the first information, the physical address of the old physical block is stored in the third information. If the fourth identifier of a newly written physical block indicates that the newly written physical block is the last physical block written in the transaction, then the data of all old physical blocks written in this transaction is erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
6. The method according to claim 1, characterized in that, When initializing the NVM, obtaining the first information and the second information includes: When the NVM is initialized, the physical blocks in the NVM are traversed. During the traversal, first information and second information are created. If any physical block is in an erased state, the physical address of the currently traversed physical block is stored in the second information. Alternatively, if any physical block is in a non-erased state, and the physical block in the non-erased state successfully passes the integrity check based on the third identifier, the mapping relationship between the physical address of the currently traversed physical block and the second logical address is stored in the first information. The second information does not contain the physical address of the currently traversed physical block. The second logical address is the logical address indicated by the first identifier of the physical block in the non-erased state.
7. The method according to claim 6, characterized in that, The method further includes: During the traversal, if any physical block fails the integrity check based on the third identifier, the physical address of the physical block that failed the integrity check is stored in the third information. After the traversal is completed, the physical blocks that failed the integrity check are erased based on the physical address in the third information, and the physical address in the third information is deleted after erasure.
8. The method according to claim 6, characterized in that, The method further includes: During the traversal, if there is a case where a logical address maps to two physical addresses, the physical address of the old physical block is stored in the third information, where the first information contains the mapping relationship between the physical address and the logical address of the new physical block.
9. The method according to claim 8, characterized in that, The method further includes: After the traversal is completed, the old physical block is erased based on the third information, and the physical address in the third information is deleted after erasure.
10. The method according to claim 8, characterized in that, The identification information further includes a fourth identifier, which indicates whether it is the last physical block written in the transaction. The method further includes: During the traversal, the fourth identifier is used to determine whether the current transaction write has been completed; After the traversal is complete, if it is determined that the current transaction write is incomplete, then based on the physical address in the third information, the mapping relationship between the physical address and logical address of the old physical block is recreated in the first information. After creating the mapping relationship, the physical address of the old physical block is deleted from the third information, the physical address of the new physical block in the first information is stored in the third information, the new physical block is erased based on the physical address in the third information, and the physical address of the new physical block in the third information is deleted after erasure; or, After the traversal is completed, if it is determined that the current transaction write has been completed, the old physical block is erased based on the physical address of the old physical block in the third information, and the physical address of the old physical block in the third information is deleted after erasure.
11. The method according to claim 10, characterized in that, The step of determining whether the current transaction write has been completed based on the fourth identifier specifically includes: If the fourth identifier of any new physical block indicates that the new physical block is the last physical block written in this transaction, then the transaction write is determined to be complete; or, If the fourth identifier of all new physical blocks indicates that all new physical blocks are not the last physical blocks written in this transaction, then it is determined that the transaction write is incomplete.
12. The method according to any one of claims 1-11, characterized in that, The first information and the second information are stored in random access memory (RAM) or registers.
13. The method according to any one of claims 1-11, characterized in that, The physical address of the free physical block in the second information is obtained by traversing from the random physical address of the NVM.
14. A data storage device applied to a non-volatile memory (NVM), characterized in that, The data storage device includes: The acquisition module is used to acquire first information and second information when the NVM is initialized. The first information is determined by identification information in physical blocks within the NVM. The identification information includes at least a first identifier, a second identifier, and a third identifier. The first identifier indicates the logical address corresponding to the physical block, the second identifier indicates the physical block's age, and the third identifier indicates an integrity check code. The first information includes a mapping relationship between logical addresses and the physical addresses of occupied physical blocks. The second information is determined by the erase status of physical blocks in the NVM and includes the physical addresses of free physical blocks. The write module is used to write the data to be written into the physical block of the NVM based on the first information and the second information.
15. An electronic device, characterized in that, include: A processor and a memory, the memory being used to store a computer program; the processor being used to run the computer program and perform the data storage method as described in any one of claims 1-13.
Citation Information
Patent Citations
Data reading method, memory control circuit unit and memory storage device
CN108959109A
Secondary mapping table data mapping method and device, storage medium and electronic equipment
CN112100091A