xref: /linux/drivers/md/dm-zoned.h (revision 8f22272a)
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 /*
49  * Zoned block device information.
50  */
51 struct dmz_dev {
52 	struct block_device	*bdev;
53 
54 	char			name[BDEVNAME_SIZE];
55 	uuid_t			uuid;
56 
57 	sector_t		capacity;
58 
59 	unsigned int		nr_zones;
60 	unsigned int		zone_offset;
61 
62 	unsigned int		flags;
63 
64 	sector_t		zone_nr_sectors;
65 };
66 
67 #define dmz_bio_chunk(zmd, bio)	((bio)->bi_iter.bi_sector >> \
68 				 dmz_zone_nr_sectors_shift(zmd))
69 #define dmz_chunk_block(zmd, b)	((b) & (dmz_zone_nr_blocks(zmd) - 1))
70 
71 /* Device flags. */
72 #define DMZ_BDEV_DYING		(1 << 0)
73 #define DMZ_CHECK_BDEV		(2 << 0)
74 #define DMZ_BDEV_REGULAR	(4 << 0)
75 
76 /*
77  * Zone descriptor.
78  */
79 struct dm_zone {
80 	/* For listing the zone depending on its state */
81 	struct list_head	link;
82 
83 	/* Device containing this zone */
84 	struct dmz_dev		*dev;
85 
86 	/* Zone type and state */
87 	unsigned long		flags;
88 
89 	/* Zone activation reference count */
90 	atomic_t		refcount;
91 
92 	/* Zone id */
93 	unsigned int		id;
94 
95 	/* Zone write pointer block (relative to the zone start block) */
96 	unsigned int		wp_block;
97 
98 	/* Zone weight (number of valid blocks in the zone) */
99 	unsigned int		weight;
100 
101 	/* The chunk that the zone maps */
102 	unsigned int		chunk;
103 
104 	/*
105 	 * For a sequential data zone, pointer to the random zone
106 	 * used as a buffer for processing unaligned writes.
107 	 * For a buffer zone, this points back to the data zone.
108 	 */
109 	struct dm_zone		*bzone;
110 };
111 
112 /*
113  * Zone flags.
114  */
115 enum {
116 	/* Zone write type */
117 	DMZ_CACHE,
118 	DMZ_RND,
119 	DMZ_SEQ,
120 
121 	/* Zone critical condition */
122 	DMZ_OFFLINE,
123 	DMZ_READ_ONLY,
124 
125 	/* How the zone is being used */
126 	DMZ_META,
127 	DMZ_DATA,
128 	DMZ_BUF,
129 	DMZ_RESERVED,
130 
131 	/* Zone internal state */
132 	DMZ_RECLAIM,
133 	DMZ_SEQ_WRITE_ERR,
134 	DMZ_RECLAIM_TERMINATE,
135 };
136 
137 /*
138  * Zone data accessors.
139  */
140 #define dmz_is_cache(z)		test_bit(DMZ_CACHE, &(z)->flags)
141 #define dmz_is_rnd(z)		test_bit(DMZ_RND, &(z)->flags)
142 #define dmz_is_seq(z)		test_bit(DMZ_SEQ, &(z)->flags)
143 #define dmz_is_empty(z)		((z)->wp_block == 0)
144 #define dmz_is_offline(z)	test_bit(DMZ_OFFLINE, &(z)->flags)
145 #define dmz_is_readonly(z)	test_bit(DMZ_READ_ONLY, &(z)->flags)
146 #define dmz_in_reclaim(z)	test_bit(DMZ_RECLAIM, &(z)->flags)
147 #define dmz_is_reserved(z)	test_bit(DMZ_RESERVED, &(z)->flags)
148 #define dmz_seq_write_err(z)	test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags)
149 #define dmz_reclaim_should_terminate(z) \
150 				test_bit(DMZ_RECLAIM_TERMINATE, &(z)->flags)
151 
152 #define dmz_is_meta(z)		test_bit(DMZ_META, &(z)->flags)
153 #define dmz_is_buf(z)		test_bit(DMZ_BUF, &(z)->flags)
154 #define dmz_is_data(z)		test_bit(DMZ_DATA, &(z)->flags)
155 
156 #define dmz_weight(z)		((z)->weight)
157 
158 /*
159  * Message functions.
160  */
161 #define dmz_dev_info(dev, format, args...)	\
162 	DMINFO("(%s): " format, (dev)->name, ## args)
163 
164 #define dmz_dev_err(dev, format, args...)	\
165 	DMERR("(%s): " format, (dev)->name, ## args)
166 
167 #define dmz_dev_warn(dev, format, args...)	\
168 	DMWARN("(%s): " format, (dev)->name, ## args)
169 
170 #define dmz_dev_debug(dev, format, args...)	\
171 	DMDEBUG("(%s): " format, (dev)->name, ## args)
172 
173 struct dmz_metadata;
174 struct dmz_reclaim;
175 
176 /*
177  * Functions defined in dm-zoned-metadata.c
178  */
179 int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev,
180 		     struct dmz_metadata **zmd, const char *devname);
181 void dmz_dtr_metadata(struct dmz_metadata *zmd);
182 int dmz_resume_metadata(struct dmz_metadata *zmd);
183 
184 void dmz_lock_map(struct dmz_metadata *zmd);
185 void dmz_unlock_map(struct dmz_metadata *zmd);
186 void dmz_lock_metadata(struct dmz_metadata *zmd);
187 void dmz_unlock_metadata(struct dmz_metadata *zmd);
188 void dmz_lock_flush(struct dmz_metadata *zmd);
189 void dmz_unlock_flush(struct dmz_metadata *zmd);
190 int dmz_flush_metadata(struct dmz_metadata *zmd);
191 const char *dmz_metadata_label(struct dmz_metadata *zmd);
192 
193 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone);
194 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone);
195 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd);
196 
197 bool dmz_check_dev(struct dmz_metadata *zmd);
198 bool dmz_dev_is_dying(struct dmz_metadata *zmd);
199 
200 #define DMZ_ALLOC_RND		0x01
201 #define DMZ_ALLOC_CACHE		0x02
202 #define DMZ_ALLOC_SEQ		0x04
203 #define DMZ_ALLOC_RECLAIM	0x10
204 
205 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags);
206 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
207 
208 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
209 		  unsigned int chunk);
210 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
211 unsigned int dmz_nr_zones(struct dmz_metadata *zmd);
212 unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd);
213 unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd);
214 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd);
215 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd);
216 unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd);
217 unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd);
218 unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd);
219 unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd);
220 unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd);
221 unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd);
222 
223 /*
224  * Activate a zone (increment its reference count).
225  */
226 static inline void dmz_activate_zone(struct dm_zone *zone)
227 {
228 	atomic_inc(&zone->refcount);
229 }
230 
231 /*
232  * Deactivate a zone. This decrement the zone reference counter
233  * indicating that all BIOs to the zone have completed when the count is 0.
234  */
235 static inline void dmz_deactivate_zone(struct dm_zone *zone)
236 {
237 	atomic_dec(&zone->refcount);
238 }
239 
240 /*
241  * Test if a zone is active, that is, has a refcount > 0.
242  */
243 static inline bool dmz_is_active(struct dm_zone *zone)
244 {
245 	return atomic_read(&zone->refcount);
246 }
247 
248 int dmz_lock_zone_reclaim(struct dm_zone *zone);
249 void dmz_unlock_zone_reclaim(struct dm_zone *zone);
250 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd, bool idle);
251 
252 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
253 				      unsigned int chunk, int op);
254 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone);
255 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
256 				     struct dm_zone *dzone);
257 
258 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
259 			sector_t chunk_block, unsigned int nr_blocks);
260 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
261 			  sector_t chunk_block, unsigned int nr_blocks);
262 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
263 		    sector_t chunk_block);
264 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
265 			  sector_t *chunk_block);
266 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
267 			  struct dm_zone *to_zone);
268 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
269 			   struct dm_zone *to_zone, sector_t chunk_block);
270 
271 /*
272  * Functions defined in dm-zoned-reclaim.c
273  */
274 int dmz_ctr_reclaim(struct dmz_metadata *zmd, struct dmz_reclaim **zrc);
275 void dmz_dtr_reclaim(struct dmz_reclaim *zrc);
276 void dmz_suspend_reclaim(struct dmz_reclaim *zrc);
277 void dmz_resume_reclaim(struct dmz_reclaim *zrc);
278 void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc);
279 void dmz_schedule_reclaim(struct dmz_reclaim *zrc);
280 
281 /*
282  * Functions defined in dm-zoned-target.c
283  */
284 bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
285 bool dmz_check_bdev(struct dmz_dev *dmz_dev);
286 
287 #endif /* DM_ZONED_H */
288