Skip to content

Commit 69eb33d

Browse files
gnehzuiltytso
authored andcommitted
ext4: remove single extent cache
Single extent cache could be removed because we have extent status tree as a extent cache, and it would be better. Signed-off-by: Zheng Liu <wenqing.lz@taobao.com> Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: Jan kara <jack@suse.cz>
1 parent d100eef commit 69eb33d

5 files changed

Lines changed: 38 additions & 163 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,17 +809,6 @@ do { \
809809

810810
#endif /* defined(__KERNEL__) || defined(__linux__) */
811811

812-
/*
813-
* storage for cached extent
814-
* If ec_len == 0, then the cache is invalid.
815-
* If ec_start == 0, then the cache represents a gap (null mapping)
816-
*/
817-
struct ext4_ext_cache {
818-
ext4_fsblk_t ec_start;
819-
ext4_lblk_t ec_block;
820-
__u32 ec_len; /* must be 32bit to return holes */
821-
};
822-
823812
#include "extents_status.h"
824813

825814
/*
@@ -886,7 +875,6 @@ struct ext4_inode_info {
886875
struct inode vfs_inode;
887876
struct jbd2_inode *jinode;
888877

889-
struct ext4_ext_cache i_cached_extent;
890878
/*
891879
* File creation time. Its function is same as that of
892880
* struct timespec i_{a,c,m}time in the generic inode.

‎fs/ext4/ext4_extents.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,6 @@ static inline unsigned short ext_depth(struct inode *inode)
193193
return le16_to_cpu(ext_inode_hdr(inode)->eh_depth);
194194
}
195195

196-
static inline void
197-
ext4_ext_invalidate_cache(struct inode *inode)
198-
{
199-
EXT4_I(inode)->i_cached_extent.ec_len = 0;
200-
}
201-
202196
static inline void ext4_ext_mark_uninitialized(struct ext4_extent *ext)
203197
{
204198
/* We can not have an uninitialized extent of zero length! */

‎fs/ext4/extents.c‎

Lines changed: 38 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int ext4_split_extent_at(handle_t *handle,
112112
int flags);
113113

114114
static int ext4_find_delayed_extent(struct inode *inode,
115-
struct ext4_ext_cache *newex);
115+
struct extent_status *newes);
116116

117117
static int ext4_ext_truncate_extend_restart(handle_t *handle,
118118
struct inode *inode,
@@ -714,7 +714,6 @@ int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
714714
eh->eh_magic = EXT4_EXT_MAGIC;
715715
eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
716716
ext4_mark_inode_dirty(handle, inode);
717-
ext4_ext_invalidate_cache(inode);
718717
return 0;
719718
}
720719

@@ -1963,7 +1962,6 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
19631962
ext4_ext_drop_refs(npath);
19641963
kfree(npath);
19651964
}
1966-
ext4_ext_invalidate_cache(inode);
19671965
return err;
19681966
}
19691967

