Compare commits
2 Commits
ef7f9ff553
...
4f610340f5
Author | SHA1 | Date | |
---|---|---|---|
4f610340f5 | |||
4de89d57f1 |
163
src/fs.c
163
src/fs.c
@ -313,54 +313,43 @@ int fs_prohibit_write(void *d)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct fs_directory_record *fs_read_dir(unsigned int fs_inode_ptr)
|
||||
{
|
||||
unsigned int inode_block_no = read_inode_ptr(fs_inode_ptr);
|
||||
struct fs_inode dir_inode;
|
||||
read_block(inode_block_no, &dir_inode);
|
||||
|
||||
if (dir_inode.size == 0) {
|
||||
pr("directory is empty\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct fs_header fsh;
|
||||
read_block(0, (void *) &fsh);
|
||||
|
||||
struct fs_directory_record *recs = malloc(fsh.max_inode_count * sizeof(struct fs_directory_record));
|
||||
memset(recs, 0, fsh.max_inode_count * sizeof(struct fs_directory_record));
|
||||
|
||||
// read block portion from dir_inode
|
||||
for (int i = 0; (i*DIRECTORY_RECORDS_PER_BLOCK < dir_inode.size) && (i < BLOCK_ADDRESSES_PER_INODE); i++) {
|
||||
if (dir_inode.blocks[i]) {
|
||||
read_block(dir_inode.blocks[i], &(recs[i*DIRECTORY_RECORDS_PER_BLOCK]));
|
||||
}
|
||||
}
|
||||
|
||||
// read block portions from inode extensions
|
||||
// TODO
|
||||
|
||||
return recs;
|
||||
}
|
||||
|
||||
static int fs_find_free_directory_record(unsigned int dir_inode_ptr)
|
||||
{
|
||||
struct fs_directory_record *recs = fs_read_dir(dir_inode_ptr);
|
||||
if (!recs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct fs_header fsh;
|
||||
read_block(0, (void *) &fsh);
|
||||
|
||||
for (unsigned int i = 0; i < fsh.max_inode_count; i++) {
|
||||
if (!recs[i].inode_no) {
|
||||
free(recs);
|
||||
return i;
|
||||
struct fs_inode dir;
|
||||
read_block(read_inode_ptr(dir_inode_ptr), (void *) &dir);
|
||||
|
||||
int found_block_no = 0;
|
||||
|
||||
// search in base inode
|
||||
for (unsigned int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
||||
struct fs_directory_record r[DIRECTORY_RECORDS_PER_BLOCK];
|
||||
|
||||
if (!dir.blocks[i]) {
|
||||
if (found_block_no < fsh.max_inode_count)
|
||||
return (found_block_no);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
read_block(dir.blocks[i], (void *) &r);
|
||||
|
||||
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;
|
||||
|
||||
return found_block_no;
|
||||
}
|
||||
}
|
||||
|
||||
free(recs);
|
||||
// search in inode extensions
|
||||
// TODO
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -494,6 +483,7 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
|
||||
|
||||
pr("Directory record for '%s' found in block=%d, record=%d, removing\n", fname, i, k);
|
||||
|
||||
unsigned int inode_ptr_cache = recs[k].inode_no;
|
||||
unsigned int inode_location_cache = read_inode_ptr(recs[k].inode_no);
|
||||
|
||||
// decrement ref_count
|
||||
@ -502,54 +492,53 @@ static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fnam
|
||||
f.ref_count--;
|
||||
write_block(read_inode_ptr(recs[k].inode_no), (void *) &f);
|
||||
|
||||
// if it drops to zero, nullify inode_ptr pointing to this inode
|
||||
if (!f.ref_count) {
|
||||
pr("ref_count=0, clearing inode_ptr\n");
|
||||
write_inode_ptr(recs[k].inode_no, 0);
|
||||
|
||||
// if no fd reference this inode, clean it up altogether
|
||||
int i;
|
||||
for (i = 0; i < FS_MAX_OPEN_FD; i++)
|
||||
if (fs_file_descriptions[i].inode == inode_location_cache)
|
||||
break;
|
||||
|
||||
if (i == FS_MAX_OPEN_FD) {
|
||||
pr("No open fd reference inode %d, cleaning up\n", inode_location_cache);
|
||||
|
||||
struct fs_inode f;
|
||||
read_block(inode_location_cache, (void *) &f);
|
||||
|
||||
// clear blocks referenced in base inode
|
||||
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++) {
|
||||
if (f.blocks[i])
|
||||
mark_free(f.blocks[i]);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pr("Inode %d is still referenced by fd %d, not removing it\n",
|
||||
inode_location_cache, i);
|
||||
}
|
||||
}
|
||||
|
||||
// clear directory record inode_ptr
|
||||
recs[k].inode_no = 0;
|
||||
write_block(dir.blocks[i], (void *) &recs);
|
||||
|
||||
// if it drops to zero, nullify inode_ptr pointing to this inode
|
||||
if (f.ref_count)
|
||||
goto fs_remove_fname_from_directory_finish;
|
||||
|
||||
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);
|
||||
goto fs_remove_fname_from_directory_finish;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
goto fs_remove_fname_from_directory_finish;
|
||||
}
|
||||
}
|
||||
@ -781,7 +770,7 @@ int fs_ln(void *d)
|
||||
// list entries from inode extensions
|
||||
// TODO
|
||||
|
||||
pr_err("no such file '%d'\n", existing_fname);
|
||||
pr_err("no such file '%s'\n", existing_fname);
|
||||
return 0;
|
||||
|
||||
original_inode_ptr_found:
|
||||
@ -789,10 +778,10 @@ original_inode_ptr_found:
|
||||
|
||||
// register new filename with the same inode_ptr
|
||||
if (fs_add_fname_to_directory(fs_cwd_inode_ptr, original_inode_ptr, new_fname) < 0) {
|
||||
pr_err("failed to register filename in directory '%s'\n", fs_cwd_inode_ptr);
|
||||
pr_err("failed to register filename in directory '%s'\n", fs_cwd);
|
||||
return 0;
|
||||
} else {
|
||||
pr("Registered new filename in directory '%s'\n", fs_cwd_inode_ptr);
|
||||
pr("Registered new filename in directory '%s'\n", fs_cwd);
|
||||
}
|
||||
|
||||
// update ref_count in file inode
|
||||
|
Loading…
x
Reference in New Issue
Block a user