2025-04-23 22:51:00 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2025-04-25 21:41:08 +03:00
|
|
|
#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);
|
2025-04-25 21:41:08 +03:00
|
|
|
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];
|
2025-04-25 21:41:08 +03:00
|
|
|
static unsigned int fs_cwd_inode_ptr;
|
2025-04-24 21:40:16 +03:00
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
static struct fs_file_description fs_file_descriptions[FS_MAX_OPEN_FD];
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
static int extract_basename(char *path, char *name);
|
|
|
|
|
2025-04-23 22:51:00 +03:00
|
|
|
|
|
|
|
static int read_block(unsigned int block_no, void *data)
|
|
|
|
{
|
2025-04-30 21:39:27 +03:00
|
|
|
// failsafe
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(used_file_fd, &st) < 0) {
|
|
|
|
pr_err("could not stat device '%s'\n", used_file_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block_no * FS_BLOCK_SIZE >= st.st_size) {
|
2025-05-17 13:53:32 +03:00
|
|
|
pr_err("read beyond device address space denied (%d >= %ld)\n",
|
2025-04-30 21:39:27 +03:00
|
|
|
block_no * FS_BLOCK_SIZE, st.st_size);
|
|
|
|
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 read(used_file_fd, data, FS_BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_block(unsigned int block_no, void *data)
|
|
|
|
{
|
2025-04-30 21:39:27 +03:00
|
|
|
// failsafe
|
|
|
|
struct stat st;
|
|
|
|
if (fstat(used_file_fd, &st) < 0) {
|
|
|
|
pr_err("could not stat device '%s'\n", used_file_path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block_no * FS_BLOCK_SIZE >= st.st_size) {
|
2025-05-17 13:53:32 +03:00
|
|
|
pr_err("write beyond device address space denied (%d >= %ld)\n",
|
2025-04-30 21:39:27 +03:00
|
|
|
block_no * FS_BLOCK_SIZE, st.st_size);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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) {
|
2025-04-30 22:33:23 +03:00
|
|
|
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;
|
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",
|
2025-05-01 00:29:11 +03:00
|
|
|
block_no, bitmap_block_index, bitmap_block_offset, block_no & 0x7);
|
2025-04-24 21:40:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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);
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
// 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) {
|
2025-04-25 21:41:08 +03:00
|
|
|
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;
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
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) {
|
2025-04-25 21:41:08 +03:00
|
|
|
return i;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-26 10:37:48 +03:00
|
|
|
if (i < fsh.max_inode_count) {
|
2025-04-25 21:41:08 +03:00
|
|
|
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 {
|
2025-04-30 21:39:27 +03:00
|
|
|
// inode_ptr is in fs_header_extension
|
|
|
|
int extension_no = (inode_ptr - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
int extension_offset = (inode_ptr - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fsh.next_extension)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned int next_ext = fsh.next_extension;
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
struct fs_header_extension ext;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < extension_no + 1; i++) {
|
|
|
|
if (!next_ext)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext.inode_ptrs[extension_offset];
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2025-04-30 21:39:27 +03:00
|
|
|
// inode_ptr is in fs_header_extension
|
|
|
|
int extension_no = (inode_ptr - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
int extension_offset = (inode_ptr - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case: if header has no extensions, create one
|
|
|
|
// if it was not needed, we would not have ended here in the first place
|
|
|
|
if (!fsh.next_extension) {
|
|
|
|
unsigned int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to allocate block for fs_header_extension\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
struct fs_header_extension ext;
|
|
|
|
memset(&ext, 0, sizeof(struct fs_header_extension));
|
|
|
|
write_block(new_block, (void *) &ext);
|
|
|
|
|
|
|
|
fsh.next_extension = new_block;
|
|
|
|
write_block(0, (void *) &fsh);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int next_ext = fsh.next_extension;
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
struct fs_header_extension ext;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < extension_no + 1; i++) {
|
|
|
|
if (!next_ext) {
|
|
|
|
unsigned int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to allocate block for fs_header_extension\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
struct fs_header_extension new_ext;
|
|
|
|
memset(&new_ext, 0, sizeof(struct fs_header_extension));
|
|
|
|
write_block(new_block, (void *) &new_ext);
|
|
|
|
|
|
|
|
ext.next_extension = new_block;
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
|
|
|
next_ext = new_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
ext.inode_ptrs[extension_offset] = block_ptr;
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
|
|
|
pr("Updated inode ptr %d -> %d\n", inode_ptr, block_ptr);
|
2025-04-24 21:40:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
2025-04-25 21:41:08 +03:00
|
|
|
{
|
2025-04-26 10:37:48 +03:00
|
|
|
struct fs_header fsh;
|
|
|
|
read_block(0, (void *) &fsh);
|
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
struct fs_inode dir;
|
|
|
|
read_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
int found_block_no = 0;
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
// search in base inode
|
|
|
|
for (unsigned int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
|
|
|
struct fs_directory_record r[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
if (!dir.blocks[i]) {
|
|
|
|
if (found_block_no < fsh.max_inode_count)
|
2025-04-30 21:39:27 +03:00
|
|
|
return found_block_no;
|
2025-04-26 16:05:40 +03:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
read_block(dir.blocks[i], (void *) &r);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (r[j].fname[0])
|
2025-04-26 16:05:40 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (found_block_no >= fsh.max_inode_count)
|
|
|
|
return -1;
|
2025-04-26 10:37:48 +03:00
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
return found_block_no;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
// search in inode extensions
|
2025-04-30 21:39:27 +03:00
|
|
|
struct fs_inode_extension ext;
|
2025-04-26 16:05:40 +03:00
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
struct fs_directory_record r[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
if (!ext.blocks[i]) {
|
|
|
|
if (found_block_no < fsh.max_inode_count)
|
|
|
|
return found_block_no;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_block(ext.blocks[i], (void *) &r);
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (r[j].fname[0])
|
2025-04-30 21:39:27 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (found_block_no >= fsh.max_inode_count)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return found_block_no;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_block_no >= fsh.max_inode_count)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return found_block_no;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
2025-04-26 10:21:23 +03:00
|
|
|
static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
{
|
2025-05-16 12:22:31 +03:00
|
|
|
int dir_inode = read_inode_ptr(dir_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]) {
|
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-26 10:21:23 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// filename found
|
2025-04-26 14:41:43 +03:00
|
|
|
int *r = malloc(sizeof(int) * 3);
|
2025-04-26 10:21:23 +03:00
|
|
|
r[0] = i;
|
|
|
|
r[1] = k;
|
2025-04-26 14:41:43 +03:00
|
|
|
r[2] = recs[k].inode_no;
|
2025-04-26 10:21:23 +03:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
// list entries from inode extensions
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
if (ext.blocks[i]) {
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-05-01 22:03:32 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// filename found
|
|
|
|
int *r = malloc(sizeof(int) * 3);
|
|
|
|
r[0] = i;
|
|
|
|
r[1] = k;
|
|
|
|
r[2] = recs[k].inode_no;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-26 10:21:23 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
static int find_fname_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
{
|
|
|
|
int *r = find_filename_in_directory(dir_inode_ptr, fname);
|
|
|
|
int res;
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (r) {
|
2025-05-16 12:22:31 +03:00
|
|
|
res = r[2];
|
2025-05-16 14:32:44 +03:00
|
|
|
free(r);
|
|
|
|
} else {
|
2025-05-16 12:22:31 +03:00
|
|
|
res = -1;
|
2025-05-16 14:32:44 +03:00
|
|
|
}
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
static int inode_is_referenced(unsigned int inode)
|
|
|
|
{
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(inode, (void *) &f);
|
|
|
|
|
|
|
|
if (f.ref_count)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < FS_MAX_OPEN_FD; i++)
|
|
|
|
if (fs_file_descriptions[i].inode == inode)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clean_inode(unsigned int inode)
|
|
|
|
{
|
|
|
|
pr("Removing all data related to inode=%d\n", inode);
|
|
|
|
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(inode, (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]);
|
|
|
|
}
|
|
|
|
mark_free(inode);
|
|
|
|
|
|
|
|
// 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-25 21:41:08 +03:00
|
|
|
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);
|
2025-05-16 12:22:31 +03:00
|
|
|
return -1;
|
2025-04-26 10:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
int new_directory_record_index = fs_find_free_directory_record(dir_inode_ptr);
|
|
|
|
if (new_directory_record_index < 0) {
|
2025-04-30 21:39:27 +03:00
|
|
|
pr_err("no free directory record found\n");
|
2025-04-25 21:41:08 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
struct fs_inode dir;
|
|
|
|
read_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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;
|
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
if (dir.blocks[block_no] == 0) {
|
2025-04-25 21:41:08 +03:00
|
|
|
// 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);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
char zero_data[FS_BLOCK_SIZE] = {0};
|
2025-04-25 21:41:08 +03:00
|
|
|
memset(zero_data, 0, FS_BLOCK_SIZE);
|
|
|
|
write_block(new_block, &zero_data);
|
|
|
|
|
|
|
|
mark_used(new_block);
|
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
dir.blocks[block_no] = new_block;
|
|
|
|
if (dir.size <= block_no * FS_BLOCK_SIZE) {
|
2025-04-25 21:41:08 +03:00
|
|
|
pr("Updated directory size at inode_ptr=%d: %d -> %d\n",
|
2025-04-30 21:39:27 +03:00
|
|
|
dir_inode_ptr, dir.size, (block_no + 1) * FS_BLOCK_SIZE);
|
|
|
|
dir.size = (block_no + 1) * FS_BLOCK_SIZE;
|
|
|
|
write_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-30 21:39:27 +03:00
|
|
|
read_block(dir.blocks[block_no], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
strcpy(recs[block_offset].fname, fname);
|
|
|
|
recs[block_offset].inode_no = target_inode_ptr;
|
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
write_block(dir.blocks[block_no], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
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",
|
2025-04-25 21:41:08 +03:00
|
|
|
new_directory_record_index, fname, target_inode_ptr, dir_inode_ptr);
|
|
|
|
} else {
|
|
|
|
// record is located in inode extension
|
2025-04-30 21:39:27 +03:00
|
|
|
unsigned int extension_no = (new_directory_record_index - (DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE))
|
|
|
|
/ (DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE_EXTENSION);
|
|
|
|
unsigned int extension_block_index = (new_directory_record_index - (DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE))
|
|
|
|
% (DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE_EXTENSION);
|
|
|
|
unsigned int extension_block_offset = (new_directory_record_index - (DIRECTORY_RECORDS_PER_BLOCK * BLOCK_ADDRESSES_PER_INODE))
|
|
|
|
% DIRECTORY_RECORDS_PER_BLOCK;
|
|
|
|
|
|
|
|
// seek to next extension
|
|
|
|
// special case
|
|
|
|
if (!dir.next_extension) {
|
|
|
|
unsigned int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to allocate block for fs_inode_extension\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
write_block(new_block, (void *) &ext);
|
|
|
|
|
|
|
|
dir.next_extension = new_block;
|
|
|
|
write_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < extension_no + 1; i++) {
|
|
|
|
if (!next_ext) {
|
|
|
|
unsigned int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to allocate block for fs_inode_extension\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
struct fs_inode_extension new_ext;
|
|
|
|
memset(&new_ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
write_block(new_block, (void *) &new_ext);
|
|
|
|
|
|
|
|
ext.next_extension = new_block;
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
|
|
|
next_ext = new_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_directory_record r[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
if (!ext.blocks[extension_block_index]) {
|
|
|
|
unsigned int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to allocate block for file data\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
ext.blocks[extension_block_index] = new_block;
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
|
|
|
memset(&r, 0, sizeof(r));
|
|
|
|
|
|
|
|
unsigned int dir_size = BLOCK_ADDRESSES_PER_INODE * FS_BLOCK_SIZE
|
|
|
|
+ extension_no * BLOCK_ADDRESSES_PER_INODE_EXTENSION * FS_BLOCK_SIZE
|
|
|
|
+ (extension_block_index + 1) * FS_BLOCK_SIZE;
|
|
|
|
if (dir.size < dir_size) {
|
|
|
|
dir.size = dir_size;
|
|
|
|
write_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
read_block(ext.blocks[extension_block_index], (void *) &r);
|
|
|
|
}
|
|
|
|
|
|
|
|
r[extension_block_offset].inode_no = target_inode_ptr;
|
|
|
|
strcpy(r[extension_block_offset].fname, fname);
|
|
|
|
write_block(ext.blocks[extension_block_index], (void *) &r);
|
|
|
|
|
|
|
|
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);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
2025-04-30 21:39:27 +03:00
|
|
|
|
|
|
|
return 0;
|
2025-04-26 10:21:23 +03:00
|
|
|
}
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
static void fs_remove_fname_from_directory_removal(struct fs_directory_record *recs, unsigned int k, unsigned int dir_block_addr)
|
|
|
|
{
|
|
|
|
unsigned int inode_ptr_cache = recs[k].inode_no;
|
|
|
|
unsigned int inode_location_cache = read_inode_ptr(recs[k].inode_no);
|
|
|
|
|
|
|
|
// decrement ref_count
|
|
|
|
struct fs_inode f;
|
2025-05-17 12:59:07 +03:00
|
|
|
read_block(read_inode_ptr(recs[k].inode_no), &f);
|
2025-05-01 22:03:32 +03:00
|
|
|
f.ref_count--;
|
2025-05-17 12:59:07 +03:00
|
|
|
write_block(read_inode_ptr(recs[k].inode_no), &f);
|
2025-05-01 22:03:32 +03:00
|
|
|
|
|
|
|
// clear directory record inode_ptr
|
2025-05-16 12:22:31 +03:00
|
|
|
recs[k].fname[0] = 0;
|
2025-05-17 12:59:07 +03:00
|
|
|
write_block(dir_block_addr, recs);
|
2025-05-01 22:03:32 +03:00
|
|
|
|
|
|
|
// if it drops to zero, nullify inode_ptr pointing to this inode
|
|
|
|
if (f.ref_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pr("ref_count=0, clearing inode_ptr\n");
|
|
|
|
write_inode_ptr(inode_ptr_cache, 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("Inode %d is still referenced by fd %d, not removing it\n",
|
|
|
|
inode_location_cache, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
|
|
|
|
|
|
|
|
clean_inode(inode_location_cache);
|
|
|
|
}
|
|
|
|
|
2025-04-26 10:21:23 +03:00
|
|
|
static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
{
|
|
|
|
struct fs_inode dir;
|
2025-05-17 12:59:07 +03:00
|
|
|
read_block(read_inode_ptr(dir_inode_ptr), &dir);
|
2025-04-26 10:21:23 +03:00
|
|
|
|
|
|
|
// list entries from base inode
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
if (!dir.blocks[i])
|
|
|
|
continue;
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
read_block(dir.blocks[i], &recs);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-26 14:41:43 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
continue;
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
pr("Directory record for '%s' found in block=%d, record=%d, removing\n", fname, i, k);
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
fs_remove_fname_from_directory_removal(recs, k, dir.blocks[i]);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
}
|
|
|
|
}
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
// list entries from inode extension
|
|
|
|
struct fs_inode_extension ext;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
unsigned int curr_ext = 0;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
unsigned int ext_no = 0;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
while (next_ext) {
|
|
|
|
ext_no++;
|
|
|
|
curr_ext = next_ext;
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
if (!ext.blocks[i])
|
|
|
|
continue;
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-05-01 22:03:32 +03:00
|
|
|
continue;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
if (strcmp(fname, recs[k].fname))
|
|
|
|
continue;
|
2025-04-26 15:28:17 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
pr("Directory record for '%s' found in ext=%d, block=%d, record=%d, removing\n",
|
|
|
|
fname, ext_no, i, k);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
fs_remove_fname_from_directory_removal(recs, k, ext.blocks[i]);
|
|
|
|
|
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
}
|
2025-04-26 10:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
static void resolve_path(struct resolved_path *rp, char *path, unsigned int params)
|
2025-05-16 12:22:31 +03:00
|
|
|
{
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
rp->target_inode_ptr = -1;
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
if (!path_len) {
|
|
|
|
pr_err("no path specified\n");
|
2025-05-16 14:32:44 +03:00
|
|
|
return;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
if (!strcmp(path, "/")) {
|
|
|
|
rp->parent_inode_ptr = 0;
|
|
|
|
rp->target_inode_ptr = 0;
|
|
|
|
strcpy(rp->parent_fname, "/");
|
|
|
|
strcpy(rp->target_fname, "/");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
char current_fname[FS_MAX_FNAME_LEN+1] = {0};
|
2025-05-17 12:59:07 +03:00
|
|
|
int current_inode_ptr;
|
2025-05-16 12:22:31 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (path[0] == '/') {
|
2025-05-17 12:59:07 +03:00
|
|
|
current_inode_ptr = 0;
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = 0;
|
2025-05-17 12:59:07 +03:00
|
|
|
strcpy(current_fname, "/");
|
2025-05-16 12:22:31 +03:00
|
|
|
i = 1;
|
|
|
|
} else {
|
2025-05-17 12:59:07 +03:00
|
|
|
current_inode_ptr = fs_cwd_inode_ptr;
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = fs_cwd_inode_ptr;
|
2025-05-17 12:59:07 +03:00
|
|
|
extract_basename(fs_cwd, current_fname);
|
2025-05-16 12:22:31 +03:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
char next_fname[FS_MAX_FNAME_LEN+1] = {0};
|
2025-05-17 12:59:07 +03:00
|
|
|
int next_fname_ptr = 0;
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
for ( ; i < path_len; i++) {
|
|
|
|
if (path[i] != '/') {
|
2025-05-17 12:59:07 +03:00
|
|
|
next_fname[next_fname_ptr] = path[i];
|
|
|
|
next_fname_ptr++;
|
2025-05-16 12:22:31 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// path[i] == '/', trying to change current directory
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
if (!next_fname_ptr)
|
2025-05-16 12:22:31 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i+1 == path_len)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
{
|
2025-05-17 12:59:07 +03:00
|
|
|
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
if (res < 0) {
|
2025-05-17 12:59:07 +03:00
|
|
|
pr_warn("directory '%s' does not exist\n", next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
break;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct fs_inode d;
|
2025-05-16 14:32:44 +03:00
|
|
|
read_block(read_inode_ptr(res), &d);
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
if (d.ftype == REGULAR) {
|
2025-05-17 12:59:07 +03:00
|
|
|
pr_warn("'%s' is a regular file\n", next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = -1;
|
|
|
|
break;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
pr("Found directory '%s'\n", next_fname);
|
|
|
|
current_inode_ptr = res;
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->parent_inode_ptr = res;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
next_fname_ptr = 0;
|
|
|
|
strcpy(current_fname, next_fname);
|
|
|
|
memset(next_fname, 0, FS_MAX_FNAME_LEN);
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (rp->parent_inode_ptr < 0) {
|
|
|
|
rp->target_inode_ptr = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
strcpy(rp->parent_fname, current_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
|
|
|
|
{
|
2025-05-17 12:59:07 +03:00
|
|
|
int res = find_fname_in_directory(current_inode_ptr, next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
if (res < 0) {
|
2025-05-17 12:59:07 +03:00
|
|
|
pr_warn("target fname '%s' does not exist\n", next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
rp->target_inode_ptr = -1;
|
|
|
|
} else {
|
|
|
|
rp->target_inode_ptr = res;
|
2025-05-17 12:59:07 +03:00
|
|
|
strcpy(rp->target_fname, next_fname);
|
2025-05-16 14:32:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
static int find_free_fd(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < FS_MAX_OPEN_FD; i++)
|
|
|
|
if (!fs_file_descriptions[i].inode)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
int fs_open(void *d)
|
|
|
|
{
|
2025-05-17 16:49:39 +03:00
|
|
|
char *path = *((char **) d);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, path, FOLLOW_LAST_SYMLINK);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("no such file: '%s'\n", path);
|
|
|
|
return 0;
|
2025-04-26 14:41:43 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
int free_fd = find_free_fd();
|
|
|
|
if (free_fd < 0) {
|
2025-04-26 14:41:43 +03:00
|
|
|
pr_err("no free file descriptor found\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-17 17:14:06 +03:00
|
|
|
{
|
|
|
|
struct fs_inode i;
|
|
|
|
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
|
|
|
|
|
|
|
|
if (i.ftype == DIRECTORY) {
|
|
|
|
pr_err("can't open '%s': file is a directory\n", rp.target_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_file_descriptions[free_fd].inode = read_inode_ptr(rp.target_inode_ptr);
|
2025-05-17 16:49:39 +03:00
|
|
|
fs_file_descriptions[free_fd].rw_offset = 0;
|
2025-04-26 14:41:43 +03:00
|
|
|
|
|
|
|
pr_stdout("%d\n", free_fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 19:03:39 +03:00
|
|
|
int fs_seek(void *d)
|
|
|
|
{
|
|
|
|
int fd = ((int *) d)[0];
|
|
|
|
int offset = ((int *) d)[1];
|
|
|
|
|
|
|
|
if (!fs_file_descriptions[fd].inode) {
|
|
|
|
pr_err("fd %d is not open\n", fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
|
|
|
|
if (offset > f.size) {
|
|
|
|
pr_err("offset is larger than file (%d > %d)\n", offset, f.size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr("Moving rw_offset of fd %d: %d -> %d\n",
|
|
|
|
fd, fs_file_descriptions[fd].rw_offset, offset);
|
|
|
|
fs_file_descriptions[fd].rw_offset = offset;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_fd_block(unsigned int fd, unsigned int block_index, unsigned char *buf)
|
|
|
|
{
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
|
|
|
|
if (block_index < BLOCK_ADDRESSES_PER_INODE) {
|
|
|
|
if (f.blocks[block_index])
|
|
|
|
read_block(f.blocks[block_index], (void *) buf);
|
|
|
|
else
|
|
|
|
memset(buf, 0, FS_BLOCK_SIZE);
|
|
|
|
} else {
|
2025-04-30 22:46:35 +03:00
|
|
|
/*
|
|
|
|
* TODO: rewrite this potentially broken abomination normally
|
|
|
|
* it has not caused issues so far, but you can never be too sure
|
|
|
|
*/
|
|
|
|
|
2025-04-26 19:03:39 +03:00
|
|
|
int ext_no = (block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
int ext_offset = (block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
|
|
|
|
unsigned int target_extension_address;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// seek to target extension address
|
|
|
|
for (
|
|
|
|
target_extension_address = f.next_extension, i = 0;
|
|
|
|
(i < ext_no) && target_extension_address;
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
read_block(target_extension_address, (void *) &ext);
|
|
|
|
target_extension_address = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target_extension_address) {
|
|
|
|
memset(buf, 0, FS_BLOCK_SIZE);
|
|
|
|
} else {
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
read_block(target_extension_address, (void *) &ext);
|
|
|
|
|
|
|
|
if (!ext.blocks[ext_offset])
|
|
|
|
memset(buf, 0, FS_BLOCK_SIZE);
|
|
|
|
else
|
|
|
|
read_block(ext.blocks[ext_offset], (void *) buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned char *buf)
|
|
|
|
{
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
|
|
|
|
if (block_index < BLOCK_ADDRESSES_PER_INODE) {
|
|
|
|
if (!f.blocks[block_index]) {
|
|
|
|
int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to write to fd=%d block=%d: can't allocate block for writing file data\n",
|
|
|
|
fd, block_index);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
f.blocks[block_index] = new_block;
|
|
|
|
write_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
}
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
return write_block(f.blocks[block_index], (void *) buf);
|
2025-04-26 19:03:39 +03:00
|
|
|
} else {
|
|
|
|
int ext_no = (block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
int ext_offset = (block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
|
|
|
|
// treat switch from base inode to inode extension as special case
|
|
|
|
if (!f.next_extension) {
|
|
|
|
int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n",
|
|
|
|
fd, block_index);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
2025-04-30 22:33:23 +03:00
|
|
|
write_block(new_block, (void *) &ext);
|
2025-04-26 19:03:39 +03:00
|
|
|
|
|
|
|
f.next_extension = new_block;
|
|
|
|
write_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
}
|
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
unsigned int next_ext = f.next_extension;
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
2025-04-26 19:03:39 +03:00
|
|
|
|
|
|
|
// seek to target extension address
|
2025-04-30 22:46:35 +03:00
|
|
|
for (unsigned int i = 0; i < ext_no + 1; i++) {
|
|
|
|
if (!next_ext) {
|
2025-04-26 19:03:39 +03:00
|
|
|
int new_block = find_free_block();
|
|
|
|
if (!new_block) {
|
|
|
|
pr_err("failed to write to fd=%d block=%d: can't allocate block for inode extension\n",
|
|
|
|
fd, block_index);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_block);
|
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
ext.next_extension = new_block;
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
2025-04-26 19:03:39 +03:00
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
struct fs_inode_extension new_ext;
|
|
|
|
memset(&new_ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
write_block(new_block, (void *) &new_ext);
|
2025-04-30 21:39:27 +03:00
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
next_ext = new_block;
|
2025-04-26 19:03:39 +03:00
|
|
|
}
|
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
curr_ext = next_ext;
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
2025-04-26 19:03:39 +03:00
|
|
|
}
|
|
|
|
|
2025-04-30 22:46:35 +03:00
|
|
|
if (!ext.blocks[ext_offset]) {
|
2025-04-26 19:03:39 +03:00
|
|
|
int new_data_block = find_free_block();
|
|
|
|
if (!new_data_block) {
|
|
|
|
pr_err("failed to write to fd=%d block=%d: can't allocate block for data\n",
|
|
|
|
fd, block_index);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mark_used(new_data_block);
|
|
|
|
|
|
|
|
ext.blocks[ext_offset] = new_data_block;
|
2025-04-30 22:46:35 +03:00
|
|
|
write_block(curr_ext, (void *) &ext);
|
2025-04-26 19:03:39 +03:00
|
|
|
}
|
2025-04-30 22:46:35 +03:00
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
return write_block(ext.blocks[ext_offset], (void *) buf);
|
2025-04-26 19:03:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_read(void *d)
|
|
|
|
{
|
|
|
|
int fd = ((int *) d)[0];
|
|
|
|
int amount = ((int *) d)[1];
|
|
|
|
|
|
|
|
if (!fs_file_descriptions[fd].inode) {
|
|
|
|
pr_err("fd %d is not open\n", fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (amount <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
struct fs_inode f;
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
|
|
|
|
if (fs_file_descriptions[fd].rw_offset + amount > f.size) {
|
|
|
|
pr_err("can not read outside of a file (offset %d + amount %d > f.size %d)\n",
|
|
|
|
fs_file_descriptions[fd].rw_offset, amount, f.size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr("Reading %d bytes from fd %d (offset %d)\n",
|
|
|
|
amount, fd, fs_file_descriptions[fd].rw_offset);
|
|
|
|
|
|
|
|
unsigned char *read_data = malloc(amount);
|
|
|
|
unsigned int total_read_data_amount = 0;
|
|
|
|
|
|
|
|
// read from first block
|
|
|
|
unsigned char block_buffer[FS_BLOCK_SIZE];
|
|
|
|
|
|
|
|
int block_index = fs_file_descriptions[fd].rw_offset / FS_BLOCK_SIZE;
|
|
|
|
int block_offset = fs_file_descriptions[fd].rw_offset % FS_BLOCK_SIZE;
|
|
|
|
|
|
|
|
read_fd_block(fd, block_index, block_buffer);
|
|
|
|
|
|
|
|
if (block_offset + amount <= FS_BLOCK_SIZE) {
|
|
|
|
memcpy(read_data, &(block_buffer[block_offset]), amount);
|
|
|
|
goto print_read_data;
|
|
|
|
} else {
|
|
|
|
memcpy(read_data, &(block_buffer[block_offset]), FS_BLOCK_SIZE - block_offset);
|
|
|
|
total_read_data_amount += FS_BLOCK_SIZE - block_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read from all next blocks
|
|
|
|
while (amount - total_read_data_amount > 0) {
|
|
|
|
block_index++;
|
|
|
|
read_fd_block(fd, block_index, block_buffer);
|
|
|
|
int bytes_to_read = (amount - total_read_data_amount > FS_BLOCK_SIZE)
|
|
|
|
? FS_BLOCK_SIZE : amount - total_read_data_amount;
|
|
|
|
|
|
|
|
memcpy(&(read_data[total_read_data_amount]), block_buffer, bytes_to_read);
|
|
|
|
total_read_data_amount += bytes_to_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_read_data:
|
|
|
|
pr("Updating fd %d offset: %d -> %d\n",
|
|
|
|
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset+amount);
|
|
|
|
fs_file_descriptions[fd].rw_offset += amount;
|
|
|
|
|
|
|
|
write_stdout(read_data, amount);
|
|
|
|
pr_stdout("\n");
|
|
|
|
|
|
|
|
free(read_data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_write(void *d)
|
|
|
|
{
|
|
|
|
int fd = ((int *) d)[0];
|
2025-04-26 19:46:34 +03:00
|
|
|
char *str = *((char **) ((char *) d+4));
|
2025-04-26 19:03:39 +03:00
|
|
|
int str_len = strlen(str);
|
|
|
|
|
|
|
|
if (!fs_file_descriptions[fd].inode) {
|
|
|
|
pr_err("fd %d is not open\n", fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str_len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int block_index = fs_file_descriptions[fd].rw_offset / FS_BLOCK_SIZE;
|
|
|
|
int block_offset = fs_file_descriptions[fd].rw_offset % FS_BLOCK_SIZE;
|
|
|
|
|
2025-04-30 22:33:23 +03:00
|
|
|
unsigned int total_bytes_written = 0;
|
|
|
|
|
2025-04-26 19:03:39 +03:00
|
|
|
unsigned char data_buffer[FS_BLOCK_SIZE];
|
|
|
|
|
2025-04-30 22:33:23 +03:00
|
|
|
// first block
|
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
|
|
|
{
|
|
|
|
unsigned int copy_amount = ((FS_BLOCK_SIZE - block_offset) <= str_len)
|
|
|
|
? FS_BLOCK_SIZE - block_offset
|
|
|
|
: str_len;
|
|
|
|
memcpy(&(data_buffer[block_offset]), str, copy_amount);
|
|
|
|
total_bytes_written += copy_amount;
|
|
|
|
}
|
|
|
|
write_fd_block(fd, block_index, data_buffer);
|
|
|
|
block_index++;
|
|
|
|
|
|
|
|
// full middle blocks
|
|
|
|
for ( ; str_len - total_bytes_written >= FS_BLOCK_SIZE; block_index++) {
|
|
|
|
write_fd_block(fd, block_index, (unsigned char *) &(str[total_bytes_written]));
|
|
|
|
total_bytes_written += FS_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// last partial block
|
|
|
|
if (str_len - total_bytes_written) {
|
2025-04-26 19:03:39 +03:00
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
2025-04-30 22:33:23 +03:00
|
|
|
memcpy(data_buffer, &(str[total_bytes_written]), str_len - total_bytes_written);
|
2025-04-26 19:03:39 +03:00
|
|
|
write_fd_block(fd, block_index, data_buffer);
|
|
|
|
|
2025-04-30 22:33:23 +03:00
|
|
|
total_bytes_written += (str_len - total_bytes_written);
|
2025-04-26 19:03:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pr("Moving fd %d offset: %d -> %d\n",
|
2025-04-30 22:33:23 +03:00
|
|
|
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + total_bytes_written);
|
|
|
|
fs_file_descriptions[fd].rw_offset += total_bytes_written;
|
2025-04-26 19:03:39 +03:00
|
|
|
|
2025-04-26 19:46:34 +03:00
|
|
|
struct fs_inode f;
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
|
|
|
|
if (fs_file_descriptions[fd].rw_offset > f.size) {
|
2025-04-30 21:39:27 +03:00
|
|
|
pr("Increasing fd %d file size: %d -> %d\n",
|
|
|
|
fd, f.size, fs_file_descriptions[fd].rw_offset);
|
2025-04-26 19:46:34 +03:00
|
|
|
f.size = fs_file_descriptions[fd].rw_offset;
|
|
|
|
write_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
}
|
|
|
|
|
2025-04-26 19:03:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 22:12:26 +03:00
|
|
|
int fs_truncate(void *d)
|
|
|
|
{
|
2025-05-17 16:49:39 +03:00
|
|
|
char *path = *((char **) d);
|
2025-04-26 22:12:26 +03:00
|
|
|
int size = *((int *) ((char **) d+1));
|
|
|
|
|
|
|
|
if (size < 0) {
|
|
|
|
pr_err("file size can not be negative\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, path, FOLLOW_LAST_SYMLINK);
|
2025-04-26 22:12:26 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("no such file: '%s'\n", path);
|
|
|
|
return 0;
|
2025-04-26 22:12:26 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
struct fs_inode inode;
|
|
|
|
read_block(read_inode_ptr(rp.target_inode_ptr), &inode);
|
2025-04-26 22:12:26 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (size > inode.size) {
|
|
|
|
pr("Increasing file size of '%s': %d -> %d\n", rp.target_fname, inode.size, size);
|
|
|
|
inode.size = size;
|
|
|
|
write_block(read_inode_ptr(rp.target_inode_ptr), &inode);
|
2025-04-26 22:12:26 +03:00
|
|
|
} else {
|
2025-05-17 16:49:39 +03:00
|
|
|
pr("Decreasing file size of '%s': %d -> %d\n", rp.target_fname, inode.size, size);
|
|
|
|
inode.size = size;
|
2025-04-26 22:12:26 +03:00
|
|
|
|
|
|
|
// cleanup
|
2025-05-17 16:49:39 +03:00
|
|
|
int new_block_amount = inode.size / FS_BLOCK_SIZE;
|
|
|
|
if (inode.size % FS_BLOCK_SIZE)
|
2025-04-26 22:12:26 +03:00
|
|
|
new_block_amount++;
|
|
|
|
|
|
|
|
int blocks_seen = 0;
|
|
|
|
|
|
|
|
// look through base inode blocks
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++, blocks_seen++) {
|
2025-05-17 16:49:39 +03:00
|
|
|
if ((blocks_seen >= new_block_amount) && (inode.blocks[i])) {
|
|
|
|
mark_free(inode.blocks[i]);
|
|
|
|
inode.blocks[i] = 0;
|
2025-04-26 22:12:26 +03:00
|
|
|
}
|
|
|
|
}
|
2025-05-17 16:49:39 +03:00
|
|
|
write_block(read_inode_ptr(rp.target_inode_ptr), &inode);
|
2025-04-26 22:12:26 +03:00
|
|
|
|
|
|
|
// look through inode extension blocks
|
|
|
|
struct fs_inode_extension ext;
|
2025-05-17 16:49:39 +03:00
|
|
|
unsigned int next_ext = inode.next_extension;
|
2025-04-26 22:12:26 +03:00
|
|
|
unsigned int curr_ext;
|
|
|
|
|
|
|
|
while (next_ext) {
|
2025-05-17 16:49:39 +03:00
|
|
|
read_block(next_ext, &ext);
|
2025-04-26 22:12:26 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++, blocks_seen++) {
|
2025-05-01 22:40:24 +03:00
|
|
|
if ((blocks_seen >= new_block_amount) && (ext.blocks[i])) {
|
2025-04-26 22:12:26 +03:00
|
|
|
mark_free(ext.blocks[i]);
|
|
|
|
ext.blocks[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
write_block(next_ext, &ext);
|
2025-04-26 22:12:26 +03:00
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
// look through inode extensions themselves
|
2025-05-17 16:49:39 +03:00
|
|
|
int required_blocks_after_base_inode = (int) inode.size - (int) BLOCK_ADDRESSES_PER_INODE;
|
2025-05-01 22:40:24 +03:00
|
|
|
int required_extensions = required_blocks_after_base_inode >= 0
|
|
|
|
? required_blocks_after_base_inode / BLOCK_ADDRESSES_PER_INODE_EXTENSION
|
|
|
|
: 0;
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if ((inode.size - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION)
|
2025-04-26 22:12:26 +03:00
|
|
|
required_extensions++;
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
next_ext = inode.next_extension;
|
2025-04-26 22:12:26 +03:00
|
|
|
|
|
|
|
if (!required_extensions) {
|
|
|
|
// zero base inode next_extension ptr
|
2025-05-17 16:49:39 +03:00
|
|
|
inode.next_extension = 0;
|
|
|
|
write_block(read_inode_ptr(rp.target_inode_ptr), &inode);
|
2025-05-01 22:40:24 +03:00
|
|
|
} else {
|
|
|
|
// seek to last required extension
|
|
|
|
for (int i = 0; i < required_extensions; i++) {
|
|
|
|
if (!next_ext)
|
|
|
|
return 0;
|
2025-04-26 22:12:26 +03:00
|
|
|
|
2025-05-01 22:40:24 +03:00
|
|
|
curr_ext = next_ext;
|
2025-05-17 16:49:39 +03:00
|
|
|
read_block(curr_ext, &ext);
|
2025-05-01 22:40:24 +03:00
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
2025-04-26 22:12:26 +03:00
|
|
|
|
2025-05-01 22:40:24 +03:00
|
|
|
// remove next_extension ptr
|
|
|
|
ext.next_extension = 0;
|
2025-05-17 16:49:39 +03:00
|
|
|
write_block(curr_ext, &ext);
|
2025-04-26 22:12:26 +03:00
|
|
|
}
|
|
|
|
|
2025-05-01 22:40:24 +03:00
|
|
|
// erase all remaining extensions
|
2025-04-26 22:12:26 +03:00
|
|
|
while (next_ext) {
|
|
|
|
mark_free(next_ext);
|
2025-05-17 16:49:39 +03:00
|
|
|
read_block(next_ext, &ext);
|
2025-04-26 22:12:26 +03:00
|
|
|
next_ext = ext.next_extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
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);
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
if (inode_is_referenced(inode_location_cache))
|
2025-04-26 14:41:43 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
|
|
|
|
|
2025-05-01 22:03:32 +03:00
|
|
|
clean_inode(inode_location_cache);
|
2025-05-17 13:53:32 +03:00
|
|
|
|
|
|
|
return 0;
|
2025-04-26 14:41:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
char *path = *((char **) d);
|
2025-04-26 10:21:23 +03:00
|
|
|
{
|
2025-05-17 12:59:07 +03:00
|
|
|
int path_len = strlen(path);
|
|
|
|
if (path_len > FS_MAX_PATH_LEN) {
|
|
|
|
pr_err("path too long (%d > %d)\n",
|
|
|
|
path_len, FS_MAX_PATH_LEN);
|
2025-04-26 10:21:23 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, path, 0);
|
|
|
|
|
|
|
|
if (rp.parent_inode_ptr < 0) {
|
|
|
|
pr_err("unable to create file: no such directory: '%s'\n", path);
|
2025-04-25 21:41:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
if (rp.target_inode_ptr >= 0) {
|
|
|
|
pr_err("file already exists: '%s'\n", rp.target_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char fname[FS_MAX_FNAME_LEN+1];
|
|
|
|
extract_basename(path, fname);
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
2025-05-17 13:53:32 +03:00
|
|
|
struct fs_inode newf = {0};
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
2025-05-17 12:59:07 +03:00
|
|
|
fs_add_fname_to_directory(rp.parent_inode_ptr, inode_ptr_index, fname);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 10:21:23 +03:00
|
|
|
int fs_ln(void *d)
|
|
|
|
{
|
2025-05-17 16:49:39 +03:00
|
|
|
char *prev_path = ((char **) d)[0];
|
|
|
|
char *new_path = ((char **)d)[1];
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
char new_fname[FS_MAX_FNAME_LEN+1];
|
2025-04-26 10:21:23 +03:00
|
|
|
{
|
2025-05-17 16:49:39 +03:00
|
|
|
int new_fname_len = extract_basename(new_path, new_fname);
|
|
|
|
if (new_fname_len > FS_MAX_FNAME_LEN) {
|
|
|
|
pr_err("new filename too long (%d > %d)\n", new_fname_len, FS_MAX_FNAME_LEN);
|
2025-04-26 10:21:23 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
struct resolved_path prev_rp, new_rp;
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
resolve_path(&prev_rp, prev_path, 0);
|
|
|
|
if (prev_rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("no such file: '%s'\n", prev_path);
|
2025-04-25 21:41:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
{
|
|
|
|
struct fs_inode prev_i;
|
|
|
|
read_block(read_inode_ptr(prev_rp.target_inode_ptr), &prev_i);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (prev_i.ftype == DIRECTORY) {
|
|
|
|
pr_err("refusing to create hard link for directory\n");
|
|
|
|
return 0;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
resolve_path(&new_rp, new_path, 1);
|
|
|
|
if (new_rp.parent_inode_ptr < 0) {
|
|
|
|
pr_err("failed to create hard link: no such directory: '%s'\n", new_path);
|
|
|
|
return 0;
|
|
|
|
} else if (new_rp.target_inode_ptr >= 0) {
|
|
|
|
pr_err("file already exists: '%s'\n", new_rp.target_fname);
|
|
|
|
return 0;
|
2025-05-01 22:03:32 +03:00
|
|
|
}
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
pr("Making hard link '%s' -> '%s'\n", new_path, prev_path);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (fs_add_fname_to_directory(new_rp.parent_inode_ptr, prev_rp.target_inode_ptr, new_fname) < 0) {
|
|
|
|
pr_err("failed to add filename to directory '%s' (inode_ptr=%d)\n",
|
|
|
|
new_rp.parent_fname, new_rp.parent_inode_ptr);
|
2025-04-25 21:41:08 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2025-05-17 16:49:39 +03:00
|
|
|
pr("Added new filename to directory '%s' (inode_ptr=%d)\n",
|
|
|
|
new_rp.parent_fname, new_rp.parent_inode_ptr);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// update ref_count in file inode
|
2025-05-17 16:49:39 +03:00
|
|
|
struct fs_inode i;
|
|
|
|
read_block(read_inode_ptr(prev_rp.target_inode_ptr), &i);
|
|
|
|
i.ref_count++;
|
|
|
|
write_block(read_inode_ptr(prev_rp.target_inode_ptr), &i);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
pr("Updated inode ref_count (%d -> %d)\n", i.ref_count-1, i.ref_count);
|
2025-05-17 13:53:32 +03:00
|
|
|
|
|
|
|
return 0;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
static int last_significant_slash_position(char *path, int path_len)
|
|
|
|
{
|
|
|
|
int last_position = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < path_len; i++)
|
|
|
|
if ((path[i] == '/') && (i+1 != path_len))
|
|
|
|
last_position = i;
|
|
|
|
|
|
|
|
return last_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int extract_basename(char *path, char *name)
|
|
|
|
{
|
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
int lsp = last_significant_slash_position(path, path_len);
|
|
|
|
int fname_len = path_len - lsp - 1;
|
|
|
|
|
|
|
|
if (fname_len > FS_MAX_FNAME_LEN) {
|
|
|
|
pr_err("filename too long (%d > %d)",
|
|
|
|
fname_len, FS_MAX_FNAME_LEN);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
// allow the usage of NULL ptr to discard basename string
|
|
|
|
if (name)
|
|
|
|
strcpy(name, &(path[lsp+1]));
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
return fname_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_mkdir(void *d)
|
|
|
|
{
|
|
|
|
char *dirpath = *((char **)d);
|
|
|
|
int dirpath_len = strlen(dirpath);
|
|
|
|
|
|
|
|
if (dirpath_len > FS_MAX_PATH_LEN) {
|
|
|
|
pr_err("path too long ('%s', %d > %d)\n",
|
|
|
|
dirpath, dirpath_len, FS_MAX_FNAME_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char dirname[FS_MAX_FNAME_LEN+1];
|
|
|
|
{
|
|
|
|
int res = extract_basename(dirpath, dirname);
|
|
|
|
if (res < 0) {
|
|
|
|
pr_err("failed to extract dirname from path '%s'\n",
|
|
|
|
dirpath);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int dirname_len = strlen(dirname);
|
|
|
|
|
|
|
|
if (dirname_len > FS_MAX_FNAME_LEN) {
|
|
|
|
pr_err("directory name too long ('%s', %d > %d)\n",
|
|
|
|
dirname, dirname_len, FS_MAX_FNAME_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
struct resolved_path rp;
|
2025-05-17 12:59:07 +03:00
|
|
|
resolve_path(&rp, dirpath, 0);
|
2025-05-16 14:32:44 +03:00
|
|
|
if (rp.parent_inode_ptr < 0) {
|
|
|
|
pr_err("failed to find parent of '%s'\n", dirpath);
|
|
|
|
return 0;
|
|
|
|
} else if (rp.target_inode_ptr >= 0) {
|
|
|
|
pr_err("fname '%s' already exists in directory '%s'\n",
|
|
|
|
rp.target_fname, rp.parent_fname);
|
2025-05-16 12:22:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int target_inode_ptr = find_free_inode_ptr();
|
|
|
|
if (!target_inode_ptr) {
|
|
|
|
pr_err("no free inode ptr\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
int target_inode_block = find_free_block();
|
|
|
|
if (!target_inode_block) {
|
2025-05-16 12:22:31 +03:00
|
|
|
pr_err("no space left on device\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode dir;
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
|
|
|
|
|
|
|
dir.ftype = DIRECTORY;
|
|
|
|
dir.ref_count = 1;
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
write_block(target_inode_block, &dir);
|
|
|
|
mark_used(target_inode_block);
|
2025-05-16 12:22:31 +03:00
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
write_inode_ptr(target_inode_ptr, target_inode_block);
|
2025-05-16 12:22:31 +03:00
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (fs_add_fname_to_directory(rp.parent_inode_ptr, target_inode_ptr, dirname) < 0) {
|
|
|
|
pr_err("failed to add record '%s' -> inode_ptr=%d to directory '%s' (inode_ptr=%d)\n",
|
|
|
|
dirname, target_inode_ptr, rp.parent_fname, rp.parent_inode_ptr);
|
|
|
|
return 0;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// add . and ..
|
|
|
|
fs_add_fname_to_directory(target_inode_ptr, target_inode_ptr, ".");
|
2025-05-16 14:32:44 +03:00
|
|
|
fs_add_fname_to_directory(target_inode_ptr, rp.parent_inode_ptr, "..");
|
2025-05-17 13:53:32 +03:00
|
|
|
|
|
|
|
return 0;
|
2025-05-16 12:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
static int count_active_directory_records(struct fs_inode *inode)
|
|
|
|
{
|
|
|
|
int total_active_records = 0;
|
|
|
|
|
|
|
|
// base inode
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
|
|
|
if (!inode->blocks[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
read_block(inode->blocks[i], &recs);
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
|
|
|
if (!recs[j].fname[0])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(recs[j].fname, "."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(recs[j].fname, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
total_active_records++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inode extensions
|
|
|
|
unsigned int next_ext = inode->next_extension;
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
if (!ext.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
read_block(ext.blocks[i], &recs);
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
|
|
|
if (!recs[j].fname[0])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(recs[j].fname, "."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(recs[j].fname, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
total_active_records++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_active_records;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_rmdir(void *d)
|
|
|
|
{
|
|
|
|
char *dirpath = *((char **)d);
|
|
|
|
int dirpath_len = strlen(dirpath);
|
|
|
|
|
|
|
|
if (dirpath_len > FS_MAX_PATH_LEN) {
|
|
|
|
pr_err("path too long ('%s', %d > %d)\n",
|
|
|
|
dirpath, dirpath_len, FS_MAX_FNAME_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, dirpath, 0);
|
|
|
|
|
|
|
|
if (rp.parent_inode_ptr < 0) {
|
|
|
|
pr_err("failed to find parent directory of '%s'\n", dirpath);
|
|
|
|
return 0;
|
|
|
|
} else if (rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("no such file: '%s'\n", dirpath);
|
|
|
|
return 0;
|
|
|
|
} else if (!strcmp(rp.target_fname, ".")) {
|
|
|
|
pr_err("can not remove '.' from a directory\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct fs_inode i;
|
|
|
|
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
|
|
|
|
|
|
|
|
if (i.ftype != DIRECTORY) {
|
|
|
|
pr_err("'%s' is not a directory\n", rp.target_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count_active_directory_records(&i)) {
|
|
|
|
pr_err("directory '%s' is not empty\n", rp.target_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_remove_fname_from_directory(rp.parent_inode_ptr, rp.target_fname);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
static int get_fname_by_inode_ptr(int dir_inode_ptr, int file_inode_ptr, char *name)
|
|
|
|
{
|
|
|
|
struct fs_inode dir;
|
|
|
|
read_block(read_inode_ptr(dir_inode_ptr), &dir);
|
|
|
|
|
|
|
|
// search 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;
|
|
|
|
|
|
|
|
read_block(dir.blocks[i], &recs);
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[j].fname[0])
|
2025-05-16 12:22:31 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (recs[j].inode_no != file_inode_ptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strcpy(name, recs[j].fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// search inode extensions
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
|
|
|
|
if (!ext.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
read_block(ext.blocks[i], &recs);
|
|
|
|
|
|
|
|
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++) {
|
2025-05-17 13:53:32 +03:00
|
|
|
if (!recs[j].fname[0])
|
2025-05-16 12:22:31 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (recs[j].inode_no != file_inode_ptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strcpy(name, recs[j].fname);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_err("no such file (inode_ptr=%d)\n", file_inode_ptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int build_canonical_dir_path(char *path, int dir_inode_ptr)
|
|
|
|
{
|
2025-05-16 14:32:44 +03:00
|
|
|
// build directory inode_ptr-based reverse path
|
2025-05-16 12:22:31 +03:00
|
|
|
int dir_inode_ptrs[FS_MAX_DIRECTORY_DEPTH];
|
|
|
|
int dir_inode_ptrs_len = 1;
|
|
|
|
|
|
|
|
dir_inode_ptrs[0] = dir_inode_ptr;
|
|
|
|
|
|
|
|
for ( ; dir_inode_ptrs_len < FS_MAX_DIRECTORY_DEPTH; dir_inode_ptrs_len++) {
|
|
|
|
// check if reached the root dir
|
|
|
|
if (!dir_inode_ptrs[dir_inode_ptrs_len-1])
|
|
|
|
break;
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
int *r = find_filename_in_directory(dir_inode_ptrs[dir_inode_ptrs_len-1], "..");
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
if (!r) {
|
2025-05-16 14:32:44 +03:00
|
|
|
pr_err("failed to find parent directory of inode_ptr=%d\n", dir_inode_ptrs[dir_inode_ptrs_len-1]);
|
2025-05-16 12:22:31 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_inode_ptrs[dir_inode_ptrs_len] = r[2];
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
// build string path based on inode_ptr list
|
2025-05-16 12:22:31 +03:00
|
|
|
memset(path, 0, sizeof(FS_MAX_PATH_LEN));
|
|
|
|
|
|
|
|
for (int i = dir_inode_ptrs_len - 2; i >= 0; i--) {
|
|
|
|
char fname[FS_MAX_FNAME_LEN+1];
|
|
|
|
|
|
|
|
if (get_fname_by_inode_ptr(dir_inode_ptrs[i+1], dir_inode_ptrs[i], fname) < 0) {
|
|
|
|
pr_err("failed to build canonical path\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
strcat(path, "/");
|
2025-05-16 12:22:31 +03:00
|
|
|
strcat(path, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_cd(void *d)
|
|
|
|
{
|
|
|
|
char *new_dir = *((char **)d);
|
|
|
|
int new_dir_len = strlen(new_dir);
|
|
|
|
|
|
|
|
if (new_dir_len > FS_MAX_PATH_LEN) {
|
|
|
|
pr_err("path too long (%d > %d)\n", new_dir_len, FS_MAX_PATH_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
struct resolved_path rp;
|
2025-05-17 12:59:07 +03:00
|
|
|
resolve_path(&rp, new_dir, FOLLOW_LAST_SYMLINK);
|
2025-05-16 12:22:31 +03:00
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (rp.parent_inode_ptr < 0) {
|
|
|
|
pr_err("failed to find parent of '%s' from '%s'\n", new_dir, fs_cwd);
|
2025-05-16 12:22:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (rp.target_inode_ptr < 0) {
|
2025-05-17 12:59:07 +03:00
|
|
|
pr_err("no such directory: '%s'\n", new_dir);
|
2025-05-16 12:22:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char canonical_path[FS_MAX_PATH_LEN+1];
|
2025-05-16 14:32:44 +03:00
|
|
|
if (build_canonical_dir_path(canonical_path, rp.target_inode_ptr) < 0) {
|
|
|
|
pr_err("failed to build canonical path to '%s' (inode_ptr=%d)\n",
|
|
|
|
new_dir, rp.target_inode_ptr);
|
2025-05-16 12:22:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-05-16 14:32:44 +03:00
|
|
|
if (!canonical_path[0])
|
|
|
|
canonical_path[0] = '/';
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
char *cpp = (char *) canonical_path;
|
|
|
|
fs_chdir(&cpp);
|
2025-05-16 14:32:44 +03:00
|
|
|
fs_cwd_inode_ptr = rp.target_inode_ptr;
|
2025-05-16 12:22:31 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
char *path = *((char **) d);
|
|
|
|
{
|
|
|
|
int path_len = strlen(path);
|
|
|
|
if (path_len > FS_MAX_PATH_LEN) {
|
|
|
|
pr_err("path too long (%d > %d)\n",
|
|
|
|
path_len, FS_MAX_PATH_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, path, 0);
|
|
|
|
|
|
|
|
if (rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("file does not exist: '%s'\n", path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode i;
|
|
|
|
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
|
|
|
|
|
|
|
|
if (i.ftype == DIRECTORY) {
|
|
|
|
pr_err("file '%s' is a directory\n", rp.target_fname);
|
|
|
|
return 0;
|
|
|
|
}
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-05-17 12:59:07 +03:00
|
|
|
if (fs_remove_fname_from_directory(rp.parent_inode_ptr, rp.target_fname) < 0) {
|
|
|
|
pr_err("failed to unlink '%s'\n", rp.target_fname);
|
2025-04-26 10:21:23 +03:00
|
|
|
} else {
|
2025-05-17 12:59:07 +03:00
|
|
|
pr("Unlinked '%s'\n", rp.target_fname);
|
2025-04-26 10:21:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fs_ls(void *d)
|
|
|
|
{
|
2025-04-25 21:41:08 +03:00
|
|
|
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++) {
|
2025-04-30 21:39:27 +03:00
|
|
|
if (!dir.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-30 21:39:27 +03:00
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-16 15:57:09 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-30 21:39:27 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// list entries from inode extension
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
if (!ext.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-16 15:57:09 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-25 21:41:08 +03:00
|
|
|
continue;
|
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 10:21:23 +03:00
|
|
|
int fs_la(void *d)
|
|
|
|
{
|
2025-04-25 21:41:08 +03:00
|
|
|
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++) {
|
2025-04-30 21:39:27 +03:00
|
|
|
if (!dir.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
2025-04-30 21:39:27 +03:00
|
|
|
read_block(dir.blocks[i], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
2025-04-30 21:39:27 +03:00
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-16 15:57:09 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-30 21:39:27 +03:00
|
|
|
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
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++) {
|
|
|
|
if (!ext.blocks[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct fs_directory_record recs[DIRECTORY_RECORDS_PER_BLOCK];
|
|
|
|
read_block(ext.blocks[i], (void *) &recs);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
|
2025-05-16 15:57:09 +03:00
|
|
|
if (!recs[k].fname[0])
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
2025-04-30 21:39:27 +03:00
|
|
|
" (inode_ptr=%d -> inode=%d, ref_count=%d, size=%d, type=dir)\n",
|
2025-04-25 21:41:08 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-23 22:51:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-26 22:33:28 +03:00
|
|
|
int fs_stat(void *d)
|
|
|
|
{
|
2025-05-17 16:49:39 +03:00
|
|
|
char *path = *((char **) d);
|
|
|
|
|
|
|
|
struct resolved_path rp;
|
|
|
|
resolve_path(&rp, path, 0);
|
2025-04-26 22:33:28 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (rp.target_inode_ptr < 0) {
|
|
|
|
pr_err("no such file: '%s'\n", path);
|
2025-05-16 15:57:09 +03:00
|
|
|
return 0;
|
2025-04-26 22:33:28 +03:00
|
|
|
}
|
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
struct fs_inode i;
|
|
|
|
read_block(read_inode_ptr(rp.target_inode_ptr), &i);
|
2025-04-26 22:33:28 +03:00
|
|
|
|
2025-05-17 16:49:39 +03:00
|
|
|
if (i.ftype == DIRECTORY) {
|
2025-04-26 22:33:28 +03:00
|
|
|
pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=dir\n",
|
2025-05-17 16:49:39 +03:00
|
|
|
rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size);
|
2025-04-26 22:33:28 +03:00
|
|
|
} else {
|
|
|
|
pr_stdout("inode_ptr=%d -> inode=%d\nref_count=%d\nsize=%d\ntype=reg\n",
|
2025-05-17 16:49:39 +03:00
|
|
|
rp.target_inode_ptr, read_inode_ptr(rp.target_inode_ptr), i.ref_count, i.size);
|
2025-04-26 22:33:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-04-23 22:51:00 +03:00
|
|
|
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) {
|
2025-05-16 12:22:31 +03:00
|
|
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
2025-04-24 21:40:16 +03:00
|
|
|
char *root_dir_path = "/";
|
|
|
|
fs_chdir((void *) &root_dir_path);
|
2025-04-25 21:41:08 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
pr("Formatting storage device '%s' of size %ld (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);
|
2025-04-23 22:51:00 +03:00
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
struct fs_header fsh = {0};
|
2025-04-23 22:51:00 +03:00
|
|
|
fsh.version = 0x1;
|
|
|
|
fsh.max_inode_count = max_inode_count;
|
|
|
|
fsh.block_count = block_count;
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
pr("header size is %ld bytes, writing it to the first block\n", sizeof(fsh));
|
2025-04-23 22:51:00 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2025-05-17 13:53:32 +03:00
|
|
|
struct fs_inode root_dir = {0};
|
2025-04-24 21:40:16 +03:00
|
|
|
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);
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
memset(fs_cwd, 0, sizeof(fs_cwd));
|
2025-04-24 21:40:16 +03:00
|
|
|
char *root_dir_path = "/";
|
|
|
|
fs_chdir((void *) &root_dir_path);
|
2025-04-23 22:51:00 +03:00
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
fs_cwd_inode_ptr = 0;
|
|
|
|
|
2025-05-16 12:22:31 +03:00
|
|
|
// add . and ..
|
|
|
|
fs_add_fname_to_directory(0, 0, ".");
|
|
|
|
fs_add_fname_to_directory(0, 0, "..");
|
|
|
|
|
2025-04-23 22:51:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|