Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp774329pxp; Fri, 11 Mar 2022 14:47:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJwYBUctd/Bf+s2sOLgiMAdp4kH9Z4dLkuw7mltSo/4Ct29VRemNw9/jUHmzsX6kxY86VE0r X-Received: by 2002:a63:ad47:0:b0:373:4c14:35e2 with SMTP id y7-20020a63ad47000000b003734c1435e2mr10144071pgo.67.1647038858029; Fri, 11 Mar 2022 14:47:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647038858; cv=none; d=google.com; s=arc-20160816; b=Tksbfq3yN7hRzDlwAvE2l3dx3Q5lXGfRwBm2IFv/oslDwefUgtM5BAf2IDPhxWZre4 Gd2anEVsTW4qv93bu92QlK+gldOnWcjEAJ3BgkdwLqCew+M9Kzx6KfBK4buHd41uhWTZ j0FfaGQKD3o/J4YI8mzso6PPd5TP8VAcpvbq1Z6ujE9LiFeQUOAJtPqSnKswbK6Pn+so 4tonzGInfG2w4JDlk7Y4ATyOkVjaQlS7RvowV2dMu2MpcXFGsGqNq2mMGQMzXopsN0Ip wjQGVgIt1GQvyOSrrSjmne/15m/2woSLrGj2UezWp6IUQ+47GaMubgSaAQ4lA6YaMexQ JBrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Apo17SFTV9dOPZLNaybzvszIuETY629ibw6t+ujy1GE=; b=KrkQWnH/W+r3QXJxFjA70PB130rhzPe0bZC3SKJJD2w4AIHav3yzuV/mK/ul6WCxls mHX5b/RWmYjpNzyBzXrlEUAD3fnv1m9xCX53Xeh87Dr8+4RRgLL4xnztzmzLP0gcEme4 kShhCTX4jDo0sTdQEU69f14TNJW88qOV58wb7F/5rdy/hRCet0WO87JYTZIsZzFRMh/+ 0Krcwbu/BofEi0cyHARrSnC7s0BmKa0hPPivOCUP1Q6kGUJoft5B9hOKl8dZLvOKSVE2 RIE1vOivNcO62+++rT9vpk+jL6HyE0p4I9n0bVGFmPNOA7HD/dQJKG4qbByNpPrXX/U4 gWsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DdtFC1tH; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s2-20020a17090302c200b0014d45a6a036si8440328plk.42.2022.03.11.14.47.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 14:47:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=DdtFC1tH; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0B4332459D1; Fri, 11 Mar 2022 13:46:52 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351025AbiCKTIA (ORCPT + 99 others); Fri, 11 Mar 2022 14:08:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351052AbiCKTH6 (ORCPT ); Fri, 11 Mar 2022 14:07:58 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 35A831AE650 for ; Fri, 11 Mar 2022 11:06:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1647025613; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Apo17SFTV9dOPZLNaybzvszIuETY629ibw6t+ujy1GE=; b=DdtFC1tHAj7LkhQw1ZHW9CyIzXaSf85AsJ1QPqsUAEkypaBcLMt8qs8fb2GeGWdej8inSW 3B9786WrtSsk4aZGnpC9bDn6RXh9Gznq8BDBGe+7wup0QaxSXe4uRlGbtDlQKlP4A1+YUr tidfchOPXeZ/Wmt10BDNdTA3o97yle0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-185-WUGyyc3MPB2l2BFD_YjIsA-1; Fri, 11 Mar 2022 14:06:50 -0500 X-MC-Unique: WUGyyc3MPB2l2BFD_YjIsA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F12BF51DC; Fri, 11 Mar 2022 19:06:48 +0000 (UTC) Received: from nyarly.rlyeh.local (ovpn-116-72.gru2.redhat.com [10.97.116.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2A77C60BF4; Fri, 11 Mar 2022 19:06:44 +0000 (UTC) From: Thiago Becker To: linux-nfs@vger.kernel.org Cc: steved@redhat.com, trond.myklebust@hammerspace.com, anna.schumaker@netapp.com, kolga@netapp.com, Thiago Becker Subject: [RFC v2 PATCH 4/7] readahead: only set readahead for nfs devices. Date: Fri, 11 Mar 2022 16:06:14 -0300 Message-Id: <20220311190617.3294919-5-tbecker@redhat.com> In-Reply-To: <20220311190617.3294919-1-tbecker@redhat.com> References: <20220311190617.3294919-1-tbecker@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Limit setting the readahead for nfs devices. Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1946283 Signed-off-by: Thiago Becker --- tools/nfs-readahead-udev/99-nfs_bdi.rules.in | 2 +- tools/nfs-readahead-udev/Makefile.am | 1 + tools/nfs-readahead-udev/main.c | 132 ++++++++++++++++++- 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/tools/nfs-readahead-udev/99-nfs_bdi.rules.in b/tools/nfs-readahead-udev/99-nfs_bdi.rules.in index 15f8a995..744c59be 100644 --- a/tools/nfs-readahead-udev/99-nfs_bdi.rules.in +++ b/tools/nfs-readahead-udev/99-nfs_bdi.rules.in @@ -1 +1 @@ -SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="_libexecdir_/nfs-readahead-udev", ATTR{read_ahead_kb}="%c" +SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="_libexecdir_/nfs-readahead-udev %k", ATTR{read_ahead_kb}="%c" diff --git a/tools/nfs-readahead-udev/Makefile.am b/tools/nfs-readahead-udev/Makefile.am index 5078db9a..551d22e9 100644 --- a/tools/nfs-readahead-udev/Makefile.am +++ b/tools/nfs-readahead-udev/Makefile.am @@ -1,5 +1,6 @@ libexec_PROGRAMS = nfs-readahead-udev nfs_readahead_udev_SOURCES = main.c syslog.c +nfs_readahead_udev_LDFLAGS= -lmount udev_rulesdir = /etc/udev/rules.d udev_rules_DATA = 99-nfs_bdi.rules diff --git a/tools/nfs-readahead-udev/main.c b/tools/nfs-readahead-udev/main.c index dd2c9f8c..bbb408c0 100644 --- a/tools/nfs-readahead-udev/main.c +++ b/tools/nfs-readahead-udev/main.c @@ -1,15 +1,145 @@ #include +#include +#include +#include + +#include +#include #include "log.h" +#ifndef MOUNTINFO_PATH +#define MOUNTINFO_PATH "/proc/self/mountinfo" +#endif + +/* Device information from the system */ +struct device_info { + char *device_number; + dev_t dev; + char *mountpoint; + char *fstype; +}; + +/* Convert a string in the format n:m to a device number */ +static dev_t dev_from_arg(const char *device_number) +{ + char *s = strdup(device_number), *p; + char *maj_s, *min_s; + unsigned int maj, min; + dev_t dev; + + maj_s = p = s; + for ( ; *p != ':'; p++) + ; + + *p = '\0'; + min_s = p + 1; + + maj = strtol(maj_s, NULL, 10); + min = strtol(min_s, NULL, 10); + + dev = makedev(maj, min); + + free(s); + return dev; +} + +#define sfree(ptr) if (ptr) free(ptr) + +/* device_info maintenance */ +static void init_device_info(struct device_info *di, const char *device_number) +{ + di->device_number = strdup(device_number); + di->dev = dev_from_arg(device_number); + di->mountpoint = NULL; + di->fstype = NULL; +} + + +static void free_device_info(struct device_info *di) +{ + sfree(di->mountpoint); + sfree(di->fstype); + sfree(di->device_number); +} + +static int get_mountinfo(const char *device_number, struct device_info *device_info, const char *mountinfo_path) +{ + int ret = 0; + struct libmnt_table *mnttbl; + struct libmnt_fs *fs; + char *target; + + init_device_info(device_info, device_number); + + mnttbl = mnt_new_table(); + + if ((ret = mnt_table_parse_file(mnttbl, mountinfo_path)) < 0) + goto out_free_tbl; + + if ((fs = mnt_table_find_devno(mnttbl, device_info->dev, MNT_ITER_FORWARD)) == NULL) { + ret = ENOENT; + goto out_free_tbl; + } + + if ((target = (char *)mnt_fs_get_target(fs)) == NULL) { + ret = ENOENT; + goto out_free_fs; + } + + device_info->mountpoint = strdup(target); + target = (char *)mnt_fs_get_fstype(fs); + if (target) + device_info->fstype = strdup(target); + +out_free_fs: + mnt_free_fs(fs); +out_free_tbl: + mnt_free_table(mnttbl); + free(device_info->device_number); + device_info->device_number = NULL; + return ret; +} + +static int get_device_info(const char *device_number, struct device_info *device_info) +{ + int ret = ENOENT; + for (int retry_count = 0; retry_count < 10 && ret != 0; retry_count++) + ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH); + + return ret; +} + int main(int argc, char **argv, char **envp) { + int ret = 0; + struct device_info device; unsigned int readahead = 128; log_open(); - info("Setting the readahead to 128\n"); + if (argc != 2) { + err("Expected device number as argument, got none\n"); + return -EINVAL; + } + + if ((ret = get_device_info(argv[1], &device)) != 0) { + err("Failed to find device %s (%d)\n", argv[1], ret); + goto out; + } + + if (strncmp("nfs", device.fstype, 3) != 0) { + ret = -EINVAL; + info("Not setting the readahead for fstype %s\n", device.fstype); + goto out; + } + + info("Setting %s readahead to 128\n", device.mountpoint); log_close(); printf("%d\n", readahead); + +out: + free_device_info(&device); + return ret; } -- 2.35.1