Linux内核当中关于设备的管理

4010阅读 0评论2020-10-12 stolennnxb
分类:LINUX

在linux内核当中,主要就是字符设备和块设备,对应的结构体就是struct cdev和struct gendisk,在内核当中是通过如下结构的两个变量bdev_map和cdev_map来进行管理的:

点击(此处)折叠或打开

  1. struct kobj_map {
  2.     struct probe {
  3.         struct probe *next;
  4.         dev_t dev;
  5.         unsigned long range;
  6.         struct module *owner;
  7.         kobj_probe_t *get;
  8.         int (*lock)(dev_t, void *);
  9.         void *data;
  10.     } *probes[255];
  11.     struct mutex *lock;
  12. };
它本质上就是一个带互斥量的哈希表,根据设备的主设备号%255来找到对应的槽,槽冲突使用链表的形式进行解决
其中两个通用的设备结构的定义如下,其中gendisk稍微复杂一点,会涉及到分区等信息:

点击(此处)折叠或打开

  1. struct cdev {
  2.     struct kobject kobj;
  3.     struct module *owner;
  4.     const struct file_operations *ops;
  5.     struct list_head list;
  6.     dev_t dev;
  7.     unsigned int count;
  8. } __randomize_layout;

点击(此处)折叠或打开

  1. struct gendisk {
  2.     /* major, first_minor and minors are input parameters only,
  3.      * don't use directly. Use disk_devt() and disk_max_parts().
  4.      */
  5.     int major;            /* major number of driver */
  6.     int first_minor;
  7.     int minors; /* maximum number of minors, =1 for
  8.                                          * disks that can't be partitioned. */

  9.     char disk_name[DISK_NAME_LEN];    /* name of major driver */
  10.     char *(*devnode)(struct gendisk *gd, umode_t *mode);

  11.     unsigned int events;        /* supported events */
  12.     unsigned int async_events;    /* async events, subset of all */

  13.     /* Array of pointers to partitions indexed by partno.
  14.      * Protected with matching bdev lock but stat and other
  15.      * non-critical accesses use RCU. Always access through
  16.      * helpers.
  17.      */
  18.     struct disk_part_tbl __rcu *part_tbl;// 分区表
  19.     struct hd_struct part0;

  20.     const struct block_device_operations *fops;
  21.     struct request_queue *queue;
  22.     void *private_data;

  23.     int flags;
  24.     struct kobject *slave_dir;

  25.     struct timer_rand_state *random;
  26.     atomic_t sync_io;        /* RAID */
  27.     struct disk_events *ev;
  28. #ifdef CONFIG_BLK_DEV_INTEGRITY
  29.     struct kobject integrity_kobj;
  30. #endif    /* CONFIG_BLK_DEV_INTEGRITY */
  31.     int node_id;
  32.     struct badblocks *bb;
  33. };




上一篇:linux用户态ipc实现机制
下一篇:Linux在打开字符设备文件的时候都干了哪些工作