13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */
24cc96131SMike Snitzer /*
34cc96131SMike Snitzer * Internal header file _only_ for device mapper core
44cc96131SMike Snitzer *
54cc96131SMike Snitzer * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
64cc96131SMike Snitzer *
74cc96131SMike Snitzer * This file is released under the LGPL.
84cc96131SMike Snitzer */
94cc96131SMike Snitzer
104cc96131SMike Snitzer #ifndef DM_CORE_INTERNAL_H
114cc96131SMike Snitzer #define DM_CORE_INTERNAL_H
124cc96131SMike Snitzer
134cc96131SMike Snitzer #include <linux/kthread.h>
144cc96131SMike Snitzer #include <linux/ktime.h>
154cc96131SMike Snitzer #include <linux/blk-mq.h>
161e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
17442761fdSMike Snitzer #include <linux/jump_label.h>
184cc96131SMike Snitzer
194cc96131SMike Snitzer #include <trace/events/block.h>
204cc96131SMike Snitzer
214cc96131SMike Snitzer #include "dm.h"
2291ccbbacSTushar Sugandhi #include "dm-ima.h"
234cc96131SMike Snitzer
244cc96131SMike Snitzer #define DM_RESERVED_MAX_IOS 1024
25*438d1949SMikulas Patocka #define DM_MAX_TARGETS 1048576
26*438d1949SMikulas Patocka #define DM_MAX_TARGET_PARAMS 1024
274cc96131SMike Snitzer
288b211aacSMing Lei struct dm_io;
298b211aacSMing Lei
304cc96131SMike Snitzer struct dm_kobject_holder {
314cc96131SMike Snitzer struct kobject kobj;
324cc96131SMike Snitzer struct completion completion;
334cc96131SMike Snitzer };
344cc96131SMike Snitzer
354cc96131SMike Snitzer /*
3633bd6f06SMike Snitzer * DM core internal structures used directly by dm.c, dm-rq.c and dm-table.c.
3733bd6f06SMike Snitzer * DM targets must _not_ deference a mapped_device or dm_table to directly
3833bd6f06SMike Snitzer * access their members!
394cc96131SMike Snitzer */
4033bd6f06SMike Snitzer
4129dec90aSChristoph Hellwig /*
4229dec90aSChristoph Hellwig * For mempools pre-allocation at the table loading time.
4329dec90aSChristoph Hellwig */
4429dec90aSChristoph Hellwig struct dm_md_mempools {
4529dec90aSChristoph Hellwig struct bio_set bs;
4629dec90aSChristoph Hellwig struct bio_set io_bs;
4729dec90aSChristoph Hellwig };
4829dec90aSChristoph Hellwig
494cc96131SMike Snitzer struct mapped_device {
504cc96131SMike Snitzer struct mutex suspend_lock;
514cc96131SMike Snitzer
5272d711c8SMike Snitzer struct mutex table_devices_lock;
5372d711c8SMike Snitzer struct list_head table_devices;
5472d711c8SMike Snitzer
554cc96131SMike Snitzer /*
564cc96131SMike Snitzer * The current mapping (struct dm_table *).
574cc96131SMike Snitzer * Use dm_get_live_table{_fast} or take suspend_lock for
584cc96131SMike Snitzer * dereference.
594cc96131SMike Snitzer */
604cc96131SMike Snitzer void __rcu *map;
614cc96131SMike Snitzer
624cc96131SMike Snitzer unsigned long flags;
634cc96131SMike Snitzer
644cc96131SMike Snitzer /* Protect queue and type against concurrent access. */
654cc96131SMike Snitzer struct mutex type_lock;
6672d711c8SMike Snitzer enum dm_queue_mode type;
6772d711c8SMike Snitzer
6872d711c8SMike Snitzer int numa_node_id;
6972d711c8SMike Snitzer struct request_queue *queue;
704cc96131SMike Snitzer
714cc96131SMike Snitzer atomic_t holders;
724cc96131SMike Snitzer atomic_t open_count;
734cc96131SMike Snitzer
744cc96131SMike Snitzer struct dm_target *immutable_target;
754cc96131SMike Snitzer struct target_type *immutable_target_type;
764cc96131SMike Snitzer
7772d711c8SMike Snitzer char name[16];
784cc96131SMike Snitzer struct gendisk *disk;
79f26c5719SDan Williams struct dax_device *dax_dev;
804cc96131SMike Snitzer
81205649d8SMike Snitzer wait_queue_head_t wait;
829f6dc633SMike Snitzer unsigned long __percpu *pending_io;
839f6dc633SMike Snitzer
84205649d8SMike Snitzer /* forced geometry settings */
85205649d8SMike Snitzer struct hd_geometry geometry;
86205649d8SMike Snitzer
87205649d8SMike Snitzer /*
88205649d8SMike Snitzer * Processing queue (flush)
89205649d8SMike Snitzer */
90205649d8SMike Snitzer struct workqueue_struct *wq;
91205649d8SMike Snitzer
924cc96131SMike Snitzer /*
934cc96131SMike Snitzer * A list of ios that arrived while we were suspended.
944cc96131SMike Snitzer */
954cc96131SMike Snitzer struct work_struct work;
964cc96131SMike Snitzer spinlock_t deferred_lock;
974cc96131SMike Snitzer struct bio_list deferred;
984cc96131SMike Snitzer
998b211aacSMing Lei /*
1008b211aacSMing Lei * requeue work context is needed for cloning one new bio
1018b211aacSMing Lei * to represent the dm_io to be requeued, since each
1028b211aacSMing Lei * dm_io may point to the original bio from FS.
1038b211aacSMing Lei */
1048b211aacSMing Lei struct work_struct requeue_work;
1058b211aacSMing Lei struct dm_io *requeue_list;
1068b211aacSMing Lei
10772d711c8SMike Snitzer void *interface_ptr;
10872d711c8SMike Snitzer
1094cc96131SMike Snitzer /*
1104cc96131SMike Snitzer * Event handling.
1114cc96131SMike Snitzer */
1124cc96131SMike Snitzer wait_queue_head_t eventq;
1134cc96131SMike Snitzer atomic_t event_nr;
1144cc96131SMike Snitzer atomic_t uevent_seq;
1154cc96131SMike Snitzer struct list_head uevent_list;
1164cc96131SMike Snitzer spinlock_t uevent_lock; /* Protect access to uevent_list */
1174cc96131SMike Snitzer
118205649d8SMike Snitzer /* for blk-mq request-based DM support */
119205649d8SMike Snitzer bool init_tio_pdu:1;
120205649d8SMike Snitzer struct blk_mq_tag_set *tag_set;
121205649d8SMike Snitzer
122205649d8SMike Snitzer struct dm_stats stats;
123205649d8SMike Snitzer
1244cc96131SMike Snitzer /* the number of internal suspends */
12586a3238cSHeinz Mauelshagen unsigned int internal_suspend_count;
1264cc96131SMike Snitzer
127205649d8SMike Snitzer int swap_bios;
128205649d8SMike Snitzer struct semaphore swap_bios_semaphore;
129205649d8SMike Snitzer struct mutex swap_bios_lock;
130205649d8SMike Snitzer
1314cc96131SMike Snitzer /*
1324cc96131SMike Snitzer * io objects are allocated from here.
1334cc96131SMike Snitzer */
13429dec90aSChristoph Hellwig struct dm_md_mempools *mempools;
1354cc96131SMike Snitzer
1364cc96131SMike Snitzer /* kobject and completion */
1374cc96131SMike Snitzer struct dm_kobject_holder kobj_holder;
1384cc96131SMike Snitzer
139856eb091SMikulas Patocka struct srcu_struct io_barrier;
140bb37d772SDamien Le Moal
141bb37d772SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
142bb37d772SDamien Le Moal unsigned int nr_zones;
143bb37d772SDamien Le Moal unsigned int *zwp_offset;
144bb37d772SDamien Le Moal #endif
14591ccbbacSTushar Sugandhi
14691ccbbacSTushar Sugandhi #ifdef CONFIG_IMA
14791ccbbacSTushar Sugandhi struct dm_ima_measurements ima;
14891ccbbacSTushar Sugandhi #endif
1494cc96131SMike Snitzer };
1504cc96131SMike Snitzer
151e2118b3cSDamien Le Moal /*
152e2118b3cSDamien Le Moal * Bits for the flags field of struct mapped_device.
153e2118b3cSDamien Le Moal */
154e2118b3cSDamien Le Moal #define DMF_BLOCK_IO_FOR_SUSPEND 0
155e2118b3cSDamien Le Moal #define DMF_SUSPENDED 1
156e2118b3cSDamien Le Moal #define DMF_FROZEN 2
157e2118b3cSDamien Le Moal #define DMF_FREEING 3
158e2118b3cSDamien Le Moal #define DMF_DELETING 4
159e2118b3cSDamien Le Moal #define DMF_NOFLUSH_SUSPENDING 5
160e2118b3cSDamien Le Moal #define DMF_DEFERRED_REMOVE 6
161e2118b3cSDamien Le Moal #define DMF_SUSPENDED_INTERNALLY 7
162e2118b3cSDamien Le Moal #define DMF_POST_SUSPENDING 8
163bb37d772SDamien Le Moal #define DMF_EMULATE_ZONE_APPEND 9
164e2118b3cSDamien Le Moal
165bcb44433SMike Snitzer void disable_discard(struct mapped_device *md);
166ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md);
1674cc96131SMike Snitzer
dm_get_size(struct mapped_device * md)16833bd6f06SMike Snitzer static inline sector_t dm_get_size(struct mapped_device *md)
16933bd6f06SMike Snitzer {
17033bd6f06SMike Snitzer return get_capacity(md->disk);
17133bd6f06SMike Snitzer }
17233bd6f06SMike Snitzer
dm_get_stats(struct mapped_device * md)17333bd6f06SMike Snitzer static inline struct dm_stats *dm_get_stats(struct mapped_device *md)
17433bd6f06SMike Snitzer {
17533bd6f06SMike Snitzer return &md->stats;
17633bd6f06SMike Snitzer }
17733bd6f06SMike Snitzer
178442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(stats_enabled);
179442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(swap_bios_enabled);
180442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(zoned_enabled);
181442761fdSMike Snitzer
dm_emulate_zone_append(struct mapped_device * md)182bb37d772SDamien Le Moal static inline bool dm_emulate_zone_append(struct mapped_device *md)
183bb37d772SDamien Le Moal {
184bb37d772SDamien Le Moal if (blk_queue_is_zoned(md->queue))
185bb37d772SDamien Le Moal return test_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
186bb37d772SDamien Le Moal return false;
187bb37d772SDamien Le Moal }
188bb37d772SDamien Le Moal
18933bd6f06SMike Snitzer #define DM_TABLE_MAX_DEPTH 16
19033bd6f06SMike Snitzer
19133bd6f06SMike Snitzer struct dm_table {
19233bd6f06SMike Snitzer struct mapped_device *md;
19333bd6f06SMike Snitzer enum dm_queue_mode type;
19433bd6f06SMike Snitzer
19533bd6f06SMike Snitzer /* btree table */
19633bd6f06SMike Snitzer unsigned int depth;
19733bd6f06SMike Snitzer unsigned int counts[DM_TABLE_MAX_DEPTH]; /* in nodes */
19833bd6f06SMike Snitzer sector_t *index[DM_TABLE_MAX_DEPTH];
19933bd6f06SMike Snitzer
20033bd6f06SMike Snitzer unsigned int num_targets;
20133bd6f06SMike Snitzer unsigned int num_allocated;
20233bd6f06SMike Snitzer sector_t *highs;
20333bd6f06SMike Snitzer struct dm_target *targets;
20433bd6f06SMike Snitzer
20533bd6f06SMike Snitzer struct target_type *immutable_target_type;
20633bd6f06SMike Snitzer
20733bd6f06SMike Snitzer bool integrity_supported:1;
20833bd6f06SMike Snitzer bool singleton:1;
20933bd6f06SMike Snitzer unsigned integrity_added:1;
21033bd6f06SMike Snitzer
21133bd6f06SMike Snitzer /*
21205bdb996SChristoph Hellwig * Indicates the rw permissions for the new logical device. This
21305bdb996SChristoph Hellwig * should be a combination of BLK_OPEN_READ and BLK_OPEN_WRITE.
21433bd6f06SMike Snitzer */
21505bdb996SChristoph Hellwig blk_mode_t mode;
21633bd6f06SMike Snitzer
21733bd6f06SMike Snitzer /* a list of devices used by this table */
21833bd6f06SMike Snitzer struct list_head devices;
219f6007dceSMikulas Patocka struct rw_semaphore devices_lock;
22033bd6f06SMike Snitzer
22133bd6f06SMike Snitzer /* events get handed up using this callback */
22202f10ba1SHeinz Mauelshagen void (*event_fn)(void *data);
22333bd6f06SMike Snitzer void *event_context;
22433bd6f06SMike Snitzer
22533bd6f06SMike Snitzer struct dm_md_mempools *mempools;
226aa6ce87aSSatya Tangirala
227aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
228cb77cb5aSEric Biggers struct blk_crypto_profile *crypto_profile;
229aa6ce87aSSatya Tangirala #endif
23033bd6f06SMike Snitzer };
23133bd6f06SMike Snitzer
dm_table_get_target(struct dm_table * t,unsigned int index)232564b5c54SMike Snitzer static inline struct dm_target *dm_table_get_target(struct dm_table *t,
233564b5c54SMike Snitzer unsigned int index)
234564b5c54SMike Snitzer {
235564b5c54SMike Snitzer BUG_ON(index >= t->num_targets);
236564b5c54SMike Snitzer return t->targets + index;
237564b5c54SMike Snitzer }
238564b5c54SMike Snitzer
239e2118b3cSDamien Le Moal /*
240e2118b3cSDamien Le Moal * One of these is allocated per clone bio.
241e2118b3cSDamien Le Moal */
242bd4a6dd2SMike Snitzer #define DM_TIO_MAGIC 28714
243e2118b3cSDamien Le Moal struct dm_target_io {
244bd4a6dd2SMike Snitzer unsigned short magic;
245aad5b23eSMikulas Patocka blk_short_t flags;
246300432f5SMike Snitzer unsigned int target_bio_nr;
247e2118b3cSDamien Le Moal struct dm_io *io;
248e2118b3cSDamien Le Moal struct dm_target *ti;
249e2118b3cSDamien Le Moal unsigned int *len_ptr;
250743598f0SMike Snitzer sector_t old_sector;
251e2118b3cSDamien Le Moal struct bio clone;
252e2118b3cSDamien Le Moal };
253e810cb78SChristoph Hellwig #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone))
254e810cb78SChristoph Hellwig #define DM_IO_BIO_OFFSET \
255e810cb78SChristoph Hellwig (offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio))
256e2118b3cSDamien Le Moal
257e2118b3cSDamien Le Moal /*
258655f3aadSMike Snitzer * dm_target_io flags
259655f3aadSMike Snitzer */
260655f3aadSMike Snitzer enum {
261655f3aadSMike Snitzer DM_TIO_INSIDE_DM_IO,
262655f3aadSMike Snitzer DM_TIO_IS_DUPLICATE_BIO
263655f3aadSMike Snitzer };
264655f3aadSMike Snitzer
dm_tio_flagged(struct dm_target_io * tio,unsigned int bit)265655f3aadSMike Snitzer static inline bool dm_tio_flagged(struct dm_target_io *tio, unsigned int bit)
266655f3aadSMike Snitzer {
267655f3aadSMike Snitzer return (tio->flags & (1U << bit)) != 0;
268655f3aadSMike Snitzer }
269655f3aadSMike Snitzer
dm_tio_set_flag(struct dm_target_io * tio,unsigned int bit)270655f3aadSMike Snitzer static inline void dm_tio_set_flag(struct dm_target_io *tio, unsigned int bit)
271655f3aadSMike Snitzer {
272655f3aadSMike Snitzer tio->flags |= (1U << bit);
273655f3aadSMike Snitzer }
274655f3aadSMike Snitzer
dm_tio_is_normal(struct dm_target_io * tio)2753b03f7c1SMike Snitzer static inline bool dm_tio_is_normal(struct dm_target_io *tio)
2763b03f7c1SMike Snitzer {
2773b03f7c1SMike Snitzer return (dm_tio_flagged(tio, DM_TIO_INSIDE_DM_IO) &&
2783b03f7c1SMike Snitzer !dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
2793b03f7c1SMike Snitzer }
2803b03f7c1SMike Snitzer
281655f3aadSMike Snitzer /*
282e2118b3cSDamien Le Moal * One of these is allocated per original bio.
283e2118b3cSDamien Le Moal * It contains the first clone used for that original.
284e2118b3cSDamien Le Moal */
285bd4a6dd2SMike Snitzer #define DM_IO_MAGIC 19577
286e2118b3cSDamien Le Moal struct dm_io {
287bd4a6dd2SMike Snitzer unsigned short magic;
2889d20653fSMike Snitzer blk_short_t flags;
2894d7bca13SMike Snitzer spinlock_t lock;
290e2118b3cSDamien Le Moal unsigned long start_time;
291b99fdcdcSMing Lei void *data;
292ec211631SMing Lei struct dm_io *next;
293e2118b3cSDamien Le Moal struct dm_stats_aux stats_aux;
294982b48aeSMike Snitzer blk_status_t status;
295982b48aeSMike Snitzer atomic_t io_count;
296982b48aeSMike Snitzer struct mapped_device *md;
2977dd76d1fSMing Lei
2987dd76d1fSMing Lei /* The three fields represent mapped part of original bio */
299982b48aeSMike Snitzer struct bio *orig_bio;
3007dd76d1fSMing Lei unsigned int sector_offset; /* offset to end of orig_bio */
3017dd76d1fSMing Lei unsigned int sectors;
3027dd76d1fSMing Lei
303e2118b3cSDamien Le Moal /* last member of dm_target_io is 'struct bio' */
304e2118b3cSDamien Le Moal struct dm_target_io tio;
305e2118b3cSDamien Le Moal };
306e2118b3cSDamien Le Moal
30782f6cdccSMike Snitzer /*
30882f6cdccSMike Snitzer * dm_io flags
30982f6cdccSMike Snitzer */
31082f6cdccSMike Snitzer enum {
3117dd76d1fSMing Lei DM_IO_ACCOUNTED,
312526d1006SLi Nan DM_IO_WAS_SPLIT,
313526d1006SLi Nan DM_IO_BLK_STAT
31482f6cdccSMike Snitzer };
31582f6cdccSMike Snitzer
dm_io_flagged(struct dm_io * io,unsigned int bit)31682f6cdccSMike Snitzer static inline bool dm_io_flagged(struct dm_io *io, unsigned int bit)
31782f6cdccSMike Snitzer {
31882f6cdccSMike Snitzer return (io->flags & (1U << bit)) != 0;
31982f6cdccSMike Snitzer }
32082f6cdccSMike Snitzer
dm_io_set_flag(struct dm_io * io,unsigned int bit)32182f6cdccSMike Snitzer static inline void dm_io_set_flag(struct dm_io *io, unsigned int bit)
32282f6cdccSMike Snitzer {
32382f6cdccSMike Snitzer io->flags |= (1U << bit);
32482f6cdccSMike Snitzer }
32582f6cdccSMike Snitzer
3268b211aacSMing Lei void dm_io_rewind(struct dm_io *io, struct bio_set *bs);
3278b211aacSMing Lei
dm_get_completion_from_kobject(struct kobject * kobj)3284cc96131SMike Snitzer static inline struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
3294cc96131SMike Snitzer {
3304cc96131SMike Snitzer return &container_of(kobj, struct dm_kobject_holder, kobj)->completion;
3314cc96131SMike Snitzer }
3324cc96131SMike Snitzer
33386a3238cSHeinz Mauelshagen unsigned int __dm_get_module_param(unsigned int *module_param, unsigned int def, unsigned int max);
3344cc96131SMike Snitzer
dm_message_test_buffer_overflow(char * result,unsigned int maxlen)33586a3238cSHeinz Mauelshagen static inline bool dm_message_test_buffer_overflow(char *result, unsigned int maxlen)
3364cc96131SMike Snitzer {
3374cc96131SMike Snitzer return !maxlen || strlen(result) + 1 >= maxlen;
3384cc96131SMike Snitzer }
3394cc96131SMike Snitzer
34093e6442cSMikulas Patocka extern atomic_t dm_global_event_nr;
34193e6442cSMikulas Patocka extern wait_queue_head_t dm_global_eventq;
34262e08243SMikulas Patocka void dm_issue_global_event(void);
34393e6442cSMikulas Patocka
3444cc96131SMike Snitzer #endif
345