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