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 bvec_iter_sectors(iter) ((iter).bi_size >> 9)
48 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
50 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
51 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
54 * Return the data direction, READ or WRITE.
56 #define bio_data_dir(bio) \
57 (op_is_write(bio_op(bio)) ? WRITE : READ)
60 * Check whether this bio carries any data or not. A NULL bio is allowed.
62 static inline bool bio_has_data(struct bio *bio)
65 bio->bi_iter.bi_size &&
66 bio_op(bio) != REQ_OP_DISCARD &&
67 bio_op(bio) != REQ_OP_SECURE_ERASE &&
68 bio_op(bio) != REQ_OP_WRITE_ZEROES)
74 static inline bool bio_no_advance_iter(const struct bio *bio)
76 return bio_op(bio) == REQ_OP_DISCARD ||
77 bio_op(bio) == REQ_OP_SECURE_ERASE ||
78 bio_op(bio) == REQ_OP_WRITE_SAME ||
79 bio_op(bio) == REQ_OP_WRITE_ZEROES;
82 static inline bool bio_mergeable(struct bio *bio)
84 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
90 static inline unsigned int bio_cur_bytes(struct bio *bio)
92 if (bio_has_data(bio))
93 return bio_iovec(bio).bv_len;
94 else /* dataless requests such as discard */
95 return bio->bi_iter.bi_size;
98 static inline void *bio_data(struct bio *bio)
100 if (bio_has_data(bio))
101 return page_address(bio_page(bio)) + bio_offset(bio);
107 * bio_full - check if the bio is full
109 * @len: length of one segment to be added
111 * Return true if @bio is full and one segment with @len bytes can't be
112 * added to the bio, otherwise return false
114 static inline bool bio_full(struct bio *bio, unsigned len)
116 if (bio->bi_vcnt >= bio->bi_max_vecs)
119 if (bio->bi_iter.bi_size > UINT_MAX - len)
125 static inline bool bio_next_segment(const struct bio *bio,
126 struct bvec_iter_all *iter)
128 if (iter->idx >= bio->bi_vcnt)
131 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
136 * drivers should _never_ use the all version - the bio may have been split
137 * before it got to the driver and the driver won't own all of it
139 #define bio_for_each_segment_all(bvl, bio, iter) \
140 for (bvl = bvec_init_iter_all(&iter); bio_next_segment((bio), &iter); )
142 static inline void bio_advance_iter(const struct bio *bio,
143 struct bvec_iter *iter, unsigned int bytes)
145 iter->bi_sector += bytes >> 9;
147 if (bio_no_advance_iter(bio))
148 iter->bi_size -= bytes;
150 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
151 /* TODO: It is reasonable to complete bio with error here. */
154 /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */
155 static inline void bio_advance_iter_single(const struct bio *bio,
156 struct bvec_iter *iter,
159 iter->bi_sector += bytes >> 9;
161 if (bio_no_advance_iter(bio))
162 iter->bi_size -= bytes;
164 bvec_iter_advance_single(bio->bi_io_vec, iter, bytes);
167 #define __bio_for_each_segment(bvl, bio, iter, start) \
168 for (iter = (start); \
170 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
171 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
173 #define bio_for_each_segment(bvl, bio, iter) \
174 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
176 #define __bio_for_each_bvec(bvl, bio, iter, start) \
177 for (iter = (start); \
179 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
180 bio_advance_iter_single((bio), &(iter), (bvl).bv_len))
182 /* iterate over multi-page bvec */
183 #define bio_for_each_bvec(bvl, bio, iter) \
184 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
187 * Iterate over all multi-page bvecs. Drivers shouldn't use this version for the
188 * same reasons as bio_for_each_segment_all().
190 #define bio_for_each_bvec_all(bvl, bio, i) \
191 for (i = 0, bvl = bio_first_bvec_all(bio); \
192 i < (bio)->bi_vcnt; i++, bvl++) \
194 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
196 static inline unsigned bio_segments(struct bio *bio)
200 struct bvec_iter iter;
203 * We special case discard/write same/write zeroes, because they
204 * interpret bi_size differently:
207 switch (bio_op(bio)) {
209 case REQ_OP_SECURE_ERASE:
210 case REQ_OP_WRITE_ZEROES:
212 case REQ_OP_WRITE_SAME:
218 bio_for_each_segment(bv, bio, iter)
225 * get a reference to a bio, so it won't disappear. the intended use is
229 * submit_bio(rw, bio);
230 * if (bio->bi_flags ...)
234 * without the bio_get(), it could potentially complete I/O before submit_bio
235 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
238 static inline void bio_get(struct bio *bio)
240 bio->bi_flags |= (1 << BIO_REFFED);
241 smp_mb__before_atomic();
242 atomic_inc(&bio->__bi_cnt);
245 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
248 bio->bi_flags |= (1 << BIO_REFFED);
251 atomic_set(&bio->__bi_cnt, count);
254 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
256 return (bio->bi_flags & (1U << bit)) != 0;
259 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
261 bio->bi_flags |= (1U << bit);
264 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
266 bio->bi_flags &= ~(1U << bit);
269 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
271 *bv = mp_bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
274 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
276 struct bvec_iter iter = bio->bi_iter;
279 bio_get_first_bvec(bio, bv);
280 if (bv->bv_len == bio->bi_iter.bi_size)
281 return; /* this bio only has a single bvec */
283 bio_advance_iter(bio, &iter, iter.bi_size);
285 if (!iter.bi_bvec_done)
286 idx = iter.bi_idx - 1;
287 else /* in the middle of bvec */
290 *bv = bio->bi_io_vec[idx];
293 * iter.bi_bvec_done records actual length of the last bvec
294 * if this bio ends in the middle of one io vector
296 if (iter.bi_bvec_done)
297 bv->bv_len = iter.bi_bvec_done;
300 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
302 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
303 return bio->bi_io_vec;
306 static inline struct page *bio_first_page_all(struct bio *bio)
308 return bio_first_bvec_all(bio)->bv_page;
311 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
313 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
314 return &bio->bi_io_vec[bio->bi_vcnt - 1];
318 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
319 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
320 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
321 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
322 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
326 * bio integrity payload
328 struct bio_integrity_payload {
329 struct bio *bip_bio; /* parent bio */
331 struct bvec_iter bip_iter;
333 unsigned short bip_vcnt; /* # of integrity bio_vecs */
334 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
335 unsigned short bip_flags; /* control flags */
337 struct bvec_iter bio_iter; /* for rewinding parent bio */
339 struct work_struct bip_work; /* I/O completion */
341 struct bio_vec *bip_vec;
342 struct bio_vec bip_inline_vecs[];/* embedded bvec array */
345 #if defined(CONFIG_BLK_DEV_INTEGRITY)
347 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
349 if (bio->bi_opf & REQ_INTEGRITY)
350 return bio->bi_integrity;
355 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
357 struct bio_integrity_payload *bip = bio_integrity(bio);
360 return bip->bip_flags & flag;
365 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
367 return bip->bip_iter.bi_sector;
370 static inline void bip_set_seed(struct bio_integrity_payload *bip,
373 bip->bip_iter.bi_sector = seed;
376 #endif /* CONFIG_BLK_DEV_INTEGRITY */
378 extern void bio_trim(struct bio *bio, int offset, int size);
379 extern struct bio *bio_split(struct bio *bio, int sectors,
380 gfp_t gfp, struct bio_set *bs);
383 * bio_next_split - get next @sectors from a bio, splitting if necessary
385 * @sectors: number of sectors to split from the front of @bio
387 * @bs: bio set to allocate from
389 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
390 * than @sectors, returns the original bio unchanged.
392 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
393 gfp_t gfp, struct bio_set *bs)
395 if (sectors >= bio_sectors(bio))
398 return bio_split(bio, sectors, gfp, bs);
402 BIOSET_NEED_BVECS = BIT(0),
403 BIOSET_NEED_RESCUER = BIT(1),
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_kmalloc(gfp_t gfp_mask, unsigned short nr_iovecs);
413 extern void bio_put(struct bio *);
415 extern void __bio_clone_fast(struct bio *, struct bio *);
416 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
418 extern struct bio_set fs_bio_set;
420 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned short nr_iovecs)
422 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
425 extern blk_qc_t submit_bio(struct bio *);
427 extern void bio_endio(struct bio *);
429 static inline void bio_io_error(struct bio *bio)
431 bio->bi_status = BLK_STS_IOERR;
435 static inline void bio_wouldblock_error(struct bio *bio)
437 bio_set_flag(bio, BIO_QUIET);
438 bio->bi_status = BLK_STS_AGAIN;
443 * Calculate number of bvec segments that should be allocated to fit data
444 * pointed by @iter. If @iter is backed by bvec it's going to be reused
445 * instead of allocating a new one.
447 static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
449 if (iov_iter_is_bvec(iter))
451 return iov_iter_npages(iter, max_segs);
454 struct request_queue;
456 extern int submit_bio_wait(struct bio *bio);
457 extern void bio_advance(struct bio *, unsigned);
459 extern void bio_init(struct bio *bio, struct bio_vec *table,
460 unsigned short max_vecs);
461 extern void bio_uninit(struct bio *);
462 extern void bio_reset(struct bio *);
463 void bio_chain(struct bio *, struct bio *);
465 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
466 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
467 unsigned int, unsigned int);
468 int bio_add_zone_append_page(struct bio *bio, struct page *page,
469 unsigned int len, unsigned int offset);
470 bool __bio_try_merge_page(struct bio *bio, struct page *page,
471 unsigned int len, unsigned int off, bool *same_page);
472 void __bio_add_page(struct bio *bio, struct page *page,
473 unsigned int len, unsigned int off);
474 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
475 void bio_release_pages(struct bio *bio, bool mark_dirty);
476 extern void bio_set_pages_dirty(struct bio *bio);
477 extern void bio_check_pages_dirty(struct bio *bio);
479 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
480 struct bio *src, struct bvec_iter *src_iter);
481 extern void bio_copy_data(struct bio *dst, struct bio *src);
482 extern void bio_free_pages(struct bio *bio);
483 void bio_truncate(struct bio *bio, unsigned new_size);
484 void guard_bio_eod(struct bio *bio);
485 void zero_fill_bio(struct bio *bio);
487 extern const char *bio_devname(struct bio *bio, char *buffer);
489 #define bio_set_dev(bio, bdev) \
491 bio_clear_flag(bio, BIO_REMAPPED); \
492 if ((bio)->bi_bdev != (bdev)) \
493 bio_clear_flag(bio, BIO_THROTTLED); \
494 (bio)->bi_bdev = (bdev); \
495 bio_associate_blkg(bio); \
498 #define bio_copy_dev(dst, src) \
500 bio_clear_flag(dst, BIO_REMAPPED); \
501 (dst)->bi_bdev = (src)->bi_bdev; \
502 bio_clone_blkg_association(dst, src); \
505 #define bio_dev(bio) \
506 disk_devt((bio)->bi_bdev->bd_disk)
508 #ifdef CONFIG_BLK_CGROUP
509 void bio_associate_blkg(struct bio *bio);
510 void bio_associate_blkg_from_css(struct bio *bio,
511 struct cgroup_subsys_state *css);
512 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
513 #else /* CONFIG_BLK_CGROUP */
514 static inline void bio_associate_blkg(struct bio *bio) { }
515 static inline void bio_associate_blkg_from_css(struct bio *bio,
516 struct cgroup_subsys_state *css)
518 static inline void bio_clone_blkg_association(struct bio *dst,
520 #endif /* CONFIG_BLK_CGROUP */
522 #ifdef CONFIG_HIGHMEM
524 * remember never ever reenable interrupts between a bvec_kmap_irq and
527 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
532 * might not be a highmem page, but the preempt/irq count
533 * balancing is a lot nicer this way
535 local_irq_save(*flags);
536 addr = (unsigned long) kmap_atomic(bvec->bv_page);
538 BUG_ON(addr & ~PAGE_MASK);
540 return (char *) addr + bvec->bv_offset;
543 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
545 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
547 kunmap_atomic((void *) ptr);
548 local_irq_restore(*flags);
552 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
554 return page_address(bvec->bv_page) + bvec->bv_offset;
557 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
564 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
566 * A bio_list anchors a singly-linked list of bios chained through the bi_next
567 * member of the bio. The bio_list also caches the last list member to allow
568 * fast access to the tail.
575 static inline int bio_list_empty(const struct bio_list *bl)
577 return bl->head == NULL;
580 static inline void bio_list_init(struct bio_list *bl)
582 bl->head = bl->tail = NULL;
585 #define BIO_EMPTY_LIST { NULL, NULL }
587 #define bio_list_for_each(bio, bl) \
588 for (bio = (bl)->head; bio; bio = bio->bi_next)
590 static inline unsigned bio_list_size(const struct bio_list *bl)
595 bio_list_for_each(bio, bl)
601 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
606 bl->tail->bi_next = bio;
613 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
615 bio->bi_next = bl->head;
623 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
629 bl->tail->bi_next = bl2->head;
631 bl->head = bl2->head;
633 bl->tail = bl2->tail;
636 static inline void bio_list_merge_head(struct bio_list *bl,
637 struct bio_list *bl2)
643 bl2->tail->bi_next = bl->head;
645 bl->tail = bl2->tail;
647 bl->head = bl2->head;
650 static inline struct bio *bio_list_peek(struct bio_list *bl)
655 static inline struct bio *bio_list_pop(struct bio_list *bl)
657 struct bio *bio = bl->head;
660 bl->head = bl->head->bi_next;
670 static inline struct bio *bio_list_get(struct bio_list *bl)
672 struct bio *bio = bl->head;
674 bl->head = bl->tail = NULL;
680 * Increment chain count for the bio. Make sure the CHAIN flag update
681 * is visible before the raised count.
683 static inline void bio_inc_remaining(struct bio *bio)
685 bio_set_flag(bio, BIO_CHAIN);
686 smp_mb__before_atomic();
687 atomic_inc(&bio->__bi_remaining);
691 * bio_set is used to allow other portions of the IO system to
692 * allocate their own private memory pools for bio and iovec structures.
693 * These memory pools in turn all allocate from the bio_slab
694 * and the bvec_slabs[].
696 #define BIO_POOL_SIZE 2
699 struct kmem_cache *bio_slab;
700 unsigned int front_pad;
704 #if defined(CONFIG_BLK_DEV_INTEGRITY)
705 mempool_t bio_integrity_pool;
706 mempool_t bvec_integrity_pool;
709 unsigned int back_pad;
711 * Deadlock avoidance for stacking block drivers: see comments in
712 * bio_alloc_bioset() for details
714 spinlock_t rescue_lock;
715 struct bio_list rescue_list;
716 struct work_struct rescue_work;
717 struct workqueue_struct *rescue_workqueue;
720 static inline bool bioset_initialized(struct bio_set *bs)
722 return bs->bio_slab != NULL;
725 #if defined(CONFIG_BLK_DEV_INTEGRITY)
727 #define bip_for_each_vec(bvl, bip, iter) \
728 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
730 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
732 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
734 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
735 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
736 extern bool bio_integrity_prep(struct bio *);
737 extern void bio_integrity_advance(struct bio *, unsigned int);
738 extern void bio_integrity_trim(struct bio *);
739 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
740 extern int bioset_integrity_create(struct bio_set *, int);
741 extern void bioset_integrity_free(struct bio_set *);
742 extern void bio_integrity_init(void);
744 #else /* CONFIG_BLK_DEV_INTEGRITY */
746 static inline void *bio_integrity(struct bio *bio)
751 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
756 static inline void bioset_integrity_free (struct bio_set *bs)
761 static inline bool bio_integrity_prep(struct bio *bio)
766 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
772 static inline void bio_integrity_advance(struct bio *bio,
773 unsigned int bytes_done)
778 static inline void bio_integrity_trim(struct bio *bio)
783 static inline void bio_integrity_init(void)
788 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
793 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
796 return ERR_PTR(-EINVAL);
799 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
800 unsigned int len, unsigned int offset)
805 #endif /* CONFIG_BLK_DEV_INTEGRITY */
808 * Mark a bio as polled. Note that for async polled IO, the caller must
809 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
810 * We cannot block waiting for requests on polled IO, as those completions
811 * must be found by the caller. This is different than IRQ driven IO, where
812 * it's safe to wait for IO to complete.
814 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
816 bio->bi_opf |= REQ_HIPRI;
817 if (!is_sync_kiocb(kiocb))
818 bio->bi_opf |= REQ_NOWAIT;
821 struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
823 #endif /* __LINUX_BIO_H */