Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Red Hat UK Ltd. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #ifndef DM_BIO_LIST_H |
| 8 | #define DM_BIO_LIST_H |
| 9 | |
| 10 | #include <linux/bio.h> |
Heinz Mauelshagen | 0ba6993 | 2007-05-09 02:33:05 -0700 | [diff] [blame] | 11 | #include <linux/prefetch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct bio_list { |
| 14 | struct bio *head; |
| 15 | struct bio *tail; |
| 16 | }; |
| 17 | |
Heinz Mauelshagen | 0ba6993 | 2007-05-09 02:33:05 -0700 | [diff] [blame] | 18 | static inline int bio_list_empty(const struct bio_list *bl) |
| 19 | { |
| 20 | return bl->head == NULL; |
| 21 | } |
| 22 | |
| 23 | #define BIO_LIST_INIT { .head = NULL, .tail = NULL } |
| 24 | |
| 25 | #define BIO_LIST(bl) \ |
| 26 | struct bio_list bl = BIO_LIST_INIT |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | static inline void bio_list_init(struct bio_list *bl) |
| 29 | { |
| 30 | bl->head = bl->tail = NULL; |
| 31 | } |
| 32 | |
Heinz Mauelshagen | 0ba6993 | 2007-05-09 02:33:05 -0700 | [diff] [blame] | 33 | #define bio_list_for_each(bio, bl) \ |
| 34 | for (bio = (bl)->head; bio && ({ prefetch(bio->bi_next); 1; }); \ |
| 35 | bio = bio->bi_next) |
| 36 | |
| 37 | static inline unsigned bio_list_size(const struct bio_list *bl) |
| 38 | { |
| 39 | unsigned sz = 0; |
| 40 | struct bio *bio; |
| 41 | |
| 42 | bio_list_for_each(bio, bl) |
| 43 | sz++; |
| 44 | |
| 45 | return sz; |
| 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static inline void bio_list_add(struct bio_list *bl, struct bio *bio) |
| 49 | { |
| 50 | bio->bi_next = NULL; |
| 51 | |
| 52 | if (bl->tail) |
| 53 | bl->tail->bi_next = bio; |
| 54 | else |
| 55 | bl->head = bio; |
| 56 | |
| 57 | bl->tail = bio; |
| 58 | } |
| 59 | |
| 60 | static inline void bio_list_merge(struct bio_list *bl, struct bio_list *bl2) |
| 61 | { |
jblunck@suse.de | 233886dd | 2005-11-21 21:32:36 -0800 | [diff] [blame] | 62 | if (!bl2->head) |
| 63 | return; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (bl->tail) |
| 66 | bl->tail->bi_next = bl2->head; |
| 67 | else |
| 68 | bl->head = bl2->head; |
| 69 | |
| 70 | bl->tail = bl2->tail; |
| 71 | } |
| 72 | |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 73 | static inline void bio_list_merge_head(struct bio_list *bl, |
| 74 | struct bio_list *bl2) |
| 75 | { |
| 76 | if (!bl2->head) |
| 77 | return; |
| 78 | |
| 79 | if (bl->head) |
| 80 | bl2->tail->bi_next = bl->head; |
| 81 | else |
| 82 | bl->tail = bl2->tail; |
| 83 | |
| 84 | bl->head = bl2->head; |
| 85 | } |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static inline struct bio *bio_list_pop(struct bio_list *bl) |
| 88 | { |
| 89 | struct bio *bio = bl->head; |
| 90 | |
| 91 | if (bio) { |
| 92 | bl->head = bl->head->bi_next; |
| 93 | if (!bl->head) |
| 94 | bl->tail = NULL; |
| 95 | |
| 96 | bio->bi_next = NULL; |
| 97 | } |
| 98 | |
| 99 | return bio; |
| 100 | } |
| 101 | |
| 102 | static inline struct bio *bio_list_get(struct bio_list *bl) |
| 103 | { |
| 104 | struct bio *bio = bl->head; |
| 105 | |
| 106 | bl->head = bl->tail = NULL; |
| 107 | |
| 108 | return bio; |
| 109 | } |
| 110 | |
| 111 | #endif |