@@ -1972,8 +1970,8 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
19721970
struct fiemap_extent_info *fieinfo)
19731971
{
19741972
struct ext4_ext_path *path = NULL;
1975-
struct ext4_ext_cache newex;
19761973
struct ext4_extent *ex;
1974+
struct extent_status es;
19771975
ext4_lblk_t next, next_del, start = 0, end = 0;
19781976
ext4_lblk_t last = block + num;
19791977
int exists, depth = 0, err = 0;
@@ -2047,31 +2045,31 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
20472045
BUG_ON(end <= start);
20482046

20492047
if (!exists) {
2050-
newex.ec_block = start;
2051-
newex.ec_len = end - start;
2052-
newex.ec_start = 0;
2048+
es.es_lblk = start;
2049+
es.es_len = end - start;
2050+
es.es_pblk = 0;
20532051
} else {
2054-
newex.ec_block = le32_to_cpu(ex->ee_block);
2055-
newex.ec_len = ext4_ext_get_actual_len(ex);
2056-
newex.ec_start = ext4_ext_pblock(ex);
2052+
es.es_lblk = le32_to_cpu(ex->ee_block);
2053+
es.es_len = ext4_ext_get_actual_len(ex);
2054+
es.es_pblk = ext4_ext_pblock(ex);
20572055
if (ext4_ext_is_uninitialized(ex))
20582056
flags |= FIEMAP_EXTENT_UNWRITTEN;
20592057
}
20602058

20612059
/*
2062-
* Find delayed extent and update newex accordingly. We call
2063-
* it even in !exists case to find out whether newex is the
2060+
* Find delayed extent and update es accordingly. We call
2061+
* it even in !exists case to find out whether es is the
20642062
* last existing extent or not.
20652063
*/
2066-
next_del = ext4_find_delayed_extent(inode, &newex);
2064+
next_del = ext4_find_delayed_extent(inode, &es);
20672065
if (!exists && next_del) {
20682066
exists = 1;
20692067
flags |= FIEMAP_EXTENT_DELALLOC;
20702068
}
20712069
up_read(&EXT4_I(inode)->i_data_sem);
20722070

2073-
if (unlikely(newex.ec_len == 0)) {
2074-
EXT4_ERROR_INODE(inode, "newex.ec_len == 0");
2071+
if (unlikely(es.es_len == 0)) {
2072+
EXT4_ERROR_INODE(inode, "es.es_len == 0");
20752073
err = -EIO;
20762074
break;
20772075
}
@@ -2102,9 +2100,9 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
21022100

21032101
if (exists) {
21042102
err = fiemap_fill_next_extent(fieinfo,
2105-
(__u64)newex.ec_block << blksize_bits,
2106-
(__u64)newex.ec_start << blksize_bits,
2107-
(__u64)newex.ec_len << blksize_bits,
2103+
(__u64)es.es_lblk << blksize_bits,
2104+
(__u64)es.es_pblk << blksize_bits,
2105+
(__u64)es.es_len << blksize_bits,
21082106
flags);
21092107
if (err < 0)
21102108
break;
@@ -2114,7 +2112,7 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
21142112
}
21152113
}
21162114

2117-
block = newex.ec_block + newex.ec_len;
2115+
block = es.es_lblk + es.es_len;
21182116
}
21192117

21202118
if (path) {
@@ -2125,21 +2123,6 @@ static int ext4_fill_fiemap_extents(struct inode *inode,
21252123
return err;
21262124
}
21272125

2128-
static void
2129-
ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
2130-
__u32 len, ext4_fsblk_t start)
2131-
{
2132-
struct ext4_ext_cache *cex;
2133-
BUG_ON(len == 0);
2134-
spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
2135-
trace_ext4_ext_put_in_cache(inode, block, len, start);
2136-
cex = &EXT4_I(inode)->i_cached_extent;
2137-
cex->ec_block = block;
2138-
cex->ec_len = len;
2139-
cex->ec_start = start;
2140-
spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2141-
}
2142-
21432126
/*
21442127
* ext4_ext_put_gap_in_cache:
21452128
* calculate boundaries of the gap that the requested block fits into
@@ -2156,9 +2139,10 @@ ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
21562139

21572140
ex = path[depth].p_ext;
21582141
if (ex == NULL) {
2159-
/* there is no extent yet, so gap is [0;-] */
2160-
lblock = 0;
2161-
len = EXT_MAX_BLOCKS;
2142+
/*
2143+
* there is no extent yet, so gap is [0;-] and we
2144+
* don't cache it
2145+
*/
21622146
ext_debug("cache gap(whole file):");
21632147
} else if (block < le32_to_cpu(ex->ee_block)) {
21642148
lblock = block;
@@ -2192,52 +2176,6 @@ ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
21922176
}
21932177

