xref: /openbmc/linux/include/linux/bio.h (revision 249592bf)
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_free_pages(struct bio *bio);
487 void bio_truncate(struct bio *bio, unsigned new_size);
488 void guard_bio_eod(struct bio *bio);
489 void zero_fill_bio(struct bio *bio);
490 
491 extern const char *bio_devname(struct bio *bio, char *buffer);
492 
493 #define bio_set_dev(bio, bdev) 				\
494 do {							\
495 	bio_clear_flag(bio, BIO_REMAPPED);		\
496 	if ((bio)->bi_bdev != (bdev))			\
497 		bio_clear_flag(bio, BIO_THROTTLED);	\
498 	(bio)->bi_bdev = (bdev);			\
499 	bio_associate_blkg(bio);			\
500 } while (0)
501 
502 #define bio_copy_dev(dst, src)			\
503 do {						\
504 	bio_clear_flag(dst, BIO_REMAPPED);		\
505 	(dst)->bi_bdev = (src)->bi_bdev;	\
506 	bio_clone_blkg_association(dst, src);	\
507 } while (0)
508 
509 #define bio_dev(bio) \
510 	disk_devt((bio)->bi_bdev->bd_disk)
511 
512 #ifdef CONFIG_BLK_CGROUP
513 void bio_associate_blkg(struct bio *bio);
514 void bio_associate_blkg_from_css(struct bio *bio,
515 				 struct cgroup_subsys_state *css);
516 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
517 #else	/* CONFIG_BLK_CGROUP */
518 static inline void bio_associate_blkg(struct bio *bio) { }
519 static inline void bio_associate_blkg_from_css(struct bio *bio,
520 					       struct cgroup_subsys_state *css)
521 { }
522 static inline void bio_clone_blkg_association(struct bio *dst,
523 					      struct bio *src) { }
524 #endif	/* CONFIG_BLK_CGROUP */
525 
526 #ifdef CONFIG_HIGHMEM
527 /*
528  * remember never ever reenable interrupts between a bvec_kmap_irq and
529  * bvec_kunmap_irq!
530  */
531 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
532 {
533 	unsigned long addr;
534 
535 	/*
536 	 * might not be a highmem page, but the preempt/irq count
537 	 * balancing is a lot nicer this way
538 	 */
539 	local_irq_save(*flags);
540 	addr = (unsigned long) kmap_atomic(bvec->bv_page);
541 
542 	BUG_ON(addr & ~PAGE_MASK);
543 
544 	return (char *) addr + bvec->bv_offset;
545 }
546 
547 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
548 {
549 	unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
550 
551 	kunmap_atomic((void *) ptr);
552 	local_irq_restore(*flags);
553 }
554 
555 #else
556 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
557 {
558 	return page_address(bvec->bv_page) + bvec->bv_offset;
559 }
560 
561 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
562 {
563 	*flags = 0;
564 }
565 #endif
566 
567 /*
568  * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
569  *
570  * A bio_list anchors a singly-linked list of bios chained through the bi_next
571  * member of the bio.  The bio_list also caches the last list member to allow
572  * fast access to the tail.
573  */
574 struct bio_list {
575 	struct bio *head;
576 	struct bio *tail;
577 };
578 
579 static inline int bio_list_empty(const struct bio_list *bl)
580 {
581 	return bl->head == NULL;
582 }
583 
584 static inline void bio_list_init(struct bio_list *bl)
585 {
586 	bl->head = bl->tail = NULL;
587 }
588 
589 #define BIO_EMPTY_LIST	{ NULL, NULL }
590 
591 #define bio_list_for_each(bio, bl) \
592 	for (bio = (bl)->head; bio; bio = bio->bi_next)
593 
594 static inline unsigned bio_list_size(const struct bio_list *bl)
595 {
596 	unsigned sz = 0;
597 	struct bio *bio;
598 
599 	bio_list_for_each(bio, bl)
600 		sz++;
601 
602 	return sz;
603 }
604 
605 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
606 {
607 	bio->bi_next = NULL;
608 
609 	if (bl->tail)
610 		bl->tail->bi_next = bio;
611 	else
612 		bl->head = bio;
613 
614 	bl->tail = bio;
615 }
616 
617 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
618 {
619 	bio->bi_next = bl->head;
620 
621 	bl->head = bio;
622 
623 	if (!bl->tail)
624 		bl->tail = bio;
625 }
626 
627 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
628 {
629 	if (!bl2->head)
630 		return;
631 
632 	if (bl->tail)
633 		bl->tail->bi_next = bl2->head;
634 	else
635 		bl->head = bl2->head;
636 
637 	bl->tail = bl2->tail;
638 }
639 
640 static inline void bio_list_merge_head(struct bio_list *bl,
641 				       struct bio_list *bl2)
642 {
643 	if (!bl2->head)
644 		return;
645 
646 	if (bl->head)
647 		bl2->tail->bi_next = bl->head;
648 	else
649 		bl->tail = bl2->tail;
650 
651 	bl->head = bl2->head;
652 }
653 
654 static inline struct bio *bio_list_peek(struct bio_list *bl)
655 {
656 	return bl->head;
657 }
658 
659 static inline struct bio *bio_list_pop(struct bio_list *bl)
660 {
661 	struct bio *bio = bl->head;
662 
663 	if (bio) {
664 		bl->head = bl->head->bi_next;
665 		if (!bl->head)
666 			bl->tail = NULL;
667 
668 		bio->bi_next = NULL;
669 	}
670 
671 	return bio;
672 }
673 
674 static inline struct bio *bio_list_get(struct bio_list *bl)
675 {
676 	struct bio *bio = bl->head;
677 
678 	bl->head = bl->tail = NULL;
679 
680 	return bio;
681 }
682 
683 /*
684  * Increment chain count for the bio. Make sure the CHAIN flag update
685  * is visible before the raised count.
686  */
687 static inline void bio_inc_remaining(struct bio *bio)
688 {
689 	bio_set_flag(bio, BIO_CHAIN);
690 	smp_mb__before_atomic();
691 	atomic_inc(&bio->__bi_remaining);
692 }
693 
694 /*
695  * bio_set is used to allow other portions of the IO system to
696  * allocate their own private memory pools for bio and iovec structures.
697  * These memory pools in turn all allocate from the bio_slab
698  * and the bvec_slabs[].
699  */
700 #define BIO_POOL_SIZE 2
701 
702 struct bio_set {
703 	struct kmem_cache *bio_slab;
704 	unsigned int front_pad;
705 
706 	mempool_t bio_pool;
707 	mempool_t bvec_pool;
708 #if defined(CONFIG_BLK_DEV_INTEGRITY)
709 	mempool_t bio_integrity_pool;
710 	mempool_t bvec_integrity_pool;
711 #endif
712 
713 	unsigned int back_pad;
714 	/*
715 	 * Deadlock avoidance for stacking block drivers: see comments in
716 	 * bio_alloc_bioset() for details
717 	 */
718 	spinlock_t		rescue_lock;
719 	struct bio_list		rescue_list;
720 	struct work_struct	rescue_work;
721 	struct workqueue_struct	*rescue_workqueue;
722 };
723 
724 static inline bool bioset_initialized(struct bio_set *bs)
725 {
726 	return bs->bio_slab != NULL;
727 }
728 
729 #if defined(CONFIG_BLK_DEV_INTEGRITY)
730 
731 #define bip_for_each_vec(bvl, bip, iter)				\
732 	for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
733 
734 #define bio_for_each_integrity_vec(_bvl, _bio, _iter)			\
735 	for_each_bio(_bio)						\
736 		bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
737 
738 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
739 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
740 extern bool bio_integrity_prep(struct bio *);
741 extern void bio_integrity_advance(struct bio *, unsigned int);
742 extern void bio_integrity_trim(struct bio *);
743 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
744 extern int bioset_integrity_create(struct bio_set *, int);
745 extern void bioset_integrity_free(struct bio_set *);
746 extern void bio_integrity_init(void);
747 
748 #else /* CONFIG_BLK_DEV_INTEGRITY */
749 
750 static inline void *bio_integrity(struct bio *bio)
751 {
752 	return NULL;
753 }
754 
755 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
756 {
757 	return 0;
758 }
759 
760 static inline void bioset_integrity_free (struct bio_set *bs)
761 {
762 	return;
763 }
764 
765 static inline bool bio_integrity_prep(struct bio *bio)
766 {
767 	return true;
768 }
769 
770 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
771 				      gfp_t gfp_mask)
772 {
773 	return 0;
774 }
775 
776 static inline void bio_integrity_advance(struct bio *bio,
777 					 unsigned int bytes_done)
778 {
779 	return;
780 }
781 
782 static inline void bio_integrity_trim(struct bio *bio)
783 {
784 	return;
785 }
786 
787 static inline void bio_integrity_init(void)
788 {
789 	return;
790 }
791 
792 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
793 {
794 	return false;
795 }
796 
797 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
798 								unsigned int nr)
799 {
800 	return ERR_PTR(-EINVAL);
801 }
802 
803 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
804 					unsigned int len, unsigned int offset)
805 {
806 	return 0;
807 }
808 
809 #endif /* CONFIG_BLK_DEV_INTEGRITY */
810 
811 /*
812  * Mark a bio as polled. Note that for async polled IO, the caller must
813  * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
814  * We cannot block waiting for requests on polled IO, as those completions
815  * must be found by the caller. This is different than IRQ driven IO, where
816  * it's safe to wait for IO to complete.
817  */
818 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
819 {
820 	bio->bi_opf |= REQ_HIPRI;
821 	if (!is_sync_kiocb(kiocb))
822 		bio->bi_opf |= REQ_NOWAIT;
823 }
824 
825 #endif /* __LINUX_BIO_H */
826