First, refer to the kernel code in z2ram.c
Simulate a block device driver using memory
The specific process of a block device driver:
1. Register the block device with the kernel
int register_blkdev(unsigned int major, const char *name)
Parameter 1: major write 0 to automatically get the major device number
Parameter 2: The registered name
Return value: When parameter 1 is written as 0, it returns the applied major device number
2. Allocate the number of disks
struct gendisk *alloc_disk(int minors)
Return value: Pointer to the generic block device data structure
3. Initialize the members of the gendisk pointer returned by alloc_disk
1) major Initialization of the major device number
2) first_minor Initialization of the first minor device number, generally 0
3) struct block_device_operations *fops;
Interfaces provided upward
4) disk_name Initialization of the name
5) Set the disk capacity
static inline void set_capacity(struct gendisk *disk, sector_t size)
6) struct request_queue *queue;
Initialization of the request queue
Use the following function for initialization:
struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Parameter 1: Callback function
Parameter 2: Used to resolve race conditions
Callback function request_fn_proc *rfn
1. Get the request req = blk_fetch_request(q);
2. Determine read and write operations based on the request and perform the corresponding memcpy
blk_init_queue
—->blk_init_queue_node
—-> blk_init_allocated_queue
—> blk_queue_make_request(q, blk_queue_bio);
—>blk_queue_bio Elevator optimization algorithm
4. Add the initialized gendisk pointer to the kernel
add_disk(struct gendisk *)
5. blk_register_region Create a device node in the dev directory named disk_name
6. Reverse operation
blk_unregister_region Destroy the device node
unregister_blkdev Unregister the block device
del_gendisk(z2ram_gendisk); Delete gendisk
put_disk(z2ram_gendisk); Release disk
blk_cleanup_queue(z2_queue); Release request queue
Testing method:
1. mkdosfs /dev/fs_blk0
2. mount /dev/fs_blk0 /mnt
3. cd /mnt
touch 1.c 2.c 3.c
4. cd ..
umount /mnt
ls to check the contents under /mnt
5. Remount to check the contents under mnt, which should include 1.c 2.c 3.c
Today’s sharing ends here. If you feel that this does not satisfy your learning desire, you can also click the original text to read and sign up for Huqing Yuanjian!
Recommended Recent Articles
1. Master 20 Linux commands, essential for beginners’ interviews!
2. What are the career development directions in the embedded field?
3. What are the differences between working in Internet companies and software companies?
4. Interview sharing from 2017 Sohu, LeTV, Didi, NetEase, etc.
5. Learning embedded systems from 0 to 1 is this simple! (Knowledge framework diagram)
6. The high-paying employment list for embedded systems in April is crazy, with the national highest salary breaking 15k!
Reply with any number from “0-71” to view selected previous articles!
Hold the QR code for 3 seconds
Make friends with 100,000 programming experts
Daily dry goods to satisfy you
(Remember to scan the QR code!)
Or search for Huqing Yuanjian on WeChat to follow us
Free lectures | Dry goods sharing | Programmer life | Employment recruitment
High-endITEmployment training experts
m.embedu.org