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