Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3474235rdb; Wed, 13 Sep 2023 13:11:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbRR2R/PI9WDpqA0wgQTErQMls30q98rcsSqcM3K9zirIy/LMAzsbgsbilb7tjM5+PV1xG X-Received: by 2002:a05:6a20:729b:b0:13c:dee4:ceaa with SMTP id o27-20020a056a20729b00b0013cdee4ceaamr3900591pzk.16.1694635864887; Wed, 13 Sep 2023 13:11:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694635864; cv=none; d=google.com; s=arc-20160816; b=YBlu8tihGfCWq2aU9a4iiHoKgk0DuuxqljbGPNGUxG0f1Jy5smiya6Hzbha9sWlGqB aHHeLp3J6EbbrHgj6yUH0C7qJZHZscsp5nJi9YVJUVUVUlrNiOO4xXzhrLcFreK9UYXV 8veEHIKmrkYRDK+QWPNaHB9rfrveU6KkAzriR1U8BFE9WLj8y9Dzhclp4lsz7nHBPUVc 2BUxfBEJQVY7uO91OpP61hBV8qEnyOV06b/oTIgTaXbH9QUaaGHJlNyCPYRX6+pEwFr8 26LrDarGyhEoe+spx6QWxCL1wZLbsuP6o7R/ouLE43zmCKawPv6Lj9fdo3HsP9qLh0FN Yp7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:reply-to:message-id:subject:cc:to:from:date :dkim-signature:dkim-signature; bh=ZCUJIvSAdfq2/FYSl4AeA+MvtXsJkXBjw1qDBWVvC0E=; fh=qGRpR2wmuAnt6ln+qRJR0DHzKnQGrV7OLrCPT1OfHE0=; b=1GMhMohELmim4wqHJTIP/Rti02LfcO4LBC03ZDYMdmILmJWCaCXkZ0RNd1SAExPPwk 0474ZEF2Lg5ZCZtWsIi1e1238yD3xa+2dtfyYsjOOZDe0v6ylkTdu3lCVa61+0un/4+l 1aGqlkWN/hBV0WZEvEGAmXq+oI1VghomeV65OSRcKRNLpyPaSyCpNAhnAg3CQ0t1IxSA xHF7QESMDXFOk90vntTiNYz0sj555xC+A+LoxmJsvcmF63lixbL6srfbPcpaadESxp80 iq1YWP9S3fsTYqy4Z4XqpaF6nIIsgqltwLQuFH+RvouW/6WzwGepyUevgVqOnoIYHEfz 3BSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=m6FXeNKU; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=rPMdukcV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id k8-20020a628408000000b0068a49a550b9si10595533pfd.139.2023.09.13.13.11.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 13:11:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=m6FXeNKU; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=rPMdukcV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id ED2028112989; Wed, 13 Sep 2023 09:57:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229713AbjIMQ5T (ORCPT + 99 others); Wed, 13 Sep 2023 12:57:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229625AbjIMQ5Q (ORCPT ); Wed, 13 Sep 2023 12:57:16 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1C0F1BE2; Wed, 13 Sep 2023 09:57:11 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 70ADA1F383; Wed, 13 Sep 2023 16:57:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1694624230; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ZCUJIvSAdfq2/FYSl4AeA+MvtXsJkXBjw1qDBWVvC0E=; b=m6FXeNKU0el+A4CbL0q8LJTrgsL7x+VelfnZkvubqyUiLjE+KxxdqeFkWdbaC2xEmiVSYU 2NHQD67GwQUoyp7dfSwKarlRreEQJXA2FTW1lv9Q6jS2sBfC+bc9iqFrPF3SFfB8esN+lV DqtFSHHcquBnvuxJ3VBWvZJDcLFMNS8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1694624230; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ZCUJIvSAdfq2/FYSl4AeA+MvtXsJkXBjw1qDBWVvC0E=; b=rPMdukcVliWd+qcxQ32gbOPFHBjD+oQDtdcG7rRVo3P55r2q1vG1R67WmHy4s6WHac8YfB hFfG6uigNlw1udBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1A3BB13582; Wed, 13 Sep 2023 16:57:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 4F+gBebpAWUxLAAAMHmgww (envelope-from ); Wed, 13 Sep 2023 16:57:10 +0000 Date: Wed, 13 Sep 2023 18:57:08 +0200 From: David Sterba To: Johannes Thumshirn Cc: Chris Mason , Josef Bacik , David Sterba , Christoph Hellwig , Naohiro Aota , Qu Wenruo , Damien Le Moal , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 03/11] btrfs: add support for inserting raid stripe extents Message-ID: <20230913165708.GR20408@twin.jikos.cz> Reply-To: dsterba@suse.cz References: <20230911-raid-stripe-tree-v8-0-647676fa852c@wdc.com> <20230911-raid-stripe-tree-v8-3-647676fa852c@wdc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230911-raid-stripe-tree-v8-3-647676fa852c@wdc.com> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 09:57:36 -0700 (PDT) On Mon, Sep 11, 2023 at 05:52:04AM -0700, Johannes Thumshirn wrote: > +static int btrfs_insert_striped_mirrored_raid_extents( > + struct btrfs_trans_handle *trans, > + struct btrfs_ordered_extent *ordered, > + u64 map_type) > +{ > + struct btrfs_io_context *bioc; > + struct btrfs_io_context *rbioc; > + const int nstripes = list_count_nodes(&ordered->bioc_list); > + const int index = btrfs_bg_flags_to_raid_index(map_type); > + const int substripes = btrfs_raid_array[index].sub_stripes; > + const int max_stripes = trans->fs_info->fs_devices->rw_devices / 2; > + int left = nstripes; > + int stripe = 0, j = 0; > + int i = 0; Please move the initialization right before the block that uses the variables. > + int ret = 0; > + u64 stripe_end; > + u64 prev_end; > + > + if (nstripes == 1) > + return btrfs_insert_mirrored_raid_extents(trans, ordered, map_type); > + > + rbioc = kzalloc(struct_size(rbioc, stripes, nstripes * substripes), > + GFP_KERNEL); > + if (!rbioc) > + return -ENOMEM; > + > + rbioc->map_type = map_type; > + rbioc->logical = list_first_entry(&ordered->bioc_list, typeof(*rbioc), > + ordered_entry)->logical; > + > + stripe_end = rbioc->logical; > + prev_end = stripe_end; Like here, initializing i > + list_for_each_entry(bioc, &ordered->bioc_list, ordered_entry) { > + > + rbioc->size += bioc->size; > + for (j = 0; j < substripes; j++) { And if you don't use 'j' outside of the for cycle you can use the delcarations inside the for (...). > + stripe = i + j; > + rbioc->stripes[stripe].dev = bioc->stripes[j].dev; > + rbioc->stripes[stripe].physical = bioc->stripes[j].physical; > + rbioc->stripes[stripe].length = bioc->size; > + } > + > + stripe_end += rbioc->size; > + if (i >= nstripes || > + (stripe_end - prev_end >= max_stripes * BTRFS_STRIPE_LEN)) { > + ret = btrfs_insert_one_raid_extent(trans, > + nstripes * substripes, > + rbioc); > + if (ret) > + goto out; > + > + left -= nstripes; > + i = 0; > + rbioc->logical += rbioc->size; > + rbioc->size = 0; > + } else { > + i += substripes; > + prev_end = stripe_end; > + } > + } > + > + if (left) { > + bioc = list_prev_entry(bioc, ordered_entry); > + ret = btrfs_insert_one_raid_extent(trans, substripes, bioc); > + } > + > +out: > + kfree(rbioc); > + return ret; > +} > + > +static int btrfs_insert_striped_raid_extents(struct btrfs_trans_handle *trans, > + struct btrfs_ordered_extent *ordered, > + u64 map_type) > +{ > + struct btrfs_io_context *bioc; > + struct btrfs_io_context *rbioc; > + const int nstripes = list_count_nodes(&ordered->bioc_list); > + int i = 0; > + int ret = 0; > + > + rbioc = kzalloc(struct_size(rbioc, stripes, nstripes), GFP_KERNEL); You can't use GFP_KERNEL generally in any function that takes a transaction handle parameter. Either GFP_NOFS or with the memalloc_nofs_* protection. > + if (!rbioc) > + return -ENOMEM; > + rbioc->map_type = map_type; > + rbioc->logical = list_first_entry(&ordered->bioc_list, typeof(*rbioc), > + ordered_entry)->logical; > + Maybe initializing 'i' here would be better so it's consistent with other code. > + list_for_each_entry(bioc, &ordered->bioc_list, ordered_entry) { > + rbioc->size += bioc->size; > + rbioc->stripes[i].dev = bioc->stripes[0].dev; > + rbioc->stripes[i].physical = bioc->stripes[0].physical; > + rbioc->stripes[i].length = bioc->size; > + > + if (i == nstripes - 1) { > + ret = btrfs_insert_one_raid_extent(trans, nstripes, rbioc); > + if (ret) > + goto out; > + > + i = 0; > + rbioc->logical += rbioc->size; > + rbioc->size = 0; > + } else { > + i++; > + } > + } > + > + if (i && i < nstripes - 1) > + ret = btrfs_insert_one_raid_extent(trans, i, rbioc); > + > +out: > + kfree(rbioc); > + return ret; > +} > + > +int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans, > + struct btrfs_ordered_extent *ordered_extent) > +{ > + struct btrfs_io_context *bioc; > + u64 map_type; > + int ret; > + > + if (!trans->fs_info->stripe_root) > + return 0; > + > + map_type = list_first_entry(&ordered_extent->bioc_list, typeof(*bioc), > + ordered_entry)->map_type; > + > + switch (map_type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { > + case BTRFS_BLOCK_GROUP_DUP: > + case BTRFS_BLOCK_GROUP_RAID1: > + case BTRFS_BLOCK_GROUP_RAID1C3: > + case BTRFS_BLOCK_GROUP_RAID1C4: > + ret = btrfs_insert_mirrored_raid_extents(trans, ordered_extent, > + map_type); > + break; > + case BTRFS_BLOCK_GROUP_RAID0: > + ret = btrfs_insert_striped_raid_extents(trans, ordered_extent, > + map_type); > + break; > + case BTRFS_BLOCK_GROUP_RAID10: > + ret = btrfs_insert_striped_mirrored_raid_extents(trans, ordered_extent, map_type); > + break; > + default: > + ret = -EINVAL; > + break; > + } > + > + while (!list_empty(&ordered_extent->bioc_list)) { > + bioc = list_first_entry(&ordered_extent->bioc_list, > + typeof(*bioc), ordered_entry); > + list_del(&bioc->ordered_entry); > + btrfs_put_bioc(bioc); > + } > + > + return ret; > +} > diff --git a/fs/btrfs/raid-stripe-tree.h b/fs/btrfs/raid-stripe-tree.h > new file mode 100644 > index 000000000000..f36e4c2d46b0 > --- /dev/null > +++ b/fs/btrfs/raid-stripe-tree.h > @@ -0,0 +1,34 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (C) 2023 Western Digital Corporation or its affiliates. > + */ > + > +#ifndef BTRFS_RAID_STRIPE_TREE_H > +#define BTRFS_RAID_STRIPE_TREE_H > + > +#include "disk-io.h" > + > +struct btrfs_io_context; > +struct btrfs_io_stripe; Please add more forward declarations, btrfs_trans_handle, btrfs_ordered_extent or fs_info. > + > +int btrfs_insert_raid_extent(struct btrfs_trans_handle *trans, > + struct btrfs_ordered_extent *ordered_extent); > + > +static inline bool btrfs_need_stripe_tree_update(struct btrfs_fs_info *fs_info, > + u64 map_type) > +{ > + u64 type = map_type & BTRFS_BLOCK_GROUP_TYPE_MASK; > + u64 profile = map_type & BTRFS_BLOCK_GROUP_PROFILE_MASK; > + > + if (!btrfs_stripe_tree_root(fs_info)) > + return false; > + > + if (type != BTRFS_BLOCK_GROUP_DATA) > + return false; > + > + if (profile & BTRFS_BLOCK_GROUP_RAID1_MASK) > + return true; > + > + return false; > +} > +#endif