Navigation
Highlights

Release 0.0.5

The latest release, alpha testing, unstable. See "downloads".

2005-06-01

Links
Documentation

FAT file system driver

The FreeDOS-32 FAT driver is a loadable module for FreeDOS-32 that allows to use the FAT file system, the standard one for DOS and Windows machines. This document is intended for those people who want to use its features and/or study the source code.

Contents

Introduction

The FreeDOS-32 FAT driver is a highly portable driver to access media formatted with the Microsoft FAT file system. It provides services to read and write existing files, as well as to create new files and directories and to delete them. It relies on very few external facilities, such as C library functions and the means to access a block device such a disk medium.

FAT driver versions

The FAT driver has evolved over the time. Most notably, the current version of the FAT driver, that is version 2.0, is mostly a rewrite of the driver, much cleaner and more efficient than the previous version. Version 1.0 is superseded by version 2.0 and is no longer maintained.

Features

The following is a summary of the features of the FAT driver:

  • facilities implemented using the Single Unix Specification (equivalent to POSIX) as a reference;
  • support for all FAT file system variants FAT12, FAT16 and FAT32;
  • cached I/O though internal block buffers to compensate for the speed issues of the FAT file system;
  • support for long file names, that can be disabled with a compile-time option;
  • write support can be disabled with a compile-time option;
  • mostly self-contained as a library.

Getting the FAT driver

The most up to date version of the FAT driver can be found in the CVS repository of the FreeDOS-32 project, checking out the fd32 CVS module. The FAT driver is currently stored in the modules/fat2 directory, while the modules/fat directory currently stores the superseded 1.0 version. In the future the modules/fat directory will be used for the current version.

A snapshot of the source of the FAT driver is included in the FreeDOS-32 alpha releases, that you can download from the FreeDOS-32 web site, but this could be outdated in comparison with the code in the CVS repository.

Licensing terms

The FreeDOS-32 FAT driver is free software, distributed under the terms of the GNU General Public License. This means that you are free to use, modify and redistribute it using the same licensing terms, that basically means that you are free to include it in free programs.

You can't use the FAT driver in a proprietary system under these licensing terms.

The FAT driver comes with NO WARRANTY. The write facilities of the current version are quite young, thus they might contain unknown bugs dangerous for your data.

The FAT file system

This section provides a discussion on the architecture of the FAT file system. It is intended to show what implementation issues are behind the FreeDOS-32 FAT driver. This discussion has been kept simple, without explaining all the details. You can learn more from the documentation at the Operating System Resource Center.

Overview

The FAT file system gets its name from the Fat Allocation Table, a structure used to keep track of how files are stored in a volume. There are three variants of FAT file systems: FAT12, FAT16 and FAT32. The main difference between them is the number of bits used for each entry of the file allocation table: 12, 16 and 32 bits respectively. Larger entries allow to address larger volumes.

A FAT volume is logically subdivided in blocks called clusters, usually formed by several disk sectors. A cluster is the minimum allocation unit of the FAT file system: file occupations are described in terms of clusters, thus a whole cluster can be free or allocated for a file, even if only a small part of the cluster extension is necessary to store part of a file. The FAT type (FAT12, FAT16 or FAT32) is determined by the count of clusters on the volume.

There are four important areas in a FAT file system: the boot sector, the file allocation table, the root directory area (not present in FAT32 volumes) and the data area.

Boot sector

The boot sector is the first sector of a FAT volume. It contains a small program to load an operating system in case it is installed on the FAT volume, but also a data structure called the BPB (BIOS Parameter Block), which stores information about the volume itself, like its size, the number of bytes per sector, the number of sectors per cluster and the like. Moreover the BPB contains information to find the location of the other areas in the volume. The FreeDOS-32 FAT driver uses the BPB to know how to deal with a FAT volume.

File allocation table

The file allocation table (FAT) is an on-disk array to store the allocation status of each cluster of the volume. Each entry of the FAT is 1:1 mapped with the clusters of a volume, and it is an integer number of 12, 16 or 32 bits, according to the FAT type (only 28 bits are actually used in FAT32 entries).

