Skip to contentSkip to conversation

dm/md dependency tree in sysfs (rev.4)

PATCH Applied Jun'ichi Nomura 3 participants 13 messages Mar 13, 2006 – Mar 14, 2006

Subsystems (5)
VFS Library code MD RAID Driver core +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover dm/md dependency tree in sysfs (rev.4)

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.
#1 lore raw

PATCH 1/7 dm/md dependency tree in sysfs: kobject_add_dir

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 include/linux/kobject.h |    2 ++
 lib/kobject.c           |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
include/linux/kobject.h +2 -0
@@ -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 {
lib/kobject.c +37 -0
@@ -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 
#3 lore raw

PATCH 2/7 dm/md dependency tree in sysfs: holders/slaves subdirectory

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 fs/partitions/check.c |   27 +++++++++++++++++++++++++++
 include/linux/genhd.h |    3 +++
 2 files changed, 30 insertions(+)
include/linux/genhd.h +3 -0
@@ -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;
fs/partitions/check.c +27 -0
@@ -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");
#5 lore raw

PATCH 3/7 dm/md dependency tree in sysfs: bd_claim_by_kobject

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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.

 fs/block_dev.c     |  206 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h |    3
 2 files changed, 209 insertions(+)
include/linux/fs.h +3 -0
@@ -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 *);
fs/block_dev.c +206 -0

218 lines not on this page · raw message

#8 lore raw

PATCH 4/7 dm/md dependency tree in sysfs: bd_claim_by_disk

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 include/linux/genhd.h |   13 +++++++++++++
 1 files changed, 13 insertions(+)
include/linux/genhd.h +13 -0
@@ -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
#10 lore raw

PATCH 5/7 dm/md dependency tree in sysfs: md to use bd_claim_by_disk

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 drivers/md/md.c |    2 ++
 1 files changed, 2 insertions(+)
drivers/md/md.c +2 -0
@@ -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;
#11 lore raw

PATCH 6/7 dm/md dependency tree in sysfs: dm to use bd_claim_by_disk

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 drivers/md/dm-table.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
drivers/md/dm-table.c +10 -10
@@ -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);
 	}
#12 lore raw

PATCH 7/7 dm/md dependency tree in sysfs: convert bd_sem to bd_mutex

In reply to Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

Convert bd_sem to bd_mutex

 block_dev.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
fs/block_dev.c renamed from fs/block_dev.c.orig-mm1 +4 -4
@@ -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);
#6 lore raw

Re: [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject

In reply to Jun'ichi Nomura
Quoted 11 lines · Jun'ichi Nomura

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.

...

include/linux/fs.h struct block_device { · line 408

Quoted 7 lines
include/linux/fs.h +1 -0
@@ -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.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+static inline struct kobject * bdev_get_kobj(struct block_device *bdev)

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

fs/block_dev.c void bd_release(struct block_device *bde · line 588

Quoted 12 lines
fs/block_dev.c +12 -0
+{
+	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.

fs/block_dev.c void bd_release(struct block_device *bde · line 620

Quoted 6 lines
fs/block_dev.c +6 -0
+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.

Quoted 32 lines
fs/block_dev.c +30 -0
+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);

fs/block_dev.c void bd_release(struct block_device *bde · line 659

Quoted 10 lines
fs/block_dev.c +10 -0
+				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.

fs/block_dev.c void bd_release(struct block_device *bde · line 663

Quoted 4 lines
fs/block_dev.c +4 -0
+
+int bd_claim_by_kobject(struct block_device *bdev, void *holder,
+			struct kobject *kobj)
+{

Ditto.

Quoted 10 lines
fs/block_dev.c +10 -0
+	int res = -EBUSY;
+	struct bd_holder *bo;
+
+	if (!kobj)
+		return -EINVAL;
+
+	bo = alloc_bd_holder(kobj);
+	if (!bo)
+		return -ENOMEM;
+

fs/block_dev.c void bd_release(struct block_device *bde · line 683

Quoted 10 lines
fs/block_dev.c +10 -0
+	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.

Quoted 5 lines
fs/block_dev.c +5 -0
+void bd_release_from_kobject(struct block_device *bdev, struct kobject *kobj)
+{
+	struct bd_holder *bo;
+
+	if (!kobj)

fs/block_dev.c void bd_release(struct block_device *bde · line 699

Quoted 10 lines
fs/block_dev.c +10 -0
+		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.

#9 lore raw

Re: [PATCH 4/7] dm/md dependency tree in sysfs: bd_claim_by_disk

In reply to Jun'ichi Nomura
Quoted 15 lines · Jun'ichi Nomura

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

  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);
}

Quoted 13 lines

+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.

#7 lore raw

Re: [PATCH 3/7] dm/md dependency tree in sysfs: bd_claim_by_kobject

In reply to Andrew Morton

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?

Quoted 3 lines · Andrew Morton

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.

Quoted 3 lines
Quoted 1 line

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

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

Fixed.

Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+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.

Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+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.

Quoted 2 lines

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.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+}
+
+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.

Quoted 1 line

EXPORT_SYMBOL_GPL() might be preferred.

I changed them to EXPORT_SYMBOL_GPL().

Thanks,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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.

 fs/block_dev.c     |  297 +++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h |   10 +
 2 files changed, 307 insertions(+)
include/linux/fs.h +10 -0
@@ -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 *);
fs/block_dev.c +297 -0

309 lines not on this page · raw message

#4 lore raw

Re: [PATCH 2/7] dm/md dependency tree in sysfs: holders/slaves subdirectory

In reply to Jun'ichi Nomura

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?

Quoted 5 lines · Jun'ichi Nomura

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,

Signature · 1 line
Jun'ichi Nomura, NEC Solutions (America), Inc.

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

 fs/partitions/check.c |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/genhd.h |    7 +++++++
 2 files changed, 43 insertions(+)
include/linux/genhd.h +7 -0
@@ -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;
fs/partitions/check.c +36 -0
@@ -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");