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