blob: 6ee2ea8b378a67ed70f404321c5ae25b986caadb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
3 *
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.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Tejun Heo7cc01582010-08-03 13:14:58 +020010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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-
17 */
18#ifndef __LINUX_BIO_H
19#define __LINUX_BIO_H
20
21#include <linux/highmem.h>
22#include <linux/mempool.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020023#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells02a5e0a2007-08-11 22:34:32 +020025#ifdef CONFIG_BLOCK
Tejun Heo7cc01582010-08-03 13:14:58 +020026/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
27#include <linux/blk_types.h>
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define BIO_DEBUG
30
31#ifdef BIO_DEBUG
32#define BIO_BUG_ON BUG_ON
33#else
34#define BIO_BUG_ON
35#endif
36
Huang Ying225311a2017-09-06 16:22:30 -070037#ifdef CONFIG_THP_SWAP
38#if HPAGE_PMD_NR > 256
39#define BIO_MAX_PAGES HPAGE_PMD_NR
40#else
Alexey Dobriyand84a8472006-06-25 05:49:32 -070041#define BIO_MAX_PAGES 256
Huang Ying225311a2017-09-06 16:22:30 -070042#endif
43#else
44#define BIO_MAX_PAGES 256
45#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Mike Christie43b62ce2016-06-05 14:32:20 -050047#define bio_prio(bio) (bio)->bi_ioprio
48#define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
Jens Axboe22e2c502005-06-27 10:55:12 +020049
Kent Overstreet4550dd62013-08-07 14:26:21 -070050#define bio_iter_iovec(bio, iter) \
51 bvec_iter_bvec((bio)->bi_io_vec, (iter))
52
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))
59
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)
Kent Overstreet79886132013-11-23 17:19:00 -080063
Kent Overstreet458b76e2013-09-24 16:26:05 -070064#define bio_multiple_segments(bio) \
65 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
Kent Overstreet38a72da2018-05-08 21:33:53 -040066
67#define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
68#define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
69
70#define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
71#define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
Jens Axboebf2de6f2007-09-27 13:01:25 +020072
Kent Overstreet458b76e2013-09-24 16:26:05 -070073/*
Christoph Hellwigd3849952016-11-01 07:40:11 -060074 * Return the data direction, READ or WRITE.
75 */
76#define bio_data_dir(bio) \
77 (op_is_write(bio_op(bio)) ? WRITE : READ)
78
79/*
Kent Overstreet458b76e2013-09-24 16:26:05 -070080 * Check whether this bio carries any data or not. A NULL bio is allowed.
81 */
82static inline bool bio_has_data(struct bio *bio)
83{
84 if (bio &&
85 bio->bi_iter.bi_size &&
Adrian Hunter7afafc82016-08-16 10:59:35 +030086 bio_op(bio) != REQ_OP_DISCARD &&
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -080087 bio_op(bio) != REQ_OP_SECURE_ERASE &&
88 bio_op(bio) != REQ_OP_WRITE_ZEROES)
Kent Overstreet458b76e2013-09-24 16:26:05 -070089 return true;
90
91 return false;
92}
93
Mike Christie95fe6c12016-06-05 14:31:48 -050094static inline bool bio_no_advance_iter(struct bio *bio)
95{
Adrian Hunter7afafc82016-08-16 10:59:35 +030096 return bio_op(bio) == REQ_OP_DISCARD ||
97 bio_op(bio) == REQ_OP_SECURE_ERASE ||
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -080098 bio_op(bio) == REQ_OP_WRITE_SAME ||
99 bio_op(bio) == REQ_OP_WRITE_ZEROES;
Mike Christie95fe6c12016-06-05 14:31:48 -0500100}
101
Kent Overstreet458b76e2013-09-24 16:26:05 -0700102static inline bool bio_mergeable(struct bio *bio)
103{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600104 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
Kent Overstreet458b76e2013-09-24 16:26:05 -0700105 return false;
106
107 return true;
108}
109
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900110static inline unsigned int bio_cur_bytes(struct bio *bio)
Jens Axboebf2de6f2007-09-27 13:01:25 +0200111{
Kent Overstreet458b76e2013-09-24 16:26:05 -0700112 if (bio_has_data(bio))
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700113 return bio_iovec(bio).bv_len;
David Woodhousefb2dce82008-08-05 18:01:53 +0100114 else /* dataless requests such as discard */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700115 return bio->bi_iter.bi_size;
Jens Axboebf2de6f2007-09-27 13:01:25 +0200116}
117
118static inline void *bio_data(struct bio *bio)
119{
Kent Overstreet458b76e2013-09-24 16:26:05 -0700120 if (bio_has_data(bio))
Jens Axboebf2de6f2007-09-27 13:01:25 +0200121 return page_address(bio_page(bio)) + bio_offset(bio);
122
123 return NULL;
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Hellwig0aa69fd2018-06-01 09:03:05 -0700126static inline bool bio_full(struct bio *bio)
127{
128 return bio->bi_vcnt >= bio->bi_max_vecs;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800132 * 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
134 */
135#define bio_for_each_segment_all(bvl, bio, i) \
Kent Overstreetf619d252013-08-07 14:30:33 -0700136 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800137
Kent Overstreet4550dd62013-08-07 14:26:21 -0700138static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
139 unsigned bytes)
140{
141 iter->bi_sector += bytes >> 9;
142
Ming Lei7759eb22018-09-05 15:45:54 -0600143 if (bio_no_advance_iter(bio))
Kent Overstreet4550dd62013-08-07 14:26:21 -0700144 iter->bi_size -= bytes;
Ming Lei7759eb22018-09-05 15:45:54 -0600145 else
Kent Overstreet4550dd62013-08-07 14:26:21 -0700146 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -0700147 /* TODO: It is reasonable to complete bio with error here. */
Dmitry Monakhovf9df1cd2017-06-29 11:31:14 -0700148}
149
Kent Overstreet79886132013-11-23 17:19:00 -0800150#define __bio_for_each_segment(bvl, bio, iter, start) \
151 for (iter = (start); \
Kent Overstreet4550dd62013-08-07 14:26:21 -0700152 (iter).bi_size && \
153 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
154 bio_advance_iter((bio), &(iter), (bvl).bv_len))
Kent Overstreet79886132013-11-23 17:19:00 -0800155
156#define bio_for_each_segment(bvl, bio, iter) \
157 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
158
Kent Overstreet4550dd62013-08-07 14:26:21 -0700159#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Shaohua Lif4595872017-03-24 10:34:43 -0700161static inline unsigned bio_segments(struct bio *bio)
Kent Overstreet458b76e2013-09-24 16:26:05 -0700162{
163 unsigned segs = 0;
164 struct bio_vec bv;
165 struct bvec_iter iter;
166
Kent Overstreet8423ae32014-02-10 17:45:50 -0800167 /*
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800168 * We special case discard/write same/write zeroes, because they
169 * interpret bi_size differently:
Kent Overstreet8423ae32014-02-10 17:45:50 -0800170 */
171
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800172 switch (bio_op(bio)) {
173 case REQ_OP_DISCARD:
174 case REQ_OP_SECURE_ERASE:
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800175 case REQ_OP_WRITE_ZEROES:
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700176 return 0;
177 case REQ_OP_WRITE_SAME:
Kent Overstreet8423ae32014-02-10 17:45:50 -0800178 return 1;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800179 default:
180 break;
181 }
Kent Overstreet8423ae32014-02-10 17:45:50 -0800182
Shaohua Lif4595872017-03-24 10:34:43 -0700183 bio_for_each_segment(bv, bio, iter)
Kent Overstreet458b76e2013-09-24 16:26:05 -0700184 segs++;
185
186 return segs;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * get a reference to a bio, so it won't disappear. the intended use is
191 * something like:
192 *
193 * bio_get(bio);
194 * submit_bio(rw, bio);
195 * if (bio->bi_flags ...)
196 * do_something
197 * bio_put(bio);
198 *
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 ...)
201 * runs
202 */
Jens Axboedac56212015-04-17 16:23:59 -0600203static inline void bio_get(struct bio *bio)
204{
205 bio->bi_flags |= (1 << BIO_REFFED);
206 smp_mb__before_atomic();
207 atomic_inc(&bio->__bi_cnt);
208}
209
210static inline void bio_cnt_set(struct bio *bio, unsigned int count)
211{
212 if (count != 1) {
213 bio->bi_flags |= (1 << BIO_REFFED);
214 smp_mb__before_atomic();
215 }
216 atomic_set(&bio->__bi_cnt, count);
217}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600219static inline bool bio_flagged(struct bio *bio, unsigned int bit)
220{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600221 return (bio->bi_flags & (1U << bit)) != 0;
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600222}
223
224static inline void bio_set_flag(struct bio *bio, unsigned int bit)
225{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600226 bio->bi_flags |= (1U << bit);
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600227}
228
229static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
230{
Jens Axboe2c68f6d2015-07-28 13:14:32 -0600231 bio->bi_flags &= ~(1U << bit);
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600232}
233
Ming Lei7bcd79a2016-02-26 23:40:50 +0800234static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
235{
236 *bv = bio_iovec(bio);
237}
238
239static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
240{
241 struct bvec_iter iter = bio->bi_iter;
242 int idx;
243
Ming Lei7bcd79a2016-02-26 23:40:50 +0800244 if (unlikely(!bio_multiple_segments(bio))) {
245 *bv = bio_iovec(bio);
246 return;
247 }
248
249 bio_advance_iter(bio, &iter, iter.bi_size);
250
251 if (!iter.bi_bvec_done)
252 idx = iter.bi_idx - 1;
253 else /* in the middle of bvec */
254 idx = iter.bi_idx;
255
256 *bv = bio->bi_io_vec[idx];
257
258 /*
259 * iter.bi_bvec_done records actual length of the last bvec
260 * if this bio ends in the middle of one io vector
261 */
262 if (iter.bi_bvec_done)
263 bv->bv_len = iter.bi_bvec_done;
264}
265
Ming Lei86292ab2017-12-18 20:22:03 +0800266static inline unsigned bio_pages_all(struct bio *bio)
267{
268 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
269 return bio->bi_vcnt;
270}
271
272static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
273{
274 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
275 return bio->bi_io_vec;
276}
277
278static inline struct page *bio_first_page_all(struct bio *bio)
279{
280 return bio_first_bvec_all(bio)->bv_page;
281}
282
283static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
284{
285 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
286 return &bio->bi_io_vec[bio->bi_vcnt - 1];
287}
288
Martin K. Petersenc6115292014-09-26 19:20:08 -0400289enum bip_flags {
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 */
295};
296
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200297/*
298 * bio integrity payload
299 */
300struct bio_integrity_payload {
301 struct bio *bip_bio; /* parent bio */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200302
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800303 struct bvec_iter bip_iter;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200304
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200305 unsigned short bip_slab; /* slab the bip came from */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200306 unsigned short bip_vcnt; /* # of integrity bio_vecs */
Gu Zhengcbcd10542014-07-01 10:36:47 -0600307 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
Martin K. Petersenb1f0138852014-09-26 19:20:04 -0400308 unsigned short bip_flags; /* control flags */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200309
Ming Lei7759eb22018-09-05 15:45:54 -0600310 struct bvec_iter bio_iter; /* for rewinding parent bio */
311
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200312 struct work_struct bip_work; /* I/O completion */
Kent Overstreet6fda9812012-10-12 13:18:27 -0700313
314 struct bio_vec *bip_vec;
315 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200316};
Martin K. Petersen18593082014-09-26 19:20:01 -0400317
Keith Busch06c1e392015-12-03 09:32:21 -0700318#if defined(CONFIG_BLK_DEV_INTEGRITY)
319
320static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
321{
Jens Axboe1eff9d32016-08-05 15:35:16 -0600322 if (bio->bi_opf & REQ_INTEGRITY)
Keith Busch06c1e392015-12-03 09:32:21 -0700323 return bio->bi_integrity;
324
325 return NULL;
326}
327
Martin K. Petersenc6115292014-09-26 19:20:08 -0400328static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
329{
330 struct bio_integrity_payload *bip = bio_integrity(bio);
331
332 if (bip)
333 return bip->bip_flags & flag;
334
335 return false;
336}
Martin K. Petersenb1f0138852014-09-26 19:20:04 -0400337
Martin K. Petersen18593082014-09-26 19:20:01 -0400338static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
339{
340 return bip->bip_iter.bi_sector;
341}
342
343static inline void bip_set_seed(struct bio_integrity_payload *bip,
344 sector_t seed)
345{
346 bip->bip_iter.bi_sector = seed;
347}
348
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200349#endif /* CONFIG_BLK_DEV_INTEGRITY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Kent Overstreet6678d832013-08-07 11:14:32 -0700351extern void bio_trim(struct bio *bio, int offset, int size);
Kent Overstreet20d01892013-11-23 18:21:01 -0800352extern struct bio *bio_split(struct bio *bio, int sectors,
353 gfp_t gfp, struct bio_set *bs);
354
355/**
356 * bio_next_split - get next @sectors from a bio, splitting if necessary
357 * @bio: bio to split
358 * @sectors: number of sectors to split from the front of @bio
359 * @gfp: gfp mask
360 * @bs: bio set to allocate from
361 *
362 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
363 * than @sectors, returns the original bio unchanged.
364 */
365static inline struct bio *bio_next_split(struct bio *bio, int sectors,
366 gfp_t gfp, struct bio_set *bs)
367{
368 if (sectors >= bio_sectors(bio))
369 return bio;
370
371 return bio_split(bio, sectors, gfp, bs);
372}
373
NeilBrown011067b2017-06-18 14:38:57 +1000374enum {
375 BIOSET_NEED_BVECS = BIT(0),
NeilBrown47e0fb42017-06-18 14:38:57 +1000376 BIOSET_NEED_RESCUER = BIT(1),
NeilBrown011067b2017-06-18 14:38:57 +1000377};
Kent Overstreetdad08522018-05-20 18:25:58 -0400378extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
379extern void bioset_exit(struct bio_set *);
Kent Overstreet8aa6ba22018-05-08 21:33:50 -0400380extern int biovec_init_pool(mempool_t *pool, int pool_entries);
Jens Axboe28e89fd92018-06-07 14:42:05 -0600381extern int bioset_init_from_src(struct bio_set *bs, struct bio_set *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dan Carpenter7a88fa12017-03-23 13:24:55 +0300383extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384extern void bio_put(struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Kent Overstreet59d276f2013-11-23 18:19:27 -0800386extern void __bio_clone_fast(struct bio *, struct bio *);
387extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700388
Kent Overstreetf4f81542018-05-08 21:33:52 -0400389extern struct bio_set fs_bio_set;
Kent Overstreet3f86a822012-09-06 15:35:01 -0700390
391static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
392{
Kent Overstreetf4f81542018-05-08 21:33:52 -0400393 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
Kent Overstreet3f86a822012-09-06 15:35:01 -0700394}
395
396static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
397{
398 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
399}
400
Christoph Hellwig1e3914d2016-11-01 07:40:12 -0600401extern blk_qc_t submit_bio(struct bio *);
402
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200403extern void bio_endio(struct bio *);
404
405static inline void bio_io_error(struct bio *bio)
406{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200407 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200408 bio_endio(bio);
409}
410
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500411static inline void bio_wouldblock_error(struct bio *bio)
412{
413 bio->bi_status = BLK_STS_AGAIN;
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700414 bio_endio(bio);
415}
NeilBrown6712ecf2007-09-27 12:47:43 +0200416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417struct request_queue;
418extern int bio_phys_segments(struct request_queue *, struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Mike Christie4e49ea42016-06-05 14:31:41 -0500420extern int submit_bio_wait(struct bio *bio);
Kent Overstreet054bdf62012-09-28 13:17:55 -0700421extern void bio_advance(struct bio *, unsigned);
422
Ming Lei3a83f462016-11-22 08:57:21 -0700423extern void bio_init(struct bio *bio, struct bio_vec *table,
424 unsigned short max_vecs);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600425extern void bio_uninit(struct bio *);
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700426extern void bio_reset(struct bio *);
Kent Overstreet196d38bc2013-11-23 18:34:15 -0800427void bio_chain(struct bio *, struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
Mike Christie6e68af62005-11-11 05:30:27 -0600430extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
431 unsigned int, unsigned int);
Christoph Hellwig0aa69fd2018-06-01 09:03:05 -0700432bool __bio_try_merge_page(struct bio *bio, struct page *page,
433 unsigned int len, unsigned int off);
434void __bio_add_page(struct bio *bio, struct page *page,
435 unsigned int len, unsigned int off);
Kent Overstreet2cefe4d2016-10-31 11:59:24 -0600436int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900437struct rq_map_data;
James Bottomley f1970ba2005-06-20 14:06:52 +0200438extern struct bio *bio_map_user_iov(struct request_queue *,
Al Viroe81cef52017-09-24 09:25:39 -0400439 struct iov_iter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440extern void bio_unmap_user(struct bio *);
Mike Christie df46b9a2005-06-20 14:04:44 +0200441extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
Al Viro27496a82005-10-21 03:20:48 -0400442 gfp_t);
FUJITA Tomonori68154e92008-04-25 12:47:50 +0200443extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
444 gfp_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445extern void bio_set_pages_dirty(struct bio *bio);
446extern void bio_check_pages_dirty(struct bio *bio);
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100447
Michael Callahanddcf35d2018-07-18 04:47:39 -0700448void generic_start_io_acct(struct request_queue *q, int op,
Jens Axboed62e26b2017-06-30 21:55:08 -0600449 unsigned long sectors, struct hd_struct *part);
Michael Callahanddcf35d2018-07-18 04:47:39 -0700450void generic_end_io_acct(struct request_queue *q, int op,
Jens Axboed62e26b2017-06-30 21:55:08 -0600451 struct hd_struct *part,
452 unsigned long start_time);
Gu Zheng394ffa52014-11-24 11:05:22 +0800453
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100454#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
455# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
456#endif
457#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
458extern void bio_flush_dcache_pages(struct bio *bi);
459#else
460static inline void bio_flush_dcache_pages(struct bio *bi)
461{
462}
463#endif
464
Kent Overstreet45db54d2018-05-08 21:33:54 -0400465extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
466 struct bio *src, struct bvec_iter *src_iter);
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700467extern void bio_copy_data(struct bio *dst, struct bio *src);
Kent Overstreet45db54d2018-05-08 21:33:54 -0400468extern void bio_list_copy_data(struct bio *dst, struct bio *src);
Guoqing Jiang491221f2016-09-22 03:10:01 -0400469extern void bio_free_pages(struct bio *bio);
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700470
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900471extern struct bio *bio_copy_user_iov(struct request_queue *,
Al Viro86d564c2014-02-08 20:42:52 -0500472 struct rq_map_data *,
Al Viroe81cef52017-09-24 09:25:39 -0400473 struct iov_iter *,
Kent Overstreet26e49cf2015-01-18 16:16:31 +0100474 gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475extern int bio_uncopy_user(struct bio *);
Kent Overstreet38a72da2018-05-08 21:33:53 -0400476void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
477
478static inline void zero_fill_bio(struct bio *bio)
479{
480 zero_fill_bio_iter(bio, bio->bi_iter);
481}
482
Kent Overstreet9f060e22012-10-12 15:29:33 -0700483extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
484extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200485extern unsigned int bvec_nr_vecs(unsigned short idx);
Jiufei Xue9c0fb1e2018-02-27 20:10:18 +0800486extern const char *bio_devname(struct bio *bio, char *buffer);
Martin K. Petersen51d654e2008-06-17 18:59:56 +0200487
Christoph Hellwig74d46992017-08-23 19:10:32 +0200488#define bio_set_dev(bio, bdev) \
489do { \
Shaohua Li111be882017-12-20 11:10:17 -0700490 if ((bio)->bi_disk != (bdev)->bd_disk) \
491 bio_clear_flag(bio, BIO_THROTTLED);\
Christoph Hellwig74d46992017-08-23 19:10:32 +0200492 (bio)->bi_disk = (bdev)->bd_disk; \
493 (bio)->bi_partno = (bdev)->bd_partno; \
Dennis Zhou5cdf2e32018-12-05 12:10:31 -0500494 bio_associate_blkg(bio); \
Christoph Hellwig74d46992017-08-23 19:10:32 +0200495} while (0)
496
497#define bio_copy_dev(dst, src) \
498do { \
499 (dst)->bi_disk = (src)->bi_disk; \
500 (dst)->bi_partno = (src)->bi_partno; \
Dennis Zhou5cdf2e32018-12-05 12:10:31 -0500501 bio_clone_blkcg_association(dst, src); \
Christoph Hellwig74d46992017-08-23 19:10:32 +0200502} while (0)
503
504#define bio_dev(bio) \
505 disk_devt((bio)->bi_disk)
506
Tejun Heo0d3bd882018-07-03 11:14:54 -0400507#if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
Dennis Zhoub5f29542018-11-01 17:24:10 -0400508int bio_associate_blkcg_from_page(struct bio *bio, struct page *page);
Tejun Heo0d3bd882018-07-03 11:14:54 -0400509#else
Dennis Zhoub5f29542018-11-01 17:24:10 -0400510static inline int bio_associate_blkcg_from_page(struct bio *bio,
511 struct page *page) { return 0; }
Tejun Heo0d3bd882018-07-03 11:14:54 -0400512#endif
513
Tejun Heo852c7882012-03-05 13:15:27 -0800514#ifdef CONFIG_BLK_CGROUP
Dennis Zhoub5f29542018-11-01 17:24:10 -0400515int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
Dennis Zhou2268c0f2018-12-05 12:10:29 -0500516void bio_disassociate_blkg(struct bio *bio);
517void bio_associate_blkg(struct bio *bio);
Tejun Heo852c7882012-03-05 13:15:27 -0800518void bio_disassociate_task(struct bio *bio);
Dennis Zhoub5f29542018-11-01 17:24:10 -0400519void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
Tejun Heo852c7882012-03-05 13:15:27 -0800520#else /* CONFIG_BLK_CGROUP */
Dennis Zhoub5f29542018-11-01 17:24:10 -0400521static inline int bio_associate_blkcg(struct bio *bio,
522 struct cgroup_subsys_state *blkcg_css) { return 0; }
Dennis Zhou2268c0f2018-12-05 12:10:29 -0500523static inline void bio_disassociate_blkg(struct bio *bio) { }
524static inline void bio_associate_blkg(struct bio *bio) { }
Tejun Heo852c7882012-03-05 13:15:27 -0800525static inline void bio_disassociate_task(struct bio *bio) { }
Dennis Zhoub5f29542018-11-01 17:24:10 -0400526static inline void bio_clone_blkcg_association(struct bio *dst,
527 struct bio *src) { }
Tejun Heo852c7882012-03-05 13:15:27 -0800528#endif /* CONFIG_BLK_CGROUP */
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#ifdef CONFIG_HIGHMEM
531/*
Alberto Bertogli20b636b2009-02-02 12:41:07 +0100532 * remember never ever reenable interrupts between a bvec_kmap_irq and
533 * bvec_kunmap_irq!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100535static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 unsigned long addr;
538
539 /*
540 * might not be a highmem page, but the preempt/irq count
541 * balancing is a lot nicer this way
542 */
543 local_irq_save(*flags);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800544 addr = (unsigned long) kmap_atomic(bvec->bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 BUG_ON(addr & ~PAGE_MASK);
547
548 return (char *) addr + bvec->bv_offset;
549}
550
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100551static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
554
Cong Wange8e3c3d2011-11-25 23:14:27 +0800555 kunmap_atomic((void *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 local_irq_restore(*flags);
557}
558
559#else
Geert Uytterhoeven11a691b2010-10-21 10:32:29 +0200560static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
561{
562 return page_address(bvec->bv_page) + bvec->bv_offset;
563}
564
565static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
566{
567 *flags = 0;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570
Jens Axboe7a67f632008-08-08 11:17:12 +0200571/*
Akinobu Mitae6863072009-04-17 08:41:21 +0200572 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200573 *
574 * A bio_list anchors a singly-linked list of bios chained through the bi_next
575 * member of the bio. The bio_list also caches the last list member to allow
576 * fast access to the tail.
577 */
578struct bio_list {
579 struct bio *head;
580 struct bio *tail;
581};
582
583static inline int bio_list_empty(const struct bio_list *bl)
584{
585 return bl->head == NULL;
586}
587
588static inline void bio_list_init(struct bio_list *bl)
589{
590 bl->head = bl->tail = NULL;
591}
592
Jens Axboe320ae512013-10-24 09:20:05 +0100593#define BIO_EMPTY_LIST { NULL, NULL }
594
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200595#define bio_list_for_each(bio, bl) \
596 for (bio = (bl)->head; bio; bio = bio->bi_next)
597
598static inline unsigned bio_list_size(const struct bio_list *bl)
599{
600 unsigned sz = 0;
601 struct bio *bio;
602
603 bio_list_for_each(bio, bl)
604 sz++;
605
606 return sz;
607}
608
609static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
610{
611 bio->bi_next = NULL;
612
613 if (bl->tail)
614 bl->tail->bi_next = bio;
615 else
616 bl->head = bio;
617
618 bl->tail = bio;
619}
620
621static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
622{
623 bio->bi_next = bl->head;
624
625 bl->head = bio;
626
627 if (!bl->tail)
628 bl->tail = bio;
629}
630
631static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
632{
633 if (!bl2->head)
634 return;
635
636 if (bl->tail)
637 bl->tail->bi_next = bl2->head;
638 else
639 bl->head = bl2->head;
640
641 bl->tail = bl2->tail;
642}
643
644static inline void bio_list_merge_head(struct bio_list *bl,
645 struct bio_list *bl2)
646{
647 if (!bl2->head)
648 return;
649
650 if (bl->head)
651 bl2->tail->bi_next = bl->head;
652 else
653 bl->tail = bl2->tail;
654
655 bl->head = bl2->head;
656}
657
Geert Uytterhoeven13685a12009-06-10 04:38:40 +0000658static inline struct bio *bio_list_peek(struct bio_list *bl)
659{
660 return bl->head;
661}
662
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200663static inline struct bio *bio_list_pop(struct bio_list *bl)
664{
665 struct bio *bio = bl->head;
666
667 if (bio) {
668 bl->head = bl->head->bi_next;
669 if (!bl->head)
670 bl->tail = NULL;
671
672 bio->bi_next = NULL;
673 }
674
675 return bio;
676}
677
678static inline struct bio *bio_list_get(struct bio_list *bl)
679{
680 struct bio *bio = bl->head;
681
682 bl->head = bl->tail = NULL;
683
684 return bio;
685}
686
Kent Overstreet57fb2332012-08-24 04:56:11 -0700687/*
Mike Snitzer0ef5a502016-05-05 11:54:22 -0400688 * Increment chain count for the bio. Make sure the CHAIN flag update
689 * is visible before the raised count.
690 */
691static inline void bio_inc_remaining(struct bio *bio)
692{
693 bio_set_flag(bio, BIO_CHAIN);
694 smp_mb__before_atomic();
695 atomic_inc(&bio->__bi_remaining);
696}
697
698/*
Kent Overstreet57fb2332012-08-24 04:56:11 -0700699 * bio_set is used to allow other portions of the IO system to
700 * allocate their own private memory pools for bio and iovec structures.
701 * These memory pools in turn all allocate from the bio_slab
702 * and the bvec_slabs[].
703 */
704#define BIO_POOL_SIZE 2
Kent Overstreet57fb2332012-08-24 04:56:11 -0700705
706struct bio_set {
707 struct kmem_cache *bio_slab;
708 unsigned int front_pad;
709
Kent Overstreet8aa6ba22018-05-08 21:33:50 -0400710 mempool_t bio_pool;
711 mempool_t bvec_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700712#if defined(CONFIG_BLK_DEV_INTEGRITY)
Kent Overstreet8aa6ba22018-05-08 21:33:50 -0400713 mempool_t bio_integrity_pool;
714 mempool_t bvec_integrity_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700715#endif
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700716
717 /*
718 * Deadlock avoidance for stacking block drivers: see comments in
719 * bio_alloc_bioset() for details
720 */
721 spinlock_t rescue_lock;
722 struct bio_list rescue_list;
723 struct work_struct rescue_work;
724 struct workqueue_struct *rescue_workqueue;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700725};
726
727struct biovec_slab {
728 int nr_vecs;
729 char *name;
730 struct kmem_cache *slab;
731};
732
Kent Overstreet338aa962018-05-20 18:25:47 -0400733static inline bool bioset_initialized(struct bio_set *bs)
734{
735 return bs->bio_slab != NULL;
736}
737
Kent Overstreet57fb2332012-08-24 04:56:11 -0700738/*
739 * a small number of entries is fine, not going to be performance critical.
740 * basically we just need to survive
741 */
742#define BIO_SPLIT_ENTRIES 2
743
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200744#if defined(CONFIG_BLK_DEV_INTEGRITY)
745
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800746#define bip_for_each_vec(bvl, bip, iter) \
747 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200748
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200749#define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
750 for_each_bio(_bio) \
751 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
752
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200753extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200754extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
Dmitry Monakhove23947b2017-06-29 11:31:11 -0700755extern bool bio_integrity_prep(struct bio *);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200756extern void bio_integrity_advance(struct bio *, unsigned int);
Dmitry Monakhovfbd08e72017-06-29 11:31:10 -0700757extern void bio_integrity_trim(struct bio *);
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700758extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200759extern int bioset_integrity_create(struct bio_set *, int);
760extern void bioset_integrity_free(struct bio_set *);
761extern void bio_integrity_init(void);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200762
763#else /* CONFIG_BLK_DEV_INTEGRITY */
764
Martin K. Petersenc6115292014-09-26 19:20:08 -0400765static inline void *bio_integrity(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100766{
Martin K. Petersenc6115292014-09-26 19:20:08 -0400767 return NULL;
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100768}
769
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100770static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
771{
772 return 0;
773}
774
775static inline void bioset_integrity_free (struct bio_set *bs)
776{
777 return;
778}
779
Dmitry Monakhove23947b2017-06-29 11:31:11 -0700780static inline bool bio_integrity_prep(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100781{
Dmitry Monakhove23947b2017-06-29 11:31:11 -0700782 return true;
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100783}
784
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100785static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700786 gfp_t gfp_mask)
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100787{
788 return 0;
789}
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100790
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100791static inline void bio_integrity_advance(struct bio *bio,
792 unsigned int bytes_done)
793{
794 return;
795}
796
Dmitry Monakhovfbd08e72017-06-29 11:31:10 -0700797static inline void bio_integrity_trim(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100798{
799 return;
800}
801
802static inline void bio_integrity_init(void)
803{
804 return;
805}
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200806
Martin K. Petersenc6115292014-09-26 19:20:08 -0400807static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
808{
809 return false;
810}
811
Keith Busch06c1e392015-12-03 09:32:21 -0700812static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
813 unsigned int nr)
814{
815 return ERR_PTR(-EINVAL);
816}
817
818static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
819 unsigned int len, unsigned int offset)
820{
821 return 0;
822}
823
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200824#endif /* CONFIG_BLK_DEV_INTEGRITY */
825
David Howells02a5e0a2007-08-11 22:34:32 +0200826#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif /* __LINUX_BIO_H */