Linux block Device Driver instance
The first two blogs are familiar with the structure of Block devices. Here we will summarize some examples of block device drivers mentioned in ldd3;
1. Registration: register a block device driver with the kernel. In fact, the main device number is used to tell the kernel that this represents the block device driver.
sbull_major = register_blkdev(sbull_major, "sbull"); if (0 >= sbull_major){ printk(KERN_WARNING "sbull: unable to get major number!\n"); return -EBUSY; }
2. Define the device struct:
Struct sbull_dev {int size; // unit: slice, device size u8 * data; // data array short users; // Number of users short media_change; // The ID of the media change spinlock_t lock; // used for mutex struct request_queue * queue; // The device Request queue struct gendisk * gd; // The gendisk structure struct timer_list time; // used to simulate media changes };
3. initialize the device structure:
Memset (dev, 0, sizeof (struct sbull_dev); dev-> size = nsectors * hardsect_size; dev-> data = vmalloc (dev-> size ); if (dev-> data = NULL) {printk (KERN_NOTICE "vmalloc failure. \ n "); return;} spin_lock_init (& dev-> lock); // initialize the spin lock and assign it to the next queue
4. Create a request queue for a device:
Dev-> queue = blk_init_queue (sbull_request, & dev-> lock );
5. Allocate, initialize, and install the corresponding gendisk structure:
Dev-> gd = alloc_disk (SBULL_MINORS); if (! Dev-> gd) {printk (KERN_NOTICE "alloc_disk failure. \ n "); goto out_vfree;} dev-> gd-> major = sbull_major; dev-> gd-> first_minor = which * SBULL_MINORS; dev-> gd-> fops = & sbull_ops; dev-> gd-> queue = dev-> queue; dev-> gd-> private_data = dev; snprintf (dev-> gd-> disk_name, 32, "sbull % c", which + 'A'); set_capacity (dev-> gd, nsectors * (hardsect_size/KERNEL_SECTOR_SIZE); // use the KERNEL_SECTOR_SIZE local constant to convert the size of the 512-byte kernel sector to the actually used sector by add_disk (dev-> gd );
SBULL_MINORS is the number of sub-device numbers supported by each device. One device is sbulla and the other is sbullb. you can add partitions to a user space. The third partition on the second device may be/dev/sbullb3.
6. Set the slice size supported by the queue
Notify the kernel device of the supported slice size. The hardware slice size is placed in the queue as a parameter, rather than in gendisk. Call the following function immediately after the queue is allocated:
Blk_queue_hardsect_size (dev-> queue, hardset_size );
After the preceding function is called, the kernel uses the specified hardware sector size for our devices. All I/O Request viruses are located at the beginning of the hardware sector, the size of each request is an integer multiple of the slice size. Remember: The kernel always considers the slice size to be 512 bytes, so the number of all slice must be converted.
7. Implement the operation function:
Enable the device function:
Static int sbull_open (struct inode * inode, struct file * filp) {struct sbull_dev * dev = inode-> I _bdev-> bd_disk-> private_data; del_timer_sync (& dev-> timer ); // remove the timer filp-> private_data = dev; spin_lock (& dev-> lock); if (! Dev-> users) check_disk_change (inode-> I _bdev); // check whether the media in the drive has changed dev-> users ++; // increase the number of users (& dev-> lock); return 0 ;}
Disable the device function:
Static int sbull_release (struct inode * inode, struct file * filp) {struct sbull_dev * dev = inode-> I _bdev-> bd_disk-> private_data; spin_lock (& dev-> lock ); dev-> users --; if (! Dev-> users) {dev-> timer. expires = jiffies + INVALIDATE_DELAY; // sets the timer add_timer (& dev-> timer);} spin_unlock (& dev-> lock); return 0 ;}
Other functions are also implemented, similar to character device drivers. I will not write it here. Next let's look at the core part. For a block device driver, the core part is the request, and almost all the center of gravity is in the Request function;
8. Process request operations
Dev-> queue = blk_queue_init (& sbull_request, & dev-> lock); static void sbull_request (request_queue_t * q) {struct request * req; while (req = elv_next_request (q ))! = NULL) {// get the first unfinished request in the queue. If no, NULL is returned. After processing, the request struct sbull_dev * dev = req-> rq_disk-> private_data; if (! Blk_fs_request (req) {// determine whether a file system request is a block device request printk (KERN_NOTICE "skip non-fs request. \ n "); end_request (req, 0); continue;} // The sbull_transfer () function is the real block device request processing function sbull_transfer (dev, req-> sector, req-> current_nr_sectors, req-> buffer, rq_data_dir (req); end_request (req, 1) ;}} void end_request (struct request * req, int succeeded ); // Index Number of the Start sector of the sector, which refers to the 512-byte sector. If it is a 2048-byte sector, sector/4 and then pass // nsect to indicate the number of sectors to be passed; buffer data cache address pointer; write to indicate the direction of data transmission, namely: read/write; static void sbull_transfer (struct sbull_dev * dev, unsigned long sector, unsigned long nsect, char * buffer, int write) {unsigned long offset = sector * KERNEL_SECTOR_SIZE; unsigned long nbytes = nsect * KERNEL_SECTOR_SIZE; if (offset + nbytes)> dev-> size) {printk (KERN_NOTICE "Beyond-end write (% ld) \ n ", offset, nbytes); return;} if (write) memcpy (dev-> data + offset, buffer, nbytes); else memcpy (buffer, dev-> data + offset, nbytes );}
Address: linux block Device Driver instance
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.