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;
65 unsigned int nr_zones;
66 unsigned int zone_offset;
70 sector_t zone_nr_sectors;
73 atomic_t unmap_nr_rnd;
74 struct list_head unmap_rnd_list;
75 struct list_head map_rnd_list;
78 atomic_t unmap_nr_seq;
79 struct list_head unmap_seq_list;
80 struct list_head map_seq_list;
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))
88 #define DMZ_BDEV_DYING (1 << 0)
89 #define DMZ_CHECK_BDEV (2 << 0)
90 #define DMZ_BDEV_REGULAR (4 << 0)
96 /* For listing the zone depending on its state */
97 struct list_head link;
99 /* Device containing this zone */
102 /* Zone type and state */
105 /* Zone activation reference count */
111 /* Zone write pointer block (relative to the zone start block) */
112 unsigned int wp_block;
114 /* Zone weight (number of valid blocks in the zone) */
117 /* The chunk that the zone maps */
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.
125 struct dm_zone *bzone;
132 /* Zone write type */
137 /* Zone critical condition */
141 /* How the zone is being used */
147 /* Zone internal state */
150 DMZ_RECLAIM_TERMINATE,
154 * Zone data accessors.
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)
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)
172 #define dmz_weight(z) ((z)->weight)
177 #define dmz_dev_info(dev, format, args...) \
178 DMINFO("(%pg): " format, (dev)->bdev, ## args)
180 #define dmz_dev_err(dev, format, args...) \
181 DMERR("(%pg): " format, (dev)->bdev, ## args)
183 #define dmz_dev_warn(dev, format, args...) \
184 DMWARN("(%pg): " format, (dev)->bdev, ## args)
186 #define dmz_dev_debug(dev, format, args...) \
187 DMDEBUG("(%pg): " format, (dev)->bdev, ## args)
190 * Functions defined in dm-zoned-metadata.c
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);
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);
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);
210 bool dmz_check_dev(struct dmz_metadata *zmd);
211 bool dmz_dev_is_dying(struct dmz_metadata *zmd);
213 #define DMZ_ALLOC_RND 0x01
214 #define DMZ_ALLOC_CACHE 0x02
215 #define DMZ_ALLOC_SEQ 0x04
216 #define DMZ_ALLOC_RECLAIM 0x10
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);
222 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
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);
238 * Activate a zone (increment its reference count).
240 static inline void dmz_activate_zone(struct dm_zone *zone)
242 atomic_inc(&zone->refcount);
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);
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);
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);
270 * Functions defined in dm-zoned-reclaim.c
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);
280 * Functions defined in dm-zoned-target.c
282 bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
283 bool dmz_check_bdev(struct dmz_dev *dmz_dev);
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.
289 static inline void dmz_deactivate_zone(struct dm_zone *zone)
291 dmz_reclaim_bio_acc(zone->dev->reclaim);
292 atomic_dec(&zone->refcount);
296 * Test if a zone is active, that is, has a refcount > 0.
298 static inline bool dmz_is_active(struct dm_zone *zone)
300 return atomic_read(&zone->refcount);
303 #endif /* DM_ZONED_H */