如何从内核中的超级块读取数据?

问题描述

我在用户空间的 c 上编写了主管应用程序,我在超级块上保留了列入白名单的 ID。它从 superblock 读取数据: filepath = "/dev/flashSSD"

        char soz[15];
        int i=1;
        fp = open(filepath,O_RDONLY);
        if (fp<0) {printf("Unable to open %s block.\n",filepath);exit(EXIT_FAILURE);}

        off_t off = lseek(fp,SEEK_SET);
        ssize_t len = read(fp,prc,sizeof prc);
        sscanf(prc[0],"%d",sany);

并且在管理(插入,删除)之后,它会在退出时写回超级块:

        fp = open(filepath,O_RDWR);
        if (fp<0) {printf("Unable to open %s block.\n",filepath);exit(EXIT_FAILURE);}

        if (lseek(fp,SEEK_SET)<0) {perror("write1: ");}
        if (write(fp,&prc,sizeof(prc))<0) {perror("write2: ");}
        close(fp);

现在,我如何从 kernel 中的超级块读取数据?

解决方法

感谢@Tsyvarev,我了解到我们可以像从文件一样读取超级块。

int read_sb() {
  int fd;
  char magl[1024];
  
  fd = open("/dev/flashSSD",O_RDONLY);

  /* Reads the boot section and the superblock */
  read(fd,magl,1024);
  
  close(fd);

  return 0;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...