21942178
ext_debug(" -> %u:%lu\n", lblock, len);
2195-
ext4_ext_put_in_cache(inode, lblock, len, 0);
2196-
}
2197-
2198-
/*
2199-
* ext4_ext_in_cache()
2200-
* Checks to see if the given block is in the cache.
2201-
* If it is, the cached extent is stored in the given
2202-
* cache extent pointer.
2203-
*
2204-
* @inode: The files inode
2205-
* @block: The block to look for in the cache
2206-
* @ex: Pointer where the cached extent will be stored
2207-
* if it contains block
2208-
*
2209-
* Return 0 if cache is invalid; 1 if the cache is valid
2210-
*/
2211-
static int
2212-
ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2213-
struct ext4_extent *ex)
2214-
{
2215-
struct ext4_ext_cache *cex;
2216-
int ret = 0;
2217-
2218-
/*
2219-
* We borrow i_block_reservation_lock to protect i_cached_extent
2220-
*/
2221-
spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
2222-
cex = &EXT4_I(inode)->i_cached_extent;
2223-
2224-
/* has cache valid data? */
2225-
if (cex->ec_len == 0)
2226-
goto errout;
2227-
2228-
if (in_range(block, cex->ec_block, cex->ec_len)) {
2229-
ex->ee_block = cpu_to_le32(cex->ec_block);
2230-
ext4_ext_store_pblock(ex, cex->ec_start);
2231-
ex->ee_len = cpu_to_le16(cex->ec_len);
2232-
ext_debug("%u cached by %u:%u:%llu\n",
2233-
block,
2234-
cex->ec_block, cex->ec_len, cex->ec_start);
2235-
ret = 1;
2236-
}
2237-
errout:
2238-
trace_ext4_ext_in_cache(inode, block, ret);
2239-
spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2240-
return ret;
22412179
}
22422180

22432181
/*
@@ -2677,8 +2615,6 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
26772615
return PTR_ERR(handle);
26782616

26792617
again:
2680-
ext4_ext_invalidate_cache(inode);
2681-
26822618
trace_ext4_ext_remove_space(inode, start, depth);
26832619

26842620
/*
@@ -3920,35 +3856,6 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
39203856
map->m_lblk, map->m_len, inode->i_ino);
39213857
trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
39223858

3923-
/* check in cache */
3924-
if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
3925-
if (!newex.ee_start_lo && !newex.ee_start_hi) {
3926-
if ((sbi->s_cluster_ratio > 1) &&
3927-
ext4_find_delalloc_cluster(inode, map->m_lblk))
3928-
map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3929-
3930-
if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3931-
/*
3932-
* block isn't allocated yet and
3933-
* user doesn't want to allocate it
3934-
*/
3935-
goto out2;
3936-
}
3937-
/* we should allocate requested block */
3938-
} else {
3939-
/* block is already allocated */
3940-
if (sbi->s_cluster_ratio > 1)
3941-
map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3942-
newblock = map->m_lblk
3943-
- le32_to_cpu(newex.ee_block)
3944-
+ ext4_ext_pblock(&newex);
3945-
/* number of remaining blocks in the extent */
3946-
allocated = ext4_ext_get_actual_len(&newex) -
3947-
(map->m_lblk - le32_to_cpu(newex.ee_block));
3948-
goto out;
3949-
}
3950-
}
3951-
39523859
/* find extent for this block */
39533860
path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
39543861
if (IS_ERR(path)) {
@@ -3995,15 +3902,9 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
39953902
ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
39963903
ee_block, ee_len, newblock);
39973904

3998-
/*
3999-
* Do not put uninitialized extent
4000-
* in the cache
4001-
*/
4002-
if (!ext4_ext_is_uninitialized(ex)) {
4003-
ext4_ext_put_in_cache(inode, ee_block,
4004-
ee_len, ee_start);
3905+
if (!ext4_ext_is_uninitialized(ex))
40053906
goto out;
4006-
}
3907+
40073908
allocated = ext4_ext_handle_uninitialized_extents(
40083909
handle, inode, map, path, flags,
40093910
allocated, newblock);
@@ -4265,10 +4166,9 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
42654166
* Cache the extent and update transaction to commit on fdatasync only
42664167
* when it is _not_ an uninitialized extent.
42674168
*/
4268-
if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
4269-
ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
4169+
if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0)
42704170
ext4_update_inode_fsync_trans(handle, inode, 1);
4271-
} else
4171+
else
42724172
ext4_update_inode_fsync_trans(handle, inode, 0);
42734173
out:
42744174
if (allocated > map->m_len)
@@ -4327,7 +4227,6 @@ void ext4_ext_truncate(struct inode *inode)
43274227
goto out_stop;
43284228

