blob: 930cb73c894b309aa3643df6a0242c1bbb39f2c9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define BIO_MAX_SIZE (BIO_MAX_PAGES << PAGE_CACHE_SHIFT)
45#define BIO_MAX_SECTORS (BIO_MAX_SIZE >> 9)
46
47/*
Jens Axboe22e2c502005-06-27 10:55:12 +020048 * upper 16 bits of bi_rw define the io priority of this bio
49 */
50#define BIO_PRIO_SHIFT (8 * sizeof(unsigned long) - IOPRIO_BITS)
51#define bio_prio(bio) ((bio)->bi_rw >> BIO_PRIO_SHIFT)
52#define bio_prio_valid(bio) ioprio_valid(bio_prio(bio))
53
54#define bio_set_prio(bio, prio) do { \
55 WARN_ON(prio >= (1 << IOPRIO_BITS)); \
56 (bio)->bi_rw &= ((1UL << BIO_PRIO_SHIFT) - 1); \
57 (bio)->bi_rw |= ((unsigned long) (prio) << BIO_PRIO_SHIFT); \
58} while (0)
59
60/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * various member access, note that bio_data should of course not be used
62 * on highmem page vectors
63 */
64#define bio_iovec_idx(bio, idx) (&((bio)->bi_io_vec[(idx)]))
Kent Overstreeta4ad39b12013-08-07 14:24:32 -070065#define __bio_iovec(bio) bio_iovec_idx((bio), (bio)->bi_iter.bi_idx)
Kent Overstreet79886132013-11-23 17:19:00 -080066
Kent Overstreet4550dd62013-08-07 14:26:21 -070067#define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
Kent Overstreeta4ad39b12013-08-07 14:24:32 -070068
Kent Overstreet4550dd62013-08-07 14:26:21 -070069#define bvec_iter_page(bvec, iter) \
70 (__bvec_iter_bvec((bvec), (iter))->bv_page)
71
72#define bvec_iter_len(bvec, iter) \
73 min((iter).bi_size, \
74 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
75
76#define bvec_iter_offset(bvec, iter) \
77 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
78
79#define bvec_iter_bvec(bvec, iter) \
80((struct bio_vec) { \
81 .bv_page = bvec_iter_page((bvec), (iter)), \
82 .bv_len = bvec_iter_len((bvec), (iter)), \
83 .bv_offset = bvec_iter_offset((bvec), (iter)), \
84})
85
86#define bio_iter_iovec(bio, iter) \
87 bvec_iter_bvec((bio)->bi_io_vec, (iter))
88
89#define bio_iter_page(bio, iter) \
90 bvec_iter_page((bio)->bi_io_vec, (iter))
91#define bio_iter_len(bio, iter) \
92 bvec_iter_len((bio)->bi_io_vec, (iter))
93#define bio_iter_offset(bio, iter) \
94 bvec_iter_offset((bio)->bi_io_vec, (iter))
95
96#define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
97#define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
98#define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
Kent Overstreet79886132013-11-23 17:19:00 -080099
Kent Overstreet4f024f32013-10-11 15:44:27 -0700100#define bio_segments(bio) ((bio)->bi_vcnt - (bio)->bi_iter.bi_idx)
101#define bio_sectors(bio) ((bio)->bi_iter.bi_size >> 9)
102#define bio_end_sector(bio) ((bio)->bi_iter.bi_sector + bio_sectors((bio)))
Jens Axboebf2de6f2007-09-27 13:01:25 +0200103
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900104static inline unsigned int bio_cur_bytes(struct bio *bio)
Jens Axboebf2de6f2007-09-27 13:01:25 +0200105{
106 if (bio->bi_vcnt)
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700107 return bio_iovec(bio).bv_len;
David Woodhousefb2dce82008-08-05 18:01:53 +0100108 else /* dataless requests such as discard */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700109 return bio->bi_iter.bi_size;
Jens Axboebf2de6f2007-09-27 13:01:25 +0200110}
111
112static inline void *bio_data(struct bio *bio)
113{
114 if (bio->bi_vcnt)
115 return page_address(bio_page(bio)) + bio_offset(bio);
116
117 return NULL;
118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * will die
122 */
123#define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
124#define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
125
126/*
127 * queues that have highmem support enabled may still need to revert to
128 * PIO transfers occasionally and thus map high pages temporarily. For
129 * permanent PIO fall back, user is probably better off disabling highmem
130 * I/O completely on that queue (see ide-dma for example)
131 */
Zhao Hongjiang0eb5afb2013-07-08 15:22:50 +0800132#define __bio_kmap_atomic(bio, idx) \
Cong Wange8e3c3d2011-11-25 23:14:27 +0800133 (kmap_atomic(bio_iovec_idx((bio), (idx))->bv_page) + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 bio_iovec_idx((bio), (idx))->bv_offset)
135
Zhao Hongjiang0eb5afb2013-07-08 15:22:50 +0800136#define __bio_kunmap_atomic(addr) kunmap_atomic(addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * merge helpers etc
140 */
141
142#define __BVEC_END(bio) bio_iovec_idx((bio), (bio)->bi_vcnt - 1)
Kent Overstreet4f024f32013-10-11 15:44:27 -0700143#define __BVEC_START(bio) bio_iovec_idx((bio), (bio)->bi_iter.bi_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jeremy Fitzhardingef92131c2008-10-29 14:10:51 +0100145/* Default implementation of BIOVEC_PHYS_MERGEABLE */
146#define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
147 ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
150 * allow arch override, for eg virtualized architectures (put in asm/io.h)
151 */
152#ifndef BIOVEC_PHYS_MERGEABLE
153#define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
Jeremy Fitzhardingef92131c2008-10-29 14:10:51 +0100154 __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
158 (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
159#define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400160 __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 -0700161#define BIO_SEG_BOUNDARY(q, b1, b2) \
162 BIOVEC_SEG_BOUNDARY((q), __BVEC_END((b1)), __BVEC_START((b2)))
163
NeilBrown6712ecf2007-09-27 12:47:43 +0200164#define bio_io_error(bio) bio_endio((bio), -EIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166/*
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800167 * drivers should _never_ use the all version - the bio may have been split
168 * before it got to the driver and the driver won't own all of it
169 */
170#define bio_for_each_segment_all(bvl, bio, i) \
171 for (i = 0; \
172 bvl = bio_iovec_idx((bio), (i)), i < (bio)->bi_vcnt; \
173 i++)
174
Kent Overstreet4550dd62013-08-07 14:26:21 -0700175static inline void bvec_iter_advance(struct bio_vec *bv, struct bvec_iter *iter,
176 unsigned bytes)
177{
178 WARN_ONCE(bytes > iter->bi_size,
179 "Attempted to advance past end of bvec iter\n");
180
181 while (bytes) {
182 unsigned len = min(bytes, bvec_iter_len(bv, *iter));
183
184 bytes -= len;
185 iter->bi_size -= len;
186 iter->bi_bvec_done += len;
187
188 if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
189 iter->bi_bvec_done = 0;
190 iter->bi_idx++;
191 }
192 }
193}
194
195#define for_each_bvec(bvl, bio_vec, iter, start) \
196 for ((iter) = start; \
197 (bvl) = bvec_iter_bvec((bio_vec), (iter)), \
198 (iter).bi_size; \
199 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
200
201
202static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
203 unsigned bytes)
204{
205 iter->bi_sector += bytes >> 9;
206
207 if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK)
208 iter->bi_size -= bytes;
209 else
210 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
211}
212
Kent Overstreet79886132013-11-23 17:19:00 -0800213#define __bio_for_each_segment(bvl, bio, iter, start) \
214 for (iter = (start); \
Kent Overstreet4550dd62013-08-07 14:26:21 -0700215 (iter).bi_size && \
216 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
217 bio_advance_iter((bio), &(iter), (bvl).bv_len))
Kent Overstreet79886132013-11-23 17:19:00 -0800218
219#define bio_for_each_segment(bvl, bio, iter) \
220 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
221
Kent Overstreet4550dd62013-08-07 14:26:21 -0700222#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224/*
225 * get a reference to a bio, so it won't disappear. the intended use is
226 * something like:
227 *
228 * bio_get(bio);
229 * submit_bio(rw, bio);
230 * if (bio->bi_flags ...)
231 * do_something
232 * bio_put(bio);
233 *
234 * without the bio_get(), it could potentially complete I/O before submit_bio
235 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
236 * runs
237 */
238#define bio_get(bio) atomic_inc(&(bio)->bi_cnt)
239
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200240#if defined(CONFIG_BLK_DEV_INTEGRITY)
241/*
242 * bio integrity payload
243 */
244struct bio_integrity_payload {
245 struct bio *bip_bio; /* parent bio */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200246
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800247 struct bvec_iter bip_iter;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200248
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800249 /* kill - should just use bip_vec */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200250 void *bip_buf; /* generated integrity data */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200251
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800252 bio_end_io_t *bip_end_io; /* saved I/O completion fn */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200253
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200254 unsigned short bip_slab; /* slab the bip came from */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200255 unsigned short bip_vcnt; /* # of integrity bio_vecs */
Kent Overstreet29ed7812012-09-04 09:54:22 -0700256 unsigned bip_owns_buf:1; /* should free bip_buf */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200257
258 struct work_struct bip_work; /* I/O completion */
Kent Overstreet6fda9812012-10-12 13:18:27 -0700259
260 struct bio_vec *bip_vec;
261 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200262};
263#endif /* CONFIG_BLK_DEV_INTEGRITY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265/*
266 * A bio_pair is used when we need to split a bio.
267 * This can only happen for a bio that refers to just one
268 * page of data, and in the unusual situation when the
269 * page crosses a chunk/device boundary
270 *
271 * The address of the master bio is stored in bio1.bi_private
272 * The address of the pool the pair was allocated from is stored
273 * in bio2.bi_private
274 */
275struct bio_pair {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200276 struct bio bio1, bio2;
277 struct bio_vec bv1, bv2;
278#if defined(CONFIG_BLK_DEV_INTEGRITY)
279 struct bio_integrity_payload bip1, bip2;
280 struct bio_vec iv1, iv2;
281#endif
282 atomic_t cnt;
283 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
Denis ChengRq6feef532008-10-09 08:57:05 +0200285extern struct bio_pair *bio_split(struct bio *bi, int first_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286extern void bio_pair_release(struct bio_pair *dbio);
Kent Overstreet6678d832013-08-07 11:14:32 -0700287extern void bio_trim(struct bio *bio, int offset, int size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jens Axboebb799ca2008-12-10 15:35:05 +0100289extern struct bio_set *bioset_create(unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290extern void bioset_free(struct bio_set *);
Kent Overstreet9f060e22012-10-12 15:29:33 -0700291extern mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Al Virodd0fc662005-10-07 07:46:04 +0100293extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294extern void bio_put(struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700296extern void __bio_clone(struct bio *, struct bio *);
297extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
298
Kent Overstreet3f86a822012-09-06 15:35:01 -0700299extern struct bio_set *fs_bio_set;
300
301static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
302{
303 return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
304}
305
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700306static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
307{
308 return bio_clone_bioset(bio, gfp_mask, fs_bio_set);
309}
310
Kent Overstreet3f86a822012-09-06 15:35:01 -0700311static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
312{
313 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
314}
315
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700316static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
317{
318 return bio_clone_bioset(bio, gfp_mask, NULL);
319
320}
321
NeilBrown6712ecf2007-09-27 12:47:43 +0200322extern void bio_endio(struct bio *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323struct request_queue;
324extern int bio_phys_segments(struct request_queue *, struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Kent Overstreet9e882242012-09-10 14:41:12 -0700326extern int submit_bio_wait(int rw, struct bio *bio);
Kent Overstreet054bdf62012-09-28 13:17:55 -0700327extern void bio_advance(struct bio *, unsigned);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329extern void bio_init(struct bio *);
Kent Overstreetf44b48c2012-09-06 15:34:58 -0700330extern void bio_reset(struct bio *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
Mike Christie6e68af62005-11-11 05:30:27 -0600333extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
334 unsigned int, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335extern int bio_get_nr_vecs(struct block_device *);
Martin K. Petersenad3316b2008-10-01 22:42:53 -0400336extern sector_t bio_sector_offset(struct bio *, unsigned short, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337extern struct bio *bio_map_user(struct request_queue *, struct block_device *,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +0900338 unsigned long, unsigned int, int, gfp_t);
James Bottomley f1970ba2005-06-20 14:06:52 +0200339struct sg_iovec;
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900340struct rq_map_data;
James Bottomley f1970ba2005-06-20 14:06:52 +0200341extern struct bio *bio_map_user_iov(struct request_queue *,
342 struct block_device *,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +0900343 struct sg_iovec *, int, int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344extern void bio_unmap_user(struct bio *);
Mike Christie df46b9a2005-06-20 14:04:44 +0200345extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
Al Viro27496a82005-10-21 03:20:48 -0400346 gfp_t);
FUJITA Tomonori68154e92008-04-25 12:47:50 +0200347extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
348 gfp_t, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349extern void bio_set_pages_dirty(struct bio *bio);
350extern void bio_check_pages_dirty(struct bio *bio);
Ilya Loginov2d4dc892009-11-26 09:16:19 +0100351
352#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
353# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
354#endif
355#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
356extern void bio_flush_dcache_pages(struct bio *bi);
357#else
358static inline void bio_flush_dcache_pages(struct bio *bi)
359{
360}
361#endif
362
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700363extern void bio_copy_data(struct bio *dst, struct bio *src);
Kent Overstreeta0787602012-09-10 14:03:28 -0700364extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
Kent Overstreet16ac3d62012-09-10 13:57:51 -0700365
FUJITA Tomonori152e2832008-08-28 16:17:06 +0900366extern struct bio *bio_copy_user(struct request_queue *, struct rq_map_data *,
367 unsigned long, unsigned int, int, gfp_t);
368extern struct bio *bio_copy_user_iov(struct request_queue *,
369 struct rq_map_data *, struct sg_iovec *,
FUJITA Tomonoria3bce902008-08-28 16:17:05 +0900370 int, int, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371extern int bio_uncopy_user(struct bio *);
372void zero_fill_bio(struct bio *bio);
Kent Overstreet9f060e22012-10-12 15:29:33 -0700373extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
374extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200375extern unsigned int bvec_nr_vecs(unsigned short idx);
Martin K. Petersen51d654e2008-06-17 18:59:56 +0200376
Tejun Heo852c7882012-03-05 13:15:27 -0800377#ifdef CONFIG_BLK_CGROUP
378int bio_associate_current(struct bio *bio);
379void bio_disassociate_task(struct bio *bio);
380#else /* CONFIG_BLK_CGROUP */
381static inline int bio_associate_current(struct bio *bio) { return -ENOENT; }
382static inline void bio_disassociate_task(struct bio *bio) { }
383#endif /* CONFIG_BLK_CGROUP */
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifdef CONFIG_HIGHMEM
386/*
Alberto Bertogli20b636b2009-02-02 12:41:07 +0100387 * remember never ever reenable interrupts between a bvec_kmap_irq and
388 * bvec_kunmap_irq!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100390static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 unsigned long addr;
393
394 /*
395 * might not be a highmem page, but the preempt/irq count
396 * balancing is a lot nicer this way
397 */
398 local_irq_save(*flags);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800399 addr = (unsigned long) kmap_atomic(bvec->bv_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 BUG_ON(addr & ~PAGE_MASK);
402
403 return (char *) addr + bvec->bv_offset;
404}
405
Alberto Bertogli4f570f92009-11-02 11:40:16 +0100406static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
409
Cong Wange8e3c3d2011-11-25 23:14:27 +0800410 kunmap_atomic((void *) ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 local_irq_restore(*flags);
412}
413
414#else
Geert Uytterhoeven11a691b2010-10-21 10:32:29 +0200415static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
416{
417 return page_address(bvec->bv_page) + bvec->bv_offset;
418}
419
420static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
421{
422 *flags = 0;
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
425
Adrian Bunkc2d08da2005-09-10 00:27:18 -0700426static inline char *__bio_kmap_irq(struct bio *bio, unsigned short idx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned long *flags)
428{
429 return bvec_kmap_irq(bio_iovec_idx(bio, idx), flags);
430}
431#define __bio_kunmap_irq(buf, flags) bvec_kunmap_irq(buf, flags)
432
433#define bio_kmap_irq(bio, flags) \
Kent Overstreet4f024f32013-10-11 15:44:27 -0700434 __bio_kmap_irq((bio), (bio)->bi_iter.bi_idx, (flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#define bio_kunmap_irq(buf,flags) __bio_kunmap_irq(buf, flags)
436
Jens Axboe7a67f632008-08-08 11:17:12 +0200437/*
438 * Check whether this bio carries any data or not. A NULL bio is allowed.
439 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400440static inline bool bio_has_data(struct bio *bio)
Jens Axboe7a67f632008-08-08 11:17:12 +0200441{
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400442 if (bio && bio->bi_vcnt)
443 return true;
444
445 return false;
446}
447
448static inline bool bio_is_rw(struct bio *bio)
449{
450 if (!bio_has_data(bio))
451 return false;
452
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400453 if (bio->bi_rw & REQ_WRITE_SAME)
454 return false;
455
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400456 return true;
457}
458
459static inline bool bio_mergeable(struct bio *bio)
460{
461 if (bio->bi_rw & REQ_NOMERGE_FLAGS)
462 return false;
463
464 return true;
Jens Axboe7a67f632008-08-08 11:17:12 +0200465}
466
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200467/*
Akinobu Mitae6863072009-04-17 08:41:21 +0200468 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200469 *
470 * A bio_list anchors a singly-linked list of bios chained through the bi_next
471 * member of the bio. The bio_list also caches the last list member to allow
472 * fast access to the tail.
473 */
474struct bio_list {
475 struct bio *head;
476 struct bio *tail;
477};
478
479static inline int bio_list_empty(const struct bio_list *bl)
480{
481 return bl->head == NULL;
482}
483
484static inline void bio_list_init(struct bio_list *bl)
485{
486 bl->head = bl->tail = NULL;
487}
488
Jens Axboe320ae512013-10-24 09:20:05 +0100489#define BIO_EMPTY_LIST { NULL, NULL }
490
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200491#define bio_list_for_each(bio, bl) \
492 for (bio = (bl)->head; bio; bio = bio->bi_next)
493
494static inline unsigned bio_list_size(const struct bio_list *bl)
495{
496 unsigned sz = 0;
497 struct bio *bio;
498
499 bio_list_for_each(bio, bl)
500 sz++;
501
502 return sz;
503}
504
505static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
506{
507 bio->bi_next = NULL;
508
509 if (bl->tail)
510 bl->tail->bi_next = bio;
511 else
512 bl->head = bio;
513
514 bl->tail = bio;
515}
516
517static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
518{
519 bio->bi_next = bl->head;
520
521 bl->head = bio;
522
523 if (!bl->tail)
524 bl->tail = bio;
525}
526
527static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
528{
529 if (!bl2->head)
530 return;
531
532 if (bl->tail)
533 bl->tail->bi_next = bl2->head;
534 else
535 bl->head = bl2->head;
536
537 bl->tail = bl2->tail;
538}
539
540static inline void bio_list_merge_head(struct bio_list *bl,
541 struct bio_list *bl2)
542{
543 if (!bl2->head)
544 return;
545
546 if (bl->head)
547 bl2->tail->bi_next = bl->head;
548 else
549 bl->tail = bl2->tail;
550
551 bl->head = bl2->head;
552}
553
Geert Uytterhoeven13685a12009-06-10 04:38:40 +0000554static inline struct bio *bio_list_peek(struct bio_list *bl)
555{
556 return bl->head;
557}
558
Christoph Hellwig8f3d8ba2009-04-07 19:55:13 +0200559static inline struct bio *bio_list_pop(struct bio_list *bl)
560{
561 struct bio *bio = bl->head;
562
563 if (bio) {
564 bl->head = bl->head->bi_next;
565 if (!bl->head)
566 bl->tail = NULL;
567
568 bio->bi_next = NULL;
569 }
570
571 return bio;
572}
573
574static inline struct bio *bio_list_get(struct bio_list *bl)
575{
576 struct bio *bio = bl->head;
577
578 bl->head = bl->tail = NULL;
579
580 return bio;
581}
582
Kent Overstreet57fb2332012-08-24 04:56:11 -0700583/*
584 * bio_set is used to allow other portions of the IO system to
585 * allocate their own private memory pools for bio and iovec structures.
586 * These memory pools in turn all allocate from the bio_slab
587 * and the bvec_slabs[].
588 */
589#define BIO_POOL_SIZE 2
590#define BIOVEC_NR_POOLS 6
591#define BIOVEC_MAX_IDX (BIOVEC_NR_POOLS - 1)
592
593struct bio_set {
594 struct kmem_cache *bio_slab;
595 unsigned int front_pad;
596
597 mempool_t *bio_pool;
Kent Overstreet9f060e22012-10-12 15:29:33 -0700598 mempool_t *bvec_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700599#if defined(CONFIG_BLK_DEV_INTEGRITY)
600 mempool_t *bio_integrity_pool;
Kent Overstreet9f060e22012-10-12 15:29:33 -0700601 mempool_t *bvec_integrity_pool;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700602#endif
Kent Overstreetdf2cb6d2012-09-10 14:33:46 -0700603
604 /*
605 * Deadlock avoidance for stacking block drivers: see comments in
606 * bio_alloc_bioset() for details
607 */
608 spinlock_t rescue_lock;
609 struct bio_list rescue_list;
610 struct work_struct rescue_work;
611 struct workqueue_struct *rescue_workqueue;
Kent Overstreet57fb2332012-08-24 04:56:11 -0700612};
613
614struct biovec_slab {
615 int nr_vecs;
616 char *name;
617 struct kmem_cache *slab;
618};
619
620/*
621 * a small number of entries is fine, not going to be performance critical.
622 * basically we just need to survive
623 */
624#define BIO_SPLIT_ENTRIES 2
625
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200626#if defined(CONFIG_BLK_DEV_INTEGRITY)
627
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800628
629
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200630#define bip_vec_idx(bip, idx) (&(bip->bip_vec[(idx)]))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200631
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800632#define bip_for_each_vec(bvl, bip, iter) \
633 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200634
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200635#define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
636 for_each_bio(_bio) \
637 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
638
Alberto Bertogli8deaf722008-10-02 12:46:53 +0200639#define bio_integrity(bio) (bio->bi_integrity != NULL)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200640
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200641extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700642extern void bio_integrity_free(struct bio *);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200643extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
644extern int bio_integrity_enabled(struct bio *bio);
645extern int bio_integrity_set_tag(struct bio *, void *, unsigned int);
646extern int bio_integrity_get_tag(struct bio *, void *, unsigned int);
647extern int bio_integrity_prep(struct bio *);
648extern void bio_integrity_endio(struct bio *, int);
649extern void bio_integrity_advance(struct bio *, unsigned int);
650extern void bio_integrity_trim(struct bio *, unsigned int, unsigned int);
651extern void bio_integrity_split(struct bio *, struct bio_pair *, int);
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700652extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
Martin K. Petersen7878cba2009-06-26 15:37:49 +0200653extern int bioset_integrity_create(struct bio_set *, int);
654extern void bioset_integrity_free(struct bio_set *);
655extern void bio_integrity_init(void);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200656
657#else /* CONFIG_BLK_DEV_INTEGRITY */
658
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100659static inline int bio_integrity(struct bio *bio)
660{
661 return 0;
662}
663
664static inline int bio_integrity_enabled(struct bio *bio)
665{
666 return 0;
667}
668
669static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
670{
671 return 0;
672}
673
674static inline void bioset_integrity_free (struct bio_set *bs)
675{
676 return;
677}
678
679static inline int bio_integrity_prep(struct bio *bio)
680{
681 return 0;
682}
683
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700684static inline void bio_integrity_free(struct bio *bio)
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100685{
686 return;
687}
688
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100689static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
Kent Overstreet1e2a410f2012-09-06 15:34:56 -0700690 gfp_t gfp_mask)
Stephen Rothwell0c614e22011-11-16 09:21:48 +0100691{
692 return 0;
693}
Martin K. Petersen6898e3b2012-01-13 08:15:33 +0100694
695static inline void bio_integrity_split(struct bio *bio, struct bio_pair *bp,
696 int sectors)
697{
698 return;
699}
700
701static inline void bio_integrity_advance(struct bio *bio,
702 unsigned int bytes_done)
703{
704 return;
705}
706
707static inline void bio_integrity_trim(struct bio *bio, unsigned int offset,
708 unsigned int sectors)
709{
710 return;
711}
712
713static inline void bio_integrity_init(void)
714{
715 return;
716}
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200717
718#endif /* CONFIG_BLK_DEV_INTEGRITY */
719
David Howells02a5e0a2007-08-11 22:34:32 +0200720#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#endif /* __LINUX_BIO_H */