int main() { unsigned int cpt = 0, i; /* init hardware */ if(!init_hardware(HW_CONFIG)){ perror("Initialization error\n"); exit(EXIT_FAILURE); } /* Interreupt handlers */ for(i=0; i<16; i++) IRQVECTOR[i] = empty_it; /* Allows all IT */ _mask(1); /* chargement du mbr */ if(!load_mbr()){ perror("Erreur lors du chargement du Master Boot Record."); exit(EXIT_FAILURE); } /* creation d'un volume bidon */ if(!make_vol(1, 1, 10)) { perror("Erreur a la creation d'un volume bidon."); exit(EXIT_FAILURE); } /* initialise le super du premier volume */ init_super(CURRENT_VOLUME); /* charge le super du premier volume dans la variable globale */ load_super(CURRENT_VOLUME); /* creation de nouveau bloc jusqu'a ce qu'il y est une erreur */ while(new_bloc()); if(is_full()) { free_bloc(2); free_bloc(5); free_bloc(6); printf("nombre de bloc libre = %d\n", get_nb_free_bloc()); while(new_bloc()) cpt++; printf("le nombre de bloc alloué est %d\n", cpt); } else { printf("le disque n'est pas encore rempli\n"); } exit(EXIT_SUCCESS); }
void test_superbloc(){ int i=0; execute("list"); printf("Choisir le volume sur lequel nous allons effectuer les opérations: "); scanf("%d", ¤t_vol); current_vol=current_vol-1; //current_vol=1; if(current_vol == 0 && mbr.nbr_volumes == 0){ printf("Erreur il n'y a pas de volume, veuillez en créer un avant\n"); return; } if( load_super(current_vol) == 1 ){ printf("Chargement de Super réussi !\n"); } else printf("Chargement de Super: FAIL\n"); printf("Size: %d", mbr.disque[current_vol].size); for(i=0; i<5; i++){ printf("\ni = %d", i); printf("\tfirst-free-bloc: %d", current_super.super_first_free_bloc); printf("\tnb_free: %d\n", current_super.super_nb_free); new_bloc(); } i=10; for(i=10; i<15; i++){ printf("\ni = %d", i); printf("\tfirst-free-bloc: %d", current_super.super_first_free_bloc); printf("\tnb_free: %d\n", current_super.super_nb_free); free_bloc(i); } }
/*initialize a new bloc with all 0 inside*/ unsigned new_bloc_zero(){ int res; unsigned char buf[SECTORSIZE] = {0}; res=new_bloc(); write_bloc(CURRENT_VOLUME, res, buf); return res; }
/* alloue et initialise un bloc et range l'inoeud dedans retourne l'inode correspondant*/ unsigned int create_inode(enum file_type_e type) { int i,inumber; struct inode_s inode; inode.type = type; inode.ind_size = 0; /* inode size */ for(i = 0 ; i < NDIRECT; i++) { inode.direct[i] = 0; } inumber = new_bloc(); if(inumber == 0) { return 0;} /* volume plein */ write_inode(inumber,&inode); /*on alloue le bloc pour y ranger l'inoeud */ return inumber; }
/* * @brief Détermine le bon bloc * * @param kv descripteur d'accès à la base * @param offset_bloc index dans le .blk * @param offset_data index dans le .kv (à écrire) */ int write_bloc(KV *kv, len_t offset_bloc, len_t * offset_data) { len_t offset_lu_courant, offset_courant, offset_bloc_suivant, offset_sauvegarde = offset_bloc; int i; off_t off; while(offset_bloc) { read_entete_bloc(kv, offset_bloc, &offset_bloc_suivant); // ce qui déplace juste apres l'en-tête du bon bloc for(i = 0; i < 1023; i++) { off = lseek(kv->fd2, 0, SEEK_CUR); if(off == -1) { return -1; } offset_courant = off; read(kv->fd2, &offset_lu_courant, 4); if(offset_lu_courant == 0) { write_bloc_entry(kv, offset_courant, *offset_data); return 42; } } offset_bloc = offset_bloc_suivant; } // pas de bloc suivant et plus de place dans le bloc courant len_t offset_new_bloc; if(new_bloc(kv, &offset_new_bloc)) { return -1; } if(write_bloc_entry(kv, offset_sauvegarde, offset_new_bloc)) { return -1; }; // lien entre les 2 blocs inverser les 2 ? if(write_bloc(kv, offset_new_bloc, offset_data)) { return -1; }; return 42; }
int main(int argc, char **argv) { uint volume; init_hard(); if(argc < 2){ printf("exe: ./create_bloc volume\n"); printf("On which volume: "); scanf("%d", &volume); } else { volume = atoi(argv[1]); } assert(volume < MAX_VOL); if(load_mbr() == 1){ printf("Okay, partition ready to be use\n"); } else { printf("Partition not initialized.. Now it is !"); } info_on_volume_to_display(volume); if(load_super(volume) == 1){ printf("Okay, superbloc of volume n°%d loaded\n", volume); } else { printf("Superbloc not initialized.. :(\n"); } while (superbloc.nb_free != 0) { printf("New bloc: %d\n", new_bloc()); } printf("There is no more freebloc available... :(\n"); while (mbr.vol[volume].nb_bloc-1 != superbloc.nb_free) { printf("free_bloc(%d)\n", (mbr.vol[volume].nb_bloc-1) - superbloc.nb_free); free_bloc((mbr.vol[volume].nb_bloc-1) - superbloc.nb_free); } save_super(); save_mbr(); printf("All blocs are now available and free. :)\n"); info_on_volume_to_display(volume); return 0; }
unsigned int create_inode(enum file_type_e type) { int i,inumber; struct inode_s inode; inode.type = type; inode.ind_size = 0; inode.taille = 0; for(i = 0 ; i < n ; i++) { inode.inode_direct[i] = 0; } inumber = new_bloc(); if(inumber == 0) { return 0;} /* volume plein */ write_inode(inumber,&inode); return inumber; }
unsigned int create_inode(enum file_type_e type) { struct inode_s inode; int i; /*memset(&inode, 0, sizeof(struct inode_s)); */ inode.type = type; inode.size = 0; inode.blocs_double_indirect = 0; inode.blocs_indirect = 0; for (i=0; i<NB_DIRECTS; i++){ inode.blocs_directs[i] = 0; } unsigned int inumber = new_bloc(); write_inode(inumber, &inode); return inumber; }
unsigned int create_inode(enum file_type_e type) { struct inode_s inode; unsigned int inumber; memset(&inode, 0, sizeof(struct inode_s)); inode.inode_type = type; inumber = new_bloc(); if(inumber == BLOC_NULL) { fprintf(stderr, "Impossible to create new inode. no space remain\n"); return BLOC_NULL; } write_inode(inumber, &inode); return inumber; }
/* * @brief Gestion du .blk lors de l'ajout d'un couple key/val * * Si un index de bloc existe déjà : * -> écriture de offset_key dans ce bloc (avec gestion de bloc plein) * Sinon : * -> création d'un nouveau bloc et écriture de l'index de ce bloc dans le .h * -> écriture de offset_key dans ce bloc (avec gestion de bloc plein) * * @param kv descripteur d'accès à la base * @param key clé * @param offset_key index du couple dans le .kv */ int kv_put_blk(KV *kv, const kv_datum *key, len_t *offset_key) { len_t val_h, offset_h; int offset_int = hash(key->ptr, kv); if(offset_int == -1) { return -1; } else { offset_h = offset_int; } int n = read_h(kv, offset_h, &val_h); if(n == -1) { return -1; } if(!n || val_h == 0) // clé pas hachée { len_t offset_new_bloc; if(new_bloc(kv, &offset_new_bloc) == -1) { return -1; } if(write_h(kv, offset_h, offset_new_bloc) == -1) { return -1; } if(write_bloc(kv, offset_new_bloc, offset_key) == -1) { return -1; } } else // clé déjà hachée { if(write_bloc(kv, val_h, offset_key) == -1) { return -1; } } return 42; }
/*Create a new inode initialized with 0*/ unsigned int create_inode(enum file_type_e type) { /*Create the inode initialized with 0*/ struct inode_s inode; int i, inumber; inode.type = type; inode.size = 0; for(i=0; i<NDIRECT; i++) { inode.direct[i] = 0; } inode.indirect = 0; inode.two_indirect = 0; /*Place the inode on a free bloc*/ inumber = new_bloc(); if(inumber == 0) { return 0; } write_inode(inumber, &inode); return inumber; }
int main(){ int vol; int nom; int nbblocs; int i; unsigned int test_serial = 0xDEADBEEF; init_drive(); read_mbr(); dump_vols(); printf("Saisir le numero du volume:\n"); scanf("%i", &vol); while (mbr.mbr_vol[vol].vol_nblocs == 0){ printf("Veuillez saisir un numero de volume valide:\n"); scanf("%i", &vol); } printf("Saisir le nom:\n"); scanf("%i", &nom); init_super(vol, nom, test_serial); printf("Saisir le nombre de blocs:\n"); scanf("%i", &nbblocs); for (i=0; i<nbblocs; i++){ new_bloc(); } save_super(); if (nbblocs > mbr.mbr_vol[vol].vol_nblocs-1){ nbblocs = mbr.mbr_vol[vol].vol_nblocs-1; } printf("Superbloc rempli de %d blocs !\n", nbblocs); printf("Taux d'occupation : %f/100\n", used_percents(vol)); return 0; }
// Recopié depuis le cahier, les noms sont pas forcément les bons unsigned int vbloc_of_fbloc(unsigned int inumber, unsigned int fbloc,bool_t do_allocate){ struct inode_s inode; unsigned int bloc_index = fbloc; read_inode(inumber, &inode); //direct if (bloc_index < NDIRECT){ if (inode.direct[bloc_index] == 0){ if(do_allocate){ inode.direct[bloc_index] = initialize_bloc(new_bloc()); write_inode(inumber,&inode); }else{return BLOC_NULL;} } return inode.direct[bloc_index]; } bloc_index -= NDIRECT; //indirect simple if (bloc_index < NBPB){ // if the indirect entry in the inode is not allocated yet if (inode.direct == 0){ if(do_allocate){ inode.indirect = initialize_bloc(new_bloc());; write_inode(inumber,&inode); }else{return BLOC_NULL;} } int indirect[NBPB]; read_bloc(current_volume, inode.indirect, (unsigned char *)indirect); if (indirect[bloc_index] == 0){ if(do_allocate){ indirect[bloc_index] = initialize_bloc(new_bloc());; write_bloc(current_volume, inode.indirect, (unsigned char*)indirect); }else{return BLOC_NULL;} } return indirect[bloc_index]; } bloc_index -= NBPB; //indirect double if(bloc_index < NBPB*NBPB){ if (inode.two_indirect == 0){ if(do_allocate){ inode.two_indirect = initialize_bloc(new_bloc());; write_inode(inumber,&inode); }else{return BLOC_NULL;} } int db_indirect_index = bloc_index / NBPB; int indirect_index = bloc_index % NBPB; int db_indirect[NBPB]; read_bloc(current_volume, inode.two_indirect, (unsigned char*)db_indirect); if (db_indirect[db_indirect_index] == 0){ if(do_allocate){ db_indirect[db_indirect_index] = initialize_bloc(new_bloc()); write_bloc(current_volume, inode.two_indirect, (unsigned char*)db_indirect); }else{return BLOC_NULL;} } int indirect[NBPB]; read_bloc(current_volume, db_indirect[db_indirect_index], (unsigned char*)indirect); if (indirect[indirect_index] == 0){ if(do_allocate){ //printf("allocate indirect[%d]\n",indirect_index); indirect[indirect_index] = initialize_bloc(new_bloc());; write_bloc(current_volume, db_indirect[db_indirect_index], (unsigned char*)indirect); }else{return BLOC_NULL;} } return indirect[indirect_index]; } //fprintf(stderr,"fbloc is too big.\n\tfbloc provided: %d\n\tfbloc max size: %d",fbloc, NDIRECT+NBPB+NBPB*NBPB); return -1; }
int main(int argc, char** argv) { /* Variable declaration */ unsigned char* buffer; int free_size = 0, count = 0, i, j; /* Variable initialization */ srand(time(NULL)); buffer = (unsigned char*) malloc(HDA_SECTORSIZE * sizeof(unsigned char)); /* Check the usage of the main program */ cmdname = argv[0]; processtype = argv[1]; if (argc == 1) { usage(); return EXIT_FAILURE; } /* Only disk creation */ if(strcmp(processtype, "mkhd") == 0) { /* Delete the old disks */ remove("vdiskA.bin"); remove("vdiskB.bin"); /* New disk initialization */ init_master(); printf("The disks have been successfully created.\n"); return EXIT_SUCCESS; } /* Disk initialization */ init_master(); /* Load master boot record and partition information */ load_mbr(); init_volume(); /* Get the status of the disk (free space) */ if (strcmp(processtype, "status") == 0) { if (!load_super(CURRENT_VOL)) { fprintf(stderr, "No file system on the chosen partition\n"); return EXIT_FAILURE; } printf("Space status of the volume : %s \n", current_super.super_name); free_size = mbr.mbr_vol[CURRENT_VOL].vol_n_sector - 1; double percent = (current_super.super_nb_free / (double) free_size) * 100; printf("Free space : %f %\n", percent); printf("Free blocs : %d\n", current_super.super_nb_free); return EXIT_SUCCESS; } /* Fill the partition */ if (strcmp(processtype, "debug_fill") == 0) { printf("Filling the current partition\n"); if (!load_super(CURRENT_VOL)) { fprintf(stderr, "No file system on the chosen partition\n"); return EXIT_FAILURE; } int b; do { b = new_bloc(); if(b != BLOC_NULL) { count++; printf("."); fflush(stdout); } } while (b != 0); printf("\n"); printf("Number of allocated blocs : %d\n", count); return EXIT_SUCCESS; } /* Random free of the partition */ if (strcmp(processtype, "debug_free") == 0) { unsigned size = mbr.mbr_vol[CURRENT_VOL].vol_n_sector - 1; unsigned begin = mbr.mbr_vol[CURRENT_VOL].vol_first_sector; int it = begin + size; int n; count = 0; if (!load_super(CURRENT_VOL)) { fprintf(stderr, "No file system on the chosen partition\n"); return EXIT_FAILURE; } /* Check if the partition is empty */ if(current_super.super_nb_free == size) { fprintf(stderr, "No bloc to free, the current partition is empty.\n"); return EXIT_FAILURE; } /* Random free of the partition blocs */ for (n = begin; n < it / 10; n++) { int random = rand() % it; if(random == 0) continue; free_bloc(random); printf("%d\n", random); count++; } printf("Number of desallocated blocs : %d\n", count); return EXIT_SUCCESS; } /* Make a new filesystem */ if (strcmp(processtype, "mknfs") == 0) { init_super(CURRENT_VOL); printf("A new file system has been successfully installed on the current partition N°%d.\n", CURRENT_VOL); return EXIT_SUCCESS; } /* Process to format the entire disk */ if (strcmp(processtype, "frmt") == 0) { for (i = 0; i < HDA_MAXCYLINDER; i++) { for (j = 0; j < HDA_MAXSECTOR; j++) { format_sector(i, j, HDA_SECTORSIZE, 0); } } printf("The entire disk has been successfully formated.\n"); return EXIT_SUCCESS; } /* Process to dump the content of the first sector */ if (strcmp(processtype, "dmps") == 0) { read_sector_n(0, 0, HDA_SECTORSIZE, buffer); dump(buffer, HDA_SECTORSIZE, 0, 1); return EXIT_SUCCESS; } /* Process type unknown */ usage(); return EXIT_FAILURE; }
unsigned int vbloc_of_fbloc(unsigned int inumber, unsigned int fbloc, bool_t do_allocate) { struct inode_s inode; unsigned int buffer_indirect[SECTOR_SIZE]; unsigned int buffer_indirect2[SECTOR_SIZE]; PRINT_ASSERT_ERROR_MSG(inumber != BLOC_NULL, "Null inode (vbloc_of_fbloc)"); read_inode(inumber, &inode); if(fbloc < NB_DIRECT_BLOC) { /* Direct */ if(inode.inode_direct_bloc[fbloc] == BLOC_NULL) { if(do_allocate) { inode.inode_direct_bloc[fbloc] = new_bloc(); write_inode(inumber, &inode); } else { return BLOC_NULL; } } return inode.inode_direct_bloc[fbloc]; } else if(fbloc < NB_INDIRECT_BLOC + NB_DIRECT_BLOC) { /* Indirect 1 */ fbloc -= NB_DIRECT_BLOC; unsigned int vbloc; /* Level 0 */ if(inode.inode_indirect_1 == BLOC_NULL) { if(do_allocate) { inode.inode_indirect_1 = new_bloc(); if(inode.inode_indirect_1 == BLOC_NULL) { return BLOC_NULL; } /* set 0 to new bloc */ memset(buffer_indirect, 0, SECTOR_SIZE); write_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); write_inode(inumber, &inode); } else { return BLOC_NULL; } } /* Level 1 */ read_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); vbloc = buffer_indirect[fbloc]; if(vbloc == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc] = new_bloc(); if(buffer_indirect[fbloc] == BLOC_NULL) { return BLOC_NULL; } write_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); vbloc = buffer_indirect[fbloc]; } else { return BLOC_NULL; } } return vbloc; } else if (fbloc < NB_INDIRECT_BLOC + NB_DIRECT_BLOC + NB_INDIRECT_BLOC*NB_INDIRECT_BLOC) { /* Indirect 2 */ unsigned int vbloc_1, vbloc_2; fbloc -= NB_DIRECT_BLOC+NB_INDIRECT_BLOC; /* Level 0 */ if(inode.inode_indirect_2 == BLOC_NULL) { if(do_allocate) { inode.inode_indirect_2 = new_bloc(); if(inode.inode_indirect_2 == BLOC_NULL) { return BLOC_NULL; } /* set 0 to new bloc */ memset(buffer_indirect, 0, SECTOR_SIZE); write_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); write_inode(inumber, &inode); } else { return BLOC_NULL; } } /* Level 1 */ read_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); vbloc_1 = buffer_indirect[fbloc/NB_INDIRECT_BLOC]; if(vbloc_1 == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc/NB_INDIRECT_BLOC] = new_bloc(); if(buffer_indirect[fbloc/NB_INDIRECT_BLOC] == BLOC_NULL) { return BLOC_NULL; }; /* set 0 to new bloc */ memset(buffer_indirect2, 0, SECTOR_SIZE); write_bloc(current_vol, buffer_indirect[fbloc/NB_INDIRECT_BLOC], (unsigned char*)buffer_indirect2); write_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); vbloc_1 = buffer_indirect[fbloc/NB_INDIRECT_BLOC]; } else { return BLOC_NULL; } } /* Level 2 */ read_bloc(current_vol, vbloc_1, (unsigned char*)buffer_indirect); vbloc_2 = buffer_indirect[fbloc%NB_INDIRECT_BLOC]; if(vbloc_2 == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc%NB_INDIRECT_BLOC] = new_bloc(); if(buffer_indirect[fbloc%NB_INDIRECT_BLOC] == BLOC_NULL) { return BLOC_NULL; } write_bloc(current_vol, vbloc_1, (unsigned char*)buffer_indirect); vbloc_2 = buffer_indirect[fbloc%NB_INDIRECT_BLOC]; } else { return BLOC_NULL; } } return vbloc_2; } else return BLOC_NULL; }