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