Allocating JFS2 file space

File space allocation is the method by which data is apportioned physical storage space in the operating system.

The kernel allocates disk space to a file or directory in the form of logical blocks. A logical block refers to the division of a file or directory contents into 512, 1024, 2048, or 4096 byte units. When a JFS2 file system is created, the logical block size is specified to be one of 512, 1024, 2048, or 4096 bytes. Logical blocks are not tangible entities; however, the data in a logical block consumes physical storage space on the disk. Each file or directory consists of 0 or more logical blocks.

Full and partial logical blocks

A file or directory may contain full or partial logical blocks. A full logical block contains 512, 1024, 2048, or 4096 bytes of data, depending on the file system block size specified when the JFS2 file system was created. Partial logical blocks occur when the last logical block of a file or directory contains less than a file-system block size of data.

For example, a JFS2 file system with a logical block size of 4096 with a file of 8192 bytes is two logical blocks. The first 4096 bytes reside in the first logical block and the following 4096 bytes reside in the second logical block. Likewise, a file of 4608 bytes consists of two logical blocks. However, the last logical block is a partial logical block containing the last 512 bytes of the file's data.

JFS2 File space allocation

The default block size is 4096 bytes. You can specify smaller block sizes with the mkfs command during a file system's creation. Allowable block sizes are 512, 1024, 2048, and 4096 bytes. You can use only one block size in a file system.

The kernel allocates disk space so only the last file system block of data receives a partial block allocation. As the partial block grows beyond the limits of its current allocation, additional blocks are allocated.

Block reallocation also occurs if data is added to logical blocks that represent file holes. A file hole is an empty logical block located prior to the last logical block that stores data. (File holes do not occur within directories.) These empty logical blocks are not allocated blocks. However, as data is added to file holes, allocation occurs. Each logical block that was not previously allocated disk space is allocated a file system block of space.

Additional block allocation is not required if existing data in the middle of a file or a directory is overwritten. The logical block containing the existing data has already been allocated file system blocks.

JFS2 tries to maintain contiguous allocation of a file or directory's logical blocks on the disk. Maintaining contiguous allocation lessens seek time because the data for a file or directory can be accessed sequentially and found on the same area of the disk. The disk space required for contiguous allocation might not be available if another file or directory has already written to it.

The file system uses a bitmap called the block allocation map to record the status of every block in the file system. When the file system needs to allocate a new block, it refers to the block allocation map to identify which blocks are available. A block can only be allocated to a single file or directory at a time.

Extents

An extent is a contiguous variable-length sequence of file system blocks allocated to a JFS2 object as a unit. Large extents may span multiple allocation groups.

An i-node represents every JFS2 object. I-nodes contain the expected object-specific information such as time stamps or file type (regular or directory, and so on). They also contain a B+ tree to record the allocation of extents.

The length and address values are necessary to define an extent. The length is measured in units of the file system block size. A 24-bit value represents the length of an extent, so an extent can range in size from 1 to 224 -1 file system blocks. Therefore, the size of the maximum extent depends on the file system block size. The address is the address of the first block of the extent. The address is also in units of file system blocks; it is the block offset from the beginning of the file system.

An extent-based file system combined with user-specified file system block size allows JFS2 to not have separate support for internal fragmentation. You can configure the file system with a small file system block size, such as 512 bytes, to minimize internal fragmentation for file systems with large numbers of small-sized files.

In general, the allocation policy for JFS2 tries to maximize contiguous allocation by allowing a minimum number of extents, with each extent as large and contiguous as possible. This allows for larger I/O transfer, resulting in improved performance. However, in some cases, this is not always possible.

B+ trees

The B+ tree data structure is used for file layout. The most common operations that JFS2 performs are reading and writing extents. B+ trees are used to help with performance of these operations.

An extent allocation descriptor (xad_t structure) describes the extent and adds two more fields that are needed for representing files: an offset field, describing the logical byte address the extent represents, and a flags field. The xad_t structure is defined in the /usr/include/j2/j2_xtree.h file.

An xad structure describes two abstract ranges:

  • The physical range of disk blocks. This starts at file system block number addressXAD(xadp) address and extends for lengthXAD(xadp) file system blocks.
  • The logical range of bytes within a file. This starts at byte number offsetXAD(xadp)*(file system block size) and extends for lengthXAD(xadp)*(file system block size.)

The physical range and the logical range are both the same number of bytes in length. Note that offset is stored in units of file system block size (for example, a value of 3) in offset means 3 file system blocks, not 3 bytes. Extents within a file are always aligned on file system block size boundaries.

JFS2 limitation

JFS2 requires contiguous free space of at least a page, or 4 KB, in length when extending files. If you do not have contiguous free space of at least 4 KB, then the file system does not allow the extension of the file, even if there is enough available storage space in smaller blocks.