PKUOS - Pintos
Pintos source browser for PKU Operating System course
Data Structures | Functions | Variables
block.c File Reference
#include "devices/block.h"
#include <list.h>
#include <string.h>
#include <stdio.h>
#include "devices/ide.h"
#include "threads/malloc.h"
Include dependency graph for block.c:

Go to the source code of this file.

Data Structures

struct  block
 A block device. More...
 

Functions

static struct blocklist_elem_to_block (struct list_elem *list_elem)
 Returns the block device corresponding to LIST_ELEM, or a null pointer if LIST_ELEM is the list end of all_blocks. More...
 
const char * block_type_name (enum block_type type)
 Returns a human-readable name for the given block device TYPE. More...
 
struct blockblock_get_role (enum block_type role)
 Returns the block device fulfilling the given ROLE, or a null pointer if no block device has been assigned that role. More...
 
void block_set_role (enum block_type role, struct block *block)
 Assigns BLOCK the given ROLE. More...
 
struct blockblock_first (void)
 Returns the first block device in kernel probe order, or a null pointer if no block devices are registered. More...
 
struct blockblock_next (struct block *block)
 Returns the block device following BLOCK in kernel probe order, or a null pointer if BLOCK is the last block device. More...
 
struct blockblock_get_by_name (const char *name)
 Returns the block device with the given NAME, or a null pointer if no block device has that name. More...
 
static void check_sector (struct block *block, block_sector_t sector)
 Verifies that SECTOR is a valid offset within BLOCK. More...
 
void block_read (struct block *block, block_sector_t sector, void *buffer)
 Reads sector SECTOR from BLOCK into BUFFER, which must have room for BLOCK_SECTOR_SIZE bytes. More...
 
void block_write (struct block *block, block_sector_t sector, const void *buffer)
 Write sector SECTOR to BLOCK from BUFFER, which must contain BLOCK_SECTOR_SIZE bytes. More...
 
block_sector_t block_size (struct block *block)
 Returns the number of sectors in BLOCK. More...
 
const char * block_name (struct block *block)
 Returns BLOCK's name (e.g. More...
 
enum block_type block_type (struct block *block)
 Returns BLOCK's type. More...
 
void block_print_stats (void)
 Prints statistics for each block device used for a Pintos role. More...
 
struct blockblock_register (const char *name, enum block_type type, const char *extra_info, block_sector_t size, const struct block_operations *ops, void *aux)
 Registers a new block device with the given NAME. More...
 

Variables

static struct list all_blocks = LIST_INITIALIZER (all_blocks)
 List of all block devices. More...
 
static struct blockblock_by_role [BLOCK_ROLE_CNT]
 The block block assigned to each Pintos role. More...
 

Function Documentation

◆ block_first()

struct block * block_first ( void  )

Returns the first block device in kernel probe order, or a null pointer if no block devices are registered.

Definition at line 71 of file block.c.

References all_blocks, list_begin(), and list_elem_to_block().

Here is the call graph for this function:

◆ block_get_by_name()

struct block * block_get_by_name ( const char *  name)

Returns the block device with the given NAME, or a null pointer if no block device has that name.

Definition at line 87 of file block.c.

References all_blocks, list_begin(), list_end(), list_entry, list_next(), block::name, name, NULL, and strcmp().

Here is the call graph for this function:

◆ block_get_role()

struct block * block_get_role ( enum block_type  role)

Returns the block device fulfilling the given ROLE, or a null pointer if no block device has been assigned that role.

Finding block devices.

Definition at line 54 of file block.c.

References ASSERT, block_by_role, and BLOCK_ROLE_CNT.

Referenced by filesys_init(), fsutil_append(), and fsutil_extract().

Here is the caller graph for this function:

◆ block_name()

const char * block_name ( struct block block)

Returns BLOCK's name (e.g.

"hda").

Definition at line 151 of file block.c.

References block::name.

Referenced by check_sector(), found_partition(), partition_scan(), and read_partition_table().

Here is the caller graph for this function:

◆ block_next()

struct block * block_next ( struct block block)

Returns the block device following BLOCK in kernel probe order, or a null pointer if BLOCK is the last block device.

Definition at line 79 of file block.c.

References block::list_elem, list_elem_to_block(), and list_next().

Here is the call graph for this function:

◆ block_print_stats()

void block_print_stats ( void  )

Prints statistics for each block device used for a Pintos role.

Statistics.

