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> 1561a7afa2SJames Bottomley #include <linux/raid_class.h> 1661a7afa2SJames Bottomley #include <scsi/scsi_device.h> 1761a7afa2SJames Bottomley #include <scsi/scsi_host.h> 1861a7afa2SJames Bottomley 1961a7afa2SJames Bottomley #define RAID_NUM_ATTRS 3 2061a7afa2SJames Bottomley 2161a7afa2SJames Bottomley struct raid_internal { 2261a7afa2SJames Bottomley struct raid_template r; 2361a7afa2SJames Bottomley struct raid_function_template *f; 2461a7afa2SJames Bottomley /* The actual attributes */ 2561a7afa2SJames Bottomley struct class_device_attribute private_attrs[RAID_NUM_ATTRS]; 2661a7afa2SJames Bottomley /* The array of null terminated pointers to attributes 2761a7afa2SJames Bottomley * needed by scsi_sysfs.c */ 2861a7afa2SJames Bottomley struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1]; 2961a7afa2SJames Bottomley }; 3061a7afa2SJames Bottomley 3161a7afa2SJames Bottomley struct raid_component { 3261a7afa2SJames Bottomley struct list_head node; 33b1081ea6SJames Bottomley struct class_device cdev; 3461a7afa2SJames Bottomley int num; 3561a7afa2SJames Bottomley }; 3661a7afa2SJames Bottomley 3761a7afa2SJames Bottomley #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r) 3861a7afa2SJames Bottomley 3961a7afa2SJames Bottomley #define tc_to_raid_internal(tcont) ({ \ 4061a7afa2SJames Bottomley struct raid_template *r = \ 4161a7afa2SJames Bottomley container_of(tcont, struct raid_template, raid_attrs); \ 4261a7afa2SJames Bottomley to_raid_internal(r); \ 4361a7afa2SJames Bottomley }) 4461a7afa2SJames Bottomley 4561a7afa2SJames Bottomley #define ac_to_raid_internal(acont) ({ \ 4661a7afa2SJames Bottomley struct transport_container *tc = \ 4761a7afa2SJames Bottomley container_of(acont, struct transport_container, ac); \ 4861a7afa2SJames Bottomley tc_to_raid_internal(tc); \ 4961a7afa2SJames Bottomley }) 5061a7afa2SJames Bottomley 5161a7afa2SJames Bottomley #define class_device_to_raid_internal(cdev) ({ \ 5261a7afa2SJames Bottomley struct attribute_container *ac = \ 5361a7afa2SJames Bottomley attribute_container_classdev_to_container(cdev); \ 5461a7afa2SJames Bottomley ac_to_raid_internal(ac); \ 5561a7afa2SJames Bottomley }) 5661a7afa2SJames Bottomley 5761a7afa2SJames Bottomley 5861a7afa2SJames Bottomley static int raid_match(struct attribute_container *cont, struct device *dev) 5961a7afa2SJames Bottomley { 6061a7afa2SJames Bottomley /* We have to look for every subsystem that could house 6161a7afa2SJames Bottomley * emulated RAID devices, so start with SCSI */ 6261a7afa2SJames Bottomley struct raid_internal *i = ac_to_raid_internal(cont); 6361a7afa2SJames Bottomley 6461a7afa2SJames Bottomley if (scsi_is_sdev_device(dev)) { 6561a7afa2SJames Bottomley struct scsi_device *sdev = to_scsi_device(dev); 6661a7afa2SJames Bottomley 6761a7afa2SJames Bottomley if (i->f->cookie != sdev->host->hostt) 6861a7afa2SJames Bottomley return 0; 6961a7afa2SJames Bottomley 7061a7afa2SJames Bottomley return i->f->is_raid(dev); 7161a7afa2SJames Bottomley } 7261a7afa2SJames Bottomley /* FIXME: look at other subsystems too */ 7361a7afa2SJames Bottomley return 0; 7461a7afa2SJames Bottomley } 7561a7afa2SJames Bottomley 7661a7afa2SJames Bottomley static int raid_setup(struct transport_container *tc, struct device *dev, 7761a7afa2SJames Bottomley struct class_device *cdev) 7861a7afa2SJames Bottomley { 7961a7afa2SJames Bottomley struct raid_data *rd; 8061a7afa2SJames Bottomley 8161a7afa2SJames Bottomley BUG_ON(class_get_devdata(cdev)); 8261a7afa2SJames Bottomley 83b1081ea6SJames Bottomley rd = kzalloc(sizeof(*rd), GFP_KERNEL); 8461a7afa2SJames Bottomley if (!rd) 8561a7afa2SJames Bottomley return -ENOMEM; 8661a7afa2SJames Bottomley 8761a7afa2SJames Bottomley INIT_LIST_HEAD(&rd->component_list); 8861a7afa2SJames Bottomley class_set_devdata(cdev, rd); 8961a7afa2SJames Bottomley 9061a7afa2SJames Bottomley return 0; 9161a7afa2SJames Bottomley } 9261a7afa2SJames Bottomley 9361a7afa2SJames Bottomley static int raid_remove(struct transport_container *tc, struct device *dev, 9461a7afa2SJames Bottomley struct class_device *cdev) 9561a7afa2SJames Bottomley { 9661a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); 9761a7afa2SJames Bottomley struct raid_component *rc, *next; 98b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); 9961a7afa2SJames Bottomley class_set_devdata(cdev, NULL); 10061a7afa2SJames Bottomley list_for_each_entry_safe(rc, next, &rd->component_list, node) { 10161a7afa2SJames Bottomley list_del(&rc->node); 102b1081ea6SJames Bottomley dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n"); 103b1081ea6SJames Bottomley class_device_unregister(&rc->cdev); 10461a7afa2SJames Bottomley } 105b1081ea6SJames Bottomley dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); 106b1081ea6SJames Bottomley kfree(rd); 10761a7afa2SJames Bottomley return 0; 10861a7afa2SJames Bottomley } 10961a7afa2SJames Bottomley 11061a7afa2SJames Bottomley static DECLARE_TRANSPORT_CLASS(raid_class, 11161a7afa2SJames Bottomley "raid_devices", 11261a7afa2SJames Bottomley raid_setup, 11361a7afa2SJames Bottomley raid_remove, 11461a7afa2SJames Bottomley NULL); 11561a7afa2SJames Bottomley 11661a7afa2SJames Bottomley static struct { 11761a7afa2SJames Bottomley enum raid_state value; 11861a7afa2SJames Bottomley char *name; 11961a7afa2SJames Bottomley } raid_states[] = { 120b1081ea6SJames Bottomley { RAID_STATE_UNKNOWN, "unknown" }, 121b1081ea6SJames Bottomley { RAID_STATE_ACTIVE, "active" }, 122b1081ea6SJames Bottomley { RAID_STATE_DEGRADED, "degraded" }, 123b1081ea6SJames Bottomley { RAID_STATE_RESYNCING, "resyncing" }, 124b1081ea6SJames Bottomley { RAID_STATE_OFFLINE, "offline" }, 12561a7afa2SJames Bottomley }; 12661a7afa2SJames Bottomley 12761a7afa2SJames Bottomley static const char *raid_state_name(enum raid_state state) 12861a7afa2SJames Bottomley { 12961a7afa2SJames Bottomley int i; 13061a7afa2SJames Bottomley char *name = NULL; 13161a7afa2SJames Bottomley 13261a7afa2SJames Bottomley for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) { 13361a7afa2SJames Bottomley if (raid_states[i].value == state) { 13461a7afa2SJames Bottomley name = raid_states[i].name; 13561a7afa2SJames Bottomley break; 13661a7afa2SJames Bottomley } 13761a7afa2SJames Bottomley } 13861a7afa2SJames Bottomley return name; 13961a7afa2SJames Bottomley } 14061a7afa2SJames Bottomley 141b1081ea6SJames Bottomley static struct { 142b1081ea6SJames Bottomley enum raid_level value; 143b1081ea6SJames Bottomley char *name; 144b1081ea6SJames Bottomley } raid_levels[] = { 145b1081ea6SJames Bottomley { RAID_LEVEL_UNKNOWN, "unknown" }, 146b1081ea6SJames Bottomley { RAID_LEVEL_LINEAR, "linear" }, 147b1081ea6SJames Bottomley { RAID_LEVEL_0, "raid0" }, 148b1081ea6SJames Bottomley { RAID_LEVEL_1, "raid1" }, 149b1081ea6SJames Bottomley { RAID_LEVEL_3, "raid3" }, 150b1081ea6SJames Bottomley { RAID_LEVEL_4, "raid4" }, 151b1081ea6SJames Bottomley { RAID_LEVEL_5, "raid5" }, 152b1081ea6SJames Bottomley { RAID_LEVEL_6, "raid6" }, 153b1081ea6SJames Bottomley }; 154b1081ea6SJames Bottomley 155b1081ea6SJames Bottomley static const char *raid_level_name(enum raid_level level) 156b1081ea6SJames Bottomley { 157b1081ea6SJames Bottomley int i; 158b1081ea6SJames Bottomley char *name = NULL; 159b1081ea6SJames Bottomley 160b1081ea6SJames Bottomley for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) { 161b1081ea6SJames Bottomley if (raid_levels[i].value == level) { 162b1081ea6SJames Bottomley name = raid_levels[i].name; 163b1081ea6SJames Bottomley break; 164b1081ea6SJames Bottomley } 165b1081ea6SJames Bottomley } 166b1081ea6SJames Bottomley return name; 167b1081ea6SJames Bottomley } 16861a7afa2SJames Bottomley 16961a7afa2SJames Bottomley #define raid_attr_show_internal(attr, fmt, var, code) \ 17061a7afa2SJames Bottomley static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \ 17161a7afa2SJames Bottomley { \ 17261a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); \ 17361a7afa2SJames Bottomley code \ 17461a7afa2SJames Bottomley return snprintf(buf, 20, #fmt "\n", var); \ 17561a7afa2SJames Bottomley } 17661a7afa2SJames Bottomley 17761a7afa2SJames Bottomley #define raid_attr_ro_states(attr, states, code) \ 17861a7afa2SJames Bottomley raid_attr_show_internal(attr, %s, name, \ 17961a7afa2SJames Bottomley const char *name; \ 18061a7afa2SJames Bottomley code \ 18161a7afa2SJames Bottomley name = raid_##states##_name(rd->attr); \ 18261a7afa2SJames Bottomley ) \ 18361a7afa2SJames Bottomley static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 18461a7afa2SJames Bottomley 18561a7afa2SJames Bottomley 18661a7afa2SJames Bottomley #define raid_attr_ro_internal(attr, code) \ 18761a7afa2SJames Bottomley raid_attr_show_internal(attr, %d, rd->attr, code) \ 18861a7afa2SJames Bottomley static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) 18961a7afa2SJames Bottomley 19061a7afa2SJames Bottomley #define ATTR_CODE(attr) \ 19161a7afa2SJames Bottomley struct raid_internal *i = class_device_to_raid_internal(cdev); \ 19261a7afa2SJames Bottomley if (i->f->get_##attr) \ 19361a7afa2SJames Bottomley i->f->get_##attr(cdev->dev); 19461a7afa2SJames Bottomley 19561a7afa2SJames Bottomley #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) 19661a7afa2SJames Bottomley #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) 197b1081ea6SJames Bottomley #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) 198b1081ea6SJames Bottomley #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) 19961a7afa2SJames Bottomley 200b1081ea6SJames Bottomley 201b1081ea6SJames Bottomley raid_attr_ro_state(level); 20261a7afa2SJames Bottomley raid_attr_ro_fn(resync); 203b1081ea6SJames Bottomley raid_attr_ro_state_fn(state); 204b1081ea6SJames Bottomley 205b1081ea6SJames Bottomley static void raid_component_release(struct class_device *cdev) 206b1081ea6SJames Bottomley { 207b1081ea6SJames Bottomley struct raid_component *rc = container_of(cdev, struct raid_component, 208b1081ea6SJames Bottomley cdev); 209b1081ea6SJames Bottomley dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n"); 210b1081ea6SJames Bottomley put_device(rc->cdev.dev); 211b1081ea6SJames Bottomley kfree(rc); 212b1081ea6SJames Bottomley } 21361a7afa2SJames Bottomley 21461a7afa2SJames Bottomley void raid_component_add(struct raid_template *r,struct device *raid_dev, 21561a7afa2SJames Bottomley struct device *component_dev) 21661a7afa2SJames Bottomley { 21761a7afa2SJames Bottomley struct class_device *cdev = 21861a7afa2SJames Bottomley attribute_container_find_class_device(&r->raid_attrs.ac, 21961a7afa2SJames Bottomley raid_dev); 22061a7afa2SJames Bottomley struct raid_component *rc; 22161a7afa2SJames Bottomley struct raid_data *rd = class_get_devdata(cdev); 22261a7afa2SJames Bottomley 223b1081ea6SJames Bottomley rc = kzalloc(sizeof(*rc), GFP_KERNEL); 22461a7afa2SJames Bottomley if (!rc) 22561a7afa2SJames Bottomley return; 22661a7afa2SJames Bottomley 22761a7afa2SJames Bottomley INIT_LIST_HEAD(&rc->node); 228b1081ea6SJames Bottomley class_device_initialize(&rc->cdev); 229b1081ea6SJames Bottomley rc->cdev.release = raid_component_release; 230b1081ea6SJames Bottomley rc->cdev.dev = get_device(component_dev); 23161a7afa2SJames Bottomley rc->num = rd->component_count++; 23261a7afa2SJames Bottomley 233b1081ea6SJames Bottomley snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id), 234b1081ea6SJames Bottomley "component-%d", rc->num); 23561a7afa2SJames Bottomley list_add_tail(&rc->node, &rd->component_list); 236b1081ea6SJames Bottomley rc->cdev.parent = cdev; 237b1081ea6SJames Bottomley rc->cdev.class = &raid_class.class; 238b1081ea6SJames Bottomley class_device_add(&rc->cdev); 23961a7afa2SJames Bottomley } 24061a7afa2SJames Bottomley EXPORT_SYMBOL(raid_component_add); 24161a7afa2SJames Bottomley 24261a7afa2SJames Bottomley struct raid_template * 24361a7afa2SJames Bottomley raid_class_attach(struct raid_function_template *ft) 24461a7afa2SJames Bottomley { 245b1081ea6SJames Bottomley struct raid_internal *i = kzalloc(sizeof(struct raid_internal), 24661a7afa2SJames Bottomley GFP_KERNEL); 24761a7afa2SJames Bottomley int count = 0; 24861a7afa2SJames Bottomley 24961a7afa2SJames Bottomley if (unlikely(!i)) 25061a7afa2SJames Bottomley return NULL; 25161a7afa2SJames Bottomley 25261a7afa2SJames Bottomley i->f = ft; 25361a7afa2SJames Bottomley 25461a7afa2SJames Bottomley i->r.raid_attrs.ac.class = &raid_class.class; 25561a7afa2SJames Bottomley i->r.raid_attrs.ac.match = raid_match; 25661a7afa2SJames Bottomley i->r.raid_attrs.ac.attrs = &i->attrs[0]; 25761a7afa2SJames Bottomley 25861a7afa2SJames Bottomley attribute_container_register(&i->r.raid_attrs.ac); 25961a7afa2SJames Bottomley 26061a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_level; 26161a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_resync; 26261a7afa2SJames Bottomley i->attrs[count++] = &class_device_attr_state; 26361a7afa2SJames Bottomley 26461a7afa2SJames Bottomley i->attrs[count] = NULL; 26561a7afa2SJames Bottomley BUG_ON(count > RAID_NUM_ATTRS); 26661a7afa2SJames Bottomley 26761a7afa2SJames Bottomley return &i->r; 26861a7afa2SJames Bottomley } 26961a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_attach); 27061a7afa2SJames Bottomley 27161a7afa2SJames Bottomley void 27261a7afa2SJames Bottomley raid_class_release(struct raid_template *r) 27361a7afa2SJames Bottomley { 27461a7afa2SJames Bottomley struct raid_internal *i = to_raid_internal(r); 27561a7afa2SJames Bottomley 27661a7afa2SJames Bottomley attribute_container_unregister(&i->r.raid_attrs.ac); 27761a7afa2SJames Bottomley 27861a7afa2SJames Bottomley kfree(i); 27961a7afa2SJames Bottomley } 28061a7afa2SJames Bottomley EXPORT_SYMBOL(raid_class_release); 28161a7afa2SJames Bottomley 28261a7afa2SJames Bottomley static __init int raid_init(void) 28361a7afa2SJames Bottomley { 28461a7afa2SJames Bottomley return transport_class_register(&raid_class); 28561a7afa2SJames Bottomley } 28661a7afa2SJames Bottomley 28761a7afa2SJames Bottomley static __exit void raid_exit(void) 28861a7afa2SJames Bottomley { 28961a7afa2SJames Bottomley transport_class_unregister(&raid_class); 29061a7afa2SJames Bottomley } 29161a7afa2SJames Bottomley 29261a7afa2SJames Bottomley MODULE_AUTHOR("James Bottomley"); 29361a7afa2SJames Bottomley MODULE_DESCRIPTION("RAID device class"); 29461a7afa2SJames Bottomley MODULE_LICENSE("GPL"); 29561a7afa2SJames Bottomley 29661a7afa2SJames Bottomley module_init(raid_init); 29761a7afa2SJames Bottomley module_exit(raid_exit); 29861a7afa2SJames Bottomley 299