#include #include #include #include #include "print.h" #include "fs.h" #include "config.h" const static int BLOCK_ADDRESSES_PER_INODE = (FS_BLOCK_SIZE-sizeof(int)*4) / sizeof(int); const static int BLOCK_ADDRESSES_PER_INODE_EXTENSION = (FS_BLOCK_SIZE-sizeof(int)) / sizeof(int); static char used_file_path[FS_MAX_DEVICE_FILE_NAME_LEN+1]; static int used_file_fd; static int write_permitted; static int read_block(unsigned int block_no, void *data) { if (lseek(used_file_fd, block_no * FS_BLOCK_SIZE, SEEK_SET) < 0) { pr_err("failed to seek to block %d (bs=%d) on device '%s'\n", block_no, FS_BLOCK_SIZE, used_file_path); return -1; } return read(used_file_fd, data, FS_BLOCK_SIZE); } static int write_block(unsigned int block_no, void *data) { if (lseek(used_file_fd, block_no * FS_BLOCK_SIZE, SEEK_SET) < 0) { pr_err("failed to seek to block %d (bs=%d) on device '%s'\n", block_no, FS_BLOCK_SIZE, used_file_path); return -1; } return write(used_file_fd, data, FS_BLOCK_SIZE); } static int identify_fs(void) { if (lseek(used_file_fd, 0, SEEK_SET) < 0) { pr_err("failed to seek '%s' to 0\n", used_file_path); return 0; } unsigned char read_buf[FS_BLOCK_SIZE]; { int read_amount = read(used_file_fd, read_buf, FS_BLOCK_SIZE); if (read_amount < 0) { pr_err("failed to read from storage device\n"); return 0; } if (read_amount < FS_BLOCK_SIZE) pr_warn("failed to read full first block of %d bytes\n", FS_BLOCK_SIZE); if (read_amount == 0) { pr_err("storage device size is 0\n"); return 0; } } return read_buf[0]; } int fs_allow_write(void *d) { if (used_file_fd <= 0) { pr_err("no device present\n"); return 0; } pr_info("Allowing write operations on device '%s'\n", used_file_path); write_permitted = 1; } int fs_prohibit_write(void *d) { if (used_file_fd <= 0) { pr_err("no device present\n"); return 0; } pr_info("Prohibiting write operations on device '%s'\n", used_file_path); write_permitted = 0; } int fs_create(void *d) { pr("[mock] Regular file '%s' created\n", *((char **) d)); return 0; } int fs_use(void *d) { char *fname = *((char **) d); int name_len = strlen(fname); if (name_len > FS_MAX_DEVICE_FILE_NAME_LEN) { pr_err("device filename too long (> %d)\n", FS_MAX_DEVICE_FILE_NAME_LEN); return 0; } pr("Using file '%s' as storage device\n", fname); strcpy(used_file_path, fname); if (used_file_fd > 0) close(used_file_fd); used_file_fd = open(fname, O_RDWR); if (used_file_fd < 0) { pr_err("failed to open filename '%s'\n", fname); return 0; } int fs_version = identify_fs(); if (!fs_version) { pr_info("filesystem could not be identified on device '%s'\n", fname); write_permitted = 0; } else if (fs_version != 1) { pr_warn("filesystem is corrupted or has unsupported version (0x%hhx)\n", fs_version); write_permitted = 0; } else if (fs_version == 1) { pr_info("filesystem v1 has been identified on device '%s'\n", fname); write_permitted = 1; } return 0; } int fs_mkfs(void *d) { if (!write_permitted) { pr_err("device '%s' is write-protected\n", used_file_path); return 0; } if (used_file_fd <= 0) { pr_err("storage device not present\n"); return 0; } int max_inode_count = *((int *)d); struct stat st; if (fstat(used_file_fd, &st) < 0) { pr_err("could not stat device '%s'\n", used_file_path); return 0; } int block_count = st.st_size / FS_BLOCK_SIZE; int blocks_used_for_bitmap = block_count / (FS_BLOCK_SIZE * 8); if (block_count % (FS_BLOCK_SIZE * 8)) blocks_used_for_bitmap++; if (blocks_used_for_bitmap > FS_MAX_BITMAP_SIZE) { pr_err("memory bitmap is too large (%d blocks > %d)\n", blocks_used_for_bitmap, FS_MAX_BITMAP_SIZE); return 0; } pr("Formatting storage device '%s' of size %d (total_block_count = %d, bitmap_blocks = %d) with %d allowed inode pointers\n", used_file_path, st.st_size, block_count, blocks_used_for_bitmap, max_inode_count); struct fs_header fsh = {}; fsh.version = 0x1; fsh.max_inode_count = max_inode_count; fsh.block_count = block_count; pr("header size is %d bytes, writing it to the first block\n", sizeof(fsh)); int result = write_block(0, (void *) &fsh); if (FS_BLOCK_SIZE != result) { if (result < 0) { pr_err("failed to write fs header to device '%s'\n", used_file_path); } else { pr_err("failed to write full block to device, written %d/%d bytes\n", result, FS_BLOCK_SIZE); } return 0; } unsigned char bitmap[blocks_used_for_bitmap * FS_BLOCK_SIZE]; memset(bitmap, 0, blocks_used_for_bitmap * FS_BLOCK_SIZE); int blocks_used = 1 + blocks_used_for_bitmap; //for (int i = 0; i < FS_MAX_BITMAP_SIZE; i++) return 0; }