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