[PATCH] zramctl: add hot_add device support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi guys,
In linux kernel 4.2, has been added support for hot add/remove zram device

I've added:
1. Function to wrap this functionality
2. Auto add new zram devices if no one free in way like losetup -f
i.e. zramctl -f -s 1G try to find free device -> try add new -> return
new if successful

My email client eat code formatting, so i attach patch
Also you can see changes in github:
https://github.com/karelzak/util-linux/issues/318

Thanks.
-- 
Have a nice day,
Timofey.
From 0bc18882fbe774018aa0e61c3d53f34d52a7cb8c Mon Sep 17 00:00:00 2001
From: Timofey Titovets <nefelim4ag@xxxxxxxxx>
Date: Wed, 11 May 2016 11:05:07 +0300
Subject: [PATCH] zramctl: add hot_add device support

Signed-off-by: Timofey Titovets <nefelim4ag@xxxxxxxxx>
---
 include/sysfs.h     |  1 +
 lib/sysfs.c         | 21 +++++++++++++++++++++
 sys-utils/zramctl.c | 11 +++++++++--
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/include/sysfs.h b/include/sysfs.h
index 5521378..3573ca9 100644
--- a/include/sysfs.h
+++ b/include/sysfs.h
@@ -90,6 +90,7 @@ extern char *sysfs_scsi_host_strdup_attribute(struct sysfs_cxt *cxt,
 extern int sysfs_scsi_host_is(struct sysfs_cxt *cxt, const char *type);
 extern int sysfs_scsi_has_attribute(struct sysfs_cxt *cxt, const char *attr);
 extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern);
+extern int sysfs_zram_control(const char *attr, const int num);
 
 /**
  * sysfs_devname_sys_to_dev:
diff --git a/lib/sysfs.c b/lib/sysfs.c
index 9e973a4..5e08242 100644
--- a/lib/sysfs.c
+++ b/lib/sysfs.c
@@ -15,6 +15,7 @@
 #include "sysfs.h"
 #include "fileutils.h"
 #include "all-io.h"
+#include "path.h"
 
 char *sysfs_devno_attribute_path(dev_t devno, char *buf,
 				 size_t bufsiz, const char *attr)
@@ -1014,6 +1015,26 @@ int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern)
 	return strstr(linkc, pattern) != NULL;
 }
 
+int sysfs_zram_control(const char *attr, const int num)
+{
+	int ret;
+	char zram_num[12];
+
+	if (strncmp(attr, "hot_add", 7) == 0) {
+		ret = path_read_s32(_PATH_SYS_CLASS "/zram-control/hot_add");
+		return ret;
+	}
+
+	if (strncmp(attr, "hot_remove", 10) == 0) {
+		snprintf(zram_num, sizeof(zram_num), "%i", num);
+		ret = path_write_str(zram_num,
+							_PATH_SYS_CLASS "/zram-control/hot_remove");
+		return ret;
+	}
+
+	return ENOENT;
+}
+
 #ifdef TEST_PROGRAM_SYSFS
 #include <errno.h>
 #include <err.h>
diff --git a/sys-utils/zramctl.c b/sys-utils/zramctl.c
index 29041eb..0f366a8 100644
--- a/sys-utils/zramctl.c
+++ b/sys-utils/zramctl.c
@@ -266,12 +266,19 @@ static struct zram *find_free_zram(void)
 	struct zram *z = new_zram(NULL);
 	size_t i;
 	int isfree = 0;
+	int new_dev;
 
 	for (i = 0; isfree == 0; i++) {
 		DBG(fprintf(stderr, "find free: checking zram%zu", i));
 		zram_set_devname(z, NULL, i);
-		if (!zram_exist(z))
-			break;
+
+		if (!zram_exist(z)) {
+			new_dev = sysfs_zram_control("hot_add", 0);
+			if ( new_dev >= 0) {
+				zram_set_devname(z, NULL, new_dev);
+			} else
+				break;
+		}
 		isfree = !zram_used(z);
 	}
 	if (!isfree) {
-- 
2.8.2


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux