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 #ifdef CONFIG_THP_SWAP
38 #if HPAGE_PMD_NR > 256
39 #define BIO_MAX_PAGES HPAGE_PMD_NR
41 #define BIO_MAX_PAGES 256
44 #define BIO_MAX_PAGES 256
47 #define bio_prio(bio) (bio)->bi_ioprio
48 #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
50 #define bio_iter_iovec(bio, iter) \
51 bvec_iter_bvec((bio)->bi_io_vec, (iter))
53 #define bio_iter_page(bio, iter) \
54 bvec_iter_page((bio)->bi_io_vec, (iter))
55 #define bio_iter_len(bio, iter) \
56 bvec_iter_len((bio)->bi_io_vec, (iter))
57 #define bio_iter_offset(bio, iter) \
58 bvec_iter_offset((bio)->bi_io_vec, (iter))
60 #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
61 #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
62 #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
64 #define bio_multiple_segments(bio) \
65 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
67 #define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
68 #define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
70 #define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
71 #define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
74 * Return the data direction, READ or WRITE.
76 #define bio_data_dir(bio) \
77 (op_is_write(bio_op(bio)) ? WRITE : READ)
80 * Check whether this bio carries any data or not. A NULL bio is allowed.
82 static inline bool bio_has_data(struct bio *bio)
85 bio->bi_iter.bi_size &&
86 bio_op(bio) != REQ_OP_DISCARD &&
87 bio_op(bio) != REQ_OP_SECURE_ERASE &&
88 bio_op(bio) != REQ_OP_WRITE_ZEROES)
94 static inline bool bio_no_advance_iter(struct bio *bio)
96 return bio_op(bio) == REQ_OP_DISCARD ||
97 bio_op(bio) == REQ_OP_SECURE_ERASE ||
98 bio_op(bio) == REQ_OP_WRITE_SAME ||
99 bio_op(bio) == REQ_OP_WRITE_ZEROES;
102 static inline bool bio_mergeable(struct bio *bio)
104 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
110 static inline unsigned int bio_cur_bytes(struct bio *bio)
112 if (bio_has_data(bio))
113 return bio_iovec(bio).bv_len;
114 else /* dataless requests such as discard */
115 return bio->bi_iter.bi_size;
118 static inline void *bio_data(struct bio *bio)
120 if (bio_has_data(bio))
121 return page_address(bio_page(bio)) + bio_offset(bio);
126 static inline bool bio_full(struct bio *bio)
128 return bio->bi_vcnt >= bio->bi_max_vecs;
132 * drivers should _never_ use the all version - the bio may have been split
133 * before it got to the driver and the driver won't own all of it
135 #define bio_for_each_segment_all(bvl, bio, i) \
136 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
138 static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
141 iter->bi_sector += bytes >> 9;
143 if (bio_no_advance_iter(bio))
144 iter->bi_size -= bytes;
146 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
147 /* TODO: It is reasonable to complete bio with error here. */
150 #define __bio_for_each_segment(bvl, bio, iter, start) \
151 for (iter = (start); \
153 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
154 bio_advance_iter((bio), &(iter), (bvl).bv_len))
156 #define bio_for_each_segment(bvl, bio, iter) \
157 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
159 #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
161 static inline unsigned bio_segments(struct bio *bio)
165 struct bvec_iter iter;
168 * We special case discard/write same/write zeroes, because they
169 * interpret bi_size differently:
172 switch (bio_op(bio)) {
174 case REQ_OP_SECURE_ERASE:
175 case REQ_OP_WRITE_ZEROES:
177 case REQ_OP_WRITE_SAME:
183 bio_for_each_segment(bv, bio, iter)
190 * get a reference to a bio, so it won't disappear. the intended use is
194 * submit_bio(rw, bio);
195 * if (bio->bi_flags ...)
199 * without the bio_get(), it could potentially complete I/O before submit_bio
200 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
203 static inline void bio_get(struct bio *bio)
205 bio->bi_flags |= (1 << BIO_REFFED);
206 smp_mb__before_atomic();
207 atomic_inc(&bio->__bi_cnt);
210 static inline void bio_cnt_set(struct bio *bio, unsigned int count)
213 bio->bi_flags |= (1 << BIO_REFFED);
214 smp_mb__before_atomic();
216 atomic_set(&bio->__bi_cnt, count);
219 static inline bool bio_flagged(struct bio *bio, unsigned int bit)
221 return (bio->bi_flags & (1U << bit)) != 0;
224 static inline void bio_set_flag(struct bio *bio, unsigned int bit)
226 bio->bi_flags |= (1U << bit);
229 static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
231 bio->bi_flags &= ~(1U << bit);
234 static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
236 *bv = bio_iovec(bio);
239 static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
241 struct bvec_iter iter = bio->bi_iter;
244 if (unlikely(!bio_multiple_segments(bio))) {
245 *bv = bio_iovec(bio);
249 bio_advance_iter(bio, &iter, iter.bi_size);
251 if (!iter.bi_bvec_done)
252 idx = iter.bi_idx - 1;
253 else /* in the middle of bvec */
256 *bv = bio->bi_io_vec[idx];
259 * iter.bi_bvec_done records actual length of the last bvec
260 * if this bio ends in the middle of one io vector
262 if (iter.bi_bvec_done)
263 bv->bv_len = iter.bi_bvec_done;
266 static inline unsigned bio_pages_all(struct bio *bio)
268 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
272 static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
274 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
275 return bio->bi_io_vec;
278 static inline struct page *bio_first_page_all(struct bio *bio)
280 return bio_first_bvec_all(bio)->bv_page;
283 static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
285 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
286 return &bio->bi_io_vec[bio->bi_vcnt - 1];
290 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
291 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
292 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
293 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
294 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
298 * bio integrity payload
300 struct bio_integrity_payload {
301 struct bio *bip_bio; /* parent bio */
303 struct bvec_iter bip_iter;
305 unsigned short bip_slab; /* slab the bip came from */
306 unsigned short bip_vcnt; /* # of integrity bio_vecs */
307 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
308 unsigned short bip_flags; /* control flags */
310 struct bvec_iter bio_iter; /* for rewinding parent bio */
312 struct work_struct bip_work; /* I/O completion */
314 struct bio_vec *bip_vec;
315 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
318 #if defined(CONFIG_BLK_DEV_INTEGRITY)
320 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
322 if (bio->bi_opf & REQ_INTEGRITY)
323 return bio->bi_integrity;
328 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
330 struct bio_integrity_payload *bip = bio_integrity(bio);
333 return bip->bip_flags & flag;
338 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
340 return bip->bip_iter.bi_sector;
343 static inline void bip_set_seed(struct bio_integrity_payload *bip,
346 bip->bip_iter.bi_sector = seed;
349 #endif /* CONFIG_BLK_DEV_INTEGRITY */
351 extern void bio_trim(struct bio *bio, int offset, int size);
352 extern struct bio *bio_split(struct bio *bio, int sectors,
353 gfp_t gfp, struct bio_set *bs);
356 * bio_next_split - get next @sectors from a bio, splitting if necessary
358 * @sectors: number of sectors to split from the front of @bio
360 * @bs: bio set to allocate from
362 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
363 * than @sectors, returns the original bio unchanged.
365 static inline struct bio *bio_next_split(struct bio *bio, int sectors,
366 gfp_t gfp, struct bio_set *bs)
368 if (sectors >= bio_sectors(bio))
371 return bio_split(bio, sectors, gfp, bs);
375 BIOSET_NEED_BVECS = BIT(0),
376 BIOSET_NEED_RESCUER = BIT(1),
378 extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
379 extern void bioset_exit(struct bio_set *);
380 extern int biovec_init_pool(mempool_t *pool, int pool_entries);
381 extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
383 extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *);
384 extern void bio_put(struct bio *);
386 extern void __bio_clone_fast(struct bio *, struct bio *);
387 extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
389 extern struct bio_set fs_bio_set;
391 static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
393 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
396 static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
398 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
401 extern blk_qc_t submit_bio(struct bio *);
403 extern void bio_endio(struct bio *);
405 static inline void bio_io_error(struct bio *bio)
407 bio->bi_status = BLK_STS_IOERR;
411 static inline void bio_wouldblock_error(struct bio *bio)
413 bio->bi_status = BLK_STS_AGAIN;
417 struct request_queue;
418 extern int bio_phys_segments(struct request_queue *, struct bio *);
420 extern int submit_bio_wait(struct bio *bio);
421 extern void bio_advance(struct bio *, unsigned);
423 extern void bio_init(struct bio *bio, struct bio_vec *table,
424 unsigned short max_vecs);
425 extern void bio_uninit(struct bio *);
426 extern void bio_reset(struct bio *);
427 void bio_chain(struct bio *, struct bio *);
429 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
430 extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
431 unsigned int, unsigned int);
432 bool __bio_try_merge_page(struct bio *bio, struct page *page,
433 unsigned int len, unsigned int off);
434 void __bio_add_page(struct bio *bio, struct page *page,
435 unsigned int len, unsigned int off);
436 int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
438 extern struct bio *bio_map_user_iov(struct request_queue *,
439 struct iov_iter *, gfp_t);
440 extern void bio_unmap_user(struct bio *);
441 extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
443 extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
445 extern void bio_set_pages_dirty(struct bio *bio);
446 extern void bio_check_pages_dirty(struct bio *bio);
448 void generic_start_io_acct(struct request_queue *q, int op,
449 unsigned long sectors, struct hd_struct *part);
450 void generic_end_io_acct(struct request_queue *q, int op,
451 struct hd_struct *part,
452 unsigned long start_time);
454 #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
455 # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
457 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
458 extern void bio_flush_dcache_pages(struct bio *bi);
460 static inline void bio_flush_dcache_pages(struct bio *bi)
465 extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
466 struct bio *src, struct bvec_iter *src_iter);
467 extern void bio_copy_data(struct bio *dst, struct bio *src);
468 extern void bio_list_copy_data(struct bio *dst, struct bio *src);
469 extern void bio_free_pages(struct bio *bio);
471 extern struct bio *bio_copy_user_iov(struct request_queue *,
472 struct rq_map_data *,
475 extern int bio_uncopy_user(struct bio *);
476 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
478 static inline void zero_fill_bio(struct bio *bio)
480 zero_fill_bio_iter(bio, bio->bi_iter);
483 extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
484 extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
485 extern unsigned int bvec_nr_vecs(unsigned short idx);
486 extern const char *bio_devname(struct bio *bio, char *buffer);
488 #define bio_set_dev(bio, bdev) \
490 if ((bio)->bi_disk != (bdev)->bd_disk) \
491 bio_clear_flag(bio, BIO_THROTTLED);\
492 (bio)->bi_disk = (bdev)->bd_disk; \
493 (bio)->bi_partno = (bdev)->bd_partno; \
496 #define bio_copy_dev(dst, src) \
498 (dst)->bi_disk = (src)->bi_disk; \
499 (dst)->bi_partno = (src)->bi_partno; \
502 #define bio_dev(bio) \
503 disk_devt((bio)->bi_disk)
505 #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
506 int bio_associate_blkcg_from_page(struct bio *bio, struct page *page);
508 static inline int bio_associate_blkcg_from_page(struct bio *bio,
509 struct page *page) { return 0; }
512 #ifdef CONFIG_BLK_CGROUP
513 int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
514 int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg);
515 void bio_disassociate_task(struct bio *bio);
516 void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
517 #else /* CONFIG_BLK_CGROUP */
518 static inline int bio_associate_blkcg(struct bio *bio,
519 struct cgroup_subsys_state *blkcg_css) { return 0; }
520 static inline void bio_disassociate_task(struct bio *bio) { }
521 static inline void bio_clone_blkcg_association(struct bio *dst,
523 #endif /* CONFIG_BLK_CGROUP */
525 #ifdef CONFIG_HIGHMEM
527 * remember never ever reenable interrupts between a bvec_kmap_irq and
530 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
535 * might not be a highmem page, but the preempt/irq count
536 * balancing is a lot nicer this way
538 local_irq_save(*flags);
539 addr = (unsigned long) kmap_atomic(bvec->bv_page);
541 BUG_ON(addr & ~PAGE_MASK);
543 return (char *) addr + bvec->bv_offset;
546 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
548 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
550 kunmap_atomic((void *) ptr);
551 local_irq_restore(*flags);
555 static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
557 return page_address(bvec->bv_page) + bvec->bv_offset;
560 static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
567 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
569 * A bio_list anchors a singly-linked list of bios chained through the bi_next
570 * member of the bio. The bio_list also caches the last list member to allow
571 * fast access to the tail.
578 static inline int bio_list_empty(const struct bio_list *bl)
580 return bl->head == NULL;
583 static inline void bio_list_init(struct bio_list *bl)
585 bl->head = bl->tail = NULL;
588 #define BIO_EMPTY_LIST { NULL, NULL }
590 #define bio_list_for_each(bio, bl) \
591 for (bio = (bl)->head; bio; bio = bio->bi_next)
593 static inline unsigned bio_list_size(const struct bio_list *bl)
598 bio_list_for_each(bio, bl)
604 static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
609 bl->tail->bi_next = bio;
616 static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
618 bio->bi_next = bl->head;
626 static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
632 bl->tail->bi_next = bl2->head;
634 bl->head = bl2->head;
636 bl->tail = bl2->tail;
639 static inline void bio_list_merge_head(struct bio_list *bl,
640 struct bio_list *bl2)
646 bl2->tail->bi_next = bl->head;
648 bl->tail = bl2->tail;
650 bl->head = bl2->head;
653 static inline struct bio *bio_list_peek(struct bio_list *bl)
658 static inline struct bio *bio_list_pop(struct bio_list *bl)
660 struct bio *bio = bl->head;
663 bl->head = bl->head->bi_next;
673 static inline struct bio *bio_list_get(struct bio_list *bl)
675 struct bio *bio = bl->head;
677 bl->head = bl->tail = NULL;
683 * Increment chain count for the bio. Make sure the CHAIN flag update
684 * is visible before the raised count.
686 static inline void bio_inc_remaining(struct bio *bio)
688 bio_set_flag(bio, BIO_CHAIN);
689 smp_mb__before_atomic();
690 atomic_inc(&bio->__bi_remaining);
694 * bio_set is used to allow other portions of the IO system to
695 * allocate their own private memory pools for bio and iovec structures.
696 * These memory pools in turn all allocate from the bio_slab
697 * and the bvec_slabs[].
699 #define BIO_POOL_SIZE 2
702 struct kmem_cache *bio_slab;
703 unsigned int front_pad;
707 #if defined(CONFIG_BLK_DEV_INTEGRITY)
708 mempool_t bio_integrity_pool;
709 mempool_t bvec_integrity_pool;
713 * Deadlock avoidance for stacking block drivers: see comments in
714 * bio_alloc_bioset() for details
716 spinlock_t rescue_lock;
717 struct bio_list rescue_list;
718 struct work_struct rescue_work;
719 struct workqueue_struct *rescue_workqueue;
725 struct kmem_cache *slab;
728 static inline bool bioset_initialized(struct bio_set *bs)
730 return bs->bio_slab != NULL;
734 * a small number of entries is fine, not going to be performance critical.
735 * basically we just need to survive
737 #define BIO_SPLIT_ENTRIES 2
739 #if defined(CONFIG_BLK_DEV_INTEGRITY)
741 #define bip_for_each_vec(bvl, bip, iter) \
742 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
744 #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
746 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
748 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
749 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
750 extern bool bio_integrity_prep(struct bio *);
751 extern void bio_integrity_advance(struct bio *, unsigned int);
752 extern void bio_integrity_trim(struct bio *);
753 extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
754 extern int bioset_integrity_create(struct bio_set *, int);
755 extern void bioset_integrity_free(struct bio_set *);
756 extern void bio_integrity_init(void);
758 #else /* CONFIG_BLK_DEV_INTEGRITY */
760 static inline void *bio_integrity(struct bio *bio)
765 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
770 static inline void bioset_integrity_free (struct bio_set *bs)
775 static inline bool bio_integrity_prep(struct bio *bio)
780 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
786 static inline void bio_integrity_advance(struct bio *bio,
787 unsigned int bytes_done)
792 static inline void bio_integrity_trim(struct bio *bio)
797 static inline void bio_integrity_init(void)
802 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
807 static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
810 return ERR_PTR(-EINVAL);
813 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
814 unsigned int len, unsigned int offset)
819 #endif /* CONFIG_BLK_DEV_INTEGRITY */
821 #endif /* CONFIG_BLOCK */
822 #endif /* __LINUX_BIO_H */