Blame SOURCES/nfs-utils-2.5.4-nfsrahead-cmd.patch

16940a
diff --git a/.gitignore b/.gitignore
16940a
index c89d1cd2..df791a83 100644
16940a
--- a/.gitignore
16940a
+++ b/.gitignore
16940a
@@ -61,6 +61,8 @@ utils/statd/statd
16940a
 tools/locktest/testlk
16940a
 tools/getiversion/getiversion
16940a
 tools/nfsconf/nfsconf
16940a
+tools/nfsrahead/nfsrahead
16940a
+tools/nfsrahead/99-nfs_bdi.rules
16940a
 support/export/mount.h
16940a
 support/export/mount_clnt.c
16940a
 support/export/mount_xdr.c
16940a
diff --git a/configure.ac b/configure.ac
16940a
index d01ce6e4..3f48bd54 100644
16940a
--- a/configure.ac
16940a
+++ b/configure.ac
16940a
@@ -251,6 +251,16 @@ AC_ARG_ENABLE(nfsdcld,
16940a
 	enable_nfsdcld=$enableval,
16940a
 	enable_nfsdcld="yes")
16940a
 
16940a
+AC_ARG_ENABLE(nfsrahead,
16940a
+	[AS_HELP_STRING([--disable-nfsrahead],[disable nfsrahead command @<:@default=no@:>@])],
16940a
+	enable_nfsrahead=$enableval,
16940a
+	enable_nfsrahead="yes")
16940a
+	AM_CONDITIONAL(CONFIG_NFSRAHEAD, [test "$enable_nfsrahead" = "yes" ])
16940a
+	if test "$enable_nfsrahead" = yes; then
16940a
+		dnl Check for -lmount
16940a
+		PKG_CHECK_MODULES([LIBMOUNT], [mount])
16940a
+	fi
16940a
+
16940a
 AC_ARG_ENABLE(nfsdcltrack,
16940a
 	[AC_HELP_STRING([--disable-nfsdcltrack],
16940a
 			[disable NFSv4 clientid tracking programs @<:@default=no@:>@])],
16940a
@@ -712,6 +722,7 @@ AC_CONFIG_FILES([
16940a
 	tools/rpcgen/Makefile
16940a
 	tools/mountstats/Makefile
16940a
 	tools/nfs-iostat/Makefile
16940a
+	tools/nfsrahead/Makefile
16940a
 	tools/rpcctl/Makefile
16940a
 	tools/nfsdclnts/Makefile
16940a
 	tools/nfsconf/Makefile
16940a
diff --git a/nfs.conf b/nfs.conf
16940a
index bc1de8d1..6aec1dd9 100644
16940a
--- a/nfs.conf
16940a
+++ b/nfs.conf
16940a
@@ -5,6 +5,10 @@
16940a
 [general]
16940a
 # pipefs-directory=/var/lib/nfs/rpc_pipefs
16940a
 #
16940a
+[nfsrahead]
16940a
+# nfs=15000
16940a
+# nfs4=16000
16940a
+#
16940a
 [exports]
16940a
 # rootdir=/export
16940a
 #
16940a
diff --git a/systemd/nfs.conf.man b/systemd/nfs.conf.man
16940a
index be487a11..e74083e9 100644
16940a
--- a/systemd/nfs.conf.man
16940a
+++ b/systemd/nfs.conf.man
16940a
@@ -294,6 +294,17 @@ Only
16940a
 .B debug=
16940a
 is recognized.
16940a
 
16940a
+.TP
16940a
+.B nfsrahead
16940a
+Recognized values:
16940a
+.BR nfs ,
16940a
+.BR nfsv4 ,
16940a
+.BR default .
16940a
+
16940a
+See
16940a
+.BR nfsrahead (5)
16940a
+for deatils.
16940a
+
16940a
 .SH FILES
16940a
 .TP 10n
16940a
 .I /etc/nfs.conf
16940a
diff --git a/tools/Makefile.am b/tools/Makefile.am
16940a
index c3feabbe..48fd0cdf 100644
16940a
--- a/tools/Makefile.am
16940a
+++ b/tools/Makefile.am
16940a
@@ -12,6 +12,10 @@ if CONFIG_NFSDCLD
16940a
 OPTDIRS += nfsdclddb
16940a
 endif
16940a
 
16940a
+if CONFIG_NFSRAHEAD
16940a
+OPTDIRS += nfsrahead
16940a
+endif
16940a
+
16940a
 SUBDIRS = locktest rpcdebug nlmtest mountstats nfs-iostat rpcctl nfsdclnts $(OPTDIRS)
16940a
 
16940a
 MAINTAINERCLEANFILES = Makefile.in
16940a
diff --git a/tools/nfsrahead/99-nfs.rules b/tools/nfsrahead/99-nfs.rules
16940a
new file mode 100644
16940a
index 00000000..c74914b2
16940a
--- /dev/null
16940a
+++ b/tools/nfsrahead/99-nfs.rules
16940a
@@ -0,0 +1 @@
16940a
+SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="/usr/libexec/nfsrahead %k", ATTR{read_ahead_kb}="%c"
16940a
diff --git a/tools/nfsrahead/99-nfs.rules.in b/tools/nfsrahead/99-nfs.rules.in
16940a
new file mode 100644
16940a
index 00000000..648813c5
16940a
--- /dev/null
16940a
+++ b/tools/nfsrahead/99-nfs.rules.in
16940a
@@ -0,0 +1 @@
16940a
+SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="_libexecdir_/nfsrahead %k", ATTR{read_ahead_kb}="%c"
16940a
diff --git a/tools/nfsrahead/Makefile.am b/tools/nfsrahead/Makefile.am
16940a
new file mode 100644
16940a
index 00000000..7e08233a
16940a
--- /dev/null
16940a
+++ b/tools/nfsrahead/Makefile.am
16940a
@@ -0,0 +1,16 @@
16940a
+libexec_PROGRAMS = nfsrahead
16940a
+nfsrahead_SOURCES = main.c
16940a
+nfsrahead_LDFLAGS= $(LIBMOUNT_LIBS)
16940a
+nfsrahead_LDADD = ../../support/nfs/libnfsconf.la
16940a
+
16940a
+man5_MANS = nfsrahead.man
16940a
+EXTRA_DIST = $(man5_MANS)
16940a
+
16940a
+udev_rulesdir = /usr/lib/udev/rules.d/
16940a
+udev_rules_DATA = 99-nfs.rules
16940a
+
16940a
+99-nfs.rules: 99-nfs.rules.in $(builddefs)
16940a
+	$(SED) "s|_libexecdir_|@libexecdir@|g" 99-nfs.rules.in > $@
16940a
+
16940a
+clean-local:
16940a
+	$(RM) 99-nfs.rules
16940a
diff --git a/tools/nfsrahead/main.c b/tools/nfsrahead/main.c
16940a
new file mode 100644
16940a
index 00000000..c83c6f71
16940a
--- /dev/null
16940a
+++ b/tools/nfsrahead/main.c
16940a
@@ -0,0 +1,192 @@
16940a
+#include <stdio.h>
16940a
+#include <string.h>
16940a
+#include <stdlib.h>
16940a
+#include <errno.h>
16940a
+#include <unistd.h>
16940a
+
16940a
+#include <libmount/libmount.h>
16940a
+#include <sys/sysmacros.h>
16940a
+
16940a
+#include "xlog.h"
16940a
+#include "conffile.h"
16940a
+
16940a
+#ifndef MOUNTINFO_PATH
16940a
+#define MOUNTINFO_PATH "/proc/self/mountinfo"
16940a
+#endif
16940a
+
16940a
+#define CONF_NAME "nfsrahead"
16940a
+#define NFS_DEFAULT_READAHEAD 128
16940a
+
16940a
+/* Device information from the system */
16940a
+struct device_info {
16940a
+	char *device_number;
16940a
+	dev_t dev;
16940a
+	char *mountpoint;
16940a
+	char *fstype;
16940a
+};
16940a
+
16940a
+/* Convert a string in the format n:m to a device number */
16940a
+static int fill_device_number(struct device_info *info)
16940a
+{
16940a
+	char *s = strdup(info->device_number), *p;
16940a
+	char *maj_s, *min_s;
16940a
+	unsigned int maj, min;
16940a
+	int err = -EINVAL;
16940a
+
16940a
+	maj_s = p = s;
16940a
+	for ( ; *p != ':' && *p != '\0'; p++)
16940a
+		;
16940a
+
16940a
+	if (*p == '\0')
16940a
+		goto out_free;
16940a
+
16940a
+	err = 0;
16940a
+	*p = '\0';
16940a
+	min_s = p + 1;
16940a
+
16940a
+	maj = strtol(maj_s, NULL, 10);
16940a
+	min = strtol(min_s, NULL, 10);
16940a
+
16940a
+	info->dev = makedev(maj, min);
16940a
+out_free:
16940a
+	free(s);
16940a
+	return err;
16940a
+}
16940a
+
16940a
+#define sfree(ptr) if (ptr) free(ptr)
16940a
+
16940a
+/* device_info maintenance */
16940a
+static void init_device_info(struct device_info *di, const char *device_number)
16940a
+{
16940a
+	di->device_number = strdup(device_number);
16940a
+	di->dev = 0;
16940a
+	di->mountpoint = NULL;
16940a
+	di->fstype = NULL;
16940a
+}
16940a
+
16940a
+
16940a
+static void free_device_info(struct device_info *di)
16940a
+{
16940a
+	sfree(di->mountpoint);
16940a
+	sfree(di->fstype);
16940a
+	sfree(di->device_number);
16940a
+}
16940a
+
16940a
+static int get_mountinfo(const char *device_number, struct device_info *device_info, const char *mountinfo_path)
16940a
+{
16940a
+	int ret = 0;
16940a
+	struct libmnt_table *mnttbl;
16940a
+	struct libmnt_fs *fs;
16940a
+	char *target;
16940a
+
16940a
+	init_device_info(device_info, device_number);
16940a
+	if ((ret = fill_device_number(device_info)) < 0)
16940a
+		goto out_free_device_info;
16940a
+
16940a
+	mnttbl = mnt_new_table();
16940a
+
16940a
+	if ((ret = mnt_table_parse_file(mnttbl, mountinfo_path)) < 0) {
16940a
+		xlog(D_GENERAL, "Failed to parse %s\n", mountinfo_path);
16940a
+		goto out_free_tbl;
16940a
+	}
16940a
+
16940a
+	if ((fs = mnt_table_find_devno(mnttbl, device_info->dev, MNT_ITER_FORWARD)) == NULL) {
16940a
+		ret = ENOENT;
16940a
+		goto out_free_tbl;
16940a
+	}
16940a
+
16940a
+	if ((target = (char *)mnt_fs_get_target(fs)) == NULL) {
16940a
+		ret = ENOENT;
16940a
+		goto out_free_fs;
16940a
+	}
16940a
+
16940a
+	device_info->mountpoint = strdup(target);
16940a
+	target = (char *)mnt_fs_get_fstype(fs);
16940a
+	if (target)
16940a
+		device_info->fstype = strdup(target);
16940a
+
16940a
+out_free_fs:
16940a
+	mnt_free_fs(fs);
16940a
+out_free_tbl:
16940a
+	mnt_free_table(mnttbl);
16940a
+out_free_device_info:
16940a
+	free(device_info->device_number);
16940a
+	device_info->device_number = NULL;
16940a
+	return ret;
16940a
+}
16940a
+
16940a
+static int get_device_info(const char *device_number, struct device_info *device_info)
16940a
+{
16940a
+	int ret = ENOENT;
16940a
+	for (int retry_count = 0; retry_count < 10 && ret != 0; retry_count++)
16940a
+		ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
16940a
+
16940a
+	return ret;
16940a
+}
16940a
+
16940a
+static int conf_get_readahead(const char *kind) {
16940a
+	int readahead = 0;
16940a
+
16940a
+	if((readahead = conf_get_num(CONF_NAME, kind, -1)) == -1)
16940a
+		readahead = conf_get_num(CONF_NAME, "default", NFS_DEFAULT_READAHEAD);
16940a
+	
16940a
+	return readahead;
16940a
+}
16940a
+
16940a
+int main(int argc, char **argv)
16940a
+{
16940a
+	int ret = 0, retry, opt;
16940a
+	struct device_info device;
16940a
+	unsigned int readahead = 128, log_level, log_stderr = 0;
16940a
+
16940a
+
16940a
+	log_level = D_ALL & ~D_GENERAL;
16940a
+	while((opt = getopt(argc, argv, "dF")) != -1) {
16940a
+		switch (opt) {
16940a
+		case 'd':
16940a
+			log_level = D_ALL;
16940a
+			break;
16940a
+		case 'F':
16940a
+			log_stderr = 1;
16940a
+			break;
16940a
+		}
16940a
+	}
16940a
+
16940a
+	conf_init_file(NFS_CONFFILE);
16940a
+
16940a
+	xlog_stderr(log_stderr);
16940a
+	xlog_syslog(~log_stderr);
16940a
+	xlog_config(log_level, 1);
16940a
+	xlog_open(CONF_NAME);
16940a
+
16940a
+	// xlog_err causes the system to exit
16940a
+	if ((argc - optind) != 1)
16940a
+		xlog_err("expected the device number of a BDI; is udev ok?");
16940a
+
16940a
+	for (retry = 0; retry <= 10; retry++ )
16940a
+		if ((ret = get_device_info(argv[optind], &device)) == 0)
16940a
+			break;
16940a
+
16940a
+	if (ret != 0) {
16940a
+		xlog(D_GENERAL, "unable to find device %s\n", argv[optind]);
16940a
+		goto out;
16940a
+	}
16940a
+
16940a
+	if (strncmp("nfs", device.fstype, 3) != 0) {
16940a
+		xlog(D_GENERAL,
16940a
+			"not setting readahead for non supported fstype %s on device %s\n",
16940a
+			device.fstype, argv[optind]);
16940a
+		ret = -EINVAL;
16940a
+		goto out;
16940a
+	}
16940a
+
16940a
+	readahead = conf_get_readahead(device.fstype);
16940a
+
16940a
+	xlog(D_FAC7, "setting %s readahead to %d\n", device.mountpoint, readahead);
16940a
+
16940a
+	printf("%d\n", readahead);
16940a
+
16940a
+out:
16940a
+	free_device_info(&device);
16940a
+	return ret;
16940a
+}
16940a
diff --git a/tools/nfsrahead/nfsrahead.man b/tools/nfsrahead/nfsrahead.man
16940a
new file mode 100644
16940a
index 00000000..5488f633
16940a
--- /dev/null
16940a
+++ b/tools/nfsrahead/nfsrahead.man
16940a
@@ -0,0 +1,72 @@
16940a
+.\" Manpage for nfsrahead.
16940a
+.nh
16940a
+.ad l
16940a
+.TH man 5 "08 Mar 2022" "1.0" "nfsrahead man page"
16940a
+.SH NAME
16940a
+
16940a
+nfsrahead \- Configure the readahead for NFS mounts
16940a
+
16940a
+.SH SYNOPSIS
16940a
+
16940a
+nfsrahead [-F] [-d] <device>
16940a
+
16940a
+.SH DESCRIPTION
16940a
+
16940a
+\fInfsrahead\fR is a tool intended to be used with udev to set the \fIread_ahead_kb\fR parameter of NFS mounts, according to the configuration file (see \fICONFIGURATION\fR). \fIdevice\fR is the device number for the NFS backing device as provided by the kernel.
16940a
+
16940a
+.SH OPTIONS
16940a
+.TP
16940a
+.B -F
16940a
+Send messages to 
16940a
+.I stderr 
16940a
+instead of
16940a
+.I syslog
16940a
+
16940a
+.TP
16940a
+.B -d
16940a
+Increase the debugging level.
16940a
+
16940a
+.SH CONFIGURATION
16940a
+.I nfsrahead
16940a
+is configured in
16940a
+.IR /etc/nfs.conf ,
16940a
+in the section titled
16940a
+.IR nfsrahead .
16940a
+It accepts the following configurations.
16940a
+
16940a
+.TP
16940a
+.B nfs=<value>
16940a
+The readahead value applied to NFSv3 mounts.
16940a
+
16940a
+.TP
16940a
+.B nfs4=<value>
16940a
+The readahead value applied to NFSv4 mounts.
16940a
+
16940a
+.TP
16940a
+.B default=<value>
16940a
+The default configuration when none of the configurations above is set.
16940a
+
16940a
+.SH EXAMPLE CONFIGURATION
16940a
+[nfsrahead]
16940a
+.br
16940a
+nfs=15000              # readahead of 15000 for NFSv3 mounts
16940a
+.br
16940a
+nfs4=16000             # readahead of 16000 for NFSv4 mounts
16940a
+.br
16940a
+default=128            # default is 128
16940a
+
16940a
+.SH SEE ALSO
16940a
+
16940a
+.BR mount.nfs (8),
16940a
+.BR nfs (5),
16940a
+.BR nfs.conf (5),
16940a
+.BR udev (7),
16940a
+.BR bcc-readahead (8)
16940a
+
16940a
+.SH BUGS
16940a
+
16940a
+No known bugs.
16940a
+
16940a
+.SH AUTHOR
16940a
+
16940a
+Thiago Rafael Becker <trbecker@gmail.com>