void mksfs(int fresh){ int i; if (fresh){ init_fresh_disk(disk_name, BLOCK_SIZE, NUM_BLOCKS); free_blocks = list_create(NULL); for (i=2; i<NUM_BLOCKS-1; i++){ list_append_int(free_blocks, i); } for (i=0; i<NUM_BLOCKS; i++){ fat[i][block_index] = unused; fat[i][next] = eof; } for (i=0; i<MAX_NUM_FILES; i++){ files[i].created = false; files[i].fatIndex = eof; } } else{ init_disk(disk_name, BLOCK_SIZE, NUM_BLOCKS); read_blocks(0,1, &files); read_blocks(1,1, &fat); read_blocks(NUM_BLOCKS-1, 1, &free_blocks); } for(i=0; i<MAX_NUM_FILES; i++){ fdt[i][open] = false; fdt[i][read_ptr] = false; fdt[i][write_ptr] = files[i].size; } }
int mksfs(int fresh) { int diskinit; //Create a new filesystem if the fresh variable is set if(fresh==1){ diskinit = init_fresh_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS); if(diskinit != 0){ //perror("Error creating disk"); exit(-1); } else { //To keep the root directory in memory initDirectory(); //Initialize SuperBlock mySuperBlock.magic = MAGIC; mySuperBlock.blockSize = BLOCKSIZE; mySuperBlock.fileSystemSize = NUMBEROFBLOCKS; mySuperBlock.iNodeTableLength = NUMBEROFINODES; mySuperBlock.rootINode = 0; //Use a block sized buffer unsigned char buffer[512]; memset(buffer,255,512); memcpy(buffer,&mySuperBlock,512); write_blocks(0,1,buffer); initializeFreeBitMap(); initializeFDTable(); //Now for the Inodes: initialize the first one: (for root dir) int numRootINode = createINode(RWE, 1, ROOT_UID, ROOT_GID, 1); readDirectory(); return 0; } } //if the free variable is not set, load else if (fresh==0) { diskinit = init_disk(FILESYSTEMNAME, BLOCKSIZE, NUMBEROFBLOCKS); if(diskinit != 0){ //perror("Error reading disc"); exit(-1); } else { return 0; } } return -1; }
//======================================================================================= //==============================PRIMARY API FUNCTIONS==================================== //======================================================================================= void mksfs(int fresh){ buffer = (void*) malloc(BLOCK_SIZE); memset(buffer,0,BLOCK_SIZE); if (fresh){ //FORMAT VIRTUAL DISK, CREATE NEW FROM SCRATCH init_fresh_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE); //=========Storing sb in-memory=========== sb = (superblock){0, BLOCK_SIZE, FILESYSTEM_SIZE, MAX_FD, 0}; memcpy(buffer, &sb, sizeof(superblock)); //========Storing sb on-disk============== write_blocks(0, 1, buffer); //========Initializing Bitmap block======= unsigned int bit_row = 4294967295; //Row consisting of 32 '1' bits for (int i = 0; i<256; i++){ bitmap[i] = bit_row; //Filling table with 1's (1 means block is free) } /*We use the first 3 blocks to store the Root Directory mapping table*/ bitmap[0] = bitmap[0] << 3; write_blocks(1, 1, bitmap); //========Storing inode table on-disk===== //inode inode_table[MAX_FD+1]; for (int i = 0; i <= MAX_FD; i++){ inode_table[i] = (inode){0, 1, 1, 0, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, -1}; if (i == 0) { //First inode is root dir inode. //It spans 3 blocks of data -> first three blocks in the datablock section inode_table[i] = (inode){0,1,1,3,{FIRST_DB,FIRST_DB+1,FIRST_DB+2,-1,-1,-1,-1,-1,-1,-1,-1,-1},-1}; } memcpy(buffer, &(inode_table[i]), sizeof(inode)); write_blocks(i+2, 1, buffer); } //====Initializing rootdir in-memory====== for (int i = 0; i < MAX_FD; i++){ root[i] = (dir){"", -1}; } //====Initializing rootdir on-disk======== free(buffer); buffer = (void*) malloc(BLOCK_SIZE*3); memset(buffer,0,BLOCK_SIZE*3); memcpy(buffer, &root, 3*BLOCK_SIZE); write_blocks(MAX_FD+3, 3, buffer); //====Initializing fd table in-memory===== for (int i = 0; i < MAX_FD; i++) fd_table[i] = (fd){1,-1,-1,-1}; //mark all entries in fd_table as empty (flag = 1 means empty) } else { init_disk(FILESYSTEM, BLOCK_SIZE, FILESYSTEM_SIZE); } free(buffer); }
int mksfs(int fresh) { super_block spr_block; char buff[BLKSIZ]; int i; // Initialize fd_table for(i = 0; i < MAXOPENFILES; i++) fd_table[i].status = FREE; if(fresh) { // Initialize and write super block to disk init_fresh_disk(DISKIMG, BLKSIZ, BLOCKNUM); spr_block.magic_number = MAGICNUM; spr_block.block_size = BLKSIZ; spr_block.fs_size = BLOCKNUM; spr_block.inode_tbl_length = INODETBLSIZ; spr_block.root_inode = ROOTINODENUM; write_blocks(SUPERBLOCKADD, 1, (void*) &spr_block); // Setup datablock bitmap memset(buff, FREE, BLKSIZ); write_blocks(DBBITMAPADD, 1, buff); // Setup INODE Bitmap and allocate first i-node entry to root directory buff[ROOTINODENUM] = USED; write_blocks(INBITMAPADD, 1, buff); // Allocate fd to root directory fd_table[ROOTFD].status = USED; fd_table[ROOTFD].inode_num = ROOTINODENUM; fd_table[ROOTFD].rw_pointer = 0; fd_table[ROOTFD].inode = (inode_struct*) calloc(1, sizeof(inode_struct)); fd_table[ROOTFD].inode->size = 0; write_inode_by_index(fd_table[ROOTFD].inode_num, fd_table[ROOTFD].inode); } else { init_disk(DISKIMG, BLKSIZ, BLOCKNUM); fd_table[ROOTFD].status = USED; fd_table[ROOTFD].inode_num = ROOTINODENUM; fd_table[ROOTFD].inode = get_inode_by_index(ROOTINODENUM); fd_table[ROOTFD].rw_pointer = fd_table[ROOTFD].inode->size; } return 0; }
void mksfs(int fresh) { if (fresh == 1){ //Makes a new disk if fresh flag is true printf("Initializing disk\n"); init_fresh_disk(DISK,BLOCK_SIZE,MAX_BLOCKS); fillWithZeroes(); //calloc for fdt fileDescriptorTable = (FileDescriptorTable*)calloc(1,sizeof(FileDescriptorTable)); //Write superblock printf("Writing superblock\n"); //setupSuperblock(superBlock); printf("Setting up superblock\n"); write_blocks(0,1,&superBlock); //Write root directory bzero(&rootDirectory,sizeof(Directory)); printf("Writing root directory\n"); write_blocks(1,1, &rootDirectory); //Write free blocks printf("Writing free blocks\n"); freeBlocks.numberFree = MAX_BLOCKS; freeBlocks.freeBlocks[0] = 1; //superblock freeBlocks.numberFree--; freeBlocks.freeBlocks[1] = 1; //root directory freeBlocks.numberFree--; freeBlocks.freeBlocks[MAX_BLOCKS-1] = 1; //free_blocks; freeBlocks.numberFree--; //Write free blocks to disk printf("writing free blocks to disk \n"); write_blocks(MAX_BLOCKS - 1, 1, &freeBlocks); }else{ printf("Loading disk\n"); //Pull disk data into memory init_disk(DISK, BLOCK_SIZE, MAX_BLOCKS); } //Initializes variable for sfs_getnextfilename; sfsGetNextIndex = 0; //Initializes variable for fileDescriptor table (*fileDescriptorTable).firstOpenIndex = 0; (*fileDescriptorTable).size = 0; //Initializes variable for rootDirectory rootDirectory.firstOpenIndex = 2; rootDirectory.size = 3; return; }
/** * creates the file system */ void mksfs(int fresh) { // Allocate the local file system allocate_necessary_memory(); //Implement mksfs here if (fresh) { // We need to construct the file system from scratch // Delete the disk file, if it exists remove(FILE_SYSTEM_NAME); // Initialize a new fresh disk init_fresh_disk(FILE_SYSTEM_NAME, DISK_BLOCK_SIZE, DISK_BLOCK_COUNT); // Write superblock to disk SuperBlock sb = {}; sb.magic = SB_MAGIC; sb.block_size = 512; sb.file_system_size = DISK_BLOCK_COUNT; sb.i_node_table_length = I_NODE_COUNT; sb.root_directory_i_node = 0; write_blocks(SUPER_BLOCK_INDEX, 1, &sb); // Write the rest of the stuff to disk save_local_file_system_to_disk(freeBitMap, iNodeTable, directoryCache); } else { // File system already exists on disk, so we need to load it from the disk. // Initialize an existing disk init_disk(FILE_SYSTEM_NAME, DISK_BLOCK_SIZE, DISK_BLOCK_COUNT); // Copy iNodes into local memory load_i_node_cache_from_disk(iNodeTable); // Copy directory cache into memory load_directory_cache_from_disk(directoryCache); // Copy the free bit map into memory load_free_bitmap_from_disk(freeBitMap); } return; }
//Creates the file system int mksfs(int fresh){ char buf[BLOCKSIZE]; memset(buf,0, BLOCKSIZE); //sets a block-size buffer and set it to zero if(fresh){ init_fresh_disk(DISK_FILE, BLOCKSIZE, TOTAL_NUM_BLOCKS); //initialize super block super_block superblock; superblock.magic=0xAABB0005; superblock.block_size=BLOCKSIZE; superblock.file_system_size=TOTAL_NUM_BLOCKS; superblock.inode_table_length=NUMBER_INODES; superblock.root_dir_inode_number=0; //set root directory at the first inode //copy super block to buffer memcpy((void *)buf,(const void *) &superblock, sizeof(super_block)); //write the superblock to the first block in the disk write_blocks(0,1, buf); //initialize inode table memset(inodes, 0, sizeof(inodes)); //Set first inode to root directory inode INODE root_dir_inode={ .mode=0777, .link_cnt=1, .uid=0, .gid=0, .size=0, .pointers={14,15,16,17,18,0,0,0,0,0,0,0,0} //pointer to the first data block starting at 14 }; inodes[0]=root_dir_inode; //write inode into disk int inode_number = 0; int block_of_inode=inode_number/8+1; char inode_update_buf[BLOCKSIZE]; memset(inode_update_buf,0, BLOCKSIZE); read_blocks(block_of_inode, 1, inode_update_buf); memcpy((void *)inode_update_buf+(inode_number%8)*sizeof(INODE),(const void *) &inodes[inode_number], sizeof(INODE)); write_blocks(block_of_inode, 1, inode_update_buf); //initialize the bitmap memset(free_bitmap, 0, sizeof(free_bitmap)); free_bitmap[0]=255; //superblock and first part of inodes to occupied. free_bitmap[1]=255; //inodes table to occupied. free_bitmap[2]=224; //directory entry table to occupied. free_bitmap[BLOCKSIZE-1]=1; //last block for the bitmap write_blocks(TOTAL_NUM_BLOCKS-1,1,free_bitmap);//write the bitmap into disk //initialize directory table into memory memset(dir_table, 0, sizeof(dir_table)); //Initialize all file's RW pointers to 0 and open to 0 memset(file_des_t, 0, sizeof(file_des_t)); cur_pos=-1; return 0; } else{ //file system already exists init_disk(DISK_FILE, BLOCKSIZE, TOTAL_NUM_BLOCKS); // set free_bitmap to zeros memset(free_bitmap,0, sizeof(free_bitmap)); //load existing bitmap read_blocks(TOTAL_NUM_BLOCKS-1,1,free_bitmap); //load existing superblock super_block superblock; read_blocks(0,1,&superblock); //load existing inode table char inodes_buffer[BLOCKS_INODES_TABLE*BLOCKSIZE]; memset(inodes_buffer,0,sizeof(inodes_buffer)); read_blocks(1,BLOCKS_INODES_TABLE,inodes_buffer); int n; for(n=0;n<NUMBER_INODES;n++){ memcpy((void *)&(inodes[n]),(const void *)(inodes_buffer+n*(BLOCKSIZE/8)), BLOCKSIZE/8); } //load existing directory table INODE root_directory=inodes[superblock.root_dir_inode_number]; int i; char root_dir_buffer[BLOCKSIZE]; memset(root_dir_buffer,0,BLOCKSIZE); for(i=0;i<INODE_POINTERS;i++){ if(root_directory.pointers[i]!=0){ read_blocks(root_directory.pointers[i],1, root_dir_buffer); int j; for(j=0;j<NUM_ENTRIES_PERBLOCK;j++){ if((j+NUM_ENTRIES_PERBLOCK*i) >= TOTAL_NUM_FILES){ break; } memcpy((void *)&(dir_table[j]), (const void *)(root_dir_buffer+j*(sizeof(dir_entry))), sizeof(dir_entry)); } memset(root_dir_buffer,0,BLOCKSIZE); } } //initialize all rw pointers to 0 and open to 0 memset(file_des_t, 0, sizeof(file_des_t)); cur_pos=-1; return 0; } }
void mksfs(int fresh){ //Implement mksfs here if (fresh == 1){ //deletes fs if it already exists if(access(DISK_FILE, F_OK) != -1){ unlink(DISK_FILE); } //fs creation printf("Initalizing sfs\n"); init_fresh_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS); //zero_everything(); // writes superblock to the first block printf("Writing superblocks\n"); init_superblock(); write_blocks(0, 1, &sb); // write the inode table to the 2nd block printf("Writing inode table\n"); init_inode_table(); //add_root_dir_inode(); //add_dummy_file_inode(); //write_blocks(1, 1, &inode_table); // creates free list init_free_list(); // write root directory data to the 3rd block printf("Writing root dir\n"); init_root_dir(); //allocate memory for directory inode inode_t *inode = malloc(ADJUST((MAX_FILES+1)*sizeof(inode_t))); read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode); if(inode == NULL){ return; } //set first inode to point to directory inode[0].size = DIRECTORY_SIZE*BLOCK_SIZE; inode[0].link_cnt = DIRECTORY_SIZE; inode[0].exists = 1; //check to see if we need to use ref_ptr //if(DIRECTORY_SIZE > 12){ //inode[0].ref_ptr = search(); //setAlloc(inode[0].ref_ptr); //unsigned int *buffer = malloc(BLOCK_SIZE); //write_blocks(19 + inode[0].ref_ptr, 1, buffer); //free(buffer); //assign the pointers the location of directory files int i; for(i = 0; i < DIRECTORY_SIZE; i++){ if(i > 11){ unsigned int *buffer = malloc(BLOCK_SIZE); read_blocks(19+ inode[0].ref_ptr, 1, buffer); buffer[i - 12] = 2 + i; write_blocks(19+ inode[0].ref_ptr, 1, buffer); free(buffer); } else{ inode[0].data_ptrs[i] = 2 + i; } } //update inode and free memory write_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode); free(inode); } else if (fresh == 0){ if(init_disk(DISK_FILE, BLOCK_SIZE, MAX_BLOCKS) != 0){ printf("Error initializing disk\n"); return; } } //allocate main memory for filesystem data structures int *superblock = malloc(BLOCK_SIZE*SUPERBLOCK_SIZE); if(superblock == NULL){ printf("Error allocating memory for superblock\n"); return; } read_blocks(0, SUPERBLOCK_SIZE, superblock); //allocate main memory for directory root_dir = malloc(ADJUST(sizeof(dir_entry_t)*MAX_FILES)); if(root_dir == NULL){ printf("Error allocating memory for root directory\n"); return; } read_blocks(2, DIRECTORY_SIZE, root_dir); //allocate memory for inode table inode_table = malloc(ADJUST(sizeof(inode_t)*(MAX_FILES+1))); if(inode_table == NULL){ printf("Error allocating memory for inode table"); return; } read_blocks(INODE_TABLE, INODE_TABLE_SIZE, inode_table); fileCount = 0; fd_table = NULL; return; }