blk-merge.c 9.7 KB

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