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