Back to series
· 17 min read

Hard Drives and Storage Devices: How Is Data Stored?


1. Why do computers need storage devices?

When a computer is running, the CPU needs to continuously read instructions and data. But memory (RAM) has an important characteristic: data is lost when power is cut.

For example:

  • When you open a program, its code is loaded into memory.
  • When you edit a document, the changes may be temporarily kept in memory.
  • After the computer is shut down, data in memory usually cannot be retained.
  • If you want to be able to find this document the next time the computer starts, the data must be written to a non-volatile storage device.

The core tasks of storage devices are:

  1. Store data over the long term.
  2. Read data when needed.
  3. Support modifying, deleting, and organizing data.
  4. Retain data after power loss and restart.

1.1 Differences between memory and storage devices

Comparison dimension Memory (RAM) Storage device
Data persistence Usually lost when power is cut Retained after power loss
Primary use Temporary runtime data Long-term data storage
Access speed Usually faster Relatively slower
Capacity Usually smaller Usually larger
Cost Higher cost per unit capacity Lower cost per unit capacity
Typical devices DDR4, DDR5 HDD, SSD, USB flash drive

The two can be understood as:

  • Memory: the desktop the computer is currently working on.
  • Hard disk: a filing cabinet for long-term data storage.

The CPU usually does not use a hard disk directly as ordinary RAM. The operating system must handle data exchange through storage drivers, file systems, and memory management mechanisms.

1.2 Computer storage hierarchy

Computers typically use a multi-level storage structure, with different levels making trade-offs among speed, capacity, and cost.

Storage level Typical device Main characteristics
CPU registers Register Extremely fast, very small capacity
CPU cache L1/L2/L3 Cache Caches recently used data
Main memory RAM Workspace for running programs
High-speed persistent storage NVMe SSD Low latency and good random-access performance
Standard persistent storage SATA SSD, HDD Used for system, applications, and file storage
Archival storage Tape, cold storage Low cost, suitable for long-term preservation

Storage hierarchy:

flowchart TD
    A["CPU registers"] --> B["CPU Cache"]
    B --> C["Main memory (RAM)"]
    C --> D["High-speed SSD"]
    D --> E["Standard SSD / HDD"]
    E --> F["Tape / archival storage"]

    style A fill:#FCE8C8,stroke:#D9A441,color:#765018
    style B fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style D fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style E fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style F fill:#F3E8E2,stroke:#C9A99A,color:#6E4D40

The closer to the CPU, the faster it usually is, but the smaller the capacity and the higher the cost per unit capacity.


2. Basic classification of hard disks and storage devices

Based on the physical principles of data storage, common storage devices are mainly divided into magnetic storage, flash storage, and optical storage.

2.1 Mechanical hard disk HDD

HDD (Hard Disk Drive) is a traditional mechanical hard disk that uses magnetic material to record data.

The inside of a mechanical hard disk mainly includes:

Component Function
Platter Stores magnetic data
Spindle motor Rotates the platters
Read/write head Reads and writes magnetic information
Actuator arm Moves the head
Actuator Controls head positioning
Control circuit board Manages read/write, caching, and interface communication
Cache Temporarily stores read/write data and control information

Working principle of a mechanical hard disk:

  1. The spindle motor drives the platters to spin at high speed.
  2. The actuator arm moves the head to the target position.
  3. The head is positioned over the target track.
  4. The platter rotates so that the target sector passes under the head.
  5. The head reads or changes the magnetic state.
  6. The controller transmits the data to the computer.

2.2 Principles of Magnetic Recording

Hard disk drives use magnetic material to store binary information.

The surface of a disk contains a large number of tiny regions that can be magnetized; data is encoded by controlling the magnetic states and the changes between them.

From a logical perspective:

  • Data consists of binary bits.
  • The magnetic medium represents data through physical magnetic states.
  • The head reads data by detecting changes in the magnetic field.
  • When writing, a magnetic field changes the magnetization state of the medium.

Note: modern hard drives do not simply equate one fixed direction with 0 and another with 1. Actual magnetic recording uses complex encoding and signal processing mechanisms to ensure that data can be read reliably.

2.3 The Spatial Structure of a Hard Disk Drive

The physical structure of a traditional hard disk drive can be understood through the following concepts:

