blk-merge.c 12 KB

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