From patchwork Sat Jan 27 01:58:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Yi X-Patchwork-Id: 13533905 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3722C47422 for ; Sat, 27 Jan 2024 02:03:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8134C6B009B; Fri, 26 Jan 2024 21:02:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C42A6B009C; Fri, 26 Jan 2024 21:02:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EB556B009E; Fri, 26 Jan 2024 21:02:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2DF1D6B009C for ; Fri, 26 Jan 2024 21:02:55 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0A1C78086A for ; Sat, 27 Jan 2024 02:02:55 +0000 (UTC) X-FDA: 81723442710.16.283D8D1 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by imf07.hostedemail.com (Postfix) with ESMTP id 812EE40015 for ; Sat, 27 Jan 2024 02:02:52 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706320973; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fN+CuPyasvpUZa0cOQv4KhVMd/nrmJogmtJKnzS8QfM=; b=ANzolhDuczR2fukDTWrD2HCvpAf2Vsr3Rump/DpAmgHRpPWDwK0k9iAkRkp1X+yvxgRr6b 3geE0nZDSKZ8xTQYCISXkMnxC/IJZ0Vi7tKFJQD2Zk46uvEPkLXJMwNUcYpgdTZloHqtiu R6On+m92n8vsYNEDIQ4EoUmyfrQXulc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706320973; a=rsa-sha256; cv=none; b=izaoV+coct0PbUmp93S3MiAcHyYvCctHtxA2Sn330YWRXY4InMzmZNdDx7CVZTG7es0Xsv fkqrT8RBDyNKrM227/vnNK/bVEP2RL1rGgXlkoGE7X2ibWdDlJCA8uXTVIi1zOAeAHfJbb Cu5tD5G/ZaMEl89Laj9+fXFl3JraHKg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.hostedemail.com: domain of yi.zhang@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=yi.zhang@huaweicloud.com; dmarc=none Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4TMHrg1zRfz4f3lwC for ; Sat, 27 Jan 2024 10:02:43 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 477D41A016E for ; Sat, 27 Jan 2024 10:02:47 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP1 (Coremail) with SMTP id cCh0CgAX5g40ZLRlGJtmCA--.7377S16; Sat, 27 Jan 2024 10:02:47 +0800 (CST) From: Zhang Yi To: linux-ext4@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@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, zokeefe@google.com, yi.zhang@huawei.com, yi.zhang@huaweicloud.com, chengzhihao1@huawei.com, yukuai3@huawei.com, wangkefeng.wang@huawei.com Subject: [RFC PATCH v3 12/26] ext4: factor out bh handles to ext4_da_get_block_prep() Date: Sat, 27 Jan 2024 09:58:11 +0800 Message-Id: <20240127015825.1608160-13-yi.zhang@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240127015825.1608160-1-yi.zhang@huaweicloud.com> References: <20240127015825.1608160-1-yi.zhang@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgAX5g40ZLRlGJtmCA--.7377S16 X-Coremail-Antispam: 1UD129KBjvJXoW3XF1rWr48Zw4ruF47JF1DJrb_yoW7Ary8p3 9xAF1rGr13Ww109a1ftr1UXF1fK3WqyrW7Cr93GryFy3s5JrnaqF1UAFySqF98trZ3Zrs8 WF45Kry8uF18GrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPI14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Gr1j6F4UJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr 0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUl 2NtUUUUU= X-CM-SenderInfo: d1lo6xhdqjqx5xdzvxpfor3voofrz/ X-Rspamd-Queue-Id: 812EE40015 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: f6axg1j7u8t7w1rj9hwgh9m1d1west34 X-HE-Tag: 1706320972-411143 X-HE-Meta: U2FsdGVkX19kIp75wyXT+CuYXyh7NXUs7LDOyAW7H085yj9fxKYlkKJFnI9+nKNHia8FR1hzcX7mp6KB8clkKouXS5oDW0ZcU6TgYcIoQYzV39+W09SThP+VFlhPHS+atCrBJs3z0ZrlpkHBQIcO7vilhfAMWrcNJAR1p6fxyPw8A7M8s3KNfdU7FzlWC++UVrzXU3gJuu/8FzCOAz0mNmWHQnVyQOXdNRlLwoZhc8dRB5MRBbqHEOIOxUSUHBGjmx6pGV2GL0GHkgubbkAWghm5zNTU/I7NUxieyb0d4/yL9VBWc6o4dZ6BXk+DYoN+ul3sln0lnQQGlzFkEC9JGy9EIhEmofelCEiVK17ozqZFTpLLS7s2TLWf+UiTfYABtrfxKZrUPFEMdBqgwzt8QkXwrF8GDoTGnSURI5Dxbm4t0n31qqwnzpBP3bQZyO7QCHWlSnEb0PTY7TAp4s7gfg3w+1IDi5i5cipFcVjMzrZuOWQxgO3cg0t/oo0/G5qURieusgx6ups2iW9RSmbJhbI3dhMiTAlk0TyC0MsqgX10JyKvRy1ep6H4sj28VtzQ4qPx11mltTelWWUt9gM1QvucbSW1epCrhagX/Bf+f/QMG4+WDrE10J+Kgam2cCtHe0pGoVw84vlb1+q8B0D4g2btGJ+W/84miLNjXofarTXYhXpDsdd9hiv45Tu2mxrIxFVCRcgACoc5ncg1HhSHRooc+MW7ig3e2JDz/bzhy4rc1R3ABkSOc0gl5SQgWJMxHiSpK+XCsrSCwd6eAEJW+pldTgi0v5vsqsVEEVVh+udDRenYSQBwmt+GK9Laa1Ew6JkUzZ+aVGpV56W5fzFHXbB3YloMSu8gISykYk8AITOvpqLVCGHsYfut7zSwk5WJpcUvijJRdK6UPDZeH7KXoVcJmAaXJN1UZrXu1/vxH+o/qQIo5WqFOi6ZNykDNDz9qqMZX/lQCbOO2H4a3Uq iYDGwvVs wRHd8017NBwua0wNCGUqojcE15O/32m75I4RGcx44TdqcaXGBfNiGUlvp1gRAdP99y2Gku2h5V2nqlbhIsQ4mL9Vkx8u+h2iVybTmO3qj9SXTdF+rZLwKutw6NOvKqgATycaZUu1sNVg1B5v3q1YR7EGeyN1F5NnCmFegCXCX2J5OHPhohG+QtEHaZW+5UtOL9UNG17SAVth+1A+RxZHjHbw3QPFxqz0YlumWFTAns09kbtwi4n4utSoAxttLDXMEpAp09R7mRiqh3kcCtbFZWAmKyDKuOhdM+pJfpULZiYsOXsw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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; }