blk-merge.c 10 KB

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