]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
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 | |
7cc01581 | 10 | * |
1da177e4 LT |
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 | ||
21 | #include <linux/highmem.h> | |
22 | #include <linux/mempool.h> | |
22e2c507 | 23 | #include <linux/ioprio.h> |
1da177e4 | 24 | |
02a5e0ac | 25 | #ifdef CONFIG_BLOCK |
7cc01581 TH |
26 | /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */ |
27 | #include <linux/blk_types.h> | |
28 | ||
1da177e4 LT |
29 | #define BIO_DEBUG |
30 | ||
31 | #ifdef BIO_DEBUG | |
32 | #define BIO_BUG_ON BUG_ON | |
33 | #else | |
34 | #define BIO_BUG_ON | |
35 | #endif | |
36 | ||
225311a4 HY |
37 | #ifdef CONFIG_THP_SWAP |
38 | #if HPAGE_PMD_NR > 256 | |
39 | #define BIO_MAX_PAGES HPAGE_PMD_NR | |
40 | #else | |
d84a8477 | 41 | #define BIO_MAX_PAGES 256 |
225311a4 HY |
42 | #endif |
43 | #else | |
44 | #define BIO_MAX_PAGES 256 | |
45 | #endif | |
1da177e4 | 46 | |
43b62ce3 MC |
47 | #define bio_prio(bio) (bio)->bi_ioprio |
48 | #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio) | |
22e2c507 | 49 | |
4550dd6c KO |
50 | #define bio_iter_iovec(bio, iter) \ |
51 | bvec_iter_bvec((bio)->bi_io_vec, (iter)) | |
52 | ||
53 | #define bio_iter_page(bio, iter) \ | |
54 | bvec_iter_page((bio)->bi_io_vec, (iter)) | |
55 | #define bio_iter_len(bio, iter) \ | |
56 | bvec_iter_len((bio)->bi_io_vec, (iter)) | |
57 | #define bio_iter_offset(bio, iter) \ | |
58 | bvec_iter_offset((bio)->bi_io_vec, (iter)) | |
59 | ||
60 | #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter) | |
61 | #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter) | |
62 | #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter) | |
7988613b | 63 | |
458b76ed KO |
64 | #define bio_multiple_segments(bio) \ |
65 | ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len) | |
38a72dac KO |
66 | |
67 | #define bvec_iter_sectors(iter) ((iter).bi_size >> 9) | |
68 | #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter))) | |
69 | ||
70 | #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter) | |
71 | #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter) | |
bf2de6f5 | 72 | |
d3849953 CH |
73 | /* |
74 | * Return the data direction, READ or WRITE. | |
75 | */ | |
76 | #define bio_data_dir(bio) \ | |
77 | (op_is_write(bio_op(bio)) ? WRITE : READ) | |
78 | ||
458b76ed KO |
79 | /* |
80 | * Check whether this bio carries any data or not. A NULL bio is allowed. | |
81 | */ | |
82 | static inline bool bio_has_data(struct bio *bio) | |
83 | { | |
84 | if (bio && | |
85 | bio->bi_iter.bi_size && | |
7afafc8a | 86 | bio_op(bio) != REQ_OP_DISCARD && |
a6f0788e CK |
87 | bio_op(bio) != REQ_OP_SECURE_ERASE && |
88 | bio_op(bio) != REQ_OP_WRITE_ZEROES) | |
458b76ed KO |
89 | return true; |
90 | ||
91 | return false; | |
92 | } | |
93 | ||
95fe6c1a MC |
94 | static inline bool bio_no_advance_iter(struct bio *bio) |
95 | { | |
7afafc8a AH |
96 | return bio_op(bio) == REQ_OP_DISCARD || |
97 | bio_op(bio) == REQ_OP_SECURE_ERASE || | |
a6f0788e CK |
98 | bio_op(bio) == REQ_OP_WRITE_SAME || |
99 | bio_op(bio) == REQ_OP_WRITE_ZEROES; | |
95fe6c1a MC |
100 | } |
101 | ||
458b76ed KO |
102 | static inline bool bio_mergeable(struct bio *bio) |
103 | { | |
1eff9d32 | 104 | if (bio->bi_opf & REQ_NOMERGE_FLAGS) |
458b76ed KO |
105 | return false; |
106 | ||
107 | return true; | |
108 | } | |
109 | ||
2e46e8b2 | 110 | static inline unsigned int bio_cur_bytes(struct bio *bio) |
bf2de6f5 | 111 | { |
458b76ed | 112 | if (bio_has_data(bio)) |
a4ad39b1 | 113 | return bio_iovec(bio).bv_len; |
fb2dce86 | 114 | else /* dataless requests such as discard */ |
4f024f37 | 115 | return bio->bi_iter.bi_size; |
bf2de6f5 JA |
116 | } |
117 | ||
118 | static inline void *bio_data(struct bio *bio) | |
119 | { | |
458b76ed | 120 | if (bio_has_data(bio)) |
bf2de6f5 JA |
121 | return page_address(bio_page(bio)) + bio_offset(bio); |
122 | ||
123 | return NULL; | |
124 | } | |
1da177e4 | 125 | |
0aa69fd3 CH |
126 | static inline bool bio_full(struct bio *bio) |
127 | { | |
128 | return bio->bi_vcnt >= bio->bi_max_vecs; | |
129 | } | |
130 | ||
d74c6d51 KO |
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) \ | |
f619d254 | 136 | for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++) |
d74c6d51 | 137 | |
4550dd6c KO |
138 | static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, |
139 | unsigned bytes) | |
140 | { | |
141 | iter->bi_sector += bytes >> 9; | |
142 | ||
7759eb23 | 143 | if (bio_no_advance_iter(bio)) |
4550dd6c | 144 | iter->bi_size -= bytes; |
7759eb23 | 145 | else |
4550dd6c | 146 | bvec_iter_advance(bio->bi_io_vec, iter, bytes); |
b1fb2c52 | 147 | /* TODO: It is reasonable to complete bio with error here. */ |
f9df1cd9 DM |
148 | } |
149 | ||
7988613b KO |
150 | #define __bio_for_each_segment(bvl, bio, iter, start) \ |
151 | for (iter = (start); \ | |
4550dd6c KO |
152 | (iter).bi_size && \ |
153 | ((bvl = bio_iter_iovec((bio), (iter))), 1); \ | |
154 | bio_advance_iter((bio), &(iter), (bvl).bv_len)) | |
7988613b KO |
155 | |
156 | #define bio_for_each_segment(bvl, bio, iter) \ | |
157 | __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter) | |
158 | ||
4550dd6c | 159 | #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) |
1da177e4 | 160 | |
f4595875 | 161 | static inline unsigned bio_segments(struct bio *bio) |
458b76ed KO |
162 | { |
163 | unsigned segs = 0; | |
164 | struct bio_vec bv; | |
165 | struct bvec_iter iter; | |
166 | ||
8423ae3d | 167 | /* |
a6f0788e CK |
168 | * We special case discard/write same/write zeroes, because they |
169 | * interpret bi_size differently: | |
8423ae3d KO |
170 | */ |
171 | ||
a6f0788e CK |
172 | switch (bio_op(bio)) { |
173 | case REQ_OP_DISCARD: | |
174 | case REQ_OP_SECURE_ERASE: | |
a6f0788e | 175 | case REQ_OP_WRITE_ZEROES: |
f9d03f96 CH |
176 | return 0; |
177 | case REQ_OP_WRITE_SAME: | |
8423ae3d | 178 | return 1; |
a6f0788e CK |
179 | default: |
180 | break; | |
181 | } | |
8423ae3d | 182 | |
f4595875 | 183 | bio_for_each_segment(bv, bio, iter) |
458b76ed KO |
184 | segs++; |
185 | ||
186 | return segs; | |
187 | } | |
188 | ||
1da177e4 LT |
189 | /* |
190 | * get a reference to a bio, so it won't disappear. the intended use is | |
191 | * something like: | |
192 | * | |
193 | * bio_get(bio); | |
194 | * submit_bio(rw, bio); | |
195 | * if (bio->bi_flags ...) | |
196 | * do_something | |
197 | * bio_put(bio); | |
198 | * | |
199 | * without the bio_get(), it could potentially complete I/O before submit_bio | |
200 | * returns. and then bio would be freed memory when if (bio->bi_flags ...) | |
201 | * runs | |
202 | */ | |
dac56212 JA |
203 | static inline void bio_get(struct bio *bio) |
204 | { | |
205 | bio->bi_flags |= (1 << BIO_REFFED); | |
206 | smp_mb__before_atomic(); | |
207 | atomic_inc(&bio->__bi_cnt); | |
208 | } | |
209 | ||
210 | static inline void bio_cnt_set(struct bio *bio, unsigned int count) | |
211 | { | |
212 | if (count != 1) { | |
213 | bio->bi_flags |= (1 << BIO_REFFED); | |
214 | smp_mb__before_atomic(); | |
215 | } | |
216 | atomic_set(&bio->__bi_cnt, count); | |
217 | } | |
1da177e4 | 218 | |
b7c44ed9 JA |
219 | static inline bool bio_flagged(struct bio *bio, unsigned int bit) |
220 | { | |
2c68f6dc | 221 | return (bio->bi_flags & (1U << bit)) != 0; |
b7c44ed9 JA |
222 | } |
223 | ||
224 | static inline void bio_set_flag(struct bio *bio, unsigned int bit) | |
225 | { | |
2c68f6dc | 226 | bio->bi_flags |= (1U << bit); |
b7c44ed9 JA |
227 | } |
228 | ||
229 | static inline void bio_clear_flag(struct bio *bio, unsigned int bit) | |
230 | { | |
2c68f6dc | 231 | bio->bi_flags &= ~(1U << bit); |
b7c44ed9 JA |
232 | } |
233 | ||
7bcd79ac ML |
234 | static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) |
235 | { | |
236 | *bv = bio_iovec(bio); | |
237 | } | |
238 | ||
239 | static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) | |
240 | { | |
241 | struct bvec_iter iter = bio->bi_iter; | |
242 | int idx; | |
243 | ||
7bcd79ac ML |
244 | if (unlikely(!bio_multiple_segments(bio))) { |
245 | *bv = bio_iovec(bio); | |
246 | return; | |
247 | } | |
248 | ||
249 | bio_advance_iter(bio, &iter, iter.bi_size); | |
250 | ||
251 | if (!iter.bi_bvec_done) | |
252 | idx = iter.bi_idx - 1; | |
253 | else /* in the middle of bvec */ | |
254 | idx = iter.bi_idx; | |
255 | ||
256 | *bv = bio->bi_io_vec[idx]; | |
257 | ||
258 | /* | |
259 | * iter.bi_bvec_done records actual length of the last bvec | |
260 | * if this bio ends in the middle of one io vector | |
261 | */ | |
262 | if (iter.bi_bvec_done) | |
263 | bv->bv_len = iter.bi_bvec_done; | |
264 | } | |
265 | ||
86292abc ML |
266 | static inline unsigned bio_pages_all(struct bio *bio) |
267 | { | |
268 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); | |
269 | return bio->bi_vcnt; | |
270 | } | |
271 | ||
272 | static inline struct bio_vec *bio_first_bvec_all(struct bio *bio) | |
273 | { | |
274 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); | |
275 | return bio->bi_io_vec; | |
276 | } | |
277 | ||
278 | static inline struct page *bio_first_page_all(struct bio *bio) | |
279 | { | |
280 | return bio_first_bvec_all(bio)->bv_page; | |
281 | } | |
282 | ||
283 | static inline struct bio_vec *bio_last_bvec_all(struct bio *bio) | |
284 | { | |
285 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); | |
286 | return &bio->bi_io_vec[bio->bi_vcnt - 1]; | |
287 | } | |
288 | ||
c611529e MP |
289 | enum bip_flags { |
290 | BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */ | |
291 | BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */ | |
292 | BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */ | |
293 | BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */ | |
294 | BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */ | |
295 | }; | |
296 | ||
7ba1ba12 MP |
297 | /* |
298 | * bio integrity payload | |
299 | */ | |
300 | struct bio_integrity_payload { | |
301 | struct bio *bip_bio; /* parent bio */ | |
7ba1ba12 | 302 | |
d57a5f7c | 303 | struct bvec_iter bip_iter; |
7ba1ba12 | 304 | |
7878cba9 | 305 | unsigned short bip_slab; /* slab the bip came from */ |
7ba1ba12 | 306 | unsigned short bip_vcnt; /* # of integrity bio_vecs */ |
cbcd1054 | 307 | unsigned short bip_max_vcnt; /* integrity bio_vec slots */ |
b1f01388 | 308 | unsigned short bip_flags; /* control flags */ |
7ba1ba12 | 309 | |
7759eb23 ML |
310 | struct bvec_iter bio_iter; /* for rewinding parent bio */ |
311 | ||
7ba1ba12 | 312 | struct work_struct bip_work; /* I/O completion */ |
6fda981c KO |
313 | |
314 | struct bio_vec *bip_vec; | |
315 | struct bio_vec bip_inline_vecs[0];/* embedded bvec array */ | |
7ba1ba12 | 316 | }; |
18593088 | 317 | |
06c1e390 KB |
318 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
319 | ||
320 | static inline struct bio_integrity_payload *bio_integrity(struct bio *bio) | |
321 | { | |
1eff9d32 | 322 | if (bio->bi_opf & REQ_INTEGRITY) |
06c1e390 KB |
323 | return bio->bi_integrity; |
324 | ||
325 | return NULL; | |
326 | } | |
327 | ||
c611529e MP |
328 | static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag) |
329 | { | |
330 | struct bio_integrity_payload *bip = bio_integrity(bio); | |
331 | ||
332 | if (bip) | |
333 | return bip->bip_flags & flag; | |
334 | ||
335 | return false; | |
336 | } | |
b1f01388 | 337 | |
18593088 MP |
338 | static inline sector_t bip_get_seed(struct bio_integrity_payload *bip) |
339 | { | |
340 | return bip->bip_iter.bi_sector; | |
341 | } | |
342 | ||
343 | static inline void bip_set_seed(struct bio_integrity_payload *bip, | |
344 | sector_t seed) | |
345 | { | |
346 | bip->bip_iter.bi_sector = seed; | |
347 | } | |
348 | ||
7ba1ba12 | 349 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
1da177e4 | 350 | |
6678d83f | 351 | extern void bio_trim(struct bio *bio, int offset, int size); |
20d0189b KO |
352 | extern struct bio *bio_split(struct bio *bio, int sectors, |
353 | gfp_t gfp, struct bio_set *bs); | |
354 | ||
355 | /** | |
356 | * bio_next_split - get next @sectors from a bio, splitting if necessary | |
357 | * @bio: bio to split | |
358 | * @sectors: number of sectors to split from the front of @bio | |
359 | * @gfp: gfp mask | |
360 | * @bs: bio set to allocate from | |
361 | * | |
362 | * Returns a bio representing the next @sectors of @bio - if the bio is smaller | |
363 | * than @sectors, returns the original bio unchanged. | |
364 | */ | |
365 | static inline struct bio *bio_next_split(struct bio *bio, int sectors, | |
366 | gfp_t gfp, struct bio_set *bs) | |
367 | { | |
368 | if (sectors >= bio_sectors(bio)) | |
369 | return bio; | |
370 | ||
371 | return bio_split(bio, sectors, gfp, bs); | |
372 | } | |
373 | ||
011067b0 N |
374 | enum { |
375 | BIOSET_NEED_BVECS = BIT(0), | |
47e0fb46 | 376 | BIOSET_NEED_RESCUER = BIT(1), |
011067b0 | 377 | }; |
dad08527 KO |
378 | extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags); |
379 | extern void bioset_exit(struct bio_set *); | |
8aa6ba2f | 380 | extern int biovec_init_pool(mempool_t *pool, int pool_entries); |
28e89fd9 | 381 | extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src); |
1da177e4 | 382 | |
7a88fa19 | 383 | extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *); |
1da177e4 LT |
384 | extern void bio_put(struct bio *); |
385 | ||
59d276fe KO |
386 | extern void __bio_clone_fast(struct bio *, struct bio *); |
387 | extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *); | |
bf800ef1 | 388 | |
f4f8154a | 389 | extern struct bio_set fs_bio_set; |
3f86a82a KO |
390 | |
391 | static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) | |
392 | { | |
f4f8154a | 393 | return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set); |
3f86a82a KO |
394 | } |
395 | ||
396 | static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs) | |
397 | { | |
398 | return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL); | |
399 | } | |
400 | ||
1e3914d4 CH |
401 | extern blk_qc_t submit_bio(struct bio *); |
402 | ||
4246a0b6 CH |
403 | extern void bio_endio(struct bio *); |
404 | ||
405 | static inline void bio_io_error(struct bio *bio) | |
406 | { | |
4e4cbee9 | 407 | bio->bi_status = BLK_STS_IOERR; |
4246a0b6 CH |
408 | bio_endio(bio); |
409 | } | |
410 | ||
03a07c92 GR |
411 | static inline void bio_wouldblock_error(struct bio *bio) |
412 | { | |
413 | bio->bi_status = BLK_STS_AGAIN; | |
4246a0b6 CH |
414 | bio_endio(bio); |
415 | } | |
416 | ||
1da177e4 LT |
417 | struct request_queue; |
418 | extern int bio_phys_segments(struct request_queue *, struct bio *); | |
1da177e4 | 419 | |
4e49ea4a | 420 | extern int submit_bio_wait(struct bio *bio); |
054bdf64 KO |
421 | extern void bio_advance(struct bio *, unsigned); |
422 | ||
3a83f467 ML |
423 | extern void bio_init(struct bio *bio, struct bio_vec *table, |
424 | unsigned short max_vecs); | |
9ae3b3f5 | 425 | extern void bio_uninit(struct bio *); |
f44b48c7 | 426 | extern void bio_reset(struct bio *); |
196d38bc | 427 | void bio_chain(struct bio *, struct bio *); |
1da177e4 LT |
428 | |
429 | extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int); | |
6e68af66 MC |
430 | extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, |
431 | unsigned int, unsigned int); | |
0aa69fd3 CH |
432 | bool __bio_try_merge_page(struct bio *bio, struct page *page, |
433 | unsigned int len, unsigned int off); | |
434 | void __bio_add_page(struct bio *bio, struct page *page, | |
435 | unsigned int len, unsigned int off); | |
2cefe4db | 436 | int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter); |
152e283f | 437 | struct rq_map_data; |
f1970baf | 438 | extern struct bio *bio_map_user_iov(struct request_queue *, |
e81cef5d | 439 | struct iov_iter *, gfp_t); |
1da177e4 | 440 | extern void bio_unmap_user(struct bio *); |
df46b9a4 | 441 | extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int, |
27496a8c | 442 | gfp_t); |
68154e90 FT |
443 | extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int, |
444 | gfp_t, int); | |
1da177e4 LT |
445 | extern void bio_set_pages_dirty(struct bio *bio); |
446 | extern void bio_check_pages_dirty(struct bio *bio); | |
2d4dc890 | 447 | |
ddcf35d3 | 448 | void generic_start_io_acct(struct request_queue *q, int op, |
d62e26b3 | 449 | unsigned long sectors, struct hd_struct *part); |
ddcf35d3 | 450 | void generic_end_io_acct(struct request_queue *q, int op, |
d62e26b3 JA |
451 | struct hd_struct *part, |
452 | unsigned long start_time); | |
394ffa50 | 453 | |
2d4dc890 IL |
454 | #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
455 | # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform" | |
456 | #endif | |
457 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE | |
458 | extern void bio_flush_dcache_pages(struct bio *bi); | |
459 | #else | |
460 | static inline void bio_flush_dcache_pages(struct bio *bi) | |
461 | { | |
462 | } | |
463 | #endif | |
464 | ||
45db54d5 KO |
465 | extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, |
466 | struct bio *src, struct bvec_iter *src_iter); | |
16ac3d63 | 467 | extern void bio_copy_data(struct bio *dst, struct bio *src); |
45db54d5 | 468 | extern void bio_list_copy_data(struct bio *dst, struct bio *src); |
491221f8 | 469 | extern void bio_free_pages(struct bio *bio); |
16ac3d63 | 470 | |
152e283f | 471 | extern struct bio *bio_copy_user_iov(struct request_queue *, |
86d564c8 | 472 | struct rq_map_data *, |
e81cef5d | 473 | struct iov_iter *, |
26e49cfc | 474 | gfp_t); |
1da177e4 | 475 | extern int bio_uncopy_user(struct bio *); |
38a72dac KO |
476 | void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter); |
477 | ||
478 | static inline void zero_fill_bio(struct bio *bio) | |
479 | { | |
480 | zero_fill_bio_iter(bio, bio->bi_iter); | |
481 | } | |
482 | ||
9f060e22 KO |
483 | extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *); |
484 | extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int); | |
7ba1ba12 | 485 | extern unsigned int bvec_nr_vecs(unsigned short idx); |
9c0fb1e3 | 486 | extern const char *bio_devname(struct bio *bio, char *buffer); |
51d654e1 | 487 | |
74d46992 CH |
488 | #define bio_set_dev(bio, bdev) \ |
489 | do { \ | |
111be883 SL |
490 | if ((bio)->bi_disk != (bdev)->bd_disk) \ |
491 | bio_clear_flag(bio, BIO_THROTTLED);\ | |
74d46992 CH |
492 | (bio)->bi_disk = (bdev)->bd_disk; \ |
493 | (bio)->bi_partno = (bdev)->bd_partno; \ | |
494 | } while (0) | |
495 | ||
496 | #define bio_copy_dev(dst, src) \ | |
497 | do { \ | |
498 | (dst)->bi_disk = (src)->bi_disk; \ | |
499 | (dst)->bi_partno = (src)->bi_partno; \ | |
500 | } while (0) | |
501 | ||
502 | #define bio_dev(bio) \ | |
503 | disk_devt((bio)->bi_disk) | |
504 | ||
0d3bd88d | 505 | #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) |
74b7c02a | 506 | int bio_associate_blkg_from_page(struct bio *bio, struct page *page); |
0d3bd88d | 507 | #else |
74b7c02a DZF |
508 | static inline int bio_associate_blkg_from_page(struct bio *bio, |
509 | struct page *page) { return 0; } | |
0d3bd88d TH |
510 | #endif |
511 | ||
852c788f | 512 | #ifdef CONFIG_BLK_CGROUP |
08e18eab | 513 | int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg); |
74b7c02a DZF |
514 | int bio_associate_blkg_from_css(struct bio *bio, |
515 | struct cgroup_subsys_state *css); | |
a7b39b4e | 516 | int bio_associate_create_blkg(struct request_queue *q, struct bio *bio); |
d459d853 | 517 | int bio_reassociate_blkg(struct request_queue *q, struct bio *bio); |
852c788f | 518 | void bio_disassociate_task(struct bio *bio); |
c839e7a0 | 519 | void bio_clone_blkg_association(struct bio *dst, struct bio *src); |
852c788f | 520 | #else /* CONFIG_BLK_CGROUP */ |
74b7c02a DZF |
521 | static inline int bio_associate_blkg_from_css(struct bio *bio, |
522 | struct cgroup_subsys_state *css) | |
523 | { return 0; } | |
a7b39b4e DZF |
524 | static inline int bio_associate_create_blkg(struct request_queue *q, |
525 | struct bio *bio) { return 0; } | |
d459d853 DZ |
526 | static inline int bio_reassociate_blkg(struct request_queue *q, struct bio *bio) |
527 | { return 0; } | |
852c788f | 528 | static inline void bio_disassociate_task(struct bio *bio) { } |
c839e7a0 DZF |
529 | static inline void bio_clone_blkg_association(struct bio *dst, |
530 | struct bio *src) { } | |
852c788f TH |
531 | #endif /* CONFIG_BLK_CGROUP */ |
532 | ||
1da177e4 LT |
533 | #ifdef CONFIG_HIGHMEM |
534 | /* | |
20b636bf AB |
535 | * remember never ever reenable interrupts between a bvec_kmap_irq and |
536 | * bvec_kunmap_irq! | |
1da177e4 | 537 | */ |
4f570f99 | 538 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
1da177e4 LT |
539 | { |
540 | unsigned long addr; | |
541 | ||
542 | /* | |
543 | * might not be a highmem page, but the preempt/irq count | |
544 | * balancing is a lot nicer this way | |
545 | */ | |
546 | local_irq_save(*flags); | |
e8e3c3d6 | 547 | addr = (unsigned long) kmap_atomic(bvec->bv_page); |
1da177e4 LT |
548 | |
549 | BUG_ON(addr & ~PAGE_MASK); | |
550 | ||
551 | return (char *) addr + bvec->bv_offset; | |
552 | } | |
553 | ||
4f570f99 | 554 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) |
1da177e4 LT |
555 | { |
556 | unsigned long ptr = (unsigned long) buffer & PAGE_MASK; | |
557 | ||
e8e3c3d6 | 558 | kunmap_atomic((void *) ptr); |
1da177e4 LT |
559 | local_irq_restore(*flags); |
560 | } | |
561 | ||
562 | #else | |
11a691be GU |
563 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
564 | { | |
565 | return page_address(bvec->bv_page) + bvec->bv_offset; | |
566 | } | |
567 | ||
568 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) | |
569 | { | |
570 | *flags = 0; | |
571 | } | |
1da177e4 LT |
572 | #endif |
573 | ||
8f3d8ba2 | 574 | /* |
e686307f | 575 | * BIO list management for use by remapping drivers (e.g. DM or MD) and loop. |
8f3d8ba2 CH |
576 | * |
577 | * A bio_list anchors a singly-linked list of bios chained through the bi_next | |
578 | * member of the bio. The bio_list also caches the last list member to allow | |
579 | * fast access to the tail. | |
580 | */ | |
581 | struct bio_list { | |
582 | struct bio *head; | |
583 | struct bio *tail; | |
584 | }; | |
585 | ||
586 | static inline int bio_list_empty(const struct bio_list *bl) | |
587 | { | |
588 | return bl->head == NULL; | |
589 | } | |
590 | ||
591 | static inline void bio_list_init(struct bio_list *bl) | |
592 | { | |
593 | bl->head = bl->tail = NULL; | |
594 | } | |
595 | ||
320ae51f JA |
596 | #define BIO_EMPTY_LIST { NULL, NULL } |
597 | ||
8f3d8ba2 CH |
598 | #define bio_list_for_each(bio, bl) \ |
599 | for (bio = (bl)->head; bio; bio = bio->bi_next) | |
600 | ||
601 | static inline unsigned bio_list_size(const struct bio_list *bl) | |
602 | { | |
603 | unsigned sz = 0; | |
604 | struct bio *bio; | |
605 | ||
606 | bio_list_for_each(bio, bl) | |
607 | sz++; | |
608 | ||
609 | return sz; | |
610 | } | |
611 | ||
612 | static inline void bio_list_add(struct bio_list *bl, struct bio *bio) | |
613 | { | |
614 | bio->bi_next = NULL; | |
615 | ||
616 | if (bl->tail) | |
617 | bl->tail->bi_next = bio; | |
618 | else | |
619 | bl->head = bio; | |
620 | ||
621 | bl->tail = bio; | |
622 | } | |
623 | ||
624 | static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio) | |
625 | { | |
626 | bio->bi_next = bl->head; | |
627 | ||
628 | bl->head = bio; | |
629 | ||
630 | if (!bl->tail) | |
631 | bl->tail = bio; | |
632 | } | |
633 | ||
634 | static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) | |
635 | { | |
636 | if (!bl2->head) | |
637 | return; | |
638 | ||
639 | if (bl->tail) | |
640 | bl->tail->bi_next = bl2->head; | |
641 | else | |
642 | bl->head = bl2->head; | |
643 | ||
644 | bl->tail = bl2->tail; | |
645 | } | |
646 | ||
647 | static inline void bio_list_merge_head(struct bio_list *bl, | |
648 | struct bio_list *bl2) | |
649 | { | |
650 | if (!bl2->head) | |
651 | return; | |
652 | ||
653 | if (bl->head) | |
654 | bl2->tail->bi_next = bl->head; | |
655 | else | |
656 | bl->tail = bl2->tail; | |
657 | ||
658 | bl->head = bl2->head; | |
659 | } | |
660 | ||
13685a16 GU |
661 | static inline struct bio *bio_list_peek(struct bio_list *bl) |
662 | { | |
663 | return bl->head; | |
664 | } | |
665 | ||
8f3d8ba2 CH |
666 | static inline struct bio *bio_list_pop(struct bio_list *bl) |
667 | { | |
668 | struct bio *bio = bl->head; | |
669 | ||
670 | if (bio) { | |
671 | bl->head = bl->head->bi_next; | |
672 | if (!bl->head) | |
673 | bl->tail = NULL; | |
674 | ||
675 | bio->bi_next = NULL; | |
676 | } | |
677 | ||
678 | return bio; | |
679 | } | |
680 | ||
681 | static inline struct bio *bio_list_get(struct bio_list *bl) | |
682 | { | |
683 | struct bio *bio = bl->head; | |
684 | ||
685 | bl->head = bl->tail = NULL; | |
686 | ||
687 | return bio; | |
688 | } | |
689 | ||
0ef5a50c MS |
690 | /* |
691 | * Increment chain count for the bio. Make sure the CHAIN flag update | |
692 | * is visible before the raised count. | |
693 | */ | |
694 | static inline void bio_inc_remaining(struct bio *bio) | |
695 | { | |
696 | bio_set_flag(bio, BIO_CHAIN); | |
697 | smp_mb__before_atomic(); | |
698 | atomic_inc(&bio->__bi_remaining); | |
699 | } | |
700 | ||
57fb233f KO |
701 | /* |
702 | * bio_set is used to allow other portions of the IO system to | |
703 | * allocate their own private memory pools for bio and iovec structures. | |
704 | * These memory pools in turn all allocate from the bio_slab | |
705 | * and the bvec_slabs[]. | |
706 | */ | |
707 | #define BIO_POOL_SIZE 2 | |
57fb233f KO |
708 | |
709 | struct bio_set { | |
710 | struct kmem_cache *bio_slab; | |
711 | unsigned int front_pad; | |
712 | ||
8aa6ba2f KO |
713 | mempool_t bio_pool; |
714 | mempool_t bvec_pool; | |
57fb233f | 715 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
8aa6ba2f KO |
716 | mempool_t bio_integrity_pool; |
717 | mempool_t bvec_integrity_pool; | |
57fb233f | 718 | #endif |
df2cb6da KO |
719 | |
720 | /* | |
721 | * Deadlock avoidance for stacking block drivers: see comments in | |
722 | * bio_alloc_bioset() for details | |
723 | */ | |
724 | spinlock_t rescue_lock; | |
725 | struct bio_list rescue_list; | |
726 | struct work_struct rescue_work; | |
727 | struct workqueue_struct *rescue_workqueue; | |
57fb233f KO |
728 | }; |
729 | ||
730 | struct biovec_slab { | |
731 | int nr_vecs; | |
732 | char *name; | |
733 | struct kmem_cache *slab; | |
734 | }; | |
735 | ||
338aa96d KO |
736 | static inline bool bioset_initialized(struct bio_set *bs) |
737 | { | |
738 | return bs->bio_slab != NULL; | |
739 | } | |
740 | ||
57fb233f KO |
741 | /* |
742 | * a small number of entries is fine, not going to be performance critical. | |
743 | * basically we just need to survive | |
744 | */ | |
745 | #define BIO_SPLIT_ENTRIES 2 | |
746 | ||
7ba1ba12 MP |
747 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
748 | ||
d57a5f7c KO |
749 | #define bip_for_each_vec(bvl, bip, iter) \ |
750 | for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter) | |
7ba1ba12 | 751 | |
13f05c8d MP |
752 | #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \ |
753 | for_each_bio(_bio) \ | |
754 | bip_for_each_vec(_bvl, _bio->bi_integrity, _iter) | |
755 | ||
7ba1ba12 | 756 | extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); |
7ba1ba12 | 757 | extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); |
e23947bd | 758 | extern bool bio_integrity_prep(struct bio *); |
7ba1ba12 | 759 | extern void bio_integrity_advance(struct bio *, unsigned int); |
fbd08e76 | 760 | extern void bio_integrity_trim(struct bio *); |
1e2a410f | 761 | extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t); |
7878cba9 MP |
762 | extern int bioset_integrity_create(struct bio_set *, int); |
763 | extern void bioset_integrity_free(struct bio_set *); | |
764 | extern void bio_integrity_init(void); | |
7ba1ba12 MP |
765 | |
766 | #else /* CONFIG_BLK_DEV_INTEGRITY */ | |
767 | ||
c611529e | 768 | static inline void *bio_integrity(struct bio *bio) |
6898e3bd | 769 | { |
c611529e | 770 | return NULL; |
6898e3bd MP |
771 | } |
772 | ||
6898e3bd MP |
773 | static inline int bioset_integrity_create(struct bio_set *bs, int pool_size) |
774 | { | |
775 | return 0; | |
776 | } | |
777 | ||
778 | static inline void bioset_integrity_free (struct bio_set *bs) | |
779 | { | |
780 | return; | |
781 | } | |
782 | ||
e23947bd | 783 | static inline bool bio_integrity_prep(struct bio *bio) |
6898e3bd | 784 | { |
e23947bd | 785 | return true; |
6898e3bd MP |
786 | } |
787 | ||
0c614e2d | 788 | static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src, |
1e2a410f | 789 | gfp_t gfp_mask) |
0c614e2d SR |
790 | { |
791 | return 0; | |
792 | } | |
6898e3bd | 793 | |
6898e3bd MP |
794 | static inline void bio_integrity_advance(struct bio *bio, |
795 | unsigned int bytes_done) | |
796 | { | |
797 | return; | |
798 | } | |
799 | ||
fbd08e76 | 800 | static inline void bio_integrity_trim(struct bio *bio) |
6898e3bd MP |
801 | { |
802 | return; | |
803 | } | |
804 | ||
805 | static inline void bio_integrity_init(void) | |
806 | { | |
807 | return; | |
808 | } | |
7ba1ba12 | 809 | |
c611529e MP |
810 | static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag) |
811 | { | |
812 | return false; | |
813 | } | |
814 | ||
06c1e390 KB |
815 | static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp, |
816 | unsigned int nr) | |
817 | { | |
818 | return ERR_PTR(-EINVAL); | |
819 | } | |
820 | ||
821 | static inline int bio_integrity_add_page(struct bio *bio, struct page *page, | |
822 | unsigned int len, unsigned int offset) | |
823 | { | |
824 | return 0; | |
825 | } | |
826 | ||
7ba1ba12 MP |
827 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
828 | ||
02a5e0ac | 829 | #endif /* CONFIG_BLOCK */ |
1da177e4 | 830 | #endif /* __LINUX_BIO_H */ |