Commit | Line | Data |
---|---|---|
82664963 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
61a7afa2 | 2 | /* |
b1081ea6 JB |
3 | * raid_class.c - implementation of a simple raid visualisation class |
4 | * | |
5 | * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com> | |
6 | * | |
b1081ea6 JB |
7 | * This class is designed to allow raid attributes to be visualised and |
8 | * manipulated in a form independent of the underlying raid. Ultimately this | |
9 | * should work for both hardware and software raids. | |
61a7afa2 JB |
10 | */ |
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/list.h> | |
8c65b4a6 TS |
14 | #include <linux/slab.h> |
15 | #include <linux/string.h> | |
61a7afa2 JB |
16 | #include <linux/raid_class.h> |
17 | #include <scsi/scsi_device.h> | |
18 | #include <scsi/scsi_host.h> | |
19 | ||
20 | #define RAID_NUM_ATTRS 3 | |
21 | ||
22 | struct raid_internal { | |
23 | struct raid_template r; | |
24 | struct raid_function_template *f; | |
25 | /* The actual attributes */ | |
ee959b00 | 26 | struct device_attribute private_attrs[RAID_NUM_ATTRS]; |
61a7afa2 JB |
27 | /* The array of null terminated pointers to attributes |
28 | * needed by scsi_sysfs.c */ | |
ee959b00 | 29 | struct device_attribute *attrs[RAID_NUM_ATTRS + 1]; |
61a7afa2 JB |
30 | }; |
31 | ||
32 | struct raid_component { | |
33 | struct list_head node; | |
ee959b00 | 34 | struct device dev; |
61a7afa2 JB |
35 | int num; |
36 | }; | |
37 | ||
38 | #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r) | |
39 | ||
40 | #define tc_to_raid_internal(tcont) ({ \ | |
41 | struct raid_template *r = \ | |
42 | container_of(tcont, struct raid_template, raid_attrs); \ | |
43 | to_raid_internal(r); \ | |
44 | }) | |
45 | ||
46 | #define ac_to_raid_internal(acont) ({ \ | |
47 | struct transport_container *tc = \ | |
48 | container_of(acont, struct transport_container, ac); \ | |
49 | tc_to_raid_internal(tc); \ | |
50 | }) | |
51 | ||
ee959b00 | 52 | #define device_to_raid_internal(dev) ({ \ |
61a7afa2 | 53 | struct attribute_container *ac = \ |
ee959b00 | 54 | attribute_container_classdev_to_container(dev); \ |
61a7afa2 JB |
55 | ac_to_raid_internal(ac); \ |
56 | }) | |
57 | ||
58 | ||
59 | static int raid_match(struct attribute_container *cont, struct device *dev) | |
60 | { | |
61 | /* We have to look for every subsystem that could house | |
62 | * emulated RAID devices, so start with SCSI */ | |
63 | struct raid_internal *i = ac_to_raid_internal(cont); | |
64 | ||
0eeec014 | 65 | if (IS_ENABLED(CONFIG_SCSI) && scsi_is_sdev_device(dev)) { |
61a7afa2 JB |
66 | struct scsi_device *sdev = to_scsi_device(dev); |
67 | ||
68 | if (i->f->cookie != sdev->host->hostt) | |
69 | return 0; | |
70 | ||
71 | return i->f->is_raid(dev); | |
72 | } | |
73 | /* FIXME: look at other subsystems too */ | |
74 | return 0; | |
75 | } | |
76 | ||
77 | static int raid_setup(struct transport_container *tc, struct device *dev, | |
ee959b00 | 78 | struct device *cdev) |
61a7afa2 JB |
79 | { |
80 | struct raid_data *rd; | |
81 | ||
ee959b00 | 82 | BUG_ON(dev_get_drvdata(cdev)); |
61a7afa2 | 83 | |
b1081ea6 | 84 | rd = kzalloc(sizeof(*rd), GFP_KERNEL); |
61a7afa2 JB |
85 | if (!rd) |
86 | return -ENOMEM; | |
87 | ||
61a7afa2 | 88 | INIT_LIST_HEAD(&rd->component_list); |
ee959b00 | 89 | dev_set_drvdata(cdev, rd); |
61a7afa2 JB |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
94 | static int raid_remove(struct transport_container *tc, struct device *dev, | |
ee959b00 | 95 | struct device *cdev) |
61a7afa2 | 96 | { |
ee959b00 | 97 | struct raid_data *rd = dev_get_drvdata(cdev); |
61a7afa2 | 98 | struct raid_component *rc, *next; |
b1081ea6 | 99 | dev_printk(KERN_ERR, dev, "RAID REMOVE\n"); |
ee959b00 | 100 | dev_set_drvdata(cdev, NULL); |
61a7afa2 | 101 | list_for_each_entry_safe(rc, next, &rd->component_list, node) { |
61a7afa2 | 102 | list_del(&rc->node); |
ee959b00 TJ |
103 | dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n"); |
104 | device_unregister(&rc->dev); | |
61a7afa2 | 105 | } |
b1081ea6 JB |
106 | dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n"); |
107 | kfree(rd); | |
61a7afa2 JB |
108 | return 0; |
109 | } | |
110 | ||
111 | static DECLARE_TRANSPORT_CLASS(raid_class, | |
112 | "raid_devices", | |
113 | raid_setup, | |
114 | raid_remove, | |
115 | NULL); | |
116 | ||
0ad78200 | 117 | static const struct { |
61a7afa2 JB |
118 | enum raid_state value; |
119 | char *name; | |
120 | } raid_states[] = { | |
b1081ea6 JB |
121 | { RAID_STATE_UNKNOWN, "unknown" }, |
122 | { RAID_STATE_ACTIVE, "active" }, | |
123 | { RAID_STATE_DEGRADED, "degraded" }, | |
124 | { RAID_STATE_RESYNCING, "resyncing" }, | |
125 | { RAID_STATE_OFFLINE, "offline" }, | |
61a7afa2 JB |
126 | }; |
127 | ||
128 | static const char *raid_state_name(enum raid_state state) | |
129 | { | |
130 | int i; | |
131 | char *name = NULL; | |
132 | ||
6391a113 | 133 | for (i = 0; i < ARRAY_SIZE(raid_states); i++) { |
61a7afa2 JB |
134 | if (raid_states[i].value == state) { |
135 | name = raid_states[i].name; | |
136 | break; | |
137 | } | |
138 | } | |
139 | return name; | |
140 | } | |
141 | ||
b1081ea6 JB |
142 | static struct { |
143 | enum raid_level value; | |
144 | char *name; | |
145 | } raid_levels[] = { | |
146 | { RAID_LEVEL_UNKNOWN, "unknown" }, | |
147 | { RAID_LEVEL_LINEAR, "linear" }, | |
148 | { RAID_LEVEL_0, "raid0" }, | |
149 | { RAID_LEVEL_1, "raid1" }, | |
8e32ca49 | 150 | { RAID_LEVEL_10, "raid10" }, |
8e4a0cf7 | 151 | { RAID_LEVEL_1E, "raid1e" }, |
b1081ea6 JB |
152 | { RAID_LEVEL_3, "raid3" }, |
153 | { RAID_LEVEL_4, "raid4" }, | |
154 | { RAID_LEVEL_5, "raid5" }, | |
8e32ca49 | 155 | { RAID_LEVEL_50, "raid50" }, |
b1081ea6 | 156 | { RAID_LEVEL_6, "raid6" }, |
34e81f7a | 157 | { RAID_LEVEL_JBOD, "jbod" }, |
b1081ea6 JB |
158 | }; |
159 | ||
160 | static const char *raid_level_name(enum raid_level level) | |
161 | { | |
162 | int i; | |
163 | char *name = NULL; | |
164 | ||
6391a113 | 165 | for (i = 0; i < ARRAY_SIZE(raid_levels); i++) { |
b1081ea6 JB |
166 | if (raid_levels[i].value == level) { |
167 | name = raid_levels[i].name; | |
168 | break; | |
169 | } | |
170 | } | |
171 | return name; | |
172 | } | |
61a7afa2 JB |
173 | |
174 | #define raid_attr_show_internal(attr, fmt, var, code) \ | |
ee959b00 TJ |
175 | static ssize_t raid_show_##attr(struct device *dev, \ |
176 | struct device_attribute *attr, \ | |
177 | char *buf) \ | |
61a7afa2 | 178 | { \ |
ee959b00 | 179 | struct raid_data *rd = dev_get_drvdata(dev); \ |
61a7afa2 JB |
180 | code \ |
181 | return snprintf(buf, 20, #fmt "\n", var); \ | |
182 | } | |
183 | ||
184 | #define raid_attr_ro_states(attr, states, code) \ | |
185 | raid_attr_show_internal(attr, %s, name, \ | |
186 | const char *name; \ | |
187 | code \ | |
188 | name = raid_##states##_name(rd->attr); \ | |
189 | ) \ | |
ee959b00 | 190 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
61a7afa2 JB |
191 | |
192 | ||
193 | #define raid_attr_ro_internal(attr, code) \ | |
194 | raid_attr_show_internal(attr, %d, rd->attr, code) \ | |
ee959b00 | 195 | static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL) |
61a7afa2 JB |
196 | |
197 | #define ATTR_CODE(attr) \ | |
ee959b00 | 198 | struct raid_internal *i = device_to_raid_internal(dev); \ |
61a7afa2 | 199 | if (i->f->get_##attr) \ |
ee959b00 | 200 | i->f->get_##attr(dev->parent); |
61a7afa2 JB |
201 | |
202 | #define raid_attr_ro(attr) raid_attr_ro_internal(attr, ) | |
203 | #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr)) | |
b1081ea6 JB |
204 | #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ) |
205 | #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr)) | |
206 | ||
61a7afa2 | 207 | |
b1081ea6 | 208 | raid_attr_ro_state(level); |
61a7afa2 | 209 | raid_attr_ro_fn(resync); |
b1081ea6 JB |
210 | raid_attr_ro_state_fn(state); |
211 | ||
ee959b00 | 212 | static void raid_component_release(struct device *dev) |
b1081ea6 | 213 | { |
ee959b00 TJ |
214 | struct raid_component *rc = |
215 | container_of(dev, struct raid_component, dev); | |
216 | dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n"); | |
217 | put_device(rc->dev.parent); | |
b1081ea6 JB |
218 | kfree(rc); |
219 | } | |
61a7afa2 | 220 | |
ed542bed JG |
221 | int raid_component_add(struct raid_template *r,struct device *raid_dev, |
222 | struct device *component_dev) | |
61a7afa2 | 223 | { |
ee959b00 | 224 | struct device *cdev = |
61a7afa2 JB |
225 | attribute_container_find_class_device(&r->raid_attrs.ac, |
226 | raid_dev); | |
227 | struct raid_component *rc; | |
ee959b00 | 228 | struct raid_data *rd = dev_get_drvdata(cdev); |
ed542bed | 229 | int err; |
61a7afa2 | 230 | |
b1081ea6 | 231 | rc = kzalloc(sizeof(*rc), GFP_KERNEL); |
61a7afa2 | 232 | if (!rc) |
ed542bed | 233 | return -ENOMEM; |
61a7afa2 JB |
234 | |
235 | INIT_LIST_HEAD(&rc->node); | |
ee959b00 TJ |
236 | device_initialize(&rc->dev); |
237 | rc->dev.release = raid_component_release; | |
238 | rc->dev.parent = get_device(component_dev); | |
61a7afa2 JB |
239 | rc->num = rd->component_count++; |
240 | ||
71610f55 | 241 | dev_set_name(&rc->dev, "component-%d", rc->num); |
61a7afa2 | 242 | list_add_tail(&rc->node, &rd->component_list); |
ee959b00 TJ |
243 | rc->dev.class = &raid_class.class; |
244 | err = device_add(&rc->dev); | |
ed542bed JG |
245 | if (err) |
246 | goto err_out; | |
247 | ||
248 | return 0; | |
249 | ||
250 | err_out: | |
251 | list_del(&rc->node); | |
252 | rd->component_count--; | |
253 | put_device(component_dev); | |
254 | kfree(rc); | |
255 | return err; | |
61a7afa2 JB |
256 | } |
257 | EXPORT_SYMBOL(raid_component_add); | |
258 | ||
259 | struct raid_template * | |
260 | raid_class_attach(struct raid_function_template *ft) | |
261 | { | |
b1081ea6 | 262 | struct raid_internal *i = kzalloc(sizeof(struct raid_internal), |
61a7afa2 JB |
263 | GFP_KERNEL); |
264 | int count = 0; | |
265 | ||
266 | if (unlikely(!i)) | |
267 | return NULL; | |
268 | ||
61a7afa2 JB |
269 | i->f = ft; |
270 | ||
271 | i->r.raid_attrs.ac.class = &raid_class.class; | |
272 | i->r.raid_attrs.ac.match = raid_match; | |
273 | i->r.raid_attrs.ac.attrs = &i->attrs[0]; | |
274 | ||
275 | attribute_container_register(&i->r.raid_attrs.ac); | |
276 | ||
ee959b00 TJ |
277 | i->attrs[count++] = &dev_attr_level; |
278 | i->attrs[count++] = &dev_attr_resync; | |
279 | i->attrs[count++] = &dev_attr_state; | |
61a7afa2 JB |
280 | |
281 | i->attrs[count] = NULL; | |
282 | BUG_ON(count > RAID_NUM_ATTRS); | |
283 | ||
284 | return &i->r; | |
285 | } | |
286 | EXPORT_SYMBOL(raid_class_attach); | |
287 | ||
288 | void | |
289 | raid_class_release(struct raid_template *r) | |
290 | { | |
291 | struct raid_internal *i = to_raid_internal(r); | |
292 | ||
2f3edc69 | 293 | BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac)); |
61a7afa2 JB |
294 | |
295 | kfree(i); | |
296 | } | |
297 | EXPORT_SYMBOL(raid_class_release); | |
298 | ||
299 | static __init int raid_init(void) | |
300 | { | |
301 | return transport_class_register(&raid_class); | |
302 | } | |
303 | ||
304 | static __exit void raid_exit(void) | |
305 | { | |
306 | transport_class_unregister(&raid_class); | |
307 | } | |
308 | ||
309 | MODULE_AUTHOR("James Bottomley"); | |
310 | MODULE_DESCRIPTION("RAID device class"); | |
311 | MODULE_LICENSE("GPL"); | |
312 | ||
313 | module_init(raid_init); | |
314 | module_exit(raid_exit); | |
315 |