[comp.sys.ibm.pc] DOS FAT table updates

wei@hpctdls.HP.COM (Bill Ives) (05/26/90)

> / croley@walt.cc.utexas.edu (David T. Croley) / 10:58 am  May 23, 1990 /
>I am curious.  When DOS is saving a file, does it write the file
>to disk and then update the FAT and write it to disk, or is the order
>reversed.

   When saving a file, if the file has not changed size, there is
   no need for DOS to update the FAT. Updating the file data area 
   is sufficient.  In the case of growing a file ( or changing its
   number of allocated clusters in the FAT ) I do not know for sure,
   although I would guess it doesn't matter which one is updated first
   since in the event of power failure the FAT could be corrupted
   in either case.  My best guess is that DOS determines which cluster
   is free for adding to the file, writes its data to it, then updates
   the FAT.  In this case if power fails as the data is written,
   the FAT is still okay for the original file size ( although the
   extra data in the new cluster is not reliable ).  Then DOS must
   update the FAT which may involve 1 or 2 sector updates (in our
   case of adding only one cluster). It may mark the free cluster
   as the the EOF (0xFFFF in 16 bit FAT), write it to its sector, then
   update the old EOF to point to the new EOF cluster number. If
   power failed between these operations you would have the famous
   "lost cluster" condition often flaged by CHKDSK.  I believe it
   may still be possible to corrupt the disk FAT if power fails at
   the most in-oportune time -- reconstruction utilities come in handy
   then...
 
>Also, does anyone know where I can get info on the routines DOS uses
>to allocate clusters to a file.  I would like to replace these with my
>own routines if possible.
   
   My guess is that these are MS DOS secrets :-) .  I have written my
   own utilities to handle the FAT and ROOT DIR for an in-house project
   and its not too hard to figure it out.  DOS appears to just allocate
   the next available cluster whether never used before or part of
   a removed file cluster chain.

>I have looked in every DOS reference that I can find, and none are
>detailed enough to have this type of information.

   I found the same thing.  You can figure out most of it by poking
   around the disk sector by sector.  An okay starting point book is
   "Advanced MS-DOS" by Ray Duncan -- I like his articles and his book
   seems to follow the same easy mannered tone IMHO .....
   A hint to get you started -- the cluster numbers ( whether in
   12 or 16 bit FATs) have to be adjusted down by two to determine
   the logical sector number used by DOS absolute disk I/O interrupts
   25h and 26h.  This is due to the first two clusters being dummied
   out to hold the FAT ID byte.  You probably already knew this, but hey
   just in case :-)  

   Good Luck

   Bill Ives
   HP CTD
   #include <std_disclaimer_here>

dixon@sagittarius.crd.ge.com (walt dixon) (05/28/90)

In <30365@ut-emx.UUCP> David T. Croley asks:

>I am curious.  When DOS is saving a file, does it write the file
>to disk and then update the FAT and write it to disk, or is the order
>reversed.

In most cases the write comes first.  FAT access is cached.  DOS only
uses the disk cache for partial block reads and writes,  FAT access,
and directory access.  There are several conditions that cause DOS
to flush the cache.  Closing a file,  disk reset,  and running out of
cache blocks all cause the cache to be flushed.  Its been a while since
I looked through the caching code;  I may be missing some important
cases.  BTW the caching code is not particularly well written and
consequently is somewhat difficult to follow.

>Also, does anyone know where I can get info on the routines DOS uses
>to allocate clusters to a file.  I would like to replace these with my
>own routines if possible.

At least in IBM-DOS 3.10,  the search began after the last cluster in
the file and progressed until either the end of the disk had been reached
or the required number of clusters had been found.  There's no clean
way to replace the cluster allocation code.  You could try patching
the internal write routine or the cluster allocation routine.  The
strategy DOS uses actually is not too bad.

>I have looked in every DOS reference that I can find, and none are
>detailed enough to have this type of information.

I got my information with a disassembler.  The original draft of Chapter
10 of "The MS-DOS Papers" had a detailed explaination of how DOS
does disk i/o.  Unfortunately this material was edited out to save
space.

Walt Dixon		{internet:	dixon@crd.ge.com	}
			{us mail:	ge crd			}
			{		po box 8		}
			{		schenectady, ny 12301	}
			{voice:		518-387-5798		}
Walt Dixon dixon@crd.ge.com