歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

字符設備驅動常用數據結構

truct file_operations是一個字符設備把驅動的操作和設備號聯系在一起的紐帶,是一系列指針的集合,每個被打開的文件都對應於一系列的操作,這就是file_operations,用來執行一系列的系統調用。

linux-2.6.22/include/linux/fs.h
struct file_operations {
        struct module *owner;    //防止模塊還在被使用的時候被卸載
        loff_t        (*llseek) ();
        ssize_t       (*read) ();
        ssize_t       (*write) ();
        ssize_t       (*aio_read) ();
        ssize_t       (*aio_write) ();
        int           (*readdir) ();
        unsigned int (*poll) ();
        int           (*ioctl) ();
        long          (*unlocked_ioctl) ();
        long          (*compat_ioctl) ();
        int           (*mmap) ();
        int           (*open) ();
        int           (*flush) ();
        int           (*release) ();
        int           (*fsync) ();
        int           (*aio_fsync) ();
        int           (*fasync) ();
        int           (*lock) ();
        ssize_t       (*sendfile) ();
        ssize_t       (*sendpage) ();
        unsigned long (*get_unmapped_area) ();
        int           (*check_flags) ();
        int           (*dir_notify) ();
        int           (*flock) ();
        ssize_t       (*splice_write) ();
        ssize_t       (*splice_read) ();
};


struct--file
-----------------------------------------
    struct file代表一個打開的文件,在執行file_operation中的open操作時被創建,這裡需要注意的是與用戶空間file指針的區別,一個在內核,而file指針在用戶空間,由c庫來定義。

linux-2.6.22/include/linux/fs.h
struct file {
        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;
        void                            *f_security;
        void                            *private_data;
        struct list_head                f_ep_links;
        spinlock_t                      f_ep_lock;
        struct address_space            *f_mapping;
};
*索引節點對象由Linux/1672.html' target='_blank'>inode結構體表示,定義文件在linux/fs.h中
*/
struct inode {
        struct hlist_node       i_hash;              /* 哈希表 */
        struct list_head        i_list;              /* 索引節點鏈表 */
        struct list_head        i_dentry;            /* 目錄項鏈表 */
        unsigned long           i_ino;               /* 節點號 */
        atomic_t                i_count;             /* 引用記數 */
        umode_t                 i_mode;              /* 訪問權限控制 */
        unsigned int            i_nlink;             /* 硬鏈接數 */
        uid_t                   i_uid;               /* 使用者id */
        gid_t                   i_gid;               /* 使用者id組 */
        kdev_t                  i_rdev;              /* 實設備標識符 */                                對於字符串設備驅動程序來說有意義
        loff_t                  i_size;              /* 以字節為單位的文件大小 */
        struct timespec         i_atime;             /* 最後訪問時間 */
        struct timespec         i_mtime;             /* 最後修改(modify)時間 */
        struct timespec         i_ctime;             /* 最後改變(change)時間 */
        unsigned int            i_blkbits;           /* 以位為單位的塊大小 */
        unsigned long           i_blksize;           /* 以字節為單位的塊大小 */
        unsigned long           i_version;           /* 版本號 */
        unsigned long           i_blocks;            /* 文件的塊數 */
        unsigned short          i_bytes;             /* 使用的字節數 */
        spinlock_t              i_lock;              /* 自旋鎖 */
        struct rw_semaphore     i_alloc_sem;         /* 索引節點信號量 */
        struct inode_operations *i_op;               /* 索引節點操作表 */
        struct file_operations  *i_fop;              /* 默認的索引節點操作 */
        struct super_block      *i_sb;               /* 相關的超級塊 */
        struct file_lock        *i_flock;            /* 文件鎖鏈表 */
        struct address_space    *i_mapping;          /* 相關的地址映射 */
        struct address_space    i_data;              /* 設備地址映射 */
        struct dquot            *i_dquot[MAXQUOTAS]; /* 節點的磁盤限額 */
        struct list_head        i_devices;           /* 塊設備鏈表 */
        struct pipe_inode_info  *i_pipe;             /* 管道信息 */
        struct block_device     *i_bdev;             /* 塊設備驅動 */
        unsigned long           i_dnotify_mask;      /* 目錄通知掩碼 */
        struct dnotify_struct   *i_dnotify;          /* 目錄通知 */
        unsigned long           i_state;             /* 狀態標志 */
        unsigned long           dirtied_when;        /* 首次修改時間 */
        unsigned int            i_flags;             /* 文件系統標志 */
        unsigned char           i_sock;              /* 可能是個套接字吧 */
        atomic_t                i_writecount;        /* 寫者記數 */
        void                    *i_security;         /* 安全模塊 */
        __u32                   i_generation;        /* 索引節點版本號 */
        union {
                void            *generic_ip;         /* 文件特殊信息 */
        } u;
};
Copyright © Linux教程網 All Rights Reserved