Compare commits

5 Commits

7 changed files with 631 additions and 112 deletions

View File

@@ -1,4 +1,4 @@
CFLAGS = -g3 -O0 -I. -Iinc CFLAGS = -g3 -O0 -Wall -Wpedantic -I. -Iinc
build: main build: main

View File

@@ -26,6 +26,7 @@
#define FS_MAX_PATH_LEN 512 #define FS_MAX_PATH_LEN 512
#define FS_MAX_OPEN_FD 32 #define FS_MAX_OPEN_FD 32
#define FS_MAX_FNAME_LEN 11 #define FS_MAX_FNAME_LEN 11
#define FS_MAX_DIRECTORY_DEPTH 512
#endif #endif

View File

@@ -5,11 +5,11 @@
#include "config.h" #include "config.h"
#if COLOR_ENABLE == 1 #if COLOR_ENABLE == 1
#define COLOR_RESET "\e[0m" #define COLOR_RESET "\x1b[0m"
#define COLOR_RED "\e[0;31m" #define COLOR_RED "\x1b[0;31m"
#define COLOR_YELLOW "\e[0;33m" #define COLOR_YELLOW "\x1b[0;33m"
#define COLOR_BLUE "\e[0;34m" #define COLOR_BLUE "\x1b[0;34m"
#define COLOR_CYAN "\e[0;36m" #define COLOR_CYAN "\x1b[0;36m"
#else #else
#define COLOR_RESET "" #define COLOR_RESET ""
#define COLOR_RED "" #define COLOR_RED ""

View File

