Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp955856lqj; Mon, 3 Jun 2024 06:15:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV9HqqogZGb2JsgpAaPPIo7414ytgE/1LxvjAnzhBdyfoXC7IGAqbn3knhFEQPjLoyv7/lqkOtQBfwp2kzggbngGAd/lZRh+8E4xuJZOA== X-Google-Smtp-Source: AGHT+IEo1TbQfbScvWCmj6m9CpecQcgrfB4NE1HnOqL55GJbNZG77TsAB+O/2Fg3LZcT3xUiNGoo X-Received: by 2002:a17:903:22d0:b0:1f4:8e6d:6efc with SMTP id d9443c01a7336-1f636fe8766mr103309065ad.5.1717420532749; Mon, 03 Jun 2024 06:15:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717420532; cv=pass; d=google.com; s=arc-20160816; b=BcGMTCok9Sw9r+p2JEEPyoHOgkhxPLyzyp0esduQdQcZn0AcRweAJM1g3oY7dDcFpJ Xd5gi0GMV7QTQHCw/rUOCcWOCpjCSFn5dJLTQN02mFJUzap5g68pejY8iY8fAKh5yySW n2Ufiyw1HZMEl9jGsmizPYEfSmv1aY7Q0Se9ffCLOk3ycqpr/m9xB3HtvYgZFusnIWfJ IAyKRnjlYy4evB7KBI6nEH8Jzuc+Oh3VEP0VlPXMKMS5lhxMAI3Ziy1VXAxRLEsQXdq0 qA/P7tBCW109nAaEPGOYQkqoxWC3DfwFP5PNzrcg0IVo2Iop2WFaWQBO6iD7MzHiRl6V dLqQ== ARC-Message-Signature: i=2; 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=gU7lX51h6MTMzRGP3XWqXJ0ZgcSnt3npBpe1Q5fNYYA=; fh=1LhuwRtrwnHwkJ8SSfr9g2yPev2+fxl/FqZerKdeGVc=; b=C/sQzO7xombuV3Xqgn535ZfPtAVaTllltlGTxteye9d6f6X3mCTOTuVVie9b9EwZWc AuEHs3VK0j+f6jb01k9ZrKYW6m3blhwLvXLiQ1MuZlzrhXD/ashqhKJDNBPuMQLyZX86 WiqSDF8SvHFrhVpNG4ltcopUlOnLT1VJo9VTT7hPU0A+ckh/BSap7xuwY0MiXNPLChEg za1flYTTz31z4wYar72ARIgXKb0/QBXM/c5n+OPCiMczKy+8cy/MpPBYjOnPYmjvm5v8 V3O4CgrsyW9cDKhqVosYpuLXYcV6z9Riylz3mFZ8SNUYWMkU5Skh4rtFSyj/g9wPRCuv QbSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-199153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199153-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d9443c01a7336-1f6323de8c7si67761865ad.255.2024.06.03.06.15.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 06:15:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-199153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199153-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 15FD0B25460 for ; Mon, 3 Jun 2024 12:59:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A79FE12D1F6; Mon, 3 Jun 2024 12:59:16 +0000 (UTC) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (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 5E55612C816; Mon, 3 Jun 2024 12:59:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717419556; cv=none; b=Z0cyuaxisRmjPDMybOZ942AwCNsto9HpJbVCHzUMgj5ToJebAu4lqCibpi6+fxpRyOchlBcpPrAg3dzsvcEbFB2PHf5wlHJ2HlN44jQ7xz8H+BUCxM9kTf7gkHY+vL4ViVQDV4h6fAcj+2chYkTNcyYhYF9bDzZZT2mvYGvuhhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717419556; c=relaxed/simple; bh=25a+xXQ1heY5RNKMbD3/mNhgR1Z1gljogRR9UUG+yTE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QI2TqLnPMHAY4PrTaTWzwsPm6sTiBOileO8Qsx8rql4alWxvlbcXXaI4lWVLdKqqxd4eKqSOjLc+1tfY7YwTOvPWgrSFbWmqNVEi44JHU5mno05qNLQPgvzLNM0lTLr1/osg/XyVJ3xNHCuJyNlb2j/sMTQvGX1O3AWleb3vKgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4VtDFt0q8WzmW8y; Mon, 3 Jun 2024 20:54:42 +0800 (CST) Received: from kwepemm600009.china.huawei.com (unknown [7.193.23.164]) by mail.maildlp.com (Postfix) with ESMTPS id 6A6AD140413; Mon, 3 Jun 2024 20:58:57 +0800 (CST) Received: from huawei.com (10.175.104.67) by kwepemm600009.china.huawei.com (7.193.23.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.39; Mon, 3 Jun 2024 20:58:14 +0800 From: Yu Kuai To: , , , , , , CC: , , , , , , Subject: [PATCH 05/12] md: replace sysfs api sync_action with new helpers Date: Mon, 3 Jun 2024 20:58:08 +0800 Message-ID: <20240603125815.2199072-6-yukuai3@huawei.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240603125815.2199072-1-yukuai3@huawei.com> References: <20240603125815.2199072-1-yukuai3@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600009.china.huawei.com (7.193.23.164) To get rid of extrem long if else if usage, and make code cleaner. Signed-off-by: Yu Kuai --- drivers/md/md.c | 94 +++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index 28f50a106566..8a54b56e3463 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -4949,27 +4949,9 @@ const char *md_sync_action_name(enum sync_action action) static ssize_t action_show(struct mddev *mddev, char *page) { - char *type = "idle"; - unsigned long recovery = mddev->recovery; - if (test_bit(MD_RECOVERY_FROZEN, &recovery)) - type = "frozen"; - else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || - (md_is_rdwr(mddev) && test_bit(MD_RECOVERY_NEEDED, &recovery))) { - if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) - type = "reshape"; - else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { - if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) - type = "resync"; - else if (test_bit(MD_RECOVERY_CHECK, &recovery)) - type = "check"; - else - type = "repair"; - } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) - type = "recover"; - else if (mddev->reshape_position != MaxSector) - type = "reshape"; - } - return sprintf(page, "%s\n", type); + enum sync_action action = md_sync_action(mddev); + + return sprintf(page, "%s\n", md_sync_action_name(action)); } /** @@ -5112,35 +5094,63 @@ static int mddev_start_reshape(struct mddev *mddev) static ssize_t action_store(struct mddev *mddev, const char *page, size_t len) { + int ret; + enum sync_action action; + if (!mddev->pers || !mddev->pers->sync_request) return -EINVAL; + action = md_sync_action_by_name(page); - if (cmd_match(page, "idle")) - idle_sync_thread(mddev); - else if (cmd_match(page, "frozen")) - frozen_sync_thread(mddev); - else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) - return -EBUSY; - else if (cmd_match(page, "resync")) - clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); - else if (cmd_match(page, "recover")) { - clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); - set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); - } else if (cmd_match(page, "reshape")) { - int err = mddev_start_reshape(mddev); - - if (err) - return err; + /* TODO: mdadm rely on "idle" to start sync_thread. */ + if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { + switch (action) { + case ACTION_FROZEN: + frozen_sync_thread(mddev); + return len; + case ACTION_IDLE: + idle_sync_thread(mddev); + break; + case ACTION_RESHAPE: + case ACTION_RECOVER: + case ACTION_CHECK: + case ACTION_REPAIR: + case ACTION_RESYNC: + return -EBUSY; + default: + return -EINVAL; + } } else { - if (cmd_match(page, "check")) + switch (action) { + case ACTION_FROZEN: + set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); + return len; + case ACTION_RESHAPE: + clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); + ret = mddev_start_reshape(mddev); + if (ret) + return ret; + break; + case ACTION_RECOVER: + clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); + set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); + break; + case ACTION_CHECK: set_bit(MD_RECOVERY_CHECK, &mddev->recovery); - else if (!cmd_match(page, "repair")) + fallthrough; + case ACTION_REPAIR: + set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); + set_bit(MD_RECOVERY_SYNC, &mddev->recovery); + fallthrough; + case ACTION_RESYNC: + case ACTION_IDLE: + clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); + break; + default: return -EINVAL; - clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); - set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); - set_bit(MD_RECOVERY_SYNC, &mddev->recovery); + } } + if (mddev->ro == MD_AUTO_READ) { /* A write to sync_action is enough to justify * canceling read-auto mode -- 2.39.2