xref: /linux-6.15/include/linux/bio.h (revision e8007fad)
18c16567dSChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2001 Jens Axboe <[email protected]>
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds #ifndef __LINUX_BIO_H
61da177e4SLinus Torvalds #define __LINUX_BIO_H
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mempool.h>
97cc01581STejun Heo /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
107cc01581STejun Heo #include <linux/blk_types.h>
113e1a88ecSPavel Begunkov #include <linux/uio.h>
127cc01581STejun Heo 
13a8affc03SChristoph Hellwig #define BIO_MAX_VECS		256U
14*e8007fadSSteve Siwinski #define BIO_MAX_INLINE_VECS	UIO_MAXIOV
155f7136dbSMatthew Wilcox (Oracle) 
16fd8f8edeSChristoph Hellwig struct queue_limits;
17fd8f8edeSChristoph Hellwig 
bio_max_segs(unsigned int nr_segs)185f7136dbSMatthew Wilcox (Oracle) static inline unsigned int bio_max_segs(unsigned int nr_segs)
195f7136dbSMatthew Wilcox (Oracle) {
20a8affc03SChristoph Hellwig 	return min(nr_segs, BIO_MAX_VECS);
215f7136dbSMatthew Wilcox (Oracle) }
221da177e4SLinus Torvalds 
234550dd6cSKent Overstreet #define bio_iter_iovec(bio, iter)				\
244550dd6cSKent Overstreet 	bvec_iter_bvec((bio)->bi_io_vec, (iter))
254550dd6cSKent Overstreet 
264550dd6cSKent Overstreet #define bio_iter_page(bio, iter)				\
274550dd6cSKent Overstreet 	bvec_iter_page((bio)->bi_io_vec, (iter))
284550dd6cSKent Overstreet #define bio_iter_len(bio, iter)					\
294550dd6cSKent Overstreet 	bvec_iter_len((bio)->bi_io_vec, (iter))
304550dd6cSKent Overstreet #define bio_iter_offset(bio, iter)				\
314550dd6cSKent Overstreet 	bvec_iter_offset((bio)->bi_io_vec, (iter))
324550dd6cSKent Overstreet 
334550dd6cSKent Overstreet #define bio_page(bio)		bio_iter_page((bio), (bio)->bi_iter)
344550dd6cSKent Overstreet #define bio_offset(bio)		bio_iter_offset((bio), (bio)->bi_iter)
354550dd6cSKent Overstreet #define bio_iovec(bio)		bio_iter_iovec((bio), (bio)->bi_iter)
367988613bSKent Overstreet 
3738a72dacSKent Overstreet #define bvec_iter_sectors(iter)	((iter).bi_size >> 9)
3838a72dacSKent Overstreet #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
3938a72dacSKent Overstreet 
4038a72dacSKent Overstreet #define bio_sectors(bio)	bvec_iter_sectors((bio)->bi_iter)
4138a72dacSKent Overstreet #define bio_end_sector(bio)	bvec_iter_end_sector((bio)->bi_iter)
42bf2de6f5SJens Axboe 
43458b76edSKent Overstreet /*
44d3849953SChristoph Hellwig  * Return the data direction, READ or WRITE.
45d3849953SChristoph Hellwig  */
46d3849953SChristoph Hellwig #define bio_data_dir(bio) \
47d3849953SChristoph Hellwig 	(op_is_write(bio_op(bio)) ? WRITE : READ)
48d3849953SChristoph Hellwig 
49d3849953SChristoph Hellwig /*
50458b76edSKent Overstreet  * Check whether this bio carries any data or not. A NULL bio is allowed.
51458b76edSKent Overstreet  */
bio_has_data(struct bio * bio)52458b76edSKent Overstreet static inline bool bio_has_data(struct bio *bio)
53458b76edSKent Overstreet {
54458b76edSKent Overstreet 	if (bio &&
55458b76edSKent Overstreet 	    bio->bi_iter.bi_size &&
567afafc8aSAdrian Hunter 	    bio_op(bio) != REQ_OP_DISCARD &&
57a6f0788eSChaitanya Kulkarni 	    bio_op(bio) != REQ_OP_SECURE_ERASE &&
58a6f0788eSChaitanya Kulkarni 	    bio_op(bio) != REQ_OP_WRITE_ZEROES)
59458b76edSKent Overstreet 		return true;
60458b76edSKent Overstreet 
61458b76edSKent Overstreet 	return false;
62458b76edSKent Overstreet }
63458b76edSKent Overstreet 
bio_no_advance_iter(const struct bio * bio)64c1527c0eSBart Van Assche static inline bool bio_no_advance_iter(const struct bio *bio)
6595fe6c1aSMike Christie {
667afafc8aSAdrian Hunter 	return bio_op(bio) == REQ_OP_DISCARD ||
677afafc8aSAdrian Hunter 	       bio_op(bio) == REQ_OP_SECURE_ERASE ||
68a6f0788eSChaitanya Kulkarni 	       bio_op(bio) == REQ_OP_WRITE_ZEROES;
6995fe6c1aSMike Christie }
7095fe6c1aSMike Christie 
bio_data(struct bio * bio)71bf2de6f5SJens Axboe static inline void *bio_data(struct bio *bio)
72bf2de6f5SJens Axboe {
73458b76edSKent Overstreet 	if (bio_has_data(bio))
74bf2de6f5SJens Axboe 		return page_address(bio_page(bio)) + bio_offset(bio);
75bf2de6f5SJens Axboe 
76bf2de6f5SJens Axboe 	return NULL;
77bf2de6f5SJens Axboe }
781da177e4SLinus Torvalds 
bio_next_segment(const struct bio * bio,struct bvec_iter_all * iter)791200e07fSMing Lei static inline bool bio_next_segment(const struct bio *bio,
801200e07fSMing Lei 				    struct bvec_iter_all *iter)
811200e07fSMing Lei {
821200e07fSMing Lei 	if (iter->idx >= bio->bi_vcnt)
831200e07fSMing Lei 		return false;
841200e07fSMing Lei 
851200e07fSMing Lei 	bvec_advance(&bio->bi_io_vec[iter->idx], iter);
861200e07fSMing Lei 	return true;
871200e07fSMing Lei }
886dc4f100SMing Lei 
891da177e4SLinus Torvalds /*
90d74c6d51SKent Overstreet  * drivers should _never_ use the all version - the bio may have been split
91d74c6d51SKent Overstreet  * before it got to the driver and the driver won't own all of it
92d74c6d51SKent Overstreet  */
932b070cfeSChristoph Hellwig #define bio_for_each_segment_all(bvl, bio, iter) \
942b070cfeSChristoph Hellwig 	for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
95d74c6d51SKent Overstreet 
bio_advance_iter(const struct bio * bio,struct bvec_iter * iter,unsigned int bytes)96c1527c0eSBart Van Assche static inline void bio_advance_iter(const struct bio *bio,
97c1527c0eSBart Van Assche 				    struct bvec_iter *iter, unsigned int bytes)
984550dd6cSKent Overstreet {
994550dd6cSKent Overstreet 	iter->bi_sector += bytes >> 9;
1004550dd6cSKent Overstreet 
1017759eb23SMing Lei 	if (bio_no_advance_iter(bio))
1024550dd6cSKent Overstreet 		iter->bi_size -= bytes;
1037759eb23SMing Lei 	else
1044550dd6cSKent Overstreet 		bvec_iter_advance(bio->bi_io_vec, iter, bytes);
105b1fb2c52SDmitry Monakhov 		/* TODO: It is reasonable to complete bio with error here. */
106b1fb2c52SDmitry Monakhov }
107f9df1cd9SDmitry Monakhov 
10822b56c29SPavel Begunkov /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
bio_advance_iter_single(const struct bio * bio,struct bvec_iter * iter,unsigned int bytes)10922b56c29SPavel Begunkov static inline void bio_advance_iter_single(const struct bio *bio,
11022b56c29SPavel Begunkov 					   struct bvec_iter *iter,
11122b56c29SPavel Begunkov 					   unsigned int bytes)
11222b56c29SPavel Begunkov {
11322b56c29SPavel Begunkov 	iter->bi_sector += bytes >> 9;
11422b56c29SPavel Begunkov 
11522b56c29SPavel Begunkov 	if (bio_no_advance_iter(bio))
11622b56c29SPavel Begunkov 		iter->bi_size -= bytes;
11722b56c29SPavel Begunkov 	else
11822b56c29SPavel Begunkov 		bvec_iter_advance_single(bio->bi_io_vec, iter, bytes);
11922b56c29SPavel Begunkov }
12022b56c29SPavel Begunkov 
121d4aa57a1SJens Axboe void __bio_advance(struct bio *, unsigned bytes);
122d4aa57a1SJens Axboe 
123d4aa57a1SJens Axboe /**
124d4aa57a1SJens Axboe  * bio_advance - increment/complete a bio by some number of bytes
125d4aa57a1SJens Axboe  * @bio:	bio to advance
1266fd3c510SRandy Dunlap  * @nbytes:	number of bytes to complete
127d4aa57a1SJens Axboe  *
128d4aa57a1SJens Axboe  * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
129d4aa57a1SJens Axboe  * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
130d4aa57a1SJens Axboe  * be updated on the last bvec as well.
131d4aa57a1SJens Axboe  *
132d4aa57a1SJens Axboe  * @bio will then represent the remaining, uncompleted portion of the io.
133d4aa57a1SJens Axboe  */
bio_advance(struct bio * bio,unsigned int nbytes)134d4aa57a1SJens Axboe static inline void bio_advance(struct bio *bio, unsigned int nbytes)
135d4aa57a1SJens Axboe {
136d4aa57a1SJens Axboe 	if (nbytes == bio->bi_iter.bi_size) {
137d4aa57a1SJens Axboe 		bio->bi_iter.bi_size = 0;
138d4aa57a1SJens Axboe 		return;
139d4aa57a1SJens Axboe 	}
140d4aa57a1SJens Axboe 	__bio_advance(bio, nbytes);
141d4aa57a1SJens Axboe }
142d4aa57a1SJens Axboe 
1437988613bSKent Overstreet #define __bio_for_each_segment(bvl, bio, iter, start)			\
1447988613bSKent Overstreet 	for (iter = (start);						\
1454550dd6cSKent Overstreet 	     (iter).bi_size &&						\
1464550dd6cSKent Overstreet 		((bvl = bio_iter_iovec((bio), (iter))), 1);		\
14722b56c29SPavel Begunkov 	     bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
1487988613bSKent Overstreet 
1497988613bSKent Overstreet #define bio_for_each_segment(bvl, bio, iter)				\
1507988613bSKent Overstreet 	__bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
1517988613bSKent Overstreet 
152d18d9174SMing Lei #define __bio_for_each_bvec(bvl, bio, iter, start)		\
153d18d9174SMing Lei 	for (iter = (start);						\
154d18d9174SMing Lei 	     (iter).bi_size &&						\
155d18d9174SMing Lei 		((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
15622b56c29SPavel Begunkov 	     bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
157d18d9174SMing Lei 
158d18d9174SMing Lei /* iterate over multi-page bvec */
159d18d9174SMing Lei #define bio_for_each_bvec(bvl, bio, iter)			\
160d18d9174SMing Lei 	__bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
161d18d9174SMing Lei 
1621072c12dSOmar Sandoval /*
1631072c12dSOmar Sandoval  * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
1641072c12dSOmar Sandoval  * same reasons as bio_for_each_segment_all().
1651072c12dSOmar Sandoval  */
1661072c12dSOmar Sandoval #define bio_for_each_bvec_all(bvl, bio, i)		\
1671072c12dSOmar Sandoval 	for (i = 0, bvl = bio_first_bvec_all(bio);	\
168640d1930SMatthew Wilcox (Oracle) 	     i < (bio)->bi_vcnt; i++, bvl++)
1691072c12dSOmar Sandoval 
1704550dd6cSKent Overstreet #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
1711da177e4SLinus Torvalds 
bio_segments(struct bio * bio)172f4595875SShaohua Li static inline unsigned bio_segments(struct bio *bio)
173458b76edSKent Overstreet {
174458b76edSKent Overstreet 	unsigned segs = 0;
175458b76edSKent Overstreet 	struct bio_vec bv;
176458b76edSKent Overstreet 	struct bvec_iter iter;
177458b76edSKent Overstreet 
1788423ae3dSKent Overstreet 	/*
179a6f0788eSChaitanya Kulkarni 	 * We special case discard/write same/write zeroes, because they
180a6f0788eSChaitanya Kulkarni 	 * interpret bi_size differently:
1818423ae3dSKent Overstreet 	 */
1828423ae3dSKent Overstreet 
183a6f0788eSChaitanya Kulkarni 	switch (bio_op(bio)) {
184a6f0788eSChaitanya Kulkarni 	case REQ_OP_DISCARD:
185a6f0788eSChaitanya Kulkarni 	case REQ_OP_SECURE_ERASE:
186a6f0788eSChaitanya Kulkarni 	case REQ_OP_WRITE_ZEROES:
187f9d03f96SChristoph Hellwig 		return 0;
188a6f0788eSChaitanya Kulkarni 	default:
189a6f0788eSChaitanya Kulkarni 		break;
190a6f0788eSChaitanya Kulkarni 	}
1918423ae3dSKent Overstreet 
192f4595875SShaohua Li 	bio_for_each_segment(bv, bio, iter)
193458b76edSKent Overstreet 		segs++;
194458b76edSKent Overstreet 
195458b76edSKent Overstreet 	return segs;
196458b76edSKent Overstreet }
197458b76edSKent Overstreet 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  * get a reference to a bio, so it won't disappear. the intended use is
2001da177e4SLinus Torvalds  * something like:
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * bio_get(bio);
2031da177e4SLinus Torvalds  * submit_bio(rw, bio);
2041da177e4SLinus Torvalds  * if (bio->bi_flags ...)
2051da177e4SLinus Torvalds  *	do_something
2061da177e4SLinus Torvalds  * bio_put(bio);
2071da177e4SLinus Torvalds  *
2081da177e4SLinus Torvalds  * without the bio_get(), it could potentially complete I/O before submit_bio
2091da177e4SLinus Torvalds  * returns. and then bio would be freed memory when if (bio->bi_flags ...)
2101da177e4SLinus Torvalds  * runs
2111da177e4SLinus Torvalds  */
bio_get(struct bio * bio)212dac56212SJens Axboe static inline void bio_get(struct bio *bio)
213dac56212SJens Axboe {
214dac56212SJens Axboe 	bio->bi_flags |= (1 << BIO_REFFED);
215dac56212SJens Axboe 	smp_mb__before_atomic();
216dac56212SJens Axboe 	atomic_inc(&bio->__bi_cnt);
217dac56212SJens Axboe }
218dac56212SJens Axboe 
bio_cnt_set(struct bio * bio,unsigned int count)219dac56212SJens Axboe static inline void bio_cnt_set(struct bio *bio, unsigned int count)
220dac56212SJens Axboe {
221dac56212SJens Axboe 	if (count != 1) {
222dac56212SJens Axboe 		bio->bi_flags |= (1 << BIO_REFFED);
223f381c6a4SAndrea Parri 		smp_mb();
224dac56212SJens Axboe 	}
225dac56212SJens Axboe 	atomic_set(&bio->__bi_cnt, count);
226dac56212SJens Axboe }
2271da177e4SLinus Torvalds 
bio_flagged(struct bio * bio,unsigned int bit)228b7c44ed9SJens Axboe static inline bool bio_flagged(struct bio *bio, unsigned int bit)
229b7c44ed9SJens Axboe {
23009e8c253SDavid Howells 	return bio->bi_flags & (1U << bit);
231b7c44ed9SJens Axboe }
232b7c44ed9SJens Axboe 
bio_set_flag(struct bio * bio,unsigned int bit)233b7c44ed9SJens Axboe static inline void bio_set_flag(struct bio *bio, unsigned int bit)
234b7c44ed9SJens Axboe {
2352c68f6dcSJens Axboe 	bio->bi_flags |= (1U << bit);
236b7c44ed9SJens Axboe }
237b7c44ed9SJens Axboe 
bio_clear_flag(struct bio * bio,unsigned int bit)238b7c44ed9SJens Axboe static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
239b7c44ed9SJens Axboe {
2402c68f6dcSJens Axboe 	bio->bi_flags &= ~(1U << bit);
241b7c44ed9SJens Axboe }
242b7c44ed9SJens Axboe 
bio_first_bvec_all(struct bio * bio)24386292abcSMing Lei static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
24486292abcSMing Lei {
24586292abcSMing Lei 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
24686292abcSMing Lei 	return bio->bi_io_vec;
24786292abcSMing Lei }
24886292abcSMing Lei 
bio_first_page_all(struct bio * bio)24986292abcSMing Lei static inline struct page *bio_first_page_all(struct bio *bio)
25086292abcSMing Lei {
25186292abcSMing Lei 	return bio_first_bvec_all(bio)->bv_page;
25286292abcSMing Lei }
25386292abcSMing Lei 
bio_first_folio_all(struct bio * bio)2546d2790d9SZhangPeng static inline struct folio *bio_first_folio_all(struct bio *bio)
2556d2790d9SZhangPeng {
2566d2790d9SZhangPeng 	return page_folio(bio_first_page_all(bio));
2576d2790d9SZhangPeng }
2586d2790d9SZhangPeng 
bio_last_bvec_all(struct bio * bio)25986292abcSMing Lei static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
26086292abcSMing Lei {
26186292abcSMing Lei 	WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
26286292abcSMing Lei 	return &bio->bi_io_vec[bio->bi_vcnt - 1];
26386292abcSMing Lei }
26486292abcSMing Lei 
265640d1930SMatthew Wilcox (Oracle) /**
266640d1930SMatthew Wilcox (Oracle)  * struct folio_iter - State for iterating all folios in a bio.
267640d1930SMatthew Wilcox (Oracle)  * @folio: The current folio we're iterating.  NULL after the last folio.
268640d1930SMatthew Wilcox (Oracle)  * @offset: The byte offset within the current folio.
269640d1930SMatthew Wilcox (Oracle)  * @length: The number of bytes in this iteration (will not cross folio
270640d1930SMatthew Wilcox (Oracle)  *	boundary).
271640d1930SMatthew Wilcox (Oracle)  */
272640d1930SMatthew Wilcox (Oracle) struct folio_iter {
273640d1930SMatthew Wilcox (Oracle) 	struct folio *folio;
274640d1930SMatthew Wilcox (Oracle) 	size_t offset;
275640d1930SMatthew Wilcox (Oracle) 	size_t length;
276640d1930SMatthew Wilcox (Oracle) 	/* private: for use by the iterator */
277170f37d6SMatthew Wilcox (Oracle) 	struct folio *_next;
278640d1930SMatthew Wilcox (Oracle) 	size_t _seg_count;
279640d1930SMatthew Wilcox (Oracle) 	int _i;
280640d1930SMatthew Wilcox (Oracle) };
281640d1930SMatthew Wilcox (Oracle) 
bio_first_folio(struct folio_iter * fi,struct bio * bio,int i)282640d1930SMatthew Wilcox (Oracle) static inline void bio_first_folio(struct folio_iter *fi, struct bio *bio,
283640d1930SMatthew Wilcox (Oracle) 				   int i)
284640d1930SMatthew Wilcox (Oracle) {
285640d1930SMatthew Wilcox (Oracle) 	struct bio_vec *bvec = bio_first_bvec_all(bio) + i;
286640d1930SMatthew Wilcox (Oracle) 
2877bed6f3dSMatthew Wilcox (Oracle) 	if (unlikely(i >= bio->bi_vcnt)) {
2887bed6f3dSMatthew Wilcox (Oracle) 		fi->folio = NULL;
2897bed6f3dSMatthew Wilcox (Oracle) 		return;
2907bed6f3dSMatthew Wilcox (Oracle) 	}
2917bed6f3dSMatthew Wilcox (Oracle) 
292640d1930SMatthew Wilcox (Oracle) 	fi->folio = page_folio(bvec->bv_page);
293640d1930SMatthew Wilcox (Oracle) 	fi->offset = bvec->bv_offset +
294640d1930SMatthew Wilcox (Oracle) 			PAGE_SIZE * (bvec->bv_page - &fi->folio->page);
295640d1930SMatthew Wilcox (Oracle) 	fi->_seg_count = bvec->bv_len;
296640d1930SMatthew Wilcox (Oracle) 	fi->length = min(folio_size(fi->folio) - fi->offset, fi->_seg_count);
297170f37d6SMatthew Wilcox (Oracle) 	fi->_next = folio_next(fi->folio);
298640d1930SMatthew Wilcox (Oracle) 	fi->_i = i;
299640d1930SMatthew Wilcox (Oracle) }
300640d1930SMatthew Wilcox (Oracle) 
bio_next_folio(struct folio_iter * fi,struct bio * bio)301640d1930SMatthew Wilcox (Oracle) static inline void bio_next_folio(struct folio_iter *fi, struct bio *bio)
302640d1930SMatthew Wilcox (Oracle) {
303640d1930SMatthew Wilcox (Oracle) 	fi->_seg_count -= fi->length;
304640d1930SMatthew Wilcox (Oracle) 	if (fi->_seg_count) {
305170f37d6SMatthew Wilcox (Oracle) 		fi->folio = fi->_next;
306640d1930SMatthew Wilcox (Oracle) 		fi->offset = 0;
307640d1930SMatthew Wilcox (Oracle) 		fi->length = min(folio_size(fi->folio), fi->_seg_count);
308170f37d6SMatthew Wilcox (Oracle) 		fi->_next = folio_next(fi->folio);
309640d1930SMatthew Wilcox (Oracle) 	} else {
3107bed6f3dSMatthew Wilcox (Oracle) 		bio_first_folio(fi, bio, fi->_i + 1);
311640d1930SMatthew Wilcox (Oracle) 	}
312640d1930SMatthew Wilcox (Oracle) }
313640d1930SMatthew Wilcox (Oracle) 
314640d1930SMatthew Wilcox (Oracle) /**
315640d1930SMatthew Wilcox (Oracle)  * bio_for_each_folio_all - Iterate over each folio in a bio.
316640d1930SMatthew Wilcox (Oracle)  * @fi: struct folio_iter which is updated for each folio.
317640d1930SMatthew Wilcox (Oracle)  * @bio: struct bio to iterate over.
318640d1930SMatthew Wilcox (Oracle)  */
319640d1930SMatthew Wilcox (Oracle) #define bio_for_each_folio_all(fi, bio)				\
320640d1930SMatthew Wilcox (Oracle) 	for (bio_first_folio(&fi, bio, 0); fi.folio; bio_next_folio(&fi, bio))
321640d1930SMatthew Wilcox (Oracle) 
322e83502caSChaitanya Kulkarni void bio_trim(struct bio *bio, sector_t offset, sector_t size);
32320d0189bSKent Overstreet extern struct bio *bio_split(struct bio *bio, int sectors,
32420d0189bSKent Overstreet 			     gfp_t gfp, struct bio_set *bs);
325b35243a4SChristoph Hellwig int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim,
326b35243a4SChristoph Hellwig 		unsigned *segs, unsigned max_bytes);
32720d0189bSKent Overstreet 
32820d0189bSKent Overstreet /**
32920d0189bSKent Overstreet  * bio_next_split - get next @sectors from a bio, splitting if necessary
33020d0189bSKent Overstreet  * @bio:	bio to split
33120d0189bSKent Overstreet  * @sectors:	number of sectors to split from the front of @bio
33220d0189bSKent Overstreet  * @gfp:	gfp mask
33320d0189bSKent Overstreet  * @bs:		bio set to allocate from
33420d0189bSKent Overstreet  *
3356fd3c510SRandy Dunlap  * Return: a bio representing the next @sectors of @bio - if the bio is smaller
33620d0189bSKent Overstreet  * than @sectors, returns the original bio unchanged.
33720d0189bSKent Overstreet  */
bio_next_split(struct bio * bio,int sectors,gfp_t gfp,struct bio_set * bs)33820d0189bSKent Overstreet static inline struct bio *bio_next_split(struct bio *bio, int sectors,
33920d0189bSKent Overstreet 					 gfp_t gfp, struct bio_set *bs)
34020d0189bSKent Overstreet {
34120d0189bSKent Overstreet 	if (sectors >= bio_sectors(bio))
34220d0189bSKent Overstreet 		return bio;
34320d0189bSKent Overstreet 
34420d0189bSKent Overstreet 	return bio_split(bio, sectors, gfp, bs);
34520d0189bSKent Overstreet }
34620d0189bSKent Overstreet 
347011067b0SNeilBrown enum {
348011067b0SNeilBrown 	BIOSET_NEED_BVECS = BIT(0),
34947e0fb46SNeilBrown 	BIOSET_NEED_RESCUER = BIT(1),
350be4d234dSJens Axboe 	BIOSET_PERCPU_CACHE = BIT(2),
351011067b0SNeilBrown };
352dad08527SKent Overstreet extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
353dad08527SKent Overstreet extern void bioset_exit(struct bio_set *);
3548aa6ba2fSKent Overstreet extern int biovec_init_pool(mempool_t *pool, int pool_entries);
3551da177e4SLinus Torvalds 
356609be106SChristoph Hellwig struct bio *bio_alloc_bioset(struct block_device *bdev, unsigned short nr_vecs,
35716458cf3SBart Van Assche 			     blk_opf_t opf, gfp_t gfp_mask,
3580f2e6ab8SChristoph Hellwig 			     struct bio_set *bs);
359066ff571SChristoph Hellwig struct bio *bio_kmalloc(unsigned short nr_vecs, gfp_t gfp_mask);
3601da177e4SLinus Torvalds extern void bio_put(struct bio *);
3611da177e4SLinus Torvalds 
362abfc426dSChristoph Hellwig struct bio *bio_alloc_clone(struct block_device *bdev, struct bio *bio_src,
363abfc426dSChristoph Hellwig 		gfp_t gfp, struct bio_set *bs);
364abfc426dSChristoph Hellwig int bio_init_clone(struct block_device *bdev, struct bio *bio,
365abfc426dSChristoph Hellwig 		struct bio *bio_src, gfp_t gfp);
366bf800ef1SKent Overstreet 
367f4f8154aSKent Overstreet extern struct bio_set fs_bio_set;
3683f86a82aSKent Overstreet 
bio_alloc(struct block_device * bdev,unsigned short nr_vecs,blk_opf_t opf,gfp_t gfp_mask)36907888c66SChristoph Hellwig static inline struct bio *bio_alloc(struct block_device *bdev,
37016458cf3SBart Van Assche 		unsigned short nr_vecs, blk_opf_t opf, gfp_t gfp_mask)
3713f86a82aSKent Overstreet {
37207888c66SChristoph Hellwig 	return bio_alloc_bioset(bdev, nr_vecs, opf, gfp_mask, &fs_bio_set);
3733f86a82aSKent Overstreet }
3743f86a82aSKent Overstreet 
3753e08773cSChristoph Hellwig void submit_bio(struct bio *bio);
3761e3914d4SChristoph Hellwig 
3774246a0b6SChristoph Hellwig extern void bio_endio(struct bio *);
3784246a0b6SChristoph Hellwig 
bio_io_error(struct bio * bio)3794246a0b6SChristoph Hellwig static inline void bio_io_error(struct bio *bio)
3804246a0b6SChristoph Hellwig {
3814e4cbee9SChristoph Hellwig 	bio->bi_status = BLK_STS_IOERR;
3824246a0b6SChristoph Hellwig 	bio_endio(bio);
3834246a0b6SChristoph Hellwig }
3844246a0b6SChristoph Hellwig 
bio_wouldblock_error(struct bio * bio)38503a07c92SGoldwyn Rodrigues static inline void bio_wouldblock_error(struct bio *bio)
38603a07c92SGoldwyn Rodrigues {
387abb30460SJens Axboe 	bio_set_flag(bio, BIO_QUIET);
38803a07c92SGoldwyn Rodrigues 	bio->bi_status = BLK_STS_AGAIN;
389bf800ef1SKent Overstreet 	bio_endio(bio);
390bf800ef1SKent Overstreet }
3916712ecf8SNeilBrown 
3923e1a88ecSPavel Begunkov /*
3933e1a88ecSPavel Begunkov  * Calculate number of bvec segments that should be allocated to fit data
394c42bca92SPavel Begunkov  * pointed by @iter. If @iter is backed by bvec it's going to be reused
395c42bca92SPavel Begunkov  * instead of allocating a new one.
3963e1a88ecSPavel Begunkov  */
bio_iov_vecs_to_alloc(struct iov_iter * iter,int max_segs)3973e1a88ecSPavel Begunkov static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
3983e1a88ecSPavel Begunkov {
399c42bca92SPavel Begunkov 	if (iov_iter_is_bvec(iter))
400c42bca92SPavel Begunkov 		return 0;
4013e1a88ecSPavel Begunkov 	return iov_iter_npages(iter, max_segs);
4023e1a88ecSPavel Begunkov }
4033e1a88ecSPavel Begunkov 
4041da177e4SLinus Torvalds struct request_queue;
4051da177e4SLinus Torvalds 
4064e49ea4aSMike Christie extern int submit_bio_wait(struct bio *bio);
40749add496SChristoph Hellwig void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
40816458cf3SBart Van Assche 	      unsigned short max_vecs, blk_opf_t opf);
4099ae3b3f5SJens Axboe extern void bio_uninit(struct bio *);
41016458cf3SBart Van Assche void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf);
411196d38bcSKent Overstreet void bio_chain(struct bio *, struct bio *);
4121da177e4SLinus Torvalds 
41383f2caaaSJohannes Thumshirn int __must_check bio_add_page(struct bio *bio, struct page *page, unsigned len,
41483f2caaaSJohannes Thumshirn 			      unsigned off);
4156c500000SJohannes Thumshirn bool __must_check bio_add_folio(struct bio *bio, struct folio *folio,
4166c500000SJohannes Thumshirn 				size_t len, size_t off);
4170aa69fd3SChristoph Hellwig void __bio_add_page(struct bio *bio, struct page *page,
4180aa69fd3SChristoph Hellwig 		unsigned int len, unsigned int off);
4197a150f1eSJohannes Thumshirn void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
4207a150f1eSJohannes Thumshirn 			  size_t off);
4212cefe4dbSKent Overstreet int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
4222f4873f9SJohn Garry void bio_iov_bvec_set(struct bio *bio, const struct iov_iter *iter);
423c809084aSPavel Begunkov void __bio_release_pages(struct bio *bio, bool mark_dirty);
4241da177e4SLinus Torvalds extern void bio_set_pages_dirty(struct bio *bio);
4251da177e4SLinus Torvalds extern void bio_check_pages_dirty(struct bio *bio);
4262d4dc890SIlya Loginov 
427ee4b4e22SJens Axboe extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
428ee4b4e22SJens Axboe 			       struct bio *src, struct bvec_iter *src_iter);
42916ac3d63SKent Overstreet extern void bio_copy_data(struct bio *dst, struct bio *src);
430491221f8SGuoqing Jiang extern void bio_free_pages(struct bio *bio);
43129125ed6SChristoph Hellwig void guard_bio_eod(struct bio *bio);
432649f070eSKent Overstreet void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
433649f070eSKent Overstreet 
zero_fill_bio(struct bio * bio)434649f070eSKent Overstreet static inline void zero_fill_bio(struct bio *bio)
435649f070eSKent Overstreet {
436649f070eSKent Overstreet 	zero_fill_bio_iter(bio, bio->bi_iter);
437649f070eSKent Overstreet }
43838a72dacSKent Overstreet 
bio_release_pages(struct bio * bio,bool mark_dirty)439c809084aSPavel Begunkov static inline void bio_release_pages(struct bio *bio, bool mark_dirty)
440c809084aSPavel Begunkov {
441e4cc6465SChristoph Hellwig 	if (bio_flagged(bio, BIO_PAGE_PINNED))
442c809084aSPavel Begunkov 		__bio_release_pages(bio, mark_dirty);
443c809084aSPavel Begunkov }
444c809084aSPavel Begunkov 
44574d46992SChristoph Hellwig #define bio_dev(bio) \
446309dca30SChristoph Hellwig 	disk_devt((bio)->bi_bdev->bd_disk)
44774d46992SChristoph Hellwig 
448852c788fSTejun Heo #ifdef CONFIG_BLK_CGROUP
4492268c0feSDennis Zhou void bio_associate_blkg(struct bio *bio);
450fd42df30SDennis Zhou void bio_associate_blkg_from_css(struct bio *bio,
451fd42df30SDennis Zhou 				 struct cgroup_subsys_state *css);
452db6638d7SDennis Zhou void bio_clone_blkg_association(struct bio *dst, struct bio *src);
4533480373eSChristoph Hellwig void blkcg_punt_bio_submit(struct bio *bio);
454852c788fSTejun Heo #else	/* CONFIG_BLK_CGROUP */
bio_associate_blkg(struct bio * bio)4552268c0feSDennis Zhou static inline void bio_associate_blkg(struct bio *bio) { }
bio_associate_blkg_from_css(struct bio * bio,struct cgroup_subsys_state * css)456fd42df30SDennis Zhou static inline void bio_associate_blkg_from_css(struct bio *bio,
457fd42df30SDennis Zhou 					       struct cgroup_subsys_state *css)
458fd42df30SDennis Zhou { }
bio_clone_blkg_association(struct bio * dst,struct bio * src)459db6638d7SDennis Zhou static inline void bio_clone_blkg_association(struct bio *dst,
46020bd723eSPaolo Valente 					      struct bio *src) { }
blkcg_punt_bio_submit(struct bio * bio)4613480373eSChristoph Hellwig static inline void blkcg_punt_bio_submit(struct bio *bio)
4623480373eSChristoph Hellwig {
4633480373eSChristoph Hellwig 	submit_bio(bio);
4643480373eSChristoph Hellwig }
465852c788fSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
466852c788fSTejun Heo 
bio_set_dev(struct bio * bio,struct block_device * bdev)467cf6d6238SPavel Begunkov static inline void bio_set_dev(struct bio *bio, struct block_device *bdev)
468cf6d6238SPavel Begunkov {
469cf6d6238SPavel Begunkov 	bio_clear_flag(bio, BIO_REMAPPED);
470cf6d6238SPavel Begunkov 	if (bio->bi_bdev != bdev)
471320fb0f9SYu Kuai 		bio_clear_flag(bio, BIO_BPS_THROTTLED);
472cf6d6238SPavel Begunkov 	bio->bi_bdev = bdev;
473cf6d6238SPavel Begunkov 	bio_associate_blkg(bio);
474cf6d6238SPavel Begunkov }
475cf6d6238SPavel Begunkov 
4767a67f63bSJens Axboe /*
477e686307fSAkinobu Mita  * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
4788f3d8ba2SChristoph Hellwig  *
4798f3d8ba2SChristoph Hellwig  * A bio_list anchors a singly-linked list of bios chained through the bi_next
4808f3d8ba2SChristoph Hellwig  * member of the bio.  The bio_list also caches the last list member to allow
4818f3d8ba2SChristoph Hellwig  * fast access to the tail.
4828f3d8ba2SChristoph Hellwig  */
4838f3d8ba2SChristoph Hellwig struct bio_list {
4848f3d8ba2SChristoph Hellwig 	struct bio *head;
4858f3d8ba2SChristoph Hellwig 	struct bio *tail;
4868f3d8ba2SChristoph Hellwig };
4878f3d8ba2SChristoph Hellwig 
bio_list_empty(const struct bio_list * bl)4888f3d8ba2SChristoph Hellwig static inline int bio_list_empty(const struct bio_list *bl)
4898f3d8ba2SChristoph Hellwig {
4908f3d8ba2SChristoph Hellwig 	return bl->head == NULL;
4918f3d8ba2SChristoph Hellwig }
4928f3d8ba2SChristoph Hellwig 
bio_list_init(struct bio_list * bl)4938f3d8ba2SChristoph Hellwig static inline void bio_list_init(struct bio_list *bl)
4948f3d8ba2SChristoph Hellwig {
4958f3d8ba2SChristoph Hellwig 	bl->head = bl->tail = NULL;
4968f3d8ba2SChristoph Hellwig }
4978f3d8ba2SChristoph Hellwig 
498320ae51fSJens Axboe #define BIO_EMPTY_LIST	{ NULL, NULL }
499320ae51fSJens Axboe 
5008f3d8ba2SChristoph Hellwig #define bio_list_for_each(bio, bl) \
5018f3d8ba2SChristoph Hellwig 	for (bio = (bl)->head; bio; bio = bio->bi_next)
5028f3d8ba2SChristoph Hellwig 
bio_list_size(const struct bio_list * bl)5038f3d8ba2SChristoph Hellwig static inline unsigned bio_list_size(const struct bio_list *bl)
5048f3d8ba2SChristoph Hellwig {
5058f3d8ba2SChristoph Hellwig 	unsigned sz = 0;
5068f3d8ba2SChristoph Hellwig 	struct bio *bio;
5078f3d8ba2SChristoph Hellwig 
5088f3d8ba2SChristoph Hellwig 	bio_list_for_each(bio, bl)
5098f3d8ba2SChristoph Hellwig 		sz++;
5108f3d8ba2SChristoph Hellwig 
5118f3d8ba2SChristoph Hellwig 	return sz;
5128f3d8ba2SChristoph Hellwig }
5138f3d8ba2SChristoph Hellwig 
bio_list_add(struct bio_list * bl,struct bio * bio)5148f3d8ba2SChristoph Hellwig static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
5158f3d8ba2SChristoph Hellwig {
5168f3d8ba2SChristoph Hellwig 	bio->bi_next = NULL;
5178f3d8ba2SChristoph Hellwig 
5188f3d8ba2SChristoph Hellwig 	if (bl->tail)
5198f3d8ba2SChristoph Hellwig 		bl->tail->bi_next = bio;
5208f3d8ba2SChristoph Hellwig 	else
5218f3d8ba2SChristoph Hellwig 		bl->head = bio;
5228f3d8ba2SChristoph Hellwig 
5238f3d8ba2SChristoph Hellwig 	bl->tail = bio;
5248f3d8ba2SChristoph Hellwig }
5258f3d8ba2SChristoph Hellwig 
bio_list_add_head(struct bio_list * bl,struct bio * bio)5268f3d8ba2SChristoph Hellwig static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
5278f3d8ba2SChristoph Hellwig {
5288f3d8ba2SChristoph Hellwig 	bio->bi_next = bl->head;
5298f3d8ba2SChristoph Hellwig 
5308f3d8ba2SChristoph Hellwig 	bl->head = bio;
5318f3d8ba2SChristoph Hellwig 
5328f3d8ba2SChristoph Hellwig 	if (!bl->tail)
5338f3d8ba2SChristoph Hellwig 		bl->tail = bio;
5348f3d8ba2SChristoph Hellwig }
5358f3d8ba2SChristoph Hellwig 
bio_list_merge(struct bio_list * bl,struct bio_list * bl2)5368f3d8ba2SChristoph Hellwig static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
5378f3d8ba2SChristoph Hellwig {
5388f3d8ba2SChristoph Hellwig 	if (!bl2->head)
5398f3d8ba2SChristoph Hellwig 		return;
5408f3d8ba2SChristoph Hellwig 
5418f3d8ba2SChristoph Hellwig 	if (bl->tail)
5428f3d8ba2SChristoph Hellwig 		bl->tail->bi_next = bl2->head;
5438f3d8ba2SChristoph Hellwig 	else
5448f3d8ba2SChristoph Hellwig 		bl->head = bl2->head;
5458f3d8ba2SChristoph Hellwig 
5468f3d8ba2SChristoph Hellwig 	bl->tail = bl2->tail;
5478f3d8ba2SChristoph Hellwig }
5488f3d8ba2SChristoph Hellwig 
bio_list_merge_init(struct bio_list * bl,struct bio_list * bl2)549c9418adfSChristoph Hellwig static inline void bio_list_merge_init(struct bio_list *bl,
550c9418adfSChristoph Hellwig 		struct bio_list *bl2)
551c9418adfSChristoph Hellwig {
552c9418adfSChristoph Hellwig 	bio_list_merge(bl, bl2);
553c9418adfSChristoph Hellwig 	bio_list_init(bl2);
554c9418adfSChristoph Hellwig }
555c9418adfSChristoph Hellwig 
bio_list_merge_head(struct bio_list * bl,struct bio_list * bl2)5568f3d8ba2SChristoph Hellwig static inline void bio_list_merge_head(struct bio_list *bl,
5578f3d8ba2SChristoph Hellwig 				       struct bio_list *bl2)
5588f3d8ba2SChristoph Hellwig {
5598f3d8ba2SChristoph Hellwig 	if (!bl2->head)
5608f3d8ba2SChristoph Hellwig 		return;
5618f3d8ba2SChristoph Hellwig 
5628f3d8ba2SChristoph Hellwig 	if (bl->head)
5638f3d8ba2SChristoph Hellwig 		bl2->tail->bi_next = bl->head;
5648f3d8ba2SChristoph Hellwig 	else
5658f3d8ba2SChristoph Hellwig 		bl->tail = bl2->tail;
5668f3d8ba2SChristoph Hellwig 
5678f3d8ba2SChristoph Hellwig 	bl->head = bl2->head;
5688f3d8ba2SChristoph Hellwig }
5698f3d8ba2SChristoph Hellwig 
bio_list_peek(struct bio_list * bl)57013685a16SGeert Uytterhoeven static inline struct bio *bio_list_peek(struct bio_list *bl)
57113685a16SGeert Uytterhoeven {
57213685a16SGeert Uytterhoeven 	return bl->head;
57313685a16SGeert Uytterhoeven }
57413685a16SGeert Uytterhoeven 
bio_list_pop(struct bio_list * bl)5758f3d8ba2SChristoph Hellwig static inline struct bio *bio_list_pop(struct bio_list *bl)
5768f3d8ba2SChristoph Hellwig {
5778f3d8ba2SChristoph Hellwig 	struct bio *bio = bl->head;
5788f3d8ba2SChristoph Hellwig 
5798f3d8ba2SChristoph Hellwig 	if (bio) {
5808f3d8ba2SChristoph Hellwig 		bl->head = bl->head->bi_next;
5818f3d8ba2SChristoph Hellwig 		if (!bl->head)
5828f3d8ba2SChristoph Hellwig 			bl->tail = NULL;
5838f3d8ba2SChristoph Hellwig 
5848f3d8ba2SChristoph Hellwig 		bio->bi_next = NULL;
5858f3d8ba2SChristoph Hellwig 	}
5868f3d8ba2SChristoph Hellwig 
5878f3d8ba2SChristoph Hellwig 	return bio;
5888f3d8ba2SChristoph Hellwig }
5898f3d8ba2SChristoph Hellwig 
bio_list_get(struct bio_list * bl)5908f3d8ba2SChristoph Hellwig static inline struct bio *bio_list_get(struct bio_list *bl)
5918f3d8ba2SChristoph Hellwig {
5928f3d8ba2SChristoph Hellwig 	struct bio *bio = bl->head;
5938f3d8ba2SChristoph Hellwig 
5948f3d8ba2SChristoph Hellwig 	bl->head = bl->tail = NULL;
5958f3d8ba2SChristoph Hellwig 
5968f3d8ba2SChristoph Hellwig 	return bio;
5978f3d8ba2SChristoph Hellwig }
5988f3d8ba2SChristoph Hellwig 
59957fb233fSKent Overstreet /*
6000ef5a50cSMike Snitzer  * Increment chain count for the bio. Make sure the CHAIN flag update
6010ef5a50cSMike Snitzer  * is visible before the raised count.
6020ef5a50cSMike Snitzer  */
bio_inc_remaining(struct bio * bio)6030ef5a50cSMike Snitzer static inline void bio_inc_remaining(struct bio *bio)
6040ef5a50cSMike Snitzer {
6050ef5a50cSMike Snitzer 	bio_set_flag(bio, BIO_CHAIN);
6060ef5a50cSMike Snitzer 	smp_mb__before_atomic();
6070ef5a50cSMike Snitzer 	atomic_inc(&bio->__bi_remaining);
6080ef5a50cSMike Snitzer }
6090ef5a50cSMike Snitzer 
6100ef5a50cSMike Snitzer /*
61157fb233fSKent Overstreet  * bio_set is used to allow other portions of the IO system to
61257fb233fSKent Overstreet  * allocate their own private memory pools for bio and iovec structures.
61357fb233fSKent Overstreet  * These memory pools in turn all allocate from the bio_slab
61457fb233fSKent Overstreet  * and the bvec_slabs[].
61557fb233fSKent Overstreet  */
61657fb233fSKent Overstreet #define BIO_POOL_SIZE 2
61757fb233fSKent Overstreet 
61857fb233fSKent Overstreet struct bio_set {
61957fb233fSKent Overstreet 	struct kmem_cache *bio_slab;
62057fb233fSKent Overstreet 	unsigned int front_pad;
62157fb233fSKent Overstreet 
622be4d234dSJens Axboe 	/*
623be4d234dSJens Axboe 	 * per-cpu bio alloc cache
624be4d234dSJens Axboe 	 */
625be4d234dSJens Axboe 	struct bio_alloc_cache __percpu *cache;
626be4d234dSJens Axboe 
6278aa6ba2fSKent Overstreet 	mempool_t bio_pool;
6288aa6ba2fSKent Overstreet 	mempool_t bvec_pool;
629df2cb6daSKent Overstreet 
6309f180e31SMing Lei 	unsigned int back_pad;
631df2cb6daSKent Overstreet 	/*
632df2cb6daSKent Overstreet 	 * Deadlock avoidance for stacking block drivers: see comments in
633df2cb6daSKent Overstreet 	 * bio_alloc_bioset() for details
634df2cb6daSKent Overstreet 	 */
635df2cb6daSKent Overstreet 	spinlock_t		rescue_lock;
636df2cb6daSKent Overstreet 	struct bio_list		rescue_list;
637df2cb6daSKent Overstreet 	struct work_struct	rescue_work;
638df2cb6daSKent Overstreet 	struct workqueue_struct	*rescue_workqueue;
639be4d234dSJens Axboe 
640be4d234dSJens Axboe 	/*
641be4d234dSJens Axboe 	 * Hot un-plug notifier for the per-cpu cache, if used
642be4d234dSJens Axboe 	 */
643be4d234dSJens Axboe 	struct hlist_node cpuhp_dead;
64457fb233fSKent Overstreet };
64557fb233fSKent Overstreet 
bioset_initialized(struct bio_set * bs)646338aa96dSKent Overstreet static inline bool bioset_initialized(struct bio_set *bs)
647338aa96dSKent Overstreet {
648338aa96dSKent Overstreet 	return bs->bio_slab != NULL;
649338aa96dSKent Overstreet }
650338aa96dSKent Overstreet 
6510bbb280dSJens Axboe /*
6520bbb280dSJens Axboe  * Mark a bio as polled. Note that for async polled IO, the caller must
6530bbb280dSJens Axboe  * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
6540bbb280dSJens Axboe  * We cannot block waiting for requests on polled IO, as those completions
6550bbb280dSJens Axboe  * must be found by the caller. This is different than IRQ driven IO, where
6560bbb280dSJens Axboe  * it's safe to wait for IO to complete.
6570bbb280dSJens Axboe  */
bio_set_polled(struct bio * bio,struct kiocb * kiocb)6580bbb280dSJens Axboe static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
6590bbb280dSJens Axboe {
6606ce913feSChristoph Hellwig 	bio->bi_opf |= REQ_POLLED;
6612bc05769SJens Axboe 	if (kiocb->ki_flags & IOCB_NOWAIT)
6620bbb280dSJens Axboe 		bio->bi_opf |= REQ_NOWAIT;
6630bbb280dSJens Axboe }
6640bbb280dSJens Axboe 
bio_clear_polled(struct bio * bio)665b53f3dcdSMike Snitzer static inline void bio_clear_polled(struct bio *bio)
666b53f3dcdSMike Snitzer {
66753eab8e7SJens Axboe 	bio->bi_opf &= ~REQ_POLLED;
668b53f3dcdSMike Snitzer }
669b53f3dcdSMike Snitzer 
6700ef2b9e6SChristoph Hellwig /**
6710ef2b9e6SChristoph Hellwig  * bio_is_zone_append - is this a zone append bio?
6720ef2b9e6SChristoph Hellwig  * @bio:	bio to check
6730ef2b9e6SChristoph Hellwig  *
6740ef2b9e6SChristoph Hellwig  * Check if @bio is a zone append operation.  Core block layer code and end_io
6750ef2b9e6SChristoph Hellwig  * handlers must use this instead of an open coded REQ_OP_ZONE_APPEND check
6760ef2b9e6SChristoph Hellwig  * because the block layer can rewrite REQ_OP_ZONE_APPEND to REQ_OP_WRITE if
6770ef2b9e6SChristoph Hellwig  * it is not natively supported.
6780ef2b9e6SChristoph Hellwig  */
bio_is_zone_append(struct bio * bio)6790ef2b9e6SChristoph Hellwig static inline bool bio_is_zone_append(struct bio *bio)
6800ef2b9e6SChristoph Hellwig {
6810ef2b9e6SChristoph Hellwig 	if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED))
6820ef2b9e6SChristoph Hellwig 		return false;
6830ef2b9e6SChristoph Hellwig 	return bio_op(bio) == REQ_OP_ZONE_APPEND ||
6840ef2b9e6SChristoph Hellwig 		bio_flagged(bio, BIO_EMULATES_ZONE_APPEND);
6850ef2b9e6SChristoph Hellwig }
6860ef2b9e6SChristoph Hellwig 
6870a3140eaSChaitanya Kulkarni struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev,
68816458cf3SBart Van Assche 		unsigned int nr_pages, blk_opf_t opf, gfp_t gfp);
68981c2168cSChristoph Hellwig struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new);
690c28a6147SChaitanya Kulkarni 
691e8b4869bSChristoph Hellwig struct bio *blk_alloc_discard_bio(struct block_device *bdev,
692e8b4869bSChristoph Hellwig 		sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask);
693e8b4869bSChristoph Hellwig 
6941da177e4SLinus Torvalds #endif /* __LINUX_BIO_H */
695