blk-merge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 && q->dma_drain_needed(rq)) {
  199. if (rq->cmd_flags & REQ_RW)
  200. memset(q->dma_drain_buffer, 0, q->dma_drain_size);
  201. sg->page_link &= ~0x02;
  202. sg = sg_next(sg);
  203. sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
  204. q->dma_drain_size,
  205. ((unsigned long)q->dma_drain_buffer) &
  206. (PAGE_SIZE - 1));
  207. nsegs++;
  208. rq->extra_len += q->dma_drain_size;
  209. }
  210. if (sg)
  211. sg_mark_end(sg);
  212. return nsegs;
  213. }
  214. EXPORT_SYMBOL(blk_rq_map_sg);
  215. static inline int ll_new_mergeable(struct request_queue *q,
  216. struct request *req,
  217. struct bio *bio)
  218. {
  219. int nr_phys_segs = bio_phys_segments(q, bio);
  220. if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  221. req->cmd_flags |= REQ_NOMERGE;
  222. if (req == q->last_merge)
  223. q->last_merge = NULL;
  224. return 0;
  225. }
  226. /*
  227. * A hw segment is just getting larger, bump just the phys
  228. * counter.
  229. */
  230. req->nr_phys_segments += nr_phys_segs;
  231. return 1;
  232. }
  233. static inline int ll_new_hw_segment(struct request_queue *q,
  234. struct request *req,
  235. struct bio *bio)
  236. {
  237. int nr_hw_segs = bio_hw_segments(q, bio);
  238. int nr_phys_segs = bio_phys_segments(q, bio);
  239. if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
  240. || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
  241. req->cmd_flags |= REQ_NOMERGE;
  242. if (req == q->last_merge)
  243. q->last_merge = NULL;
  244. return 0;
  245. }
  246. /*
  247. * This will form the start of a new hw segment. Bump both
  248. * counters.
  249. */
  250. req->nr_hw_segments += nr_hw_segs;
  251. req->nr_phys_segments += nr_phys_segs;
  252. return 1;
  253. }
  254. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  255. struct bio *bio)
  256. {
  257. unsigned short max_sectors;
  258. int len;
  259. if (unlikely(blk_pc_request(req)))
  260. max_sectors = q->max_hw_sectors;
  261. else
  262. max_sectors = q->max_sectors;
  263. if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
  264. req->cmd_flags |= REQ_NOMERGE;
  265. if (req == q->last_merge)
  266. q->last_merge = NULL;
  267. return 0;
  268. }
  269. if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
  270. blk_recount_segments(q, req->biotail);
  271. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  272. blk_recount_segments(q, bio);
  273. len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
  274. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio))
  275. && !BIOVEC_VIRT_OVERSIZE(len)) {
  276. int mergeable = ll_new_mergeable(q, req, bio);
  277. if (mergeable) {
  278. if (req->nr_hw_segments == 1)
  279. req->bio->bi_hw_front_size = len;
  280. if (bio->bi_hw_segments == 1)
  281. bio->bi_hw_back_size = len;
  282. }
  283. return mergeable;
  284. }
  285. return ll_new_hw_segment(q, req, bio);
  286. }
  287. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  288. struct bio *bio)
  289. {
  290. unsigned short max_sectors;
  291. int len;
  292. if (unlikely(blk_pc_request(req)))
  293. max_sectors = q->max_hw_sectors;
  294. else
  295. max_sectors = q->max_sectors;
  296. if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
  297. req->cmd_flags |= REQ_NOMERGE;
  298. if (req == q->last_merge)
  299. q->last_merge = NULL;
  300. return 0;
  301. }
  302. len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
  303. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  304. blk_recount_segments(q, bio);
  305. if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
  306. blk_recount_segments(q, req->bio);
  307. if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
  308. !BIOVEC_VIRT_OVERSIZE(len)) {
  309. int mergeable = ll_new_mergeable(q, req, bio);
  310. if (mergeable) {
  311. if (bio->bi_hw_segments == 1)
  312. bio->bi_hw_front_size = len;
  313. if (req->nr_hw_segments == 1)
  314. req->biotail->bi_hw_back_size = len;
  315. }
  316. return mergeable;
  317. }
  318. return ll_new_hw_segment(q, req, bio);
  319. }
  320. static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
  321. struct request *next)
  322. {
  323. int total_phys_segments;
  324. int total_hw_segments;
  325. /*
  326. * First check if the either of the requests are re-queued
  327. * requests. Can't merge them if they are.
  328. */
  329. if (req->special || next->special)
  330. return 0;
  331. /*
  332. * Will it become too large?
  333. */
  334. if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
  335. return 0;
  336. total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
  337. if (blk_phys_contig_segment(q, req->biotail, next->bio))
  338. total_phys_segments--;
  339. if (total_phys_segments > q->max_phys_segments)
  340. return 0;
  341. total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
  342. if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
  343. int len = req->biotail->bi_hw_back_size +
  344. next->bio->bi_hw_front_size;
  345. /*
  346. * propagate the combined length to the end of the requests
  347. */
  348. if (req->nr_hw_segments == 1)
  349. req->bio->bi_hw_front_size = len;
  350. if (next->nr_hw_segments == 1)
  351. next->biotail->bi_hw_back_size = len;
  352. total_hw_segments--;
  353. }
  354. if (total_hw_segments > q->max_hw_segments)
  355. return 0;
  356. /* Merge is OK... */
  357. req->nr_phys_segments = total_phys_segments;
  358. req->nr_hw_segments = total_hw_segments;
  359. return 1;
  360. }
  361. /*
  362. * Has to be called with the request spinlock acquired
  363. */
  364. static int attempt_merge(struct request_queue *q, struct request *req,
  365. struct request *next)
  366. {
  367. if (!rq_mergeable(req) || !rq_mergeable(next))
  368. return 0;
  369. /*
  370. * not contiguous
  371. */
  372. if (req->sector + req->nr_sectors != next->sector)
  373. return 0;
  374. if (rq_data_dir(req) != rq_data_dir(next)
  375. || req->rq_disk != next->rq_disk
  376. || next->special)
  377. return 0;
  378. /*
  379. * If we are allowed to merge, then append bio list
  380. * from next to rq and release next. merge_requests_fn
  381. * will have updated segment counts, update sector
  382. * counts here.
  383. */
  384. if (!ll_merge_requests_fn(q, req, next))
  385. return 0;
  386. /*
  387. * At this point we have either done a back merge
  388. * or front merge. We need the smaller start_time of
  389. * the merged requests to be the current request
  390. * for accounting purposes.
  391. */
  392. if (time_after(req->start_time, next->start_time))
  393. req->start_time = next->start_time;
  394. req->biotail->bi_next = next->bio;
  395. req->biotail = next->biotail;
  396. req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
  397. elv_merge_requests(q, req, next);
  398. if (req->rq_disk) {
  399. struct hd_struct *part
  400. = get_part(req->rq_disk, req->sector);
  401. disk_round_stats(req->rq_disk);
  402. req->rq_disk->in_flight--;
  403. if (part) {
  404. part_round_stats(part);
  405. part->in_flight--;
  406. }
  407. }
  408. req->ioprio = ioprio_best(req->ioprio, next->ioprio);
  409. __blk_put_request(q, next);
  410. return 1;
  411. }
  412. int attempt_back_merge(struct request_queue *q, struct request *rq)
  413. {
  414. struct request *next = elv_latter_request(q, rq);
  415. if (next)
  416. return attempt_merge(q, rq, next);
  417. return 0;
  418. }
  419. int attempt_front_merge(struct request_queue *q, struct request *rq)
  420. {
  421. struct request *prev = elv_former_request(q, rq);
  422. if (prev)
  423. return attempt_merge(q, prev, rq);
  424. return 0;
  425. }