analysisOfLiquidLevelDataOperation.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Functions related to tagged command queuing
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/bio.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/slab.h>
  9. #include "blk.h"
  10. /**
  11. * blk_queue_find_tag - find a request by its tag and queue
  12. * @q: The request queue for the device
  13. * @tag: The tag of the request
  14. *
  15. * Notes:
  16. * Should be used when a device returns a tag and you want to match
  17. * it with a request.
  18. *
  19. * no locks need be held.
  20. **/
  21. struct request *blk_queue_find_tag(struct request_queue *q, int tag)
  22. {
  23. return blk_map_queue_find_tag(q->queue_tags, tag);
  24. }
  25. EXPORT_SYMBOL(blk_queue_find_tag);
  26. /**
  27. * __blk_free_tags - release a given set of tag maintenance info
  28. * @bqt: the tag map to free
  29. *
  30. * Tries to free the specified @bqt. Returns true if it was
  31. * actually freed and false if there are still references using it
  32. */
  33. static int __blk_free_tags(struct blk_queue_tag *bqt)
  34. {
  35. int retval;
  36. retval = atomic_dec_and_test(&bqt->refcnt);
  37. if (retval) {
  38. BUG_ON(find_first_bit(bqt->tag_map, bqt->max_depth) <
  39. bqt->max_depth);
  40. kfree(bqt->tag_index);
  41. bqt->tag_index = NULL;
  42. kfree(bqt->tag_map);
  43. bqt->tag_map = NULL;
  44. kfree(bqt);
  45. }
  46. return retval;
  47. }
  48. /**
  49. * __blk_queue_free_tags - release tag maintenance info
  50. * @q: the request queue for the device
  51. *
  52. * Notes:
  53. * blk_cleanup_queue() will take care of calling this function, if tagging
  54. * has been used. So there's no need to call this directly.
  55. **/
  56. void __blk_queue_free_tags(struct request_queue *q)
  57. {
  58. struct blk_queue_tag *bqt = q->queue_tags;
  59. if (!bqt)
  60. return;
  61. __blk_free_tags(bqt);
  62. q->queue_tags = NULL;
  63. queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
  64. }
  65. /**
  66. * blk_free_tags - release a given set of tag maintenance info
  67. * @bqt: the tag map to free
  68. *
  69. * For externally managed @bqt frees the map. Callers of this
  70. * function must guarantee to have released all the queues that
  71. * might have been using this tag map.
  72. */
  73. void blk_free_tags(struct blk_queue_tag *bqt)
  74. {
  75. if (unlikely(!__blk_free_tags(bqt)))
  76. BUG();
  77. }
  78. EXPORT_SYMBOL(blk_free_tags);
  79. /**
  80. * blk_queue_free_tags - release tag maintenance info
  81. * @q: the request queue for the device
  82. *
  83. * Notes:
  84. * This is used to disable tagged queuing to a device, yet leave
  85. * queue in function.
  86. **/
  87. void blk_queue_free_tags(struct request_queue *q)
  88. {
  89. queue_flag_clear_unlocked(QUEUE_FLAG_QUEUED, q);
  90. }
  91. EXPORT_SYMBOL(blk_queue_free_tags);
  92. static int
  93. init_tag_map(struct request_queue *q, struct blk_queue_tag *tags, int depth)
  94. {
  95. struct request **tag_index;
  96. unsigned long *tag_map;
  97. int nr_ulongs;
  98. if (q && depth > q->nr_requests * 2) {
  99. depth = q->nr_requests * 2;
  100. printk(KERN_ERR "%s: adjusted depth to %d\n",
  101. __func__, depth);
  102. }
  103. tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
  104. if (!tag_index)
  105. goto fail;
  106. nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
  107. tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
  108. if (!tag_map)
  109. goto fail;
  110. tags->real_max_depth = depth;
  111. tags->max_depth = depth;
  112. tags->tag_index = tag_index;
  113. tags->tag_map = tag_map;
  114. return 0;
  115. fail:
  116. kfree(tag_index);
  117. return -ENOMEM;
  118. }
  119. static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
  120. int depth)
  121. {
  122. struct blk_queue_tag *tags;
  123. tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
  124. if (!tags)
  125. goto fail;
  126. if (init_tag_map(q, tags, depth))
  127. goto fail;
  128. atomic_set(&tags->refcnt, 1);
  129. return tags;
  130. fail:
  131. kfree(tags);
  132. return NULL;
  133. }
  134. /**
  135. * blk_init_tags - initialize the tag info for an external tag map
  136. * @depth: the maximum queue depth supported
  137. **/
  138. struct blk_queue_tag *blk_init_tags(int depth)
  139. {
  140. return __blk_queue_init_tags(NULL, depth);
  141. }
  142. EXPORT_SYMBOL(blk_init_tags);
  143. /**
  144. * blk_queue_init_tags - initialize the queue tag info
  145. * @q: the request queue for the device
  146. * @depth: the maximum queue depth supported
  147. * @tags: the tag to use
  148. *
  149. * Queue lock must be held here if the function is called to resize an
  150. * existing map.
  151. **/
  152. int blk_queue_init_tags(struct request_queue *q, int depth,
  153. struct blk_queue_tag *tags)
  154. {
  155. int rc;
  156. BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
  157. if (!tags && !q->queue_tags) {
  158. tags = __blk_queue_init_tags(q, depth);
  159. if (!tags)
  160. return -ENOMEM;
  161. } else if (q->queue_tags) {
  162. rc = blk_queue_resize_tags(q, depth);
  163. if (rc)
  164. return rc;
  165. queue_flag_set(QUEUE_FLAG_QUEUED, q);
  166. return 0;
  167. } else
  168. atomic_inc(&tags->refcnt);
  169. /*
  170. * assign it, all done
  171. */
  172. q->queue_tags = tags;
  173. queue_flag_set_unlocked(QUEUE_FLAG_QUEUED, q);
  174. INIT_LIST_HEAD(&q->tag_busy_list);
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(blk_queue_init_tags);
  178. /**
  179. * blk_queue_resize_tags - change the queueing depth
  180. * @q: the request queue for the device
  181. * @new_depth: the new max command queueing depth
  182. *
  183. * Notes:
  184. * Must be called with the queue lock held.
  185. **/
  186. int blk_queue_resize_tags(struct request_queue *q, int new_depth)
  187. {
  188. struct blk_queue_tag *bqt = q->queue_tags;
  189. struct request **tag_index;
  190. unsigned long *tag_map;
  191. int max_depth, nr_ulongs;
  192. if (!bqt)
  193. return -ENXIO;
  194. /*
  195. * if we already have large enough real_max_depth. just
  196. * adjust max_depth. *NOTE* as requests with tag value
  197. * between new_depth and real_max_depth can be in-flight, tag
  198. * map can not be shrunk blindly here.
  199. */
  200. if (new_depth <= bqt->real_max_depth) {
  201. bqt->max_depth = new_depth;
  202. return 0;
  203. }
  204. /*
  205. * Currently cannot replace a shared tag map with a new
  206. * one, so error out if this is the case
  207. */
  208. if (atomic_read(&bqt->refcnt) != 1)