raid_class.c 7.6 KB
Newer Older
J
James Bottomley 已提交
1
/*
J
James Bottomley 已提交
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.
J
James Bottomley 已提交
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>
J
James Bottomley 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#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 */
	struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
	/* The array of null terminated pointers to attributes 
	 * needed by scsi_sysfs.c */
	struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
};

struct raid_component {
	struct list_head node;
J
James Bottomley 已提交
35
	struct class_device cdev;
J
James Bottomley 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	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);					\
})

#define class_device_to_raid_internal(cdev) ({				\
	struct attribute_container *ac =				\
		attribute_container_classdev_to_container(cdev);	\
	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);

	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);
	}
	/* FIXME: look at other subsystems too */
	return 0;
}

static int raid_setup(struct transport_container *tc, struct device *dev,
		       struct class_device *cdev)
{
	struct raid_data *rd;

	BUG_ON(class_get_devdata(cdev));

J
James Bottomley 已提交
85
	rd = kzalloc(sizeof(*rd), GFP_KERNEL);
J
James Bottomley 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if (!rd)
		return -ENOMEM;

	INIT_LIST_HEAD(&rd->component_list);
	class_set_devdata(cdev, rd);
		
	return 0;
}

static int raid_remove(struct transport_container *tc, struct device *dev,
		       struct class_device *cdev)
{
	struct raid_data *rd = class_get_devdata(cdev);
	struct raid_component *rc, *next;
J
James Bottomley 已提交
100
	dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
J
James Bottomley 已提交
101 102 103
	class_set_devdata(cdev, NULL);
	list_for_each_entry_safe(rc, next, &rd->component_list, node) {
		list_del(&rc->node);
J
James Bottomley 已提交
104 105
		dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
		class_device_unregister(&rc->cdev);
J
James Bottomley 已提交
106
	}
J
James Bottomley 已提交
107 108
	dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
	kfree(rd);
J
James Bottomley 已提交
109 110 111 112 113 114 115 116 117
	return 0;
}

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

118
static const struct {
J
James Bottomley 已提交
119 120 121
	enum raid_state	value;
	char		*name;
} raid_states[] = {
J
James Bottomley 已提交
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" },
J
James Bottomley 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
};

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

	for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
		if (raid_states[i].value == state) {
			name = raid_states[i].name;
			break;
		}
	}
	return name;
}

J
James Bottomley 已提交
143 144 145 146 147 148 149 150
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" },
151
	{ RAID_LEVEL_10, "raid10" },
J
James Bottomley 已提交
152 153 154
	{ RAID_LEVEL_3, "raid3" },
	{ RAID_LEVEL_4, "raid4" },
	{ RAID_LEVEL_5, "raid5" },
155
	{ RAID_LEVEL_50, "raid50" },
J
James Bottomley 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	{ RAID_LEVEL_6, "raid6" },
};

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

	for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) {
		if (raid_levels[i].value == level) {
			name = raid_levels[i].name;
			break;
		}
	}
	return name;
}
J
James Bottomley 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

#define raid_attr_show_internal(attr, fmt, var, code)			\
static ssize_t raid_show_##attr(struct class_device *cdev, char *buf)	\
{									\
	struct raid_data *rd = class_get_devdata(cdev);			\
	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);				\
)									\
static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)


#define raid_attr_ro_internal(attr, code)				\
raid_attr_show_internal(attr, %d, rd->attr, code)			\
static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)

#define ATTR_CODE(attr)							\
	struct raid_internal *i = class_device_to_raid_internal(cdev);	\
	if (i->f->get_##attr)						\
		i->f->get_##attr(cdev->dev);

#define raid_attr_ro(attr)	raid_attr_ro_internal(attr, )
#define raid_attr_ro_fn(attr)	raid_attr_ro_internal(attr, ATTR_CODE(attr))
J
James Bottomley 已提交
201 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))

J
James Bottomley 已提交
204

J
James Bottomley 已提交
205
raid_attr_ro_state(level);
J
James Bottomley 已提交
206
raid_attr_ro_fn(resync);
J
James Bottomley 已提交
207 208 209 210 211 212 213 214 215 216
raid_attr_ro_state_fn(state);

static void raid_component_release(struct class_device *cdev)
{
	struct raid_component *rc = container_of(cdev, struct raid_component,
						 cdev);
	dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
	put_device(rc->cdev.dev);
	kfree(rc);
}
J
James Bottomley 已提交
217 218 219 220 221 222 223 224 225 226

void raid_component_add(struct raid_template *r,struct device *raid_dev,
			struct device *component_dev)
{
	struct class_device *cdev =
		attribute_container_find_class_device(&r->raid_attrs.ac,
						      raid_dev);
	struct raid_component *rc;
	struct raid_data *rd = class_get_devdata(cdev);

J
James Bottomley 已提交
227
	rc = kzalloc(sizeof(*rc), GFP_KERNEL);
J
James Bottomley 已提交
228 229 230 231
	if (!rc)
		return;

	INIT_LIST_HEAD(&rc->node);
J
James Bottomley 已提交
232 233 234
	class_device_initialize(&rc->cdev);
	rc->cdev.release = raid_component_release;
	rc->cdev.dev = get_device(component_dev);
J
James Bottomley 已提交
235 236
	rc->num = rd->component_count++;

J
James Bottomley 已提交
237 238
	snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
		 "component-%d", rc->num);
J
James Bottomley 已提交
239
	list_add_tail(&rc->node, &rd->component_list);
J
James Bottomley 已提交
240 241 242
	rc->cdev.parent = cdev;
	rc->cdev.class = &raid_class.class;
	class_device_add(&rc->cdev);
J
James Bottomley 已提交
243 244 245 246 247 248
}
EXPORT_SYMBOL(raid_component_add);

struct raid_template *
raid_class_attach(struct raid_function_template *ft)
{
J
James Bottomley 已提交
249
	struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
J
James Bottomley 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
					  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);

	i->attrs[count++] = &class_device_attr_level;
	i->attrs[count++] = &class_device_attr_resync;
	i->attrs[count++] = &class_device_attr_state;

	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);

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

	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);