Académique Documents
Professionnel Documents
Culture Documents
6
Writing Block Drivers
6-1
How block drivers are integrated into the I/O system Block driver entry points. Supporting multiple partitions on a physical disk. Managing removable media.
6.1
6-2
Denition of a block driver. How block drivers differ from character drivers. How the le system libraries connect to block drivers. Overview of DOS and raw le systems.
6-3
Block Drivers
C D
6-4
I/O System calls (open, close, etc.) map directly to le system routines, not to driver routines. The le system makes calls to the block driver. This diagram was shown earlier. The point is to remind the students about the difference between character and block drivers by building on what they have already learned.
6-5
Avoid using the RT-11 le system, as the DOS le system is more exible and tends to be faster. To create a contiguous le, use the FIOCONTIG ioctl command. To use UNIX style le naming, see the section Using Extended File Names in the dosFsLib manual pages. Using this option makes the le system no longer DOS compatible.
6-6
New le system
dosFsMkfsOptionsSet (options); /* if needed */ dosFsMkfs (fsName, pBlkDev);
or
dosFsConfigInit (pConfig, ...); dosFsDevInit (fsName, pBlkDev, pConfig); fd = open (fsName, O_RDWR, 0); ioctl (fd, DISKINIT, 0); close (fd);
Tornado Device Driver Workshop Copyright Wind River Systems
6-7
The dosFsMkfs( ) routine creates a le system with default parameters. Use dosFsDevInit( ) with an ioctl( ) FIODISKINT command to control parameters yourself. The dosFsCongInit( ) routine initializes a DOS_VOL_CONFIG structure, pCong in the example above. The structure may also be initialized directly. The dosFsMkfsOptionsSet( ) is optional. It allows setting VxWorksspecic options when creating le systems with dosFsMkfs( ).
Conguration (contd)
q
Existing le system
pVolDesc = dosFsDevInit (fsName, pBlkDev, NULL); /* Set volatile options, if needed */ dosFsVolOptionsSet (pVolDesc, options);
6-8
Using dosFsDevInit( ) with a NULL third argument, causes the volume conguration to be read from the le system existing on the disk. The dosFsVolOptionsSet( ) above is optional. It allows setting VxWorks options which are not stored on disk such as DOS_OPT_CHANGENOWARN and DOS_OPT_AUTOSYNC.
Data Integrity
dosFsLib buffers FAT and directory information for efciency. To ush buffers use:
q q
6-9
FAT (File Allocation Table) is the data structure used by DOS to manage disk blocks. The DOS_OPT_AUTOSYNC and DOS_OPT_CHANGENOWARN ags are set by bitwise ORing them into the dosvc_options eld of the DOS_VOL_CONFIG structure. The DOS_OPT_CHANGENOWARN ag:
q
DOS
DOS
Raw
6-10
A driver may support multiple devices. Multiple le systems may reside on a single physical device. Driver has no information about le systems on the device. See the Local File Systems of the Programmers Guide for additional information.
"/dos" 1
Driver Table
creat 0 1 2
dosFsCreate dosFsDelete dosFsOpen dosFsClose dosFsRead dosFsWrite dosFsIoctl
delete
open
close
read
write
ioctl
6-11
Application calls for block devices invoke le system routines directly, not driver routines. The driver may or may not be called at all. There is no block driver open routine. The dosFsLib routines may make calls to the driver, including:
q q
xxStatusChk( ) routine (if provided) xxBlkRd( ) when a new open on removable media that has changed
delete
open
close
read
write
ioctl
6-12
The block drivers read routine is called by the le system. Note that the request is specied in blocks, not bytes. The xxBlkRd( ) routine will be discussed in detail later.
6-13
Block driver initialization. The BLK_DEV structure. Block driver entry points.
6-14
6-15
6-16
The rst argument is not the device name. The block driver does not call iosDevAdd( ). A character driver returns STATUS; a block driver returns a pointer to a BLK_DEV structure.
The le system is responsible for adding the device descriptor to the device list. The le systems xxDevInit() routine denes the logical devices name. The block drivers xxDevCreate() is preparing a partition (which may be the entire disk) to hold a le system. The pointer to a BLK_DEV structure returned is used by the le system routines to connect the le system to the physical partition.
BLK_DEV Structure
bd_blkRd bd_blkWrt bd_ioctl bd_reset bd_statusChk bd_removable bd_nBlocks Address of block drivers read routine Address of block drivers write routine Address of block drivers ioctl routine Address of block drivers reset routine (optional) Address of block drivers routine to check disk status (optional) if device uses removable media, otherwise FALSE
TRUE
6-17
The BLK_DEV structure serves the same role as the driver table does for character drivers, i.e. provides the driver entry points. BLK_DEV structure is dened in blkIo.h. Read, write and ioctl routines are required in a block driver. Note that bd_nBlocks is the size of the partition, i.e. logical device, not the physical device. Discuss the reset and statusChk routines later.
6-18
6-19
The above code assumes that pMyDev is the address of a MY_DEV structure. The partitionOffset must then be used by the xxBlkRd( ) and xxBlkWrt( ) routines to perform I/O in the correct partition. The number of blocks in the partition is part of the BLK_DEV structure (bd_nBlocks).
A B
6-20
Request A2 will not be made to the driver until A1 is completed Request A3 will not be made to the driver until A2 is completed Request B1 may occur before, during or after A1. Request B2 may occur before, during or after A1 (but it will only occur after B1 is completed) Use mutex semaphores to insure exclusive access, or Allow only one partition on a physical device
6-21
Must supply a xxBlkWrt( ) routine. For read-only devices, just return ERROR. Must supply a xxBlkRd( ) routine. For write-only devices, just return ERROR. If you are supporting multiple le systems on the same device, remember that the startBlk is relative to the beginning of the partition (logical disk, not physical). The rst block is block number zero (not one).
Application
File System
Driver
6-22
An xxIoctl( ) must be supplied, even if it does nothing but fail requests as above. The ioctl commands supported by dosFsLib and rawFsLib are documented in the respective man pages. The drivers ioctl commands should be dened to be numbers above 0x1000 to avoid conicts with the le system ioctl commands.
File system is rst mounted on device. After a failed read or write and before a retry.
Return STATUS.
6-23
Note that a driver may control more than one device. Only the device in question (identied by pBlkDev) should be reset when this routine is called. The xxReset( ) routine is optional.
If ERROR returned, open( ) will fail. When failing a request set errno to indicate reason. If media changed, set bd_readyChanged to TRUE. Return OK.
6-24
Another use of xxStatusChk( ) is to check for write-protected media. If write-protected, set bd_mode to O_RDONLY. Setting bd_readyChanged is equivalent to executing either: the le system ready-change routine, e.g. dosFsReadyChanged(), or the le systems ioctl() with the FIODISKCHANGE command. It remounts the le system. This routine was designed to handle removable media. An example of why you might want to fail a xxStatusChk() call is if no disk is in the disk drive or your device is hung. Modifying bd_mode lets the le system fail the open() if the user requested O_WRONLY or O_RDWR access.
6-25
Summary
Block driver initialization xxDrv( ) xxDevCreate( ) returns a BLK_DEV pointer Do not install driver in driver table or add device to the device list.
q q
6-26
Summary
Supporting partitions
q q
Store partition offset in device descriptor In xxBlkRd and xxBlkWrt routines, add the partition offset to the start block to determine region to read or write Write a xxStatusChk routine Set bd_readyChanged eld in the BLK_DEV structure when media has changed Update bd_mode to O_RDONLY or O_RDWR
Removable Media
q q
6-27