SlideShare a Scribd company logo
1 of 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?

More Related Content

What's hot

Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal BootloaderSatpal Parmar
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
 
linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
Audio in linux embedded
Audio in linux embeddedAudio in linux embedded
Audio in linux embeddedtrx2001
 
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLinaro
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image艾鍗科技
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewRajKumar Rampelli
 

What's hot (20)

Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Audio in linux embedded
Audio in linux embeddedAudio in linux embedded
Audio in linux embedded
 
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 

Viewers also liked (15)

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
 
Network Drivers
Network DriversNetwork Drivers
Network 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
 
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
 
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
 

Similar to Block Drivers

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 EnvironmentsNicolas Collery
 
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 v2Acácio Oliveira
 
TLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsTLPI Chapter 14 File Systems
TLPI Chapter 14 File SystemsShu-Yu Fu
 
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 v2Acácio Oliveira
 
[Defcon] Hardware backdooring is practical
[Defcon] Hardware backdooring is practical[Defcon] Hardware backdooring is practical
[Defcon] Hardware backdooring is practicalMoabi.com
 
Hardware backdooring is practical : slides
Hardware backdooring is practical : slidesHardware backdooring is practical : slides
Hardware backdooring is practical : slidesMoabi.com
 
Bootkits: Past, Present & Future - Virus Bulletin
Bootkits: Past, Present & Future - Virus BulletinBootkits: Past, Present & Future - Virus Bulletin
Bootkits: Past, Present & Future - Virus BulletinESET
 

Similar to 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
 
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
 
Character drivers
Character driversCharacter drivers
Character drivers
 
Linux admin course
Linux admin courseLinux admin course
Linux admin course
 

More from 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
 

Recently uploaded

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

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?