161a7afa2SJames Bottomley /* 2b1081ea6SJames Bottomley * raid_class.c - implementation of a simple raid visualisation class 3b1081ea6SJames Bottomley * 4b1081ea6SJames Bottomley * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com> 5b1081ea6SJames Bottomley * 6b1081ea6SJames Bottomley * This file is licensed under GPLv2 7b1081ea6SJames Bottomley * 8b1081ea6SJames Bottomley * This class is designed to allow raid attributes to be visualised and 9b1081ea6SJames Bottomley * manipulated in a form independent of the underlying raid. Ultimately this 10b1081ea6SJames Bottomley * should work for both hardware and software raids. 1161a7afa2SJames Bottomley */ 1261a7afa2SJames Bottomley #include <linux/init.h> 1361a7afa2SJames Bottomley #include <linux/module.h> 1461a7afa2SJames Bottomley #include <linux/list.h> 158c65b4a6STim Schmielau #include <linux/slab.h> 168c65b4a6STim Schmielau #include <linux/string.h> 1761a7afa2SJames Bottomley #include <linux/raid_class.h> 1861a7afa2SJames Bottomley #include <scsi/scsi_device.h> 1961a7afa2SJames Bottomley #include <scsi/scsi_host.h> 2061a7afa2SJames Bottomley 2161a7afa2SJames Bottomley #define RAID_NUM_ATTRS 3 2261a7afa2SJames Bottomley 2361a7afa2SJames Bottomley struct raid_internal { 2461a7afa2SJames Bottomley struct raid_template r; 2561a7afa2SJames Bottomley struct raid_function_template *f; 2661a7afa2SJames Bottomley /* The actual attributes */ 27ee959b00STony Jones struct device_attribute private_attrs[RAID_NUM_ATTRS]; 2861a7afa2SJames Bottomley /* The array of null terminated pointers to attributes 2961a7afa2SJames Bottomley * needed by scsi_sysfs.c */ 30ee959b00STony Jones struct device_attribute *attrs[RAID_NUM_ATTRS + 1]; 3161a7afa2SJames Bottomley }; 3261a7afa2SJames Bottomley 3361a7afa2SJames Bottomley struct raid_component { 3461a7afa2SJames Bottomley struct list_head node; 35ee959b00STony Jones struct device dev; 3661a7afa2SJames Bottomley int num; 3761a7afa2SJames Bottomley }; 3861a7afa2SJames Bottomley 3961a7afa2SJames Bottomley #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r) 4061a7afa2SJames Bottomley 4161a7afa2SJames Bottomley #define tc_to_raid_internal(tcont) ({ \ 4261a7afa2SJames Bottomley struct raid_template *r = \ 4361a7afa2SJames Bottomley container_of(tcont, struct raid_template, raid_attrs); \ 4461a7afa2SJames Bottomley to_raid_internal(r); \ 4561a7afa2SJames Bottomley }) 4661a7afa2SJames Bottomley 4761a7afa2SJames Bottomley #define ac_to_raid_internal(acont) ({ \ 4861a7afa2SJames Bottomley struct transport_container *tc = \ 4961a7afa2SJames Bottomley container_of(acont, struct transport_container, ac); \ 5061a7afa2SJames Bottomley tc_to_raid_internal(tc); \ 5161a7afa2SJames Bottomley }) 5261a7afa2SJames Bottomley 53ee959b00STony Jones #define device_to_raid_internal(dev) ({ \ 5461a7afa2SJames Bottomley struct attribute_container *ac = \ 55ee959b00STony Jones attribute_container_classdev_to_container(dev); \ 5661a7afa2SJames Bottomley ac_to_raid_internal(ac); \ 5761a7afa2SJames Bottomley }) 5861a7afa2SJames Bottomley 5961a7afa2SJames Bottomley 6061a7afa2SJames Bottomley static int raid_match(struct attribute_container *cont, struct device *dev) 6161a7afa2SJames Bottomley { 6261a7afa2SJames Bottomley /* We have to look for every subsystem that could house 6361a7afa2SJames Bottomley * emulated RAID devices, so start with SCSI */ 6461a7afa2SJames Bottomley struct raid_internal *i = ac_to_raid_internal(cont); 6561a7afa2SJames Bottomley 66fac829fdSJames Bottomley #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE) 6761a7afa2SJames Bottomley if (scsi_is_sdev_device(dev)) { 6861a7afa2SJames Bottomley struct scsi_device *sdev = to_scsi_device(dev); 6961a7afa2SJames Bottomley 7061a7afa2SJames Bottomley if (i->f->cookie != sdev->host->hostt) 7161a7afa2SJames Bottomley return 0; 7261a7afa2SJames Bottomley 7361a7afa2SJames Bottomley return i->f->is_raid(dev); 7461a7afa2SJames Bottomley } 75fac829fdSJames Bottomley #endif 7661a7afa2SJames Bottomley /* FIXME: look at other subsystems too */ 7761a7afa2SJames Bottomley return 0; 7861a7afa2SJames Bottomley } 7961a7afa2SJames Bottomley 8061a7afa2SJames Bottomley static int raid_setup(struct transport_container *tc, struct device *dev, 81ee959b00STony Jones struct device *cdev) 8261a7afa2SJames Bottomley { 8361a7afa2SJames Bottomley struct raid_data *rd; 8461a7afa2SJames Bottomley 85ee959b00STony Jones BUG_ON(dev_get_drvdata(cdev)); 8661a7afa2SJames Bottomley 87b1081ea6SJames Bottomley rd = kzalloc(sizeof(*rd), GFP_KERNEL); 8861a7afa2SJames Bottomley if (!rd) 8961a7afa2SJames Bottomley return -ENOMEM; 9061a7afa2SJames Bottomley 9161a7afa2SJames Bottomley INIT_LIST_HEAD(&rd->component_list); 92ee959b00STony Jones dev_set_drvdata(cdev, rd); 9361a7afa2SJames Bottomley 9461a7afa2SJames Bottomley return 0; 9561a7afa2SJames Bottomley } 9661a7afa2SJames Bottomley 9761a7afa2SJames Bottomley static int raid_remove(struct transport_container *tc, struct device *dev, 98ee959b00STony Jones struct device *cdev) 9961a7afa2SJames Bottomley { 100ee959b00STony Jones struct raid_data *rd = dev_get_drvdata(cdev); 10161a7afa2SJames Bottomley struct raid_component *rc, *next; 102b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); 103ee959b00STony Jones dev_set_drvdata(cdev, NULL); 10461a7afa2SJames Bottomley list_for_each_entry_safe(rc, next, &rd->component_list, node) { 10561a7afa2SJames Bottomley list_del(&rc->node); 106ee959b00STony Jones dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n"); 107ee959b00STony Jones device_unregister(&rc->dev); 10861a7afa2SJames Bottomley } 109b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); 110b1081ea6SJames Bottomley kfree(rd); 11161a7afa2SJames Bottomley return 0; 11261a7afa2SJames Bottomley } 11361a7afa2SJames Bottomley 11461a7afa2SJames Bottomley static DECLARE_TRANSPORT_CLASS(raid_class, 11561a7afa2SJames Bottomley "raid_devices", 11661a7afa2SJames Bottomley raid_setup, 11761a7afa2SJames Bottomley raid_remove, 11861a7afa2SJames Bottomley NULL); 11961a7afa2SJames Bottomley 1200ad78200SArjan van de Ven static const struct { 12161a7afa2SJames Bottomley enum raid_state value; 12261a7afa2SJames Bottomley char *name; 12361a7afa2SJames Bottomley } raid_states[] = { 124b1081ea6SJames Bottomley { RAID_STATE_UNKNOWN, "unknown" }, 125b1081ea6SJames Bottomley { RAID_STATE_ACTIVE, "active" }, 126b1081ea6SJames Bottomley { RAID_STATE_DEGRADED, "degraded" }, 127b1081ea6SJames Bottomley { RAID_STATE_RESYNCING, "resyncing" }, 128b1081ea6SJames Bottomley { RAID_STATE_OFFLINE, "offline" }, 12961a7afa2SJames Bottomley }; 13061a7afa2SJames Bottomley 13161a7afa2SJames Bottomley static const char *raid_state_name(enum raid_state state) 13261a7afa2SJames Bottomley { 13361a7afa2SJames Bottomley int i; 13461a7afa2SJames Bottomley char *name = NULL; 13561a7afa2SJames Bottomley 1366391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(raid_states); i++) { 13761a7afa2SJames Bottomley if (raid_states[i].value == state) { 13861a7afa2SJames Bottomley name = raid_states[i].name; 13961a7afa2SJames Bottomley break; 14061a7afa2SJames Bottomley } 14161a7afa2SJames Bottomley } 14261a7afa2SJames Bottomley return name; 14361a7afa2SJames Bottomley } 14461a7afa2SJames Bottomley 145b1081ea6SJames Bottomley static struct { 146b1081ea6SJames Bottomley enum raid_level value; 147b1081ea6SJames Bottomley char *name; 148b1081ea6SJames Bottomley } raid_levels[] = { 149b1081ea6SJames Bottomley { RAID_LEVEL_UNKNOWN, "unknown" }, 150b1081ea6SJames Bottomley { RAID_LEVEL_LINEAR, "linear" }, 151b1081ea6SJames Bottomley { RAID_LEVEL_0, "raid0" }, 152b1081ea6SJames Bottomley { RAID_LEVEL_1, "raid1" }, 1538e32ca49SMoore, Eric { RAID_LEVEL_10, "raid10" }, 1548e4a0cf7SKashyap, Desai { RAID_LEVEL_1E, "raid1e" }, 155b1081ea6SJames Bottomley { RAID_LEVEL_3, "raid3" }, 156b1081ea6SJames Bottomley { RAID_LEVEL_4, "raid4" }, 157b1081ea6SJames Bottomley { RAID_LEVEL_5, "raid5" }, 1588e32ca49SMoore, Eric { RAID_LEVEL_50, "raid50" }, 159b1081ea6SJames Bottomley { RAID_LEVEL_6, "raid6" }, 160b1081ea6SJames Bottomley }; 161b1081ea6SJames Bottomley 162b1081ea6SJames Bottomley static const char *raid_level_name(enum raid_level level) 163b1081ea6SJames Bottomley { 164b1081ea6SJames Bottomley int i; 165b1081ea6SJames Bottomley char *name = NULL; 166b1081ea6SJames Bottomley 1676391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(raid_levels); i++) { 168b1081ea6SJames Bottomley if (raid_levels[i].value == level) { 169b1081ea6SJames Bottomley name = raid_levels[i].name; 170b1081ea6SJames Bottomley break; 171b1081ea6SJames Bottomley } 172b1081ea6SJames Bottomley } 173b1081ea6SJames Bottomley return name; 174b1081ea6SJames Bottomley } 17561a7afa2SJames Bottomley 17661a7afa2SJames Bottomley #define raid_attr_show_internal(attr, fmt, var, code) \ 177ee959b00STony Jones static ssize_t raid_show_##attr(struct device *dev, \ 178ee959b00STony Jones struct device_attribute *attr, \ 179ee959b00STony Jones char *buf) \ 18061a7afa2SJames Bottomley { \ 181ee959b00STony Jones struct raid_data *rd = dev_get_drvdata(dev); \ 18261a7afa2SJames Bottomley code \ 18361a7afa2SJames Bottomley return snprintf(buf, 20, #fmt "\n", var); \ 18461a7afa2SJames Bottomley } 18561a7afa2SJames Bottomley 18661a7afa2SJames Bottomley #define raid_attr_ro_states(attr, states, code) \ 18761a7afa2SJames Bottomley raid_attr_show_internal(attr, %s, name, \ 18861a7afa2SJames Bottomley const char *name; \ 18961a7afa2SJames Bottomley code \ 19061a7afa2SJames Bottomley name = raid_##states##_name(rd->attr); \ 19161a7afa2SJames Bottomley ) \ 192ee959b00STony Jones static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 19361a7afa2SJames Bottomley 19461a7afa2SJames Bottomley 19561a7afa2SJames Bottomley #define raid_attr_ro_internal(attr, code) \ 19661a7afa2SJames Bottomley raid_attr_show_internal(attr, %d, rd->attr, code) \ 197ee959b00STony Jones static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 19861a7afa2SJames Bottomley 19961a7afa2SJames Bottomley #define ATTR_CODE(attr) \ 200ee959b00STony Jones struct raid_internal *i = device_to_raid_internal(dev); \ 20161a7afa2SJames Bottomley if (i->f->get_##attr) \ 202ee959b00STony Jones i->f->get_##attr(dev->parent); 20361a7afa2SJames Bottomley 20461a7afa2SJames Bottomley #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) 20561a7afa2SJames Bottomley #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) 206b1081ea6SJames Bottomley #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) 207b1081ea6SJames Bottomley #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) 20861a7afa2SJames Bottomley 209b1081ea6SJames Bottomley 210b1081ea6SJames Bottomley raid_attr_ro_state(level); 21161a7afa2SJames Bottomley raid_attr_ro_fn(resync); 212b1081ea6SJames Bottomley raid_attr_ro_state_fn(state); 213b1081ea6SJames Bottomley 214ee959b00STony Jones static void raid_component_release(struct device *dev) 215b1081ea6SJames Bottomley { 216ee959b00STony Jones struct raid_component *rc = 217ee959b00STony Jones container_of(dev, struct raid_component, dev); 218ee959b00STony Jones dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n"); 219ee959b00STony Jones put_device(rc->dev.parent); 220b1081ea6SJames Bottomley kfree(rc); 221b1081ea6SJames Bottomley } 22261a7afa2SJames Bottomley 223ed542bedSJeff Garzik int raid_component_add(struct raid_template *r,struct device *raid_dev, 22461a7afa2SJames Bottomley struct device *component_dev) 22561a7afa2SJames Bottomley { 226ee959b00STony Jones struct device *cdev = 22761a7afa2SJames Bottomley attribute_container_find_class_device(&r->raid_attrs.ac, 22861a7afa2SJames Bottomley raid_dev); 22961a7afa2SJames Bottomley struct raid_component *rc; 230ee959b00STony Jones struct raid_data *rd = dev_get_drvdata(cdev); 231ed542bedSJeff Garzik int err; 23261a7afa2SJames Bottomley 233b1081ea6SJames Bottomley rc = kzalloc(sizeof(*rc), GFP_KERNEL); 23461a7afa2SJames Bottomley if (!rc) 235ed542bedSJeff Garzik return -ENOMEM; 23661a7afa2SJames Bottomley 23761a7afa2SJames Bottomley INIT_LIST_HEAD(&rc->node); 238ee959b00STony Jones device_initialize(&rc->dev); 239ee959b00STony Jones rc->dev.release = raid_component_release; 240ee959b00STony Jones rc->dev.parent = get_device(component_dev); 24161a7afa2SJames Bottomley rc->num = rd->component_count++; 24261a7afa2SJames Bottomley 24371610f55SKay Sievers dev_set_name(&rc->dev, "component-%d", rc->num); 24461a7afa2SJames Bottomley list_add_tail(&rc->node, &rd->component_list); 245ee959b00STony Jones rc->dev.class = &raid_class.class; 246ee959b00STony Jones err = device_add(&rc->dev); 247ed542bedSJeff Garzik if (err) 248ed542bedSJeff Garzik goto err_out; 249ed542bedSJeff Garzik 250ed542bedSJeff Garzik return 0; 251ed542bedSJeff Garzik 252ed542bedSJeff Garzik err_out: 253ed542bedSJeff Garzik list_del(&rc->node); 254ed542bedSJeff Garzik rd->component_count--; 255ed542bedSJeff Garzik put_device(component_dev); 256ed542bedSJeff Garzik kfree(rc); 257ed542bedSJeff Garzik return err; 25861a7afa2SJames Bottomley } 25961a7afa2SJames Bottomley EXPORT_SYMBOL(raid_component_add); 26061a7afa2SJames Bottomley 26161a7afa2SJames Bottomley struct raid_template * 26261a7afa2SJames Bottomley raid_class_attach(struct raid_function_template *ft) 26361a7afa2SJames Bottomley { 264b1081ea6SJames Bottomley struct raid_internal *i = kzalloc(sizeof(struct raid_internal), 26561a7afa2SJames Bottomley GFP_KERNEL); 26661a7afa2SJames Bottomley int count = 0; 26761a7afa2SJames Bottomley 26861a7afa2SJames Bottomley if (unlikely(!i)) 26961a7afa2SJames Bottomley return NULL; 27061a7afa2SJames Bottomley 27161a7afa2SJames Bottomley i->f = ft; 27261a7afa2SJames Bottomley 27361a7afa2SJames Bottomley i->r.raid_attrs.ac.class = &raid_class.class; 27461a7afa2SJames Bottomley i->r.raid_attrs.ac.match = raid_match; 27561a7afa2SJames Bottomley i->r.raid_attrs.ac.attrs = &i->attrs[0]; 27661a7afa2SJames Bottomley 27761a7afa2SJames Bottomley attribute_container_register(&i->r.raid_attrs.ac); 27861a7afa2SJames Bottomley 279ee959b00STony Jones i->attrs[count++] = &dev_attr_level; 280ee959b00STony Jones i->attrs[count++] = &dev_attr_resync; 281ee959b00STony Jones i->attrs[count++] = &dev_attr_state; 28261a7afa2SJames Bottomley 28361a7afa2SJames Bottomley i->attrs[count] = NULL; 28461a7afa2SJames Bottomley BUG_ON(count > RAID_NUM_ATTRS); 28561a7afa2SJames Bottomley 28661a7afa2SJames Bottomley return &i->r; 28761a7afa2SJames Bottomley } 28861a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_attach); 28961a7afa2SJames Bottomley 29061a7afa2SJames Bottomley void 29161a7afa2SJames Bottomley raid_class_release(struct raid_template *r) 29261a7afa2SJames Bottomley { 29361a7afa2SJames Bottomley struct raid_internal *i = to_raid_internal(r); 29461a7afa2SJames Bottomley 2952f3edc69SJames Bottomley BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac)); 29661a7afa2SJames Bottomley 29761a7afa2SJames Bottomley kfree(i); 29861a7afa2SJames Bottomley } 29961a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_release); 30061a7afa2SJames Bottomley 30161a7afa2SJames Bottomley static __init int raid_init(void) 30261a7afa2SJames Bottomley { 30361a7afa2SJames Bottomley return transport_class_register(&raid_class); 30461a7afa2SJames Bottomley } 30561a7afa2SJames Bottomley 30661a7afa2SJames Bottomley static __exit void raid_exit(void) 30761a7afa2SJames Bottomley { 30861a7afa2SJames Bottomley transport_class_unregister(&raid_class); 30961a7afa2SJames Bottomley } 31061a7afa2SJames Bottomley 31161a7afa2SJames Bottomley MODULE_AUTHOR("James Bottomley"); 31261a7afa2SJames Bottomley MODULE_DESCRIPTION("RAID device class"); 31361a7afa2SJames Bottomley MODULE_LICENSE("GPL"); 31461a7afa2SJames Bottomley 31561a7afa2SJames Bottomley module_init(raid_init); 31661a7afa2SJames Bottomley module_exit(raid_exit); 31761a7afa2SJames Bottomley 318