Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * 2.5 block I/O model |
| 3 | * |
| 4 | * Copyright (C) 2001 Jens Axboe <axboe@suse.de> |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Tejun Heo | 7cc0158 | 2010-08-03 13:14:58 +0200 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 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- |
| 19 | */ |
| 20 | #ifndef __LINUX_BIO_H |
| 21 | #define __LINUX_BIO_H |
| 22 | |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/mempool.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 25 | #include <linux/ioprio.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 26 | #include <linux/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Howells | 02a5e0a | 2007-08-11 22:34:32 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_BLOCK |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/io.h> |
| 31 | |
Tejun Heo | 7cc0158 | 2010-08-03 13:14:58 +0200 | [diff] [blame] | 32 | /* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */ |
| 33 | #include <linux/blk_types.h> |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define BIO_DEBUG |
| 36 | |
| 37 | #ifdef BIO_DEBUG |
| 38 | #define BIO_BUG_ON BUG_ON |
| 39 | #else |
| 40 | #define BIO_BUG_ON |
| 41 | #endif |
| 42 | |
Alexey Dobriyan | d84a847 | 2006-06-25 05:49:32 -0700 | [diff] [blame] | 43 | #define BIO_MAX_PAGES 256 |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 44 | #define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9) |
| 46 | |
Mike Christie | f215082 | 2016-06-05 14:31:42 -0500 | [diff] [blame] | 47 | #define bio_op(bio) (op_from_rq_bits((bio)->bi_rw)) |
| 48 | #define bio_set_op_attrs(bio, op, flags) ((bio)->bi_rw |= (op | flags)) |
| 49 | |
Mike Christie | 43b62ce | 2016-06-05 14:32:20 -0500 | [diff] [blame^] | 50 | #define bio_prio(bio) (bio)->bi_ioprio |
| 51 | #define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 52 | |
| 53 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * various member access, note that bio_data should of course not be used |
| 55 | * on highmem page vectors |
| 56 | */ |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 57 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) |
Kent Overstreet | a4ad39b1 | 2013-08-07 14:24:32 -0700 | [diff] [blame] | 58 | |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 59 | #define bvec_iter_page(bvec, iter) \ |
| 60 | (__bvec_iter_bvec((bvec), (iter))->bv_page) |
| 61 | |
| 62 | #define bvec_iter_len(bvec, iter) \ |
| 63 | min((iter).bi_size, \ |
| 64 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) |
| 65 | |
| 66 | #define bvec_iter_offset(bvec, iter) \ |
| 67 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) |
| 68 | |
| 69 | #define bvec_iter_bvec(bvec, iter) \ |
| 70 | ((struct bio_vec) { \ |
| 71 | .bv_page = bvec_iter_page((bvec), (iter)), \ |
| 72 | .bv_len = bvec_iter_len((bvec), (iter)), \ |
| 73 | .bv_offset = bvec_iter_offset((bvec), (iter)), \ |
| 74 | }) |
| 75 | |
| 76 | #define bio_iter_iovec(bio, iter) \ |
| 77 | bvec_iter_bvec((bio)->bi_io_vec, (iter)) |
| 78 | |
| 79 | #define bio_iter_page(bio, iter) \ |
| 80 | bvec_iter_page((bio)->bi_io_vec, (iter)) |
| 81 | #define bio_iter_len(bio, iter) \ |
| 82 | bvec_iter_len((bio)->bi_io_vec, (iter)) |
| 83 | #define bio_iter_offset(bio, iter) \ |
| 84 | bvec_iter_offset((bio)->bi_io_vec, (iter)) |
| 85 | |
| 86 | #define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter) |
| 87 | #define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter) |
| 88 | #define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter) |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 89 | |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 90 | #define bio_multiple_segments(bio) \ |
| 91 | ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 92 | #define bio_sectors(bio) ((bio)->bi_iter.bi_size >> 9) |
| 93 | #define bio_end_sector(bio) ((bio)->bi_iter.bi_sector + bio_sectors((bio))) |
Jens Axboe | bf2de6f | 2007-09-27 13:01:25 +0200 | [diff] [blame] | 94 | |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 95 | /* |
| 96 | * Check whether this bio carries any data or not. A NULL bio is allowed. |
| 97 | */ |
| 98 | static inline bool bio_has_data(struct bio *bio) |
| 99 | { |
| 100 | if (bio && |
| 101 | bio->bi_iter.bi_size && |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 102 | bio_op(bio) != REQ_OP_DISCARD) |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 103 | return true; |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 108 | static inline bool bio_no_advance_iter(struct bio *bio) |
| 109 | { |
| 110 | return bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_WRITE_SAME; |
| 111 | } |
| 112 | |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 113 | static inline bool bio_is_rw(struct bio *bio) |
| 114 | { |
| 115 | if (!bio_has_data(bio)) |
| 116 | return false; |
| 117 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 118 | if (bio_no_advance_iter(bio)) |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 119 | return false; |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | static inline bool bio_mergeable(struct bio *bio) |
| 125 | { |
| 126 | if (bio->bi_rw & REQ_NOMERGE_FLAGS) |
| 127 | return false; |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 132 | static inline unsigned int bio_cur_bytes(struct bio *bio) |
Jens Axboe | bf2de6f | 2007-09-27 13:01:25 +0200 | [diff] [blame] | 133 | { |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 134 | if (bio_has_data(bio)) |
Kent Overstreet | a4ad39b1 | 2013-08-07 14:24:32 -0700 | [diff] [blame] | 135 | return bio_iovec(bio).bv_len; |
David Woodhouse | fb2dce8 | 2008-08-05 18:01:53 +0100 | [diff] [blame] | 136 | else /* dataless requests such as discard */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 137 | return bio->bi_iter.bi_size; |
Jens Axboe | bf2de6f | 2007-09-27 13:01:25 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static inline void *bio_data(struct bio *bio) |
| 141 | { |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 142 | if (bio_has_data(bio)) |
Jens Axboe | bf2de6f | 2007-09-27 13:01:25 +0200 | [diff] [blame] | 143 | return page_address(bio_page(bio)) + bio_offset(bio); |
| 144 | |
| 145 | return NULL; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * will die |
| 150 | */ |
| 151 | #define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio))) |
| 152 | #define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset) |
| 153 | |
| 154 | /* |
| 155 | * queues that have highmem support enabled may still need to revert to |
| 156 | * PIO transfers occasionally and thus map high pages temporarily. For |
| 157 | * permanent PIO fall back, user is probably better off disabling highmem |
| 158 | * I/O completely on that queue (see ide-dma for example) |
| 159 | */ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 160 | #define __bio_kmap_atomic(bio, iter) \ |
| 161 | (kmap_atomic(bio_iter_iovec((bio), (iter)).bv_page) + \ |
| 162 | bio_iter_iovec((bio), (iter)).bv_offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 164 | #define __bio_kunmap_atomic(addr) kunmap_atomic(addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * merge helpers etc |
| 168 | */ |
| 169 | |
Jeremy Fitzhardinge | f92131c | 2008-10-29 14:10:51 +0100 | [diff] [blame] | 170 | /* Default implementation of BIOVEC_PHYS_MERGEABLE */ |
| 171 | #define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \ |
| 172 | ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2))) |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* |
| 175 | * allow arch override, for eg virtualized architectures (put in asm/io.h) |
| 176 | */ |
| 177 | #ifndef BIOVEC_PHYS_MERGEABLE |
| 178 | #define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \ |
Jeremy Fitzhardinge | f92131c | 2008-10-29 14:10:51 +0100 | [diff] [blame] | 179 | __BIOVEC_PHYS_MERGEABLE(vec1, vec2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | #endif |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | #define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \ |
| 183 | (((addr1) | (mask)) == (((addr2) - 1) | (mask))) |
| 184 | #define BIOVEC_SEG_BOUNDARY(q, b1, b2) \ |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 185 | __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Jens Axboe | 66cb45a | 2014-06-24 16:22:24 -0600 | [diff] [blame] | 187 | /* |
Kent Overstreet | d74c6d5 | 2013-02-06 12:23:11 -0800 | [diff] [blame] | 188 | * drivers should _never_ use the all version - the bio may have been split |
| 189 | * before it got to the driver and the driver won't own all of it |
| 190 | */ |
| 191 | #define bio_for_each_segment_all(bvl, bio, i) \ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 192 | for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++) |
Kent Overstreet | d74c6d5 | 2013-02-06 12:23:11 -0800 | [diff] [blame] | 193 | |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 194 | static inline void bvec_iter_advance(struct bio_vec *bv, struct bvec_iter *iter, |
| 195 | unsigned bytes) |
| 196 | { |
| 197 | WARN_ONCE(bytes > iter->bi_size, |
| 198 | "Attempted to advance past end of bvec iter\n"); |
| 199 | |
| 200 | while (bytes) { |
| 201 | unsigned len = min(bytes, bvec_iter_len(bv, *iter)); |
| 202 | |
| 203 | bytes -= len; |
| 204 | iter->bi_size -= len; |
| 205 | iter->bi_bvec_done += len; |
| 206 | |
| 207 | if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { |
| 208 | iter->bi_bvec_done = 0; |
| 209 | iter->bi_idx++; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #define for_each_bvec(bvl, bio_vec, iter, start) \ |
Martin K. Petersen | b7aa84d | 2014-04-08 22:43:43 -0400 | [diff] [blame] | 215 | for (iter = (start); \ |
| 216 | (iter).bi_size && \ |
| 217 | ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \ |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 218 | bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len)) |
| 219 | |
| 220 | |
| 221 | static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter, |
| 222 | unsigned bytes) |
| 223 | { |
| 224 | iter->bi_sector += bytes >> 9; |
| 225 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 226 | if (bio_no_advance_iter(bio)) |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 227 | iter->bi_size -= bytes; |
| 228 | else |
| 229 | bvec_iter_advance(bio->bi_io_vec, iter, bytes); |
| 230 | } |
| 231 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 232 | #define __bio_for_each_segment(bvl, bio, iter, start) \ |
| 233 | for (iter = (start); \ |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 234 | (iter).bi_size && \ |
| 235 | ((bvl = bio_iter_iovec((bio), (iter))), 1); \ |
| 236 | bio_advance_iter((bio), &(iter), (bvl).bv_len)) |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 237 | |
| 238 | #define bio_for_each_segment(bvl, bio, iter) \ |
| 239 | __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter) |
| 240 | |
Kent Overstreet | 4550dd6 | 2013-08-07 14:26:21 -0700 | [diff] [blame] | 241 | #define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 243 | static inline unsigned bio_segments(struct bio *bio) |
| 244 | { |
| 245 | unsigned segs = 0; |
| 246 | struct bio_vec bv; |
| 247 | struct bvec_iter iter; |
| 248 | |
Kent Overstreet | 8423ae3 | 2014-02-10 17:45:50 -0800 | [diff] [blame] | 249 | /* |
| 250 | * We special case discard/write same, because they interpret bi_size |
| 251 | * differently: |
| 252 | */ |
| 253 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 254 | if (bio_op(bio) == REQ_OP_DISCARD) |
Kent Overstreet | 8423ae3 | 2014-02-10 17:45:50 -0800 | [diff] [blame] | 255 | return 1; |
| 256 | |
Mike Christie | 95fe6c1 | 2016-06-05 14:31:48 -0500 | [diff] [blame] | 257 | if (bio_op(bio) == REQ_OP_WRITE_SAME) |
Kent Overstreet | 8423ae3 | 2014-02-10 17:45:50 -0800 | [diff] [blame] | 258 | return 1; |
| 259 | |
Kent Overstreet | 458b76e | 2013-09-24 16:26:05 -0700 | [diff] [blame] | 260 | bio_for_each_segment(bv, bio, iter) |
| 261 | segs++; |
| 262 | |
| 263 | return segs; |
| 264 | } |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | /* |
| 267 | * get a reference to a bio, so it won't disappear. the intended use is |
| 268 | * something like: |
| 269 | * |
| 270 | * bio_get(bio); |
| 271 | * submit_bio(rw, bio); |
| 272 | * if (bio->bi_flags ...) |
| 273 | * do_something |
| 274 | * bio_put(bio); |
| 275 | * |
| 276 | * without the bio_get(), it could potentially complete I/O before submit_bio |
| 277 | * returns. and then bio would be freed memory when if (bio->bi_flags ...) |
| 278 | * runs |
| 279 | */ |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 280 | static inline void bio_get(struct bio *bio) |
| 281 | { |
| 282 | bio->bi_flags |= (1 << BIO_REFFED); |
| 283 | smp_mb__before_atomic(); |
| 284 | atomic_inc(&bio->__bi_cnt); |
| 285 | } |
| 286 | |
| 287 | static inline void bio_cnt_set(struct bio *bio, unsigned int count) |
| 288 | { |
| 289 | if (count != 1) { |
| 290 | bio->bi_flags |= (1 << BIO_REFFED); |
| 291 | smp_mb__before_atomic(); |
| 292 | } |
| 293 | atomic_set(&bio->__bi_cnt, count); |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 296 | static inline bool bio_flagged(struct bio *bio, unsigned int bit) |
| 297 | { |
Jens Axboe | 2c68f6d | 2015-07-28 13:14:32 -0600 | [diff] [blame] | 298 | return (bio->bi_flags & (1U << bit)) != 0; |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static inline void bio_set_flag(struct bio *bio, unsigned int bit) |
| 302 | { |
Jens Axboe | 2c68f6d | 2015-07-28 13:14:32 -0600 | [diff] [blame] | 303 | bio->bi_flags |= (1U << bit); |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static inline void bio_clear_flag(struct bio *bio, unsigned int bit) |
| 307 | { |
Jens Axboe | 2c68f6d | 2015-07-28 13:14:32 -0600 | [diff] [blame] | 308 | bio->bi_flags &= ~(1U << bit); |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 309 | } |
| 310 | |
Ming Lei | 7bcd79a | 2016-02-26 23:40:50 +0800 | [diff] [blame] | 311 | static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) |
| 312 | { |
| 313 | *bv = bio_iovec(bio); |
| 314 | } |
| 315 | |
| 316 | static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) |
| 317 | { |
| 318 | struct bvec_iter iter = bio->bi_iter; |
| 319 | int idx; |
| 320 | |
Ming Lei | 7bcd79a | 2016-02-26 23:40:50 +0800 | [diff] [blame] | 321 | if (unlikely(!bio_multiple_segments(bio))) { |
| 322 | *bv = bio_iovec(bio); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | bio_advance_iter(bio, &iter, iter.bi_size); |
| 327 | |
| 328 | if (!iter.bi_bvec_done) |
| 329 | idx = iter.bi_idx - 1; |
| 330 | else /* in the middle of bvec */ |
| 331 | idx = iter.bi_idx; |
| 332 | |
| 333 | *bv = bio->bi_io_vec[idx]; |
| 334 | |
| 335 | /* |
| 336 | * iter.bi_bvec_done records actual length of the last bvec |
| 337 | * if this bio ends in the middle of one io vector |
| 338 | */ |
| 339 | if (iter.bi_bvec_done) |
| 340 | bv->bv_len = iter.bi_bvec_done; |
| 341 | } |
| 342 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 343 | enum bip_flags { |
| 344 | BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */ |
| 345 | BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */ |
| 346 | BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */ |
| 347 | BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */ |
| 348 | BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */ |
| 349 | }; |
| 350 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 351 | /* |
| 352 | * bio integrity payload |
| 353 | */ |
| 354 | struct bio_integrity_payload { |
| 355 | struct bio *bip_bio; /* parent bio */ |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 356 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 357 | struct bvec_iter bip_iter; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 358 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 359 | bio_end_io_t *bip_end_io; /* saved I/O completion fn */ |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 360 | |
Martin K. Petersen | 7878cba | 2009-06-26 15:37:49 +0200 | [diff] [blame] | 361 | unsigned short bip_slab; /* slab the bip came from */ |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 362 | unsigned short bip_vcnt; /* # of integrity bio_vecs */ |
Gu Zheng | cbcd1054 | 2014-07-01 10:36:47 -0600 | [diff] [blame] | 363 | unsigned short bip_max_vcnt; /* integrity bio_vec slots */ |
Martin K. Petersen | b1f013885 | 2014-09-26 19:20:04 -0400 | [diff] [blame] | 364 | unsigned short bip_flags; /* control flags */ |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 365 | |
| 366 | struct work_struct bip_work; /* I/O completion */ |
Kent Overstreet | 6fda981 | 2012-10-12 13:18:27 -0700 | [diff] [blame] | 367 | |
| 368 | struct bio_vec *bip_vec; |
| 369 | struct bio_vec bip_inline_vecs[0];/* embedded bvec array */ |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 370 | }; |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 371 | |
Keith Busch | 06c1e39 | 2015-12-03 09:32:21 -0700 | [diff] [blame] | 372 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 373 | |
| 374 | static inline struct bio_integrity_payload *bio_integrity(struct bio *bio) |
| 375 | { |
| 376 | if (bio->bi_rw & REQ_INTEGRITY) |
| 377 | return bio->bi_integrity; |
| 378 | |
| 379 | return NULL; |
| 380 | } |
| 381 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 382 | static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag) |
| 383 | { |
| 384 | struct bio_integrity_payload *bip = bio_integrity(bio); |
| 385 | |
| 386 | if (bip) |
| 387 | return bip->bip_flags & flag; |
| 388 | |
| 389 | return false; |
| 390 | } |
Martin K. Petersen | b1f013885 | 2014-09-26 19:20:04 -0400 | [diff] [blame] | 391 | |
Martin K. Petersen | 1859308 | 2014-09-26 19:20:01 -0400 | [diff] [blame] | 392 | static inline sector_t bip_get_seed(struct bio_integrity_payload *bip) |
| 393 | { |
| 394 | return bip->bip_iter.bi_sector; |
| 395 | } |
| 396 | |
| 397 | static inline void bip_set_seed(struct bio_integrity_payload *bip, |
| 398 | sector_t seed) |
| 399 | { |
| 400 | bip->bip_iter.bi_sector = seed; |
| 401 | } |
| 402 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 403 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Kent Overstreet | 6678d83 | 2013-08-07 11:14:32 -0700 | [diff] [blame] | 405 | extern void bio_trim(struct bio *bio, int offset, int size); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 406 | extern struct bio *bio_split(struct bio *bio, int sectors, |
| 407 | gfp_t gfp, struct bio_set *bs); |
| 408 | |
| 409 | /** |
| 410 | * bio_next_split - get next @sectors from a bio, splitting if necessary |
| 411 | * @bio: bio to split |
| 412 | * @sectors: number of sectors to split from the front of @bio |
| 413 | * @gfp: gfp mask |
| 414 | * @bs: bio set to allocate from |
| 415 | * |
| 416 | * Returns a bio representing the next @sectors of @bio - if the bio is smaller |
| 417 | * than @sectors, returns the original bio unchanged. |
| 418 | */ |
| 419 | static inline struct bio *bio_next_split(struct bio *bio, int sectors, |
| 420 | gfp_t gfp, struct bio_set *bs) |
| 421 | { |
| 422 | if (sectors >= bio_sectors(bio)) |
| 423 | return bio; |
| 424 | |
| 425 | return bio_split(bio, sectors, gfp, bs); |
| 426 | } |
| 427 | |
Jens Axboe | bb799ca | 2008-12-10 15:35:05 +0100 | [diff] [blame] | 428 | extern struct bio_set *bioset_create(unsigned int, unsigned int); |
Junichi Nomura | d8f429e | 2014-10-03 17:27:12 -0400 | [diff] [blame] | 429 | extern struct bio_set *bioset_create_nobvec(unsigned int, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | extern void bioset_free(struct bio_set *); |
Fabian Frederick | a6c39cb4f | 2014-04-22 15:09:05 -0600 | [diff] [blame] | 431 | extern mempool_t *biovec_create_pool(int pool_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 433 | extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | extern void bio_put(struct bio *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Kent Overstreet | 59d276f | 2013-11-23 18:19:27 -0800 | [diff] [blame] | 436 | extern void __bio_clone_fast(struct bio *, struct bio *); |
| 437 | extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *); |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 438 | extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs); |
| 439 | |
Kent Overstreet | 3f86a82 | 2012-09-06 15:35:01 -0700 | [diff] [blame] | 440 | extern struct bio_set *fs_bio_set; |
| 441 | |
| 442 | static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 443 | { |
| 444 | return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set); |
| 445 | } |
| 446 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 447 | static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask) |
| 448 | { |
| 449 | return bio_clone_bioset(bio, gfp_mask, fs_bio_set); |
| 450 | } |
| 451 | |
Kent Overstreet | 3f86a82 | 2012-09-06 15:35:01 -0700 | [diff] [blame] | 452 | static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs) |
| 453 | { |
| 454 | return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL); |
| 455 | } |
| 456 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 457 | static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask) |
| 458 | { |
| 459 | return bio_clone_bioset(bio, gfp_mask, NULL); |
| 460 | |
| 461 | } |
| 462 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 463 | extern void bio_endio(struct bio *); |
| 464 | |
| 465 | static inline void bio_io_error(struct bio *bio) |
| 466 | { |
| 467 | bio->bi_error = -EIO; |
| 468 | bio_endio(bio); |
| 469 | } |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct request_queue; |
| 472 | extern int bio_phys_segments(struct request_queue *, struct bio *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 474 | extern int submit_bio_wait(struct bio *bio); |
Kent Overstreet | 054bdf6 | 2012-09-28 13:17:55 -0700 | [diff] [blame] | 475 | extern void bio_advance(struct bio *, unsigned); |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | extern void bio_init(struct bio *); |
Kent Overstreet | f44b48c | 2012-09-06 15:34:58 -0700 | [diff] [blame] | 478 | extern void bio_reset(struct bio *); |
Kent Overstreet | 196d38bc | 2013-11-23 18:34:15 -0800 | [diff] [blame] | 479 | void bio_chain(struct bio *, struct bio *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int); |
Mike Christie | 6e68af6 | 2005-11-11 05:30:27 -0600 | [diff] [blame] | 482 | extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, |
| 483 | unsigned int, unsigned int); |
FUJITA Tomonori | 152e283 | 2008-08-28 16:17:06 +0900 | [diff] [blame] | 484 | struct rq_map_data; |
James Bottomley | f1970ba | 2005-06-20 14:06:52 +0200 | [diff] [blame] | 485 | extern struct bio *bio_map_user_iov(struct request_queue *, |
Kent Overstreet | 26e49cf | 2015-01-18 16:16:31 +0100 | [diff] [blame] | 486 | const struct iov_iter *, gfp_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | extern void bio_unmap_user(struct bio *); |
Mike Christie | df46b9a | 2005-06-20 14:04:44 +0200 | [diff] [blame] | 488 | extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int, |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 489 | gfp_t); |
FUJITA Tomonori | 68154e9 | 2008-04-25 12:47:50 +0200 | [diff] [blame] | 490 | extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int, |
| 491 | gfp_t, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | extern void bio_set_pages_dirty(struct bio *bio); |
| 493 | extern void bio_check_pages_dirty(struct bio *bio); |
Ilya Loginov | 2d4dc89 | 2009-11-26 09:16:19 +0100 | [diff] [blame] | 494 | |
Gu Zheng | 394ffa5 | 2014-11-24 11:05:22 +0800 | [diff] [blame] | 495 | void generic_start_io_acct(int rw, unsigned long sectors, |
| 496 | struct hd_struct *part); |
| 497 | void generic_end_io_acct(int rw, struct hd_struct *part, |
| 498 | unsigned long start_time); |
| 499 | |
Ilya Loginov | 2d4dc89 | 2009-11-26 09:16:19 +0100 | [diff] [blame] | 500 | #ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
| 501 | # error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform" |
| 502 | #endif |
| 503 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
| 504 | extern void bio_flush_dcache_pages(struct bio *bi); |
| 505 | #else |
| 506 | static inline void bio_flush_dcache_pages(struct bio *bi) |
| 507 | { |
| 508 | } |
| 509 | #endif |
| 510 | |
Kent Overstreet | 16ac3d6 | 2012-09-10 13:57:51 -0700 | [diff] [blame] | 511 | extern void bio_copy_data(struct bio *dst, struct bio *src); |
Kent Overstreet | a078760 | 2012-09-10 14:03:28 -0700 | [diff] [blame] | 512 | extern int bio_alloc_pages(struct bio *bio, gfp_t gfp); |
Kent Overstreet | 16ac3d6 | 2012-09-10 13:57:51 -0700 | [diff] [blame] | 513 | |
FUJITA Tomonori | 152e283 | 2008-08-28 16:17:06 +0900 | [diff] [blame] | 514 | extern struct bio *bio_copy_user_iov(struct request_queue *, |
Al Viro | 86d564c | 2014-02-08 20:42:52 -0500 | [diff] [blame] | 515 | struct rq_map_data *, |
Kent Overstreet | 26e49cf | 2015-01-18 16:16:31 +0100 | [diff] [blame] | 516 | const struct iov_iter *, |
| 517 | gfp_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | extern int bio_uncopy_user(struct bio *); |
| 519 | void zero_fill_bio(struct bio *bio); |
Kent Overstreet | 9f060e2 | 2012-10-12 15:29:33 -0700 | [diff] [blame] | 520 | extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *); |
| 521 | extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 522 | extern unsigned int bvec_nr_vecs(unsigned short idx); |
Martin K. Petersen | 51d654e | 2008-06-17 18:59:56 +0200 | [diff] [blame] | 523 | |
Tejun Heo | 852c788 | 2012-03-05 13:15:27 -0800 | [diff] [blame] | 524 | #ifdef CONFIG_BLK_CGROUP |
Tejun Heo | 1d933cf | 2015-05-22 17:13:24 -0400 | [diff] [blame] | 525 | int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css); |
Tejun Heo | 852c788 | 2012-03-05 13:15:27 -0800 | [diff] [blame] | 526 | int bio_associate_current(struct bio *bio); |
| 527 | void bio_disassociate_task(struct bio *bio); |
| 528 | #else /* CONFIG_BLK_CGROUP */ |
Tejun Heo | 1d933cf | 2015-05-22 17:13:24 -0400 | [diff] [blame] | 529 | static inline int bio_associate_blkcg(struct bio *bio, |
| 530 | struct cgroup_subsys_state *blkcg_css) { return 0; } |
Tejun Heo | 852c788 | 2012-03-05 13:15:27 -0800 | [diff] [blame] | 531 | static inline int bio_associate_current(struct bio *bio) { return -ENOENT; } |
| 532 | static inline void bio_disassociate_task(struct bio *bio) { } |
| 533 | #endif /* CONFIG_BLK_CGROUP */ |
| 534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | #ifdef CONFIG_HIGHMEM |
| 536 | /* |
Alberto Bertogli | 20b636b | 2009-02-02 12:41:07 +0100 | [diff] [blame] | 537 | * remember never ever reenable interrupts between a bvec_kmap_irq and |
| 538 | * bvec_kunmap_irq! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | */ |
Alberto Bertogli | 4f570f9 | 2009-11-02 11:40:16 +0100 | [diff] [blame] | 540 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
| 542 | unsigned long addr; |
| 543 | |
| 544 | /* |
| 545 | * might not be a highmem page, but the preempt/irq count |
| 546 | * balancing is a lot nicer this way |
| 547 | */ |
| 548 | local_irq_save(*flags); |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 549 | addr = (unsigned long) kmap_atomic(bvec->bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | BUG_ON(addr & ~PAGE_MASK); |
| 552 | |
| 553 | return (char *) addr + bvec->bv_offset; |
| 554 | } |
| 555 | |
Alberto Bertogli | 4f570f9 | 2009-11-02 11:40:16 +0100 | [diff] [blame] | 556 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
| 558 | unsigned long ptr = (unsigned long) buffer & PAGE_MASK; |
| 559 | |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 560 | kunmap_atomic((void *) ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | local_irq_restore(*flags); |
| 562 | } |
| 563 | |
| 564 | #else |
Geert Uytterhoeven | 11a691b | 2010-10-21 10:32:29 +0200 | [diff] [blame] | 565 | static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags) |
| 566 | { |
| 567 | return page_address(bvec->bv_page) + bvec->bv_offset; |
| 568 | } |
| 569 | |
| 570 | static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags) |
| 571 | { |
| 572 | *flags = 0; |
| 573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | #endif |
| 575 | |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 576 | static inline char *__bio_kmap_irq(struct bio *bio, struct bvec_iter iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | unsigned long *flags) |
| 578 | { |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 579 | return bvec_kmap_irq(&bio_iter_iovec(bio, iter), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | #define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags) |
| 582 | |
| 583 | #define bio_kmap_irq(bio, flags) \ |
Kent Overstreet | f619d25 | 2013-08-07 14:30:33 -0700 | [diff] [blame] | 584 | __bio_kmap_irq((bio), (bio)->bi_iter, (flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | #define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags) |
| 586 | |
Jens Axboe | 7a67f63 | 2008-08-08 11:17:12 +0200 | [diff] [blame] | 587 | /* |
Akinobu Mita | e686307 | 2009-04-17 08:41:21 +0200 | [diff] [blame] | 588 | * BIO list management for use by remapping drivers (e.g. DM or MD) and loop. |
Christoph Hellwig | 8f3d8ba | 2009-04-07 19:55:13 +0200 | [diff] [blame] | 589 | * |
| 590 | * A bio_list anchors a singly-linked list of bios chained through the bi_next |
| 591 | * member of the bio. The bio_list also caches the last list member to allow |
| 592 | * fast access to the tail. |
| 593 | */ |
| 594 | struct bio_list { |
| 595 | struct bio *head; |
| 596 | struct bio *tail; |
| 597 | }; |
| 598 | |
| 599 | static inline int bio_list_empty(const struct bio_list *bl) |
| 600 | { |
| 601 | return bl->head == NULL; |
| 602 | } |
| 603 | |
| 604 | static inline void bio_list_init(struct bio_list *bl) |
| 605 | { |
| 606 | bl->head = bl->tail = NULL; |
| 607 | } |
| 608 | |
Jens Axboe | 320ae51 | 2013-10-24 09:20:05 +0100 | [diff] [blame] | 609 | #define BIO_EMPTY_LIST { NULL, NULL } |
| 610 | |
Christoph Hellwig | 8f3d8ba | 2009-04-07 19:55:13 +0200 | [diff] [blame] | 611 | #define bio_list_for_each(bio, bl) \ |
| 612 | for (bio = (bl)->head; bio; bio = bio->bi_next) |
| 613 | |
| 614 | static inline unsigned bio_list_size(const struct bio_list *bl) |
| 615 | { |
| 616 | unsigned sz = 0; |
| 617 | struct bio *bio; |
| 618 | |
| 619 | bio_list_for_each(bio, bl) |
| 620 | sz++; |
| 621 | |
| 622 | return sz; |
| 623 | } |
| 624 | |
| 625 | static inline void bio_list_add(struct bio_list *bl, struct bio *bio) |
| 626 | { |
| 627 | bio->bi_next = NULL; |
| 628 | |
| 629 | if (bl->tail) |
| 630 | bl->tail->bi_next = bio; |
| 631 | else |
| 632 | bl->head = bio; |
| 633 | |
| 634 | bl->tail = bio; |
| 635 | } |
| 636 | |
| 637 | static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio) |
| 638 | { |
| 639 | bio->bi_next = bl->head; |
| 640 | |
| 641 | bl->head = bio; |
| 642 | |
| 643 | if (!bl->tail) |
| 644 | bl->tail = bio; |
| 645 | } |
| 646 | |
| 647 | static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) |
| 648 | { |
| 649 | if (!bl2->head) |
| 650 | return; |
| 651 | |
| 652 | if (bl->tail) |
| 653 | bl->tail->bi_next = bl2->head; |
| 654 | else |
| 655 | bl->head = bl2->head; |
| 656 | |
| 657 | bl->tail = bl2->tail; |
| 658 | } |
| 659 | |
| 660 | static inline void bio_list_merge_head(struct bio_list *bl, |
| 661 | struct bio_list *bl2) |
| 662 | { |
| 663 | if (!bl2->head) |
| 664 | return; |
| 665 | |
| 666 | if (bl->head) |
| 667 | bl2->tail->bi_next = bl->head; |
| 668 | else |
| 669 | bl->tail = bl2->tail; |
| 670 | |
| 671 | bl->head = bl2->head; |
| 672 | } |
| 673 | |
Geert Uytterhoeven | 13685a1 | 2009-06-10 04:38:40 +0000 | [diff] [blame] | 674 | static inline struct bio *bio_list_peek(struct bio_list *bl) |
| 675 | { |
| 676 | return bl->head; |
| 677 | } |
| 678 | |
Christoph Hellwig | 8f3d8ba | 2009-04-07 19:55:13 +0200 | [diff] [blame] | 679 | static inline struct bio *bio_list_pop(struct bio_list *bl) |
| 680 | { |
| 681 | struct bio *bio = bl->head; |
| 682 | |
| 683 | if (bio) { |
| 684 | bl->head = bl->head->bi_next; |
| 685 | if (!bl->head) |
| 686 | bl->tail = NULL; |
| 687 | |
| 688 | bio->bi_next = NULL; |
| 689 | } |
| 690 | |
| 691 | return bio; |
| 692 | } |
| 693 | |
| 694 | static inline struct bio *bio_list_get(struct bio_list *bl) |
| 695 | { |
| 696 | struct bio *bio = bl->head; |
| 697 | |
| 698 | bl->head = bl->tail = NULL; |
| 699 | |
| 700 | return bio; |
| 701 | } |
| 702 | |
Kent Overstreet | 57fb233 | 2012-08-24 04:56:11 -0700 | [diff] [blame] | 703 | /* |
Mike Snitzer | 0ef5a50 | 2016-05-05 11:54:22 -0400 | [diff] [blame] | 704 | * Increment chain count for the bio. Make sure the CHAIN flag update |
| 705 | * is visible before the raised count. |
| 706 | */ |
| 707 | static inline void bio_inc_remaining(struct bio *bio) |
| 708 | { |
| 709 | bio_set_flag(bio, BIO_CHAIN); |
| 710 | smp_mb__before_atomic(); |
| 711 | atomic_inc(&bio->__bi_remaining); |
| 712 | } |
| 713 | |
| 714 | /* |
Kent Overstreet | 57fb233 | 2012-08-24 04:56:11 -0700 | [diff] [blame] | 715 | * bio_set is used to allow other portions of the IO system to |
| 716 | * allocate their own private memory pools for bio and iovec structures. |
| 717 | * These memory pools in turn all allocate from the bio_slab |
| 718 | * and the bvec_slabs[]. |
| 719 | */ |
| 720 | #define BIO_POOL_SIZE 2 |
| 721 | #define BIOVEC_NR_POOLS 6 |
| 722 | #define BIOVEC_MAX_IDX (BIOVEC_NR_POOLS - 1) |
| 723 | |
| 724 | struct bio_set { |
| 725 | struct kmem_cache *bio_slab; |
| 726 | unsigned int front_pad; |
| 727 | |
| 728 | mempool_t *bio_pool; |
Kent Overstreet | 9f060e2 | 2012-10-12 15:29:33 -0700 | [diff] [blame] | 729 | mempool_t *bvec_pool; |
Kent Overstreet | 57fb233 | 2012-08-24 04:56:11 -0700 | [diff] [blame] | 730 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 731 | mempool_t *bio_integrity_pool; |
Kent Overstreet | 9f060e2 | 2012-10-12 15:29:33 -0700 | [diff] [blame] | 732 | mempool_t *bvec_integrity_pool; |
Kent Overstreet | 57fb233 | 2012-08-24 04:56:11 -0700 | [diff] [blame] | 733 | #endif |
Kent Overstreet | df2cb6d | 2012-09-10 14:33:46 -0700 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Deadlock avoidance for stacking block drivers: see comments in |
| 737 | * bio_alloc_bioset() for details |
| 738 | */ |
| 739 | spinlock_t rescue_lock; |
| 740 | struct bio_list rescue_list; |
| 741 | struct work_struct rescue_work; |
| 742 | struct workqueue_struct *rescue_workqueue; |
Kent Overstreet | 57fb233 | 2012-08-24 04:56:11 -0700 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | struct biovec_slab { |
| 746 | int nr_vecs; |
| 747 | char *name; |
| 748 | struct kmem_cache *slab; |
| 749 | }; |
| 750 | |
| 751 | /* |
| 752 | * a small number of entries is fine, not going to be performance critical. |
| 753 | * basically we just need to survive |
| 754 | */ |
| 755 | #define BIO_SPLIT_ENTRIES 2 |
| 756 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 757 | #if defined(CONFIG_BLK_DEV_INTEGRITY) |
| 758 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 759 | #define bip_for_each_vec(bvl, bip, iter) \ |
| 760 | for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 761 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 762 | #define bio_for_each_integrity_vec(_bvl, _bio, _iter) \ |
| 763 | for_each_bio(_bio) \ |
| 764 | bip_for_each_vec(_bvl, _bio->bi_integrity, _iter) |
| 765 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 766 | extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); |
Kent Overstreet | 1e2a410f | 2012-09-06 15:34:56 -0700 | [diff] [blame] | 767 | extern void bio_integrity_free(struct bio *); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 768 | extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); |
Martin K. Petersen | e7258c1 | 2014-09-26 19:19:55 -0400 | [diff] [blame] | 769 | extern bool bio_integrity_enabled(struct bio *bio); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 770 | extern int bio_integrity_prep(struct bio *); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 771 | extern void bio_integrity_endio(struct bio *); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 772 | extern void bio_integrity_advance(struct bio *, unsigned int); |
| 773 | extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int); |
Kent Overstreet | 1e2a410f | 2012-09-06 15:34:56 -0700 | [diff] [blame] | 774 | extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t); |
Martin K. Petersen | 7878cba | 2009-06-26 15:37:49 +0200 | [diff] [blame] | 775 | extern int bioset_integrity_create(struct bio_set *, int); |
| 776 | extern void bioset_integrity_free(struct bio_set *); |
| 777 | extern void bio_integrity_init(void); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 778 | |
| 779 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
| 780 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 781 | static inline void *bio_integrity(struct bio *bio) |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 782 | { |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 783 | return NULL; |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Martin K. Petersen | e7258c1 | 2014-09-26 19:19:55 -0400 | [diff] [blame] | 786 | static inline bool bio_integrity_enabled(struct bio *bio) |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 787 | { |
Martin K. Petersen | e7258c1 | 2014-09-26 19:19:55 -0400 | [diff] [blame] | 788 | return false; |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static inline int bioset_integrity_create(struct bio_set *bs, int pool_size) |
| 792 | { |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static inline void bioset_integrity_free (struct bio_set *bs) |
| 797 | { |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | static inline int bio_integrity_prep(struct bio *bio) |
| 802 | { |
| 803 | return 0; |
| 804 | } |
| 805 | |
Kent Overstreet | 1e2a410f | 2012-09-06 15:34:56 -0700 | [diff] [blame] | 806 | static inline void bio_integrity_free(struct bio *bio) |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 807 | { |
| 808 | return; |
| 809 | } |
| 810 | |
Stephen Rothwell | 0c614e2 | 2011-11-16 09:21:48 +0100 | [diff] [blame] | 811 | static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src, |
Kent Overstreet | 1e2a410f | 2012-09-06 15:34:56 -0700 | [diff] [blame] | 812 | gfp_t gfp_mask) |
Stephen Rothwell | 0c614e2 | 2011-11-16 09:21:48 +0100 | [diff] [blame] | 813 | { |
| 814 | return 0; |
| 815 | } |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 816 | |
Martin K. Petersen | 6898e3b | 2012-01-13 08:15:33 +0100 | [diff] [blame] | 817 | static inline void bio_integrity_advance(struct bio *bio, |
| 818 | unsigned int bytes_done) |
| 819 | { |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | static inline void bio_integrity_trim(struct bio *bio, unsigned int offset, |
| 824 | unsigned int sectors) |
| 825 | { |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | static inline void bio_integrity_init(void) |
| 830 | { |
| 831 | return; |
| 832 | } |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 833 | |
Martin K. Petersen | c611529 | 2014-09-26 19:20:08 -0400 | [diff] [blame] | 834 | static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag) |
| 835 | { |
| 836 | return false; |
| 837 | } |
| 838 | |
Keith Busch | 06c1e39 | 2015-12-03 09:32:21 -0700 | [diff] [blame] | 839 | static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp, |
| 840 | unsigned int nr) |
| 841 | { |
| 842 | return ERR_PTR(-EINVAL); |
| 843 | } |
| 844 | |
| 845 | static inline int bio_integrity_add_page(struct bio *bio, struct page *page, |
| 846 | unsigned int len, unsigned int offset) |
| 847 | { |
| 848 | return 0; |
| 849 | } |
| 850 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 851 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ |
| 852 | |
David Howells | 02a5e0a | 2007-08-11 22:34:32 +0200 | [diff] [blame] | 853 | #endif /* CONFIG_BLOCK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | #endif /* __LINUX_BIO_H */ |