bio.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public Licens
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  17. */
  18. #ifndef __LINUX_BIO_H
  19. #define __LINUX_BIO_H
  20. #include <linux/highmem.h>
  21. #include <linux/mempool.h>
  22. #include <linux/ioprio.h>
  23. #include <linux/bug.h>
  24. #ifdef CONFIG_BLOCK
  25. #include <asm/io.h>
  26. /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
  27. #include <linux/blk_types.h>
  28. #define BIO_DEBUG
  29. #ifdef BIO_DEBUG
  30. #define BIO_BUG_ON BUG_ON
  31. #else
  32. #define BIO_BUG_ON
  33. #endif
  34. #define BIO_MAX_PAGES 256
  35. #define bio_prio(bio) (bio)->bi_ioprio
  36. #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
  37. #define bio_iter_iovec(bio, iter) \
  38. bvec_iter_bvec((bio)->bi_io_vec, (iter))
  39. #define bio_iter_page(bio, iter) \
  40. bvec_iter_page((bio)->bi_io_vec, (iter))
  41. #define bio_iter_len(bio, iter) \
  42. bvec_iter_len((bio)->bi_io_vec, (iter))
  43. #define bio_iter_offset(bio, iter) \
  44. bvec_iter_offset((bio)->bi_io_vec, (iter))
  45. #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
  46. #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
  47. #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
  48. #define bio_multiple_segments(bio) \
  49. ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
  50. #define bio_sectors(bio) ((bio)->bi_iter.bi_size >> 9)
  51. #define bio_end_sector(bio) ((bio)->bi_iter.bi_sector + bio_sectors((bio)))
  52. /*
  53. * Check whether this bio carries any data or not. A NULL bio is allowed.
  54. */
  55. static inline bool bio_has_data(struct bio *bio)
  56. {
  57. if (bio &&
  58. bio->bi_iter.bi_size &&
  59. bio_op(bio) != REQ_OP_DISCARD &&
  60. bio_op(bio) != REQ_OP_SECURE_ERASE)
  61. return true;
  62. return false;
  63. }
  64. static inline bool bio_no_advance_iter(struct bio *bio)
  65. {
  66. return bio_op(bio) == REQ_OP_DISCARD ||
  67. bio_op(bio) == REQ_OP_SECURE_ERASE ||
  68. bio_op(bio) == REQ_OP_WRITE_SAME;
  69. }
  70. static inline bool bio_is_rw(struct bio *bio)
  71. {
  72. if (!bio_has_data(bio))
  73. return false;
  74. if (bio_no_advance_iter(bio))
  75. return false;
  76. return true;
  77. }
  78. static inline bool bio_mergeable(struct bio *bio)
  79. {
  80. if (bio->bi_opf & REQ_NOMERGE_FLAGS)
  81. return false;
  82. return true;
  83. }
  84. static inline unsigned int bio_cur_bytes(struct bio *bio)
  85. {
  86. if (bio_has_data(bio))
  87. return bio_iovec(bio).bv_len;
  88. else /* dataless requests such as discard */
  89. return bio->bi_iter.bi_size;
  90. }
  91. static inline void *bio_data(struct bio *bio)
  92. {
  93. if (bio_has_data(bio))
  94. return page_address(bio_page(bio)) + bio_offset(bio);
  95. return NULL;
  96. }
  97. /*
  98. * will die
  99. */
  100. #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
  101. #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
  102. /*
  103. * queues that have highmem support enabled may still need to revert to
  104. * PIO transfers occasionally and thus map high pages temporarily. For
  105. * permanent PIO fall back, user is probably better off disabling highmem
  106. * I/O completely on that queue (see ide-dma for example)
  107. */
  108. #define __bio_kmap_atomic(bio, iter) \
  109. (kmap_atomic(bio_iter_iovec((bio), (iter)).bv_page) + \
  110. bio_iter_iovec((bio), (iter)).bv_offset)
  111. #define __bio_kunmap_atomic(addr) kunmap_atomic(addr)
  112. /*
  113. * merge helpers etc
  114. */
  115. /* Default implementation of BIOVEC_PHYS_MERGEABLE */
  116. #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  117. ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
  118. /*
  119. * allow arch override, for eg virtualized architectures (put in asm/io.h)
  120. */
  121. #ifndef BIOVEC_PHYS_MERGEABLE
  122. #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  123. __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
  124. #endif
  125. #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
  126. (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
  127. #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
  128. __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
  129. /*
  130. * drivers should _never_ use the all version - the bio may have been split
  131. * before it got to the driver and the driver won't own all of it
  132. */
  133. #define bio_for_each_segment_all(bvl, bio, i) \
  134. for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
  135. static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
  136. unsigned bytes)
  137. {
  138. iter->bi_sector += bytes >> 9;
  139. if (bio_no_advance_iter(bio))
  140. iter->bi_size -= bytes;
  141. else
  142. bvec_iter_advance(bio->bi_io_vec, iter, bytes);
  143. }
  144. #define __bio_for_each_segment(bvl, bio, iter, start) \
  145. for (iter = (start); \
  146. (iter).bi_size && \
  147. ((bvl = bio_iter_iovec((bio), (iter))), 1); \
  148. bio_advance_iter((bio), &(iter), (bvl).bv_len))
  149. #define bio_for_each_segment(bvl, bio, iter) \
  150. __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
  151. #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
  152. static inline unsigned bio_segments(struct bio *bio)
  153. {
  154. unsigned segs = 0;
  155. struct bio_vec bv;
  156. struct bvec_iter iter;
  157. /*
  158. * We special case discard/write same, because they interpret bi_size
  159. * differently:
  160. */
  161. if (bio_op(bio) == REQ_OP_DISCARD)
  162. return 1;
  163. if (bio_op(bio) == REQ_OP_SECURE_ERASE)
  164. return 1;
  165. if (bio_op(bio) == REQ_OP_WRITE_SAME)
  166. return 1;
  167. bio_for_each_segment(bv, bio, iter)
  168. segs++;
  169. return segs;
  170. }
  171. /*
  172. * get a reference to a bio, so it won't disappear. the intended use is
  173. * something like:
  174. *
  175. * bio_get(bio);
  176. * submit_bio(rw, bio);
  177. * if (bio->bi_flags ...)
  178. * do_something
  179. * bio_put(bio);
  180. *
  181. * without the bio_get(), it could potentially complete I/O before submit_bio
  182. * returns. and then bio would be freed memory when if (bio->bi_flags ...)
  183. * runs
  184. */
  185. static inline void bio_get(struct bio *bio)
  186. {
  187. bio->bi_flags |= (1 << BIO_REFFED);
  188. smp_mb__before_atomic();
  189. atomic_inc(&bio->__bi_cnt);
  190. }
  191. static inline void bio_cnt_set(struct bio *bio, unsigned int count)
  192. {
  193. if (count != 1) {
  194. bio->bi_flags |= (1 << BIO_REFFED);
  195. smp_mb__before_atomic();
  196. }
  197. atomic_set(&bio->__bi_cnt, count);
  198. }
  199. static inline bool bio_flagged(struct bio *bio, unsigned int bit)
  200. {
  201. return (bio->bi_flags & (1U << bit)) != 0;
  202. }
  203. static inline void bio_set_flag(struct bio *bio, unsigned int bit)
  204. {
  205. bio->bi_flags |= (1U << bit);
  206. }
  207. static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
  208. {
  209. bio->bi_flags &= ~(1U << bit);
  210. }
  211. static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
  212. {
  213. *bv = bio_iovec(bio);
  214. }
  215. static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
  216. {
  217. struct bvec_iter iter = bio->bi_iter;
  218. int idx;
  219. if (unlikely(!bio_multiple_segments(bio))) {
  220. *bv = bio_iovec(bio);
  221. return;
  222. }
  223. bio_advance_iter(bio, &iter, iter.bi_size);
  224. if (!iter.bi_bvec_done)
  225. idx = iter.bi_idx - 1;
  226. else /* in the middle of bvec */
  227. idx = iter.bi_idx;
  228. *bv = bio->bi_io_vec[idx];
  229. /*
  230. * iter.bi_bvec_done records actual length of the last bvec
  231. * if this bio ends in the middle of one io vector
  232. */
  233. if (iter.bi_bvec_done)
  234. bv->bv_len = iter.bi_bvec_done;
  235. }
  236. enum bip_flags {
  237. BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
  238. BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
  239. BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
  240. BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
  241. BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
  242. };
  243. /*
  244. * bio integrity payload
  245. */
  246. struct bio_integrity_payload {
  247. struct bio *bip_bio; /* parent bio */
  248. struct bvec_iter bip_iter;
  249. bio_end_io_t *bip_end_io; /* saved I/O completion fn */
  250. unsigned short bip_slab; /* slab the bip came from */
  251. unsigned short bip_vcnt; /* # of integrity bio_vecs */
  252. unsigned short bip_max_vcnt; /* integrity bio_vec slots */
  253. unsigned short bip_flags; /* control flags */
  254. struct work_struct bip_work; /* I/O completion */
  255. struct bio_vec *bip_vec;
  256. struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
  257. };
  258. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  259. static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
  260. {
  261. if (bio->bi_opf & REQ_INTEGRITY)
  262. return bio->bi_integrity;
  263. return NULL;
  264. }
  265. static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
  266. {
  267. struct bio_integrity_payload *bip = bio_integrity(bio);
  268. if (bip)
  269. return bip->bip_flags & flag;
  270. return false;
  271. }
  272. static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
  273. {
  274. return bip->bip_iter.bi_sector;
  275. }
  276. static inline void bip_set_seed(struct bio_integrity_payload *bip,
  277. sector_t seed)
  278. {
  279. bip->bip_iter.bi_sector = seed;
  280. }
  281. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  282. extern void bio_trim(struct bio *bio, int offset, int size);
  283. extern struct bio *bio_split(struct bio *bio, int sectors,
  284. gfp_t gfp, struct bio_set *bs);
  285. /**
  286. * bio_next_split - get next @sectors from a bio, splitting if necessary
  287. * @bio: bio to split
  288. * @sectors: number of sectors to split from the front of @bio
  289. * @gfp: gfp mask
  290. * @bs: bio set to allocate from
  291. *
  292. * Returns a bio representing the next @sectors of @bio - if the bio is smaller
  293. * than @sectors, returns the original bio unchanged.
  294. */
  295. static inline struct bio *bio_next_split(struct bio *bio, int sectors,
  296. gfp_t gfp, struct bio_set *bs)
  297. {
  298. if (sectors >= bio_sectors(bio))
  299. return bio;
  300. return bio_split(bio, sectors, gfp, bs);
  301. }
  302. extern struct bio_set *bioset_create(unsigned int, unsigned int);
  303. extern struct bio_set *bioset_create_nobvec(unsigned int, unsigned int);
  304. extern void bioset_free(struct bio_set *);
  305. extern mempool_t *biovec_create_pool(int pool_entries);
  306. extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
  307. extern void bio_put(struct bio *);
  308. extern void __bio_clone_fast(struct bio *, struct bio *);
  309. extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
  310. extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
  311. extern struct bio_set *fs_bio_set;
  312. static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  313. {
  314. return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
  315. }
  316. static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
  317. {
  318. return bio_clone_bioset(bio, gfp_mask, fs_bio_set);
  319. }
  320. static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
  321. {
  322. return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
  323. }
  324. static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
  325. {
  326. return bio_clone_bioset(bio, gfp_mask, NULL);
  327. }
  328. extern void bio_endio(struct bio *);
  329. static inline void bio_io_error(struct bio *bio)
  330. {
  331. bio->bi_error = -EIO;
  332. bio_endio(bio);
  333. }
  334. struct request_queue;
  335. extern int bio_phys_segments(struct request_queue *, struct bio *);
  336. extern int submit_bio_wait(struct bio *bio);
  337. extern void bio_advance(struct bio *, unsigned);
  338. extern void bio_init(struct bio *);
  339. extern void bio_reset(struct bio *);
  340. void bio_chain(struct bio *, struct bio *);
  341. extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
  342. extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
  343. unsigned int, unsigned int);
  344. struct rq_map_data;
  345. extern struct bio *bio_map_user_iov(struct request_queue *,
  346. const struct iov_iter *, gfp_t);
  347. extern void bio_unmap_user(struct bio *);
  348. extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
  349. gfp_t);
  350. extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
  351. gfp_t, int);
  352. extern void bio_set_pages_dirty(struct bio *bio);
  353. extern void bio_check_pages_dirty(struct bio *bio);
  354. void generic_start_io_acct(int rw, unsigned long sectors,
  355. struct hd_struct *part);
  356. void generic_end_io_acct(int rw, struct hd_struct *part,
  357. unsigned long start_time);
  358. #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  359. # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
  360. #endif
  361. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  362. extern void bio_flush_dcache_pages(struct bio *bi);
  363. #else
  364. static inline void bio_flush_dcache_pages(struct bio *bi)
  365. {
  366. }
  367. #endif
  368. extern void bio_copy_data(struct bio *dst, struct bio *src);
  369. extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
  370. extern void bio_free_pages(struct bio *bio);
  371. extern struct bio *bio_copy_user_iov(struct request_queue *,
  372. struct rq_map_data *,
  373. const struct iov_iter *,
  374. gfp_t);
  375. extern int bio_uncopy_user(struct bio *);
  376. void zero_fill_bio(struct bio *bio);
  377. extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
  378. extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
  379. extern unsigned int bvec_nr_vecs(unsigned short idx);
  380. #ifdef CONFIG_BLK_CGROUP
  381. int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
  382. int bio_associate_current(struct bio *bio);
  383. void bio_disassociate_task(struct bio *bio);
  384. void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
  385. #else /* CONFIG_BLK_CGROUP */
  386. static inline int bio_associate_blkcg(struct bio *bio,
  387. struct cgroup_subsys_state *blkcg_css) { return 0; }
  388. static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
  389. static inline void bio_disassociate_task(struct bio *bio) { }
  390. static inline void bio_clone_blkcg_association(struct bio *dst,
  391. struct bio *src) { }
  392. #endif /* CONFIG_BLK_CGROUP */
  393. #ifdef CONFIG_HIGHMEM
  394. /*
  395. * remember never ever reenable interrupts between a bvec_kmap_irq and
  396. * bvec_kunmap_irq!
  397. */
  398. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  399. {
  400. unsigned long addr;
  401. /*
  402. * might not be a highmem page, but the preempt/irq count
  403. * balancing is a lot nicer this way
  404. */
  405. local_irq_save(*flags);
  406. addr = (unsigned long) kmap_atomic(bvec->bv_page);
  407. BUG_ON(addr & ~PAGE_MASK);
  408. return (char *) addr + bvec->bv_offset;
  409. }
  410. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  411. {
  412. unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
  413. kunmap_atomic((void *) ptr);
  414. local_irq_restore(*flags);
  415. }
  416. #else
  417. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  418. {
  419. return page_address(bvec->bv_page) + bvec->bv_offset;
  420. }
  421. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  422. {
  423. *flags = 0;
  424. }
  425. #endif
  426. static inline char *__bio_kmap_irq(struct bio *bio, struct bvec_iter iter,
  427. unsigned long *flags)
  428. {
  429. return bvec_kmap_irq(&bio_iter_iovec(bio, iter), flags);
  430. }
  431. #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
  432. #define bio_kmap_irq(bio, flags) \
  433. __bio_kmap_irq((bio), (bio)->bi_iter, (flags))
  434. #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
  435. /*
  436. * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
  437. *
  438. * A bio_list anchors a singly-linked list of bios chained through the bi_next
  439. * member of the bio. The bio_list also caches the last list member to allow
  440. * fast access to the tail.
  441. */
  442. struct bio_list {
  443. struct bio *head;
  444. struct bio *tail;
  445. };
  446. static inline int bio_list_empty(const struct bio_list *bl)
  447. {
  448. return bl->head == NULL;
  449. }
  450. static inline void bio_list_init(struct bio_list *bl)
  451. {
  452. bl->head = bl->tail = NULL;
  453. }
  454. #define BIO_EMPTY_LIST { NULL, NULL }
  455. #define bio_list_for_each(bio, bl) \
  456. for (bio = (bl)->head; bio; bio = bio->bi_next)
  457. static inline unsigned bio_list_size(const struct bio_list *bl)
  458. {
  459. unsigned sz = 0;
  460. struct bio *bio;
  461. bio_list_for_each(bio, bl)
  462. sz++;
  463. return sz;
  464. }
  465. static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
  466. {
  467. bio->bi_next = NULL;
  468. if (bl->tail)
  469. bl->tail->bi_next = bio;
  470. else
  471. bl->head = bio;
  472. bl->tail = bio;
  473. }
  474. static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
  475. {
  476. bio->bi_next = bl->head;
  477. bl->head = bio;
  478. if (!bl->tail)
  479. bl->tail = bio;
  480. }
  481. static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
  482. {
  483. if (!bl2->head)
  484. return;
  485. if (bl->tail)
  486. bl->tail->bi_next = bl2->head;
  487. else
  488. bl->head = bl2->head;
  489. bl->tail = bl2->tail;
  490. }
  491. static inline void bio_list_merge_head(struct bio_list *bl,
  492. struct bio_list *bl2)
  493. {
  494. if (!bl2->head)
  495. return;
  496. if (bl->head)
  497. bl2->tail->bi_next = bl->head;
  498. else
  499. bl->tail = bl2->tail;
  500. bl->head = bl2->head;
  501. }
  502. static inline struct bio *bio_list_peek(struct bio_list *bl)
  503. {
  504. return bl->head;
  505. }
  506. static inline struct bio *bio_list_pop(struct bio_list *bl)
  507. {
  508. struct bio *bio = bl->head;
  509. if (bio) {
  510. bl->head = bl->head->bi_next;
  511. if (!bl->head)
  512. bl->tail = NULL;
  513. bio->bi_next = NULL;
  514. }
  515. return bio;
  516. }
  517. static inline struct bio *bio_list_get(struct bio_list *bl)
  518. {
  519. struct bio *bio = bl->head;
  520. bl->head = bl->tail = NULL;
  521. return bio;
  522. }
  523. /*
  524. * Increment chain count for the bio. Make sure the CHAIN flag update
  525. * is visible before the raised count.
  526. */
  527. static inline void bio_inc_remaining(struct bio *bio)
  528. {
  529. bio_set_flag(bio, BIO_CHAIN);
  530. smp_mb__before_atomic();
  531. atomic_inc(&bio->__bi_remaining);
  532. }
  533. /*
  534. * bio_set is used to allow other portions of the IO system to
  535. * allocate their own private memory pools for bio and iovec structures.
  536. * These memory pools in turn all allocate from the bio_slab
  537. * and the bvec_slabs[].
  538. */
  539. #define BIO_POOL_SIZE 2
  540. struct bio_set {
  541. struct kmem_cache *bio_slab;
  542. unsigned int front_pad;
  543. mempool_t *bio_pool;
  544. mempool_t *bvec_pool;
  545. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  546. mempool_t *bio_integrity_pool;
  547. mempool_t *bvec_integrity_pool;
  548. #endif
  549. /*
  550. * Deadlock avoidance for stacking block drivers: see comments in
  551. * bio_alloc_bioset() for details
  552. */
  553. spinlock_t rescue_lock;
  554. struct bio_list rescue_list;
  555. struct work_struct rescue_work;
  556. struct workqueue_struct *rescue_workqueue;
  557. };
  558. struct biovec_slab {
  559. int nr_vecs;
  560. char *name;
  561. struct kmem_cache *slab;
  562. };
  563. /*
  564. * a small number of entries is fine, not going to be performance critical.
  565. * basically we just need to survive
  566. */
  567. #define BIO_SPLIT_ENTRIES 2
  568. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  569. #define bip_for_each_vec(bvl, bip, iter) \
  570. for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
  571. #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
  572. for_each_bio(_bio) \
  573. bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
  574. extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
  575. extern void bio_integrity_free(struct bio *);
  576. extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
  577. extern bool bio_integrity_enabled(struct bio *bio);
  578. extern int bio_integrity_prep(struct bio *);
  579. extern void bio_integrity_endio(struct bio *);
  580. extern void bio_integrity_advance(struct bio *, unsigned int);
  581. extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
  582. extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
  583. extern int bioset_integrity_create(struct bio_set *, int);
  584. extern void bioset_integrity_free(struct bio_set *);
  585. extern void bio_integrity_init(void);
  586. #else /* CONFIG_BLK_DEV_INTEGRITY */
  587. static inline void *bio_integrity(struct bio *bio)
  588. {
  589. return NULL;
  590. }
  591. static inline bool bio_integrity_enabled(struct bio *bio)
  592. {
  593. return false;
  594. }
  595. static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
  596. {
  597. return 0;
  598. }
  599. static inline void bioset_integrity_free (struct bio_set *bs)
  600. {
  601. return;
  602. }
  603. static inline int bio_integrity_prep(struct bio *bio)
  604. {
  605. return 0;
  606. }
  607. static inline void bio_integrity_free(struct bio *bio)
  608. {
  609. return;
  610. }
  611. static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  612. gfp_t gfp_mask)
  613. {
  614. return 0;
  615. }
  616. static inline void bio_integrity_advance(struct bio *bio,
  617. unsigned int bytes_done)
  618. {
  619. return;
  620. }
  621. static inline void bio_integrity_trim(struct bio *bio, unsigned int offset,
  622. unsigned int sectors)
  623. {
  624. return;
  625. }
  626. static inline void bio_integrity_init(void)
  627. {
  628. return;
  629. }
  630. static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
  631. {
  632. return false;
  633. }
  634. static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
  635. unsigned int nr)
  636. {
  637. return ERR_PTR(-EINVAL);
  638. }
  639. static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
  640. unsigned int len, unsigned int offset)
  641. {
  642. return 0;
  643. }
  644. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  645. #endif /* CONFIG_BLOCK */
  646. #endif /* __LINUX_BIO_H */