diff --git a/config.h b/config.h index e611c00..d183c48 100644 --- a/config.h +++ b/config.h @@ -5,6 +5,7 @@ #define DEBUG 1 #define LOG_LEVEL 4 #define ENABLE_FILE_LINE_IN_OTHER_PR 1 +#define ENABLE_STDOUT 1 /* Output color config section */ diff --git a/inc/color.h b/inc/color.h index 8de8611..7902582 100644 --- a/inc/color.h +++ b/inc/color.h @@ -8,10 +8,14 @@ #define COLOR_RESET "\e[0m" #define COLOR_RED "\e[0;31m" #define COLOR_YELLOW "\e[0;33m" + #define COLOR_BLUE "\e[0;34m" + #define COLOR_CYAN "\e[0;36m" #else #define COLOR_RESET "" #define COLOR_RED "" #define COLOR_YELLOW "" + #define COLOR_BLUE "" + #define COLOR_CYAN "" #endif diff --git a/inc/fs.h b/inc/fs.h index f5ef234..941f486 100644 --- a/inc/fs.h +++ b/inc/fs.h @@ -36,7 +36,7 @@ struct fs_inode_extension { }; __attribute__((packed)) -struct fs_directory_block_data { +struct fs_directory_record { unsigned char fname[60]; unsigned int inode_no; }; @@ -44,7 +44,10 @@ struct fs_directory_block_data { char *fs_get_cwd(void); int fs_create(void *d); +int fs_ln(void *); int fs_use(void *d); int fs_mkfs(void *d); +int fs_ls(void *d); +int fs_la(void *d); int fs_allow_write(void *d); int fs_prohibit_write(void *d); diff --git a/inc/print.h b/inc/print.h index 7df0b13..c232cd5 100644 --- a/inc/print.h +++ b/inc/print.h @@ -13,6 +13,13 @@ #endif +#if ENABLE_STDOUT == 1 + #define pr_stdout(...) { printf(__VA_ARGS__); } +#else + #define pr_stdout(...) {} +#endif + + #if LOG_LEVEL >= 2 #if ENABLE_FILE_LINE_IN_OTHER_PR == 1 #define pr_err(...) { \ diff --git a/src/cli.c b/src/cli.c index 5d2d606..6fc0f08 100644 --- a/src/cli.c +++ b/src/cli.c @@ -15,9 +15,12 @@ static const struct CliCommandEntry cmd[] = { // mandatory commands {"mkfs", 1, (enum CliArgType[]) {INT}, fs_mkfs}, {"create", 1, (enum CliArgType[]) {STR}, fs_create}, + {"ls", 0, NULL, fs_ls}, + {"ln", 2, (enum CliArgType[]) {STR, STR}, fs_ln}, // custom commands {"use", 1, (enum CliArgType[]) {STR}, fs_use}, + {"la", 0, NULL, fs_la}, {"prohibit-write", 0, NULL, fs_prohibit_write}, {"allow-write", 0, NULL, fs_allow_write}, {"exit", 0, NULL, ctrl_exit} diff --git a/src/fs.c b/src/fs.c index 1f6a769..72caa38 100644 --- a/src/fs.c +++ b/src/fs.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "print.h" #include "fs.h" @@ -10,6 +11,7 @@ const static int BLOCK_ADDRESSES_PER_INODE = (FS_BLOCK_SIZE-sizeof(int)*3) / sizeof(int); const static int BLOCK_ADDRESSES_PER_INODE_EXTENSION = (FS_BLOCK_SIZE-sizeof(int)) / sizeof(int); +const static int DIRECTORY_RECORDS_PER_BLOCK = FS_BLOCK_SIZE / sizeof(struct fs_directory_record); static char used_file_path[FS_MAX_DEVICE_FILE_NAME_LEN+1]; @@ -19,6 +21,7 @@ static int write_permitted; static char fs_cwd[FS_MAX_PATH_LEN+1]; static struct fs_header fsh_cache; static char fs_bitmap_cache[FS_BLOCK_SIZE * FS_MAX_BITMAP_SIZE]; +static unsigned int fs_cwd_inode_ptr; static int read_block(unsigned int block_no, void *data) @@ -136,6 +139,75 @@ static unsigned int find_free_block(void) return 0; } +static unsigned int find_free_inode_ptr(void) +{ + unsigned int i = 1; // inode0 always points to root dir, so can't be free + + // search fs_header + for ( ; i < BLOCK_ADDRESSES_PER_INODE; i++) { + if (fsh_cache.inode_ptrs[i] == 0) { + if (i < fsh_cache.max_inode_count) { + return i; + } else { + return 0; + } + } + } + + // search fs_header_extensions + struct fs_header_extension ext; + unsigned int next_extension = fsh_cache.next_extension; + + while (next_extension) { + read_block(next_extension, (void *) &ext); + next_extension = ext.next_extension; + + for (int j = 0; j < BLOCK_ADDRESSES_PER_INODE_EXTENSION; j++, i++) { + if (ext.inode_ptrs[j] == 0) { + if (i < fsh_cache.max_inode_count) { + return i; + } else { + return 0; + } + } + } + } + + if (i < fsh_cache.max_inode_count) { + return i; + } else { + return 0; + } +} + +static unsigned int read_inode_ptr(unsigned int inode_ptr) +{ + if ((inode_ptr / BLOCK_ADDRESSES_PER_INODE) == 0) { + // inode_ptr is in the fs_header + struct fs_header fsh; + int read_result = read_block(0, &fsh); + if (FS_BLOCK_SIZE != read_result) { + if (read_result < 0) { + pr_err("failed to read fs header from device '%s'\n", used_file_path); + } else { + pr_err("failed to read full header from device (read %d/%d bytes)\n", read_result, FS_BLOCK_SIZE); + } + + return 0; + } + + return fsh.inode_ptrs[inode_ptr]; + } else { + pr_err("inode_ptr %d is in fs_header_extension, reading is not implemented\n", inode_ptr); + return 0; + // TODO: find block with relevant inode_ptr + /* + unsigned int relevant_block_index = ((free_block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION) + 1; + unsigned int relevant_block_record_offset = (free_block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION; + */ + } +} + static void write_inode_ptr(unsigned int inode_ptr, unsigned int block_ptr) { if ((inode_ptr / BLOCK_ADDRESSES_PER_INODE) == 0) { @@ -167,6 +239,7 @@ static void write_inode_ptr(unsigned int inode_ptr, unsigned int block_ptr) pr("Updated inode ptr %d -> %d\n", inode_ptr, block_ptr); } else { + pr_err("inode_ptr %d is in fs_header_extension, writing is not implemented\n", inode_ptr); // TODO: find block with relevant inode_ptr, extend fs_header if needed /* unsigned int relevant_block_index = ((free_block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION) + 1; @@ -214,9 +287,314 @@ int fs_prohibit_write(void *d) return 0; } +static struct fs_directory_record *fs_read_dir(unsigned int fs_inode_ptr) +{ + unsigned int inode_block_no = read_inode_ptr(fs_inode_ptr); + struct fs_inode dir_inode; + read_block(inode_block_no, &dir_inode); + + if (dir_inode.size == 0) { + pr("directory is empty\n"); + return NULL; + } + + struct fs_directory_record *recs = malloc(fsh_cache.max_inode_count * sizeof(struct fs_directory_record)); + memset(recs, 0, fsh_cache.max_inode_count * sizeof(struct fs_directory_record)); + + // read block portion from dir_inode + for (int i = 0; (i*DIRECTORY_RECORDS_PER_BLOCK < dir_inode.size) && (i < BLOCK_ADDRESSES_PER_INODE); i++) { + if (dir_inode.blocks[i]) { + read_block(dir_inode.blocks[i], &(recs[i*DIRECTORY_RECORDS_PER_BLOCK])); + } + } + + // read block portions from inode extensions + // TODO + + return recs; +} + +static int fs_find_free_directory_record(unsigned int dir_inode_ptr) +{ + struct fs_directory_record *recs = fs_read_dir(dir_inode_ptr); + if (!recs) { + return 0; + } + + for (unsigned int i = 0; i < fsh_cache.max_inode_count; i++) { + if (!recs[i].inode_no) { + free(recs); + return i; + } + } + + free(recs); + return -1; +} + +static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int target_inode_ptr, char *fname) +{ + int new_directory_record_index = fs_find_free_directory_record(dir_inode_ptr); + if (new_directory_record_index < 0) { + pr_err("no free inode pointer found\n"); + return -1; + } + + if (new_directory_record_index < DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE) { + // record is located in base inode, writing to it + int block_no = new_directory_record_index / DIRECTORY_RECORDS_PER_BLOCK; + int block_offset = new_directory_record_index % DIRECTORY_RECORDS_PER_BLOCK; + + struct fs_inode dir_inode; + read_block(read_inode_ptr(dir_inode_ptr), (void *) &dir_inode); + + if (dir_inode.blocks[block_no] == 0) { + // allocate new block + unsigned int new_block = find_free_block(); + if (new_block == 0) { + pr_err("failed to allocate block to extend directory (inode_ptr=%d)\n", dir_inode_ptr); + return -1; + } else { + pr("Allocated new physical block %d for inode_ptr=%d (inode=%d, inner_block_no=%d)\n", + new_block, read_inode_ptr(dir_inode_ptr), block_no); + } + + char zero_data[FS_BLOCK_SIZE] = {}; + memset(zero_data, 0, FS_BLOCK_SIZE); + write_block(new_block, &zero_data); + + mark_used(new_block); + + dir_inode.blocks[block_no] = new_block; + if (dir_inode.size <= block_no * FS_BLOCK_SIZE) { + pr("Updated directory size at inode_ptr=%d: %d -> %d\n", + dir_inode_ptr, dir_inode.size, (block_no + 1) * FS_BLOCK_SIZE); + dir_inode.size = (block_no + 1) * FS_BLOCK_SIZE; + write_block(read_inode_ptr(dir_inode_ptr), (void *) &dir_inode); + } + } + + struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK]; + read_block(dir_inode.blocks[block_no], (void *) &recs); + + strcpy(recs[block_offset].fname, fname); + recs[block_offset].inode_no = target_inode_ptr; + + write_block(dir_inode.blocks[block_no], (void *) &recs); + + pr("Written new directory record #%d for file '%s' (-> %d) in inode_ptr=%d\n", + new_directory_record_index, fname, target_inode_ptr, dir_inode_ptr); + + return 0; + } else { + // record is located in inode extension + // TODO + } + + // I don't remember what was this, but this function shouldn't need it + + // write new record + //unsigned int inode_block_no = read_inode_ptr(fs_inode_ptr); +} + int fs_create(void *d) { - pr("[mock] Regular file '%s' created\n", *((char **) d)); + char *fname = *((char **) d); + int fname_len = strlen(fname); + + if (fname_len > 59) { + pr_err("filename too long (%d > %d)\n", fname_len, 59); + return 0; + } + + pr("Creating regular file '%s'\n", fname); + + int inode_block_no = find_free_block(); + if (!inode_block_no) { + pr_err("no free blocks available to save file inode\n"); + return 0; + } + + // write new file inode + struct fs_inode newf = {}; + newf.ftype = REGULAR; + newf.ref_count = 1; + newf.size = 0; + + { + int bytes_written = write_block(inode_block_no, (void *) &newf); + + if (bytes_written < 0) { + pr_err("failed to write inode block to device '%s'\n", used_file_path); + return 0; + } else if (bytes_written < FS_BLOCK_SIZE) { + pr_err("failed to write full inode block to device '%s' (written %d/%d bytes)\n", + used_file_path, bytes_written, FS_BLOCK_SIZE); + return 0; + } else { + pr("Written inode block to device '%s'\n", used_file_path); + } + + mark_used(inode_block_no); + } + + // allocate and write inode pointer for new inode + unsigned int inode_ptr_index = find_free_inode_ptr(); + if (!inode_ptr_index) { + pr_err("no free inode ptr found\n"); + return 0; + } + write_inode_ptr(inode_ptr_index, inode_block_no); + + // add filename to current directory inode + fs_add_fname_to_directory(fs_cwd_inode_ptr, inode_ptr_index, fname); + + return 0; +} + +int fs_ln(void *d) { + if (used_file_fd <= 0) { + pr_err("no storage device\n"); + return 0; + } + + char *existing_fname = ((char **) d)[0]; + char *new_fname = ((char **)d)[1]; + int new_fname_len = strlen(new_fname); + + if (new_fname_len > 59) { + pr_err("new filename too long (%d > 59)\n", new_fname_len); + return 0; + } + + pr("Making hard link '%s' -> '%s'\n", new_fname, existing_fname); + + // find original file name + unsigned int original_inode_ptr = 0; + + int dir_inode = read_inode_ptr(fs_cwd_inode_ptr); + + struct fs_inode dir; + read_block(dir_inode, (void *) &dir); + + // list entries from base inode + for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) { + struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK]; + + if (dir.blocks[i]) { + read_block(dir.blocks[i], (void *) &recs); + + for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { + if (!recs[k].inode_no) + continue; + + if (!strcmp(existing_fname, recs[k].fname)) { + original_inode_ptr = recs[k].inode_no; + goto original_inode_ptr_found; + } + } + } + } + + pr_err("no such file '%d'\n", existing_fname); + return 0; + +original_inode_ptr_found: + pr("Original inode_ptr found (%d)\n", original_inode_ptr); + + // register new filename with the same inode_ptr + if (fs_add_fname_to_directory(fs_cwd_inode_ptr, original_inode_ptr, new_fname) < 0) { + pr_err("failed to register filename in directory '%s'\n", fs_cwd_inode_ptr); + return 0; + } else { + pr("Registered new filename in directory '%s'\n", fs_cwd_inode_ptr); + } + + // update ref_count in file inode + struct fs_inode f; + read_block(read_inode_ptr(original_inode_ptr), (void *) &f); + f.ref_count++; + write_block(read_inode_ptr(original_inode_ptr), (void *) &f); + + pr("Updated inode ref_count (%d -> %d)\n", f.ref_count-1, f.ref_count); +} + +int fs_ls(void *d) { + if (used_file_fd <= 0) { + pr_err("no storage device\n"); + return 0; + } + + int dir_inode = read_inode_ptr(fs_cwd_inode_ptr); + + struct fs_inode dir; + read_block(dir_inode, (void *) &dir); + + // list entries from base inode + for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) { + struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK]; + + if (dir.blocks[i]) { + read_block(dir.blocks[i], (void *) &recs); + + for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { + if (!recs[k].inode_no) + continue; + + pr_stdout("%s\n", recs[k].fname); + } + } + } + + // list entries from inode extension + // TODO + + return 0; +} + +int fs_la(void *d) { + if (used_file_fd <= 0) { + pr_err("no storage device\n"); + return 0; + } + + int dir_inode = read_inode_ptr(fs_cwd_inode_ptr); + + struct fs_inode dir; + read_block(dir_inode, (void *) &dir); + + pr_stdout("directory size: %d\n", dir.size); + + // list entries from base inode + for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) { + struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK]; + + if (dir.blocks[i]) { + read_block(dir.blocks[i], (void *) &recs); + + for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { + if (!recs[k].inode_no) + continue; + + struct fs_inode f_inode; + read_block(read_inode_ptr(recs[k].inode_no), (void *) &f_inode); + + if (f_inode.ftype == DIRECTORY) { + pr_stdout(COLOR_BLUE "%s" COLOR_RESET + "(inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=dir)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); + } else { + pr_stdout("%s (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=reg)\n", + recs[k].fname, recs[k].inode_no, read_inode_ptr(recs[k].inode_no), + f_inode.ref_count, f_inode.size); + } + } + } + } + + // list entries from inode extension + // TODO return 0; } @@ -304,6 +682,8 @@ int fs_use(void *d) char *root_dir_path = "/"; fs_chdir((void *) &root_dir_path); + + fs_cwd_inode_ptr = 0; } return 0; @@ -439,8 +819,13 @@ finish_current_block: // inode0 -> root_dir_block write_inode_ptr(0, free_block_index); + // update fsh cache + memcpy(&fsh_cache, &fsh, FS_BLOCK_SIZE); + char *root_dir_path = "/"; fs_chdir((void *) &root_dir_path); + fs_cwd_inode_ptr = 0; + return 0; }