SlideShare ist ein Scribd-Unternehmen logo
1 von 21
© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block Drivers
2© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
Why the need for the Block Layer?
Decoding a Block Device in Linux
Role of Block Drivers
Writing a Block Driver
3© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block vs Character
Concept Similarities
Device Registration
Usage of Device Files
Major & Minor number
File Operations
Then, why a different category?
To access block-oriented devices (Really?)
To achieve buffering for efficiency
To enable a generic caching abstraction
To provide a device independent block access of data
4© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System-wide Block Devices
Category is to Major
IDE: 3; SCSI: 8; ...
Naming Convention (Try: ls -l /dev/sd*)
IDE: hd*; SCSI: sd*; ...
Disk is to Minor
Typically limited to 4 per system, represented using a, b, ...
Partition also is to Minor
256 / 4 = 64 to each disk
First one for the whole disk, e.g. hda
Remaining for the partitions, thus limiting to 63, e.g. hda1
5© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The Generic Hard Disk
Disk or
Platter
Tracks
Sectors
6© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
The Generic Hard Disk
.
.
.
.
.
.
Spindle
Heads
Cylinder
7© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Computing a Generic Hard Disk
Example (Hard Disk)
Heads (or Platters): 0 – 9
Tracks (or Cylinders): 0 – 24
Sectors: 1 – 64
Size of the Hard Disk
10 x 25 x 64 x 512 bytes = 8000KiB
Device independent numbering
(h, t, s) → 64 * (10 * t + h) + s → (1 - 16000)
8© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Partitioning Visualization
2 3 4 5 7 86 N-5 N-4 N-3 N-2 N-1 N
Linearized sector numbers
1MBR
MBR P1 P2 P4P3ExtBR BR BR BR
ExtL1 RemLBR L2 RemL3LBR L4LBR LnLBR
Boot Code
Partition Table
Sec. Boot Code
Logical Part. Table
BRLBR
9© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Partitioning a Block Device
First Sector – Master Boot Record (MBR)
Contains Boot Info
Contains Physical Partition Table
Maximum Physical Partitions: 4
At max 1 as Extended Partition
Rest as Primary Partition
Extended could be further partitioned into
Logical Partitions
In each partition
First Sector – Boot Record (BR)
Remaining for File System / Format
Extended Partition BR contains the Logical Partition Table
10© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block Input / Output
Virtual File System (VFS) Layer
Block Driver
Individual Filesystems (ext3, vfat, ...)
Buffer Cache (Page Cache)
I/O Schedulers
Block Driver
User Space
Storage Space
Kernel Space
CD
Drive
......
Disk
File I/O
Request Queue Request Queue
......
11© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Now, let's write a Driver
to
Achieve the Purpose
12© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block Registration
Driver Registration
Header: <linux/fs.h>
APIs
int register_blkdev(major, name);
int unregister_blkdev(major, name);
Disk Drive Registration
Header: <linux/genhd.h>
Data Structure: struct gendisk *gd
APIs
struct gendisk *alloc_disk(minors); void put_disk(gd);
void add_disk(gd); void del_gendisk(gd);
13© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
struct gendisk
int major
int first_minor
int minors
char disk_name[32]
struct block_device_operations *fops
struct request_queue *queue
int flags (GENHD_FL_REMOVABLE, ...)
sector_t nr_sects → struct hd_struct part0
void *private_data
14© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
struct hd_struct
sector_t start_sect
sector_t nr_sects
sector_t alignment_offset
...
15© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block Device Operations
Header: <linux/blkdev.h>
System Calls (till <= 2.6.27)
int open(struct inode *i, struct file *f);
int close(struct inode *i, struct file *f);
int ioctl(struct inode *i, struct file *f, cmd, arg);
int media_changed(struct gendisk *gd);
int revalidate_disk(struct gendisk *gd);
...
Other Important Fields
struct module *owner;
16© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Block Device Operations
Header: <linux/blkdev.h>
System Calls (after 2.6.27)
int (*open)(struct block_device *, fmode_t);
int (*release)(struct block_device *, fmode_t);
int (*ioctl)(struct block_device *, fmode_t, cmd, arg);
int (*media_changed)(struct gendisk *gd);
int (*revalidate_disk)(struct gendisk *gd);
int (*getgeo)(struct block_device *, struct hd_geometry *);
...
Other Important Fields
struct module *owner;
17© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Request Queues & Processing
Header: <linux/blkdev.h>
Types
request_queue_t *q;
request_fn_proc rqf;
struct request *req;
APIs
q = blk_init_queue(rqf, lock);
blk_cleanup_queue(q);
req = blk_fetch_request(q);
18© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Requests
Interfaces
rq_data_dir(req) – Operation type
zero: read from device
non-zero: write to the device
blk_req_pos(req) – Starting sector
blk_req_sectors(req) – Total sectors
Iterator for extracting buffers from bio_vec
Request Function
typedef void (*request_fn_proc)(request_queue_t
*queue);
19© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Disk on RAM
Let's try out the RAM Block Driver
Horizontal: Disk on RAM
ram_device.c, ram_device.h
Vertical: Block Driver
ram_block.c
Useful commands
blockdev
dd
fdisk
20© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
Understood the need for the Block Layer
Decoding a Block Device in Linux
Role of Block Drivers
Writing a Block Driver
Registration
Block Device Operations
Request & Request Queues
21© 2010-16 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

