bvec.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * bvec iterator
  3. *
  4. * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public Licens
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  19. */
  20. #ifndef __LINUX_BVEC_ITER_H
  21. #define __LINUX_BVEC_ITER_H
  22. #include <linux/kernel.h>
  23. #include <linux/bug.h>
  24. #include <linux/errno.h>
  25. /*
  26. * was unsigned short, but we might as well be ready for > 64kB I/O pages
  27. */
  28. struct bio_vec {
  29. struct page *bv_page;
  30. unsigned int bv_len;
  31. unsigned int bv_offset;
  32. };
  33. struct bvec_iter {
  34. sector_t bi_sector; /* device address in 512 byte
  35. sectors */
  36. unsigned int bi_size; /* residual I/O count */
  37. unsigned int bi_idx; /* current index into bvl_vec */
  38. unsigned int bi_bvec_done; /* number of bytes completed in
  39. current bvec */
  40. };
  41. /*
  42. * various member access, note that bio_data should of course not be used
  43. * on highmem page vectors
  44. */
  45. #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
  46. #define bvec_iter_page(bvec, iter) \
  47. (__bvec_iter_bvec((bvec), (iter))->bv_page)
  48. #define bvec_iter_len(bvec, iter) \
  49. min((iter).bi_size, \
  50. __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
  51. #define bvec_iter_offset(bvec, iter) \
  52. (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
  53. #define bvec_iter_bvec(bvec, iter) \
  54. ((struct bio_vec) { \
  55. .bv_page = bvec_iter_page((bvec), (iter)), \
  56. .bv_len = bvec_iter_len((bvec), (iter)), \
  57. .bv_offset = bvec_iter_offset((bvec), (iter)), \
  58. })
  59. static inline bool bvec_iter_advance(const struct bio_vec *bv,
  60. struct bvec_iter *iter, unsigned bytes)
  61. {
  62. if (WARN_ONCE(bytes > iter->bi_size,
  63. "Attempted to advance past end of bvec iter\n")) {
  64. iter->bi_size = 0;
  65. return false;
  66. }
  67. while (bytes) {
  68. unsigned iter_len = bvec_iter_len(bv, *iter);
  69. unsigned len = min(bytes, iter_len);
  70. bytes -= len;
  71. iter->bi_size -= len;
  72. iter->bi_bvec_done += len;
  73. if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
  74. iter->bi_bvec_done = 0;
  75. iter->bi_idx++;
  76. }
  77. }
  78. return true;
  79. }
  80. static inline bool bvec_iter_rewind(const struct bio_vec *bv,
  81. struct bvec_iter *iter,
  82. unsigned int bytes)
  83. {
  84. while (bytes) {
  85. unsigned len = min(bytes, iter->bi_bvec_done);
  86. if (iter->bi_bvec_done == 0) {
  87. if (WARN_ONCE(iter->bi_idx == 0,
  88. "Attempted to rewind iter beyond "
  89. "bvec's boundaries\n")) {
  90. return false;
  91. }
  92. iter->bi_idx--;
  93. iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len;
  94. continue;
  95. }
  96. bytes -= len;
  97. iter->bi_size += len;
  98. iter->bi_bvec_done -= len;
  99. }
  100. return true;
  101. }
  102. #define for_each_bvec(bvl, bio_vec, iter, start) \
  103. for (iter = (start); \
  104. (iter).bi_size && \
  105. ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
  106. bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
  107. /* for iterating one bio from start to end */
  108. #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
  109. { \
  110. .bi_sector = 0, \
  111. .bi_size = UINT_MAX, \
  112. .bi_idx = 0, \
  113. .bi_bvec_done = 0, \
  114. }
  115. #endif /* __LINUX_BVEC_ITER_H */