# HG changeset patch # User Jonathan Pevarnek # Date 1303866486 14400 # Node ID 7f312eb75ec0401a78fff74ddaf764924c012020 # Parent a435ee2c668a4148edbfce07456f1b1677452f3f Changed around the order of the functions, made sure that everything was where it should be diff -r a435ee2c668a -r 7f312eb75ec0 include/error.h --- a/include/error.h Mon Apr 25 13:35:43 2011 -0400 +++ b/include/error.h Tue Apr 26 21:08:06 2011 -0400 @@ -4,7 +4,7 @@ #define mkError(mod,code,sev) (((mod) << 12) | ((code) << 2) | (sev)) #define isError(code) (((code) & 3) == ERROR) #define errCode(code) (((code) >> 2) & 1023) -#define makeWarn(code) ((((code) >> 2) << 2) | WARN) //convert an error into a warning +#define makeWarn(code) (isError(code)?((((code) >> 2) << 2) | WARN):code) //convert an error into a warning //Severities #define INFO 0 diff -r a435ee2c668a -r 7f312eb75ec0 include/fs.h --- a/include/fs.h Mon Apr 25 13:35:43 2011 -0400 +++ b/include/fs.h Tue Apr 26 21:08:06 2011 -0400 @@ -56,7 +56,6 @@ ErrCode getFileSize(u32 fid, u32 *size); ErrCode getFileData(u32 fid, void *ptr); ErrCode setFileData(u32 fid, const void *ptr, size_t size); -ErrCode isBlockAt(u32 block, short *b); void printFname(char *name); void getFname(char *fname); int fnameCmp(const char *a, const char *b); diff -r a435ee2c668a -r 7f312eb75ec0 src/fs.c --- a/src/fs.c Mon Apr 25 13:35:43 2011 -0400 +++ b/src/fs.c Tue Apr 26 21:08:06 2011 -0400 @@ -3,15 +3,16 @@ #include #include -static ErrCode setTod(Inode *inPtr); -static u32 getCacheLoc(u32 blkno); -static u32 cacheAdd(u32 blkno); -static ErrCode setFileSize(u32 fid, size_t size); -static ErrCode setBlockAt(u32 block, short isBlock); -static ErrCode getBlockData(u32 blkno, void* ptr); static void* readFSBlock(u32 blkno); static ErrCode writeFSBlock(u32 blkno, void *ptr); static ErrCode sync(u32 blkno); +static ErrCode getBlockData(u32 blkno, void* ptr); +static ErrCode setFileSize(u32 fid, size_t size); +static ErrCode isBlockAt(u32 block, short *b); +static ErrCode setBlockAt(u32 block, short isBlock); +static u32 getCacheLoc(u32 blkno); +static u32 cacheAdd(u32 blkno); +static ErrCode setTod(Inode *inPtr); static u32 DevID; static u32 CacheCap; //how many blocks the cache can hold @@ -150,6 +151,90 @@ return ret; } +void printFname(char *name) +{ + putline(name, 28); +} + +//PRECON: fname points to a location 28 characters long +//Sets fname to contain a filename entered by the user +void getFname(char *fname) +{ + int chars = getline(fname, FNAMELEN); + for(;chars < FNAMELEN; chars++) { + fname[chars] = ' '; + } +} + +//Checks whether two file names are equal +int fnameCmp(const char *a, const char *b) +{ + int i; + for(i = 0; i < FNAMELEN; i++, a++, b++) { + if(*a - *b) return *a - *b; + } + return 0; +} + +//XXX HELPER FUNCTIONS XXX + +//returns a pointer pointing to a location where the data from blkno will be +//stored +static void* readFSBlock(u32 blkno) +{ + u32 cacheLoc = getCacheLoc(blkno); //Do not reload if in cache + if(cacheLoc == CacheCap) { //It was not found in the cache + cacheLoc = cacheAdd(blkno); + FSBlk *nextFSB = FS_Cache + cacheLoc; + DskBlk *nextDSKB = (DskBlk *) nextFSB; //block to put data in + if(fba_read_blk(DevID, blkno*2, nextDSKB)) return NULL; + if(fba_read_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return NULL; + } + return (void *)(FS_Cache + cacheLoc); +} + +//Writes 1024 bytes from *ptr to data block blkno +static ErrCode writeFSBlock(u32 blkno, void *ptr) +{ + u32 cacheLoc = getCacheLoc(blkno); + ErrCode err; + if(cacheLoc == CacheCap) + cacheLoc = cacheAdd(blkno); //need room in cache for data + FSBlk *nextFSB = FS_Cache + cacheLoc; //TODO abstraction levels? + memcpy(nextFSB, ptr, FSBLKSIZE); //copy the data to the cache + DskBlk *nextDSKB = (DskBlk *) nextFSB; //where to get the data from + if(isError(err = fba_write_blk(DevID, blkno*2, nextDSKB))) return err; + //load from the cache to the disk + if(isError(err = fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1))) return err; + short ib; + if(isError(err = isBlockAt(blkno, &ib))) return err; + if(!ib) { + if(isError(err = setBlockAt(blkno, 1))) return makeWarn(err); + } + return 0; +} + +//updates the data in block blkno based upon the data in the cache. If block +//blkno is not in the cache, returns non-zero +static ErrCode sync(u32 blkno) +{ + u32 cacheLoc = getCacheLoc(blkno); + if(cacheLoc == CacheCap) return mkError(MODFS, NOTINCACHE, ERROR); + FSBlk *nextFSB = FS_Cache + cacheLoc; + DskBlk *nextDSKB = (DskBlk *) nextFSB; //where to get the data from + if(fba_write_blk(DevID, blkno*2, nextDSKB)) return mkError(MODFS, BLKWRITEFAIL, ERROR); + if(fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return mkError(MODFS, BLKWRITEFAIL, ERROR); + return 0; +} + +static ErrCode getBlockData(u32 blkno, void* ptr) +{ + Inode *temp = readFSBlock(blkno); + if(!temp) return ERR_FSBLKREADFAIL; + memcpy(ptr, temp, FSBLKSIZE); + return 0; +} + //sets the file to have a size equal to size. The data will be preserved if //the file is currently larger than that or will be jibberish if the file is //currently smaller @@ -206,7 +291,7 @@ return ret; } -ErrCode isBlockAt(u32 block, short *b) +static ErrCode isBlockAt(u32 block, short *b) { u32 inBlock = block/(8*FSBLKSIZE) + 2; //which block the bit is in u32 blockSpot = (block%(8*FSBLKSIZE))/8; //which u8 the bit is in @@ -218,7 +303,7 @@ } //sets the block table to indicate whether there is a block at block -ErrCode setBlockAt(u32 block, short isBlock) +static ErrCode setBlockAt(u32 block, short isBlock) { u32 inBlock = block/(8*FSBLKSIZE) + 2; //which block the bit is in u32 blockSpot = (block%(8*FSBLKSIZE))/8; //which u8 the bit is in @@ -232,42 +317,7 @@ return 0; } -void printFname(char *name) -{ - putline(name, 28); -} - -//PRECON: fname points to a location 28 characters long -//Sets fname to contain a filename entered by the user -void getFname(char *fname) -{ - int chars = getline(fname, FNAMELEN); - for(;chars < FNAMELEN; chars++) { - fname[chars] = ' '; - } -} - -//Checks whether two file names are equal -int fnameCmp(const char *a, const char *b) -{ - int i; - for(i = 0; i < FNAMELEN; i++, a++, b++) { - if(*a - *b) return *a - *b; - } - return 0; -} - -//TODO consider what to do in other places if this returns a warning -static ErrCode setTod(Inode *inPtr) -{ - u64 tod; - ErrCode ret; - ret = get_tod(&tod); - inPtr->mtime = tod >> 12; - return ret; -} - -//TODO ErrCode +//TODO ErrCode? static u32 getCacheLoc(u32 blkno) { int i; @@ -291,59 +341,12 @@ return ret; } -//returns a pointer pointing to a location where the data from blkno will be -//stored -static void* readFSBlock(u32 blkno) -{ - u32 cacheLoc = getCacheLoc(blkno); //Do not reload if in cache - if(cacheLoc == CacheCap) { //It was not found in the cache - cacheLoc = cacheAdd(blkno); - FSBlk *nextFSB = FS_Cache + cacheLoc; - DskBlk *nextDSKB = (DskBlk *) nextFSB; //block to put data in - if(fba_read_blk(DevID, blkno*2, nextDSKB)) return NULL; - if(fba_read_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return NULL; - } - return (void *)(FS_Cache + cacheLoc); -} - -static ErrCode getBlockData(u32 blkno, void* ptr) -{ - Inode *temp = readFSBlock(blkno); - if(!temp) return ERR_FSBLKREADFAIL; - memcpy(ptr, temp, FSBLKSIZE); - return 0; -} - -//Writes 1024 bytes from *ptr to data block blkno -static ErrCode writeFSBlock(u32 blkno, void *ptr) +//TODO consider what to do in other places if this returns a warning/error +static ErrCode setTod(Inode *inPtr) { - u32 cacheLoc = getCacheLoc(blkno); - ErrCode err; - if(cacheLoc == CacheCap) - cacheLoc = cacheAdd(blkno); //need room in cache for data - FSBlk *nextFSB = FS_Cache + cacheLoc; //TODO abstraction levels? - memcpy(nextFSB, ptr, FSBLKSIZE); //copy the data to the cache - DskBlk *nextDSKB = (DskBlk *) nextFSB; //where to get the data from - if(isError(err = fba_write_blk(DevID, blkno*2, nextDSKB))) return err; - //load from the cache to the disk - if(isError(err = fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1))) return err; - short ib; - if(isError(err = isBlockAt(blkno, &ib))) return err; - if(!ib) { - if(isError(err = setBlockAt(blkno, 1))) return makeWarn(err); - } - return 0; + u64 tod; + ErrCode ret; + ret = get_tod(&tod); + inPtr->mtime = tod >> 12; + return ret; } - -//updates the data in block blkno based upon the data in the cache. If block -//blkno is not in the cache, returns non-zero -static ErrCode sync(u32 blkno) -{ - u32 cacheLoc = getCacheLoc(blkno); - if(cacheLoc == CacheCap) return mkError(MODFS, NOTINCACHE, ERROR); - FSBlk *nextFSB = FS_Cache + cacheLoc; - DskBlk *nextDSKB = (DskBlk *) nextFSB; //where to get the data from - if(fba_write_blk(DevID, blkno*2, nextDSKB)) return mkError(MODFS, BLKWRITEFAIL, ERROR); - if(fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return mkError(MODFS, BLKWRITEFAIL, ERROR); - return 0; -}