dm-hw-handler.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. BUG_ON(hwhi->use < 0);
  66. out:
  67. up_read(&_hwh_lock);
  68. }
  69. static struct hwh_internal *_alloc_hw_handler(struct hw_handler_type *hwht)
  70. {
  71. struct hwh_internal *hwhi = kzalloc(sizeof(*hwhi), GFP_KERNEL);
  72. if (hwhi)
  73. hwhi->hwht = *hwht;
  74. return hwhi;
  75. }
  76. int dm_register_hw_handler(struct hw_handler_type *hwht)
  77. {
  78. int r = 0;
  79. struct hwh_internal *hwhi = _alloc_hw_handler(hwht);
  80. if (!hwhi)
  81. return -ENOMEM;
  82. down_write(&_hwh_lock);
  83. if (__find_hw_handler_type(hwht->name)) {
  84. kfree(hwhi);
  85. r = -EEXIST;
  86. } else
  87. list_add(&hwhi->list, &_hw_handlers);
  88. up_write(&_hwh_lock);
  89. return r;
  90. }
  91. int dm_unregister_hw_handler(struct hw_handler_type *hwht)
  92. {
  93. struct hwh_internal *hwhi;
  94. down_write(&_hwh_lock);
  95. hwhi = __find_hw_handler_type(hwht->name);
  96. if (!hwhi) {
  97. up_write(&_hwh_lock);
  98. return -EINVAL;
  99. }
  100. if (hwhi->use) {
  101. up_write(&_hwh_lock);
  102. return -ETXTBSY;
  103. }
  104. list_del(&hwhi->list);
  105. up_write(&_hwh_lock);
  106. kfree(hwhi);
  107. return 0;
  108. }
  109. unsigned dm_scsi_err_handler(struct hw_handler *hwh, struct bio *bio)
  110. {
  111. #if 0
  112. int sense_key, asc, ascq;
  113. if (bio->bi_error & BIO_SENSE) {
  114. /* FIXME: This is just an initial guess. */
  115. /* key / asc / ascq */
  116. sense_key = (bio->bi_error >> 16) & 0xff;
  117. asc = (bio->bi_error >> 8) & 0xff;
  118. ascq = bio->bi_error & 0xff;
  119. switch (sense_key) {
  120. /* This block as a whole comes from the device.
  121. * So no point retrying on another path. */
  122. case 0x03: /* Medium error */
  123. case 0x05: /* Illegal request */
  124. case 0x07: /* Data protect */
  125. case 0x08: /* Blank check */
  126. case 0x0a: /* copy aborted */
  127. case 0x0c: /* obsolete - no clue ;-) */
  128. case 0x0d: /* volume overflow */
  129. case 0x0e: /* data miscompare */
  130. case 0x0f: /* reserved - no idea either. */
  131. return MP_ERROR_IO;
  132. /* For these errors it's unclear whether they
  133. * come from the device or the controller.
  134. * So just lets try a different path, and if
  135. * it eventually succeeds, user-space will clear
  136. * the paths again... */
  137. case 0x02: /* Not ready */
  138. case 0x04: /* Hardware error */
  139. case 0x09: /* vendor specific */
  140. case 0x0b: /* Aborted command */
  141. return MP_FAIL_PATH;
  142. case 0x06: /* Unit attention - might want to decode */
  143. if (asc == 0x04 && ascq == 0x01)
  144. /* "Unit in the process of
  145. * becoming ready" */
  146. return 0;
  147. return MP_FAIL_PATH;
  148. /* FIXME: For Unit Not Ready we may want
  149. * to have a generic pg activation
  150. * feature (START_UNIT). */
  151. /* Should these two ever end up in the
  152. * error path? I don't think so. */
  153. case 0x00: /* No sense */
  154. case 0x01: /* Recovered error */
  155. return 0;
  156. }
  157. }
  158. #endif
  159. /* We got no idea how to decode the other kinds of errors ->
  160. * assume generic error condition. */
  161. return MP_FAIL_PATH;
  162. }
  163. EXPORT_SYMBOL_GPL(dm_register_hw_handler);
  164. EXPORT_SYMBOL_GPL(dm_unregister_hw_handler);
  165. EXPORT_SYMBOL_GPL(dm_scsi_err_handler);