2015-11-07 89 views
0

我试图访问在linux/fs.h中定义的超级块对象。 但是如何初始化对象以便我们可以访问它的属性。 我发现alloc_super()用于初始化超级,但它是如何调用的?在系统调用中访问Linux内核的SuperBlock对象

#include <fcntl.h> 
    #include <unistd.h> 
    #include <stdio.h> 
    #include <sys/stat.h> 
    #include <sys/types.h> 
    #include <errno.h> 
    #include <linux/fs.h> 




    int main(){ 

    printf("hello there"); 

    struct super_block *sb; 

    return 0; 

    } 
+0

'super_block'结构描述安装的文件系统。您需要引用该文件系统中的任何对象:inode,file或dentry;相应的'super_block'可以通过该对象的字段访问。 – Tsyvarev

回答

1

答案是非常依赖文件系统,因为不同的文件系统将有不同的超级块布局和实际上不同的块安排。例如,ext2文件系统超级块位于磁盘上的已知位置(字节1024),并且具有已知的大小(sizeof(结构超级块)字节)。

因此,一个典型的实现(这不是一个工作的代码,但有细微的修改,可向工作),你想会是什么

struct superblock *read_superblock(int fd) { 

    struct superblock *sb = malloc(sizeof(struct superblock)); 
    assert(sb != NULL); 

    lseek(fd, (off_t) 1024, SEEK_SET)); 
    read(fd, (void *) sb, sizeof(struct superblock)); 

    return sb; 
} 

现在,你可以使用Linux的Alloc超级/头文件,或者编写与ext2/ext3/etc/etc文件系统超级块完全匹配的自己的结构。

然后你必须知道在哪里可以找到超级块(lseek()来到这里)。

此外,您还需要将磁盘文件名称file_descriptor传递给函数。

所以做一个

int fd = open(argv[1], O_RDONLY);

struct superblock * sb = read_superblock(fd); 
相关问题