Weitere ähnliche Inhalte

Was ist angesagt?

Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
Houcheng Lin
 

Was ist angesagt? (20)

Linux Device Tree
Linux Device TreeLinux Device Tree
Linux Device Tree
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
Linux dma engine
Linux dma engineLinux dma engine
Linux dma engine
 
U boot-boot-flow
U boot-boot-flowU boot-boot-flow
U boot-boot-flow
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
Board Bringup
Board BringupBoard Bringup
Board Bringup
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
USB Drivers
USB DriversUSB Drivers
USB Drivers
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Introduction to armv8 aarch64
Introduction to armv8 aarch64Introduction to armv8 aarch64
Introduction to armv8 aarch64
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Basics of boot-loader
Basics of boot-loaderBasics of boot-loader
Basics of boot-loader
 
LCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted FirmwareLCU13: An Introduction to ARM Trusted Firmware
LCU13: An Introduction to ARM Trusted Firmware
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Character drivers
Character driversCharacter drivers
Character drivers
 

Andere mochten auch (19)

File System Modules
File System ModulesFile System Modules
File System Modules
 
References
ReferencesReferences
References
 
Interrupts
InterruptsInterrupts
Interrupts
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Embedded C
Embedded CEmbedded C
Embedded C
 
gcc and friends
gcc and friendsgcc and friends
gcc and friends
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
File Systems
File SystemsFile Systems
File Systems
 

Ähnlich wie Block Drivers

TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
Shu-Yu Fu
 

Ähnlich wie Block Drivers (20)

File System Modules
File System ModulesFile System Modules
File System Modules
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
Linux IO
Linux IOLinux IO
Linux IO
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Let Me Pick Your Brain - Remote Forensics in Hardened Environments
Let Me Pick Your Brain - Remote Forensics in Hardened EnvironmentsLet Me Pick Your Brain - Remote Forensics in Hardened Environments
Let Me Pick Your Brain - Remote Forensics in Hardened Environments
 
101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2101 2.1 design hard disk layout v2
101 2.1 design hard disk layout v2
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File Systems
 
2.1 design hard disk layout v2
2.1 design hard disk layout v22.1 design hard disk layout v2
2.1 design hard disk layout v2
 
[Defcon] Hardware backdooring is practical
[Defcon] Hardware backdooring is practical[Defcon] Hardware backdooring is practical
[Defcon] Hardware backdooring is practical
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Linux
LinuxLinux
Linux
 
Hardware backdooring is practical : slides
Hardware backdooring is practical : slidesHardware backdooring is practical : slides
Hardware backdooring is practical : slides
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Bootkits: Past, Present & Future - Virus Bulletin
Bootkits: Past, Present & Future - Virus BulletinBootkits: Past, Present & Future - Virus Bulletin
Bootkits: Past, Present & Future - Virus Bulletin
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Linux admin course
Linux admin courseLinux admin course
Linux admin course
 

Mehr von Anil Kumar Pugalia (18)

Processes
ProcessesProcesses
Processes
 
System Calls
System CallsSystem Calls
System Calls
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Power of vi
Power of viPower of vi
Power of vi
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
System Calls
System CallsSystem Calls
System Calls
 
Timers
TimersTimers
Timers
 
Threads
ThreadsThreads
Threads
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Processes
ProcessesProcesses
Processes
 
