1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
8 #include <linux/highmem.h>
9 #include <linux/mempool.h>
10 #include <linux/ioprio.h>
11 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
12 #include <linux/blk_types.h>
13 #include <linux/uio.h>
18 #define BIO_BUG_ON BUG_ON
23 #define BIO_MAX_VECS 256U
25 static inline unsigned int bio_max_segs(unsigned int nr_segs)
27 return min(nr_segs, BIO_MAX_VECS);
30 #define bio_prio(bio) (bio)->bi_ioprio
31 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
33 #define bio_iter_iovec(bio, iter) \
34 bvec_iter_bvec((bio)->bi_io_vec, (iter))
36 #define bio_iter_page(bio, iter) \
37 bvec_iter_page((bio)->bi_io_vec, (iter))
38 #define bio_iter_len(bio, iter) \
39 bvec_iter_len((bio)->bi_io_vec, (iter))
40 #define bio_iter_offset(bio, iter) \
41 bvec_iter_offset((bio)->bi_io_vec, (iter))
43 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
44 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
45 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
47 #define bio_multiple_segments(bio) \
48 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
50 #define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
51 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
53 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
54 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
57 * Return the data direction, READ or WRITE.
59 #define bio_data_dir(bio) \
60 (op_is_write(bio_op(bio)) ? WRITE : READ)
63 * Check whether this bio carries any data or not. A NULL bio is allowed.
65 static inline bool bio_has_data(struct bio *bio)
68 bio->bi_iter.bi_size &&
69 bio_op(bio) != REQ_OP_DISCARD &&
70 bio_op(bio) != REQ_OP_SECURE_ERASE &&
71 bio_op(bio) != REQ_OP_WRITE_ZEROES)
77 static inline bool bio_no_advance_iter(const struct bio *bio)
79 return bio_op(bio) == REQ_OP_DISCARD ||
80 bio_op(bio) == REQ_OP_SECURE_ERASE ||
81 bio_op(bio) == REQ_OP_WRITE_SAME ||
82 bio_op(bio) == REQ_OP_WRITE_ZEROES;
85 static inline bool bio_mergeable(struct bio *bio)
87 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
93 static inline unsigned int bio_cur_bytes(struct bio *bio)
95 if (bio_has_data(bio))
96 return bio_iovec(bio).bv_len;
97 else /* dataless requests such as discard */
98 return bio->bi_iter.bi_size;
101 static inline void *bio_data(struct bio *bio)
103 if (bio_has_data(bio))
104 return page_address(bio_page(bio)) + bio_offset(bio);
110 * bio_full - check if the bio is full
112 * @len: length of one segment to be added
114 * Return true if @bio is full and one segment with @len bytes can't be
115 * added to the bio, otherwise return false
117 static inline bool bio_full(struct bio *bio, unsigned len)
119 if (bio->bi_vcnt >= bio->bi_max_vecs)
122 if (bio->bi_iter.bi_size > UINT_MAX - len)
128 static inline bool bio_next_segment(const struct bio *bio,
129 struct bvec_iter_all *iter)
131 if (iter->idx >= bio->bi_vcnt)
134 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
139 * drivers should _never_ use the all version - the bio may have been split
140 * before it got to the driver and the driver won't own all of it
142 #define bio_for_each_segment_all(bvl, bio, iter) \
143 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
145 static inline void bio_advance_iter(const struct bio *bio,
146 struct bvec_iter *iter, unsigned int bytes)
148 iter->bi_sector += bytes >> 9;
150 if (bio_no_advance_iter(bio))
151 iter->bi_size -= bytes;
153 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
154 /* TODO: It is reasonable to complete bio with error here. */
157 /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
158 static inline void bio_advance_iter_single(const struct bio *bio,
159 struct bvec_iter *iter,
162 iter->bi_sector += bytes >> 9;
164 if (bio_no_advance_iter(bio))
165 iter->bi_size -= bytes;
167 bvec_iter_advance_single(bio->bi_io_vec, iter, bytes);
170 #define __bio_for_each_segment(bvl, bio, iter, start) \
171 for (iter = (start); \
173 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
174 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
176 #define bio_for_each_segment(bvl, bio, iter) \
177 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
179 #define __bio_for_each_bvec(bvl, bio, iter, start) \
180 for (iter = (start); \
182 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
183 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
185 /* iterate over multi-page bvec */
186 #define bio_for_each_bvec(bvl, bio, iter) \
187 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
190 * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
191 * same reasons as bio_for_each_segment_all().
193 #define bio_for_each_bvec_all(bvl, bio, i) \
194 for (i = 0, bvl = bio_first_bvec_all(bio); \
195 i < (bio)->bi_vcnt; i++, bvl++) \
197 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
199 static inline unsigned bio_segments(struct bio *bio)
203 struct bvec_iter iter;
206 * We special case discard/write same/write zeroes, because they
207 * interpret bi_size differently:
210 switch (bio_op(bio)) {
212 case REQ_OP_SECURE_ERASE:
213 case REQ_OP_WRITE_ZEROES:
215 case REQ_OP_WRITE_SAME:
221 bio_for_each_segment(bv, bio, iter)
228 * get a reference to a bio, so it won't disappear. the intended use is
232 * submit_bio(rw, bio);
233 * if (bio->bi_flags ...)
237 * without the bio_get(), it could potentially complete I/O before submit_bio
238 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
241 static inline void bio_get(struct bio *bio)
243 bio->bi_flags |= (1 << BIO_REFFED);
244 smp_mb__before_atomic();
245 atomic_inc(&bio->__bi_cnt);
248 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
251 bio->bi_flags |= (1 << BIO_REFFED);
254 atomic_set(&bio->__bi_cnt, count);
257 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
259 return (bio->bi_flags & (1U << bit)) != 0;
262 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
264 bio->bi_flags |= (1U << bit);
267 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
269 bio->bi_flags &= ~(1U << bit);
272 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
274 *bv = bio_iovec(bio);
277 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
279 struct bvec_iter iter = bio->bi_iter;
282 if (unlikely(!bio_multiple_segments(bio))) {
283 *bv = bio_iovec(bio);
287 bio_advance_iter(bio, &iter, iter.bi_size);
289 if (!iter.bi_bvec_done)
290 idx = iter.bi_idx - 1;
291 else /* in the middle of bvec */
294 *bv = bio->bi_io_vec[idx];
297 * iter.bi_bvec_done records actual length of the last bvec
298 * if this bio ends in the middle of one io vector
300 if (iter.bi_bvec_done)
301 bv->bv_len = iter.bi_bvec_done;
304 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
306 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
307 return bio->bi_io_vec;
310 static inline struct page *bio_first_page_all(struct bio *bio)
312 return bio_first_bvec_all(bio)->bv_page;
315 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
317 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
318 return &bio->bi_io_vec[bio->bi_vcnt - 1];
322 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
323 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
324 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
325 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
326 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
330 * bio integrity payload
332 struct bio_integrity_payload {
333 struct bio *bip_bio; /* parent bio */
335 struct bvec_iter bip_iter;
337 unsigned short bip_vcnt; /* # of integrity bio_vecs */
338 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
339 unsigned short bip_flags; /* control flags */
341 struct bvec_iter bio_iter; /* for rewinding parent bio */
343 struct work_struct bip_work; /* I/O completion */
345 struct bio_vec *bip_vec;
346 struct bio_vec bip_inline_vecs[];/* embedded bvec array */
349 #if defined(CONFIG_BLK_DEV_INTEGRITY)
351 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
353 if (bio->bi_opf & REQ_INTEGRITY)
354 return bio->bi_integrity;
359 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
361 struct bio_integrity_payload *bip = bio_integrity(bio);
364 return bip->bip_flags & flag;
369 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
371 return bip->bip_iter.bi_sector;
374 static inline void bip_set_seed(struct bio_integrity_payload *bip,
377 bip->bip_iter.bi_sector = seed;
380 #endif /* CONFIG_BLK_DEV_INTEGRITY */
382 extern void bio_trim(struct bio *bio, int offset, int size);
383 extern struct bio *bio_split(struct bio *bio, int sectors,
384 gfp_t gfp, struct bio_set *bs);
387 * bio_next_split - get next @sectors from a bio, splitting if necessary
389 * @sectors: number of sectors to split from the front of @bio
391 * @bs: bio set to allocate from
393 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
394 * than @sectors, returns the original bio unchanged.
396 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
397 gfp_t gfp, struct bio_set *bs)
399 if (sectors >= bio_sectors(bio))
402 return bio_split(bio, sectors, gfp, bs);
406 BIOSET_NEED_BVECS = BIT(0),
407 BIOSET_NEED_RESCUER = BIT(1),
409 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
410 extern void bioset_exit(struct bio_set *);
411 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
412 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
414 struct bio *bio_alloc_bioset(gfp_t gfp, unsigned short nr_iovecs,
416 struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
417 extern void bio_put(struct bio *);
419 extern void __bio_clone_fast(struct bio *, struct bio *);
420 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
422 extern struct bio_set fs_bio_set;
424 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned short nr_iovecs)
426 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
429 extern blk_qc_t submit_bio(struct bio *);
431 extern void bio_endio(struct bio *);
433 static inline void bio_io_error(struct bio *bio)
435 bio->bi_status = BLK_STS_IOERR;
439 static inline void bio_wouldblock_error(struct bio *bio)
441 bio_set_flag(bio, BIO_QUIET);
442 bio->bi_status = BLK_STS_AGAIN;
447 * Calculate number of bvec segments that should be allocated to fit data
448 * pointed by @iter. If @iter is backed by bvec it's going to be reused
449 * instead of allocating a new one.
451 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
453 if (iov_iter_is_bvec(iter))
455 return iov_iter_npages(iter, max_segs);
458 struct request_queue;
460 extern int submit_bio_wait(struct bio *bio);
461 extern void bio_advance(struct bio *, unsigned);
463 extern void bio_init(struct bio *bio, struct bio_vec *table,
464 unsigned short max_vecs);
465 extern void bio_uninit(struct bio *);
466 extern void bio_reset(struct bio *);
467 void bio_chain(struct bio *, struct bio *);
469 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
470 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
471 unsigned int, unsigned int);
472 int bio_add_zone_append_page(struct bio *bio, struct page *page,
473 unsigned int len, unsigned int offset);
474 bool __bio_try_merge_page(struct bio *bio, struct page *page,
475 unsigned int len, unsigned int off, bool *same_page);
476 void __bio_add_page(struct bio *bio, struct page *page,
477 unsigned int len, unsigned int off);
478 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
479 void bio_release_pages(struct bio *bio, bool mark_dirty);
480 extern void bio_set_pages_dirty(struct bio *bio);
481 extern void bio_check_pages_dirty(struct bio *bio);
483 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
484 struct bio *src, struct bvec_iter *src_iter);
485 extern void bio_copy_data(struct bio *dst, struct bio *src);
486 extern void bio_free_pages(struct bio *bio);
487 void bio_truncate(struct bio *bio, unsigned new_size);
488 void guard_bio_eod(struct bio *bio);
489 void zero_fill_bio(struct bio *bio);
491 extern const char *bio_devname(struct bio *bio, char *buffer);
493 #define bio_set_dev(bio, bdev) \
495 bio_clear_flag(bio, BIO_REMAPPED); \
496 if ((bio)->bi_bdev != (bdev)) \
497 bio_clear_flag(bio, BIO_THROTTLED); \
498 (bio)->bi_bdev = (bdev); \
499 bio_associate_blkg(bio); \
502 #define bio_copy_dev(dst, src) \
504 bio_clear_flag(dst, BIO_REMAPPED); \
505 (dst)->bi_bdev = (src)->bi_bdev; \
506 bio_clone_blkg_association(dst, src); \
509 #define bio_dev(bio) \
510 disk_devt((bio)->bi_bdev->bd_disk)
512 #ifdef CONFIG_BLK_CGROUP
513 void bio_associate_blkg(struct bio *bio);
514 void bio_associate_blkg_from_css(struct bio *bio,
515 struct cgroup_subsys_state *css);
516 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
517 #else /* CONFIG_BLK_CGROUP */
518 static inline void bio_associate_blkg(struct bio *bio) { }
519 static inline void bio_associate_blkg_from_css(struct bio *bio,
520 struct cgroup_subsys_state *css)
522 static inline void bio_clone_blkg_association(struct bio *dst,
524 #endif /* CONFIG_BLK_CGROUP */
526 #ifdef CONFIG_HIGHMEM
528 * remember never ever reenable interrupts between a bvec_kmap_irq and
531 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
536 * might not be a highmem page, but the preempt/irq count
537 * balancing is a lot nicer this way
539 local_irq_save(*flags);
540 addr = (unsigned long) kmap_atomic(bvec->bv_page);
542 BUG_ON(addr & ~PAGE_MASK);
544 return (char *) addr + bvec->bv_offset;
547 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
549 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
551 kunmap_atomic((void *) ptr);
552 local_irq_restore(*flags);
556 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
558 return page_address(bvec->bv_page) + bvec->bv_offset;
561 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
568 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
570 * A bio_list anchors a singly-linked list of bios chained through the bi_next
571 * member of the bio. The bio_list also caches the last list member to allow
572 * fast access to the tail.
579 static inline int bio_list_empty(const struct bio_list *bl)
581 return bl->head == NULL;
584 static inline void bio_list_init(struct bio_list *bl)
586 bl->head = bl->tail = NULL;
589 #define BIO_EMPTY_LIST { NULL, NULL }
591 #define bio_list_for_each(bio, bl) \
592 for (bio = (bl)->head; bio; bio = bio->bi_next)
594 static inline unsigned bio_list_size(const struct bio_list *bl)
599 bio_list_for_each(bio, bl)
605 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
610 bl->tail->bi_next = bio;
617 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
619 bio->bi_next = bl->head;
627 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
633 bl->tail->bi_next = bl2->head;
635 bl->head = bl2->head;
637 bl->tail = bl2->tail;
640 static inline void bio_list_merge_head(struct bio_list *bl,
641 struct bio_list *bl2)
647 bl2->tail->bi_next = bl->head;
649 bl->tail = bl2->tail;
651 bl->head = bl2->head;
654 static inline struct bio *bio_list_peek(struct bio_list *bl)
659 static inline struct bio *bio_list_pop(struct bio_list *bl)
661 struct bio *bio = bl->head;
664 bl->head = bl->head->bi_next;
674 static inline struct bio *bio_list_get(struct bio_list *bl)
676 struct bio *bio = bl->head;
678 bl->head = bl->tail = NULL;
684 * Increment chain count for the bio. Make sure the CHAIN flag update
685 * is visible before the raised count.
687 static inline void bio_inc_remaining(struct bio *bio)
689 bio_set_flag(bio, BIO_CHAIN);
690 smp_mb__before_atomic();
691 atomic_inc(&bio->__bi_remaining);
695 * bio_set is used to allow other portions of the IO system to
696 * allocate their own private memory pools for bio and iovec structures.
697 * These memory pools in turn all allocate from the bio_slab
698 * and the bvec_slabs[].
700 #define BIO_POOL_SIZE 2
703 struct kmem_cache *bio_slab;
704 unsigned int front_pad;
708 #if defined(CONFIG_BLK_DEV_INTEGRITY)
709 mempool_t bio_integrity_pool;
710 mempool_t bvec_integrity_pool;
713 unsigned int back_pad;
715 * Deadlock avoidance for stacking block drivers: see comments in
716 * bio_alloc_bioset() for details
718 spinlock_t rescue_lock;
719 struct bio_list rescue_list;
720 struct work_struct rescue_work;
721 struct workqueue_struct *rescue_workqueue;
724 static inline bool bioset_initialized(struct bio_set *bs)
726 return bs->bio_slab != NULL;
729 #if defined(CONFIG_BLK_DEV_INTEGRITY)
731 #define bip_for_each_vec(bvl, bip, iter) \
732 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
734 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
736 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
738 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
739 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
740 extern bool bio_integrity_prep(struct bio *);
741 extern void bio_integrity_advance(struct bio *, unsigned int);
742 extern void bio_integrity_trim(struct bio *);
743 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
744 extern int bioset_integrity_create(struct bio_set *, int);
745 extern void bioset_integrity_free(struct bio_set *);
746 extern void bio_integrity_init(void);
748 #else /* CONFIG_BLK_DEV_INTEGRITY */
750 static inline void *bio_integrity(struct bio *bio)
755 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
760 static inline void bioset_integrity_free (struct bio_set *bs)
765 static inline bool bio_integrity_prep(struct bio *bio)
770 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
776 static inline void bio_integrity_advance(struct bio *bio,
777 unsigned int bytes_done)
782 static inline void bio_integrity_trim(struct bio *bio)
787 static inline void bio_integrity_init(void)
792 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
797 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
800 return ERR_PTR(-EINVAL);
803 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
804 unsigned int len, unsigned int offset)
809 #endif /* CONFIG_BLK_DEV_INTEGRITY */
812 * Mark a bio as polled. Note that for async polled IO, the caller must
813 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
814 * We cannot block waiting for requests on polled IO, as those completions
815 * must be found by the caller. This is different than IRQ driven IO, where
816 * it's safe to wait for IO to complete.
818 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
820 bio->bi_opf |= REQ_HIPRI;
821 if (!is_sync_kiocb(kiocb))
822 bio->bi_opf |= REQ_NOWAIT;
825 #endif /* __LINUX_BIO_H */