bio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * 2.5 block I/O model
  3. *
  4. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  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. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public Licens
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  18. */
  19. #ifndef __LINUX_BIO_H
  20. #define __LINUX_BIO_H
  21. #include <linux/highmem.h>
  22. #include <linux/mempool.h>
  23. /* Platforms may set this to teach the BIO layer about IOMMU hardware. */
  24. #include <asm/io.h>
  25. #if defined(BIO_VMERGE_MAX_SIZE) && defined(BIO_VMERGE_BOUNDARY)
  26. #define BIOVEC_VIRT_START_SIZE(x) (bvec_to_phys(x) & (BIO_VMERGE_BOUNDARY - 1))
  27. #define BIOVEC_VIRT_OVERSIZE(x) ((x) > BIO_VMERGE_MAX_SIZE)
  28. #else
  29. #define BIOVEC_VIRT_START_SIZE(x) 0
  30. #define BIOVEC_VIRT_OVERSIZE(x) 0
  31. #endif
  32. #ifndef BIO_VMERGE_BOUNDARY
  33. #define BIO_VMERGE_BOUNDARY 0
  34. #endif
  35. #define BIO_DEBUG
  36. #ifdef BIO_DEBUG
  37. #define BIO_BUG_ON BUG_ON
  38. #else
  39. #define BIO_BUG_ON
  40. #endif
  41. #define BIO_MAX_PAGES (256)
  42. #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_CACHE_SHIFT)
  43. #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
  44. /*
  45. * was unsigned short, but we might as well be ready for > 64kB I/O pages
  46. */
  47. struct bio_vec {
  48. struct page *bv_page;
  49. unsigned int bv_len;
  50. unsigned int bv_offset;
  51. };
  52. struct bio_set;
  53. struct bio;
  54. typedef int (bio_end_io_t) (struct bio *, unsigned int, int);
  55. typedef void (bio_destructor_t) (struct bio *);
  56. /*
  57. * main unit of I/O for the block layer and lower layers (ie drivers and
  58. * stacking drivers)
  59. */
  60. struct bio {
  61. sector_t bi_sector;
  62. struct bio *bi_next; /* request queue link */
  63. struct block_device *bi_bdev;
  64. unsigned long bi_flags; /* status, command, etc */
  65. unsigned long bi_rw; /* bottom bits READ/WRITE,
  66. * top bits priority
  67. */
  68. unsigned short bi_vcnt; /* how many bio_vec's */
  69. unsigned short bi_idx; /* current index into bvl_vec */
  70. /* Number of segments in this BIO after
  71. * physical address coalescing is performed.
  72. */
  73. unsigned short bi_phys_segments;
  74. /* Number of segments after physical and DMA remapping
  75. * hardware coalescing is performed.
  76. */
  77. unsigned short bi_hw_segments;
  78. unsigned int bi_size; /* residual I/O count */
  79. /*
  80. * To keep track of the max hw size, we account for the
  81. * sizes of the first and last virtually mergeable segments
  82. * in this bio
  83. */
  84. unsigned int bi_hw_front_size;
  85. unsigned int bi_hw_back_size;
  86. unsigned int bi_max_vecs; /* max bvl_vecs we can hold */
  87. struct bio_vec *bi_io_vec; /* the actual vec list */
  88. bio_end_io_t *bi_end_io;
  89. atomic_t bi_cnt; /* pin count */
  90. void *bi_private;
  91. bio_destructor_t *bi_destructor; /* destructor */
  92. struct bio_set *bi_set; /* memory pools set */
  93. };
  94. /*
  95. * bio flags
  96. */
  97. #define BIO_UPTODATE 0 /* ok after I/O completion */
  98. #define BIO_RW_BLOCK 1 /* RW_AHEAD set, and read/write would block */
  99. #define BIO_EOF 2 /* out-out-bounds error */
  100. #define BIO_SEG_VALID 3 /* nr_hw_seg valid */
  101. #define BIO_CLONED 4 /* doesn't own data */
  102. #define BIO_BOUNCED 5 /* bio is a bounce bio */
  103. #define BIO_USER_MAPPED 6 /* contains user pages */
  104. #define BIO_EOPNOTSUPP 7 /* not supported */
  105. #define bio_flagged(bio, flag) ((bio)->bi_flags & (1 << (flag)))
  106. /*
  107. * top 4 bits of bio flags indicate the pool this bio came from
  108. */
  109. #define BIO_POOL_BITS (4)
  110. #define BIO_POOL_OFFSET (BITS_PER_LONG - BIO_POOL_BITS)
  111. #define BIO_POOL_MASK (1UL << BIO_POOL_OFFSET)
  112. #define BIO_POOL_IDX(bio) ((bio)->bi_flags >> BIO_POOL_OFFSET)
  113. /*
  114. * bio bi_rw flags
  115. *
  116. * bit 0 -- read (not set) or write (set)
  117. * bit 1 -- rw-ahead when set
  118. * bit 2 -- barrier
  119. * bit 3 -- fail fast, don't want low level driver retries
  120. * bit 4 -- synchronous I/O hint: the block layer will unplug immediately
  121. */
  122. #define BIO_RW 0
  123. #define BIO_RW_AHEAD 1
  124. #define BIO_RW_BARRIER 2
  125. #define BIO_RW_FAILFAST 3
  126. #define BIO_RW_SYNC 4
  127. /*
  128. * various member access, note that bio_data should of course not be used
  129. * on highmem page vectors
  130. */
  131. #define bio_iovec_idx(bio, idx) (&((bio)->bi_io_vec[(idx)]))
  132. #define bio_iovec(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  133. #define bio_page(bio) bio_iovec((bio))->bv_page
  134. #define bio_offset(bio) bio_iovec((bio))->bv_offset
  135. #define bio_segments(bio) ((bio)->bi_vcnt - (bio)->bi_idx)
  136. #define bio_sectors(bio) ((bio)->bi_size >> 9)
  137. #define bio_cur_sectors(bio) (bio_iovec(bio)->bv_len >> 9)
  138. #define bio_data(bio) (page_address(bio_page((bio))) + bio_offset((bio)))
  139. #define bio_barrier(bio) ((bio)->bi_rw & (1 << BIO_RW_BARRIER))
  140. #define bio_sync(bio) ((bio)->bi_rw & (1 << BIO_RW_SYNC))
  141. #define bio_failfast(bio) ((bio)->bi_rw & (1 << BIO_RW_FAILFAST))
  142. #define bio_rw_ahead(bio) ((bio)->bi_rw & (1 << BIO_RW_AHEAD))
  143. /*
  144. * will die
  145. */
  146. #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
  147. #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
  148. /*
  149. * queues that have highmem support enabled may still need to revert to
  150. * PIO transfers occasionally and thus map high pages temporarily. For
  151. * permanent PIO fall back, user is probably better off disabling highmem
  152. * I/O completely on that queue (see ide-dma for example)
  153. */
  154. #define __bio_kmap_atomic(bio, idx, kmtype) \
  155. (kmap_atomic(bio_iovec_idx((bio), (idx))->bv_page, kmtype) + \
  156. bio_iovec_idx((bio), (idx))->bv_offset)
  157. #define __bio_kunmap_atomic(addr, kmtype) kunmap_atomic(addr, kmtype)
  158. /*
  159. * merge helpers etc
  160. */
  161. #define __BVEC_END(bio) bio_iovec_idx((bio), (bio)->bi_vcnt - 1)
  162. #define __BVEC_START(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  163. /*
  164. * allow arch override, for eg virtualized architectures (put in asm/io.h)
  165. */
  166. #ifndef BIOVEC_PHYS_MERGEABLE
  167. #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  168. ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
  169. #endif
  170. #define BIOVEC_VIRT_MERGEABLE(vec1, vec2) \
  171. ((((bvec_to_phys((vec1)) + (vec1)->bv_len) | bvec_to_phys((vec2))) & (BIO_VMERGE_BOUNDARY - 1)) == 0)
  172. #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
  173. (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
  174. #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
  175. __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, (q)->seg_boundary_mask)
  176. #define BIO_SEG_BOUNDARY(q, b1, b2) \
  177. BIOVEC_SEG_BOUNDARY((q), __BVEC_END((b1)), __BVEC_START((b2)))
  178. #define bio_io_error(bio, bytes) bio_endio((bio), (bytes), -EIO)
  179. /*
  180. * drivers should not use the __ version unless they _really_ want to
  181. * run through the entire bio and not just pending pieces
  182. */
  183. #define __bio_for_each_segment(bvl, bio, i, start_idx) \
  184. for (bvl = bio_iovec_idx((bio), (start_idx)), i = (start_idx); \
  185. i < (bio)->bi_vcnt; \
  186. bvl++, i++)
  187. #define bio_for_each_segment(bvl, bio, i) \
  188. __bio_for_each_segment(bvl, bio, i, (bio)->bi_idx)
  189. /*
  190. * get a reference to a bio, so it won't disappear. the intended use is
  191. * something like:
  192. *
  193. * bio_get(bio);
  194. * submit_bio(rw, bio);
  195. * if (bio->bi_flags ...)
  196. * do_something
  197. * bio_put(bio);
  198. *
  199. * without the bio_get(), it could potentially complete I/O before submit_bio
  200. * returns. and then bio would be freed memory when if (bio->bi_flags ...)
  201. * runs
  202. */
  203. #define bio_get(bio) atomic_inc(&(bio)->bi_cnt)
  204. /*
  205. * A bio_pair is used when we need to split a bio.
  206. * This can only happen for a bio that refers to just one
  207. * page of data, and in the unusual situation when the
  208. * page crosses a chunk/device boundary
  209. *
  210. * The address of the master bio is stored in bio1.bi_private
  211. * The address of the pool the pair was allocated from is stored
  212. * in bio2.bi_private
  213. */
  214. struct bio_pair {
  215. struct bio bio1, bio2;
  216. struct bio_vec bv1, bv2;
  217. atomic_t cnt;
  218. int error;
  219. };
  220. extern struct bio_pair *bio_split(struct bio *bi, mempool_t *pool,
  221. int first_sectors);
  222. extern mempool_t *bio_split_pool;
  223. extern void bio_pair_release(struct bio_pair *dbio);
  224. extern struct bio_set *bioset_create(int, int, int);
  225. extern void bioset_free(struct bio_set *);
  226. extern struct bio *bio_alloc(unsigned int __nocast, int);
  227. extern struct bio *bio_alloc_bioset(unsigned int __nocast, int, struct bio_set *);
  228. extern void bio_put(struct bio *);
  229. extern void bio_endio(struct bio *, unsigned int, int);
  230. struct request_queue;
  231. extern int bio_phys_segments(struct request_queue *, struct bio *);
  232. extern int bio_hw_segments(struct request_queue *, struct bio *);
  233. extern void __bio_clone(struct bio *, struct bio *);
  234. extern struct bio *bio_clone(struct bio *, unsigned int __nocast);
  235. extern void bio_init(struct bio *);
  236. extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
  237. extern int bio_get_nr_vecs(struct block_device *);
  238. extern struct bio *bio_map_user(struct request_queue *, struct block_device *,
  239. unsigned long, unsigned int, int);
  240. extern void bio_unmap_user(struct bio *);
  241. extern void bio_set_pages_dirty(struct bio *bio);
  242. extern void bio_check_pages_dirty(struct bio *bio);
  243. extern struct bio *bio_copy_user(struct request_queue *, unsigned long, unsigned int, int);
  244. extern int bio_uncopy_user(struct bio *);
  245. void zero_fill_bio(struct bio *bio);
  246. #ifdef CONFIG_HIGHMEM
  247. /*
  248. * remember to add offset! and never ever reenable interrupts between a
  249. * bvec_kmap_irq and bvec_kunmap_irq!!
  250. *
  251. * This function MUST be inlined - it plays with the CPU interrupt flags.
  252. * Hence the `extern inline'.
  253. */
  254. extern inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  255. {
  256. unsigned long addr;
  257. /*
  258. * might not be a highmem page, but the preempt/irq count
  259. * balancing is a lot nicer this way
  260. */
  261. local_irq_save(*flags);
  262. addr = (unsigned long) kmap_atomic(bvec->bv_page, KM_BIO_SRC_IRQ);
  263. BUG_ON(addr & ~PAGE_MASK);
  264. return (char *) addr + bvec->bv_offset;
  265. }
  266. extern inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  267. {
  268. unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
  269. kunmap_atomic((void *) ptr, KM_BIO_SRC_IRQ);
  270. local_irq_restore(*flags);
  271. }
  272. #else
  273. #define bvec_kmap_irq(bvec, flags) (page_address((bvec)->bv_page) + (bvec)->bv_offset)
  274. #define bvec_kunmap_irq(buf, flags) do { *(flags) = 0; } while (0)
  275. #endif
  276. extern inline char *__bio_kmap_irq(struct bio *bio, unsigned short idx,
  277. unsigned long *flags)
  278. {
  279. return bvec_kmap_irq(bio_iovec_idx(bio, idx), flags);
  280. }
  281. #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
  282. #define bio_kmap_irq(bio, flags) \
  283. __bio_kmap_irq((bio), (bio)->bi_idx, (flags))
  284. #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
  285. #endif /* __LINUX_BIO_H */