驱动中 open()函数里面的两个结构体参数

struct inode 和 struct file 

1struct inode──字符设备驱动相关的重要结构介绍

内核中用inode结构表示具体的文件,而用file结构表示打开的文件描述符。Linux2.6.27内核中,inode结构体具体定义如下:
struct inode {
struct hlist_node    i_hash;
struct list_head    i_list;
struct list_head    i_sb_list;
struct list_head    i_dentry;
unsigned long        i_ino;
atomic_t        i_count;
unsigned int        i_nlink;
uid_t            i_uid;
gid_t            i_gid;
 dev_t            i_rdev;   //该成员表示设备文件的inode结构,它包含了真正的设备编号。
u64            i_version;
loff_t            i_size;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t        i_size_seqcount;
#endif
struct timespec        i_atime;
struct timespec        i_mtime;
struct timespec        i_ctime;
unsigned int        i_blkbits;
blkcnt_t        i_blocks;
unsigned short          i_bytes;
umode_t            i_mode;
spinlock_t        i_lock;    /* i_blocks, i_bytes, maybe i_size */
struct mutex        i_mutex;
struct rw_semaphore    i_alloc_sem;
const struct inode_operations    *i_op;
const struct file_operations    *i_fop;    /* former ->i_op->default_file_ops */
struct super_block    *i_sb;
struct file_lock    *i_flock;
struct address_space    *i_mapping;
struct address_space    i_data;
#ifdef CONFIG_QUOTA
struct dquot        *i_dquot[MAXQUOTAS];
#endif
struct list_head    i_devices;
union {
struct pipe_inode_info    *i_pipe;
struct block_device    *i_bdev;
 struct cdev        *i_cdev; //该成员表示字符设备的内核的 内部结构。当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,其中cdev结构是字符设备结构体。
};
int            i_cindex;

__u32            i_generation;

#ifdef CONFIG_DNOTIFY
unsigned long        i_dnotify_mask; /* Directory notify events */
struct dnotify_struct    *i_dnotify; /* for directory notifications */
#endif

#ifdef CONFIG_INOTIFY
struct list_head    inotify_watches; /* watches on this inode */
struct mutex        inotify_mutex;    /* protects the watches list */
#endif

unsigned long        i_state;
unsigned long        dirtied_when;    /* jiffies of first dirtying */

unsigned int        i_flags;

atomic_t        i_writecount;
#ifdef CONFIG_SECURITY
void            *i_security;
#endif
void            *i_private; /* fs or device private pointer */
};

2struct file ──字符设备驱动相关重要结构

文件结构 代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file。它由内核在open时创建,并传递给在文件上操作的任何函数,知道最后关闭。当文件的所有实例都关闭之后,内核释放这个数据结构。
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;  //该操作 是定义文件关联的操作的。内核在执行open时对这个 指针赋值。 
atomic_long_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;

u64            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;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};

----------------------------------------------------------------------------

file结构体和inode结构体  

1struct file结构体定义在include/linux/fs.h中定义。文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file。它由内核在打开文件时创建,并传递给在文件上进行操作的任何函数。在文件的所有实例都关闭后,内核释放这个数据结构。在内核创建和驱动源码中,struct file的指针通常被命名为filefilp。如下所示:
struct file {
        union {
             struct list_head fu_list; 文件对象链表指针linux/include/linux/list.h
             struct rcu_head fu_rcuhead; RCU(Read-Copy Update)Linux 2.6内核中新的锁机制
        } f_u;
        struct path f_path;  包含dentrymnt两个成员,用于确定文件路径
        #define f_dentry  f_path.dentry  f_path的成员之一,当前文件的dentry结构
        #define f_vfsmnt  f_path.mnt  表示当前文件所在文件系统的挂载根目录
        const struct file_operations *f_op; 与该文件相关联的操作函数
        atomic_t  f_count; 文件的引用计数(有多少进程打开该文件)
        unsigned int  f_flags;  对应于open时指定的flag
        mode_t  f_mode; 读写模式:openmod_t mode参数
        off_t  f_pos; 该文件在当前进程中的文件偏移量
        struct fown_struct f_owner; 该结构的作用是通过信号进行I/O时间通知的数据。
        unsigned int  f_uid, f_gid; 文件所有者id,所有者组id
        struct file_ra_state f_ra;  linux/include/linux/fs.h中定义,文件预读相关
        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;
};
2struct dentry
dentry 的中文名称是目录项,是Linux文件系统中某个索引节点(inode)的链接。这个索引节点可以是文件,也可以是目录。inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在运行的时候链接多个dentry,而d_count记录了这个链接的数量。
struct dentry { 
        atomic_t d_count; 目录项对象使用计数器,可以有未使用态,使用态和负状态                                            
        unsigned int d_flags; 目录项标志 
        struct inode * d_inode; 与文件名关联的索引节点 
        struct dentry * d_parent; 父目录的目录项对象 
        struct list_head d_hash; 散列表表项的指针 
        struct list_head d_lru; 未使用链表的指针 
        struct list_head d_child; 父目录中目录项对象的链表的指针 
        struct list_head d_subdirs;对目录而言,表示子目录目录项对象的链表 
        struct list_head d_alias; 相关索引节点(别名)的链表 
        int d_mounted; 对于安装点而言,表示被安装文件系统根项 
        struct qstr d_name; 文件名 
        unsigned long d_time; /* used by d_revalidate */ 
        struct dentry_operations *d_op; 目录项方法 
        struct super_block * d_sb; 文件的超级块对象 
        vunsigned long d_vfs_flags; 
        void * d_fsdata;与文件系统相关的数据 
        unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名

}; 

