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