raid_class.c 7.66 KB
Newer Older
James Bottomley's avatar
James Bottomley committed
1
/*
James Bottomley's avatar
James Bottomley committed
2
3
4
5
6
7
8
9
10
 * raid_class.c - implementation of a simple raid visualisation class
 *
 * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
 *
 * This file is licensed under GPLv2
 *
 * This class is designed to allow raid attributes to be visualised and
 * manipulated in a form independent of the underlying raid.  Ultimately this
 * should work for both hardware and software raids.
James Bottomley's avatar
James Bottomley committed
11
12
13
14
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/list.h>
15
16
#include <linux/slab.h>
#include <linux/string.h>
James Bottomley's avatar
James Bottomley committed
17
18
19
20
21
22
23
24
25
26
#include <linux/raid_class.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>

#define RAID_NUM_ATTRS	3

struct raid_internal {
	struct raid_template r;
	struct raid_function_template *f;
	/* The actual attributes */
27
	struct device_attribute private_attrs[RAID_NUM_ATTRS];
James Bottomley's avatar
James Bottomley committed
28
29
	/* The array of null terminated pointers to attributes 
	 * needed by scsi_sysfs.c */
30
	struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
James Bottomley's avatar
James Bottomley committed
31
32
33
34
};

struct raid_component {
	struct list_head node;
35
	struct device dev;
James Bottomley's avatar
James Bottomley committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	int num;
};

#define to_raid_internal(tmpl)	container_of(tmpl, struct raid_internal, r)

#define tc_to_raid_internal(tcont) ({					\
	struct raid_template *r =					\
		container_of(tcont, struct raid_template, raid_attrs);	\
	to_raid_internal(r);						\
})

#define ac_to_raid_internal(acont) ({					\
	struct transport_container *tc =				\
		container_of(acont, struct transport_container, ac);	\
	tc_to_raid_internal(tc);					\
})

53
#define device_to_raid_internal(dev) ({				\
James Bottomley's avatar
James Bottomley committed
54
	struct attribute_container *ac =				\
55
		attribute_container_classdev_to_container(dev);	\
James Bottomley's avatar
James Bottomley committed
56
57
58
59
60
61
62
63
64
65
	ac_to_raid_internal(ac);					\
})
	

static int raid_match(struct attribute_container *cont, struct device *dev)
{
	/* We have to look for every subsystem that could house
	 * emulated RAID devices, so start with SCSI */
	struct raid_internal *i = ac_to_raid_internal(cont);

66
#if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
James Bottomley's avatar
James Bottomley committed
67
68
69
70
71
72
73
74
	if (scsi_is_sdev_device(dev)) {
		struct scsi_device *sdev = to_scsi_device(dev);

		if (i->f->cookie != sdev->host->hostt)
			return 0;

		return i->f->is_raid(dev);
	}
75
#endif
James Bottomley's avatar
James Bottomley committed
76
77
78
79
80
	/* FIXME: look at other subsystems too */
	return 0;
}

static int raid_setup(struct transport_container *tc, struct device *dev,
81
		       struct device *cdev)
James Bottomley's avatar
James Bottomley committed
82
83
84
{
	struct raid_data *rd;

85
	BUG_ON(dev_get_drvdata(cdev));
James Bottomley's avatar
James Bottomley committed
86

James Bottomley's avatar
James Bottomley committed
87
	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
James Bottomley's avatar
James Bottomley committed
88
89
90
91
	if (!rd)
		return -ENOMEM;

	INIT_LIST_HEAD(&rd->component_list);
92
	dev_set_drvdata(cdev, rd);
James Bottomley's avatar
James Bottomley committed
93
94
95
96
97
		
	return 0;
}

static int raid_remove(struct transport_container *tc, struct device *dev,
98
		       struct device *cdev)
James Bottomley's avatar
James Bottomley committed
99
{
100
	struct raid_data *rd = dev_get_drvdata(cdev);
James Bottomley's avatar
James Bottomley committed
101
	struct raid_component *rc, *next;
James Bottomley's avatar
James Bottomley committed
102
	dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
103
	dev_set_drvdata(cdev, NULL);
James Bottomley's avatar
James Bottomley committed
104
105
	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
		list_del(&rc->node);
106
107
		dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
		device_unregister(&rc->dev);
James Bottomley's avatar
James Bottomley committed
108
	}
