Commit | Line | Data |
---|---|---|
61a7afa2 | 1 | /* |
b1081ea6 JB |
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. | |
61a7afa2 JB |
11 | */ |
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/list.h> | |
8c65b4a6 TS |
15 | #include <linux/slab.h> |
16 | #include <linux/string.h> | |
61a7afa2 JB |
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 */ | |
ee959b00 | 27 | struct device_attribute private_attrs[RAID_NUM_ATTRS]; |
61a7afa2 JB |
28 | /* The array of null terminated pointers to attributes |
29 | * needed by scsi_sysfs.c */ | |
ee959b00 | 30 | struct device_attribute *attrs[RAID_NUM_ATTRS + 1]; |
61a7afa2 JB |
31 | }; |
32 | ||
33 | struct raid_component { | |
34 | struct list_head node; | |
ee959b00 | 35 | struct device dev; |
61a7afa2 JB |
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 | ||
ee959b00 | 53 | #define device_to_raid_internal(dev) ({ \ |
61a7afa2 | 54 | struct attribute_container *ac = \ |
ee959b00 | 55 | attribute_container_classdev_to_container(dev); \ |
61a7afa2 JB |
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 | ||
fac829fd | 66 | #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE) |
61a7afa2 JB |
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 | } | |
fac829fd | 75 | #endif |
61a7afa2 JB |
76 | /* FIXME: look at other subsystems too */ |
77 | return 0; | |
78 | } | |
79 | ||
80 | static int raid_setup(struct transport_container *tc, struct device *dev, | |
ee959b00 | 81 | struct device *cdev) |
61a7afa2 JB |
82 | { |
83 | struct raid_data *rd; | |
84 | ||
ee959b00 | 85 | BUG_ON(dev_get_drvdata(cdev)); |
61a7afa2 | 86 | |
b1081ea6 | 87 | rd = kzalloc(sizeof(*rd), GFP_KERNEL); |
61a7afa2 JB |
88 | if (!rd) |
89 | return -ENOMEM; | |
90 | ||
61a7afa2 | 91 | INIT_LIST_HEAD(&rd->component_list); |
ee959b00 | 92 | dev_set_drvdata(cdev, rd); |
61a7afa2 JB |
93 | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int raid_remove(struct transport_container *tc, struct device *dev, | |
ee959b00 | 98 | struct device *cdev) |
61a7afa2 | 99 | { |
ee959b00 | 100 | struct raid_data *rd = dev_get_drvdata(cdev); |
61a7afa2 | 101 | struct raid_component *rc, *next; |
b1081ea6 | 102 | dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); |
ee959b00 | 103 | dev_set_drvdata(cdev, NULL); |
61a7afa2 | 104 | list_for_each_entry_safe(rc, next, &rd->component_list, node) { |
61a7afa2 | 105 | list_del(&rc->node); |
ee959b00 TJ |
106 | dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n"); |
107 | device_unregister(&rc->dev); | |
61a7afa2 | 108 | } |
b1081ea6 JB |
109 | dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); |
110 | kfree(rd); | |
61a7afa2 JB |
111 | return 0; |
112 | } | |
113 | ||
114 | static DECLARE_TRANSPORT_CLASS(raid_class, | |
115 | "raid_devices", | |
116 | raid_setup, | |
117 | raid_remove, | |
118 | NULL); | |
119 | ||
0ad78200 | 120 | static const struct { |
61a7afa2 JB |
121 | enum raid_state value; |
122 | char *name; | |
123 | } raid_states[] = { | |
b1081ea6 JB |
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" }, | |
61a7afa2 JB |
129 | }; |
130 | ||
131 | static const char *raid_state_name(enum raid_state state) | |
132 | { | |
133 | int i; | |
134 | char *name = NULL; | |
135 | ||
6391a113 | 136 | for (i = 0; i < ARRAY_SIZE(raid_states); i++) { |
61a7afa2 JB |
137 | if (raid_states[i].value == state) { |
138 | name = raid_states[i].name; | |
139 | break; | |
140 | } | |
141 | } | |
142 | return name; | |
143 | } | |
144 | ||
b1081ea6 JB |
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" }, | |
8e32ca49 | 153 | { RAID_LEVEL_10, "raid10" }, |
8e4a0cf7 | 154 | { RAID_LEVEL_1E, "raid1e" }, |
b1081ea6 JB |
155 | { RAID_LEVEL_3, "raid3" }, |
156 | { RAID_LEVEL_4, "raid4" }, | |
157 | { RAID_LEVEL_5, "raid5" }, | |
8e32ca49 | 158 | { RAID_LEVEL_50, "raid50" }, |
b1081ea6 | 159 | { RAID_LEVEL_6, "raid6" }, |
34e81f7a | 160 | { RAID_LEVEL_JBOD, "jbod" }, |
b1081ea6 JB |
161 | }; |
162 | ||
163 | static const char *raid_level_name(enum raid_level level) | |
164 | { | |
165 | int i; | |
166 | char *name = NULL; | |
167 | ||
6391a113 | 168 | for (i = 0; i < ARRAY_SIZE(raid_levels); i++) { |
b1081ea6 JB |
169 | if (raid_levels[i].value == level) { |
170 | name = raid_levels[i].name; | |
171 | break; | |
172 | } | |
173 | } | |
174 | return name; | |
175 | } | |
61a7afa2 JB |
176 | |
177 | #define raid_attr_show_internal(attr, fmt, var, code) \ | |
ee959b00 TJ |
178 | static ssize_t raid_show_##attr(struct device *dev, \ |
179 | struct device_attribute *attr, \ | |
180 | char *buf) \ | |
61a7afa2 | 181 | { \ |
ee959b00 | 182 | struct raid_data *rd = dev_get_drvdata(dev); \ |
61a7afa2 JB |
183 | code \ |
184 | return snprintf(buf, 20, #fmt "\n", var); \ | |
185 | } | |
186 | ||
187 | #define raid_attr_ro_states(attr, states, code) \ | |
188 | raid_attr_show_internal(attr, %s, name, \ | |
189 | const char *name; \ | |
190 | code \ | |
191 | name = raid_##states##_name(rd->attr); \ | |
192 | ) \ | |
ee959b00 | 193 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
61a7afa2 JB |
194 | |
195 | ||
196 | #define raid_attr_ro_internal(attr, code) \ | |
197 | raid_attr_show_internal(attr, %d, rd->attr, code) \ | |
ee959b00 | 198 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
61a7afa2 JB |
199 | |
200 | #define ATTR_CODE(attr) \ | |
ee959b00 | 201 | struct raid_internal *i = device_to_raid_internal(dev); \ |
61a7afa2 | 202 | if (i->f->get_##attr) \ |
ee959b00 | 203 | i->f->get_##attr(dev->parent); |
61a7afa2 JB |
204 | |
205 | #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) | |
206 | #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) | |
b1081ea6 JB |
207 | #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) |
208 | #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) | |
209 | ||
61a7afa2 | 210 | |
b1081ea6 | 211 | raid_attr_ro_state(level); |
61a7afa2 | 212 | raid_attr_ro_fn(resync); |
b1081ea6 JB |
213 | raid_attr_ro_state_fn(state); |
214 | ||
ee959b00 | 215 | static void raid_component_release(struct device *dev) |
b1081ea6 | 216 | { |
ee959b00 TJ |
217 | struct raid_component *rc = |
218 | container_of(dev, struct raid_component, dev); | |
219 | dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n"); | |
220 | put_device(rc->dev.parent); | |
b1081ea6 JB |
221 | kfree(rc); |
222 | } | |
61a7afa2 | 223 | |
ed542bed JG |
224 | int raid_component_add(struct raid_template *r,struct device *raid_dev, |
225 | struct device *component_dev) | |
61a7afa2 | 226 | { |
ee959b00 | 227 | struct device *cdev = |
61a7afa2 JB |
228 | attribute_container_find_class_device(&r->raid_attrs.ac, |
229 | raid_dev); | |
230 | struct raid_component *rc; | |
ee959b00 | 231 | struct raid_data *rd = dev_get_drvdata(cdev); |
ed542bed | 232 | int err; |
61a7afa2 | 233 | |
b1081ea6 | 234 | rc = kzalloc(sizeof(*rc), GFP_KERNEL); |
61a7afa2 | 235 | if (!rc) |
ed542bed | 236 | return -ENOMEM; |
61a7afa2 JB |
237 | |
238 | INIT_LIST_HEAD(&rc->node); | |
ee959b00 TJ |
239 | device_initialize(&rc->dev); |
240 | rc->dev.release = raid_component_release; | |
241 | rc->dev.parent = get_device(component_dev); | |
61a7afa2 JB |
242 | rc->num = rd->component_count++; |
243 | ||
71610f55 | 244 | dev_set_name(&rc->dev, "component-%d", rc->num); |
61a7afa2 | 245 | list_add_tail(&rc->node, &rd->component_list); |
ee959b00 TJ |
246 | rc->dev.class = &raid_class.class; |
247 | err = device_add(&rc->dev); | |
ed542bed JG |
248 | if (err) |
249 | goto err_out; | |
250 | ||
251 | return 0; | |
252 | ||
253 | err_out: | |
254 | list_del(&rc->node); | |
255 | rd->component_count--; | |
256 | put_device(component_dev); | |
257 | kfree(rc); | |
258 | return err; | |
61a7afa2 JB |
259 | } |
260 | EXPORT_SYMBOL(raid_component_add); | |
261 | ||
262 | struct raid_template * | |
263 | raid_class_attach(struct raid_function_template *ft) | |
264 | { | |
b1081ea6 | 265 | struct raid_internal *i = kzalloc(sizeof(struct raid_internal), |
61a7afa2 JB |
266 | GFP_KERNEL); |
267 | int count = 0; | |
268 | ||
269 | if (unlikely(!i)) | |
270 | return NULL; | |
271 | ||
61a7afa2 JB |
272 | i->f = ft; |
273 | ||
274 | i->r.raid_attrs.ac.class = &raid_class.class; | |
275 | i->r.raid_attrs.ac.match = raid_match; | |
276 | i->r.raid_attrs.ac.attrs = &i->attrs[0]; | |
277 | ||
278 | attribute_container_register(&i->r.raid_attrs.ac); | |
279 | ||
ee959b00 TJ |
280 | i->attrs[count++] = &dev_attr_level; |
281 | i->attrs[count++] = &dev_attr_resync; | |
282 | i->attrs[count++] = &dev_attr_state; | |
61a7afa2 JB |
283 | |
284 | i->attrs[count] = NULL; | |
285 | BUG_ON(count > RAID_NUM_ATTRS); | |
286 | ||
287 | return &i->r; | |
288 | } | |
289 | EXPORT_SYMBOL(raid_class_attach); | |
290 | ||
291 | void | |
292 | raid_class_release(struct raid_template *r) | |
293 | { | |
294 | struct raid_internal *i = to_raid_internal(r); | |
295 | ||
2f3edc69 | 296 | BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac)); |
61a7afa2 JB |
297 | |
298 | kfree(i); | |
299 | } | |
300 | EXPORT_SYMBOL(raid_class_release); | |
301 | ||
302 | static __init int raid_init(void) | |
303 | { | |
304 | return transport_class_register(&raid_class); | |
305 | } | |
306 | ||
307 | static __exit void raid_exit(void) | |
308 | { | |
309 | transport_class_unregister(&raid_class); | |
310 | } | |
311 | ||
312 | MODULE_AUTHOR("James Bottomley"); | |
313 | MODULE_DESCRIPTION("RAID device class"); | |
314 | MODULE_LICENSE("GPL"); | |
315 | ||
316 | module_init(raid_init); | |
317 | module_exit(raid_exit); | |
318 |