# HG changeset patch # User Jonathan Pevarnek # Date 1314849513 14400 # Node ID b6b3f04a732ff7f0a84d25616faec4636b1d8f08 # Parent 550840bcc1403914c16c68dbfce9449aee13adc0 Cleaned up fs code init_fs actually uses the devnum passed to it rootLoc changed to RootLoc for consistency vint other variables Added comments on anything that was not instantly obvious to me TODO: multiple disks (oh, how I wish I had C++ classes for this... Oh well, I am now thinking of a fake OO interface where some structure is just required to be passed to every single function. I just do not know how well that would work with program-level stuff... I will think about it more...) diff -r 550840bcc140 -r b6b3f04a732f include/error.h --- a/include/error.h Wed Aug 31 16:11:06 2011 -0400 +++ b/include/error.h Wed Aug 31 23:58:33 2011 -0400 @@ -29,6 +29,7 @@ #define NOTINCACHE 8 #define DISKFULL 9 //every block on the disk is currently set to be in use #define EXISTS 10 //the user tried to create a file that already exists +#define SMALLCACHE 11 //the cache size is not large enough for the operation //Codes: Time of date #define UPTIME 1 //the actual time of date was not set in the system diff -r 550840bcc140 -r b6b3f04a732f include/os/fs.h --- a/include/os/fs.h Wed Aug 31 16:11:06 2011 -0400 +++ b/include/os/fs.h Wed Aug 31 23:58:33 2011 -0400 @@ -1,10 +1,10 @@ +#ifndef __FS_H +#define __FS_H + #include #include #include -#ifndef __FS_H -#define __FS_H - #define CACHESPACE 1 //percentage of heap to use for the FS Cache #define FSMAGICNUM 0x42420374 #define DEPBLK (FSBLKSIZE / sizeof(Direntry)) //director entries per block diff -r 550840bcc140 -r b6b3f04a732f src/os/fs.c --- a/src/os/fs.c Wed Aug 31 16:11:06 2011 -0400 +++ b/src/os/fs.c Wed Aug 31 23:58:33 2011 -0400 @@ -22,7 +22,7 @@ static FSBlk *FS_Cache = NULL; //A pointer to the actual cache static u32 *CacheLocs; //what location each block points to static u32 CacheNext; //the next location to toss a cached block into -static u32 rootLoc; +static u32 RootLoc; static u32 MaxBlocks; ErrCode init_fs(u32 devnum, u64 __memsize) @@ -37,20 +37,20 @@ CacheSize = 0; CacheNext = 0; - DevID = find_dev(0x100); + DevID = find_dev(devnum); Superblock *sb; - sb = readFSBlock(1); + sb = readFSBlock(1); //second file system block has the superblock if(!sb) return ERR_FSBLKREADFAIL; if(sb->magic != FSMAGICNUM) return mkError(MODFS, MUGGLE, ERROR); MaxBlocks = sb->nblocks; - rootLoc = sb->root_inode; + RootLoc = sb->root_inode; return 0; } ErrCode getFInfo(u32 n, void* de) { - Inode *root = readFSBlock(rootLoc); + Inode *root = readFSBlock(RootLoc); if(!root) return ERR_FSBLKREADFAIL; if(n >= root->size/DEPBLK) return mkError(MODFS, OUTOFRANGE, WARN); Direntry *dp = readFSBlock(root->blocks[n/DEPBLK]); @@ -82,12 +82,13 @@ ErrCode makeFile(char *fname, u32 *fid) { ErrCode err; + //lookupFile should return the notfile erro as the file should not yet exist if(!isError(err = lookupFile(fname, fid))) return mkError(MODFS, EXISTS, ERROR); //the file should not exist if(err != mkError(MODFS, NOTFILE, ERROR)) return err; if(isError(err = nextFreeBlock(0, fid))) return err; - Inode nin; + Inode nin; //new inode nin.size = 0; nin.nblocks = 0; if(isError(err = setTod(&nin))) return err; @@ -98,7 +99,7 @@ Direntry de; de.inode = *fid; memcpy(de.fname, fname, FNAMELEN); - if(isError(err = fileAppend(rootLoc, &de, sizeof(Direntry)))) return err; + if(isError(err = fileAppend(RootLoc, &de, sizeof(Direntry)))) return err; return 0; } @@ -106,7 +107,7 @@ ErrCode deleteFile(u32 fid) { ErrCode err; - if(isError(err = setFileSize(fid, 0))) return err; + if(isError(err = setFileSize(fid, 0))) return err; //get rid of all the data Direntry de; u32 fileNo; for(fileNo = 0; 1; fileNo++) { @@ -114,7 +115,7 @@ else if(err == mkError(MODFS, OUTOFRANGE, WARN)) return mkError(MODFS, NOTFILE, ERROR); if(de.inode == fid) break; } - Inode *root = readFSBlock(rootLoc); + Inode *root = readFSBlock(RootLoc); if(!root) return ERR_FSBLKREADFAIL; u32 rootSize = root->size; u32 numDE = rootSize/sizeof(Direntry); @@ -124,16 +125,19 @@ if(!td) return ERR_FSBLKREADFAIL; td += fileNo%DEPBLK; Direntry *end = readFSBlock(endBlkNo); + //XXX HACK consider fixing //this will fail if there is only space for one item in cache and the 2 DEs - //are in different blocks. There will be no indication of failure + //are in different blocks. The line below should detect this failure + if(end == td) return mkError(MODFS, SMALLCACHE, ERROR); if(!end) return ERR_FSBLKREADFAIL; end += numDE%DEPBLK - 1; *td = *end; - if(isError(err = setFileSize(rootLoc, rootSize - sizeof(Direntry)))) return err; + if(isError(err = setFileSize(RootLoc, rootSize - sizeof(Direntry)))) return err; if(isError(err = sync(toBlkNo))) return err; //end does not need to be synchronized return 0; } +//a simple wrapper for fileWrite that just appends the data to the file ErrCode fileAppend(u32 fid, void *data, u32 length) { ErrCode err; @@ -149,11 +153,13 @@ Inode *iptr = readFSBlock(fid); if(!iptr) return ERR_FSBLKREADFAIL; u32 oldSize = iptr->size; - u32 dataStop = offset + length; - if(dataStop > oldSize) { + u32 dataStop = offset + length; //where the new data will stop being added + if(dataStop > oldSize) { //if the file will need to be longer than it is, make it so if(isError(err = setFileSize(fid, dataStop))) return err; } + //I will be using the cache for the file contents so copy this out just in + //case it would be overwritten Inode in; if(isError(err = getBlockData(fid, &in))) return err; @@ -165,8 +171,8 @@ u32 tcOS = currPos%FSBLKSIZE; //the current offset for the block u32 tcSize = Min_u32(dataStop - currPos, FSBLKSIZE); //assume 0 offset tcSize = (tcSize + tcOS > FSBLKSIZE)?FSBLKSIZE - tcOS:tcSize; //take into account the offset - memcpy(ptr + tcOS, data, tcSize); - if(isError(err = sync(currBlock))) return err; + memcpy(ptr + tcOS, data, tcSize); //copy the data into the cache + if(isError(err = sync(currBlock))) return err; //update the disk data += tcSize; currPos += tcSize; } @@ -291,6 +297,7 @@ return 0; } +//copies the data from block blkno to the location specified by the user static ErrCode getBlockData(u32 blkno, void* ptr) { Inode *temp = readFSBlock(blkno); @@ -416,6 +423,8 @@ return ret; } + +//This function sets the modified time of the inode to be the current time //TODO consider what to do in other places if this returns a warning/error static ErrCode setTod(Inode *inPtr) {