SlideShare una empresa de Scribd logo
1 de 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?

Más contenido relacionado

La actualidad más candente

Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device DriversNEEVEE Technologies
 
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
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchlinuxlab_conf
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_BootingRashila Rr
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)shimosawa
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
 
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 Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image艾鍗科技
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequenceHoucheng Lin
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Macpaul Lin
 
Linux booting procedure
Linux booting procedureLinux booting procedure
Linux booting procedureDhaval Kaneria
 

La actualidad más candente (20)

Bootloaders
BootloadersBootloaders
Bootloaders
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
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
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
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 Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Linux Kernel Image
Linux Kernel ImageLinux Kernel Image
Linux Kernel Image
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
Linux booting procedure
Linux booting procedureLinux booting procedure
Linux booting procedure
 

Destacado (14)

File System Modules
File System ModulesFile System Modules
File System Modules
 
References
ReferencesReferences
References
 
Interrupts
InterruptsInterrupts
Interrupts
 
Serial Drivers
Serial DriversSerial Drivers
Serial Drivers
 
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
 
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
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
File Systems
File SystemsFile Systems
File Systems
 

Similar a 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 a 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
 
Character drivers
Character driversCharacter drivers
Character drivers
 

Más de 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
 

Último

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...Zilliz
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
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 Takeoffsammart93
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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...Jeffrey Haguewood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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 Processorsdebabhi2
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

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...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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?