Operating System
CSE-222
Vikas Bajpai
Disk Management
Disk Management:
• Disk Formatting
• Boot Block
• Bad Blocks
Disk Formatting :
• A new magnetic disk is in a blank slate, just a platter of a magnetic
recording material.
• Low-level formatting: Before a disk can store data, is must be divided
into sectors that the disk controller can read-write. This process is
called low-level formatting or physical formatting.
• This low-level formatting fills the disk with a special data structure for
each sector.
• The data structure for a sector consists of a header, a data area
(Sector sizes are traditionally 512 bytes, but may be larger,
particularly in larger drives) and a trailer.
Disk Formatting :
• The header and trailer contain information used by disk controller, such as
sector number and Error-correcting code (ECC).
• Error-correcting codes, ECC, allows damaged sectors to not only be
detected, but in many cases for the damaged data to be recovered
(depending on the extent of the damage).
• ECC calculation is performed with every disk read or write, and if damage is
detected but the data is recoverable, then a soft error has occurred. Soft
errors are generally handled by the on-board disk controller, and never
seen by the OS.
• Once the disk is low-level formatted, the next step is to partition the drive
into one or more separate partitions. This step must be completed even if
the disk is to be used as a single large partition, so that the partition table
can be written to the beginning of the disk.
Disk Formatting :
• Most hard disks are low level formatted at the factory as a part of the
manufacturing process.
• After partitioning, then the filesystems must be logically
formatted, which involves laying down the master directory
information ( FAT table or inode structure ), initializing free lists, and
creating at least the root directory of the filesystem.
Disk Formatting :
• Disk partitions which are to be used as raw devices are not logically
formatted. This saves the overhead and disk space of the filesystem
structure, but requires that the application program manage its own disk
storage requirements.
• Logical formatting: After partitioning, it is the next step in which OS stores
initial file-system, data structures onto the disk. These data structures may
include maps of free and allocated space (a FAT or inodes) and an initial
empty directory.
• To increase efficiency, most file systems group blocks together into
larger chunks, called as clusters.
Boot Block:
• For a computer to start running, when it is powered up or rebooted, it
must have an initial program to run.
• This initial bootstrap program initializes all aspects of the system,
from CPU registers to device controllers and the content of main
memory, and then starts the operating system.
• For this job, the bootstrap program finds the OS kernel on disk, loads
that kernel into memory, and jumps into an initial address to begin
the operating-system execution.
Boot Block:
• Mostly, this bootstrap is stored in Read-only-memory (ROM).
• This location (ROM) is convenient, because ROM needs no
initialization and is at a fixed location.
• Since ROM is read only, it cannot be infected by a computer virus.
• Problem: changing the bootstrap code requires changing the ROM
hardware chips.
• Boot disk: or system disk is the disk that has a boot partition.
Boot Block:
MBR: Master Boot Record
Booting from disk in Windows 2000
Image source: Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating system concepts with Java. Wiley Publishing, 2009.
Bad Blocks:
• No disk can be manufactured to 100% perfection, and all physical objects
wear out over time.
• All disks are shipped with a few bad blocks.
• Additional blocks can be expected to go bad slowly over time. If a large
number of blocks go bad then the entire disk will need to be replaced, but
a few here and there can be handled through other means.
• In the old days, bad blocks had to be checked for manually.
• Formatting of the disk or running certain disk-analysis tools would identify
bad blocks, and attempt to read the data off of them one last time through
repeated tries.
Bad Blocks:
• Afterwards the bad blocks would be mapped out and taken out of
future service. Sometimes the data could be recovered, and
sometimes it was lost forever. (Disk analysis tools could be either
destructive or non-destructive.)
• Modern disk controllers make much better use of the error-correcting
codes, so that bad blocks can be detected earlier and the data usually
recovered.
• Blocks are tested with every write as well as with every read, so often
errors can be detected before the write operation is complete, and
the data simply written to a different sector instead.
Bad Blocks:
• Re-mapping of sectors from their normal linear progression can throw off
the disk scheduling optimization of the OS, especially if the replacement
sector is physically far away from the sector it is replacing. For this reason
most disks normally keep a few spare sectors on each cylinder, as well as at
least one spare cylinder.
• Sector slipping may also be performed, in which all sectors between the
bad sector and the replacement sector are moved down by one, so that
the linear progression of sector numbers can be maintained.
• If the data on a bad block cannot be recovered, then a hard error has
occurred., which requires replacing the file(s) from backups, or rebuilding
them from scratch.
Bad Blocks: (recovery)
• More sophisticated disks, such as SCSI disks used in high end PCs and
workstations are smarter about bad-block recovery.
• The controller maintains a list of bad blocks on the disk.
• The list is initialized during the low-level formatting at the factory and
is updated over the life of the disk.
• Low level formatting also sets aside spare sectors not visible to the
operating system.
• The controller can be told to replace each bad sector logically with
one of the spare sectors and this scheme is known as Sector Sparing
or Forwarding.
A typical bad-sector transaction:
• The operating system tries to read logical block 87.
• The controller calculates the ECC and finds that the sector is bad. It reports
this finding to the operating system.
• The next time the system is rebooted, a special command is run to tell the
SCSI controller to replace the bas sector with a spare.
• After that, whenever the system requests logical block 87, the request is
translated into the replacement sector’s address by the controller.
*Most disks are formatted to provide a few spare sectors in each cylinder
and a spare cylinder as well. When a bad block is remapped, the controller
uses a spare sector from the same cylinder, if possible.
Swap-Space Management
Swapping:
• Moving entire processes between disk and main memory.
• Swapping occurs when the amount of physical memory reaches a
critically low point and processes (which are least active) are ,moved
from memory to swap-space to free available memory.
• In practice very few latest operating systems implement swapping in
this manner.
• These days, OS now combine swapping with virtual memory
techniques and swap pages, not necessarily entire processes.
• Some systems now use the terms ‘swapping’ and ‘paging’
interchangeably, reflecting the merging of these two concepts.
Swap-Space Management
• It is another low-level task of OS.
• Virtual memory uses disk space as an extension of main memory.
• Problem: Since disk access is much slower than memory access, using
swap space decreases the performance significantly.
• Goal for the design and implementation of swap space is to provide
the best throughput for the virtual memory system. ☺
Swap-Space Management
• Modern systems typically swap out pages as needed, rather than
swapping out entire processes. Hence the swapping system is part of
the virtual memory management system.
• Managing swap space is obviously an important task for modern
OSes.
Swap-Space use:
• The amount of swap space needed by an OS varies greatly according
to how it is used. Some systems require an amount equal to physical
RAM; some want a multiple of that; some want an amount equal to
the amount by which virtual memory exceeds physical RAM, and
some systems use little or none at all!
• Some systems support multiple swap spaces on separate disks in
order to speed up the virtual memory system.
Swap-Space Location
• Swap space can be physically located in one of two locations:
• As a large file which is part of the regular filesystem. This is easy to
implement, but inefficient. Not only must the swap space be accessed
through the directory system, the file is also subject to fragmentation issues.
Caching the block location helps in finding the physical blocks, but that is not
a complete fix.
• As a raw partition, possibly on a separate or little-used disk. This allows the
OS more control over swap space management, which is usually faster and
more efficient. Fragmentation of swap space is generally not a big issue, as
the space is re-initialized every time the system is rebooted. The downside of
keeping swap space on a raw partition is that it can only be grown by
repartitioning the hard drive.
Swap-Space Management: An Example
• Historically OSes swapped out entire processes as needed. Modern systems
swap out only individual pages, and only as needed. ( For example process
code blocks and other blocks that have not been changed since they were
originally loaded are normally just freed from the virtual memory system
rather than copying them to swap space, because it is faster to go find
them again in the filesystem and read them back in from there than to
write them out to swap space and then read them back. )
• In the mapping system shown in the next slide for Linux systems, a map of
swap space is kept in memory, where each entry corresponds to a 4K block
in the swap space. Zeros indicate free slots and non-zeros refer to how
many processes have a mapping to that particular block ( >1 for shared
pages only. )
Swap-Space Management: An Example
The data structures for swapping on Linux systems
Image source: Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating system concepts with Java. Wiley Publishing, 2009.
References:
• Silberschatz, Abraham, Peter B. Galvin, and Greg Gagne. Operating
system concepts with Java. Wiley Publishing, 2009.
• Dietel, Harvey M., and Barbara Deitel. An introduction to information
processing. Academic Press, 2014.