1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
8 #include <linux/mempool.h>
9 #include <linux/ioprio.h>
10 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
11 #include <linux/blk_types.h>
12 #include <linux/uio.h>
17 #define BIO_BUG_ON BUG_ON
22 #define BIO_MAX_VECS 256U
24 static inline unsigned int bio_max_segs(unsigned int nr_segs)
26 return min(nr_segs, BIO_MAX_VECS);
29 #define bio_prio(bio) (bio)->bi_ioprio
30 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
32 #define bio_iter_iovec(bio, iter) \
33 bvec_iter_bvec((bio)->bi_io_vec, (iter))
35 #define bio_iter_page(bio, iter) \
36 bvec_iter_page((bio)->bi_io_vec, (iter))
37 #define bio_iter_len(bio, iter) \
38 bvec_iter_len((bio)->bi_io_vec, (iter))
39 #define bio_iter_offset(bio, iter) \
40 bvec_iter_offset((bio)->bi_io_vec, (iter))
42 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
43 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
44 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
46 #define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
47 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
49 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
50 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
53 * Return the data direction, READ or WRITE.
55 #define bio_data_dir(bio) \
56 (op_is_write(bio_op(bio)) ? WRITE : READ)
59 * Check whether this bio carries any data or not. A NULL bio is allowed.
61 static inline bool bio_has_data(struct bio *bio)
64 bio->bi_iter.bi_size &&
65 bio_op(bio) != REQ_OP_DISCARD &&
66 bio_op(bio) != REQ_OP_SECURE_ERASE &&
67 bio_op(bio) != REQ_OP_WRITE_ZEROES)
73 static inline bool bio_no_advance_iter(const struct bio *bio)
75 return bio_op(bio) == REQ_OP_DISCARD ||
76 bio_op(bio) == REQ_OP_SECURE_ERASE ||
77 bio_op(bio) == REQ_OP_WRITE_SAME ||
78 bio_op(bio) == REQ_OP_WRITE_ZEROES;
81 static inline bool bio_mergeable(struct bio *bio)
83 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
89 static inline unsigned int bio_cur_bytes(struct bio *bio)
91 if (bio_has_data(bio))
92 return bio_iovec(bio).bv_len;
93 else /* dataless requests such as discard */
94 return bio->bi_iter.bi_size;
97 static inline void *bio_data(struct bio *bio)
99 if (bio_has_data(bio))
100 return page_address(bio_page(bio)) + bio_offset(bio);
106 * bio_full - check if the bio is full
108 * @len: length of one segment to be added
110 * Return true if @bio is full and one segment with @len bytes can't be
111 * added to the bio, otherwise return false
113 static inline bool bio_full(struct bio *bio, unsigned len)
115 if (bio->bi_vcnt >= bio->bi_max_vecs)
118 if (bio->bi_iter.bi_size > UINT_MAX - len)
124 static inline bool bio_next_segment(const struct bio *bio,
125 struct bvec_iter_all *iter)
127 if (iter->idx >= bio->bi_vcnt)
130 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
135 * drivers should _never_ use the all version - the bio may have been split
136 * before it got to the driver and the driver won't own all of it
138 #define bio_for_each_segment_all(bvl, bio, iter) \
139 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
141 static inline void bio_advance_iter(const struct bio *bio,
142 struct bvec_iter *iter, unsigned int bytes)
144 iter->bi_sector += bytes >> 9;
146 if (bio_no_advance_iter(bio))
147 iter->bi_size -= bytes;
149 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
150 /* TODO: It is reasonable to complete bio with error here. */
153 /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
154 static inline void bio_advance_iter_single(const struct bio *bio,
155 struct bvec_iter *iter,
158 iter->bi_sector += bytes >> 9;
160 if (bio_no_advance_iter(bio))
161 iter->bi_size -= bytes;
163 bvec_iter_advance_single(bio->bi_io_vec, iter, bytes);
166 #define __bio_for_each_segment(bvl, bio, iter, start) \
167 for (iter = (start); \
169 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
170 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
172 #define bio_for_each_segment(bvl, bio, iter) \
173 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
175 #define __bio_for_each_bvec(bvl, bio, iter, start) \
176 for (iter = (start); \
178 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
179 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
181 /* iterate over multi-page bvec */
182 #define bio_for_each_bvec(bvl, bio, iter) \
183 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
186 * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
187 * same reasons as bio_for_each_segment_all().
189 #define bio_for_each_bvec_all(bvl, bio, i) \
190 for (i = 0, bvl = bio_first_bvec_all(bio); \
191 i < (bio)->bi_vcnt; i++, bvl++) \
193 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
195 static inline unsigned bio_segments(struct bio *bio)
199 struct bvec_iter iter;
202 * We special case discard/write same/write zeroes, because they
203 * interpret bi_size differently:
206 switch (bio_op(bio)) {
208 case REQ_OP_SECURE_ERASE:
209 case REQ_OP_WRITE_ZEROES:
211 case REQ_OP_WRITE_SAME:
217 bio_for_each_segment(bv, bio, iter)
224 * get a reference to a bio, so it won't disappear. the intended use is
228 * submit_bio(rw, bio);
229 * if (bio->bi_flags ...)
233 * without the bio_get(), it could potentially complete I/O before submit_bio
234 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
237 static inline void bio_get(struct bio *bio)
239 bio->bi_flags |= (1 << BIO_REFFED);
240 smp_mb__before_atomic();
241 atomic_inc(&bio->__bi_cnt);
244 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
247 bio->bi_flags |= (1 << BIO_REFFED);
250 atomic_set(&bio->__bi_cnt, count);
253 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
255 return (bio->bi_flags & (1U << bit)) != 0;
258 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
260 bio->bi_flags |= (1U << bit);
263 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
265 bio->bi_flags &= ~(1U << bit);
268 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
270 *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
273 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
275 struct bvec_iter iter = bio->bi_iter;
278 bio_get_first_bvec(bio, bv);
279 if (bv->bv_len == bio->bi_iter.bi_size)
280 return; /* this bio only has a single bvec */
282 bio_advance_iter(bio, &iter, iter.bi_size);
284 if (!iter.bi_bvec_done)
285 idx = iter.bi_idx - 1;
286 else /* in the middle of bvec */
289 *bv = bio->bi_io_vec[idx];
292 * iter.bi_bvec_done records actual length of the last bvec
293 * if this bio ends in the middle of one io vector
295 if (iter.bi_bvec_done)
296 bv->bv_len = iter.bi_bvec_done;
299 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
301 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
302 return bio->bi_io_vec;
305 static inline struct page *bio_first_page_all(struct bio *bio)
307 return bio_first_bvec_all(bio)->bv_page;
310 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
312 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
313 return &bio->bi_io_vec[bio->bi_vcnt - 1];
317 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
318 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
319 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
320 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
321 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
325 * bio integrity payload
327 struct bio_integrity_payload {
328 struct bio *bip_bio; /* parent bio */
330 struct bvec_iter bip_iter;
332 unsigned short bip_vcnt; /* # of integrity bio_vecs */
333 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
334 unsigned short bip_flags; /* control flags */
336 struct bvec_iter bio_iter; /* for rewinding parent bio */
338 struct work_struct bip_work; /* I/O completion */
340 struct bio_vec *bip_vec;
341 struct bio_vec bip_inline_vecs[];/* embedded bvec array */
344 #if defined(CONFIG_BLK_DEV_INTEGRITY)
346 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
348 if (bio->bi_opf & REQ_INTEGRITY)
349 return bio->bi_integrity;
354 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
356 struct bio_integrity_payload *bip = bio_integrity(bio);
359 return bip->bip_flags & flag;
364 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
366 return bip->bip_iter.bi_sector;
369 static inline void bip_set_seed(struct bio_integrity_payload *bip,
372 bip->bip_iter.bi_sector = seed;
375 #endif /* CONFIG_BLK_DEV_INTEGRITY */
377 void bio_trim(struct bio *bio, sector_t offset, sector_t size);
378 extern struct bio *bio_split(struct bio *bio, int sectors,
379 gfp_t gfp, struct bio_set *bs);
382 * bio_next_split - get next @sectors from a bio, splitting if necessary
384 * @sectors: number of sectors to split from the front of @bio
386 * @bs: bio set to allocate from
388 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
389 * than @sectors, returns the original bio unchanged.
391 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
392 gfp_t gfp, struct bio_set *bs)
394 if (sectors >= bio_sectors(bio))
397 return bio_split(bio, sectors, gfp, bs);
401 BIOSET_NEED_BVECS = BIT(0),
402 BIOSET_NEED_RESCUER = BIT(1),
403 BIOSET_PERCPU_CACHE = BIT(2),
405 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
406 extern void bioset_exit(struct bio_set *);
407 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
408 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
410 struct bio *bio_alloc_bioset(gfp_t gfp, unsigned short nr_iovecs,
412 struct bio *bio_alloc_kiocb(struct kiocb *kiocb, unsigned short nr_vecs,
414 struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
415 extern void bio_put(struct bio *);
417 extern void __bio_clone_fast(struct bio *, struct bio *);
418 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
420 extern struct bio_set fs_bio_set;
422 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned short nr_iovecs)
424 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
427 extern blk_qc_t submit_bio(struct bio *);
429 extern void bio_endio(struct bio *);
431 static inline void bio_io_error(struct bio *bio)
433 bio->bi_status = BLK_STS_IOERR;
437 static inline void bio_wouldblock_error(struct bio *bio)
439 bio_set_flag(bio, BIO_QUIET);
440 bio->bi_status = BLK_STS_AGAIN;
445 * Calculate number of bvec segments that should be allocated to fit data
446 * pointed by @iter. If @iter is backed by bvec it's going to be reused
447 * instead of allocating a new one.
449 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
451 if (iov_iter_is_bvec(iter))
453 return iov_iter_npages(iter, max_segs);
456 struct request_queue;
458 extern int submit_bio_wait(struct bio *bio);
459 extern void bio_advance(struct bio *, unsigned);
461 extern void bio_init(struct bio *bio, struct bio_vec *table,
462 unsigned short max_vecs);
463 extern void bio_uninit(struct bio *);
464 extern void bio_reset(struct bio *);
465 void bio_chain(struct bio *, struct bio *);
467 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
468 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
469 unsigned int, unsigned int);
470 int bio_add_zone_append_page(struct bio *bio, struct page *page,
471 unsigned int len, unsigned int offset);
472 bool __bio_try_merge_page(struct bio *bio, struct page *page,
473 unsigned int len, unsigned int off, bool *same_page);
474 void __bio_add_page(struct bio *bio, struct page *page,
475 unsigned int len, unsigned int off);
476 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
477 void bio_release_pages(struct bio *bio, bool mark_dirty);
478 extern void bio_set_pages_dirty(struct bio *bio);
479 extern void bio_check_pages_dirty(struct bio *bio);
481 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
482 struct bio *src, struct bvec_iter *src_iter);
483 extern void bio_copy_data(struct bio *dst, struct bio *src);
484 extern void bio_free_pages(struct bio *bio);
485 void bio_truncate(struct bio *bio, unsigned new_size);
486 void guard_bio_eod(struct bio *bio);
487 void zero_fill_bio(struct bio *bio);
489 extern const char *bio_devname(struct bio *bio, char *buffer);
491 #define bio_set_dev(bio, bdev) \
493 bio_clear_flag(bio, BIO_REMAPPED); \
494 if ((bio)->bi_bdev != (bdev)) \
495 bio_clear_flag(bio, BIO_THROTTLED); \
496 (bio)->bi_bdev = (bdev); \
497 bio_associate_blkg(bio); \
500 #define bio_copy_dev(dst, src) \
502 bio_clear_flag(dst, BIO_REMAPPED); \
503 (dst)->bi_bdev = (src)->bi_bdev; \
504 bio_clone_blkg_association(dst, src); \
507 #define bio_dev(bio) \
508 disk_devt((bio)->bi_bdev->bd_disk)
510 #ifdef CONFIG_BLK_CGROUP
511 void bio_associate_blkg(struct bio *bio);
512 void bio_associate_blkg_from_css(struct bio *bio,
513 struct cgroup_subsys_state *css);
514 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
515 #else /* CONFIG_BLK_CGROUP */
516 static inline void bio_associate_blkg(struct bio *bio) { }
517 static inline void bio_associate_blkg_from_css(struct bio *bio,
518 struct cgroup_subsys_state *css)
520 static inline void bio_clone_blkg_association(struct bio *dst,
522 #endif /* CONFIG_BLK_CGROUP */
525 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
527 * A bio_list anchors a singly-linked list of bios chained through the bi_next
528 * member of the bio. The bio_list also caches the last list member to allow
529 * fast access to the tail.
536 static inline int bio_list_empty(const struct bio_list *bl)
538 return bl->head == NULL;
541 static inline void bio_list_init(struct bio_list *bl)
543 bl->head = bl->tail = NULL;
546 #define BIO_EMPTY_LIST { NULL, NULL }
548 #define bio_list_for_each(bio, bl) \
549 for (bio = (bl)->head; bio; bio = bio->bi_next)
551 static inline unsigned bio_list_size(const struct bio_list *bl)
556 bio_list_for_each(bio, bl)
562 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
567 bl->tail->bi_next = bio;
574 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
576 bio->bi_next = bl->head;
584 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
590 bl->tail->bi_next = bl2->head;
592 bl->head = bl2->head;
594 bl->tail = bl2->tail;
597 static inline void bio_list_merge_head(struct bio_list *bl,
598 struct bio_list *bl2)
604 bl2->tail->bi_next = bl->head;
606 bl->tail = bl2->tail;
608 bl->head = bl2->head;
611 static inline struct bio *bio_list_peek(struct bio_list *bl)
616 static inline struct bio *bio_list_pop(struct bio_list *bl)
618 struct bio *bio = bl->head;
621 bl->head = bl->head->bi_next;
631 static inline struct bio *bio_list_get(struct bio_list *bl)
633 struct bio *bio = bl->head;
635 bl->head = bl->tail = NULL;
641 * Increment chain count for the bio. Make sure the CHAIN flag update
642 * is visible before the raised count.
644 static inline void bio_inc_remaining(struct bio *bio)
646 bio_set_flag(bio, BIO_CHAIN);
647 smp_mb__before_atomic();
648 atomic_inc(&bio->__bi_remaining);
652 * bio_set is used to allow other portions of the IO system to
653 * allocate their own private memory pools for bio and iovec structures.
654 * These memory pools in turn all allocate from the bio_slab
655 * and the bvec_slabs[].
657 #define BIO_POOL_SIZE 2
660 struct kmem_cache *bio_slab;
661 unsigned int front_pad;
664 * per-cpu bio alloc cache
666 struct bio_alloc_cache __percpu *cache;
670 #if defined(CONFIG_BLK_DEV_INTEGRITY)
671 mempool_t bio_integrity_pool;
672 mempool_t bvec_integrity_pool;
675 unsigned int back_pad;
677 * Deadlock avoidance for stacking block drivers: see comments in
678 * bio_alloc_bioset() for details
680 spinlock_t rescue_lock;
681 struct bio_list rescue_list;
682 struct work_struct rescue_work;
683 struct workqueue_struct *rescue_workqueue;
686 * Hot un-plug notifier for the per-cpu cache, if used
688 struct hlist_node cpuhp_dead;
691 static inline bool bioset_initialized(struct bio_set *bs)
693 return bs->bio_slab != NULL;
696 #if defined(CONFIG_BLK_DEV_INTEGRITY)
698 #define bip_for_each_vec(bvl, bip, iter) \
699 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
701 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
703 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
705 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
706 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
707 extern bool bio_integrity_prep(struct bio *);
708 extern void bio_integrity_advance(struct bio *, unsigned int);
709 extern void bio_integrity_trim(struct bio *);
710 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
711 extern int bioset_integrity_create(struct bio_set *, int);
712 extern void bioset_integrity_free(struct bio_set *);
713 extern void bio_integrity_init(void);
715 #else /* CONFIG_BLK_DEV_INTEGRITY */
717 static inline void *bio_integrity(struct bio *bio)
722 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
727 static inline void bioset_integrity_free (struct bio_set *bs)
732 static inline bool bio_integrity_prep(struct bio *bio)
737 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
743 static inline void bio_integrity_advance(struct bio *bio,
744 unsigned int bytes_done)
749 static inline void bio_integrity_trim(struct bio *bio)
754 static inline void bio_integrity_init(void)
759 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
764 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
767 return ERR_PTR(-EINVAL);
770 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
771 unsigned int len, unsigned int offset)
776 #endif /* CONFIG_BLK_DEV_INTEGRITY */
779 * Mark a bio as polled. Note that for async polled IO, the caller must
780 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
781 * We cannot block waiting for requests on polled IO, as those completions
782 * must be found by the caller. This is different than IRQ driven IO, where
783 * it's safe to wait for IO to complete.
785 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
787 bio->bi_opf |= REQ_HIPRI;
788 if (!is_sync_kiocb(kiocb))
789 bio->bi_opf |= REQ_NOWAIT;
792 struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
794 #endif /* __LINUX_BIO_H */