Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[linux-2.6-block.git] / include / linux / bio.h
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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
7cc01581 10 *
1da177e4
LT
11 * 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>
22e2c507 23#include <linux/ioprio.h>
187f1882 24#include <linux/bug.h>
1da177e4 25
02a5e0ac
DH
26#ifdef CONFIG_BLOCK
27
1da177e4
LT
28#include <asm/io.h>
29
7cc01581
TH
30/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
31#include <linux/blk_types.h>
32
1da177e4
LT
33#define BIO_DEBUG
34
35#ifdef BIO_DEBUG
36#define BIO_BUG_ON BUG_ON
37#else
38#define BIO_BUG_ON
39#endif
40
225311a4
HY
41#ifdef CONFIG_THP_SWAP
42#if HPAGE_PMD_NR > 256
43#define BIO_MAX_PAGES HPAGE_PMD_NR
44#else
d84a8477 45#define BIO_MAX_PAGES 256
225311a4
HY
46#endif
47#else
48#define BIO_MAX_PAGES 256
49#endif
1da177e4 50
43b62ce3
MC
51#define bio_prio(bio) (bio)->bi_ioprio
52#define bio_set_prio(bio, prio) ((bio)->bi_ioprio = prio)
22e2c507 53
4550dd6c
KO
54#define bio_iter_iovec(bio, iter) \
55 bvec_iter_bvec((bio)->bi_io_vec, (iter))
56
57#define bio_iter_page(bio, iter) \
58 bvec_iter_page((bio)->bi_io_vec, (iter))
59#define bio_iter_len(bio, iter) \
60 bvec_iter_len((bio)->bi_io_vec, (iter))
61#define bio_iter_offset(bio, iter) \
62 bvec_iter_offset((bio)->bi_io_vec, (iter))
63
64#define bio_page(bio) bio_iter_page((bio), (bio)->bi_iter)
65#define bio_offset(bio) bio_iter_offset((bio), (bio)->bi_iter)
66#define bio_iovec(bio) bio_iter_iovec((bio), (bio)->bi_iter)
7988613b 67
458b76ed
KO
68#define bio_multiple_segments(bio) \
69 ((bio)->bi_iter.bi_size != bio_iovec(bio).bv_len)
38a72dac
KO
70
71#define bvec_iter_sectors(iter) ((iter).bi_size >> 9)
72#define bvec_iter_end_sector(iter) ((iter).bi_sector + bvec_iter_sectors((iter)))
73
74#define bio_sectors(bio) bvec_iter_sectors((bio)->bi_iter)
75#define bio_end_sector(bio) bvec_iter_end_sector((bio)->bi_iter)
bf2de6f5 76
d3849953
CH
77/*
78 * Return the data direction, READ or WRITE.
79 */
80#define bio_data_dir(bio) \
81 (op_is_write(bio_op(bio)) ? WRITE : READ)
82
458b76ed
KO
83/*
84 * Check whether this bio carries any data or not. A NULL bio is allowed.
85 */
86static inline bool bio_has_data(struct bio *bio)
87{
88 if (bio &&
89 bio->bi_iter.bi_size &&
7afafc8a 90 bio_op(bio) != REQ_OP_DISCARD &&
a6f0788e
CK
91 bio_op(bio) != REQ_OP_SECURE_ERASE &&
92 bio_op(bio) != REQ_OP_WRITE_ZEROES)
458b76ed
KO
93 return true;
94
95 return false;
96}
97
95fe6c1a
MC
98static inline bool bio_no_advance_iter(struct bio *bio)
99{
7afafc8a
AH
100 return bio_op(bio) == REQ_OP_DISCARD ||
101 bio_op(bio) == REQ_OP_SECURE_ERASE ||
a6f0788e
CK
102 bio_op(bio) == REQ_OP_WRITE_SAME ||
103 bio_op(bio) == REQ_OP_WRITE_ZEROES;
95fe6c1a
MC
104}
105
458b76ed
KO
106static inline bool bio_mergeable(struct bio *bio)
107{
1eff9d32 108 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
458b76ed
KO
109 return false;
110
111 return true;
112}
113
2e46e8b2 114static inline unsigned int bio_cur_bytes(struct bio *bio)
bf2de6f5 115{
458b76ed 116 if (bio_has_data(bio))
a4ad39b1 117 return bio_iovec(bio).bv_len;
fb2dce86 118 else /* dataless requests such as discard */
4f024f37 119 return bio->bi_iter.bi_size;
bf2de6f5
JA
120}
121
122static inline void *bio_data(struct bio *bio)
123{
458b76ed 124 if (bio_has_data(bio))
bf2de6f5
JA
125 return page_address(bio_page(bio)) + bio_offset(bio);
126
127 return NULL;
128}
1da177e4
LT
129
130/*
131 * will die
132 */
1da177e4
LT
133#define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
134
1da177e4
LT
135/*
136 * merge helpers etc
137 */
138
f92131c3
JF
139/* Default implementation of BIOVEC_PHYS_MERGEABLE */
140#define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
141 ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
142
1da177e4
LT
143/*
144 * allow arch override, for eg virtualized architectures (put in asm/io.h)
145 */
146#ifndef BIOVEC_PHYS_MERGEABLE
147#define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
f92131c3 148 __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
1da177e4
LT
149#endif
150
1da177e4
LT
151#define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
152 (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
153#define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
ae03bf63 154 __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
1da177e4 155
d74c6d51
KO
156/*
157 * drivers should _never_ use the all version - the bio may have been split
158 * before it got to the driver and the driver won't own all of it
159 */
160#define bio_for_each_segment_all(bvl, bio, i) \
f619d254 161 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
d74c6d51 162
4550dd6c
KO
163static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
164 unsigned bytes)
165{
166 iter->bi_sector += bytes >> 9;
167
f9df1cd9 168 if (bio_no_advance_iter(bio)) {
4550dd6c 169 iter->bi_size -= bytes;
f9df1cd9
DM
170 iter->bi_done += bytes;
171 } else {
4550dd6c 172 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
b1fb2c52
DM
173 /* TODO: It is reasonable to complete bio with error here. */
174 }
4550dd6c
KO
175}
176
f9df1cd9
DM
177static inline bool bio_rewind_iter(struct bio *bio, struct bvec_iter *iter,
178 unsigned int bytes)
179{
180 iter->bi_sector -= bytes >> 9;
181
182 if (bio_no_advance_iter(bio)) {
183 iter->bi_size += bytes;
184 iter->bi_done -= bytes;
185 return true;
186 }
187
188 return bvec_iter_rewind(bio->bi_io_vec, iter, bytes);
189}
190
7988613b
KO
191#define __bio_for_each_segment(bvl, bio, iter, start) \
192 for (iter = (start); \
4550dd6c
KO
193 (iter).bi_size && \
194 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
195 bio_advance_iter((bio), &(iter), (bvl).bv_len))
7988613b
KO
196
197#define bio_for_each_segment(bvl, bio, iter) \
198 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
199
4550dd6c 200#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
1da177e4 201
f4595875 202static inline unsigned bio_segments(struct bio *bio)
458b76ed
KO
203{
204 unsigned segs = 0;
205 struct bio_vec bv;
206 struct bvec_iter iter;
207
8423ae3d 208 /*
a6f0788e
CK
209 * We special case discard/write same/write zeroes, because they
210 * interpret bi_size differently:
8423ae3d
KO
211 */
212
a6f0788e
CK
213 switch (bio_op(bio)) {
214 case REQ_OP_DISCARD:
215 case REQ_OP_SECURE_ERASE:
a6f0788e 216 case REQ_OP_WRITE_ZEROES:
f9d03f96
CH
217 return 0;
218 case REQ_OP_WRITE_SAME:
8423ae3d 219 return 1;
a6f0788e
CK
220 default:
221 break;
222 }
8423ae3d 223
f4595875 224 bio_for_each_segment(bv, bio, iter)
458b76ed
KO
225 segs++;
226
227 return segs;
228}
229
1da177e4
LT
230/*
231 * get a reference to a bio, so it won't disappear. the intended use is
232 * something like:
233 *
234 * bio_get(bio);
235 * submit_bio(rw, bio);
236 * if (bio->bi_flags ...)
237 * do_something
238 * bio_put(bio);
239 *
240 * without the bio_get(), it could potentially complete I/O before submit_bio
241 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
242 * runs
243 */
dac56212
JA
244static inline void bio_get(struct bio *bio)
245{
246 bio->bi_flags |= (1 << BIO_REFFED);
247 smp_mb__before_atomic();
248 atomic_inc(&bio->__bi_cnt);
249}
250
251static inline void bio_cnt_set(struct bio *bio, unsigned int count)
252{
253 if (count != 1) {
254 bio->bi_flags |= (1 << BIO_REFFED);
255 smp_mb__before_atomic();
256 }
257 atomic_set(&bio->__bi_cnt, count);
258}
1da177e4 259
b7c44ed9
JA
260static inline bool bio_flagged(struct bio *bio, unsigned int bit)
261{
2c68f6dc 262 return (bio->bi_flags & (1U << bit)) != 0;
b7c44ed9
JA
263}
264
265static inline void bio_set_flag(struct bio *bio, unsigned int bit)
266{
2c68f6dc 267 bio->bi_flags |= (1U << bit);
b7c44ed9
JA
268}
269
270static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
271{
2c68f6dc 272 bio->bi_flags &= ~(1U << bit);
b7c44ed9
JA
273}
274
7bcd79ac
ML
275static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
276{
277 *bv = bio_iovec(bio);
278}
279
280static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
281{
282 struct bvec_iter iter = bio->bi_iter;
283 int idx;
284
7bcd79ac
ML
285 if (unlikely(!bio_multiple_segments(bio))) {
286 *bv = bio_iovec(bio);
287 return;
288 }
289
290 bio_advance_iter(bio, &iter, iter.bi_size);
291
292 if (!iter.bi_bvec_done)
293 idx = iter.bi_idx - 1;
294 else /* in the middle of bvec */
295 idx = iter.bi_idx;
296
297 *bv = bio->bi_io_vec[idx];
298
299 /*
300 * iter.bi_bvec_done records actual length of the last bvec
301 * if this bio ends in the middle of one io vector
302 */
303 if (iter.bi_bvec_done)
304 bv->bv_len = iter.bi_bvec_done;
305}
306
86292abc
ML
307static inline unsigned bio_pages_all(struct bio *bio)
308{
309 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
310 return bio->bi_vcnt;
311}
312
313static inline struct bio_vec *bio_first_bvec_all(struct bio *bio)
314{
315 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
316 return bio->bi_io_vec;
317}
318
319static inline struct page *bio_first_page_all(struct bio *bio)
320{
321 return bio_first_bvec_all(bio)->bv_page;
322}
323
324static inline struct bio_vec *bio_last_bvec_all(struct bio *bio)
325{
326 WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
327 return &bio->bi_io_vec[bio->bi_vcnt - 1];
328}
329
c611529e
MP
330enum bip_flags {
331 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
332 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
333 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
334 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
335 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
336};
337
7ba1ba12
MP
338/*
339 * bio integrity payload
340 */
341struct bio_integrity_payload {
342 struct bio *bip_bio; /* parent bio */
7ba1ba12 343
d57a5f7c 344 struct bvec_iter bip_iter;
7ba1ba12 345
7878cba9 346 unsigned short bip_slab; /* slab the bip came from */
7ba1ba12 347 unsigned short bip_vcnt; /* # of integrity bio_vecs */
cbcd1054 348 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
b1f01388 349 unsigned short bip_flags; /* control flags */
7ba1ba12
MP
350
351 struct work_struct bip_work; /* I/O completion */
6fda981c
KO
352
353 struct bio_vec *bip_vec;
354 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
7ba1ba12 355};
18593088 356
06c1e390
KB
357#if defined(CONFIG_BLK_DEV_INTEGRITY)
358
359static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
360{
1eff9d32 361 if (bio->bi_opf & REQ_INTEGRITY)
06c1e390
KB
362 return bio->bi_integrity;
363
364 return NULL;
365}
366
c611529e
MP
367static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
368{
369 struct bio_integrity_payload *bip = bio_integrity(bio);
370
371 if (bip)
372 return bip->bip_flags & flag;
373
374 return false;
375}
b1f01388 376
18593088
MP
377static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
378{
379 return bip->bip_iter.bi_sector;
380}
381
382static inline void bip_set_seed(struct bio_integrity_payload *bip,
383 sector_t seed)
384{
385 bip->bip_iter.bi_sector = seed;
386}
387
7ba1ba12 388#endif /* CONFIG_BLK_DEV_INTEGRITY */
1da177e4 389
6678d83f 390extern void bio_trim(struct bio *bio, int offset, int size);
20d0189b
KO
391extern struct bio *bio_split(struct bio *bio, int sectors,
392 gfp_t gfp, struct bio_set *bs);
393
394/**
395 * bio_next_split - get next @sectors from a bio, splitting if necessary
396 * @bio: bio to split
397 * @sectors: number of sectors to split from the front of @bio
398 * @gfp: gfp mask
399 * @bs: bio set to allocate from
400 *
401 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
402 * than @sectors, returns the original bio unchanged.
403 */
404static inline struct bio *bio_next_split(struct bio *bio, int sectors,
405 gfp_t gfp, struct bio_set *bs)
406{
407 if (sectors >= bio_sectors(bio))
408 return bio;
409
410 return bio_split(bio, sectors, gfp, bs);
411}
412
011067b0
N
413enum {
414 BIOSET_NEED_BVECS = BIT(0),
47e0fb46 415 BIOSET_NEED_RESCUER = BIT(1),
011067b0 416};
dad08527
KO
417extern int bioset_init(struct bio_set *, unsigned int, unsigned int, int flags);
418extern void bioset_exit(struct bio_set *);
8aa6ba2f 419extern int biovec_init_pool(mempool_t *pool, int pool_entries);
1da177e4 420
7a88fa19 421extern struct bio *bio_alloc_bioset(gfp_t, unsigned int, struct bio_set *);
1da177e4
LT
422extern void bio_put(struct bio *);
423
59d276fe
KO
424extern void __bio_clone_fast(struct bio *, struct bio *);
425extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
bf800ef1
KO
426extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
427
f4f8154a 428extern struct bio_set fs_bio_set;
3f86a82a
KO
429
430static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
431{
f4f8154a 432 return bio_alloc_bioset(gfp_mask, nr_iovecs, &fs_bio_set);
3f86a82a
KO
433}
434
435static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
436{
437 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
438}
439
bf800ef1
KO
440static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
441{
442 return bio_clone_bioset(bio, gfp_mask, NULL);
443
444}
445
1e3914d4
CH
446extern blk_qc_t submit_bio(struct bio *);
447
4246a0b6
CH
448extern void bio_endio(struct bio *);
449
450static inline void bio_io_error(struct bio *bio)
451{
4e4cbee9 452 bio->bi_status = BLK_STS_IOERR;
4246a0b6
CH
453 bio_endio(bio);
454}
455
03a07c92
GR
456static inline void bio_wouldblock_error(struct bio *bio)
457{
458 bio->bi_status = BLK_STS_AGAIN;
4246a0b6
CH
459 bio_endio(bio);
460}
461
1da177e4
LT
462struct request_queue;
463extern int bio_phys_segments(struct request_queue *, struct bio *);
1da177e4 464
4e49ea4a 465extern int submit_bio_wait(struct bio *bio);
054bdf64
KO
466extern void bio_advance(struct bio *, unsigned);
467
3a83f467
ML
468extern void bio_init(struct bio *bio, struct bio_vec *table,
469 unsigned short max_vecs);
9ae3b3f5 470extern void bio_uninit(struct bio *);
f44b48c7 471extern void bio_reset(struct bio *);
196d38bc 472void bio_chain(struct bio *, struct bio *);
1da177e4
LT
473
474extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
6e68af66
MC
475extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
476 unsigned int, unsigned int);
2cefe4db 477int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter);
152e283f 478struct rq_map_data;
f1970baf 479extern struct bio *bio_map_user_iov(struct request_queue *,
e81cef5d 480 struct iov_iter *, gfp_t);
1da177e4 481extern void bio_unmap_user(struct bio *);
df46b9a4 482extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
27496a8c 483 gfp_t);
68154e90
FT
484extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
485 gfp_t, int);
1da177e4
LT
486extern void bio_set_pages_dirty(struct bio *bio);
487extern void bio_check_pages_dirty(struct bio *bio);
2d4dc890 488
d62e26b3
JA
489void generic_start_io_acct(struct request_queue *q, int rw,
490 unsigned long sectors, struct hd_struct *part);
491void generic_end_io_acct(struct request_queue *q, int rw,
492 struct hd_struct *part,
493 unsigned long start_time);
394ffa50 494
2d4dc890
IL
495#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
496# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
497#endif
498#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
499extern void bio_flush_dcache_pages(struct bio *bi);
500#else
501static inline void bio_flush_dcache_pages(struct bio *bi)
502{
503}
504#endif
505
45db54d5
KO
506extern void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
507 struct bio *src, struct bvec_iter *src_iter);
16ac3d63 508extern void bio_copy_data(struct bio *dst, struct bio *src);
45db54d5 509extern void bio_list_copy_data(struct bio *dst, struct bio *src);
491221f8 510extern void bio_free_pages(struct bio *bio);
16ac3d63 511
152e283f 512extern struct bio *bio_copy_user_iov(struct request_queue *,
86d564c8 513 struct rq_map_data *,
e81cef5d 514 struct iov_iter *,
26e49cfc 515 gfp_t);
1da177e4 516extern int bio_uncopy_user(struct bio *);
38a72dac
KO
517void zero_fill_bio_iter(struct bio *bio, struct bvec_iter iter);
518
519static inline void zero_fill_bio(struct bio *bio)
520{
521 zero_fill_bio_iter(bio, bio->bi_iter);
522}
523
9f060e22
KO
524extern struct bio_vec *bvec_alloc(gfp_t, int, unsigned long *, mempool_t *);
525extern void bvec_free(mempool_t *, struct bio_vec *, unsigned int);
7ba1ba12 526extern unsigned int bvec_nr_vecs(unsigned short idx);
9c0fb1e3 527extern const char *bio_devname(struct bio *bio, char *buffer);
51d654e1 528
74d46992
CH
529#define bio_set_dev(bio, bdev) \
530do { \
111be883
SL
531 if ((bio)->bi_disk != (bdev)->bd_disk) \
532 bio_clear_flag(bio, BIO_THROTTLED);\
74d46992
CH
533 (bio)->bi_disk = (bdev)->bd_disk; \
534 (bio)->bi_partno = (bdev)->bd_partno; \
535} while (0)
536
537#define bio_copy_dev(dst, src) \
538do { \
539 (dst)->bi_disk = (src)->bi_disk; \
540 (dst)->bi_partno = (src)->bi_partno; \
541} while (0)
542
543#define bio_dev(bio) \
544 disk_devt((bio)->bi_disk)
545
852c788f 546#ifdef CONFIG_BLK_CGROUP
1d933cf0 547int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
852c788f 548void bio_disassociate_task(struct bio *bio);
20bd723e 549void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
852c788f 550#else /* CONFIG_BLK_CGROUP */
1d933cf0
TH
551static inline int bio_associate_blkcg(struct bio *bio,
552 struct cgroup_subsys_state *blkcg_css) { return 0; }
852c788f 553static inline void bio_disassociate_task(struct bio *bio) { }
20bd723e
PV
554static inline void bio_clone_blkcg_association(struct bio *dst,
555 struct bio *src) { }
852c788f
TH
556#endif /* CONFIG_BLK_CGROUP */
557
1da177e4
LT
558#ifdef CONFIG_HIGHMEM
559/*
20b636bf
AB
560 * remember never ever reenable interrupts between a bvec_kmap_irq and
561 * bvec_kunmap_irq!
1da177e4 562 */
4f570f99 563static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
1da177e4
LT
564{
565 unsigned long addr;
566
567 /*
568 * might not be a highmem page, but the preempt/irq count
569 * balancing is a lot nicer this way
570 */
571 local_irq_save(*flags);
e8e3c3d6 572 addr = (unsigned long) kmap_atomic(bvec->bv_page);
1da177e4
LT
573
574 BUG_ON(addr & ~PAGE_MASK);
575
576 return (char *) addr + bvec->bv_offset;
577}
578
4f570f99 579static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
1da177e4
LT
580{
581 unsigned long ptr = (unsigned long) buffer & PAGE_MASK;
582
e8e3c3d6 583 kunmap_atomic((void *) ptr);
1da177e4
LT
584 local_irq_restore(*flags);
585}
586
587#else
11a691be
GU
588static inline char *bvec_kmap_irq(struct bio_vec *bvec, unsigned long *flags)
589{
590 return page_address(bvec->bv_page) + bvec->bv_offset;
591}
592
593static inline void bvec_kunmap_irq(char *buffer, unsigned long *flags)
594{
595 *flags = 0;
596}
1da177e4
LT
597#endif
598
8f3d8ba2 599/*
e686307f 600 * BIO list management for use by remapping drivers (e.g. DM or MD) and loop.
8f3d8ba2
CH
601 *
602 * A bio_list anchors a singly-linked list of bios chained through the bi_next
603 * member of the bio. The bio_list also caches the last list member to allow
604 * fast access to the tail.
605 */
606struct bio_list {
607 struct bio *head;
608 struct bio *tail;
609};
610
611static inline int bio_list_empty(const struct bio_list *bl)
612{
613 return bl->head == NULL;
614}
615
616static inline void bio_list_init(struct bio_list *bl)
617{
618 bl->head = bl->tail = NULL;
619}
620
320ae51f
JA
621#define BIO_EMPTY_LIST { NULL, NULL }
622
8f3d8ba2
CH
623#define bio_list_for_each(bio, bl) \
624 for (bio = (bl)->head; bio; bio = bio->bi_next)
625
626static inline unsigned bio_list_size(const struct bio_list *bl)
627{
628 unsigned sz = 0;
629 struct bio *bio;
630
631 bio_list_for_each(bio, bl)
632 sz++;
633
634 return sz;
635}
636
637static inline void bio_list_add(struct bio_list *bl, struct bio *bio)
638{
639 bio->bi_next = NULL;
640
641 if (bl->tail)
642 bl->tail->bi_next = bio;
643 else
644 bl->head = bio;
645
646 bl->tail = bio;
647}
648
649static inline void bio_list_add_head(struct bio_list *bl, struct bio *bio)
650{
651 bio->bi_next = bl->head;
652
653 bl->head = bio;
654
655 if (!bl->tail)
656 bl->tail = bio;
657}
658
659static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2)
660{
661 if (!bl2->head)
662 return;
663
664 if (bl->tail)
665 bl->tail->bi_next = bl2->head;
666 else
667 bl->head = bl2->head;
668
669 bl->tail = bl2->tail;
670}
671
672static inline void bio_list_merge_head(struct bio_list *bl,
673 struct bio_list *bl2)
674{
675 if (!bl2->head)
676 return;
677
678 if (bl->head)
679 bl2->tail->bi_next = bl->head;
680 else
681 bl->tail = bl2->tail;
682
683 bl->head = bl2->head;
684}
685
13685a16
GU
686static inline struct bio *bio_list_peek(struct bio_list *bl)
687{
688 return bl->head;
689}
690
8f3d8ba2
CH
691static inline struct bio *bio_list_pop(struct bio_list *bl)
692{
693 struct bio *bio = bl->head;
694
695 if (bio) {
696 bl->head = bl->head->bi_next;
697 if (!bl->head)
698 bl->tail = NULL;
699
700 bio->bi_next = NULL;
701 }
702
703 return bio;
704}
705
706static inline struct bio *bio_list_get(struct bio_list *bl)
707{
708 struct bio *bio = bl->head;
709
710 bl->head = bl->tail = NULL;
711
712 return bio;
713}
714
0ef5a50c
MS
715/*
716 * Increment chain count for the bio. Make sure the CHAIN flag update
717 * is visible before the raised count.
718 */
719static inline void bio_inc_remaining(struct bio *bio)
720{
721 bio_set_flag(bio, BIO_CHAIN);
722 smp_mb__before_atomic();
723 atomic_inc(&bio->__bi_remaining);
724}
725
57fb233f
KO
726/*
727 * bio_set is used to allow other portions of the IO system to
728 * allocate their own private memory pools for bio and iovec structures.
729 * These memory pools in turn all allocate from the bio_slab
730 * and the bvec_slabs[].
731 */
732#define BIO_POOL_SIZE 2
57fb233f
KO
733
734struct bio_set {
735 struct kmem_cache *bio_slab;
736 unsigned int front_pad;
737
8aa6ba2f
KO
738 mempool_t bio_pool;
739 mempool_t bvec_pool;
57fb233f 740#if defined(CONFIG_BLK_DEV_INTEGRITY)
8aa6ba2f
KO
741 mempool_t bio_integrity_pool;
742 mempool_t bvec_integrity_pool;
57fb233f 743#endif
df2cb6da
KO
744
745 /*
746 * Deadlock avoidance for stacking block drivers: see comments in
747 * bio_alloc_bioset() for details
748 */
749 spinlock_t rescue_lock;
750 struct bio_list rescue_list;
751 struct work_struct rescue_work;
752 struct workqueue_struct *rescue_workqueue;
57fb233f
KO
753};
754
755struct biovec_slab {
756 int nr_vecs;
757 char *name;
758 struct kmem_cache *slab;
759};
760
338aa96d
KO
761static inline bool bioset_initialized(struct bio_set *bs)
762{
763 return bs->bio_slab != NULL;
764}
765
57fb233f
KO
766/*
767 * a small number of entries is fine, not going to be performance critical.
768 * basically we just need to survive
769 */
770#define BIO_SPLIT_ENTRIES 2
771
7ba1ba12
MP
772#if defined(CONFIG_BLK_DEV_INTEGRITY)
773
d57a5f7c
KO
774#define bip_for_each_vec(bvl, bip, iter) \
775 for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
7ba1ba12 776
13f05c8d
MP
777#define bio_for_each_integrity_vec(_bvl, _bio, _iter) \
778 for_each_bio(_bio) \
779 bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
780
7ba1ba12 781extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int);
7ba1ba12 782extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int);
e23947bd 783extern bool bio_integrity_prep(struct bio *);
7ba1ba12 784extern void bio_integrity_advance(struct bio *, unsigned int);
fbd08e76 785extern void bio_integrity_trim(struct bio *);
1e2a410f 786extern int bio_integrity_clone(struct bio *, struct bio *, gfp_t);
7878cba9
MP
787extern int bioset_integrity_create(struct bio_set *, int);
788extern void bioset_integrity_free(struct bio_set *);
789extern void bio_integrity_init(void);
7ba1ba12
MP
790
791#else /* CONFIG_BLK_DEV_INTEGRITY */
792
c611529e 793static inline void *bio_integrity(struct bio *bio)
6898e3bd 794{
c611529e 795 return NULL;
6898e3bd
MP
796}
797
6898e3bd
MP
798static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
799{
800 return 0;
801}
802
803static inline void bioset_integrity_free (struct bio_set *bs)
804{
805 return;
806}
807
e23947bd 808static inline bool bio_integrity_prep(struct bio *bio)
6898e3bd 809{
e23947bd 810 return true;
6898e3bd
MP
811}
812
0c614e2d 813static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
1e2a410f 814 gfp_t gfp_mask)
0c614e2d
SR
815{
816 return 0;
817}
6898e3bd 818
6898e3bd
MP
819static inline void bio_integrity_advance(struct bio *bio,
820 unsigned int bytes_done)
821{
822 return;
823}
824
fbd08e76 825static inline void bio_integrity_trim(struct bio *bio)
6898e3bd
MP
826{
827 return;
828}
829
830static inline void bio_integrity_init(void)
831{
832 return;
833}
7ba1ba12 834
c611529e
MP
835static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
836{
837 return false;
838}
839
06c1e390
KB
840static inline void *bio_integrity_alloc(struct bio * bio, gfp_t gfp,
841 unsigned int nr)
842{
843 return ERR_PTR(-EINVAL);
844}
845
846static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
847 unsigned int len, unsigned int offset)
848{
849 return 0;
850}
851
7ba1ba12
MP
852#endif /* CONFIG_BLK_DEV_INTEGRITY */
853
02a5e0ac 854#endif /* CONFIG_BLOCK */
1da177e4 855#endif /* __LINUX_BIO_H */