bio.h 21 KB

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