Chap 10. THE I/O SUBSYSTEM
SS Lab 석사 1학기 이상근
Contents
Driver Interfaces Disk Drivers Terminal Drivers Streams
Driver Interfaces
Unix contains two types of devices
– Block devices – Raw or character devices
Device file is distinguished from other by file type stored in inode => block, or character special if both interface, represented by two files System call open, close, read, write => appropriate meaning for devices ioctl system call is not applicable to regular file(character devices only) fcntl(applicable to all file)
Device File
Creating a device file => mknod command
– Ex) mknod /dev/tty13 c 2 13 – Major number indicates device type – Minor number indicates a unit of device
Device special files do not have to be created every time the system is booted => only if configuration changes, such as adding device
Driver Entry Points
File Subsystem
open close read write ioctl open mount close unmount read write
buffer cache calls
Character Device Switch Table Block Device Switch Table
open close read write ioctl Driver device interrupt handler
Driver Entry Points
open
close Driver
strategy
device interrupt handler
Interrupt Vector
Interrupt Vector
Device Interrupts
Sample Block and Character Device Switch Tables
Block device switch table
Entry
0 1
Open
gdopen gtopen
Close
gdclose gtclose
Strategy
gdstrategy gtstrategy
Character device switch table Entry 0 1 2 3 4 5 open conopen dzbopen syopen nulldev gdopen gtopen close read write conwrite dzbwrite sywrite mmwrite gdwrite gtwrite Ioctl conioctl dzbioctl syioctl nodev nodev nodev conclose conread dzbclose dzbread nulldev nulldev gdclose gtclose syread mmread gdread gtread
System Calls and the Driver Interface
The kernel follows pointers from the user file descripter to kernel file table and inode Using major number as an index into the appropriate table Calls the driver function according to the system call being made Passing minor number as parameter Inode of special file is not locked while the kernel executes the driver => because kernel cannot determine how long a process will sleep
Open
Algorithm open (p. 317) Input: pathname, openmode Output: file descriptor { convert path name to inode, increment inode reference count, allocate entry in file table, user file descriptor, as in open of regular file; get major, minor number from inode;
save context (algorithm setjmp) in case of long jump from driver;
if(block device) { Use major number as index to block device switch table; Call driver open procedure for index; Pass minor number, open mode; } else { Use major number as index to character device switch table; Call driver open procedure for index; Pass minor number, open modes; } If(open fails in driver) Decrement file table, inode counts; } }
Close
Algorithm close (p. 319) Input: file descriptor Output: none { Do regular close algorithm (chap 5xxx); If(file table reference count not 0) Goto finish; If(character device) { Use major number to index into character device switch table; Call driver close routine; parameter minor number; }
If(block device) { If(device mounted) Goto finish; Write device blocks in buffer cache to device; Use major number to index into block device switch table; Call driver close routine; parameter minor number; Invalidate device blocks still in buffer cache; } Finish: Release inode; }
Read & write
Similar to regular file read and write algorithm Memory mapped I/O: meaning that certain addresses in the kernel address space are not locations in physical memory but are special registers control particular devices Programmed I/O: meaning that the machine contains instructions to control devices – Ex) IA32 Architecture • int inb(int read_addr); • void outb(int write_addr, char value);
DMA: driver can issue control issue control sequences to a device to setup DMA
Strategy interface
Kernel uses strategy interface to transmit data between the buffer cache and device Read() & write() of character devices sometimes uses strategy procedure to transfer data directly between device and user address space. The strategy procedure may queue I/O jobs for a device on a work list or do more sophisticated processing to schedule I/O jobs The kernel passes a buffer header address to the driver strategy procedure When buffer cache algorithms (bread, bwrite) access the disk, they invoke strategy procedure indicated by major number
Reading a disk block
algorithm bread /*block bread*/ input: file system block number output: buffer containing data { get buffer for block(algorithm getblk); if(buffer data valid) return buffer; initiate disk read; sleep(event disk read complete); return(buffer); }
Writing a disk block
algorithm bwrite /*block write*/ input: buffer output: none { initiate disk write; if(I/O synchronous) { sleep(event I/O complete); release buffer(algorithm brelse); } else if(buffer marked for delayed write) mark buffer to put at head of free list; }
Ioctl
ioctl provides a general, catch-all entry point for device specific commands allowing a process to set hardware options associated with a device and software options associated with the driver Syntax: ioctl(fd, command, arg); fd: file descriptor returned by open system call Command: driver specific Arg: pointer to a structure depends on command
Other File System Related Calls
File system calls such as stat and chmod work for devices as they do for regular file => manipulate inode without accessing the driver lseek => updates file table offset but does no driver specific operations
Device Interrupts
Peripheral Devices
tty00 tty01
tty07 tty08 tty09 tty14 console printer00 printer03
Hardware Backplane
Interrupt Vector
ttyintr 0
ttyintr 1
consintr printintr 0
Interrupt Handlers
Interrupt cause kernel to execute interrupt handler based on correlation of device and offset in interrupt vector table many physical devices can be associated with one interrupt vector entry
– > the driver must be able to resolve which device caused interrupt
Device number used by the interrupt handler identifies a hardware unit minor number in the device file identifies a device for the kernel
Disk Sections
“The [disk] pack to be broken up into more manageable pieces.”
Section Start Block Length in Blocks
Size of block = 512 bytes 0 1 2 0 64000 168000 64000 944000 840000
3
4 5 6 7
336000
504000 672000 840000 0
672000
504000 336000 168000 1008000
Disk Drivers(cont.)
Disk driver translates a file system consisting of a logical device number and block number, to a particular sector on the disk Driver gets the address in one of two ways
– Strategy procedure: use buffer, buffer header contains device and block number – Read, write procedure: logical device number as parameter – after they convert the byte offset saved in u area to the appropriate block address – Add block number of the file system to the start sector number(driver maintaining the start sector number)
Reading Disk Data Using Block and Raw Interface
#include “fcntl.h” main() {
char buf1[4096], buf2[4096]; int fd1, fd2, i; If(((fd1 = open(“/dev/dsk5”, O_RDONLY)) == -1) || ((fd2 = open(“/dev/rdsk5”, O_RDONLY)) == -1)) { printf(“failure on open\n”); exit(); } lseek(fd1, 8192L, 0); lseek(fd2, 8192L, 0);
if((read(fd1, buf1, sizeof(buf1)) == -1) || (read(fd2, buf2, sizeof(buf2)) == -1)) { printf(“failure on read\n”); exit(); } for(i = 0; i ex) tape drive, – If one process writes a block device and a second process reads a raw devices at the same time => fsck to mounted file system is dangerous
Terminal Drivers
Terminal driver’s function: to control transmisson of data to and from terminals To accommodate interactive use of UNIX, terminal driver contains an internal interface to a line discipline module Line discipline module has two modes:
– canonical mode: line discipline converts raw data sequences to the canonical form before sending data to receiving process – raw mode: without conversion
Function of Line Discipline
To parse input strings into lines; To process erase characters; To process a “kill” characters that invalidates all characters typed so far on the current line; To echo(write) received characters to the terminal;
Function of Line Discipline(cont.)
To expand output such as tab characters to a sequence of blank space To generate signals to processes for terminal hangups, line breaks, or in response to a user hitting the delete key; To allow a raw mode that does not interpret special characters such as a erase, kill or carriage return
Call Sequence and Data Flow through Line Discipline
Data Flow
Process read/write
Control Flow
Process read/write
output
Line discipline
input
Terminal driver read/write
Terminal driver
Line discipline
Driver input/output
Device input/output