blk-merge.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Functions related to segment and merge 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/scatterlist.h>
  9. #include "blk.h"
  10. static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
  11. struct bio *bio)
  12. {
  13. unsigned int phys_size;
  14. struct bio_vec *bv, *bvprv = NULL;
  15. int cluster, i, high, highprv = 1;
  16. unsigned int seg_size, nr_phys_segs;
  17. struct bio *fbio, *bbio;
  18. if (!bio)
  19. return 0;
  20. fbio = bio;
  21. cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  22. seg_size = 0;
  23. phys_size = nr_phys_segs = 0;
  24. for_each_bio(bio) {
  25. bio_for_each_segment(bv, bio, i) {
  26. /*
  27. * the trick here is making sure that a high page is
  28. * never considered part of another segment, since that
  29. * might change with the bounce page.
  30. */
  31. high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
  32. if (high || highprv)
  33. goto new_segment;
  34. if (cluster) {
  35. if (seg_size + bv->bv_len > q->max_segment_size)
  36. goto new_segment;
  37. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
  38. goto new_segment;
  39. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
  40. goto new_segment;
  41. seg_size += bv->bv_len;
  42. bvprv = bv;
  43. continue;
  44. }
  45. new_segment:
  46. if (nr_phys_segs == 1 && seg_size >
  47. fbio->bi_seg_front_size)
  48. fbio->bi_seg_front_size = seg_size;
  49. nr_phys_segs++;
  50. bvprv = bv;
  51. seg_size = bv->bv_len;
  52. highprv = high;
  53. }
  54. bbio = bio;
  55. }
  56. if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
  57. fbio->bi_seg_front_size = seg_size;
  58. if (seg_size > bbio->bi_seg_back_size)
  59. bbio->bi_seg_back_size = seg_size;
  60. return nr_phys_segs;
  61. }
  62. void blk_recalc_rq_segments(struct request *rq)
  63. {
  64. rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
  65. }
  66. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  67. {
  68. struct bio *nxt = bio->bi_next;
  69. bio->bi_next = NULL;
  70. bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
  71. bio->bi_next = nxt;
  72. bio->bi_flags |= (1 << BIO_SEG_VALID);
  73. }
  74. EXPORT_SYMBOL(blk_recount_segments);
  75. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  76. struct bio *nxt)
  77. {
  78. if (!test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
  79. return 0;
  80. if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
  81. q->max_segment_size)
  82. return 0;
  83. if (!bio_has_data(bio))
  84. return 1;
  85. if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
  86. return 0;
  87. /*
  88. * bio and nxt are contiguous in memory; check if the queue allows
  89. * these two to be merged into one
  90. */
  91. if (BIO_SEG_BOUNDARY(q, bio, nxt))
  92. return 1;
  93. return 0;
  94. }
  95. /*
  96. * map a request to scatterlist, return number of sg entries setup. Caller
  97. * must make sure sg can hold rq->nr_phys_segments entries
  98. */
  99. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  100. struct scatterlist *sglist)
  101. {
  102. struct bio_vec *bvec, *bvprv;
  103. struct req_iterator iter;
  104. struct scatterlist *sg;
  105. int nsegs, cluster;
  106. nsegs = 0;
  107. cluster = test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  108. /*
  109. * for each bio in rq
  110. */
  111. bvprv = NULL;
  112. sg = NULL;
  113. rq_for_each_segment(bvec, rq, iter) {
  114. int nbytes = bvec->bv_len;
  115. if (bvprv && cluster) {
  116. if (sg->length + nbytes > q->max_segment_size)
  117. goto new_segment;
  118. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
  119. goto new_segment;
  120. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
  121. goto new_segment;
  122. sg->length += nbytes;
  123. } else {
  124. new_segment:
  125. if (!sg)
  126. sg = sglist;
  127. else {
  128. /*
  129. * If the driver previously mapped a shorter
  130. * list, we could see a termination bit
  131. * prematurely unless it fully inits the sg
  132. * table on each mapping. We KNOW that there
  133. * must be more entries here or the driver
  134. * would be buggy, so force clear the
  135. * termination bit to avoid doing a full
  136. * sg_init_table() in drivers for each command.
  137. */
  138. sg->page_link &= ~0x02;
  139. sg = sg_next(sg);
  140. }
  141. sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
  142. nsegs++;
  143. }
  144. bvprv = bvec;
  145. } /* segments in rq */
  146. if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
  147. (blk_rq_bytes(rq) & q->dma_pad_mask)) {
  148. unsigned int pad_len =
  149. (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
  150. sg->length += pad_len;
  151. rq->extra_len += pad_len;
  152. }
  153. if (q->dma_drain_size && q->dma_drain_needed(rq)) {
  154. if (rq->cmd_flags & REQ_RW)
  155. memset(q->dma_drain_buffer, 0, q->dma_drain_size);
  156. sg->page_link &= ~0x02;
  157. sg = sg_next(sg);
  158. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  159. q->dma_drain_size,
  160. ((unsigned long)q->dma_drain_buffer) &
  161. (PAGE_SIZE - 1));
  162. nsegs++;
  163. rq->extra_len += q->dma_drain_size;
  164. }
  165. if (sg)
  166. sg_mark_end(sg);
  167. return nsegs;
  168. }
  169. EXPORT_SYMBOL(blk_rq_map_sg);
  170. static inline int ll_new_hw_segment(struct request_queue *q,
  171. struct request *req,
  172. struct bio *bio)
  173. {
  174. int nr_phys_segs = bio_phys_segments(q, bio);
  175. if (req->nr_phys_segments + nr_phys_segs > q->max_hw_segments
  176. || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  177. req->cmd_flags |= REQ_NOMERGE;
  178. if (req == q->last_merge)
  179. q->last_merge = NULL;
  180. return 0;
  181. }
  182. /*
  183. * This will form the start of a new hw segment. Bump both
  184. * counters.
  185. */
  186. req->nr_phys_segments += nr_phys_segs;
  187. return 1;
  188. }
  189. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  190. struct bio *bio)
  191. {
  192. unsigned short max_sectors;
  193. if (unlikely(blk_pc_request(req)))
  194. max_sectors = q->max_hw_sectors;
  195. else
  196. max_sectors = q->max_sectors;
  197. if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
  198. req->cmd_flags |= REQ_NOMERGE;
  199. if (req == q->last_merge)
  200. q->last_merge = NULL;
  201. return 0;
  202. }
  203. if (!bio_flagged(req->biotail, BIO_SEG_VALID))
  204. blk_recount_segments(q, req->biotail);
  205. if (!bio_flagged(bio, BIO_SEG_VALID))
  206. blk_recount_segments(q, bio);
  207. return ll_new_hw_segment(q, req, bio);
  208. }
  209. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  210. struct bio *bio)
  211. {
  212. unsigned short max_sectors;
  213. if (unlikely(blk_pc_request(req)))
  214. max_sectors = q->max_hw_sectors;
  215. else
  216. max_sectors = q->max_sectors;
  217. if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
  218. req->cmd_flags |= REQ_NOMERGE;
  219. if (req == q->last_merge)
  220. q->last_merge = NULL;
  221. return 0;
  222. }
  223. if (!bio_flagged(bio, BIO_SEG_VALID))
  224. blk_recount_segments(q, bio);
  225. if (!bio_flagged(req->bio, BIO_SEG_VALID))
  226. blk_recount_segments(q, req->bio);
  227. return ll_new_hw_segment(q, req, bio);
  228. }
  229. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  230. struct request *next)
  231. {
  232. int total_phys_segments;
  233. unsigned int seg_size =
  234. req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
  235. /*
  236. * First check if the either of the requests are re-queued
  237. * requests. Can't merge them if they are.
  238. */
  239. if (req->special || next->special)
  240. return 0;
  241. /*
  242. * Will it become too large?
  243. */
  244. if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > q->max_sectors)
  245. return 0;
  246. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  247. if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
  248. if (req->nr_phys_segments == 1)
  249. req->bio->bi_seg_front_size = seg_size;
  250. if (next->nr_phys_segments == 1)
  251. next->biotail->bi_seg_back_size = seg_size;
  252. total_phys_segments--;
  253. }
  254. if (total_phys_segments > q->max_phys_segments)
  255. return 0;
  256. if (total_phys_segments > q->max_hw_segments)
  257. return 0;
  258. /* Merge is OK... */
  259. req->nr_phys_segments = total_phys_segments;
  260. return 1;
  261. }
  262. static void blk_account_io_merge(struct request *req)
  263. {
  264. if (blk_do_io_stat(req)) {
  265. struct hd_struct *part;
  266. int cpu;
  267. cpu = part_stat_lock();
  268. part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
  269. part_round_stats(cpu, part);
  270. part_dec_in_flight(part);
  271. part_stat_unlock();
  272. }
  273. }
  274. /*
  275. * Has to be called with the request spinlock acquired
  276. */
  277. static int attempt_merge(struct request_queue *q, struct request *req,
  278. struct request *next)
  279. {
  280. if (!rq_mergeable(req) || !rq_mergeable(next))
  281. return 0;
  282. /*
  283. * not contiguous
  284. */
  285. if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
  286. return 0;
  287. if (rq_data_dir(req) != rq_data_dir(next)
  288. || req->rq_disk != next->rq_disk
  289. || next->special)
  290. return 0;
  291. if (blk_integrity_rq(req) != blk_integrity_rq(next))
  292. return 0;
  293. /*
  294. * If we are allowed to merge, then append bio list
  295. * from next to rq and release next. merge_requests_fn
  296. * will have updated segment counts, update sector
  297. * counts here.
  298. */
  299. if (!ll_merge_requests_fn(q, req, next))
  300. return 0;
  301. /*
  302. * At this point we have either done a back merge
  303. * or front merge. We need the smaller start_time of
  304. * the merged requests to be the current request
  305. * for accounting purposes.
  306. */
  307. if (time_after(req->start_time, next->start_time))
  308. req->start_time = next->start_time;
  309. req->biotail->bi_next = next->bio;
  310. req->biotail = next->biotail;
  311. req->__data_len += blk_rq_bytes(next);
  312. elv_merge_requests(q, req, next);
  313. /*
  314. * 'next' is going away, so update stats accordingly
  315. */
  316. blk_account_io_merge(next);
  317. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  318. if (blk_rq_cpu_valid(next))
  319. req->cpu = next->cpu;
  320. /* owner-ship of bio passed from next to req */
  321. next->bio = NULL;
  322. __blk_put_request(q, next);
  323. return 1;
  324. }
  325. int attempt_back_merge(struct request_queue *q, struct request *rq)
  326. {
  327. struct request *next = elv_latter_request(q, rq);
  328. if (next)
  329. return attempt_merge(q, rq, next);
  330. return 0;
  331. }
  332. int attempt_front_merge(struct request_queue *q, struct request *rq)
  333. {
  334. struct request *prev = elv_former_request(q, rq);
  335. if (prev)
  336. return attempt_merge(q, prev, rq);
  337. return 0;
  338. }