changeset 73:4c47b80ad9ac

Added a function, nextFreeBlock that determines the next free block in the filesystem after a given block
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 26 Apr 2011 21:32:20 -0400
parents 7f312eb75ec0
children 36e6fc4a0487
files include/error.h src/fs.c
diffstat 2 files changed, 23 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/include/error.h	Tue Apr 26 21:08:06 2011 -0400
+++ b/include/error.h	Tue Apr 26 21:32:20 2011 -0400
@@ -26,6 +26,7 @@
 #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
+#define DISKFULL 9 //every block on the disk is currently set to be in use
 
 //Codes: Time of date
 #define UPTIME 1 //the actual time of date was not set in the system
--- a/src/fs.c	Tue Apr 26 21:08:06 2011 -0400
+++ b/src/fs.c	Tue Apr 26 21:32:20 2011 -0400
@@ -10,6 +10,7 @@
 static ErrCode setFileSize(u32 fid, size_t size);
 static ErrCode isBlockAt(u32 block, short *b);
 static ErrCode setBlockAt(u32 block, short isBlock);
+static ErrCode nextFreeBlock(u32 start, u32 *loc);
 static u32 getCacheLoc(u32 blkno);
 static u32 cacheAdd(u32 blkno);
 static ErrCode setTod(Inode *inPtr);
@@ -21,6 +22,7 @@
 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 MaxBlocks;
 
 ErrCode init_fs(u32 devnum, u64 __memsize)
 {
@@ -39,6 +41,7 @@
 	sb = readFSBlock(1);
 	if(!sb) return ERR_FSBLKREADFAIL;
 	if(sb->magic != FSMAGICNUM) return mkError(MODFS, MUGGLE, ERROR);
+	MaxBlocks = sb->nblocks;
 	rootLoc = sb->root_inode;
 
 	return 0;
@@ -241,7 +244,7 @@
 static ErrCode setFileSize(u32 fid, size_t size) //TODO clean
 {
 	ErrCode ret = 0, err;
-	unsigned int i, j;
+	unsigned int i;
 	u16 neededBlocks = (size + FSBLKSIZE - 1)/FSBLKSIZE;
 	if(neededBlocks > MAXBLOCKS) {
 		ret = mkError(MODFS, SIZETOOLARGE, ERROR);
@@ -262,14 +265,9 @@
 		}
 	} else { //there are fewer blocks
 		for(i = in.nblocks; i < neededBlocks; i++) {
-			in.blocks[i] = 0;
-			for(j = in.blocks[i - 1]; in.blocks[i] == 0; j++) {
-				short ib;
-				if(isError(err = isBlockAt(j, &ib))) {
-					ret = err;
-					goto end;
-				}
-				if(!ib) in.blocks[i] = j;
+			if(isError(err = nextFreeBlock(in.blocks[i - 1], &in.blocks[i]))) {
+				ret = err;
+				goto end;
 			}
 			if(isError(err = setBlockAt(in.blocks[i], 1))) {
 				ret = err;
@@ -317,6 +315,21 @@
 	return 0;
 }
 
+static ErrCode nextFreeBlock(u32 start, u32 *loc)
+{
+	u32 i;
+	ErrCode err;
+	for(i = start; i < MaxBlocks; i++) {
+		short ib;
+		if(isError(err = isBlockAt(i, &ib))) return err;
+		if(!ib) {
+			*loc = i;
+			return 0;
+		}
+	}
+	return mkError(MODFS, DISKFULL, ERROR);
+}
+
 //TODO ErrCode?
 static u32 getCacheLoc(u32 blkno)
 {