blob: bd88349b852684d3d31f87d614f2c50947b86fe3 [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>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080015#include <linux/slab.h>
16#include <linux/string.h>
James Bottomley61a7afa2005-08-16 18:27:34 -050017#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
23struct raid_internal {
24 struct raid_template r;
25 struct raid_function_template *f;
26 /* The actual attributes */
Tony Jonesee959b02008-02-22 00:13:36 +010027 struct device_attribute private_attrs[RAID_NUM_ATTRS];
James Bottomley61a7afa2005-08-16 18:27:34 -050028 /* The array of null terminated pointers to attributes
29 * needed by scsi_sysfs.c */
Tony Jonesee959b02008-02-22 00:13:36 +010030 struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
James Bottomley61a7afa2005-08-16 18:27:34 -050031};
32
33struct raid_component {
34 struct list_head node;
Tony Jonesee959b02008-02-22 00:13:36 +010035 struct device dev;
James Bottomley61a7afa2005-08-16 18:27:34 -050036 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
Tony Jonesee959b02008-02-22 00:13:36 +010053#define device_to_raid_internal(dev) ({ \
James Bottomley61a7afa2005-08-16 18:27:34 -050054 struct attribute_container *ac = \
Tony Jonesee959b02008-02-22 00:13:36 +010055 attribute_container_classdev_to_container(dev); \
James Bottomley61a7afa2005-08-16 18:27:34 -050056 ac_to_raid_internal(ac); \
57})
58
59
60static 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
78static int raid_setup(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010079 struct device *cdev)
James Bottomley61a7afa2005-08-16 18:27:34 -050080{
81 struct raid_data *rd;
82
Tony Jonesee959b02008-02-22 00:13:36 +010083 BUG_ON(dev_get_drvdata(cdev));
James Bottomley61a7afa2005-08-16 18:27:34 -050084
James Bottomleyb1081ea2005-11-06 11:59:08 -060085 rd = kzalloc(sizeof(*rd), GFP_KERNEL);
James Bottomley61a7afa2005-08-16 18:27:34 -050086 if (!rd)
87 return -ENOMEM;
88
James Bottomley61a7afa2005-08-16 18:27:34 -050089 INIT_LIST_HEAD(&rd->component_list);
Tony Jonesee959b02008-02-22 00:13:36 +010090 dev_set_drvdata(cdev, rd);
James Bottomley61a7afa2005-08-16 18:27:34 -050091
92 return 0;
93}
94
95static int raid_remove(struct transport_container *tc, struct device *dev,
Tony Jonesee959b02008-02-22 00:13:36 +010096 struct device *cdev)
James Bottomley61a7afa2005-08-16 18:27:34 -050097{
Tony Jonesee959b02008-02-22 00:13:36 +010098 struct raid_data *rd = dev_get_drvdata(cdev);
James Bottomley61a7afa2005-08-16 18:27:34 -050099 struct raid_component *rc, *next;
James Bottomleyb1081ea2005-11-06 11:59:08 -0600100 dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
Tony Jonesee959b02008-02-22 00:13:36 +0100101 dev_set_drvdata(cdev, NULL);
James Bottomley61a7afa2005-08-16 18:27:34 -0500102 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
James Bottomley61a7afa2005-08-16 18:27:34 -0500103 list_del(&rc->node);
Tony Jonesee959b02008-02-22 00:13:36 +0100104 dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
105 device_unregister(&rc->dev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500106 }
James Bottomleyb1081ea2005-11-06 11:59:08 -0600107 dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
108 kfree(rd);
James Bottomley61a7afa2005-08-16 18:27:34 -0500109 return 0;
110}
111
112static DECLARE_TRANSPORT_CLASS(raid_class,
113 "raid_devices",
114 raid_setup,
115 raid_remove,
116 NULL);
117
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100118static const struct {
James Bottomley61a7afa2005-08-16 18:27:34 -0500119 enum raid_state value;
120 char *name;
121} raid_states[] = {
James Bottomleyb1081ea2005-11-06 11:59:08 -0600122 { RAID_STATE_UNKNOWN, "unknown" },
123 { RAID_STATE_ACTIVE, "active" },
124 { RAID_STATE_DEGRADED, "degraded" },
125 { RAID_STATE_RESYNCING, "resyncing" },
126 { RAID_STATE_OFFLINE, "offline" },
James Bottomley61a7afa2005-08-16 18:27:34 -0500127};
128
129static const char *raid_state_name(enum raid_state state)
130{
131 int i;
132 char *name = NULL;
133
Tobias Klauser6391a112006-06-08 22:23:48 -0700134 for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
James Bottomley61a7afa2005-08-16 18:27:34 -0500135 if (raid_states[i].value == state) {
136 name = raid_states[i].name;
137 break;
138 }
139 }
140 return name;
141}
142
James Bottomleyb1081ea2005-11-06 11:59:08 -0600143static 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" },
Moore, Eric8e32ca42006-01-04 14:58:43 -0700151 { RAID_LEVEL_10, "raid10" },
Kashyap, Desai8e4a0cf2010-02-17 16:13:04 +0530152 { RAID_LEVEL_1E, "raid1e" },
James Bottomleyb1081ea2005-11-06 11:59:08 -0600153 { RAID_LEVEL_3, "raid3" },
154 { RAID_LEVEL_4, "raid4" },
155 { RAID_LEVEL_5, "raid5" },
Moore, Eric8e32ca42006-01-04 14:58:43 -0700156 { RAID_LEVEL_50, "raid50" },
James Bottomleyb1081ea2005-11-06 11:59:08 -0600157 { RAID_LEVEL_6, "raid6" },
158};
159
160static const char *raid_level_name(enum raid_level level)
161{
162 int i;
163 char *name = NULL;
164
Tobias Klauser6391a112006-06-08 22:23:48 -0700165 for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
James Bottomleyb1081ea2005-11-06 11:59:08 -0600166 if (raid_levels[i].value == level) {
167 name = raid_levels[i].name;
168 break;
169 }
170 }
171 return name;
172}
James Bottomley61a7afa2005-08-16 18:27:34 -0500173
174#define raid_attr_show_internal(attr, fmt, var, code) \
Tony Jonesee959b02008-02-22 00:13:36 +0100175static ssize_t raid_show_##attr(struct device *dev, \
176 struct device_attribute *attr, \
177 char *buf) \
James Bottomley61a7afa2005-08-16 18:27:34 -0500178{ \
Tony Jonesee959b02008-02-22 00:13:36 +0100179 struct raid_data *rd = dev_get_drvdata(dev); \
James Bottomley61a7afa2005-08-16 18:27:34 -0500180 code \
181 return snprintf(buf, 20, #fmt "\n", var); \
182}
183
184#define raid_attr_ro_states(attr, states, code) \
185raid_attr_show_internal(attr, %s, name, \
186 const char *name; \
187 code \
188 name = raid_##states##_name(rd->attr); \
189) \
Tony Jonesee959b02008-02-22 00:13:36 +0100190static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
James Bottomley61a7afa2005-08-16 18:27:34 -0500191
192
193#define raid_attr_ro_internal(attr, code) \
194raid_attr_show_internal(attr, %d, rd->attr, code) \
Tony Jonesee959b02008-02-22 00:13:36 +0100195static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
James Bottomley61a7afa2005-08-16 18:27:34 -0500196
197#define ATTR_CODE(attr) \
Tony Jonesee959b02008-02-22 00:13:36 +0100198 struct raid_internal *i = device_to_raid_internal(dev); \
James Bottomley61a7afa2005-08-16 18:27:34 -0500199 if (i->f->get_##attr) \
Tony Jonesee959b02008-02-22 00:13:36 +0100200 i->f->get_##attr(dev->parent);
James Bottomley61a7afa2005-08-16 18:27:34 -0500201
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))
James Bottomleyb1081ea2005-11-06 11:59:08 -0600204#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))
James Bottomley61a7afa2005-08-16 18:27:34 -0500206
James Bottomleyb1081ea2005-11-06 11:59:08 -0600207
208raid_attr_ro_state(level);
James Bottomley61a7afa2005-08-16 18:27:34 -0500209raid_attr_ro_fn(resync);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600210raid_attr_ro_state_fn(state);
211
Tony Jonesee959b02008-02-22 00:13:36 +0100212static void raid_component_release(struct device *dev)
James Bottomleyb1081ea2005-11-06 11:59:08 -0600213{
Tony Jonesee959b02008-02-22 00:13:36 +0100214 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);
James Bottomleyb1081ea2005-11-06 11:59:08 -0600218 kfree(rc);
219}
James Bottomley61a7afa2005-08-16 18:27:34 -0500220
Jeff Garziked542be2006-10-04 07:05:11 -0400221int raid_component_add(struct raid_template *r,struct device *raid_dev,
222 struct device *component_dev)
James Bottomley61a7afa2005-08-16 18:27:34 -0500223{
Tony Jonesee959b02008-02-22 00:13:36 +0100224 struct device *cdev =
James Bottomley61a7afa2005-08-16 18:27:34 -0500225 attribute_container_find_class_device(&r->raid_attrs.ac,
226 raid_dev);
227 struct raid_component *rc;
Tony Jonesee959b02008-02-22 00:13:36 +0100228 struct raid_data *rd = dev_get_drvdata(cdev);
Jeff Garziked542be2006-10-04 07:05:11 -0400229 int err;
James Bottomley61a7afa2005-08-16 18:27:34 -0500230
James Bottomleyb1081ea2005-11-06 11:59:08 -0600231 rc = kzalloc(sizeof(*rc), GFP_KERNEL);
James Bottomley61a7afa2005-08-16 18:27:34 -0500232 if (!rc)
Jeff Garziked542be2006-10-04 07:05:11 -0400233 return -ENOMEM;
James Bottomley61a7afa2005-08-16 18:27:34 -0500234
235 INIT_LIST_HEAD(&rc->node);
Tony Jonesee959b02008-02-22 00:13:36 +0100236 device_initialize(&rc->dev);
237 rc->dev.release = raid_component_release;
238 rc->dev.parent = get_device(component_dev);
James Bottomley61a7afa2005-08-16 18:27:34 -0500239 rc->num = rd->component_count++;
240
Kay Sievers71610f52008-12-03 22:41:36 +0100241 dev_set_name(&rc->dev, "component-%d", rc->num);
James Bottomley61a7afa2005-08-16 18:27:34 -0500242 list_add_tail(&rc->node, &rd->component_list);
Tony Jonesee959b02008-02-22 00:13:36 +0100243 rc->dev.class = &raid_class.class;
244 err = device_add(&rc->dev);
Jeff Garziked542be2006-10-04 07:05:11 -0400245 if (err)
246 goto err_out;
247
248 return 0;
249
250err_out:
251 list_del(&rc->node);
252 rd->component_count--;
253 put_device(component_dev);
254 kfree(rc);
255 return err;
James Bottomley61a7afa2005-08-16 18:27:34 -0500256}
257EXPORT_SYMBOL(raid_component_add);
258
259struct raid_template *
260raid_class_attach(struct raid_function_template *ft)
261{
James Bottomleyb1081ea2005-11-06 11:59:08 -0600262 struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
James Bottomley61a7afa2005-08-16 18:27:34 -0500263 GFP_KERNEL);
264 int count = 0;
265
266 if (unlikely(!i))
267 return NULL;
268
James Bottomley61a7afa2005-08-16 18:27:34 -0500269 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
Tony Jonesee959b02008-02-22 00:13:36 +0100277 i->attrs[count++] = &dev_attr_level;
278 i->attrs[count++] = &dev_attr_resync;
279 i->attrs[count++] = &dev_attr_state;
James Bottomley61a7afa2005-08-16 18:27:34 -0500280
281 i->attrs[count] = NULL;
282 BUG_ON(count > RAID_NUM_ATTRS);
283
284 return &i->r;
285}
286EXPORT_SYMBOL(raid_class_attach);
287
288void
289raid_class_release(struct raid_template *r)
290{
291 struct raid_internal *i = to_raid_internal(r);
292
James Bottomley2f3edc62008-04-02 10:05:48 -0500293 BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
James Bottomley61a7afa2005-08-16 18:27:34 -0500294
295 kfree(i);
296}
297EXPORT_SYMBOL(raid_class_release);
298
299static __init int raid_init(void)
300{
301 return transport_class_register(&raid_class);
302}
303
304static __exit void raid_exit(void)
305{
306 transport_class_unregister(&raid_class);
307}
308
309MODULE_AUTHOR("James Bottomley");
310MODULE_DESCRIPTION("RAID device class");
311MODULE_LICENSE("GPL");
312
313module_init(raid_init);
314module_exit(raid_exit);
315