13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
27fc18728SDamien Le Moal /*
37fc18728SDamien Le Moal * Copyright (C) 2021 Western Digital Corporation or its affiliates.
47fc18728SDamien Le Moal */
57fc18728SDamien Le Moal
67fc18728SDamien Le Moal #include <linux/blkdev.h>
7bb37d772SDamien Le Moal #include <linux/mm.h>
8bb37d772SDamien Le Moal #include <linux/sched/mm.h>
9bb37d772SDamien Le Moal #include <linux/slab.h>
10*e118029cSChristophe JAILLET #include <linux/bitmap.h>
117fc18728SDamien Le Moal
127fc18728SDamien Le Moal #include "dm-core.h"
137fc18728SDamien Le Moal
14bb37d772SDamien Le Moal #define DM_MSG_PREFIX "zone"
15bb37d772SDamien Le Moal
16bb37d772SDamien Le Moal #define DM_ZONE_INVALID_WP_OFST UINT_MAX
17bb37d772SDamien Le Moal
18bb37d772SDamien Le Moal /*
19bb37d772SDamien Le Moal * For internal zone reports bypassing the top BIO submission path.
20bb37d772SDamien Le Moal */
dm_blk_do_report_zones(struct mapped_device * md,struct dm_table * t,sector_t sector,unsigned int nr_zones,report_zones_cb cb,void * data)21bb37d772SDamien Le Moal static int dm_blk_do_report_zones(struct mapped_device *md, struct dm_table *t,
22bb37d772SDamien Le Moal sector_t sector, unsigned int nr_zones,
23bb37d772SDamien Le Moal report_zones_cb cb, void *data)
24bb37d772SDamien Le Moal {
25bb37d772SDamien Le Moal struct gendisk *disk = md->disk;
26bb37d772SDamien Le Moal int ret;
27bb37d772SDamien Le Moal struct dm_report_zones_args args = {
28bb37d772SDamien Le Moal .next_sector = sector,
29bb37d772SDamien Le Moal .orig_data = data,
30bb37d772SDamien Le Moal .orig_cb = cb,
31bb37d772SDamien Le Moal };
32bb37d772SDamien Le Moal
33bb37d772SDamien Le Moal do {
34bb37d772SDamien Le Moal struct dm_target *tgt;
35bb37d772SDamien Le Moal
36bb37d772SDamien Le Moal tgt = dm_table_find_target(t, args.next_sector);
37bb37d772SDamien Le Moal if (WARN_ON_ONCE(!tgt->type->report_zones))
38bb37d772SDamien Le Moal return -EIO;
39bb37d772SDamien Le Moal
40bb37d772SDamien Le Moal args.tgt = tgt;
41bb37d772SDamien Le Moal ret = tgt->type->report_zones(tgt, &args,
42bb37d772SDamien Le Moal nr_zones - args.zone_idx);
43bb37d772SDamien Le Moal if (ret < 0)
44bb37d772SDamien Le Moal return ret;
45bb37d772SDamien Le Moal } while (args.zone_idx < nr_zones &&
46bb37d772SDamien Le Moal args.next_sector < get_capacity(disk));
47bb37d772SDamien Le Moal
48bb37d772SDamien Le Moal return args.zone_idx;
49bb37d772SDamien Le Moal }
50bb37d772SDamien Le Moal
517fc18728SDamien Le Moal /*
527fc18728SDamien Le Moal * User facing dm device block device report zone operation. This calls the
537fc18728SDamien Le Moal * report_zones operation for each target of a device table. This operation is
547fc18728SDamien Le Moal * generally implemented by targets using dm_report_zones().
557fc18728SDamien Le Moal */
dm_blk_report_zones(struct gendisk * disk,sector_t sector,unsigned int nr_zones,report_zones_cb cb,void * data)567fc18728SDamien Le Moal int dm_blk_report_zones(struct gendisk *disk, sector_t sector,
577fc18728SDamien Le Moal unsigned int nr_zones, report_zones_cb cb, void *data)
587fc18728SDamien Le Moal {
597fc18728SDamien Le Moal struct mapped_device *md = disk->private_data;
607fc18728SDamien Le Moal struct dm_table *map;
617fc18728SDamien Le Moal int srcu_idx, ret;
627fc18728SDamien Le Moal
637fc18728SDamien Le Moal if (dm_suspended_md(md))
647fc18728SDamien Le Moal return -EAGAIN;
657fc18728SDamien Le Moal
667fc18728SDamien Le Moal map = dm_get_live_table(md, &srcu_idx);
67bb37d772SDamien Le Moal if (!map)
68bb37d772SDamien Le Moal return -EIO;
697fc18728SDamien Le Moal
70bb37d772SDamien Le Moal ret = dm_blk_do_report_zones(md, map, sector, nr_zones, cb, data);
717fc18728SDamien Le Moal
727fc18728SDamien Le Moal dm_put_live_table(md, srcu_idx);
73bb37d772SDamien Le Moal
747fc18728SDamien Le Moal return ret;
757fc18728SDamien Le Moal }
767fc18728SDamien Le Moal
dm_report_zones_cb(struct blk_zone * zone,unsigned int idx,void * data)77912e8875SDamien Le Moal static int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx,
78912e8875SDamien Le Moal void *data)
797fc18728SDamien Le Moal {
807fc18728SDamien Le Moal struct dm_report_zones_args *args = data;
817fc18728SDamien Le Moal sector_t sector_diff = args->tgt->begin - args->start;
827fc18728SDamien Le Moal
837fc18728SDamien Le Moal /*
847fc18728SDamien Le Moal * Ignore zones beyond the target range.
857fc18728SDamien Le Moal */
867fc18728SDamien Le Moal if (zone->start >= args->start + args->tgt->len)
877fc18728SDamien Le Moal return 0;
887fc18728SDamien Le Moal
897fc18728SDamien Le Moal /*
907fc18728SDamien Le Moal * Remap the start sector and write pointer position of the zone
917fc18728SDamien Le Moal * to match its position in the target range.
927fc18728SDamien Le Moal */
937fc18728SDamien Le Moal zone->start += sector_diff;
947fc18728SDamien Le Moal if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
957fc18728SDamien Le Moal if (zone->cond == BLK_ZONE_COND_FULL)
967fc18728SDamien Le Moal zone->wp = zone->start + zone->len;
977fc18728SDamien Le Moal else if (zone->cond == BLK_ZONE_COND_EMPTY)
987fc18728SDamien Le Moal zone->wp = zone->start;
997fc18728SDamien Le Moal else
1007fc18728SDamien Le Moal zone->wp += sector_diff;
1017fc18728SDamien Le Moal }
1027fc18728SDamien Le Moal
1037fc18728SDamien Le Moal args->next_sector = zone->start + zone->len;
1047fc18728SDamien Le Moal return args->orig_cb(zone, args->zone_idx++, args->orig_data);
1057fc18728SDamien Le Moal }
106912e8875SDamien Le Moal
107912e8875SDamien Le Moal /*
108912e8875SDamien Le Moal * Helper for drivers of zoned targets to implement struct target_type
109912e8875SDamien Le Moal * report_zones operation.
110912e8875SDamien Le Moal */
dm_report_zones(struct block_device * bdev,sector_t start,sector_t sector,struct dm_report_zones_args * args,unsigned int nr_zones)111912e8875SDamien Le Moal int dm_report_zones(struct block_device *bdev, sector_t start, sector_t sector,
112912e8875SDamien Le Moal struct dm_report_zones_args *args, unsigned int nr_zones)
113912e8875SDamien Le Moal {
114912e8875SDamien Le Moal /*
115912e8875SDamien Le Moal * Set the target mapping start sector first so that
116912e8875SDamien Le Moal * dm_report_zones_cb() can correctly remap zone information.
117912e8875SDamien Le Moal */
118912e8875SDamien Le Moal args->start = start;
119912e8875SDamien Le Moal
120912e8875SDamien Le Moal return blkdev_report_zones(bdev, sector, nr_zones,
121912e8875SDamien Le Moal dm_report_zones_cb, args);
122912e8875SDamien Le Moal }
123912e8875SDamien Le Moal EXPORT_SYMBOL_GPL(dm_report_zones);
1247fc18728SDamien Le Moal
dm_is_zone_write(struct mapped_device * md,struct bio * bio)125bf14e2b2SDamien Le Moal bool dm_is_zone_write(struct mapped_device *md, struct bio *bio)
126bf14e2b2SDamien Le Moal {
127bf14e2b2SDamien Le Moal struct request_queue *q = md->queue;
128bf14e2b2SDamien Le Moal
129bf14e2b2SDamien Le Moal if (!blk_queue_is_zoned(q))
130bf14e2b2SDamien Le Moal return false;
131bf14e2b2SDamien Le Moal
132bf14e2b2SDamien Le Moal switch (bio_op(bio)) {
133bf14e2b2SDamien Le Moal case REQ_OP_WRITE_ZEROES:
134bf14e2b2SDamien Le Moal case REQ_OP_WRITE:
135bf14e2b2SDamien Le Moal return !op_is_flush(bio->bi_opf) && bio_sectors(bio);
136bf14e2b2SDamien Le Moal default:
137bf14e2b2SDamien Le Moal return false;
138bf14e2b2SDamien Le Moal }
139bf14e2b2SDamien Le Moal }
140bf14e2b2SDamien Le Moal
dm_cleanup_zoned_dev(struct mapped_device * md)141bb37d772SDamien Le Moal void dm_cleanup_zoned_dev(struct mapped_device *md)
1427fc18728SDamien Le Moal {
143d86e716aSChristoph Hellwig if (md->disk) {
144*e118029cSChristophe JAILLET bitmap_free(md->disk->conv_zones_bitmap);
145d86e716aSChristoph Hellwig md->disk->conv_zones_bitmap = NULL;
146*e118029cSChristophe JAILLET bitmap_free(md->disk->seq_zones_wlock);
147d86e716aSChristoph Hellwig md->disk->seq_zones_wlock = NULL;
148bb37d772SDamien Le Moal }
149bb37d772SDamien Le Moal
150bb37d772SDamien Le Moal kvfree(md->zwp_offset);
151bb37d772SDamien Le Moal md->zwp_offset = NULL;
152bb37d772SDamien Le Moal md->nr_zones = 0;
153bb37d772SDamien Le Moal }
154bb37d772SDamien Le Moal
dm_get_zone_wp_offset(struct blk_zone * zone)155bb37d772SDamien Le Moal static unsigned int dm_get_zone_wp_offset(struct blk_zone *zone)
156bb37d772SDamien Le Moal {
157bb37d772SDamien Le Moal switch (zone->cond) {
158bb37d772SDamien Le Moal case BLK_ZONE_COND_IMP_OPEN:
159bb37d772SDamien Le Moal case BLK_ZONE_COND_EXP_OPEN:
160bb37d772SDamien Le Moal case BLK_ZONE_COND_CLOSED:
161bb37d772SDamien Le Moal return zone->wp - zone->start;
162bb37d772SDamien Le Moal case BLK_ZONE_COND_FULL:
163bb37d772SDamien Le Moal return zone->len;
164bb37d772SDamien Le Moal case BLK_ZONE_COND_EMPTY:
165bb37d772SDamien Le Moal case BLK_ZONE_COND_NOT_WP:
166bb37d772SDamien Le Moal case BLK_ZONE_COND_OFFLINE:
167bb37d772SDamien Le Moal case BLK_ZONE_COND_READONLY:
168bb37d772SDamien Le Moal default:
169bb37d772SDamien Le Moal /*
170bb37d772SDamien Le Moal * Conventional, offline and read-only zones do not have a valid
171bb37d772SDamien Le Moal * write pointer. Use 0 as for an empty zone.
172bb37d772SDamien Le Moal */
173bb37d772SDamien Le Moal return 0;
174bb37d772SDamien Le Moal }
175bb37d772SDamien Le Moal }
176bb37d772SDamien Le Moal
dm_zone_revalidate_cb(struct blk_zone * zone,unsigned int idx,void * data)177bb37d772SDamien Le Moal static int dm_zone_revalidate_cb(struct blk_zone *zone, unsigned int idx,
178bb37d772SDamien Le Moal void *data)
179bb37d772SDamien Le Moal {
180bb37d772SDamien Le Moal struct mapped_device *md = data;
181d86e716aSChristoph Hellwig struct gendisk *disk = md->disk;
182bb37d772SDamien Le Moal
183bb37d772SDamien Le Moal switch (zone->type) {
184bb37d772SDamien Le Moal case BLK_ZONE_TYPE_CONVENTIONAL:
185d86e716aSChristoph Hellwig if (!disk->conv_zones_bitmap) {
186*e118029cSChristophe JAILLET disk->conv_zones_bitmap = bitmap_zalloc(disk->nr_zones,
187*e118029cSChristophe JAILLET GFP_NOIO);
188d86e716aSChristoph Hellwig if (!disk->conv_zones_bitmap)
189bb37d772SDamien Le Moal return -ENOMEM;
190bb37d772SDamien Le Moal }
191d86e716aSChristoph Hellwig set_bit(idx, disk->conv_zones_bitmap);
192bb37d772SDamien Le Moal break;
193bb37d772SDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_REQ:
194bb37d772SDamien Le Moal case BLK_ZONE_TYPE_SEQWRITE_PREF:
195d86e716aSChristoph Hellwig if (!disk->seq_zones_wlock) {
196*e118029cSChristophe JAILLET disk->seq_zones_wlock = bitmap_zalloc(disk->nr_zones,
197*e118029cSChristophe JAILLET GFP_NOIO);
198d86e716aSChristoph Hellwig if (!disk->seq_zones_wlock)
199bb37d772SDamien Le Moal return -ENOMEM;
200bb37d772SDamien Le Moal }
201bb37d772SDamien Le Moal if (!md->zwp_offset) {
202bb37d772SDamien Le Moal md->zwp_offset =
203d86e716aSChristoph Hellwig kvcalloc(disk->nr_zones, sizeof(unsigned int),
20428436ba3SDamien Le Moal GFP_KERNEL);
205bb37d772SDamien Le Moal if (!md->zwp_offset)
206bb37d772SDamien Le Moal return -ENOMEM;
207bb37d772SDamien Le Moal }
208bb37d772SDamien Le Moal md->zwp_offset[idx] = dm_get_zone_wp_offset(zone);
209bb37d772SDamien Le Moal
210bb37d772SDamien Le Moal break;
211bb37d772SDamien Le Moal default:
212bb37d772SDamien Le Moal DMERR("Invalid zone type 0x%x at sectors %llu",
213bb37d772SDamien Le Moal (int)zone->type, zone->start);
214bb37d772SDamien Le Moal return -ENODEV;
215bb37d772SDamien Le Moal }
216bb37d772SDamien Le Moal
217bb37d772SDamien Le Moal return 0;
218bb37d772SDamien Le Moal }
219bb37d772SDamien Le Moal
220bb37d772SDamien Le Moal /*
221bb37d772SDamien Le Moal * Revalidate the zones of a mapped device to initialize resource necessary
222bb37d772SDamien Le Moal * for zone append emulation. Note that we cannot simply use the block layer
223bb37d772SDamien Le Moal * blk_revalidate_disk_zones() function here as the mapped device is suspended
224bb37d772SDamien Le Moal * (this is called from __bind() context).
225bb37d772SDamien Le Moal */
dm_revalidate_zones(struct mapped_device * md,struct dm_table * t)226bb37d772SDamien Le Moal static int dm_revalidate_zones(struct mapped_device *md, struct dm_table *t)
227bb37d772SDamien Le Moal {
228d86e716aSChristoph Hellwig struct gendisk *disk = md->disk;
22928436ba3SDamien Le Moal unsigned int noio_flag;
230bb37d772SDamien Le Moal int ret;
231bb37d772SDamien Le Moal
232bb37d772SDamien Le Moal /*
233bb37d772SDamien Le Moal * Check if something changed. If yes, cleanup the current resources
234bb37d772SDamien Le Moal * and reallocate everything.
235bb37d772SDamien Le Moal */
236d86e716aSChristoph Hellwig if (!disk->nr_zones || disk->nr_zones != md->nr_zones)
237bb37d772SDamien Le Moal dm_cleanup_zoned_dev(md);
238bb37d772SDamien Le Moal if (md->nr_zones)
239bb37d772SDamien Le Moal return 0;
240bb37d772SDamien Le Moal
24128436ba3SDamien Le Moal /*
24228436ba3SDamien Le Moal * Scan all zones to initialize everything. Ensure that all vmalloc
24328436ba3SDamien Le Moal * operations in this context are done as if GFP_NOIO was specified.
24428436ba3SDamien Le Moal */
24528436ba3SDamien Le Moal noio_flag = memalloc_noio_save();
246d86e716aSChristoph Hellwig ret = dm_blk_do_report_zones(md, t, 0, disk->nr_zones,
247bb37d772SDamien Le Moal dm_zone_revalidate_cb, md);
24828436ba3SDamien Le Moal memalloc_noio_restore(noio_flag);
249bb37d772SDamien Le Moal if (ret < 0)
250bb37d772SDamien Le Moal goto err;
251d86e716aSChristoph Hellwig if (ret != disk->nr_zones) {
252bb37d772SDamien Le Moal ret = -EIO;
253bb37d772SDamien Le Moal goto err;
254bb37d772SDamien Le Moal }
255bb37d772SDamien Le Moal
256d86e716aSChristoph Hellwig md->nr_zones = disk->nr_zones;
257bb37d772SDamien Le Moal
258bb37d772SDamien Le Moal return 0;
259bb37d772SDamien Le Moal
260bb37d772SDamien Le Moal err:
261bb37d772SDamien Le Moal DMERR("Revalidate zones failed %d", ret);
262bb37d772SDamien Le Moal dm_cleanup_zoned_dev(md);
263bb37d772SDamien Le Moal return ret;
264bb37d772SDamien Le Moal }
265bb37d772SDamien Le Moal
device_not_zone_append_capable(struct dm_target * ti,struct dm_dev * dev,sector_t start,sector_t len,void * data)266bb37d772SDamien Le Moal static int device_not_zone_append_capable(struct dm_target *ti,
267bb37d772SDamien Le Moal struct dm_dev *dev, sector_t start,
268bb37d772SDamien Le Moal sector_t len, void *data)
269bb37d772SDamien Le Moal {
270edd1dbc8SChristoph Hellwig return !bdev_is_zoned(dev->bdev);
271bb37d772SDamien Le Moal }
272bb37d772SDamien Le Moal
dm_table_supports_zone_append(struct dm_table * t)273bb37d772SDamien Le Moal static bool dm_table_supports_zone_append(struct dm_table *t)
274bb37d772SDamien Le Moal {
275564b5c54SMike Snitzer for (unsigned int i = 0; i < t->num_targets; i++) {
276564b5c54SMike Snitzer struct dm_target *ti = dm_table_get_target(t, i);
277bb37d772SDamien Le Moal
278bb37d772SDamien Le Moal if (ti->emulate_zone_append)
279bb37d772SDamien Le Moal return false;
280bb37d772SDamien Le Moal
281bb37d772SDamien Le Moal if (!ti->type->iterate_devices ||
282bb37d772SDamien Le Moal ti->type->iterate_devices(ti, device_not_zone_append_capable, NULL))
283bb37d772SDamien Le Moal return false;
284bb37d772SDamien Le Moal }
285bb37d772SDamien Le Moal
286bb37d772SDamien Le Moal return true;
287bb37d772SDamien Le Moal }
288bb37d772SDamien Le Moal
dm_set_zones_restrictions(struct dm_table * t,struct request_queue * q)289bb37d772SDamien Le Moal int dm_set_zones_restrictions(struct dm_table *t, struct request_queue *q)
290bb37d772SDamien Le Moal {
291bb37d772SDamien Le Moal struct mapped_device *md = t->md;
2927fc18728SDamien Le Moal
2937fc18728SDamien Le Moal /*
2947fc18728SDamien Le Moal * For a zoned target, the number of zones should be updated for the
295bb37d772SDamien Le Moal * correct value to be exposed in sysfs queue/nr_zones.
2967fc18728SDamien Le Moal */
2977fc18728SDamien Le Moal WARN_ON_ONCE(queue_is_mq(q));
298d86e716aSChristoph Hellwig md->disk->nr_zones = bdev_nr_zones(md->disk->part0);
299bb37d772SDamien Le Moal
300bb37d772SDamien Le Moal /* Check if zone append is natively supported */
301bb37d772SDamien Le Moal if (dm_table_supports_zone_append(t)) {
302bb37d772SDamien Le Moal clear_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
303bb37d772SDamien Le Moal dm_cleanup_zoned_dev(md);
304bb37d772SDamien Le Moal return 0;
305bb37d772SDamien Le Moal }
306bb37d772SDamien Le Moal
307bb37d772SDamien Le Moal /*
308bb37d772SDamien Le Moal * Mark the mapped device as needing zone append emulation and
309bb37d772SDamien Le Moal * initialize the emulation resources once the capacity is set.
310bb37d772SDamien Le Moal */
311bb37d772SDamien Le Moal set_bit(DMF_EMULATE_ZONE_APPEND, &md->flags);
312bb37d772SDamien Le Moal if (!get_capacity(md->disk))
313bb37d772SDamien Le Moal return 0;
314bb37d772SDamien Le Moal
315bb37d772SDamien Le Moal return dm_revalidate_zones(md, t);
316bb37d772SDamien Le Moal }
317bb37d772SDamien Le Moal
dm_update_zone_wp_offset_cb(struct blk_zone * zone,unsigned int idx,void * data)318bb37d772SDamien Le Moal static int dm_update_zone_wp_offset_cb(struct blk_zone *zone, unsigned int idx,
319bb37d772SDamien Le Moal void *data)
320bb37d772SDamien Le Moal {
321bb37d772SDamien Le Moal unsigned int *wp_offset = data;
322bb37d772SDamien Le Moal
323bb37d772SDamien Le Moal *wp_offset = dm_get_zone_wp_offset(zone);
324bb37d772SDamien Le Moal
325bb37d772SDamien Le Moal return 0;
326bb37d772SDamien Le Moal }
327bb37d772SDamien Le Moal
dm_update_zone_wp_offset(struct mapped_device * md,unsigned int zno,unsigned int * wp_ofst)328bb37d772SDamien Le Moal static int dm_update_zone_wp_offset(struct mapped_device *md, unsigned int zno,
329bb37d772SDamien Le Moal unsigned int *wp_ofst)
330bb37d772SDamien Le Moal {
331de71973cSChristoph Hellwig sector_t sector = zno * bdev_zone_sectors(md->disk->part0);
332bb37d772SDamien Le Moal unsigned int noio_flag;
333bb37d772SDamien Le Moal struct dm_table *t;
334bb37d772SDamien Le Moal int srcu_idx, ret;
335bb37d772SDamien Le Moal
336bb37d772SDamien Le Moal t = dm_get_live_table(md, &srcu_idx);
337bb37d772SDamien Le Moal if (!t)
338bb37d772SDamien Le Moal return -EIO;
339bb37d772SDamien Le Moal
340bb37d772SDamien Le Moal /*
341bb37d772SDamien Le Moal * Ensure that all memory allocations in this context are done as if
342bb37d772SDamien Le Moal * GFP_NOIO was specified.
343bb37d772SDamien Le Moal */
344bb37d772SDamien Le Moal noio_flag = memalloc_noio_save();
345bb37d772SDamien Le Moal ret = dm_blk_do_report_zones(md, t, sector, 1,
346bb37d772SDamien Le Moal dm_update_zone_wp_offset_cb, wp_ofst);
347bb37d772SDamien Le Moal memalloc_noio_restore(noio_flag);
348bb37d772SDamien Le Moal
349bb37d772SDamien Le Moal dm_put_live_table(md, srcu_idx);
350bb37d772SDamien Le Moal
351bb37d772SDamien Le Moal if (ret != 1)
352bb37d772SDamien Le Moal return -EIO;
353bb37d772SDamien Le Moal
354bb37d772SDamien Le Moal return 0;
355bb37d772SDamien Le Moal }
356bb37d772SDamien Le Moal
35773d7b06eSMike Snitzer struct orig_bio_details {
3588a5a7ce8SBart Van Assche enum req_op op;
35973d7b06eSMike Snitzer unsigned int nr_sectors;
36073d7b06eSMike Snitzer };
36173d7b06eSMike Snitzer
362bb37d772SDamien Le Moal /*
363bb37d772SDamien Le Moal * First phase of BIO mapping for targets with zone append emulation:
364bb37d772SDamien Le Moal * check all BIO that change a zone writer pointer and change zone
365bb37d772SDamien Le Moal * append operations into regular write operations.
366bb37d772SDamien Le Moal */
dm_zone_map_bio_begin(struct mapped_device * md,unsigned int zno,struct bio * clone)367bb37d772SDamien Le Moal static bool dm_zone_map_bio_begin(struct mapped_device *md,
36873d7b06eSMike Snitzer unsigned int zno, struct bio *clone)
369bb37d772SDamien Le Moal {
370de71973cSChristoph Hellwig sector_t zsectors = bdev_zone_sectors(md->disk->part0);
371bb37d772SDamien Le Moal unsigned int zwp_offset = READ_ONCE(md->zwp_offset[zno]);
372bb37d772SDamien Le Moal
373bb37d772SDamien Le Moal /*
374bb37d772SDamien Le Moal * If the target zone is in an error state, recover by inspecting the
375bb37d772SDamien Le Moal * zone to get its current write pointer position. Note that since the
376bb37d772SDamien Le Moal * target zone is already locked, a BIO issuing context should never
377bb37d772SDamien Le Moal * see the zone write in the DM_ZONE_UPDATING_WP_OFST state.
378bb37d772SDamien Le Moal */
379bb37d772SDamien Le Moal if (zwp_offset == DM_ZONE_INVALID_WP_OFST) {
380bb37d772SDamien Le Moal if (dm_update_zone_wp_offset(md, zno, &zwp_offset))
381bb37d772SDamien Le Moal return false;
382bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno], zwp_offset);
383bb37d772SDamien Le Moal }
384bb37d772SDamien Le Moal
38573d7b06eSMike Snitzer switch (bio_op(clone)) {
386bb37d772SDamien Le Moal case REQ_OP_ZONE_RESET:
387bb37d772SDamien Le Moal case REQ_OP_ZONE_FINISH:
388bb37d772SDamien Le Moal return true;
389bb37d772SDamien Le Moal case REQ_OP_WRITE_ZEROES:
390bb37d772SDamien Le Moal case REQ_OP_WRITE:
391bb37d772SDamien Le Moal /* Writes must be aligned to the zone write pointer */
392bb37d772SDamien Le Moal if ((clone->bi_iter.bi_sector & (zsectors - 1)) != zwp_offset)
393bb37d772SDamien Le Moal return false;
394bb37d772SDamien Le Moal break;
395bb37d772SDamien Le Moal case REQ_OP_ZONE_APPEND:
396bb37d772SDamien Le Moal /*
397bb37d772SDamien Le Moal * Change zone append operations into a non-mergeable regular
398bb37d772SDamien Le Moal * writes directed at the current write pointer position of the
399bb37d772SDamien Le Moal * target zone.
400bb37d772SDamien Le Moal */
401bb37d772SDamien Le Moal clone->bi_opf = REQ_OP_WRITE | REQ_NOMERGE |
40273d7b06eSMike Snitzer (clone->bi_opf & (~REQ_OP_MASK));
40373d7b06eSMike Snitzer clone->bi_iter.bi_sector += zwp_offset;
404bb37d772SDamien Le Moal break;
405bb37d772SDamien Le Moal default:
406bb37d772SDamien Le Moal DMWARN_LIMIT("Invalid BIO operation");
407bb37d772SDamien Le Moal return false;
408bb37d772SDamien Le Moal }
409bb37d772SDamien Le Moal
410bb37d772SDamien Le Moal /* Cannot write to a full zone */
411bb37d772SDamien Le Moal if (zwp_offset >= zsectors)
412bb37d772SDamien Le Moal return false;
413bb37d772SDamien Le Moal
414bb37d772SDamien Le Moal return true;
415bb37d772SDamien Le Moal }
416bb37d772SDamien Le Moal
417bb37d772SDamien Le Moal /*
418bb37d772SDamien Le Moal * Second phase of BIO mapping for targets with zone append emulation:
419bb37d772SDamien Le Moal * update the zone write pointer offset array to account for the additional
420bb37d772SDamien Le Moal * data written to a zone. Note that at this point, the remapped clone BIO
421bb37d772SDamien Le Moal * may already have completed, so we do not touch it.
422bb37d772SDamien Le Moal */
dm_zone_map_bio_end(struct mapped_device * md,unsigned int zno,struct orig_bio_details * orig_bio_details,unsigned int nr_sectors)42373d7b06eSMike Snitzer static blk_status_t dm_zone_map_bio_end(struct mapped_device *md, unsigned int zno,
42473d7b06eSMike Snitzer struct orig_bio_details *orig_bio_details,
425bb37d772SDamien Le Moal unsigned int nr_sectors)
426bb37d772SDamien Le Moal {
427bb37d772SDamien Le Moal unsigned int zwp_offset = READ_ONCE(md->zwp_offset[zno]);
428bb37d772SDamien Le Moal
429bb37d772SDamien Le Moal /* The clone BIO may already have been completed and failed */
430bb37d772SDamien Le Moal if (zwp_offset == DM_ZONE_INVALID_WP_OFST)
431bb37d772SDamien Le Moal return BLK_STS_IOERR;
432bb37d772SDamien Le Moal
433bb37d772SDamien Le Moal /* Update the zone wp offset */
43473d7b06eSMike Snitzer switch (orig_bio_details->op) {
435bb37d772SDamien Le Moal case REQ_OP_ZONE_RESET:
436bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno], 0);
437bb37d772SDamien Le Moal return BLK_STS_OK;
438bb37d772SDamien Le Moal case REQ_OP_ZONE_FINISH:
439bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno],
440de71973cSChristoph Hellwig bdev_zone_sectors(md->disk->part0));
441bb37d772SDamien Le Moal return BLK_STS_OK;
442bb37d772SDamien Le Moal case REQ_OP_WRITE_ZEROES:
443bb37d772SDamien Le Moal case REQ_OP_WRITE:
444bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno], zwp_offset + nr_sectors);
445bb37d772SDamien Le Moal return BLK_STS_OK;
446bb37d772SDamien Le Moal case REQ_OP_ZONE_APPEND:
447bb37d772SDamien Le Moal /*
448bb37d772SDamien Le Moal * Check that the target did not truncate the write operation
449bb37d772SDamien Le Moal * emulating a zone append.
450bb37d772SDamien Le Moal */
45173d7b06eSMike Snitzer if (nr_sectors != orig_bio_details->nr_sectors) {
452bb37d772SDamien Le Moal DMWARN_LIMIT("Truncated write for zone append");
453bb37d772SDamien Le Moal return BLK_STS_IOERR;
454bb37d772SDamien Le Moal }
455bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno], zwp_offset + nr_sectors);
456bb37d772SDamien Le Moal return BLK_STS_OK;
457bb37d772SDamien Le Moal default:
458bb37d772SDamien Le Moal DMWARN_LIMIT("Invalid BIO operation");
459bb37d772SDamien Le Moal return BLK_STS_IOERR;
460bb37d772SDamien Le Moal }
461bb37d772SDamien Le Moal }
462bb37d772SDamien Le Moal
dm_zone_lock(struct gendisk * disk,unsigned int zno,struct bio * clone)463d86e716aSChristoph Hellwig static inline void dm_zone_lock(struct gendisk *disk, unsigned int zno,
464d86e716aSChristoph Hellwig struct bio *clone)
465bb37d772SDamien Le Moal {
466bb37d772SDamien Le Moal if (WARN_ON_ONCE(bio_flagged(clone, BIO_ZONE_WRITE_LOCKED)))
467bb37d772SDamien Le Moal return;
468bb37d772SDamien Le Moal
469d86e716aSChristoph Hellwig wait_on_bit_lock_io(disk->seq_zones_wlock, zno, TASK_UNINTERRUPTIBLE);
470bb37d772SDamien Le Moal bio_set_flag(clone, BIO_ZONE_WRITE_LOCKED);
471bb37d772SDamien Le Moal }
472bb37d772SDamien Le Moal
dm_zone_unlock(struct gendisk * disk,unsigned int zno,struct bio * clone)473d86e716aSChristoph Hellwig static inline void dm_zone_unlock(struct gendisk *disk, unsigned int zno,
474d86e716aSChristoph Hellwig struct bio *clone)
475bb37d772SDamien Le Moal {
476bb37d772SDamien Le Moal if (!bio_flagged(clone, BIO_ZONE_WRITE_LOCKED))
477bb37d772SDamien Le Moal return;
478bb37d772SDamien Le Moal
479d86e716aSChristoph Hellwig WARN_ON_ONCE(!test_bit(zno, disk->seq_zones_wlock));
480d86e716aSChristoph Hellwig clear_bit_unlock(zno, disk->seq_zones_wlock);
481bb37d772SDamien Le Moal smp_mb__after_atomic();
482d86e716aSChristoph Hellwig wake_up_bit(disk->seq_zones_wlock, zno);
483bb37d772SDamien Le Moal
484bb37d772SDamien Le Moal bio_clear_flag(clone, BIO_ZONE_WRITE_LOCKED);
485bb37d772SDamien Le Moal }
486bb37d772SDamien Le Moal
dm_need_zone_wp_tracking(struct bio * bio)48773d7b06eSMike Snitzer static bool dm_need_zone_wp_tracking(struct bio *bio)
488bb37d772SDamien Le Moal {
489bb37d772SDamien Le Moal /*
490bb37d772SDamien Le Moal * Special processing is not needed for operations that do not need the
491bb37d772SDamien Le Moal * zone write lock, that is, all operations that target conventional
492bb37d772SDamien Le Moal * zones and all operations that do not modify directly a sequential
493bb37d772SDamien Le Moal * zone write pointer.
494bb37d772SDamien Le Moal */
49573d7b06eSMike Snitzer if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
496bb37d772SDamien Le Moal return false;
49773d7b06eSMike Snitzer switch (bio_op(bio)) {
498bb37d772SDamien Le Moal case REQ_OP_WRITE_ZEROES:
499bb37d772SDamien Le Moal case REQ_OP_WRITE:
500bb37d772SDamien Le Moal case REQ_OP_ZONE_RESET:
501bb37d772SDamien Le Moal case REQ_OP_ZONE_FINISH:
502bb37d772SDamien Le Moal case REQ_OP_ZONE_APPEND:
50373d7b06eSMike Snitzer return bio_zone_is_seq(bio);
504bb37d772SDamien Le Moal default:
505bb37d772SDamien Le Moal return false;
506bb37d772SDamien Le Moal }
507bb37d772SDamien Le Moal }
508bb37d772SDamien Le Moal
509bb37d772SDamien Le Moal /*
510bb37d772SDamien Le Moal * Special IO mapping for targets needing zone append emulation.
511bb37d772SDamien Le Moal */
dm_zone_map_bio(struct dm_target_io * tio)512bb37d772SDamien Le Moal int dm_zone_map_bio(struct dm_target_io *tio)
513bb37d772SDamien Le Moal {
514bb37d772SDamien Le Moal struct dm_io *io = tio->io;
515bb37d772SDamien Le Moal struct dm_target *ti = tio->ti;
516bb37d772SDamien Le Moal struct mapped_device *md = io->md;
517bb37d772SDamien Le Moal struct bio *clone = &tio->clone;
51873d7b06eSMike Snitzer struct orig_bio_details orig_bio_details;
519bb37d772SDamien Le Moal unsigned int zno;
520bb37d772SDamien Le Moal blk_status_t sts;
521bb37d772SDamien Le Moal int r;
522bb37d772SDamien Le Moal
523bb37d772SDamien Le Moal /*
524bb37d772SDamien Le Moal * IOs that do not change a zone write pointer do not need
525bb37d772SDamien Le Moal * any additional special processing.
526bb37d772SDamien Le Moal */
52773d7b06eSMike Snitzer if (!dm_need_zone_wp_tracking(clone))
528bb37d772SDamien Le Moal return ti->type->map(ti, clone);
529bb37d772SDamien Le Moal
530bb37d772SDamien Le Moal /* Lock the target zone */
53173d7b06eSMike Snitzer zno = bio_zone_no(clone);
532d86e716aSChristoph Hellwig dm_zone_lock(md->disk, zno, clone);
533bb37d772SDamien Le Moal
53473d7b06eSMike Snitzer orig_bio_details.nr_sectors = bio_sectors(clone);
53573d7b06eSMike Snitzer orig_bio_details.op = bio_op(clone);
53673d7b06eSMike Snitzer
537bb37d772SDamien Le Moal /*
538bb37d772SDamien Le Moal * Check that the bio and the target zone write pointer offset are
539bb37d772SDamien Le Moal * both valid, and if the bio is a zone append, remap it to a write.
540bb37d772SDamien Le Moal */
54173d7b06eSMike Snitzer if (!dm_zone_map_bio_begin(md, zno, clone)) {
542d86e716aSChristoph Hellwig dm_zone_unlock(md->disk, zno, clone);
543bb37d772SDamien Le Moal return DM_MAPIO_KILL;
544bb37d772SDamien Le Moal }
545bb37d772SDamien Le Moal
546bb37d772SDamien Le Moal /* Let the target do its work */
547bb37d772SDamien Le Moal r = ti->type->map(ti, clone);
548bb37d772SDamien Le Moal switch (r) {
549bb37d772SDamien Le Moal case DM_MAPIO_SUBMITTED:
550bb37d772SDamien Le Moal /*
551bb37d772SDamien Le Moal * The target submitted the clone BIO. The target zone will
552bb37d772SDamien Le Moal * be unlocked on completion of the clone.
553bb37d772SDamien Le Moal */
55473d7b06eSMike Snitzer sts = dm_zone_map_bio_end(md, zno, &orig_bio_details,
55573d7b06eSMike Snitzer *tio->len_ptr);
556bb37d772SDamien Le Moal break;
557bb37d772SDamien Le Moal case DM_MAPIO_REMAPPED:
558bb37d772SDamien Le Moal /*
559bb37d772SDamien Le Moal * The target only remapped the clone BIO. In case of error,
560bb37d772SDamien Le Moal * unlock the target zone here as the clone will not be
561bb37d772SDamien Le Moal * submitted.
562bb37d772SDamien Le Moal */
56373d7b06eSMike Snitzer sts = dm_zone_map_bio_end(md, zno, &orig_bio_details,
56473d7b06eSMike Snitzer *tio->len_ptr);
565bb37d772SDamien Le Moal if (sts != BLK_STS_OK)
566d86e716aSChristoph Hellwig dm_zone_unlock(md->disk, zno, clone);
567bb37d772SDamien Le Moal break;
568bb37d772SDamien Le Moal case DM_MAPIO_REQUEUE:
569bb37d772SDamien Le Moal case DM_MAPIO_KILL:
570bb37d772SDamien Le Moal default:
571d86e716aSChristoph Hellwig dm_zone_unlock(md->disk, zno, clone);
572bb37d772SDamien Le Moal sts = BLK_STS_IOERR;
573bb37d772SDamien Le Moal break;
574bb37d772SDamien Le Moal }
575bb37d772SDamien Le Moal
576bb37d772SDamien Le Moal if (sts != BLK_STS_OK)
577bb37d772SDamien Le Moal return DM_MAPIO_KILL;
578bb37d772SDamien Le Moal
579bb37d772SDamien Le Moal return r;
580bb37d772SDamien Le Moal }
581bb37d772SDamien Le Moal
582bb37d772SDamien Le Moal /*
583bb37d772SDamien Le Moal * IO completion callback called from clone_endio().
584bb37d772SDamien Le Moal */
dm_zone_endio(struct dm_io * io,struct bio * clone)585bb37d772SDamien Le Moal void dm_zone_endio(struct dm_io *io, struct bio *clone)
586bb37d772SDamien Le Moal {
587bb37d772SDamien Le Moal struct mapped_device *md = io->md;
588de71973cSChristoph Hellwig struct gendisk *disk = md->disk;
589bb37d772SDamien Le Moal struct bio *orig_bio = io->orig_bio;
590bb37d772SDamien Le Moal unsigned int zwp_offset;
591bb37d772SDamien Le Moal unsigned int zno;
592bb37d772SDamien Le Moal
593bb37d772SDamien Le Moal /*
594bb37d772SDamien Le Moal * For targets that do not emulate zone append, we only need to
595bb37d772SDamien Le Moal * handle native zone-append bios.
596bb37d772SDamien Le Moal */
597bb37d772SDamien Le Moal if (!dm_emulate_zone_append(md)) {
598bb37d772SDamien Le Moal /*
599bb37d772SDamien Le Moal * Get the offset within the zone of the written sector
600bb37d772SDamien Le Moal * and add that to the original bio sector position.
601bb37d772SDamien Le Moal */
602bb37d772SDamien Le Moal if (clone->bi_status == BLK_STS_OK &&
603bb37d772SDamien Le Moal bio_op(clone) == REQ_OP_ZONE_APPEND) {
604de71973cSChristoph Hellwig sector_t mask =
605de71973cSChristoph Hellwig (sector_t)bdev_zone_sectors(disk->part0) - 1;
606bb37d772SDamien Le Moal
607bb37d772SDamien Le Moal orig_bio->bi_iter.bi_sector +=
608bb37d772SDamien Le Moal clone->bi_iter.bi_sector & mask;
609bb37d772SDamien Le Moal }
610bb37d772SDamien Le Moal
611bb37d772SDamien Le Moal return;
612bb37d772SDamien Le Moal }
613bb37d772SDamien Le Moal
614bb37d772SDamien Le Moal /*
615bb37d772SDamien Le Moal * For targets that do emulate zone append, if the clone BIO does not
616bb37d772SDamien Le Moal * own the target zone write lock, we have nothing to do.
617bb37d772SDamien Le Moal */
618bb37d772SDamien Le Moal if (!bio_flagged(clone, BIO_ZONE_WRITE_LOCKED))
619bb37d772SDamien Le Moal return;
620bb37d772SDamien Le Moal
621bb37d772SDamien Le Moal zno = bio_zone_no(orig_bio);
622bb37d772SDamien Le Moal
623bb37d772SDamien Le Moal if (clone->bi_status != BLK_STS_OK) {
624bb37d772SDamien Le Moal /*
625bb37d772SDamien Le Moal * BIOs that modify a zone write pointer may leave the zone
626bb37d772SDamien Le Moal * in an unknown state in case of failure (e.g. the write
627bb37d772SDamien Le Moal * pointer was only partially advanced). In this case, set
628bb37d772SDamien Le Moal * the target zone write pointer as invalid unless it is
629bb37d772SDamien Le Moal * already being updated.
630bb37d772SDamien Le Moal */
631bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno], DM_ZONE_INVALID_WP_OFST);
632bb37d772SDamien Le Moal } else if (bio_op(orig_bio) == REQ_OP_ZONE_APPEND) {
633bb37d772SDamien Le Moal /*
634bb37d772SDamien Le Moal * Get the written sector for zone append operation that were
635bb37d772SDamien Le Moal * emulated using regular write operations.
636bb37d772SDamien Le Moal */
637bb37d772SDamien Le Moal zwp_offset = READ_ONCE(md->zwp_offset[zno]);
638bb37d772SDamien Le Moal if (WARN_ON_ONCE(zwp_offset < bio_sectors(orig_bio)))
639bb37d772SDamien Le Moal WRITE_ONCE(md->zwp_offset[zno],
640bb37d772SDamien Le Moal DM_ZONE_INVALID_WP_OFST);
641bb37d772SDamien Le Moal else
642bb37d772SDamien Le Moal orig_bio->bi_iter.bi_sector +=
643bb37d772SDamien Le Moal zwp_offset - bio_sectors(orig_bio);
644bb37d772SDamien Le Moal }
645bb37d772SDamien Le Moal
646d86e716aSChristoph Hellwig dm_zone_unlock(disk, zno, clone);
6477fc18728SDamien Le Moal }
648