Figure 915 Check if a desired allocation can be fulfilled with a given reservation window
If the file is equipped with a reservation window and a goal block is specified (as checked by the condition grp_goal >0), the kernel has to check if the desired allocation will fit into the existing reservation. Starting from the desired allocation goal that specifies a block number relative to the beginning of the group, the code computes the number of blocks until the end of the block group. The calculation is illustrated in Figure 9-15. If the desired allocation as given by count is larger than the possible region, the reservation window is increased with try_to_extend_reservation. The function simply queries the pre-allocation data structures to see if no other reservation window prevents the current window to grow, and does so if possible.
Finally, the kernel can pass the allocation request together with the (possibly modified) reservation window to ext2_try_to_allocate. While the function guarantees that a consecutive number of blocks is allocated if some free space can be found, it cannot guarantee that the desired number of blocks is available. This has some implications on the returned values. While the first allocated block is returned as the direct function result, the number of allocated blocks must be passed upward via the pointer num.
If some space could be allocated, ret is larger than or equal to zero. The kernel then needs to update the allocation hit counter rsv_alloc_hit and return the number of allocated blocks via the count pointer. If the allocation has failed, the loop needs to start again. Since ret is negative in this case, the kernel allocates a new reservation window in the next run as guaranteed by the condition ret <0 in the initial if conditional. Otherwise, everything runs again as described.
Finally, ext2_try_to_allocate is responsible for the low-level allocation that directly interacts with the block bitmaps. Recall that the function can work with a reservation window or not. The kernel now needs to search through the block bitmap, and thus an interval for the search needs to be determined. Note that the boundaries are specified relative to the current block group. This means that numbering starts from zero. A number of scenarios is distinguished, and Figure 9-16 illustrates various cases.
(no reservation window & goal block)
I block group reservation ------1 window
| goal block
Continue reading here: Figure 916 Search interval selection for block allocation in ext2trytoallocate
Was this article helpful?