Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Or is only the reference changed? It's a general question. Not really Ubuntu-specific.

BTW: I've tried sudo fdisk -l, it doesn't seem to give the full picture.

enter image description here

share|improve this question

2 Answers 2

up vote 2 down vote accepted

A file in *nix consists of three things:

  • The data on the disk
  • An inode that points to the data on a disk and stores useful things like access and modified times, permissions, etc, etc. These will vary based on the filesystem.
  • One (or many) hardlinks that provide the location (path and filename) and point to the inode.

In effect the system is editing the hardlink but that might cause concurrency conflicts so what actually happens is the system creates a new hardlink and then deletes the old one.

I'm the last but from here but I'll try and find a technical source for this.

share|improve this answer
    
Thanks. The hardlink part doesn't make an sense to me. The pointer seems conceptually like a variable in Python, which points to a specific memory adress. –  empedokles Aug 8 at 21:31

I've actually been reading up on this recently, and although it was specifically about ext2, I believe ext3 and ext4 are similar if not the same.

Let's call your physical hard disk sda. At the highest level, your disk is first broken up into separate partitions. Each partition gets its own filesystem that decides how to manage files on that partition. For example, you might have Windows on sda1 using the NTFS filesystem, Ubuntu on sda2 using the ext4 filesystem, and Swap on sda3 that doesn't use a filesystem. Near the beginning of sda, before the partitions start, are two things. First, the boot sector, which your BIOS loads at startup and probably contains grub. Second, the partition table, which lists the details about where all the partitions begin and end on the disk. You can find out details about you partitions using sudo fdisk -l.

Inside the ext4 partition on sda2, the partition is further divided into block groups. Inside a block group is first the superblock, which gives important information about the filesystem and free space. After a few more areas used by the filesystem, are the inodes. Every inode includes a list of the locations of up to 12 data blocks. Each inode can also have a reference to up to three other inodes. The final area in the block group is for actual data, and is divided into blocks (typically 4KB each).

Every directory, file, and link has a unique inode. The data in files is contained in many different disk blocks that could be anywhere on the disk. The file's inode lists where to find all of the potentially scattered data blocks. Likewise, the contents of every directory are stored as data in the same way.

When you open a file, for example /home/user/contacts.txt first the computer finds the inode for the root directory, which it always knows where to find. The inode lists the locations of all the data blocks that describe the contents of the directory. Then the computer searches the data blocks for an entry called "home". The entry for the home directory tells it where to find the inode for home on the disk. It then looks in that inode, searches the specified blocks for user, and repeats the process to find the file contacts.txt, whose inode lists the blocks that contain the file's contents.

If you want to move contacts.txt to /home, first you have to do everything described above to find the file's inode. Then the computer has to navigate to the inode for /home (which will be fast because Linux maintains a cache of the inodes of recently accessed directories called the dentry cache), and adds contacts.txt to the directory's data blocks. Then it has to navigate to the inode of user and search its data blocks for contacts.txt and delete its entry.

Therefore, if both locations are on the same partition, the file's data blocks are never actually moved. The inode reference for the file will simply be moved from the origin directory's data blocks to the destination directory's data blocks. If one location is on a different partition, which counts as an entirely separate file system, the file's inode entry will be deleted, its data blocks copied, and a new inode entry created in the relevant directory's data blocks.

There's also one more thing that goes on in the background on ext3 and ext4. It's called journaling, and it helps ensure filesystem integrity despite crashes. Any time a file operation occurs, the filesystem write an entry in the journal describing what it's going to do. The entry is only deleted once every operation it describes has been completed. In this way, when a computer starts up after a crash in the middle of file operations, it immediately sees outstanding entries in the journal and can attempt to fix the problems.

Source: Modern Operating Systems 3e by Andrew S. Tanenbaum

share|improve this answer
    
I believe the OS does add-then-delete rather than delete-then-add: if the computer crashes during the move, it's better to have the file show up twice than not at all. –  Mark Aug 9 at 1:25
    
Thanks, think you're right, swapped the order. Also added about journaling. –  TheSchwa Aug 9 at 1:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.