raid_class.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/raid_class.h>
  16. #include <scsi/scsi_device.h>
  17. #include <scsi/scsi_host.h>
  18. #define RAID_NUM_ATTRS 3
  19. struct raid_internal {
  20. struct raid_template r;
  21. struct raid_function_template *f;
  22. /* The actual attributes */
  23. struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
  24. /* The array of null terminated pointers to attributes
  25. * needed by scsi_sysfs.c */
  26. struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
  27. };
  28. struct raid_component {
  29. struct list_head node;
  30. struct class_device cdev;
  31. int num;
  32. };
  33. #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
  34. #define tc_to_raid_internal(tcont) ({ \
  35. struct raid_template *r = \
  36. container_of(tcont, struct raid_template, raid_attrs); \
  37. to_raid_internal(r); \
  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. #define class_device_to_raid_internal(cdev) ({ \
  45. struct attribute_container *ac = \
  46. attribute_container_classdev_to_container(cdev); \
  47. ac_to_raid_internal(ac); \
  48. })
  49. static int raid_match(struct attribute_container *cont, struct device *dev)
  50. {
  51. /* We have to look for every subsystem that could house
  52. * emulated RAID devices, so start with SCSI */
  53. struct raid_internal *i = ac_to_raid_internal(cont);
  54. if (scsi_is_sdev_device(dev)) {
  55. struct scsi_device *sdev = to_scsi_device(dev);
  56. if (i->f->cookie != sdev->host->hostt)
  57. return 0;
  58. return i->f->is_raid(dev);
  59. }
  60. /* FIXME: look at other subsystems too */
  61. return 0;
  62. }
  63. static int raid_setup(struct transport_container *tc, struct device *dev,
  64. struct class_device *cdev)
  65. {
  66. struct raid_data *rd;
  67. BUG_ON(class_get_devdata(cdev));
  68. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  69. if (!rd)
  70. return -ENOMEM;
  71. INIT_LIST_HEAD(&rd->component_list);
  72. class_set_devdata(cdev, rd);
  73. return 0;
  74. }
  75. static int raid_remove(struct transport_container *tc, struct device *dev,
  76. struct class_device *cdev)
  77. {
  78. struct raid_data *rd = class_get_devdata(cdev);
  79. struct raid_component *rc, *next;
  80. dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
  81. class_set_devdata(cdev, NULL);
  82. list_for_each_entry_safe(rc, next, &rd->component_list, node) {
  83. list_del(&rc->node);
  84. dev_printk(KERN_ERR, rc->cdev.dev, "RAID COMPONENT REMOVE\n");
  85. class_device_unregister(&rc->cdev);
  86. }
  87. dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
  88. kfree(rd);
  89. return 0;
  90. }
  91. static DECLARE_TRANSPORT_CLASS(raid_class,
  92. "raid_devices",
  93. raid_setup,
  94. raid_remove,
  95. NULL);
  96. static struct {
  97. enum raid_state value;
  98. char *name;
  99. } raid_states[] = {
  100. { RAID_STATE_UNKNOWN, "unknown" },
  101. { RAID_STATE_ACTIVE, "active" },
  102. { RAID_STATE_DEGRADED, "degraded" },
  103. { RAID_STATE_RESYNCING, "resyncing" },
  104. { RAID_STATE_OFFLINE, "offline" },
  105. };
  106. static const char *raid_state_name(enum raid_state state)
  107. {
  108. int i;
  109. char *name = NULL;
  110. for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
  111. if (raid_states[i].value == state) {
  112. name = raid_states[i].name;
  113. break;
  114. }
  115. }
  116. return name;
  117. }
  118. static struct {
  119. enum raid_level value;
  120. char *name;
  121. } raid_levels[] = {
  122. { RAID_LEVEL_UNKNOWN, "unknown" },
  123. { RAID_LEVEL_LINEAR, "linear" },
  124. { RAID_LEVEL_0, "raid0" },
  125. { RAID_LEVEL_1, "raid1" },
  126. { RAID_LEVEL_3, "raid3" },
  127. { RAID_LEVEL_4, "raid4" },
  128. { RAID_LEVEL_5, "raid5" },
  129. { RAID_LEVEL_6, "raid6" },
  130. };
  131. static const char *raid_level_name(enum raid_level level)
  132. {
  133. int i;
  134. char *name = NULL;
  135. for (i = 0; i < sizeof(raid_levels)/sizeof(raid_levels[0]); i++) {
  136. if (raid_levels[i].value == level) {
  137. name = raid_levels[i].name;
  138. break;
  139. }
  140. }
  141. return name;
  142. }
  143. #define raid_attr_show_internal(attr, fmt, var, code) \
  144. static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
  145. { \
  146. struct raid_data *rd = class_get_devdata(cdev); \
  147. code \
  148. return snprintf(buf, 20, #fmt "\n", var); \
  149. }
  150. #define raid_attr_ro_states(attr, states, code) \
  151. raid_attr_show_internal(attr, %s, name, \
  152. const char *name; \
  153. code \
  154. name = raid_##states##_name(rd->attr); \
  155. ) \
  156. static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  157. #define raid_attr_ro_internal(attr, code) \
  158. raid_attr_show_internal(attr, %d, rd->attr, code) \
  159. static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  160. #define ATTR_CODE(attr) \
  161. struct raid_internal *i = class_device_to_raid_internal(cdev); \
  162. if (i->f->get_##attr) \
  163. i->f->get_##attr(cdev->dev);
  164. #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
  165. #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
  166. #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
  167. #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
  168. raid_attr_ro_state(level);
  169. raid_attr_ro_fn(resync);
  170. raid_attr_ro_state_fn(state);
  171. static void raid_component_release(struct class_device *cdev)
  172. {
  173. struct raid_component *rc = container_of(cdev, struct raid_component,
  174. cdev);
  175. dev_printk(KERN_ERR, rc->cdev.dev, "COMPONENT RELEASE\n");
  176. put_device(rc->cdev.dev);
  177. kfree(rc);
  178. }
  179. void raid_component_add(struct raid_template *r,struct device *raid_dev,
  180. struct device *component_dev)
  181. {
  182. struct class_device *cdev =
  183. attribute_container_find_class_device(&r->raid_attrs.ac,
  184. raid_dev);
  185. struct raid_component *rc;
  186. struct raid_data *rd = class_get_devdata(cdev);
  187. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  188. if (!rc)
  189. return;
  190. INIT_LIST_HEAD(&rc->node);
  191. class_device_initialize(&rc->cdev);
  192. rc->cdev.release = raid_component_release;
  193. rc->cdev.dev = get_device(component_dev);
  194. rc->num = rd->component_count++;
  195. snprintf(rc->cdev.class_id, sizeof(rc->cdev.class_id),
  196. "component-%d", rc->num);
  197. list_add_tail(&rc->node, &rd->component_list);
  198. rc->cdev.parent = cdev;
  199. rc->cdev.class = &raid_class.class;
  200. class_device_add(&rc->cdev);
  201. }
  202. EXPORT_SYMBOL(raid_component_add);
  203. struct raid_template *
  204. raid_class_attach(struct raid_function_template *ft)
  205. {
  206. struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
  207. GFP_KERNEL);
  208. int count = 0;
  209. if (unlikely(!i))
  210. return NULL;
  211. i->f = ft;
  212. i->r.raid_attrs.ac.class = &raid_class.class;
  213. i->r.raid_attrs.ac.match = raid_match;
  214. i->r.raid_attrs.ac.attrs = &i->attrs[0];
  215. attribute_container_register(&i->r.raid_attrs.ac);
  216. i->attrs[count++] = &class_device_attr_level;
  217. i->attrs[count++] = &class_device_attr_resync;
  218. i->attrs[count++] = &class_device_attr_state;
  219. i->attrs[count] = NULL;
  220. BUG_ON(count > RAID_NUM_ATTRS);
  221. return &i->r;
  222. }
  223. EXPORT_SYMBOL(raid_class_attach);
  224. void
  225. raid_class_release(struct raid_template *r)
  226. {
  227. struct raid_internal *i = to_raid_internal(r);
  228. attribute_container_unregister(&i->r.raid_attrs.ac);
  229. kfree(i);
  230. }
  231. EXPORT_SYMBOL(raid_class_release);
  232. static __init int raid_init(void)
  233. {
  234. return transport_class_register(&raid_class);
  235. }
  236. static __exit void raid_exit(void)
  237. {
  238. transport_class_unregister(&raid_class);
  239. }
  240. MODULE_AUTHOR("James Bottomley");
  241. MODULE_DESCRIPTION("RAID device class");
  242. MODULE_LICENSE("GPL");
  243. module_init(raid_init);
  244. module_exit(raid_exit);