Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 9: The Extended Filesystem Family


inoderepresents the inode for which the allocation is performed, whilecountdesignates the desired
number of blocks. Since the function returns the number of the first block in the allocated block sequence,
possible error codes cannot be passed as a function result, thus the pointererrpis used. Finally, thegoal
parameter allows for specifying agoal block. This provides a hint to the allocation code about which block
would be preferred. This is only a suggestion: Should this block not be available, then any other block
can be selected.

First of all, the function decides if the pre-allocation mechanism should be used and a reserved, but
not yet allocated area be created. The choice is simple: If the inode is equipped with information for
pre-allocation, then use it; otherwise, not.

Allocations only make sense if the filesystem contains at least one free block, andext2_has_free_blocks
checks this. If the condition is not fulfilled, the allocation can immediately be canceled.

In a world where all wishes come true, the goal block will be free, but in reality, this need not be the case.
In fact, the goal block need not even be a valid block at all, and the kernel needs to check this (esis the
ext2_super_blockinstance for the filesystem under consideration).

fs/ext2/balloc.c
if (goal < le32_to_cpu(es->s_first_data_block) ||
goal >= le32_to_cpu(es->s_blocks_count))
goal = le32_to_cpu(es->s_first_data_block);

group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
EXT2_BLOCKS_PER_GROUP(sb);
goal_group = group_no;
retry_alloc:
gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);

If the goal block is not within a valid range, the first data block of the filesystem is picked as the new
goal. In any case, the block group of the goal block is computed.ext2_get_group_descprovides the
corresponding group descriptor.

Afterward, a little bookkeeping for the pre-allocation mechanism is again necessary. If reservations
are enabled but the free space is not sufficient to fulfill it, then the mechanism is turned off. By
callingext2_try_to_allocate_with_rsv, the kernel then tries to actually reserve the desired data
blocks — possibly using the reservation mechanism. As promised, this function is discussed below.

For now, let us just observe the two possible outcomes:


  1. The allocation was successful. In this case,ext2_new_blocksneeds to update the statistical
    information, but is otherwise done and can return to the caller.

  2. If the request could not be satisfied in the current block group, then all other block groups
    are tried. If this still fails, the whole allocation is restartedwithoutthe pre-allocation mecha-
    nism in case it was still turned on at this point — recall that it might have been turned off by
    default or in the previous course of action.


Pre-allocation Handling


In the hierarchy of ext2 allocation functions, we’ve come as deep down asext2_try_to_allocate_with_
rsv. However, there’s good news: The kernel source code cheers us up by remarking that this is ‘‘the
Free download pdf