raid_class.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 (IS_ENABLED(CONFIG_SCSI) && scsi_is_sdev_device(dev)) {
  57. struct scsi_device *sdev = to_scsi_device(dev);
  58. if (i->f->cookie != sdev->host->hostt)
  59. return 0;
  60. return i->f->is_raid(dev);
  61. }
  62. /* FIXME: look at other subsystems too */
  63. return 0;
  64. }
  65. static int raid_setup(struct transport_container *tc, struct device *dev,
  66. struct device *cdev)
  67. {
  68. struct raid_data *rd;
  69. BUG_ON(dev_get_drvdata(cdev));
  70. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  71. if (!rd)
  72. return -ENOMEM;
  73. INIT_LIST_HEAD(&rd->component_list);
  74. dev_set_drvdata(cdev, rd);
  75. return 0;
  76. }
  77. static int raid_remove(struct transport_container *tc, struct device *dev,
  78. struct device *cdev)
  79. {
  80. struct raid_data *rd = dev_get_drvdata(cdev);
  81. struct raid_component *rc, *next;
  82. dev_printk(KERN_ERR, dev, "RAID REMOVE\n");
  83. dev_set_drvdata(cdev, NULL);
  84. list_for_each_entry_safe(rc, next, &rd->component_list, node) {
  85. list_del(&rc->node);
  86. dev_printk(KERN_ERR, rc->dev.parent, "RAID COMPONENT REMOVE\n");
  87. device_unregister(&rc->dev);
  88. }
  89. dev_printk(KERN_ERR, dev, "RAID REMOVE DONE\n");
  90. kfree(rd);
  91. return 0;
  92. }
  93. static DECLARE_TRANSPORT_CLASS(raid_class,
  94. "raid_devices",
  95. raid_setup,
  96. raid_remove,
  97. NULL);
  98. static const struct {
  99. enum raid_state value;
  100. char *name;
  101. } raid_states[] = {
  102. { RAID_STATE_UNKNOWN, "unknown" },
  103. { RAID_STATE_ACTIVE, "active" },
  104. { RAID_STATE_DEGRADED, "degraded" },
  105. { RAID_STATE_RESYNCING, "resyncing" },
  106. { RAID_STATE_OFFLINE, "offline" },
  107. };
  108. static const char *raid_state_name(enum raid_state state)
  109. {
  110. int i;
  111. char *name = NULL;
  112. for (i = 0; i < ARRAY_SIZE(raid_states); i++) {
  113. if (raid_states[i].value == state) {
  114. name = raid_states[i].name;
  115. break;
  116. }
  117. }
  118. return name;
  119. }
  120. static struct {
  121. enum raid_level value;
  122. char *name;
  123. } raid_levels[] = {
  124. { RAID_LEVEL_UNKNOWN, "unknown" },
  125. { RAID_LEVEL_LINEAR, "linear" },
  126. { RAID_LEVEL_0, "raid0" },
  127. { RAID_LEVEL_1, "raid1" },
  128. { RAID_LEVEL_10, "raid10" },
  129. { RAID_LEVEL_1E, "raid1e" },
  130. { RAID_LEVEL_3, "raid3" },
  131. { RAID_LEVEL_4, "raid4" },
  132. { RAID_LEVEL_5, "raid5" },
  133. { RAID_LEVEL_50, "raid50" },
  134. { RAID_LEVEL_6, "raid6" },
  135. { RAID_LEVEL_JBOD, "jbod" },
  136. };
  137. static const char *raid_level_name(enum raid_level level)
  138. {
  139. int i;
  140. char *name = NULL;
  141. for (i = 0; i < ARRAY_SIZE(raid_levels); i++) {
  142. if (raid_levels[i].value == level) {
  143. name = raid_levels[i].name;
  144. break;
  145. }
  146. }
  147. return name;
  148. }
  149. #define raid_attr_show_internal(attr, fmt, var, code) \
  150. static ssize_t raid_show_##attr(struct device *dev, \
  151. struct device_attribute *attr, \
  152. char *buf) \
  153. { \
  154. struct raid_data *rd = dev_get_drvdata(dev); \
  155. code \
  156. return snprintf(buf, 20, #fmt "\n", var); \
  157. }
  158. #define raid_attr_ro_states(attr, states, code) \
  159. raid_attr_show_internal(attr, %s, name, \
  160. const char *name; \
  161. code \
  162. name = raid_##states##_name(rd->attr); \
  163. ) \
  164. static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  165. #define raid_attr_ro_internal(attr, code) \
  166. raid_attr_show_internal(attr, %d, rd->attr, code) \
  167. static DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  168. #define ATTR_CODE(attr) \
  169. struct raid_internal *i = device_to_raid_internal(dev); \
  170. if (i->f->get_##attr) \
  171. i->f->get_##attr(dev->parent);
  172. #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
  173. #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
  174. #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, )
  175. #define raid_attr_ro_state_fn(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
  176. raid_attr_ro_state(level);
  177. raid_attr_ro_fn(resync);
  178. raid_attr_ro_state_fn(state);
  179. static void raid_component_release(struct device *dev)
  180. {
  181. struct raid_component *rc =
  182. container_of(dev, struct raid_component, dev);
  183. dev_printk(KERN_ERR, rc->dev.parent, "COMPONENT RELEASE\n");
  184. put_device(rc->dev.parent);
  185. kfree(rc);
  186. }
  187. int raid_component_add(struct raid_template *r,struct device *raid_dev,
  188. struct device *component_dev)
  189. {
  190. struct device *cdev =
  191. attribute_container_find_class_device(&r->raid_attrs.ac,
  192. raid_dev);
  193. struct raid_component *rc;
  194. struct raid_data *rd = dev_get_drvdata(cdev);
  195. int err;
  196. rc = kzalloc(sizeof(*rc), GFP_KERNEL);
  197. if (!rc)
  198. return -ENOMEM;
  199. INIT_LIST_HEAD(&rc->node);
  200. device_initialize(&rc->dev);
  201. rc->dev.release = raid_component_release;
  202. rc->dev.parent = get_device(component_dev);
  203. rc->num = rd->component_count++;
  204. dev_set_name(&rc->dev, "component-%d", rc->num);
  205. list_add_tail(&rc->node, &rd->component_list);
  206. rc->dev.class = &raid_class.class;
  207. err = device_add(&rc->dev);
  208. if (err)
  209. goto err_out;
  210. return 0;
  211. err_out:
  212. list_del(&rc->node);
  213. rd->component_count--;
  214. put_device(component_dev);
  215. kfree(rc);
  216. return err;
  217. }
  218. EXPORT_SYMBOL(raid_component_add);
  219. struct raid_template *
  220. raid_class_attach(struct raid_function_template *ft)
  221. {
  222. struct raid_internal *i = kzalloc(sizeof(struct raid_internal),
  223. GFP_KERNEL);
  224. int count = 0;
  225. if (unlikely(!i))
  226. return NULL;
  227. i->f = ft;
  228. i->r.raid_attrs.ac.class = &raid_class.class;
  229. i->r.raid_attrs.ac.match = raid_match;
  230. i->r.raid_attrs.ac.attrs = &i->attrs[0];
  231. attribute_container_register(&i->r.raid_attrs.ac);
  232. i->attrs[count++] = &dev_attr_level;
  233. i->attrs[count++] = &dev_attr_resync;
  234. i->attrs[count++] = &dev_attr_state;
  235. i->attrs[count] = NULL;
  236. BUG_ON(count > RAID_NUM_ATTRS);
  237. return &i->r;
  238. }
  239. EXPORT_SYMBOL(raid_class_attach);
  240. void
  241. raid_class_release(struct raid_template *r)
  242. {
  243. struct raid_internal *i = to_raid_internal(r);
  244. BUG_ON(attribute_container_unregister(&i->r.raid_attrs.ac));
  245. kfree(i);
  246. }
  247. EXPORT_SYMBOL(raid_class_release);
  248. static __init int raid_init(void)
  249. {
  250. return transport_class_register(&raid_class);
  251. }
  252. static __exit void raid_exit(void)
  253. {
  254. transport_class_unregister(&raid_class);
  255. }
  256. MODULE_AUTHOR("James Bottomley");
  257. MODULE_DESCRIPTION("RAID device class");
  258. MODULE_LICENSE("GPL");
  259. module_init(raid_init);
  260. module_exit(raid_exit);