Re:Universal File System
Posted: Mon Jan 16, 2006 11:33 am
[font=Lucida Console]
#define ENTRYTYPE_UNUSED 0x00
#define ENTRYTYPE_DIRECTORY 0x01
#define ENTRYTYPE_FILE 0x10
#define ENTRYTYPE_UNUSABLE 0x80
#define ENTRYTYPE_DELETEDDIR 0x81
#define ENTRYTYPE_DELETEDFILE 0x90
struct tMagic{
unsigned char m0;
unsigned short m1;
};
struct tSuperBlock{
unsigned char bootCode0[11];
unsigned char legacyBIOS[21];
unsigned char bootCode1[372];
__int64 timeStamp;
__int64 dataCount;
__int64 indexCount;
tMagic magic;
unsigned char version;
__int64 blockTotal;
unsigned int blockReserved;
unsigned char blockSize;
unsigned char checksum;
unsigned char compParitionTable[64];
unsigned char compBootSignature[2];
};
struct tIndexVolumeIndentiferEntry{
unsigned char type;
struct{
unsigned short reserved0;
unsigned char reserved1;
}reserved;
tTimeDate timeDate;
unsigned char name[52];
};
struct tIndexStandardEntry{
unsigned char type;
unsigned char flags;
unsigned short continuationCount;
__int64 timeDate;
__int64 blockStart;
__int64 blockEnd;
__int64 fileLength;
unsigned char name[20];
};
struct tIndexContinuationEntry{
unsigned char name[64];
};
struct tIndexStartingMarkerEntry{
unsigned char type;
unsigned char reserved[63];
};
unsigned int mmAllocPage(void); // allocates page
void mmAllocFreePage(unsigned int page); // free page
unsigned int fsReadBlock(__int64 blockIndex, unsigned int address); // read fs block
unsigned int fsWriteBlock(__int64 blockIndex, unsigned int address);// write fs block
unsigned int fsDefragment(tSuperBlock * superBlock){
tIndexStandardEntry * stdEntry;
__int64 dataBlockIndex = superBlock->blockReserved;
__int64 dataBlockClosest = 0;
__int64 dataBlockMove;
__int64 found;
unsigned int memData = mmAllocPage();
unsigned int tmpData = mmAllocPage();
__int64 bi;
for(;;){
found = superBlock->blockTotal; /* We use this to compute a between range of last defragment block, and the largest block. */
/* cycle through all blocks in the FS using the index area. */
for(bi = superBlock->blockTotal - superBlock->indexCount; bi < superBlock->blockTotal; bi++){
if(!fsReadBlock(bi, memData))
return 0; /* error reading block */
stdEntry = (tIndexStandardEntry*)memData;
/* Remove deleted things. This can corrupt the FS, after defrag. */
if((stdEntry->type == ENTRYTYPE_DELETEDDIR) | (stdEntry->type == ENTRYTYPE_DELETEDFILE)){
stdEntry->type = ENTRYTYPE_UNUSED;
if(!fsWriteBlock(bi, memData))
return 0;
}
/* make sure the entry is a directory or file */
if((stdEntry->type == ENTRYTYPE_DIRECTORY)|(stdEntry->type = ENTRYTYPE_FILE))
/* if we have previously found one, make sure this one is closer to dataBlockClosest than
our previous find. We assume that the index entrys are in ANY order!
*/
if((stdEntry->blockStart >= dataBlockClosest) & (stdEntry->blockStart < found))
found = stdEntry->blockStart;
/*
Jump past any continuation blocks.
*/
bi += stdEntry->continuationCount;
}
/*
This means that dataBlockClosest was so big. It exceeded any known file in the system. This means we
have defragmented every file possible.
*/
if(found == superBlock->blockTotal)
break; /* we are done. */
/*
Add one to jump out of the actual data block for a file, and into the next avaible block.
The next availible block could be used. It might not. Hopefully it points to some slacks pace
so the next cycle of for(BI.. will find it.
*/
dataBlockClosest = stdEntry->blockEnd + 1;
/* --------------- defragment found entry -------------------- */
/* How many blocks down should we shift? */
dataBlockMove = stdEntry->blockStart - dataBlockClosest;
/* None. The file's data was already contingious with the last. */
if(dataBlockMove > 0){
/* Lets move the data using dataBlockMove as the shift down count. */
for(__int64 mi = 0; mi < stdEntry->blockEnd - stdEntry->blockStart; mi++){
/* read it */
if(!fsReadBlock(bi + mi, tmpData))
return 0;
/* write it - shifted down #datablockMoves# number of times. */
if(!fsWriteBlock(bi + mi - dataBlockMove, tmpData))
return 0;
}
/* update the index block on what we did. */
dataBlockClosest -= dataBlockMove; /* dont forget we changed the disk structor */
stdEntry->blockStart -= dataBlockMove;
stdEntry->blockEnd -= dataBlockMove;
if(!fsWriteBlock(bi, memData)) /* update stdEntry */
return 0;
}
/* try another */
}
return 1; /* we are all done. */
}
[/font]
#define ENTRYTYPE_UNUSED 0x00
#define ENTRYTYPE_DIRECTORY 0x01
#define ENTRYTYPE_FILE 0x10
#define ENTRYTYPE_UNUSABLE 0x80
#define ENTRYTYPE_DELETEDDIR 0x81
#define ENTRYTYPE_DELETEDFILE 0x90
struct tMagic{
unsigned char m0;
unsigned short m1;
};
struct tSuperBlock{
unsigned char bootCode0[11];
unsigned char legacyBIOS[21];
unsigned char bootCode1[372];
__int64 timeStamp;
__int64 dataCount;
__int64 indexCount;
tMagic magic;
unsigned char version;
__int64 blockTotal;
unsigned int blockReserved;
unsigned char blockSize;
unsigned char checksum;
unsigned char compParitionTable[64];
unsigned char compBootSignature[2];
};
struct tIndexVolumeIndentiferEntry{
unsigned char type;
struct{
unsigned short reserved0;
unsigned char reserved1;
}reserved;
tTimeDate timeDate;
unsigned char name[52];
};
struct tIndexStandardEntry{
unsigned char type;
unsigned char flags;
unsigned short continuationCount;
__int64 timeDate;
__int64 blockStart;
__int64 blockEnd;
__int64 fileLength;
unsigned char name[20];
};
struct tIndexContinuationEntry{
unsigned char name[64];
};
struct tIndexStartingMarkerEntry{
unsigned char type;
unsigned char reserved[63];
};
unsigned int mmAllocPage(void); // allocates page
void mmAllocFreePage(unsigned int page); // free page
unsigned int fsReadBlock(__int64 blockIndex, unsigned int address); // read fs block
unsigned int fsWriteBlock(__int64 blockIndex, unsigned int address);// write fs block
unsigned int fsDefragment(tSuperBlock * superBlock){
tIndexStandardEntry * stdEntry;
__int64 dataBlockIndex = superBlock->blockReserved;
__int64 dataBlockClosest = 0;
__int64 dataBlockMove;
__int64 found;
unsigned int memData = mmAllocPage();
unsigned int tmpData = mmAllocPage();
__int64 bi;
for(;;){
found = superBlock->blockTotal; /* We use this to compute a between range of last defragment block, and the largest block. */
/* cycle through all blocks in the FS using the index area. */
for(bi = superBlock->blockTotal - superBlock->indexCount; bi < superBlock->blockTotal; bi++){
if(!fsReadBlock(bi, memData))
return 0; /* error reading block */
stdEntry = (tIndexStandardEntry*)memData;
/* Remove deleted things. This can corrupt the FS, after defrag. */
if((stdEntry->type == ENTRYTYPE_DELETEDDIR) | (stdEntry->type == ENTRYTYPE_DELETEDFILE)){
stdEntry->type = ENTRYTYPE_UNUSED;
if(!fsWriteBlock(bi, memData))
return 0;
}
/* make sure the entry is a directory or file */
if((stdEntry->type == ENTRYTYPE_DIRECTORY)|(stdEntry->type = ENTRYTYPE_FILE))
/* if we have previously found one, make sure this one is closer to dataBlockClosest than
our previous find. We assume that the index entrys are in ANY order!
*/
if((stdEntry->blockStart >= dataBlockClosest) & (stdEntry->blockStart < found))
found = stdEntry->blockStart;
/*
Jump past any continuation blocks.
*/
bi += stdEntry->continuationCount;
}
/*
This means that dataBlockClosest was so big. It exceeded any known file in the system. This means we
have defragmented every file possible.
*/
if(found == superBlock->blockTotal)
break; /* we are done. */
/*
Add one to jump out of the actual data block for a file, and into the next avaible block.
The next availible block could be used. It might not. Hopefully it points to some slacks pace
so the next cycle of for(BI.. will find it.
*/
dataBlockClosest = stdEntry->blockEnd + 1;
/* --------------- defragment found entry -------------------- */
/* How many blocks down should we shift? */
dataBlockMove = stdEntry->blockStart - dataBlockClosest;
/* None. The file's data was already contingious with the last. */
if(dataBlockMove > 0){
/* Lets move the data using dataBlockMove as the shift down count. */
for(__int64 mi = 0; mi < stdEntry->blockEnd - stdEntry->blockStart; mi++){
/* read it */
if(!fsReadBlock(bi + mi, tmpData))
return 0;
/* write it - shifted down #datablockMoves# number of times. */
if(!fsWriteBlock(bi + mi - dataBlockMove, tmpData))
return 0;
}
/* update the index block on what we did. */
dataBlockClosest -= dataBlockMove; /* dont forget we changed the disk structor */
stdEntry->blockStart -= dataBlockMove;
stdEntry->blockEnd -= dataBlockMove;
if(!fsWriteBlock(bi, memData)) /* update stdEntry */
return 0;
}
/* try another */
}
return 1; /* we are all done. */
}
[/font]