bvec.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /*
  25. * was unsigned short, but we might as well be ready for > 64kB I/O pages
  26. */
  27. struct bio_vec {
  28. struct page *bv_page;
  29. unsigned int bv_len;
  30. unsigned int bv_offset;
  31. };
  32. struct bvec_iter {
  33. sector_t bi_sector; /* device address in 512 byte
  34. sectors */
  35. unsigned int bi_size; /* residual I/O count */
  36. unsigned int bi_idx; /* current index into bvl_vec */
  37. unsigned int bi_bvec_done; /* number of bytes completed in
  38. current bvec */
  39. };
  40. /*
  41. * various member access, note that bio_data should of course not be used
  42. * on highmem page vectors
  43. */
  44. #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
  45. #define bvec_iter_page(bvec, iter) \
  46. (__bvec_iter_bvec((bvec), (iter))->bv_page)
  47. #define bvec_iter_len(bvec, iter) \
  48. min((iter).bi_size, \
  49. __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
  50. #define bvec_iter_offset(bvec, iter) \
  51. (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
  52. #define bvec_iter_bvec(bvec, iter) \
  53. ((struct bio_vec) { \
  54. .bv_page = bvec_iter_page((bvec), (iter)), \
  55. .bv_len = bvec_iter_len((bvec), (iter)), \
  56. .bv_offset = bvec_iter_offset((bvec), (iter)), \
  57. })
  58. static inline void bvec_iter_advance(const struct bio_vec *bv,
  59. struct bvec_iter *iter,
  60. unsigned bytes)
  61. {
  62. WARN_ONCE(bytes > iter->bi_size,
  63. "Attempted to advance past end of bvec iter\n");
  64. while (bytes) {
  65. unsigned iter_len = bvec_iter_len(bv, *iter);
  66. unsigned len = min(bytes, iter_len);
  67. bytes -= len;
  68. iter->bi_size -= len;
  69. iter->bi_bvec_done += len;
  70. if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
  71. iter->bi_bvec_done = 0;
  72. iter->bi_idx++;
  73. }
  74. }
  75. }
  76. #define for_each_bvec(bvl, bio_vec, iter, start) \
  77. for (iter = (start); \
  78. (iter).bi_size && \
  79. ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
  80. bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
  81. #endif /* __LINUX_BVEC_ITER_H */