Blame view

drivers/scsi/raid_class.c 7.66 KB
61a7afa2c   James Bottomley   [SCSI] embryonic ...
1
  /*
b1081ea6f   James Bottomley   [SCSI] raid class...
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.
61a7afa2c   James Bottomley   [SCSI] embryonic ...
11
12
13
14
   */
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/list.h>
8c65b4a60   Tim Schmielau   [PATCH] fix remai...
15
16
  #include <linux/slab.h>
  #include <linux/string.h>
61a7afa2c   James Bottomley   [SCSI] embryonic ...
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 */
ee959b00c   Tony Jones   SCSI: convert str...
27
  	struct device_attribute private_attrs[RAID_NUM_ATTRS];
61a7afa2c   James Bottomley   [SCSI] embryonic ...
28
29
  	/* The array of null terminated pointers to attributes 
  	 * needed by scsi_sysfs.c */
ee959b00c   Tony Jones   SCSI: convert str...
30
  	struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
61a7afa2c   James Bottomley   [SCSI] embryonic ...
31
32
33
34
  };
  
  struct raid_component {
  	struct list_head node;
ee959b00c   Tony Jones   SCSI: convert str...
35
  	struct device dev;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  	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);					\
  })
ee959b00c   Tony Jones   SCSI: convert str...
52
  #define device_to_raid_internal(dev) ({				\
61a7afa2c   James Bottomley   [SCSI] embryonic ...
53
  	struct attribute_container *ac =				\
ee959b00c   Tony Jones   SCSI: convert str...
54
  		attribute_container_classdev_to_container(dev);	\
61a7afa2c   James Bottomley   [SCSI] embryonic ...
55
56
57
58
59
60
61
62
63
  	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);
fac829fdc   James Bottomley   [SCSI] raid_attrs...
64
  #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
65
66
67
68
69
70
71
72
  	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);
  	}
fac829fdc   James Bottomley   [SCSI] raid_attrs...
73
  #endif
61a7afa2c   James Bottomley   [SCSI] embryonic ...
74
75
76
77
78
  	/* FIXME: look at other subsystems too */
  	return 0;
  }
  
  static int raid_setup(struct transport_container *tc, struct device *dev,
ee959b00c   Tony Jones   SCSI: convert str...
79
  		       struct device *cdev)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
80
81
  {
  	struct raid_data *rd;
ee959b00c   Tony Jones   SCSI: convert str...
82
  	BUG_ON(dev_get_drvdata(cdev));
61a7afa2c   James Bottomley   [SCSI] embryonic ...
83

b1081ea6f   James Bottomley   [SCSI] raid class...
84
  	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
85
86
  	if (!rd)
  		return -ENOMEM;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
87
  	INIT_LIST_HEAD(&rd->component_list);
ee959b00c   Tony Jones   SCSI: convert str...
88
  	dev_set_drvdata(cdev, rd);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
89
90
91
92
93
  		
  	return 0;
  }
  
  static int raid_remove(struct transport_container *tc, struct device *dev,
ee959b00c   Tony Jones   SCSI: convert str...
94
  		       struct device *cdev)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
95
  {
ee959b00c   Tony Jones   SCSI: convert str...
96
  	struct raid_data *rd = dev_get_drvdata(cdev);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
97
  	struct raid_component *rc, *next;
b1081ea6f   James Bottomley   [SCSI] raid class...
98
99
  	dev_printk(KERN_ERR, dev, "RAID REMOVE
  ");
ee959b00c   Tony Jones   SCSI: convert str...
100
  	dev_set_drvdata(cdev, NULL);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
101
  	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
61a7afa2c   James Bottomley   [SCSI] embryonic ...
102
  		list_del(&rc->node);
ee959b00c   Tony Jones   SCSI: convert str...
103
104
105
  		dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE
  ");
  		device_unregister(&rc->dev);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
106
  	}
b1081ea6f   James Bottomley   [SCSI] raid class...
107
108
109
  	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE
  ");
  	kfree(rd);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
110
111
112
113
114
115
116
117
  	return 0;
  }
  
  static DECLARE_TRANSPORT_CLASS(raid_class,
  			       "raid_devices",
  			       raid_setup,
  			       raid_remove,
  			       NULL);
