2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
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
{
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
);
41 spin_lock_init(&prison
->lock
);
43 prison
->cell_pool
= mempool_create_slab_pool(MIN_CELLS
, _cell_cache
);
44 if (!prison
->cell_pool
) {
49 prison
->cells
= RB_ROOT
;
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
);
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
,
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)
90 if (lhs
->virtual > rhs
->virtual)
93 if (lhs
->dev
< rhs
->dev
)
96 if (lhs
->dev
> rhs
->dev
)
99 if (lhs
->block_end
<= rhs
->block_begin
)
102 if (lhs
->block_begin
>= rhs
->block_end
)
108 static int __bio_detain(struct dm_bio_prison
*prison
,
109 struct dm_cell_key
*key
,
111 struct dm_bio_prison_cell
*cell_prealloc
,
112 struct dm_bio_prison_cell
**cell_result
)
115 struct rb_node
**new = &prison
->cells
.rb_node
, *parent
= NULL
;
118 struct dm_bio_prison_cell
*cell
=
119 rb_entry(*new, struct dm_bio_prison_cell
, node
);
121 r
= cmp_keys(key
, &cell
->key
);
125 new = &((*new)->rb_left
);
127 new = &((*new)->rb_right
);
130 bio_list_add(&cell
->bios
, inmate
);
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
);
145 static int bio_detain(struct dm_bio_prison
*prison
,
146 struct dm_cell_key
*key
,
148 struct dm_bio_prison_cell
*cell_prealloc
,
149 struct dm_bio_prison_cell
**cell_result
)
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
);
161 int dm_bio_detain(struct dm_bio_prison
*prison
,
162 struct dm_cell_key
*key
,
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
);
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
)
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
)
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
;
237 bio_list_init(&bios
);
238 dm_cell_release(prison
, cell
, &bios
);
240 while ((bio
= bio_list_pop(&bios
))) {
241 bio
->bi_status
= error
;
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
*),
250 struct dm_bio_prison_cell
*cell
)
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
);
269 cell
->holder
= bio_list_pop(&cell
->bios
);
273 int dm_cell_promote_or_release(struct dm_bio_prison
*prison
,
274 struct dm_bio_prison_cell
*cell
)
279 spin_lock_irqsave(&prison
->lock
, flags
);
280 r
= __promote_or_release(prison
, cell
);
281 spin_unlock_irqrestore(&prison
->lock
, flags
);
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
;
294 struct list_head work_items
;
297 struct dm_deferred_set
{
299 unsigned current_entry
;
301 struct dm_deferred_entry entries
[DEFERRED_SET_SIZE
];
304 struct dm_deferred_set
*dm_deferred_set_create(void)
307 struct dm_deferred_set
*ds
;
309 ds
= kmalloc(sizeof(*ds
), GFP_KERNEL
);
313 spin_lock_init(&ds
->lock
);
314 ds
->current_entry
= 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
);
324 EXPORT_SYMBOL_GPL(dm_deferred_set_create
);
326 void dm_deferred_set_destroy(struct dm_deferred_set
*ds
)
330 EXPORT_SYMBOL_GPL(dm_deferred_set_destroy
);
332 struct dm_deferred_entry
*dm_deferred_entry_inc(struct dm_deferred_set
*ds
)
335 struct dm_deferred_entry
*entry
;
337 spin_lock_irqsave(&ds
->lock
, flags
);
338 entry
= ds
->entries
+ ds
->current_entry
;
340 spin_unlock_irqrestore(&ds
->lock
, flags
);
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
)
367 spin_lock_irqsave(&entry
->ds
->lock
, flags
);
368 BUG_ON(!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
)
384 spin_lock_irqsave(&ds
->lock
, flags
);
385 if ((ds
->sweeper
== ds
->current_entry
) &&
386 !ds
->entries
[ds
->current_entry
].count
)
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
);
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);
411 static void dm_bio_prison_exit_v1(void)
413 kmem_cache_destroy(_cell_cache
);
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
);
433 for (i
= 0; i
< count
; i
++) {
448 static void __exit
dm_bio_prison_exit(void)
450 int i
= ARRAY_SIZE(_exits
);
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");