ext2: extend inode block searching
This commit is contained in:
parent
9aea93214a
commit
4c040f280f
4 changed files with 129 additions and 23 deletions
134
fs/ext2.c
134
fs/ext2.c
|
|
@ -44,6 +44,92 @@ int ext2_read_block(struct ext2fs *fs, void *buf, size_t block)
|
|||
return block_read(fs->bdev, buf, bdev_block, num_blocks_read);
|
||||
}
|
||||
|
||||
/* nasty function, but it works */
|
||||
static int ext2_inode_read_block(struct ext2fs *fs, struct ext2_inode *inode, void *buf, size_t ino_block)
|
||||
{
|
||||
if (ino_block < 12)
|
||||
return ext2_read_block(fs, buf, inode->block[ino_block]);
|
||||
|
||||
if (ino_block < 12 + fs->indir_block_size) {
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_read_block(fs, indir_buf, inode->block[12]);
|
||||
|
||||
int ret = ext2_read_block(fs, buf, indir_buf[ino_block - 12]);
|
||||
kfree(indir_buf);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ino_block < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size) {
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_read_block(fs, indir_buf, inode->block[13]);
|
||||
|
||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf2) {
|
||||
kfree(indir_buf);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ext2_read_block(fs, indir_buf2, indir_buf[(ino_block - 12 - fs->indir_block_size) / fs->indir_block_size]);
|
||||
|
||||
int ret = ext2_read_block(fs, buf, indir_buf2[(ino_block - 12 - fs->indir_block_size) % fs->indir_block_size]);
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ino_block < 12 + fs->indir_block_size + fs->indir_block_size * fs->indir_block_size +
|
||||
fs->indir_block_size * fs->indir_block_size * fs->indir_block_size) {
|
||||
uint32_t *indir_buf = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf)
|
||||
return -ENOMEM;
|
||||
|
||||
ext2_read_block(fs, indir_buf, inode->block[14]);
|
||||
|
||||
uint32_t *indir_buf2 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf2) {
|
||||
kfree(indir_buf);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ext2_read_block(fs, indir_buf2,
|
||||
indir_buf[(ino_block - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) /
|
||||
(fs->indir_block_size * fs->indir_block_size)]);
|
||||
|
||||
uint32_t *indir_buf3 = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!indir_buf3) {
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ext2_read_block(fs, indir_buf3,
|
||||
indir_buf2[((ino_block - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) /
|
||||
fs->indir_block_size]);
|
||||
|
||||
int ret = ext2_read_block(
|
||||
fs, buf,
|
||||
indir_buf3[((ino_block - 12 - fs->indir_block_size - fs->indir_block_size * fs->indir_block_size) %
|
||||
(fs->indir_block_size * fs->indir_block_size)) %
|
||||
fs->indir_block_size]);
|
||||
kfree(indir_buf);
|
||||
kfree(indir_buf2);
|
||||
kfree(indir_buf3);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int ext2_write_block(struct ext2fs *fs, void *buf, size_t block)
|
||||
{
|
||||
uint32_t bdev_block_size = fs->bdev->block_size;
|
||||
|
|
@ -122,7 +208,8 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
|||
if (!inode)
|
||||
return -ENOMEM;
|
||||
|
||||
int ret = ext2_read_inode(fs, inode, 2);
|
||||
uint32_t inode_no = 2;
|
||||
int ret = ext2_read_inode(fs, inode, inode_no);
|
||||
if (ret < 0) {
|
||||
kfree(inode);
|
||||
return ret;
|
||||
|
|
@ -138,7 +225,6 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
|||
|
||||
char *strtok_last = NULL;
|
||||
char *token = strtok(path_copy, "/", &strtok_last);
|
||||
uint32_t inode_no = 2;
|
||||
while (token) {
|
||||
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
|
||||
if (!entry) {
|
||||
|
|
@ -147,29 +233,35 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
|||
return -ENOMEM;
|
||||
}
|
||||
|
||||
ext2_read_block(fs, entry, inode->block[0]);
|
||||
for (int i = 0; i < inode->blocks; i++) {
|
||||
/* since this inode is a directory, we just read the blocks
|
||||
* sequentially until we find the file we're looking for
|
||||
*/
|
||||
|
||||
while (entry->inode) {
|
||||
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
|
||||
if (!name) {
|
||||
kfree(inode);
|
||||
kfree(path_copy);
|
||||
kfree(entry);
|
||||
return -ENOMEM;
|
||||
}
|
||||
ext2_inode_read_block(fs, inode, entry, i);
|
||||
|
||||
memcpy(name, entry->name, entry->name_len);
|
||||
name[entry->name_len] = '\0';
|
||||
while (entry->inode) {
|
||||
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
|
||||
if (!name) {
|
||||
kfree(inode);
|
||||
kfree(path_copy);
|
||||
kfree(entry);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memcpy(name, entry->name, entry->name_len);
|
||||
name[entry->name_len] = '\0';
|
||||
|
||||
if (!strcmp(name, token)) {
|
||||
kfree(name);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!strcmp(name, token)) {
|
||||
kfree(name);
|
||||
break;
|
||||
|
||||
inode_no = entry->inode;
|
||||
entry = (void *)entry + entry->rec_len;
|
||||
}
|
||||
|
||||
kfree(name);
|
||||
|
||||
inode_no = entry->inode;
|
||||
entry = (void *)entry + entry->rec_len;
|
||||
}
|
||||
|
||||
if (!entry->inode) {
|
||||
|
|
@ -184,7 +276,6 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
|||
token = strtok(NULL, "/", &strtok_last);
|
||||
}
|
||||
|
||||
|
||||
kfree(path_copy);
|
||||
|
||||
memcpy(&file->inode, inode, sizeof(struct inode));
|
||||
|
|
@ -232,6 +323,7 @@ struct fs *ext2_init_fs(struct block_device *bdev)
|
|||
|
||||
extfs->bdev = bdev;
|
||||
extfs->block_size = 1024 << extfs->sb.log_block_size;
|
||||
extfs->indir_block_size = extfs->block_size / sizeof(uint32_t);
|
||||
|
||||
bdev->fs = extfs;
|
||||
|
||||
|
|
|
|||
5
fs/vfs.c
5
fs/vfs.c
|
|
@ -72,11 +72,14 @@ int open(const char *pathname, int flags)
|
|||
if (result < 0) {
|
||||
kprintf(LOG_ERROR "Failed to open %s\n", pathname);
|
||||
return result;
|
||||
} else if (result == VFSE_IS_BDEV) {
|
||||
/* TODO: continue search into mounted filesystems */
|
||||
}
|
||||
|
||||
/* add the file descriptor to the tree */
|
||||
fd->fd = fd_counter;
|
||||
fd->fs = rootfs;
|
||||
fd->file = file;
|
||||
fd_counter += 1;
|
||||
|
||||
struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
|
||||
|
|
@ -89,7 +92,7 @@ int close(int fd)
|
|||
{
|
||||
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||
|
||||
if (!fdnode)
|
||||
if (!fdnode)
|
||||
return -EBADF;
|
||||
|
||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||
|
|
|
|||
|
|
@ -138,6 +138,8 @@ struct ext2fs {
|
|||
|
||||
struct ext2_group_desc *bgdt;
|
||||
|
||||
uint32_t indir_block_size;
|
||||
|
||||
uint32_t block_size;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,14 @@ struct inode {
|
|||
uint32_t faddr;
|
||||
} PACKED; /* PACKED to allow for memcpy */
|
||||
|
||||
struct dirent {
|
||||
uint64_t inode;
|
||||
uint64_t offset;
|
||||
uint16_t reclen;
|
||||
uint8_t type;
|
||||
char name[256];
|
||||
};
|
||||
|
||||
struct file {
|
||||
struct inode inode;
|
||||
|
||||
|
|
@ -49,9 +57,10 @@ struct file {
|
|||
struct file_descriptor {
|
||||
struct file file;
|
||||
|
||||
uint64_t pos;
|
||||
uint64_t pos;
|
||||
int fd;
|
||||
uint32_t flags;
|
||||
uint32_t mode;
|
||||
|
||||
struct fs *fs;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue