linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/7] dm/md dependency tree in sysfs (rev.4)
@ 2006-03-13 22:03 Jun'ichi Nomura
  2006-03-13 22:14 ` [PATCH 1/7] dm/md dependency tree in sysfs: kobject_add_dir Jun'ichi Nomura
                   ` (6 more replies)
  0 siblings, 7 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:03 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

Hello,

This is an updated version of "dm/md dependency tree in sysfs" patch.
For example, if dm-0 maps to sda, we'll have following symlinks;
   /sys/block/dm-0/slaves/sda --> /sys/block/sda
   /sys/block/sda/holders/dm-0 --> /sys/block/dm-0

Difference from the previous version is a fix for a problem
of calling possibly-sleeping function inside the fast path.
There were 3 such calls in my previous bd_claim patch:
   - kmalloc(GFP_KERNEL)
   - sysfs_create_symlink()
   - sysfs_remove_symlink()
The allocation code is moved outside of locks.
Symlink operations needs to be atomic, so they are moved out
of global bdev_lock and use bdev->bd_sem instead.

Comments are welcome.

I tested this on 2.6.16-rc6 and 2.6.16-rc6-mm1 with both
CONFIG_PREEMPT and CONFIG_DEBUG_SPINLOCK_SLEEP enabled
and saw no warnings in kernel log.

Patches included are:

  1. [PATCH 1/7] kobject_add_dir
     Adding kobject_add_dir() function which creates
     a subdirectory for a given kobject.

  2. [PATCH 2/7] add holders/slaves subdirectory to /sys/block
     Creating "slaves" and "holders" directories in /sys/block/<disk>,
     creating "holders" directory under /sys/block/<disk>/<partition>

  3. [PATCH 3/7] bd_claim_by_kobject
     Adding bd_claim_by_kobject() function which takes kobject as
     additional signature of holder device and creates sysfs symlinks
     between holder device and claimed device.
     bd_release_from_kobject() is a counter part of bd_claim_by_kobject.

  4. [PATCH 4/7] bd_claim_by_disk
     Variants which take gendisk instead of kobject
     and do kobject_{get,put}(&gendisk->kobj).

  5. [PATCH 5/7] md to use bd_claim_by_disk
     Use bd_claim_by_disk.

  6. [PATCH 6/7] dm to use bd_claim_by_disk
     Use bd_claim_by_disk.

  7. [PATCH 7/7] conver bd_sem to bd_mutex
     bd_sem is converted to bd_mutex in 2.6.16-rc6-mm1.
     The patch follows that conversion.

Patch 3 includes the fix mentioned above.

Patch 6 depends on dm-table-store-md.patch and dm-tidy-mdptr.patch
which were committed to -mm yesterday.

Patch 7 depends on sem2mutex-blockdev-2.patch in 2.6.16-rc6-mm1.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.


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

* [PATCH 1/7] dm/md dependency tree in sysfs: kobject_add_dir
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
@ 2006-03-13 22:14 ` Jun'ichi Nomura
  2006-03-13 22:14 ` [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory Jun'ichi Nomura
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:14 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

This adds kobject_add_dir() function which creates a subdirectory
for a given kobject.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 01-kobject_add_dir.patch --]
[-- Type: text/x-patch, Size: 1753 bytes --]

Adding kobject_add_dir() function which creates a subdirectory
for a given kobject.

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 include/linux/kobject.h |    2 ++
 lib/kobject.c           |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/kobject.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/kobject.h	2006-03-13 11:23:55.000000000 -0500
@@ -80,6 +80,8 @@ extern void kobject_unregister(struct ko
 extern struct kobject * kobject_get(struct kobject *);
 extern void kobject_put(struct kobject *);
 
+extern struct kobject * kobject_add_dir(struct kobject *, const char *);
+
 extern char * kobject_get_path(struct kobject *, gfp_t);
 
 struct kobj_type {
--- linux-2.6.16-rc6.orig/lib/kobject.c	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/lib/kobject.c	2006-03-13 11:23:55.000000000 -0500
@@ -379,6 +379,43 @@ void kobject_put(struct kobject * kobj)
 }
 
 
+static void dir_release(struct kobject *kobj)
+{
+	kfree(kobj);
+}
+
+static struct kobj_type dir_ktype = {
+	.release	= dir_release,
+	.sysfs_ops	= NULL,
+	.default_attrs	= NULL,
+};
+
+/**
+ *	kobject_add_dir - add sub directory of object.
+ *	@parent:	object in which a directory is created.
+ *	@name:	directory name.
+ *
+ *	Add a plain directory object as child of given object.
+ */
+struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
+{
+	struct kobject *k;
+
+	if (!parent)
+		return NULL;
+
+	k = kzalloc(sizeof(*k), GFP_KERNEL);
+	if (!k)
+		return NULL;
+
+	k->parent = parent;
+	k->ktype = &dir_ktype;
+	kobject_set_name(k, name);
+	kobject_register(k);
+
+	return k;
+}
+
 /**
  *	kset_init - initialize a kset for use
  *	@k:	kset 

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

* [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
  2006-03-13 22:14 ` [PATCH 1/7] dm/md dependency tree in sysfs: kobject_add_dir Jun'ichi Nomura
@ 2006-03-13 22:14 ` Jun'ichi Nomura
  2006-03-14 17:21   ` Jun'ichi Nomura
  2006-03-13 22:15 ` [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject Jun'ichi Nomura
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:14 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

With this patch, "slaves" and "holders" directories are
created in /sys/block/<disk> and
"holders" directory is created in /sys/block/<disk>/<partition>.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 02-add_subdirs.patch --]
[-- Type: text/x-patch, Size: 2824 bytes --]

Creating "slaves" and "holders" directories in /sys/block/<disk> and
creating "holders" directory under /sys/block/<disk>/<partition>

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 fs/partitions/check.c |   27 +++++++++++++++++++++++++++
 include/linux/genhd.h |    3 +++
 2 files changed, 30 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/genhd.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/genhd.h	2006-03-13 11:24:13.000000000 -0500
@@ -78,6 +78,7 @@ struct hd_struct {
 	sector_t start_sect;
 	sector_t nr_sects;
 	struct kobject kobj;
+	struct kobject *holder_dir;
 	unsigned ios[2], sectors[2];	/* READs and WRITEs */
 	int policy, partno;
 };
