Re: [PATCH RFC] brd: Add miscdevice to control creation and deletion of ramdisks

2012-10-13 Thread Hannes Frederic Sowa
On Sun, Oct 14, 2012 at 04:48:36AM +0200, Hannes Frederic Sowa wrote:
> + case BRD_CTL_DEL:
> + error = get_user(val, (int __user *)param);
> + if (error < 0)
> + break;
> + if ((val & max_part) != 0) {
> + error = -EINVAL;
> + break;
> + }
> + val >>= part_shift;
> + brd2 = NULL;
> + list_for_each_entry(brd, _devices, brd_list) {
> + if (brd->brd_number == val) {
> + brd2 = brd;
> + break;
> + }
> + }
> + if (brd2 == NULL) {
> + error = -ENODEV;
> + break;
> + }
> + brd_del_one(brd2);
> + break;

Sorry, first bug spotted. I should check the bd_openers before destroying the
device. Will be fixed in a follow-up patch, if needed.

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


[PATCH RFC] brd: Add miscdevice to control creation and deletion of ramdisks

2012-10-13 Thread Hannes Frederic Sowa
While toying around with a library which needs small block devices for
quorum management, I came up with this small patch to dynamically create
and destroy ram disks (I wanted to have non-swappable backed devices).

I am not really comfortable with the the double-use of the ioctl parameter
of BRD_CTL_ADD passing the size and returning the minor id. But it seemed
the easiest way to go.  I even don't know if going the ioctl-route is
the right way.

Currently new minor ids are allocated strictly sequential. Deleted ones
won't get reused. If the ioctl way is the way to go, I may change the
bookkeeping to idrs. Feedback appreciated!

This patch is against 3.6.

This patch adds the miscdevice /dev/brd-control with two ioctls:

  1) BRD_CTL_ADD: Instantiates a new ram disk with a given size as
 parameter. This parameter is filled in with the new minor id
 on return.

  2) BRD_CTL_DEL: Deletes a ram disk. Takes the minor id as parameter.

Signed-off-by: Hannes Frederic Sowa 
---
 Documentation/ioctl/ioctl-number.txt |   1 +
 drivers/block/brd.c  | 115 ---
 include/linux/Kbuild |   1 +
 include/linux/brd.h  |  11 
 include/linux/miscdevice.h   |   1 +
 5 files changed, 122 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/brd.h

diff --git a/Documentation/ioctl/ioctl-number.txt 
b/Documentation/ioctl/ioctl-number.txt
index 849b771..b8827eb 100644
--- a/Documentation/ioctl/ioctl-number.txt
+++ b/Documentation/ioctl/ioctl-number.txt
@@ -326,4 +326,5 @@ Code  Seq#(hex) Include FileComments

 0xF6   all LTTng   Linux Trace Toolkit Next Generation

+0xF7   00-01   drivers/block/brd.c block ram device control driver
 0xFD   all linux/dm-ioctl.h
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 531ceb3..7401de7 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -19,6 +19,9 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 
 #include 
 
@@ -460,7 +463,7 @@ __setup("ramdisk_size=", ramdisk_size);
 static LIST_HEAD(brd_devices);
 static DEFINE_MUTEX(brd_devices_mutex);
 
-static struct brd_device *brd_alloc(int i)
+static struct brd_device *brd_alloc(int i, int size_kb)
 {
struct brd_device *brd;
struct gendisk *disk;
@@ -494,7 +497,7 @@ static struct brd_device *brd_alloc(int i)
disk->queue = brd->brd_queue;
disk->flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
sprintf(disk->disk_name, "ram%d", i);
-   set_capacity(disk, rd_size * 2);
+   set_capacity(disk, size_kb * 2);
 
return brd;
 
@@ -523,7 +526,7 @@ static struct brd_device *brd_init_one(int i)
goto out;
}
 
