dm-hw-handler.c 4.3 KB

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