Commit 96de1e66 authored by NeilBrown's avatar NeilBrown Committed by Linus Torvalds

[PATCH] md: fix some locking and module refcounting issues with md's use of sysfs

1/ I really should be using the __ATTR macros for defining attributes, so
   that the .owner field get set properly, otherwise modules can be removed
   while sysfs files are open.  This also involves some name changes of _show
   routines.

2/ Always lock the mddev (against reconfiguration) for all sysfs attribute
   access.  This easily avoid certain races and is completely consistant with
   other interfaces (ioctl and /proc/mdstat both always lock against
   reconfiguration).

3/ raid5 attributes must check that the 'conf' structure actually exists
   (the array could have been stopped while an attribute file was open).

4/ A missing 'kfree' from when the raid5_conf_t was converted to have a
   kobject embedded, and then converted back again.
Signed-off-by: default avatarNeil Brown <neilb@suse.de>
Acked-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 3855ad9f
...@@ -1504,7 +1504,7 @@ struct rdev_sysfs_entry { ...@@ -1504,7 +1504,7 @@ struct rdev_sysfs_entry {
}; };
static ssize_t static ssize_t
rdev_show_state(mdk_rdev_t *rdev, char *page) state_show(mdk_rdev_t *rdev, char *page)
{ {
char *sep = ""; char *sep = "";
int len=0; int len=0;
...@@ -1525,13 +1525,11 @@ rdev_show_state(mdk_rdev_t *rdev, char *page) ...@@ -1525,13 +1525,11 @@ rdev_show_state(mdk_rdev_t *rdev, char *page)
return len+sprintf(page+len, "\n"); return len+sprintf(page+len, "\n");
} }
static struct rdev_sysfs_entry rdev_state = { static struct rdev_sysfs_entry
.attr = {.name = "state", .mode = S_IRUGO }, rdev_state = __ATTR_RO(state);
.show = rdev_show_state,
};
static ssize_t static ssize_t
rdev_show_super(mdk_rdev_t *rdev, char *page) super_show(mdk_rdev_t *rdev, char *page)
{ {
if (rdev->sb_loaded && rdev->sb_size) { if (rdev->sb_loaded && rdev->sb_size) {
memcpy(page, page_address(rdev->sb_page), rdev->sb_size); memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
...@@ -1539,10 +1537,8 @@ rdev_show_super(mdk_rdev_t *rdev, char *page) ...@@ -1539,10 +1537,8 @@ rdev_show_super(mdk_rdev_t *rdev, char *page)
} else } else
return 0; return 0;
} }
static struct rdev_sysfs_entry rdev_super = { static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
.attr = {.name = "super", .mode = S_IRUGO },
.show = rdev_show_super,
};
static struct attribute *rdev_default_attrs[] = { static struct attribute *rdev_default_attrs[] = {
&rdev_state.attr, &rdev_state.attr,
&rdev_super.attr, &rdev_super.attr,
...@@ -1728,7 +1724,7 @@ static void analyze_sbs(mddev_t * mddev) ...@@ -1728,7 +1724,7 @@ static void analyze_sbs(mddev_t * mddev)
} }
static ssize_t static ssize_t
md_show_level(mddev_t *mddev, char *page) level_show(mddev_t *mddev, char *page)
{ {
mdk_personality_t *p = mddev->pers; mdk_personality_t *p = mddev->pers;
if (p == NULL) if (p == NULL)
...@@ -1739,21 +1735,15 @@ md_show_level(mddev_t *mddev, char *page) ...@@ -1739,21 +1735,15 @@ md_show_level(mddev_t *mddev, char *page)
return sprintf(page, "%s\n", p->name); return sprintf(page, "%s\n", p->name);
} }
static struct md_sysfs_entry md_level = { static struct md_sysfs_entry md_level = __ATTR_RO(level);
.attr = {.name = "level", .mode = S_IRUGO },
.show = md_show_level,
};
static ssize_t static ssize_t
md_show_rdisks(mddev_t *mddev, char *page) raid_disks_show(mddev_t *mddev, char *page)
{ {
return sprintf(page, "%d\n", mddev->raid_disks); return sprintf(page, "%d\n", mddev->raid_disks);
} }
static struct md_sysfs_entry md_raid_disks = { static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
.attr = {.name = "raid_disks", .mode = S_IRUGO },
.show = md_show_rdisks,
};
static ssize_t static ssize_t
md_show_scan(mddev_t *mddev, char *page) md_show_scan(mddev_t *mddev, char *page)
...@@ -1782,10 +1772,10 @@ md_store_scan(mddev_t *mddev, const char *page, size_t len) ...@@ -1782,10 +1772,10 @@ md_store_scan(mddev_t *mddev, const char *page, size_t len)
if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
return -EBUSY; return -EBUSY;
down(&mddev->reconfig_sem);
if (mddev->pers && mddev->pers->sync_request) if (mddev->pers && mddev->pers->sync_request)
canscan=1; canscan=1;
up(&mddev->reconfig_sem);
if (!canscan) if (!canscan)
return -EINVAL; return -EINVAL;
...@@ -1801,22 +1791,18 @@ md_store_scan(mddev_t *mddev, const char *page, size_t len) ...@@ -1801,22 +1791,18 @@ md_store_scan(mddev_t *mddev, const char *page, size_t len)
} }
static ssize_t static ssize_t
md_show_mismatch(mddev_t *mddev, char *page) mismatch_cnt_show(mddev_t *mddev, char *page)
{ {
return sprintf(page, "%llu\n", return sprintf(page, "%llu\n",
(unsigned long long) mddev->resync_mismatches); (unsigned long long) mddev->resync_mismatches);
} }
static struct md_sysfs_entry md_scan_mode = { static struct md_sysfs_entry
.attr = {.name = "scan_mode", .mode = S_IRUGO|S_IWUSR }, md_scan_mode = __ATTR(scan_mode, S_IRUGO|S_IWUSR, md_show_scan, md_store_scan);
.show = md_show_scan,
.store = md_store_scan,
};
static struct md_sysfs_entry md_mismatches = {
.attr = {.name = "mismatch_cnt", .mode = S_IRUGO }, static struct md_sysfs_entry
.show = md_show_mismatch, md_mismatches = __ATTR_RO(mismatch_cnt);
};
static struct attribute *md_default_attrs[] = { static struct attribute *md_default_attrs[] = {
&md_level.attr, &md_level.attr,
...@@ -1831,10 +1817,14 @@ md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) ...@@ -1831,10 +1817,14 @@ md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
{ {
struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
ssize_t rv;
if (!entry->show) if (!entry->show)
return -EIO; return -EIO;
return entry->show(mddev, page); mddev_lock(mddev);
rv = entry->show(mddev, page);
mddev_unlock(mddev);
return rv;
} }
static ssize_t static ssize_t
...@@ -1843,10 +1833,14 @@ md_attr_store(struct kobject *kobj, struct attribute *attr, ...@@ -1843,10 +1833,14 @@ md_attr_store(struct kobject *kobj, struct attribute *attr,
{ {
struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
ssize_t rv;
if (!entry->store) if (!entry->store)
return -EIO; return -EIO;
return entry->store(mddev, page, length); mddev_lock(mddev);
rv = entry->store(mddev, page, length);
mddev_unlock(mddev);
return rv;
} }
static void md_free(struct kobject *ko) static void md_free(struct kobject *ko)
......
...@@ -1746,7 +1746,10 @@ static ssize_t ...@@ -1746,7 +1746,10 @@ static ssize_t
raid5_show_stripe_cache_size(mddev_t *mddev, char *page) raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
{ {
raid5_conf_t *conf = mddev_to_conf(mddev); raid5_conf_t *conf = mddev_to_conf(mddev);
if (conf)
return sprintf(page, "%d\n", conf->max_nr_stripes); return sprintf(page, "%d\n", conf->max_nr_stripes);
else
return 0;
} }
static ssize_t static ssize_t
...@@ -1757,6 +1760,8 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) ...@@ -1757,6 +1760,8 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
int new; int new;
if (len >= PAGE_SIZE) if (len >= PAGE_SIZE)
return -EINVAL; return -EINVAL;
if (!conf)
return -ENODEV;
new = simple_strtoul(page, &end, 10); new = simple_strtoul(page, &end, 10);
if (!*page || (*end && *end != '\n') ) if (!*page || (*end && *end != '\n') )
...@@ -1777,23 +1782,23 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len) ...@@ -1777,23 +1782,23 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
return len; return len;
} }
static struct md_sysfs_entry raid5_stripecache_size = { static struct md_sysfs_entry
.attr = {.name = "stripe_cache_size", .mode = S_IRUGO | S_IWUSR }, raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
.show = raid5_show_stripe_cache_size, raid5_show_stripe_cache_size,
.store = raid5_store_stripe_cache_size, raid5_store_stripe_cache_size);
};
static ssize_t static ssize_t
raid5_show_stripe_cache_active(mddev_t *mddev, char *page) stripe_cache_active_show(mddev_t *mddev, char *page)
{ {
raid5_conf_t *conf = mddev_to_conf(mddev); raid5_conf_t *conf = mddev_to_conf(mddev);
if (conf)
return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
else
return 0;
} }
static struct md_sysfs_entry raid5_stripecache_active = { static struct md_sysfs_entry
.attr = {.name = "stripe_cache_active", .mode = S_IRUGO}, raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
.show = raid5_show_stripe_cache_active,
};
static struct attribute *raid5_attrs[] = { static struct attribute *raid5_attrs[] = {
&raid5_stripecache_size.attr, &raid5_stripecache_size.attr,
...@@ -1981,6 +1986,7 @@ static int stop(mddev_t *mddev) ...@@ -1981,6 +1986,7 @@ static int stop(mddev_t *mddev)
free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER); free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
sysfs_remove_group(&mddev->kobj, &raid5_attrs_group); sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
kfree(conf);
mddev->private = NULL; mddev->private = NULL;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment