xref: /openbmc/linux/drivers/md/dm-core.h (revision 8b211aaccb915bbf4f4a68f1910c4de701df393b)
14cc96131SMike Snitzer /*
24cc96131SMike Snitzer  * Internal header file _only_ for device mapper core
34cc96131SMike Snitzer  *
44cc96131SMike Snitzer  * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
54cc96131SMike Snitzer  *
64cc96131SMike Snitzer  * This file is released under the LGPL.
74cc96131SMike Snitzer  */
84cc96131SMike Snitzer 
94cc96131SMike Snitzer #ifndef DM_CORE_INTERNAL_H
104cc96131SMike Snitzer #define DM_CORE_INTERNAL_H
114cc96131SMike Snitzer 
124cc96131SMike Snitzer #include <linux/kthread.h>
134cc96131SMike Snitzer #include <linux/ktime.h>
144cc96131SMike Snitzer #include <linux/blk-mq.h>
151e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
16442761fdSMike Snitzer #include <linux/jump_label.h>
174cc96131SMike Snitzer 
184cc96131SMike Snitzer #include <trace/events/block.h>
194cc96131SMike Snitzer 
204cc96131SMike Snitzer #include "dm.h"
2191ccbbacSTushar Sugandhi #include "dm-ima.h"
224cc96131SMike Snitzer 
234cc96131SMike Snitzer #define DM_RESERVED_MAX_IOS		1024
244cc96131SMike Snitzer 
25*8b211aacSMing Lei struct dm_io;
26*8b211aacSMing Lei 
274cc96131SMike Snitzer struct dm_kobject_holder {
284cc96131SMike Snitzer 	struct kobject kobj;
294cc96131SMike Snitzer 	struct completion completion;
304cc96131SMike Snitzer };
314cc96131SMike Snitzer 
324cc96131SMike Snitzer /*
3333bd6f06SMike Snitzer  * DM core internal structures used directly by dm.c, dm-rq.c and dm-table.c.
3433bd6f06SMike Snitzer  * DM targets must _not_ deference a mapped_device or dm_table to directly
3533bd6f06SMike Snitzer  * access their members!
364cc96131SMike Snitzer  */
3733bd6f06SMike Snitzer 
3829dec90aSChristoph Hellwig /*
3929dec90aSChristoph Hellwig  * For mempools pre-allocation at the table loading time.
4029dec90aSChristoph Hellwig  */
4129dec90aSChristoph Hellwig struct dm_md_mempools {
4229dec90aSChristoph Hellwig 	struct bio_set bs;
4329dec90aSChristoph Hellwig 	struct bio_set io_bs;
4429dec90aSChristoph Hellwig };
4529dec90aSChristoph Hellwig 
464cc96131SMike Snitzer struct mapped_device {
474cc96131SMike Snitzer 	struct mutex suspend_lock;
484cc96131SMike Snitzer 
4972d711c8SMike Snitzer 	struct mutex table_devices_lock;
5072d711c8SMike Snitzer 	struct list_head table_devices;
5172d711c8SMike Snitzer 
524cc96131SMike Snitzer 	/*
534cc96131SMike Snitzer 	 * The current mapping (struct dm_table *).
544cc96131SMike Snitzer 	 * Use dm_get_live_table{_fast} or take suspend_lock for
554cc96131SMike Snitzer 	 * dereference.
564cc96131SMike Snitzer 	 */
574cc96131SMike Snitzer 	void __rcu *map;
584cc96131SMike Snitzer 
594cc96131SMike Snitzer 	unsigned long flags;
604cc96131SMike Snitzer 
614cc96131SMike Snitzer 	/* Protect queue and type against concurrent access. */
624cc96131SMike Snitzer 	struct mutex type_lock;
6372d711c8SMike Snitzer 	enum dm_queue_mode type;
6472d711c8SMike Snitzer 
6572d711c8SMike Snitzer 	int numa_node_id;
6672d711c8SMike Snitzer 	struct request_queue *queue;
674cc96131SMike Snitzer 
684cc96131SMike Snitzer 	atomic_t holders;
694cc96131SMike Snitzer 	atomic_t open_count;
704cc96131SMike Snitzer 
714cc96131SMike Snitzer 	struct dm_target *immutable_target;
724cc96131SMike Snitzer 	struct target_type *immutable_target_type;
734cc96131SMike Snitzer 
7472d711c8SMike Snitzer 	char name[16];
754cc96131SMike Snitzer 	struct gendisk *disk;
76f26c5719SDan Williams 	struct dax_device *dax_dev;
774cc96131SMike Snitzer 
78205649d8SMike Snitzer 	wait_queue_head_t wait;
799f6dc633SMike Snitzer 	unsigned long __percpu *pending_io;
809f6dc633SMike Snitzer 
81205649d8SMike Snitzer 	/* forced geometry settings */
82205649d8SMike Snitzer 	struct hd_geometry geometry;
83205649d8SMike Snitzer 
84205649d8SMike Snitzer 	/*
85205649d8SMike Snitzer 	 * Processing queue (flush)
86205649d8SMike Snitzer 	 */
87205649d8SMike Snitzer 	struct workqueue_struct *wq;
88205649d8SMike Snitzer 
894cc96131SMike Snitzer 	/*
904cc96131SMike Snitzer 	 * A list of ios that arrived while we were suspended.
914cc96131SMike Snitzer 	 */
924cc96131SMike Snitzer 	struct work_struct work;
934cc96131SMike Snitzer 	spinlock_t deferred_lock;
944cc96131SMike Snitzer 	struct bio_list deferred;
954cc96131SMike Snitzer 
96*8b211aacSMing Lei 	/*
97*8b211aacSMing Lei 	 * requeue work context is needed for cloning one new bio
98*8b211aacSMing Lei 	 * to represent the dm_io to be requeued, since each
99*8b211aacSMing Lei 	 * dm_io may point to the original bio from FS.
100*8b211aacSMing Lei 	 */
101*8b211aacSMing Lei 	struct work_struct requeue_work;
102*8b211aacSMing Lei 	struct dm_io *requeue_list;
103*8b211aacSMing Lei 
10472d711c8SMike Snitzer 	void *interface_ptr;
10572d711c8SMike Snitzer 
1064cc96131SMike Snitzer 	/*
1074cc96131SMike Snitzer 	 * Event handling.
1084cc96131SMike Snitzer 	 */
1094cc96131SMike Snitzer 	wait_queue_head_t eventq;
1104cc96131SMike Snitzer 	atomic_t event_nr;
1114cc96131SMike Snitzer 	atomic_t uevent_seq;
1124cc96131SMike Snitzer 	struct list_head uevent_list;
1134cc96131SMike Snitzer 	spinlock_t uevent_lock; /* Protect access to uevent_list */
1144cc96131SMike Snitzer 
115205649d8SMike Snitzer 	/* for blk-mq request-based DM support */
116205649d8SMike Snitzer 	bool init_tio_pdu:1;
117205649d8SMike Snitzer 	struct blk_mq_tag_set *tag_set;
118205649d8SMike Snitzer 
119205649d8SMike Snitzer 	struct dm_stats stats;
120205649d8SMike Snitzer 
1214cc96131SMike Snitzer 	/* the number of internal suspends */
1224cc96131SMike Snitzer 	unsigned internal_suspend_count;
1234cc96131SMike Snitzer 
124205649d8SMike Snitzer 	int swap_bios;
125205649d8SMike Snitzer 	struct semaphore swap_bios_semaphore;
126205649d8SMike Snitzer 	struct mutex swap_bios_lock;
127205649d8SMike Snitzer 
1284cc96131SMike Snitzer 	/*
1294cc96131SMike Snitzer 	 * io objects are allocated from here.
1304cc96131SMike Snitzer 	 */
13129dec90aSChristoph Hellwig 	struct dm_md_mempools *mempools;
1324cc96131SMike Snitzer 
1334cc96131SMike Snitzer 	/* kobject and completion */
1344cc96131SMike Snitzer 	struct dm_kobject_holder kobj_holder;
1354cc96131SMike Snitzer 
136856eb091SMikulas Patocka 	struct srcu_struct io_barrier;
137bb37d772SDamien Le Moal 
138bb37d772SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
139bb37d772SDamien Le Moal 	unsigned int nr_zones;
140bb37d772SDamien Le Moal 	unsigned int *zwp_offset;
141bb37d772SDamien Le Moal #endif
14291ccbbacSTushar Sugandhi 
14391ccbbacSTushar Sugandhi #ifdef CONFIG_IMA
14491ccbbacSTushar Sugandhi 	struct dm_ima_measurements ima;
14591ccbbacSTushar Sugandhi #endif
1464cc96131SMike Snitzer };
1474cc96131SMike Snitzer 
148e2118b3cSDamien Le Moal /*
149e2118b3cSDamien Le Moal  * Bits for the flags field of struct mapped_device.
150e2118b3cSDamien Le Moal  */
151e2118b3cSDamien Le Moal #define DMF_BLOCK_IO_FOR_SUSPEND 0
152e2118b3cSDamien Le Moal #define DMF_SUSPENDED 1
153e2118b3cSDamien Le Moal #define DMF_FROZEN 2
154e2118b3cSDamien Le Moal #define DMF_FREEING 3
155e2118b3cSDamien Le Moal #define DMF_DELETING 4
156e2118b3cSDamien Le Moal #define DMF_NOFLUSH_SUSPENDING 5
157e2118b3cSDamien Le Moal #define DMF_DEFERRED_REMOVE 6
158e2118b3cSDamien Le Moal #define DMF_SUSPENDED_INTERNALLY 7
159e2118b3cSDamien Le Moal #define DMF_POST_SUSPENDING 8
160bb37d772SDamien Le Moal #define DMF_EMULATE_ZONE_APPEND 9
161e2118b3cSDamien Le Moal 
162bcb44433SMike Snitzer void disable_discard(struct mapped_device *md);
163ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md);
1644cc96131SMike Snitzer 
16533bd6f06SMike Snitzer static inline sector_t dm_get_size(struct mapped_device *md)
16633bd6f06SMike Snitzer {
16733bd6f06SMike Snitzer 	return get_capacity(md->disk);
16833bd6f06SMike Snitzer }
16933bd6f06SMike Snitzer 
17033bd6f06SMike Snitzer static inline struct dm_stats *dm_get_stats(struct mapped_device *md)
17133bd6f06SMike Snitzer {
17233bd6f06SMike Snitzer 	return &md->stats;
17333bd6f06SMike Snitzer }
17433bd6f06SMike Snitzer 
175442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(stats_enabled);
176442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(swap_bios_enabled);
177442761fdSMike Snitzer DECLARE_STATIC_KEY_FALSE(zoned_enabled);
178442761fdSMike Snitzer 
179bb37d772SDamien Le Moal static inline bool dm_emulate_zone_append(struct mapped_device *md)
180bb37d772SDamien Le Moal {
181bb37d772SDamien Le Moal 	if (blk_queue_is_zoned(md->queue))
182bb37d772SDamien Le Moal 		return test_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
183bb37d772SDamien Le Moal 	return false;
184bb37d772SDamien Le Moal }
185bb37d772SDamien Le Moal 
18633bd6f06SMike Snitzer #define DM_TABLE_MAX_DEPTH 16
18733bd6f06SMike Snitzer 
18833bd6f06SMike Snitzer struct dm_table {
18933bd6f06SMike Snitzer 	struct mapped_device *md;
19033bd6f06SMike Snitzer 	enum dm_queue_mode type;
19133bd6f06SMike Snitzer 
19233bd6f06SMike Snitzer 	/* btree table */
19333bd6f06SMike Snitzer 	unsigned int depth;
19433bd6f06SMike Snitzer 	unsigned int counts[DM_TABLE_MAX_DEPTH]; /* in nodes */
19533bd6f06SMike Snitzer 	sector_t *index[DM_TABLE_MAX_DEPTH];
19633bd6f06SMike Snitzer 
19733bd6f06SMike Snitzer 	unsigned int num_targets;
19833bd6f06SMike Snitzer 	unsigned int num_allocated;
19933bd6f06SMike Snitzer 	sector_t *highs;
20033bd6f06SMike Snitzer 	struct dm_target *targets;
20133bd6f06SMike Snitzer 
20233bd6f06SMike Snitzer 	struct target_type *immutable_target_type;
20333bd6f06SMike Snitzer 
20433bd6f06SMike Snitzer 	bool integrity_supported:1;
20533bd6f06SMike Snitzer 	bool singleton:1;
20633bd6f06SMike Snitzer 	unsigned integrity_added:1;
20733bd6f06SMike Snitzer 
20833bd6f06SMike Snitzer 	/*
20933bd6f06SMike Snitzer 	 * Indicates the rw permissions for the new logical
21033bd6f06SMike Snitzer 	 * device.  This should be a combination of FMODE_READ
21133bd6f06SMike Snitzer 	 * and FMODE_WRITE.
21233bd6f06SMike Snitzer 	 */
21333bd6f06SMike Snitzer 	fmode_t mode;
21433bd6f06SMike Snitzer 
21533bd6f06SMike Snitzer 	/* a list of devices used by this table */
21633bd6f06SMike Snitzer 	struct list_head devices;
21733bd6f06SMike Snitzer 
21833bd6f06SMike Snitzer 	/* events get handed up using this callback */
21933bd6f06SMike Snitzer 	void (*event_fn)(void *);
22033bd6f06SMike Snitzer 	void *event_context;
22133bd6f06SMike Snitzer 
22233bd6f06SMike Snitzer 	struct dm_md_mempools *mempools;
223aa6ce87aSSatya Tangirala 
224aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
225cb77cb5aSEric Biggers 	struct blk_crypto_profile *crypto_profile;
226aa6ce87aSSatya Tangirala #endif
22733bd6f06SMike Snitzer };
22833bd6f06SMike Snitzer 
229e2118b3cSDamien Le Moal /*
230e2118b3cSDamien Le Moal  * One of these is allocated per clone bio.
231e2118b3cSDamien Le Moal  */
232bd4a6dd2SMike Snitzer #define DM_TIO_MAGIC 28714
233e2118b3cSDamien Le Moal struct dm_target_io {
234bd4a6dd2SMike Snitzer 	unsigned short magic;
235aad5b23eSMikulas Patocka 	blk_short_t flags;
236300432f5SMike Snitzer 	unsigned int target_bio_nr;
237e2118b3cSDamien Le Moal 	struct dm_io *io;
238e2118b3cSDamien Le Moal 	struct dm_target *ti;
239e2118b3cSDamien Le Moal 	unsigned int *len_ptr;
240743598f0SMike Snitzer 	sector_t old_sector;
241e2118b3cSDamien Le Moal 	struct bio clone;
242e2118b3cSDamien Le Moal };
243e810cb78SChristoph Hellwig #define DM_TARGET_IO_BIO_OFFSET (offsetof(struct dm_target_io, clone))
244e810cb78SChristoph Hellwig #define DM_IO_BIO_OFFSET \
245e810cb78SChristoph Hellwig 	(offsetof(struct dm_target_io, clone) + offsetof(struct dm_io, tio))
246e2118b3cSDamien Le Moal 
247e2118b3cSDamien Le Moal /*
248655f3aadSMike Snitzer  * dm_target_io flags
249655f3aadSMike Snitzer  */
250655f3aadSMike Snitzer enum {
251655f3aadSMike Snitzer 	DM_TIO_INSIDE_DM_IO,
252655f3aadSMike Snitzer 	DM_TIO_IS_DUPLICATE_BIO
253655f3aadSMike Snitzer };
254655f3aadSMike Snitzer 
255655f3aadSMike Snitzer static inline bool dm_tio_flagged(struct dm_target_io *tio, unsigned int bit)
256655f3aadSMike Snitzer {
257655f3aadSMike Snitzer 	return (tio->flags & (1U << bit)) != 0;
258655f3aadSMike Snitzer }
259655f3aadSMike Snitzer 
260655f3aadSMike Snitzer static inline void dm_tio_set_flag(struct dm_target_io *tio, unsigned int bit)
261655f3aadSMike Snitzer {
262655f3aadSMike Snitzer 	tio->flags |= (1U << bit);
263655f3aadSMike Snitzer }
264655f3aadSMike Snitzer 
2653b03f7c1SMike Snitzer static inline bool dm_tio_is_normal(struct dm_target_io *tio)
2663b03f7c1SMike Snitzer {
2673b03f7c1SMike Snitzer 	return (dm_tio_flagged(tio, DM_TIO_INSIDE_DM_IO) &&
2683b03f7c1SMike Snitzer 		!dm_tio_flagged(tio, DM_TIO_IS_DUPLICATE_BIO));
2693b03f7c1SMike Snitzer }
2703b03f7c1SMike Snitzer 
271655f3aadSMike Snitzer /*
272e2118b3cSDamien Le Moal  * One of these is allocated per original bio.
273e2118b3cSDamien Le Moal  * It contains the first clone used for that original.
274e2118b3cSDamien Le Moal  */
275bd4a6dd2SMike Snitzer #define DM_IO_MAGIC 19577
276e2118b3cSDamien Le Moal struct dm_io {
277bd4a6dd2SMike Snitzer 	unsigned short magic;
2789d20653fSMike Snitzer 	blk_short_t flags;
2794d7bca13SMike Snitzer 	spinlock_t lock;
280e2118b3cSDamien Le Moal 	unsigned long start_time;
281b99fdcdcSMing Lei 	void *data;
282ec211631SMing Lei 	struct dm_io *next;
283e2118b3cSDamien Le Moal 	struct dm_stats_aux stats_aux;
284982b48aeSMike Snitzer 	blk_status_t status;
285982b48aeSMike Snitzer 	atomic_t io_count;
286982b48aeSMike Snitzer 	struct mapped_device *md;
2877dd76d1fSMing Lei 
2887dd76d1fSMing Lei 	/* The three fields represent mapped part of original bio */
289982b48aeSMike Snitzer 	struct bio *orig_bio;
2907dd76d1fSMing Lei 	unsigned int sector_offset; /* offset to end of orig_bio */
2917dd76d1fSMing Lei 	unsigned int sectors;
2927dd76d1fSMing Lei 
293e2118b3cSDamien Le Moal 	/* last member of dm_target_io is 'struct bio' */
294e2118b3cSDamien Le Moal 	struct dm_target_io tio;
295e2118b3cSDamien Le Moal };
296e2118b3cSDamien Le Moal 
29782f6cdccSMike Snitzer /*
29882f6cdccSMike Snitzer  * dm_io flags
29982f6cdccSMike Snitzer  */
30082f6cdccSMike Snitzer enum {
3017dd76d1fSMing Lei 	DM_IO_ACCOUNTED,
3027dd76d1fSMing Lei 	DM_IO_WAS_SPLIT
30382f6cdccSMike Snitzer };
30482f6cdccSMike Snitzer 
30582f6cdccSMike Snitzer static inline bool dm_io_flagged(struct dm_io *io, unsigned int bit)
30682f6cdccSMike Snitzer {
30782f6cdccSMike Snitzer 	return (io->flags & (1U << bit)) != 0;
30882f6cdccSMike Snitzer }
30982f6cdccSMike Snitzer 
31082f6cdccSMike Snitzer static inline void dm_io_set_flag(struct dm_io *io, unsigned int bit)
31182f6cdccSMike Snitzer {
31282f6cdccSMike Snitzer 	io->flags |= (1U << bit);
31382f6cdccSMike Snitzer }
31482f6cdccSMike Snitzer 
315*8b211aacSMing Lei void dm_io_rewind(struct dm_io *io, struct bio_set *bs);
316*8b211aacSMing Lei 
3174cc96131SMike Snitzer static inline struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
3184cc96131SMike Snitzer {
3194cc96131SMike Snitzer 	return &container_of(kobj, struct dm_kobject_holder, kobj)->completion;
3204cc96131SMike Snitzer }
3214cc96131SMike Snitzer 
3224cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, unsigned def, unsigned max);
3234cc96131SMike Snitzer 
3244cc96131SMike Snitzer static inline bool dm_message_test_buffer_overflow(char *result, unsigned maxlen)
3254cc96131SMike Snitzer {
3264cc96131SMike Snitzer 	return !maxlen || strlen(result) + 1 >= maxlen;
3274cc96131SMike Snitzer }
3284cc96131SMike Snitzer 
32993e6442cSMikulas Patocka extern atomic_t dm_global_event_nr;
33093e6442cSMikulas Patocka extern wait_queue_head_t dm_global_eventq;
33162e08243SMikulas Patocka void dm_issue_global_event(void);
33293e6442cSMikulas Patocka 
3334cc96131SMike Snitzer #endif
334