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