blk-barrier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <linux/gfp.h>
  9. #include "blk.h"
  10. /**
  11. * blk_queue_ordered - does this queue support ordered writes
  12. * @q: the request queue
  13. * @ordered: one of QUEUE_ORDERED_*
  14. * @prepare_flush_fn: rq setup helper for cache flush ordered writes
  15. *
  16. * Description:
  17. * For journalled file systems, doing ordered writes on a commit
  18. * block instead of explicitly doing wait_on_buffer (which is bad
  19. * for performance) can be a big win. Block drivers supporting this
  20. * feature should call this function and indicate so.
  21. *
  22. **/
  23. int blk_queue_ordered(struct request_queue *q, unsigned ordered,
  24. prepare_flush_fn *prepare_flush_fn)
  25. {
  26. if (!prepare_flush_fn && (ordered & (QUEUE_ORDERED_DO_PREFLUSH |
  27. QUEUE_ORDERED_DO_POSTFLUSH))) {
  28. printk(KERN_ERR "%s: prepare_flush_fn required\n", __func__);
  29. return -EINVAL;
  30. }
  31. if (ordered != QUEUE_ORDERED_NONE &&
  32. ordered != QUEUE_ORDERED_DRAIN &&
  33. ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
  34. ordered != QUEUE_ORDERED_DRAIN_FUA &&
  35. ordered != QUEUE_ORDERED_TAG &&
  36. ordered != QUEUE_ORDERED_TAG_FLUSH &&
  37. ordered != QUEUE_ORDERED_TAG_FUA) {
  38. printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
  39. return -EINVAL;
  40. }
  41. q->ordered = ordered;
  42. q->next_ordered = ordered;
  43. q->prepare_flush_fn = prepare_flush_fn;
  44. return 0;
  45. }
  46. EXPORT_SYMBOL(blk_queue_ordered);
  47. /*
  48. * Cache flushing for ordered writes handling
  49. */
  50. unsigned blk_ordered_cur_seq(struct request_queue *q)
  51. {
  52. if (!q->ordseq)
  53. return 0;
  54. return 1 << ffz(q->ordseq);
  55. }
  56. unsigned blk_ordered_req_seq(struct request *rq)
  57. {
  58. struct request_queue *q = rq->q;
  59. BUG_ON(q->ordseq == 0);
  60. if (rq == &q->pre_flush_rq)
  61. return QUEUE_ORDSEQ_PREFLUSH;
  62. if (rq == &q->bar_rq)
  63. return QUEUE_ORDSEQ_BAR;
  64. if (rq == &q->post_flush_rq)
  65. return QUEUE_ORDSEQ_POSTFLUSH;
  66. /*
  67. * !fs requests don't need to follow barrier ordering. Always
  68. * put them at the front. This fixes the following deadlock.
  69. *
  70. * http://thread.gmane.org/gmane.linux.kernel/537473
  71. */
  72. if (!blk_fs_request(rq))
  73. return QUEUE_ORDSEQ_DRAIN;
  74. if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
  75. (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
  76. return QUEUE_ORDSEQ_DRAIN;
  77. else
  78. return QUEUE_ORDSEQ_DONE;
  79. }
  80. bool blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
  81. {
  82. struct request *rq;
  83. if (error && !q->orderr)
  84. q->orderr = error;
  85. BUG_ON(q->ordseq & seq);
  86. q->ordseq |= seq;
  87. if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
  88. return false;
  89. /*
  90. * Okay, sequence complete.
  91. */
  92. q->ordseq = 0;
  93. rq = q->orig_bar_rq;
  94. __blk_end_request_all(rq, q->orderr);
  95. return true;
  96. }
  97. static void pre_flush_end_io(struct request *rq, int error)
  98. {
  99. elv_completed_request(rq->q, rq);
  100. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
  101. }
  102. static void bar_end_io(struct request *rq, int error)
  103. {
  104. elv_completed_request(rq->q, rq);
  105. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
  106. }
  107. static void post_flush_end_io(struct request *rq, int error)
  108. {
  109. elv_completed_request(rq->q, rq);
  110. blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
  111. }
  112. static void queue_flush(struct request_queue *q, unsigned which)
  113. {
  114. struct request *rq;
  115. rq_end_io_fn *end_io;
  116. if (which == QUEUE_ORDERED_DO_PREFLUSH) {
  117. rq = &q->pre_flush_rq;
  118. end_io = pre_flush_end_io;
  119. } else {
  120. rq = &q->post_flush_rq;
  121. end_io = post_flush_end_io;
  122. }
  123. blk_rq_init(q, rq);
  124. rq->cmd_flags = REQ_HARDBARRIER;
  125. rq->rq_disk = q->bar_rq.rq_disk;
  126. rq->end_io = end_io;
  127. q->prepare_flush_fn(q, rq);
  128. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  129. }
  130. static inline bool start_ordered(struct request_queue *q, struct request **rqp)
  131. {
  132. struct request *rq = *rqp;
  133. unsigned skip = 0;
  134. q->orderr = 0;
  135. q->ordered = q->next_ordered;
  136. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  137. /*
  138. * For an empty barrier, there's no actual BAR request, which
  139. * in turn makes POSTFLUSH unnecessary. Mask them off.
  140. */
  141. if (!blk_rq_sectors(rq)) {
  142. q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
  143. QUEUE_ORDERED_DO_POSTFLUSH);
  144. /*
  145. * Empty barrier on a write-through device w/ ordered
  146. * tag has no command to issue and without any command
  147. * to issue, ordering by tag can't be used. Drain
  148. * instead.
  149. */
  150. if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
  151. !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
  152. q->ordered &= ~QUEUE_ORDERED_BY_TAG;
  153. q->ordered |= QUEUE_ORDERED_BY_DRAIN;
  154. }
  155. }
  156. /* stash away the original request */
  157. blk_dequeue_request(rq);
  158. q->orig_bar_rq = rq;
  159. rq = NULL;
  160. /*
  161. * Queue ordered sequence. As we stack them at the head, we
  162. * need to queue in reverse order. Note that we rely on that
  163. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  164. * request gets inbetween ordered sequence.
  165. */
  166. if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
  167. queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
  168. rq = &q->post_flush_rq;
  169. } else
  170. skip |= QUEUE_ORDSEQ_POSTFLUSH;
  171. if (q->ordered & QUEUE_ORDERED_DO_BAR) {
  172. rq = &q->bar_rq;
  173. /* initialize proxy request and queue it */
  174. blk_rq_init(q, rq);
  175. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  176. rq->cmd_flags |= REQ_RW;
  177. if (q->ordered & QUEUE_ORDERED_DO_FUA)
  178. rq->cmd_flags |= REQ_FUA;
  179. init_request_from_bio(rq, q->orig_bar_rq->bio);
  180. rq->end_io = bar_end_io;
  181. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  182. } else
  183. skip |= QUEUE_ORDSEQ_BAR;
  184. if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
  185. queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
  186. rq = &q->pre_flush_rq;
  187. } else
  188. skip |= QUEUE_ORDSEQ_PREFLUSH;
  189. if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
  190. rq = NULL;
  191. else
  192. skip |= QUEUE_ORDSEQ_DRAIN;
  193. *rqp = rq;
  194. /*
  195. * Complete skipped sequences. If whole sequence is complete,
  196. * return false to tell elevator that this request is gone.
  197. */
  198. return !blk_ordered_complete_seq(q, skip, 0);
  199. }
  200. bool blk_do_ordered(struct request_queue *q, struct request **rqp)
  201. {
  202. struct request *rq = *rqp;
  203. const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
  204. if (!q->ordseq) {
  205. if (!is_barrier)
  206. return true;
  207. if (q->next_ordered != QUEUE_ORDERED_NONE)
  208. return start_ordered(q, rqp);
  209. else {
  210. /*
  211. * Queue ordering not supported. Terminate
  212. * with prejudice.
  213. */
  214. blk_dequeue_request(rq);
  215. __blk_end_request_all(rq, -EOPNOTSUPP);
  216. *rqp = NULL;
  217. return false;
  218. }
  219. }
  220. /*
  221. * Ordered sequence in progress
  222. */
  223. /* Special requests are not subject to ordering rules. */
  224. if (!blk_fs_request(rq) &&
  225. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  226. return true;
  227. if (q->ordered & QUEUE_ORDERED_BY_TAG) {
  228. /* Ordered by tag. Blocking the next barrier is enough. */
  229. if (is_barrier && rq != &q->bar_rq)
  230. *rqp = NULL;
  231. } else {
  232. /* Ordered by draining. Wait for turn. */
  233. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  234. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  235. *rqp = NULL;
  236. }
  237. return true;
  238. }
  239. static void bio_end_empty_barrier(struct bio *bio, int err)
  240. {
  241. if (err) {
  242. if (err == -EOPNOTSUPP)
  243. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  244. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  245. }
  246. complete(bio->bi_private);
  247. }
  248. /**
  249. * blkdev_issue_flush - queue a flush
  250. * @bdev: blockdev to issue flush for
  251. * @gfp_mask: memory allocation flags (for bio_alloc)
  252. * @error_sector: error sector
  253. * @flags: BLKDEV_IFL_* flags to control behaviour
  254. *
  255. * Description:
  256. * Issue a flush for the block device in question. Caller can supply
  257. * room for storing the error offset in case of a flush error, if they
  258. * wish to.
  259. */
  260. int blkdev_issue_flush(struct block_device *bdev, gfp_t gfp_mask,
  261. sector_t *error_sector, unsigned long flags)
  262. {
  263. DECLARE_COMPLETION_ONSTACK(wait);
  264. struct request_queue *q;
  265. struct bio *bio;
  266. int ret = 0;
  267. if (bdev->bd_disk == NULL)
  268. return -ENXIO;
  269. q = bdev_get_queue(bdev);
  270. if (!q)
  271. return -ENXIO;
  272. bio = bio_alloc(gfp_mask, 0);
  273. bio->bi_end_io = bio_end_empty_barrier;
  274. bio->bi_private = &wait;
  275. bio->bi_bdev = bdev;
  276. submit_bio(WRITE_BARRIER, bio);
  277. wait_for_completion(&wait);
  278. /*
  279. * The driver must store the error location in ->bi_sector, if
  280. * it supports it. For non-stacked drivers, this should be copied
  281. * from blk_rq_pos(rq).
  282. */
  283. if (error_sector)
  284. *error_sector = bio->bi_sector;
  285. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  286. ret = -EOPNOTSUPP;
  287. else if (!bio_flagged(bio, BIO_UPTODATE))
  288. ret = -EIO;
  289. bio_put(bio);
  290. return ret;
  291. }
  292. EXPORT_SYMBOL(blkdev_issue_flush);
  293. static void blkdev_discard_end_io(struct bio *bio, int err)
  294. {
  295. if (err) {
  296. if (err == -EOPNOTSUPP)
  297. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  298. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  299. }
  300. if (bio->bi_private)
  301. complete(bio->bi_private);
  302. __free_page(bio_page(bio));
  303. bio_put(bio);
  304. }
  305. /**
  306. * blkdev_issue_discard - queue a discard
  307. * @bdev: blockdev to issue discard for
  308. * @sector: start sector
  309. * @nr_sects: number of sectors to discard
  310. * @gfp_mask: memory allocation flags (for bio_alloc)
  311. * @flags: BLKDEV_IFL_* flags to control behaviour
  312. *
  313. * Description:
  314. * Issue a discard request for the sectors in question.
  315. */
  316. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  317. sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
  318. {
  319. DECLARE_COMPLETION_ONSTACK(wait);
  320. struct request_queue *q = bdev_get_queue(bdev);
  321. int type = flags & BLKDEV_IFL_BARRIER ?
  322. DISCARD_BARRIER : DISCARD_NOBARRIER;
  323. struct bio *bio;
  324. struct page *page;
  325. int ret = 0;
  326. if (!q)
  327. return -ENXIO;
  328. if (!blk_queue_discard(q))
  329. return -EOPNOTSUPP;
  330. while (nr_sects && !ret) {
  331. unsigned int sector_size = q->limits.logical_block_size;
  332. unsigned int max_discard_sectors =
  333. min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  334. bio = bio_alloc(gfp_mask, 1);
  335. if (!bio)
  336. goto out;
  337. bio->bi_sector = sector;
  338. bio->bi_end_io = blkdev_discard_end_io;
  339. bio->bi_bdev = bdev;
  340. if (flags & BLKDEV_IFL_WAIT)
  341. bio->bi_private = &wait;
  342. /*
  343. * Add a zeroed one-sector payload as that's what
  344. * our current implementations need. If we'll ever need
  345. * more the interface will need revisiting.
  346. */
  347. page = alloc_page(gfp_mask | __GFP_ZERO);
  348. if (!page)
  349. goto out_free_bio;
  350. if (bio_add_pc_page(q, bio, page, sector_size, 0) < sector_size)
  351. goto out_free_page;
  352. /*
  353. * And override the bio size - the way discard works we
  354. * touch many more blocks on disk than the actual payload
  355. * length.
  356. */
  357. if (nr_sects > max_discard_sectors) {
  358. bio->bi_size = max_discard_sectors << 9;
  359. nr_sects -= max_discard_sectors;
  360. sector += max_discard_sectors;
  361. } else {
  362. bio->bi_size = nr_sects << 9;
  363. nr_sects = 0;
  364. }
  365. bio_get(bio);
  366. submit_bio(type, bio);
  367. if (flags & BLKDEV_IFL_WAIT)
  368. wait_for_completion(&wait);
  369. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  370. ret = -EOPNOTSUPP;
  371. else if (!bio_flagged(bio, BIO_UPTODATE))
  372. ret = -EIO;
  373. bio_put(bio);
  374. }
  375. return ret;
  376. out_free_page:
  377. __free_page(page);
  378. out_free_bio:
  379. bio_put(bio);
  380. out:
  381. return -ENOMEM;
  382. }
  383. EXPORT_SYMBOL(blkdev_issue_discard);