blob: a9f99c68556f0cdeff85d1d6bab5e0999028591d [file] [log] [blame]
James Bottomley61a7afa2005-08-16 18:27:34 -05001/*
James Bottomleyb1081ea2005-11-06 11:59:08 -06002 * 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.
James Bottomley61a7afa2005-08-16 18:27:34 -050011 */
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
15#include <linux/raid_class.h>
16#include <scsi/scsi_device.h>
17#include <scsi/scsi_host.h>
18
19#define RAID_NUM_ATTRS 3
20
21struct raid_internal {
22 struct raid_template r;
23 struct raid_function_template *f;
24 /* The actual attributes */
25 struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
26 /* The array of null terminated pointers to attributes
27 * needed by scsi_sysfs.c */
28 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
29};
30
31struct raid_component {
32 struct list_head node;
James Bottomleyb1081ea2005-11-06 11:59:08 -060033 struct class_device cdev;
James Bottomley61a7afa2005-08-16 18:27:34 -050034 int num;
35};
36
37#define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
38
39#define tc_to_raid_internal(tcont) ({ \
40 struct raid_template *r = \
41 container_of(tcont, struct raid_template, raid_attrs); \
42 to_raid_internal(r); \
43})
44
45#define ac_to_raid_internal(acont) ({ \
46 struct transport_container *tc = \
47 container_of(acont, struct transport_container, ac); \
48 tc_to_raid_internal(tc); \
49})
50
51#define class_device_to_raid_internal(cdev) ({ \
52 struct attribute_container *ac = \
53 attribute_container_classdev_to_container(cdev); \
54 ac_to_raid_internal(ac); \
55})
56
57
58static int raid_match(struct attribute_container *cont, struct device *dev)
59{
60 /* We have to look for every subsystem that could house
61 * emulated RAID devices, so start with SCSI */
62 struct raid_internal *i = ac_to_raid_internal(cont);
63
64 if (scsi_is_sdev_device(dev)) {
65 struct scsi_device *sdev = to_scsi_device(dev);
66
67 if (i->f->cookie != sdev->host->hostt)
68 return 0;
69
70 return i->f->is_raid(dev);
71 }
72 /* FIXME: look at other subsystems too */
73 return 0;
74}
75
76static int raid_setup(struct transport_container *tc, struct device *dev,
77 struct class_device *cdev)
78{
79 struct raid_data *rd;
80
81 BUG_ON(class_get_devdata(cdev));
82
James Bottomleyb1081ea2005-11-06 11:59:08 -060083 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
James Bottomley61a7afa2005-08-16 18:27:34 -050084 if (!rd)
85 return -ENOMEM;
86
James Bottomley61a7afa2005-08-16 18:27:34 -050087 INIT_LIST_HEAD(&rd->component_list);
88 class_set_devdata(cdev, rd);
89
90 return 0;
91}
92
93static int raid_remove(struct transport_container *tc, struct device *dev,
94 struct class_device *cdev)
95{
96 struct raid_data *rd = class_get_devdata(cdev);
97 struct raid_component *rc, *next;
James Bottomleyb1081ea2005-11-06 11:59:08 -060098 dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
James Bottomley61a7afa2005-08-16 18:27:34 -050099 class_set_devdata(cdev, NULL);
100 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
James Bottomley61a7afa2005-08-16 18:27:34 -0500101 list_del(&rc->node);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600102 dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
103 class_device_unregister(&rc->cdev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500104 }
James Bottomleyb1081ea2005-11-06 11:59:08 -0600105 dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
106 kfree(rd);
James Bottomley61a7afa2005-08-16 18:27:34 -0500107 return 0;
108}
109
110static DECLARE_TRANSPORT_CLASS(raid_class,
111 "raid_devices",
112 raid_setup,
113 raid_remove,
114 NULL);
115
116static struct {
117 enum raid_state value;
118 char *name;
119} raid_states[] = {
James Bottomleyb1081ea2005-11-06 11:59:08 -0600120 { RAID_STATE_UNKNOWN, "unknown" },
121 { RAID_STATE_ACTIVE, "active" },
122 { RAID_STATE_DEGRADED, "degraded" },
123 { RAID_STATE_RESYNCING, "resyncing" },
124 { RAID_STATE_OFFLINE, "offline" },
James Bottomley61a7afa2005-08-16 18:27:34 -0500125};
126
127static const char *raid_state_name(enum raid_state state)
128{
129 int i;
130 char *name = NULL;
131
132 for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
133 if (raid_states[i].value == state) {
134 name = raid_states[i].name;
135 break;
136 }
137 }
138 return name;
139}
140
James Bottomleyb1081ea2005-11-06 11:59:08 -0600141static struct {
142 enum raid_level value;
143 char *name;
144} raid_levels[] = {
145 { RAID_LEVEL_UNKNOWN, "unknown" },
146 { RAID_LEVEL_LINEAR, "linear" },
147 { RAID_LEVEL_0, "raid0" },
148 { RAID_LEVEL_1, "raid1" },
149 { RAID_LEVEL_3, "raid3" },
150 { RAID_LEVEL_4, "raid4" },
151 { RAID_LEVEL_5, "raid5" },
152 { RAID_LEVEL_6, "raid6" },
153};
154
155static const char *raid_level_name(enum raid_level level)
156{
157 int i;
158 char *name = NULL;
159
160 for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) {
161 if (raid_levels[i].value == level) {
162 name = raid_levels[i].name;
163 break;
164 }
165 }
166 return name;
167}
James Bottomley61a7afa2005-08-16 18:27:34 -0500168
169#define raid_attr_show_internal(attr, fmt, var, code) \
170static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
171{ \
172 struct raid_data *rd = class_get_devdata(cdev); \
173 code \
174 return snprintf(buf, 20, #fmt "\n", var); \
175}
176
177#define raid_attr_ro_states(attr, states, code) \
178raid_attr_show_internal(attr, %s, name, \
179 const char *name; \
180 code \
181 name = raid_##states##_name(rd->attr); \
182) \
183static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
184
185
186#define raid_attr_ro_internal(attr, code) \
187raid_attr_show_internal(attr, %d, rd->attr, code) \
188static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
189
190#define ATTR_CODE(attr) \
191 struct raid_internal *i = class_device_to_raid_internal(cdev); \
192 if (i->f->get_##attr) \
193 i->f->get_##attr(cdev->dev);
194
195#define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
196#define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
James Bottomleyb1081ea2005-11-06 11:59:08 -0600197#define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
198#define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
James Bottomley61a7afa2005-08-16 18:27:34 -0500199
James Bottomleyb1081ea2005-11-06 11:59:08 -0600200
201raid_attr_ro_state(level);
James Bottomley61a7afa2005-08-16 18:27:34 -0500202raid_attr_ro_fn(resync);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600203raid_attr_ro_state_fn(state);
204
205static void raid_component_release(struct class_device *cdev)
206{
207 struct raid_component *rc = container_of(cdev, struct raid_component,
208 cdev);
209 dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
210 put_device(rc->cdev.dev);
211 kfree(rc);
212}
James Bottomley61a7afa2005-08-16 18:27:34 -0500213
214void raid_component_add(struct raid_template *r,struct device *raid_dev,
215 struct device *component_dev)
216{
217 struct class_device *cdev =
218 attribute_container_find_class_device(&r->raid_attrs.ac,
219 raid_dev);
220 struct raid_component *rc;
221 struct raid_data *rd = class_get_devdata(cdev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500222
James Bottomleyb1081ea2005-11-06 11:59:08 -0600223 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
James Bottomley61a7afa2005-08-16 18:27:34 -0500224 if (!rc)
225 return;
226
227 INIT_LIST_HEAD(&rc->node);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600228 class_device_initialize(&rc->cdev);
229 rc->cdev.release = raid_component_release;
230 rc->cdev.dev = get_device(component_dev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500231 rc->num = rd->component_count++;
232
James Bottomleyb1081ea2005-11-06 11:59:08 -0600233 snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
234 "component-%d", rc->num);
James Bottomley61a7afa2005-08-16 18:27:34 -0500235 list_add_tail(&rc->node, &rd->component_list);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600236 rc->cdev.parent = cdev;
237 rc->cdev.class = &raid_class.class;
238 class_device_add(&rc->cdev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500239}
240EXPORT_SYMBOL(raid_component_add);
241
242struct raid_template *
243raid_class_attach(struct raid_function_template *ft)
244{
James Bottomleyb1081ea2005-11-06 11:59:08 -0600245 struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
James Bottomley61a7afa2005-08-16 18:27:34 -0500246 GFP_KERNEL);
247 int count = 0;
248
249 if (unlikely(!i))
250 return NULL;
251
James Bottomley61a7afa2005-08-16 18:27:34 -0500252 i->f = ft;
253
254 i->r.raid_attrs.ac.class = &raid_class.class;
255 i->r.raid_attrs.ac.match = raid_match;
256 i->r.raid_attrs.ac.attrs = &i->attrs[0];
257
258 attribute_container_register(&i->r.raid_attrs.ac);
259
260 i->attrs[count++] = &class_device_attr_level;
261 i->attrs[count++] = &class_device_attr_resync;
262 i->attrs[count++] = &class_device_attr_state;
263
264 i->attrs[count] = NULL;
265 BUG_ON(count > RAID_NUM_ATTRS);
266
267 return &i->r;
268}
269EXPORT_SYMBOL(raid_class_attach);
270
271void
272raid_class_release(struct raid_template *r)
273{
274 struct raid_internal *i = to_raid_internal(r);
275
276 attribute_container_unregister(&i->r.raid_attrs.ac);
277
278 kfree(i);
279}
280EXPORT_SYMBOL(raid_class_release);
281
282static __init int raid_init(void)
283{
284 return transport_class_register(&raid_class);
285}
286
287static __exit void raid_exit(void)
288{
289 transport_class_unregister(&raid_class);
290}
291
292MODULE_AUTHOR("James Bottomley");
293MODULE_DESCRIPTION("RAID device class");
294MODULE_LICENSE("GPL");
295
296module_init(raid_init);
297module_exit(raid_exit);
298