dm-hw-handler.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. *
  6. * Multipath hardware handler registration.
  7. */
  8. #include "dm.h"
  9. #include "dm-hw-handler.h"
  10. #include <linux/slab.h>
  11. struct hwh_internal {
  12. struct hw_handler_type hwht;
  13. struct list_head list;
  14. long use;
  15. };
  16. #define hwht_to_hwhi(__hwht) container_of((__hwht), struct hwh_internal, hwht)
  17. static LIST_HEAD(_hw_handlers);
  18. static DECLARE_RWSEM(_hwh_lock);
  19. static struct hwh_internal *__find_hw_handler_type(const char *name)
  20. {
  21. struct hwh_internal *hwhi;
  22. list_for_each_entry(hwhi, &_hw_handlers, list) {
  23. if (!strcmp(name, hwhi->hwht.name))
  24. return hwhi;
  25. }
  26. return NULL;
  27. }
  28. static struct hwh_internal *get_hw_handler(const char *name)
  29. {
  30. struct hwh_internal *hwhi;
  31. down_read(&_hwh_lock);
  32. hwhi = __find_hw_handler_type(name);
  33. if (hwhi) {
  34. if ((hwhi->use == 0) && !try_module_get(hwhi->hwht.module))
  35. hwhi = NULL;
  36. else
  37. hwhi->use++;
  38. }
  39. up_read(&_hwh_lock);
  40. return hwhi;
  41. }
  42. struct hw_handler_type *dm_get_hw_handler(const char *name)
  43. {
  44. struct hwh_internal *hwhi;
  45. if (!name)
  46. return NULL;
  47. hwhi = get_hw_handler(name);
  48. if (!hwhi) {
  49. request_module("dm-%s", name);
  50. hwhi = get_hw_handler(name);
  51. }
  52. return hwhi ? &hwhi->hwht : NULL;
  53. }
  54. void dm_put_hw_handler(struct hw_handler_type *hwht)
  55. {
  56. struct hwh_internal *hwhi;
  57. if (!hwht)
  58. return;
  59. down_read(&_hwh_lock);
  60. hwhi = __find_hw_handler_type(hwht->name);
  61. if (!hwhi)
  62. goto out;
  63. if (--hwhi->use == 0)
  64. module_put(hwhi->hwht.module);
  65. if (hwhi->use < 0)
  66. BUG();
  67. out:
  68. up_read(&_hwh_lock);
  69. }
  70. static struct hwh_internal *_alloc_hw_handler(struct hw_handler_type *hwht)
  71. {
  72. struct hwh_internal *hwhi = kmalloc(sizeof(*hwhi), GFP_KERNEL);
  73. if (hwhi) {
  74. memset(hwhi, 0, sizeof(*hwhi));
  75. hwhi->hwht = *hwht;
  76. }
  77. return hwhi;
  78. }
  79. int dm_register_hw_handler(struct hw_handler_type *hwht)
  80. {
  81. int r = 0;
  82. struct hwh_internal *hwhi = _alloc_hw_handler(hwht);
  83. if (!hwhi)
  84. return -ENOMEM;
  85. down_write(&_hwh_lock);
  86. if (__find_hw_handler_type(hwht->name)) {
  87. kfree(hwhi);
  88. r = -EEXIST;
  89. } else
  90. list_add(&hwhi->list, &_hw_handlers);
  91. up_write(&_hwh_lock);
  92. return r;
  93. }
  94. int dm_unregister_hw_handler(struct hw_handler_type *hwht)
  95. {
  96. struct hwh_internal *hwhi;
  97. down_write(&_hwh_lock);
  98. hwhi = __find_hw_handler_type(hwht->name);
  99. if (!hwhi) {
  100. up_write(&_hwh_lock);
  101. return -EINVAL;
  102. }
  103. if (hwhi->use) {
  104. up_write(&_hwh_lock);
  105. return -ETXTBSY;
  106. }
  107. list_del(&hwhi->list);
  108. up_write(&_hwh_lock);
  109. kfree(hwhi);
  110. return 0;
  111. }
  112. unsigned dm_scsi_err_handler(struct hw_handler *hwh, struct bio *bio)
  113. {
  114. #if 0
  115. int sense_key, asc, ascq;
  116. if (bio->bi_error & BIO_SENSE) {
  117. /* FIXME: This is just an initial guess. */
  118. /* key / asc / ascq */
  119. sense_key = (bio->bi_error >> 16) & 0xff;
  120. asc = (bio->bi_error >> 8) & 0xff;
  121. ascq = bio->bi_error & 0xff;
  122. switch (sense_key) {
  123. /* This block as a whole comes from the device.
  124. * So no point retrying on another path. */
  125. case 0x03: /* Medium error */
  126. case 0x05: /* Illegal request */
  127. case 0x07: /* Data protect */
  128. case 0x08: /* Blank check */
  129. case 0x0a: /* copy aborted */
  130. case 0x0c: /* obsolete - no clue ;-) */
  131. case 0x0d: /* volume overflow */
  132. case 0x0e: /* data miscompare */
  133. case 0x0f: /* reserved - no idea either. */
  134. return MP_ERROR_IO;
  135. /* For these errors it's unclear whether they
  136. * come from the device or the controller.
  137. * So just lets try a different path, and if
  138. * it eventually succeeds, user-space will clear
  139. * the paths again... */
  140. case 0x02: /* Not ready */
  141. case 0x04: /* Hardware error */
  142. case 0x09: /* vendor specific */
  143. case 0x0b: /* Aborted command */
  144. return MP_FAIL_PATH;
  145. case 0x06: /* Unit attention - might want to decode */
  146. if (asc == 0x04 && ascq == 0x01)
  147. /* "Unit in the process of
  148. * becoming ready" */
  149. return 0;
  150. return MP_FAIL_PATH;
  151. /* FIXME: For Unit Not Ready we may want
  152. * to have a generic pg activation
  153. * feature (START_UNIT). */
  154. /* Should these two ever end up in the
  155. * error path? I don't think so. */
  156. case 0x00: /* No sense */
  157. case 0x01: /* Recovered error */
  158. return 0;
  159. }
  160. }
  161. #endif
  162. /* We got no idea how to decode the other kinds of errors ->
  163. * assume generic error condition. */
  164. return MP_FAIL_PATH;
  165. }
  166. EXPORT_SYMBOL_GPL(dm_register_hw_handler);
  167. EXPORT_SYMBOL_GPL(dm_unregister_hw_handler);
  168. EXPORT_SYMBOL_GPL(dm_scsi_err_handler);