当用户进程操作字符设备驱动,内核会根据设备号找到对应的cdev对象。
open/read/write这些接口就是组成VFS(虚拟文件系统)。
在Linux内核,用一个inode节点对象描述一个要操作的文件/设备文件,包括权限 ,设备号等信息,就是描述一个要操作的文凭的属性,一个文件可以打开很多次, 但都是共用一个inode对象来描述属性的,文件描述符属于一个进程的资源,不同进程里有可能相同的文件描述符。(文件描述符默认值为1024)命令 ulimit -n
struct inode { ...... dev_t i_rdev;//设备文件对应的设备号,驱动里即可通过区分次设备号来区别不同的具体硬件 struct cdev *i_cdev;//指向对应 的字符设备驱动CDEV对象的地址 ... };// VFS的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类型来表示文件描述符.但文件描述符里有还存有对文件位置的偏移,打开标志等信息, 用一个int数无法记录下来的,所在每个文件描述符的信息都是由内核里用file对象描述文件描述符, 在文件打开时创建, 关闭时销毁
文件描述符fd:
fd只是一个小整数,在open时产生。起到一个索引的作用,进程通过PCB中的文件描述符表找到该fd所指向的文件指针filp。struct file的指针通常被命名为file或filp。
struct file { ... struct path f_path;包含dentry和mnt两个成员,用于确定文件路径
#define f_dentry f_path.dentry f_path的成员之一,当前文件的dentry结构
#define f_vfsmnt f_path.mnt 表示当前文件所在文件系统的挂载根目录 const struct file_operations *f_op;//对应的文件操作对象的地址 unsigned int f_flags;//文件打开的标志 fmode_t f_mode;//权限 loff_t f_pos; //文件描述符的偏移 struct fown_struct f_owner;//属于那个进程 unsigned int f_uid, f_gid; void *private_data; //给驱动程序员使用 }; 如果打开设备文件,那么得到的file对象: file对象里的成员 f_path.dentry->d_inode->i_rdev可以获取到设备文件的设备号 file对象里的成员f_path.dentry->d_inode可以获取到设备文件的inode对象的地址
注意: 一个文件只有一个inode节点对象, 但是可以打开多次,得到不同的文件描述符对象(也就是多个struct file对象)
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]; 存放短文件名 };
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; };
// struct file_operations里函数参数
//inode表示应用程序打开的文件的节点对象,file表示打开文件获取到的文件描述符 int (*open) (struct inode *, struct file *); //返回值 0 表示成功打开,负数 表示打开失败,内核根据open函数的返回值来确定是否给调用的用户进程分配文件描述符。 //在驱动可以不实现此函数,如不实现,则表示每次打开都是成功的。 ssize_t (*read) (struct file *fl, char __user *buf, size_t leln, loff_t *off); //buf指向用户的缓冲区,len表示buf的大小(由用户调用read时进来的) //off表示fl文件描述符的操作偏移,返回值为实际给用户的数据字节数。 //注意,必须通过off指针来改变文件描述符的偏移(*off += 操作字节数). 不可以直接通过"fl->f_pos"来设置 //用户进程把数据给驱动, 也就是让驱动存放用户进程传进来的数据. // 参考read函数 ssize_t (*write) (struct file *, const char __user *buf, size_t len, loff_t *off); // cmd表示用户进程调用ioctl时的第二个参数, arg表示第三个参数(可选) long (*unlocked_ioctl) (struct file *fl, unsigned int cmd, unsigned long arg); // 返回值为0表示ioctl成功, 返回负数表示失败. // app: lseek(fd, 54, SEEK_SET) loff_t (*llseek) (struct file *fl, loff_t offset, int whence);
//在驱动里操作用户数据缓冲区的函数:
#include <asm/uaccess.h> //to指用户进程的缓冲区, from指驱动里装数据的缓冲区, n要复制多少字节。 extern inline long copy_to_user(void __user *to, const void *from, long n) //返回值为还有多少字节没有复制成功. 正常情况下返回0. //to指驱动的... from用户... n多少字节, .... extern inline long copy_from_user(void *to, const void __user *from, long n) //返回值为还有多少字节没有复制成功. 正常情况下返回0. //如果与用户进程交互的数据是1,2,4,8字节的话, 可用 put_user(x,p) //x为值, p为地址 //如果从用户进程获取1,2,4字节的话, 可用 get_user(x,p)
/驱动里动态申请缓冲区的函数:
#include <linux/slab.h> ///动态申请内存, 并清零. size为申请多大(不要超过128K), //flags为标志(常为GFP_KERNEL). 成功返回地址, 失败返回NULL // GFP_ATOMIC, 使用系统的内存紧急池 void *kmalloc(size_t size, gfp_t flags);//申请后要内存要清零 void *kzalloc(size_t size, gfp_t flags); //申请出来的内存已清零 void kfree(const void *objp); //回收kmalloc/kzalloc的内存 void *vmalloc(unsigned long size); //申请大内存空间 void vfree(const void *addr); //回收vmalloc的内存 // kmalloc申请出来的内存是物理地址连续的, vmalloc不一定是连续的