James Bottomley's avatar
James Bottomley committed
109
110
	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
	kfree(rd);
James Bottomley's avatar
James Bottomley committed
111
112
113
114
115
116
117
118
119
	return 0;
}

static DECLARE_TRANSPORT_CLASS(raid_class,
			       "raid_devices",
			       raid_setup,
			       raid_remove,
			       NULL);

120
static const struct {
James Bottomley's avatar
James Bottomley committed
121
122
123
	enum raid_state	value;
	char		*name;
} raid_states[] = {
James Bottomley's avatar
James Bottomley committed
124
125
126
127
128
	{ RAID_STATE_UNKNOWN, "unknown" },
	{ RAID_STATE_ACTIVE, "active" },
	{ RAID_STATE_DEGRADED, "degraded" },
	{ RAID_STATE_RESYNCING, "resyncing" },
	{ RAID_STATE_OFFLINE, "offline" },
James Bottomley's avatar
James Bottomley committed
129
130
131
132
133
134
135
};

static const char *raid_state_name(enum raid_state state)
{
	int i;
	char *name = NULL;

136
	for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
James Bottomley's avatar
James Bottomley committed
137
138
139
140
141
142
143
144
		if (raid_states[i].value == state) {
			name = raid_states[i].name;
			break;
		}
	}
	return name;
}

James Bottomley's avatar
James Bottomley committed
145
146
147
148
149
150
151
152
static struct {
	enum raid_level value;
	char *name;
} raid_levels[] = {
	{ RAID_LEVEL_UNKNOWN, "unknown" },
	{ RAID_LEVEL_LINEAR, "linear" },
	{ RAID_LEVEL_0, "raid0" },
	{ RAID_LEVEL_1, "raid1" },
153
	{ RAID_LEVEL_10, "raid10" },
Kashyap, Desai's avatar
Kashyap, Desai committed
154
	{ RAID_LEVEL_1E, "raid1e" },
James Bottomley's avatar
James Bottomley committed
155
156
157
	{ RAID_LEVEL_3, "raid3" },
	{ RAID_LEVEL_4, "raid4" },
	{ RAID_LEVEL_5, "raid5" },
158
	{ RAID_LEVEL_50, "raid50" },
James Bottomley's avatar
James Bottomley committed
159
160
161
162
163
164
165
166
	{ RAID_LEVEL_6, "raid6" },
};

static const char *raid_level_name(enum raid_level level)
{
	int i;
	char *name = NULL;

167
	for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
James Bottomley's avatar
James Bottomley committed
168
169
170
171
172
173
174
		if (raid_levels[i].value == level) {
			name = raid_levels[i].name;
			break;
		}
	}
	return name;
}
James Bottomley's avatar
James Bottomley committed
175
176

#define raid_attr_show_internal(attr, fmt, var, code)			\
177
178
179
static ssize_t raid_show_##attr(struct device *dev, 			\
				struct device_attribute *attr, 		\
				char *buf)				\
