xref: /linux/drivers/md/dm-zone.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
4  */
5 
6 #include <linux/blkdev.h>
7 #include <linux/mm.h>
8 #include <linux/sched/mm.h>
9 #include <linux/slab.h>
10 #include <linux/bitmap.h>
11 
12 #include "dm-core.h"
13 
14 #define DM_MSG_PREFIX "zone"
15 
16 /*
17  * For internal zone reports bypassing the top BIO submission path.
18  */
19 static int dm_blk_do_report_zones(struct mapped_device *md, struct dm_table *t,
20 				  unsigned int nr_zones,
21 				  struct dm_report_zones_args *args)
22 {
23 	do {
24 		struct dm_target *tgt;
25 		int ret;
26 
27 		tgt = dm_table_find_target(t, args->next_sector);
28 		if (WARN_ON_ONCE(!tgt->type->report_zones))
29 			return -EIO;
30 
31 		args->tgt = tgt;
32 		ret = tgt->type->report_zones(tgt, args,
33 					      nr_zones - args->zone_idx);
34 		if (ret < 0)
35 			return ret;
36 		if (!ret)
37 			return -EIO;
38 	} while (args->zone_idx < nr_zones &&
39 		 args->next_sector < get_capacity(md->disk));
40 
41 	return args->zone_idx;
42 }
43 
44 /*
45  * User facing dm device block device report zone operation. This calls the
46  * report_zones operation for each target of a device table. This operation is
47  * generally implemented by targets using dm_report_zones().
48  */
49 int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
50 			unsigned int nr_zones,
51 			struct blk_report_zones_args *args)
52 {
53 	struct mapped_device *md = disk->private_data;
54 	struct dm_table *map;
55 	struct dm_table *zone_revalidate_map = READ_ONCE(md->zone_revalidate_map);
56 	int srcu_idx, ret = -EIO;
57 	bool put_table = false;
58 
59 	if (!zone_revalidate_map || md->revalidate_map_task != current) {
60 		/*
61 		 * Regular user context or
62 		 * Zone revalidation during __bind() is in progress, but this
63 		 * call is from a different process
64 		 */
65 		map = dm_get_live_table(md, &srcu_idx);
66 		put_table = true;
67 
68 		if (dm_suspended_md(md)) {
69 			ret = -EAGAIN;
70 			goto do_put_table;
71 		}
72 	} else {
73 		/* Zone revalidation during __bind() */
74 		map = zone_revalidate_map;
75 	}
76 
77 	if (map) {
78 		struct dm_report_zones_args dm_args = {
79 			.disk = md->disk,
80 			.next_sector = sector,
81 			.rep_args = args,
82 		};
83 		ret = dm_blk_do_report_zones(md, map, nr_zones, &dm_args);
84 	}
85 
86 do_put_table:
87 	if (put_table)
88 		dm_put_live_table(md, srcu_idx);
89 
90 	return ret;
91 }
92 
93 static int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx,
94 			      void *data)
95 {
96 	struct dm_report_zones_args *args = data;
97 	sector_t sector_diff = args->tgt->begin - args->start;
98 
99 	/*
100 	 * Ignore zones beyond the target range.
101 	 */
102 	if (zone->start >= args->start + args->tgt->len)
103 		return 0;
104 
105 	/*
106 	 * Remap the start sector and write pointer position of the zone
107 	 * to match its position in the target range.
108 	 */
109 	zone->start += sector_diff;
110 	if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
111 		if (zone->cond == BLK_ZONE_COND_FULL)
112 			zone->wp = zone->start + zone->len;
113 		else if (zone->cond == BLK_ZONE_COND_EMPTY)
114 			zone->wp = zone->start;
115 		else
116 			zone->wp += sector_diff;
117 	}
118 
119 	args->next_sector = zone->start + zone->len;
120 
121 	/* If we have an internal callback, call it first. */
122 	if (args->cb) {
123 		int ret;
124 
125 		ret = args->cb(zone, args->zone_idx, args->data);
126 		if (ret)
127 			return ret;
128 	}
129 
130 	return disk_report_zone(args->disk, zone, args->zone_idx++,
131 				args->rep_args);
132 }
133 
134 /*
135  * Helper for drivers of zoned targets to implement struct target_type
136  * report_zones operation.
137  */
138 int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector,
139 		    struct dm_report_zones_args *args, unsigned int nr_zones)
140 {
141 	/*
142 	 * Set the target mapping start sector first so that
143 	 * dm_report_zones_cb() can correctly remap zone information.
144 	 */
145 	args->start = start;
146 
147 	return blkdev_report_zones(bdev, sector, nr_zones,
148 				   dm_report_zones_cb, args);
149 }
150 EXPORT_SYMBOL_GPL(dm_report_zones);
151 
152 bool dm_is_zone_write(struct mapped_device *md, struct bio *bio)
153 {
154 	struct request_queue *q = md->queue;
155 
156 	if (!blk_queue_is_zoned(q))
157 		return false;
158 
159 	switch (bio_op(bio)) {
160 	case REQ_OP_WRITE_ZEROES:
161 	case REQ_OP_WRITE:
162 		return !op_is_flush(bio->bi_opf) && bio_sectors(bio);
163 	default:
164 		return false;
165 	}
166 }
167 
168 /*
169  * Revalidate the zones of a mapped device to initialize resource necessary
170  * for zone append emulation. Note that we cannot simply use the block layer
171  * blk_revalidate_disk_zones() function here as the mapped device is suspended
172  * (this is called from __bind() context).
173  */
174 int dm_revalidate_zones(struct dm_table *t, struct request_queue *q)
175 {
176 	struct mapped_device *md = t->md;
177 	struct gendisk *disk = md->disk;
178 	unsigned int nr_zones = disk->nr_zones;
179 	int ret;
180 
181 	if (!get_capacity(disk))
182 		return 0;
183 
184 	/*
185 	 * Do not revalidate if zone write plug resources have already
186 	 * been allocated.
187 	 */
188 	if (dm_has_zone_plugs(md))
189 		return 0;
190 
191 	DMINFO("%s using %s zone append", disk->disk_name,
192 	       queue_emulates_zone_append(q) ? "emulated" : "native");
193 
194 	/*
195 	 * Our table is not live yet. So the call to dm_get_live_table()
196 	 * in dm_blk_report_zones() will fail. Set a temporary pointer to
197 	 * our table for dm_blk_report_zones() to use directly.
198 	 */
199 	md->zone_revalidate_map = t;
200 	md->revalidate_map_task = current;
201 	ret = blk_revalidate_disk_zones(disk);
202 	md->revalidate_map_task = NULL;
203 	md->zone_revalidate_map = NULL;
204 
205 	if (ret) {
206 		DMERR("Revalidate zones failed %d", ret);
207 		disk->nr_zones = nr_zones;
208 		return ret;
209 	}
210 
211 	return 0;
212 }
213 
214 static int device_not_zone_append_capable(struct dm_target *ti,
215 					  struct dm_dev *dev, sector_t start,
216 					  sector_t len, void *data)
217 {
218 	return !bdev_is_zoned(dev->bdev);
219 }
220 
221 static bool dm_table_supports_zone_append(struct dm_table *t)
222 {
223 	for (unsigned int i = 0; i < t->num_targets; i++) {
224 		struct dm_target *ti = dm_table_get_target(t, i);
225 
226 		if (ti->emulate_zone_append)
227 			return false;
228 
229 		if (!ti->type->iterate_devices ||
230 		    ti->type->iterate_devices(ti, device_not_zone_append_capable, NULL))
231 			return false;
232 	}
233 
234 	return true;
235 }
236 
237 struct dm_device_zone_count {
238 	sector_t start;
239 	sector_t len;
240 	unsigned int total_nr_seq_zones;
241 	unsigned int target_nr_seq_zones;
242 };
243 
244 /*
245  * Count the total number of and the number of mapped sequential zones of a
246  * target zoned device.
247  */
248 static int dm_device_count_zones_cb(struct blk_zone *zone,
249 				    unsigned int idx, void *data)
250 {
251 	struct dm_device_zone_count *zc = data;
252 
253 	if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
254 		zc->total_nr_seq_zones++;
255 		if (zone->start >= zc->start &&
256 		    zone->start < zc->start + zc->len)
257 			zc->target_nr_seq_zones++;
258 	}
259 
260 	return 0;
261 }
262 
263 static int dm_device_count_zones(struct dm_dev *dev,
264 				 struct dm_device_zone_count *zc)
265 {
266 	int ret;
267 
268 	ret = blkdev_report_zones(dev->bdev, 0, BLK_ALL_ZONES,
269 				  dm_device_count_zones_cb, zc);
270 	if (ret < 0)
271 		return ret;
272 	if (!ret)
273 		return -EIO;
274 	return 0;
275 }
276 
277 struct dm_zone_resource_limits {
278 	unsigned int mapped_nr_seq_zones;
279 	struct queue_limits *lim;
280 	bool reliable_limits;
281 };
282 
283 static int device_get_zone_resource_limits(struct dm_target *ti,
284 					   struct dm_dev *dev, sector_t start,
285 					   sector_t len, void *data)
286 {
287 	struct dm_zone_resource_limits *zlim = data;
288 	struct gendisk *disk = dev->bdev->bd_disk;
289 	unsigned int max_open_zones, max_active_zones;
290 	int ret;
291 	struct dm_device_zone_count zc = {
292 		.start = start,
293 		.len = len,
294 	};
295 
296 	/*
297 	 * If the target is not the whole device, the device zone resources may
298 	 * be shared between different targets. Check this by counting the
299 	 * number of mapped sequential zones: if this number is smaller than the
300 	 * total number of sequential zones of the target device, then resource
301 	 * sharing may happen and the zone limits will not be reliable.
302 	 */
303 	ret = dm_device_count_zones(dev, &zc);
304 	if (ret) {
305 		DMERR("Count %s zones failed %d", disk->disk_name, ret);
306 		return ret;
307 	}
308 
309 	/*
310 	 * If the target does not map any sequential zones, then we do not need
311 	 * any zone resource limits.
312 	 */
313 	if (!zc.target_nr_seq_zones)
314 		return 0;
315 
316 	/*
317 	 * If the target does not map all sequential zones, the limits
318 	 * will not be reliable and we cannot use REQ_OP_ZONE_RESET_ALL.
319 	 */
320 	if (zc.target_nr_seq_zones < zc.total_nr_seq_zones) {
321 		zlim->reliable_limits = false;
322 		ti->zone_reset_all_supported = false;
323 	}
324 
325 	/*
326 	 * If the target maps less sequential zones than the limit values, then
327 	 * we do not have limits for this target.
328 	 */
329 	max_active_zones = disk->queue->limits.max_active_zones;
330 	if (max_active_zones >= zc.target_nr_seq_zones)
331 		max_active_zones = 0;
332 	zlim->lim->max_active_zones =
333 		min_not_zero(max_active_zones, zlim->lim->max_active_zones);
334 
335 	max_open_zones = disk->queue->limits.max_open_zones;
336 	if (max_open_zones >= zc.target_nr_seq_zones)
337 		max_open_zones = 0;
338 	zlim->lim->max_open_zones =
339 		min_not_zero(max_open_zones, zlim->lim->max_open_zones);
340 
341 	/*
342 	 * Also count the total number of sequential zones for the mapped
343 	 * device so that when we are done inspecting all its targets, we are
344 	 * able to check if the mapped device actually has any sequential zones.
345 	 */
346 	zlim->mapped_nr_seq_zones += zc.target_nr_seq_zones;
347 
348 	return 0;
349 }
350 
351 int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q,
352 		struct queue_limits *lim)
353 {
354 	struct mapped_device *md = t->md;
355 	struct gendisk *disk = md->disk;
356 	struct dm_zone_resource_limits zlim = {
357 		.reliable_limits = true,
358 		.lim = lim,
359 	};
360 
361 	/*
362 	 * Check if zone append is natively supported, and if not, set the
363 	 * mapped device queue as needing zone append emulation. If zone
364 	 * append is natively supported, make sure that
365 	 * max_hw_zone_append_sectors is not set to 0.
366 	 */
367 	WARN_ON_ONCE(queue_is_mq(q));
368 	if (!dm_table_supports_zone_append(t))
369 		lim->max_hw_zone_append_sectors = 0;
370 	else if (lim->max_hw_zone_append_sectors == 0)
371 		lim->max_hw_zone_append_sectors = lim->max_zone_append_sectors;
372 
373 	/*
374 	 * Determine the max open and max active zone limits for the mapped
375 	 * device by inspecting the zone resource limits and the zones mapped
376 	 * by each target.
377 	 */
378 	for (unsigned int i = 0; i < t->num_targets; i++) {
379 		struct dm_target *ti = dm_table_get_target(t, i);
380 
381 		/*
382 		 * Assume that the target can accept REQ_OP_ZONE_RESET_ALL.
383 		 * device_get_zone_resource_limits() may adjust this if one of
384 		 * the device used by the target does not have all its
385 		 * sequential write required zones mapped.
386 		 */
387 		ti->zone_reset_all_supported = true;
388 
389 		if (!ti->type->iterate_devices ||
390 		    ti->type->iterate_devices(ti,
391 				device_get_zone_resource_limits, &zlim)) {
392 			DMERR("Could not determine %s zone resource limits",
393 			      disk->disk_name);
394 			return -ENODEV;
395 		}
396 	}
397 
398 	/*
399 	 * If we only have conventional zones mapped, expose the mapped device
400 	 + as a regular device.
401 	 */
402 	if (!zlim.mapped_nr_seq_zones) {
403 		lim->max_open_zones = 0;
404 		lim->max_active_zones = 0;
405 		lim->max_hw_zone_append_sectors = 0;
406 		lim->max_zone_append_sectors = 0;
407 		lim->zone_write_granularity = 0;
408 		lim->chunk_sectors = 0;
409 		lim->features &= ~BLK_FEAT_ZONED;
410 		return 0;
411 	}
412 
413 	if (get_capacity(disk) && dm_has_zone_plugs(t->md)) {
414 		if (q->limits.chunk_sectors != lim->chunk_sectors) {
415 			DMWARN("%s: device has zone write plug resources. "
416 			       "Cannot change zone size",
417 			       disk->disk_name);
418 			return -EINVAL;
419 		}
420 		if (lim->max_hw_zone_append_sectors != 0 &&
421 		    !dm_table_is_wildcard(t)) {
422 			DMWARN("%s: device has zone write plug resources. "
423 			       "New table must emulate zone append",
424 			       disk->disk_name);
425 			return -EINVAL;
426 		}
427 	}
428 	/*
429 	 * Warn once (when the capacity is not yet set) if the mapped device is
430 	 * partially using zone resources of the target devices as that leads to
431 	 * unreliable limits, i.e. if another mapped device uses the same
432 	 * underlying devices, we cannot enforce zone limits to guarantee that
433 	 * writing will not lead to errors. Note that we really should return
434 	 * an error for such case but there is no easy way to find out if
435 	 * another mapped device uses the same underlying zoned devices.
436 	 */
437 	if (!get_capacity(disk) && !zlim.reliable_limits)
438 		DMWARN("%s zone resource limits may be unreliable",
439 		       disk->disk_name);
440 
441 	if (lim->features & BLK_FEAT_ZONED &&
442 	    !static_key_enabled(&zoned_enabled.key))
443 		static_branch_enable(&zoned_enabled);
444 	return 0;
445 }
446 
447 void dm_finalize_zone_settings(struct dm_table *t, struct queue_limits *lim)
448 {
449 	struct mapped_device *md = t->md;
450 
451 	if (lim->features & BLK_FEAT_ZONED) {
452 		assign_bit(DMF_EMULATE_ZONE_APPEND, &md->flags,
453 			   !dm_table_supports_zone_append(t));
454 	} else {
455 		clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
456 		md->disk->nr_zones = 0;
457 	}
458 }
459 
460 
461 /*
462  * IO completion callback called from clone_endio().
463  */
464 void dm_zone_endio(struct dm_io *io, struct bio *clone)
465 {
466 	struct mapped_device *md = io->md;
467 	struct gendisk *disk = md->disk;
468 	struct bio *orig_bio = io->orig_bio;
469 
470 	/*
471 	 * Get the offset within the zone of the written sector
472 	 * and add that to the original bio sector position.
473 	 */
474 	if (clone->bi_status == BLK_STS_OK &&
475 	    bio_op(clone) == REQ_OP_ZONE_APPEND) {
476 		orig_bio->bi_iter.bi_sector +=
477 			bdev_offset_from_zone_start(disk->part0,
478 						    clone->bi_iter.bi_sector);
479 	}
480 }
481 
482 static int dm_zone_need_reset_cb(struct blk_zone *zone, unsigned int idx,
483 				 void *data)
484 {
485 	/*
486 	 * For an all-zones reset, ignore conventional, empty, read-only
487 	 * and offline zones.
488 	 */
489 	switch (zone->cond) {
490 	case BLK_ZONE_COND_NOT_WP:
491 	case BLK_ZONE_COND_EMPTY:
492 	case BLK_ZONE_COND_READONLY:
493 	case BLK_ZONE_COND_OFFLINE:
494 		return 0;
495 	default:
496 		set_bit(idx, (unsigned long *)data);
497 		return 0;
498 	}
499 }
500 
501 int dm_zone_get_reset_bitmap(struct mapped_device *md, struct dm_table *t,
502 			     sector_t sector, unsigned int nr_zones,
503 			     unsigned long *need_reset)
504 {
505 	struct dm_report_zones_args args = {
506 		.disk = md->disk,
507 		.next_sector = sector,
508 		.cb = dm_zone_need_reset_cb,
509 		.data = need_reset,
510 	};
511 	int ret;
512 
513 	ret = dm_blk_do_report_zones(md, t, nr_zones, &args);
514 	if (ret != nr_zones) {
515 		DMERR("Get %s zone reset bitmap failed\n",
516 		      md->disk->disk_name);
517 		return -EIO;
518 	}
519 
520 	return 0;
521 }
522