xref: /linux/drivers/md/dm-bio-prison-v2.h (revision 132db93572821ec2fdf81e354cc40f558faf7e4f)
1 /*
2  * Copyright (C) 2011-2017 Red Hat, Inc.
3  *
4  * This file is released under the GPL.
5  */
6 
7 #ifndef DM_BIO_PRISON_V2_H
8 #define DM_BIO_PRISON_V2_H
9 
10 #include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */
11 #include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */
12 
13 #include <linux/bio.h>
14 #include <linux/rbtree.h>
15 #include <linux/workqueue.h>
16 
17 /*----------------------------------------------------------------*/
18 
19 int dm_bio_prison_init_v2(void);
20 void dm_bio_prison_exit_v2(void);
21 
22 /*
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.
27  */
28 struct dm_bio_prison_v2;
29 
30 /*
31  * Keys define a range of blocks within either a virtual or physical
32  * device.
33  */
34 struct dm_cell_key_v2 {
35 	int virtual;
36 	dm_thin_id dev;
37 	dm_block_t block_begin, block_end;
38 };
39 
40 /*
41  * Treat this as opaque, only in header so callers can manage allocation
42  * themselves.
43  */
44 struct dm_bio_prison_cell_v2 {
45 	// FIXME: pack these
46 	bool exclusive_lock;
47 	unsigned exclusive_level;
48 	unsigned shared_count;
49 	struct work_struct *quiesce_continuation;
50 
51 	struct rb_node node;
52 	struct dm_cell_key_v2 key;
53 	struct bio_list bios;
54 };
55 
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);
58 
59 /*
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.
62  *
63  * Like mempool_alloc(), dm_bio_prison_alloc_cell_v2() can only fail if called
64  * in interrupt context or passed GFP_NOWAIT.
65  */
66 struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison,
67 						    gfp_t gfp);
68 void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison,
69 				struct dm_bio_prison_cell_v2 *cell);
70 
71 /*
72  * Shared locks have a bio associated with them.
73  *
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.
76  *
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.
79  *
80  * See dm_cell_lock_v2() for discussion of the lock_level parameter.
81  *
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.
84  *
85  * Returns true if the lock is granted.
86  */
87 bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison,
88 		    struct dm_cell_key_v2 *key,
89 		    unsigned lock_level,
90 		    struct bio *inmate,
91 		    struct dm_bio_prison_cell_v2 *cell_prealloc,
92 		    struct dm_bio_prison_cell_v2 **cell_result);
93 
94 /*
95  * Decrement the shared reference count for the lock.  Returns true if
96  * returning ownership of the cell (ie. you should free it).
97  */
98 bool dm_cell_put_v2(struct dm_bio_prison_v2 *prison,
99 		    struct dm_bio_prison_cell_v2 *cell);
100 
101 /*
102  * Locks a cell.  No associated bio.  Exclusive locks get priority.  These
103  * locks constrain whether the io locks are granted according to level.
104  *
105  * Shared locks will still be granted if the lock_level is > (not = to) the
106  * exclusive lock level.
107  *
108  * If an _exclusive_ lock is already held then -EBUSY is returned.
109  *
110  * Return values:
111  *  < 0 - error
112  *  0   - locked; no quiescing needed
113  *  1   - locked; quiescing needed
114  */
115 int dm_cell_lock_v2(struct dm_bio_prison_v2 *prison,
116 		    struct dm_cell_key_v2 *key,
117 		    unsigned lock_level,
118 		    struct dm_bio_prison_cell_v2 *cell_prealloc,
119 		    struct dm_bio_prison_cell_v2 **cell_result);
120 
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);
124 
125 /*
126  * Promotes an _exclusive_ lock to a higher lock level.
127  *
128  * Return values:
129  *  < 0 - error
130  *  0   - promoted; no quiescing needed
131  *  1   - promoted; quiescing needed
132  */
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);
136 
137 /*
138  * Adds any held bios to the bio list.
139  *
140  * There may be shared locks still held at this point even if you quiesced
141  * (ie. different lock levels).
142  *
143  * Returns true if returning ownership of the cell (ie. you should free
144  * it).
145  */
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);
149 
150 /*----------------------------------------------------------------*/
151 
152 #endif
153