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