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