简体   繁体   English

内存转换在FAT文件系统中如何工作?

[英]How does memory translation work in the FAT filesystem?

I am required to create my own implementation of a filesystem in C. I am planning on creating a system similar to that of the FAT system. 我需要用C创建自己的文件系统实现。我计划创建一个类似于FAT系统的系统。 We are given one file of size 10MB, which acts as our own "disk." 我们获得了一个大小为10MB的文件,它充当我们自己的“磁盘”。 I understand that the FAT table stores cluster numbers, and the Root Directory stores other pertinent information about each file we create (eg file name, size, date and time of last modification, start block in FAT, etc.). 我知道FAT表存储簇号,而根目录存储有关我们创建的每个文件的其他相关信息(例如,文件名,大小,上次修改的日期和时间,FAT中的起始块等)。 But I am confused about how the cluster numbers are translated to physical addresses in the data region on the disk. 但是我对群集号如何转换为磁盘上数据区域中的物理地址感到困惑。

For example, let's say an entry in the Root Directory says that a file starts in block 100 in the FAT table, and in block 100 of the FAT table is the integer 327, which is where the next cluster of the file is located. 例如,假设根目录中的一个条目说文件从FAT表的块100开始,而在FAT表的块100中是整数327,这是文件的下一个簇所在的位置。 How are these addresses translated to physical addresses in the data region of the disk? 这些地址如何转换为磁盘数据区域中的物理地址? Where are these physical addresses translated and stored? 这些物理地址在哪里转换和存储?

Clusters vary in size between different versions of FAT (FAT12, FAT16, and FAT32), but in general the cluster number points to a consecutively numbered cluster of whatever size is present in the format for the existing file system. 群集的大小在不同版本的FAT(FAT12,FAT16和FAT32)之间会有所不同,但通常,群集号指向的是连续编号的群集,无论其大小与现有文件系统的格式相同。 As I recall (from long ago) FAT12, at least on hard disks, used 2 kibibyte clusters (made up of four 512-byte sectors each), with a maximum cluster number of 2^11 (12 bits starting with zero), so cluster 327 would be 327 * 2048 bytes from the start of the data area of the disk. 我记得(很久以前),FAT12至少在硬盘上使用2 kb的群集(每个群集由四个512字节的扇区组成),最大群集数为2 ^ 11(12位从零开始),因此从磁盘的数据区域开始,群集327将为327 * 2048字节。

The data area includes the FAT, backup FAT, and all directories. 数据区域包括FAT,备份FAT和所有目录。 My recollection is that each cluster entry in the FAT contains a pointer to the next cluster in the file that occupies that cluster, length of data if it's the last cluster of the file, and some other information needed in reading or writing the file, while the directory entry contains the file name, first cluster, size/date/etc.. 我的回忆是,FAT中的每个群集条目都包含一个指向文件中占据该群集的下一个群集的指针,数据长度(如果它是文件的最后一个群集)以及读取或写入文件所需的一些其他信息,而目录条目包含文件名,第一个群集,大小/日期/等。

A disk is divided into sectors. 磁盘分为多个扇区。 A hard disk for example has a sector size of 512 bytes. 例如,硬盘的扇区大小为512字节。 Addressing data on the disk usually uses these sectors and data is read/written in blocks of this size. 在磁盘上寻址数据通常使用这些扇区,并且以这种大小的块读取/写入数据。 The FAT filesystem groups a number of sectors into clusters. FAT文件系统将多个扇区分组到群集中。 For example you could have 8 sectors per cluster. 例如,每个群集可能有8个扇区。 This constant is stored along with other information about the filesystem in the first few sectors of the partition. 该常数与有关文件系统的其他信息一起存储在分区的前几个扇区中。 The FAT driver uses this value to compute the sector number from the cluster number. FAT驱动程序使用此值从群集号计算扇区号。 The formula is something like this: 公式是这样的:

SectorNumber = SectorsPerCluster * ClusterNumber + Constant SectorNumber = SectorsPerCluster * ClusterNumber +常数

The constant is the sector number of the first sector of the data region of the partition. 该常数是分区数据区域的第一个扇区的扇区号。 You can find the exact formula in the FAT Specification. 您可以在FAT规范中找到确切的公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM