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-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;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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 {
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return (found_block_no);
|
|
|
|
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++) {
|
|
|
|
if (r[j].inode_no)
|
|
|
|
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
|
|
|
|
// TODO
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2025-04-25 21:41:08 +03:00
|
|
|
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-25 21:41:08 +03:00
|
|
|
|
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];
|
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-04-26 14:41:43 +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
|
|
|
|
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-04-26 16:05:40 +03:00
|
|
|
unsigned int inode_ptr_cache = recs[k].inode_no;
|
2025-04-26 14:41:43 +03:00
|
|
|
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);
|
|
|
|
|
2025-04-26 15:28:17 +03:00
|
|
|
// clear directory record inode_ptr
|
|
|
|
recs[k].inode_no = 0;
|
|
|
|
write_block(dir.blocks[i], (void *) &recs);
|
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
// if it drops to zero, nullify inode_ptr pointing to this inode
|
2025-04-26 15:28:17 +03:00
|
|
|
if (f.ref_count)
|
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
|
|
|
|
pr("ref_count=0, clearing inode_ptr\n");
|
2025-04-26 16:05:40 +03:00
|
|
|
write_inode_ptr(inode_ptr_cache, 0);
|
2025-04-26 15:28:17 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
}
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-04-26 15:28:17 +03:00
|
|
|
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
|
2025-04-26 10:21:23 +03:00
|
|
|
|
2025-04-26 15:28:17 +03:00
|
|
|
// 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);
|
2025-04-26 14:41:43 +03:00
|
|
|
|
2025-04-26 15:28:17 +03:00
|
|
|
// 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-26 10:21:23 +03:00
|
|
|
}
|
|
|
|
}
|
2025-04-26 14:41:43 +03:00
|
|
|
|
|
|
|
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;
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
2025-04-26 14:41:43 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
write_block(f.blocks[block_index], (void *) buf);
|
|
|
|
} 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));
|
|
|
|
|
|
|
|
f.next_extension = new_block;
|
|
|
|
write_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int target_extension_address;
|
|
|
|
unsigned int previous_extension_address = f.next_extension;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// seek to target extension address
|
|
|
|
for (
|
|
|
|
target_extension_address = previous_extension_address, i = 0;
|
|
|
|
(i < ext_no);
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
if (!target_extension_address) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
// updating previous inode extension
|
|
|
|
struct fs_inode_extension prev_ext;
|
|
|
|
read_block(previous_extension_address, (void *) &prev_ext); // read
|
|
|
|
prev_ext.next_extension = new_block; // modify
|
|
|
|
write_block(previous_extension_address, (void *) &prev_ext); // write
|
|
|
|
|
|
|
|
// writing new inode extension
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
write_block(target_extension_address, (void *) &ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
read_block(target_extension_address, (void *) &ext);
|
|
|
|
previous_extension_address = target_extension_address;
|
|
|
|
target_extension_address = ext.next_extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!target_extension_address) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_inode_extension prev_ext;
|
|
|
|
read_block(previous_extension_address, (void *) &prev_ext); // read
|
|
|
|
prev_ext.next_extension = new_block; // modify
|
|
|
|
write_block(previous_extension_address, (void *) &prev_ext); // write
|
|
|
|
|
|
|
|
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_block);
|
|
|
|
mark_used(new_data_block);
|
|
|
|
|
|
|
|
// writing new inode extension
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
ext.blocks[ext_offset] = new_data_block;
|
|
|
|
write_block(target_extension_address, (void *) &ext);
|
|
|
|
|
|
|
|
// writing new data block
|
|
|
|
write_block(new_data_block, (void *) buf);
|
|
|
|
} else {
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
read_block(target_extension_address, (void *) &ext);
|
|
|
|
|
|
|
|
if (!ext.blocks[ext_offset]) {
|
|
|
|
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;
|
|
|
|
write_block(target_extension_address, (void *) &ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
write_block(ext.blocks[ext_offset], (void *) buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
char *str = *((char **) d+4);
|
|
|
|
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;
|
|
|
|
|
|
|
|
unsigned char data_buffer[FS_BLOCK_SIZE];
|
|
|
|
|
|
|
|
if (block_offset + str_len <= FS_BLOCK_SIZE) {
|
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
|
|
|
memcpy(&(data_buffer[block_offset]), str, str_len);
|
|
|
|
write_fd_block(fd, block_index, data_buffer);
|
|
|
|
} else {
|
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
|
|
|
memcpy(&(data_buffer[block_offset]), str, FS_BLOCK_SIZE - block_offset);
|
|
|
|
write_fd_block(fd, block_index, data_buffer);
|
|
|
|
|
|
|
|
read_fd_block(fd, block_index+1, data_buffer);
|
|
|
|
memcpy(data_buffer, &(str[FS_BLOCK_SIZE - block_offset]), str_len - block_offset);
|
|
|
|
write_fd_block(fd, block_index+1, data_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
pr("Moving fd %d offset: %d -> %d\n",
|
|
|
|
fd, fs_file_descriptions[fd].rw_offset, fs_file_descriptions[fd].rw_offset + str_len);
|
|
|
|
fs_file_descriptions[fd].rw_offset += str_len;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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)
|
|
|
|
{
|
2025-04-25 21:41:08 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
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
|
|
|
|
|
2025-04-26 16:05:40 +03:00
|
|
|
pr_err("no such file '%s'\n", existing_fname);
|
2025-04-25 21:41:08 +03:00
|
|
|
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) {
|
2025-04-26 16:05:40 +03:00
|
|
|
pr_err("failed to register filename in directory '%s'\n", fs_cwd);
|
2025-04-25 21:41:08 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2025-04-26 16:05:40 +03:00
|
|
|
pr("Registered new filename in directory '%s'\n", fs_cwd);
|
2025-04-25 21:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
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++) {
|
|
|
|
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)
|
|
|
|
{
|
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++) {
|
|
|
|
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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2025-04-25 21:41:08 +03:00
|
|
|
fs_cwd_inode_ptr = 0;
|
|
|
|
|
2025-04-23 22:51:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|