Reputation: 121
To my naivete , everything stored in the secondary memory (hard disks) are stored in files. As such I understand of file system as the systematization of the information about each and every block of a file .(This in turn might turn out to be a systematization of the information about inodes) .
So what I wanted to know is what happens at assembly and hardware level when a program intends to do an operation on a file . I tried thinking about it and it was along the following lines .
So if my understanding , which I wrote down in the first paragraph , is correct ,then any read operation demanding a certain number of bytes , let's say X , will divide X by block size to get a result ,say N , then query the file system for the location of the contents of the first N blocks and fetch the content stored in those locations . If it is a hard disk then the contents are fetched from the disk . But does this reading from disk involve using DMA and hence the communication being controlled through some DMA controller ?
When the required number of bytes are read then do they place the content in the stack of the program executed where a variable will be holding the required number of bytes or the hence obtained contents are placed in some other location , like , heap ?
Similar mechanism I could think of for write operation , where the offset is taken , the required block number is determined , then the new bytes to be written are divided into blocks and the new blocks are updated in the file-system .
Please correct if my above understanding is wrong .
However, I could not think of anything significant for closing of files and could not figure out the ill effects of not closing files in terms of hardware and assembly .
I have been through the following posts :
http://www.brokenthorn.com/Resources/OSDev20.html
and
http://www.brokenthorn.com/Resources/OSDev20.html .
Here , I can see the illustration in assembly for writing to a floppy device or doing DMA but in the following chapter :
http://www.brokenthorn.com/Resources/OSDev22.html
which is about file systems I could not exactly find out the assembly level details of file operations . (Although in some of the previous chapters , reading from FAT had been covered but to the best of my memory it used BIOS interrupts ) .
So in a nutshell , what happens at hardware and assembly level during file opening , file reading , file writing and file closing ?
Upvotes: 1
Views: 758
Reputation: 18523
What do file operations look like at assembly level
In the application this is done using a system call (e.g. an int
instruction on x86 CPUs). Such a system call will call a function in the OS.
That function in the OS is typically a short function written in assembly that calls another function which is typically written in a "high-level" programming language (typically C; but writing this function in assembly is also possible of course).
That second function will call a lot of other functions.
On a hard disk or a floppy disk data is stored in form of sectors. Each sector contains a certain number of bytes (for example 512) and you can only read or write complete sectors, not single bytes. The disk does not know anything about files.
In a typical operating system two different types of functions are involved in file handling:
1) The device driver functionality
These functions are there to read or write "sector" data from or to the disk. It depends on the CPU and on the disk drive type how this is actually done.
In the simplest case you have a CPU with memory-mapped I/O and you neither use interrupts nor DMA. The historic Commodore 1581 floppy drive is an example for this.
In this case the software writes some data to the hardware by simply writing a byte to an address - the same way you write a byte to the RAM. Reading from the hardware works like reading a byte from the RAM.
The software will write some data to the hardware that instructs the disk drive to read some data from the disk. Then the software continuously reads data from a certain address. This data contains information if the disk drive is ready. As soon as the disk drive is ready, the software reads the actual data by reading data from another address. Writing works similarly.
If you use a CPU that do not use memory-mapped I/O, accessing the hardware does not work like accessing the RAM, but special instructions (in
and out
) are used. (Older x86 computers are an example of this.)
If interrupts are used (as in modern computers), the software does not need to check continuously if the disk drive is ready. A special function - a so-called "interrupt handler" - will be called by the CPU as soon as the disk drive reports to be ready.
If DMA is used (as in modern computers), the disk drive can directly write the data read from the disk to the RAM (or read the data to be written from the RAM). So the software only needs to write the special data that instructs the disk drive to write some sector. The software does not have to write the actual data that shall be written to the disk because the disk drive will directly read this data from the RAM. (The same is true for reading.)
2) The file system functionality
These functions will call the "device driver" functions to access the disk.
A file system is more or less nothing but some description how files are stored on a disk. If a disk uses the "FAT16" file system, there are three areas on a disk: The FAT, the root directory and the clusters.
(On file systems using "inodes" this is a bit different; however, the principle is the same.)
The FAT contains information about where some files are stored on a disk and which clusters are free. The root directory contains information about the names (and some more information such as the file size) of the files on the disk. And the clusters contain the actual file data.
When writing a file to the disk, the file system functions will read the sectors of the FAT (by calling the functions of the "device driver"). It will check the content of the FAT for free clusters. Now it will read the root directory, add the file name of the file written and write the root directory back to the disk. Then the functions will write the actual data of the file to the clusters. In the end, the functions will update the FAT (the corresponding sectors are no longer free) and the root directory (the file size is no longer zero) data and write this information back to the disk.
Upvotes: 1