block: add a report_zones method
[linux-2.6-block.git] / block / blk-zoned.c
CommitLineData
6a0cb1bc
HR
1/*
2 * Zoned block device handling
3 *
4 * Copyright (c) 2015, Hannes Reinecke
5 * Copyright (c) 2015, SUSE Linux GmbH
6 *
7 * Copyright (c) 2016, Damien Le Moal
8 * Copyright (c) 2016, Western Digital
9 */
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/rbtree.h>
14#include <linux/blkdev.h>
15
a2d6b3a2
DLM
16#include "blk.h"
17
6a0cb1bc
HR
18static inline sector_t blk_zone_start(struct request_queue *q,
19 sector_t sector)
20{
f99e8648 21 sector_t zone_mask = blk_queue_zone_sectors(q) - 1;
6a0cb1bc
HR
22
23 return sector & ~zone_mask;
24}
25
6cc77e9c
CH
26/*
27 * Return true if a request is a write requests that needs zone write locking.
28 */
29bool blk_req_needs_zone_write_lock(struct request *rq)
30{
31 if (!rq->q->seq_zones_wlock)
32 return false;
33
34 if (blk_rq_is_passthrough(rq))
35 return false;
36
37 switch (req_op(rq)) {
38 case REQ_OP_WRITE_ZEROES:
39 case REQ_OP_WRITE_SAME:
40 case REQ_OP_WRITE:
41 return blk_rq_zone_is_seq(rq);
42 default:
43 return false;
44 }
45}
46EXPORT_SYMBOL_GPL(blk_req_needs_zone_write_lock);
47
48void __blk_req_zone_write_lock(struct request *rq)
49{
50 if (WARN_ON_ONCE(test_and_set_bit(blk_rq_zone_no(rq),
51 rq->q->seq_zones_wlock)))
52 return;
53
54 WARN_ON_ONCE(rq->rq_flags & RQF_ZONE_WRITE_LOCKED);
55 rq->rq_flags |= RQF_ZONE_WRITE_LOCKED;
56}
57EXPORT_SYMBOL_GPL(__blk_req_zone_write_lock);
58
59void __blk_req_zone_write_unlock(struct request *rq)
60{
61 rq->rq_flags &= ~RQF_ZONE_WRITE_LOCKED;
62 if (rq->q->seq_zones_wlock)
63 WARN_ON_ONCE(!test_and_clear_bit(blk_rq_zone_no(rq),
64 rq->q->seq_zones_wlock));
65}
66EXPORT_SYMBOL_GPL(__blk_req_zone_write_unlock);
67
a91e1380
DLM
68static inline unsigned int __blkdev_nr_zones(struct request_queue *q,
69 sector_t nr_sectors)
70{
71 unsigned long zone_sectors = blk_queue_zone_sectors(q);
72
73 return (nr_sectors + zone_sectors - 1) >> ilog2(zone_sectors);
74}
75
76/**
77 * blkdev_nr_zones - Get number of zones
78 * @bdev: Target block device
79 *
80 * Description:
81 * Return the total number of zones of a zoned block device.
82 * For a regular block device, the number of zones is always 0.
83 */
84unsigned int blkdev_nr_zones(struct block_device *bdev)
85{
86 struct request_queue *q = bdev_get_queue(bdev);
87
88 if (!blk_queue_is_zoned(q))
89 return 0;
90
91 return __blkdev_nr_zones(q, bdev->bd_part->nr_sects);
92}
93EXPORT_SYMBOL_GPL(blkdev_nr_zones);
94
6a0cb1bc 95/*
e76239a3
CH
96 * Check that a zone report belongs to this partition, and if yes, fix its start
97 * sector and write pointer and return true. Return false otherwise.
6a0cb1bc 98 */
e76239a3 99static bool blkdev_report_zone(struct block_device *bdev, struct blk_zone *rep)
6a0cb1bc
HR
100{
101 sector_t offset = get_start_sect(bdev);
102
103 if (rep->start < offset)
104 return false;
105
106 rep->start -= offset;
107 if (rep->start + rep->len > bdev->bd_part->nr_sects)
108 return false;
109
110 if (rep->type == BLK_ZONE_TYPE_CONVENTIONAL)
111 rep->wp = rep->start + rep->len;
112 else
113 rep->wp -= offset;
6a0cb1bc
HR
114 return true;
115}
116
e76239a3
CH
117static int blk_report_zones(struct gendisk *disk, sector_t sector,
118 struct blk_zone *zones, unsigned int *nr_zones,
119 gfp_t gfp_mask)
120{
121 struct request_queue *q = disk->queue;
122 unsigned int z = 0, n, nrz = *nr_zones;
123 sector_t capacity = get_capacity(disk);
124 int ret;
125
126 while (z < nrz && sector < capacity) {
127 n = nrz - z;
128 ret = disk->fops->report_zones(disk, sector, &zones[z], &n,
129 gfp_mask);
130 if (ret)
131 return ret;
132 if (!n)
133 break;
134 sector += blk_queue_zone_sectors(q) * n;
135 z += n;
136 }
137
138 WARN_ON(z > *nr_zones);
139 *nr_zones = z;
140
141 return 0;
142}
143
6a0cb1bc
HR
144/**
145 * blkdev_report_zones - Get zones information
146 * @bdev: Target block device
147 * @sector: Sector from which to report zones
148 * @zones: Array of zone structures where to return the zones information
149 * @nr_zones: Number of zone structures in the zone array
150 * @gfp_mask: Memory allocation flags (for bio_alloc)
151 *
152 * Description:
153 * Get zone information starting from the zone containing @sector.
154 * The number of zone information reported may be less than the number
155 * requested by @nr_zones. The number of zones actually reported is
156 * returned in @nr_zones.
157 */
e76239a3
CH
158int blkdev_report_zones(struct block_device *bdev, sector_t sector,
159 struct blk_zone *zones, unsigned int *nr_zones,
6a0cb1bc
HR
160 gfp_t gfp_mask)
161{
162 struct request_queue *q = bdev_get_queue(bdev);
e76239a3 163 unsigned int i, nrz;
3c4da758 164 int ret;
6a0cb1bc 165
6a0cb1bc
HR
166 if (!blk_queue_is_zoned(q))
167 return -EOPNOTSUPP;
168
e76239a3
CH
169 /*
170 * A block device that advertized itself as zoned must have a
171 * report_zones method. If it does not have one defined, the device
172 * driver has a bug. So warn about that.
173 */
174 if (WARN_ON_ONCE(!bdev->bd_disk->fops->report_zones))
175 return -EOPNOTSUPP;
6a0cb1bc 176
e76239a3 177 if (!*nr_zones || sector >= bdev->bd_part->nr_sects) {
6a0cb1bc
HR
178 *nr_zones = 0;
179 return 0;
180 }
181
e76239a3
CH
182 nrz = min(*nr_zones,
183 __blkdev_nr_zones(q, bdev->bd_part->nr_sects - sector));
184 ret = blk_report_zones(bdev->bd_disk, get_start_sect(bdev) + sector,
185 zones, &nrz, gfp_mask);
6a0cb1bc 186 if (ret)
e76239a3 187 return ret;
6a0cb1bc 188
e76239a3
CH
189 for (i = 0; i < nrz; i++) {
190 if (!blkdev_report_zone(bdev, zones))
6a0cb1bc 191 break;
e76239a3 192 zones++;
6a0cb1bc
HR
193 }
194
e76239a3 195 *nr_zones = i;
6a0cb1bc 196
e76239a3 197 return 0;
6a0cb1bc
HR
198}
199EXPORT_SYMBOL_GPL(blkdev_report_zones);
200
201/**
202 * blkdev_reset_zones - Reset zones write pointer
203 * @bdev: Target block device
204 * @sector: Start sector of the first zone to reset
205 * @nr_sectors: Number of sectors, at least the length of one zone
206 * @gfp_mask: Memory allocation flags (for bio_alloc)
207 *
208 * Description:
209 * Reset the write pointer of the zones contained in the range
210 * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
211 * is valid, but the specified range should not contain conventional zones.
212 */
213int blkdev_reset_zones(struct block_device *bdev,
214 sector_t sector, sector_t nr_sectors,
215 gfp_t gfp_mask)
216{
217 struct request_queue *q = bdev_get_queue(bdev);
218 sector_t zone_sectors;
219 sector_t end_sector = sector + nr_sectors;
a2d6b3a2
DLM
220 struct bio *bio = NULL;
221 struct blk_plug plug;
6a0cb1bc
HR
222 int ret;
223
6a0cb1bc
HR
224 if (!blk_queue_is_zoned(q))
225 return -EOPNOTSUPP;
226
a2d6b3a2
DLM
227 if (bdev_read_only(bdev))
228 return -EPERM;
229
230 if (!nr_sectors || end_sector > bdev->bd_part->nr_sects)
6a0cb1bc
HR
231 /* Out of range */
232 return -EINVAL;
233
234 /* Check alignment (handle eventual smaller last zone) */
f99e8648 235 zone_sectors = blk_queue_zone_sectors(q);
6a0cb1bc
HR
236 if (sector & (zone_sectors - 1))
237 return -EINVAL;
238
239 if ((nr_sectors & (zone_sectors - 1)) &&
240 end_sector != bdev->bd_part->nr_sects)
241 return -EINVAL;
242
a2d6b3a2 243 blk_start_plug(&plug);
6a0cb1bc
HR
244 while (sector < end_sector) {
245
a2d6b3a2 246 bio = blk_next_bio(bio, 0, gfp_mask);
6a0cb1bc 247 bio->bi_iter.bi_sector = sector;
74d46992 248 bio_set_dev(bio, bdev);
6a0cb1bc
HR
249 bio_set_op_attrs(bio, REQ_OP_ZONE_RESET, 0);
250
6a0cb1bc
HR
251 sector += zone_sectors;
252
253 /* This may take a while, so be nice to others */
254 cond_resched();
255
256 }
257
a2d6b3a2
DLM
258 ret = submit_bio_wait(bio);
259 bio_put(bio);
260
261 blk_finish_plug(&plug);
262
263 return ret;
6a0cb1bc
HR
264}
265EXPORT_SYMBOL_GPL(blkdev_reset_zones);
3ed05a98 266
56c4bddb 267/*
3ed05a98
ST
268 * BLKREPORTZONE ioctl processing.
269 * Called from blkdev_ioctl.
270 */
271int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
272 unsigned int cmd, unsigned long arg)
273{
274 void __user *argp = (void __user *)arg;
275 struct request_queue *q;
276 struct blk_zone_report rep;
277 struct blk_zone *zones;
278 int ret;
279
280 if (!argp)
281 return -EINVAL;
282
283 q = bdev_get_queue(bdev);
284 if (!q)
285 return -ENXIO;
286
287 if (!blk_queue_is_zoned(q))
288 return -ENOTTY;
289
290 if (!capable(CAP_SYS_ADMIN))
291 return -EACCES;
292
293 if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
294 return -EFAULT;
295
296 if (!rep.nr_zones)
297 return -EINVAL;
298
2e85fbaf 299 rep.nr_zones = min(blkdev_nr_zones(bdev), rep.nr_zones);
327ea4ad 300
344476e1
KC
301 zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone),
302 GFP_KERNEL | __GFP_ZERO);
3ed05a98
ST
303 if (!zones)
304 return -ENOMEM;
305
306 ret = blkdev_report_zones(bdev, rep.sector,
307 zones, &rep.nr_zones,
308 GFP_KERNEL);
309 if (ret)
310 goto out;
311
312 if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report))) {
313 ret = -EFAULT;
314 goto out;
315 }
316
317 if (rep.nr_zones) {
318 if (copy_to_user(argp + sizeof(struct blk_zone_report), zones,
319 sizeof(struct blk_zone) * rep.nr_zones))
320 ret = -EFAULT;
321 }
322
323 out:
327ea4ad 324 kvfree(zones);
3ed05a98
ST
325
326 return ret;
327}
328
56c4bddb 329/*
3ed05a98
ST
330 * BLKRESETZONE ioctl processing.
331 * Called from blkdev_ioctl.
332 */
333int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
334 unsigned int cmd, unsigned long arg)
335{
336 void __user *argp = (void __user *)arg;
337 struct request_queue *q;
338 struct blk_zone_range zrange;
339
340 if (!argp)
341 return -EINVAL;
342
343 q = bdev_get_queue(bdev);
344 if (!q)
345 return -ENXIO;
346
347 if (!blk_queue_is_zoned(q))
348 return -ENOTTY;
349
350 if (!capable(CAP_SYS_ADMIN))
351 return -EACCES;
352
353 if (!(mode & FMODE_WRITE))
354 return -EBADF;
355
356 if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
357 return -EFAULT;
358
359 return blkdev_reset_zones(bdev, zrange.sector, zrange.nr_sectors,
360 GFP_KERNEL);
361}