sysfs文件系統的創建【轉】

http://blog.csdn.net/dndxhej/article/details/7434615node

對sysfs和設備模型有了解的都會知道sysfs實際是爲了將設備模型導出到用戶空間的一個內存文件系統。linux

設備模型的關鍵結構體kobject會組成設備模型的樹形結構,而sysfs的關鍵結構體sysfs_dirent也是相似的樹形的結構,vfs中的dentry一樣是相似的樹形結構。函數

sysfs目錄文件的建立都是由設備模型的上層構件(bus device driver class)在註冊的時候調用它們內含的kobject(設備模型的底層基石)的添加註冊操做,而kobject的操做就調用sysfs文件系統的具體操做atom

 這些結構體是有聯繫的,但這一次咱們先不過分關注他們的聯繫,僅僅對sysfs下目錄和文件的建立作個分析:spa

 

kobject結構體:.net

 1 struct kobject {
 2     const char        *name;
 3     struct list_head    entry;
 4     struct kobject        *parent;
 5     struct kset        *kset;
 6     struct kobj_type    *ktype;
 7     struct sysfs_dirent    *sd;
 8     struct kref        kref;
 9     unsigned int state_initialized:1;
10     unsigned int state_in_sysfs:1;
11     unsigned int state_add_uevent_sent:1;
12     unsigned int state_remove_uevent_sent:1;
13     unsigned int uevent_suppress:1;
14 };

 

 

 

sysfs_dirent結構中的union有四項:目錄(s_dir)、連接文件(s_symlink),屬性文件(s_attr)和二進制屬性文件(s_bin_attr)。設計

struct sysfs_dirent {
    atomic_t        s_count;
    atomic_t        s_active;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
    struct lockdep_map    dep_map;
#endif
    struct sysfs_dirent    *s_parent;
    struct sysfs_dirent    *s_sibling;
    const char        *s_name;

    union {
        struct sysfs_elem_dir        s_dir;
        struct sysfs_elem_symlink    s_symlink;
        struct sysfs_elem_attr        s_attr;
        struct sysfs_elem_bin_attr    s_bin_attr;
    };

    unsigned int        s_flags;
    unsigned short        s_mode;
    ino_t            s_ino;
    struct sysfs_inode_attrs *s_iattr;
};

 

 

 咱們vfs中有inode和entry兩種關鍵的對象,在不少文件系統的設計中,都會有相似omfs_inode和omfs_extent_entry等等結構體來抽象代表具體文件系統的節點和目錄項。指針

 而在sysfs中,無論是目錄仍是文件,都用同一個結構體sysfs_dirent來表示,這個結構體能夠說是inode和dentry的綜合,由於有:code

1 unsignedint s_flags;
2 unsignedshort s_mode;
3 ino_t s_ino;

 

 等相似於inode的內容,也有:對象

1 structsysfs_dirent *s_parent;
2 structsysfs_dirent *s_sibling;
3 constchar *s_name;

 

等相似於dentry的內容。

 

 總的來講,sysfs_dirent結構體是sysfs和kobject創建鏈接的橋樑。

 

咱們知道,kobject對應於sysfs下的一個目錄:

1 structsysfs_elem_dir {
2 structkobject     *kobj;
3 /*children list starts here and goes through sd->s_sibling */
4 structsysfs_dirent    *children;
5 };

 

上面的結構體緊密的將kobject和sysfs_dirent聯繫起來。kobject表明目錄,而對於表明目錄的sysfs_dirent,內嵌的sysfs_elem_dir有kobject的指針,因此kobject和sysfs_dirent是你中有我,我中有你。

不過對於文件就不同了,由於文件也有本身的sysfs_dirent,而文件並無本身的kobject。

 

在普通的文件系統中,好比omfs中,創建一個目錄的話,首先會從物理存儲介質(磁盤)中讀取相關信息填充omfs下的omfs_inode結構體,而後會創建vfs層的inode和dentry等結構體。

 

而在sysfs中,在創建目錄和文件時,只會經過sysfs_dirent結構體來創建目錄文件的層次結構,而看不到vfs中的dentry、inode。既然是linux下的文件系統,無論多麼特殊,確定要有inode和dentry的,sysfs文件系統也不例外,只不過sysfs將創建inode和dentry的操做推到sysfs_lookup函數中來作,在sysfs_lookup中還會創建dentry與sysfs_dirtnt的關係。dentry->d_fsdata= sysfs_get(sd);

 

其實這個關係,在sysfs_fill_super就有體現:root->d_fsdata= &sysfs_root;

1 struct sysfs_dirent sysfs_root = {
2     .s_name        = "",
3     .s_count    = ATOMIC_INIT(1),
4     .s_flags    = SYSFS_DIR,
5     .s_mode        = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
6     .s_ino        = 1,
7 };

 這個sysfs_root就是sysfs_dirent層次結構中的根。

 下面是sysfs建立目錄的過程,注意的是這個過程只能經過kobject的操做來實現,在/sys下用mkdir是沒做用的:

 1 int sysfs_create_dir(struct kobject * kobj)
 2 {
 3     struct sysfs_dirent *parent_sd, *sd;
 4     int error = 0;
 5 
 6     BUG_ON(!kobj);
 7 
 8     if (kobj->parent)
 9         parent_sd = kobj->parent->sd;
10     else
11         parent_sd = &sysfs_root;
12 
13     error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
14     if (!error)
15         kobj->sd = sd;
16     return error;
17 }

 

 

 1 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
 2               const char *name, struct sysfs_dirent **p_sd)
 3 {
 4     umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
 5     struct sysfs_addrm_cxt acxt;
 6     struct sysfs_dirent *sd;
 7     int rc;
 8 
 9     /* allocate */
10     sd = sysfs_new_dirent(name, mode, SYSFS_DIR);   //初始化sysfs_dirent結構體
11     if (!sd)
12         return -ENOMEM;
13     sd->s_dir.kobj = kobj;              //sysfs_dirent與kobject的聯繫創建
14 
15     /* link in */
16     sysfs_addrm_start(&acxt, parent_sd);
17     rc = sysfs_add_one(&acxt, sd);
18     sysfs_addrm_finish(&acxt);
19 
20     if (rc == 0)
21         *p_sd = sd;
22     else
23         sysfs_put(sd);
24     return rc;
25 }

在sysfs_add_one是層次關係的創建:

 1 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 2 {
 3     struct sysfs_inode_attrs *ps_iattr;
 4 
 5     if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
 6         return -EEXIST;
 7 
 8     sd->s_parent = sysfs_get(acxt->parent_sd);       //創建上下層次間的父子關係
 9 
10     sysfs_link_sibling(sd);                        //創建同一層次的兄弟關係
11 
12     /* Update timestamps on the parent */
13     ps_iattr = acxt->parent_sd->s_iattr;                    
14     if (ps_iattr) {
15         struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
16         ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
17     }
18 
19     return 0;
20 }

 

在sysfs_link_sibling中關鍵代碼:

1     for (pos = &parent_sd->s_dir.children; *pos; pos = &(*pos)->s_sibling) {
2         if (sd->s_ino < (*pos)->s_ino)
3             break;
4     }

 

 根據s_ino從小到大的順序組成一個鏈。附上一個圖,這個關係就一目瞭然:

 

 從這個圖能夠很清楚的看到sysfs中sysfs_dirent架成的樹形結構(注意他們的s_ino排列),這個結構和kobject,dentry的樹形結構幾乎一致,由於他們是有聯繫的,後面咱們會一步步理清他們的聯繫,到時侯就明白sysfs是如何將設備模型(kobject)倒成文件系統的。

相關文章
相關標籤/搜索