bio.h 20 KB

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