0ad78200b   Arjan van de Ven   [SCSI] Mark some ...
118
  static const struct {
61a7afa2c   James Bottomley   [SCSI] embryonic ...
119
120
121
  	enum raid_state	value;
  	char		*name;
  } raid_states[] = {
b1081ea6f   James Bottomley   [SCSI] raid class...
122
123
124
125
126
  	{ RAID_STATE_UNKNOWN, "unknown" },
  	{ RAID_STATE_ACTIVE, "active" },
  	{ RAID_STATE_DEGRADED, "degraded" },
  	{ RAID_STATE_RESYNCING, "resyncing" },
  	{ RAID_STATE_OFFLINE, "offline" },
61a7afa2c   James Bottomley   [SCSI] embryonic ...
127
128
129
130
131
132
  };
  
  static const char *raid_state_name(enum raid_state state)
  {
  	int i;
  	char *name = NULL;
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
133
  	for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
61a7afa2c   James Bottomley   [SCSI] embryonic ...
134
135
136
137
138
139
140
  		if (raid_states[i].value == state) {
  			name = raid_states[i].name;
  			break;
  		}
  	}
  	return name;
  }
b1081ea6f   James Bottomley   [SCSI] raid class...
141
142
143
144
145
146
147
148
  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" },
8e32ca49e   Moore, Eric   [SCSI] raid_class...
149
  	{ RAID_LEVEL_10, "raid10" },
8e4a0cf79   Kashyap, Desai   [SCSI] raid_class...
150
  	{ RAID_LEVEL_1E, "raid1e" },
b1081ea6f   James Bottomley   [SCSI] raid class...
151
152
153
  	{ RAID_LEVEL_3, "raid3" },
  	{ RAID_LEVEL_4, "raid4" },
  	{ RAID_LEVEL_5, "raid5" },
8e32ca49e   Moore, Eric   [SCSI] raid_class...
154
  	{ RAID_LEVEL_50, "raid50" },
b1081ea6f   James Bottomley   [SCSI] raid class...
155
156
157
158
159
160
161
  	{ RAID_LEVEL_6, "raid6" },
  };
  
  static const char *raid_level_name(enum raid_level level)
  {
  	int i;
  	char *name = NULL;
6391a1137   Tobias Klauser   [SCSI] drivers/sc...
162
  	for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
b1081ea6f   James Bottomley   [SCSI] raid class...
163
164
165
166
167
168
169
  		if (raid_levels[i].value == level) {
  			name = raid_levels[i].name;
  			break;
  		}
  	}
  	return name;
  }
61a7afa2c   James Bottomley   [SCSI] embryonic ...
170
171
  
  #define raid_attr_show_internal(attr, fmt, var, code)			\
ee959b00c   Tony Jones   SCSI: convert str...
172
173
174
  static ssize_t raid_show_##attr(struct device *dev, 			\
  				struct device_attribute *attr, 		\
  				char *buf)				\