-   brd = brd_alloc(i);
+   brd = brd_alloc(i, rd_size);
if (brd) {
add_disk(brd->brd_disk);
list_add_tail(>brd_list, _devices);
@@ -553,9 +556,97 @@ static struct kobject *brd_probe(dev_t dev, int *part, 
void *data)
return kobj;
 }
 
+static long brd_control_ioctl(struct file *file, unsigned int cmd,
+   unsigned long param)
+{
+   int error = -ENOSYS;
+   int size_kb, val;
+   struct brd_device *brd, *brd2;
+
+   if (rd_nr)
+   return -EINVAL;
+
+   mutex_lock(_devices_mutex);
+   switch (cmd) {
+   case BRD_CTL_ADD:
+   error = get_user(size_kb, (int __user *)param);
+   if (error < 0)
+   break;
+   if (size_kb < 0) {
+   error = -EINVAL;
+   break;
+   }
+   val = 0;
+   list_for_each_entry(brd, _devices, brd_list) {
+   val = max(val, 1 + brd->brd_number);
+   }
+   if (val >= (MINORMASK >> part_shift)) {
+   error = -EINVAL;
+   break;
+   }
+   brd = brd_alloc(val, size_kb);
+   if (brd == NULL) {
+   error = -ENOMEM;
+   break;
+   }
+   list_add_tail(>brd_list, _devices);
+   add_disk(brd->brd_disk);
+   val <<= part_shift;
+   error = put_user(val, (int __user *)param);
+   if (error < 0)
+   break;
+   error = 0;
+   break;
+   case BRD_CTL_DEL:
+   error = get_user(val, (int __user *)param);
+   if (error < 0)
+   break;
+   if ((val & max_part) != 0) {
+   error = -EINVAL;
+   break;
+   }
+   val >>= part_shift;
+   brd2 = NULL;
+   list_for_each_entry(brd, 

[PATCH RFC] brd: Add miscdevice to control creation and deletion of ramdisks

2012-10-13 Thread Hannes Frederic Sowa
While toying around with a library which needs small block devices for
quorum management, I came up with this small patch to dynamically create
and destroy ram disks (I wanted to have non-swappable backed devices).

I am not really comfortable with the the double-use of the ioctl parameter
of BRD_CTL_ADD passing the size and returning the minor id. But it seemed
the easiest way to go.  I even don't know if going the ioctl-route is
the right way.

Currently new minor ids are allocated strictly sequential. Deleted ones
won't get reused. If the ioctl way is the way to go, I may change the
bookkeeping to idrs. Feedback appreciated!

This patch is against 3.6.

This patch adds the miscdevice /dev/brd-control with two ioctls:

  1) BRD_CTL_ADD: Instantiates a new ram disk with a given size as
 parameter. This parameter is filled in with the new minor id
 on return.

  2) BRD_CTL_DEL: Deletes a ram disk. Takes the minor id as parameter.

Signed-off-by: Hannes Frederic Sowa han...@stressinduktion.org
---
 Documentation/ioctl/ioctl-number.txt |   1 +
 drivers/block/brd.c  | 115 ---
 include/linux/Kbuild |   1 +
 include/linux/brd.h  |  11 
 include/linux/miscdevice.h   |   1 +
 5 files changed, 122 insertions(+), 7 deletions(-)
 create mode 100644 include/linux/brd.h

diff --git a/Documentation/ioctl/ioctl-number.txt 
b/Documentation/ioctl/ioctl-number.txt
index 849b771..b8827eb 100644
--- a/Documentation/ioctl/ioctl-number.txt
+++ b/Documentation/ioctl/ioctl-number.txt
@@ -326,4 +326,5 @@ Code  Seq#(hex) Include FileComments
mailto:r...@8d.com
 0xF6   all LTTng   Linux Trace Toolkit Next Generation
mailto:mathieu.desnoy...@efficios.com
+0xF7   00-01   drivers/block/brd.c block ram device control driver
 0xFD   all linux/dm-ioctl.h
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 531ceb3..7401de7 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -19,6 +19,9 @@
 #include linux/radix-tree.h
 #include linux/fs.h
 #include linux/slab.h
+#include linux/kernel.h
+#include linux/miscdevice.h
+#include linux/brd.h
 
 #include asm/uaccess.h
 
@@ -460,7 +463,7 @@ __setup(ramdisk_size=, ramdisk_size);
 static LIST_HEAD(brd_devices);
 static DEFINE_MUTEX(brd_devices_mutex);
 
-static struct brd_device *brd_alloc(int i)
+static struct brd_device *brd_alloc(int i, int size_kb)
 {
struct brd_device *brd;
struct gendisk *disk;
@@ -494,7 +497,7 @@ static struct brd_device *brd_alloc(int i)
disk-queue = brd-brd_queue;
disk-flags |= GENHD_FL_SUPPRESS_PARTITION_INFO;
sprintf(disk-disk_name, ram%d, i);
-   set_capacity(disk, rd_size * 2);
+   set_capacity(disk, size_kb * 2);
 
return brd;
 
@@ -523,7 +526,7 @@ static struct brd_device *brd_init_one(int i)
goto out;
}
 
-   brd = brd_alloc(i);
+   brd = brd_alloc(i, rd_size);
if (brd) {
add_disk(brd-brd_disk);
list_add_tail(brd-brd_list, brd_devices);
@@ -553,9 +556,97 @@ static struct kobject *brd_probe(dev_t dev, int *part, 
void *data)
return kobj;
 }
 
+static long brd_control_ioctl(struct file *file, unsigned int cmd,
+   unsigned long param)
+{
+   int error = -ENOSYS;
+   int size_kb, val;
+   struct brd_device *brd, *brd2;
+
+   if (rd_nr)
+   return -EINVAL;
+
+   mutex_lock(brd_devices_mutex);
+   switch (cmd) {
+   case BRD_CTL_ADD:
+   error = get_user(size_kb, (int __user *)param);
+   if (error  0)
+   break;
+   if (size_kb  0) {
+   error = -EINVAL;
+   break;
+   }
+   val = 0;
+   list_for_each_entry(brd, brd_devices, brd_list) {
+   val = max(val, 1 + brd-brd_number);
+   }
+   if (val = (MINORMASK  part_shift)) {
+   error = -EINVAL;
+   break;
+   }
+   brd = brd_alloc(val, size_kb);
+   if (brd == NULL) {
+   error = -ENOMEM;
+   break;
+   }
+   list_add_tail(brd-brd_list, brd_devices);
+   add_disk(brd-brd_disk);
+   val = part_shift;
+   error = put_user(val, (int __user *)param);
+   if (error  0)
+   break;
+   error = 0;
+   break;
+   case BRD_CTL_DEL:
+   error = get_user(val, (int __user *)param);
+   if (error  0)
+   break;
+   if ((val  max_part) != 0) {
+   error = -EINVAL;
+   break;
+ 

Re: [PATCH RFC] brd: Add miscdevice to control creation and deletion of ramdisks

2012-10-13 Thread Hannes Frederic Sowa
On Sun, Oct 14, 2012 at 04:48:36AM +0200, Hannes Frederic Sowa wrote:
 + case BRD_CTL_DEL:
 + error = get_user(val, (int __user *)param);
 + if (error  0)
 + break;
 + if ((val  max_part) != 0) {
 + error = -EINVAL;
 + break;
 + }
 + val = part_shift;
 + brd2 = NULL;
 + list_for_each_entry(brd, brd_devices, brd_list) {
 + if (brd-brd_number == val) {
 + brd2 = brd;
 + break;
 + }
 + }
 + if (brd2 == NULL) {
 + error = -ENODEV;
 + break;
 + }
 + brd_del_one(brd2);
 + break;

Sorry, first bug spotted. I should check the bd_openers before destroying the
device. Will be fixed in a follow-up patch, if needed.

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