dataOperationOfSprayTerminal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * blk-integrity.c - Block layer data integrity extensions
  3. *
  4. * Copyright (C) 2007, 2008 Oracle Corporation
  5. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  19. * USA.
  20. *
  21. */
  22. #include <linux/blkdev.h>
  23. #include <linux/mempool.h>
  24. #include <linux/bio.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/export.h>
  27. #include <linux/slab.h>
  28. #include "blk.h"
  29. static struct kmem_cache *integrity_cachep;
  30. static const char *bi_unsupported_name = "unsupported";
  31. /**
  32. * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
  33. * @q: request queue
  34. * @bio: bio with integrity metadata attached
  35. *
  36. * Description: Returns the number of elements required in a
  37. * scatterlist corresponding to the integrity metadata in a bio.
  38. */
  39. int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
  40. {
  41. struct bio_vec *iv, *ivprv = NULL;
  42. unsigned int segments = 0;
  43. unsigned int seg_size = 0;
  44. unsigned int i = 0;
  45. bio_for_each_integrity_vec(iv, bio, i) {
  46. if (ivprv) {
  47. if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
  48. goto new_segment;
  49. if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
  50. goto new_segment;
  51. if (seg_size + iv->bv_len > queue_max_segment_size(q))
  52. goto new_segment;
  53. seg_size += iv->bv_len;
  54. } else {
  55. new_segment:
  56. segments++;
  57. seg_size = iv->bv_len;
  58. }
  59. ivprv = iv;
  60. }
  61. return segments;
  62. }
  63. EXPORT_SYMBOL(blk_rq_count_integrity_sg);
  64. /**
  65. * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
  66. * @q: request queue
  67. * @bio: bio with integrity metadata attached
  68. * @sglist: target scatterlist
  69. *
  70. * Description: Map the integrity vectors in request into a
  71. * scatterlist. The scatterlist must be big enough to hold all
  72. * elements. I.e. sized using blk_rq_count_integrity_sg().
  73. */
  74. int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
  75. struct scatterlist *sglist)
  76. {
  77. struct bio_vec *iv, *ivprv = NULL;
  78. struct scatterlist *sg = NULL;
  79. unsigned int segments = 0;
  80. unsigned int i = 0;
  81. bio_for_each_integrity_vec(iv, bio, i) {
  82. if (ivprv) {
  83. if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
  84. goto new_segment;
  85. if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
  86. goto new_segment;
  87. if (sg->length + iv->bv_len > queue_max_segment_size(q))
  88. goto new_segment;
  89. sg->length += iv->bv_len;
  90. } else {
  91. new_segment:
  92. if (!sg)
  93. sg = sglist;
  94. else {
  95. sg->page_link &= ~0x02;
  96. sg = sg_next(sg);
  97. }
  98. sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
  99. segments++;
  100. }
  101. ivprv = iv;
  102. }
  103. if (sg)
  104. sg_mark_end(sg);
  105. return segments;
  106. }
  107. EXPORT_SYMBOL(blk_rq_map_integrity_sg);
  108. /**
  109. * blk_integrity_compare - Compare integrity profile of two disks
  110. * @gd1: Disk to compare
  111. * @gd2: Disk to compare
  112. *
  113. * Description: Meta-devices like DM and MD need to verify that all
  114. * sub-devices use the same integrity format before advertising to
  115. * upper layers that they can send/receive integrity metadata. This
  116. * function can be used to check whether two gendisk devices have
  117. * compatible integrity formats.
  118. */
  119. int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
  120. {
  121. struct blk_integrity *b1 = gd1->integrity;
  122. struct blk_integrity *b2 = gd2->integrity;
  123. if (!b1 && !b2)
  124. return 0;
  125. if (!b1 || !b2)
  126. return -1;
  127. if (b1->sector_size != b2->sector_size) {
  128. printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
  129. gd1->disk_name, gd2->disk_name,
  130. b1->sector_size, b2->sector_size);
  131. return -1;
  132. }
  133. if (b1->tuple_size != b2->tuple_size) {
  134. printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
  135. gd1->disk_name, gd2->disk_name,
  136. b1->tuple_size, b2->tuple_size);
  137. return -1;
  138. }
  139. if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
  140. printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
  141. gd1->disk_name, gd2->disk_name,
  142. b1->tag_size, b2->tag_size);
  143. return -1;
  144. }
  145. if (strcmp(b1->name, b2->name)) {
  146. printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
  147. gd1->disk_name, gd2->disk_name,
  148. b1->name, b2->name);
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(blk_integrity_compare);
  154. int blk_integrity_merge_rq(struct request_queue *q, struct request *req,
  155. struct request *next)
  156. {
  157. if (blk_integrity_rq(req) != blk_integrity_rq(next))
  158. return -1;
  159. if (req->nr_integrity_segments + next->nr_integrity_segments >
  160. q->limits.max_integrity_segments)
  161. return -1;