Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Weinberger <richard@nod.at>
To: linux-mtd@lists.infradead.org
Cc: Richard Weinberger <richard@nod.at>, david.oberhollenzer@sigma-star.at
Subject: [PATCH] ubi-utils: Implement a ubihealthd
Date: Thu, 25 Jul 2019 22:34:42 +0200	[thread overview]
Message-ID: <20190725203442.29795-1-richard@nod.at> (raw)

ubihealthd is a simple daemon which scans every PEB
of an UBI device in random order.
It helps to deal with read disturb on systems which either
reboot seldom, use fastmap or read few data.

To use this daemon you need Linux >= v5.1.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 ubi-utils/Makemodule.am |   6 +-
 ubi-utils/ubihealthd.c  | 272 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 277 insertions(+), 1 deletion(-)
 create mode 100644 ubi-utils/ubihealthd.c

diff --git a/ubi-utils/Makemodule.am b/ubi-utils/Makemodule.am
index 215eac27f5a5..984e2cd79287 100644
--- a/ubi-utils/Makemodule.am
+++ b/ubi-utils/Makemodule.am
@@ -37,9 +37,13 @@ ubirsvol_LDADD = libmtd.a libubi.a
 ubiblock_SOURCES = ubi-utils/ubiblock.c
 ubiblock_LDADD = libmtd.a libubi.a
 
+ubihealthd_SOURCES = ubi-utils/ubihealthd.c
+ubihealthd_LDADD = libmtd.a libubi.a
+
 UBI_BINS = \
 	ubiupdatevol ubimkvol ubirmvol ubicrc32 ubinfo ubiattach \
-	ubidetach ubinize ubiformat ubirename mtdinfo ubirsvol ubiblock
+	ubidetach ubinize ubiformat ubirename mtdinfo ubirsvol ubiblock \
+	ubihealthd
 
 UBI_MAN = \
 	ubi-utils/ubinize.8
