Skip to content

Commit 140a525

Browse files
jankaratytso
authored andcommitted
ext4: factor out determining of hole size
ext4_ext_put_gap_in_cache() determines hole size in the extent tree, then trims this with possible delayed allocated blocks, and inserts the result into the extent status tree. Factor out determination of the size of the hole in the extent tree as we will need this information in ext4_ext_map_blocks() as well. Signed-off-by: Jan Kara <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 87d8a74 commit 140a525

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

fs/ext4/extents.c

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,59 +2299,69 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
22992299
}
23002300

23012301
/*
2302-
* ext4_ext_put_gap_in_cache:
2303-
* calculate boundaries of the gap that the requested block fits into
2304-
* and cache this gap
2302+
* ext4_ext_determine_hole - determine hole around given block
2303+
* @inode: inode we lookup in
2304+
* @path: path in extent tree to @lblk
2305+
* @lblk: pointer to logical block around which we want to determine hole
2306+
*
2307+
* Determine hole length (and start if easily possible) around given logical
2308+
* block. We don't try too hard to find the beginning of the hole but @path
2309+
* actually points to extent before @lblk, we provide it.
2310+
*
2311+
* The function returns the length of a hole starting at @lblk. We update @lblk
2312+
* to the beginning of the hole if we managed to find it.
23052313
*/
2306-
static void
2307-
ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
2308-
ext4_lblk_t block)
2314+
static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2315+
struct ext4_ext_path *path,
2316+
ext4_lblk_t *lblk)
23092317
{
23102318
int depth = ext_depth(inode);
2311-
ext4_lblk_t len;
2312-
ext4_lblk_t lblock;
23132319
struct ext4_extent *ex;
2314-
struct extent_status es;
2320+
ext4_lblk_t len;
23152321

23162322
ex = path[depth].p_ext;
23172323
if (ex == NULL) {
23182324
/* there is no extent yet, so gap is [0;-] */
2319-
lblock = 0;
2325+
*lblk = 0;
23202326
len = EXT_MAX_BLOCKS;
2321-
ext_debug("cache gap(whole file):");
2322-
} else if (block < le32_to_cpu(ex->ee_block)) {
2323-
lblock = block;
2324-
len = le32_to_cpu(ex->ee_block) - block;
2325-
ext_debug("cache gap(before): %u [%u:%u]",
2326-
block,
2327-
le32_to_cpu(ex->ee_block),
2328-
ext4_ext_get_actual_len(ex));
2329-
} else if (block >= le32_to_cpu(ex->ee_block)
2327+
} else if (*lblk < le32_to_cpu(ex->ee_block)) {
2328+
len = le32_to_cpu(ex->ee_block) - *lblk;
2329+
} else if (*lblk >= le32_to_cpu(ex->ee_block)
23302330
+ ext4_ext_get_actual_len(ex)) {
23312331
ext4_lblk_t next;
2332-
lblock = le32_to_cpu(ex->ee_block)
2333-
+ ext4_ext_get_actual_len(ex);
23342332

2333+
*lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
23352334
next = ext4_ext_next_allocated_block(path);
2336-
ext_debug("cache gap(after): [%u:%u] %u",
2337-
le32_to_cpu(ex->ee_block),
2338-
ext4_ext_get_actual_len(ex),
2339-
block);
2340-
BUG_ON(next == lblock);
2341-
len = next - lblock;
2335+
BUG_ON(next == *lblk);
2336+
len = next - *lblk;
23422337
} else {
23432338
BUG();
23442339
}
2340+
return len;
2341+
}
23452342

2346-
ext4_es_find_delayed_extent_range(inode, lblock, lblock + len - 1, &es);
2343+
/*
2344+
* ext4_ext_put_gap_in_cache:
2345+
* calculate boundaries of the gap that the requested block fits into
2346+
* and cache this gap
2347+
*/
2348+
static void
2349+
ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2350+
ext4_lblk_t hole_len)
2351+
{
2352+
struct extent_status es;
2353+
2354+
ext4_es_find_delayed_extent_range(inode, hole_start,
2355+
hole_start + hole_len - 1, &es);
23472356
if (es.es_len) {
23482357
/* There's delayed extent containing lblock? */
2349-
if (es.es_lblk <= lblock)
2358+
if (es.es_lblk <= hole_start)
23502359
return;
2351-
len = min(es.es_lblk - lblock, len);
2360+
hole_len = min(es.es_lblk - hole_start, hole_len);
23522361
}
2353-
ext_debug(" -> %u:%u\n", lblock, len);
2354-
ext4_es_insert_extent(inode, lblock, len, ~0, EXTENT_STATUS_HOLE);
2362+
ext_debug(" -> %u:%u\n", hole_start, hole_len);
2363+
ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2364+
EXTENT_STATUS_HOLE);
23552365
}
23562366

23572367
/*
@@ -4362,11 +4372,15 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
43624372
* we couldn't try to create block if create flag is zero
43634373
*/
43644374
if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4375+
ext4_lblk_t hole_start, hole_len;
4376+
43654377
/*
43664378
* put just found gap into cache to speed up
43674379
* subsequent requests
43684380
*/
4369-
ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
4381+
hole_start = map->m_lblk;
4382+
hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
4383+
ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
43704384
goto out2;
43714385
}
43724386

0 commit comments

Comments
 (0)