raid_class.c 6.0 KB

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