fireHydrantDataOperation.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
  11. struct bio *bio)
  12. {
  13. struct bio_vec *bv, *bvprv = NULL;
  14. int cluster, i, high, highprv = 1;
  15. unsigned int seg_size, nr_phys_segs;
  16. struct bio *fbio, *bbio;
  17. if (!bio)
  18. return 0;
  19. fbio = bio;
  20. cluster = blk_queue_cluster(q);
  21. seg_size = 0;
  22. nr_phys_segs = 0;
  23. for_each_bio(bio) {
  24. bio_for_each_segment(bv, bio, i) {
  25. /*
  26. * the trick here is making sure that a high page is
  27. * never considered part of another segment, since that
  28. * might change with the bounce page.
  29. */
  30. high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q);
  31. if (high || highprv)
  32. goto new_segment;
  33. if (cluster) {
  34. if (seg_size + bv->bv_len
  35. > queue_max_segment_size(q))
  36. goto new_segment;
  37. if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
  38. goto new_segment;
  39. if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
  40. goto new_segment;
  41. seg_size += bv->bv_len;
  42. bvprv = bv;
  43. continue;
  44. }
  45. new_segment:
  46. if (nr_phys_segs == 1 && seg_size >
  47. fbio->bi_seg_front_size)
  48. fbio->bi_seg_front_size = seg_size;
  49. nr_phys_segs++;
  50. bvprv = bv;
  51. seg_size = bv->bv_len;
  52. highprv = high;
  53. }
  54. bbio = bio;
  55. }
  56. if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
  57. fbio->bi_seg_front_size = seg_size;
  58. if (seg_size > bbio->bi_seg_back_size)
  59. bbio->bi_seg_back_size = seg_size;
  60. return nr_phys_segs;
  61. }
  62. void blk_recalc_rq_segments(struct request *rq)
  63. {
  64. rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
  65. }
  66. void blk_recount_segments(struct request_queue *q, struct bio *bio)
  67. {
  68. struct bio *nxt = bio->bi_next;
  69. bio->bi_next = NULL;
  70. bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
  71. bio->bi_next = nxt;
  72. bio->bi_flags |= (1 << BIO_SEG_VALID);
  73. }
  74. EXPORT_SYMBOL(blk_recount_segments);
  75. static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
  76. struct bio *nxt)
  77. {
  78. if (!blk_queue_cluster(q))
  79. return 0;
  80. if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
  81. queue_max_segment_size(q))
  82. return 0;
  83. if (!bio_has_data(bio))
  84. return 1;
  85. if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
  86. return 0;
  87. /*
  88. * bio and nxt are contiguous in memory; check if the queue allows
  89. * these two to be merged into one
  90. */
  91. if (BIO_SEG_BOUNDARY(q, bio, nxt))
  92. return 1;
  93. return 0;
  94. }
  95. static void
  96. __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
  97. struct scatterlist *sglist, struct bio_vec **bvprv,
  98. struct scatterlist **sg, int *nsegs, int *cluster)
  99. {
  100. int nbytes = bvec->bv_len;
  101. if (*bvprv && *cluster) {
  102. if ((*sg)->length + nbytes > queue_max_segment_size(q))
  103. goto new_segment;
  104. if (!BIOVEC_PHYS_MERGEABLE(*bvprv, bvec))
  105. goto new_segment;
  106. if (!BIOVEC_SEG_BOUNDARY(q, *bvprv, bvec))
  107. goto new_segment;
  108. (*sg)->length += nbytes;
  109. } else {
  110. new_segment:
  111. if (!*sg)
  112. *sg = sglist;
  113. else {
  114. /*
  115. * If the driver previously mapped a shorter
  116. * list, we could see a termination bit
  117. * prematurely unless it fully inits the sg
  118. * table on each mapping. We KNOW that there
  119. * must be more entries here or the driver
  120. * would be buggy, so force clear the
  121. * termination bit to avoid doing a full
  122. * sg_init_table() in drivers for each command.
  123. */
  124. (*sg)->page_link &= ~0x02;
  125. *sg = sg_next(*sg);
  126. }
  127. sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
  128. (*nsegs)++;
  129. }
  130. *bvprv = bvec;
  131. }
  132. /*
  133. * map a request to scatterlist, return number of sg entries setup. Caller
  134. * must make sure sg can hold rq->nr_phys_segments entries
  135. */
  136. int blk_rq_map_sg(struct request_queue *q, struct request *rq,
  137. struct scatterlist *sglist)
  138. {
  139. struct bio_vec *bvec, *bvprv;
  140. struct req_iterator iter;
  141. struct scatterlist *sg;
  142. int nsegs, cluster;
  143. nsegs = 0;
  144. cluster = blk_queue_cluster(q);
  145. /*
  146. * for each bio in rq
  147. */
  148. bvprv = NULL;
  149. sg = NULL;
  150. rq_for_each_segment(bvec, rq, iter) {
  151. __blk_segment_map_sg(q, bvec, sglist, &bvprv, &sg,
  152. &nsegs, &cluster);
  153. } /* segments in rq */
  154. if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
  155. (blk_rq_bytes(rq) & q->dma_pad_mask)) {
  156. unsigned int pad_len =
  157. (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
  158. sg->length += pad_len;
  159. rq->extra_len += pad_len;
  160. }
  161. if (q->dma_drain_size && q->dma_drain_needed(rq)) {
  162. if (rq->cmd_flags & REQ_WRITE)