]>
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 | ||
1da177e4 | 8 | #include <linux/mempool.h> |
7cc01581 TH |
9 | /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */ |
10 | #include <linux/blk_types.h> | |
3e1a88ec | 11 | #include <linux/uio.h> |
7cc01581 | 12 | |
a8affc03 | 13 | #define BIO_MAX_VECS 256U |
e8007fad | 14 | #define BIO_MAX_INLINE_VECS UIO_MAXIOV |
5f7136db | 15 | |
fd8f8ede CH |
16 | struct queue_limits; |
17 | ||
5f7136db MWO |
18 | static inline unsigned int bio_max_segs(unsigned int nr_segs) |
19 | { | |
a8affc03 | 20 | return min(nr_segs, BIO_MAX_VECS); |
5f7136db | 21 | } |
1da177e4 | 22 | |
4550dd6c KO |
23 | #define bio_iter_iovec(bio, iter) \ |
24 | bvec_iter_bvec((bio)->bi_io_vec, (iter)) | |
25 | ||
26 | #define bio_iter_page(bio, iter) \ | |
27 | bvec_iter_page((bio)->bi_io_vec, (iter)) | |
28 | #define bio_iter_len(bio, iter) \ | |
29 | bvec_iter_len((bio)->bi_io_vec, (iter)) | |
30 | #define bio_iter_offset(bio, iter) \ | |
31 | bvec_iter_offset((bio)->bi_io_vec, (iter)) | |
32 | ||
33 | #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter) | |
34 | #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter) | |
35 | #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter) | |
7988613b | 36 | |
38a72dac KO |
37 | #define bvec_iter_sectors(iter) ((iter).bi_size >> 9) |
38 | #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter))) | |
39 | ||
40 | #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter) | |
41 | #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter) | |
bf2de6f5 | 42 | |
d3849953 CH |
43 | /* |
44 | * Return the data direction, READ or WRITE. | |
45 | */ | |
46 | #define bio_data_dir(bio) \ | |
47 | (op_is_write(bio_op(bio)) ? WRITE : READ) | |
48 | ||
458b76ed KO |
49 | /* |
50 | * Check whether this bio carries any data or not. A NULL bio is allowed. | |
51 | */ | |
52 | static inline bool bio_has_data(struct bio *bio) | |
53 | { | |
54 | if (bio && | |
55 | bio->bi_iter.bi_size && | |
7afafc8a | 56 | bio_op(bio) != REQ_OP_DISCARD && |
a6f0788e CK |
57 | bio_op(bio) != REQ_OP_SECURE_ERASE && |
58 | bio_op(bio) != REQ_OP_WRITE_ZEROES) | |
458b76ed KO |
59 | return true; |
60 | ||
61 | return false; | |
62 | } | |
63 | ||
c1527c0e | 64 | static inline bool bio_no_advance_iter(const struct bio *bio) |
95fe6c1a | 65 | { |
7afafc8a AH |
66 | return bio_op(bio) == REQ_OP_DISCARD || |
67 | bio_op(bio) == REQ_OP_SECURE_ERASE || | |
a6f0788e | 68 | bio_op(bio) == REQ_OP_WRITE_ZEROES; |
95fe6c1a MC |
69 | } |
70 | ||
bf2de6f5 JA |
71 | static inline void *bio_data(struct bio *bio) |
72 | { | |
458b76ed | 73 | if (bio_has_data(bio)) |
bf2de6f5 JA |
74 | return page_address(bio_page(bio)) + bio_offset(bio); |
75 | ||
76 | return NULL; | |
77 | } | |
1da177e4 | 78 | |
1200e07f ML |
79 | static inline bool bio_next_segment(const struct bio *bio, |
80 | struct bvec_iter_all *iter) | |
81 | { | |
82 | if (iter->idx >= bio->bi_vcnt) | |
83 | return false; | |
84 | ||
85 | bvec_advance(&bio->bi_io_vec[iter->idx], iter); | |
86 | return true; | |
87 | } | |
6dc4f100 | 88 | |
d74c6d51 KO |
89 | /* |
90 | * drivers should _never_ use the all version - the bio may have been split | |
91 | * before it got to the driver and the driver won't own all of it | |
92 | */ | |
2b070cfe CH |
93 | #define bio_for_each_segment_all(bvl, bio, iter) \ |
94 | for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); ) | |
d74c6d51 | 95 | |
c1527c0e BVA |
96 | static inline void bio_advance_iter(const struct bio *bio, |
97 | struct bvec_iter *iter, unsigned int bytes) | |
4550dd6c KO |
98 | { |
99 | iter->bi_sector += bytes >> 9; | |
100 | ||
7759eb23 | 101 | if (bio_no_advance_iter(bio)) |
4550dd6c | 102 | iter->bi_size -= bytes; |
7759eb23 | 103 | else |
4550dd6c | 104 | bvec_iter_advance(bio->bi_io_vec, iter, bytes); |
b1fb2c52 | 105 | /* TODO: It is reasonable to complete bio with error here. */ |
f9df1cd9 DM |
106 | } |
107 | ||
22b56c29 PB |
108 | /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */ |
109 | static inline void bio_advance_iter_single(const struct bio *bio, | |
110 | struct bvec_iter *iter, | |
111 | unsigned int bytes) | |
112 | { | |
113 | iter->bi_sector += bytes >> 9; | |
114 | ||
115 | if (bio_no_advance_iter(bio)) | |
116 | iter->bi_size -= bytes; | |
117 | else | |
118 | bvec_iter_advance_single(bio->bi_io_vec, iter, bytes); | |
119 | } | |
120 | ||
d4aa57a1 JA |
121 | void __bio_advance(struct bio *, unsigned bytes); |
122 | ||
123 | /** | |
124 | * bio_advance - increment/complete a bio by some number of bytes | |
125 | * @bio: bio to advance | |
6fd3c510 | 126 | * @nbytes: number of bytes to complete |
d4aa57a1 JA |
127 | * |
128 | * This updates bi_sector, bi_size and bi_idx; if the number of bytes to | |
129 | * complete doesn't align with a bvec boundary, then bv_len and bv_offset will | |
130 | * be updated on the last bvec as well. | |
131 | * | |
132 | * @bio will then represent the remaining, uncompleted portion of the io. | |
133 | */ | |
134 | static inline void bio_advance(struct bio *bio, unsigned int nbytes) | |
135 | { | |
136 | if (nbytes == bio->bi_iter.bi_size) { | |
137 | bio->bi_iter.bi_size = 0; | |
138 | return; | |
139 | } | |
140 | __bio_advance(bio, nbytes); | |
141 | } | |
142 | ||
7988613b KO |
143 | #define __bio_for_each_segment(bvl, bio, iter, start) \ |
144 | for (iter = (start); \ | |
4550dd6c KO |
145 | (iter).bi_size && \ |
146 | ((bvl = bio_iter_iovec((bio), (iter))), 1); \ | |
22b56c29 | 147 | bio_advance_iter_single((bio), &(iter), (bvl).bv_len)) |
7988613b KO |
148 | |
149 | #define bio_for_each_segment(bvl, bio, iter) \ | |
150 | __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter) | |
151 | ||
d18d9174 ML |
152 | #define __bio_for_each_bvec(bvl, bio, iter, start) \ |
153 | for (iter = (start); \ | |
154 | (iter).bi_size && \ | |
155 | ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \ | |
22b56c29 | 156 | bio_advance_iter_single((bio), &(iter), (bvl).bv_len)) |
d18d9174 ML |
157 | |
158 | /* iterate over multi-page bvec */ | |
159 | #define bio_for_each_bvec(bvl, bio, iter) \ | |
160 | __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter) | |
161 | ||
1072c12d OS |
162 | /* |
163 | * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the | |
164 | * same reasons as bio_for_each_segment_all(). | |
165 | */ | |
166 | #define bio_for_each_bvec_all(bvl, bio, i) \ | |
167 | for (i = 0, bvl = bio_first_bvec_all(bio); \ | |
640d1930 | 168 | i < (bio)->bi_vcnt; i++, bvl++) |
1072c12d | 169 | |
4550dd6c | 170 | #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) |
1da177e4 | 171 | |
f4595875 | 172 | static inline unsigned bio_segments(struct bio *bio) |
458b76ed KO |
173 | { |
174 | unsigned segs = 0; | |
175 | struct bio_vec bv; | |
176 | struct bvec_iter iter; | |
177 | ||
8423ae3d | 178 | /* |
a6f0788e CK |
179 | * We special case discard/write same/write zeroes, because they |
180 | * interpret bi_size differently: | |
8423ae3d KO |
181 | */ |
182 | ||
a6f0788e CK |
183 | switch (bio_op(bio)) { |
184 | case REQ_OP_DISCARD: | |
185 | case REQ_OP_SECURE_ERASE: | |
a6f0788e | 186 | case REQ_OP_WRITE_ZEROES: |
f9d03f96 | 187 | return 0; |
a6f0788e CK |
188 | default: |
189 | break; | |
190 | } | |
8423ae3d | 191 | |
f4595875 | 192 | bio_for_each_segment(bv, bio, iter) |
458b76ed KO |
193 | segs++; |
194 | ||
195 | return segs; | |
196 | } | |
197 | ||
1da177e4 LT |
198 | /* |
199 | * get a reference to a bio, so it won't disappear. the intended use is | |
200 | * something like: | |
201 | * | |
202 | * bio_get(bio); | |
203 | * submit_bio(rw, bio); | |
204 | * if (bio->bi_flags ...) | |
205 | * do_something | |
206 | * bio_put(bio); | |
207 | * | |
208 | * without the bio_get(), it could potentially complete I/O before submit_bio | |
209 | * returns. and then bio would be freed memory when if (bio->bi_flags ...) | |
210 | * runs | |
211 | */ | |
dac56212 JA |
212 | static inline void bio_get(struct bio *bio) |
213 | { | |
214 | bio->bi_flags |= (1 << BIO_REFFED); | |
215 | smp_mb__before_atomic(); | |
216 | atomic_inc(&bio->__bi_cnt); | |
217 | } | |
218 | ||
219 | static inline void bio_cnt_set(struct bio *bio, unsigned int count) | |
220 | { | |
221 | if (count != 1) { | |
222 | bio->bi_flags |= (1 << BIO_REFFED); | |
f381c6a4 | 223 | smp_mb(); |
dac56212 JA |
224 | } |
225 | atomic_set(&bio->__bi_cnt, count); | |
226 | } | |
1da177e4 | 227 | |
b7c44ed9 JA |
228 | static inline bool bio_flagged(struct bio *bio, unsigned int bit) |
229 | { | |
09e8c253 | 230 | return bio->bi_flags & (1U << bit); |
b7c44ed9 JA |
231 | } |
232 | ||
233 | static inline void bio_set_flag(struct bio *bio, unsigned int bit) | |
234 | { | |
2c68f6dc | 235 | bio->bi_flags |= (1U << bit); |
b7c44ed9 JA |
236 | } |
237 | ||
238 | static inline void bio_clear_flag(struct bio *bio, unsigned int bit) | |
239 | { | |
2c68f6dc | 240 | bio->bi_flags &= ~(1U << bit); |
b7c44ed9 JA |
241 | } |
242 | ||
86292abc ML |
243 | static inline struct bio_vec *bio_first_bvec_all(struct bio *bio) |
244 | { | |
245 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); | |
246 | return bio->bi_io_vec; | |
247 | } | |
248 | ||
249 | static inline struct page *bio_first_page_all(struct bio *bio) | |
250 | { | |
251 | return bio_first_bvec_all(bio)->bv_page; | |
252 | } | |
253 | ||
6d2790d9 Z |
254 | static inline struct folio *bio_first_folio_all(struct bio *bio) |
255 | { | |
256 | return page_folio(bio_first_page_all(bio)); | |
257 | } | |
258 | ||
86292abc ML |
259 | static inline struct bio_vec *bio_last_bvec_all(struct bio *bio) |
260 | { | |
261 | WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)); | |
262 | return &bio->bi_io_vec[bio->bi_vcnt - 1]; | |
263 | } | |
264 | ||
640d1930 MWO |
265 | /** |
266 | * struct folio_iter - State for iterating all folios in a bio. | |
267 | * @folio: The current folio we're iterating. NULL after the last folio. | |
268 | * @offset: The byte offset within the current folio. | |
269 | * @length: The number of bytes in this iteration (will not cross folio | |
270 | * boundary). | |
271 | */ | |
272 | struct folio_iter { | |
273 | struct folio *folio; | |
274 | size_t offset; | |
275 | size_t length; | |
276 | /* private: for use by the iterator */ | |
170f37d6 | 277 | struct folio *_next; |
640d1930 MWO |
278 | size_t _seg_count; |
279 | int _i; | |
280 | }; | |
281 | ||
282 | static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio, | |
283 | int i) | |
284 | { | |
285 | struct bio_vec *bvec = bio_first_bvec_all(bio) + i; | |
286 | ||
7bed6f3d MWO |
287 | if (unlikely(i >= bio->bi_vcnt)) { |
288 | fi->folio = NULL; | |
289 | return; | |
290 | } | |
291 | ||
640d1930 MWO |
292 | fi->folio = page_folio(bvec->bv_page); |
293 | fi->offset = bvec->bv_offset + | |
f826ec79 | 294 | PAGE_SIZE * folio_page_idx(fi->folio, bvec->bv_page); |
640d1930 MWO |
295 | fi->_seg_count = bvec->bv_len; |
296 | fi->length = min(folio_size(fi->folio) - fi->offset, fi->_seg_count); | |
170f37d6 | 297 | fi->_next = folio_next(fi->folio); |
640d1930 MWO |
298 | fi->_i = i; |
299 | } | |
300 | ||
301 | static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio) | |
302 | { | |
303 | fi->_seg_count -= fi->length; | |
304 | if (fi->_seg_count) { | |
170f37d6 | 305 | fi->folio = fi->_next; |
640d1930 MWO |
306 | fi->offset = 0; |
307 | fi->length = min(folio_size(fi->folio), fi->_seg_count); | |
170f37d6 | 308 | fi->_next = folio_next(fi->folio); |
640d1930 | 309 | } else { |
7bed6f3d | 310 | bio_first_folio(fi, bio, fi->_i + 1); |
640d1930 MWO |
311 | } |
312 | } | |
313 | ||
314 | /** | |
315 | * bio_for_each_folio_all - Iterate over each folio in a bio. | |
316 | * @fi: struct folio_iter which is updated for each folio. | |
317 | * @bio: struct bio to iterate over. | |
318 | */ | |
319 | #define bio_for_each_folio_all(fi, bio) \ | |
320 | for (bio_first_folio(&fi, bio, 0); fi.folio; bio_next_folio(&fi, bio)) | |
321 | ||
e83502ca | 322 | void bio_trim(struct bio *bio, sector_t offset, sector_t size); |
20d0189b KO |
323 | extern struct bio *bio_split(struct bio *bio, int sectors, |
324 | gfp_t gfp, struct bio_set *bs); | |
b35243a4 CH |
325 | int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim, |
326 | unsigned *segs, unsigned max_bytes); | |
20d0189b KO |
327 | |
328 | /** | |
329 | * bio_next_split - get next @sectors from a bio, splitting if necessary | |
330 | * @bio: bio to split | |
331 | * @sectors: number of sectors to split from the front of @bio | |
332 | * @gfp: gfp mask | |
333 | * @bs: bio set to allocate from | |
334 | * | |
6fd3c510 | 335 | * Return: a bio representing the next @sectors of @bio - if the bio is smaller |
20d0189b KO |
336 | * than @sectors, returns the original bio unchanged. |
337 | */ | |
338 | static inline struct bio *bio_next_split(struct bio *bio, int sectors, | |
339 | gfp_t gfp, struct bio_set *bs) | |
340 | { | |
341 | if (sectors >= bio_sectors(bio)) | |
342 | return bio; | |
343 | ||
344 | return bio_split(bio, sectors, gfp, bs); | |
345 | } | |
346 | ||
011067b0 N |
347 | enum { |
348 | BIOSET_NEED_BVECS = BIT(0), | |
47e0fb46 | 349 | BIOSET_NEED_RESCUER = BIT(1), |
be4d234d | 350 | BIOSET_PERCPU_CACHE = BIT(2), |
011067b0 | 351 | }; |
dad08527 KO |
352 | extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags); |
353 | extern void bioset_exit(struct bio_set *); | |
8aa6ba2f | 354 | extern int biovec_init_pool(mempool_t *pool, int pool_entries); |
1da177e4 | 355 | |
609be106 | 356 | struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs, |
16458cf3 | 357 | blk_opf_t opf, gfp_t gfp_mask, |
609be106 | 358 | struct bio_set *bs); |
066ff571 | 359 | struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask); |
1da177e4 LT |
360 | extern void bio_put(struct bio *); |
361 | ||
abfc426d CH |
362 | struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src, |
363 | gfp_t gfp, struct bio_set *bs); | |
364 | int bio_init_clone(struct block_device *bdev, struct bio *bio, | |
365 | struct bio *bio_src, gfp_t gfp); | |
bf800ef1 | 366 | |
f4f8154a | 367 | extern struct bio_set fs_bio_set; |
3f86a82a | 368 | |
07888c66 | 369 | static inline struct bio *bio_alloc(struct block_device *bdev, |
16458cf3 | 370 | unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp_mask) |
3f86a82a | 371 | { |
07888c66 | 372 | return bio_alloc_bioset(bdev, nr_vecs, opf, gfp_mask, &fs_bio_set); |
3f86a82a KO |
373 | } |
374 | ||
3e08773c | 375 | void submit_bio(struct bio *bio); |
1e3914d4 | 376 | |
4246a0b6 CH |
377 | extern void bio_endio(struct bio *); |
378 | ||
379 | static inline void bio_io_error(struct bio *bio) | |
380 | { | |
4e4cbee9 | 381 | bio->bi_status = BLK_STS_IOERR; |
4246a0b6 CH |
382 | bio_endio(bio); |
383 | } | |
384 | ||
03a07c92 GR |
385 | static inline void bio_wouldblock_error(struct bio *bio) |
386 | { | |
abb30460 | 387 | bio_set_flag(bio, BIO_QUIET); |
03a07c92 | 388 | bio->bi_status = BLK_STS_AGAIN; |
4246a0b6 CH |
389 | bio_endio(bio); |
390 | } | |
391 | ||
3e1a88ec PB |
392 | /* |
393 | * Calculate number of bvec segments that should be allocated to fit data | |
c42bca92 PB |
394 | * pointed by @iter. If @iter is backed by bvec it's going to be reused |
395 | * instead of allocating a new one. | |
3e1a88ec PB |
396 | */ |
397 | static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs) | |
398 | { | |
c42bca92 PB |
399 | if (iov_iter_is_bvec(iter)) |
400 | return 0; | |
3e1a88ec PB |
401 | return iov_iter_npages(iter, max_segs); |
402 | } | |
403 | ||
1da177e4 | 404 | struct request_queue; |
1da177e4 | 405 | |
49add496 | 406 | void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table, |
16458cf3 | 407 | unsigned short max_vecs, blk_opf_t opf); |
9ae3b3f5 | 408 | extern void bio_uninit(struct bio *); |
16458cf3 | 409 | void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf); |
196d38bc | 410 | void bio_chain(struct bio *, struct bio *); |
1da177e4 | 411 | |
83f2caaa JT |
412 | int __must_check bio_add_page(struct bio *bio, struct page *page, unsigned len, |
413 | unsigned off); | |
6c500000 JT |
414 | bool __must_check bio_add_folio(struct bio *bio, struct folio *folio, |
415 | size_t len, size_t off); | |
0aa69fd3 CH |
416 | void __bio_add_page(struct bio *bio, struct page *page, |
417 | unsigned int len, unsigned int off); | |
7a150f1e JT |
418 | void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len, |
419 | size_t off); | |
850e210d CH |
420 | void bio_add_virt_nofail(struct bio *bio, void *vaddr, unsigned len); |
421 | ||
75f88659 CH |
422 | /** |
423 | * bio_add_max_vecs - number of bio_vecs needed to add data to a bio | |
424 | * @kaddr: kernel virtual address to add | |
425 | * @len: length in bytes to add | |
426 | * | |
427 | * Calculate how many bio_vecs need to be allocated to add the kernel virtual | |
428 | * address range in [@kaddr:@len] in the worse case. | |
429 | */ | |
430 | static inline unsigned int bio_add_max_vecs(void *kaddr, unsigned int len) | |
431 | { | |
432 | if (is_vmalloc_addr(kaddr)) | |
433 | return DIV_ROUND_UP(offset_in_page(kaddr) + len, PAGE_SIZE); | |
434 | return 1; | |
435 | } | |
436 | ||
8dd16f5e CH |
437 | unsigned int bio_add_vmalloc_chunk(struct bio *bio, void *vaddr, unsigned len); |
438 | bool bio_add_vmalloc(struct bio *bio, void *vaddr, unsigned int len); | |
439 | ||
10b1e59c CH |
440 | int submit_bio_wait(struct bio *bio); |
441 | int bdev_rw_virt(struct block_device *bdev, sector_t sector, void *data, | |
442 | size_t len, enum req_op op); | |
443 | ||
2cefe4db | 444 | int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter); |
2f4873f9 | 445 | void bio_iov_bvec_set(struct bio *bio, const struct iov_iter *iter); |
c809084a | 446 | void __bio_release_pages(struct bio *bio, bool mark_dirty); |
1da177e4 LT |
447 | extern void bio_set_pages_dirty(struct bio *bio); |
448 | extern void bio_check_pages_dirty(struct bio *bio); | |
2d4dc890 | 449 | |
ee4b4e22 JA |
450 | extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter, |
451 | struct bio *src, struct bvec_iter *src_iter); | |
16ac3d63 | 452 | extern void bio_copy_data(struct bio *dst, struct bio *src); |
491221f8 | 453 | extern void bio_free_pages(struct bio *bio); |
29125ed6 | 454 | void guard_bio_eod(struct bio *bio); |
649f070e KO |
455 | void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter); |
456 | ||
457 | static inline void zero_fill_bio(struct bio *bio) | |
458 | { | |
459 | zero_fill_bio_iter(bio, bio->bi_iter); | |
460 | } | |
38a72dac | 461 | |
c809084a PB |
462 | static inline void bio_release_pages(struct bio *bio, bool mark_dirty) |
463 | { | |
e4cc6465 | 464 | if (bio_flagged(bio, BIO_PAGE_PINNED)) |
c809084a PB |
465 | __bio_release_pages(bio, mark_dirty); |
466 | } | |
467 | ||
74d46992 | 468 | #define bio_dev(bio) \ |
309dca30 | 469 | disk_devt((bio)->bi_bdev->bd_disk) |
74d46992 | 470 | |
852c788f | 471 | #ifdef CONFIG_BLK_CGROUP |
2268c0fe | 472 | void bio_associate_blkg(struct bio *bio); |
fd42df30 DZ |
473 | void bio_associate_blkg_from_css(struct bio *bio, |
474 | struct cgroup_subsys_state *css); | |
db6638d7 | 475 | void bio_clone_blkg_association(struct bio *dst, struct bio *src); |
3480373e | 476 | void blkcg_punt_bio_submit(struct bio *bio); |
852c788f | 477 | #else /* CONFIG_BLK_CGROUP */ |
2268c0fe | 478 | static inline void bio_associate_blkg(struct bio *bio) { } |
fd42df30 DZ |
479 | static inline void bio_associate_blkg_from_css(struct bio *bio, |
480 | struct cgroup_subsys_state *css) | |
481 | { } | |
db6638d7 DZ |
482 | static inline void bio_clone_blkg_association(struct bio *dst, |
483 | struct bio *src) { } | |
3480373e CH |
484 | static inline void blkcg_punt_bio_submit(struct bio *bio) |
485 | { | |
486 | submit_bio(bio); | |
487 | } | |
852c788f TH |
488 | #endif /* CONFIG_BLK_CGROUP */ |
489 | ||
cf6d6238 PB |
490 | static inline void bio_set_dev(struct bio *bio, struct block_device *bdev) |
491 | { | |
492 | bio_clear_flag(bio, BIO_REMAPPED); | |
493 | if (bio->bi_bdev != bdev) | |
320fb0f9 | 494 | bio_clear_flag(bio, BIO_BPS_THROTTLED); |
cf6d6238 PB |
495 | bio->bi_bdev = bdev; |
496 | bio_associate_blkg(bio); | |
497 | } | |
498 | ||
8f3d8ba2 | 499 | /* |
e686307f | 500 | * BIO list management for use by remapping drivers (e.g. DM or MD) and loop. |
8f3d8ba2 CH |
501 | * |
502 | * A bio_list anchors a singly-linked list of bios chained through the bi_next | |
503 | * member of the bio. The bio_list also caches the last list member to allow | |
504 | * fast access to the tail. | |
505 | */ | |
506 | struct bio_list { | |
507 | struct bio *head; | |
508 | struct bio *tail; | |
509 | }; | |
510 | ||
511 | static inline int bio_list_empty(const struct bio_list *bl) | |
512 | { | |
513 | return bl->head == NULL; | |
514 | } | |
515 | ||
516 | static inline void bio_list_init(struct bio_list *bl) | |
517 | { | |
518 | bl->head = bl->tail = NULL; | |
519 | } | |
520 | ||
320ae51f JA |
521 | #define BIO_EMPTY_LIST { NULL, NULL } |
522 | ||
8f3d8ba2 CH |
523 | #define bio_list_for_each(bio, bl) \ |
524 | for (bio = (bl)->head; bio; bio = bio->bi_next) | |
525 | ||
526 | static inline unsigned bio_list_size(const struct bio_list *bl) | |
527 | { | |
528 | unsigned sz = 0; | |
529 | struct bio *bio; | |
530 | ||
531 | bio_list_for_each(bio, bl) | |
532 | sz++; | |
533 | ||
534 | return sz; | |
535 | } | |
536 | ||
537 | static inline void bio_list_add(struct bio_list *bl, struct bio *bio) | |
538 | { | |
539 | bio->bi_next = NULL; | |
540 | ||
541 | if (bl->tail) | |
542 | bl->tail->bi_next = bio; | |
543 | else | |
544 | bl->head = bio; | |
545 | ||
546 | bl->tail = bio; | |
547 | } | |
548 | ||
549 | static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio) | |
550 | { | |
551 | bio->bi_next = bl->head; | |
552 | ||
553 | bl->head = bio; | |
554 | ||
555 | if (!bl->tail) | |
556 | bl->tail = bio; | |
557 | } | |
558 | ||
559 | static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) | |
560 | { | |
561 | if (!bl2->head) | |
562 | return; | |
563 | ||
564 | if (bl->tail) | |
565 | bl->tail->bi_next = bl2->head; | |
566 | else | |
567 | bl->head = bl2->head; | |
568 | ||
569 | bl->tail = bl2->tail; | |
570 | } | |
571 | ||
c9418adf CH |
572 | static inline void bio_list_merge_init(struct bio_list *bl, |
573 | struct bio_list *bl2) | |
574 | { | |
575 | bio_list_merge(bl, bl2); | |
576 | bio_list_init(bl2); | |
577 | } | |
578 | ||
8f3d8ba2 CH |
579 | static inline void bio_list_merge_head(struct bio_list *bl, |
580 | struct bio_list *bl2) | |
581 | { | |
582 | if (!bl2->head) | |
583 | return; | |
584 | ||
585 | if (bl->head) | |
586 | bl2->tail->bi_next = bl->head; | |
587 | else | |
588 | bl->tail = bl2->tail; | |
589 | ||
590 | bl->head = bl2->head; | |
591 | } | |
592 | ||
13685a16 GU |
593 | static inline struct bio *bio_list_peek(struct bio_list *bl) |
594 | { | |
595 | return bl->head; | |
596 | } | |
597 | ||
8f3d8ba2 CH |
598 | static inline struct bio *bio_list_pop(struct bio_list *bl) |
599 | { | |
600 | struct bio *bio = bl->head; | |
601 | ||
602 | if (bio) { | |
603 | bl->head = bl->head->bi_next; | |
604 | if (!bl->head) | |
605 | bl->tail = NULL; | |
606 | ||
607 | bio->bi_next = NULL; | |
608 | } | |
609 | ||
610 | return bio; | |
611 | } | |
612 | ||
613 | static inline struct bio *bio_list_get(struct bio_list *bl) | |
614 | { | |
615 | struct bio *bio = bl->head; | |
616 | ||
617 | bl->head = bl->tail = NULL; | |
618 | ||
619 | return bio; | |
620 | } | |
621 | ||
0ef5a50c MS |
622 | /* |
623 | * Increment chain count for the bio. Make sure the CHAIN flag update | |
624 | * is visible before the raised count. | |
625 | */ | |
626 | static inline void bio_inc_remaining(struct bio *bio) | |
627 | { | |
628 | bio_set_flag(bio, BIO_CHAIN); | |
629 | smp_mb__before_atomic(); | |
630 | atomic_inc(&bio->__bi_remaining); | |
631 | } | |
632 | ||
57fb233f KO |
633 | /* |
634 | * bio_set is used to allow other portions of the IO system to | |
635 | * allocate their own private memory pools for bio and iovec structures. | |
636 | * These memory pools in turn all allocate from the bio_slab | |
637 | * and the bvec_slabs[]. | |
638 | */ | |
639 | #define BIO_POOL_SIZE 2 | |
57fb233f KO |
640 | |
641 | struct bio_set { | |
642 | struct kmem_cache *bio_slab; | |
643 | unsigned int front_pad; | |
644 | ||
be4d234d JA |
645 | /* |
646 | * per-cpu bio alloc cache | |
647 | */ | |
648 | struct bio_alloc_cache __percpu *cache; | |
649 | ||
8aa6ba2f KO |
650 | mempool_t bio_pool; |
651 | mempool_t bvec_pool; | |
df2cb6da | 652 | |
9f180e31 | 653 | unsigned int back_pad; |
df2cb6da KO |
654 | /* |
655 | * Deadlock avoidance for stacking block drivers: see comments in | |
656 | * bio_alloc_bioset() for details | |
657 | */ | |
658 | spinlock_t rescue_lock; | |
659 | struct bio_list rescue_list; | |
660 | struct work_struct rescue_work; | |
661 | struct workqueue_struct *rescue_workqueue; | |
be4d234d JA |
662 | |
663 | /* | |
664 | * Hot un-plug notifier for the per-cpu cache, if used | |
665 | */ | |
666 | struct hlist_node cpuhp_dead; | |
57fb233f KO |
667 | }; |
668 | ||
338aa96d KO |
669 | static inline bool bioset_initialized(struct bio_set *bs) |
670 | { | |
671 | return bs->bio_slab != NULL; | |
672 | } | |
673 | ||
0bbb280d JA |
674 | /* |
675 | * Mark a bio as polled. Note that for async polled IO, the caller must | |
676 | * expect -EWOULDBLOCK if we cannot allocate a request (or other resources). | |
677 | * We cannot block waiting for requests on polled IO, as those completions | |
678 | * must be found by the caller. This is different than IRQ driven IO, where | |
679 | * it's safe to wait for IO to complete. | |
680 | */ | |
681 | static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb) | |
682 | { | |
6ce913fe | 683 | bio->bi_opf |= REQ_POLLED; |
2bc05769 | 684 | if (kiocb->ki_flags & IOCB_NOWAIT) |
0bbb280d JA |
685 | bio->bi_opf |= REQ_NOWAIT; |
686 | } | |
687 | ||
b53f3dcd MS |
688 | static inline void bio_clear_polled(struct bio *bio) |
689 | { | |
53eab8e7 | 690 | bio->bi_opf &= ~REQ_POLLED; |
b53f3dcd MS |
691 | } |
692 | ||
0ef2b9e6 CH |
693 | /** |
694 | * bio_is_zone_append - is this a zone append bio? | |
695 | * @bio: bio to check | |
696 | * | |
697 | * Check if @bio is a zone append operation. Core block layer code and end_io | |
698 | * handlers must use this instead of an open coded REQ_OP_ZONE_APPEND check | |
699 | * because the block layer can rewrite REQ_OP_ZONE_APPEND to REQ_OP_WRITE if | |
700 | * it is not natively supported. | |
701 | */ | |
702 | static inline bool bio_is_zone_append(struct bio *bio) | |
703 | { | |
704 | if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) | |
705 | return false; | |
706 | return bio_op(bio) == REQ_OP_ZONE_APPEND || | |
707 | bio_flagged(bio, BIO_EMULATES_ZONE_APPEND); | |
708 | } | |
709 | ||
0a3140ea | 710 | struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev, |
16458cf3 | 711 | unsigned int nr_pages, blk_opf_t opf, gfp_t gfp); |
81c2168c | 712 | struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new); |
c28a6147 | 713 | |
e8b4869b CH |
714 | struct bio *blk_alloc_discard_bio(struct block_device *bdev, |
715 | sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask); | |
716 | ||
1da177e4 | 717 | #endif /* __LINUX_BIO_H */ |