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