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