From patchwork Mon Jul 24 14:22:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324873 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE680C001B0 for ; Mon, 24 Jul 2023 14:22:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231479AbjGXOWq (ORCPT ); Mon, 24 Jul 2023 10:22:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229667AbjGXOWq (ORCPT ); Mon, 24 Jul 2023 10:22:46 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C241B7 for ; Mon, 24 Jul 2023 07:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=xsBvUcQfoEWTfGOm5VUo3ESEXs5/PPyx0DWl0kQturM=; b=ivz5yECaHm/jbe3O6WrpsppCMA ytaYuZserA0UqIxVCE3FlfBXdM1ljCfoP6U/YZbO+pazdAaJHNxV+5NqFvoZkWx82Y0oidLI4UgpR Q029Z/TKcieX/BTWFDjqbPld0xPFZ7BJKQzL98Db6mFDBZMSTfEpDq/e25s6AOi7g4G9rF9r4wOPx 1RpCxqYYP6xmeSL6IO+ankY2RxJsiNCTm+Mxb3j1en8oc27abStbw6KJQXoqeM3Eb57H02JtwCieW +YZDq9O67nzHrBnnhhwLpjY2WarcvYSVMi8ovl8VOb7xsLS5oT4Povd/LW8LP1vgIIs8Sow2T8cwj OPdpLquA==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSR-004b9V-2M; Mon, 24 Jul 2023 14:22:43 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 1/6] btrfs: fix error handling when in a COW window in run_delalloc_nocow Date: Mon, 24 Jul 2023 07:22:38 -0700 Message-Id: <20230724142243.5742-2-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When run_delalloc_nocow has cow_start set to a value other than (u64)-1, it has delayed COW writeback pending behind cur_offset. When an error occurs in such a window, the range going back to cow_start and not just cur_offset needs to be unlocked, but only two error cases handle this correctly Move the code to handle unlock the COW range to the common error handling label and document the logic. To make things even more complicated, cow_file_range as called by fallback_to_cow will unlock the range it is operating on when it fails as well, so we need to reset cow_start right after caling fallback_to_cow instead of only when it succeeded. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 640e7dd26f6132..434f82fa4957d3 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2031,11 +2031,8 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, leaf = path->nodes[0]; if (path->slots[0] >= btrfs_header_nritems(leaf)) { ret = btrfs_next_leaf(root, path); - if (ret < 0) { - if (cow_start != (u64)-1) - cur_offset = cow_start; + if (ret < 0) goto error; - } if (ret > 0) break; leaf = path->nodes[0]; @@ -2098,13 +2095,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, nocow_args.start = cur_offset; ret = can_nocow_file_extent(path, &found_key, inode, &nocow_args); - if (ret < 0) { - if (cow_start != (u64)-1) - cur_offset = cow_start; + if (ret < 0) goto error; - } else if (ret == 0) { + if (ret == 0) goto out_check; - } ret = 0; bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); @@ -2135,9 +2129,9 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, if (cow_start != (u64)-1) { ret = fallback_to_cow(inode, locked_page, cow_start, found_key.offset - 1); + cow_start = (u64)-1; if (ret) goto error; - cow_start = (u64)-1; } nocow_end = cur_offset + nocow_args.num_bytes - 1; @@ -2216,6 +2210,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, if (cow_start != (u64)-1) { cur_offset = end; ret = fallback_to_cow(inode, locked_page, cow_start, end); + cow_start = (u64)-1; if (ret) goto error; } @@ -2224,6 +2219,13 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, if (nocow) btrfs_dec_nocow_writers(bg); + /* + * If an error happened while a COW region is outstanding, cur_offset + * needs to be reset to cow_start to ensure the COW region is unlocked + * as well. + */ + if (cow_start != (u64)-1) + cur_offset = cow_start; if (ret && cur_offset < end) extent_clear_unlock_delalloc(inode, cur_offset, end, locked_page, EXTENT_LOCKED | From patchwork Mon Jul 24 14:22:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324874 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6C0BC00528 for ; Mon, 24 Jul 2023 14:22:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231835AbjGXOWr (ORCPT ); Mon, 24 Jul 2023 10:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229883AbjGXOWq (ORCPT ); Mon, 24 Jul 2023 10:22:46 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F1CEBC for ; Mon, 24 Jul 2023 07:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=319R32b0U8OC06DymviRccGHCPYdZ117hoeWCv+aEeI=; b=wWtSKw708n5UK51iPjvd5MNAfA uyAlxk3/d4Y7rVGbnbO0S5g9pdPxF0PPFhdvqnM6tQCBEbrcX3NkNRMeTkgtS5uitmeo9cP8C4b1s 6pX+rpJip/VaXZSKWqOb7v/kl3qx1XJmEfElWLWa7WVw04A/1CC8F8XZQ8DvpJpNKegySfzoZP9uP 1UH+fnCo5c70roZxLtj1QBDOBt6DB1pH3LLkmHx4VyLVsYRDgqySI9Y/ox6/rlbJsq0v3iM1NI6w7 GUzVtvzm310Z9WjoFf/cedR0XhB4REJ2RwHxAfdHvn4u1D0gu5+R+IFm4mg2PYo3gJo9CBgFqFW3g EKU/oROA==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSR-004b9d-2w; Mon, 24 Jul 2023 14:22:43 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 2/6] btrfs: cleanup the COW fallback logic in run_delalloc_nocow Date: Mon, 24 Jul 2023 07:22:39 -0700 Message-Id: <20230724142243.5742-3-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Use the block group pointer used to track the outstanding NOCOW writes as a boolean to remove the duplicate nocow variable, and keep it contained in the main loop to simplify the logic. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 434f82fa4957d3..212aca4eea442b 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1976,8 +1976,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, int ret; bool check_prev = true; u64 ino = btrfs_ino(inode); - struct btrfs_block_group *bg; - bool nocow = false; struct can_nocow_file_extent_args nocow_args = { 0 }; path = btrfs_alloc_path(); @@ -1995,6 +1993,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, nocow_args.writeback_path = true; while (1) { + struct btrfs_block_group *nocow_bg = NULL; struct btrfs_ordered_extent *ordered; struct btrfs_key found_key; struct btrfs_file_extent_item *fi; @@ -2005,8 +2004,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, int extent_type; bool is_prealloc; - nocow = false; - ret = btrfs_lookup_file_extent(NULL, root, path, ino, cur_offset, 0); if (ret < 0) @@ -2065,7 +2062,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, if (found_key.offset > cur_offset) { extent_end = found_key.offset; extent_type = 0; - goto out_check; + goto must_cow; } /* @@ -2098,18 +2095,19 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, if (ret < 0) goto error; if (ret == 0) - goto out_check; + goto must_cow; ret = 0; - bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); - if (bg) - nocow = true; -out_check: - /* - * If nocow is false then record the beginning of the range - * that needs to be COWed - */ - if (!nocow) { + nocow_bg = btrfs_inc_nocow_writers(fs_info, nocow_args.disk_bytenr); + if (!nocow_bg) { +must_cow: + /* + * If we can't perform NOCOW writeback for the range, + * then record the beginning of the range that needs to + * be COWed. It will be written out before the next + * NOCOW range if we find one, or when exiting this + * loop. + */ if (cow_start == (u64)-1) cow_start = cur_offset; cur_offset = extent_end; @@ -2130,8 +2128,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, ret = fallback_to_cow(inode, locked_page, cow_start, found_key.offset - 1); cow_start = (u64)-1; - if (ret) + if (ret) { + btrfs_dec_nocow_writers(nocow_bg); goto error; + } } nocow_end = cur_offset + nocow_args.num_bytes - 1; @@ -2148,6 +2148,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, ram_bytes, BTRFS_COMPRESS_NONE, BTRFS_ORDERED_PREALLOC); if (IS_ERR(em)) { + btrfs_dec_nocow_writers(nocow_bg); ret = PTR_ERR(em); goto error; } @@ -2161,6 +2162,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, ? (1 << BTRFS_ORDERED_PREALLOC) : (1 << BTRFS_ORDERED_NOCOW), BTRFS_COMPRESS_NONE); + btrfs_dec_nocow_writers(nocow_bg); if (IS_ERR(ordered)) { if (is_prealloc) { btrfs_drop_extent_map_range(inode, cur_offset, @@ -2170,11 +2172,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, goto error; } - if (nocow) { - btrfs_dec_nocow_writers(bg); - nocow = false; - } - if (btrfs_is_data_reloc_root(root)) /* * Error handled later, as we must prevent @@ -2215,10 +2212,10 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, goto error; } -error: - if (nocow) - btrfs_dec_nocow_writers(bg); + btrfs_free_path(path); + return 0; +error: /* * If an error happened while a COW region is outstanding, cur_offset * needs to be reset to cow_start to ensure the COW region is unlocked @@ -2226,7 +2223,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, */ if (cow_start != (u64)-1) cur_offset = cow_start; - if (ret && cur_offset < end) + if (cur_offset < end) extent_clear_unlock_delalloc(inode, cur_offset, end, locked_page, EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DEFRAG | From patchwork Mon Jul 24 14:22:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324878 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99D58C04A6A for ; Mon, 24 Jul 2023 14:22:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231695AbjGXOWv (ORCPT ); Mon, 24 Jul 2023 10:22:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231821AbjGXOWr (ORCPT ); Mon, 24 Jul 2023 10:22:47 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B856BC for ; Mon, 24 Jul 2023 07:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=NxrOayYA2ZaGKhdQ6VW3bI8ywI3ep4NRjpzmsZzHy4g=; b=K1SIkh2r0EKdtcBk0tomo8dU35 5WrUy7Eecky4nivGhr2knARE/yRj6VAz+IHboGKdy3Cbmak0X7RjampU6anFY0sT62FolGugv7gcf 5LSnMi86z8awRN0YY5brNYAoTeQlAI5/cUvPyVPo3Q65MuSbiQxhiaVKKvu95e6fhVi9xQm+yvDxm tOdRclN2jKjwVV8naCO05BNZjc9+/5m2aNplsV3kvy/4faDrb6sHkYD6QhX57C1QYPaBR97Odr99y dLHoji6Hd6PNbP80ZHiUBJRPTjPmTVuRqBXNzP8JQGnBwLl3DOd2CO0j4WshSXmUXbuyFpo2VAiXN AVFohvNw==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSS-004b9h-0J; Mon, 24 Jul 2023 14:22:44 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 3/6] btrfs: consolidate the error handling in run_delalloc_nocow Date: Mon, 24 Jul 2023 07:22:40 -0700 Message-Id: <20230724142243.5742-4-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Share the calls to extent_clear_unlock_delalloc for btrfs_path allocation failure handling and the normal exit path. This relies on btrfs_free_path ignoring a NULL pointer, and the initialization of cur_offset to start at the beginning of the function. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 212aca4eea442b..2d465b50c228ed 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1973,21 +1973,14 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, struct btrfs_path *path; u64 cow_start = (u64)-1; u64 cur_offset = start; - int ret; + int ret = -ENOMEM; bool check_prev = true; u64 ino = btrfs_ino(inode); struct can_nocow_file_extent_args nocow_args = { 0 }; path = btrfs_alloc_path(); - if (!path) { - extent_clear_unlock_delalloc(inode, start, end, locked_page, - EXTENT_LOCKED | EXTENT_DELALLOC | - EXTENT_DO_ACCOUNTING | - EXTENT_DEFRAG, PAGE_UNLOCK | - PAGE_START_WRITEBACK | - PAGE_END_WRITEBACK); - return -ENOMEM; - } + if (!path) + goto error; nocow_args.end = end; nocow_args.writeback_path = true; From patchwork Mon Jul 24 14:22:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324875 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96500C001DF for ; Mon, 24 Jul 2023 14:22:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231959AbjGXOWt (ORCPT ); Mon, 24 Jul 2023 10:22:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231532AbjGXOWr (ORCPT ); Mon, 24 Jul 2023 10:22:47 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A36A3CF for ; Mon, 24 Jul 2023 07:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=jgeJ4CUi9vRUabJkqpzFSJAbNDBwazU6jwUOiaI5sFk=; b=oxbMsu4QpTPtN5sppEny45UCC5 Vqn0bO9QCA/G+MY+1R0uswRmVZCzt//0tSm3zXcOGIe/yuuumQ3M52jn6q2ergfiLSystHOKekRKd 7b2vOhvtebBM98ypLC0b5ShT8LxlcNHx9rYnKJbzuipoIe//TpPGcogJ7xadP3LtKz6fDBTS4Nt9y CQ6itnjj9lxSD/9wK0QB4O4VcAKhynf/fcDqG14f0tByAecLfPTHbN2tnNE1QoScbLHrrP/fhtwZ4 DMoBtLpPutCkZxg9X+Om38exLlibVRNJO7ES5fxBkAwwIQWt4wngKyQ5UDQfUsVUCYNo03pmDtsHX 4xiDLGUQ==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSS-004b9l-0x; Mon, 24 Jul 2023 14:22:44 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 4/6] btrfs: move the !zoned assert into run_delalloc_cow Date: Mon, 24 Jul 2023 07:22:41 -0700 Message-Id: <20230724142243.5742-5-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Having the assert in the actual helper documents the pre-conditions much better than having it in the caller, so move it. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 2d465b50c228ed..92182e0d27fdb5 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1978,6 +1978,13 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, u64 ino = btrfs_ino(inode); struct can_nocow_file_extent_args nocow_args = { 0 }; + /* + * Normally on a zoned device we're only doing COW writes, but in case + * of relocation on a zoned filesystem serializes I/O so that we're only + * writing sequentially and can end up here as well. + */ + ASSERT(!btrfs_is_zoned(fs_info) || btrfs_is_data_reloc_root(root)); + path = btrfs_alloc_path(); if (!path) goto error; @@ -2257,14 +2264,6 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page start >= page_offset(locked_page) + PAGE_SIZE)); if (should_nocow(inode, start, end)) { - /* - * Normally on a zoned device we're only doing COW writes, but - * in case of relocation on a zoned filesystem we have taken - * precaution, that we're only writing sequentially. It's safe - * to use run_delalloc_nocow() here, like for regular - * preallocated inodes. - */ - ASSERT(!zoned || btrfs_is_data_reloc_root(inode->root)); ret = run_delalloc_nocow(inode, locked_page, start, end); goto out; } From patchwork Mon Jul 24 14:22:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324876 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D54DFC04A94 for ; Mon, 24 Jul 2023 14:22:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231304AbjGXOWt (ORCPT ); Mon, 24 Jul 2023 10:22:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231535AbjGXOWr (ORCPT ); Mon, 24 Jul 2023 10:22:47 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DC63D7 for ; Mon, 24 Jul 2023 07:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=ZJSGpxwxr/hSBv3Fud2DF+r+ppU6qxc6oTTz/Ybi4Ds=; b=N4KHwD++uorNpmcREM7d2m04mL 3AtVc0yKDBx1Gg4EwzlAiISsob0EvOzQzeumqlf7VNoFjUuC6tZSeknYiQATGSl4Ell5nQR42eyMZ Ik5NkklTqmh1rQisPB680Nh0cwLCUBzZ+/W//Azqdv30Qp0zcRBltNnoCsJ2GN7zGwnGNq53KAnZR oHz0AnI1ybBt8G2qdh3DgpXQVLAvCTDtUbUCQie82/dq5i8DhWSd/JU7hPz+YbjMgwhVuKhSVGh3s 38GBpJrz0f9nZIk5wC0ryghpoNQJbnkahmn1vh1i+LxBqfeXYaAB0Z9wzMMLMLWuIBcmEOUb8Yx2X FwpsC6hQ==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSS-004b9v-1W; Mon, 24 Jul 2023 14:22:44 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 5/6] btrfs: use nocow_end for the loop iteration in run_delalloc_cow Date: Mon, 24 Jul 2023 07:22:42 -0700 Message-Id: <20230724142243.5742-6-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org When run_delalloc_cow allocates an ordered extent for an actual NOCOW range, it uses the nocow_end variable calculated based on the current offset and the nocow_args.num_bytes value returned from can_nocow_file_extent for all the actual I/O, but the loop iteration then resets cur_offset to extent_end, which caused me a lot of confusion. It turns out that nocow_end is based of the minimum of the extent end and the range end, and thus actually works perfectly fine for the loop iteration, but using a different variable here from the actual I/O submission is horribly confusing and wasted some of my precious brain cells when train to understand the logic. Switch to using nocow_end adjusted by the the off by one to make it an exclusive range. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 92182e0d27fdb5..caaf2c002d795d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2187,7 +2187,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, EXTENT_CLEAR_DATA_RESV, PAGE_UNLOCK | PAGE_SET_ORDERED); - cur_offset = extent_end; + cur_offset = nocow_end + 1; /* * btrfs_reloc_clone_csums() error, now we're OK to call error From patchwork Mon Jul 24 14:22:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13324877 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 302CBC001B0 for ; Mon, 24 Jul 2023 14:22:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231532AbjGXOWu (ORCPT ); Mon, 24 Jul 2023 10:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231695AbjGXOWr (ORCPT ); Mon, 24 Jul 2023 10:22:47 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59FEAB7 for ; Mon, 24 Jul 2023 07:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=nmuhtZoGLRIjF7U6JdFSvOOnfV2Ls5UB4SJyqThOUok=; b=CYroLKFjtWidxe5EWLM82xWguC 57dwuifZQBOqkwMI6LNRe/7NCXqPK3aIIhxU9tp7IHFAZl231TKK0YoZ1h1lEhKGXqZObHB8OqLNX P2cJlFotW2Mg2zJqQ/vwZwmXomT+P0+c4x6jY9voQFLBsIxB4Jck/Is38yVI9d5QV8AzYkpZeeWLm /atjEmHWap7w/OCKvqW9TE2bt9VF4A3xTv+qeXfHdtxH608yMWG4PWrcDYErM0UOVHR/WhCsc5+Le 6FvXtZd4R/31jcrDpUtPqpy16VMrW+6/D8bFd2IIVV9Rkz3w4oeJ0hgk890EpHbNzMnEkLNs0JRqg fp5lO9Rw==; Received: from 67-207-104-238.static.wiline.com ([67.207.104.238] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1qNwSS-004bA4-2C; Mon, 24 Jul 2023 14:22:44 +0000 From: Christoph Hellwig To: Chris Mason , Josef Bacik , David Sterba Cc: linux-btrfs@vger.kernel.org Subject: [PATCH 6/6] btrfs: clone relocation checksums in btrfs_alloc_ordered_extent Date: Mon, 24 Jul 2023 07:22:43 -0700 Message-Id: <20230724142243.5742-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230724142243.5742-1-hch@lst.de> References: <20230724142243.5742-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Error handling for btrfs_reloc_clone_csums is a royal pain. That is not because btrfs_reloc_clone_csums does something particularly nasty: the only failure can come from looking up the csums in the csum tree - instead it is because btrfs_reloc_clone_csums is called after btrfs_alloc_ordered_extent because it adds those founds csums to the list in the ordred extent, and cleaning up after an ordered extent has been added to the lookup data structures is very cumbersome. Fix this by calling btrfs_reloc_clone_csums in btrfs_alloc_ordered_extent after allocting the ordered extents, but before making it reachable by the wider word and thus bypassing the complex ordered extent removal path entirely. Signed-off-by: Christoph Hellwig --- fs/btrfs/inode.c | 44 ----------------------------------------- fs/btrfs/ordered-data.c | 24 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 46 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index caaf2c002d795d..7864cae3ee2094 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1435,26 +1435,6 @@ static noinline int cow_file_range(struct btrfs_inode *inode, ret = PTR_ERR(ordered); goto out_drop_extent_cache; } - - if (btrfs_is_data_reloc_root(root)) { - ret = btrfs_reloc_clone_csums(ordered); - - /* - * Only drop cache here, and process as normal. - * - * We must not allow extent_clear_unlock_delalloc() - * at out_unlock label to free meta of this ordered - * extent, as its meta should be freed by - * btrfs_finish_ordered_io(). - * - * So we must continue until @start is increased to - * skip current ordered extent. - */ - if (ret) - btrfs_drop_extent_map_range(inode, start, - start + ram_size - 1, - false); - } btrfs_put_ordered_extent(ordered); btrfs_dec_block_group_reservations(fs_info, ins.objectid); @@ -1481,14 +1461,6 @@ static noinline int cow_file_range(struct btrfs_inode *inode, alloc_hint = ins.objectid + ins.offset; start += cur_alloc_size; extent_reserved = false; - - /* - * btrfs_reloc_clone_csums() error, since start is increased - * extent_clear_unlock_delalloc() at out_unlock label won't - * free metadata of current ordered extent, we're OK to exit. - */ - if (ret) - goto out_unlock; } done: if (done_offset) @@ -2171,14 +2143,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, ret = PTR_ERR(ordered); goto error; } - - if (btrfs_is_data_reloc_root(root)) - /* - * Error handled later, as we must prevent - * extent_clear_unlock_delalloc() in error handler - * from freeing metadata of created ordered extent. - */ - ret = btrfs_reloc_clone_csums(ordered); btrfs_put_ordered_extent(ordered); extent_clear_unlock_delalloc(inode, cur_offset, nocow_end, @@ -2188,14 +2152,6 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, PAGE_UNLOCK | PAGE_SET_ORDERED); cur_offset = nocow_end + 1; - - /* - * btrfs_reloc_clone_csums() error, now we're OK to call error - * handler, as metadata for created ordered extent will only - * be freed by btrfs_finish_ordered_io(). - */ - if (ret) - goto error; if (cur_offset > end) break; } diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c index 109e80ed25b669..caa5dbf48db572 100644 --- a/fs/btrfs/ordered-data.c +++ b/fs/btrfs/ordered-data.c @@ -18,6 +18,7 @@ #include "delalloc-space.h" #include "qgroup.h" #include "subpage.h" +#include "relocation.h" #include "file.h" #include "super.h" @@ -274,8 +275,27 @@ struct btrfs_ordered_extent *btrfs_alloc_ordered_extent( entry = alloc_ordered_extent(inode, file_offset, num_bytes, ram_bytes, disk_bytenr, disk_num_bytes, offset, flags, compress_type); - if (!IS_ERR(entry)) - insert_ordered_extent(entry); + if (IS_ERR(entry)) + return entry; + + /* + * Writes to relocation roots are special, and clones the existing csums + * from the csum tree instead of calculating them. + * + * Clone the csums here so that the ordered extent never gets inserted + * into the per-inode ordered extent tree and per-root list on failure. + */ + if (btrfs_is_data_reloc_root(inode->root)) { + int ret; + + ret = btrfs_reloc_clone_csums(entry); + if (ret) { + kmem_cache_free(btrfs_ordered_extent_cache, entry); + return ERR_PTR(ret); + } + } + + insert_ordered_extent(entry); return entry; }