/* * Initialize filesystem. Read in file system metadata and initialize * memory structures. If there are inconsistencies, now would also be * a good time to deal with that. * * HINT: You don't need to deal with the 'conn' parameter AND you may * just return NULL. * */ static void* vfs_mount(struct fuse_conn_info *conn) { fprintf(stderr, "vfs_mount called\n"); // Do not touch or move this code; connects the disk dconnect(); /* 3600: YOU SHOULD ADD CODE HERE TO CHECK THE CONSISTENCY OF YOUR DISK AND LOAD ANY DATA STRUCTURES INTO MEMORY */ vcb volblock = getvcb(); if(volblock.disk_id != MAGICNUM){ fprintf(stderr, "Invalid disk: Invalid magic number."); dunconnect(); } if(volblock.mounted != 0){ fprintf(stderr, "Invalid disk: Disk did not unmount correctly."); dunconnect(); } else{ volblock.mounted = 1; setvcb(volblock); } return NULL; }
void myformat(int size) { // Do not touch or move this function dcreate_connect(); // Calculate number of data blocks, FAT entries. int num_dblocks = get_num_dblocks(size); // Format volume control block. vcb volblock = make_volblock(num_dblocks); char vcbtemp[BLOCKSIZE]; memset(vcbtemp,0,BLOCKSIZE); memcpy(vcbtemp,&volblock,sizeof(vcb)); dwrite(0,vcbtemp); // Format directory entries. dirent dent = make_directent(); char dirtemp[BLOCKSIZE]; //FIXME: Eventually will have to be smaller. memset(dirtemp,0,BLOCKSIZE); memcpy(dirtemp,&dent, sizeof(dirent)); for(int i = 1; i < 101; i++){ dwrite(i,dirtemp); } /* dirent dent; char dirblock[BLOCKSIZE]; memset(dirblock,0,BLOCKSIZE); memcpy(dirblock,&dirent,sizeof(dirblock)); for(int i = 1; i <= 100; i++){ dwrite(i,dirblock); } */ /* 3600: FILL IN CODE HERE. YOU SHOULD INITIALIZE ANY ON-DISK STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING A BLANK DISK. YOUR DISK SHOULD BE size BLOCKS IN SIZE. */ /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */ /* // first, create a zero-ed out array of memory char *tmp = (char *) malloc(BLOCKSIZE); memset(tmp, 0, BLOCKSIZE); // now, write that to every block for (int i=0; i<size; i++) if (dwrite(i, tmp) < 0) perror("Error while writing to disk"); */ // voila! we now have a disk containing all zeros // Do not touch or move this function dunconnect(); }
/* * Called when your file system is unmounted. * */ static void vfs_unmount (void *private_data) { fprintf(stderr, "vfs_unmount called\n"); /* 3600: YOU SHOULD ADD CODE HERE TO MAKE SURE YOUR ON-DISK STRUCTURES ARE IN-SYNC BEFORE THE DISK IS UNMOUNTED (ONLY NECESSARY IF YOU KEEP DATA CACHED THAT'S NOT ON DISK */ // Do not touch or move this code; unconnects the disk dunconnect(); }
void myformat(int size) { // Do not touch or move this function dcreate_connect(); //setting up vcb vcb myvcb; initvcb(&myvcb, size); dirent myde; initde(&myde); fatent myfat = { 0, 0, 0 }; //like 20% bruh char tmp[BLOCKSIZE]; int block = 0; //current block to write to //write vcb to file memset(tmp, 0, BLOCKSIZE); memcpy(tmp, &myvcb, sizeof(vcb)); dwrite(block,tmp); block++; //write directory entries to file for(int i=0; i<25; i++){ memset(tmp, 0, BLOCKSIZE); memcpy(tmp, &myde, sizeof(dirent)); memcpy(tmp + 128, &myde, sizeof(dirent)); memcpy(tmp + 256, &myde, sizeof(dirent)); memcpy(tmp + 384, &myde, sizeof(dirent)); if(dwrite(block,tmp)< 0) perror("Error while writing to disk"); block++; } //write fatents to file for (int i=0; i< myvcb.fat_length; i++){ memset(tmp, 0, BLOCKSIZE); for(int x=0; x<128; x++){ memcpy(tmp + (4 * x), &myfat, sizeof(fatent)); } if (dwrite(block, tmp) < 0) perror("Error while writing to disk"); block++; } //0 out diskspace where there will be data(not really needed..) for (int i=0; i<size; i++){ if (dwrite(block, tmp) < 0) perror("Error while writing to disk"); block++; } // Do not touch or move this function dunconnect(); }
/* * Called when your file system is unmounted. * */ static void vfs_unmount (void *private_data) { fprintf(stderr, "vfs_unmount called\n"); vcb volblock = getvcb(); // char temp[BLOCKSIZE]; // memset(temp, 0, BLOCKSIZE); // dread(0, temp); // memcpy(&volblock, temp, sizeof(volblock)); volblock.mounted = 0; setvcb(volblock); // memcpy(temp, &volblock, sizeof(volblock)); // dwrite(0, temp); /* 3600: YOU SHOULD ADD CODE HERE TO MAKE SURE YOUR ON-DISK STRUCTURES ARE IN-SYNC BEFORE THE DISK IS UNMOUNTED (ONLY NECESSARY IF YOU KEEP DATA CACHED THAT'S NOT ON DISK */ // Do not touch or move this code; unconnects the disk dunconnect(); }
void myformat(int size) { // Do not touch or move this function dcreate_connect(); /* 3600: FILL IN CODE HERE. YOU SHOULD INITIALIZE ANY ON-DISK STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING A BLANK DISK. YOUR DISK SHOULD BE size BLOCKS IN SIZE. */ blocknum *blocks = calloc(size-1, BLOCKSIZE); for (int i = 1; i < size; i++) { blocknum b; b.index = i; b.valid = 0; blocks[i] = b; } freeb *free_blocks = calloc(size - 3, BLOCKSIZE); for (int i = 0; i < size - 3; i++) { freeb f; blocknum b = blocks[i+4]; if ((i + 4) == size) { b.valid = 1; b.index = -1; } else { b.valid = 0; } f.next = b; free_blocks[i] = f; // printf("Free block created with next pointing to %d\n", b.index); } blocknum invalid_block; invalid_block.index = -1; invalid_block.valid = 0; vcb myvcb; myvcb.blocksize = BLOCKSIZE; myvcb.magic = 17; myvcb.root = blocks[1]; myvcb.root.valid = 1; myvcb.free = blocks[3]; strcpy(myvcb.name, "Josh and Mich's excellent file system."); dnode root; root.direct[0] = blocks[2]; root.direct[0].valid = 1; root.size = sizeof(blocknum); root.user = getuid(); root.group = getgid(); root.mode = 0777; root.single_indirect = invalid_block; root.double_indirect = invalid_block; for (int i = 1; i < 54; i++) { root.direct[i] = invalid_block; } struct timespec current_time; struct timeval ctval; if (gettimeofday(&ctval, NULL) != 0) { printf("Error getting time.\n"); } current_time.tv_sec = ctval.tv_sec; current_time.tv_nsec = ctval.tv_usec * 1000; // if ((clock_gettime(CLOCK_REALTIME, ¤t_time)) != 0) { // printf("ERROR: Cannot get time. \n"); // } root.access_time = current_time; root.modify_time = current_time; root.create_time = current_time; direntry dot; dot.type = 'd'; dot.block = blocks[1]; dot.block.valid = 1; strncpy(dot.name, ".", 55); direntry dotdot; dotdot.type = 'd'; dotdot.block = blocks[1]; dotdot.block.valid = 1; strncpy(dotdot.name, "..", 55); dirent root_dir; root_dir.entries[0] = dot; root_dir.entries[1] = dotdot; direntry empty; empty.type = '\0'; empty.block = blocks[size]; strcpy(empty.name, ""); for (int i = 2; i < 8; i++) { root_dir.entries[i] = empty; } //printf("The size of vcb is: %d\n", (int) sizeof(myvcb)); // Write the vcb to memory at block 0 if (bwrite(0, &myvcb) < 0) { perror("ERROR: Could not write vcb to disk.\n"); } // printf("The size of root_dir is %d\n", (int) sizeof(root_dir)); // Write the root dir to memory at block 1 char* root_tmp = malloc(sizeof(root)); memcpy(root_tmp, &root, sizeof(root)); if (dwrite(1, root_tmp) < 0) { perror("Error writing root dir to disk."); } free(root_tmp); // Write the root dir entries to memory at block 2 char* root_dir_tmp = malloc(sizeof(root_dir)); memcpy(root_dir_tmp, &root_dir, sizeof(root_dir)); if (dwrite(2, root_dir_tmp) < 0) { perror("Error writing root dir entries to disk."); } free(root_dir_tmp); // Write the rest of the disk as free blocks for (int i = 3; i < size; i++) { freeb f = free_blocks[i-3]; char* free_tmp = malloc(sizeof(f)); memcpy(free_tmp, &f, sizeof(f)); // printf("Writing free block %d to disk at block %d.\n", f.next.index, i); if (dwrite(i, free_tmp) < 0) { perror("Error writing free blocks to disk."); } free(free_tmp); } /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */ /* // first, create a zero-ed out array of memory char *tmp = (char *) malloc(BLOCKSIZE); memset(tmp, 0, BLOCKSIZE); // now, write that to every block for (int i=0; i<size; i++) if (dwrite(i, tmp) < 0) perror("Error while writing to disk"); // voila! we now have a disk containing all zeros */ // Do not touch or move this function dunconnect(); }
void myformat(int size) { // Do not touch or move this function dcreate_connect(); /* 3600: FILL IN CODE HERE. YOU SHOULD INITIALIZE ANY ON-DISK STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING A BLANK DISK. YOUR DISK SHOULD BE size BLOCKS IN SIZE. */ vcb *vol_ctrl_blk = (vcb *) calloc(1, sizeof(vcb)); if (vol_ctrl_blk == NULL) { printf("Error: Failed to allocate the volume control block.\n"); } vol_ctrl_blk->magic_num = MAGIC_NUM; vol_ctrl_blk->blocksize = BLOCKSIZE; vol_ctrl_blk->de_start = 1; // next block vol_ctrl_blk->de_length = NUM_DIRENTS; // allocate 100 files at start vol_ctrl_blk->fat_start = vol_ctrl_blk->de_start + vol_ctrl_blk->de_length; // next available block vol_ctrl_blk->fat_length = floor((size - (vol_ctrl_blk->de_length+1))/(NUM_FATENTS_PER_BLOCK + 1)); vol_ctrl_blk->db_start = 1 + vol_ctrl_blk->de_length + vol_ctrl_blk->fat_length; vol_ctrl_blk->volume_size = size; vol_ctrl_blk->user = getuid(); vol_ctrl_blk->group = getgid(); vol_ctrl_blk->mode = 0777; struct timespec time; clock_gettime(CLOCK_REALTIME, &time); vol_ctrl_blk->access_time = vol_ctrl_blk->modify_time = vol_ctrl_blk->create_time = time; // Initialize our disk cache to store all disk changes before flushing all at end. init_cache(vol_ctrl_blk); write_data(0, vol_ctrl_blk); // write empty_dirent to dirent blocks dirent empty_dirent; empty_dirent.valid = 0; write_region_data(vol_ctrl_blk->de_start, vol_ctrl_blk->de_start + vol_ctrl_blk->de_length, &empty_dirent); // Create empty fatent fatent empty_fatent; empty_fatent.used = 0; empty_fatent.eof = 0; empty_fatent.next = 0; fatent empty_fatents[NUM_FATENTS_PER_BLOCK]; for (unsigned int i=0; i < NUM_FATENTS_PER_BLOCK; i++) { // initialize the array to have NUM_FATENTS_PER_BLOCK in each block empty_fatents[i] = empty_fatent; // all empty } // write empty fatents to fatent blocks write_region_data(vol_ctrl_blk->fat_start, vol_ctrl_blk->fat_start + vol_ctrl_blk->fat_length, empty_fatents); // create a zero-ed out array of memory char *tmp = (char *) malloc(BLOCKSIZE); memset(tmp, 0, BLOCKSIZE); // write 0's to data blocks write_region_data(vol_ctrl_blk->db_start, size, tmp); free(tmp); // Flush our changes from the cache to disk flush_cache(vol_ctrl_blk); free(vol_ctrl_blk); // Do not touch or move this function dunconnect(); }
void myformat(int size) { // Do not touch or move this function dcreate_connect(); int num_dblocks = get_num_dblocks(size); // Format vcb vcb volblock = make_volblock(num_dblocks); // Do we need to malloc space(BLOCKSIZE) for vcbtemp? char vcbtemp[BLOCKSIZE]; memset(vcbtemp, 0, BLOCKSIZE); memcpy(vcbtemp, &volblock, BLOCKSIZE); dwrite(0, vcbtemp); // Format dirents dirent dent = make_dirent(); // Do we need to malloc space(BLOCKSIZE) for dirtemp? char dirtemp[BLOCKSIZE]; memset(dirtemp, 0, BLOCKSIZE); memcpy(dirtemp, &dent, BLOCKSIZE); for(int i = volblock.de_start; i < volblock.de_start+volblock.de_length; i++){ dwrite(i, dirtemp); } // char fat_block_temp[BLOCKSIZE]; // memset(fat_block_temp,0,BLOCKSIZE); fatent fe = make_fatent(); fatent fat_block[128]; int remaining = volblock.fat_length; int block = volblock.fat_start; while(remaining > 0){ for(int i = 0; i<128; i++){ fat_block[i] = fe; remaining--; } char fat_block_temp[BLOCKSIZE]; memset(fat_block_temp,0,BLOCKSIZE); memcpy(fat_block_temp,&fat_block,sizeof(fat_block)); dwrite(block,fat_block_temp); block++; } char empty_block[BLOCKSIZE]; memset(empty_block,0,BLOCKSIZE); for(int i = 0; i < num_dblocks; i++){ dwrite((volblock.db_start + i), empty_block); } /* char fat_entry_temp[4]; // memset(fat_entry_temp,0,4); // memcpy(fat_entry_temp,&fe,4); for(int i = volblock.fat_start; i<volblock.fat_start+volblock.fat_length;i++){ fprintf(stderr,"writing fat shit"); for(int j = 0; j < 128; j++){ // Set FAT defaults // Format single FAT block with 128 default FAT entries memcpy(&fat_block_temp[4*j],fat_entry_temp,4); fprintf(stderr,"memcpy(&fat_block_temp..."); } dwrite(i, fat_block_temp); }*/ /* 3600: FILL IN CODE HERE. YOU SHOULD INITIALIZE ANY ON-DISK STRUCTURES TO THEIR INITIAL VALUE, AS YOU ARE FORMATTING A BLANK DISK. YOUR DISK SHOULD BE size BLOCKS IN SIZE. */ /* 3600: AN EXAMPLE OF READING/WRITING TO THE DISK IS BELOW - YOU'LL WANT TO REPLACE THE CODE BELOW WITH SOMETHING MEANINGFUL. */ /* // first, create a zero-ed out array of memory char *tmp = (char *) malloc(BLOCKSIZE); memset(tmp, 0, BLOCKSIZE); // now, write that to every block for (int i=0; i<size; i++) if (dwrite(i, tmp) < 0) perror("Error while writing to disk"); // voila! we now have a disk containing all zeros */ // Do not touch or move this function dunconnect(); }