Blame SOURCES/nfs-utils-2.3.3-nfsrahead.patch

bc8330
diff --git a/.gitignore b/.gitignore
bc8330
index e97b31f5..e504d492 100644
bc8330
--- a/.gitignore
bc8330
+++ b/.gitignore
bc8330
@@ -60,6 +60,8 @@ utils/statd/statd
bc8330
 tools/locktest/testlk
bc8330
 tools/getiversion/getiversion
bc8330
 tools/nfsconf/nfsconf
bc8330
+tools/nfsrahead/nfsrahead
bc8330
+tools/nfsrahead/99-nfs_bdi.rules
bc8330
 support/export/mount.h
bc8330
 support/export/mount_clnt.c
bc8330
 support/export/mount_xdr.c
bc8330
diff --git a/configure.ac b/configure.ac
bc8330
index 6d464ac5..f462a645 100644
bc8330
--- a/configure.ac
bc8330
+++ b/configure.ac
bc8330
@@ -639,6 +639,7 @@ AC_CONFIG_FILES([
bc8330
 	tools/rpcgen/Makefile
bc8330
 	tools/mountstats/Makefile
bc8330
 	tools/nfs-iostat/Makefile
bc8330
+	tools/nfsrahead/Makefile
bc8330
 	tools/rpcctl/Makefile
bc8330
 	tools/nfsdclnts/Makefile
bc8330
 	tools/nfsconf/Makefile
bc8330
diff --git a/nfs.conf b/nfs.conf
bc8330
index 86ed7d53..30f9e109 100644
bc8330
--- a/nfs.conf
bc8330
+++ b/nfs.conf
bc8330
@@ -5,6 +5,10 @@
bc8330
 [general]
bc8330
 # pipefs-directory=/var/lib/nfs/rpc_pipefs
bc8330
 #
bc8330
+[nfsrahead]
bc8330
+# nfs=15000
bc8330
+# nfs4=16000
bc8330
+#
bc8330
 [exportfs]
bc8330
 # debug=0
bc8330
 #
bc8330
diff --git a/systemd/nfs.conf.man b/systemd/nfs.conf.man
bc8330
index f32c690b..ebbf28d0 100644
bc8330
--- a/systemd/nfs.conf.man
bc8330
+++ b/systemd/nfs.conf.man
bc8330
@@ -245,6 +245,17 @@ Only
bc8330
 .B debug=
bc8330
 is recognized.
bc8330
 
bc8330
+.TP
bc8330
+.B nfsrahead
bc8330
+Recognized values:
bc8330
+.BR nfs ,
bc8330
+.BR nfsv4 ,
bc8330
+.BR default .
bc8330
+
bc8330
+See
bc8330
+.BR nfsrahead (5)
bc8330
+for deatils.
bc8330
+
bc8330
 .SH FILES
bc8330
 .I /etc/nfs.conf
bc8330
 .SH SEE ALSO
bc8330
diff --git a/tools/Makefile.am b/tools/Makefile.am
bc8330
index c3feabbe..40c17c37 100644
bc8330
--- a/tools/Makefile.am
bc8330
+++ b/tools/Makefile.am
bc8330
@@ -12,6 +12,6 @@ if CONFIG_NFSDCLD
bc8330
 OPTDIRS += nfsdclddb
bc8330
 endif
bc8330
 
bc8330
-SUBDIRS = locktest rpcdebug nlmtest mountstats nfs-iostat rpcctl nfsdclnts $(OPTDIRS)
bc8330
+SUBDIRS = locktest rpcdebug nlmtest mountstats nfs-iostat rpcctl nfsdclnts nfsrahead $(OPTDIRS)
bc8330
 
bc8330
 MAINTAINERCLEANFILES = Makefile.in
bc8330
diff --git a/tools/nfsrahead/99-nfs.rules b/tools/nfsrahead/99-nfs.rules
bc8330
new file mode 100644
bc8330
index 00000000..c74914b2
bc8330
--- /dev/null
bc8330
+++ b/tools/nfsrahead/99-nfs.rules
bc8330
@@ -0,0 +1 @@
bc8330
+SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="/usr/libexec/nfsrahead %k", ATTR{read_ahead_kb}="%c"
bc8330
diff --git a/tools/nfsrahead/99-nfs.rules.in b/tools/nfsrahead/99-nfs.rules.in
bc8330
new file mode 100644
bc8330
index 00000000..648813c5
bc8330
--- /dev/null
bc8330
+++ b/tools/nfsrahead/99-nfs.rules.in
bc8330
@@ -0,0 +1 @@
bc8330
+SUBSYSTEM=="bdi", ACTION=="add", PROGRAM="_libexecdir_/nfsrahead %k", ATTR{read_ahead_kb}="%c"
bc8330
diff --git a/tools/nfsrahead/Makefile.am b/tools/nfsrahead/Makefile.am
bc8330
new file mode 100644
bc8330
index 00000000..845ea0d5
bc8330
--- /dev/null
bc8330
+++ b/tools/nfsrahead/Makefile.am
bc8330
@@ -0,0 +1,16 @@
bc8330
+libexec_PROGRAMS = nfsrahead
bc8330
+nfsrahead_SOURCES = main.c
bc8330
+nfsrahead_LDFLAGS= -lmount
bc8330
+nfsrahead_LDADD = ../../support/nfs/libnfsconf.la
bc8330
+
bc8330
+man5_MANS = nfsrahead.man
bc8330
+EXTRA_DIST = $(man5_MANS)
bc8330
+
bc8330
+udev_rulesdir = /usr/lib/udev/rules.d/
bc8330
+udev_rules_DATA = 99-nfs.rules
bc8330
+
bc8330
+99-nfs.rules: 99-nfs.rules.in $(builddefs)
bc8330
+	$(SED) "s|_libexecdir_|@libexecdir@|g" 99-nfs.rules.in > $@
bc8330
+
bc8330
+clean-local:
bc8330
+	$(RM) 99-nfs.rules
bc8330
diff --git a/tools/nfsrahead/main.c b/tools/nfsrahead/main.c
bc8330
new file mode 100644
bc8330
index 00000000..c83c6f71
bc8330
--- /dev/null
bc8330
+++ b/tools/nfsrahead/main.c
bc8330
@@ -0,0 +1,192 @@
bc8330
+#include <stdio.h>
bc8330
+#include <string.h>
bc8330
+#include <stdlib.h>
bc8330
+#include <errno.h>
bc8330
+#include <unistd.h>
bc8330
+
bc8330
+#include <libmount/libmount.h>
bc8330
+#include <sys/sysmacros.h>
bc8330
+
bc8330
+#include "xlog.h"
bc8330
+#include "conffile.h"
bc8330
+
bc8330
+#ifndef MOUNTINFO_PATH
bc8330
+#define MOUNTINFO_PATH "/proc/self/mountinfo"
bc8330
+#endif
bc8330
+
bc8330
+#define CONF_NAME "nfsrahead"
bc8330
+#define NFS_DEFAULT_READAHEAD 128
bc8330
+
bc8330
+/* Device information from the system */
bc8330
+struct device_info {
bc8330
+	char *device_number;
bc8330
+	dev_t dev;
bc8330
+	char *mountpoint;
bc8330
+	char *fstype;
bc8330
+};
bc8330
+
bc8330
+/* Convert a string in the format n:m to a device number */
bc8330
+static int fill_device_number(struct device_info *info)
bc8330
+{
bc8330
+	char *s = strdup(info->device_number), *p;
bc8330
+	char *maj_s, *min_s;
bc8330
+	unsigned int maj, min;
bc8330
+	int err = -EINVAL;
bc8330
+
bc8330
+	maj_s = p = s;
bc8330
+	for ( ; *p != ':' && *p != '\0'; p++)
bc8330
+		;
bc8330
+
bc8330
+	if (*p == '\0')
bc8330
+		goto out_free;
bc8330
+
bc8330
+	err = 0;
bc8330
+	*p = '\0';
bc8330
+	min_s = p + 1;
bc8330
+
bc8330
+	maj = strtol(maj_s, NULL, 10);
bc8330
+	min = strtol(min_s, NULL, 10);
bc8330
+
bc8330
+	info->dev = makedev(maj, min);
bc8330
+out_free:
bc8330
+	free(s);
bc8330
+	return err;
bc8330
+}
bc8330
+
bc8330
+#define sfree(ptr) if (ptr) free(ptr)
bc8330
+
bc8330
+/* device_info maintenance */
bc8330
+static void init_device_info(struct device_info *di, const char *device_number)
bc8330
+{
bc8330
+	di->device_number = strdup(device_number);
bc8330
+	di->dev = 0;
bc8330
+	di->mountpoint = NULL;
bc8330
+	di->fstype = NULL;
bc8330
+}
bc8330
+
bc8330
+
bc8330
+static void free_device_info(struct device_info *di)
bc8330
+{
bc8330
+	sfree(di->mountpoint);
bc8330
+	sfree(di->fstype);
bc8330
+	sfree(di->device_number);
bc8330
+}
bc8330
+
bc8330
+static int get_mountinfo(const char *device_number, struct device_info *device_info, const char *mountinfo_path)
bc8330
+{
bc8330
+	int ret = 0;
bc8330
+	struct libmnt_table *mnttbl;
bc8330
+	struct libmnt_fs *fs;
bc8330
+	char *target;
bc8330
+
bc8330
+	init_device_info(device_info, device_number);
bc8330
+	if ((ret = fill_device_number(device_info)) < 0)
bc8330
+		goto out_free_device_info;
bc8330
+
bc8330
+	mnttbl = mnt_new_table();
bc8330
+
bc8330
+	if ((ret = mnt_table_parse_file(mnttbl, mountinfo_path)) < 0) {
bc8330
+		xlog(D_GENERAL, "Failed to parse %s\n", mountinfo_path);
bc8330
+		goto out_free_tbl;
bc8330
+	}
bc8330
+
bc8330
+	if ((fs = mnt_table_find_devno(mnttbl, device_info->dev, MNT_ITER_FORWARD)) == NULL) {
bc8330
+		ret = ENOENT;
bc8330
+		goto out_free_tbl;
bc8330
+	}
bc8330
+
bc8330
+	if ((target = (char *)mnt_fs_get_target(fs)) == NULL) {
bc8330
+		ret = ENOENT;
bc8330
+		goto out_free_fs;
bc8330
+	}
bc8330
+
bc8330
+	device_info->mountpoint = strdup(target);
bc8330
+	target = (char *)mnt_fs_get_fstype(fs);
bc8330
+	if (target)
bc8330
+		device_info->fstype = strdup(target);
bc8330
+
bc8330
+out_free_fs:
bc8330
+	mnt_free_fs(fs);
bc8330
+out_free_tbl:
bc8330
+	mnt_free_table(mnttbl);
bc8330
+out_free_device_info:
bc8330
+	free(device_info->device_number);
bc8330
+	device_info->device_number = NULL;
bc8330
+	return ret;
bc8330
+}
bc8330
+
bc8330
+static int get_device_info(const char *device_number, struct device_info *device_info)
bc8330
+{
bc8330
+	int ret = ENOENT;
bc8330
+	for (int retry_count = 0; retry_count < 10 && ret != 0; retry_count++)
bc8330
+		ret = get_mountinfo(device_number, device_info, MOUNTINFO_PATH);
bc8330
+
bc8330
+	return ret;
bc8330
+}
bc8330
+
bc8330
+static int conf_get_readahead(const char *kind) {
bc8330
+	int readahead = 0;
bc8330
+
bc8330
+	if((readahead = conf_get_num(CONF_NAME, kind, -1)) == -1)
bc8330
+		readahead = conf_get_num(CONF_NAME, "default", NFS_DEFAULT_READAHEAD);
bc8330
+	
bc8330
+	return readahead;
bc8330
+}
bc8330
+
bc8330
+int main(int argc, char **argv)
bc8330
+{
bc8330
+	int ret = 0, retry, opt;
bc8330
+	struct device_info device;
bc8330
+	unsigned int readahead = 128, log_level, log_stderr = 0;
bc8330
+
bc8330
+
bc8330
+	log_level = D_ALL & ~D_GENERAL;
bc8330
+	while((opt = getopt(argc, argv, "dF")) != -1) {
bc8330
+		switch (opt) {
bc8330
+		case 'd':
bc8330
+			log_level = D_ALL;
bc8330
+			break;
bc8330
+		case 'F':
bc8330
+			log_stderr = 1;
bc8330
+			break;
bc8330
+		}
bc8330
+	}
bc8330
+
bc8330
+	conf_init_file(NFS_CONFFILE);
bc8330
+
bc8330
+	xlog_stderr(log_stderr);
bc8330
+	xlog_syslog(~log_stderr);
bc8330
+	xlog_config(log_level, 1);
bc8330
+	xlog_open(CONF_NAME);
bc8330
+
bc8330
+	// xlog_err causes the system to exit
bc8330
+	if ((argc - optind) != 1)
bc8330
+		xlog_err("expected the device number of a BDI; is udev ok?");
bc8330
+
bc8330
+	for (retry = 0; retry <= 10; retry++ )
bc8330
+		if ((ret = get_device_info(argv[optind], &device)) == 0)
bc8330
+			break;
bc8330
+
bc8330
+	if (ret != 0) {
bc8330
+		xlog(D_GENERAL, "unable to find device %s\n", argv[optind]);
bc8330
+		goto out;
bc8330
+	}
bc8330
+
bc8330
+	if (strncmp("nfs", device.fstype, 3) != 0) {
bc8330
+		xlog(D_GENERAL,
bc8330
+			"not setting readahead for non supported fstype %s on device %s\n",
bc8330
+			device.fstype, argv[optind]);
bc8330
+		ret = -EINVAL;
bc8330
+		goto out;
bc8330
+	}
bc8330
+
bc8330
+	readahead = conf_get_readahead(device.fstype);
bc8330
+
bc8330
+	xlog(D_FAC7, "setting %s readahead to %d\n", device.mountpoint, readahead);
bc8330
+
bc8330
+	printf("%d\n", readahead);
bc8330
+
bc8330
+out:
bc8330
+	free_device_info(&device);
bc8330
+	return ret;
bc8330
+}
bc8330
diff --git a/tools/nfsrahead/nfsrahead.man b/tools/nfsrahead/nfsrahead.man
bc8330
new file mode 100644
bc8330
index 00000000..5488f633
bc8330
--- /dev/null
bc8330
+++ b/tools/nfsrahead/nfsrahead.man
bc8330
@@ -0,0 +1,72 @@
bc8330
+.\" Manpage for nfsrahead.
bc8330
+.nh
bc8330
+.ad l
bc8330
+.TH man 5 "08 Mar 2022" "1.0" "nfsrahead man page"
bc8330
+.SH NAME
bc8330
+
bc8330
+nfsrahead \- Configure the readahead for NFS mounts
bc8330
+
bc8330
+.SH SYNOPSIS
bc8330
+
bc8330
+nfsrahead [-F] [-d] <device>
bc8330
+
bc8330
+.SH DESCRIPTION
bc8330
+
bc8330
+\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.
bc8330
+
bc8330
+.SH OPTIONS
bc8330
+.TP
bc8330
+.B -F
bc8330
+Send messages to 
bc8330
+.I stderr 
bc8330
+instead of
bc8330
+.I syslog
bc8330
+
bc8330
+.TP
bc8330
+.B -d
bc8330
+Increase the debugging level.
bc8330
+
bc8330
+.SH CONFIGURATION
bc8330
+.I nfsrahead
bc8330
+is configured in
bc8330
+.IR /etc/nfs.conf ,
bc8330
+in the section titled
bc8330
+.IR nfsrahead .
bc8330
+It accepts the following configurations.
bc8330
+
bc8330
+.TP
bc8330
+.B nfs=<value>
bc8330
+The readahead value applied to NFSv3 mounts.
bc8330
+
bc8330
+.TP
bc8330
+.B nfs4=<value>
bc8330
+The readahead value applied to NFSv4 mounts.
bc8330
+
bc8330
+.TP
bc8330
+.B default=<value>
bc8330
+The default configuration when none of the configurations above is set.
bc8330
+
bc8330
+.SH EXAMPLE CONFIGURATION
bc8330
+[nfsrahead]
bc8330
+.br
bc8330
+nfs=15000              # readahead of 15000 for NFSv3 mounts
bc8330
+.br
bc8330
+nfs4=16000             # readahead of 16000 for NFSv4 mounts
bc8330
+.br
bc8330
+default=128            # default is 128
bc8330
+
bc8330
+.SH SEE ALSO
bc8330
+
bc8330
+.BR mount.nfs (8),
bc8330
+.BR nfs (5),
bc8330
+.BR nfs.conf (5),
bc8330
+.BR udev (7),
bc8330
+.BR bcc-readahead (8)
bc8330
+
bc8330
+.SH BUGS
bc8330
+
bc8330
+No known bugs.
bc8330
+
bc8330
+.SH AUTHOR
bc8330
+
bc8330
+Thiago Rafael Becker <trbecker@gmail.com>
bc8330
diff --git a/utils/nfsidmap/nfsidmap.man b/utils/nfsidmap/nfsidmap.man
bc8330
index 2af16f31..1911c41b 100644
bc8330
--- a/utils/nfsidmap/nfsidmap.man
bc8330
+++ b/utils/nfsidmap/nfsidmap.man
bc8330
@@ -2,7 +2,7 @@
bc8330
 .\"@(#)nfsidmap(8) - The NFS idmapper upcall program
bc8330
 .\"
bc8330
 .\" Copyright (C) 2010 Bryan Schumaker <bjschuma@netapp.com>
bc8330
-.TH nfsidmap 5 "1 October 2010"
bc8330
+.TH nfsidmap 8 "1 October 2010"
bc8330
 .SH NAME
bc8330
 nfsidmap \- The NFS idmapper upcall program
bc8330
 .SH SYNOPSIS