Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Red Hat, Inc. |
| 3 | * |
| 4 | * This file is released under the GPL. |
| 5 | */ |
| 6 | |
| 7 | #include "dm.h" |
Joe Thornber | 742c8fd | 2016-10-21 10:06:40 -0400 | [diff] [blame] | 8 | #include "dm-bio-prison-v1.h" |
| 9 | #include "dm-bio-prison-v2.h" |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 10 | |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/mempool.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | |
| 16 | /*----------------------------------------------------------------*/ |
| 17 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 18 | #define MIN_CELLS 1024 |
Heinz Mauelshagen | adcc444 | 2014-06-05 15:23:09 +0100 | [diff] [blame] | 19 | |
| 20 | struct dm_bio_prison { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 21 | spinlock_t lock; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 22 | mempool_t *cell_pool; |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 23 | struct rb_root cells; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 24 | }; |
| 25 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 26 | static struct kmem_cache *_cell_cache; |
| 27 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 28 | /*----------------------------------------------------------------*/ |
Heinz Mauelshagen | adcc444 | 2014-06-05 15:23:09 +0100 | [diff] [blame] | 29 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 30 | /* |
| 31 | * @nr_cells should be the number of cells you want in use _concurrently_. |
| 32 | * Don't confuse it with the number of distinct keys. |
| 33 | */ |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 34 | struct dm_bio_prison *dm_bio_prison_create(void) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 35 | { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 36 | struct dm_bio_prison *prison = kmalloc(sizeof(*prison), GFP_KERNEL); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 37 | |
| 38 | if (!prison) |
| 39 | return NULL; |
| 40 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 41 | spin_lock_init(&prison->lock); |
| 42 | |
| 43 | prison->cell_pool = mempool_create_slab_pool(MIN_CELLS, _cell_cache); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 44 | if (!prison->cell_pool) { |
| 45 | kfree(prison); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 49 | prison->cells = RB_ROOT; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 50 | |
| 51 | return prison; |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(dm_bio_prison_create); |
| 54 | |
| 55 | void dm_bio_prison_destroy(struct dm_bio_prison *prison) |
| 56 | { |
| 57 | mempool_destroy(prison->cell_pool); |
| 58 | kfree(prison); |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(dm_bio_prison_destroy); |
| 61 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 62 | struct dm_bio_prison_cell *dm_bio_prison_alloc_cell(struct dm_bio_prison *prison, gfp_t gfp) |
| 63 | { |
| 64 | return mempool_alloc(prison->cell_pool, gfp); |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell); |
| 67 | |
| 68 | void dm_bio_prison_free_cell(struct dm_bio_prison *prison, |
| 69 | struct dm_bio_prison_cell *cell) |
| 70 | { |
| 71 | mempool_free(cell, prison->cell_pool); |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell); |
| 74 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 75 | static void __setup_new_cell(struct dm_cell_key *key, |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 76 | struct bio *holder, |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 77 | struct dm_bio_prison_cell *cell) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 78 | { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 79 | memcpy(&cell->key, key, sizeof(cell->key)); |
| 80 | cell->holder = holder; |
| 81 | bio_list_init(&cell->bios); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 82 | } |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 83 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 84 | static int cmp_keys(struct dm_cell_key *lhs, |
| 85 | struct dm_cell_key *rhs) |
| 86 | { |
| 87 | if (lhs->virtual < rhs->virtual) |
| 88 | return -1; |
| 89 | |
| 90 | if (lhs->virtual > rhs->virtual) |
| 91 | return 1; |
| 92 | |
| 93 | if (lhs->dev < rhs->dev) |
| 94 | return -1; |
| 95 | |
| 96 | if (lhs->dev > rhs->dev) |
| 97 | return 1; |
| 98 | |
Joe Thornber | 5f274d8 | 2014-09-17 10:17:39 +0100 | [diff] [blame] | 99 | if (lhs->block_end <= rhs->block_begin) |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 100 | return -1; |
| 101 | |
Joe Thornber | 5f274d8 | 2014-09-17 10:17:39 +0100 | [diff] [blame] | 102 | if (lhs->block_begin >= rhs->block_end) |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 103 | return 1; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int __bio_detain(struct dm_bio_prison *prison, |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 109 | struct dm_cell_key *key, |
| 110 | struct bio *inmate, |
| 111 | struct dm_bio_prison_cell *cell_prealloc, |
| 112 | struct dm_bio_prison_cell **cell_result) |
| 113 | { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 114 | int r; |
| 115 | struct rb_node **new = &prison->cells.rb_node, *parent = NULL; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 116 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 117 | while (*new) { |
| 118 | struct dm_bio_prison_cell *cell = |
| 119 | container_of(*new, struct dm_bio_prison_cell, node); |
| 120 | |
| 121 | r = cmp_keys(key, &cell->key); |
| 122 | |
| 123 | parent = *new; |
| 124 | if (r < 0) |
| 125 | new = &((*new)->rb_left); |
| 126 | else if (r > 0) |
| 127 | new = &((*new)->rb_right); |
| 128 | else { |
| 129 | if (inmate) |
| 130 | bio_list_add(&cell->bios, inmate); |
| 131 | *cell_result = cell; |
| 132 | return 1; |
| 133 | } |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 136 | __setup_new_cell(key, inmate, cell_prealloc); |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 137 | *cell_result = cell_prealloc; |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 138 | |
| 139 | rb_link_node(&cell_prealloc->node, parent, new); |
| 140 | rb_insert_color(&cell_prealloc->node, &prison->cells); |
| 141 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int bio_detain(struct dm_bio_prison *prison, |
| 146 | struct dm_cell_key *key, |
| 147 | struct bio *inmate, |
| 148 | struct dm_bio_prison_cell *cell_prealloc, |
| 149 | struct dm_bio_prison_cell **cell_result) |
| 150 | { |
| 151 | int r; |
| 152 | unsigned long flags; |
| 153 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 154 | spin_lock_irqsave(&prison->lock, flags); |
| 155 | r = __bio_detain(prison, key, inmate, cell_prealloc, cell_result); |
| 156 | spin_unlock_irqrestore(&prison->lock, flags); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 157 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 158 | return r; |
| 159 | } |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 160 | |
| 161 | int dm_bio_detain(struct dm_bio_prison *prison, |
| 162 | struct dm_cell_key *key, |
| 163 | struct bio *inmate, |
| 164 | struct dm_bio_prison_cell *cell_prealloc, |
| 165 | struct dm_bio_prison_cell **cell_result) |
| 166 | { |
| 167 | return bio_detain(prison, key, inmate, cell_prealloc, cell_result); |
| 168 | } |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(dm_bio_detain); |
| 170 | |
Joe Thornber | c6b4fcb | 2013-03-01 22:45:51 +0000 | [diff] [blame] | 171 | int dm_get_cell(struct dm_bio_prison *prison, |
| 172 | struct dm_cell_key *key, |
| 173 | struct dm_bio_prison_cell *cell_prealloc, |
| 174 | struct dm_bio_prison_cell **cell_result) |
| 175 | { |
| 176 | return bio_detain(prison, key, NULL, cell_prealloc, cell_result); |
| 177 | } |
| 178 | EXPORT_SYMBOL_GPL(dm_get_cell); |
| 179 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 180 | /* |
| 181 | * @inmates must have been initialised prior to this call |
| 182 | */ |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 183 | static void __cell_release(struct dm_bio_prison *prison, |
| 184 | struct dm_bio_prison_cell *cell, |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 185 | struct bio_list *inmates) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 186 | { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 187 | rb_erase(&cell->node, &prison->cells); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 188 | |
| 189 | if (inmates) { |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 190 | if (cell->holder) |
| 191 | bio_list_add(inmates, cell->holder); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 192 | bio_list_merge(inmates, &cell->bios); |
| 193 | } |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 196 | void dm_cell_release(struct dm_bio_prison *prison, |
| 197 | struct dm_bio_prison_cell *cell, |
| 198 | struct bio_list *bios) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 199 | { |
| 200 | unsigned long flags; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 201 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 202 | spin_lock_irqsave(&prison->lock, flags); |
| 203 | __cell_release(prison, cell, bios); |
| 204 | spin_unlock_irqrestore(&prison->lock, flags); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 205 | } |
| 206 | EXPORT_SYMBOL_GPL(dm_cell_release); |
| 207 | |
| 208 | /* |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 209 | * Sometimes we don't want the holder, just the additional bios. |
| 210 | */ |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 211 | static void __cell_release_no_holder(struct dm_bio_prison *prison, |
| 212 | struct dm_bio_prison_cell *cell, |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 213 | struct bio_list *inmates) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 214 | { |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 215 | rb_erase(&cell->node, &prison->cells); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 216 | bio_list_merge(inmates, &cell->bios); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 219 | void dm_cell_release_no_holder(struct dm_bio_prison *prison, |
| 220 | struct dm_bio_prison_cell *cell, |
| 221 | struct bio_list *inmates) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 222 | { |
| 223 | unsigned long flags; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 224 | |
Joe Thornber | a195db2 | 2014-10-06 16:30:06 -0400 | [diff] [blame] | 225 | spin_lock_irqsave(&prison->lock, flags); |
| 226 | __cell_release_no_holder(prison, cell, inmates); |
| 227 | spin_unlock_irqrestore(&prison->lock, flags); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 228 | } |
| 229 | EXPORT_SYMBOL_GPL(dm_cell_release_no_holder); |
| 230 | |
Joe Thornber | 6beca5e | 2013-03-01 22:45:50 +0000 | [diff] [blame] | 231 | void dm_cell_error(struct dm_bio_prison *prison, |
Mike Snitzer | af91805 | 2014-05-22 14:32:51 -0400 | [diff] [blame] | 232 | struct dm_bio_prison_cell *cell, int error) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 233 | { |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 234 | struct bio_list bios; |
| 235 | struct bio *bio; |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 236 | |
| 237 | bio_list_init(&bios); |
Heinz Mauelshagen | adcc444 | 2014-06-05 15:23:09 +0100 | [diff] [blame] | 238 | dm_cell_release(prison, cell, &bios); |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 239 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 240 | while ((bio = bio_list_pop(&bios))) { |
| 241 | bio->bi_error = error; |
| 242 | bio_endio(bio); |
| 243 | } |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 244 | } |
| 245 | EXPORT_SYMBOL_GPL(dm_cell_error); |
| 246 | |
Joe Thornber | 2d759a4 | 2014-10-10 15:27:16 +0100 | [diff] [blame] | 247 | void dm_cell_visit_release(struct dm_bio_prison *prison, |
| 248 | void (*visit_fn)(void *, struct dm_bio_prison_cell *), |
| 249 | void *context, |
| 250 | struct dm_bio_prison_cell *cell) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | |
| 254 | spin_lock_irqsave(&prison->lock, flags); |
| 255 | visit_fn(context, cell); |
| 256 | rb_erase(&cell->node, &prison->cells); |
| 257 | spin_unlock_irqrestore(&prison->lock, flags); |
| 258 | } |
| 259 | EXPORT_SYMBOL_GPL(dm_cell_visit_release); |
| 260 | |
Joe Thornber | 3cdf93f | 2015-05-15 15:23:35 +0100 | [diff] [blame] | 261 | static int __promote_or_release(struct dm_bio_prison *prison, |
| 262 | struct dm_bio_prison_cell *cell) |
| 263 | { |
| 264 | if (bio_list_empty(&cell->bios)) { |
| 265 | rb_erase(&cell->node, &prison->cells); |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | cell->holder = bio_list_pop(&cell->bios); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int dm_cell_promote_or_release(struct dm_bio_prison *prison, |
| 274 | struct dm_bio_prison_cell *cell) |
| 275 | { |
| 276 | int r; |
| 277 | unsigned long flags; |
| 278 | |
| 279 | spin_lock_irqsave(&prison->lock, flags); |
| 280 | r = __promote_or_release(prison, cell); |
| 281 | spin_unlock_irqrestore(&prison->lock, flags); |
| 282 | |
| 283 | return r; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(dm_cell_promote_or_release); |
| 286 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 287 | /*----------------------------------------------------------------*/ |
| 288 | |
| 289 | #define DEFERRED_SET_SIZE 64 |
| 290 | |
| 291 | struct dm_deferred_entry { |
| 292 | struct dm_deferred_set *ds; |
| 293 | unsigned count; |
| 294 | struct list_head work_items; |
| 295 | }; |
| 296 | |
| 297 | struct dm_deferred_set { |
| 298 | spinlock_t lock; |
| 299 | unsigned current_entry; |
| 300 | unsigned sweeper; |
| 301 | struct dm_deferred_entry entries[DEFERRED_SET_SIZE]; |
| 302 | }; |
| 303 | |
| 304 | struct dm_deferred_set *dm_deferred_set_create(void) |
| 305 | { |
| 306 | int i; |
| 307 | struct dm_deferred_set *ds; |
| 308 | |
| 309 | ds = kmalloc(sizeof(*ds), GFP_KERNEL); |
| 310 | if (!ds) |
| 311 | return NULL; |
| 312 | |
| 313 | spin_lock_init(&ds->lock); |
| 314 | ds->current_entry = 0; |
| 315 | ds->sweeper = 0; |
| 316 | for (i = 0; i < DEFERRED_SET_SIZE; i++) { |
| 317 | ds->entries[i].ds = ds; |
| 318 | ds->entries[i].count = 0; |
| 319 | INIT_LIST_HEAD(&ds->entries[i].work_items); |
| 320 | } |
| 321 | |
| 322 | return ds; |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(dm_deferred_set_create); |
| 325 | |
| 326 | void dm_deferred_set_destroy(struct dm_deferred_set *ds) |
| 327 | { |
| 328 | kfree(ds); |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(dm_deferred_set_destroy); |
| 331 | |
| 332 | struct dm_deferred_entry *dm_deferred_entry_inc(struct dm_deferred_set *ds) |
| 333 | { |
| 334 | unsigned long flags; |
| 335 | struct dm_deferred_entry *entry; |
| 336 | |
| 337 | spin_lock_irqsave(&ds->lock, flags); |
| 338 | entry = ds->entries + ds->current_entry; |
| 339 | entry->count++; |
| 340 | spin_unlock_irqrestore(&ds->lock, flags); |
| 341 | |
| 342 | return entry; |
| 343 | } |
| 344 | EXPORT_SYMBOL_GPL(dm_deferred_entry_inc); |
| 345 | |
| 346 | static unsigned ds_next(unsigned index) |
| 347 | { |
| 348 | return (index + 1) % DEFERRED_SET_SIZE; |
| 349 | } |
| 350 | |
| 351 | static void __sweep(struct dm_deferred_set *ds, struct list_head *head) |
| 352 | { |
| 353 | while ((ds->sweeper != ds->current_entry) && |
| 354 | !ds->entries[ds->sweeper].count) { |
| 355 | list_splice_init(&ds->entries[ds->sweeper].work_items, head); |
| 356 | ds->sweeper = ds_next(ds->sweeper); |
| 357 | } |
| 358 | |
| 359 | if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count) |
| 360 | list_splice_init(&ds->entries[ds->sweeper].work_items, head); |
| 361 | } |
| 362 | |
| 363 | void dm_deferred_entry_dec(struct dm_deferred_entry *entry, struct list_head *head) |
| 364 | { |
| 365 | unsigned long flags; |
| 366 | |
| 367 | spin_lock_irqsave(&entry->ds->lock, flags); |
| 368 | BUG_ON(!entry->count); |
| 369 | --entry->count; |
| 370 | __sweep(entry->ds, head); |
| 371 | spin_unlock_irqrestore(&entry->ds->lock, flags); |
| 372 | } |
| 373 | EXPORT_SYMBOL_GPL(dm_deferred_entry_dec); |
| 374 | |
| 375 | /* |
| 376 | * Returns 1 if deferred or 0 if no pending items to delay job. |
| 377 | */ |
| 378 | int dm_deferred_set_add_work(struct dm_deferred_set *ds, struct list_head *work) |
| 379 | { |
| 380 | int r = 1; |
| 381 | unsigned long flags; |
| 382 | unsigned next_entry; |
| 383 | |
| 384 | spin_lock_irqsave(&ds->lock, flags); |
| 385 | if ((ds->sweeper == ds->current_entry) && |
| 386 | !ds->entries[ds->current_entry].count) |
| 387 | r = 0; |
| 388 | else { |
| 389 | list_add(work, &ds->entries[ds->current_entry].work_items); |
| 390 | next_entry = ds_next(ds->current_entry); |
| 391 | if (!ds->entries[next_entry].count) |
| 392 | ds->current_entry = next_entry; |
| 393 | } |
| 394 | spin_unlock_irqrestore(&ds->lock, flags); |
| 395 | |
| 396 | return r; |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(dm_deferred_set_add_work); |
| 399 | |
| 400 | /*----------------------------------------------------------------*/ |
| 401 | |
Joe Thornber | 742c8fd | 2016-10-21 10:06:40 -0400 | [diff] [blame] | 402 | static int __init dm_bio_prison_init_v1(void) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 403 | { |
| 404 | _cell_cache = KMEM_CACHE(dm_bio_prison_cell, 0); |
| 405 | if (!_cell_cache) |
| 406 | return -ENOMEM; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Joe Thornber | 742c8fd | 2016-10-21 10:06:40 -0400 | [diff] [blame] | 411 | static void dm_bio_prison_exit_v1(void) |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 412 | { |
| 413 | kmem_cache_destroy(_cell_cache); |
| 414 | _cell_cache = NULL; |
| 415 | } |
| 416 | |
Joe Thornber | 742c8fd | 2016-10-21 10:06:40 -0400 | [diff] [blame] | 417 | static int (*_inits[])(void) __initdata = { |
| 418 | dm_bio_prison_init_v1, |
| 419 | dm_bio_prison_init_v2, |
| 420 | }; |
| 421 | |
| 422 | static void (*_exits[])(void) = { |
| 423 | dm_bio_prison_exit_v1, |
| 424 | dm_bio_prison_exit_v2, |
| 425 | }; |
| 426 | |
| 427 | static int __init dm_bio_prison_init(void) |
| 428 | { |
| 429 | const int count = ARRAY_SIZE(_inits); |
| 430 | |
| 431 | int r, i; |
| 432 | |
| 433 | for (i = 0; i < count; i++) { |
| 434 | r = _inits[i](); |
| 435 | if (r) |
| 436 | goto bad; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | |
| 441 | bad: |
| 442 | while (i--) |
| 443 | _exits[i](); |
| 444 | |
| 445 | return r; |
| 446 | } |
| 447 | |
| 448 | static void __exit dm_bio_prison_exit(void) |
| 449 | { |
| 450 | int i = ARRAY_SIZE(_exits); |
| 451 | |
| 452 | while (i--) |
| 453 | _exits[i](); |
| 454 | } |
| 455 | |
Mike Snitzer | 4f81a41 | 2012-10-12 21:02:13 +0100 | [diff] [blame] | 456 | /* |
| 457 | * module hooks |
| 458 | */ |
| 459 | module_init(dm_bio_prison_init); |
| 460 | module_exit(dm_bio_prison_exit); |
| 461 | |
| 462 | MODULE_DESCRIPTION(DM_NAME " bio prison"); |
| 463 | MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>"); |
| 464 | MODULE_LICENSE("GPL"); |