blob: 35108c2f8ea965f4f88cd0ac149b4361b4a5039d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo7cc01582010-08-03 13:14:58 +020012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Axboe22e2c502005-06-27 10:55:12 +020025#include <linux/ioprio.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050026#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells02a5e0a2007-08-11 22:34:32 +020028#ifdef CONFIG_BLOCK
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31
Tejun Heo7cc01582010-08-03 13:14:58 +020032/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
33#include <linux/blk_types.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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 Dobriyand84a8472006-06-25 05:49:32 -070043#define BIO_MAX_PAGES 256
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030044#define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
46
Mike Christief2150822016-06-05 14:31:42 -050047#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 Christie43b62ce2016-06-05 14:32:20 -050050#define bio_prio(bio) (bio)->bi_ioprio
51#define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
Jens Axboe22e2c502005-06-27 10:55:12 +020052
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * various member access, note that bio_data should of course not be used
55 * on highmem page vectors
56 */
Kent Overstreet4550dd62013-08-07 14:26:21 -070057#define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
Kent Overstreeta4ad39b12013-08-07 14:24:32 -070058
Kent Overstreet4550dd62013-08-07 14:26:21 -070059#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 Overstreet79886132013-11-23 17:19:00 -080089
Kent Overstreet458b76e2013-09-24 16:26:05 -070090#define bio_multiple_segments(bio) \
91 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
Kent Overstreet4f024f32013-10-11 15:44:27 -070092#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 Axboebf2de6f2007-09-27 13:01:25 +020094
Kent Overstreet458b76e2013-09-24 16:26:05 -070095/*
96 * Check whether this bio carries any data or not. A NULL bio is allowed.
97 */
98static inline bool bio_has_data(struct bio *bio)
99{
100 if (bio &&
101 bio->bi_iter.bi_size &&
Mike Christie95fe6c12016-06-05 14:31:48 -0500102 bio_op(bio) != REQ_OP_DISCARD)
Kent Overstreet458b76e2013-09-24 16:26:05 -0700103 return true;
104
105 return false;
106}
107
Mike Christie95fe6c12016-06-05 14:31:48 -0500108static 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 Overstreet458b76e2013-09-24 16:26:05 -0700113static inline bool bio_is_rw(struct bio *bio)
114{
115 if (!bio_has_data(bio))
116 return false;
117
Mike Christie95fe6c12016-06-05 14:31:48 -0500118 if (bio_no_advance_iter(bio))
Kent Overstreet458b76e2013-09-24 16:26:05 -0700119 return false;
120
121 return true;
122}
123
124static 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 Heo2e46e8b2009-05-07 22:24:41 +0900132static inline unsigned int bio_cur_bytes(struct bio *bio)
Jens Axboebf2de6f2007-09-27 13:01:25 +0200133{
Kent Overstreet458b76e2013-09-24 16:26:05 -0700134 if (bio_has_data(bio))
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700135 return bio_iovec(bio).bv_len;
David Woodhousefb2dce82008-08-05 18:01:53 +0100136 else /* dataless requests such as discard */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700137 return bio->bi_iter.bi_size;
Jens Axboebf2de6f2007-09-27 13:01:25 +0200138}
139
140static inline void *bio_data(struct bio *bio)
141{
Kent Overstreet458b76e2013-09-24 16:26:05 -0700142 if (bio_has_data(bio))
Jens Axboebf2de6f2007-09-27 13:01:25 +0200143 return page_address(bio_page(bio)) + bio_offset(bio);
144
145 return NULL;
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
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 Overstreetf619d252013-08-07 14:30:33 -0700160#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 Torvalds1da177e2005-04-16 15:20:36 -0700163
Kent Overstreetf619d252013-08-07 14:30:33 -0700164#define __bio_kunmap_atomic(addr) kunmap_atomic(addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
167 * merge helpers etc
168 */
169
Jeremy Fitzhardingef92131c2008-10-29 14:10:51 +0100170/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700174/*
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 Fitzhardingef92131c2008-10-29 14:10:51 +0100179 __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
183 (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
184#define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400185 __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jens Axboe66cb45a2014-06-24 16:22:24 -0600187/*
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800188 * 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 Overstreetf619d252013-08-07 14:30:33 -0700192 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800193
Kent Overstreet4550dd62013-08-07 14:26:21 -0700194static 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. Petersenb7aa84d2014-04-08 22:43:43 -0400215 for (iter = (start); \
216 (iter).bi_size && \
217 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
Kent Overstreet4550dd62013-08-07 14:26:21 -0700218 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
219
220
221static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
222 unsigned bytes)
223{
224 iter->bi_sector += bytes >> 9;
225
Mike Christie95fe6c12016-06-05 14:31:48 -0500226 if (bio_no_advance_iter(bio))
Kent Overstreet4550dd62013-08-07 14:26:21 -0700227 iter->bi_size -= bytes;
228 else
229 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
230}
231
Kent Overstreet79886132013-11-23 17:19:00 -0800232#define __bio_for_each_segment(bvl, bio, iter, start) \
233 for (iter = (start); \
Kent Overstreet4550dd62013-08-07 14:26:21 -0700234 (iter).bi_size && \
235 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
236 bio_advance_iter((bio), &(iter), (bvl).bv_len))
Kent Overstreet79886132013-11-23 17:19:00 -0800237
238#define bio_for_each_segment(bvl, bio, iter) \
239 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
240
Kent Overstreet4550dd62013-08-07 14:26:21 -0700241#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Kent Overstreet458b76e2013-09-24 16:26:05 -0700243static inline unsigned bio_segments(struct bio *bio)
244{
245 unsigned segs = 0;
246 struct bio_vec bv;
247 struct bvec_iter iter;
248
Kent Overstreet8423ae32014-02-10 17:45:50 -0800249 /*
250 * We special case discard/write same, because they interpret bi_size
251 * differently:
252 */
253
Mike Christie95fe6c12016-06-05 14:31:48 -0500254 if (bio_op(bio) == REQ_OP_DISCARD)
Kent Overstreet8423ae32014-02-10 17:45:50 -0800255 return 1;
256
Mike Christie95fe6c12016-06-05 14:31:48 -0500257 if (bio_op(bio) == REQ_OP_WRITE_SAME)
Kent Overstreet8423ae32014-02-10 17:45:50 -0800258 return 1;
259
Kent Overstreet458b76e2013-09-24 16:26:05 -0700260 bio_for_each_segment(bv, bio, iter)
261 segs++;
262
263 return segs;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
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 Axboedac56212015-04-17 16:23:59 -0600280static 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
287static 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 Torvalds1da177e2005-04-16 15:20:36 -0700295
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600296static inline bool bio_flagged(struct bio *bio, unsigned int bit)
297{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600298 return (bio->bi_flags & (1U << bit)) != 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600299}
300
301static inline void bio_set_flag(struct bio *bio, unsigned int bit)
302{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600303 bio->bi_flags |= (1U << bit);
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600304}
305
306static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
307{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600308 bio->bi_flags &= ~(1U << bit);
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600309}
310
Ming Lei7bcd79a2016-02-26 23:40:50 +0800311static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
312{
313 *bv = bio_iovec(bio);
314}
315
316static 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 Lei7bcd79a2016-02-26 23:40:50 +0800321 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. Petersenc6115292014-09-26 19:20:08 -0400343enum 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. Petersen7ba1ba12008-06-30 20:04:41 +0200351/*
352 * bio integrity payload
353 */
354struct bio_integrity_payload {
355 struct bio *bip_bio; /* parent bio */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200356
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800357 struct bvec_iter bip_iter;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200358
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800359 bio_end_io_t *bip_end_io; /* saved I/O completion fn */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200360
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200361 unsigned short bip_slab; /* slab the bip came from */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200362 unsigned short bip_vcnt; /* # of integrity bio_vecs */
Gu Zhengcbcd10542014-07-01 10:36:47 -0600363 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
Martin K. Petersenb1f0138852014-09-26 19:20:04 -0400364 unsigned short bip_flags; /* control flags */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200365
366 struct work_struct bip_work; /* I/O completion */
Kent Overstreet6fda9812012-10-12 13:18:27 -0700367
368 struct bio_vec *bip_vec;
369 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200370};
Martin K. Petersen18593082014-09-26 19:20:01 -0400371
Keith Busch06c1e392015-12-03 09:32:21 -0700372#if defined(CONFIG_BLK_DEV_INTEGRITY)
373
374static 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. Petersenc6115292014-09-26 19:20:08 -0400382static 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. Petersenb1f0138852014-09-26 19:20:04 -0400391
Martin K. Petersen18593082014-09-26 19:20:01 -0400392static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
393{
394 return bip->bip_iter.bi_sector;
395}
396
397static 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. Petersen7ba1ba12008-06-30 20:04:41 +0200403#endif /* CONFIG_BLK_DEV_INTEGRITY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Kent Overstreet6678d832013-08-07 11:14:32 -0700405extern void bio_trim(struct bio *bio, int offset, int size);
Kent Overstreet20d01892013-11-23 18:21:01 -0800406extern 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 */
419static 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 Axboebb799ca2008-12-10 15:35:05 +0100428extern struct bio_set *bioset_create(unsigned int, unsigned int);
Junichi Nomurad8f429e2014-10-03 17:27:12 -0400429extern struct bio_set *bioset_create_nobvec(unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430extern void bioset_free(struct bio_set *);
Fabian Fredericka6c39cb4f2014-04-22 15:09:05 -0600431extern mempool_t *biovec_create_pool(int pool_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Al Virodd0fc662005-10-07 07:46:04 +0100433extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434extern void bio_put(struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Kent Overstreet59d276f2013-11-23 18:19:27 -0800436extern void __bio_clone_fast(struct bio *, struct bio *);
437extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700438extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
439
Kent Overstreet3f86a822012-09-06 15:35:01 -0700440extern struct bio_set *fs_bio_set;
441
442static 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 Overstreetbf800ef2012-09-06 15:35:02 -0700447static 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 Overstreet3f86a822012-09-06 15:35:01 -0700452static 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 Overstreetbf800ef2012-09-06 15:35:02 -0700457static 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 Hellwig4246a0b2015-07-20 15:29:37 +0200463extern void bio_endio(struct bio *);
464
465static inline void bio_io_error(struct bio *bio)
466{
467 bio->bi_error = -EIO;
468 bio_endio(bio);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471struct request_queue;
472extern int bio_phys_segments(struct request_queue *, struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Mike Christie4e49ea42016-06-05 14:31:41 -0500474extern int submit_bio_wait(struct bio *bio);
Kent Overstreet054bdf62012-09-28 13:17:55 -0700475extern void bio_advance(struct bio *, unsigned);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477extern void bio_init(struct bio *);
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700478extern void bio_reset(struct bio *);
Kent Overstreet196d38bc2013-11-23 18:34:15 -0800479void bio_chain(struct bio *, struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
Mike Christie6e68af62005-11-11 05:30:27 -0600482extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
483 unsigned int, unsigned int);
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900484struct rq_map_data;
James Bottomley f1970ba2005-06-20 14:06:52 +0200485extern struct bio *bio_map_user_iov(struct request_queue *,
Kent Overstreet26e49cf2015-01-18 16:16:31 +0100486 const struct iov_iter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487extern void bio_unmap_user(struct bio *);
Mike Christie df46b9a2005-06-20 14:04:44 +0200488extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
Al Viro27496a82005-10-21 03:20:48 -0400489 gfp_t);
FUJITA Tomonori68154e92008-04-25 12:47:50 +0200490extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
491 gfp_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492extern void bio_set_pages_dirty(struct bio *bio);
493extern void bio_check_pages_dirty(struct bio *bio);
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100494
Gu Zheng394ffa52014-11-24 11:05:22 +0800495void generic_start_io_acct(int rw, unsigned long sectors,
496 struct hd_struct *part);
497void generic_end_io_acct(int rw, struct hd_struct *part,
498 unsigned long start_time);
499
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100500#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
504extern void bio_flush_dcache_pages(struct bio *bi);
505#else
506static inline void bio_flush_dcache_pages(struct bio *bi)
507{
508}
509#endif
510
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700511extern void bio_copy_data(struct bio *dst, struct bio *src);
Kent Overstreeta0787602012-09-10 14:03:28 -0700512extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700513
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900514extern struct bio *bio_copy_user_iov(struct request_queue *,
Al Viro86d564c2014-02-08 20:42:52 -0500515 struct rq_map_data *,
Kent Overstreet26e49cf2015-01-18 16:16:31 +0100516 const struct iov_iter *,
517 gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518extern int bio_uncopy_user(struct bio *);
519void zero_fill_bio(struct bio *bio);
Kent Overstreet9f060e22012-10-12 15:29:33 -0700520extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
521extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200522extern unsigned int bvec_nr_vecs(unsigned short idx);
Martin K. Petersen51d654e2008-06-17 18:59:56 +0200523
Tejun Heo852c7882012-03-05 13:15:27 -0800524#ifdef CONFIG_BLK_CGROUP
Tejun Heo1d933cf2015-05-22 17:13:24 -0400525int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
Tejun Heo852c7882012-03-05 13:15:27 -0800526int bio_associate_current(struct bio *bio);
527void bio_disassociate_task(struct bio *bio);
528#else /* CONFIG_BLK_CGROUP */
Tejun Heo1d933cf2015-05-22 17:13:24 -0400529static inline int bio_associate_blkcg(struct bio *bio,
530 struct cgroup_subsys_state *blkcg_css) { return 0; }
Tejun Heo852c7882012-03-05 13:15:27 -0800531static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
532static inline void bio_disassociate_task(struct bio *bio) { }
533#endif /* CONFIG_BLK_CGROUP */
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#ifdef CONFIG_HIGHMEM
536/*
Alberto Bertogli20b636b2009-02-02 12:41:07 +0100537 * remember never ever reenable interrupts between a bvec_kmap_irq and
538 * bvec_kunmap_irq!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100540static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
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 Wange8e3c3d2011-11-25 23:14:27 +0800549 addr = (unsigned long) kmap_atomic(bvec->bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 BUG_ON(addr & ~PAGE_MASK);
552
553 return (char *) addr + bvec->bv_offset;
554}
555
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100556static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
559
Cong Wange8e3c3d2011-11-25 23:14:27 +0800560 kunmap_atomic((void *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 local_irq_restore(*flags);
562}
563
564#else
Geert Uytterhoeven11a691b2010-10-21 10:32:29 +0200565static 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
570static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
571{
572 *flags = 0;
573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
575
Kent Overstreetf619d252013-08-07 14:30:33 -0700576static inline char *__bio_kmap_irq(struct bio *bio, struct bvec_iter iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 unsigned long *flags)
578{
Kent Overstreetf619d252013-08-07 14:30:33 -0700579 return bvec_kmap_irq(&bio_iter_iovec(bio, iter), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581#define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
582
583#define bio_kmap_irq(bio, flags) \
Kent Overstreetf619d252013-08-07 14:30:33 -0700584 __bio_kmap_irq((bio), (bio)->bi_iter, (flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
586
Jens Axboe7a67f632008-08-08 11:17:12 +0200587/*
Akinobu Mitae6863072009-04-17 08:41:21 +0200588 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200589 *
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 */
594struct bio_list {
595 struct bio *head;
596 struct bio *tail;
597};
598
599static inline int bio_list_empty(const struct bio_list *bl)
600{
601 return bl->head == NULL;
602}
603
604static inline void bio_list_init(struct bio_list *bl)
605{
606 bl->head = bl->tail = NULL;
607}
608
Jens Axboe320ae512013-10-24 09:20:05 +0100609#define BIO_EMPTY_LIST { NULL, NULL }
610
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200611#define bio_list_for_each(bio, bl) \
612 for (bio = (bl)->head; bio; bio = bio->bi_next)
613
614static 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
625static 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
637static 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
647static 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
660static 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 Uytterhoeven13685a12009-06-10 04:38:40 +0000674static inline struct bio *bio_list_peek(struct bio_list *bl)
675{
676 return bl->head;
677}
678
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200679static 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
694static 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 Overstreet57fb2332012-08-24 04:56:11 -0700703/*
Mike Snitzer0ef5a502016-05-05 11:54:22 -0400704 * Increment chain count for the bio. Make sure the CHAIN flag update
705 * is visible before the raised count.
706 */
707static 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 Overstreet57fb2332012-08-24 04:56:11 -0700715 * 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
724struct bio_set {
725 struct kmem_cache *bio_slab;
726 unsigned int front_pad;
727
728 mempool_t *bio_pool;
Kent Overstreet9f060e22012-10-12 15:29:33 -0700729 mempool_t *bvec_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700730#if defined(CONFIG_BLK_DEV_INTEGRITY)
731 mempool_t *bio_integrity_pool;
Kent Overstreet9f060e22012-10-12 15:29:33 -0700732 mempool_t *bvec_integrity_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700733#endif
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700734
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 Overstreet57fb2332012-08-24 04:56:11 -0700743};
744
745struct 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. Petersen7ba1ba12008-06-30 20:04:41 +0200757#if defined(CONFIG_BLK_DEV_INTEGRITY)
758
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800759#define bip_for_each_vec(bvl, bip, iter) \
760 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200761
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200762#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. Petersen7ba1ba12008-06-30 20:04:41 +0200766extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700767extern void bio_integrity_free(struct bio *);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200768extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
Martin K. Petersene7258c12014-09-26 19:19:55 -0400769extern bool bio_integrity_enabled(struct bio *bio);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200770extern int bio_integrity_prep(struct bio *);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200771extern void bio_integrity_endio(struct bio *);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200772extern void bio_integrity_advance(struct bio *, unsigned int);
773extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700774extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200775extern int bioset_integrity_create(struct bio_set *, int);
776extern void bioset_integrity_free(struct bio_set *);
777extern void bio_integrity_init(void);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200778
779#else /* CONFIG_BLK_DEV_INTEGRITY */
780
Martin K. Petersenc6115292014-09-26 19:20:08 -0400781static inline void *bio_integrity(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100782{
Martin K. Petersenc6115292014-09-26 19:20:08 -0400783 return NULL;
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100784}
785
Martin K. Petersene7258c12014-09-26 19:19:55 -0400786static inline bool bio_integrity_enabled(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100787{
Martin K. Petersene7258c12014-09-26 19:19:55 -0400788 return false;
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100789}
790
791static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
792{
793 return 0;
794}
795
796static inline void bioset_integrity_free (struct bio_set *bs)
797{
798 return;
799}
800
801static inline int bio_integrity_prep(struct bio *bio)
802{
803 return 0;
804}
805
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700806static inline void bio_integrity_free(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100807{
808 return;
809}
810
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100811static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700812 gfp_t gfp_mask)
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100813{
814 return 0;
815}
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100816
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100817static inline void bio_integrity_advance(struct bio *bio,
818 unsigned int bytes_done)
819{
820 return;
821}
822
823static inline void bio_integrity_trim(struct bio *bio, unsigned int offset,
824 unsigned int sectors)
825{
826 return;
827}
828
829static inline void bio_integrity_init(void)
830{
831 return;
832}
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200833
Martin K. Petersenc6115292014-09-26 19:20:08 -0400834static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
835{
836 return false;
837}
838
Keith Busch06c1e392015-12-03 09:32:21 -0700839static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
840 unsigned int nr)
841{
842 return ERR_PTR(-EINVAL);
843}
844
845static 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. Petersen7ba1ba12008-06-30 20:04:41 +0200851#endif /* CONFIG_BLK_DEV_INTEGRITY */
852
David Howells02a5e0a2007-08-11 22:34:32 +0200853#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#endif /* __LINUX_BIO_H */