lightnvm: propagate device_add() error code
[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
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
458b76ed
KO
65/*
66 * Check whether this bio carries any data or not. A NULL bio is allowed.
67 */
68static inline bool bio_has_data(struct bio *bio)
69{
70 if (bio &&
71 bio->bi_iter.bi_size &&
7afafc8a
AH
72 bio_op(bio) != REQ_OP_DISCARD &&
73 bio_op(bio) != REQ_OP_SECURE_ERASE)
458b76ed
KO
74 return true;
75
76 return false;
77}
78
95fe6c1a
MC
79static inline bool bio_no_advance_iter(struct bio *bio)
80{
7afafc8a
AH
81 return bio_op(bio) == REQ_OP_DISCARD ||
82 bio_op(bio) == REQ_OP_SECURE_ERASE ||
83 bio_op(bio) == REQ_OP_WRITE_SAME;
95fe6c1a
MC
84}
85
458b76ed
KO
86static inline bool bio_is_rw(struct bio *bio)
87{
88 if (!bio_has_data(bio))
89 return false;
90
95fe6c1a 91 if (bio_no_advance_iter(bio))
458b76ed
KO
92 return false;
93
94 return true;
95}
96
97static inline bool bio_mergeable(struct bio *bio)
98{
1eff9d32 99 if (bio->bi_opf & REQ_NOMERGE_FLAGS)
458b76ed
KO
100 return false;
101
102 return true;
103}
104
2e46e8b2 105static inline unsigned int bio_cur_bytes(struct bio *bio)
bf2de6f5 106{
458b76ed 107 if (bio_has_data(bio))
a4ad39b1 108 return bio_iovec(bio).bv_len;
fb2dce86 109 else /* dataless requests such as discard */
4f024f37 110 return bio->bi_iter.bi_size;
bf2de6f5
JA
111}
112
113static inline void *bio_data(struct bio *bio)
114{
458b76ed 115 if (bio_has_data(bio))
bf2de6f5
JA
116 return page_address(bio_page(bio)) + bio_offset(bio);
117
118 return NULL;
119}
1da177e4
LT
120
121/*
122 * will die
123 */
124#define bio_to_phys(bio) (page_to_phys(bio_page((bio))) + (unsigned long) bio_offset((bio)))
125#define bvec_to_phys(bv) (page_to_phys((bv)->bv_page) + (unsigned long) (bv)->bv_offset)
126
127/*
128 * queues that have highmem support enabled may still need to revert to
129 * PIO transfers occasionally and thus map high pages temporarily. For
130 * permanent PIO fall back, user is probably better off disabling highmem
131 * I/O completely on that queue (see ide-dma for example)
132 */
f619d254
KO
133#define __bio_kmap_atomic(bio, iter) \
134 (kmap_atomic(bio_iter_iovec((bio), (iter)).bv_page) + \
135 bio_iter_iovec((bio), (iter)).bv_offset)
1da177e4 136
f619d254 137#define __bio_kunmap_atomic(addr) kunmap_atomic(addr)
1da177e4
LT
138
139/*
140 * merge helpers etc
141 */
142
f92131c3
JF
143/* Default implementation of BIOVEC_PHYS_MERGEABLE */
144#define __BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
145 ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
146
1da177e4
LT
147/*
148 * allow arch override, for eg virtualized architectures (put in asm/io.h)
149 */
150#ifndef BIOVEC_PHYS_MERGEABLE
151#define BIOVEC_PHYS_MERGEABLE(vec1, vec2) \
f92131c3 152 __BIOVEC_PHYS_MERGEABLE(vec1, vec2)
1da177e4
LT
153#endif
154
1da177e4
LT
155#define __BIO_SEG_BOUNDARY(addr1, addr2, mask) \
156 (((addr1) | (mask)) == (((addr2) - 1) | (mask)))
157#define BIOVEC_SEG_BOUNDARY(q, b1, b2) \
ae03bf63 158 __BIO_SEG_BOUNDARY(bvec_to_phys((b1)), bvec_to_phys((b2)) + (b2)->bv_len, queue_segment_boundary((q)))
1da177e4 159
d74c6d51
KO
160/*
161 * drivers should _never_ use the all version - the bio may have been split
162 * before it got to the driver and the driver won't own all of it
163 */
164#define bio_for_each_segment_all(bvl, bio, i) \
f619d254 165 for (i = 0, bvl = (bio)->bi_io_vec; i < (bio)->bi_vcnt; i++, bvl++)
d74c6d51 166
4550dd6c
KO
167static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,
168 unsigned bytes)
169{
170 iter->bi_sector += bytes >> 9;
171
95fe6c1a 172 if (bio_no_advance_iter(bio))
4550dd6c
KO
173 iter->bi_size -= bytes;
174 else
175 bvec_iter_advance(bio->bi_io_vec, iter, bytes);
176}
177
7988613b
KO
178#define __bio_for_each_segment(bvl, bio, iter, start) \
179 for (iter = (start); \
4550dd6c
KO
180 (iter).bi_size && \
181 ((bvl = bio_iter_iovec((bio), (iter))), 1); \
182 bio_advance_iter((bio), &(iter), (bvl).bv_len))
7988613b
KO
183
184#define bio_for_each_segment(bvl, bio, iter) \
185 __bio_for_each_segment(bvl, bio, iter, (bio)->bi_iter)
186
4550dd6c 187#define bio_iter_last(bvec, iter) ((iter).bi_size == (bvec).bv_len)
1da177e4 188
458b76ed
KO
189static inline unsigned bio_segments(struct bio *bio)
190{
191 unsigned segs = 0;
192 struct bio_vec bv;
193 struct bvec_iter iter;
194
8423ae3d
KO
195 /*
196 * We special case discard/write same, because they interpret bi_size
197 * differently:
198 */
199
95fe6c1a 200 if (bio_op(bio) == REQ_OP_DISCARD)
8423ae3d
KO
201 return 1;
202
7afafc8a
AH
203 if (bio_op(bio) == REQ_OP_SECURE_ERASE)
204 return 1;
205
95fe6c1a 206 if (bio_op(bio) == REQ_OP_WRITE_SAME)
8423ae3d
KO
207 return 1;
208
458b76ed
KO
209 bio_for_each_segment(bv, bio, iter)
210 segs++;
211
212 return segs;
213}
214
1da177e4
LT
215/*
216 * get a reference to a bio, so it won't disappear. the intended use is
217 * something like:
218 *
219 * bio_get(bio);
220 * submit_bio(rw, bio);
221 * if (bio->bi_flags ...)
222 * do_something
223 * bio_put(bio);
224 *
225 * without the bio_get(), it could potentially complete I/O before submit_bio
226 * returns. and then bio would be freed memory when if (bio->bi_flags ...)
227 * runs
228 */
dac56212
JA
229static inline void bio_get(struct bio *bio)
230{
231 bio->bi_flags |= (1 << BIO_REFFED);
232 smp_mb__before_atomic();
233 atomic_inc(&bio->__bi_cnt);
234}
235
236static inline void bio_cnt_set(struct bio *bio, unsigned int count)
237{
238 if (count != 1) {
239 bio->bi_flags |= (1 << BIO_REFFED);
240 smp_mb__before_atomic();
241 }
242 atomic_set(&bio->__bi_cnt, count);
243}
1da177e4 244
b7c44ed9
JA
245static inline bool bio_flagged(struct bio *bio, unsigned int bit)
246{
2c68f6dc 247 return (bio->bi_flags & (1U << bit)) != 0;
b7c44ed9
JA
248}
249
250static inline void bio_set_flag(struct bio *bio, unsigned int bit)
251{
2c68f6dc 252 bio->bi_flags |= (1U << bit);
b7c44ed9
JA
253}
254
255static inline void bio_clear_flag(struct bio *bio, unsigned int bit)
256{
2c68f6dc 257 bio->bi_flags &= ~(1U << bit);
b7c44ed9
JA
258}
259
7bcd79ac
ML
260static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv)
261{
262 *bv = bio_iovec(bio);
263}
264
265static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv)
266{
267 struct bvec_iter iter = bio->bi_iter;
268 int idx;
269
7bcd79ac
ML
270 if (unlikely(!bio_multiple_segments(bio))) {
271 *bv = bio_iovec(bio);
272 return;
273 }
274
275 bio_advance_iter(bio, &iter, iter.bi_size);
276
277 if (!iter.bi_bvec_done)
278 idx = iter.bi_idx - 1;
279 else /* in the middle of bvec */
280 idx = iter.bi_idx;
281
282 *bv = bio->bi_io_vec[idx];
283
284 /*
285 * iter.bi_bvec_done records actual length of the last bvec
286 * if this bio ends in the middle of one io vector
287 */
288 if (iter.bi_bvec_done)
289 bv->bv_len = iter.bi_bvec_done;
290}
291
c611529e
MP
292enum bip_flags {
293 BIP_BLOCK_INTEGRITY = 1 << 0, /* block layer owns integrity data */
294 BIP_MAPPED_INTEGRITY = 1 << 1, /* ref tag has been remapped */
295 BIP_CTRL_NOCHECK = 1 << 2, /* disable HBA integrity checking */
296 BIP_DISK_NOCHECK = 1 << 3, /* disable disk integrity checking */
297 BIP_IP_CHECKSUM = 1 << 4, /* IP checksum */
298};
299
7ba1ba12
MP
300/*
301 * bio integrity payload
302 */
303struct bio_integrity_payload {
304 struct bio *bip_bio; /* parent bio */
7ba1ba12 305
d57a5f7c 306 struct bvec_iter bip_iter;
7ba1ba12 307
d57a5f7c 308 bio_end_io_t *bip_end_io; /* saved I/O completion fn */
7ba1ba12 309
7878cba9 310 unsigned short bip_slab; /* slab the bip came from */
7ba1ba12 311 unsigned short bip_vcnt; /* # of integrity bio_vecs */
cbcd1054 312 unsigned short bip_max_vcnt; /* integrity bio_vec slots */
b1f01388 313 unsigned short bip_flags; /* control flags */
7ba1ba12
MP
314
315 struct work_struct bip_work; /* I/O completion */
6fda981c
KO
316
317 struct bio_vec *bip_vec;
318 struct bio_vec bip_inline_vecs[0];/* embedded bvec array */
7ba1ba12 319};
18593088 320
06c1e390
KB
321#if defined(CONFIG_BLK_DEV_INTEGRITY)
322
323static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
324{
1eff9d32 325 if (bio->bi_opf & REQ_INTEGRITY)
06c1e390
KB
326 return bio->bi_integrity;
327
328 return NULL;
329}
330
c611529e
MP
331static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
332{
333 struct bio_integrity_payload *bip = bio_integrity(bio);
334
335 if (bip)
336 return bip->bip_flags & flag;
337
338 return false;
339}
b1f01388 340
18593088
MP
341static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
342{
343 return bip->bip_iter.bi_sector;
344}
345
346static inline void bip_set_seed(struct bio_integrity_payload *bip,
347 sector_t seed)
348{
349 bip->bip_iter.bi_sector = seed;
350}
351
7ba1ba12 352#endif /* CONFIG_BLK_DEV_INTEGRITY */
1da177e4 353
6678d83f 354extern void bio_trim(struct bio *bio, int offset, int size);
20d0189b
KO
355extern struct bio *bio_split(struct bio *bio, int sectors,
356 gfp_t gfp, struct bio_set *bs);
357
358/**
359 * bio_next_split - get next @sectors from a bio, splitting if necessary
360 * @bio: bio to split
361 * @sectors: number of sectors to split from the front of @bio
362 * @gfp: gfp mask
363 * @bs: bio set to allocate from
364 *
365 * Returns a bio representing the next @sectors of @bio - if the bio is smaller
366 * than @sectors, returns the original bio unchanged.
367 */
368static inline struct bio *bio_next_split(struct bio *bio, int sectors,
369 gfp_t gfp, struct bio_set *bs)
370{
371 if (sectors >= bio_sectors(bio))
372 return bio;
373
374 return bio_split(bio, sectors, gfp, bs);
375}
376
bb799ca0 377extern struct bio_set *bioset_create(unsigned int, unsigned int);
d8f429e1 378extern struct bio_set *bioset_create_nobvec(unsigned int, unsigned int);
1da177e4 379extern void bioset_free(struct bio_set *);
a6c39cb4 380extern mempool_t *biovec_create_pool(int pool_entries);
1da177e4 381
dd0fc66f 382extern struct bio *bio_alloc_bioset(gfp_t, int, struct bio_set *);
1da177e4
LT
383extern void bio_put(struct bio *);
384
59d276fe
KO
385extern void __bio_clone_fast(struct bio *, struct bio *);
386extern struct bio *bio_clone_fast(struct bio *, gfp_t, struct bio_set *);
bf800ef1
KO
387extern struct bio *bio_clone_bioset(struct bio *, gfp_t, struct bio_set *bs);
388
3f86a82a
KO
389extern struct bio_set *fs_bio_set;
390
391static inline struct bio *bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
392{
393 return bio_alloc_bioset(gfp_mask, nr_iovecs, fs_bio_set);
394}
395
bf800ef1
KO
396static inline struct bio *bio_clone(struct bio *bio, gfp_t gfp_mask)
397{
398 return bio_clone_bioset(bio, gfp_mask, fs_bio_set);
399}
400
3f86a82a
KO
401static inline struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
402{
403 return bio_alloc_bioset(gfp_mask, nr_iovecs, NULL);
404}
405
bf800ef1
KO
406static inline struct bio *bio_clone_kmalloc(struct bio *bio, gfp_t gfp_mask)
407{
408 return bio_clone_bioset(bio, gfp_mask, NULL);
409
410}
411
4246a0b6
CH
412extern void bio_endio(struct bio *);
413
414static inline void bio_io_error(struct bio *bio)
415{
416 bio->bi_error = -EIO;
417 bio_endio(bio);
418}
419
1da177e4
LT
420struct request_queue;
421extern int bio_phys_segments(struct request_queue *, struct bio *);
1da177e4 422
4e49ea4a 423extern int submit_bio_wait(struct bio *bio);
054bdf64
KO
424extern void bio_advance(struct bio *, unsigned);
425
1da177e4 426extern void bio_init(struct bio *);
f44b48c7 427extern void bio_reset(struct bio *);
196d38bc 428void bio_chain(struct bio *, struct bio *);
1da177e4
LT
429
430extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
6e68af66
MC
431extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
432 unsigned int, unsigned int);
152e283f 433struct rq_map_data;
f1970baf 434extern struct bio *bio_map_user_iov(struct request_queue *,
26e49cfc 435 const struct iov_iter *, gfp_t);
1da177e4 436extern void bio_unmap_user(struct bio *);
df46b9a4 437extern struct bio *bio_map_kern(struct request_queue *, void *, unsigned int,
27496a8c 438 gfp_t);
68154e90
FT
439extern struct bio *bio_copy_kern(struct request_queue *, void *, unsigned int,
440 gfp_t, int);
1da177e4
LT
441extern void bio_set_pages_dirty(struct bio *bio);
442extern void bio_check_pages_dirty(struct bio *bio);
2d4dc890 443
394ffa50
GZ
444void generic_start_io_acct(int rw, unsigned long sectors,
445 struct hd_struct *part);
446void generic_end_io_acct(int rw, struct hd_struct *part,
447 unsigned long start_time);
448
2d4dc890
IL
449#ifndef ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
450# error "You should define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE for your platform"
451#endif
452#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
453extern void bio_flush_dcache_pages(struct bio *bi);
454#else
455static inline void bio_flush_dcache_pages(struct bio *bi)
456{
457}
458#endif
459
16ac3d63 460extern void bio_copy_data(struct bio *dst, struct bio *src);
a0787606 461extern int bio_alloc_pages(struct bio *bio, gfp_t gfp);
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 */