3)索引节点对象由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; 
};

      inode 译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是BlockBlock是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件。 
      做个比喻,比如一本书,存储设备或分区就相当于这本书,Block相当于书中的每一页,inode 就相当于这本书前面的目录,一本书有很多的内容,如果想查找某部份的内容,我们可以先查目录,通过目录能最快的找到我们想要看的内容。
      当我们用ls 查看某个目录或文件时,如果加上-i 参数,就可以看到inode节点了;比如ls -li lsfile.sh ,最前面的数值就是inode信息

 
 
 

struct file 结构体 (转)

(2011-02-17 19:11:25) 

转载

标签: 

杂谈

分类: Linux驱动学习 

在 file_operations 结构体中,会看到许多函数指针所指向的函数都必须传进 struct file 结构体指针 struct file * 作为参数。struct file 结构体定义在 <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;
        spinlock_t              f_lock;
        atomic_long_t           f_count;
        unsigned int            f_flags;
        fmode_t                 f_mode;
        loff_t                  f_pos;
        struct fown_struct      f_owner;
        const struct cred       *f_cred;
        struct file_ra_state    f_ra;

        u64                     f_version;
#ifdef CONFIG_SECURITY
        void                    *f_security;
#endif
       
        void                    *private_data;

#ifdef CONFIG_EPOLL
       
        struct list_head        f_ep_links;
#endif
        struct address_space    *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
        unsigned long f_mnt_write_state;
#endif
};


在设备驱动中,struct file 结构体也是一个非常重要的数据结构。注意的是,这里的 file 和应用程序中的 FILE 流指针没有什么关系,FILE 定义在 C 库中,它永远不会出现在内核代码中。

file structure 结构代表一个打开的文件(open file).(打开的文件并没有确切的指定到哪个设备驱动,实际上每个打开的文件都与内核空间中的 struct file 结构相关联)。

file structure 结构在调用open 打开一个文件时由内核创建,并会被传递给任一个对这个打开文件进行操作的函数;当所有事情都做完后,会调用 close() 关闭掉文件,此时内核释放这个数据结构。

一般地,在内核源码中,struct file 结构体的指针往往写成 filp 。

struct file 中的几个重要成员

mode_t f_mode;
文件模式根据 FMMODE_READ 和 FMODE_WRITE 位来识别文件是否可读或可写,或是可读可写。在read() 和 write() 系统调用中,没有必要对此权限进行检查,因为内核已经在你的系统调用之前已经做了检查。如果文件没有相应的读或写权限,那么如果尝试读写都将被拒绝,驱动程序甚至对此情况毫无知觉。

loff_t f_pos;
此变量表示当前的文件读写位置。loff_t 在所有的平台上都是 64 位的变量( long long 型, gcc 专用术语)。驱动程序如果想知道当前在文件中所处位置,那么可以通过读取此变量得知,但是一般地不应直接对此进行更改。通过 llseek() 方法可以改变文件位置。

unsigned int f_flags;
这是表示如 O_RDONLY, O_NONBLOCK 与 O_SYNC 这样的标志。一个驱动程序应该检查 O_NONBLOCK 标志,以查看是否有非阻塞操作的请求。其它的标志用得比较少。需要注意的是,检查 read/write 权限应该是通过检查 f_mode 得到而不是 f_flags 。所有的标志定义在头文件 linux/fcntl.h 中可以看到。

struct file_operations *f_op;
内核安排这个指针作为它的 open 实现的一部分,当需要分派什么操作时,会读取它。filp->f_op 因为不会被内核保存起来以在其后之用,所以我们可以改变我们对相关文件的操作,在对文件使用新的操作方法时,我们就会转移到相应调用上。

void *private_data;
在对驱动调用 open 方法之前,open() 系统调用会这个指针设置为 NULL 。用户可以自由使用这个域,或者对其忽略。可以使用这个域之想分配的数据空间,但必须记得在内核销毁 file structure 之前在 release 方法里释放掉原来分配的内存。private_data 对于系统调用之间信息的保存会显得非常有用。