@@ -114,6 +115,8 @@ struct gendisk {
 	int number;			/* more of the same */
 	struct device *driverfs_dev;
 	struct kobject kobj;
+	struct kobject *holder_dir;
+	struct kobject *slave_dir;
 
 	struct timer_rand_state *random;
 	int policy;
--- linux-2.6.16-rc6.orig/fs/partitions/check.c	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/fs/partitions/check.c	2006-03-13 11:24:03.000000000 -0500
@@ -297,6 +297,25 @@ struct kobj_type ktype_part = {
 	.sysfs_ops	= &part_sysfs_ops,
 };
 
+static inline void partition_sysfs_add_subdir(struct hd_struct *p)
+{
+	struct kobject *k;
+
+	k = kobject_get(&p->kobj);
+	p->holder_dir = kobject_add_dir(k, "holders");
+	kobject_put(k);
+}
+
+static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
+{
+	struct kobject *k;
+
+	k = kobject_get(&disk->kobj);
+	disk->holder_dir = kobject_add_dir(k, "holders");
+	disk->slave_dir = kobject_add_dir(k, "slaves");
+	kobject_put(k);
+}
+
 void delete_partition(struct gendisk *disk, int part)
 {
 	struct hd_struct *p = disk->part[part-1];
@@ -310,6 +329,8 @@ void delete_partition(struct gendisk *di
 	p->ios[0] = p->ios[1] = 0;
 	p->sectors[0] = p->sectors[1] = 0;
 	devfs_remove("%s/part%d", disk->devfs_name, part);
+	if (p->holder_dir)
+		kobject_unregister(p->holder_dir);
 	kobject_unregister(&p->kobj);
 }
 
@@ -337,6 +358,7 @@ void add_partition(struct gendisk *disk,
 	p->kobj.parent = &disk->kobj;
 	p->kobj.ktype = &ktype_part;
 	kobject_register(&p->kobj);
+	partition_sysfs_add_subdir(p);
 	disk->part[part-1] = p;
 }
 
@@ -383,6 +405,7 @@ void register_disk(struct gendisk *disk)
 	if ((err = kobject_add(&disk->kobj)))
 		return;
 	disk_sysfs_symlinks(disk);
+ 	disk_sysfs_add_subdirs(disk);
 	kobject_uevent(&disk->kobj, KOBJ_ADD);
 
 	/* No minors to use for partitions */
@@ -483,6 +506,10 @@ void del_gendisk(struct gendisk *disk)
 
 	devfs_remove_disk(disk);
 
+	if (disk->holder_dir)
+		kobject_unregister(disk->holder_dir);
+	if (disk->holder_dir)
+		kobject_unregister(disk->slave_dir);
 	if (disk->driverfs_dev) {
 		char *disk_name = make_block_name(disk);
 		sysfs_remove_link(&disk->kobj, "device");

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

* [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
  2006-03-13 22:14 ` [PATCH 1/7] dm/md dependency tree in sysfs: kobject_add_dir Jun'ichi Nomura
  2006-03-13 22:14 ` [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory Jun'ichi Nomura
@ 2006-03-13 22:15 ` Jun'ichi Nomura
  2006-03-14  2:27   ` Andrew Morton
  2006-03-13 22:16 ` [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk Jun'ichi Nomura
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:15 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

This adds bd_claim_by_kobject() function which takes kobject as
additional signature of holder device and creates sysfs symlinks
between holder device and claimed device.
bd_release_from_kobject() is a counter part of bd_claim_by_kobject.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 03-bd_claim_by_kobj.patch --]
[-- Type: text/x-patch, Size: 6619 bytes --]

Adding bd_claim_by_kobject() function which takes kobject as
additional signature of holder device and creates sysfs symlinks
between holder device and claimed device.
bd_release_from_kobject() is a counter part of bd_claim_by_kobject.

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 fs/block_dev.c     |  206 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h |    3
 2 files changed, 209 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/fs.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/fs.h	2006-03-13 11:24:18.000000000 -0500
@@ -402,6 +402,7 @@ struct block_device {
 	struct list_head	bd_inodes;
 	void *			bd_holder;
 	int			bd_holders;
+	struct list_head	bd_holder_list;
 	struct block_device *	bd_contains;
 	unsigned		bd_block_size;
 	struct hd_struct *	bd_part;
@@ -1381,6 +1382,8 @@ extern int blkdev_get(struct block_devic
 extern int blkdev_put(struct block_device *);
 extern int bd_claim(struct block_device *, void *);
 extern void bd_release(struct block_device *);
+extern int bd_claim_by_kobject(struct block_device *, void *, struct kobject *);
+extern void bd_release_from_kobject(struct block_device *, struct kobject *);
 
 /* fs/char_dev.c */
 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
--- linux-2.6.16-rc6.orig/fs/block_dev.c	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/fs/block_dev.c	2006-03-13 11:24:18.000000000 -0500
@@ -269,6 +269,7 @@ static void init_once(void * foo, kmem_c
 		sema_init(&bdev->bd_mount_sem, 1);
 		INIT_LIST_HEAD(&bdev->bd_inodes);
 		INIT_LIST_HEAD(&bdev->bd_list);
+		INIT_LIST_HEAD(&bdev->bd_holder_list);
 		inode_init_once(&ei->vfs_inode);
 	}
 }
@@ -493,6 +494,211 @@ void bd_release(struct block_device *bde
 EXPORT_SYMBOL(bd_release);
 
 /*
+ * Functions for bd_claim_by_kobject / bd_release_from_kobject
+ *
+ *     If a kobject is passed to bd_claim_by_kobject() 
+ *     and the kobject has a parent directory,
+ *     following symlinks are created:
+ *        o from the kobject to the claimed bdev
+ *        o from "holders" directory of the bdev to the parent of the kobject
+ *     bd_release_from_kobject() removes these symlinks.
+ *
+ *     Example:
+ *        If /dev/dm-0 maps to /dev/sda, kobject corresponding to
+ *        /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
+ *           /sys/block/dm-0/slaves/sda --> /sys/block/sda
+ *           /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
+ */
+
+
+static inline struct kobject * bdev_get_kobj(struct block_device *bdev)
+{
+	if (bdev->bd_contains != bdev)
+		return kobject_get(&bdev->bd_part->kobj);
+	else
+		return kobject_get(&bdev->bd_disk->kobj);
+}
+
+static inline struct kobject * bdev_get_holder(struct block_device *bdev)
+{
+	if (bdev->bd_contains != bdev)
+		return kobject_get(bdev->bd_part->holder_dir);
+	else
+		return kobject_get(bdev->bd_disk->holder_dir);
+}
+
+static inline void add_symlink(struct kobject *from, struct kobject *to)
+{
+	if (!from || !to)
+		return;
+	sysfs_create_link(from, to, kobject_name(to));
+}
+
+static inline void del_symlink(struct kobject *from, struct kobject *to)
+{
+	if (!from || !to)
+		return;
+	sysfs_remove_link(from, kobject_name(to));
+}
+
+/* bd_holder_list is protected by bdev_lock */
+struct bd_holder {
+	struct list_head list;	/* chain of holders of the bdev */
+	int count;		/* references from the holder */
+	struct kobject *sdir;	/* holder object, e.g. "/block/dm-0/slaves" */
+	struct kobject *hdev;	/* e.g. "/block/dm-0" */
+	struct kobject *hdir;	/* e.g. "/block/sda/holders" */
+	struct kobject *sdev;	/* e.g. "/block/sda" */
+};
+
+static inline int bd_holder_grab_dirs(struct block_device *bdev,
+			struct bd_holder *bo)
+{
+	if (!bdev || !bo)
+		return 0;
+
+	bo->sdir = kobject_get(bo->sdir);
+	if (!bo->sdir)
+		return 0;
+
+	bo->hdev = kobject_get(bo->sdir->parent);
+	if (!bo->hdev)
+		goto fail_put_sdir;
+
+	bo->sdev = bdev_get_kobj(bdev);
+	if (!bo->sdev)
+		goto fail_put_hdev;
+
+	bo->hdir = bdev_get_holder(bdev);
+	if (!bo->hdir)
+		goto fail_put_sdev;
+
+	return 1;
+
+fail_put_sdev:
+	kobject_put(bo->sdev);
+fail_put_hdev:
+	kobject_put(bo->hdev);
+fail_put_sdir:
+	kobject_put(bo->sdir);
+
+	return 0;
+}
+
+static inline void bd_holder_release_dirs(struct bd_holder *bo)
+{
+	kobject_put(bo->hdir);
+	kobject_put(bo->sdev);
+	kobject_put(bo->hdev);
+	kobject_put(bo->sdir);
+}
+
+static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
+	if (!bo)
+		return NULL;
+
+	bo->count = 1;
+	bo->sdir = kobj;
+
+	return bo;
+}
+
+static void free_bd_holder(struct bd_holder *bo)
+{
+	kfree(bo);
+}
+
+static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
+{
+        struct bd_holder *tmp;
+
+	if (!bo)
+		return 0;
+
+	list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
+		if (tmp->sdir == bo->sdir) {
+			tmp->count++;
+			return 0;
+		}
+	}
+
+	if (!bd_holder_grab_dirs(bdev, bo))
+		return 0;
+
+	add_symlink(bo->sdir, bo->sdev);
+	add_symlink(bo->hdir, bo->hdev);
+	list_add_tail(&bo->list, &bdev->bd_holder_list);
+	return 1;
+}
+
+static struct bd_holder *del_bd_holder(struct block_device *bdev,
+					struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	list_for_each_entry(bo, &bdev->bd_holder_list, list) {
+		if (bo->sdir == kobj) {
+			bo->count--;
+			BUG_ON(bo->count < 0);
+			if (!bo->count) {
+				list_del(&bo->list);
+				del_symlink(bo->sdir, bo->sdev);
+				del_symlink(bo->hdir, bo->hdev);
+				bd_holder_release_dirs(bo);
+				return bo;
+			}
+			break;
+		}
+	}
+
+	return NULL;
+}
+
+int bd_claim_by_kobject(struct block_device *bdev, void *holder,
+			struct kobject *kobj)
+{
+	int res = -EBUSY;
+	struct bd_holder *bo;
+
+	if (!kobj)
+		return -EINVAL;
+
+	bo = alloc_bd_holder(kobj);
+	if (!bo)
+		return -ENOMEM;
+
+	down(&bdev->bd_sem);
+	res = bd_claim(bdev, holder);
+	if (res || !add_bd_holder(bdev, bo))
+		free_bd_holder(bo);
+	up(&bdev->bd_sem);
+
+	return res;
+}
+
+EXPORT_SYMBOL(bd_claim_by_kobject);
+
+void bd_release_from_kobject(struct block_device *bdev, struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	if (!kobj)
+		return;
+
+	down(&bdev->bd_sem);
+	bd_release(bdev);
+	if ((bo = del_bd_holder(bdev, kobj)))
+		free_bd_holder(bo);
+	up(&bdev->bd_sem);
+}
+
+EXPORT_SYMBOL(bd_release_from_kobject);
+
+/*
  * Tries to open block device by device number.  Use it ONLY if you
  * really do not have anything better - i.e. when you are behind a
  * truly sucky interface and all you are given is a device number.  _Never_

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

* [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
                   ` (2 preceding siblings ...)
  2006-03-13 22:15 ` [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject Jun'ichi Nomura
@ 2006-03-13 22:16 ` Jun'ichi Nomura
  2006-03-14  2:29   ` Andrew Morton
  2006-03-13 22:16 ` [PATCH 5/7] dm/md dependency tree in sysfs: md to use bd_claim_by_disk Jun'ichi Nomura
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:16 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

This adds variants of bd_claim_by_kobject which takes gendisk instead
of kobject and do kobject_{get,put}(&gendisk->slave_dir).

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 04-bd_claim_by_disk.patch --]
[-- Type: text/x-patch, Size: 940 bytes --]

Variants of bd_claim_by_kobject which takes gendisk instead
of kobject and do kobject_{get,put}(&gendisk->slave_dir).

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 include/linux/genhd.h |   13 +++++++++++++
 1 files changed, 13 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/genhd.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/genhd.h	2006-03-13 11:24:13.000000000 -0500
@@ -421,6 +424,19 @@ static inline struct block_device *bdget
 	return bdget(MKDEV(disk->major, disk->first_minor) + index);
 }
 
+static inline int bd_claim_by_disk(struct block_device *bdev,
+					void *holder, struct gendisk *disk)
+{
+	return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
+}
+
+static inline void bd_release_from_disk(struct block_device *bdev,
+					struct gendisk *disk)
+{
+	bd_release_from_kobject(bdev, disk->slave_dir);
+	kobject_put(disk->slave_dir);
+}
+
 #endif
 
 #endif

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

* [PATCH 5/7] dm/md dependency tree in sysfs: md to use bd_claim_by_disk
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
                   ` (3 preceding siblings ...)
  2006-03-13 22:16 ` [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk Jun'ichi Nomura
@ 2006-03-13 22:16 ` Jun'ichi Nomura
  2006-03-13 22:17 ` [PATCH 6/7] dm/md dependency tree in sysfs: dm " Jun'ichi Nomura
  2006-03-13 22:17 ` [PATCH 7/7] dm/md dependency tree in sysfs: convert bd_sem to bd_mutex Jun'ichi Nomura
  6 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:16 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

Following symlinks are created if md0 is built from sda and sdb
  /sys/block/md0/slaves/sda --> /sys/block/sda
  /sys/block/md0/slaves/sdb --> /sys/block/sdb
  /sys/block/sda/holders/md0 --> /sys/block/md0
  /sys/block/sdb/holders/md0 --> /sys/block/md0

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 05-md_deptree.patch --]
[-- Type: text/x-patch, Size: 1041 bytes --]

Use bd_claim_by_disk.

Following symlinks are created if md0 is built from sda and sdb
  /sys/block/md0/slaves/sda --> /sys/block/sda
  /sys/block/md0/slaves/sdb --> /sys/block/sdb
  /sys/block/sda/holders/md0 --> /sys/block/md0
  /sys/block/sdb/holders/md0 --> /sys/block/md0

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 drivers/md/md.c |    2 ++
 1 files changed, 2 insertions(+)

--- linux-2.6.16-rc6.orig/drivers/md/md.c	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/drivers/md/md.c	2006-03-13 11:24:00.000000000 -0500
@@ -1298,6 +1298,7 @@ static int bind_rdev_to_array(mdk_rdev_t
 	else
 		ko = &rdev->bdev->bd_disk->kobj;
 	sysfs_create_link(&rdev->kobj, ko, "block");
+	bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
 	return 0;
 }
 
@@ -1308,6 +1309,7 @@ static void unbind_rdev_from_array(mdk_r
 		MD_BUG();
 		return;
 	}
+	bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
 	list_del_init(&rdev->same_set);
 	printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
 	rdev->mddev = NULL;

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

* [PATCH 6/7] dm/md dependency tree in sysfs: dm to use bd_claim_by_disk
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
                   ` (4 preceding siblings ...)
  2006-03-13 22:16 ` [PATCH 5/7] dm/md dependency tree in sysfs: md to use bd_claim_by_disk Jun'ichi Nomura
@ 2006-03-13 22:17 ` Jun'ichi Nomura
  2006-03-13 22:17 ` [PATCH 7/7] dm/md dependency tree in sysfs: convert bd_sem to bd_mutex Jun'ichi Nomura
  6 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:17 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

Following symlinks are created if dm-0 maps to sda:
  /sys/block/dm-0/slaves/sda --> /sys/block/sda
  /sys/block/sda/holders/dm-0 --> /sys/block/dm-0

This patch depends on the following patches in mm tree:
  dm-tidy-mdptr.patch
  dm-table-store-md.patch

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 06-dm_deptree.patch --]
[-- Type: text/x-patch, Size: 2764 bytes --]

Use bd_claim_by_disk.

Following symlinks are created if dm-0 maps to sda:
  /sys/block/dm-0/slaves/sda --> /sys/block/sda
  /sys/block/sda/holders/dm-0 --> /sys/block/dm-0

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 drivers/md/dm-table.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)

--- linux-2.6.16-rc6.orig/drivers/md/dm-table.c	2006-03-13 11:20:09.000000000 -0500
+++ linux-2.6.16-rc6/drivers/md/dm-table.c	2006-03-13 14:19:28.000000000 -0500
@@ -348,7 +348,7 @@ static struct dm_dev *find_device(struct
 /*
  * Open a device so we can use it as a map destination.
  */
-static int open_dev(struct dm_dev *d, dev_t dev)
+static int open_dev(struct dm_dev *d, dev_t dev, struct mapped_device *md)
 {
 	static char *_claim_ptr = "I belong to device-mapper";
 	struct block_device *bdev;
@@ -361,7 +361,7 @@ static int open_dev(struct dm_dev *d, de
 	bdev = open_by_devnum(dev, d->mode);
 	if (IS_ERR(bdev))
 		return PTR_ERR(bdev);
-	r = bd_claim(bdev, _claim_ptr);
+	r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md));
 	if (r)
 		blkdev_put(bdev);
 	else
@@ -372,12 +372,12 @@ static int open_dev(struct dm_dev *d, de
 /*
  * Close a device that we've been using.
  */
-static void close_dev(struct dm_dev *d)
+static void close_dev(struct dm_dev *d, struct mapped_device *md)
 {
 	if (!d->bdev)
 		return;
 
-	bd_release(d->bdev);
+	bd_release_from_disk(d->bdev, dm_disk(md));
 	blkdev_put(d->bdev);
 	d->bdev = NULL;
 }
@@ -398,7 +398,7 @@ static int check_device_area(struct dm_d
  * careful to leave things as they were if we fail to reopen the
  * device.
  */
-static int upgrade_mode(struct dm_dev *dd, int new_mode)
+static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md)
 {
 	int r;
 	struct dm_dev dd_copy;
@@ -408,9 +408,9 @@ static int upgrade_mode(struct dm_dev *d
 
 	dd->mode |= new_mode;
 	dd->bdev = NULL;
-	r = open_dev(dd, dev);
+	r = open_dev(dd, dev, md);
 	if (!r)
-		close_dev(&dd_copy);
+		close_dev(&dd_copy, md);
 	else
 		*dd = dd_copy;
 
@@ -453,7 +453,7 @@ static int __table_get_device(struct dm_
 		dd->mode = mode;
 		dd->bdev = NULL;
 
-		if ((r = open_dev(dd, dev))) {
+		if ((r = open_dev(dd, dev, t->md))) {
 			kfree(dd);
 			return r;
 		}
@@ -464,7 +464,7 @@ static int __table_get_device(struct dm_
 		list_add(&dd->list, &t->devices);
 
 	} else if (dd->mode != (mode | dd->mode)) {
-		r = upgrade_mode(dd, mode);
+		r = upgrade_mode(dd, mode, t->md);
 		if (r)
 			return r;
 	}
@@ -539,7 +539,7 @@ int dm_get_device(struct dm_target *ti, 
 void dm_put_device(struct dm_target *ti, struct dm_dev *dd)
 {
 	if (atomic_dec_and_test(&dd->count)) {
-		close_dev(dd);
+		close_dev(dd, ti->table->md);
 		list_del(&dd->list);
 		kfree(dd);
 	}

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

* [PATCH 7/7] dm/md dependency tree in sysfs: convert bd_sem to bd_mutex
  2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
                   ` (5 preceding siblings ...)
  2006-03-13 22:17 ` [PATCH 6/7] dm/md dependency tree in sysfs: dm " Jun'ichi Nomura
@ 2006-03-13 22:17 ` Jun'ichi Nomura
  6 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-13 22:17 UTC (permalink / raw)
  To: Andrew Morton, Alasdair Kergon, Greg KH, Neil Brown
  Cc: Lars Marowsky-Bree, device-mapper development, linux-kernel

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

This patch is part of dm/md dependency tree in sysfs.

This patch follows the change introduced by
sem2mutex-blockdev-2.patch in 2.6.16-rc6-mm1.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 07-sem2mutex-bd_sem.patch --]
[-- Type: text/x-patch, Size: 973 bytes --]

Convert bd_sem to bd_mutex

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 block_dev.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

--- linux-2.6.16-rc6-mm1.orig/fs/block_dev.c.orig-mm1	2006-03-13 14:04:32.000000000 -0500
+++ linux-2.6.16-rc6-mm1/fs/block_dev.c	2006-03-13 12:29:35.000000000 -0500
@@ -674,11 +674,11 @@ int bd_claim_by_kobject(struct block_dev
 	if (!bo)
 		return -ENOMEM;
 
-	down(&bdev->bd_sem);
+	mutex_lock(&bdev->bd_mutex);
 	res = bd_claim(bdev, holder);
 	if (res || !add_bd_holder(bdev, bo))
 		free_bd_holder(bo);
-	up(&bdev->bd_sem);
+	mutex_unlock(&bdev->bd_mutex);
 
 	return res;
 }
@@ -692,11 +692,11 @@ void bd_release_from_kobject(struct bloc
 	if (!kobj)
 		return;
 
-	down(&bdev->bd_sem);
+	mutex_lock(&bdev->bd_mutex);
 	bd_release(bdev);
 	if ((bo = del_bd_holder(bdev, kobj)))
 		free_bd_holder(bo);
-	up(&bdev->bd_sem);
+	mutex_unlock(&bdev->bd_mutex);
 }
 
 EXPORT_SYMBOL(bd_release_from_kobject);

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

* Re: [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject
  2006-03-13 22:15 ` [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject Jun'ichi Nomura
@ 2006-03-14  2:27   ` Andrew Morton
  2006-03-14 17:21     ` Jun'ichi Nomura
  0 siblings, 1 reply; 12+ messages in thread
From: Andrew Morton @ 2006-03-14  2:27 UTC (permalink / raw)
  To: Jun'ichi Nomura; +Cc: agk, gregkh, neilb, lmb, dm-devel, linux-kernel

"Jun'ichi Nomura" <j-nomura@ce.jp.nec.com> wrote:
>
> This patch is part of dm/md dependency tree in sysfs.
> 
> This adds bd_claim_by_kobject() function which takes kobject as
> additional signature of holder device and creates sysfs symlinks
> between holder device and claimed device.
> bd_release_from_kobject() is a counter part of bd_claim_by_kobject.
> 
> ...
>
> @@ -402,6 +402,7 @@ struct block_device {
>  	struct list_head	bd_inodes;
>  	void *			bd_holder;
>  	int			bd_holders;
> +	struct list_head	bd_holder_list;
>  	struct block_device *	bd_contains;
>  	unsigned		bd_block_size;
>  	struct hd_struct *	bd_part;

Bear in mind that sysfs is Kconfigurable (CONFIG_SYSFS).  If possible,
newly-added code which doesn't make sense without sysfs should not appear
in a CONFIG_SYSFS=n vmlinux.

> +
> +static inline struct kobject * bdev_get_kobj(struct block_device *bdev)

pet-peeve: The space after asterisk has no use.

> +{
> +	if (bdev->bd_contains != bdev)
> +		return kobject_get(&bdev->bd_part->kobj);
> +	else
> +		return kobject_get(&bdev->bd_disk->kobj);
> +}
> +
> +static inline struct kobject * bdev_get_holder(struct block_device *bdev)
> +static inline void add_symlink(struct kobject *from, struct kobject *to)
> +static inline void del_symlink(struct kobject *from, struct kobject *to)
> +static inline int bd_holder_grab_dirs(struct block_device *bdev,
> +static inline void bd_holder_release_dirs(struct bd_holder *bo)

I suposse the inlines are OK, given that we "know" that there's only a
single callsite.  But recent gcc's take care of that automatically.

> +static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
> +{
> +        struct bd_holder *tmp;
> +
> +	if (!bo)
> +		return 0;

whitespace went wild there.

> +static void free_bd_holder(struct bd_holder *bo)
> ...
> +
> +	bo->sdir = kobj;
> +	list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
> +		if (tmp->sdir == bo->sdir) {
> +			tmp->count++;
> +			return 0;
> +		}
> +	}
> +
> +	if (!bd_holder_grab_dirs(bdev, bo))
> +		return 0;
> +
> +	add_symlink(bo->sdir, bo->sdev);
> +	add_symlink(bo->hdir, bo->hdev);
> +	list_add_tail(&bo->list, &bdev->bd_holder_list);
> +	return 1;
> +}
> 
> +static struct bd_holder *del_bd_holder(struct block_device *bdev,
> +					struct kobject *kobj)
> +{
> +	struct bd_holder *bo;
> +
> +	list_for_each_entry(bo, &bdev->bd_holder_list, list) {
> +		if (bo->sdir == kobj) {
> +			bo->count--;
> +			BUG_ON(bo->count < 0);
> +			if (!bo->count) {
> +				list_del(&bo->list);
> +				del_symlink(bo->sdir, bo->sdev);
> +				del_symlink(bo->hdir, bo->hdev);
> +				bd_holder_release_dirs(bo);
> +				return bo;
> +			}
> +			break;
> +		}
> +	}
> +
> +	return NULL;
> +}

Some comments which describe what these do, what they return and why they
return it would be nice.

> +
> +int bd_claim_by_kobject(struct block_device *bdev, void *holder,
> +			struct kobject *kobj)
> +{

Ditto.

> +	int res = -EBUSY;
> +	struct bd_holder *bo;
> +
> +	if (!kobj)
> +		return -EINVAL;
> +
> +	bo = alloc_bd_holder(kobj);
> +	if (!bo)
> +		return -ENOMEM;
> +
> +	down(&bdev->bd_sem);
> +	res = bd_claim(bdev, holder);
> +	if (res || !add_bd_holder(bdev, bo))
> +		free_bd_holder(bo);
> +	up(&bdev->bd_sem);
> +
> +	return res;
> +}
> +
> +EXPORT_SYMBOL(bd_claim_by_kobject);

I don't think the blank line before the EXPORT_SYMBOL() does anything useful.

EXPORT_SYMBOL_GPL() might be preferred.

> +void bd_release_from_kobject(struct block_device *bdev, struct kobject *kobj)
> +{
> +	struct bd_holder *bo;
> +
> +	if (!kobj)
> +		return;
> +
> +	down(&bdev->bd_sem);
> +	bd_release(bdev);
> +	if ((bo = del_bd_holder(bdev, kobj)))
> +		free_bd_holder(bo);
> +	up(&bdev->bd_sem);
> +}
> +
> +EXPORT_SYMBOL(bd_release_from_kobject);

Ditto.

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

* Re: [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk
  2006-03-13 22:16 ` [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk Jun'ichi Nomura
@ 2006-03-14  2:29   ` Andrew Morton
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2006-03-14  2:29 UTC (permalink / raw)
  To: Jun'ichi Nomura; +Cc: agk, gregkh, neilb, lmb, dm-devel, linux-kernel

"Jun'ichi Nomura" <j-nomura@ce.jp.nec.com> wrote:
>
>  Variants of bd_claim_by_kobject which takes gendisk instead
>  of kobject and do kobject_{get,put}(&gendisk->slave_dir).
> 
>  Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>
> 
>   include/linux/genhd.h |   13 +++++++++++++
>   1 files changed, 13 insertions(+)
> 
>  --- linux-2.6.16-rc6.orig/include/linux/genhd.h	2006-03-11 17:12:55.000000000 -0500
>  +++ linux-2.6.16-rc6/include/linux/genhd.h	2006-03-13 11:24:13.000000000 -0500
>  @@ -421,6 +424,19 @@ static inline struct block_device *bdget
>   	return bdget(MKDEV(disk->major, disk->first_minor) + index);
>   }
>   
>  +static inline int bd_claim_by_disk(struct block_device *bdev,
>  +					void *holder, struct gendisk *disk)
>  +{
>  +	return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
>  +}
>  +
>  +static inline void bd_release_from_disk(struct block_device *bdev,
>  +					struct gendisk *disk)
>  +{
>  +	bd_release_from_kobject(bdev, disk->slave_dir);
>  +	kobject_put(disk->slave_dir);
>  +}
>  +

genhd.h doesn't include kobject.h, so this only compiles due to luck.

An alternative to adding possibly risky nested header inclusions would be
to uninline these functions.


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

* Re: [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject
  2006-03-14  2:27   ` Andrew Morton
@ 2006-03-14 17:21     ` Jun'ichi Nomura
  0 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-14 17:21 UTC (permalink / raw)
  To: Andrew Morton; +Cc: agk, gregkh, neilb, lmb, dm-devel, linux-kernel

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

Hi Andrew,

Thank you for the review and comments.

Attached patch reflects your comments.
The patch also reflects the other comment regarding uninlining
bd_claim_by_disk() and bd_release_from_disk().

Could you please replace
dm-md-dependency-tree-in-sysfs-bd_claim_by_kobject.patch with this
and drop dm-md-dependency-tree-in-sysfs-bd_claim_by_disk.patch?
Or am I better to send a patch relative to the previous ones?


Andrew Morton wrote:
> Bear in mind that sysfs is Kconfigurable (CONFIG_SYSFS).  If possible,
> newly-added code which doesn't make sense without sysfs should not appear
> in a CONFIG_SYSFS=n vmlinux.

OK, fixed.
CONFIG_SYSFS=n built ok and confirmed no symbols related to
my sysfs symlinking exist in it.

>>+static inline struct kobject * bdev_get_kobj(struct block_device *bdev)
> 
> pet-peeve: The space after asterisk has no use.

Fixed.

>>+static inline struct kobject * bdev_get_holder(struct block_device *bdev)
>>+static inline void add_symlink(struct kobject *from, struct kobject *to)
>>+static inline void del_symlink(struct kobject *from, struct kobject *to)
>>+static inline int bd_holder_grab_dirs(struct block_device *bdev,
>>+static inline void bd_holder_release_dirs(struct bd_holder *bo)
> 
> I suposse the inlines are OK, given that we "know" that there's only a
> single callsite.  But recent gcc's take care of that automatically.

I removed 'inline's from them and let it cared by gcc.

>>+static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
>>+{
>>+        struct bd_holder *tmp;
>>+
>>+	if (!bo)
>>+		return 0;
> 
> whitespace went wild there.

Fixed. Thank you.

> Some comments which describe what these do, what they return and why they
> return it would be nice.

Comments are added for non-trivial functions and a structure.

>>+}
>>+
>>+EXPORT_SYMBOL(bd_claim_by_kobject);
> 
> I don't think the blank line before the EXPORT_SYMBOL() does anything useful.

Sure. They just followed the other exports in the file.
Fixed.

> EXPORT_SYMBOL_GPL() might be preferred.

I changed them to EXPORT_SYMBOL_GPL().

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 03-bd_claim_by_kobj.patch --]
[-- Type: text/x-patch, Size: 9545 bytes --]

Adding bd_claim_by_kobject() function which takes kobject as
additional signature of holder device and creates sysfs symlinks
between holder device and claimed device.
bd_release_from_kobject() is a counter part of bd_claim_by_kobject.

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 fs/block_dev.c     |  297 +++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h |   10 +
 2 files changed, 307 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/fs.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/fs.h	2006-03-14 08:01:33.000000000 -0500
@@ -402,6 +402,9 @@ struct block_device {
 	struct list_head	bd_inodes;
 	void *			bd_holder;
 	int			bd_holders;
+#ifdef CONFIG_SYSFS
+	struct list_head	bd_holder_list;
+#endif
 	struct block_device *	bd_contains;
 	unsigned		bd_block_size;
 	struct hd_struct *	bd_part;
@@ -1381,6 +1384,13 @@ extern int blkdev_get(struct block_devic
 extern int blkdev_put(struct block_device *);
 extern int bd_claim(struct block_device *, void *);
 extern void bd_release(struct block_device *);
+#ifdef CONFIG_SYSFS
+extern int bd_claim_by_disk(struct block_device *, void *, struct gendisk *);
+extern void bd_release_from_disk(struct block_device *, struct gendisk *);
+#else
+#define bd_claim_by_disk(bdev, holder, disk)	bd_claim(bdev, holder)
+#define bd_release_from_disk(bdev, disk)	bd_release(bdev)
+#endif
 
 /* fs/char_dev.c */
 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
--- linux-2.6.16-rc6.orig/fs/block_dev.c	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/fs/block_dev.c	2006-03-14 11:46:47.000000000 -0500
@@ -269,6 +269,9 @@ static void init_once(void * foo, kmem_c
 		sema_init(&bdev->bd_mount_sem, 1);
 		INIT_LIST_HEAD(&bdev->bd_inodes);
 		INIT_LIST_HEAD(&bdev->bd_list);
+#ifdef CONFIG_SYSFS
+		INIT_LIST_HEAD(&bdev->bd_holder_list);
+#endif
 		inode_init_once(&ei->vfs_inode);
 	}
 }
@@ -492,6 +495,300 @@ void bd_release(struct block_device *bde
 
 EXPORT_SYMBOL(bd_release);
 
+#ifdef CONFIG_SYSFS
+/*
+ * Functions for bd_claim_by_kobject / bd_release_from_kobject
+ *
+ *     If a kobject is passed to bd_claim_by_kobject() 
+ *     and the kobject has a parent directory,
+ *     following symlinks are created:
+ *        o from the kobject to the claimed bdev
+ *        o from "holders" directory of the bdev to the parent of the kobject
+ *     bd_release_from_kobject() removes these symlinks.
+ *
+ *     Example:
+ *        If /dev/dm-0 maps to /dev/sda, kobject corresponding to
+ *        /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
+ *           /sys/block/dm-0/slaves/sda --> /sys/block/sda
+ *           /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
+ */
+
+static struct kobject *bdev_get_kobj(struct block_device *bdev)
+{
+	if (bdev->bd_contains != bdev)
+		return kobject_get(&bdev->bd_part->kobj);
+	else
+		return kobject_get(&bdev->bd_disk->kobj);
+}
+
+static struct kobject *bdev_get_holder(struct block_device *bdev)
+{
+	if (bdev->bd_contains != bdev)
+		return kobject_get(bdev->bd_part->holder_dir);
+	else
+		return kobject_get(bdev->bd_disk->holder_dir);
+}
+
+static void add_symlink(struct kobject *from, struct kobject *to)
+{
+	if (!from || !to)
+		return;
+	sysfs_create_link(from, to, kobject_name(to));
+}
+
+static void del_symlink(struct kobject *from, struct kobject *to)
+{
+	if (!from || !to)
+		return;
+	sysfs_remove_link(from, kobject_name(to));
+}
+
+/*
+ * 'struct bd_holder' contains pointers to kobjects symlinked by
+ * bd_claim_by_kobject.
+ * It's connected to bd_holder_list which is protected by bdev->bd_sem.
+ */
+struct bd_holder {
+	struct list_head list;	/* chain of holders of the bdev */
+	int count;		/* references from the holder */
+	struct kobject *sdir;	/* holder object, e.g. "/block/dm-0/slaves" */
+	struct kobject *hdev;	/* e.g. "/block/dm-0" */
+	struct kobject *hdir;	/* e.g. "/block/sda/holders" */
+	struct kobject *sdev;	/* e.g. "/block/sda" */
+};
+
+/*
+ * Get references of related kobjects at once.
+ * Returns 1 on success. 0 on failure.
+ *
+ * Should call bd_holder_release_dirs() after successful use.
+ */
+static int bd_holder_grab_dirs(struct block_device *bdev,
+			struct bd_holder *bo)
+{
+	if (!bdev || !bo)
+		return 0;
+
+	bo->sdir = kobject_get(bo->sdir);
+	if (!bo->sdir)
+		return 0;
+
+	bo->hdev = kobject_get(bo->sdir->parent);
+	if (!bo->hdev)
+		goto fail_put_sdir;
+
+	bo->sdev = bdev_get_kobj(bdev);
+	if (!bo->sdev)
+		goto fail_put_hdev;
+
+	bo->hdir = bdev_get_holder(bdev);
+	if (!bo->hdir)
+		goto fail_put_sdev;
+
+	return 1;
+
+fail_put_sdev:
+	kobject_put(bo->sdev);
+fail_put_hdev:
+	kobject_put(bo->hdev);
+fail_put_sdir:
+	kobject_put(bo->sdir);
+
+	return 0;
+}
+
+/* Put references of related kobjects at once. */
+static void bd_holder_release_dirs(struct bd_holder *bo)
+{
+	kobject_put(bo->hdir);
+	kobject_put(bo->sdev);
+	kobject_put(bo->hdev);
+	kobject_put(bo->sdir);
+}
+
+static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
+	if (!bo)
+		return NULL;
+
+	bo->count = 1;
+	bo->sdir = kobj;
+
+	return bo;
+}
+
+static void free_bd_holder(struct bd_holder *bo)
+{
+	kfree(bo);
+}
+
+/**
+ * add_bd_holder - create sysfs symlinks for bd_claim() relationship
+ *
+ * @bdev:	block device to be bd_claimed
+ * @bo:		preallocated and initialized by alloc_bd_holder()
+ *
+ * If there is no matching entry with @bo in @bdev->bd_holder_list,
+ * add @bo to the list, create symlinks.
+ *
+ * Returns 1 if @bo was added to the list.
+ * Returns 0 if @bo wasn't used by any reason and should be freed.
+ */
+static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
+{
+	struct bd_holder *tmp;
+
+	if (!bo)
+		return 0;
+
+	list_for_each_entry(tmp, &bdev->bd_holder_list, list) {
+		if (tmp->sdir == bo->sdir) {
+			tmp->count++;
+			return 0;
+		}
+	}
+
+	if (!bd_holder_grab_dirs(bdev, bo))
+		return 0;
+
+	add_symlink(bo->sdir, bo->sdev);
+	add_symlink(bo->hdir, bo->hdev);
+	list_add_tail(&bo->list, &bdev->bd_holder_list);
+	return 1;
+}
+
+/**
+ * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
+ *
+ * @bdev:	block device to be bd_claimed
+ * @kobj:	holder's kobject
+ *
+ * If there is matching entry with @kobj in @bdev->bd_holder_list
+ * and no other bd_claim() from the same kobject,
+ * remove the struct bd_holder from the list, delete symlinks for it.
+ *
+ * Returns a pointer to the struct bd_holder when it's removed from the list
+ * and ready to be freed.
+ * Returns NULL if matching claim isn't found or there is other bd_claim()
+ * by the same kobject.
+ */
+static struct bd_holder *del_bd_holder(struct block_device *bdev,
+					struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	list_for_each_entry(bo, &bdev->bd_holder_list, list) {
+		if (bo->sdir == kobj) {
+			bo->count--;
+			BUG_ON(bo->count < 0);
+			if (!bo->count) {
+				list_del(&bo->list);
+				del_symlink(bo->sdir, bo->sdev);
+				del_symlink(bo->hdir, bo->hdev);
+				bd_holder_release_dirs(bo);
+				return bo;
+			}
+			break;
+		}
+	}
+
+	return NULL;
+}
+
+/**
+ * bd_claim_by_kobject - bd_claim() with additional kobject signature 
+ *
+ * @bdev:	block device to be claimed
+ * @holder:	holder's signature
+ * @kobj:	holder's kobject
+ *
+ * Do bd_claim() and if it succeeds, create sysfs symlinks between
+ * the bdev and the holder's kobject.
+ * Use bd_release_from_kobject() when relesing the claimed bdev.
+ *
+ * Returns 0 on success. (same as bd_claim())
+ * Returns errno on failure.
+ */
+static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
+				struct kobject *kobj)
+{
+	int res;
+	struct bd_holder *bo;
+
+	if (!kobj)
+		return -EINVAL;
+
+	bo = alloc_bd_holder(kobj);
+	if (!bo)
+		return -ENOMEM;
+
+	down(&bdev->bd_sem);
+	res = bd_claim(bdev, holder);
+	if (res || !add_bd_holder(bdev, bo))
+		free_bd_holder(bo);
+	up(&bdev->bd_sem);
+
+	return res;
+}
+
+/**
+ * bd_release_from_kobject - bd_release() with additional kobject signature
+ *
+ * @bdev:	block device to be released
+ * @kobj:	holder's kobject
+ *
+ * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
+ */
+static void bd_release_from_kobject(struct block_device *bdev,
+					struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	if (!kobj)
+		return;
+
+	down(&bdev->bd_sem);
+	bd_release(bdev);
+	if ((bo = del_bd_holder(bdev, kobj)))
+		free_bd_holder(bo);
+	up(&bdev->bd_sem);
+}
+
+/**
+ * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
+ *
+ * @bdev:	block device to be claimed
+ * @holder:	holder's signature
+ * @disk:	holder's gendisk
+ *
+ * Call bd_claim_by_kobject() with getting @disk->slave_dir.
+ */
+int bd_claim_by_disk(struct block_device *bdev, void *holder,
+			struct gendisk *disk)
+{
+	return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
+}
+EXPORT_SYMBOL_GPL(bd_claim_by_disk);
+
+/**
+ * bd_release_from_disk - wrapper function for bd_release_from_kobject()
+ *
+ * @bdev:	block device to be claimed
+ * @disk:	holder's gendisk
+ *
+ * Call bd_release_from_kobject() and put @disk->slave_dir.
+ */
+void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
+{
+	bd_release_from_kobject(bdev, disk->slave_dir);
+	kobject_put(disk->slave_dir);
+}
+EXPORT_SYMBOL_GPL(bd_release_from_disk);
+#endif
+
 /*
  * Tries to open block device by device number.  Use it ONLY if you
  * really do not have anything better - i.e. when you are behind a

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

* Re: [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory
  2006-03-13 22:14 ` [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory Jun'ichi Nomura
@ 2006-03-14 17:21   ` Jun'ichi Nomura
  0 siblings, 0 replies; 12+ messages in thread
From: Jun'ichi Nomura @ 2006-03-14 17:21 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Alasdair Kergon, Greg KH, Neil Brown, Lars Marowsky-Bree,
	device-mapper development, linux-kernel

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

Hi Andrew,

Attached patch makes sysfs symlinking related codes conditional
on CONFIG_SYSFS.
Could you please replace
dm-md-dependency-tree-in-sysfs-holders-slaves-subdirectory.patch
with this?

Jun'ichi Nomura wrote:
> This patch is part of dm/md dependency tree in sysfs.
> 
> With this patch, "slaves" and "holders" directories are
> created in /sys/block/<disk> and
> "holders" directory is created in /sys/block/<disk>/<partition>.

Thanks,
-- 
Jun'ichi Nomura, NEC Solutions (America), Inc.

[-- Attachment #2: 02-add_subdirs.patch --]
[-- Type: text/x-patch, Size: 3098 bytes --]

Creating "slaves" and "holders" directories in /sys/block/<disk> and
creating "holders" directory under /sys/block/<disk>/<partition>

Signed-off-by: Jun'ichi Nomura <j-nomura@ce.jp.nec.com>

 fs/partitions/check.c |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/genhd.h |    7 +++++++
 2 files changed, 43 insertions(+)

--- linux-2.6.16-rc6.orig/include/linux/genhd.h	2006-03-11 17:12:55.000000000 -0500
+++ linux-2.6.16-rc6/include/linux/genhd.h	2006-03-14 08:09:51.000000000 -0500
@@ -78,6 +78,9 @@ struct hd_struct {
 	sector_t start_sect;
 	sector_t nr_sects;
 	struct kobject kobj;
+#ifdef CONFIG_SYSFS
+	struct kobject *holder_dir;
+#endif
 	unsigned ios[2], sectors[2];	/* READs and WRITEs */
 	int policy, partno;
 };
@@ -114,6 +117,10 @@ struct gendisk {
 	int number;			/* more of the same */
 	struct device *driverfs_dev;
 	struct kobject kobj;
+#ifdef CONFIG_SYSFS
+	struct kobject *holder_dir;
+	struct kobject *slave_dir;
+#endif
 
 	struct timer_rand_state *random;
 	int policy;
--- linux-2.6.16-rc6.orig/fs/partitions/check.c	2006-03-14 08:09:24.000000000 -0500
+++ linux-2.6.16-rc6/fs/partitions/check.c	2006-03-14 11:49:15.000000000 -0500
@@ -297,6 +297,30 @@ struct kobj_type ktype_part = {
 	.sysfs_ops	= &part_sysfs_ops,
 };
 
+#ifdef CONFIG_SYSFS
+static inline void partition_sysfs_add_subdir(struct hd_struct *p)
+{
+	struct kobject *k;
+
+	k = kobject_get(&p->kobj);
+	p->holder_dir = kobject_add_dir(k, "holders");
+	kobject_put(k);
+}
+
+static inline void disk_sysfs_add_subdirs(struct gendisk *disk)
+{
+	struct kobject *k;
+
+	k = kobject_get(&disk->kobj);
+	disk->holder_dir = kobject_add_dir(k, "holders");
+	disk->slave_dir = kobject_add_dir(k, "slaves");
+	kobject_put(k);
+}
+#else
+#define partition_sysfs_add_subdir(x)	do { } while (0)
+#define disk_sysfs_add_subdirs(x)	do { } while (0)
+#endif
+
 void delete_partition(struct gendisk *disk, int part)
 {
 	struct hd_struct *p = disk->part[part-1];
@@ -310,6 +334,10 @@ void delete_partition(struct gendisk *di
 	p->ios[0] = p->ios[1] = 0;
 	p->sectors[0] = p->sectors[1] = 0;
 	devfs_remove("%s/part%d", disk->devfs_name, part);
+#ifdef CONFIG_SYSFS
+	if (p->holder_dir)
+		kobject_unregister(p->holder_dir);
+#endif
 	kobject_unregister(&p->kobj);
 }
 
@@ -337,6 +365,7 @@ void add_partition(struct gendisk *disk,
 	p->kobj.parent = &disk->kobj;
 	p->kobj.ktype = &ktype_part;
 	kobject_register(&p->kobj);
+	partition_sysfs_add_subdir(p);
 	disk->part[part-1] = p;
 }
 
@@ -383,6 +412,7 @@ void register_disk(struct gendisk *disk)
 	if ((err = kobject_add(&disk->kobj)))
 		return;
 	disk_sysfs_symlinks(disk);
+ 	disk_sysfs_add_subdirs(disk);
 	kobject_uevent(&disk->kobj, KOBJ_ADD);
 
 	/* No minors to use for partitions */
@@ -483,6 +513,12 @@ void del_gendisk(struct gendisk *disk)
 
 	devfs_remove_disk(disk);
 
+#ifdef CONFIG_SYSFS
+	if (disk->holder_dir)
+		kobject_unregister(disk->holder_dir);
+	if (disk->slave_dir)
+		kobject_unregister(disk->slave_dir);
+#endif
 	if (disk->driverfs_dev) {
 		char *disk_name = make_block_name(disk);
 		sysfs_remove_link(&disk->kobj, "device");

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

end of thread, other threads:[~2006-03-14 17:20 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-13 22:03 [PATCH 0/7] dm/md dependency tree in sysfs (rev.4) Jun'ichi Nomura
2006-03-13 22:14 ` [PATCH 1/7] dm/md dependency tree in sysfs: kobject_add_dir Jun'ichi Nomura
2006-03-13 22:14 ` [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory Jun'ichi Nomura
2006-03-14 17:21   ` Jun'ichi Nomura
2006-03-13 22:15 ` [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject Jun'ichi Nomura
2006-03-14  2:27   ` Andrew Morton
2006-03-14 17:21     ` Jun'ichi Nomura
2006-03-13 22:16 ` [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk Jun'ichi Nomura
2006-03-14  2:29   ` Andrew Morton
2006-03-13 22:16 ` [PATCH 5/7] dm/md dependency tree in sysfs: md to use bd_claim_by_disk Jun'ichi Nomura
2006-03-13 22:17 ` [PATCH 6/7] dm/md dependency tree in sysfs: dm " Jun'ichi Nomura
2006-03-13 22:17 ` [PATCH 7/7] dm/md dependency tree in sysfs: convert bd_sem to bd_mutex Jun'ichi Nomura

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).