diff --git a/fs/devfs.c b/fs/devfs.c index 852c302..28af46f 100644 --- a/fs/devfs.c +++ b/fs/devfs.c @@ -90,8 +90,6 @@ struct fs *devfs_init() devfs->ops->write = devfs_write; devfs->ops->unlink = devfs_unlink; devfs->ops->open_vno = devfs_open_vno; - devfs->ops->readdir = devfs_readdir; - devfs->ops->open_dir = devfs_open_dir; devfs->root->flags = VFS_VNO_DIR; devfs->root->fs = devfs; diff --git a/fs/ext2.c b/fs/ext2.c index e5eeded..9baa83e 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -8,12 +8,16 @@ #define N_INDIR (fs->block_size / sizeof(uint32_t)) #define N_DINDIR (N_INDIR * N_INDIR) #define N_TINDIR (N_DINDIR * N_INDIR + N_DINDIR + N_INDIR) - #define N_PER_INDIRECT (fs->block_size / sizeof(uint32_t)) #define ITYPE_DECL(_ino, _de) [(_ino >> 12)] = (_de) #define DTYPE_DECL(_de, _ino) [(_de)] = (_ino) +struct ext2_block_index { + uint32_t n_indirs; + uint32_t indices[4]; +}; + static struct fs_ops *ext2_ops; static uint8_t inode_to_ftype[] = { @@ -26,6 +30,13 @@ static uint8_t inode_to_ftype[] = { #undef ITYPE_DECL #undef DTYPE_DECL +/* Base ext2 operations: + * ----------------------------------------------------------------------------- + * Superblock + * R/W block + * BGDT_write + * ----------------------------------------------------------------------------- + */ static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb_struct) { char *buf = kmalloc(1024, ALLOC_DMA); @@ -65,74 +76,6 @@ static int ext2_read_block(struct ext2fs *fs, void *buf, size_t block) return block_read(fs->bdev, buf, bdev_block, num_blocks_read); } -static long ext2_block_single_indir(struct ext2fs *fs, uint32_t dir_block, size_t index) -{ - uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!indir_buf) - return -ENOMEM; - - ext2_read_block(fs, indir_buf, dir_block); - - long ret = indir_buf[index]; - - kfree(indir_buf); - - return ret; -} - -static long ext2_block_double_indir(struct ext2fs *fs, uint32_t dir_block, size_t index, size_t index_2) -{ - uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!indir_buf) - return -ENOMEM; - - ext2_read_block(fs, indir_buf, dir_block); - - return ext2_block_single_indir(fs, indir_buf[index], index_2); -} - -static long ext2_inode_get_block(struct ext2fs *fs, struct ext2_inode *inode, size_t ino_block) -{ - const uintptr_t zone_direct_limit = 12; - const uintptr_t zone_indirect_limit = zone_direct_limit + N_INDIR; - const uintptr_t zone_dindirect_limit = zone_indirect_limit + N_DINDIR; - const uintptr_t limit = zone_dindirect_limit + N_TINDIR; - - if (ino_block >= limit) - return -EINVAL; - - if (ino_block < zone_direct_limit) { - return inode->block[ino_block]; - } else if (ino_block < zone_indirect_limit) { - return ext2_block_single_indir(fs, inode->block[N_DIRECT], ino_block - N_DIRECT); - } else if (ino_block < zone_dindirect_limit) { - size_t index = (ino_block - N_DIRECT - N_INDIR) / N_PER_INDIRECT; - size_t index_2 = (ino_block - N_DIRECT - N_INDIR) % N_PER_INDIRECT; - - return ext2_block_double_indir(fs, inode->block[N_DIRECT + 1], index, index_2); - } else if (ino_block < limit) { - size_t index = (ino_block - N_DIRECT - N_INDIR - N_DINDIR) / (N_PER_INDIRECT * N_PER_INDIRECT); - size_t index_2 = ((ino_block - N_DIRECT - N_INDIR - N_DINDIR) % (N_PER_INDIRECT * N_PER_INDIRECT)) / N_PER_INDIRECT; - size_t index_3 = ((ino_block - N_DIRECT - N_INDIR - N_DINDIR) % (N_PER_INDIRECT * N_PER_INDIRECT)) % N_PER_INDIRECT; - - long ret = ext2_block_double_indir(fs, inode->block[N_DIRECT + 2], index, index_2); - return ext2_block_single_indir(fs, ret, index_3); - } else { - return -EINVAL; - } - - return -EINVAL; -} - -static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block) -{ - long ret = ext2_inode_get_block(fs, inode, ino_block); - if (ret < 0) - return ret; - - return ext2_read_block(fs, buf, ret); -} - static int ext2_write_block(struct ext2fs *fs, void *buf, size_t block) { uint32_t bdev_block_size = fs->bdev->block_size; @@ -143,6 +86,11 @@ static int ext2_write_block(struct ext2fs *fs, void *buf, size_t block) return block_write(fs->bdev, buf, bdev_block, num_blocks_read); } +static int ext2_write_super(struct ext2fs *fs) +{ + return ext2_write_block(fs, &fs->sb, 1); +} + static int ext2_write_bgdt(struct ext2fs *fs) { for (size_t i = 0; i < fs->bgdt_num_blocks; i++) { @@ -155,20 +103,126 @@ static int ext2_write_bgdt(struct ext2fs *fs) return 0; } -static int ext2_write_super(struct ext2fs *fs) +static long ext2_find_free_block(struct ext2fs *fs, size_t block_group) { - return ext2_write_block(fs, &fs->sb, 1); + struct ext2_group_desc *bg = &fs->bgdt[block_group]; + + if (!bg->free_blocks_count) + return -ENOSPC; + + uint32_t block = bg->block_bitmap; + uint32_t block_offset = 0; + + char *buf = kmalloc(fs->block_size, ALLOC_DMA); + if (!buf) + return -ENOMEM; + + ext2_read_block(fs, buf, block); + + uint8_t *byte; + while (block_offset < fs->block_size) { + byte = (uint8_t *)buf + block_offset; + if (*byte == 0xFF) { + block_offset += 8; + } else { + break; + } + } + int j; + for (j = 0; j < 8; j++) { + if (!(*byte & (1 << j))) + break; + } + + *byte |= (1 << j); + bg->free_blocks_count--; + fs->sb.free_blocks_count--; + + ext2_write_block(fs, buf, block); + + /* update superblock and bgdt */ + ext2_write_super(fs); + ext2_write_bgdt(fs); + + kfree(buf); + + uint32_t block_ret_no = (block_group * fs->sb.blocks_per_group) + (block_offset * 8) + j + 1; + + /* zero out the block */ + char *block_buf = kmalloc(fs->block_size, ALLOC_DMA); + if (!block_buf) + return -ENOMEM; + + memset(block_buf, 0, fs->block_size); + ext2_write_block(fs, block_buf, block_ret_no); + kfree(block_buf); + + return block_ret_no; } -static int ext2_inode_write_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block) +static long ext2_alloc_block(struct ext2fs *fs) { - long ret = ext2_inode_get_block(fs, inode, ino_block); - if (ret < 0) - return ret; + uint32_t num_groups = fs->sb.blocks_count / fs->sb.blocks_per_group; + if (fs->sb.blocks_count % fs->sb.blocks_per_group) + num_groups++; - return ext2_write_block(fs, buf, ret); + spinlock_acquire(&fs->lock); + for (int i = 0; i < num_groups; i++) { + long ret = ext2_find_free_block(fs, i); + if (ret >= 0) + return ret; + } + spinlock_release(&fs->lock); + + return -ENOSPC; } +static long ext2_free_block(struct ext2fs *fs, uint32_t block) +{ + uint32_t block_group = (block - 1) / fs->sb.blocks_per_group; + uint32_t block_offset = (block - 1) % fs->sb.blocks_per_group; + + struct ext2_group_desc *bg = &fs->bgdt[block_group]; + + uint32_t block_bitmap_block = bg->block_bitmap; + uint32_t block_bitmap_offset = block_offset >> 3; + + char *buf = kmalloc(fs->block_size, ALLOC_DMA); + if (!buf) + return -ENOMEM; + + spinlock_acquire(&fs->lock); + ext2_read_block(fs, buf, block_bitmap_block); + + uint8_t *byte = (uint8_t *)buf + block_bitmap_offset; + *byte &= ~(1 << (block_offset & 7)); + bg->free_blocks_count++; + fs->sb.free_blocks_count++; + ext2_write_block(fs, buf, block_bitmap_block); + + ext2_write_super(fs); + ext2_write_bgdt(fs); + + spinlock_release(&fs->lock); + + kfree(buf); + return 0; +} + +/* inode operations: + * ----------------------------------------------------------------------------- + * R/W inode + * Get block index (helper) + * Get block number (helper) + * + * inode block operations: + * ext2_ino_read_block + * ext2_ino_write_block + * + * ext2_dno_find (helper) + * ext2_ino_find_by_name: search for a file by name + * ----------------------------------------------------------------------------- + */ static int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, ino_t inode) { uint32_t group = (inode - 1) / fs->sb.inodes_per_group; @@ -190,7 +244,6 @@ static int ext2_read_inode(struct ext2fs *fs, struct ext2_inode *out, ino_t inod } memcpy(out, buf + offset, sizeof(struct ext2_inode)); - kfree(buf); return 0; @@ -229,350 +282,160 @@ static int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, ino_t inod return 0; } -static long find_free_block(struct ext2fs *fs, size_t block_group) +static void ext2_get_block_idx(struct ext2fs *fs, struct ext2_block_index *ret, uint32_t block) { - struct ext2_group_desc *bg = &fs->bgdt[block_group]; + const uint32_t limit_direct = N_DIRECT; + const uint32_t limit_indirect = limit_direct + N_INDIR; + const uint32_t limit_dindirect = limit_indirect + N_DINDIR; + const uint32_t limit = limit_dindirect + N_TINDIR; - if (bg->free_blocks_count) { - uint32_t block = bg->block_bitmap; - uint32_t block_offset = 0; + if (block < N_DIRECT) { + ret->n_indirs = 0; + ret->indices[0] = block; + } else if (block < limit_indirect) { + ret->n_indirs = 1; + ret->indices[0] = N_DIRECT; + ret->indices[1] = block - N_DIRECT; + } else if (block < limit_dindirect) { + ret->n_indirs = 2; + ret->indices[0] = N_DIRECT + 1; + ret->indices[1] = (block - (N_DIRECT)) / N_PER_INDIRECT; + ret->indices[2] = (block - (N_DIRECT)) % N_PER_INDIRECT; + } else if (block < limit) { + ret->n_indirs = 3; + /* tindir block */ + ret->indices[0] = N_DIRECT + 2; + ret->indices[1] = (block - (N_DIRECT + N_DINDIR)) / N_PER_INDIRECT / N_PER_INDIRECT; + ret->indices[2] = (block - (N_DIRECT + N_DINDIR)) / N_PER_INDIRECT % N_PER_INDIRECT; + ret->indices[3] = (block - (N_DIRECT + N_DINDIR)) % N_PER_INDIRECT; + } else { + ret->n_indirs = -1; + } +} - char *buf = kmalloc(fs->block_size, ALLOC_DMA); +static long ext2_ino_get_blocknum(struct ext2fs *fs, struct ext2_inode *ino, uint32_t ino_blocknum, bool alloc) +{ + const uint32_t limit_direct = N_DIRECT; + const uint32_t limit_indirect = limit_direct + N_INDIR; + const uint32_t limit_dindirect = limit_indirect + N_DINDIR; + const uint32_t limit = limit_dindirect + N_TINDIR; + + struct ext2_block_index idx; + ext2_get_block_idx(fs, &idx, ino_blocknum); + + if (idx.n_indirs == -1) + return -EINVAL; + + long ret; + long last = 0; + + ret = ino->block[idx.indices[0]]; + if (ret == 0) { + if (!alloc) + return -ENOENT; + + ret = ext2_alloc_block(fs); + if (ret < 0) + return ret; + + ino->block[idx.indices[0]] = ret; + } + + for (int i = 1; i <= idx.n_indirs; i++) { + last = ret; + uint32_t *buf = kmalloc(fs->block_size, ALLOC_DMA); if (!buf) return -ENOMEM; - ext2_read_block(fs, buf, block); + ext2_read_block(fs, buf, last); - while (block_offset < fs->block_size) { - uint8_t *byte = (uint8_t *)buf + block_offset; - if (*byte == 0xFF) { - block_offset += 8; - continue; + ret = buf[idx.indices[i]]; + + if (ret == 0) { + if (!alloc) { + kfree(buf); + return -ENOENT; } - for (int j = 0; j < 8; j++) { - if (!(*byte & (1 << j))) { - *byte |= (1 << j); - bg->free_blocks_count--; - fs->sb.free_blocks_count--; - - ext2_write_block(fs, buf, block); - - /* update superblock and bgdt */ - ext2_write_super(fs); - ext2_write_bgdt(fs); - - kfree(buf); - - uint32_t block_ret_no = (block_group * fs->sb.blocks_per_group) + (block_offset * 8) + j + 1; - - /* zero out the block */ - char *block_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!block_buf) - return -ENOMEM; - - memset(block_buf, 0, fs->block_size); - ext2_write_block(fs, block_buf, block_ret_no); - kfree(block_buf); - - return block_ret_no; - } + ret = ext2_alloc_block(fs); + if (ret < 0) { + kfree(buf); + return ret; } - block_offset++; + buf[idx.indices[i]] = ret; + ext2_write_block(fs, buf, last); + + kfree(buf); } - - kfree(buf); } - return -ENOSPC; -} - -static long ext2_alloc_block(struct ext2fs *fs) -{ - uint32_t num_groups = fs->sb.blocks_count / fs->sb.blocks_per_group; - if (fs->sb.blocks_count % fs->sb.blocks_per_group) - num_groups++; - - for (int i = 0; i < num_groups; i++) { - long ret = find_free_block(fs, i); - if (ret >= 0) - return ret; - } - - return -ENOSPC; -} - -static long ext2_alloc_block_indir(struct ext2fs *fs, uint32_t dir_block, size_t index) -{ - uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!indir_buf) - return -ENOMEM; - - ext2_read_block(fs, indir_buf, dir_block); - - long ret = ext2_alloc_block(fs); - if (ret < 0) { - kfree(indir_buf); - return ret; - } - - indir_buf[index] = ret; - ext2_write_block(fs, indir_buf, dir_block); - kfree(indir_buf); - return ret; } -static long ext2_inode_alloc_block(struct ext2fs *fs, struct ext2_inode *inode, ino_t inode_no) +static long ext2_ino_read_block(struct ext2fs *fs, struct ext2_inode *ino, void *buf, uint32_t ino_blocknum) { - /* allocate inode block and handle indirect if necessary */ - long block; - size_t num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size; - - if (num_blocks < N_DIRECT) { - block = ext2_alloc_block(fs); - inode->block[num_blocks] = block; - } else if (num_blocks < N_DIRECT + N_INDIR) { - size_t index = num_blocks - N_DIRECT; - - long indir_block = inode->block[N_DIRECT]; - if (index == 0) { - indir_block = ext2_alloc_block(fs); - inode->block[N_DIRECT] = indir_block; - } - - block = ext2_alloc_block_indir(fs, indir_block, index); - } else if (num_blocks < N_DIRECT + N_INDIR + N_DINDIR) { - size_t index = (num_blocks - N_DIRECT - N_INDIR) / N_PER_INDIRECT; - size_t index2 = (num_blocks - N_DIRECT - N_INDIR) % N_PER_INDIRECT; - - long dindir_block = inode->block[N_DIRECT + 1]; - if (index == 0) { - dindir_block = ext2_alloc_block(fs); - inode->block[N_DIRECT + 1] = dindir_block; - } - - long indir_block = ext2_block_single_indir(fs, dindir_block, index); - if (index2 == 0) { - indir_block = ext2_alloc_block(fs); - uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!indir_buf) - return -ENOMEM; - - ext2_read_block(fs, indir_buf, dindir_block); - indir_buf[index] = indir_block; - ext2_write_block(fs, indir_buf, dindir_block); - kfree(indir_buf); - } - - block = ext2_alloc_block_indir(fs, indir_block, index2); - } else if (num_blocks < N_DIRECT + N_INDIR + N_DINDIR + N_TINDIR) { - size_t index = (num_blocks - N_DIRECT - N_INDIR - N_DINDIR) / (N_PER_INDIRECT * N_PER_INDIRECT); - size_t index2 = ((num_blocks - N_DIRECT - N_INDIR - N_DINDIR) % (N_PER_INDIRECT * N_PER_INDIRECT)) / N_PER_INDIRECT; - size_t index3 = ((num_blocks - N_DIRECT - N_INDIR - N_DINDIR) % (N_PER_INDIRECT * N_PER_INDIRECT)) % N_PER_INDIRECT; - - long tindir_block = inode->block[N_DIRECT + 2]; - if (index == 0) { - tindir_block = ext2_alloc_block(fs); - inode->block[N_DIRECT + 2] = tindir_block; - } - - long dindir_block = ext2_block_double_indir(fs, tindir_block, index, index2); - if (index3 == 0) { - dindir_block = ext2_alloc_block(fs); - uint32_t *dindir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!dindir_buf) - return -ENOMEM; - - ext2_read_block(fs, dindir_buf, tindir_block); - dindir_buf[index] = dindir_block; - ext2_write_block(fs, dindir_buf, tindir_block); - kfree(dindir_buf); - } - - long indir_block = ext2_block_single_indir(fs, dindir_block, index3); - if (indir_block == 0) { - indir_block = ext2_alloc_block(fs); - uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA); - if (!indir_buf) - return -ENOMEM; - - ext2_read_block(fs, indir_buf, dindir_block); - indir_buf[index3] = indir_block; - ext2_write_block(fs, indir_buf, dindir_block); - kfree(indir_buf); - } - - block = ext2_alloc_block_indir(fs, indir_block, index3); - } else { - return -ENOSPC; - } - - /* write inode to disk */ - inode->blocks += fs->block_size / fs->bdev->block_size; - ext2_write_inode(fs, inode, inode_no); - - return block; -} - -static int ext2_free_block(struct ext2fs *fs, uint32_t block_no) -{ - /* find the group that this block belongs to */ - uint32_t group = (block_no - 1) / fs->sb.blocks_per_group; - uint32_t index = (block_no - 1) % fs->sb.blocks_per_group; - - struct ext2_group_desc *bg = &fs->bgdt[group]; - - /* mark the block as free */ - char *buf = kmalloc(fs->block_size * fs->bgdt_num_blocks, ALLOC_DMA); - if (!buf) - return -ENOMEM; - - ext2_read_block(fs, buf, bg->block_bitmap); - - uint32_t byte = index / 8; - uint32_t bit = index % 8; - - uint8_t *byte_ptr = (uint8_t *)buf + byte; - *byte_ptr &= ~(1 << bit); - - bg->free_blocks_count++; - fs->sb.free_blocks_count++; - - /* update superblock and bgdt */ - ext2_write_super(fs); - ext2_write_bgdt(fs); - - ext2_write_block(fs, buf, bg->block_bitmap); - - kfree(buf); + long ret = ext2_ino_get_blocknum(fs, ino, ino_blocknum, false); + if (ret < 0) + return ret; + ext2_read_block(fs, buf, ret); return 0; } -static long ext2_alloc_inode(struct fs *vfs, uint32_t mode) +static long ext2_ino_write_block(struct ext2fs *fs, struct ext2_inode *ino, void *buf, uint32_t ino_blocknum) { - /* allocate an inode */ - struct ext2fs *fs = (struct ext2fs *)vfs->fs; + long ret = ext2_ino_get_blocknum(fs, ino, ino_blocknum, true); + if (ret < 0) + return ret; - uint32_t num_groups = fs->sb.blocks_count / fs->sb.blocks_per_group; - if (fs->sb.blocks_count % fs->sb.blocks_per_group) - num_groups++; - - for (int i = 0; i < num_groups; i++) { - /* iterate free groups for a free inode */ - struct ext2_group_desc *bg = &fs->bgdt[i]; - - if (bg->free_inodes_count == 0) - continue; - - uint8_t *inode_bitmap = kmalloc(fs->block_size, ALLOC_DMA); - if (!inode_bitmap) - return -ENOMEM; - - ext2_read_block(fs, inode_bitmap, bg->inode_bitmap); - - /* iterate over the inode bitmap to find a free inode */ - uint32_t byte = 0; - uint8_t *byte_ptr = inode_bitmap + byte; - - while (*byte_ptr == 0xFF) { - byte++; - byte_ptr = inode_bitmap + byte; - } - - uint32_t bit = __builtin_ffs(~byte) - 1; - *byte_ptr |= (1 << bit); - - ino_t inode_no = (i * fs->sb.inodes_per_group) + (byte * 8) + bit + 1; - - /* write inode bitmap back to disk */ - ext2_write_block(fs, inode_bitmap, bg->inode_bitmap); - - kfree(inode_bitmap); - - /* update superblock and bgdt */ - fs->sb.free_inodes_count--; - - bg->free_inodes_count--; - - ext2_write_super(fs); - ext2_write_bgdt(fs); - - /* create the inode */ - struct ext2_inode *inode_table = kmalloc(fs->block_size, ALLOC_DMA); - if (!inode_table) - return -ENOMEM; - - ext2_read_block(fs, inode_table, bg->inode_table); - struct ext2_inode *ino = &inode_table[(inode_no - 1) % fs->sb.inodes_per_group]; - memset(ino, 0, sizeof(*ino)); - ino->mode = mode; - - /* write inode back to disk */ - ext2_write_block(fs, inode_table, bg->inode_table); - - kfree(inode_table); - - return inode_no; - } - - return -ENOSPC; + ext2_write_block(fs, buf, ret); + return 0; } -static int ext2_free_inode(struct fs *vfs, ino_t ino_num) +static long ext2_dno_find(struct ext2fs *fs, struct ext2_inode *ino, char *name) { - struct ext2fs *fs = (struct ext2fs *)vfs->fs; + size_t n_blocks = ino->size; + if (n_blocks % fs->block_size) + n_blocks += fs->block_size; + n_blocks /= fs->block_size; - uint32_t group = (ino_num - 1) / fs->sb.inodes_per_group; - uint32_t index = (ino_num - 1) % fs->sb.inodes_per_group; - - struct ext2_group_desc *bg = &fs->bgdt[group]; - - struct ext2_inode *inode_table = kmalloc(fs->block_size, ALLOC_DMA); - if (!inode_table) + struct ext2_dir_entry *entry = kmalloc(fs->block_size * n_blocks, ALLOC_DMA); + char *entry_buf = (char *)entry; + if (!entry) return -ENOMEM; - ext2_read_block(fs, inode_table, bg->inode_table); + size_t ino_num_blocks = (ino->blocks * fs->bdev->block_size) / fs->block_size; - struct ext2_inode *ino = &inode_table[index]; + uint32_t rec_offset = 0; + for (int i = 0; i < ino_num_blocks; i++) { + /* since this ino is a directory, we just read the blocks + * sequentially until we find the file we're looking for + */ + + long ret = ext2_ino_read_block(fs, ino, entry_buf + fs->block_size * i, i); - /* free all the blocks */ - size_t num_blocks = (ino->blocks * fs->bdev->block_size) / fs->block_size; - for (size_t i = 0; i < num_blocks; i++) { - long ret = ext2_free_block(fs, ext2_inode_get_block(fs, ino, i)); if (ret < 0) { - kfree(inode_table); + kfree(entry); return ret; } } - if (num_blocks >= N_DIRECT + N_INDIR) - ext2_free_block(fs, ino->block[N_DIRECT]); - if (num_blocks >= N_DIRECT + N_INDIR + N_DINDIR) - ext2_free_block(fs, ino->block[N_DIRECT + 1]); - if (num_blocks >= N_DIRECT + N_INDIR + N_DINDIR + N_TINDIR) - ext2_free_block(fs, ino->block[N_DIRECT + 2]); + while (entry->inode && rec_offset < fs->block_size * n_blocks) { + size_t name_len = strlen(name); + int res = strncmp(entry->name, name, MIN(entry->name_len, name_len)); - memset(ino, 0, sizeof(*ino)); + if (!res && (name_len == entry->name_len || name_len == 0)) { + kfree(entry); + return entry->inode; + } + } - ext2_write_block(fs, inode_table, bg->inode_table); - - /* update superblock and bgdt */ - fs->sb.free_inodes_count++; - - bg->free_inodes_count++; - - ext2_write_super(fs); - ext2_write_bgdt(fs); - - kfree(inode_table); - - return 0; + kfree(entry); + return -ENOENT; } -static long ext2_inode_find_by_name(struct ext2fs *fs, const char *path) +static long ext2_ino_find_by_name(struct ext2fs *fs, const char *path) { struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_DMA); @@ -593,208 +456,23 @@ static long ext2_inode_find_by_name(struct ext2fs *fs, const char *path) char *strtok_last = NULL; char *token = strtok(path_copy, "/", &strtok_last); - struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); - void *oentry = entry; + while (token) { - if (!entry) { - kfree(inode); - kfree(path_copy); - return -ENOMEM; - } - /* inode blocks value is number of BDEV blocks, not EXT2 blocks. so we convert it */ - size_t ino_num_blocks = (inode->blocks * fs->bdev->block_size) / fs->block_size; - for (int i = 0; i < ino_num_blocks; i++) { - /* since this inode is a directory, we just read the blocks - * sequentially until we find the file we're looking for - */ - ext2_inode_read_block(fs, inode, entry, i); - - uint32_t rec_offset = 0; - while (entry->inode && rec_offset < fs->block_size) { - inode_no = entry->inode; - size_t token_len = strlen(token); - if (!strncmp(entry->name, token, MIN(entry->name_len, token_len))) { - if (token_len == entry->name_len || token_len == 0) { - goto found; - } - } - - entry = (void *)entry + entry->rec_len; - rec_offset += entry->rec_len; - } + ret = ext2_dno_find(fs, inode, token); + if (ret < 0) { + break; } - if (!entry || !entry->inode || ino_num_blocks == 0) { - kfree(inode); - kfree(path_copy); - kfree(oentry); - return -ENOENT; - } - -found: - ext2_read_inode(fs, inode, entry->inode); - + ext2_read_inode(fs, inode, ret); token = strtok(NULL, "/", &strtok_last); } kfree(path_copy); - kfree(oentry); - - return inode_no; -} - -static int ext2_readdir(struct vnode *vnode, struct dirent **out) -{ - struct fs *vfs = vnode->fs; - struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); - if (!inode) - return -ENOMEM; - - struct ext2fs *extfs = (struct ext2fs *)vfs->fs; - int ret = ext2_read_inode(extfs, inode, vnode->ino_num); - if (ret < 0) { - kfree(inode); - return ret; - } - - if ((inode->mode & 0xF000) != EXT2_INO_DIR) { - kfree(inode); - return -ENOTDIR; - } - - struct ext2_dir_entry *entry = kmalloc(extfs->block_size, ALLOC_DMA); - void *oentry = entry; - if (!entry) { - kfree(inode); - return -ENOMEM; - } - - /* populate the dirent array with all the entries in the inode */ - int num_entries = 0; - size_t ino_num_blocks = (inode->blocks * extfs->bdev->block_size) / extfs->block_size; - size_t dir_arr_size = ino_num_blocks * extfs->block_size; - dir_arr_size /= sizeof(struct dirent); - - struct dirent *dir_arr = kmalloc(dir_arr_size * sizeof(struct dirent), ALLOC_KERN); - if (!dir_arr) { - kfree(inode); - kfree(entry); - return -ENOMEM; - } - - for (int i = 0; i < ino_num_blocks; i++) { - ext2_inode_read_block(extfs, inode, entry, i); - - uint32_t rec_offset = 0; - while (rec_offset < extfs->block_size) { - if (entry->inode == 0) { - rec_offset += 1; - continue; - } - - struct dirent *dirent = &dir_arr[num_entries]; - - dirent->inode = entry->inode; - dirent->type = inode_to_ftype[entry->file_type]; - dirent->reclen = entry->rec_len; - - memcpy(dirent->name, entry->name, MIN(255, entry->name_len)); - dirent->name[MIN(255, entry->name_len)] = '\0'; - - num_entries++; - - entry = (void *)entry + entry->rec_len; - rec_offset += entry->rec_len; - } - } - - /* copy to a fixed-size array */ - struct dirent *dirents = kmalloc(num_entries * sizeof(struct dirent), ALLOC_KERN); - for (int i = 0; i < num_entries; i++) { - dirents[i] = dir_arr[i]; - } - kfree(dir_arr); - kfree(inode); - kfree(oentry); - *out = dirents; - return num_entries; -} - -static int ext2_write_file(struct vnode *vnode, void *buf, size_t offset, size_t count) -{ - struct fs *vfs = vnode->fs; - struct ext2fs *fs = vfs->fs; - struct ext2_inode inode; - - int res = ext2_read_inode(fs, &inode, vnode->ino_num); - if (res < 0) - return res; - - if ((inode.flags & 0xF000) != EXT2_INO_REG) - return -EISDIR; - - size_t cur_num_blocks = (inode.blocks * fs->block_size) / fs->bdev->block_size; - size_t block = offset / fs->block_size; - size_t ooffset = offset; - - if (offset > vnode->size) - return -EINVAL; - - if (offset + count > vnode->size) { - /* allocate more blocks */ - size_t new_num_blocks = npow2(offset + count) / fs->block_size; - new_num_blocks -= MIN(cur_num_blocks, new_num_blocks); - - for (size_t i = 0; i < new_num_blocks; i++) { - long block_no = ext2_inode_alloc_block(fs, (struct ext2_inode *)&inode, vnode->ino_num); - if (block_no < 0) - return block_no; - } - } - - offset %= fs->block_size; - void *block_buf = kmalloc(fs->block_size, ALLOC_KERN); - - if (!block_buf) - return -ENOMEM; - - size_t bufoffset = 0; - size_t num = 0; - - while (count > 0) { - int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&inode, block_buf, block); - if (retval < 0) { - kfree(block_buf); - return retval; - } - - memcpy(block_buf + offset, buf + bufoffset, MIN(count, fs->block_size - offset)); - bufoffset += fs->block_size; - offset = 0; - - retval = ext2_inode_write_block(fs, (struct ext2_inode *)&inode, block_buf, block); - if (retval < 0) { - kfree(block_buf); - return retval; - } - - /* adjust count accounting for the fact that we may have written less than a block */ - num += MIN(fs->block_size - offset, count); - count -= MIN(fs->block_size - offset, count); - offset = 0; - block++; - } - - kfree(block_buf); - - inode.size = MAX(vnode->size, num + ooffset); - ext2_write_inode(fs, (struct ext2_inode *)&inode, vnode->ino_num); - - return num; + return ret; } static int ino_type_to_dent(uint32_t ino_flags) @@ -820,148 +498,74 @@ static int ino_type_to_dent(uint32_t ino_flags) return -1; } -static int ext2_unlink(struct fs *vfs, const char *path) +/* VFS operations: + * ----------------------------------------------------------------------------- + * read dirs (helper) + * open vno + * read file + * write file + * unlink + * mkdir + * ----------------------------------------------------------------------------- + */ + +static long ext2_vno_read_dirs(struct ext2fs *fs, struct ext2_inode *inode, struct vnode *out) { - struct ext2fs *fs = (struct ext2fs *)vfs->fs; + size_t n_blocks = inode->size; + if (n_blocks % fs->block_size) + n_blocks += fs->block_size; + n_blocks /= fs->block_size; - char *path_copy = kzalloc(strlen(path) + 1, ALLOC_KERN); - if (!path_copy) + void *entry_buf = kmalloc(fs->block_size * n_blocks, ALLOC_DMA); + if (!entry_buf) return -ENOMEM; - strcpy(path_copy, path); + size_t ino_num_blocks = inode->size; + if (ino_num_blocks % fs->block_size) + ino_num_blocks += fs->block_size; + ino_num_blocks /= fs->block_size; - char *base = basename(path_copy); - char *dir = dirname(path_copy); - - long parent_ino_num; - if (dir == NULL) - parent_ino_num = EXT2_ROOT_INO; - else - parent_ino_num = ext2_inode_find_by_name(vfs->fs, dir); - - if (parent_ino_num < 0) { - kfree(path_copy); - return parent_ino_num; - } - - struct ext2_inode *dir_inode = kmalloc(sizeof(struct ext2_inode), ALLOC_DMA); - if (!dir_inode) { - kfree(path_copy); - return -ENOMEM; - } - - ext2_read_inode(vfs->fs, dir_inode, parent_ino_num); - - struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA); - if (!entry) { - kfree(path_copy); - kfree(dir_inode); - return -ENOMEM; - } - - size_t baselen = strlen(base); - size_t num_blocks = (dir_inode->blocks * fs->bdev->block_size) / fs->block_size; - - for (size_t i = 0; i < num_blocks; i++) { - ext2_inode_read_block(fs, dir_inode, entry, i); - - uint32_t rec_offset = 0; - uint32_t last_len = 0; - while (rec_offset < fs->block_size) { - struct ext2_dir_entry *d_ent = (void *)entry + rec_offset; - - if (d_ent->name_len == baselen && !strncmp(d_ent->name, base, strlen(base))) { - /* free the inode */ - struct ext2_inode inode; - ext2_read_inode(fs, &inode, d_ent->inode); - - if (inode.links_count == 1) { - ext2_free_inode(vfs, d_ent->inode); - } else { - inode.links_count--; - ext2_write_inode(fs, &inode, d_ent->inode); - } - - /* if this entry is not the first entry, we add its rec_len to the previous entry */ - if (last_len) { - struct ext2_dir_entry *last_ent = (void *)entry + rec_offset - last_len; - last_ent->rec_len += d_ent->rec_len; - } - - /* remove the entry */ - memset(d_ent, 0, d_ent->rec_len); - - /* write the block back to disk */ - ext2_inode_write_block(fs, dir_inode, entry, i); - - kfree(path_copy); - kfree(dir_inode); - kfree(entry); - - return 0; - } - - last_len = d_ent->rec_len; - rec_offset += d_ent->rec_len; + /* read the directory entries */ + for (int i = 0; i < ino_num_blocks; i++) { + long ret = ext2_ino_read_block(fs, inode, entry_buf + fs->block_size * i, i); + if (ret < 0) { + kfree(entry_buf); + return ret; } } - kfree(path_copy); - kfree(dir_inode); - kfree(entry); + void *oe = entry_buf; + size_t offset = 0; - return -ENOENT; -} + while (offset < fs->block_size * n_blocks) { + struct ext2_dir_entry *entry = (struct ext2_dir_entry *)((char *)entry_buf + offset); + struct dirent dentry = { 0 }; -static int ext2_read_file(struct vnode *vnode, void *buf, size_t offset, size_t count) -{ - struct fs *vfs = vnode->fs; - struct ext2fs *fs = vfs->fs; - struct ext2_inode inode; - - int res = ext2_read_inode(fs, &inode, vnode->ino_num); - if (res < 0) - return res; - - if ((inode.mode & 0xF000) != EXT2_INO_REG) - return -EISDIR; - - size_t block = offset / fs->block_size; - - if (offset > vnode->size) - return -EINVAL; - - if (offset + count > vnode->size) - count = (vnode->size - offset); - - offset %= fs->block_size; - - size_t ret = count; - - void *block_buf = kmalloc(fs->block_size, ALLOC_KERN); - - if (!block_buf) - return -ENOMEM; - - size_t bufoffset = 0; - - while (count > 0) { - int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&inode, block_buf, block); - if (retval < 0) { - kfree(block_buf); - return retval; + if (entry->inode == 0 || entry->name_len == 0) { + break; } - memcpy(buf + bufoffset, block_buf + offset, MIN(count, fs->block_size - offset)); - bufoffset += fs->block_size; - offset = 0; + memcpy(&dentry.name, entry->name, entry->name_len); + dentry.name[entry->name_len] = '\0'; + dentry.inode = entry->inode; + dentry.type = ino_type_to_dent(entry->file_type); - count -= MIN(fs->block_size, count); + /* insert into dirents linked list */ + out->dirents = krealloc(out->dirents, (out->num_dirents + 1) * sizeof(struct dirent), ALLOC_KERN); + if (!out->dirents) { + kfree(oe); + return -ENOMEM; + } - block++; + memcpy(&out->dirents[out->num_dirents], &dentry, sizeof(struct dirent)); + + out->num_dirents++; + offset += entry->rec_len; } - return ret; + kfree(entry_buf); + + return 0; } static int ext2_open_vno(struct fs *vfs, struct vnode *out, ino_t ino_num) @@ -984,28 +588,52 @@ static int ext2_open_vno(struct fs *vfs, struct vnode *out, ino_t ino_num) /* Init to NULL, allow vfs to fill in */ out->ptr = NULL; out->next = NULL; - out->dirents = NULL; - return res; -} - -static int ext2_open_dir(struct fs *vfs, struct vnode *vnode) -{ - /* read dirents from the inode */ - if ((vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) - return -ENOTDIR; - - struct dirent *dirents; - ssize_t num_entries = ext2_readdir(vnode, &dirents); - if (num_entries < 0) - return num_entries; - - vnode->dirents = dirents; - vnode->num_dirents = num_entries; + if (inode.mode & EXT2_INO_DIR) { + /* read the directory entries */ + if ((res = ext2_vno_read_dirs(fs, &inode, out)) < 0) + return res; + } return 0; } +int ext2_read_file(struct vnode *vnode, void *buf, size_t offset, size_t size) +{ + struct fs *fs = vnode->fs; + struct ext2fs *extfs = (struct ext2fs *)fs->fs; + + struct ext2_inode inode; + ext2_read_inode(extfs, &inode, vnode->ino_num); + + if (offset > inode.size) + return -EINVAL; + + if (offset + size > inode.size) + size = inode.size - offset; + + size_t first_block = offset / extfs->block_size; + size_t last_block = (offset + size) / extfs->block_size; + size_t n_blocks_read = (last_block - first_block) + 1; + + char *block_buf = kmalloc(n_blocks_read * extfs->block_size, ALLOC_DMA); + if (!block_buf) + return -ENOMEM; + + for (size_t i = first_block; i <= last_block; i++) { + long ret = ext2_ino_read_block(extfs, &inode, block_buf + (i * extfs->block_size), i); + if (ret < 0) { + kfree(block_buf); + return ret; + } + } + + memcpy(buf, block_buf + (offset % extfs->block_size), size); + kfree(block_buf); + + return size; +} + struct fs *ext2_init_fs(struct block_device *bdev) { struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN); @@ -1053,11 +681,12 @@ struct fs *ext2_init_fs(struct block_device *bdev) goto out; ext2_ops->open_vno = ext2_open_vno; - ext2_ops->open_dir = ext2_open_dir; ext2_ops->read = ext2_read_file; + /* ext2_ops->write = ext2_write_file; - ext2_ops->readdir = ext2_readdir; ext2_ops->unlink = ext2_unlink; + ext2_ops->mkdir = ext2_mkdir; + */ ret->ops = ext2_ops; @@ -1080,6 +709,8 @@ struct fs *ext2_init_fs(struct block_device *bdev) ret->root->ino_num = EXT2_ROOT_INO; ret->root->no_free = true; + ext2_vno_read_dirs(extfs, root_inode, ret->root); + return ret; out_3: diff --git a/fs/vfs.c b/fs/vfs.c index af4a648..f403a33 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -132,15 +132,6 @@ struct file *vfs_open(const char *pathname, int *err) goto out_2; } - if (new_vnode->flags & VFS_VNO_DIR) { - int res = fs->ops->open_dir(new_vnode->fs, new_vnode); - if (res < 0) { - ATTEMPT_WRITE(err, res); - slab_free(vnode_slab, new_vnode); - goto out_2; - } - } - cur_vnode = new_vnode; entry->vnode = cur_vnode; } @@ -213,7 +204,6 @@ ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) if (off + count > file->vnode->size) count = file->vnode->size - off; - return file->vnode->fs->ops->read(file->vnode, buf, off, count); } @@ -456,14 +446,8 @@ struct fs *vfs_mount_root(const char *dev, const char *mount_point) strcpy(fs->mount_point, mount_point); struct vnode *root = fs->root; - struct dirent *dirents = NULL; - int num_dirents = fs->ops->readdir(root, &dirents); - if (num_dirents < 0) - goto out_dealloc_fs; - - root->dirents = dirents; - root->num_dirents = num_dirents; root->no_free = true; + return fs; out_dealloc_fs: diff --git a/include/fs/ext2.h b/include/fs/ext2.h index 8813b00..f87d1a5 100644 --- a/include/fs/ext2.h +++ b/include/fs/ext2.h @@ -146,6 +146,8 @@ struct ext2fs { uint32_t bgdt_num_blocks; struct ext2_group_desc *bgdt; + spinlock_t lock; + uint32_t indir_block_size; uint32_t block_size; diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 8f7171d..93115bc 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -117,10 +117,8 @@ struct file_descriptor { struct fs_ops { int (*read)(struct vnode *vnode, void *buf, size_t offset, size_t size); int (*write)(struct vnode *vnode, void *buf, size_t offset, size_t size); - int (*readdir)(struct vnode *vnode, struct dirent **dir); int (*unlink)(struct fs *fs, const char *path); /* change to vno, not path) */ int (*open_vno)(struct fs *vfs, struct vnode *out, ino_t ino_num); - int (*open_dir)(struct fs *vfs, struct vnode *out); }; struct fs {