For each cluster of the volume, its corresponding FAT entry stores the address (the cluster number) of the next cluster of that file. Thus, the FAT file system organizes the set of clusters allocated by a file in a linked list, keeping track of the occupation of each file using an allocation chain in the file allocation table: once the first cluster of a file is known, a FAT driver traverses the FAT to find the other clusters of the file.

Some values for a FAT entry are reserved to indicate that a cluster is free, or bad (damaged) or the last cluster of a file (the EOC mark, End Of Clusterchain). The first two entries of the FAT are reserved, so the address of the first legal data cluster is 2. Finally, in FAT volumes there is usually stored more than one copy of the FAT (usually two), for backup purpose.

Root directory area

The root directory area is a fixed-size table following the FAT, and stores the content of the root directory of the volume. The standard directory format is used, see below. FAT32 volumes have no such a special area for the root directory: it is just like any other directory, i.e. it can be anywhere in the data area of the volume, and its first cluster is stored into the BPB. This means that the root directory in non-FAT32 volumes can only hold a limited (usually small) amount of files.

Data area

After the FAT and the root directory (if present), the data area is formed by all the remaining space of the volume. File system objects (files and directories) are stored here. As said, the data area is logically subdivied in clusters, and each file system object is stored in an integer amount of clusters, that in general are not contiguous.

For each cluster its corresponding entry in the file allocation table stores the address of the next cluster. To access a cluster of a file, the linked list of clusters (the cluster chain) must be traversed to know the cluster location. The FreeDOS-32 FAT driver caches the location of the last accessed cluster in order to avoid to scan the cluster chain every time, unless it is required to access a preceding cluster.

When a file is read sequentially, a FAT file system driver must continuously jump back and forth between the data area and the file allocation table, to read data and to know the location of the next cluster. This has of course a serious performance hit as the underlying block device (for example a disk) must perform a lot of seeks. For this reason the FreeDOS-32 FAT driver provides buffered access, caching the most recently used blocks, greatly improving the I/O performance.

Directory format

In the FAT file system a directory is just a file. The content of this file is a table of 32-bytes entries which describes the list of files in that directory. A 32-byte directory entry contains the file name, attributes, time stamps, the first cluster of the file (needed to start to traverse the cluster chain) and the file size. Thus, all the metadata of a file are stored in its directory entry. Each file can have only one directory entry, i.e. hard links are not supported.

If a file is a directory, it has the directory attribute set and its size is zero. If a non-directory file is zero bytes long, no cluster is allocated and its first cluster value is set to zero. The address of the starting cluster of a file can be used as a file serial number (also called inode number) to uniquely identify a file, but since any zero-length file has its starting cluster set to zero this makes assigning serial number to files more difficult. The FreeDOS-32 FAT driver uses the location of the directory entry for zero-lenght files, since there are no hard links. This is used, for example, to know if a certain file is already open.

Directory entries in a directory are not sorted, thus looking up a file in a directory can be a slow operation. This is especially true when a pathname has to be resolved, because each component of the pathname must be looked up.

Long file names

A 32-byte directory entry contains a field for the file name that is only 11 bytes long: 8 bytes for the name and 3 for the extension, the separating dot is not stored. To introduce long file names (LFN), the architecture of the FAT file system has been hacked to allow extra "special" directory entries. Many use the denomination VFAT for this modified FAT file system.

Please note that the long file names capability is not directly related with FAT32: you can have long file names on FAT12, FAT16 and FAT32 volumes. People often confuse LFN with FAT32 because they were introduced almost at the same time, upon the launch of two near revisions of Windows 95.

No regular file can have the volume label attribute bit set together with other attribute bits, so this "impossible" attribute mask is used to indicate a special directory entry, used to store a part of a long file name. Each LFN directory entry stores up to 13 characters of a long name in Unicode UTF-16 (16 bits per character). The first cluster field of such a directory entry is always set to zero, and several checks are used to ensure directory entry consistency.

For each long file name, a standard directory entry is also created, to store attributes, time stamps, first cluster and file size, and also a short name alias for compatibility with older applications. A long file name can be longer than 13 characters, and in this case several LFN entries are used. Each entry has a progressive counter, and an 8-bit checksum of the short name alias. The LFN entries are stored in reverse order, from the last to the first, and right after them, the short name alias directory entry with the metadata.

Portability

This section, to be done, will describe how to port the FreeDOS-32 FAT driver to other platforms.