// SPDX-License-Identifier: GPL-2.0
/*
* f2fs extent cache support
*
* Copyright (c) 2015 Motorola Mobility
* Copyright (c) 2015 Samsung Electronics
* Authors: Jaegeuk Kim <jaegeuk@kernel.org>
* Chao Yu <chao2.yu@samsung.com>
*
* block_age-based extent cache added by:
* Copyright (c) 2022 xiaomi Co., Ltd.
* http://www.xiaomi.com/
*/
#include <linux/fs.h>
#include <linux/f2fs_fs.h>
#include "f2fs.h"
#include "node.h"
#include <trace/events/f2fs.h>
bool sanity_check_extent_cache(struct inode *inode, struct page *ipage)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct f2fs_extent *i_ext = &F2FS_INODE(ipage)->i_ext;
struct extent_info ei;
get_read_extent_info(&ei, i_ext);
if (!ei.len)
return true;
if (!f2fs_is_valid_blkaddr(sbi, ei.blk, DATA_GENERIC_ENHANCE) ||
!f2fs_is_valid_blkaddr(sbi, ei.blk + ei.len - 1,
DATA_GENERIC_ENHANCE)) {
f2fs_warn(sbi, "%s: inode (ino=%lx) extent info [%u, %u, %u] is incorrect, run fsck to fix",
__func__, inode->i_ino,
ei.blk, ei.fofs, ei.len);
return false;
}
return true;
}
static void __set_extent_info(struct extent_info *ei,
unsigned int fofs, unsigned int len,
block_t blk, bool keep_clen,
unsigned long age, unsigned long last_blocks,
enum extent_type type)
{
ei->fofs = fofs;
ei->len = len;
if (type == EX_READ) {
ei->blk = blk;
if (keep_clen)
return;
#ifdef CONFIG_F2FS_FS_COMPRESSION
ei->c_len = 0;
#endif
} else if (type == EX_BLOCK_AGE) {
ei->age = age;
ei->last_blocks = last_blocks;
}
}
static bool __init_may_extent_tree(struct inode *inode, enum extent_type type)
{
if (type == EX_READ)
return test_opt(F2FS_I_SB(inode), READ_EXTENT_CACHE) &&
S_ISREG(inode->i_mode);
if (type == EX_BLOCK_AGE)
return test_opt(F2FS_I_SB(inode), AGE_EXTENT_CACHE) &&
(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode));
return false;
}
static bool __may_extent_tree(struct inode *inode, enum extent_type type)
{
/*
* for recovered files during mount do not create extents
* if shrinker is not registered.
*/
if (list_empty(&F2FS_I_SB(inode)->s_list))
return false;
if (!__init_may_extent_tree(inode, type))
return false;
if (type == EX_READ) {
if (is_inode_flag_set(inode, FI_NO_EXTENT))
return false;
if (is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
!f2fs_sb_has_readonly(F2FS_I_SB(inode)))
return false;
} else if (type == EX_BLOCK_AGE) {
if (is_inode_flag_set(inode, FI_COMPRESSED_FILE))
return false;
if (file_is_cold(inode))
return false;
}
return true;
}
static void __try_update_largest_extent(struct extent_tree *et,
struct extent_node *en)
{
if (et->type != EX_READ)
return;
if (en->ei.len <= et->largest.len)
return;
et->largest = en->ei;
et->largest_updated = true;
}
static bool __is_extent_mergeable(struct extent_info *back,
struct extent_info *front, enum extent_type type)
{
if (type == EX_READ) {
#ifdef CONFIG_F2FS_FS_COMPRESSION
if (back->c_len && back->len != back->c_len)
return false;
if (front->c_len && front->len != front->c_len)
return false;
#endif
return (back->fofs + back->len == front->fofs &&
back->blk + back->len == front->blk);
}