43294229
down_write(&EXT4_I(inode)->i_data_sem);
4330-
ext4_ext_invalidate_cache(inode);
43314230

43324231
ext4_discard_preallocations(inode);
43334232

@@ -4576,42 +4475,42 @@ int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
45764475
}
45774476

45784477
/*
4579-
* If newex is not existing extent (newex->ec_start equals zero) find
4580-
* delayed extent at start of newex and update newex accordingly and
4478+
* If newes is not existing extent (newes->ec_pblk equals zero) find
4479+
* delayed extent at start of newes and update newes accordingly and
45814480
* return start of the next delayed extent.
45824481
*
4583-
* If newex is existing extent (newex->ec_start is not equal zero)
4482+
* If newes is existing extent (newes->ec_pblk is not equal zero)
45844483
* return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
4585-
* extent found. Leave newex unmodified.
4484+
* extent found. Leave newes unmodified.
45864485
*/
45874486
static int ext4_find_delayed_extent(struct inode *inode,
4588-
struct ext4_ext_cache *newex)
4487+
struct extent_status *newes)
45894488
{
45904489
struct extent_status es;
45914490
ext4_lblk_t block, next_del;
45924491

4593-
ext4_es_find_delayed_extent(inode, newex->ec_block, &es);
4492+
ext4_es_find_delayed_extent(inode, newes->es_lblk, &es);
45944493

4595-
if (newex->ec_start == 0) {
4494+
if (newes->es_pblk == 0) {
45964495
/*
4597-
* No extent in extent-tree contains block @newex->ec_start,
4496+
* No extent in extent-tree contains block @newes->es_pblk,
45984497
* then the block may stay in 1)a hole or 2)delayed-extent.
45994498
*/
46004499
if (es.es_len == 0)
46014500
/* A hole found. */
46024501
return 0;
46034502

4604-
if (es.es_lblk > newex->ec_block) {
4503+
if (es.es_lblk > newes->es_lblk) {
46054504
/* A hole found. */
4606-
newex->ec_len = min(es.es_lblk - newex->ec_block,
4607-
newex->ec_len);
4505+
newes->es_len = min(es.es_lblk - newes->es_lblk,
4506+
newes->es_len);
46084507
return 0;
46094508
}
46104509

4611-
newex->ec_len = es.es_lblk + es.es_len - newex->ec_block;
4510+
newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
46124511
}
46134512

4614-
block = newex->ec_block + newex->ec_len;
4513+
block = newes->es_lblk + newes->es_len;
46154514
ext4_es_find_delayed_extent(inode, block, &es);
46164515
if (es.es_len == 0)
46174516
next_del = EXT_MAX_BLOCKS;
@@ -4815,14 +4714,12 @@ int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
48154714
goto out;
48164715

48174716
down_write(&EXT4_I(inode)->i_data_sem);
4818-
ext4_ext_invalidate_cache(inode);
48194717
ext4_discard_preallocations(inode);
48204718

48214719
err = ext4_es_remove_extent(inode, first_block,
48224720
stop_block - first_block);
48234721
err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
48244722

4825-
ext4_ext_invalidate_cache(inode);
48264723
ext4_discard_preallocations(inode);
48274724

48284725
if (IS_SYNC(inode))

‎fs/ext4/move_extent.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,6 @@ mext_replace_branches(handle_t *handle, struct inode *orig_inode,
764764
kfree(donor_path);
765765
}
766766

767-
ext4_ext_invalidate_cache(orig_inode);
768-
ext4_ext_invalidate_cache(donor_inode);
769-
770767
return replaced_count;
771768
}
772769

0 commit comments

Comments
 (0)