James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 1 | /* |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 2 | * raid_class.c - implementation of a simple raid visualisation class |
| 3 | * |
| 4 | * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com> |
| 5 | * |
| 6 | * This file is licensed under GPLv2 |
| 7 | * |
| 8 | * This class is designed to allow raid attributes to be visualised and |
| 9 | * manipulated in a form independent of the underlying raid. Ultimately this |
| 10 | * should work for both hardware and software raids. |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/list.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/string.h> |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 17 | #include <linux/raid_class.h> |
| 18 | #include <scsi/scsi_device.h> |
| 19 | #include <scsi/scsi_host.h> |
| 20 | |
| 21 | #define RAID_NUM_ATTRS 3 |
| 22 | |
| 23 | struct raid_internal { |
| 24 | struct raid_template r; |
| 25 | struct raid_function_template *f; |
| 26 | /* The actual attributes */ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 27 | struct device_attribute private_attrs[RAID_NUM_ATTRS]; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 28 | /* The array of null terminated pointers to attributes |
| 29 | * needed by scsi_sysfs.c */ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 30 | struct device_attribute *attrs[RAID_NUM_ATTRS + 1]; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct raid_component { |
| 34 | struct list_head node; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 35 | struct device dev; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 36 | int num; |
| 37 | }; |
| 38 | |
| 39 | #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r) |
| 40 | |
| 41 | #define tc_to_raid_internal(tcont) ({ \ |
| 42 | struct raid_template *r = \ |
| 43 | container_of(tcont, struct raid_template, raid_attrs); \ |
| 44 | to_raid_internal(r); \ |
| 45 | }) |
| 46 | |
| 47 | #define ac_to_raid_internal(acont) ({ \ |
| 48 | struct transport_container *tc = \ |
| 49 | container_of(acont, struct transport_container, ac); \ |
| 50 | tc_to_raid_internal(tc); \ |
| 51 | }) |
| 52 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 53 | #define device_to_raid_internal(dev) ({ \ |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 54 | struct attribute_container *ac = \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 55 | attribute_container_classdev_to_container(dev); \ |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 56 | ac_to_raid_internal(ac); \ |
| 57 | }) |
| 58 | |
| 59 | |
| 60 | static int raid_match(struct attribute_container *cont, struct device *dev) |
| 61 | { |
| 62 | /* We have to look for every subsystem that could house |
| 63 | * emulated RAID devices, so start with SCSI */ |
| 64 | struct raid_internal *i = ac_to_raid_internal(cont); |
| 65 | |
James Bottomley | fac829f | 2010-03-03 11:06:56 +0530 | [diff] [blame] | 66 | #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 67 | if (scsi_is_sdev_device(dev)) { |
| 68 | struct scsi_device *sdev = to_scsi_device(dev); |
| 69 | |
| 70 | if (i->f->cookie != sdev->host->hostt) |
| 71 | return 0; |
| 72 | |
| 73 | return i->f->is_raid(dev); |
| 74 | } |
James Bottomley | fac829f | 2010-03-03 11:06:56 +0530 | [diff] [blame] | 75 | #endif |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 76 | /* FIXME: look at other subsystems too */ |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int raid_setup(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 81 | struct device *cdev) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 82 | { |
| 83 | struct raid_data *rd; |
| 84 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 85 | BUG_ON(dev_get_drvdata(cdev)); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 86 | |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 87 | rd = kzalloc(sizeof(*rd), GFP_KERNEL); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 88 | if (!rd) |
| 89 | return -ENOMEM; |
| 90 | |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 91 | INIT_LIST_HEAD(&rd->component_list); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 92 | dev_set_drvdata(cdev, rd); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int raid_remove(struct transport_container *tc, struct device *dev, |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 98 | struct device *cdev) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 99 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 100 | struct raid_data *rd = dev_get_drvdata(cdev); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 101 | struct raid_component *rc, *next; |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 102 | dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 103 | dev_set_drvdata(cdev, NULL); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 104 | list_for_each_entry_safe(rc, next, &rd->component_list, node) { |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 105 | list_del(&rc->node); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 106 | dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n"); |
| 107 | device_unregister(&rc->dev); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 108 | } |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 109 | dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); |
| 110 | kfree(rd); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static DECLARE_TRANSPORT_CLASS(raid_class, |
| 115 | "raid_devices", |
| 116 | raid_setup, |
| 117 | raid_remove, |
| 118 | NULL); |
| 119 | |
Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 120 | static const struct { |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 121 | enum raid_state value; |
| 122 | char *name; |
| 123 | } raid_states[] = { |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 124 | { RAID_STATE_UNKNOWN, "unknown" }, |
| 125 | { RAID_STATE_ACTIVE, "active" }, |
| 126 | { RAID_STATE_DEGRADED, "degraded" }, |
| 127 | { RAID_STATE_RESYNCING, "resyncing" }, |
| 128 | { RAID_STATE_OFFLINE, "offline" }, |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static const char *raid_state_name(enum raid_state state) |
| 132 | { |
| 133 | int i; |
| 134 | char *name = NULL; |
| 135 | |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 136 | for (i = 0; i < ARRAY_SIZE(raid_states); i++) { |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 137 | if (raid_states[i].value == state) { |
| 138 | name = raid_states[i].name; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | return name; |
| 143 | } |
| 144 | |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 145 | static struct { |
| 146 | enum raid_level value; |
| 147 | char *name; |
| 148 | } raid_levels[] = { |
| 149 | { RAID_LEVEL_UNKNOWN, "unknown" }, |
| 150 | { RAID_LEVEL_LINEAR, "linear" }, |
| 151 | { RAID_LEVEL_0, "raid0" }, |
| 152 | { RAID_LEVEL_1, "raid1" }, |
Moore, Eric | 8e32ca4 | 2006-01-04 14:58:43 -0700 | [diff] [blame] | 153 | { RAID_LEVEL_10, "raid10" }, |
Kashyap, Desai | 8e4a0cf | 2010-02-17 16:13:04 +0530 | [diff] [blame] | 154 | { RAID_LEVEL_1E, "raid1e" }, |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 155 | { RAID_LEVEL_3, "raid3" }, |
| 156 | { RAID_LEVEL_4, "raid4" }, |
| 157 | { RAID_LEVEL_5, "raid5" }, |
Moore, Eric | 8e32ca4 | 2006-01-04 14:58:43 -0700 | [diff] [blame] | 158 | { RAID_LEVEL_50, "raid50" }, |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 159 | { RAID_LEVEL_6, "raid6" }, |
| 160 | }; |
| 161 | |
| 162 | static const char *raid_level_name(enum raid_level level) |
| 163 | { |
| 164 | int i; |
| 165 | char *name = NULL; |
| 166 | |
Tobias Klauser | 6391a11 | 2006-06-08 22:23:48 -0700 | [diff] [blame] | 167 | for (i = 0; i < ARRAY_SIZE(raid_levels); i++) { |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 168 | if (raid_levels[i].value == level) { |
| 169 | name = raid_levels[i].name; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | return name; |
| 174 | } |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 175 | |
| 176 | #define raid_attr_show_internal(attr, fmt, var, code) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 177 | static ssize_t raid_show_##attr(struct device *dev, \ |
| 178 | struct device_attribute *attr, \ |
| 179 | char *buf) \ |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 180 | { \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 181 | struct raid_data *rd = dev_get_drvdata(dev); \ |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 182 | code \ |
| 183 | return snprintf(buf, 20, #fmt "\n", var); \ |
| 184 | } |
| 185 | |
| 186 | #define raid_attr_ro_states(attr, states, code) \ |
| 187 | raid_attr_show_internal(attr, %s, name, \ |
| 188 | const char *name; \ |
| 189 | code \ |
| 190 | name = raid_##states##_name(rd->attr); \ |
| 191 | ) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 192 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 193 | |
| 194 | |
| 195 | #define raid_attr_ro_internal(attr, code) \ |
| 196 | raid_attr_show_internal(attr, %d, rd->attr, code) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 197 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 198 | |
| 199 | #define ATTR_CODE(attr) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 200 | struct raid_internal *i = device_to_raid_internal(dev); \ |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 201 | if (i->f->get_##attr) \ |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 202 | i->f->get_##attr(dev->parent); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 203 | |
| 204 | #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) |
| 205 | #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 206 | #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) |
| 207 | #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 208 | |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 209 | |
| 210 | raid_attr_ro_state(level); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 211 | raid_attr_ro_fn(resync); |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 212 | raid_attr_ro_state_fn(state); |
| 213 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 214 | static void raid_component_release(struct device *dev) |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 215 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 216 | struct raid_component *rc = |
| 217 | container_of(dev, struct raid_component, dev); |
| 218 | dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n"); |
| 219 | put_device(rc->dev.parent); |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 220 | kfree(rc); |
| 221 | } |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 222 | |
Jeff Garzik | ed542be | 2006-10-04 07:05:11 -0400 | [diff] [blame] | 223 | int raid_component_add(struct raid_template *r,struct device *raid_dev, |
| 224 | struct device *component_dev) |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 225 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 226 | struct device *cdev = |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 227 | attribute_container_find_class_device(&r->raid_attrs.ac, |
| 228 | raid_dev); |
| 229 | struct raid_component *rc; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 230 | struct raid_data *rd = dev_get_drvdata(cdev); |
Jeff Garzik | ed542be | 2006-10-04 07:05:11 -0400 | [diff] [blame] | 231 | int err; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 232 | |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 233 | rc = kzalloc(sizeof(*rc), GFP_KERNEL); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 234 | if (!rc) |
Jeff Garzik | ed542be | 2006-10-04 07:05:11 -0400 | [diff] [blame] | 235 | return -ENOMEM; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 236 | |
| 237 | INIT_LIST_HEAD(&rc->node); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 238 | device_initialize(&rc->dev); |
| 239 | rc->dev.release = raid_component_release; |
| 240 | rc->dev.parent = get_device(component_dev); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 241 | rc->num = rd->component_count++; |
| 242 | |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 243 | dev_set_name(&rc->dev, "component-%d", rc->num); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 244 | list_add_tail(&rc->node, &rd->component_list); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 245 | rc->dev.class = &raid_class.class; |
| 246 | err = device_add(&rc->dev); |
Jeff Garzik | ed542be | 2006-10-04 07:05:11 -0400 | [diff] [blame] | 247 | if (err) |
| 248 | goto err_out; |
| 249 | |
| 250 | return 0; |
| 251 | |
| 252 | err_out: |
| 253 | list_del(&rc->node); |
| 254 | rd->component_count--; |
| 255 | put_device(component_dev); |
| 256 | kfree(rc); |
| 257 | return err; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 258 | } |
| 259 | EXPORT_SYMBOL(raid_component_add); |
| 260 | |
| 261 | struct raid_template * |
| 262 | raid_class_attach(struct raid_function_template *ft) |
| 263 | { |
James Bottomley | b1081ea | 2005-11-06 11:59:08 -0600 | [diff] [blame] | 264 | struct raid_internal *i = kzalloc(sizeof(struct raid_internal), |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 265 | GFP_KERNEL); |
| 266 | int count = 0; |
| 267 | |
| 268 | if (unlikely(!i)) |
| 269 | return NULL; |
| 270 | |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 271 | i->f = ft; |
| 272 | |
| 273 | i->r.raid_attrs.ac.class = &raid_class.class; |
| 274 | i->r.raid_attrs.ac.match = raid_match; |
| 275 | i->r.raid_attrs.ac.attrs = &i->attrs[0]; |
| 276 | |
| 277 | attribute_container_register(&i->r.raid_attrs.ac); |
| 278 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 279 | i->attrs[count++] = &dev_attr_level; |
| 280 | i->attrs[count++] = &dev_attr_resync; |
| 281 | i->attrs[count++] = &dev_attr_state; |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 282 | |
| 283 | i->attrs[count] = NULL; |
| 284 | BUG_ON(count > RAID_NUM_ATTRS); |
| 285 | |
| 286 | return &i->r; |
| 287 | } |
| 288 | EXPORT_SYMBOL(raid_class_attach); |
| 289 | |
| 290 | void |
| 291 | raid_class_release(struct raid_template *r) |
| 292 | { |
| 293 | struct raid_internal *i = to_raid_internal(r); |
| 294 | |
James Bottomley | 2f3edc6 | 2008-04-02 10:05:48 -0500 | [diff] [blame] | 295 | BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac)); |
James Bottomley | 61a7afa | 2005-08-16 18:27:34 -0500 | [diff] [blame] | 296 | |
| 297 | kfree(i); |
| 298 | } |
| 299 | EXPORT_SYMBOL(raid_class_release); |
| 300 | |
| 301 | static __init int raid_init(void) |
| 302 | { |
| 303 | return transport_class_register(&raid_class); |
| 304 | } |
| 305 | |
| 306 | static __exit void raid_exit(void) |
| 307 | { |
| 308 | transport_class_unregister(&raid_class); |
| 309 | } |
| 310 | |
| 311 | MODULE_AUTHOR("James Bottomley"); |
| 312 | MODULE_DESCRIPTION("RAID device class"); |
| 313 | MODULE_LICENSE("GPL"); |
| 314 | |
| 315 | module_init(raid_init); |
| 316 | module_exit(raid_exit); |
| 317 | |