Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
7cc01581 | 12 | * |
1da177e4 LT |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public Licens | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- | |
19 | */ | |
20 | #ifndef __LINUX_BIO_H | |
21 | #define __LINUX_BIO_H | |
22 | ||
23 | #include <linux/highmem.h> | |
24 | #include <linux/mempool.h> | |
22e2c507 | 25 | #include <linux/ioprio.h> |
187f1882 | 26 | #include <linux/bug.h> |
1da177e4 | 27 | |
02a5e0ac DH |
28 | #ifdef CONFIG_BLOCK |
29 | ||
1da177e4 LT |
30 | #include <asm/io.h> |
31 | ||
7cc01581 TH |
32 | /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */ |
33 | #include <linux/blk_types.h> | |
34 | ||
1da177e4 LT |
35 | #define BIO_DEBUG |
36 | ||
37 | #ifdef BIO_DEBUG | |
38 | #define BIO_BUG_ON BUG_ON | |
39 | #else | |
40 | #define BIO_BUG_ON | |
41 | #endif | |
42 | ||
d84a8477 | 43 | #define BIO_MAX_PAGES 256 |
1da177e4 LT |
44 | #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_CACHE_SHIFT) |
45 | #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9) | |
46 | ||
22e2c507 JA |
47 | /* |
48 | * upper 16 bits of bi_rw define the io priority of this bio | |
49 | */ | |
50 | #define BIO_PRIO_SHIFT (8 * sizeof(unsigned long) - IOPRIO_BITS) | |
51 | #define bio_prio(bio) ((bio)->bi_rw >> BIO_PRIO_SHIFT) | |
52 | #define bio_prio_valid(bio) ioprio_valid(bio_prio(bio)) | |
53 | ||
54 | #define bio_set_prio(bio, prio) do { \ | |
55 | WARN_ON(prio >= (1 << IOPRIO_BITS)); \ | |
56 | (bio)->bi_rw &= ((1UL << BIO_PRIO_SHIFT) - 1); \ | |
57 | (bio)->bi_rw |= ((unsigned long) (prio) << BIO_PRIO_SHIFT); \ | |
58 | } while (0) | |
59 | ||
1da177e4 LT |
60 | /* |
61 | * various member access, note that bio_data should of course not be used | |
62 | * on highmem page vectors | |
63 | */ | |
64 | #define bio_iovec_idx(bio, idx) (&((bio)->bi_io_vec[(idx)])) | |
a4ad39b1 | 65 | #define __bio_iovec(bio) bio_iovec_idx((bio), (bio)->bi_iter.bi_idx) |
7988613b | 66 | |
4550dd6c | 67 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) |
a4ad39b1 | 68 | |
4550dd6c KO |
69 | #define bvec_iter_page(bvec, iter) \ |
70 | (__bvec_iter_bvec((bvec), (iter))->bv_page) | |
71 | ||
72 | #define bvec_iter_len(bvec, iter) \ | |
73 | min((iter).bi_size, \ | |
74 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) | |
75 | ||
76 | #define bvec_iter_offset(bvec, iter) \ | |
77 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) | |
78 | ||
79 | #define bvec_iter_bvec(bvec, iter) \ | |
80 | ((struct bio_vec) { \ | |
81 | .bv_page = bvec_iter_page((bvec), (iter)), \ | |
82 | .bv_len = bvec_iter_len((bvec), (iter)), \ | |
83 | .bv_offset = bvec_iter_offset((bvec), (iter)), \ | |
84 | }) | |
85 | ||
86 | #define bio_iter_iovec(bio, iter) \ | |
87 | bvec_iter_bvec((bio)->bi_io_vec, (iter)) | |
88 | ||
89 | #define bio_iter_page(bio, iter) \ | |
90 | bvec_iter_page((bio)->bi_io_vec, (iter)) | |
91 | #define bio_iter_len(bio, iter) \ | |
92 | bvec_iter_len((bio)->bi_io_vec, (iter)) | |
93 | #define bio_iter_offset(bio, iter) \ | |
94 | bvec_iter_offset((bio)->bi_io_vec, (iter)) | |
95 | ||
96 | #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter) | |
97 | #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter) | |
98 | #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter) | |
7988613b | 99 | |
4f024f37 KO |
100 | #define bio_segments(bio) ((bio)->bi_vcnt - (bio)->bi_iter.bi_idx) |
101 | #define bio_sectors(bio) ((bio)->bi_iter.bi_size >> 9) | |
102 | #define bio_end_sector(bio) ((bio)->bi_iter.bi_sector + bio_sectors((bio))) | |
bf2de6f5 | 103 | |
2e46e8b2 | 104 | static inline unsigned int bio_cur_bytes(struct bio *bio) |
bf2de6f5 JA |
105 | { |
106 | if (bio->bi_vcnt) | |
a4ad39b1 | 107 | return bio_iovec(bio).bv_len; |
fb2dce86 | 108 | else /* dataless requests such as discard */ |
4f024f37 | 109 | return bio->bi_iter.bi_size; |
bf2de6f5 JA |
110 | } |
111 | ||
112 | static inline void *bio_data(struct bio *bio) | |
113 | { | |
114 | if (bio->bi_vcnt) | |
115 | return page_address(bio_page(bio)) + bio_offset(bio); | |
116 | ||
117 | return NULL; | |
118 | } | |
1da177e4 LT |
119 | |
120 | /* | |
121 | * will die | |
122 | */ | |
123 | #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio))) | |
124 | #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset) | |
125 | ||
126 | /* | |
127 | * queues that have highmem support enabled may still need to revert to | |
128 | * PIO transfers occasionally and thus map high pages temporarily. For | |
129 | * permanent PIO fall back, user is probably better off disabling highmem | |
130 | * I/O completely on that queue (see ide-dma for example) | |
131 | */ | |
0eb5afb3 | 132 | #define __bio_kmap_atomic(bio, idx) \ |
e8e3c3d6 | 133 | (kmap_atomic(bio_iovec_idx((bio), (idx))->bv_page) + \ |
1da177e4 LT |
134 | bio_iovec_idx((bio), (idx))->bv_offset) |
135 | ||
0eb5afb3 | 136 | #define __bio_kunmap_atomic(addr) kunmap_atomic(addr) |
1da177e4 LT |
137 | |
138 | /* | |
139 | * merge helpers etc | |
140 | */ | |
141 | ||
142 | #define __BVEC_END(bio) bio_iovec_idx((bio), (bio)->bi_vcnt - 1) | |
4f024f37 | 143 | #define __BVEC_START(bio) bio_iovec_idx((bio), (bio)->bi_iter.bi_idx) |
1da177e4 | 144 | |
f92131c3 JF |
145 | /* Default implementation of BIOVEC_PHYS_MERGEABLE */ |
146 | #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \ | |
147 | ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2))) | |
148 | ||
1da177e4 LT |
149 | /* |
150 | * allow arch override, for eg virtualized architectures (put in asm/io.h) | |
151 | */ | |
152 | #ifndef BIOVEC_PHYS_MERGEABLE | |
153 | #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \ | |
f92131c3 | 154 | __BIOVEC_PHYS_MERGEABLE(vec1, vec2) |
1da177e4 LT |
155 | #endif |
156 | ||
1da177e4 LT |
157 | #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \ |
158 | (((addr1) | (mask)) == (((addr2) - 1) | (mask))) | |
159 | #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \ | |
ae03bf63 | 160 | __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q))) |
1da177e4 LT |
161 | #define BIO_SEG_BOUNDARY(q, b1, b2) \ |
162 | BIOVEC_SEG_BOUNDARY((q), __BVEC_END((b1)), __BVEC_START((b2))) | |
163 | ||
6712ecf8 | 164 | #define bio_io_error(bio) bio_endio((bio), -EIO) |
1da177e4 | 165 | |
d74c6d51 KO |
166 | /* |
167 | * drivers should _never_ use the all version - the bio may have been split | |
168 | * before it got to the driver and the driver won't own all of it | |
169 | */ | |
170 | #define bio_for_each_segment_all(bvl, bio, i) \ | |
171 | for (i = 0; \ | |
172 | bvl = bio_iovec_idx((bio), (i)), i < (bio)->bi_vcnt; \ | |
173 | i++) | |
174 | ||
4550dd6c KO |
175 | static inline void bvec_iter_advance(struct bio_vec *bv, struct bvec_iter *iter, |
176 | unsigned bytes) | |
177 | { | |
178 | WARN_ONCE(bytes > iter->bi_size, | |
179 | "Attempted to advance past end of bvec iter\n"); | |
180 | ||
181 | while (bytes) { | |
182 | unsigned len = min(bytes, bvec_iter_len(bv, *iter)); | |
183 | ||
184 | bytes -= len; | |
185 | iter->bi_size -= len; | |
186 | iter->bi_bvec_done += len; | |
187 | ||
188 | if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { | |
189 | iter->bi_bvec_done = 0; | |
190 | iter->bi_idx++; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | #define for_each_bvec(bvl, bio_vec, iter, start) \ | |
196 | for ((iter) = start; \ | |
197 | (bvl) = bvec_iter_bvec((bio_vec), (iter)), \ | |
198 | (iter).bi_size; \ | |
199 | bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len)) | |
200 | ||
201 | ||
202 | static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, | |
203 | unsigned bytes) | |
204 | { | |
205 | iter->bi_sector += bytes >> 9; | |
206 | ||
207 | if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK) | |
208 | iter->bi_size -= bytes; | |
209 | else | |
210 | bvec_iter_advance(bio->bi_io_vec, iter, bytes); | |
211 | } | |
212 | ||
7988613b KO |
213 | #define __bio_for_each_segment(bvl, bio, iter, start) \ |
214 | for (iter = (start); \ | |
4550dd6c KO |
215 | (iter).bi_size && \ |
216 | ((bvl = bio_iter_iovec((bio), (iter))), 1); \ | |
217 | bio_advance_iter((bio), &(iter), (bvl).bv_len)) | |
7988613b KO |
218 | |
219 | #define bio_for_each_segment(bvl, bio, iter) \ | |
220 | __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter) | |
221 | ||
4550dd6c | 222 | #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) |
1da177e4 LT |
223 | |
224 | /* | |
225 | * get a reference to a bio, so it won't disappear. the intended use is | |
226 | * something like: | |
227 | * | |
228 | * bio_get(bio); | |
229 | * submit_bio(rw, bio); | |
230 | * if (bio->bi_flags ...) | |
231 | * do_something | |
232 | * bio_put(bio); | |
233 | * | |
234 | * without the bio_get(), it could potentially complete I/O before submit_bio | |
235 | * returns. and then bio would be freed memory when if (bio->bi_flags ...) | |
236 | * runs | |
237 | */ | |
238 | #define bio_get(bio) atomic_inc(&(bio)->bi_cnt) | |
239 | ||
7ba1ba12 MP |
240 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
241 | /* | |
242 | * bio integrity payload | |
243 | */ | |
244 | struct bio_integrity_payload { | |
245 | struct bio *bip_bio; /* parent bio */ | |
7ba1ba12 MP |
246 | |
247 | sector_t bip_sector; /* virtual start sector */ | |
248 | ||
249 | void *bip_buf; /* generated integrity data */ | |
250 | bio_end_io_t *bip_end_io; /* saved I/O completion fn */ | |
251 | ||
7ba1ba12 MP |
252 | unsigned int bip_size; |
253 | ||
7878cba9 | 254 | unsigned short bip_slab; /* slab the bip came from */ |
7ba1ba12 MP |
255 | unsigned short bip_vcnt; /* # of integrity bio_vecs */ |
256 | unsigned short bip_idx; /* current bip_vec index */ | |
29ed7813 | 257 | unsigned bip_owns_buf:1; /* should free bip_buf */ |
7ba1ba12 MP |
258 | |
259 | struct work_struct bip_work; /* I/O completion */ | |
6fda981c KO |
260 | |
261 | struct bio_vec *bip_vec; | |
262 | struct bio_vec bip_inline_vecs[0];/* embedded bvec array */ | |
7ba1ba12 MP |
263 | }; |
264 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
1da177e4 LT |
265 | |
266 | /* | |
267 | * A bio_pair is used when we need to split a bio. | |
268 | * This can only happen for a bio that refers to just one | |
269 | * page of data, and in the unusual situation when the | |
270 | * page crosses a chunk/device boundary | |
271 | * | |
272 | * The address of the master bio is stored in bio1.bi_private | |
273 | * The address of the pool the pair was allocated from is stored | |
274 | * in bio2.bi_private | |
275 | */ | |
276 | struct bio_pair { | |
7ba1ba12 MP |
277 | struct bio bio1, bio2; |
278 | struct bio_vec bv1, bv2; | |
279 | #if defined(CONFIG_BLK_DEV_INTEGRITY) | |
280 | struct bio_integrity_payload bip1, bip2; | |
281 | struct bio_vec iv1, iv2; | |
282 | #endif | |
283 | atomic_t cnt; | |
284 | int error; | |
1da177e4 | 285 | }; |
6feef531 | 286 | extern struct bio_pair *bio_split(struct bio *bi, int first_sectors); |
1da177e4 | 287 | extern void bio_pair_release(struct bio_pair *dbio); |
6678d83f | 288 | extern void bio_trim(struct bio *bio, int offset, int size); |
1da177e4 | 289 | |
bb799ca0 | 290 | extern struct bio_set *bioset_create(unsigned int, unsigned int); |
1da177e4 | 291 | extern void bioset_free(struct bio_set *); |
9f060e22 | 292 | extern mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries); |
1da177e4 | 293 | |
dd0fc66f | 294 | extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *); |
1da177e4 LT |
295 | extern void bio_put(struct bio *); |
296 | ||
bf800ef1 KO |
297 | extern void __bio_clone(struct bio *, struct bio *); |
298 | extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs); | |
299 | ||
3f86a82a KO |
300 | extern struct bio_set *fs_bio_set; |
301 | ||
302 | static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) | |
303 | { | |
304 | return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); | |
305 | } | |
306 | ||
bf800ef1 KO |
307 | static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask) |
308 | { | |
309 | return bio_clone_bioset(bio, gfp_mask, fs_bio_set); | |
310 | } | |
311 | ||
3f86a82a KO |
312 | static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
313 | { | |
314 | return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL); | |
315 | } | |
316 | ||
bf800ef1 KO |
317 | static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask) |
318 | { | |
319 | return bio_clone_bioset(bio, gfp_mask, NULL); | |
320 | ||
321 | } | |
322 | ||
6712ecf8 | 323 | extern void bio_endio(struct bio *, int); |
1da177e4 LT |
324 | struct request_queue; |
325 | extern int bio_phys_segments(struct request_queue *, struct bio *); | |
1da177e4 | 326 | |
9e882242 | 327 | extern int submit_bio_wait(int rw, struct bio *bio); |
054bdf64 KO |
328 | extern void bio_advance(struct bio *, unsigned); |
329 | ||
1da177e4 | 330 | extern void bio_init(struct bio *); |
f44b48c7 | 331 | extern void bio_reset(struct bio *); |
1da177e4 LT |
332 | |
333 | extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int); | |
6e68af66 MC |
334 | extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, |
335 | unsigned int, unsigned int); | |
1da177e4 | 336 | extern int bio_get_nr_vecs(struct block_device *); |
ad3316bf | 337 | extern sector_t bio_sector_offset(struct bio *, unsigned short, unsigned int); |
1da177e4 | 338 | extern struct bio *bio_map_user(struct request_queue *, struct block_device *, |
a3bce90e | 339 | unsigned long, unsigned int, int, gfp_t); |
f1970baf | 340 | struct sg_iovec; |
152e283f | 341 | struct rq_map_data; |
f1970baf JB |
342 | extern struct bio *bio_map_user_iov(struct request_queue *, |
343 | struct block_device *, | |
a3bce90e | 344 | struct sg_iovec *, int, int, gfp_t); |
1da177e4 | 345 | extern void bio_unmap_user(struct bio *); |
df46b9a4 | 346 | extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int, |
27496a8c | 347 | gfp_t); |
68154e90 FT |
348 | extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int, |
349 | gfp_t, int); | |
1da177e4 LT |
350 | extern void bio_set_pages_dirty(struct bio *bio); |
351 | extern void bio_check_pages_dirty(struct bio *bio); | |
2d4dc890 IL |
352 | |
353 | #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE | |
354 | # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform" | |
355 | #endif | |
356 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE | |
357 | extern void bio_flush_dcache_pages(struct bio *bi); | |
358 | #else | |
359 | static inline void bio_flush_dcache_pages(struct bio *bi) | |
360 | { | |
361 | } | |
362 | #endif | |
363 | ||
16ac3d63 | 364 | extern void bio_copy_data(struct bio *dst, struct bio *src); |
a0787606 | 365 | extern int bio_alloc_pages(struct bio *bio, gfp_t gfp); |
16ac3d63 | 366 | |
152e283f FT |
367 | extern struct bio *bio_copy_user(struct request_queue *, struct rq_map_data *, |
368 | unsigned long, unsigned int, int, gfp_t); | |
369 | extern struct bio *bio_copy_user_iov(struct request_queue *, | |
370 | struct rq_map_data *, struct sg_iovec *, | |
a3bce90e | 371 | int, int, gfp_t); |
1da177e4 LT |
372 | extern int bio_uncopy_user(struct bio *); |
373 | void zero_fill_bio(struct bio *bio); | |
9f060e22 KO |
374 | extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *); |
375 | extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int); | |
7ba1ba12 | 376 | extern unsigned int bvec_nr_vecs(unsigned short idx); |
51d654e1 | 377 | |
852c788f TH |
378 | #ifdef CONFIG_BLK_CGROUP |
379 | int bio_associate_current(struct bio *bio); | |
380 | void bio_disassociate_task(struct bio *bio); | |
381 | #else /* CONFIG_BLK_CGROUP */ | |
382 | static inline int bio_associate_current(struct bio *bio) { return -ENOENT; } | |
383 | static inline void bio_disassociate_task(struct bio *bio) { } | |
384 | #endif /* CONFIG_BLK_CGROUP */ | |
385 | ||
1da177e4 LT |
386 | #ifdef CONFIG_HIGHMEM |
387 | /* | |
20b636bf AB |
388 | * remember never ever reenable interrupts between a bvec_kmap_irq and |
389 | * bvec_kunmap_irq! | |
1da177e4 | 390 | */ |
4f570f99 | 391 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
1da177e4 LT |
392 | { |
393 | unsigned long addr; | |
394 | ||
395 | /* | |
396 | * might not be a highmem page, but the preempt/irq count | |
397 | * balancing is a lot nicer this way | |
398 | */ | |
399 | local_irq_save(*flags); | |
e8e3c3d6 | 400 | addr = (unsigned long) kmap_atomic(bvec->bv_page); |
1da177e4 LT |
401 | |
402 | BUG_ON(addr & ~PAGE_MASK); | |
403 | ||
404 | return (char *) addr + bvec->bv_offset; | |
405 | } | |
406 | ||
4f570f99 | 407 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) |
1da177e4 LT |
408 | { |
409 | unsigned long ptr = (unsigned long) buffer & PAGE_MASK; | |
410 | ||
e8e3c3d6 | 411 | kunmap_atomic((void *) ptr); |
1da177e4 LT |
412 | local_irq_restore(*flags); |
413 | } | |
414 | ||
415 | #else | |
11a691be GU |
416 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
417 | { | |
418 | return page_address(bvec->bv_page) + bvec->bv_offset; | |
419 | } | |
420 | ||
421 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) | |
422 | { | |
423 | *flags = 0; | |
424 | } | |
1da177e4 LT |
425 | #endif |
426 | ||
c2d08dad | 427 | static inline char *__bio_kmap_irq(struct bio *bio, unsigned short idx, |
1da177e4 LT |
428 | unsigned long *flags) |
429 | { | |
430 | return bvec_kmap_irq(bio_iovec_idx(bio, idx), flags); | |
431 | } | |
432 | #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags) | |
433 | ||
434 | #define bio_kmap_irq(bio, flags) \ | |
4f024f37 | 435 | __bio_kmap_irq((bio), (bio)->bi_iter.bi_idx, (flags)) |
1da177e4 LT |
436 | #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags) |
437 | ||
7a67f63b JA |
438 | /* |
439 | * Check whether this bio carries any data or not. A NULL bio is allowed. | |
440 | */ | |
e2a60da7 | 441 | static inline bool bio_has_data(struct bio *bio) |
7a67f63b | 442 | { |
e2a60da7 MP |
443 | if (bio && bio->bi_vcnt) |
444 | return true; | |
445 | ||
446 | return false; | |
447 | } | |
448 | ||
449 | static inline bool bio_is_rw(struct bio *bio) | |
450 | { | |
451 | if (!bio_has_data(bio)) | |
452 | return false; | |
453 | ||
4363ac7c MP |
454 | if (bio->bi_rw & REQ_WRITE_SAME) |
455 | return false; | |
456 | ||
e2a60da7 MP |
457 | return true; |
458 | } | |
459 | ||
460 | static inline bool bio_mergeable(struct bio *bio) | |
461 | { | |
462 | if (bio->bi_rw & REQ_NOMERGE_FLAGS) | |
463 | return false; | |
464 | ||
465 | return true; | |
7a67f63b JA |
466 | } |
467 | ||
8f3d8ba2 | 468 | /* |
e686307f | 469 | * BIO list management for use by remapping drivers (e.g. DM or MD) and loop. |
8f3d8ba2 CH |
470 | * |
471 | * A bio_list anchors a singly-linked list of bios chained through the bi_next | |
472 | * member of the bio. The bio_list also caches the last list member to allow | |
473 | * fast access to the tail. | |
474 | */ | |
475 | struct bio_list { | |
476 | struct bio *head; | |
477 | struct bio *tail; | |
478 | }; | |
479 | ||
480 | static inline int bio_list_empty(const struct bio_list *bl) | |
481 | { | |
482 | return bl->head == NULL; | |
483 | } | |
484 | ||
485 | static inline void bio_list_init(struct bio_list *bl) | |
486 | { | |
487 | bl->head = bl->tail = NULL; | |
488 | } | |
489 | ||
320ae51f JA |
490 | #define BIO_EMPTY_LIST { NULL, NULL } |
491 | ||
8f3d8ba2 CH |
492 | #define bio_list_for_each(bio, bl) \ |
493 | for (bio = (bl)->head; bio; bio = bio->bi_next) | |
494 | ||
495 | static inline unsigned bio_list_size(const struct bio_list *bl) | |
496 | { | |
497 | unsigned sz = 0; | |
498 | struct bio *bio; | |
499 | ||
500 | bio_list_for_each(bio, bl) | |
501 | sz++; | |
502 | ||
503 | return sz; | |
504 | } | |
505 | ||
506 | static inline void bio_list_add(struct bio_list *bl, struct bio *bio) | |
507 | { | |
508 | bio->bi_next = NULL; | |
509 | ||
510 | if (bl->tail) | |
511 | bl->tail->bi_next = bio; | |
512 | else | |
513 | bl->head = bio; | |
514 | ||
515 | bl->tail = bio; | |
516 | } | |
517 | ||
518 | static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio) | |
519 | { | |
520 | bio->bi_next = bl->head; | |
521 | ||
522 | bl->head = bio; | |
523 | ||
524 | if (!bl->tail) | |
525 | bl->tail = bio; | |
526 | } | |
527 | ||
528 | static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) | |
529 | { | |
530 | if (!bl2->head) | |
531 | return; | |
532 | ||
533 | if (bl->tail) | |
534 | bl->tail->bi_next = bl2->head; | |
535 | else | |
536 | bl->head = bl2->head; | |
537 | ||
538 | bl->tail = bl2->tail; | |
539 | } | |
540 | ||
541 | static inline void bio_list_merge_head(struct bio_list *bl, | |
542 | struct bio_list *bl2) | |
543 | { | |
544 | if (!bl2->head) | |
545 | return; | |
546 | ||
547 | if (bl->head) | |
548 | bl2->tail->bi_next = bl->head; | |
549 | else | |
550 | bl->tail = bl2->tail; | |
551 | ||
552 | bl->head = bl2->head; | |
553 | } | |
554 | ||
13685a16 GU |
555 | static inline struct bio *bio_list_peek(struct bio_list *bl) |
556 | { | |
557 | return bl->head; | |
558 | } | |
559 | ||
8f3d8ba2 CH |
560 | static inline struct bio *bio_list_pop(struct bio_list *bl) |
561 | { | |
562 | struct bio *bio = bl->head; | |
563 | ||
564 | if (bio) { | |
565 | bl->head = bl->head->bi_next; | |
566 | if (!bl->head) | |
567 | bl->tail = NULL; | |
568 | ||
569 | bio->bi_next = NULL; | |
570 | } | |
571 | ||
572 | return bio; | |
573 | } | |
574 | ||
575 | static inline struct bio *bio_list_get(struct bio_list *bl) | |
576 | { | |
577 | struct bio *bio = bl->head; | |
578 | ||
579 | bl->head = bl->tail = NULL; | |
580 | ||
581 | return bio; | |
582 | } | |
583 | ||
57fb233f KO |
584 | /* |
585 | * bio_set is used to allow other portions of the IO system to | |
586 | * allocate their own private memory pools for bio and iovec structures. | |
587 | * These memory pools in turn all allocate from the bio_slab | |
588 | * and the bvec_slabs[]. | |
589 | */ | |
590 | #define BIO_POOL_SIZE 2 | |
591 | #define BIOVEC_NR_POOLS 6 | |
592 | #define BIOVEC_MAX_IDX (BIOVEC_NR_POOLS - 1) | |
593 | ||
594 | struct bio_set { | |
595 | struct kmem_cache *bio_slab; | |
596 | unsigned int front_pad; | |
597 | ||
598 | mempool_t *bio_pool; | |
9f060e22 | 599 | mempool_t *bvec_pool; |
57fb233f KO |
600 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
601 | mempool_t *bio_integrity_pool; | |
9f060e22 | 602 | mempool_t *bvec_integrity_pool; |
57fb233f | 603 | #endif |
df2cb6da KO |
604 | |
605 | /* | |
606 | * Deadlock avoidance for stacking block drivers: see comments in | |
607 | * bio_alloc_bioset() for details | |
608 | */ | |
609 | spinlock_t rescue_lock; | |
610 | struct bio_list rescue_list; | |
611 | struct work_struct rescue_work; | |
612 | struct workqueue_struct *rescue_workqueue; | |
57fb233f KO |
613 | }; |
614 | ||
615 | struct biovec_slab { | |
616 | int nr_vecs; | |
617 | char *name; | |
618 | struct kmem_cache *slab; | |
619 | }; | |
620 | ||
621 | /* | |
622 | * a small number of entries is fine, not going to be performance critical. | |
623 | * basically we just need to survive | |
624 | */ | |
625 | #define BIO_SPLIT_ENTRIES 2 | |
626 | ||
7ba1ba12 MP |
627 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
628 | ||
629 | #define bip_vec_idx(bip, idx) (&(bip->bip_vec[(idx)])) | |
630 | #define bip_vec(bip) bip_vec_idx(bip, 0) | |
631 | ||
632 | #define __bip_for_each_vec(bvl, bip, i, start_idx) \ | |
633 | for (bvl = bip_vec_idx((bip), (start_idx)), i = (start_idx); \ | |
634 | i < (bip)->bip_vcnt; \ | |
635 | bvl++, i++) | |
636 | ||
637 | #define bip_for_each_vec(bvl, bip, i) \ | |
638 | __bip_for_each_vec(bvl, bip, i, (bip)->bip_idx) | |
639 | ||
13f05c8d MP |
640 | #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \ |
641 | for_each_bio(_bio) \ | |
642 | bip_for_each_vec(_bvl, _bio->bi_integrity, _iter) | |
643 | ||
8deaf721 | 644 | #define bio_integrity(bio) (bio->bi_integrity != NULL) |
7ba1ba12 | 645 | |
7ba1ba12 | 646 | extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); |
1e2a410f | 647 | extern void bio_integrity_free(struct bio *); |
7ba1ba12 MP |
648 | extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); |
649 | extern int bio_integrity_enabled(struct bio *bio); | |
650 | extern int bio_integrity_set_tag(struct bio *, void *, unsigned int); | |
651 | extern int bio_integrity_get_tag(struct bio *, void *, unsigned int); | |
652 | extern int bio_integrity_prep(struct bio *); | |
653 | extern void bio_integrity_endio(struct bio *, int); | |
654 | extern void bio_integrity_advance(struct bio *, unsigned int); | |
655 | extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int); | |
656 | extern void bio_integrity_split(struct bio *, struct bio_pair *, int); | |
1e2a410f | 657 | extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t); |
7878cba9 MP |
658 | extern int bioset_integrity_create(struct bio_set *, int); |
659 | extern void bioset_integrity_free(struct bio_set *); | |
660 | extern void bio_integrity_init(void); | |
7ba1ba12 MP |
661 | |
662 | #else /* CONFIG_BLK_DEV_INTEGRITY */ | |
663 | ||
6898e3bd MP |
664 | static inline int bio_integrity(struct bio *bio) |
665 | { | |
666 | return 0; | |
667 | } | |
668 | ||
669 | static inline int bio_integrity_enabled(struct bio *bio) | |
670 | { | |
671 | return 0; | |
672 | } | |
673 | ||
674 | static inline int bioset_integrity_create(struct bio_set *bs, int pool_size) | |
675 | { | |
676 | return 0; | |
677 | } | |
678 | ||
679 | static inline void bioset_integrity_free (struct bio_set *bs) | |
680 | { | |
681 | return; | |
682 | } | |
683 | ||
684 | static inline int bio_integrity_prep(struct bio *bio) | |
685 | { | |
686 | return 0; | |
687 | } | |
688 | ||
1e2a410f | 689 | static inline void bio_integrity_free(struct bio *bio) |
6898e3bd MP |
690 | { |
691 | return; | |
692 | } | |
693 | ||
0c614e2d | 694 | static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src, |
1e2a410f | 695 | gfp_t gfp_mask) |
0c614e2d SR |
696 | { |
697 | return 0; | |
698 | } | |
6898e3bd MP |
699 | |
700 | static inline void bio_integrity_split(struct bio *bio, struct bio_pair *bp, | |
701 | int sectors) | |
702 | { | |
703 | return; | |
704 | } | |
705 | ||
706 | static inline void bio_integrity_advance(struct bio *bio, | |
707 | unsigned int bytes_done) | |
708 | { | |
709 | return; | |
710 | } | |
711 | ||
712 | static inline void bio_integrity_trim(struct bio *bio, unsigned int offset, | |
713 | unsigned int sectors) | |
714 | { | |
715 | return; | |
716 | } | |
717 | ||
718 | static inline void bio_integrity_init(void) | |
719 | { | |
720 | return; | |
721 | } | |
7ba1ba12 MP |
722 | |
723 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
724 | ||
02a5e0ac | 725 | #endif /* CONFIG_BLOCK */ |
1da177e4 | 726 | #endif /* __LINUX_BIO_H */ |