spz-lab4/src/fs.c

1081 lines
27 KiB
C
Raw Normal View History

2025-04-23 22:51:00 +03:00
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
2025-04-23 22:51:00 +03:00
#include "print.h"
#include "fs.h"
#include "config.h"
2025-04-24 21:40:16 +03:00
const static int BLOCK_ADDRESSES_PER_INODE = (FS_BLOCK_SIZE-sizeof(int)*3) / sizeof(int);
2025-04-23 22:51:00 +03:00
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);
2025-04-23 22:51:00 +03:00
static char used_file_path[FS_MAX_DEVICE_FILE_NAME_LEN+1];
static int used_file_fd;
static int write_permitted;
2025-04-24 21:40:16 +03:00
static char fs_cwd[FS_MAX_PATH_LEN+1];
static unsigned int fs_cwd_inode_ptr;
2025-04-24 21:40:16 +03:00
static struct fs_file_description fs_file_descriptions[FS_MAX_OPEN_FD];
2025-04-23 22:51:00 +03:00
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)
{
2025-04-26 10:21:23 +03:00
if (!write_permitted) {
pr_err("write operations are prohibited\n");
return -1;
}
2025-04-23 22:51:00 +03:00
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);
}
2025-04-24 21:40:16 +03:00
static void mark_used(unsigned int block_no)
{
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
if (block_no > fsh.block_count) {
pr_err("block %d is out of filesystem block range (%d)\n", block_no, fsh.block_count);
2025-04-24 21:40:16 +03:00
return;
}
unsigned char bitmap_bit = 1 << (block_no & 0x7);
unsigned int bitmap_block_offset = (block_no >> 3) % FS_BLOCK_SIZE;
unsigned int bitmap_block_index = (block_no >> 3) / FS_BLOCK_SIZE;
2025-04-23 22:51:00 +03:00
2025-04-26 10:37:48 +03:00
unsigned char bitmap_block[FS_BLOCK_SIZE];
read_block(bitmap_block_index+1, (void *) bitmap_block);
bitmap_block[bitmap_block_offset] |= bitmap_bit;
2025-04-24 21:40:16 +03:00
// write changes to device
2025-04-26 10:37:48 +03:00
write_block(bitmap_block_index+1, (void *) &bitmap_block);
2025-04-24 21:40:16 +03:00
pr("Marked block_no=%d (block=%d, offset=%d, bit=%d) as used\n",
block_no, bitmap_block_index, bitmap_block_offset, block_no & 0x7);
}
static void mark_free(unsigned int block_no)
2025-04-23 22:51:00 +03:00
{
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
if (block_no > fsh.block_count) {
pr_err("block %d is out of fimesystem block range (%d)\n", block_no, fsh.block_count);
2025-04-24 21:40:16 +03:00
return;
2025-04-23 22:51:00 +03:00
}
2025-04-24 21:40:16 +03:00
unsigned char bitmap_bit = 1 << (block_no & 0x7);
unsigned int bitmap_block_offset = (block_no >> 3) % FS_BLOCK_SIZE;
unsigned int bitmap_block_index = (block_no >> 3) / FS_BLOCK_SIZE;
2025-04-26 10:37:48 +03:00
unsigned char bitmap_block[FS_BLOCK_SIZE];
read_block(bitmap_block_index+1, (void *) bitmap_block);
bitmap_block[bitmap_block_offset] &= ~bitmap_bit;
2025-04-24 21:40:16 +03:00
// write changes to device
2025-04-26 10:37:48 +03:00
write_block(bitmap_block_index+1, (void *) bitmap_block);
2025-04-24 21:40:16 +03:00
pr("Marked block_no=%d (block=%d, offset=%d, bit=%d) as free\n",
block_no, bitmap_block_index, bitmap_block_offset, bitmap_bit);
}
static int identify_fs(void)
{
struct fs_header read_buf;
2025-04-23 22:51:00 +03:00
{
2025-04-24 21:40:16 +03:00
int read_amount = read_block(0, (void *) &read_buf);
2025-04-23 22:51:00 +03:00
if (read_amount < 0) {
2025-04-24 21:40:16 +03:00
pr_err("failed to read fs_header from storage device '%s'\n", used_file_path);
2025-04-23 22:51:00 +03:00
return 0;
}
2025-04-24 21:40:16 +03:00
if (read_amount < FS_BLOCK_SIZE) {
pr_warn("failed to read full block (read %d/%d bytes)\n", read_amount, FS_BLOCK_SIZE);
} else if (read_amount == 0) {
2025-04-23 22:51:00 +03:00
pr_err("storage device size is 0\n");
return 0;
}
}
2025-04-24 21:40:16 +03:00
if (read_buf.next_extension) {
pr_info("identified filesystem version 0x%hhx with %d max inodes (on %d blocks), next header extension is at block 0x%x\n",
read_buf.version, read_buf.max_inode_count, read_buf.block_count, read_buf.next_extension);
} else {
pr_info("identified filesystem version 0x%hhx with %d max inodes (on %d blocks), with no header extensions\n",
read_buf.version, read_buf.max_inode_count, read_buf.block_count);
}
return read_buf.version;
}
static unsigned int find_free_block(void)
{
unsigned int b = 0;
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
int blocks_used_for_bitmap = fsh.block_count / (FS_BLOCK_SIZE * 8);
if (fsh.block_count % (FS_BLOCK_SIZE * 8))
2025-04-24 21:40:16 +03:00
blocks_used_for_bitmap++;
for (int i = 0; i < blocks_used_for_bitmap; i++) {
2025-04-26 10:37:48 +03:00
unsigned char bitmap_block[FS_BLOCK_SIZE];
read_block(i+1, (void *) bitmap_block);
2025-04-24 21:40:16 +03:00
for (int j = 0; j < FS_BLOCK_SIZE; j++) {
2025-04-26 10:37:48 +03:00
if (!(~(bitmap_block[j]))) {
2025-04-24 21:40:16 +03:00
b += 8;
} else {
for (int k = 0; k < 8; k++, b++) {
2025-04-26 10:37:48 +03:00
if (!((bitmap_block[j]) & (1 << k))) {
2025-04-24 21:40:16 +03:00
return b;
}
}
}
}
}
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
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
// search fs_header
for ( ; i < BLOCK_ADDRESSES_PER_INODE; i++) {
2025-04-26 10:37:48 +03:00
if (fsh.inode_ptrs[i] == 0) {
if (i < fsh.max_inode_count) {
return i;
} else {
return 0;
}
}
}
// search fs_header_extensions
struct fs_header_extension ext;
2025-04-26 10:37:48 +03:00
unsigned int next_extension = fsh.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) {
2025-04-26 10:37:48 +03:00
if (i < fsh.max_inode_count) {
return i;
} else {
return 0;
}
}
}
}
2025-04-26 10:37:48 +03:00
if (i < fsh.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;
*/
}
}
2025-04-24 21:40:16 +03:00
static void write_inode_ptr(unsigned int inode_ptr, unsigned int block_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;
}
fsh.inode_ptrs[inode_ptr] = block_ptr;
int write_result = write_block(0, (void *) &fsh);
if (FS_BLOCK_SIZE != write_result) {
if (write_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", write_result, FS_BLOCK_SIZE);
}
return;
}
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);
2025-04-24 21:40:16 +03:00
// 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;
unsigned int relevant_block_record_offset = (free_block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
*/
}
}
char *fs_get_cwd(void)
{
return fs_cwd;
}
int fs_chdir(void *d)
{
memset(fs_cwd, 0, sizeof(fs_cwd));
strcpy(fs_cwd, *((char**)d));
return 0;
2025-04-23 22:51:00 +03:00
}
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;
2025-04-24 21:40:16 +03:00
return 0;
2025-04-23 22:51:00 +03:00
}
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;
2025-04-24 21:40:16 +03:00
return 0;
2025-04-23 22:51:00 +03:00
}
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;
}
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
struct fs_directory_record *recs = malloc(fsh.max_inode_count * sizeof(struct fs_directory_record));
memset(recs, 0, fsh.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;
}
2025-04-26 10:37:48 +03:00
struct fs_header fsh;
read_block(0, (void *) &fsh);
for (unsigned int i = 0; i < fsh.max_inode_count; i++) {
if (!recs[i].inode_no) {
free(recs);
return i;
}
}
free(recs);
return -1;
}
2025-04-26 10:21:23 +03:00
static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
{
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(fname, recs[k].fname))
continue;
// filename found
int *r = malloc(sizeof(int) * 3);
2025-04-26 10:21:23 +03:00
r[0] = i;
r[1] = k;
r[2] = recs[k].inode_no;
2025-04-26 10:21:23 +03:00
return r;
}
}
}
return NULL;
}
static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int target_inode_ptr, char *fname)
{
2025-04-26 10:21:23 +03:00
{
// check if duplicate filename exists in specified directory
int *r = find_filename_in_directory(dir_inode_ptr, fname);
if (r) {
free(r);
pr_err("filename '%s' already exists\n", fname);
return 0;
}
}
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",
2025-04-26 10:21:23 +03:00
new_block, dir_inode_ptr, 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);
2025-04-26 10:21:23 +03:00
pr("Written new directory record #%d for file '%s' (-> inode_ptr=%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
}
2025-04-26 10:21:23 +03:00
}
static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname)
{
// find directory record with this fname
int dir_inode = read_inode_ptr(fs_cwd_inode_ptr);
2025-04-26 10:21:23 +03:00
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])
continue;
2025-04-26 10:21:23 +03:00
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(fname, recs[k].fname))
continue;
2025-04-26 10:21:23 +03:00
pr("Directory record for '%s' found in block=%d, record=%d, removing\n", fname, i, k);
unsigned int inode_location_cache = read_inode_ptr(recs[k].inode_no);
// decrement ref_count
struct fs_inode f;
read_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
f.ref_count--;
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
// if it drops to zero, nullify inode_ptr pointing to this inode
if (!f.ref_count) {
pr("ref_count=0, clearing inode_ptr\n");
write_inode_ptr(recs[k].inode_no, 0);
// if no fd reference this inode, clean it up altogether
int i;
for (i = 0; i < FS_MAX_OPEN_FD; i++)
if (fs_file_descriptions[i].inode == inode_location_cache)
break;
if (i == FS_MAX_OPEN_FD) {
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
2025-04-26 10:21:23 +03:00
struct fs_inode f;
read_block(inode_location_cache, (void *) &f);
// clear blocks referenced in base inode
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
if (f.blocks[i])
mark_free(f.blocks[i]);
2025-04-26 10:21:23 +03:00
}
mark_free(inode_location_cache);
2025-04-26 10:21:23 +03:00
// clear blocks referenced in inode extensions
struct fs_inode_extension ext;
unsigned int next_extension = f.next_extension;
2025-04-26 10:21:23 +03:00
while (next_extension) {
mark_free(next_extension);
read_block(next_extension, (void *) &ext);
next_extension = ext.next_extension;
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
if (ext.blocks[i])
mark_free(ext.blocks[i]);
}
}
} else {
pr("Inode %d is still referenced by fd %d, not removing it\n",
inode_location_cache, i);
2025-04-26 10:21:23 +03:00
}
}
// clear directory record inode_ptr
recs[k].inode_no = 0;
write_block(dir.blocks[i], (void *) &recs);
goto fs_remove_fname_from_directory_finish;
2025-04-26 10:21:23 +03:00
}
}
// list entries from inode extension
// TODO
pr_err("no such file '%s'\n", fname);
return -1;
fs_remove_fname_from_directory_finish:
pr("Removed fname from directory record successfully\n");
return 0;
}
int fs_open(void *d)
{
char *fname = *((char **) d);
// find file inode
struct fs_file_description fd;
{
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
if (!r) {
pr_err("no such file: '%s'\n", fname);
return 0;
}
fd.inode = read_inode_ptr(r[2]);
free(r);
}
fd.rw_offset = 0;
// find free file descriptor
int free_fd;
for (free_fd = 0; (free_fd < FS_MAX_OPEN_FD) && (fs_file_descriptions[free_fd].inode); free_fd++);
if (free_fd == FS_MAX_OPEN_FD) {
pr_err("no free file descriptor found\n");
return 0;
}
memcpy(&(fs_file_descriptions[free_fd]), &fd, sizeof(struct fs_file_description));
pr_stdout("%d\n", free_fd);
return 0;
}
int fs_close(void *d)
{
int fd = *((int *) d);
// remove inode number from fd
if (!fs_file_descriptions[fd].inode) {
pr_err("fd %d is not open\n", fd);
return 0;
}
unsigned int inode_location_cache = fs_file_descriptions[fd].inode;
fs_file_descriptions[fd].inode = 0;
pr("fd %d closed\n", fd);
// cleanup file data on disk if ref_count=0
// and no other open descriptor references it's inode
struct fs_inode f;
read_block(inode_location_cache, (void *) &f);
if (f.ref_count)
return 0;
for (int i = 0; i < FS_MAX_OPEN_FD; i++)
if (fs_file_descriptions[i].inode == inode_location_cache)
return 0;
// if ended up here, the inode is not referenced anywhere
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
// clear blocks referenced in base inode
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
if (f.blocks[i])
mark_free(f.blocks[i]);
}
mark_free(inode_location_cache);
// clear blocks referenced in inode extensions
struct fs_inode_extension ext;
unsigned int next_extension = f.next_extension;
while (next_extension) {
mark_free(next_extension);
read_block(next_extension, (void *) &ext);
next_extension = ext.next_extension;
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
if (ext.blocks[i])
mark_free(ext.blocks[i]);
}
}
}
2025-04-23 22:51:00 +03:00
int fs_create(void *d)
{
2025-04-26 10:21:23 +03:00
if (!write_permitted) {
pr_err("write operations are prohibited\n");
return 0;
}
char *fname = *((char **) d);
int fname_len = strlen(fname);
2025-04-26 10:21:23 +03:00
{
// check if duplicate filename exists in current directory
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname);
if (r) {
free(r);
pr_err("filename '%s' already exists\n", fname);
return 0;
}
}
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;
}
2025-04-26 10:21:23 +03:00
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);
2025-04-26 10:21:23 +03:00
{
// check if duplicate filename exists in current directory
int *r = find_filename_in_directory(fs_cwd_inode_ptr, new_fname);
if (r) {
free(r);
pr_err("filename '%s' already exists\n", new_fname);
return 0;
}
}
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;
}
}
}
}
2025-04-26 10:21:23 +03:00
// list entries from inode extensions
// TODO
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);
}
2025-04-26 10:21:23 +03:00
int fs_rm(void *d)
{
if (!write_permitted) {
pr_err("device '%s' is write-protected\n", used_file_path);
return 0;
}
char *fname = *((char **) d);
if (fs_remove_fname_from_directory(fs_cwd_inode_ptr, fname) < 0) {
pr_err("failed to unlink '%s'\n", fname);
} else {
pr("Unlinked '%s'\n", fname);
}
return 0;
}
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;
}
2025-04-26 10:21:23 +03:00
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
2025-04-23 22:51:00 +03:00
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;
}
2025-04-24 21:40:16 +03:00
if (write_permitted) {
char *root_dir_path = "/";
fs_chdir((void *) &root_dir_path);
fs_cwd_inode_ptr = 0;
2025-04-24 21:40:16 +03:00
}
2025-04-23 22:51:00 +03:00
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);
2025-04-24 21:40:16 +03:00
if (max_inode_count <= 0) {
pr_err("max inode count must be positive (got %d)\n", max_inode_count);
return 0;
}
2025-04-23 22:51:00 +03:00
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;
}
int blocks_used = 1 + blocks_used_for_bitmap;
2025-04-24 21:40:16 +03:00
unsigned char bitmap_block[FS_BLOCK_SIZE];
unsigned int j = 0;
for (int i = 0; i < blocks_used_for_bitmap; i++) {
memset(bitmap_block, 0, FS_BLOCK_SIZE);
for (int k = 0; k < FS_BLOCK_SIZE; k++) {
for (int t = 0; t < 8; t++, j++) {
if (j == blocks_used)
goto finish_current_block;
bitmap_block[i] |= (1 << t);
}
}
finish_current_block:
{
int bytes_written = write_block(i+1, (void *) bitmap_block);
if (bytes_written < 0) {
pr_err("failed to write bitmap block %d/%d on device '%s'\n",
i+1, blocks_used_for_bitmap, used_file_path);
return 0;
} else if (bytes_written < FS_BLOCK_SIZE) {
pr_err("failed to write full bitmap block %d/%d on device '%s' (written %d/%d bytes)\n",
i+1, blocks_used_for_bitmap, used_file_path, bytes_written, FS_BLOCK_SIZE);
return 0;
} else {
pr("Written bitmap block %d/%d on device '%s'\n", i+1, blocks_used_for_bitmap, used_file_path);
}
}
}
// create root directory automatically
struct fs_inode root_dir = {};
root_dir.ftype = DIRECTORY;
root_dir.ref_count = 1;
unsigned int free_block_index = find_free_block();
if (!free_block_index) {
pr_err("failed to find free block for root directory\n");
return 0;
}
{
int bytes_written = write_block(free_block_index, (void *) &root_dir);
if (bytes_written < 0) {
pr_err("failed to write root directory block on device '%s'\n", used_file_path);
return 0;
} else if (bytes_written < FS_BLOCK_SIZE) {
pr_err("failed to write full root directory block on device '%s' (written %d/%d bytes)\n",
used_file_path, bytes_written, FS_BLOCK_SIZE);
return 0;
} else {
pr("Written root directory block on device '%s'\n", used_file_path);
}
}
mark_used(free_block_index);
// inode0 -> root_dir_block
write_inode_ptr(0, free_block_index);
char *root_dir_path = "/";
fs_chdir((void *) &root_dir_path);
2025-04-23 22:51:00 +03:00
fs_cwd_inode_ptr = 0;
2025-04-23 22:51:00 +03:00
return 0;
}