对于大学作业,如果文件小于 60 字节,我们必须修改 ext2 文件系统以将文件存储在 inode 的块指针中,并在文件增长到大于该值时移动到常规块存储。
我从 2.6 linux 内核源代码(按照说明)复制了 ext2 代码,然后从那里开始。
当文件大于 60 字节时,我需要将当前在 inode 的块指针数组中的所有数据复制到实际块中。所以,我需要将内核内存写入 ext2 块。一个简单的调用在do_sync_write
这里不起作用,因为它需要用户空间内存。
我已经查看了的实现,do_sync_write
但我不确定如何复制它的功能,而是使用内核内存。
这是我当前对此特定部分的实现(不起作用):
ssize_t extmod_write(struct file *filp, const char *buf,
size_t len, loff_t *ppos)
{
...
printk(KERN_INFO "Switching to regular file");
temp = kmalloc(inode->i_size, GFP_KERNEL);
memcpy(temp, EXT2_I(inode)->i_data, inode->i_size);
/* Need to clear the block pointers before they are allocated by kernel */
memset(EXT2_I(inode)->i_data, 0, sizeof(EXT2_I(inode)->i_data));
if (do_sync_write(filp, temp, inode->i_size, &dummy) < 0) {
printk(KERN_INFO "DAMN! Writing current buffer failed");
return -EINVAL;
}
kfree(temp);
return do_sync_write(filp, buf, len, ppos);
编辑:
我查看了符号链接。基本上,ext2 具有“快速符号链接”的概念;即链接名称的长度小于 60 字节。如果它是快速符号链接,则数据将存储在块指针中。这很容易做到,我已经为常规文件实现了这一点。如果链接不是快速符号链接,则数据的处理方式与常规文件相同。我想我又回到了原点。