blob: 29e01a853f8470fa62e1c2aab96467257a2ebee4 [file] [log] [blame]
Dmitry Fomichevbae9a0a2019-08-02 15:02:50 -07001/* SPDX-License-Identifier: GPL-2.0 */
Damien Le Moal3b1a94c2017-06-07 15:55:39 +09002/*
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 */
51struct dmz_dev {
52 struct block_device *bdev;
53
54 char name[BDEVNAME_SIZE];
Hannes Reineckebd5c4032020-05-11 10:24:30 +020055 uuid_t uuid;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090056
57 sector_t capacity;
58
59 unsigned int nr_zones;
Hannes Reineckebd5c4032020-05-11 10:24:30 +020060 unsigned int zone_offset;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090061
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -070062 unsigned int flags;
63
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090064 sector_t zone_nr_sectors;
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090065};
66
Hannes Reinecke36820562020-05-11 10:24:21 +020067#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))
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090070
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -070071/* Device flags. */
72#define DMZ_BDEV_DYING (1 << 0)
Dmitry Fomicheve7fad902019-11-06 14:34:35 -080073#define DMZ_CHECK_BDEV (2 << 0)
Hannes Reineckebd5c4032020-05-11 10:24:30 +020074#define DMZ_BDEV_REGULAR (4 << 0)
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -070075
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090076/*
77 * Zone descriptor.
78 */
79struct dm_zone {
80 /* For listing the zone depending on its state */
81 struct list_head link;
82
83 /* Zone type and state */
84 unsigned long flags;
85
86 /* Zone activation reference count */
87 atomic_t refcount;
88
Hannes Reineckeb7122872020-05-11 10:24:18 +020089 /* Zone id */
90 unsigned int id;
91
Damien Le Moal3b1a94c2017-06-07 15:55:39 +090092 /* Zone write pointer block (relative to the zone start block) */
93 unsigned int wp_block;
94
95 /* Zone weight (number of valid blocks in the zone) */
96 unsigned int weight;
97
98 /* The chunk that the zone maps */
99 unsigned int chunk;
100
101 /*
102 * For a sequential data zone, pointer to the random zone
103 * used as a buffer for processing unaligned writes.
104 * For a buffer zone, this points back to the data zone.
105 */
106 struct dm_zone *bzone;
107};
108
109/*
110 * Zone flags.
111 */
112enum {
113 /* Zone write type */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200114 DMZ_CACHE,
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900115 DMZ_RND,
116 DMZ_SEQ,
117
118 /* Zone critical condition */
119 DMZ_OFFLINE,
120 DMZ_READ_ONLY,
121
122 /* How the zone is being used */
123 DMZ_META,
124 DMZ_DATA,
125 DMZ_BUF,
126
127 /* Zone internal state */
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900128 DMZ_RECLAIM,
129 DMZ_SEQ_WRITE_ERR,
130};
131
132/*
133 * Zone data accessors.
134 */
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200135#define dmz_is_cache(z) test_bit(DMZ_CACHE, &(z)->flags)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900136#define dmz_is_rnd(z) test_bit(DMZ_RND, &(z)->flags)
137#define dmz_is_seq(z) test_bit(DMZ_SEQ, &(z)->flags)
138#define dmz_is_empty(z) ((z)->wp_block == 0)
139#define dmz_is_offline(z) test_bit(DMZ_OFFLINE, &(z)->flags)
140#define dmz_is_readonly(z) test_bit(DMZ_READ_ONLY, &(z)->flags)
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900141#define dmz_in_reclaim(z) test_bit(DMZ_RECLAIM, &(z)->flags)
142#define dmz_seq_write_err(z) test_bit(DMZ_SEQ_WRITE_ERR, &(z)->flags)
143
144#define dmz_is_meta(z) test_bit(DMZ_META, &(z)->flags)
145#define dmz_is_buf(z) test_bit(DMZ_BUF, &(z)->flags)
146#define dmz_is_data(z) test_bit(DMZ_DATA, &(z)->flags)
147
148#define dmz_weight(z) ((z)->weight)
149
150/*
151 * Message functions.
152 */
153#define dmz_dev_info(dev, format, args...) \
154 DMINFO("(%s): " format, (dev)->name, ## args)
155
156#define dmz_dev_err(dev, format, args...) \
157 DMERR("(%s): " format, (dev)->name, ## args)
158
159#define dmz_dev_warn(dev, format, args...) \
160 DMWARN("(%s): " format, (dev)->name, ## args)
161
162#define dmz_dev_debug(dev, format, args...) \
163 DMDEBUG("(%s): " format, (dev)->name, ## args)
164
165struct dmz_metadata;
166struct dmz_reclaim;
167
168/*
169 * Functions defined in dm-zoned-metadata.c
170 */
Hannes Reineckebd5c4032020-05-11 10:24:30 +0200171int dmz_ctr_metadata(struct dmz_dev *dev, int num_dev,
172 struct dmz_metadata **zmd, const char *devname);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900173void dmz_dtr_metadata(struct dmz_metadata *zmd);
174int dmz_resume_metadata(struct dmz_metadata *zmd);
175
176void dmz_lock_map(struct dmz_metadata *zmd);
177void dmz_unlock_map(struct dmz_metadata *zmd);
178void dmz_lock_metadata(struct dmz_metadata *zmd);
179void dmz_unlock_metadata(struct dmz_metadata *zmd);
180void dmz_lock_flush(struct dmz_metadata *zmd);
181void dmz_unlock_flush(struct dmz_metadata *zmd);
182int dmz_flush_metadata(struct dmz_metadata *zmd);
Hannes Reinecke2234e732020-05-11 10:24:22 +0200183const char *dmz_metadata_label(struct dmz_metadata *zmd);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900184
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900185sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone);
186sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone);
187unsigned int dmz_nr_chunks(struct dmz_metadata *zmd);
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200188struct dmz_dev *dmz_zone_to_dev(struct dmz_metadata *zmd, struct dm_zone *zone);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900189
Hannes Reinecked0e21ce2020-05-11 10:24:23 +0200190bool dmz_check_dev(struct dmz_metadata *zmd);
191bool dmz_dev_is_dying(struct dmz_metadata *zmd);
192
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900193#define DMZ_ALLOC_RND 0x01
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200194#define DMZ_ALLOC_CACHE 0x02
195#define DMZ_ALLOC_SEQ 0x04
196#define DMZ_ALLOC_RECLAIM 0x10
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900197
198struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags);
199void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
200
201void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
202 unsigned int chunk);
203void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone);
Hannes Reineckebc3d5712020-05-11 10:24:16 +0200204unsigned int dmz_nr_zones(struct dmz_metadata *zmd);
Hannes Reinecke34f5aff2020-05-19 10:14:20 +0200205unsigned int dmz_nr_cache_zones(struct dmz_metadata *zmd);
206unsigned int dmz_nr_unmap_cache_zones(struct dmz_metadata *zmd);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900207unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd);
208unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd);
Hannes Reineckebc3d5712020-05-11 10:24:16 +0200209unsigned int dmz_nr_seq_zones(struct dmz_metadata *zmd);
210unsigned int dmz_nr_unmap_seq_zones(struct dmz_metadata *zmd);
Hannes Reinecke36820562020-05-11 10:24:21 +0200211unsigned int dmz_zone_nr_blocks(struct dmz_metadata *zmd);
212unsigned int dmz_zone_nr_blocks_shift(struct dmz_metadata *zmd);
213unsigned int dmz_zone_nr_sectors(struct dmz_metadata *zmd);
214unsigned int dmz_zone_nr_sectors_shift(struct dmz_metadata *zmd);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900215
Damien Le Moal3b8cafd2019-07-16 14:39:34 +0900216/*
217 * Activate a zone (increment its reference count).
218 */
219static inline void dmz_activate_zone(struct dm_zone *zone)
220{
221 atomic_inc(&zone->refcount);
222}
223
224/*
225 * Deactivate a zone. This decrement the zone reference counter
226 * indicating that all BIOs to the zone have completed when the count is 0.
227 */
228static inline void dmz_deactivate_zone(struct dm_zone *zone)
229{
230 atomic_dec(&zone->refcount);
231}
232
233/*
234 * Test if a zone is active, that is, has a refcount > 0.
235 */
236static inline bool dmz_is_active(struct dm_zone *zone)
237{
238 return atomic_read(&zone->refcount);
239}
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900240
241int dmz_lock_zone_reclaim(struct dm_zone *zone);
242void dmz_unlock_zone_reclaim(struct dm_zone *zone);
243struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd);
244
245struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd,
246 unsigned int chunk, int op);
247void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *zone);
248struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
249 struct dm_zone *dzone);
250
251int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
252 sector_t chunk_block, unsigned int nr_blocks);
253int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
254 sector_t chunk_block, unsigned int nr_blocks);
255int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
256 sector_t chunk_block);
257int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
258 sector_t *chunk_block);
259int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
260 struct dm_zone *to_zone);
261int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
262 struct dm_zone *to_zone, sector_t chunk_block);
263
264/*
265 * Functions defined in dm-zoned-reclaim.c
266 */
Hannes Reinecke6c805f72020-05-11 10:24:24 +0200267int dmz_ctr_reclaim(struct dmz_metadata *zmd, struct dmz_reclaim **zrc);
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900268void dmz_dtr_reclaim(struct dmz_reclaim *zrc);
269void dmz_suspend_reclaim(struct dmz_reclaim *zrc);
270void dmz_resume_reclaim(struct dmz_reclaim *zrc);
271void dmz_reclaim_bio_acc(struct dmz_reclaim *zrc);
272void dmz_schedule_reclaim(struct dmz_reclaim *zrc);
273
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -0700274/*
275 * Functions defined in dm-zoned-target.c
276 */
277bool dmz_bdev_is_dying(struct dmz_dev *dmz_dev);
Dmitry Fomicheve7fad902019-11-06 14:34:35 -0800278bool dmz_check_bdev(struct dmz_dev *dmz_dev);
Dmitry Fomichev75d66ff2019-08-10 14:43:11 -0700279
Damien Le Moal3b1a94c2017-06-07 15:55:39 +0900280#endif /* DM_ZONED_H */