2015-11-07 59 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);