• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            『轉(zhuǎn)』文件結(jié)構(gòu)體struct file(Linux 2.6.23內(nèi)核)

            struct file結(jié)構(gòu)體定義在/linux/include/linux/fs.h(Linux 2.6.11內(nèi)核)中,其原型是:
            struct file {
                    /*
                     * fu_list becomes invalid after file_free is called and queued via
                     * fu_rcuhead for RCU freeing
                     */
                    union {
                            struct list_head        fu_list;
                            struct rcu_head         fu_rcuhead;
                    } f_u;
                    struct path             f_path;
            #define f_dentry        f_path.dentry
            #define f_vfsmnt        f_path.mnt
                    const struct file_operations    *f_op;
                    atomic_t                f_count;
                    unsigned int            f_flags;
                    mode_t                  f_mode;
                    loff_t                  f_pos;
                    struct fown_struct      f_owner;
                    unsigned int            f_uid, f_gid;
                    struct file_ra_state    f_ra;
                    unsigned long           f_version;
            #ifdef CONFIG_SECURITY
                    void                    *f_security;
            #endif
                    /* needed for tty driver, and maybe others */
                    void                    *private_data;
            #ifdef CONFIG_EPOLL
                    /* Used by fs/eventpoll.c to link all the hooks to this file */
                    struct list_head        f_ep_links;
                    spinlock_t              f_ep_lock;
            #endif /* #ifdef CONFIG_EPOLL */
                    struct address_space    *f_mapping;
            };
            文件結(jié)構(gòu)體代表一個(gè)打開(kāi)的文件,系統(tǒng)中的每個(gè)打開(kāi)的文件在內(nèi)核空間都有一個(gè)關(guān)聯(lián)的struct file。它由內(nèi)核在打開(kāi)文件時(shí)創(chuàng)建,并傳遞給在文件上進(jìn)行操作的任何函數(shù)。在文件的所有實(shí)例都關(guān)閉后,內(nèi)核釋放這個(gè)數(shù)據(jù)結(jié)構(gòu)。在內(nèi)核創(chuàng)建和驅(qū)動(dòng)源碼中,struct file的指針通常被命名為file或filp。一下是對(duì)結(jié)構(gòu)中的每個(gè)數(shù)據(jù)成員的解釋:
            一、
            union {
                struct list_head fu_list;
                struct rcu_head rcuhead;
            }f_u;
            其中的struct list_head定義在 linux/include/linux/list.h中,原型為:
            struct list_head {
                    struct list_head *next, *prev;
            };
            用于通用文件對(duì)象鏈表的指針。
            struct rcu_head定義在linux/include/linux/rcupdate.h中,其原型為:
            /**
            * struct rcu_head - callback structure for use with RCU
            * @next: next update requests in a list
            * @func: actual update function to call after the grace period.
            */
            struct rcu_head {
                    struct rcu_head *next;
                    void (*func)(struct rcu_head *head);
            };
            RCU(Read-Copy Update)是Linux 2.6內(nèi)核中新的鎖機(jī)制,具體在這里有介紹:
            http://www.ibm.com/developerworks/cn/linux/l-rcu/
            二、
            struct path             f_path;
            被定義在linux/include/linux/namei.h中,其原型為:
            struct path {
                    struct vfsmount *mnt;
                    struct dentry *dentry;
            };
            在早些版本的內(nèi)核中并沒(méi)有此結(jié)構(gòu),而是直接將path的兩個(gè)數(shù)據(jù)成員作為struct file的數(shù)據(jù)成員,
            struct vfsmount *mnt的作用是指出該文件的已安裝的文件系統(tǒng),
            struct dentry *dentry是與文件相關(guān)的目錄項(xiàng)對(duì)象。
            三、
            const struct file_operations    *f_op;
            被定義在linux/include/linux/fs.h中,其中包含著與文件關(guān)聯(lián)的操作,如:
            loff_t (*llseek) (struct file *, loff_t, int);
            ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
            ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
            等。當(dāng)打開(kāi)一個(gè)文件時(shí),內(nèi)核就創(chuàng)建一個(gè)與該文件相關(guān)聯(lián)的struct file結(jié)構(gòu),其中的*f_op就指向的是
            具體對(duì)該文件進(jìn)行操作的函數(shù)。例如用戶調(diào)用系統(tǒng)調(diào)用read來(lái)讀取該文件的內(nèi)容時(shí),那么系統(tǒng)調(diào)用read最終會(huì)陷入內(nèi)核調(diào)用sys_read函數(shù),而sys_read最終會(huì)調(diào)用于該文件關(guān)聯(lián)的struct file結(jié)構(gòu)中的f_op->read函數(shù)對(duì)文件內(nèi)容進(jìn)行讀取。
            四、
            atomic_t                f_count;
            atomic_t被定義為:
            typedef struct { volatile int counter; } atomic_t;
            volatile修飾字段告訴gcc不要對(duì)該類型的數(shù)據(jù)做優(yōu)化處理,對(duì)它的訪問(wèn)都是對(duì)內(nèi)存的訪問(wèn),而不是對(duì)寄存器的訪問(wèn)。 
            本質(zhì)是int類型,之所以這樣寫是讓編譯器對(duì)基于該類型變量的操作進(jìn)行嚴(yán)格的類型檢查。此處f_count的作用是記錄對(duì)文件對(duì)象的引用計(jì)數(shù),也即當(dāng)前有多少個(gè)進(jìn)程在使用該文件。
            五、
            unsigned int            f_flags;
            當(dāng)打開(kāi)文件時(shí)指定的標(biāo)志,對(duì)應(yīng)系統(tǒng)調(diào)用open的int flags參數(shù)。驅(qū)動(dòng)程序?yàn)榱酥С址亲枞筒僮餍枰獧z查這個(gè)標(biāo)志。
            六、
            mode_t                  f_mode;
            對(duì)文件的讀寫模式,對(duì)應(yīng)系統(tǒng)調(diào)用open的mod_t mode參數(shù)。如果驅(qū)動(dòng)程序需要這個(gè)值,可以直接讀取這個(gè)字段。
            mod_t被定義為:
            typedef unsigned int __kernel_mode_t;
            typedef __kernel_mode_t         mode_t;
            七、
            loff_t                  f_pos;
            當(dāng)前的文件指針位置,即文件的讀寫位置。
            loff_t被定義為:
            typedef long long       __kernel_loff_t;
            typedef __kernel_loff_t         loff_t;
            八、
            struct fown_struct      f_owner;
            struct fown_struct在linux/include/linux/fs.h被定義,原型為:
            struct fown_struct {
                    rwlock_t lock;          /* protects pid, uid, euid fields */
                    struct pid *pid;        /* pid or -pgrp where SIGIO should be sent */
                    enum pid_type pid_type; /* Kind of process group SIGIO should be sent to */
                    uid_t uid, euid;        /* uid/euid of process setting the owner */
                    int signum;             /* posix.1b rt signal to be delivered on IO */
            };
            該結(jié)構(gòu)的作用是通過(guò)信號(hào)進(jìn)行I/O時(shí)間通知的數(shù)據(jù)。
            九、
            unsigned int            f_uid, f_gid;
            標(biāo)識(shí)文件的所有者id,所有者所在組的id.
            十、
            struct file_ra_state    f_ra;
            struct file_ra_state結(jié)構(gòu)被定義在/linux/include/linux/fs.h中,原型為:
            struct file_ra_state {
                    pgoff_t start;                  /* where readahead started */
                    unsigned long size;             /* # of readahead pages */
                    unsigned long async_size;       /* do asynchronous readahead when
                                                       there are only # of pages ahead */
                                                       
                    unsigned long ra_pages;         /* Maximum readahead window */
                    unsigned long mmap_hit;         /* Cache hit stat for mmap accesses */
                    unsigned long mmap_miss;        /* Cache miss stat for mmap accesses */
                    unsigned long prev_index;       /* Cache last read() position */
                    unsigned int prev_offset;       /* Offset where last read() ended in a page */
            };
            文件預(yù)讀狀態(tài),文件預(yù)讀算法使用的主要數(shù)據(jù)結(jié)構(gòu),當(dāng)打開(kāi)一個(gè)文件時(shí),f_ra中出了perv_page(默認(rèn)為-1)和ra_apges(對(duì)該文件允許的最大預(yù)讀量)這兩個(gè)字段外,其他的所有西端都置為0。
            十一、
            unsigned long           f_version;
            記錄文件的版本號(hào),每次使用后都自動(dòng)遞增。
            十二、
            #ifdef CONFIG_SECURITY
                    void                    *f_security;
            #endif
            此處我的理解是如果在編譯內(nèi)核時(shí)配置了安全措施,那么struct file結(jié)構(gòu)中就會(huì)有void *f_security數(shù)據(jù)項(xiàng),用來(lái)描述安全措施或者是記錄與安全有關(guān)的信息。
            十三、
            void *private_data;
            系統(tǒng)在調(diào)用驅(qū)動(dòng)程序的open方法前將這個(gè)指針置為NULL。驅(qū)動(dòng)程序可以將這個(gè)字段用于任意目的,也可以忽略這個(gè)字段。驅(qū)動(dòng)程序可以用這個(gè)字段指向已分配的數(shù)據(jù),但是一定要在內(nèi)核釋放file結(jié)構(gòu)前的release方法中清除它。
            十四、
            #ifdef CONFIG_EPOLL
                    /* Used by fs/eventpoll.c to link all the hooks to this file */
                    struct list_head        f_ep_links;
                    spinlock_t              f_ep_lock;
            #endif /* #ifdef CONFIG_EPOLL */
            被用在fs/eventpoll.c來(lái)鏈接所有鉤到這個(gè)文件上。其中f_ep_links是文件的事件輪詢等待者鏈表的頭,f_ep_lock是保護(hù)f_ep_links鏈表的自旋鎖。
            十五、struct address_space    *f_mapping;
            struct address_space被定義在/linux/include/linux/fs.h中,此處是指向文件地址空間的指針。
              在驅(qū)動(dòng)開(kāi)發(fā)中,文件讀/寫模式mode、標(biāo)志f_flags都是設(shè)備驅(qū)動(dòng)關(guān)心的內(nèi)容,而私有數(shù)據(jù)指針private_data在折本驅(qū)動(dòng)中被廣泛使用,大多被指向設(shè)備驅(qū)動(dòng)自定義用于描述設(shè)備的結(jié)構(gòu)體。 
            驅(qū)動(dòng)程序中常用如下類似的代碼來(lái)檢測(cè)用戶打開(kāi)文件的讀寫方式:
            if (file->f_mode & FMODE_WRITE) //用戶要求可寫
              {
              }
              if (file->f_mode & FMODE_READ) //用戶要求可讀
              {
              }
            下面的代碼可用于判斷以阻塞還是非阻塞方式打開(kāi)設(shè)備文件:
              if (file->f_flags & O_NONBLOCK) //非阻塞
                  pr_debug("open:non-blocking\n");
              else //阻塞
                  pr_debug("open:blocking\n");
            參考:
            Linux設(shè)備驅(qū)動(dòng)開(kāi)發(fā)詳解
            深入理解linux內(nèi)核

            posted on 2010-06-17 23:14 小默 閱讀(308) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Linux

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            久久久久亚洲av无码专区喷水| 欧美久久综合九色综合| 日韩AV无码久久一区二区| 久久精品国产亚洲av影院| 久久婷婷国产麻豆91天堂| 亚洲国产一成久久精品国产成人综合| 久久久无码精品亚洲日韩京东传媒| 久久人人爽人人爽人人AV东京热 | 久久精品国产亚洲精品2020| 青青青国产成人久久111网站| 亚洲国产成人久久一区WWW| 色8久久人人97超碰香蕉987| 国产精品免费久久久久电影网| 亚洲色欲久久久综合网东京热| 国产精品成人无码久久久久久| 亚洲中文字幕久久精品无码喷水| av无码久久久久久不卡网站| 久久久久亚洲国产| 精品一久久香蕉国产线看播放| 久久综合狠狠综合久久| 久久福利资源国产精品999| 国产69精品久久久久99| 久久国产精品无码一区二区三区 | 久久综合视频网站| 国内精品九九久久久精品| 老男人久久青草av高清| 欧美午夜A∨大片久久 | 久久99精品久久久久久水蜜桃 | 久久人人爽人人爽人人片AV不 | 99久久人妻无码精品系列蜜桃| 麻豆精品久久久久久久99蜜桃| 久久久久久久亚洲精品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 一本久久a久久精品vr综合| 久久婷婷人人澡人人| 91久久成人免费| 久久午夜电影网| 久久综合中文字幕| 久久综合九色综合久99| 久久久久综合网久久| 久久久91精品国产一区二区三区 |