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