Linux Block devices-finishing (i)

Source: Internet
Author: User

1. Basic Concepts:

Linux Device Driver Development (Song Baohua):

The difference between a character device and a block device I/O operation is as follows.
(1) Block devices can only accept input and return output in blocks, while character devices are in bytes.

Most devices are character devices because they do not need to be buffered and do not operate at a fixed block size.
(2) block devices have buffers for I/O requests, so they can choose in what order to respond, and character devices do not need to be buffered and read and written directly.

The order in which the read and write is adjusted for storage devices is significant because the read-write sequential sector is faster than the detached sector.
(3) Character devices can only be read and written sequentially, while block devices are randomly accessible.

Although block devices can be accessed randomly, the sequential organization of block device access can improve performance for mechanical devices such as disks.

For a block device, the most important thing is to process the request, and the queue and consolidation of the request is resolved by the I/O scheduling algorithm,

Therefore, the core of the block device driver is the request handler function or the "Manufacturing request" function.

The smallest addressable unit in a block device is a sector, the sector size is generally 2 integer multiples, and the most common size is 512 bytes.

The size of the sector is the physical property of the device, and the sector is the basic unit of all the block devices, which cannot be addressed and manipulated in comparison to its smaller units.

However, many block devices can transmit multiple sectors at once. Although the sector size of most block devices is 512 bytes,

However, other sectors of the size are also common, for example, many Cd-ro M disk sector is 2KB.

Regardless of the actual sector size of the physical device, the sectors that the kernel interacts with the block device driver are in 512-byte units

The following work is usually done in block device-driven module loading functions.
1 Assign, initialize the request queue, bind the request queue, and request the function.
2 Assign, initialize Gendisk, assign value to Gendisk's major, FoPs, queue and so on, add Gendisk finally.
3 Register block device drivers.

Specific code:

[CPP]View PlainCopy
  1. #include <linux/module.h>
  2. #include <linux/errno.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/mm.h>
  5. #include <linux/fs.h>
  6. #include <linux/kernel.h>
  7. #include <linux/timer.h>
  8. #include <linux/genhd.h>
  9. #include <linux/hdreg.h>
  10. #include <linux/ioport.h>
  11. #include <linux/init.h>
  12. #include <linux/wait.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/blkpg.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <asm/system.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/dma.h>
  20. Static struct Gendisk *ramblock_disk;
  21. Static request_queue_t *ramblock_queue;
  22. static int major;
  23. Static Define_spinlock (Ramblock_lock);
  24. Static struct Block_device_operations ramblock_fops = {
  25. . Owner = This_module,
  26. };
  27. #define RAMBLOCK_SIZE (1024*1024)
  28. static void Do_ramblock_request (request_queue_t * q)
  29. {
  30. static int cnt = 0;
  31. PRINTK ("Do_ramblock_request%d\n", ++cnt);
  32. }
  33. static int ramblock_init (void)
  34. {
  35. / * 1. Assign a gendisk struct * /
  36. Ramblock_disk = Alloc_disk (16); /* Secondary device number: number of partitions +1 */
  37. / * 2. Set * /
  38. /* 2.1 Assign/Set queue: Provide read/write capability */
  39. Ramblock_queue = Blk_init_queue (Do_ramblock_request, &ramblock_lock);
  40. Ramblock_disk->queue = Ramblock_queue;
  41. /* 2.2 Set other properties: such as capacity */
  42. Major = Register_blkdev (0, "Ramblock"); / * cat/proc/devices * /
  43. Ramblock_disk->major = major;
  44. Ramblock_disk->first_minor = 0;
  45. sprintf (Ramblock_disk->disk_name, "Ramblock");
  46. Ramblock_disk->fops = &ramblock_fops;
  47. Set_capacity (Ramblock_disk, ramblock_size/512);
  48. / * 3. Register * /
  49. Add_disk (Ramblock_disk);
  50. return 0;
  51. }
  52. static void Ramblock_exit (void)
  53. {
  54. Unregister_blkdev (Major, "Ramblock");
  55. Del_gendisk (Ramblock_disk);
  56. Put_disk (Ramblock_disk);
  57. Blk_cleanup_queue (Ramblock_queue);
  58. }
  59. Module_init (Ramblock_init);
  60. Module_exit (Ramblock_exit);
  61. Module_license ("GPL");

Linux Block devices-finishing (i)

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.