Concept Meaning
Platter Circular magnetic recording medium
Track Concentric circular recording area on the platter surface
Sector Data storage unit divided out of a track
Cylinder The set of tracks at the same radius on different platter surfaces
Head The component that reads from and writes to the corresponding platter surface

Track: The platter surface consists of many concentric circles, and each circular region is called a track.

Sector: A track is further divided into multiple sectors, and the sector is an important unit for physical data management on a hard drive.

Cylinder: The tracks at the same radius on multiple platter surfaces are combined and called a cylinder.

Head: Each platter surface usually has a corresponding read/write head, which is responsible for sensing or changing the magnetic state on that surface.

Traditional hard drives once widely used CHS (Cylinder, Head, Sector) addressing. Modern hard drives mainly provide the operating system with a logical block addressing interface through LBA (Logical Block Addressing), hiding the complex physical structure.

2.4 Solid State Drives (SSD)

An SSD (Solid State Drive) is a solid-state drive that mainly uses NAND Flash memory to store data.

Unlike hard disk drives, SSDs have no platters spinning at high speed and no heads that need to move.

The main components include:

Component Function
NAND Flash Actually stores the data
Controller chip Manages reads and writes, mapping, error correction, and scheduling
DRAM Cache Caches address mapping and data
SLC Cache Provides a high-speed write buffer
Firmware Executes internal management algorithms
Interface Exchanges data with the host

Core characteristics of SSDs:

  • No mechanical moving parts.
  • Random read performance is usually significantly better than that of HDDs.
  • Access latency is lower.
  • Shock resistance is usually better.
  • NAND Flash has limitations in terms of program/erase cycles and data retention.
  • Write speed may be affected by cache, temperature, and garbage collection.

2.5 HDD vs. SSD Comparison

Comparison Dimension HDD SSD
Storage principle Magnetic recording NAND Flash
Physical structure Platters, heads, motor Flash chips, controller
Random read Slower Faster
Sequential read Depends on rotational speed and data location Usually faster
Random write Affected by seek and rotational latency Affected by flash and controller mechanisms
Noise Has mechanical noise Usually no mechanical noise
Shock resistance Relatively weak Usually strong
Program/erase limits Does not use the NAND program/erase mechanism Has a program/erase lifespan limit
Cost per unit capacity Usually lower Usually higher

2.6 Other Common Storage Devices

USB flash drive

Uses NAND Flash to store data and connects to a computer via a USB interface, making it suitable for portable file exchange.

SD card

Commonly found in cameras, phones, and embedded devices, and also uses flash memory technology.

Optical disc

Reads or changes information on an optical medium using a laser; common types include CD, DVD, and Blu-ray.

Magnetic tape

Data is stored on magnetic media, commonly used for enterprise backups, long-term archiving, and large-scale cold data storage.


3. How Does an SSD Store Data?

To understand SSDs, it is important to grasp the internal structure of NAND Flash.

3.1 Basic Principles of NAND Flash

NAND Flash is a type of non-volatile flash memory; even when power is lost, it can retain stored data under certain conditions.

It stores information through the charge state in special transistors.

Traditional floating-gate NAND Flash uses a Floating Gate to store charge; modern 3D NAND also widely uses a Charge Trap structure.

The basic idea is:

  1. Write charge into the memory cell.
  2. The charge changes the transistor's threshold voltage.
  3. During reading, detect the threshold voltage.
  4. Determine the stored data based on the threshold voltage range.

Therefore, an SSD does not rely on continuous power to maintain data like RAM does; instead, it preserves information through the charge state inside the memory cells.

3.2 NAND Flash Memory Cell Types

A NAND memory cell can represent different numbers of bits.

Type Bits per Cell Number of Voltage States Key Characteristics
SLC 1 bit 2 Generally better performance and endurance
MLC 2 bit 4 Compromise between capacity and performance
TLC 3 bit 8 Common in consumer SSDs
QLC 4 bit 16 High capacity density, relatively lower endurance

For example:

  • SLC: one memory cell represents 0 or 1.
  • MLC: one memory cell represents 4 states.
  • TLC: one memory cell represents 8 states.
  • QLC: one memory cell represents 16 states.

