Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6512692rdb; Tue, 2 Jan 2024 04:43:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IHyr0E8pI2fziWTzMbAsjo3UdvgIQ8AF99rw1pwLT2+89jS3eaIpKMoSjhECql0mszQLVim X-Received: by 2002:a05:6214:1c86:b0:680:99af:9d7 with SMTP id ib6-20020a0562141c8600b0068099af09d7mr9146404qvb.45.1704199435761; Tue, 02 Jan 2024 04:43:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704199435; cv=none; d=google.com; s=arc-20160816; b=UUo2L7yOFDhhAw06BjB5S+CO4ncDYQIWA5oby4i2/jyShyNAH/FZV8HS8iNuGP8oBX VPvXQbTX3NmpMRjjLvuDcU3fVGDEEiADBOkRc6KNlNLBmAn4aBq4A+Xaw0geiJfs2tPb YRpoMJ6H6dPGS+gbQ5qewGTzGBDNaFskgKBBhHdcYSLvC2FdZRnMHXkyqg6agEB6Izwb ZV3o31wzciFxfYNyJK96L5xlcBvY+aL9W261ab9VtuWJF1CAoiqXLrm6krYjjz+LuQcQ WL0ZPFHf1v9XVNdTlf28KKdGEsW3nEtjuLA+O//EywItsKoQQ4KTeUkjwYh65h9zgTQs KkvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=fN+CuPyasvpUZa0cOQv4KhVMd/nrmJogmtJKnzS8QfM=; fh=yWWoL3IRufgA/+2kSpO6uyfw9eN3ZOO7zvul0qbDPkA=; b=n0LLq2tMTaC5jW0HhmLmWKPo4fwWIUk/5xfFyach4vYeM6bZU8P6xu1zdpU/xSuBQw 8UcrQB/Rfht9jsLUkjZmn8/PDT/YoH6XlqXDcirzr4Z0cPsDvP4W3pVPVrUd39+S/VFE mlwfxZ9zae/bpD+9d1ftgca+4y+q7h4J6tcPm+9QVFeazLDlyXFV/1je29noxD4ITeFy urLav4osqbbE7SAaH8oz0OQcXNfVTgRWD8blLrPhvp9TFGXWHnAW1K6JmuaNimsLiM2C LgukTig2d4S8iLJNSF0l7K8Mt80iROtOkJF8daObRWCm8HT0vEb64xu2mPY5aYhufu0n B+Bg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-606-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id s5-20020a0ce305000000b0067f29c99880si5492152qvl.292.2024.01.02.04.43.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Jan 2024 04:43:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4+bounces-606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4+bounces-606-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-ext4+bounces-606-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 758FA1C21F9F for ; Tue, 2 Jan 2024 12:43:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B711714AB6; Tue, 2 Jan 2024 12:42:16 +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 8E3B3125CA; Tue, 2 Jan 2024 12:42:14 +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.235]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4T4CCy1l6Yz4f3nT7; Tue, 2 Jan 2024 20:42:06 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id D76491A017F; Tue, 2 Jan 2024 20:42:11 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgBnwUGUBJRl+EvDFQ--.31823S16; Tue, 02 Jan 2024 20:42:11 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, ritesh.list@gmail.com, hch@infradead.org, djwong@kernel.org, willy@infradead.org, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com, wangkefeng.wang@huawei.com Subject: [RFC PATCH v2 12/25] ext4: factor out bh handles to ext4_da_get_block_prep() Date: Tue, 2 Jan 2024 20:39:05 +0800 Message-Id: <20240102123918.799062-13-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240102123918.799062-1-yi.zhang@huaweicloud.com> References: <20240102123918.799062-1-yi.zhang@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:gCh0CgBnwUGUBJ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VW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUl 2NtUUUUU= X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ From: Zhang Yi Factor buffer_head related handles out from ext4_da_map_blocks() to ext4_da_get_block_prep(), and make ext4_da_map_blocks() always return 0 if success, we can distinguish delalloc map through EXT4_MAP_DELAYED flag. Signed-off-by: Zhang Yi --- fs/ext4/inode.c | 75 ++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 9ac9bb548a4c..9f9b1fce8da8 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -1690,48 +1690,37 @@ static int ext4_insert_delayed_blocks(struct inode *inode, ext4_lblk_t lblk, * time. This function looks up the requested blocks and sets the * buffer delay bit under the protection of i_data_sem. */ -static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, - struct ext4_map_blocks *map, - struct buffer_head *bh) +static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map) { struct extent_status es; int retval; - sector_t invalid_block = ~((sector_t) 0xffff); #ifdef ES_AGGRESSIVE_TEST struct ext4_map_blocks orig_map; memcpy(&orig_map, map, sizeof(*map)); #endif - if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) - invalid_block = ~0; - map->m_flags = 0; ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len, (unsigned long) map->m_lblk); /* Lookup extent status tree firstly */ - if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) { - retval = es.es_len - (iblock - es.es_lblk); - if (retval > map->m_len) - retval = map->m_len; - map->m_len = retval; - - if (ext4_es_is_hole(&es)) - goto add_delayed; + if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) { + retval = es.es_len - (map->m_lblk - es.es_lblk); + map->m_len = min_t(unsigned int, map->m_len, retval); /* * Delayed extent could be allocated by fallocate. * So we need to check it. */ - if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { - map_bh(bh, inode->i_sb, invalid_block); - set_buffer_new(bh); - set_buffer_delay(bh); + if (ext4_es_is_delonly(&es)) { + map->m_flags |= EXT4_MAP_DELAYED; return 0; } + if (ext4_es_is_hole(&es)) + goto add_delayed; - map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; + map->m_pblk = ext4_es_pblock(&es) + map->m_lblk - es.es_lblk; if (ext4_es_is_written(&es)) map->m_flags |= EXT4_MAP_MAPPED; else if (ext4_es_is_unwritten(&es)) @@ -1743,7 +1732,7 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); #endif if (ext4_es_is_delayed(&es) || ext4_es_is_written(&es)) - return retval; + return 0; down_read(&EXT4_I(inode)->i_data_sem); goto insert_extent; @@ -1775,6 +1764,7 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, WARN_ON(1); } insert_extent: + retval = 0; status = map->m_flags & EXT4_MAP_UNWRITTEN ? EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; if (status == EXTENT_STATUS_UNWRITTEN) @@ -1788,14 +1778,10 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, add_delayed: down_write(&EXT4_I(inode)->i_data_sem); + map->m_flags |= EXT4_MAP_DELAYED; retval = ext4_insert_delayed_blocks(inode, map->m_lblk, map->m_len); up_write(&EXT4_I(inode)->i_data_sem); - if (retval) - return retval; - map_bh(bh, inode->i_sb, invalid_block); - set_buffer_new(bh); - set_buffer_delay(bh); return retval; } @@ -1815,11 +1801,15 @@ int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, struct buffer_head *bh, int create) { struct ext4_map_blocks map; + sector_t invalid_block = ~((sector_t) 0xffff); int ret = 0; BUG_ON(create == 0); BUG_ON(bh->b_size != inode->i_sb->s_blocksize); + if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) + invalid_block = ~0; + map.m_lblk = iblock; map.m_len = 1; @@ -1828,22 +1818,29 @@ int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, * preallocated blocks are unmapped but should treated * the same as allocated blocks. */ - ret = ext4_da_map_blocks(inode, iblock, &map, bh); - if (ret <= 0) + ret = ext4_da_map_blocks(inode, &map); + if (ret < 0) return ret; - map_bh(bh, inode->i_sb, map.m_pblk); - ext4_update_bh_state(bh, map.m_flags); - - if (buffer_unwritten(bh)) { - /* A delayed write to unwritten bh should be marked - * new and mapped. Mapped ensures that we don't do - * get_block multiple times when we write to the same - * offset and new ensures that we do proper zero out - * for partial write. - */ + if (map.m_flags & EXT4_MAP_DELAYED) { + map_bh(bh, inode->i_sb, invalid_block); set_buffer_new(bh); - set_buffer_mapped(bh); + set_buffer_delay(bh); + } else { + map_bh(bh, inode->i_sb, map.m_pblk); + ext4_update_bh_state(bh, map.m_flags); + + if (buffer_unwritten(bh)) { + /* + * A delayed write to unwritten bh should be marked + * new and mapped. Mapped ensures that we don't do + * get_block multiple times when we write to the same + * offset and new ensures that we do proper zero out + * for partial write. + */ + set_buffer_new(bh); + set_buffer_mapped(bh); + } } return 0; } -- 2.39.2