18

I was reading about the ext4 filesystem that it supports compression, encryption and a feature called extents which is used to save disk space. What are extents and how are they are effective for saving disk space?

1
  • "Extents allocate space on a storage device in blocks and only store the starting block location in the inode table. This helps save space in the inode table by not having to list all the data blocks used to store data from the file". See also ext4 faq and IBM's ext4 articles Commented Aug 14, 2015 at 21:46

2 Answers 2

20

Extents reduce the amount of metadata needed to keep track of the data blocks for large files. Instead of storing a list of every individual block which makes up the file, the idea is to store just the address of the first and last block of each continuous range of blocks. These continuous ranges of data blocks (and the pairs of numbers which represent them) are called extents.

The addresses of a file's first few data blocks are stored in the inode, but since the inode has a fixed size, this only works for small files. In ext2 or ext3, large files require the use of indirect blocks to store the rest of the list of block addresses which won't fit in the inode itself. That is, the inode contains the address of a block which itself contains a list of blocks. These are called indirect blocks. These extra blocks are not usually needed when using extents, because storing an extent takes a constant amount of space regardless of how big a range of blocks it describes.

A very fragmented file might still need extra metadata blocks (which ext4 calls extent nodes) to store a long list of extents, but typically still much fewer than would be needed otherwise.

The reduction in metadata size is usually quite small in proportion to the size of the file, though. The main motivation for extents is about improving performance (by reducing fragmentation and having fewer metadata blocks to read and write) rather than saving space per se.

5
  • Not exactly, I think. You will typically still need indirect blocks, but the amount of metadata (in inode and indirect blocks together) will be much smaller (if the file system is not extremely fragmented).
    – Philippos
    Commented Apr 21, 2017 at 10:01
  • 1
    The inode can still point to additional blocks if there are too many extents to fit in the inode itself. They're called extent nodes, though, rather than indirect blocks. See ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Extent_Tree
    – sengi
    Commented Apr 28, 2017 at 19:43
  • 1
    @sengi - Is the purpose of extents to only reduce the number of indirect blocks associated with large files? What do offset extents refer to?
    – Motivated
    Commented Dec 31, 2018 at 23:55
  • @Philippos - In reading sengi's response, it suggests that there are no longer indirect blocks? Is this your understanding?
    – Motivated
    Commented Dec 31, 2018 at 23:56
  • 1
    With the advent of larger hard disks, it also more closely suits newer file types whose filesize is known at creation time, which are typically downloaded/copied once and not modified, such as photos, music, videos, etc.
    – user117529
    Commented Sep 8, 2020 at 22:51
2

From http://ext2read.blogspot.ch/2010/03/how-ext4-extents-work-earlier-ext2-and.html :

Earlier Ext2 and Ext3 had the limitation on the size of the file. It used 32 bit block number to access the data blocks. So that limited the maximum size of file to be 2^32 * blocksize(eg. 4k**) = 16TB*. Also the access time for large Files were slow because in had to go through lots of indirection.Ext4 Filesystem can support very large files it has 48 bits to adress a block. Also its uses extents to store data so access time is faster for large files.

The information for the data blocks are stored in the i_data of the inode structure. In a system without Extents, the first 12 entries contains the block numbers of the first 12 blocks of data. Then it contains the block number for the Indirect blocks. That block contains the array of block numbers which point to the data. Similarly, there is Double indirect block and triple indirect block. So if we need to get the data from a very large file, we need to go through those indirection.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .