bio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. *
  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_BIO_H
  21. #define __LINUX_BIO_H
  22. #include <linux/highmem.h>
  23. #include <linux/mempool.h>
  24. #include <linux/ioprio.h>
  25. #ifdef CONFIG_BLOCK
  26. #include <asm/io.h>
  27. /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
  28. #include <linux/blk_types.h>
  29. #define BIO_DEBUG
  30. #ifdef BIO_DEBUG
  31. #define BIO_BUG_ON BUG_ON
  32. #else
  33. #define BIO_BUG_ON
  34. #endif
  35. #define BIO_MAX_PAGES 256
  36. #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_CACHE_SHIFT)
  37. #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
  38. /*
  39. * upper 16 bits of bi_rw define the io priority of this bio
  40. */
  41. #define BIO_PRIO_SHIFT (8 * sizeof(unsigned long) - IOPRIO_BITS)
  42. #define bio_prio(bio) ((bio)->bi_rw >> BIO_PRIO_SHIFT)
  43. #define bio_prio_valid(bio) ioprio_valid(bio_prio(bio))
  44. #define bio_set_prio(bio, prio) do { \
  45. WARN_ON(prio >= (1 << IOPRIO_BITS)); \
  46. (bio)->bi_rw &= ((1UL << BIO_PRIO_SHIFT) - 1); \
  47. (bio)->bi_rw |= ((unsigned long) (prio) << BIO_PRIO_SHIFT); \
  48. } while (0)
  49. /*
  50. * various member access, note that bio_data should of course not be used
  51. * on highmem page vectors
  52. */
  53. #define bio_iovec_idx(bio, idx) (&((bio)->bi_io_vec[(idx)]))
  54. #define bio_iovec(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  55. #define bio_page(bio) bio_iovec((bio))->bv_page
  56. #define bio_offset(bio) bio_iovec((bio))->bv_offset
  57. #define bio_segments(bio) ((bio)->bi_vcnt - (bio)->bi_idx)
  58. #define bio_sectors(bio) ((bio)->bi_size >> 9)
  59. static inline unsigned int bio_cur_bytes(struct bio *bio)
  60. {
  61. if (bio->bi_vcnt)
  62. return bio_iovec(bio)->bv_len;
  63. else /* dataless requests such as discard */
  64. return bio->bi_size;
  65. }
  66. static inline void *bio_data(struct bio *bio)
  67. {
  68. if (bio->bi_vcnt)
  69. return page_address(bio_page(bio)) + bio_offset(bio);
  70. return NULL;
  71. }
  72. static inline int bio_has_allocated_vec(struct bio *bio)
  73. {
  74. return bio->bi_io_vec && bio->bi_io_vec != bio->bi_inline_vecs;
  75. }
  76. /*
  77. * will die
  78. */
  79. #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
  80. #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
  81. /*
  82. * queues that have highmem support enabled may still need to revert to
  83. * PIO transfers occasionally and thus map high pages temporarily. For
  84. * permanent PIO fall back, user is probably better off disabling highmem
  85. * I/O completely on that queue (see ide-dma for example)
  86. */
  87. #define __bio_kmap_atomic(bio, idx, kmtype) \
  88. (kmap_atomic(bio_iovec_idx((bio), (idx))->bv_page) + \
  89. bio_iovec_idx((bio), (idx))->bv_offset)
  90. #define __bio_kunmap_atomic(addr, kmtype) kunmap_atomic(addr)
  91. /*
  92. * merge helpers etc
  93. */
  94. #define __BVEC_END(bio) bio_iovec_idx((bio), (bio)->bi_vcnt - 1)
  95. #define __BVEC_START(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  96. /* Default implementation of BIOVEC_PHYS_MERGEABLE */
  97. #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  98. ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
  99. /*
  100. * allow arch override, for eg virtualized architectures (put in asm/io.h)
  101. */
  102. #ifndef BIOVEC_PHYS_MERGEABLE
  103. #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  104. __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
  105. #endif
  106. #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
  107. (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
  108. #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
  109. __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
  110. #define BIO_SEG_BOUNDARY(q, b1, b2) \
  111. BIOVEC_SEG_BOUNDARY((q), __BVEC_END((b1)), __BVEC_START((b2)))
  112. #define bio_io_error(bio) bio_endio((bio), -EIO)
  113. /*
  114. * drivers should not use the __ version unless they _really_ want to
  115. * run through the entire bio and not just pending pieces
  116. */
  117. #define __bio_for_each_segment(bvl, bio, i, start_idx) \
  118. for (bvl = bio_iovec_idx((bio), (start_idx)), i = (start_idx); \
  119. i < (bio)->bi_vcnt; \
  120. bvl++, i++)
  121. #define bio_for_each_segment(bvl, bio, i) \
  122. __bio_for_each_segment(bvl, bio, i, (bio)->bi_idx)
  123. /*
  124. * get a reference to a bio, so it won't disappear. the intended use is
  125. * something like:
  126. *
  127. * bio_get(bio);
  128. * submit_bio(rw, bio);
  129. * if (bio->bi_flags ...)
  130. * do_something
  131. * bio_put(bio);
  132. *
  133. * without the bio_get(), it could potentially complete I/O before submit_bio
  134. * returns. and then bio would be freed memory when if (bio->bi_flags ...)
  135. * runs
  136. */
  137. #define bio_get(bio) atomic_inc(&(bio)->bi_cnt)
  138. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  139. /*
  140. * bio integrity payload
  141. */
  142. struct bio_integrity_payload {
  143. struct bio *bip_bio; /* parent bio */
  144. sector_t bip_sector; /* virtual start sector */
  145. void *bip_buf; /* generated integrity data */
  146. bio_end_io_t *bip_end_io; /* saved I/O completion fn */
  147. unsigned int bip_size;
  148. unsigned short bip_slab; /* slab the bip came from */
  149. unsigned short bip_vcnt; /* # of integrity bio_vecs */
  150. unsigned short bip_idx; /* current bip_vec index */
  151. struct work_struct bip_work; /* I/O completion */
  152. struct bio_vec bip_vec[0]; /* embedded bvec array */
  153. };
  154. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  155. /*
  156. * A bio_pair is used when we need to split a bio.
  157. * This can only happen for a bio that refers to just one
  158. * page of data, and in the unusual situation when the
  159. * page crosses a chunk/device boundary
  160. *
  161. * The address of the master bio is stored in bio1.bi_private
  162. * The address of the pool the pair was allocated from is stored
  163. * in bio2.bi_private
  164. */
  165. struct bio_pair {
  166. struct bio bio1, bio2;
  167. struct bio_vec bv1, bv2;
  168. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  169. struct bio_integrity_payload bip1, bip2;
  170. struct bio_vec iv1, iv2;
  171. #endif
  172. atomic_t cnt;
  173. int error;
  174. };
  175. extern struct bio_pair *bio_split(struct bio *bi, int first_sectors);
  176. extern void bio_pair_release(struct bio_pair *dbio);
  177. extern struct bio_set *bioset_create(unsigned int, unsigned int);
  178. extern void bioset_free(struct bio_set *);
  179. extern struct bio *bio_alloc(gfp_t, unsigned int);
  180. extern struct bio *bio_kmalloc(gfp_t, unsigned int);
  181. extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
  182. extern void bio_put(struct bio *);
  183. extern void bio_free(struct bio *, struct bio_set *);
  184. extern void bio_endio(struct bio *, int);
  185. struct request_queue;
  186. extern int bio_phys_segments(struct request_queue *, struct bio *);
  187. extern void __bio_clone(struct bio *, struct bio *);
  188. extern struct bio *bio_clone(struct bio *, gfp_t);
  189. extern void bio_init(struct bio *);
  190. extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
  191. extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
  192. unsigned int, unsigned int);
  193. extern int bio_get_nr_vecs(struct block_device *);
  194. extern sector_t bio_sector_offset(struct bio *, unsigned short, unsigned int);
  195. extern struct bio *bio_map_user(struct request_queue *, struct block_device *,
  196. unsigned long, unsigned int, int, gfp_t);
  197. struct sg_iovec;
  198. struct rq_map_data;
  199. extern struct bio *bio_map_user_iov(struct request_queue *,
  200. struct block_device *,
  201. struct sg_iovec *, int, int, gfp_t);
  202. extern void bio_unmap_user(struct bio *);
  203. extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
  204. gfp_t);
  205. extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
  206. gfp_t, int);
  207. extern void bio_set_pages_dirty(struct bio *bio);
  208. extern void bio_check_pages_dirty(struct bio *bio);
  209. #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  210. # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
  211. #endif
  212. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  213. extern void bio_flush_dcache_pages(struct bio *bi);
  214. #else
  215. static inline void bio_flush_dcache_pages(struct bio *bi)
  216. {
  217. }
  218. #endif
  219. extern struct bio *bio_copy_user(struct request_queue *, struct rq_map_data *,
  220. unsigned long, unsigned int, int, gfp_t);
  221. extern struct bio *bio_copy_user_iov(struct request_queue *,
  222. struct rq_map_data *, struct sg_iovec *,
  223. int, int, gfp_t);
  224. extern int bio_uncopy_user(struct bio *);
  225. void zero_fill_bio(struct bio *bio);
  226. extern struct bio_vec *bvec_alloc_bs(gfp_t, int, unsigned long *, struct bio_set *);
  227. extern void bvec_free_bs(struct bio_set *, struct bio_vec *, unsigned int);
  228. extern unsigned int bvec_nr_vecs(unsigned short idx);
  229. /*
  230. * bio_set is used to allow other portions of the IO system to
  231. * allocate their own private memory pools for bio and iovec structures.
  232. * These memory pools in turn all allocate from the bio_slab
  233. * and the bvec_slabs[].
  234. */
  235. #define BIO_POOL_SIZE 2
  236. #define BIOVEC_NR_POOLS 6
  237. #define BIOVEC_MAX_IDX (BIOVEC_NR_POOLS - 1)
  238. struct bio_set {
  239. struct kmem_cache *bio_slab;
  240. unsigned int front_pad;
  241. mempool_t *bio_pool;
  242. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  243. mempool_t *bio_integrity_pool;
  244. #endif
  245. mempool_t *bvec_pool;
  246. };
  247. struct biovec_slab {
  248. int nr_vecs;
  249. char *name;
  250. struct kmem_cache *slab;
  251. };
  252. extern struct bio_set *fs_bio_set;
  253. /*
  254. * a small number of entries is fine, not going to be performance critical.
  255. * basically we just need to survive
  256. */
  257. #define BIO_SPLIT_ENTRIES 2
  258. #ifdef CONFIG_HIGHMEM
  259. /*
  260. * remember never ever reenable interrupts between a bvec_kmap_irq and
  261. * bvec_kunmap_irq!
  262. */
  263. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  264. {
  265. unsigned long addr;
  266. /*
  267. * might not be a highmem page, but the preempt/irq count
  268. * balancing is a lot nicer this way
  269. */
  270. local_irq_save(*flags);
  271. addr = (unsigned long) kmap_atomic(bvec->bv_page);
  272. BUG_ON(addr & ~PAGE_MASK);
  273. return (char *) addr + bvec->bv_offset;
  274. }
  275. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  276. {
  277. unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
  278. kunmap_atomic((void *) ptr);
  279. local_irq_restore(*flags);
  280. }
  281. #else
  282. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  283. {
  284. return page_address(bvec->bv_page) + bvec->bv_offset;
  285. }
  286. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  287. {
  288. *flags = 0;
  289. }
  290. #endif
  291. static inline char *__bio_kmap_irq(struct bio *bio, unsigned short idx,
  292. unsigned long *flags)
  293. {
  294. return bvec_kmap_irq(bio_iovec_idx(bio, idx), flags);
  295. }
  296. #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
  297. #define bio_kmap_irq(bio, flags) \
  298. __bio_kmap_irq((bio), (bio)->bi_idx, (flags))
  299. #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
  300. /*
  301. * Check whether this bio carries any data or not. A NULL bio is allowed.
  302. */
  303. static inline int bio_has_data(struct bio *bio)
  304. {
  305. return bio && bio->bi_io_vec != NULL;
  306. }
  307. /*
  308. * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
  309. *
  310. * A bio_list anchors a singly-linked list of bios chained through the bi_next
  311. * member of the bio. The bio_list also caches the last list member to allow
  312. * fast access to the tail.
  313. */
  314. struct bio_list {
  315. struct bio *head;
  316. struct bio *tail;
  317. };
  318. static inline int bio_list_empty(const struct bio_list *bl)
  319. {
  320. return bl->head == NULL;
  321. }
  322. static inline void bio_list_init(struct bio_list *bl)
  323. {
  324. bl->head = bl->tail = NULL;
  325. }
  326. #define bio_list_for_each(bio, bl) \
  327. for (bio = (bl)->head; bio; bio = bio->bi_next)
  328. static inline unsigned bio_list_size(const struct bio_list *bl)
  329. {
  330. unsigned sz = 0;
  331. struct bio *bio;
  332. bio_list_for_each(bio, bl)
  333. sz++;
  334. return sz;
  335. }
  336. static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
  337. {
  338. bio->bi_next = NULL;
  339. if (bl->tail)
  340. bl->tail->bi_next = bio;
  341. else
  342. bl->head = bio;
  343. bl->tail = bio;
  344. }
  345. static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
  346. {
  347. bio->bi_next = bl->head;
  348. bl->head = bio;
  349. if (!bl->tail)
  350. bl->tail = bio;
  351. }
  352. static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
  353. {
  354. if (!bl2->head)
  355. return;
  356. if (bl->tail)
  357. bl->tail->bi_next = bl2->head;
  358. else
  359. bl->head = bl2->head;
  360. bl->tail = bl2->tail;
  361. }
  362. static inline void bio_list_merge_head(struct bio_list *bl,
  363. struct bio_list *bl2)
  364. {
  365. if (!bl2->head)
  366. return;
  367. if (bl->head)
  368. bl2->tail->bi_next = bl->head;
  369. else
  370. bl->tail = bl2->tail;
  371. bl->head = bl2->head;
  372. }
  373. static inline struct bio *bio_list_peek(struct bio_list *bl)
  374. {
  375. return bl->head;
  376. }
  377. static inline struct bio *bio_list_pop(struct bio_list *bl)
  378. {
  379. struct bio *bio = bl->head;
  380. if (bio) {
  381. bl->head = bl->head->bi_next;
  382. if (!bl->head)
  383. bl->tail = NULL;
  384. bio->bi_next = NULL;
  385. }
  386. return bio;
  387. }
  388. static inline struct bio *bio_list_get(struct bio_list *bl)
  389. {
  390. struct bio *bio = bl->head;
  391. bl->head = bl->tail = NULL;
  392. return bio;
  393. }
  394. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  395. #define bip_vec_idx(bip, idx) (&(bip->bip_vec[(idx)]))
  396. #define bip_vec(bip) bip_vec_idx(bip, 0)
  397. #define __bip_for_each_vec(bvl, bip, i, start_idx) \
  398. for (bvl = bip_vec_idx((bip), (start_idx)), i = (start_idx); \
  399. i < (bip)->bip_vcnt; \
  400. bvl++, i++)
  401. #define bip_for_each_vec(bvl, bip, i) \
  402. __bip_for_each_vec(bvl, bip, i, (bip)->bip_idx)
  403. #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
  404. for_each_bio(_bio) \
  405. bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
  406. #define bio_integrity(bio) (bio->bi_integrity != NULL)
  407. extern struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *, gfp_t, unsigned int, struct bio_set *);
  408. extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
  409. extern void bio_integrity_free(struct bio *, struct bio_set *);
  410. extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
  411. extern int bio_integrity_enabled(struct bio *bio);
  412. extern int bio_integrity_set_tag(struct bio *, void *, unsigned int);
  413. extern int bio_integrity_get_tag(struct bio *, void *, unsigned int);
  414. extern int bio_integrity_prep(struct bio *);
  415. extern void bio_integrity_endio(struct bio *, int);
  416. extern void bio_integrity_advance(struct bio *, unsigned int);
  417. extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
  418. extern void bio_integrity_split(struct bio *, struct bio_pair *, int);
  419. extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t, struct bio_set *);
  420. extern int bioset_integrity_create(struct bio_set *, int);
  421. extern void bioset_integrity_free(struct bio_set *);
  422. extern void bio_integrity_init(void);
  423. #else /* CONFIG_BLK_DEV_INTEGRITY */
  424. static inline int bio_integrity(struct bio *bio)
  425. {
  426. return 0;
  427. }
  428. static inline int bio_integrity_enabled(struct bio *bio)
  429. {
  430. return 0;
  431. }
  432. static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
  433. {
  434. return 0;
  435. }
  436. static inline void bioset_integrity_free (struct bio_set *bs)
  437. {
  438. return;
  439. }
  440. static inline int bio_integrity_prep(struct bio *bio)
  441. {
  442. return 0;
  443. }
  444. static inline void bio_integrity_free(struct bio *bio, struct bio_set *bs)
  445. {
  446. return;
  447. }
  448. static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  449. gfp_t gfp_mask, struct bio_set *bs)
  450. {
  451. return 0;
  452. }
  453. static inline void bio_integrity_split(struct bio *bio, struct bio_pair *bp,
  454. int sectors)
  455. {
  456. return;
  457. }
  458. static inline void bio_integrity_advance(struct bio *bio,
  459. unsigned int bytes_done)
  460. {
  461. return;
  462. }
  463. static inline void bio_integrity_trim(struct bio *bio, unsigned int offset,
  464. unsigned int sectors)
  465. {
  466. return;
  467. }
  468. static inline void bio_integrity_init(void)
  469. {
  470. return;
  471. }
  472. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  473. #endif /* CONFIG_BLOCK */
  474. #endif /* __LINUX_BIO_H */