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