blk-barrier.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Functions related to barrier IO handling
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/bio.h>
  7. #include <linux/blkdev.h>
  8. #include "blk.h"
  9. /**
  10. * blk_queue_ordered - does this queue support ordered writes
  11. * @q: the request queue
  12. * @ordered: one of QUEUE_ORDERED_*
  13. * @prepare_flush_fn: rq setup helper for cache flush ordered writes
  14. *
  15. * Description:
  16. * For journalled file systems, doing ordered writes on a commit
  17. * block instead of explicitly doing wait_on_buffer (which is bad
  18. * for performance) can be a big win. Block drivers supporting this
  19. * feature should call this function and indicate so.
  20. *
  21. **/
  22. int blk_queue_ordered(struct request_queue *q, unsigned ordered,
  23. prepare_flush_fn *prepare_flush_fn)
  24. {
  25. if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
  26. prepare_flush_fn == NULL) {
  27. printk(KERN_ERR "%s: prepare_flush_fn required\n", __func__);
  28. return -EINVAL;
  29. }
  30. if (ordered != QUEUE_ORDERED_NONE &&
  31. ordered != QUEUE_ORDERED_DRAIN &&
  32. ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
  33. ordered != QUEUE_ORDERED_DRAIN_FUA &&
  34. ordered != QUEUE_ORDERED_TAG &&
  35. ordered != QUEUE_ORDERED_TAG_FLUSH &&
  36. ordered != QUEUE_ORDERED_TAG_FUA) {
  37. printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
  38. return -EINVAL;
  39. }
  40. q->ordered = ordered;
  41. q->next_ordered = ordered;
  42. q->prepare_flush_fn = prepare_flush_fn;
  43. return 0;
  44. }
  45. EXPORT_SYMBOL(blk_queue_ordered);
  46. /*
  47. * Cache flushing for ordered writes handling
  48. */
  49. unsigned blk_ordered_cur_seq(struct request_queue *q)
  50. {
  51. if (!q->ordseq)
  52. return 0;
  53. return 1 << ffz(q->ordseq);
  54. }
  55. unsigned blk_ordered_req_seq(struct request *rq)
  56. {
  57. struct request_queue *q = rq->q;
  58. BUG_ON(q->ordseq == 0);
  59. if (rq == &q->pre_flush_rq)
  60. return QUEUE_ORDSEQ_PREFLUSH;
  61. if (rq == &q->bar_rq)
  62. return QUEUE_ORDSEQ_BAR;
  63. if (rq == &q->post_flush_rq)
  64. return QUEUE_ORDSEQ_POSTFLUSH;
  65. /*
  66. * !fs requests don't need to follow barrier ordering. Always
  67. * put them at the front. This fixes the following deadlock.
  68. *
  69. * http://thread.gmane.org/gmane.linux.kernel/537473
  70. */
  71. if (!blk_fs_request(rq))
  72. return QUEUE_ORDSEQ_DRAIN;
  73. if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
  74. (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
  75. return QUEUE_ORDSEQ_DRAIN;
  76. else
  77. return QUEUE_ORDSEQ_DONE;
  78. }
  79. void blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
  80. {
  81. struct request *rq;
  82. if (error && !q->orderr)
  83. q->orderr = error;
  84. BUG_ON(q->ordseq & seq);
  85. q->ordseq |= seq;
  86. if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
  87. return;
  88. /*
  89. * Okay, sequence complete.
  90. */
  91. q->ordseq = 0;
  92. rq = q->orig_bar_rq;
  93. if (__blk_end_request(rq, q->orderr, blk_rq_bytes(rq)))
  94. BUG();
  95. }
  96. static void pre_flush_end_io(struct request *rq, int error)
  97. {
  98. elv_completed_request(rq->q, rq);
  99. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
  100. }
  101. static void bar_end_io(struct request *rq, int error)
  102. {
  103. elv_completed_request(rq->q, rq);
  104. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
  105. }
  106. static void post_flush_end_io(struct request *rq, int error)
  107. {
  108. elv_completed_request(rq->q, rq);
  109. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
  110. }
  111. static void queue_flush(struct request_queue *q, unsigned which)
  112. {
  113. struct request *rq;
  114. rq_end_io_fn *end_io;
  115. if (which == QUEUE_ORDERED_PREFLUSH) {
  116. rq = &q->pre_flush_rq;
  117. end_io = pre_flush_end_io;
  118. } else {
  119. rq = &q->post_flush_rq;
  120. end_io = post_flush_end_io;
  121. }
  122. blk_rq_init(q, rq);
  123. rq->cmd_flags = REQ_HARDBARRIER;
  124. rq->rq_disk = q->bar_rq.rq_disk;
  125. rq->end_io = end_io;
  126. q->prepare_flush_fn(q, rq);
  127. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  128. }
  129. static inline struct request *start_ordered(struct request_queue *q,
  130. struct request *rq)
  131. {
  132. q->orderr = 0;
  133. q->ordered = q->next_ordered;
  134. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  135. /*
  136. * Prep proxy barrier request.
  137. */
  138. blkdev_dequeue_request(rq);
  139. q->orig_bar_rq = rq;
  140. rq = &q->bar_rq;
  141. blk_rq_init(q, rq);
  142. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  143. rq->cmd_flags |= REQ_RW;
  144. if (q->ordered & QUEUE_ORDERED_FUA)
  145. rq->cmd_flags |= REQ_FUA;
  146. init_request_from_bio(rq, q->orig_bar_rq->bio);
  147. rq->end_io = bar_end_io;
  148. /*
  149. * Queue ordered sequence. As we stack them at the head, we
  150. * need to queue in reverse order. Note that we rely on that
  151. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  152. * request gets inbetween ordered sequence. If this request is
  153. * an empty barrier, we don't need to do a postflush ever since
  154. * there will be no data written between the pre and post flush.
  155. * Hence a single flush will suffice.
  156. */
  157. if ((q->ordered & QUEUE_ORDERED_POSTFLUSH) && !blk_empty_barrier(rq))
  158. queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
  159. else
  160. q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
  161. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  162. if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
  163. queue_flush(q, QUEUE_ORDERED_PREFLUSH);
  164. rq = &q->pre_flush_rq;
  165. } else
  166. q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
  167. if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
  168. q->ordseq |= QUEUE_ORDSEQ_DRAIN;
  169. else
  170. rq = NULL;
  171. return rq;
  172. }
  173. int blk_do_ordered(struct request_queue *q, struct request **rqp)
  174. {
  175. struct request *rq = *rqp;
  176. const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
  177. if (!q->ordseq) {
  178. if (!is_barrier)
  179. return 1;
  180. if (q->next_ordered != QUEUE_ORDERED_NONE) {
  181. *rqp = start_ordered(q, rq);
  182. return 1;
  183. } else {
  184. /*
  185. * This can happen when the queue switches to
  186. * ORDERED_NONE while this request is on it.
  187. */
  188. blkdev_dequeue_request(rq);
  189. if (__blk_end_request(rq, -EOPNOTSUPP,
  190. blk_rq_bytes(rq)))
  191. BUG();
  192. *rqp = NULL;
  193. return 0;
  194. }
  195. }
  196. /*
  197. * Ordered sequence in progress
  198. */
  199. /* Special requests are not subject to ordering rules. */
  200. if (!blk_fs_request(rq) &&
  201. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  202. return 1;
  203. if (q->ordered & QUEUE_ORDERED_TAG) {
  204. /* Ordered by tag. Blocking the next barrier is enough. */
  205. if (is_barrier && rq != &q->bar_rq)
  206. *rqp = NULL;
  207. } else {
  208. /* Ordered by draining. Wait for turn. */
  209. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  210. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  211. *rqp = NULL;
  212. }
  213. return 1;
  214. }
  215. static void bio_end_empty_barrier(struct bio *bio, int err)
  216. {
  217. if (err) {
  218. if (err == -EOPNOTSUPP)
  219. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  220. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  221. }
  222. complete(bio->bi_private);
  223. }
  224. /**
  225. * blkdev_issue_flush - queue a flush
  226. * @bdev: blockdev to issue flush for
  227. * @error_sector: error sector
  228. *
  229. * Description:
  230. * Issue a flush for the block device in question. Caller can supply
  231. * room for storing the error offset in case of a flush error, if they
  232. * wish to. Caller must run wait_for_completion() on its own.
  233. */
  234. int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
  235. {
  236. DECLARE_COMPLETION_ONSTACK(wait);
  237. struct request_queue *q;
  238. struct bio *bio;
  239. int ret;
  240. if (bdev->bd_disk == NULL)
  241. return -ENXIO;
  242. q = bdev_get_queue(bdev);
  243. if (!q)
  244. return -ENXIO;
  245. bio = bio_alloc(GFP_KERNEL, 0);
  246. if (!bio)
  247. return -ENOMEM;
  248. bio->bi_end_io = bio_end_empty_barrier;
  249. bio->bi_private = &wait;
  250. bio->bi_bdev = bdev;
  251. submit_bio(WRITE_BARRIER, bio);
  252. wait_for_completion(&wait);
  253. /*
  254. * The driver must store the error location in ->bi_sector, if
  255. * it supports it. For non-stacked drivers, this should be copied
  256. * from rq->sector.
  257. */
  258. if (error_sector)
  259. *error_sector = bio->bi_sector;
  260. ret = 0;
  261. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  262. ret = -EOPNOTSUPP;
  263. else if (!bio_flagged(bio, BIO_UPTODATE))
  264. ret = -EIO;
  265. bio_put(bio);
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(blkdev_issue_flush);
  269. static void blkdev_discard_end_io(struct bio *bio, int err)
  270. {
  271. if (err) {
  272. if (err == -EOPNOTSUPP)
  273. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  274. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  275. }
  276. bio_put(bio);
  277. }
  278. /**
  279. * blkdev_issue_discard - queue a discard
  280. * @bdev: blockdev to issue discard for
  281. * @sector: start sector
  282. * @nr_sects: number of sectors to discard
  283. * @gfp_mask: memory allocation flags (for bio_alloc)
  284. *
  285. * Description:
  286. * Issue a discard request for the sectors in question. Does not wait.
  287. */
  288. int blkdev_issue_discard(struct block_device *bdev,
  289. sector_t sector, sector_t nr_sects, gfp_t gfp_mask)
  290. {
  291. struct request_queue *q;
  292. struct bio *bio;
  293. int ret = 0;
  294. if (bdev->bd_disk == NULL)
  295. return -ENXIO;
  296. q = bdev_get_queue(bdev);
  297. if (!q)
  298. return -ENXIO;
  299. if (!q->prepare_discard_fn)
  300. return -EOPNOTSUPP;
  301. while (nr_sects && !ret) {
  302. bio = bio_alloc(gfp_mask, 0);
  303. if (!bio)
  304. return -ENOMEM;
  305. bio->bi_end_io = blkdev_discard_end_io;
  306. bio->bi_bdev = bdev;
  307. bio->bi_sector = sector;
  308. if (nr_sects > q->max_hw_sectors) {
  309. bio->bi_size = q->max_hw_sectors << 9;
  310. nr_sects -= q->max_hw_sectors;
  311. sector += q->max_hw_sectors;
  312. } else {
  313. bio->bi_size = nr_sects << 9;
  314. nr_sects = 0;
  315. }
  316. bio_get(bio);
  317. submit_bio(DISCARD_BARRIER, bio);
  318. /* Check if it failed immediately */
  319. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  320. ret = -EOPNOTSUPP;
  321. else if (!bio_flagged(bio, BIO_UPTODATE))
  322. ret = -EIO;
  323. bio_put(bio);
  324. }
  325. return ret;
  326. }
  327. EXPORT_SYMBOL(blkdev_issue_discard);