Received: by 2002:a05:7412:3210:b0:e2:908c:2ebd with SMTP id eu16csp499451rdb; Thu, 31 Aug 2023 16:16:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFP4Rol5VdDcXkwdTRWQVcCYLEU1b0Bo11y8srRYKiHOpxXbGXwQ7lhVFGPytI7Xych7dUz X-Received: by 2002:a19:504e:0:b0:500:b7ed:105a with SMTP id z14-20020a19504e000000b00500b7ed105amr488047lfj.29.1693523797965; Thu, 31 Aug 2023 16:16:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693523797; cv=none; d=google.com; s=arc-20160816; b=DBojG9wU6JXHJro6CJYjkflCRx/sWLyXijjVIB3JnKU/4PosHIxCSyoQXLnoUVhKbh Ch2zVtH1zyH0xFsL3ROcakPgrhij1Q4c5NJJ/Y9a4PBtD5/oeDN6R/zLflI37gOdNVW5 qYzpVn3tPx9VyWCQyUnf+F1G3OrOF78NK/5OJmSj9rMnNGQWRanmKfqn6cAXIvvCfO1m LeNirLUAIJnngJLrSRf911vb9AfKOkpc5Jzvl5wfb7qXqp/b5/KFfKB7dtme1uwlQTCJ f5mmhe6uGJ9QJCktsY6VE48FvPCsnwvwnAHTtCxx+WwpK7NJrZDye/w30LOss6nx7Yct dtNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:to:subject; bh=y5YIC2sqbNDB/Agl/cQccsF0fx7+dZalgfWXQ/VhveQ=; fh=JaHQ2wEV+57F2ViXFx3r3KoROlRM9Ph/xpqQAZp/KQo=; b=B91/epvWkCnlQrsKBWSXlbCPvMHtR9V7ItUPAgk2pcHJjhr/g0D71wwQ8Xe0K7nKVl inTYOCVd0/aPCntuSTVYCJUicpXmUN4oPQSCwr2FuK6RdCrVo/QWOfMC2LKZk7euT0cw aIhI8bzHHMTB5waOMMlYRbbceTt7pD19QEfOBHGScNEMLGsR7ky0XLEyEi9p5cjn3Eoo ax/xp03MtrcNgWP7qPJt4Ut+XcIMPfZ6BbsBmpK9ICbUpZLBBbrOZvsYKA+WfGAPhHV2 XQQkVnTDYPhJeRO6fVo2bTaKdcenPYh8hz7ye0fUj3y7VJM9bA/AkNxsvPAiSM8FbsqY rL1w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a26-20020aa7d75a000000b00529fbe92dbdsi1680312eds.124.2023.08.31.16.16.30; Thu, 31 Aug 2023 16:16:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346388AbjHaNnB (ORCPT + 27 others); Thu, 31 Aug 2023 09:43:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346396AbjHaNmc (ORCPT ); Thu, 31 Aug 2023 09:42:32 -0400 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C88FCE5A; Thu, 31 Aug 2023 06:42:27 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4Rc2Qj3jwwz4f3jJM; Thu, 31 Aug 2023 21:42:21 +0800 (CST) Received: from [10.174.178.129] (unknown [10.174.178.129]) by APP2 (Coremail) with SMTP id Syh0CgAHc2e_mPBkMJAMCA--.35476S2; Thu, 31 Aug 2023 21:42:25 +0800 (CST) Subject: Re: [PATCH v6 01/11] ext4: factor out codes to update block bitmap and group descriptor on disk from ext4_mb_mark_bb To: Ritesh Harjani , tytso@mit.edu, adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org References: <87sf7z8k8d.fsf@doe.com> From: Kemeng Shi Message-ID: Date: Thu, 31 Aug 2023 21:42:23 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: <87sf7z8k8d.fsf@doe.com> Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 7bit X-CM-TRANSID: Syh0CgAHc2e_mPBkMJAMCA--.35476S2 X-Coremail-Antispam: 1UD129KBjvJXoWxtryrGw4kCrWDAF1rKryfJFb_yoWfCr43pr yDAF1DCF43JFnruF47Z3y2q3WfKw18KF1kGryfGa4YkF9Iyr95ZF4xKFyFkF9IyFsrAFna vF1Yyrsrur4FkrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyKb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I 0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lc7I2V7IY0VAS07AlzVAYIcxG8wCF04k20xvY0x0E wIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E74 80Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0 I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04 k26cxKx2IYs7xG6rW3Jr0E3s1lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY 1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxUzsqWUUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org on 8/31/2023 8:33 PM, Ritesh Harjani wrote: > Kemeng Shi writes: > > Hello Kemeng, > >> There are several reasons to add a general function to update block >> bitmap and group descriptor on disk: > > ... named ext4_mb_mark_context() > >> 1. pair behavior of alloc/free bits. For example, >> ext4_mb_new_blocks_simple will update free_clusters in struct flex_groups >> in ext4_mb_mark_bb while ext4_free_blocks_simple forgets this. >> 2. remove repeat code to read from disk, update and write back to disk. >> 3. reduce future unit test mocks to catch real IO to update structure >> on disk. >> >> Signed-off-by: Kemeng Shi >> Reviewed-by: Ojaswin Mujoo >> --- >> fs/ext4/mballoc.c | 169 +++++++++++++++++++++++++++------------------- >> 1 file changed, 99 insertions(+), 70 deletions(-) >> >> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c >> index c91db9f57524..e2be572deb75 100644 >> --- a/fs/ext4/mballoc.c >> +++ b/fs/ext4/mballoc.c >> @@ -3952,6 +3952,100 @@ void ext4_exit_mballoc(void) >> ext4_groupinfo_destroy_slabs(); >> } >> >> +/* >> + * Collect global setting to reduce the number of variable passing to >> + * ext4_mb_mark_context. Pass target group blocks range directly to >> + * reuse the prepared global setting for multiple block ranges and >> + * to show clearly the specific block range will be marked. >> + */ >> +struct ext4_mark_context { >> + struct super_block *sb; >> + int state; >> +}; > > This structure definition does not reflect of it's naming. > Why can't we also add cblk & clen, flags to it? > > I think the idea of defining a new function named > ext4_mb_prepare_mark_context() was that we can prepare "struct ext4_mark_context" > with different cblk, clen & flags arguments for cases where we might > have to call ext4_mb_mark_context() more than once in the same function > or call ext4_mb_mark_context() anywhere but at the start of the function. > > As I see it in the current series, we are calling > ext4_mb_prepare_mark_context() at the start of every function. Just for > this purpose we don't need an extra function, right? That we can directly do > at the time of declaring a structure variable itself (like how you did > in previous version) > Hi Ritesh, thanks for reply. The ext4_mark_context structure aims to reduce variable passing to ext4_mb_mark_context. If we have to prepare a lot member in ext4_mb_prepare_mark_context, then too many variables issue occurs in ext4_mb_prepare_mark_context. The name of ext4_mark_context maybe not proper. Actually I want a structure to collect information which is not strongly relevant to mark blk bits. In this way, we can initialize them at beginning of function, then there is no need to pay attention to them or to pass them respectively in each call to ext4_mb_mark_context. Instead, we foucus on the useful information called with ext4_mb_mark_context. This design also achive the goal to define ext4_mb_mark_context once for multiple use in the same function as ext4_mark_context unlikely changes after initialization at beginning. > What do you think of the approach where we add cblk, clen & flags > variables to ext4_mark_context()? Do you see any problems/difficulties > with that design? > The providing desgin looks good to me. Please let me konw if you still perfre this and I will do this in next version. Thanks! >> + >> +static inline void ext4_mb_prepare_mark_context(struct ext4_mark_context *mc, >> + struct super_block *sb, >> + int state) >> +{ >> + mc->sb = sb; >> + mc->state = state; >> +} >> + >> +static int >> +ext4_mb_mark_context(struct ext4_mark_context *mc, ext4_group_t group, >> + ext4_grpblk_t blkoff, ext4_grpblk_t len) >> +{ >> + struct super_block *sb = mc->sb; >> + struct ext4_sb_info *sbi = EXT4_SB(sb); >> + struct buffer_head *bitmap_bh = NULL; >> + struct ext4_group_desc *gdp; >> + struct buffer_head *gdp_bh; >> + int err; >> + unsigned int i, already, changed; >> + >> + bitmap_bh = ext4_read_block_bitmap(sb, group); >> + if (IS_ERR(bitmap_bh)) >> + return PTR_ERR(bitmap_bh); >> + >> + err = -EIO; >> + gdp = ext4_get_group_desc(sb, group, &gdp_bh); >> + if (!gdp) >> + goto out_err; >> + >> + ext4_lock_group(sb, group); >> + if (ext4_has_group_desc_csum(sb) && >> + (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { >> + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); >> + ext4_free_group_clusters_set(sb, gdp, >> + ext4_free_clusters_after_init(sb, group, gdp)); >> + } >> + >> + already = 0; >> + for (i = 0; i < len; i++) >> + if (mb_test_bit(blkoff + i, bitmap_bh->b_data) == >> + mc->state) >> + already++; >> + changed = len - already; >> + >> + if (mc->state) { >> + mb_set_bits(bitmap_bh->b_data, blkoff, len); >> + ext4_free_group_clusters_set(sb, gdp, >> + ext4_free_group_clusters(sb, gdp) - changed); >> + } else { >> + mb_clear_bits(bitmap_bh->b_data, blkoff, len); >> + ext4_free_group_clusters_set(sb, gdp, >> + ext4_free_group_clusters(sb, gdp) + changed); >> + } >> + >> + ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh); >> + ext4_group_desc_csum_set(sb, group, gdp); >> + ext4_unlock_group(sb, group); >> + >> + if (sbi->s_log_groups_per_flex) { >> + ext4_group_t flex_group = ext4_flex_group(sbi, group); >> + struct flex_groups *fg = sbi_array_rcu_deref(sbi, >> + s_flex_groups, flex_group); >> + >> + if (mc->state) >> + atomic64_sub(changed, &fg->free_clusters); >> + else >> + atomic64_add(changed, &fg->free_clusters); >> + } >> + >> + err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh); >> + if (err) >> + goto out_err; >> + err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh); >> + if (err) >> + goto out_err; >> + >> + sync_dirty_buffer(bitmap_bh); >> + sync_dirty_buffer(gdp_bh); >> + >> +out_err: >> + brelse(bitmap_bh); >> + return err; >> +} >> >> /* >> * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps >> @@ -4078,16 +4172,14 @@ ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac, >> void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block, >> int len, int state) >> { >> - struct buffer_head *bitmap_bh = NULL; >> - struct ext4_group_desc *gdp; >> - struct buffer_head *gdp_bh; >> + struct ext4_mark_context mc; >> struct ext4_sb_info *sbi = EXT4_SB(sb); >> ext4_group_t group; >> ext4_grpblk_t blkoff; >> - int i, err = 0; >> - int already; >> - unsigned int clen, clen_changed, thisgrp_len; >> + int err = 0; >> + unsigned int clen, thisgrp_len; >> >> + ext4_mb_prepare_mark_context(&mc, sb, state); >> while (len > 0) { >> ext4_get_group_no_and_offset(sb, block, &group, &blkoff); >> >> @@ -4107,80 +4199,17 @@ void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block, >> ext4_error(sb, "Marking blocks in system zone - " >> "Block = %llu, len = %u", >> block, thisgrp_len); >> - bitmap_bh = NULL; >> - break; >> - } >> - >> - bitmap_bh = ext4_read_block_bitmap(sb, group); >> - if (IS_ERR(bitmap_bh)) { >> - err = PTR_ERR(bitmap_bh); >> - bitmap_bh = NULL; >> break; >> } >> >> - err = -EIO; >> - gdp = ext4_get_group_desc(sb, group, &gdp_bh); >> - if (!gdp) >> - break; >> - >> - ext4_lock_group(sb, group); >> - already = 0; >> - for (i = 0; i < clen; i++) >> - if (!mb_test_bit(blkoff + i, bitmap_bh->b_data) == >> - !state) >> - already++; >> - >> - clen_changed = clen - already; >> - if (state) >> - mb_set_bits(bitmap_bh->b_data, blkoff, clen); >> - else >> - mb_clear_bits(bitmap_bh->b_data, blkoff, clen); >> - if (ext4_has_group_desc_csum(sb) && >> - (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) { >> - gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT); >> - ext4_free_group_clusters_set(sb, gdp, >> - ext4_free_clusters_after_init(sb, group, gdp)); >> - } >> - if (state) >> - clen = ext4_free_group_clusters(sb, gdp) - clen_changed; >> - else >> - clen = ext4_free_group_clusters(sb, gdp) + clen_changed; >> - >> - ext4_free_group_clusters_set(sb, gdp, clen); >> - ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh); >> - ext4_group_desc_csum_set(sb, group, gdp); >> - >> - ext4_unlock_group(sb, group); >> - >> - if (sbi->s_log_groups_per_flex) { >> - ext4_group_t flex_group = ext4_flex_group(sbi, group); >> - struct flex_groups *fg = sbi_array_rcu_deref(sbi, >> - s_flex_groups, flex_group); >> - >> - if (state) >> - atomic64_sub(clen_changed, &fg->free_clusters); >> - else >> - atomic64_add(clen_changed, &fg->free_clusters); >> - >> - } >> - >> - err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh); >> - if (err) >> - break; >> - sync_dirty_buffer(bitmap_bh); >> - err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh); >> - sync_dirty_buffer(gdp_bh); >> + err = ext4_mb_mark_context(&mc, group, blkoff, clen); >> if (err) >> break; >> >> block += thisgrp_len; >> len -= thisgrp_len; >> - brelse(bitmap_bh); >> BUG_ON(len < 0); >> } >> - >> - if (err) >> - brelse(bitmap_bh); >> } >> >> /* >> -- >> 2.30.0 > > > -ritesh >