blk-merge.c 9.9 KB

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