Linux kernel 在系统调用中访问linux内核的SuperBlock对象

Linux kernel 在系统调用中访问linux内核的SuperBlock对象,linux-kernel,filesystems,system-calls,Linux Kernel,Filesystems,System Calls,我试图访问linux/fs.h中定义的超级块对象。 但是如何初始化对象以便我们可以访问它的属性。 我发现alloc_super()用于初始化super,但它是如何调用的 #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include

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

    #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;

    }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(){
printf(“你好”);
struct super_block*sb;
返回0;
}

答案在很大程度上取决于文件系统,因为不同的文件系统将具有不同的超级块布局,并且实际上块的排列也不同

例如,ext2文件系统超级块位于磁盘上的已知位置(字节1024),并且具有已知大小(sizeof(struct superblock)字节)

因此,一个典型的实现(这不是一个工作代码,但只需稍加修改即可工作)您想要的是:

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/headers来alloc superblock,或者编写与ext2/ext3/etc/etc文件系统superblock完全匹配的自己的结构

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

您还需要将磁盘文件名file_描述符传递给函数

我也是

intfd=open(argv[1],仅限ordu)


super_block
结构描述了装载的文件系统。您需要获取对该文件系统中任何对象的引用:inode、file或dentry;可以通过该对象的字段访问相应的
super_块
struct superblock * sb = read_superblock(fd);