#include #include #include #include #include #include "superbloc.h" #include "freeblock.h" static struct superblock_desc_t cached_sblock[MAX_VOLUMES]; void init_super(unsigned int volume) { struct superblock_desc_t sblock; struct dskinfo_t info = dskinfo(); /* Charge le mbr */ struct mbr_desc_t mbr; load_mbr(&mbr); sblock.magic_key = SUPERBLOCK_MAGIC; srandom(time(NULL)); sblock.serial = (int)random() % 30000; strcpy(sblock.label, "volume"); sblock.first_inode = 0; sblock.first_free_block = 1; sblock.free_space = mbr.volume[volume].size - 1; /* Ecrire le superbloc sur le disque */ void *data = malloc(info.taille_sec); memcpy(data, &sblock, sizeof(struct superblock_desc_t)); write_bloc(volume, 0, data); /* Mettre en cache */ cached_sblock[volume] = sblock; /* Ecrire le bloc libre */ memset(data, 0, info.taille_sec); struct freeblock_t fb; fb.block_count = sblock.free_space; fb.next_block = 0; memcpy(data, &fb, sizeof(struct freeblock_t)); write_bloc(volume, 1, data); free(data); } int load_super(unsigned int volume, struct superblock_desc_t *sblock) { struct dskinfo_t info = dskinfo(); void *data; /* Charge le mbr */ struct mbr_desc_t mbr; load_mbr(&mbr); /* Si le numéro de volume est incorrect */ if ( volume < 0 || volume >= mbr.vol_count ) return SBLOCK_FAILURE; /* Si nous avons le superblock en cache */ if ( cached_sblock[volume].magic_key == SUPERBLOCK_MAGIC ) { memcpy(sblock, &cached_sblock[volume], sizeof(struct superblock_desc_t)); return SBLOCK_SUCCESS; } /* Sinon on le lit depuis le disque */ data = malloc(info.taille_sec); read_bloc(volume, 0, data); memcpy(sblock, data, sizeof(struct superblock_desc_t)); free(data); /* Si le superblock est incorrect, on en recrée un */ if ( sblock->magic_key != SUPERBLOCK_MAGIC ) { init_super(volume); memcpy(sblock, &cached_sblock[volume], sizeof(struct superblock_desc_t)); return SBLOCK_FAILURE; } /* Et on le met en cache */ memcpy(&cached_sblock[volume], sblock, sizeof(struct superblock_desc_t)); return SBLOCK_SUCCESS; } void save_super(unsigned int volume, struct superblock_desc_t *sblock) { void *data; struct dskinfo_t info = dskinfo(); data = malloc(info.taille_sec); memcpy(data, sblock, sizeof(struct superblock_desc_t)); /* Ecrire */ write_bloc(volume, 0, data); /* Mettre à jour le cache */ memcpy(&cached_sblock[volume], sblock, sizeof(struct superblock_desc_t)); free(data); }