[PATCH] zram: check compressor name before setting it

From: Marcin Jabrzyk
Date: Fri May 22 2015 - 04:32:16 EST


Zram sysfs interface was not making any check of
proper compressor name when setting it.
Any name is accepted, but further tries of device
creation would end up with not very meaningfull error.
eg.

echo lz0 > comp_algorithm
echo 200M > disksize
echo: write error: Invalid argument

This commit fixes that behaviour with returning
EINVAL and proper error message.

Signed-off-by: Marcin Jabrzyk <m.jabrzyk@xxxxxxxxxxx>
---
drivers/block/zram/zcomp.c | 22 +++++++++++-----------
drivers/block/zram/zcomp.h | 1 +
drivers/block/zram/zram_drv.c | 5 +++++
3 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/drivers/block/zram/zcomp.c b/drivers/block/zram/zcomp.c
index f1ff39a3d1c1..f81a2b5fef43 100644
--- a/drivers/block/zram/zcomp.c
+++ b/drivers/block/zram/zcomp.c
@@ -51,17 +51,6 @@ static struct zcomp_backend *backends[] = {
NULL
};

-static struct zcomp_backend *find_backend(const char *compress)
-{
- int i = 0;
- while (backends[i]) {
- if (sysfs_streq(compress, backends[i]->name))
- break;
- i++;
- }
- return backends[i];
-}
-
static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
{
if (zstrm->private)
@@ -267,6 +256,17 @@ static int zcomp_strm_single_create(struct zcomp *comp)
return 0;
}

+struct zcomp_backend *find_backend(const char *compress)
+{
+ int i = 0;
+ while (backends[i]) {
+ if (sysfs_streq(compress, backends[i]->name))
+ break;
+ i++;
+ }
+ return backends[i];
+}
+
/* show available compressors */
ssize_t zcomp_available_show(const char *comp, char *buf)
{
diff --git a/drivers/block/zram/zcomp.h b/drivers/block/zram/zcomp.h
index c59d1fca72c0..a531350858d0 100644
--- a/drivers/block/zram/zcomp.h
+++ b/drivers/block/zram/zcomp.h
@@ -50,6 +50,7 @@ struct zcomp {
void (*destroy)(struct zcomp *comp);
};

+struct zcomp_backend *find_backend(const char *compress);
ssize_t zcomp_available_show(const char *comp, char *buf);

struct zcomp *zcomp_create(const char *comp, int max_strm);
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 01ec6945c2a9..ef4acd6e52d1 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -268,6 +268,11 @@ static ssize_t comp_algorithm_store(struct device *dev,
{
struct zram *zram = dev_to_zram(dev);
down_write(&zram->init_lock);
+ if (!find_backend(buf)) {
+ up_write(&zram->init_lock);
+ pr_info("There is no such compression algorithm\n");
+ return -EINVAL;
+ }
if (init_done(zram)) {
up_write(&zram->init_lock);
pr_info("Can't change algorithm for initialized device\n");
--
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/