I have recently been interested in low level computer science (as a hobby), and I have just learned about the way a computer organizes and stores information on a hard drive. I have learned the basics of how a filesystem works. I understand that in the case of Unix, an inode entry is created for each file in the inode table near the beginning of your drive, and each inode has a preallocated block for storing direct and indirect pointers to fragments of the file.
I have a few questions regarding this:
If the block pointers are stored in order (which I'm assuming they are), how does the computer handle deleting or modifying from the middle of a file? As I've understood so far, filesystems were designed because storing files in order on the drive is messy and modifying/removing files in the middle of the drive would mean everything else would have to be shifted around. But don't inodes create this same issue on a smaller scale? When deleting or modifying the middle of a file, if the computer reads from the block pointers in order, you would need to shift every pointer back one space, even in the indirect pointers, because there would be a new empty space in the inode table for a block pointer, assuming you deleted/modified more than the size of the block its pointing to. How does the filesystem handle this?
The same question applies to the actual inode table itself. When removing a file indexed somewhere between other inodes, are all the following inodes just shifted back? I would think if this is how the filesystem truly operates, it would slow down your computer a lot.
How is empty space in each block dealt with? Say the size of each block being pointed to is 4K on this particular system. What if I delete 2K of data from the center of the file? Is the remaining 2K zeroed over and left unused? What about the end of the file? If this is the case how does the CPU know how much free space is at the end of each block when loading a file into RAM? And wouldn't this leave a lot of wasted storage space on your drive?
If someone could clear these questions up for me that would be great. I have a curious mind. Thank you.