1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2011-2017 Red Hat, Inc.
5 * This file is released under the GPL.
8 #ifndef DM_BIO_PRISON_V2_H
9 #define DM_BIO_PRISON_V2_H
11 #include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */
12 #include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */
14 #include <linux/bio.h>
15 #include <linux/rbtree.h>
16 #include <linux/workqueue.h>
18 /*----------------------------------------------------------------*/
20 int dm_bio_prison_init_v2(void);
21 void dm_bio_prison_exit_v2(void);
24 * Sometimes we can't deal with a bio straight away. We put them in prison
25 * where they can't cause any mischief. Bios are put in a cell identified
26 * by a key, multiple bios can be in the same cell. When the cell is
27 * subsequently unlocked the bios become available.
29 struct dm_bio_prison_v2
;
32 * Keys define a range of blocks within either a virtual or physical
35 struct dm_cell_key_v2
{
38 dm_block_t block_begin
, block_end
;
42 * Treat this as opaque, only in header so callers can manage allocation
45 struct dm_bio_prison_cell_v2
{
48 unsigned int exclusive_level
;
49 unsigned int shared_count
;
50 struct work_struct
*quiesce_continuation
;
53 struct dm_cell_key_v2 key
;
57 struct dm_bio_prison_v2
*dm_bio_prison_create_v2(struct workqueue_struct
*wq
);
58 void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2
*prison
);
61 * These two functions just wrap a mempool. This is a transitory step:
62 * Eventually all bio prison clients should manage their own cell memory.
64 * Like mempool_alloc(), dm_bio_prison_alloc_cell_v2() can only fail if called
65 * in interrupt context or passed GFP_NOWAIT.
67 struct dm_bio_prison_cell_v2
*dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2
*prison
,
69 void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2
*prison
,
70 struct dm_bio_prison_cell_v2
*cell
);
73 * Shared locks have a bio associated with them.
75 * If the lock is granted the caller can continue to use the bio, and must
76 * call dm_cell_put_v2() to drop the reference count when finished using it.
78 * If the lock cannot be granted then the bio will be tracked within the
79 * cell, and later given to the holder of the exclusive lock.
81 * See dm_cell_lock_v2() for discussion of the lock_level parameter.
83 * Compare *cell_result with cell_prealloc to see if the prealloc was used.
84 * If cell_prealloc was used then inmate wasn't added to it.
86 * Returns true if the lock is granted.
88 bool dm_cell_get_v2(struct dm_bio_prison_v2
*prison
,
89 struct dm_cell_key_v2
*key
,
90 unsigned int lock_level
,
92 struct dm_bio_prison_cell_v2
*cell_prealloc
,
93 struct dm_bio_prison_cell_v2
**cell_result
);
96 * Decrement the shared reference count for the lock. Returns true if
97 * returning ownership of the cell (ie. you should free it).
99 bool dm_cell_put_v2(struct dm_bio_prison_v2
*prison
,
100 struct dm_bio_prison_cell_v2
*cell
);
103 * Locks a cell. No associated bio. Exclusive locks get priority. These
104 * locks constrain whether the io locks are granted according to level.
106 * Shared locks will still be granted if the lock_level is > (not = to) the
107 * exclusive lock level.
109 * If an _exclusive_ lock is already held then -EBUSY is returned.
113 * 0 - locked; no quiescing needed
114 * 1 - locked; quiescing needed
116 int dm_cell_lock_v2(struct dm_bio_prison_v2
*prison
,
117 struct dm_cell_key_v2
*key
,
118 unsigned int lock_level
,
119 struct dm_bio_prison_cell_v2
*cell_prealloc
,
120 struct dm_bio_prison_cell_v2
**cell_result
);
122 void dm_cell_quiesce_v2(struct dm_bio_prison_v2
*prison
,
123 struct dm_bio_prison_cell_v2
*cell
,
124 struct work_struct
*continuation
);
127 * Promotes an _exclusive_ lock to a higher lock level.
131 * 0 - promoted; no quiescing needed
132 * 1 - promoted; quiescing needed
134 int dm_cell_lock_promote_v2(struct dm_bio_prison_v2
*prison
,
135 struct dm_bio_prison_cell_v2
*cell
,
136 unsigned int new_lock_level
);
139 * Adds any held bios to the bio list.
141 * There may be shared locks still held at this point even if you quiesced
142 * (ie. different lock levels).
144 * Returns true if returning ownership of the cell (ie. you should free
147 bool dm_cell_unlock_v2(struct dm_bio_prison_v2
*prison
,
148 struct dm_bio_prison_cell_v2
*cell
,
149 struct bio_list
*bios
);
151 /*----------------------------------------------------------------*/