Linux 4.16.11
[linux/fpc-iii.git] / drivers / md / dm-bio-prison-v1.c
blob874841f0fc837a1c631771fc29c0c6130975b573
1 /*
2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
5 */
7 #include "dm.h"
8 #include "dm-bio-prison-v1.h"
9 #include "dm-bio-prison-v2.h"
11 #include <linux/spinlock.h>
12 #include <linux/mempool.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 /*----------------------------------------------------------------*/
18 #define MIN_CELLS 1024
20 struct dm_bio_prison {
21 spinlock_t lock;
22 mempool_t *cell_pool;
23 struct rb_root cells;
26 static struct kmem_cache *_cell_cache;
28 /*----------------------------------------------------------------*/
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.
34 struct dm_bio_prison *dm_bio_prison_create(void)
36 struct dm_bio_prison *prison = kmalloc(sizeof(*prison), GFP_KERNEL);
38 if (!prison)
39 return NULL;
41 spin_lock_init(&prison->lock);
43 prison->cell_pool = mempool_create_slab_pool(MIN_CELLS, _cell_cache);
44 if (!prison->cell_pool) {
45 kfree(prison);
46 return NULL;
49 prison->cells = RB_ROOT;
51 return prison;
53 EXPORT_SYMBOL_GPL(dm_bio_prison_create);
55 void dm_bio_prison_destroy(struct dm_bio_prison *prison)
57 mempool_destroy(prison->cell_pool);
58 kfree(prison);
60 EXPORT_SYMBOL_GPL(dm_bio_prison_destroy);
62 struct dm_bio_prison_cell *dm_bio_prison_alloc_cell(struct dm_bio_prison *prison, gfp_t gfp)
64 return mempool_alloc(prison->cell_pool, gfp);
66 EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell);
68 void dm_bio_prison_free_cell(struct dm_bio_prison *prison,
69 struct dm_bio_prison_cell *cell)
71 mempool_free(cell, prison->cell_pool);
73 EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell);
75 static void __setup_new_cell(struct dm_cell_key *key,
76 struct bio *holder,
77 struct dm_bio_prison_cell *cell)
79 memcpy(&cell->key, key, sizeof(cell->key));
80 cell->holder = holder;
81 bio_list_init(&cell->bios);
84 static int cmp_keys(struct dm_cell_key *lhs,
85 struct dm_cell_key *rhs)
87 if (lhs->virtual < rhs->virtual)
88 return -1;
90 if (lhs->virtual > rhs->virtual)
91 return 1;
93 if (lhs->dev < rhs->dev)
94 return -1;
96 if (lhs->dev > rhs->dev)
97 return 1;
99 if (lhs->block_end <= rhs->block_begin)
100 return -1;
102 if (lhs->block_begin >= rhs->block_end)
103 return 1;
105 return 0;
108 static int __bio_detain(struct dm_bio_prison *prison,
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)
114 int r;
115 struct rb_node **new = &prison->cells.rb_node, *parent = NULL;
117 while (*new) {
118 struct dm_bio_prison_cell *cell =
119 rb_entry(*new, struct dm_bio_prison_cell, node);
121 r = cmp_keys(key, &cell->key);
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;
136 __setup_new_cell(key, inmate, cell_prealloc);
137 *cell_result = cell_prealloc;
139 rb_link_node(&cell_prealloc->node, parent, new);
140 rb_insert_color(&cell_prealloc->node, &prison->cells);
142 return 0;
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)
151 int r;
152 unsigned long flags;
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);
158 return r;
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)
167 return bio_detain(prison, key, inmate, cell_prealloc, cell_result);
169 EXPORT_SYMBOL_GPL(dm_bio_detain);
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)
176 return bio_detain(prison, key, NULL, cell_prealloc, cell_result);
178 EXPORT_SYMBOL_GPL(dm_get_cell);
181 * @inmates must have been initialised prior to this call
183 static void __cell_release(struct dm_bio_prison *prison,
184 struct dm_bio_prison_cell *cell,
185 struct bio_list *inmates)
187 rb_erase(&cell->node, &prison->cells);
189 if (inmates) {
190 if (cell->holder)
191 bio_list_add(inmates, cell->holder);
192 bio_list_merge(inmates, &cell->bios);
196 void dm_cell_release(struct dm_bio_prison *prison,
197 struct dm_bio_prison_cell *cell,
198 struct bio_list *bios)
200 unsigned long flags;
202 spin_lock_irqsave(&prison->lock, flags);
203 __cell_release(prison, cell, bios);
204 spin_unlock_irqrestore(&prison->lock, flags);
206 EXPORT_SYMBOL_GPL(dm_cell_release);
209 * Sometimes we don't want the holder, just the additional bios.
211 static void __cell_release_no_holder(struct dm_bio_prison *prison,
212 struct dm_bio_prison_cell *cell,
213 struct bio_list *inmates)
215 rb_erase(&cell->node, &prison->cells);
216 bio_list_merge(inmates, &cell->bios);
219 void dm_cell_release_no_holder(struct dm_bio_prison *prison,
220 struct dm_bio_prison_cell *cell,
221 struct bio_list *inmates)
223 unsigned long flags;
225 spin_lock_irqsave(&prison->lock, flags);
226 __cell_release_no_holder(prison, cell, inmates);
227 spin_unlock_irqrestore(&prison->lock, flags);
229 EXPORT_SYMBOL_GPL(dm_cell_release_no_holder);
231 void dm_cell_error(struct dm_bio_prison *prison,
232 struct dm_bio_prison_cell *cell, blk_status_t error)
234 struct bio_list bios;
235 struct bio *bio;
237 bio_list_init(&bios);
238 dm_cell_release(prison, cell, &bios);
240 while ((bio = bio_list_pop(&bios))) {
241 bio->bi_status = error;
242 bio_endio(bio);
245 EXPORT_SYMBOL_GPL(dm_cell_error);
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)
252 unsigned long flags;
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);
259 EXPORT_SYMBOL_GPL(dm_cell_visit_release);
261 static int __promote_or_release(struct dm_bio_prison *prison,
262 struct dm_bio_prison_cell *cell)
264 if (bio_list_empty(&cell->bios)) {
265 rb_erase(&cell->node, &prison->cells);
266 return 1;
269 cell->holder = bio_list_pop(&cell->bios);
270 return 0;
273 int dm_cell_promote_or_release(struct dm_bio_prison *prison,
274 struct dm_bio_prison_cell *cell)
276 int r;
277 unsigned long flags;
279 spin_lock_irqsave(&prison->lock, flags);
280 r = __promote_or_release(prison, cell);
281 spin_unlock_irqrestore(&prison->lock, flags);
283 return r;
285 EXPORT_SYMBOL_GPL(dm_cell_promote_or_release);
287 /*----------------------------------------------------------------*/
289 #define DEFERRED_SET_SIZE 64
291 struct dm_deferred_entry {
292 struct dm_deferred_set *ds;
293 unsigned count;
294 struct list_head work_items;
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];
304 struct dm_deferred_set *dm_deferred_set_create(void)
306 int i;
307 struct dm_deferred_set *ds;
309 ds = kmalloc(sizeof(*ds), GFP_KERNEL);
310 if (!ds)
311 return NULL;
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);
322 return ds;
324 EXPORT_SYMBOL_GPL(dm_deferred_set_create);
326 void dm_deferred_set_destroy(struct dm_deferred_set *ds)
328 kfree(ds);
330 EXPORT_SYMBOL_GPL(dm_deferred_set_destroy);
332 struct dm_deferred_entry *dm_deferred_entry_inc(struct dm_deferred_set *ds)
334 unsigned long flags;
335 struct dm_deferred_entry *entry;
337 spin_lock_irqsave(&ds->lock, flags);
338 entry = ds->entries + ds->current_entry;
339 entry->count++;
340 spin_unlock_irqrestore(&ds->lock, flags);
342 return entry;
344 EXPORT_SYMBOL_GPL(dm_deferred_entry_inc);
346 static unsigned ds_next(unsigned index)
348 return (index + 1) % DEFERRED_SET_SIZE;
351 static void __sweep(struct dm_deferred_set *ds, struct list_head *head)
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);
359 if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count)
360 list_splice_init(&ds->entries[ds->sweeper].work_items, head);
363 void dm_deferred_entry_dec(struct dm_deferred_entry *entry, struct list_head *head)
365 unsigned long flags;
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);
373 EXPORT_SYMBOL_GPL(dm_deferred_entry_dec);
376 * Returns 1 if deferred or 0 if no pending items to delay job.
378 int dm_deferred_set_add_work(struct dm_deferred_set *ds, struct list_head *work)
380 int r = 1;
381 unsigned long flags;
382 unsigned next_entry;
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;
394 spin_unlock_irqrestore(&ds->lock, flags);
396 return r;
398 EXPORT_SYMBOL_GPL(dm_deferred_set_add_work);
400 /*----------------------------------------------------------------*/
402 static int __init dm_bio_prison_init_v1(void)
404 _cell_cache = KMEM_CACHE(dm_bio_prison_cell, 0);
405 if (!_cell_cache)
406 return -ENOMEM;
408 return 0;
411 static void dm_bio_prison_exit_v1(void)
413 kmem_cache_destroy(_cell_cache);
414 _cell_cache = NULL;
417 static int (*_inits[])(void) __initdata = {
418 dm_bio_prison_init_v1,
419 dm_bio_prison_init_v2,
422 static void (*_exits[])(void) = {
423 dm_bio_prison_exit_v1,
424 dm_bio_prison_exit_v2,
427 static int __init dm_bio_prison_init(void)
429 const int count = ARRAY_SIZE(_inits);
431 int r, i;
433 for (i = 0; i < count; i++) {
434 r = _inits[i]();
435 if (r)
436 goto bad;
439 return 0;
441 bad:
442 while (i--)
443 _exits[i]();
445 return r;
448 static void __exit dm_bio_prison_exit(void)
450 int i = ARRAY_SIZE(_exits);
452 while (i--)
453 _exits[i]();
457 * module hooks
459 module_init(dm_bio_prison_init);
460 module_exit(dm_bio_prison_exit);
462 MODULE_DESCRIPTION(DM_NAME " bio prison");
463 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
464 MODULE_LICENSE("GPL");