2 * Copyright (C) 2011-2017 Red Hat, Inc.
4 * This file is released under the GPL.
7 #ifndef DM_BIO_PRISON_V2_H
8 #define DM_BIO_PRISON_V2_H
10 #include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */
11 #include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */
13 #include <linux/bio.h>
14 #include <linux/rbtree.h>
15 #include <linux/workqueue.h>
17 /*----------------------------------------------------------------*/
19 int dm_bio_prison_init_v2(void);
20 void dm_bio_prison_exit_v2(void);
23 * Sometimes we can't deal with a bio straight away. We put them in prison
24 * where they can't cause any mischief. Bios are put in a cell identified
25 * by a key, multiple bios can be in the same cell. When the cell is
26 * subsequently unlocked the bios become available.
28 struct dm_bio_prison_v2;
31 * Keys define a range of blocks within either a virtual or physical
34 struct dm_cell_key_v2 {
37 dm_block_t block_begin, block_end;
41 * Treat this as opaque, only in header so callers can manage allocation
44 struct dm_bio_prison_cell_v2 {
47 unsigned exclusive_level;
48 unsigned shared_count;
49 struct work_struct *quiesce_continuation;
52 struct dm_cell_key_v2 key;
56 struct dm_bio_prison_v2 *dm_bio_prison_create_v2(struct workqueue_struct *wq);
57 void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2 *prison);
60 * These two functions just wrap a mempool. This is a transitory step:
61 * Eventually all bio prison clients should manage their own cell memory.
63 * Like mempool_alloc(), dm_bio_prison_alloc_cell_v2() can only fail if called
64 * in interrupt context or passed GFP_NOWAIT.
66 struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison,
68 void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison,
69 struct dm_bio_prison_cell_v2 *cell);
72 * Shared locks have a bio associated with them.
74 * If the lock is granted the caller can continue to use the bio, and must
75 * call dm_cell_put_v2() to drop the reference count when finished using it.
77 * If the lock cannot be granted then the bio will be tracked within the
78 * cell, and later given to the holder of the exclusive lock.
80 * See dm_cell_lock_v2() for discussion of the lock_level parameter.
82 * Compare *cell_result with cell_prealloc to see if the prealloc was used.
83 * If cell_prealloc was used then inmate wasn't added to it.
85 * Returns true if the lock is granted.
87 bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison,
88 struct dm_cell_key_v2 *key,
91 struct dm_bio_prison_cell_v2 *cell_prealloc,
92 struct dm_bio_prison_cell_v2 **cell_result);
95 * Decrement the shared reference count for the lock. Returns true if
96 * returning ownership of the cell (ie. you should free it).
98 bool dm_cell_put_v2(struct dm_bio_prison_v2 *prison,
99 struct dm_bio_prison_cell_v2 *cell);
102 * Locks a cell. No associated bio. Exclusive locks get priority. These
103 * locks constrain whether the io locks are granted according to level.
105 * Shared locks will still be granted if the lock_level is > (not = to) the
106 * exclusive lock level.
108 * If an _exclusive_ lock is already held then -EBUSY is returned.
112 * 0 - locked; no quiescing needed
113 * 1 - locked; quiescing needed
115 int dm_cell_lock_v2(struct dm_bio_prison_v2 *prison,
116 struct dm_cell_key_v2 *key,
118 struct dm_bio_prison_cell_v2 *cell_prealloc,
119 struct dm_bio_prison_cell_v2 **cell_result);
121 void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison,
122 struct dm_bio_prison_cell_v2 *cell,
123 struct work_struct *continuation);
126 * Promotes an _exclusive_ lock to a higher lock level.
130 * 0 - promoted; no quiescing needed
131 * 1 - promoted; quiescing needed
133 int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison,
134 struct dm_bio_prison_cell_v2 *cell,
135 unsigned new_lock_level);
138 * Adds any held bios to the bio list.
140 * There may be shared locks still held at this point even if you quiesced
141 * (ie. different lock levels).
143 * Returns true if returning ownership of the cell (ie. you should free
146 bool dm_cell_unlock_v2(struct dm_bio_prison_v2 *prison,
147 struct dm_bio_prison_cell_v2 *cell,
148 struct bio_list *bios);
150 /*----------------------------------------------------------------*/