// Superbloc void init_super(uint vol, char *nom) { // read_bloc_n(vol, 0, (uchar *) &superbloc, sizeof(struct superbloc_s)); load_super(vol); if(superbloc.magic == MAGIC_SUPERBLOC){ printf("Wait, superbloc already initialized :)\n"); exit(EXIT_FAILURE); } else { superbloc.magic = MAGIC_SUPERBLOC; strcpy(superbloc.nom, nom); superbloc.first_free_bloc = 1; superbloc.root = 0; superbloc.nb_free = mbr.vol[vol].nb_bloc-1; int i = 0; for(i = 1; i<mbr.vol[vol].nb_bloc; i++) { struct free_bloc_s fb; if(i == mbr.vol[vol].nb_bloc-1) fb.next_free_bloc = 0; else fb.next_free_bloc = i+1; write_bloc_n(vol, i, (uchar *) &fb, sizeof(struct free_bloc_s)); } save_super(); } }
void umount() { /* save current volume super bloc */ save_super(); /* bye */ }
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; }
/* ^ * / \ * / | \ * / | \ * /___°___\ * * * Attention, il n'y a pas de verification, on peut free deux fois le même block, augmentant la taille libre et modifiant notre liste chaîné. */ void free_block(int vol, unsigned int block){ struct free_block_s fb; fb.next = super_block.first_free; write_n_block(vol, block, sizeof(struct free_block_s), (unsigned char*)&fb); super_block.first_free = block; super_block.free_size++; save_super(vol); }
unsigned int new_block(int vol){ int retValue = super_block.first_free; struct free_block_s fb; if ( super_block.free_size == 0 ){ return BLOCK_NULL; } read_n_block(vol, retValue, sizeof(struct free_block_s), (unsigned char*)&fb); super_block.first_free = fb.next; super_block.free_size--; save_super(vol); return retValue; }
void free_bloc(unsigned int bloc) { if(bloc==0){ printf("Impossible de libérer le bloc : bloc inexistant\n"); return; } struct freeBloc_s freeB; unsigned char buffer[HDA_SECTORSIZE]; freeB.freebloc_nb_free = 1; freeB.freebloc_next = current_super.super_first_free_bloc; current_super.super_first_free_bloc = bloc; current_super.super_nb_free++; memset(buffer, 0, HDA_SECTORSIZE); memcpy(buffer, &freeB, sizeof(struct freeBloc_s)); write_bloc(current_vol, bloc, buffer); save_super(); }
void init_super(unsigned int vol) { printf("Initialisation du superbloc du volume ...\n"); struct volume_s volume; struct freeBloc_s fb; unsigned char buffer[HDA_SECTORSIZE]; //load_mbr(); volume = mbr.disque[vol]; current_super.super_magic = SUPER_MAGIC; current_super.super_first_free_bloc = 1; current_super.super_nb_free = volume.size - 1; //strncpy(current_super.super_name, nom, 32); fb.freebloc_nb_free=volume.size - 1; fb.freebloc_next=0; save_super(); memset(buffer, 0, HDA_SECTORSIZE); memcpy(buffer, &fb, sizeof(struct freeBloc_s)); write_bloc(vol, 1, buffer); current_vol=vol; printf("Initialisation du superbloc du volume terminé\n"); }
unsigned int new_bloc(){ struct freeBloc_s *fb; unsigned char buffer[HDA_SECTORSIZE]; unsigned res; if(current_super.super_nb_free==0) return 0; assert(current_super.super_first_free_bloc); read_bloc(current_vol, current_super.super_first_free_bloc, buffer); fb = (struct freeBloc_s*) buffer; res = current_super.super_first_free_bloc; if(fb->freebloc_nb_free > 1){ current_super.super_nb_free --; fb->freebloc_nb_free--; write_bloc(current_vol, current_super.super_first_free_bloc, buffer); } else { current_super.super_first_free_bloc = fb->freebloc_next; } //current_super.super_first_free_bloc--; save_super(); return res; }
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; }
int main(int argc, char** argv){ int v, first_cylinder, first_sector, n_block; unsigned int i; /* init hardware */ if(init_hardware("hardware.ini") == 0) { fprintf(stderr, "Error in hardware initialization\n"); exit(EXIT_FAILURE); } /* Interreupt handlers */ for(i=0; i<16; i++) { IRQVECTOR[i] = empty_it; } /* Allows all IT */ _mask(1); if ( argc < 5 ){ printf("Usage : mkvol vol_name first_cylinder first_sector nb_block [-tother, -tannexe]\n"); return EXIT_FAILURE; } first_cylinder = atoi(argv[2]); first_sector = atoi(argv[3]); n_block = atoi(argv[4]); if ( atoi(argv[2])==0 && atoi(argv[3])==0 ){ printf("Cannot create volume at Master Boot Record position.\n"); return EXIT_FAILURE; } printf("Loading Master Boot Record...\n"); load_mbr(); if ( mbr.n_vol >= MAX_VOL ){ printf("Hard drive already has 8 volumes, please delete at least one volume before creating new one.\n"); return EXIT_FAILURE; } first_cylinder = atoi(argv[2]); check_before_create(atoi(argv[2]), atoi(argv[3]), atoi(argv[4])); v = mbr.n_vol; mbr.vol[v].first_cylinder = first_cylinder; mbr.vol[v].first_sector = first_sector; mbr.vol[v].n_block = n_block; printf("Creating volume %d...\n", v); mbr.vol[v].type = BASE; if ( argc == 5 ){ if ( strcmp(argv[4], "-tother") == 0){ mbr.vol[v].type = OTHER; } if ( strcmp(argv[4], "-tannexe") == 0){ mbr.vol[v].type = ANNEXE; } } srand((unsigned)time(NULL)); init_super(v, argv[1], rand(), n_block); mbr.n_vol++; save_mbr(); save_super(v); printf("Volume %d created.\n", v); return EXIT_SUCCESS; }