4 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public Licens
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
23 #include <linux/highmem.h>
24 #include <linux/mempool.h>
25 #include <linux/ioprio.h>
26 #include <linux/bug.h>
32 /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
33 #include <linux/blk_types.h>
38 #define BIO_BUG_ON BUG_ON
43 #define BIO_MAX_PAGES 256
44 #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_SHIFT)
45 #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
47 #define bio_prio(bio) (bio)->bi_ioprio
48 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
51 * various member access, note that bio_data should of course not be used
52 * on highmem page vectors
54 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
56 #define bvec_iter_page(bvec, iter) \
57 (__bvec_iter_bvec((bvec), (iter))->bv_page)
59 #define bvec_iter_len(bvec, iter) \
61 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
63 #define bvec_iter_offset(bvec, iter) \
64 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
66 #define bvec_iter_bvec(bvec, iter) \
68 .bv_page = bvec_iter_page((bvec), (iter)), \
69 .bv_len = bvec_iter_len((bvec), (iter)), \
70 .bv_offset = bvec_iter_offset((bvec), (iter)), \
73 #define bio_iter_iovec(bio, iter) \
74 bvec_iter_bvec((bio)->bi_io_vec, (iter))
76 #define bio_iter_page(bio, iter) \
77 bvec_iter_page((bio)->bi_io_vec, (iter))
78 #define bio_iter_len(bio, iter) \
79 bvec_iter_len((bio)->bi_io_vec, (iter))
80 #define bio_iter_offset(bio, iter) \
81 bvec_iter_offset((bio)->bi_io_vec, (iter))
83 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
84 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
85 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
87 #define bio_multiple_segments(bio) \
88 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
89 #define bio_sectors(bio) ((bio)->bi_iter.bi_size >> 9)
90 #define bio_end_sector(bio) ((bio)->bi_iter.bi_sector + bio_sectors((bio)))
93 * Check whether this bio carries any data or not. A NULL bio is allowed.
95 static inline bool bio_has_data(struct bio *bio)
98 bio->bi_iter.bi_size &&
99 bio_op(bio) != REQ_OP_DISCARD)
105 static inline bool bio_no_advance_iter(struct bio *bio)
107 return bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_WRITE_SAME;
110 static inline bool bio_is_rw(struct bio *bio)
112 if (!bio_has_data(bio))
115 if (bio_no_advance_iter(bio))
121 static inline bool bio_mergeable(struct bio *bio)
123 if (bio->bi_rw & REQ_NOMERGE_FLAGS)
129 static inline unsigned int bio_cur_bytes(struct bio *bio)
131 if (bio_has_data(bio))
132 return bio_iovec(bio).bv_len;
133 else /* dataless requests such as discard */
134 return bio->bi_iter.bi_size;
137 static inline void *bio_data(struct bio *bio)
139 if (bio_has_data(bio))
140 return page_address(bio_page(bio)) + bio_offset(bio);
148 #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
149 #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
152 * queues that have highmem support enabled may still need to revert to
153 * PIO transfers occasionally and thus map high pages temporarily. For
154 * permanent PIO fall back, user is probably better off disabling highmem
155 * I/O completely on that queue (see ide-dma for example)
157 #define __bio_kmap_atomic(bio, iter) \
158 (kmap_atomic(bio_iter_iovec((bio), (iter)).bv_page) + \
159 bio_iter_iovec((bio), (iter)).bv_offset)
161 #define __bio_kunmap_atomic(addr) kunmap_atomic(addr)
167 /* Default implementation of BIOVEC_PHYS_MERGEABLE */
168 #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
169 ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
172 * allow arch override, for eg virtualized architectures (put in asm/io.h)
174 #ifndef BIOVEC_PHYS_MERGEABLE
175 #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
176 __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
179 #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
180 (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
181 #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
182 __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
185 * drivers should _never_ use the all version - the bio may have been split
186 * before it got to the driver and the driver won't own all of it
188 #define bio_for_each_segment_all(bvl, bio, i) \
189 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
191 static inline void bvec_iter_advance(struct bio_vec *bv, struct bvec_iter *iter,
194 WARN_ONCE(bytes > iter->bi_size,
195 "Attempted to advance past end of bvec iter\n");
198 unsigned len = min(bytes, bvec_iter_len(bv, *iter));
201 iter->bi_size -= len;
202 iter->bi_bvec_done += len;
204 if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
205 iter->bi_bvec_done = 0;
211 #define for_each_bvec(bvl, bio_vec, iter, start) \
212 for (iter = (start); \
214 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
215 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
218 static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
221 iter->bi_sector += bytes >> 9;
223 if (bio_no_advance_iter(bio))
224 iter->bi_size -= bytes;
226 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
229 #define __bio_for_each_segment(bvl, bio, iter, start) \
230 for (iter = (start); \
232 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
233 bio_advance_iter((bio), &(iter), (bvl).bv_len))
235 #define bio_for_each_segment(bvl, bio, iter) \
236 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
238 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
240 static inline unsigned bio_segments(struct bio *bio)
244 struct bvec_iter iter;
247 * We special case discard/write same, because they interpret bi_size
251 if (bio_op(bio) == REQ_OP_DISCARD)
254 if (bio_op(bio) == REQ_OP_WRITE_SAME)
257 bio_for_each_segment(bv, bio, iter)
264 * get a reference to a bio, so it won't disappear. the intended use is
268 * submit_bio(rw, bio);
269 * if (bio->bi_flags ...)
273 * without the bio_get(), it could potentially complete I/O before submit_bio
274 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
277 static inline void bio_get(struct bio *bio)
279 bio->bi_flags |= (1 << BIO_REFFED);
280 smp_mb__before_atomic();
281 atomic_inc(&bio->__bi_cnt);
284 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
287 bio->bi_flags |= (1 << BIO_REFFED);
288 smp_mb__before_atomic();
290 atomic_set(&bio->__bi_cnt, count);
293 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
295 return (bio->bi_flags & (1U << bit)) != 0;
298 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
300 bio->bi_flags |= (1U << bit);
303 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
305 bio->bi_flags &= ~(1U << bit);
308 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
310 *bv = bio_iovec(bio);
313 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
315 struct bvec_iter iter = bio->bi_iter;
318 if (unlikely(!bio_multiple_segments(bio))) {
319 *bv = bio_iovec(bio);
323 bio_advance_iter(bio, &iter, iter.bi_size);
325 if (!iter.bi_bvec_done)
326 idx = iter.bi_idx - 1;
327 else /* in the middle of bvec */
330 *bv = bio->bi_io_vec[idx];
333 * iter.bi_bvec_done records actual length of the last bvec
334 * if this bio ends in the middle of one io vector
336 if (iter.bi_bvec_done)
337 bv->bv_len = iter.bi_bvec_done;
341 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
342 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
343 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
344 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
345 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
349 * bio integrity payload
351 struct bio_integrity_payload {
352 struct bio *bip_bio; /* parent bio */
354 struct bvec_iter bip_iter;
356 bio_end_io_t *bip_end_io; /* saved I/O completion fn */
358 unsigned short bip_slab; /* slab the bip came from */
359 unsigned short bip_vcnt; /* # of integrity bio_vecs */
360 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
361 unsigned short bip_flags; /* control flags */
363 struct work_struct bip_work; /* I/O completion */
365 struct bio_vec *bip_vec;
366 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
369 #if defined(CONFIG_BLK_DEV_INTEGRITY)
371 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
373 if (bio->bi_rw & REQ_INTEGRITY)
374 return bio->bi_integrity;
379 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
381 struct bio_integrity_payload *bip = bio_integrity(bio);
384 return bip->bip_flags & flag;
389 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
391 return bip->bip_iter.bi_sector;
394 static inline void bip_set_seed(struct bio_integrity_payload *bip,
397 bip->bip_iter.bi_sector = seed;
400 #endif /* CONFIG_BLK_DEV_INTEGRITY */
402 extern void bio_trim(struct bio *bio, int offset, int size);
403 extern struct bio *bio_split(struct bio *bio, int sectors,
404 gfp_t gfp, struct bio_set *bs);
407 * bio_next_split - get next @sectors from a bio, splitting if necessary
409 * @sectors: number of sectors to split from the front of @bio
411 * @bs: bio set to allocate from
413 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
414 * than @sectors, returns the original bio unchanged.
416 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
417 gfp_t gfp, struct bio_set *bs)
419 if (sectors >= bio_sectors(bio))
422 return bio_split(bio, sectors, gfp, bs);
425 extern struct bio_set *bioset_create(unsigned int, unsigned int);
426 extern struct bio_set *bioset_create_nobvec(unsigned int, unsigned int);
427 extern void bioset_free(struct bio_set *);
428 extern mempool_t *biovec_create_pool(int pool_entries);
430 extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
431 extern void bio_put(struct bio *);
433 extern void __bio_clone_fast(struct bio *, struct bio *);
434 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
435 extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
437 extern struct bio_set *fs_bio_set;
439 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
441 return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
444 static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
446 return bio_clone_bioset(bio, gfp_mask, fs_bio_set);
449 static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
451 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
454 static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
456 return bio_clone_bioset(bio, gfp_mask, NULL);
460 extern void bio_endio(struct bio *);
462 static inline void bio_io_error(struct bio *bio)
464 bio->bi_error = -EIO;
468 struct request_queue;
469 extern int bio_phys_segments(struct request_queue *, struct bio *);
471 extern int submit_bio_wait(struct bio *bio);
472 extern void bio_advance(struct bio *, unsigned);
474 extern void bio_init(struct bio *);
475 extern void bio_reset(struct bio *);
476 void bio_chain(struct bio *, struct bio *);
478 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
479 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
480 unsigned int, unsigned int);
482 extern struct bio *bio_map_user_iov(struct request_queue *,
483 const struct iov_iter *, gfp_t);
484 extern void bio_unmap_user(struct bio *);
485 extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
487 extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
489 extern void bio_set_pages_dirty(struct bio *bio);
490 extern void bio_check_pages_dirty(struct bio *bio);
492 void generic_start_io_acct(int rw, unsigned long sectors,
493 struct hd_struct *part);
494 void generic_end_io_acct(int rw, struct hd_struct *part,
495 unsigned long start_time);
497 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
498 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
500 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
501 extern void bio_flush_dcache_pages(struct bio *bi);
503 static inline void bio_flush_dcache_pages(struct bio *bi)
508 extern void bio_copy_data(struct bio *dst, struct bio *src);
509 extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
511 extern struct bio *bio_copy_user_iov(struct request_queue *,
512 struct rq_map_data *,
513 const struct iov_iter *,
515 extern int bio_uncopy_user(struct bio *);
516 void zero_fill_bio(struct bio *bio);
517 extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
518 extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
519 extern unsigned int bvec_nr_vecs(unsigned short idx);
521 #ifdef CONFIG_BLK_CGROUP
522 int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
523 int bio_associate_current(struct bio *bio);
524 void bio_disassociate_task(struct bio *bio);
525 #else /* CONFIG_BLK_CGROUP */
526 static inline int bio_associate_blkcg(struct bio *bio,
527 struct cgroup_subsys_state *blkcg_css) { return 0; }
528 static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
529 static inline void bio_disassociate_task(struct bio *bio) { }
530 #endif /* CONFIG_BLK_CGROUP */
532 #ifdef CONFIG_HIGHMEM
534 * remember never ever reenable interrupts between a bvec_kmap_irq and
537 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
542 * might not be a highmem page, but the preempt/irq count
543 * balancing is a lot nicer this way
545 local_irq_save(*flags);
546 addr = (unsigned long) kmap_atomic(bvec->bv_page);
548 BUG_ON(addr & ~PAGE_MASK);
550 return (char *) addr + bvec->bv_offset;
553 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
555 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
557 kunmap_atomic((void *) ptr);
558 local_irq_restore(*flags);
562 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
564 return page_address(bvec->bv_page) + bvec->bv_offset;
567 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
573 static inline char *__bio_kmap_irq(struct bio *bio, struct bvec_iter iter,
574 unsigned long *flags)
576 return bvec_kmap_irq(&bio_iter_iovec(bio, iter), flags);
578 #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
580 #define bio_kmap_irq(bio, flags) \
581 __bio_kmap_irq((bio), (bio)->bi_iter, (flags))
582 #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
585 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
587 * A bio_list anchors a singly-linked list of bios chained through the bi_next
588 * member of the bio. The bio_list also caches the last list member to allow
589 * fast access to the tail.
596 static inline int bio_list_empty(const struct bio_list *bl)
598 return bl->head == NULL;
601 static inline void bio_list_init(struct bio_list *bl)
603 bl->head = bl->tail = NULL;
606 #define BIO_EMPTY_LIST { NULL, NULL }
608 #define bio_list_for_each(bio, bl) \
609 for (bio = (bl)->head; bio; bio = bio->bi_next)
611 static inline unsigned bio_list_size(const struct bio_list *bl)
616 bio_list_for_each(bio, bl)
622 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
627 bl->tail->bi_next = bio;
634 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
636 bio->bi_next = bl->head;
644 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
650 bl->tail->bi_next = bl2->head;
652 bl->head = bl2->head;
654 bl->tail = bl2->tail;
657 static inline void bio_list_merge_head(struct bio_list *bl,
658 struct bio_list *bl2)
664 bl2->tail->bi_next = bl->head;
666 bl->tail = bl2->tail;
668 bl->head = bl2->head;
671 static inline struct bio *bio_list_peek(struct bio_list *bl)
676 static inline struct bio *bio_list_pop(struct bio_list *bl)
678 struct bio *bio = bl->head;
681 bl->head = bl->head->bi_next;
691 static inline struct bio *bio_list_get(struct bio_list *bl)
693 struct bio *bio = bl->head;
695 bl->head = bl->tail = NULL;
701 * Increment chain count for the bio. Make sure the CHAIN flag update
702 * is visible before the raised count.
704 static inline void bio_inc_remaining(struct bio *bio)
706 bio_set_flag(bio, BIO_CHAIN);
707 smp_mb__before_atomic();
708 atomic_inc(&bio->__bi_remaining);
712 * bio_set is used to allow other portions of the IO system to
713 * allocate their own private memory pools for bio and iovec structures.
714 * These memory pools in turn all allocate from the bio_slab
715 * and the bvec_slabs[].
717 #define BIO_POOL_SIZE 2
720 struct kmem_cache *bio_slab;
721 unsigned int front_pad;
724 mempool_t *bvec_pool;
725 #if defined(CONFIG_BLK_DEV_INTEGRITY)
726 mempool_t *bio_integrity_pool;
727 mempool_t *bvec_integrity_pool;
731 * Deadlock avoidance for stacking block drivers: see comments in
732 * bio_alloc_bioset() for details
734 spinlock_t rescue_lock;
735 struct bio_list rescue_list;
736 struct work_struct rescue_work;
737 struct workqueue_struct *rescue_workqueue;
743 struct kmem_cache *slab;
747 * a small number of entries is fine, not going to be performance critical.
748 * basically we just need to survive
750 #define BIO_SPLIT_ENTRIES 2
752 #if defined(CONFIG_BLK_DEV_INTEGRITY)
754 #define bip_for_each_vec(bvl, bip, iter) \
755 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
757 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
759 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
761 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
762 extern void bio_integrity_free(struct bio *);
763 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
764 extern bool bio_integrity_enabled(struct bio *bio);
765 extern int bio_integrity_prep(struct bio *);
766 extern void bio_integrity_endio(struct bio *);
767 extern void bio_integrity_advance(struct bio *, unsigned int);
768 extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
769 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
770 extern int bioset_integrity_create(struct bio_set *, int);
771 extern void bioset_integrity_free(struct bio_set *);
772 extern void bio_integrity_init(void);
774 #else /* CONFIG_BLK_DEV_INTEGRITY */
776 static inline void *bio_integrity(struct bio *bio)
781 static inline bool bio_integrity_enabled(struct bio *bio)
786 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
791 static inline void bioset_integrity_free (struct bio_set *bs)
796 static inline int bio_integrity_prep(struct bio *bio)
801 static inline void bio_integrity_free(struct bio *bio)
806 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
812 static inline void bio_integrity_advance(struct bio *bio,
813 unsigned int bytes_done)
818 static inline void bio_integrity_trim(struct bio *bio, unsigned int offset,
819 unsigned int sectors)
824 static inline void bio_integrity_init(void)
829 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
834 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
837 return ERR_PTR(-EINVAL);
840 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
841 unsigned int len, unsigned int offset)
846 #endif /* CONFIG_BLK_DEV_INTEGRITY */
848 #endif /* CONFIG_BLOCK */
849 #endif /* __LINUX_BIO_H */