xref: /openbmc/linux/include/linux/bio.h (revision 7ce05074)
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/uio.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 
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  */
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 
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 
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 
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 
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  */
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 
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 
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 */
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 
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  */
238 static inline void bio_get(struct bio *bio)
239 {
240 	bio->bi_flags |= (1 << BIO_REFFED);
241 	smp_mb__before_atomic();
242 	atomic_inc(&bio->__bi_cnt);
243 }
244 
245 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
246 {
247 	if (count != 1) {
248 		bio->bi_flags |= (1 << BIO_REFFED);
249 		smp_mb();
250 	}
251 	atomic_set(&bio->__bi_cnt, count);
252 }
253 
254 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
255 {
256 	return (bio->bi_flags & (1U << bit)) != 0;
257 }
258 
259 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
260 {
261 	bio->bi_flags |= (1U << bit);
262 }
263 
264 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
265 {
266 	bio->bi_flags &= ~(1U << bit);
267 }
268 
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 
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 
300 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
301 {
302 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
303 	return bio->bi_io_vec;
304 }
305 
306 static inline struct page *bio_first_page_all(struct bio *bio)
307 {
308 	return bio_first_bvec_all(bio)->bv_page;
309 }
310 
311 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
312 {
313 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
314 	return &bio->bi_io_vec[bio->bi_vcnt - 1];
315 }
316 
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 	struct bio_vec		bip_inline_vecs[];/* embedded bvec array */
343 };
344 
345 #if defined(CONFIG_BLK_DEV_INTEGRITY)
346 
347 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
348 {
349 	if (bio->bi_opf & REQ_INTEGRITY)
350 		return bio->bi_integrity;
351 
352 	return NULL;
353 }
354 
355 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
356 {
357 	struct bio_integrity_payload *bip = bio_integrity(bio);
358 
359 	if (bip)
360 		return bip->bip_flags & flag;
361 
362 	return false;
363 }
364 
365 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
366 {
367 	return bip->bip_iter.bi_sector;
368 }
369 
370 static inline void bip_set_seed(struct bio_integrity_payload *bip,
371 				sector_t seed)
372 {
373 	bip->bip_iter.bi_sector = seed;
374 }
375 
376 #endif /* CONFIG_BLK_DEV_INTEGRITY */
377 
378 extern void bio_trim(struct bio *bio, int offset, int size);
379 extern struct bio *bio_split(struct bio *bio, int sectors,
380 			     gfp_t gfp, struct bio_set *bs);
381 
382 /**
383  * bio_next_split - get next @sectors from a bio, splitting if necessary
384  * @bio:	bio to split
385  * @sectors:	number of sectors to split from the front of @bio
386  * @gfp:	gfp mask
387  * @bs:		bio set to allocate from
388  *
389  * Returns a bio representing the next @sectors of @bio - if the bio is smaller
390  * than @sectors, returns the original bio unchanged.
391  */
392 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
393 					 gfp_t gfp, struct bio_set *bs)
394 {
395 	if (sectors >= bio_sectors(bio))
396 		return bio;
397 
398 	return bio_split(bio, sectors, gfp, bs);
399 }
400 
401 enum {
402 	BIOSET_NEED_BVECS = BIT(0),
403 	BIOSET_NEED_RESCUER = BIT(1),
404 };
405 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
406 extern void bioset_exit(struct bio_set *);
407 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
408 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
409 
410 struct bio *bio_alloc_bioset(gfp_t gfp, unsigned short nr_iovecs,
411 		struct bio_set *bs);
412 struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
413 extern void bio_put(struct bio *);
414 
415 extern void __bio_clone_fast(struct bio *, struct bio *);
416 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
417 
418 extern struct bio_set fs_bio_set;
419 
420 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned short nr_iovecs)
421 {
422 	return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
423 }
424 
425 extern blk_qc_t submit_bio(struct bio *);
426 
427 extern void bio_endio(struct bio *);
428 
429 static inline void bio_io_error(struct bio *bio)
430 {
431 	bio->bi_status = BLK_STS_IOERR;
432 	bio_endio(bio);
433 }
434 
435 static inline void bio_wouldblock_error(struct bio *bio)
436 {
437 	bio_set_flag(bio, BIO_QUIET);
438 	bio->bi_status = BLK_STS_AGAIN;
439 	bio_endio(bio);
440 }
441 
442 /*
443  * Calculate number of bvec segments that should be allocated to fit data
444  * pointed by @iter. If @iter is backed by bvec it's going to be reused
445  * instead of allocating a new one.
446  */
447 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
448 {
449 	if (iov_iter_is_bvec(iter))
450 		return 0;
451 	return iov_iter_npages(iter, max_segs);
452 }
453 
454 struct request_queue;
455 
456 extern int submit_bio_wait(struct bio *bio);
457 extern void bio_advance(struct bio *, unsigned);
458 
459 extern void bio_init(struct bio *bio, struct bio_vec *table,
460 		     unsigned short max_vecs);
461 extern void bio_uninit(struct bio *);
462 extern void bio_reset(struct bio *);
463 void bio_chain(struct bio *, struct bio *);
464 
465 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
466 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
467 			   unsigned int, unsigned int);
468 int bio_add_zone_append_page(struct bio *bio, struct page *page,
469 			     unsigned int len, unsigned int offset);
470 bool __bio_try_merge_page(struct bio *bio, struct page *page,
471 		unsigned int len, unsigned int off, bool *same_page);
472 void __bio_add_page(struct bio *bio, struct page *page,
473 		unsigned int len, unsigned int off);
474 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
475 void bio_release_pages(struct bio *bio, bool mark_dirty);
476 extern void bio_set_pages_dirty(struct bio *bio);
477 extern void bio_check_pages_dirty(struct bio *bio);
478 
479 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
480 			       struct bio *src, struct bvec_iter *src_iter);
481 extern void bio_copy_data(struct bio *dst, struct bio *src);
482 extern void bio_free_pages(struct bio *bio);
483 void bio_truncate(struct bio *bio, unsigned new_size);
484 void guard_bio_eod(struct bio *bio);
485 void zero_fill_bio(struct bio *bio);
486 
487 extern const char *bio_devname(struct bio *bio, char *buffer);
488 
489 #define bio_set_dev(bio, bdev) 				\
490 do {							\
491 	bio_clear_flag(bio, BIO_REMAPPED);		\
492 	if ((bio)->bi_bdev != (bdev))			\
493 		bio_clear_flag(bio, BIO_THROTTLED);	\
494 	(bio)->bi_bdev = (bdev);			\
495 	bio_associate_blkg(bio);			\
496 } while (0)
497 
498 #define bio_copy_dev(dst, src)			\
499 do {						\
500 	bio_clear_flag(dst, BIO_REMAPPED);		\
501 	(dst)->bi_bdev = (src)->bi_bdev;	\
502 	bio_clone_blkg_association(dst, src);	\
503 } while (0)
504 
505 #define bio_dev(bio) \
506 	disk_devt((bio)->bi_bdev->bd_disk)
507 
508 #ifdef CONFIG_BLK_CGROUP
509 void bio_associate_blkg(struct bio *bio);
510 void bio_associate_blkg_from_css(struct bio *bio,
511 				 struct cgroup_subsys_state *css);
512 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
513 #else	/* CONFIG_BLK_CGROUP */
514 static inline void bio_associate_blkg(struct bio *bio) { }
515 static inline void bio_associate_blkg_from_css(struct bio *bio,
516 					       struct cgroup_subsys_state *css)
517 { }
518 static inline void bio_clone_blkg_association(struct bio *dst,
519 					      struct bio *src) { }
520 #endif	/* CONFIG_BLK_CGROUP */
521 
522 #ifdef CONFIG_HIGHMEM
523 /*
524  * remember never ever reenable interrupts between a bvec_kmap_irq and
525  * bvec_kunmap_irq!
526  */
527 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
528 {
529 	unsigned long addr;
530 
531 	/*
532 	 * might not be a highmem page, but the preempt/irq count
533 	 * balancing is a lot nicer this way
534 	 */
535 	local_irq_save(*flags);
536 	addr = (unsigned long) kmap_atomic(bvec->bv_page);
537 
538 	BUG_ON(addr & ~PAGE_MASK);
539 
540 	return (char *) addr + bvec->bv_offset;
541 }
542 
543 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
544 {
545 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
546 
547 	kunmap_atomic((void *) ptr);
548 	local_irq_restore(*flags);
549 }
550 
551 #else
552 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
553 {
554 	return page_address(bvec->bv_page) + bvec->bv_offset;
555 }
556 
557 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
558 {
559 	*flags = 0;
560 }
561 #endif
562 
563 /*
564  * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
565  *
566  * A bio_list anchors a singly-linked list of bios chained through the bi_next
567  * member of the bio.  The bio_list also caches the last list member to allow
568  * fast access to the tail.
569  */
570 struct bio_list {
571 	struct bio *head;
572 	struct bio *tail;
573 };
574 
575 static inline int bio_list_empty(const struct bio_list *bl)
576 {
577 	return bl->head == NULL;
578 }
579 
580 static inline void bio_list_init(struct bio_list *bl)
581 {
582 	bl->head = bl->tail = NULL;
583 }
584 
585 #define BIO_EMPTY_LIST	{ NULL, NULL }
586 
587 #define bio_list_for_each(bio, bl) \
588 	for (bio = (bl)->head; bio; bio = bio->bi_next)
589 
590 static inline unsigned bio_list_size(const struct bio_list *bl)
591 {
592 	unsigned sz = 0;
593 	struct bio *bio;
594 
595 	bio_list_for_each(bio, bl)
596 		sz++;
597 
598 	return sz;
599 }
600 
601 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
602 {
603 	bio->bi_next = NULL;
604 
605 	if (bl->tail)
606 		bl->tail->bi_next = bio;
607 	else
608 		bl->head = bio;
609 
610 	bl->tail = bio;
611 }
612 
613 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
614 {
615 	bio->bi_next = bl->head;
616 
617 	bl->head = bio;
618 
619 	if (!bl->tail)
620 		bl->tail = bio;
621 }
622 
623 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
624 {
625 	if (!bl2->head)
626 		return;
627 
628 	if (bl->tail)
629 		bl->tail->bi_next = bl2->head;
630 	else
631 		bl->head = bl2->head;
632 
633 	bl->tail = bl2->tail;
634 }
635 
636 static inline void bio_list_merge_head(struct bio_list *bl,
637 				       struct bio_list *bl2)
638 {
639 	if (!bl2->head)
640 		return;
641 
642 	if (bl->head)
643 		bl2->tail->bi_next = bl->head;
644 	else
645 		bl->tail = bl2->tail;
646 
647 	bl->head = bl2->head;
648 }
649 
650 static inline struct bio *bio_list_peek(struct bio_list *bl)
651 {
652 	return bl->head;
653 }
654 
655 static inline struct bio *bio_list_pop(struct bio_list *bl)
656 {
657 	struct bio *bio = bl->head;
658 
659 	if (bio) {
660 		bl->head = bl->head->bi_next;
661 		if (!bl->head)
662 			bl->tail = NULL;
663 
664 		bio->bi_next = NULL;
665 	}
666 
667 	return bio;
668 }
669 
670 static inline struct bio *bio_list_get(struct bio_list *bl)
671 {
672 	struct bio *bio = bl->head;
673 
674 	bl->head = bl->tail = NULL;
675 
676 	return bio;
677 }
678 
679 /*
680  * Increment chain count for the bio. Make sure the CHAIN flag update
681  * is visible before the raised count.
682  */
683 static inline void bio_inc_remaining(struct bio *bio)
684 {
685 	bio_set_flag(bio, BIO_CHAIN);
686 	smp_mb__before_atomic();
687 	atomic_inc(&bio->__bi_remaining);
688 }
689 
690 /*
691  * bio_set is used to allow other portions of the IO system to
692  * allocate their own private memory pools for bio and iovec structures.
693  * These memory pools in turn all allocate from the bio_slab
694  * and the bvec_slabs[].
695  */
696 #define BIO_POOL_SIZE 2
697 
698 struct bio_set {
699 	struct kmem_cache *bio_slab;
700 	unsigned int front_pad;
701 
702 	mempool_t bio_pool;
703 	mempool_t bvec_pool;
704 #if defined(CONFIG_BLK_DEV_INTEGRITY)
705 	mempool_t bio_integrity_pool;
706 	mempool_t bvec_integrity_pool;
707 #endif
708 
709 	unsigned int back_pad;
710 	/*
711 	 * Deadlock avoidance for stacking block drivers: see comments in
712 	 * bio_alloc_bioset() for details
713 	 */
714 	spinlock_t		rescue_lock;
715 	struct bio_list		rescue_list;
716 	struct work_struct	rescue_work;
717 	struct workqueue_struct	*rescue_workqueue;
718 };
719 
720 static inline bool bioset_initialized(struct bio_set *bs)
721 {
722 	return bs->bio_slab != NULL;
723 }
724 
725 #if defined(CONFIG_BLK_DEV_INTEGRITY)
726 
727 #define bip_for_each_vec(bvl, bip, iter)				\
728 	for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
729 
730 #define bio_for_each_integrity_vec(_bvl, _bio, _iter)			\
731 	for_each_bio(_bio)						\
732 		bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
733 
734 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
735 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
736 extern bool bio_integrity_prep(struct bio *);
737 extern void bio_integrity_advance(struct bio *, unsigned int);
738 extern void bio_integrity_trim(struct bio *);
739 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
740 extern int bioset_integrity_create(struct bio_set *, int);
741 extern void bioset_integrity_free(struct bio_set *);
742 extern void bio_integrity_init(void);
743 
744 #else /* CONFIG_BLK_DEV_INTEGRITY */
745 
746 static inline void *bio_integrity(struct bio *bio)
747 {
748 	return NULL;
749 }
750 
751 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
752 {
753 	return 0;
754 }
755 
756 static inline void bioset_integrity_free (struct bio_set *bs)
757 {
758 	return;
759 }
760 
761 static inline bool bio_integrity_prep(struct bio *bio)
762 {
763 	return true;
764 }
765 
766 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
767 				      gfp_t gfp_mask)
768 {
769 	return 0;
770 }
771 
772 static inline void bio_integrity_advance(struct bio *bio,
773 					 unsigned int bytes_done)
774 {
775 	return;
776 }
777 
778 static inline void bio_integrity_trim(struct bio *bio)
779 {
780 	return;
781 }
782 
783 static inline void bio_integrity_init(void)
784 {
785 	return;
786 }
787 
788 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
789 {
790 	return false;
791 }
792 
793 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
794 								unsigned int nr)
795 {
796 	return ERR_PTR(-EINVAL);
797 }
798 
799 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
800 					unsigned int len, unsigned int offset)
801 {
802 	return 0;
803 }
804 
805 #endif /* CONFIG_BLK_DEV_INTEGRITY */
806 
807 /*
808  * Mark a bio as polled. Note that for async polled IO, the caller must
809  * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
810  * We cannot block waiting for requests on polled IO, as those completions
811  * must be found by the caller. This is different than IRQ driven IO, where
812  * it's safe to wait for IO to complete.
813  */
814 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
815 {
816 	bio->bi_opf |= REQ_HIPRI;
817 	if (!is_sync_kiocb(kiocb))
818 		bio->bi_opf |= REQ_NOWAIT;
819 }
820 
821 struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
822 
823 #endif /* __LINUX_BIO_H */
824