红联Linux门户
Linux帮助

linux内核源代码include/linux/fs.h中关键的和设备驱动程序有关的结构

发布时间:2008-05-09 10:22:51来源:红联作者:Dssiuet
struct file_operations结构位置:

linux内核源代码中include目录中linux目录fs.h中[code]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 (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, 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 *);
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 (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
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 (*dir_notify)(struct file *filp, unsigned long arg);
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);
};[/code]linux/fs.h中定义的struct file是设备驱动程序使用的第二个最重要的数据结构:

file结构代表一个打开的文件(不仅仅限定于设备驱动程序,系统中每个打开的文件在内核空间都有一个对应的file结构),它由内核在open时创建,并传递给在改文件上进行操作的所有函数,直到最后的close函数,在文件的所有实例都关闭后,内核会释放这个数据结构。

内核源代码中,指向struct file的指针通常被称为file或者filp[code]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 dentry *f_dentry; //文件对应的目录项结构

struct vfsmount *f_vfsmnt;

const struct file_operations *f_op; //与文件相关的操作

atomic_t f_count;

unsigned int f_flags; //文件标志,如O_RDONLY,O_NONBLOCK和O_SYNC

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;

/* 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;
};
extern spinlock_t files_lock;[/code]内核用inode结构在内部表示文件,因此它和file结构不痛,后者表示打开的文件描述符,对单个文件,可能会有许多个表示打开的文件描述符的file结构,但是他们都指向单个inode结构。

inode结构包含了大量有关文件的信息,作为常规,只有下面2个字段对编写驱动程序代码有用:[code]dev_t i_rdev;

//对表示设备文件的inode结构,改字段包含了真正的设备编号。

struct cdev *i_cdev;
//struct cdev是表示字符设备的内核的内部结构,当inode指向一个字符设备文件时,该字段包含了指向struct cdev结构的指针。

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;
umode_t i_mode;
unsigned int i_nlink;
uid_t i_uid;
gid_t i_gid;
dev_t i_rdev;
loff_t i_size;
struct timespec i_atime;
struct timespec i_mtime;
struct timespec i_ctime;
unsigned int i_blkbits;
unsigned long i_blksize;
unsigned long i_version;
blkcnt_t i_blocks;
unsigned short i_bytes;
spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */
struct mutex i_mutex;
struct rw_semaphore i_alloc_sem;
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
/* These three should probably be a union */
struct list_head i_devices;
struct pipe_inode_info *i_pipe;
struct block_device *i_bdev;
struct cdev *i_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;
void *i_security;
union {
void *generic_ip;
} u;
#ifdef __NEED_I_SIZE_ORDERED
seqcount_t i_size_seqcount;
#endif
};[/code]
文章评论

共有 6 条评论

  1. 于 2013-08-01 15:15:21发表:

    看不懂。。

  2. xieyc 于 2009-11-17 00:49:20发表:

    看看。。。学习一下

  3. linshao.Solin 于 2009-11-15 19:33:53发表:

    不知道,不过没C基础的就不好说了

  4. andawater3 于 2009-08-19 17:03:18发表:

    看看,学习学习

  5. andawater3 于 2009-08-19 17:03:15发表:

    看看,学习学习

  6. js001sdx 于 2009-08-19 10:14:13发表:

    学习