Signals
SignalsSignals
Signals
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Block Drivers

  • 1. © 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block Drivers
  • 2. 2© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? Why the need for the Block Layer? Decoding a Block Device in Linux Role of Block Drivers Writing a Block Driver
  • 3. 3© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block vs Character Concept Similarities Device Registration Usage of Device Files Major & Minor number File Operations Then, why a different category? To access block-oriented devices (Really?) To achieve buffering for efficiency To enable a generic caching abstraction To provide a device independent block access of data
  • 4. 4© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. System-wide Block Devices Category is to Major IDE: 3; SCSI: 8; ... Naming Convention (Try: ls -l /dev/sd*) IDE: hd*; SCSI: sd*; ... Disk is to Minor Typically limited to 4 per system, represented using a, b, ... Partition also is to Minor 256 / 4 = 64 to each disk First one for the whole disk, e.g. hda Remaining for the partitions, thus limiting to 63, e.g. hda1
  • 5. 5© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The Generic Hard Disk Disk or Platter Tracks Sectors
  • 6. 6© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. The Generic Hard Disk . . . . . . Spindle Heads Cylinder
  • 7. 7© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Computing a Generic Hard Disk Example (Hard Disk) Heads (or Platters): 0 – 9 Tracks (or Cylinders): 0 – 24 Sectors: 1 – 64 Size of the Hard Disk 10 x 25 x 64 x 512 bytes = 8000KiB Device independent numbering (h, t, s) → 64 * (10 * t + h) + s → (1 - 16000)
  • 8. 8© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Partitioning Visualization 2 3 4 5 7 86 N-5 N-4 N-3 N-2 N-1 N Linearized sector numbers 1MBR MBR P1 P2 P4P3ExtBR BR BR BR ExtL1 RemLBR L2 RemL3LBR L4LBR LnLBR Boot Code Partition Table Sec. Boot Code Logical Part. Table BRLBR
  • 9. 9© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Partitioning a Block Device First Sector – Master Boot Record (MBR) Contains Boot Info Contains Physical Partition Table Maximum Physical Partitions: 4 At max 1 as Extended Partition Rest as Primary Partition Extended could be further partitioned into Logical Partitions In each partition First Sector – Boot Record (BR) Remaining for File System / Format Extended Partition BR contains the Logical Partition Table
  • 10. 10© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block Input / Output Virtual File System (VFS) Layer Block Driver Individual Filesystems (ext3, vfat, ...) Buffer Cache (Page Cache) I/O Schedulers Block Driver User Space Storage Space Kernel Space CD Drive ...... Disk File I/O Request Queue Request Queue ......
  • 11. 11© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Now, let's write a Driver to Achieve the Purpose
  • 12. 12© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block Registration Driver Registration Header: <linux/fs.h> APIs int register_blkdev(major, name); int unregister_blkdev(major, name); Disk Drive Registration Header: <linux/genhd.h> Data Structure: struct gendisk *gd APIs struct gendisk *alloc_disk(minors); void put_disk(gd); void add_disk(gd); void del_gendisk(gd);
  • 13. 13© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. struct gendisk int major int first_minor int minors char disk_name[32] struct block_device_operations *fops struct request_queue *queue int flags (GENHD_FL_REMOVABLE, ...) sector_t nr_sects → struct hd_struct part0 void *private_data
  • 14. 14© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. struct hd_struct sector_t start_sect sector_t nr_sects sector_t alignment_offset ...
  • 15. 15© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block Device Operations Header: <linux/blkdev.h> System Calls (till <= 2.6.27) int open(struct inode *i, struct file *f); int close(struct inode *i, struct file *f); int ioctl(struct inode *i, struct file *f, cmd, arg); int media_changed(struct gendisk *gd); int revalidate_disk(struct gendisk *gd); ... Other Important Fields struct module *owner;
  • 16. 16© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Block Device Operations Header: <linux/blkdev.h> System Calls (after 2.6.27) int (*open)(struct block_device *, fmode_t); int (*release)(struct block_device *, fmode_t); int (*ioctl)(struct block_device *, fmode_t, cmd, arg); int (*media_changed)(struct gendisk *gd); int (*revalidate_disk)(struct gendisk *gd); int (*getgeo)(struct block_device *, struct hd_geometry *); ... Other Important Fields struct module *owner;
  • 17. 17© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Request Queues & Processing Header: <linux/blkdev.h> Types request_queue_t *q; request_fn_proc rqf; struct request *req; APIs q = blk_init_queue(rqf, lock); blk_cleanup_queue(q); req = blk_fetch_request(q);
  • 18. 18© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Requests Interfaces rq_data_dir(req) – Operation type zero: read from device non-zero: write to the device blk_req_pos(req) – Starting sector blk_req_sectors(req) – Total sectors Iterator for extracting buffers from bio_vec Request Function typedef void (*request_fn_proc)(request_queue_t *queue);
  • 19. 19© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Disk on RAM Let's try out the RAM Block Driver Horizontal: Disk on RAM ram_device.c, ram_device.h Vertical: Block Driver ram_block.c Useful commands blockdev dd fdisk
  • 20. 20© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? Understood the need for the Block Layer Decoding a Block Device in Linux Role of Block Drivers Writing a Block Driver Registration Block Device Operations Request & Request Queues
  • 21. 21© 2010-16 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?