xref: /openbmc/linux/drivers/md/dm-zoned.h (revision baf2c002)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2017 Western Digital Corporation or its affiliates.
4  *
5  * This file is released under the GPL.
6  */
7 
8 #ifndef DM_ZONED_H
9 #define DM_ZONED_H
10 
11 #include <linux/types.h>
12 #include <linux/blkdev.h>
13 #include <linux/device-mapper.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/list.h>
16 #include <linux/spinlock.h>
17 #include <linux/mutex.h>
18 #include <linux/workqueue.h>
19 #include <linux/rwsem.h>
20 #include <linux/rbtree.h>
21 #include <linux/radix-tree.h>
22 #include <linux/shrinker.h>
23 
24 /*
25  * dm-zoned creates block devices with 4KB blocks, always.
26  */
27 #define DMZ_BLOCK_SHIFT		12
28 #define DMZ_BLOCK_SIZE		(1 << DMZ_BLOCK_SHIFT)
29 #define DMZ_BLOCK_MASK		(DMZ_BLOCK_SIZE - 1)
30 
31 #define DMZ_BLOCK_SHIFT_BITS	(DMZ_BLOCK_SHIFT + 3)
32 #define DMZ_BLOCK_SIZE_BITS	(1 << DMZ_BLOCK_SHIFT_BITS)
33 #define DMZ_BLOCK_MASK_BITS	(DMZ_BLOCK_SIZE_BITS - 1)
34 
35 #define DMZ_BLOCK_SECTORS_SHIFT	(DMZ_BLOCK_SHIFT - SECTOR_SHIFT)
36 #define DMZ_BLOCK_SECTORS	(DMZ_BLOCK_SIZE >> SECTOR_SHIFT)
37 #define DMZ_BLOCK_SECTORS_MASK	(DMZ_BLOCK_SECTORS - 1)
38 
39 /*
40  * 4KB block <-> 512B sector conversion.
41  */
42 #define dmz_blk2sect(b)		((sector_t)(b) << DMZ_BLOCK_SECTORS_SHIFT)
43 #define dmz_sect2blk(s)		((sector_t)(s) >> DMZ_BLOCK_SECTORS_SHIFT)
44 
45 #define dmz_bio_block(bio)	dmz_sect2blk((bio)->bi_iter.bi_sector)
46 #define dmz_bio_blocks(bio)	dmz_sect2blk(bio_sectors(bio))
47 
48 struct dmz_metadata;
49 struct dmz_reclaim;
50 
51 /*
52  * Zoned block device information.
53  */
54 struct dmz_dev {
55 	struct block_device	*bdev;
56 	struct dmz_metadata	*metadata;
57 	struct dmz_reclaim	*reclaim;
58 
59 	uuid_t			uuid;
60 
61 	sector_t		capacity;
62 
63 	unsigned int		dev_idx;
64 
65 	unsigned int		nr_zones;
66 	unsigned int		zone_offset;
67 
68 	unsigned int		flags;
69 
70 	sector_t		zone_nr_sectors;
71 
72 	unsigned int		nr_rnd;
73 	atomic_t		unmap_nr_rnd;
74 	struct list_head	unmap_rnd_list;
75 	struct list_head	map_rnd_list;
76 
77 	unsigned int		nr_seq;
78 	atomic_t		unmap_nr_seq;
79 	struct list_head	unmap_seq_list;
80 	struct list_head	map_seq_list;
81 };
82 
83 #define dmz_bio_chunk(zmd, bio)	((bio)->bi_iter.bi_sector >> \
84 				 dmz_zone_nr_sectors_shift(zmd))
85 #define dmz_chunk_block(zmd, b)	((b) & (dmz_zone_nr_blocks(zmd) - 1))
86 
87 /* Device flags. */
88 #define DMZ_BDEV_DYING		(1 << 0)
89 #define DMZ_CHECK_BDEV		(2 << 0)
90 #define DMZ_BDEV_REGULAR	(4 << 0)
91 
92 /*
93  * Zone descriptor.
94  */
95 struct dm_zone {
96 	/* For listing the zone depending on its state */
97 	struct list_head	link;
98 
99 	/* Device containing this zone */
100 	struct dmz_dev		*dev;
101 
102 	/* Zone type and state */
103 	unsigned long		flags;
104 
105 	/* Zone activation reference count */
106 	atomic_t		refcount;
107 
108 	/* Zone id */
109 	unsigned int		id;
110 
111 	/* Zone write pointer block (relative to the zone start block) */
112 	unsigned int		wp_block;
113 
114 	/* Zone weight (number of valid blocks in the zone) */
115 	unsigned int		weight;
116 
117 	/* The chunk that the zone maps */
118 	unsigned int		chunk;
119 
120 	/*
121 	 * For a sequential data zone, pointer to the random zone
122 	 * used as a buffer for processing unaligned writes.
123 	 * For a buffer zone, this points back to the data zone.
124 	 */
125 	struct dm_zone		*bzone;
126 };
127 
128 /*
129  * Zone flags.
130  */
131 enum {
132 	/* Zone write type */
133 	DMZ_CACHE,
134 	DMZ_RND,
135 	DMZ_SEQ,
136 
137 	/* Zone critical condition */
138 	DMZ_OFFLINE,
139 	DMZ_READ_ONLY,
140 
141 	/* How the zone is being used */
142 	DMZ_META,
143 	DMZ_DATA,
144 	DMZ_BUF,
145 	DMZ_RESERVED,
146 
147 	/* Zone internal state */
148 	DMZ_RECLAIM,
149 	DMZ_SEQ_WRITE_ERR,
150 	DMZ_RECLAIM_TERMINATE,
151 };
152 
153 /*
154  * Zone data accessors.
155  */
156 #define dmz_is_cache(z)		test_bit(DMZ_CACHE, &(z)->flags)
157 #define dmz_is_rnd(z)		test_bit(DMZ_RND, &(z)->flags)
158 #define dmz_is_seq(z)		test_bit(DMZ_SEQ, &(z)->flags)
159 #define dmz_is_empty(z)		((z)->wp_block == 0)
160 #define dmz_is_offline(z)	test_bit(DMZ_OFFLINE, &(z)->flags)
161 #define dmz_is_readonly(z)	test_bit(DMZ_READ_ONLY, &(z)->flags)
162 #define dmz_in_reclaim(z)	test_bit(DMZ_RECLAIM, &(z)->flags)
163 #define dmz_is_reserved(z)	test_bit(DMZ_RESERVED, &(z)->flags)
164 #define dmz_seq_write_err(z)	test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags)
165 #define dmz_reclaim_should_terminate(z) \
166 				test_bit(DMZ_RECLAIM_TERMINATE, &(z)->flags)
167 
168 #define dmz_is_meta(z)		test_bit(DMZ_META, &(z)->flags)
169 #define dmz_is_buf(z)		test_bit(DMZ_BUF, &(z)->flags)
170 #define dmz_is_data(z)		test_bit(DMZ_DATA, &(z)->flags)
171 
172 #define dmz_weight(z)		((z)->weight)
173 
174 /*
175  * Message functions.
176  */
177 #define dmz_dev_info(dev, format, args...)	\
178 	DMINFO("(%pg): " format, (dev)->bdev, ## args)
179 
180 #define dmz_dev_err(dev, format, args...)	\
181 	DMERR("(%pg): " format, (dev)->bdev, ## args)
182 
183 #define dmz_dev_warn(dev, format, args...)	\
184 	DMWARN("(%pg): " format, (dev)->bdev, ## args)
185 
186 #define dmz_dev_debug(dev, format, args...)	\
187 	DMDEBUG("(%pg): " format, (dev)->bdev, ## args)
188 
189 /*
190  * Functions defined in dm-zoned-metadata.c
191  */
192 int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev,
193 		     struct dmz_metadata **zmd, const char *devname);
194 void dmz_dtr_metadata(struct dmz_metadata *zmd);
195 int dmz_resume_metadata(struct dmz_metadata *zmd);
196 
197 void dmz_lock_map(struct dmz_metadata *zmd);
198 void dmz_unlock_map(struct dmz_metadata *zmd);
199 void dmz_lock_metadata(struct dmz_metadata *zmd);
200 void dmz_unlock_metadata(struct dmz_metadata *zmd);
201 void dmz_lock_flush(struct dmz_metadata *zmd);
202 void dmz_unlock_flush(struct dmz_metadata *zmd);
203 int dmz_flush_metadata(struct dmz_metadata *zmd);
204 const char *dmz_metadata_label(struct dmz_metadata *zmd);
205 
206 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone);
207 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone);
208 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd);
209 
210 bool dmz_check_dev(struct dmz_metadata *zmd);
211 bool dmz_dev_is_dying(struct dmz_metadata *zmd);
212 
213 #define DMZ_ALLOC_RND		0x01
214 #define DMZ_ALLOC_CACHE		0x02
215 #define DMZ_ALLOC_SEQ		0x04
216 #define DMZ_ALLOC_RECLAIM	0x10
217 
218 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd,
219 			       unsigned int dev_idx, unsigned long flags);
220 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
221 
222 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
223 		  unsigned int chunk);
224 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
225 unsigned int dmz_nr_zones(struct dmz_metadata *zmd);
226 unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd);
227 unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd);
228 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd, int idx);
229 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd, int idx);
230 unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd, int idx);
231 unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd, int idx);
232 unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd);
233 unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd);
234 unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd);
235 unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd);
236 
237 /*
238  * Activate a zone (increment its reference count).
239  */
240 static inline void dmz_activate_zone(struct dm_zone *zone)
241 {
242 	atomic_inc(&zone->refcount);
243 }
244 
245 int dmz_lock_zone_reclaim(struct dm_zone *zone);
246 void dmz_unlock_zone_reclaim(struct dm_zone *zone);
247 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd,
248 					 unsigned int dev_idx, bool idle);
249 
250 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
251 				      unsigned int chunk, enum req_op op);
252 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone);
253 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
254 				     struct dm_zone *dzone);
255 
256 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
257 			sector_t chunk_block, unsigned int nr_blocks);
258 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
259 			  sector_t chunk_block, unsigned int nr_blocks);
260 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
261 		    sector_t chunk_block);
262 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
263 			  sector_t *chunk_block);
264 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
265 			  struct dm_zone *to_zone);
266 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
267 			   struct dm_zone *to_zone, sector_t chunk_block);
268 
269 /*
270  * Functions defined in dm-zoned-reclaim.c
271  */
272 int dmz_ctr_reclaim(struct dmz_metadata *zmd, struct dmz_reclaim **zrc, int idx);
273 void dmz_dtr_reclaim(struct dmz_reclaim *zrc);
274 void dmz_suspend_reclaim(struct dmz_reclaim *zrc);
275 void dmz_resume_reclaim(struct dmz_reclaim *zrc);
276 void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc);
277 void dmz_schedule_reclaim(struct dmz_reclaim *zrc);
278 
279 /*
280  * Functions defined in dm-zoned-target.c
281  */
282 bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
283 bool dmz_check_bdev(struct dmz_dev *dmz_dev);
284 
285 /*
286  * Deactivate a zone. This decrement the zone reference counter
287  * indicating that all BIOs to the zone have completed when the count is 0.
288  */
289 static inline void dmz_deactivate_zone(struct dm_zone *zone)
290 {
291 	dmz_reclaim_bio_acc(zone->dev->reclaim);
292 	atomic_dec(&zone->refcount);
293 }
294 
295 /*
296  * Test if a zone is active, that is, has a refcount > 0.
297  */
298 static inline bool dmz_is_active(struct dm_zone *zone)
299 {
300 	return atomic_read(&zone->refcount);
301 }
302 
303 #endif /* DM_ZONED_H */
304