Implement Byte-based I/O over Block-based Interface

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-03-17

Problem Statement

Design and implement a byte-oriented read/write API on top of a lower-level block device API. The system should allow applications to read and write arbitrary byte ranges while internally interacting with the storage only in terms of fixed-size blocks.


Background


1.<span></span>Storage devices (disks, SSDs) typically expose block-based interfaces: data is read/written in fixed-size blocks (e.g., 4KB).
2.<span></span>Applications, however, require byte-based access: reading/writing arbitrary lengths at arbitrary offsets.
3.<span></span>Bridging these requires:
4.<span></span>Mapping byte offsets to block offsets
5.<span></span>Handling unaligned accesses (partial block updates)
6.<span></span>Efficient caching and synchronization under concurrent access


Functional Requirements


1.<span></span>Underlying Block API (given)
2.<span></span>read_block(block_id, buffer) → Reads one fixed-size block into buffer
3.<span></span>write_block(block_id, buffer) → Writes one fixed-size block from buffer
4.<span></span>Constant: BLOCK_SIZE (e.g., 4KB)
5.<span></span>Byte I/O API (to implement)
6.<span></span>read(offset, length, buffer) → Read length bytes starting from offset.
7.<span></span>write(offset, length, buffer) → Write length bytes starting from offset.
8.<span></span>Support requests that:
9.<span></span>fit within a block,
10.<span></span>span multiple blocks,
11.<span></span>are unaligned to block boundaries.
12.<span></span>Correctness
13.<span></span>Reads must return the latest written data.
14.<span></span>Writes must correctly update overlapping byte ranges (using read-modify-write for partial blocks).
15.<span></span>No torn or corrupted writes.
16.<span></span>Concurrency
17.<span></span>Multiple threads should be able to call read and write concurrently.
18.<span></span>Writes to disjoint blocks can proceed in parallel.
19.<span></span>Overlapping reads/writes must be synchronized to preserve correctness.


Constraints


1.<span></span>Block size fixed at 4KB.
2.<span></span>File/device size up to 10 GB.
3.<span></span>Support at least 100+ concurrent threads.
4.<span></span>Target performance close to raw block API when requests are large and aligned.


Example


1.<span></span>Write 100 bytes at offset 5000:
2.<span></span>Maps to Block-1 (offset 4096–8191).
3.<span></span>Perform read-modify-write of Block-1.
4.<span></span>Read 2000 bytes at offset 7000:
5.<span></span>Spans Block-1 (7000–8191) and Block-2 (8192–8999).
6.<span></span>Read both blocks, return correct slice.


Deliverables


1.<span></span>Class design (e.g., BlockDevice, ByteIOWrapper).
2.<span></span>Implementation of read and write with proper block mapping.
3.<span></span>Thread-safety guarantees (per-block locks or lock striping).
4.<span></span>Unit tests for aligned, unaligned, and concurrent reads/writes.
5.<span></span>Benchmark results comparing performance of aligned vs. unaligned access.


Solution

Comments