1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
4 */
5 #ifndef __LINUX_BIO_H
6 #define __LINUX_BIO_H
7
8 #include <linux/mempool.h>
9 #include <linux/ioprio.h>
10 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
11 #include <linux/blk_types.h>
12 #include <linux/uio.h>
13 #include <linux/android_kabi.h>
14
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
23 #define BIO_MAX_VECS 256U
24
bio_max_segs(unsigned int nr_segs)25 static inline unsigned int bio_max_segs(unsigned int nr_segs)
26 {
27 return min(nr_segs, BIO_MAX_VECS);
28 }
29
30 #define bio_prio(bio) (bio)->bi_ioprio
31 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
32
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)
46
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)
52
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
59 /*
60 * Check whether this bio carries any data or not. A NULL bio is allowed.
61 */
bio_has_data(struct bio * bio)62 static inline bool bio_has_data(struct bio *bio)
63 {
64 if (bio &&
65 bio->bi_iter.bi_size &&
66 bio_op(bio) != REQ_OP_DISCARD &&
67 bio_op(bio) != REQ_OP_SECURE_ERASE &&
68 bio_op(bio) != REQ_OP_WRITE_ZEROES)
69 return true;
70
71 return false;
72 }
73
bio_no_advance_iter(const struct bio * bio)74 static inline bool bio_no_advance_iter(const struct bio *bio)
75 {
76 return bio_op(bio) == REQ_OP_DISCARD ||
77 bio_op(bio) == REQ_OP_SECURE_ERASE ||
78 bio_op(bio) == REQ_OP_WRITE_SAME ||
79 bio_op(bio) == REQ_OP_WRITE_ZEROES;
80 }
81
bio_mergeable(struct bio * bio)82 static inline bool bio_mergeable(struct bio *bio)
83 {
84 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
85 return false;
86
87 return true;
88 }
89
bio_cur_bytes(struct bio * bio)90 static inline unsigned int bio_cur_bytes(struct bio *bio)
91 {
92 if (bio_has_data(bio))
93 return bio_iovec(bio).bv_len;
94 else /* dataless requests such as discard */
95 return bio->bi_iter.bi_size;
96 }
97
bio_data(struct bio * bio)98 static inline void *bio_data(struct bio *bio)
99 {
100 if (bio_has_data(bio))
101 return page_address(bio_page(bio)) + bio_offset(bio);
102
103 return NULL;
104 }
105
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 */
bio_full(struct bio * bio,unsigned len)114 static inline bool bio_full(struct bio *bio, unsigned len)
115 {
116 if (bio->bi_vcnt >= bio->bi_max_vecs)
117 return true;
118
119 if (bio->bi_iter.bi_size > UINT_MAX - len)
120 return true;
121
122 return false;
123 }
124
bio_next_segment(const struct bio * bio,struct bvec_iter_all * iter)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 }
134
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 */
139 #define bio_for_each_segment_all(bvl, bio, iter) \
140 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
141
bio_advance_iter(const struct bio * bio,struct bvec_iter * iter,unsigned int bytes)142 static inline void bio_advance_iter(const struct bio *bio,
143 struct bvec_iter *iter, unsigned int bytes)
144 {
145 iter->bi_sector += bytes >> 9;
146
147 if (bio_no_advance_iter(bio))
148 iter->bi_size -= bytes;
149 else
150 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
151 /* TODO: It is reasonable to complete bio with error here. */
152 }
153
154 /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
bio_advance_iter_single(const struct bio * bio,struct bvec_iter * iter,unsigned int bytes)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
167 #define __bio_for_each_segment(bvl, bio, iter, start) \
168 for (iter = (start); \
169 (iter).bi_size && \
170 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
171 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
172
173 #define bio_for_each_segment(bvl, bio, iter) \
174 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
175
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); \
180 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
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
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
194 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
195
bio_segments(struct bio * bio)196 static inline unsigned bio_segments(struct bio *bio)
197 {
198 unsigned segs = 0;
199 struct bio_vec bv;
200 struct bvec_iter iter;
201
202 /*
203 * We special case discard/write same/write zeroes, because they
204 * interpret bi_size differently:
205 */
206
207 switch (bio_op(bio)) {
208 case REQ_OP_DISCARD:
209 case REQ_OP_SECURE_ERASE:
210 case REQ_OP_WRITE_ZEROES:
211 return 0;
212 case REQ_OP_WRITE_SAME:
213 return 1;
214 default:
215 break;
216 }
217
218 bio_for_each_segment(bv, bio, iter)
219 segs++;
220
221 return segs;
222 }
223
224 /*
225 * get a reference to a bio, so it won't disappear. the intended use is
226 * something like:
227 *
228 * bio_get(bio);
229 * submit_bio(rw, bio);
230 * if (bio->bi_flags ...)
231 * do_something
232 * bio_put(bio);
233 *
234 * without the bio_get(), it could potentially complete I/O before submit_bio
235 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
236 * runs
237 */
bio_get(struct bio * bio)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
bio_cnt_set(struct bio * bio,unsigned int count)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);
249 smp_mb();
250 }
251 atomic_set(&bio->__bi_cnt, count);
252 }
253
bio_flagged(struct bio * bio,unsigned int bit)254 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
255 {
256 return (bio->bi_flags & (1U << bit)) != 0;
257 }
258
bio_set_flag(struct bio * bio,unsigned int bit)259 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
260 {
261 bio->bi_flags |= (1U << bit);
262 }
263
bio_clear_flag(struct bio * bio,unsigned int bit)264 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
265 {
266 bio->bi_flags &= ~(1U << bit);
267 }
268
bio_get_first_bvec(struct bio * bio,struct bio_vec * bv)269 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
270 {
271 *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
272 }
273
bio_get_last_bvec(struct bio * bio,struct bio_vec * bv)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
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 */
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
bio_first_bvec_all(struct bio * bio)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
bio_first_page_all(struct bio * bio)306 static inline struct page *bio_first_page_all(struct bio *bio)
307 {
308 return bio_first_bvec_all(bio)->bv_page;
309 }
310
bio_last_bvec_all(struct bio * bio)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
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
325 /*
326 * bio integrity payload
327 */
328 struct bio_integrity_payload {
329 struct bio *bip_bio; /* parent bio */
330
331 struct bvec_iter bip_iter;
332
333 unsigned short bip_vcnt; /* # of integrity bio_vecs */
334 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
335 unsigned short bip_flags; /* control flags */
336
337 struct bvec_iter bio_iter; /* for rewinding parent bio */
338
339 struct work_struct bip_work; /* I/O completion */
340
341 struct bio_vec *bip_vec;
342
343 ANDROID_KABI_RESERVE(1);
344 ANDROID_KABI_RESERVE(2);
345
346 struct bio_vec bip_inline_vecs[];/* embedded bvec array */
347 };
348
349 #if defined(CONFIG_BLK_DEV_INTEGRITY)
350
bio_integrity(struct bio * bio)351 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
352 {
353 if (bio->bi_opf & REQ_INTEGRITY)
354 return bio->bi_integrity;
355
356 return NULL;
357 }
358
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)359 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
360 {
361 struct bio_integrity_payload *bip = bio_integrity(bio);
362
363 if (bip)
364 return bip->bip_flags & flag;
365
366 return false;
367 }
368
bip_get_seed(struct bio_integrity_payload * bip)369 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
370 {
371 return bip->bip_iter.bi_sector;
372 }
373
bip_set_seed(struct bio_integrity_payload * bip,sector_t seed)374 static inline void bip_set_seed(struct bio_integrity_payload *bip,
375 sector_t seed)
376 {
377 bip->bip_iter.bi_sector = seed;
378 }
379
380 #endif /* CONFIG_BLK_DEV_INTEGRITY */
381
382 void bio_trim(struct bio *bio, sector_t offset, sector_t size);
383 extern struct bio *bio_split(struct bio *bio, int sectors,
384 gfp_t gfp, struct bio_set *bs);
385
386 /**
387 * bio_next_split - get next @sectors from a bio, splitting if necessary
388 * @bio: bio to split
389 * @sectors: number of sectors to split from the front of @bio
390 * @gfp: gfp mask
391 * @bs: bio set to allocate from
392 *
393 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
394 * than @sectors, returns the original bio unchanged.
395 */
bio_next_split(struct bio * bio,int sectors,gfp_t gfp,struct bio_set * bs)396 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
397 gfp_t gfp, struct bio_set *bs)
398 {
399 if (sectors >= bio_sectors(bio))
400 return bio;
401
402 return bio_split(bio, sectors, gfp, bs);
403 }
404
405 enum {
406 BIOSET_NEED_BVECS = BIT(0),
407 BIOSET_NEED_RESCUER = BIT(1),
408 BIOSET_PERCPU_CACHE = BIT(2),
409 };
410 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
411 extern void bioset_exit(struct bio_set *);
412 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
413 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
414
415 struct bio *bio_alloc_bioset(gfp_t gfp, unsigned short nr_iovecs,
416 struct bio_set *bs);
417 struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
418 struct bio_set *bs);
419 struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
420 extern void bio_put(struct bio *);
421
422 extern void __bio_clone_fast(struct bio *, struct bio *);
423 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
424
425 extern struct bio_set fs_bio_set;
426
bio_alloc(gfp_t gfp_mask,unsigned short nr_iovecs)427 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned short nr_iovecs)
428 {
429 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
430 }
431
432 extern blk_qc_t submit_bio(struct bio *);
433
434 extern void bio_endio(struct bio *);
435
bio_io_error(struct bio * bio)436 static inline void bio_io_error(struct bio *bio)
437 {
438 bio->bi_status = BLK_STS_IOERR;
439 bio_endio(bio);
440 }
441
bio_wouldblock_error(struct bio * bio)442 static inline void bio_wouldblock_error(struct bio *bio)
443 {
444 bio_set_flag(bio, BIO_QUIET);
445 bio->bi_status = BLK_STS_AGAIN;
446 bio_endio(bio);
447 }
448
449 /*
450 * Calculate number of bvec segments that should be allocated to fit data
451 * pointed by @iter. If @iter is backed by bvec it's going to be reused
452 * instead of allocating a new one.
453 */
bio_iov_vecs_to_alloc(struct iov_iter * iter,int max_segs)454 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
455 {
456 if (iov_iter_is_bvec(iter))
457 return 0;
458 return iov_iter_npages(iter, max_segs);
459 }
460
461 struct request_queue;
462
463 extern int submit_bio_wait(struct bio *bio);
464 extern void bio_advance(struct bio *, unsigned);
465
466 extern void bio_init(struct bio *bio, struct bio_vec *table,
467 unsigned short max_vecs);
468 extern void bio_uninit(struct bio *);
469 extern void bio_reset(struct bio *);
470 void bio_chain(struct bio *, struct bio *);
471
472 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
473 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
474 unsigned int, unsigned int);
475 int bio_add_zone_append_page(struct bio *bio, struct page *page,
476 unsigned int len, unsigned int offset);
477 bool __bio_try_merge_page(struct bio *bio, struct page *page,
478 unsigned int len, unsigned int off, bool *same_page);
479 void __bio_add_page(struct bio *bio, struct page *page,
480 unsigned int len, unsigned int off);
481 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
482 void bio_release_pages(struct bio *bio, bool mark_dirty);
483 extern void bio_set_pages_dirty(struct bio *bio);
484 extern void bio_check_pages_dirty(struct bio *bio);
485
486 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
487 struct bio *src, struct bvec_iter *src_iter);
488 extern void bio_copy_data(struct bio *dst, struct bio *src);
489 extern void bio_free_pages(struct bio *bio);
490 void bio_truncate(struct bio *bio, unsigned new_size);
491 void guard_bio_eod(struct bio *bio);
492 void zero_fill_bio(struct bio *bio);
493
494 extern const char *bio_devname(struct bio *bio, char *buffer);
495
496 #define bio_set_dev(bio, bdev) \
497 do { \
498 bio_clear_flag(bio, BIO_REMAPPED); \
499 if ((bio)->bi_bdev != (bdev)) \
500 bio_clear_flag(bio, BIO_THROTTLED); \
501 (bio)->bi_bdev = (bdev); \
502 bio_associate_blkg(bio); \
503 } while (0)
504
505 #define bio_copy_dev(dst, src) \
506 do { \
507 bio_clear_flag(dst, BIO_REMAPPED); \
508 (dst)->bi_bdev = (src)->bi_bdev; \
509 bio_clone_blkg_association(dst, src); \
510 } while (0)
511
512 #define bio_dev(bio) \
513 disk_devt((bio)->bi_bdev->bd_disk)
514
515 #ifdef CONFIG_BLK_CGROUP
516 void bio_associate_blkg(struct bio *bio);
517 void bio_associate_blkg_from_css(struct bio *bio,
518 struct cgroup_subsys_state *css);
519 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
520 #else /* CONFIG_BLK_CGROUP */
bio_associate_blkg(struct bio * bio)521 static inline void bio_associate_blkg(struct bio *bio) { }
bio_associate_blkg_from_css(struct bio * bio,struct cgroup_subsys_state * css)522 static inline void bio_associate_blkg_from_css(struct bio *bio,
523 struct cgroup_subsys_state *css)
524 { }
bio_clone_blkg_association(struct bio * dst,struct bio * src)525 static inline void bio_clone_blkg_association(struct bio *dst,
526 struct bio *src) { }
527 #endif /* CONFIG_BLK_CGROUP */
528
529 /*
530 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
531 *
532 * A bio_list anchors a singly-linked list of bios chained through the bi_next
533 * member of the bio. The bio_list also caches the last list member to allow
534 * fast access to the tail.
535 */
536 struct bio_list {
537 struct bio *head;
538 struct bio *tail;
539 };
540
bio_list_empty(const struct bio_list * bl)541 static inline int bio_list_empty(const struct bio_list *bl)
542 {
543 return bl->head == NULL;
544 }
545
bio_list_init(struct bio_list * bl)546 static inline void bio_list_init(struct bio_list *bl)
547 {
548 bl->head = bl->tail = NULL;
549 }
550
551 #define BIO_EMPTY_LIST { NULL, NULL }
552
553 #define bio_list_for_each(bio, bl) \
554 for (bio = (bl)->head; bio; bio = bio->bi_next)
555
bio_list_size(const struct bio_list * bl)556 static inline unsigned bio_list_size(const struct bio_list *bl)
557 {
558 unsigned sz = 0;
559 struct bio *bio;
560
561 bio_list_for_each(bio, bl)
562 sz++;
563
564 return sz;
565 }
566
bio_list_add(struct bio_list * bl,struct bio * bio)567 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
568 {
569 bio->bi_next = NULL;
570
571 if (bl->tail)
572 bl->tail->bi_next = bio;
573 else
574 bl->head = bio;
575
576 bl->tail = bio;
577 }
578
bio_list_add_head(struct bio_list * bl,struct bio * bio)579 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
580 {
581 bio->bi_next = bl->head;
582
583 bl->head = bio;
584
585 if (!bl->tail)
586 bl->tail = bio;
587 }
588
bio_list_merge(struct bio_list * bl,struct bio_list * bl2)589 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
590 {
591 if (!bl2->head)
592 return;
593
594 if (bl->tail)
595 bl->tail->bi_next = bl2->head;
596 else
597 bl->head = bl2->head;
598
599 bl->tail = bl2->tail;
600 }
601
bio_list_merge_head(struct bio_list * bl,struct bio_list * bl2)602 static inline void bio_list_merge_head(struct bio_list *bl,
603 struct bio_list *bl2)
604 {
605 if (!bl2->head)
606 return;
607
608 if (bl->head)
609 bl2->tail->bi_next = bl->head;
610 else
611 bl->tail = bl2->tail;
612
613 bl->head = bl2->head;
614 }
615
bio_list_peek(struct bio_list * bl)616 static inline struct bio *bio_list_peek(struct bio_list *bl)
617 {
618 return bl->head;
619 }
620
bio_list_pop(struct bio_list * bl)621 static inline struct bio *bio_list_pop(struct bio_list *bl)
622 {
623 struct bio *bio = bl->head;
624
625 if (bio) {
626 bl->head = bl->head->bi_next;
627 if (!bl->head)
628 bl->tail = NULL;
629
630 bio->bi_next = NULL;
631 }
632
633 return bio;
634 }
635
bio_list_get(struct bio_list * bl)636 static inline struct bio *bio_list_get(struct bio_list *bl)
637 {
638 struct bio *bio = bl->head;
639
640 bl->head = bl->tail = NULL;
641
642 return bio;
643 }
644
645 /*
646 * Increment chain count for the bio. Make sure the CHAIN flag update
647 * is visible before the raised count.
648 */
bio_inc_remaining(struct bio * bio)649 static inline void bio_inc_remaining(struct bio *bio)
650 {
651 bio_set_flag(bio, BIO_CHAIN);
652 smp_mb__before_atomic();
653 atomic_inc(&bio->__bi_remaining);
654 }
655
656 /*
657 * bio_set is used to allow other portions of the IO system to
658 * allocate their own private memory pools for bio and iovec structures.
659 * These memory pools in turn all allocate from the bio_slab
660 * and the bvec_slabs[].
661 */
662 #define BIO_POOL_SIZE 2
663
664 struct bio_set {
665 struct kmem_cache *bio_slab;
666 unsigned int front_pad;
667
668 /*
669 * per-cpu bio alloc cache
670 */
671 struct bio_alloc_cache __percpu *cache;
672
673 mempool_t bio_pool;
674 mempool_t bvec_pool;
675 #if defined(CONFIG_BLK_DEV_INTEGRITY)
676 mempool_t bio_integrity_pool;
677 mempool_t bvec_integrity_pool;
678 #endif
679
680 unsigned int back_pad;
681 /*
682 * Deadlock avoidance for stacking block drivers: see comments in
683 * bio_alloc_bioset() for details
684 */
685 spinlock_t rescue_lock;
686 struct bio_list rescue_list;
687 struct work_struct rescue_work;
688 struct workqueue_struct *rescue_workqueue;
689
690 /*
691 * Hot un-plug notifier for the per-cpu cache, if used
692 */
693 struct hlist_node cpuhp_dead;
694
695 ANDROID_KABI_RESERVE(1);
696 ANDROID_KABI_RESERVE(2);
697 ANDROID_KABI_RESERVE(3);
698 ANDROID_KABI_RESERVE(4);
699 };
700
bioset_initialized(struct bio_set * bs)701 static inline bool bioset_initialized(struct bio_set *bs)
702 {
703 return bs->bio_slab != NULL;
704 }
705
706 #if defined(CONFIG_BLK_DEV_INTEGRITY)
707
708 #define bip_for_each_vec(bvl, bip, iter) \
709 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
710
711 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
712 for_each_bio(_bio) \
713 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
714
715 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
716 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
717 extern bool bio_integrity_prep(struct bio *);
718 extern void bio_integrity_advance(struct bio *, unsigned int);
719 extern void bio_integrity_trim(struct bio *);
720 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
721 extern int bioset_integrity_create(struct bio_set *, int);
722 extern void bioset_integrity_free(struct bio_set *);
723 extern void bio_integrity_init(void);
724
725 #else /* CONFIG_BLK_DEV_INTEGRITY */
726
bio_integrity(struct bio * bio)727 static inline void *bio_integrity(struct bio *bio)
728 {
729 return NULL;
730 }
731
bioset_integrity_create(struct bio_set * bs,int pool_size)732 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
733 {
734 return 0;
735 }
736
bioset_integrity_free(struct bio_set * bs)737 static inline void bioset_integrity_free (struct bio_set *bs)
738 {
739 return;
740 }
741
bio_integrity_prep(struct bio * bio)742 static inline bool bio_integrity_prep(struct bio *bio)
743 {
744 return true;
745 }
746
bio_integrity_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp_mask)747 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
748 gfp_t gfp_mask)
749 {
750 return 0;
751 }
752
bio_integrity_advance(struct bio * bio,unsigned int bytes_done)753 static inline void bio_integrity_advance(struct bio *bio,
754 unsigned int bytes_done)
755 {
756 return;
757 }
758
bio_integrity_trim(struct bio * bio)759 static inline void bio_integrity_trim(struct bio *bio)
760 {
761 return;
762 }
763
bio_integrity_init(void)764 static inline void bio_integrity_init(void)
765 {
766 return;
767 }
768
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)769 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
770 {
771 return false;
772 }
773
bio_integrity_alloc(struct bio * bio,gfp_t gfp,unsigned int nr)774 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
775 unsigned int nr)
776 {
777 return ERR_PTR(-EINVAL);
778 }
779
bio_integrity_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)780 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
781 unsigned int len, unsigned int offset)
782 {
783 return 0;
784 }
785
786 #endif /* CONFIG_BLK_DEV_INTEGRITY */
787
788 /*
789 * Mark a bio as polled. Note that for async polled IO, the caller must
790 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
791 * We cannot block waiting for requests on polled IO, as those completions
792 * must be found by the caller. This is different than IRQ driven IO, where
793 * it's safe to wait for IO to complete.
794 */
bio_set_polled(struct bio * bio,struct kiocb * kiocb)795 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
796 {
797 bio->bi_opf |= REQ_HIPRI;
798 if (!is_sync_kiocb(kiocb))
799 bio->bi_opf |= REQ_NOWAIT;
800 }
801
802 struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
803
804 #endif /* __LINUX_BIO_H */
805