4

I'm currently trying to code a FAT system in C on a Xillinx Kintex 7 card. It is equipped with a MicroBlaze and I've already managed to create most of the required functions.

The problem I'm facing is about the total capacity of a folder, I've read on the web that in FAT32 a folder should be able to contain more than 65 000 files but with the system I've put in place I'm limited to 509 files per folder. I think it's because of my comprehension of the way FAT32 works but here's what I've made so far:

  • I've created a format function that writes the correct data in the MBR (sector 0) and the Volume ID (sector 2048 on my disk).
  • I've created a function that writes the content of the root directory (first cluster that starts on sector 124 148)
  • I've created a function that writes a new folder that contains N files of size X. The name of the folder is written in the root directory (sector 124148) and the filenames are written on the next cluster (sector 124212 since I've set cluster size to 64 sectors). Finally, the content of the files (a simple counter) is written on the next cluster that starts on sector 124276.

Here, the thing is that a folder has a size of 1 cluster which means that it has a capacity of 64 sectors = 32KB and I can create only 512 (minus 2) files in a directory! Then, my question is: is it possible to change the size of a folder in number of cluster? Currently I use only 1 cluster and I don't understand how to change it. Is it related to the FAT of the drive?

Thanks in advance for your help!

NOTE: My drive is recognized by Windows when I plug it in, I can access and read every file (except those that exceed the 510 limit) and I can create new files through the Windows Explorer. It obviously comes from the way I understand file creation and folder creation!

  • How do you handle files that are larger than one cluster? – Neil May 10 '16 at 10:16
  • I've tried to keep it simple and avoid to fragment every file, currently the files are stored sequentially and the FAT points directly to the next cluster (the 6th points to the 7th and so on until end of file 0xFFFFFF0F) which means that first the space for a file is reserved in the clusters and the FAT is written accordingly, then I write data in the files. From your question I understand that folders work the same way, is that so? Then, is it possible to reserve, let's say, 16 clusters (approximately 8160 files) and set up the FAT accordingly? Thanks! – Arthur Penguin May 10 '16 at 10:39

2 Answers2

3

A directory in the FAT filesystem is only a special type of file. So use more clusters for this "file" just as you would with any other file.

The cluster number of the root directory is stored at offset 0x2c of the FAT32 header and is usually cluster 2. The entry in the cluster map for cluster 2 contains the value 0x0FFFFFFF (end-of-clusters) if this is the only cluster for the root directory. You can use two clusters (for example cluster 2 and 3) for the root directory if you set cluster 3 in the cluster map as the next cluster for cluster 2 (set 0x00000003 as value for the entry of cluster 2 in the cluster map). Now, cluster 3 can either be the last cluster (by setting its entry to 0x0FFFFFFF) or can point in turn to another cluster, to make the space for the root directory even bigger.

The clusters do not need to be subsequent, but it usually has a performance gain on sequential reading (that's why defragmenting a volume can largly increase performance).

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • I tried to reproduce the same function that I created for the file creation and everything is working properly, thanks for your help! – Arthur Penguin May 10 '16 at 21:01
2

The maximum number of files within a directory of a FAT file system is 65,536 if all files have short filenames (8.3 format). Short filenames are stored in a single 32-byte entry.

That means the maximum size of a direcotry (file) is 65,536 * 32 bytes, i.e. 2,097,152 bytes.

Short filenames in 8.3 format consists of 8 characters plus optional a "." followed by maximum 3 characters. The character set is limited. Short filenames that contain lower case letters are additionally stored in a Long File Name entry.

If the filename is longer (Long File Name), it is spread over multiple 32-byte long entries. Each entry contains 13 characters of the filename. If the length of the filename is not a multiple of 13, the last entry is padded.

Additionally there is one short file name entry for each Long File Name entry.

2 32-byte entries are already taken by the "." and ".." entries in each directory (except root).

1 32-byte entry is taken as end marker?

So the actual maximum number of files in a directory depends on the length of the filenames.

katce
  • 21
  • 4