xref: /openbmc/linux/drivers/md/dm-core.h (revision 7671f967)
1 /*
2  * Internal header file _only_ for device mapper core
3  *
4  * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
5  *
6  * This file is released under the LGPL.
7  */
8 
9 #ifndef DM_CORE_INTERNAL_H
10 #define DM_CORE_INTERNAL_H
11 
12 #include <linux/kthread.h>
13 #include <linux/ktime.h>
14 #include <linux/blk-mq.h>
15 #include <linux/blk-crypto-profile.h>
16 
17 #include <trace/events/block.h>
18 
19 #include "dm.h"
20 #include "dm-ima.h"
21 
22 #define DM_RESERVED_MAX_IOS		1024
23 
24 struct dm_kobject_holder {
25 	struct kobject kobj;
26 	struct completion completion;
27 };
28 
29 /*
30  * DM core internal structures used directly by dm.c, dm-rq.c and dm-table.c.
31  * DM targets must _not_ deference a mapped_device or dm_table to directly
32  * access their members!
33  */
34 
35 struct mapped_device {
36 	struct mutex suspend_lock;
37 
38 	struct mutex table_devices_lock;
39 	struct list_head table_devices;
40 
41 	/*
42 	 * The current mapping (struct dm_table *).
43 	 * Use dm_get_live_table{_fast} or take suspend_lock for
44 	 * dereference.
45 	 */
46 	void __rcu *map;
47 
48 	unsigned long flags;
49 
50 	/* Protect queue and type against concurrent access. */
51 	struct mutex type_lock;
52 	enum dm_queue_mode type;
53 
54 	int numa_node_id;
55 	struct request_queue *queue;
56 
57 	atomic_t holders;
58 	atomic_t open_count;
59 
60 	struct dm_target *immutable_target;
61 	struct target_type *immutable_target_type;
62 
63 	char name[16];
64 	struct gendisk *disk;
65 	struct dax_device *dax_dev;
66 
67 	wait_queue_head_t wait;
68 	unsigned long __percpu *pending_io;
69 
70 	/* forced geometry settings */
71 	struct hd_geometry geometry;
72 
73 	/*
74 	 * Processing queue (flush)
75 	 */
76 	struct workqueue_struct *wq;
77 
78 	/*
79 	 * A list of ios that arrived while we were suspended.
80 	 */
81 	struct work_struct work;
82 	spinlock_t deferred_lock;
83 	struct bio_list deferred;
84 
85 	void *interface_ptr;
86 
87 	/*
88 	 * Event handling.
89 	 */
90 	wait_queue_head_t eventq;
91 	atomic_t event_nr;
92 	atomic_t uevent_seq;
93 	struct list_head uevent_list;
94 	spinlock_t uevent_lock; /* Protect access to uevent_list */
95 
96 	/* for blk-mq request-based DM support */
97 	bool init_tio_pdu:1;
98 	struct blk_mq_tag_set *tag_set;
99 
100 	struct dm_stats stats;
101 
102 	/* the number of internal suspends */
103 	unsigned internal_suspend_count;
104 
105 	int swap_bios;
106 	struct semaphore swap_bios_semaphore;
107 	struct mutex swap_bios_lock;
108 
109 	/*
110 	 * io objects are allocated from here.
111 	 */
112 	struct bio_set io_bs;
113 	struct bio_set bs;
114 
115 	/* kobject and completion */
116 	struct dm_kobject_holder kobj_holder;
117 
118 	struct srcu_struct io_barrier;
119 
120 #ifdef CONFIG_BLK_DEV_ZONED
121 	unsigned int nr_zones;
122 	unsigned int *zwp_offset;
123 #endif
124 
125 #ifdef CONFIG_IMA
126 	struct dm_ima_measurements ima;
127 #endif
128 };
129 
130 /*
131  * Bits for the flags field of struct mapped_device.
132  */
133 #define DMF_BLOCK_IO_FOR_SUSPEND 0
134 #define DMF_SUSPENDED 1
135 #define DMF_FROZEN 2
136 #define DMF_FREEING 3
137 #define DMF_DELETING 4
138 #define DMF_NOFLUSH_SUSPENDING 5
139 #define DMF_DEFERRED_REMOVE 6
140 #define DMF_SUSPENDED_INTERNALLY 7
141 #define DMF_POST_SUSPENDING 8
142 #define DMF_EMULATE_ZONE_APPEND 9
143 
144 void disable_discard(struct mapped_device *md);
145 void disable_write_zeroes(struct mapped_device *md);
146 
147 static inline sector_t dm_get_size(struct mapped_device *md)
148 {
149 	return get_capacity(md->disk);
150 }
151 
152 static inline struct dm_stats *dm_get_stats(struct mapped_device *md)
153 {
154 	return &md->stats;
155 }
156 
157 static inline bool dm_emulate_zone_append(struct mapped_device *md)
158 {
159 	if (blk_queue_is_zoned(md->queue))
160 		return test_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
161 	return false;
162 }
163 
164 #define DM_TABLE_MAX_DEPTH 16
165 
166 struct dm_table {
167 	struct mapped_device *md;
168 	enum dm_queue_mode type;
169 
170 	/* btree table */
171 	unsigned int depth;
172 	unsigned int counts[DM_TABLE_MAX_DEPTH]; /* in nodes */
173 	sector_t *index[DM_TABLE_MAX_DEPTH];
174 
175 	unsigned int num_targets;
176 	unsigned int num_allocated;
177 	sector_t *highs;
178 	struct dm_target *targets;
179 
180 	struct target_type *immutable_target_type;
181 
182 	bool integrity_supported:1;
183 	bool singleton:1;
184 	unsigned integrity_added:1;
185 
186 	/*
187 	 * Indicates the rw permissions for the new logical
188 	 * device.  This should be a combination of FMODE_READ
189 	 * and FMODE_WRITE.
190 	 */
191 	fmode_t mode;
192 
193 	/* a list of devices used by this table */
194 	struct list_head devices;
195 
196 	/* events get handed up using this callback */
197 	void (*event_fn)(void *);
198 	void *event_context;
199 
200 	struct dm_md_mempools *mempools;
201 
202 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
203 	struct blk_crypto_profile *crypto_profile;
204 #endif
205 };
206 
207 /*
208  * One of these is allocated per clone bio.
209  */
210 #define DM_TIO_MAGIC 28714
211 struct dm_target_io {
212 	unsigned short magic;
213 	blk_short_t flags;
214 	unsigned int target_bio_nr;
215 	struct dm_io *io;
216 	struct dm_target *ti;
217 	unsigned int *len_ptr;
218 	sector_t old_sector;
219 	struct bio clone;
220 };
221 
222 /*
223  * dm_target_io flags
224  */
225 enum {
226 	DM_TIO_INSIDE_DM_IO,
227 	DM_TIO_IS_DUPLICATE_BIO
228 };
229 
230 static inline bool dm_tio_flagged(struct dm_target_io *tio, unsigned int bit)
231 {
232 	return (tio->flags & (1U << bit)) != 0;
233 }
234 
235 static inline void dm_tio_set_flag(struct dm_target_io *tio, unsigned int bit)
236 {
237 	tio->flags |= (1U << bit);
238 }
239 
240 /*
241  * One of these is allocated per original bio.
242  * It contains the first clone used for that original.
243  */
244 #define DM_IO_MAGIC 19577
245 struct dm_io {
246 	unsigned short magic;
247 	blk_short_t flags;
248 	atomic_t io_count;
249 	struct mapped_device *md;
250 	struct bio *orig_bio;
251 	blk_status_t status;
252 	spinlock_t lock;
253 	unsigned long start_time;
254 	void *data;
255 	struct hlist_node node;
256 	struct task_struct *map_task;
257 	struct dm_stats_aux stats_aux;
258 	/* last member of dm_target_io is 'struct bio' */
259 	struct dm_target_io tio;
260 };
261 
262 /*
263  * dm_io flags
264  */
265 enum {
266 	DM_IO_START_ACCT,
267 	DM_IO_ACCOUNTED
268 };
269 
270 static inline bool dm_io_flagged(struct dm_io *io, unsigned int bit)
271 {
272 	return (io->flags & (1U << bit)) != 0;
273 }
274 
275 static inline void dm_io_set_flag(struct dm_io *io, unsigned int bit)
276 {
277 	io->flags |= (1U << bit);
278 }
279 
280 static inline void dm_io_inc_pending(struct dm_io *io)
281 {
282 	atomic_inc(&io->io_count);
283 }
284 
285 void dm_io_dec_pending(struct dm_io *io, blk_status_t error);
286 
287 static inline struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
288 {
289 	return &container_of(kobj, struct dm_kobject_holder, kobj)->completion;
290 }
291 
292 unsigned __dm_get_module_param(unsigned *module_param, unsigned def, unsigned max);
293 
294 static inline bool dm_message_test_buffer_overflow(char *result, unsigned maxlen)
295 {
296 	return !maxlen || strlen(result) + 1 >= maxlen;
297 }
298 
299 extern atomic_t dm_global_event_nr;
300 extern wait_queue_head_t dm_global_eventq;
301 void dm_issue_global_event(void);
302 
303 #endif
304