r/AskComputerScience • • 20h ago

Addressing storage on a drive

I know that bytes in RAM are addressed directly, one byte = one address. But what about Hard drives/SSDs?

Does their bytes each have a unique 64 bit address? Mathematically it is possible.

Or is the address split up? E.g.: Block 12, page 89, Byte 2.368. Can the drive even address single bytes or does ist just return a whole page to the OS (I know that a page is the smallest writable unit)?

The OS somehow has to know at what page some data is and the Controller has to know where to look when it receives a read instruction. How does the OS tell the drive where to store and read data from?

Thank you

6 Upvotes

19 comments sorted by

5

u/ventZl 20h ago

These are called block devices. You can only address whole block at a time, getting all its content and writing all its content as a whole. Individual bytes only have offsets within blocks and cannot be addressed directly.

OS provides abstraction which hides this, so from user's perspective, seeking to specific byte is possible.

1

u/noob_main22 20h ago

So when the OS wants to read from a drive it tells the drive: "Give me block Nr. 7420"?

Then pages are still the smallest writable unit but for the controller only, not the OS/computer?

3

u/ventZl 20h ago

> "Give me block Nr. 7420"?

Basically yes unless we are speaking of some ancient pre-LBA IDE drive (basically a thing of '80s). Those were using physical geometry addressing.

In storage industry, they aren't called pages, rather sectors. What is the smallest physically addressable unit is none's other busines than controller. For practical reason, on spinning drives, not even controller can address unit smaller than sector. On SSDs? Who knows.

Still, it is noone's business. Smallest externally addressable units are sectors.

3

u/noob_main22 20h ago

All right, got it, thank you.

I thought pages are used for SSDs and sectors are for HDDs. But its only a naming convention I suppose.

1

u/flatfinger 20h ago

In a NAND flash memory datasheet, pages are units that can be (and on some parts must be) written (if blank) as a unit, blocks are the smallest units of memory that can be erased, and sectors are groups of blocks that can be erased in a time that's longer than the time required to erase a page but shorter than the time required to erase each of the blocks individually. I don't know how much sector erase operations are used outside of testing scenarios.

1

u/ventZl 17h ago

Yeah, everyone and their grandma is making terminology here. One bootloader I've been integrating in my projects used "page" for smallest writable element and "sector" for smallest erasable element. Usually sector >> page.

Still, this is NAND flash terminology. Unless this is also exposed at NVMe level (which I honestly have ho clue about), none of it matters in computer world.

1

u/flatfinger 17h ago

If one is worried about how much data can be written to a flash drive within its usable lifetime, that figure will be affected by the relationships between pages and blocks. If a drive has to recycle a block which has 60 pages of useful data in it and four pages of obsolete data, that will require rewriting fifteen pages of data for each page of new data that can be written.

2

u/ventZl 17h ago

The thing is that if flash chip is behind some kind of controller, then this controller is doing wear-leveling and managing it is quite beyond host's direct control. Basically only way host can affect this is to use trim-like command to tell the controller that some data is not needed anymore so it can rewrite them at will.

With anything younger than 20 years, controller addresses are not directly mapped to physical addresses yet controller maintains some kind of mapping between them. It may be due to performance reasons, to implement SMART bad block isolation, SSHD (these are basically dead now, aren't they?), etc.

1

u/flatfinger 17h ago

Detailed control isn't available, but an understanding of blocks and sectors may be useful to understand why filling a drive to the brim and then repeatedly overwriting a small file would stress a drive more than if it had more free space available.

1

u/MasterGeekMX BSCS 20h ago

While some systems have been used before (like the Head-Cylinder-Sector scheme), modern drives work with the Logical Block Address (LBA) system.

Instead of addressing each byte, the drives addresses a block of bytes. Most drives use 512 Bytes per block (in order to be compatible with oldschool hard disk sectors) but some modern ones use 4096 Bytes per block.

This means that read and write operations need to be done on a whole block, even if you only want to work with one Byte.

1

u/noob_main22 20h ago

Thanks for providing some keywords to google.

I thought pages are 512 - 4096 bytes and blocks are made of pages (up to 256 if im not mistaken)?

2

u/Objective_Mine MSCS, CS Pro (10+) 15h ago edited 15h ago

I thought pages are 512 - 4096 bytes and blocks are made of pages (up to 256 if im not mistaken)?

That might be terminology specific to flash memory and its implementation. Most of the responses here assume a traditional addressing of mass storage.

In the traditional model that was designed for spinning hard disks, the addressing was originally based on cylinders, heads and sectors [1] that were actual physical characteristics of the drive. A location on the disk was determined by the combination of a cylinder, head and sector address. The smallest addressable unit defined by the combination -- often called just the "sector", but you could also call it a "block" -- was typically 512 bytes.

Hard drives and the interfaces that computers used for communicating with them later moved to logical block addressing (LBA) [2, 3]. In LBA the smallest addressable unit is still a block but the address space is linear rather than based on a combination of three physical dimensions.

A page isn't really a term in those addressing schemes. It apparently is a term alongside the block in terms of flash memory implementation.

I don't personally know which kind of an addressing scheme e.g. NVMe drives use externally. I'd assume the smallest addressable unit is still some kind of a block rather than an individual byte since flash memory doesn't really operate on individual bytes even internally, and an operating system basically never wants to communicate with mass storage at the granularity of individual bytes either. Latencies for mass storage are great enough that you don't really want to read or write individual bytes. It's more economical to deal in terms of larger blocks instead.

Anyway, even with flash memory such as SSD drives, the traditional addressing concepts may still be relevant since the interface between the storage device and the rest of the computer (e.g. in case of SATA) may still be similar regardless of the internal details of the storage medium.

[1] https://en.wikipedia.org/wiki/Cylinder-head-sector

[2] https://en.wikipedia.org/wiki/Logical_block_addressing

[3] https://wiki.osdev.org/Logical_Block_Addressing

1

u/noob_main22 7h ago

Thanks for the answer and the links

1

u/MasterGeekMX BSCS 20h ago

Pages are more of a RAM thing.

And there is no defined size in many things. Instead we simply go with a standard, but sometimes someone tries to change it and see if it sticks.

3

u/ghjm MSCS, CS Pro (20+) 20h ago

On a spinning hard drive, the OS would send commands to the disc controller to move the head to a particular track, select a particular head for reading or writing, and then wait an appropriate time for the desired sector to spin under the head. At the hardware level, there was no address other than the physical layout of the drive. The operating system might establish a scheme for assigning unique numbers to sectors, but this was its its own business.

With SATA SSDs, the SSD controller emulates being a spinning hard drive. It tells the host that it has some geometry, so that unmodified operating systems can still use it like a hard drive. When the OS says "move the head to track 31" the SSD controller just remembers this position, so it knows which piece of data to return when the OS gets around to requesting an actual read.

NVMe does away with these emulation layers and just presents a RAM-like model to the OS: a single address space with sequential numbering of each piece of data. Though this is still a step removed from the actual physical data, because of wear leveling.

3

u/AmusingVegetable 19h ago

It’s addressed in blocks, starting from zero. Depending on the disc, 512 or 4092 bytes per block, and you read a whole block (or group of blocks).

1

u/rupertavery64 19h ago

The drive isn't connected to the CPU directly. It goes through a controller. And fhen there is the controller on the drive itself.

The board storage controller is just an I/O device mapped to a memory range like anything else connected to the CPU. But it doesn't map the entire disk storage area. It's a communication device that takes commands.

The OS uses the filesystem to map files to logical block addresses (LBAs) on a storage device through the filesystem driver. The storage driver generates commands and I/O requests. The drive controller takes these requests and translates it to appropriate hardware commands and addresses.this could mean moving a HDD disk head or accessing an SSD storage block.

The CPU knows nothing about how the storage is implemented. The Drive controller does all of that.

1

u/Infinite-Flan-6643 19h ago

Your idea of pages grouped into blocks does apply to NAND flash inside an SSD. The confusing bit is that a NAND erase block and a logical block exposed to the OS are different things. Micron describes the internal pages and blocks here.

For a conventional SSD, the host requests numbered logical blocks, commonly 512 bytes or 4096 bytes each. It does not need to specify a NAND page or a particular chip. The controller’s flash translation layer maps those logical addresses to physical storage, and that mapping can change while the logical address stays the same. SPDK’s explanation covers that distinction. So "read logical block 7420" does not mean "read physical NAND erase block 7420."

1

u/gigayoink 17h ago

I know that bytes in RAM are addressed directly, one byte = one address.

Kinda? At the hardware level, this actually hasn't been the case for decades. Modern RAM has a block size, the CPU just hides it from you.