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 */ 2761a7afa2SJames Bottomley struct class_device_attribute private_attrs[RAID_NUM_ATTRS]; 2861a7afa2SJames Bottomley /* The array of null terminated pointers to attributes 2961a7afa2SJames Bottomley * needed by scsi_sysfs.c */ 3061a7afa2SJames Bottomley struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1]; 3161a7afa2SJames Bottomley }; 3261a7afa2SJames Bottomley 3361a7afa2SJames Bottomley struct raid_component { 3461a7afa2SJames Bottomley struct list_head node; 35b1081ea6SJames Bottomley struct class_device cdev; 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 5361a7afa2SJames Bottomley #define class_device_to_raid_internal(cdev) ({ \ 5461a7afa2SJames Bottomley struct attribute_container *ac = \ 5561a7afa2SJames Bottomley attribute_container_classdev_to_container(cdev); \ 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 6661a7afa2SJames Bottomley if (scsi_is_sdev_device(dev)) { 6761a7afa2SJames Bottomley struct scsi_device *sdev = to_scsi_device(dev); 6861a7afa2SJames Bottomley 6961a7afa2SJames Bottomley if (i->f->cookie != sdev->host->hostt) 7061a7afa2SJames Bottomley return 0; 7161a7afa2SJames Bottomley 7261a7afa2SJames Bottomley return i->f->is_raid(dev); 7361a7afa2SJames Bottomley } 7461a7afa2SJames Bottomley /* FIXME: look at other subsystems too */ 7561a7afa2SJames Bottomley return 0; 7661a7afa2SJames Bottomley } 7761a7afa2SJames Bottomley 7861a7afa2SJames Bottomley static int raid_setup(struct transport_container *tc, struct device *dev, 7961a7afa2SJames Bottomley struct class_device *cdev) 8061a7afa2SJames Bottomley { 8161a7afa2SJames Bottomley struct raid_data *rd; 8261a7afa2SJames Bottomley 8361a7afa2SJames Bottomley BUG_ON(class_get_devdata(cdev)); 8461a7afa2SJames Bottomley 85b1081ea6SJames Bottomley rd = kzalloc(sizeof(*rd), GFP_KERNEL); 8661a7afa2SJames Bottomley if (!rd) 8761a7afa2SJames Bottomley return -ENOMEM; 8861a7afa2SJames Bottomley 8961a7afa2SJames Bottomley INIT_LIST_HEAD(&rd->component_list); 9061a7afa2SJames Bottomley class_set_devdata(cdev, rd); 9161a7afa2SJames Bottomley 9261a7afa2SJames Bottomley return 0; 9361a7afa2SJames Bottomley } 9461a7afa2SJames Bottomley 9561a7afa2SJames Bottomley static int raid_remove(struct transport_container *tc, struct device *dev, 9661a7afa2SJames Bottomley struct class_device *cdev) 9761a7afa2SJames Bottomley { 9861a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); 9961a7afa2SJames Bottomley struct raid_component *rc, *next; 100b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); 10161a7afa2SJames Bottomley class_set_devdata(cdev, NULL); 10261a7afa2SJames Bottomley list_for_each_entry_safe(rc, next, &rd->component_list, node) { 10361a7afa2SJames Bottomley list_del(&rc->node); 104b1081ea6SJames Bottomley dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n"); 105b1081ea6SJames Bottomley class_device_unregister(&rc->cdev); 10661a7afa2SJames Bottomley } 107b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); 108b1081ea6SJames Bottomley kfree(rd); 10961a7afa2SJames Bottomley return 0; 11061a7afa2SJames Bottomley } 11161a7afa2SJames Bottomley 11261a7afa2SJames Bottomley static DECLARE_TRANSPORT_CLASS(raid_class, 11361a7afa2SJames Bottomley "raid_devices", 11461a7afa2SJames Bottomley raid_setup, 11561a7afa2SJames Bottomley raid_remove, 11661a7afa2SJames Bottomley NULL); 11761a7afa2SJames Bottomley 1180ad78200SArjan van de Ven static const struct { 11961a7afa2SJames Bottomley enum raid_state value; 12061a7afa2SJames Bottomley char *name; 12161a7afa2SJames Bottomley } raid_states[] = { 122b1081ea6SJames Bottomley { RAID_STATE_UNKNOWN, "unknown" }, 123b1081ea6SJames Bottomley { RAID_STATE_ACTIVE, "active" }, 124b1081ea6SJames Bottomley { RAID_STATE_DEGRADED, "degraded" }, 125b1081ea6SJames Bottomley { RAID_STATE_RESYNCING, "resyncing" }, 126b1081ea6SJames Bottomley { RAID_STATE_OFFLINE, "offline" }, 12761a7afa2SJames Bottomley }; 12861a7afa2SJames Bottomley 12961a7afa2SJames Bottomley static const char *raid_state_name(enum raid_state state) 13061a7afa2SJames Bottomley { 13161a7afa2SJames Bottomley int i; 13261a7afa2SJames Bottomley char *name = NULL; 13361a7afa2SJames Bottomley 1346391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(raid_states); i++) { 13561a7afa2SJames Bottomley if (raid_states[i].value == state) { 13661a7afa2SJames Bottomley name = raid_states[i].name; 13761a7afa2SJames Bottomley break; 13861a7afa2SJames Bottomley } 13961a7afa2SJames Bottomley } 14061a7afa2SJames Bottomley return name; 14161a7afa2SJames Bottomley } 14261a7afa2SJames Bottomley 143b1081ea6SJames Bottomley static struct { 144b1081ea6SJames Bottomley enum raid_level value; 145b1081ea6SJames Bottomley char *name; 146b1081ea6SJames Bottomley } raid_levels[] = { 147b1081ea6SJames Bottomley { RAID_LEVEL_UNKNOWN, "unknown" }, 148b1081ea6SJames Bottomley { RAID_LEVEL_LINEAR, "linear" }, 149b1081ea6SJames Bottomley { RAID_LEVEL_0, "raid0" }, 150b1081ea6SJames Bottomley { RAID_LEVEL_1, "raid1" }, 1518e32ca49SMoore, Eric { RAID_LEVEL_10, "raid10" }, 152b1081ea6SJames Bottomley { RAID_LEVEL_3, "raid3" }, 153b1081ea6SJames Bottomley { RAID_LEVEL_4, "raid4" }, 154b1081ea6SJames Bottomley { RAID_LEVEL_5, "raid5" }, 1558e32ca49SMoore, Eric { RAID_LEVEL_50, "raid50" }, 156b1081ea6SJames Bottomley { RAID_LEVEL_6, "raid6" }, 157b1081ea6SJames Bottomley }; 158b1081ea6SJames Bottomley 159b1081ea6SJames Bottomley static const char *raid_level_name(enum raid_level level) 160b1081ea6SJames Bottomley { 161b1081ea6SJames Bottomley int i; 162b1081ea6SJames Bottomley char *name = NULL; 163b1081ea6SJames Bottomley 1646391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(raid_levels); i++) { 165b1081ea6SJames Bottomley if (raid_levels[i].value == level) { 166b1081ea6SJames Bottomley name = raid_levels[i].name; 167b1081ea6SJames Bottomley break; 168b1081ea6SJames Bottomley } 169b1081ea6SJames Bottomley } 170b1081ea6SJames Bottomley return name; 171b1081ea6SJames Bottomley } 17261a7afa2SJames Bottomley 17361a7afa2SJames Bottomley #define raid_attr_show_internal(attr, fmt, var, code) \ 17461a7afa2SJames Bottomley static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \ 17561a7afa2SJames Bottomley { \ 17661a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); \ 17761a7afa2SJames Bottomley code \ 17861a7afa2SJames Bottomley return snprintf(buf, 20, #fmt "\n", var); \ 17961a7afa2SJames Bottomley } 18061a7afa2SJames Bottomley 18161a7afa2SJames Bottomley #define raid_attr_ro_states(attr, states, code) \ 18261a7afa2SJames Bottomley raid_attr_show_internal(attr, %s, name, \ 18361a7afa2SJames Bottomley const char *name; \ 18461a7afa2SJames Bottomley code \ 18561a7afa2SJames Bottomley name = raid_##states##_name(rd->attr); \ 18661a7afa2SJames Bottomley ) \ 18761a7afa2SJames Bottomley static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 18861a7afa2SJames Bottomley 18961a7afa2SJames Bottomley 19061a7afa2SJames Bottomley #define raid_attr_ro_internal(attr, code) \ 19161a7afa2SJames Bottomley raid_attr_show_internal(attr, %d, rd->attr, code) \ 19261a7afa2SJames Bottomley static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 19361a7afa2SJames Bottomley 19461a7afa2SJames Bottomley #define ATTR_CODE(attr) \ 19561a7afa2SJames Bottomley struct raid_internal *i = class_device_to_raid_internal(cdev); \ 19661a7afa2SJames Bottomley if (i->f->get_##attr) \ 19761a7afa2SJames Bottomley i->f->get_##attr(cdev->dev); 19861a7afa2SJames Bottomley 19961a7afa2SJames Bottomley #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) 20061a7afa2SJames Bottomley #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) 201b1081ea6SJames Bottomley #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) 202b1081ea6SJames Bottomley #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) 20361a7afa2SJames Bottomley 204b1081ea6SJames Bottomley 205b1081ea6SJames Bottomley raid_attr_ro_state(level); 20661a7afa2SJames Bottomley raid_attr_ro_fn(resync); 207b1081ea6SJames Bottomley raid_attr_ro_state_fn(state); 208b1081ea6SJames Bottomley 209b1081ea6SJames Bottomley static void raid_component_release(struct class_device *cdev) 210b1081ea6SJames Bottomley { 211b1081ea6SJames Bottomley struct raid_component *rc = container_of(cdev, struct raid_component, 212b1081ea6SJames Bottomley cdev); 213b1081ea6SJames Bottomley dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n"); 214b1081ea6SJames Bottomley put_device(rc->cdev.dev); 215b1081ea6SJames Bottomley kfree(rc); 216b1081ea6SJames Bottomley } 21761a7afa2SJames Bottomley 21861a7afa2SJames Bottomley void raid_component_add(struct raid_template *r,struct device *raid_dev, 21961a7afa2SJames Bottomley struct device *component_dev) 22061a7afa2SJames Bottomley { 22161a7afa2SJames Bottomley struct class_device *cdev = 22261a7afa2SJames Bottomley attribute_container_find_class_device(&r->raid_attrs.ac, 22361a7afa2SJames Bottomley raid_dev); 22461a7afa2SJames Bottomley struct raid_component *rc; 22561a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); 22661a7afa2SJames Bottomley 227b1081ea6SJames Bottomley rc = kzalloc(sizeof(*rc), GFP_KERNEL); 22861a7afa2SJames Bottomley if (!rc) 22961a7afa2SJames Bottomley return; 23061a7afa2SJames Bottomley 23161a7afa2SJames Bottomley INIT_LIST_HEAD(&rc->node); 232b1081ea6SJames Bottomley class_device_initialize(&rc->cdev); 233b1081ea6SJames Bottomley rc->cdev.release = raid_component_release; 234b1081ea6SJames Bottomley rc->cdev.dev = get_device(component_dev); 23561a7afa2SJames Bottomley rc->num = rd->component_count++; 23661a7afa2SJames Bottomley 237b1081ea6SJames Bottomley snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id), 238b1081ea6SJames Bottomley "component-%d", rc->num); 23961a7afa2SJames Bottomley list_add_tail(&rc->node, &rd->component_list); 240b1081ea6SJames Bottomley rc->cdev.parent = cdev; 241b1081ea6SJames Bottomley rc->cdev.class = &raid_class.class; 242b1081ea6SJames Bottomley class_device_add(&rc->cdev); 24361a7afa2SJames Bottomley } 24461a7afa2SJames Bottomley EXPORT_SYMBOL(raid_component_add); 24561a7afa2SJames Bottomley 24661a7afa2SJames Bottomley struct raid_template * 24761a7afa2SJames Bottomley raid_class_attach(struct raid_function_template *ft) 24861a7afa2SJames Bottomley { 249b1081ea6SJames Bottomley struct raid_internal *i = kzalloc(sizeof(struct raid_internal), 25061a7afa2SJames Bottomley GFP_KERNEL); 25161a7afa2SJames Bottomley int count = 0; 25261a7afa2SJames Bottomley 25361a7afa2SJames Bottomley if (unlikely(!i)) 25461a7afa2SJames Bottomley return NULL; 25561a7afa2SJames Bottomley 25661a7afa2SJames Bottomley i->f = ft; 25761a7afa2SJames Bottomley 25861a7afa2SJames Bottomley i->r.raid_attrs.ac.class = &raid_class.class; 25961a7afa2SJames Bottomley i->r.raid_attrs.ac.match = raid_match; 26061a7afa2SJames Bottomley i->r.raid_attrs.ac.attrs = &i->attrs[0]; 26161a7afa2SJames Bottomley 26261a7afa2SJames Bottomley attribute_container_register(&i->r.raid_attrs.ac); 26361a7afa2SJames Bottomley 26461a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_level; 26561a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_resync; 26661a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_state; 26761a7afa2SJames Bottomley 26861a7afa2SJames Bottomley i->attrs[count] = NULL; 26961a7afa2SJames Bottomley BUG_ON(count > RAID_NUM_ATTRS); 27061a7afa2SJames Bottomley 27161a7afa2SJames Bottomley return &i->r; 27261a7afa2SJames Bottomley } 27361a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_attach); 27461a7afa2SJames Bottomley 27561a7afa2SJames Bottomley void 27661a7afa2SJames Bottomley raid_class_release(struct raid_template *r) 27761a7afa2SJames Bottomley { 27861a7afa2SJames Bottomley struct raid_internal *i = to_raid_internal(r); 27961a7afa2SJames Bottomley 28061a7afa2SJames Bottomley attribute_container_unregister(&i->r.raid_attrs.ac); 28161a7afa2SJames Bottomley 28261a7afa2SJames Bottomley kfree(i); 28361a7afa2SJames Bottomley } 28461a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_release); 28561a7afa2SJames Bottomley 28661a7afa2SJames Bottomley static __init int raid_init(void) 28761a7afa2SJames Bottomley { 28861a7afa2SJames Bottomley return transport_class_register(&raid_class); 28961a7afa2SJames Bottomley } 29061a7afa2SJames Bottomley 29161a7afa2SJames Bottomley static __exit void raid_exit(void) 29261a7afa2SJames Bottomley { 29361a7afa2SJames Bottomley transport_class_unregister(&raid_class); 29461a7afa2SJames Bottomley } 29561a7afa2SJames Bottomley 29661a7afa2SJames Bottomley MODULE_AUTHOR("James Bottomley"); 29761a7afa2SJames Bottomley MODULE_DESCRIPTION("RAID device class"); 29861a7afa2SJames Bottomley MODULE_LICENSE("GPL"); 29961a7afa2SJames Bottomley 30061a7afa2SJames Bottomley module_init(raid_init); 30161a7afa2SJames Bottomley module_exit(raid_exit); 30261a7afa2SJames Bottomley 303