changeset 70:3b73044b740f

I think I have just about all of the FS code using errcodes There may be some of the FS code that messes up with error handling, I will examine more after my exam. I probably should have been studying for that just now...
author Jonathan Pevarnek <pevarnj@gmail.com>
date Mon, 25 Apr 2011 10:15:56 -0400
parents 19000e354e36
children a435ee2c668a
files include/error.h include/fs.h include/tod.h src/fs.c src/testFS.c
diffstat 5 files changed, 115 insertions(+), 113 deletions(-) [+]
line wrap: on
line diff
--- a/include/error.h	Sun Apr 24 18:28:12 2011 -0400
+++ b/include/error.h	Mon Apr 25 10:15:56 2011 -0400
@@ -2,9 +2,9 @@
 #define __ERROR_H
 
 #define mkError(mod,code,sev) (((mod) << 12) | ((code) << 2) | (sev))
-#define notError(mod) (((mod) << 12) | (NOERROR << 2) | INFO)
 #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
 
 //Severities
 #define INFO 0
@@ -13,14 +13,24 @@
 
 //Modules
 #define MODFS 1 //stuff in the file system
+#define MODTOD 2 //Time of date
 
-//Codes
-#define NOERROR 0 //there was no issue
+#define NOERROR 0 //General error code for not an error
+
+//Codes: filesystem
 #define ALLOCFAIL 1 //a general code for a malloc failing
-#define NOMAGIC 2 //The magic code in the superblock was not set (filesystem)
-#define BLKREADFAIL 3 //failure for reading a filesystem block
-#define OUTOFRANGE 4 //
+#define OUTOFRANGE 2 //
+#define MUGGLE 3 //The magic code in the superblock was not set correctly
+#define BLKREADFAIL 4 //failure for reading a filesystem block
 #define NOTFILE 5
+#define BLKWRITEFAIL 6 //failure to write a filesystem block
+#define SIZETOOLARGE 7 //the user requested a size that is too large to be set for a file
+#define NOTINCACHE 8
+
+//Codes: Time of date
+#define UPTIME 1 //the actual time of date was not set in the system
+#define INVALID 2
+
 
 typedef u32 ErrCode;
 
--- a/include/fs.h	Sun Apr 24 18:28:12 2011 -0400
+++ b/include/fs.h	Mon Apr 25 10:15:56 2011 -0400
@@ -9,8 +9,11 @@
 #define FSBLKSIZE 1024
 #define DSKBLKSIZE 512
 #define MAXBLOCKS 248
+#define FSMAGICNUM 0x42420374
 #define DEPBLK (FSBLKSIZE / sizeof(Direntry)) //director entries per block
 
