1a2247f19SDamien Le Moal // SPDX-License-Identifier: GPL-2.0
2a2247f19SDamien Le Moal /*
3a2247f19SDamien Le Moal * Block device concurrent positioning ranges.
4a2247f19SDamien Le Moal *
5a2247f19SDamien Le Moal * Copyright (C) 2021 Western Digital Corporation or its Affiliates.
6a2247f19SDamien Le Moal */
7a2247f19SDamien Le Moal #include <linux/kernel.h>
8a2247f19SDamien Le Moal #include <linux/blkdev.h>
9a2247f19SDamien Le Moal #include <linux/slab.h>
10a2247f19SDamien Le Moal #include <linux/init.h>
11a2247f19SDamien Le Moal
12a2247f19SDamien Le Moal #include "blk.h"
13a2247f19SDamien Le Moal
14a2247f19SDamien Le Moal static ssize_t
blk_ia_range_sector_show(struct blk_independent_access_range * iar,char * buf)15a2247f19SDamien Le Moal blk_ia_range_sector_show(struct blk_independent_access_range *iar,
16a2247f19SDamien Le Moal char *buf)
17a2247f19SDamien Le Moal {
18a2247f19SDamien Le Moal return sprintf(buf, "%llu\n", iar->sector);
19a2247f19SDamien Le Moal }
20a2247f19SDamien Le Moal
21a2247f19SDamien Le Moal static ssize_t
blk_ia_range_nr_sectors_show(struct blk_independent_access_range * iar,char * buf)22a2247f19SDamien Le Moal blk_ia_range_nr_sectors_show(struct blk_independent_access_range *iar,
23a2247f19SDamien Le Moal char *buf)
24a2247f19SDamien Le Moal {
25a2247f19SDamien Le Moal return sprintf(buf, "%llu\n", iar->nr_sectors);
26a2247f19SDamien Le Moal }
27a2247f19SDamien Le Moal
28a2247f19SDamien Le Moal struct blk_ia_range_sysfs_entry {
29a2247f19SDamien Le Moal struct attribute attr;
30a2247f19SDamien Le Moal ssize_t (*show)(struct blk_independent_access_range *iar, char *buf);
31a2247f19SDamien Le Moal };
32a2247f19SDamien Le Moal
33*3c912263SThomas Weißschuh static const struct blk_ia_range_sysfs_entry blk_ia_range_sector_entry = {
34a2247f19SDamien Le Moal .attr = { .name = "sector", .mode = 0444 },
35a2247f19SDamien Le Moal .show = blk_ia_range_sector_show,
36a2247f19SDamien Le Moal };
37a2247f19SDamien Le Moal
38*3c912263SThomas Weißschuh static const struct blk_ia_range_sysfs_entry blk_ia_range_nr_sectors_entry = {
39a2247f19SDamien Le Moal .attr = { .name = "nr_sectors", .mode = 0444 },
40a2247f19SDamien Le Moal .show = blk_ia_range_nr_sectors_show,
41a2247f19SDamien Le Moal };
42a2247f19SDamien Le Moal
43*3c912263SThomas Weißschuh static const struct attribute *const blk_ia_range_attrs[] = {
44a2247f19SDamien Le Moal &blk_ia_range_sector_entry.attr,
45a2247f19SDamien Le Moal &blk_ia_range_nr_sectors_entry.attr,
46a2247f19SDamien Le Moal NULL,
47a2247f19SDamien Le Moal };
48a2247f19SDamien Le Moal ATTRIBUTE_GROUPS(blk_ia_range);
49a2247f19SDamien Le Moal
blk_ia_range_sysfs_show(struct kobject * kobj,struct attribute * attr,char * buf)50a2247f19SDamien Le Moal static ssize_t blk_ia_range_sysfs_show(struct kobject *kobj,
51a2247f19SDamien Le Moal struct attribute *attr, char *buf)
52a2247f19SDamien Le Moal {
53a2247f19SDamien Le Moal struct blk_ia_range_sysfs_entry *entry =
54a2247f19SDamien Le Moal container_of(attr, struct blk_ia_range_sysfs_entry, attr);
55a2247f19SDamien Le Moal struct blk_independent_access_range *iar =
56a2247f19SDamien Le Moal container_of(kobj, struct blk_independent_access_range, kobj);
57a2247f19SDamien Le Moal
5841e46b3cSDamien Le Moal return entry->show(iar, buf);
59a2247f19SDamien Le Moal }
60a2247f19SDamien Le Moal
61a2247f19SDamien Le Moal static const struct sysfs_ops blk_ia_range_sysfs_ops = {
62a2247f19SDamien Le Moal .show = blk_ia_range_sysfs_show,
63a2247f19SDamien Le Moal };
64a2247f19SDamien Le Moal
65a2247f19SDamien Le Moal /*
66a2247f19SDamien Le Moal * Independent access range entries are not freed individually, but alltogether
67a2247f19SDamien Le Moal * with struct blk_independent_access_ranges and its array of ranges. Since
68a2247f19SDamien Le Moal * kobject_add() takes a reference on the parent kobject contained in
69a2247f19SDamien Le Moal * struct blk_independent_access_ranges, the array of independent access range
70a2247f19SDamien Le Moal * entries cannot be freed until kobject_del() is called for all entries.
71a2247f19SDamien Le Moal * So we do not need to do anything here, but still need this no-op release
72a2247f19SDamien Le Moal * operation to avoid complaints from the kobject code.
73a2247f19SDamien Le Moal */
blk_ia_range_sysfs_nop_release(struct kobject * kobj)74a2247f19SDamien Le Moal static void blk_ia_range_sysfs_nop_release(struct kobject *kobj)
75a2247f19SDamien Le Moal {
76a2247f19SDamien Le Moal }
77a2247f19SDamien Le Moal
785f622417SThomas Weißschuh static const struct kobj_type blk_ia_range_ktype = {
79a2247f19SDamien Le Moal .sysfs_ops = &blk_ia_range_sysfs_ops,
80a2247f19SDamien Le Moal .default_groups = blk_ia_range_groups,
81a2247f19SDamien Le Moal .release = blk_ia_range_sysfs_nop_release,
82a2247f19SDamien Le Moal };
83a2247f19SDamien Le Moal
84a2247f19SDamien Le Moal /*
85a2247f19SDamien Le Moal * This will be executed only after all independent access range entries are
86a2247f19SDamien Le Moal * removed with kobject_del(), at which point, it is safe to free everything,
87a2247f19SDamien Le Moal * including the array of ranges.
88a2247f19SDamien Le Moal */
blk_ia_ranges_sysfs_release(struct kobject * kobj)89a2247f19SDamien Le Moal static void blk_ia_ranges_sysfs_release(struct kobject *kobj)
90a2247f19SDamien Le Moal {
91a2247f19SDamien Le Moal struct blk_independent_access_ranges *iars =
92a2247f19SDamien Le Moal container_of(kobj, struct blk_independent_access_ranges, kobj);
93a2247f19SDamien Le Moal
94a2247f19SDamien Le Moal kfree(iars);
95a2247f19SDamien Le Moal }
96a2247f19SDamien Le Moal
975f622417SThomas Weißschuh static const struct kobj_type blk_ia_ranges_ktype = {
98a2247f19SDamien Le Moal .release = blk_ia_ranges_sysfs_release,
99a2247f19SDamien Le Moal };
100a2247f19SDamien Le Moal
101a2247f19SDamien Le Moal /**
102438cd742SJens Axboe * disk_register_independent_access_ranges - register with sysfs a set of
103438cd742SJens Axboe * independent access ranges
104a2247f19SDamien Le Moal * @disk: Target disk
105a2247f19SDamien Le Moal *
106a2247f19SDamien Le Moal * Register with sysfs a set of independent access ranges for @disk.
107a2247f19SDamien Le Moal */
disk_register_independent_access_ranges(struct gendisk * disk)10822d0c408SChristoph Hellwig int disk_register_independent_access_ranges(struct gendisk *disk)
109a2247f19SDamien Le Moal {
11022d0c408SChristoph Hellwig struct blk_independent_access_ranges *iars = disk->ia_ranges;
111a2247f19SDamien Le Moal struct request_queue *q = disk->queue;
112a2247f19SDamien Le Moal int i, ret;
113a2247f19SDamien Le Moal
114a2247f19SDamien Le Moal lockdep_assert_held(&q->sysfs_lock);
115a2247f19SDamien Le Moal
116a2247f19SDamien Le Moal if (!iars)
117a2247f19SDamien Le Moal return 0;
118a2247f19SDamien Le Moal
119a2247f19SDamien Le Moal /*
120a2247f19SDamien Le Moal * At this point, iars is the new set of sector access ranges that needs
121a2247f19SDamien Le Moal * to be registered with sysfs.
122a2247f19SDamien Le Moal */
123a2247f19SDamien Le Moal WARN_ON(iars->sysfs_registered);
124a2247f19SDamien Le Moal ret = kobject_init_and_add(&iars->kobj, &blk_ia_ranges_ktype,
1252bd85221SChristoph Hellwig &disk->queue_kobj, "%s",
1262bd85221SChristoph Hellwig "independent_access_ranges");
127a2247f19SDamien Le Moal if (ret) {
1286a27d28cSChristoph Hellwig disk->ia_ranges = NULL;
12983114df3SMiaoqian Lin kobject_put(&iars->kobj);
130a2247f19SDamien Le Moal return ret;
131a2247f19SDamien Le Moal }
132a2247f19SDamien Le Moal
133a2247f19SDamien Le Moal for (i = 0; i < iars->nr_ia_ranges; i++) {
134a2247f19SDamien Le Moal ret = kobject_init_and_add(&iars->ia_range[i].kobj,
135a2247f19SDamien Le Moal &blk_ia_range_ktype, &iars->kobj,
136a2247f19SDamien Le Moal "%d", i);
137a2247f19SDamien Le Moal if (ret) {
138a2247f19SDamien Le Moal while (--i >= 0)
139a2247f19SDamien Le Moal kobject_del(&iars->ia_range[i].kobj);
140a2247f19SDamien Le Moal kobject_del(&iars->kobj);
141a2247f19SDamien Le Moal kobject_put(&iars->kobj);
142a2247f19SDamien Le Moal return ret;
143a2247f19SDamien Le Moal }
144a2247f19SDamien Le Moal }
145a2247f19SDamien Le Moal
146a2247f19SDamien Le Moal iars->sysfs_registered = true;
147a2247f19SDamien Le Moal
148a2247f19SDamien Le Moal return 0;
149a2247f19SDamien Le Moal }
150a2247f19SDamien Le Moal
disk_unregister_independent_access_ranges(struct gendisk * disk)151a2247f19SDamien Le Moal void disk_unregister_independent_access_ranges(struct gendisk *disk)
152a2247f19SDamien Le Moal {
153a2247f19SDamien Le Moal struct request_queue *q = disk->queue;
1546a27d28cSChristoph Hellwig struct blk_independent_access_ranges *iars = disk->ia_ranges;
155a2247f19SDamien Le Moal int i;
156a2247f19SDamien Le Moal
157a2247f19SDamien Le Moal lockdep_assert_held(&q->sysfs_lock);
158a2247f19SDamien Le Moal
159a2247f19SDamien Le Moal if (!iars)
160a2247f19SDamien Le Moal return;
161a2247f19SDamien Le Moal
162a2247f19SDamien Le Moal if (iars->sysfs_registered) {
163a2247f19SDamien Le Moal for (i = 0; i < iars->nr_ia_ranges; i++)
164a2247f19SDamien Le Moal kobject_del(&iars->ia_range[i].kobj);
165a2247f19SDamien Le Moal kobject_del(&iars->kobj);
166a2247f19SDamien Le Moal kobject_put(&iars->kobj);
167a2247f19SDamien Le Moal } else {
168a2247f19SDamien Le Moal kfree(iars);
169a2247f19SDamien Le Moal }
170a2247f19SDamien Le Moal
1716a27d28cSChristoph Hellwig disk->ia_ranges = NULL;
172a2247f19SDamien Le Moal }
173a2247f19SDamien Le Moal
174a2247f19SDamien Le Moal static struct blk_independent_access_range *
disk_find_ia_range(struct blk_independent_access_ranges * iars,sector_t sector)175a2247f19SDamien Le Moal disk_find_ia_range(struct blk_independent_access_ranges *iars,
176a2247f19SDamien Le Moal sector_t sector)
177a2247f19SDamien Le Moal {
178a2247f19SDamien Le Moal struct blk_independent_access_range *iar;
179a2247f19SDamien Le Moal int i;
180a2247f19SDamien Le Moal
181a2247f19SDamien Le Moal for (i = 0; i < iars->nr_ia_ranges; i++) {
182a2247f19SDamien Le Moal iar = &iars->ia_range[i];
183a2247f19SDamien Le Moal if (sector >= iar->sector &&
184a2247f19SDamien Le Moal sector < iar->sector + iar->nr_sectors)
185a2247f19SDamien Le Moal return iar;
186a2247f19SDamien Le Moal }
187a2247f19SDamien Le Moal
188a2247f19SDamien Le Moal return NULL;
189a2247f19SDamien Le Moal }
190a2247f19SDamien Le Moal
disk_check_ia_ranges(struct gendisk * disk,struct blk_independent_access_ranges * iars)191a2247f19SDamien Le Moal static bool disk_check_ia_ranges(struct gendisk *disk,
192a2247f19SDamien Le Moal struct blk_independent_access_ranges *iars)
193a2247f19SDamien Le Moal {
194a2247f19SDamien Le Moal struct blk_independent_access_range *iar, *tmp;
195a2247f19SDamien Le Moal sector_t capacity = get_capacity(disk);
196a2247f19SDamien Le Moal sector_t sector = 0;
197a2247f19SDamien Le Moal int i;
198a2247f19SDamien Le Moal
19922d0c408SChristoph Hellwig if (WARN_ON_ONCE(!iars->nr_ia_ranges))
20022d0c408SChristoph Hellwig return false;
20122d0c408SChristoph Hellwig
202a2247f19SDamien Le Moal /*
203a2247f19SDamien Le Moal * While sorting the ranges in increasing LBA order, check that the
204a2247f19SDamien Le Moal * ranges do not overlap, that there are no sector holes and that all
205a2247f19SDamien Le Moal * sectors belong to one range.
206a2247f19SDamien Le Moal */
207a2247f19SDamien Le Moal for (i = 0; i < iars->nr_ia_ranges; i++) {
208a2247f19SDamien Le Moal tmp = disk_find_ia_range(iars, sector);
209a2247f19SDamien Le Moal if (!tmp || tmp->sector != sector) {
210a2247f19SDamien Le Moal pr_warn("Invalid non-contiguous independent access ranges\n");
211a2247f19SDamien Le Moal return false;
212a2247f19SDamien Le Moal }
213a2247f19SDamien Le Moal
214a2247f19SDamien Le Moal iar = &iars->ia_range[i];
215a2247f19SDamien Le Moal if (tmp != iar) {
216a2247f19SDamien Le Moal swap(iar->sector, tmp->sector);
217a2247f19SDamien Le Moal swap(iar->nr_sectors, tmp->nr_sectors);
218a2247f19SDamien Le Moal }
219a2247f19SDamien Le Moal
220a2247f19SDamien Le Moal sector += iar->nr_sectors;
221a2247f19SDamien Le Moal }
222a2247f19SDamien Le Moal
223a2247f19SDamien Le Moal if (sector != capacity) {
224a2247f19SDamien Le Moal pr_warn("Independent access ranges do not match disk capacity\n");
225a2247f19SDamien Le Moal return false;
226a2247f19SDamien Le Moal }
227a2247f19SDamien Le Moal
228a2247f19SDamien Le Moal return true;
229a2247f19SDamien Le Moal }
230a2247f19SDamien Le Moal
disk_ia_ranges_changed(struct gendisk * disk,struct blk_independent_access_ranges * new)231a2247f19SDamien Le Moal static bool disk_ia_ranges_changed(struct gendisk *disk,
232a2247f19SDamien Le Moal struct blk_independent_access_ranges *new)
233a2247f19SDamien Le Moal {
2346a27d28cSChristoph Hellwig struct blk_independent_access_ranges *old = disk->ia_ranges;
235a2247f19SDamien Le Moal int i;
236a2247f19SDamien Le Moal
237a2247f19SDamien Le Moal if (!old)
238a2247f19SDamien Le Moal return true;
239a2247f19SDamien Le Moal
240a2247f19SDamien Le Moal if (old->nr_ia_ranges != new->nr_ia_ranges)
241a2247f19SDamien Le Moal return true;
242a2247f19SDamien Le Moal
243a2247f19SDamien Le Moal for (i = 0; i < old->nr_ia_ranges; i++) {
244a2247f19SDamien Le Moal if (new->ia_range[i].sector != old->ia_range[i].sector ||
245a2247f19SDamien Le Moal new->ia_range[i].nr_sectors != old->ia_range[i].nr_sectors)
246a2247f19SDamien Le Moal return true;
247a2247f19SDamien Le Moal }
248a2247f19SDamien Le Moal
249a2247f19SDamien Le Moal return false;
250a2247f19SDamien Le Moal }
251a2247f19SDamien Le Moal
252a2247f19SDamien Le Moal /**
253a2247f19SDamien Le Moal * disk_alloc_independent_access_ranges - Allocate an independent access ranges
254a2247f19SDamien Le Moal * data structure
255a2247f19SDamien Le Moal * @disk: target disk
256a2247f19SDamien Le Moal * @nr_ia_ranges: Number of independent access ranges
257a2247f19SDamien Le Moal *
258a2247f19SDamien Le Moal * Allocate a struct blk_independent_access_ranges structure with @nr_ia_ranges
259a2247f19SDamien Le Moal * access range descriptors.
260a2247f19SDamien Le Moal */
261a2247f19SDamien Le Moal struct blk_independent_access_ranges *
disk_alloc_independent_access_ranges(struct gendisk * disk,int nr_ia_ranges)262a2247f19SDamien Le Moal disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges)
263a2247f19SDamien Le Moal {
264a2247f19SDamien Le Moal struct blk_independent_access_ranges *iars;
265a2247f19SDamien Le Moal
266a2247f19SDamien Le Moal iars = kzalloc_node(struct_size(iars, ia_range, nr_ia_ranges),
267a2247f19SDamien Le Moal GFP_KERNEL, disk->queue->node);
268a2247f19SDamien Le Moal if (iars)
269a2247f19SDamien Le Moal iars->nr_ia_ranges = nr_ia_ranges;
270a2247f19SDamien Le Moal return iars;
271a2247f19SDamien Le Moal }
272a2247f19SDamien Le Moal EXPORT_SYMBOL_GPL(disk_alloc_independent_access_ranges);
273a2247f19SDamien Le Moal
274a2247f19SDamien Le Moal /**
275a2247f19SDamien Le Moal * disk_set_independent_access_ranges - Set a disk independent access ranges
276a2247f19SDamien Le Moal * @disk: target disk
277a2247f19SDamien Le Moal * @iars: independent access ranges structure
278a2247f19SDamien Le Moal *
279a2247f19SDamien Le Moal * Set the independent access ranges information of the request queue
280a2247f19SDamien Le Moal * of @disk to @iars. If @iars is NULL and the independent access ranges
281a2247f19SDamien Le Moal * structure already set is cleared. If there are no differences between
282a2247f19SDamien Le Moal * @iars and the independent access ranges structure already set, @iars
283a2247f19SDamien Le Moal * is freed.
284a2247f19SDamien Le Moal */
disk_set_independent_access_ranges(struct gendisk * disk,struct blk_independent_access_ranges * iars)285a2247f19SDamien Le Moal void disk_set_independent_access_ranges(struct gendisk *disk,
286a2247f19SDamien Le Moal struct blk_independent_access_ranges *iars)
287a2247f19SDamien Le Moal {
288a2247f19SDamien Le Moal struct request_queue *q = disk->queue;
289a2247f19SDamien Le Moal
290a2247f19SDamien Le Moal mutex_lock(&q->sysfs_lock);
29122d0c408SChristoph Hellwig if (iars && !disk_check_ia_ranges(disk, iars)) {
292a2247f19SDamien Le Moal kfree(iars);
293a2247f19SDamien Le Moal iars = NULL;
294a2247f19SDamien Le Moal }
29522d0c408SChristoph Hellwig if (iars && !disk_ia_ranges_changed(disk, iars)) {
296a2247f19SDamien Le Moal kfree(iars);
297a2247f19SDamien Le Moal goto unlock;
298a2247f19SDamien Le Moal }
299a2247f19SDamien Le Moal
300a2247f19SDamien Le Moal /*
301a2247f19SDamien Le Moal * This may be called for a registered queue. E.g. during a device
302a2247f19SDamien Le Moal * revalidation. If that is the case, we need to unregister the old
303a2247f19SDamien Le Moal * set of independent access ranges and register the new set. If the
304a2247f19SDamien Le Moal * queue is not registered, registration of the device request queue
30522d0c408SChristoph Hellwig * will register the independent access ranges.
306a2247f19SDamien Le Moal */
30722d0c408SChristoph Hellwig disk_unregister_independent_access_ranges(disk);
30822d0c408SChristoph Hellwig disk->ia_ranges = iars;
30922d0c408SChristoph Hellwig if (blk_queue_registered(q))
31022d0c408SChristoph Hellwig disk_register_independent_access_ranges(disk);
311a2247f19SDamien Le Moal unlock:
312a2247f19SDamien Le Moal mutex_unlock(&q->sysfs_lock);
313a2247f19SDamien Le Moal }
314a2247f19SDamien Le Moal EXPORT_SYMBOL_GPL(disk_set_independent_access_ranges);
315