bio.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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(struct request_queue *q, int rw,
  378. unsigned long sectors, struct hd_struct *part);
  379. void generic_end_io_acct(struct request_queue *q, int rw,
  380. struct hd_struct *part,
  381. unsigned long start_time);
  382. #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  383. # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
  384. #endif
  385. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  386. extern void bio_flush_dcache_pages(struct bio *bi);
  387. #else
  388. static inline void bio_flush_dcache_pages(struct bio *bi)
  389. {
  390. }
  391. #endif
  392. extern void bio_copy_data(struct bio *dst, struct bio *src);
  393. extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
  394. extern void bio_free_pages(struct bio *bio);
  395. extern struct bio *bio_copy_user_iov(struct request_queue *,
  396. struct rq_map_data *,
  397. const struct iov_iter *,
  398. gfp_t);
  399. extern int bio_uncopy_user(struct bio *);
  400. void zero_fill_bio(struct bio *bio);
  401. extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
  402. extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
  403. extern unsigned int bvec_nr_vecs(unsigned short idx);
  404. #ifdef CONFIG_BLK_CGROUP
  405. int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
  406. int bio_associate_current(struct bio *bio);
  407. void bio_disassociate_task(struct bio *bio);
  408. void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
  409. #else /* CONFIG_BLK_CGROUP */
  410. static inline int bio_associate_blkcg(struct bio *bio,
  411. struct cgroup_subsys_state *blkcg_css) { return 0; }
  412. static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
  413. static inline void bio_disassociate_task(struct bio *bio) { }
  414. static inline void bio_clone_blkcg_association(struct bio *dst,
  415. struct bio *src) { }
  416. #endif /* CONFIG_BLK_CGROUP */
  417. #ifdef CONFIG_HIGHMEM
  418. /*
  419. * remember never ever reenable interrupts between a bvec_kmap_irq and
  420. * bvec_kunmap_irq!
  421. */
  422. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  423. {
  424. unsigned long addr;
  425. /*
  426. * might not be a highmem page, but the preempt/irq count
  427. * balancing is a lot nicer this way
  428. */
  429. local_irq_save(*flags);
  430. addr = (unsigned long) kmap_atomic(bvec->bv_page);
  431. BUG_ON(addr & ~PAGE_MASK);
  432. return (char *) addr + bvec->bv_offset;
  433. }
  434. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  435. {
  436. unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
  437. kunmap_atomic((void *) ptr);
  438. local_irq_restore(*flags);
  439. }
  440. #else
  441. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  442. {
  443. return page_address(bvec->bv_page) + bvec->bv_offset;
  444. }
  445. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  446. {
  447. *flags = 0;
  448. }
  449. #endif
  450. static inline char *__bio_kmap_irq(struct bio *bio, struct bvec_iter iter,
  451. unsigned long *flags)
  452. {
  453. return bvec_kmap_irq(&bio_iter_iovec(bio, iter), flags);
  454. }
  455. #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
  456. #define bio_kmap_irq(bio, flags) \
  457. __bio_kmap_irq((bio), (bio)->bi_iter, (flags))
  458. #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
  459. /*
  460. * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
  461. *
  462. * A bio_list anchors a singly-linked list of bios chained through the bi_next
  463. * member of the bio. The bio_list also caches the last list member to allow
  464. * fast access to the tail.
  465. */
  466. struct bio_list {
  467. struct bio *head;
  468. struct bio *tail;
  469. };
  470. static inline int bio_list_empty(const struct bio_list *bl)
  471. {
  472. return bl->head == NULL;
  473. }
  474. static inline void bio_list_init(struct bio_list *bl)
  475. {
  476. bl->head = bl->tail = NULL;
  477. }
  478. #define BIO_EMPTY_LIST { NULL, NULL }
  479. #define bio_list_for_each(bio, bl) \
  480. for (bio = (bl)->head; bio; bio = bio->bi_next)
  481. static inline unsigned bio_list_size(const struct bio_list *bl)
  482. {
  483. unsigned sz = 0;
  484. struct bio *bio;
  485. bio_list_for_each(bio, bl)
  486. sz++;
  487. return sz;
  488. }
  489. static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
  490. {
  491. bio->bi_next = NULL;
  492. if (bl->tail)
  493. bl->tail->bi_next = bio;
  494. else
  495. bl->head = bio;
  496. bl->tail = bio;
  497. }
  498. static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
  499. {
  500. bio->bi_next = bl->head;
  501. bl->head = bio;
  502. if (!bl->tail)
  503. bl->tail = bio;
  504. }
  505. static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
  506. {
  507. if (!bl2->head)
  508. return;
  509. if (bl->tail)
  510. bl->tail->bi_next = bl2->head;
  511. else
  512. bl->head = bl2->head;
  513. bl->tail = bl2->tail;
  514. }
  515. static inline void bio_list_merge_head(struct bio_list *bl,
  516. struct bio_list *bl2)
  517. {
  518. if (!bl2->head)
  519. return;
  520. if (bl->head)
  521. bl2->tail->bi_next = bl->head;
  522. else
  523. bl->tail = bl2->tail;
  524. bl->head = bl2->head;
  525. }
  526. static inline struct bio *bio_list_peek(struct bio_list *bl)
  527. {
  528. return bl->head;
  529. }
  530. static inline struct bio *bio_list_pop(struct bio_list *bl)
  531. {
  532. struct bio *bio = bl->head;
  533. if (bio) {
  534. bl->head = bl->head->bi_next;
  535. if (!bl->head)
  536. bl->tail = NULL;
  537. bio->bi_next = NULL;
  538. }
  539. return bio;
  540. }
  541. static inline struct bio *bio_list_get(struct bio_list *bl)
  542. {
  543. struct bio *bio = bl->head;
  544. bl->head = bl->tail = NULL;
  545. return bio;
  546. }
  547. /*
  548. * Increment chain count for the bio. Make sure the CHAIN flag update
  549. * is visible before the raised count.
  550. */
  551. static inline void bio_inc_remaining(struct bio *bio)
  552. {
  553. bio_set_flag(bio, BIO_CHAIN);
  554. smp_mb__before_atomic();
  555. atomic_inc(&bio->__bi_remaining);
  556. }
  557. /*
  558. * bio_set is used to allow other portions of the IO system to
  559. * allocate their own private memory pools for bio and iovec structures.
  560. * These memory pools in turn all allocate from the bio_slab
  561. * and the bvec_slabs[].
  562. */
  563. #define BIO_POOL_SIZE 2
  564. struct bio_set {
  565. struct kmem_cache *bio_slab;
  566. unsigned int front_pad;
  567. mempool_t *bio_pool;
  568. mempool_t *bvec_pool;
  569. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  570. mempool_t *bio_integrity_pool;
  571. mempool_t *bvec_integrity_pool;
  572. #endif
  573. /*
  574. * Deadlock avoidance for stacking block drivers: see comments in
  575. * bio_alloc_bioset() for details
  576. */
  577. spinlock_t rescue_lock;
  578. struct bio_list rescue_list;
  579. struct work_struct rescue_work;
  580. struct workqueue_struct *rescue_workqueue;
  581. };
  582. struct biovec_slab {
  583. int nr_vecs;
  584. char *name;
  585. struct kmem_cache *slab;
  586. };
  587. /*
  588. * a small number of entries is fine, not going to be performance critical.
  589. * basically we just need to survive
  590. */
  591. #define BIO_SPLIT_ENTRIES 2
  592. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  593. #define bip_for_each_vec(bvl, bip, iter) \
  594. for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
  595. #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
  596. for_each_bio(_bio) \
  597. bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
  598. extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
  599. extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
  600. extern bool bio_integrity_prep(struct bio *);
  601. extern void bio_integrity_advance(struct bio *, unsigned int);
  602. extern void bio_integrity_trim(struct bio *);
  603. extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
  604. extern int bioset_integrity_create(struct bio_set *, int);
  605. extern void bioset_integrity_free(struct bio_set *);
  606. extern void bio_integrity_init(void);
  607. #else /* CONFIG_BLK_DEV_INTEGRITY */
  608. static inline void *bio_integrity(struct bio *bio)
  609. {
  610. return NULL;
  611. }
  612. static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
  613. {
  614. return 0;
  615. }
  616. static inline void bioset_integrity_free (struct bio_set *bs)
  617. {
  618. return;
  619. }
  620. static inline bool bio_integrity_prep(struct bio *bio)
  621. {
  622. return true;
  623. }
  624. static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  625. gfp_t gfp_mask)
  626. {
  627. return 0;
  628. }
  629. static inline void bio_integrity_advance(struct bio *bio,
  630. unsigned int bytes_done)
  631. {
  632. return;
  633. }
  634. static inline void bio_integrity_trim(struct bio *bio)
  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 */