2 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 #include <linux/highmem.h>
22 #include <linux/mempool.h>
23 #include <linux/ioprio.h>
26 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
27 #include <linux/blk_types.h>
32 #define BIO_BUG_ON BUG_ON
37 #define BIO_MAX_PAGES 256
39 #define bio_prio(bio) (bio)->bi_ioprio
40 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
42 #define bio_iter_iovec(bio, iter) \
43 bvec_iter_bvec((bio)->bi_io_vec, (iter))
45 #define bio_iter_page(bio, iter) \
46 bvec_iter_page((bio)->bi_io_vec, (iter))
47 #define bio_iter_len(bio, iter) \
48 bvec_iter_len((bio)->bi_io_vec, (iter))
49 #define bio_iter_offset(bio, iter) \
50 bvec_iter_offset((bio)->bi_io_vec, (iter))
52 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
53 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
54 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
56 #define bio_multiple_segments(bio) \
57 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
59 #define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
60 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
62 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
63 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
66 * Return the data direction, READ or WRITE.
68 #define bio_data_dir(bio) \
69 (op_is_write(bio_op(bio)) ? WRITE : READ)
72 * Check whether this bio carries any data or not. A NULL bio is allowed.
74 static inline bool bio_has_data(struct bio *bio)
77 bio->bi_iter.bi_size &&
78 bio_op(bio) != REQ_OP_DISCARD &&
79 bio_op(bio) != REQ_OP_SECURE_ERASE &&
80 bio_op(bio) != REQ_OP_WRITE_ZEROES)
86 static inline bool bio_no_advance_iter(struct bio *bio)
88 return bio_op(bio) == REQ_OP_DISCARD ||
89 bio_op(bio) == REQ_OP_SECURE_ERASE ||
90 bio_op(bio) == REQ_OP_WRITE_SAME ||
91 bio_op(bio) == REQ_OP_WRITE_ZEROES;
94 static inline bool bio_mergeable(struct bio *bio)
96 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
102 static inline unsigned int bio_cur_bytes(struct bio *bio)
104 if (bio_has_data(bio))
105 return bio_iovec(bio).bv_len;
106 else /* dataless requests such as discard */
107 return bio->bi_iter.bi_size;
110 static inline void *bio_data(struct bio *bio)
112 if (bio_has_data(bio))
113 return page_address(bio_page(bio)) + bio_offset(bio);
118 static inline bool bio_full(struct bio *bio)
120 return bio->bi_vcnt >= bio->bi_max_vecs;
123 static inline bool bio_next_segment(const struct bio *bio,
124 struct bvec_iter_all *iter)
126 if (iter->idx >= bio->bi_vcnt)
129 bvec_advance(&bio->bi_io_vec[iter->idx], iter);
134 * drivers should _never_ use the all version - the bio may have been split
135 * before it got to the driver and the driver won't own all of it
137 #define bio_for_each_segment_all(bvl, bio, i, iter) \
138 for (i = 0, bvl = bvec_init_iter_all(&iter); \
139 bio_next_segment((bio), &iter); i++)
141 static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
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 #define __bio_for_each_segment(bvl, bio, iter, start) \
154 for (iter = (start); \
156 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
157 bio_advance_iter((bio), &(iter), (bvl).bv_len))
159 #define bio_for_each_segment(bvl, bio, iter) \
160 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
162 #define __bio_for_each_bvec(bvl, bio, iter, start) \
163 for (iter = (start); \
165 ((bvl = mp_bvec_iter_bvec((bio)->bi_io_vec, (iter))), 1); \
166 bio_advance_iter((bio), &(iter), (bvl).bv_len))
168 /* iterate over multi-page bvec */
169 #define bio_for_each_bvec(bvl, bio, iter) \
170 __bio_for_each_bvec(bvl, bio, iter, (bio)->bi_iter)
172 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
174 static inline unsigned bio_segments(struct bio *bio)
178 struct bvec_iter iter;
181 * We special case discard/write same/write zeroes, because they
182 * interpret bi_size differently:
185 switch (bio_op(bio)) {
187 case REQ_OP_SECURE_ERASE:
188 case REQ_OP_WRITE_ZEROES:
190 case REQ_OP_WRITE_SAME:
196 bio_for_each_segment(bv, bio, iter)
203 * get a reference to a bio, so it won't disappear. the intended use is
207 * submit_bio(rw, bio);
208 * if (bio->bi_flags ...)
212 * without the bio_get(), it could potentially complete I/O before submit_bio
213 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
216 static inline void bio_get(struct bio *bio)
218 bio->bi_flags |= (1 << BIO_REFFED);
219 smp_mb__before_atomic();
220 atomic_inc(&bio->__bi_cnt);
223 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
226 bio->bi_flags |= (1 << BIO_REFFED);
227 smp_mb__before_atomic();
229 atomic_set(&bio->__bi_cnt, count);
232 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
234 return (bio->bi_flags & (1U << bit)) != 0;
237 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
239 bio->bi_flags |= (1U << bit);
242 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
244 bio->bi_flags &= ~(1U << bit);
247 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
249 *bv = bio_iovec(bio);
252 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
254 struct bvec_iter iter = bio->bi_iter;
257 if (unlikely(!bio_multiple_segments(bio))) {
258 *bv = bio_iovec(bio);
262 bio_advance_iter(bio, &iter, iter.bi_size);
264 if (!iter.bi_bvec_done)
265 idx = iter.bi_idx - 1;
266 else /* in the middle of bvec */
269 *bv = bio->bi_io_vec[idx];
272 * iter.bi_bvec_done records actual length of the last bvec
273 * if this bio ends in the middle of one io vector
275 if (iter.bi_bvec_done)
276 bv->bv_len = iter.bi_bvec_done;
279 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
281 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
282 return bio->bi_io_vec;
285 static inline struct page *bio_first_page_all(struct bio *bio)
287 return bio_first_bvec_all(bio)->bv_page;
290 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
292 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
293 return &bio->bi_io_vec[bio->bi_vcnt - 1];
297 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
298 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
299 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
300 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
301 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
305 * bio integrity payload
307 struct bio_integrity_payload {
308 struct bio *bip_bio; /* parent bio */
310 struct bvec_iter bip_iter;
312 unsigned short bip_slab; /* slab the bip came from */
313 unsigned short bip_vcnt; /* # of integrity bio_vecs */
314 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
315 unsigned short bip_flags; /* control flags */
317 struct bvec_iter bio_iter; /* for rewinding parent bio */
319 struct work_struct bip_work; /* I/O completion */
321 struct bio_vec *bip_vec;
322 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
325 #if defined(CONFIG_BLK_DEV_INTEGRITY)
327 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
329 if (bio->bi_opf & REQ_INTEGRITY)
330 return bio->bi_integrity;
335 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
337 struct bio_integrity_payload *bip = bio_integrity(bio);
340 return bip->bip_flags & flag;
345 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
347 return bip->bip_iter.bi_sector;
350 static inline void bip_set_seed(struct bio_integrity_payload *bip,
353 bip->bip_iter.bi_sector = seed;
356 #endif /* CONFIG_BLK_DEV_INTEGRITY */
358 extern void bio_trim(struct bio *bio, int offset, int size);
359 extern struct bio *bio_split(struct bio *bio, int sectors,
360 gfp_t gfp, struct bio_set *bs);
363 * bio_next_split - get next @sectors from a bio, splitting if necessary
365 * @sectors: number of sectors to split from the front of @bio
367 * @bs: bio set to allocate from
369 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
370 * than @sectors, returns the original bio unchanged.
372 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
373 gfp_t gfp, struct bio_set *bs)
375 if (sectors >= bio_sectors(bio))
378 return bio_split(bio, sectors, gfp, bs);
382 BIOSET_NEED_BVECS = BIT(0),
383 BIOSET_NEED_RESCUER = BIT(1),
385 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
386 extern void bioset_exit(struct bio_set *);
387 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
388 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
390 extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *);
391 extern void bio_put(struct bio *);
393 extern void __bio_clone_fast(struct bio *, struct bio *);
394 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
396 extern struct bio_set fs_bio_set;
398 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
400 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
403 static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
405 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
408 extern blk_qc_t submit_bio(struct bio *);
410 extern void bio_endio(struct bio *);
412 static inline void bio_io_error(struct bio *bio)
414 bio->bi_status = BLK_STS_IOERR;
418 static inline void bio_wouldblock_error(struct bio *bio)
420 bio->bi_status = BLK_STS_AGAIN;
424 struct request_queue;
425 extern int bio_phys_segments(struct request_queue *, struct bio *);
427 extern int submit_bio_wait(struct bio *bio);
428 extern void bio_advance(struct bio *, unsigned);
430 extern void bio_init(struct bio *bio, struct bio_vec *table,
431 unsigned short max_vecs);
432 extern void bio_uninit(struct bio *);
433 extern void bio_reset(struct bio *);
434 void bio_chain(struct bio *, struct bio *);
436 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
437 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
438 unsigned int, unsigned int);
439 bool __bio_try_merge_page(struct bio *bio, struct page *page,
440 unsigned int len, unsigned int off, bool same_page);
441 void __bio_add_page(struct bio *bio, struct page *page,
442 unsigned int len, unsigned int off);
443 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
445 extern struct bio *bio_map_user_iov(struct request_queue *,
446 struct iov_iter *, gfp_t);
447 extern void bio_unmap_user(struct bio *);
448 extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
450 extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
452 extern void bio_set_pages_dirty(struct bio *bio);
453 extern void bio_check_pages_dirty(struct bio *bio);
455 void generic_start_io_acct(struct request_queue *q, int op,
456 unsigned long sectors, struct hd_struct *part);
457 void generic_end_io_acct(struct request_queue *q, int op,
458 struct hd_struct *part,
459 unsigned long start_time);
461 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
462 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
464 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
465 extern void bio_flush_dcache_pages(struct bio *bi);
467 static inline void bio_flush_dcache_pages(struct bio *bi)
472 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
473 struct bio *src, struct bvec_iter *src_iter);
474 extern void bio_copy_data(struct bio *dst, struct bio *src);
475 extern void bio_list_copy_data(struct bio *dst, struct bio *src);
476 extern void bio_free_pages(struct bio *bio);
478 extern struct bio *bio_copy_user_iov(struct request_queue *,
479 struct rq_map_data *,
482 extern int bio_uncopy_user(struct bio *);
483 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
485 static inline void zero_fill_bio(struct bio *bio)
487 zero_fill_bio_iter(bio, bio->bi_iter);
490 extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
491 extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
492 extern unsigned int bvec_nr_vecs(unsigned short idx);
493 extern const char *bio_devname(struct bio *bio, char *buffer);
495 #define bio_set_dev(bio, bdev) \
497 if ((bio)->bi_disk != (bdev)->bd_disk) \
498 bio_clear_flag(bio, BIO_THROTTLED);\
499 (bio)->bi_disk = (bdev)->bd_disk; \
500 (bio)->bi_partno = (bdev)->bd_partno; \
501 bio_associate_blkg(bio); \
504 #define bio_copy_dev(dst, src) \
506 (dst)->bi_disk = (src)->bi_disk; \
507 (dst)->bi_partno = (src)->bi_partno; \
508 bio_clone_blkg_association(dst, src); \
511 #define bio_dev(bio) \
512 disk_devt((bio)->bi_disk)
514 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
515 void bio_associate_blkg_from_page(struct bio *bio, struct page *page);
517 static inline void bio_associate_blkg_from_page(struct bio *bio,
518 struct page *page) { }
521 #ifdef CONFIG_BLK_CGROUP
522 void bio_disassociate_blkg(struct bio *bio);
523 void bio_associate_blkg(struct bio *bio);
524 void bio_associate_blkg_from_css(struct bio *bio,
525 struct cgroup_subsys_state *css);
526 void bio_clone_blkg_association(struct bio *dst, struct bio *src);
527 #else /* CONFIG_BLK_CGROUP */
528 static inline void bio_disassociate_blkg(struct bio *bio) { }
529 static inline void bio_associate_blkg(struct bio *bio) { }
530 static inline void bio_associate_blkg_from_css(struct bio *bio,
531 struct cgroup_subsys_state *css)
533 static inline void bio_clone_blkg_association(struct bio *dst,
535 #endif /* CONFIG_BLK_CGROUP */
537 #ifdef CONFIG_HIGHMEM
539 * remember never ever reenable interrupts between a bvec_kmap_irq and
542 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
547 * might not be a highmem page, but the preempt/irq count
548 * balancing is a lot nicer this way
550 local_irq_save(*flags);
551 addr = (unsigned long) kmap_atomic(bvec->bv_page);
553 BUG_ON(addr & ~PAGE_MASK);
555 return (char *) addr + bvec->bv_offset;
558 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
560 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
562 kunmap_atomic((void *) ptr);
563 local_irq_restore(*flags);
567 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
569 return page_address(bvec->bv_page) + bvec->bv_offset;
572 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
579 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
581 * A bio_list anchors a singly-linked list of bios chained through the bi_next
582 * member of the bio. The bio_list also caches the last list member to allow
583 * fast access to the tail.
590 static inline int bio_list_empty(const struct bio_list *bl)
592 return bl->head == NULL;
595 static inline void bio_list_init(struct bio_list *bl)
597 bl->head = bl->tail = NULL;
600 #define BIO_EMPTY_LIST { NULL, NULL }
602 #define bio_list_for_each(bio, bl) \
603 for (bio = (bl)->head; bio; bio = bio->bi_next)
605 static inline unsigned bio_list_size(const struct bio_list *bl)
610 bio_list_for_each(bio, bl)
616 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
621 bl->tail->bi_next = bio;
628 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
630 bio->bi_next = bl->head;
638 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
644 bl->tail->bi_next = bl2->head;
646 bl->head = bl2->head;
648 bl->tail = bl2->tail;
651 static inline void bio_list_merge_head(struct bio_list *bl,
652 struct bio_list *bl2)
658 bl2->tail->bi_next = bl->head;
660 bl->tail = bl2->tail;
662 bl->head = bl2->head;
665 static inline struct bio *bio_list_peek(struct bio_list *bl)
670 static inline struct bio *bio_list_pop(struct bio_list *bl)
672 struct bio *bio = bl->head;
675 bl->head = bl->head->bi_next;
685 static inline struct bio *bio_list_get(struct bio_list *bl)
687 struct bio *bio = bl->head;
689 bl->head = bl->tail = NULL;
695 * Increment chain count for the bio. Make sure the CHAIN flag update
696 * is visible before the raised count.
698 static inline void bio_inc_remaining(struct bio *bio)
700 bio_set_flag(bio, BIO_CHAIN);
701 smp_mb__before_atomic();
702 atomic_inc(&bio->__bi_remaining);
706 * bio_set is used to allow other portions of the IO system to
707 * allocate their own private memory pools for bio and iovec structures.
708 * These memory pools in turn all allocate from the bio_slab
709 * and the bvec_slabs[].
711 #define BIO_POOL_SIZE 2
714 struct kmem_cache *bio_slab;
715 unsigned int front_pad;
719 #if defined(CONFIG_BLK_DEV_INTEGRITY)
720 mempool_t bio_integrity_pool;
721 mempool_t bvec_integrity_pool;
725 * Deadlock avoidance for stacking block drivers: see comments in
726 * bio_alloc_bioset() for details
728 spinlock_t rescue_lock;
729 struct bio_list rescue_list;
730 struct work_struct rescue_work;
731 struct workqueue_struct *rescue_workqueue;
737 struct kmem_cache *slab;
740 static inline bool bioset_initialized(struct bio_set *bs)
742 return bs->bio_slab != NULL;
746 * a small number of entries is fine, not going to be performance critical.
747 * basically we just need to survive
749 #define BIO_SPLIT_ENTRIES 2
751 #if defined(CONFIG_BLK_DEV_INTEGRITY)
753 #define bip_for_each_vec(bvl, bip, iter) \
754 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
756 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
758 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
760 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
761 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
762 extern bool bio_integrity_prep(struct bio *);
763 extern void bio_integrity_advance(struct bio *, unsigned int);
764 extern void bio_integrity_trim(struct bio *);
765 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
766 extern int bioset_integrity_create(struct bio_set *, int);
767 extern void bioset_integrity_free(struct bio_set *);
768 extern void bio_integrity_init(void);
770 #else /* CONFIG_BLK_DEV_INTEGRITY */
772 static inline void *bio_integrity(struct bio *bio)
777 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
782 static inline void bioset_integrity_free (struct bio_set *bs)
787 static inline bool bio_integrity_prep(struct bio *bio)
792 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
798 static inline void bio_integrity_advance(struct bio *bio,
799 unsigned int bytes_done)
804 static inline void bio_integrity_trim(struct bio *bio)
809 static inline void bio_integrity_init(void)
814 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
819 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
822 return ERR_PTR(-EINVAL);
825 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
826 unsigned int len, unsigned int offset)
831 #endif /* CONFIG_BLK_DEV_INTEGRITY */
834 * Mark a bio as polled. Note that for async polled IO, the caller must
835 * expect -EWOULDBLOCK if we cannot allocate a request (or other resources).
836 * We cannot block waiting for requests on polled IO, as those completions
837 * must be found by the caller. This is different than IRQ driven IO, where
838 * it's safe to wait for IO to complete.
840 static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
842 bio->bi_opf |= REQ_HIPRI;
843 if (!is_sync_kiocb(kiocb))
844 bio->bi_opf |= REQ_NOWAIT;
847 #endif /* CONFIG_BLOCK */
848 #endif /* __LINUX_BIO_H */