blk-barrier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 (!prepare_flush_fn && (ordered & (QUEUE_ORDERED_DO_PREFLUSH |
  26. QUEUE_ORDERED_DO_POSTFLUSH))) {
  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. bool 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 false;
  88. /*
  89. * Okay, sequence complete.
  90. */
  91. q->ordseq = 0;
  92. rq = q->orig_bar_rq;
  93. __blk_end_request_all(rq, q->orderr);
  94. return true;
  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_DO_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 bool start_ordered(struct request_queue *q, struct request **rqp)
  130. {
  131. struct request *rq = *rqp;
  132. unsigned skip = 0;
  133. q->orderr = 0;
  134. q->ordered = q->next_ordered;
  135. q->ordseq |= QUEUE_ORDSEQ_STARTED;
  136. /*
  137. * For an empty barrier, there's no actual BAR request, which
  138. * in turn makes POSTFLUSH unnecessary. Mask them off.
  139. */
  140. if (!blk_rq_sectors(rq)) {
  141. q->ordered &= ~(QUEUE_ORDERED_DO_BAR |
  142. QUEUE_ORDERED_DO_POSTFLUSH);
  143. /*
  144. * Empty barrier on a write-through device w/ ordered
  145. * tag has no command to issue and without any command
  146. * to issue, ordering by tag can't be used. Drain
  147. * instead.
  148. */
  149. if ((q->ordered & QUEUE_ORDERED_BY_TAG) &&
  150. !(q->ordered & QUEUE_ORDERED_DO_PREFLUSH)) {
  151. q->ordered &= ~QUEUE_ORDERED_BY_TAG;
  152. q->ordered |= QUEUE_ORDERED_BY_DRAIN;
  153. }
  154. }
  155. /* stash away the original request */
  156. blk_dequeue_request(rq);
  157. q->orig_bar_rq = rq;
  158. rq = NULL;
  159. /*
  160. * Queue ordered sequence. As we stack them at the head, we
  161. * need to queue in reverse order. Note that we rely on that
  162. * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
  163. * request gets inbetween ordered sequence.
  164. */
  165. if (q->ordered & QUEUE_ORDERED_DO_POSTFLUSH) {
  166. queue_flush(q, QUEUE_ORDERED_DO_POSTFLUSH);
  167. rq = &q->post_flush_rq;
  168. } else
  169. skip |= QUEUE_ORDSEQ_POSTFLUSH;
  170. if (q->ordered & QUEUE_ORDERED_DO_BAR) {
  171. rq = &q->bar_rq;
  172. /* initialize proxy request and queue it */
  173. blk_rq_init(q, rq);
  174. if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
  175. rq->cmd_flags |= REQ_RW;
  176. if (q->ordered & QUEUE_ORDERED_DO_FUA)
  177. rq->cmd_flags |= REQ_FUA;
  178. init_request_from_bio(rq, q->orig_bar_rq->bio);
  179. rq->end_io = bar_end_io;
  180. elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
  181. } else
  182. skip |= QUEUE_ORDSEQ_BAR;
  183. if (q->ordered & QUEUE_ORDERED_DO_PREFLUSH) {
  184. queue_flush(q, QUEUE_ORDERED_DO_PREFLUSH);
  185. rq = &q->pre_flush_rq;
  186. } else
  187. skip |= QUEUE_ORDSEQ_PREFLUSH;
  188. if ((q->ordered & QUEUE_ORDERED_BY_DRAIN) && queue_in_flight(q))
  189. rq = NULL;
  190. else
  191. skip |= QUEUE_ORDSEQ_DRAIN;
  192. *rqp = rq;
  193. /*
  194. * Complete skipped sequences. If whole sequence is complete,
  195. * return false to tell elevator that this request is gone.
  196. */
  197. return !blk_ordered_complete_seq(q, skip, 0);
  198. }
  199. bool blk_do_ordered(struct request_queue *q, struct request **rqp)
  200. {
  201. struct request *rq = *rqp;
  202. const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
  203. if (!q->ordseq) {
  204. if (!is_barrier)
  205. return true;
  206. if (q->next_ordered != QUEUE_ORDERED_NONE)
  207. return start_ordered(q, rqp);
  208. else {
  209. /*
  210. * Queue ordering not supported. Terminate
  211. * with prejudice.
  212. */
  213. blk_dequeue_request(rq);
  214. __blk_end_request_all(rq, -EOPNOTSUPP);
  215. *rqp = NULL;
  216. return false;
  217. }
  218. }
  219. /*
  220. * Ordered sequence in progress
  221. */
  222. /* Special requests are not subject to ordering rules. */
  223. if (!blk_fs_request(rq) &&
  224. rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
  225. return true;
  226. if (q->ordered & QUEUE_ORDERED_BY_TAG) {
  227. /* Ordered by tag. Blocking the next barrier is enough. */
  228. if (is_barrier && rq != &q->bar_rq)
  229. *rqp = NULL;
  230. } else {
  231. /* Ordered by draining. Wait for turn. */
  232. WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
  233. if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
  234. *rqp = NULL;
  235. }
  236. return true;
  237. }
  238. static void bio_end_empty_barrier(struct bio *bio, int err)
  239. {
  240. if (err) {
  241. if (err == -EOPNOTSUPP)
  242. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  243. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  244. }
  245. complete(bio->bi_private);
  246. }
  247. /**
  248. * blkdev_issue_flush - queue a flush
  249. * @bdev: blockdev to issue flush for
  250. * @error_sector: error sector
  251. *
  252. * Description:
  253. * Issue a flush for the block device in question. Caller can supply
  254. * room for storing the error offset in case of a flush error, if they
  255. * wish to.
  256. */
  257. int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
  258. {
  259. DECLARE_COMPLETION_ONSTACK(wait);
  260. struct request_queue *q;
  261. struct bio *bio;
  262. int ret;
  263. if (bdev->bd_disk == NULL)
  264. return -ENXIO;
  265. q = bdev_get_queue(bdev);
  266. if (!q)
  267. return -ENXIO;
  268. bio = bio_alloc(GFP_KERNEL, 0);
  269. bio->bi_end_io = bio_end_empty_barrier;
  270. bio->bi_private = &wait;
  271. bio->bi_bdev = bdev;
  272. submit_bio(WRITE_BARRIER, bio);
  273. wait_for_completion(&wait);
  274. /*
  275. * The driver must store the error location in ->bi_sector, if
  276. * it supports it. For non-stacked drivers, this should be copied
  277. * from blk_rq_pos(rq).
  278. */
  279. if (error_sector)
  280. *error_sector = bio->bi_sector;
  281. ret = 0;
  282. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  283. ret = -EOPNOTSUPP;
  284. else if (!bio_flagged(bio, BIO_UPTODATE))
  285. ret = -EIO;
  286. bio_put(bio);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(blkdev_issue_flush);
  290. static void blkdev_discard_end_io(struct bio *bio, int err)
  291. {
  292. if (err) {
  293. if (err == -EOPNOTSUPP)
  294. set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
  295. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  296. }
  297. if (bio->bi_private)
  298. complete(bio->bi_private);
  299. __free_page(bio_page(bio));
  300. bio_put(bio);
  301. }
  302. /**
  303. * blkdev_issue_discard - queue a discard
  304. * @bdev: blockdev to issue discard for
  305. * @sector: start sector
  306. * @nr_sects: number of sectors to discard
  307. * @gfp_mask: memory allocation flags (for bio_alloc)
  308. * @flags: DISCARD_FL_* flags to control behaviour
  309. *
  310. * Description:
  311. * Issue a discard request for the sectors in question.
  312. */
  313. int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
  314. sector_t nr_sects, gfp_t gfp_mask, int flags)
  315. {
  316. DECLARE_COMPLETION_ONSTACK(wait);
  317. struct request_queue *q = bdev_get_queue(bdev);
  318. int type = flags & DISCARD_FL_BARRIER ?
  319. DISCARD_BARRIER : DISCARD_NOBARRIER;
  320. struct bio *bio;
  321. struct page *page;
  322. int ret = 0;
  323. if (!q)
  324. return -ENXIO;
  325. if (!blk_queue_discard(q))
  326. return -EOPNOTSUPP;
  327. while (nr_sects && !ret) {
  328. unsigned int sector_size = q->limits.logical_block_size;
  329. unsigned int max_discard_sectors =
  330. min(q->limits.max_discard_sectors, UINT_MAX >> 9);
  331. bio = bio_alloc(gfp_mask, 1);
  332. if (!bio)
  333. goto out;
  334. bio->bi_sector = sector;
  335. bio->bi_end_io = blkdev_discard_end_io;
  336. bio->bi_bdev = bdev;
  337. if (flags & DISCARD_FL_WAIT)
  338. bio->bi_private = &wait;
  339. /*
  340. * Add a zeroed one-sector payload as that's what
  341. * our current implementations need. If we'll ever need
  342. * more the interface will need revisiting.
  343. */
  344. page = alloc_page(gfp_mask | __GFP_ZERO);
  345. if (!page)
  346. goto out_free_bio;
  347. if (bio_add_pc_page(q, bio, page, sector_size, 0) < sector_size)
  348. goto out_free_page;
  349. /*
  350. * And override the bio size - the way discard works we
  351. * touch many more blocks on disk than the actual payload
  352. * length.
  353. */
  354. if (nr_sects > max_discard_sectors) {
  355. bio->bi_size = max_discard_sectors << 9;
  356. nr_sects -= max_discard_sectors;
  357. sector += max_discard_sectors;
  358. } else {
  359. bio->bi_size = nr_sects << 9;
  360. nr_sects = 0;
  361. }
  362. bio_get(bio);
  363. submit_bio(type, bio);
  364. if (flags & DISCARD_FL_WAIT)
  365. wait_for_completion(&wait);
  366. if (bio_flagged(bio, BIO_EOPNOTSUPP))
  367. ret = -EOPNOTSUPP;
  368. else if (!bio_flagged(bio, BIO_UPTODATE))
  369. ret = -EIO;
  370. bio_put(bio);
  371. }
  372. return ret;
  373. out_free_page:
  374. __free_page(page);
  375. out_free_bio:
  376. bio_put(bio);
  377. out:
  378. return -ENOMEM;
  379. }
  380. EXPORT_SYMBOL(blkdev_issue_discard);