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

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