xref: /openbmc/linux/drivers/md/dm-core.h (revision e2118b3c3d94289852417f70ec128c25f4833aad)
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>
1433bd6f06SMike Snitzer #include <linux/genhd.h>
154cc96131SMike Snitzer #include <linux/blk-mq.h>
16aa6ce87aSSatya Tangirala #include <linux/keyslot-manager.h>
174cc96131SMike Snitzer 
184cc96131SMike Snitzer #include <trace/events/block.h>
194cc96131SMike Snitzer 
204cc96131SMike Snitzer #include "dm.h"
214cc96131SMike Snitzer 
224cc96131SMike Snitzer #define DM_RESERVED_MAX_IOS		1024
234cc96131SMike Snitzer 
244cc96131SMike Snitzer struct dm_kobject_holder {
254cc96131SMike Snitzer 	struct kobject kobj;
264cc96131SMike Snitzer 	struct completion completion;
274cc96131SMike Snitzer };
284cc96131SMike Snitzer 
294cc96131SMike Snitzer /*
3033bd6f06SMike Snitzer  * DM core internal structures used directly by dm.c, dm-rq.c and dm-table.c.
3133bd6f06SMike Snitzer  * DM targets must _not_ deference a mapped_device or dm_table to directly
3233bd6f06SMike Snitzer  * access their members!
334cc96131SMike Snitzer  */
3433bd6f06SMike Snitzer 
354cc96131SMike Snitzer struct mapped_device {
364cc96131SMike Snitzer 	struct mutex suspend_lock;
374cc96131SMike Snitzer 
3872d711c8SMike Snitzer 	struct mutex table_devices_lock;
3972d711c8SMike Snitzer 	struct list_head table_devices;
4072d711c8SMike Snitzer 
414cc96131SMike Snitzer 	/*
424cc96131SMike Snitzer 	 * The current mapping (struct dm_table *).
434cc96131SMike Snitzer 	 * Use dm_get_live_table{_fast} or take suspend_lock for
444cc96131SMike Snitzer 	 * dereference.
454cc96131SMike Snitzer 	 */
464cc96131SMike Snitzer 	void __rcu *map;
474cc96131SMike Snitzer 
484cc96131SMike Snitzer 	unsigned long flags;
494cc96131SMike Snitzer 
504cc96131SMike Snitzer 	/* Protect queue and type against concurrent access. */
514cc96131SMike Snitzer 	struct mutex type_lock;
5272d711c8SMike Snitzer 	enum dm_queue_mode type;
5372d711c8SMike Snitzer 
5472d711c8SMike Snitzer 	int numa_node_id;
5572d711c8SMike Snitzer 	struct request_queue *queue;
564cc96131SMike Snitzer 
574cc96131SMike Snitzer 	atomic_t holders;
584cc96131SMike Snitzer 	atomic_t open_count;
594cc96131SMike Snitzer 
604cc96131SMike Snitzer 	struct dm_target *immutable_target;
614cc96131SMike Snitzer 	struct target_type *immutable_target_type;
624cc96131SMike Snitzer 
6372d711c8SMike Snitzer 	char name[16];
644cc96131SMike Snitzer 	struct gendisk *disk;
65f26c5719SDan Williams 	struct dax_device *dax_dev;
664cc96131SMike Snitzer 
674cc96131SMike Snitzer 	/*
684cc96131SMike Snitzer 	 * A list of ios that arrived while we were suspended.
694cc96131SMike Snitzer 	 */
704cc96131SMike Snitzer 	struct work_struct work;
7172d711c8SMike Snitzer 	wait_queue_head_t wait;
724cc96131SMike Snitzer 	spinlock_t deferred_lock;
734cc96131SMike Snitzer 	struct bio_list deferred;
744cc96131SMike Snitzer 
7572d711c8SMike Snitzer 	void *interface_ptr;
7672d711c8SMike Snitzer 
774cc96131SMike Snitzer 	/*
784cc96131SMike Snitzer 	 * Event handling.
794cc96131SMike Snitzer 	 */
804cc96131SMike Snitzer 	wait_queue_head_t eventq;
814cc96131SMike Snitzer 	atomic_t event_nr;
824cc96131SMike Snitzer 	atomic_t uevent_seq;
834cc96131SMike Snitzer 	struct list_head uevent_list;
844cc96131SMike Snitzer 	spinlock_t uevent_lock; /* Protect access to uevent_list */
854cc96131SMike Snitzer 
864cc96131SMike Snitzer 	/* the number of internal suspends */
874cc96131SMike Snitzer 	unsigned internal_suspend_count;
884cc96131SMike Snitzer 
894cc96131SMike Snitzer 	/*
904cc96131SMike Snitzer 	 * io objects are allocated from here.
914cc96131SMike Snitzer 	 */
926f1c819cSKent Overstreet 	struct bio_set io_bs;
936f1c819cSKent Overstreet 	struct bio_set bs;
944cc96131SMike Snitzer 
954cc96131SMike Snitzer 	/*
9672d711c8SMike Snitzer 	 * Processing queue (flush)
9772d711c8SMike Snitzer 	 */
9872d711c8SMike Snitzer 	struct workqueue_struct *wq;
9972d711c8SMike Snitzer 
1004cc96131SMike Snitzer 	/* forced geometry settings */
1014cc96131SMike Snitzer 	struct hd_geometry geometry;
1024cc96131SMike Snitzer 
1034cc96131SMike Snitzer 	/* kobject and completion */
1044cc96131SMike Snitzer 	struct dm_kobject_holder kobj_holder;
1054cc96131SMike Snitzer 
106a666e5c0SMikulas Patocka 	int swap_bios;
107a666e5c0SMikulas Patocka 	struct semaphore swap_bios_semaphore;
108a666e5c0SMikulas Patocka 	struct mutex swap_bios_lock;
109a666e5c0SMikulas Patocka 
1104cc96131SMike Snitzer 	struct dm_stats stats;
1114cc96131SMike Snitzer 
1124cc96131SMike Snitzer 	/* for blk-mq request-based DM support */
1134cc96131SMike Snitzer 	struct blk_mq_tag_set *tag_set;
1144cc96131SMike Snitzer 	bool init_tio_pdu:1;
115856eb091SMikulas Patocka 
116856eb091SMikulas Patocka 	struct srcu_struct io_barrier;
1174cc96131SMike Snitzer };
1184cc96131SMike Snitzer 
119*e2118b3cSDamien Le Moal /*
120*e2118b3cSDamien Le Moal  * Bits for the flags field of struct mapped_device.
121*e2118b3cSDamien Le Moal  */
122*e2118b3cSDamien Le Moal #define DMF_BLOCK_IO_FOR_SUSPEND 0
123*e2118b3cSDamien Le Moal #define DMF_SUSPENDED 1
124*e2118b3cSDamien Le Moal #define DMF_FROZEN 2
125*e2118b3cSDamien Le Moal #define DMF_FREEING 3
126*e2118b3cSDamien Le Moal #define DMF_DELETING 4
127*e2118b3cSDamien Le Moal #define DMF_NOFLUSH_SUSPENDING 5
128*e2118b3cSDamien Le Moal #define DMF_DEFERRED_REMOVE 6
129*e2118b3cSDamien Le Moal #define DMF_SUSPENDED_INTERNALLY 7
130*e2118b3cSDamien Le Moal #define DMF_POST_SUSPENDING 8
131*e2118b3cSDamien Le Moal 
132bcb44433SMike Snitzer void disable_discard(struct mapped_device *md);
1334cc96131SMike Snitzer void disable_write_same(struct mapped_device *md);
134ac62d620SChristoph Hellwig void disable_write_zeroes(struct mapped_device *md);
1354cc96131SMike Snitzer 
13633bd6f06SMike Snitzer static inline sector_t dm_get_size(struct mapped_device *md)
13733bd6f06SMike Snitzer {
13833bd6f06SMike Snitzer 	return get_capacity(md->disk);
13933bd6f06SMike Snitzer }
14033bd6f06SMike Snitzer 
14133bd6f06SMike Snitzer static inline struct dm_stats *dm_get_stats(struct mapped_device *md)
14233bd6f06SMike Snitzer {
14333bd6f06SMike Snitzer 	return &md->stats;
14433bd6f06SMike Snitzer }
14533bd6f06SMike Snitzer 
14633bd6f06SMike Snitzer #define DM_TABLE_MAX_DEPTH 16
14733bd6f06SMike Snitzer 
14833bd6f06SMike Snitzer struct dm_table {
14933bd6f06SMike Snitzer 	struct mapped_device *md;
15033bd6f06SMike Snitzer 	enum dm_queue_mode type;
15133bd6f06SMike Snitzer 
15233bd6f06SMike Snitzer 	/* btree table */
15333bd6f06SMike Snitzer 	unsigned int depth;
15433bd6f06SMike Snitzer 	unsigned int counts[DM_TABLE_MAX_DEPTH]; /* in nodes */
15533bd6f06SMike Snitzer 	sector_t *index[DM_TABLE_MAX_DEPTH];
15633bd6f06SMike Snitzer 
15733bd6f06SMike Snitzer 	unsigned int num_targets;
15833bd6f06SMike Snitzer 	unsigned int num_allocated;
15933bd6f06SMike Snitzer 	sector_t *highs;
16033bd6f06SMike Snitzer 	struct dm_target *targets;
16133bd6f06SMike Snitzer 
16233bd6f06SMike Snitzer 	struct target_type *immutable_target_type;
16333bd6f06SMike Snitzer 
16433bd6f06SMike Snitzer 	bool integrity_supported:1;
16533bd6f06SMike Snitzer 	bool singleton:1;
16633bd6f06SMike Snitzer 	unsigned integrity_added:1;
16733bd6f06SMike Snitzer 
16833bd6f06SMike Snitzer 	/*
16933bd6f06SMike Snitzer 	 * Indicates the rw permissions for the new logical
17033bd6f06SMike Snitzer 	 * device.  This should be a combination of FMODE_READ
17133bd6f06SMike Snitzer 	 * and FMODE_WRITE.
17233bd6f06SMike Snitzer 	 */
17333bd6f06SMike Snitzer 	fmode_t mode;
17433bd6f06SMike Snitzer 
17533bd6f06SMike Snitzer 	/* a list of devices used by this table */
17633bd6f06SMike Snitzer 	struct list_head devices;
17733bd6f06SMike Snitzer 
17833bd6f06SMike Snitzer 	/* events get handed up using this callback */
17933bd6f06SMike Snitzer 	void (*event_fn)(void *);
18033bd6f06SMike Snitzer 	void *event_context;
18133bd6f06SMike Snitzer 
18233bd6f06SMike Snitzer 	struct dm_md_mempools *mempools;
183aa6ce87aSSatya Tangirala 
184aa6ce87aSSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
185aa6ce87aSSatya Tangirala 	struct blk_keyslot_manager *ksm;
186aa6ce87aSSatya Tangirala #endif
18733bd6f06SMike Snitzer };
18833bd6f06SMike Snitzer 
189*e2118b3cSDamien Le Moal /*
190*e2118b3cSDamien Le Moal  * One of these is allocated per clone bio.
191*e2118b3cSDamien Le Moal  */
192*e2118b3cSDamien Le Moal #define DM_TIO_MAGIC 7282014
193*e2118b3cSDamien Le Moal struct dm_target_io {
194*e2118b3cSDamien Le Moal 	unsigned int magic;
195*e2118b3cSDamien Le Moal 	struct dm_io *io;
196*e2118b3cSDamien Le Moal 	struct dm_target *ti;
197*e2118b3cSDamien Le Moal 	unsigned int target_bio_nr;
198*e2118b3cSDamien Le Moal 	unsigned int *len_ptr;
199*e2118b3cSDamien Le Moal 	bool inside_dm_io;
200*e2118b3cSDamien Le Moal 	struct bio clone;
201*e2118b3cSDamien Le Moal };
202*e2118b3cSDamien Le Moal 
203*e2118b3cSDamien Le Moal /*
204*e2118b3cSDamien Le Moal  * One of these is allocated per original bio.
205*e2118b3cSDamien Le Moal  * It contains the first clone used for that original.
206*e2118b3cSDamien Le Moal  */
207*e2118b3cSDamien Le Moal #define DM_IO_MAGIC 5191977
208*e2118b3cSDamien Le Moal struct dm_io {
209*e2118b3cSDamien Le Moal 	unsigned int magic;
210*e2118b3cSDamien Le Moal 	struct mapped_device *md;
211*e2118b3cSDamien Le Moal 	blk_status_t status;
212*e2118b3cSDamien Le Moal 	atomic_t io_count;
213*e2118b3cSDamien Le Moal 	struct bio *orig_bio;
214*e2118b3cSDamien Le Moal 	unsigned long start_time;
215*e2118b3cSDamien Le Moal 	spinlock_t endio_lock;
216*e2118b3cSDamien Le Moal 	struct dm_stats_aux stats_aux;
217*e2118b3cSDamien Le Moal 	/* last member of dm_target_io is 'struct bio' */
218*e2118b3cSDamien Le Moal 	struct dm_target_io tio;
219*e2118b3cSDamien Le Moal };
220*e2118b3cSDamien Le Moal 
221*e2118b3cSDamien Le Moal static inline void dm_io_inc_pending(struct dm_io *io)
222*e2118b3cSDamien Le Moal {
223*e2118b3cSDamien Le Moal 	atomic_inc(&io->io_count);
224*e2118b3cSDamien Le Moal }
225*e2118b3cSDamien Le Moal 
226*e2118b3cSDamien Le Moal void dm_io_dec_pending(struct dm_io *io, blk_status_t error);
227*e2118b3cSDamien Le Moal 
2284cc96131SMike Snitzer static inline struct completion *dm_get_completion_from_kobject(struct kobject *kobj)
2294cc96131SMike Snitzer {
2304cc96131SMike Snitzer 	return &container_of(kobj, struct dm_kobject_holder, kobj)->completion;
2314cc96131SMike Snitzer }
2324cc96131SMike Snitzer 
2334cc96131SMike Snitzer unsigned __dm_get_module_param(unsigned *module_param, unsigned def, unsigned max);
2344cc96131SMike Snitzer 
2354cc96131SMike Snitzer static inline bool dm_message_test_buffer_overflow(char *result, unsigned maxlen)
2364cc96131SMike Snitzer {
2374cc96131SMike Snitzer 	return !maxlen || strlen(result) + 1 >= maxlen;
2384cc96131SMike Snitzer }
2394cc96131SMike Snitzer 
24093e6442cSMikulas Patocka extern atomic_t dm_global_event_nr;
24193e6442cSMikulas Patocka extern wait_queue_head_t dm_global_eventq;
24262e08243SMikulas Patocka void dm_issue_global_event(void);
24393e6442cSMikulas Patocka 
2444cc96131SMike Snitzer #endif
245