2 * raid_class.c - implementation of a simple raid visualisation class
4 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
6 * This file is licensed under GPLv2
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.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/raid_class.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_host.h>
21 #define RAID_NUM_ATTRS 3
23 struct raid_internal {
24 struct raid_template r;
25 struct raid_function_template *f;
26 /* The actual attributes */
27 struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
28 /* The array of null terminated pointers to attributes
29 * needed by scsi_sysfs.c */
30 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
33 struct raid_component {
34 struct list_head node;
35 struct class_device cdev;
39 #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
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); \
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); \
53 #define class_device_to_raid_internal(cdev) ({ \
54 struct attribute_container *ac = \
55 attribute_container_classdev_to_container(cdev); \
56 ac_to_raid_internal(ac); \
60 static int raid_match(struct attribute_container *cont, struct device *dev)
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);
66 if (scsi_is_sdev_device(dev)) {
67 struct scsi_device *sdev = to_scsi_device(dev);
69 if (i->f->cookie != sdev->host->hostt)
72 return i->f->is_raid(dev);
74 /* FIXME: look at other subsystems too */
78 static int raid_setup(struct transport_container *tc, struct device *dev,
79 struct class_device *cdev)
83 BUG_ON(class_get_devdata(cdev));
85 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
89 INIT_LIST_HEAD(&rd->component_list);
90 class_set_devdata(cdev, rd);
95 static int raid_remove(struct transport_container *tc, struct device *dev,
96 struct class_device *cdev)
98 struct raid_data *rd = class_get_devdata(cdev);
99 struct raid_component *rc, *next;
100 dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
101 class_set_devdata(cdev, NULL);
102 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
104 dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
105 class_device_unregister(&rc->cdev);
107 dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
112 static DECLARE_TRANSPORT_CLASS(raid_class,
118 static const struct {
119 enum raid_state value;
122 { RAID_STATE_UNKNOWN, "unknown" },
123 { RAID_STATE_ACTIVE, "active" },
124 { RAID_STATE_DEGRADED, "degraded" },
125 { RAID_STATE_RESYNCING, "resyncing" },
126 { RAID_STATE_OFFLINE, "offline" },
129 static const char *raid_state_name(enum raid_state state)
134 for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
135 if (raid_states[i].value == state) {
136 name = raid_states[i].name;
144 enum raid_level value;
147 { RAID_LEVEL_UNKNOWN, "unknown" },
148 { RAID_LEVEL_LINEAR, "linear" },
149 { RAID_LEVEL_0, "raid0" },
150 { RAID_LEVEL_1, "raid1" },
151 { RAID_LEVEL_10, "raid10" },
152 { RAID_LEVEL_3, "raid3" },
153 { RAID_LEVEL_4, "raid4" },
154 { RAID_LEVEL_5, "raid5" },
155 { RAID_LEVEL_50, "raid50" },
156 { RAID_LEVEL_6, "raid6" },
159 static const char *raid_level_name(enum raid_level level)
164 for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
165 if (raid_levels[i].value == level) {
166 name = raid_levels[i].name;
173 #define raid_attr_show_internal(attr, fmt, var, code) \
174 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
176 struct raid_data *rd = class_get_devdata(cdev); \
178 return snprintf(buf, 20, #fmt "\n", var); \
181 #define raid_attr_ro_states(attr, states, code) \
182 raid_attr_show_internal(attr, %s, name, \
185 name = raid_##states##_name(rd->attr); \
187 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
190 #define raid_attr_ro_internal(attr, code) \
191 raid_attr_show_internal(attr, %d, rd->attr, code) \
192 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
194 #define ATTR_CODE(attr) \
195 struct raid_internal *i = class_device_to_raid_internal(cdev); \
196 if (i->f->get_##attr) \
197 i->f->get_##attr(cdev->dev);
199 #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
200 #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
201 #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
202 #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
205 raid_attr_ro_state(level);
206 raid_attr_ro_fn(resync);
207 raid_attr_ro_state_fn(state);
209 static void raid_component_release(struct class_device *cdev)
211 struct raid_component *rc = container_of(cdev, struct raid_component,
213 dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
214 put_device(rc->cdev.dev);
218 void raid_component_add(struct raid_template *r,struct device *raid_dev,
219 struct device *component_dev)
221 struct class_device *cdev =
222 attribute_container_find_class_device(&r->raid_attrs.ac,
224 struct raid_component *rc;
225 struct raid_data *rd = class_get_devdata(cdev);
227 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
231 INIT_LIST_HEAD(&rc->node);
232 class_device_initialize(&rc->cdev);
233 rc->cdev.release = raid_component_release;
234 rc->cdev.dev = get_device(component_dev);
235 rc->num = rd->component_count++;
237 snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
238 "component-%d", rc->num);
239 list_add_tail(&rc->node, &rd->component_list);
240 rc->cdev.parent = cdev;
241 rc->cdev.class = &raid_class.class;
242 class_device_add(&rc->cdev);
244 EXPORT_SYMBOL(raid_component_add);
246 struct raid_template *
247 raid_class_attach(struct raid_function_template *ft)
249 struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
258 i->r.raid_attrs.ac.class = &raid_class.class;
259 i->r.raid_attrs.ac.match = raid_match;
260 i->r.raid_attrs.ac.attrs = &i->attrs[0];
262 attribute_container_register(&i->r.raid_attrs.ac);
264 i->attrs[count++] = &class_device_attr_level;
265 i->attrs[count++] = &class_device_attr_resync;
266 i->attrs[count++] = &class_device_attr_state;
268 i->attrs[count] = NULL;
269 BUG_ON(count > RAID_NUM_ATTRS);
273 EXPORT_SYMBOL(raid_class_attach);
276 raid_class_release(struct raid_template *r)
278 struct raid_internal *i = to_raid_internal(r);
280 attribute_container_unregister(&i->r.raid_attrs.ac);
284 EXPORT_SYMBOL(raid_class_release);
286 static __init int raid_init(void)
288 return transport_class_register(&raid_class);
291 static __exit void raid_exit(void)
293 transport_class_unregister(&raid_class);
296 MODULE_AUTHOR("James Bottomley");
297 MODULE_DESCRIPTION("RAID device class");
298 MODULE_LICENSE("GPL");
300 module_init(raid_init);
301 module_exit(raid_exit);