61a7afa2c   James Bottomley   [SCSI] embryonic ...
175
  {									\
ee959b00c   Tony Jones   SCSI: convert str...
176
  	struct raid_data *rd = dev_get_drvdata(dev);			\
61a7afa2c   James Bottomley   [SCSI] embryonic ...
177
178
179
180
181
182
183
184
185
186
187
  	code								\
  	return snprintf(buf, 20, #fmt "
  ", 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);				\
  )									\
ee959b00c   Tony Jones   SCSI: convert str...
188
  static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
189
190
191
192
  
  
  #define raid_attr_ro_internal(attr, code)				\
  raid_attr_show_internal(attr, %d, rd->attr, code)			\
ee959b00c   Tony Jones   SCSI: convert str...
193
  static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
194
195
  
  #define ATTR_CODE(attr)							\
ee959b00c   Tony Jones   SCSI: convert str...
196
  	struct raid_internal *i = device_to_raid_internal(dev);		\
61a7afa2c   James Bottomley   [SCSI] embryonic ...
197
  	if (i->f->get_##attr)						\
ee959b00c   Tony Jones   SCSI: convert str...
198
  		i->f->get_##attr(dev->parent);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
199
200
201
  
  #define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
  #define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
b1081ea6f   James Bottomley   [SCSI] raid class...
202
203
  #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))
61a7afa2c   James Bottomley   [SCSI] embryonic ...
204

b1081ea6f   James Bottomley   [SCSI] raid class...
205
  raid_attr_ro_state(level);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
206
  raid_attr_ro_fn(resync);
b1081ea6f   James Bottomley   [SCSI] raid class...
207
  raid_attr_ro_state_fn(state);
ee959b00c   Tony Jones   SCSI: convert str...
208
  static void raid_component_release(struct device *dev)
b1081ea6f   James Bottomley   [SCSI] raid class...
209
  {
ee959b00c   Tony Jones   SCSI: convert str...
210
211
212
213
214
  	struct raid_component *rc =
  		container_of(dev, struct raid_component, dev);
  	dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE
  ");
  	put_device(rc->dev.parent);
b1081ea6f   James Bottomley   [SCSI] raid class...
215
216
  	kfree(rc);
  }
61a7afa2c   James Bottomley   [SCSI] embryonic ...
217

ed542bed1   Jeff Garzik   [SCSI] raid class...
218
219
  int raid_component_add(struct raid_template *r,struct device *raid_dev,
  		       struct device *component_dev)
61a7afa2c   James Bottomley   [SCSI] embryonic ...
220
  {
ee959b00c   Tony Jones   SCSI: convert str...
221
  	struct device *cdev =
61a7afa2c   James Bottomley   [SCSI] embryonic ...
222
223
224
  		attribute_container_find_class_device(&r->raid_attrs.ac,
  						      raid_dev);
  	struct raid_component *rc;
ee959b00c   Tony Jones   SCSI: convert str...
225
  	struct raid_data *rd = dev_get_drvdata(cdev);
ed542bed1   Jeff Garzik   [SCSI] raid class...
226
  	int err;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
227

b1081ea6f   James Bottomley   [SCSI] raid class...
228
  	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
229
  	if (!rc)
ed542bed1   Jeff Garzik   [SCSI] raid class...
230
  		return -ENOMEM;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
231
232
  
  	INIT_LIST_HEAD(&rc->node);
ee959b00c   Tony Jones   SCSI: convert str...
233
234
235
  	device_initialize(&rc->dev);
  	rc->dev.release = raid_component_release;
  	rc->dev.parent = get_device(component_dev);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
236
  	rc->num = rd->component_count++;
71610f55f   Kay Sievers   [SCSI] struct dev...
237
  	dev_set_name(&rc->dev, "component-%d", rc->num);
61a7afa2c   James Bottomley   [SCSI] embryonic ...
238
  	list_add_tail(&rc->node, &rd->component_list);
ee959b00c   Tony Jones   SCSI: convert str...
239
240
  	rc->dev.class = &raid_class.class;
  	err = device_add(&rc->dev);
ed542bed1   Jeff Garzik   [SCSI] raid class...
241
242
243
244
245
246
247
248
249
250
251
  	if (err)
  		goto err_out;
  
  	return 0;
  
  err_out:
  	list_del(&rc->node);
  	rd->component_count--;
  	put_device(component_dev);
  	kfree(rc);
  	return err;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
252
253
254
255
256
257
  }
  EXPORT_SYMBOL(raid_component_add);
  
  struct raid_template *
  raid_class_attach(struct raid_function_template *ft)
  {
b1081ea6f   James Bottomley   [SCSI] raid class...
258
  	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
61a7afa2c   James Bottomley   [SCSI] embryonic ...
259
260
261
262
263
  					  GFP_KERNEL);
  	int count = 0;
  
  	if (unlikely(!i))
  		return NULL;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
264
265
266
267
268
269
270
  	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);
ee959b00c   Tony Jones   SCSI: convert str...
271
272
273
  	i->attrs[count++] = &dev_attr_level;
  	i->attrs[count++] = &dev_attr_resync;
  	i->attrs[count++] = &dev_attr_state;
61a7afa2c   James Bottomley   [SCSI] embryonic ...
274
275
276
277
278
279
280
281
282
283
284
285
  
  	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);
2f3edc693   James Bottomley   [SCSI] transport_...
286
  	BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
61a7afa2c   James Bottomley   [SCSI] embryonic ...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  
  	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);