我们在驱动编程中经常会遇到文件的操作:struct file_operations.比如open()、release()、read()、write()、ioctl()、poll()、select()、fsync()、llseek()等.
比如,
用户空间的open()函数原型如下:
int open(const char *pathname,int flags)
内核空间对应的open()函数原型如下:
int (*open) (struct inode *, struct file *);
用户空间的参数和其对应的内核空间的函数的参数看上去并没有太直接的关系.对于用户空间参数pathname和flags都是用户已知并指定的;而对于内核空间对应的函数,参数inode和file则是内核根据用户空间的两个参数构建的.它除了包含用户传递的那点微量信息之外,还丰富了很多内核本身需要的信息.
1.struct file:
/*
* NOTE:
* read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
* can be called without the big kernel lock held in all filesystems.
*/
struct file_operations {
struct module *owner;
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 *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
};
可见,所有内核的操作函数尤其是常用的都有struct file.其中,函数open()和函数release()还有struct inode.
2.struct inode:
struct inode是当用户空间打开一个文件(设备节点)内核自动构建的一个结构体,用来表征一个物理文件,它是唯一性的.比如当用户空间打开下面的设备节点:
/dev/chrdev c 135 0
内核便构建一个和此设备节点相对应的struct inode.其中struct inode域在驱动中常用到的域有:
dev_t i_rdev:包含了真正的设备编号;
union {
struct pipe_inode_info *i_pipe;//管道
struct block_device *i_bdev;//块设备
struct cdev *i_cdev; //该成员表示字符设备的内核的内部结构.当inode指向一个字符设备文件时,该成员包含了指向struct cdev结构的指针,cdev结构是字符设备结构体.
};
当一个驱动支持多个设备的时候,我们可以通过struct inode的dev_t域来判断当前我们操作的具体是哪个设备.比如三个串口都用同一套驱动的时候,其中有个串口是用来外接485,这时候需要一根GPIO来实现485的半双工机制.这时候可以通过dev_t域来判断出哪个串口是外接485的串口,然后在底层发送数据的时候使能485发送数据.
当我们打开的设备是字符设备,内核便帮我们把struct inode中的cdev域赋值为我们目标具体的字符设备,如struct scull_dev.这时候可以通过内核提供的函数container_of把具体的字符设备提取出来.见LDD3的P63下面代码:
int scull_open(struct inode *inode, struct file *filp)
{
struct scull_dev *dev; /* device information */
dev = container_of(inode->i_cdev, struct scull_dev, cdev);
filp->private_data = dev; /* for other methods */
/* now trim to 0 the length of the device if open was write-only */
if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
scull_trim(dev); /* ignore errors */
}
return 0; /* success */
}
struct inode定义如下:
struct inode {
struct hlist_node i_hash;
struct list_head i_list; /* backing dev IO 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;
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;
blkcnt_t i_blocks;
unsigned int i_blkbits;
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;
};
__u32 i_generation;
#ifdef CONFIG_FSNOTIFY
__u32 i_fsnotify_mask; /* all events this inode cares about */
struct hlist_head i_fsnotify_mark_entries; /* fsnotify mark entries */
#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
#ifdef CONFIG_FS_POSIX_ACL
struct posix_acl *i_acl;
struct posix_acl *i_default_acl;
#endif
void *i_private; /* fs or device private pointer */
};
因此,struct inode最常用的两个域便是i_rdev和联合体union,其中联合体来表征此被打开的设备文件是管道、字符设备还是块设备,里面记录的是我们具体的设备,如struct scull_dev.
3.struct file:
struct file文件结构代表一个打开的文件描述符,它不是专门给驱动程序使用的,系统中每一个打开的文件在内核中都有一个关联的struct file.它由内核在open时创建,并传递给在文件上操作的任何函数(如read(),write()等),直到最后关闭.当文件的所有实例都关闭之后,内核释放这个数据结构.
private_data:
在struct file_operations里面基本上所有的函数的参数都带了这个结构体.从宏观上来讲,这个结构体一旦被open之后,对后续的操作,如read、write、ioctl等动作都是可见的.尤其是其private_data,一般在open的时候用来存放具体的目标操作数据,后续的read、write、ioctl便通过此域来获取到目标数据实现交互.如上述的snull_open()函数.这种技巧经常在驱动里面用到.
f_path.dentry->d_inode:
此域记录的是相关联的struct inode.拿到此域,便可实现上述2中的一切关于inode的操作.如函数tty_write()下面片断代码:
static ssize_t tty_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct tty_struct *tty;
struct inode *inode = file->f_path.dentry->d_inode;
ssize_t ret;
struct tty_ldisc *ld;
const struct file_operations:
这个是用来绑定具体的struct file_operations.内核在执行open时对这个指针赋值.
可见,只要拿到了struct file这个结构体,极大的方便的后续准确无误的操作,包括struct inode、struct file_operations.其定义如下:
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;
spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
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
/* 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;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};