E N D
1. BLOCK DEVICE DRIVER LAB 12
2. Linux Device Driver Architecture
3. Quick Functions Reference #include <Linux/fs.h>
int register_blkdev(unsigned int major, const char *name);
int unregister_blkdev(unsigned int major, const char *name);
struct block_device_operations
#include <linux/genhd.h>
struct gendisk *alloc_disk(int minors);
void add_disk(struct gendisk *gd);
void set_capacity(struct gendisk *gd, setor_t sectors);
void put_disk(struct gendisk *gd);
#include <linux/blkdev.h>
#define blk_fs_request(rq) ((rq)->flags & REQ_CMD)
request_queue_t blk_init_queue(request_fn_proc *request, spinlock_t *lock);
struct request *elv_next_request(request_queue_t *queue);
void end_request(struct request *req, int success);
#include <mm/vmalloc.h>
void *vmalloc(unsigned long size);
4. Quick Functions Reference (cont’) #include <linux/fs.h>
struct block_device_operations
struct block_device
struct inode
#include <linuix/genhd.h>
struct gendisk
#include <linux/blkdev.h>
struct request
5. struct gendisk
6. struct block_device
7. struct block_device_operations
8. Relationship between these structures
9. Simple block device driver: sbull Needed files:
sbull.c
Makefile
Driver description:
it allocates 10 MB memory space to simulate a ramdisk which is like you insert a disk into the system
the ramdisk has only one partition
many file operations can be performed on it
mkfs, mount, umount, cp, ls, …
10. Simple block device driver: sbull (cont’) Needed functions:
sbull_init()
sbull_exit()
sbull_open()
sbull_release()
sbull_request()
sbull_transfer()
11. Source code of sbull.c include and define
12. Source code of sbull.c (cont’) struct and function prototype:
13. Source code of sbull.c (cont’) struct Device and open, release methods:
14. Source code of sbull.c (cont’) sbull_init method:
15. Source code of sbull.c (cont’) sbull_init method: (cont’)
16. Source code of sbull.c (cont’) sbull_request method:
17. Source code of sbull.c (cont’) sbull_transfer method:
18. Source code of sbull.c (cont’) sbull_exit method and module-related macros:
19. The Makefile of sbull
20. LAB 1 Create the simple block device driver and insert into the kernel:
make
sudo insmod sbull.ko
cat /proc/devices | grep sbull
21. LAB 1 (cont’) Create the device node and mount it:
sudo mknod /dev/sbulla b XXX 0
replaces XXX to the major number you get above
Ubuntu may create the /dev/sbulla for us
sudo mkfs.ext2 /dev/sbulla
mkdir temp
sudo mount /dev/sbulla temp
df
22. LAB 1 (cont’)
23. LAB 1 (cont’) Test the read and write abilities:
cd temp
sudo mkdir HANEL
cd ..
sudo umount temp
mkdir temp2
sudo mount /dev/sbulla temp2
ls temp2
24. LAB 1 (cont’)
25. LAB 1 (cont’) Remove the sbull module from the kernel:
sudo umount temp2
sudo rmmod sbull
Ubuntu will also clear the /dev/sbulla for us
26. LAB 2 In this exercise, a extended version sbull will be introduced
it allocates 10 MB memory space to simulate a ramdisk which is like you insert a disk into the system
the ramdisk has 2 partitions (partitionable)
many file operations can be performed on it
mkfs, mount, umount, cp, ls, …
27. LAB 2 (cont’) In order to let the sbull able to be partitioned, there are something needed be done:
sets SBULL_MINORS to 2
adds new structure member “short users” in sbull_dev
adds one new function in sbull_ops structure
alters the sbull_open and sbull_release functions
28. LAB 2 (cont’) The structure method sbull_ioctl():
29. LAB 2 (cont’) The functions sbull_open and sbull_release:
30. LAB 2 (cont’) Test the extended sbull modules:
make clean
make
sudo insmod sbull.ko
sudo fdisk –H 16 /dev/sbulla
31. LAB 2 (cont’)
32. LAB 2 (cont’) Now we have two partitions in the sbull ramdisk
Formats they by the following instructions:
sudo mkfs.ext2 /dev/sbulla1
sudo mkfs.ext2 /dev/sbulla2
sudo mount /dev/sbulla1 temp
sudo mount /dev/sbulla2 temp2
df
33. LAB 3 Please format the sbull in LAB1 by instruction:
sudo mkfs.vfat –F16 /dev/sbulla
and observe what happened
Try the same instruction executed on the sbulla in LAB2
Based on the LAB2, give me one block device with:
20MB and can be partitioned automatically:
you can consult the sbull of LDD here
34. LAB 3 (cont’)
35. LAB 3 There are two errors in the sbull of O’Reilly:
comment out the #include <linux/config.h>
change the line 181
from “end_that_request_last(req);”
to “end_that_request_last(req, 1);”
if you want to try that one, just type:
make
sudo insmod sbull.ko
ls –al /dev/sbull*
36. Reference Driver porting: a simple block driver
“Linux Device Drivers” 3rd Edition
Cross-Referencing Linux
Linux Block Device Architecture