2

Let's say I create a text file and write "Hello world!" to it. I stick with the default 4096 byte allocation size (assuming NTFS file system). I wonder what happens when I change (overwrite) the data in the file, to for example "Hello hello!"

I assume a physical hard drive simply overwrites the last 5 bytes in the allocated block, because it doesn't wear out with frequent writing. But what happens in case of an SSD? Does the system simply allocate another 4KB and leaves the previous one intact (that sounds horribly inefficient)? Because if the NAND memory chips can only survive so many writes they would corrupt rather quickly with frequent changes, right?

Or do file systems handle it in another, more efficient way? I'm coming from CS background so please excuse my ignorance :L

YepNope
  • 31
  • 1
  • 1
    Sounds like you have both scenarios correct. It sounds inefficient for small files, but most files require many, many 4k blocks. – Neil_UK Sep 11 '22 at 17:54
  • It depends quite heavily on the drive in question, the filesystem, and any translation layers between the drive and filesystem. In the case of SSDs, there are specialized drive controller ICs just for load balancing to avoid the filesystem having to manage all that. (though modern filesystems can handle it, as I understand it) – Hearth Sep 11 '22 at 17:55
  • Particularly in the case of magnetic drives, the computer may overwrite the entire file, not just the changed bits, to simplify the hardware. Or, if the file is made much larger, it may fragment the file, putting part of it elsewhere on the drive if there wasn't enough room in the current location. – Hearth Sep 11 '22 at 17:57
  • It depends on both how the file system handles it and the editing program I think. In most cases, I would think that the whole file gets overwritten. But part of it could be overwritten too. I doubt it would allocate more space though, that doesn't make much sense. – Hormoz Sep 11 '22 at 17:58
  • A very warm welcome to the site :-) This question is essentially asking for tuition on SSDs and filings systems. It's a Q&A site, rather than discussion forum, and can't be a personal tutorial service, which is effectively what you're asking for. VTC for those reasons. You'll find plenty of detailed and free documentation on this available on the internet for you to research and learn from. I know as that's where I learnt this very subject from. Hope you can see how the site works but refer to other high-voted questions for examples of what's well-regarded here. Thanks. – TonyM Sep 11 '22 at 18:20
  • The details of how SSD drives ACTUALLY work are hidden from us. But they do implement some kind of wear leveling to avoid writing to the same locations over and over. Also, since they hide their inner workings, they present themselves to the operating system just like spinning platter drives. – user57037 Sep 12 '22 at 00:20
  • 2
    @TonyM your comment does not seem all that welcoming to me. Like you are saying "warm welcome, and even though I could answer your question because I have spent a great deal of time studying it on the internet, I won't answer it and instead I suggest you learn it the hard way since that is what I did." I realize that is probably not what you intended. I am just telling you how it sounds to me. If it sounds that way to me it probably sounds that way to some other people also. – user57037 Sep 12 '22 at 00:25
  • 2
    @mkeith On the other hand, it is basically an off-topic "how does an electrical device called SSD work" kind of question, not an electronics question. And the question shows no research effort, such as simple Google search or reading the Wikipedia page on SSDs. – Justme Sep 12 '22 at 00:41
  • It’s hard to see what happens under the hood of an SSD or usb stick unless you get real low level. Generally NAND flash does not support partial writes, so sector remapping is used. Depending on the NAND technology , a given sector might only have a life of a few thousand writes. As for filesystems, each file has an overhead be it empty or jigabytes and they won’t try to fill every sector. Your 5 byte file might just end up consuming 64k bytes or more of storage. – Kartman Sep 12 '22 at 05:10
  • @mkeith, it's simply explaining politely how the question lines up with the site is about, sorry you can't see that or have chosen not to. It sounds like you disagree with the principle but I don't understand your claims of "learning the hard way". In engineering, (pro, amateur, learning), research and self-education are the bedrock on which one stands. OPs learn far more that way. Once it's exhausted, sure, go to a wider audience. But this site's not the place if that info already exists, is straightforward to find and, most importantly, already better written in much better detail elsewhere. – TonyM Sep 12 '22 at 07:44
  • 2
    @Kartman This is NTFS. If you create a 5-byte file and take properties of that file, Windows will report size on disk is 0 bytes because it fits into the directory entry. But it depends on the filesystem. – Justme Sep 12 '22 at 07:52

2 Answers2

5

Actually since you specifically ask about NTFS, the file size would be so small the contents can be stored inside the file entry structure, so it won't allocate a separate data block for something like 15 bytes.

And what the filesystem does is one thing, and what the SSD Flash controller does is completely unrelated, as SSDs have wear leveling so they just remap sectors and do a erase only when a block is fully unused or a read-modify-write is really needed.

Justme
  • 127,425
  • 3
  • 97
  • 261
3

Drives do not support partially overwriting sectors.

For this reason and more, the operating system likes to keep copies of drive data in memory, called caching. It will partially overwrite the data in the disk cache, and when it feels like it, it will write the entire sector from the disk cache to the disk.

It is likely that the disk cache works in units of sectors, not blocks, but this depends on the specific operating system. The filesystem works in blocks, by definition. Often each block is 1 sector so the distinction is irrelevant. Modern drives have 4096-byte sectors.

user253751
  • 11,998
  • 2
  • 21
  • 37