diff --git a/ubi-utils/ubihealthd.c b/ubi-utils/ubihealthd.c
new file mode 100644
index 000000000000..3e665be455ac
--- /dev/null
+++ b/ubi-utils/ubihealthd.c
@@ -0,0 +1,272 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <inttypes.h>
+#include <mtd/ubi-user.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <syslog.h>
+#include <sys/random.h>
+#include <sys/signalfd.h>
+#include <sys/stat.h>
+#include <sys/timerfd.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#define PROGRAM_NAME "ubihealthd"
+
+#include "libubi.h"
+#include "common.h"
+
+#ifndef UBI_IOCRPEB
+#define UBI_IOCRPEB _IOW(UBI_IOC_MAGIC, 4, int32_t)
+#endif
+
+struct peb_state {
+	int alive;
+	int pnum;
+	int last_errno;
+};
+
+static struct peb_state **peb_state_array;
+static int peb_state_array_len;
+static int cur_pos;
+static const char *ubi_device = "/dev/ubi0";
+static int ubi_fd;
+static int interval_secs = 120;
+static int nodaemon;
+
+static const char opt_string[] = "d:i:f";
+static const struct option options[] = {
+        {
+                .name = "device",
+                .has_arg = required_argument,
+                .flag = NULL,
+                .val = 'd'
+        },
+        {
+                .name = "interval",
+                .has_arg = required_argument,
+                .flag = NULL,
+                .val = 'i'
+        },
+};
+
+static void dolog(const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	if (nodaemon)
+		vfprintf(stderr, fmt, ap);
+	else
+		vsyslog(LOG_DAEMON | LOG_WARNING, fmt, ap);
+	va_end(ap);
+}
+
+static void build_peb_list(void)
+{
+	int i, pos;
+	struct peb_state *ps;
+
+	peb_state_array = xmalloc(sizeof(ps) * peb_state_array_len);
+
+	for (i = 0; i < peb_state_array_len; i++) {
+		ps = xmalloc(sizeof(*ps));
+
+		ps->pnum = i;
+		ps->last_errno = 0;
+		ps->alive = 1;
+
+		peb_state_array[i] = ps;
+	}
+
+	/* Shuffle the list */
+	for (i = 0; i < peb_state_array_len; i++) {
+		pos = rand() % peb_state_array_len;
+
+		ps = peb_state_array[pos];
+		peb_state_array[pos] = peb_state_array[i];
+		peb_state_array[i] = ps;
+	}
+}
+
+static struct peb_state *__next_peb(void)
+{
+	struct peb_state *ps = peb_state_array[cur_pos];
+
+	cur_pos++;
+	if (cur_pos >= peb_state_array_len)
+		cur_pos = 0;
+
+	return ps;
+}
+
+static struct peb_state *next_peb(void)
+{
+	int i;
+	struct peb_state *ps;
+
+	/* Find next PEB in our list, skip bad PEBs */
+	for (i = 0; i < peb_state_array_len; i++) {
+		ps = __next_peb();
+		if (ps->alive)
+			return ps;
+	}
+
+	dolog("Fatal: All PEBs are gone?!\n");
+	exit(1);
+
+	return NULL;
+}
+
+static int process_one_peb(void)
+{
+	int rc;
+	struct peb_state *ps = next_peb();
+
+	rc = ioctl(ubi_fd, UBI_IOCRPEB, &ps->pnum);
+	if (!rc)
+		return 0;
+	else
+		rc = errno;
+
+	switch (rc) {
+	case EINVAL: {
+		dolog("Unable to check PEB %i for unknown reason!\n", ps->pnum);
+		break;
+	}
+	case ENOENT: {
+		/* UBI ignores this PEB */
+		ps->alive = 0;
+		break;
+	}
+	case EBUSY: {
+		if (ps->last_errno == rc)
+			dolog("Warning: Unable to check PEB %i\n", ps->pnum);
+		break;
+	}
+	case EAGAIN: {
+		if (ps->last_errno == rc)
+			dolog("Warning: PEB %i has bitflips, but cannot scrub!\n", ps->pnum);
+		break;
+	}
+	case EUCLEAN: {
+		/* Scrub happened */
+		break;
+	}
+	case ENOTTY: {
+		dolog("Fatal: Kernel does not support this interface. Too old kernel?\n");
+		exit(1);
+		break;
+	}
+	case ENODEV: {
+		dolog("Fatal: UBI device vanished under us.\n");
+		exit(1);
+	}
+	default:
+		dolog("Warning: Unknown return code from kernel: %i\n", rc);
+	}
+
+	ps->last_errno = rc;
+
+	return 0;
+}
+
+static int get_peb_count(void)
+{
+	libubi_t libubi = libubi_open();
+	struct ubi_dev_info dev_info;
+
+	if (!libubi) {
+		fprintf(stderr, "Unable to init libubi, is UBI present?\n");
+		exit(1);
+	}
+
+	if (ubi_get_dev_info(libubi, ubi_device, &dev_info)) {
+		fprintf(stderr, "Fatal: Could not get ubi info for %s\n", ubi_device);
+		exit(1);
+	}
+
+	libubi_close(libubi);
+
+	return dev_info.total_lebs;
+}
+
+static void init_prng(void)
+{
+	int ret, seed;
+
+	ret = getrandom(&seed, sizeof(seed), 0);
+	if (ret != sizeof(seed)) {
+		if (ret == -1)
+			fprintf(stderr, "Unable to get random seed: %m\n");
+		else
+			fprintf(stderr, "Unable to get %zi bytes random seed\n", sizeof(seed));
+
+		exit(1);
+	}
+	srand(seed);
+}
+
+int main (int argc, char *argv[])
+{
+	int c, i;
+
+	while ((c = getopt_long(argc, argv, opt_string, options, &i)) != -1) {
+		switch(c) {
+		case 'd': {
+			ubi_device = optarg;
+			break;
+		}
+		case 'i': {
+			interval_secs = atoi(optarg);
+			if (!interval_secs) {
+				fprintf(stderr, "Bad interval value! %s\n", optarg);
+				exit(1);
+			}
+			break;
+		}
+		case 'f': {
+			nodaemon = 1;
+			break;
+		}
+		case '?':
+		default:
+			fprintf(stderr, "Usage: %s [ -d UBI_DEVICE ] [-i INTERVAL_SEC ] [ -f ]\n", argv[0]);
+			exit(1);
+			break;
+		}
+	}
+
+	ubi_fd = open(ubi_device, O_RDONLY);
+	if (ubi_fd == -1) {
+		fprintf(stderr, "Fatal: Unable to open %s: %m\n", ubi_device);
+		exit(1);
+	}
+
+	init_prng();
+
+	peb_state_array_len = get_peb_count();
+	build_peb_list();
+
+	if (!nodaemon) {
+		if (daemon(0, 0) == -1) {
+			fprintf(stderr, "Unable to become a daemon: %m\n");
+			exit(1);
+		}
+	}
+
+	for (;;) {
+		process_one_peb();
+		sleep(interval_secs);
+	}
+
+	return 0;
+}
-- 
2.16.4


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

             reply	other threads:[~2019-07-25 20:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-25 20:34 Richard Weinberger [this message]
2019-08-25 11:58 ` [PATCH] ubi-utils: Implement a ubihealthd David Oberhollenzer
2019-09-26  8:30 ` Alexander Dahl
2019-09-26 11:44   ` Richard Weinberger
2019-09-26 14:00     ` Richard Weinberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190725203442.29795-1-richard@nod.at \
    --to=richard@nod.at \
    --cc=david.oberhollenzer@sigma-star.at \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox