1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Internal header file _only_ for device mapper core
5 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
7 * This file is released under the LGPL.
10 #ifndef DM_CORE_INTERNAL_H
11 #define DM_CORE_INTERNAL_H
13 #include <linux/kthread.h>
14 #include <linux/ktime.h>
15 #include <linux/blk-mq.h>
16 #include <linux/blk-crypto-profile.h>
17 #include <linux/jump_label.h>
19 #include <trace/events/block.h>
24 #define DM_RESERVED_MAX_IOS 1024
28 struct dm_kobject_holder {
30 struct completion completion;
34 * DM core internal structures used directly by dm.c, dm-rq.c and dm-table.c.
35 * DM targets must _not_ deference a mapped_device or dm_table to directly
36 * access their members!
40 * For mempools pre-allocation at the table loading time.
42 struct dm_md_mempools {
47 struct mapped_device {
48 struct mutex suspend_lock;
50 struct mutex table_devices_lock;
51 struct list_head table_devices;
54 * The current mapping (struct dm_table *).
55 * Use dm_get_live_table{_fast} or take suspend_lock for
62 /* Protect queue and type against concurrent access. */
63 struct mutex type_lock;
64 enum dm_queue_mode type;
67 struct request_queue *queue;
72 struct dm_target *immutable_target;
73 struct target_type *immutable_target_type;
77 struct dax_device *dax_dev;
79 wait_queue_head_t wait;
80 unsigned long __percpu *pending_io;
82 /* forced geometry settings */
83 struct hd_geometry geometry;
86 * Processing queue (flush)
88 struct workqueue_struct *wq;
91 * A list of ios that arrived while we were suspended.
93 struct work_struct work;
94 spinlock_t deferred_lock;
95 struct bio_list deferred;
98 * requeue work context is needed for cloning one new bio
99 * to represent the dm_io to be requeued, since each
100 * dm_io may point to the original bio from FS.
102 struct work_struct requeue_work;
103 struct dm_io *requeue_list;
110 wait_queue_head_t eventq;
113 struct list_head uevent_list;
114 spinlock_t uevent_lock; /* Protect access to uevent_list */
116 /* for blk-mq request-based DM support */
118 struct blk_mq_tag_set *tag_set;
120 struct dm_stats stats;
122 /* the number of internal suspends */
123 unsigned int internal_suspend_count;
126 struct semaphore swap_bios_semaphore;
127 struct mutex swap_bios_lock;
130 * io objects are allocated from here.
132 struct dm_md_mempools *mempools;
134 /* kobject and completion */
135 struct dm_kobject_holder kobj_holder;
137 struct srcu_struct io_barrier;
139 #ifdef CONFIG_BLK_DEV_ZONED
140 unsigned int nr_zones;
141 unsigned int *zwp_offset;
145 struct dm_ima_measurements ima;
150 * Bits for the flags field of struct mapped_device.
152 #define DMF_BLOCK_IO_FOR_SUSPEND 0
153 #define DMF_SUSPENDED 1
155 #define DMF_FREEING 3
156 #define DMF_DELETING 4
157 #define DMF_NOFLUSH_SUSPENDING 5
158 #define DMF_DEFERRED_REMOVE 6
159 #define DMF_SUSPENDED_INTERNALLY 7
160 #define DMF_POST_SUSPENDING 8
161 #define DMF_EMULATE_ZONE_APPEND 9
163 void disable_discard(struct mapped_device *md);
164 void disable_write_zeroes(struct mapped_device *md);
166 static inline sector_t dm_get_size(struct mapped_device *md)
168 return get_capacity(md->disk);
171 static inline struct dm_stats *dm_get_stats(struct mapped_device *md)
176 DECLARE_STATIC_KEY_FALSE(stats_enabled);
177 DECLARE_STATIC_KEY_FALSE(swap_bios_enabled);
178 DECLARE_STATIC_KEY_FALSE(zoned_enabled);
180 static inline bool dm_emulate_zone_append(struct mapped_device *md)
182 if (blk_queue_is_zoned(md->queue))
183 return test_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
187 #define DM_TABLE_MAX_DEPTH 16
190 struct mapped_device *md;
191 enum dm_queue_mode type;
195 unsigned int counts[DM_TABLE_MAX_DEPTH]; /* in nodes */
196 sector_t *index[DM_TABLE_MAX_DEPTH];
198 unsigned int num_targets;
199 unsigned int num_allocated;
201 struct dm_target *targets;
203 struct target_type *immutable_target_type;
205 bool integrity_supported:1;
207 unsigned integrity_added:1;
210 * Indicates the rw permissions for the new logical device. This
211 * should be a combination of BLK_OPEN_READ and BLK_OPEN_WRITE.
215 /* a list of devices used by this table */
216 struct list_head devices;
217 struct rw_semaphore devices_lock;
219 /* events get handed up using this callback */
220 void (*event_fn)(void *data);
223 struct dm_md_mempools *mempools;
225 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
226 struct blk_crypto_profile *crypto_profile;
230 static inline struct dm_target *dm_table_get_target(struct dm_table *t,
233 BUG_ON(index >= t->num_targets);
234 return t->targets + index;
238 * One of these is allocated per clone bio.
240 #define DM_TIO_MAGIC 28714
241 struct dm_target_io {
242 unsigned short magic;
244 unsigned int target_bio_nr;
246 struct dm_target *ti;
247 unsigned int *len_ptr;
251 #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone))
252 #define DM_IO_BIO_OFFSET \
253 (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio))
260 DM_TIO_IS_DUPLICATE_BIO
263 static inline bool dm_tio_flagged(struct dm_target_io *tio, unsigned int bit)
265 return (tio->flags & (1U << bit)) != 0;
268 static inline void dm_tio_set_flag(struct dm_target_io *tio, unsigned int bit)
270 tio->flags |= (1U << bit);
273 static inline bool dm_tio_is_normal(struct dm_target_io *tio)
275 return (dm_tio_flagged(tio, DM_TIO_INSIDE_DM_IO) &&
276 !dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
280 * One of these is allocated per original bio.
281 * It contains the first clone used for that original.
283 #define DM_IO_MAGIC 19577
285 unsigned short magic;
288 unsigned long start_time;
291 struct dm_stats_aux stats_aux;
294 struct mapped_device *md;
296 /* The three fields represent mapped part of original bio */
297 struct bio *orig_bio;
298 unsigned int sector_offset; /* offset to end of orig_bio */
299 unsigned int sectors;
301 /* last member of dm_target_io is 'struct bio' */
302 struct dm_target_io tio;
314 static inline bool dm_io_flagged(struct dm_io *io, unsigned int bit)
316 return (io->flags & (1U << bit)) != 0;
319 static inline void dm_io_set_flag(struct dm_io *io, unsigned int bit)
321 io->flags |= (1U << bit);
324 void dm_io_rewind(struct dm_io *io, struct bio_set *bs);
326 static inline struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
328 return &container_of(kobj, struct dm_kobject_holder, kobj)->completion;
331 unsigned int __dm_get_module_param(unsigned int *module_param, unsigned int def, unsigned int max);
333 static inline bool dm_message_test_buffer_overflow(char *result, unsigned int maxlen)
335 return !maxlen || strlen(result) + 1 >= maxlen;
338 extern atomic_t dm_global_event_nr;
339 extern wait_queue_head_t dm_global_eventq;
340 void dm_issue_global_event(void);