From patchwork Tue Oct 20 14:02:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 11847061 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E415C4363A for ; Tue, 20 Oct 2020 14:03:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C01F222249 for ; Tue, 20 Oct 2020 14:03:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="jg2ifzCe" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407802AbgJTODR (ORCPT ); Tue, 20 Oct 2020 10:03:17 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:44740 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407796AbgJTODQ (ORCPT ); Tue, 20 Oct 2020 10:03:16 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KDwe04020166; Tue, 20 Oct 2020 14:03:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=TzE67M+tCTfHivihyiWDT/nwZ+UJ/PZUoWqYwEwrgEI=; b=jg2ifzCeT/jiNGE/dWBh5tXI6wMMBsBWoRnXL+1Dh/CrIQgTZ2eNI1Lrc5epTOdTC/q4 eBppaKQT6YBi1wXa9SIXsaC3wi7XfwQZc/ZvWrgtdJ+YCj1947NOrxxueBPjL1CY0ElW MGecaqhuC2chW6BQfWWJ6vhyB+brRmyW1B5RY9Uw7KcKPxJOXK1RPn5+K8Ecjhg4lODE cDsJDTf4DwJwb3dFtFoCoLXbolXiK0Y7n32eHI9VhhKdqTe3pIHcF8OZhISrTkNzLID/ f1h9l9Z0Iv1MHmWP+M/Ew8YDwVbboJ/YdOACckMEw4ZD8+MiXBzPuDY5vz4IBfkqibTP /A== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by aserp2120.oracle.com with ESMTP id 349jrpkcvu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 20 Oct 2020 14:03:12 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KE1aJx127767; Tue, 20 Oct 2020 14:03:11 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 348agxec1q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Oct 2020 14:03:11 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 09KE3B5M008863; Tue, 20 Oct 2020 14:03:11 GMT Received: from localhost.localdomain (/39.109.231.106) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 20 Oct 2020 07:03:09 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, Anand Jain Subject: [PATCH v8 1/3] btrfs: add btrfs_strmatch helper Date: Tue, 20 Oct 2020 22:02:13 +0800 Message-Id: <7418ae34e5a4cc7d110eed756d9e37c5630ec568.1602756068.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 phishscore=0 malwarescore=0 spamscore=0 suspectscore=1 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 lowpriorityscore=0 priorityscore=1501 impostorscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 spamscore=0 suspectscore=1 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Add a generic helper to match the golden-string in the given-string, and ignore the leading and trailing whitespaces if any. Signed-off-by: Anand Jain Suggested-by: David Sterba --- v5: born fs/btrfs/sysfs.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 8424f5d0e5ed..eb0b2bfcce67 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -863,6 +863,29 @@ static ssize_t btrfs_generation_show(struct kobject *kobj, } BTRFS_ATTR(, generation, btrfs_generation_show); +/* + * Match the %golden in the %given. Ignore the leading and trailing whitespaces + * if any. + */ +static int btrfs_strmatch(const char *given, const char *golden) +{ + size_t len = strlen(golden); + char *stripped; + + /* strip leading whitespace */ + stripped = skip_spaces(given); + + if (strncmp(stripped, golden, len) == 0) { + /* strip trailing whitespace */ + if (strlen(skip_spaces(stripped + len))) + return -EINVAL; + + return 0; + } + + return -EINVAL; +} + static const struct attribute *btrfs_attrs[] = { BTRFS_ATTR_PTR(, label), BTRFS_ATTR_PTR(, nodesize), From patchwork Tue Oct 20 14:02:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 11847063 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27E94C4363D for ; Tue, 20 Oct 2020 14:03:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC11522249 for ; Tue, 20 Oct 2020 14:03:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="oRI7TtDc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407804AbgJTODZ (ORCPT ); Tue, 20 Oct 2020 10:03:25 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:41730 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407762AbgJTODY (ORCPT ); Tue, 20 Oct 2020 10:03:24 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KE09L0044121; Tue, 20 Oct 2020 14:03:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=Hk0OLAcugKcV3OmyzB1nnMlWisbheWtl/zpagE0RmUE=; b=oRI7TtDcZIelM2Kip87DmWsFN/4L6sitridN4Z8P3RjE4BSSRVAb79tGiVGPgkwI0vdc omwGSHUglKpoUx52NIMnhYOPUxRPIdCyxTZiEtzU2cIm4FhraVEAAzvCqWJFgbx2IJCC 40qhy+iuQ1zGYCj3DFA2VqgnjvaErkGSNBY3+LN294jwkjIHz4kePcqqeaZQc5N63n4K ylisy0mg6pweUM2hhtTX/+tz4/JTZByuotFz4ZRrnVcQMuZB4U+ZMJt15XAUEUExHAct 4GM6jsC7s9Bfs92o7jP95UNXyJyVnhE4/JdrjhKOz1zs8yNBZVZA7YIlUlJTMJWR9ZVz 4w== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 347s8mu6ha-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 20 Oct 2020 14:03:20 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KE1cFt127904; Tue, 20 Oct 2020 14:03:19 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by aserp3020.oracle.com with ESMTP id 348agxec5p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Oct 2020 14:03:19 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 09KE3I4M019396; Tue, 20 Oct 2020 14:03:18 GMT Received: from localhost.localdomain (/39.109.231.106) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 20 Oct 2020 07:03:15 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, Anand Jain Subject: [PATCH v8 2/3] btrfs: create read policy framework Date: Tue, 20 Oct 2020 22:02:14 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=96 mlxscore=0 phishscore=0 malwarescore=0 spamscore=0 suspectscore=1 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=1 lowpriorityscore=0 mlxlogscore=999 priorityscore=1501 spamscore=0 phishscore=0 clxscore=1015 bulkscore=0 impostorscore=0 adultscore=90 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org As of now, we use the %pid method to read striped mirrored data, which means process id determines the stripe id to read. This type of routing typically helps in a system with many small independent processes tying to read random data. On the other hand, the %pid based read IO policy is inefficient because if there is a single process trying to read a large file, the overall disk bandwidth remains under-utilized. So this patch introduces a read policy framework so that we could add more read policies, such as IO routing based on the device's wait-queue or manual when we have a read-preferred device or a policy based on the target storage caching. Signed-off-by: Anand Jain Reviewed-by: Josef Bacik --- v8: use fallthrough; v7: Fix missing /* fall through */ in the switch Removed Reviewed-by: Josef Bacik v6:- v5: Title renamed from:- btrfs: add read_policy framework Change log updated. Unnecessary comment dropped, added more where necessary. Optimize code in the switch remove duplicate code. Define BTRFS_READ_POLICY_DEFAULT dropped. Rename enum btrfs_read_policy_type to enum btrfs_read_policy. Rename BTRFS_READ_BY_PID to BTRFS_READ_POLICY_PID. (As its mainly renames. Reviewed-by retained). v4: - v3: Declare fs_devices::readmirror as enum btrfs_readmirror_policy_type v2: Declare fs_devices::readmirror as u8 instead of atomic_t A small change in comment and change log wordings. fs/btrfs/volumes.c | 15 ++++++++++++++- fs/btrfs/volumes.h | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 58b9c419a2b6..da31b11ceb61 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -1223,6 +1223,7 @@ static int open_fs_devices(struct btrfs_fs_devices *fs_devices, fs_devices->latest_bdev = latest_dev->bdev; fs_devices->total_rw_bytes = 0; fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; + fs_devices->read_policy = BTRFS_READ_POLICY_PID; return 0; } @@ -5482,7 +5483,19 @@ static int find_live_mirror(struct btrfs_fs_info *fs_info, else num_stripes = map->num_stripes; - preferred_mirror = first + current->pid % num_stripes; + switch (fs_info->fs_devices->read_policy) { + default: + /* + * Shouldn't happen, just warn and use pid instead of failing. + */ + btrfs_warn_rl(fs_info, + "unknown read_policy type %u, fallback to pid", + fs_info->fs_devices->read_policy); + fallthrough; + case BTRFS_READ_POLICY_PID: + preferred_mirror = first + current->pid % num_stripes; + break; + } if (dev_replace_is_ongoing && fs_info->dev_replace.cont_reading_from_srcdev_mode == diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index bf27ac07d315..e3c36951742d 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -211,6 +211,15 @@ enum btrfs_chunk_allocation_policy { BTRFS_CHUNK_ALLOC_REGULAR, }; +/* + * Read policies for the mirrored block groups, read picks the stripe based + * on these policies. + */ +enum btrfs_read_policy { + BTRFS_READ_POLICY_PID, + BTRFS_NR_READ_POLICY, +}; + struct btrfs_fs_devices { u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */ u8 metadata_uuid[BTRFS_FSID_SIZE]; @@ -264,6 +273,11 @@ struct btrfs_fs_devices { struct completion kobj_unregister; enum btrfs_chunk_allocation_policy chunk_alloc_policy; + + /* + * policy used to read the mirrored stripes + */ + enum btrfs_read_policy read_policy; }; #define BTRFS_BIO_INLINE_CSUM_SIZE 64 From patchwork Tue Oct 20 14:02:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 11847067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE659C4363A for ; Tue, 20 Oct 2020 14:03:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 45A5422249 for ; Tue, 20 Oct 2020 14:03:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="MmPov+fP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407805AbgJTODj (ORCPT ); Tue, 20 Oct 2020 10:03:39 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:41958 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407753AbgJTODj (ORCPT ); Tue, 20 Oct 2020 10:03:39 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KE080x043993; Tue, 20 Oct 2020 14:03:34 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=corp-2020-01-29; bh=YDtsIqK0KvjFrYmOoRGe86dnOaglZR3nKv7EhJB14O4=; b=MmPov+fPF5vZbI488ZZwB69V1oUdB82aeyzcrYQh229V763Kh9hsjnqvL2NZQMV29qMG ErrB6qSGcWmwp90qBVYiPlG9HsKRC72omHEpRfRKWzvKrsHNfbkmFwkw5mFAaDRaRTfr xbDonQx43ak7NqaLk7//EyNFe0vGxyoAj+PmF0i3jquuhGrzPIyPoEmOCMRqXnPCOD8H 40LxoyMMiIBbIJVKsy/UtpFaWjuRJ/FRuKzZCfxBcp7duVexnpJI4c+RKsKaOqk2Y4/n qoWLgJRFc5jQUILhBEiBPyfDNf4dqREt6vCNNbNtxKrNK3o290KuVRGr2IrHOKBExME0 hg== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 347s8mu6jf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 20 Oct 2020 14:03:34 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 09KE1bgD127823; Tue, 20 Oct 2020 14:03:34 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 348agxecf5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 20 Oct 2020 14:03:33 +0000 Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 09KE3W7E006745; Tue, 20 Oct 2020 14:03:32 GMT Received: from localhost.localdomain (/39.109.231.106) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 20 Oct 2020 07:03:29 -0700 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.com, Anand Jain Subject: [PATCH v8 3/3] btrfs: create read policy sysfs attribute, pid Date: Tue, 20 Oct 2020 22:02:15 +0800 Message-Id: <806bf3aaa5cb0243dd2cea6bb79e5ac9ae347111.1602756068.git.anand.jain@oracle.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxscore=0 phishscore=0 malwarescore=0 spamscore=0 suspectscore=1 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9779 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 suspectscore=1 lowpriorityscore=0 mlxlogscore=999 priorityscore=1501 spamscore=0 phishscore=0 clxscore=1015 bulkscore=0 impostorscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2010200095 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Add /sys/fs/btrfs/UUID/read_policy attribute so that the read policy for the raid1, raid1c34 and raid10 can be tuned. When this attribute is read, it shall show all available policies, with active policy being with in [ ]. The read_policy attribute can be written using one of the items listed in there. For example: $cat /sys/fs/btrfs/UUID/read_policy [pid] $echo pid > /sys/fs/btrfs/UUID/read_policy Signed-off-by: Anand Jain --- rebase on latest misc-next v5: Title rename: old: btrfs: sysfs, add read_policy attribute Uses the btrfs_strmatch() helper (BTRFS_READ_POLICY_NAME_MAX dropped). Use the table for the policy names. Rename len to ret. Use a simple logic to prefix space in btrfs_read_policy_show() Reviewed-by: Josef Bacik dropped. v4:- v3: rename [by_pid] to [pid] v2: v2: check input len before strip and kstrdup fs/btrfs/sysfs.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index eb0b2bfcce67..07a1a57b2df2 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -886,6 +886,54 @@ static int btrfs_strmatch(const char *given, const char *golden) return -EINVAL; } +static const char* const btrfs_read_policy_name[] = { "pid" }; + +static ssize_t btrfs_read_policy_show(struct kobject *kobj, + struct kobj_attribute *a, char *buf) +{ + int i; + ssize_t ret = 0; + struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); + + for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { + if (fs_devices->read_policy == i) + ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]", + (ret == 0 ? "" : " "), + btrfs_read_policy_name[i]); + else + ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s", + (ret == 0 ? "" : " "), + btrfs_read_policy_name[i]); + } + + ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n"); + + return ret; +} + +static ssize_t btrfs_read_policy_store(struct kobject *kobj, + struct kobj_attribute *a, + const char *buf, size_t len) +{ + int i; + struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); + + for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { + if (btrfs_strmatch(buf, btrfs_read_policy_name[i]) == 0) { + if (i != fs_devices->read_policy) { + fs_devices->read_policy = i; + btrfs_info(fs_devices->fs_info, + "read policy set to '%s'", + btrfs_read_policy_name[i]); + } + return len; + } + } + + return -EINVAL; +} +BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store); + static const struct attribute *btrfs_attrs[] = { BTRFS_ATTR_PTR(, label), BTRFS_ATTR_PTR(, nodesize), @@ -896,6 +944,7 @@ static const struct attribute *btrfs_attrs[] = { BTRFS_ATTR_PTR(, checksum), BTRFS_ATTR_PTR(, exclusive_operation), BTRFS_ATTR_PTR(, generation), + BTRFS_ATTR_PTR(, read_policy), NULL, };