how to make FAT table on disk up to date? while running

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
blackoil
Member
Member
Posts: 148
Joined: Mon Feb 12, 2007 4:45 am

how to make FAT table on disk up to date? while running

Post by blackoil »

It's not good to synchronize the FAT table on disk, per FILE read or write.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: how to make FAT table on disk up to date? while running

Post by System123 »

blackoil wrote:It's not good to synchronize the FAT table on disk, per FILE read or write.
Firstly I don't think this is a question. And secondly why would you want to sync the FAT table? What are you going to sync it to?
Gizmic OS
Currently - Busy with FAT12 driver and VFS
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: how to make FAT table on disk up to date? while running

Post by JAAman »

System123 wrote:
blackoil wrote:It's not good to synchronize the FAT table on disk, per FILE read or write.
Firstly I don't think this is a question. And secondly why would you want to sync the FAT table? What are you going to sync it to?
the other copy of the FAT table perhaps?
blackoil
Member
Member
Posts: 148
Joined: Mon Feb 12, 2007 4:45 am

Re: how to make FAT table on disk up to date? while running

Post by blackoil »

I copy the FAT to ram when startup, if read/write file, the FAT updates in ram.
It has to write it back to disk.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: how to make FAT table on disk up to date? while running

Post by System123 »

blackoil wrote:I copy the FAT to ram when startup, if read/write file, the FAT updates in ram.
It has to write it back to disk.
The FAT never changes when you read. You will only need to change FAT on a write. But if you are writing to the disk you shouldn't be writting the FAT entries to RAM, this will cause trouble later.

You also need to update FAT2 table on the disk as it is a back up.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
blackoil
Member
Member
Posts: 148
Joined: Mon Feb 12, 2007 4:45 am

Re: how to make FAT table on disk up to date? while running

Post by blackoil »

yes, I know a read doesn't change the FAT.
If write back the FAT per writefile, it's very slow for only 5gb partition.
If I cache the FAT table, it's fast

FileOpen
{
if no files opened before, read the FAT to ram
open the file
}

FileClose
{
close the file
if no files left open, write back the FAT to disk
}

does this works best? if no power loss when running.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: how to make FAT table on disk up to date? while running

Post by System123 »

blackoil wrote:FileOpen
{
if no files opened before, read the FAT to ram
open the file
}

FileClose
{
close the file
if no files left open, write back the FAT to disk
}

does this works best? if no power loss when running.
The first part will work fine. The second part is a problem. Because what if the system crashes, but you still had files open. The 2nd one could work if you sync the FAT on any change. Here is a fast way to do it.

Keep 2 arrays of the 10 bytes, (the last 5 in the FAT, and the first 5 after the last FAT entry. 1 for RAM and 1 for Disk). Then after x delay check that those bytes are the same. If they are the FAT is fine, if they not then copy the FAT.

You could also just copy the FAT on every successful write to the drive. This is probably the easier and quicker route.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: how to make FAT table on disk up to date? while running

Post by jal »

There's of course only a need for updating the FAT if the write causes the file to expand, in which case you need to allocate new blocks for the file. Typically, allocating blocks on a disk and writing a file are separate tasks, executed by separate parts of the VFS/driver. Also, you only need to write the FAT once on a single data write, as you know on forehand how much the file is going to expand, so you can reserve room and update the FAT only once. Next, you write the data. Anyway, you need update the directory entry as well, writing causing file expansion is just slow, live with it.


JAL
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: how to make FAT table on disk up to date? while running

Post by Dex »

In my OS any write is done in ram, than written to disk ( as in no waiting ), But mine is designed a a single-task games console OS.
One thing i would add is, i only write to the first fat on my OS.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: how to make FAT table on disk up to date? while running

Post by jal »

Dex wrote:One thing i would add is, i only write to the first fat on my OS.
Which means that the disk is unreadable by any other OS? In that case, I'd go for my own file system format.


JAL
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: how to make FAT table on disk up to date? while running

Post by Combuster »

The idea of a doubled FAT is to have some redundancy.

IIRC In real life OSes do not normally check the second FAT (and therefore do not notice its off)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: how to make FAT table on disk up to date? while running

Post by pcmattman »

One of the advantages to a second FAT comes if the first FAT becomes corrupted (somehow) or the disk sectors for the first FAT become unreadable. Recovery utilities could (and this is only speculation) then use the second FAT to create a replacement.

Also, you (should) use the BPB_NumFATs variable rather than assuming there's two (or less) FATs, if you do decide to write to all the FATs ;)
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: how to make FAT table on disk up to date? while running

Post by jal »

pcmattman wrote:One of the advantages to a second FAT comes if the first FAT becomes corrupted (somehow) or the disk sectors for the first FAT become unreadable. Recovery utilities could (and this is only speculation) then use the second FAT to create a replacement.
The problem is, the second FAT could be corrupted instead, and reverting to it leads to disaster (it happened to me a long time ago). There's no checksum over the FAT tables, so software is only able to detect whether there's differences between the versions, not true corruption (well, obviously some corruption like double allocation etc.).
Also, you (should) use the BPB_NumFATs variable rather than assuming there's two (or less) FATs, if you do decide to write to all the FATs ;)
Iirc, Microsoft in its FAT document says that only a value of 2 is allowed, ever. I'm too lazy to check however.


JAL
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: how to make FAT table on disk up to date? while running

Post by JAAman »

jal wrote: Iirc, Microsoft in its FAT document says that only a value of 2 is allowed, ever. I'm too lazy to check however.
JAL
ya, the new version of the FAT docs changed that... (the original specification made no such requirement)

actually, any number of FAT tables can be used, however the new revision require only 2 to be a valid value, for compatibility with improperly written software (back in the old days, it was quite common for programs to write their own FAT drivers rather than use DOSs, and they often assumed things they shouldnt have)

the specification also mentions that all MS OSs can properly handle disks with any number of FAT tables

for maximum compatibility, you should not expect it to be 2 for existing disks, but should only create disks with exactly 2
User avatar
abachler
Member
Member
Posts: 33
Joined: Thu Jan 15, 2009 2:21 pm

Re: how to make FAT table on disk up to date? while running

Post by abachler »

blackoil wrote:I copy the FAT to ram when startup, if read/write file, the FAT updates in ram.
It has to write it back to disk.
For this you want a write through buffer. Reads are fetched from the buffer, while writes update the buffer and the disk, in this way the disk is always in sync. There is little to be gained from delaying the write to disk and much to lose. When writing kernel level disk I/O routines, keep in mind that the power could fail at any millisecond. This is why journaling file systems are so nifty.
Post Reply