4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/list.h>
7 #include <linux/raid_class.h>
8 #include <scsi/scsi_device.h>
9 #include <scsi/scsi_host.h>
11 #define RAID_NUM_ATTRS 3
13 struct raid_internal {
14 struct raid_template r;
15 struct raid_function_template *f;
16 /* The actual attributes */
17 struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
18 /* The array of null terminated pointers to attributes
19 * needed by scsi_sysfs.c */
20 struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
23 struct raid_component {
24 struct list_head node;
29 #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
31 #define tc_to_raid_internal(tcont) ({ \
32 struct raid_template *r = \
33 container_of(tcont, struct raid_template, raid_attrs); \
34 to_raid_internal(r); \
37 #define ac_to_raid_internal(acont) ({ \
38 struct transport_container *tc = \
39 container_of(acont, struct transport_container, ac); \
40 tc_to_raid_internal(tc); \
43 #define class_device_to_raid_internal(cdev) ({ \
44 struct attribute_container *ac = \
45 attribute_container_classdev_to_container(cdev); \
46 ac_to_raid_internal(ac); \
50 static int raid_match(struct attribute_container *cont, struct device *dev)
52 /* We have to look for every subsystem that could house
53 * emulated RAID devices, so start with SCSI */
54 struct raid_internal *i = ac_to_raid_internal(cont);
56 if (scsi_is_sdev_device(dev)) {
57 struct scsi_device *sdev = to_scsi_device(dev);
59 if (i->f->cookie != sdev->host->hostt)
62 return i->f->is_raid(dev);
64 /* FIXME: look at other subsystems too */
68 static int raid_setup(struct transport_container *tc, struct device *dev,
69 struct class_device *cdev)
73 BUG_ON(class_get_devdata(cdev));
75 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
79 memset(rd, 0, sizeof(*rd));
80 INIT_LIST_HEAD(&rd->component_list);
81 class_set_devdata(cdev, rd);
86 static int raid_remove(struct transport_container *tc, struct device *dev,
87 struct class_device *cdev)
89 struct raid_data *rd = class_get_devdata(cdev);
90 struct raid_component *rc, *next;
91 class_set_devdata(cdev, NULL);
92 list_for_each_entry_safe(rc, next, &rd->component_list, node) {
94 snprintf(buf, sizeof(buf), "component-%d", rc->num);
96 sysfs_remove_link(&cdev->kobj, buf);
99 kfree(class_get_devdata(cdev));
103 static DECLARE_TRANSPORT_CLASS(raid_class,
110 enum raid_state value;
113 { RAID_ACTIVE, "active" },
114 { RAID_DEGRADED, "degraded" },
115 { RAID_RESYNCING, "resyncing" },
116 { RAID_OFFLINE, "offline" },
119 static const char *raid_state_name(enum raid_state state)
124 for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
125 if (raid_states[i].value == state) {
126 name = raid_states[i].name;
134 #define raid_attr_show_internal(attr, fmt, var, code) \
135 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
137 struct raid_data *rd = class_get_devdata(cdev); \
139 return snprintf(buf, 20, #fmt "\n", var); \
142 #define raid_attr_ro_states(attr, states, code) \
143 raid_attr_show_internal(attr, %s, name, \
146 name = raid_##states##_name(rd->attr); \
148 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
151 #define raid_attr_ro_internal(attr, code) \
152 raid_attr_show_internal(attr, %d, rd->attr, code) \
153 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
155 #define ATTR_CODE(attr) \
156 struct raid_internal *i = class_device_to_raid_internal(cdev); \
157 if (i->f->get_##attr) \
158 i->f->get_##attr(cdev->dev);
160 #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
161 #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
162 #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
165 raid_attr_ro_fn(resync);
166 raid_attr_ro_state(state);
168 void raid_component_add(struct raid_template *r,struct device *raid_dev,
169 struct device *component_dev)
171 struct class_device *cdev =
172 attribute_container_find_class_device(&r->raid_attrs.ac,
174 struct raid_component *rc;
175 struct raid_data *rd = class_get_devdata(cdev);
178 rc = kmalloc(sizeof(*rc), GFP_KERNEL);
182 INIT_LIST_HEAD(&rc->node);
183 rc->dev = component_dev;
184 rc->num = rd->component_count++;
186 snprintf(buf, sizeof(buf), "component-%d", rc->num);
187 list_add_tail(&rc->node, &rd->component_list);
188 sysfs_create_link(&cdev->kobj, &component_dev->kobj, buf);
190 EXPORT_SYMBOL(raid_component_add);
192 struct raid_template *
193 raid_class_attach(struct raid_function_template *ft)
195 struct raid_internal *i = kmalloc(sizeof(struct raid_internal),
202 memset(i, 0, sizeof(*i));
206 i->r.raid_attrs.ac.class = &raid_class.class;
207 i->r.raid_attrs.ac.match = raid_match;
208 i->r.raid_attrs.ac.attrs = &i->attrs[0];
210 attribute_container_register(&i->r.raid_attrs.ac);
212 i->attrs[count++] = &class_device_attr_level;
213 i->attrs[count++] = &class_device_attr_resync;
214 i->attrs[count++] = &class_device_attr_state;
216 i->attrs[count] = NULL;
217 BUG_ON(count > RAID_NUM_ATTRS);
221 EXPORT_SYMBOL(raid_class_attach);
224 raid_class_release(struct raid_template *r)
226 struct raid_internal *i = to_raid_internal(r);
228 attribute_container_unregister(&i->r.raid_attrs.ac);
232 EXPORT_SYMBOL(raid_class_release);
234 static __init int raid_init(void)
236 return transport_class_register(&raid_class);
239 static __exit void raid_exit(void)
241 transport_class_unregister(&raid_class);
244 MODULE_AUTHOR("James Bottomley");
245 MODULE_DESCRIPTION("RAID device class");
246 MODULE_LICENSE("GPL");
248 module_init(raid_init);
249 module_exit(raid_exit);