raid_class.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * RAID Attributes
  3. */
  4. #include <linux/init.h>
  5. #include <linux/module.h>
  6. #include <linux/list.h>
  7. #include <linux/slab.h>
  8. #include <linux/string.h>
  9. #include <linux/raid_class.h>
  10. #include <scsi/scsi_device.h>
  11. #include <scsi/scsi_host.h>
  12. #define RAID_NUM_ATTRS 3
  13. struct raid_internal {
  14. struct raid_template r;
  15. struct raid_function_template *f;
  16. /* The actual attributes */
  17. struct class_device_attribute private_attrs[RAID_NUM_ATTRS];
  18. /* The array of null terminated pointers to attributes
  19. * needed by scsi_sysfs.c */
  20. struct class_device_attribute *attrs[RAID_NUM_ATTRS + 1];
  21. };
  22. struct raid_component {
  23. struct list_head node;
  24. struct device *dev;
  25. int num;
  26. };
  27. #define to_raid_internal(tmpl) container_of(tmpl, struct raid_internal, r)
  28. #define tc_to_raid_internal(tcont) ({ \
  29. struct raid_template *r = \
  30. container_of(tcont, struct raid_template, raid_attrs); \
  31. to_raid_internal(r); \
  32. })
  33. #define ac_to_raid_internal(acont) ({ \
  34. struct transport_container *tc = \
  35. container_of(acont, struct transport_container, ac); \
  36. tc_to_raid_internal(tc); \
  37. })
  38. #define class_device_to_raid_internal(cdev) ({ \
  39. struct attribute_container *ac = \
  40. attribute_container_classdev_to_container(cdev); \
  41. ac_to_raid_internal(ac); \
  42. })
  43. static int raid_match(struct attribute_container *cont, struct device *dev)
  44. {
  45. /* We have to look for every subsystem that could house
  46. * emulated RAID devices, so start with SCSI */
  47. struct raid_internal *i = ac_to_raid_internal(cont);
  48. if (scsi_is_sdev_device(dev)) {
  49. struct scsi_device *sdev = to_scsi_device(dev);
  50. if (i->f->cookie != sdev->host->hostt)
  51. return 0;
  52. return i->f->is_raid(dev);
  53. }
  54. /* FIXME: look at other subsystems too */
  55. return 0;
  56. }
  57. static int raid_setup(struct transport_container *tc, struct device *dev,
  58. struct class_device *cdev)
  59. {
  60. struct raid_data *rd;
  61. BUG_ON(class_get_devdata(cdev));
  62. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  63. if (!rd)
  64. return -ENOMEM;
  65. memset(rd, 0, sizeof(*rd));
  66. INIT_LIST_HEAD(&rd->component_list);
  67. class_set_devdata(cdev, rd);
  68. return 0;
  69. }
  70. static int raid_remove(struct transport_container *tc, struct device *dev,
  71. struct class_device *cdev)
  72. {
  73. struct raid_data *rd = class_get_devdata(cdev);
  74. struct raid_component *rc, *next;
  75. class_set_devdata(cdev, NULL);
  76. list_for_each_entry_safe(rc, next, &rd->component_list, node) {
  77. char buf[40];
  78. snprintf(buf, sizeof(buf), "component-%d", rc->num);
  79. list_del(&rc->node);
  80. sysfs_remove_link(&cdev->kobj, buf);
  81. kfree(rc);
  82. }
  83. kfree(class_get_devdata(cdev));
  84. return 0;
  85. }
  86. static DECLARE_TRANSPORT_CLASS(raid_class,
  87. "raid_devices",
  88. raid_setup,
  89. raid_remove,
  90. NULL);
  91. static struct {
  92. enum raid_state value;
  93. char *name;
  94. } raid_states[] = {
  95. { RAID_ACTIVE, "active" },
  96. { RAID_DEGRADED, "degraded" },
  97. { RAID_RESYNCING, "resyncing" },
  98. { RAID_OFFLINE, "offline" },
  99. };
  100. static const char *raid_state_name(enum raid_state state)
  101. {
  102. int i;
  103. char *name = NULL;
  104. for (i = 0; i < sizeof(raid_states)/sizeof(raid_states[0]); i++) {
  105. if (raid_states[i].value == state) {
  106. name = raid_states[i].name;
  107. break;
  108. }
  109. }
  110. return name;
  111. }
  112. #define raid_attr_show_internal(attr, fmt, var, code) \
  113. static ssize_t raid_show_##attr(struct class_device *cdev, char *buf) \
  114. { \
  115. struct raid_data *rd = class_get_devdata(cdev); \
  116. code \
  117. return snprintf(buf, 20, #fmt "\n", var); \
  118. }
  119. #define raid_attr_ro_states(attr, states, code) \
  120. raid_attr_show_internal(attr, %s, name, \
  121. const char *name; \
  122. code \
  123. name = raid_##states##_name(rd->attr); \
  124. ) \
  125. static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  126. #define raid_attr_ro_internal(attr, code) \
  127. raid_attr_show_internal(attr, %d, rd->attr, code) \
  128. static CLASS_DEVICE_ATTR(attr, S_IRUGO, raid_show_##attr, NULL)
  129. #define ATTR_CODE(attr) \
  130. struct raid_internal *i = class_device_to_raid_internal(cdev); \
  131. if (i->f->get_##attr) \
  132. i->f->get_##attr(cdev->dev);
  133. #define raid_attr_ro(attr) raid_attr_ro_internal(attr, )
  134. #define raid_attr_ro_fn(attr) raid_attr_ro_internal(attr, ATTR_CODE(attr))
  135. #define raid_attr_ro_state(attr) raid_attr_ro_states(attr, attr, ATTR_CODE(attr))
  136. raid_attr_ro(level);
  137. raid_attr_ro_fn(resync);
  138. raid_attr_ro_state(state);
  139. void raid_component_add(struct raid_template *r,struct device *raid_dev,
  140. struct device *component_dev)
  141. {
  142. struct class_device *cdev =
  143. attribute_container_find_class_device(&r->raid_attrs.ac,
  144. raid_dev);
  145. struct raid_component *rc;
  146. struct raid_data *rd = class_get_devdata(cdev);
  147. char buf[40];
  148. rc = kmalloc(sizeof(*rc), GFP_KERNEL);
  149. if (!rc)
  150. return;
  151. INIT_LIST_HEAD(&rc->node);
  152. rc->dev = component_dev;
  153. rc->num = rd->component_count++;
  154. snprintf(buf, sizeof(buf), "component-%d", rc->num);
  155. list_add_tail(&rc->node, &rd->component_list);
  156. sysfs_create_link(&cdev->kobj, &component_dev->kobj, buf);
  157. }
  158. EXPORT_SYMBOL(raid_component_add);
  159. struct raid_template *
  160. raid_class_attach(struct raid_function_template *ft)
  161. {
  162. struct raid_internal *i = kmalloc(sizeof(struct raid_internal),
  163. GFP_KERNEL);
  164. int count = 0;
  165. if (unlikely(!i))
  166. return NULL;
  167. memset(i, 0, sizeof(*i));
  168. i->f = ft;
  169. i->r.raid_attrs.ac.class = &raid_class.class;
  170. i->r.raid_attrs.ac.match = raid_match;
  171. i->r.raid_attrs.ac.attrs = &i->attrs[0];
  172. attribute_container_register(&i->r.raid_attrs.ac);
  173. i->attrs[count++] = &class_device_attr_level;
  174. i->attrs[count++] = &class_device_attr_resync;
  175. i->attrs[count++] = &class_device_attr_state;
  176. i->attrs[count] = NULL;
  177. BUG_ON(count > RAID_NUM_ATTRS);
  178. return &i->r;
  179. }
  180. EXPORT_SYMBOL(raid_class_attach);
  181. void
  182. raid_class_release(struct raid_template *r)
  183. {
  184. struct raid_internal *i = to_raid_internal(r);
  185. attribute_container_unregister(&i->r.raid_attrs.ac);
  186. kfree(i);
  187. }
  188. EXPORT_SYMBOL(raid_class_release);
  189. static __init int raid_init(void)
  190. {
  191. return transport_class_register(&raid_class);
  192. }
  193. static __exit void raid_exit(void)
  194. {
  195. transport_class_unregister(&raid_class);
  196. }
  197. MODULE_AUTHOR("James Bottomley");
  198. MODULE_DESCRIPTION("RAID device class");
  199. MODULE_LICENSE("GPL");
  200. module_init(raid_init);
  201. module_exit(raid_exit);