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