现在的位置: 首页 > 综合 > 正文

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

2017年10月28日 ⁄ 综合 ⁄ 共 4491字 ⁄ 字号 评论关闭
struct file_operations结构位置:

linux内核源代码中include目录中linux目录fs.h中

  1. struct file_operations {
  2. struct module *owner;
  3. loff_t (*llseek) (struct file *, loff_t, int);
  4. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  5. ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
  6. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  7. ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
  8. int (*readdir) (struct file *, void *, filldir_t);
  9. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  10. int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  11. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  12. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  13. int (*mmap) (struct file *, struct vm_area_struct *);
  14. int (*open) (struct inode *, struct file *);
  15. int (*flush) (struct file *);
  16. int (*release) (struct inode *, struct file *);
  17. int (*fsync) (struct file *, struct dentry *, int datasync);
  18. int (*aio_fsync) (struct kiocb *, int datasync);
  19. int (*fasync) (int, struct file *, int);
  20. int (*lock) (struct file *, int, struct file_lock *);
  21. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
  22. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
  23. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
  24. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  25. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  26. int (*check_flags)(int);
  27. int (*dir_notify)(struct file *filp, unsigned long arg);
  28. int (*flock) (struct file *, int, struct file_lock *);
  29. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
  30. ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
  31. };

linux/fs.h中定义的struct file是设备驱动程序使用的第二个最重要的数据结构:

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

内核源代码中,指向struct file的指针通常被称为file或者filp

  1. struct file {
  2. /*
  3.     * fu_list becomes invalid after file_free is called and queued via
  4.     * fu_rcuhead for RCU freeing
  5.     */
  6. union {
  7.        struct list_head fu_list;
  8.        struct rcu_head     fu_rcuhead;
  9. } f_u;
  10. struct dentry        *f_dentry; //文件对应的目录项结构
  11. struct vfsmount       *f_vfsmnt;
  12. const struct file_operations *f_op; //与文件相关的操作
  13. atomic_t        f_count;
  14. unsigned int       f_flags;   //文件标志,如O_RDONLY,O_NONBLOCK和O_SYNC
  15. mode_t          f_mode;        //文件模式
  16. loff_t          f_pos; //当前读写位置
  17. struct fown_struct f_owner;
  18. unsigned int        f_uid, f_gid;
  19. struct file_ra_state f_ra;
  20. unsigned long        f_version;
  21. void          *f_security;
  22. /* needed for tty driver, and maybe others */
  23. void          *private_data;
  24. #ifdef CONFIG_EPOLL
  25. /* Used by fs/eventpoll.c to link all the hooks to this file */
  26. struct list_head f_ep_links;
  27. spinlock_t        f_ep_lock;
  28. #endif /* #ifdef CONFIG_EPOLL */
  29. struct address_space *f_mapping;
  30. };
  31. extern spinlock_t files_lock;

内核用inode结构在内部表示文件,因此它和file结构不痛,后者表示打开的文件描述符,对单个文件,可能会有许多个表示打开的文件描述符的file结构,但是他们都指向单个inode结构。

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

  1. dev_t i_rdev;
  2. //对表示设备文件的inode结构,改字段包含了真正的设备编号。
  3. struct cdev   *i_cdev;
  4. //struct cdev是表示字符设备的内核的内部结构,当inode指向一个字符设备文件时,该字段包含了指向struct cdev结构的指针。
  5. struct inode {
  6. struct hlist_node i_hash;
  7. struct list_head i_list;
  8. struct list_head i_sb_list;
  9. struct list_head i_dentry;
  10. unsigned long        i_ino;
  11. atomic_t        i_count;
  12. umode_t          i_mode;
  13. unsigned int        i_nlink;
  14. uid_t          i_uid;
  15. gid_t          i_gid;
  16. dev_t          i_rdev;
  17. loff_t          i_size;
  18. struct timespec        i_atime;
  19. struct timespec        i_mtime;
  20. struct timespec        i_ctime;
  21. unsigned int        i_blkbits;
  22. unsigned long        i_blksize;
  23. unsigned long        i_version;
  24. blkcnt_t        i_blocks;
  25. unsigned short       i_bytes;
  26. spinlock_t        i_lock; /* i_blocks, i_bytes, maybe i_size */
  27. struct mutex        i_mutex;
  28. struct rw_semaphore i_alloc_sem;
  29. struct inode_operations *i_op;
  30. const struct file_operations *i_fop; /* former ->i_op->default_file_ops */
  31. struct super_block *i_sb;
  32. struct file_lock *i_flock;
  33. struct address_space *i_mapping;
  34. struct address_space i_data;
  35. #ifdef CONFIG_QUOTA
  36. struct dquot        *i_dquot[MAXQUOTAS];
  37. #endif
  38. /* These three should probably be a union */
  39. struct list_head i_devices;
  40. struct pipe_inode_info *i_pipe;
  41. struct block_device *i_bdev;
  42. struct cdev        *i_cdev;
  43. int          i_cindex;
  44. __u32          i_generation;
  45. #ifdef CONFIG_DNOTIFY
  46. unsigned long        i_dnotify_mask; /* Directory notify events */
  47. struct dnotify_struct *i_dnotify; /* for directory notifications */
  48. #endif
  49. #ifdef CONFIG_INOTIFY
  50. struct list_head inotify_watches; /* watches on this inode */
  51. struct mutex        inotify_mutex; /* protects the watches list */
  52. #endif
  53. unsigned long        i_state;
  54. unsigned long        dirtied_when; /* jiffies of first dirtying */
  55. unsigned int        i_flags;
  56. atomic_t        i_writecount;
  57. void          *i_security;
  58. union {
  59.        void        *generic_ip;
  60. } u;
  61. #ifdef __NEED_I_SIZE_ORDERED
  62. seqcount_t        i_size_seqcount;
  63. #endif
  64. };

抱歉!评论已关闭.