struct dentry *f_dentry;
目录入口 (dentry) 结构与文件相关。一般的,除了在以 filp->f_dentry->d_inode 来访问 inode 结构时,我们不太关心 dentry 这个结构。

 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在写一个 "Hello World" 驱动之前,你需要先搭建好 Linux 内核开发环境。如果你还没有搭建好环境,可以参考这份指南: https://www.cnblogs.com/gatieme/p/6123174.html 1. 创建一个目录来存放你的驱动代码,并创建一个名为 "hello.c" 的文件,用来存放你的驱动代码。 2. 编写驱动代码。在 hello.c 输入以下代码: ``` #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple Hello World module"); static int __init hello_init(void) { printk(KERN_INFO "Hello World!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye World!\n"); } module_init(hello_init); module_exit(hello_exit); ``` 3. 创建 Makefile 文件。在你的驱动代码目录创建一个名为 "Makefile" 的文件,用来指定如何编译和链接你的驱动。输入以下内容: ``` obj-m += hello.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ``` 4. 编译驱动。打开终端,进入到你的驱动代码目录,并输入 "make" 命令来编译驱动。 5. 加载驱动。输入 "sudo insmod hello.ko" 命令来加载驱动。 6. 查看驱动输出。输入 "dmesg ### 回答2: Hello World驱动是一个简单的设备驱动程序,用于向操作系统内核注册设备并进行数据读写操作。以下是一个基本的Hello World驱动程序的示例: ```c #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #define DEVICE_NAME "hello" // 设备名称 #define EXAMPLE_MSG "Hello World!\n" // 要写入设备的字符串 MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple hello world driver"); MODULE_VERSION("0.1"); static int major_number; // 设备主编号 // 设备文件打开函数 static int hello_open(struct inode *inodep, struct file *filep) { printk(KERN_INFO "Hello: Device has been opened\n"); return 0; } // 设备文件读取函数 static ssize_t hello_read(struct file *filep, char *buffer, size_t len, loff_t *offset) { int error_count = 0; // 将字符串写入用户空间缓冲区 error_count = copy_to_user(buffer, EXAMPLE_MSG, strlen(EXAMPLE_MSG)); if (error_count == 0) { printk(KERN_INFO "Hello: Sent %d characters to the user\n", strlen(EXAMPLE_MSG)); return 0; } else { printk(KERN_ERR "Hello: Failed to send %d characters to the user\n", error_count); return -EFAULT; } } // 设备文件关闭函数 static int hello_release(struct inode *inodep, struct file *filep) { printk(KERN_INFO "Hello: Device successfully closed\n"); return 0; } // 定义设备文件操作函数结构体 static struct file_operations fops = { .open = hello_open, .read = hello_read, .release = hello_release, }; // 模块加载函数 static int __init hello_init(void) { // 在内核注册设备 major_number = register_chrdev(0, DEVICE_NAME, &fops); if (major_number < 0) { printk(KERN_ALERT "Hello: Failed to register a major number\n"); return major_number; } printk(KERN_INFO "Hello: Registered correctly with major number %d\n", major_number); return 0; } // 模块卸载函数 static void __exit hello_exit(void) { // 注销设备 unregister_chrdev(major_number, DEVICE_NAME); printk(KERN_INFO "Hello: Goodbye!\n"); } // 注册初始化和卸载函数 module_init(hello_init); module_exit(hello_exit); ``` 通过编译该驱动程序并将其加载到操作系统内核,可以创建一个名为“hello”的设备文件,并在用户空间读取该设备文件,从而输出"Hello World!"的字符串。 ### 回答3: 编写一个"Hello World"驱动程序是一种在操作系统创建基本驱动程序的常见练习。这我将使用Linux内核的示例来说明如何编写一个简单的"Hello World"驱动程序。 首先,我们需要一个简单的C文件。我们可以将其命名为"hello_world_driver.c"。以下是该文件的代码: ```c #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple hello world driver"); static int __init hello_init(void) { printk(KERN_INFO "Hello, world!\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, world!\n"); } module_init(hello_init); module_exit(hello_exit); ``` 在这个驱动程序,我们使用了一些Linux内核的头文件,以及一些内核宏和函数。在驱动程序,我们定义了一个`hello_init`函数作为模块初始化函数,并在其使用`printk`函数输出"Hello, world!"。我们还定义了一个`hello_exit`函数作为模块退出函数,并在其使用`printk`函数输出"Goodbye, world!"。最后,我们使用`module_init`和`module_exit`宏将这两个函数分别设为模块的初始化和退出函数。 要编译这个驱动程序,我们需要一个Makefile。将以下代码保存为名为"Makefile"的文件: ```makefile obj-m += hello_world_driver.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean ``` 在当前目录下打开终端,然后运行以下命令来编译驱动程序: ``` make ``` 如果一切顺利,将会生成一个名为"hello_world_driver.ko"的内核模块文件。 要插入驱动程序并查看其输出,请运行以下命令: ``` sudo insmod hello_world_driver.ko dmesg | tail ``` 您将看到内核日志输出的"Hello, world!"消息。 要删除驱动程序,请运行以下命令: ``` sudo rmmod hello_world_driver dmesg | tail ``` 您将看到内核日志输出的"Goodbye, world!"消息。 这就是一个简单的"Hello World"驱动程序的写法。这个例子向您展示了如何创建一个基本的内核模块,并在加载和卸载模块时输出一些消息。实际的驱动程序可能会更加复杂,但这是一个入门的起点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值