raid_class.c 6.5 KB

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