All right guys.
So let's go through a simple read.
That.
From the application and goes deep, deep, deep, deep, deep through.
The colonel then goes to the desk.
How what exactly happens and what kind of translations are we doing through that?
But before we start, we need to talk about this thing.
It's called Posix.
Well.
Proposed Posix is the standard Unix based.
Yeah, which is different than windows.
Windows uses Win32 APIs.
It's very different than Posix, which is what Unix and the Linux descendants essentially use.
And basically these are APIs for I o.
You know, how do I talk to your storage, to your sockets?
You know, windows tend to have specific APIs for everything.
Like they have one sockets for, for specific one, you know, socket management, you know.
But but in Linux they use the same syscalls like read write.
And they added like except for.
For specific for socket programming, but these are the APIs that you can use system calls to do things.
Yeah.
To ask the kernel to do things for you.
Read write.
And we talked about if sync and if data sync.
Right.
Uh, read read from this file or as we're going to see in the socket management section.
Read from connection because a connection is also a file.
Hey read from the received buffer.
This abstraction creates certain, you know, unease in certain people.
They don't like this because like because hey, yeah, you're just reading.
I don't care what you're reading from.
Some people like it.
Hey, hide the complexity.
Some people know and show me the complexity because reading from a connection is different than reading
from a file.
Anyway, it's opinion based, but regardless, this is what we use to read from files.
Read and write by read.
I specify the file descriptor, which is a number that points to specifically to the inode, where the
file lives and the file descriptor is lives in the process, as we know, and we can tell what file
is opened based on that, the buffer and the size, how much you want to read.
And of course, this buffer is a malloc in userspace.
And when you call a read uh the kernel will call will do the read, put it in the its buffer which is
the page cache, and then do another copy all the way to user space where you have specified that.
So there is two copies going on here.
Right is the opposite.
You right?
Specify the content right and how much to write.
And we write it to the page cache and then eventually to the disk when the when the operating system
feels right.
And if sync forces those page caches to be flushed to disk.
So let's go through an example.
Right.
So I want to read let's say I want to read an entire file test dot dot dot.
No.
And this has like 5000 bytes.
This is my file 5000 bytes.
So let's go through certain important metadata information so that we can understand.
And I want to make it the simplest.
For.
To make this easy to understand, the logical sector size is 4096.
So for kilobyte the physical size, fixed sector size is also 4096.
So the PBA and the LBA are identical.
Makes our life easy.
The page size, of course, doesn't change.
4096 well, almost does the virtual memory page size.
The file system block is also formatted for the particular partition to be 4096.
So we're we're clean.
This is the best.
Configuration.
You can give me one block gets you one beautiful physical sector side and mapping is easier.
So you have less entries in the OS, right.
You have 1 to 1 mapping one block equal one LBA one sector.
So technically to read 5000 bytes you need to read one full block plus an additional 1000 block.
Well ish right.
Actually less than 1000 bytes.
It's 96 bytes.
Uh, well, less than that.
So because you read 4096 and then the remaining, which is slightly less than a thousand bytes, I should
have added a tilde here.
So.
All right.
For simplicity, I say that a thousand bytes.
Right.
But that's another block.
You still need another block.
With the remaining bytes you don't get to choose a thousand exact bytes.
You have to read a whole block.
So you get we're reading two blocks right.
So two physical sector sizes.
So how do we read.
Well first we identify where files.
Well, the file is still stored that we look it up.
From the process.
Then find the file descriptor and then look up the inode and from the inode we.
The file system uses its own technology to find the metadata.
And that's where the bread and butter for file systems are.
Then we find once you find the file.
And this should be quick because you know file descriptor that should be like a poof right.
You're not looking it up as it should be really fast.
Because if you know the file and then we know the start block right.
It's block.
We're starting block six.
In this particular case we know.
All right six.
What's the next one.
Three.
All right let's I'm going to read the entire thing.
Right.
So three.
All right let's go three.
And then what's after three.
Nothing.
So we really need exactly two blocks as we have described.
Right.
So this is where the 4096 and this is the remaining one, uh, the 10,000 whatever 1000.
So.
We followed.
Okay, so we still need to convert the six and three.
Two.
Elba's right because that's not enough.
Blocks.
Blocks are something, but those need to be converted to lbma's.
Hmm.
So the first thing we need to do.
What?
Check the base cache.
Query the OS file system.
Page cache.
All right.
It's by block.
Right.
In this particular case.
So we query the page and.
All right I'm I need I need page.
So in our case page block three happened to be cached.
And there is a nice pointer to virtual memory Z.
That's an address of where the frame exists.
So block three is nice.
It's ready.
We can read.
We can copy right now.
But hold on we need six right.
So in six to get six we need to issue a command for the disk controller to fetch six.
But controller doesn't know what six is right.
It needs the actual LBA and it happens to be LBA six.
It doesn't have to be of course.
Right.
You do the math that we talked about.
So block size is happened to be LBA six in this particular case.
In this case we're matching.
But if it's like all right so block six happened to be six because one block is one LBA and LBA.
So go ahead and read LBA six for one length.
The disk controller converts the LBA to VPA and returns the data to the OS.
Very simple things, right?
Talked about all of that and then we put that entry.
We create a new entry in the page cache and we say hey this is block six and here's where it lives in
page cache.
So we cache it.
Those are the OS now needs to copy Z and J, which is 8000, right?
To the user space, but.
User really has only for a thousand.
So it's going to copy this whole Z.
Right.
The point.
Remember Z is an address right.
So it's going to be copy the whole for 4K.
But then for J is only going to pick the first thousand or whatever say 900 bytes.
And then copies only vote in the buffer for the user.
So there is that's the second copy we talked about.
So here we got really a nice one copy to fill this guy with.
Already had this.
And then we return to the user.
So I thought I'll talk about this a little bit.
You see there are so many layers we're going through.
There is translation from the Posix read to a file system, blocks to pages to lbas to PBA's.
So this translation happening all over the translation in the SSD is actually a little bit more expensive
than it's it is for, uh, hard drives because, um, an SSDs we they are moving things around.
It's not like you can use a simple mathematical equation.
Right.
Because the logical blocks move.
Locations, and as a result, you cannot use a simple math to always get you where based on the logical
block, you know where two is, right?
Uh, an SSD is at least not shingled.
Shingled magnetic also uses a mapping, but.
But in a normal hard drives.
If I give you a block thousand, LBA 1000, it's always going to fit into certain physical location.
You know, it's like 1 to 1 mapping, which is nice.
So the translation is, is is is is faster.
Of course, the only the suffering we have is the SSD.
And that's the nature of if you write a page, you have to invalidate it and the block and the user
really need to um.
And then we are basically rewrite this whole map.
The flash translation layer.
That's what I thought.
I'll talk about that.
See you in the next one.