2 * Copyright (C) 2012 Red Hat, Inc.
4 * This file is released under the GPL.
8 #include "dm-bio-prison.h"
10 #include <linux/spinlock.h>
11 #include <linux/mempool.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
15 /*----------------------------------------------------------------*/
17 #define MIN_CELLS 1024
19 struct dm_bio_prison
{
25 static struct kmem_cache
*_cell_cache
;
27 /*----------------------------------------------------------------*/
30 * @nr_cells should be the number of cells you want in use _concurrently_.
31 * Don't confuse it with the number of distinct keys.
33 struct dm_bio_prison
*dm_bio_prison_create(void)
35 struct dm_bio_prison
*prison
= kmalloc(sizeof(*prison
), GFP_KERNEL
);
40 spin_lock_init(&prison
->lock
);
42 prison
->cell_pool
= mempool_create_slab_pool(MIN_CELLS
, _cell_cache
);
43 if (!prison
->cell_pool
) {
48 prison
->cells
= RB_ROOT
;
52 EXPORT_SYMBOL_GPL(dm_bio_prison_create
);
54 void dm_bio_prison_destroy(struct dm_bio_prison
*prison
)
56 mempool_destroy(prison
->cell_pool
);
59 EXPORT_SYMBOL_GPL(dm_bio_prison_destroy
);
61 struct dm_bio_prison_cell
*dm_bio_prison_alloc_cell(struct dm_bio_prison
*prison
, gfp_t gfp
)
63 return mempool_alloc(prison
->cell_pool
, gfp
);
65 EXPORT_SYMBOL_GPL(dm_bio_prison_alloc_cell
);
67 void dm_bio_prison_free_cell(struct dm_bio_prison
*prison
,
68 struct dm_bio_prison_cell
*cell
)
70 mempool_free(cell
, prison
->cell_pool
);
72 EXPORT_SYMBOL_GPL(dm_bio_prison_free_cell
);
74 static void __setup_new_cell(struct dm_cell_key
*key
,
76 struct dm_bio_prison_cell
*cell
)
78 memcpy(&cell
->key
, key
, sizeof(cell
->key
));
79 cell
->holder
= holder
;
80 bio_list_init(&cell
->bios
);
83 static int cmp_keys(struct dm_cell_key
*lhs
,
84 struct dm_cell_key
*rhs
)
86 if (lhs
->virtual < rhs
->virtual)
89 if (lhs
->virtual > rhs
->virtual)
92 if (lhs
->dev
< rhs
->dev
)
95 if (lhs
->dev
> rhs
->dev
)
98 if (lhs
->block_end
<= rhs
->block_begin
)
101 if (lhs
->block_begin
>= rhs
->block_end
)
107 static int __bio_detain(struct dm_bio_prison
*prison
,
108 struct dm_cell_key
*key
,
110 struct dm_bio_prison_cell
*cell_prealloc
,
111 struct dm_bio_prison_cell
**cell_result
)
114 struct rb_node
**new = &prison
->cells
.rb_node
, *parent
= NULL
;
117 struct dm_bio_prison_cell
*cell
=
118 container_of(*new, struct dm_bio_prison_cell
, node
);
120 r
= cmp_keys(key
, &cell
->key
);
124 new = &((*new)->rb_left
);
126 new = &((*new)->rb_right
);
129 bio_list_add(&cell
->bios
, inmate
);
135 __setup_new_cell(key
, inmate
, cell_prealloc
);
136 *cell_result
= cell_prealloc
;
138 rb_link_node(&cell_prealloc
->node
, parent
, new);
139 rb_insert_color(&cell_prealloc
->node
, &prison
->cells
);
144 static int bio_detain(struct dm_bio_prison
*prison
,
145 struct dm_cell_key
*key
,
147 struct dm_bio_prison_cell
*cell_prealloc
,
148 struct dm_bio_prison_cell
**cell_result
)
153 spin_lock_irqsave(&prison
->lock
, flags
);
154 r
= __bio_detain(prison
, key
, inmate
, cell_prealloc
, cell_result
);
155 spin_unlock_irqrestore(&prison
->lock
, flags
);
160 int dm_bio_detain(struct dm_bio_prison
*prison
,
161 struct dm_cell_key
*key
,
163 struct dm_bio_prison_cell
*cell_prealloc
,
164 struct dm_bio_prison_cell
**cell_result
)
166 return bio_detain(prison
, key
, inmate
, cell_prealloc
, cell_result
);
168 EXPORT_SYMBOL_GPL(dm_bio_detain
);
170 int dm_get_cell(struct dm_bio_prison
*prison
,
171 struct dm_cell_key
*key
,
172 struct dm_bio_prison_cell
*cell_prealloc
,
173 struct dm_bio_prison_cell
**cell_result
)
175 return bio_detain(prison
, key
, NULL
, cell_prealloc
, cell_result
);
177 EXPORT_SYMBOL_GPL(dm_get_cell
);
180 * @inmates must have been initialised prior to this call
182 static void __cell_release(struct dm_bio_prison
*prison
,
183 struct dm_bio_prison_cell
*cell
,
184 struct bio_list
*inmates
)
186 rb_erase(&cell
->node
, &prison
->cells
);
190 bio_list_add(inmates
, cell
->holder
);
191 bio_list_merge(inmates
, &cell
->bios
);
195 void dm_cell_release(struct dm_bio_prison
*prison
,
196 struct dm_bio_prison_cell
*cell
,
197 struct bio_list
*bios
)
201 spin_lock_irqsave(&prison
->lock
, flags
);
202 __cell_release(prison
, cell
, bios
);
203 spin_unlock_irqrestore(&prison
->lock
, flags
);
205 EXPORT_SYMBOL_GPL(dm_cell_release
);
208 * Sometimes we don't want the holder, just the additional bios.
210 static void __cell_release_no_holder(struct dm_bio_prison
*prison
,
211 struct dm_bio_prison_cell
*cell
,
212 struct bio_list
*inmates
)
214 rb_erase(&cell
->node
, &prison
->cells
);
215 bio_list_merge(inmates
, &cell
->bios
);
218 void dm_cell_release_no_holder(struct dm_bio_prison
*prison
,
219 struct dm_bio_prison_cell
*cell
,
220 struct bio_list
*inmates
)
224 spin_lock_irqsave(&prison
->lock
, flags
);
225 __cell_release_no_holder(prison
, cell
, inmates
);
226 spin_unlock_irqrestore(&prison
->lock
, flags
);
228 EXPORT_SYMBOL_GPL(dm_cell_release_no_holder
);
230 void dm_cell_error(struct dm_bio_prison
*prison
,
231 struct dm_bio_prison_cell
*cell
, int error
)
233 struct bio_list bios
;
236 bio_list_init(&bios
);
237 dm_cell_release(prison
, cell
, &bios
);
239 while ((bio
= bio_list_pop(&bios
))) {
240 bio
->bi_error
= error
;
244 EXPORT_SYMBOL_GPL(dm_cell_error
);
246 void dm_cell_visit_release(struct dm_bio_prison
*prison
,
247 void (*visit_fn
)(void *, struct dm_bio_prison_cell
*),
249 struct dm_bio_prison_cell
*cell
)
253 spin_lock_irqsave(&prison
->lock
, flags
);
254 visit_fn(context
, cell
);
255 rb_erase(&cell
->node
, &prison
->cells
);
256 spin_unlock_irqrestore(&prison
->lock
, flags
);
258 EXPORT_SYMBOL_GPL(dm_cell_visit_release
);
260 static int __promote_or_release(struct dm_bio_prison
*prison
,
261 struct dm_bio_prison_cell
*cell
)
263 if (bio_list_empty(&cell
->bios
)) {
264 rb_erase(&cell
->node
, &prison
->cells
);
268 cell
->holder
= bio_list_pop(&cell
->bios
);
272 int dm_cell_promote_or_release(struct dm_bio_prison
*prison
,
273 struct dm_bio_prison_cell
*cell
)
278 spin_lock_irqsave(&prison
->lock
, flags
);
279 r
= __promote_or_release(prison
, cell
);
280 spin_unlock_irqrestore(&prison
->lock
, flags
);
284 EXPORT_SYMBOL_GPL(dm_cell_promote_or_release
);
286 /*----------------------------------------------------------------*/
288 #define DEFERRED_SET_SIZE 64
290 struct dm_deferred_entry
{
291 struct dm_deferred_set
*ds
;
293 struct list_head work_items
;
296 struct dm_deferred_set
{
298 unsigned current_entry
;
300 struct dm_deferred_entry entries
[DEFERRED_SET_SIZE
];
303 struct dm_deferred_set
*dm_deferred_set_create(void)
306 struct dm_deferred_set
*ds
;
308 ds
= kmalloc(sizeof(*ds
), GFP_KERNEL
);
312 spin_lock_init(&ds
->lock
);
313 ds
->current_entry
= 0;
315 for (i
= 0; i
< DEFERRED_SET_SIZE
; i
++) {
316 ds
->entries
[i
].ds
= ds
;
317 ds
->entries
[i
].count
= 0;
318 INIT_LIST_HEAD(&ds
->entries
[i
].work_items
);
323 EXPORT_SYMBOL_GPL(dm_deferred_set_create
);
325 void dm_deferred_set_destroy(struct dm_deferred_set
*ds
)
329 EXPORT_SYMBOL_GPL(dm_deferred_set_destroy
);
331 struct dm_deferred_entry
*dm_deferred_entry_inc(struct dm_deferred_set
*ds
)
334 struct dm_deferred_entry
*entry
;
336 spin_lock_irqsave(&ds
->lock
, flags
);
337 entry
= ds
->entries
+ ds
->current_entry
;
339 spin_unlock_irqrestore(&ds
->lock
, flags
);
343 EXPORT_SYMBOL_GPL(dm_deferred_entry_inc
);
345 static unsigned ds_next(unsigned index
)
347 return (index
+ 1) % DEFERRED_SET_SIZE
;
350 static void __sweep(struct dm_deferred_set
*ds
, struct list_head
*head
)
352 while ((ds
->sweeper
!= ds
->current_entry
) &&
353 !ds
->entries
[ds
->sweeper
].count
) {
354 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
355 ds
->sweeper
= ds_next(ds
->sweeper
);
358 if ((ds
->sweeper
== ds
->current_entry
) && !ds
->entries
[ds
->sweeper
].count
)
359 list_splice_init(&ds
->entries
[ds
->sweeper
].work_items
, head
);
362 void dm_deferred_entry_dec(struct dm_deferred_entry
*entry
, struct list_head
*head
)
366 spin_lock_irqsave(&entry
->ds
->lock
, flags
);
367 BUG_ON(!entry
->count
);
369 __sweep(entry
->ds
, head
);
370 spin_unlock_irqrestore(&entry
->ds
->lock
, flags
);
372 EXPORT_SYMBOL_GPL(dm_deferred_entry_dec
);
375 * Returns 1 if deferred or 0 if no pending items to delay job.
377 int dm_deferred_set_add_work(struct dm_deferred_set
*ds
, struct list_head
*work
)
383 spin_lock_irqsave(&ds
->lock
, flags
);
384 if ((ds
->sweeper
== ds
->current_entry
) &&
385 !ds
->entries
[ds
->current_entry
].count
)
388 list_add(work
, &ds
->entries
[ds
->current_entry
].work_items
);
389 next_entry
= ds_next(ds
->current_entry
);
390 if (!ds
->entries
[next_entry
].count
)
391 ds
->current_entry
= next_entry
;
393 spin_unlock_irqrestore(&ds
->lock
, flags
);
397 EXPORT_SYMBOL_GPL(dm_deferred_set_add_work
);
399 /*----------------------------------------------------------------*/
401 static int __init
dm_bio_prison_init(void)
403 _cell_cache
= KMEM_CACHE(dm_bio_prison_cell
, 0);
410 static void __exit
dm_bio_prison_exit(void)
412 kmem_cache_destroy(_cell_cache
);
419 module_init(dm_bio_prison_init
);
420 module_exit(dm_bio_prison_exit
);
422 MODULE_DESCRIPTION(DM_NAME
" bio prison");
423 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
424 MODULE_LICENSE("GPL");