bio.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * 2.5 block I/O model
  3. *
  4. * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public Licens
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  18. */
  19. #ifndef __LINUX_BIO_H
  20. #define __LINUX_BIO_H
  21. #include <linux/highmem.h>
  22. #include <linux/mempool.h>
  23. #include <linux/ioprio.h>
  24. #ifdef CONFIG_BLOCK
  25. #include <asm/io.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. #define BIO_MAX_PAGES 256
  33. #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_CACHE_SHIFT)
  34. #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
  35. /*
  36. * was unsigned short, but we might as well be ready for > 64kB I/O pages
  37. */
  38. struct bio_vec {
  39. struct page *bv_page;
  40. unsigned int bv_len;
  41. unsigned int bv_offset;
  42. };
  43. struct bio_set;
  44. struct bio;
  45. struct bio_integrity_payload;
  46. typedef void (bio_end_io_t) (struct bio *, int);
  47. typedef void (bio_destructor_t) (struct bio *);
  48. /*
  49. * main unit of I/O for the block layer and lower layers (ie drivers and
  50. * stacking drivers)
  51. */
  52. struct bio {
  53. sector_t bi_sector; /* device address in 512 byte
  54. sectors */
  55. struct bio *bi_next; /* request queue link */
  56. struct block_device *bi_bdev;
  57. unsigned long bi_flags; /* status, command, etc */
  58. unsigned long bi_rw; /* bottom bits READ/WRITE,
  59. * top bits priority
  60. */
  61. unsigned short bi_vcnt; /* how many bio_vec's */
  62. unsigned short bi_idx; /* current index into bvl_vec */
  63. /* Number of segments in this BIO after
  64. * physical address coalescing is performed.
  65. */
  66. unsigned int bi_phys_segments;
  67. unsigned int bi_size; /* residual I/O count */
  68. /*
  69. * To keep track of the max segment size, we account for the
  70. * sizes of the first and last mergeable segments in this bio.
  71. */
  72. unsigned int bi_seg_front_size;
  73. unsigned int bi_seg_back_size;
  74. unsigned int bi_max_vecs; /* max bvl_vecs we can hold */
  75. unsigned int bi_comp_cpu; /* completion CPU */
  76. atomic_t bi_cnt; /* pin count */
  77. struct bio_vec *bi_io_vec; /* the actual vec list */
  78. bio_end_io_t *bi_end_io;
  79. void *bi_private;
  80. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  81. struct bio_integrity_payload *bi_integrity; /* data integrity */
  82. #endif
  83. bio_destructor_t *bi_destructor; /* destructor */
  84. /*
  85. * We can inline a number of vecs at the end of the bio, to avoid
  86. * double allocations for a small number of bio_vecs. This member
  87. * MUST obviously be kept at the very end of the bio.
  88. */
  89. struct bio_vec bi_inline_vecs[0];
  90. };
  91. /*
  92. * bio flags
  93. */
  94. #define BIO_UPTODATE 0 /* ok after I/O completion */
  95. #define BIO_RW_BLOCK 1 /* RW_AHEAD set, and read/write would block */
  96. #define BIO_EOF 2 /* out-out-bounds error */
  97. #define BIO_SEG_VALID 3 /* bi_phys_segments valid */
  98. #define BIO_CLONED 4 /* doesn't own data */
  99. #define BIO_BOUNCED 5 /* bio is a bounce bio */
  100. #define BIO_USER_MAPPED 6 /* contains user pages */
  101. #define BIO_EOPNOTSUPP 7 /* not supported */
  102. #define BIO_CPU_AFFINE 8 /* complete bio on same CPU as submitted */
  103. #define BIO_NULL_MAPPED 9 /* contains invalid user pages */
  104. #define BIO_FS_INTEGRITY 10 /* fs owns integrity data, not block layer */
  105. #define BIO_QUIET 11 /* Make BIO Quiet */
  106. #define bio_flagged(bio, flag) ((bio)->bi_flags & (1 << (flag)))
  107. /*
  108. * top 4 bits of bio flags indicate the pool this bio came from
  109. */
  110. #define BIO_POOL_BITS (4)
  111. #define BIO_POOL_NONE ((1UL << BIO_POOL_BITS) - 1)
  112. #define BIO_POOL_OFFSET (BITS_PER_LONG - BIO_POOL_BITS)
  113. #define BIO_POOL_MASK (1UL << BIO_POOL_OFFSET)
  114. #define BIO_POOL_IDX(bio) ((bio)->bi_flags >> BIO_POOL_OFFSET)
  115. /*
  116. * Request flags. For use in the cmd_flags field of struct request, and in
  117. * bi_rw of struct bio. Note that some flags are only valid in either one.
  118. */
  119. enum rq_flag_bits {
  120. /* common flags */
  121. __REQ_WRITE, /* not set, read. set, write */
  122. __REQ_FAILFAST_DEV, /* no driver retries of device errors */
  123. __REQ_FAILFAST_TRANSPORT, /* no driver retries of transport errors */
  124. __REQ_FAILFAST_DRIVER, /* no driver retries of driver errors */
  125. __REQ_HARDBARRIER, /* may not be passed by drive either */
  126. __REQ_SYNC, /* request is sync (sync write or read) */
  127. __REQ_META, /* metadata io request */
  128. __REQ_DISCARD, /* request to discard sectors */
  129. __REQ_NOIDLE, /* don't anticipate more IO after this one */
  130. /* bio only flags */
  131. __REQ_UNPLUG, /* unplug the immediately after submission */
  132. __REQ_RAHEAD, /* read ahead, can fail anytime */
  133. /* request only flags */
  134. __REQ_SORTED, /* elevator knows about this request */
  135. __REQ_SOFTBARRIER, /* may not be passed by ioscheduler */
  136. __REQ_FUA, /* forced unit access */
  137. __REQ_NOMERGE, /* don't touch this for merging */
  138. __REQ_STARTED, /* drive already may have started this one */
  139. __REQ_DONTPREP, /* don't call prep for this one */
  140. __REQ_QUEUED, /* uses queueing */
  141. __REQ_ELVPRIV, /* elevator private data attached */
  142. __REQ_FAILED, /* set if the request failed */
  143. __REQ_QUIET, /* don't worry about errors */
  144. __REQ_PREEMPT, /* set for "ide_preempt" requests */
  145. __REQ_ORDERED_COLOR, /* is before or after barrier */
  146. __REQ_ALLOCED, /* request came from our alloc pool */
  147. __REQ_COPY_USER, /* contains copies of user pages */
  148. __REQ_INTEGRITY, /* integrity metadata has been remapped */
  149. __REQ_IO_STAT, /* account I/O stat */
  150. __REQ_MIXED_MERGE, /* merge of different types, fail separately */
  151. __REQ_NR_BITS, /* stops here */
  152. };
  153. #define REQ_WRITE (1 << __REQ_WRITE)
  154. #define REQ_FAILFAST_DEV (1 << __REQ_FAILFAST_DEV)
  155. #define REQ_FAILFAST_TRANSPORT (1 << __REQ_FAILFAST_TRANSPORT)
  156. #define REQ_FAILFAST_DRIVER (1 << __REQ_FAILFAST_DRIVER)
  157. #define REQ_HARDBARRIER (1 << __REQ_HARDBARRIER)
  158. #define REQ_SYNC (1 << __REQ_SYNC)
  159. #define REQ_META (1 << __REQ_META)
  160. #define REQ_DISCARD (1 << __REQ_DISCARD)
  161. #define REQ_NOIDLE (1 << __REQ_NOIDLE)
  162. #define REQ_FAILFAST_MASK \
  163. (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER)
  164. #define REQ_COMMON_MASK \
  165. (REQ_WRITE | REQ_FAILFAST_MASK | REQ_HARDBARRIER | REQ_SYNC | \
  166. REQ_META| REQ_DISCARD | REQ_NOIDLE)
  167. #define REQ_UNPLUG (1 << __REQ_UNPLUG)
  168. #define REQ_RAHEAD (1 << __REQ_RAHEAD)
  169. #define REQ_SORTED (1 << __REQ_SORTED)
  170. #define REQ_SOFTBARRIER (1 << __REQ_SOFTBARRIER)
  171. #define REQ_FUA (1 << __REQ_FUA)
  172. #define REQ_NOMERGE (1 << __REQ_NOMERGE)
  173. #define REQ_STARTED (1 << __REQ_STARTED)
  174. #define REQ_DONTPREP (1 << __REQ_DONTPREP)
  175. #define REQ_QUEUED (1 << __REQ_QUEUED)
  176. #define REQ_ELVPRIV (1 << __REQ_ELVPRIV)
  177. #define REQ_FAILED (1 << __REQ_FAILED)
  178. #define REQ_QUIET (1 << __REQ_QUIET)
  179. #define REQ_PREEMPT (1 << __REQ_PREEMPT)
  180. #define REQ_ORDERED_COLOR (1 << __REQ_ORDERED_COLOR)
  181. #define REQ_ALLOCED (1 << __REQ_ALLOCED)
  182. #define REQ_COPY_USER (1 << __REQ_COPY_USER)
  183. #define REQ_INTEGRITY (1 << __REQ_INTEGRITY)
  184. #define REQ_IO_STAT (1 << __REQ_IO_STAT)
  185. #define REQ_MIXED_MERGE (1 << __REQ_MIXED_MERGE)
  186. /*
  187. * upper 16 bits of bi_rw define the io priority of this bio
  188. */
  189. #define BIO_PRIO_SHIFT (8 * sizeof(unsigned long) - IOPRIO_BITS)
  190. #define bio_prio(bio) ((bio)->bi_rw >> BIO_PRIO_SHIFT)
  191. #define bio_prio_valid(bio) ioprio_valid(bio_prio(bio))
  192. #define bio_set_prio(bio, prio) do { \
  193. WARN_ON(prio >= (1 << IOPRIO_BITS)); \
  194. (bio)->bi_rw &= ((1UL << BIO_PRIO_SHIFT) - 1); \
  195. (bio)->bi_rw |= ((unsigned long) (prio) << BIO_PRIO_SHIFT); \
  196. } while (0)
  197. /*
  198. * various member access, note that bio_data should of course not be used
  199. * on highmem page vectors
  200. */
  201. #define bio_iovec_idx(bio, idx) (&((bio)->bi_io_vec[(idx)]))
  202. #define bio_iovec(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  203. #define bio_page(bio) bio_iovec((bio))->bv_page
  204. #define bio_offset(bio) bio_iovec((bio))->bv_offset
  205. #define bio_segments(bio) ((bio)->bi_vcnt - (bio)->bi_idx)
  206. #define bio_sectors(bio) ((bio)->bi_size >> 9)
  207. #define bio_empty_barrier(bio) \
  208. ((bio->bi_rw & REQ_HARDBARRIER) && \
  209. !bio_has_data(bio) && \
  210. !(bio->bi_rw & REQ_DISCARD))
  211. static inline unsigned int bio_cur_bytes(struct bio *bio)
  212. {
  213. if (bio->bi_vcnt)
  214. return bio_iovec(bio)->bv_len;
  215. else /* dataless requests such as discard */
  216. return bio->bi_size;
  217. }
  218. static inline void *bio_data(struct bio *bio)
  219. {
  220. if (bio->bi_vcnt)
  221. return page_address(bio_page(bio)) + bio_offset(bio);
  222. return NULL;
  223. }
  224. static inline int bio_has_allocated_vec(struct bio *bio)
  225. {
  226. return bio->bi_io_vec && bio->bi_io_vec != bio->bi_inline_vecs;
  227. }
  228. /*
  229. * will die
  230. */
  231. #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
  232. #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
  233. /*
  234. * queues that have highmem support enabled may still need to revert to
  235. * PIO transfers occasionally and thus map high pages temporarily. For
  236. * permanent PIO fall back, user is probably better off disabling highmem
  237. * I/O completely on that queue (see ide-dma for example)
  238. */
  239. #define __bio_kmap_atomic(bio, idx, kmtype) \
  240. (kmap_atomic(bio_iovec_idx((bio), (idx))->bv_page, kmtype) + \
  241. bio_iovec_idx((bio), (idx))->bv_offset)
  242. #define __bio_kunmap_atomic(addr, kmtype) kunmap_atomic(addr, kmtype)
  243. /*
  244. * merge helpers etc
  245. */
  246. #define __BVEC_END(bio) bio_iovec_idx((bio), (bio)->bi_vcnt - 1)
  247. #define __BVEC_START(bio) bio_iovec_idx((bio), (bio)->bi_idx)
  248. /* Default implementation of BIOVEC_PHYS_MERGEABLE */
  249. #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  250. ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
  251. /*
  252. * allow arch override, for eg virtualized architectures (put in asm/io.h)
  253. */
  254. #ifndef BIOVEC_PHYS_MERGEABLE
  255. #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
  256. __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
  257. #endif
  258. #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
  259. (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
  260. #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
  261. __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
  262. #define BIO_SEG_BOUNDARY(q, b1, b2) \
  263. BIOVEC_SEG_BOUNDARY((q), __BVEC_END((b1)), __BVEC_START((b2)))
  264. #define bio_io_error(bio) bio_endio((bio), -EIO)
  265. /*
  266. * drivers should not use the __ version unless they _really_ want to
  267. * run through the entire bio and not just pending pieces
  268. */
  269. #define __bio_for_each_segment(bvl, bio, i, start_idx) \
  270. for (bvl = bio_iovec_idx((bio), (start_idx)), i = (start_idx); \
  271. i < (bio)->bi_vcnt; \
  272. bvl++, i++)
  273. #define bio_for_each_segment(bvl, bio, i) \
  274. __bio_for_each_segment(bvl, bio, i, (bio)->bi_idx)
  275. /*
  276. * get a reference to a bio, so it won't disappear. the intended use is
  277. * something like:
  278. *
  279. * bio_get(bio);
  280. * submit_bio(rw, bio);
  281. * if (bio->bi_flags ...)
  282. * do_something
  283. * bio_put(bio);
  284. *
  285. * without the bio_get(), it could potentially complete I/O before submit_bio
  286. * returns. and then bio would be freed memory when if (bio->bi_flags ...)
  287. * runs
  288. */
  289. #define bio_get(bio) atomic_inc(&(bio)->bi_cnt)
  290. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  291. /*
  292. * bio integrity payload
  293. */
  294. struct bio_integrity_payload {
  295. struct bio *bip_bio; /* parent bio */
  296. sector_t bip_sector; /* virtual start sector */
  297. void *bip_buf; /* generated integrity data */
  298. bio_end_io_t *bip_end_io; /* saved I/O completion fn */
  299. unsigned int bip_size;
  300. unsigned short bip_slab; /* slab the bip came from */
  301. unsigned short bip_vcnt; /* # of integrity bio_vecs */
  302. unsigned short bip_idx; /* current bip_vec index */
  303. struct work_struct bip_work; /* I/O completion */
  304. struct bio_vec bip_vec[0]; /* embedded bvec array */
  305. };
  306. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  307. /*
  308. * A bio_pair is used when we need to split a bio.
  309. * This can only happen for a bio that refers to just one
  310. * page of data, and in the unusual situation when the
  311. * page crosses a chunk/device boundary
  312. *
  313. * The address of the master bio is stored in bio1.bi_private
  314. * The address of the pool the pair was allocated from is stored
  315. * in bio2.bi_private
  316. */
  317. struct bio_pair {
  318. struct bio bio1, bio2;
  319. struct bio_vec bv1, bv2;
  320. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  321. struct bio_integrity_payload bip1, bip2;
  322. struct bio_vec iv1, iv2;
  323. #endif
  324. atomic_t cnt;
  325. int error;
  326. };
  327. extern struct bio_pair *bio_split(struct bio *bi, int first_sectors);
  328. extern void bio_pair_release(struct bio_pair *dbio);
  329. extern struct bio_set *bioset_create(unsigned int, unsigned int);
  330. extern void bioset_free(struct bio_set *);
  331. extern struct bio *bio_alloc(gfp_t, int);
  332. extern struct bio *bio_kmalloc(gfp_t, int);
  333. extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
  334. extern void bio_put(struct bio *);
  335. extern void bio_free(struct bio *, struct bio_set *);
  336. extern void bio_endio(struct bio *, int);
  337. struct request_queue;
  338. extern int bio_phys_segments(struct request_queue *, struct bio *);
  339. extern void __bio_clone(struct bio *, struct bio *);
  340. extern struct bio *bio_clone(struct bio *, gfp_t);
  341. extern void bio_init(struct bio *);
  342. extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
  343. extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
  344. unsigned int, unsigned int);
  345. extern int bio_get_nr_vecs(struct block_device *);
  346. extern sector_t bio_sector_offset(struct bio *, unsigned short, unsigned int);
  347. extern struct bio *bio_map_user(struct request_queue *, struct block_device *,
  348. unsigned long, unsigned int, int, gfp_t);
  349. struct sg_iovec;
  350. struct rq_map_data;
  351. extern struct bio *bio_map_user_iov(struct request_queue *,
  352. struct block_device *,
  353. struct sg_iovec *, int, int, gfp_t);
  354. extern void bio_unmap_user(struct bio *);
  355. extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
  356. gfp_t);
  357. extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
  358. gfp_t, int);
  359. extern void bio_set_pages_dirty(struct bio *bio);
  360. extern void bio_check_pages_dirty(struct bio *bio);
  361. #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  362. # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
  363. #endif
  364. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  365. extern void bio_flush_dcache_pages(struct bio *bi);
  366. #else
  367. static inline void bio_flush_dcache_pages(struct bio *bi)
  368. {
  369. }
  370. #endif
  371. extern struct bio *bio_copy_user(struct request_queue *, struct rq_map_data *,
  372. unsigned long, unsigned int, int, gfp_t);
  373. extern struct bio *bio_copy_user_iov(struct request_queue *,
  374. struct rq_map_data *, struct sg_iovec *,
  375. int, int, gfp_t);
  376. extern int bio_uncopy_user(struct bio *);
  377. void zero_fill_bio(struct bio *bio);
  378. extern struct bio_vec *bvec_alloc_bs(gfp_t, int, unsigned long *, struct bio_set *);
  379. extern void bvec_free_bs(struct bio_set *, struct bio_vec *, unsigned int);
  380. extern unsigned int bvec_nr_vecs(unsigned short idx);
  381. /*
  382. * Allow queuer to specify a completion CPU for this bio
  383. */
  384. static inline void bio_set_completion_cpu(struct bio *bio, unsigned int cpu)
  385. {
  386. bio->bi_comp_cpu = cpu;
  387. }
  388. /*
  389. * bio_set is used to allow other portions of the IO system to
  390. * allocate their own private memory pools for bio and iovec structures.
  391. * These memory pools in turn all allocate from the bio_slab
  392. * and the bvec_slabs[].
  393. */
  394. #define BIO_POOL_SIZE 2
  395. #define BIOVEC_NR_POOLS 6
  396. #define BIOVEC_MAX_IDX (BIOVEC_NR_POOLS - 1)
  397. struct bio_set {
  398. struct kmem_cache *bio_slab;
  399. unsigned int front_pad;
  400. mempool_t *bio_pool;
  401. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  402. mempool_t *bio_integrity_pool;
  403. #endif
  404. mempool_t *bvec_pool;
  405. };
  406. struct biovec_slab {
  407. int nr_vecs;
  408. char *name;
  409. struct kmem_cache *slab;
  410. };
  411. extern struct bio_set *fs_bio_set;
  412. extern struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly;
  413. /*
  414. * a small number of entries is fine, not going to be performance critical.
  415. * basically we just need to survive
  416. */
  417. #define BIO_SPLIT_ENTRIES 2
  418. #ifdef CONFIG_HIGHMEM
  419. /*
  420. * remember never ever reenable interrupts between a bvec_kmap_irq and
  421. * bvec_kunmap_irq!
  422. */
  423. static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
  424. {
  425. unsigned long addr;
  426. /*
  427. * might not be a highmem page, but the preempt/irq count
  428. * balancing is a lot nicer this way
  429. */
  430. local_irq_save(*flags);
  431. addr = (unsigned long) kmap_atomic(bvec->bv_page, KM_BIO_SRC_IRQ);
  432. BUG_ON(addr & ~PAGE_MASK);
  433. return (char *) addr + bvec->bv_offset;
  434. }
  435. static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
  436. {
  437. unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
  438. kunmap_atomic((void *) ptr, KM_BIO_SRC_IRQ);
  439. local_irq_restore(*flags);
  440. }
  441. #else
  442. #define bvec_kmap_irq(bvec, flags) (page_address((bvec)->bv_page) + (bvec)->bv_offset)
  443. #define bvec_kunmap_irq(buf, flags) do { *(flags) = 0; } while (0)
  444. #endif
  445. static inline char *__bio_kmap_irq(struct bio *bio, unsigned short idx,
  446. unsigned long *flags)
  447. {
  448. return bvec_kmap_irq(bio_iovec_idx(bio, idx), flags);
  449. }
  450. #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
  451. #define bio_kmap_irq(bio, flags) \
  452. __bio_kmap_irq((bio), (bio)->bi_idx, (flags))
  453. #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
  454. /*
  455. * Check whether this bio carries any data or not. A NULL bio is allowed.
  456. */
  457. static inline int bio_has_data(struct bio *bio)
  458. {
  459. return bio && bio->bi_io_vec != NULL;
  460. }
  461. /*
  462. * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
  463. *
  464. * A bio_list anchors a singly-linked list of bios chained through the bi_next
  465. * member of the bio. The bio_list also caches the last list member to allow
  466. * fast access to the tail.
  467. */
  468. struct bio_list {
  469. struct bio *head;
  470. struct bio *tail;
  471. };
  472. static inline int bio_list_empty(const struct bio_list *bl)
  473. {
  474. return bl->head == NULL;
  475. }
  476. static inline void bio_list_init(struct bio_list *bl)
  477. {
  478. bl->head = bl->tail = NULL;
  479. }
  480. #define bio_list_for_each(bio, bl) \
  481. for (bio = (bl)->head; bio; bio = bio->bi_next)
  482. static inline unsigned bio_list_size(const struct bio_list *bl)
  483. {
  484. unsigned sz = 0;
  485. struct bio *bio;
  486. bio_list_for_each(bio, bl)
  487. sz++;
  488. return sz;
  489. }
  490. static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
  491. {
  492. bio->bi_next = NULL;
  493. if (bl->tail)
  494. bl->tail->bi_next = bio;
  495. else
  496. bl->head = bio;
  497. bl->tail = bio;
  498. }
  499. static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
  500. {
  501. bio->bi_next = bl->head;
  502. bl->head = bio;
  503. if (!bl->tail)
  504. bl->tail = bio;
  505. }
  506. static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
  507. {
  508. if (!bl2->head)
  509. return;
  510. if (bl->tail)
  511. bl->tail->bi_next = bl2->head;
  512. else
  513. bl->head = bl2->head;
  514. bl->tail = bl2->tail;
  515. }
  516. static inline void bio_list_merge_head(struct bio_list *bl,
  517. struct bio_list *bl2)
  518. {
  519. if (!bl2->head)
  520. return;
  521. if (bl->head)
  522. bl2->tail->bi_next = bl->head;
  523. else
  524. bl->tail = bl2->tail;
  525. bl->head = bl2->head;
  526. }
  527. static inline struct bio *bio_list_peek(struct bio_list *bl)
  528. {
  529. return bl->head;
  530. }
  531. static inline struct bio *bio_list_pop(struct bio_list *bl)
  532. {
  533. struct bio *bio = bl->head;
  534. if (bio) {
  535. bl->head = bl->head->bi_next;
  536. if (!bl->head)
  537. bl->tail = NULL;
  538. bio->bi_next = NULL;
  539. }
  540. return bio;
  541. }
  542. static inline struct bio *bio_list_get(struct bio_list *bl)
  543. {
  544. struct bio *bio = bl->head;
  545. bl->head = bl->tail = NULL;
  546. return bio;
  547. }
  548. #if defined(CONFIG_BLK_DEV_INTEGRITY)
  549. #define bip_vec_idx(bip, idx) (&(bip->bip_vec[(idx)]))
  550. #define bip_vec(bip) bip_vec_idx(bip, 0)
  551. #define __bip_for_each_vec(bvl, bip, i, start_idx) \
  552. for (bvl = bip_vec_idx((bip), (start_idx)), i = (start_idx); \
  553. i < (bip)->bip_vcnt; \
  554. bvl++, i++)
  555. #define bip_for_each_vec(bvl, bip, i) \
  556. __bip_for_each_vec(bvl, bip, i, (bip)->bip_idx)
  557. #define bio_integrity(bio) (bio->bi_integrity != NULL)
  558. extern struct bio_integrity_payload *bio_integrity_alloc_bioset(struct bio *, gfp_t, unsigned int, struct bio_set *);
  559. extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
  560. extern void bio_integrity_free(struct bio *, struct bio_set *);
  561. extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
  562. extern int bio_integrity_enabled(struct bio *bio);
  563. extern int bio_integrity_set_tag(struct bio *, void *, unsigned int);
  564. extern int bio_integrity_get_tag(struct bio *, void *, unsigned int);
  565. extern int bio_integrity_prep(struct bio *);
  566. extern void bio_integrity_endio(struct bio *, int);
  567. extern void bio_integrity_advance(struct bio *, unsigned int);
  568. extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
  569. extern void bio_integrity_split(struct bio *, struct bio_pair *, int);
  570. extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t, struct bio_set *);
  571. extern int bioset_integrity_create(struct bio_set *, int);
  572. extern void bioset_integrity_free(struct bio_set *);
  573. extern void bio_integrity_init(void);
  574. #else /* CONFIG_BLK_DEV_INTEGRITY */
  575. #define bio_integrity(a) (0)
  576. #define bioset_integrity_create(a, b) (0)
  577. #define bio_integrity_prep(a) (0)
  578. #define bio_integrity_enabled(a) (0)
  579. #define bio_integrity_clone(a, b, c, d) (0)
  580. #define bioset_integrity_free(a) do { } while (0)
  581. #define bio_integrity_free(a, b) do { } while (0)
  582. #define bio_integrity_endio(a, b) do { } while (0)
  583. #define bio_integrity_advance(a, b) do { } while (0)
  584. #define bio_integrity_trim(a, b, c) do { } while (0)
  585. #define bio_integrity_split(a, b, c) do { } while (0)
  586. #define bio_integrity_set_tag(a, b, c) do { } while (0)
  587. #define bio_integrity_get_tag(a, b, c) do { } while (0)
  588. #define bio_integrity_init(a) do { } while (0)
  589. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  590. #endif /* CONFIG_BLOCK */
  591. #endif /* __LINUX_BIO_H */