Linux driver Development Block Device Learning notes

Source: Internet
Author: User


Study reference: http://www.cnblogs.com/yuanfang/archive/2010/12/24/1916231.html


1, Block equipment
 block devices keep data in blocks of fixed block size, and each block is typically between 512 bytes and 32768 bytes, and disk and SD cards are common block devices.   2, the difference between the character device and the block device: Character device block device----------------------------------------------access by byte per block accesses the device only by accessing random access by data flow The way that hangs in the file system Access 3, Linux block device processing model
   |-------------------------------------------------------------|                           |   VFS |   |-------------------------------------------------------------|     |                                         |             |   | |             Disk Caches |   |     |                                         |             |   |   |-------------------------------------------------------------| |  Disk Filesystem |  Disk Filesystem |   Block Device File |   |-----------------                     -----------------------|                     |   Mapping Layer |   |-------------------------------------------------------------|                   |   Generic Block Layer |   |-------------------------------------------------------------|                  |   I/o Scheduler Layer |   |-------------------------------------------------------------|    | BlOck Device Driver |   Block Device Driver |        |------------------------     |   --------------------   |       |            Hard Disk |   Hard Disk | |------------------------------------------------------------— |

  

VFS: Virtual file system, VFS is an encapsulation of various specific file systems, providing a unified interface for user programs to access files.
Disk cache: When a user initiates a file access request, it will first go to disk cache to find out if the file is cached, if it is in the cache, and if the data is not cached, it must be read in the file system. Mapping layer:1, first determine the file system block size, and then calculate the requested data contains how many blocks, 2, call the specific file system function to access the file Inode, determine the requested data on the disk above the logical address. The Generic block Layer:linux kernel is a unified model for fast devices, and the block device is considered as a data space composed of several sectors. To complete the relevant core functions of the block device. The I/O scheduler layer:i/o Scheduling layer is responsible for the sequence of I/O operations, using the elevator algorithm. Increasing the efficiency of the I/O Scheduler is also an aspect that affects the efficiency of the data management of the whole system to the block device. Block device Driver: Fast device driver, complete and hardware-specific interaction, data structure related to blocks

4, equipment description Gendisk structure inside the core using the Gendisk structure to represent a separate disk device, the kernel also uses the GENDISK structure to represent the partition, in this structure, many programs must be initialized by the driver. The structure is defined in <linux/genhd.h>.

4.1, Gendisk structure analysis struct Gendisk {int major;            /* Device main device number */int first_minor;                    /* Start device number */int minors;            /* Number of secondary device number, also known as the number of partitions, if the value is changed to 1, the partition */char disk_name[32];    /* Device name */struct hd_struct **part;        /* Information for partition table */int part_uevent_suppress;         struct block_device_operations *fops;/* block device operation set */struct request_queue *queue;                     /* Request queue, a pointer to manage the device IO request queue */void *private_data;                     /* Private Data */sector_t capacity;    /* Number of sectors, 512 bytes of 1 sectors, description of device capacity */...};    struct Gendisk *alloc_disk (int minors) assigns a gendisk structure, minors number of good for this device = number of partitions +1 void Del_gendisk (struct gendisk *disk) When this disk is not needed, the release Gendisk structure Endisk contains a Kobject member, which is a struct that can be referenced by a count. Use the Get_disk () and Put_disk () functions to manipulate reference counts.    Drivers do not usually need to do this.    struct Kobject *get_disk (struct gendisk *disk);    void Put_disk (struct gendisk *disk);    void Add_disk (struct gendisk *gd); Cannot be used with this device after initialization of Gendisk, should useAdd_disk registers the disk device static inline void set_capacity (struct gendisk *disk, sector_t size) to set the Gendisk capacity, the smallest unit in the block device is the sector, the sector size is generally An integer multiple of 2, the most common size is 512kb (xx>>9) representing/512 (Xx<<9) represents *512. The sector size is determined by the physical device.

5, the block_device_operationse structure is similar to the file_operations structure in the character device driver, which is used to control the operation of the device, but in most cases it is accessed in the form of Mount. User programs generally do not directly access files in a block device. Need to include<linux/blkdev.h>header file. structblock_device_operations {/*When this device is opened, it is called*/        int(*open) (structInode *,structFile *); /*called when closing/releasing*/        int(*release) (structInode *,structFile *); /*the implementation of the IOCTL () system call, the block device contains a large number of standard requests, which are processed by the Linux block device layer, so most block device-driven*/        int(*IOCTL) (structInode *,structFile *, unsigned, unsignedLong); Long(*UNLOCKED_IOCTL) (structFile *, unsigned, unsignedLong); Long(*COMPAT_IOCTL) (structFile *, unsigned, unsignedLong); int(*direct_access) (structBlock_device *, sector_t, unsignedLong*); /*the kernel cycle call checks that the drive media has not changed, the change returns non 0, and no change returns 0. Used to support removable devices, non-removable devices are not implemented*/        int(*media_changed) (structGendisk *); /*The called response media is changed to drive the necessary work*/        int(*revalidate_disk) (structGendisk *); /*the hd_geometry is filled according to the geometry information of the actuator, including the head, cylinder, sector and so on.*/        int(*getgeo) (structBlock_device *,structHd_geometry *); /*module owner, generally initialized to This_module*/        structModule *owner;    }; A function in the block_device_operations structure that does not actually read or write data, in block I/O subsystem, these operations are handled by the request function. 6, request, and bio structuresstructRequest {structList_head queuelist;/*Request Chain List*/        structList_head donelist; request_queue_t*q;/*request belongs to queue*/unsignedintCmd_flags; enumrq_cmd_type_bits Cmd_type;         sector_t sector; /*Current Sector*/sector_t Hard_sector; /*the next sector to transfer*/unsignedLongNr_sectors;/*number of sectors to transfer*/unsignedLongHard_nr_sectors;/*number of sectors to be completed*/unsignedintCurrent_nr_sectors;/*sectors currently being transferred*/unsignedintHard_cur_sectors;/*number of sectors currently to be completed*/        structBio *bio;/*requested block I/O (bio) struct list first request requests the first bio*/        structBio *biotail;/*requested bio struct chain footer request last bio*/        Char*buffer;/*Request Interrupt First bio*/        intRef_count;/*Reference Count*/        .....................    };

Note NOT finished!!!!!!

Linux driver Development Block Device Learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.