Definition at line 165 of file block.c.

References block_by_role, BLOCK_ROLE_CNT, block_type_name(), block::name, NULL, printf(), block::read_cnt, block::type, and block::write_cnt.

Referenced by print_stats().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_read()

void block_read ( struct block block,
block_sector_t  sector,
void *  buffer 
)

Reads sector SECTOR from BLOCK into BUFFER, which must have room for BLOCK_SECTOR_SIZE bytes.

Internally synchronizes accesses to block devices, so external per-block device locking is unneeded.

Definition at line 121 of file block.c.

References block::aux, buffer, check_sector(), block::ops, block_operations::read, and block::read_cnt.

Referenced by fsutil_extract(), inode_open(), inode_read_at(), inode_write_at(), partition_read(), and read_partition_table().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_register()

struct block * block_register ( const char *  name,
enum block_type  type,
const char *  extra_info,
block_sector_t  size,
const struct block_operations ops,
void *  aux 
)

Registers a new block device with the given NAME.

devices/block.h

If EXTRA_INFO is non-null, it is printed as part of a user message. The block device's SIZE in sectors and its TYPE must be provided, as well as the it operation functions OPS, which will be passed AUX in each function call.

Definition at line 187 of file block.c.

References all_blocks, block::aux, BLOCK_SECTOR_SIZE, block::list_elem, list_push_back(), malloc(), block::name, name, NULL, block::ops, PANIC, PRDSNu, print_human_readable_size(), printf(), block::read_cnt, block::size, strlcpy(), block::type, and block::write_cnt.

Referenced by found_partition(), and identify_ata_device().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_set_role()

void block_set_role ( enum block_type  role,
struct block block 
)

Assigns BLOCK the given ROLE.

Definition at line 62 of file block.c.

References ASSERT, block_by_role, and BLOCK_ROLE_CNT.

◆ block_size()

block_sector_t block_size ( struct block block)

Returns the number of sectors in BLOCK.

Block device operations.

Definition at line 144 of file block.c.

References block::size.

Referenced by bitmap_create_in_buf(), check_file_handle(), found_partition(), free_map_init(), fsutil_append(), read_partition_table(), seq_test(), and write_some_bytes().

Here is the caller graph for this function:

◆ block_type()

enum block_type block_type ( struct block block)

Returns BLOCK's type.

Definition at line 158 of file block.c.

References block::type.

◆ block_type_name()

const char * block_type_name ( enum block_type  type)

Returns a human-readable name for the given block device TYPE.

Definition at line 35 of file block.c.

References ASSERT, BLOCK_CNT, and block::type.

Referenced by block_print_stats().

Here is the caller graph for this function:

◆ block_write()

void block_write ( struct block block,
block_sector_t  sector,
const void *  buffer 
)

Write sector SECTOR to BLOCK from BUFFER, which must contain BLOCK_SECTOR_SIZE bytes.

Returns after the block device has acknowledged receiving the data. Internally synchronizes accesses to block devices, so external per-block device locking is unneeded.

Definition at line 134 of file block.c.

References ASSERT, block::aux, BLOCK_FOREIGN, buffer, check_sector(), block::ops, block::type, block_operations::write, and block::write_cnt.

Referenced by fsutil_append(), fsutil_extract(), inode_create(), inode_write_at(), and partition_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_sector()

static void check_sector ( struct block block,
block_sector_t  sector 
)
static

Verifies that SECTOR is a valid offset within BLOCK.

Panics if not.

Definition at line 105 of file block.c.

References block_name(), PANIC, PRDSNu, and block::size.

Referenced by block_read(), and block_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ list_elem_to_block()

static struct block * list_elem_to_block ( struct list_elem list_elem)
static

Returns the block device corresponding to LIST_ELEM, or a null pointer if LIST_ELEM is the list end of all_blocks.

Definition at line 217 of file block.c.

References all_blocks, list_end(), list_entry, and NULL.

Referenced by block_first(), and block_next().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ all_blocks

struct list all_blocks = LIST_INITIALIZER (all_blocks)
static

List of all block devices.

Definition at line 25 of file block.c.

Referenced by block_first(), block_get_by_name(), block_register(), and list_elem_to_block().

◆ block_by_role

struct block* block_by_role[BLOCK_ROLE_CNT]
static

The block block assigned to each Pintos role.

Definition at line 28 of file block.c.

Referenced by block_get_role(), block_print_stats(), and block_set_role().