[PATCH] ubi-utils: add wear-leveling threshold parameter to ubiattach

Ran Hongyun posted 1 patch 1 day, 2 hours ago
include/libubi.h       |  2 ++
include/mtd/ubi-user.h |  3 ++-
lib/libubi.c           |  1 +
ubi-utils/ubiattach.c  | 24 +++++++++++++++++++++++-
4 files changed, 28 insertions(+), 2 deletions(-)
[PATCH] ubi-utils: add wear-leveling threshold parameter to ubiattach
Posted by Ran Hongyun 1 day, 2 hours ago
Add wl_threshold support end-to-end: expose the kernel's wear-leveling
threshold via a new --wl-threshold / -w option in ubiattach, plumb it
through libubi, and define it by replacing 4 bytes of padding in
struct ubi_attach_req.

Valid range is 2-65536; 0 means use the kernel default.

Signed-off-by: Ran Hongyun <ranhongyun1@huawei.com>
---
 include/libubi.h       |  2 ++
 include/mtd/ubi-user.h |  3 ++-
 lib/libubi.c           |  1 +
 ubi-utils/ubiattach.c  | 24 +++++++++++++++++++++++-
 4 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/include/libubi.h b/include/libubi.h
index b5b3d8f..9203416 100644
--- a/include/libubi.h
+++ b/include/libubi.h
@@ -56,6 +56,7 @@ typedef void * libubi_t;
  * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  * @disable_fm: whether disable fastmap
  * @need_resv_pool: whether reserve free pebs for filling pool/wl_pool
+ * @wl_threshold: wear-leveling threshold (0 means use kernel default)
  */
 struct ubi_attach_request
 {
@@ -66,6 +67,7 @@ struct ubi_attach_request
 	int max_beb_per1024;
 	bool disable_fm;
 	bool need_resv_pool;
+	int wl_threshold;
 };
 
 /**
diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h
index bb5c0f9..601f3f4 100644
--- a/include/mtd/ubi-user.h
+++ b/include/mtd/ubi-user.h
@@ -284,7 +284,8 @@ struct ubi_attach_req {
 	int16_t max_beb_per1024;
 	int8_t	disable_fm;
 	int8_t  need_resv_pool;
-	int8_t  padding[8];
+	int32_t wl_threshold;
+	int8_t  padding[4];
 };
 
 /*
diff --git a/lib/libubi.c b/lib/libubi.c
index 86736dd..5d55148 100644
--- a/lib/libubi.c
+++ b/lib/libubi.c
@@ -769,6 +769,7 @@ int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req)
 	r.vid_hdr_offset = req->vid_hdr_offset;
 	r.disable_fm = req->disable_fm ? 1 : 0;
 	r.need_resv_pool = req->need_resv_pool ? 1 : 0;
+	r.wl_threshold = req->wl_threshold;
 
 	if (req->max_beb_per1024) {
 		/*
diff --git a/ubi-utils/ubiattach.c b/ubi-utils/ubiattach.c
index 11e171b..0789a5f 100644
--- a/ubi-utils/ubiattach.c
+++ b/ubi-utils/ubiattach.c
@@ -44,6 +44,7 @@ struct args {
 	int max_beb_per1024;
 	bool disable_fm;
 	bool need_resv_pool;
+	int wl_threshold;
 };
 
 static struct args args = {
@@ -55,6 +56,7 @@ static struct args args = {
 	.max_beb_per1024 = 0,
 	.disable_fm = false,
 	.need_resv_pool = false,
+	.wl_threshold = 0,
 };
 
 static const char doc[] = PROGRAM_NAME " version " VERSION
@@ -76,6 +78,9 @@ static const char optionsstr[] =
 "-r, --reserve-pool    Slow down the frequency of updating fastmap by reserving\n"
 "                      pebs for filling pool/wl_pool, which can prolong flash\n"
 "                      service life.\n"
+"-w, --wl-threshold    Set the wear-leveling threshold for this UBI device.\n"
+"                      If 0, the kernel default value is used.\n"
+"                      Accepted range is 2-65536.\n"
 "-h, --help            print help message\n"
 "-V, --version         print program version";
 
@@ -85,6 +90,7 @@ static const char usage[] =
 "\t[--mtdn=<MTD device number>] [--devn=<UBI device number>]\n"
 "\t[--dev-path=<path to device>] [-f] [--disable-fastmap] [-r] [--reserve-pool]\n"
 "\t[--max-beb-per1024=<maximum bad block number per 1024 blocks>]\n"
+"\t[--wl-threshold=<wear-leveling threshold>]\n"
 "UBI control device defaults to " DEFAULT_CTRL_DEV " if not supplied.\n"
 "Example 1: " PROGRAM_NAME " -p /dev/mtd0 - attach /dev/mtd0 to UBI\n"
 "Example 2: " PROGRAM_NAME " -m 0 - attach MTD device 0 (mtd0) to UBI\n"
@@ -104,6 +110,7 @@ static const struct option long_options[] = {
 	{ .name = "max-beb-per1024", .has_arg = 1, .flag = NULL, .val = 'b' },
 	{ .name = "disable-fastmap", .has_arg = 0, .flag = NULL, .val = 'f' },
 	{ .name = "reserve-pool",    .has_arg = 0, .flag = NULL, .val = 'r' },
+	{ .name = "wl-threshold",    .has_arg = 1, .flag = NULL, .val = 'w' },
 	{ .name = "help",            .has_arg = 0, .flag = NULL, .val = 'h' },
 	{ .name = "version",         .has_arg = 0, .flag = NULL, .val = 'V' },
 	{ NULL, 0, NULL, 0},
@@ -114,7 +121,7 @@ static int parse_opt(int argc, char * const argv[])
 	while (1) {
 		int key, error = 0;
 
-		key = getopt_long(argc, argv, "p:m:d:O:b:frhV", long_options, NULL);
+		key = getopt_long(argc, argv, "p:m:d:O:b:frw:hV", long_options, NULL);
 		if (key == -1)
 			break;
 
@@ -162,6 +169,20 @@ static int parse_opt(int argc, char * const argv[])
 			args.need_resv_pool = true;
 			break;
 
+		case 'w':
+		{
+			unsigned long tmp = simple_strtoul(optarg, &error);
+
+			if (error || (tmp && (tmp < 2 || tmp > 65536)))
+				return errmsg("bad wear-leveling threshold: \"%s\" (2-65536)",
+					optarg);
+			if (tmp == 0)
+				warnmsg("wear-leveling threshold use the default kernel value");
+			args.wl_threshold = tmp;
+
+			break;
+		}
+
 		case 'h':
 			printf("%s\n\n", doc);
 			printf("%s\n\n", usage);
@@ -234,6 +255,7 @@ int main(int argc, char * const argv[])
 	req.max_beb_per1024 = args.max_beb_per1024;
 	req.disable_fm = args.disable_fm;
 	req.need_resv_pool = args.need_resv_pool;
+	req.wl_threshold = args.wl_threshold;
 
 	err = ubi_attach(libubi, args.node, &req);
 	if (err < 0) {
-- 
2.52.0