bio.h 21 KB

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