【问题标题】:Accessing SuperBlock object of linux kernel in a system call在系统调用中访问 Linux 内核的 SuperBlock 对象
【发布时间】:2016-02-08 14:48:20
【问题描述】:

我正在尝试访问在 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;

    }

【问题讨论】:

  • super_block 结构描述了挂载的文件系统。您需要获取对该文件系统中任何对象的引用:inode、file 或 dentry;对应的super_block可以通过该对象的字段访问。

标签: linux-kernel filesystems system-calls


【解决方案1】:

答案很大程度上取决于文件系统,因为不同的文件系统会有不同的超级块布局,实际上块的排列方式也不同。

例如,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 分配超级块,或者编写您自己的与 ext2/ext3/etc/etc 文件系统超级块完全匹配的结构。

那么你必须知道在哪里可以找到超级块(这里是 lseek())。

还需要将磁盘文件名file_descriptor传递给函数。

那么做

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

struct superblock * sb = read_superblock(fd);

【讨论】:

    猜你喜欢
    • 2010-09-20
    • 2013-07-13
    • 1970-01-01
    • 2012-01-07
    • 2012-11-18
    • 1970-01-01
    • 2023-03-15
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多