All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] zramctl: add hot_add device support
@ 2016-05-11  9:20 Timofey Titovets
  2016-05-11 11:30 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Timofey Titovets @ 2016-05-11  9:20 UTC (permalink / raw)
  To: util-linux

[-- Attachment #1: Type: text/plain, Size: 473 bytes --]

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.

[-- Attachment #2: 0001-zramctl-add-hot_add-device-support.patch --]
[-- Type: text/x-patch, Size: 2611 bytes --]

From 0bc18882fbe774018aa0e61c3d53f34d52a7cb8c Mon Sep 17 00:00:00 2001
From: Timofey Titovets <nefelim4ag@gmail.com>
Date: Wed, 11 May 2016 11:05:07 +0300
Subject: [PATCH] zramctl: add hot_add device support

Signed-off-by: Timofey Titovets <nefelim4ag@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] zramctl: add hot_add device support
  2016-05-11  9:20 [PATCH] zramctl: add hot_add device support Timofey Titovets
@ 2016-05-11 11:30 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2016-05-11 11:30 UTC (permalink / raw)
  To: Timofey Titovets; +Cc: util-linux

On Wed, May 11, 2016 at 12:20:00PM +0300, Timofey Titovets wrote:
> 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! I have applied a little bit different solution.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-05-11 11:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-11  9:20 [PATCH] zramctl: add hot_add device support Timofey Titovets
2016-05-11 11:30 ` Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.