blk-merge.c 12 KB

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