@@ -1,5 +1,7 @@
#include "config.h" #include "config.h"
#define FOLLOW_LAST_SYMLINK 0x1
enum fs_filetype { enum fs_filetype {
REGULAR, REGULAR,
DIRECTORY DIRECTORY
@@ -37,7 +39,7 @@ struct fs_inode_extension {
__attribute__((packed)) __attribute__((packed))
struct fs_directory_record { struct fs_directory_record {
unsigned char fname[FS_MAX_FNAME_LEN+1]; char fname[FS_MAX_FNAME_LEN+1];
unsigned int inode_no; unsigned int inode_no;
}; };
@@ -46,6 +48,13 @@ struct fs_file_description {
unsigned int rw_offset; unsigned int rw_offset;
}; };
struct resolved_path {
int target_inode_ptr;
int parent_inode_ptr;
char target_fname[FS_MAX_FNAME_LEN+1];
char parent_fname[FS_MAX_FNAME_LEN+1];
};
char *fs_get_cwd(void); char *fs_get_cwd(void);
int fs_create(void *d); int fs_create(void *d);
@@ -61,6 +70,9 @@ int fs_seek(void *d);
int fs_read(void *d); int fs_read(void *d);
int fs_write(void *d); int fs_write(void *d);
int fs_close(void *d); int fs_close(void *d);
int fs_mkdir(void *d);
int fs_rmdir(void *d);
int fs_cd(void *d);
int fs_truncate(void *d); int fs_truncate(void *d);
int fs_allow_write(void *d); int fs_allow_write(void *d);
int fs_prohibit_write(void *d); int fs_prohibit_write(void *d);

View File

@@ -9,68 +9,58 @@
#include "color.h" #include "color.h"
#if DEBUG == 1 #if DEBUG == 1
#define pr(...) { printf("[%s:%d] ", __FILE__, __LINE__); printf(__VA_ARGS__); } #define pr(...) do { printf("[%s:%d] ", __FILE__, __LINE__); printf(__VA_ARGS__); } while (0)
#else #else
#define pr(...) {} #define pr(...)
#endif #endif
#if ENABLE_STDOUT == 1 #if ENABLE_STDOUT == 1
#define pr_stdout(...) { printf(__VA_ARGS__); } #define pr_stdout(...) do { printf(__VA_ARGS__); } while (0)
#define write_stdout(data, size) { write(1, (data), (size)); } #define write_stdout(data, size) do { write(1, (data), (size)); } while (0)
#else #else
#define pr_stdout(...) {} #define pr_stdout(...)
#define write_stdout(data, size) {} #define write_stdout(data, size)
#endif
#if LOG_LEVEL >= 0
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1
#define pr_generic(color, prefix, ...) do { \
printf(color "[%s:%d] " prefix, __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf(COLOR_RESET); \
} while (0)
#else
#define pr_generic(color, prefix, ...) do { \
printf(color prefix); \
printf(__VA_ARGS__); \
printf(COLOR_RESET); \
} while (0)
#endif
#else
#define pr_generic(color, prefix, ...)
#endif #endif
#if LOG_LEVEL >= 2 #if LOG_LEVEL >= 2
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1 #define pr_err(...) pr_generic(COLOR_RED, "Error: ", __VA_ARGS__)
#define pr_err(...) { \
printf(COLOR_RED "[%s:%d] Error: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf(COLOR_RESET); \
}
#else
#define pr_err(...) { \
printf(COLOR_RED "Error: "); \
printf(__VA_ARGS__); \
printf(COLOR_RESET);\
}
#endif
#else #else
#define pr_err(...) {} #define pr_err(...)
#endif #endif
#if LOG_LEVEL >= 3 #if LOG_LEVEL >= 3
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1 #define pr_warn(...) pr_generic(COLOR_YELLOW, "Warning: ", __VA_ARGS__)
#define pr_warn(...) { \
printf(COLOR_YELLOW "[%s:%d] Warning: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf(COLOR_RESET); \
}
#else
#define pr_warn(...) { \
printf(COLOR_YELLOW "Warning: "); \
printf(__VA_ARGS__); \
printf(COLOR_RESET); \
}
#endif
#else #else
#define pr_warn(...) {} #define pr_warn(...)
#endif #endif
#if LOG_LEVEL >= 4 #if LOG_LEVEL >= 4
#if ENABLE_FILE_LINE_IN_OTHER_PR == 1 #define pr_info(...) pr_generic(COLOR_RESET, "Info: ", __VA_ARGS__)
#define pr_info(...) { \
printf("[%s:%d] Info: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
}
#else
#define pr_info(...) { printf("Info: "); printf(__VA_ARGS__); }
#endif
#else #else
#define pr_info(...) {} #define pr_info(...)
#endif #endif

View File

@@ -26,6 +26,10 @@ static const struct CliCommandEntry cmd[] = {
{"write", 2, (enum CliArgType[]) {INT, STR}, fs_write}, {"write", 2, (enum CliArgType[]) {INT, STR}, fs_write},
{"close", 1, (enum CliArgType[]) {INT}, fs_close}, {"close", 1, (enum CliArgType[]) {INT}, fs_close},
{"mkdir", 1, (enum CliArgType[]) {STR}, fs_mkdir},
{"rmdir", 1, (enum CliArgType[]) {STR}, fs_rmdir},
{"cd", 1, (enum CliArgType[]) {STR}, fs_cd},
// custom commands // custom commands
{"use", 1, (enum CliArgType[]) {STR}, fs_use}, {"use", 1, (enum CliArgType[]) {STR}, fs_use},
{"la", 0, NULL, fs_la}, {"la", 0, NULL, fs_la},
@@ -58,7 +62,7 @@ static int tokenized_line_len(char **t)
static char **tokenize_line(char *line, ssize_t result) static char **tokenize_line(char *line, ssize_t result)
{ {
if (result > CLI_MAX_LINE_LENGTH) { if (result > CLI_MAX_LINE_LENGTH) {
pr_err("command too long (%d > %d)\n", result+1, CLI_MAX_LINE_LENGTH); pr_err("command too long (%ld > %d)\n", result+1, CLI_MAX_LINE_LENGTH);
return NULL; return NULL;
} }
@@ -68,8 +72,8 @@ static char **tokenize_line(char *line, ssize_t result)
unsigned int i, t, l; unsigned int i, t, l;
for ( for (
i = 0, t = 0, l = 0; i = 0, t = 0, l = 0;
i < CLI_MAX_LINE_LENGTH, i < CLI_MAX_LINE_LENGTH &&
t <= CLI_MAX_ACCEPTED_ARGS, t <= CLI_MAX_ACCEPTED_ARGS &&
l < CLI_MAX_TOKEN_LENGTH; l < CLI_MAX_TOKEN_LENGTH;
i++ i++
) { ) {
@@ -152,6 +156,9 @@ void *format_args(char **tokenized_line, unsigned int arg_count, enum CliArgType
case STR: case STR:
struct_size += sizeof(char *); struct_size += sizeof(char *);
break; break;
case WRONG_TYPE:
pr_err("Wrong type occured in i=%d\n", i);
break;
} }
new_struct = malloc(struct_size); new_struct = malloc(struct_size);
@@ -204,9 +211,9 @@ unsigned int cli_poll_process_next(void)
fputs(fs_get_cwd(), stdout); fputs(fs_get_cwd(), stdout);
if (fs_get_cwd()[0]) { if (fs_get_cwd()[0]) {
fputs(" $ ", stdout); printf(" $ ");
} else { } else {
fputs("$ ", stdout); printf("$ ");
} }
size_t buf_size = 0; size_t buf_size = 0;

623
src/fs.c
View File

@@ -23,6 +23,8 @@ static unsigned int fs_cwd_inode_ptr;
static struct fs_file_description fs_file_descriptions[FS_MAX_OPEN_FD]; static struct fs_file_description fs_file_descriptions[FS_MAX_OPEN_FD];
static int extract_basename(char *path, char *name);
static int read_block(unsigned int block_no, void *data) static int read_block(unsigned int block_no, void *data)
{ {
@@ -34,7 +36,7 @@ static int read_block(unsigned int block_no, void *data)
} }
if (block_no * FS_BLOCK_SIZE >= st.st_size) { if (block_no * FS_BLOCK_SIZE >= st.st_size) {
pr_err("read beyond device address space denied (%d >= %d)\n", pr_err("read beyond device address space denied (%d >= %ld)\n",
block_no * FS_BLOCK_SIZE, st.st_size); block_no * FS_BLOCK_SIZE, st.st_size);
return -1; return -1;
} }
@@ -57,7 +59,7 @@ static int write_block(unsigned int block_no, void *data)
} }
if (block_no * FS_BLOCK_SIZE >= st.st_size) { if (block_no * FS_BLOCK_SIZE >= st.st_size) {
pr_err("write beyond device address space denied (%d >= %d)\n", pr_err("write beyond device address space denied (%d >= %ld)\n",
block_no * FS_BLOCK_SIZE, st.st_size); block_no * FS_BLOCK_SIZE, st.st_size);
return -1; return -1;
} }
@@ -451,7 +453,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
read_block(dir.blocks[i], (void *) &r); read_block(dir.blocks[i], (void *) &r);
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) { for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
if (r[j].inode_no) if (r[j].fname[0])
continue; continue;
if (found_block_no >= fsh.max_inode_count) if (found_block_no >= fsh.max_inode_count)
@@ -465,7 +467,6 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
struct fs_inode_extension ext; struct fs_inode_extension ext;
unsigned int next_ext = dir.next_extension; unsigned int next_ext = dir.next_extension;
unsigned int curr_ext = 0;
while (next_ext) { while (next_ext) {
read_block(next_ext, (void *) &ext); read_block(next_ext, (void *) &ext);
@@ -484,7 +485,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
read_block(ext.blocks[i], (void *) &r); read_block(ext.blocks[i], (void *) &r);
for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) { for (int j = 0; j < DIRECTORY_RECORDS_PER_BLOCK; j++, found_block_no++) {
if (r[j].inode_no) if (r[j].fname[0])
continue; continue;
if (found_block_no >= fsh.max_inode_count) if (found_block_no >= fsh.max_inode_count)
@@ -503,7 +504,7 @@ static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname) static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
{ {
int dir_inode = read_inode_ptr(fs_cwd_inode_ptr); int dir_inode = read_inode_ptr(dir_inode_ptr);
struct fs_inode dir; struct fs_inode dir;
read_block(dir_inode, (void *) &dir); read_block(dir_inode, (void *) &dir);
@@ -516,7 +517,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
if (strcmp(fname, recs[k].fname)) if (strcmp(fname, recs[k].fname))
@@ -547,7 +548,7 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
if (strcmp(fname, recs[k].fname)) if (strcmp(fname, recs[k].fname))
@@ -567,6 +568,21 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
return NULL; return NULL;
} }
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;
if (r) {
res = r[2];
free(r);
} else {
res = -1;
}
return res;
}
static int inode_is_referenced(unsigned int inode) static int inode_is_referenced(unsigned int inode)
{ {
struct fs_inode f; struct fs_inode f;
@@ -620,7 +636,7 @@ static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int ta
if (r) { if (r) {
free(r); free(r);
pr_err("filename '%s' already exists\n", fname); pr_err("filename '%s' already exists\n", fname);
return 0; return -1;
} }
} }
@@ -649,7 +665,7 @@ static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int ta
new_block, dir_inode_ptr, read_inode_ptr(dir_inode_ptr), block_no); new_block, dir_inode_ptr, read_inode_ptr(dir_inode_ptr), block_no);
} }
char zero_data[FS_BLOCK_SIZE] = {}; char zero_data[FS_BLOCK_SIZE] = {0};
memset(zero_data, 0, FS_BLOCK_SIZE); memset(zero_data, 0, FS_BLOCK_SIZE);
write_block(new_block, &zero_data); write_block(new_block, &zero_data);
@@ -773,13 +789,13 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
// decrement ref_count // decrement ref_count
struct fs_inode f; struct fs_inode f;
read_block(read_inode_ptr(recs[k].inode_no), (void *) &f); read_block(read_inode_ptr(recs[k].inode_no), &f);
f.ref_count--; f.ref_count--;
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f); write_block(read_inode_ptr(recs[k].inode_no), &f);
// clear directory record inode_ptr // clear directory record inode_ptr
recs[k].inode_no = 0; recs[k].fname[0] = 0;
write_block(dir_block_addr, (void *) recs); write_block(dir_block_addr, recs);
// if it drops to zero, nullify inode_ptr pointing to this inode // if it drops to zero, nullify inode_ptr pointing to this inode
if (f.ref_count) if (f.ref_count)
@@ -807,11 +823,8 @@ static void fs_remove_fname_from_directory_removal(struct fs_directory_record *r
static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname) 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);
struct fs_inode dir; struct fs_inode dir;
read_block(dir_inode, (void *) &dir); read_block(read_inode_ptr(dir_inode_ptr), &dir);
// list entries from base inode // list entries from base inode
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) { for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
@@ -820,10 +833,10 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
if (!dir.blocks[i]) if (!dir.blocks[i])
continue; continue;
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
if (strcmp(fname, recs[k].fname)) if (strcmp(fname, recs[k].fname))
@@ -860,7 +873,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
if (strcmp(fname, recs[k].fname)) if (strcmp(fname, recs[k].fname))
@@ -884,6 +897,111 @@ fs_remove_fname_from_directory_finish:
return 0; return 0;
} }
static void resolve_path(struct resolved_path *rp, char *path, unsigned int params)
{
rp->parent_inode_ptr = -1;
rp->target_inode_ptr = -1;
int path_len = strlen(path);
if (!path_len) {
pr_err("no path specified\n");
return;
}
if (!strcmp(path, "/")) {
rp->parent_inode_ptr = 0;
rp->target_inode_ptr = 0;
strcpy(rp->parent_fname, "/");
strcpy(rp->target_fname, "/");
return;
}
char current_fname[FS_MAX_FNAME_LEN+1] = {0};
int current_inode_ptr;
int i;
if (path[0] == '/') {
current_inode_ptr = 0;
rp->parent_inode_ptr = 0;
strcpy(current_fname, "/");
i = 1;
} else {
current_inode_ptr = fs_cwd_inode_ptr;
rp->parent_inode_ptr = fs_cwd_inode_ptr;
extract_basename(fs_cwd, current_fname);
i = 0;
}
char next_fname[FS_MAX_FNAME_LEN+1] = {0};
int next_fname_ptr = 0;
for ( ; i < path_len; i++) {
if (path[i] != '/') {
next_fname[next_fname_ptr] = path[i];
next_fname_ptr++;
continue;
}
// path[i] == '/', trying to change current directory
if (!next_fname_ptr)
continue;
if (i+1 == path_len)
continue;
{
int res = find_fname_in_directory(current_inode_ptr, next_fname);
if (res < 0) {
pr_warn("directory '%s' does not exist\n", next_fname);
rp->parent_inode_ptr = -1;
break;
}
{
struct fs_inode d;
read_block(read_inode_ptr(res), &d);
if (d.ftype == REGULAR) {
pr_warn("'%s' is a regular file\n", next_fname);
rp->parent_inode_ptr = -1;
break;
}
}
pr("Found directory '%s'\n", next_fname);
current_inode_ptr = res;
rp->parent_inode_ptr = res;
}
next_fname_ptr = 0;
strcpy(current_fname, next_fname);
memset(next_fname, 0, FS_MAX_FNAME_LEN);
}
if (rp->parent_inode_ptr < 0) {
rp->target_inode_ptr = -1;
return;
}
strcpy(rp->parent_fname, current_fname);
{
int res = find_fname_in_directory(current_inode_ptr, next_fname);
if (res < 0) {
pr_warn("target fname '%s' does not exist\n", next_fname);
rp->target_inode_ptr = -1;
} else {
rp->target_inode_ptr = res;
strcpy(rp->target_fname, next_fname);
}
}
return;
}
int fs_open(void *d) int fs_open(void *d)
{ {
char *fname = *((char **) d); char *fname = *((char **) d);
@@ -1008,7 +1126,7 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
write_block(fs_file_descriptions[fd].inode, (void *) &f); write_block(fs_file_descriptions[fd].inode, (void *) &f);
} }
write_block(f.blocks[block_index], (void *) buf); return write_block(f.blocks[block_index], (void *) buf);
} else { } else {
int ext_no = (block_index - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION; 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; int ext_offset = (block_index - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION;
@@ -1075,7 +1193,7 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
write_block(curr_ext, (void *) &ext); write_block(curr_ext, (void *) &ext);
} }
write_block(ext.blocks[ext_offset], (void *) buf); return write_block(ext.blocks[ext_offset], (void *) buf);
} }
} }
@@ -1343,6 +1461,8 @@ int fs_close(void *d)
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache); pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
clean_inode(inode_location_cache); clean_inode(inode_location_cache);
return 0;
} }
@@ -1353,24 +1473,32 @@ int fs_create(void *d)
return 0; return 0;
} }
char *fname = *((char **) d); char *path = *((char **) d);
int fname_len = strlen(fname);
{ {
// check if duplicate filename exists in current directory int path_len = strlen(path);
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname); if (path_len > FS_MAX_PATH_LEN) {
if (r) { pr_err("path too long (%d > %d)\n",
free(r); path_len, FS_MAX_PATH_LEN);
pr_err("filename '%s' already exists\n", fname);
return 0; return 0;
} }
} }
if (fname_len > FS_MAX_FNAME_LEN) { struct resolved_path rp;
pr_err("filename too long (%d > %d)\n", fname_len, FS_MAX_FNAME_LEN); resolve_path(&rp, path, 0);
if (rp.parent_inode_ptr < 0) {
pr_err("unable to create file: no such directory: '%s'\n", path);
return 0; return 0;
} }
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);
pr("Creating regular file '%s'\n", fname); pr("Creating regular file '%s'\n", fname);
int inode_block_no = find_free_block(); int inode_block_no = find_free_block();
@@ -1380,7 +1508,7 @@ int fs_create(void *d)
} }
// write new file inode // write new file inode
struct fs_inode newf = {}; struct fs_inode newf = {0};
newf.ftype = REGULAR; newf.ftype = REGULAR;
newf.ref_count = 1; newf.ref_count = 1;
newf.size = 0; newf.size = 0;
@@ -1411,7 +1539,7 @@ int fs_create(void *d)
write_inode_ptr(inode_ptr_index, inode_block_no); write_inode_ptr(inode_ptr_index, inode_block_no);
// add filename to current directory inode // add filename to current directory inode
fs_add_fname_to_directory(fs_cwd_inode_ptr, inode_ptr_index, fname); fs_add_fname_to_directory(rp.parent_inode_ptr, inode_ptr_index, fname);
return 0; return 0;
} }
@@ -1520,8 +1648,365 @@ original_inode_ptr_found:
write_block(read_inode_ptr(original_inode_ptr), (void *) &f); 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); pr("Updated inode ref_count (%d -> %d)\n", f.ref_count-1, f.ref_count);
return 0;
} }
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;
}
strcpy(name, &(path[lsp+1]));
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;
}
struct resolved_path rp;
resolve_path(&rp, dirpath, 0);
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);
return 0;
}
int target_inode_ptr = find_free_inode_ptr();
if (!target_inode_ptr) {
pr_err("no free inode ptr\n");
return 0;
}
int target_inode_block = find_free_block();
if (!target_inode_block) {
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;
write_block(target_inode_block, &dir);
mark_used(target_inode_block);
write_inode_ptr(target_inode_ptr, target_inode_block);
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;
}
// add . and ..
fs_add_fname_to_directory(target_inode_ptr, target_inode_ptr, ".");
fs_add_fname_to_directory(target_inode_ptr, rp.parent_inode_ptr, "..");
return 0;
}
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;
}
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++) {
if (!recs[j].fname[0])
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++) {
if (!recs[j].fname[0])
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)
{
// build directory inode_ptr-based reverse path
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;
int *r = find_filename_in_directory(dir_inode_ptrs[dir_inode_ptrs_len-1], "..");
if (!r) {
pr_err("failed to find parent directory of inode_ptr=%d\n", dir_inode_ptrs[dir_inode_ptrs_len-1]);
return -1;
}
dir_inode_ptrs[dir_inode_ptrs_len] = r[2];
free(r);
}
// build string path based on inode_ptr list
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;
}
strcat(path, "/");
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;
}
struct resolved_path rp;
resolve_path(&rp, new_dir, FOLLOW_LAST_SYMLINK);
if (rp.parent_inode_ptr < 0) {
pr_err("failed to find parent of '%s' from '%s'\n", new_dir, fs_cwd);
return 0;
}
if (rp.target_inode_ptr < 0) {
pr_err("no such directory: '%s'\n", new_dir);
return 0;
}
char canonical_path[FS_MAX_PATH_LEN+1];
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);
return 0;
}
if (!canonical_path[0])
canonical_path[0] = '/';
char *cpp = (char *) canonical_path;
fs_chdir(&cpp);
fs_cwd_inode_ptr = rp.target_inode_ptr;
return 0;
}
int fs_rm(void *d) int fs_rm(void *d)
{ {
if (!write_permitted) { if (!write_permitted) {
@@ -1529,12 +2014,36 @@ int fs_rm(void *d)
return 0; return 0;
} }
char *fname = *((char **) d); 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;
}
}
if (fs_remove_fname_from_directory(fs_cwd_inode_ptr, fname) < 0) { struct resolved_path rp;
pr_err("failed to unlink '%s'\n", fname); 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;
}
if (fs_remove_fname_from_directory(rp.parent_inode_ptr, rp.target_fname) < 0) {
pr_err("failed to unlink '%s'\n", rp.target_fname);
} else { } else {
pr("Unlinked '%s'\n", fname); pr("Unlinked '%s'\n", rp.target_fname);
} }
return 0; return 0;
@@ -1561,7 +2070,7 @@ int fs_ls(void *d)
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no); pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
@@ -1584,7 +2093,7 @@ int fs_ls(void *d)
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no); pr_stdout("%s -> inode_ptr=%d\n", recs[k].fname, recs[k].inode_no);
@@ -1619,7 +2128,7 @@ int fs_la(void *d)
read_block(dir.blocks[i], (void *) &recs); read_block(dir.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
struct fs_inode f_inode; struct fs_inode f_inode;
@@ -1654,7 +2163,7 @@ int fs_la(void *d)
read_block(ext.blocks[i], (void *) &recs); read_block(ext.blocks[i], (void *) &recs);
for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) { for (int k = 0; k < DIRECTORY_RECORDS_PER_BLOCK; k++) {
if (!recs[k].inode_no) if (!recs[k].fname[0])
continue; continue;
struct fs_inode f_inode; struct fs_inode f_inode;
@@ -1681,16 +2190,10 @@ int fs_stat(void *d)
{ {
char *fname = *((char **) d); char *fname = *((char **) d);
int file_inode_ptr; int file_inode_ptr = find_fname_in_directory(fs_cwd_inode_ptr, fname);
{ if (file_inode_ptr < 0) {
int *r = find_filename_in_directory(fs_cwd_inode_ptr, fname); pr_err("no such file: '%s'\n", fname);
if (r == NULL) { return 0;
pr_err("no such file: '%s'\n", fname);
return 0;
}
file_inode_ptr = r[2];
free(r);
} }
struct fs_inode f; struct fs_inode f;
@@ -1744,6 +2247,7 @@ int fs_use(void *d)
} }
if (write_permitted) { if (write_permitted) {
memset(fs_cwd, 0, sizeof(fs_cwd));
char *root_dir_path = "/"; char *root_dir_path = "/";
fs_chdir((void *) &root_dir_path); fs_chdir((void *) &root_dir_path);
@@ -1788,14 +2292,14 @@ int fs_mkfs(void *d)
return 0; 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); 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);
struct fs_header fsh = {}; struct fs_header fsh = {0};
fsh.version = 0x1; fsh.version = 0x1;
fsh.max_inode_count = max_inode_count; fsh.max_inode_count = max_inode_count;
fsh.block_count = block_count; fsh.block_count = block_count;
pr("header size is %d bytes, writing it to the first block\n", sizeof(fsh)); pr("header size is %ld bytes, writing it to the first block\n", sizeof(fsh));
int result = write_block(0, (void *) &fsh); int result = write_block(0, (void *) &fsh);
if (FS_BLOCK_SIZE != result) { if (FS_BLOCK_SIZE != result) {
@@ -1845,7 +2349,7 @@ finish_current_block:
// create root directory automatically // create root directory automatically
struct fs_inode root_dir = {}; struct fs_inode root_dir = {0};
root_dir.ftype = DIRECTORY; root_dir.ftype = DIRECTORY;
root_dir.ref_count = 1; root_dir.ref_count = 1;
@@ -1874,10 +2378,15 @@ finish_current_block:
// inode0 -> root_dir_block // inode0 -> root_dir_block
write_inode_ptr(0, free_block_index); write_inode_ptr(0, free_block_index);
memset(fs_cwd, 0, sizeof(fs_cwd));
char *root_dir_path = "/"; char *root_dir_path = "/";
fs_chdir((void *) &root_dir_path); fs_chdir((void *) &root_dir_path);
fs_cwd_inode_ptr = 0; fs_cwd_inode_ptr = 0;
// add . and ..
fs_add_fname_to_directory(0, 0, ".");
fs_add_fname_to_directory(0, 0, "..");
return 0; return 0;
} }