blk-merge.c 12 KB

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