The more bits stored, the more voltage states must be distinguished, and the higher the requirements for voltage control and read accuracy.

3.3 Physical Organization Structure of NAND Flash

NAND Flash is not a continuous memory space that can be overwritten arbitrarily.

It has a specific hierarchy:

Level Meaning
Die An independent group of memory cells inside a flash chip
Plane Storage area within a Die
Block Basic unit of erase operations
Page Basic unit of read and program operations
Cell The memory cell that actually stores bit information

Typical relationship:

flowchart TD
    A["NAND Flash Die"] --> B["Plane"]
    B --> C["Block"]
    C --> D["Page"]
    D --> E["Memory Cell"]
    E --> F["Stores 1 / 2 / 3 / 4 bit"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style F fill:#F6FAF7,stroke:#DCE9E0,color:#334155

There are three concepts to remember in particular:

  • Page: usually the basic unit for reading and programming.
  • Block: usually the basic unit for erasure.
  • Cell: the physical memory cell that actually stores data.

Different NAND products may have different Page and Block sizes and organization methods.

3.4 Why Can't an SSD Be Overwritten Directly Like Memory?

An important limitation of NAND Flash is:

It can be programmed by page, but usually must be erased by block.

Suppose a Block contains multiple Pages:

  • Data can be written to a blank Page.
  • A Page that has already been written cannot be overwritten arbitrarily like ordinary memory.
  • To reuse an area that has already been written, the entire Block usually must be erased first.

This introduces a problem:

If the user modifies only a very small data block, the SSD may need to migrate the relevant valid data to another location and then erase the original Block.

This is one reason why garbage collection is needed inside an SSD.


4. Core Technology Inside an SSD: FTL

An SSD does not directly expose the logical addresses submitted by the operating system as physical addresses in NAND Flash.

Instead, it uses the FTL (Flash Translation Layer) to manage the mapping between logical addresses and physical storage locations.

4.1 What Is the FTL?

What the operating system sees are logical block addresses (LBAs).

For example:

  • LBA 0
  • LBA 1
  • LBA 2
  • LBA 3

But the actual physical locations inside the NAND Flash might be:

  • Die 0 / Plane 0 / Block 10 / Page 3
  • Die 0 / Plane 1 / Block 5 / Page 8
  • Die 1 / Plane 0 / Block 20 / Page 1

The FTL is responsible for maintaining this mapping.

4.2 Main Responsibilities of the FTL

Function Purpose
Address mapping Maps logical addresses to physical flash locations
Garbage collection Reclaims blocks that contain invalid data
Wear leveling Distributes program/erase cycles as evenly as possible
Bad block management Avoids continued use of unreliable storage areas
Error correction Detects and corrects data errors within a certain range
Over-provisioning management Manages physical capacity that users cannot access directly
Write scheduling Coordinates multiple flash channels and storage units

4.3 Logical Address Mapping

Suppose the operating system wants to write data to logical address LBA 100.

The FTL might map it to:

LBA 100
   |
   v
Physical Address
   |
   v
Die 0 / Plane 1 / Block 20 / Page 5

When the user modifies LBA 100 again, the SSD usually does not overwrite the original page in place.

A typical approach is:

  1. Find a new free page.
  2. Write the modified data to the new location.
  3. Update the logical address mapping.
  4. Mark the old page as invalid.
  5. Wait for garbage collection to erase the entire block it belongs to.

This is an out-of-place update mechanism.

4.4 SSD Write Flow

flowchart TD
    A["Operating system submits a write request"] --> B["SSD controller receives the request"]
    B --> C["FTL looks up the logical address mapping"]
    C --> D["Find an available physical page"]
    D --> E["Program data into NAND Flash"]
    E --> F["Update the address mapping table"]
    F --> G["Confirm the write is complete"]
    G --> H["Mark old data as invalid"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155
    style H fill:#E8EDF2,stroke:#B6C2CF,color:#334155

This is only the typical logical flow. A real SSD may also involve write caching, parallel channels, error-correcting codes, metadata persistence, and power-loss protection.

4.5 Garbage Collection (GC)

Because NAND Flash cannot freely overwrite data that has already been programmed, the SSD must periodically clean up invalid data.

For example, a block contains 4 pages:

Page Current State
Page 0 Valid data A
Page 1 Invalid data
Page 2 Valid data B
Page 3 Invalid data

To erase this block, valid data A and B must first be migrated to other free locations.

Only then can the entire block be erased to free up space.

Garbage collection flow:

flowchart TD
    A["Identify a Block containing invalid Pages"] --> B["Read the valid Pages in it"]
    B --> C["Migrate the valid data to a new location"]
    C --> D["Update the FTL address mapping"]
    D --> E["Erase the original Block"]
    E --> F["The Block re-enters the free pool"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style C fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style F fill:#F6FAF7,stroke:#DCE9E0,color:#334155

If garbage collection occurs while the user is writing data, it may affect SSD write latency.

This is also why some SSDs experience a noticeable drop in speed after continuously writing large amounts of data.

4.6 Wear Leveling

NAND Flash memory cells have a limited erase/write lifespan.

If the same set of Blocks is used repeatedly, they may reach their lifespan limit earlier than other Blocks.

The goal of wear leveling is to keep the erase/write counts of different Blocks as even as possible.

It is mainly divided into:

  • Dynamic wear leveling: newly written data is preferentially allocated to available Blocks with fewer erase/write cycles.
  • Static wear leveling: when necessary, migrate data that remains unchanged for a long time to free up low-wear Blocks for other data.

Wear leveling can extend the service life of the entire SSD.

4.7 TRIM Command

When a user deletes a file, the operating system usually only marks the relevant space in the file system as reusable.

The SSD may not immediately know which logical addresses are no longer needed.

The role of TRIM is to notify the SSD of these logical addresses that are no longer in use.

Its basic flow is as follows:

flowchart TD
    A["User deletes a file"] --> B["File system frees the corresponding space"]
    B --> C["Operating system sends TRIM / Deallocate"]
    C --> D["SSD updates logical address status"]
    D --> E["Related Pages can be considered invalid"]
    E --> F["Subsequent garbage collection frees the Block"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style F fill:#F6FAF7,stroke:#DCE9E0,color:#334155

TRIM does not mean data is erased immediately, nor is it a guarantee of secure deletion. It mainly helps the SSD manage logical space that is no longer in use more accurately.


5. How Does a Hard Disk Drive Read and Write Data?

SSDs rely on flash memory and a controller to read and write data, while HDDs rely on mechanical movement and magnetic recording.

5.1 HDD Read Process

Suppose the operating system needs to read a file from the hard disk.

The hard disk controller converts the logical address into an internal physical location and reads the corresponding data through the read/write head.

The read process mainly consists of three stages:

Stage 1: Seek Time

The actuator arm moves the read/write head so that it reaches the target track.

Stage 2: Rotational Latency

Wait for the platter to rotate until the target sector arrives beneath the head.

Stage 3: Transfer Time

The head reads the data and transfers it to the host through the controller.

5.2 HDD Access Time

The access latency of a mechanical hard drive can be approximated as:

T_{access}=T_{seek}+T_{rotation}+T_{transfer}

Where:

  • T_{seek}: seek time.
  • T_{rotation}: rotational latency.
  • T_{transfer}: transfer time.

For average rotational latency:

T_{rotation}=\frac{60}{2R}

Where:

  • R: rotational speed in RPM.
  • The result is in seconds.

For example, for a 7200 RPM hard drive:

T_{rotation}=\frac{60}{2\times7200}
T_{rotation}\approx0.00417s

That is approximately 4.17 ms.

This is only the average rotational latency and does not include seek time or transfer time.

5.3 Why Is Random Read Slow on HDDs?

Suppose a large contiguous file needs to be read:

  • The magnetic head only needs to seek once or a few times.
  • The platter keeps rotating.
  • Data can be transferred continuously.

If many scattered small files need to be read:

  • The magnetic head needs to move frequently.
  • Each time, it may have to wait for a different sector to rotate into position.
  • Mechanical seeking introduces additional latency.

Therefore, HDD random read performance is usually significantly lower than sequential read performance.

5.4 HDD Write Process

flowchart TD
    A["Operating system submits a write request"] --> B["Hard drive controller receives the request"]
    B --> C["Locate target track"]
    C --> D["Wait for target sector to rotate into position"]
    D --> E["Magnetic head changes magnetization state of the medium"]
    E --> F["Complete data encoding and writing"]
    F --> G["Controller returns write status"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Actual mechanical hard drives also use caching, write scheduling, error correction, and internal data management mechanisms.


6. Hard Drive Interfaces and Protocols: SATA, NVMe, PCIe

Storage devices differ not only in their physical media, but also in their connection interfaces and communication protocols.

6.1 SATA Interface

SATA (Serial ATA) is a common interface standard for traditional hard drives and SSDs.

Characteristics:

  • Commonly used for mechanical hard drives and 2.5-inch SATA SSDs.
  • Uses the SATA communication protocol.
  • SATA III has a theoretical interface bandwidth of 6 Gbit/s.
  • Actual effective data throughput is affected by protocol overhead and device performance.

6.2 PCIe Interface

PCIe (Peripheral Component Interconnect Express) is a high-speed serial expansion bus.

Many high-speed SSDs connect to the CPU or chipset via PCIe.

PCIe bandwidth is affected by the following factors:

  • PCIe generation.
  • Number of lanes.
  • Encoding and protocol overhead.
  • The actual connection topology of the motherboard and CPU.

6.3 NVMe Protocol

NVMe (Non-Volatile Memory Express) is a communication protocol designed specifically for non-volatile storage devices.

It typically runs over PCIe.

Main characteristics of NVMe:

  • Supports multiple submission queues and completion queues.
  • Supports a large number of parallel commands.
  • Reduces some overhead of traditional storage protocols.
  • Better suited to the low latency and parallel access characteristics of SSDs.

6.4 Differences Between SATA and NVMe

Comparison Dimension SATA SSD NVMe SSD
Common Interface SATA PCIe
Communication Protocol AHCI NVMe
Typical Form Factor 2.5-inch M.2, U.2, PCIe card, etc.
Command Queue Traditional queue mechanism Multi-queue architecture
Parallel Processing Capability Relatively limited Stronger
Performance Ceiling Limited by SATA bandwidth Limited by PCIe generation and lanes

Note: M.2 is a physical form factor and is not equivalent to NVMe.

M.2 devices may use either the SATA protocol or the PCIe/NVMe protocol. When purchasing, confirm that the motherboard slot and device protocol are compatible.

6.5 NVMe Queue Mechanism

NVMe manages commands through Submission Queues and Completion Queues.

Basic process:

  1. The host places read/write commands into the submission queue.
  2. The SSD controller reads the commands.
  3. The SSD performs the data operations.
  4. The SSD writes the execution results to the completion queue.
  5. The host retrieves the completion status.
flowchart TD
    A["Application submits I/O"] --> B["Operating system storage stack"]
    B --> C["NVMe Submission Queue"]
    C --> D["SSD controller"]
    D --> E["NAND Flash performs reads and writes"]
    E --> F["NVMe Completion Queue"]
    F --> G["Host processes completion status"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Multiple queues allow multiple CPU cores to submit I/O requests concurrently, reducing queue contention in high-concurrency scenarios.


7. File System: How Is Data Organized After It Is Written to Disk?

A hard disk only provides raw storage space; it cannot directly understand folders, file names, or file types.

For example, the disk does not know:

  • What report.docx is.
  • That photos is a folder.
  • Which user a given file belongs to.
  • Which data blocks belong to the same file.

This information is managed by the file system.

7.1 What Is a File System?

A file system is the mechanism the operating system uses to organize, manage, and access stored data.

It is mainly responsible for:

  1. Managing files and directories.
  2. Allocating storage space.
  3. Recording file metadata.
  4. Managing file permissions.
  5. Maintaining the relationship between data blocks and files.
  6. Supporting file creation, reading, modification, and deletion.

Common file systems:

File System Common Applications
FAT32 USB flash drives, compatibility scenarios
exFAT Large-capacity removable storage
NTFS Windows systems
ext4 Linux systems
XFS Linux servers
APFS Apple devices
ZFS Advanced storage management and data integrity scenarios

7.2 How Does a File System Organize Data?

Take a single file as an example:

report.txt

The file system needs to store:

  • The file name.
  • The file size.
  • The creation and modification times.
  • File permissions.
  • The storage locations corresponding to the file's contents.
  • The directory the file belongs to.

A file is usually not stored directly under its complete file name in one contiguous physical space.

The file system uses metadata structures to record the locations of a file's data blocks.

7.3 The Logical Structure of a File System

flowchart TD
    A["Storage device"] --> B["Partition"]
    B --> C["File system"]
    C --> D["Directory structure"]
    D --> E["File metadata"]
    E --> F["Data block mapping"]
    F --> G["Data actually stored"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#F6FAF7,stroke:#DCE9E0,color:#334155

7.4 File Metadata

File metadata is information that describes a file, not the content of the file itself.

For example:

<||DSML|| calls> <||DSML|| invoke name="bash"> <||DSML|| parameter name="command" string="true">ls -la 2>/dev/null && echo "---" && find . -maxdepth 3 -type f \( -name "*.md" -o -name "*.txt" -o -name "*.rst" -o -name "*.adoc" \) -not -path "./.git/*" 2>/dev/null | head -50 <||DSML|| parameter name="description" string="true">List files in working directory
flowchart TD
    A["Connect Storage Device"] --> B["Operating System Detects Device"]
    B --> C["Check Disk Status"]
    C --> D["Create Partition Table"]
    D --> E["Create Partition"]
    E --> F["Format File System"]
    F --> G["Mount or Assign Drive Letter"]
    G --> H["Start Saving Files"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#E8EDF2,stroke:#B6C2CF,color:#334155
    style H fill:#F6FAF7,stroke:#DCE9E0,color:#334155

Note: Initialization, partitioning, and formatting may destroy existing data. For disks that already contain important files, do not perform these operations before confirming the data status.


IX. What Happens Under the Hood During a File Save Operation?

Understanding the complete storage process is an important part of mastering operating systems and computer organization principles.

Suppose a user saves a file using an editor:

/data/report.txt

The user only sees that the save button was clicked, but the operating system and hardware actually perform multiple steps.

9.1 From Application to Storage Device

The typical process is as follows:

  1. The application generates the data to be saved.
  2. The application calls file system-related APIs.
  3. The operating system checks the file descriptor, permissions, and path.
  4. The file system maps the file offset to logical data blocks.
  5. The block device layer hands the request to the storage driver.
  6. The storage driver submits read/write requests according to the device protocol.
  7. The SSD or HDD controller executes the actual operation.
  8. The storage device completes the data write.
  9. The operating system updates the relevant state and notifies the application.
flowchart TD
    A["Application"] --> B["File API"]
    B --> C["Operating System Kernel"]
    C --> D["File System"]
    D --> E["Block Device Layer"]
    E --> F["Storage Driver"]
    F --> G["SATA / NVMe"]
    G --> H["Storage Device Controller"]
    H --> I["NAND Flash / Disk Platters"]
    I --> J["Data Persistence"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style C fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style D fill:#FCE8C8,stroke:#D9A441,color:#765018
    style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style F fill:#DCEFE5,stroke:#8BBFA2,color:#315E45
    style G fill:#EDE4FA,stroke:#B5A0D8,color:#51416F
    style H fill:#FCE8C8,stroke:#D9A441,color:#765018
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#F6FAF7,stroke:#DCE9E0,color:#334155

9.2 File APIs and System Calls

Applications typically do not directly operate on the physical sectors of a hard drive; instead, they access files through interfaces provided by the operating system.

For example, on Linux:

#include <stdio.h>

int main() {
    FILE *fp = fopen("report.txt", "w");

    if (fp == NULL) {
        perror("fopen failed");
        return 1;
    }

    fprintf(fp, "Hello Storage!\n");

    fclose(fp);

    return 0;
}

This code accomplishes:

  • Open or create a file.
  • Write a string.
  • Close the file.

However, note that a successful return from fclose() does not necessarily mean the data has been safely written to the physical medium.

The data may still go through stages such as the operating system page cache and device cache.

9.3 Page Cache

Operating systems typically use Page Cache to cache file data.

When an application writes to a file, the data may first enter the page cache in memory, and then the kernel writes it back to the storage device at an appropriate time.

Doing so can:

  • Reduce the number of repeated disk accesses.
  • Improve file read performance.
  • Merge multiple small write requests.
  • Reduce I/O pressure on storage devices.

Typical write process: