The method for managing security of file system and device
Technical field
The present invention relates to the file system safe management, particularly relate to the method for managing security and the device of file system in a kind of movable storage device.
Background technology
Development along with society, the function of mobile phone is more and more, its memory capacity is also increasing, the user can regard mobile phone as the equipment that a multi-functional mobile information is handled, not only have functions such as phone, note, but also can be used as mobile storage information, therefore, produced the demand of mobile phone as movable storage device.In addition, because the overwhelming majority adopts file allocation table systems (FAT, FileAllocation Table) on PC, so existing most of mobile phones also are to adopt the FAT file system to adapt with it.But, at present also there are the characteristic of storage medium Flash and the shortcoming of FAT file system on the mobile phone, make that the FAT file system lacks functions such as power down protection, fragment recovery, abrasion equilibrium on the mobile phone.
Therefore, present FAT file system also is not suitable for directly using in the mobile phone, its reason has two: the first, the mobile phone application conditions is abominable more than PC, unstable and the sudden power down of supply voltage will cause the storage of flash memory Flash and have a strong impact on, and the FAT file system is considered deficiency for Design in Reliability; The second, described FAT file system is developed at the profuse desktop platform of system resource, its objective is and need expend more system resources in order to promote the velocity characteristic of file system.Therefore, these factors are very limited for the system resource of cell phone apparatus, and the Flash storer is with respect to disc driver applicable cases difference faster.
At present, at above-mentioned reason, its solution mainly is that the data structure of Main Boot Record, file allocation table and three file system of root directory during FAT is shown backs up, to reduce the possibility that causes the file system collapse because of the damage of these three data structures.But this solution only plays the function of some power down protections, for the fragment of Flash itself reclaim, abrasion equilibrium but do not play substantial effect.
This be because: Flash has two big labile factors in the mobile phone: the one, itself the damage of physical property may appear in the Flash storer, and is too much as erasing times; The 2nd, mobile phone is faced more burst power down and is restarted, and causes the abnormal end of Flash memory write operation.And in the existing method, the data structure of backup Main Boot Record, file allocation table and three file system of root directory is a kind of reasonable modes, but,, can not solve Flash storer itself and the damage of physical property may occur though this method can solve factor two preferably.In addition, if the data structure of these three file system adopts the mode of fixed position storage, because the data structure of described three file system is all in preceding several sectors of storage space, and these sectors substantially all can because the operational ton of FAT table is very big, cause this block often to be wiped free of, to write on same block, and damage easily, that is to say, if, the physical damage of fixed position will occur because the repeatedly Flash of these fixed positions wipes.This will cause the collapse of file system.
Summary of the invention
The technical matters that the present invention solves provides a kind of method for managing security and device of file system, is the file system of unit and is the unmatched problem of physical characteristics of the flash memory of unit with the piece to solve in the present technical scheme with the sector.
For addressing the above problem, the invention provides a kind of method for managing security of file system, described method comprises step:
A, the flash memory after the initialization is divided into a plurality of logic sectors, and marks the state of each logic sector;
B, in flash memory, set up the mapping table of logic sector and physical sector;
C, when write data in logic sector, by searching or revise mapping table abnormal conditions are carried out recovery management.
Initialization described in the steps A is to convert the file allocation table system to new file system according to the requirement of safety management to download in the flash memory.
The principle of described conversion comprises: the file of file allocation table system is copied in the flash memory as sectors of data one by one respectively, and the zone bit that marks this sector is effective, and the sector number of the logic sector of this sector correspondence is also write in the information of this sector.
The implementation procedure of setting up logic sector and physical sector mapping table among the step B is:
B1) obtain the physical sector number of first logic sector;
B2) from first logic sector, obtain the size of current file system;
B3) scan the shared flash area of current file system, in internal memory, set up the mapping table of logic sector and physical sector.
The size of file system step B2) is that total sector number and sector-size are long-pending;
Step B3) specific implementation process is:
B31) the shared flash area of scanning current file system, the information of physical sector in the information of acquisition flash block and the piece;
B32) being marked when effective when detecting physical sector, obtaining its pairing logic sector number from the information of this physical sector, is index with this logic sector number, sets up the mapping table of logic sector number and physical sector number.
Described physical sector number is the sign that is divided into a plurality of physical sectors in flash memory by the order of physical address; Described physical sector number is started from scratch, so that find the actual address of physical sector according to physical sector number.
Described comprises a plurality of physical sectors, and wherein, first physical sector and second physical sector have been stored the information of each physical sector in the information of current block and this piece respectively.
The specific implementation process of described step C is:
C1) when to the logic sector write data, judge whether current piece is full; If execution in step C2 finishes; Otherwise, execution in step C3;
C2) judged whether free block,, then from the chained list of free block, obtained a piece, changed the status information of current block if having; If do not have, then execute and reclaim, provide free block;
C3) state of current sector is changed into invalid, and write a sector in current block, the status information of change sector is available;
C4) query mappings table judges whether this logic sector corresponding physical sector is empty, if, revise mapping table, make logic sector point to current sector; If not, this physical sector is changed into and can wipe.
In addition, the present invention also provides a kind of security control apparatus of file system, and described device comprises:
Division unit is used for the flash memory after the initialization is divided into a plurality of logic sectors, and marks the state of each logic sector;
Set up the unit, link to each other, be used to set up the mapping table of logic sector and physical sector with division unit;
The recovery management unit and is set up the unit and is linked to each other, and is used for to the logic sector write data time, recovers various abnormal conditions by searching or revise mapping table.
The described unit of setting up comprises:
Obtain subelement, link to each other, be used to obtain the physical sector number of first logic sector with the division subelement;
Computation subunit and is obtained subelement and is linked to each other, and is used for calculating the size that obtains the current file system from this first logic sector;
Mapping table is set up subelement, links to each other with computation subunit, is used to scan the shared flash area of current file system, sets up the mapping table of logic sector and physical sector in internal memory.
The content of described mapping table comprises: piece, logic sector number, mapping table and physical sector number.
Described comprises a plurality of physical sectors, and first and second physical sectors are stored the information of each physical sector in the information of current block and this piece respectively.
Described recovery management unit comprises:
Data write subelement, set up subelement with mapping table and link to each other, and are used for to the logic sector write data;
Judgment sub-unit writes subelement with data and links to each other, and writes fashionablely as data, is used to judge whether current piece is full, and sends judged result;
Abnormal restoring is handled subelement, links to each other with judgment sub-unit, is used to receive the judged result that judgment sub-unit sends, and according to judged result abnormal conditions is recovered to handle accordingly.
By above-mentioned disclosed technical scheme as can be known, the present invention earlier converts the file allocation table system mirror image of new file system to according to the requirement of safety management, and it is downloaded in the flash memory; According to the characteristics of flash memory self, between FAT file system and driver, increase one deck flash memory transport layer then.After starting flash memory, set up the mapping table of logic sector and physical sector; In setting up the process of mapping table, promptly in logic sector, during write data, abnormal conditions are carried out recovery management by searching or revise mapping table.Thereby solve with the sector and be the file system of unit and be the unmatched problem of flash memory physical characteristics of unit with the piece.In addition, technical scheme of the present invention can also play the protection of flash memory power down protection, flash memory abrasion equilibrium and the effect of flash memory fragment recovery.
Description of drawings
Fig. 1 is the process flow diagram of the method for managing security of file system of the present invention;
Fig. 2 is a process flow diagram of setting up the mapping table of logic sector and physical sector in the method for the invention;
Fig. 3 is the structural representation of the mapping table of logic sector and physical sector in the method for the invention;
Fig. 4 is the structural representation of piece in the method for the invention;
Fig. 5 is at the process flow diagram of write data in logic sector in the method for the invention;
Fig. 6 be in the method for the invention when write data in logic sector, recover the process flow diagram of various abnormal conditions;
Fig. 7 is the structural representation of the security control apparatus of file system of the present invention.
Embodiment
Core of the present invention is the physical characteristics according to flash memory Flash itself, adopts the mode of floating position storage to preserve Main Boot Record, file allocation table, root directory and user data.The core of described floating position storage mode is to add last layer flash memory transmission medium (FTL, Flash Translation Layer) in file allocation table system (FAT, File Allocation Table) with between driving.Be that the FAT file system is operated logic sector, and driver is operated the absolute physical address, the two again the FTL layer change.Solution is the file system of unit with sector (512 byte) and is the unmatched problem of flash memory Flash physical characteristics of unit with piece Block (being generally the multiple of 512 bytes).
For the ease of those skilled in the art's understanding, the present invention is described further below in conjunction with accompanying drawing.
See also Fig. 1, be the process flow diagram of the method for managing security of file system of the present invention.Described method comprises step:
Step S11: the flash memory after the initialization is divided into a plurality of logic sectors, and marks the state of each logic sector;
Step S12: the mapping table of in flash memory, setting up logic sector and physical sector;
Step S13: when write data in logic sector, abnormal conditions are carried out recovery management by searching or revise mapping table.
Power down protection strategy of the present invention is closely related with flash memory flash; in step S11 by flash being carried out subregion and marking the state of each subregion; need initialization then to flash; be about to flash and do unified management; such as which sector of mark is idle; which sector is effective or the like, promptly writes the needed information of management on flash.
Initialization to flash of the present invention mainly is the new file system mirror image that converts to according to the requirement of safety management by with the image of FAT file system, and it is downloaded to flash realizes.Because described image form has comprised the information of sector of the present invention, and this information is that the used flash sector auxiliary information of flash memory transport layer ftl is consistent, and the flash after having downloaded has had very complete flash management information in fact.
Wherein, the principle of described conversion is: regard the image (image) of former FAT as sectors of data one by one, then with sector data copy (copy) one by one in flash memory, and on the zone bit of this sector, be labeled as effectively, the sector number with this pairing logic sector in sector also writes in the block of information of this sector simultaneously.After sector copy copy finishes, flash is gone up remaining sector be labeled as idle free.
In step S12, behind the described startup flash memory, set up the mapping table of logic sector and physical sector.In the time will setting up mapping table, the flash zone that beginning scanning document system takies, obtain the information of flash block (flashblock) and the information of the middle sector of this piece (block), be noted as when effective when detecting current physical sector, then can from the block of information of described physical sector, obtain the sector number of the pairing logic sector of this physical sector, sector number with this logic sector is the index of mapping table then, and the sector number of physical sector is written in the mapping table.Wherein, the meaning of described physical sector number is: by sequence of addresses flash is divided into sector, a sector on flash, each sector is all identical.And physical sector number so just can find the actual address of this physical sector according to physical sector number since 0.Its concrete implementation procedure sees Fig. 2 for details, and described process is:
Step S121: the physical sector number of obtaining first logic sector.During flash memory transport layer FTL initialization, can scan flash memory, and set up the mapping table of logic sector, from this table, can obtain the physical sector number of logic sector 0 (sector0), i.e. physical address to physical address.
Step S122: the size that from first logic sector, obtains the current file system.The size of described file system is that total sector number and sector-size are long-pending, this is because in logic sector 0, because the FAT file system has been stipulated the storage organization of sector0, therefrom can obtain total sector number, described total sector number multiply by sector-size just can obtain the file system size.
Step S123: scan the shared flash area of current file system, in internal memory, set up the mapping table of logic sector and physical sector.In setting up the process of mapping table, can repair because the mistake that various accidents (power down) cause.That is to say the shared flash area of scanning current file system, the information of sector in the information of acquisition flash block and the piece; Being marked when effective when detecting physical sector, obtaining obtaining in the information of this physical sector the sector number of institute counterlogic sector from the information of this physical sector, is the index of mapping table with this logic sector number, and the sector number of physical sector is write in the mapping table.Wherein, of the present invention comprises a plurality of physical sectors, and first physical sector and second physical sector (physical sector 0 and physical sector 1) have been stored the information of each physical sector in the information of current block and this piece respectively.
By above-mentioned implementation procedure as can be known, which piece block the user does not need to know deposit data at, only need know the logic sector position of depositing.The mapping table structure of described logic sector and physical sector comprises as shown in Figure 3: piece (block), logic sector (such as 0,1,2 etc.), mapping table map (such as 2,1,5 etc.) and physical sector (such as 0,1,2 etc.).The corresponding connection with the map epiphase of wherein said logic sector is with physical sector journey mapping relations.
And form by several physical sectors (sector) at each Block described in Fig. 2, wherein sector0 and sector1 have preserved each sector information among current Block information and this Block respectively.The structure of described Block specifically as shown in Figure 4.Described sector0 and sector1 have preserved the Block information of 4byte respectively, and the information of the sector of the sector2 to 5byte of 5byte (N-1); Described sector2 to sector (N-1) has preserved each sector of file system respectively, comprises original sector data message in N-2 the file system among the block.
Wherein, the content of the block information of current 4 byte preservation sees table 1 for details; And the state of Block described in the table 1 sees table 2 for details.
Table 1
The Block state | Other |
1byte | 3byte |
Table 2
The Block state | Numerical value | Explanation |
Idle (FREE) | 0xFF | Be indicated as free block (block) |
Current (CURRENT) | 0xFE | Be indicated as current block (current block) |
Available (USED) | 0xFC | Be indicated as active block (block), stored valid data, all sectors (sector) all finishes using |
Dirty (DIRTY) | 0Xf8 | All sectors (sector) in the piece (Block) all are to wipe (dirty) |
The content that the information of described each 5byte sector (sector) is preserved sees table 3 for details, and the state of sector described in the table 3 sees table 4 for details.
Table 3
The sector state | Logic sector number |
1byte | 4byte |
Table 4
The sector state | Numerical value | Explanation |
Idle (FREE) | 0xFF | Be indicated as the sky sector |
Invalid INVALID | 0xFE | Show invalid data |
Available (USED) | 0xFC | Show valid data |
Dirty (DIRTY) | 0xF8 | Show erasable data |
Described in the step S13 during, abnormal conditions are carried out recovery management by searching or revise mapping table in write data in logic sector.That is to say, in file system of the present invention, all be to finish to the write operation of data by logic sector, what the user write on the surface is logic sector, and be actually write operation by logical data, data are write among the different physical sector sector of different masses Block.Wherein, the process flow diagram of described write operation data sees Fig. 5 for details, and the performing step of described write operation data comprises:
Step S131: when to the logic sector write data, judge whether current piece is full; If, execution in step S132; Otherwise, execution in step S135;
Step S132: judged whether free block,, then from the chained list of free block, obtained a piece, changed the status information of current block, finished (step S133) if having; If do not have, then execute and reclaim, provide free block, finish (step S134);
Step S135: it is invalid that the state of current sector is changed into, and write a sector in current block, and the status information of change sector is available;
Step S136: the query mappings table, judge whether this logic sector corresponding physical sector is empty, if, revise mapping table, make logic sector point to current sector (step S137); If not, this physical sector is changed into can wipe (step S138).
Without any influence, key is the power down protection in write operation to system in the power down of technical scheme of the present invention in flash memory Flash read operation.From above-mentioned steps as can be known, power down in the write operation of flash memory may have only four kinds of situations to what file system exerted an influence, technical scheme of the present invention can be recovered these four kinds of possible situations in the flow process by start, and its detailed process of recovering these four kinds of power-down conditions sees Fig. 6 for details.Suppose that described four kinds of power-down conditions are made as power down A, power down B, power down C and power down D respectively.
As shown in Figure 6, for power down A, current block (current block) state changes and finishes, and the state of original current block (current block) does not also change; Find to have two current blocks (current block) when the result who causes is initialized, if write data at this moment, systems inspection is empty to which piece block, and just confirming as this piece is current block (current block).For power down B, the sector sector of invalid (invalid) changed into dirtyly maybe can wipe (dirty).For power down C, because this change procedure only need be changed one, if change is not finished, the status information of this sector sector is still invalid (invalid) so at this moment, and the mode of its processing is with power down B.Do not repeat them here.For power down D, the effect that causes owing to power down is to find to have the corresponding same logic sector (logical sector) of two physical sectors (physical sector) in mapping table, be as the criterion with the content in the current block (current block) this time, just gets up-to-date data.
In addition, in power down process of the present invention, also should be noted that a kind of special circumstances, i.e. power down in removal process.What in fact described removal process carried out is copying data or data erase operation.Power down when copying among the piece block valid data is similar to the process of writing for copy (copy) class of operation of each sector (sector), and its power down process is identical with the process of above-mentioned power down A, B, C and D, specifically sees for details above-mentioned.If power down when wiping a piece block is because each sector sector of this piece block maybe can wipe (dirty) for dirty.So it also is the same can wiping (erase) to it when recovery is carried out in start next time.
In internal memory, preserve the form of a start back piece block erasing times.In recovery, pay the utmost attention to the less piece block of erasing times and reclaim.Played the function of abrasion equilibrium like this.Such as, two free block block are arranged, erasable mistake 5 times, another had been wiped 3 times, and that is just preferential to use second piece block that had wiped 3 times, thereby avoids too many to the same erasable number of times of block, causes too early damage.Can be according to the idle ratio of sector among the every Block, drawing needs the Block that reclaims to reclaim.That is to say that FTL can regularly check the free block block number of current existence, if be lower than certain particular value, just begin removal process.In removal process, can find and to wipe maximum that piece block in sector (dirty sector), the useful content on it is moved on other pieces block, wipe this piece block then, and produce a new free block block.
In addition, the present invention also provides a kind of security control apparatus of file system, and its structural representation sees Fig. 7 for details, and described device comprises: division unit 11, set up unit 12 and recovery management unit 13.Wherein, described division unit 11 is used for the flash memory after the initialization is divided into a plurality of logic sectors, and marks the state of each logic sector; The described unit 12 of setting up links to each other with division unit 11, is used to set up the mapping table of logic sector and physical sector; Described recovery management unit 13 and is set up unit 12 and is linked to each other, and is used for to the logic sector write data time, recovers various abnormal conditions by searching or revise mapping table.
The described unit 12 of setting up comprises: obtain subelement 121, computation subunit 122 and mapping table and set up subelement 123.The described subelement 121 that obtains links to each other with division subelement 11, is used to obtain the physical sector number of first logic sector; Described computation subunit 122 and is obtained subelement 121 and is linked to each other, and is used for calculating the size that obtains the current file system from this first logic sector; Described mapping table is set up subelement 123, links to each other with computation subunit 122, is used to scan the shared flash area of current file system, sets up the mapping table of logic sector and physical sector in internal memory.The content of described mapping table comprises: piece, logic sector number, mapping table MAP table and physical sector number.And described comprise a plurality of physical sectors, and described first and second physical sectors are stored the information of each physical sector in the information of current block and this piece respectively; Other physical sector has been stored each physical message of file system respectively, comprises original physical sector data message in N-2 the file system among the piece block.
Described recovery management unit 13 comprises: data write subelement 131, judgment sub-unit 132, and abnormal restoring is handled subelement 133.Described data write subelement 131, set up subelement 123 with mapping table and link to each other, and are used for to the logic sector write data; Described judgment sub-unit 132 writes subelement 131 with data and links to each other, and writes fashionablely as data, is used to judge whether current piece is full, and sends judged result; Described abnormal restoring is handled subelement 133, links to each other with judgment sub-unit 132, is used to receive the judged result that judgment sub-unit sends, and according to judged result abnormal conditions is recovered to handle accordingly.
The realization function of each unit of the above device sees also the implementation procedure of said method, does not repeat them here.
The above only is a preferred implementation of the present invention; should be pointed out that for those skilled in the art, under the prerequisite that does not break away from the principle of the invention; can also make some improvements and modifications, these improvements and modifications also should be considered as protection scope of the present invention.