#include <stddef.h> #include <unistd.h> #include <string.h> #include "ubixfs.h" #include "btree.h" UbixFS::UbixFS(device_interface * dev) { device = dev; freeBlockList = NULL; } // UbixFS::UbixFS int UbixFS::vfs_init(void) { return 0; } // UbixFS::init blockRun UbixFS::getFreeBlock(uInt32 AG) { // AG == AllocationGroup blockRun br; signed char * ptr; int32 count; int32 subCount = 128; br.allocationGroup = 0; br.start = 0; br.len = 0; // Check to make sure neither of these are null if (device == NULL || freeBlockList == NULL || superBlock == NULL) return br; // Are there any blocks available? if (superBlock->numBlocks == superBlock->usedBlocks) return br; /* * count is the block from the base of the list. * Since we're given a specific AG to look through, we start the count at * AG << AGShift, where AGShift is the shift value of the number of blocks * in an AG */ count = (AG << superBlock->AGShift); /* * The freeBlockList is a bit map of the free/used blocks. * Used = on bit * Unused = off bit * There are 8 bits per byte (hopefully) and so we have to divide the count * by 8 to get our starting byte offset to look from */ ptr = freeBlockList + (count >> 3); // Scan through the freeBlockList rescan: while (*ptr == -1) { ++ptr; count += 8; if (count+8 > superBlock->numBlocks) break; } // while *ptr == -1 subCount = 128; do { if ((*ptr & subCount) == 0) break; subCount >>= 1; ++count; if (count == superBlock->numBlocks) { count = 0; ptr = freeBlockList; goto rescan; } // if } while(subCount > 1); *ptr |= subCount; // mark this block as used ++superBlock->usedBlocks; // increment the number of used blocks br.allocationGroup = count / superBlock->blocksPerAG; br.start = count % superBlock->blocksPerAG; br.len = 1; return br; // return the allocated block number } // Ubixfs::getFreeBlock uInt32 UbixFS::getNextAG(void) { if (superBlock->lastUsedAG == superBlock->numAGs) superBlock->lastUsedAG = 0; else superBlock->lastUsedAG++; return superBlock->lastUsedAG; } // UbixFS::getNextAG /* * UbixFS::getFreeBlock(void) * upon success returns a free block based on the next AG after the lastUsedAG * failure returns -1 */ blockRun UbixFS::getFreeBlock(void) { return getFreeBlock(getNextAG()); } // UbixFS::getFreeBlock blockRun UbixFS::get8FreeBlocks(uInt32 AG) { // AG == AllocationGroup blockRun br; signed char * ptr; signed char * endPtr; int32 count; br.allocationGroup = 0; br.start = 0; br.len = 0; if (device == NULL || freeBlockList == NULL || superBlock == NULL) return br; // Are there any blocks available? if (superBlock->usedBlocks+8 > superBlock->numBlocks) return br; /* * count is the block from the base of the list. * Since we're given a specific AG to look through, we start the count at * AG << AGShift, where AGShift is the shift value of the number of blocks * in an AG */ count = (AG << superBlock->AGShift); ptr = freeBlockList + (count >> 3); endPtr = freeBlockList + (superBlock->numBlocks >> 3); bool secondTime = false; while (*ptr != 0) { ++ptr; count += 8; if (ptr == endPtr) { if (secondTime) return br; else secondTime = true; count = 0; ptr = freeBlockList; } // if } // while *ptr = -1; // mark 8 blocks as taken br.allocationGroup = count / superBlock->blocksPerAG; br.start = count % superBlock->blocksPerAG; br.len = 8; return br; } // UbixFS::get8FreeBlocks void * UbixFS::mknod(const char *filename, ubixfsInode * parent) { ubixfsInode * inode = NULL; inode = new ubixfsInode; if (inode == NULL) return NULL; memset(inode, 0, sizeof(ubixfsInode)); // inode->magic1 = ; if (parent == NULL) { inode->inodeNum = getFreeBlock(); inode->parent.iAddr.allocationGroup = 0; inode->parent.iAddr.start = 0; inode->parent.iAddr.len = 0; } else { inode->inodeNum = getFreeBlock(parent->inodeNum.allocationGroup); inode->parent.iAddr = parent->inodeNum; } // else strncpy(inode->name, filename, MAX_FILENAME_LENGTH); inode->uid = getuid(); inode->gid = getgid(); // inode->mode // inode->flags // inode->createTime // inode->lastModifiedTime // inode->type inode->inodeSize = superBlock->inodeSize; /* * next and prev are used in memory to hold pointers to the next/prev * inodes in this dir. On disk they may have another value, but for * now they should be set to null. */ inode->next.offset = 0; inode->prev.offset = 0; return inode; } // UbixFS::mknod void * UbixFS::vfs_mknod(const char *path, mode_t mode) { return mknod(path, 0); } // UbixFS::mknod int UbixFS::vfs_format(device_interface * dev) { if (dev == NULL) return -1; // ubixDiskLabel *d = new ubixDiskLabel; diskSuperBlock *sb = new diskSuperBlock; if (sb == NULL) return -1; memset(sb, 0, sizeof(diskSuperBlock)); return 0; } // UbixFS::vfs_format UbixFS::~UbixFS(void) { delete [] freeBlockList; return; }