[PATCH] fix remaining missing includes
[safe/jmp/linux-2.6] / drivers / scsi / raid_class.c
1 /*
2  * RAID Attributes
3  */
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>
12
13 #define RAID_NUM_ATTRS  3
14
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];
23 };
24
25 struct raid_component {
26         struct list_head node;
27         struct device *dev;
28         int num;
29 };
30
31 #define to_raid_internal(tmpl)  container_of(tmpl, struct raid_internal, r)
32
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);                                            \
37 })
38
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);                                        \
43 })
44
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);                                        \
49 })
50         
51
52 static int raid_match(struct attribute_container *cont, struct device *dev)
53 {
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);
57
58         if (scsi_is_sdev_device(dev)) {
59                 struct scsi_device *sdev = to_scsi_device(dev);
60
61                 if (i->f->cookie != sdev->host->hostt)
62                         return 0;
63
64                 return i->f->is_raid(dev);
65         }
66         /* FIXME: look at other subsystems too */
67         return 0;
68 }
69
70 static int raid_setup(struct transport_container *tc, struct device *dev,
71                        struct class_device *cdev)
72 {
73         struct raid_data *rd;
74
75         BUG_ON(class_get_devdata(cdev));
76
77         rd = kmalloc(sizeof(*rd), GFP_KERNEL);
78         if (!rd)
79                 return -ENOMEM;
80
81         memset(rd, 0, sizeof(*rd));
82         INIT_LIST_HEAD(&rd->component_list);
83         class_set_devdata(cdev, rd);
84                 
85         return 0;
86 }
87
88 static int raid_remove(struct transport_container *tc, struct device *dev,
89                        struct class_device *cdev)
90 {
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) {
95                 char buf[40];
96                 snprintf(buf, sizeof(buf), "component-%d", rc->num);
97                 list_del(&rc->node);
98                 sysfs_remove_link(&cdev->kobj, buf);
99                 kfree(rc);
100         }
101         kfree(class_get_devdata(cdev));
102         return 0;
103 }
104
105 static DECLARE_TRANSPORT_CLASS(raid_class,
106                                "raid_devices",
107                                raid_setup,
108                                raid_remove,
109                                NULL);
110
111 static struct {
112         enum raid_state value;
113         char            *name;
114 } raid_states[] = {
115         { RAID_ACTIVE, "active" },
116         { RAID_DEGRADED, "degraded" },
117         { RAID_RESYNCING, "resyncing" },
118         { RAID_OFFLINE, "offline" },
119 };
120
121 static const char *raid_state_name(enum raid_state state)
122 {
123         int i;
124         char *name = NULL;
125
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;
129                         break;
130                 }
131         }
132         return name;
133 }
134
135
136 #define raid_attr_show_internal(attr, fmt, var, code)                   \
137 static ssize_t raid_show_##attr(struct class_device *cdev, char *buf)   \
138 {                                                                       \
139         struct raid_data *rd = class_get_devdata(cdev);                 \
140         code                                                            \
141         return snprintf(buf, 20, #fmt "\n", var);                       \
142 }
143
144 #define raid_attr_ro_states(attr, states, code)                         \
145 raid_attr_show_internal(attr, %s, name,                                 \
146         const char *name;                                               \
147         code                                                            \
148         name = raid_##states##_name(rd->attr);                          \
149 )                                                                       \
150 static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
151
152
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)
156
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);
161
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))
165
166 raid_attr_ro(level);
167 raid_attr_ro_fn(resync);
168 raid_attr_ro_state(state);
169
170 void raid_component_add(struct raid_template *r,struct device *raid_dev,
171                         struct device *component_dev)
172 {
173         struct class_device *cdev =
174                 attribute_container_find_class_device(&r->raid_attrs.ac,
175                                                       raid_dev);
176         struct raid_component *rc;
177         struct raid_data *rd = class_get_devdata(cdev);
178         char buf[40];
179
180         rc = kmalloc(sizeof(*rc), GFP_KERNEL);
181         if (!rc)
182                 return;
183
184         INIT_LIST_HEAD(&rc->node);
185         rc->dev = component_dev;
186         rc->num = rd->component_count++;
187
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);
191 }
192 EXPORT_SYMBOL(raid_component_add);
193
194 struct raid_template *
195 raid_class_attach(struct raid_function_template *ft)
196 {
197         struct raid_internal *i = kmalloc(sizeof(struct raid_internal),
198                                           GFP_KERNEL);
199         int count = 0;
200
201         if (unlikely(!i))
202                 return NULL;
203
204         memset(i, 0, sizeof(*i));
205
206         i->f = ft;
207
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];
211
212         attribute_container_register(&i->r.raid_attrs.ac);
213
214         i->attrs[count++] = &class_device_attr_level;
215         i->attrs[count++] = &class_device_attr_resync;
216         i->attrs[count++] = &class_device_attr_state;
217
218         i->attrs[count] = NULL;
219         BUG_ON(count > RAID_NUM_ATTRS);
220
221         return &i->r;
222 }
223 EXPORT_SYMBOL(raid_class_attach);
224
225 void
226 raid_class_release(struct raid_template *r)
227 {
228         struct raid_internal *i = to_raid_internal(r);
229
230         attribute_container_unregister(&i->r.raid_attrs.ac);
231
232         kfree(i);
233 }
234 EXPORT_SYMBOL(raid_class_release);
235
236 static __init int raid_init(void)
237 {
238         return transport_class_register(&raid_class);
239 }
240
241 static __exit void raid_exit(void)
242 {
243         transport_class_unregister(&raid_class);
244 }
245
246 MODULE_AUTHOR("James Bottomley");
247 MODULE_DESCRIPTION("RAID device class");
248 MODULE_LICENSE("GPL");
249
250 module_init(raid_init);
251 module_exit(raid_exit);
252