Summer 2003 Lecture 26 07/24/03 Organization of Data on the Disk The logical organization of the FAT file system on a disk is made up of the following elements. BOOT Sector Root Directory Structure File Allocation Tables (FAT) Data Clusters The BOOT sector is described in Section 9.2 of the text. It contains a data structure that describes the format of the disk, followed by the boot loader. The boot loader is a short program that loads the operating system from the disk into memory and starts it executing. In the case of DOS, the boot loader actually loads the operating system. In the case of many other operating systems, such as Windows or Windows NT, the boot loader in the boot sector only loads a more sophisticated loader, which then loads the actual operating system. The Root directory structure contains the top level directory structure of the hierarchical file system. The root directory is special, in that the information in it is stored in a hard coded location on the disk, and a fixed amount of space is allocated for it when the disk format is defined. If a disk has hard sector errors in the region where the root directory is stored, the disk is unusable. Subdirectories of the root directory are stored in special files A directory is a table of fixed sized directory entries. Each directory entry is 32 bytes long and contains the file name, creation and access dates and times for the file, file attributes, and all other information that DOS knows about the file. For the root directory, this table is stored at a fixed location on the disk. Subdirectories are special files that contain directory entries. Windows NT and versions of Windows beginning with Window 95, long file names are supported. Files with a long file name (or a name containing embedded spaces) will have multiple directory entries. There will be a directory entry containing a short file name (confirming to the 8.3 character file naming convention of DOS).
There will also be multiple additional directory entries containing the complete file name. The number of directory entries will vary depending on the length of the file name. These directory entries immediately precede the short file name entry in the directory, and contain reserved fields with special values that cause earlier versions of DOS to ignore them and to indicate that they are all bound together to make up a complete file name. One of the key pieces of information that is stored in the root directory is the head of the FAT chain. This is the information that allows the DOS file manager to find the data associated with the directory entry (i.e. the actual file data for the file). Space on a disk is allocated in units called a cluster. A cluster is some number of sectors that are treated by DOS as an atomic unit. When space is allocated for a file, it is always allocated a cluster at a time. On small disks, such as floppy disks, a cluster is 1 sector. On large hard disks, a cluster may be as large as 16 or 32 sectors. Each cluster on a disk is assigned an index number, starting at 0. The file allocation table (FAT) is an array of entries indexed by the cluster number. If the cluster is free, the FAT entry for cluster will have a special value in it indicating that it is free. If the cluster contains a bad sector, there will be another special value that marks the cluster as bad, and not to be used. Clusters that are part of a file, will be linked together into a linked list of all of the clusters that make up the file. So, for clusters that are part of a file, the FAT entry for a cluster will contain the index of the next cluster in the file. The last cluster assigned to a file will have a special value indicating that it is the end of the chain for that file. The directory entry for a file has a field that indicates the cluster number of the first cluster assigned to the file (this is the head of the linked list). DOS Functions for Accessing Files The common way of accessing files is the use the HANDLE based functions. This method of accessing files was introduced in DOS version 2.0 along with the hierarchical file system. In these functions, when a file is opened or created, DOS returns a 16 bit integer value called a file handle.
For future operations involving that file (e.g. reading, writing), the file handle is one of the parameters to the DOS call, and it specified which file the operation is to be performed on. With this set of functions, file names are represented as ASCII strings. The following are the file handle based functions used for accessing files under DOS:
Function 3Ch (60) Create a File (CREAT) Create a new file or open and truncate an existing file to zero length. On entry: AH 3Ch CX File attribute DS:DX Pointer to filename (Zeroterminated ASCII string) Returns: AX File handle, if CF is not set, or Error code, if CF is set Error codes: 3 Path not found 4 No handle available 5 Access denied (file is readonly, or root directory is full) Notes: The CX register specifies the attribute of the file you're creating, as follows: CX 00h 01h 02h 04h Attribute specified Normal Read-only Hidden System Sum the values to combine attributes. Fcn 3Dh Open File
Fcn 3Eh Close File Function 3Eh (62) Close a File Handle Flush the file's buffers, close the file, and release the file handle. On Entry: AH 3Eh BX File handle Returns: AX Error code, if CF is set Error codes: 6 Invalid handle ------------------------------------------------ This function flushes the file's buffers, closes the file, releases the handle, and updates the directory. Fcn 3Fh Read File Function 3Fh (63) Handle Read from File or Device, Using a Read bytes from a file or device into a buffer. On entry: AH 3Fh BX File handle CX Number of bytes to read DS:DX Address of buffer Returns: AX Number of bytes read, or Error code, if CF is set Error codes: 5 Access denied 6 Invalid handle Fcn 40h Write File Function 40h (64) Handle Write to File or Device, Using a Write bytes from a buffer to a file or device.
On entry: AH 40h BX File handle CX Number of bytes to write DS:DX Address of buffer Returns: AX Number of bytes written, or Error code, if CF is set Error codes: 5 Access denied 6 Invalid handle Fcn 41h Delete File (doesn t use a handle, uses ASCII string) Function 41h (65) Delete File (UNLINK) Delete the named file. On entry: AH 41h DS:DX Pointer to filespec (Zeroterminated ASCII string) Returns: AX Error code, if CF is set Error codes: 2 File not found 3 Path not found 5 Access denied Fcn 42h Fcn 45h Fcn 46h Fcn 4Eh Fcn 4Fh Fcn 57h Fcn 5Bh Seek File Pointer Duplicate File Handle Force Duplicate File Handle Find First Matching File Entry (uses ASCIIZ file name) Find Next Matching File Entry Read File Date and Time Force Create a New File (deletes existing file) Function 59h (89). DOS 3.0 Get Extended Error Information Returns diagnostic information following an unsuccessful DOS function call or Interrupt 24h.
On entry: AH 59h BX 0000h for DOS 3.0 and above Returns: AX Extended error code BH Error class BL Suggested action CH Locus (Source of error) Destroys: changed CL, DX, SI, DI, ES, and DS are all Values returned in AX (extended error code): AX Extended error code AX Extended error code 01h Invalid function number 14h Unknown unit 02h File not found 15h Drive not ready 03h Path not found 16h Unknown command 04h Too many open files 17h Data error (CRC) 05h Access denied 18h Bad request structure 06h Invalid handle length 07h Memory control blocks 19h Seek error destroyed 1Ah Unknown medium type 08h Insufficient memory 1Bh Sector not found 09h Invalid memory block address 1Ch Printer out of paper 0Ah Invalid environment 1Dh Write fault 0Bh Invalid format 1Eh Read fault 0Ch Invalid access code 1Fh General failure 0Dh Invalid data 20h Sharing violation 0Fh Invalid disk drive 21h Lock violation
10h Attempt to remove current disk change directory unavailable 11h Not the same device already exists 12h No more files make directory 13h Disk is write-protected error 22h Invalid 23h FCB 50h File 52h Cannot 53h Critical Values returned in BH (error class): BX Error class 01h Out of resource 02h Temporary problem that can be expected to go away 03h Authorization (permission) problem 04h Internal error (i.e., a system software bug) 05h Hardware problem (not the fault of your program) 06h System software failure (e.g. bad or missing CONFIG.SYS) 07h Application program error 08h File or other item not found 09h File or other item of invalid or unsuitable format or type 0Ah File or other item interlocked 0Bh Media problem (e.g. CRC error, wrong disk in drive) 0Ch Collision with existing item 0Dh Other (unclassified) Values returned in BL (suggested action): BL Suggested action 01h Retry a few times 02h Pause, then retry 03h Ask user to resupply input 04h Abort with cleanup (orderly shutdown) 05h Immediate abort (do not pass GO) 06h Ignore 07h Ask user for remedial action (e.g. insertion of diskette), then retry
Values returned in CH (source of error): CH Source of error 01h Unknown 02h Block device (disk) 03h Network 04h Serial device 05h Memory
File Operations Using File Handles The file handle based file operations in DOS were patterned after the low level file operations in the C runtime (e.g. open, read, write, etc), so file operations in DOS are very similar to those written in C using these low lever functions. Before a file can be accessed, it must be opened. The open function takes a pointer to a zero terminated string giving the file name (DS:DX), and an access type byte indicating if the file is being opened for read, write, or both (AL). If the file open is successful, the file handle is returned in AX. If not, an error code is returned in AX and the carry flag is set. Once the file is opened, the file can be read or written by passing the file handle (BX), a byte count (CX), and a pointer to the data buffer (DS:DX). If the operation is successful, the count of bytes read or written is returned in AX. If not, an error code is returned in AX and the carry flag is set. When access to the file is no longer needed, it should be closed.