+#define ERR_FSBLKREADFAIL mkError(MODFS, BLKREADFAIL, ERROR)
+
 struct SUPERBLOCK {
 	u32 magic;              // == 0x42420374
 	u32 root_inode;         // the disk block containing the root inode
@@ -51,11 +54,11 @@
 ErrCode getFInfo(u32 n, void* de);
 ErrCode lookupFile(char *fname, u32 *fid);
 ErrCode getFileSize(u32 fid, u32 *size);
-int getFileData(u32 fid, void *ptr);
-int setFileData(u32 fid, const void *ptr, size_t size);
+ErrCode getFileData(u32 fid, void *ptr);
+ErrCode setFileData(u32 fid, const void *ptr, size_t size);
 short isBlockAt(u32 block);
 void printFname(char *name);
 void getFname(char *fname);
 int fnameCmp(const char *a, const char *b);
 
-#endif //__FSH_H
+#endif //__FS_H
--- a/include/tod.h	Sun Apr 24 18:28:12 2011 -0400
+++ b/include/tod.h	Mon Apr 25 10:15:56 2011 -0400
@@ -56,9 +56,10 @@
  *                 *tod == 0 is the instant the system powered on
  *   2 or 3 on failure, value of *tod is unpredictable
  */
-static inline int get_tod(u64 *tod)
+static inline ErrCode get_tod(u64 *tod)
 {
 	int cc;
+	ErrCode ret = 0;
 
 	asm volatile(
 		"stck	%0\n"
@@ -71,8 +72,20 @@
 	: /* clobber */
 	  "cc"
 	);
+	switch(cc) {
+		case 0:
+			ret = 0;
+			break;
+		case 1:
+			ret = mkError(MODTOD, UPTIME, WARN);
+			break;
+		case 2:
+		case 3:
+			ret = mkError(MODTOD, INVALID, ERROR);
+			break;
+	}
 
-	return cc;
+	return ret;
 }
 
 #endif
--- a/src/fs.c	Sun Apr 24 18:28:12 2011 -0400
+++ b/src/fs.c	Mon Apr 25 10:15:56 2011 -0400
@@ -3,15 +3,15 @@
 #include <tod.h>
 #include <error.h>
 
-static u32 setTod(Inode *inPtr);
+static ErrCode setTod(Inode *inPtr);
 static u32 getCacheLoc(u32 blkno);
 static u32 cacheAdd(u32 blkno);
-static int setFileSize(u32 fid, size_t size);
-static int setBlockAt(u32 block, short isBlock);
+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 u32 getBlockData(u32 blkno, void* ptr);
-static int writeFSBlock(u32 blkno, void *ptr);
-static int sync(u32 blkno);
+static ErrCode writeFSBlock(u32 blkno, void *ptr);
+static ErrCode sync(u32 blkno);
 
 static u32 DevID;
 static u32 CacheCap; //how many blocks the cache can hold
@@ -36,22 +36,22 @@
 	DevID = find_dev(0x100);
 	Superblock *sb;
 	sb = readFSBlock(1);
-	if(!sb) return -1;
-	if(sb->magic != 0x42420374) return mkError(MODFS, NOMAGIC, ERROR);
+	if(!sb) return ERR_FSBLKREADFAIL;
+	if(sb->magic != 0x42420374) return mkError(MODFS, MUGGLE, ERROR);
 	rootLoc = sb->root_inode;
 
-	return notError(MODFS);
+	return 0;
 }
 
 ErrCode getFInfo(u32 n, void* de)
 {
 	Inode *root = readFSBlock(rootLoc);
-	if(!root) return mkError(MODFS, BLKREADFAIL, ERROR);
+	if(!root) return ERR_FSBLKREADFAIL;
 	if(n >= root->size/DEPBLK) return mkError(MODFS, OUTOFRANGE, WARN);
 	Direntry *dp = readFSBlock(root->blocks[n/DEPBLK]);
-	if(!dp) return mkError(MODFS, BLKREADFAIL, ERROR);
+	if(!dp) return ERR_FSBLKREADFAIL;
 	memcpy(de, dp + n%DEPBLK, sizeof(Direntry));
-	return notError(MODFS);
+	return 0;
 }
 
 //This sets *fid to be equal to the fileid of the file with name fname
@@ -62,10 +62,10 @@
 		Direntry de;
 		ErrCode err = getFInfo(i, &de);
 		if(err == mkError(MODFS, OUTOFRANGE, WARN)) break; //have iterated through all files
-		else if(isError(err)) return err; //blah
+		else if(isError(err)) return err; //forward error
 		if(!fnameCmp(fname, de.fname)) {
 			*fid = de.inode;
-			return notError(MODFS);
+			return 0;
 		}
 	}
 	return mkError(MODFS, NOTFILE, ERROR);
@@ -76,20 +76,20 @@
 ErrCode getFileSize(u32 fid, u32 *size)
 {
 	Inode *ptr = readFSBlock(fid);
-	if(!ptr) return mkError(MODFS, BLKREADFAIL, ERROR);
+	if(!ptr) return ERR_FSBLKREADFAIL;
 	*size = ptr->size;
-	return notError(MODFS);
+	return 0;
 }
 
 //PRECON:  ptr points to a large enough location to hold all the data in file
 //         fid
