Commit e4ebad20 authored by Zhang Yi's avatar Zhang Yi
Browse files

ext4: factor out check for whether a cluster is allocated

hulk inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I9DN5Z


CVE: NA

--------------------------------

Factor out a common helper ext4_da_check_clu_allocated(), check whether
the cluster containing a delalloc block to be added has been delayed or
allocated, no logic changes.

Signed-off-by: default avatarZhang Yi <yi.zhang@huawei.com>
parent 0264e12c
Loading
Loading
Loading
Loading
+35 −17
Original line number Diff line number Diff line
@@ -1651,6 +1651,34 @@ static void ext4_print_free_blocks(struct inode *inode)
	return;
}

/*
 * Check whether the cluster containing lblk has been delayed or allocated,
 * if not, it means we should reserve a cluster when add delalloc, return 1,
 * otherwise return 0 or error code.
 */
static int ext4_da_check_clu_allocated(struct inode *inode, ext4_lblk_t lblk,
				       bool *allocated)
{
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	int ret;

	*allocated = false;
	if (ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk))
		return 0;

	if (ext4_es_scan_clu(inode, &ext4_es_is_mapped, lblk))
		goto allocated;

	ret = ext4_clu_mapped(inode, EXT4_B2C(sbi, lblk));
	if (ret < 0)
		return ret;
	if (ret == 0)
		return 1;
allocated:
	*allocated = true;
	return 0;
}

/*
 * ext4_insert_delayed_block - adds a delayed block to the extents status
 *                             tree, incrementing the reserved cluster/block
@@ -1684,23 +1712,13 @@ static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
		if (ret != 0)   /* ENOSPC */
			return ret;
	} else {   /* bigalloc */
		if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
			if (!ext4_es_scan_clu(inode,
					      &ext4_es_is_mapped, lblk)) {
				ret = ext4_clu_mapped(inode,
						      EXT4_B2C(sbi, lblk));
		ret = ext4_da_check_clu_allocated(inode, lblk, &allocated);
		if (ret < 0)
			return ret;
				if (ret == 0) {
		if (ret > 0) {
			ret = ext4_da_reserve_space(inode, 1);
			if (ret != 0)   /* ENOSPC */
				return ret;
				} else {
					allocated = true;
				}
			} else {
				allocated = true;
			}
		}
	}