|
|
|
@@ -107,7 +107,7 @@ static void mark_free(unsigned int block_no)
|
|
|
|
|
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);
|
|
|
|
|
pr_err("block %d is out of filesystem block range (%d)\n", block_no, fsh.block_count);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -124,7 +124,7 @@ static void mark_free(unsigned int block_no)
|
|
|
|
|
write_block(bitmap_block_index+1, (void *) bitmap_block);
|
|
|
|
|
|
|
|
|
|
pr("Marked block_no=%d (block=%d, offset=%d, bit=%d) as free\n",
|
|
|
|
|
block_no, bitmap_block_index, bitmap_block_offset, bitmap_bit);
|
|
|
|
|
block_no, bitmap_block_index, bitmap_block_offset, block_no & 0x7);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int identify_fs(void)
|
|
|
|
@@ -532,9 +532,86 @@ static int *find_filename_in_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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++) {
|
|
|
|
|
if (!recs[k].inode_no)
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int target_inode_ptr, char *fname)
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
@@ -689,6 +766,45 @@ static int fs_add_fname_to_directory(unsigned int dir_inode_ptr, unsigned int ta
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// clear directory record inode_ptr
|
|
|
|
|
recs[k].inode_no = 0;
|
|
|
|
|
write_block(dir_block_addr, (void *) recs);
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fs_remove_fname_from_directory(unsigned int dir_inode_ptr, char *fname)
|
|
|
|
|
{
|
|
|
|
|
// find directory record with this fname
|
|
|
|
@@ -715,68 +831,50 @@ 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
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fs_remove_fname_from_directory_removal(recs, k, dir.blocks[i]);
|
|
|
|
|
|
|
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// list entries from inode extension
|
|
|
|
|
// TODO
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
|
|
|
|
|
unsigned int next_ext = dir.next_extension;
|
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
|
|
unsigned int ext_no = 0;
|
|
|
|
|
|
|
|
|
|
while (next_ext) {
|
|
|
|
|
ext_no++;
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
|
read_block(curr_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])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
read_block(ext.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;
|
|
|
|
|
|
|
|
|
|
pr("Directory record for '%s' found in ext=%d, block=%d, record=%d, removing\n",
|
|
|
|
|
fname, ext_no, i, k);
|
|
|
|
|
|
|
|
|
|
fs_remove_fname_from_directory_removal(recs, k, ext.blocks[i]);
|
|
|
|
|
|
|
|
|
|
goto fs_remove_fname_from_directory_finish;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pr_err("no such file '%s'\n", fname);
|
|
|
|
|
return -1;
|
|
|
|
@@ -855,6 +953,11 @@ static void read_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|
|
|
|
else
|
|
|
|
|
memset(buf, 0, FS_BLOCK_SIZE);
|
|
|
|
|
} else {
|
|
|
|
|
/*
|
|
|
|
|
* TODO: rewrite this potentially broken abomination normally
|
|
|
|
|
* it has not caused issues so far, but you can never be too sure
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
@@ -922,22 +1025,20 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
memset(&ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
|
write_block(new_block, (void *) &ext);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
unsigned int next_ext = f.next_extension;
|
|
|
|
|
unsigned int curr_ext = 0;
|
|
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
|
|
|
|
|
// seek to target extension address
|
|
|
|
|
for (
|
|
|
|
|
target_extension_address = previous_extension_address, i = 0;
|
|
|
|
|
(i < ext_no);
|
|
|
|
|
i++
|
|
|
|
|
) {
|
|
|
|
|
if (!target_extension_address) {
|
|
|
|
|
for (unsigned int i = 0; i < ext_no + 1; i++) {
|
|
|
|
|
if (!next_ext) {
|
|
|
|
|
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",
|
|
|
|
@@ -946,61 +1047,21 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
ext.next_extension = new_block;
|
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
|
|
|
|
|
// 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 new_ext;
|
|
|
|
|
memset(&new_ext, 0, sizeof(struct fs_inode_extension));
|
|
|
|
|
write_block(new_block, (void *) &new_ext);
|
|
|
|
|
|
|
|
|
|
target_extension_address = new_block;
|
|
|
|
|
next_ext = new_block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct fs_inode_extension ext;
|
|
|
|
|
read_block(target_extension_address, (void *) &ext);
|
|
|
|
|
previous_extension_address = target_extension_address;
|
|
|
|
|
target_extension_address = ext.next_extension;
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
|
next_ext = 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) {
|
|
|
|
@@ -1011,12 +1072,11 @@ static int write_fd_block(unsigned int fd, unsigned int block_index, unsigned ch
|
|
|
|
|
mark_used(new_data_block);
|
|
|
|
|
|
|
|
|
|
ext.blocks[ext_offset] = new_data_block;
|
|
|
|
|
write_block(target_extension_address, (void *) &ext);
|
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write_block(ext.blocks[ext_offset], (void *) buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fs_read(void *d)
|
|
|
|
@@ -1104,25 +1164,40 @@ int fs_write(void *d)
|
|
|
|
|
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 int total_bytes_written = 0;
|
|
|
|
|
|
|
|
|
|
unsigned char data_buffer[FS_BLOCK_SIZE];
|
|
|
|
|
|
|
|
|
|
if (block_offset + str_len <= FS_BLOCK_SIZE) {
|
|
|
|
|
// first block
|
|
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
|
|
|
|
memcpy(&(data_buffer[block_offset]), str, str_len);
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
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) {
|
|
|
|
|
read_fd_block(fd, block_index, data_buffer);
|
|
|
|
|
memcpy(&(data_buffer[block_offset]), str, FS_BLOCK_SIZE - block_offset);
|
|
|
|
|
memcpy(data_buffer, &(str[total_bytes_written]), str_len - total_bytes_written);
|
|
|
|
|
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);
|
|
|
|
|
total_bytes_written += (str_len - total_bytes_written);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
struct fs_inode f;
|
|
|
|
|
read_block(fs_file_descriptions[fd].inode, (void *) &f);
|
|
|
|
@@ -1179,7 +1254,7 @@ int fs_truncate(void *d)
|
|
|
|
|
|
|
|
|
|
// look through base inode blocks
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE; i++, blocks_seen++) {
|
|
|
|
|
if ((blocks_seen > new_block_amount) && (f.blocks[i])) {
|
|
|
|
|
if ((blocks_seen >= new_block_amount) && (f.blocks[i])) {
|
|
|
|
|
mark_free(f.blocks[i]);
|
|
|
|
|
f.blocks[i] = 0;
|
|
|
|
|
}
|
|
|
|
@@ -1195,7 +1270,7 @@ int fs_truncate(void *d)
|
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < BLOCK_ADDRESSES_PER_INODE_EXTENSION; i++, blocks_seen++) {
|
|
|
|
|
if ((blocks_seen > new_block_amount) && (ext.blocks[i])) {
|
|
|
|
|
if ((blocks_seen >= new_block_amount) && (ext.blocks[i])) {
|
|
|
|
|
mark_free(ext.blocks[i]);
|
|
|
|
|
ext.blocks[i] = 0;
|
|
|
|
|
}
|
|
|
|
@@ -1206,7 +1281,11 @@ int fs_truncate(void *d)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// look through inode extensions themselves
|
|
|
|
|
int required_extensions = (f.size - BLOCK_ADDRESSES_PER_INODE) / BLOCK_ADDRESSES_PER_INODE_EXTENSION;
|
|
|
|
|
int required_blocks_after_base_inode = (int) f.size - (int) BLOCK_ADDRESSES_PER_INODE;
|
|
|
|
|
int required_extensions = required_blocks_after_base_inode >= 0
|
|
|
|
|
? required_blocks_after_base_inode / BLOCK_ADDRESSES_PER_INODE_EXTENSION
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
if ((f.size - BLOCK_ADDRESSES_PER_INODE) % BLOCK_ADDRESSES_PER_INODE_EXTENSION)
|
|
|
|
|
required_extensions++;
|
|
|
|
|
|
|
|
|
@@ -1216,23 +1295,23 @@ int fs_truncate(void *d)
|
|
|
|
|
// zero base inode next_extension ptr
|
|
|
|
|
f.next_extension = 0;
|
|
|
|
|
write_block(read_inode_ptr(file_inode_ptr), (void *) &f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// seek to last required extension
|
|
|
|
|
for (int i = 0; i < required_extensions; i++) {
|
|
|
|
|
if (!next_ext)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
curr_ext = next_ext;
|
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
|
read_block(curr_ext, (void *) &ext);
|
|
|
|
|
next_ext = ext.next_extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove next_extension ptr
|
|
|
|
|
ext.next_extension = 0;
|
|
|
|
|
write_block(curr_ext, (void *) &ext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// erase all extensions after this one
|
|
|
|
|
// erase all remaining extensions
|
|
|
|
|
while (next_ext) {
|
|
|
|
|
mark_free(next_ext);
|
|
|
|
|
read_block(next_ext, (void *) &ext);
|
|
|
|
@@ -1258,42 +1337,12 @@ int fs_close(void *d)
|
|
|
|
|
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)
|
|
|
|
|
if (inode_is_referenced(inode_location_cache))
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
clean_inode(inode_location_cache);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1424,7 +1473,31 @@ int fs_ln(void *d)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// list entries from inode extensions
|
|
|
|
|
// TODO
|
|
|
|
|
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 (dir.blocks[i]) {
|
|
|
|
|
read_block(ext.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pr_err("no such file '%s'\n", existing_fname);
|
|
|
|
|
return 0;
|
|
|
|
|