-int getFileData(u32 fid, void *ptr)
+ErrCode getFileData(u32 fid, void *ptr)
 {
-	int ret = 0;
+	ErrCode ret = 0, err;
 	int i;
 	Inode in;
-	if(getBlockData(fid, &in)) {
-		ret = -1;
+	if(isError(err = getBlockData(fid, &in))) {
+		ret = err;
 		goto end;
 	}
 
@@ -97,7 +97,7 @@
 	for(i = 0; i < in.nblocks; i++) {
 		Inode *filePtr = readFSBlock(in.blocks[i]);
 		if(!filePtr) {
-			ret = -1;
+			ret = ERR_FSBLKREADFAIL;
 			goto end;
 		}
 		memcpy(ptr, filePtr, Min_u32(size, FSBLKSIZE));
@@ -109,38 +109,40 @@
 	return ret;
 }
 
-int setFileData(u32 fid, const void *ptr, size_t size)
+ErrCode setFileData(u32 fid, const void *ptr, size_t size)
 {
-	int ret = 0;
+	ErrCode ret = 0, err;
 	unsigned int i;
-	if(setFileSize(fid, size)) { //TODO efficiency (talk to Jeff)
-		ret = -1;
+	if(isError(err = setFileSize(fid, size))) { //TODO efficiency (talk to Jeff)
+		ret = err;
 		goto end; 
 	}
 	Inode in;
-	if(getBlockData(fid, &in)) {
-		ret = -1;
+	if(isError(err = getBlockData(fid, &in))) {
+		ret = err;
 		goto end;
 	}
 
 	for(i = 0; i < in.nblocks; i++) {
 		Inode *filePtr = readFSBlock(in.blocks[i]);
 		if(!filePtr) {
-			ret = -1;
+			ret = ERR_FSBLKREADFAIL;
 			goto end;
 		}
 		memcpy(filePtr, ptr, Min_u32(size, FSBLKSIZE));
-		sync(in.blocks[i]);
+		if(isError(err = sync(in.blocks[i]))) {
+			ret = err;
+			goto end;
+		}
 		ptr += FSBLKSIZE;
 		size -= FSBLKSIZE;
 	}
 
-	if(setTod(&in)) { //sadly, this needs to be updated...
-		ret = -1;
-		goto end;
+	if(isError(err = setTod(&in))) { //sadly, this needs to be updated...
+		ret = makeWarn(err); //everything else should still be good
 	}
-	if(writeFSBlock(fid, &in)) {
-		ret = -1;
+	if(isError(err = writeFSBlock(fid, &in))) {
+		ret = err;
 		goto end;
 	}
 
@@ -151,25 +153,25 @@
 //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
-static int setFileSize(u32 fid, size_t size)
+static ErrCode setFileSize(u32 fid, size_t size) //TODO clean
 {
-	int ret = 0;
+	ErrCode ret = 0, err;
 	unsigned int i, j;
 	u16 neededBlocks = (size + FSBLKSIZE - 1)/FSBLKSIZE;
 	if(neededBlocks > MAXBLOCKS) {
-		ret = -1;
+		ret = mkError(MODFS, SIZETOOLARGE, ERROR);
 		goto end;
 	}
 	Inode in;
-	if(getBlockData(fid, &in)) {
-		ret = -1;
+	if(isError(err = getBlockData(fid, &in))) {
+		ret = err;
 		goto end;
 	}
 
 	if(in.nblocks >= neededBlocks) { //get rid of some blocks
 		for(i = neededBlocks; i < in.nblocks; i++) { //if it is equal, will skip this loop
-			if(setBlockAt(in.blocks[i], 0)) {
-				ret = -1;
+			if(isError(err = setBlockAt(in.blocks[i], 0))) {
+				ret = err;
 				goto end;
 			}
 		}
@@ -178,20 +180,19 @@
 			in.blocks[i] = 0;
 			for(j = in.blocks[i - 1]; in.blocks[i] == 0; j++)
 				if(!isBlockAt(j)) in.blocks[i] = j;
-			if(setBlockAt(in.blocks[i], 1)) {
-				ret = -1;
+			if(isError(err = setBlockAt(in.blocks[i], 1))) {
+				ret = err;
 				goto end;
 			}
 		}
 	}
 	in.nblocks = neededBlocks;
 	in.size = size;
-	if(setTod(&in)) {
-		ret = -1;
-		goto end;
+	if(isError(err = setTod(&in))) {
+		ret = makeWarn(err);
 	}
-	if(writeFSBlock(fid, &in)) {
-		ret = -1;
+	if(isError(err = writeFSBlock(fid, &in))) {
+		ret = err;
 		goto end;
 	}
 
@@ -200,7 +201,7 @@
 }
 
 short isBlockAt(u32 block)
-{ //TODO reconsider return value
+{ //TODO ErrCode
 	u32 inBlock = block/(8*FSBLKSIZE) + 2; //which block the bit is in
 	u32 blockSpot = (block%(8*FSBLKSIZE))/8; //which u8 the bit is in
 	u32 spotSpot = 7 - block%8; //which bit the bit is
@@ -210,16 +211,17 @@
 }
 
 //sets the block table to indicate whether there is a block at block
-int setBlockAt(u32 block, short isBlock)
+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
 	u32 spotSpot = 7 - block%8; //which bit the bit is
 	u8 *ptr = readFSBlock(inBlock);
-	if(!ptr) return -1;
+	if(!ptr) return ERR_FSBLKREADFAIL;
 	if(isBlock) ptr[blockSpot] |= 1 << spotSpot;
 	else ptr[blockSpot] &= ~(1 << spotSpot);
-	sync(inBlock);
+	ErrCode err;
+	if(isError(err = sync(inBlock))) return err;
 	return 0;
 }
 
@@ -248,14 +250,17 @@
 	return 0;
 }
 
-static u32 setTod(Inode *inPtr)
+//TODO consider what to do in other places if this returns a warning
+static ErrCode setTod(Inode *inPtr)
 {
 	u64 tod;
-	if(get_tod(&tod)) return -1;
+	ErrCode ret;
+	ret = get_tod(&tod);
 	inPtr->mtime = tod >> 12;
-	return 0;
+	return ret;
 }
 
+//TODO ErrCode
 static u32 getCacheLoc(u32 blkno)
 {
 	int i;
@@ -266,6 +271,7 @@
 }
 
 //PRECON:   block blkno is not already in the cache
+//Returns the locaion in the cache where the data is
 //sets aside space in the cache for some item.  NOTE: this does not read the
 //current contents of the disk to the cache
 static u32 cacheAdd(u32 blkno)
@@ -293,40 +299,42 @@
 	return (void *)(FS_Cache + cacheLoc);
 }
 
-static u32 getBlockData(u32 blkno, void* ptr)
+static ErrCode getBlockData(u32 blkno, void* ptr)
 {
 	Inode *temp = readFSBlock(blkno);
-	if(!temp) return -1;
+	if(!temp) return ERR_FSBLKREADFAIL;
 	memcpy(ptr, temp, FSBLKSIZE);
 	return 0;
 }
 
 //Writes 1024 bytes from *ptr to data block blkno
-static int writeFSBlock(u32 blkno, void *ptr)
+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;
+	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(fba_write_blk(DevID, blkno*2, nextDSKB)) return -1; //load from the cache to the disk
-	
-	if(fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return -1;
-	if(!isBlockAt(blkno))
-		if(setBlockAt(blkno, 1)) return -1;
+	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;
+	if(!isBlockAt(blkno)) {
+		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 int sync(u32 blkno)
+static ErrCode sync(u32 blkno)
 {
 	u32 cacheLoc = getCacheLoc(blkno);
-	if(cacheLoc == CacheCap) return -1;
+	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 -1;
-	if(fba_write_blk(DevID, blkno*2 + 1, nextDSKB + 1)) return -1;
+	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;
 }
--- a/src/testFS.c	Sun Apr 24 18:28:12 2011 -0400
+++ b/src/testFS.c	Mon Apr 25 10:15:56 2011 -0400
@@ -21,7 +21,7 @@
 		else if(isError(err)) return err;
 		printFname(de.fname), sPrint("\n");
 	}
-	return notError(0);
+	return 0;
 }
 
 void start(u64 __memsize)
@@ -45,7 +45,7 @@
 		if(isError(getFileSize(fid, &fileSize))) continue;
 		char *text = malloc(fileSize);
 		if(!text) continue;
-		if(getFileData(fid, text)) {
+		if(isError(getFileData(fid, text))) {
 			free(text);
 			continue;
 		}
@@ -53,38 +53,6 @@
 		free(text);
 	}
 
-	/*
-	while(1) {
-		//Prints off the name of each file
-		listFiles();
-		char fname[28];
-		sPrint("Please enter the file to read: ");
-		getFname(fname);
-		u32 fid = lookupFile(fname);
-		if(!fid) continue; //if fid is 0, the file was not found
-		sPrint("Please enter the file to copy to: ");
-		getFname(fname);
-		u32 fid2 = lookupFile(fname);
-		if(!fid2) continue;
-		u32 fileSize;
-		if(getFileSize(fid, &fileSize)) continue;
-		char *text = malloc(fileSize);
-		if(!text) continue;
-		if(getFileData(fid, text)) {
-			free(text);
-			continue;
-		}
-		setFileData(fid2, text, fileSize);
-		free(text);
-		
-		if(getFileSize(fid2, &fileSize)) continue;
-		text = malloc(fileSize);
-		if(getFileData(fid2, text)) continue;
-		dumpText(text, fileSize);
-		free(text);
-	}
-	*/
-
 END:
 	sPrint("DONE\n");
 	for(;;) {