4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/list.h>
7 #include <linux/slab.h>
8 #include <linux/string.h>
9 #include <linux/raid_class.h>
10 #include <scsi/scsi_device.h>
11 #include <scsi/scsi_host.h>
13 #define RAID_NUM_ATTRS 3
15 struct raid_internal {
16 struct raid_template r;
17 struct raid_function_template *f;
18 /* The actual attributes */
19 struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
20 /* The array of null terminated pointers to attributes
21 * needed by scsi_sysfs.c */
22 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
25 struct raid_component {
26 struct list_head node;
31 #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
33 #define tc_to_raid_internal(tcont) ({ \
34 struct raid_template *r = \
35 container_of(tcont, struct raid_template, raid_attrs); \
36 to_raid_internal(r); \
39 #define ac_to_raid_internal(acont) ({ \
40 struct transport_container *tc = \
41 container_of(acont, struct transport_container, ac); \
42 tc_to_raid_internal(tc); \
45 #define class_device_to_raid_internal(cdev) ({ \
46 struct attribute_container *ac = \
47 attribute_container_classdev_to_container(cdev); \
48 ac_to_raid_internal(ac); \
52 static int raid_match(struct attribute_container *cont, struct device *dev)
54 /* We have to look for every subsystem that could house
55 * emulated RAID devices, so start with SCSI */
56 struct raid_internal *i = ac_to_raid_internal(cont);
58 if (scsi_is_sdev_device(dev)) {
59 struct scsi_device *sdev = to_scsi_device(dev);
61 if (i->f->cookie != sdev->host->hostt)
64 return i->f->is_raid(dev);
66 /* FIXME: look at other subsystems too */
70 static int raid_setup(struct transport_container *tc, struct device *dev,
71 struct class_device *cdev)
75 BUG_ON(class_get_devdata(cdev));
77 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
81 memset(rd, 0, sizeof(*rd));
82 INIT_LIST_HEAD(&rd->component_list);
83 class_set_devdata(cdev, rd);
88 static int raid_remove(struct transport_container *tc, struct device *dev,
89 struct class_device *cdev)
91 struct raid_data *rd = class_get_devdata(cdev);
92 struct raid_component *rc, *next;
93 class_set_devdata(cdev, NULL);
94 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
96 snprintf(buf, sizeof(buf), "component-%d", rc->num);
98 sysfs_remove_link(&cdev->kobj, buf);
101 kfree(class_get_devdata(cdev));
105 static DECLARE_TRANSPORT_CLASS(raid_class,
112 enum raid_state value;
115 { RAID_ACTIVE, "active" },
116 { RAID_DEGRADED, "degraded" },
117 { RAID_RESYNCING, "resyncing" },
118 { RAID_OFFLINE, "offline" },
121 static const char *raid_state_name(enum raid_state state)
126 for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
127 if (raid_states[i].value == state) {
128 name = raid_states[i].name;
136 #define raid_attr_show_internal(attr, fmt, var, code) \
137 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
139 struct raid_data *rd = class_get_devdata(cdev); \
141 return snprintf(buf, 20, #fmt "\n", var); \
144 #define raid_attr_ro_states(attr, states, code) \
145 raid_attr_show_internal(attr, %s, name, \
148 name = raid_##states##_name(rd->attr); \
150 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
153 #define raid_attr_ro_internal(attr, code) \
154 raid_attr_show_internal(attr, %d, rd->attr, code) \
155 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
157 #define ATTR_CODE(attr) \
158 struct raid_internal *i = class_device_to_raid_internal(cdev); \
159 if (i->f->get_##attr) \
160 i->f->get_##attr(cdev->dev);
162 #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
163 #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
164 #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
167 raid_attr_ro_fn(resync);
168 raid_attr_ro_state(state);
170 void raid_component_add(struct raid_template *r,struct device *raid_dev,
171 struct device *component_dev)
173 struct class_device *cdev =
174 attribute_container_find_class_device(&r->raid_attrs.ac,
176 struct raid_component *rc;
177 struct raid_data *rd = class_get_devdata(cdev);
180 rc = kmalloc(sizeof(*rc), GFP_KERNEL);
184 INIT_LIST_HEAD(&rc->node);
185 rc->dev = component_dev;
186 rc->num = rd->component_count++;
188 snprintf(buf, sizeof(buf), "component-%d", rc->num);
189 list_add_tail(&rc->node, &rd->component_list);
190 sysfs_create_link(&cdev->kobj, &component_dev->kobj, buf);
192 EXPORT_SYMBOL(raid_component_add);
194 struct raid_template *
195 raid_class_attach(struct raid_function_template *ft)
197 struct raid_internal *i = kmalloc(sizeof(struct raid_internal),
204 memset(i, 0, sizeof(*i));
208 i->r.raid_attrs.ac.class = &raid_class.class;
209 i->r.raid_attrs.ac.match = raid_match;
210 i->r.raid_attrs.ac.attrs = &i->attrs[0];
212 attribute_container_register(&i->r.raid_attrs.ac);
214 i->attrs[count++] = &class_device_attr_level;
215 i->attrs[count++] = &class_device_attr_resync;
216 i->attrs[count++] = &class_device_attr_state;
218 i->attrs[count] = NULL;
219 BUG_ON(count > RAID_NUM_ATTRS);
223 EXPORT_SYMBOL(raid_class_attach);
226 raid_class_release(struct raid_template *r)
228 struct raid_internal *i = to_raid_internal(r);
230 attribute_container_unregister(&i->r.raid_attrs.ac);
234 EXPORT_SYMBOL(raid_class_release);
236 static __init int raid_init(void)
238 return transport_class_register(&raid_class);
241 static __exit void raid_exit(void)
243 transport_class_unregister(&raid_class);
246 MODULE_AUTHOR("James Bottomley");
247 MODULE_DESCRIPTION("RAID device class");
248 MODULE_LICENSE("GPL");
250 module_init(raid_init);
251 module_exit(raid_exit);