bio.h 20 KB

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