Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp78082rdb; Fri, 5 Jan 2024 03:26:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6uUo5/Rx47QWWJ24NkjFV5BkpbdthhfMlz8MJ4Lhd9mDTy/GPBRb4Ha88F5C4NsJbQTen X-Received: by 2002:a50:c04a:0:b0:556:6b8e:e6cc with SMTP id u10-20020a50c04a000000b005566b8ee6ccmr1933725edd.35.1704453959803; Fri, 05 Jan 2024 03:25:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704453959; cv=none; d=google.com; s=arc-20160816; b=RJzcsulSREszZA7urTrd2avpEeVHc8vFPdiCX1bHyHX526s4P6VtB4aAGzc2ivqxtp RhGM3Qmlx2BDylyYpQhNm/O4a8gpb4Ukf05vQJ5gV8i5cOZMDcE/ngpI8zaZECuaYqaO kXBqHT0LOHGFIgOBw3cvnNc3DLicvvFnS8zyOY2ae1GhuCWaeS5Y12XeD3xMcjVQgh4C 2KhBNQXVeDBdRfakbu3opGOSEvGlpzFxcUD1UVgY5N0C4/F9q6/Ct2CGDWEMY+2RPF2v aeqJ9j54PdjR9f/lK0aTDWE6pJpLQyp8vNUl+D9hbrrNs/ZCckPWX98R705CfCbRfbyg 6OQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject; bh=qUUEtYhD6dqo+je9sZxa/5ChOCIoTcwHRK4U47seH0Y=; fh=Gjln/ziIfA03BlwpZtQZsmawb37JaPmMlXMdl0Zbruo=; b=xfDuUzfRSne1SOg1BJ9yJD10gUdggap0BKsNU2HOwE4rw3/U+qo3XYDC6a6iYAu3Ym s5pLQlFVkKTNWp1aZGaYvM5SuSNHG/7l7zSCDT3xW9nZx14JKSYUAviM3X7Nntu4nuAW WGX7MMH6CEQlFTBtbegplnJby2u4WyxrUl+PyqICv6BoNcA+wZM4ZNCPrsTnaxt0RPhE zDNscsgup6fs2a7J7XDkxLwHwbWND27lQF8gqJIOIm3OspEm+BVwpRL9P/tiZbDilqmT Lk7QQ2TNlUgw9+gre4GW5ieg+wvfWEJuBEhogdT3mBg1dSAg7QLQ+JD0PdZvHsS2GMMJ S7tg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-729-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-ext4+bounces-729-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id cy13-20020a0564021c8d00b00550f590b413si566679edb.647.2024.01.05.03.25.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 03:25:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-729-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-729-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-ext4+bounces-729-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id B718C1F25CD6 for ; Fri, 5 Jan 2024 11:17:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8020528DDD; Fri, 5 Jan 2024 11:17:21 +0000 (UTC) X-Original-To: linux-ext4@vger.kernel.org Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52D2A28E12 for ; Fri, 5 Jan 2024 11:17:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4T61Bc6j8mz4f3k64 for ; Fri, 5 Jan 2024 19:17:12 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id BBFC71A0483 for ; Fri, 5 Jan 2024 19:17:14 +0800 (CST) Received: from [10.174.176.34] (unknown [10.174.176.34]) by APP1 (Coremail) with SMTP id cCh0CgBnaQ855ZdlNO1gFg--.51224S3; Fri, 05 Jan 2024 19:17:14 +0800 (CST) Subject: Re: [PATCH v3 3/6] ext4: correct the hole length returned by ext4_map_blocks() To: Jan Kara Cc: linux-ext4@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, yi.zhang@huawei.com, chengzhihao1@huawei.com, yukuai3@huawei.com References: <20240105033018.1665752-1-yi.zhang@huaweicloud.com> <20240105033018.1665752-4-yi.zhang@huaweicloud.com> <20240105101723.gl5ew2mkhtn4nyyg@quack3> From: Zhang Yi Message-ID: Date: Fri, 5 Jan 2024 19:17:13 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: <20240105101723.gl5ew2mkhtn4nyyg@quack3> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-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xhdqjqx5xdzvxpfor3voofrz/ On 2024/1/5 18:17, Jan Kara wrote: > On Fri 05-01-24 11:30:15, Zhang Yi wrote: >> From: Zhang Yi >> >> In ext4_map_blocks(), if we can't find a range of mapping in the >> extents cache, we are calling ext4_ext_map_blocks() to search the real >> path and ext4_ext_determine_hole() to determine the hole range. But if >> the querying range was partially or completely overlaped by a delalloc >> extent, we can't find it in the real extent path, so the returned hole >> length could be incorrect. >> >> Fortunately, ext4_ext_put_gap_in_cache() have already handle delalloc >> extent, but it searches start from the expanded hole_start, doesn't >> start from the querying range, so the delalloc extent found could not be >> the one that overlaped the querying range, plus, it also didn't adjust >> the hole length. Let's just remove ext4_ext_put_gap_in_cache(), handle >> delalloc and insert adjusted hole extent in ext4_ext_determine_hole(). >> >> Signed-off-by: Zhang Yi >> Suggested-by: Jan Kara > > Thanks! Looks good. Feel free to add: > > Reviewed-by: Jan Kara > Thanks a lot for your review! Yi. > >> --- >> fs/ext4/extents.c | 111 +++++++++++++++++++++++++++++----------------- >> 1 file changed, 70 insertions(+), 41 deletions(-) >> >> diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c >> index d5efe076d3d3..e0b7e48c4c67 100644 >> --- a/fs/ext4/extents.c >> +++ b/fs/ext4/extents.c >> @@ -2229,7 +2229,7 @@ static int ext4_fill_es_cache_info(struct inode *inode, >> >> >> /* >> - * ext4_ext_determine_hole - determine hole around given block >> + * ext4_ext_find_hole - find hole around given block according to the given path >> * @inode: inode we lookup in >> * @path: path in extent tree to @lblk >> * @lblk: pointer to logical block around which we want to determine hole >> @@ -2241,9 +2241,9 @@ static int ext4_fill_es_cache_info(struct inode *inode, >> * The function returns the length of a hole starting at @lblk. We update @lblk >> * to the beginning of the hole if we managed to find it. >> */ >> -static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, >> - struct ext4_ext_path *path, >> - ext4_lblk_t *lblk) >> +static ext4_lblk_t ext4_ext_find_hole(struct inode *inode, >> + struct ext4_ext_path *path, >> + ext4_lblk_t *lblk) >> { >> int depth = ext_depth(inode); >> struct ext4_extent *ex; >> @@ -2270,30 +2270,6 @@ static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, >> return len; >> } >> >> -/* >> - * ext4_ext_put_gap_in_cache: >> - * calculate boundaries of the gap that the requested block fits into >> - * and cache this gap >> - */ >> -static void >> -ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, >> - ext4_lblk_t hole_len) >> -{ >> - struct extent_status es; >> - >> - ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, >> - hole_start + hole_len - 1, &es); >> - if (es.es_len) { >> - /* There's delayed extent containing lblock? */ >> - if (es.es_lblk <= hole_start) >> - return; >> - hole_len = min(es.es_lblk - hole_start, hole_len); >> - } >> - ext_debug(inode, " -> %u:%u\n", hole_start, hole_len); >> - ext4_es_insert_extent(inode, hole_start, hole_len, ~0, >> - EXTENT_STATUS_HOLE); >> -} >> - >> /* >> * ext4_ext_rm_idx: >> * removes index from the index block. >> @@ -4062,6 +4038,69 @@ static int get_implied_cluster_alloc(struct super_block *sb, >> return 0; >> } >> >> +/* >> + * Determine hole length around the given logical block, first try to >> + * locate and expand the hole from the given @path, and then adjust it >> + * if it's partially or completely converted to delayed extents, insert >> + * it into the extent cache tree if it's indeed a hole, finally return >> + * the length of the determined extent. >> + */ >> +static ext4_lblk_t ext4_ext_determine_insert_hole(struct inode *inode, >> + struct ext4_ext_path *path, >> + ext4_lblk_t lblk) >> +{ >> + ext4_lblk_t hole_start, len; >> + struct extent_status es; >> + >> + hole_start = lblk; >> + len = ext4_ext_find_hole(inode, path, &hole_start); >> +again: >> + ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, >> + hole_start + len - 1, &es); >> + if (!es.es_len) >> + goto insert_hole; >> + >> + /* >> + * There's a delalloc extent in the hole, handle it if the delalloc >> + * extent is in front of, behind and straddle the queried range. >> + */ >> + if (lblk >= es.es_lblk + es.es_len) { >> + /* >> + * The delalloc extent is in front of the queried range, >> + * find again from the queried start block. >> + */ >> + len -= lblk - hole_start; >> + hole_start = lblk; >> + goto again; >> + } else if (in_range(lblk, es.es_lblk, es.es_len)) { >> + /* >> + * The delalloc extent containing lblk, it must have been >> + * added after ext4_map_blocks() checked the extent status >> + * tree, adjust the length to the delalloc extent's after >> + * lblk. >> + */ >> + len = es.es_lblk + es.es_len - lblk; >> + return len; >> + } else { >> + /* >> + * The delalloc extent is partially or completely behind >> + * the queried range, update hole length until the >> + * beginning of the delalloc extent. >> + */ >> + len = min(es.es_lblk - hole_start, len); >> + } >> + >> +insert_hole: >> + /* Put just found gap into cache to speed up subsequent requests */ >> + ext_debug(inode, " -> %u:%u\n", hole_start, len); >> + ext4_es_insert_extent(inode, hole_start, len, ~0, EXTENT_STATUS_HOLE); >> + >> + /* Update hole_len to reflect hole size after lblk */ >> + if (hole_start != lblk) >> + len -= lblk - hole_start; >> + >> + return len; >> +} >> >> /* >> * Block allocation/map/preallocation routine for extents based files >> @@ -4179,22 +4218,12 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, >> * we couldn't try to create block if create flag is zero >> */ >> if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { >> - ext4_lblk_t hole_start, hole_len; >> + ext4_lblk_t len; >> >> - hole_start = map->m_lblk; >> - hole_len = ext4_ext_determine_hole(inode, path, &hole_start); >> - /* >> - * put just found gap into cache to speed up >> - * subsequent requests >> - */ >> - ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); >> + len = ext4_ext_determine_insert_hole(inode, path, map->m_lblk); >> >> - /* Update hole_len to reflect hole size after map->m_lblk */ >> - if (hole_start != map->m_lblk) >> - hole_len -= map->m_lblk - hole_start; >> map->m_pblk = 0; >> - map->m_len = min_t(unsigned int, map->m_len, hole_len); >> - >> + map->m_len = min_t(unsigned int, map->m_len, len); >> goto out; >> } >> >> -- >> 2.39.2 >>