1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2017 Western Digital Corporation or its affiliates.
5 * This file is released under the GPL.
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>
25 * dm-zoned creates block devices with 4KB blocks, always.
27 #define DMZ_BLOCK_SHIFT 12
28 #define DMZ_BLOCK_SIZE (1 << DMZ_BLOCK_SHIFT)
29 #define DMZ_BLOCK_MASK (DMZ_BLOCK_SIZE - 1)
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)
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)
40 * 4KB block <-> 512B sector conversion.
42 #define dmz_blk2sect(b) ((sector_t)(b) << DMZ_BLOCK_SECTORS_SHIFT)
43 #define dmz_sect2blk(s) ((sector_t)(s) >> DMZ_BLOCK_SECTORS_SHIFT)
45 #define dmz_bio_block(bio) dmz_sect2blk((bio)->bi_iter.bi_sector)
46 #define dmz_bio_blocks(bio) dmz_sect2blk(bio_sectors(bio))
52 * Zoned block device information.
55 struct block_device *bdev;
56 struct dmz_metadata *metadata;
57 struct dmz_reclaim *reclaim;
59 char name[BDEVNAME_SIZE];
66 unsigned int nr_zones;
67 unsigned int zone_offset;
71 sector_t zone_nr_sectors;
74 atomic_t unmap_nr_rnd;
75 struct list_head unmap_rnd_list;
76 struct list_head map_rnd_list;
79 atomic_t unmap_nr_seq;
80 struct list_head unmap_seq_list;
81 struct list_head map_seq_list;
84 #define dmz_bio_chunk(zmd, bio) ((bio)->bi_iter.bi_sector >> \
85 dmz_zone_nr_sectors_shift(zmd))
86 #define dmz_chunk_block(zmd, b) ((b) & (dmz_zone_nr_blocks(zmd) - 1))
89 #define DMZ_BDEV_DYING (1 << 0)
90 #define DMZ_CHECK_BDEV (2 << 0)
91 #define DMZ_BDEV_REGULAR (4 << 0)
97 /* For listing the zone depending on its state */
98 struct list_head link;
100 /* Device containing this zone */
103 /* Zone type and state */
106 /* Zone activation reference count */
112 /* Zone write pointer block (relative to the zone start block) */
113 unsigned int wp_block;
115 /* Zone weight (number of valid blocks in the zone) */
118 /* The chunk that the zone maps */
122 * For a sequential data zone, pointer to the random zone
123 * used as a buffer for processing unaligned writes.
124 * For a buffer zone, this points back to the data zone.
126 struct dm_zone *bzone;
133 /* Zone write type */
138 /* Zone critical condition */
142 /* How the zone is being used */
148 /* Zone internal state */
151 DMZ_RECLAIM_TERMINATE,
155 * Zone data accessors.
157 #define dmz_is_cache(z) test_bit(DMZ_CACHE, &(z)->flags)
158 #define dmz_is_rnd(z) test_bit(DMZ_RND, &(z)->flags)
159 #define dmz_is_seq(z) test_bit(DMZ_SEQ, &(z)->flags)
160 #define dmz_is_empty(z) ((z)->wp_block == 0)
161 #define dmz_is_offline(z) test_bit(DMZ_OFFLINE, &(z)->flags)
162 #define dmz_is_readonly(z) test_bit(DMZ_READ_ONLY, &(z)->flags)
163 #define dmz_in_reclaim(z) test_bit(DMZ_RECLAIM, &(z)->flags)
164 #define dmz_is_reserved(z) test_bit(DMZ_RESERVED, &(z)->flags)
165 #define dmz_seq_write_err(z) test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags)
166 #define dmz_reclaim_should_terminate(z) \
167 test_bit(DMZ_RECLAIM_TERMINATE, &(z)->flags)
169 #define dmz_is_meta(z) test_bit(DMZ_META, &(z)->flags)
170 #define dmz_is_buf(z) test_bit(DMZ_BUF, &(z)->flags)
171 #define dmz_is_data(z) test_bit(DMZ_DATA, &(z)->flags)
173 #define dmz_weight(z) ((z)->weight)
178 #define dmz_dev_info(dev, format, args...) \
179 DMINFO("(%s): " format, (dev)->name, ## args)
181 #define dmz_dev_err(dev, format, args...) \
182 DMERR("(%s): " format, (dev)->name, ## args)
184 #define dmz_dev_warn(dev, format, args...) \
185 DMWARN("(%s): " format, (dev)->name, ## args)
187 #define dmz_dev_debug(dev, format, args...) \
188 DMDEBUG("(%s): " format, (dev)->name, ## args)
191 * Functions defined in dm-zoned-metadata.c
193 int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev,
194 struct dmz_metadata **zmd, const char *devname);
195 void dmz_dtr_metadata(struct dmz_metadata *zmd);
196 int dmz_resume_metadata(struct dmz_metadata *zmd);
198 void dmz_lock_map(struct dmz_metadata *zmd);
199 void dmz_unlock_map(struct dmz_metadata *zmd);
200 void dmz_lock_metadata(struct dmz_metadata *zmd);
201 void dmz_unlock_metadata(struct dmz_metadata *zmd);
202 void dmz_lock_flush(struct dmz_metadata *zmd);
203 void dmz_unlock_flush(struct dmz_metadata *zmd);
204 int dmz_flush_metadata(struct dmz_metadata *zmd);
205 const char *dmz_metadata_label(struct dmz_metadata *zmd);
207 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone);
208 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone);
209 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd);
211 bool dmz_check_dev(struct dmz_metadata *zmd);
212 bool dmz_dev_is_dying(struct dmz_metadata *zmd);
214 #define DMZ_ALLOC_RND 0x01
215 #define DMZ_ALLOC_CACHE 0x02
216 #define DMZ_ALLOC_SEQ 0x04
217 #define DMZ_ALLOC_RECLAIM 0x10
219 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd,
220 unsigned int dev_idx, unsigned long flags);
221 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
223 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
225 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
226 unsigned int dmz_nr_zones(struct dmz_metadata *zmd);
227 unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd);
228 unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd);
229 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd, int idx);
230 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd, int idx);
231 unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd, int idx);
232 unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd, int idx);
233 unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd);
234 unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd);
235 unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd);
236 unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd);
239 * Activate a zone (increment its reference count).
241 static inline void dmz_activate_zone(struct dm_zone *zone)
243 atomic_inc(&zone->refcount);
246 int dmz_lock_zone_reclaim(struct dm_zone *zone);
247 void dmz_unlock_zone_reclaim(struct dm_zone *zone);
248 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd,
249 unsigned int dev_idx, bool idle);
251 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
252 unsigned int chunk, int op);
253 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone);
254 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
255 struct dm_zone *dzone);
257 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
258 sector_t chunk_block, unsigned int nr_blocks);
259 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
260 sector_t chunk_block, unsigned int nr_blocks);
261 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
262 sector_t chunk_block);
263 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
264 sector_t *chunk_block);
265 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
266 struct dm_zone *to_zone);
267 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
268 struct dm_zone *to_zone, sector_t chunk_block);
271 * Functions defined in dm-zoned-reclaim.c
273 int dmz_ctr_reclaim(struct dmz_metadata *zmd, struct dmz_reclaim **zrc, int idx);
274 void dmz_dtr_reclaim(struct dmz_reclaim *zrc);
275 void dmz_suspend_reclaim(struct dmz_reclaim *zrc);
276 void dmz_resume_reclaim(struct dmz_reclaim *zrc);
277 void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc);
278 void dmz_schedule_reclaim(struct dmz_reclaim *zrc);
281 * Functions defined in dm-zoned-target.c
283 bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
284 bool dmz_check_bdev(struct dmz_dev *dmz_dev);
287 * Deactivate a zone. This decrement the zone reference counter
288 * indicating that all BIOs to the zone have completed when the count is 0.
290 static inline void dmz_deactivate_zone(struct dm_zone *zone)
292 dmz_reclaim_bio_acc(zone->dev->reclaim);
293 atomic_dec(&zone->refcount);
297 * Test if a zone is active, that is, has a refcount > 0.
299 static inline bool dmz_is_active(struct dm_zone *zone)
301 return atomic_read(&zone->refcount);
304 #endif /* DM_ZONED_H */