#ifndef LSFS_DISK_CONTROLLER_H #define LSFS_DISK_CONTROLLER_H #include #include #include #include #include #include #include #include #include "lsfs_string.h" typedef struct Directory_Table Directory_Table; typedef struct struct_table_entry struct_table_entry; typedef struct struct_partition_control partition_control; typedef struct struct_partition_control partition_control; typedef struct File_System_Control_Information FSCI; typedef struct meta_information_format mif; typedef struct tag_record tag_record; typedef struct lsfs_file lsfs_file; typedef enum Table_Entry_Kind Table_Entry_Kind; typedef uint64_t lsfs_sector_offset; typedef lsfs_sector_offset lsfs_file_id; //typedef uint64_t sector_index; static FILE* disk; static partition_control p_control; int create_file_system(); int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind); int lsfs_disk_getattr(lsfs_file* find_file, const char *path); int lsfs_disk_delete_file(lsfs_file_id file_id); int get_free_sectors_table(); int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array); int lsfs_disk_read_data_from_file(lsfs_file* file, int buffer_size, void* buffer_for_data); int lsfs_disk_write_data_to_file(lsfs_file* file, int data_length, char *data); int lsfs_disk_rename_file(lsfs_file* file, const char* new_filename); int lsfs_disk_load_disk(); int write_data_to_disk(lsfs_sector_offset at_sector, uint32_t file_block_size, void* data_to_write); int read_data_from_disk(lsfs_sector_offset index, uint32_t file_block_size, void* data_buffer); int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset); int save_modified_file_information(lsfs_file* file); #define SECTOR_SIZE 512 #define NUMBER_OF_MBR_PARTITIONS 4 #define DEFAULT_FILE_SIZE 4 // This is in sectors #define DEFAULT_TABLE_SIZE 10 #define NUM_DATA_POINTERS 28 typedef enum Table_Entry_Kind { // These are specific values since, is has to corrospond to the implementation in assembly ENTRY_EMPTY = 0, ENTRY_FILE = 1, ENTRY_DIRECTORY = 2, } Table_Entry_Kind; typedef struct Partition_Entry { uint8_t active_falg; // This has value 0x80 if it is a bootable partition / it is an active partition. uint8_t CHS_start_addr[3]; // [0] = H, [1] = S, [2] = C uint8_t partition_type; // This has a value such that one can idenfity which file system the partition is. uint8_t CHS_last_addr[3]; // [0] = H, [1] = S, [2] = C uint32_t LBA_abs_first_sector; uint32_t number_of_sectors; } __attribute__((packed)) Partition_Entry; typedef struct Master_Boot_record { uint8_t offset_on_disk[446]; // The code for the bootloader Partition_Entry partitions[4]; uint8_t master_tag_records[2]; // Signature } __attribute__((packed)) Master_Boot_record; typedef struct struct_table_entry { char filename[256]; lsfs_file_id file_id; uint64_t file_size; mif* ext_file_data; uint32_t file_block_size; // This tells how many block there are allocated for the specific file. eg. we read this amount of bloks for the file. uint8_t entry_kind; uint8_t extra_control_bits1; uint8_t extra_control_bits2; uint8_t extra_control_bits3; lsfs_sector_offset data_pointer[NUM_DATA_POINTERS]; // if it is a directory, the first pointer will be to the next table. } __attribute__((packed)) table_entry; typedef struct Directory_Table { struct_table_entry entries[10]; } __attribute__((packed)) Directory_Table; typedef struct File_System_Control_Information { char filesyste_information[256]; uint64_t master_table_index; uint64_t this_partition_offset_on_disk; uint64_t next_free_sector; uint64_t next_uniqe_id; // both files and directories gets this. uint64_t next_sector_reuse_pointer; uint64_t last_sector_index_on_partition; uint64_t maximum_sectors_on_disk; uint64_t sectors_size_on_disk; uint64_t not_used[24]; } __attribute__((packed)) FSCI; typedef struct struct_partition_control { FSCI fsci; Directory_Table master_table; } __attribute__((packed)) partition_control; typedef struct meta_information_format { char filename[246]; // remeber that the 246 bytes has to be a /0 terminator.. uint32_t owner_id; lsfs_file_id tags[32]; uint64_t file_size; uint32_t control_bits; /* not pressent - Permission key table 64 bytes sha-265 pr. key*/ uint64_t creation_date; uint64_t last_modification_data; uint64_t last_access_date; /* * 256 first pointers in direct mapping to data * 94 next pointers is a pointer * 94 next pointers to pointers to data */ lsfs_sector_offset one_level_pointer_data[NUM_DATA_POINTERS]; lsfs_sector_offset two_level_pointer_data[94]; lsfs_sector_offset three_level_pointer_data[94]; } __attribute__((packed)) mif; typedef struct tag_record { /* SIZE 16 bytes */ lsfs_file_id mif_record; struct { uint64_t is_filename : 1; } control_bits; } __attribute__((packed)) tag_record; typedef struct lsfs_file { lsfs_file_id file_id; lsfs_sector_offset table_entry_pointer; Table_Entry_Kind entry_kind; char* filename; uint32_t owner_id; uint64_t size; uint64_t creation_date; uint64_t access_time; uint64_t modification_time; uint32_t file_block_size; lsfs_sector_offset *data; } lsfs_file; int lsfs_disk_getattr(lsfs_file* find_file, const char* path) { int i = 0; int found = 0; while((p_control.master_table.entries[i].filename[0]) != 0 && !found) { if(strcmp( (path + 1 ), p_control.master_table.entries[i].filename ) == 0) { time_t current_time; time ( ¤t_time ); find_file->file_id = p_control.master_table.entries[i].file_id; find_file->entry_kind = p_control.master_table.entries[i].entry_kind; find_file->table_entry_pointer = i; find_file->filename = p_control.master_table.entries[i].filename; find_file->owner_id = getuid(); find_file->size = p_control.master_table.entries[i].file_size; // p_control.master_table.entries[i].data_pointer[0]; //; find_file->creation_date = (uint64_t) current_time; find_file->access_time = (uint64_t) current_time; find_file->modification_time = (uint64_t) current_time; find_file->data = p_control.master_table.entries[i].data_pointer; find_file->file_block_size = 1; // TODO: should be loaded from disk. found = 1; } i++; } return found; } int lsfs_disk_read_data_from_file(lsfs_file *file, int buffer_size, void* buffer_for_data) { // TODO some offset, to tell where in the file we want to write int return_val = 0; for (int i = 0; i < NUM_DATA_POINTERS; ++i) { if(file->data[i] == 0) { break; } return_val += read_data_from_disk(file->data[i], file->file_block_size, buffer_for_data + (SECTOR_SIZE * i)); } //time_t current_time; //time ( ¤t_time ); //mif_record->last_access_date = (uint64_t) current_time; //write_data_to_disk(file_id, mif_record); //free(mif_record); return return_val; } static inline time_t lsfs_disk_update_timestamps(lsfs_file *file) { return file->modification_time = file->access_time = time(NULL); } #define lsfs_num_sectors_for_size(x) (((x)+SECTOR_SIZE-1)&~(SECTOR_SIZE-1)) int lsfs_disk_write_data_to_file(lsfs_file *file, int data_length, char *data) { int written; int amount_written = data_length; lsfs_sector_offset current_sector = file->size / SECTOR_SIZE; unsigned int offset_in_sector = file->size % SECTOR_SIZE; char *tmp_buffer = calloc(file->file_block_size, SECTOR_SIZE); assert(tmp_buffer); read_data_from_disk(file->data[current_sector], file->file_block_size, tmp_buffer); memcpy(tmp_buffer + offset_in_sector, data, SECTOR_SIZE-offset_in_sector); data_length -= SECTOR_SIZE-offset_in_sector; if (data_length < 0) { data_length = 0; } for (;;) { assert(current_sector <= NUM_DATA_POINTERS); written = written + write_data_to_disk(p_control.master_table.entries[file->file_id].data_pointer[current_sector], 4, tmp_buffer); if (data_length <= 0) break; data += SECTOR_SIZE; if (data_length >= SECTOR_SIZE) { memcpy(tmp_buffer, data, SECTOR_SIZE); data_length -= SECTOR_SIZE; } else { memset(tmp_buffer, 0, SECTOR_SIZE); memcpy(tmp_buffer, data, data_length); data_length = 0; } } amount_written -= data_length; free(tmp_buffer); //lsfs_disk_update_timestamps(&mif_record); file->size += amount_written; // update file size save_modified_file_information(file); //write_data_to_disk(file->file_id, 4, &p_control.master_table[file->file_id]); return amount_written; } time_t lsfs_disk_truncate_file(lsfs_file *file, off_t offset) { //mif file_mif; //read_data_from_disk(file_id, &file_mif); time_t result = lsfs_disk_update_timestamps(file); file->size = (int) offset; // p_control.master_table.entries[i].data_pointer[0]; //; save_modified_file_information(file); //write_data_to_disk(file->file_id, 4, NULL); return result; } int lsfs_disk_rename_file(lsfs_file* file, const char* new_filename) { memset(file->filename, 0, 256); sprintf(file->filename, "%s", new_filename); time_t current_time; time ( ¤t_time ); file->access_time = (uint64_t) current_time; file->modification_time = (uint64_t) current_time; save_modified_file_information(file); return 1; } int lsfs_disk_delete_file(lsfs_file_id file_id) { mif* mif_record = calloc(1, SECTOR_SIZE); read_data_from_disk(file_id, 1, mif_record); // TODO Delete/free all data sectors. // Delete/free the mif record sector. free(mif_record); return 1; } int get_free_sectors_table() { // We need DEFAULT_TABLE_SIZE sectors straight contigious for a table // Otherwise the file system cannot make a new table. // We return the offset where the table is starting. // If we cannot assing DEFAULT_TABLE_SIZE sectors, we report errror. int return_index = p_control.fsci.next_free_sector; if ((p_control.fsci.next_free_sector + DEFAULT_TABLE_SIZE) > p_control.fsci.last_sector_index_on_partition) { // We don't have space, report error return -EINVAL; } p_control.fsci.next_free_sector += DEFAULT_TABLE_SIZE; fseek ( disk , (p_control.fsci.this_partition_offset_on_disk) * SECTOR_SIZE, SEEK_SET ); fwrite(&p_control.fsci, 1, SECTOR_SIZE, disk); return return_index; } int get_free_sectors(int num_sectors_needed, lsfs_sector_offset* output_array) { if ((p_control.fsci.next_free_sector + num_sectors_needed) > p_control.fsci.last_sector_index_on_partition ) { // We cannot assign what we want. return -EINVAL; } for (int i = 0; i < num_sectors_needed; ++i) { output_array[i] = p_control.fsci.next_free_sector; p_control.fsci.next_free_sector += DEFAULT_FILE_SIZE; } fseek ( disk , (p_control.fsci.this_partition_offset_on_disk) * SECTOR_SIZE, SEEK_SET ); fwrite(&p_control.fsci, 1, SECTOR_SIZE, disk); return 0; } int create_file_system() { //char* sector_to_write; // make default File System Control information (FSCI) // first integer says how many pointers we got // to master tag tables // Second and forward is the pointers to the master Tag Tables // we need the first number to allocate memory at one go. int* zero_buffer; FSCI fsci; fsci.this_partition_offset_on_disk = 1; //fsci.maximum_sectors_on_partition = 1048576; // Max 4GiB fsci.next_free_sector = 257; fseek ( disk , 0, SEEK_SET ); fwrite(&fsci, 1, sizeof(fsci), disk); zero_buffer = calloc(1, (4096 - sizeof(fsci))); fwrite(zero_buffer, 1, (4096 - sizeof(fsci)), disk); free(zero_buffer); /* MASTER TAG TABLE */ table_entry master_table[DEFAULT_TABLE_SIZE]; memset(master_table, 0, (DEFAULT_TABLE_SIZE * sizeof(table_entry))); fwrite(&master_table, 1, sizeof(master_table), disk); zero_buffer = calloc(1, 16); fwrite(zero_buffer, 1, 16, disk); free(zero_buffer); return 0; } int lsfs_disk_load_disk() { // Find the partition talbe: // This makes is BIOS dependent. // UEFI is not supported. Master_Boot_record mbr; fseek( disk , 0 * SECTOR_SIZE, SEEK_SET ); fread(&mbr, 1, sizeof(mbr), disk); for (int i = 0; i < NUMBER_OF_MBR_PARTITIONS; ++i) { // TODO (Jørn) We maybe wnat to optimize, such that we can detect if we have more than one partition opn the system. if (mbr.partitions[i].partition_type == 0x18) { printf("%d\n", mbr.partitions[i].LBA_abs_first_sector); // First we find the File system control information. fseek ( disk , mbr.partitions[i].LBA_abs_first_sector * SECTOR_SIZE, SEEK_SET ); fread(&p_control.fsci, 1, SECTOR_SIZE , disk); //printf("next free sector: %d\n", p_control.fsci.next_free_sector); //printf("next free ID: %d\n", p_control.fsci.next_uniqe_id); // next we find the Mater Table. fseek ( disk , (mbr.partitions[i].LBA_abs_first_sector + 1) * SECTOR_SIZE, SEEK_SET ); fread(&p_control.master_table, 1, DEFAULT_TABLE_SIZE * SECTOR_SIZE , disk); return 1; } } return 0; } int lsfs_disk_create_entry(const char* path, Table_Entry_Kind entry_kind) { lsfs_string_array split_path = lsfs_string_split_c(path, '/', false); lsfs_string filename = split_path.strings[split_path.length-1]; // Start from the master table int free_index = -1; // -1 is no index found. Directory_Table *dir_table = &p_control.master_table; lsfs_sector_offset table_disk_position = p_control.fsci.master_table_index; for (int i = 0; i < split_path.length; ++i) { for (int j = 0; j < DEFAULT_TABLE_SIZE; ++j) { if (i == (split_path.length - 1)) { // Find free index and be sure that there dosent exist a file with the same name. if (dir_table->entries[j].entry_kind == ENTRY_EMPTY) { // Set the free index, continue to see if the filename exist. // if not -1, we have found a better index. if (free_index == -1) { printf("Index found for file: %d\n", j); free_index = j; } } else if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) { // Abort mission, we have a file with the same name. return -EINVAL; } } else { if (strcmp(dir_table->entries[j].filename, split_path.strings[0].chars) == 0) { // We have found the next directory to traverse. ; } } } } if (free_index == -1) { // The table is full, and we cannot create an entry return -EINVAL; } // Find the entry for the file in the table structure: dir_table->entries[free_index].file_id = p_control.fsci.next_uniqe_id; p_control.fsci.next_uniqe_id++; sprintf(dir_table->entries[free_index].filename, "%s", filename.chars); dir_table->entries[free_index].entry_kind = entry_kind; if (entry_kind == ENTRY_DIRECTORY) { dir_table->entries[free_index].data_pointer[0] = get_free_sectors_table(); dir_table->entries[free_index].file_size = 5120; } else if (entry_kind == ENTRY_FILE) { // We assign one data pointer consiting of DEFAULT_FILE_SIZE sectors get_free_sectors_table(1, dir_table->entries[free_index].data_pointer); } else { return -EINVAL; } /* find_file->creation_date = (uint64_t) current_time; find_file->access_time = (uint64_t) current_time; find_file->modification_time = (uint64_t) current_time; find_file->data = p_control.master_table.entries[i].data_pointer; find_file->owner_id = getuid(); new_file_data.owner_id = getuid(); time_t current_time; time ( ¤t_time ); */ fseek ( disk , (table_disk_position + free_index) * SECTOR_SIZE, SEEK_SET ); fwrite(&dir_table->entries[free_index], 1, SECTOR_SIZE, disk); return 0; } int save_modified_file_information(lsfs_file* file) { // Write the file struct into the table_entry, such that we can save the data correct. p_control.master_table.entries[file->table_entry_pointer].file_id = file->file_id; memcpy(p_control.master_table.entries[file->table_entry_pointer].filename, file->filename, 256); p_control.master_table.entries[file->table_entry_pointer].file_size = file->size; // p_control.master_table.entries[i].data_pointer[0]; //; //p_control.master_table.entries[i].data_pointer = find_file->data; write_data_to_disk(file->table_entry_pointer, file->file_block_size, &p_control.master_table.entries[file->table_entry_pointer]); return 0; } int write_data_to_disk(lsfs_sector_offset index, uint32_t file_block_size, void* data_to_write) { fseek ( disk, (index * SECTOR_SIZE), SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here. int written = fwrite(data_to_write, 1, (4 * SECTOR_SIZE), disk); return written; } int write_data_to_disk_off(lsfs_sector_offset index, void* data_to_write, int offset) { fseek ( disk, (index * SECTOR_SIZE) + offset, SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here. int written = fwrite(data_to_write, 1, (4 * SECTOR_SIZE), disk); return written; } int read_data_from_disk(lsfs_sector_offset index, uint32_t file_block_size, void* data_buffer) { fseek ( disk, (index * SECTOR_SIZE ), SEEK_SET ); // SEEK_SET start offset at index 0 and move 1 * SECTOR_SIZE, and write here. int read = fread(data_buffer, 1, (file_block_size * SECTOR_SIZE), disk); return read; } #endif