James Bottomley's avatar
James Bottomley committed
180
{									\
181
	struct raid_data *rd = dev_get_drvdata(dev);			\
James Bottomley's avatar
James Bottomley committed
182
183
184
185
186
187
188
189
190
191
	code								\
	return snprintf(buf, 20, #fmt "\n", var);			\
}

#define raid_attr_ro_states(attr, states, code)				\
raid_attr_show_internal(attr, %s, name,					\
	const char *name;						\
	code								\
	name = raid_##states##_name(rd->attr);				\
)									\
192
static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
James Bottomley's avatar
James Bottomley committed
193
194
195
196


#define raid_attr_ro_internal(attr, code)				\
raid_attr_show_internal(attr, %d, rd->attr, code)			\
197
static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
James Bottomley's avatar
James Bottomley committed
198
199

#define ATTR_CODE(attr)							\
200
	struct raid_internal *i = device_to_raid_internal(dev);		\
James Bottomley's avatar
James Bottomley committed
201
	if (i->f->get_##attr)						\
202
		i->f->get_##attr(dev->parent);
James Bottomley's avatar
James Bottomley committed
203
204
205

#define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
#define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
James Bottomley's avatar
James Bottomley committed
206
207
208
#define raid_attr_ro_state(attr)	raid_attr_ro_states(attr, attr, )
#define raid_attr_ro_state_fn(attr)	raid_attr_ro_states(attr, attr, ATTR_CODE(attr))

James Bottomley's avatar
James Bottomley committed
209

James Bottomley's avatar
James Bottomley committed
210
raid_attr_ro_state(level);
James Bottomley's avatar
James Bottomley committed
211
raid_attr_ro_fn(resync);
James Bottomley's avatar
James Bottomley committed
212
213
raid_attr_ro_state_fn(state);

214
static void raid_component_release(struct device *dev)
James Bottomley's avatar
James Bottomley committed
215
{
216
217
218
219
	struct raid_component *rc =
		container_of(dev, struct raid_component, dev);
	dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n");
	put_device(rc->dev.parent);
James Bottomley's avatar
James Bottomley committed
220
221
	kfree(rc);
}
James Bottomley's avatar
James Bottomley committed
222

223
224
int raid_component_add(struct raid_template *r,struct device *raid_dev,
		       struct device *component_dev)
James Bottomley's avatar
James Bottomley committed
225
{
226
	struct device *cdev =
James Bottomley's avatar
James Bottomley committed
227
228
229
		attribute_container_find_class_device(&r->raid_attrs.ac,
						      raid_dev);
	struct raid_component *rc;
230
	struct raid_data *rd = dev_get_drvdata(cdev);
231
	int err;
James Bottomley's avatar
James Bottomley committed
232

James Bottomley's avatar
James Bottomley committed
233
	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
James Bottomley's avatar
James Bottomley committed
234
	if (!rc)
235
		return -ENOMEM;
James Bottomley's avatar
James Bottomley committed
236
237

	INIT_LIST_HEAD(&rc->node);
238
239
240
	device_initialize(&rc->dev);
	rc->dev.release = raid_component_release;
	rc->dev.parent = get_device(component_dev);
James Bottomley's avatar
James Bottomley committed
241
242
	rc->num = rd->component_count++;

243
	dev_set_name(&rc->dev, "component-%d", rc->num);
James Bottomley's avatar
James Bottomley committed
244
	list_add_tail(&rc->node, &rd->component_list);
245
246
	rc->dev.class = &raid_class.class;
	err = device_add(&rc->dev);
247
248
249
250
251
252
253
254
255
256
257
	if (err)
		goto err_out;

	return 0;

err_out:
	list_del(&rc->node);
	rd->component_count--;
	put_device(component_dev);
	kfree(rc);
	return err;
James Bottomley's avatar
James Bottomley committed
258
259
260
261
262
263
}
EXPORT_SYMBOL(raid_component_add);

struct raid_template *
raid_class_attach(struct raid_function_template *ft)
{
James Bottomley's avatar
James Bottomley committed
264
	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
James Bottomley's avatar
James Bottomley committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
					  GFP_KERNEL);
	int count = 0;

	if (unlikely(!i))
		return NULL;

	i->f = ft;

	i->r.raid_attrs.ac.class = &raid_class.class;
	i->r.raid_attrs.ac.match = raid_match;
	i->r.raid_attrs.ac.attrs = &i->attrs[0];

	attribute_container_register(&i->r.raid_attrs.ac);

279
280
281
	i->attrs[count++] = &dev_attr_level;
	i->attrs[count++] = &dev_attr_resync;
	i->attrs[count++] = &dev_attr_state;
James Bottomley's avatar
James Bottomley committed
282
283
284
285
286
287
288
289
290
291
292
293
294

	i->attrs[count] = NULL;
	BUG_ON(count > RAID_NUM_ATTRS);

	return &i->r;
}
EXPORT_SYMBOL(raid_class_attach);

void
raid_class_release(struct raid_template *r)
{
	struct raid_internal *i = to_raid_internal(r);

295
	BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
James Bottomley's avatar
James Bottomley committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

	kfree(i);
}
EXPORT_SYMBOL(raid_class_release);

static __init int raid_init(void)
{
	return transport_class_register(&raid_class);
}

static __exit void raid_exit(void)
{
	transport_class_unregister(&raid_class);
}

MODULE_AUTHOR("James Bottomley");
MODULE_DESCRIPTION("RAID device class");
MODULE_LICENSE("GPL");

module_init(raid_init);
module_exit(raid_exit);