raid_class.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * raid_class.c - implementation of a simple raid visualisation class
  3. *
  4. * Copyright (c) 2005 - James Bottomley <James.Bottomley@steeleye.com>
  5. *
  6. * This file is licensed under GPLv2
  7. *
  8. * This class is designed to allow raid attributes to be visualised and
  9. * manipulated in a form independent of the underlying raid. Ultimately this
  10. * should work for both hardware and software raids.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/raid_class.h>
  18. #include <scsi/scsi_device.h>
  19. #include <scsi/scsi_host.h>
  20. #define RAID_NUM_ATTRS 3
  21. struct raid_internal {
  22. struct raid_template r;
  23. struct raid_function_template *f;
  24. /* The actual attributes */
  25. struct device_attribute private_attrs[RAID_NUM_ATTRS];
  26. /* The array of null terminated pointers to attributes
  27. * needed by scsi_sysfs.c */
  28. struct device_attribute *attrs[RAID_NUM_ATTRS + 1];
  29. };
  30. struct raid_component {
  31. struct list_head node;
  32. struct device dev;
  33. int num;
  34. };
  35. #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
  36. #define tc_to_raid_internal(tcont) ({ \
  37. struct raid_template *r = \
  38. container_of(tcont, struct raid_template, raid_attrs); \
  39. to_raid_internal(r); \
  40. })
  41. #define ac_to_raid_internal(acont) ({ \
  42. struct transport_container *tc = \
  43. container_of(acont, struct transport_container, ac); \
  44. tc_to_raid_internal(tc); \
  45. })
  46. #define device_to_raid_internal(dev) ({ \
  47. struct attribute_container *ac = \
  48. attribute_container_classdev_to_container(dev); \
  49. ac_to_raid_internal(ac); \
  50. })
  51. static int raid_match(struct attribute_container *cont, struct device *dev)
  52. {
  53. /* We have to look for every subsystem that could house
  54. * emulated RAID devices, so start with SCSI */
  55. struct raid_internal *i = ac_to_raid_internal(cont);
  56. #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
  57. if (scsi_is_sdev_device(dev)) {
  58. struct scsi_device *sdev = to_scsi_device(dev);
  59. if (i->f->cookie != sdev->host->hostt)
  60. return 0;
  61. return i->f->is_raid(dev);
  62. }
  63. #endif
  64. /* FIXME: look at other subsystems too */
  65. return 0;
  66. }
  67. static int raid_setup(struct transport_container *tc, struct device *dev,
  68. struct device *cdev)
  69. {
  70. struct raid_data *rd;
  71. BUG_ON(dev_get_drvdata(cdev));
  72. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  73. if (!rd)
  74. return -ENOMEM;
  75. INIT_LIST_HEAD(&rd->component_list);
  76. dev_set_drvdata(cdev, rd);
  77. return 0;
  78. }
  79. static int raid_remove(struct transport_container *tc, struct device *dev,
  80. struct device *cdev)
  81. {
  82. struct raid_data *rd = dev_get_drvdata(cdev);
  83. struct raid_component *rc, *next;
  84. dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
  85. dev_set_drvdata(cdev, NULL);
  86. list_for_each_entry_safe(rc, next, &rd->component_list, node) {
  87. list_del(&rc->node);
  88. dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
  89. device_unregister(&rc->dev);
  90. }
  91. dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
  92. kfree(rd);
  93. return 0;
  94. }
  95. static DECLARE_TRANSPORT_CLASS(raid_class,
  96. "raid_devices",
  97. raid_setup,
  98. raid_remove,
  99. NULL);
  100. static const struct {
  101. enum raid_state value;
  102. char *name;
  103. } raid_states[] = {
  104. { RAID_STATE_UNKNOWN, "unknown" },
  105. { RAID_STATE_ACTIVE, "active" },
  106. { RAID_STATE_DEGRADED, "degraded" },
  107. { RAID_STATE_RESYNCING, "resyncing" },
  108. { RAID_STATE_OFFLINE, "offline" },
  109. };
  110. static const char *raid_state_name(enum raid_state state)
  111. {
  112. int i;
  113. char *name = NULL;
  114. for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
  115. if (raid_states[i].value == state) {
  116. name = raid_states[i].name;
  117. break;
  118. }
  119. }
  120. return name;
  121. }
  122. static struct {
  123. enum raid_level value;
  124. char *name;
  125. } raid_levels[] = {
  126. { RAID_LEVEL_UNKNOWN, "unknown" },
  127. { RAID_LEVEL_LINEAR, "linear" },
  128. { RAID_LEVEL_0, "raid0" },
  129. { RAID_LEVEL_1, "raid1" },
  130. { RAID_LEVEL_10, "raid10" },
  131. { RAID_LEVEL_1E, "raid1e" },
  132. { RAID_LEVEL_3, "raid3" },
  133. { RAID_LEVEL_4, "raid4" },
  134. { RAID_LEVEL_5, "raid5" },
  135. { RAID_LEVEL_50, "raid50" },
  136. { RAID_LEVEL_6, "raid6" },
  137. };
  138. static const char *raid_level_name(enum raid_level level)
  139. {
  140. int i;
  141. char *name = NULL;
  142. for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
  143. if (raid_levels[i].value == level) {
  144. name = raid_levels[i].name;
  145. break;
  146. }
  147. }
  148. return name;
  149. }
  150. #define raid_attr_show_internal(attr, fmt, var, code) \
  151. static ssize_t raid_show_##attr(struct device *dev, \
  152. struct device_attribute *attr, \
  153. char *buf) \
  154. { \
  155. struct raid_data *rd = dev_get_drvdata(dev); \
  156. code \
  157. return snprintf(buf, 20, #fmt "\n", var); \
  158. }
  159. #define raid_attr_ro_states(attr, states, code) \
  160. raid_attr_show_internal(attr, %s, name, \
  161. const char *name; \
  162. code \
  163. name = raid_##states##_name(rd->attr); \
  164. ) \
  165. static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  166. #define raid_attr_ro_internal(attr, code) \
  167. raid_attr_show_internal(attr, %d, rd->attr, code) \
  168. static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  169. #define ATTR_CODE(attr) \
  170. struct raid_internal *i = device_to_raid_internal(dev); \
  171. if (i->f->get_##attr) \
  172. i->f->get_##attr(dev->parent);
  173. #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
  174. #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
  175. #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
  176. #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
  177. raid_attr_ro_state(level);
  178. raid_attr_ro_fn(resync);
  179. raid_attr_ro_state_fn(state);
  180. static void raid_component_release(struct device *dev)
  181. {
  182. struct raid_component *rc =
  183. container_of(dev, struct raid_component, dev);
  184. dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n");
  185. put_device(rc->dev.parent);
  186. kfree(rc);
  187. }
  188. int raid_component_add(struct raid_template *r,struct device *raid_dev,
  189. struct device *component_dev)
  190. {
  191. struct device *cdev =
  192. attribute_container_find_class_device(&r->raid_attrs.ac,
  193. raid_dev);
  194. struct raid_component *rc;
  195. struct raid_data *rd = dev_get_drvdata(cdev);
  196. int err;
  197. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  198. if (!rc)
  199. return -ENOMEM;
  200. INIT_LIST_HEAD(&rc->node);
  201. device_initialize(&rc->dev);
  202. rc->dev.release = raid_component_release;
  203. rc->dev.parent = get_device(component_dev);
  204. rc->num = rd->component_count++;
  205. dev_set_name(&rc->dev, "component-%d", rc->num);
  206. list_add_tail(&rc->node, &rd->component_list);
  207. rc->dev.class = &raid_class.class;
  208. err = device_add(&rc->dev);
  209. if (err)
  210. goto err_out;
  211. return 0;
  212. err_out:
  213. list_del(&rc->node);
  214. rd->component_count--;
  215. put_device(component_dev);
  216. kfree(rc);
  217. return err;
  218. }
  219. EXPORT_SYMBOL(raid_component_add);
  220. struct raid_template *
  221. raid_class_attach(struct raid_function_template *ft)
  222. {
  223. struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
  224. GFP_KERNEL);
  225. int count = 0;
  226. if (unlikely(!i))
  227. return NULL;
  228. i->f = ft;
  229. i->r.raid_attrs.ac.class = &raid_class.class;
  230. i->r.raid_attrs.ac.match = raid_match;
  231. i->r.raid_attrs.ac.attrs = &i->attrs[0];
  232. attribute_container_register(&i->r.raid_attrs.ac);
  233. i->attrs[count++] = &dev_attr_level;
  234. i->attrs[count++] = &dev_attr_resync;
  235. i->attrs[count++] = &dev_attr_state;
  236. i->attrs[count] = NULL;
  237. BUG_ON(count > RAID_NUM_ATTRS);
  238. return &i->r;
  239. }
  240. EXPORT_SYMBOL(raid_class_attach);
  241. void
  242. raid_class_release(struct raid_template *r)
  243. {
  244. struct raid_internal *i = to_raid_internal(r);
  245. BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
  246. kfree(i);
  247. }
  248. EXPORT_SYMBOL(raid_class_release);
  249. static __init int raid_init(void)
  250. {
  251. return transport_class_register(&raid_class);
  252. }
  253. static __exit void raid_exit(void)
  254. {
  255. transport_class_unregister(&raid_class);
  256. }
  257. MODULE_AUTHOR("James Bottomley");
  258. MODULE_DESCRIPTION("RAID device class");
  259. MODULE_LICENSE("GPL");
  260. module_init(raid_init);
  261. module_exit(raid_exit);